all repos — mgba @ 5b5c8c8d2d7b0bb76a770780aced880e212a7993

mGBA Game Boy Advance Emulator

src/platform/ffmpeg/ffmpeg-encoder.c (view raw)

  1#include "ffmpeg-encoder.h"
  2
  3#include "gba-video.h"
  4
  5#include <libavcodec/avcodec.h>
  6
  7#include <libavutil/imgutils.h>
  8#include <libavutil/opt.h>
  9
 10#include <libavresample/avresample.h>
 11#include <libswscale/swscale.h>
 12
 13static void _ffmpegPostVideoFrame(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
 14static void _ffmpegPostAudioFrame(struct GBAAVStream*, int32_t left, int32_t right);
 15
 16enum {
 17	PREFERRED_SAMPLE_RATE = 0x8000
 18};
 19
 20void FFmpegEncoderInit(struct FFmpegEncoder* encoder) {
 21	av_register_all();
 22
 23	encoder->d.postVideoFrame = _ffmpegPostVideoFrame;
 24	encoder->d.postAudioFrame = _ffmpegPostAudioFrame;
 25
 26	encoder->audioCodec = 0;
 27	encoder->videoCodec = 0;
 28	encoder->containerFormat = 0;
 29	FFmpegEncoderSetAudio(encoder, "flac", 0);
 30	FFmpegEncoderSetVideo(encoder, "png", 0);
 31	FFmpegEncoderSetContainer(encoder, "matroska");
 32	FFmpegEncoderSetDimensions(encoder, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
 33	encoder->resampleContext = 0;
 34	encoder->absf = 0;
 35	encoder->context = 0;
 36}
 37
 38bool FFmpegEncoderSetAudio(struct FFmpegEncoder* encoder, const char* acodec, unsigned abr) {
 39	static const struct {
 40		int format;
 41		int priority;
 42	} priorities[] = {
 43		{ AV_SAMPLE_FMT_S16, 0 },
 44		{ AV_SAMPLE_FMT_S16P, 1 },
 45		{ AV_SAMPLE_FMT_S32, 2 },
 46		{ AV_SAMPLE_FMT_S32P, 2 },
 47		{ AV_SAMPLE_FMT_FLT, 3 },
 48		{ AV_SAMPLE_FMT_FLTP, 3 },
 49		{ AV_SAMPLE_FMT_DBL, 4 },
 50		{ AV_SAMPLE_FMT_DBLP, 4 }
 51	};
 52
 53	if (!acodec) {
 54		encoder->audioCodec = 0;
 55		return true;
 56	}
 57
 58	AVCodec* codec = avcodec_find_encoder_by_name(acodec);
 59	if (!codec) {
 60		return false;
 61	}
 62
 63	if (!codec->sample_fmts) {
 64		return false;
 65	}
 66	size_t i;
 67	size_t j;
 68	int priority = INT_MAX;
 69	encoder->sampleFormat = AV_SAMPLE_FMT_NONE;
 70	for (i = 0; codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; ++i) {
 71		for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
 72			if (codec->sample_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
 73				priority = priorities[j].priority;
 74				encoder->sampleFormat = codec->sample_fmts[i];
 75			}
 76		}
 77	}
 78	if (encoder->sampleFormat == AV_SAMPLE_FMT_NONE) {
 79		return false;
 80	}
 81	encoder->sampleRate = PREFERRED_SAMPLE_RATE;
 82	if (codec->supported_samplerates) {
 83		for (i = 0; codec->supported_samplerates[i]; ++i) {
 84			if (codec->supported_samplerates[i] < PREFERRED_SAMPLE_RATE) {
 85				continue;
 86			}
 87			if (encoder->sampleRate == PREFERRED_SAMPLE_RATE || encoder->sampleRate > codec->supported_samplerates[i]) {
 88				encoder->sampleRate = codec->supported_samplerates[i];
 89			}
 90		}
 91	} else if (codec->id == AV_CODEC_ID_AAC) {
 92		// HACK: AAC doesn't support 32768Hz (it rounds to 32000), but libfaac doesn't tell us that
 93		encoder->sampleRate = 44100;
 94	}
 95	encoder->audioCodec = acodec;
 96	encoder->audioBitrate = abr;
 97	return true;
 98}
 99
100bool FFmpegEncoderSetVideo(struct FFmpegEncoder* encoder, const char* vcodec, unsigned vbr) {
101	static const struct {
102		enum AVPixelFormat format;
103		int priority;
104	} priorities[] = {
105		{ AV_PIX_FMT_RGB555, 0 },
106		{ AV_PIX_FMT_BGR555, 0 },
107		{ AV_PIX_FMT_RGB565, 1 },
108		{ AV_PIX_FMT_BGR565, 1 },
109		{ AV_PIX_FMT_RGB24, 2 },
110		{ AV_PIX_FMT_BGR24, 2 },
111		{ AV_PIX_FMT_BGR0, 3 },
112		{ AV_PIX_FMT_RGB0, 3 },
113		{ AV_PIX_FMT_0BGR, 3 },
114		{ AV_PIX_FMT_0RGB, 3 },
115		{ AV_PIX_FMT_RGB8, 3 },
116		{ AV_PIX_FMT_BGR8, 3 },
117		{ AV_PIX_FMT_YUV422P, 4 },
118		{ AV_PIX_FMT_YUV444P, 5 },
119		{ AV_PIX_FMT_YUV420P, 6 }
120	};
121	AVCodec* codec = avcodec_find_encoder_by_name(vcodec);
122	if (!codec) {
123		return false;
124	}
125
126	size_t i;
127	size_t j;
128	int priority = INT_MAX;
129	encoder->pixFormat = AV_PIX_FMT_NONE;
130	for (i = 0; codec->pix_fmts[i] != AV_PIX_FMT_NONE; ++i) {
131		for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
132			if (codec->pix_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
133				priority = priorities[j].priority;
134				encoder->pixFormat = codec->pix_fmts[i];
135			}
136		}
137	}
138	if (encoder->pixFormat == AV_PIX_FMT_NONE) {
139		return false;
140	}
141	encoder->videoCodec = vcodec;
142	encoder->videoBitrate = vbr;
143	return true;
144}
145
146bool FFmpegEncoderSetContainer(struct FFmpegEncoder* encoder, const char* container) {
147	AVOutputFormat* oformat = av_guess_format(container, 0, 0);
148	if (!oformat) {
149		return false;
150	}
151	encoder->containerFormat = container;
152	return true;
153}
154
155void FFmpegEncoderSetDimensions(struct FFmpegEncoder* encoder, int width, int height) {
156	encoder->width = width > 0 ? width : VIDEO_HORIZONTAL_PIXELS;
157	encoder->height = height > 0 ? height : VIDEO_VERTICAL_PIXELS;
158}
159
160bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder* encoder) {
161	AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
162	AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
163	AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
164	if ((encoder->audioCodec && !acodec) || !vcodec || !oformat) {
165		return false;
166	}
167	if (encoder->audioCodec && !avformat_query_codec(oformat, acodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
168		return false;
169	}
170	if (!avformat_query_codec(oformat, vcodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
171		return false;
172	}
173	return true;
174}
175
176bool FFmpegEncoderOpen(struct FFmpegEncoder* encoder, const char* outfile) {
177	AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
178	AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
179	if ((encoder->audioCodec && !acodec) || !vcodec || !FFmpegEncoderVerifyContainer(encoder)) {
180		return false;
181	}
182
183	encoder->currentAudioSample = 0;
184	encoder->currentAudioFrame = 0;
185	encoder->currentVideoFrame = 0;
186	encoder->nextAudioPts = 0;
187
188	avformat_alloc_output_context2(&encoder->context, 0, 0, outfile);
189
190	encoder->context->oformat = av_guess_format(encoder->containerFormat, 0, 0);
191
192	if (acodec) {
193		encoder->audioStream = avformat_new_stream(encoder->context, acodec);
194		encoder->audio = encoder->audioStream->codec;
195		encoder->audio->bit_rate = encoder->audioBitrate;
196		encoder->audio->channels = 2;
197		encoder->audio->channel_layout = AV_CH_LAYOUT_STEREO;
198		encoder->audio->sample_rate = encoder->sampleRate;
199		encoder->audio->sample_fmt = encoder->sampleFormat;
200		AVDictionary* opts = 0;
201		av_dict_set(&opts, "strict", "-2", 0);
202		if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
203			encoder->audio->flags |= CODEC_FLAG_GLOBAL_HEADER;
204		}
205		avcodec_open2(encoder->audio, acodec, &opts);
206		av_dict_free(&opts);
207		encoder->audioFrame = av_frame_alloc();
208		encoder->audioFrame->nb_samples = encoder->audio->frame_size;
209		encoder->audioFrame->format = encoder->audio->sample_fmt;
210		encoder->audioFrame->pts = 0;
211		encoder->resampleContext = avresample_alloc_context();
212		av_opt_set_int(encoder->resampleContext, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
213		av_opt_set_int(encoder->resampleContext, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
214		av_opt_set_int(encoder->resampleContext, "in_sample_rate", PREFERRED_SAMPLE_RATE, 0);
215		av_opt_set_int(encoder->resampleContext, "out_sample_rate", encoder->sampleRate, 0);
216		av_opt_set_int(encoder->resampleContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
217		av_opt_set_int(encoder->resampleContext, "out_sample_fmt", encoder->sampleFormat, 0);
218		avresample_open(encoder->resampleContext);
219		encoder->audioBufferSize = (encoder->audioFrame->nb_samples * PREFERRED_SAMPLE_RATE / encoder->sampleRate) * 4;
220		encoder->audioBuffer = av_malloc(encoder->audioBufferSize);
221		encoder->postaudioBufferSize = av_samples_get_buffer_size(0, encoder->audio->channels, encoder->audio->frame_size, encoder->audio->sample_fmt, 0);
222		encoder->postaudioBuffer = av_malloc(encoder->postaudioBufferSize);
223		avcodec_fill_audio_frame(encoder->audioFrame, encoder->audio->channels, encoder->audio->sample_fmt, (const uint8_t*) encoder->postaudioBuffer, encoder->postaudioBufferSize, 0);
224
225		if (encoder->audio->codec->id == AV_CODEC_ID_AAC &&
226			(strcasecmp(encoder->containerFormat, "mp4") ||
227			strcasecmp(encoder->containerFormat, "m4v") ||
228			strcasecmp(encoder->containerFormat, "mov"))) {
229			// MP4 container doesn't support the raw ADTS AAC format that the encoder spits out
230			encoder->absf = av_bitstream_filter_init("aac_adtstoasc");
231		}
232	}
233
234	encoder->videoStream = avformat_new_stream(encoder->context, vcodec);
235	encoder->video = encoder->videoStream->codec;
236	encoder->video->bit_rate = encoder->videoBitrate;
237	encoder->video->width = encoder->width;
238	encoder->video->height = encoder->height;
239	encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH, GBA_ARM7TDMI_FREQUENCY };
240	encoder->video->pix_fmt = encoder->pixFormat;
241	encoder->video->gop_size = 60;
242	encoder->video->max_b_frames = 3;
243	if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
244		encoder->video->flags |= CODEC_FLAG_GLOBAL_HEADER;
245	}
246	if (strcmp(vcodec->name, "libx264") == 0) {
247		// Try to adaptively figure out when you can use a slower encoder
248		if (encoder->width * encoder->height > 1000000) {
249			av_opt_set(encoder->video->priv_data, "preset", "superfast", 0);
250		} else if (encoder->width * encoder->height > 500000) {
251			av_opt_set(encoder->video->priv_data, "preset", "veryfast", 0);
252		} else {
253			av_opt_set(encoder->video->priv_data, "preset", "faster", 0);
254		}
255		av_opt_set(encoder->video->priv_data, "tune", "zerolatency", 0);
256	}
257	avcodec_open2(encoder->video, vcodec, 0);
258	encoder->videoFrame = av_frame_alloc();
259	encoder->videoFrame->format = encoder->video->pix_fmt;
260	encoder->videoFrame->width = encoder->video->width;
261	encoder->videoFrame->height = encoder->video->height;
262	encoder->videoFrame->pts = 0;
263	encoder->scaleContext = sws_getContext(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, AV_PIX_FMT_0BGR32,
264		encoder->videoFrame->width, encoder->videoFrame->height, encoder->video->pix_fmt,
265		SWS_POINT, 0, 0, 0);
266	av_image_alloc(encoder->videoFrame->data, encoder->videoFrame->linesize, encoder->video->width, encoder->video->height, encoder->video->pix_fmt, 32);
267
268	avio_open(&encoder->context->pb, outfile, AVIO_FLAG_WRITE);
269	avformat_write_header(encoder->context, 0);
270
271	return true;
272}
273
274void FFmpegEncoderClose(struct FFmpegEncoder* encoder) {
275	if (!encoder->context) {
276		return;
277	}
278	av_write_trailer(encoder->context);
279	avio_close(encoder->context->pb);
280
281	if (encoder->audioCodec) {
282		av_free(encoder->postaudioBuffer);
283		if (encoder->audioBuffer) {
284			av_free(encoder->audioBuffer);
285		}
286		av_frame_free(&encoder->audioFrame);
287		avcodec_close(encoder->audio);
288
289		if (encoder->resampleContext) {
290			avresample_close(encoder->resampleContext);
291		}
292
293		if (encoder->absf) {
294			av_bitstream_filter_close(encoder->absf);
295			encoder->absf = 0;
296		}
297	}
298
299	av_frame_free(&encoder->videoFrame);
300	avcodec_close(encoder->video);
301
302	sws_freeContext(encoder->scaleContext);
303
304	avformat_free_context(encoder->context);
305	encoder->context = 0;
306}
307
308bool FFmpegEncoderIsOpen(struct FFmpegEncoder* encoder) {
309	return !!encoder->context;
310}
311
312void _ffmpegPostAudioFrame(struct GBAAVStream* stream, int32_t left, int32_t right) {
313	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
314	if (!encoder->context || !encoder->audioCodec) {
315		return;
316	}
317
318	av_frame_make_writable(encoder->audioFrame);
319	encoder->audioBuffer[encoder->currentAudioSample * 2] = left;
320	encoder->audioBuffer[encoder->currentAudioSample * 2 + 1] = right;
321
322	++encoder->currentAudioFrame;
323	++encoder->currentAudioSample;
324
325	if ((encoder->currentAudioSample * 4) < encoder->audioBufferSize) {
326		return;
327	}
328	encoder->currentAudioSample = 0;
329
330	int channelSize = 2 * av_get_bytes_per_sample(encoder->audio->sample_fmt);
331	avresample_convert(encoder->resampleContext,
332		0, 0, encoder->postaudioBufferSize / channelSize,
333		(uint8_t**) &encoder->audioBuffer, 0, encoder->audioBufferSize / 4);
334	if ((ssize_t) avresample_available(encoder->resampleContext) < (ssize_t) encoder->postaudioBufferSize / channelSize) {
335		return;
336	}
337	avresample_read(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize);
338
339	AVRational timeBase = { 1, PREFERRED_SAMPLE_RATE };
340	encoder->audioFrame->pts = encoder->nextAudioPts;
341	encoder->nextAudioPts = av_rescale_q(encoder->currentAudioFrame, timeBase, encoder->audioStream->time_base);
342
343	AVPacket packet;
344	av_init_packet(&packet);
345	packet.data = 0;
346	packet.size = 0;
347	int gotData;
348	avcodec_encode_audio2(encoder->audio, &packet, encoder->audioFrame, &gotData);
349	if (gotData) {
350		if (encoder->absf) {
351			AVPacket tempPacket = packet;
352			int success = av_bitstream_filter_filter(encoder->absf, encoder->audio, 0,
353				&tempPacket.data, &tempPacket.size,
354				packet.data, packet.size, 0);
355			if (success > 0) {
356				tempPacket.buf = av_buffer_create(tempPacket.data, tempPacket.size, av_buffer_default_free, 0, 0);
357				av_free_packet(&packet);
358			}
359			packet = tempPacket;
360		}
361		packet.stream_index = encoder->audioStream->index;
362		av_interleaved_write_frame(encoder->context, &packet);
363	}
364	av_free_packet(&packet);
365}
366
367void _ffmpegPostVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
368	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
369	if (!encoder->context) {
370		return;
371	}
372	uint8_t* pixels;
373	unsigned stride;
374	renderer->getPixels(renderer, &stride, (void**) &pixels);
375	stride *= 4;
376
377	AVPacket packet;
378
379	av_init_packet(&packet);
380	packet.data = 0;
381	packet.size = 0;
382	av_frame_make_writable(encoder->videoFrame);
383	encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
384	++encoder->currentVideoFrame;
385
386	sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, VIDEO_VERTICAL_PIXELS, encoder->videoFrame->data, encoder->videoFrame->linesize);
387
388	int gotData;
389	avcodec_encode_video2(encoder->video, &packet, encoder->videoFrame, &gotData);
390	if (gotData) {
391		if (encoder->videoStream->codec->coded_frame->key_frame) {
392			packet.flags |= AV_PKT_FLAG_KEY;
393		}
394		packet.stream_index = encoder->videoStream->index;
395		av_interleaved_write_frame(encoder->context, &packet);
396	}
397	av_free_packet(&packet);
398}