annotate adpcm.c @ 1106:1e39f273ecd6 libavcodec

per file doxy
author michaelni
date Thu, 06 Mar 2003 11:32:04 +0000
parents b6442efcdd27
children eff1dc4bed49
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
1 /*
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
2 * ADPCM codecs
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
3 * Copyright (c) 2001 Fabrice Bellard.
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
4 *
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
9 *
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
13 * Lesser General Public License for more details.
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
14 *
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
18 */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
19 #include "avcodec.h"
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
20
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1093
diff changeset
21 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1093
diff changeset
22 * @file adpcm.c
1e39f273ecd6 per file doxy
michaelni
parents: 1093
diff changeset
23 * ADPCM codecs.
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
24 * First version by Francois Revol revol@free.fr
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
25 *
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
26 * Features and limitations:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
27 *
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
28 * Reference documents:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
29 * http://www.pcisys.net/~melanson/codecs/adpcm.txt
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
30 * http://www.geocities.com/SiliconValley/8682/aud3.txt
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
31 * http://openquicktime.sourceforge.net/plugins.htm
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
32 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
33 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
34 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
35 */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
36
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
37 #define BLKSIZE 1024
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
38
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
39 #define CLAMP_TO_SHORT(value) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
40 if (value > 32767) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
41 value = 32767; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
42 else if (value < -32768) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
43 value = -32768; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
44
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
45 /* step_table[] and index_table[] are from the ADPCM reference source */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
46 /* This is the index table: */
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
47 static const int index_table[16] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
48 -1, -1, -1, -1, 2, 4, 6, 8,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
49 -1, -1, -1, -1, 2, 4, 6, 8,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
50 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
51
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1093
diff changeset
52 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1093
diff changeset
53 * This is the step table. Note that many programs use slight deviations from
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
54 * this table, but such deviations are negligible:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
55 */
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
56 static const int step_table[89] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
57 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
58 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
59 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
60 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
61 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
62 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
63 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
64 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
65 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
66 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
67
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
68 /* Those are for MS-ADPCM */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
69 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
70 static const int AdaptationTable[] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
71 230, 230, 230, 230, 307, 409, 512, 614,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
72 768, 614, 512, 409, 307, 230, 230, 230
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
73 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
74
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
75 static const int AdaptCoeff1[] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
76 256, 512, 0, 192, 240, 460, 392
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
77 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
78
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
79 static const int AdaptCoeff2[] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
80 0, -256, 0, 64, 0, -208, -232
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
81 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
82
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
83 /* end of tables */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
84
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
85 typedef struct ADPCMChannelStatus {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
86 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
87 short int step_index;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
88 int step;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
89 /* for encoding */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
90 int prev_sample;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
91
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
92 /* MS version */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
93 short sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
94 short sample2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
95 int coeff1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
96 int coeff2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
97 int idelta;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
98 } ADPCMChannelStatus;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
99
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
100 typedef struct ADPCMContext {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
101 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
102 ADPCMChannelStatus status[2];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
103 short sample_buffer[32]; /* hold left samples while waiting for right samples */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
104 } ADPCMContext;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
105
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
106 /* XXX: implement encoding */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
107
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
108 static int adpcm_encode_init(AVCodecContext *avctx)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
109 {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
110 if (avctx->channels > 2)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
111 return -1; /* only stereo or mono =) */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
112 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
113 case CODEC_ID_ADPCM_IMA_QT:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
114 fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
115 avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
116 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
117 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
118 case CODEC_ID_ADPCM_IMA_WAV:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
119 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
120 /* and we have 4 bytes per channel overhead */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
121 avctx->block_align = BLKSIZE;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
122 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
123 break;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
124 case CODEC_ID_ADPCM_MS:
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
125 fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
126 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
127 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
128 default:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
129 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
130 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
131 }
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
132
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
133 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
134 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
135
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
136 return 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
137 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
138
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
139 static int adpcm_encode_close(AVCodecContext *avctx)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
140 {
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
141 av_freep(&avctx->coded_frame);
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
142
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
143 return 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
144 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
145
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
146
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
147 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
148 {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
149 int step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
150 unsigned char nibble;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
151
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
152 int sign = 0; /* sign bit of the nibble (MSB) */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
153 int delta, predicted_delta;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
154
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
155 delta = sample - c->prev_sample;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
156
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
157 if (delta < 0) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
158 sign = 1;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
159 delta = -delta;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
160 }
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
161
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
162 step_index = c->step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
163
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
164 /* nibble = 4 * delta / step_table[step_index]; */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
165 nibble = (delta << 2) / step_table[step_index];
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
166
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
167 if (nibble > 7)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
168 nibble = 7;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
169
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
170 step_index += index_table[nibble];
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
171 if (step_index < 0)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
172 step_index = 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
173 if (step_index > 88)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
174 step_index = 88;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
175
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
176 /* what the decoder will find */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
177 predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
178
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
179 if (sign)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
180 c->prev_sample -= predicted_delta;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
181 else
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
182 c->prev_sample += predicted_delta;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
183
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
184 CLAMP_TO_SHORT(c->prev_sample);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
185
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
186
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
187 nibble += sign << 3; /* sign * 8 */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
188
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
189 /* save back */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
190 c->step_index = step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
191
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
192 return nibble;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
193 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
194
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
195 static int adpcm_encode_frame(AVCodecContext *avctx,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
196 unsigned char *frame, int buf_size, void *data)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
197 {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
198 int n;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
199 short *samples;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
200 unsigned char *dst;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
201 ADPCMContext *c = avctx->priv_data;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
202
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
203 dst = frame;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
204 samples = (short *)data;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
205 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
206
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
207 switch(avctx->codec->id) {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
208 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
209 break;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
210 case CODEC_ID_ADPCM_IMA_WAV:
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
211 n = avctx->frame_size / 8;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
212 c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
213 /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
214 *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
215 *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
216 *dst++ = (unsigned char)c->status[0].step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
217 *dst++ = 0; /* unknown */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
218 samples++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
219 if (avctx->channels == 2) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
220 c->status[1].prev_sample = (signed short)samples[0];
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
221 /* c->status[1].step_index = 0; */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
222 *dst++ = (c->status[1].prev_sample) & 0xFF;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
223 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
224 *dst++ = (unsigned char)c->status[1].step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
225 *dst++ = 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
226 samples++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
227 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
228
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
229 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
230 for (; n>0; n--) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
231 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
232 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
233 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
234 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
235 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
236 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
237 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
238 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
239 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
240 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
241 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
242 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
243 /* right channel */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
244 if (avctx->channels == 2) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
245 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
246 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
247 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
248 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
249 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
250 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
251 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
252 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
253 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
254 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
255 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
256 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
257 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
258 samples += 8 * avctx->channels;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
259 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
260 break;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
261 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
262 return -1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
263 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
264 return dst - frame;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
265 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
266
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
267 static int adpcm_decode_init(AVCodecContext * avctx)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
268 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
269 ADPCMContext *c = avctx->priv_data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
270
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
271 c->channel = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
272 c->status[0].predictor = c->status[1].predictor = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
273 c->status[0].step_index = c->status[1].step_index = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
274 c->status[0].step = c->status[1].step = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
275
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
276 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
277 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
278 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
279 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
280 return 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
281 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
282
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
283 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
284 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
285 int step_index;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
286 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
287 int sign, delta, diff, step;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
288
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
289 step = step_table[c->step_index];
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
290 step_index = c->step_index + index_table[(unsigned)nibble];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
291 if (step_index < 0) step_index = 0;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
292 else if (step_index > 88) step_index = 88;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
293
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
294 sign = nibble & 8;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
295 delta = nibble & 7;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
296 #if 0
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
297 diff = step >> 3;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
298 if (delta & 4) diff += step;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
299 if (delta & 2) diff += step >> 1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
300 if (delta & 1) diff += step >> 2;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
301 #else
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
302 diff = ((2 * delta + 1) * step) >> 3; // no jumps
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
303 #endif
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
304 predictor = c->predictor;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
305 if (sign) predictor -= diff;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
306 else predictor += diff;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
307
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
308 CLAMP_TO_SHORT(predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
309 c->predictor = predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
310 c->step_index = step_index;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
311
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
312 return (short)predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
313 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
314
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
315 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
316 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
317 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
318
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
319 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
320 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
321 CLAMP_TO_SHORT(predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
322
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
323 c->sample2 = c->sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
324 c->sample1 = predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
325 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
326 if (c->idelta < 16) c->idelta = 16;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
327
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
328 return (short)predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
329 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
330
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
331 static int adpcm_decode_frame(AVCodecContext *avctx,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
332 void *data, int *data_size,
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 925
diff changeset
333 uint8_t *buf, int buf_size)
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
334 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
335 ADPCMContext *c = avctx->priv_data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
336 ADPCMChannelStatus *cs;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
337 int n, m, channel;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
338 int block_predictor[2];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
339 short *samples;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 925
diff changeset
340 uint8_t *src;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
341 int st; /* stereo */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
342
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
343 samples = data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
344 src = buf;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
345
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
346 st = avctx->channels == 2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
347
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
348 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
349 case CODEC_ID_ADPCM_IMA_QT:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
350 n = (buf_size - 2);/* >> 2*avctx->channels;*/
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
351 channel = c->channel;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
352 cs = &(c->status[channel]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
353 /* (pppppp) (piiiiiii) */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
354
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
355 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
356 cs->predictor = (*src++) << 8;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
357 cs->predictor |= (*src & 0x80);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
358 cs->predictor &= 0xFF80;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
359
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
360 /* sign extension */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
361 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
362 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
363
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
364 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
365
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
366 cs->step_index = (*src++) & 0x7F;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
367
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
368 if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
369 if (cs->step_index > 88) cs->step_index = 88;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
370
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
371 cs->step = step_table[cs->step_index];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
372
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
373 if (st && channel)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
374 samples++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
375
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
376 *samples++ = cs->predictor;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
377 samples += st;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
378
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
379 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
380 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
381 samples += avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
382 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
383 samples += avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
384 src ++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
385 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
386
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
387 if(st) { /* handle stereo interlacing */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
388 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
389 if(channel == 0) { /* wait for the other packet before outputing anything */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
390 *data_size = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
391 return src - buf;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
392 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
393 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
394 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
395 case CODEC_ID_ADPCM_IMA_WAV:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
396 if (buf_size > BLKSIZE) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
397 if (avctx->block_align != 0)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
398 buf_size = avctx->block_align;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
399 else
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
400 buf_size = BLKSIZE;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
401 }
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
402 // XXX: do as per-channel loop
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
403 cs = &(c->status[0]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
404 cs->predictor = (*src++) & 0x0FF;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
405 cs->predictor |= ((*src++) << 8) & 0x0FF00;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
406 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
407 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
408 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
409
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
410 // XXX: is this correct ??: *samples++ = cs->predictor;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
411
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
412 cs->step_index = *src++;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
413 if (cs->step_index < 0) cs->step_index = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
414 if (cs->step_index > 88) cs->step_index = 88;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
415 if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
416
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
417 if (st) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
418 cs = &(c->status[1]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
419 cs->predictor = (*src++) & 0x0FF;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
420 cs->predictor |= ((*src++) << 8) & 0x0FF00;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
421 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
422 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
423 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
424
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
425 // XXX: is this correct ??: *samples++ = cs->predictor;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
426
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
427 cs->step_index = *src++;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
428 if (cs->step_index < 0) cs->step_index = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
429 if (cs->step_index > 88) cs->step_index = 88;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
430 src++; /* if != 0 -> out-of-sync */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
431 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
432
1093
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
433 for(m=4; src < (buf + buf_size);) {
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
434 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
435 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
436 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
437 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
1093
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
438 if (st) {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
439 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
1093
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
440 if (!--m) {
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
441 m=4;
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
442 src+=4;
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
443 }
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
444 }
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
445 src++;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
446 }
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
447 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
448 case CODEC_ID_ADPCM_MS:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
449
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
450 if (buf_size > BLKSIZE) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
451 if (avctx->block_align != 0)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
452 buf_size = avctx->block_align;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
453 else
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
454 buf_size = BLKSIZE;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
455 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
456 n = buf_size - 7 * avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
457 if (n < 0)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
458 return -1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
459 block_predictor[0] = (*src++); /* should be bound */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
460 block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
461 block_predictor[1] = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
462 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
463 block_predictor[1] = (*src++);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
464 block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
465 c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
466 if (c->status[0].idelta & 0x08000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
467 c->status[0].idelta -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
468 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
469 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
470 c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
471 if (st && c->status[1].idelta & 0x08000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
472 c->status[1].idelta |= 0xFFFF0000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
473 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
474 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
475 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
476 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
477 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
478 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
479
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
480 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
481 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
482 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
483 if (st) src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
484 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
485 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
486 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
487 if (st) src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
488
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
489 *samples++ = c->status[0].sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
490 if (st) *samples++ = c->status[1].sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
491 *samples++ = c->status[0].sample2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
492 if (st) *samples++ = c->status[1].sample2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
493 for(;n>0;n--) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
494 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
495 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
496 src ++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
497 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
498 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
499 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
500 *data_size = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
501 return -1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
502 }
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 925
diff changeset
503 *data_size = (uint8_t *)samples - (uint8_t *)data;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
504 return src - buf;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
505 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
506
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
507 #define ADPCM_CODEC(id, name) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
508 AVCodec name ## _encoder = { \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
509 #name, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
510 CODEC_TYPE_AUDIO, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
511 id, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
512 sizeof(ADPCMContext), \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
513 adpcm_encode_init, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
514 adpcm_encode_frame, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
515 adpcm_encode_close, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
516 NULL, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
517 }; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
518 AVCodec name ## _decoder = { \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
519 #name, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
520 CODEC_TYPE_AUDIO, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
521 id, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
522 sizeof(ADPCMContext), \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
523 adpcm_decode_init, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
524 NULL, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
525 NULL, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
526 adpcm_decode_frame, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
527 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
528
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
529 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
530 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
531 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
532
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
533 #undef ADPCM_CODEC
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
534