C ++ h264 ffmpeg / libav编码/解码(无损)问题

时间:2016-07-07 21:40:54

标签: c++ ffmpeg h.264 libav

使用ffmpeg h264(无损)编码/解码视频的见解

所以我在编码部分工作了一些东西,在264编码avi然而VLC不会播放它,但Totem会。 解码相同的文件证明很麻烦。 (我想要完全相同的数据/帧进出),我得到了这些;

saving frame   5
Video decoding
[h264 @ 0x1d19880] decode_slice_header error
frame :6
saving frame   6
Video decoding
[h264 @ 0x1d19880] error while decoding MB 15 7, bytestream -27
[h264 @ 0x1d19880] concealing 194 DC, 194 AC, 194 MV errors in I frame
frame :7
saving frame   7
Video decoding
[h264 @ 0x1d19880] decode_slice_header error

最后这个

[H264 Decoder @ 0x7f1320766040] frame :11
Broken frame packetizing
[h264 @ 0x1d19880] SPS changed in the middle of the frame
[h264 @ 0x1d19880] decode_slice_header error
[h264 @ 0x1d19880] no frame!
Error while decoding frame 11

GAME OVER。

现在我怀疑我必须回到1.编码部分,有一个很好的理由VLC不会播放它!

我这样编码。

void encode(char *Y,char *U,char *V){
av_init_packet(&pkt);
pkt.data = NULL;    // packet data will be allocated by the encoder
pkt.size = 0;
fflush(stdout);

frame->data[0] = (uint8_t*)Y;
frame->data[1] = (uint8_t*)U;
frame->data[2] = (uint8_t*)V;
frame->pts = ++i;

ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
if (ret < 0) {
    fprintf(stderr, "Error encoding frame\n");
    exit (EXIT_FAILURE);
}
if (got_output) {
    printf("Write frame %3d (size=%5d)\n", i, pkt.size);
    fwrite(pkt.data, 1, pkt.size, f);
    av_free_packet(&pkt);
}
}

编解码器设置如下:

AVCodecID dasd = AV_CODEC_ID_H264;
codec = avcodec_find_encoder(dasd);
c = avcodec_alloc_context3(codec);
c->bit_rate = 400000;
c->width = 320;
c->height = 240;
c->time_base= (AVRational){1,25};
c->gop_size = 10; 
c->max_b_frames=1;
c->pix_fmt = AV_PIX_FMT_YUV420P;
av_opt_set(c->priv_data, "preset", "slow", 0);
avcodec_open2(c, codec, NULL);

因为我要无损,我不处理延迟帧(这是正确的假设吗?) 我可能实际上没有编码无损,似乎我可能不得不使用像

这样的东西
AVDictionary *param;
av_dict_set(&param, "qp", "0", 0);

然后打开......

所以我想我的问题是这些:

  • 无损编码的正确编解码器参数是什么(如果h264在这方面是一个糟糕的主意,建议使用。)
  • 去无损时我是否需要处理延迟帧?
  • 为什么VLC对我很生气?

感谢。

2 个答案:

答案 0 :(得分:0)

  1. 实现无损:av_dict_set(&amp; param,“crf”,“0”,0);
  2. 延迟帧(B帧)与无损帧无关。如果您需要低延迟,则不要使用B帧。
  3. 你的编码有些严重错误。错误“I帧中的MV错误”在这里是奇数,I帧中不应该有任何MV。似乎解析它的标题 - 自己出错了。请分享比特流&amp;有关VLC故障的更多详细信息

答案 1 :(得分:0)

您将原始附件框架写入文件而没有任何容器包装。使用像mp4或matroska这样的容器,VLC应该很开心。