annotate ws-snd1.c @ 12478:5942c42c44e5 libavcodec

Add G.722 ADPCM audio decoder
author mstorsjo
date Thu, 09 Sep 2010 19:21:16 +0000
parents 7915312dd862
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
1 /*
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
2 * Westwood SNDx codecs
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
3 * Copyright (c) 2005 Konstantin Shishkov
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
15 * Lesser General Public License for more details.
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
16 *
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
20 */
8573
2acf0ae7b041 Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents: 7451
diff changeset
21
12434
7915312dd862 Include stdint.h instead of inttypes.h, it is enough for what this file need.
vitor
parents: 12433
diff changeset
22 #include <stdint.h>
8573
2acf0ae7b041 Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents: 7451
diff changeset
23 #include "libavutil/intreadwrite.h"
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
24 #include "avcodec.h"
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
25
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
26 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
27 * @file
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
28 * Westwood SNDx codecs.
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
29 *
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
30 * Reference documents about VQA format and its audio codecs
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
31 * can be found here:
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
32 * http://www.multimedia.cx
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
33 */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
34
12433
46724a37cc2d Hopefully fix the fate-ws_snd breakage on PPC
vitor
parents: 11644
diff changeset
35 static const int8_t ws_adpcm_2bit[] = { -2, -1, 0, 1};
46724a37cc2d Hopefully fix the fate-ws_snd breakage on PPC
vitor
parents: 11644
diff changeset
36 static const int8_t ws_adpcm_4bit[] = {
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
37 -9, -8, -6, -5, -4, -3, -2, -1,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
38 0, 1, 2, 3, 4, 5, 6, 8 };
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
39
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
40 #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
41
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6299
diff changeset
42 static av_cold int ws_snd_decode_init(AVCodecContext * avctx)
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
43 {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
44 // WSSNDContext *c = avctx->priv_data;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2585
diff changeset
45
7451
85ab7655ad4d Modify all codecs to report their supported input and output sample format(s).
pross
parents: 7040
diff changeset
46 avctx->sample_fmt = SAMPLE_FMT_S16;
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
47 return 0;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
48 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
49
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
50 static int ws_snd_decode_frame(AVCodecContext *avctx,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
51 void *data, int *data_size,
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
52 AVPacket *avpkt)
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
53 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
54 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
55 int buf_size = avpkt->size;
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
56 // WSSNDContext *c = avctx->priv_data;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2585
diff changeset
57
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
58 int in_size, out_size;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
59 int sample = 0;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
60 int i;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
61 short *samples = data;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2585
diff changeset
62
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
63 if (!buf_size)
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
64 return 0;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
65
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4019
diff changeset
66 out_size = AV_RL16(&buf[0]);
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
67 *data_size = out_size * 2;
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4019
diff changeset
68 in_size = AV_RL16(&buf[2]);
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
69 buf += 4;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2585
diff changeset
70
5674
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
71 if (out_size > *data_size) {
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
72 av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
73 return -1;
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
74 }
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
75 if (in_size > buf_size) {
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
76 av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
77 return -1;
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 4364
diff changeset
78 }
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
79 if (in_size == out_size) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
80 for (i = 0; i < out_size; i++)
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
81 *samples++ = (*buf++ - 0x80) << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
82 return buf_size;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
83 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2585
diff changeset
84
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
85 while (out_size > 0) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
86 int code;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
87 uint8_t count;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
88 code = (*buf) >> 6;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
89 count = (*buf) & 0x3F;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
90 buf++;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
91 switch(code) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
92 case 0: /* ADPCM 2-bit */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
93 for (count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
94 code = *buf++;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
95 sample += ws_adpcm_2bit[code & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
96 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
97 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
98 sample += ws_adpcm_2bit[(code >> 2) & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
99 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
100 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
101 sample += ws_adpcm_2bit[(code >> 4) & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
102 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
103 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
104 sample += ws_adpcm_2bit[(code >> 6) & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
105 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
106 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
107 out_size -= 4;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
108 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
109 break;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
110 case 1: /* ADPCM 4-bit */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
111 for (count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
112 code = *buf++;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
113 sample += ws_adpcm_4bit[code & 0xF];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
114 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
115 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
116 sample += ws_adpcm_4bit[code >> 4];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
117 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
118 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
119 out_size -= 2;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
120 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
121 break;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
122 case 2: /* no compression */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
123 if (count & 0x20) { /* big delta */
12433
46724a37cc2d Hopefully fix the fate-ws_snd breakage on PPC
vitor
parents: 11644
diff changeset
124 int8_t t;
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
125 t = count;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
126 t <<= 3;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
127 sample += t >> 3;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
128 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
129 out_size--;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
130 } else { /* copy */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
131 for (count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
132 *samples++ = (*buf++ - 0x80) << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
133 out_size--;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
134 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
135 sample = buf[-1] - 0x80;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
136 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
137 break;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
138 default: /* run */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
139 for(count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
140 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
141 out_size--;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
142 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
143 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
144 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2585
diff changeset
145
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
146 return buf_size;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
147 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
148
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
149 AVCodec ws_snd1_decoder = {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
150 "ws_snd1",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 9355
diff changeset
151 AVMEDIA_TYPE_AUDIO,
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
152 CODEC_ID_WESTWOOD_SND1,
4019
6d4ac21853d7 Remove empty structures,
gpoirier
parents: 3947
diff changeset
153 0,
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
154 ws_snd_decode_init,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
155 NULL,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
156 NULL,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
157 ws_snd_decode_frame,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6717
diff changeset
158 .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
159 };