aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/apac.c
blob: e9f6a0dd88d51fee7aac8811d7e3e9f660ec0299 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * APAC audio decoder
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/audio_fifo.h"
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "codec_internal.h"
#include "decode.h"
#include "get_bits.h"

typedef struct ChContext {
    int have_code;
    int last_sample;
    int last_delta;
    int bit_length;
    int block_length;
    uint8_t block[32 * 2];
    AVAudioFifo *samples;
} ChContext;

typedef struct APACContext {
    GetBitContext gb;
    int skip;

    int cur_ch;
    ChContext ch[2];

    uint8_t *bitstream;
    int64_t max_framesize;
    int bitstream_size;
    int bitstream_index;
} APACContext;

static av_cold int apac_close(AVCodecContext *avctx)
{
    APACContext *s = avctx->priv_data;

    av_freep(&s->bitstream);
    s->bitstream_size = 0;

    for (int ch = 0; ch < 2; ch++) {
        ChContext *c = &s->ch[ch];

        av_audio_fifo_free(c->samples);
    }

    return 0;
}

static av_cold int apac_init(AVCodecContext *avctx)
{
    APACContext *s = avctx->priv_data;

    if (avctx->bits_per_coded_sample > 8)
        avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
    else
        avctx->sample_fmt = AV_SAMPLE_FMT_U8P;

    if (avctx->ch_layout.nb_channels < 1 ||
        avctx->ch_layout.nb_channels > 2)
        return AVERROR_INVALIDDATA;

    for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
        ChContext *c = &s->ch[ch];

        c->bit_length = avctx->bits_per_coded_sample;
        c->block_length = 8;
        c->have_code = 0;
        c->samples = av_audio_fifo_alloc(avctx->sample_fmt, 1, 1024);
        if (!c->samples)
            return AVERROR(ENOMEM);
    }

    s->max_framesize = 1024;
    s->bitstream = av_realloc_f(s->bitstream, s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
    if (!s->bitstream)
        return AVERROR(ENOMEM);

    return 0;
}

static int get_code(ChContext *c, GetBitContext *gb)
{
    if (get_bits1(gb)) {
        int code = get_bits(gb, 2);

        switch (code) {
        case 0:
            c->bit_length--;
            break;
        case 1:
            c->bit_length++;
            break;
        case 2:
            c->bit_length = get_bits(gb, 5);
            break;
        case 3:
            c->block_length = get_bits(gb, 4);
            return 1;
        }
    }

    return 0;
}

static int apac_decode(AVCodecContext *avctx, AVFrame *frame,
                       int *got_frame_ptr, AVPacket *pkt)
{
    APACContext *s = avctx->priv_data;
    GetBitContext *gb = &s->gb;
    int ret, n, buf_size, input_buf_size;
    const uint8_t *buf;
    int nb_samples;

    if (!pkt->size && s->bitstream_size <= 0) {
        *got_frame_ptr = 0;
        return 0;
    }

    buf_size = pkt->size;
    input_buf_size = buf_size;

    if (s->bitstream_index > 0 && s->bitstream_size > 0) {
        memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
        s->bitstream_index = 0;
    }

    if (s->bitstream_index + s->bitstream_size + buf_size > s->max_framesize) {
        s->bitstream = av_realloc_f(s->bitstream, s->bitstream_index +
                                    s->bitstream_size +
                                    buf_size + AV_INPUT_BUFFER_PADDING_SIZE,
                                    sizeof(*s->bitstream));
        if (!s->bitstream)
            return AVERROR(ENOMEM);
        s->max_framesize = s->bitstream_index + s->bitstream_size + buf_size;
    }
    if (pkt->data)
        memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size);
    buf                = &s->bitstream[s->bitstream_index];
    buf_size          += s->bitstream_size;
    s->bitstream_size  = buf_size;

    frame->nb_samples = s->bitstream_size * 16 * 8;
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
        return ret;

    if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
        return ret;

    skip_bits(gb, s->skip);
    s->skip = 0;

    while (get_bits_left(gb) > 0) {
        for (int ch = s->cur_ch; ch < avctx->ch_layout.nb_channels; ch++) {
            ChContext *c = &s->ch[ch];
            int16_t *dst16 = (int16_t *)c->block;
            uint8_t *dst8 = (uint8_t *)c->block;
            void *samples[4];

            samples[0] = &c->block[0];
            if (get_bits_left(gb) < 16 && pkt->size) {
                s->cur_ch = ch;
                goto end;
            }

            if (!c->have_code && get_code(c, gb))
                get_code(c, gb);
            c->have_code = 0;

            if (c->block_length <= 0)
                continue;

            if (c->bit_length < 0 ||
                c->bit_length > 17) {
                c->bit_length = avctx->bits_per_coded_sample;
                s->bitstream_index = 0;
                s->bitstream_size  = 0;
                return AVERROR_INVALIDDATA;
            }

            if (get_bits_left(gb) < c->block_length * c->bit_length && pkt->size) {
                c->have_code = 1;
                s->cur_ch = ch;
                goto end;
            }

            for (int i = 0; i < c->block_length; i++) {
                int val = get_bits_long(gb, c->bit_length);
                int delta = (val & 1) ? ~(val >> 1) : (val >> 1);
                int sample;

                delta += c->last_delta;
                sample = c->last_sample + delta;
                c->last_delta = delta;
                c->last_sample = sample;

                switch (avctx->sample_fmt) {
                case AV_SAMPLE_FMT_S16P:
                    dst16[i] = sample;
                    break;
                case AV_SAMPLE_FMT_U8P:
                    dst8[i] = sample;
                    break;
                }
            }

            av_audio_fifo_write(c->samples, samples, c->block_length);
        }

        s->cur_ch = 0;
    }
end:
    nb_samples = frame->nb_samples;
    for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
        nb_samples = FFMIN(av_audio_fifo_size(s->ch[ch].samples), nb_samples);

    frame->nb_samples = nb_samples;
    for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
        void *samples[1] = { frame->extended_data[ch] };
        av_audio_fifo_read(s->ch[ch].samples, samples, nb_samples);
    }

    s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8);
    n = get_bits_count(gb) / 8;

    if (nb_samples > 0 || pkt->size)
        *got_frame_ptr = 1;

    if (s->bitstream_size > 0) {
        s->bitstream_index += n;
        s->bitstream_size  -= n;
        return input_buf_size;
    }
    return n;
}

const FFCodec ff_apac_decoder = {
    .p.name           = "apac",
    CODEC_LONG_NAME("Marian's A-pac audio"),
    .p.type           = AVMEDIA_TYPE_AUDIO,
    .p.id             = AV_CODEC_ID_APAC,
    .priv_data_size   = sizeof(APACContext),
    .init             = apac_init,
    FF_CODEC_DECODE_CB(apac_decode),
    .close            = apac_close,
    .p.capabilities   = AV_CODEC_CAP_DELAY |
                        AV_CODEC_CAP_DR1 |
                        AV_CODEC_CAP_SUBFRAMES,
    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
    .p.sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
                                                        AV_SAMPLE_FMT_S16P,
                                                        AV_SAMPLE_FMT_NONE },
};