FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/crc.h"
29#include "libavutil/mem.h"
30
37
39{
40 TTAEncContext *s = avctx->priv_data;
41
43
44 switch (avctx->sample_fmt) {
46 avctx->bits_per_raw_sample = 8;
47 break;
49 avctx->bits_per_raw_sample = 16;
50 break;
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 if (avctx->sample_rate > 0x7FFFFF) {
58 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
59 return AVERROR(EINVAL);
60 }
61
62 s->bps = avctx->bits_per_raw_sample >> 3;
63 avctx->frame_size = 256 * avctx->sample_rate / 245;
64
65 s->ch_ctx = av_malloc_array(avctx->ch_layout.nb_channels, sizeof(*s->ch_ctx));
66 if (!s->ch_ctx)
67 return AVERROR(ENOMEM);
68
69 ff_ttaencdsp_init(&s->dsp);
70
71 return 0;
72}
73
76{
78
79 if (format == AV_SAMPLE_FMT_U8) {
80 ret = frame->data[0][sample] - 0x80;
81 } else if (format == AV_SAMPLE_FMT_S16) {
82 const int16_t *ptr = (const int16_t *)frame->data[0];
83 ret = ptr[sample];
84 } else {
85 const int32_t *ptr = (const int32_t *)frame->data[0];
86 ret = ptr[sample] >> 8;
87 }
88
89 return ret;
90}
91
92static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
93 const AVFrame *frame, int *got_packet_ptr)
94{
95 TTAEncContext *s = avctx->priv_data;
97 int ret, i, out_bytes, cur_chan, res, samples;
98 int64_t pkt_size = frame->nb_samples * 2LL * avctx->ch_layout.nb_channels * s->bps + 4;
100
101pkt_alloc:
102 cur_chan = 0, res = 0, samples = 0;
103 if ((ret = ff_alloc_packet(avctx, avpkt, pkt_size)) < 0)
104 return ret;
105 init_put_bits(&pb, avpkt->data, avpkt->size);
106 bits_left = 8 * (pkt_size - 4);
107
108 // init per channel states
109 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
110 s->ch_ctx[i].predictor = 0;
111 ff_tta_filter_init(&s->ch_ctx[i].filter, ff_tta_filter_configs[s->bps - 1]);
112 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
113 }
114
115 for (i = 0; i < frame->nb_samples * avctx->ch_layout.nb_channels; i++) {
116 TTAChannel *c = &s->ch_ctx[cur_chan];
117 TTAFilter *filter = &c->filter;
118 TTARice *rice = &c->rice;
119 uint32_t k, unary = 0, outval;
121
122 value = get_sample(frame, samples++, avctx->sample_fmt);
123
124 if (avctx->ch_layout.nb_channels > 1) {
125 if (cur_chan < avctx->ch_layout.nb_channels - 1)
126 value = res = get_sample(frame, samples, avctx->sample_fmt) - value;
127 else
128 value -= res / 2;
129 }
130
131 temp = value;
132#define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
133 switch (s->bps) {
134 case 1: value -= PRED(c->predictor, 4); break;
135 case 2:
136 case 3: value -= PRED(c->predictor, 5); break;
137 }
138 c->predictor = temp;
139
140 value = s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, value,
141 filter->shift, filter->round);
142 outval = (value > 0) ? (value << 1) - 1: -value << 1;
143
144 k = rice->k0;
145
146 rice->sum0 += outval - (rice->sum0 >> 4);
147 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
148 rice->k0--;
149 else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
150 rice->k0++;
151
152 if (outval >= ff_tta_shift_1[k]) {
153 outval -= ff_tta_shift_1[k];
154 k = rice->k1;
155
156 rice->sum1 += outval - (rice->sum1 >> 4);
157 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
158 rice->k1--;
159 else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
160 rice->k1++;
161
162 unary = 1 + (outval >> k);
163 }
164
165 if (unary + 1LL + k > bits_left) {
166 if (pkt_size < INT_MAX/2) {
167 pkt_size *= 2;
168 av_packet_unref(avpkt);
169 goto pkt_alloc;
170 } else
171 return AVERROR(ENOMEM);
172 }
173 bits_left -= unary + 1 + k;
174 while (unary) {
175 if (unary > 31) {
176 put_bits(&pb, 31, 0x7FFFFFFF);
177 unary -= 31;
178 } else {
179 put_bits(&pb, unary, (1U << unary) - 1);
180 unary = 0;
181 }
182 }
183
184 put_bits(&pb, 1, 0);
185
186 if (k)
187 put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
188
189 if (cur_chan < avctx->ch_layout.nb_channels - 1)
190 cur_chan++;
191 else
192 cur_chan = 0;
193 }
194
195 flush_put_bits(&pb);
196 out_bytes = put_bytes_output(&pb);
197 put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
198 flush_put_bits(&pb);
199
200 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
static const char *const format[]
Definition af_aiir.c:445
const FFCodec ff_tta_encoder
Definition ttaenc.c:212
int32_t
Libavcodec external API header.
#define bits_left
Definition bitstream.h:116
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:63
double value
Definition eval.c:102
int ret
Definition ffplay.c:391
#define sample
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
@ AV_CODEC_ID_TTA
Definition codec_id.h:481
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t AVCRC
Definition crc.h:46
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:421
@ AV_CRC_32_IEEE_LE
Definition crc.h:53
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition samplefmt.h:57
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
static av_cold int tta_encode_init(AVCodecContext *avctx)
Definition ttaenc.c:38
static int32_t get_sample(const AVFrame *frame, int sample, enum AVSampleFormat format)
Definition ttaenc.c:74
static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition ttaenc.c:92
static av_cold int tta_encode_close(AVCodecContext *avctx)
Definition ttaenc.c:205
#define PRED(x, k)
#define av_cold
Definition attributes.h:117
Memory handling functions.
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1059
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1581
int sample_rate
samples per second
Definition avcodec.h:1044
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1072
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
TTAEncDSPContext dsp
Definition ttaenc.c:35
const AVCRC * crc_table
Definition ttaenc.c:32
TTAChannel * ch_ctx
Definition ttaenc.c:34
uint32_t k1
Definition ttadata.h:37
uint32_t sum0
Definition ttadata.h:37
uint32_t k0
Definition ttadata.h:37
uint32_t sum1
Definition ttadata.h:37
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
const uint32_t *const ff_tta_shift_16
Definition ttadata.c:38
const uint32_t ff_tta_shift_1[]
Definition ttadata.c:24
void ff_tta_rice_init(TTARice *c, uint32_t k0, uint32_t k1)
Definition ttadata.c:42
void ff_tta_filter_init(TTAFilter *c, int32_t shift)
Definition ttadata.c:50
const uint8_t ff_tta_filter_configs[]
Definition ttadata.c:40
av_cold void ff_ttaencdsp_init(TTAEncDSPContext *c)
Definition ttaencdsp.c:58
else temp
Definition vf_mcdeint.c:275
static double c[64]