all repos — mgba @ bb7f41e8cca003d57f7470293a9951d9e027a8a5

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 <libavutil/version.h>
 15#if LIBAVUTIL_VERSION_MAJOR >= 53
 16#include <libavutil/buffer.h>
 17#endif
 18#include <libavutil/imgutils.h>
 19#include <libavutil/mathematics.h>
 20#include <libavutil/opt.h>
 21
 22#ifdef USE_LIBAVRESAMPLE
 23#include <libavresample/avresample.h>
 24#else
 25#include <libswresample/swresample.h>
 26#endif
 27#include <libswscale/swscale.h>
 28
 29static void _ffmpegPostVideoFrame(struct mAVStream*, const color_t* pixels, size_t stride);
 30static void _ffmpegPostAudioFrame(struct mAVStream*, int16_t left, int16_t right);
 31static void _ffmpegSetVideoDimensions(struct mAVStream*, unsigned width, unsigned height);
 32
 33enum {
 34	PREFERRED_SAMPLE_RATE = 0x8000
 35};
 36
 37void FFmpegEncoderInit(struct FFmpegEncoder* encoder) {
 38	av_register_all();
 39
 40	encoder->d.videoDimensionsChanged = _ffmpegSetVideoDimensions;
 41	encoder->d.postVideoFrame = _ffmpegPostVideoFrame;
 42	encoder->d.postAudioFrame = _ffmpegPostAudioFrame;
 43	encoder->d.postAudioBuffer = 0;
 44
 45	encoder->audioCodec = NULL;
 46	encoder->videoCodec = NULL;
 47	encoder->containerFormat = NULL;
 48	FFmpegEncoderSetAudio(encoder, "flac", 0);
 49	FFmpegEncoderSetVideo(encoder, "png", 0);
 50	FFmpegEncoderSetContainer(encoder, "matroska");
 51	FFmpegEncoderSetDimensions(encoder, GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
 52	encoder->iwidth = GBA_VIDEO_HORIZONTAL_PIXELS;
 53	encoder->iheight = GBA_VIDEO_VERTICAL_PIXELS;
 54	encoder->resampleContext = NULL;
 55	encoder->absf = NULL;
 56	encoder->context = NULL;
 57	encoder->scaleContext = NULL;
 58	encoder->audio = NULL;
 59	encoder->audioStream = NULL;
 60	encoder->audioFrame = NULL;
 61	encoder->audioBuffer = NULL;
 62	encoder->postaudioBuffer = NULL;
 63	encoder->video = NULL;
 64	encoder->videoStream = NULL;
 65	encoder->videoFrame = NULL;
 66}
 67
 68bool FFmpegEncoderSetAudio(struct FFmpegEncoder* encoder, const char* acodec, unsigned abr) {
 69	static const struct {
 70		int format;
 71		int priority;
 72	} priorities[] = {
 73		{ AV_SAMPLE_FMT_S16, 0 },
 74		{ AV_SAMPLE_FMT_S16P, 1 },
 75		{ AV_SAMPLE_FMT_S32, 2 },
 76		{ AV_SAMPLE_FMT_S32P, 2 },
 77		{ AV_SAMPLE_FMT_FLT, 3 },
 78		{ AV_SAMPLE_FMT_FLTP, 3 },
 79		{ AV_SAMPLE_FMT_DBL, 4 },
 80		{ AV_SAMPLE_FMT_DBLP, 4 }
 81	};
 82
 83	if (!acodec) {
 84		encoder->audioCodec = 0;
 85		return true;
 86	}
 87
 88	AVCodec* codec = avcodec_find_encoder_by_name(acodec);
 89	if (!codec) {
 90		return false;
 91	}
 92
 93	if (!codec->sample_fmts) {
 94		return false;
 95	}
 96	size_t i;
 97	size_t j;
 98	int priority = INT_MAX;
 99	encoder->sampleFormat = AV_SAMPLE_FMT_NONE;
100	for (i = 0; codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; ++i) {
101		for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
102			if (codec->sample_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
103				priority = priorities[j].priority;
104				encoder->sampleFormat = codec->sample_fmts[i];
105			}
106		}
107	}
108	if (encoder->sampleFormat == AV_SAMPLE_FMT_NONE) {
109		return false;
110	}
111	encoder->sampleRate = PREFERRED_SAMPLE_RATE;
112	if (codec->supported_samplerates) {
113		for (i = 0; codec->supported_samplerates[i]; ++i) {
114			if (codec->supported_samplerates[i] < PREFERRED_SAMPLE_RATE) {
115				continue;
116			}
117			if (encoder->sampleRate == PREFERRED_SAMPLE_RATE || encoder->sampleRate > codec->supported_samplerates[i]) {
118				encoder->sampleRate = codec->supported_samplerates[i];
119			}
120		}
121	} else if (codec->id == AV_CODEC_ID_AAC) {
122		// HACK: AAC doesn't support 32768Hz (it rounds to 32000), but libfaac doesn't tell us that
123		encoder->sampleRate = 44100;
124	}
125	encoder->audioCodec = acodec;
126	encoder->audioBitrate = abr;
127	return true;
128}
129
130bool FFmpegEncoderSetVideo(struct FFmpegEncoder* encoder, const char* vcodec, unsigned vbr) {
131	static const struct {
132		enum AVPixelFormat format;
133		int priority;
134	} priorities[] = {
135		{ AV_PIX_FMT_RGB555, 0 },
136		{ AV_PIX_FMT_BGR555, 0 },
137		{ AV_PIX_FMT_RGB565, 1 },
138		{ AV_PIX_FMT_BGR565, 1 },
139		{ AV_PIX_FMT_RGB24, 2 },
140		{ AV_PIX_FMT_BGR24, 2 },
141#ifndef USE_LIBAV
142		{ AV_PIX_FMT_BGR0, 3 },
143		{ AV_PIX_FMT_RGB0, 3 },
144		{ AV_PIX_FMT_0BGR, 3 },
145		{ AV_PIX_FMT_0RGB, 3 },
146#endif
147		{ AV_PIX_FMT_YUV422P, 4 },
148		{ AV_PIX_FMT_YUV444P, 5 },
149		{ AV_PIX_FMT_YUV420P, 6 }
150	};
151
152	if (!vcodec) {
153		encoder->videoCodec = 0;
154		return true;
155	}
156
157	AVCodec* codec = avcodec_find_encoder_by_name(vcodec);
158	if (!codec) {
159		return false;
160	}
161
162	size_t i;
163	size_t j;
164	int priority = INT_MAX;
165	encoder->pixFormat = AV_PIX_FMT_NONE;
166	for (i = 0; codec->pix_fmts[i] != AV_PIX_FMT_NONE; ++i) {
167		for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
168			if (codec->pix_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
169				priority = priorities[j].priority;
170				encoder->pixFormat = codec->pix_fmts[i];
171			}
172		}
173	}
174	if (encoder->pixFormat == AV_PIX_FMT_NONE) {
175		return false;
176	}
177	encoder->videoCodec = vcodec;
178	encoder->videoBitrate = vbr;
179	return true;
180}
181
182bool FFmpegEncoderSetContainer(struct FFmpegEncoder* encoder, const char* container) {
183	AVOutputFormat* oformat = av_guess_format(container, 0, 0);
184	if (!oformat) {
185		return false;
186	}
187	encoder->containerFormat = container;
188	return true;
189}
190
191void FFmpegEncoderSetDimensions(struct FFmpegEncoder* encoder, int width, int height) {
192	encoder->width = width > 0 ? width : GBA_VIDEO_HORIZONTAL_PIXELS;
193	encoder->height = height > 0 ? height : GBA_VIDEO_VERTICAL_PIXELS;
194}
195
196bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder* encoder) {
197	AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
198	AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
199	AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
200	if ((encoder->audioCodec && !acodec) || (encoder->videoCodec && !vcodec) || !oformat) {
201		return false;
202	}
203	if (encoder->audioCodec && !avformat_query_codec(oformat, acodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
204		return false;
205	}
206	if (encoder->videoCodec && !avformat_query_codec(oformat, vcodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
207		return false;
208	}
209	return true;
210}
211
212bool FFmpegEncoderOpen(struct FFmpegEncoder* encoder, const char* outfile) {
213	AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
214	AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
215	if ((encoder->audioCodec && !acodec) || (encoder->videoCodec && !vcodec) || !FFmpegEncoderVerifyContainer(encoder)) {
216		return false;
217	}
218
219	if (encoder->context) {
220		return false;
221	}
222
223	encoder->currentAudioSample = 0;
224	encoder->currentAudioFrame = 0;
225	encoder->currentVideoFrame = 0;
226
227	AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
228#ifndef USE_LIBAV
229	avformat_alloc_output_context2(&encoder->context, oformat, 0, outfile);
230#else
231	encoder->context = avformat_alloc_context();
232	strncpy(encoder->context->filename, outfile, sizeof(encoder->context->filename) - 1);
233	encoder->context->filename[sizeof(encoder->context->filename) - 1] = '\0';
234	encoder->context->oformat = oformat;
235#endif
236
237	if (acodec) {
238#ifdef FFMPEG_USE_CODECPAR
239		encoder->audioStream = avformat_new_stream(encoder->context, NULL);
240		encoder->audio = avcodec_alloc_context3(acodec);
241#else
242		encoder->audioStream = avformat_new_stream(encoder->context, acodec);
243		encoder->audio = encoder->audioStream->codec;
244#endif
245		encoder->audio->bit_rate = encoder->audioBitrate;
246		encoder->audio->channels = 2;
247		encoder->audio->channel_layout = AV_CH_LAYOUT_STEREO;
248		encoder->audio->sample_rate = encoder->sampleRate;
249		encoder->audio->sample_fmt = encoder->sampleFormat;
250		AVDictionary* opts = 0;
251		av_dict_set(&opts, "strict", "-2", 0);
252		if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
253#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
254			encoder->audio->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
255#else
256			encoder->audio->flags |= CODEC_FLAG_GLOBAL_HEADER;
257#endif
258		}
259		int res = avcodec_open2(encoder->audio, acodec, &opts);
260		av_dict_free(&opts);
261		if (res < 0) {
262			FFmpegEncoderClose(encoder);
263			return false;
264		}
265#if LIBAVCODEC_VERSION_MAJOR >= 55
266		encoder->audioFrame = av_frame_alloc();
267#else
268		encoder->audioFrame = avcodec_alloc_frame();
269#endif
270		if (!encoder->audio->frame_size) {
271			encoder->audio->frame_size = 1;
272		}
273		encoder->audioFrame->nb_samples = encoder->audio->frame_size;
274		encoder->audioFrame->format = encoder->audio->sample_fmt;
275		encoder->audioFrame->pts = 0;
276#ifdef USE_LIBAVRESAMPLE
277		encoder->resampleContext = avresample_alloc_context();
278		av_opt_set_int(encoder->resampleContext, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
279		av_opt_set_int(encoder->resampleContext, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
280		av_opt_set_int(encoder->resampleContext, "in_sample_rate", PREFERRED_SAMPLE_RATE, 0);
281		av_opt_set_int(encoder->resampleContext, "out_sample_rate", encoder->sampleRate, 0);
282		av_opt_set_int(encoder->resampleContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
283		av_opt_set_int(encoder->resampleContext, "out_sample_fmt", encoder->sampleFormat, 0);
284		avresample_open(encoder->resampleContext);
285#else
286		encoder->resampleContext = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, encoder->sampleFormat, encoder->sampleRate,
287		                                              AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, PREFERRED_SAMPLE_RATE, 0, NULL);
288		swr_init(encoder->resampleContext);
289#endif
290		encoder->audioBufferSize = (encoder->audioFrame->nb_samples * PREFERRED_SAMPLE_RATE / encoder->sampleRate) * 4;
291		encoder->audioBuffer = av_malloc(encoder->audioBufferSize);
292		encoder->postaudioBufferSize = av_samples_get_buffer_size(0, encoder->audio->channels, encoder->audio->frame_size, encoder->audio->sample_fmt, 0);
293		encoder->postaudioBuffer = av_malloc(encoder->postaudioBufferSize);
294		avcodec_fill_audio_frame(encoder->audioFrame, encoder->audio->channels, encoder->audio->sample_fmt, (const uint8_t*) encoder->postaudioBuffer, encoder->postaudioBufferSize, 0);
295
296		if (encoder->audio->codec->id == AV_CODEC_ID_AAC &&
297		    (strcasecmp(encoder->containerFormat, "mp4") ||
298		        strcasecmp(encoder->containerFormat, "m4v") ||
299		        strcasecmp(encoder->containerFormat, "mov"))) {
300			// MP4 container doesn't support the raw ADTS AAC format that the encoder spits out
301#ifdef FFMPEG_USE_NEW_BSF
302			av_bsf_alloc(av_bsf_get_by_name("aac_adtstoasc"), &encoder->absf);
303			avcodec_parameters_from_context(encoder->absf->par_in, encoder->audio);
304			av_bsf_init(encoder->absf);
305#else
306			encoder->absf = av_bitstream_filter_init("aac_adtstoasc");
307#endif
308		}
309#ifdef FFMPEG_USE_CODECPAR
310		avcodec_parameters_from_context(encoder->audioStream->codecpar, encoder->audio);
311#endif
312	}
313
314	if (vcodec) {
315#ifdef FFMPEG_USE_CODECPAR
316		encoder->videoStream = avformat_new_stream(encoder->context, NULL);
317		encoder->video = avcodec_alloc_context3(vcodec);
318#else
319		encoder->videoStream = avformat_new_stream(encoder->context, vcodec);
320		encoder->video = encoder->videoStream->codec;
321#endif
322		encoder->video->bit_rate = encoder->videoBitrate;
323		encoder->video->width = encoder->width;
324		encoder->video->height = encoder->height;
325		encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH, GBA_ARM7TDMI_FREQUENCY };
326		encoder->video->framerate = (AVRational) { GBA_ARM7TDMI_FREQUENCY, VIDEO_TOTAL_LENGTH };
327		encoder->video->pix_fmt = encoder->pixFormat;
328		encoder->video->gop_size = 60;
329		encoder->video->max_b_frames = 3;
330		if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
331#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
332			encoder->video->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
333#else
334			encoder->video->flags |= CODEC_FLAG_GLOBAL_HEADER;
335#endif
336		}
337
338		if (encoder->video->codec->id == AV_CODEC_ID_H264 &&
339		    (strcasecmp(encoder->containerFormat, "mp4") ||
340		        strcasecmp(encoder->containerFormat, "m4v") ||
341		        strcasecmp(encoder->containerFormat, "mov"))) {
342			// QuickTime and a few other things require YUV420
343			encoder->video->pix_fmt = AV_PIX_FMT_YUV420P;
344		}
345
346		if (strcmp(vcodec->name, "libx264") == 0) {
347			// Try to adaptively figure out when you can use a slower encoder
348			if (encoder->width * encoder->height > 1000000) {
349				av_opt_set(encoder->video->priv_data, "preset", "superfast", 0);
350			} else if (encoder->width * encoder->height > 500000) {
351				av_opt_set(encoder->video->priv_data, "preset", "veryfast", 0);
352			} else {
353				av_opt_set(encoder->video->priv_data, "preset", "faster", 0);
354			}
355			if (encoder->videoBitrate == 0) {
356				av_opt_set(encoder->video->priv_data, "crf", "0", 0);
357				encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
358			}
359		}
360		if (strcmp(vcodec->name, "libvpx-vp9") == 0 && encoder->videoBitrate == 0) {
361			av_opt_set(encoder->video->priv_data, "lossless", "1", 0);
362			encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
363		}
364
365		if (avcodec_open2(encoder->video, vcodec, 0) < 0) {
366			FFmpegEncoderClose(encoder);
367			return false;
368		}
369#if LIBAVCODEC_VERSION_MAJOR >= 55
370		encoder->videoFrame = av_frame_alloc();
371#else
372		encoder->videoFrame = avcodec_alloc_frame();
373#endif
374		encoder->videoFrame->format = encoder->video->pix_fmt;
375		encoder->videoFrame->width = encoder->video->width;
376		encoder->videoFrame->height = encoder->video->height;
377		encoder->videoFrame->pts = 0;
378		_ffmpegSetVideoDimensions(&encoder->d, encoder->iwidth, encoder->iheight);
379		av_image_alloc(encoder->videoFrame->data, encoder->videoFrame->linesize, encoder->video->width, encoder->video->height, encoder->video->pix_fmt, 32);
380#ifdef FFMPEG_USE_CODECPAR
381		avcodec_parameters_from_context(encoder->videoStream->codecpar, encoder->video);
382#endif
383	}
384
385	if (avio_open(&encoder->context->pb, outfile, AVIO_FLAG_WRITE) < 0 || avformat_write_header(encoder->context, 0) < 0) {
386		FFmpegEncoderClose(encoder);
387		return false;
388	}
389	return true;
390}
391
392void FFmpegEncoderClose(struct FFmpegEncoder* encoder) {
393	if (encoder->context && encoder->context->pb) {
394		av_write_trailer(encoder->context);
395		avio_close(encoder->context->pb);
396	}
397
398	if (encoder->postaudioBuffer) {
399		av_free(encoder->postaudioBuffer);
400		encoder->postaudioBuffer = NULL;
401	}
402	if (encoder->audioBuffer) {
403		av_free(encoder->audioBuffer);
404		encoder->audioBuffer = NULL;
405	}
406
407	if (encoder->audioFrame) {
408#if LIBAVCODEC_VERSION_MAJOR >= 55
409		av_frame_free(&encoder->audioFrame);
410#else
411		avcodec_free_frame(&encoder->audioFrame);
412#endif
413	}
414	if (encoder->audio) {
415		avcodec_close(encoder->audio);
416		encoder->audio = NULL;
417	}
418
419	if (encoder->resampleContext) {
420#ifdef USE_LIBAVRESAMPLE
421		avresample_close(encoder->resampleContext);
422		encoder->resampleContext = NULL;
423#else
424		swr_free(&encoder->resampleContext);
425#endif
426	}
427
428	if (encoder->absf) {
429#ifdef FFMPEG_USE_NEW_BSF
430		av_bsf_free(&encoder->absf);
431#else
432		av_bitstream_filter_close(encoder->absf);
433		encoder->absf = NULL;
434#endif
435	}
436
437	if (encoder->videoFrame) {
438#if LIBAVCODEC_VERSION_MAJOR >= 55
439		av_frame_free(&encoder->videoFrame);
440#else
441		avcodec_free_frame(&encoder->videoFrame);
442#endif
443	}
444
445	if (encoder->video) {
446		avcodec_close(encoder->video);
447		encoder->video = NULL;
448	}
449
450	if (encoder->scaleContext) {
451		sws_freeContext(encoder->scaleContext);
452		encoder->scaleContext = NULL;
453	}
454
455	if (encoder->context) {
456		avformat_free_context(encoder->context);
457		encoder->context = NULL;
458	}
459}
460
461bool FFmpegEncoderIsOpen(struct FFmpegEncoder* encoder) {
462	return !!encoder->context;
463}
464
465void _ffmpegPostAudioFrame(struct mAVStream* stream, int16_t left, int16_t right) {
466	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
467	if (!encoder->context || !encoder->audioCodec) {
468		return;
469	}
470
471	if (encoder->absf && !left) {
472		// XXX: AVBSF doesn't like silence. Figure out why.
473		left = 1;
474	}
475
476	encoder->audioBuffer[encoder->currentAudioSample * 2] = left;
477	encoder->audioBuffer[encoder->currentAudioSample * 2 + 1] = right;
478
479	++encoder->currentAudioSample;
480
481	if (encoder->currentAudioSample * 4 < encoder->audioBufferSize) {
482		return;
483	}
484
485	int channelSize = 2 * av_get_bytes_per_sample(encoder->audio->sample_fmt);
486	encoder->currentAudioSample = 0;
487#ifdef USE_LIBAVRESAMPLE
488	avresample_convert(encoder->resampleContext, 0, 0, 0,
489	                   (uint8_t**) &encoder->audioBuffer, 0, encoder->audioBufferSize / 4);
490
491	if (avresample_available(encoder->resampleContext) < encoder->audioFrame->nb_samples) {
492		return;
493	}
494#if LIBAVCODEC_VERSION_MAJOR >= 55
495	av_frame_make_writable(encoder->audioFrame);
496#endif
497	int samples = avresample_read(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize);
498#else
499#if LIBAVCODEC_VERSION_MAJOR >= 55
500	av_frame_make_writable(encoder->audioFrame);
501#endif
502	if (swr_get_out_samples(encoder->resampleContext, 1) < encoder->audioFrame->nb_samples) {
503		swr_convert(encoder->resampleContext, NULL, 0, (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
504		return;
505	}
506	int samples = swr_convert(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize,
507	                          (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
508#endif
509
510	encoder->audioFrame->pts = encoder->currentAudioFrame;
511	encoder->currentAudioFrame += samples;
512
513	AVPacket packet;
514	av_init_packet(&packet);
515	packet.data = 0;
516	packet.size = 0;
517
518	int gotData;
519#ifdef FFMPEG_USE_PACKETS
520	avcodec_send_frame(encoder->audio, encoder->audioFrame);
521	gotData = avcodec_receive_packet(encoder->audio, &packet);
522	gotData = (gotData == 0) && packet.size;
523#else
524	avcodec_encode_audio2(encoder->audio, &packet, encoder->audioFrame, &gotData);
525#endif
526	packet.pts = av_rescale_q(encoder->audioFrame->pts, encoder->audio->time_base, encoder->audioStream->time_base);
527	packet.dts = packet.pts;
528
529	if (gotData) {
530		if (encoder->absf) {
531			AVPacket tempPacket;
532
533#ifdef FFMPEG_USE_NEW_BSF
534			int success = av_bsf_send_packet(encoder->absf, &packet);
535			if (success >= 0) {
536				success = av_bsf_receive_packet(encoder->absf, &tempPacket);
537			}
538#else
539			int success = av_bitstream_filter_filter(encoder->absf, encoder->audio, 0,
540			    &tempPacket.data, &tempPacket.size,
541			    packet.data, packet.size, 0);
542#endif
543
544			if (success >= 0) {
545#if LIBAVUTIL_VERSION_MAJOR >= 53
546				tempPacket.buf = av_buffer_create(tempPacket.data, tempPacket.size, av_buffer_default_free, 0, 0);
547#endif
548
549#ifdef FFMPEG_USE_PACKET_UNREF
550				av_packet_move_ref(&packet, &tempPacket);
551#else
552				av_free_packet(&packet);
553				packet = tempPacket;
554#endif
555
556				packet.stream_index = encoder->audioStream->index;
557				av_interleaved_write_frame(encoder->context, &packet);
558			}
559		} else {
560			packet.stream_index = encoder->audioStream->index;
561			av_interleaved_write_frame(encoder->context, &packet);
562		}
563	}
564#ifdef FFMPEG_USE_PACKET_UNREF
565	av_packet_unref(&packet);
566#else
567	av_free_packet(&packet);
568#endif
569}
570
571void _ffmpegPostVideoFrame(struct mAVStream* stream, const color_t* pixels, size_t stride) {
572	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
573	if (!encoder->context || !encoder->videoCodec) {
574		return;
575	}
576	stride *= BYTES_PER_PIXEL;
577
578	AVPacket packet;
579
580	av_init_packet(&packet);
581	packet.data = 0;
582	packet.size = 0;
583#if LIBAVCODEC_VERSION_MAJOR >= 55
584	av_frame_make_writable(encoder->videoFrame);
585#endif
586	encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
587	++encoder->currentVideoFrame;
588
589	sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, encoder->iheight, encoder->videoFrame->data, encoder->videoFrame->linesize);
590
591	int gotData;
592#ifdef FFMPEG_USE_PACKETS
593	avcodec_send_frame(encoder->video, encoder->videoFrame);
594	gotData = avcodec_receive_packet(encoder->video, &packet) == 0;
595#else
596	avcodec_encode_video2(encoder->video, &packet, encoder->videoFrame, &gotData);
597#endif
598	packet.pts = encoder->videoFrame->pts;
599	if (gotData) {
600#ifndef FFMPEG_USE_PACKET_UNREF
601		if (encoder->video->coded_frame->key_frame) {
602			packet.flags |= AV_PKT_FLAG_KEY;
603		}
604#endif
605		packet.stream_index = encoder->videoStream->index;
606		av_interleaved_write_frame(encoder->context, &packet);
607	}
608#ifdef FFMPEG_USE_PACKET_UNREF
609	av_packet_unref(&packet);
610#else
611	av_free_packet(&packet);
612#endif
613}
614
615static void _ffmpegSetVideoDimensions(struct mAVStream* stream, unsigned width, unsigned height) {
616	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
617	if (!encoder->context || !encoder->videoCodec) {
618		return;
619	}
620	encoder->iwidth = width;
621	encoder->iheight = height;
622	if (encoder->scaleContext) {
623		sws_freeContext(encoder->scaleContext);
624	}
625	encoder->scaleContext = sws_getContext(encoder->iwidth, encoder->iheight,
626#ifdef COLOR_16_BIT
627#ifdef COLOR_5_6_5
628	    AV_PIX_FMT_RGB565,
629#else
630	    AV_PIX_FMT_BGR555,
631#endif
632#else
633#ifndef USE_LIBAV
634	    AV_PIX_FMT_0BGR32,
635#else
636	    AV_PIX_FMT_BGR32,
637#endif
638#endif
639	    encoder->videoFrame->width, encoder->videoFrame->height, encoder->video->pix_fmt,
640	    SWS_POINT, 0, 0, 0);
641}