[problem] avcode_decode_video2 return -1

For the developers that use FFmpeg in their software.
Post Reply
nrson
Posts: 1
Joined: Thu Nov 24, 2011 2:03 am

[problem] avcode_decode_video2 return -1

Post by nrson » Thu Nov 24, 2011 2:37 am

Hi, all

Recently I have been working using ffmpeg source(avcode_decode_video2 sample), but I finds the error in part of decoder.
The error is just like the following codes:

static void video_decode_example(const char *outfilename, const char *filename)
{
AVCodec *codec;
AVCodecContext *c= NULL;
int frame, got_picture, len;
FILE *f, *outf;
AVFrame *picture;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
char buf[1024];
AVPacket avpkt;
int i;

c= avcodec_alloc_context();
av_init_packet(&avpkt);

/* 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);

printf("Video decoding\n");

/* find the h264 video decoder */
codec = avcodec_find_decoder(CODEC_ID_H264);

if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
picture= avcodec_alloc_frame();

if(codec->capabilities&CODEC_CAP_TRUNCATED)
c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames
*/
/* open it */
if (avcodec_open(c, codec) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}

/* the codec gives us the frame size, in samples */

f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "could not open %s\n", filename);
exit(1);
}

outf = fopen("out.yuv","wb");
if(!outf) {
pav_free(c);
exit(1);
}

frame = 0;

for(;;) {
avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
if (avpkt.size == 0)
break;
avpkt.data = inbuf;

while (avpkt.size > 0) {
len = avcode_decode_video2(c, picture, &got_picture, &avpkt);
if (len < 0) {
fprintf(stderr, "Error while decoding frame %d\n", frame);
exit(1);
}
if (got_picture) {
printf("saving frame %3d\n", frame);
fflush(stdout);
for(i=0; i<c->height; i++)
fwrite(picture->data[0] + i * picture->linesize[0], 1, c->width,outf );
for(i=0; i<c->height/2; i++)
fwrite(picture->data[1] + i * picture->linesize[1], 1, c->width/2, outf );
for(i=0; i<c->height/2; i++)
fwrite(picture->data[2] + i * picture->linesize[2], 1, c->width/2, outf );

frame++;
}
avpkt.size -= len;
avpkt.data += len;
}
}

fclose(f);
fclose(outf);

pfnclose(c);
pfnfree(c);
pfnfree(picture);
printf("\n");
}

Firstly av_read_frame read some data from an “a.h264” file, but avcodec_decode_video2 always return -1.
My source is refered the api-example.c.

How to solve the problem?

Thank a lot.

Post Reply