-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMediaDecoder.hpp
executable file
·97 lines (76 loc) · 3.04 KB
/
MediaDecoder.hpp
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
#ifndef MEDIA_DECODER_H_
#define MEDIA_DECODER_H_
#include "libav.hpp"
typedef enum {
Stride_Tight,
Stride_PowerOfTwo,
Stride_4bytes,
} StrideMode;
class MediaDecoder
{
private:
AVFormatContext* mFormatContext;
SwsContext* mSwsContext;
AVFrame* mFrame;
int mFirstVideoStreamIndex;
int mScaleWidth;
int mScaleHeight;
int mScaleStride;
PixelFormat mScalePixelFormat;
public:
MediaDecoder();
~MediaDecoder();
void outputMessage(int level, const char* format, ...);
// init
int openFile(const char* filename);
// properties
AVFormatContext* getFormatContext() const { return mFormatContext; }
SwsContext* getSwsContext() const { return mSwsContext; }
double getDuration() const { return (double)mFormatContext->duration / AV_TIME_BASE; }
int setScaleParameters(int width, int height, PixelFormat pixelFormat, StrideMode strideMode);
int getScaleWidth() const { return mScaleWidth; }
int getScaleHeight() const { return mScaleHeight; }
int getScaleStride() const { return mScaleStride; }
int getScalePixelFormat() const { return mScalePixelFormat; }
int getScaleBufferSize() const { return mScaleStride * mScaleHeight; }
AVStream* getStream(int streamIndex) const {
if (streamIndex < 0 || getStreamCount() <= streamIndex) { return NULL; }
return mFormatContext->streams[streamIndex];
}
AVCodecContext* getCodec(int streamIndex) const {
AVStream* stream = getStream(streamIndex);
if (!stream) { return NULL; }
return stream->codec;
}
int getStreamCount() const { return mFormatContext->nb_streams; }
int getVideoStreamIndex() const {
return mFirstVideoStreamIndex;
}
AVCodecContext* getVideoCodec() const {
int i = this->getVideoStreamIndex();
return getCodec(i);
}
AVMediaType getStreamMediaType(AVPacket* packet) const {
return getCodec(packet->stream_index)->codec_type;
}
// decode
int seek(double timestamp);
int seek(int streamIndex, int64_t timestamp);
int seekToIndex(int streamIndex, int index);
int decodeAudio(AVPacket* packet, uint8_t* buffer, int bufferSize, double* outTimestamp, int* outDataSize);
int decodeVideo(AVPacket* packet, uint8_t* buffer, int bufferSize, double thresholdTimestamp, double* outTimestamp, int* outDataSize, bool* outSkipped);
int readPacket(AVPacket* packet);
private:
// init
int prepareStream(int streamIndex);
// decode
int decodeVideoFrame(AVPacket* packet, AVFrame* outFrame, double* outTimestamp);
double getVideoTimestamp(AVPacket* packet, AVFrame* frame);
int scaleVideoFrame(AVFrame* frame, uint8_t* buffer, int bufferSize);
double getAudioTimestamp(const AVPacket* packet);
public:
static void initFfmpeg();
static AVPacket* newPacket() { AVPacket* p = new AVPacket; av_init_packet(p); return p; }
static void freePacket(AVPacket* packet) { av_free_packet(packet); }
};
#endif