Mercurial > libavcodec.hg
annotate mlp_parser.c @ 7177:a82e7d9c8c34 libavcodec
Clarify comment about parity nibble.
| author | ramiro |
|---|---|
| date | Wed, 02 Jul 2008 12:24:50 +0000 |
| parents | ae78650d4ac8 |
| children | 479fc906650f |
| rev | line source |
|---|---|
| 5954 | 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 mlp_parser.c | |
| 24 * MLP parser | |
| 25 */ | |
| 26 | |
| 6763 | 27 #include "libavutil/crc.h" |
| 5954 | 28 #include "bitstream.h" |
| 29 #include "parser.h" | |
| 30 #include "mlp_parser.h" | |
| 31 | |
| 32 static const uint8_t mlp_quants[16] = { | |
| 33 16, 20, 24, 0, 0, 0, 0, 0, | |
| 34 0, 0, 0, 0, 0, 0, 0, 0, | |
| 35 }; | |
| 36 | |
| 37 static const uint8_t mlp_channels[32] = { | |
| 38 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4, | |
| 39 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 40 }; | |
| 41 | |
| 42 static const uint8_t thd_chancount[13] = { | |
| 43 // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2 | |
| 44 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1 | |
| 45 }; | |
| 46 | |
| 47 static int mlp_samplerate(int in) | |
| 48 { | |
| 49 if (in == 0xF) | |
| 50 return 0; | |
| 51 | |
| 52 return (in & 8 ? 44100 : 48000) << (in & 7) ; | |
| 53 } | |
| 54 | |
| 55 static int truehd_channels(int chanmap) | |
| 56 { | |
| 57 int channels = 0, i; | |
| 58 | |
| 59 for (i = 0; i < 13; i++) | |
| 60 channels += thd_chancount[i] * ((chanmap >> i) & 1); | |
| 61 | |
| 62 return channels; | |
| 63 } | |
| 64 | |
| 65 static int crc_init = 0; | |
| 66 static AVCRC crc_2D[1024]; | |
| 67 | |
| 68 /** MLP uses checksums that seem to be based on the standard CRC algorithm, | |
| 5980 | 69 * but not (in implementation terms, the table lookup and XOR are reversed). |
| 70 * We can implement this behavior using a standard av_crc on all but the | |
| 5954 | 71 * last element, then XOR that with the last element. |
| 72 */ | |
| 73 | |
| 74 static uint16_t mlp_checksum16(const uint8_t *buf, unsigned int buf_size) | |
| 75 { | |
| 76 uint16_t crc; | |
| 77 | |
| 78 if (!crc_init) { | |
| 79 av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D)); | |
| 80 crc_init = 1; | |
| 81 } | |
| 82 | |
| 83 crc = av_crc(crc_2D, 0, buf, buf_size - 2); | |
| 84 crc ^= AV_RL16(buf + buf_size - 2); | |
| 85 return crc; | |
| 86 } | |
| 87 | |
| 88 /** Read a major sync info header - contains high level information about | |
| 89 * the stream - sample rate, channel arrangement etc. Most of this | |
| 90 * information is not actually necessary for decoding, only for playback. | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
91 * gb must be a freshly initialized GetBitContext with no bits read. |
| 5954 | 92 */ |
| 93 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
94 int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb) |
| 5954 | 95 { |
| 96 int ratebits; | |
| 97 uint16_t checksum; | |
| 98 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
99 assert(get_bits_count(gb) == 0); |
|
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
100 |
|
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
101 if (gb->size_in_bits < 28 << 3) { |
| 5954 | 102 av_log(log, AV_LOG_ERROR, "Packet too short, unable to read major sync\n"); |
| 103 return -1; | |
| 104 } | |
| 105 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
106 checksum = mlp_checksum16(gb->buffer, 26); |
|
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
107 if (checksum != AV_RL16(gb->buffer+26)) { |
| 5954 | 108 av_log(log, AV_LOG_ERROR, "Major sync info header checksum error\n"); |
| 109 return -1; | |
| 110 } | |
| 111 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
112 if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */ |
| 5954 | 113 return -1; |
| 114 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
115 mh->stream_type = get_bits(gb, 8); |
| 5954 | 116 |
| 117 if (mh->stream_type == 0xbb) { | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
118 mh->group1_bits = mlp_quants[get_bits(gb, 4)]; |
|
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
119 mh->group2_bits = mlp_quants[get_bits(gb, 4)]; |
| 5954 | 120 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
121 ratebits = get_bits(gb, 4); |
| 5954 | 122 mh->group1_samplerate = mlp_samplerate(ratebits); |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
123 mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4)); |
| 5954 | 124 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
125 skip_bits(gb, 11); |
| 5954 | 126 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
127 mh->channels_mlp = get_bits(gb, 5); |
| 5954 | 128 } else if (mh->stream_type == 0xba) { |
| 129 mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere? | |
| 130 mh->group2_bits = 0; | |
| 131 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
132 ratebits = get_bits(gb, 4); |
| 5954 | 133 mh->group1_samplerate = mlp_samplerate(ratebits); |
| 134 mh->group2_samplerate = 0; | |
| 135 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
136 skip_bits(gb, 8); |
| 5954 | 137 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
138 mh->channels_thd_stream1 = get_bits(gb, 5); |
| 5954 | 139 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
140 skip_bits(gb, 2); |
| 5954 | 141 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
142 mh->channels_thd_stream2 = get_bits(gb, 13); |
| 5954 | 143 } else |
| 144 return -1; | |
| 145 | |
| 146 mh->access_unit_size = 40 << (ratebits & 7); | |
| 147 mh->access_unit_size_pow2 = 64 << (ratebits & 7); | |
| 148 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
149 skip_bits_long(gb, 48); |
| 5954 | 150 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
151 mh->is_vbr = get_bits1(gb); |
| 5954 | 152 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
153 mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4; |
| 5954 | 154 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
155 mh->num_substreams = get_bits(gb, 4); |
| 5954 | 156 |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
157 skip_bits_long(gb, 4 + 11 * 8); |
| 5954 | 158 |
| 159 return 0; | |
| 160 } | |
| 161 | |
| 162 typedef struct MLPParseContext | |
| 163 { | |
| 164 ParseContext pc; | |
| 165 | |
| 166 int bytes_left; | |
| 167 | |
| 168 int in_sync; | |
| 169 | |
| 170 int num_substreams; | |
| 171 } MLPParseContext; | |
| 172 | |
| 173 static int mlp_parse(AVCodecParserContext *s, | |
| 174 AVCodecContext *avctx, | |
| 175 const uint8_t **poutbuf, int *poutbuf_size, | |
| 176 const uint8_t *buf, int buf_size) | |
| 177 { | |
| 178 MLPParseContext *mp = s->priv_data; | |
| 179 int sync_present; | |
| 180 uint8_t parity_bits; | |
| 181 int next; | |
| 182 int i, p = 0; | |
| 183 | |
| 184 *poutbuf_size = 0; | |
| 185 if (buf_size == 0) | |
| 186 return 0; | |
| 187 | |
| 188 if (!mp->in_sync) { | |
| 189 // Not in sync - find a major sync header | |
| 190 | |
| 191 for (i = 0; i < buf_size; i++) { | |
| 192 mp->pc.state = (mp->pc.state << 8) | buf[i]; | |
| 193 if ((mp->pc.state & 0xfffffffe) == 0xf8726fba) { | |
| 194 mp->in_sync = 1; | |
| 195 mp->bytes_left = 0; | |
| 196 break; | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 if (!mp->in_sync) { | |
| 201 ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size); | |
| 202 return buf_size; | |
| 203 } | |
| 204 | |
| 205 ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size); | |
| 206 | |
| 207 return i - 7; | |
| 208 } | |
| 209 | |
| 210 if (mp->bytes_left == 0) { | |
| 211 // Find length of this packet | |
| 212 | |
| 213 /* Copy overread bytes from last frame into buffer. */ | |
| 214 for(; mp->pc.overread>0; mp->pc.overread--) { | |
| 215 mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++]; | |
| 216 } | |
| 217 | |
| 218 if (mp->pc.index + buf_size < 2) { | |
| 219 ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size); | |
| 220 return buf_size; | |
| 221 } | |
| 222 | |
| 223 mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8) | |
| 224 | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]); | |
| 225 mp->bytes_left = (mp->bytes_left & 0xfff) * 2; | |
| 226 mp->bytes_left -= mp->pc.index; | |
| 227 } | |
| 228 | |
| 229 next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left; | |
| 230 | |
| 231 if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) { | |
| 232 mp->bytes_left -= buf_size; | |
| 233 return buf_size; | |
| 234 } | |
| 235 | |
| 236 mp->bytes_left = 0; | |
| 237 | |
| 238 sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba; | |
| 239 | |
| 240 if (!sync_present) { | |
| 7177 | 241 /* The first nibble of a frame is a parity check of the 4-byte |
| 242 * access unit header and all the 2- or 4-byte substream headers. */ | |
| 5980 | 243 // Only check when this isn't a sync frame - syncs have a checksum. |
| 5954 | 244 |
| 245 parity_bits = 0; | |
|
7137
186eb61a9860
Make it a little easier to spot that the code is not dealing only with
ramiro
parents:
6763
diff
changeset
|
246 for (i = -1; i < mp->num_substreams; i++) { |
| 5954 | 247 parity_bits ^= buf[p++]; |
| 248 parity_bits ^= buf[p++]; | |
| 249 | |
|
7137
186eb61a9860
Make it a little easier to spot that the code is not dealing only with
ramiro
parents:
6763
diff
changeset
|
250 if (i < 0 || buf[p-2] & 0x80) { |
| 5954 | 251 parity_bits ^= buf[p++]; |
| 252 parity_bits ^= buf[p++]; | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) { | |
| 5980 | 257 av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n"); |
| 5954 | 258 goto lost_sync; |
| 259 } | |
| 260 } else { | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
261 GetBitContext gb; |
| 5954 | 262 MLPHeaderInfo mh; |
| 263 | |
|
7176
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
264 init_get_bits(&gb, buf + 4, (buf_size - 4) << 3); |
|
ae78650d4ac8
Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
ramiro
parents:
7154
diff
changeset
|
265 if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0) |
| 5954 | 266 goto lost_sync; |
| 267 | |
| 268 #ifdef CONFIG_AUDIO_NONSHORT | |
| 269 avctx->bits_per_sample = mh.group1_bits; | |
| 270 if (avctx->bits_per_sample > 16) | |
| 271 avctx->sample_fmt = SAMPLE_FMT_S32; | |
| 272 #endif | |
| 273 avctx->sample_rate = mh.group1_samplerate; | |
| 274 avctx->frame_size = mh.access_unit_size; | |
| 275 | |
| 276 if (mh.stream_type == 0xbb) { | |
| 277 /* MLP stream */ | |
| 278 avctx->channels = mlp_channels[mh.channels_mlp]; | |
| 279 } else { /* mh.stream_type == 0xba */ | |
| 280 /* TrueHD stream */ | |
| 281 if (mh.channels_thd_stream2) | |
| 282 avctx->channels = truehd_channels(mh.channels_thd_stream2); | |
| 283 else | |
| 284 avctx->channels = truehd_channels(mh.channels_thd_stream1); | |
| 285 } | |
| 286 | |
| 287 if (!mh.is_vbr) /* Stream is CBR */ | |
| 288 avctx->bit_rate = mh.peak_bitrate; | |
| 289 | |
| 290 mp->num_substreams = mh.num_substreams; | |
| 291 } | |
| 292 | |
| 293 *poutbuf = buf; | |
| 294 *poutbuf_size = buf_size; | |
| 295 | |
| 296 return next; | |
| 297 | |
| 298 lost_sync: | |
| 299 mp->in_sync = 0; | |
|
7154
a9ed669e8cd1
Seek the file forwards instead of backwards when sync is lost.
ramiro
parents:
7137
diff
changeset
|
300 return 1; |
| 5954 | 301 } |
| 302 | |
| 303 AVCodecParser mlp_parser = { | |
| 304 { CODEC_ID_MLP }, | |
| 305 sizeof(MLPParseContext), | |
| 306 NULL, | |
| 307 mlp_parse, | |
| 308 NULL, | |
| 309 }; |
