-
Notifications
You must be signed in to change notification settings - Fork 4
/
h26xdec.cpp
115 lines (96 loc) · 2.84 KB
/
h26xdec.cpp
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
};
int main(int argc, char **argv)
{
av_register_all();
AVFrame* frame = avcodec_alloc_frame();
if (!frame)
{
return 1;
}
AVFormatContext* formatContext = NULL;
if (avformat_open_input(&formatContext, argv[1], NULL, NULL) != 0)
{
av_free(frame);
return 1;
}
if (avformat_find_stream_info(formatContext, NULL) < 0)
{
av_free(frame);
avformat_close_input(&formatContext);
return 1;
}
if (formatContext->nb_streams < 1 ||
formatContext->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
{
av_free(frame);
avformat_close_input(&formatContext);
return 1;
}
AVStream* stream = formatContext->streams[0];
AVCodecContext* codecContext = stream->codec;
codecContext->codec = avcodec_find_decoder(codecContext->codec_id);
if (codecContext->codec == NULL)
{
av_free(frame);
avcodec_close(codecContext);
avformat_close_input(&formatContext);
return 1;
}
else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
{
av_free(frame);
avcodec_close(codecContext);
avformat_close_input(&formatContext);
return 1;
}
AVPacket packet;
av_init_packet(&packet);
int frameNumber = 0;
while (av_read_frame(formatContext, &packet) == 0)
{
if (packet.stream_index == stream->index)
{
int frameFinished = 0;
avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
if (frameFinished)
{
saveFrame(frame, codecContext->width, codecContext->height, frameNumber++,argv[1]);
std::cout << frameNumber << ":\trepeat: " << frame->repeat_pict
<< "\tkeyframe: " << frame->key_frame
<< "\tbest_ts: " << frame->best_effort_timestamp << '\n';
}
}
}
int frameFinished = 1;
while(frameFinished){
packet.data = NULL;
packet.size = 0;
int rcode = avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
if(rcode < 0 ){
std::cerr << "[delayed]\tdecide error detected\n";
break;
}
if (frameFinished)
{
saveFrame(frame, codecContext->width, codecContext->height, frameNumber++,argv[1]);
std::cout << frameNumber << ":\trepeat: " << frame->repeat_pict
<< "\tkeyframe: " << frame->key_frame
<< "\tbest_ts: " << frame->best_effort_timestamp << "[delayed]\n";
}
}
av_free_packet(&packet);
av_free(frame);
avcodec_close(codecContext);
avformat_close_input(&formatContext);
return 0;
}