FFmpeg  4.4.9
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 "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;
35 
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  if (avctx->sample_rate > 0x7FFFFF) {
56  av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
57  return AVERROR(EINVAL);
58  }
59 
60  s->bps = avctx->bits_per_raw_sample >> 3;
61  avctx->frame_size = 256 * avctx->sample_rate / 245;
62 
63  s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
64  if (!s->ch_ctx)
65  return AVERROR(ENOMEM);
66 
67  ff_ttaencdsp_init(&s->dsp);
68 
69  return 0;
70 }
71 
72 static int32_t get_sample(const AVFrame *frame, int sample,
74 {
75  int32_t ret;
76 
77  if (format == AV_SAMPLE_FMT_U8) {
78  ret = frame->data[0][sample] - 0x80;
79  } else if (format == AV_SAMPLE_FMT_S16) {
80  const int16_t *ptr = (const int16_t *)frame->data[0];
81  ret = ptr[sample];
82  } else {
83  const int32_t *ptr = (const int32_t *)frame->data[0];
84  ret = ptr[sample] >> 8;
85  }
86 
87  return ret;
88 }
89 
90 static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
91  const AVFrame *frame, int *got_packet_ptr)
92 {
93  TTAEncContext *s = avctx->priv_data;
94  PutBitContext pb;
95  int ret, i, out_bytes, cur_chan, res, samples;
96  int64_t pkt_size = frame->nb_samples * 2LL * avctx->channels * s->bps + 4;
97  int64_t bits_left;
98 
99 pkt_alloc:
100  cur_chan = 0, res = 0, samples = 0;
101  if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
102  return ret;
103  init_put_bits(&pb, avpkt->data, avpkt->size);
104  bits_left = 8 * (pkt_size - 4);
105 
106  // init per channel states
107  for (i = 0; i < avctx->channels; i++) {
108  s->ch_ctx[i].predictor = 0;
109  ff_tta_filter_init(&s->ch_ctx[i].filter, ff_tta_filter_configs[s->bps - 1]);
110  ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
111  }
112 
113  for (i = 0; i < frame->nb_samples * avctx->channels; i++) {
114  TTAChannel *c = &s->ch_ctx[cur_chan];
115  TTAFilter *filter = &c->filter;
116  TTARice *rice = &c->rice;
117  uint32_t k, unary = 0, outval;
118  int32_t value, temp;
119 
120  value = get_sample(frame, samples++, avctx->sample_fmt);
121 
122  if (avctx->channels > 1) {
123  if (cur_chan < avctx->channels - 1)
124  value = res = get_sample(frame, samples, avctx->sample_fmt) - value;
125  else
126  value -= res / 2;
127  }
128 
129  temp = value;
130 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
131  switch (s->bps) {
132  case 1: value -= PRED(c->predictor, 4); break;
133  case 2:
134  case 3: value -= PRED(c->predictor, 5); break;
135  }
136  c->predictor = temp;
137 
138  s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, &value,
139  filter->shift, filter->round);
140  outval = (value > 0) ? (value << 1) - 1: -value << 1;
141 
142  k = rice->k0;
143 
144  rice->sum0 += outval - (rice->sum0 >> 4);
145  if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
146  rice->k0--;
147  else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
148  rice->k0++;
149 
150  if (outval >= ff_tta_shift_1[k]) {
151  outval -= ff_tta_shift_1[k];
152  k = rice->k1;
153 
154  rice->sum1 += outval - (rice->sum1 >> 4);
155  if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
156  rice->k1--;
157  else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
158  rice->k1++;
159 
160  unary = 1 + (outval >> k);
161  }
162 
163  if (unary + 1LL + k > bits_left) {
164  if (pkt_size < INT_MAX/2) {
165  pkt_size *= 2;
166  av_packet_unref(avpkt);
167  goto pkt_alloc;
168  } else
169  return AVERROR(ENOMEM);
170  }
171  bits_left -= unary + 1 + k;
172  while (unary) {
173  if (unary > 31) {
174  put_bits(&pb, 31, 0x7FFFFFFF);
175  unary -= 31;
176  } else {
177  put_bits(&pb, unary, (1U << unary) - 1);
178  unary = 0;
179  }
180  }
181 
182  put_bits(&pb, 1, 0);
183 
184  if (k)
185  put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
186 
187  if (cur_chan < avctx->channels - 1)
188  cur_chan++;
189  else
190  cur_chan = 0;
191  }
192 
193  flush_put_bits(&pb);
194  out_bytes = put_bits_count(&pb) >> 3;
195  put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
196  flush_put_bits(&pb);
197 
198  avpkt->pts = frame->pts;
199  avpkt->size = out_bytes + 4;
201  *got_packet_ptr = 1;
202  return 0;
203 }
204 
206 {
207  TTAEncContext *s = avctx->priv_data;
208  av_freep(&s->ch_ctx);
209  return 0;
210 }
211 
213  .name = "tta",
214  .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
215  .type = AVMEDIA_TYPE_AUDIO,
216  .id = AV_CODEC_ID_TTA,
217  .priv_data_size = sizeof(TTAEncContext),
219  .close = tta_encode_close,
220  .encode2 = tta_encode_frame,
221  .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
222  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
226 };
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
static const char *const format[]
Definition: af_aiir.c:457
channels
Definition: aptx.h:33
#define av_cold
Definition: attributes.h:88
AV_SAMPLE_FMT_U8
int32_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
static av_always_inline void filter(int16_t *output, ptrdiff_t out_stride, const int16_t *low, ptrdiff_t low_stride, const int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhddsp.c:27
long long int64_t
Definition: coverity.c:34
Public header for CRC hash function implementation.
static AVFrame * frame
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:34
double value
Definition: eval.c:100
#define sample
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:82
@ AV_CODEC_ID_TTA
Definition: codec_id.h:446
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
uint32_t AVCRC
Definition: crc.h:47
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:394
@ AV_CRC_32_IEEE_LE
Definition: crc.h:54
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
int i
Definition: input.c:407
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
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:277
static av_cold int tta_encode_init(AVCodecContext *avctx)
Definition: ttaenc.c:36
static int32_t get_sample(const AVFrame *frame, int sample, enum AVSampleFormat format)
Definition: ttaenc.c:72
static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: ttaenc.c:90
static av_cold int tta_encode_close(AVCodecContext *avctx)
Definition: ttaenc.c:205
#define PRED(x, k)
AVCodec ff_tta_encoder
Definition: ttaenc.c:212
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:263
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:76
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
main external API structure.
Definition: avcodec.h:536
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1751
int sample_rate
samples per second
Definition: avcodec.h:1196
int channels
number of audio channels
Definition: avcodec.h:1197
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
TTAEncDSPContext dsp
Definition: ttaenc.c:33
const AVCRC * crc_table
Definition: ttaenc.c:30
TTAChannel * ch_ctx
Definition: ttaenc.c:32
uint32_t k1
Definition: ttadata.h:35
uint32_t sum0
Definition: ttadata.h:35
uint32_t k0
Definition: ttadata.h:35
uint32_t sum1
Definition: ttadata.h:35
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
const uint32_t *const ff_tta_shift_16
Definition: ttadata.c:37
const uint32_t ff_tta_shift_1[]
Definition: ttadata.c:23
void ff_tta_rice_init(TTARice *c, uint32_t k0, uint32_t k1)
Definition: ttadata.c:41
void ff_tta_filter_init(TTAFilter *c, int32_t shift)
Definition: ttadata.c:49
const uint8_t ff_tta_filter_configs[]
Definition: ttadata.c:39
av_cold void ff_ttaencdsp_init(TTAEncDSPContext *c)
Definition: ttaencdsp.c:56
else temp
Definition: vf_mcdeint.c:259
static double c[64]