-/*
- * Copyright (C) 2003-2006 Andre Noll <maan@systemlinux.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * 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, USA.
- */
-
-/** \file mp3.c para_server's mp3 audio format handler */
-
-/*
- * This file is based in part on mp3tech.c and mp3tech.h, Copyright (C)
- * 2000-2001 Cedric Tefft <cedric@earthling.net>, which in turn is based
- * in part on
- *
- * * MP3Info 0.5 by Ricardo Cerqueira <rmc@rccn.net>
- * * MP3Stat 0.9 by Ed Sweetman <safemode@voicenet.com> and
- * Johannes Overmann <overmann@iname.com>
- */
-
-#include "server.cmdline.h"
-#include "server.h"
-#include "afs.h"
-#include "afh.h"
-#include "error.h"
-#include "fd.h"
-
-/** \cond some defines and structs which are only used in this file */
-
-/*
- * MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need
- * to see before we decide we are looking at a real MP3 file
- */
-#define MIN_CONSEC_GOOD_FRAMES 3
-
-#define FRAME_HEADER_SIZE 4
-#define MIN_FRAME_SIZE 21
-
-struct mp3header {
- unsigned long sync;
- unsigned int version;
- unsigned int layer;
- unsigned int crc;
- unsigned int bitrate;
- unsigned int freq;
- unsigned int padding;
- unsigned int mode;
- unsigned int copyright;
- unsigned int original;
- unsigned int emphasis;
-};
-
-struct id3tag {
- char title[31];
- char artist[31];
- char album[31];
- char year[5];
- char comment[31];
-};
-
-struct mp3info {
- char *filename;
- FILE *file;
- struct mp3header header;
- int id3_isvalid;
- struct id3tag id3;
- int vbr;
- long unsigned br_average;
- long unsigned seconds;
- int frames;
- int freq;
-};
-
-/** \endcond */
-static int frequencies[3][4] = {
- {22050,24000,16000,50000}, /* MPEG 2.0 */
- {44100,48000,32000,50000}, /* MPEG 1.0 */
- {11025,12000,8000,50000} /* MPEG 2.5 */
-};
-
-static int mp3info_bitrate[2][3][14] = {
-{ /* MPEG 2.0 */
- {32,48,56,64,80,96,112,128,144,160,176,192,224,256}, /* layer 1 */
- {8,16,24,32,40,48,56,64,80,96,112,128,144,160}, /* layer 2 */
- {8,16,24,32,40,48,56,64,80,96,112,128,144,160} /* layer 3 */
-},
-
-{ /* MPEG 1.0 */
- {32,64,96,128,160,192,224,256,288,320,352,384,416,448}, /* layer 1 */
- {32,48,56,64,80,96,112,128,160,192,224,256,320,384}, /* layer 2 */
- {32,40,48,56,64,80,96,112,128,160,192,224,256,320} /* layer 3 */
-}
-};
-
-static int frame_size_index[] = {24000, 72000, 72000};
-static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
-
-static struct mp3info mp3;
-static char mp3buf[8192];
-static int chunk_size;
-static struct audio_format_handler *af;
-
-static int header_frequency(struct mp3header *h)
-{
- if (h->version > 2 || h->freq > 3)
- return -E_HEADER_FREQ;
- return frequencies[h->version][h->freq];
-}
-
-static const char *header_mode(struct mp3header *h)
-{
- if (h->mode > 4)
- h->mode = 4; /* invalid */
- return mode_text[h->mode];
-}
-static int header_bitrate(struct mp3header *h)
-{
- if (h->layer > 3 || h->bitrate > 14)
- return -E_HEADER_BITRATE;
- return mp3info_bitrate[h->version & 1][3 - h->layer][h->bitrate - 1];
-}
-
-static int frame_length(struct mp3header *header)
-{
- int hb, hf = header_frequency(header);
-
- if (hf < 0)
- return hf;
- hb = header_bitrate(header);
- if (hb < 0)
- return hb;
- if (header->sync != 0xFFE || header->layer > 3)
- return -E_FRAME;
- return frame_size_index[3 - header->layer] *
- ((header->version & 1) + 1) * hb / hf
- + header->padding;
-}
-
-static void write_info_str(char *info_str)
-{
- int v = mp3.id3_isvalid;
-
- snprintf(info_str, MMD_INFO_SIZE,
- "audio_file_info1:%d x %lums, %lu kbit/s (%cbr) %i KHz %s\n"
- "audio_file_info2:%s, by %s\n"
- "audio_file_info3:A: %s, Y: %s, C: %s\n",
- mp3.frames,
- tv2ms(&af->chunk_tv),
- mp3.br_average,
- mp3.vbr? 'v' : 'c',
- mp3.freq / 1000,
- header_mode(&mp3.header),
- v && *mp3.id3.title? mp3.id3.title : "(title tag not set)",
- v && *mp3.id3.artist? mp3.id3.artist : "(artist tag not set)",
- v && *mp3.id3.album? mp3.id3.album : "(album tag not set)",
- v && *mp3.id3.year? mp3.id3.year : "????",
- v && *mp3.id3.comment? mp3.id3.comment : "(comment tag not set)"
- );
-}
-
-/*
- * Remove trailing whitespace from the end of a string
- */
-static char *unpad(char *string)
-{
- char *pos = string + strlen(string) - 1;
- while (isspace(pos[0]))
- (pos--)[0] = 0;
- return string;
-}
-
-static int compare_headers(struct mp3header *h1,struct mp3header *h2)
-{
- if ((*(uint*)h1) == (*(uint*)h2))
- return 1;
- if ((h1->version == h2->version) &&
- (h1->layer == h2->layer) &&
- (h1->crc == h2->crc) &&
- (h1->freq == h2->freq) &&
- (h1->mode == h2->mode) &&
- (h1->copyright == h2->copyright) &&
- (h1->original == h2->original) &&
- (h1->emphasis == h2->emphasis))
- return 1;
- else
- return 0;
-}
-
-/**
- * get next MP3 frame header.
- *
- * \param stream to read the header from
- * \param header structure that gets filled in by get_header()
- *
- * \return On success, the header frame length is returned. A return value of
- * zero means that we did not retrieve a valid frame header, and a negative
- * return value indicates an error.
- */
-static int get_header(FILE *file, struct mp3header *header)
-{
- unsigned char buffer[FRAME_HEADER_SIZE];
- int fl, ret;
-
- if (!file || !header)
- return -E_MP3_NO_FILE;
- ret = para_fread(buffer, FRAME_HEADER_SIZE, 1, file);
- if (ret < FRAME_HEADER_SIZE) {
- header->sync = 0;
- return ret < 0? ret : 0;
- }
- header->layer = (buffer[1] >> 1) & 3;
- header->sync = (((int)buffer[0]<<4) | ((int)(buffer[1]&0xE0)>>4));
- if (buffer[1] & 0x10)
- header->version = (buffer[1] >> 3) & 1;
- else
- header->version = 2;
- if ((header->sync != 0xFFE) || (header->layer != 1)) {
- header->sync = 0;
-// PARA_DEBUG_LOG("%s: header not found\n", __func__);
- return 0;
- }
- header->crc = buffer[1] & 1;
- header->bitrate = (buffer[2] >> 4) & 0x0F;
-// PARA_DEBUG_LOG("%s: found header, bitrate: %u\n", __func__,
-// header->bitrate);
- header->freq = (buffer[2] >> 2) & 0x3;
- header->padding = (buffer[2] >>1) & 0x1;
- header->mode = (buffer[3] >> 6) & 0x3;
- fl = frame_length(header);
- return (fl >= MIN_FRAME_SIZE)? fl : -E_FRAME_LENGTH;
-}
-
-/**
- * find the next mp3 header
- *
- * \return On success, the length of the next frame header. If the end of the
- * file was reached, the function returns zero. On errors, a negative value is
- * returned.
- *
- */
-static int mp3_seek_next_header(void)
-{
- int k, l = 0, c, first_len;
- struct mp3header h, h2;
- long valid_start = 0;
-
- while (1) {
- while ((c = fgetc(mp3.file)) != 255 && (c != EOF))
- ; /* nothing */
- if (c != 255)
- return 0;
- ungetc(c, mp3.file);
- valid_start = ftell(mp3.file);
- first_len = get_header(mp3.file, &h);
- if (first_len <= 0)
- continue;
- if (fseek(mp3.file, first_len - FRAME_HEADER_SIZE, SEEK_CUR) < 0)
- return -E_FSEEK;
- for (k = 1; k < MIN_CONSEC_GOOD_FRAMES; k++) {
- if ((l = get_header(mp3.file, &h2)) <= 0)
- break;
- if (!compare_headers(&h, &h2))
- break;
- fseek(mp3.file, l - FRAME_HEADER_SIZE, SEEK_CUR);
- }
- if (k == MIN_CONSEC_GOOD_FRAMES) {
- fseek(mp3.file, valid_start, SEEK_SET);
- memcpy(&(mp3.header), &h2, sizeof(struct mp3header));
- return first_len;
- }
- }
-}
-
-static int mp3_get_id3(void)
-{
- char fbuf[4];
-
- mp3.id3_isvalid = 0;
- mp3.id3.title[0] = '\0';
- mp3.id3.artist[0] = '\0';
- mp3.id3.album[0] = '\0';
- mp3.id3.comment[0] = '\0';
- mp3.id3.year[0] = '\0';
- if (fseek(mp3.file, -128, SEEK_END))
- return -E_FSEEK;
- if (para_fread(fbuf, 1, 3, mp3.file) < 0)
- return -E_FREAD;
- fbuf[3] = '\0';
- if (strcmp("TAG", fbuf)) {
- PARA_INFO_LOG("%s", "no id3 tag\n");
- return 0;
- }
- if (fseek(mp3.file, -125, SEEK_END) < 0)
- return -E_FSEEK;
- if (para_fread(mp3.id3.title, 1, 30, mp3.file) != 30)
- return -E_FREAD;
- mp3.id3.title[30] = '\0';
- if (para_fread(mp3.id3.artist, 1, 30, mp3.file) != 30)
- return -E_FREAD;
- mp3.id3.artist[30] = '\0';
- if (para_fread(mp3.id3.album, 1, 30, mp3.file) != 30)
- return -E_FREAD;
- mp3.id3.album[30] = '\0';
- if (para_fread(mp3.id3.year, 1, 4, mp3.file) != 4)
- return -E_FREAD;
- mp3.id3.year[4] = '\0';
- if (para_fread(mp3.id3.comment, 1, 30, mp3.file) != 30)
- return -E_FREAD;
- mp3.id3.comment[30] = '\0';
- mp3.id3_isvalid = 1;
- unpad(mp3.id3.title);
- unpad(mp3.id3.artist);
- unpad(mp3.id3.album);
- unpad(mp3.id3.year);
- unpad(mp3.id3.comment);
- return 1;
-}
-
-static int find_valid_start(void)
-{
- int frame_len;
-
- if (!mp3.file)
- return -E_MP3_NO_FILE;
- frame_len = get_header(mp3.file, &mp3.header);
- if (frame_len < 0)
- return frame_len;
- if (!frame_len) {
- frame_len = mp3_seek_next_header();
- if (frame_len <= 0)
- return frame_len;
- } else
- if (fseek(mp3.file, -FRAME_HEADER_SIZE, SEEK_CUR) < 0)
- return -E_FSEEK;
- if (frame_len <= 1) /* FRAME_HEADER_SIZE? */
- return -E_FRAME_LENGTH;
- return frame_len;
-}
-
-static int mp3_read_info(void)
-{
- long fl_avg = 0, freq_avg = 0, br_avg = 0, fcount = 0;
- int ret, len = 0, old_br = -1;
- struct timeval total_time = {0, 0};
-
- ret = mp3_get_id3();
- if (ret < 0)
- return ret;
- rewind(mp3.file);
- mp3.vbr = 0;
- mp3.freq = 0;
- while (1) {
- int freq, br, fl;
- struct timeval tmp, cct; /* current chunk time */
- if (len > 0)
- if (fseek(mp3.file, len, SEEK_CUR) < 0)
- return -E_FSEEK;
- len = find_valid_start();
- if (len <= 0)
- break;
- freq = header_frequency(&mp3.header);
- br = header_bitrate(&mp3.header);
- fl = frame_length(&mp3.header);
- if (freq < 0 || br < 0 || fl < 0)
- continue;
- tmp.tv_sec = fl;
- tmp.tv_usec = 0;
- tv_divide(br * 125, &tmp, &cct);
- tv_add(&cct, &total_time, &tmp);
- total_time = tmp;
-// PARA_DEBUG_LOG("%s: br: %d, freq: %d, fl: %d, cct: %lu\n", __func__, br, freq, fl, cct.tv_usec);
- fcount++;
- if (fcount == 1) {
- freq_avg = freq;
- br_avg = br;
- old_br = br;
- fl_avg = fl;
- continue;
- }
- freq_avg += (freq - freq_avg) / (fcount + 1);
- fl_avg += (fl - fl_avg) / (fcount + 1);
- br_avg += (br - br_avg) / (fcount + 1);
- if (old_br != br)
- mp3.vbr = 1;
- old_br = br;
- }
- if (!fcount || !freq_avg || !br_avg)
- return -E_MP3_INFO;
- mp3.br_average = br_avg;
- mp3.freq = freq_avg;
- mp3.frames = fcount;
- mp3.seconds = (tv2ms(&total_time) + 500) / 1000;
- tv_divide(fcount, &total_time, &af->chunk_tv);
- rewind(mp3.file);
- PARA_DEBUG_LOG("chunk_time: %lums\n", tv2ms(&af->chunk_tv));
- tv_scale(30, &af->chunk_tv, &af->eof_tv);
- PARA_DEBUG_LOG("eof timeout: %lu\n", tv2ms(&af->eof_tv));
- return 1;
-}
-
-/*
- * Read mp3 information from audio file
- */
-static int mp3_get_file_info(FILE *audio_file, char *info_str,
- long unsigned *frames, int *seconds)
-{
- int ret;
-
- if (!audio_file)
- return -E_MP3_NO_FILE;
- mp3.file = audio_file;
- ret = mp3_read_info();
- if (ret < 0) {
- mp3.file = NULL;
- return ret;
- }
- write_info_str(info_str);
- *frames = mp3.frames;
- *seconds = mp3.seconds;
- if (*seconds < 2 || !*frames)
- return -E_MP3_INFO;
- return 1;
-}
-
-static int mp3_reposition_stream(long unsigned new_frame)
-{
- int count = 0, len;
-
- PARA_DEBUG_LOG("jmp to frame %lu/%i\n", new_frame, mp3.frames);
- rewind(mp3.file);
- while (count < new_frame && (len = find_valid_start()) > 0) {
-// PARA_DEBUG_LOG("%s: jmp to frame %d\n", __func__, count);
- if (fseek(mp3.file, len, SEEK_CUR) < 0)
- return -E_FSEEK;
- count++;
- }
- if (count != new_frame) {
- rewind(mp3.file);
- return -E_MP3_REPOS;
- }
- return 1;
-}
-
-static int mp3_read_next_chunk(void)
-{
- int len = find_valid_start();
-
- if (len <= 0) {
- if (len < 0)
- PARA_ERROR_LOG("invalid frame len (%d)\n", len);
- return len;
- }
- chunk_size = para_fread(mp3buf, len, 1, mp3.file);
- if (len != chunk_size)
- PARA_DEBUG_LOG("short read (%d/%d)\n", chunk_size, len);
- return chunk_size;
-}
-
-static char *mp3_read_chunk(__a_unused long unsigned chunk_num, ssize_t *len)
-{
- *len = mp3_read_next_chunk();
- if (*len <= 0)
- return NULL;
- return mp3buf;
-}
-
-static void mp3_close_audio_file(void)
-{
- if (!mp3.file)
- return;
- fclose(mp3.file);
- mp3.file = NULL;
-}
-
-static const char* mp3_suffixes[] = {"mp3", NULL};
-void mp3_init(struct audio_format_handler *p)
-{
- af = p;
- af->get_file_info = mp3_get_file_info;
- af->reposition_stream = mp3_reposition_stream;
- af->read_chunk = mp3_read_chunk;
- af->close_audio_file = mp3_close_audio_file;
- af->get_header_info = NULL;
- /* eof_tv gets overwritten in mp3_get_file_info() */
- af->eof_tv.tv_sec = 0;
- af->eof_tv.tv_usec = 100 * 1000;
- af->suffixes = mp3_suffixes;
-}