2 * TTA (The Lossless True Audio) encoder
4 * This file is part of FFmpeg.
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.
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.
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
21 #define BITSTREAM_WRITER_LE
23 #include "ttaencdsp.h"
27 #include "libavutil/crc.h"
29 typedef struct TTAEncContext {
30 const AVCRC *crc_table;
36 static av_cold int tta_encode_init(AVCodecContext *avctx)
38 TTAEncContext *s = avctx->priv_data;
40 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
42 switch (avctx->sample_fmt) {
43 case AV_SAMPLE_FMT_U8:
44 avctx->bits_per_raw_sample = 8;
46 case AV_SAMPLE_FMT_S16:
47 avctx->bits_per_raw_sample = 16;
49 case AV_SAMPLE_FMT_S32:
50 if (avctx->bits_per_raw_sample > 24)
51 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
52 avctx->bits_per_raw_sample = 24;
55 s->bps = avctx->bits_per_raw_sample >> 3;
56 avctx->frame_size = 256 * avctx->sample_rate / 245;
58 s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
60 return AVERROR(ENOMEM);
62 ff_ttaencdsp_init(&s->dsp);
67 static int32_t get_sample(const AVFrame *frame, int sample,
68 enum AVSampleFormat format)
72 if (format == AV_SAMPLE_FMT_U8) {
73 ret = frame->data[0][sample] - 0x80;
74 } else if (format == AV_SAMPLE_FMT_S16) {
75 const int16_t *ptr = (const int16_t *)frame->data[0];
78 const int32_t *ptr = (const int32_t *)frame->data[0];
79 ret = ptr[sample] >> 8;
85 static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
86 const AVFrame *frame, int *got_packet_ptr)
88 TTAEncContext *s = avctx->priv_data;
90 int ret, i, out_bytes, cur_chan, res, samples;
91 int64_t pkt_size = frame->nb_samples * 2LL * avctx->channels * s->bps;
94 cur_chan = 0, res = 0, samples = 0;
95 if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
97 init_put_bits(&pb, avpkt->data, avpkt->size);
99 // init per channel states
100 for (i = 0; i < avctx->channels; i++) {
101 s->ch_ctx[i].predictor = 0;
102 ff_tta_filter_init(&s->ch_ctx[i].filter, ff_tta_filter_configs[s->bps - 1]);
103 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
106 for (i = 0; i < frame->nb_samples * avctx->channels; i++) {
107 TTAChannel *c = &s->ch_ctx[cur_chan];
108 TTAFilter *filter = &c->filter;
109 TTARice *rice = &c->rice;
110 uint32_t k, unary, outval;
113 value = get_sample(frame, samples++, avctx->sample_fmt);
115 if (avctx->channels > 1) {
116 if (cur_chan < avctx->channels - 1)
117 value = res = get_sample(frame, samples, avctx->sample_fmt) - value;
123 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
125 case 1: value -= PRED(c->predictor, 4); break;
127 case 3: value -= PRED(c->predictor, 5); break;
131 s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, &value,
132 filter->shift, filter->round);
133 outval = (value > 0) ? (value << 1) - 1: -value << 1;
137 rice->sum0 += outval - (rice->sum0 >> 4);
138 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
140 else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
143 if (outval >= ff_tta_shift_1[k]) {
144 outval -= ff_tta_shift_1[k];
147 rice->sum1 += outval - (rice->sum1 >> 4);
148 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
150 else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
153 unary = 1 + (outval >> k);
154 if (unary + 100LL > put_bits_left(&pb)) {
155 if (pkt_size < INT_MAX/2) {
157 av_packet_unref(avpkt);
160 return AVERROR(ENOMEM);
164 put_bits(&pb, 31, 0x7FFFFFFF);
167 put_bits(&pb, unary, (1 << unary) - 1);
176 put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
178 if (cur_chan < avctx->channels - 1)
185 out_bytes = put_bits_count(&pb) >> 3;
186 put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
189 avpkt->pts = frame->pts;
190 avpkt->size = out_bytes + 4;
191 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
196 static av_cold int tta_encode_close(AVCodecContext *avctx)
198 TTAEncContext *s = avctx->priv_data;
199 av_freep(&s->ch_ctx);
203 AVCodec ff_tta_encoder = {
205 .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
206 .type = AVMEDIA_TYPE_AUDIO,
207 .id = AV_CODEC_ID_TTA,
208 .priv_data_size = sizeof(TTAEncContext),
209 .init = tta_encode_init,
210 .close = tta_encode_close,
211 .encode2 = tta_encode_frame,
212 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_LOSSLESS,
213 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
216 AV_SAMPLE_FMT_NONE },