-
Notifications
You must be signed in to change notification settings - Fork 5
/
poFFMpegDecoder.h
169 lines (128 loc) · 3.21 KB
/
poFFMpegDecoder.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// MovieDecoder.h
// curve
//
// Created by Joshua Fisher on 1/10/13.
// Copyright (c) 2013 Joshua Fisher. All rights reserved.
//
#pragma once
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
}
namespace po {
class PacketQueue {
std::list<AVPacket> packets;
public:
PacketQueue();
bool isEmpty();
void push(AVPacket pkt);
int pop(AVPacket* pkt);
void flush();
};
class Demuxer {
public:
static Demuxer* open(const char*);
~Demuxer();
AVFormatContext* getFormat();
int getStreamIndex(AVMediaType);
AVStream* getStream(int streamIdx);
PacketQueue* getPacketQueue(int streamIdx);
// trigger loading packets into all registered queues
void demux(int streamIndx);
void seekToTime(double time);
private:
Demuxer();
AVFormatContext* format;
std::map<int,PacketQueue*> packetQueues;
};
// single frame of RGB video
struct VideoFrame {
typedef boost::shared_ptr<VideoFrame> Ptr;
int width;
int height;
int numBytes;
uint8_t* bytes;
double outTime;
~VideoFrame();
static Ptr create(double o, int w, int h, int sz, uint8_t* ptr);
private:
VideoFrame();
VideoFrame(const VideoFrame&) {}
VideoFrame& operator=(const VideoFrame&) {};
};
struct AudioBuffer {
typedef boost::shared_ptr<AudioBuffer> Ptr;
int sampleRate;
int numBytes;
uint8_t* bytes;
double outTime;
~AudioBuffer();
static Ptr create(double o, int sr, int sz, uint8_t* ptr);
private:
AudioBuffer();
AudioBuffer(const AudioBuffer&) {};
AudioBuffer& operator=(const AudioBuffer&) {};
};
class VideoDecoder {
public:
static VideoDecoder* open(Demuxer*);
~VideoDecoder();
int getWidth();
int getHeight();
int getBytesPerFrame();
bool isLastFrame();
VideoFrame::Ptr previousFrame();
VideoFrame::Ptr nextFrame();
int64_t getCurrentFrame();
int64_t getFrameCount();
double getCurrentTime();
double getNextTime();
void seekToFrame(int64_t frame);
void seekToTime(double time);
private:
VideoDecoder();
Demuxer* demuxer;
PacketQueue* packets;
int streamIdx;
AVStream* stream;
AVCodecContext* context;
AVFrame *frame, *frameRGB;
SwsContext* sws;
double clock;
double nextFrameTime;
int width, height, bytesPerFrame;
bool lastFrame;
int64_t currentFrame;
int64_t numFrames;
double frameRate;
double bitRate;
};
class AudioDecoder {
public:
static AudioDecoder* open(Demuxer*);
~AudioDecoder();
bool isLastBuffer() const;
int getSampleRate() const;
int getSampleSize() const;
int getFrameSize() const;
int getChannelCount() const;
AudioBuffer::Ptr nextBuffer();
void seekToTime(double time);
private:
AudioDecoder();
AudioBuffer::Ptr convert(AVFrame*);
Demuxer* demuxer;
PacketQueue* packets;
int streamIdx;
AVStream* stream;
AVCodecContext* context;
AVFrame* frame;
SwrContext* swr;
int frameSize;
int channels, sampleRate, sampleSize;
bool lastBuffer;
};
}