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
27 #include "libavutil/crc.h"
28 #include "bitstream.h"
30 #include "mlp_parser.h"
32 static const uint8_t mlp_quants
[16] = {
33 16, 20, 24, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0,
37 static const uint8_t mlp_channels
[32] = {
38 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
39 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 static const uint8_t thd_chancount
[13] = {
43 // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
44 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
47 static int mlp_samplerate(int in
)
52 return (in
& 8 ? 44100 : 48000) << (in
& 7) ;
55 static int truehd_channels(int chanmap
)
59 for (i
= 0; i
< 13; i
++)
60 channels
+= thd_chancount
[i
] * ((chanmap
>> i
) & 1);
65 static int crc_init
= 0;
66 static AVCRC crc_2D
[1024];
68 /** MLP uses checksums that seem to be based on the standard CRC algorithm,
69 * but not (in implementation terms, the table lookup and XOR are reversed).
70 * We can implement this behavior using a standard av_crc on all but the
71 * last element, then XOR that with the last element.
74 static uint16_t mlp_checksum16(const uint8_t *buf
, unsigned int buf_size
)
79 av_crc_init(crc_2D
, 0, 16, 0x002D, sizeof(crc_2D
));
83 crc
= av_crc(crc_2D
, 0, buf
, buf_size
- 2);
84 crc
^= AV_RL16(buf
+ buf_size
- 2);
88 /** Read a major sync info header - contains high level information about
89 * the stream - sample rate, channel arrangement etc. Most of this
90 * information is not actually necessary for decoding, only for playback.
93 int ff_mlp_read_major_sync(void *log
, MLPHeaderInfo
*mh
, const uint8_t *buf
,
94 unsigned int buf_size
)
101 av_log(log
, AV_LOG_ERROR
, "Packet too short, unable to read major sync\n");
105 checksum
= mlp_checksum16(buf
, 26);
106 if (checksum
!= AV_RL16(buf
+26)) {
107 av_log(log
, AV_LOG_ERROR
, "Major sync info header checksum error\n");
111 init_get_bits(&gb
, buf
, buf_size
* 8);
113 if (get_bits_long(&gb
, 24) != 0xf8726f) /* Sync words */
116 mh
->stream_type
= get_bits(&gb
, 8);
118 if (mh
->stream_type
== 0xbb) {
119 mh
->group1_bits
= mlp_quants
[get_bits(&gb
, 4)];
120 mh
->group2_bits
= mlp_quants
[get_bits(&gb
, 4)];
122 ratebits
= get_bits(&gb
, 4);
123 mh
->group1_samplerate
= mlp_samplerate(ratebits
);
124 mh
->group2_samplerate
= mlp_samplerate(get_bits(&gb
, 4));
128 mh
->channels_mlp
= get_bits(&gb
, 5);
129 } else if (mh
->stream_type
== 0xba) {
130 mh
->group1_bits
= 24; // TODO: Is this information actually conveyed anywhere?
133 ratebits
= get_bits(&gb
, 4);
134 mh
->group1_samplerate
= mlp_samplerate(ratebits
);
135 mh
->group2_samplerate
= 0;
139 mh
->channels_thd_stream1
= get_bits(&gb
, 5);
143 mh
->channels_thd_stream2
= get_bits(&gb
, 13);
147 mh
->access_unit_size
= 40 << (ratebits
& 7);
148 mh
->access_unit_size_pow2
= 64 << (ratebits
& 7);
150 skip_bits_long(&gb
, 48);
152 mh
->is_vbr
= get_bits1(&gb
);
154 mh
->peak_bitrate
= (get_bits(&gb
, 15) * mh
->group1_samplerate
+ 8) >> 4;
156 mh
->num_substreams
= get_bits(&gb
, 4);
158 skip_bits_long(&gb
, 4 + 11 * 8);
163 typedef struct MLPParseContext
174 static int mlp_parse(AVCodecParserContext
*s
,
175 AVCodecContext
*avctx
,
176 const uint8_t **poutbuf
, int *poutbuf_size
,
177 const uint8_t *buf
, int buf_size
)
179 MLPParseContext
*mp
= s
->priv_data
;
190 // Not in sync - find a major sync header
192 for (i
= 0; i
< buf_size
; i
++) {
193 mp
->pc
.state
= (mp
->pc
.state
<< 8) | buf
[i
];
194 if ((mp
->pc
.state
& 0xfffffffe) == 0xf8726fba) {
202 ff_combine_frame(&mp
->pc
, END_NOT_FOUND
, &buf
, &buf_size
);
206 ff_combine_frame(&mp
->pc
, i
- 7, &buf
, &buf_size
);
211 if (mp
->bytes_left
== 0) {
212 // Find length of this packet
214 /* Copy overread bytes from last frame into buffer. */
215 for(; mp
->pc
.overread
>0; mp
->pc
.overread
--) {
216 mp
->pc
.buffer
[mp
->pc
.index
++]= mp
->pc
.buffer
[mp
->pc
.overread_index
++];
219 if (mp
->pc
.index
+ buf_size
< 2) {
220 ff_combine_frame(&mp
->pc
, END_NOT_FOUND
, &buf
, &buf_size
);
224 mp
->bytes_left
= ((mp
->pc
.index
> 0 ? mp
->pc
.buffer
[0] : buf
[0]) << 8)
225 | (mp
->pc
.index
> 1 ? mp
->pc
.buffer
[1] : buf
[1-mp
->pc
.index
]);
226 mp
->bytes_left
= (mp
->bytes_left
& 0xfff) * 2;
227 mp
->bytes_left
-= mp
->pc
.index
;
230 next
= (mp
->bytes_left
> buf_size
) ? END_NOT_FOUND
: mp
->bytes_left
;
232 if (ff_combine_frame(&mp
->pc
, next
, &buf
, &buf_size
) < 0) {
233 mp
->bytes_left
-= buf_size
;
239 sync_present
= (AV_RB32(buf
+ 4) & 0xfffffffe) == 0xf8726fba;
242 // First nibble of a frame is a parity check of the first few nibbles.
243 // Only check when this isn't a sync frame - syncs have a checksum.
246 for (i
= 0; i
<= mp
->num_substreams
; i
++) {
247 parity_bits
^= buf
[p
++];
248 parity_bits
^= buf
[p
++];
250 if (i
== 0 || buf
[p
-2] & 0x80) {
251 parity_bits
^= buf
[p
++];
252 parity_bits
^= buf
[p
++];
256 if ((((parity_bits
>> 4) ^ parity_bits
) & 0xF) != 0xF) {
257 av_log(avctx
, AV_LOG_INFO
, "mlpparse: Parity check failed.\n");
263 if (ff_mlp_read_major_sync(avctx
, &mh
, buf
+ 4, buf_size
- 4) < 0)
266 #ifdef CONFIG_AUDIO_NONSHORT
267 avctx
->bits_per_sample
= mh
.group1_bits
;
268 if (avctx
->bits_per_sample
> 16)
269 avctx
->sample_fmt
= SAMPLE_FMT_S32
;
271 avctx
->sample_rate
= mh
.group1_samplerate
;
272 avctx
->frame_size
= mh
.access_unit_size
;
274 if (mh
.stream_type
== 0xbb) {
276 avctx
->channels
= mlp_channels
[mh
.channels_mlp
];
277 } else { /* mh.stream_type == 0xba */
279 if (mh
.channels_thd_stream2
)
280 avctx
->channels
= truehd_channels(mh
.channels_thd_stream2
);
282 avctx
->channels
= truehd_channels(mh
.channels_thd_stream1
);
285 if (!mh
.is_vbr
) /* Stream is CBR */
286 avctx
->bit_rate
= mh
.peak_bitrate
;
288 mp
->num_substreams
= mh
.num_substreams
;
292 *poutbuf_size
= buf_size
;
301 AVCodecParser mlp_parser
= {
303 sizeof(MLPParseContext
),