all repos — mgba @ d33f1d13926f4aa2afebad31775222cdd278411c

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