annotate adpcm.c @ 988:001b7d3045e5 libavcodec

moved avcodec_get_chroma_sub_sample() to imgconvert.c
author bellard
date Sat, 11 Jan 2003 00:07:06 +0000
parents 7fccaa0d699d
children b32afefe7d33
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
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
21 /*
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
22 * 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
23 *
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
24 * Features and limitations:
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 * Reference documents:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
27 * 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
28 * 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
29 * http://openquicktime.sourceforge.net/plugins.htm
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
30 * 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
31 * 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
32 * 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
33 */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
34
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
35 #define BLKSIZE 1024
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 CLAMP_TO_SHORT(value) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
38 if (value > 32767) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
39 value = 32767; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
40 else if (value < -32768) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
41 value = -32768; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
42
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
43 /* 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
44 /* This is the index table: */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
45 static int index_table[16] = {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
46 -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
47 -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
48 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
49
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
50 /* This is the step table. Note that many programs use slight deviations from
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
51 * this table, but such deviations are negligible:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
52 */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
53 static int step_table[89] = {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
64
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
65 /* Those are for MS-ADPCM */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
66 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
67 static int AdaptationTable[] = {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
68 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
69 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
70 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
71
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
72 static int AdaptCoeff1[] = {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
73 256, 512, 0, 192, 240, 460, 392
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
74 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
75
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
76 static int AdaptCoeff2[] = {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
77 0, -256, 0, 64, 0, -208, -232
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
78 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
79
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
80 /* end of tables */
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 typedef struct ADPCMChannelStatus {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
83 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
84 short int step_index;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
85 int step;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
86 /* for encoding */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
87 int prev_sample;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
88
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
89 /* MS version */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
90 short sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
91 short sample2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
92 int coeff1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
93 int coeff2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
94 int idelta;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
95 } ADPCMChannelStatus;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
96
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
97 typedef struct ADPCMContext {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
98 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
99 ADPCMChannelStatus status[2];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
100 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
101 } ADPCMContext;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
102
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
103 /* XXX: implement encoding */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
104
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
105 static int adpcm_encode_init(AVCodecContext *avctx)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
106 {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
107 if (avctx->channels > 2)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
108 return -1; /* only stereo or mono =) */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
109 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
110 case CODEC_ID_ADPCM_IMA_QT:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
111 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
112 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
113 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
114 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
115 case CODEC_ID_ADPCM_IMA_WAV:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
116 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
117 /* 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
118 avctx->block_align = BLKSIZE;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
119 /* 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
120 break;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
121 case CODEC_ID_ADPCM_MS:
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
122 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
123 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
124 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
125 default:
577
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 }
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
129
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
130 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
131 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
132
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
133 return 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
134 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
135
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
136 static int adpcm_encode_close(AVCodecContext *avctx)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
137 {
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
138 av_freep(&avctx->coded_frame);
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
139
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
140 return 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
141 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
142
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
143
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
144 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
145 {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
146 int step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
147 unsigned char nibble;
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 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
150 int delta, predicted_delta;
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 delta = sample - c->prev_sample;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
153
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
154 if (delta < 0) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
155 sign = 1;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
156 delta = -delta;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
157 }
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
158
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
159 step_index = c->step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
160
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
161 /* nibble = 4 * delta / step_table[step_index]; */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
162 nibble = (delta << 2) / step_table[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 if (nibble > 7)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
165 nibble = 7;
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 step_index += index_table[nibble];
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
168 if (step_index < 0)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
169 step_index = 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
170 if (step_index > 88)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
171 step_index = 88;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
172
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
173 /* what the decoder will find */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
174 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
175
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
176 if (sign)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
177 c->prev_sample -= predicted_delta;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
178 else
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
179 c->prev_sample += predicted_delta;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
180
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
181 CLAMP_TO_SHORT(c->prev_sample);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
182
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 nibble += sign << 3; /* sign * 8 */
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 /* save back */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
187 c->step_index = step_index;
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 return nibble;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
190 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
191
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
192 static int adpcm_encode_frame(AVCodecContext *avctx,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
193 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
194 {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
195 int n;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
196 short *samples;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
197 unsigned char *dst;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
198 ADPCMContext *c = avctx->priv_data;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
199
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
200 dst = frame;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
201 samples = (short *)data;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
202 /* 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
203
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
204 switch(avctx->codec->id) {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
205 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
206 break;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
207 case CODEC_ID_ADPCM_IMA_WAV:
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
208 n = avctx->frame_size / 8;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
209 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
210 /* 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
211 *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
212 *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
213 *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
214 *dst++ = 0; /* unknown */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
215 samples++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
216 if (avctx->channels == 2) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
217 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
218 /* c->status[1].step_index = 0; */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
219 *dst++ = (c->status[1].prev_sample) & 0xFF;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
220 *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
221 *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
222 *dst++ = 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
223 samples++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
224 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
225
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
226 /* 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
227 for (; n>0; n--) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
228 *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
229 *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
230 dst++;
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[avctx->channels * 2]) & 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 * 3]) << 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 * 4]) & 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 * 5]) << 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 * 6]) & 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 * 7]) << 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 /* right channel */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
241 if (avctx->channels == 2) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
242 *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
243 *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
244 dst++;
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[5]);
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[7]) << 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[9]);
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[11]) << 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[13]);
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[15]) << 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 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
255 samples += 8 * avctx->channels;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
256 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
257 break;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
258 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
259 return -1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
260 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
261 return dst - frame;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
262 }
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 static int adpcm_decode_init(AVCodecContext * avctx)
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 ADPCMContext *c = avctx->priv_data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
267
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
268 c->channel = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
269 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
270 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
271 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
272
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
273 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
274 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
275 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
276 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
277 return 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
278 }
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 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
281 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
282 int step_index;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
283 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
284 int sign, delta, diff, step;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
285
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
286 predictor = c->predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
287 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
288 if (step_index < 0) step_index = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
289 if (step_index > 88) step_index = 88;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
290
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
291 step = c->step;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
292
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 diff = ((signed)((nibble & 0x08)?(nibble | 0xF0):(nibble)) + 0.5) * step / 4;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
295 predictor += diff;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
296 */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
297 sign = nibble & 8;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
298 delta = nibble & 7;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
299 diff = step >> 3;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
300 if (delta & 4) diff += step;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
301 if (delta & 2) diff += step >> 1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
302 if (delta & 1) diff += step >> 2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
303 if (sign) predictor -= diff;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
304 else predictor += diff;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
305
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
306 CLAMP_TO_SHORT(predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
307 c->predictor = predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
308 c->step_index = step_index;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
309 c->step = step_table[step_index];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
310
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
311 return (short)predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
312 }
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 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
315 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
316 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
317
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
318 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
319 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
320 CLAMP_TO_SHORT(predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
321
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
322 c->sample2 = c->sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
323 c->sample1 = predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
324 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
325 if (c->idelta < 16) c->idelta = 16;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
326
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
327 return (short)predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
328 }
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 static int adpcm_decode_frame(AVCodecContext *avctx,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
331 void *data, int *data_size,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
332 UINT8 *buf, int buf_size)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
333 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
334 ADPCMContext *c = avctx->priv_data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
335 ADPCMChannelStatus *cs;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
336 int n, m, channel;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
337 int block_predictor[2];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
338 short *samples;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
339 UINT8 *src;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
340 int st; /* stereo */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
341
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
342 samples = data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
343 src = buf;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
344
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
345 st = avctx->channels == 2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
346
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
347 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
348 case CODEC_ID_ADPCM_IMA_QT:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
349 n = (buf_size - 2);/* >> 2*avctx->channels;*/
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
350 channel = c->channel;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
351 cs = &(c->status[channel]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
352 /* (pppppp) (piiiiiii) */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
353
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
354 /* 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
355 cs->predictor = (*src++) << 8;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
356 cs->predictor |= (*src & 0x80);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
357 cs->predictor &= 0xFF80;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
358
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
359 /* sign extension */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
360 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
361 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
362
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
363 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
364
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
365 cs->step_index = (*src++) & 0x7F;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
366
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
367 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
368 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
369
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
370 cs->step = step_table[cs->step_index];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
371
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
372 if (st && channel)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
373 samples++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
374
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
375 *samples++ = cs->predictor;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
376 samples += st;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
377
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
378 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
379 *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
380 samples += avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
381 *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
382 samples += avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
383 src ++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
384 }
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 if(st) { /* handle stereo interlacing */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
387 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
388 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
389 *data_size = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
390 return src - buf;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
391 }
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 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
394 case CODEC_ID_ADPCM_IMA_WAV:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
395 if (buf_size > BLKSIZE) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
396 if (avctx->block_align != 0)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
397 buf_size = avctx->block_align;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
398 else
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
399 buf_size = BLKSIZE;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
400 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
401 n = buf_size - 4 * avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
402 cs = &(c->status[0]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
403 cs->predictor = (*src++) & 0x0FF;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
404 cs->predictor |= ((*src++) << 8) & 0x0FF00;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
405 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
406 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
407 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
408
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
409 *samples++ = cs->predictor;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
410
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
411 cs->step_index = *src++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
412 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
413 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
414 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
415
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
416 if (st) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
417 cs = &(c->status[1]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
418 cs->predictor = (*src++) & 0x0FF;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
419 cs->predictor |= ((*src++) << 8) & 0x0FF00;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
420 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
421 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
422 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
423
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
424 *samples++ = cs->predictor;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
425
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
426 cs->step_index = *src++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
427 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
428 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
429 src++; /* unused */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
430 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
431 cs = &(c->status[0]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
432
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
433
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
434 for(m=3; n>0; n--, m--) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
435 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
436 if (st)
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[1], src[4] & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
438 *samples++ = adpcm_ima_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
439 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
440 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
441 src ++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
442 if (st && !m) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
443 m=3;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
444 src+=4;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
445 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
446 }
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 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
503 *data_size = (UINT8 *)samples - (UINT8 *)data;
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