Question

I am getting H.264 images from an IP camera and want to save the encoded images (without decoding). I am using output-example.c from ffmpeg (libavformat/output-example.c) for this purpose. For Saving the raw H.264 image, I do the following:

AVPacket pkt;
av_init_packet(&pkt);
if (c->coded_frame->pts != AV_NOPTS_VALUE)
        pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
if(c->coded_frame->key_frame)
        pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.data= (uint8_t *)ulAddr;//video_outbuf;
pkt.size= out_size;
save_image(pkt.data, out_size);

Where ulAddr is the address pointer to the image and out_size is the image size. Instead of saving the images to a media video file, I want to save the individual images. save_image function simply uses basic fopen and fwrite functions for saving the images. If I decode the frame and then save, everything works fine. But I have problem saving the encoded frames. The encoded frames are saved with a very small size and then they cannot be decoded. Is there anything wrong? I will really appreciate any help in this regard.

Was it helpful?

Solution

H.264 is not a "picture" encoding format, it is a "movie" encoding format. The encoder does not encode each picture individually, it looks at a group of pictures all together and spreads the encoding for any given picture among the pictures in the group.

If you look at a single encoded picture in most cases you'll find that it has references to other pictures that can be before are after it in the encoded stream. A decoder may need to be given several other pictures prior to being able to decode your target picture.

You may need to select a different encoding format that allows you to do what you want, with H.264 you can't. The formats that encode each picture independently of the rest are called "intra-coded".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top