1 /*
2 * TTA (The Lossless True Audio) encoder
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 #define BITSTREAM_WRITER_LE
22 #include "ttadata.h"
23 #include "ttaencdsp.h"
24 #include "avcodec.h"
25 #include "put_bits.h"
26 #include "internal.h"
27 #include "libavutil/crc.h"
28
29 typedef struct TTAEncContext {
30 const AVCRC *crc_table;
31 int bps;
32 TTAChannel *ch_ctx;
33 TTAEncDSPContext dsp;
34 } TTAEncContext;
35
tta_encode_init(AVCodecContext * avctx)36 static av_cold int tta_encode_init(AVCodecContext *avctx)
37 {
38 TTAEncContext *s = avctx->priv_data;
39
40 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
41
42 switch (avctx->sample_fmt) {
43 case AV_SAMPLE_FMT_U8:
44 avctx->bits_per_raw_sample = 8;
45 break;
46 case AV_SAMPLE_FMT_S16:
47 avctx->bits_per_raw_sample = 16;
48 break;
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;
53 }
54
55 s->bps = avctx->bits_per_raw_sample >> 3;
56 avctx->frame_size = 256 * avctx->sample_rate / 245;
57
58 s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
59 if (!s->ch_ctx)
60 return AVERROR(ENOMEM);
61
62 ff_ttaencdsp_init(&s->dsp);
63
64 return 0;
65 }
66
get_sample(const AVFrame * frame,int sample,enum AVSampleFormat format)67 static int32_t get_sample(const AVFrame *frame, int sample,
68 enum AVSampleFormat format)
69 {
70 int32_t ret;
71
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];
76 ret = ptr[sample];
77 } else {
78 const int32_t *ptr = (const int32_t *)frame->data[0];
79 ret = ptr[sample] >> 8;
80 }
81
82 return ret;
83 }
84
tta_encode_frame(AVCodecContext * avctx,AVPacket * avpkt,const AVFrame * frame,int * got_packet_ptr)85 static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
86 const AVFrame *frame, int *got_packet_ptr)
87 {
88 TTAEncContext *s = avctx->priv_data;
89 PutBitContext pb;
90 int ret, i, out_bytes, cur_chan, res, samples;
91 int64_t pkt_size = frame->nb_samples * 2LL * avctx->channels * s->bps;
92
93 pkt_alloc:
94 cur_chan = 0, res = 0, samples = 0;
95 if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
96 return ret;
97 init_put_bits(&pb, avpkt->data, avpkt->size);
98
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);
104 }
105
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;
111 int32_t value, temp;
112
113 value = get_sample(frame, samples++, avctx->sample_fmt);
114
115 if (avctx->channels > 1) {
116 if (cur_chan < avctx->channels - 1)
117 value = res = get_sample(frame, samples, avctx->sample_fmt) - value;
118 else
119 value -= res / 2;
120 }
121
122 temp = value;
123 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
124 switch (s->bps) {
125 case 1: value -= PRED(c->predictor, 4); break;
126 case 2:
127 case 3: value -= PRED(c->predictor, 5); break;
128 }
129 c->predictor = temp;
130
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;
134
135 k = rice->k0;
136
137 rice->sum0 += outval - (rice->sum0 >> 4);
138 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
139 rice->k0--;
140 else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
141 rice->k0++;
142
143 if (outval >= ff_tta_shift_1[k]) {
144 outval -= ff_tta_shift_1[k];
145 k = rice->k1;
146
147 rice->sum1 += outval - (rice->sum1 >> 4);
148 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
149 rice->k1--;
150 else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
151 rice->k1++;
152
153 unary = 1 + (outval >> k);
154 if (unary + 100LL > put_bits_left(&pb)) {
155 if (pkt_size < INT_MAX/2) {
156 pkt_size *= 2;
157 av_packet_unref(avpkt);
158 goto pkt_alloc;
159 } else
160 return AVERROR(ENOMEM);
161 }
162 do {
163 if (unary > 31) {
164 put_bits(&pb, 31, 0x7FFFFFFF);
165 unary -= 31;
166 } else {
167 put_bits(&pb, unary, (1U << unary) - 1);
168 unary = 0;
169 }
170 } while (unary);
171 }
172
173 put_bits(&pb, 1, 0);
174
175 if (k)
176 put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
177
178 if (cur_chan < avctx->channels - 1)
179 cur_chan++;
180 else
181 cur_chan = 0;
182 }
183
184 flush_put_bits(&pb);
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);
187 flush_put_bits(&pb);
188
189 avpkt->pts = frame->pts;
190 avpkt->size = out_bytes + 4;
191 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
192 *got_packet_ptr = 1;
193 return 0;
194 }
195
tta_encode_close(AVCodecContext * avctx)196 static av_cold int tta_encode_close(AVCodecContext *avctx)
197 {
198 TTAEncContext *s = avctx->priv_data;
199 av_freep(&s->ch_ctx);
200 return 0;
201 }
202
203 AVCodec ff_tta_encoder = {
204 .name = "tta",
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,
213 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
214 AV_SAMPLE_FMT_S16,
215 AV_SAMPLE_FMT_S32,
216 AV_SAMPLE_FMT_NONE },
217 };
218