user1257043
user1257043

Reputation:

C++ h264 ffmpeg/libav encode/decode(lossless) issues

Insights to encode/decode video with ffmpeg h264 (lossless)

So I got something working on the encoding part, encode an avi in 264 however VLC wont play it, however Totem will. Decoding the same file proves troublesome. (I want the exact same data/frame going in as going out), I get these ;

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

and ultimatly this

[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.

Now I suspect that I have to go back to 1. the encoding part, there is problary a good reason VLC wont play it!

I encode like this.

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);
}
}

And the codec is setup like this:

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

Since I am going for lossless i am not dealing with delayed frames(is this a correct assumption?) I may not actually be encoding lossless, it seems like I may have to go with something like

AVDictionary *param;
av_dict_set(&param, "qp", "0", 0);

And then open...

So I guess me questions is these :

Thanks.

Upvotes: 2

Views: 3242

Answers (2)

Ronald S. Bultje
Ronald S. Bultje

Reputation: 11184

You're writing raw annexb frames into a file without any container wrapping. Use a container like mp4 or matroska and VLC should be happy.

Upvotes: 0

ARK
ARK

Reputation: 699

  1. To achieve lossless: av_dict_set(&param, "crf", "0", 0);
  2. Delayed frames (B-frames) has nothing to do with lossless. If you need low-delay, then don't use B-frames.

Some thing is seriously wrong in your encoding. The error "MV errors in I frame" is odd one here, there shouldn't be any MVs in I-frame. It seems the header parsing it-self gone wrong. Please share the bit-stream & more details for VLC failure

Upvotes: 0

Related Questions