00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "bitstream.h"
00028 #include "parser.h"
00029 #include "crc.h"
00030 #include "mlp_parser.h"
00031
00032 static const uint8_t mlp_quants[16] = {
00033 16, 20, 24, 0, 0, 0, 0, 0,
00034 0, 0, 0, 0, 0, 0, 0, 0,
00035 };
00036
00037 static const uint8_t mlp_channels[32] = {
00038 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
00039 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00040 };
00041
00042 static const uint8_t thd_chancount[13] = {
00043
00044 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
00045 };
00046
00047 static int mlp_samplerate(int in)
00048 {
00049 if (in == 0xF)
00050 return 0;
00051
00052 return (in & 8 ? 44100 : 48000) << (in & 7) ;
00053 }
00054
00055 static int truehd_channels(int chanmap)
00056 {
00057 int channels = 0, i;
00058
00059 for (i = 0; i < 13; i++)
00060 channels += thd_chancount[i] * ((chanmap >> i) & 1);
00061
00062 return channels;
00063 }
00064
00065 static int crc_init = 0;
00066 static AVCRC crc_2D[1024];
00067
00074 static uint16_t mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
00075 {
00076 uint16_t crc;
00077
00078 if (!crc_init) {
00079 av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
00080 crc_init = 1;
00081 }
00082
00083 crc = av_crc(crc_2D, 0, buf, buf_size - 2);
00084 crc ^= AV_RL16(buf + buf_size - 2);
00085 return crc;
00086 }
00087
00093 int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, const uint8_t *buf,
00094 unsigned int buf_size)
00095 {
00096 GetBitContext gb;
00097 int ratebits;
00098 uint16_t checksum;
00099
00100 if (buf_size < 28) {
00101 av_log(log, AV_LOG_ERROR, "Packet too short, unable to read major sync\n");
00102 return -1;
00103 }
00104
00105 checksum = mlp_checksum16(buf, 26);
00106 if (checksum != AV_RL16(buf+26)) {
00107 av_log(log, AV_LOG_ERROR, "Major sync info header checksum error\n");
00108 return -1;
00109 }
00110
00111 init_get_bits(&gb, buf, buf_size * 8);
00112
00113 if (get_bits_long(&gb, 24) != 0xf8726f)
00114 return -1;
00115
00116 mh->stream_type = get_bits(&gb, 8);
00117
00118 if (mh->stream_type == 0xbb) {
00119 mh->group1_bits = mlp_quants[get_bits(&gb, 4)];
00120 mh->group2_bits = mlp_quants[get_bits(&gb, 4)];
00121
00122 ratebits = get_bits(&gb, 4);
00123 mh->group1_samplerate = mlp_samplerate(ratebits);
00124 mh->group2_samplerate = mlp_samplerate(get_bits(&gb, 4));
00125
00126 skip_bits(&gb, 11);
00127
00128 mh->channels_mlp = get_bits(&gb, 5);
00129 } else if (mh->stream_type == 0xba) {
00130 mh->group1_bits = 24;
00131 mh->group2_bits = 0;
00132
00133 ratebits = get_bits(&gb, 4);
00134 mh->group1_samplerate = mlp_samplerate(ratebits);
00135 mh->group2_samplerate = 0;
00136
00137 skip_bits(&gb, 8);
00138
00139 mh->channels_thd_stream1 = get_bits(&gb, 5);
00140
00141 skip_bits(&gb, 2);
00142
00143 mh->channels_thd_stream2 = get_bits(&gb, 13);
00144 } else
00145 return -1;
00146
00147 mh->access_unit_size = 40 << (ratebits & 7);
00148 mh->access_unit_size_pow2 = 64 << (ratebits & 7);
00149
00150 skip_bits_long(&gb, 48);
00151
00152 mh->is_vbr = get_bits1(&gb);
00153
00154 mh->peak_bitrate = (get_bits(&gb, 15) * mh->group1_samplerate + 8) >> 4;
00155
00156 mh->num_substreams = get_bits(&gb, 4);
00157
00158 skip_bits_long(&gb, 4 + 11 * 8);
00159
00160 return 0;
00161 }
00162
00163 typedef struct MLPParseContext
00164 {
00165 ParseContext pc;
00166
00167 int bytes_left;
00168
00169 int in_sync;
00170
00171 int num_substreams;
00172 } MLPParseContext;
00173
00174 static int mlp_parse(AVCodecParserContext *s,
00175 AVCodecContext *avctx,
00176 const uint8_t **poutbuf, int *poutbuf_size,
00177 const uint8_t *buf, int buf_size)
00178 {
00179 MLPParseContext *mp = s->priv_data;
00180 int sync_present;
00181 uint8_t parity_bits;
00182 int next;
00183 int i, p = 0;
00184
00185 *poutbuf_size = 0;
00186 if (buf_size == 0)
00187 return 0;
00188
00189 if (!mp->in_sync) {
00190
00191
00192 for (i = 0; i < buf_size; i++) {
00193 mp->pc.state = (mp->pc.state << 8) | buf[i];
00194 if ((mp->pc.state & 0xfffffffe) == 0xf8726fba) {
00195 mp->in_sync = 1;
00196 mp->bytes_left = 0;
00197 break;
00198 }
00199 }
00200
00201 if (!mp->in_sync) {
00202 ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
00203 return buf_size;
00204 }
00205
00206 ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
00207
00208 return i - 7;
00209 }
00210
00211 if (mp->bytes_left == 0) {
00212
00213
00214
00215 for(; mp->pc.overread>0; mp->pc.overread--) {
00216 mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
00217 }
00218
00219 if (mp->pc.index + buf_size < 2) {
00220 ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
00221 return buf_size;
00222 }
00223
00224 mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
00225 | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
00226 mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
00227 mp->bytes_left -= mp->pc.index;
00228 }
00229
00230 next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
00231
00232 if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
00233 mp->bytes_left -= buf_size;
00234 return buf_size;
00235 }
00236
00237 mp->bytes_left = 0;
00238
00239 sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
00240
00241 if (!sync_present) {
00242
00243
00244
00245 parity_bits = 0;
00246 for (i = 0; i <= mp->num_substreams; i++) {
00247 parity_bits ^= buf[p++];
00248 parity_bits ^= buf[p++];
00249
00250 if (i == 0 || buf[p-2] & 0x80) {
00251 parity_bits ^= buf[p++];
00252 parity_bits ^= buf[p++];
00253 }
00254 }
00255
00256 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
00257 av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
00258 goto lost_sync;
00259 }
00260 } else {
00261 MLPHeaderInfo mh;
00262
00263 if (ff_mlp_read_major_sync(avctx, &mh, buf + 4, buf_size - 4) < 0)
00264 goto lost_sync;
00265
00266 #ifdef CONFIG_AUDIO_NONSHORT
00267 avctx->bits_per_sample = mh.group1_bits;
00268 if (avctx->bits_per_sample > 16)
00269 avctx->sample_fmt = SAMPLE_FMT_S32;
00270 #endif
00271 avctx->sample_rate = mh.group1_samplerate;
00272 avctx->frame_size = mh.access_unit_size;
00273
00274 if (mh.stream_type == 0xbb) {
00275
00276 avctx->channels = mlp_channels[mh.channels_mlp];
00277 } else {
00278
00279 if (mh.channels_thd_stream2)
00280 avctx->channels = truehd_channels(mh.channels_thd_stream2);
00281 else
00282 avctx->channels = truehd_channels(mh.channels_thd_stream1);
00283 }
00284
00285 if (!mh.is_vbr)
00286 avctx->bit_rate = mh.peak_bitrate;
00287
00288 mp->num_substreams = mh.num_substreams;
00289 }
00290
00291 *poutbuf = buf;
00292 *poutbuf_size = buf_size;
00293
00294 return next;
00295
00296 lost_sync:
00297 mp->in_sync = 0;
00298 return -1;
00299 }
00300
00301 AVCodecParser mlp_parser = {
00302 { CODEC_ID_MLP },
00303 sizeof(MLPParseContext),
00304 NULL,
00305 mlp_parse,
00306 NULL,
00307 };