使用 libavcodec 保存 H.264 编码图像
我从 IP 摄像机获取 H.264 图像,并希望保存编码图像(不解码)。为此,我使用 ffmpeg (libavformat/output-example.c) 中的 output-example.c 。为了保存原始 H.264 图像,我执行以下操作:
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);
其中 ulAddr 是指向图像的地址指针,out_size 是图像大小。我不想将图像保存到媒体视频文件,而是保存单个图像。 save_image 函数仅使用基本的 fopen 和 fwrite 函数来保存图像。如果我解码帧然后保存,一切正常。但我在保存编码帧时遇到问题。编码后的帧以非常小的尺寸保存,然后无法解码。 有什么问题吗?我将非常感谢在这方面的任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
H.264不是“图片”编码格式,它是“电影”编码格式。编码器不会单独对每个图片进行编码,而是一起查看一组图片,并将任何给定图片的编码分散到该组图片中。
如果您在大多数情况下查看单个编码图片,您会发现它引用了编码流中可能位于其之前或之后的其他图片。解码器可能需要先获得几张其他图片才能解码目标图片。
您可能需要选择不同的编码格式来实现您想要的功能,而 H.264 则不能。独立于其余图像对每个图像进行编码的格式称为“帧内编码”。
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".