Mercurial > libavcodec.hg
comparison api-example.c @ 9382:621852d53087 libavcodec
Use the new avcodec_decode_* API.
Patch by Thilo Borgmann name.surname AT googlemail.com.
author | stefano |
---|---|
date | Fri, 10 Apr 2009 11:07:52 +0000 |
parents | b225f51903af |
children | b790df0d7046 |
comparison
equal
deleted
inserted
replaced
9381:7dfbd59b04e5 | 9382:621852d53087 |
---|---|
113 */ | 113 */ |
114 static void audio_decode_example(const char *outfilename, const char *filename) | 114 static void audio_decode_example(const char *outfilename, const char *filename) |
115 { | 115 { |
116 AVCodec *codec; | 116 AVCodec *codec; |
117 AVCodecContext *c= NULL; | 117 AVCodecContext *c= NULL; |
118 int out_size, size, len; | 118 int out_size, len; |
119 FILE *f, *outfile; | 119 FILE *f, *outfile; |
120 uint8_t *outbuf; | 120 uint8_t *outbuf; |
121 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; | 121 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
122 AVPacket avpkt; | |
123 | |
124 av_init_packet(&avpkt); | |
122 | 125 |
123 printf("Audio decoding\n"); | 126 printf("Audio decoding\n"); |
124 | 127 |
125 /* find the mpeg audio decoder */ | 128 /* find the mpeg audio decoder */ |
126 codec = avcodec_find_decoder(CODEC_ID_MP2); | 129 codec = avcodec_find_decoder(CODEC_ID_MP2); |
149 av_free(c); | 152 av_free(c); |
150 exit(1); | 153 exit(1); |
151 } | 154 } |
152 | 155 |
153 /* decode until eof */ | 156 /* decode until eof */ |
154 inbuf_ptr = inbuf; | 157 avpkt.data = inbuf; |
155 for(;;) { | 158 for(;;) { |
156 size = fread(inbuf, 1, INBUF_SIZE, f); | 159 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); |
157 if (size == 0) | 160 if (avpkt.size == 0) |
158 break; | 161 break; |
159 | 162 |
160 inbuf_ptr = inbuf; | 163 avpkt.data = inbuf; |
161 while (size > 0) { | 164 while (avpkt.size > 0) { |
162 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; | 165 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
163 len = avcodec_decode_audio2(c, (short *)outbuf, &out_size, | 166 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); |
164 inbuf_ptr, size); | |
165 if (len < 0) { | 167 if (len < 0) { |
166 fprintf(stderr, "Error while decoding\n"); | 168 fprintf(stderr, "Error while decoding\n"); |
167 exit(1); | 169 exit(1); |
168 } | 170 } |
169 if (out_size > 0) { | 171 if (out_size > 0) { |
170 /* if a frame has been decoded, output it */ | 172 /* if a frame has been decoded, output it */ |
171 fwrite(outbuf, 1, out_size, outfile); | 173 fwrite(outbuf, 1, out_size, outfile); |
172 } | 174 } |
173 size -= len; | 175 avpkt.size -= len; |
174 inbuf_ptr += len; | 176 avpkt.data += len; |
175 } | 177 } |
176 } | 178 } |
177 | 179 |
178 fclose(outfile); | 180 fclose(outfile); |
179 fclose(f); | 181 fclose(f); |
312 | 314 |
313 static void video_decode_example(const char *outfilename, const char *filename) | 315 static void video_decode_example(const char *outfilename, const char *filename) |
314 { | 316 { |
315 AVCodec *codec; | 317 AVCodec *codec; |
316 AVCodecContext *c= NULL; | 318 AVCodecContext *c= NULL; |
317 int frame, size, got_picture, len; | 319 int frame, got_picture, len; |
318 FILE *f; | 320 FILE *f; |
319 AVFrame *picture; | 321 AVFrame *picture; |
320 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; | 322 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
321 char buf[1024]; | 323 char buf[1024]; |
324 AVPacket avpkt; | |
325 | |
326 av_init_packet(&avpkt); | |
322 | 327 |
323 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ | 328 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ |
324 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); | 329 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
325 | 330 |
326 printf("Video decoding\n"); | 331 printf("Video decoding\n"); |
356 exit(1); | 361 exit(1); |
357 } | 362 } |
358 | 363 |
359 frame = 0; | 364 frame = 0; |
360 for(;;) { | 365 for(;;) { |
361 size = fread(inbuf, 1, INBUF_SIZE, f); | 366 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); |
362 if (size == 0) | 367 if (avpkt.size == 0) |
363 break; | 368 break; |
364 | 369 |
365 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) | 370 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) |
366 and this is the only method to use them because you cannot | 371 and this is the only method to use them because you cannot |
367 know the compressed data size before analysing it. | 372 know the compressed data size before analysing it. |
375 sample rate) to be changed at any frame. We handle this, so | 380 sample rate) to be changed at any frame. We handle this, so |
376 you should also take care of it */ | 381 you should also take care of it */ |
377 | 382 |
378 /* here, we use a stream based decoder (mpeg1video), so we | 383 /* here, we use a stream based decoder (mpeg1video), so we |
379 feed decoder and see if it could decode a frame */ | 384 feed decoder and see if it could decode a frame */ |
380 inbuf_ptr = inbuf; | 385 avpkt.data = inbuf; |
381 while (size > 0) { | 386 while (avpkt.size > 0) { |
382 len = avcodec_decode_video(c, picture, &got_picture, | 387 len = avcodec_decode_video2(c, picture, &got_picture, &avpkt); |
383 inbuf_ptr, size); | |
384 if (len < 0) { | 388 if (len < 0) { |
385 fprintf(stderr, "Error while decoding frame %d\n", frame); | 389 fprintf(stderr, "Error while decoding frame %d\n", frame); |
386 exit(1); | 390 exit(1); |
387 } | 391 } |
388 if (got_picture) { | 392 if (got_picture) { |
394 snprintf(buf, sizeof(buf), outfilename, frame); | 398 snprintf(buf, sizeof(buf), outfilename, frame); |
395 pgm_save(picture->data[0], picture->linesize[0], | 399 pgm_save(picture->data[0], picture->linesize[0], |
396 c->width, c->height, buf); | 400 c->width, c->height, buf); |
397 frame++; | 401 frame++; |
398 } | 402 } |
399 size -= len; | 403 avpkt.size -= len; |
400 inbuf_ptr += len; | 404 avpkt.data += len; |
401 } | 405 } |
402 } | 406 } |
403 | 407 |
404 /* some codecs, such as MPEG, transmit the I and P frame with a | 408 /* some codecs, such as MPEG, transmit the I and P frame with a |
405 latency of one frame. You must do the following to have a | 409 latency of one frame. You must do the following to have a |
406 chance to get the last frame of the video */ | 410 chance to get the last frame of the video */ |
407 len = avcodec_decode_video(c, picture, &got_picture, | 411 avpkt.data = NULL; |
408 NULL, 0); | 412 avpkt.size = 0; |
413 len = avcodec_decode_video2(c, picture, &got_picture, &avpkt); | |
409 if (got_picture) { | 414 if (got_picture) { |
410 printf("saving last frame %3d\n", frame); | 415 printf("saving last frame %3d\n", frame); |
411 fflush(stdout); | 416 fflush(stdout); |
412 | 417 |
413 /* the picture is allocated by the decoder. no need to | 418 /* the picture is allocated by the decoder. no need to |