1 /*
2 * WMA compatible encoder
3 * Copyright (c) 2007 Michael Niedermayer
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
24
29
30
32 {
34 int i, flags1, flags2, block_align;
36 int ret;
37
39
42 "too many channels: got %i, need %i or fewer\n",
45 }
46
51 }
52
55 "bitrate too low: got %"PRId64", need 24000 or higher\n",
58 }
59
60 /* extract flag info */
61 flags1 = 0;
62 flags2 = 1;
65 if (!extradata)
72 if (!extradata)
77 } else {
79 }
86
88 return ret;
89
90 /* init MDCT */
93
99
100 return 0;
101 }
102
104 {
113 float n = 2.0 * 32768.0 / window_len;
114
125 }
126 }
127
128 return 0;
129 }
130
131 // FIXME use for decoding too
133 {
135 const uint16_t *ptr;
136 float v, *q, max_scale, *q_end;
137
141 max_scale = 0;
142 while (q < q_end) {
143 /* XXX: use a table */
144 v =
ff_exp10(*exp_param++ *(1.0 / 16.0));
145 max_scale =
FFMAX(max_scale, v);
146 n = *ptr++;
147 do {
148 *q++ = v;
149 } while (--n);
150 }
152 }
153
155 {
156 int last_exp;
157 const uint16_t *ptr;
158 float *q, *q_end;
159
164 last_exp = *exp_param++;
165 av_assert0(last_exp - 10 >= 0 && last_exp - 10 < 32);
167 q += *ptr++;
168 } else
169 last_exp = 36;
170 while (q < q_end) {
171 int exp = *exp_param++;
172 int code = exp - last_exp + 60;
176 /* XXX: use a table */
177 q += *ptr++;
179 }
180 }
181
183 int total_gain)
184 {
186 float mdct_norm;
189 20, 20, 20, 20, 20,
190 20, 20, 20, 20, 20,
191 20, 20, 20, 20, 20,
192 20, 20, 20, 20, 20,
193 20, 20, 20, 20, 20
194 };
195
196 // FIXME remove duplication relative to decoder
199 } else {
200 /* fixed block len */
204 }
205
207 // av_assert0((s->block_pos + s->block_len) <= s->frame_len);
209
210 // FIXME factor
213 nb_coefs[ch] = v;
214 {
216 mdct_norm = 1.0 / (float) n4;
218 mdct_norm *= sqrt(n4);
219 }
220
223
225 // FIXME only set channel_coded when needed, instead of always
229 }
230
234 float *coefs, *exponents,
mult;
236
240 mult *= mdct_norm;
241 coefs = src_coefs[
ch];
244 } else {
247 for (i = 0; i <
n; i++) {
248 double t = *coefs++ / (exponents[i] *
mult);
249 if (t < -32768 || t > 32767)
250 return -1;
251
252 coefs1[i] =
lrint(t);
253 }
254 }
255 }
256 }
257
258 v = 0;
263 }
264
265 if (!v)
266 return 1;
267
268 for (v = total_gain - 1; v >= 127; v -= 127)
271
273
279 for (i = 0; i <
n; i++) {
281 if (0)
283 }
284 }
285 }
286 }
287
288 parse_exponents = 1;
291
292 if (parse_exponents) {
297 } else {
299 // encode_exp_lsp(s, ch);
300 }
301 }
302 }
303 } else
305
312 eptr = ptr + nb_coefs[
ch];
313
314 run = 0;
315 for (; ptr < eptr; ptr++) {
316 if (*ptr) {
318 int abs_level =
FFABS(level);
319 int code = 0;
320 if (abs_level <= s->
coef_vlcs[tindex]->max_level)
321 if (run < s->
coef_vlcs[tindex]->levels[abs_level - 1])
322 code = run + s->
int_table[tindex][abs_level - 1];
323
327
328 if (code == 0) {
329 if (1 << coef_nb_bits <= abs_level)
330 return -1;
331
334 }
335 // FIXME the sign is flipped somewhere
337 run = 0;
338 } else
339 run++;
340 }
341 if (run)
344 }
347 }
348 return 0;
349 }
350
353 {
355
359 return INT_MAX;
360
362
364 }
365
368 {
370 int i, total_gain, ret,
error;
371
374
376
377 if (ret < 0)
378 return ret;
379
382 int i;
383
385 a = s->
coefs[0][i] * 0.5;
386 b = s->
coefs[1][i] * 0.5;
389 }
390 }
391
393 return ret;
394
395 total_gain = 128;
396 for (i = 64; i; i >>= 1) {
398 total_gain - i);
399 if (error <= 0)
400 total_gain -= i;
401 }
402
403 while(total_gain <= 128 && error > 0)
405 if (error > 0) {
406 av_log(avctx,
AV_LOG_ERROR,
"Invalid input data or requested bitrate too low, cannot encode\n");
409 }
413 while(i--)
415
418
421
423 *got_packet_ptr = 1;
424 return 0;
425 }
426
427 #if CONFIG_WMAV1_ENCODER
439 };
440 #endif
441 #if CONFIG_WMAV2_ENCODER
453 };
454 #endif
const struct AVCodec * codec
static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
int64_t bit_rate
the average bitrate
int next_block_len_bits
log2 of next block length
static float win(SuperEqualizerContext *s, float n, int N)
static av_cold int init(AVCodecContext *avctx)
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
int block_len
block length in samples
const uint8_t * huffbits
VLC bit size.
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats, and store the result in a vector of floats...
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE]
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Macro definitions for various function/variable attributes.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
float WMACoef
type for decoded coefficients, int16_t would be enough for wma 1/2
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
const uint8_t ff_aac_scalefactor_bits[121]
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
const uint32_t * huffcodes
VLC bit values.
static int encode_frame(WMACodecContext *s, float(*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain)
int nb_block_sizes
number of block sizes
int ff_wma_total_gain_to_bits(int total_gain)
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats...
uint16_t exponent_bands[BLOCK_NB_SIZES][25]
uint8_t channel_coded[MAX_CHANNELS]
true if channel is coded
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int initial_padding
Audio only.
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
FFTSample output[BLOCK_MAX_SIZE *2]
static int put_bits_count(PutBitContext *s)
int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE]
int ff_wma_end(AVCodecContext *avctx)
static int16_t mult(Float11 *f1, Float11 *f2)
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define MAX_CODED_SUPERFRAME_SIZE
av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int version
1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
int frame_len
frame length in samples
static void error(const char *err)
static void init_exp(WMACodecContext *s, int ch, const int *exp_param)
int frame_size
Number of samples per channel in an audio frame.
int frame_len_bits
frame_len = 1 << frame_len_bits
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
static int apply_window_and_mdct(AVCodecContext *avctx, const AVFrame *frame)
int use_exp_vlc
exponent coding: 0 = lsp, 1 = vlc + delta
main external API structure.
float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE *2]
int exponent_high_sizes[BLOCK_NB_SIZES]
static int fixed_exp(int x)
int use_noise_coding
true if perceptual noise is added
int use_variable_block_len
uint8_t ms_stereo
true if mid/side stereo mode
static av_cold int encode_init(AVCodecContext *avctx)
FFTContext mdct_ctx[BLOCK_NB_SIZES]
const uint32_t ff_aac_scalefactor_code[121]
float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE]
int prev_block_len_bits
log2 of prev block length
static int encode_block(WMACodecContext *s, float(*src_coefs)[BLOCK_MAX_SIZE], int total_gain)
int coefs_end[BLOCK_NB_SIZES]
max number of coded coefficients
internal math functions header
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
int channels
number of audio channels
static int parse_exponents(DBEContext *s, DBEChannel *c)
WMACoef coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE]
static const CoefVLCTable coef_vlcs[6]
static enum AVSampleFormat sample_fmts[]
float max_exponent[MAX_CHANNELS]
int coefs_start
first coded coef
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
int block_len_bits
log2 of current block length
uint8_t ** extended_data
pointers to the data planes/channels.
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE]
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
const CoefVLCTable * coef_vlcs[2]
#define AV_NOPTS_VALUE
Undefined timestamp value.
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_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FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
const float * windows[BLOCK_NB_SIZES]