1/*
2 * ADX ADPCM codecs
3 * Copyright (c) 2001,2003 BERO
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
23#include "adx.h"
24#include "bytestream.h"
25#include "internal.h"
26#include "put_bits.h"
27
28/**
29 * @file
30 * SEGA CRI adx codecs.
31 *
32 * Reference documents:
33 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
34 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
35 */
36
37static void adx_encode(ADXContext *c, uint8_t *adx, const int16_t *wav,
38                       ADXChannelState *prev, int channels)
39{
40    PutBitContext pb;
41    int scale;
42    int i, j;
43    int s0, s1, s2, d;
44    int max = 0;
45    int min = 0;
46    int data[BLOCK_SAMPLES];
47
48    s1 = prev->s1;
49    s2 = prev->s2;
50    for (i = 0, j = 0; j < 32; i += channels, j++) {
51        s0 = wav[i];
52        d = ((s0 << COEFF_BITS) - c->coeff[0] * s1 - c->coeff[1] * s2) >> COEFF_BITS;
53        data[j] = d;
54        if (max < d)
55            max = d;
56        if (min > d)
57            min = d;
58        s2 = s1;
59        s1 = s0;
60    }
61    prev->s1 = s1;
62    prev->s2 = s2;
63
64    if (max == 0 && min == 0) {
65        memset(adx, 0, BLOCK_SIZE);
66        return;
67    }
68
69    if (max / 7 > -min / 8)
70        scale = max / 7;
71    else
72        scale = -min / 8;
73
74    if (scale == 0)
75        scale = 1;
76
77    AV_WB16(adx, scale);
78
79    init_put_bits(&pb, adx + 2, 16);
80    for (i = 0; i < BLOCK_SAMPLES; i++)
81        put_sbits(&pb, 4, av_clip(data[i] / scale, -8, 7));
82    flush_put_bits(&pb);
83}
84
85#define HEADER_SIZE 36
86
87static int adx_encode_header(AVCodecContext *avctx, uint8_t *buf, int bufsize)
88{
89    ADXContext *c = avctx->priv_data;
90
91    bytestream_put_be16(&buf, 0x8000);              /* header signature */
92    bytestream_put_be16(&buf, HEADER_SIZE - 4);     /* copyright offset */
93    bytestream_put_byte(&buf, 3);                   /* encoding */
94    bytestream_put_byte(&buf, BLOCK_SIZE);          /* block size */
95    bytestream_put_byte(&buf, 4);                   /* sample size */
96    bytestream_put_byte(&buf, avctx->channels);     /* channels */
97    bytestream_put_be32(&buf, avctx->sample_rate);  /* sample rate */
98    bytestream_put_be32(&buf, 0);                   /* total sample count */
99    bytestream_put_be16(&buf, c->cutoff);           /* cutoff frequency */
100    bytestream_put_byte(&buf, 3);                   /* version */
101    bytestream_put_byte(&buf, 0);                   /* flags */
102    bytestream_put_be32(&buf, 0);                   /* unknown */
103    bytestream_put_be32(&buf, 0);                   /* loop enabled */
104    bytestream_put_be16(&buf, 0);                   /* padding */
105    bytestream_put_buffer(&buf, "(c)CRI", 6);       /* copyright signature */
106
107    return HEADER_SIZE;
108}
109
110static av_cold int adx_encode_init(AVCodecContext *avctx)
111{
112    ADXContext *c = avctx->priv_data;
113
114    if (avctx->channels > 2) {
115        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
116        return AVERROR(EINVAL);
117    }
118    avctx->frame_size = BLOCK_SAMPLES;
119
120    /* the cutoff can be adjusted, but this seems to work pretty well */
121    c->cutoff = 500;
122    ff_adx_calculate_coeffs(c->cutoff, avctx->sample_rate, COEFF_BITS, c->coeff);
123
124    return 0;
125}
126
127static int adx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
128                            const AVFrame *frame, int *got_packet_ptr)
129{
130    ADXContext *c          = avctx->priv_data;
131    const int16_t *samples = (const int16_t *)frame->data[0];
132    uint8_t *dst;
133    int ch, out_size, ret;
134
135    out_size = BLOCK_SIZE * avctx->channels + !c->header_parsed * HEADER_SIZE;
136    if ((ret = ff_alloc_packet2(avctx, avpkt, out_size)) < 0)
137        return ret;
138    dst = avpkt->data;
139
140    if (!c->header_parsed) {
141        int hdrsize;
142        if ((hdrsize = adx_encode_header(avctx, dst, avpkt->size)) < 0) {
143            av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
144            return AVERROR(EINVAL);
145        }
146        dst      += hdrsize;
147        c->header_parsed = 1;
148    }
149
150    for (ch = 0; ch < avctx->channels; ch++) {
151        adx_encode(c, dst, samples + ch, &c->prev[ch], avctx->channels);
152        dst += BLOCK_SIZE;
153    }
154
155    *got_packet_ptr = 1;
156    return 0;
157}
158
159AVCodec ff_adpcm_adx_encoder = {
160    .name           = "adpcm_adx",
161    .long_name      = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
162    .type           = AVMEDIA_TYPE_AUDIO,
163    .id             = AV_CODEC_ID_ADPCM_ADX,
164    .priv_data_size = sizeof(ADXContext),
165    .init           = adx_encode_init,
166    .encode2        = adx_encode_frame,
167    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
168                                                      AV_SAMPLE_FMT_NONE },
169};
170