mlp_parser.c
Go to the documentation of this file.
1 /*
2  * MLP parser
3  * Copyright (c) 2007 Ian Caulfield
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include <stdint.h>
28 
29 #include "libavutil/crc.h"
30 #include "libavutil/audioconvert.h"
31 #include "get_bits.h"
32 #include "parser.h"
33 #include "mlp_parser.h"
34 #include "mlp.h"
35 
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,
39 };
40 
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,
44 };
45 
46 static const uint64_t 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_2_2|AV_CH_LOW_FREQUENCY,
57  AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
58  AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
62  AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
63  AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
65  AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
68  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
69 };
70 
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
74 };
75 
76 static const uint64_t thd_layout[13] = {
78  AV_CH_FRONT_CENTER, // C
79  AV_CH_LOW_FREQUENCY, // LFE
84  AV_CH_BACK_CENTER, // Cs
88  AV_CH_TOP_BACK_CENTER, // Cvh
89  AV_CH_LOW_FREQUENCY // LFE2
90 };
91 
92 static int mlp_samplerate(int in)
93 {
94  if (in == 0xF)
95  return 0;
96 
97  return (in & 8 ? 44100 : 48000) << (in & 7) ;
98 }
99 
100 static int truehd_channels(int chanmap)
101 {
102  int channels = 0, i;
103 
104  for (i = 0; i < 13; i++)
105  channels += thd_chancount[i] * ((chanmap >> i) & 1);
106 
107  return channels;
108 }
109 
110 static uint64_t truehd_layout(int chanmap)
111 {
112  int layout = 0, i;
113 
114  for (i = 0; i < 13; i++)
115  layout |= thd_layout[i] * ((chanmap >> i) & 1);
116 
117  return layout;
118 }
119 
127 {
128  int ratebits;
129  uint16_t checksum;
130 
131  assert(get_bits_count(gb) == 0);
132 
133  if (gb->size_in_bits < 28 << 3) {
134  av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
135  return -1;
136  }
137 
138  checksum = ff_mlp_checksum16(gb->buffer, 26);
139  if (checksum != AV_RL16(gb->buffer+26)) {
140  av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
141  return AVERROR_INVALIDDATA;
142  }
143 
144  if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
145  return AVERROR_INVALIDDATA;
146 
147  mh->stream_type = get_bits(gb, 8);
148 
149  if (mh->stream_type == 0xbb) {
150  mh->group1_bits = mlp_quants[get_bits(gb, 4)];
151  mh->group2_bits = mlp_quants[get_bits(gb, 4)];
152 
153  ratebits = get_bits(gb, 4);
154  mh->group1_samplerate = mlp_samplerate(ratebits);
156 
157  skip_bits(gb, 11);
158 
159  mh->channels_mlp = get_bits(gb, 5);
160  } else if (mh->stream_type == 0xba) {
161  mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
162  mh->group2_bits = 0;
163 
164  ratebits = get_bits(gb, 4);
165  mh->group1_samplerate = mlp_samplerate(ratebits);
166  mh->group2_samplerate = 0;
167 
168  skip_bits(gb, 8);
169 
170  mh->channels_thd_stream1 = get_bits(gb, 5);
171 
172  skip_bits(gb, 2);
173 
174  mh->channels_thd_stream2 = get_bits(gb, 13);
175  } else
176  return AVERROR_INVALIDDATA;
177 
178  mh->access_unit_size = 40 << (ratebits & 7);
179  mh->access_unit_size_pow2 = 64 << (ratebits & 7);
180 
181  skip_bits_long(gb, 48);
182 
183  mh->is_vbr = get_bits1(gb);
184 
185  mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
186 
187  mh->num_substreams = get_bits(gb, 4);
188 
189  skip_bits_long(gb, 4 + 11 * 8);
190 
191  return 0;
192 }
193 
194 typedef struct MLPParseContext
195 {
197 
199 
200  int in_sync;
201 
204 
206 {
207  ff_mlp_init_crc();
208  return 0;
209 }
210 
212  AVCodecContext *avctx,
213  const uint8_t **poutbuf, int *poutbuf_size,
214  const uint8_t *buf, int buf_size)
215 {
216  MLPParseContext *mp = s->priv_data;
217  int sync_present;
218  uint8_t parity_bits;
219  int next;
220  int i, p = 0;
221 
222  *poutbuf_size = 0;
223  if (buf_size == 0)
224  return 0;
225 
226  if (!mp->in_sync) {
227  // Not in sync - find a major sync header
228 
229  for (i = 0; i < buf_size; i++) {
230  mp->pc.state = (mp->pc.state << 8) | buf[i];
231  if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
232  // ignore if we do not have the data for the start of header
233  mp->pc.index + i >= 7) {
234  mp->in_sync = 1;
235  mp->bytes_left = 0;
236  break;
237  }
238  }
239 
240  if (!mp->in_sync) {
241  ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
242  return buf_size;
243  }
244 
245  ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
246 
247  return i - 7;
248  }
249 
250  if (mp->bytes_left == 0) {
251  // Find length of this packet
252 
253  /* Copy overread bytes from last frame into buffer. */
254  for(; mp->pc.overread>0; mp->pc.overread--) {
255  mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
256  }
257 
258  if (mp->pc.index + buf_size < 2) {
259  ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
260  return buf_size;
261  }
262 
263  mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
264  | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
265  mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
266  mp->bytes_left -= mp->pc.index;
267  }
268 
269  next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
270 
271  if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
272  mp->bytes_left -= buf_size;
273  return buf_size;
274  }
275 
276  mp->bytes_left = 0;
277 
278  sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
279 
280  if (!sync_present) {
281  /* The first nibble of a frame is a parity check of the 4-byte
282  * access unit header and all the 2- or 4-byte substream headers. */
283  // Only check when this isn't a sync frame - syncs have a checksum.
284 
285  parity_bits = 0;
286  for (i = -1; i < mp->num_substreams; i++) {
287  parity_bits ^= buf[p++];
288  parity_bits ^= buf[p++];
289 
290  if (i < 0 || buf[p-2] & 0x80) {
291  parity_bits ^= buf[p++];
292  parity_bits ^= buf[p++];
293  }
294  }
295 
296  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
297  av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
298  goto lost_sync;
299  }
300  } else {
301  GetBitContext gb;
302  MLPHeaderInfo mh;
303 
304  init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
305  if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
306  goto lost_sync;
307 
308  avctx->bits_per_raw_sample = mh.group1_bits;
309  if (avctx->bits_per_raw_sample > 16)
310  avctx->sample_fmt = AV_SAMPLE_FMT_S32;
311  else
312  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
313  avctx->sample_rate = mh.group1_samplerate;
314  avctx->frame_size = mh.access_unit_size;
315 
316  if (mh.stream_type == 0xbb) {
317  /* MLP stream */
318  avctx->channels = mlp_channels[mh.channels_mlp];
320  } else { /* mh.stream_type == 0xba */
321  /* TrueHD stream */
322  if (mh.channels_thd_stream2) {
325  } else {
328  }
329  }
330 
331  if (!mh.is_vbr) /* Stream is CBR */
332  avctx->bit_rate = mh.peak_bitrate;
333 
335  }
336 
337  *poutbuf = buf;
338  *poutbuf_size = buf_size;
339 
340  return next;
341 
342 lost_sync:
343  mp->in_sync = 0;
344  return 1;
345 }
346 
349  .priv_data_size = sizeof(MLPParseContext),
350  .parser_init = mlp_init,
351  .parser_parse = mlp_parse,
352  .parser_close = ff_parse_close,
353 };
int channels_thd_stream1
Channel arrangement for substream 1 of TrueHD streams (5.1)
Definition: mlp_parser.h:43
struct MLPParseContext MLPParseContext
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define AV_CH_TOP_FRONT_RIGHT
Definition: audioconvert.h:55
AV_WL32 AV_WL24 AV_WL16 AV_RB32
Definition: bytestream.h:89
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
#define AV_CH_LAYOUT_SURROUND
Definition: audioconvert.h:80
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
Definition: mlp.c:64
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:197
#define AV_CH_TOP_FRONT_LEFT
Definition: audioconvert.h:53
int num_substreams
Number of substreams within stream.
Definition: mlp_parser.h:52
int codec_ids[5]
Definition: avcodec.h:4542
const uint8_t * buffer
Definition: get_bits.h:53
#define AV_CH_LAYOUT_4POINT0
Definition: audioconvert.h:82
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2863
#define AV_CH_LAYOUT_STEREO
Definition: audioconvert.h:77
static uint64_t truehd_layout(int chanmap)
Definition: mlp_parser.c:110
signed 16 bits
Definition: samplefmt.h:30
#define AV_CH_LAYOUT_5POINT0
Definition: audioconvert.h:86
int access_unit_size
Number of samples per coded frame.
Definition: mlp_parser.h:46
int peak_bitrate
Peak bitrate for VBR, actual bitrate (==peak) for CBR.
Definition: mlp_parser.h:50
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1464
#define av_cold
Definition: attributes.h:71
static const uint64_t thd_layout[13]
Definition: mlp_parser.c:76
#define AV_CH_TOP_BACK_CENTER
Definition: audioconvert.h:57
ParseContext pc
Definition: mlp_parser.c:196
#define AV_CH_LOW_FREQUENCY
Definition: audioconvert.h:44
static int mlp_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: mlp_parser.c:211
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
bitstream reader API header.
#define AV_CH_BACK_LEFT
Definition: audioconvert.h:45
static const uint8_t mlp_channels[32]
Definition: mlp_parser.c:41
AVCodecParser ff_mlp_parser
Definition: mlp_parser.c:347
audio conversion routines
#define AV_CH_LAYOUT_5POINT1
Definition: audioconvert.h:87
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:222
static int truehd_channels(int chanmap)
Definition: mlp_parser.c:100
int channels_mlp
Channel arrangement for MLP streams.
Definition: mlp_parser.h:42
int overread_index
the index into ParseContext.buffer of the overread bytes
Definition: parser.h:36
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2870
#define AV_CH_LAYOUT_2_1
Definition: audioconvert.h:79
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:287
#define AV_CH_LAYOUT_2_2
Definition: audioconvert.h:84
signed 32 bits
Definition: samplefmt.h:31
int bit_rate
the average bitrate
Definition: avcodec.h:1340
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
int size_in_bits
Definition: get_bits.h:55
MLP parser prototypes.
int is_vbr
Stream is VBR instead of CBR.
Definition: mlp_parser.h:49
#define AV_CH_FRONT_LEFT_OF_CENTER
Definition: audioconvert.h:47
#define AV_CH_FRONT_CENTER
Definition: audioconvert.h:43
#define AV_CH_FRONT_RIGHT_OF_CENTER
Definition: audioconvert.h:48
int stream_type
0xBB for MLP, 0xBA for TrueHD
Definition: mlp_parser.h:34
int frame_size
Samples per packet, initialized when calling 'init'.
Definition: avcodec.h:1470
static const uint8_t mlp_quants[16]
Definition: mlp_parser.c:36
int access_unit_size_pow2
Next power of two above number of samples per frame.
Definition: mlp_parser.h:47
uint8_t * buffer
Definition: parser.h:29
int sample_rate
samples per second
Definition: avcodec.h:1456
static int mlp_samplerate(int in)
Definition: mlp_parser.c:92
main external API structure.
Definition: avcodec.h:1329
#define AV_CH_FRONT_LEFT
Definition: audioconvert.h:41
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
uint64_t layout
Definition: audioconvert.c:63
#define END_NOT_FOUND
Definition: parser.h:55
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:301
int group2_bits
Bit depth of the second substream (MLP only)
Definition: mlp_parser.h:37
#define AV_CH_BACK_CENTER
Definition: audioconvert.h:49
#define AV_CH_SIDE_RIGHT
Definition: audioconvert.h:51
int channels_thd_stream2
Channel arrangement for substream 2 of TrueHD streams (7.1)
Definition: mlp_parser.h:44
AV_WL32 AV_WL24 AV_RL16
Definition: bytestream.h:89
int index
Definition: parser.h:30
int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
Read a major sync info header - contains high level information about the stream - sample rate...
Definition: mlp_parser.c:126
int channels
number of audio channels
Definition: avcodec.h:1457
int group1_bits
The bit depth of the first substream.
Definition: mlp_parser.h:36
av_cold void ff_mlp_init_crc(void)
Definition: mlp.c:54
#define AV_CH_FRONT_RIGHT
Definition: audioconvert.h:42
#define AV_LOG_INFO
Definition: log.h:119
static const uint8_t thd_chancount[13]
Definition: mlp_parser.c:71
#define AV_CH_SIDE_LEFT
Definition: audioconvert.h:50
int group1_samplerate
Sample rate of first substream.
Definition: mlp_parser.h:39
#define AV_CH_LAYOUT_MONO
Definition: audioconvert.h:76
int group2_samplerate
Sample rate of second substream (MLP only)
Definition: mlp_parser.h:40
#define AV_CH_BACK_RIGHT
Definition: audioconvert.h:46
static av_cold int mlp_init(AVCodecParserContext *s)
Definition: mlp_parser.c:205
static const uint64_t mlp_layout[32]
Definition: mlp_parser.c:46