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 struct AVAudioResampleContext* resampleContext;
61#ifdef FFMPEG_USE_NEW_BSF
62 struct AVBSFContext* absf; // Needed for AAC in MP4
63#else
64 struct AVBitStreamFilterContext* absf; // Needed for AAC in MP4
65#endif
66 struct AVStream* audioStream;
67
68 struct AVCodecContext* video;
69 enum AVPixelFormat pixFormat;
70 struct AVFrame* videoFrame;
71 int width;
72 int height;
73 int iwidth;
74 int iheight;
75 int64_t currentVideoFrame;
76 struct SwsContext* scaleContext;
77 struct AVStream* videoStream;
78};
79
80void FFmpegEncoderInit(struct FFmpegEncoder*);
81bool FFmpegEncoderSetAudio(struct FFmpegEncoder*, const char* acodec, unsigned abr);
82bool FFmpegEncoderSetVideo(struct FFmpegEncoder*, const char* vcodec, unsigned vbr);
83bool FFmpegEncoderSetContainer(struct FFmpegEncoder*, const char* container);
84void FFmpegEncoderSetDimensions(struct FFmpegEncoder*, int width, int height);
85bool FFmpegEncoderVerifyContainer(struct FFmpegEncoder*);
86bool FFmpegEncoderOpen(struct FFmpegEncoder*, const char* outfile);
87void FFmpegEncoderClose(struct FFmpegEncoder*);
88bool FFmpegEncoderIsOpen(struct FFmpegEncoder*);
89
90CXX_GUARD_END
91
92#endif