FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ttaenc.c
Go to the documentation of this file.
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 "avcodec.h"
24 #include "put_bits.h"
25 #include "internal.h"
26 #include "libavutil/crc.h"
27 
28 typedef struct TTAEncContext {
29  const AVCRC *crc_table;
30  int bps;
33 
35 {
36  TTAEncContext *s = avctx->priv_data;
37 
39 
40  switch (avctx->sample_fmt) {
41  case AV_SAMPLE_FMT_U8:
42  avctx->bits_per_raw_sample = 8;
43  break;
44  case AV_SAMPLE_FMT_S16:
45  avctx->bits_per_raw_sample = 16;
46  break;
47  case AV_SAMPLE_FMT_S32:
48  if (avctx->bits_per_raw_sample > 24)
49  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
50  avctx->bits_per_raw_sample = 24;
51  }
52 
53  s->bps = avctx->bits_per_raw_sample >> 3;
54  avctx->frame_size = 256 * avctx->sample_rate / 245;
55 
56  s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
57  if (!s->ch_ctx)
58  return AVERROR(ENOMEM);
59 
60  return 0;
61 }
62 
63 static inline void ttafilter_process(TTAFilter *c, int32_t *in)
64 {
65  register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
66 
67  if (c->error < 0) {
68  qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
69  qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
70  } else if (c->error > 0) {
71  qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
72  qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
73  }
74 
75  sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
76  dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
77 
78  dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
79  dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
80 
81  dx[4] = ((dl[4] >> 30) | 1);
82  dx[5] = ((dl[5] >> 30) | 2) & ~1;
83  dx[6] = ((dl[6] >> 30) | 2) & ~1;
84  dx[7] = ((dl[7] >> 30) | 4) & ~3;
85 
86  dl[4] = -dl[5]; dl[5] = -dl[6];
87  dl[6] = *in - dl[7]; dl[7] = *in;
88  dl[5] += dl[6]; dl[4] += dl[5];
89 
90  *in -= (sum >> c->shift);
91  c->error = *in;
92 }
93 
94 static int32_t get_sample(const AVFrame *frame, int sample,
96 {
97  int32_t ret;
98 
99  if (format == AV_SAMPLE_FMT_U8) {
100  ret = frame->data[0][sample] - 0x80;
101  } else if (format == AV_SAMPLE_FMT_S16) {
102  const int16_t *ptr = (const int16_t *)frame->data[0];
103  ret = ptr[sample];
104  } else {
105  const int32_t *ptr = (const int32_t *)frame->data[0];
106  ret = ptr[sample] >> 8;
107  }
108 
109  return ret;
110 }
111 
112 static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
113  const AVFrame *frame, int *got_packet_ptr)
114 {
115  TTAEncContext *s = avctx->priv_data;
116  PutBitContext pb;
117  int ret, i, out_bytes, cur_chan, res, samples;
118  int64_t pkt_size = frame->nb_samples * 2LL * avctx->channels * s->bps;
119 
120 pkt_alloc:
121  cur_chan = 0, res = 0, samples = 0;
122  if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
123  return ret;
124  init_put_bits(&pb, avpkt->data, avpkt->size);
125 
126  // init per channel states
127  for (i = 0; i < avctx->channels; i++) {
128  s->ch_ctx[i].predictor = 0;
130  ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
131  }
132 
133  for (i = 0; i < frame->nb_samples * avctx->channels; i++) {
134  TTAChannel *c = &s->ch_ctx[cur_chan];
135  TTAFilter *filter = &c->filter;
136  TTARice *rice = &c->rice;
137  uint32_t k, unary, outval;
138  int32_t value, temp;
139 
140  value = get_sample(frame, samples++, avctx->sample_fmt);
141 
142  if (avctx->channels > 1) {
143  if (cur_chan < avctx->channels - 1)
144  value = res = get_sample(frame, samples, avctx->sample_fmt) - value;
145  else
146  value -= res / 2;
147  }
148 
149  temp = value;
150 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
151  switch (s->bps) {
152  case 1: value -= PRED(c->predictor, 4); break;
153  case 2:
154  case 3: value -= PRED(c->predictor, 5); break;
155  }
156  c->predictor = temp;
157 
158  ttafilter_process(filter, &value);
159  outval = (value > 0) ? (value << 1) - 1: -value << 1;
160 
161  k = rice->k0;
162 
163  rice->sum0 += outval - (rice->sum0 >> 4);
164  if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
165  rice->k0--;
166  else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
167  rice->k0++;
168 
169  if (outval >= ff_tta_shift_1[k]) {
170  outval -= ff_tta_shift_1[k];
171  k = rice->k1;
172 
173  rice->sum1 += outval - (rice->sum1 >> 4);
174  if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
175  rice->k1--;
176  else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
177  rice->k1++;
178 
179  unary = 1 + (outval >> k);
180  if (unary + 100LL > put_bits_left(&pb)) {
181  if (pkt_size < INT_MAX/2) {
182  pkt_size *= 2;
183  av_packet_unref(avpkt);
184  goto pkt_alloc;
185  } else
186  return AVERROR(ENOMEM);
187  }
188  do {
189  if (unary > 31) {
190  put_bits(&pb, 31, 0x7FFFFFFF);
191  unary -= 31;
192  } else {
193  put_bits(&pb, unary, (1 << unary) - 1);
194  unary = 0;
195  }
196  } while (unary);
197  }
198 
199  put_bits(&pb, 1, 0);
200 
201  if (k)
202  put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
203 
204  if (cur_chan < avctx->channels - 1)
205  cur_chan++;
206  else
207  cur_chan = 0;
208  }
209 
210  flush_put_bits(&pb);
211  out_bytes = put_bits_count(&pb) >> 3;
212  put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
213  flush_put_bits(&pb);
214 
215  avpkt->pts = frame->pts;
216  avpkt->size = out_bytes + 4;
217  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
218  *got_packet_ptr = 1;
219  return 0;
220 }
221 
223 {
224  TTAEncContext *s = avctx->priv_data;
225  av_freep(&s->ch_ctx);
226  return 0;
227 }
228 
230  .name = "tta",
231  .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
232  .type = AVMEDIA_TYPE_AUDIO,
233  .id = AV_CODEC_ID_TTA,
234  .priv_data_size = sizeof(TTAEncContext),
236  .close = tta_encode_close,
237  .encode2 = tta_encode_frame,
239  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
243 };
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:210
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:180
static av_cold int tta_encode_init(AVCodecContext *avctx)
Definition: ttaenc.c:34
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
else temp
Definition: vf_mcdeint.c:259
AVCodec ff_tta_encoder
Definition: ttaenc.c:229
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
void ff_tta_rice_init(TTARice *c, uint32_t k0, uint32_t k1)
Definition: ttadata.c:40
int size
Definition: avcodec.h:1575
int32_t predictor
Definition: ttadata.h:39
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3039
#define sample
AVCodec.
Definition: avcodec.h:3531
static av_cold int tta_encode_close(AVCodecContext *avctx)
Definition: ttaenc.c:222
int32_t error
Definition: ttadata.h:28
int32_t round
Definition: ttadata.h:28
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2408
#define av_cold
Definition: attributes.h:82
AV_SAMPLE_FMT_U8
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1592
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:264
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:80
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1574
int32_t qm[MAX_ORDER]
Definition: ttadata.h:29
int32_t shift
Definition: ttadata.h:28
signed 32 bits
Definition: samplefmt.h:62
#define av_log(a,...)
const AVCRC * crc_table
Definition: ttaenc.c:29
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
#define AVERROR(e)
Definition: error.h:43
#define PRED(x, k)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
static int32_t get_sample(const AVFrame *frame, int sample, enum AVSampleFormat format)
Definition: ttaenc.c:94
const char * name
Name of the codec implementation.
Definition: avcodec.h:3538
uint32_t sum1
Definition: ttadata.h:35
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
const uint32_t ff_tta_shift_1[]
Definition: ttadata.c:23
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:980
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
const uint32_t *const ff_tta_shift_16
Definition: ttadata.c:36
int32_t
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:357
uint32_t k1
Definition: ttadata.h:35
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2420
TTAChannel * ch_ctx
Definition: ttaenc.c:31
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int sample_rate
samples per second
Definition: avcodec.h:2400
const uint8_t ff_tta_filter_configs[]
Definition: ttadata.c:38
main external API structure.
Definition: avcodec.h:1639
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:549
static const char * format
Definition: movenc-test.c:47
int32_t dx[MAX_ORDER]
Definition: ttadata.h:30
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static void ttafilter_process(TTAFilter *c, int32_t *in)
Definition: ttaenc.c:63
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1683
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:194
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:343
uint32_t k0
Definition: ttadata.h:35
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
Definition: avcodec.h:1037
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void * priv_data
Definition: avcodec.h:1681
TTAFilter filter
Definition: ttadata.h:40
int channels
number of audio channels
Definition: avcodec.h:2401
int32_t dl[MAX_ORDER]
Definition: ttadata.h:31
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:248
#define av_malloc_array(a, b)
TTARice rice
Definition: ttadata.h:41
void ff_tta_filter_init(TTAFilter *c, int32_t shift)
Definition: ttadata.c:48
This structure stores compressed data.
Definition: avcodec.h:1551
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:237
uint32_t AVCRC
Definition: crc.h:35
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1567
uint32_t sum0
Definition: ttadata.h:35
static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: ttaenc.c:112
bitstream writer API