diff mbox

[FFmpeg-devel,v1] avcodec/hevc_sei: switch to AVBufferRef buffer for a53 caption

Message ID 20191220011721.9795-1-lance.lmwang@gmail.com
State New
Headers show

Commit Message

Limin Wang Dec. 20, 2019, 1:17 a.m. UTC
From: Limin Wang <lance.lmwang@gmail.com>

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
Please apply after the following patch:
https://patchwork.ffmpeg.org/patch/16878/

 libavcodec/hevc_sei.c | 15 +++++++--------
 libavcodec/hevc_sei.h |  3 +--
 libavcodec/hevcdec.c  | 16 ++++++++--------
 3 files changed, 16 insertions(+), 18 deletions(-)

Comments

James Almer Dec. 20, 2019, 5:02 p.m. UTC | #1
On 12/19/2019 10:17 PM, lance.lmwang@gmail.com wrote:
> From: Limin Wang <lance.lmwang@gmail.com>
> 
> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> ---
> Please apply after the following patch:
> https://patchwork.ffmpeg.org/patch/16878/

It doesn't really depend on it, so i'll apply it alone while that set is
reviewed.

Thanks.

> 
>  libavcodec/hevc_sei.c | 15 +++++++--------
>  libavcodec/hevc_sei.h |  3 +--
>  libavcodec/hevcdec.c  | 16 ++++++++--------
>  3 files changed, 16 insertions(+), 18 deletions(-)
> 
> diff --git a/libavcodec/hevc_sei.c b/libavcodec/hevc_sei.c
> index 7f738a049c..b87871e52a 100644
> --- a/libavcodec/hevc_sei.c
> +++ b/libavcodec/hevc_sei.c
> @@ -177,7 +177,8 @@ static int decode_registered_user_data_closed_caption(HEVCSEIA53Caption *s, GetB
>              size -= 2;
>  
>              if (cc_count && size >= cc_count * 3) {
> -                const uint64_t new_size = (s->a53_caption_size + cc_count
> +                int old_size = s->buf_ref ? s->buf_ref->size : 0;
> +                const uint64_t new_size = (old_size + cc_count
>                                             * UINT64_C(3));
>                  int i, ret;
>  
> @@ -185,14 +186,14 @@ static int decode_registered_user_data_closed_caption(HEVCSEIA53Caption *s, GetB
>                      return AVERROR(EINVAL);
>  
>                  /* Allow merging of the cc data from two fields. */
> -                ret = av_reallocp(&s->a53_caption, new_size);
> +                ret = av_buffer_realloc(&s->buf_ref, new_size);
>                  if (ret < 0)
>                      return ret;
>  
>                  for (i = 0; i < cc_count; i++) {
> -                    s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
> -                    s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
> -                    s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
> +                    s->buf_ref->data[old_size++] = get_bits(gb, 8);
> +                    s->buf_ref->data[old_size++] = get_bits(gb, 8);
> +                    s->buf_ref->data[old_size++] = get_bits(gb, 8);
>                  }
>                  skip_bits(gb, 8); // marker_bits
>              }
> @@ -392,9 +393,7 @@ int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s,
>  
>  void ff_hevc_reset_sei(HEVCSEI *s)
>  {
> -    s->a53_caption.a53_caption_size = 0;
> -    av_freep(&s->a53_caption.a53_caption);
> -
> +    av_buffer_unref(&s->a53_caption.buf_ref);
>      for (int i = 0; i < s->unregistered.nb_buf_ref; i++)
>          av_buffer_unref(&s->unregistered.buf_ref[i]);
>      s->unregistered.nb_buf_ref = 0;
> diff --git a/libavcodec/hevc_sei.h b/libavcodec/hevc_sei.h
> index 2464117d47..a8a2ab7dc6 100644
> --- a/libavcodec/hevc_sei.h
> +++ b/libavcodec/hevc_sei.h
> @@ -83,8 +83,7 @@ typedef struct HEVCSEIPictureTiming {
>  } HEVCSEIPictureTiming;
>  
>  typedef struct HEVCSEIA53Caption {
> -    int a53_caption_size;
> -    uint8_t *a53_caption;
> +    AVBufferRef *buf_ref;
>  } HEVCSEIA53Caption;
>  
>  typedef struct HEVCSEIUnregistered {
> diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
> index a4981e983c..451a58fb7f 100644
> --- a/libavcodec/hevcdec.c
> +++ b/libavcodec/hevcdec.c
> @@ -2778,14 +2778,14 @@ static int set_side_data(HEVCContext *s)
>                 metadata->MaxCLL, metadata->MaxFALL);
>      }
>  
> -    if (s->sei.a53_caption.a53_caption) {
> -        AVFrameSideData* sd = av_frame_new_side_data(out,
> -                                                     AV_FRAME_DATA_A53_CC,
> -                                                     s->sei.a53_caption.a53_caption_size);
> -        if (sd)
> -            memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
> -        av_freep(&s->sei.a53_caption.a53_caption);
> -        s->sei.a53_caption.a53_caption_size = 0;
> +    if (s->sei.a53_caption.buf_ref) {
> +        HEVCSEIA53Caption *a53 = &s->sei.a53_caption;
> +
> +        AVFrameSideData *sd = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_A53_CC, a53->buf_ref);
> +        if (!sd)
> +            av_buffer_unref(&a53->buf_ref);
> +        a53->buf_ref = NULL;
> +
>          s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
>      }
>  
>
diff mbox

Patch

diff --git a/libavcodec/hevc_sei.c b/libavcodec/hevc_sei.c
index 7f738a049c..b87871e52a 100644
--- a/libavcodec/hevc_sei.c
+++ b/libavcodec/hevc_sei.c
@@ -177,7 +177,8 @@  static int decode_registered_user_data_closed_caption(HEVCSEIA53Caption *s, GetB
             size -= 2;
 
             if (cc_count && size >= cc_count * 3) {
-                const uint64_t new_size = (s->a53_caption_size + cc_count
+                int old_size = s->buf_ref ? s->buf_ref->size : 0;
+                const uint64_t new_size = (old_size + cc_count
                                            * UINT64_C(3));
                 int i, ret;
 
@@ -185,14 +186,14 @@  static int decode_registered_user_data_closed_caption(HEVCSEIA53Caption *s, GetB
                     return AVERROR(EINVAL);
 
                 /* Allow merging of the cc data from two fields. */
-                ret = av_reallocp(&s->a53_caption, new_size);
+                ret = av_buffer_realloc(&s->buf_ref, new_size);
                 if (ret < 0)
                     return ret;
 
                 for (i = 0; i < cc_count; i++) {
-                    s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
-                    s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
-                    s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
+                    s->buf_ref->data[old_size++] = get_bits(gb, 8);
+                    s->buf_ref->data[old_size++] = get_bits(gb, 8);
+                    s->buf_ref->data[old_size++] = get_bits(gb, 8);
                 }
                 skip_bits(gb, 8); // marker_bits
             }
@@ -392,9 +393,7 @@  int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s,
 
 void ff_hevc_reset_sei(HEVCSEI *s)
 {
-    s->a53_caption.a53_caption_size = 0;
-    av_freep(&s->a53_caption.a53_caption);
-
+    av_buffer_unref(&s->a53_caption.buf_ref);
     for (int i = 0; i < s->unregistered.nb_buf_ref; i++)
         av_buffer_unref(&s->unregistered.buf_ref[i]);
     s->unregistered.nb_buf_ref = 0;
diff --git a/libavcodec/hevc_sei.h b/libavcodec/hevc_sei.h
index 2464117d47..a8a2ab7dc6 100644
--- a/libavcodec/hevc_sei.h
+++ b/libavcodec/hevc_sei.h
@@ -83,8 +83,7 @@  typedef struct HEVCSEIPictureTiming {
 } HEVCSEIPictureTiming;
 
 typedef struct HEVCSEIA53Caption {
-    int a53_caption_size;
-    uint8_t *a53_caption;
+    AVBufferRef *buf_ref;
 } HEVCSEIA53Caption;
 
 typedef struct HEVCSEIUnregistered {
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index a4981e983c..451a58fb7f 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2778,14 +2778,14 @@  static int set_side_data(HEVCContext *s)
                metadata->MaxCLL, metadata->MaxFALL);
     }
 
-    if (s->sei.a53_caption.a53_caption) {
-        AVFrameSideData* sd = av_frame_new_side_data(out,
-                                                     AV_FRAME_DATA_A53_CC,
-                                                     s->sei.a53_caption.a53_caption_size);
-        if (sd)
-            memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
-        av_freep(&s->sei.a53_caption.a53_caption);
-        s->sei.a53_caption.a53_caption_size = 0;
+    if (s->sei.a53_caption.buf_ref) {
+        HEVCSEIA53Caption *a53 = &s->sei.a53_caption;
+
+        AVFrameSideData *sd = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_A53_CC, a53->buf_ref);
+        if (!sd)
+            av_buffer_unref(&a53->buf_ref);
+        a53->buf_ref = NULL;
+
         s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
     }