1 /*
2 * MLP parser
3 * Copyright (c) 2007 Ian Caulfield
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 /**
23 * @file
24 * MLP parser
25 */
26
27 #include <stdint.h>
28
36
38 16, 20, 24, 0, 0, 0, 0, 0,
39 0, 0, 0, 0, 0, 0, 0, 0,
40 };
41
43 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
44 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 };
46
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
70 };
71
73 // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
74 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
75 };
76
91 };
92
94 {
95 if (in == 0xF)
96 return 0;
97
98 return (in & 8 ? 44100 : 48000) << (in & 7) ;
99 }
100
102 {
103 int channels = 0, i;
104
105 for (i = 0; i < 13; i++)
107
108 return channels;
109 }
110
112 {
113 int i;
115
116 for (i = 0; i < 13; i++)
117 layout |=
thd_layout[i] * ((chanmap >> i) & 1);
118
120 }
121
123 {
124 int has_extension, extensions = 0;
126 if (bufsize < 28)
127 return -1;
128
129 if (
AV_RB32(buf) == 0xf8726fba) {
130 has_extension = buf[25] & 1;
131 if (has_extension) {
132 extensions = buf[26] >> 4;
133 size += 2 + extensions * 2;
134 }
135 }
137 }
138
139 /** Read a major sync info header - contains high level information about
140 * the stream - sample rate, channel arrangement etc. Most of this
141 * information is not actually necessary for decoding, only for playback.
142 * gb must be a freshly initialized GetBitContext with no bits read.
143 */
144
146 {
147 int ratebits, channel_arrangement, header_size;
148 uint16_t checksum;
149
151
153 if (header_size < 0 || gb->size_in_bits < header_size << 3) {
155 return -1;
156 }
157
162 }
163
166
169
173
177
179
181 channel_arrangement =
get_bits(gb, 5);
185 mh->
group1_bits = 24;
// TODO: Is this information actually conveyed anywhere?
187
191
193
196
198 channel_arrangement =
get_bits(gb, 5);
201
203
204 channel_arrangement =
get_bits(gb, 13);
207 } else
209
212
214
216
218
220
222
223 return 0;
224 }
225
227 {
229
231
233
236
238 {
240 return 0;
241 }
242
245 const uint8_t **poutbuf,
int *poutbuf_size,
247 {
249 int sync_present;
251 int next;
253 int i, p = 0;
254
255 *poutbuf_size = 0;
256 if (buf_size == 0)
257 return 0;
258
260 // Not in sync - find a major sync header
261
262 for (i = 0; i < buf_size; i++) {
264 if ((mp->
pc.
state & 0xfffffffe) == 0xf8726fba &&
265 // ignore if we do not have the data for the start of header
269 break;
270 }
271 }
272
276 return buf_size;
277 }
278
282 }
283
284 return i - 7;
285 }
286
288 // Find length of this packet
289
290 /* Copy overread bytes from last frame into buffer. */
293 }
294
295 if (mp->
pc.
index + buf_size < 2) {
298 return buf_size;
299 }
300
304 if (mp->
bytes_left <= 0) {
// prevent infinite loop
305 goto lost_sync;
306 }
308 }
309
311
314 return buf_size;
315 }
316
318
319 sync_present = (
AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
320
321 if (!sync_present) {
322 /* The first nibble of a frame is a parity check of the 4-byte
323 * access unit header and all the 2- or 4-byte substream headers. */
324 // Only check when this isn't a sync frame - syncs have a checksum.
325
326 parity_bits = 0;
328 parity_bits ^= buf[p++];
329 parity_bits ^= buf[p++];
330
331 if (i < 0 || buf[p-2] & 0x80) {
332 parity_bits ^= buf[p++];
333 parity_bits ^= buf[p++];
334 }
335 }
336
337 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
339 goto lost_sync;
340 }
341 } else {
344
347 goto lost_sync;
348
352 else
356
359 /* MLP stream */
360 #if FF_API_REQUEST_CHANNELS
367 } else
368 #endif
375 } else {
378 }
379 } else { /* mh.stream_type == 0xba */
380 /* TrueHD stream */
381 #if FF_API_REQUEST_CHANNELS
392 } else
393 #endif
406 } else {
409 }
410 }
411 }
412
413 if (!mh.
is_vbr)
/* Stream is CBR */
415
417 }
418
420 *poutbuf_size = buf_size;
421
422 return next;
423
424 lost_sync:
426 return 1;
427 }
428
435 };