diff options
author | Diego 'Flameeyes' Pettenò <flameeyes@gmail.com> | 2008-07-03 16:56:34 +0200 |
---|---|---|
committer | Diego 'Flameeyes' Pettenò <flameeyes@gmail.com> | 2008-07-03 16:56:34 +0200 |
commit | f44b4db93f7ab7a1cad98271f84f1c1717405a65 (patch) | |
tree | 2ef6a0b6952f082169f5d8cc069ee77a6ce4f063 | |
parent | 2b0e330c2642081066316ca5ec9965912cd248de (diff) | |
download | xine-lib-f44b4db93f7ab7a1cad98271f84f1c1717405a65.tar.gz xine-lib-f44b4db93f7ab7a1cad98271f84f1c1717405a65.tar.bz2 |
Rewrite demux_real and demux_realaudio for style.
Also try to simplify frame buffer allocation.
--HG--
extra : transplant_source : %B6%B5o%A8%24%E1%F5B%D2%D8%08%F8%DE%E7%9E%B6%B8C%A4j
-rw-r--r-- | src/demuxers/demux_real.c | 679 | ||||
-rw-r--r-- | src/demuxers/demux_realaudio.c | 102 |
2 files changed, 358 insertions, 423 deletions
diff --git a/src/demuxers/demux_real.c b/src/demuxers/demux_real.c index 48bf24969..f258fd73a 100644 --- a/src/demuxers/demux_real.c +++ b/src/demuxers/demux_real.c @@ -58,12 +58,10 @@ #include "real_common.h" #define FOURCC_TAG BE_FOURCC -#define RMF_TAG FOURCC_TAG('.', 'R', 'M', 'F') #define PROP_TAG FOURCC_TAG('P', 'R', 'O', 'P') #define MDPR_TAG FOURCC_TAG('M', 'D', 'P', 'R') #define CONT_TAG FOURCC_TAG('C', 'O', 'N', 'T') #define DATA_TAG FOURCC_TAG('D', 'A', 'T', 'A') -#define INDX_TAG FOURCC_TAG('I', 'N', 'D', 'X') #define RA_TAG FOURCC_TAG('.', 'r', 'a', 0xfd) #define VIDO_TAG FOURCC_TAG('V', 'I', 'D', 'O') @@ -176,6 +174,18 @@ typedef struct { demux_class_t demux_class; } demux_real_class_t; +static int is_indx_tag(const void *ptr) { + return memcmp(ptr, "INDX", 4) == 0; +} + +static int is_data_tag(const void *ptr) { + return memcmp(ptr, "DATA", 4) == 0; +} + +static int is_rmf_tag(const void *ptr) { + return memcmp(ptr, ".RMF", 4) == 0; +} + static void real_parse_index(demux_real_t *this) { off_t next_index_chunk = this->index_start; @@ -183,8 +193,6 @@ static void real_parse_index(demux_real_t *this) { unsigned char index_chunk_header[INDEX_CHUNK_HEADER_SIZE]; unsigned char index_record[INDEX_RECORD_SIZE]; int i; - unsigned int entries, stream_num; - real_index_entry_t **index; while(next_index_chunk) { lprintf("reading index chunk at %"PRIX64"\n", next_index_chunk); @@ -200,70 +208,68 @@ static void real_parse_index(demux_real_t *this) { } /* Check chunk is actually an index chunk */ - if(_X_BE_32(&index_chunk_header[0]) == INDX_TAG) { - unsigned short version; + if(!is_indx_tag(&index_chunk_header[0])) { + lprintf("expected index chunk found chunk type: %.4s\n", &index_chunk_header[0]); + break; + } - /* Check version */ - version = _X_BE_16(&index_chunk_header[8]); - if(version != 0) { - xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, - "demux_real: unknown object version in INDX: 0x%04x\n", version); - break; - } + /* Check version */ + const uint16_t version = _X_BE_16(&index_chunk_header[8]); + if(version != 0) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "demux_real: unknown object version in INDX: 0x%04x\n", version); + break; + } - /* Read data from header */ - entries = _X_BE_32(&index_chunk_header[10]); - stream_num = _X_BE_16(&index_chunk_header[14]); - next_index_chunk = _X_BE_32(&index_chunk_header[16]); - - /* Find which stream this index is for */ - index = NULL; - for(i = 0; i < this->num_video_streams; i++) { - if(stream_num == this->video_streams[i].mdpr->stream_number) { - index = &this->video_streams[i].index; - this->video_streams[i].index_entries = entries; - lprintf("found index chunk for video stream with num %d\n", stream_num); - break; - } + /* Read data from header */ + const uint32_t entries = _X_BE_32(&index_chunk_header[10]); + const uint16_t stream_num = _X_BE_16(&index_chunk_header[14]); + next_index_chunk = _X_BE_32(&index_chunk_header[16]); + + /* Find which stream this index is for */ + real_index_entry_t **index = NULL; + for(i = 0; i < this->num_video_streams; i++) { + if(stream_num == this->video_streams[i].mdpr->stream_number) { + index = &this->video_streams[i].index; + this->video_streams[i].index_entries = entries; + lprintf("found index chunk for video stream with num %d\n", stream_num); + break; } + } - if(!index) { - for(i = 0; i < this->num_audio_streams; i++) { - if(stream_num == this->audio_streams[i].mdpr->stream_number) { - index = &this->audio_streams[i].index; - this->audio_streams[i].index_entries = entries; - lprintf("found index chunk for audio stream with num %d\n", stream_num); - break; - } - } + if(!index) { + for(i = 0; i < this->num_audio_streams; i++) { + if(stream_num == this->audio_streams[i].mdpr->stream_number) { + index = &this->audio_streams[i].index; + this->audio_streams[i].index_entries = entries; + lprintf("found index chunk for audio stream with num %d\n", stream_num); + break; + } } + } - if(index && entries) - /* Allocate memory for index */ - *index = calloc(entries, sizeof(real_index_entry_t)); + if(index && entries) + /* Allocate memory for index */ + *index = calloc(entries, sizeof(real_index_entry_t)); - if(index && entries && *index) { - /* Read index */ - for(i = 0; i < entries; i++) { - if(this->input->read(this->input, index_record, INDEX_RECORD_SIZE) - != INDEX_RECORD_SIZE) { - lprintf("index record not read\n"); - free(*index); - *index = NULL; - break; - } - - (*index)[i].timestamp = _X_BE_32(&index_record[2]); - (*index)[i].offset = _X_BE_32(&index_record[6]); - (*index)[i].packetno = _X_BE_32(&index_record[10]); - } - } else { - lprintf("unused index chunk with %d entries for stream num %d\n", - entries, stream_num); + if(index && entries && *index) { + /* Read index */ + for(i = 0; i < entries; i++) { + if(this->input->read(this->input, index_record, INDEX_RECORD_SIZE) + != INDEX_RECORD_SIZE) { + lprintf("index record not read\n"); + free(*index); + *index = NULL; + break; + } + + (*index)[i].timestamp = _X_BE_32(&index_record[2]); + (*index)[i].offset = _X_BE_32(&index_record[6]); + (*index)[i].packetno = _X_BE_32(&index_record[10]); } } else { - lprintf("expected index chunk found chunk type: %.4s\n", &index_chunk_header[0]); - break; + lprintf("unused index chunk with %d entries for stream num %d\n", + entries, stream_num); } } @@ -325,17 +331,12 @@ static void real_free_mdpr (mdpr_t *mdpr) { static void real_parse_audio_specific_data (demux_real_t *this, real_stream_t * stream, - uint8_t * data, int len) + uint8_t * data) { - int coded_frame_size; - int codec_data_length; - int coded_frame_size2; - int subpacket_size; - - coded_frame_size = _X_BE_32 (data+24); - codec_data_length = _X_BE_16 (data+40); - coded_frame_size2 = _X_BE_16 (data+42); - subpacket_size = _X_BE_16 (data+44); + const uint32_t coded_frame_size = _X_BE_32 (data+24); + const uint16_t codec_data_length = _X_BE_16 (data+40); + const uint16_t coded_frame_size2 = _X_BE_16 (data+42); + const uint16_t subpacket_size = _X_BE_16 (data+44); stream->sps = subpacket_size; stream->w = coded_frame_size2; @@ -365,15 +366,15 @@ static void real_parse_audio_specific_data (demux_real_t *this, "demux_real: error, i don't handle buf type 0x%08x\n", stream->buf_type); } - if (stream->sps) { - stream->frame_size = stream->w / stream->sps * stream->h * stream->sps; - stream->frame_buffer = xine_xmalloc (stream->frame_size); - stream->frame_num_bytes = 0; - } else { - stream->frame_size = stream->w * stream->h; - stream->frame_buffer = xine_xmalloc (stream->frame_size); - stream->frame_num_bytes = 0; - } + /* + * when stream->sps is set it used to do this: + * stream->frame_size = stream->w / stream->sps * stream->h * stream->sps; + * but it looks pointless? the compiler will probably optimise it away, I suppose? + */ + stream->frame_size = stream->w * stream->h; + + stream->frame_buffer = calloc(stream->frame_size, 1); + stream->frame_num_bytes = 0; stream->sub_packet_cnt = 0; xprintf (this->stream->xine, XINE_VERBOSITY_LOG, @@ -387,37 +388,35 @@ static void real_parse_headers (demux_real_t *this) { char preamble[PREAMBLE_SIZE]; unsigned int chunk_type = 0; unsigned int chunk_size; - unsigned short version; - unsigned char *chunk_buffer; - int field_size; - int stream_ptr; - unsigned char data_chunk_header[DATA_CHUNK_HEADER_SIZE]; - unsigned char signature[REAL_SIGNATURE_SIZE]; - - this->data_start = 0; - this->data_size = 0; - this->num_video_streams = 0; - this->num_audio_streams = 0; if (INPUT_IS_SEEKABLE(this->input)) this->input->seek (this->input, 0, SEEK_SET); - if (this->input->read(this->input, signature, REAL_SIGNATURE_SIZE) != - REAL_SIGNATURE_SIZE) { + { + uint8_t signature[REAL_SIGNATURE_SIZE]; + if (this->input->read(this->input, signature, REAL_SIGNATURE_SIZE) != + REAL_SIGNATURE_SIZE) { + + lprintf ("signature not read\n"); + this->status = DEMUX_FINISHED; + return; + } + + if ( !is_rmf_tag(signature) ) { + this->status = DEMUX_FINISHED; + lprintf ("signature not found '%.4s'\n", signature); + return; + } - lprintf ("signature not read\n"); - this->status = DEMUX_FINISHED; - return; + /* skip to the start of the first chunk and start traversing */ + chunk_size = _X_BE_32(&signature[4]); } - if (_X_BE_32(signature) != RMF_TAG) { - this->status = DEMUX_FINISHED; - lprintf ("signature not found '%.4s'\n", signature); - return; - } + this->data_start = 0; + this->data_size = 0; + this->num_video_streams = 0; + this->num_audio_streams = 0; - /* skip to the start of the first chunk and start traversing */ - chunk_size = _X_BE_32(&signature[4]); this->input->seek(this->input, chunk_size-8, SEEK_CUR); /* iterate through chunks and gather information until the first DATA @@ -438,183 +437,175 @@ static void real_parse_headers (demux_real_t *this) { case PROP_TAG: case MDPR_TAG: case CONT_TAG: - - chunk_size -= PREAMBLE_SIZE; - chunk_buffer = malloc(chunk_size); - if (this->input->read(this->input, chunk_buffer, chunk_size) != - chunk_size) { - free (chunk_buffer); - this->status = DEMUX_FINISHED; - return; - } + { + chunk_size -= PREAMBLE_SIZE; + uint8_t *const chunk_buffer = malloc(chunk_size); + if (this->input->read(this->input, chunk_buffer, chunk_size) != + chunk_size) { + free (chunk_buffer); + this->status = DEMUX_FINISHED; + return; + } - version = _X_BE_16(&chunk_buffer[0]); + uint16_t version = _X_BE_16(&chunk_buffer[0]); - if (chunk_type == PROP_TAG) { + if (chunk_type == PROP_TAG) { - if(version != 0) { - xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, - "demuxe_real: unknown object version in PROP: 0x%04x\n", version); - free(chunk_buffer); - this->status = DEMUX_FINISHED; - return; - } + if(version != 0) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "demuxe_real: unknown object version in PROP: 0x%04x\n", version); + free(chunk_buffer); + this->status = DEMUX_FINISHED; + return; + } - this->duration = _X_BE_32(&chunk_buffer[22]); - this->index_start = _X_BE_32(&chunk_buffer[30]); - this->data_start = _X_BE_32(&chunk_buffer[34]); - this->avg_bitrate = _X_BE_32(&chunk_buffer[6]); + this->duration = _X_BE_32(&chunk_buffer[22]); + this->index_start = _X_BE_32(&chunk_buffer[30]); + this->data_start = _X_BE_32(&chunk_buffer[34]); + this->avg_bitrate = _X_BE_32(&chunk_buffer[6]); - lprintf("PROP: duration: %d ms\n", this->duration); - lprintf("PROP: index start: %"PRIX64"\n", this->index_start); - lprintf("PROP: data start: %"PRIX64"\n", this->data_start); - lprintf("PROP: average bit rate: %"PRId64"\n", this->avg_bitrate); + lprintf("PROP: duration: %d ms\n", this->duration); + lprintf("PROP: index start: %"PRIX64"\n", this->index_start); + lprintf("PROP: data start: %"PRIX64"\n", this->data_start); + lprintf("PROP: average bit rate: %"PRId64"\n", this->avg_bitrate); - if (this->avg_bitrate<1) - this->avg_bitrate = 1; + if (this->avg_bitrate<1) + this->avg_bitrate = 1; - _x_stream_info_set(this->stream, XINE_STREAM_INFO_BITRATE, + _x_stream_info_set(this->stream, XINE_STREAM_INFO_BITRATE, this->avg_bitrate); - } else if (chunk_type == MDPR_TAG) { - - mdpr_t *mdpr; - uint32_t fourcc; - - if (version != 0) { - xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, - "demux_real: unknown object version in MDPR: 0x%04x\n", version); - free(chunk_buffer); - continue; - } + } else if (chunk_type == MDPR_TAG) { + if (version != 0) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "demux_real: unknown object version in MDPR: 0x%04x\n", version); + free(chunk_buffer); + continue; + } - mdpr = real_parse_mdpr (chunk_buffer); - - lprintf ("parsing type specific data...\n"); - if(!strcmp(mdpr->mime_type, "audio/X-MP3-draft-00")) { - lprintf ("mpeg layer 3 audio detected...\n"); - - fourcc = ME_FOURCC('a', 'd', 'u', 0x55); - this->audio_streams[this->num_audio_streams].fourcc = fourcc; - this->audio_streams[this->num_audio_streams].buf_type = _x_formattag_to_buf_audio(fourcc); - this->audio_streams[this->num_audio_streams].index = NULL; - this->audio_streams[this->num_audio_streams].mdpr = mdpr; - this->num_audio_streams++; - } else if(_X_BE_32(mdpr->type_specific_data) == RA_TAG) { - int version, len; - - if(this->num_audio_streams == MAX_AUDIO_STREAMS) { - xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, - "demux_real: maximum number of audio stream exceeded\n"); - goto unknown; - } + mdpr_t *const mdpr = real_parse_mdpr (chunk_buffer); + + lprintf ("parsing type specific data...\n"); + if(!strcmp(mdpr->mime_type, "audio/X-MP3-draft-00")) { + lprintf ("mpeg layer 3 audio detected...\n"); + + static const uint32_t fourcc = ME_FOURCC('a', 'd', 'u', 0x55); + this->audio_streams[this->num_audio_streams].fourcc = fourcc; + this->audio_streams[this->num_audio_streams].buf_type = _x_formattag_to_buf_audio(fourcc); + this->audio_streams[this->num_audio_streams].index = NULL; + this->audio_streams[this->num_audio_streams].mdpr = mdpr; + this->num_audio_streams++; + } else if(_X_BE_32(mdpr->type_specific_data) == RA_TAG) { + if(this->num_audio_streams == MAX_AUDIO_STREAMS) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "demux_real: maximum number of audio stream exceeded\n"); + goto unknown; + } - version = _X_BE_16(mdpr->type_specific_data + 4); + const uint16_t version = _X_BE_16(mdpr->type_specific_data + 4); - lprintf("audio version %d detected\n", version); + lprintf("audio version %d detected\n", version); - switch(version) { + char *fourcc_ptr = NULL; + switch(version) { case 3: /* Version 3 header stores fourcc after meta info - cheat by reading backwards from the * end of the header instead of having to parse it all */ - fourcc = _X_ME_32(mdpr->type_specific_data + mdpr->type_specific_len - 5); + fourcc_ptr = mdpr->type_specific_data + mdpr->type_specific_len - 5; break; - case 4: - len = *(mdpr->type_specific_data + 56); - fourcc = _X_ME_32(mdpr->type_specific_data + 58 + len); + case 4: { + const uint8_t len = *(mdpr->type_specific_data + 56); + fourcc_ptr = mdpr->type_specific_data + 58 + len; + } break; case 5: - fourcc = _X_ME_32(mdpr->type_specific_data + 66); + fourcc_ptr = mdpr->type_specific_data + 66; break; default: lprintf("unsupported audio header version %d\n", version); goto unknown; - } + } - lprintf("fourcc = %.4s\n", (char *) &fourcc); + lprintf("fourcc = %.4s\n", fourcc_ptr); - this->audio_streams[this->num_audio_streams].fourcc = fourcc; - this->audio_streams[this->num_audio_streams].buf_type = _x_formattag_to_buf_audio(fourcc); - this->audio_streams[this->num_audio_streams].index = NULL; - this->audio_streams[this->num_audio_streams].mdpr = mdpr; + const uint32_t fourcc = _X_ME_32(fourcc_ptr); - real_parse_audio_specific_data (this, - &this->audio_streams[this->num_audio_streams], - mdpr->type_specific_data, - mdpr->type_specific_len); - this->num_audio_streams++; + this->audio_streams[this->num_audio_streams].fourcc = fourcc; + this->audio_streams[this->num_audio_streams].buf_type = _x_formattag_to_buf_audio(fourcc); + this->audio_streams[this->num_audio_streams].index = NULL; + this->audio_streams[this->num_audio_streams].mdpr = mdpr; - } else if(_X_BE_32(mdpr->type_specific_data + 4) == VIDO_TAG) { + real_parse_audio_specific_data (this, + &this->audio_streams[this->num_audio_streams], + mdpr->type_specific_data); + this->num_audio_streams++; - if(this->num_video_streams == MAX_VIDEO_STREAMS) { - xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, - "demux_real: maximum number of video stream exceeded\n"); - goto unknown; - } + } else if(_X_BE_32(mdpr->type_specific_data + 4) == VIDO_TAG) { + + if(this->num_video_streams == MAX_VIDEO_STREAMS) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "demux_real: maximum number of video stream exceeded\n"); + goto unknown; + } - lprintf ("video detected\n"); - fourcc = _X_ME_32(mdpr->type_specific_data + 8); - lprintf("fourcc = %.4s\n", (char *) &fourcc); + lprintf ("video detected\n"); + const uint32_t fourcc = _X_ME_32(mdpr->type_specific_data + 8); + lprintf("fourcc = %.4s\n", (char *) &fourcc); - this->video_streams[this->num_video_streams].fourcc = fourcc; - this->video_streams[this->num_video_streams].buf_type = _x_fourcc_to_buf_video(fourcc); - this->video_streams[this->num_video_streams].format = _X_BE_32(mdpr->type_specific_data + 30); - this->video_streams[this->num_video_streams].index = NULL; - this->video_streams[this->num_video_streams].mdpr = mdpr; + this->video_streams[this->num_video_streams].fourcc = fourcc; + this->video_streams[this->num_video_streams].buf_type = _x_fourcc_to_buf_video(fourcc); + this->video_streams[this->num_video_streams].format = _X_BE_32(mdpr->type_specific_data + 30); + this->video_streams[this->num_video_streams].index = NULL; + this->video_streams[this->num_video_streams].mdpr = mdpr; - this->num_video_streams++; + this->num_video_streams++; - } else { - lprintf("unrecognised type specific data\n"); + } else { + lprintf("unrecognised type specific data\n"); -unknown: - real_free_mdpr(mdpr); - } + unknown: + real_free_mdpr(mdpr); + } - } else if (chunk_type == CONT_TAG) { + } else if (chunk_type == CONT_TAG) { - if(version != 0) { - xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, - "demux_real: unknown object version in CONT: 0x%04x\n", version); - free(chunk_buffer); - continue; - } + if(version != 0) { + xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, + "demux_real: unknown object version in CONT: 0x%04x\n", version); + free(chunk_buffer); + continue; + } - stream_ptr = 2; - - /* load the title string */ - field_size = _X_BE_16(&chunk_buffer[stream_ptr]); - stream_ptr += 2; - _x_meta_info_n_set(this->stream, XINE_META_INFO_TITLE, - &chunk_buffer[stream_ptr], field_size); - stream_ptr += field_size; - - /* load the author string */ - field_size = _X_BE_16(&chunk_buffer[stream_ptr]); - stream_ptr += 2; - _x_meta_info_n_set(this->stream, XINE_META_INFO_ARTIST, - &chunk_buffer[stream_ptr], field_size); - stream_ptr += field_size; - - /* load the copyright string as the year */ - field_size = _X_BE_16(&chunk_buffer[stream_ptr]); - stream_ptr += 2; - _x_meta_info_n_set(this->stream, XINE_META_INFO_YEAR, - &chunk_buffer[stream_ptr], field_size); - stream_ptr += field_size; - - /* load the comment string */ - field_size = _X_BE_16(&chunk_buffer[stream_ptr]); - stream_ptr += 2; - _x_meta_info_n_set(this->stream, XINE_META_INFO_COMMENT, - &chunk_buffer[stream_ptr], field_size); - stream_ptr += field_size; - } + int stream_ptr = 2; +#define SET_METADATA_STRING(type) \ + do { \ + const uint16_t field_size = _X_BE_16(&chunk_buffer[stream_ptr]); \ + stream_ptr += 2; \ + _x_meta_info_n_set(this->stream, type, \ + &chunk_buffer[stream_ptr], field_size); \ + stream_ptr += field_size; \ + } while(0) + + /* load the title string */ + SET_METADATA_STRING(XINE_META_INFO_TITLE); + + /* load the author string */ + SET_METADATA_STRING(XINE_META_INFO_ARTIST); + + /* load the copyright string as the year */ + SET_METADATA_STRING(XINE_META_INFO_YEAR); + + /* load the comment string */ + SET_METADATA_STRING(XINE_META_INFO_COMMENT); + } - free(chunk_buffer); + free(chunk_buffer); + } break; - case DATA_TAG: + case DATA_TAG: { + uint8_t data_chunk_header[DATA_CHUNK_HEADER_SIZE]; + if (this->input->read(this->input, data_chunk_header, DATA_CHUNK_HEADER_SIZE) != DATA_CHUNK_HEADER_SIZE) { this->status = DEMUX_FINISHED; @@ -622,7 +613,7 @@ unknown: } /* check version */ - version = _X_BE_16(&data_chunk_header[0]); + const uint16_t version = _X_BE_16(&data_chunk_header[0]); if(version != 0) { xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_real: unknown object version in DATA: 0x%04x\n", version); @@ -633,7 +624,8 @@ unknown: this->current_data_chunk_packet_count = _X_BE_32(&data_chunk_header[2]); this->next_data_chunk_offset = _X_BE_32(&data_chunk_header[6]); this->data_chunk_size = chunk_size; - break; + } + break; default: /* this should not occur, but in case it does, skip the chunk */ @@ -649,16 +641,9 @@ unknown: real_parse_index(this); /* Simple stream selection case - 0/1 audio/video streams */ - if(this->num_video_streams == 1) - this->video_stream = &this->video_streams[0]; - else - this->video_stream = NULL; + this->video_stream = (this->num_video_streams == 1) ? &this->video_streams[0] : NULL; + this->audio_stream = (this->num_audio_streams == 1) ? &this->audio_streams[0] : NULL; - if(this->num_audio_streams == 1) - this->audio_stream = &this->audio_streams[0]; - else - this->audio_stream = NULL; - /* In the case of multiple audio/video streams select the first streams found in the file */ if((this->num_video_streams > 1) || (this->num_audio_streams > 1)) { @@ -698,15 +683,13 @@ unknown: while((offset < len) && ((!this->video_stream && (this->num_video_streams > 0)) || (!this->audio_stream && (this->num_audio_streams > 0)))) { - uint32_t id; - int i, stream; + int i; /* Check for end of the data chunk */ - if(((id = _X_BE_32(&search_buffer[offset])) == DATA_TAG) || - (id == INDX_TAG)) - break; + if (is_indx_tag(&search_buffer[offset]) || is_data_tag(&search_buffer[offset])) + break; - stream = _X_BE_16(&search_buffer[offset + 4]); + const int stream = _X_BE_16(&search_buffer[offset + 4]); for(i = 0; !this->video_stream && (i < this->num_video_streams); i++) { if(stream == this->video_streams[i].mdpr->stream_number) { @@ -740,14 +723,12 @@ unknown: /* Send headers and set meta info */ if(this->video_stream) { - buf_element_t *buf; - /* Check for recognised codec*/ if(!this->video_stream->buf_type) this->video_stream->buf_type = BUF_VIDEO_UNKNOWN; /* Send header */ - buf = this->video_fifo->buffer_pool_alloc(this->video_fifo); + buf_element_t *const buf = this->video_fifo->buffer_pool_alloc(this->video_fifo); buf->content = buf->mem; memcpy(buf->content, this->video_stream->mdpr->type_specific_data, @@ -780,10 +761,9 @@ unknown: /* Send headers */ if(this->audio_fifo) { - mdpr_t *mdpr = this->audio_stream->mdpr; - buf_element_t *buf; + mdpr_t *const mdpr = this->audio_stream->mdpr; - buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo); + buf_element_t *buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo); buf->type = this->audio_stream->buf_type; buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_FRAME_END; @@ -793,7 +773,7 @@ unknown: * The second is the codec initialisation data found at the end of * the type specific data for the audio stream */ if(buf->type == BUF_AUDIO_AAC) { - int version = _X_BE_16(mdpr->type_specific_data + 4); + const uint16_t version = _X_BE_16(mdpr->type_specific_data + 4); if(version != 5) { xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, @@ -954,9 +934,7 @@ static int demux_real_parse_references( demux_real_t *this) { #define PTS_VIDEO 1 static void check_newpts (demux_real_t *this, int64_t pts, int video, int preview) { - int64_t diff; - - diff = pts - this->last_pts[video]; + const int64_t diff = pts - this->last_pts[video]; lprintf ("check_newpts %"PRId64"\n", pts); if (!preview && pts && @@ -1062,19 +1040,14 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { demux_real_t *this = (demux_real_t *) this_gen; char header[DATA_PACKET_HEADER_SIZE]; - int stream, size, keyframe, input_time = 0; - unsigned short version; - uint32_t id, timestamp; - int64_t pts; - off_t offset, input_length = 0; + int keyframe, input_time = 0; int normpos = 0; - read_next_packet: if(this->reference_mode) return demux_real_parse_references(this); /* load a header from wherever the stream happens to be pointing */ - if ( (size=this->input->read(this->input, header, DATA_PACKET_HEADER_SIZE)) != + if ( this->input->read(this->input, header, DATA_PACKET_HEADER_SIZE) != DATA_PACKET_HEADER_SIZE) { xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, @@ -1085,15 +1058,14 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { } /* Check to see if we've gone past the end of the data chunk */ - if(((id = _X_BE_32(&header[0])) == DATA_TAG) || - (id == INDX_TAG)) { + if (is_indx_tag(&header[0]) || is_data_tag(&header[0])) { lprintf("finished reading data chunk\n"); this->status = DEMUX_FINISHED; return this->status; } /* check version */ - version = _X_BE_16(&header[0]); + const uint16_t version = _X_BE_16(&header[0]); if(version > 1) { xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_real: unknown object version in data packet: 0x%04x\n", version); @@ -1102,11 +1074,11 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { } /* read the packet information */ - stream = _X_BE_16(&header[4]); - offset = this->input->get_current_pos(this->input); - size = _X_BE_16(&header[2]) - DATA_PACKET_HEADER_SIZE; - timestamp= _X_BE_32(&header[6]); - pts = (int64_t) timestamp * 90; + const uint16_t stream = _X_BE_16(&header[4]); + const off_t offset = this->input->get_current_pos(this->input); + uint16_t size = _X_BE_16(&header[2]) - DATA_PACKET_HEADER_SIZE; + const uint32_t timestamp= _X_BE_32(&header[6]); + int64_t pts = (int64_t) timestamp * 90; /* Data packet header with version 1 contains 1 extra byte */ if(version == 0) @@ -1121,11 +1093,9 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { if (this->video_stream && (stream == this->video_stream->mdpr->stream_number)) { - int vpkg_header, vpkg_length, vpkg_offset; int vpkg_seqnum = -1; int vpkg_subseq = 0; buf_element_t *buf; - int n, fragment_size; uint32_t decoder_flags; lprintf ("video chunk detected.\n"); @@ -1140,21 +1110,20 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { * bit 6: 1=short header (only one block?) */ - vpkg_header = stream_read_char (this); size--; + const int vpkg_header = stream_read_char (this); size--; lprintf ("vpkg_hdr: %02x (size=%d)\n", vpkg_header, size); + int vpkg_length, vpkg_offset; if (0x40==(vpkg_header&0xc0)) { /* * seems to be a very short header * 2 bytes, purpose of the second byte yet unknown */ - int bummer; - - bummer = stream_read_char (this); size--; + const int bummer = stream_read_char (this); lprintf ("bummer == %02X\n",bummer); vpkg_offset = 0; - vpkg_length = size; + vpkg_length = --size; } else { @@ -1215,9 +1184,8 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { input_time = (int)((int64_t) this->input->get_current_pos(this->input) * 8 * 1000 / this->avg_bitrate); - if(this->data_start && this->data_chunk_size) - input_length = this->data_start + 18 + this->data_chunk_size; - if( input_length ) + const off_t input_length = this->data_start + 18 + this->data_chunk_size; + if( input_length > 18 ) normpos = (int)((double) this->input->get_current_pos(this->input) * 65535 / input_length); check_newpts (this, pts, PTS_VIDEO, 0); @@ -1262,13 +1230,17 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { * calc size of fragment */ - if ((vpkg_header & 0xc0) == 0x080) + int fragment_size; + switch(vpkg_header & 0xc0) { + case 0x80: fragment_size = vpkg_offset; - else { - if (0x00 == (vpkg_header&0xc0)) - fragment_size = size; - else - fragment_size = vpkg_length; + break; + case 0x00: + fragment_size = size; + break; + default: + fragment_size = vpkg_length; + break; } lprintf ("fragment size is %d\n", fragment_size); @@ -1276,14 +1248,11 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { * read fragment_size bytes of data */ - n = fragment_size; + int n = fragment_size; while(n) { buf = this->video_fifo->buffer_pool_alloc(this->video_fifo); - if(n>buf->max_size) - buf->size = buf->max_size; - else - buf->size = n; + buf->size = MIN(n, buf->max_size); buf->decoder_flags = decoder_flags; decoder_flags &= ~BUF_FLAG_FRAME_START; @@ -1367,12 +1336,9 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { input_time = (int)((int64_t) this->input->get_current_pos(this->input) * 8 * 1000 / this->avg_bitrate); - if(this->data_start && this->data_chunk_size) - input_length = this->data_start + 18 + this->data_chunk_size; - else - input_length = 0; + const off_t input_length = this->data_start + 18 + this->data_chunk_size; - if( input_length ) + if( input_length > 18 ) normpos = (int)((double) this->input->get_current_pos(this->input) * 65535 / input_length); check_newpts (this, pts, PTS_AUDIO, 0); @@ -1380,13 +1346,14 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { /* Each packet of AAC is made up of several AAC frames preceded by a * header defining the size of the frames */ if(this->audio_stream->buf_type == BUF_AUDIO_AAC) { - int i, frames, *sizes; + int i; /* Upper 4 bits of second byte is frame count */ - frames = (stream_read_word(this) & 0xf0) >> 4; + const int frames = (stream_read_word(this) & 0xf0) >> 4; /* 2 bytes per frame size */ - sizes = calloc(frames, sizeof(int)); + int sizes[frames]; + for(i = 0; i < frames; i++) sizes[i] = stream_read_word(this); @@ -1398,15 +1365,12 @@ static int demux_real_send_chunk(demux_plugin_t *this_gen) { xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_real: failed to read AAC frame\n"); - free(sizes); this->status = DEMUX_FINISHED; return this->status; } pts = 0; /* Only set pts on first frame */ } - - free(sizes); } else if (this->audio_stream->buf_type == BUF_AUDIO_COOK || this->audio_stream->buf_type == BUF_AUDIO_ATRK || this->audio_stream->buf_type == BUF_AUDIO_28_8 || @@ -1497,18 +1461,10 @@ discard: /* check if it's time to reload */ if (!this->current_data_chunk_packet_count && this->next_data_chunk_offset) { - char preamble[PREAMBLE_SIZE]; unsigned char data_chunk_header[DATA_CHUNK_HEADER_SIZE]; /* seek to the next DATA chunk offset */ - this->input->seek(this->input, this->next_data_chunk_offset, SEEK_SET); - - /* load the DATA chunk preamble */ - if (this->input->read(this->input, preamble, PREAMBLE_SIZE) != - PREAMBLE_SIZE) { - this->status = DEMUX_FINISHED; - return this->status; - } + this->input->seek(this->input, this->next_data_chunk_offset + PREAMBLE_SIZE, SEEK_SET); /* load the rest of the DATA chunk header */ if (this->input->read(this->input, data_chunk_header, @@ -1647,21 +1603,16 @@ static void demux_real_dispose (demux_plugin_t *this_gen) { for(i = 0; i < this->num_video_streams; i++) { real_free_mdpr(this->video_streams[i].mdpr); - if(this->video_streams[i].index) - free(this->video_streams[i].index); + free(this->video_streams[i].index); } for(i = 0; i < this->num_audio_streams; i++) { real_free_mdpr(this->audio_streams[i].mdpr); - if(this->audio_streams[i].index) - free(this->audio_streams[i].index); - if(this->audio_streams[i].frame_buffer) - free(this->audio_streams[i].frame_buffer); + free(this->audio_streams[i].index); + free(this->audio_streams[i].frame_buffer); } - if(this->fragment_tab) - free(this->fragment_tab); - + free(this->fragment_tab); free(this); } @@ -1688,21 +1639,27 @@ static int demux_real_get_optional_data(demux_plugin_t *this_gen, } /* help function to discover stream type. returns: + * -1 if couldn't read * 0 if not known. * 1 if normal stream. * 2 if reference stream. */ -static int real_check_stream_type(uint8_t *buf, int len) +static int real_check_stream_type(input_plugin_t *input) { - if ((buf[0] == 0x2e) - && (buf[1] == 'R') - && (buf[2] == 'M') - && (buf[3] == 'F')) + uint8_t buf[1024]; + off_t len = _x_demux_read_header(input, buf, sizeof(buf)); + + if ( len < 4 ) + return -1; + + if ( memcmp(buf, "\x2eRMF", 4) == 0 ) return 1; - buf[len] = '\0'; - if( strstr(buf,"pnm://") || strstr(buf,"rtsp://") || strstr(buf,"<smil>") || - !strncmp(buf,"http://",7) ) +#define my_strnstr(haystack, haystacklen, needle) \ + memmem(haystack, haystacklen, needle, sizeof(needle)) + + if( my_strnstr(buf, len, "pnm://") || my_strnstr(buf, len, "rtsp://") || + my_strnstr(buf, len, "<smil>") || !strncmp(buf, "http://", MIN(7, len)) ) return 2; return 0; @@ -1711,32 +1668,24 @@ static int real_check_stream_type(uint8_t *buf, int len) static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *stream, input_plugin_t *input) { - demux_real_t *this; - uint8_t buf[1024+1]; - int len, stream_type=0; - - switch (stream->content_detection_method) { - - case METHOD_BY_CONTENT:{ + /* discover stream type */ + const int stream_type = real_check_stream_type(input); - if (! (len = _x_demux_read_header(input, buf, 1024)) ) - return NULL; + if ( stream_type < 0 ) + return NULL; - lprintf ("read 4 bytes: %02x %02x %02x %02x\n", - buf[0], buf[1], buf[2], buf[3]); + switch (stream->content_detection_method) { - if (!(stream_type = real_check_stream_type(buf,len))) + case METHOD_BY_CONTENT: + if ( stream_type < 1 ) return NULL; - } - - lprintf ("by content accepted.\n"); - break; + + lprintf ("by content accepted.\n"); + break; case METHOD_BY_EXTENSION: { - const char *extensions, *mrl; - - mrl = input->get_mrl (input); - extensions = class_gen->get_extensions (class_gen); + const char *const mrl = input->get_mrl (input); + const char *const extensions = class_gen->get_extensions (class_gen); lprintf ("by extension '%s'\n", mrl); @@ -1755,17 +1704,11 @@ static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *str return NULL; } - - this = calloc(1, sizeof(demux_real_t)); + demux_real_t *this = calloc(1, sizeof(demux_real_t)); this->stream = stream; this->input = input; - /* discover stream type */ - if(!stream_type) - if ( (len = _x_demux_read_header(this->input, buf, 1024)) ) - stream_type = real_check_stream_type(buf,len); - if(stream_type == 2){ this->reference_mode = 1; lprintf("reference stream detected\n"); @@ -1812,9 +1755,7 @@ static void class_dispose (demux_class_t *this_gen) { } static void *init_class (xine_t *xine, void *data) { - demux_real_class_t *this; - - this = calloc(1, sizeof(demux_real_class_t)); + demux_real_class_t *const this = calloc(1, sizeof(demux_real_class_t)); this->demux_class.open_plugin = open_plugin; this->demux_class.get_description = get_description; diff --git a/src/demuxers/demux_realaudio.c b/src/demuxers/demux_realaudio.c index bc132adae..5b713cb60 100644 --- a/src/demuxers/demux_realaudio.c +++ b/src/demuxers/demux_realaudio.c @@ -64,7 +64,9 @@ typedef struct { off_t data_start; off_t data_size; - int sps, cfs, w, h; + uint32_t cfs; + uint16_t w, h; + int frame_len; int frame_size; uint8_t *frame_buffer; @@ -77,27 +79,22 @@ typedef struct { } demux_ra_class_t; /* Map flavour to bytes per second */ -static int sipr_fl2bps[4] = {813, 1062, 625, 2000}; // 6.5, 8.5, 5, 16 kbit per second +static const int sipr_fl2bps[4] = {813, 1062, 625, 2000}; // 6.5, 8.5, 5, 16 kbit per second /* returns 1 if the RealAudio file was opened successfully, 0 otherwise */ static int open_ra_file(demux_ra_t *this) { - unsigned char file_header[RA_FILE_HEADER_PREV_SIZE], len; - unsigned short version; - off_t offset; - + uint8_t file_header[RA_FILE_HEADER_PREV_SIZE]; /* check the signature */ if (_x_demux_read_header(this->input, file_header, RA_FILE_HEADER_PREV_SIZE) != RA_FILE_HEADER_PREV_SIZE) return 0; - if ((file_header[0] != '.') || - (file_header[1] != 'r') || - (file_header[2] != 'a')) + if ( memcmp(file_header, ".ra", 3) != 0 ) return 0; /* read version */ - version = _X_BE_16(&file_header[0x04]); + const uint16_t version = _X_BE_16(&file_header[0x04]); /* read header size according to version */ if (version == 3) @@ -118,6 +115,7 @@ static int open_ra_file(demux_ra_t *this) { return 0; } + off_t offset; /* read header data according to version */ if((version == 3) && (this->header_size >= 32)) { this->data_size = _X_BE_32(&this->header[0x12]); @@ -147,31 +145,37 @@ static int open_ra_file(demux_ra_t *this) { } /* Read title */ - len = this->header[offset]; - if(len && ((offset+len+2) < this->header_size)) { - _x_meta_info_n_set(this->stream, XINE_META_INFO_TITLE, - &this->header[offset+1], len); - offset += len+1; - } else - offset++; + { + const uint8_t len = this->header[offset]; + if(len && ((offset+len+2) < this->header_size)) { + _x_meta_info_n_set(this->stream, XINE_META_INFO_TITLE, + &this->header[offset+1], len); + offset += len+1; + } else + offset++; + } /* Author */ - len = this->header[offset]; - if(len && ((offset+len+1) < this->header_size)) { - _x_meta_info_n_set(this->stream, XINE_META_INFO_ARTIST, - &this->header[offset+1], len); - offset += len+1; - } else - offset++; + { + const uint8_t len = this->header[offset]; + if(len && ((offset+len+1) < this->header_size)) { + _x_meta_info_n_set(this->stream, XINE_META_INFO_ARTIST, + &this->header[offset+1], len); + offset += len+1; + } else + offset++; + } /* Copyright/Date */ - len = this->header[offset]; - if(len && ((offset+len) <= this->header_size)) { - _x_meta_info_n_set(this->stream, XINE_META_INFO_YEAR, - &this->header[offset+1], len); - offset += len+1; - } else - offset++; + { + const uint8_t len = this->header[offset]; + if(len && ((offset+len) <= this->header_size)) { + _x_meta_info_n_set(this->stream, XINE_META_INFO_YEAR, + &this->header[offset+1], len); + offset += len+1; + } else + offset++; + } /* Fourcc for version 3 comes after meta info */ if(version == 3) { @@ -193,17 +197,16 @@ static int open_ra_file(demux_ra_t *this) { this->audio_type = _x_formattag_to_buf_audio(this->fourcc); if (version == 4) { - this->sps = _X_BE_16 (this->header+44); + const uint16_t sps = _X_BE_16 (this->header+44) ? : 1; this->w = _X_BE_16 (this->header+42); this->h = _X_BE_16 (this->header+40); this->cfs = _X_BE_32 (this->header+24); - if (this->sps) { - this->frame_size = this->sps * this->h * this->sps; - this->frame_buffer = xine_xmalloc (this->frame_size); - } else { - this->frame_size = this->w * this->h; - this->frame_buffer = xine_xmalloc (this->frame_size); - } + + this->frame_len = this->w * this->h; + this->frame_size = this->frame_len * sps; + + this->frame_buffer = calloc(this->frame_size, 1); + _x_assert(this->frame_buffer != NULL); if (this->audio_type == BUF_AUDIO_28_8 || this->audio_type == BUF_AUDIO_SIPRO) this->block_align = this->cfs; @@ -227,7 +230,6 @@ static int demux_ra_send_chunk(demux_plugin_t *this_gen) { demux_ra_t *this = (demux_ra_t *) this_gen; off_t current_normpos = 0; - int64_t current_pts; /* just load data chunks from wherever the stream happens to be * pointing; issue a DEMUX_FINISHED status if EOF is reached */ @@ -235,7 +237,7 @@ static int demux_ra_send_chunk(demux_plugin_t *this_gen) { current_normpos = (int)( (double) (this->input->get_current_pos (this->input) - this->data_start) * 65535 / this->data_size ); - current_pts = 0; /* let the engine sort out the pts for now */ + const int64_t current_pts = 0; /* let the engine sort out the pts for now */ if (this->seek_flag) { _x_demux_control_newpts(this->stream, current_pts, BUF_FLAG_SEEK); @@ -243,26 +245,21 @@ static int demux_ra_send_chunk(demux_plugin_t *this_gen) { } if (this->audio_type == BUF_AUDIO_28_8 || this->audio_type == BUF_AUDIO_SIPRO) { - uint8_t * buffer; - - buffer = this->frame_buffer; if (this->audio_type == BUF_AUDIO_SIPRO) { - int len = this->h * this->w; - if(this->input->read(this->input, this->frame_buffer, len) < len) { + if(this->input->read(this->input, this->frame_buffer, this->frame_len) < this->frame_len) { xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_realaudio: failed to read audio chunk\n"); this->status = DEMUX_FINISHED; return this->status; } - demux_real_sipro_swap (this->frame_buffer, len * 2 / 96); + demux_real_sipro_swap (this->frame_buffer, this->frame_len * 2 / 96); } else { int x, y; - int pos; for (y = 0; y < this->h; y++) for (x = 0; x < this->h / 2; x++) { - pos = x * 2 * this->w + y * this->cfs; + const int pos = x * 2 * this->w + y * this->cfs; if(this->input->read(this->input, this->frame_buffer + pos, this->cfs) < this->cfs) { xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, @@ -275,7 +272,7 @@ static int demux_ra_send_chunk(demux_plugin_t *this_gen) { } _x_demux_send_data(this->audio_fifo, - buffer, this->frame_size, + this->frame_buffer, this->frame_size, current_pts, this->audio_type, 0, current_normpos, current_pts / 90, 0, 0); } else if(_x_demux_read_send_data(this->audio_fifo, this->input, this->block_align, @@ -310,10 +307,7 @@ static void demux_ra_send_headers(demux_plugin_t *this_gen) { buf->type = this->audio_type; buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_FRAME_END; - if(this->header_size > buf->max_size) - buf->size = buf->max_size; - else - buf->size = this->header_size; + buf->size = MIN(this->header_size, buf->max_size); memcpy(buf->content, this->header, buf->size); |