annotate libmpcodecs/ad_msadpcm.c @ 34984:c24d1a0c7665

Make compiler give the ana_coeff array automatically the right size. Makes adding more colour schemes easier.
author reimar
date Sun, 12 Aug 2012 17:20:24 +0000
parents a93891202051
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
1 /*
30421
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
2 * MS ADPCM decoder
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
3 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
4 * This file is responsible for decoding Microsoft ADPCM data.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
5 * Details about the data format can be found here:
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
6 * http://www.pcisys.net/~melanson/codecs/
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
7 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
8 * Copyright (c) 2002 Mike Melanson
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
9 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
10 * This file is part of MPlayer.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
11 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
12 * MPlayer is free software; you can redistribute it and/or modify
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
13 * it under the terms of the GNU General Public License as published by
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
14 * the Free Software Foundation; either version 2 of the License, or
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
15 * (at your option) any later version.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
16 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
17 * MPlayer is distributed in the hope that it will be useful,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
20 * GNU General Public License for more details.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
21 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
22 * You should have received a copy of the GNU General Public License along
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
23 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
25 */
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
26
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
27 #include <stdio.h>
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
28 #include <stdlib.h>
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
29 #include <unistd.h>
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
30
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
31 #include "config.h"
34174
a93891202051 Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents: 30504
diff changeset
32 #include "mp_msg.h"
27224
f9b5d028f097 Copy macro simplification from imaadpcm to msadpcm
reimar
parents: 27223
diff changeset
33 #include "libavutil/common.h"
27225
46bcd3f1b123 Simplify ad_msadpmc.c: Use AV_RL16, merge sign extension into LE_16 read and
reimar
parents: 27224
diff changeset
34 #include "libavutil/intreadwrite.h"
21507
fa99b3d31d13 Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents: 21372
diff changeset
35 #include "mpbswap.h"
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
36 #include "ad_internal.h"
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
37
30504
cc27da5d7286 Mark all ad_info_t/vd_info_t structure declarations as const.
diego
parents: 30421
diff changeset
38 static const ad_info_t info =
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
39 {
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
40 "MS ADPCM audio decoder",
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
41 "msadpcm",
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
42 "Nick Kurshev",
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
43 "Mike Melanson",
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
44 ""
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
45 };
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
46
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
47 LIBAD_EXTERN(msadpcm)
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
48
27222
5b7bc104bdaf Make msadpcm arrays const
reimar
parents: 26280
diff changeset
49 static const int ms_adapt_table[] =
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
50 {
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
51 230, 230, 230, 230, 307, 409, 512, 614,
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
52 768, 614, 512, 409, 307, 230, 230, 230
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
53 };
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
54
27237
6a283385441b 100l, fix MS ADPCM decoding for e.g. http://samples.mplayerhq.hu/mov/qtaudio/surge-2-16-L-ms02.mov
reimar
parents: 27228
diff changeset
55 static const uint8_t ms_adapt_coeff1[] =
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
56 {
27226
847ed11ede5e Scale msadpcm coefficients to fit into 8 bits
reimar
parents: 27225
diff changeset
57 64, 128, 0, 48, 60, 115, 98
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
58 };
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
59
27226
847ed11ede5e Scale msadpcm coefficients to fit into 8 bits
reimar
parents: 27225
diff changeset
60 static const int8_t ms_adapt_coeff2[] =
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
61 {
27226
847ed11ede5e Scale msadpcm coefficients to fit into 8 bits
reimar
parents: 27225
diff changeset
62 0, -64, 0, 16, 0, -52, -58
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
63 };
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
64
6609
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
65 #define MS_ADPCM_PREAMBLE_SIZE 6
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
66
27225
46bcd3f1b123 Simplify ad_msadpmc.c: Use AV_RL16, merge sign extension into LE_16 read and
reimar
parents: 27224
diff changeset
67 #define LE_16(x) ((int16_t)AV_RL16(x))
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
68
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
69 // clamp a number between 0 and 88
27224
f9b5d028f097 Copy macro simplification from imaadpcm to msadpcm
reimar
parents: 27223
diff changeset
70 #define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88);
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
71 // clamp a number within a signed 16-bit range
27224
f9b5d028f097 Copy macro simplification from imaadpcm to msadpcm
reimar
parents: 27223
diff changeset
72 #define CLAMP_S16(x) x = av_clip_int16(x);
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
73 // clamp a number above 16
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
74 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
75 // sign extend a 4-bit value
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
76 #define SE_4BIT(x) if (x & 0x8) x -= 0x10;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
77
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
78 static int preinit(sh_audio_t *sh_audio)
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
79 {
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
80 sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 4;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27238
diff changeset
81 sh_audio->ds->ss_div =
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
82 (sh_audio->wf->nBlockAlign - MS_ADPCM_PREAMBLE_SIZE) * 2;
5458
b3d1348b251f audio input buffer allocation/free cleanup
arpi
parents: 5408
diff changeset
83 sh_audio->audio_in_minsize =
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
84 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign;
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
85 return 1;
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
86 }
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
87
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
88 static int init(sh_audio_t *sh_audio)
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
89 {
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
90 sh_audio->channels=sh_audio->wf->nChannels;
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
91 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
92 sh_audio->i_bps = sh_audio->wf->nBlockAlign *
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
93 (sh_audio->channels*sh_audio->samplerate) / sh_audio->ds->ss_div;
13427
9d0b052c4f74 setting samplesize to 2 in decoders where neccessary.
reimar
parents: 7893
diff changeset
94 sh_audio->samplesize=2;
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
95
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
96 return 1;
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
97 }
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
98
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
99 static void uninit(sh_audio_t *sh_audio)
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
100 {
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
101 }
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
102
5481
8ae2bf330ad5 control() done
arpi
parents: 5458
diff changeset
103 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...)
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
104 {
5481
8ae2bf330ad5 control() done
arpi
parents: 5458
diff changeset
105 if(cmd==ADCTRL_SKIP_FRAME){
8ae2bf330ad5 control() done
arpi
parents: 5458
diff changeset
106 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul);
8ae2bf330ad5 control() done
arpi
parents: 5458
diff changeset
107 return CONTROL_TRUE;
8ae2bf330ad5 control() done
arpi
parents: 5458
diff changeset
108 }
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
109 return CONTROL_UNKNOWN;
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
110 }
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
111
27238
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
112 static inline int check_coeff(uint8_t c) {
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
113 if (c > 6) {
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
114 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
115 "MS ADPCM: coefficient (%d) out of range (should be [0..6])\n",
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
116 c);
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
117 c = 6;
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
118 }
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
119 return c;
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
120 }
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
121
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
122 static int ms_adpcm_decode_block(unsigned short *output, unsigned char *input,
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
123 int channels, int block_size)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
124 {
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
125 int current_channel = 0;
27238
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
126 int coeff_idx;
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
127 int idelta[2];
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
128 int sample1[2];
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
129 int sample2[2];
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
130 int coeff1[2];
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
131 int coeff2[2];
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
132 int stream_ptr = 0;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
133 int out_ptr = 0;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
134 int upper_nibble = 1;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
135 int nibble;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
136 int snibble; // signed nibble
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
137 int predictor;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
138
27228
860c265b08e8 Check length of input buffer for msadpcm
reimar
parents: 27227
diff changeset
139 if (channels != 1) channels = 2;
860c265b08e8 Check length of input buffer for msadpcm
reimar
parents: 27227
diff changeset
140 if (block_size < 7 * channels)
860c265b08e8 Check length of input buffer for msadpcm
reimar
parents: 27227
diff changeset
141 return -1;
860c265b08e8 Check length of input buffer for msadpcm
reimar
parents: 27227
diff changeset
142
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
143 // fetch the header information, in stereo if both channels are present
27238
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
144 coeff_idx = check_coeff(input[stream_ptr]);
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
145 coeff1[0] = ms_adapt_coeff1[coeff_idx];
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
146 coeff2[0] = ms_adapt_coeff2[coeff_idx];
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
147 stream_ptr++;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
148 if (channels == 2)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
149 {
27238
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
150 coeff_idx = check_coeff(input[stream_ptr]);
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
151 coeff1[1] = ms_adapt_coeff1[coeff_idx];
7aa646bb7589 Simplify and clamp coefficient index for MS ADPCM
reimar
parents: 27237
diff changeset
152 coeff2[1] = ms_adapt_coeff2[coeff_idx];
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
153 stream_ptr++;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
154 }
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
155
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
156 idelta[0] = LE_16(&input[stream_ptr]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
157 stream_ptr += 2;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
158 if (channels == 2)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
159 {
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
160 idelta[1] = LE_16(&input[stream_ptr]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
161 stream_ptr += 2;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
162 }
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
163
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
164 sample1[0] = LE_16(&input[stream_ptr]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
165 stream_ptr += 2;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
166 if (channels == 2)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
167 {
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
168 sample1[1] = LE_16(&input[stream_ptr]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
169 stream_ptr += 2;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
170 }
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
171
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
172 sample2[0] = LE_16(&input[stream_ptr]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
173 stream_ptr += 2;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
174 if (channels == 2)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
175 {
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
176 sample2[1] = LE_16(&input[stream_ptr]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
177 stream_ptr += 2;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
178 }
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
179
6609
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
180 if (channels == 1)
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
181 {
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
182 output[out_ptr++] = sample2[0];
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
183 output[out_ptr++] = sample1[0];
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
184 } else {
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
185 output[out_ptr++] = sample2[0];
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
186 output[out_ptr++] = sample2[1];
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
187 output[out_ptr++] = sample1[0];
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
188 output[out_ptr++] = sample1[1];
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
189 }
7745d73a5f20 bugfix by Keiki SATOH <kki@wakusei.ne.jp>
arpi
parents: 5481
diff changeset
190
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
191 while (stream_ptr < block_size)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
192 {
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
193 // get the next nibble
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
194 if (upper_nibble)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
195 nibble = snibble = input[stream_ptr] >> 4;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
196 else
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
197 nibble = snibble = input[stream_ptr++] & 0x0F;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
198 upper_nibble ^= 1;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
199 SE_4BIT(snibble);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
200
27227
2de688c15263 Add a comment on shift vs. division
reimar
parents: 27226
diff changeset
201 // should this really be a division and not a shift?
2de688c15263 Add a comment on shift vs. division
reimar
parents: 27226
diff changeset
202 // coefficients were originally scaled by for, which might have
2de688c15263 Add a comment on shift vs. division
reimar
parents: 27226
diff changeset
203 // been an optimization for 8-bit CPUs _if_ a shift is correct
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
204 predictor = (
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
205 ((sample1[current_channel] * coeff1[current_channel]) +
27226
847ed11ede5e Scale msadpcm coefficients to fit into 8 bits
reimar
parents: 27225
diff changeset
206 (sample2[current_channel] * coeff2[current_channel])) / 64) +
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
207 (snibble * idelta[current_channel]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
208 CLAMP_S16(predictor);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
209 sample2[current_channel] = sample1[current_channel];
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
210 sample1[current_channel] = predictor;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
211 output[out_ptr++] = predictor;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
212
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
213 // compute the next adaptive scale factor (a.k.a. the variable idelta)
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
214 idelta[current_channel] =
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
215 (ms_adapt_table[nibble] * idelta[current_channel]) / 256;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
216 CLAMP_ABOVE_16(idelta[current_channel]);
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
217
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
218 // toggle the channel
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
219 current_channel ^= channels - 1;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
220 }
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
221
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
222 return (block_size - (MS_ADPCM_PREAMBLE_SIZE * channels)) * 2;
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
223 }
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
224
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
225 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
226 {
27228
860c265b08e8 Check length of input buffer for msadpcm
reimar
parents: 27227
diff changeset
227 int res;
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
228 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27238
diff changeset
229 sh_audio->ds->ss_mul) !=
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27238
diff changeset
230 sh_audio->ds->ss_mul)
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
231 return -1; /* EOF */
5350
d59e27f2f5be fixed so the decoder cooperates better with the rest of the system
melanson
parents: 5340
diff changeset
232
27228
860c265b08e8 Check length of input buffer for msadpcm
reimar
parents: 27227
diff changeset
233 res = ms_adpcm_decode_block(
5408
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
234 (unsigned short*)buf, sh_audio->a_in_buffer,
f9cd6381e327 reworked ADPCM decoders; changes include:
melanson
parents: 5350
diff changeset
235 sh_audio->wf->nChannels, sh_audio->wf->nBlockAlign);
27228
860c265b08e8 Check length of input buffer for msadpcm
reimar
parents: 27227
diff changeset
236 return res < 0 ? res : 2 * res;
5340
0f12fb7c1c5d imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff changeset
237 }