Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/dep/ffmpeg/include/libavcodec/smpte_436m.h
5196 views
1
/*
2
* MXF SMPTE-436M VBI/ANC parsing functions
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
#ifndef AVCODEC_SMPTE_436M_H
23
#define AVCODEC_SMPTE_436M_H
24
25
#include <stdint.h>
26
27
/**
28
* Iterator over the ANC packets in a single AV_CODEC_ID_SMPTE_436M_ANC AVPacket's data
29
*/
30
typedef struct AVSmpte436mAncIterator {
31
uint16_t anc_packets_left;
32
int size_left;
33
const uint8_t *data_left;
34
} AVSmpte436mAncIterator;
35
36
/**
37
* Wrapping Type from Table 7 (page 13) of:
38
* https://pub.smpte.org/latest/st436/s436m-2006.pdf
39
*/
40
typedef enum AVSmpte436mWrappingType
41
{
42
AV_SMPTE_436M_WRAPPING_TYPE_VANC_FRAME = 1,
43
AV_SMPTE_436M_WRAPPING_TYPE_VANC_FIELD_1 = 2,
44
AV_SMPTE_436M_WRAPPING_TYPE_VANC_FIELD_2 = 3,
45
AV_SMPTE_436M_WRAPPING_TYPE_VANC_PROGRESSIVE_FRAME = 4,
46
AV_SMPTE_436M_WRAPPING_TYPE_HANC_FRAME = 0x11,
47
AV_SMPTE_436M_WRAPPING_TYPE_HANC_FIELD_1 = 0x12,
48
AV_SMPTE_436M_WRAPPING_TYPE_HANC_FIELD_2 = 0x13,
49
AV_SMPTE_436M_WRAPPING_TYPE_HANC_PROGRESSIVE_FRAME = 0x14,
50
/** not a real wrapping type, just here to guarantee the enum is big enough */
51
AV_SMPTE_436M_WRAPPING_TYPE_MAX = 0xFF,
52
} AVSmpte436mWrappingType;
53
54
/**
55
* Payload Sample Coding from Table 4 (page 10) and Table 7 (page 13) of:
56
* https://pub.smpte.org/latest/st436/s436m-2006.pdf
57
*/
58
typedef enum AVSmpte436mPayloadSampleCoding
59
{
60
/** only used for VBI */
61
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_1BIT_LUMA = 1,
62
/** only used for VBI */
63
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_1BIT_COLOR_DIFF = 2,
64
/** only used for VBI */
65
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_1BIT_LUMA_AND_COLOR_DIFF = 3,
66
/** used for VBI and ANC */
67
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_8BIT_LUMA = 4,
68
/** used for VBI and ANC */
69
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_8BIT_COLOR_DIFF = 5,
70
/** used for VBI and ANC */
71
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_8BIT_LUMA_AND_COLOR_DIFF = 6,
72
/** used for VBI and ANC */
73
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_10BIT_LUMA = 7,
74
/** used for VBI and ANC */
75
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_10BIT_COLOR_DIFF = 8,
76
/** used for VBI and ANC */
77
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_10BIT_LUMA_AND_COLOR_DIFF = 9,
78
/** only used for ANC */
79
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_8BIT_LUMA_WITH_PARITY_ERROR = 10,
80
/** only used for ANC */
81
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_8BIT_COLOR_DIFF_WITH_PARITY_ERROR = 11,
82
/** only used for ANC */
83
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_8BIT_LUMA_AND_COLOR_DIFF_WITH_PARITY_ERROR = 12,
84
/** not a real sample coding, just here to guarantee the enum is big enough */
85
AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_MAX = 0xFF,
86
} AVSmpte436mPayloadSampleCoding;
87
88
/** the payload capacity of AVSmpte291mAnc8bit (and of AVSmpte291mAnc10bit when that gets added) */
89
#define AV_SMPTE_291M_ANC_PAYLOAD_CAPACITY 0xFF
90
91
/**
92
* An ANC packet with an 8-bit payload.
93
* This can be decoded from AVSmpte436mCodedAnc::payload.
94
*
95
* Note: Some ANC packets need a 10-bit payload, if stored in this struct,
96
* the most-significant 2 bits of each sample are discarded.
97
*/
98
typedef struct AVSmpte291mAnc8bit {
99
uint8_t did;
100
uint8_t sdid_or_dbn;
101
uint8_t data_count;
102
uint8_t payload[AV_SMPTE_291M_ANC_PAYLOAD_CAPACITY];
103
uint8_t checksum;
104
} AVSmpte291mAnc8bit;
105
106
/** max number of samples that can be stored in the payload of AVSmpte436mCodedAnc */
107
#define AV_SMPTE_436M_CODED_ANC_SAMPLE_CAPACITY \
108
(AV_SMPTE_291M_ANC_PAYLOAD_CAPACITY + 4) /* 4 for did, sdid_or_dbn, data_count, and checksum */
109
/** max number of bytes that can be stored in the payload of AVSmpte436mCodedAnc */
110
#define AV_SMPTE_436M_CODED_ANC_PAYLOAD_CAPACITY (((AV_SMPTE_436M_CODED_ANC_SAMPLE_CAPACITY + 2) / 3) * 4)
111
112
/**
113
* An encoded ANC packet within a single AV_CODEC_ID_SMPTE_436M_ANC AVPacket's data.
114
* The repeated section of Table 7 (page 13) of:
115
* https://pub.smpte.org/latest/st436/s436m-2006.pdf
116
*/
117
typedef struct AVSmpte436mCodedAnc {
118
uint16_t line_number;
119
AVSmpte436mWrappingType wrapping_type;
120
AVSmpte436mPayloadSampleCoding payload_sample_coding;
121
uint16_t payload_sample_count;
122
uint32_t payload_array_length;
123
/** the payload, has size payload_array_length.
124
* can be decoded into AVSmpte291mAnc8bit
125
*/
126
uint8_t payload[AV_SMPTE_436M_CODED_ANC_PAYLOAD_CAPACITY];
127
} AVSmpte436mCodedAnc;
128
129
/**
130
* Validate a AVSmpte436mCodedAnc structure. Doesn't check if the payload is valid.
131
* @param[in] anc ANC packet to validate
132
* @return 0 on success, AVERROR codes otherwise.
133
*/
134
int av_smpte_436m_coded_anc_validate(const AVSmpte436mCodedAnc *anc);
135
136
/**
137
* Encode ANC packets into a single AV_CODEC_ID_SMPTE_436M_ANC AVPacket's data.
138
* @param[in] anc_packet_count number of ANC packets to encode
139
* @param[in] anc_packets the ANC packets to encode
140
* @param[in] size the size of out. ignored if out is NULL.
141
* @param[out] out Output bytes. Doesn't write anything if out is NULL.
142
* @return the number of bytes written on success, AVERROR codes otherwise.
143
* If out is NULL, returns the number of bytes it would have written.
144
*/
145
int av_smpte_436m_anc_encode(uint8_t *out, int size, int anc_packet_count, const AVSmpte436mCodedAnc *anc_packets);
146
147
struct AVPacket;
148
149
/**
150
* Append more ANC packets to a single AV_CODEC_ID_SMPTE_436M_ANC AVPacket's data.
151
* @param[in] anc_packet_count number of ANC packets to encode
152
* @param[in] anc_packets the ANC packets to encode
153
* @param pkt the AVPacket to append to.
154
* it must either be size 0 or contain valid SMPTE_436M_ANC data.
155
* @return 0 on success, AVERROR codes otherwise.
156
*/
157
int av_smpte_436m_anc_append(struct AVPacket *pkt, int anc_packet_count, const AVSmpte436mCodedAnc *anc_packets);
158
159
/**
160
* Set up iteration over the ANC packets in a single AV_CODEC_ID_SMPTE_436M_ANC AVPacket's data.
161
* @param[in] buf Pointer to the data from a AV_CODEC_ID_SMPTE_436M_ANC AVPacket.
162
* @param[in] buf_size Size of the data from a AV_CODEC_ID_SMPTE_436M_ANC AVPacket.
163
* @param[out] iter Pointer to the iterator.
164
* @return 0 on success, AVERROR codes otherwise.
165
*/
166
int av_smpte_436m_anc_iter_init(AVSmpte436mAncIterator *iter, const uint8_t *buf, int buf_size);
167
168
/**
169
* Get the next ANC packet from the iterator, advancing the iterator.
170
* @param[in,out] iter Pointer to the iterator.
171
* @param[out] anc The returned ANC packet.
172
* @return 0 on success, AVERROR_EOF when the iterator has reached the end, AVERROR codes otherwise.
173
*/
174
int av_smpte_436m_anc_iter_next(AVSmpte436mAncIterator *iter, AVSmpte436mCodedAnc *anc);
175
176
/**
177
* Get the minimum number of bytes needed to store a AVSmpte436mCodedAnc payload.
178
* @param sample_coding the payload sample coding
179
* @param sample_count the number of samples stored in the payload
180
* @return returns the minimum number of bytes needed, on error returns < 0.
181
* always <= SMPTE_436M_CODED_ANC_PAYLOAD_CAPACITY
182
*/
183
int av_smpte_436m_coded_anc_payload_size(AVSmpte436mPayloadSampleCoding sample_coding, uint16_t sample_count);
184
185
/**
186
* Decode a AVSmpte436mCodedAnc payload into AVSmpte291mAnc8bit
187
* @param[in] sample_coding the payload sample coding
188
* @param[in] sample_count the number of samples stored in the payload
189
* @param[in] payload the bytes storing the payload,
190
* the needed size can be obtained from
191
avpriv_smpte_436m_coded_anc_payload_size
192
* @param[in] log_ctx context pointer for av_log
193
* @param[out] out The decoded ANC packet.
194
* @return returns 0 on success, otherwise < 0.
195
*/
196
int av_smpte_291m_anc_8bit_decode(AVSmpte291mAnc8bit *out,
197
AVSmpte436mPayloadSampleCoding sample_coding,
198
uint16_t sample_count,
199
const uint8_t *payload,
200
void *log_ctx);
201
202
/**
203
* Fill in the correct checksum for a AVSmpte291mAnc8bit
204
* @param[in,out] anc The ANC packet.
205
*/
206
void av_smpte_291m_anc_8bit_fill_checksum(AVSmpte291mAnc8bit *anc);
207
208
/**
209
* Compute the sample count needed to encode a AVSmpte291mAnc8bit into a AVSmpte436mCodedAnc payload
210
* @param[in] anc The ANC packet.
211
* @param[in] sample_coding The sample coding.
212
* @param[in] log_ctx context pointer for av_log
213
* @return returns the sample count on success, otherwise < 0.
214
*/
215
int av_smpte_291m_anc_8bit_get_sample_count(const AVSmpte291mAnc8bit *anc,
216
AVSmpte436mPayloadSampleCoding sample_coding,
217
void *log_ctx);
218
219
/**
220
* Encode a AVSmpte291mAnc8bit into a AVSmpte436mCodedAnc
221
* @param[in] line_number the line number the ANC packet is on
222
* @param[in] wrapping_type the wrapping type
223
* @param[in] sample_coding the payload sample coding
224
* @param[in] payload the ANC packet to encode.
225
* @param[in] log_ctx context pointer for av_log
226
* @param[out] out The encoded ANC packet.
227
* @return returns 0 on success, otherwise < 0.
228
*/
229
int av_smpte_291m_anc_8bit_encode(AVSmpte436mCodedAnc *out,
230
uint16_t line_number,
231
AVSmpte436mWrappingType wrapping_type,
232
AVSmpte436mPayloadSampleCoding sample_coding,
233
const AVSmpte291mAnc8bit *payload,
234
void *log_ctx);
235
236
/** AVSmpte291mAnc8bit::did when carrying CTA-708 data (for AV_CODEC_ID_EIA_608) */
237
#define AV_SMPTE_291M_ANC_DID_CTA_708 0x61
238
239
/** AVSmpte291mAnc8bit::sdid_or_dbn when carrying CTA-708 data (for AV_CODEC_ID_EIA_608) */
240
#define AV_SMPTE_291M_ANC_SDID_CTA_708 0x1
241
242
/**
243
* Try to decode an ANC packet into EIA-608/CTA-708 data (AV_CODEC_ID_EIA_608). This
244
* @param[in] anc The ANC packet.
245
* @param[in] log_ctx Context pointer for av_log
246
* @param[out] cc_data the buffer to store the extracted EIA-608/CTA-708 data,
247
* you can pass NULL to not store the data.
248
* the required size is 3 * cc_count bytes.
249
* SMPTE_291M_ANC_PAYLOAD_CAPACITY is always enough size.
250
* @return returns cc_count (>= 0) on success, AVERROR(EAGAIN) if it wasn't a CTA-708 ANC packet, < 0 on error.
251
*/
252
int av_smpte_291m_anc_8bit_extract_cta_708(const AVSmpte291mAnc8bit *anc, uint8_t *cc_data, void *log_ctx);
253
254
#endif /* AVCODEC_SMPTE_436M_H */
255
256