[8/9] avcodec/extract_extradata: add support for AV1

Message ID 20181003191538.33448-9-lu_zero@gentoo.org
State New
Headers show
Series
  • [1/9] avcodec/bsf: Add ff_bsf_get_packet_ref() function
Related show

Commit Message

Luca Barbato Oct. 3, 2018, 7:15 p.m.
From: James Almer <jamrial@gmail.com>

Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
---
 libavcodec/Makefile                |  2 +-
 libavcodec/extract_extradata_bsf.c | 80 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 1 deletion(-)

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ea0c9dceae..3f6ecb2d13 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -781,7 +781,7 @@  OBJS-$(CONFIG_AAC_ADTSTOASC_BSF)          += aac_adtstoasc_bsf.o mpeg4audio.o
 OBJS-$(CONFIG_CHOMP_BSF)                  += chomp_bsf.o
 OBJS-$(CONFIG_DUMP_EXTRADATA_BSF)         += dump_extradata_bsf.o
 OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF)      += extract_extradata_bsf.o    \
-                                             h2645_parse.o
+                                             av1_parse.o h2645_parse.o
 OBJS-$(CONFIG_H264_METADATA_BSF)          += h264_metadata_bsf.o
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)       += h264_mp4toannexb_bsf.o
 OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF)     += h264_redundant_pps_bsf.o
diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c
index 0094d11496..2734a7b7d1 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -24,6 +24,8 @@ 
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
+#include "av1.h"
+#include "av1_parse.h"
 #include "bsf.h"
 #include "h2645_parse.h"
 #include "h264.h"
@@ -36,6 +38,9 @@  typedef struct ExtractExtradataContext {
     int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
                    uint8_t **data, int *size);
 
+    /* AV1 specifc fields */
+    AV1Packet av1_pkt;
+
     /* H264/HEVC specifc fields */
     H2645Packet h2645_pkt;
 
@@ -52,6 +57,78 @@  static int val_in_array(const int *arr, int len, int val)
     return 0;
 }
 
+static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
+                                 uint8_t **data, int *size)
+{
+    static const int extradata_obu_types[] = {
+        AV1_OBU_SEQUENCE_HEADER, AV1_OBU_METADATA,
+    };
+    ExtractExtradataContext *s = ctx->priv_data;
+
+    int extradata_size = 0, filtered_size = 0;
+    int nb_extradata_obu_types = FF_ARRAY_ELEMS(extradata_obu_types);
+    int i, ret = 0;
+
+    ret = ff_av1_packet_split(&s->av1_pkt, pkt->data, pkt->size, ctx);
+    if (ret < 0)
+        return ret;
+
+    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)) {
+            extradata_size += obu->raw_size;
+        } else if (s->remove) {
+            filtered_size += obu->raw_size;
+        }
+    }
+
+    if (extradata_size) {
+        AVBufferRef *filtered_buf;
+        uint8_t *extradata, *filtered_data;
+
+        if (s->remove) {
+            filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
+            if (!filtered_buf) {
+                return AVERROR(ENOMEM);
+            }
+            memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+
+            filtered_data = filtered_buf->data;
+        }
+
+        extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
+        if (!extradata) {
+            av_buffer_unref(&filtered_buf);
+            return AVERROR(ENOMEM);
+        }
+        memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+
+        *data = extradata;
+        *size = extradata_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;
+            } else if (s->remove) {
+                memcpy(filtered_data, obu->raw_data, obu->raw_size);
+                filtered_data += obu->raw_size;
+            }
+        }
+
+        if (s->remove) {
+            av_buffer_unref(&pkt->buf);
+            pkt->buf  = filtered_buf;
+            pkt->data = filtered_buf->data;
+            pkt->size = filtered_size;
+        }
+    }
+
+    return 0;
+}
+
 static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
                                    uint8_t **data, int *size)
 {
@@ -225,6 +302,7 @@  static const struct {
     int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
                    uint8_t **data, int *size);
 } extract_tab[] = {
+    { AV_CODEC_ID_AV1,        extract_extradata_av1     },
     { AV_CODEC_ID_CAVS,       extract_extradata_mpeg124 },
     { AV_CODEC_ID_H264,       extract_extradata_h2645   },
     { AV_CODEC_ID_HEVC,       extract_extradata_h2645   },
@@ -285,10 +363,12 @@  fail:
 static void extract_extradata_close(AVBSFContext *ctx)
 {
     ExtractExtradataContext *s = ctx->priv_data;
+    ff_av1_packet_uninit(&s->av1_pkt);
     ff_h2645_packet_uninit(&s->h2645_pkt);
 }
 
 static const enum AVCodecID codec_ids[] = {
+    AV_CODEC_ID_AV1,
     AV_CODEC_ID_CAVS,
     AV_CODEC_ID_H264,
     AV_CODEC_ID_HEVC,