/*
* The simplest mpeg audio layer 2 encoder
- * Copyright (c) 2000, 2001 Gerard Lantau.
+ * Copyright (c) 2000, 2001 Fabrice Bellard.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library 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.
*
- * This program is distributed in the hope that it will be useful,
+ * This library 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 General Public License for more details.
+ * 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 General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
+/**
+ * @file mpegaudio.c
+ * The simplest mpeg audio layer 2 encoder.
+ */
+
#include "avcodec.h"
-#include <math.h>
+#include "bitstream.h"
#include "mpegaudio.h"
/* currently, cannot change these constants (need to modify
quantization stage) */
-#define FRAC_BITS 15
-#define WFRAC_BITS 14
-#define MUL(a,b) (((INT64)(a) * (INT64)(b)) >> FRAC_BITS)
+#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
#define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
#define SAMPLES_BUF_SIZE 4096
int bitrate_index; /* bit rate */
int freq_index;
int frame_size; /* frame size, in bits, without padding */
- INT64 nb_samples; /* total number of samples encoded */
+ int64_t nb_samples; /* total number of samples encoded */
/* padding computation */
int frame_frac, frame_frac_incr, do_padding;
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
#include "mpegaudiotab.h"
-int MPA_encode_init(AVCodecContext *avctx)
+static int MPA_encode_init(AVCodecContext *avctx)
{
MpegAudioContext *s = avctx->priv_data;
int freq = avctx->sample_rate;
s->freq = freq;
s->bit_rate = bitrate * 1000;
avctx->frame_size = MPA_FRAME_SIZE;
- avctx->key_frame = 1; /* always key frame */
/* encoding freq */
s->lsf = 0;
break;
}
}
- if (i == 3)
+ if (i == 3){
+ av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
return -1;
+ }
s->freq_index = i;
/* encoding bitrate & frequency */
if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)
break;
}
- if (i == 15)
+ if (i == 15){
+ av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
return -1;
+ }
s->bitrate_index = i;
/* compute total header size & pad bit */
s->alloc_table = alloc_tables[table];
#ifdef DEBUG
- printf("%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
+ av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
bitrate, freq, s->frame_size, table, s->frame_frac_incr);
#endif
total_quant_bits[i] = 12 * v;
}
+ avctx->coded_frame= avcodec_alloc_frame();
+ avctx->coded_frame->key_frame= 1;
+
return 0;
}
sf[1] = sf[2] = sf[0];
break;
default:
- abort();
+ assert(0); //cant happen
+ code = 0; /* kill warning */
}
#if 0
flush_put_bits(p);
}
-int MPA_encode_frame(AVCodecContext *avctx,
- unsigned char *frame, int buf_size, void *data)
+static int MPA_encode_frame(AVCodecContext *avctx,
+ unsigned char *frame, int buf_size, void *data)
{
MpegAudioContext *s = avctx->priv_data;
short *samples = data;
}
compute_bit_allocation(s, smr, bit_alloc, &padding);
- init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE, NULL, NULL);
+ init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
encode_frame(s, bit_alloc, padding);
s->nb_samples += MPA_FRAME_SIZE;
- return s->pb.buf_ptr - s->pb.buf;
+ return pbBufPtr(&s->pb) - s->pb.buf;
}
+static int MPA_encode_close(AVCodecContext *avctx)
+{
+ av_freep(&avctx->coded_frame);
+ return 0;
+}
+#ifdef CONFIG_MP2_ENCODER
AVCodec mp2_encoder = {
"mp2",
CODEC_TYPE_AUDIO,
sizeof(MpegAudioContext),
MPA_encode_init,
MPA_encode_frame,
+ MPA_encode_close,
NULL,
};
+#endif // CONFIG_MP2_ENCODER
+
+#undef FIX