FFmpeg
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 "codec_internal.h"
26 #include "encode.h"
27 #include "put_bits.h"
28 #include "internal.h"
29 #include "libavutil/crc.h"
30 
31 typedef struct TTAEncContext {
32  const AVCRC *crc_table;
33  int bps;
37 
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 
69 static int32_t get_sample(const AVFrame *frame, int sample,
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 
87 static 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 
95 pkt_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 
199 {
200  TTAEncContext *s = avctx->priv_data;
201  av_freep(&s->ch_ctx);
202  return 0;
203 }
204 
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),
212  .close = tta_encode_close,
214  .p.capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
215  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
219  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
220 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1026
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
tta_encode_frame
static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: ttaenc.c:87
put_bits32
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:291
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:89
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:998
AVCRC
uint32_t AVCRC
Definition: crc.h:46
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
ff_tta_filter_init
void ff_tta_filter_init(TTAFilter *c, int32_t shift)
Definition: ttadata.c:50
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:221
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
encode.h
FFCodec
Definition: codec_internal.h:112
ff_ttaencdsp_init
av_cold void ff_ttaencdsp_init(TTAEncDSPContext *c)
Definition: ttaencdsp.c:53
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
ff_tta_rice_init
void ff_tta_rice_init(TTARice *c, uint32_t k0, uint32_t k1)
Definition: ttadata.c:42
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
TTARice::k1
uint32_t k1
Definition: ttadata.h:35
init
static int init
Definition: av_tx.c:47
crc.h
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
U
#define U(x)
Definition: vp56_arith.h:37
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2056
put_bits_left
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:125
TTARice::sum1
uint32_t sum1
Definition: ttadata.h:35
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:263
ff_samples_to_time_base
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:173
av_cold
#define av_cold
Definition: attributes.h:90
tta_encode_close
static av_cold int tta_encode_close(AVCodecContext *avctx)
Definition: ttaenc.c:198
AV_CODEC_ID_TTA
@ AV_CODEC_ID_TTA
Definition: codec_id.h:449
TTAEncContext::ch_ctx
TTAChannel * ch_ctx
Definition: ttaenc.c:34
s
#define s(width, name)
Definition: cbs_vp9.c:256
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
TTAEncContext::dsp
TTAEncDSPContext dsp
Definition: ttaenc.c:35
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1448
TTAEncContext::bps
int bps
Definition: ttaenc.c:33
PutBitContext
Definition: put_bits.h:50
TTARice::sum0
uint32_t sum0
Definition: ttadata.h:35
TTAFilter
Definition: ttadata.h:27
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
codec_internal.h
PRED
#define PRED(x, k)
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1014
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
TTAEncContext::crc_table
const AVCRC * crc_table
Definition: ttaenc.c:32
ff_tta_shift_1
const uint32_t ff_tta_shift_1[]
Definition: ttadata.c:24
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
ff_tta_shift_16
const uint32_t *const ff_tta_shift_16
Definition: ttadata.c:38
ff_tta_filter_configs
const uint8_t ff_tta_filter_configs[]
Definition: ttadata.c:40
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
ttadata.h
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
TTAChannel
Definition: ttadata.h:38
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
avcodec.h
TTARice::k0
uint32_t k0
Definition: ttadata.h:35
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
TTARice
Definition: ttadata.h:34
AVCodecContext
main external API structure.
Definition: avcodec.h:389
av_crc
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:392
temp
else temp
Definition: vf_mcdeint.c:248
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ff_tta_encoder
const FFCodec ff_tta_encoder
Definition: ttaenc.c:205
AV_CRC_32_IEEE_LE
@ AV_CRC_32_IEEE_LE
Definition: crc.h:53
TTAEncDSPContext
Definition: ttaencdsp.h:24
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
TTAEncContext
Definition: ttaenc.c:31
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
get_sample
static int32_t get_sample(const AVFrame *frame, int sample, enum AVSampleFormat format)
Definition: ttaenc.c:69
ttaencdsp.h
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:87
put_bits.h
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:35
tta_encode_init
static av_cold int tta_encode_init(AVCodecContext *avctx)
Definition: ttaenc.c:38