src/feature/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 <mgba/core/core.h>
9#include <mgba/gba/interface.h>
10
11#include <libavcodec/version.h>
12#include <libavcodec/avcodec.h>
13
14#include <libavutil/version.h>
15#if LIBAVUTIL_VERSION_MAJOR >= 53
16#include <libavutil/buffer.h>
17#endif
18#include <libavutil/imgutils.h>
19#include <libavutil/mathematics.h>
20#include <libavutil/opt.h>
21
22#ifdef USE_LIBAVRESAMPLE
23#include <libavresample/avresample.h>
24#else
25#include <libswresample/swresample.h>
26#endif
27#include <libswscale/swscale.h>
28
29static void _ffmpegPostVideoFrame(struct mAVStream*, const color_t* pixels, size_t stride);
30static void _ffmpegPostAudioFrame(struct mAVStream*, int16_t left, int16_t right);
31static void _ffmpegSetVideoDimensions(struct mAVStream*, unsigned width, unsigned height);
32
33enum {
34 PREFERRED_SAMPLE_RATE = 0x8000
35};
36
37void FFmpegEncoderInit(struct FFmpegEncoder* encoder) {
38 av_register_all();
39
40 encoder->d.videoDimensionsChanged = _ffmpegSetVideoDimensions;
41 encoder->d.postVideoFrame = _ffmpegPostVideoFrame;
42 encoder->d.postAudioFrame = _ffmpegPostAudioFrame;
43 encoder->d.postAudioBuffer = 0;
44
45 encoder->audioCodec = NULL;
46 encoder->videoCodec = NULL;
47 encoder->containerFormat = NULL;
48 FFmpegEncoderSetAudio(encoder, "flac", 0);
49 FFmpegEncoderSetVideo(encoder, "png", 0);
50 FFmpegEncoderSetContainer(encoder, "matroska");
51 FFmpegEncoderSetDimensions(encoder, GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
52 encoder->iwidth = GBA_VIDEO_HORIZONTAL_PIXELS;
53 encoder->iheight = GBA_VIDEO_VERTICAL_PIXELS;
54 encoder->resampleContext = NULL;
55 encoder->absf = NULL;
56 encoder->context = NULL;
57 encoder->scaleContext = NULL;
58 encoder->audioStream = NULL;
59 encoder->audioFrame = NULL;
60 encoder->audioBuffer = NULL;
61 encoder->postaudioBuffer = NULL;
62 encoder->videoStream = NULL;
63 encoder->videoFrame = NULL;
64}
65
66bool FFmpegEncoderSetAudio(struct FFmpegEncoder* encoder, const char* acodec, unsigned abr) {
67 static const struct {
68 int format;
69 int priority;
70 } priorities[] = {
71 { AV_SAMPLE_FMT_S16, 0 },
72 { AV_SAMPLE_FMT_S16P, 1 },
73 { AV_SAMPLE_FMT_S32, 2 },
74 { AV_SAMPLE_FMT_S32P, 2 },
75 { AV_SAMPLE_FMT_FLT, 3 },
76 { AV_SAMPLE_FMT_FLTP, 3 },
77 { AV_SAMPLE_FMT_DBL, 4 },
78 { AV_SAMPLE_FMT_DBLP, 4 }
79 };
80
81 if (!acodec) {
82 encoder->audioCodec = 0;
83 return true;
84 }
85
86 AVCodec* codec = avcodec_find_encoder_by_name(acodec);
87 if (!codec) {
88 return false;
89 }
90
91 if (!codec->sample_fmts) {
92 return false;
93 }
94 size_t i;
95 size_t j;
96 int priority = INT_MAX;
97 encoder->sampleFormat = AV_SAMPLE_FMT_NONE;
98 for (i = 0; codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; ++i) {
99 for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
100 if (codec->sample_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
101 priority = priorities[j].priority;
102 encoder->sampleFormat = codec->sample_fmts[i];
103 }
104 }
105 }
106 if (encoder->sampleFormat == AV_SAMPLE_FMT_NONE) {
107 return false;
108 }
109 encoder->sampleRate = PREFERRED_SAMPLE_RATE;
110 if (codec->supported_samplerates) {
111 for (i = 0; codec->supported_samplerates[i]; ++i) {
112 if (codec->supported_samplerates[i] < PREFERRED_SAMPLE_RATE) {
113 continue;
114 }
115 if (encoder->sampleRate == PREFERRED_SAMPLE_RATE || encoder->sampleRate > codec->supported_samplerates[i]) {
116 encoder->sampleRate = codec->supported_samplerates[i];
117 }
118 }
119 } else if (codec->id == AV_CODEC_ID_AAC) {
120 // HACK: AAC doesn't support 32768Hz (it rounds to 32000), but libfaac doesn't tell us that
121 encoder->sampleRate = 44100;
122 }
123 encoder->audioCodec = acodec;
124 encoder->audioBitrate = abr;
125 return true;
126}
127
128bool FFmpegEncoderSetVideo(struct FFmpegEncoder* encoder, const char* vcodec, unsigned vbr) {
129 static const struct {
130 enum AVPixelFormat format;
131 int priority;
132 } priorities[] = {
133 { AV_PIX_FMT_RGB555, 0 },
134 { AV_PIX_FMT_BGR555, 0 },
135 { AV_PIX_FMT_RGB565, 1 },
136 { AV_PIX_FMT_BGR565, 1 },
137 { AV_PIX_FMT_RGB24, 2 },
138 { AV_PIX_FMT_BGR24, 2 },
139#ifndef USE_LIBAV
140 { AV_PIX_FMT_BGR0, 3 },
141 { AV_PIX_FMT_RGB0, 3 },
142 { AV_PIX_FMT_0BGR, 3 },
143 { AV_PIX_FMT_0RGB, 3 },
144#endif
145 { AV_PIX_FMT_YUV422P, 4 },
146 { AV_PIX_FMT_YUV444P, 5 },
147 { AV_PIX_FMT_YUV420P, 6 }
148 };
149 AVCodec* codec = avcodec_find_encoder_by_name(vcodec);
150 if (!codec) {
151 return false;
152 }
153
154 size_t i;
155 size_t j;
156 int priority = INT_MAX;
157 encoder->pixFormat = AV_PIX_FMT_NONE;
158 for (i = 0; codec->pix_fmts[i] != AV_PIX_FMT_NONE; ++i) {
159 for (j = 0; j < sizeof(priorities) / sizeof(*priorities); ++j) {
160 if (codec->pix_fmts[i] == priorities[j].format && priority > priorities[j].priority) {
161 priority = priorities[j].priority;
162 encoder->pixFormat = codec->pix_fmts[i];
163 }
164 }
165 }
166 if (encoder->pixFormat == AV_PIX_FMT_NONE) {
167 return false;
168 }
169 encoder->videoCodec = vcodec;
170 encoder->videoBitrate = vbr;
171 return true;
172}
173
174bool FFmpegEncoderSetContainer(struct FFmpegEncoder* encoder, const char* container) {
175 AVOutputFormat* oformat = av_guess_format(container, 0, 0);
176 if (!oformat) {
177 return false;
178 }
179 encoder->containerFormat = container;
180 return true;
181}
182
183void FFmpegEncoderSetDimensions(struct FFmpegEncoder* encoder, int width, int height) {
184 encoder->width = width > 0 ? width : GBA_VIDEO_HORIZONTAL_PIXELS;
185 encoder->height = height > 0 ? height : GBA_VIDEO_VERTICAL_PIXELS;
186}
187
188bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder* encoder) {
189 AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
190 AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
191 AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
192 if ((encoder->audioCodec && !acodec) || !vcodec || !oformat) {
193 return false;
194 }
195 if (encoder->audioCodec && !avformat_query_codec(oformat, acodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
196 return false;
197 }
198 if (!avformat_query_codec(oformat, vcodec->id, FF_COMPLIANCE_EXPERIMENTAL)) {
199 return false;
200 }
201 return true;
202}
203
204bool FFmpegEncoderOpen(struct FFmpegEncoder* encoder, const char* outfile) {
205 AVCodec* acodec = avcodec_find_encoder_by_name(encoder->audioCodec);
206 AVCodec* vcodec = avcodec_find_encoder_by_name(encoder->videoCodec);
207 if ((encoder->audioCodec && !acodec) || (encoder->videoCodec && !vcodec) || !FFmpegEncoderVerifyContainer(encoder)) {
208 return false;
209 }
210
211 if (encoder->context) {
212 return false;
213 }
214
215 encoder->currentAudioSample = 0;
216 encoder->currentAudioFrame = 0;
217 encoder->currentVideoFrame = 0;
218
219 AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0);
220#ifndef USE_LIBAV
221 avformat_alloc_output_context2(&encoder->context, oformat, 0, outfile);
222#else
223 encoder->context = avformat_alloc_context();
224 strncpy(encoder->context->filename, outfile, sizeof(encoder->context->filename) - 1);
225 encoder->context->filename[sizeof(encoder->context->filename) - 1] = '\0';
226 encoder->context->oformat = oformat;
227#endif
228
229 if (acodec) {
230#ifdef FFMPEG_USE_CODECPAR
231 encoder->audioStream = avformat_new_stream(encoder->context, NULL);
232 encoder->audio = avcodec_alloc_context3(acodec);
233#else
234 encoder->audioStream = avformat_new_stream(encoder->context, acodec);
235 encoder->audio = encoder->audioStream->codec;
236#endif
237 encoder->audio->bit_rate = encoder->audioBitrate;
238 encoder->audio->channels = 2;
239 encoder->audio->channel_layout = AV_CH_LAYOUT_STEREO;
240 encoder->audio->sample_rate = encoder->sampleRate;
241 encoder->audio->sample_fmt = encoder->sampleFormat;
242 AVDictionary* opts = 0;
243 av_dict_set(&opts, "strict", "-2", 0);
244 if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
245#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
246 encoder->audio->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
247#else
248 encoder->audio->flags |= CODEC_FLAG_GLOBAL_HEADER;
249#endif
250 }
251 int res = avcodec_open2(encoder->audio, acodec, &opts);
252 av_dict_free(&opts);
253 if (res < 0) {
254 FFmpegEncoderClose(encoder);
255 return false;
256 }
257#if LIBAVCODEC_VERSION_MAJOR >= 55
258 encoder->audioFrame = av_frame_alloc();
259#else
260 encoder->audioFrame = avcodec_alloc_frame();
261#endif
262 if (!encoder->audio->frame_size) {
263 encoder->audio->frame_size = 1;
264 }
265 encoder->audioFrame->nb_samples = encoder->audio->frame_size;
266 encoder->audioFrame->format = encoder->audio->sample_fmt;
267 encoder->audioFrame->pts = 0;
268#ifdef USE_LIBAVRESAMPLE
269 encoder->resampleContext = avresample_alloc_context();
270 av_opt_set_int(encoder->resampleContext, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
271 av_opt_set_int(encoder->resampleContext, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
272 av_opt_set_int(encoder->resampleContext, "in_sample_rate", PREFERRED_SAMPLE_RATE, 0);
273 av_opt_set_int(encoder->resampleContext, "out_sample_rate", encoder->sampleRate, 0);
274 av_opt_set_int(encoder->resampleContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
275 av_opt_set_int(encoder->resampleContext, "out_sample_fmt", encoder->sampleFormat, 0);
276 avresample_open(encoder->resampleContext);
277#else
278 encoder->resampleContext = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, encoder->sampleFormat, encoder->sampleRate,
279 AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, PREFERRED_SAMPLE_RATE, 0, NULL);
280 swr_init(encoder->resampleContext);
281#endif
282 encoder->audioBufferSize = (encoder->audioFrame->nb_samples * PREFERRED_SAMPLE_RATE / encoder->sampleRate) * 4;
283 encoder->audioBuffer = av_malloc(encoder->audioBufferSize);
284 encoder->postaudioBufferSize = av_samples_get_buffer_size(0, encoder->audio->channels, encoder->audio->frame_size, encoder->audio->sample_fmt, 0);
285 encoder->postaudioBuffer = av_malloc(encoder->postaudioBufferSize);
286 avcodec_fill_audio_frame(encoder->audioFrame, encoder->audio->channels, encoder->audio->sample_fmt, (const uint8_t*) encoder->postaudioBuffer, encoder->postaudioBufferSize, 0);
287
288 if (encoder->audio->codec->id == AV_CODEC_ID_AAC &&
289 (strcasecmp(encoder->containerFormat, "mp4") ||
290 strcasecmp(encoder->containerFormat, "m4v") ||
291 strcasecmp(encoder->containerFormat, "mov"))) {
292 // MP4 container doesn't support the raw ADTS AAC format that the encoder spits out
293#ifdef FFMPEG_USE_NEW_BSF
294 av_bsf_alloc(av_bsf_get_by_name("aac_adtstoasc"), &encoder->absf);
295 avcodec_parameters_from_context(encoder->absf->par_in, encoder->audio);
296 av_bsf_init(encoder->absf);
297#else
298 encoder->absf = av_bitstream_filter_init("aac_adtstoasc");
299#endif
300 }
301#ifdef FFMPEG_USE_CODECPAR
302 avcodec_parameters_from_context(encoder->audioStream->codecpar, encoder->audio);
303#endif
304 }
305
306 if (vcodec) {
307#ifdef FFMPEG_USE_CODECPAR
308 encoder->videoStream = avformat_new_stream(encoder->context, NULL);
309 encoder->video = avcodec_alloc_context3(vcodec);
310#else
311 encoder->videoStream = avformat_new_stream(encoder->context, vcodec);
312 encoder->video = encoder->videoStream->codec;
313#endif
314 encoder->video->bit_rate = encoder->videoBitrate;
315 encoder->video->width = encoder->width;
316 encoder->video->height = encoder->height;
317 encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH, GBA_ARM7TDMI_FREQUENCY };
318 encoder->video->framerate = (AVRational) { GBA_ARM7TDMI_FREQUENCY, VIDEO_TOTAL_LENGTH };
319 encoder->video->pix_fmt = encoder->pixFormat;
320 encoder->video->gop_size = 60;
321 encoder->video->max_b_frames = 3;
322 if (encoder->context->oformat->flags & AVFMT_GLOBALHEADER) {
323#ifdef AV_CODEC_FLAG_GLOBAL_HEADER
324 encoder->video->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
325#else
326 encoder->video->flags |= CODEC_FLAG_GLOBAL_HEADER;
327#endif
328 }
329
330 if (encoder->video->codec->id == AV_CODEC_ID_H264 &&
331 (strcasecmp(encoder->containerFormat, "mp4") ||
332 strcasecmp(encoder->containerFormat, "m4v") ||
333 strcasecmp(encoder->containerFormat, "mov"))) {
334 // QuickTime and a few other things require YUV420
335 encoder->video->pix_fmt = AV_PIX_FMT_YUV420P;
336 }
337
338 if (strcmp(vcodec->name, "libx264") == 0) {
339 // Try to adaptively figure out when you can use a slower encoder
340 if (encoder->width * encoder->height > 1000000) {
341 av_opt_set(encoder->video->priv_data, "preset", "superfast", 0);
342 } else if (encoder->width * encoder->height > 500000) {
343 av_opt_set(encoder->video->priv_data, "preset", "veryfast", 0);
344 } else {
345 av_opt_set(encoder->video->priv_data, "preset", "faster", 0);
346 }
347 if (encoder->videoBitrate == 0) {
348 av_opt_set(encoder->video->priv_data, "crf", "0", 0);
349 encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
350 }
351 }
352 if (strcmp(vcodec->name, "libvpx-vp9") == 0 && encoder->videoBitrate == 0) {
353 av_opt_set(encoder->video->priv_data, "lossless", "1", 0);
354 encoder->video->pix_fmt = AV_PIX_FMT_YUV444P;
355 }
356
357 if (avcodec_open2(encoder->video, vcodec, 0) < 0) {
358 FFmpegEncoderClose(encoder);
359 return false;
360 }
361#if LIBAVCODEC_VERSION_MAJOR >= 55
362 encoder->videoFrame = av_frame_alloc();
363#else
364 encoder->videoFrame = avcodec_alloc_frame();
365#endif
366 encoder->videoFrame->format = encoder->video->pix_fmt;
367 encoder->videoFrame->width = encoder->video->width;
368 encoder->videoFrame->height = encoder->video->height;
369 encoder->videoFrame->pts = 0;
370 _ffmpegSetVideoDimensions(&encoder->d, encoder->iwidth, encoder->iheight);
371 av_image_alloc(encoder->videoFrame->data, encoder->videoFrame->linesize, encoder->video->width, encoder->video->height, encoder->video->pix_fmt, 32);
372#ifdef FFMPEG_USE_CODECPAR
373 avcodec_parameters_from_context(encoder->videoStream->codecpar, encoder->video);
374#endif
375 }
376
377 if (avio_open(&encoder->context->pb, outfile, AVIO_FLAG_WRITE) < 0 || avformat_write_header(encoder->context, 0) < 0) {
378 FFmpegEncoderClose(encoder);
379 return false;
380 }
381 return true;
382}
383
384void FFmpegEncoderClose(struct FFmpegEncoder* encoder) {
385 if (encoder->context && encoder->context->pb) {
386 av_write_trailer(encoder->context);
387 avio_close(encoder->context->pb);
388 }
389
390 if (encoder->postaudioBuffer) {
391 av_free(encoder->postaudioBuffer);
392 encoder->postaudioBuffer = NULL;
393 }
394 if (encoder->audioBuffer) {
395 av_free(encoder->audioBuffer);
396 encoder->audioBuffer = NULL;
397 }
398
399 if (encoder->audioFrame) {
400#if LIBAVCODEC_VERSION_MAJOR >= 55
401 av_frame_free(&encoder->audioFrame);
402#else
403 avcodec_free_frame(&encoder->audioFrame);
404#endif
405 }
406 if (encoder->audio) {
407 avcodec_close(encoder->audio);
408 encoder->audio = NULL;
409 }
410
411 if (encoder->resampleContext) {
412#ifdef USE_LIBAVRESAMPLE
413 avresample_close(encoder->resampleContext);
414 encoder->resampleContext = NULL;
415#else
416 swr_free(&encoder->resampleContext);
417#endif
418 }
419
420 if (encoder->absf) {
421#ifdef FFMPEG_USE_NEW_BSF
422 av_bsf_free(&encoder->absf);
423#else
424 av_bitstream_filter_close(encoder->absf);
425 encoder->absf = NULL;
426#endif
427 }
428
429 if (encoder->videoFrame) {
430#if LIBAVCODEC_VERSION_MAJOR >= 55
431 av_frame_free(&encoder->videoFrame);
432#else
433 avcodec_free_frame(&encoder->videoFrame);
434#endif
435 }
436
437 if (encoder->video) {
438 avcodec_close(encoder->video);
439 encoder->video = NULL;
440 }
441
442 if (encoder->scaleContext) {
443 sws_freeContext(encoder->scaleContext);
444 encoder->scaleContext = NULL;
445 }
446
447 if (encoder->context) {
448 avformat_free_context(encoder->context);
449 encoder->context = NULL;
450 }
451}
452
453bool FFmpegEncoderIsOpen(struct FFmpegEncoder* encoder) {
454 return !!encoder->context;
455}
456
457void _ffmpegPostAudioFrame(struct mAVStream* stream, int16_t left, int16_t right) {
458 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
459 if (!encoder->context || !encoder->audioCodec) {
460 return;
461 }
462
463 if (encoder->absf && !left) {
464 // XXX: AVBSF doesn't like silence. Figure out why.
465 left = 1;
466 }
467
468 encoder->audioBuffer[encoder->currentAudioSample * 2] = left;
469 encoder->audioBuffer[encoder->currentAudioSample * 2 + 1] = right;
470
471 ++encoder->currentAudioSample;
472
473 if (encoder->currentAudioSample * 4 < encoder->audioBufferSize) {
474 return;
475 }
476
477 int channelSize = 2 * av_get_bytes_per_sample(encoder->audio->sample_fmt);
478 encoder->currentAudioSample = 0;
479#ifdef USE_LIBAVRESAMPLE
480 avresample_convert(encoder->resampleContext, 0, 0, 0,
481 (uint8_t**) &encoder->audioBuffer, 0, encoder->audioBufferSize / 4);
482
483 if (avresample_available(encoder->resampleContext) < encoder->audioFrame->nb_samples) {
484 return;
485 }
486#if LIBAVCODEC_VERSION_MAJOR >= 55
487 av_frame_make_writable(encoder->audioFrame);
488#endif
489 int samples = avresample_read(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize);
490#else
491#if LIBAVCODEC_VERSION_MAJOR >= 55
492 av_frame_make_writable(encoder->audioFrame);
493#endif
494 if (swr_get_out_samples(encoder->resampleContext, encoder->audioBufferSize / 4) < encoder->audioFrame->nb_samples) {
495 swr_convert(encoder->resampleContext, NULL, 0, (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
496 return;
497 }
498 int samples = swr_convert(encoder->resampleContext, encoder->audioFrame->data, encoder->postaudioBufferSize / channelSize,
499 (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4);
500#endif
501
502 encoder->audioFrame->pts = encoder->currentAudioFrame;
503 encoder->currentAudioFrame += samples;
504
505 AVPacket packet;
506 av_init_packet(&packet);
507 packet.data = 0;
508 packet.size = 0;
509
510 int gotData;
511#ifdef FFMPEG_USE_PACKETS
512 avcodec_send_frame(encoder->audio, encoder->audioFrame);
513 gotData = avcodec_receive_packet(encoder->audio, &packet);
514 gotData = (gotData == 0) && packet.size;
515#else
516 avcodec_encode_audio2(encoder->audio, &packet, encoder->audioFrame, &gotData);
517#endif
518 packet.pts = av_rescale_q(encoder->audioFrame->pts, encoder->audio->time_base, encoder->audioStream->time_base);
519 packet.dts = packet.pts;
520
521 if (gotData) {
522 if (encoder->absf) {
523 AVPacket tempPacket;
524
525#ifdef FFMPEG_USE_NEW_BSF
526 int success = av_bsf_send_packet(encoder->absf, &packet);
527 if (success >= 0) {
528 success = av_bsf_receive_packet(encoder->absf, &tempPacket);
529 }
530#else
531 int success = av_bitstream_filter_filter(encoder->absf, encoder->audio, 0,
532 &tempPacket.data, &tempPacket.size,
533 packet.data, packet.size, 0);
534#endif
535
536 if (success >= 0) {
537#if LIBAVUTIL_VERSION_MAJOR >= 53
538 tempPacket.buf = av_buffer_create(tempPacket.data, tempPacket.size, av_buffer_default_free, 0, 0);
539#endif
540
541#ifdef FFMPEG_USE_PACKET_UNREF
542 av_packet_move_ref(&packet, &tempPacket);
543#else
544 av_free_packet(&packet);
545 packet = tempPacket;
546#endif
547
548 packet.stream_index = encoder->audioStream->index;
549 av_interleaved_write_frame(encoder->context, &packet);
550 }
551 } else {
552 packet.stream_index = encoder->audioStream->index;
553 av_interleaved_write_frame(encoder->context, &packet);
554 }
555 }
556#ifdef FFMPEG_USE_PACKET_UNREF
557 av_packet_unref(&packet);
558#else
559 av_free_packet(&packet);
560#endif
561}
562
563void _ffmpegPostVideoFrame(struct mAVStream* stream, const color_t* pixels, size_t stride) {
564 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
565 if (!encoder->context) {
566 return;
567 }
568 stride *= BYTES_PER_PIXEL;
569
570 AVPacket packet;
571
572 av_init_packet(&packet);
573 packet.data = 0;
574 packet.size = 0;
575#if LIBAVCODEC_VERSION_MAJOR >= 55
576 av_frame_make_writable(encoder->videoFrame);
577#endif
578 encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base);
579 ++encoder->currentVideoFrame;
580
581 sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, encoder->iheight, encoder->videoFrame->data, encoder->videoFrame->linesize);
582
583 int gotData;
584#ifdef FFMPEG_USE_PACKETS
585 avcodec_send_frame(encoder->video, encoder->videoFrame);
586 gotData = avcodec_receive_packet(encoder->video, &packet) == 0;
587#else
588 avcodec_encode_video2(encoder->video, &packet, encoder->videoFrame, &gotData);
589#endif
590 packet.pts = encoder->videoFrame->pts;
591 if (gotData) {
592#ifndef FFMPEG_USE_PACKET_UNREF
593 if (encoder->video->coded_frame->key_frame) {
594 packet.flags |= AV_PKT_FLAG_KEY;
595 }
596#endif
597 packet.stream_index = encoder->videoStream->index;
598 av_interleaved_write_frame(encoder->context, &packet);
599 }
600#ifdef FFMPEG_USE_PACKET_UNREF
601 av_packet_unref(&packet);
602#else
603 av_free_packet(&packet);
604#endif
605}
606
607static void _ffmpegSetVideoDimensions(struct mAVStream* stream, unsigned width, unsigned height) {
608 struct FFmpegEncoder* encoder = (struct FFmpegEncoder*) stream;
609 encoder->iwidth = width;
610 encoder->iheight = height;
611 if (encoder->scaleContext) {
612 sws_freeContext(encoder->scaleContext);
613 }
614 encoder->scaleContext = sws_getContext(encoder->iwidth, encoder->iheight,
615#ifdef COLOR_16_BIT
616#ifdef COLOR_5_6_5
617 AV_PIX_FMT_RGB565,
618#else
619 AV_PIX_FMT_BGR555,
620#endif
621#else
622#ifndef USE_LIBAV
623 AV_PIX_FMT_0BGR32,
624#else
625 AV_PIX_FMT_BGR32,
626#endif
627#endif
628 encoder->videoFrame->width, encoder->videoFrame->height, encoder->video->pix_fmt,
629 SWS_POINT, 0, 0, 0);
630}