comparison alsdec.c @ 12016:ebef2d6d5a8e libavcodec

Add CRC check to the ALS decoder.
author thilo.borgmann
date Tue, 29 Jun 2010 16:56:28 +0000
parents 74c0478534cf
children fdafbcef52f5
comparison
equal deleted inserted replaced
12015:b9bbf46b3623 12016:ebef2d6d5a8e
33 #include "get_bits.h" 33 #include "get_bits.h"
34 #include "unary.h" 34 #include "unary.h"
35 #include "mpeg4audio.h" 35 #include "mpeg4audio.h"
36 #include "bytestream.h" 36 #include "bytestream.h"
37 #include "bgmc.h" 37 #include "bgmc.h"
38 #include "dsputil.h"
39 #include "libavutil/crc.h"
38 40
39 #include <stdint.h> 41 #include <stdint.h>
40 42
41 /** Rice parameters and corresponding index offsets for decoding the 43 /** Rice parameters and corresponding index offsets for decoding the
42 * indices of scaled PARCOR values. The table choosen is set globally 44 * indices of scaled PARCOR values. The table choosen is set globally
152 154
153 typedef struct { 155 typedef struct {
154 uint32_t samples; ///< number of samples, 0xFFFFFFFF if unknown 156 uint32_t samples; ///< number of samples, 0xFFFFFFFF if unknown
155 int resolution; ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit 157 int resolution; ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit
156 int floating; ///< 1 = IEEE 32-bit floating-point, 0 = integer 158 int floating; ///< 1 = IEEE 32-bit floating-point, 0 = integer
159 int msb_first; ///< 1 = original CRC calculated on big-endian system, 0 = little-endian
157 int frame_length; ///< frame length for each frame (last frame may differ) 160 int frame_length; ///< frame length for each frame (last frame may differ)
158 int ra_distance; ///< distance between RA frames (in frames, 0...255) 161 int ra_distance; ///< distance between RA frames (in frames, 0...255)
159 enum RA_Flag ra_flag; ///< indicates where the size of ra units is stored 162 enum RA_Flag ra_flag; ///< indicates where the size of ra units is stored
160 int adapt_order; ///< adaptive order: 1 = on, 0 = off 163 int adapt_order; ///< adaptive order: 1 = on, 0 = off
161 int coef_table; ///< table index of Rice code parameters 164 int coef_table; ///< table index of Rice code parameters
169 int chan_config; ///< indicates that a chan_config_info field is present 172 int chan_config; ///< indicates that a chan_config_info field is present
170 int chan_sort; ///< channel rearrangement: 1 = on, 0 = off 173 int chan_sort; ///< channel rearrangement: 1 = on, 0 = off
171 int rlslms; ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off 174 int rlslms; ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off
172 int chan_config_info; ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented. 175 int chan_config_info; ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented.
173 int *chan_pos; ///< original channel positions 176 int *chan_pos; ///< original channel positions
177 int crc_enabled; ///< enable Cyclic Redundancy Checksum
174 } ALSSpecificConfig; 178 } ALSSpecificConfig;
175 179
176 180
177 typedef struct { 181 typedef struct {
178 int stop_flag; 182 int stop_flag;
186 190
187 typedef struct { 191 typedef struct {
188 AVCodecContext *avctx; 192 AVCodecContext *avctx;
189 ALSSpecificConfig sconf; 193 ALSSpecificConfig sconf;
190 GetBitContext gb; 194 GetBitContext gb;
195 DSPContext dsp;
196 const AVCRC *crc_table;
197 uint32_t crc_org; ///< CRC value of the original input data
198 uint32_t crc; ///< CRC value calculated from decoded data
191 unsigned int cur_frame_length; ///< length of the current frame to decode 199 unsigned int cur_frame_length; ///< length of the current frame to decode
192 unsigned int frame_id; ///< the frame ID / number of the current frame 200 unsigned int frame_id; ///< the frame ID / number of the current frame
193 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced 201 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced
194 unsigned int num_blocks; ///< number of blocks used in the current frame 202 unsigned int num_blocks; ///< number of blocks used in the current frame
195 unsigned int s_max; ///< maximum Rice parameter allowed in entropy coding 203 unsigned int s_max; ///< maximum Rice parameter allowed in entropy coding
209 ALSChannelData *chan_data_buffer; ///< contains channel data for all channels 217 ALSChannelData *chan_data_buffer; ///< contains channel data for all channels
210 int *reverted_channels; ///< stores a flag for each reverted channel 218 int *reverted_channels; ///< stores a flag for each reverted channel
211 int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block 219 int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
212 int32_t **raw_samples; ///< decoded raw samples for each channel 220 int32_t **raw_samples; ///< decoded raw samples for each channel
213 int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples 221 int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples
222 uint8_t *crc_buffer; ///< buffer of byte order corrected samples used for CRC check
214 } ALSDecContext; 223 } ALSDecContext;
215 224
216 225
217 typedef struct { 226 typedef struct {
218 unsigned int block_length; ///< number of samples within the block 227 unsigned int block_length; ///< number of samples within the block
266 */ 275 */
267 static av_cold int read_specific_config(ALSDecContext *ctx) 276 static av_cold int read_specific_config(ALSDecContext *ctx)
268 { 277 {
269 GetBitContext gb; 278 GetBitContext gb;
270 uint64_t ht_size; 279 uint64_t ht_size;
271 int i, config_offset, crc_enabled; 280 int i, config_offset;
272 MPEG4AudioConfig m4ac; 281 MPEG4AudioConfig m4ac;
273 ALSSpecificConfig *sconf = &ctx->sconf; 282 ALSSpecificConfig *sconf = &ctx->sconf;
274 AVCodecContext *avctx = ctx->avctx; 283 AVCodecContext *avctx = ctx->avctx;
275 uint32_t als_id, header_size, trailer_size; 284 uint32_t als_id, header_size, trailer_size;
276 285
295 avctx->channels = m4ac.channels; 304 avctx->channels = m4ac.channels;
296 skip_bits(&gb, 16); // number of channels already knwon 305 skip_bits(&gb, 16); // number of channels already knwon
297 skip_bits(&gb, 3); // skip file_type 306 skip_bits(&gb, 3); // skip file_type
298 sconf->resolution = get_bits(&gb, 3); 307 sconf->resolution = get_bits(&gb, 3);
299 sconf->floating = get_bits1(&gb); 308 sconf->floating = get_bits1(&gb);
300 skip_bits1(&gb); // skip msb_first 309 sconf->msb_first = get_bits1(&gb);
301 sconf->frame_length = get_bits(&gb, 16) + 1; 310 sconf->frame_length = get_bits(&gb, 16) + 1;
302 sconf->ra_distance = get_bits(&gb, 8); 311 sconf->ra_distance = get_bits(&gb, 8);
303 sconf->ra_flag = get_bits(&gb, 2); 312 sconf->ra_flag = get_bits(&gb, 2);
304 sconf->adapt_order = get_bits1(&gb); 313 sconf->adapt_order = get_bits1(&gb);
305 sconf->coef_table = get_bits(&gb, 2); 314 sconf->coef_table = get_bits(&gb, 2);
310 sconf->sb_part = get_bits1(&gb); 319 sconf->sb_part = get_bits1(&gb);
311 sconf->joint_stereo = get_bits1(&gb); 320 sconf->joint_stereo = get_bits1(&gb);
312 sconf->mc_coding = get_bits1(&gb); 321 sconf->mc_coding = get_bits1(&gb);
313 sconf->chan_config = get_bits1(&gb); 322 sconf->chan_config = get_bits1(&gb);
314 sconf->chan_sort = get_bits1(&gb); 323 sconf->chan_sort = get_bits1(&gb);
315 crc_enabled = get_bits1(&gb); 324 sconf->crc_enabled = get_bits1(&gb);
316 sconf->rlslms = get_bits1(&gb); 325 sconf->rlslms = get_bits1(&gb);
317 skip_bits(&gb, 5); // skip 5 reserved bits 326 skip_bits(&gb, 5); // skip 5 reserved bits
318 skip_bits1(&gb); // skip aux_data_enabled 327 skip_bits1(&gb); // skip aux_data_enabled
319 328
320 329
373 return -1; 382 return -1;
374 383
375 skip_bits_long(&gb, ht_size); 384 skip_bits_long(&gb, ht_size);
376 385
377 386
378 // skip the crc data 387 // initialize CRC calculation
379 if (crc_enabled) { 388 if (sconf->crc_enabled) {
380 if (get_bits_left(&gb) < 32) 389 if (get_bits_left(&gb) < 32)
381 return -1; 390 return -1;
382 391
383 skip_bits_long(&gb, 32); 392 if (avctx->error_recognition >= FF_ER_CAREFUL) {
393 ctx->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
394 ctx->crc = 0xFFFFFFFF;
395 ctx->crc_org = ~get_bits_long(&gb, 32);
396 } else
397 skip_bits_long(&gb, 32);
384 } 398 }
385 399
386 400
387 // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data) 401 // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
388 402
1434 if (ctx->avctx->bits_per_raw_sample <= 16) { 1448 if (ctx->avctx->bits_per_raw_sample <= 16) {
1435 INTERLEAVE_OUTPUT(16) 1449 INTERLEAVE_OUTPUT(16)
1436 } else { 1450 } else {
1437 INTERLEAVE_OUTPUT(32) 1451 INTERLEAVE_OUTPUT(32)
1438 } 1452 }
1453
1454 // update CRC
1455 if (sconf->crc_enabled && avctx->error_recognition >= FF_ER_CAREFUL) {
1456 int swap = HAVE_BIGENDIAN != sconf->msb_first;
1457
1458 if (ctx->avctx->bits_per_raw_sample == 24) {
1459 int32_t *src = data;
1460
1461 for (sample = 0;
1462 sample < ctx->cur_frame_length * avctx->channels;
1463 sample++) {
1464 int32_t v;
1465
1466 if (swap)
1467 v = bswap_32(src[sample]);
1468 else
1469 v = src[sample];
1470 if (!HAVE_BIGENDIAN)
1471 v >>= 8;
1472
1473 ctx->crc = av_crc(ctx->crc_table, ctx->crc, (uint8_t*)(&v), 3);
1474 }
1475 } else {
1476 uint8_t *crc_source;
1477
1478 if (swap) {
1479 if (ctx->avctx->bits_per_raw_sample <= 16) {
1480 int16_t *src = (int16_t*) data;
1481 int16_t *dest = (int16_t*) ctx->crc_buffer;
1482 for (sample = 0;
1483 sample < ctx->cur_frame_length * avctx->channels;
1484 sample++)
1485 *dest++ = bswap_16(src[sample]);
1486 } else {
1487 ctx->dsp.bswap_buf((uint32_t*)ctx->crc_buffer, data,
1488 ctx->cur_frame_length * avctx->channels);
1489 }
1490 crc_source = ctx->crc_buffer;
1491 } else {
1492 crc_source = data;
1493 }
1494
1495 ctx->crc = av_crc(ctx->crc_table, ctx->crc, crc_source, size);
1496 }
1497
1498
1499 // check CRC sums if this is the last frame
1500 if (ctx->cur_frame_length != sconf->frame_length &&
1501 ctx->crc_org != ctx->crc) {
1502 av_log(avctx, AV_LOG_ERROR, "CRC error.\n");
1503 }
1504 }
1505
1439 1506
1440 bytes_read = invalid_frame ? buffer_size : 1507 bytes_read = invalid_frame ? buffer_size :
1441 (get_bits_count(&ctx->gb) + 7) >> 3; 1508 (get_bits_count(&ctx->gb) + 7) >> 3;
1442 1509
1443 return bytes_read; 1510 return bytes_read;
1604 // assign raw samples buffers 1671 // assign raw samples buffers
1605 ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order; 1672 ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
1606 for (c = 1; c < avctx->channels; c++) 1673 for (c = 1; c < avctx->channels; c++)
1607 ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size; 1674 ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
1608 1675
1676 // allocate crc buffer
1677 if (HAVE_BIGENDIAN != sconf->msb_first && sconf->crc_enabled &&
1678 avctx->error_recognition >= FF_ER_CAREFUL) {
1679 ctx->crc_buffer = av_malloc(sizeof(*ctx->crc_buffer) *
1680 ctx->cur_frame_length *
1681 avctx->channels *
1682 (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3));
1683 if (!ctx->crc_buffer) {
1684 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1685 decode_end(avctx);
1686 return AVERROR(ENOMEM);
1687 }
1688 }
1689
1690 dsputil_init(&ctx->dsp, avctx);
1691
1609 return 0; 1692 return 0;
1610 } 1693 }
1611 1694
1612 1695
1613 /** Flushes (resets) the frame ID after seeking. 1696 /** Flushes (resets) the frame ID after seeking.