Merge commit '4baba6c813b7a1f27370e20fb1a87b05fcb39208'
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 16 Oct 2013 10:51:26 +0000 (12:51 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 16 Oct 2013 10:51:26 +0000 (12:51 +0200)
* commit '4baba6c813b7a1f27370e20fb1a87b05fcb39208':
  h264_parser: Fix POC parsing for the case where MMCO_RESET is present.

Conflicts:
libavcodec/h264.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/h264.c
libavcodec/h264.h
libavcodec/h264_parser.c

@@@ -3310,6 -3198,50 +3310,49 @@@ static int h264_slice_header_init(H264C
      return 0;
  }
  
 -    int num_ref_idx_active_override_flag, max_refs;
+ int ff_set_ref_count(H264Context *h)
+ {
 -            if (h->ref_count[0] < 1)
 -                return AVERROR_INVALIDDATA;
++    int num_ref_idx_active_override_flag;
+     // set defaults, might be overridden a few lines later
+     h->ref_count[0] = h->pps.ref_count[0];
+     h->ref_count[1] = h->pps.ref_count[1];
+     if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
++        unsigned max[2];
++        max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
++
+         if (h->slice_type_nos == AV_PICTURE_TYPE_B)
+             h->direct_spatial_mv_pred = get_bits1(&h->gb);
+         num_ref_idx_active_override_flag = get_bits1(&h->gb);
+         if (num_ref_idx_active_override_flag) {
+             h->ref_count[0] = get_ue_golomb(&h->gb) + 1;
 -                if (h->ref_count[1] < 1)
 -                    return AVERROR_INVALIDDATA;
 -            }
+             if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
+                 h->ref_count[1] = get_ue_golomb(&h->gb) + 1;
 -    max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
 -
 -    if (h->ref_count[0] > max_refs || h->ref_count[1] > max_refs) {
 -        av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
 -        h->ref_count[0] = h->ref_count[1] = 0;
 -        return AVERROR_INVALIDDATA;
 -    }
 -
++            } else
++                // full range is spec-ok in this case, even for frames
++                h->ref_count[1] = 1;
++        }
++
++        if (h->ref_count[0]-1 > max[0] || h->ref_count[1]-1 > max[1]){
++            av_log(h->avctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n", h->ref_count[0]-1, max[0], h->ref_count[1]-1, max[1]);
++            h->ref_count[0] = h->ref_count[1] = 0;
++            return AVERROR_INVALIDDATA;
+         }
+         if (h->slice_type_nos == AV_PICTURE_TYPE_B)
+             h->list_count = 2;
+         else
+             h->list_count = 1;
+     } else {
+         h->list_count   = 0;
+         h->ref_count[0] = h->ref_count[1] = 0;
+     }
+     return 0;
+ }
  /**
   * Decode a slice header.
   * This will also call ff_MPV_common_init() and frame_start() as needed.
@@@ -3324,10 -3256,10 +3367,10 @@@ static int decode_slice_header(H264Cont
  {
      unsigned int first_mb_in_slice;
      unsigned int pps_id;
-     int num_ref_idx_active_override_flag, ret;
+     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;
  
      if (h->pps.redundant_pic_cnt_present)
          h->redundant_pic_count = get_ue_golomb(&h->gb);
  
-     // set defaults, might be overridden a few lines later
-     h->ref_count[0] = h->pps.ref_count[0];
-     h->ref_count[1] = h->pps.ref_count[1];
-     if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
-         unsigned max[2];
-         max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
-         if (h->slice_type_nos == AV_PICTURE_TYPE_B)
-             h->direct_spatial_mv_pred = get_bits1(&h->gb);
-         num_ref_idx_active_override_flag = get_bits1(&h->gb);
-         if (num_ref_idx_active_override_flag) {
-             h->ref_count[0] = get_ue_golomb(&h->gb) + 1;
-             if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
-                 h->ref_count[1] = get_ue_golomb(&h->gb) + 1;
-             } else
-                 // full range is spec-ok in this case, even for frames
-                 h->ref_count[1] = 1;
-         }
-         if (h->ref_count[0]-1 > max[0] || h->ref_count[1]-1 > max[1]){
-             av_log(h->avctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n", h->ref_count[0]-1, max[0], h->ref_count[1]-1, max[1]);
-             h->ref_count[0] = h->ref_count[1] = 0;
-             return AVERROR_INVALIDDATA;
-         }
+     ret = ff_set_ref_count(h);
+     if (ret < 0)
+         return ret;
  
-         if (h->slice_type_nos == AV_PICTURE_TYPE_B)
-             h->list_count = 2;
-         else
-             h->list_count = 1;
-     } else {
-         h->list_count   = 0;
-         h->ref_count[0] = h->ref_count[1] = 0;
-     }
 -    if (!default_ref_list_done)
 +    if (slice_type != AV_PICTURE_TYPE_I &&
 +        (h0->current_slice == 0 ||
 +         slice_type != h0->last_slice_type ||
 +         memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
++
          ff_h264_fill_default_ref_list(h);
 +    }
  
      if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
         ret = ff_h264_decode_ref_pic_list_reordering(h);
Simple merge
@@@ -115,11 -84,78 +115,80 @@@ static int h264_find_frame_end(H264Cont
  found:
      pc->state             = 7;
      pc->frame_start_found = 0;
 -    return i - (state & 5);
 +    if (h->is_avc)
 +        return next_avc;
 +    return i - (state & 5) - 3 * (state > 7);
  }
  
+ static int scan_mmco_reset(AVCodecParserContext *s)
+ {
+     H264Context *h = s->priv_data;
+     h->slice_type_nos = s->pict_type & 3;
+     if (h->pps.redundant_pic_cnt_present)
+         get_ue_golomb(&h->gb); // redundant_pic_count
+     if (ff_set_ref_count(h) < 0)
+         return AVERROR_INVALIDDATA;
+     if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
+         int list;
+         for (list = 0; list < h->list_count; list++) {
+             if (get_bits1(&h->gb)) {
+                 int index;
+                 for (index = 0; ; index++) {
+                     unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
+                     if (reordering_of_pic_nums_idc < 3)
+                         get_ue_golomb(&h->gb);
+                     else if (reordering_of_pic_nums_idc > 3) {
+                         av_log(h->avctx, AV_LOG_ERROR,
+                                "illegal reordering_of_pic_nums_idc %d\n",
+                                reordering_of_pic_nums_idc);
+                         return AVERROR_INVALIDDATA;
+                     } else
+                         break;
+                     if (index >= h->ref_count[list]) {
+                         av_log(h->avctx, AV_LOG_ERROR, "reference count overflow\n");
+                         return AVERROR_INVALIDDATA;
+                     }
+                 }
+             }
+         }
+     }
+     if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
+         (h->pps.weighted_bipred_idc == 1 && h->slice_type_nos == AV_PICTURE_TYPE_B))
+         ff_pred_weight_table(h);
+     if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag
+         int i;
+         for (i = 0; i < MAX_MMCO_COUNT; i++) {
+             MMCOOpcode opcode = get_ue_golomb_31(&h->gb);
+             if (opcode > (unsigned) MMCO_LONG) {
+                 av_log(h->avctx, AV_LOG_ERROR,
+                        "illegal memory management control operation %d\n",
+                        opcode);
+                 return AVERROR_INVALIDDATA;
+             }
+             if (opcode == MMCO_END)
+                return 0;
+             else if (opcode == MMCO_RESET)
+                 return 1;
+             if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
+                 get_ue_golomb(&h->gb);
+             if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
+                 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
+                 get_ue_golomb_31(&h->gb);
+         }
+     }
+     return 0;
+ }
  /**
   * Parse NAL units of found picture and decode some basic information.
   *
@@@ -136,9 -172,8 +205,9 @@@ static inline int parse_nal_units(AVCod
      const uint8_t *buf_end = buf + buf_size;
      unsigned int pps_id;
      unsigned int slice_type;
-     int state = -1;
+     int state = -1, got_reset = 0;
      const uint8_t *ptr;
 +    int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
      int field_poc[2];
  
      /* set some sane default values */