all repos — mgba @ 9c83462343100901461db1b1caae2fa434c4555f

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