History

avcodec/ac3enc: Avoid calculating the CRC multiple times

Andreas Rheinhardt committed 2 years ago

Commit 0458fdb1 parent 28ffc391 file · +9 −8Browse files

Unified · Split

libavcodec/ac3enc.c+9−8
@@ -1942,7 +1942,7 @@ static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly) · 1941 unmodified lines
19421942 static void output_frame_end(AC3EncodeContext *s)
19431943 {
19441944 const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
1945− int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1945+ int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
19461946 uint8_t *frame;
19471947
19481948 frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
@@ -1958,7 +1958,7 @@ static void output_frame_end(AC3EncodeContext *s) · 9 unmodified lines
19581958
19591959 if (s->eac3) {
19601960 /* compute crc2 */
1961− crc2_partial = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 5);
1961+ crc2 = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 4);
19621962 } else {
19631963 /* compute crc1 */
19641964 /* this is not so easy because it is at the beginning of the data... */
@@ -1968,16 +1968,17 @@ static void output_frame_end(AC3EncodeContext *s) · 3 unmodified lines
19681968 AV_WB16(frame + 2, crc1);
19691969
19701970 /* compute crc2 */
1971− crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
1972− s->frame_size - frame_size_58 - 3);
1971+ crc2 = av_crc(crc_ctx, 0, frame + frame_size_58,
1972+ s->frame_size - frame_size_58 - 2);
19731973 }
1974− crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
1974+ crc2 = av_bswap16(crc2);
19751975 /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
1976− if (crc2 == 0x770B) {
1976+ if (crc2 == 0x0B77) {
1977+ /* The CRC generator polynomial is x^16 + x^15 + x^2 + 1,
1978+ * so xor'ing with 0x18005 does not affect the CRC. */
19771979 frame[s->frame_size - 3] ^= 0x1;
1978− crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
1980+ crc2 ^= 0x8005;
19791981 }
1980− crc2 = av_bswap16(crc2);
19811982 AV_WB16(frame + s->frame_size - 2, crc2);
19821983 }
19831984

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t