]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/h264.c
Merge commit '050324d020f843ce333276ebb6f27cc6026f37d0'
[ffmpeg] / libavcodec / h264.c
index 63f063a7f3f1c6ea384ce303f4bdf9252e6d7d6d..933c58234157174995cb46a19577139ac6200243 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,19 +387,6 @@ 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;
-
-        if (free_rbsp) {
-            av_freep(&hx->rbsp_buffer);
-            hx->rbsp_buffer_size = 0;
-        }
-        if (i)
-            av_freep(&h->thread_context[i]);
-    }
-
     for (i = 0; i < h->nb_slice_ctx; i++) {
         H264SliceContext *sl = &h->slice_ctx[i];
 
@@ -427,6 +404,11 @@ void ff_h264_free_tables(H264Context *h, int free_rbsp)
         sl->edge_emu_buffer_allocated   = 0;
         sl->top_borders_allocated[0]    = 0;
         sl->top_borders_allocated[1]    = 0;
+
+        if (free_rbsp) {
+            av_freep(&sl->rbsp_buffer);
+            sl->rbsp_buffer_size            = 0;
+        }
     }
 }
 
@@ -637,9 +619,6 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx)
     h->bit_depth_luma    = 8;
     h->chroma_format_idc = 1;
 
-    h->avctx->bits_per_raw_sample = 8;
-    h->cur_chroma_format_idc = 1;
-
     ff_h264dsp_init(&h->h264dsp, 8, 1);
     av_assert0(h->sps.bit_depth_chroma == 0);
     ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
@@ -651,6 +630,7 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx)
 
     /* needed so that IDCT permutation is known early */
     ff_videodsp_init(&h->vdsp, 8);
+    h->cur_chroma_format_idc = -1;
 
     memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
     memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
@@ -672,6 +652,7 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx)
     ff_init_cabac_states();
 
     h->pixel_shift        = 0;
+    h->cur_bit_depth_luma =
     h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
 
     h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ?  H264_MAX_THREADS : 1;
@@ -681,9 +662,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++)
@@ -721,6 +701,15 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx)
 
     ff_h264_flush_change(h);
 
+    if (h->enable_er < 0 && (avctx->active_thread_type & FF_THREAD_SLICE))
+        h->enable_er = 0;
+
+    if (h->enable_er && (avctx->active_thread_type & FF_THREAD_SLICE)) {
+        av_log(avctx, AV_LOG_WARNING,
+               "Error resilience with slice threads is enabled. It is unsafe and unsupported and may crash. "
+               "Use it at your own risk\n");
+    }
+
     return 0;
 }
 
@@ -745,8 +734,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;
@@ -1258,7 +1245,7 @@ int ff_h264_set_parameter_from_sps(H264Context *h)
     if (h->avctx->has_b_frames < 2)
         h->avctx->has_b_frames = !h->low_delay;
 
-    if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
+    if (h->cur_bit_depth_luma         != h->sps.bit_depth_luma ||
         h->cur_chroma_format_idc      != h->sps.chroma_format_idc) {
         if (h->avctx->codec &&
             h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU &&
@@ -1269,6 +1256,7 @@ int ff_h264_set_parameter_from_sps(H264Context *h)
         }
         if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 14 &&
             h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13) {
+            h->cur_bit_depth_luma         =
             h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
             h->cur_chroma_format_idc      = h->sps.chroma_format_idc;
             h->pixel_shift                = h->sps.bit_depth_luma > 8;
@@ -1392,7 +1380,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)
@@ -1433,7 +1421,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;
@@ -1491,10 +1478,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;
@@ -1507,7 +1493,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,
@@ -1530,7 +1516,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:
@@ -1538,16 +1524,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");
@@ -1561,14 +1547,20 @@ again:
                     ret = -1;
                     goto end;
                 }
-                if(!idr_cleared)
+                if(!idr_cleared) {
+                    if (h->current_slice && (avctx->active_thread_type & FF_THREAD_SLICE)) {
+                        av_log(h, AV_LOG_ERROR, "invalid mixed IDR / non IDR frames cannot be decoded in slice multithreading mode\n");
+                        ret = AVERROR_INVALIDDATA;
+                        goto end;
+                    }
                     idr(h); // FIXME ensure we don't lose some frames if there is reordering
+                }
                 idr_cleared = 1;
                 h->has_recovery_point = 1;
             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) {
@@ -1586,16 +1578,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);
@@ -1610,8 +1602,8 @@ again:
                         decode_postinit(h, nal_index >= nals_needed);
 
                     if (h->avctx->hwaccel &&
-                        (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
-                        return ret;
+                        (ret = h->avctx->hwaccel->start_frame(h->avctx, buf, buf_size)) < 0)
+                        goto end;
                     if (CONFIG_H264_VDPAU_DECODER &&
                         h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
                         ff_vdpau_h264_picture_start(h);
@@ -1623,7 +1615,7 @@ again:
                                                            &buf[buf_index - consumed],
                                                            consumed);
                         if (ret < 0)
-                            return ret;
+                            goto end;
                     } else if (CONFIG_H264_VDPAU_DECODER &&
                                h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
                         ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
@@ -1681,7 +1673,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) {
@@ -1696,13 +1688,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;
             }
@@ -1951,6 +1939,22 @@ static av_cold int h264_decode_end(AVCodecContext *avctx)
     return 0;
 }
 
+#define OFFSET(x) offsetof(H264Context, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+static const AVOption h264_options[] = {
+    {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
+    {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
+    { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VD },
+    { NULL },
+};
+
+static const AVClass h264_class = {
+    .class_name = "H264 Decoder",
+    .item_name  = av_default_item_name,
+    .option     = h264_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVProfile profiles[] = {
     { FF_PROFILE_H264_BASELINE,             "Baseline"              },
     { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline"  },
@@ -1968,19 +1972,6 @@ static const AVProfile profiles[] = {
     { FF_PROFILE_UNKNOWN },
 };
 
-static const AVOption h264_options[] = {
-    {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
-    {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
-    {NULL}
-};
-
-static const AVClass h264_class = {
-    .class_name = "H264 Decoder",
-    .item_name  = av_default_item_name,
-    .option     = h264_options,
-    .version    = LIBAVUTIL_VERSION_INT,
-};
-
 AVCodec ff_h264_decoder = {
     .name                  = "h264",
     .long_name             = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),