|
| 1 | +/* |
| 2 | + * CEA-708 Closed Caption Repacker |
| 3 | + * Copyright (c) 2023 LTN Global Communications |
| 4 | + * |
| 5 | + * Author: Devin Heitmueller <[email protected]> |
| 6 | + * |
| 7 | + * This file is part of FFmpeg. |
| 8 | + * |
| 9 | + * FFmpeg is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 2.1 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * FFmpeg is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with FFmpeg; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * Repackage CEA-708 arrays, which deals with incorrect cc_count for a given |
| 26 | + * output framerate, and incorrect 708 padding. |
| 27 | + * |
| 28 | + * See CEA CEA-10-A "EIA-708-B Implementation Guidance", Section 26.5 |
| 29 | + * "Grouping DTVCC Data Within user_data() Structure" |
| 30 | + */ |
| 31 | + |
| 32 | +#include "avfilter.h" |
| 33 | +#include "internal.h" |
| 34 | +#include "ccfifo.h" |
| 35 | +#include "libavutil/opt.h" |
| 36 | + |
| 37 | +typedef struct CCRepackContext |
| 38 | +{ |
| 39 | + const AVClass *class; |
| 40 | + AVCCFifo *cc_fifo; |
| 41 | +} CCRepackContext; |
| 42 | + |
| 43 | +static const AVOption ccrepack_options[] = { |
| 44 | + { NULL } |
| 45 | +}; |
| 46 | + |
| 47 | +AVFILTER_DEFINE_CLASS(ccrepack); |
| 48 | + |
| 49 | +static int config_input(AVFilterLink *link) |
| 50 | +{ |
| 51 | + CCRepackContext *ctx = link->dst->priv; |
| 52 | + |
| 53 | + if (!(ctx->cc_fifo = ff_ccfifo_alloc(link->frame_rate, ctx))) { |
| 54 | + av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n"); |
| 55 | + return AVERROR(ENOMEM); |
| 56 | + } |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) |
| 62 | +{ |
| 63 | + CCRepackContext *ctx = inlink->dst->priv; |
| 64 | + AVFilterLink *outlink = inlink->dst->outputs[0]; |
| 65 | + |
| 66 | + ff_ccfifo_extract(ctx->cc_fifo, frame); |
| 67 | + ff_ccfifo_inject(ctx->cc_fifo, frame); |
| 68 | + |
| 69 | + return ff_filter_frame(outlink, frame); |
| 70 | +} |
| 71 | + |
| 72 | +static av_cold void uninit(AVFilterContext *ctx) |
| 73 | +{ |
| 74 | + CCRepackContext *s = ctx->priv; |
| 75 | + ff_ccfifo_freep(&s->cc_fifo); |
| 76 | +} |
| 77 | + |
| 78 | +static const AVFilterPad avfilter_vf_ccrepack_inputs[] = { |
| 79 | + { |
| 80 | + .name = "default", |
| 81 | + .type = AVMEDIA_TYPE_VIDEO, |
| 82 | + .filter_frame = filter_frame, |
| 83 | + .config_props = config_input, |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +static const AVFilterPad avfilter_vf_ccrepack_outputs[] = { |
| 88 | + { |
| 89 | + .name = "default", |
| 90 | + .type = AVMEDIA_TYPE_VIDEO, |
| 91 | + }, |
| 92 | +}; |
| 93 | + |
| 94 | +AVFilter ff_vf_ccrepack = { |
| 95 | + .name = "ccrepack", |
| 96 | + .description = NULL_IF_CONFIG_SMALL("Repack CEA-708 closed caption metadata"), |
| 97 | + .uninit = uninit, |
| 98 | + .priv_size = sizeof(CCRepackContext), |
| 99 | + .priv_class = &ccrepack_class, |
| 100 | + FILTER_INPUTS(avfilter_vf_ccrepack_inputs), |
| 101 | + FILTER_OUTPUTS(avfilter_vf_ccrepack_outputs), |
| 102 | +}; |
0 commit comments