FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 
30 #include "libavutil/crc.h"
31 #include "libavutil/internal.h"
32 #include "get_bits.h"
33 #include "parser.h"
34 #include "mlp_parser.h"
35 #include "mlp.h"
36 
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,
40 };
41 
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,
45 };
46 
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,
58  AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
59  AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
63  AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
64  AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
66  AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
69  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
70 };
71 
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
75 };
76 
77 static const uint64_t thd_layout[13] = {
79  AV_CH_FRONT_CENTER, // C
80  AV_CH_LOW_FREQUENCY, // LFE
85  AV_CH_BACK_CENTER, // Cs
86  AV_CH_TOP_CENTER, // Ts
90  AV_CH_LOW_FREQUENCY_2, // LFE2
91 };
92 
93 static int mlp_samplerate(int in)
94 {
95  if (in == 0xF)
96  return 0;
97 
98  return (in & 8 ? 44100 : 48000) << (in & 7) ;
99 }
100 
101 static int truehd_channels(int chanmap)
102 {
103  int channels = 0, i;
104 
105  for (i = 0; i < 13; i++)
106  channels += thd_chancount[i] * ((chanmap >> i) & 1);
107 
108  return channels;
109 }
110 
111 uint64_t ff_truehd_layout(int chanmap)
112 {
113  int i;
114  uint64_t layout = 0;
115 
116  for (i = 0; i < 13; i++)
117  layout |= thd_layout[i] * ((chanmap >> i) & 1);
118 
119  return layout;
120 }
121 
122 /** Read a major sync info header - contains high level information about
123  * the stream - sample rate, channel arrangement etc. Most of this
124  * information is not actually necessary for decoding, only for playback.
125  * gb must be a freshly initialized GetBitContext with no bits read.
126  */
127 
129 {
130  int ratebits, channel_arrangement;
131  uint16_t checksum;
132 
133  av_assert1(get_bits_count(gb) == 0);
134 
135  if (gb->size_in_bits < 28 << 3) {
136  av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
137  return -1;
138  }
139 
140  checksum = ff_mlp_checksum16(gb->buffer, 26);
141  if (checksum != AV_RL16(gb->buffer+26)) {
142  av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
143  return AVERROR_INVALIDDATA;
144  }
145 
146  if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
147  return AVERROR_INVALIDDATA;
148 
149  mh->stream_type = get_bits(gb, 8);
150 
151  if (mh->stream_type == 0xbb) {
152  mh->group1_bits = mlp_quants[get_bits(gb, 4)];
153  mh->group2_bits = mlp_quants[get_bits(gb, 4)];
154 
155  ratebits = get_bits(gb, 4);
156  mh->group1_samplerate = mlp_samplerate(ratebits);
158 
159  skip_bits(gb, 11);
160 
162  channel_arrangement = get_bits(gb, 5);
163  mh->channels_mlp = mlp_channels[channel_arrangement];
164  mh->channel_layout_mlp = ff_mlp_layout[channel_arrangement];
165  } else if (mh->stream_type == 0xba) {
166  mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
167  mh->group2_bits = 0;
168 
169  ratebits = get_bits(gb, 4);
170  mh->group1_samplerate = mlp_samplerate(ratebits);
171  mh->group2_samplerate = 0;
172 
173  skip_bits(gb, 4);
174 
177 
179  channel_arrangement = get_bits(gb, 5);
180  mh->channels_thd_stream1 = truehd_channels(channel_arrangement);
181  mh->channel_layout_thd_stream1 = ff_truehd_layout(channel_arrangement);
182 
184 
185  channel_arrangement = get_bits(gb, 13);
186  mh->channels_thd_stream2 = truehd_channels(channel_arrangement);
187  mh->channel_layout_thd_stream2 = ff_truehd_layout(channel_arrangement);
188  } else
189  return AVERROR_INVALIDDATA;
190 
191  mh->access_unit_size = 40 << (ratebits & 7);
192  mh->access_unit_size_pow2 = 64 << (ratebits & 7);
193 
194  skip_bits_long(gb, 48);
195 
196  mh->is_vbr = get_bits1(gb);
197 
198  mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
199 
200  mh->num_substreams = get_bits(gb, 4);
201 
202  skip_bits_long(gb, 4 + 11 * 8);
203 
204  return 0;
205 }
206 
207 typedef struct MLPParseContext
208 {
210 
212 
213  int in_sync;
214 
217 
219 {
220  ff_mlp_init_crc();
221  return 0;
222 }
223 
225  AVCodecContext *avctx,
226  const uint8_t **poutbuf, int *poutbuf_size,
227  const uint8_t *buf, int buf_size)
228 {
229  MLPParseContext *mp = s->priv_data;
230  int sync_present;
231  uint8_t parity_bits;
232  int next;
233  int i, p = 0;
234 
235  *poutbuf_size = 0;
236  if (buf_size == 0)
237  return 0;
238 
239  if (!mp->in_sync) {
240  // Not in sync - find a major sync header
241 
242  for (i = 0; i < buf_size; i++) {
243  mp->pc.state = (mp->pc.state << 8) | buf[i];
244  if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
245  // ignore if we do not have the data for the start of header
246  mp->pc.index + i >= 7) {
247  mp->in_sync = 1;
248  mp->bytes_left = 0;
249  break;
250  }
251  }
252 
253  if (!mp->in_sync) {
254  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
255  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
256  return buf_size;
257  }
258 
259  ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
260 
261  return i - 7;
262  }
263 
264  if (mp->bytes_left == 0) {
265  // Find length of this packet
266 
267  /* Copy overread bytes from last frame into buffer. */
268  for(; mp->pc.overread>0; mp->pc.overread--) {
269  mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
270  }
271 
272  if (mp->pc.index + buf_size < 2) {
273  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
274  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
275  return buf_size;
276  }
277 
278  mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
279  | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
280  mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
281  if (mp->bytes_left <= 0) { // prevent infinite loop
282  goto lost_sync;
283  }
284  mp->bytes_left -= mp->pc.index;
285  }
286 
287  next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
288 
289  if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
290  mp->bytes_left -= buf_size;
291  return buf_size;
292  }
293 
294  mp->bytes_left = 0;
295 
296  sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
297 
298  if (!sync_present) {
299  /* The first nibble of a frame is a parity check of the 4-byte
300  * access unit header and all the 2- or 4-byte substream headers. */
301  // Only check when this isn't a sync frame - syncs have a checksum.
302 
303  parity_bits = 0;
304  for (i = -1; i < mp->num_substreams; i++) {
305  parity_bits ^= buf[p++];
306  parity_bits ^= buf[p++];
307 
308  if (i < 0 || buf[p-2] & 0x80) {
309  parity_bits ^= buf[p++];
310  parity_bits ^= buf[p++];
311  }
312  }
313 
314  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
315  av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
316  goto lost_sync;
317  }
318  } else {
319  GetBitContext gb;
321 
322  init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
323  if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
324  goto lost_sync;
325 
326  avctx->bits_per_raw_sample = mh.group1_bits;
327  if (avctx->bits_per_raw_sample > 16)
328  avctx->sample_fmt = AV_SAMPLE_FMT_S32;
329  else
330  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
331  avctx->sample_rate = mh.group1_samplerate;
332  s->duration = mh.access_unit_size;
333 
334  if(!avctx->channels || !avctx->channel_layout) {
335  if (mh.stream_type == 0xbb) {
336  /* MLP stream */
337 #if FF_API_REQUEST_CHANNELS
339  if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
340  mh.num_substreams > 1) {
341  avctx->channels = 2;
344  } else
345 #endif
346  if (avctx->request_channel_layout &&
348  avctx->request_channel_layout &&
349  mh.num_substreams > 1) {
350  avctx->channels = 2;
352  } else {
353  avctx->channels = mh.channels_mlp;
355  }
356  } else { /* mh.stream_type == 0xba */
357  /* TrueHD stream */
358 #if FF_API_REQUEST_CHANNELS
360  if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
361  mh.num_substreams > 1) {
362  avctx->channels = 2;
364  } else if (avctx->request_channels > 0 &&
365  avctx->request_channels <= mh.channels_thd_stream1) {
366  avctx->channels = mh.channels_thd_stream1;
369  } else
370 #endif
371  if (avctx->request_channel_layout &&
373  avctx->request_channel_layout &&
374  mh.num_substreams > 1) {
375  avctx->channels = 2;
377  } else if (!mh.channels_thd_stream2 ||
378  (avctx->request_channel_layout &&
380  avctx->request_channel_layout)) {
381  avctx->channels = mh.channels_thd_stream1;
383  } else {
384  avctx->channels = mh.channels_thd_stream2;
386  }
387  }
388  }
389 
390  if (!mh.is_vbr) /* Stream is CBR */
391  avctx->bit_rate = mh.peak_bitrate;
392 
394  }
395 
396  *poutbuf = buf;
397  *poutbuf_size = buf_size;
398 
399  return next;
400 
401 lost_sync:
402  mp->in_sync = 0;
403  return 1;
404 }
405 
408  .priv_data_size = sizeof(MLPParseContext),
409  .parser_init = mlp_init,
410  .parser_parse = mlp_parse,
411  .parser_close = ff_parse_close,
412 };