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