mpeg2_metadata_bsf: Use common cbs bsf implementation

This also adds support for updating new extradata.
This commit is contained in:
Mark Thompson 2021-01-01 21:35:20 +00:00
parent b95f901c7f
commit f35b268327
1 changed files with 12 additions and 82 deletions

View File

@ -21,16 +21,13 @@
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "bsf.h" #include "bsf.h"
#include "bsf_internal.h"
#include "cbs.h" #include "cbs.h"
#include "cbs_bsf.h"
#include "cbs_mpeg2.h" #include "cbs_mpeg2.h"
#include "mpeg12.h" #include "mpeg12.h"
typedef struct MPEG2MetadataContext { typedef struct MPEG2MetadataContext {
const AVClass *class; CBSBSFContext common;
CodedBitstreamContext *cbc;
CodedBitstreamFragment fragment;
MPEG2RawExtensionData sequence_display_extension; MPEG2RawExtensionData sequence_display_extension;
@ -48,6 +45,7 @@ typedef struct MPEG2MetadataContext {
static int mpeg2_metadata_update_fragment(AVBSFContext *bsf, static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
AVPacket *pkt,
CodedBitstreamFragment *frag) CodedBitstreamFragment *frag)
{ {
MPEG2MetadataContext *ctx = bsf->priv_data; MPEG2MetadataContext *ctx = bsf->priv_data;
@ -170,49 +168,16 @@ static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
return 0; return 0;
} }
static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *pkt) static const CBSBSFType mpeg2_metadata_type = {
{ .codec_id = AV_CODEC_ID_MPEG2VIDEO,
MPEG2MetadataContext *ctx = bsf->priv_data; .fragment_name = "frame",
CodedBitstreamFragment *frag = &ctx->fragment; .unit_name = "start code",
int err; .update_fragment = &mpeg2_metadata_update_fragment,
};
err = ff_bsf_get_packet_ref(bsf, pkt);
if (err < 0)
return err;
err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
goto fail;
}
err = mpeg2_metadata_update_fragment(bsf, frag);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to update frame fragment.\n");
goto fail;
}
err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
goto fail;
}
err = 0;
fail:
ff_cbs_fragment_reset(frag);
if (err < 0)
av_packet_unref(pkt);
return err;
}
static int mpeg2_metadata_init(AVBSFContext *bsf) static int mpeg2_metadata_init(AVBSFContext *bsf)
{ {
MPEG2MetadataContext *ctx = bsf->priv_data; MPEG2MetadataContext *ctx = bsf->priv_data;
CodedBitstreamFragment *frag = &ctx->fragment;
int err;
#define VALIDITY_CHECK(name) do { \ #define VALIDITY_CHECK(name) do { \
if (!ctx->name) { \ if (!ctx->name) { \
@ -226,42 +191,7 @@ static int mpeg2_metadata_init(AVBSFContext *bsf)
VALIDITY_CHECK(matrix_coefficients); VALIDITY_CHECK(matrix_coefficients);
#undef VALIDITY_CHECK #undef VALIDITY_CHECK
err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_MPEG2VIDEO, bsf); return ff_cbs_bsf_generic_init(bsf, &mpeg2_metadata_type);
if (err < 0)
return err;
if (bsf->par_in->extradata) {
err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
goto fail;
}
err = mpeg2_metadata_update_fragment(bsf, frag);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to update metadata fragment.\n");
goto fail;
}
err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
goto fail;
}
}
err = 0;
fail:
ff_cbs_fragment_reset(frag);
return err;
}
static void mpeg2_metadata_close(AVBSFContext *bsf)
{
MPEG2MetadataContext *ctx = bsf->priv_data;
ff_cbs_fragment_free(&ctx->fragment);
ff_cbs_close(&ctx->cbc);
} }
#define OFFSET(x) offsetof(MPEG2MetadataContext, x) #define OFFSET(x) offsetof(MPEG2MetadataContext, x)
@ -307,7 +237,7 @@ const AVBitStreamFilter ff_mpeg2_metadata_bsf = {
.priv_data_size = sizeof(MPEG2MetadataContext), .priv_data_size = sizeof(MPEG2MetadataContext),
.priv_class = &mpeg2_metadata_class, .priv_class = &mpeg2_metadata_class,
.init = &mpeg2_metadata_init, .init = &mpeg2_metadata_init,
.close = &mpeg2_metadata_close, .close = &ff_cbs_bsf_generic_close,
.filter = &mpeg2_metadata_filter, .filter = &ff_cbs_bsf_generic_filter,
.codec_ids = mpeg2_metadata_codec_ids, .codec_ids = mpeg2_metadata_codec_ids,
}; };