FFmpeg:无法使用 Ffmpeg 库函数解码 H264 编码文件
FFmpeg: Not able to decode H264 encoded file using Ffmpeg library functions
I am trying to decode a H264 encoded attached file using FFmpeg
library functions...
avformat_open_input()
av_read_frame()
avcodec_send_packet()
avcodec_recieve_frame() .
文件:- https://drive.google.com/open?id=1t_0nh-6jm5d3VpWIRVd25wYKNvt4Yjo7
当我使用-
解码它时
ffplay frame1.bin
命令它给我输出图像。
但是当我尝试使用 C 程序解码它时,它在 avcodec_recieve_frame()
上给出了错误。它returns-11.
我用于解码目的的部分代码..
uint8_t *inbuf = (uint8_t*) malloc(sizeof(uint8_t)*(INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE));
uint8_t *data;
size_t data_size;
int ret=0;
/* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
pkt = av_packet_alloc();
if (!pkt)
exit(1);
/* find the video decoder */
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
parser = av_parser_init(codec->id);
if (!parser) {
fprintf(stderr, "parser not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
char *filename = "D:\frame1.bin";
fopen_s(&f, filename, "rb");
printf("\n%s", filename);
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
frame = av_frame_alloc();
frameRGB = av_frame_alloc();
if (!frame || !frameRGB) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
while (!feof(f)) {
//Reading complete data in one go.. INBUF_SIZE=FILE_SIZE
data_size = fread(inbuf, 1, INBUF_SIZE, f);
if (!data_size)
break;
/* use the parser to split the data into frames */
data = inbuf;
while (data_size > 0) {
ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
fprintf(stderr, "Error while parsing\n");
goto even;
}
data += ret;
data_size -= ret;
if (pkt->size)
{
AVCodecContext *dec_ctx = c;
char buf[1024];
int ret_send_pkt, ret_recv_frame;
struct SwsContext *sws_ctx = NULL;
ret_send_pkt = avcodec_send_packet(dec_ctx, pkt);
if (ret_send_pkt < 0) {
fprintf(stderr, "Error sending a packet for decoding\n");
//exit(1);
goto even;
}
while (ret_send_pkt >= 0) {
ret_recv_frame = avcodec_receive_frame(dec_ctx, frame);
if (ret_recv_frame == AVERROR(EAGAIN) || ret_recv_frame == AVERROR_EOF)
{
//fprintf(stderr, "Error during decoding11\n");
goto even;
}
else if (ret_recv_frame < 0) {
fprintf(stderr, "Error during decoding\n");
//exit(1);
goto even;
}
}
even: ;
}
}
}
-11 是 EAGAIN。这意味着它需要更多数据来解码帧。继续发送数据包,直到返回一个帧。然后通过发送空数据包在最后刷新。
I am trying to decode a H264 encoded attached file using FFmpeg library functions...
avformat_open_input()
av_read_frame()
avcodec_send_packet()
avcodec_recieve_frame() .
文件:- https://drive.google.com/open?id=1t_0nh-6jm5d3VpWIRVd25wYKNvt4Yjo7 当我使用-
解码它时ffplay frame1.bin
命令它给我输出图像。
但是当我尝试使用 C 程序解码它时,它在 avcodec_recieve_frame()
上给出了错误。它returns-11.
我用于解码目的的部分代码..
uint8_t *inbuf = (uint8_t*) malloc(sizeof(uint8_t)*(INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE));
uint8_t *data;
size_t data_size;
int ret=0;
/* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
pkt = av_packet_alloc();
if (!pkt)
exit(1);
/* find the video decoder */
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
parser = av_parser_init(codec->id);
if (!parser) {
fprintf(stderr, "parser not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
char *filename = "D:\frame1.bin";
fopen_s(&f, filename, "rb");
printf("\n%s", filename);
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
frame = av_frame_alloc();
frameRGB = av_frame_alloc();
if (!frame || !frameRGB) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
while (!feof(f)) {
//Reading complete data in one go.. INBUF_SIZE=FILE_SIZE
data_size = fread(inbuf, 1, INBUF_SIZE, f);
if (!data_size)
break;
/* use the parser to split the data into frames */
data = inbuf;
while (data_size > 0) {
ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
fprintf(stderr, "Error while parsing\n");
goto even;
}
data += ret;
data_size -= ret;
if (pkt->size)
{
AVCodecContext *dec_ctx = c;
char buf[1024];
int ret_send_pkt, ret_recv_frame;
struct SwsContext *sws_ctx = NULL;
ret_send_pkt = avcodec_send_packet(dec_ctx, pkt);
if (ret_send_pkt < 0) {
fprintf(stderr, "Error sending a packet for decoding\n");
//exit(1);
goto even;
}
while (ret_send_pkt >= 0) {
ret_recv_frame = avcodec_receive_frame(dec_ctx, frame);
if (ret_recv_frame == AVERROR(EAGAIN) || ret_recv_frame == AVERROR_EOF)
{
//fprintf(stderr, "Error during decoding11\n");
goto even;
}
else if (ret_recv_frame < 0) {
fprintf(stderr, "Error during decoding\n");
//exit(1);
goto even;
}
}
even: ;
}
}
}
-11 是 EAGAIN。这意味着它需要更多数据来解码帧。继续发送数据包,直到返回一个帧。然后通过发送空数据包在最后刷新。