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 "codec_internal.h" 26#include "encode.h" 27#include "put_bits.h" 28#include "internal.h" 29#include "libavutil/crc.h" 30 31typedef struct TTAEncContext { 32 const AVCRC *crc_table; 33 int bps; 34 TTAChannel *ch_ctx; 35 TTAEncDSPContext dsp; 36} TTAEncContext; 37 38static av_cold int tta_encode_init(AVCodecContext *avctx) 39{ 40 TTAEncContext *s = avctx->priv_data; 41 42 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE); 43 44 switch (avctx->sample_fmt) { 45 case AV_SAMPLE_FMT_U8: 46 avctx->bits_per_raw_sample = 8; 47 break; 48 case AV_SAMPLE_FMT_S16: 49 avctx->bits_per_raw_sample = 16; 50 break; 51 case AV_SAMPLE_FMT_S32: 52 if (avctx->bits_per_raw_sample > 24) 53 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n"); 54 avctx->bits_per_raw_sample = 24; 55 } 56 57 s->bps = avctx->bits_per_raw_sample >> 3; 58 avctx->frame_size = 256 * avctx->sample_rate / 245; 59 60 s->ch_ctx = av_malloc_array(avctx->ch_layout.nb_channels, sizeof(*s->ch_ctx)); 61 if (!s->ch_ctx) 62 return AVERROR(ENOMEM); 63 64 ff_ttaencdsp_init(&s->dsp); 65 66 return 0; 67} 68 69static int32_t get_sample(const AVFrame *frame, int sample, 70 enum AVSampleFormat format) 71{ 72 int32_t ret; 73 74 if (format == AV_SAMPLE_FMT_U8) { 75 ret = frame->data[0][sample] - 0x80; 76 } else if (format == AV_SAMPLE_FMT_S16) { 77 const int16_t *ptr = (const int16_t *)frame->data[0]; 78 ret = ptr[sample]; 79 } else { 80 const int32_t *ptr = (const int32_t *)frame->data[0]; 81 ret = ptr[sample] >> 8; 82 } 83 84 return ret; 85} 86 87static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, 88 const AVFrame *frame, int *got_packet_ptr) 89{ 90 TTAEncContext *s = avctx->priv_data; 91 PutBitContext pb; 92 int ret, i, out_bytes, cur_chan, res, samples; 93 int64_t pkt_size = frame->nb_samples * 2LL * avctx->ch_layout.nb_channels * s->bps; 94 95pkt_alloc: 96 cur_chan = 0, res = 0, samples = 0; 97 if ((ret = ff_alloc_packet(avctx, avpkt, pkt_size)) < 0) 98 return ret; 99 init_put_bits(&pb, avpkt->data, avpkt->size); 100 101 // init per channel states 102 for (i = 0; i < avctx->ch_layout.nb_channels; i++) { 103 s->ch_ctx[i].predictor = 0; 104 ff_tta_filter_init(&s->ch_ctx[i].filter, ff_tta_filter_configs[s->bps - 1]); 105 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10); 106 } 107 108 for (i = 0; i < frame->nb_samples * avctx->ch_layout.nb_channels; i++) { 109 TTAChannel *c = &s->ch_ctx[cur_chan]; 110 TTAFilter *filter = &c->filter; 111 TTARice *rice = &c->rice; 112 uint32_t k, unary, outval; 113 int32_t value, temp; 114 115 value = get_sample(frame, samples++, avctx->sample_fmt); 116 117 if (avctx->ch_layout.nb_channels > 1) { 118 if (cur_chan < avctx->ch_layout.nb_channels - 1) 119 value = res = get_sample(frame, samples, avctx->sample_fmt) - value; 120 else 121 value -= res / 2; 122 } 123 124 temp = value; 125#define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k)) 126 switch (s->bps) { 127 case 1: value -= PRED(c->predictor, 4); break; 128 case 2: 129 case 3: value -= PRED(c->predictor, 5); break; 130 } 131 c->predictor = temp; 132 133 s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, &value, 134 filter->shift, filter->round); 135 outval = (value > 0) ? (value << 1) - 1: -value << 1; 136 137 k = rice->k0; 138 139 rice->sum0 += outval - (rice->sum0 >> 4); 140 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0]) 141 rice->k0--; 142 else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1]) 143 rice->k0++; 144 145 if (outval >= ff_tta_shift_1[k]) { 146 outval -= ff_tta_shift_1[k]; 147 k = rice->k1; 148 149 rice->sum1 += outval - (rice->sum1 >> 4); 150 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1]) 151 rice->k1--; 152 else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1]) 153 rice->k1++; 154 155 unary = 1 + (outval >> k); 156 if (unary + 100LL > put_bits_left(&pb)) { 157 if (pkt_size < INT_MAX/2) { 158 pkt_size *= 2; 159 av_packet_unref(avpkt); 160 goto pkt_alloc; 161 } else 162 return AVERROR(ENOMEM); 163 } 164 do { 165 if (unary > 31) { 166 put_bits(&pb, 31, 0x7FFFFFFF); 167 unary -= 31; 168 } else { 169 put_bits(&pb, unary, (1U << unary) - 1); 170 unary = 0; 171 } 172 } while (unary); 173 } 174 175 put_bits(&pb, 1, 0); 176 177 if (k) 178 put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1)); 179 180 if (cur_chan < avctx->ch_layout.nb_channels - 1) 181 cur_chan++; 182 else 183 cur_chan = 0; 184 } 185 186 flush_put_bits(&pb); 187 out_bytes = put_bytes_output(&pb); 188 put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX); 189 flush_put_bits(&pb); 190 191 avpkt->pts = frame->pts; 192 avpkt->size = out_bytes + 4; 193 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); 194 *got_packet_ptr = 1; 195 return 0; 196} 197 198static av_cold int tta_encode_close(AVCodecContext *avctx) 199{ 200 TTAEncContext *s = avctx->priv_data; 201 av_freep(&s->ch_ctx); 202 return 0; 203} 204 205const FFCodec ff_tta_encoder = { 206 .p.name = "tta", 207 .p.long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"), 208 .p.type = AVMEDIA_TYPE_AUDIO, 209 .p.id = AV_CODEC_ID_TTA, 210 .priv_data_size = sizeof(TTAEncContext), 211 .init = tta_encode_init, 212 .close = tta_encode_close, 213 FF_CODEC_ENCODE_CB(tta_encode_frame), 214 .p.capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME, 215 .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8, 216 AV_SAMPLE_FMT_S16, 217 AV_SAMPLE_FMT_S32, 218 AV_SAMPLE_FMT_NONE }, 219 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, 220}; 221