From patchwork Mon Apr 19 14:10:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 27070 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:5014:0:0:0:0:0 with SMTP id e20csp566888iob; Mon, 19 Apr 2021 07:25:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxdeVi8X3OwN/AK+yfWtHqjo6MjZms7YjrJzE15kfgN+9eyZfsApL7THRkit4xlDpb9YG6q X-Received: by 2002:aa7:cf16:: with SMTP id a22mr6328287edy.23.1618842301784; Mon, 19 Apr 2021 07:25:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618842301; cv=none; d=google.com; s=arc-20160816; b=DBfDlhWvzcDEL9k+dTOvmi9Fvviatn8iBGHvTmmVTSPGgSEnT51TTWylZpABY8DQOT a9rzEoJakgVdmpsZMw4rvNu5ld8jVz2OD1bJz5Y9ezwN+uT0zR/elSi3DMDQIFeL+tcp /rmQ0CW6YPnCf2PSsT545i9CGdugX64EtECIKfqca1dSTRA71jHETaPEbIChAxLB+XAd NhYJQ6JIaHroZHCsZCey5Gx9ZJevrduCLpXXImlUJr088aG7m1iR0Dn/gTQvphqUOzIS NVU8Fw6bFdEzKONUVy3n7aTiSupIpodk4F5DNV1OzsjRokuiEoO3AncoFYNr6fq5lYUF eVUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=6G20VCmPgl8yihUaI9aQFu4JWnTS2sUBs5CA0TVWvJ8=; b=Kk8C/d9Jekc6NemQqYgngHSjtOVdpVyaqwAKIR94zuqywlkqcjUU4pyXtgASCiTX8m RobuH4WQCgLD1RpJt7xozx7B7aSyVXaFOlKIICWXKPrbfYA3iW/RZNRmSxU85zg1pEj9 wLlcugqqF9dvGp7I4T+S7UnEt+LMZn5gwyFTvLk43i4Z4QhdRTR0bd2CvAtYM0FZGv5d Ep1RMTT7LbS6d7RzaXDWEsAj5LLIbRh6htlaKQmu4zKW9hbQxWDzJJagVmsSG+M14qnT GxKukMjsYKLhST6dzKa3j+yRNED5kc4BNyyVVEksk1+GFj5bS5Zzdij36kGZsyJWB5fq wMpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=NuigSref; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s21si12976993ejm.689.2021.04.19.07.25.01; Mon, 19 Apr 2021 07:25:01 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=NuigSref; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B5469689F14; Mon, 19 Apr 2021 17:12:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CC114680856 for ; Mon, 19 Apr 2021 17:12:47 +0300 (EEST) Received: by mail-qk1-f176.google.com with SMTP id s5so27075895qkj.5 for ; Mon, 19 Apr 2021 07:12:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=+pxUAhNilDZrFeQMD8P+bwVz3x8o08kWJBtTu9BFAy0=; b=NuigSrefmW+s+Xi6uw3cmprTKWgK5+ZKIB5x4uZNHbj9QTQujBmhgi5/nY0zuqqBgH KOQbqD04RmGHzI9yP/HSNTpfEwiWsqKPiTY/1Guoc7EXAMfy7646FhVyKBKrDj50tl8l jpJAGEOUm0EklwxxRp3rhwY5gLK7cE8dXVewwsPNjU/2llScND0SMoAhJy/shcg5xo5n UfDObTT5XTaDumg1KE1Vq6BEnhrrE7MurNbVJeTCA254xrrfIUdAw5UCgiaxPj6OVFNE 52yxBsAgCbiKq64VoBgsu8u0TyZuz2Ru91IGsTt8kThY8/s0Oqus316dx5aCAIl3g4k5 WntQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+pxUAhNilDZrFeQMD8P+bwVz3x8o08kWJBtTu9BFAy0=; b=o+/Fvo08xnbjHjy+tq2Gtk/weg7shQ1YLEm685Pph2ai4k07Dsgik93U9JKe/vWT71 SVuNWCrh7H9jam64FpyRpXF+Vq5ab2/SSVqlR5HfnS31lD8WxqY36vkByy77MN2pouvm nJTyfofOu0Tt8aPpK+vzLZg4SRX7s3rrRlfmLXXnWB8gFVBzJrf3ENDzc3wLJWg4NAZt eBHm3YC3muwwkGRXcoX2y3tzjUbnZA3n6bZ2wxpu02VSqbYdiFg9EeesZX3JZ7h4TAP/ Qz3dkNX72xsH6kO5iVWWci4GwMdonKPX0sqIfeU9MoZp0LsP3EHO9cZP0b81KgSCWZ5d cV+g== X-Gm-Message-State: AOAM533DPYXR5Vjqcqb5RXyMVPdMWcndtXpIcFAue+Txk6Rhj/KWplLs K7YjLuDu4MXyBiBmdE/vxJpUBuUMVWI= X-Received: by 2002:a05:620a:1181:: with SMTP id b1mr12061998qkk.81.1618841566209; Mon, 19 Apr 2021 07:12:46 -0700 (PDT) Received: from localhost.localdomain ([191.83.209.133]) by smtp.gmail.com with ESMTPSA id f7sm2049494qtv.53.2021.04.19.07.12.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 07:12:45 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 19 Apr 2021 11:10:18 -0300 Message-Id: <20210419141024.8174-82-jamrial@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210419141024.8174-1-jamrial@gmail.com> References: <20210419141024.8174-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 81/87] avcodec: Move all AVCodecParser.split functions to remove_extradata_bsf X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: QpXgAOQunpcQ From: Andreas Rheinhardt The remove_extradata bsf is the only user of these functions. Signed-off-by: Andreas Rheinhardt --- libavcodec/Makefile | 4 +- libavcodec/av1_parser.c | 25 +--- libavcodec/avs2_parser.c | 1 - libavcodec/avs3_parser.c | 1 - libavcodec/cavs_parser.c | 1 - libavcodec/h264_parser.c | 38 ------ libavcodec/hevc_parser.c | 34 ----- libavcodec/mpeg4video_parser.c | 1 - libavcodec/mpegvideo_parser.c | 18 --- libavcodec/parser.c | 14 --- libavcodec/remove_extradata_bsf.c | 201 +++++++++++++++++++++++++----- libavcodec/vc1_parser.c | 19 --- 12 files changed, 171 insertions(+), 186 deletions(-) diff --git a/libavcodec/Makefile b/libavcodec/Makefile index a640c548e5..c19a3cb60d 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1075,7 +1075,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 @@ -1159,7 +1159,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 diff --git a/libavcodec/av1_parser.c b/libavcodec/av1_parser.c index 578f5293c8..b6c8004ee3 100644 --- a/libavcodec/av1_parser.c +++ b/libavcodec/av1_parser.c @@ -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, }; diff --git a/libavcodec/avs2_parser.c b/libavcodec/avs2_parser.c index 059faf77c5..b7d5d7774e 100644 --- a/libavcodec/avs2_parser.c +++ b/libavcodec/avs2_parser.c @@ -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, }; diff --git a/libavcodec/avs3_parser.c b/libavcodec/avs3_parser.c index b0e720a844..1a05ea042e 100644 --- a/libavcodec/avs3_parser.c +++ b/libavcodec/avs3_parser.c @@ -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, }; diff --git a/libavcodec/cavs_parser.c b/libavcodec/cavs_parser.c index 20adca1dbc..03f392c2e5 100644 --- a/libavcodec/cavs_parser.c +++ b/libavcodec/cavs_parser.c @@ -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, }; diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c index 880ccb50fa..d3c56cc188 100644 --- a/libavcodec/h264_parser.c +++ b/libavcodec/h264_parser.c @@ -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, }; diff --git a/libavcodec/hevc_parser.c b/libavcodec/hevc_parser.c index 320d4eb562..c944a6aacd 100644 --- a/libavcodec/hevc_parser.c +++ b/libavcodec/hevc_parser.c @@ -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, }; diff --git a/libavcodec/mpeg4video_parser.c b/libavcodec/mpeg4video_parser.c index afa10c7727..1b0e2555da 100644 --- a/libavcodec/mpeg4video_parser.c +++ b/libavcodec/mpeg4video_parser.c @@ -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, }; diff --git a/libavcodec/mpegvideo_parser.c b/libavcodec/mpegvideo_parser.c index 4e4f569b13..7864224643 100644 --- a/libavcodec/mpegvideo_parser.c +++ b/libavcodec/mpegvideo_parser.c @@ -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= 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, }; diff --git a/libavcodec/parser.c b/libavcodec/parser.c index f81a62d592..fc57246965 100644 --- a/libavcodec/parser.c +++ b/libavcodec/parser.c @@ -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; -} diff --git a/libavcodec/remove_extradata_bsf.c b/libavcodec/remove_extradata_bsf.c index 5783b075f0..1d5f193f89 100644 --- a/libavcodec/remove_extradata_bsf.c +++ b/libavcodec/remove_extradata_bsf.c @@ -21,9 +21,13 @@ #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, }; diff --git a/libavcodec/vc1_parser.c b/libavcodec/vc1_parser.c index 0f17d299e1..00896ddb09 100644 --- a/libavcodec/vc1_parser.c +++ b/libavcodec/vc1_parser.c @@ -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, };