summaryrefslogtreecommitdiff
path: root/src/demuxers/demux_mpgaudio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/demuxers/demux_mpgaudio.c')
-rw-r--r--src/demuxers/demux_mpgaudio.c491
1 files changed, 288 insertions, 203 deletions
diff --git a/src/demuxers/demux_mpgaudio.c b/src/demuxers/demux_mpgaudio.c
index 31b2d33ff..cf410eed8 100644
--- a/src/demuxers/demux_mpgaudio.c
+++ b/src/demuxers/demux_mpgaudio.c
@@ -15,15 +15,12 @@
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
- *
- * $Id: demux_mpgaudio.c,v 1.156 2007/03/29 16:32:12 dgp85 Exp $
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*
* demultiplexer for mpeg audio (i.e. mp3) streams
*
* mp3 file structure:
* [id3v2][Xing|Vbri] Frame1 Frame2 Frame3...FrameX [Lyrics][id3v2][id3v1]
- *
*/
#ifdef HAVE_CONFIG_H
@@ -36,15 +33,15 @@
#include <string.h>
#include <stdlib.h>
-#define LOG_MODULE "demux_mpeg_audio"
+#define LOG_MODULE "demux_mpgaudio"
#define LOG_VERBOSE
/*
#define LOG
*/
-#include "xine_internal.h"
-#include "xineutils.h"
-#include "demux.h"
-#include "compat.h"
+#include <xine/xine_internal.h>
+#include <xine/xineutils.h>
+#include <xine/demux.h>
+#include <xine/compat.h>
#include "bswap.h"
#include "group_audio.h"
#include "id3.h"
@@ -54,19 +51,20 @@
* the second mp3 frame is sent to the decoder
*/
#define NUM_PREVIEW_BUFFERS 2
+#define NUM_VALID_FRAMES 3
-#define WRAP_THRESHOLD 120000
#define FOURCC_TAG BE_FOURCC
#define RIFF_CHECK_BYTES 1024
#define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
#define AVI_TAG FOURCC_TAG('A', 'V', 'I', ' ')
#define CDXA_TAG FOURCC_TAG('C', 'D', 'X', 'A')
-#define MPEG_MARKER FOURCC_TAG( 0x00, 0x00, 0x01, 0xBA )
+#define MPEG_MARKER ME_FOURCC( 0x00, 0x00, 0x01, 0xBA )
/* Xing header stuff */
#define XING_TAG FOURCC_TAG('X', 'i', 'n', 'g')
+#define INFO_TAG FOURCC_TAG('I', 'n', 'f', 'o')
#define XING_FRAMES_FLAG 0x0001
#define XING_BYTES_FLAG 0x0002
#define XING_TOC_FLAG 0x0004
@@ -79,16 +77,16 @@
/* mp3 frame struct */
typedef struct {
/* header */
- double duration;
- uint32_t size; /* in bytes */
+ double duration; /* in milliseconds */
+ uint32_t size; /* in bytes; including padding */
uint32_t bitrate; /* in bit per second */
uint16_t freq; /* in Hz */
-
uint8_t layer;
-
uint8_t version_idx:2; /* 0: mpeg1, 1: mpeg2, 2: mpeg2.5 */
uint8_t lsf_bit:1;
uint8_t channel_mode:3;
+ uint8_t padding:3; /* in bytes */
+ uint8_t is_free_bitrate:1;
} mpg_audio_frame_t;
/* Xing Vbr Header struct */
@@ -127,7 +125,13 @@ typedef struct {
int br; /* bitrate in bits/second */
uint32_t blocksize;
+ /* current mp3 frame */
mpg_audio_frame_t cur_frame;
+
+ /* next mp3 frame, used when the frame size cannot be computed from the
+ * current frame header */
+ mpg_audio_frame_t next_frame;
+
double cur_time; /* in milliseconds */
off_t mpg_frame_start; /* offset */
@@ -137,7 +141,15 @@ typedef struct {
int check_vbr_header;
xing_header_t *xing_header;
vbri_header_t *vbri_header;
-
+
+ int found_next_frame:1;
+ int free_bitrate_count;
+ off_t free_bitrate_size; /* use this size if 3 free bitrate frames are encountered */
+ uint8_t next_header[4];
+ int mpg_version;
+ int mpg_layer;
+ int valid_frames;
+
} demux_mpgaudio_t ;
/* demuxer class struct */
@@ -205,17 +217,17 @@ static int parse_frame_header(mpg_audio_frame_t *const frame, const uint8_t *con
#endif
} frame_header;
- const uint32_t head = BE_32(buf);
+ const uint32_t head = _X_BE_32(buf);
const uint16_t frame_sync = head >> 21;
- lprintf("header: %08X\n", head);
if (frame_sync != 0x7ff) {
- lprintf("invalid frame sync\n");
+ lprintf("invalid frame sync %08X\n", head);
return 0;
}
+ lprintf("header: %08X\n", head);
frame_header.mpeg25_bit = (head >> 20) & 0x1;
- frame->lsf_bit = (head >> 19) & 0x1;
+ frame->lsf_bit = (head >> 19) & 0x1;
if (!frame_header.mpeg25_bit) {
if (frame->lsf_bit) {
lprintf("reserved mpeg25 lsf combination\n");
@@ -236,14 +248,14 @@ static int parse_frame_header(mpg_audio_frame_t *const frame, const uint8_t *con
}
frame_header.bitrate_idx = (head >> 12) & 0xf;
- if ((frame_header.bitrate_idx == 0) || (frame_header.bitrate_idx == 15)) {
- lprintf("invalid bitrate index\n");
+ if (frame_header.bitrate_idx == 15) {
+ lprintf("invalid bitrate index: %d\n", frame_header.bitrate_idx);
return 0;
}
frame_header.freq_idx = (head >> 10) & 0x3;
if (frame_header.freq_idx == 3) {
- lprintf("invalid frequence index\n");
+ lprintf("invalid frequence index: %d\n", frame_header.freq_idx);
return 0;
}
@@ -276,19 +288,27 @@ static int parse_frame_header(mpg_audio_frame_t *const frame, const uint8_t *con
const uint16_t samples = mp3_samples[frame->version_idx][frame->layer - 1];
frame->bitrate = mp3_bitrates[frame->version_idx][frame->layer - 1][frame_header.bitrate_idx] * 1000;
frame->freq = mp3_freqs[frame->version_idx][frame_header.freq_idx];
-
- frame->size = samples * (frame->bitrate / 8);
- frame->size /= frame->freq;
- /* Padding: only if padding_bit is set; 4 bytes for Layer 1 and 1 byte for others */
- frame->size += ( frame_header.padding_bit ? ( frame->layer == 1 ? 4 : 1 ) : 0 );
-
frame->duration = 1000.0f * (double)samples / (double)frame->freq;
+ frame->padding = ( frame_header.padding_bit ? ( frame->layer == 1 ? 4 : 1 ) : 0 );
+ frame->channel_mode = frame_header.channel_mode;
+
+ if (frame->bitrate > 0) {
+ frame->size = samples * (frame->bitrate / 8);
+ frame->size /= frame->freq;
+ /* Padding: only if padding_bit is set; 4 bytes for Layer 1 and 1 byte for others */
+ frame->size += frame->padding;
+ } else {
+ /* Free bitrate frame, the size of the frame cannot be computed from the header. */
+ frame->is_free_bitrate = 1;
+ frame->size = 0;
+ }
}
- lprintf("mpeg %d, layer %d\n", frame->version_idx + 1, frame->layer);
- lprintf("bitrate: %d bps, samplerate: %d Hz\n", frame->bitrate, frame->freq);
+ lprintf("mpeg %d, layer %d, channel_mode: %d\n", frame->version_idx + 1,
+ frame->layer, frame->channel_mode);
+ lprintf("bitrate: %d bps, output freq: %d Hz\n", frame->bitrate, frame->freq);
lprintf("length: %d bytes, %f ms\n", frame->size, frame->duration);
- lprintf("padding: %d bytes\n", ( frame_header.padding_bit ? ( frame->layer == 1 ? 4 : 1 ) : 0 ));
+ lprintf("padding: %d bytes\n", frame->padding);
return 1;
}
@@ -298,16 +318,8 @@ static int parse_frame_header(mpg_audio_frame_t *const frame, const uint8_t *con
*/
static xing_header_t* parse_xing_header(mpg_audio_frame_t *frame,
uint8_t *buf, int bufsize) {
-
-#ifdef LOG
- int i;
-#endif
uint8_t *ptr = buf;
- xing_header_t *xing;
-
- xing = xine_xmalloc (sizeof (xing_header_t));
- if (!xing)
- return NULL;
+ xing_header_t *xing = NULL;
/* offset of the Xing header */
if (frame->lsf_bit) {
@@ -322,52 +334,87 @@ static xing_header_t* parse_xing_header(mpg_audio_frame_t *frame,
ptr += (9 + 4);
}
- if (ptr >= (buf + bufsize - 4)) return 0;
+ if (ptr >= (buf + bufsize - 4)) goto exit_error;
lprintf("checking %08X\n", *ptr);
- if (BE_32(ptr) == XING_TAG) {
- lprintf("Xing header found\n");
+
+ if (_X_BE_32(ptr) == XING_TAG) {
+ int has_frames_flag = 0;
+ int has_bytes_flag = 0;
+
+ xing = xine_xmalloc (sizeof (xing_header_t));
+ if (!xing)
+ goto exit_error;
+
+ lprintf("found Xing header\n");
ptr += 4;
- if (ptr >= (buf + bufsize - 4)) return 0;
- xing->flags = BE_32(ptr); ptr += 4;
+ if (ptr >= (buf + bufsize - 4)) goto exit_error;
+ xing->flags = _X_BE_32(ptr); ptr += 4;
if (xing->flags & XING_FRAMES_FLAG) {
- if (ptr >= (buf + bufsize - 4)) return 0;
- xing->stream_frames = BE_32(ptr); ptr += 4;
+ if (ptr >= (buf + bufsize - 4)) goto exit_error;
+ xing->stream_frames = _X_BE_32(ptr); ptr += 4;
lprintf("stream frames: %d\n", xing->stream_frames);
+ has_frames_flag = 1;
}
if (xing->flags & XING_BYTES_FLAG) {
- if (ptr >= (buf + bufsize - 4)) return 0;
- xing->stream_size = BE_32(ptr); ptr += 4;
+ if (ptr >= (buf + bufsize - 4)) goto exit_error;
+ xing->stream_size = _X_BE_32(ptr); ptr += 4;
lprintf("stream size: %d\n", xing->stream_size);
+ has_bytes_flag = 1;
}
+
+ /* check if it's a useful Xing header */
+ if (!has_frames_flag || !has_bytes_flag) {
+ lprintf("Stupid Xing tag, cannot do anything with it !\n");
+ goto exit_error;
+ }
+
if (xing->flags & XING_TOC_FLAG) {
+ int i;
+
lprintf("toc found\n");
- if (ptr >= (buf + bufsize - XING_TOC_LENGTH)) return 0;
+ if (ptr >= (buf + bufsize - XING_TOC_LENGTH)) goto exit_error;
memcpy(xing->toc, ptr, XING_TOC_LENGTH);
#ifdef LOG
for (i = 0; i < XING_TOC_LENGTH; i++) {
- lprintf("%d ", xing->toc[i]);
+ printf("%d ", xing->toc[i]);
}
- lprintf("\n");
+ printf("\n");
#endif
+ /* check the table validity
+ * - MUST start with 0
+ * - values MUST increase
+ */
+ if (xing->toc[0] != 0) {
+ lprintf("invalid Xing toc\n");
+ goto exit_error;
+ }
+ for (i = 1; i < XING_TOC_LENGTH; i++) {
+ if (xing->toc[i] < xing->toc[i-1]) {
+ lprintf("invalid Xing toc\n");
+ goto exit_error;
+ }
+ }
ptr += XING_TOC_LENGTH;
}
xing->vbr_scale = -1;
if (xing->flags & XING_VBR_SCALE_FLAG) {
- if (ptr >= (buf + bufsize - 4)) return 0;
- xing->vbr_scale = BE_32(ptr);
+ if (ptr >= (buf + bufsize - 4)) goto exit_error;
+ xing->vbr_scale = _X_BE_32(ptr);
lprintf("vbr_scale: %d\n", xing->vbr_scale);
}
-
- return xing;
} else {
lprintf("Xing header not found\n");
+ }
+ return xing;
+
+exit_error:
+ lprintf("Xing header parse error\n");
free(xing);
return NULL;
}
-}
/*
* Parse a Vbri header
@@ -388,20 +435,20 @@ static vbri_header_t* parse_vbri_header(mpg_audio_frame_t *frame,
if ((ptr + 4) >= (buf + bufsize)) return 0;
lprintf("Checking %08X\n", *ptr);
- if (BE_32(ptr) == VBRI_TAG) {
- lprintf("Vbri header found\n");
+ if (_X_BE_32(ptr) == VBRI_TAG) {
+ lprintf("found Vbri header\n");
ptr += 4;
if ((ptr + 22) >= (buf + bufsize)) return 0;
- vbri->version = BE_16(ptr); ptr += 2;
- vbri->delai = BE_16(ptr); ptr += 2;
- vbri->quality = BE_16(ptr); ptr += 2;
- vbri->stream_size = BE_32(ptr); ptr += 4;
- vbri->stream_frames = BE_32(ptr); ptr += 4;
- vbri->toc_entries = BE_16(ptr); ptr += 2;
- vbri->toc_scale_factor = BE_16(ptr); ptr += 2;
- vbri->entry_size = BE_16(ptr); ptr += 2;
- vbri->entry_frames = BE_16(ptr); ptr += 2;
+ vbri->version = _X_BE_16(ptr); ptr += 2;
+ vbri->delai = _X_BE_16(ptr); ptr += 2;
+ vbri->quality = _X_BE_16(ptr); ptr += 2;
+ vbri->stream_size = _X_BE_32(ptr); ptr += 4;
+ vbri->stream_frames = _X_BE_32(ptr); ptr += 4;
+ vbri->toc_entries = _X_BE_16(ptr); ptr += 2;
+ vbri->toc_scale_factor = _X_BE_16(ptr); ptr += 2;
+ vbri->entry_size = _X_BE_16(ptr); ptr += 2;
+ vbri->entry_frames = _X_BE_16(ptr); ptr += 2;
lprintf("version: %d\n", vbri->version);
lprintf("delai: %d\n", vbri->delai);
lprintf("quality: %d\n", vbri->quality);
@@ -413,7 +460,7 @@ static vbri_header_t* parse_vbri_header(mpg_audio_frame_t *frame,
lprintf("entry_frames: %d\n", vbri->entry_frames);
if ((ptr + (vbri->toc_entries + 1) * vbri->entry_size) >= (buf + bufsize)) return 0;
- vbri->toc = xine_xmalloc (sizeof(int) * (vbri->toc_entries + 1));
+ vbri->toc = xine_xcalloc ((vbri->toc_entries + 1), sizeof(int));
if (!vbri->toc) {
free (vbri);
return NULL;
@@ -453,6 +500,7 @@ static vbri_header_t* parse_vbri_header(mpg_audio_frame_t *frame,
}
}
+
/*
* Parse a mp3 frame paylod
* return 1 on success, 0 on error
@@ -463,43 +511,97 @@ static int parse_frame_payload(demux_mpgaudio_t *this,
buf_element_t *buf;
off_t frame_pos, len;
uint64_t pts = 0;
+ int payload_size = 0;
frame_pos = this->input->get_current_pos(this->input) - 4;
- lprintf("frame_pos = %"PRId64"\n", frame_pos);
+ lprintf("frame_pos = %"PRId64", header: %08X\n", frame_pos, _X_BE_32(frame_header));
buf = this->audio_fifo->buffer_pool_alloc(this->audio_fifo);
if (this->cur_frame.size > buf->max_size) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
- "demux_mpgaudio: frame size is greater than fifo buffer size\n");
+ LOG_MODULE ": frame size is greater than fifo buffer size\n");
buf->free_buffer(buf);
return 0;
}
-
- /* the decoder needs the frame header */
- memcpy(buf->mem, frame_header, 4);
- len = this->input->read(this->input, buf->mem + 4, this->cur_frame.size - 4);
- if (len != (this->cur_frame.size - 4)) {
- buf->free_buffer(buf);
- return 0;
+ memcpy(buf->content, frame_header, 4);
+
+ /* compute the payload size */
+ if (this->cur_frame.size > 0) {
+ payload_size = this->cur_frame.size - 4;
+ this->free_bitrate_count = 0;
+ } else if (this->free_bitrate_count >= NUM_VALID_FRAMES) {
+ payload_size = this->free_bitrate_size + this->cur_frame.padding - 4;
+ this->cur_frame.size = payload_size + 4;
+ } else {
+ this->free_bitrate_count++;
+ payload_size = 0;
+ }
+
+ /* Read the payload data. */
+ if (payload_size > 0) {
+ off_t len;
+
+ /* If we know the payload size, it's easy */
+ this->found_next_frame = 0;
+ len = this->input->read(this->input, buf->content + 4, payload_size);
+ if (len != payload_size) {
+ buf->free_buffer(buf);
+ return 0;
+ }
+ } else {
+ /* Search for the beginning of the next frame and deduce the size of the
+ * current frame from the position of the next one. */
+ int payload_size = 0;
+ int max_size = buf->max_size - 4;
+
+ while (payload_size < max_size) {
+ len = this->input->read(this->input, &buf->content[4 + payload_size], 1);
+ if (len != 1) {
+ lprintf("EOF\n");
+ buf->free_buffer(buf);
+ return 0;
+ }
+ payload_size += len;
+
+ if (parse_frame_header(&this->next_frame, &buf->content[payload_size])) {
+ lprintf("found next frame header\n");
+
+ if (this->free_bitrate_size == 0) {
+ this->free_bitrate_size = payload_size - this->cur_frame.padding;
+ }
+
+ /* don't read the frame header twice */
+ this->found_next_frame = 1;
+ memcpy(&this->next_header[0], &buf->content[payload_size], 4);
+ payload_size -= 4;
+ break;
+ }
+ }
+ this->cur_frame.size = payload_size + 4;
+ this->cur_frame.bitrate = 8000 * this->cur_frame.size / this->cur_frame.duration;
+ lprintf("free bitrate: bitrate: %d, frame size: %d\n", this->br, this->cur_frame.size);
}
if (this->check_vbr_header) {
this->check_vbr_header = 0;
this->mpg_frame_start = frame_pos;
- this->xing_header = parse_xing_header(&this->cur_frame, buf->mem, this->cur_frame.size);
+ this->xing_header = parse_xing_header(&this->cur_frame, buf->content, this->cur_frame.size);
if (this->xing_header) {
buf->free_buffer(buf);
+ xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
+ LOG_MODULE ": found Xing header at offset %PRId64\n", frame_pos);
return 1;
}
- this->vbri_header = parse_vbri_header(&this->cur_frame, buf->mem, this->cur_frame.size);
+ this->vbri_header = parse_vbri_header(&this->cur_frame, buf->content, this->cur_frame.size);
if (this->vbri_header) {
buf->free_buffer(buf);
+ xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
+ LOG_MODULE ": found Vbri header at offset %PRId64\n", frame_pos);
return 1;
}
}
-
pts = (int64_t)(this->cur_time * 90.0f);
@@ -508,14 +610,13 @@ static int parse_frame_payload(demux_mpgaudio_t *this,
buf->extra_info->input_time = this->cur_time;
buf->pts = pts;
- buf->size = len + 4;
- buf->content = buf->mem;
+ buf->size = this->cur_frame.size;
buf->type = BUF_AUDIO_MPEG;
buf->decoder_info[0] = 1;
buf->decoder_flags = decoder_flags|BUF_FLAG_FRAME_END;
+ lprintf("send buffer: size=%d, pts=%"PRId64"\n", buf->size, pts);
this->audio_fifo->put(this->audio_fifo, buf);
- lprintf("send buffer: pts=%"PRId64"\n", pts);
this->cur_time += this->cur_frame.duration;
return 1;
}
@@ -579,40 +680,51 @@ static int read_frame_header(demux_mpgaudio_t *this, uint8_t *header_buf, int by
* Parse next mp3 frame
*/
static int demux_mpgaudio_next (demux_mpgaudio_t *this, int decoder_flags, int send_header) {
- uint8_t header_buf[4];
- int bytes = 4;
-
- for (;;) {
-
- if (read_frame_header(this, header_buf, bytes)) {
-
- if (parse_frame_header(&this->cur_frame, header_buf)) {
-
- /* send header buffer */
- if ( send_header ) {
- buf_element_t *buf;
-
- buf = this->audio_fifo->buffer_pool_alloc(this->audio_fifo);
-
- buf->type = BUF_AUDIO_MPEG;
- buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAME_END;
-
- buf->decoder_info[0] = 0;
- buf->decoder_info[1] = this->cur_frame.freq;
- buf->decoder_info[2] = 0; /* bits_per_sample */
-
- /* Only for channel_mode == 3 (mono) there is one channel, for any other case, there are 2 */
- buf->decoder_info[3] = ( this->cur_frame.channel_mode == 3 ) ? 1 : 2;
-
- buf->size = 0; /* No extra header data */
-
- this->audio_fifo->put(this->audio_fifo, buf);
- }
-
- return parse_frame_payload(this, header_buf, decoder_flags);
-
- } else if ( id3v2_istag(header_buf) ) {
- if (!id3v2_parse_tag(this->input, this->stream, header_buf)) {
+ uint8_t buffer[4];
+ uint8_t *header = buffer;
+
+ if (this->found_next_frame) {
+ lprintf("skip header reading\n");
+ header = this->next_header;
+ memcpy(&this->cur_frame, &this->next_frame, sizeof(mpg_audio_frame_t));
+ } else {
+ int bytes = 4;
+ int loose_sync = 0;
+
+ for (;;) {
+ if (!read_frame_header(this, header, bytes))
+ return 0;
+ if (parse_frame_header(&this->cur_frame, header)) {
+ lprintf("frame found\n");
+
+ /* additionnal checks */
+ if ((this->mpg_version == (this->cur_frame.version_idx + 1)) &&
+ (this->mpg_layer == this->cur_frame.layer)) {
+ this->valid_frames++;
+ break;
+ } else {
+ if (this->valid_frames >= NUM_VALID_FRAMES) {
+ lprintf("invalid frame. expected mpeg %d, layer %d\n", this->mpg_version, this->mpg_layer);
+ } else {
+ this->mpg_version = this->cur_frame.version_idx + 1;
+ this->mpg_layer = this->cur_frame.layer;
+ this->valid_frames = 0;
+ break;
+ }
+ }
+ }
+
+ if (!loose_sync) {
+ off_t frame_pos = this->input->get_current_pos(this->input) - 4;
+ loose_sync = 1;
+ xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
+ LOG_MODULE ": loose mp3 sync at offset %"PRId64"\n", frame_pos);
+ }
+ /* the stream is broken, don't keep info about previous frames */
+ this->free_bitrate_size = 0;
+
+ if ( id3v2_istag(header) ) {
+ if (!id3v2_parse_tag(this->input, this->stream, header)) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
LOG_MODULE ": ID3V2 tag parsing error\n");
bytes = 1; /* resync */
@@ -623,12 +735,31 @@ static int demux_mpgaudio_next (demux_mpgaudio_t *this, int decoder_flags, int s
/* skip */
bytes = 1;
}
-
- } else {
- lprintf("read error\n");
- return 0;
}
}
+
+ /* send header buffer */
+ if ( send_header ) {
+ buf_element_t *buf;
+
+ buf = this->audio_fifo->buffer_pool_alloc(this->audio_fifo);
+
+ buf->type = BUF_AUDIO_MPEG;
+ buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAME_END;
+
+ buf->decoder_info[0] = 0;
+ buf->decoder_info[1] = this->cur_frame.freq;
+ buf->decoder_info[2] = 0; /* bits_per_sample */
+
+ /* Only for channel_mode == 3 (mono) there is one channel, for any other case, there are 2 */
+ buf->decoder_info[3] = ( this->cur_frame.channel_mode == 3 ) ? 1 : 2;
+
+ buf->size = 0; /* No extra header data */
+
+ this->audio_fifo->put(this->audio_fifo, buf);
+ }
+
+ return parse_frame_payload(this, header, decoder_flags);
}
static int demux_mpgaudio_send_chunk (demux_plugin_t *this_gen) {
@@ -685,20 +816,16 @@ static int detect_mpgaudio_file(input_plugin_t *input) {
if (preview_len < 4)
return 0;
- lprintf("got preview %02x %02x %02x %02x\n",
- buf[0], buf[1], buf[2], buf[3]);
+ head = _X_ME_32(buf);
- head = BE_32(buf);
+ lprintf("got preview %08x\n", head);
- if ((head == ID3V22_TAG) ||
- (head == ID3V23_TAG) ||
- (head == ID3V24_TAG)) {
+ if (id3v2_istag(head)) {
/* check if a mp3 frame follows the tag
* id3v2 are not specific to mp3 files,
* flac files can contain id3v2 tags
*/
- uint8_t *ptr = &buf[6];
- uint32_t tag_size = id3v2_tagsize(ptr);
+ int tag_size = _X_BE_32_synchsafe(&buf[6]);
lprintf("try to skip id3v2 tag (%d bytes)\n", tag_size);
if ((10 + tag_size) >= preview_len) {
lprintf("cannot skip id3v2 tag\n");
@@ -755,11 +882,12 @@ static void demux_mpgaudio_send_headers (demux_plugin_t *this_gen) {
*/
this->check_vbr_header = 1;
for (i = 0; i < NUM_PREVIEW_BUFFERS; i++) {
+ lprintf("preview buffer number %d / %d\n", i + 1, NUM_PREVIEW_BUFFERS);
if (!demux_mpgaudio_next (this, BUF_FLAG_PREVIEW, i == 0)) {
break;
}
}
-
+
if (this->xing_header) {
xing_header_t *xing = this->xing_header;
@@ -770,7 +898,7 @@ static void demux_mpgaudio_send_headers (demux_plugin_t *this_gen) {
if (this->stream_length) {
this->br = ((uint64_t)xing->stream_size * 8 * 1000) / this->stream_length;
}
-
+
} else if (this->vbri_header) {
vbri_header_t *vbri = this->vbri_header;
@@ -812,8 +940,8 @@ static void demux_mpgaudio_send_headers (demux_plugin_t *this_gen) {
*/
{
char scratch_buf[256];
- char *mpeg_ver[3] = {"1", "2", "2.5"};
-
+ static const char mpeg_ver[3][4] = {"1", "2", "2.5"};
+
snprintf(scratch_buf, 256, "MPEG %s Layer %1d%s",
mpeg_ver[this->cur_frame.version_idx], this->cur_frame.layer,
(this->xing_header)? " VBR" : " CBR" );
@@ -926,7 +1054,7 @@ static int demux_mpgaudio_seek (demux_plugin_t *this_gen,
if (this->stream_length > 0) {
if (this->xing_header &&
- (this->xing_header->flags & (XING_TOC_FLAG | XING_BYTES_FLAG))) {
+ (this->xing_header->flags & XING_TOC_FLAG)) {
seek_pos += xing_get_seek_point(this->xing_header, start_time, this->stream_length);
lprintf("time seek: xing: time=%d, pos=%"PRId64"\n", start_time, seek_pos);
} else if (this->vbri_header) {
@@ -941,7 +1069,8 @@ static int demux_mpgaudio_seek (demux_plugin_t *this_gen,
/* assume seeking is always perfect... */
this->cur_time = start_time;
this->input->seek (this->input, seek_pos, SEEK_SET);
-
+ this->found_next_frame = 0;
+
if (playing) {
_x_demux_flush_engine(this->stream);
}
@@ -954,11 +1083,6 @@ static int demux_mpgaudio_seek (demux_plugin_t *this_gen,
return this->status;
}
-static void demux_mpgaudio_dispose (demux_plugin_t *this) {
-
- free (this);
-}
-
static int demux_mpgaudio_get_stream_length (demux_plugin_t *this_gen) {
demux_mpgaudio_t *this = (demux_mpgaudio_t *) this_gen;
@@ -992,18 +1116,7 @@ static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *str
}
break;
- case METHOD_BY_EXTENSION: {
- const char *const mrl = input->get_mrl(input);
- const char *const extensions = class_gen->get_extensions (class_gen);
-
- lprintf ("stage by extension %s\n", mrl);
-
- if (!_x_demux_check_extension (mrl, extensions))
- return NULL;
-
- }
- break;
-
+ case METHOD_BY_MRL:
case METHOD_EXPLICIT:
break;
@@ -1016,7 +1129,7 @@ static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *str
this->demux_plugin.send_headers = demux_mpgaudio_send_headers;
this->demux_plugin.send_chunk = demux_mpgaudio_send_chunk;
this->demux_plugin.seek = demux_mpgaudio_seek;
- this->demux_plugin.dispose = demux_mpgaudio_dispose;
+ this->demux_plugin.dispose = default_demux_plugin_dispose;
this->demux_plugin.get_status = demux_mpgaudio_get_status;
this->demux_plugin.get_stream_length = demux_mpgaudio_get_stream_length;
this->demux_plugin.get_capabilities = demux_mpgaudio_get_capabilities;
@@ -1034,49 +1147,6 @@ static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *str
/*
* demux mpegaudio class
*/
-
-static const char *get_description (demux_class_t *this_gen) {
- return "MPEG audio demux plugin";
-}
-
-static const char *get_identifier (demux_class_t *this_gen) {
- return "MPEGAUDIO";
-}
-
-static const char *get_extensions (demux_class_t *this_gen) {
- demux_mpgaudio_class_t *this = (demux_mpgaudio_class_t *) this_gen;
-
- if( _x_decoder_available(this->xine, BUF_AUDIO_MPEG) )
- return "mp3 mp2 mpa mpega";
- else
- return "";
-}
-
-static const char *get_mimetypes (demux_class_t *this_gen) {
- demux_mpgaudio_class_t *this = (demux_mpgaudio_class_t *) this_gen;
-
- if( _x_decoder_available(this->xine, BUF_AUDIO_MPEG) )
- return "audio/mpeg2: mp2: MPEG audio;"
- "audio/x-mpeg2: mp2: MPEG audio;"
- "audio/mpeg3: mp3: MPEG audio;"
- "audio/x-mpeg3: mp3: MPEG audio;"
- "audio/mpeg: mpa,abs,mpega: MPEG audio;"
- "audio/x-mpeg: mpa,abs,mpega: MPEG audio;"
- "audio/x-mpegurl: mp3: MPEG audio;"
- "audio/mpegurl: mp3: MPEG audio;"
- "audio/mp3: mp3: MPEG audio;"
- "audio/x-mp3: mp3: MPEG audio;";
- else
- return "";
-}
-
-static void class_dispose (demux_class_t *this_gen) {
-
- demux_mpgaudio_class_t *this = (demux_mpgaudio_class_t *) this_gen;
-
- free (this);
-}
-
void *demux_mpgaudio_init_class (xine_t *xine, void *data) {
demux_mpgaudio_class_t *this;
@@ -1085,11 +1155,26 @@ void *demux_mpgaudio_init_class (xine_t *xine, void *data) {
this->xine = xine;
this->demux_class.open_plugin = open_plugin;
- this->demux_class.get_description = get_description;
- this->demux_class.get_identifier = get_identifier;
- this->demux_class.get_mimetypes = get_mimetypes;
- this->demux_class.get_extensions = get_extensions;
- this->demux_class.dispose = class_dispose;
+ this->demux_class.description = N_("MPEG audio demux plugin");
+ this->demux_class.identifier = "MPEGAUDIO";
+ if( _x_decoder_available(this->xine, BUF_AUDIO_MPEG) ) {
+ this->demux_class.mimetypes =
+ "audio/mpeg2: mp2: MPEG audio;"
+ "audio/x-mpeg2: mp2: MPEG audio;"
+ "audio/mpeg3: mp3: MPEG audio;"
+ "audio/x-mpeg3: mp3: MPEG audio;"
+ "audio/mpeg: mpa,abs,mpega: MPEG audio;"
+ "audio/x-mpeg: mpa,abs,mpega: MPEG audio;"
+ "audio/x-mpegurl: mp3: MPEG audio;"
+ "audio/mpegurl: mp3: MPEG audio;"
+ "audio/mp3: mp3: MPEG audio;"
+ "audio/x-mp3: mp3: MPEG audio;";
+ this->demux_class.extensions = "mp3 mp2 mpa mpega";
+ } else {
+ this->demux_class.mimetypes = NULL;
+ this->demux_class.extensions = NULL;
+ }
+ this->demux_class.dispose = default_demux_class_dispose;
return this;
}