Commit 4f313925 authored by Thilo Borgmann's avatar Thilo Borgmann Committed by Stefano Sabatini

Use the new avcodec_decode_* API.

Patch by Thilo Borgmann name.surname AT googlemail.com.

Originally committed as revision 18409 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 655d47c2
...@@ -115,10 +115,13 @@ static void audio_decode_example(const char *outfilename, const char *filename) ...@@ -115,10 +115,13 @@ static void audio_decode_example(const char *outfilename, const char *filename)
{ {
AVCodec *codec; AVCodec *codec;
AVCodecContext *c= NULL; AVCodecContext *c= NULL;
int out_size, size, len; int out_size, len;
FILE *f, *outfile; FILE *f, *outfile;
uint8_t *outbuf; uint8_t *outbuf;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
AVPacket avpkt;
av_init_packet(&avpkt);
printf("Audio decoding\n"); printf("Audio decoding\n");
...@@ -151,17 +154,16 @@ static void audio_decode_example(const char *outfilename, const char *filename) ...@@ -151,17 +154,16 @@ static void audio_decode_example(const char *outfilename, const char *filename)
} }
/* decode until eof */ /* decode until eof */
inbuf_ptr = inbuf; avpkt.data = inbuf;
for(;;) { for(;;) {
size = fread(inbuf, 1, INBUF_SIZE, f); avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
if (size == 0) if (avpkt.size == 0)
break; break;
inbuf_ptr = inbuf; avpkt.data = inbuf;
while (size > 0) { while (avpkt.size > 0) {
out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
len = avcodec_decode_audio2(c, (short *)outbuf, &out_size, len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
inbuf_ptr, size);
if (len < 0) { if (len < 0) {
fprintf(stderr, "Error while decoding\n"); fprintf(stderr, "Error while decoding\n");
exit(1); exit(1);
...@@ -170,8 +172,8 @@ static void audio_decode_example(const char *outfilename, const char *filename) ...@@ -170,8 +172,8 @@ static void audio_decode_example(const char *outfilename, const char *filename)
/* if a frame has been decoded, output it */ /* if a frame has been decoded, output it */
fwrite(outbuf, 1, out_size, outfile); fwrite(outbuf, 1, out_size, outfile);
} }
size -= len; avpkt.size -= len;
inbuf_ptr += len; avpkt.data += len;
} }
} }
...@@ -314,11 +316,14 @@ static void video_decode_example(const char *outfilename, const char *filename) ...@@ -314,11 +316,14 @@ static void video_decode_example(const char *outfilename, const char *filename)
{ {
AVCodec *codec; AVCodec *codec;
AVCodecContext *c= NULL; AVCodecContext *c= NULL;
int frame, size, got_picture, len; int frame, got_picture, len;
FILE *f; FILE *f;
AVFrame *picture; AVFrame *picture;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
char buf[1024]; char buf[1024];
AVPacket avpkt;
av_init_packet(&avpkt);
/* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
...@@ -358,8 +363,8 @@ static void video_decode_example(const char *outfilename, const char *filename) ...@@ -358,8 +363,8 @@ static void video_decode_example(const char *outfilename, const char *filename)
frame = 0; frame = 0;
for(;;) { for(;;) {
size = fread(inbuf, 1, INBUF_SIZE, f); avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
if (size == 0) if (avpkt.size == 0)
break; break;
/* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
...@@ -377,10 +382,9 @@ static void video_decode_example(const char *outfilename, const char *filename) ...@@ -377,10 +382,9 @@ static void video_decode_example(const char *outfilename, const char *filename)
/* here, we use a stream based decoder (mpeg1video), so we /* here, we use a stream based decoder (mpeg1video), so we
feed decoder and see if it could decode a frame */ feed decoder and see if it could decode a frame */
inbuf_ptr = inbuf; avpkt.data = inbuf;
while (size > 0) { while (avpkt.size > 0) {
len = avcodec_decode_video(c, picture, &got_picture, len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
inbuf_ptr, size);
if (len < 0) { if (len < 0) {
fprintf(stderr, "Error while decoding frame %d\n", frame); fprintf(stderr, "Error while decoding frame %d\n", frame);
exit(1); exit(1);
...@@ -396,16 +400,17 @@ static void video_decode_example(const char *outfilename, const char *filename) ...@@ -396,16 +400,17 @@ static void video_decode_example(const char *outfilename, const char *filename)
c->width, c->height, buf); c->width, c->height, buf);
frame++; frame++;
} }
size -= len; avpkt.size -= len;
inbuf_ptr += len; avpkt.data += len;
} }
} }
/* some codecs, such as MPEG, transmit the I and P frame with a /* some codecs, such as MPEG, transmit the I and P frame with a
latency of one frame. You must do the following to have a latency of one frame. You must do the following to have a
chance to get the last frame of the video */ chance to get the last frame of the video */
len = avcodec_decode_video(c, picture, &got_picture, avpkt.data = NULL;
NULL, 0); avpkt.size = 0;
len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
if (got_picture) { if (got_picture) {
printf("saving last frame %3d\n", frame); printf("saving last frame %3d\n", frame);
fflush(stdout); fflush(stdout);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment