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 if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
188 encoder->audio->flags |= CODEC_FLAG_GLOBAL_HEADER;
189 }
190 avcodec_open2(encoder->audio, acodec, &opts);
191 av_dict_free(&opts);
192 encoder->audioFrame = av_frame_alloc();
193 encoder->audioFrame->nb_samples = encoder->audio->frame_size;
194 encoder->audioFrame->format = encoder->audio->sample_fmt;
195 encoder->audioFrame->pts = 0;
196 encoder->resampleContext = avresample_alloc_context();
197 av_opt_set_int(encoder->resampleContext, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
198 av_opt_set_int(encoder->resampleContext, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
199 av_opt_set_int(encoder->resampleContext, "in_sample_rate", PREFERRED_SAMPLE_RATE, 0);
200 av_opt_set_int(encoder->resampleContext, "out_sample_rate", encoder->sampleRate, 0);
201 av_opt_set_int(encoder->resampleContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
202 av_opt_set_int(encoder->resampleContext, "out_sample_fmt", encoder->sampleFormat, 0);
203 avresample_open(encoder->resampleContext);
204 encoder->audioBufferSize = (encoder->audioFrame->nb_samples * PREFERRED_SAMPLE_RATE / encoder->sampleRate) * 4;
205 encoder->audioBuffer = av_malloc(encoder->audioBufferSize);
206 encoder->postaudioBufferSize = av_samples_get_buffer_size(0, encoder->audio->channels, encoder->audio->frame_size, encoder->audio->sample_fmt, 0);
207 encoder->postaudioBuffer = av_malloc(encoder->postaudioBufferSize);
208 avcodec_fill_audio_frame(encoder->audioFrame, encoder->audio->channels, encoder->audio->sample_fmt, (const uint8_t*) encoder->postaudioBuffer, encoder->postaudioBufferSize, 0);
209
210 if (encoder->audio->codec->id == AV_CODEC_ID_AAC &&
211 (strcasecmp(encoder->containerFormat, "mp4") ||
212 strcasecmp(encoder->containerFormat, "m4v") ||
213 strcasecmp(encoder->containerFormat, "mov"))) {
214 // MP4 container doesn't support the raw ADTS AAC format that the encoder spits out
215 encoder->absf = av_bitstream_filter_init("aac_adtstoasc");
216 }
217
218 encoder->videoStream = avformat_new_stream(encoder->context, vcodec);
219 encoder->video = encoder->videoStream->codec;
220 encoder->video->bit_rate = encoder->videoBitrate;
221 encoder->video->width = VIDEO_HORIZONTAL_PIXELS;
222 encoder->video->height = VIDEO_VERTICAL_PIXELS;
223 encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH, GBA_ARM7TDMI_FREQUENCY };
224 encoder->video->pix_fmt = encoder->pixFormat;
225 encoder->video->gop_size = 15;
226 encoder->video->max_b_frames = 0;
227 if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
228 encoder->video->flags |= CODEC_FLAG_GLOBAL_HEADER;
229 }
230 avcodec_open2(encoder->video, vcodec, 0);
231 encoder->videoFrame = av_frame_alloc();
232 encoder->videoFrame->format = encoder->video->pix_fmt;
233 encoder->videoFrame->width = encoder->video->width;
234 encoder->videoFrame->height = encoder->video->height;
235 encoder->videoFrame->pts = 0;
236 encoder->scaleContext = sws_getContext(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, AV_PIX_FMT_0BGR32,
237 VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, encoder->video->pix_fmt,
238 0, 0, 0, 0);
239 av_image_alloc(encoder->videoFrame->data, encoder->videoFrame->linesize, encoder->video->width, encoder->video->height, encoder->video->pix_fmt, 32);
240
241 avio_open(&encoder->context->pb, outfile, AVIO_FLAG_WRITE);
242 avformat_write_header(encoder->context, 0);
243
244 return true;
245}
246
247void FFmpegEncoderClose(struct FFmpegEncoder* encoder) {
248 if (!encoder->context) {
249 return;
250 }
251 av_write_trailer(encoder->context);
252 avio_close(encoder->context->pb);
253
254 av_free(encoder->postaudioBuffer);
255 if (encoder->audioBuffer) {
256 av_free(encoder->audioBuffer);
257 }
258 av_frame_free(&encoder->audioFrame);
259 avcodec_close(encoder->audio);
260
261 av_frame_free(&encoder->videoFrame);
262 avcodec_close(encoder->video);
263
264 if (encoder->resampleContext) {
265 avresample_close(encoder->resampleContext);
266 }
267
268 if (encoder->absf) {
269 av_bitstream_filter_close(encoder->absf);
270 encoder->absf = 0;
271 }
272
273 sws_freeContext(encoder->scaleContext);
274
275 avformat_free_context(encoder->context);
276 encoder->context = 0;
277}
278
279void _ffmpegPostAudioFrame(struct GBAAVStream* stream, int32_t left, int32_t right) {
280 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
281 if (!encoder->context) {
282 return;
283 }
284
285 av_frame_make_writable(encoder->audioFrame);
286 encoder->audioBuffer[encoder->currentAudioSample * 2] = left;
287 encoder->audioBuffer[encoder->currentAudioSample * 2 + 1] = right;
288
289 ++encoder->currentAudioFrame;
290 ++encoder->currentAudioSample;
291
292 if ((encoder->currentAudioSample * 4) < encoder->audioBufferSize) {
293 return;
294 }
295 encoder->currentAudioSample = 0;
296
297 int channelSize = 2 * av_get_bytes_per_sample(encoder->audio->sample_fmt);
298 avresample_convert(encoder->resampleContext,
299 0, 0, encoder->postaudioBufferSize / channelSize,
300 (uint8_t**) &encoder->audioBuffer, 0, encoder->audioBufferSize / 4);
301 if ((ssize_t) avresample_available(encoder->resampleContext) < (ssize_t) encoder->postaudioBufferSize / channelSize) {
302 return;
303 }
304 avresample_read(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize);
305
306 AVRational timeBase = { 1, PREFERRED_SAMPLE_RATE };
307 encoder->audioFrame->pts = encoder->nextAudioPts;
308 encoder->nextAudioPts = av_rescale_q(encoder->currentAudioFrame, timeBase, encoder->audioStream->time_base);
309
310 AVPacket packet;
311 av_init_packet(&packet);
312 packet.data = 0;
313 packet.size = 0;
314 int gotData;
315 avcodec_encode_audio2(encoder->audio, &packet, encoder->audioFrame, &gotData);
316 if (gotData) {
317 if (encoder->absf) {
318 AVPacket tempPacket = packet;
319 int success = av_bitstream_filter_filter(encoder->absf, encoder->audio, 0,
320 &tempPacket.data, &tempPacket.size,
321 packet.data, packet.size, 0);
322 if (success > 0) {
323 tempPacket.buf = av_buffer_create(tempPacket.data, tempPacket.size, av_buffer_default_free, 0, 0);
324 av_free_packet(&packet);
325 }
326 packet = tempPacket;
327 }
328 packet.stream_index = encoder->audioStream->index;
329 av_interleaved_write_frame(encoder->context, &packet);
330 }
331 av_free_packet(&packet);
332}
333
334void _ffmpegPostVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
335 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
336 if (!encoder->context) {
337 return;
338 }
339 uint8_t* pixels;
340 unsigned stride;
341 renderer->getPixels(renderer, &stride, (void**) &pixels);
342 stride *= 4;
343
344 AVPacket packet;
345
346 av_init_packet(&packet);
347 packet.data = 0;
348 packet.size = 0;
349 av_frame_make_writable(encoder->videoFrame);
350 encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
351 ++encoder->currentVideoFrame;
352
353 sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, VIDEO_VERTICAL_PIXELS, encoder->videoFrame->data, encoder->videoFrame->linesize);
354
355 int gotData;
356 avcodec_encode_video2(encoder->video, &packet, encoder->videoFrame, &gotData);
357 if (gotData) {
358 if (encoder->videoStream->codec->coded_frame->key_frame) {
359 packet.flags |= AV_PKT_FLAG_KEY;
360 }
361 packet.stream_index = encoder->videoStream->index;
362 av_interleaved_write_frame(encoder->context, &packet);
363 }
364 av_free_packet(&packet);
365}