annotate apiexample.c @ 808:e9bfaabcf07d libavcodec

fixed nb_block_sizes detection - fixed codec_id test (avctx->codec_id does not need to be initialized)
author bellard
date Thu, 31 Oct 2002 00:07:13 +0000
parents 44a1dab0205c
children 1a5926aeed5f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /* avcodec API use example.
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 * not file formats (avi, vob, etc...). See library 'libav' for the
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * format handling
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 #include <string.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 #include <math.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
11
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
13
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 #define INBUF_SIZE 4096
986e461dc072 Initial revision
glantau
parents:
diff changeset
15
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Audio encoding example
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 void audio_encode_example(const char *filename)
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 AVCodec *codec;
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
22 AVCodecContext *c= NULL;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 int frame_size, i, j, out_size, outbuf_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 FILE *f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 short *samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 float t, tincr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 UINT8 *outbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
28
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 printf("Audio encoding\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
30
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 /* find the MP2 encoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 codec = avcodec_find_encoder(CODEC_ID_MP2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 if (!codec) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 fprintf(stderr, "codec not found\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
37
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
38 c= avcodec_alloc_context();
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
39
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 /* put sample parameters */
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 c->bit_rate = 64000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 c->sample_rate = 44100;
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 c->channels = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
44
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 /* open it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 if (avcodec_open(c, codec) < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 fprintf(stderr, "could not open codec\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
50
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 /* the codec gives us the frame size, in samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 frame_size = c->frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 samples = malloc(frame_size * 2 * c->channels);
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 outbuf_size = 10000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 outbuf = malloc(outbuf_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
56
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 f = fopen(filename, "w");
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 if (!f) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 fprintf(stderr, "could not open %s\n", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
62
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 /* encode a single tone sound */
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 t = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 tincr = 2 * M_PI * 440.0 / c->sample_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 for(i=0;i<200;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 for(j=0;j<frame_size;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 samples[2*j] = (int)(sin(t) * 10000);
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 samples[2*j+1] = samples[2*j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 t += tincr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 /* encode the samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 fwrite(outbuf, 1, out_size, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 free(outbuf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 free(samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
79
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 avcodec_close(c);
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
81 free(c);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
83
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 * Audio decoding.
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 void audio_decode_example(const char *outfilename, const char *filename)
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 AVCodec *codec;
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
90 AVCodecContext *c= NULL;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 int out_size, size, len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 FILE *f, *outfile;
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 UINT8 *outbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 UINT8 inbuf[INBUF_SIZE], *inbuf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 printf("Audio decoding\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
97
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 /* find the mpeg audio decoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 codec = avcodec_find_decoder(CODEC_ID_MP2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 if (!codec) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 fprintf(stderr, "codec not found\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
104
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
105 c= avcodec_alloc_context();
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
106
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 /* open it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 if (avcodec_open(c, codec) < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 fprintf(stderr, "could not open codec\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
112
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
986e461dc072 Initial revision
glantau
parents:
diff changeset
114
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 f = fopen(filename, "r");
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 if (!f) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 fprintf(stderr, "could not open %s\n", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 outfile = fopen(outfilename, "w");
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 if (!outfile) {
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
122 free(c);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
125
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 /* decode until eof */
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 inbuf_ptr = inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 size = fread(inbuf, 1, INBUF_SIZE, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 if (size == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
132
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 inbuf_ptr = inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 while (size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 inbuf_ptr, size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 if (len < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 fprintf(stderr, "Error while decoding\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 if (out_size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 /* if a frame has been decoded, output it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 fwrite(outbuf, 1, out_size, outfile);
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
149
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 fclose(outfile);
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 free(outbuf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
153
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 avcodec_close(c);
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
155 free(c);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
157
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 * Video encoding example
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 void video_encode_example(const char *filename)
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 AVCodec *codec;
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
164 AVCodecContext *c= NULL;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 int i, out_size, size, x, y, outbuf_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 FILE *f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 AVPicture picture;
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 UINT8 *outbuf, *picture_buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
169
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 printf("Video encoding\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
171
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 /* find the mpeg1 video encoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 if (!codec) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 fprintf(stderr, "codec not found\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
178
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
179 c= avcodec_alloc_context();
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
180
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 /* put sample parameters */
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 c->bit_rate = 400000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 /* resolution must be a multiple of two */
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 c->width = 352;
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 c->height = 288;
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 /* frames per second */
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 c->frame_rate = 25 * FRAME_RATE_BASE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 c->gop_size = 10; /* emit one intra frame every ten frames */
986e461dc072 Initial revision
glantau
parents:
diff changeset
189
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 /* open it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 if (avcodec_open(c, codec) < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 fprintf(stderr, "could not open codec\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
195
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 /* the codec gives us the frame size, in samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
197
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 f = fopen(filename, "w");
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 if (!f) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 fprintf(stderr, "could not open %s\n", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
203
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 /* alloc image and output buffer */
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 outbuf_size = 100000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 outbuf = malloc(outbuf_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 size = c->width * c->height;
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
209
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 picture.data[0] = picture_buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 picture.data[1] = picture.data[0] + size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 picture.data[2] = picture.data[1] + size / 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 picture.linesize[0] = c->width;
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 picture.linesize[1] = c->width / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 picture.linesize[2] = c->width / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
216
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 /* encode 1 second of video */
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 for(i=0;i<25;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 printf("encoding frame %3d\r", i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 fflush(stdout);
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 /* prepare a dummy image */
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 /* Y */
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 for(y=0;y<c->height;y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 for(x=0;x<c->width;x++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 picture.data[0][y * picture.linesize[0] + x] = x + y + i * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
228
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 /* Cb and Cr */
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 for(y=0;y<c->height/2;y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 for(x=0;x<c->width/2;x++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 picture.data[1][y * picture.linesize[1] + x] = 128 + y + i * 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 picture.data[2][y * picture.linesize[2] + x] = 64 + x + i * 5;
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
236
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 /* encode the image */
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &picture);
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 fwrite(outbuf, 1, out_size, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
241
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 /* add sequence end code to have a real mpeg file */
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 outbuf[0] = 0x00;
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 outbuf[1] = 0x00;
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 outbuf[2] = 0x01;
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 outbuf[3] = 0xb7;
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 fwrite(outbuf, 1, 4, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 free(picture_buf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 free(outbuf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
251
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 avcodec_close(c);
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
253 free(c);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
256
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 * Video decoding example
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
260
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 FILE *f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
265
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 f=fopen(filename,"w");
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 for(i=0;i<ysize;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 fwrite(buf + i * wrap,1,xsize,f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
272
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 void video_decode_example(const char *outfilename, const char *filename)
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 AVCodec *codec;
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
276 AVCodecContext *c= NULL;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 int frame, size, got_picture, len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 FILE *f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 AVPicture picture;
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 UINT8 inbuf[INBUF_SIZE], *inbuf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 char buf[1024];
986e461dc072 Initial revision
glantau
parents:
diff changeset
282
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 printf("Video decoding\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
284
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 /* find the mpeg1 video decoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 if (!codec) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 fprintf(stderr, "codec not found\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
291
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
292 c= avcodec_alloc_context();
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
293
71
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
294 /* for some codecs, such as msmpeg4 and mpeg4, width and height
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 MUST be initialized there because these info are not available
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 in the bitstream */
986e461dc072 Initial revision
glantau
parents:
diff changeset
297
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 /* open it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 if (avcodec_open(c, codec) < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 fprintf(stderr, "could not open codec\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
303
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 /* the codec gives us the frame size, in samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
305
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 f = fopen(filename, "r");
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 if (!f) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 fprintf(stderr, "could not open %s\n", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
311
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 frame = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 size = fread(inbuf, 1, INBUF_SIZE, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 if (size == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
317
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 and this is the only method to use them because you cannot
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 know the compressed data size before analysing it.
986e461dc072 Initial revision
glantau
parents:
diff changeset
321
71
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
322 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
323 based, so you must call them with all the data for one
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
324 frame exactly. You must also initialize 'width' and
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 'height' before initializing them. */
986e461dc072 Initial revision
glantau
parents:
diff changeset
326
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 /* NOTE2: some codecs allow the raw parameters (frame size,
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 sample rate) to be changed at any frame. We handle this, so
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 you should also take care of it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
330
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 /* here, we use a stream based decoder (mpeg1video), so we
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 feed decoder and see if it could decode a frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 inbuf_ptr = inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 while (size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 len = avcodec_decode_video(c, &picture, &got_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 inbuf_ptr, size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 if (len < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 fprintf(stderr, "Error while decoding frame %d\n", frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 if (got_picture) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 printf("saving frame %3d\r", frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 fflush(stdout);
986e461dc072 Initial revision
glantau
parents:
diff changeset
344
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 /* the picture is allocated by the decoder. no need to
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 free it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 snprintf(buf, sizeof(buf), outfilename, frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 pgm_save(picture.data[0], picture.linesize[0],
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 c->width, c->height, buf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 frame++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
356
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 /* some codecs, such as MPEG, transmit the I and P frame with a
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 latency of one frame. You must do the following to have a
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 chance to get the last frame of the video */
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 len = avcodec_decode_video(c, &picture, &got_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 NULL, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 if (got_picture) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 printf("saving frame %3d\r", frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 fflush(stdout);
986e461dc072 Initial revision
glantau
parents:
diff changeset
365
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 /* the picture is allocated by the decoder. no need to
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 free it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 snprintf(buf, sizeof(buf), outfilename, frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 pgm_save(picture.data[0], picture.linesize[0],
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 c->width, c->height, buf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
371 frame++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
373
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
375
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 avcodec_close(c);
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
377 free(c);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
380
986e461dc072 Initial revision
glantau
parents:
diff changeset
381
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 int main(int argc, char **argv)
986e461dc072 Initial revision
glantau
parents:
diff changeset
383 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 const char *filename;
986e461dc072 Initial revision
glantau
parents:
diff changeset
385
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 /* must be called before using avcodec lib */
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 avcodec_init();
986e461dc072 Initial revision
glantau
parents:
diff changeset
388
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 /* register all the codecs (you can also register only the codec
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 you wish to have smaller code */
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 avcodec_register_all();
986e461dc072 Initial revision
glantau
parents:
diff changeset
392
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 if (argc <= 1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 audio_encode_example("/tmp/test.mp2");
986e461dc072 Initial revision
glantau
parents:
diff changeset
395 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
986e461dc072 Initial revision
glantau
parents:
diff changeset
396
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 video_encode_example("/tmp/test.mpg");
986e461dc072 Initial revision
glantau
parents:
diff changeset
398 filename = "/tmp/test.mpg";
986e461dc072 Initial revision
glantau
parents:
diff changeset
399 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 filename = argv[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
402
986e461dc072 Initial revision
glantau
parents:
diff changeset
403 // audio_decode_example("/tmp/test.sw", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 video_decode_example("/tmp/test%d.pgm", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
405
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 }