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 "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 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,
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_QUAD|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
85  AV_CH_TOP_CENTER, // Ts
89  AV_CH_LOW_FREQUENCY_2, // 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 uint64_t ff_truehd_layout(int chanmap)
111 {
112  int i;
113  uint64_t layout = 0;
114 
115  for (i = 0; i < 13; i++)
116  layout |= thd_layout[i] * ((chanmap >> i) & 1);
117 
118  return layout;
119 }
120 
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.
125  */
126 
128 {
129  int ratebits, channel_arrangement;
130  uint16_t checksum;
131 
132  av_assert1(get_bits_count(gb) == 0);
133 
134  if (gb->size_in_bits < 28 << 3) {
135  av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
136  return -1;
137  }
138 
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;
143  }
144 
145  if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
146  return AVERROR_INVALIDDATA;
147 
148  mh->stream_type = get_bits(gb, 8);
149 
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)];
153 
154  ratebits = get_bits(gb, 4);
155  mh->group1_samplerate = mlp_samplerate(ratebits);
157 
158  skip_bits(gb, 11);
159 
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?
166  mh->group2_bits = 0;
167 
168  ratebits = get_bits(gb, 4);
169  mh->group1_samplerate = mlp_samplerate(ratebits);
170  mh->group2_samplerate = 0;
171 
172  skip_bits(gb, 8);
173 
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);
178 
179  skip_bits(gb, 2);
180 
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);
184  } else
185  return AVERROR_INVALIDDATA;
186 
187  mh->access_unit_size = 40 << (ratebits & 7);
188  mh->access_unit_size_pow2 = 64 << (ratebits & 7);
189 
190  skip_bits_long(gb, 48);
191 
192  mh->is_vbr = get_bits1(gb);
193 
194  mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
195 
196  mh->num_substreams = get_bits(gb, 4);
197 
198  skip_bits_long(gb, 4 + 11 * 8);
199 
200  return 0;
201 }
202 
203 typedef struct MLPParseContext
204 {
206 
208 
209  int in_sync;
210 
213 
215 {
216  ff_mlp_init_crc();
217  return 0;
218 }
219 
221  AVCodecContext *avctx,
222  const uint8_t **poutbuf, int *poutbuf_size,
223  const uint8_t *buf, int buf_size)
224 {
225  MLPParseContext *mp = s->priv_data;
226  int sync_present;
227  uint8_t parity_bits;
228  int next;
229  int i, p = 0;
230 
231  *poutbuf_size = 0;
232  if (buf_size == 0)
233  return 0;
234 
235  if (!mp->in_sync) {
236  // Not in sync - find a major sync header
237 
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) {
243  mp->in_sync = 1;
244  mp->bytes_left = 0;
245  break;
246  }
247  }
248 
249  if (!mp->in_sync) {
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");
252  return buf_size;
253  }
254 
255  ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
256 
257  return i - 7;
258  }
259 
260  if (mp->bytes_left == 0) {
261  // Find length of this packet
262 
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++];
266  }
267 
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");
271  return buf_size;
272  }
273 
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
278  goto lost_sync;
279  }
280  mp->bytes_left -= mp->pc.index;
281  }
282 
283  next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
284 
285  if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
286  mp->bytes_left -= buf_size;
287  return buf_size;
288  }
289 
290  mp->bytes_left = 0;
291 
292  sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
293 
294  if (!sync_present) {
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.
298 
299  parity_bits = 0;
300  for (i = -1; i < mp->num_substreams; i++) {
301  parity_bits ^= buf[p++];
302  parity_bits ^= buf[p++];
303 
304  if (i < 0 || buf[p-2] & 0x80) {
305  parity_bits ^= buf[p++];
306  parity_bits ^= buf[p++];
307  }
308  }
309 
310  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
311  av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
312  goto lost_sync;
313  }
314  } else {
315  GetBitContext gb;
317 
318  init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
319  if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
320  goto lost_sync;
321 
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;
325  else
326  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
327  avctx->sample_rate = mh.group1_samplerate;
328  s->duration = mh.access_unit_size;
329 
330  if(!avctx->channels || !avctx->channel_layout) {
331  if (mh.stream_type == 0xbb) {
332  /* MLP stream */
333 #if FF_API_REQUEST_CHANNELS
334  if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
335  mh.num_substreams > 1) {
336  avctx->channels = 2;
338  } else
339 #endif
341  mh.num_substreams > 1) {
342  avctx->channels = 2;
344  } else {
345  avctx->channels = mh.channels_mlp;
347  }
348  } else { /* mh.stream_type == 0xba */
349  /* TrueHD stream */
350 #if FF_API_REQUEST_CHANNELS
351  if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
352  mh.num_substreams > 1) {
353  avctx->channels = 2;
355  } else if (avctx->request_channels > 0 &&
356  avctx->request_channels <= mh.channels_thd_stream1) {
357  avctx->channels = mh.channels_thd_stream1;
359  } else
360 #endif
362  mh.num_substreams > 1) {
363  avctx->channels = 2;
365  } else if (avctx->request_channel_layout == mh.channel_layout_thd_stream1 ||
366  !mh.channels_thd_stream2) {
367  avctx->channels = mh.channels_thd_stream1;
369  } else {
370  avctx->channels = mh.channels_thd_stream2;
372  }
373  }
374  }
375 
376  if (!mh.is_vbr) /* Stream is CBR */
377  avctx->bit_rate = mh.peak_bitrate;
378 
380  }
381 
382  *poutbuf = buf;
383  *poutbuf_size = buf_size;
384 
385  return next;
386 
387 lost_sync:
388  mp->in_sync = 0;
389  return 1;
390 }
391 
394  .priv_data_size = sizeof(MLPParseContext),
395  .parser_init = mlp_init,
396  .parser_parse = mlp_parse,
397  .parser_close = ff_parse_close,
398 };
int channels_thd_stream1
Channel count for substream 1 of TrueHD streams ("6-channel presentation")
Definition: mlp_parser.h:44
struct MLPParseContext MLPParseContext
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_CH_TOP_FRONT_RIGHT
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_CH_LAYOUT_SURROUND
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:198
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
#define AV_CH_TOP_FRONT_LEFT
int num_substreams
Number of substreams within stream.
Definition: mlp_parser.h:56
#define AV_CH_TOP_FRONT_CENTER
#define AV_CH_LOW_FREQUENCY_2
const uint8_t * buffer
Definition: get_bits.h:55
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
#define AV_RL16
#define AV_CH_LAYOUT_4POINT0
uint64_t channel_layout_mlp
Channel layout for MLP streams.
Definition: mlp_parser.h:46
int duration
Duration of the current frame.
#define AV_CH_SURROUND_DIRECT_RIGHT
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_CH_LAYOUT_STEREO
signed 16 bits
Definition: samplefmt.h:52
int access_unit_size
Number of samples per coded frame.
Definition: mlp_parser.h:50
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
int peak_bitrate
Peak bitrate for VBR, actual bitrate (==peak) for CBR.
Definition: mlp_parser.h:54
#define AV_CH_WIDE_LEFT
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
static const uint64_t thd_layout[13]
Definition: mlp_parser.c:76
#define AV_RB32
#define AV_CH_WIDE_RIGHT
ParseContext pc
Definition: mlp_parser.c:205
#define AV_CH_LOW_FREQUENCY
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:220
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
uint64_t ff_truehd_layout(int chanmap)
Definition: mlp_parser.c:110
bitstream reader API header.
#define AV_CH_BACK_LEFT
static const uint8_t mlp_channels[32]
Definition: mlp_parser.c:41
int channel_arrangement
Definition: mlp_parser.h:42
AVCodecParser ff_mlp_parser
Definition: mlp_parser.c:392
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:214
static int truehd_channels(int chanmap)
Definition: mlp_parser.c:100
int channels_mlp
Channel count for MLP streams.
Definition: mlp_parser.h:43
#define AV_CH_LAYOUT_QUAD
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:246
uint64_t channel_layout
Audio channel layout.
#define AV_CH_LAYOUT_2_1
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:279
signed 32 bits
Definition: samplefmt.h:53
#define AV_CH_TOP_CENTER
int bit_rate
the average bitrate
audio channel layout utility functions
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
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:57
MLP parser prototypes.
int is_vbr
Stream is VBR instead of CBR.
Definition: mlp_parser.h:53
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_CENTER
#define AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_FRONT_RIGHT_OF_CENTER
int stream_type
0xBB for MLP, 0xBA for TrueHD
Definition: mlp_parser.h:34
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:51
uint8_t * buffer
Definition: parser.h:29
int sample_rate
samples per second
static int mlp_samplerate(int in)
Definition: mlp_parser.c:92
main external API structure.
#define AV_CH_FRONT_LEFT
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
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:273
const uint64_t ff_mlp_layout[32]
Definition: mlp_parser.c:46
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
#define AV_CH_LAYOUT_5POINT0_BACK
#define END_NOT_FOUND
Definition: parser.h:40
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:306
int group2_bits
Bit depth of the second substream (MLP only)
Definition: mlp_parser.h:37
#define AV_CH_BACK_CENTER
#define AV_CH_SIDE_RIGHT
int channels_thd_stream2
Channel count for substream 2 of TrueHD streams ("8-channel presentation")
Definition: mlp_parser.h:45
uint64_t channel_layout_thd_stream1
Channel layout for substream 1 of TrueHD streams ("6-channel presentation")
Definition: mlp_parser.h:47
uint64_t channel_layout_thd_stream2
Channel layout for substream 2 of TrueHD streams ("8-channel presentation")
Definition: mlp_parser.h:48
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:127
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
int channels
number of audio channels
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_SURROUND_DIRECT_LEFT
#define AV_CH_FRONT_RIGHT
#define AV_LOG_INFO
Definition: log.h:156
static const uint8_t thd_chancount[13]
Definition: mlp_parser.c:71
#define AV_CH_SIDE_LEFT
int group1_samplerate
Sample rate of first substream.
Definition: mlp_parser.h:39
#define AV_CH_LAYOUT_MONO
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
int group2_samplerate
Sample rate of second substream (MLP only)
Definition: mlp_parser.h:40
#define mh
#define AV_CH_BACK_RIGHT
static av_cold int mlp_init(AVCodecParserContext *s)
Definition: mlp_parser.c:214