FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/spdifenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 245 319 76.8%
Functions: 12 12 100.0%
Branches: 94 146 64.4%

Line Branch Exec Source
1 /*
2 * IEC 61937 muxer
3 * Copyright (c) 2009 Bartlomiej Wolowiec
4 * Copyright (c) 2010, 2020 Anssi Hannula
5 * Copyright (c) 2010 Carl Eugen Hoyos
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 * @file
26 * IEC-61937 encapsulation of various formats, used by S/PDIF
27 * @author Bartlomiej Wolowiec
28 * @author Anssi Hannula
29 * @author Carl Eugen Hoyos
30 */
31
32 /*
33 * Terminology used in specification:
34 * data-burst - IEC61937 frame, contains header and encapsuled frame
35 * burst-preamble - IEC61937 frame header, contains 16-bit words named Pa, Pb, Pc and Pd
36 * burst-payload - encapsuled frame
37 * Pa, Pb - syncword - 0xF872, 0x4E1F
38 * Pc - burst-info, contains data-type (bits 0-6), error flag (bit 7), data-type-dependent info (bits 8-12)
39 * and bitstream number (bits 13-15)
40 * data-type - determines type of encapsuled frames
41 * Pd - length code (number of bits or bytes of encapsuled frame - according to data_type)
42 *
43 * IEC 61937 frames at normal usage start every specific count of bytes,
44 * dependent from data-type (spaces between packets are filled by zeros)
45 */
46
47 #include <inttypes.h>
48
49 #include "avformat.h"
50 #include "avio_internal.h"
51 #include "mux.h"
52 #include "spdif.h"
53 #include "libavcodec/ac3defs.h"
54 #include "libavcodec/adts_parser.h"
55 #include "libavcodec/dca.h"
56 #include "libavcodec/dca_syncwords.h"
57 #include "libavutil/mem.h"
58 #include "libavutil/opt.h"
59
60 typedef struct IEC61937Context {
61 const AVClass *av_class;
62 enum IEC61937DataType data_type;///< burst info - reference to type of payload of the data-burst
63 int length_code; ///< length code in bits or bytes, depending on data type
64 int pkt_offset; ///< data burst repetition period in bytes
65 uint8_t *buffer; ///< allocated buffer, used for swap bytes
66 int buffer_size; ///< size of allocated buffer
67
68 const uint8_t *out_buf; ///< pointer to the outgoing data before byte-swapping
69 int out_bytes; ///< amount of outgoing bytes
70
71 int use_preamble; ///< preamble enabled (disabled for exactly pre-padded DTS)
72 int extra_bswap; ///< extra bswap for payload (for LE DTS => standard BE DTS)
73
74 uint8_t *hd_buf[2]; ///< allocated buffers to concatenate hd audio frames
75 int hd_buf_size; ///< size of the hd audio buffer (eac3, dts4)
76 int hd_buf_count; ///< number of frames in the hd audio buffer (eac3)
77 int hd_buf_filled; ///< amount of bytes in the hd audio buffer (eac3, truehd)
78 int hd_buf_idx; ///< active hd buffer index (truehd)
79
80 int dtshd_skip; ///< counter used for skipping DTS-HD frames
81
82 uint16_t truehd_prev_time; ///< input_timing from the last frame
83 int truehd_prev_size; ///< previous frame size in bytes, including any MAT codes
84 int truehd_samples_per_frame; ///< samples per frame for padding calculation
85
86 /* AVOptions: */
87 int dtshd_rate;
88 int dtshd_fallback;
89 #define SPDIF_FLAG_BIGENDIAN 0x01
90 int spdif_flags;
91
92 /// function, which generates codec dependent header information.
93 /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
94 int (*header_info) (AVFormatContext *s, AVPacket *pkt);
95 } IEC61937Context;
96
97 static const AVOption options[] = {
98 { "spdif_flags", "IEC 61937 encapsulation flags", offsetof(IEC61937Context, spdif_flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "spdif_flags" },
99 { "be", "output in big-endian format (for use as s16be)", 0, AV_OPT_TYPE_CONST, {.i64 = SPDIF_FLAG_BIGENDIAN}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "spdif_flags" },
100 { "dtshd_rate", "mux complete DTS frames in HD mode at the specified IEC958 rate (in Hz, default 0=disabled)", offsetof(IEC61937Context, dtshd_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 768000, AV_OPT_FLAG_ENCODING_PARAM },
101 { "dtshd_fallback_time", "min secs to strip HD for after an overflow (-1: till the end, default 60)", offsetof(IEC61937Context, dtshd_fallback), AV_OPT_TYPE_INT, {.i64 = 60}, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
102 { NULL },
103 };
104
105 static const AVClass spdif_class = {
106 .class_name = "spdif",
107 .item_name = av_default_item_name,
108 .option = options,
109 .version = LIBAVUTIL_VERSION_INT,
110 };
111
112 56 static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
113 {
114 56 IEC61937Context *ctx = s->priv_data;
115 56 int bitstream_mode = pkt->data[5] & 0x7;
116
117 56 ctx->data_type = IEC61937_AC3 | (bitstream_mode << 8);
118 56 ctx->pkt_offset = AC3_FRAME_SIZE << 2;
119 56 return 0;
120 }
121
122 1304 static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
123 {
124 1304 IEC61937Context *ctx = s->priv_data;
125 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
126 1304 int repeat = 1;
127 uint8_t *tmp;
128
129 1304 int bsid = pkt->data[5] >> 3;
130
3/4
✓ Branch 0 taken 1304 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1302 times.
✓ Branch 3 taken 2 times.
1304 if (bsid > 10 && (pkt->data[4] & 0xc0) != 0xc0) /* fscod */
131 1302 repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4]; /* numblkscod */
132
133 1304 tmp = av_fast_realloc(ctx->hd_buf[0], &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1304 times.
1304 if (!tmp)
135 return AVERROR(ENOMEM);
136 1304 ctx->hd_buf[0] = tmp;
137
138 1304 memcpy(&ctx->hd_buf[0][ctx->hd_buf_filled], pkt->data, pkt->size);
139
140 1304 ctx->hd_buf_filled += pkt->size;
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1304 times.
1304 if (++ctx->hd_buf_count < repeat){
142 ctx->pkt_offset = 0;
143 return 0;
144 }
145 1304 ctx->data_type = IEC61937_EAC3;
146 1304 ctx->pkt_offset = 24576;
147 1304 ctx->out_buf = ctx->hd_buf[0];
148 1304 ctx->out_bytes = ctx->hd_buf_filled;
149 1304 ctx->length_code = ctx->hd_buf_filled;
150
151 1304 ctx->hd_buf_count = 0;
152 1304 ctx->hd_buf_filled = 0;
153 1304 return 0;
154 }
155
156 /*
157 * DTS type IV (DTS-HD) can be transmitted with various frame repetition
158 * periods; longer repetition periods allow for longer packets and therefore
159 * higher bitrate. Longer repetition periods mean that the constant bitrate of
160 * the output IEC 61937 stream is higher.
161 * The repetition period is measured in IEC 60958 frames (4 bytes).
162 */
163 2344 static int spdif_dts4_subtype(int period)
164 {
165
2/7
✗ Branch 0 not taken.
✓ Branch 1 taken 1172 times.
✓ Branch 2 taken 1172 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2344 switch (period) {
166 case 512: return 0x0;
167 1172 case 1024: return 0x1;
168 1172 case 2048: return 0x2;
169 case 4096: return 0x3;
170 case 8192: return 0x4;
171 case 16384: return 0x5;
172 }
173 return -1;
174 }
175
176 2344 static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size,
177 int sample_rate, int blocks)
178 {
179 2344 IEC61937Context *ctx = s->priv_data;
180 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
181 2344 int pkt_size = pkt->size;
182 int period;
183 int subtype;
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2344 times.
2344 if (!core_size) {
186 av_log(s, AV_LOG_ERROR, "HD mode not supported for this format\n");
187 return AVERROR(EINVAL);
188 }
189
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2344 times.
2344 if (!sample_rate) {
191 av_log(s, AV_LOG_ERROR, "Unknown DTS sample rate for HD\n");
192 return AVERROR_INVALIDDATA;
193 }
194
195 2344 period = ctx->dtshd_rate * (blocks << 5) / sample_rate;
196 2344 subtype = spdif_dts4_subtype(period);
197
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2344 times.
2344 if (subtype < 0) {
199 av_log(s, AV_LOG_ERROR, "Specified HD rate of %d Hz would require an "
200 "impossible repetition period of %d for the current DTS stream"
201 " (blocks = %d, sample rate = %d)\n", ctx->dtshd_rate, period,
202 blocks << 5, sample_rate);
203 return AVERROR(EINVAL);
204 }
205
206 /* set pkt_offset and DTS IV subtype according to the requested output
207 * rate */
208 2344 ctx->pkt_offset = period * 4;
209 2344 ctx->data_type = IEC61937_DTSHD | subtype << 8;
210
211 /* If the bitrate is too high for transmitting at the selected
212 * repetition period setting, strip DTS-HD until a good amount
213 * of consecutive non-overflowing HD frames have been observed.
214 * This generally only happens if the caller is cramming a Master
215 * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
216 2344 if (sizeof(dtshd_start_code) + 2 + pkt_size
217
3/4
✓ Branch 0 taken 1069 times.
✓ Branch 1 taken 1275 times.
✓ Branch 2 taken 1069 times.
✗ Branch 3 not taken.
2344 > ctx->pkt_offset - BURST_HEADER_SIZE && core_size) {
218
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1068 times.
1069 if (!ctx->dtshd_skip)
219 1 av_log(s, AV_LOG_WARNING, "DTS-HD bitrate too high, "
220 "temporarily sending core only\n");
221
1/2
✓ Branch 0 taken 1069 times.
✗ Branch 1 not taken.
1069 if (ctx->dtshd_fallback > 0)
222 1069 ctx->dtshd_skip = sample_rate * ctx->dtshd_fallback / (blocks << 5);
223 else
224 /* skip permanently (dtshd_fallback == -1) or just once
225 * (dtshd_fallback == 0) */
226 ctx->dtshd_skip = 1;
227 }
228
3/4
✓ Branch 0 taken 1074 times.
✓ Branch 1 taken 1270 times.
✓ Branch 2 taken 1074 times.
✗ Branch 3 not taken.
2344 if (ctx->dtshd_skip && core_size) {
229 1074 pkt_size = core_size;
230
1/2
✓ Branch 0 taken 1074 times.
✗ Branch 1 not taken.
1074 if (ctx->dtshd_fallback >= 0)
231 1074 --ctx->dtshd_skip;
232 }
233
234 2344 ctx->out_bytes = sizeof(dtshd_start_code) + 2 + pkt_size;
235
236 /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
237 * with some receivers, but the exact requirement is unconfirmed. */
238 2344 ctx->length_code = FFALIGN(ctx->out_bytes + 0x8, 0x10) - 0x8;
239
240 2344 av_fast_malloc(&ctx->hd_buf[0], &ctx->hd_buf_size, ctx->out_bytes);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2344 times.
2344 if (!ctx->hd_buf[0])
242 return AVERROR(ENOMEM);
243
244 2344 ctx->out_buf = ctx->hd_buf[0];
245
246 2344 memcpy(ctx->hd_buf[0], dtshd_start_code, sizeof(dtshd_start_code));
247 2344 AV_WB16(ctx->hd_buf[0] + sizeof(dtshd_start_code), pkt_size);
248 2344 memcpy(ctx->hd_buf[0] + sizeof(dtshd_start_code) + 2, pkt->data, pkt_size);
249
250 2344 return 0;
251 }
252
253 3529 static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
254 {
255 3529 IEC61937Context *ctx = s->priv_data;
256 3529 uint32_t syncword_dts = AV_RB32(pkt->data);
257 int blocks;
258 3529 int sample_rate = 0;
259 3529 int core_size = 0;
260
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3529 times.
3529 if (pkt->size < 9)
262 return AVERROR_INVALIDDATA;
263
264
1/6
✓ Branch 0 taken 3529 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3529 switch (syncword_dts) {
265 3529 case DCA_SYNCWORD_CORE_BE:
266 3529 blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
267 3529 core_size = ((AV_RB24(pkt->data + 5) >> 4) & 0x3fff) + 1;
268 3529 sample_rate = ff_dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
269 3529 break;
270 case DCA_SYNCWORD_CORE_LE:
271 blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
272 ctx->extra_bswap = 1;
273 break;
274 case DCA_SYNCWORD_CORE_14B_BE:
275 blocks =
276 (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
277 break;
278 case DCA_SYNCWORD_CORE_14B_LE:
279 blocks =
280 (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
281 ctx->extra_bswap = 1;
282 break;
283 case DCA_SYNCWORD_SUBSTREAM:
284 /* We only handle HD frames that are paired with core. However,
285 sometimes DTS-HD streams with core have a stray HD frame without
286 core in the beginning of the stream. */
287 av_log(s, AV_LOG_ERROR, "stray DTS-HD frame\n");
288 return AVERROR_INVALIDDATA;
289 default:
290 av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%"PRIx32"\n", syncword_dts);
291 return AVERROR_INVALIDDATA;
292 }
293 3529 blocks++;
294
295
2/2
✓ Branch 0 taken 2344 times.
✓ Branch 1 taken 1185 times.
3529 if (ctx->dtshd_rate)
296 /* DTS type IV output requested */
297 2344 return spdif_header_dts4(s, pkt, core_size, sample_rate, blocks);
298
299
1/4
✓ Branch 0 taken 1185 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1185 switch (blocks) {
300 1185 case 512 >> 5: ctx->data_type = IEC61937_DTS1; break;
301 case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
302 case 2048 >> 5: ctx->data_type = IEC61937_DTS3; break;
303 default:
304 av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
305 blocks << 5);
306 return AVERROR(ENOSYS);
307 }
308
309 /* discard extraneous data by default */
310
3/4
✓ Branch 0 taken 1185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1173 times.
✓ Branch 3 taken 12 times.
1185 if (core_size && core_size < pkt->size) {
311 1173 ctx->out_bytes = core_size;
312 1173 ctx->length_code = core_size << 3;
313 }
314
315 1185 ctx->pkt_offset = blocks << 7;
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1185 times.
1185 if (ctx->out_bytes == ctx->pkt_offset) {
318 /* The DTS stream fits exactly into the output stream, so skip the
319 * preamble as it would not fit in there. This is the case for dts
320 * discs and dts-in-wav. */
321 ctx->use_preamble = 0;
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1185 times.
1185 } else if (ctx->out_bytes > ctx->pkt_offset - BURST_HEADER_SIZE) {
323 avpriv_request_sample(s, "Unrecognized large DTS frame");
324 /* This will fail with a "bitrate too high" in the caller */
325 }
326
327 1185 return 0;
328 }
329
330 static const enum IEC61937DataType mpeg_data_type[2][3] = {
331 // LAYER1 LAYER2 LAYER3
332 { IEC61937_MPEG2_LAYER1_LSF, IEC61937_MPEG2_LAYER2_LSF, IEC61937_MPEG2_LAYER3_LSF }, // MPEG-2 LSF
333 { IEC61937_MPEG1_LAYER1, IEC61937_MPEG1_LAYER23, IEC61937_MPEG1_LAYER23 }, // MPEG-1
334 };
335
336 82 static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
337 {
338 82 IEC61937Context *ctx = s->priv_data;
339 82 int version = (pkt->data[1] >> 3) & 3;
340 82 int layer = 3 - ((pkt->data[1] >> 1) & 3);
341 82 int extension = pkt->data[2] & 1;
342
343
2/4
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 82 times.
82 if (layer == 3 || version == 1) {
344 av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
345 return AVERROR_INVALIDDATA;
346 }
347 82 av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
348
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
82 if (version == 2 && extension) {
349 ctx->data_type = IEC61937_MPEG2_EXT;
350 ctx->pkt_offset = 4608;
351 } else {
352 82 ctx->data_type = mpeg_data_type [version & 1][layer];
353 82 ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
354 }
355 // TODO Data type dependent info (normal/karaoke, dynamic range control)
356 82 return 0;
357 }
358
359 86 static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
360 {
361 86 IEC61937Context *ctx = s->priv_data;
362 uint32_t samples;
363 uint8_t frames;
364 int ret;
365
366 86 ret = av_adts_header_parse(pkt->data, &samples, &frames);
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 if (ret < 0) {
368 av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
369 return ret;
370 }
371
372 86 ctx->pkt_offset = samples << 2;
373
1/4
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
86 switch (frames) {
374 86 case 1:
375 86 ctx->data_type = IEC61937_MPEG2_AAC;
376 86 break;
377 case 2:
378 ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048;
379 break;
380 case 4:
381 ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096;
382 break;
383 default:
384 av_log(s, AV_LOG_ERROR,
385 "%"PRIu32" samples in AAC frame not supported\n", samples);
386 return AVERROR(EINVAL);
387 }
388 //TODO Data type dependent info (LC profile/SBR)
389 86 return 0;
390 }
391
392
393 /*
394 * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
395 * they can be encapsulated in IEC 61937.
396 */
397 #define MAT_PKT_OFFSET 61440
398 #define MAT_FRAME_SIZE 61424
399
400 static const uint8_t mat_start_code[20] = {
401 0x07, 0x9E, 0x00, 0x03, 0x84, 0x01, 0x01, 0x01, 0x80, 0x00, 0x56, 0xA5, 0x3B, 0xF4, 0x81, 0x83,
402 0x49, 0x80, 0x77, 0xE0,
403 };
404 static const uint8_t mat_middle_code[12] = {
405 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0,
406 };
407 static const uint8_t mat_end_code[16] = {
408 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11,
409 };
410
411 #define MAT_CODE(position, data) { .pos = position, .code = data, .len = sizeof(data) }
412
413 static const struct {
414 unsigned int pos;
415 unsigned int len;
416 const uint8_t *code;
417 } mat_codes[] = {
418 MAT_CODE(0, mat_start_code),
419 MAT_CODE(30708, mat_middle_code),
420 MAT_CODE(MAT_FRAME_SIZE - sizeof(mat_end_code), mat_end_code),
421 };
422
423 9095 static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
424 {
425 9095 IEC61937Context *ctx = s->priv_data;
426 9095 uint8_t *hd_buf = ctx->hd_buf[ctx->hd_buf_idx];
427 int ratebits;
428 9095 int padding_remaining = 0;
429 uint16_t input_timing;
430 9095 int total_frame_size = pkt->size;
431 9095 const uint8_t *dataptr = pkt->data;
432 9095 int data_remaining = pkt->size;
433 9095 int have_pkt = 0;
434 int next_code_idx;
435
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9095 times.
9095 if (pkt->size < 10)
437 return AVERROR_INVALIDDATA;
438
439
2/2
✓ Branch 0 taken 1116 times.
✓ Branch 1 taken 7979 times.
9095 if (AV_RB24(pkt->data + 4) == 0xf8726f) {
440 /* major sync unit, fetch sample rate */
441
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1115 times.
1116 if (pkt->data[7] == 0xba)
442 1 ratebits = pkt->data[8] >> 4;
443
1/2
✓ Branch 0 taken 1115 times.
✗ Branch 1 not taken.
1115 else if (pkt->data[7] == 0xbb)
444 1115 ratebits = pkt->data[9] >> 4;
445 else
446 return AVERROR_INVALIDDATA;
447
448 1116 ctx->truehd_samples_per_frame = 40 << (ratebits & 3);
449 1116 av_log(s, AV_LOG_TRACE, "TrueHD samples per frame: %d\n",
450 ctx->truehd_samples_per_frame);
451 }
452
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9095 times.
9095 if (!ctx->truehd_samples_per_frame)
454 return AVERROR_INVALIDDATA;
455
456 9095 input_timing = AV_RB16(pkt->data + 2);
457
2/2
✓ Branch 0 taken 9093 times.
✓ Branch 1 taken 2 times.
9095 if (ctx->truehd_prev_size) {
458 9093 uint16_t delta_samples = input_timing - ctx->truehd_prev_time;
459 /*
460 * One multiple-of-48kHz frame is 1/1200 sec and the IEC 61937 rate
461 * is 768kHz = 768000*4 bytes/sec.
462 * The nominal space per frame is therefore
463 * (768000*4 bytes/sec) * (1/1200 sec) = 2560 bytes.
464 * For multiple-of-44.1kHz frames: 1/1102.5 sec, 705.6kHz, 2560 bytes.
465 *
466 * 2560 is divisible by truehd_samples_per_frame.
467 */
468 9093 int delta_bytes = delta_samples * 2560 / ctx->truehd_samples_per_frame;
469
470 /* padding needed before this frame */
471 9093 padding_remaining = delta_bytes - ctx->truehd_prev_size;
472
473 9093 av_log(s, AV_LOG_TRACE, "delta_samples: %"PRIu16", delta_bytes: %d\n",
474 delta_samples, delta_bytes);
475
476 /* sanity check */
477
2/4
✓ Branch 0 taken 9093 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9093 times.
9093 if (padding_remaining < 0 || padding_remaining >= MAT_FRAME_SIZE / 2) {
478 avpriv_request_sample(s, "Unusual frame timing: %"PRIu16" => %"PRIu16", %d samples/frame",
479 ctx->truehd_prev_time, input_timing, ctx->truehd_samples_per_frame);
480 padding_remaining = 0;
481 }
482 }
483
484
1/2
✓ Branch 0 taken 22726 times.
✗ Branch 1 not taken.
22726 for (next_code_idx = 0; next_code_idx < FF_ARRAY_ELEMS(mat_codes); next_code_idx++)
485
2/2
✓ Branch 0 taken 9095 times.
✓ Branch 1 taken 13631 times.
22726 if (ctx->hd_buf_filled <= mat_codes[next_code_idx].pos)
486 9095 break;
487
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9095 times.
9095 if (next_code_idx >= FF_ARRAY_ELEMS(mat_codes))
489 return AVERROR_BUG;
490
491
4/4
✓ Branch 0 taken 9855 times.
✓ Branch 1 taken 9470 times.
✓ Branch 2 taken 375 times.
✓ Branch 3 taken 9095 times.
19325 while (padding_remaining || data_remaining ||
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9095 times.
9095 mat_codes[next_code_idx].pos == ctx->hd_buf_filled) {
493
494
2/2
✓ Branch 0 taken 1137 times.
✓ Branch 1 taken 9093 times.
10230 if (mat_codes[next_code_idx].pos == ctx->hd_buf_filled) {
495 /* time to insert MAT code */
496 1137 int code_len = mat_codes[next_code_idx].len;
497 1137 int code_len_remaining = code_len;
498 1137 memcpy(hd_buf + mat_codes[next_code_idx].pos,
499 1137 mat_codes[next_code_idx].code, code_len);
500 1137 ctx->hd_buf_filled += code_len;
501
502 1137 next_code_idx++;
503
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 759 times.
1137 if (next_code_idx == FF_ARRAY_ELEMS(mat_codes)) {
504 378 next_code_idx = 0;
505
506 /* this was the last code, move to the next MAT frame */
507 378 have_pkt = 1;
508 378 ctx->out_buf = hd_buf;
509 378 ctx->hd_buf_idx ^= 1;
510 378 hd_buf = ctx->hd_buf[ctx->hd_buf_idx];
511 378 ctx->hd_buf_filled = 0;
512
513 /* inter-frame gap has to be counted as well, add it */
514 378 code_len_remaining += MAT_PKT_OFFSET - MAT_FRAME_SIZE;
515 }
516
517
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 375 times.
1137 if (padding_remaining) {
518 /* consider the MAT code as padding */
519 762 int counted_as_padding = FFMIN(padding_remaining,
520 code_len_remaining);
521 762 padding_remaining -= counted_as_padding;
522 762 code_len_remaining -= counted_as_padding;
523 }
524 /* count the remainder of the code as part of frame size */
525
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 762 times.
1137 if (code_len_remaining)
526 375 total_frame_size += code_len_remaining;
527 }
528
529
2/2
✓ Branch 0 taken 9108 times.
✓ Branch 1 taken 1122 times.
10230 if (padding_remaining) {
530 9108 int padding_to_insert = FFMIN(mat_codes[next_code_idx].pos - ctx->hd_buf_filled,
531 padding_remaining);
532
533 9108 memset(hd_buf + ctx->hd_buf_filled, 0, padding_to_insert);
534 9108 ctx->hd_buf_filled += padding_to_insert;
535 9108 padding_remaining -= padding_to_insert;
536
537
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 8346 times.
9108 if (padding_remaining)
538 762 continue; /* time to insert MAT code */
539 }
540
541
1/2
✓ Branch 0 taken 9468 times.
✗ Branch 1 not taken.
9468 if (data_remaining) {
542 9468 int data_to_insert = FFMIN(mat_codes[next_code_idx].pos - ctx->hd_buf_filled,
543 data_remaining);
544
545 9468 memcpy(hd_buf + ctx->hd_buf_filled, dataptr, data_to_insert);
546 9468 ctx->hd_buf_filled += data_to_insert;
547 9468 dataptr += data_to_insert;
548 9468 data_remaining -= data_to_insert;
549 }
550 }
551
552 9095 ctx->truehd_prev_size = total_frame_size;
553 9095 ctx->truehd_prev_time = input_timing;
554
555 9095 av_log(s, AV_LOG_TRACE, "TrueHD frame inserted, total size %d, buffer position %d\n",
556 total_frame_size, ctx->hd_buf_filled);
557
558
2/2
✓ Branch 0 taken 8717 times.
✓ Branch 1 taken 378 times.
9095 if (!have_pkt) {
559 8717 ctx->pkt_offset = 0;
560 8717 return 0;
561 }
562
563 378 ctx->data_type = IEC61937_TRUEHD;
564 378 ctx->pkt_offset = MAT_PKT_OFFSET;
565 378 ctx->out_bytes = MAT_FRAME_SIZE;
566 378 ctx->length_code = MAT_FRAME_SIZE;
567 378 return 0;
568 }
569
570 13 static int spdif_write_header(AVFormatContext *s)
571 {
572 13 IEC61937Context *ctx = s->priv_data;
573
574
6/7
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
13 switch (s->streams[0]->codecpar->codec_id) {
575 1 case AV_CODEC_ID_AC3:
576 1 ctx->header_info = spdif_header_ac3;
577 1 break;
578 2 case AV_CODEC_ID_EAC3:
579 2 ctx->header_info = spdif_header_eac3;
580 2 break;
581 2 case AV_CODEC_ID_MP1:
582 case AV_CODEC_ID_MP2:
583 case AV_CODEC_ID_MP3:
584 2 ctx->header_info = spdif_header_mpeg;
585 2 break;
586 5 case AV_CODEC_ID_DTS:
587 5 ctx->header_info = spdif_header_dts;
588 5 break;
589 1 case AV_CODEC_ID_AAC:
590 1 ctx->header_info = spdif_header_aac;
591 1 break;
592 2 case AV_CODEC_ID_TRUEHD:
593 case AV_CODEC_ID_MLP:
594 2 ctx->header_info = spdif_header_truehd;
595
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int i = 0; i < FF_ARRAY_ELEMS(ctx->hd_buf); i++) {
596 4 ctx->hd_buf[i] = av_malloc(MAT_FRAME_SIZE);
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ctx->hd_buf[i])
598 return AVERROR(ENOMEM);
599 }
600 2 break;
601 default:
602 avpriv_report_missing_feature(s, "Codec %d",
603 s->streams[0]->codecpar->codec_id);
604 return AVERROR_PATCHWELCOME;
605 }
606 13 return 0;
607 }
608
609 13 static void spdif_deinit(AVFormatContext *s)
610 {
611 13 IEC61937Context *ctx = s->priv_data;
612 13 av_freep(&ctx->buffer);
613
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 13 times.
39 for (int i = 0; i < FF_ARRAY_ELEMS(ctx->hd_buf); i++)
614 26 av_freep(&ctx->hd_buf[i]);
615 13 }
616
617 21782 static av_always_inline void spdif_put_16(IEC61937Context *ctx,
618 AVIOContext *pb, unsigned int val)
619 {
620
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 21758 times.
21782 if (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)
621 24 avio_wb16(pb, val);
622 else
623 21758 avio_wl16(pb, val);
624 21782 }
625
626 14152 static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
627 {
628 14152 IEC61937Context *ctx = s->priv_data;
629 int ret, padding;
630
631 14152 ctx->out_buf = pkt->data;
632 14152 ctx->out_bytes = pkt->size;
633 14152 ctx->length_code = FFALIGN(pkt->size, 2) << 3;
634 14152 ctx->use_preamble = 1;
635 14152 ctx->extra_bswap = 0;
636
637 14152 ret = ctx->header_info(s, pkt);
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14152 times.
14152 if (ret < 0)
639 return ret;
640
2/2
✓ Branch 0 taken 8717 times.
✓ Branch 1 taken 5435 times.
14152 if (!ctx->pkt_offset)
641 8717 return 0;
642
643 5435 padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5435 times.
5435 if (padding < 0) {
645 av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
646 return AVERROR(EINVAL);
647 }
648
649
1/2
✓ Branch 0 taken 5435 times.
✗ Branch 1 not taken.
5435 if (ctx->use_preamble) {
650 5435 spdif_put_16(ctx, s->pb, SYNCWORD1); //Pa
651 5435 spdif_put_16(ctx, s->pb, SYNCWORD2); //Pb
652 5435 spdif_put_16(ctx, s->pb, ctx->data_type); //Pc
653 5435 spdif_put_16(ctx, s->pb, ctx->length_code);//Pd
654 }
655
656
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5429 times.
5435 if (ctx->extra_bswap ^ (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)) {
657 6 avio_write(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
658 } else {
659 5429 av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5429 times.
5429 if (!ctx->buffer)
661 return AVERROR(ENOMEM);
662 5429 ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (const uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
663 5429 avio_write(s->pb, ctx->buffer, ctx->out_bytes & ~1);
664 }
665
666 /* a final lone byte has to be MSB aligned */
667
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 5393 times.
5435 if (ctx->out_bytes & 1)
668 42 spdif_put_16(ctx, s->pb, ctx->out_buf[ctx->out_bytes - 1] << 8);
669
670 5435 ffio_fill(s->pb, 0, padding);
671
672 5435 av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
673 5435 ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
674
675 5435 return 0;
676 }
677
678 const FFOutputFormat ff_spdif_muxer = {
679 .p.name = "spdif",
680 .p.long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (used on S/PDIF - IEC958)"),
681 .p.extensions = "spdif",
682 .priv_data_size = sizeof(IEC61937Context),
683 .p.audio_codec = AV_CODEC_ID_AC3,
684 .p.video_codec = AV_CODEC_ID_NONE,
685 .p.subtitle_codec = AV_CODEC_ID_NONE,
686 .write_header = spdif_write_header,
687 .write_packet = spdif_write_packet,
688 .deinit = spdif_deinit,
689 .p.flags = AVFMT_NOTIMESTAMPS,
690 .p.priv_class = &spdif_class,
691 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
692 };
693