all repos — mgba @ 751acd16371f670348b121bf5550deb7b810110f

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
209	AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
210#ifndef USE_LIBAV
211	avformat_alloc_output_context2(&encoder->context, oformat, 0, outfile);
212#else
213	encoder->context = avformat_alloc_context();
214	strncpy(encoder->context->filename, outfile, sizeof(encoder->context->filename) - 1);
215	encoder->context->filename[sizeof(encoder->context->filename) - 1] = '\0';
216	encoder->context->oformat = oformat;
217#endif
218
219	if (acodec) {
220#ifdef FFMPEG_USE_CODECPAR
221		encoder->audioStream = avformat_new_stream(encoder->context, NULL);
222		encoder->audio = avcodec_alloc_context3(acodec);
223#else
224		encoder->audioStream = avformat_new_stream(encoder->context, acodec);
225		encoder->audio = encoder->audioStream->codec;
226#endif
227		encoder->audio->bit_rate = encoder->audioBitrate;
228		encoder->audio->channels = 2;
229		encoder->audio->channel_layout = AV_CH_LAYOUT_STEREO;
230		encoder->audio->sample_rate = encoder->sampleRate;
231		encoder->audio->sample_fmt = encoder->sampleFormat;
232		AVDictionary* opts = 0;
233		av_dict_set(&opts, "strict", "-2", 0);
234		if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
235#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
236			encoder->audio->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
237#else
238			encoder->audio->flags |= CODEC_FLAG_GLOBAL_HEADER;
239#endif
240		}
241		avcodec_open2(encoder->audio, acodec, &opts);
242		av_dict_free(&opts);
243#if LIBAVCODEC_VERSION_MAJOR >= 55
244		encoder->audioFrame = av_frame_alloc();
245#else
246		encoder->audioFrame = avcodec_alloc_frame();
247#endif
248		if (!encoder->audio->frame_size) {
249			encoder->audio->frame_size = 1;
250		}
251		encoder->audioFrame->nb_samples = encoder->audio->frame_size;
252		encoder->audioFrame->format = encoder->audio->sample_fmt;
253		encoder->audioFrame->pts = 0;
254#ifdef USE_LIBAVRESAMPLE
255		encoder->resampleContext = avresample_alloc_context();
256		av_opt_set_int(encoder->resampleContext, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
257		av_opt_set_int(encoder->resampleContext, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
258		av_opt_set_int(encoder->resampleContext, "in_sample_rate", PREFERRED_SAMPLE_RATE, 0);
259		av_opt_set_int(encoder->resampleContext, "out_sample_rate", encoder->sampleRate, 0);
260		av_opt_set_int(encoder->resampleContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
261		av_opt_set_int(encoder->resampleContext, "out_sample_fmt", encoder->sampleFormat, 0);
262		avresample_open(encoder->resampleContext);
263#else
264		encoder->resampleContext = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, encoder->sampleFormat, encoder->sampleRate,
265		                                              AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, PREFERRED_SAMPLE_RATE, 0, NULL);
266		swr_init(encoder->resampleContext);
267#endif
268		encoder->audioBufferSize = (encoder->audioFrame->nb_samples * PREFERRED_SAMPLE_RATE / encoder->sampleRate) * 4;
269		encoder->audioBuffer = av_malloc(encoder->audioBufferSize);
270		encoder->postaudioBufferSize = av_samples_get_buffer_size(0, encoder->audio->channels, encoder->audio->frame_size, encoder->audio->sample_fmt, 0);
271		encoder->postaudioBuffer = av_malloc(encoder->postaudioBufferSize);
272		avcodec_fill_audio_frame(encoder->audioFrame, encoder->audio->channels, encoder->audio->sample_fmt, (const uint8_t*) encoder->postaudioBuffer, encoder->postaudioBufferSize, 0);
273
274		if (encoder->audio->codec->id == AV_CODEC_ID_AAC &&
275		    (strcasecmp(encoder->containerFormat, "mp4") ||
276		        strcasecmp(encoder->containerFormat, "m4v") ||
277		        strcasecmp(encoder->containerFormat, "mov"))) {
278			// MP4 container doesn't support the raw ADTS AAC format that the encoder spits out
279#ifdef FFMPEG_USE_NEW_BSF
280			av_bsf_alloc(av_bsf_get_by_name("aac_adtstoasc"), &encoder->absf);
281			avcodec_parameters_from_context(encoder->absf->par_in, encoder->audio);
282			av_bsf_init(encoder->absf);
283#else
284			encoder->absf = av_bitstream_filter_init("aac_adtstoasc");
285#endif
286		}
287#ifdef FFMPEG_USE_CODECPAR
288		avcodec_parameters_from_context(encoder->audioStream->codecpar, encoder->audio);
289#endif
290	}
291
292#ifdef FFMPEG_USE_CODECPAR
293	encoder->videoStream = avformat_new_stream(encoder->context, NULL);
294	encoder->video = avcodec_alloc_context3(vcodec);
295#else
296	encoder->videoStream = avformat_new_stream(encoder->context, vcodec);
297	encoder->video = encoder->videoStream->codec;
298#endif
299	encoder->video->bit_rate = encoder->videoBitrate;
300	encoder->video->width = encoder->width;
301	encoder->video->height = encoder->height;
302	encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH, GBA_ARM7TDMI_FREQUENCY };
303	encoder->video->framerate = (AVRational) { GBA_ARM7TDMI_FREQUENCY, VIDEO_TOTAL_LENGTH };
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 = encoder->currentAudioFrame;
461	encoder->currentAudioFrame += samples;
462
463	AVPacket packet;
464	av_init_packet(&packet);
465	packet.data = 0;
466	packet.size = 0;
467
468	int gotData;
469#ifdef FFMPEG_USE_PACKETS
470	avcodec_send_frame(encoder->audio, encoder->audioFrame);
471	gotData = avcodec_receive_packet(encoder->audio, &packet);
472	gotData = (gotData == 0) && packet.size;
473#else
474	avcodec_encode_audio2(encoder->audio, &packet, encoder->audioFrame, &gotData);
475#endif
476	packet.pts = av_rescale_q(encoder->audioFrame->pts, encoder->audio->time_base, encoder->audioStream->time_base);
477	packet.dts = packet.pts;
478
479	if (gotData) {
480		if (encoder->absf) {
481			AVPacket tempPacket;
482
483#ifdef FFMPEG_USE_NEW_BSF
484			int success = av_bsf_send_packet(encoder->absf, &packet);
485			if (success >= 0) {
486				success = av_bsf_receive_packet(encoder->absf, &tempPacket);
487			}
488#else
489			int success = av_bitstream_filter_filter(encoder->absf, encoder->audio, 0,
490			    &tempPacket.data, &tempPacket.size,
491			    packet.data, packet.size, 0);
492#endif
493
494			if (success >= 0) {
495#if LIBAVUTIL_VERSION_MAJOR >= 53
496				tempPacket.buf = av_buffer_create(tempPacket.data, tempPacket.size, av_buffer_default_free, 0, 0);
497#endif
498
499#ifdef FFMPEG_USE_PACKET_UNREF
500				av_packet_move_ref(&packet, &tempPacket);
501#else
502				av_free_packet(&packet);
503				packet = tempPacket;
504#endif
505
506				packet.stream_index = encoder->audioStream->index;
507				av_interleaved_write_frame(encoder->context, &packet);
508			}
509		} else {
510			packet.stream_index = encoder->audioStream->index;
511			av_interleaved_write_frame(encoder->context, &packet);
512		}
513	}
514#ifdef FFMPEG_USE_PACKET_UNREF
515	av_packet_unref(&packet);
516#else
517	av_free_packet(&packet);
518#endif
519}
520
521void _ffmpegPostVideoFrame(struct mAVStream* stream, const color_t* pixels, size_t stride) {
522	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
523	if (!encoder->context) {
524		return;
525	}
526	stride *= BYTES_PER_PIXEL;
527
528	AVPacket packet;
529
530	av_init_packet(&packet);
531	packet.data = 0;
532	packet.size = 0;
533#if LIBAVCODEC_VERSION_MAJOR >= 55
534	av_frame_make_writable(encoder->videoFrame);
535#endif
536	encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
537	++encoder->currentVideoFrame;
538
539	sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, encoder->iheight, encoder->videoFrame->data, encoder->videoFrame->linesize);
540
541	int gotData;
542#ifdef FFMPEG_USE_PACKETS
543	avcodec_send_frame(encoder->video, encoder->videoFrame);
544	gotData = avcodec_receive_packet(encoder->video, &packet) == 0;
545#else
546	avcodec_encode_video2(encoder->video, &packet, encoder->videoFrame, &gotData);
547#endif
548	packet.pts = encoder->videoFrame->pts;
549	if (gotData) {
550#ifndef FFMPEG_USE_PACKET_UNREF
551		if (encoder->video->coded_frame->key_frame) {
552			packet.flags |= AV_PKT_FLAG_KEY;
553		}
554#endif
555		packet.stream_index = encoder->videoStream->index;
556		av_interleaved_write_frame(encoder->context, &packet);
557	}
558#ifdef FFMPEG_USE_PACKET_UNREF
559	av_packet_unref(&packet);
560#else
561	av_free_packet(&packet);
562#endif
563}
564
565static void _ffmpegSetVideoDimensions(struct mAVStream* stream, unsigned width, unsigned height) {
566	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
567	encoder->iwidth = width;
568	encoder->iheight = height;
569	if (encoder->scaleContext) {
570		sws_freeContext(encoder->scaleContext);
571	}
572	encoder->scaleContext = sws_getContext(encoder->iwidth, encoder->iheight,
573#ifdef COLOR_16_BIT
574#ifdef COLOR_5_6_5
575	    AV_PIX_FMT_RGB565,
576#else
577	    AV_PIX_FMT_BGR555,
578#endif
579#else
580#ifndef USE_LIBAV
581	    AV_PIX_FMT_0BGR32,
582#else
583	    AV_PIX_FMT_BGR32,
584#endif
585#endif
586	    encoder->videoFrame->width, encoder->videoFrame->height, encoder->video->pix_fmt,
587	    SWS_POINT, 0, 0, 0);
588}