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