|
| 1 | +package avcodec |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + // Packages |
| 8 | + media "github.com/mutablelogic/go-media" |
| 9 | + metadata "github.com/mutablelogic/go-media/pkg/metadata" |
| 10 | + ff "github.com/mutablelogic/go-media/sys/ffmpeg71" |
| 11 | +) |
| 12 | + |
| 13 | +/////////////////////////////////////////////////////////////////////////////// |
| 14 | +// TYPES |
| 15 | + |
| 16 | +type Codec struct { |
| 17 | + codec *ff.AVCodec |
| 18 | + context *ff.AVCodecContext |
| 19 | +} |
| 20 | + |
| 21 | +/////////////////////////////////////////////////////////////////////////////// |
| 22 | +// LIFECYCLE |
| 23 | + |
| 24 | +// Return all codecs. Codecs can be either encoders or decoders. The argument |
| 25 | +// can be ANY or any combination of INPUT, OUTPUT, VIDEO, AUDIO and SUBTITLE, |
| 26 | +// in order to return a subset of codecs. |
| 27 | +func Codecs(t media.Type) []media.Metadata { |
| 28 | + result := make([]media.Metadata, 0, 100) |
| 29 | + var opaque uintptr |
| 30 | + for { |
| 31 | + codec := ff.AVCodec_iterate(&opaque) |
| 32 | + if codec == nil { |
| 33 | + break |
| 34 | + } |
| 35 | + // Filter codecs by type |
| 36 | + if t.Is(media.INPUT) && !ff.AVCodec_is_decoder(codec) { |
| 37 | + continue |
| 38 | + } |
| 39 | + if t.Is(media.OUTPUT) && !ff.AVCodec_is_encoder(codec) { |
| 40 | + continue |
| 41 | + } |
| 42 | + if t.Is(media.VIDEO) && codec.Type() != ff.AVMEDIA_TYPE_VIDEO { |
| 43 | + continue |
| 44 | + } |
| 45 | + if t.Is(media.AUDIO) && codec.Type() != ff.AVMEDIA_TYPE_AUDIO { |
| 46 | + continue |
| 47 | + } |
| 48 | + if t.Is(media.SUBTITLE) && codec.Type() != ff.AVMEDIA_TYPE_SUBTITLE { |
| 49 | + continue |
| 50 | + } |
| 51 | + if codec.Capabilities().Is(ff.AV_CODEC_CAP_EXPERIMENTAL) { |
| 52 | + // Skip experimental codecs |
| 53 | + continue |
| 54 | + } |
| 55 | + result = append(result, metadata.New(codec.Name(), &Codec{codec, nil})) |
| 56 | + } |
| 57 | + return result |
| 58 | +} |
| 59 | + |
| 60 | +// NewEncodingCodec returns an encoder by name. Options for encoding can be passed. |
| 61 | +// Call Close() to release the codec. Codec options are listed at |
| 62 | +// <https://ffmpeg.org/ffmpeg-codecs.html> |
| 63 | +func NewEncodingCodec(name string, opts ...Opt) (*Codec, error) { |
| 64 | + return newCodec(ff.AVCodec_find_encoder_by_name(name), opts...) |
| 65 | +} |
| 66 | + |
| 67 | +// NewDecodingCodec returns a decoder by name. Options for decoding can be passed. |
| 68 | +// Call Close() to release the codec context. Codec options are listed at |
| 69 | +// <https://ffmpeg.org/ffmpeg-codecs.html> |
| 70 | +func NewDecodingCodec(name string, opts ...Opt) (*Codec, error) { |
| 71 | + return newCodec(ff.AVCodec_find_decoder_by_name(name), opts...) |
| 72 | +} |
| 73 | + |
| 74 | +func newCodec(codec *ff.AVCodec, opts ...Opt) (*Codec, error) { |
| 75 | + ctx := new(Codec) |
| 76 | + |
| 77 | + // Check parameters |
| 78 | + if codec == nil { |
| 79 | + return nil, media.ErrBadParameter.Withf("unknown codec %q", codec.Name()) |
| 80 | + } |
| 81 | + |
| 82 | + // Apply options |
| 83 | + o, err := applyOptions(opts) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + // Create a dictionary of codec options |
| 89 | + dict := ff.AVUtil_dict_alloc() |
| 90 | + defer ff.AVUtil_dict_free(dict) |
| 91 | + for _, opt := range o.meta { |
| 92 | + if err := ff.AVUtil_dict_set(dict, opt.Key(), opt.Value(), ff.AV_DICT_APPEND); err != nil { |
| 93 | + ff.AVUtil_dict_free(dict) |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Codec context |
| 99 | + if context := ff.AVCodec_alloc_context(codec); context == nil { |
| 100 | + return nil, media.ErrInternalError.Withf("failed to allocate codec context for %q", codec.Name()) |
| 101 | + } else if err := set_par(context, codec, o); err != nil { |
| 102 | + ff.AVCodec_free_context(context) |
| 103 | + return nil, err |
| 104 | + } else if err := ff.AVCodec_open(context, codec, dict); err != nil { |
| 105 | + ff.AVCodec_free_context(context) |
| 106 | + return nil, err |
| 107 | + } else { |
| 108 | + ctx.context = context |
| 109 | + } |
| 110 | + |
| 111 | + // TODO: Get the options which were not consumed |
| 112 | + fmt.Println("TODO: Codec options not consumed:", dict) |
| 113 | + |
| 114 | + // Return success |
| 115 | + return ctx, nil |
| 116 | +} |
| 117 | + |
| 118 | +// Release the codec resources |
| 119 | +func (ctx *Codec) Close() error { |
| 120 | + ctx.codec = nil |
| 121 | + if ctx != nil && ctx.context != nil { |
| 122 | + ff.AVCodec_free_context(ctx.context) |
| 123 | + ctx.context = nil |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +//////////////////////////////////////////////////////////////////////////////// |
| 129 | +// STRINGIFY |
| 130 | + |
| 131 | +func (ctx *Codec) MarshalJSON() ([]byte, error) { |
| 132 | + if ctx != nil && ctx.codec != nil { |
| 133 | + return ctx.codec.MarshalJSON() |
| 134 | + } |
| 135 | + if ctx != nil && ctx.context != nil { |
| 136 | + return ctx.context.MarshalJSON() |
| 137 | + } |
| 138 | + return []byte("null"), nil |
| 139 | +} |
| 140 | + |
| 141 | +func (ctx *Codec) String() string { |
| 142 | + data, err := json.MarshalIndent(ctx, "", " ") |
| 143 | + if err != nil { |
| 144 | + return err.Error() |
| 145 | + } |
| 146 | + return string(data) |
| 147 | +} |
| 148 | + |
| 149 | +//////////////////////////////////////////////////////////////////////////////// |
| 150 | +// PUBLIC METHODS |
| 151 | + |
| 152 | +func (ctx *Codec) Type() media.Type { |
| 153 | + var t media.Type |
| 154 | + switch { |
| 155 | + case ctx != nil && ctx.codec != nil: |
| 156 | + if ff.AVCodec_is_decoder(ctx.codec) { |
| 157 | + t |= media.INPUT |
| 158 | + } |
| 159 | + if ff.AVCodec_is_encoder(ctx.codec) { |
| 160 | + t |= media.OUTPUT |
| 161 | + } |
| 162 | + t |= type2type(ctx.codec.Type()) |
| 163 | + case ctx != nil && ctx.context != nil: |
| 164 | + if ff.AVCodec_is_decoder(ctx.context.Codec()) { |
| 165 | + t |= media.INPUT |
| 166 | + } |
| 167 | + if ff.AVCodec_is_encoder(ctx.context.Codec()) { |
| 168 | + t |= media.OUTPUT |
| 169 | + } |
| 170 | + t |= type2type(ctx.context.Codec().Type()) |
| 171 | + } |
| 172 | + return t |
| 173 | +} |
| 174 | + |
| 175 | +func (ctx *Codec) Name() string { |
| 176 | + switch { |
| 177 | + case ctx != nil && ctx.codec != nil: |
| 178 | + return ctx.codec.Name() |
| 179 | + case ctx != nil && ctx.context != nil: |
| 180 | + return ctx.context.Codec().Name() |
| 181 | + } |
| 182 | + return "" |
| 183 | +} |
| 184 | + |
| 185 | +//////////////////////////////////////////////////////////////////////////////// |
| 186 | +// PRIVATE METHODS |
| 187 | + |
| 188 | +func type2type(t ff.AVMediaType) media.Type { |
| 189 | + switch t { |
| 190 | + case ff.AVMEDIA_TYPE_AUDIO: |
| 191 | + return media.AUDIO |
| 192 | + case ff.AVMEDIA_TYPE_VIDEO: |
| 193 | + return media.VIDEO |
| 194 | + case ff.AVMEDIA_TYPE_SUBTITLE: |
| 195 | + return media.SUBTITLE |
| 196 | + case ff.AVMEDIA_TYPE_ATTACHMENT: |
| 197 | + return media.DATA |
| 198 | + case ff.AVMEDIA_TYPE_DATA: |
| 199 | + return media.DATA |
| 200 | + } |
| 201 | + return media.NONE |
| 202 | +} |
| 203 | + |
| 204 | +func set_par(ctx *ff.AVCodecContext, codec *ff.AVCodec, opt *opt) error { |
| 205 | + switch codec.Type() { |
| 206 | + case ff.AVMEDIA_TYPE_AUDIO: |
| 207 | + return set_audio_par(ctx, codec, opt) |
| 208 | + case ff.AVMEDIA_TYPE_VIDEO: |
| 209 | + return set_video_par(ctx, codec, opt) |
| 210 | + case ff.AVMEDIA_TYPE_SUBTITLE: |
| 211 | + return set_subtitle_par(ctx, codec, opt) |
| 212 | + } |
| 213 | + return nil |
| 214 | +} |
| 215 | + |
| 216 | +func set_audio_par(ctx *ff.AVCodecContext, codec *ff.AVCodec, opt *opt) error { |
| 217 | + // Channel layout |
| 218 | + if ff.AVUtil_channel_layout_check(&opt.channel_layout) { |
| 219 | + ctx.SetChannelLayout(opt.channel_layout) |
| 220 | + } else if supported_layouts := codec.ChannelLayouts(); len(supported_layouts) > 0 { |
| 221 | + ctx.SetChannelLayout(supported_layouts[0]) |
| 222 | + } else { |
| 223 | + ctx.SetChannelLayout(ff.AV_CHANNEL_LAYOUT_MONO) |
| 224 | + } |
| 225 | + |
| 226 | + // Sample format |
| 227 | + if opt.sample_format != ff.AV_SAMPLE_FMT_NONE { |
| 228 | + ctx.SetSampleFormat(opt.sample_format) |
| 229 | + } else if supported_formats := codec.SampleFormats(); len(supported_formats) > 0 { |
| 230 | + ctx.SetSampleFormat(supported_formats[0]) |
| 231 | + } |
| 232 | + |
| 233 | + // Sample rate |
| 234 | + if opt.sample_rate > 0 { |
| 235 | + ctx.SetSampleRate(opt.sample_rate) |
| 236 | + } else if supported_rates := codec.SupportedSamplerates(); len(supported_rates) > 0 { |
| 237 | + ctx.SetSampleRate(supported_rates[0]) |
| 238 | + } |
| 239 | + |
| 240 | + // TODO: Time base |
| 241 | + ctx.SetTimeBase(ff.AVUtil_rational(1, ctx.SampleRate())) |
| 242 | + |
| 243 | + return nil |
| 244 | +} |
| 245 | + |
| 246 | +func set_video_par(ctx *ff.AVCodecContext, codec *ff.AVCodec, opt *opt) error { |
| 247 | + // Pixel Format |
| 248 | + if opt.pixel_format != ff.AV_PIX_FMT_NONE { |
| 249 | + ctx.SetPixFmt(opt.pixel_format) |
| 250 | + } else if supported_formats := codec.PixelFormats(); len(supported_formats) > 0 { |
| 251 | + ctx.SetPixFmt(supported_formats[0]) |
| 252 | + } else { |
| 253 | + ctx.SetPixFmt(ff.AV_PIX_FMT_YUV420P) |
| 254 | + } |
| 255 | + |
| 256 | + // Frame size |
| 257 | + if opt.width > 0 { |
| 258 | + ctx.SetWidth(opt.width) |
| 259 | + } |
| 260 | + if opt.height > 0 { |
| 261 | + ctx.SetHeight(opt.height) |
| 262 | + } |
| 263 | + |
| 264 | + // Frame rate |
| 265 | + if !opt.frame_rate.IsZero() { |
| 266 | + ctx.SetFramerate(opt.frame_rate) |
| 267 | + } else if supported_rates := codec.SupportedFramerates(); len(supported_rates) > 0 { |
| 268 | + ctx.SetFramerate(supported_rates[0]) |
| 269 | + } |
| 270 | + |
| 271 | + // Time base |
| 272 | + if frame_rate := ctx.Framerate(); !frame_rate.IsZero() { |
| 273 | + ctx.SetTimeBase(ff.AVUtil_rational_invert(frame_rate)) |
| 274 | + } |
| 275 | + |
| 276 | + return nil |
| 277 | +} |
| 278 | + |
| 279 | +func set_subtitle_par(ctx *ff.AVCodecContext, codec *ff.AVCodec, opt *opt) error { |
| 280 | + return nil |
| 281 | +} |
0 commit comments