20% faster in function output_frame_end().
---
libavcodec/ac3enc.c | 74 +++++++++++++++++++++++++++-----------------------
1 files changed, 40 insertions(+), 34 deletions(-)
diff --git libavcodec/ac3enc.c libavcodec/ac3enc.c
index 1987245..7f501a0 100644
--- libavcodec/ac3enc.c
+++ libavcodec/ac3enc.c
@@ -52,6 +52,7 @@ typedef struct AC3EncodeContext {
int frame_size_min; ///< minimum frame size in case rounding is necessary
int frame_size; ///< current frame size in bytes
+ uint16_t crc_inv[2]; ///< precalculated crc_inv
int frame_size_code; ///< frame size code (frmsizecod)
int bits_written; ///< total bits written (used to avg. bitrate)
int samples_written; ///< total samples written (used to avg. bitrate)
@@ -810,13 +811,44 @@ static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
return 0;
}
+#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
+
+static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
+{
+ unsigned int c;
+
+ c = 0;
+ while (a) {
+ if (a & 1)
+ c ^= b;
+ a = a >> 1;
+ b = b << 1;
+ if (b & (1 << 16))
+ b ^= poly;
+ }
+ return c;
+}
+
+static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
+{
+ unsigned int r;
+ r = 1;
+ while (n) {
+ if (n & 1)
+ r = mul_poly(r, a, poly);
+ a = mul_poly(a, a, poly);
+ n >>= 1;
+ }
+ return r;
+}
+
static av_cold int AC3_encode_init(AVCodecContext *avctx)
{
int freq = avctx->sample_rate;
int bitrate = avctx->bit_rate;
AC3EncodeContext *s = avctx->priv_data;
int i, j, blk, ch;
- int bw_code;
+ int bw_code, frame_size_58;
s->avctx = avctx;
@@ -862,6 +894,12 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
s->samples_written = 0;
s->frame_size = s->frame_size_min;
+ /* calculate crc_inv for both possible frame sizes */
+ frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;
+ s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
+ frame_size_58 = (((s->frame_size+1) >> 2) + ((s->frame_size+1) >> 4)) << 1;
+ s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
+
/* bit allocation init */
if (avctx->cutoff) {
/* calculate bandwidth based on user-specified cutoff frequency */
@@ -1236,37 +1274,6 @@ static void output_audio_block(AC3EncodeContext *s,
}
}
-#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
-
-static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
-{
- unsigned int c;
-
- c = 0;
- while (a) {
- if (a & 1)
- c ^= b;
- a = a >> 1;
- b = b << 1;
- if (b & (1 << 16))
- b ^= poly;
- }
- return c;
-}
-
-static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
-{
- unsigned int r;
- r = 1;
- while (n) {
- if (n & 1)
- r = mul_poly(r, a, poly);
- a = mul_poly(a, a, poly);
- n >>= 1;
- }
- return r;
-}
-
/* fill the end of the frame and compute the two crcs */
static void output_frame_end(AC3EncodeContext *s)
{
@@ -1288,8 +1295,7 @@ static void output_frame_end(AC3EncodeContext *s)
frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1;
crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
frame + 4, frame_size_58 - 4));
- /* XXX: could precompute crc_inv */
- crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
+ crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
AV_WB16(frame+2,crc1);
_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc