aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/bsf/smpte436m_to_eia608.c
blob: 529ae887945a100c28c4d6eaa9e0a0d779480233 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * MXF SMPTE-436M ANC to EIA-608 bitstream filter
 * Copyright (c) 2025 Jacob Lifshay
 *
 * 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 "bsf.h"
#include "bsf_internal.h"
#include "codec_id.h"
#include "libavcodec/smpte_436m.h"
#include "libavutil/error.h"

static av_cold int ff_smpte436m_to_eia608_init(AVBSFContext *ctx)
{
    ctx->par_out->codec_type = AVMEDIA_TYPE_SUBTITLE;
    ctx->par_out->codec_id   = AV_CODEC_ID_EIA_608;
    return 0;
}

static int ff_smpte436m_to_eia608_filter(AVBSFContext *ctx, AVPacket *out)
{
    AVPacket *in;
    int       ret = ff_bsf_get_packet(ctx, &in);
    if (ret < 0)
        return ret;

    AVSmpte436mAncIterator iter;
    ret = av_smpte_436m_anc_iter_init(&iter, in->data, in->size);
    if (ret < 0)
        goto fail;
    AVSmpte436mCodedAnc coded_anc;
    while ((ret = av_smpte_436m_anc_iter_next(&iter, &coded_anc)) >= 0) {
        AVSmpte291mAnc8bit anc;
        ret = av_smpte_291m_anc_8bit_decode(
            &anc, coded_anc.payload_sample_coding, coded_anc.payload_sample_count, coded_anc.payload, ctx);
        if (ret < 0)
            goto fail;
        ret = av_smpte_291m_anc_8bit_extract_cta_708(&anc, NULL, ctx);
        if (ret == AVERROR(EAGAIN))
            continue;
        if (ret < 0)
            goto fail;
        int cc_count = ret;

        ret = av_new_packet(out, 3 * cc_count);
        if (ret < 0)
            goto fail;

        ret = av_packet_copy_props(out, in);
        if (ret < 0)
            goto fail;

        // verified it won't fail by running it above
        av_smpte_291m_anc_8bit_extract_cta_708(&anc, out->data, ctx);

        av_packet_free(&in);

        return 0;
    }
    if (ret != AVERROR_EOF)
        return ret;
    ret = AVERROR(EAGAIN);

fail:
    if (ret < 0)
        av_packet_unref(out);
    av_packet_free(&in);
    return ret;
}

const FFBitStreamFilter ff_smpte436m_to_eia608_bsf = {
    .p.name      = "smpte436m_to_eia608",
    .p.codec_ids = (const enum AVCodecID[]){ AV_CODEC_ID_SMPTE_436M_ANC, AV_CODEC_ID_NONE },
    .init        = ff_smpte436m_to_eia608_init,
    .filter      = ff_smpte436m_to_eia608_filter,
};