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