annotate apiexample.c @ 2660:e26dc8cf7069 libavcodec

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