From 15a54da104cf4670eef3d15330ac9d09f644cbbc Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 07:55:44 +0000 Subject: Simplify ad_imaadpcm decode_audio function git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27144 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index 532eecad43..a4a5bfaf5e 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -182,7 +182,7 @@ static void decode_nibbles(unsigned short *output, } static int qt_ima_adpcm_decode_block(unsigned short *output, - unsigned char *input, int channels) + unsigned char *input, int channels, int block_size) { int initial_predictor_l = 0; int initial_predictor_r = 0; @@ -351,26 +351,20 @@ static int dk4_ima_adpcm_decode_block(unsigned short *output, static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) { + int res = -1; + int (*decode_func)(unsigned short *output, unsigned char *input, int channels, int block_size) = qt_ima_adpcm_decode_block; if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, sh_audio->ds->ss_mul) != sh_audio->ds->ss_mul) return -1; if ((sh_audio->format == 0x11) || (sh_audio->format == 0x1100736d)) - { - return 2 * ms_ima_adpcm_decode_block( - (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, - sh_audio->ds->ss_mul); - } + decode_func = ms_ima_adpcm_decode_block; else if (sh_audio->format == 0x61) - { - return 2 * dk4_ima_adpcm_decode_block( - (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, - sh_audio->ds->ss_mul); - } - else - { - return 2 * qt_ima_adpcm_decode_block( - (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels); - } + decode_func = dk4_ima_adpcm_decode_block; + + res = decode_func((unsigned short*)buf, sh_audio->a_in_buffer, + sh_audio->wf->nChannels, sh_audio->ds->ss_mul); + if (res < 0) return res; + else return 2 * res; } -- cgit v1.2.3 From 2f1ffb093bafe5377382e479b6448b2de2b15a31 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 08:08:51 +0000 Subject: Add a few size checks to IMA decoder. The code is still a mess though, but bug # 1114 is probably fixed. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27145 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index a4a5bfaf5e..b3011106f7 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -190,6 +190,10 @@ static int qt_ima_adpcm_decode_block(unsigned short *output, int initial_index_r = 0; int i; + if (channels > 1) channels = 2; + if (block_size < channels * QT_IMA_ADPCM_BLOCK_SIZE) + return -1; + initial_predictor_l = BE_16(&input[0]); initial_index_l = initial_predictor_l; @@ -255,6 +259,10 @@ static int ms_ima_adpcm_decode_block(unsigned short *output, int channel_index_l; int channel_index_r; + if (channels > 1) channels = 2; + if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels) + return -1; + predictor_l = LE_16(&input[0]); SE_16BIT(predictor_l); index_l = input[2]; @@ -322,6 +330,10 @@ static int dk4_ima_adpcm_decode_block(unsigned short *output, int index_l = 0; int index_r = 0; + if (channels > 1) channels = 2; + if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels) + return -1; + // the first predictor value goes straight to the output predictor_l = output[0] = LE_16(&input[0]); SE_16BIT(predictor_l); -- cgit v1.2.3 From 8666dab4f05acb63a9e1f28cf60c22da6931ba94 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 08:20:42 +0000 Subject: Simplify code to read index/predictor git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27146 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 85 ++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 57 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index b3011106f7..067dcbaa1a 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -184,42 +184,25 @@ static void decode_nibbles(unsigned short *output, static int qt_ima_adpcm_decode_block(unsigned short *output, unsigned char *input, int channels, int block_size) { - int initial_predictor_l = 0; - int initial_predictor_r = 0; - int initial_index_l = 0; - int initial_index_r = 0; + int initial_predictor[2]; + int initial_index[2]; int i; if (channels > 1) channels = 2; if (block_size < channels * QT_IMA_ADPCM_BLOCK_SIZE) return -1; - initial_predictor_l = BE_16(&input[0]); - initial_index_l = initial_predictor_l; - - // mask, sign-extend, and clamp the predictor portion - initial_predictor_l &= 0xFF80; - SE_16BIT(initial_predictor_l); - CLAMP_S16(initial_predictor_l); - - // mask and clamp the index portion - initial_index_l &= 0x7F; - CLAMP_0_TO_88(initial_index_l); - - // handle stereo - if (channels > 1) - { - initial_predictor_r = BE_16(&input[QT_IMA_ADPCM_BLOCK_SIZE]); - initial_index_r = initial_predictor_r; + for (i = 0; i < channels; i++) { + initial_index[i] = initial_predictor[i] = BE_16(&input[i * QT_IMA_ADPCM_BLOCK_SIZE]); // mask, sign-extend, and clamp the predictor portion - initial_predictor_r &= 0xFF80; - SE_16BIT(initial_predictor_r); - CLAMP_S16(initial_predictor_r); + initial_predictor[i] &= 0xFF80; + SE_16BIT(initial_predictor[i]); + CLAMP_S16(initial_predictor[i]); // mask and clamp the index portion - initial_index_r &= 0x7F; - CLAMP_0_TO_88(initial_index_r); + initial_index[i] &= 0x7F; + CLAMP_0_TO_88(initial_index[i]); } // break apart all of the nibbles in the block @@ -240,8 +223,8 @@ static int qt_ima_adpcm_decode_block(unsigned short *output, decode_nibbles(output, QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, - initial_predictor_l, initial_index_l, - initial_predictor_r, initial_index_r); + initial_predictor[0], initial_index[0], + initial_predictor[1], initial_index[1]); return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels; } @@ -249,10 +232,8 @@ static int qt_ima_adpcm_decode_block(unsigned short *output, static int ms_ima_adpcm_decode_block(unsigned short *output, unsigned char *input, int channels, int block_size) { - int predictor_l = 0; - int predictor_r = 0; - int index_l = 0; - int index_r = 0; + int predictor[2]; + int index[2]; int i; int channel_counter; int channel_index; @@ -263,14 +244,10 @@ static int ms_ima_adpcm_decode_block(unsigned short *output, if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels) return -1; - predictor_l = LE_16(&input[0]); - SE_16BIT(predictor_l); - index_l = input[2]; - if (channels == 2) - { - predictor_r = LE_16(&input[4]); - SE_16BIT(predictor_r); - index_r = input[6]; + for (i = 0; i < channels; i++) { + predictor[i] = LE_16(&input[i * 4]); + SE_16BIT(predictor[i]); + index[i] = input[i * 4 + 2]; } if (channels == 1) @@ -314,8 +291,8 @@ static int ms_ima_adpcm_decode_block(unsigned short *output, decode_nibbles(output, (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, channels, - predictor_l, index_l, - predictor_r, index_r); + predictor[0], index[0], + predictor[1], index[1]); return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; } @@ -325,24 +302,18 @@ static int dk4_ima_adpcm_decode_block(unsigned short *output, { int i; int output_ptr; - int predictor_l = 0; - int predictor_r = 0; - int index_l = 0; - int index_r = 0; + int predictor[2]; + int index[2]; if (channels > 1) channels = 2; if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels) return -1; - // the first predictor value goes straight to the output - predictor_l = output[0] = LE_16(&input[0]); - SE_16BIT(predictor_l); - index_l = input[2]; - if (channels == 2) - { - predictor_r = output[1] = LE_16(&input[4]); - SE_16BIT(predictor_r); - index_r = input[6]; + for (i = 0; i < channels; i++) { + // the first predictor value goes straight to the output + predictor[i] = output[i] = LE_16(&input[i * 4]); + SE_16BIT(predictor[i]); + index[i] = input[i * 4 + 2]; } output_ptr = channels; @@ -355,8 +326,8 @@ static int dk4_ima_adpcm_decode_block(unsigned short *output, decode_nibbles(&output[channels], (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels, channels, - predictor_l, index_l, - predictor_r, index_r); + predictor[0], index[0], + predictor[1], index[1]); return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels; } -- cgit v1.2.3 From 64478f25d669c3bdc9d0351f4fb2f67226ff5ed1 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 08:22:47 +0000 Subject: Make imaadpcm tables const git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27147 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index 067dcbaa1a..27da073546 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -37,7 +37,7 @@ #define LE_32(x) (le2me_32(*(unsigned int *)(x))) // pertinent tables for IMA ADPCM -static int adpcm_step[89] = +static const int adpcm_step[89] = { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, @@ -50,7 +50,7 @@ static int adpcm_step[89] = 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 }; -static int adpcm_index[16] = +static const int adpcm_index[16] = { -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8 -- cgit v1.2.3 From fe3548c668058b01710e716286340be2b4bb9ea3 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 08:27:52 +0000 Subject: Use smaller types for tables git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27148 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index 27da073546..5411e72433 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -37,7 +37,7 @@ #define LE_32(x) (le2me_32(*(unsigned int *)(x))) // pertinent tables for IMA ADPCM -static const int adpcm_step[89] = +static const int16_t adpcm_step[89] = { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, @@ -50,7 +50,7 @@ static const int adpcm_step[89] = 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 }; -static const int adpcm_index[16] = +static const int8_t adpcm_index[16] = { -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8 -- cgit v1.2.3 From 5e97c2f9603d6cc1dc126d9a62f8b873c6121c17 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 08:35:27 +0000 Subject: Directly pass arrays into decode_nibbles git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27149 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index 5411e72433..bbb30fbc6c 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -132,24 +132,17 @@ static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...) static void decode_nibbles(unsigned short *output, int output_size, int channels, - int predictor_l, int index_l, - int predictor_r, int index_r) + int predictor[2], int index[2]) { int step[2]; - int predictor[2]; - int index[2]; int diff; int i; int sign; int delta; int channel_number = 0; - step[0] = adpcm_step[index_l]; - step[1] = adpcm_step[index_r]; - predictor[0] = predictor_l; - predictor[1] = predictor_r; - index[0] = index_l; - index[1] = index_r; + step[0] = adpcm_step[index[0]]; + step[1] = adpcm_step[index[1]]; for (i = 0; i < output_size; i++) { @@ -223,8 +216,7 @@ static int qt_ima_adpcm_decode_block(unsigned short *output, decode_nibbles(output, QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, - initial_predictor[0], initial_index[0], - initial_predictor[1], initial_index[1]); + initial_predictor, initial_index); return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels; } @@ -291,8 +283,7 @@ static int ms_ima_adpcm_decode_block(unsigned short *output, decode_nibbles(output, (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, channels, - predictor[0], index[0], - predictor[1], index[1]); + predictor, index); return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; } @@ -326,8 +317,7 @@ static int dk4_ima_adpcm_decode_block(unsigned short *output, decode_nibbles(&output[channels], (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels, channels, - predictor[0], index[0], - predictor[1], index[1]); + predictor, index); return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels; } -- cgit v1.2.3 From 6767061f8bcc65d53ab979d159e4c758c1f73656 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 08:42:53 +0000 Subject: Simplify some imaadpcm macros git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27150 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index bbb30fbc6c..eb7d570f3b 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -58,14 +58,13 @@ static const int8_t adpcm_index[16] = // useful macros // clamp a number between 0 and 88 -#define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88; +#define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88); // clamp a number within a signed 16-bit range -#define CLAMP_S16(x) if (x < -32768) x = -32768; \ - else if (x > 32767) x = 32767; +#define CLAMP_S16(x) x = av_clip_int16(x); // clamp a number above 16 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; // sign extend a 16-bit value -#define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; +#define SE_16BIT(x) x = (int16_t)x; // sign extend a 4-bit value #define SE_4BIT(x) if (x & 0x8) x -= 0x10; -- cgit v1.2.3 From 8532066166f35c5017c0748e9cb67053d4e25f52 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 08:47:56 +0000 Subject: Get rid of 16-bit sign extension macro git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27151 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index eb7d570f3b..f1e03b59a7 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -63,8 +63,6 @@ static const int8_t adpcm_index[16] = #define CLAMP_S16(x) x = av_clip_int16(x); // clamp a number above 16 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; -// sign extend a 16-bit value -#define SE_16BIT(x) x = (int16_t)x; // sign extend a 4-bit value #define SE_4BIT(x) if (x & 0x8) x -= 0x10; @@ -185,11 +183,10 @@ static int qt_ima_adpcm_decode_block(unsigned short *output, return -1; for (i = 0; i < channels; i++) { - initial_index[i] = initial_predictor[i] = BE_16(&input[i * QT_IMA_ADPCM_BLOCK_SIZE]); + initial_index[i] = initial_predictor[i] = (int16_t)BE_16(&input[i * QT_IMA_ADPCM_BLOCK_SIZE]); // mask, sign-extend, and clamp the predictor portion - initial_predictor[i] &= 0xFF80; - SE_16BIT(initial_predictor[i]); + initial_predictor[i] &= ~0x7F; CLAMP_S16(initial_predictor[i]); // mask and clamp the index portion @@ -236,8 +233,7 @@ static int ms_ima_adpcm_decode_block(unsigned short *output, return -1; for (i = 0; i < channels; i++) { - predictor[i] = LE_16(&input[i * 4]); - SE_16BIT(predictor[i]); + predictor[i] = (int16_t)LE_16(&input[i * 4]); index[i] = input[i * 4 + 2]; } @@ -301,8 +297,7 @@ static int dk4_ima_adpcm_decode_block(unsigned short *output, for (i = 0; i < channels; i++) { // the first predictor value goes straight to the output - predictor[i] = output[i] = LE_16(&input[i * 4]); - SE_16BIT(predictor[i]); + predictor[i] = output[i] = (int16_t)LE_16(&input[i * 4]); index[i] = input[i * 4 + 2]; } -- cgit v1.2.3 From 9f941c8a00541ffe418f95267438ccb5458db173 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 09:10:46 +0000 Subject: Simplify predictor updates git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27152 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index f1e03b59a7..7a8e89dc41 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -150,16 +150,10 @@ static void decode_nibbles(unsigned short *output, sign = delta & 8; delta = delta & 7; + delta = 2 * delta + 1; + if (sign) delta = -delta; - diff = step[channel_number] >> 3; - if (delta & 4) diff += step[channel_number]; - if (delta & 2) diff += step[channel_number] >> 1; - if (delta & 1) diff += step[channel_number] >> 2; - - if (sign) - predictor[channel_number] -= diff; - else - predictor[channel_number] += diff; + predictor[channel_number] += (delta * step[channel_number]) >> 3; CLAMP_S16(predictor[channel_number]); output[i] = predictor[channel_number]; -- cgit v1.2.3 From c2af943b84b9ef2b32d66a3207c1055fdb7b6d4e Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 29 Jun 2008 09:14:26 +0000 Subject: Half size for adpcm_index git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@27153 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpcodecs/ad_imaadpcm.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/ad_imaadpcm.c b/libmpcodecs/ad_imaadpcm.c index 7a8e89dc41..15340ab0d5 100644 --- a/libmpcodecs/ad_imaadpcm.c +++ b/libmpcodecs/ad_imaadpcm.c @@ -50,10 +50,9 @@ static const int16_t adpcm_step[89] = 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 }; -static const int8_t adpcm_index[16] = +static const int8_t adpcm_index[8] = { -1, -1, -1, -1, 2, 4, 6, 8, - -1, -1, -1, -1, 2, 4, 6, 8 }; // useful macros @@ -144,12 +143,12 @@ static void decode_nibbles(unsigned short *output, for (i = 0; i < output_size; i++) { delta = output[i]; + sign = delta & 8; + delta = delta & 7; index[channel_number] += adpcm_index[delta]; CLAMP_0_TO_88(index[channel_number]); - sign = delta & 8; - delta = delta & 7; delta = 2 * delta + 1; if (sign) delta = -delta; -- cgit v1.2.3