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"
33 #include "mlp_parser.h"
36 static const uint8_t mlp_quants[16] = {
37 16, 20, 24, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, 0, 0, 0, 0,
41 static const uint8_t mlp_channels[32] = {
42 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
43 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 const uint64_t ff_mlp_layout[32] = {
51 AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
52 AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
53 AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
54 AV_CH_LAYOUT_SURROUND,
56 AV_CH_LAYOUT_5POINT0_BACK,
57 AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
58 AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
59 AV_CH_LAYOUT_5POINT1_BACK,
61 AV_CH_LAYOUT_5POINT0_BACK,
62 AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
63 AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
64 AV_CH_LAYOUT_5POINT1_BACK,
65 AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
66 AV_CH_LAYOUT_5POINT0_BACK,
67 AV_CH_LAYOUT_5POINT1_BACK,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71 static const uint8_t thd_chancount[13] = {
72 // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
73 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
76 static const uint64_t thd_layout[13] = {
77 AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT, // LR
78 AV_CH_FRONT_CENTER, // C
79 AV_CH_LOW_FREQUENCY, // LFE
80 AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRs
81 AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT, // LRvh
82 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, // LRc
83 AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, // LRrs
84 AV_CH_BACK_CENTER, // Cs
85 AV_CH_TOP_CENTER, // Ts
86 AV_CH_SURROUND_DIRECT_LEFT|AV_CH_SURROUND_DIRECT_RIGHT, // LRsd
87 AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT, // LRw
88 AV_CH_TOP_FRONT_CENTER, // Cvh
89 AV_CH_LOW_FREQUENCY_2, // LFE2
92 static int mlp_samplerate(int in)
97 return (in & 8 ? 44100 : 48000) << (in & 7) ;
100 static int truehd_channels(int chanmap)
104 for (i = 0; i < 13; i++)
105 channels += thd_chancount[i] * ((chanmap >> i) & 1);
110 uint64_t ff_truehd_layout(int chanmap)
115 for (i = 0; i < 13; i++)
116 layout |= thd_layout[i] * ((chanmap >> i) & 1);
121 /** Read a major sync info header - contains high level information about
122 * the stream - sample rate, channel arrangement etc. Most of this
123 * information is not actually necessary for decoding, only for playback.
124 * gb must be a freshly initialized GetBitContext with no bits read.
127 int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
129 int ratebits, channel_arrangement;
132 av_assert1(get_bits_count(gb) == 0);
134 if (gb->size_in_bits < 28 << 3) {
135 av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
139 checksum = ff_mlp_checksum16(gb->buffer, 26);
140 if (checksum != AV_RL16(gb->buffer+26)) {
141 av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
142 return AVERROR_INVALIDDATA;
145 if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
146 return AVERROR_INVALIDDATA;
148 mh->stream_type = get_bits(gb, 8);
150 if (mh->stream_type == 0xbb) {
151 mh->group1_bits = mlp_quants[get_bits(gb, 4)];
152 mh->group2_bits = mlp_quants[get_bits(gb, 4)];
154 ratebits = get_bits(gb, 4);
155 mh->group1_samplerate = mlp_samplerate(ratebits);
156 mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
160 mh->channel_arrangement=
161 channel_arrangement = get_bits(gb, 5);
162 mh->channels_mlp = mlp_channels[channel_arrangement];
163 mh->channel_layout_mlp = ff_mlp_layout[channel_arrangement];
164 } else if (mh->stream_type == 0xba) {
165 mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
168 ratebits = get_bits(gb, 4);
169 mh->group1_samplerate = mlp_samplerate(ratebits);
170 mh->group2_samplerate = 0;
174 mh->channel_arrangement=
175 channel_arrangement = get_bits(gb, 5);
176 mh->channels_thd_stream1 = truehd_channels(channel_arrangement);
177 mh->channel_layout_thd_stream1 = ff_truehd_layout(channel_arrangement);
181 channel_arrangement = get_bits(gb, 13);
182 mh->channels_thd_stream2 = truehd_channels(channel_arrangement);
183 mh->channel_layout_thd_stream2 = ff_truehd_layout(channel_arrangement);
185 return AVERROR_INVALIDDATA;
187 mh->access_unit_size = 40 << (ratebits & 7);
188 mh->access_unit_size_pow2 = 64 << (ratebits & 7);
190 skip_bits_long(gb, 48);
192 mh->is_vbr = get_bits1(gb);
194 mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
196 mh->num_substreams = get_bits(gb, 4);
198 skip_bits_long(gb, 4 + 11 * 8);
203 typedef struct MLPParseContext
214 static av_cold int mlp_init(AVCodecParserContext *s)
220 static int mlp_parse(AVCodecParserContext *s,
221 AVCodecContext *avctx,
222 const uint8_t **poutbuf, int *poutbuf_size,
223 const uint8_t *buf, int buf_size)
225 MLPParseContext *mp = s->priv_data;
236 // Not in sync - find a major sync header
238 for (i = 0; i < buf_size; i++) {
239 mp->pc.state = (mp->pc.state << 8) | buf[i];
240 if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
241 // ignore if we do not have the data for the start of header
242 mp->pc.index + i >= 7) {
250 if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
251 av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
255 ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
260 if (mp->bytes_left == 0) {
261 // Find length of this packet
263 /* Copy overread bytes from last frame into buffer. */
264 for(; mp->pc.overread>0; mp->pc.overread--) {
265 mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
268 if (mp->pc.index + buf_size < 2) {
269 if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
270 av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
274 mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
275 | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
276 mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
277 if (mp->bytes_left <= 0) { // prevent infinite loop
280 mp->bytes_left -= mp->pc.index;
283 next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
285 if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
286 mp->bytes_left -= buf_size;
292 sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
295 /* The first nibble of a frame is a parity check of the 4-byte
296 * access unit header and all the 2- or 4-byte substream headers. */
297 // Only check when this isn't a sync frame - syncs have a checksum.
300 for (i = -1; i < mp->num_substreams; i++) {
301 parity_bits ^= buf[p++];
302 parity_bits ^= buf[p++];
304 if (i < 0 || buf[p-2] & 0x80) {
305 parity_bits ^= buf[p++];
306 parity_bits ^= buf[p++];
310 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
311 av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
318 init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
319 if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
322 avctx->bits_per_raw_sample = mh.group1_bits;
323 if (avctx->bits_per_raw_sample > 16)
324 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
326 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
327 avctx->sample_rate = mh.group1_samplerate;
328 s->duration = mh.access_unit_size;
330 if(!avctx->channels || !avctx->channel_layout) {
331 if (mh.stream_type == 0xbb) {
333 #if FF_API_REQUEST_CHANNELS
334 if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
335 mh.num_substreams > 1) {
337 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
340 if (avctx->request_channel_layout == AV_CH_LAYOUT_STEREO &&
341 mh.num_substreams > 1) {
343 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
345 avctx->channels = mh.channels_mlp;
346 avctx->channel_layout = mh.channel_layout_mlp;
348 } else { /* mh.stream_type == 0xba */
350 #if FF_API_REQUEST_CHANNELS
351 if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
352 mh.num_substreams > 1) {
354 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
355 } else if (avctx->request_channels > 0 &&
356 avctx->request_channels <= mh.channels_thd_stream1) {
357 avctx->channels = mh.channels_thd_stream1;
358 avctx->channel_layout = mh.channel_layout_thd_stream1;
361 if (avctx->request_channel_layout == AV_CH_LAYOUT_STEREO &&
362 mh.num_substreams > 1) {
364 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
365 } else if (avctx->request_channel_layout == mh.channel_layout_thd_stream1 ||
366 !mh.channels_thd_stream2) {
367 avctx->channels = mh.channels_thd_stream1;
368 avctx->channel_layout = mh.channel_layout_thd_stream1;
370 avctx->channels = mh.channels_thd_stream2;
371 avctx->channel_layout = mh.channel_layout_thd_stream2;
376 if (!mh.is_vbr) /* Stream is CBR */
377 avctx->bit_rate = mh.peak_bitrate;
379 mp->num_substreams = mh.num_substreams;
383 *poutbuf_size = buf_size;
392 AVCodecParser ff_mlp_parser = {
393 .codec_ids = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
394 .priv_data_size = sizeof(MLPParseContext),
395 .parser_init = mlp_init,
396 .parser_parse = mlp_parse,
397 .parser_close = ff_parse_close,