From: Aman Karmani <ffmpegagent@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Aman Karmani <aman@tmm1.net>
Subject: [FFmpeg-devel] [PATCH 4/5] avcodec/h264_metadata_bsf: add support for a/53 closed captions
Date: Sat, 04 Feb 2023 00:41:54 +0000
Message-ID: <d79635a2fe095bf9c040c711732b8ea655afe0b0.1675471315.git.ffmpegagent@gmail.com> (raw)
In-Reply-To: <pull.50.ffstaging.FFmpeg.1675471315.ffmpegagent@gmail.com>
From: Aman Karmani <aman@tmm1.net>
Signed-off-by: Aman Karmani <aman@tmm1.net>
---
doc/bitstream_filters.texi | 11 +++
libavcodec/Makefile | 2 +-
libavcodec/h264_metadata_bsf.c | 144 +++++++++++++++++++++++++++++++++
3 files changed, 156 insertions(+), 1 deletion(-)
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index c036d37403..3d97f66315 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -243,6 +243,17 @@ ffmpeg -i hapqa_inputfile.mov -c copy -bsf:v hapqa_extract=texture=alpha -tag:v
Modify metadata embedded in an H.264 stream.
@table @option
+@item a53_cc
+Insert or remove registered userdata SEI NAL units containing A/53 closed captions.
+
+@table @samp
+@item pass
+@item insert
+@item remove
+@end table
+
+Default is pass.
+
@item aud
Insert or remove AUD NAL units in all access units of the stream.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 2a36a31ff2..9f97c9b5e5 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1212,7 +1212,7 @@ OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF) += extract_extradata_bsf.o \
av1_parse.o h2645_parse.o
OBJS-$(CONFIG_FILTER_UNITS_BSF) += filter_units_bsf.o
OBJS-$(CONFIG_H264_METADATA_BSF) += h264_metadata_bsf.o h264_levels.o \
- h2645data.o
+ h2645data.o cbs_misc.o
OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF) += h264_mp4toannexb_bsf.o
OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF) += h264_redundant_pps_bsf.o
OBJS-$(CONFIG_HAPQA_EXTRACT_BSF) += hapqa_extract_bsf.o hap.o
diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c
index d318bf0cee..bbe74c3cee 100644
--- a/libavcodec/h264_metadata_bsf.c
+++ b/libavcodec/h264_metadata_bsf.c
@@ -26,6 +26,8 @@
#include "cbs.h"
#include "cbs_bsf.h"
#include "cbs_h264.h"
+#include "cbs_misc.h"
+#include "cbs_sei.h"
#include "h264.h"
#include "h264_levels.h"
#include "h264_sei.h"
@@ -81,6 +83,7 @@ typedef struct H264MetadataContext {
H264RawSEIDisplayOrientation display_orientation_payload;
int level;
+ int a53_cc;
} H264MetadataContext;
@@ -463,6 +466,137 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf,
return 0;
}
+static int h264_metadata_handle_a53_cc(AVBSFContext *bsf, AVPacket *pkt,
+ CodedBitstreamFragment *au)
+{
+ H264MetadataContext *ctx = bsf->priv_data;
+ SEIRawMessage *message;
+ int err;
+
+ message = NULL;
+ while (ff_cbs_sei_find_message(ctx->common.output, au,
+ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
+ &message) == 0) {
+ SEIRawUserDataRegistered *udr = message->payload;
+ A53UserData a53_ud;
+ uint8_t *a53_side_data = NULL;
+ size_t a53_side_data_size = 0;
+
+ if (udr->data_length < 2) {
+ // Too short to contain a provider code.
+ continue;
+ }
+
+ if (AV_RB16(udr->data) != 0x31) { // provider_code as atsc_provider_code
+ // Not ATSC.
+ continue;
+ }
+
+ // The first two bytes of the message is provider_code so we offset the data by that
+ err = ff_cbs_read_a53_user_data(ctx->common.output,
+ &a53_ud,
+ udr->data + 2,
+ udr->data_length - 2);
+ if (err < 0) {
+ // Invalid or something completely different.
+ continue;
+ }
+ if (a53_ud.user_identifier != A53_USER_IDENTIFIER_ATSC ||
+ a53_ud.atsc.user_data_type_code !=
+ A53_USER_DATA_TYPE_CODE_CC_DATA) {
+ // Valid but something else (e.g. AFD).
+ continue;
+ }
+
+ err = ff_cbs_write_a53_cc_side_data(ctx->common.output,
+ &a53_side_data,
+ &a53_side_data_size,
+ &a53_ud);
+ if (err < 0) {
+ av_log(bsf, AV_LOG_ERROR, "Failed to write "
+ "A/53 user data for packet side data.\n");
+ return err;
+ }
+
+ if (a53_side_data) {
+ err = av_packet_add_side_data(pkt, AV_PKT_DATA_A53_CC,
+ a53_side_data, a53_side_data_size);
+ if (err) {
+ av_log(bsf, AV_LOG_ERROR, "Failed to attach extracted A/53 "
+ "side data to packet.\n");
+ av_freep(&a53_side_data);
+ return err;
+ }
+
+ if (ctx->a53_cc == BSF_ELEMENT_REMOVE ||
+ ctx->a53_cc == BSF_ELEMENT_INSERT) {
+ ff_cbs_sei_delete_message(ctx->common.output, au, message);
+
+ // Reset iteration
+ message = NULL;
+ }
+ }
+ }
+
+ if (ctx->a53_cc == BSF_ELEMENT_INSERT) {
+ uint8_t *data;
+ size_t size;
+ int offset = 0;
+
+ data = av_packet_get_side_data(pkt, AV_PKT_DATA_A53_CC, &size);
+ while (size > 0) {
+ A53UserData a53_ud;
+ int rsize = FFMIN(93, size);
+
+ err = ff_cbs_read_a53_cc_side_data(ctx->common.output, &a53_ud,
+ data + offset, rsize);
+ offset += rsize;
+ size -= rsize;
+ if (err < 0) {
+ av_log(bsf, AV_LOG_WARNING, "Invalid A/53 closed captions "
+ "in packet side data dropped.\n");
+ } else {
+ AVBufferRef *udr_buf = av_buffer_allocz(sizeof(SEIRawUserDataRegistered));
+ SEIRawUserDataRegistered *udr = (SEIRawUserDataRegistered*)udr_buf->data;
+ size_t size = 9 + 3 * a53_ud.atsc.cc_data.cc_count;
+
+ udr->data_ref = av_buffer_allocz(2 + size);
+ if (!udr->data_ref) {
+ return AVERROR(ENOMEM);
+ }
+ udr->data = udr->data_ref->data;
+ udr->data_length = udr->data_ref->size;
+
+ udr->itu_t_t35_country_code = 0xB5; // usa_country_code
+ AV_WB16(udr->data, 0x31); // provider_code as atsc_provider_code
+
+ err = ff_cbs_write_a53_user_data(ctx->common.output, udr->data + 2,
+ &size, &a53_ud);
+ if (err < 0) {
+ av_log(bsf, AV_LOG_ERROR, "Failed to write "
+ "A/53 user data.\n");
+ av_buffer_unref(&udr->data_ref);
+ av_buffer_unref(&udr_buf);
+ return err;
+ }
+
+ err = ff_cbs_sei_add_message(ctx->common.output, au, 1,
+ SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35,
+ udr, udr_buf);
+ if (err < 0) {
+ av_log(bsf, AV_LOG_ERROR, "Failed to add A/53 user data "
+ "SEI message to access unit.\n");
+ av_buffer_unref(&udr->data_ref);
+ av_buffer_unref(&udr_buf);
+ return err;
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
static int h264_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
CodedBitstreamFragment *au)
{
@@ -533,6 +667,12 @@ static int h264_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
return err;
}
+ if (pkt && ctx->a53_cc != BSF_ELEMENT_PASS) {
+ err = h264_metadata_handle_a53_cc(bsf, pkt, au);
+ if (err < 0)
+ return err;
+ }
+
if (pkt)
ctx->done_first_au = 1;
@@ -696,6 +836,10 @@ static const AVOption h264_metadata_options[] = {
{ LEVEL("6.2", 62) },
#undef LEVEL
+ BSF_ELEMENT_OPTIONS_PIRE("a53_cc",
+ "A/53 Closed Captions in SEI NAL units",
+ a53_cc, FLAGS),
+
{ NULL }
};
--
ffmpeg-codebot
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2023-02-04 0:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-04 0:41 [FFmpeg-devel] [PATCH 0/5] Bitstream filter support for A/53 Closed Captions ffmpegagent
2023-02-04 0:41 ` [FFmpeg-devel] [PATCH 1/5] cbs: Add some common code for read/write of miscellaneous user data Mark Thompson
2023-02-04 0:41 ` [FFmpeg-devel] [PATCH 2/5] cbs_sei: add ff_cbs_sei_delete_message helper Eric Lindvall
2023-02-04 0:41 ` [FFmpeg-devel] [PATCH 3/5] avcodec/mpeg2_metadata_bsf: add support for a/53 closed captions Aman Karmani
2023-02-04 0:41 ` Aman Karmani [this message]
2023-02-04 0:41 ` [FFmpeg-devel] [PATCH 5/5] avcodec/h265_metadata_bsf: " Aman Karmani
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d79635a2fe095bf9c040c711732b8ea655afe0b0.1675471315.git.ffmpegagent@gmail.com \
--to=ffmpegagent@gmail.com \
--cc=aman@tmm1.net \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git