-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (74 loc) · 1.48 KB
/
Copy pathmain.cpp
File metadata and controls
86 lines (74 loc) · 1.48 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
#include <thread>
#include "Decoder.h"
extern "C"
{
#include "libavformat/avformat.h"
};
int main(int argc, char* argv[])
{
//打开输出文件
FILE* YUVFile = fopen("out.yuv", "wb+");
if (YUVFile == nullptr)
{
return -1;
}
//创建解码器对象
ptrDecoder decodeH264 = Decoder::Create(DECODER_ASYNMODEL);
if (nullptr == decodeH264)
{
if (YUVFile)
{
fclose(YUVFile);
}
return -1;
}
//设置解码回调函数
decodeH264->SetDecoderFuncCB([=](ptrFrameData pData, int nWidth, int nHeight, YUVType yuvtype){
fwrite(pData->data(), 1, pData->size(), YUVFile);
});
//打开解码器
if (0 == decodeH264->open(DEC_CODEC_ID_H264))
{
decodeH264->close();
if (YUVFile)
{
fclose(YUVFile);
}
return -1;
}
//打开H264文件
av_register_all();
AVFormatContext *ifmt_ctx = NULL;
if ((avformat_open_input(&ifmt_ctx, "../Media/cuc_ieschool.h264", 0, 0)) < 0)
{
printf("Could not open input file.");
return -1;
}
if ((avformat_find_stream_info(ifmt_ctx, 0)) < 0)
{
printf("Failed to retrieve input stream information");
return -1;
}
//从H264文件中读取H264裸流并进行解码
while (1)
{
AVPacket pkt;
int ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
{
break;
}
ptrFrameData pData = std::make_shared<std::vector<unsigned char> >(pkt.data, pkt.data + pkt.size);
decodeH264->DecoderFrame(pData);
av_packet_unref(&pkt);
}
avformat_close_input(&ifmt_ctx);
getchar();
if (0 == decodeH264->close())
{
return -1;
}
decodeH264.reset();
fclose(YUVFile);
return 0;
}