@@ -2,9 +2,9 @@ package avcodec
22
33import (
44 "encoding/json"
5+ "fmt"
56
67 // Packages
7-
88 media "github.com/mutablelogic/go-media"
99 metadata "github.com/mutablelogic/go-media/pkg/metadata"
1010 ff "github.com/mutablelogic/go-media/sys/ffmpeg71"
@@ -57,64 +57,60 @@ func Codecs(t media.Type) []media.Metadata {
5757 return result
5858}
5959
60- // Return an encoder by name, with additional options. Call Close() to
61- // release the codec context . Codec options are listed at
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
6262// <https://ffmpeg.org/ffmpeg-codecs.html>
63- func NewEncoder (name string , opts ... Opt ) (* Codec , error ) {
64- ctx := new (Codec )
65-
66- // Options
67- o , err := applyOptions (opts )
68- if err != nil {
69- return nil , err
70- }
71-
72- // Codec context
73- if codec := ff .AVCodec_find_encoder_by_name (name ); codec == nil {
74- return nil , media .ErrBadParameter .Withf ("unknown codec %q" , name )
75- } else if context := ff .AVCodec_alloc_context (codec ); context == nil {
76- return nil , media .ErrInternalError .Withf ("failed to allocate codec context for %q" , name )
77- } else if err := set_par (context , codec , o ); err != nil {
78- ff .AVCodec_free_context (context )
79- return nil , err
80- } else if ff .AVCodec_open (context , codec , nil ); err != nil {
81- ff .AVCodec_free_context (context )
82- return nil , err
83- } else {
84- ctx .context = context
85- }
86-
87- // Return success
88- return ctx , nil
63+ func NewEncodingCodec (name string , opts ... Opt ) (* Codec , error ) {
64+ return newCodec (ff .AVCodec_find_encoder_by_name (name ), opts ... )
8965}
9066
91- // Return a decoder by name, with additional options. Call Close() to
92- // release the codec context. Codec options are listed at
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
9369// <https://ffmpeg.org/ffmpeg-codecs.html>
94- func NewDecoder (name string , opts ... Opt ) (* Codec , error ) {
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 ) {
9575 ctx := new (Codec )
9676
97- // Options
77+ // Check parameters
78+ if codec == nil {
79+ return nil , media .ErrBadParameter .Withf ("unknown codec %q" , codec .Name ())
80+ }
81+
82+ // Apply options
9883 o , err := applyOptions (opts )
9984 if err != nil {
10085 return nil , err
10186 }
10287
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+
10398 // Codec context
104- if codec := ff .AVCodec_find_decoder_by_name (name ); codec == nil {
105- return nil , media .ErrBadParameter .Withf ("unknown codec %q" , name )
106- } else if context := ff .AVCodec_alloc_context (codec ); context == nil {
107- return nil , media .ErrInternalError .Withf ("failed to allocate codec context for %q" , name )
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 ())
108101 } else if err := set_par (context , codec , o ); err != nil {
109102 ff .AVCodec_free_context (context )
110103 return nil , err
111- } else if ff .AVCodec_open (context , codec , nil ); err != nil {
104+ } else if err := ff .AVCodec_open (context , codec , dict ); err != nil {
112105 ff .AVCodec_free_context (context )
113106 return nil , err
114107 } else {
115108 ctx .context = context
116109 }
117110
111+ // TODO: Get the options which were not consumed
112+ fmt .Println ("TODO: Codec options not consumed:" , dict )
113+
118114 // Return success
119115 return ctx , nil
120116}
0 commit comments