all repos — mgba @ d6e5bfb6409daf967c2f9d1a8443f294c055f607

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