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 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
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;
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;
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 value = 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->size = out_bytes + 4;
192 *got_packet_ptr = 1;
193 return 0;
194}
195
197{
198 TTAEncContext *s = avctx->priv_data;
199 av_freep(&s->ch_ctx);
200 return 0;
201}
202
static const char *const format[]
Definition af_aiir.c:444
const FFCodec ff_tta_encoder
Definition ttaenc.c:203
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
int32_t
Libavcodec external API header.
#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:608
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
double value
Definition eval.c:102
#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:475
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
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
@ 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
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:69
static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition ttaenc.c:87
static av_cold int tta_encode_close(AVCodecContext *avctx)
Definition ttaenc.c:196
#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 int put_bits_left(PutBitContext *s)
Definition put_bits.h:135
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:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
int sample_rate
samples per second
Definition avcodec.h:1040
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
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]