annotate adpcm.c @ 1447:7c73537bc994 libavcodec

changed my mind about CODEC_ID_MPEGVIDEO - CODEC_ID_MP3LAME is absolete
author bellard
date Mon, 08 Sep 2003 22:03:29 +0000
parents 47f4c8a5a7fc
children 222643544cf1
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
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
3 * Copyright (c) 2001-2003 The ffmpeg Project
573
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
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
25 * Fringe ADPCM codecs (e.g., DK3 and DK4)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
26 * by Mike Melanson (melanson@pcisys.net)
573
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 * Features and limitations:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
29 *
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
30 * Reference documents:
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
31 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
32 * 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
33 * http://openquicktime.sourceforge.net/plugins.htm
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
34 * 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
35 * 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
36 * 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
37 */
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 BLKSIZE 1024
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
40
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
41 #define CLAMP_TO_SHORT(value) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
42 if (value > 32767) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
43 value = 32767; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
44 else if (value < -32768) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
45 value = -32768; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
46
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
47 /* 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
48 /* This is the index table: */
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
49 static const int index_table[16] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
50 -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
51 -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
52 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
53
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1093
diff changeset
54 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1093
diff changeset
55 * 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
56 * this table, but such deviations are negligible:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
57 */
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
58 static const int step_table[89] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
69
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
70 /* Those are for MS-ADPCM */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
71 /* 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
72 static const int AdaptationTable[] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
73 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
74 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
75 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
76
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
77 static const int AdaptCoeff1[] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
78 256, 512, 0, 192, 240, 460, 392
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
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
81 static const int AdaptCoeff2[] = {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
82 0, -256, 0, 64, 0, -208, -232
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
83 };
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 /* end of tables */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
86
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
87 typedef struct ADPCMChannelStatus {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
88 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
89 short int step_index;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
90 int step;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
91 /* for encoding */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
92 int prev_sample;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
93
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
94 /* MS version */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
95 short sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
96 short sample2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
97 int coeff1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
98 int coeff2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
99 int idelta;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
100 } ADPCMChannelStatus;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
101
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
102 typedef struct ADPCMContext {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
103 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
104 ADPCMChannelStatus status[2];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
105 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
106 } ADPCMContext;
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 /* XXX: implement encoding */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
109
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
110 static int adpcm_encode_init(AVCodecContext *avctx)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
111 {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
112 if (avctx->channels > 2)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
113 return -1; /* only stereo or mono =) */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
114 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
115 case CODEC_ID_ADPCM_IMA_QT:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
116 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
117 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
118 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
119 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
120 case CODEC_ID_ADPCM_IMA_WAV:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
121 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
122 /* 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
123 avctx->block_align = BLKSIZE;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
124 /* 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
125 break;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
126 case CODEC_ID_ADPCM_MS:
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
127 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
128 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
129 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
130 default:
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
131 return -1;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
132 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
133 }
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
134
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
135 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
136 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
137
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
138 return 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
139 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
140
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
141 static int adpcm_encode_close(AVCodecContext *avctx)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
142 {
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
143 av_freep(&avctx->coded_frame);
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 577
diff changeset
144
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
145 return 0;
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
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 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
150 {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
151 int step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
152 unsigned char nibble;
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 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
155 int delta, predicted_delta;
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 delta = sample - c->prev_sample;
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 if (delta < 0) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
160 sign = 1;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
161 delta = -delta;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
162 }
577
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 step_index = c->step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
165
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
166 /* nibble = 4 * delta / step_table[step_index]; */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
167 nibble = (delta << 2) / step_table[step_index];
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
168
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
169 if (nibble > 7)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
170 nibble = 7;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
171
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
172 step_index += index_table[nibble];
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
173 if (step_index < 0)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
174 step_index = 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
175 if (step_index > 88)
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
176 step_index = 88;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
177
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
178 /* what the decoder will find */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
179 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
180
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
181 if (sign)
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 else
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
184 c->prev_sample += predicted_delta;
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 CLAMP_TO_SHORT(c->prev_sample);
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
187
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 nibble += sign << 3; /* sign * 8 */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
190
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
191 /* save back */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
192 c->step_index = step_index;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
193
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
194 return nibble;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
195 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
196
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
197 static int adpcm_encode_frame(AVCodecContext *avctx,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
198 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
199 {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
200 int n;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
201 short *samples;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
202 unsigned char *dst;
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
203 ADPCMContext *c = avctx->priv_data;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
204
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
205 dst = frame;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
206 samples = (short *)data;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
207 /* 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
208
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
209 switch(avctx->codec->id) {
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
210 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
211 break;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
212 case CODEC_ID_ADPCM_IMA_WAV:
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
213 n = avctx->frame_size / 8;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
214 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
215 /* 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
216 *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
217 *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
218 *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
219 *dst++ = 0; /* unknown */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
220 samples++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
221 if (avctx->channels == 2) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
222 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
223 /* c->status[1].step_index = 0; */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
224 *dst++ = (c->status[1].prev_sample) & 0xFF;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
225 *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
226 *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
227 *dst++ = 0;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
228 samples++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
229 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
230
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
231 /* 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
232 for (; n>0; n--) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
233 *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
234 *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
235 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
236 *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
237 *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
238 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
239 *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
240 *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
241 dst++;
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[0], samples[avctx->channels * 6]) & 0x0F;
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[0], samples[avctx->channels * 7]) << 4) & 0xF0;
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 /* right channel */
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
246 if (avctx->channels == 2) {
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
247 *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
248 *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
249 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
250 *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
251 *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
252 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
253 *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
254 *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
255 dst++;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
256 *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
257 *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
258 dst++;
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 samples += 8 * avctx->channels;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
261 }
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
262 break;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
263 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
264 return -1;
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 return dst - frame;
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
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
269 static int adpcm_decode_init(AVCodecContext * avctx)
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 ADPCMContext *c = avctx->priv_data;
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 c->channel = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
274 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
275 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
276 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
277
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
278 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
279 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
280 break;
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 return 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
283 }
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 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
286 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
287 int step_index;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
288 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
289 int sign, delta, diff, step;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
290
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
291 step = step_table[c->step_index];
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
292 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
293 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
294 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
295
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
296 sign = nibble & 8;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
297 delta = nibble & 7;
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
298 /* perform direct multiplication instead of series of jumps proposed by
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
299 * the reference ADPCM implementation since modern CPUs can do the mults
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
300 * quickly enough */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
301 diff = ((2 * delta + 1) * step) >> 3;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
302 predictor = c->predictor;
573
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;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
309
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
310 return (short)predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
311 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
312
1296
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
313 static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble)
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
314 {
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
315 int step_index;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
316 int predictor;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
317 int sign, delta, diff, step;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
318
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
319 step = step_table[c->step_index];
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
320 step_index = c->step_index + index_table[(unsigned)nibble];
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
321 if (step_index < 0) step_index = 0;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
322 else if (step_index > 88) step_index = 88;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
323
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
324 sign = nibble & 8;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
325 delta = nibble & 7;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
326
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
327 diff = (delta*step + (step>>1))>>3; // difference to code above
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
328
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
329 predictor = c->predictor;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
330 if (sign) predictor -= diff;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
331 else predictor += diff;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
332
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
333 CLAMP_TO_SHORT(predictor);
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
334 c->predictor = predictor;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
335 c->step_index = step_index;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
336
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
337 return (short)predictor;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
338 }
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
339
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
340 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
341 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
342 int predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
343
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
344 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
345 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
346 CLAMP_TO_SHORT(predictor);
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 c->sample2 = c->sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
349 c->sample1 = predictor;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
350 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
351 if (c->idelta < 16) c->idelta = 16;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
352
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
353 return (short)predictor;
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
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
356 /* DK3 ADPCM support macro */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
357 #define DK3_GET_NEXT_NIBBLE() \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
358 if (decode_top_nibble_next) \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
359 { \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
360 nibble = (last_byte >> 4) & 0x0F; \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
361 decode_top_nibble_next = 0; \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
362 } \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
363 else \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
364 { \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
365 last_byte = *src++; \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
366 if (src >= buf + buf_size) break; \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
367 nibble = last_byte & 0x0F; \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
368 decode_top_nibble_next = 1; \
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
369 }
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
370
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
371 static int adpcm_decode_frame(AVCodecContext *avctx,
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
372 void *data, int *data_size,
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 925
diff changeset
373 uint8_t *buf, int buf_size)
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
374 {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
375 ADPCMContext *c = avctx->priv_data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
376 ADPCMChannelStatus *cs;
1296
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
377 int n, m, channel, i;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
378 int block_predictor[2];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
379 short *samples;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 925
diff changeset
380 uint8_t *src;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
381 int st; /* stereo */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
382
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
383 /* DK3 ADPCM accounting variables */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
384 unsigned char last_byte = 0;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
385 unsigned char nibble;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
386 int decode_top_nibble_next = 0;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
387 int diff_channel;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
388
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
389 samples = data;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
390 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 st = avctx->channels == 2;
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 switch(avctx->codec->id) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
395 case CODEC_ID_ADPCM_IMA_QT:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
396 n = (buf_size - 2);/* >> 2*avctx->channels;*/
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
397 channel = c->channel;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
398 cs = &(c->status[channel]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
399 /* (pppppp) (piiiiiii) */
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 /* 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
402 cs->predictor = (*src++) << 8;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
403 cs->predictor |= (*src & 0x80);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
404 cs->predictor &= 0xFF80;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
405
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
406 /* sign extension */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
407 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
408 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
409
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
410 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
411
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
412 cs->step_index = (*src++) & 0x7F;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
413
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
414 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
415 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
416
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
417 cs->step = step_table[cs->step_index];
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
418
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
419 if (st && channel)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
420 samples++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
421
577
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
422 *samples++ = cs->predictor;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
423 samples += st;
babaca0899f1 adpcm encoding patch by Franois Revol <revol at free dot fr>
michaelni
parents: 573
diff changeset
424
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
425 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
426 *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
427 samples += avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
428 *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
429 samples += avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
430 src ++;
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
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
433 if(st) { /* handle stereo interlacing */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
434 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
435 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
436 *data_size = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
437 return src - buf;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
438 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
439 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
440 break;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
441 case CODEC_ID_ADPCM_IMA_WAV:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
442 if (buf_size > BLKSIZE) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
443 if (avctx->block_align != 0)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
444 buf_size = avctx->block_align;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
445 else
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
446 buf_size = BLKSIZE;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
447 }
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
448 // XXX: do as per-channel loop
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
449 cs = &(c->status[0]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
450 cs->predictor = (*src++) & 0x0FF;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
451 cs->predictor |= ((*src++) << 8) & 0x0FF00;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
452 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
453 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
454 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
455
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
456 // 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
457
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
458 cs->step_index = *src++;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
459 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
460 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
461 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
462
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
463 if (st) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
464 cs = &(c->status[1]);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
465 cs->predictor = (*src++) & 0x0FF;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
466 cs->predictor |= ((*src++) << 8) & 0x0FF00;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
467 if(cs->predictor & 0x8000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
468 cs->predictor -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
469 CLAMP_TO_SHORT(cs->predictor);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
470
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
471 // 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
472
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
473 cs->step_index = *src++;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
474 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
475 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
476 src++; /* if != 0 -> out-of-sync */
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
477 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
478
1093
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
479 for(m=4; src < (buf + buf_size);) {
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
480 *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
481 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
482 *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
483 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
1093
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
484 if (st) {
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
485 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
1093
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
486 if (!--m) {
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
487 m=4;
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
488 src+=4;
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
489 }
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
490 }
b6442efcdd27 * minor optimalization
kabi
parents: 1081
diff changeset
491 src++;
1081
18157900b434 * fixed IMA WAV decoding (though still might not be fully correct)
kabi
parents: 1064
diff changeset
492 }
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
493 break;
1296
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
494 case CODEC_ID_ADPCM_4XM:
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
495 cs = &(c->status[0]);
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
496 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
497 if(st){
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
498 c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
499 }
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
500 c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
501 if(st){
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
502 c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
503 }
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
504 // if (cs->step_index < 0) cs->step_index = 0;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
505 // if (cs->step_index > 88) cs->step_index = 88;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
506
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
507 m= (buf_size - (src - buf))>>st;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
508 //printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
509 //FIXME / XXX decode chanels individual & interleave samples
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
510 for(i=0; i<m; i++) {
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
511 *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
512 if (st)
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
513 *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
514 *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
515 if (st)
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
516 *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
517 }
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
518
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
519 src += m<<st;
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
520
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
521 break;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
522 case CODEC_ID_ADPCM_MS:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
523
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
524 if (buf_size > BLKSIZE) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
525 if (avctx->block_align != 0)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
526 buf_size = avctx->block_align;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
527 else
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
528 buf_size = BLKSIZE;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
529 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
530 n = buf_size - 7 * avctx->channels;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
531 if (n < 0)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
532 return -1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
533 block_predictor[0] = (*src++); /* should be bound */
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
534 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
535 block_predictor[1] = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
536 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
537 block_predictor[1] = (*src++);
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
538 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
539 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
540 if (c->status[0].idelta & 0x08000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
541 c->status[0].idelta -= 0x10000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
542 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
543 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
544 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
545 if (st && c->status[1].idelta & 0x08000)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
546 c->status[1].idelta |= 0xFFFF0000;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
547 if (st)
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
548 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
549 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
550 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
551 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
552 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
553
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
554 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
555 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
556 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
557 if (st) src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
558 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
559 src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
560 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
561 if (st) src+=2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
562
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
563 *samples++ = c->status[0].sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
564 if (st) *samples++ = c->status[1].sample1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
565 *samples++ = c->status[0].sample2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
566 if (st) *samples++ = c->status[1].sample2;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
567 for(;n>0;n--) {
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
568 *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
569 *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
570 src ++;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
571 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
572 break;
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
573 case CODEC_ID_ADPCM_IMA_DK4:
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
574 if (buf_size > BLKSIZE) {
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
575 if (avctx->block_align != 0)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
576 buf_size = avctx->block_align;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
577 else
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
578 buf_size = BLKSIZE;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
579 }
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
580 c->status[0].predictor = (src[0] | (src[1] << 8));
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
581 c->status[0].step_index = src[2];
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
582 src += 4;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
583 if(c->status[0].predictor & 0x8000)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
584 c->status[0].predictor -= 0x10000;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
585 *samples++ = c->status[0].predictor;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
586 if (st) {
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
587 c->status[1].predictor = (src[0] | (src[1] << 8));
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
588 c->status[1].step_index = src[2];
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
589 src += 4;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
590 if(c->status[1].predictor & 0x8000)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
591 c->status[1].predictor -= 0x10000;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
592 *samples++ = c->status[1].predictor;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
593 }
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
594 while (src < buf + buf_size) {
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
595
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
596 /* take care of the top nibble (always left or mono channel) */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
597 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
598 (src[0] >> 4) & 0x0F);
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
599
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
600 /* take care of the bottom nibble, which is right sample for
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
601 * stereo, or another mono sample */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
602 if (st)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
603 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
604 src[0] & 0x0F);
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
605 else
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
606 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
607 src[0] & 0x0F);
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
608
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
609 src++;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
610 }
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
611 break;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
612 case CODEC_ID_ADPCM_IMA_DK3:
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
613 if (buf_size > BLKSIZE) {
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
614 if (avctx->block_align != 0)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
615 buf_size = avctx->block_align;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
616 else
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
617 buf_size = BLKSIZE;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
618 }
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
619 c->status[0].predictor = (src[10] | (src[11] << 8));
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
620 c->status[1].predictor = (src[12] | (src[13] << 8));
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
621 c->status[0].step_index = src[14];
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
622 c->status[1].step_index = src[15];
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
623 /* sign extend the predictors */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
624 if(c->status[0].predictor & 0x8000)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
625 c->status[0].predictor -= 0x10000;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
626 if(c->status[1].predictor & 0x8000)
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
627 c->status[1].predictor -= 0x10000;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
628 src += 16;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
629 diff_channel = c->status[1].predictor;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
630
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
631 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
632 * the buffer is consumed */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
633 while (1) {
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
634
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
635 /* for this algorithm, c->status[0] is the sum channel and
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
636 * c->status[1] is the diff channel */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
637
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
638 /* process the first predictor of the sum channel */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
639 DK3_GET_NEXT_NIBBLE();
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
640 adpcm_ima_expand_nibble(&c->status[0], nibble);
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
641
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
642 /* process the diff channel predictor */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
643 DK3_GET_NEXT_NIBBLE();
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
644 adpcm_ima_expand_nibble(&c->status[1], nibble);
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
645
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
646 /* process the first pair of stereo PCM samples */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
647 diff_channel = (diff_channel + c->status[1].predictor) / 2;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
648 *samples++ = c->status[0].predictor + c->status[1].predictor;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
649 *samples++ = c->status[0].predictor - c->status[1].predictor;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
650
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
651 /* process the second predictor of the sum channel */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
652 DK3_GET_NEXT_NIBBLE();
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
653 adpcm_ima_expand_nibble(&c->status[0], nibble);
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
654
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
655 /* process the second pair of stereo PCM samples */
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
656 diff_channel = (diff_channel + c->status[1].predictor) / 2;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
657 *samples++ = c->status[0].predictor + c->status[1].predictor;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
658 *samples++ = c->status[0].predictor - c->status[1].predictor;
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
659 }
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
660 break;
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
661 default:
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
662 *data_size = 0;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
663 return -1;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
664 }
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 925
diff changeset
665 *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
666 return src - buf;
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
667 }
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
668
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
669 #define ADPCM_CODEC(id, name) \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
670 AVCodec name ## _encoder = { \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
671 #name, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
672 CODEC_TYPE_AUDIO, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
673 id, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
674 sizeof(ADPCMContext), \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
675 adpcm_encode_init, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
676 adpcm_encode_frame, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
677 adpcm_encode_close, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
678 NULL, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
679 }; \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
680 AVCodec name ## _decoder = { \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
681 #name, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
682 CODEC_TYPE_AUDIO, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
683 id, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
684 sizeof(ADPCMContext), \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
685 adpcm_decode_init, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
686 NULL, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
687 NULL, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
688 adpcm_decode_frame, \
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
689 };
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
690
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
691 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
692 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1443
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
693 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
47f4c8a5a7fc New fringe codecs: WC3/Xan video, Xan DPCM, DK3 & DK4 ADPCM
tmmm
parents: 1296
diff changeset
694 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
695 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1296
eff1dc4bed49 4xm adpcm
michaelni
parents: 1106
diff changeset
696 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
573
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
697
b0f52172f4c5 beos/mov/adpcm patch by Franois Revol <revol at free dot fr>
michaelni
parents:
diff changeset
698 #undef ADPCM_CODEC