|
| 1 | +package ffmpeg |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "syscall" |
| 9 | + |
| 10 | + // Packages |
| 11 | + ff "github.com/mutablelogic/go-media/sys/ffmpeg61" |
| 12 | + |
| 13 | + // Namespace imports |
| 14 | + . "github.com/djthorpe/go-errors" |
| 15 | +) |
| 16 | + |
| 17 | +//////////////////////////////////////////////////////////////////////////////// |
| 18 | +// TYPES |
| 19 | + |
| 20 | +type Encoder struct { |
| 21 | + ctx *ff.AVCodecContext |
| 22 | + stream *ff.AVStream |
| 23 | + packet *ff.AVPacket |
| 24 | + //next_pts int64 |
| 25 | +} |
| 26 | + |
| 27 | +// EncoderFrameFn is a function which is called to receive a frame to encode. It should |
| 28 | +// return nil to continue encoding or io.EOF to stop encoding. |
| 29 | +type EncoderFrameFn func(int) (*ff.AVFrame, error) |
| 30 | + |
| 31 | +// EncoderPacketFn is a function which is called for each packet encoded. It should |
| 32 | +// return nil to continue encoding or io.EOF to stop encoding immediately. |
| 33 | +type EncoderPacketFn func(*ff.AVPacket) error |
| 34 | + |
| 35 | +//////////////////////////////////////////////////////////////////////////////// |
| 36 | +// LIFECYCLE |
| 37 | + |
| 38 | +// Create an encoder with the given parameters |
| 39 | +func NewEncoder(ctx *ff.AVFormatContext, stream int, par *Par) (*Encoder, error) { |
| 40 | + encoder := new(Encoder) |
| 41 | + |
| 42 | + // Get codec |
| 43 | + codec_id := ff.AV_CODEC_ID_NONE |
| 44 | + switch par.CodecType() { |
| 45 | + case ff.AVMEDIA_TYPE_AUDIO: |
| 46 | + codec_id = ctx.Output().AudioCodec() |
| 47 | + case ff.AVMEDIA_TYPE_VIDEO: |
| 48 | + codec_id = ctx.Output().VideoCodec() |
| 49 | + case ff.AVMEDIA_TYPE_SUBTITLE: |
| 50 | + codec_id = ctx.Output().SubtitleCodec() |
| 51 | + } |
| 52 | + if codec_id == ff.AV_CODEC_ID_NONE { |
| 53 | + return nil, ErrBadParameter.Withf("no codec specified for stream %v", stream) |
| 54 | + } |
| 55 | + |
| 56 | + // Allocate codec |
| 57 | + codec := ff.AVCodec_find_encoder(codec_id) |
| 58 | + if codec == nil { |
| 59 | + return nil, ErrBadParameter.Withf("codec %q cannot encode", codec_id) |
| 60 | + } |
| 61 | + if codecctx := ff.AVCodec_alloc_context(codec); codecctx == nil { |
| 62 | + return nil, ErrInternalAppError.With("could not allocate audio codec context") |
| 63 | + } else { |
| 64 | + encoder.ctx = codecctx |
| 65 | + } |
| 66 | + |
| 67 | + // Check codec against parameters and set defaults as needed, then |
| 68 | + // copy back to codec |
| 69 | + if err := par.ValidateFromCodec(encoder.ctx); err != nil { |
| 70 | + ff.AVCodec_free_context(encoder.ctx) |
| 71 | + return nil, err |
| 72 | + } else if err := par.CopyToCodec(encoder.ctx); err != nil { |
| 73 | + ff.AVCodec_free_context(encoder.ctx) |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + // Create the stream |
| 78 | + if streamctx := ff.AVFormat_new_stream(ctx, nil); streamctx == nil { |
| 79 | + ff.AVCodec_free_context(encoder.ctx) |
| 80 | + return nil, ErrInternalAppError.With("could not allocate stream") |
| 81 | + } else { |
| 82 | + streamctx.SetId(stream) |
| 83 | + encoder.stream = streamctx |
| 84 | + } |
| 85 | + |
| 86 | + // Copy parameters to stream |
| 87 | + if err := ff.AVCodec_parameters_from_context(encoder.stream.CodecPar(), encoder.ctx); err != nil { |
| 88 | + ff.AVCodec_free_context(encoder.ctx) |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + // Some formats want stream headers to be separate. |
| 93 | + if ctx.Output().Flags().Is(ff.AVFMT_GLOBALHEADER) { |
| 94 | + encoder.ctx.SetFlags(encoder.ctx.Flags() | ff.AV_CODEC_FLAG_GLOBAL_HEADER) |
| 95 | + } |
| 96 | + |
| 97 | + // Open it |
| 98 | + if err := ff.AVCodec_open(encoder.ctx, codec, nil); err != nil { |
| 99 | + ff.AVCodec_free_context(encoder.ctx) |
| 100 | + return nil, ErrInternalAppError.Withf("codec_open: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + // Create a packet |
| 104 | + packet := ff.AVCodec_packet_alloc() |
| 105 | + if packet == nil { |
| 106 | + ff.AVCodec_free_context(encoder.ctx) |
| 107 | + return nil, errors.New("failed to allocate packet") |
| 108 | + } else { |
| 109 | + encoder.packet = packet |
| 110 | + } |
| 111 | + |
| 112 | + // Return it |
| 113 | + return encoder, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (encoder *Encoder) Close() error { |
| 117 | + // Free respurces |
| 118 | + if encoder.ctx != nil { |
| 119 | + ff.AVCodec_free_context(encoder.ctx) |
| 120 | + } |
| 121 | + if encoder.packet != nil { |
| 122 | + ff.AVCodec_packet_free(encoder.packet) |
| 123 | + } |
| 124 | + |
| 125 | + // Release resources |
| 126 | + encoder.packet = nil |
| 127 | + encoder.stream = nil |
| 128 | + encoder.ctx = nil |
| 129 | + |
| 130 | + // Return success |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +//////////////////////////////////////////////////////////////////////////////// |
| 135 | +// STRINGIFY |
| 136 | + |
| 137 | +func (e *Encoder) MarshalJSON() ([]byte, error) { |
| 138 | + type jsonEncoder struct { |
| 139 | + Codec *ff.AVCodecContext `json:"codec"` |
| 140 | + Stream *ff.AVStream `json:"stream"` |
| 141 | + } |
| 142 | + return json.Marshal(&jsonEncoder{ |
| 143 | + Codec: e.ctx, |
| 144 | + Stream: e.stream, |
| 145 | + }) |
| 146 | +} |
| 147 | + |
| 148 | +func (e *Encoder) String() string { |
| 149 | + data, _ := json.MarshalIndent(e, "", " ") |
| 150 | + return string(data) |
| 151 | +} |
| 152 | + |
| 153 | +////////////////////////////////////////////////////////////////////////////// |
| 154 | +// PUBLIC METHODS |
| 155 | + |
| 156 | +// Encode a frame and pass packets to the EncoderPacketFn. If the frame is nil, then |
| 157 | +// the encoder will flush any remaining packets. If io.EOF is returned then |
| 158 | +// it indicates that the encoder has ended prematurely. |
| 159 | +func (e *Encoder) Encode(frame *ff.AVFrame, fn EncoderPacketFn) error { |
| 160 | + if fn == nil { |
| 161 | + return ErrBadParameter.With("nil fn") |
| 162 | + } |
| 163 | + // Encode a frame (or flush the encoder) |
| 164 | + return e.encode(frame, fn) |
| 165 | +} |
| 166 | + |
| 167 | +// Return the codec parameters |
| 168 | +func (e *Encoder) Par() *Par { |
| 169 | + par := new(Par) |
| 170 | + if err := ff.AVCodec_parameters_from_context(&par.AVCodecParameters, e.ctx); err != nil { |
| 171 | + return nil |
| 172 | + } else { |
| 173 | + return par |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +////////////////////////////////////////////////////////////////////////////// |
| 178 | +// PRIVATE METHODS |
| 179 | + |
| 180 | +func (e *Encoder) encode(frame *ff.AVFrame, fn EncoderPacketFn) error { |
| 181 | + // Send the frame to the encoder |
| 182 | + fmt.Println("Sending frame", frame) |
| 183 | + if err := ff.AVCodec_send_frame(e.ctx, frame); err != nil { |
| 184 | + if errors.Is(err, syscall.EAGAIN) || errors.Is(err, io.EOF) { |
| 185 | + return nil |
| 186 | + } |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + // Write out the packets |
| 191 | + var result error |
| 192 | + for { |
| 193 | + // Receive the packet |
| 194 | + fmt.Println("Receiving packet") |
| 195 | + if err := ff.AVCodec_receive_packet(e.ctx, e.packet); errors.Is(err, syscall.EAGAIN) || errors.Is(err, io.EOF) { |
| 196 | + // Finished receiving packet or EOF |
| 197 | + break |
| 198 | + } else if err != nil { |
| 199 | + return err |
| 200 | + } |
| 201 | + |
| 202 | + // Pass back to the caller |
| 203 | + if err := fn(e.packet); errors.Is(err, io.EOF) { |
| 204 | + // End early, return EOF |
| 205 | + result = io.EOF |
| 206 | + break |
| 207 | + } else if err != nil { |
| 208 | + return err |
| 209 | + } |
| 210 | + |
| 211 | + // Re-allocate frames for next iteration |
| 212 | + ff.AVCodec_packet_unref(e.packet) |
| 213 | + } |
| 214 | + |
| 215 | + // Flush |
| 216 | + if result == nil { |
| 217 | + result = fn(nil) |
| 218 | + } |
| 219 | + |
| 220 | + // Return success or EOF |
| 221 | + return result |
| 222 | +} |
0 commit comments