00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdint.h>
00028
00029 #include "libavutil/crc.h"
00030 #include "libavutil/audioconvert.h"
00031 #include "get_bits.h"
00032 #include "parser.h"
00033 #include "mlp_parser.h"
00034 #include "mlp.h"
00035
00036 static const uint8_t mlp_quants[16] = {
00037 16, 20, 24, 0, 0, 0, 0, 0,
00038 0, 0, 0, 0, 0, 0, 0, 0,
00039 };
00040
00041 static const uint8_t mlp_channels[32] = {
00042 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
00043 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00044 };
00045
00046 const uint64_t ff_mlp_layout[32] = {
00047 AV_CH_LAYOUT_MONO,
00048 AV_CH_LAYOUT_STEREO,
00049 AV_CH_LAYOUT_2_1,
00050 AV_CH_LAYOUT_QUAD,
00051 AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
00052 AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
00053 AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
00054 AV_CH_LAYOUT_SURROUND,
00055 AV_CH_LAYOUT_4POINT0,
00056 AV_CH_LAYOUT_5POINT0_BACK,
00057 AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
00058 AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
00059 AV_CH_LAYOUT_5POINT1_BACK,
00060 AV_CH_LAYOUT_4POINT0,
00061 AV_CH_LAYOUT_5POINT0_BACK,
00062 AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
00063 AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
00064 AV_CH_LAYOUT_5POINT1_BACK,
00065 AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
00066 AV_CH_LAYOUT_5POINT0_BACK,
00067 AV_CH_LAYOUT_5POINT1_BACK,
00068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00069 };
00070
00071 static const uint8_t thd_chancount[13] = {
00072
00073 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
00074 };
00075
00076 static const uint64_t thd_layout[13] = {
00077 AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT,
00078 AV_CH_FRONT_CENTER,
00079 AV_CH_LOW_FREQUENCY,
00080 AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,
00081 AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT,
00082 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER,
00083 AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT,
00084 AV_CH_BACK_CENTER,
00085 AV_CH_TOP_CENTER,
00086 AV_CH_SURROUND_DIRECT_LEFT|AV_CH_SURROUND_DIRECT_RIGHT,
00087 AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT,
00088 AV_CH_TOP_FRONT_CENTER,
00089 AV_CH_LOW_FREQUENCY_2,
00090 };
00091
00092 static int mlp_samplerate(int in)
00093 {
00094 if (in == 0xF)
00095 return 0;
00096
00097 return (in & 8 ? 44100 : 48000) << (in & 7) ;
00098 }
00099
00100 static int truehd_channels(int chanmap)
00101 {
00102 int channels = 0, i;
00103
00104 for (i = 0; i < 13; i++)
00105 channels += thd_chancount[i] * ((chanmap >> i) & 1);
00106
00107 return channels;
00108 }
00109
00110 uint64_t ff_truehd_layout(int chanmap)
00111 {
00112 int i;
00113 uint64_t layout = 0;
00114
00115 for (i = 0; i < 13; i++)
00116 layout |= thd_layout[i] * ((chanmap >> i) & 1);
00117
00118 return layout;
00119 }
00120
00127 int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
00128 {
00129 int ratebits;
00130 uint16_t checksum;
00131
00132 av_assert1(get_bits_count(gb) == 0);
00133
00134 if (gb->size_in_bits < 28 << 3) {
00135 av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
00136 return -1;
00137 }
00138
00139 checksum = ff_mlp_checksum16(gb->buffer, 26);
00140 if (checksum != AV_RL16(gb->buffer+26)) {
00141 av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
00142 return AVERROR_INVALIDDATA;
00143 }
00144
00145 if (get_bits_long(gb, 24) != 0xf8726f)
00146 return AVERROR_INVALIDDATA;
00147
00148 mh->stream_type = get_bits(gb, 8);
00149
00150 if (mh->stream_type == 0xbb) {
00151 mh->group1_bits = mlp_quants[get_bits(gb, 4)];
00152 mh->group2_bits = mlp_quants[get_bits(gb, 4)];
00153
00154 ratebits = get_bits(gb, 4);
00155 mh->group1_samplerate = mlp_samplerate(ratebits);
00156 mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
00157
00158 skip_bits(gb, 11);
00159
00160 mh->channels_mlp = get_bits(gb, 5);
00161 } else if (mh->stream_type == 0xba) {
00162 mh->group1_bits = 24;
00163 mh->group2_bits = 0;
00164
00165 ratebits = get_bits(gb, 4);
00166 mh->group1_samplerate = mlp_samplerate(ratebits);
00167 mh->group2_samplerate = 0;
00168
00169 skip_bits(gb, 8);
00170
00171 mh->channels_thd_stream1 = get_bits(gb, 5);
00172
00173 skip_bits(gb, 2);
00174
00175 mh->channels_thd_stream2 = get_bits(gb, 13);
00176 } else
00177 return AVERROR_INVALIDDATA;
00178
00179 mh->access_unit_size = 40 << (ratebits & 7);
00180 mh->access_unit_size_pow2 = 64 << (ratebits & 7);
00181
00182 skip_bits_long(gb, 48);
00183
00184 mh->is_vbr = get_bits1(gb);
00185
00186 mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
00187
00188 mh->num_substreams = get_bits(gb, 4);
00189
00190 skip_bits_long(gb, 4 + 11 * 8);
00191
00192 return 0;
00193 }
00194
00195 typedef struct MLPParseContext
00196 {
00197 ParseContext pc;
00198
00199 int bytes_left;
00200
00201 int in_sync;
00202
00203 int num_substreams;
00204 } MLPParseContext;
00205
00206 static av_cold int mlp_init(AVCodecParserContext *s)
00207 {
00208 ff_mlp_init_crc();
00209 return 0;
00210 }
00211
00212 static int mlp_parse(AVCodecParserContext *s,
00213 AVCodecContext *avctx,
00214 const uint8_t **poutbuf, int *poutbuf_size,
00215 const uint8_t *buf, int buf_size)
00216 {
00217 MLPParseContext *mp = s->priv_data;
00218 int sync_present;
00219 uint8_t parity_bits;
00220 int next;
00221 int i, p = 0;
00222
00223 *poutbuf_size = 0;
00224 if (buf_size == 0)
00225 return 0;
00226
00227 if (!mp->in_sync) {
00228
00229
00230 for (i = 0; i < buf_size; i++) {
00231 mp->pc.state = (mp->pc.state << 8) | buf[i];
00232 if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
00233
00234 mp->pc.index + i >= 7) {
00235 mp->in_sync = 1;
00236 mp->bytes_left = 0;
00237 break;
00238 }
00239 }
00240
00241 if (!mp->in_sync) {
00242 ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
00243 return buf_size;
00244 }
00245
00246 ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
00247
00248 return i - 7;
00249 }
00250
00251 if (mp->bytes_left == 0) {
00252
00253
00254
00255 for(; mp->pc.overread>0; mp->pc.overread--) {
00256 mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
00257 }
00258
00259 if (mp->pc.index + buf_size < 2) {
00260 ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
00261 return buf_size;
00262 }
00263
00264 mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
00265 | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
00266 mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
00267 if (mp->bytes_left <= 0) {
00268 goto lost_sync;
00269 }
00270 mp->bytes_left -= mp->pc.index;
00271 }
00272
00273 next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
00274
00275 if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
00276 mp->bytes_left -= buf_size;
00277 return buf_size;
00278 }
00279
00280 mp->bytes_left = 0;
00281
00282 sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
00283
00284 if (!sync_present) {
00285
00286
00287
00288
00289 parity_bits = 0;
00290 for (i = -1; i < mp->num_substreams; i++) {
00291 parity_bits ^= buf[p++];
00292 parity_bits ^= buf[p++];
00293
00294 if (i < 0 || buf[p-2] & 0x80) {
00295 parity_bits ^= buf[p++];
00296 parity_bits ^= buf[p++];
00297 }
00298 }
00299
00300 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
00301 av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
00302 goto lost_sync;
00303 }
00304 } else {
00305 GetBitContext gb;
00306 MLPHeaderInfo mh;
00307
00308 init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
00309 if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
00310 goto lost_sync;
00311
00312 avctx->bits_per_raw_sample = mh.group1_bits;
00313 if (avctx->bits_per_raw_sample > 16)
00314 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00315 else
00316 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00317 avctx->sample_rate = mh.group1_samplerate;
00318 s->duration = mh.access_unit_size;
00319
00320 if(!avctx->channels || !avctx->channel_layout) {
00321 if (mh.stream_type == 0xbb) {
00322
00323 avctx->channels = mlp_channels[mh.channels_mlp];
00324 avctx->channel_layout = ff_mlp_layout[mh.channels_mlp];
00325 } else {
00326
00327 if (mh.channels_thd_stream2) {
00328 avctx->channels = truehd_channels(mh.channels_thd_stream2);
00329 avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream2);
00330 } else {
00331 avctx->channels = truehd_channels(mh.channels_thd_stream1);
00332 avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream1);
00333 }
00334 }
00335 }
00336
00337 if (!mh.is_vbr)
00338 avctx->bit_rate = mh.peak_bitrate;
00339
00340 mp->num_substreams = mh.num_substreams;
00341 }
00342
00343 *poutbuf = buf;
00344 *poutbuf_size = buf_size;
00345
00346 return next;
00347
00348 lost_sync:
00349 mp->in_sync = 0;
00350 return 1;
00351 }
00352
00353 AVCodecParser ff_mlp_parser = {
00354 .codec_ids = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
00355 .priv_data_size = sizeof(MLPParseContext),
00356 .parser_init = mlp_init,
00357 .parser_parse = mlp_parse,
00358 .parser_close = ff_parse_close,
00359 };