[FFmpeg-devel] [PATCH v2 1/5] ccfifo: Properly handle CEA-708 captions through framerate conversion
James Almer
jamrial at gmail.com
Fri Apr 14 16:22:06 EEST 2023
On 4/7/2023 6:58 PM, Devin Heitmueller wrote:
> When transcoding video that contains 708 closed captions, the
> caption data is tied to the frames as side data. Simply dropping
> or adding frames to change the framerate will result in loss of
> data, so the caption data needs to be preserved and reformatted.
>
> For example, without this patch converting 720p59 to 1080i59
> would result in loss of 50% of the caption bytes, resulting in
> garbled 608 captions and 708 probably wouldn't render at all.
> Further, the frames that are there will have an illegal
> cc_count for the target framerate, so some decoders may ignore
> the packets entirely.
>
> Extract the 608 and 708 tuples and insert them onto queues. Then
> after dropping/adding frames, re-write the tuples back into the
> resulting frames at the appropriate rate given the target
> framerate. This includes both having the correct cc_count as
> well as clocking out the 608 pairs at the appropriate rate.
>
> Signed-off-by: Devin Heitmueller <dheitmueller at ltnglobal.com>
> ---
> libavfilter/Makefile | 1 +
> libavfilter/ccfifo.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++
> libavfilter/ccfifo.h | 85 +++++++++++++++++++++++
> 3 files changed, 277 insertions(+)
> create mode 100644 libavfilter/ccfifo.c
> create mode 100644 libavfilter/ccfifo.h
>
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 71e198b..628ade8 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -14,6 +14,7 @@ OBJS = allfilters.o \
> buffersink.o \
> buffersrc.o \
> colorspace.o \
> + ccfifo.o \
> drawutils.o \
> fifo.o \
> formats.o \
> diff --git a/libavfilter/ccfifo.c b/libavfilter/ccfifo.c
> new file mode 100644
> index 0000000..5db4149
> --- /dev/null
> +++ b/libavfilter/ccfifo.c
> @@ -0,0 +1,191 @@
> +/*
> + * CEA-708 Closed Captioning FIFO
> + * Copyright (c) 2023 LTN Global Communications
> + *
> + * Author: Devin Heitmueller <dheitmueller at ltnglobal.com>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "ccfifo.h"
> +
> +struct AVCCFifo {
> + AVFifo *cc_608_fifo;
> + AVFifo *cc_708_fifo;
> + int expected_cc_count;
> + int expected_608;
> + int cc_detected;
> + void *log_ctx;
> +};
> +
> +#define MAX_CC_ELEMENTS 128
> +#define CC_BYTES_PER_ENTRY 3
> +
> +struct cc_lookup {
> + int num;
> + int den;
> + int cc_count;
> + int num_608;
> +};
> +
> +const static struct cc_lookup cc_lookup_vals[] = {
> + { 15, 1, 40, 4 },
> + { 24, 1, 25, 3 },
> + { 24000, 1001, 25, 3 },
> + { 30, 1, 20, 2 },
> + { 30000, 1001, 20, 2},
> + { 60, 1, 10, 1 },
> + { 60000, 1001, 10, 1},
> +};
> +
> +void av_ccfifo_freep(AVCCFifo **ccf)
> +{
> + if (ccf && *ccf) {
> + AVCCFifo *tmp = *ccf;
> + if (tmp->cc_608_fifo)
> + av_fifo_freep2(&tmp->cc_608_fifo);
> + if (tmp->cc_708_fifo)
> + av_fifo_freep2(&tmp->cc_708_fifo);
> + av_freep(*ccf);
> + }
> +}
> +
> +AVCCFifo *av_ccfifo_alloc(AVRational *framerate, void *log_ctx)
None of these functions should use the av_* prefix if they are meant to
be internal, otherwise the symbols will be exported. Use ff_* instead.
More information about the ffmpeg-devel
mailing list