src/feature/ffmpeg/ffmpeg-encoder.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "ffmpeg-encoder.h"
7
8#include <mgba/core/core.h>
9#include <mgba/gba/interface.h>
10
11#include <libavcodec/version.h>
12#include <libavcodec/avcodec.h>
13
14#include <libavfilter/buffersink.h>
15#include <libavfilter/buffersrc.h>
16
17#include <libavutil/version.h>
18#if LIBAVUTIL_VERSION_MAJOR >= 53
19#include <libavutil/buffer.h>
20#endif
21#include <libavutil/imgutils.h>
22#include <libavutil/mathematics.h>
23#include <libavutil/opt.h>
24
25#ifdef USE_LIBAVRESAMPLE
26#include <libavresample/avresample.h>
27#else
28#include <libswresample/swresample.h>
29#endif
30#include <libswscale/swscale.h>
31
32static void _ffmpegPostVideoFrame(struct mAVStream*, const color_t* pixels, size_t stride);
33static void _ffmpegPostAudioFrame(struct mAVStream*, int16_t left, int16_t right);
34static void _ffmpegSetVideoDimensions(struct mAVStream*, unsigned width, unsigned height);
35
36static bool _ffmpegWriteAudioFrame(struct FFmpegEncoder* encoder, struct AVFrame* audioFrame);
37static bool _ffmpegWriteVideoFrame(struct FFmpegEncoder* encoder, struct AVFrame* videoFrame);
38
39enum {
40 PREFERRED_SAMPLE_RATE = 0x8000
41};
42
43void FFmpegEncoderInit(struct FFmpegEncoder* encoder) {
44#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
45 av_register_all();
46#endif
47
48 encoder->d.videoDimensionsChanged = _ffmpegSetVideoDimensions;
49 encoder->d.postVideoFrame = _ffmpegPostVideoFrame;
50 encoder->d.postAudioFrame = _ffmpegPostAudioFrame;
51 encoder->d.postAudioBuffer = 0;
52
53 encoder->audioCodec = NULL;
54 encoder->videoCodec = NULL;
55 encoder->containerFormat = NULL;
56 FFmpegEncoderSetAudio(encoder, "flac", 0);
57 FFmpegEncoderSetVideo(encoder, "libx264", 0, 0);
58 FFmpegEncoderSetContainer(encoder, "matroska");
59 FFmpegEncoderSetDimensions(encoder, GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
60 encoder->iwidth = GBA_VIDEO_HORIZONTAL_PIXELS;
61 encoder->iheight = GBA_VIDEO_VERTICAL_PIXELS;
62 encoder->frameskip = 1;
63 encoder->skipResidue = 0;
64 encoder->loop = false;
65 encoder->ipixFormat =
66#ifdef COLOR_16_BIT
67#ifdef COLOR_5_6_5
68 AV_PIX_FMT_RGB565;
69#else
70 AV_PIX_FMT_BGR555;
71#endif
72#else
73#ifndef USE_LIBAV
74 AV_PIX_FMT_0BGR32;
75#else
76 AV_PIX_FMT_BGR32;
77#endif
78#endif
79 encoder->resampleContext = NULL;
80 encoder->absf = NULL;
81 encoder->context = NULL;
82 encoder->scaleContext = NULL;
83 encoder->audio = NULL;
84 encoder->audioStream = NULL;
85 encoder->audioFrame = NULL;
86 encoder->audioBuffer = NULL;
87 encoder->postaudioBuffer = NULL;
88 encoder->video = NULL;
89 encoder->videoStream = NULL;
90 encoder->videoFrame = NULL;
91 encoder->graph = NULL;
92 encoder->source = NULL;
93 encoder->sink = NULL;
94 encoder->sinkFrame = NULL;
95
96 int i;
97 for (i = 0; i < FFMPEG_FILTERS_MAX; ++i) {
98 encoder->filters[i] = NULL;
99 }
100}
101
102bool FFmpegEncoderSetAudio(struct FFmpegEncoder* encoder, const char* acodec, unsigned abr) {
103 static const struct {
104 int format;
105 int priority;
106 } priorities[] = {
107 { AV_SAMPLE_FMT_S16, 0 },
108 { AV_SAMPLE_FMT_S16P, 1 },
109 { AV_SAMPLE_FMT_S32, 2 },
110 { AV_SAMPLE_FMT_S32P, 2 },
111 { AV_SAMPLE_FMT_FLT, 3 },
112 { AV_SAMPLE_FMT_FLTP, 3 },
113 { AV_SAMPLE_FMT_DBL, 4 },
114 { AV_SAMPLE_FMT_DBLP, 4 }
115 };
116
117 if (!acodec) {
118 encoder->audioCodec = 0;
119 return true;
120 }
121
122 AVCodec* codec = avcodec_find_encoder_by_name(acodec);
123 if (!codec) {
124 return false;
125 }
126
127 if (!codec->sample_fmts) {
128 return false;
129 }
130 size_t i;
131 size_t j;
132 int priority = INT_MAX;
133 encoder->sampleFormat = AV_SAMPLE_FMT_NONE;
134 for (i = 0; codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; ++i) {
135 for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
136 if (codec->sample_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
137 priority = priorities[j].priority;
138 encoder->sampleFormat = codec->sample_fmts[i];
139 }
140 }
141 }
142 if (encoder->sampleFormat == AV_SAMPLE_FMT_NONE) {
143 return false;
144 }
145 encoder->sampleRate = PREFERRED_SAMPLE_RATE;
146 if (codec->supported_samplerates) {
147 for (i = 0; codec->supported_samplerates[i]; ++i) {
148 if (codec->supported_samplerates[i] < PREFERRED_SAMPLE_RATE) {
149 continue;
150 }
151 if (encoder->sampleRate == PREFERRED_SAMPLE_RATE || encoder->sampleRate > codec->supported_samplerates[i]) {
152 encoder->sampleRate = codec->supported_samplerates[i];
153 }
154 }
155 } else if (codec->id == AV_CODEC_ID_AAC) {
156 // HACK: AAC doesn't support 32768Hz (it rounds to 32000), but libfaac doesn't tell us that
157 encoder->sampleRate = 44100;
158 }
159 encoder->audioCodec = acodec;
160 encoder->audioBitrate = abr;
161 return true;
162}
163
164bool FFmpegEncoderSetVideo(struct FFmpegEncoder* encoder, const char* vcodec, unsigned vbr, int frameskip) {
165 static const struct {
166 enum AVPixelFormat format;
167 int priority;
168 } priorities[] = {
169 { AV_PIX_FMT_RGB555, 0 },
170 { AV_PIX_FMT_BGR555, 0 },
171 { AV_PIX_FMT_RGB565, 1 },
172 { AV_PIX_FMT_BGR565, 1 },
173 { AV_PIX_FMT_RGB24, 2 },
174 { AV_PIX_FMT_BGR24, 2 },
175#ifndef USE_LIBAV
176 { AV_PIX_FMT_BGR0, 3 },
177 { AV_PIX_FMT_RGB0, 3 },
178 { AV_PIX_FMT_0BGR, 3 },
179 { AV_PIX_FMT_0RGB, 3 },
180#endif
181 { AV_PIX_FMT_YUV422P, 4 },
182 { AV_PIX_FMT_YUV444P, 5 },
183 { AV_PIX_FMT_YUV420P, 6 },
184 { AV_PIX_FMT_PAL8, 7 },
185 };
186
187 if (!vcodec) {
188 encoder->videoCodec = 0;
189 return true;
190 }
191
192 AVCodec* codec = avcodec_find_encoder_by_name(vcodec);
193 if (!codec) {
194 return false;
195 }
196
197 size_t i;
198 size_t j;
199 int priority = INT_MAX;
200 encoder->pixFormat = AV_PIX_FMT_NONE;
201 for (i = 0; codec->pix_fmts[i] != AV_PIX_FMT_NONE; ++i) {
202 for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
203 if (codec->pix_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
204 priority = priorities[j].priority;
205 encoder->pixFormat = codec->pix_fmts[i];
206 }
207 }
208 }
209 if (encoder->pixFormat == AV_PIX_FMT_NONE) {
210 return false;
211 }
212 encoder->videoCodec = vcodec;
213 encoder->videoBitrate = vbr;
214 encoder->frameskip = frameskip + 1;
215 return true;
216}
217
218bool FFmpegEncoderSetContainer(struct FFmpegEncoder* encoder, const char* container) {
219 AVOutputFormat* oformat = av_guess_format(container, 0, 0);
220 if (!oformat) {
221 return false;
222 }
223 encoder->containerFormat = container;
224 return true;
225}
226
227void FFmpegEncoderSetDimensions(struct FFmpegEncoder* encoder, int width, int height) {
228 encoder->width = width > 0 ? width : GBA_VIDEO_HORIZONTAL_PIXELS;
229 encoder->height = height > 0 ? height : GBA_VIDEO_VERTICAL_PIXELS;
230}
231
232void FFmpegEncoderSetLooping(struct FFmpegEncoder* encoder, bool loop) {
233 encoder->loop = loop;
234}
235
236bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder* encoder) {
237 AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
238 AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
239 AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
240 if ((encoder->audioCodec && !acodec) || (encoder->videoCodec && !vcodec) || !oformat || (!acodec && !vcodec)) {
241 return false;
242 }
243 if (encoder->audioCodec && !avformat_query_codec(oformat, acodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
244 return false;
245 }
246 if (encoder->videoCodec && !avformat_query_codec(oformat, vcodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
247 return false;
248 }
249 return true;
250}
251
252bool FFmpegEncoderOpen(struct FFmpegEncoder* encoder, const char* outfile) {
253 AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
254 AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
255 if ((encoder->audioCodec && !acodec) || (encoder->videoCodec && !vcodec) || !FFmpegEncoderVerifyContainer(encoder)) {
256 return false;
257 }
258
259 if (encoder->context) {
260 return false;
261 }
262
263 encoder->currentAudioSample = 0;
264 encoder->currentAudioFrame = 0;
265 encoder->currentVideoFrame = 0;
266 encoder->skipResidue = 0;
267
268 AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
269#ifndef USE_LIBAV
270 avformat_alloc_output_context2(&encoder->context, oformat, 0, outfile);
271#else
272 encoder->context = avformat_alloc_context();
273 strncpy(encoder->context->filename, outfile, sizeof(encoder->context->filename) - 1);
274 encoder->context->filename[sizeof(encoder->context->filename) - 1] = '\0';
275 encoder->context->oformat = oformat;
276#endif
277
278 if (acodec) {
279#ifdef FFMPEG_USE_CODECPAR
280 encoder->audioStream = avformat_new_stream(encoder->context, NULL);
281 encoder->audio = avcodec_alloc_context3(acodec);
282#else
283 encoder->audioStream = avformat_new_stream(encoder->context, acodec);
284 encoder->audio = encoder->audioStream->codec;
285#endif
286 encoder->audio->bit_rate = encoder->audioBitrate;
287 encoder->audio->channels = 2;
288 encoder->audio->channel_layout = AV_CH_LAYOUT_STEREO;
289 encoder->audio->sample_rate = encoder->sampleRate;
290 encoder->audio->sample_fmt = encoder->sampleFormat;
291 AVDictionary* opts = 0;
292 av_dict_set(&opts, "strict", "-2", 0);
293 if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
294#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
295 encoder->audio->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
296#else
297 encoder->audio->flags |= CODEC_FLAG_GLOBAL_HEADER;
298#endif
299 }
300 int res = avcodec_open2(encoder->audio, acodec, &opts);
301 av_dict_free(&opts);
302 if (res < 0) {
303 FFmpegEncoderClose(encoder);
304 return false;
305 }
306#if LIBAVCODEC_VERSION_MAJOR >= 55
307 encoder->audioFrame = av_frame_alloc();
308#else
309 encoder->audioFrame = avcodec_alloc_frame();
310#endif
311 if (!encoder->audio->frame_size) {
312 encoder->audio->frame_size = 1;
313 }
314 encoder->audioFrame->nb_samples = encoder->audio->frame_size;
315 encoder->audioFrame->format = encoder->audio->sample_fmt;
316 encoder->audioFrame->pts = 0;
317#ifdef USE_LIBAVRESAMPLE
318 encoder->resampleContext = avresample_alloc_context();
319 av_opt_set_int(encoder->resampleContext, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
320 av_opt_set_int(encoder->resampleContext, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
321 av_opt_set_int(encoder->resampleContext, "in_sample_rate", PREFERRED_SAMPLE_RATE, 0);
322 av_opt_set_int(encoder->resampleContext, "out_sample_rate", encoder->sampleRate, 0);
323 av_opt_set_int(encoder->resampleContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
324 av_opt_set_int(encoder->resampleContext, "out_sample_fmt", encoder->sampleFormat, 0);
325 avresample_open(encoder->resampleContext);
326#else
327 encoder->resampleContext = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, encoder->sampleFormat, encoder->sampleRate,
328 AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, PREFERRED_SAMPLE_RATE, 0, NULL);
329 swr_init(encoder->resampleContext);
330#endif
331 encoder->audioBufferSize = (encoder->audioFrame->nb_samples * PREFERRED_SAMPLE_RATE / encoder->sampleRate) * 4;
332 encoder->audioBuffer = av_malloc(encoder->audioBufferSize);
333 encoder->postaudioBufferSize = av_samples_get_buffer_size(0, encoder->audio->channels, encoder->audio->frame_size, encoder->audio->sample_fmt, 0);
334 encoder->postaudioBuffer = av_malloc(encoder->postaudioBufferSize);
335 avcodec_fill_audio_frame(encoder->audioFrame, encoder->audio->channels, encoder->audio->sample_fmt, (const uint8_t*) encoder->postaudioBuffer, encoder->postaudioBufferSize, 0);
336
337 if (encoder->audio->codec->id == AV_CODEC_ID_AAC &&
338 (strcasecmp(encoder->containerFormat, "mp4") ||
339 strcasecmp(encoder->containerFormat, "m4v") ||
340 strcasecmp(encoder->containerFormat, "mov"))) {
341 // MP4 container doesn't support the raw ADTS AAC format that the encoder spits out
342#ifdef FFMPEG_USE_NEW_BSF
343 av_bsf_alloc(av_bsf_get_by_name("aac_adtstoasc"), &encoder->absf);
344 avcodec_parameters_from_context(encoder->absf->par_in, encoder->audio);
345 av_bsf_init(encoder->absf);
346#else
347 encoder->absf = av_bitstream_filter_init("aac_adtstoasc");
348#endif
349 }
350#ifdef FFMPEG_USE_CODECPAR
351 avcodec_parameters_from_context(encoder->audioStream->codecpar, encoder->audio);
352#endif
353 }
354
355 if (vcodec) {
356#ifdef FFMPEG_USE_CODECPAR
357 encoder->videoStream = avformat_new_stream(encoder->context, NULL);
358 encoder->video = avcodec_alloc_context3(vcodec);
359#else
360 encoder->videoStream = avformat_new_stream(encoder->context, vcodec);
361 encoder->video = encoder->videoStream->codec;
362#endif
363 encoder->video->bit_rate = encoder->videoBitrate;
364 encoder->video->width = encoder->width;
365 encoder->video->height = encoder->height;
366 encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH * encoder->frameskip, GBA_ARM7TDMI_FREQUENCY };
367 encoder->video->framerate = (AVRational) { GBA_ARM7TDMI_FREQUENCY, VIDEO_TOTAL_LENGTH * encoder->frameskip };
368 encoder->videoStream->time_base = encoder->video->time_base;
369 encoder->videoStream->avg_frame_rate = encoder->video->framerate;
370 encoder->video->pix_fmt = encoder->pixFormat;
371 encoder->video->gop_size = 60;
372 encoder->video->max_b_frames = 3;
373 if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
374#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
375 encoder->video->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
376#else
377 encoder->video->flags |= CODEC_FLAG_GLOBAL_HEADER;
378#endif
379 }
380
381 if (encoder->video->codec->id == AV_CODEC_ID_H264 &&
382 (strcasecmp(encoder->containerFormat, "mp4") ||
383 strcasecmp(encoder->containerFormat, "m4v") ||
384 strcasecmp(encoder->containerFormat, "mov"))) {
385 // QuickTime and a few other things require YUV420
386 encoder->video->pix_fmt = AV_PIX_FMT_YUV420P;
387 }
388#if LIBAVCODEC_VERSION_MAJOR >= 57
389 if (encoder->video->codec->id == AV_CODEC_ID_FFV1) {
390 av_opt_set(encoder->video->priv_data, "coder", "range_tab", 0);
391 }
392#endif
393
394 if (strcmp(vcodec->name, "libx264") == 0) {
395 // Try to adaptively figure out when you can use a slower encoder
396 if (encoder->width * encoder->height > 1000000) {
397 av_opt_set(encoder->video->priv_data, "preset", "superfast", 0);
398 } else if (encoder->width * encoder->height > 500000) {
399 av_opt_set(encoder->video->priv_data, "preset", "veryfast", 0);
400 } else {
401 av_opt_set(encoder->video->priv_data, "preset", "faster", 0);
402 }
403 if (encoder->videoBitrate == 0) {
404 av_opt_set(encoder->video->priv_data, "crf", "0", 0);
405 encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
406 }
407 }
408 if (strcmp(vcodec->name, "libvpx-vp9") == 0 && encoder->videoBitrate == 0) {
409 av_opt_set(encoder->video->priv_data, "lossless", "1", 0);
410 encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
411 }
412
413 if (encoder->pixFormat == AV_PIX_FMT_PAL8) {
414 encoder->graph = avfilter_graph_alloc();
415
416 const struct AVFilter* source = avfilter_get_by_name("buffer");
417 const struct AVFilter* sink = avfilter_get_by_name("buffersink");
418 const struct AVFilter* split = avfilter_get_by_name("split");
419 const struct AVFilter* palettegen = avfilter_get_by_name("palettegen");
420 const struct AVFilter* paletteuse = avfilter_get_by_name("paletteuse");
421
422 if (!source || !sink || !split || !palettegen || !paletteuse || !encoder->graph) {
423 FFmpegEncoderClose(encoder);
424 return false;
425 }
426
427 char args[256];
428 snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d",
429 encoder->video->width, encoder->video->height, encoder->ipixFormat,
430 encoder->video->time_base.num, encoder->video->time_base.den);
431
432 int res = 0;
433 res |= avfilter_graph_create_filter(&encoder->source, source, NULL, args, NULL, encoder->graph);
434 res |= avfilter_graph_create_filter(&encoder->sink, sink, NULL, NULL, NULL, encoder->graph);
435 res |= avfilter_graph_create_filter(&encoder->filters[0], split, NULL, NULL, NULL, encoder->graph);
436 res |= avfilter_graph_create_filter(&encoder->filters[1], palettegen, NULL, "reserve_transparent=off", NULL, encoder->graph);
437 res |= avfilter_graph_create_filter(&encoder->filters[2], paletteuse, NULL, "dither=none", NULL, encoder->graph);
438 if (res < 0) {
439 FFmpegEncoderClose(encoder);
440 return false;
441 }
442
443 res = 0;
444 res |= avfilter_link(encoder->source, 0, encoder->filters[0], 0);
445 res |= avfilter_link(encoder->filters[0], 0, encoder->filters[1], 0);
446 res |= avfilter_link(encoder->filters[0], 1, encoder->filters[2], 0);
447 res |= avfilter_link(encoder->filters[1], 0, encoder->filters[2], 1);
448 res |= avfilter_link(encoder->filters[2], 0, encoder->sink, 0);
449 if (res < 0 || avfilter_graph_config(encoder->graph, NULL) < 0) {
450 FFmpegEncoderClose(encoder);
451 return false;
452 }
453
454#if LIBAVCODEC_VERSION_MAJOR >= 55
455 encoder->sinkFrame = av_frame_alloc();
456#else
457 encoder->sinkFrame = avcodec_alloc_frame();
458#endif
459 }
460 AVDictionary* opts = 0;
461 av_dict_set(&opts, "strict", "-2", 0);
462 int res = avcodec_open2(encoder->video, vcodec, &opts);
463 av_dict_free(&opts);
464 if (res < 0) {
465 FFmpegEncoderClose(encoder);
466 return false;
467 }
468#if LIBAVCODEC_VERSION_MAJOR >= 55
469 encoder->videoFrame = av_frame_alloc();
470#else
471 encoder->videoFrame = avcodec_alloc_frame();
472#endif
473 encoder->videoFrame->format = encoder->video->pix_fmt != AV_PIX_FMT_PAL8 ? encoder->video->pix_fmt : encoder->ipixFormat;
474 encoder->videoFrame->width = encoder->video->width;
475 encoder->videoFrame->height = encoder->video->height;
476 encoder->videoFrame->pts = 0;
477 _ffmpegSetVideoDimensions(&encoder->d, encoder->iwidth, encoder->iheight);
478 av_image_alloc(encoder->videoFrame->data, encoder->videoFrame->linesize, encoder->videoFrame->width, encoder->videoFrame->height, encoder->videoFrame->format, 32);
479#ifdef FFMPEG_USE_CODECPAR
480 avcodec_parameters_from_context(encoder->videoStream->codecpar, encoder->video);
481#endif
482 }
483
484 if (strcmp(encoder->containerFormat, "gif") == 0) {
485 av_opt_set(encoder->context->priv_data, "loop", encoder->loop ? "0" : "-1", 0);
486 } else if (strcmp(encoder->containerFormat, "apng") == 0) {
487 av_opt_set(encoder->context->priv_data, "plays", encoder->loop ? "0" : "1", 0);
488 }
489
490 AVDictionary* opts = 0;
491 av_dict_set(&opts, "strict", "-2", 0);
492 bool res = avio_open(&encoder->context->pb, outfile, AVIO_FLAG_WRITE) < 0 || avformat_write_header(encoder->context, &opts) < 0;
493 av_dict_free(&opts);
494 if (res) {
495 FFmpegEncoderClose(encoder);
496 return false;
497 }
498 return true;
499}
500
501void FFmpegEncoderClose(struct FFmpegEncoder* encoder) {
502 if (encoder->audio) {
503 while (true) {
504 if (!_ffmpegWriteAudioFrame(encoder, NULL)) {
505 break;
506 }
507 }
508 }
509 if (encoder->video) {
510 if (encoder->graph) {
511 if (av_buffersrc_add_frame(encoder->source, NULL) >= 0) {
512 while (true) {
513 int res = av_buffersink_get_frame(encoder->sink, encoder->sinkFrame);
514 if (res < 0) {
515 break;
516 }
517 _ffmpegWriteVideoFrame(encoder, encoder->sinkFrame);
518 av_frame_unref(encoder->sinkFrame);
519 }
520 }
521 }
522 while (true) {
523 if (!_ffmpegWriteVideoFrame(encoder, NULL)) {
524 break;
525 }
526 }
527 }
528
529 if (encoder->context && encoder->context->pb) {
530 av_write_trailer(encoder->context);
531 avio_close(encoder->context->pb);
532 }
533
534 if (encoder->postaudioBuffer) {
535 av_free(encoder->postaudioBuffer);
536 encoder->postaudioBuffer = NULL;
537 }
538 if (encoder->audioBuffer) {
539 av_free(encoder->audioBuffer);
540 encoder->audioBuffer = NULL;
541 }
542
543 if (encoder->audioFrame) {
544#if LIBAVCODEC_VERSION_MAJOR >= 55
545 av_frame_free(&encoder->audioFrame);
546#else
547 avcodec_free_frame(&encoder->audioFrame);
548#endif
549 }
550 if (encoder->audio) {
551#ifdef FFMPEG_USE_CODECPAR
552 avcodec_free_context(&encoder->audio);
553#else
554 avcodec_close(encoder->audio);
555 encoder->audio = NULL;
556#endif
557 }
558
559 if (encoder->resampleContext) {
560#ifdef USE_LIBAVRESAMPLE
561 avresample_close(encoder->resampleContext);
562 encoder->resampleContext = NULL;
563#else
564 swr_free(&encoder->resampleContext);
565#endif
566 }
567
568 if (encoder->absf) {
569#ifdef FFMPEG_USE_NEW_BSF
570 av_bsf_free(&encoder->absf);
571#else
572 av_bitstream_filter_close(encoder->absf);
573 encoder->absf = NULL;
574#endif
575 }
576
577 if (encoder->videoFrame) {
578 av_freep(encoder->videoFrame->data);
579#if LIBAVCODEC_VERSION_MAJOR >= 55
580 av_frame_free(&encoder->videoFrame);
581#else
582 avcodec_free_frame(&encoder->videoFrame);
583#endif
584 }
585
586 if (encoder->sinkFrame) {
587#if LIBAVCODEC_VERSION_MAJOR >= 55
588 av_frame_free(&encoder->sinkFrame);
589#else
590 avcodec_free_frame(&encoder->sinkFrame);
591#endif
592 encoder->sinkFrame = NULL;
593 }
594
595 if (encoder->video) {
596#ifdef FFMPEG_USE_CODECPAR
597 avcodec_free_context(&encoder->video);
598#else
599 avcodec_close(encoder->video);
600 encoder->video = NULL;
601#endif
602 }
603
604 if (encoder->scaleContext) {
605 sws_freeContext(encoder->scaleContext);
606 encoder->scaleContext = NULL;
607 }
608
609 if (encoder->graph) {
610 avfilter_graph_free(&encoder->graph);
611 encoder->graph = NULL;
612 encoder->source = NULL;
613 encoder->sink = NULL;
614
615 int i;
616 for (i = 0; i < FFMPEG_FILTERS_MAX; ++i) {
617 encoder->filters[i] = NULL;
618 }
619 }
620
621 if (encoder->context) {
622 avformat_free_context(encoder->context);
623 encoder->context = NULL;
624 }
625}
626
627bool FFmpegEncoderIsOpen(struct FFmpegEncoder* encoder) {
628 return !!encoder->context;
629}
630
631void _ffmpegPostAudioFrame(struct mAVStream* stream, int16_t left, int16_t right) {
632 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
633 if (!encoder->context || !encoder->audioCodec) {
634 return;
635 }
636
637 if (encoder->absf && !left) {
638 // XXX: AVBSF doesn't like silence. Figure out why.
639 left = 1;
640 }
641
642 encoder->audioBuffer[encoder->currentAudioSample * 2] = left;
643 encoder->audioBuffer[encoder->currentAudioSample * 2 + 1] = right;
644
645 ++encoder->currentAudioSample;
646
647 if (encoder->currentAudioSample * 4 < encoder->audioBufferSize) {
648 return;
649 }
650
651 int channelSize = 2 * av_get_bytes_per_sample(encoder->audio->sample_fmt);
652 encoder->currentAudioSample = 0;
653#ifdef USE_LIBAVRESAMPLE
654 avresample_convert(encoder->resampleContext, 0, 0, 0,
655 (uint8_t**) &encoder->audioBuffer, 0, encoder->audioBufferSize / 4);
656
657 if (avresample_available(encoder->resampleContext) < encoder->audioFrame->nb_samples) {
658 return;
659 }
660#if LIBAVCODEC_VERSION_MAJOR >= 55
661 av_frame_make_writable(encoder->audioFrame);
662#endif
663 int samples = avresample_read(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize);
664#else
665#if LIBAVCODEC_VERSION_MAJOR >= 55
666 av_frame_make_writable(encoder->audioFrame);
667#endif
668 if (swr_get_out_samples(encoder->resampleContext, 1) < encoder->audioFrame->nb_samples) {
669 swr_convert(encoder->resampleContext, NULL, 0, (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
670 return;
671 }
672 int samples = swr_convert(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize,
673 (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
674#endif
675
676 encoder->audioFrame->pts = encoder->currentAudioFrame;
677 encoder->currentAudioFrame += samples;
678
679 _ffmpegWriteAudioFrame(encoder, encoder->audioFrame);
680}
681
682bool _ffmpegWriteAudioFrame(struct FFmpegEncoder* encoder, struct AVFrame* audioFrame) {
683 AVPacket packet;
684 av_init_packet(&packet);
685 packet.data = 0;
686 packet.size = 0;
687
688 int gotData;
689#ifdef FFMPEG_USE_PACKETS
690 avcodec_send_frame(encoder->audio, audioFrame);
691 gotData = avcodec_receive_packet(encoder->audio, &packet);
692 gotData = (gotData == 0) && packet.size;
693#else
694 avcodec_encode_audio2(encoder->audio, &packet, audioFrame, &gotData);
695#endif
696 packet.pts = av_rescale_q(packet.pts, encoder->audio->time_base, encoder->audioStream->time_base);
697 packet.dts = packet.pts;
698
699 if (gotData) {
700 if (encoder->absf) {
701 AVPacket tempPacket;
702
703#ifdef FFMPEG_USE_NEW_BSF
704 int success = av_bsf_send_packet(encoder->absf, &packet);
705 if (success >= 0) {
706 success = av_bsf_receive_packet(encoder->absf, &tempPacket);
707 }
708#else
709 int success = av_bitstream_filter_filter(encoder->absf, encoder->audio, 0,
710 &tempPacket.data, &tempPacket.size,
711 packet.data, packet.size, 0);
712#endif
713
714 if (success >= 0) {
715#if LIBAVUTIL_VERSION_MAJOR >= 53
716 tempPacket.buf = av_buffer_create(tempPacket.data, tempPacket.size, av_buffer_default_free, 0, 0);
717#endif
718
719#ifdef FFMPEG_USE_PACKET_UNREF
720 av_packet_move_ref(&packet, &tempPacket);
721#else
722 av_free_packet(&packet);
723 packet = tempPacket;
724#endif
725
726 packet.stream_index = encoder->audioStream->index;
727 av_interleaved_write_frame(encoder->context, &packet);
728 }
729 } else {
730 packet.stream_index = encoder->audioStream->index;
731 av_interleaved_write_frame(encoder->context, &packet);
732 }
733 }
734#ifdef FFMPEG_USE_PACKET_UNREF
735 av_packet_unref(&packet);
736#else
737 av_free_packet(&packet);
738#endif
739 return gotData;
740}
741
742void _ffmpegPostVideoFrame(struct mAVStream* stream, const color_t* pixels, size_t stride) {
743 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
744 if (!encoder->context || !encoder->videoCodec) {
745 return;
746 }
747 encoder->skipResidue = (encoder->skipResidue + 1) % encoder->frameskip;
748 if (encoder->skipResidue) {
749 return;
750 }
751 stride *= BYTES_PER_PIXEL;
752
753#if LIBAVCODEC_VERSION_MAJOR >= 55
754 av_frame_make_writable(encoder->videoFrame);
755#endif
756 encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
757 ++encoder->currentVideoFrame;
758
759 sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, encoder->iheight, encoder->videoFrame->data, encoder->videoFrame->linesize);
760
761 if (encoder->graph) {
762 if (av_buffersrc_add_frame(encoder->source, encoder->videoFrame) < 0) {
763 return;
764 }
765 while (true) {
766 int res = av_buffersink_get_frame(encoder->sink, encoder->sinkFrame);
767 if (res < 0) {
768 break;
769 }
770 _ffmpegWriteVideoFrame(encoder, encoder->sinkFrame);
771 av_frame_unref(encoder->sinkFrame);
772 }
773 } else {
774 _ffmpegWriteVideoFrame(encoder, encoder->videoFrame);
775 }
776}
777
778bool _ffmpegWriteVideoFrame(struct FFmpegEncoder* encoder, struct AVFrame* videoFrame) {
779 AVPacket packet;
780
781 av_init_packet(&packet);
782 packet.data = 0;
783 packet.size = 0;
784
785 int gotData;
786#ifdef FFMPEG_USE_PACKETS
787 avcodec_send_frame(encoder->video, videoFrame);
788 gotData = avcodec_receive_packet(encoder->video, &packet) == 0;
789#else
790 avcodec_encode_video2(encoder->video, &packet, videoFrame, &gotData);
791#endif
792 if (gotData) {
793#ifndef FFMPEG_USE_PACKET_UNREF
794 if (encoder->video->coded_frame->key_frame) {
795 packet.flags |= AV_PKT_FLAG_KEY;
796 }
797#endif
798 packet.stream_index = encoder->videoStream->index;
799 av_interleaved_write_frame(encoder->context, &packet);
800 }
801#ifdef FFMPEG_USE_PACKET_UNREF
802 av_packet_unref(&packet);
803#else
804 av_free_packet(&packet);
805#endif
806
807 return gotData;
808}
809
810static void _ffmpegSetVideoDimensions(struct mAVStream* stream, unsigned width, unsigned height) {
811 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
812 if (!encoder->context || !encoder->videoCodec) {
813 return;
814 }
815 encoder->iwidth = width;
816 encoder->iheight = height;
817 if (encoder->scaleContext) {
818 sws_freeContext(encoder->scaleContext);
819 }
820 encoder->scaleContext = sws_getContext(encoder->iwidth, encoder->iheight, encoder->ipixFormat,
821 encoder->videoFrame->width, encoder->videoFrame->height, encoder->videoFrame->format,
822 SWS_POINT, 0, 0, 0);
823}