]> git.sesse.net Git - ffmpeg/commitdiff
avcodec: Move all AVCodecParser.split functions to remove_extradata_bsf
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Sat, 6 Mar 2021 23:28:14 +0000 (00:28 +0100)
committerJames Almer <jamrial@gmail.com>
Tue, 27 Apr 2021 13:43:16 +0000 (10:43 -0300)
The remove_extradata bsf is the only user of these functions.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
12 files changed:
libavcodec/Makefile
libavcodec/av1_parser.c
libavcodec/avs2_parser.c
libavcodec/avs3_parser.c
libavcodec/cavs_parser.c
libavcodec/h264_parser.c
libavcodec/hevc_parser.c
libavcodec/mpeg4video_parser.c
libavcodec/mpegvideo_parser.c
libavcodec/parser.c
libavcodec/remove_extradata_bsf.c
libavcodec/vc1_parser.c

index 05583283ac391da967e96499c057225a36fd1cce..47b31817c8913f530545f1be422d21e6f97aee29 100644 (file)
@@ -1073,7 +1073,7 @@ OBJS-$(CONFIG_AAC_PARSER)              += aac_parser.o aac_ac3_parser.o \
                                           mpeg4audio.o
 OBJS-$(CONFIG_AC3_PARSER)              += ac3tab.o aac_ac3_parser.o
 OBJS-$(CONFIG_ADX_PARSER)              += adx_parser.o adx.o
-OBJS-$(CONFIG_AV1_PARSER)              += av1_parser.o av1_parse.o
+OBJS-$(CONFIG_AV1_PARSER)              += av1_parser.o
 OBJS-$(CONFIG_AVS2_PARSER)             += avs2_parser.o
 OBJS-$(CONFIG_AVS3_PARSER)             += avs3_parser.o
 OBJS-$(CONFIG_BMP_PARSER)              += bmp_parser.o
@@ -1157,7 +1157,7 @@ OBJS-$(CONFIG_NULL_BSF)                   += null_bsf.o
 OBJS-$(CONFIG_OPUS_METADATA_BSF)          += opus_metadata_bsf.o
 OBJS-$(CONFIG_PCM_RECHUNK_BSF)            += pcm_rechunk_bsf.o
 OBJS-$(CONFIG_PRORES_METADATA_BSF)        += prores_metadata_bsf.o
-OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF)       += remove_extradata_bsf.o
+OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF)       += remove_extradata_bsf.o av1_parse.o
 OBJS-$(CONFIG_SETTS_BSF)                  += setts_bsf.o
 OBJS-$(CONFIG_TEXT2MOVSUB_BSF)            += movsub_bsf.o
 OBJS-$(CONFIG_TRACE_HEADERS_BSF)          += trace_headers_bsf.o
index 578f5293c87afd56f9c0c5c0b7f1d96f3157cb0a..b6c8004ee3d814632bf4eb7804ec1042f02c8ccc 100644 (file)
@@ -20,7 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "av1_parse.h"
+#include "libavutil/avassert.h"
 #include "cbs.h"
 #include "cbs_av1.h"
 #include "internal.h"
@@ -205,33 +205,10 @@ static void av1_parser_close(AVCodecParserContext *ctx)
     ff_cbs_close(&s->cbc);
 }
 
-static int av1_parser_split(AVCodecContext *avctx,
-                            const uint8_t *buf, int buf_size)
-{
-    AV1OBU obu;
-    const uint8_t *ptr = buf, *end = buf + buf_size;
-
-    while (ptr < end) {
-        int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
-        if (len < 0)
-            break;
-
-        if (obu.type == AV1_OBU_FRAME_HEADER ||
-            obu.type == AV1_OBU_FRAME) {
-            return ptr - buf;
-        }
-        ptr      += len;
-        buf_size -= len;
-    }
-
-    return 0;
-}
-
 const AVCodecParser ff_av1_parser = {
     .codec_ids      = { AV_CODEC_ID_AV1 },
     .priv_data_size = sizeof(AV1ParseContext),
     .parser_init    = av1_parser_init,
     .parser_close   = av1_parser_close,
     .parser_parse   = av1_parser_parse,
-    .split          = av1_parser_split,
 };
index 059faf77c5e733519ceaf3fe4ee8544443d20dd7..b7d5d7774e0d9a7914bcf8e0c2783e77fcf9dd25 100644 (file)
@@ -91,5 +91,4 @@ const AVCodecParser ff_avs2_parser = {
     .priv_data_size = sizeof(ParseContext),
     .parser_parse   = avs2_parse,
     .parser_close   = ff_parse_close,
-    .split          = ff_mpeg4video_split,
 };
index b0e720a844f531290a1ea1beec4c94ba84dc529a..1a05ea042e14e54c50f00950aeadc55fb91c1d79 100644 (file)
@@ -175,5 +175,4 @@ const AVCodecParser ff_avs3_parser = {
     .priv_data_size = sizeof(ParseContext),
     .parser_parse   = avs3_parse,
     .parser_close   = ff_parse_close,
-    .split          = ff_mpeg4video_split,
 };
index 20adca1dbc8d698ebc2a4c6f380546efe44a7e39..03f392c2e5a7567052078bfdbb979cb5554dd264 100644 (file)
@@ -102,5 +102,4 @@ const AVCodecParser ff_cavsvideo_parser = {
     .priv_data_size = sizeof(ParseContext),
     .parser_parse   = cavsvideo_parse,
     .parser_close   = ff_parse_close,
-    .split          = ff_mpeg4video_split,
 };
index 880ccb50fa79394b0899fba84452cb04bfabd411..d3c56cc18855a8c55e6df7144b3dba7515f8bb7d 100644 (file)
@@ -644,43 +644,6 @@ static int h264_parse(AVCodecParserContext *s,
     return next;
 }
 
-static int h264_split(AVCodecContext *avctx,
-                      const uint8_t *buf, int buf_size)
-{
-    uint32_t state = -1;
-    int has_sps    = 0;
-    int has_pps    = 0;
-    const uint8_t *ptr = buf, *end = buf + buf_size;
-    int nalu_type;
-
-    while (ptr < end) {
-        ptr = avpriv_find_start_code(ptr, end, &state);
-        if ((state & 0xFFFFFF00) != 0x100)
-            break;
-        nalu_type = state & 0x1F;
-        if (nalu_type == H264_NAL_SPS) {
-            has_sps = 1;
-        } else if (nalu_type == H264_NAL_PPS)
-            has_pps = 1;
-        /* else if (nalu_type == 0x01 ||
-         *     nalu_type == 0x02 ||
-         *     nalu_type == 0x05) {
-         *  }
-         */
-        else if ((nalu_type != H264_NAL_SEI || has_pps) &&
-                  nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
-                  nalu_type != 0x0f) {
-            if (has_sps) {
-                while (ptr - 4 > buf && ptr[-5] == 0)
-                    ptr--;
-                return ptr - 4 - buf;
-            }
-        }
-    }
-
-    return 0;
-}
-
 static void h264_close(AVCodecParserContext *s)
 {
     H264ParseContext *p = s->priv_data;
@@ -708,5 +671,4 @@ const AVCodecParser ff_h264_parser = {
     .parser_init    = init,
     .parser_parse   = h264_parse,
     .parser_close   = h264_close,
-    .split          = h264_split,
 };
index 320d4eb56238ee9ad75f03d9b115be9576c79c42..c944a6aacd8d2e7143d60f73412bcbdae54461f2 100644 (file)
@@ -336,39 +336,6 @@ static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
     return next;
 }
 
-// Split after the parameter sets at the beginning of the stream if they exist.
-static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
-{
-    const uint8_t *ptr = buf, *end = buf + buf_size;
-    uint32_t state = -1;
-    int has_vps = 0;
-    int has_sps = 0;
-    int has_pps = 0;
-    int nut;
-
-    while (ptr < end) {
-        ptr = avpriv_find_start_code(ptr, end, &state);
-        if ((state >> 8) != START_CODE)
-            break;
-        nut = (state >> 1) & 0x3F;
-        if (nut == HEVC_NAL_VPS)
-            has_vps = 1;
-        else if (nut == HEVC_NAL_SPS)
-            has_sps = 1;
-        else if (nut == HEVC_NAL_PPS)
-            has_pps = 1;
-        else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
-                  nut != HEVC_NAL_AUD) {
-            if (has_vps && has_sps) {
-                while (ptr - 4 > buf && ptr[-5] == 0)
-                    ptr--;
-                return ptr - 4 - buf;
-            }
-        }
-    }
-    return 0;
-}
-
 static void hevc_parser_close(AVCodecParserContext *s)
 {
     HEVCParserContext *ctx = s->priv_data;
@@ -385,5 +352,4 @@ const AVCodecParser ff_hevc_parser = {
     .priv_data_size = sizeof(HEVCParserContext),
     .parser_parse   = hevc_parse,
     .parser_close   = hevc_parser_close,
-    .split          = hevc_split,
 };
index afa10c772791731ff7c72faae053d48a1dff97f5..1b0e2555da580f52a0660da0efd6e1054d974ac1 100644 (file)
@@ -159,5 +159,4 @@ const AVCodecParser ff_mpeg4video_parser = {
     .parser_init    = mpeg4video_parse_init,
     .parser_parse   = mpeg4video_parse,
     .parser_close   = ff_parse_close,
-    .split          = ff_mpeg4video_split,
 };
index 4e4f569b13186bd78567ffc57cc79608e276aee7..78642246434433455f165df0decfbab3ec61fd54 100644 (file)
@@ -213,23 +213,6 @@ static int mpegvideo_parse(AVCodecParserContext *s,
     return next;
 }
 
-static int mpegvideo_split(AVCodecContext *avctx,
-                           const uint8_t *buf, int buf_size)
-{
-    int i;
-    uint32_t state= -1;
-    int found=0;
-
-    for(i=0; i<buf_size; i++){
-        state= (state<<8) | buf[i];
-        if(state == 0x1B3){
-            found=1;
-        }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
-            return i-3;
-    }
-    return 0;
-}
-
 static int mpegvideo_parse_init(AVCodecParserContext *s)
 {
     s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
@@ -242,5 +225,4 @@ const AVCodecParser ff_mpegvideo_parser = {
     .parser_init    = mpegvideo_parse_init,
     .parser_parse   = mpegvideo_parse,
     .parser_close   = ff_parse_close,
-    .split          = mpegvideo_split,
 };
index f81a62d5920e2d8b4b39615417aa9db269a4d1be..fc57246965ccd2f149ea1c05579f58583daaa381 100644 (file)
@@ -285,17 +285,3 @@ void ff_parse_close(AVCodecParserContext *s)
 
     av_freep(&pc->buffer);
 }
-
-int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
-{
-    uint32_t state = -1;
-    const uint8_t *ptr = buf, *end = buf + buf_size;
-
-    while (ptr < end) {
-        ptr = avpriv_find_start_code(ptr, end, &state);
-        if (state == 0x1B3 || state == 0x1B6)
-            return ptr - 4 - buf;
-    }
-
-    return 0;
-}
index 5783b075f017a2d4fcbeace2000956307639f9b5..1d5f193f89de32ee5d4f1d1271052d196660cbec 100644 (file)
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 
-#include "avcodec.h"
+#include "av1_parse.h"
 #include "bsf.h"
 #include "bsf_internal.h"
+#include "h264.h"
+#include "hevc.h"
+#include "internal.h"
+#include "vc1_common.h"
 
 enum RemoveFreq {
     REMOVE_FREQ_KEYFRAME,
@@ -31,63 +35,196 @@ enum RemoveFreq {
     REMOVE_FREQ_NONKEYFRAME,
 };
 
+#define START_CODE 0x000001
+
 typedef struct RemoveExtradataContext {
     const AVClass *class;
     int freq;
-
-    AVCodecParserContext *parser;
-    AVCodecContext *avctx;
 } RemoveExtradataContext;
 
-static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
+static int av1_split(const uint8_t *buf, int buf_size, void *logctx)
 {
-    RemoveExtradataContext *s = ctx->priv_data;
+    AV1OBU obu;
+    const uint8_t *ptr = buf, *end = buf + buf_size;
 
-    int ret;
+    while (ptr < end) {
+        int len = ff_av1_extract_obu(&obu, ptr, buf_size, logctx);
+        if (len < 0)
+            break;
 
-    ret = ff_bsf_get_packet_ref(ctx, pkt);
-    if (ret < 0)
-        return ret;
+        if (obu.type == AV1_OBU_FRAME_HEADER ||
+            obu.type == AV1_OBU_FRAME) {
+            return ptr - buf;
+        }
+        ptr      += len;
+        buf_size -= len;
+    }
+
+    return 0;
+}
 
-    if (s->parser && s->parser->parser->split) {
-        if (s->freq == REMOVE_FREQ_ALL ||
-            (s->freq == REMOVE_FREQ_NONKEYFRAME && !(pkt->flags & AV_PKT_FLAG_KEY)) ||
-            (s->freq == REMOVE_FREQ_KEYFRAME && pkt->flags & AV_PKT_FLAG_KEY)) {
-            int i = s->parser->parser->split(s->avctx, pkt->data, pkt->size);
-            pkt->data += i;
-            pkt->size -= i;
+static int h264_split(const uint8_t *buf, int buf_size)
+{
+    const uint8_t *ptr = buf, *end = buf + buf_size;
+    uint32_t state = -1;
+    int has_sps    = 0;
+    int has_pps    = 0;
+    int nalu_type;
+
+    while (ptr < end) {
+        ptr = avpriv_find_start_code(ptr, end, &state);
+        if ((state & 0xFFFFFF00) != 0x100)
+            break;
+        nalu_type = state & 0x1F;
+        if (nalu_type == H264_NAL_SPS) {
+            has_sps = 1;
+        } else if (nalu_type == H264_NAL_PPS)
+            has_pps = 1;
+        /* else if (nalu_type == 0x01 ||
+         *     nalu_type == 0x02 ||
+         *     nalu_type == 0x05) {
+         *  }
+         */
+        else if ((nalu_type != H264_NAL_SEI || has_pps) &&
+                  nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
+                  nalu_type != 0x0f) {
+            if (has_sps) {
+                while (ptr - 4 > buf && ptr[-5] == 0)
+                    ptr--;
+                return ptr - 4 - buf;
+            }
         }
     }
 
     return 0;
 }
 
-static int remove_extradata_init(AVBSFContext *ctx)
+// Split after the parameter sets at the beginning of the stream if they exist.
+static int hevc_split(const uint8_t *buf, int buf_size)
 {
-    RemoveExtradataContext *s = ctx->priv_data;
-    int ret;
+    const uint8_t *ptr = buf, *end = buf + buf_size;
+    uint32_t state = -1;
+    int has_vps = 0;
+    int has_sps = 0;
+    int has_pps = 0;
+    int nut;
+
+    while (ptr < end) {
+        ptr = avpriv_find_start_code(ptr, end, &state);
+        if ((state >> 8) != START_CODE)
+            break;
+        nut = (state >> 1) & 0x3F;
+        if (nut == HEVC_NAL_VPS)
+            has_vps = 1;
+        else if (nut == HEVC_NAL_SPS)
+            has_sps = 1;
+        else if (nut == HEVC_NAL_PPS)
+            has_pps = 1;
+        else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
+                  nut != HEVC_NAL_AUD) {
+            if (has_vps && has_sps) {
+                while (ptr - 4 > buf && ptr[-5] == 0)
+                    ptr--;
+                return ptr - 4 - buf;
+            }
+        }
+    }
+    return 0;
+}
 
-    s->parser = av_parser_init(ctx->par_in->codec_id);
+static int mpegvideo_split(const uint8_t *buf, int buf_size)
+{
+    uint32_t state = -1;
+    int found = 0;
+
+    for (int i = 0; i < buf_size; i++) {
+        state = (state << 8) | buf[i];
+        if (state == 0x1B3) {
+            found = 1;
+        } else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100)
+            return i - 3;
+    }
+    return 0;
+}
 
-    if (s->parser) {
-        s->avctx = avcodec_alloc_context3(NULL);
-        if (!s->avctx)
-            return AVERROR(ENOMEM);
+static int mpeg4video_split(const uint8_t *buf, int buf_size)
+{
+    const uint8_t *ptr = buf, *end = buf + buf_size;
+    uint32_t state = -1;
 
-        ret = avcodec_parameters_to_context(s->avctx, ctx->par_in);
-        if (ret < 0)
-            return ret;
+    while (ptr < end) {
+        ptr = avpriv_find_start_code(ptr, end, &state);
+        if (state == 0x1B3 || state == 0x1B6)
+            return ptr - 4 - buf;
     }
 
     return 0;
 }
 
-static void remove_extradata_close(AVBSFContext *ctx)
+static int vc1_split(const uint8_t *buf, int buf_size)
+{
+    const uint8_t *ptr = buf, *end = buf + buf_size;
+    uint32_t state = -1;
+    int charged = 0;
+
+    while (ptr < end) {
+        ptr = avpriv_find_start_code(ptr, end, &state);
+        if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
+            charged = 1;
+        } else if (charged && IS_MARKER(state))
+            return ptr - 4 - buf;
+    }
+
+    return 0;
+}
+
+static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
 {
     RemoveExtradataContext *s = ctx->priv_data;
 
-    avcodec_free_context(&s->avctx);
-    av_parser_close(s->parser);
+    int ret;
+
+    ret = ff_bsf_get_packet_ref(ctx, pkt);
+    if (ret < 0)
+        return ret;
+
+    if (s->freq == REMOVE_FREQ_ALL ||
+        (s->freq == REMOVE_FREQ_NONKEYFRAME && !(pkt->flags & AV_PKT_FLAG_KEY)) ||
+        (s->freq == REMOVE_FREQ_KEYFRAME && pkt->flags & AV_PKT_FLAG_KEY)) {
+        int i;
+
+        switch (ctx->par_in->codec_id) {
+        case AV_CODEC_ID_AV1:
+            i = av1_split(pkt->data, pkt->size, ctx);
+            break;
+        case AV_CODEC_ID_AVS2:
+        case AV_CODEC_ID_AVS3:
+        case AV_CODEC_ID_CAVS:
+        case AV_CODEC_ID_MPEG4:
+            i = mpeg4video_split(pkt->data, pkt->size);
+            break;
+        case AV_CODEC_ID_H264:
+            i = h264_split(pkt->data, pkt->size);
+            break;
+        case AV_CODEC_ID_HEVC:
+            i = hevc_split(pkt->data, pkt->size);
+            break;
+        case AV_CODEC_ID_MPEG1VIDEO:
+        case AV_CODEC_ID_MPEG2VIDEO:
+            i = mpegvideo_split(pkt->data, pkt->size);
+            break;
+        case AV_CODEC_ID_VC1:
+            i = vc1_split(pkt->data, pkt->size);
+            break;
+        default:
+            i = 0;
+        }
+
+        pkt->data += i;
+        pkt->size -= i;
+    }
+
+    return 0;
 }
 
 #define OFFSET(x) offsetof(RemoveExtradataContext, x)
@@ -112,7 +249,5 @@ const AVBitStreamFilter ff_remove_extradata_bsf = {
     .name           = "remove_extra",
     .priv_data_size = sizeof(RemoveExtradataContext),
     .priv_class     = &remove_extradata_class,
-    .init           = remove_extradata_init,
-    .close          = remove_extradata_close,
     .filter         = remove_extradata,
 };
index 0f17d299e103631e5588a0311d900f0b6c4158ef..00896ddb096c463027f97eac3311146f89be53f1 100644 (file)
@@ -256,24 +256,6 @@ static int vc1_parse(AVCodecParserContext *s,
     return next;
 }
 
-static int vc1_split(AVCodecContext *avctx,
-                           const uint8_t *buf, int buf_size)
-{
-    uint32_t state = -1;
-    int charged = 0;
-    const uint8_t *ptr = buf, *end = buf + buf_size;
-
-    while (ptr < end) {
-        ptr = avpriv_find_start_code(ptr, end, &state);
-        if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
-            charged = 1;
-        } else if (charged && IS_MARKER(state))
-            return ptr - 4 - buf;
-    }
-
-    return 0;
-}
-
 static av_cold int vc1_parse_init(AVCodecParserContext *s)
 {
     VC1ParseContext *vpc = s->priv_data;
@@ -293,5 +275,4 @@ const AVCodecParser ff_vc1_parser = {
     .parser_init    = vc1_parse_init,
     .parser_parse   = vc1_parse,
     .parser_close   = ff_parse_close,
-    .split          = vc1_split,
 };