-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavcodecwrapper.h
123 lines (93 loc) · 3.27 KB
/
avcodecwrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef AVCODECWRAPPER_H
#define AVCODECWRAPPER_H
#include <QObject>
#include <QString>
#include <QException>
#include "soundeffect.h"
#include "audiojoiner.h"
extern "C" {
#include <libavformat/avformat.h>
}
class avcodecWrapper : public QObject
{
public:
avcodecWrapper();
void AddVideoFrame (const QString &filename);
void AddAudioFile (const SoundEffect &soundEffect, double tOffset);
void Encode (const QString &filename, int w, int h, int fps);
private:
QStringList _videoFrames;
QList<SoundEffect> _soundEffects;
QString _outputFilename;
int _w;
int _h;
int _framesPerSecond;
int _numberOfFrames;
double _streamDuration;
private:
// A utility wrapper around a single output AVStream, used internally only
class OutputStream {
public:
OutputStream () :
st (nullptr),
enc(nullptr),
next_pts(0),
samples_count(0),
frame(nullptr),
sws_ctx (nullptr) {}
AVStream *st;
AVCodecContext *enc;
/* pts of the next frame that will be generated */
int64_t next_pts;
int samples_count;
AVFrame *frame;
struct SwsContext *sws_ctx;
};
private:
// These are basically the wrapped functions from the libav* examples, slightly
// modified to be member functions.
void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt);
int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt);
void add_stream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, AVCodecID codec_id);
AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,uint64_t channel_layout,int sample_rate, int nb_samples);
void open_audio(AVFormatContext *, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg);
int write_audio_frame(AVFormatContext *oc, OutputStream *ost);
AVFrame *alloc_picture(AVPixelFormat pix_fmt);
void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg);
void fill_yuv_image(AVFrame *pict, int frame_index);
AVFrame *get_video_frame(OutputStream *ost);
int write_video_frame(AVFormatContext *oc, OutputStream *ost);
void close_stream(AVFormatContext *oc, OutputStream *ost);
void wrapMain();
private:
// Formerly DEFINEs
const int INBUF_SIZE = 4096;
const int AUDIO_INBUF_SIZE = 20480;
const int AUDIO_REFILL_THRESH = 4096;
const AVPixelFormat STREAM_PIX_FMT = AV_PIX_FMT_YUV420P;
// Audio output variables
uint8_t **src_samples_data;
int src_samples_linesize;
int src_nb_samples;
int max_dst_nb_samples;
uint8_t **dst_samples_data;
int dst_samples_linesize;
int dst_samples_size;
AudioJoiner _audioJoiner;
// Video output variables
AVFrame *frame;
AVPicture src_picture, dst_picture;
int frame_count;
public:
class libavException : public QException
{
public:
libavException(QString message = "") {_message = message;}
void raise() const { throw *this; }
libavException *clone() const {return new libavException(*this); }
QString message() const {return _message;}
private:
QString _message;
};
};
#endif // AVCODECWRAPPER_H