annotate apiexample.c @ 1051:e5a9dbf597d4 libavcodec

mpeg1 bframe encoding patch by (Rapha¸«³l LEGRAND) with some modifications by me
author michaelni
date Sat, 08 Feb 2003 12:00:57 +0000
parents 7fccaa0d699d
children 890b9fb44e84
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;
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 919
diff changeset
167 AVFrame *picture;
0
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();
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 919
diff changeset
180 picture= avcodec_alloc_frame();
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
181
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 /* put sample parameters */
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 c->bit_rate = 400000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 /* resolution must be a multiple of two */
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 c->width = 352;
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 c->height = 288;
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 /* frames per second */
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 c->frame_rate = 25 * FRAME_RATE_BASE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 c->gop_size = 10; /* emit one intra frame every ten frames */
986e461dc072 Initial revision
glantau
parents:
diff changeset
190
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 /* open it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 if (avcodec_open(c, codec) < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 fprintf(stderr, "could not open codec\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
196
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 /* the codec gives us the frame size, in samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
198
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 f = fopen(filename, "w");
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 if (!f) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 fprintf(stderr, "could not open %s\n", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
204
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 /* alloc image and output buffer */
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 outbuf_size = 100000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 outbuf = malloc(outbuf_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 size = c->width * c->height;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
210
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
211 picture->data[0] = picture_buf;
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
212 picture->data[1] = picture->data[0] + size;
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
213 picture->data[2] = picture->data[1] + size / 4;
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
214 picture->linesize[0] = c->width;
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
215 picture->linesize[1] = c->width / 2;
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
216 picture->linesize[2] = c->width / 2;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
217
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 /* encode 1 second of video */
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 for(i=0;i<25;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 printf("encoding frame %3d\r", i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 fflush(stdout);
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 /* prepare a dummy image */
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 /* Y */
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 for(y=0;y<c->height;y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 for(x=0;x<c->width;x++) {
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
226 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
229
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 /* Cb and Cr */
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 for(y=0;y<c->height/2;y++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 for(x=0;x<c->width/2;x++) {
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
233 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
234 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
237
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 /* encode the image */
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
239 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 fwrite(outbuf, 1, out_size, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
242
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 /* add sequence end code to have a real mpeg file */
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 outbuf[0] = 0x00;
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 outbuf[1] = 0x00;
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 outbuf[2] = 0x01;
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 outbuf[3] = 0xb7;
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 fwrite(outbuf, 1, 4, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 free(picture_buf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 free(outbuf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
252
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 avcodec_close(c);
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
254 free(c);
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
255 free(picture);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
258
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 * Video decoding example
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
262
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 FILE *f;
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
267
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 f=fopen(filename,"w");
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 for(i=0;i<ysize;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 fwrite(buf + i * wrap,1,xsize,f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
274
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 void video_decode_example(const char *outfilename, const char *filename)
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 AVCodec *codec;
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
278 AVCodecContext *c= NULL;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 int frame, size, got_picture, len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 FILE *f;
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 919
diff changeset
281 AVFrame *picture;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 UINT8 inbuf[INBUF_SIZE], *inbuf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 char buf[1024];
986e461dc072 Initial revision
glantau
parents:
diff changeset
284
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 printf("Video decoding\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
286
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 /* find the mpeg1 video decoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 if (!codec) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 fprintf(stderr, "codec not found\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
293
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
294 c= avcodec_alloc_context();
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 919
diff changeset
295 picture= avcodec_alloc_frame();
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
296
895
1a5926aeed5f apiexample doesnt send complete frames to the codec
michaelni
parents: 685
diff changeset
297 if(codec->capabilities&CODEC_CAP_TRUNCATED)
1a5926aeed5f apiexample doesnt send complete frames to the codec
michaelni
parents: 685
diff changeset
298 c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
1a5926aeed5f apiexample doesnt send complete frames to the codec
michaelni
parents: 685
diff changeset
299
71
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
300 /* for some codecs, such as msmpeg4 and mpeg4, width and height
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 MUST be initialized there because these info are not available
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 in the bitstream */
986e461dc072 Initial revision
glantau
parents:
diff changeset
303
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 /* open it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 if (avcodec_open(c, codec) < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 fprintf(stderr, "could not open codec\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
309
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 /* the codec gives us the frame size, in samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
311
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 f = fopen(filename, "r");
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 if (!f) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 fprintf(stderr, "could not open %s\n", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
317
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 frame = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 size = fread(inbuf, 1, INBUF_SIZE, f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 if (size == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
323
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 and this is the only method to use them because you cannot
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 know the compressed data size before analysing it.
986e461dc072 Initial revision
glantau
parents:
diff changeset
327
71
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
328 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
329 based, so you must call them with all the data for one
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 0
diff changeset
330 frame exactly. You must also initialize 'width' and
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 'height' before initializing them. */
986e461dc072 Initial revision
glantau
parents:
diff changeset
332
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 /* NOTE2: some codecs allow the raw parameters (frame size,
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 sample rate) to be changed at any frame. We handle this, so
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 you should also take care of it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
336
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 /* here, we use a stream based decoder (mpeg1video), so we
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 feed decoder and see if it could decode a frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 inbuf_ptr = inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 while (size > 0) {
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
341 len = avcodec_decode_video(c, picture, &got_picture,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 inbuf_ptr, size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 if (len < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 fprintf(stderr, "Error while decoding frame %d\n", frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 exit(1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 if (got_picture) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 printf("saving frame %3d\r", frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 fflush(stdout);
986e461dc072 Initial revision
glantau
parents:
diff changeset
350
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 /* the picture is allocated by the decoder. no need to
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 free it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 snprintf(buf, sizeof(buf), outfilename, frame);
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
354 pgm_save(picture->data[0], picture->linesize[0],
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 c->width, c->height, buf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 frame++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
362
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 /* some codecs, such as MPEG, transmit the I and P frame with a
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 latency of one frame. You must do the following to have a
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 chance to get the last frame of the video */
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
366 len = avcodec_decode_video(c, picture, &got_picture,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 NULL, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 if (got_picture) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 printf("saving frame %3d\r", frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 fflush(stdout);
986e461dc072 Initial revision
glantau
parents:
diff changeset
371
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 /* the picture is allocated by the decoder. no need to
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 free it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 snprintf(buf, sizeof(buf), outfilename, frame);
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
375 pgm_save(picture->data[0], picture->linesize[0],
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 c->width, c->height, buf);
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 frame++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
379
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 fclose(f);
986e461dc072 Initial revision
glantau
parents:
diff changeset
381
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 avcodec_close(c);
685
44a1dab0205c fixing apiexample
michaelni
parents: 71
diff changeset
383 free(c);
919
c87eee6da7b1 fixing api-example
michaelni
parents: 895
diff changeset
384 free(picture);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
387
986e461dc072 Initial revision
glantau
parents:
diff changeset
388
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 int main(int argc, char **argv)
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 const char *filename;
986e461dc072 Initial revision
glantau
parents:
diff changeset
392
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 /* must be called before using avcodec lib */
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 avcodec_init();
986e461dc072 Initial revision
glantau
parents:
diff changeset
395
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 /* register all the codecs (you can also register only the codec
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 you wish to have smaller code */
986e461dc072 Initial revision
glantau
parents:
diff changeset
398 avcodec_register_all();
986e461dc072 Initial revision
glantau
parents:
diff changeset
399
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 if (argc <= 1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 audio_encode_example("/tmp/test.mp2");
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
986e461dc072 Initial revision
glantau
parents:
diff changeset
403
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 video_encode_example("/tmp/test.mpg");
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 filename = "/tmp/test.mpg";
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 filename = argv[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
409
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 // audio_decode_example("/tmp/test.sw", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 video_decode_example("/tmp/test%d.pgm", filename);
986e461dc072 Initial revision
glantau
parents:
diff changeset
412
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 }