all repos — mgba @ 2f643d79445b3cfb4d84ea6167d2c0bf005b855c

mGBA Game Boy Advance Emulator

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->video->pix_fmt = encoder->pixFormat;
369		encoder->video->gop_size = 60;
370		encoder->video->max_b_frames = 3;
371		if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
372#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
373			encoder->video->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
374#else
375			encoder->video->flags |= CODEC_FLAG_GLOBAL_HEADER;
376#endif
377		}
378
379		if (encoder->video->codec->id == AV_CODEC_ID_H264 &&
380		    (strcasecmp(encoder->containerFormat, "mp4") ||
381		        strcasecmp(encoder->containerFormat, "m4v") ||
382		        strcasecmp(encoder->containerFormat, "mov"))) {
383			// QuickTime and a few other things require YUV420
384			encoder->video->pix_fmt = AV_PIX_FMT_YUV420P;
385		}
386
387		if (strcmp(vcodec->name, "libx264") == 0) {
388			// Try to adaptively figure out when you can use a slower encoder
389			if (encoder->width * encoder->height > 1000000) {
390				av_opt_set(encoder->video->priv_data, "preset", "superfast", 0);
391			} else if (encoder->width * encoder->height > 500000) {
392				av_opt_set(encoder->video->priv_data, "preset", "veryfast", 0);
393			} else {
394				av_opt_set(encoder->video->priv_data, "preset", "faster", 0);
395			}
396			if (encoder->videoBitrate == 0) {
397				av_opt_set(encoder->video->priv_data, "crf", "0", 0);
398				encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
399			}
400		}
401		if (strcmp(vcodec->name, "libvpx-vp9") == 0 && encoder->videoBitrate == 0) {
402			av_opt_set(encoder->video->priv_data, "lossless", "1", 0);
403			encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
404		}
405
406		if (encoder->pixFormat == AV_PIX_FMT_PAL8) {
407			encoder->graph = avfilter_graph_alloc();
408
409			const struct AVFilter* source = avfilter_get_by_name("buffer");
410			const struct AVFilter* sink = avfilter_get_by_name("buffersink");
411			const struct AVFilter* split = avfilter_get_by_name("split");
412			const struct AVFilter* palettegen = avfilter_get_by_name("palettegen");
413			const struct AVFilter* paletteuse = avfilter_get_by_name("paletteuse");
414
415			if (!source || !sink || !split || !palettegen || !paletteuse || !encoder->graph) {
416				FFmpegEncoderClose(encoder);
417				return false;
418			}
419
420			char args[256];
421			snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d",
422			         encoder->video->width, encoder->video->height, encoder->ipixFormat,
423			         encoder->video->time_base.num, encoder->video->time_base.den);
424
425			int res = 0;
426			res |= avfilter_graph_create_filter(&encoder->source, source, NULL, args, NULL, encoder->graph);
427			res |= avfilter_graph_create_filter(&encoder->sink, sink, NULL, NULL, NULL, encoder->graph);
428			res |= avfilter_graph_create_filter(&encoder->filters[0], split, NULL, NULL, NULL, encoder->graph);
429			res |= avfilter_graph_create_filter(&encoder->filters[1], palettegen, NULL, "reserve_transparent=off", NULL, encoder->graph);
430			res |= avfilter_graph_create_filter(&encoder->filters[2], paletteuse, NULL, "dither=none", NULL, encoder->graph);
431			if (res < 0) {
432				FFmpegEncoderClose(encoder);
433				return false;
434			}
435
436			res = 0;
437			res |= avfilter_link(encoder->source, 0, encoder->filters[0], 0);
438			res |= avfilter_link(encoder->filters[0], 0, encoder->filters[1], 0);
439			res |= avfilter_link(encoder->filters[0], 1, encoder->filters[2], 0);
440			res |= avfilter_link(encoder->filters[1], 0, encoder->filters[2], 1);
441			res |= avfilter_link(encoder->filters[2], 0, encoder->sink, 0);
442			if (res < 0 || avfilter_graph_config(encoder->graph, NULL) < 0) {
443				FFmpegEncoderClose(encoder);
444				return false;
445			}
446
447#if LIBAVCODEC_VERSION_MAJOR >= 55
448			encoder->sinkFrame = av_frame_alloc();
449#else
450			encoder->sinkFrame = avcodec_alloc_frame();
451#endif
452		}
453		AVDictionary* opts = 0;
454		av_dict_set(&opts, "strict", "-2", 0);
455		int res = avcodec_open2(encoder->video, vcodec, &opts);
456		av_dict_free(&opts);
457		if (res < 0) {
458			FFmpegEncoderClose(encoder);
459			return false;
460		}
461#if LIBAVCODEC_VERSION_MAJOR >= 55
462		encoder->videoFrame = av_frame_alloc();
463#else
464		encoder->videoFrame = avcodec_alloc_frame();
465#endif
466		encoder->videoFrame->format = encoder->video->pix_fmt != AV_PIX_FMT_PAL8 ? encoder->video->pix_fmt : encoder->ipixFormat;
467		encoder->videoFrame->width = encoder->video->width;
468		encoder->videoFrame->height = encoder->video->height;
469		encoder->videoFrame->pts = 0;
470		_ffmpegSetVideoDimensions(&encoder->d, encoder->iwidth, encoder->iheight);
471		av_image_alloc(encoder->videoFrame->data, encoder->videoFrame->linesize, encoder->videoFrame->width, encoder->videoFrame->height, encoder->videoFrame->format, 32);
472#ifdef FFMPEG_USE_CODECPAR
473		avcodec_parameters_from_context(encoder->videoStream->codecpar, encoder->video);
474#endif
475	}
476
477	if (strcmp(encoder->containerFormat, "gif") == 0) {
478		av_opt_set(encoder->context->priv_data, "loop", encoder->loop ? "0" : "-1", 0);
479	} else if (strcmp(encoder->containerFormat, "apng") == 0) {
480		av_opt_set(encoder->context->priv_data, "plays", encoder->loop ? "0" : "1", 0);
481	}
482
483	AVDictionary* opts = 0;
484	av_dict_set(&opts, "strict", "-2", 0);
485	bool res = avio_open(&encoder->context->pb, outfile, AVIO_FLAG_WRITE) < 0 || avformat_write_header(encoder->context, &opts) < 0;
486	av_dict_free(&opts);
487	if (res) {
488		FFmpegEncoderClose(encoder);
489		return false;
490	}
491	return true;
492}
493
494void FFmpegEncoderClose(struct FFmpegEncoder* encoder) {
495	if (encoder->audio) {
496		while (true) {
497			if (!_ffmpegWriteAudioFrame(encoder, NULL)) {
498				break;
499			}
500		}
501	}
502	if (encoder->video) {
503		if (encoder->graph) {
504			if (av_buffersrc_add_frame(encoder->source, NULL) >= 0) {
505				while (true) {
506					int res = av_buffersink_get_frame(encoder->sink, encoder->sinkFrame);
507					if (res < 0) {
508						break;
509					}
510					_ffmpegWriteVideoFrame(encoder, encoder->sinkFrame);
511					av_frame_unref(encoder->sinkFrame);
512				}
513			}
514		}
515		while (true) {
516			if (!_ffmpegWriteVideoFrame(encoder, NULL)) {
517				break;
518			}
519		}
520	}
521
522	if (encoder->context && encoder->context->pb) {
523		av_write_trailer(encoder->context);
524		avio_close(encoder->context->pb);
525	}
526
527	if (encoder->postaudioBuffer) {
528		av_free(encoder->postaudioBuffer);
529		encoder->postaudioBuffer = NULL;
530	}
531	if (encoder->audioBuffer) {
532		av_free(encoder->audioBuffer);
533		encoder->audioBuffer = NULL;
534	}
535
536	if (encoder->audioFrame) {
537#if LIBAVCODEC_VERSION_MAJOR >= 55
538		av_frame_free(&encoder->audioFrame);
539#else
540		avcodec_free_frame(&encoder->audioFrame);
541#endif
542	}
543	if (encoder->audio) {
544		avcodec_close(encoder->audio);
545		encoder->audio = NULL;
546	}
547
548	if (encoder->resampleContext) {
549#ifdef USE_LIBAVRESAMPLE
550		avresample_close(encoder->resampleContext);
551		encoder->resampleContext = NULL;
552#else
553		swr_free(&encoder->resampleContext);
554#endif
555	}
556
557	if (encoder->absf) {
558#ifdef FFMPEG_USE_NEW_BSF
559		av_bsf_free(&encoder->absf);
560#else
561		av_bitstream_filter_close(encoder->absf);
562		encoder->absf = NULL;
563#endif
564	}
565
566	if (encoder->videoFrame) {
567#if LIBAVCODEC_VERSION_MAJOR >= 55
568		av_frame_free(&encoder->videoFrame);
569#else
570		avcodec_free_frame(&encoder->videoFrame);
571#endif
572	}
573
574	if (encoder->sinkFrame) {
575#if LIBAVCODEC_VERSION_MAJOR >= 55
576		av_frame_free(&encoder->sinkFrame);
577#else
578		avcodec_free_frame(&encoder->sinkFrame);
579#endif
580		encoder->sinkFrame = NULL;
581	}
582
583	if (encoder->video) {
584		avcodec_close(encoder->video);
585		encoder->video = NULL;
586	}
587
588	if (encoder->scaleContext) {
589		sws_freeContext(encoder->scaleContext);
590		encoder->scaleContext = NULL;
591	}
592
593	if (encoder->graph) {
594		avfilter_graph_free(&encoder->graph);
595		encoder->graph = NULL;
596		encoder->source = NULL;
597		encoder->sink = NULL;
598
599		int i;
600		for (i = 0; i < FFMPEG_FILTERS_MAX; ++i) {
601			encoder->filters[i] = NULL;
602		}
603	}
604
605	if (encoder->context) {
606		avformat_free_context(encoder->context);
607		encoder->context = NULL;
608	}
609}
610
611bool FFmpegEncoderIsOpen(struct FFmpegEncoder* encoder) {
612	return !!encoder->context;
613}
614
615void _ffmpegPostAudioFrame(struct mAVStream* stream, int16_t left, int16_t right) {
616	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
617	if (!encoder->context || !encoder->audioCodec) {
618		return;
619	}
620
621	if (encoder->absf && !left) {
622		// XXX: AVBSF doesn't like silence. Figure out why.
623		left = 1;
624	}
625
626	encoder->audioBuffer[encoder->currentAudioSample * 2] = left;
627	encoder->audioBuffer[encoder->currentAudioSample * 2 + 1] = right;
628
629	++encoder->currentAudioSample;
630
631	if (encoder->currentAudioSample * 4 < encoder->audioBufferSize) {
632		return;
633	}
634
635	int channelSize = 2 * av_get_bytes_per_sample(encoder->audio->sample_fmt);
636	encoder->currentAudioSample = 0;
637#ifdef USE_LIBAVRESAMPLE
638	avresample_convert(encoder->resampleContext, 0, 0, 0,
639	                   (uint8_t**) &encoder->audioBuffer, 0, encoder->audioBufferSize / 4);
640
641	if (avresample_available(encoder->resampleContext) < encoder->audioFrame->nb_samples) {
642		return;
643	}
644#if LIBAVCODEC_VERSION_MAJOR >= 55
645	av_frame_make_writable(encoder->audioFrame);
646#endif
647	int samples = avresample_read(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize);
648#else
649#if LIBAVCODEC_VERSION_MAJOR >= 55
650	av_frame_make_writable(encoder->audioFrame);
651#endif
652	if (swr_get_out_samples(encoder->resampleContext, 1) < encoder->audioFrame->nb_samples) {
653		swr_convert(encoder->resampleContext, NULL, 0, (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
654		return;
655	}
656	int samples = swr_convert(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize,
657	                          (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
658#endif
659
660	encoder->audioFrame->pts = encoder->currentAudioFrame;
661	encoder->currentAudioFrame += samples;
662
663	_ffmpegWriteAudioFrame(encoder, encoder->audioFrame);
664}
665
666bool _ffmpegWriteAudioFrame(struct FFmpegEncoder* encoder, struct AVFrame* audioFrame) {
667	AVPacket packet;
668	av_init_packet(&packet);
669	packet.data = 0;
670	packet.size = 0;
671
672	int gotData;
673#ifdef FFMPEG_USE_PACKETS
674	avcodec_send_frame(encoder->audio, audioFrame);
675	gotData = avcodec_receive_packet(encoder->audio, &packet);
676	gotData = (gotData == 0) && packet.size;
677#else
678	avcodec_encode_audio2(encoder->audio, &packet, audioFrame, &gotData);
679#endif
680	packet.pts = av_rescale_q(packet.pts, encoder->audio->time_base, encoder->audioStream->time_base);
681	packet.dts = packet.pts;
682
683	if (gotData) {
684		if (encoder->absf) {
685			AVPacket tempPacket;
686
687#ifdef FFMPEG_USE_NEW_BSF
688			int success = av_bsf_send_packet(encoder->absf, &packet);
689			if (success >= 0) {
690				success = av_bsf_receive_packet(encoder->absf, &tempPacket);
691			}
692#else
693			int success = av_bitstream_filter_filter(encoder->absf, encoder->audio, 0,
694			    &tempPacket.data, &tempPacket.size,
695			    packet.data, packet.size, 0);
696#endif
697
698			if (success >= 0) {
699#if LIBAVUTIL_VERSION_MAJOR >= 53
700				tempPacket.buf = av_buffer_create(tempPacket.data, tempPacket.size, av_buffer_default_free, 0, 0);
701#endif
702
703#ifdef FFMPEG_USE_PACKET_UNREF
704				av_packet_move_ref(&packet, &tempPacket);
705#else
706				av_free_packet(&packet);
707				packet = tempPacket;
708#endif
709
710				packet.stream_index = encoder->audioStream->index;
711				av_interleaved_write_frame(encoder->context, &packet);
712			}
713		} else {
714			packet.stream_index = encoder->audioStream->index;
715			av_interleaved_write_frame(encoder->context, &packet);
716		}
717	}
718#ifdef FFMPEG_USE_PACKET_UNREF
719	av_packet_unref(&packet);
720#else
721	av_free_packet(&packet);
722#endif
723	return gotData;
724}
725
726void _ffmpegPostVideoFrame(struct mAVStream* stream, const color_t* pixels, size_t stride) {
727	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
728	if (!encoder->context || !encoder->videoCodec) {
729		return;
730	}
731	encoder->skipResidue = (encoder->skipResidue + 1) % encoder->frameskip;
732	if (encoder->skipResidue) {
733		return;
734	}
735	stride *= BYTES_PER_PIXEL;
736
737#if LIBAVCODEC_VERSION_MAJOR >= 55
738	av_frame_make_writable(encoder->videoFrame);
739#endif
740	encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
741	++encoder->currentVideoFrame;
742
743	sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, encoder->iheight, encoder->videoFrame->data, encoder->videoFrame->linesize);
744
745	if (encoder->graph) {
746		if (av_buffersrc_add_frame(encoder->source, encoder->videoFrame) < 0) {
747			return;
748		}
749		while (true) {
750			int res = av_buffersink_get_frame(encoder->sink, encoder->sinkFrame);
751			if (res < 0) {
752				break;
753			}
754			_ffmpegWriteVideoFrame(encoder, encoder->sinkFrame);
755			av_frame_unref(encoder->sinkFrame);
756		}
757	} else {
758		_ffmpegWriteVideoFrame(encoder, encoder->videoFrame);
759	}
760}
761
762bool _ffmpegWriteVideoFrame(struct FFmpegEncoder* encoder, struct AVFrame* videoFrame) {
763	AVPacket packet;
764
765	av_init_packet(&packet);
766	packet.data = 0;
767	packet.size = 0;
768
769	int gotData;
770#ifdef FFMPEG_USE_PACKETS
771	avcodec_send_frame(encoder->video, videoFrame);
772	gotData = avcodec_receive_packet(encoder->video, &packet) == 0;
773#else
774	avcodec_encode_video2(encoder->video, &packet, videoFrame, &gotData);
775#endif
776	if (gotData) {
777#ifndef FFMPEG_USE_PACKET_UNREF
778		if (encoder->video->coded_frame->key_frame) {
779			packet.flags |= AV_PKT_FLAG_KEY;
780		}
781#endif
782		packet.stream_index = encoder->videoStream->index;
783		av_interleaved_write_frame(encoder->context, &packet);
784	}
785#ifdef FFMPEG_USE_PACKET_UNREF
786	av_packet_unref(&packet);
787#else
788	av_free_packet(&packet);
789#endif
790
791	return gotData;
792}
793
794static void _ffmpegSetVideoDimensions(struct mAVStream* stream, unsigned width, unsigned height) {
795	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
796	if (!encoder->context || !encoder->videoCodec) {
797		return;
798	}
799	encoder->iwidth = width;
800	encoder->iheight = height;
801	if (encoder->scaleContext) {
802		sws_freeContext(encoder->scaleContext);
803	}
804	encoder->scaleContext = sws_getContext(encoder->iwidth, encoder->iheight, encoder->ipixFormat,
805	    encoder->videoFrame->width, encoder->videoFrame->height, encoder->videoFrame->format,
806	    SWS_POINT, 0, 0, 0);
807}