[FFmpeg-devel] lavc/extract_extradata: Use bytestream api

Submitted by Andriy Gelman on Nov. 30, 2019, 4:21 a.m.

Details

Message ID 20191130042111.21276-1-andriy.gelman@gmail.com
State New
Headers show

Commit Message

Andriy Gelman Nov. 30, 2019, 4:21 a.m.
From: Andriy Gelman <andriy.gelman@gmail.com>

Signed-off-by: Andriy Gelman <andriy.gelman@gmail.com>
---
 libavcodec/extract_extradata_bsf.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

Comments

Andreas Rheinhardt Nov. 30, 2019, 5:51 a.m.
Andriy Gelman:
> From: Andriy Gelman <andriy.gelman@gmail.com>
> 
> Signed-off-by: Andriy Gelman <andriy.gelman@gmail.com>
> ---
>  libavcodec/extract_extradata_bsf.c | 27 +++++++++++++++++----------
>  1 file changed, 17 insertions(+), 10 deletions(-)
> 
> diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c
> index ff111de48cb..4a9f6608734 100644
> --- a/libavcodec/extract_extradata_bsf.c
> +++ b/libavcodec/extract_extradata_bsf.c
> @@ -27,6 +27,7 @@
>  #include "av1.h"
>  #include "av1_parse.h"
>  #include "bsf.h"
> +#include "bytestream.h"
>  #include "h2645_parse.h"
>  #include "h264.h"
>  #include "hevc.h"
> @@ -87,6 +88,7 @@ static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
>      if (extradata_size && has_seq) {
>          AVBufferRef *filtered_buf;
>          uint8_t *extradata, *filtered_data;
> +        PutByteContext pb_filtered_data, pb_extradata;
>  
>          if (s->remove) {
>              filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
> @@ -108,15 +110,17 @@ static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
>          *data = extradata;
>          *size = extradata_size;
>  
> +        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
> +        if (s->remove)
> +            bytestream2_init_writer(&pb_filtered_data, filtered_data, filtered_size);
> +
>          for (i = 0; i < s->av1_pkt.nb_obus; i++) {
>              AV1OBU *obu = &s->av1_pkt.obus[i];
>              if (val_in_array(extradata_obu_types, nb_extradata_obu_types,
>                               obu->type)) {
> -                memcpy(extradata, obu->raw_data, obu->raw_size);
> -                extradata += obu->raw_size;
> +                bytestream2_put_buffer(&pb_extradata, obu->raw_data, obu->raw_size);
>              } else if (s->remove) {
> -                memcpy(filtered_data, obu->raw_data, obu->raw_size);
> -                filtered_data += obu->raw_size;
> +                bytestream2_put_buffer(&pb_filtered_data, obu->raw_data, obu->raw_size);
>              }
>          }
>  
> @@ -181,6 +185,7 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
>           (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
>          AVBufferRef *filtered_buf;
>          uint8_t *extradata, *filtered_data;
> +        PutByteContext pb_filtered_data, pb_extradata;
>  
>          if (s->remove) {
>              filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
> @@ -202,17 +207,19 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
>          *data = extradata;
>          *size = extradata_size;
>  
> +        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
> +        if (s->remove)
> +            bytestream2_init_writer(&pb_filtered_data, filtered_data, filtered_size);
> +
>          for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
>              H2645NAL *nal = &s->h2645_pkt.nals[i];
>              if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
>                               nal->type)) {
> -                AV_WB24(extradata, 1); // startcode
> -                memcpy(extradata + 3, nal->raw_data, nal->raw_size);
> -                extradata += 3 + nal->raw_size;
> +                bytestream2_put_be24(&pb_extradata, 1); //startcode
> +                bytestream2_put_buffer(&pb_extradata, nal->raw_data, nal->raw_size);
>              } else if (s->remove) {
> -                AV_WB24(filtered_data, 1); // startcode
> -                memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
> -                filtered_data += 3 + nal->raw_size;
> +                bytestream2_put_be24(&pb_filtered_data, 1); // startcode
> +                bytestream2_put_buffer(&pb_filtered_data, nal->raw_data, nal->raw_size);
>              }
>          }
>  
This API has both safe (i.e. it checks whether the output buffer's
size is big enough) as well as unsafe (i.e. it just copies as now)
versions (the unsafe ones end with 'u'). Your patch would add checks,
although the size has already been exactly calculated.

- Andreas

Patch hide | download patch | download mbox

diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c
index ff111de48cb..4a9f6608734 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -27,6 +27,7 @@ 
 #include "av1.h"
 #include "av1_parse.h"
 #include "bsf.h"
+#include "bytestream.h"
 #include "h2645_parse.h"
 #include "h264.h"
 #include "hevc.h"
@@ -87,6 +88,7 @@  static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
     if (extradata_size && has_seq) {
         AVBufferRef *filtered_buf;
         uint8_t *extradata, *filtered_data;
+        PutByteContext pb_filtered_data, pb_extradata;
 
         if (s->remove) {
             filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
@@ -108,15 +110,17 @@  static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
         *data = extradata;
         *size = extradata_size;
 
+        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
+        if (s->remove)
+            bytestream2_init_writer(&pb_filtered_data, filtered_data, filtered_size);
+
         for (i = 0; i < s->av1_pkt.nb_obus; i++) {
             AV1OBU *obu = &s->av1_pkt.obus[i];
             if (val_in_array(extradata_obu_types, nb_extradata_obu_types,
                              obu->type)) {
-                memcpy(extradata, obu->raw_data, obu->raw_size);
-                extradata += obu->raw_size;
+                bytestream2_put_buffer(&pb_extradata, obu->raw_data, obu->raw_size);
             } else if (s->remove) {
-                memcpy(filtered_data, obu->raw_data, obu->raw_size);
-                filtered_data += obu->raw_size;
+                bytestream2_put_buffer(&pb_filtered_data, obu->raw_data, obu->raw_size);
             }
         }
 
@@ -181,6 +185,7 @@  static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
          (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
         AVBufferRef *filtered_buf;
         uint8_t *extradata, *filtered_data;
+        PutByteContext pb_filtered_data, pb_extradata;
 
         if (s->remove) {
             filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
@@ -202,17 +207,19 @@  static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
         *data = extradata;
         *size = extradata_size;
 
+        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
+        if (s->remove)
+            bytestream2_init_writer(&pb_filtered_data, filtered_data, filtered_size);
+
         for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
             H2645NAL *nal = &s->h2645_pkt.nals[i];
             if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
                              nal->type)) {
-                AV_WB24(extradata, 1); // startcode
-                memcpy(extradata + 3, nal->raw_data, nal->raw_size);
-                extradata += 3 + nal->raw_size;
+                bytestream2_put_be24(&pb_extradata, 1); //startcode
+                bytestream2_put_buffer(&pb_extradata, nal->raw_data, nal->raw_size);
             } else if (s->remove) {
-                AV_WB24(filtered_data, 1); // startcode
-                memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
-                filtered_data += 3 + nal->raw_size;
+                bytestream2_put_be24(&pb_filtered_data, 1); // startcode
+                bytestream2_put_buffer(&pb_filtered_data, nal->raw_data, nal->raw_size);
             }
         }