summaryrefslogtreecommitdiff
path: root/src/audio_dec/xine_mad_decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_dec/xine_mad_decoder.c')
-rw-r--r--src/audio_dec/xine_mad_decoder.c44
1 files changed, 41 insertions, 3 deletions
diff --git a/src/audio_dec/xine_mad_decoder.c b/src/audio_dec/xine_mad_decoder.c
index de7590f4e..74c04924f 100644
--- a/src/audio_dec/xine_mad_decoder.c
+++ b/src/audio_dec/xine_mad_decoder.c
@@ -48,6 +48,20 @@
#define INPUT_BUF_SIZE 16384
+/* According to Rob Leslie (libmad author) :
+ * The absolute theoretical maximum frame size is 2881 bytes: MPEG 2.5 Layer II,
+ * 8000 Hz @ 160 kbps, with a padding slot. (Such a frame is unlikely, but it was
+ * a useful exercise to compute all possible frame sizes.) Add to this an 8 byte
+ * MAD_BUFFER_GUARD, and the minimum buffer size you should be streaming to
+ * libmad in the general case is 2889 bytes.
+
+ * Theoretical frame sizes for Layer III range from 24 to 1441 bytes, but there
+ * is a "soft" limit imposed by the standard of 960 bytes. Nonetheless MAD can
+ * decode frames of any size as long as they fit entirely in the buffer you pass,
+ * not including the MAD_BUFFER_GUARD bytes.
+ */
+#define MAD_MIN_SIZE 2889
+
typedef struct {
audio_decoder_class_t decoder_class;
} mad_class_t;
@@ -135,9 +149,10 @@ static int head_check(mad_decoder_t *this) {
static void mad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
mad_decoder_t *this = (mad_decoder_t *) this_gen;
+ int bytes_in_buffer_at_pts;
+
+ lprintf ("decode data, size: %d, decoder_flags: %d\n", buf->size, buf->decoder_flags);
- lprintf ("decode data, decoder_flags: %d\n", buf->decoder_flags);
-
if (buf->size>(INPUT_BUF_SIZE-this->bytes_in_buffer)) {
xprintf (this->xstream->xine, XINE_VERBOSITY_DEBUG,
"libmad: ALERT input buffer too small (%d bytes, %d avail)!\n",
@@ -156,6 +171,8 @@ static void mad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
this->preview_mode = 1;
}
+ bytes_in_buffer_at_pts = this->bytes_in_buffer;
+
xine_fast_memcpy (&this->buffer[this->bytes_in_buffer],
buf->content, buf->size);
this->bytes_in_buffer += buf->size;
@@ -167,6 +184,9 @@ static void mad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
mad_stream_buffer (&this->stream, this->buffer,
this->bytes_in_buffer);
+ if (this->bytes_in_buffer < MAD_MIN_SIZE)
+ return;
+
while (1) {
if (mad_frame_decode (&this->frame, &this->stream) != 0) {
@@ -187,6 +207,7 @@ static void mad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
return;
default:
+ lprintf ("error 0x%04X, mad_stream_buffer %d bytes\n", this->stream.error, this->bytes_in_buffer);
mad_stream_buffer (&this->stream, this->buffer,
this->bytes_in_buffer);
}
@@ -253,6 +274,8 @@ static void mad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
struct mad_pcm *pcm = &this->synth.pcm;
audio_buffer_t *audio_buffer;
uint16_t *output;
+ int bitrate;
+ int pts_offset;
audio_buffer = this->xstream->audio_out->get_buffer (this->xstream->audio_out);
output = audio_buffer->mem;
@@ -273,7 +296,22 @@ static void mad_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {
}
audio_buffer->num_frames = pcm->length;
- audio_buffer->vpts = buf->pts;
+
+ /* pts computing */
+ if (this->frame.header.bitrate > 0) {
+ bitrate = this->frame.header.bitrate;
+ } else {
+ bitrate = _x_stream_info_get(this->xstream, XINE_STREAM_INFO_AUDIO_BITRATE);
+ lprintf("offset %d bps\n", bitrate);
+ }
+ audio_buffer->vpts = buf->pts;
+ if (audio_buffer->vpts && (bitrate > 0)) {
+ pts_offset = (bytes_in_buffer_at_pts * 8 * 90) / (bitrate / 1000);
+ lprintf("pts: %"PRId64", offset: %d pts, %d bytes\n", buf->pts, pts_offset, bytes_in_buffer_at_pts);
+ if (audio_buffer->vpts < pts_offset)
+ pts_offset = audio_buffer->vpts;
+ audio_buffer->vpts -= pts_offset;
+ }
this->xstream->audio_out->put_buffer (this->xstream->audio_out, audio_buffer, this->xstream);