annotate alac.c @ 5364:50454f3b0e88 libavcodec

Remove reimplementation of av_log2
author vitor
date Wed, 18 Jul 2007 14:15:14 +0000
parents e0b7c248c33e
children 8feb956bdbee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
1 /*
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
2 * ALAC (Apple Lossless Audio Codec) decoder
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
3 * Copyright (c) 2005 David Hammerton
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3347
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3347
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3347
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
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: 3347
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3347
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
15 * Lesser General Public License for more details.
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
16 *
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
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: 3347
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
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
20 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
21
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
22 /**
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
23 * @file alac.c
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
24 * ALAC (Apple Lossless Audio Codec) decoder
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
25 * @author 2005 David Hammerton
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
26 *
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
27 * For more information on the ALAC format, visit:
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
28 * http://crazney.net/programs/itunes/alac.html
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
29 *
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
30 * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
31 * passed through the extradata[_size] fields. This atom is tacked onto
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
32 * the end of an 'alac' stsd atom and has the following format:
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
33 * bytes 0-3 atom size (0x24), big-endian
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
34 * bytes 4-7 atom type ('alac', not the 'alac' tag from start of stsd)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
35 * bytes 8-35 data bytes needed by decoder
3135
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
36 *
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
37 * Extradata:
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
38 * 32bit size
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
39 * 32bit tag (=alac)
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
40 * 32bit zero?
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
41 * 32bit max sample per frame
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
42 * 8bit ?? (zero?)
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
43 * 8bit sample size
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
44 * 8bit history mult
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
45 * 8bit initial history
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
46 * 8bit kmodifier
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
47 * 8bit channels?
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
48 * 16bit ??
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
49 * 32bit max coded frame size
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
50 * 32bit bitrate?
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
51 * 32bit samplerate
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
52 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
53
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
54
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
55 #include "avcodec.h"
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
56 #include "bitstream.h"
5276
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
57 #include "bytestream.h"
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
58
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
59 #define ALAC_EXTRADATA_SIZE 36
5272
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
60 #define MAX_CHANNELS 2
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
61
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
62 typedef struct {
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
63
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
64 AVCodecContext *avctx;
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
65 GetBitContext gb;
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
66 /* init to 0; first frame decode should initialize from extradata and
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
67 * set this to 1 */
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
68 int context_initialized;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
69
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
70 int samplesize;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
71 int numchannels;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
72 int bytespersample;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
73
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
74 /* buffers */
5272
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
75 int32_t *predicterror_buffer[MAX_CHANNELS];
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
76
5272
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
77 int32_t *outputsamples_buffer[MAX_CHANNELS];
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
78
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
79 /* stuff from setinfo */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
80 uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
81 uint8_t setinfo_7a; /* 0x00 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
82 uint8_t setinfo_sample_size; /* 0x10 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
83 uint8_t setinfo_rice_historymult; /* 0x28 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
84 uint8_t setinfo_rice_initialhistory; /* 0x0a */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
85 uint8_t setinfo_rice_kmodifier; /* 0x0e */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
86 uint8_t setinfo_7f; /* 0x02 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
87 uint16_t setinfo_80; /* 0x00ff */
5267
8256f8ec2fac Port some useful comments from newest version of David Hammerton's decoder (from which alac.c is based)
vitor
parents: 4364
diff changeset
88 uint32_t setinfo_82; /* 0x000020e7 */ /* max sample size?? */
8256f8ec2fac Port some useful comments from newest version of David Hammerton's decoder (from which alac.c is based)
vitor
parents: 4364
diff changeset
89 uint32_t setinfo_86; /* 0x00069fe4 */ /* bit rate (average)?? */
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
90 uint32_t setinfo_8a_rate; /* 0x0000ac44 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
91 /* end setinfo stuff */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
92
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
93 } ALACContext;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
94
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
95 static void allocate_buffers(ALACContext *alac)
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
96 {
5272
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
97 int chan;
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
98 for (chan = 0; chan < MAX_CHANNELS; chan++) {
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
99 alac->predicterror_buffer[chan] =
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
100 av_malloc(alac->setinfo_max_samples_per_frame * 4);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
101
5272
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
102 alac->outputsamples_buffer[chan] =
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
103 av_malloc(alac->setinfo_max_samples_per_frame * 4);
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
104 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
105 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
106
3303
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3299
diff changeset
107 static int alac_set_info(ALACContext *alac)
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
108 {
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
109 unsigned char *ptr = alac->avctx->extradata;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
110
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
111 ptr += 4; /* size */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
112 ptr += 4; /* alac */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
113 ptr += 4; /* 0 ? */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
114
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4226
diff changeset
115 if(AV_RB32(ptr) >= UINT_MAX/4){
3303
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3299
diff changeset
116 av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3299
diff changeset
117 return -1;
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3299
diff changeset
118 }
5276
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
119
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
120 /* buffer size / 2 ? */
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
121 alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
5360
6648c82e15c7 Cosmetics: alignment
vitor
parents: 5359
diff changeset
122 alac->setinfo_7a = *ptr++;
6648c82e15c7 Cosmetics: alignment
vitor
parents: 5359
diff changeset
123 alac->setinfo_sample_size = *ptr++;
6648c82e15c7 Cosmetics: alignment
vitor
parents: 5359
diff changeset
124 alac->setinfo_rice_historymult = *ptr++;
6648c82e15c7 Cosmetics: alignment
vitor
parents: 5359
diff changeset
125 alac->setinfo_rice_initialhistory = *ptr++;
6648c82e15c7 Cosmetics: alignment
vitor
parents: 5359
diff changeset
126 alac->setinfo_rice_kmodifier = *ptr++;
6648c82e15c7 Cosmetics: alignment
vitor
parents: 5359
diff changeset
127 /* channels? */
6648c82e15c7 Cosmetics: alignment
vitor
parents: 5359
diff changeset
128 alac->setinfo_7f = *ptr++;
5276
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
129 alac->setinfo_80 = bytestream_get_be16(&ptr);
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
130 /* max coded frame size */
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
131 alac->setinfo_82 = bytestream_get_be32(&ptr);
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
132 /* bitrate ? */
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
133 alac->setinfo_86 = bytestream_get_be32(&ptr);
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
134 /* samplerate */
0d71d658b19f Use proper bytestream functions
vitor
parents: 5272
diff changeset
135 alac->setinfo_8a_rate = bytestream_get_be32(&ptr);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
136
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
137 allocate_buffers(alac);
3303
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3299
diff changeset
138
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3299
diff changeset
139 return 0;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
140 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
141
5364
50454f3b0e88 Remove reimplementation of av_log2
vitor
parents: 5362
diff changeset
142 static inline int count_leading_zeros(int32_t input)
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
143 {
5364
50454f3b0e88 Remove reimplementation of av_log2
vitor
parents: 5362
diff changeset
144 return 31-av_log2(input);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
145 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
146
3071
cc0357a90e8f make some functions static (patch by Dieter < freebsd at sopwith.solgatos.com >)
aurel
parents: 3036
diff changeset
147 static void bastardized_rice_decompress(ALACContext *alac,
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
148 int32_t *output_buffer,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
149 int output_size,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
150 int readsamplesize, /* arg_10 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
151 int rice_initialhistory, /* arg424->b */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
152 int rice_kmodifier, /* arg424->d */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
153 int rice_historymult, /* arg424->c */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
154 int rice_kmodifier_mask /* arg424->e */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
155 )
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
156 {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
157 int output_count;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
158 unsigned int history = rice_initialhistory;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
159 int sign_modifier = 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
160
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
161 for (output_count = 0; output_count < output_size; output_count++) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
162 int32_t x = 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
163 int32_t x_modified;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
164 int32_t final_val;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
165
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
166 /* read x - number of 1s before 0 represent the rice */
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
167 while (x <= 8 && get_bits1(&alac->gb)) {
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
168 x++;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
169 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
170
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
171
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
172 if (x > 8) { /* RICE THRESHOLD */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
173 /* use alternative encoding */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
174 int32_t value;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
175
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
176 value = get_bits(&alac->gb, readsamplesize);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
177
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
178 /* mask value to readsamplesize size */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
179 if (readsamplesize != 32)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
180 value &= (0xffffffff >> (32 - readsamplesize));
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
181
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
182 x = value;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
183 } else {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
184 /* standard rice encoding */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
185 int extrabits;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
186 int k; /* size of extra bits */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
187
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
188 /* read k, that is bits as is */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
189 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
190
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2782
diff changeset
191 if (k < 0)
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
192 k += rice_kmodifier;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2782
diff changeset
193 else
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
194 k = rice_kmodifier;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
195
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
196 if (k != 1) {
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
197 extrabits = show_bits(&alac->gb, k);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
198
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
199 /* multiply x by 2^k - 1, as part of their strange algorithm */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
200 x = (x << k) - x;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
201
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
202 if (extrabits > 1) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
203 x += extrabits - 1;
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
204 get_bits(&alac->gb, k);
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
205 } else {
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
206 get_bits(&alac->gb, k - 1);
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
207 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
208 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
209 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
210
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
211 x_modified = sign_modifier + x;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
212 final_val = (x_modified + 1) / 2;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
213 if (x_modified & 1) final_val *= -1;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
214
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
215 output_buffer[output_count] = final_val;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
216
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
217 sign_modifier = 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
218
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
219 /* now update the history */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
220 history += (x_modified * rice_historymult)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
221 - ((history * rice_historymult) >> 9);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
222
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
223 if (x_modified > 0xffff)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
224 history = 0xffff;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
225
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
226 /* special case: there may be compressed blocks of 0 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
227 if ((history < 128) && (output_count+1 < output_size)) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
228 int block_size;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
229
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
230 sign_modifier = 1;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
231
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
232 x = 0;
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
233 while (x <= 8 && get_bits1(&alac->gb)) {
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
234 x++;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
235 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
236
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
237 if (x > 8) {
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
238 block_size = get_bits(&alac->gb, 16);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
239 block_size &= 0xffff;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
240 } else {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
241 int k;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
242 int extrabits;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
243
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
244 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
245
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
246 extrabits = show_bits(&alac->gb, k);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
247
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
248 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
249 + extrabits - 1;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
250
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
251 if (extrabits < 2) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
252 x = 1 - extrabits;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
253 block_size += x;
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
254 get_bits(&alac->gb, k - 1);
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
255 } else {
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
256 get_bits(&alac->gb, k);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
257 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
258 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
259
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
260 if (block_size > 0) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
261 memset(&output_buffer[output_count+1], 0, block_size * 4);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
262 output_count += block_size;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
263
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
264 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
265
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
266 if (block_size > 0xffff)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
267 sign_modifier = 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
268
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
269 history = 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
270 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
271 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
272 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
273
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
274 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
275
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
276 #define SIGN_ONLY(v) \
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
277 ((v < 0) ? (-1) : \
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
278 ((v > 0) ? (1) : \
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
279 (0)))
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
280
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
281 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
282 int32_t *buffer_out,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
283 int output_size,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
284 int readsamplesize,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
285 int16_t *predictor_coef_table,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
286 int predictor_coef_num,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
287 int predictor_quantitization)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
288 {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
289 int i;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
290
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
291 /* first sample always copies */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
292 *buffer_out = *error_buffer;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
293
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
294 if (!predictor_coef_num) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
295 if (output_size <= 1) return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
296 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
297 return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
298 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
299
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
300 if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
301 /* second-best case scenario for fir decompression,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
302 * error describes a small difference from the previous sample only
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
303 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
304 if (output_size <= 1) return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
305 for (i = 0; i < output_size - 1; i++) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
306 int32_t prev_value;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
307 int32_t error_value;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
308
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
309 prev_value = buffer_out[i];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
310 error_value = error_buffer[i+1];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
311 buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
312 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
313 return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
314 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
315
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
316 /* read warm-up samples */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
317 if (predictor_coef_num > 0) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
318 int i;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
319 for (i = 0; i < predictor_coef_num; i++) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
320 int32_t val;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
321
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
322 val = buffer_out[i] + error_buffer[i+1];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
323
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
324 val = SIGN_EXTENDED32(val, readsamplesize);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
325
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
326 buffer_out[i+1] = val;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
327 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
328 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
329
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
330 #if 0
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
331 /* 4 and 8 are very common cases (the only ones i've seen). these
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
332 * should be unrolled and optimised
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
333 */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
334 if (predictor_coef_num == 4) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
335 /* FIXME: optimised general case */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
336 return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
337 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
338
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
339 if (predictor_coef_table == 8) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
340 /* FIXME: optimised general case */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
341 return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
342 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
343 #endif
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
344
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
345
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
346 /* general case */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
347 if (predictor_coef_num > 0) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
348 for (i = predictor_coef_num + 1;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
349 i < output_size;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
350 i++) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
351 int j;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
352 int sum = 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
353 int outval;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
354 int error_val = error_buffer[i];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
355
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
356 for (j = 0; j < predictor_coef_num; j++) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
357 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
358 predictor_coef_table[j];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
359 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
360
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
361 outval = (1 << (predictor_quantitization-1)) + sum;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
362 outval = outval >> predictor_quantitization;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
363 outval = outval + buffer_out[0] + error_val;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
364 outval = SIGN_EXTENDED32(outval, readsamplesize);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
365
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
366 buffer_out[predictor_coef_num+1] = outval;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
367
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
368 if (error_val > 0) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
369 int predictor_num = predictor_coef_num - 1;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
370
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
371 while (predictor_num >= 0 && error_val > 0) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
372 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
373 int sign = SIGN_ONLY(val);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
374
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
375 predictor_coef_table[predictor_num] -= sign;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
376
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
377 val *= sign; /* absolute value */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
378
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
379 error_val -= ((val >> predictor_quantitization) *
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
380 (predictor_coef_num - predictor_num));
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
381
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
382 predictor_num--;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
383 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
384 } else if (error_val < 0) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
385 int predictor_num = predictor_coef_num - 1;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
386
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
387 while (predictor_num >= 0 && error_val < 0) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
388 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
389 int sign = - SIGN_ONLY(val);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
390
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
391 predictor_coef_table[predictor_num] -= sign;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
392
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
393 val *= sign; /* neg value */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
394
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
395 error_val -= ((val >> predictor_quantitization) *
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
396 (predictor_coef_num - predictor_num));
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
397
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
398 predictor_num--;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
399 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
400 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
401
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
402 buffer_out++;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
403 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
404 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
405 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
406
3347
82277c821113 Add const to (mostly) char* and make some functions static, which aren't used
diego
parents: 3303
diff changeset
407 static void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
408 int16_t *buffer_out,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
409 int numchannels, int numsamples,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
410 uint8_t interlacing_shift,
2543
e8f1f57215ad do not use a variable before proper initialization
melanson
parents: 2542
diff changeset
411 uint8_t interlacing_leftweight)
e8f1f57215ad do not use a variable before proper initialization
melanson
parents: 2542
diff changeset
412 {
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
413 int i;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
414 if (numsamples <= 0) return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
415
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
416 /* weighted interlacing */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
417 if (interlacing_leftweight) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
418 for (i = 0; i < numsamples; i++) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
419 int32_t difference, midright;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
420 int16_t left;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
421 int16_t right;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
422
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
423 midright = buffer_a[i];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
424 difference = buffer_b[i];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
425
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
426
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
427 right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
428 left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
429 + difference;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
430
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
431 buffer_out[i*numchannels] = left;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
432 buffer_out[i*numchannels + 1] = right;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
433 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
434
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
435 return;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
436 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
437
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
438 /* otherwise basic interlacing took place */
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
439 for (i = 0; i < numsamples; i++) {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
440 int16_t left, right;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
441
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
442 left = buffer_a[i];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
443 right = buffer_b[i];
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
444
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
445 buffer_out[i*numchannels] = left;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
446 buffer_out[i*numchannels + 1] = right;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
447 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
448 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
449
2544
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
450 static int alac_decode_frame(AVCodecContext *avctx,
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
451 void *outbuffer, int *outputsize,
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
452 uint8_t *inbuffer, int input_buffer_size)
2543
e8f1f57215ad do not use a variable before proper initialization
melanson
parents: 2542
diff changeset
453 {
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
454 ALACContext *alac = avctx->priv_data;
2544
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
455
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
456 int channels;
2543
e8f1f57215ad do not use a variable before proper initialization
melanson
parents: 2542
diff changeset
457 int32_t outputsamples;
5271
289e5c2a9c98 Remove some duplicated code
vitor
parents: 5267
diff changeset
458 int hassize;
289e5c2a9c98 Remove some duplicated code
vitor
parents: 5267
diff changeset
459 int readsamplesize;
289e5c2a9c98 Remove some duplicated code
vitor
parents: 5267
diff changeset
460 int wasted_bytes;
289e5c2a9c98 Remove some duplicated code
vitor
parents: 5267
diff changeset
461 int isnotcompressed;
5358
f3a26c190f9a Move var. declaration to allow further clean up
vitor
parents: 5357
diff changeset
462 uint8_t interlacing_shift;
f3a26c190f9a Move var. declaration to allow further clean up
vitor
parents: 5357
diff changeset
463 uint8_t interlacing_leftweight;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
464
2544
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
465 /* short-circuit null buffers */
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
466 if (!inbuffer || !input_buffer_size)
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
467 return input_buffer_size;
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
468
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
469 /* initialize from the extradata */
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
470 if (!alac->context_initialized) {
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
471 if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
3135
41a01cea8885 explain extradata and pass avctx to av_log's
alex
parents: 3071
diff changeset
472 av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
473 ALAC_EXTRADATA_SIZE);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
474 return input_buffer_size;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
475 }
4226
43ebe9279fa0 fix some potential security issues, patch by Matthias Hopf, mat at mshopf dot de
bcoudurier
parents: 3947
diff changeset
476 if (alac_set_info(alac)) {
43ebe9279fa0 fix some potential security issues, patch by Matthias Hopf, mat at mshopf dot de
bcoudurier
parents: 3947
diff changeset
477 av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
43ebe9279fa0 fix some potential security issues, patch by Matthias Hopf, mat at mshopf dot de
bcoudurier
parents: 3947
diff changeset
478 return input_buffer_size;
43ebe9279fa0 fix some potential security issues, patch by Matthias Hopf, mat at mshopf dot de
bcoudurier
parents: 3947
diff changeset
479 }
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
480 alac->context_initialized = 1;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
481 }
2543
e8f1f57215ad do not use a variable before proper initialization
melanson
parents: 2542
diff changeset
482
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
483 init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
484
5298
e00f7048c50c Make "channels" variable mean the number of channels, not the number of
vitor
parents: 5292
diff changeset
485 channels = get_bits(&alac->gb, 3) + 1;
5362
e0b7c248c33e Test for supported number of channels
vitor
parents: 5361
diff changeset
486 if (channels > MAX_CHANNELS) {
e0b7c248c33e Test for supported number of channels
vitor
parents: 5361
diff changeset
487 av_log(avctx, AV_LOG_ERROR, "channels > %d not supported\n",
e0b7c248c33e Test for supported number of channels
vitor
parents: 5361
diff changeset
488 MAX_CHANNELS);
e0b7c248c33e Test for supported number of channels
vitor
parents: 5361
diff changeset
489 return input_buffer_size;
e0b7c248c33e Test for supported number of channels
vitor
parents: 5361
diff changeset
490 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
491
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
492 /* 2^result = something to do with output waiting.
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
493 * perhaps matters if we read > 1 frame in a pass?
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
494 */
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
495 get_bits(&alac->gb, 4);
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
496
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
497 get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
498
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
499 /* the output sample size is stored soon */
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
500 hassize = get_bits(&alac->gb, 1);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
501
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
502 wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
503
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
504 /* whether the frame is compressed */
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
505 isnotcompressed = get_bits(&alac->gb, 1);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
506
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
507 if (hassize) {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
508 /* now read the number of samples as a 32bit integer */
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
509 outputsamples = get_bits(&alac->gb, 32);
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
510 } else
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
511 outputsamples = alac->setinfo_max_samples_per_frame;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
512
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
513 *outputsize = outputsamples * alac->bytespersample;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
514 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + channels - 1;
5271
289e5c2a9c98 Remove some duplicated code
vitor
parents: 5267
diff changeset
515
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
516 if (!isnotcompressed) {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
517 /* so it is compressed */
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
518 int16_t predictor_coef_table[channels][32];
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
519 int predictor_coef_num[channels];
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
520 int prediction_type[channels];
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
521 int prediction_quantitization[channels];
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
522 int ricemodifier[channels];
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
523 int i, chan;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
524
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
525 interlacing_shift = get_bits(&alac->gb, 8);
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
526 interlacing_leftweight = get_bits(&alac->gb, 8);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
527
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
528 for (chan = 0; chan < channels; chan++) {
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
529 prediction_type[chan] = get_bits(&alac->gb, 4);
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
530 prediction_quantitization[chan] = get_bits(&alac->gb, 4);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
531
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
532 ricemodifier[chan] = get_bits(&alac->gb, 3);
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
533 predictor_coef_num[chan] = get_bits(&alac->gb, 5);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
534
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
535 /* read the predictor table */
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
536 for (i = 0; i < predictor_coef_num[chan]; i++) {
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
537 predictor_coef_table[chan][i] = (int16_t)get_bits(&alac->gb, 16);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
538 }
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
539 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
540
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
541 if (wasted_bytes) {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
542 av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
543 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
544
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
545 for (chan = 0; chan < channels; chan++) {
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
546 bastardized_rice_decompress(alac,
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
547 alac->predicterror_buffer[chan],
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
548 outputsamples,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
549 readsamplesize,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
550 alac->setinfo_rice_initialhistory,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
551 alac->setinfo_rice_kmodifier,
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
552 ricemodifier[chan] * alac->setinfo_rice_historymult / 4,
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
553 (1 << alac->setinfo_rice_kmodifier) - 1);
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
554
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
555 if (prediction_type[chan] == 0) {
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
556 /* adaptive fir */
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
557 predictor_decompress_fir_adapt(alac->predicterror_buffer[chan],
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
558 alac->outputsamples_buffer[chan],
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
559 outputsamples,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
560 readsamplesize,
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
561 predictor_coef_table[chan],
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
562 predictor_coef_num[chan],
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
563 prediction_quantitization[chan]);
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
564 } else {
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
565 av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type[chan]);
5359
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
566 /* i think the only other prediction type (or perhaps this is just a
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
567 * boolean?) runs adaptive fir twice.. like:
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
568 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
569 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
570 * little strange..
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
571 */
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
572 }
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
573 }
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
574 } else {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
575 /* not compressed, easy case */
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
576 if (alac->setinfo_sample_size <= 16) {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
577 int i, chan;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
578 for (chan = 0; chan < channels; chan++) {
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
579 for (i = 0; i < outputsamples; i++) {
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
580 int32_t audiobits;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
581
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
582 audiobits = get_bits(&alac->gb, alac->setinfo_sample_size);
5359
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
583 audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
584
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
585 alac->outputsamples_buffer[chan][i] = audiobits;
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
586 }
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
587 }
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
588 } else {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
589 int i, chan;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
590 for (chan = 0; chan < channels; chan++) {
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
591 for (i = 0; i < outputsamples; i++) {
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
592 int32_t audiobits;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
593
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
594 audiobits = get_bits(&alac->gb, 16);
5359
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
595 /* special case of sign extension..
4743a1e95531 Remove more code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5358
diff changeset
596 * as we'll be ORing the low 16bits into this */
5357
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
597 audiobits = audiobits << 16;
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
598 audiobits = audiobits >> (32 - alac->setinfo_sample_size);
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
599 audiobits |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
600
07c6fa478918 Remove code duplication. Based on a patch by Matthieu Castet.
vitor
parents: 5298
diff changeset
601 alac->outputsamples_buffer[chan][i] = audiobits;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
602 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
603 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
604 }
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
605 /* wasted_bytes = 0; */
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
606 interlacing_shift = 0;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
607 interlacing_leftweight = 0;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
608 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
609
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
610 switch(alac->setinfo_sample_size) {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
611 case 16: {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
612 if (channels == 2) {
5272
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
613 deinterlace_16(alac->outputsamples_buffer[0],
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
614 alac->outputsamples_buffer[1],
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
615 (int16_t*)outbuffer,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
616 alac->numchannels,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
617 outputsamples,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
618 interlacing_shift,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
619 interlacing_leftweight);
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
620 } else {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
621 int i;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
622 for (i = 0; i < outputsamples; i++) {
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
623 int16_t sample = alac->outputsamples_buffer[0][i];
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
624 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
625 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
626 }
5361
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
627 break;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
628 }
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
629 case 20:
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
630 case 24:
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
631 case 32:
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
632 av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
633 break;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
634 default:
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
635 break;
3c0a5cb7fc6b Cosmetics: indentation
vitor
parents: 5360
diff changeset
636 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
637
2544
8c426f5a09ae decoder works fine now, when fed properly-sized chunks by the demuxer;
melanson
parents: 2543
diff changeset
638 return input_buffer_size;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
639 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
640
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
641 static int alac_decode_init(AVCodecContext * avctx)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
642 {
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
643 ALACContext *alac = avctx->priv_data;
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
644 alac->avctx = avctx;
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
645 alac->context_initialized = 0;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
646
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
647 alac->samplesize = alac->avctx->bits_per_sample;
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
648 alac->numchannels = alac->avctx->channels;
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
649 alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
650
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
651 return 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
652 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
653
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
654 static int alac_decode_close(AVCodecContext *avctx)
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
655 {
2559
7d8ba28e18d9 replace custom bit-reading functions with FFmpeg's internal function;
melanson
parents: 2546
diff changeset
656 ALACContext *alac = avctx->priv_data;
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
657
5272
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
658 int chan;
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
659 for (chan = 0; chan < MAX_CHANNELS; chan++) {
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
660 av_free(alac->predicterror_buffer[chan]);
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
661 av_free(alac->outputsamples_buffer[chan]);
b51c25603164 Prepare to remove more duplicated code
vitor
parents: 5271
diff changeset
662 }
2542
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
663
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
664 return 0;
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
665 }
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
666
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
667 AVCodec alac_decoder = {
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
668 "alac",
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
669 CODEC_TYPE_AUDIO,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
670 CODEC_ID_ALAC,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
671 sizeof(ALACContext),
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
672 alac_decode_init,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
673 NULL,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
674 alac_decode_close,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
675 alac_decode_frame,
a27a580f292e first pass at ALAC decoder from David Hammerton; while David's original
melanson
parents:
diff changeset
676 };