| @@ -0,0 +1,89 @@ |
| 1 | +/* |
| 2 | + * MXF SMPTE-436M ANC to EIA-608 bitstream filter |
| 3 | + * Copyright (c) 2025 Jacob Lifshay |
| 4 | + * |
| 5 | + * This file is part of FFmpeg. |
| 6 | + * |
| 7 | + * FFmpeg is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU Lesser General Public |
| 9 | + * License as published by the Free Software Foundation; either |
| 10 | + * version 2.1 of the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * FFmpeg is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with FFmpeg; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include "bsf.h" |
| 23 | +#include "bsf_internal.h" |
| 24 | +#include "codec_id.h" |
| 25 | +#include "libavcodec/smpte_436m.h" |
| 26 | +#include "libavutil/error.h" |
| 27 | + |
| 28 | +static av_cold int ff_smpte436m_to_eia608_init(AVBSFContext *ctx) |
| 29 | +{ |
| 30 | + ctx->par_out->codec_type = AVMEDIA_TYPE_SUBTITLE; |
| 31 | + ctx->par_out->codec_id = AV_CODEC_ID_EIA_608; |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +static int ff_smpte436m_to_eia608_filter(AVBSFContext *ctx, AVPacket *out) |
| 36 | +{ |
| 37 | + AVPacket *in; |
| 38 | + int ret = ff_bsf_get_packet(ctx, &in); |
| 39 | + if (ret < 0) |
| 40 | + return ret; |
| 41 | + |
| 42 | + AVSmpte436mAncIterator iter; |
| 43 | + ret = av_smpte_436m_anc_iter_init(&iter, in->data, in->size); |
| 44 | + if (ret < 0) |
| 45 | + goto fail; |
| 46 | + AVSmpte436mCodedAnc coded_anc; |
| 47 | + while ((ret = av_smpte_436m_anc_iter_next(&iter, &coded_anc)) >= 0) { |
| 48 | + AVSmpte291mAnc8bit anc; |
| 49 | + ret = av_smpte_291m_anc_8bit_decode( |
| 50 | + &anc, coded_anc.payload_sample_coding, coded_anc.payload_sample_count, coded_anc.payload, ctx); |
| 51 | + if (ret < 0) |
| 52 | + goto fail; |
| 53 | + ret = av_smpte_291m_anc_8bit_extract_cta_708(&anc, NULL, ctx); |
| 54 | + if (ret == AVERROR(EAGAIN)) |
| 55 | + continue; |
| 56 | + if (ret < 0) |
| 57 | + goto fail; |
| 58 | + int cc_count = ret; |
| 59 | + |
| 60 | + ret = av_new_packet(out, 3 * cc_count); |
| 61 | + if (ret < 0) |
| 62 | + goto fail; |
| 63 | + |
| 64 | + ret = av_packet_copy_props(out, in); |
| 65 | + if (ret < 0) |
| 66 | + goto fail; |
| 67 | + |
| 68 | + // verified it won't fail by running it above |
| 69 | + av_smpte_291m_anc_8bit_extract_cta_708(&anc, out->data, ctx); |
| 70 | + |
| 71 | + return 0; |
| 72 | + } |
| 73 | + if (ret != AVERROR_EOF) |
| 74 | + return ret; |
| 75 | + ret = AVERROR(EAGAIN); |
| 76 | + |
| 77 | +fail: |
| 78 | + if (ret < 0) |
| 79 | + av_packet_unref(out); |
| 80 | + av_packet_free(&in); |
| 81 | + return ret; |
| 82 | +} |
| 83 | + |
| 84 | +const FFBitStreamFilter ff_smpte436m_to_eia608_bsf = { |
| 85 | + .p.name = "smpte436m_to_eia608", |
| 86 | + .p.codec_ids = (const enum AVCodecID[]){ AV_CODEC_ID_SMPTE_436M_ANC, AV_CODEC_ID_NONE }, |
| 87 | + .init = ff_smpte436m_to_eia608_init, |
| 88 | + .filter = ff_smpte436m_to_eia608_filter, |
| 89 | +}; |