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
37#define FFMPEG_FILTERS_MAX 4
38
39struct FFmpegEncoder {
40 struct mAVStream d;
41 struct AVFormatContext* context;
42
43 unsigned audioBitrate;
44 const char* audioCodec;
45
46 unsigned videoBitrate;
47 const char* videoCodec;
48
49 const char* containerFormat;
50
51 struct AVCodecContext* audio;
52 enum AVSampleFormat sampleFormat;
53 int sampleRate;
54 uint16_t* audioBuffer;
55 size_t audioBufferSize;
56 uint16_t* postaudioBuffer;
57 size_t postaudioBufferSize;
58 AVFrame* audioFrame;
59 size_t currentAudioSample;
60 int64_t currentAudioFrame;
61#ifdef USE_LIBAVRESAMPLE
62 struct AVAudioResampleContext* resampleContext;
63#else
64 struct SwrContext* resampleContext;
65#endif
66#ifdef FFMPEG_USE_NEW_BSF
67 struct AVBSFContext* absf; // Needed for AAC in MP4
68#else
69 struct AVBitStreamFilterContext* absf; // Needed for AAC in MP4
70#endif
71 struct AVStream* audioStream;
72
73 struct AVCodecContext* video;
74 enum AVPixelFormat pixFormat;
75 enum AVPixelFormat ipixFormat;
76 struct AVFrame* videoFrame;
77 int width;
78 int height;
79 int iwidth;
80 int iheight;
81 int frameCycles;
82 int cycles;
83 int frameskip;
84 int skipResidue;
85 bool loop;
86 int64_t currentVideoFrame;
87 struct SwsContext* scaleContext;
88 struct AVStream* videoStream;
89
90 struct AVFilterGraph* graph;
91 struct AVFilterContext* source;
92 struct AVFilterContext* sink;
93 struct AVFilterContext* filters[FFMPEG_FILTERS_MAX];
94 struct AVFrame* sinkFrame;
95};
96
97void FFmpegEncoderInit(struct FFmpegEncoder*);
98bool FFmpegEncoderSetAudio(struct FFmpegEncoder*, const char* acodec, unsigned abr);
99bool FFmpegEncoderSetVideo(struct FFmpegEncoder*, const char* vcodec, unsigned vbr, int frameskip);
100bool FFmpegEncoderSetContainer(struct FFmpegEncoder*, const char* container);
101void FFmpegEncoderSetDimensions(struct FFmpegEncoder*, int width, int height);
102void FFmpegEncoderSetInputFrameRate(struct FFmpegEncoder*, int numerator, int denominator);
103void FFmpegEncoderSetLooping(struct FFmpegEncoder*, bool loop);
104bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder*);
105bool FFmpegEncoderOpen(struct FFmpegEncoder*, const char* outfile);
106void FFmpegEncoderClose(struct FFmpegEncoder*);
107bool FFmpegEncoderIsOpen(struct FFmpegEncoder*);
108
109CXX_GUARD_END
110
111#endif