summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThibaut Mattern <thibaut.mattern@gmail.com>2008-01-19 13:52:43 +0100
committerThibaut Mattern <thibaut.mattern@gmail.com>2008-01-19 13:52:43 +0100
commitce16be132cec33879e3f1521997aa30993d7fd93 (patch)
tree9ba366268732893df47c64a6b45e5d1536811b9d
parent9c59211e1b6f6c41c5915a49e42737408ba7a680 (diff)
downloadxine-lib-ce16be132cec33879e3f1521997aa30993d7fd93.tar.gz
xine-lib-ce16be132cec33879e3f1521997aa30993d7fd93.tar.bz2
Initial support of free bitrate streams.
-rw-r--r--src/demuxers/demux_mpgaudio.c284
1 files changed, 203 insertions, 81 deletions
diff --git a/src/demuxers/demux_mpgaudio.c b/src/demuxers/demux_mpgaudio.c
index 8e716f095..027c46c45 100644
--- a/src/demuxers/demux_mpgaudio.c
+++ b/src/demuxers/demux_mpgaudio.c
@@ -35,9 +35,9 @@
#define LOG_MODULE "demux_mpeg_audio"
#define LOG_VERBOSE
-/*
+
#define LOG
-*/
+
#include "xine_internal.h"
#include "xineutils.h"
#include "demux.h"
@@ -52,7 +52,6 @@
*/
#define NUM_PREVIEW_BUFFERS 2
-#define WRAP_THRESHOLD 120000
#define FOURCC_TAG BE_FOURCC
#define RIFF_CHECK_BYTES 1024
@@ -77,16 +76,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 */
@@ -125,7 +124,16 @@ 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;
+
+ /* reference mp3 frame, used for extra validation when sync is lost */
+ mpg_audio_frame_t ref_frame;
+
double cur_time; /* in milliseconds */
off_t mpg_frame_start; /* offset */
@@ -135,7 +143,12 @@ typedef struct {
int check_vbr_header;
xing_header_t *xing_header;
vbri_header_t *vbri_header;
-
+
+ int found_next_frame:1;
+ int has_ref_frame:1;
+ off_t free_bitrate_size;
+ uint8_t next_header[4];
+
} demux_mpgaudio_t ;
/* demuxer class struct */
@@ -206,14 +219,14 @@ static int parse_frame_header(mpg_audio_frame_t *const frame, const uint8_t *con
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");
@@ -234,14 +247,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;
}
@@ -274,19 +287,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;
}
@@ -478,6 +499,25 @@ static vbri_header_t* parse_vbri_header(mpg_audio_frame_t *frame,
}
}
+static int check_frame_validity(demux_mpgaudio_t *const this, mpg_audio_frame_t *const frame) {
+ int result = 0;
+
+ if (this->ref_frame.is_free_bitrate) {
+ if ((frame->bitrate == 0) &&
+ (this->ref_frame.freq == frame->freq) &&
+ (this->ref_frame.version_idx == frame->version_idx) &&
+ (this->ref_frame.channel_mode == frame->channel_mode)) {
+ result = 1;
+ }
+ } else {
+ if ((this->ref_frame.version_idx == frame->version_idx) &&
+ (this->ref_frame.channel_mode == frame->channel_mode)) {
+ result = 1;
+ }
+ }
+ return result;
+}
+
/*
* Parse a mp3 frame paylod
* return 1 on success, 0 on error
@@ -488,9 +528,10 @@ 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);
@@ -500,31 +541,93 @@ static int parse_frame_payload(demux_mpgaudio_t *this,
buf->free_buffer(buf);
return 0;
}
-
+
/* the decoder needs the frame header */
- memcpy(buf->mem, frame_header, 4);
+ if (!this->found_next_frame) {
+ memcpy(buf->content, 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;
+ /* compute the payload size */
+ if (this->cur_frame.size > 0) {
+ payload_size = this->cur_frame.size - 4;
+ } else if (this->free_bitrate_size > 0) {
+// payload_size = this->free_bitrate_size + this->cur_frame.padding - 4;
+// this->cur_frame.size = payload_size + 4;
+ payload_size = 0;
+ } else {
+ 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;
+ int header_size = 4 * !this->found_next_frame;
+
+ while (payload_size < max_size) {
+ len = this->input->read(this->input,
+ &buf->content[header_size + payload_size], 1);
+ if (len != 1) {
+ lprintf("EOF\n");
+ buf->free_buffer(buf);
+ return 0;
+ }
+ payload_size += len;
+
+ if ((header_size + payload_size) >= 4) {
+ if (parse_frame_header(&this->next_frame,
+ &buf->content[header_size + payload_size - 4])) {
+ if (check_frame_validity(this, &this->next_frame)) {
+ 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[header_size + payload_size], 4);
+ payload_size -= 4;
+ break;
+ } else {
+ lprintf("invalid frame\n");
+ }
+ }
+ }
+ }
+ this->cur_frame.size = header_size + payload_size + 4;
+ if (this->br == 0) {
+ this->br = 8000 * this->cur_frame.size / this->cur_frame.duration;
+ }
+ this->cur_frame.bitrate = this->br;
+ 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);
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);
return 1;
}
}
-
pts = (int64_t)(this->cur_time * 90.0f);
@@ -533,14 +636,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;
}
@@ -604,56 +706,74 @@ 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)) {
- xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
- LOG_MODULE ": ID3V2 tag parsing error\n");
- bytes = 1; /* resync */
+ 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;
+
+ for (;;) {
+ if (!read_frame_header(this, header, bytes))
+ return 0;
+ if (parse_frame_header(&this->cur_frame, header)) {
+ if (!this->has_ref_frame) {
+ this->has_ref_frame = 1;
+ memcpy(&this->ref_frame, &this->cur_frame, sizeof(mpg_audio_frame_t));
+ lprintf("ref frame saved\n");
} else {
- bytes = 4;
+ if (!check_frame_validity(this, &this->cur_frame)) {
+ lprintf("invalid frame\n");
+ bytes = 1; /* resync */
+ continue;
+ }
}
+ lprintf("frame found\n");
+ break;
} else {
- /* skip */
- bytes = 1;
+ lprintf("loose sync\n");
+
+ 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 */
+ } else {
+ bytes = 4;
+ }
+ } else {
+ /* 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) {
@@ -779,6 +899,7 @@ 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;
}
@@ -965,7 +1086,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);
}