Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* APAC audio decoder |
3 |
|
|
* |
4 |
|
|
* This file is part of FFmpeg. |
5 |
|
|
* |
6 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include "libavutil/audio_fifo.h" |
22 |
|
|
#include "libavutil/mem.h" |
23 |
|
|
#include "avcodec.h" |
24 |
|
|
#include "codec_internal.h" |
25 |
|
|
#include "decode.h" |
26 |
|
|
#include "get_bits.h" |
27 |
|
|
|
28 |
|
|
typedef struct ChContext { |
29 |
|
|
int have_code; |
30 |
|
|
int last_sample; |
31 |
|
|
int last_delta; |
32 |
|
|
int bit_length; |
33 |
|
|
int block_length; |
34 |
|
|
uint8_t block[32 * 2]; |
35 |
|
|
AVAudioFifo *samples; |
36 |
|
|
} ChContext; |
37 |
|
|
|
38 |
|
|
typedef struct APACContext { |
39 |
|
|
GetBitContext gb; |
40 |
|
|
int skip; |
41 |
|
|
|
42 |
|
|
int cur_ch; |
43 |
|
|
ChContext ch[2]; |
44 |
|
|
|
45 |
|
|
uint8_t *bitstream; |
46 |
|
|
int64_t max_framesize; |
47 |
|
|
int bitstream_size; |
48 |
|
|
int bitstream_index; |
49 |
|
|
} APACContext; |
50 |
|
|
|
51 |
|
✗ |
static av_cold int apac_close(AVCodecContext *avctx) |
52 |
|
|
{ |
53 |
|
✗ |
APACContext *s = avctx->priv_data; |
54 |
|
|
|
55 |
|
✗ |
av_freep(&s->bitstream); |
56 |
|
✗ |
s->bitstream_size = 0; |
57 |
|
|
|
58 |
|
✗ |
for (int ch = 0; ch < 2; ch++) { |
59 |
|
✗ |
ChContext *c = &s->ch[ch]; |
60 |
|
|
|
61 |
|
✗ |
av_audio_fifo_free(c->samples); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
✗ |
return 0; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
✗ |
static av_cold int apac_init(AVCodecContext *avctx) |
68 |
|
|
{ |
69 |
|
✗ |
APACContext *s = avctx->priv_data; |
70 |
|
|
|
71 |
|
✗ |
if (avctx->bits_per_coded_sample > 8) |
72 |
|
✗ |
avctx->sample_fmt = AV_SAMPLE_FMT_S16P; |
73 |
|
|
else |
74 |
|
✗ |
avctx->sample_fmt = AV_SAMPLE_FMT_U8P; |
75 |
|
|
|
76 |
|
✗ |
if (avctx->ch_layout.nb_channels < 1 || |
77 |
|
✗ |
avctx->ch_layout.nb_channels > 2 || |
78 |
|
✗ |
avctx->bits_per_coded_sample < 8 || |
79 |
|
✗ |
avctx->bits_per_coded_sample > 16 |
80 |
|
|
) |
81 |
|
✗ |
return AVERROR_INVALIDDATA; |
82 |
|
|
|
83 |
|
✗ |
for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { |
84 |
|
✗ |
ChContext *c = &s->ch[ch]; |
85 |
|
|
|
86 |
|
✗ |
c->bit_length = avctx->bits_per_coded_sample; |
87 |
|
✗ |
c->block_length = 8; |
88 |
|
✗ |
c->have_code = 0; |
89 |
|
✗ |
c->samples = av_audio_fifo_alloc(avctx->sample_fmt, 1, 1024); |
90 |
|
✗ |
if (!c->samples) |
91 |
|
✗ |
return AVERROR(ENOMEM); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
✗ |
s->max_framesize = 1024; |
95 |
|
✗ |
s->bitstream = av_realloc_f(s->bitstream, s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream)); |
96 |
|
✗ |
if (!s->bitstream) |
97 |
|
✗ |
return AVERROR(ENOMEM); |
98 |
|
|
|
99 |
|
✗ |
return 0; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
✗ |
static int get_code(ChContext *c, GetBitContext *gb) |
103 |
|
|
{ |
104 |
|
✗ |
if (get_bits1(gb)) { |
105 |
|
✗ |
int code = get_bits(gb, 2); |
106 |
|
|
|
107 |
|
✗ |
switch (code) { |
108 |
|
✗ |
case 0: |
109 |
|
✗ |
c->bit_length--; |
110 |
|
✗ |
break; |
111 |
|
✗ |
case 1: |
112 |
|
✗ |
c->bit_length++; |
113 |
|
✗ |
break; |
114 |
|
✗ |
case 2: |
115 |
|
✗ |
c->bit_length = get_bits(gb, 5); |
116 |
|
✗ |
break; |
117 |
|
✗ |
case 3: |
118 |
|
✗ |
c->block_length = get_bits(gb, 4); |
119 |
|
✗ |
return 1; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|
123 |
|
✗ |
return 0; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
✗ |
static int apac_decode(AVCodecContext *avctx, AVFrame *frame, |
127 |
|
|
int *got_frame_ptr, AVPacket *pkt) |
128 |
|
|
{ |
129 |
|
✗ |
APACContext *s = avctx->priv_data; |
130 |
|
✗ |
GetBitContext *gb = &s->gb; |
131 |
|
|
int ret, n, buf_size, input_buf_size; |
132 |
|
|
uint8_t *buf; |
133 |
|
|
int nb_samples; |
134 |
|
|
|
135 |
|
✗ |
if (!pkt->size && s->bitstream_size <= 0) { |
136 |
|
✗ |
*got_frame_ptr = 0; |
137 |
|
✗ |
return 0; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
✗ |
buf_size = pkt->size; |
141 |
|
✗ |
input_buf_size = buf_size; |
142 |
|
|
|
143 |
|
✗ |
if (s->bitstream_index > 0 && s->bitstream_size > 0) { |
144 |
|
✗ |
memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size); |
145 |
|
✗ |
s->bitstream_index = 0; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
✗ |
if (s->bitstream_index + s->bitstream_size + buf_size > s->max_framesize) { |
149 |
|
✗ |
s->bitstream = av_realloc_f(s->bitstream, s->bitstream_index + |
150 |
|
✗ |
s->bitstream_size + |
151 |
|
✗ |
buf_size + AV_INPUT_BUFFER_PADDING_SIZE, |
152 |
|
|
sizeof(*s->bitstream)); |
153 |
|
✗ |
if (!s->bitstream) |
154 |
|
✗ |
return AVERROR(ENOMEM); |
155 |
|
✗ |
s->max_framesize = s->bitstream_index + s->bitstream_size + buf_size; |
156 |
|
|
} |
157 |
|
✗ |
if (pkt->data) |
158 |
|
✗ |
memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size); |
159 |
|
✗ |
buf = &s->bitstream[s->bitstream_index]; |
160 |
|
✗ |
buf_size += s->bitstream_size; |
161 |
|
✗ |
s->bitstream_size = buf_size; |
162 |
|
✗ |
memset(buf + buf_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
163 |
|
|
|
164 |
|
✗ |
frame->nb_samples = s->bitstream_size * 16 * 8; |
165 |
|
✗ |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
166 |
|
✗ |
return ret; |
167 |
|
|
|
168 |
|
✗ |
if ((ret = init_get_bits8(gb, buf, buf_size)) < 0) |
169 |
|
✗ |
return ret; |
170 |
|
|
|
171 |
|
✗ |
skip_bits(gb, s->skip); |
172 |
|
✗ |
s->skip = 0; |
173 |
|
|
|
174 |
|
✗ |
while (get_bits_left(gb) > 0) { |
175 |
|
✗ |
for (int ch = s->cur_ch; ch < avctx->ch_layout.nb_channels; ch++) { |
176 |
|
✗ |
ChContext *c = &s->ch[ch]; |
177 |
|
✗ |
int16_t *dst16 = (int16_t *)c->block; |
178 |
|
✗ |
uint8_t *dst8 = (uint8_t *)c->block; |
179 |
|
|
void *samples[4]; |
180 |
|
|
|
181 |
|
✗ |
samples[0] = &c->block[0]; |
182 |
|
✗ |
if (get_bits_left(gb) < 16 && pkt->size) { |
183 |
|
✗ |
s->cur_ch = ch; |
184 |
|
✗ |
goto end; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
✗ |
if (!c->have_code && get_code(c, gb)) |
188 |
|
✗ |
get_code(c, gb); |
189 |
|
✗ |
c->have_code = 0; |
190 |
|
|
|
191 |
|
✗ |
if (c->block_length <= 0) |
192 |
|
✗ |
continue; |
193 |
|
|
|
194 |
|
✗ |
if (c->bit_length < 0 || |
195 |
|
✗ |
c->bit_length > 17) { |
196 |
|
✗ |
c->bit_length = avctx->bits_per_coded_sample; |
197 |
|
✗ |
s->bitstream_index = 0; |
198 |
|
✗ |
s->bitstream_size = 0; |
199 |
|
✗ |
return AVERROR_INVALIDDATA; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
✗ |
if (get_bits_left(gb) < c->block_length * c->bit_length) { |
203 |
|
✗ |
if (pkt->size) { |
204 |
|
✗ |
c->have_code = 1; |
205 |
|
✗ |
s->cur_ch = ch; |
206 |
|
✗ |
goto end; |
207 |
|
|
} else { |
208 |
|
✗ |
break; |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
✗ |
for (int i = 0; i < c->block_length; i++) { |
213 |
|
✗ |
int val = get_bits_long(gb, c->bit_length); |
214 |
|
✗ |
unsigned delta = (val & 1) ? ~(val >> 1) : (val >> 1); |
215 |
|
|
int sample; |
216 |
|
|
|
217 |
|
✗ |
delta += c->last_delta; |
218 |
|
✗ |
sample = c->last_sample + delta; |
219 |
|
✗ |
c->last_delta = delta; |
220 |
|
✗ |
c->last_sample = sample; |
221 |
|
|
|
222 |
|
✗ |
switch (avctx->sample_fmt) { |
223 |
|
✗ |
case AV_SAMPLE_FMT_S16P: |
224 |
|
✗ |
dst16[i] = sample; |
225 |
|
✗ |
break; |
226 |
|
✗ |
case AV_SAMPLE_FMT_U8P: |
227 |
|
✗ |
dst8[i] = sample; |
228 |
|
✗ |
break; |
229 |
|
|
} |
230 |
|
|
} |
231 |
|
|
|
232 |
|
✗ |
av_audio_fifo_write(c->samples, samples, c->block_length); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
✗ |
s->cur_ch = 0; |
236 |
|
|
} |
237 |
|
✗ |
end: |
238 |
|
✗ |
nb_samples = frame->nb_samples; |
239 |
|
✗ |
for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) |
240 |
|
✗ |
nb_samples = FFMIN(av_audio_fifo_size(s->ch[ch].samples), nb_samples); |
241 |
|
|
|
242 |
|
✗ |
frame->nb_samples = nb_samples; |
243 |
|
✗ |
for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { |
244 |
|
✗ |
void *samples[1] = { frame->extended_data[ch] }; |
245 |
|
✗ |
av_audio_fifo_read(s->ch[ch].samples, samples, nb_samples); |
246 |
|
|
} |
247 |
|
|
|
248 |
|
✗ |
s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8); |
249 |
|
✗ |
n = get_bits_count(gb) / 8; |
250 |
|
|
|
251 |
|
✗ |
if (nb_samples > 0 || pkt->size) |
252 |
|
✗ |
*got_frame_ptr = 1; |
253 |
|
|
|
254 |
|
✗ |
if (s->bitstream_size > 0) { |
255 |
|
✗ |
s->bitstream_index += n; |
256 |
|
✗ |
s->bitstream_size -= n; |
257 |
|
✗ |
return input_buf_size; |
258 |
|
|
} |
259 |
|
✗ |
return n; |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
const FFCodec ff_apac_decoder = { |
263 |
|
|
.p.name = "apac", |
264 |
|
|
CODEC_LONG_NAME("Marian's A-pac audio"), |
265 |
|
|
.p.type = AVMEDIA_TYPE_AUDIO, |
266 |
|
|
.p.id = AV_CODEC_ID_APAC, |
267 |
|
|
.priv_data_size = sizeof(APACContext), |
268 |
|
|
.init = apac_init, |
269 |
|
|
FF_CODEC_DECODE_CB(apac_decode), |
270 |
|
|
.close = apac_close, |
271 |
|
|
.p.capabilities = AV_CODEC_CAP_DELAY | |
272 |
|
|
#if FF_API_SUBFRAMES |
273 |
|
|
AV_CODEC_CAP_SUBFRAMES | |
274 |
|
|
#endif |
275 |
|
|
AV_CODEC_CAP_DR1, |
276 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
277 |
|
|
.p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P, |
278 |
|
|
AV_SAMPLE_FMT_S16P, |
279 |
|
|
AV_SAMPLE_FMT_NONE }, |
280 |
|
|
}; |
281 |
|
|
|