all repos — mgba @ 6dd18fd86e8b37d068ef8b3ba36739fb1597f985

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