Repository navigation
Expand file tree
/
Copy pathtutorial01.c
More file actions
245 lines (208 loc) · 5.97 KB
/
Copy pathtutorial01.c
File metadata and controls
245 lines (208 loc) · 5.97 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// tutorial01.c
//
// This tutorial was written by Stephen Dranger (dranger@gmail.com).
//
// Code based on a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
// Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1
// A small sample program that shows how to use libavformat and libavcodec to
// read video from a file.
//
// Use the Makefile to build all examples.
//
// Run using
//
// tutorial01 myvideofile.mpg
//
// to write the first five frames from "myvideofile.mpg" to disk in PPM
// format.
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <stdio.h>
void saveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFilename[32];
int y;
// Open file
sprintf(szFilename, "frame%d.ppm", iFrame);
pFile = fopen(szFilename, "wb");
if(pFile == NULL)
{
fprintf(stderr, "Cannot open file");
return;
}
// Write header
fprintf(pFile, "P6\n%d %d\n%d\n", width, height, 255);
// Write pixel data
for(y = 0; y < height; y++)
fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, width * 3, pFile);
// Close file
fclose(pFile);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Please provide a movie file\n");
return -1;
}
// Register all formats and codecs
// Now not useful anymore since version 4.0
//av_register_all();
// Open video file
AVFormatContext *pFormatCtx = avformat_alloc_context();
if (!pFormatCtx) {
fprintf(stderr, "Could not allocate memory for format context");
return -1;
}
if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0)
{
fprintf(stderr, "Cannot open file");
return -1; // Couldn't open file
}
// Retrieve stream information
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
fprintf(stderr, "Could not find stream information");
return -1; // Couldn't find stream information
}
AVCodec *pCodec = NULL;
AVCodecParameters *pCodecParams = NULL;
// Find the first video stream
int videoStream = -1;
int i;
for (i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
pCodecParams = pFormatCtx->streams[i]->codecpar;
pCodec = avcodec_find_decoder(pCodecParams->codec_id);
break; // We want only the first video stream, the leave the other stream that might be present in the file
}
}
if (videoStream == -1)
{
fprintf(stderr, "Did not find a video stream");
return -1;
}
// Get a pointer to the codec context for the video stream
AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec);
if (pCodecCtx == NULL)
{
fprintf(stderr, "Could not allocate video codec context");
return -1;
}
// Fill the codec context from the codec parameters values
if (avcodec_parameters_to_context(pCodecCtx, pCodecParams) < 0)
{
fprintf(stderr, "Failed to copy codec params to codec context");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
fprintf(stderr, "failed to open codec");
return -1;
}
// Allocate video frame
AVFrame *pFrame = av_frame_alloc();
if (pFrame == NULL)
{
fprintf(stderr, "Could not allocate video frame");
return -1;
}
AVPacket *pPacket = av_packet_alloc();
if (pPacket == NULL)
{
fprintf(stderr, "Failed to allocate memory for AVPacket");
return -1;
}
// Allocate an AVFrame structure
AVFrame *pFrameRGB = av_frame_alloc();
if(pFrameRGB == NULL)
{
fprintf(stderr, "Could not allocate output video frame");
return -1;
}
struct SwsContext *swsCtx =
sws_getContext
(
pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_RGB24,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
int frames_to_process = 5;
i = 0;
// Read frames and save first five frames to disk
while (av_read_frame(pFormatCtx, pPacket) >= 0 && i < frames_to_process)
{
// Is this a packet from the video stream?
if (pPacket->stream_index == videoStream)
{
// Decode video frame
if (avcodec_send_packet(pCodecCtx, pPacket) < 0)
{
fprintf(stderr, "Error while sending packet to decoder.");
return -1;
}
int frameFinished = 1;
// Did we get a video frame?
while (frameFinished >= 0 && i < frames_to_process)
{
frameFinished = avcodec_receive_frame(pCodecCtx, pFrame);
// These two return values are special and mean there is no output
// frame available, but there were no errors during decoding
if (frameFinished == AVERROR(EAGAIN) || frameFinished == AVERROR_EOF)
{
break;
}
else if (frameFinished < 0)
{
fprintf(stderr, "Error during decoding");
return -1;
}
if (frameFinished >= 0)
{
if (av_image_alloc(pFrameRGB->data, pFrameRGB->linesize, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, 1) < 0)
{
fprintf(stderr, "Could not allocate output frame");
return -1;
}
// Convert the image from its native format to RGB
sws_scale
(
swsCtx,
pFrame->data,
pFrame->linesize,
0,
pCodecCtx->height,
pFrameRGB->data,
pFrameRGB->linesize
);
// Save the frame to disk
saveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, ++i);
}
}
}
// Free the packet that was allocated by av_read_frame
av_packet_unref(pPacket);
}
sws_freeContext(swsCtx);
// Free the YUV frame
av_frame_free(&pFrame);
// Free the RGB image
av_frame_free(&pFrameRGB);
// Close the codec
avcodec_free_context(&pCodecCtx);
// Close the video file
avformat_close_input(&pFormatCtx);
return 0;
}