Signed-off-by: James Almer <jamrial@gmail.com>
foo86 authored on 2016/05/03 09:42:36... | ... |
@@ -269,8 +269,7 @@ static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xc |
269 | 269 |
|
270 | 270 |
// Check CRC |
271 | 271 |
if (s->xxch_crc_present |
272 |
- && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
273 |
- && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) { |
|
272 |
+ && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) { |
|
274 | 273 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n"); |
275 | 274 |
return AVERROR_INVALIDDATA; |
276 | 275 |
} |
... | ... |
@@ -977,8 +976,7 @@ static int parse_xxch_frame(DCACoreDecoder *s) |
977 | 977 |
header_size = get_bits(&s->gb, 6) + 1; |
978 | 978 |
|
979 | 979 |
// Check XXCH frame header CRC |
980 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
981 |
- && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) { |
|
980 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) { |
|
982 | 981 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n"); |
983 | 982 |
return AVERROR_INVALIDDATA; |
984 | 983 |
} |
... | ... |
@@ -1193,8 +1191,7 @@ static int parse_xbr_frame(DCACoreDecoder *s) |
1193 | 1193 |
header_size = get_bits(&s->gb, 6) + 1; |
1194 | 1194 |
|
1195 | 1195 |
// Check XBR frame header CRC |
1196 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
1197 |
- && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) { |
|
1196 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) { |
|
1198 | 1197 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n"); |
1199 | 1198 |
return AVERROR_INVALIDDATA; |
1200 | 1199 |
} |
... | ... |
@@ -1509,8 +1506,7 @@ static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base) |
1509 | 1509 |
|
1510 | 1510 |
// Check CRC |
1511 | 1511 |
if (s->x96_crc_present |
1512 |
- && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
1513 |
- && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) { |
|
1512 |
+ && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) { |
|
1514 | 1513 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n"); |
1515 | 1514 |
return AVERROR_INVALIDDATA; |
1516 | 1515 |
} |
... | ... |
@@ -1665,8 +1661,7 @@ static int parse_x96_frame_exss(DCACoreDecoder *s) |
1665 | 1665 |
header_size = get_bits(&s->gb, 6) + 1; |
1666 | 1666 |
|
1667 | 1667 |
// Check X96 frame header CRC |
1668 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
1669 |
- && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) { |
|
1668 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) { |
|
1670 | 1669 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n"); |
1671 | 1670 |
return AVERROR_INVALIDDATA; |
1672 | 1671 |
} |
... | ... |
@@ -1785,8 +1780,7 @@ static int parse_aux_data(DCACoreDecoder *s) |
1785 | 1785 |
skip_bits(&s->gb, 16); |
1786 | 1786 |
|
1787 | 1787 |
// Check CRC |
1788 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
1789 |
- && ff_dca_check_crc(&s->gb, aux_pos, get_bits_count(&s->gb))) { |
|
1788 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) { |
|
1790 | 1789 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n"); |
1791 | 1790 |
return AVERROR_INVALIDDATA; |
1792 | 1791 |
} |
... | ... |
@@ -1884,9 +1878,10 @@ static int parse_optional_info(DCACoreDecoder *s) |
1884 | 1884 |
if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) { |
1885 | 1885 |
s->gb.index = (sync_pos + 1) * 32; |
1886 | 1886 |
size = get_bits(&s->gb, 6) + 1; |
1887 |
- if (size >= 11 && |
|
1888 |
- !ff_dca_check_crc(&s->gb, (sync_pos + 1) * 32, |
|
1889 |
- sync_pos * 32 + size * 8)) { |
|
1887 |
+ dist = s->gb.size_in_bits / 8 - sync_pos * 4; |
|
1888 |
+ if (size >= 11 && size <= dist && |
|
1889 |
+ !av_crc(dca->crctab, 0xffff, s->gb.buffer + |
|
1890 |
+ (sync_pos + 1) * 4, size - 4)) { |
|
1890 | 1891 |
s->xxch_pos = sync_pos * 32; |
1891 | 1892 |
break; |
1892 | 1893 |
} |
... | ... |
@@ -398,8 +398,7 @@ int ff_dca_exss_parse(DCAExssParser *s, uint8_t *data, int size) |
398 | 398 |
header_size = get_bits(&s->gb, 8 + 4 * wide_hdr) + 1; |
399 | 399 |
|
400 | 400 |
// Check CRC |
401 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
402 |
- && ff_dca_check_crc(&s->gb, 32 + 8, header_size * 8)) { |
|
401 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, 32 + 8, header_size * 8)) { |
|
403 | 402 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid EXSS header checksum\n"); |
404 | 403 |
return AVERROR_INVALIDDATA; |
405 | 404 |
} |
... | ... |
@@ -123,8 +123,7 @@ static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asse |
123 | 123 |
header_size = get_bits(&s->gb, 10) + 1; |
124 | 124 |
|
125 | 125 |
// Check CRC |
126 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
127 |
- && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) { |
|
126 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) { |
|
128 | 127 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n"); |
129 | 128 |
return AVERROR_INVALIDDATA; |
130 | 129 |
} |
... | ... |
@@ -784,8 +783,7 @@ static int parse_common_header(DCAXllDecoder *s) |
784 | 784 |
header_size = get_bits(&s->gb, 8) + 1; |
785 | 785 |
|
786 | 786 |
// Check CRC |
787 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
788 |
- && ff_dca_check_crc(&s->gb, 32, header_size * 8)) { |
|
787 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, 32, header_size * 8)) { |
|
789 | 788 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n"); |
790 | 789 |
return AVERROR_INVALIDDATA; |
791 | 790 |
} |
... | ... |
@@ -993,8 +991,7 @@ static int parse_navi_table(DCAXllDecoder *s) |
993 | 993 |
skip_bits(&s->gb, 16); |
994 | 994 |
|
995 | 995 |
// Check CRC |
996 |
- if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) |
|
997 |
- && ff_dca_check_crc(&s->gb, navi_pos, get_bits_count(&s->gb))) { |
|
996 |
+ if (ff_dca_check_crc(s->avctx, &s->gb, navi_pos, get_bits_count(&s->gb))) { |
|
998 | 997 |
av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n"); |
999 | 998 |
return AVERROR_INVALIDDATA; |
1000 | 999 |
} |
... | ... |
@@ -76,33 +76,6 @@ int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask |
76 | 76 |
return nchannels; |
77 | 77 |
} |
78 | 78 |
|
79 |
-static uint16_t crc16(const uint8_t *data, int size) |
|
80 |
-{ |
|
81 |
- static const uint16_t crctab[16] = { |
|
82 |
- 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, |
|
83 |
- 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, |
|
84 |
- }; |
|
85 |
- |
|
86 |
- uint16_t res = 0xffff; |
|
87 |
- int i; |
|
88 |
- |
|
89 |
- for (i = 0; i < size; i++) { |
|
90 |
- res = (res << 4) ^ crctab[(data[i] >> 4) ^ (res >> 12)]; |
|
91 |
- res = (res << 4) ^ crctab[(data[i] & 15) ^ (res >> 12)]; |
|
92 |
- } |
|
93 |
- |
|
94 |
- return res; |
|
95 |
-} |
|
96 |
- |
|
97 |
-int ff_dca_check_crc(GetBitContext *s, int p1, int p2) |
|
98 |
-{ |
|
99 |
- if (((p1 | p2) & 7) || p1 < 0 || p2 > s->size_in_bits || p2 - p1 < 16) |
|
100 |
- return -1; |
|
101 |
- if (crc16(s->buffer + p1 / 8, (p2 - p1) / 8)) |
|
102 |
- return -1; |
|
103 |
- return 0; |
|
104 |
-} |
|
105 |
- |
|
106 | 79 |
void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples, |
107 | 80 |
int *coeff_l, int nsamples, int ch_mask) |
108 | 81 |
{ |
... | ... |
@@ -359,6 +332,8 @@ static av_cold int dcadec_init(AVCodecContext *avctx) |
359 | 359 |
s->core.dcadsp = &s->dcadsp; |
360 | 360 |
s->xll.dcadsp = &s->dcadsp; |
361 | 361 |
|
362 |
+ s->crctab = av_crc_get_table(AV_CRC_16_CCITT); |
|
363 |
+ |
|
362 | 364 |
switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) { |
363 | 365 |
case 0: |
364 | 366 |
s->request_channel_layout = 0; |
... | ... |
@@ -22,6 +22,7 @@ |
22 | 22 |
#define AVCODEC_DCADEC_H |
23 | 23 |
|
24 | 24 |
#include "libavutil/common.h" |
25 |
+#include "libavutil/crc.h" |
|
25 | 26 |
#include "libavutil/float_dsp.h" |
26 | 27 |
|
27 | 28 |
#include "avcodec.h" |
... | ... |
@@ -49,6 +50,8 @@ typedef struct DCAContext { |
49 | 49 |
|
50 | 50 |
DCADSPContext dcadsp; |
51 | 51 |
|
52 |
+ const AVCRC *crctab; |
|
53 |
+ |
|
52 | 54 |
uint8_t *buffer; ///< Packet buffer |
53 | 55 |
unsigned int buffer_size; |
54 | 56 |
|
... | ... |
@@ -62,13 +65,25 @@ typedef struct DCAContext { |
62 | 62 |
|
63 | 63 |
int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask); |
64 | 64 |
|
65 |
-int ff_dca_check_crc(GetBitContext *s, int p1, int p2); |
|
66 |
- |
|
67 | 65 |
void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples, |
68 | 66 |
int *coeff_l, int nsamples, int ch_mask); |
69 | 67 |
void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples, |
70 | 68 |
int *coeff_l, int nsamples, int ch_mask); |
71 | 69 |
|
70 |
+static inline int ff_dca_check_crc(AVCodecContext *avctx, GetBitContext *s, |
|
71 |
+ int p1, int p2) |
|
72 |
+{ |
|
73 |
+ DCAContext *dca = avctx->priv_data; |
|
74 |
+ |
|
75 |
+ if (!(avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))) |
|
76 |
+ return 0; |
|
77 |
+ if (((p1 | p2) & 7) || p1 < 0 || p2 > s->size_in_bits || p2 - p1 < 16) |
|
78 |
+ return -1; |
|
79 |
+ if (av_crc(dca->crctab, 0xffff, s->buffer + p1 / 8, (p2 - p1) / 8)) |
|
80 |
+ return -1; |
|
81 |
+ return 0; |
|
82 |
+} |
|
83 |
+ |
|
72 | 84 |
static inline int ff_dca_seek_bits(GetBitContext *s, int p) |
73 | 85 |
{ |
74 | 86 |
if (p < s->index || p > s->size_in_bits) |