forked from ggkollective/FFmpegTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample04_decoding.c
More file actions
198 lines (163 loc) · 4.18 KB
/
sample04_decoding.c
File metadata and controls
198 lines (163 loc) · 4.18 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
196
197
198
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/common.h>
#include <libavutil/avutil.h>
#include <stdio.h>
typedef struct _FileContext
{
AVFormatContext* fmt_ctx;
int v_index;
int a_index;
} FileContext;
static FileContext inputFile;
static int open_decoder(AVCodecContext* codec_ctx)
{
// Find a decoder by codec ID
AVCodec* decoder = avcodec_find_decoder(codec_ctx->codec_id);
if(decoder == NULL)
{
return -1;
}
// Open the codec using decoder
if(avcodec_open2(codec_ctx, decoder, NULL) < 0)
{
return -2;
}
return 0;
}
static int open_input(const char* filename)
{
unsigned int index;
inputFile.fmt_ctx = NULL;
inputFile.a_index = inputFile.v_index = -1;
if(avformat_open_input(&inputFile.fmt_ctx, filename, NULL, NULL) < 0)
{
printf("Could not open input file %s\n", filename);
return -1;
}
if(avformat_find_stream_info(inputFile.fmt_ctx, NULL) < 0)
{
printf("Failed to retrieve input stream information\n");
return -2;
}
for(index = 0; index < inputFile.fmt_ctx->nb_streams; index++)
{
AVCodecContext* codec_ctx = inputFile.fmt_ctx->streams[index]->codec;
if(codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO && inputFile.v_index < 0)
{
if(open_decoder(codec_ctx) < 0)
{
break;
}
inputFile.v_index = index;
}
else if(codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO && inputFile.a_index < 0)
{
if(open_decoder(codec_ctx) < 0)
{
break;
}
inputFile.a_index = index;
}
} // for
if(inputFile.a_index < 0 && inputFile.a_index < 0)
{
printf("Failed to retrieve input stream information\n");
return -3;
}
return 0;
}
static void release()
{
if(inputFile.fmt_ctx != NULL)
{
unsigned int index;
for(index = 0; index < inputFile.fmt_ctx->nb_streams; index++)
{
AVCodecContext* codec_ctx = inputFile.fmt_ctx->streams[index]->codec;
if(index == inputFile.v_index || index == inputFile.a_index)
{
avcodec_close(codec_ctx);
}
}
avformat_close_input(&inputFile.fmt_ctx);
}
}
static int decode_packet(AVCodecContext* codec_ctx, AVPacket* pkt, AVFrame** frame, int* got_frame)
{
int (*decode_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
int decoded_size;
// Decide which is needed for decoding pakcet.
decode_func = (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 : avcodec_decode_audio4;
decoded_size = decode_func(codec_ctx, *frame, got_frame, pkt);
if(*got_frame)
{
// This adjust PTS/DTS automatically in frame.
(*frame)->pts = av_frame_get_best_effort_timestamp(*frame);
}
return decoded_size;
}
int main(int argc, char* argv[])
{
int ret;
av_register_all();
av_log_set_level(AV_LOG_DEBUG);
if(argc < 2)
{
printf("usage : %s <input>\n", argv[0]);
return 0;
}
if(open_input(argv[1]) < 0)
{
goto main_end;
}
// AVFrame is used to store raw frame, which is decoded from packet.
AVFrame* decoded_frame = av_frame_alloc();
if(decoded_frame == NULL) goto main_end;
AVPacket pkt;
int got_frame;
while(1)
{
ret = av_read_frame(inputFile.fmt_ctx, &pkt);
if(ret == AVERROR_EOF)
{
printf("End of frame\n");
break;
}
if(pkt.stream_index != inputFile.v_index &&
pkt.stream_index != inputFile.a_index)
{
av_free_packet(&pkt);
continue;
}
AVStream* avStream = inputFile.fmt_ctx->streams[pkt.stream_index];
AVCodecContext* codec_ctx = avStream->codec;
got_frame = 0;
av_packet_rescale_ts(&pkt, avStream->time_base, codec_ctx->time_base);
ret = decode_packet(codec_ctx, &pkt, &decoded_frame, &got_frame);
if(ret >= 0 && got_frame)
{
printf("-----------------------\n");
if(codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
{
printf("Video : frame->width, height : %dx%d\n",
decoded_frame->width, decoded_frame->height);
printf("Video : frame->sample_aspect_ratio : %d/%d\n",
decoded_frame->sample_aspect_ratio.num, decoded_frame->sample_aspect_ratio.den);
}
else
{
printf("Audio : frame->nb_samples : %d\n",
decoded_frame->nb_samples);
printf("Audio : frame->channels : %d\n",
decoded_frame->channels);
}
av_frame_unref(decoded_frame);
} // if
av_free_packet(&pkt);
} // while
av_frame_free(&decoded_frame);
main_end:
release();
return 0;
}