all repos — mgba @ 61569c0559f5f1b62638239181bcd33c138c58b2

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
  7static void _ffmpegPostVideoFrame(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
  8static void _ffmpegPostAudioFrame(struct GBAAVStream*, int32_t left, int32_t right);
  9
 10void FFmpegEncoderInit(struct FFmpegEncoder* encoder) {
 11	av_register_all();
 12
 13	encoder->d.postVideoFrame = _ffmpegPostVideoFrame;
 14	encoder->d.postAudioFrame = _ffmpegPostAudioFrame;
 15
 16	encoder->audioCodec = 0;
 17	encoder->videoCodec = 0;
 18	encoder->containerFormat = 0;
 19	FFmpegEncoderSetAudio(encoder, "flac", 0);
 20	FFmpegEncoderSetVideo(encoder, "png", 0);
 21	FFmpegEncoderSetContainer(encoder, "matroska");
 22	encoder->currentAudioSample = 0;
 23	encoder->currentAudioFrame = 0;
 24	encoder->currentVideoFrame = 0;
 25	encoder->context = 0;
 26}
 27
 28bool FFmpegEncoderSetAudio(struct FFmpegEncoder* encoder, const char* acodec, unsigned abr) {
 29	if (!avcodec_find_encoder_by_name(acodec)) {
 30		return false;
 31	}
 32	encoder->audioCodec = acodec;
 33	encoder->audioBitrate = abr;
 34	return true;
 35}
 36
 37bool FFmpegEncoderSetVideo(struct FFmpegEncoder* encoder, const char* vcodec, unsigned vbr) {
 38	AVCodec* codec = avcodec_find_encoder_by_name(vcodec);
 39	if (!codec) {
 40		return false;
 41	}
 42
 43	size_t i;
 44	encoder->pixFormat = AV_PIX_FMT_NONE;
 45	for (i = 0; codec->pix_fmts[i] != AV_PIX_FMT_NONE; ++i) {
 46		if (codec->pix_fmts[i] == AV_PIX_FMT_RGB24) {
 47			encoder->pixFormat = AV_PIX_FMT_RGB24;
 48			break;
 49		}
 50		if (codec->pix_fmts[i] == AV_PIX_FMT_BGR0) {
 51			encoder->pixFormat = AV_PIX_FMT_BGR0;
 52		}
 53	}
 54	if (encoder->pixFormat == AV_PIX_FMT_NONE) {
 55		return false;
 56	}
 57	encoder->videoCodec = vcodec;
 58	encoder->videoBitrate = vbr;
 59	return true;
 60}
 61
 62bool FFmpegEncoderSetContainer(struct FFmpegEncoder* encoder, const char* container) {
 63	AVOutputFormat* oformat = av_guess_format(container, 0, 0);
 64	if (!oformat) {
 65		return false;
 66	}
 67	encoder->containerFormat = container;
 68	return true;
 69}
 70
 71bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder* encoder) {
 72	AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
 73	AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
 74	AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
 75	if (!acodec || !vcodec || !oformat) {
 76		return false;
 77	}
 78	if (!avformat_query_codec(oformat, acodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
 79		return false;
 80	}
 81	if (!avformat_query_codec(oformat, vcodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
 82		return false;
 83	}
 84	return true;
 85}
 86
 87bool FFmpegEncoderOpen(struct FFmpegEncoder* encoder, const char* outfile) {
 88	AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
 89	AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
 90	if (!acodec || !vcodec || !FFmpegEncoderVerifyContainer(encoder)) {
 91		return false;
 92	}
 93
 94	avformat_alloc_output_context2(&encoder->context, 0, 0, outfile);
 95
 96	encoder->context->oformat = av_guess_format(encoder->containerFormat, 0, 0);
 97
 98	encoder->audioStream = avformat_new_stream(encoder->context, acodec);
 99	encoder->audio = encoder->audioStream->codec;
100	encoder->audio->bit_rate = encoder->audioBitrate;
101	encoder->audio->sample_rate = 0x8000;
102	encoder->audio->channels = 2;
103	encoder->audio->channel_layout = AV_CH_LAYOUT_STEREO;
104	encoder->audio->sample_fmt = AV_SAMPLE_FMT_S16;
105	avcodec_open2(encoder->audio, acodec, 0);
106	encoder->audioFrame = av_frame_alloc();
107	encoder->audioFrame->nb_samples = encoder->audio->frame_size;
108	encoder->audioFrame->format = encoder->audio->sample_fmt;
109	encoder->audioFrame->pts = 0;
110	encoder->audioBufferSize = av_samples_get_buffer_size(0, encoder->audio->channels, encoder->audio->frame_size, encoder->audio->sample_fmt, 0);
111	encoder->audioBuffer = av_malloc(encoder->audioBufferSize);
112	avcodec_fill_audio_frame(encoder->audioFrame, encoder->audio->channels, encoder->audio->sample_fmt, (const uint8_t*) encoder->audioBuffer, encoder->audioBufferSize, 0);
113
114	encoder->videoStream = avformat_new_stream(encoder->context, vcodec);
115	encoder->video = encoder->videoStream->codec;
116	encoder->video->bit_rate = encoder->videoBitrate;
117	encoder->video->width = VIDEO_HORIZONTAL_PIXELS;
118	encoder->video->height = VIDEO_VERTICAL_PIXELS;
119	encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH, GBA_ARM7TDMI_FREQUENCY };
120	encoder->video->pix_fmt = encoder->pixFormat;
121	encoder->video->gop_size = 15;
122	encoder->video->max_b_frames = 0;
123	avcodec_open2(encoder->video, vcodec, 0);
124	encoder->videoFrame = av_frame_alloc();
125	encoder->videoFrame->format = encoder->video->pix_fmt;
126	encoder->videoFrame->width = encoder->video->width;
127	encoder->videoFrame->height = encoder->video->height;
128	encoder->videoFrame->pts = 0;
129	av_image_alloc(encoder->videoFrame->data, encoder->videoFrame->linesize, encoder->video->width, encoder->video->height, encoder->video->pix_fmt, 32);
130
131	if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
132		encoder->audio->flags |= CODEC_FLAG_GLOBAL_HEADER;
133		encoder->video->flags |= CODEC_FLAG_GLOBAL_HEADER;
134	}
135
136	avio_open(&encoder->context->pb, outfile, AVIO_FLAG_WRITE);
137	avformat_write_header(encoder->context, 0);
138
139	return true;
140}
141
142void FFmpegEncoderClose(struct FFmpegEncoder* encoder) {
143	if (!encoder->context) {
144		return;
145	}
146	av_write_trailer(encoder->context);
147	avio_close(encoder->context->pb);
148
149	av_free(encoder->audioBuffer);
150	av_frame_free(&encoder->audioFrame);
151	avcodec_close(encoder->audio);
152
153	av_frame_free(&encoder->videoFrame);
154	avcodec_close(encoder->video);
155	avformat_free_context(encoder->context);
156	encoder->context = 0;
157}
158
159void _ffmpegPostAudioFrame(struct GBAAVStream* stream, int32_t left, int32_t right) {
160	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
161	if (!encoder->context) {
162		return;
163	}
164
165	av_frame_make_writable(encoder->audioFrame);
166	encoder->audioBuffer[encoder->currentAudioSample * 2] = left;
167	encoder->audioBuffer[encoder->currentAudioSample * 2 + 1] = right;
168	encoder->audioFrame->pts = av_rescale_q(encoder->currentAudioFrame, encoder->audio->time_base, encoder->audioStream->time_base);
169	++encoder->currentAudioFrame;
170	++encoder->currentAudioSample;
171
172	if ((encoder->currentAudioSample * 4) < encoder->audioBufferSize) {
173		return;
174	}
175	encoder->currentAudioSample = 0;
176
177	AVPacket packet;
178	av_init_packet(&packet);
179	packet.data = 0;
180	packet.size = 0;
181	int gotData;
182	avcodec_encode_audio2(encoder->audio, &packet, encoder->audioFrame, &gotData);
183	if (gotData) {
184		packet.stream_index = encoder->audioStream->index;
185		av_interleaved_write_frame(encoder->context, &packet);
186	}
187	av_free_packet(&packet);
188}
189
190void _ffmpegPostVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
191	struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
192	if (!encoder->context) {
193		return;
194	}
195	uint32_t* pixels;
196	unsigned stride;
197	renderer->getPixels(renderer, &stride, (void**) &pixels);
198
199	AVPacket packet;
200
201	av_init_packet(&packet);
202	packet.data = 0;
203	packet.size = 0;
204	av_frame_make_writable(encoder->videoFrame);
205	encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
206	++encoder->currentVideoFrame;
207
208	unsigned x, y;
209	if (encoder->videoFrame->format == AV_PIX_FMT_BGR0) {
210		for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
211			for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
212				uint32_t pixel = pixels[stride * y + x];
213				encoder->videoFrame->data[0][y * encoder->videoFrame->linesize[0] + x * 4] = pixel >> 16;
214				encoder->videoFrame->data[0][y * encoder->videoFrame->linesize[0] + x * 4 + 1] = pixel >> 8;
215				encoder->videoFrame->data[0][y * encoder->videoFrame->linesize[0] + x * 4 + 2] = pixel;
216			}
217		}
218	} else if (encoder->videoFrame->format == AV_PIX_FMT_RGB24) {
219		for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
220			for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
221				uint32_t pixel = pixels[stride * y + x];
222				encoder->videoFrame->data[0][y * encoder->videoFrame->linesize[0] + x * 3] = pixel;
223				encoder->videoFrame->data[0][y * encoder->videoFrame->linesize[0] + x * 3 + 1] = pixel >> 8;
224				encoder->videoFrame->data[0][y * encoder->videoFrame->linesize[0] + x * 3 + 2] = pixel >> 16;
225			}
226		}
227	}
228
229	int gotData;
230	avcodec_encode_video2(encoder->video, &packet, encoder->videoFrame, &gotData);
231	if (gotData) {
232		if (encoder->videoStream->codec->coded_frame->key_frame) {
233			packet.flags |= AV_PKT_FLAG_KEY;
234		}
235		packet.stream_index = encoder->videoStream->index;
236		av_interleaved_write_frame(encoder->context, &packet);
237	}
238	av_free_packet(&packet);
239}