all repos — mgba @ 1cc4839d4f28c72c15248e7e3e7915b7bf130ede

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