* The simplest AC3 encoder
* Copyright (c) 2000 Fabrice Bellard.
*
- * This library is free software; you can redistribute it and/or
+ * 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 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
- * This library is distributed in the hope that it will be useful,
+ * 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 this library; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
unsigned int bsid;
unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */
unsigned int frame_size; /* current frame size in words */
+ unsigned int bits_written;
+ unsigned int samples_written;
int halfratecod;
unsigned int frmsizecod;
unsigned int fscod; /* frequency */
bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
csnroffst -= SNR_INC1;
if (csnroffst < 0) {
- av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n");
+ av_log(NULL, AV_LOG_ERROR, "Bit allocation failed, try increasing the bitrate, -ab 384 for example!\n");
return -1;
}
while ((csnroffst + SNR_INC1) <= 63 &&
for(j=0;j<v;j++) masktab[k++]=i;
l += v;
}
- bndtab[50] = 0;
+ bndtab[50] = l;
}
s->bit_rate = bitrate;
s->frmsizecod = i << 1;
s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
- /* for now we do not handle fractional sizes */
+ s->bits_written = 0;
+ s->samples_written = 0;
s->frame_size = s->frame_size_min;
/* bit allocation init */
v = 14 - log2_tab(input_samples, N);
if (v < 0)
v = 0;
- exp_samples[i][ch] = v - 8;
+ exp_samples[i][ch] = v - 9;
lshift_tab(input_samples, N, v);
/* do the MDCT */
}
}
+ /* adjust for fractional frame sizes */
+ while(s->bits_written >= s->bit_rate*1000 && s->samples_written >= s->sample_rate) {
+ s->bits_written -= s->bit_rate*1000;
+ s->samples_written -= s->sample_rate;
+ }
+ s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate*1000);
+ s->bits_written += s->frame_size * 16;
+ s->samples_written += AC3_FRAME_SIZE;
+
compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
/* everything is known... let's output the frame */
output_frame_header(s, frame);