]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/h264.c
Merge commit 'a12d3188cbec15e22070e139fa5cc541da07e2c3'
[ffmpeg] / libavcodec / h264.c
index dd324a9f9d6140c12182893a36d48dcf797d584d..18e5a4dd8b94cefa8ad5a3e03aff06fe310053f4 100644 (file)
@@ -77,7 +77,7 @@ static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
      * practice then correct remapping should be added. */
     if (ref >= sl->ref_count[0])
         ref = 0;
-    if (!sl->ref_list[0][ref].f.data[0]) {
+    if (!sl->ref_list[0][ref].data[0]) {
         av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
         ref = 0;
     }
@@ -99,8 +99,7 @@ void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
                              int y, int height)
 {
     AVCodecContext *avctx = h->avctx;
-    const AVFrame   *cur  = &h->cur_pic.f;
-    AVFrame *last = sl->ref_list[0][0].f.data[0] ? &sl->ref_list[0][0].f : NULL;
+    const AVFrame   *src  = &h->cur_pic.f;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
     int vshift = desc->log2_chroma_h;
     const int field_pic = h->picture_structure != PICT_FRAME;
@@ -115,18 +114,9 @@ void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
         return;
 
     if (avctx->draw_horiz_band) {
-        const AVFrame *src;
         int offset[AV_NUM_DATA_POINTERS];
         int i;
 
-        if (cur->pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
-            (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
-            src = cur;
-        else if (last)
-            src = last;
-        else
-            return;
-
         offset[0] = y * src->linesize[0];
         offset[1] =
         offset[2] = (y >> vshift) * src->linesize[1];
@@ -233,7 +223,8 @@ int ff_h264_check_intra_pred_mode(const H264Context *h, H264SliceContext *sl,
     return mode;
 }
 
-const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
+const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl,
+                                  const uint8_t *src,
                                   int *dst_length, int *consumed, int length)
 {
     int i, si, di;
@@ -293,8 +284,8 @@ const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
     }
 #endif
 
-    av_fast_padded_malloc(&h->rbsp_buffer, &h->rbsp_buffer_size, length+MAX_MBPAIR_SIZE);
-    dst = h->rbsp_buffer;
+    av_fast_padded_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size, length+MAX_MBPAIR_SIZE);
+    dst = sl->rbsp_buffer;
 
     if (!dst)
         return NULL;
@@ -364,7 +355,6 @@ static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
 void ff_h264_free_tables(H264Context *h, int free_rbsp)
 {
     int i;
-    H264Context *hx;
 
     av_freep(&h->intra4x4_pred_mode);
     av_freep(&h->chroma_pred_mode_table);
@@ -397,27 +387,28 @@ void ff_h264_free_tables(H264Context *h, int free_rbsp)
 
     h->cur_pic_ptr = NULL;
 
-    for (i = 0; i < H264_MAX_THREADS; i++) {
-        hx = h->thread_context[i];
-        if (!hx)
-            continue;
-        av_freep(&hx->top_borders[1]);
-        av_freep(&hx->top_borders[0]);
-        av_freep(&hx->bipred_scratchpad);
-        av_freep(&hx->edge_emu_buffer);
-        av_freep(&hx->dc_val_base);
-        av_freep(&hx->er.mb_index2xy);
-        av_freep(&hx->er.error_status_table);
-        av_freep(&hx->er.er_temp_buffer);
-        av_freep(&hx->er.mbintra_table);
-        av_freep(&hx->er.mbskip_table);
+    for (i = 0; i < h->nb_slice_ctx; i++) {
+        H264SliceContext *sl = &h->slice_ctx[i];
+
+        av_freep(&sl->dc_val_base);
+        av_freep(&sl->er.mb_index2xy);
+        av_freep(&sl->er.error_status_table);
+        av_freep(&sl->er.er_temp_buffer);
+
+        av_freep(&sl->bipred_scratchpad);
+        av_freep(&sl->edge_emu_buffer);
+        av_freep(&sl->top_borders[0]);
+        av_freep(&sl->top_borders[1]);
+
+        sl->bipred_scratchpad_allocated = 0;
+        sl->edge_emu_buffer_allocated   = 0;
+        sl->top_borders_allocated[0]    = 0;
+        sl->top_borders_allocated[1]    = 0;
 
         if (free_rbsp) {
-            av_freep(&hx->rbsp_buffer);
-            hx->rbsp_buffer_size = 0;
+            av_freep(&sl->rbsp_buffer);
+            sl->rbsp_buffer_size            = 0;
         }
-        if (i)
-            av_freep(&h->thread_context[i]);
     }
 }
 
@@ -491,28 +482,21 @@ fail:
  * Init context
  * Allocate buffers which are not shared amongst multiple threads.
  */
-int ff_h264_context_init(H264Context *h)
+int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
 {
-    ERContext *er = &h->er;
+    ERContext *er = &sl->er;
     int mb_array_size = h->mb_height * h->mb_stride;
     int y_size  = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
     int c_size  = h->mb_stride * (h->mb_height + 1);
     int yc_size = y_size + 2   * c_size;
     int x, y, i;
 
-    FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->top_borders[0],
-                      h->mb_width, 16 * 3 * sizeof(uint8_t) * 2, fail)
-    FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->top_borders[1],
-                      h->mb_width, 16 * 3 * sizeof(uint8_t) * 2, fail)
-
-    for (i = 0; i < h->nb_slice_ctx; i++) {
-        h->slice_ctx[i].ref_cache[0][scan8[5]  + 1] =
-        h->slice_ctx[i].ref_cache[0][scan8[7]  + 1] =
-        h->slice_ctx[i].ref_cache[0][scan8[13] + 1] =
-        h->slice_ctx[i].ref_cache[1][scan8[5]  + 1] =
-        h->slice_ctx[i].ref_cache[1][scan8[7]  + 1] =
-        h->slice_ctx[i].ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
-    }
+    sl->ref_cache[0][scan8[5]  + 1] =
+    sl->ref_cache[0][scan8[7]  + 1] =
+    sl->ref_cache[0][scan8[13] + 1] =
+    sl->ref_cache[1][scan8[5]  + 1] =
+    sl->ref_cache[1][scan8[7]  + 1] =
+    sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
 
     if (CONFIG_ERROR_RESILIENCE) {
         /* init ER */
@@ -541,21 +525,16 @@ int ff_h264_context_init(H264Context *h)
         FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
                           mb_array_size * sizeof(uint8_t), fail);
 
-        FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
-        memset(er->mbintra_table, 1, mb_array_size);
-
-        FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
-
         FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
                          h->mb_height * h->mb_stride, fail);
 
-        FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base,
+        FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
                           yc_size * sizeof(int16_t), fail);
-        er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
-        er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
+        er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
+        er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
         er->dc_val[2] = er->dc_val[1] + c_size;
         for (i = 0; i < yc_size; i++)
-            h->dc_val_base[i] = 1024;
+            sl->dc_val_base[i] = 1024;
     }
 
     return 0;
@@ -684,9 +663,8 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
     }
 
-    h->thread_context[0] = h;
     for (i = 0; i < h->nb_slice_ctx; i++)
-        h->slice_ctx[i].h264 = h->thread_context[0];
+        h->slice_ctx[i].h264 = h;
 
     h->outputed_poc      = h->next_outputed_poc = INT_MIN;
     for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
@@ -748,8 +726,6 @@ static int decode_init_thread_copy(AVCodecContext *avctx)
         h->slice_ctx[i].h264 = h;
 
     h->avctx               = avctx;
-    h->rbsp_buffer         = NULL;
-    h->rbsp_buffer_size    = 0;
     h->context_initialized = 0;
 
     return 0;
@@ -1395,7 +1371,7 @@ static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
                 continue;
         }
 
-        ptr = ff_h264_decode_nal(h, buf + buf_index, &dst_length, &consumed,
+        ptr = ff_h264_decode_nal(h, &h->slice_ctx[0], buf + buf_index, &dst_length, &consumed,
                                  next_avc - buf_index);
 
         if (!ptr || dst_length < 0)
@@ -1436,7 +1412,6 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
                             int parse_extradata)
 {
     AVCodecContext *const avctx = h->avctx;
-    H264Context *hx; ///< thread context
     H264SliceContext *sl;
     int buf_index;
     unsigned context_count;
@@ -1494,10 +1469,9 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
                     continue;
             }
 
-            hx = h->thread_context[context_count];
             sl = &h->slice_ctx[context_count];
 
-            ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
+            ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
                                      &consumed, next_avc - buf_index);
             if (!ptr || dst_length < 0) {
                 ret = -1;
@@ -1510,7 +1484,7 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
             if (h->avctx->debug & FF_DEBUG_STARTCODE)
                 av_log(h->avctx, AV_LOG_DEBUG,
                        "NAL %d/%d at %d/%d length %d\n",
-                       hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length);
+                       h->nal_unit_type, h->nal_ref_idc, buf_index, buf_size, dst_length);
 
             if (h->is_avc && (nalsize != consumed) && nalsize)
                 av_log(h->avctx, AV_LOG_DEBUG,
@@ -1533,7 +1507,7 @@ again:
              * parsing. Decoding slices is not possible in codec init
              * with frame-mt */
             if (parse_extradata) {
-                switch (hx->nal_unit_type) {
+                switch (h->nal_unit_type) {
                 case NAL_IDR_SLICE:
                 case NAL_SLICE:
                 case NAL_DPA:
@@ -1541,16 +1515,16 @@ again:
                 case NAL_DPC:
                     av_log(h->avctx, AV_LOG_WARNING,
                            "Ignoring NAL %d in global header/extradata\n",
-                           hx->nal_unit_type);
+                           h->nal_unit_type);
                     // fall through to next case
                 case NAL_AUXILIARY_SLICE:
-                    hx->nal_unit_type = NAL_FF_IGNORE;
+                    h->nal_unit_type = NAL_FF_IGNORE;
                 }
             }
 
             err = 0;
 
-            switch (hx->nal_unit_type) {
+            switch (h->nal_unit_type) {
             case NAL_IDR_SLICE:
                 if ((ptr[0] & 0xFC) == 0x98) {
                     av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
@@ -1571,7 +1545,7 @@ again:
             case NAL_SLICE:
                 init_get_bits(&sl->gb, ptr, bit_length);
 
-                if ((err = ff_h264_decode_slice_header(hx, sl, h)))
+                if ((err = ff_h264_decode_slice_header(h, sl)))
                     break;
 
                 if (h->sei_recovery_frame_cnt >= 0) {
@@ -1589,16 +1563,16 @@ again:
                 }
 
                 h->cur_pic_ptr->f.key_frame |=
-                    (hx->nal_unit_type == NAL_IDR_SLICE);
+                    (h->nal_unit_type == NAL_IDR_SLICE);
 
-                if (hx->nal_unit_type == NAL_IDR_SLICE ||
+                if (h->nal_unit_type == NAL_IDR_SLICE ||
                     h->recovery_frame == h->frame_num) {
                     h->recovery_frame         = -1;
                     h->cur_pic_ptr->recovered = 1;
                 }
                 // If we have an IDR, all frames after it in decoded order are
                 // "recovered".
-                if (hx->nal_unit_type == NAL_IDR_SLICE)
+                if (h->nal_unit_type == NAL_IDR_SLICE)
                     h->frame_recovered |= FRAME_RECOVERED_IDR;
                 h->frame_recovered |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
                 h->frame_recovered |= 3*!!(avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT);
@@ -1684,7 +1658,7 @@ again:
                 break;
             default:
                 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
-                       hx->nal_unit_type, bit_length);
+                       h->nal_unit_type, bit_length);
             }
 
             if (context_count == h->max_contexts) {
@@ -1699,13 +1673,9 @@ again:
                     av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
                 sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
             } else if (err == SLICE_SINGLETHREAD) {
-                /* Slice could not be decoded in parallel mode, copy down
-                 * NAL unit stuff to context 0 and restart. Note that
-                 * rbsp_buffer is not transferred, but since we no longer
+                /* Slice could not be decoded in parallel mode, restart. Note
+                 * that rbsp_buffer is not transferred, but since we no longer
                  * run in parallel mode this should not be an issue. */
-                h->nal_unit_type = hx->nal_unit_type;
-                h->nal_ref_idc   = hx->nal_ref_idc;
-                hx               = h;
                 sl               = &h->slice_ctx[0];
                 goto again;
             }
@@ -1908,7 +1878,7 @@ static int h264_decode_frame(AVCodecContext *avctx, void *data,
                 return ret;
             *got_frame = 1;
             if (CONFIG_MPEGVIDEO) {
-                ff_print_debug_info2(h->avctx, pict, h->er.mbskip_table,
+                ff_print_debug_info2(h->avctx, pict, NULL,
                                     h->next_output_pic->mb_type,
                                     h->next_output_pic->qscale_table,
                                     h->next_output_pic->motion_val,