2007-02-06 21:19:04 +01:00
|
|
|
/*
|
|
|
|
* WMA compatible encoder
|
|
|
|
* Copyright (c) 2007 Michael Niedermayer
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
#include "libavutil/attributes.h"
|
2014-09-02 21:28:50 +02:00
|
|
|
|
2007-02-06 21:19:04 +01:00
|
|
|
#include "avcodec.h"
|
2012-02-20 18:29:14 +01:00
|
|
|
#include "internal.h"
|
2007-02-06 21:19:04 +01:00
|
|
|
#include "wma.h"
|
2012-03-05 07:27:42 +01:00
|
|
|
#include "libavutil/avassert.h"
|
2007-02-06 21:19:04 +01:00
|
|
|
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
static av_cold int encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
2007-02-27 10:54:48 +01:00
|
|
|
WMACodecContext *s = avctx->priv_data;
|
2012-10-23 22:30:59 +02:00
|
|
|
int i, flags1, flags2, block_align;
|
2007-02-06 21:19:04 +01:00
|
|
|
uint8_t *extradata;
|
|
|
|
|
2007-02-27 10:39:04 +01:00
|
|
|
s->avctx = avctx;
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
if (avctx->channels > MAX_CHANNELS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
2014-09-03 22:03:21 +02:00
|
|
|
"too many channels: got %i, need %i or fewer\n",
|
2011-03-21 10:52:36 +01:00
|
|
|
avctx->channels, MAX_CHANNELS);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-03-02 22:27:57 +01:00
|
|
|
if (avctx->sample_rate > 48000) {
|
2012-12-05 17:37:42 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz\n",
|
2012-03-02 22:27:57 +01:00
|
|
|
avctx->sample_rate);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
if (avctx->bit_rate < 24 * 1000) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"bitrate too low: got %i, need 24000 or higher\n",
|
2011-03-21 10:52:36 +01:00
|
|
|
avctx->bit_rate);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2007-03-08 11:25:06 +01:00
|
|
|
|
2007-02-06 21:19:04 +01:00
|
|
|
/* extract flag infos */
|
|
|
|
flags1 = 0;
|
|
|
|
flags2 = 1;
|
2012-08-05 11:11:04 +02:00
|
|
|
if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
|
2014-09-02 21:28:50 +02:00
|
|
|
extradata = av_malloc(4);
|
|
|
|
avctx->extradata_size = 4;
|
2007-06-02 03:41:07 +02:00
|
|
|
AV_WL16(extradata, flags1);
|
2014-09-02 21:28:50 +02:00
|
|
|
AV_WL16(extradata + 2, flags2);
|
2012-08-05 11:11:04 +02:00
|
|
|
} else if (avctx->codec->id == AV_CODEC_ID_WMAV2) {
|
2014-09-02 21:28:50 +02:00
|
|
|
extradata = av_mallocz(10);
|
|
|
|
avctx->extradata_size = 10;
|
2007-06-02 03:41:07 +02:00
|
|
|
AV_WL32(extradata, flags1);
|
2014-09-02 21:28:50 +02:00
|
|
|
AV_WL16(extradata + 4, flags2);
|
|
|
|
} else {
|
2012-07-01 18:02:17 +02:00
|
|
|
av_assert0(0);
|
2014-09-02 21:28:50 +02:00
|
|
|
}
|
|
|
|
avctx->extradata = extradata;
|
|
|
|
s->use_exp_vlc = flags2 & 0x0001;
|
|
|
|
s->use_bit_reservoir = flags2 & 0x0002;
|
2007-02-06 21:19:04 +01:00
|
|
|
s->use_variable_block_len = flags2 & 0x0004;
|
2012-03-02 23:11:25 +01:00
|
|
|
if (avctx->channels == 2)
|
|
|
|
s->ms_stereo = 1;
|
2007-02-06 21:19:04 +01:00
|
|
|
|
|
|
|
ff_wma_init(avctx, flags2);
|
|
|
|
|
|
|
|
/* init MDCT */
|
2014-09-02 21:28:50 +02:00
|
|
|
for (i = 0; i < s->nb_block_sizes; i++)
|
2009-05-16 16:17:08 +02:00
|
|
|
ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
block_align = avctx->bit_rate * (int64_t) s->frame_len /
|
2012-03-02 22:10:00 +01:00
|
|
|
(avctx->sample_rate * 8);
|
2012-10-23 22:30:59 +02:00
|
|
|
block_align = FFMIN(block_align, MAX_CODED_SUPERFRAME_SIZE);
|
|
|
|
avctx->block_align = block_align;
|
2012-11-02 14:40:04 +01:00
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
avctx->frame_size =
|
|
|
|
avctx->delay = s->frame_len;
|
2012-02-20 18:29:14 +01:00
|
|
|
|
2007-02-06 21:19:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
static void apply_window_and_mdct(AVCodecContext *avctx, const AVFrame *frame)
|
2012-08-25 19:44:30 +02:00
|
|
|
{
|
2007-02-27 10:54:48 +01:00
|
|
|
WMACodecContext *s = avctx->priv_data;
|
2014-09-02 21:28:50 +02:00
|
|
|
float **audio = (float **) frame->extended_data;
|
2012-08-25 19:44:30 +02:00
|
|
|
int len = frame->nb_samples;
|
2014-09-02 21:28:50 +02:00
|
|
|
int window_index = s->frame_len_bits - s->block_len_bits;
|
|
|
|
FFTContext *mdct = &s->mdct_ctx[window_index];
|
2012-08-25 19:44:30 +02:00
|
|
|
int ch;
|
2014-09-02 21:28:50 +02:00
|
|
|
const float *win = s->windows[window_index];
|
|
|
|
int window_len = 1 << s->block_len_bits;
|
|
|
|
float n = 2.0 * 32768.0 / window_len;
|
2012-08-25 19:44:30 +02:00
|
|
|
|
|
|
|
for (ch = 0; ch < avctx->channels; ch++) {
|
|
|
|
memcpy(s->output, s->frame_out[ch], window_len * sizeof(*s->output));
|
2012-09-23 00:13:57 +02:00
|
|
|
s->fdsp.vector_fmul_scalar(s->frame_out[ch], audio[ch], n, len);
|
2014-09-02 21:28:50 +02:00
|
|
|
s->fdsp.vector_fmul_reverse(&s->output[window_len], s->frame_out[ch],
|
|
|
|
win, len);
|
2012-08-25 19:44:30 +02:00
|
|
|
s->fdsp.vector_fmul(s->frame_out[ch], s->frame_out[ch], win, len);
|
|
|
|
mdct->mdct_calc(mdct, s->coefs[ch], s->output);
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
// FIXME use for decoding too
|
|
|
|
static void init_exp(WMACodecContext *s, int ch, const int *exp_param)
|
|
|
|
{
|
2007-02-06 21:19:04 +01:00
|
|
|
int n;
|
|
|
|
const uint16_t *ptr;
|
|
|
|
float v, *q, max_scale, *q_end;
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
|
|
|
|
q = s->exponents[ch];
|
|
|
|
q_end = q + s->block_len;
|
2007-02-06 21:19:04 +01:00
|
|
|
max_scale = 0;
|
|
|
|
while (q < q_end) {
|
|
|
|
/* XXX: use a table */
|
2014-09-02 21:28:50 +02:00
|
|
|
v = pow(10, *exp_param++ *(1.0 / 16.0));
|
|
|
|
max_scale = FFMAX(max_scale, v);
|
|
|
|
n = *ptr++;
|
2007-02-06 21:19:04 +01:00
|
|
|
do {
|
|
|
|
*q++ = v;
|
|
|
|
} while (--n);
|
|
|
|
}
|
|
|
|
s->max_exponent[ch] = max_scale;
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
|
|
|
|
{
|
2007-02-06 21:19:04 +01:00
|
|
|
int last_exp;
|
|
|
|
const uint16_t *ptr;
|
|
|
|
float *q, *q_end;
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
|
|
|
|
q = s->exponents[ch];
|
2007-02-06 21:19:04 +01:00
|
|
|
q_end = q + s->block_len;
|
|
|
|
if (s->version == 1) {
|
2014-09-02 21:28:50 +02:00
|
|
|
last_exp = *exp_param++;
|
2014-09-03 22:03:21 +02:00
|
|
|
av_assert0(last_exp - 10 >= 0 && last_exp - 10 < 32);
|
2007-02-06 21:19:04 +01:00
|
|
|
put_bits(&s->pb, 5, last_exp - 10);
|
2014-09-02 21:28:50 +02:00
|
|
|
q += *ptr++;
|
|
|
|
} else
|
2007-02-06 21:19:04 +01:00
|
|
|
last_exp = 36;
|
|
|
|
while (q < q_end) {
|
2014-09-02 21:28:50 +02:00
|
|
|
int exp = *exp_param++;
|
2007-02-06 21:19:04 +01:00
|
|
|
int code = exp - last_exp + 60;
|
2012-07-01 18:02:17 +02:00
|
|
|
av_assert1(code >= 0 && code < 120);
|
2014-09-02 21:28:50 +02:00
|
|
|
put_bits(&s->pb, ff_aac_scalefactor_bits[code],
|
|
|
|
ff_aac_scalefactor_code[code]);
|
2007-02-06 21:19:04 +01:00
|
|
|
/* XXX: use a table */
|
2014-09-02 21:28:50 +02:00
|
|
|
q += *ptr++;
|
|
|
|
last_exp = exp;
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
|
|
|
|
int total_gain)
|
|
|
|
{
|
2007-02-06 21:19:04 +01:00
|
|
|
int v, bsize, ch, coef_nb_bits, parse_exponents;
|
|
|
|
float mdct_norm;
|
|
|
|
int nb_coefs[MAX_CHANNELS];
|
2014-09-02 21:28:50 +02:00
|
|
|
static const int fixed_exp[25] = {
|
|
|
|
20, 20, 20, 20, 20,
|
|
|
|
20, 20, 20, 20, 20,
|
|
|
|
20, 20, 20, 20, 20,
|
|
|
|
20, 20, 20, 20, 20,
|
|
|
|
20, 20, 20, 20, 20
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME remove duplication relative to decoder
|
2007-02-06 21:19:04 +01:00
|
|
|
if (s->use_variable_block_len) {
|
2014-09-03 22:03:21 +02:00
|
|
|
av_assert0(0); // FIXME not implemented
|
2014-09-02 21:28:50 +02:00
|
|
|
} else {
|
2007-02-06 21:19:04 +01:00
|
|
|
/* fixed block len */
|
|
|
|
s->next_block_len_bits = s->frame_len_bits;
|
|
|
|
s->prev_block_len_bits = s->frame_len_bits;
|
2014-09-02 21:28:50 +02:00
|
|
|
s->block_len_bits = s->frame_len_bits;
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s->block_len = 1 << s->block_len_bits;
|
2013-08-02 03:45:02 +02:00
|
|
|
// av_assert0((s->block_pos + s->block_len) <= s->frame_len);
|
2007-02-06 21:19:04 +01:00
|
|
|
bsize = s->frame_len_bits - s->block_len_bits;
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
// FIXME factor
|
2007-02-06 21:19:04 +01:00
|
|
|
v = s->coefs_end[bsize] - s->coefs_start;
|
2012-10-23 22:30:59 +02:00
|
|
|
for (ch = 0; ch < s->avctx->channels; ch++)
|
2007-02-06 21:19:04 +01:00
|
|
|
nb_coefs[ch] = v;
|
|
|
|
{
|
|
|
|
int n4 = s->block_len / 2;
|
2014-09-02 21:28:50 +02:00
|
|
|
mdct_norm = 1.0 / (float) n4;
|
|
|
|
if (s->version == 1)
|
2007-02-06 21:19:04 +01:00
|
|
|
mdct_norm *= sqrt(n4);
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
if (s->avctx->channels == 2)
|
2012-03-02 23:11:25 +01:00
|
|
|
put_bits(&s->pb, 1, !!s->ms_stereo);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2012-10-23 22:30:59 +02:00
|
|
|
for (ch = 0; ch < s->avctx->channels; ch++) {
|
2014-09-02 21:28:50 +02:00
|
|
|
// FIXME only set channel_coded when needed, instead of always
|
|
|
|
s->channel_coded[ch] = 1;
|
|
|
|
if (s->channel_coded[ch])
|
2007-02-06 21:19:04 +01:00
|
|
|
init_exp(s, ch, fixed_exp);
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:30:59 +02:00
|
|
|
for (ch = 0; ch < s->avctx->channels; ch++) {
|
2007-02-06 21:19:04 +01:00
|
|
|
if (s->channel_coded[ch]) {
|
2009-06-20 11:05:28 +02:00
|
|
|
WMACoef *coefs1;
|
2007-02-06 21:19:04 +01:00
|
|
|
float *coefs, *exponents, mult;
|
|
|
|
int i, n;
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
coefs1 = s->coefs1[ch];
|
2007-02-06 21:19:04 +01:00
|
|
|
exponents = s->exponents[ch];
|
2014-09-02 21:28:50 +02:00
|
|
|
mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
|
|
|
|
mult *= mdct_norm;
|
|
|
|
coefs = src_coefs[ch];
|
2007-02-06 21:19:04 +01:00
|
|
|
if (s->use_noise_coding && 0) {
|
2014-09-03 22:03:21 +02:00
|
|
|
av_assert0(0); // FIXME not implemented
|
2007-02-06 21:19:04 +01:00
|
|
|
} else {
|
|
|
|
coefs += s->coefs_start;
|
2014-09-02 21:28:50 +02:00
|
|
|
n = nb_coefs[ch];
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
double t = *coefs++ / (exponents[i] * mult);
|
|
|
|
if (t < -32768 || t > 32767)
|
2007-02-06 21:19:04 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
coefs1[i] = lrint(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v = 0;
|
2012-10-23 22:30:59 +02:00
|
|
|
for (ch = 0; ch < s->avctx->channels; ch++) {
|
2007-02-06 23:20:55 +01:00
|
|
|
int a = s->channel_coded[ch];
|
2007-02-06 21:19:04 +01:00
|
|
|
put_bits(&s->pb, 1, a);
|
|
|
|
v |= a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!v)
|
|
|
|
return 1;
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
for (v = total_gain - 1; v >= 127; v -= 127)
|
2007-02-06 21:19:04 +01:00
|
|
|
put_bits(&s->pb, 7, 127);
|
|
|
|
put_bits(&s->pb, 7, v);
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
coef_nb_bits = ff_wma_total_gain_to_bits(total_gain);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
|
|
|
if (s->use_noise_coding) {
|
2012-10-23 22:30:59 +02:00
|
|
|
for (ch = 0; ch < s->avctx->channels; ch++) {
|
2007-02-06 21:19:04 +01:00
|
|
|
if (s->channel_coded[ch]) {
|
|
|
|
int i, n;
|
|
|
|
n = s->exponent_high_sizes[bsize];
|
2014-09-02 21:28:50 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
put_bits(&s->pb, 1, s->high_band_coded[ch][i] = 0);
|
2007-02-06 21:19:04 +01:00
|
|
|
if (0)
|
|
|
|
nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_exponents = 1;
|
2014-09-02 21:28:50 +02:00
|
|
|
if (s->block_len_bits != s->frame_len_bits)
|
2007-02-06 21:19:04 +01:00
|
|
|
put_bits(&s->pb, 1, parse_exponents);
|
|
|
|
|
|
|
|
if (parse_exponents) {
|
2012-10-23 22:30:59 +02:00
|
|
|
for (ch = 0; ch < s->avctx->channels; ch++) {
|
2007-02-06 21:19:04 +01:00
|
|
|
if (s->channel_coded[ch]) {
|
|
|
|
if (s->use_exp_vlc) {
|
|
|
|
encode_exp_vlc(s, ch, fixed_exp);
|
|
|
|
} else {
|
2014-09-03 22:03:21 +02:00
|
|
|
av_assert0(0); // FIXME not implemented
|
2007-02-06 21:19:04 +01:00
|
|
|
// encode_exp_lsp(s, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-02 21:28:50 +02:00
|
|
|
} else
|
2014-09-03 22:03:21 +02:00
|
|
|
av_assert0(0); // FIXME not implemented
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2012-10-23 22:30:59 +02:00
|
|
|
for (ch = 0; ch < s->avctx->channels; ch++) {
|
2007-02-06 21:19:04 +01:00
|
|
|
if (s->channel_coded[ch]) {
|
|
|
|
int run, tindex;
|
2009-06-20 11:05:28 +02:00
|
|
|
WMACoef *ptr, *eptr;
|
2007-02-06 21:19:04 +01:00
|
|
|
tindex = (ch == 1 && s->ms_stereo);
|
2014-09-02 21:28:50 +02:00
|
|
|
ptr = &s->coefs1[ch][0];
|
|
|
|
eptr = ptr + nb_coefs[ch];
|
|
|
|
|
|
|
|
run = 0;
|
|
|
|
for (; ptr < eptr; ptr++) {
|
|
|
|
if (*ptr) {
|
|
|
|
int level = *ptr;
|
|
|
|
int abs_level = FFABS(level);
|
|
|
|
int code = 0;
|
|
|
|
if (abs_level <= s->coef_vlcs[tindex]->max_level)
|
|
|
|
if (run < s->coef_vlcs[tindex]->levels[abs_level - 1])
|
|
|
|
code = run + s->int_table[tindex][abs_level - 1];
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2012-07-01 18:02:17 +02:00
|
|
|
av_assert2(code < s->coef_vlcs[tindex]->n);
|
2014-09-02 21:28:50 +02:00
|
|
|
put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code],
|
|
|
|
s->coef_vlcs[tindex]->huffcodes[code]);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
if (code == 0) {
|
|
|
|
if (1 << coef_nb_bits <= abs_level)
|
2007-02-06 21:19:04 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
put_bits(&s->pb, coef_nb_bits, abs_level);
|
|
|
|
put_bits(&s->pb, s->frame_len_bits, run);
|
|
|
|
}
|
2014-09-02 21:28:50 +02:00
|
|
|
// FIXME the sign is flipped somewhere
|
|
|
|
put_bits(&s->pb, 1, level < 0);
|
|
|
|
run = 0;
|
|
|
|
} else
|
2007-02-06 21:19:04 +01:00
|
|
|
run++;
|
|
|
|
}
|
2014-09-02 21:28:50 +02:00
|
|
|
if (run)
|
|
|
|
put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1],
|
|
|
|
s->coef_vlcs[tindex]->huffcodes[1]);
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
2014-09-02 21:28:50 +02:00
|
|
|
if (s->version == 1 && s->avctx->channels >= 2)
|
2011-10-17 10:10:42 +02:00
|
|
|
avpriv_align_put_bits(&s->pb);
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
|
|
|
|
uint8_t *buf, int buf_size, int total_gain)
|
|
|
|
{
|
2007-02-06 21:19:04 +01:00
|
|
|
init_put_bits(&s->pb, buf, buf_size);
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
if (s->use_bit_reservoir)
|
2014-09-03 22:03:21 +02:00
|
|
|
av_assert0(0); // FIXME not implemented
|
2014-09-02 21:28:50 +02:00
|
|
|
else if (encode_block(s, src_coefs, total_gain) < 0)
|
|
|
|
return INT_MAX;
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2011-10-17 10:10:42 +02:00
|
|
|
avpriv_align_put_bits(&s->pb);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2012-10-23 22:30:59 +02:00
|
|
|
return put_bits_count(&s->pb) / 8 - s->avctx->block_align;
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
2012-02-20 18:29:14 +01:00
|
|
|
static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
|
|
|
|
const AVFrame *frame, int *got_packet_ptr)
|
|
|
|
{
|
2007-02-27 10:54:48 +01:00
|
|
|
WMACodecContext *s = avctx->priv_data;
|
2012-06-30 19:47:00 +02:00
|
|
|
int i, total_gain, ret, error;
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
s->block_len_bits = s->frame_len_bits; // required by non variable block len
|
|
|
|
s->block_len = 1 << s->block_len_bits;
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2012-08-25 19:44:30 +02:00
|
|
|
apply_window_and_mdct(avctx, frame);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
|
|
|
if (s->ms_stereo) {
|
|
|
|
float a, b;
|
|
|
|
int i;
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
for (i = 0; i < s->block_len; i++) {
|
|
|
|
a = s->coefs[0][i] * 0.5;
|
|
|
|
b = s->coefs[1][i] * 0.5;
|
2007-02-06 21:19:04 +01:00
|
|
|
s->coefs[0][i] = a + b;
|
|
|
|
s->coefs[1][i] = a - b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 20:11:00 +01:00
|
|
|
if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE)) < 0)
|
2012-02-20 18:29:14 +01:00
|
|
|
return ret;
|
2012-03-02 22:33:33 +01:00
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
total_gain = 128;
|
|
|
|
for (i = 64; i; i >>= 1) {
|
2012-06-30 19:47:00 +02:00
|
|
|
error = encode_frame(s, s->coefs, avpkt->data, avpkt->size,
|
2012-02-20 18:29:14 +01:00
|
|
|
total_gain - i);
|
2014-09-03 22:03:21 +02:00
|
|
|
if (error <= 0)
|
2014-09-02 21:28:50 +02:00
|
|
|
total_gain -= i;
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
2012-06-30 19:47:00 +02:00
|
|
|
while(total_gain <= 128 && error > 0)
|
|
|
|
error = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain++);
|
2013-05-13 21:38:20 +02:00
|
|
|
if (error > 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid input data or requested bitrate too low, cannot encode\n");
|
|
|
|
avpkt->size = 0;
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2012-03-05 07:28:54 +01:00
|
|
|
av_assert0((put_bits_count(&s->pb) & 7) == 0);
|
2012-11-02 14:40:04 +01:00
|
|
|
i= avctx->block_align - (put_bits_count(&s->pb)+7)/8;
|
2012-03-05 07:28:54 +01:00
|
|
|
av_assert0(i>=0);
|
2012-03-05 07:24:16 +01:00
|
|
|
while(i--)
|
2007-02-06 21:19:04 +01:00
|
|
|
put_bits(&s->pb, 8, 'N');
|
|
|
|
|
|
|
|
flush_put_bits(&s->pb);
|
2012-11-02 14:40:04 +01:00
|
|
|
av_assert0(put_bits_ptr(&s->pb) - s->pb.buf == avctx->block_align);
|
2012-02-20 18:29:14 +01:00
|
|
|
|
|
|
|
if (frame->pts != AV_NOPTS_VALUE)
|
|
|
|
avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
|
|
|
|
|
2014-09-02 21:28:50 +02:00
|
|
|
avpkt->size = avctx->block_align;
|
2012-02-20 18:29:14 +01:00
|
|
|
*got_packet_ptr = 1;
|
|
|
|
return 0;
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
2012-06-11 14:40:28 +02:00
|
|
|
#if CONFIG_WMAV1_ENCODER
|
2011-09-23 21:11:15 +02:00
|
|
|
AVCodec ff_wmav1_encoder = {
|
|
|
|
.name = "wmav1",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
|
2011-09-23 21:11:15 +02:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_WMAV1,
|
2011-09-23 21:11:15 +02:00
|
|
|
.priv_data_size = sizeof(WMACodecContext),
|
|
|
|
.init = encode_init,
|
2012-02-20 18:29:14 +01:00
|
|
|
.encode2 = encode_superframe,
|
2011-09-23 21:11:15 +02:00
|
|
|
.close = ff_wma_end,
|
2014-09-02 21:28:50 +02:00
|
|
|
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_NONE },
|
2007-02-06 21:19:04 +01:00
|
|
|
};
|
2012-06-11 14:40:28 +02:00
|
|
|
#endif
|
|
|
|
#if CONFIG_WMAV2_ENCODER
|
2011-09-23 21:11:15 +02:00
|
|
|
AVCodec ff_wmav2_encoder = {
|
|
|
|
.name = "wmav2",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
|
2011-09-23 21:11:15 +02:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_WMAV2,
|
2011-09-23 21:11:15 +02:00
|
|
|
.priv_data_size = sizeof(WMACodecContext),
|
|
|
|
.init = encode_init,
|
2012-02-20 18:29:14 +01:00
|
|
|
.encode2 = encode_superframe,
|
2011-09-23 21:11:15 +02:00
|
|
|
.close = ff_wma_end,
|
2014-09-02 21:28:50 +02:00
|
|
|
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_NONE },
|
2007-02-06 21:19:04 +01:00
|
|
|
};
|
2012-06-11 14:40:28 +02:00
|
|
|
#endif
|