1 /*
2 * Copyright (c) 2001 Fabrice Bellard
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 /**
24 * @file libavcodec video decoding API usage example
25 * @example decode_video.c *
26 *
27 * Read from an MPEG1 video file, decode frames, and generate PGM images as
28 * output.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
36
37 #define INBUF_SIZE 4096
38
39 static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
40 char *filename)
41 {
44
45 f = fopen(filename,
"wb");
46 fprintf(
f,
"P5\n%d %d\n%d\n", xsize, ysize, 255);
47 for (
i = 0;
i < ysize;
i++)
48 fwrite(buf +
i *
wrap, 1, xsize,
f);
50 }
51
53 const char *filename)
54 {
55 char buf[1024];
57
60 fprintf(stderr, "Error sending a packet for decoding\n");
61 exit(1);
62 }
63
67 return;
69 fprintf(stderr, "Error during decoding\n");
70 exit(1);
71 }
72
74 fflush(stdout);
75
76 /* the picture is allocated by the decoder. no need to
77 free it */
81 }
82 }
83
84 int main(
int argc,
char **argv)
85 {
86 const char *filename, *outfilename;
94 size_t data_size;
96 int eof;
98
99 if (argc <= 2) {
100 fprintf(stderr, "Usage: %s <input file> <output file>\n"
101 "And check your input file is encoded by mpeg1video please.\n", argv[0]);
102 exit(0);
103 }
104 filename = argv[1];
105 outfilename = argv[2];
106
109 exit(1);
110
111 /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
113
114 /* find the MPEG-1 video decoder */
116 if (!codec) {
117 fprintf(stderr, "Codec not found\n");
118 exit(1);
119 }
120
122 if (!parser) {
123 fprintf(stderr, "parser not found\n");
124 exit(1);
125 }
126
129 fprintf(stderr, "Could not allocate video codec context\n");
130 exit(1);
131 }
132
133 /* For some codecs, such as msmpeg4 and mpeg4, width and height
134 MUST be initialized there because this information is not
135 available in the bitstream. */
136
137 /* open it */
139 fprintf(stderr, "Could not open codec\n");
140 exit(1);
141 }
142
143 f = fopen(filename,
"rb");
145 fprintf(stderr, "Could not open %s\n", filename);
146 exit(1);
147 }
148
151 fprintf(stderr, "Could not allocate video frame\n");
152 exit(1);
153 }
154
155 do {
156 /* read raw data from the input file */
159 break;
160 eof = !data_size;
161
162 /* use the parser to split the data into frames */
164 while (data_size > 0 || eof) {
168 fprintf(stderr, "Error while parsing\n");
169 exit(1);
170 }
173
176 else if (eof)
177 break;
178 }
179 } while (!eof);
180
181 /* flush the decoder */
183
185
190
191 return 0;
192 }