src/feature/ffmpeg/ffmpeg-encoder.h (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#ifndef FFMPEG_ENCODER
7#define FFMPEG_ENCODER
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/internal/gba/gba.h>
14
15#include <libavformat/avformat.h>
16#include <libavcodec/version.h>
17
18// Version 57.16 in FFmpeg
19#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
20#define FFMPEG_USE_PACKETS
21#endif
22
23// Version 57.15 in libav
24#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 35, 0)
25#define FFMPEG_USE_NEW_BSF
26#endif
27
28// Version 57.14 in libav
29#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 0)
30#define FFMPEG_USE_CODECPAR
31#endif
32
33#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 8, 0)
34#define FFMPEG_USE_PACKET_UNREF
35#endif
36
37struct FFmpegEncoder {
38 struct mAVStream d;
39 struct AVFormatContext* context;
40
41 unsigned audioBitrate;
42 const char* audioCodec;
43
44 unsigned videoBitrate;
45 const char* videoCodec;
46
47 const char* containerFormat;
48
49 struct AVCodecContext* audio;
50 enum AVSampleFormat sampleFormat;
51 int sampleRate;
52 uint16_t* audioBuffer;
53 size_t audioBufferSize;
54 uint16_t* postaudioBuffer;
55 size_t postaudioBufferSize;
56 AVFrame* audioFrame;
57 size_t currentAudioSample;
58 int64_t currentAudioFrame;
59 int64_t nextAudioPts; // TODO (0.6): Remove
60#ifdef USE_LIBAVRESAMPLE
61 struct AVAudioResampleContext* resampleContext;
62#else
63 struct SwrContext* resampleContext;
64#endif
65#ifdef FFMPEG_USE_NEW_BSF
66 struct AVBSFContext* absf; // Needed for AAC in MP4
67#else
68 struct AVBitStreamFilterContext* absf; // Needed for AAC in MP4
69#endif
70 struct AVStream* audioStream;
71
72 struct AVCodecContext* video;
73 enum AVPixelFormat pixFormat;
74 struct AVFrame* videoFrame;
75 int width;
76 int height;
77 int iwidth;
78 int iheight;
79 int64_t currentVideoFrame;
80 struct SwsContext* scaleContext;
81 struct AVStream* videoStream;
82};
83
84void FFmpegEncoderInit(struct FFmpegEncoder*);
85bool FFmpegEncoderSetAudio(struct FFmpegEncoder*, const char* acodec, unsigned abr);
86bool FFmpegEncoderSetVideo(struct FFmpegEncoder*, const char* vcodec, unsigned vbr);
87bool FFmpegEncoderSetContainer(struct FFmpegEncoder*, const char* container);
88void FFmpegEncoderSetDimensions(struct FFmpegEncoder*, int width, int height);
89bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder*);
90bool FFmpegEncoderOpen(struct FFmpegEncoder*, const char* outfile);
91void FFmpegEncoderClose(struct FFmpegEncoder*);
92bool FFmpegEncoderIsOpen(struct FFmpegEncoder*);
93
94CXX_GUARD_END
95
96#endif