]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/h264_refs.c
Merge commit 'b13fc1e344011949929975a3451f78f226aa1de3'
[ffmpeg] / libavcodec / h264_refs.c
index 2f80de12a5636441fd1531237d8001f6742524ac..8011413d24bcada56dbcee4492d2732637e62a2e 100644 (file)
@@ -31,6 +31,7 @@
 #include "internal.h"
 #include "avcodec.h"
 #include "h264.h"
+#include "h264dec.h"
 #include "golomb.h"
 #include "mpegutils.h"
 
@@ -270,7 +271,32 @@ static int pic_num_extract(const H264Context *h, int pic_num, int *structure)
     return pic_num;
 }
 
-int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
+static void h264_fill_mbaff_ref_list(H264SliceContext *sl)
+{
+    int list, i, j;
+    for (list = 0; list < sl->list_count; list++) {
+        for (i = 0; i < sl->ref_count[list]; i++) {
+            H264Ref *frame = &sl->ref_list[list][i];
+            H264Ref *field = &sl->ref_list[list][16 + 2 * i];
+
+            field[0] = *frame;
+
+            for (j = 0; j < 3; j++)
+                field[0].linesize[j] <<= 1;
+            field[0].reference = PICT_TOP_FIELD;
+            field[0].poc       = field[0].parent->field_poc[0];
+
+            field[1] = field[0];
+
+            for (j = 0; j < 3; j++)
+                field[1].data[j] += frame->parent->f->linesize[j];
+            field[1].reference = PICT_BOTTOM_FIELD;
+            field[1].poc       = field[1].parent->field_poc[1];
+        }
+    }
+}
+
+int ff_h264_build_ref_list(H264Context *h, H264SliceContext *sl)
 {
     int list, index, pic_structure;
 
@@ -280,102 +306,90 @@ int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
     h264_initialise_ref_list(h, sl);
 
     for (list = 0; list < sl->list_count; list++) {
-        if (get_bits1(&sl->gb)) {    // ref_pic_list_modification_flag_l[01]
-            int pred = h->curr_pic_num;
-
-            for (index = 0; ; index++) {
-                unsigned int modification_of_pic_nums_idc = get_ue_golomb_31(&sl->gb);
-                unsigned int pic_id;
-                int i;
-                H264Picture *ref = NULL;
-
-                if (modification_of_pic_nums_idc == 3)
-                    break;
-
-                if (index >= sl->ref_count[list]) {
-                    av_log(h->avctx, AV_LOG_ERROR, "reference count overflow\n");
-                    return -1;
+        int pred = sl->curr_pic_num;
+
+        for (index = 0; index < sl->nb_ref_modifications[list]; index++) {
+            unsigned int modification_of_pic_nums_idc = sl->ref_modifications[list][index].op;
+            unsigned int                          val = sl->ref_modifications[list][index].val;
+            unsigned int pic_id;
+            int i;
+            H264Picture *ref = NULL;
+
+            switch (modification_of_pic_nums_idc) {
+            case 0:
+            case 1: {
+                const unsigned int abs_diff_pic_num = val + 1;
+                int frame_num;
+
+                if (abs_diff_pic_num > sl->max_pic_num) {
+                    av_log(h->avctx, AV_LOG_ERROR,
+                           "abs_diff_pic_num overflow\n");
+                    return AVERROR_INVALIDDATA;
                 }
 
-                switch (modification_of_pic_nums_idc) {
-                case 0:
-                case 1: {
-                    const unsigned int abs_diff_pic_num = get_ue_golomb_long(&sl->gb) + 1;
-                    int frame_num;
-
-                    if (abs_diff_pic_num > h->max_pic_num) {
-                        av_log(h->avctx, AV_LOG_ERROR,
-                               "abs_diff_pic_num overflow\n");
-                        return AVERROR_INVALIDDATA;
-                    }
-
-                    if (modification_of_pic_nums_idc == 0)
-                        pred -= abs_diff_pic_num;
-                    else
-                        pred += abs_diff_pic_num;
-                    pred &= h->max_pic_num - 1;
-
-                    frame_num = pic_num_extract(h, pred, &pic_structure);
-
-                    for (i = h->short_ref_count - 1; i >= 0; i--) {
-                        ref = h->short_ref[i];
-                        assert(ref->reference);
-                        assert(!ref->long_ref);
-                        if (ref->frame_num == frame_num &&
-                            (ref->reference & pic_structure))
-                            break;
-                    }
-                    if (i >= 0)
-                        ref->pic_id = pred;
-                    break;
+                if (modification_of_pic_nums_idc == 0)
+                    pred -= abs_diff_pic_num;
+                else
+                    pred += abs_diff_pic_num;
+                pred &= sl->max_pic_num - 1;
+
+                frame_num = pic_num_extract(h, pred, &pic_structure);
+
+                for (i = h->short_ref_count - 1; i >= 0; i--) {
+                    ref = h->short_ref[i];
+                    assert(ref->reference);
+                    assert(!ref->long_ref);
+                    if (ref->frame_num == frame_num &&
+                        (ref->reference & pic_structure))
+                        break;
                 }
-                case 2: {
-                    int long_idx;
-                    pic_id = get_ue_golomb(&sl->gb); // long_term_pic_idx
+                if (i >= 0)
+                    ref->pic_id = pred;
+                break;
+            }
+            case 2: {
+                int long_idx;
+                pic_id = val; // long_term_pic_idx
 
-                    long_idx = pic_num_extract(h, pic_id, &pic_structure);
+                long_idx = pic_num_extract(h, pic_id, &pic_structure);
 
-                    if (long_idx > 31U) {
-                        av_log(h->avctx, AV_LOG_ERROR,
-                               "long_term_pic_idx overflow\n");
-                        return AVERROR_INVALIDDATA;
-                    }
-                    ref = h->long_ref[long_idx];
-                    assert(!(ref && !ref->reference));
-                    if (ref && (ref->reference & pic_structure) && !mismatches_ref(h, ref)) {
-                        ref->pic_id = pic_id;
-                        assert(ref->long_ref);
-                        i = 0;
-                    } else {
-                        i = -1;
-                    }
-                    break;
-                }
-                default:
+                if (long_idx > 31U) {
                     av_log(h->avctx, AV_LOG_ERROR,
-                           "illegal modification_of_pic_nums_idc %u\n",
-                           modification_of_pic_nums_idc);
+                           "long_term_pic_idx overflow\n");
                     return AVERROR_INVALIDDATA;
                 }
-
-                if (i < 0) {
-                    av_log(h->avctx, AV_LOG_ERROR,
-                           "reference picture missing during reorder\n");
-                    memset(&sl->ref_list[list][index], 0, sizeof(sl->ref_list[0][0])); // FIXME
+                ref = h->long_ref[long_idx];
+                assert(!(ref && !ref->reference));
+                if (ref && (ref->reference & pic_structure)) {
+                    ref->pic_id = pic_id;
+                    assert(ref->long_ref);
+                    i = 0;
                 } else {
-                    for (i = index; i + 1 < sl->ref_count[list]; i++) {
-                        if (sl->ref_list[list][i].parent &&
-                            ref->long_ref == sl->ref_list[list][i].parent->long_ref &&
-                            ref->pic_id   == sl->ref_list[list][i].pic_id)
-                            break;
-                    }
-                    for (; i > index; i--) {
-                        sl->ref_list[list][i] = sl->ref_list[list][i - 1];
-                    }
-                    ref_from_h264pic(&sl->ref_list[list][index], ref);
-                    if (FIELD_PICTURE(h)) {
-                        pic_as_field(&sl->ref_list[list][index], pic_structure);
-                    }
+                    i = -1;
+                }
+                break;
+            }
+            default:
+                av_assert1(0);
+            }
+
+            if (i < 0) {
+                av_log(h->avctx, AV_LOG_ERROR,
+                       "reference picture missing during reorder\n");
+                memset(&sl->ref_list[list][index], 0, sizeof(sl->ref_list[0][0])); // FIXME
+            } else {
+                for (i = index; i + 1 < sl->ref_count[list]; i++) {
+                    if (sl->ref_list[list][i].parent &&
+                        ref->long_ref == sl->ref_list[list][i].parent->long_ref &&
+                        ref->pic_id   == sl->ref_list[list][i].pic_id)
+                        break;
+                }
+                for (; i > index; i--) {
+                    sl->ref_list[list][i] = sl->ref_list[list][i - 1];
+                }
+                ref_from_h264pic(&sl->ref_list[list][index], ref);
+                if (FIELD_PICTURE(h)) {
+                    pic_as_field(&sl->ref_list[list][index], pic_structure);
                 }
             }
         }
@@ -398,39 +412,45 @@ int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
         }
     }
 
+    if (FRAME_MBAFF(h))
+        h264_fill_mbaff_ref_list(sl);
+
     return 0;
 }
 
-void ff_h264_fill_mbaff_ref_list(H264SliceContext *sl)
+int ff_h264_decode_ref_pic_list_reordering(H264SliceContext *sl, void *logctx)
 {
-    int list, i, j;
-    for (list = 0; list < sl->list_count; list++) {
-        for (i = 0; i < sl->ref_count[list]; i++) {
-            H264Ref *frame = &sl->ref_list[list][i];
-            H264Ref *field = &sl->ref_list[list][16 + 2 * i];
+    int list, index;
 
-            field[0] = *frame;
+    sl->nb_ref_modifications[0] = 0;
+    sl->nb_ref_modifications[1] = 0;
 
-            for (j = 0; j < 3; j++)
-                field[0].linesize[j] <<= 1;
-            field[0].reference = PICT_TOP_FIELD;
-            field[0].poc       = field[0].parent->field_poc[0];
+    for (list = 0; list < sl->list_count; list++) {
+        if (!get_bits1(&sl->gb))    // ref_pic_list_modification_flag_l[01]
+            continue;
 
-            field[1] = field[0];
+        for (index = 0; ; index++) {
+            unsigned int op = get_ue_golomb_31(&sl->gb);
 
-            for (j = 0; j < 3; j++)
-                field[1].data[j] += frame->parent->f->linesize[j];
-            field[1].reference = PICT_BOTTOM_FIELD;
-            field[1].poc       = field[1].parent->field_poc[1];
+            if (op == 3)
+                break;
 
-            sl->pwt.luma_weight[16 + 2 * i][list][0] = sl->pwt.luma_weight[16 + 2 * i + 1][list][0] = sl->pwt.luma_weight[i][list][0];
-            sl->pwt.luma_weight[16 + 2 * i][list][1] = sl->pwt.luma_weight[16 + 2 * i + 1][list][1] = sl->pwt.luma_weight[i][list][1];
-            for (j = 0; j < 2; j++) {
-                sl->pwt.chroma_weight[16 + 2 * i][list][j][0] = sl->pwt.chroma_weight[16 + 2 * i + 1][list][j][0] = sl->pwt.chroma_weight[i][list][j][0];
-                sl->pwt.chroma_weight[16 + 2 * i][list][j][1] = sl->pwt.chroma_weight[16 + 2 * i + 1][list][j][1] = sl->pwt.chroma_weight[i][list][j][1];
+            if (index >= sl->ref_count[list]) {
+                av_log(logctx, AV_LOG_ERROR, "reference count overflow\n");
+                return AVERROR_INVALIDDATA;
+            } else if (op > 2) {
+                av_log(logctx, AV_LOG_ERROR,
+                       "illegal modification_of_pic_nums_idc %u\n",
+                       op);
+                return AVERROR_INVALIDDATA;
             }
+            sl->ref_modifications[list][index].val = get_ue_golomb_long(&sl->gb);
+            sl->ref_modifications[list][index].op  = op;
+            sl->nb_ref_modifications[list]++;
         }
     }
+
+    return 0;
 }
 
 /**
@@ -562,67 +582,43 @@ void ff_h264_remove_all_refs(H264Context *h)
     h->short_ref_count = 0;
 
     memset(h->default_ref, 0, sizeof(h->default_ref));
-    for (i = 0; i < h->nb_slice_ctx; i++) {
-        H264SliceContext *sl = &h->slice_ctx[i];
-        sl->list_count = sl->ref_count[0] = sl->ref_count[1] = 0;
-        memset(sl->ref_list, 0, sizeof(sl->ref_list));
-    }
-}
-
-static int check_opcodes(MMCO *mmco1, MMCO *mmco2, int n_mmcos)
-{
-    int i;
-
-    for (i = 0; i < n_mmcos; i++) {
-        if (mmco1[i].opcode != mmco2[i].opcode) {
-            av_log(NULL, AV_LOG_ERROR, "MMCO opcode [%d, %d] at %d mismatches between slices\n",
-                   mmco1[i].opcode, mmco2[i].opcode, i);
-            return -1;
-        }
-    }
-
-    return 0;
 }
 
-int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice)
+static void generate_sliding_window_mmcos(H264Context *h)
 {
-    MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
-    int mmco_index = 0, i = 0;
+    MMCO *mmco = h->mmco;
+    int nb_mmco = 0;
 
     if (h->short_ref_count &&
         h->long_ref_count + h->short_ref_count >= h->ps.sps->ref_frame_count &&
         !(FIELD_PICTURE(h) && !h->first_field && h->cur_pic_ptr->reference)) {
         mmco[0].opcode        = MMCO_SHORT2UNUSED;
         mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
-        mmco_index            = 1;
+        nb_mmco               = 1;
         if (FIELD_PICTURE(h)) {
             mmco[0].short_pic_num *= 2;
             mmco[1].opcode         = MMCO_SHORT2UNUSED;
             mmco[1].short_pic_num  = mmco[0].short_pic_num + 1;
-            mmco_index             = 2;
+            nb_mmco                = 2;
         }
     }
 
-    if (first_slice) {
-        h->mmco_index = mmco_index;
-    } else if (!first_slice && mmco_index >= 0 &&
-               (mmco_index != h->mmco_index ||
-                (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
-        av_log(h->avctx, AV_LOG_ERROR,
-               "Inconsistent MMCO state between slices [%d, %d]\n",
-               mmco_index, h->mmco_index);
-        return AVERROR_INVALIDDATA;
-    }
-    return 0;
+    h->nb_mmco = nb_mmco;
 }
 
-int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
+int ff_h264_execute_ref_pic_marking(H264Context *h)
 {
+    MMCO *mmco = h->mmco;
+    int mmco_count;
     int i, av_uninit(j);
     int pps_ref_count[2] = {0};
     int current_ref_assigned = 0, err = 0;
     H264Picture *av_uninit(pic);
 
+    if (!h->explicit_ref_marking)
+        generate_sliding_window_mmcos(h);
+    mmco_count = h->nb_mmco;
+
     if ((h->avctx->debug & FF_DEBUG_MMCO) && mmco_count == 0)
         av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
 
@@ -825,30 +821,32 @@ int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
     return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
 }
 
-int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
-                                   int first_slice)
+int ff_h264_decode_ref_pic_marking(H264SliceContext *sl, GetBitContext *gb,
+                                   const H2645NAL *nal, void *logctx)
 {
-    int i, ret;
-    MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = mmco_temp;
-    int mmco_index = 0;
+    int i;
+    MMCO *mmco = sl->mmco;
+    int nb_mmco = 0;
 
-    if (h->nal_unit_type == NAL_IDR_SLICE) { // FIXME fields
+    if (nal->type == H264_NAL_IDR_SLICE) { // FIXME fields
         skip_bits1(gb); // broken_link
         if (get_bits1(gb)) {
             mmco[0].opcode   = MMCO_LONG;
             mmco[0].long_arg = 0;
-            mmco_index       = 1;
+            nb_mmco          = 1;
         }
+        sl->explicit_ref_marking = 1;
     } else {
-        if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
+        sl->explicit_ref_marking = get_bits1(gb);
+        if (sl->explicit_ref_marking) {
             for (i = 0; i < MAX_MMCO_COUNT; i++) {
                 MMCOOpcode opcode = get_ue_golomb_31(gb);
 
                 mmco[i].opcode = opcode;
                 if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG) {
                     mmco[i].short_pic_num =
-                        (h->curr_pic_num - get_ue_golomb_long(gb) - 1) &
-                            (h->max_pic_num - 1);
+                        (sl->curr_pic_num - get_ue_golomb_long(gb) - 1) &
+                            (sl->max_pic_num - 1);
 #if 0
                     if (mmco[i].short_pic_num >= h->short_ref_count ||
                         !h->short_ref[mmco[i].short_pic_num]) {
@@ -865,8 +863,8 @@ int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
                     if (long_arg >= 32 ||
                         (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
                                              long_arg == 16) &&
-                         !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE(h)))) {
-                        av_log(h->avctx, AV_LOG_ERROR,
+                         !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE(sl)))) {
+                        av_log(logctx, AV_LOG_ERROR,
                                "illegal long ref in memory management control "
                                "operation %d\n", opcode);
                         return -1;
@@ -875,7 +873,7 @@ int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
                 }
 
                 if (opcode > (unsigned) MMCO_LONG) {
-                    av_log(h->avctx, AV_LOG_ERROR,
+                    av_log(logctx, AV_LOG_ERROR,
                            "illegal memory management control operation %d\n",
                            opcode);
                     return -1;
@@ -883,28 +881,11 @@ int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
                 if (opcode == MMCO_END)
                     break;
             }
-            mmco_index = i;
-        } else {
-            if (first_slice) {
-                ret = ff_generate_sliding_window_mmcos(h, first_slice);
-                if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
-                    return ret;
-            }
-            mmco_index = -1;
+            nb_mmco = i;
         }
     }
 
-    if (first_slice && mmco_index != -1) {
-        memcpy(h->mmco, mmco_temp, sizeof(h->mmco));
-        h->mmco_index = mmco_index;
-    } else if (!first_slice && mmco_index >= 0 &&
-               (mmco_index != h->mmco_index ||
-                check_opcodes(h->mmco, mmco_temp, mmco_index))) {
-        av_log(h->avctx, AV_LOG_ERROR,
-               "Inconsistent MMCO state between slices [%d, %d]\n",
-               mmco_index, h->mmco_index);
-        return AVERROR_INVALIDDATA;
-    }
+    sl->nb_mmco = nb_mmco;
 
     return 0;
 }