---
 libavcodec/h264.c        | 89 +++++++++++++++++++++++++--------------------
 libavcodec/h264.h        |  2 +
 libavcodec/h264_parser.c | 95 +++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 141 insertions(+), 45 deletions(-)

diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index 832b5c7..8975f89 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -2552,7 +2552,7 @@ void ff_h264_hl_decode_mb(H264Context *h)
         hl_decode_mb_simple_8(h);
 }
 
-static int pred_weight_table(H264Context *h)
+int ff_pred_weight_table(H264Context *h)
 {
     int list, i;
     int luma_def, chroma_def;
@@ -3196,6 +3196,50 @@ static int h264_slice_header_init(H264Context *h, int 
reinit)
     return 0;
 }
 
+int ff_set_ref_count(H264Context *h)
+{
+    int num_ref_idx_active_override_flag, max_refs;
+
+    // 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) {
+        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[0] < 1)
+                return AVERROR_INVALIDDATA;
+            if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
+                h->ref_count[1] = 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->list_count = 2;
+        else
+            h->list_count = 1;
+    } else {
+        h->list_count   = 0;
+        h->ref_count[0] = h->ref_count[1] = 0;
+    }
+
+    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;
+    }
+
+    return 0;
+}
+
 /**
  * Decode a slice header.
  * This will also call ff_MPV_common_init() and frame_start() as needed.
@@ -3210,7 +3254,7 @@ static int decode_slice_header(H264Context *h, 
H264Context *h0)
 {
     unsigned int first_mb_in_slice;
     unsigned int pps_id;
-    int num_ref_idx_active_override_flag, max_refs, ret;
+    int ret;
     unsigned int slice_type, tmp, i, j;
     int default_ref_list_done = 0;
     int last_pic_structure, last_pic_droppable;
@@ -3617,42 +3661,9 @@ static int decode_slice_header(H264Context *h, 
H264Context *h0)
     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) {
-        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[0] < 1)
-                return AVERROR_INVALIDDATA;
-            if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
-                h->ref_count[1] = 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->list_count = 2;
-        else
-            h->list_count = 1;
-    } else {
-        h->list_count   = 0;
-        h->ref_count[0] = h->ref_count[1] = 0;
-    }
-
-    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;
-    }
+    ret = ff_set_ref_count(h);
+    if (ret < 0)
+        return ret;
 
     if (!default_ref_list_done)
         ff_h264_fill_default_ref_list(h);
@@ -3668,7 +3679,7 @@ static int decode_slice_header(H264Context *h, 
H264Context *h0)
     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))
-        pred_weight_table(h);
+        ff_pred_weight_table(h);
     else if (h->pps.weighted_bipred_idc == 2 &&
              h->slice_type_nos == AV_PICTURE_TYPE_B) {
         implicit_weight_table(h, -1);
diff --git a/libavcodec/h264.h b/libavcodec/h264.h
index 3ef8420..2fead60 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -950,5 +950,7 @@ static av_always_inline int get_dct8x8_allowed(H264Context 
*h)
 
 void ff_h264_draw_horiz_band(H264Context *h, int y, int height);
 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc);
+int ff_pred_weight_table(H264Context *h);
+int ff_set_ref_count(H264Context *h);
 
 #endif /* AVCODEC_H264_H */
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 75e09f4..6876a50 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -106,6 +106,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
     int state = -1;
     const uint8_t *ptr;
     int field_poc[2];
+    MMCOOpcode opcode;
 
     /* set some sane default values */
     s->pict_type         = AV_PICTURE_TYPE_I;
@@ -132,8 +133,16 @@ static inline int parse_nal_units(AVCodecParserContext *s,
         case NAL_SLICE:
         case NAL_IDR_SLICE:
             // Do not walk the whole buffer just to decode slice header
-            if (src_length > 60)
-                src_length = 60;
+            if (state & 0x1f == NAL_IDR_SLICE || (state >> 5) & 0x3 == 0) {
+                /* IDR or disposable slice
+                 * No need to decode many bytes because MMCOs shall not be 
present. */
+                if (src_length > 60)
+                    src_length = 60;
+            } else {
+                /* To decode up to MMCOs */
+                if (src_length > 1000)
+                    src_length = 1000;
+            }
             break;
         }
         ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
@@ -219,16 +228,90 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                     h->delta_poc[1] = get_se_golomb(&h->gb);
             }
 
-            /* Decode POC of this picture. */
+            /* Decode POC of this picture.
+             * The prev_ values needed for decoding POC of the next picture 
are not set here. */
             field_poc[0] = field_poc[1] = INT_MAX;
             ff_init_poc(h, field_poc, &s->output_picture_number);
 
+            opcode = MMCO_END;
+            if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
+                /* Continue parsing to check if MMCO_RESET is present.
+                 * FIXME: MMCO_RESET could appear in non-first slice.
+                 *        Maybe, we should parse all undisposable non-IDR 
slice of this
+                 *        picture until encountering MMCO_RESET in a slice of 
it. */
+                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++) {
+                        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 || opcode == MMCO_RESET)
+                            break;
+
+                        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);
+                    }
+                }
+            }
+
             /* Set up the prev_ values for decoding POC of the next picture. */
             h->prev_frame_num        = h->frame_num;
-            h->prev_frame_num_offset = h->frame_num_offset;
+            h->prev_frame_num_offset = opcode != MMCO_RESET ? 
h->frame_num_offset : 0;
             if (h->nal_ref_idc != 0) {
-                h->prev_poc_msb = h->poc_msb;
-                h->prev_poc_lsb = h->poc_lsb;
+                if (opcode != MMCO_RESET) {
+                    h->prev_poc_msb = h->poc_msb;
+                    h->prev_poc_lsb = h->poc_lsb;
+                } else {
+                    h->prev_poc_msb = 0;
+                    h->prev_poc_lsb =
+                        h->picture_structure == PICT_BOTTOM_FIELD ? 0 : 
field_poc[0];
+                }
             }
 
             if (h->sps.pic_struct_present_flag) {
-- 
1.8.3.msysgit.0

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to