FFmpeg
mlp_parse.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Ian Caulfield
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdint.h>
22 
24 #include "get_bits.h"
25 #include "mlp_parse.h"
26 #include "mlp.h"
27 
28 static const uint8_t mlp_quants[16] = {
29  16, 20, 24, 0, 0, 0, 0, 0,
30  0, 0, 0, 0, 0, 0, 0, 0,
31 };
32 
33 static const uint8_t mlp_channels[32] = {
34  1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
35  5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 };
37 
38 static const uint64_t mlp_layout[32] = {
43  AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
44  AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
45  AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
49  AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
50  AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
54  AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
55  AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
57  AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
60  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
61 };
62 
63 static int mlp_get_major_sync_size(const uint8_t * buf, int bufsize)
64 {
65  int has_extension, extensions = 0;
66  int size = 28;
67  if (bufsize < 28)
68  return -1;
69 
70  if (AV_RB32(buf) == 0xf8726fba) {
71  has_extension = buf[25] & 1;
72  if (has_extension) {
73  extensions = buf[26] >> 4;
74  size += 2 + extensions * 2;
75  }
76  }
77  return size;
78 }
79 
80 /** Read a major sync info header - contains high level information about
81  * the stream - sample rate, channel arrangement etc. Most of this
82  * information is not actually necessary for decoding, only for playback.
83  * gb must be a freshly initialized GetBitContext with no bits read.
84  */
85 
87 {
88  int ratebits, channel_arrangement, header_size;
89  uint16_t checksum;
90 
91  av_assert1(get_bits_count(gb) == 0);
92 
93  header_size = mlp_get_major_sync_size(gb->buffer, gb->size_in_bits >> 3);
94  if (header_size < 0 || gb->size_in_bits < header_size << 3) {
95  av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
96  return -1;
97  }
98 
99  checksum = ff_mlp_checksum16(gb->buffer, header_size - 2);
100  if (checksum != AV_RL16(gb->buffer+header_size-2)) {
101  av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
102  return AVERROR_INVALIDDATA;
103  }
104 
105  if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
106  return AVERROR_INVALIDDATA;
107 
108  mh->stream_type = get_bits(gb, 8);
109  mh->header_size = header_size;
110 
111  if (mh->stream_type == 0xbb) {
112  mh->group1_bits = mlp_quants[get_bits(gb, 4)];
113  mh->group2_bits = mlp_quants[get_bits(gb, 4)];
114 
115  ratebits = get_bits(gb, 4);
116  mh->group1_samplerate = mlp_samplerate(ratebits);
118 
119  skip_bits(gb, 11);
120 
122  channel_arrangement = get_bits(gb, 5);
123  mh->channels_mlp = mlp_channels[channel_arrangement];
124  mh->channel_layout_mlp = mlp_layout[channel_arrangement];
125  } else if (mh->stream_type == 0xba) {
126  mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
127  mh->group2_bits = 0;
128 
129  ratebits = get_bits(gb, 4);
130  mh->group1_samplerate = mlp_samplerate(ratebits);
131  mh->group2_samplerate = 0;
132 
133  skip_bits(gb, 4);
134 
137 
139  channel_arrangement = get_bits(gb, 5);
140  mh->channels_thd_stream1 = truehd_channels(channel_arrangement);
141  mh->channel_layout_thd_stream1 = truehd_layout(channel_arrangement);
142 
144 
145  channel_arrangement = get_bits(gb, 13);
146  mh->channels_thd_stream2 = truehd_channels(channel_arrangement);
147  mh->channel_layout_thd_stream2 = truehd_layout(channel_arrangement);
148  } else
149  return AVERROR_INVALIDDATA;
150 
151  mh->access_unit_size = 40 << (ratebits & 7);
152  mh->access_unit_size_pow2 = 64 << (ratebits & 7);
153 
154  skip_bits_long(gb, 48);
155 
156  mh->is_vbr = get_bits1(gb);
157 
158  mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
159 
160  mh->num_substreams = get_bits(gb, 4);
161 
162  skip_bits_long(gb, 4 + (header_size - 17) * 8);
163 
164  return 0;
165 }
int channels_thd_stream1
Channel count for substream 1 of TrueHD streams ("6-channel presentation")
Definition: mlp_parse.h:44
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_CH_LAYOUT_SURROUND
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
Definition: mlp.c:85
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
int num_substreams
Number of substreams within stream.
Definition: mlp_parse.h:56
const uint8_t * buffer
Definition: get_bits.h:62
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_parse.c:86
#define AV_CH_LAYOUT_4POINT0
uint64_t channel_layout_mlp
Channel layout for MLP streams.
Definition: mlp_parse.h:46
#define AV_CH_LAYOUT_STEREO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int access_unit_size
Number of samples per coded frame.
Definition: mlp_parse.h:50
int peak_bitrate
Peak bitrate for VBR, actual bitrate (==peak) for CBR.
Definition: mlp_parse.h:54
uint8_t
int channel_modifier_thd_stream0
Channel modifier for substream 0 of TrueHD streams ("2-channel presentation")
Definition: mlp_parse.h:39
#define AV_CH_LOW_FREQUENCY
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
bitstream reader API header.
int channel_arrangement
Definition: mlp_parse.h:37
ptrdiff_t size
Definition: opengl_enc.c:100
#define av_log(a,...)
static int mlp_samplerate(int in)
Definition: mlp_parse.h:80
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int channel_modifier_thd_stream2
Channel modifier for substream 2 of TrueHD streams ("8-channel presentation")
Definition: mlp_parse.h:41
int channels_mlp
Channel count for MLP streams.
Definition: mlp_parse.h:43
#define AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_2_1
audio channel layout utility functions
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int size_in_bits
Definition: get_bits.h:68
int is_vbr
Stream is VBR instead of CBR.
Definition: mlp_parse.h:53
static uint64_t truehd_layout(int chanmap)
Definition: mlp_parse.h:98
static volatile int checksum
Definition: adler32.c:30
#define AV_CH_LAYOUT_5POINT1_BACK
int stream_type
0xBB for MLP, 0xBA for TrueHD
Definition: mlp_parse.h:28
int access_unit_size_pow2
Next power of two above number of samples per frame.
Definition: mlp_parse.h:51
static const uint8_t mlp_quants[16]
Definition: mlp_parse.c:28
int channel_modifier_thd_stream1
Channel modifier for substream 1 of TrueHD streams ("6-channel presentation")
Definition: mlp_parse.h:40
static int mlp_get_major_sync_size(const uint8_t *buf, int bufsize)
Definition: mlp_parse.c:63
void * buf
Definition: avisynth_c.h:766
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
#define AV_CH_LAYOUT_5POINT0_BACK
static const uint8_t mlp_channels[32]
Definition: mlp_parse.c:33
static int truehd_channels(int chanmap)
Definition: mlp_parse.h:88
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
int group2_bits
Bit depth of the second substream (MLP only)
Definition: mlp_parse.h:32
int channels_thd_stream2
Channel count for substream 2 of TrueHD streams ("8-channel presentation")
Definition: mlp_parse.h:45
uint64_t channel_layout_thd_stream1
Channel layout for substream 1 of TrueHD streams ("6-channel presentation")
Definition: mlp_parse.h:47
uint64_t channel_layout_thd_stream2
Channel layout for substream 2 of TrueHD streams ("8-channel presentation")
Definition: mlp_parse.h:48
int header_size
Size of the major sync header, in bytes.
Definition: mlp_parse.h:29
int group1_bits
The bit depth of the first substream.
Definition: mlp_parse.h:31
int group1_samplerate
Sample rate of first substream.
Definition: mlp_parse.h:34
static const uint64_t mlp_layout[32]
Definition: mlp_parse.c:38
#define AV_CH_LAYOUT_MONO
int group2_samplerate
Sample rate of second substream (MLP only)
Definition: mlp_parse.h:35
#define mh