diff mbox series

[FFmpeg-devel,5/7] lavc/h264: export ecinfo

Message ID 20230721133746.33335-5-jdek@itanimul.li
State New
Headers show
Series [FFmpeg-devel,1/7] lavu: add ecinfo sidedata | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

J. Dekker July 21, 2023, 1:37 p.m. UTC
Export ecinfo to the user when AV_CODEC_EXPORT_DATA_ERROR is set.

Co-Authored-By: Thomas Guillem <thomas@gllm.fr>
Signed-off-by: J. Dekker <jdek@itanimul.li>
---
 libavcodec/h264_picture.c | 10 ++++++++--
 libavcodec/h264_slice.c   | 16 +++++++++++-----
 libavcodec/h264dec.c      | 33 +++++++++++++++++++++++++++------
 libavcodec/h264dec.h      |  8 +++++++-
 4 files changed, 53 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/h264_picture.c b/libavcodec/h264_picture.c
index dcaf0fdb0a..aa7c5ac673 100644
--- a/libavcodec/h264_picture.c
+++ b/libavcodec/h264_picture.c
@@ -48,6 +48,7 @@  void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
     av_buffer_unref(&pic->qscale_table_buf);
     av_buffer_unref(&pic->mb_type_buf);
     av_buffer_unref(&pic->pps_buf);
+    av_buffer_unref(&pic->ec_info_buf);
     for (i = 0; i < 2; i++) {
         av_buffer_unref(&pic->motion_val_buf[i]);
         av_buffer_unref(&pic->ref_index_buf[i]);
@@ -61,6 +62,7 @@  static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
     dst->qscale_table = src->qscale_table;
     dst->mb_type      = src->mb_type;
     dst->pps          = src->pps;
+    dst->ec_info      = src->ec_info;
 
     for (int i = 0; i < 2; i++) {
         dst->motion_val[i] = src->motion_val[i];
@@ -111,7 +113,9 @@  int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
     dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
     dst->mb_type_buf      = av_buffer_ref(src->mb_type_buf);
     dst->pps_buf          = av_buffer_ref(src->pps_buf);
-    if (!dst->qscale_table_buf || !dst->mb_type_buf || !dst->pps_buf) {
+    dst->ec_info_buf      = av_buffer_ref(src->ec_info_buf);
+    if (!dst->qscale_table_buf || !dst->mb_type_buf || !dst->pps_buf
+     || !dst->ec_info_buf) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
@@ -168,6 +172,7 @@  int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture
     ret  = av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
     ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
     ret |= av_buffer_replace(&dst->pps_buf, src->pps_buf);
+    ret |= av_buffer_replace(&dst->ec_info_buf, src->ec_info_buf);
     if (ret < 0)
         goto fail;
 
@@ -192,7 +197,7 @@  fail:
     return ret;
 }
 
-void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
+void ff_h264_set_erpic(ERPicture *dst, H264Picture *src, bool export_error)
 {
 #if CONFIG_ERROR_RESILIENCE
     int i;
@@ -202,6 +207,7 @@  void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
     if (!src)
         return;
 
+    dst->info = export_error ? src->ec_info : NULL;
     dst->f = src->f;
     dst->tf = &src->tf;
 
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index 41bf30eefc..b291405c2a 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -176,12 +176,15 @@  static int init_table_pools(H264Context *h)
                                                sizeof(int16_t), av_buffer_allocz);
     h->ref_index_pool    = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
 
+    h->ec_info_pool      = av_buffer_pool_init(sizeof(AVECInfo), av_buffer_allocz);
+
     if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
-        !h->ref_index_pool) {
+        !h->ref_index_pool || !h->ec_info_pool) {
         av_buffer_pool_uninit(&h->qscale_table_pool);
         av_buffer_pool_uninit(&h->mb_type_pool);
         av_buffer_pool_uninit(&h->motion_val_pool);
         av_buffer_pool_uninit(&h->ref_index_pool);
+        av_buffer_pool_uninit(&h->ec_info_pool);
         return AVERROR(ENOMEM);
     }
 
@@ -240,11 +243,13 @@  static int alloc_picture(H264Context *h, H264Picture *pic)
 
     pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
     pic->mb_type_buf      = av_buffer_pool_get(h->mb_type_pool);
-    if (!pic->qscale_table_buf || !pic->mb_type_buf)
+    pic->ec_info_buf      = av_buffer_pool_get(h->ec_info_pool);
+    if (!pic->qscale_table_buf || !pic->mb_type_buf || !pic->ec_info_buf)
         goto fail;
 
     pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
     pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
+    pic->ec_info = (AVECInfo*) pic->ec_info_buf->data;
 
     for (i = 0; i < 2; i++) {
         pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
@@ -514,6 +519,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
     pic->f->crop_right  = h->crop_right;
     pic->f->crop_top    = h->crop_top;
     pic->f->crop_bottom = h->crop_bottom;
+    pic->error_decode_slices = 0;
 
     pic->needs_fg = h->sei.common.film_grain_characteristics.present && !h->avctx->hwaccel &&
         !(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
@@ -524,7 +530,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
     h->cur_pic_ptr = pic;
     ff_h264_unref_picture(h, &h->cur_pic);
     if (CONFIG_ERROR_RESILIENCE) {
-        ff_h264_set_erpic(&h->er.cur_pic, NULL);
+        ff_h264_set_erpic(&h->er.cur_pic, NULL, false);
     }
 
     if ((ret = ff_h264_ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
@@ -537,8 +543,8 @@  FF_ENABLE_DEPRECATION_WARNINGS
 
     if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
         ff_er_frame_start(&h->er);
-        ff_h264_set_erpic(&h->er.last_pic, NULL);
-        ff_h264_set_erpic(&h->er.next_pic, NULL);
+        ff_h264_set_erpic(&h->er.last_pic, NULL, false);
+        ff_h264_set_erpic(&h->er.next_pic, NULL, false);
     }
 
     for (i = 0; i < 16; i++) {
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 19f8dba131..fa30f75f99 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -153,6 +153,7 @@  void ff_h264_free_tables(H264Context *h)
     av_buffer_pool_uninit(&h->mb_type_pool);
     av_buffer_pool_uninit(&h->motion_val_pool);
     av_buffer_pool_uninit(&h->ref_index_pool);
+    av_buffer_pool_uninit(&h->ec_info_pool);
 
 #if CONFIG_ERROR_RESILIENCE
     av_freep(&h->er.mb_index2xy);
@@ -741,7 +742,7 @@  static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
 
     // set decode_error_flags to allow users to detect concealed decoding errors
     if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
-        h->cur_pic_ptr->f->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
+        h->cur_pic_ptr->error_decode_slices = 1;
     }
 
     ret = 0;
@@ -764,24 +765,34 @@  end:
 
         H264SliceContext *sl = h->slice_ctx;
         int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
+        bool export_error = avctx->export_side_data & AV_CODEC_EXPORT_DATA_ERROR;
 
-        ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
+        ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr, export_error);
 
         if (use_last_pic) {
-            ff_h264_set_erpic(&h->er.last_pic, &h->last_pic_for_ec);
+            ff_h264_set_erpic(&h->er.last_pic, &h->last_pic_for_ec, export_error);
             sl->ref_list[0][0].parent = &h->last_pic_for_ec;
             memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
             memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
             sl->ref_list[0][0].reference = h->last_pic_for_ec.reference;
         } else if (sl->ref_count[0]) {
-            ff_h264_set_erpic(&h->er.last_pic, sl->ref_list[0][0].parent);
+            ff_h264_set_erpic(&h->er.last_pic, sl->ref_list[0][0].parent, export_error);
         } else
-            ff_h264_set_erpic(&h->er.last_pic, NULL);
+            ff_h264_set_erpic(&h->er.last_pic, NULL, export_error);
 
         if (sl->ref_count[1])
-            ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);
+            ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent, export_error);
 
         ff_er_frame_end(&h->er);
+
+        /* Copy the ec_info into out_ec_info now since the ec_info might be
+         * written by an other thread while outputing the picture. Don't add
+         * the ec_info in the AVFrame side data since the AVFrame metadata
+         * can't be modified after calling ff_thread_finish_setup() (prevent
+         * data-races). */
+        if (export_error)
+            h->cur_pic_ptr->out_ec_info = *h->cur_pic_ptr->ec_info;
+
         if (use_last_pic)
             memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
     }
@@ -850,6 +861,16 @@  static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
     if (ret < 0)
         return ret;
 
+    if (srcp->out_ec_info.error | srcp->out_ec_info.ref_error) {
+        AVECInfo *eci = av_eci_create_side_data(dst);
+        if (eci)
+            *eci = srcp->out_ec_info;
+        dst->decode_error_flags |= FF_DECODE_ERROR_CONCEALMENT_ACTIVE;
+        av_log(h, AV_LOG_DEBUG, "ecinfo error: %" PRIu64 ", ref_error: %" PRIu64 "\n", eci->error, eci->ref_error);
+    }
+    if (srcp->error_decode_slices)
+        dst->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
+
     if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)
         return ret;
 
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
index 9a1ec1bace..5a541e2c0b 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -154,6 +154,11 @@  typedef struct H264Picture {
 
     int mb_width, mb_height;
     int mb_stride;
+
+    AVBufferRef *ec_info_buf;
+    AVECInfo *ec_info;
+    AVECInfo out_ec_info;
+    int error_decode_slices;
 } H264Picture;
 
 typedef struct H264Ref {
@@ -551,6 +556,7 @@  typedef struct H264Context {
     AVBufferPool *mb_type_pool;
     AVBufferPool *motion_val_pool;
     AVBufferPool *ref_index_pool;
+    AVBufferPool *ec_info_pool;
     int ref2frm[MAX_SLICES][2][64];     ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
 } H264Context;
 
@@ -806,6 +812,6 @@  void ff_h264_flush_change(H264Context *h);
 
 void ff_h264_free_tables(H264Context *h);
 
-void ff_h264_set_erpic(ERPicture *dst, H264Picture *src);
+void ff_h264_set_erpic(ERPicture *dst, H264Picture *src, bool export_error);
 
 #endif /* AVCODEC_H264DEC_H */