3 * Copyright (c) 2007 Ian Caulfield
5 * This file is part of FFmpeg.
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.
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.
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
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/internal.h"
34 #include "mlp_parser.h"
37 static const uint8_t mlp_quants
[16] = {
38 16, 20, 24, 0, 0, 0, 0, 0,
39 0, 0, 0, 0, 0, 0, 0, 0,
42 static const uint8_t mlp_channels
[32] = {
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,
47 const uint64_t ff_mlp_layout
[32] = {
52 AV_CH_LAYOUT_STEREO
|AV_CH_LOW_FREQUENCY
,
53 AV_CH_LAYOUT_2_1
|AV_CH_LOW_FREQUENCY
,
54 AV_CH_LAYOUT_QUAD
|AV_CH_LOW_FREQUENCY
,
55 AV_CH_LAYOUT_SURROUND
,
57 AV_CH_LAYOUT_5POINT0_BACK
,
58 AV_CH_LAYOUT_SURROUND
|AV_CH_LOW_FREQUENCY
,
59 AV_CH_LAYOUT_4POINT0
|AV_CH_LOW_FREQUENCY
,
60 AV_CH_LAYOUT_5POINT1_BACK
,
62 AV_CH_LAYOUT_5POINT0_BACK
,
63 AV_CH_LAYOUT_SURROUND
|AV_CH_LOW_FREQUENCY
,
64 AV_CH_LAYOUT_4POINT0
|AV_CH_LOW_FREQUENCY
,
65 AV_CH_LAYOUT_5POINT1_BACK
,
66 AV_CH_LAYOUT_QUAD
|AV_CH_LOW_FREQUENCY
,
67 AV_CH_LAYOUT_5POINT0_BACK
,
68 AV_CH_LAYOUT_5POINT1_BACK
,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
72 static const uint8_t thd_chancount
[13] = {
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
77 static const uint64_t thd_layout
[13] = {
78 AV_CH_FRONT_LEFT
|AV_CH_FRONT_RIGHT
, // LR
79 AV_CH_FRONT_CENTER
, // C
80 AV_CH_LOW_FREQUENCY
, // LFE
81 AV_CH_SIDE_LEFT
|AV_CH_SIDE_RIGHT
, // LRs
82 AV_CH_TOP_FRONT_LEFT
|AV_CH_TOP_FRONT_RIGHT
, // LRvh
83 AV_CH_FRONT_LEFT_OF_CENTER
|AV_CH_FRONT_RIGHT_OF_CENTER
, // LRc
84 AV_CH_BACK_LEFT
|AV_CH_BACK_RIGHT
, // LRrs
85 AV_CH_BACK_CENTER
, // Cs
86 AV_CH_TOP_CENTER
, // Ts
87 AV_CH_SURROUND_DIRECT_LEFT
|AV_CH_SURROUND_DIRECT_RIGHT
, // LRsd
88 AV_CH_WIDE_LEFT
|AV_CH_WIDE_RIGHT
, // LRw
89 AV_CH_TOP_FRONT_CENTER
, // Cvh
90 AV_CH_LOW_FREQUENCY_2
, // LFE2
93 static int mlp_samplerate(int in
)
98 return (in
& 8 ? 44100 : 48000) << (in
& 7) ;
101 static int truehd_channels(int chanmap
)
105 for (i
= 0; i
< 13; i
++)
106 channels
+= thd_chancount
[i
] * ((chanmap
>> i
) & 1);
111 uint64_t ff_truehd_layout(int chanmap
)
116 for (i
= 0; i
< 13; i
++)
117 layout
|= thd_layout
[i
] * ((chanmap
>> i
) & 1);
122 static int ff_mlp_get_major_sync_size(const uint8_t * buf
, int bufsize
)
124 int has_extension
, extensions
= 0;
129 if (AV_RB32(buf
) == 0xf8726fba) {
130 has_extension
= buf
[25] & 1;
132 extensions
= buf
[26] >> 4;
133 size
+= 2 + extensions
* 2;
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.
145 int ff_mlp_read_major_sync(void *log
, MLPHeaderInfo
*mh
, GetBitContext
*gb
)
147 int ratebits
, channel_arrangement
, header_size
;
150 av_assert1(get_bits_count(gb
) == 0);
152 header_size
= ff_mlp_get_major_sync_size(gb
->buffer
, gb
->size_in_bits
>> 3);
153 if (header_size
< 0 || gb
->size_in_bits
< header_size
<< 3) {
154 av_log(log
, AV_LOG_ERROR
, "packet too short, unable to read major sync\n");
158 checksum
= ff_mlp_checksum16(gb
->buffer
, header_size
- 2);
159 if (checksum
!= AV_RL16(gb
->buffer
+header_size
-2)) {
160 av_log(log
, AV_LOG_ERROR
, "major sync info header checksum error\n");
161 return AVERROR_INVALIDDATA
;
164 if (get_bits_long(gb
, 24) != 0xf8726f) /* Sync words */
165 return AVERROR_INVALIDDATA
;
167 mh
->stream_type
= get_bits(gb
, 8);
168 mh
->header_size
= header_size
;
170 if (mh
->stream_type
== 0xbb) {
171 mh
->group1_bits
= mlp_quants
[get_bits(gb
, 4)];
172 mh
->group2_bits
= mlp_quants
[get_bits(gb
, 4)];
174 ratebits
= get_bits(gb
, 4);
175 mh
->group1_samplerate
= mlp_samplerate(ratebits
);
176 mh
->group2_samplerate
= mlp_samplerate(get_bits(gb
, 4));
180 mh
->channel_arrangement
=
181 channel_arrangement
= get_bits(gb
, 5);
182 mh
->channels_mlp
= mlp_channels
[channel_arrangement
];
183 mh
->channel_layout_mlp
= ff_mlp_layout
[channel_arrangement
];
184 } else if (mh
->stream_type
== 0xba) {
185 mh
->group1_bits
= 24; // TODO: Is this information actually conveyed anywhere?
188 ratebits
= get_bits(gb
, 4);
189 mh
->group1_samplerate
= mlp_samplerate(ratebits
);
190 mh
->group2_samplerate
= 0;
194 mh
->channel_modifier_thd_stream0
= get_bits(gb
, 2);
195 mh
->channel_modifier_thd_stream1
= get_bits(gb
, 2);
197 mh
->channel_arrangement
=
198 channel_arrangement
= get_bits(gb
, 5);
199 mh
->channels_thd_stream1
= truehd_channels(channel_arrangement
);
200 mh
->channel_layout_thd_stream1
= ff_truehd_layout(channel_arrangement
);
202 mh
->channel_modifier_thd_stream2
= get_bits(gb
, 2);
204 channel_arrangement
= get_bits(gb
, 13);
205 mh
->channels_thd_stream2
= truehd_channels(channel_arrangement
);
206 mh
->channel_layout_thd_stream2
= ff_truehd_layout(channel_arrangement
);
208 return AVERROR_INVALIDDATA
;
210 mh
->access_unit_size
= 40 << (ratebits
& 7);
211 mh
->access_unit_size_pow2
= 64 << (ratebits
& 7);
213 skip_bits_long(gb
, 48);
215 mh
->is_vbr
= get_bits1(gb
);
217 mh
->peak_bitrate
= (get_bits(gb
, 15) * mh
->group1_samplerate
+ 8) >> 4;
219 mh
->num_substreams
= get_bits(gb
, 4);
221 skip_bits_long(gb
, 4 + (header_size
- 17) * 8);
226 typedef struct MLPParseContext
237 static av_cold
int mlp_init(AVCodecParserContext
*s
)
243 static int mlp_parse(AVCodecParserContext
*s
,
244 AVCodecContext
*avctx
,
245 const uint8_t **poutbuf
, int *poutbuf_size
,
246 const uint8_t *buf
, int buf_size
)
248 MLPParseContext
*mp
= s
->priv_data
;
260 // Not in sync - find a major sync header
262 for (i
= 0; i
< buf_size
; i
++) {
263 mp
->pc
.state
= (mp
->pc
.state
<< 8) | buf
[i
];
264 if ((mp
->pc
.state
& 0xfffffffe) == 0xf8726fba &&
265 // ignore if we do not have the data for the start of header
266 mp
->pc
.index
+ i
>= 7) {
274 if (ff_combine_frame(&mp
->pc
, END_NOT_FOUND
, &buf
, &buf_size
) != -1)
275 av_log(avctx
, AV_LOG_WARNING
, "ff_combine_frame failed\n");
279 if ((ret
= ff_combine_frame(&mp
->pc
, i
- 7, &buf
, &buf_size
)) < 0) {
280 av_log(avctx
, AV_LOG_WARNING
, "ff_combine_frame failed\n");
287 if (mp
->bytes_left
== 0) {
288 // Find length of this packet
290 /* Copy overread bytes from last frame into buffer. */
291 for(; mp
->pc
.overread
>0; mp
->pc
.overread
--) {
292 mp
->pc
.buffer
[mp
->pc
.index
++]= mp
->pc
.buffer
[mp
->pc
.overread_index
++];
295 if (mp
->pc
.index
+ buf_size
< 2) {
296 if (ff_combine_frame(&mp
->pc
, END_NOT_FOUND
, &buf
, &buf_size
) != -1)
297 av_log(avctx
, AV_LOG_WARNING
, "ff_combine_frame failed\n");
301 mp
->bytes_left
= ((mp
->pc
.index
> 0 ? mp
->pc
.buffer
[0] : buf
[0]) << 8)
302 | (mp
->pc
.index
> 1 ? mp
->pc
.buffer
[1] : buf
[1-mp
->pc
.index
]);
303 mp
->bytes_left
= (mp
->bytes_left
& 0xfff) * 2;
304 if (mp
->bytes_left
<= 0) { // prevent infinite loop
307 mp
->bytes_left
-= mp
->pc
.index
;
310 next
= (mp
->bytes_left
> buf_size
) ? END_NOT_FOUND
: mp
->bytes_left
;
312 if (ff_combine_frame(&mp
->pc
, next
, &buf
, &buf_size
) < 0) {
313 mp
->bytes_left
-= buf_size
;
319 sync_present
= (AV_RB32(buf
+ 4) & 0xfffffffe) == 0xf8726fba;
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.
327 for (i
= -1; i
< mp
->num_substreams
; i
++) {
328 parity_bits
^= buf
[p
++];
329 parity_bits
^= buf
[p
++];
331 if (i
< 0 || buf
[p
-2] & 0x80) {
332 parity_bits
^= buf
[p
++];
333 parity_bits
^= buf
[p
++];
337 if ((((parity_bits
>> 4) ^ parity_bits
) & 0xF) != 0xF) {
338 av_log(avctx
, AV_LOG_INFO
, "mlpparse: Parity check failed.\n");
345 init_get_bits(&gb
, buf
+ 4, (buf_size
- 4) << 3);
346 if (ff_mlp_read_major_sync(avctx
, &mh
, &gb
) < 0)
349 avctx
->bits_per_raw_sample
= mh
.group1_bits
;
350 if (avctx
->bits_per_raw_sample
> 16)
351 avctx
->sample_fmt
= AV_SAMPLE_FMT_S32
;
353 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16
;
354 avctx
->sample_rate
= mh
.group1_samplerate
;
355 s
->duration
= mh
.access_unit_size
;
357 if(!avctx
->channels
|| !avctx
->channel_layout
) {
358 if (mh
.stream_type
== 0xbb) {
360 #if FF_API_REQUEST_CHANNELS
361 FF_DISABLE_DEPRECATION_WARNINGS
362 if (avctx
->request_channels
> 0 && avctx
->request_channels
<= 2 &&
363 mh
.num_substreams
> 1) {
365 avctx
->channel_layout
= AV_CH_LAYOUT_STEREO
;
366 FF_ENABLE_DEPRECATION_WARNINGS
369 if (avctx
->request_channel_layout
&&
370 (avctx
->request_channel_layout
& AV_CH_LAYOUT_STEREO
) ==
371 avctx
->request_channel_layout
&&
372 mh
.num_substreams
> 1) {
374 avctx
->channel_layout
= AV_CH_LAYOUT_STEREO
;
376 avctx
->channels
= mh
.channels_mlp
;
377 avctx
->channel_layout
= mh
.channel_layout_mlp
;
379 } else { /* mh.stream_type == 0xba */
381 #if FF_API_REQUEST_CHANNELS
382 FF_DISABLE_DEPRECATION_WARNINGS
383 if (avctx
->request_channels
> 0 && avctx
->request_channels
<= 2 &&
384 mh
.num_substreams
> 1) {
386 avctx
->channel_layout
= AV_CH_LAYOUT_STEREO
;
387 } else if (avctx
->request_channels
> 0 &&
388 avctx
->request_channels
<= mh
.channels_thd_stream1
) {
389 avctx
->channels
= mh
.channels_thd_stream1
;
390 avctx
->channel_layout
= mh
.channel_layout_thd_stream1
;
391 FF_ENABLE_DEPRECATION_WARNINGS
394 if (avctx
->request_channel_layout
&&
395 (avctx
->request_channel_layout
& AV_CH_LAYOUT_STEREO
) ==
396 avctx
->request_channel_layout
&&
397 mh
.num_substreams
> 1) {
399 avctx
->channel_layout
= AV_CH_LAYOUT_STEREO
;
400 } else if (!mh
.channels_thd_stream2
||
401 (avctx
->request_channel_layout
&&
402 (avctx
->request_channel_layout
& mh
.channel_layout_thd_stream1
) ==
403 avctx
->request_channel_layout
)) {
404 avctx
->channels
= mh
.channels_thd_stream1
;
405 avctx
->channel_layout
= mh
.channel_layout_thd_stream1
;
407 avctx
->channels
= mh
.channels_thd_stream2
;
408 avctx
->channel_layout
= mh
.channel_layout_thd_stream2
;
413 if (!mh
.is_vbr
) /* Stream is CBR */
414 avctx
->bit_rate
= mh
.peak_bitrate
;
416 mp
->num_substreams
= mh
.num_substreams
;
420 *poutbuf_size
= buf_size
;
429 AVCodecParser ff_mlp_parser
= {
430 .codec_ids
= { AV_CODEC_ID_MLP
, AV_CODEC_ID_TRUEHD
},
431 .priv_data_size
= sizeof(MLPParseContext
),
432 .parser_init
= mlp_init
,
433 .parser_parse
= mlp_parse
,
434 .parser_close
= ff_parse_close
,