FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/apac.c
Date: 2022-12-06 04:36:11
Exec Total Coverage
Lines: 0 135 0.0%
Functions: 0 4 0.0%
Branches: 0 80 0.0%

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/internal.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avcodec.h"
25 #include "codec_internal.h"
26 #include "decode.h"
27 #include "get_bits.h"
28
29 typedef struct ChContext {
30 int have_code;
31 int last_sample;
32 int last_delta;
33 int bit_length;
34 int block_length;
35 uint8_t block[32 * 2];
36 AVAudioFifo *samples;
37 } ChContext;
38
39 typedef struct APACContext {
40 GetBitContext gb;
41 int skip;
42
43 int cur_ch;
44 ChContext ch[2];
45
46 uint8_t *bitstream;
47 int64_t max_framesize;
48 int bitstream_size;
49 int bitstream_index;
50 } APACContext;
51
52 static av_cold int apac_close(AVCodecContext *avctx)
53 {
54 APACContext *s = avctx->priv_data;
55
56 av_freep(&s->bitstream);
57 s->bitstream_size = 0;
58
59 for (int ch = 0; ch < 2; ch++) {
60 ChContext *c = &s->ch[ch];
61
62 av_audio_fifo_free(c->samples);
63 }
64
65 return 0;
66 }
67
68 static av_cold int apac_init(AVCodecContext *avctx)
69 {
70 APACContext *s = avctx->priv_data;
71
72 if (avctx->bits_per_coded_sample > 8)
73 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
74 else
75 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
76
77 if (avctx->ch_layout.nb_channels < 1 ||
78 avctx->ch_layout.nb_channels > 2)
79 return AVERROR_INVALIDDATA;
80
81 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
82 ChContext *c = &s->ch[ch];
83
84 c->bit_length = avctx->bits_per_coded_sample;
85 c->block_length = 8;
86 c->have_code = 0;
87 c->samples = av_audio_fifo_alloc(avctx->sample_fmt, 1, 1024);
88 if (!c->samples)
89 return AVERROR(ENOMEM);
90 }
91
92 s->max_framesize = 1024;
93 s->bitstream = av_realloc_f(s->bitstream, s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
94 if (!s->bitstream)
95 return AVERROR(ENOMEM);
96
97 return 0;
98 }
99
100 static int get_code(ChContext *c, GetBitContext *gb)
101 {
102 if (get_bits1(gb)) {
103 int code = get_bits(gb, 2);
104
105 switch (code) {
106 case 0:
107 c->bit_length--;
108 break;
109 case 1:
110 c->bit_length++;
111 break;
112 case 2:
113 c->bit_length = get_bits(gb, 5);
114 break;
115 case 3:
116 c->block_length = get_bits(gb, 4);
117 return 1;
118 }
119 }
120
121 return 0;
122 }
123
124 static int apac_decode(AVCodecContext *avctx, AVFrame *frame,
125 int *got_frame_ptr, AVPacket *pkt)
126 {
127 APACContext *s = avctx->priv_data;
128 GetBitContext *gb = &s->gb;
129 int ret, n, buf_size, input_buf_size;
130 const uint8_t *buf;
131 int nb_samples;
132
133 if (!pkt->size && s->bitstream_size <= 0) {
134 *got_frame_ptr = 0;
135 return 0;
136 }
137
138 buf_size = pkt->size;
139 input_buf_size = buf_size;
140
141 if (s->bitstream_index > 0 && s->bitstream_size > 0) {
142 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
143 s->bitstream_index = 0;
144 }
145
146 if (s->bitstream_index + s->bitstream_size + buf_size > s->max_framesize) {
147 s->bitstream = av_realloc_f(s->bitstream, s->bitstream_index +
148 s->bitstream_size +
149 buf_size + AV_INPUT_BUFFER_PADDING_SIZE,
150 sizeof(*s->bitstream));
151 if (!s->bitstream)
152 return AVERROR(ENOMEM);
153 s->max_framesize = s->bitstream_index + s->bitstream_size + buf_size;
154 }
155 if (pkt->data)
156 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size);
157 buf = &s->bitstream[s->bitstream_index];
158 buf_size += s->bitstream_size;
159 s->bitstream_size = buf_size;
160
161 frame->nb_samples = s->bitstream_size * 16 * 8;
162 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
163 return ret;
164
165 if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
166 return ret;
167
168 skip_bits(gb, s->skip);
169 s->skip = 0;
170
171 while (get_bits_left(gb) > 0) {
172 for (int ch = s->cur_ch; ch < avctx->ch_layout.nb_channels; ch++) {
173 ChContext *c = &s->ch[ch];
174 int16_t *dst16 = (int16_t *)c->block;
175 uint8_t *dst8 = (uint8_t *)c->block;
176 void *samples[4];
177
178 samples[0] = &c->block[0];
179 if (get_bits_left(gb) < 16 && pkt->size) {
180 s->cur_ch = ch;
181 goto end;
182 }
183
184 if (!c->have_code && get_code(c, gb))
185 get_code(c, gb);
186 c->have_code = 0;
187
188 if (c->block_length <= 0)
189 continue;
190
191 if (c->bit_length < 0 ||
192 c->bit_length > 17) {
193 c->bit_length = avctx->bits_per_coded_sample;
194 s->bitstream_index = 0;
195 s->bitstream_size = 0;
196 return AVERROR_INVALIDDATA;
197 }
198
199 if (get_bits_left(gb) < c->block_length * c->bit_length) {
200 if (pkt->size) {
201 c->have_code = 1;
202 s->cur_ch = ch;
203 goto end;
204 } else {
205 break;
206 }
207 }
208
209 for (int i = 0; i < c->block_length; i++) {
210 int val = get_bits_long(gb, c->bit_length);
211 unsigned delta = (val & 1) ? ~(val >> 1) : (val >> 1);
212 int sample;
213
214 delta += c->last_delta;
215 sample = c->last_sample + delta;
216 c->last_delta = delta;
217 c->last_sample = sample;
218
219 switch (avctx->sample_fmt) {
220 case AV_SAMPLE_FMT_S16P:
221 dst16[i] = sample;
222 break;
223 case AV_SAMPLE_FMT_U8P:
224 dst8[i] = sample;
225 break;
226 }
227 }
228
229 av_audio_fifo_write(c->samples, samples, c->block_length);
230 }
231
232 s->cur_ch = 0;
233 }
234 end:
235 nb_samples = frame->nb_samples;
236 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
237 nb_samples = FFMIN(av_audio_fifo_size(s->ch[ch].samples), nb_samples);
238
239 frame->nb_samples = nb_samples;
240 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
241 void *samples[1] = { frame->extended_data[ch] };
242 av_audio_fifo_read(s->ch[ch].samples, samples, nb_samples);
243 }
244
245 s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8);
246 n = get_bits_count(gb) / 8;
247
248 if (nb_samples > 0 || pkt->size)
249 *got_frame_ptr = 1;
250
251 if (s->bitstream_size > 0) {
252 s->bitstream_index += n;
253 s->bitstream_size -= n;
254 return input_buf_size;
255 }
256 return n;
257 }
258
259 const FFCodec ff_apac_decoder = {
260 .p.name = "apac",
261 CODEC_LONG_NAME("Marian's A-pac audio"),
262 .p.type = AVMEDIA_TYPE_AUDIO,
263 .p.id = AV_CODEC_ID_APAC,
264 .priv_data_size = sizeof(APACContext),
265 .init = apac_init,
266 FF_CODEC_DECODE_CB(apac_decode),
267 .close = apac_close,
268 .p.capabilities = AV_CODEC_CAP_DELAY |
269 AV_CODEC_CAP_DR1 |
270 AV_CODEC_CAP_SUBFRAMES,
271 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
272 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
273 AV_SAMPLE_FMT_S16P,
274 AV_SAMPLE_FMT_NONE },
275 };
276