-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.cpp
More file actions
196 lines (159 loc) · 4.54 KB
/
Decoder.cpp
File metadata and controls
196 lines (159 loc) · 4.54 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <QMessageBox>
#include <thread>
#include "Decoder.h"
extern "C"
{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libavutil/mem.h"
#include "libavutil/avutil.h"
#include "libavutil/imgutils.h"
};
Decoder::Decoder()
{
}
Decoder::~Decoder()
{
}
void Decoder::SetSourcePath(QString& path)
{
m_path = path;
}
bool Decoder::DecoderInit()
{
char buf[256];
AVFormatContext* avFormatContext = avformat_alloc_context();//获取上下文
int error;
//打开视频地址并获取里面的内容(解封装)
error = avformat_open_input(&avFormatContext, m_path.toStdString().c_str(), NULL, NULL);
if (error < 0) {
qDebug("open input fail");
return false;
}
if (error = avformat_find_stream_info(avFormatContext, NULL) < 0) {
qDebug("获取内容失败,err code: %d",error);
return false;
}
//TODO
av_dump_format(avFormatContext, 0, m_path.toStdString().c_str(), 0);
//获取视频的编码信息
AVCodecParameters* origin_par = NULL;
int mVideoStreamIdx = -1;
mVideoStreamIdx = av_find_best_stream(avFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (mVideoStreamIdx < 0) {
av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
return false;
}
origin_par = avFormatContext->streams[mVideoStreamIdx]->codecpar;
// 寻找解码器 {start
AVCodec* mVcodec = NULL;
AVCodecContext* mAvContext = NULL;
mVcodec = const_cast<AVCodec*>( avcodec_find_decoder(origin_par->codec_id));
mAvContext = avcodec_alloc_context3(mVcodec);
if (!mVcodec || !mAvContext) {
return false;
}
//不初始化解码器context会导致MP4封装的mpeg4码流解码失败
int ret = avcodec_parameters_to_context(mAvContext, origin_par);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
}
// 打开解码器
if (avcodec_open2(mAvContext, mVcodec, NULL) != 0) {
qDebug("avcodec_open2 fail");
return false;
}
//循环读入数据
//申请AVPacket
AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));
av_init_packet(packet);
//申请AVFrame
AVFrame* frame = av_frame_alloc();//分配一个AVFrame结构体,AVFrame结构体一般用于存储原始数据,指向解码后的原始帧
uint8_t* byte_buffer = NULL;
int byte_buffer_size = av_image_get_buffer_size(mAvContext->pix_fmt, mAvContext->width, mAvContext->height, 32);
qDebug("width = %d , height = %d ", mAvContext->width, mAvContext->height);
byte_buffer = (uint8_t*)av_malloc(byte_buffer_size);
if (!byte_buffer) {
av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
return AVERROR(ENOMEM);
}
if (m_CallBacSizeFunc)
{
m_CallBacSizeFunc(mAvContext->width, mAvContext->height);
}
static int count1 = 0;
while (1)
{
int ret = av_read_frame(avFormatContext, packet);
if (ret != 0) {
av_strerror(ret, buf, sizeof(buf));
qDebug("--%s--\n", buf);
av_packet_unref(packet);
break;
}
if (ret >= 0 && packet->stream_index != mVideoStreamIdx) {
av_packet_unref(packet);
continue;
}
{
// 发送待解码包
int result = avcodec_send_packet(mAvContext, packet);
av_packet_unref(packet);
if (result < 0) {
av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
continue;
}
// 接收解码数据
while (result >= 0) {
result = avcodec_receive_frame(mAvContext, frame);
if (result == AVERROR_EOF)
break;
else if (result == AVERROR(EAGAIN)) {
result = 0;
break;
}
else if (result < 0) {
av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
av_frame_unref(frame);
break;
}
int number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
(const uint8_t* const*)frame->data, (const int*)frame->linesize,
mAvContext->pix_fmt, mAvContext->width, mAvContext->height, 1);
if (number_of_written_bytes < 0) {
av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
av_frame_unref(frame);
continue;
}
if (m_CallBackFuc)
{
m_CallBackFuc(byte_buffer_size, byte_buffer);
std::this_thread::sleep_for(std::chrono::milliseconds(41));
}
// 写文件保存视频数据
//fwrite(byte_buffer, number_of_written_bytes, 1, fp_YUV);
//fflush(fp_YUV);
//static long s_size = 0;
//if (s_size != number_of_written_bytes)
//{
// s_size = number_of_written_bytes;
//}
av_frame_unref(frame);
}
}
}
av_free(byte_buffer);
av_frame_free(&frame);
avcodec_close(mAvContext);
avformat_free_context(avFormatContext);
return true;
}
void Decoder::registerBufferPointFunction(callBackPtr ptr)
{
m_CallBackFuc = ptr;
}
void Decoder::registerSetSizeFunc(callBackSizePtr ptr)
{
m_CallBacSizeFunc = ptr;
}