/* Following two functions are adapted from mad_timer, from the libmad distribution */ void scan(void const *ptr, ssize_t len, buffer *buf) { struct mad_stream stream; struct mad_header header; struct xing xing; unsigned long bitrate = 0; int has_xing = 0; int is_vbr = 0; mad_stream_init(&stream); mad_header_init(&header); mad_stream_buffer(&stream, ptr, len); buf->num_frames = 0; /* There are three ways of calculating the length of an mp3: 1) Constant bitrate: One frame can provide the information needed: # of frames and duration. Just see how long it is and do the division. 2) Variable bitrate: Xing tag. It provides the number of frames. Each frame has the same number of samples, so just use that. 3) All: Count up the frames and duration of each frames by decoding each one. We do this if we've no other choice, i.e. if it's a VBR file with no Xing tag. */ while (1) { if (mad_header_decode(&header, &stream) == -1) { if (MAD_RECOVERABLE(stream.error)) continue; else break; } /* Limit xing testing to the first frame header */ if (!buf->num_frames++) { if(parse_xing(&xing, stream.anc_ptr, stream.anc_bitlen)) { is_vbr = 1; if (xing.flags & XING_FRAMES) { /* We use the Xing tag only for frames. If it doesn't have that information, it's useless to us and we have to treat it as a normal VBR file */ has_xing = 1; buf->num_frames = xing.frames; break; } } } /* Test the first n frames to see if this is a VBR file */ if (!is_vbr && !(buf->num_frames > 20)) { if (bitrate && header.bitrate != bitrate) { is_vbr = 1; } else { bitrate = header.bitrate; } } /* We have to assume it's not a VBR file if it hasn't already been marked as one and we've checked n frames for different bitrates */ else if (!is_vbr) { break; } mad_timer_add(&buf->duration, header.duration); } if (!is_vbr) { double time = (len * 8.0) / (header.bitrate); /* time in seconds */ double timefrac = (double)time - ((long)(time)); long nsamples = 32 * MAD_NSBSAMPLES(&header); /* samples per frame */ /* samplerate is a constant */ buf->num_frames = (long) (time * header.samplerate / nsamples); mad_timer_set(&buf->duration, (long)time, (long)(timefrac*100), 100); } else if (has_xing) { /* modify header.duration since we don't need it anymore */ mad_timer_multiply(&header.duration, buf->num_frames); buf->duration = header.duration; } else { /* the durations have been added up, and the number of frames counted. We do nothing here. */ } mad_header_finish(&header); mad_stream_finish(&stream); }
/* * NAME: scan_header() * DESCRIPTION: read the initial frame(s) to get stream statistics */ static int scan_header(CPs_InStream* pInStream, struct mad_header *header, struct xing *xing) { struct mad_stream stream; struct mad_frame frame; unsigned char buffer[8192]; unsigned int buflen = 0; int count = 0, result = 0; mad_stream_init(&stream); mad_frame_init(&frame); if (xing) xing->flags = 0; while (1) { if (buflen < sizeof(buffer)) { // DWORD bytes; unsigned int bytes; if (pInStream->Read(pInStream, buffer + buflen, sizeof(buffer) - buflen, &bytes) == FALSE || bytes == 0) { result = -1; break; } buflen += bytes; } mad_stream_buffer(&stream, buffer, buflen); while (1) { if (mad_frame_decode(&frame, &stream) == -1) { if (!MAD_RECOVERABLE(stream.error)) break; continue; } if (count++ || (xing && parse_xing(xing, stream.anc_ptr, stream.anc_bitlen) == 0)) break; } if (count || stream.error != MAD_ERROR_BUFLEN) break; memmove(buffer, stream.next_frame, buflen = &buffer[buflen] - stream.next_frame); } if (count) { if (header) *header = frame.header; } else result = -1; mad_frame_finish(&frame); mad_stream_finish(&stream); return result; }
//Get info on file: //Uso LibMad per calcolare la durata del pezzo perché //altrimenti dovrei gestire il buffer anche nella seekNextFrame (senza è troppo lenta). //E' una porcheria ma è più semplice. :) int MP3MEgetInfo(){ unsigned long FrameCount = 0; int fd = -1; int bufferSize = 1024*496; u8 *localBuffer; long singleDataRed = 0; struct mad_stream stream; struct mad_header header; int timeFromID3 = 0; float mediumBitrate = 0.0f; int has_xing = 0; struct xing xing; memset(&xing, 0, sizeof(xing)); if (!MP3ME_tagRead) getMP3METagInfo(MP3ME_fileName, &MP3ME_info); mad_stream_init (&stream); mad_header_init (&header); fd = sceIoOpen(MP3ME_fileName, PSP_O_RDONLY, 0777); if (fd < 0) return -1; long size = sceIoLseek(fd, 0, PSP_SEEK_END); sceIoLseek(fd, 0, PSP_SEEK_SET); MP3ME_tagsize = ID3v2TagSize(MP3ME_fileName); double startPos = MP3ME_tagsize; sceIoLseek32(fd, startPos, PSP_SEEK_SET); //Check for xing frame: unsigned char *xing_buffer; xing_buffer = (unsigned char *)malloc(XING_BUFFER_SIZE); if (xing_buffer != NULL) { sceIoRead(fd, xing_buffer, XING_BUFFER_SIZE); if(parse_xing(xing_buffer, 0, &xing)) { if (xing.flags & XING_FRAMES && xing.frames){ has_xing = 1; bufferSize = 50 * 1024; } } free(xing_buffer); xing_buffer = NULL; } size -= startPos; if (size < bufferSize * 3) bufferSize = size; localBuffer = (unsigned char *) malloc(sizeof(unsigned char) * bufferSize); unsigned char *buff = localBuffer; MP3ME_info.fileType = MP3_TYPE; MP3ME_info.defaultCPUClock = MP3ME_defaultCPUClock; MP3ME_info.needsME = 1; MP3ME_info.fileSize = size; MP3ME_filesize = size; MP3ME_info.framesDecoded = 0; double totalBitrate = 0; int i = 0; for (i=0; i<3; i++){ memset(localBuffer, 0, bufferSize); singleDataRed = sceIoRead(fd, localBuffer, bufferSize); mad_stream_buffer (&stream, localBuffer, singleDataRed); while (1){ if (mad_header_decode (&header, &stream) == -1){ if (stream.buffer == NULL || stream.error == MAD_ERROR_BUFLEN) break; else if (MAD_RECOVERABLE(stream.error)){ continue; }else{ break; } } //Informazioni solo dal primo frame: if (FrameCount++ == 0){ switch (header.layer) { case MAD_LAYER_I: strcpy(MP3ME_info.layer,"I"); break; case MAD_LAYER_II: strcpy(MP3ME_info.layer,"II"); break; case MAD_LAYER_III: strcpy(MP3ME_info.layer,"III"); break; default: strcpy(MP3ME_info.layer,"unknown"); break; } MP3ME_info.kbit = header.bitrate / 1000; MP3ME_info.instantBitrate = header.bitrate; MP3ME_info.hz = header.samplerate; switch (header.mode) { case MAD_MODE_SINGLE_CHANNEL: strcpy(MP3ME_info.mode, "single channel"); break; case MAD_MODE_DUAL_CHANNEL: strcpy(MP3ME_info.mode, "dual channel"); break; case MAD_MODE_JOINT_STEREO: strcpy(MP3ME_info.mode, "joint (MS/intensity) stereo"); break; case MAD_MODE_STEREO: strcpy(MP3ME_info.mode, "normal LR stereo"); break; default: strcpy(MP3ME_info.mode, "unknown"); break; } switch (header.emphasis) { case MAD_EMPHASIS_NONE: strcpy(MP3ME_info.emphasis,"no"); break; case MAD_EMPHASIS_50_15_US: strcpy(MP3ME_info.emphasis,"50/15 us"); break; case MAD_EMPHASIS_CCITT_J_17: strcpy(MP3ME_info.emphasis,"CCITT J.17"); break; case MAD_EMPHASIS_RESERVED: strcpy(MP3ME_info.emphasis,"reserved(!)"); break; default: strcpy(MP3ME_info.emphasis,"unknown"); break; } //Check if lenght found in tag info: if (MP3ME_info.length > 0){ timeFromID3 = 1; break; } if (has_xing) break; } totalBitrate += header.bitrate; } if (size == bufferSize) break; else if (i==0) sceIoLseek(fd, startPos + size/3, PSP_SEEK_SET); else if (i==1) sceIoLseek(fd, startPos + 2 * size/3, PSP_SEEK_SET); if (timeFromID3 || has_xing) break; } mad_header_finish (&header); mad_stream_finish (&stream); if (buff){ free(buff); buff = NULL; } sceIoClose(fd); int secs = 0; if (has_xing) { /* modify header.duration since we don't need it anymore */ mad_timer_multiply(&header.duration, xing.frames); secs = mad_timer_count(header.duration, MAD_UNITS_SECONDS); MP3ME_info.length = secs; } else if (!MP3ME_info.length){ mediumBitrate = totalBitrate / (float)FrameCount; secs = size * 8 / mediumBitrate; MP3ME_info.length = secs; }else{ secs = MP3ME_info.length; } //Formatto in stringa la durata totale: int h = secs / 3600; int m = (secs - h * 3600) / 60; int s = secs - h * 3600 - m * 60; snprintf(MP3ME_info.strLength, sizeof(MP3ME_info.strLength), "%2.2i:%2.2i:%2.2i", h, m, s); return 0; }
static bool mp3_decode_first_frame(struct mp3_data *data, struct tag **tag, struct replay_gain_info **replay_gain_info_r) { struct xing xing; struct lame lame; struct mad_bitptr ptr; int bitlen; enum mp3_action ret; /* stfu gcc */ memset(&xing, 0, sizeof(struct xing)); xing.flags = 0; while (true) { do { ret = decode_next_frame_header(data, tag, replay_gain_info_r); } while (ret == DECODE_CONT); if (ret == DECODE_BREAK) return false; if (ret == DECODE_SKIP) continue; do { ret = decodeNextFrame(data); } while (ret == DECODE_CONT); if (ret == DECODE_BREAK) return false; if (ret == DECODE_OK) break; } ptr = data->stream.anc_ptr; bitlen = data->stream.anc_bitlen; mp3_filesize_to_song_length(data); /* * if an xing tag exists, use that! */ if (parse_xing(&xing, &ptr, &bitlen)) { data->found_xing = true; data->mute_frame = MUTEFRAME_SKIP; if ((xing.flags & XING_FRAMES) && xing.frames) { mad_timer_t duration = data->frame.header.duration; mad_timer_multiply(&duration, xing.frames); data->total_time = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000; data->max_frames = xing.frames; } if (parse_lame(&lame, &ptr, &bitlen)) { if (gapless_playback && data->input_stream->seekable) { data->drop_start_samples = lame.encoder_delay + DECODERDELAY; data->drop_end_samples = lame.encoder_padding; } /* Album gain isn't currently used. See comment in * parse_lame() for details. -- jat */ if (replay_gain_info_r && !*replay_gain_info_r && lame.track_gain) { *replay_gain_info_r = replay_gain_info_new(); (*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].gain = lame.track_gain; (*replay_gain_info_r)->tuples[REPLAY_GAIN_TRACK].peak = lame.peak; } } } if (!data->max_frames) return false; if (data->max_frames > 8 * 1024 * 1024) { g_warning("mp3 file header indicates too many frames: %lu\n", data->max_frames); return false; } data->frame_offsets = g_malloc(sizeof(long) * data->max_frames); data->times = g_malloc(sizeof(mad_timer_t) * data->max_frames); return true; }
/* * NAME: tag->parse() * DESCRIPTION: parse Xing/LAME tag(s) */ int tag_parse(struct tag *tag, struct mad_stream const *stream) { struct mad_bitptr ptr = stream->anc_ptr; struct mad_bitptr start = ptr; unsigned int bitlen = stream->anc_bitlen; unsigned long magic; int i; if (bitlen < 32) return -1; magic = mad_bit_read(&ptr, 32); bitlen -= 32; if (magic != XING_MAGIC && magic != INFO_MAGIC && magic != LAME_MAGIC) { /* * Due to an unfortunate historical accident, a Xing VBR tag may be * misplaced in a stream with CRC protection. We check for this by * assuming the tag began two octets prior and the high bits of the * following flags field are always zero. */ if (magic != ((XING_MAGIC << 16) & 0xffffffffL) && magic != ((INFO_MAGIC << 16) & 0xffffffffL)) return -1; magic >>= 16; /* backtrack the bit pointer */ ptr = start; mad_bit_skip(&ptr, 16); bitlen += 16; } if ((magic & 0x0000ffffL) == (XING_MAGIC & 0x0000ffffL)) tag->flags |= TAG_VBR; /* Xing tag */ if (magic == LAME_MAGIC) { ptr = start; bitlen += 32; } else if (parse_xing(&tag->xing, &ptr, &bitlen) == 0) tag->flags |= TAG_XING; /* encoder string */ if (bitlen >= 20 * 8) { start = ptr; for (i = 0; i < 20; ++i) { tag->encoder[i] = mad_bit_read(&ptr, 8); if (tag->encoder[i] == 0) break; /* keep only printable ASCII chars */ if (tag->encoder[i] < 0x20 || tag->encoder[i] >= 0x7f) { tag->encoder[i] = 0; break; } } tag->encoder[20] = 0; ptr = start; } /* LAME tag */ if (stream->next_frame - stream->this_frame >= 192 && parse_lame(&tag->lame, &ptr, &bitlen, crc_compute(stream->this_frame, 190, 0x0000)) == 0) { tag->flags |= TAG_LAME; tag->encoder[9] = 0; } else { for (i = 0; i < 20; ++i) { if (tag->encoder[i] == 0) break; /* stop at padding chars */ if (tag->encoder[i] == 0x55) { tag->encoder[i] = 0; break; } } } return 0; }