Ticket #8343: 11_fix_h261dec_keyframe.patch

File 11_fix_h261dec_keyframe.patch, 4.0 KB (added by Lastique, 7 years ago)

A patch to fix keyframe markup in h261dec

  • libavcodec/h261.h

    old new  
    3535 * H261Context
    3636 */
    3737typedef struct H261Context {
    3838    MpegEncContext s;
    3939
     40    int freeze_picture_release; // 1 if freeze picture release bit is set in the picture header
    4041    int current_mba;
    4142    int mba_diff;
    4243    int mtype;
    4344    int current_mv_x;
    4445    int current_mv_y;
  • libavcodec/h261dec.c

    old new static int h261_decode_picture_header(H2  
    500500    s->picture_number = (s->picture_number & ~31) + i;
    501501
    502502    s->avctx->framerate = (AVRational) { 30000, 1001 };
    503503
    504504    /* PTYPE starts here */
    505     skip_bits1(&s->gb); /* split screen off */
    506     skip_bits1(&s->gb); /* camera  off */
    507     skip_bits1(&s->gb); /* freeze picture release off */
     505    skip_bits1(&s->gb); /* split screen indicator */
     506    skip_bits1(&s->gb); /* document camera indicator */
     507    h->freeze_picture_release = get_bits1(&s->gb); /* freeze picture release */
    508508
    509509    format = get_bits1(&s->gb);
    510510
    511511    // only 2 formats possible
    512512    if (format == 0) { // QCIF
    static int h261_decode_picture_header(H2  
    530530    if (skip_1stop_8data_bits(&s->gb) < 0)
    531531        return AVERROR_INVALIDDATA;
    532532
    533533    /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
    534534     * frame, the codec crashes if it does not contain all I-blocks
    535      * (e.g. when a packet is lost). */
     535     * (e.g. when a packet is lost). We will fix the picture type in the
     536     * output frame based on h->freeze_picture_release later. */
    536537    s->pict_type = AV_PICTURE_TYPE_P;
    537538
    538539    h->gob_number = 0;
    539540    return 0;
    540541}
    static int h261_decode_frame(AVCodecCont  
    588589    const uint8_t *buf = avpkt->data;
    589590    int buf_size       = avpkt->size;
    590591    H261Context *h     = avctx->priv_data;
    591592    MpegEncContext *s  = &h->s;
    592593    int ret;
     594    enum AVPictureType pict_type;
    593595    AVFrame *pict = data;
    594596
    595597    ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
    596598    ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
    597599
    retry:  
    628630            return ret;
    629631
    630632        goto retry;
    631633    }
    632634
    633     // for skipping the frame
    634     s->current_picture.f->pict_type = s->pict_type;
    635     s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
     635    // for skipping the frame and keyframe markup
     636    pict_type = h->freeze_picture_release ? AV_PICTURE_TYPE_I : s->pict_type;
    636637
    637     if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
    638         (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
     638    if ((avctx->skip_frame >= AVDISCARD_NONREF && pict_type == AV_PICTURE_TYPE_B) ||
     639        (avctx->skip_frame >= AVDISCARD_NONKEY && pict_type != AV_PICTURE_TYPE_I) ||
    639640         avctx->skip_frame >= AVDISCARD_ALL)
    640641        return get_consumed_bytes(s, buf_size);
    641642
     643    s->current_picture.f->pict_type = s->pict_type;
     644    s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
     645
    642646    if (ff_mpv_frame_start(s, avctx) < 0)
    643647        return -1;
    644648
    645649    ff_mpeg_er_frame_start(s);
    646650
    retry:  
    658662    av_assert0(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
    659663    av_assert0(s->current_picture.f->pict_type == s->pict_type);
    660664
    661665    if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
    662666        return ret;
     667
     668    // fix picture type and correctly mark keyframes
     669    pict->pict_type = pict_type;
     670    pict->key_frame = pict_type == AV_PICTURE_TYPE_I;
     671
    663672    ff_print_debug_info(s, s->current_picture_ptr, pict);
    664673
    665674    *got_frame = 1;
    666675
    667676    return get_consumed_bytes(s, buf_size);