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] = {
60  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
61 };
62 
63 static const uint8_t thd_chancount[13] = {
64 // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
65  2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
66 };
67 
68 static const uint64_t thd_layout[13] = {
70  AV_CH_FRONT_CENTER, // C
71  AV_CH_LOW_FREQUENCY, // LFE
76  AV_CH_BACK_CENTER, // Cs
77  AV_CH_TOP_CENTER, // Ts
81  AV_CH_LOW_FREQUENCY_2, // LFE2
82 };
83 
84 static int mlp_samplerate(int in)
85 {
86  if (in == 0xF)
87  return 0;
88 
89  return (in & 8 ? 44100 : 48000) << (in & 7) ;
90 }
91 
92 static int truehd_channels(int chanmap)
93 {
94  int channels = 0, i;
95 
96  for (i = 0; i < 13; i++)
97  channels += thd_chancount[i] * ((chanmap >> i) & 1);
98 
99  return channels;
100 }
101 
102 static uint64_t truehd_layout(int chanmap)
103 {
104  int i;
105  uint64_t layout = 0;
106 
107  for (i = 0; i < 13; i++)
108  layout |= thd_layout[i] * ((chanmap >> i) & 1);
109 
110  return layout;
111 }
112 
113 static int mlp_get_major_sync_size(const uint8_t * buf, int bufsize)
114 {
115  int has_extension, extensions = 0;
116  int size = 28;
117  if (bufsize < 28)
118  return -1;
119 
120  if (AV_RB32(buf) == 0xf8726fba) {
121  has_extension = buf[25] & 1;
122  if (has_extension) {
123  extensions = buf[26] >> 4;
124  size += 2 + extensions * 2;
125  }
126  }
127  return size;
128 }
129 
130 /** Read a major sync info header - contains high level information about
131  * the stream - sample rate, channel arrangement etc. Most of this
132  * information is not actually necessary for decoding, only for playback.
133  * gb must be a freshly initialized GetBitContext with no bits read.
134  */
135 
137 {
138  int ratebits, channel_arrangement, header_size;
139  uint16_t checksum;
140 
141  av_assert1(get_bits_count(gb) == 0);
142 
143  header_size = mlp_get_major_sync_size(gb->buffer, gb->size_in_bits >> 3);
144  if (header_size < 0 || gb->size_in_bits < header_size << 3) {
145  av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
146  return -1;
147  }
148 
149  checksum = ff_mlp_checksum16(gb->buffer, header_size - 2);
150  if (checksum != AV_RL16(gb->buffer+header_size-2)) {
151  av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
152  return AVERROR_INVALIDDATA;
153  }
154 
155  if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
156  return AVERROR_INVALIDDATA;
157 
158  mh->stream_type = get_bits(gb, 8);
159  mh->header_size = header_size;
160 
161  if (mh->stream_type == 0xbb) {
162  mh->group1_bits = mlp_quants[get_bits(gb, 4)];
163  mh->group2_bits = mlp_quants[get_bits(gb, 4)];
164 
165  ratebits = get_bits(gb, 4);
166  mh->group1_samplerate = mlp_samplerate(ratebits);
167  mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
168 
169  skip_bits(gb, 11);
170 
171  mh->channel_arrangement=
172  channel_arrangement = get_bits(gb, 5);
173  mh->channels_mlp = mlp_channels[channel_arrangement];
174  mh->channel_layout_mlp = mlp_layout[channel_arrangement];
175  } else if (mh->stream_type == 0xba) {
176  mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
177  mh->group2_bits = 0;
178 
179  ratebits = get_bits(gb, 4);
180  mh->group1_samplerate = mlp_samplerate(ratebits);
181  mh->group2_samplerate = 0;
182 
183  skip_bits(gb, 4);
184 
185  mh->channel_modifier_thd_stream0 = get_bits(gb, 2);
186  mh->channel_modifier_thd_stream1 = get_bits(gb, 2);
187 
188  mh->channel_arrangement=
189  channel_arrangement = get_bits(gb, 5);
190  mh->channels_thd_stream1 = truehd_channels(channel_arrangement);
191  mh->channel_layout_thd_stream1 = truehd_layout(channel_arrangement);
192 
193  mh->channel_modifier_thd_stream2 = get_bits(gb, 2);
194 
195  channel_arrangement = get_bits(gb, 13);
196  mh->channels_thd_stream2 = truehd_channels(channel_arrangement);
197  mh->channel_layout_thd_stream2 = truehd_layout(channel_arrangement);
198  } else
199  return AVERROR_INVALIDDATA;
200 
201  mh->access_unit_size = 40 << (ratebits & 7);
202  mh->access_unit_size_pow2 = 64 << (ratebits & 7);
203 
204  skip_bits_long(gb, 48);
205 
206  mh->is_vbr = get_bits1(gb);
207 
208  mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
209 
210  mh->num_substreams = get_bits(gb, 4);
211 
212  skip_bits_long(gb, 4 + (header_size - 17) * 8);
213 
214  return 0;
215 }
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:97
mlp_layout
static const uint64_t mlp_layout[32]
Definition: mlp_parse.c:38
AV_CH_TOP_FRONT_CENTER
#define AV_CH_TOP_FRONT_CENTER
Definition: channel_layout.h:62
AV_CH_LOW_FREQUENCY_2
#define AV_CH_LOW_FREQUENCY_2
Definition: channel_layout.h:73
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
GetBitContext::size_in_bits
int size_in_bits
Definition: get_bits.h:68
AV_CH_TOP_FRONT_RIGHT
#define AV_CH_TOP_FRONT_RIGHT
Definition: channel_layout.h:63
mh
#define mh
Definition: vf_colormatrix.c:107
AV_CH_TOP_FRONT_LEFT
#define AV_CH_TOP_FRONT_LEFT
Definition: channel_layout.h:61
channels
channels
Definition: aptx.c:30
AV_CH_WIDE_LEFT
#define AV_CH_WIDE_LEFT
Definition: channel_layout.h:69
AV_CH_SURROUND_DIRECT_RIGHT
#define AV_CH_SURROUND_DIRECT_RIGHT
Definition: channel_layout.h:72
AV_CH_WIDE_RIGHT
#define AV_CH_WIDE_RIGHT
Definition: channel_layout.h:70
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
truehd_layout
static uint64_t truehd_layout(int chanmap)
Definition: mlp_parse.c:102
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
ff_mlp_checksum16
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
Definition: mlp.c:85
GetBitContext
Definition: get_bits.h:61
AV_CH_BACK_LEFT
#define AV_CH_BACK_LEFT
Definition: channel_layout.h:53
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:94
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:52
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:90
mlp_samplerate
static int mlp_samplerate(int in)
Definition: mlp_parse.c:84
AV_CH_LAYOUT_2_1
#define AV_CH_LAYOUT_2_1
Definition: channel_layout.h:88
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:62
AV_CH_TOP_CENTER
#define AV_CH_TOP_CENTER
Definition: channel_layout.h:60
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:51
AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
Definition: channel_layout.h:55
size
int size
Definition: twinvq_data.h:11134
AV_RB32
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:92
ff_mlp_read_major_sync
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:136
AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:98
AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
Definition: channel_layout.h:56
layout
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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
Definition: filter_design.txt:18
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
mlp_parse.h
mlp_quants
static const uint8_t mlp_quants[16]
Definition: mlp_parse.c:28
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AV_CH_BACK_CENTER
#define AV_CH_BACK_CENTER
Definition: channel_layout.h:57
MLPHeaderInfo
Definition: mlp_parse.h:26
AV_CH_FRONT_LEFT
#define AV_CH_FRONT_LEFT
Definition: channel_layout.h:49
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:59
thd_layout
static const uint64_t thd_layout[13]
Definition: mlp_parse.c:68
mlp_channels
static const uint8_t mlp_channels[32]
Definition: mlp_parse.c:33
thd_chancount
static const uint8_t thd_chancount[13]
Definition: mlp_parse.c:63
AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_SURROUND
Definition: channel_layout.h:89
checksum
static volatile int checksum
Definition: adler32.c:30
channel_layout.h
mlp_get_major_sync_size
static int mlp_get_major_sync_size(const uint8_t *buf, int bufsize)
Definition: mlp_parse.c:113
AV_CH_SURROUND_DIRECT_LEFT
#define AV_CH_SURROUND_DIRECT_LEFT
Definition: channel_layout.h:71
AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT
Definition: channel_layout.h:50
mlp.h
AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_4POINT0
Definition: channel_layout.h:91
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AV_CH_BACK_RIGHT
#define AV_CH_BACK_RIGHT
Definition: channel_layout.h:54
AV_CH_SIDE_LEFT
#define AV_CH_SIDE_LEFT
Definition: channel_layout.h:58
truehd_channels
static int truehd_channels(int chanmap)
Definition: mlp_parse.c:92