1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec: Move all AVCodecParser.split functions to remove_extradata_bsf

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>
This commit is contained in:
Andreas Rheinhardt 2021-03-07 00:28:14 +01:00 committed by James Almer
parent e625ae6092
commit e5af920309
12 changed files with 179 additions and 194 deletions

View 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

View 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,
};

View 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,
};

View 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,
};

View 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,
};

View 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,
};

View 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,
};

View 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,
};

View 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,
};

View 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;
}

View File

@ -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,14 +35,149 @@ enum RemoveFreq {
REMOVE_FREQ_NONKEYFRAME,
};
#define START_CODE 0x000001
typedef struct RemoveExtradataContext {
const AVClass *class;
int freq;
AVCodecParserContext *parser;
AVCodecContext *avctx;
} RemoveExtradataContext;
static int av1_split(const uint8_t *buf, int buf_size, void *logctx)
{
AV1OBU obu;
const uint8_t *ptr = buf, *end = buf + buf_size;
while (ptr < end) {
int len = ff_av1_extract_obu(&obu, ptr, buf_size, logctx);
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;
}
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;
}
// 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)
{
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 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;
}
static int mpeg4video_split(const uint8_t *buf, int buf_size)
{
const uint8_t *ptr = buf, *end = buf + buf_size;
uint32_t state = -1;
while (ptr < end) {
ptr = avpriv_find_start_code(ptr, end, &state);
if (state == 0x1B3 || state == 0x1B6)
return ptr - 4 - buf;
}
return 0;
}
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;
@ -49,47 +188,45 @@ static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
if (ret < 0)
return ret;
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;
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;
}
static int remove_extradata_init(AVBSFContext *ctx)
{
RemoveExtradataContext *s = ctx->priv_data;
int ret;
s->parser = av_parser_init(ctx->par_in->codec_id);
if (s->parser) {
s->avctx = avcodec_alloc_context3(NULL);
if (!s->avctx)
return AVERROR(ENOMEM);
ret = avcodec_parameters_to_context(s->avctx, ctx->par_in);
if (ret < 0)
return ret;
}
return 0;
}
static void remove_extradata_close(AVBSFContext *ctx)
{
RemoveExtradataContext *s = ctx->priv_data;
avcodec_free_context(&s->avctx);
av_parser_close(s->parser);
}
#define OFFSET(x) offsetof(RemoveExtradataContext, x)
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
static const AVOption options[] = {
@ -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,
};

View 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,
};