Merge commit '5ec0bdf2c524224f30ba4786f47324970aed4aaa'
authorMichael Niedermayer <michaelni@gmx.at>
Sat, 27 Jun 2015 19:44:08 +0000 (21:44 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Sat, 27 Jun 2015 20:15:01 +0000 (22:15 +0200)
* commit '5ec0bdf2c524224f30ba4786f47324970aed4aaa':
  h264: do not update the context fields copied between threads after finish_setup()

Conflicts:
libavcodec/h264.h
libavcodec/h264_slice.c

See: f111831ed61103f9fa8fdda41473a23da016bdaa and others
Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/h264.c
libavcodec/h264.h
libavcodec/h264_slice.c

@@@ -1746,28 -1622,15 +1750,29 @@@ static int h264_decode_frame(AVCodecCon
      int ret;
  
      h->flags = avctx->flags;
+     h->setup_finished = 0;
  
 +    if (h->backup_width != -1) {
 +        avctx->width    = h->backup_width;
 +        h->backup_width = -1;
 +    }
 +    if (h->backup_height != -1) {
 +        avctx->height    = h->backup_height;
 +        h->backup_height = -1;
 +    }
 +    if (h->backup_pix_fmt != AV_PIX_FMT_NONE) {
 +        avctx->pix_fmt    = h->backup_pix_fmt;
 +        h->backup_pix_fmt = AV_PIX_FMT_NONE;
 +    }
 +
 +    ff_h264_unref_picture(h, &h->last_pic_for_ec);
 +
      /* end of stream, output what is still in the buffers */
 -out:
      if (buf_size == 0) {
 -        H264Picture *out;
 -        int i, out_idx;
 + out:
  
          h->cur_pic_ptr = NULL;
 +        h->first_field = 0;
  
          // FIXME factorize this with the output code below
          out     = h->delayed_pic[0];
@@@ -778,14 -721,12 +778,20 @@@ typedef struct H264Context 
  
      int frame_recovered;    ///< Initial frame has been completely recovered
  
 -    /* for frame threading, this is set to 1
 +    int has_recovery_point;
 +
 +    int missing_fields;
 +
 +    uint8_t afd;
 +    int has_afd;
 +
 +
++/* for frame threading, this is set to 1
+      * after finish_setup() has been called, so we cannot modify
+      * some context properties (which are supposed to stay constant between
+      * slices) anymore */
+     int setup_finished;
      // Timestamp stuff
      int sei_buffering_period_present;   ///< Buffering period SEI flag
      int initial_cpb_removal_delay[32];  ///< Initial timestamps for CPBs
@@@ -1146,14 -1035,12 +1146,14 @@@ int ff_h264_decode_slice_header(H264Con
      unsigned int pps_id;
      int ret;
      unsigned int slice_type, tmp, i, j;
 -    int default_ref_list_done = 0;
      int last_pic_structure, last_pic_droppable;
 +    int must_reinit;
      int needs_reinit = 0;
      int field_pic_flag, bottom_field_flag;
-     int frame_num, picture_structure, droppable;
 +    int first_slice = sl == h->slice_ctx && !h->current_slice;
 -    int mb_aff_frame = 0;
+     int frame_num, droppable, picture_structure;
 +    int mb_aff_frame, last_mb_aff_frame;
 +    PPS *pps;
  
      h->qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
      h->qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
          return AVERROR_INVALIDDATA;
      }
  
 +    if (
 +        (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
 +        (h->avctx->skip_frame >= AVDISCARD_BIDIR  && sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
 +        (h->avctx->skip_frame >= AVDISCARD_NONINTRA && sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
 +        (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != NAL_IDR_SLICE) ||
 +         h->avctx->skip_frame >= AVDISCARD_ALL) {
 +         return SLICE_SKIPED;
 +     }
 +
      // to make a few old functions happy, it's wrong though
-     h->pict_type = sl->slice_type;
+     if (!h->setup_finished)
+         h->pict_type = sl->slice_type;
  
      pps_id = get_ue_golomb(&sl->gb);
      if (pps_id >= MAX_PPS_COUNT) {
                 h->pps.sps_id);
          return AVERROR_INVALIDDATA;
      }
-     if (first_slice)
 -    if (h->pps.sps_id != h->sps.sps_id ||
 -        h->sps_buffers[h->pps.sps_id]->new) {
 -        h->sps_buffers[h->pps.sps_id]->new = 0;
++    if (first_slice && !h->setup_finished) {
 +        h->pps = *h->pps_buffers[pps_id];
++    } else if (h->setup_finished && h->dequant_coeff_pps != pps_id) {
++        av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
++        return AVERROR_INVALIDDATA;
++    }
 +
 +    if (pps->sps_id != h->sps.sps_id ||
 +        pps->sps_id != h->current_sps_id ||
 +        h->sps_buffers[pps->sps_id]->new) {
 +
 +        if (!first_slice) {
 +            av_log(h->avctx, AV_LOG_ERROR,
 +               "SPS changed in the middle of the frame\n");
 +            return AVERROR_INVALIDDATA;
 +        }
  
          h->sps = *h->sps_buffers[h->pps.sps_id];
  
  
      }
  
-     h->avctx->profile = ff_h264_get_profile(&h->sps);
-     h->avctx->level   = h->sps.level_idc;
-     h->avctx->refs    = h->sps.ref_frame_count;
 +    must_reinit = (h->context_initialized &&
 +                    (   16*h->sps.mb_width != h->avctx->coded_width
 +                     || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
 +                     || h->cur_bit_depth_luma    != h->sps.bit_depth_luma
 +                     || h->cur_chroma_format_idc != h->sps.chroma_format_idc
 +                     || h->mb_width  != h->sps.mb_width
 +                     || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)
 +                    ));
 +    if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
 +        || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
 +        must_reinit = 1;
 +
 +    if (first_slice && av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio))
 +        must_reinit = 1;
 +
-     h->mb_width  = h->sps.mb_width;
-     h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
-     h->mb_num    = h->mb_width * h->mb_height;
-     h->mb_stride = h->mb_width + 1;
+     if (!h->setup_finished) {
+         h->avctx->profile = ff_h264_get_profile(&h->sps);
+         h->avctx->level   = h->sps.level_idc;
+         h->avctx->refs    = h->sps.ref_frame_count;
  
-     h->b_stride = h->mb_width * 4;
 -        if (h->mb_width  != h->sps.mb_width ||
 -            h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag))
 -            needs_reinit = 1;
 -
+         h->mb_width  = h->sps.mb_width;
+         h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
+         h->mb_num    = h->mb_width * h->mb_height;
+         h->mb_stride = h->mb_width + 1;
  
-     h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
+         h->b_stride = h->mb_width * 4;
  
-     h->width  = 16 * h->mb_width;
-     h->height = 16 * h->mb_height;
+         h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
  
-     ret = init_dimensions(h);
-     if (ret < 0)
-         return ret;
+         h->width  = 16 * h->mb_width;
+         h->height = 16 * h->mb_height;
  
-     if (h->sps.video_signal_type_present_flag) {
-         h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
-                                                     : AVCOL_RANGE_MPEG;
-         if (h->sps.colour_description_present_flag) {
-             if (h->avctx->colorspace != h->sps.colorspace)
-                 needs_reinit = 1;
-             h->avctx->color_primaries = h->sps.color_primaries;
-             h->avctx->color_trc       = h->sps.color_trc;
-             h->avctx->colorspace      = h->sps.colorspace;
+         ret = init_dimensions(h);
+         if (ret < 0)
+             return ret;
+         if (h->sps.video_signal_type_present_flag) {
 -            h->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG
 -                : AVCOL_RANGE_MPEG;
++            h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
++                                                        : AVCOL_RANGE_MPEG;
+             if (h->sps.colour_description_present_flag) {
+                 if (h->avctx->colorspace != h->sps.colorspace)
+                     needs_reinit = 1;
+                 h->avctx->color_primaries = h->sps.color_primaries;
+                 h->avctx->color_trc       = h->sps.color_trc;
+                 h->avctx->colorspace      = h->sps.colorspace;
+             }
          }
      }
  
      }
  
      frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
 +    if (!first_slice) {
 +        if (h->frame_num != frame_num) {
 +            av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
 +                   h->frame_num, frame_num);
 +            return AVERROR_INVALIDDATA;
 +        }
 +    }
 +
+     if (!h->setup_finished)
+         h->frame_num = frame_num;
      sl->mb_mbaff       = 0;
 -
 +    mb_aff_frame       = 0;
 +    last_mb_aff_frame  = h->mb_aff_frame;
      last_pic_structure = h->picture_structure;
      last_pic_droppable = h->droppable;
-     droppable          = h->nal_ref_idc == 0;
+     droppable = h->nal_ref_idc == 0;
      if (h->sps.frame_mbs_only_flag) {
          picture_structure = PICT_FRAME;
      } else {
              mb_aff_frame      = h->sps.mb_aff;
          }
      }
 -    if (!h->setup_finished) {
 -        h->droppable         = droppable;
 -        h->picture_structure = picture_structure;
 -        h->mb_aff_frame      = mb_aff_frame;
 -    }
 -    sl->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
 -    if (h->current_slice != 0) {
 +    if (h->current_slice) {
          if (last_pic_structure != picture_structure ||
 -            last_pic_droppable != droppable) {
 +            last_pic_droppable != droppable ||
 +            last_mb_aff_frame  != mb_aff_frame) {
              av_log(h->avctx, AV_LOG_ERROR,
                     "Changing field mode (%d -> %d) between slices is not allowed\n",
                     last_pic_structure, h->picture_structure);
                     h->current_slice + 1);
              return AVERROR_INVALIDDATA;
          }
 -    } else {
 +    }
 +
 +    h->picture_structure = picture_structure;
-     h->droppable         = droppable;
-     h->frame_num         = frame_num;
-     h->mb_aff_frame      = mb_aff_frame;
++    if (!h->setup_finished) {
++        h->droppable         = droppable;
++        h->picture_structure = picture_structure;
++        h->mb_aff_frame      = mb_aff_frame;
++    }
 +    sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
 +
 +    if (h->current_slice == 0) {
          /* Shorten frame num gaps so we don't have to allocate reference
           * frames just to throw them away */
          if (h->frame_num != h->prev_frame_num) {
          } else {
              release_unused_pictures(h, 0);
          }
 +        /* Some macroblocks can be accessed before they're available in case
 +        * of lost slices, MBAFF or threading. */
 +        if (FIELD_PICTURE(h)) {
 +            for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
 +                memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
 +        } else {
 +            memset(h->slice_table, -1,
 +                (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
 +        }
 +        h->last_slice_type = -1;
      }
  
-     h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
+     if (!h->setup_finished)
+         h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
  
 -    assert(h->mb_num == h->mb_width * h->mb_height);
 +    av_assert1(h->mb_num == h->mb_width * h->mb_height);
      if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
          first_mb_in_slice >= h->mb_num) {
          av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");