2005-06-14 22:27:57 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2005-09-22 16:58:03 +00:00
|
|
|
#include <inttypes.h>
|
2005-06-14 22:27:57 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
#include "system.h"
|
2007-06-16 18:19:51 +00:00
|
|
|
#include "playback.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "logf.h"
|
2007-02-14 14:40:24 +00:00
|
|
|
#include "cuesheet.h"
|
2005-09-22 16:58:03 +00:00
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2005-06-14 23:12:34 +00:00
|
|
|
|
2007-11-28 04:58:16 +00:00
|
|
|
/* For trailing tag stripping */
|
|
|
|
#include "buffering.h"
|
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
#include "metadata/metadata_common.h"
|
|
|
|
#include "metadata/metadata_parsers.h"
|
2005-06-27 00:12:40 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
static const unsigned short a52_bitrates[] =
|
|
|
|
{
|
|
|
|
32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
|
|
|
|
192, 224, 256, 320, 384, 448, 512, 576, 640
|
|
|
|
};
|
2005-07-05 08:43:36 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
/* Only store frame sizes for 44.1KHz - others are simply multiples
|
|
|
|
of the bitrate */
|
|
|
|
static const unsigned short a52_441framesizes[] =
|
|
|
|
{
|
|
|
|
69 * 2, 70 * 2, 87 * 2, 88 * 2, 104 * 2, 105 * 2, 121 * 2,
|
|
|
|
122 * 2, 139 * 2, 140 * 2, 174 * 2, 175 * 2, 208 * 2, 209 * 2,
|
|
|
|
243 * 2, 244 * 2, 278 * 2, 279 * 2, 348 * 2, 349 * 2, 417 * 2,
|
|
|
|
418 * 2, 487 * 2, 488 * 2, 557 * 2, 558 * 2, 696 * 2, 697 * 2,
|
|
|
|
835 * 2, 836 * 2, 975 * 2, 976 * 2, 1114 * 2, 1115 * 2, 1253 * 2,
|
|
|
|
1254 * 2, 1393 * 2, 1394 * 2
|
|
|
|
};
|
2005-07-05 08:43:36 +00:00
|
|
|
|
2006-10-25 16:57:53 +00:00
|
|
|
#endif /* CONFIG_CODEC == SWCODEC */
|
|
|
|
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
/* Simple file type probing by looking at the filename extension. */
|
2006-03-26 11:33:42 +00:00
|
|
|
unsigned int probe_file_format(const char *filename)
|
2005-06-19 20:27:46 +00:00
|
|
|
{
|
2005-09-22 16:58:03 +00:00
|
|
|
char *suffix;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
suffix = strrchr(filename, '.');
|
2005-06-19 20:27:46 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
if (suffix == NULL)
|
|
|
|
{
|
|
|
|
return AFMT_UNKNOWN;
|
2005-06-19 20:27:46 +00:00
|
|
|
}
|
2005-09-22 16:58:03 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* skip '.' */
|
|
|
|
suffix++;
|
|
|
|
|
|
|
|
for (i = 1; i < AFMT_NUM_CODECS; i++)
|
|
|
|
{
|
|
|
|
/* search extension list for type */
|
|
|
|
const char *ext = audio_formats[i].ext_list;
|
2005-06-19 20:27:46 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
do
|
2005-09-22 16:58:03 +00:00
|
|
|
{
|
2006-12-29 14:59:10 +00:00
|
|
|
if (strcasecmp(suffix, ext) == 0)
|
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext += strlen(ext) + 1;
|
2005-09-22 16:58:03 +00:00
|
|
|
}
|
2006-11-06 18:07:30 +00:00
|
|
|
while (*ext != '\0');
|
2005-09-22 16:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return AFMT_UNKNOWN;
|
2005-06-19 20:27:46 +00:00
|
|
|
}
|
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
/* Get metadata for track - return false if parsing showed problems with the
|
|
|
|
* file that would prevent playback.
|
2005-06-19 20:27:46 +00:00
|
|
|
*/
|
2007-09-19 10:40:55 +00:00
|
|
|
bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
|
2005-06-19 20:27:46 +00:00
|
|
|
{
|
2006-03-26 11:33:42 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2005-09-22 16:58:03 +00:00
|
|
|
unsigned char* buf;
|
|
|
|
unsigned long totalsamples;
|
|
|
|
int i;
|
2006-03-26 11:33:42 +00:00
|
|
|
#endif
|
2005-12-01 20:39:19 +00:00
|
|
|
|
2007-10-05 16:02:35 +00:00
|
|
|
/* Clear the mp3entry to avoid having bogus pointers appear */
|
|
|
|
memset(id3, 0, sizeof(struct mp3entry));
|
|
|
|
|
2005-12-01 20:39:19 +00:00
|
|
|
/* Take our best guess at the codec type based on file extension */
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->codectype = probe_file_format(trackname);
|
2005-12-01 20:39:19 +00:00
|
|
|
|
|
|
|
/* Load codec specific track tag information and confirm the codec type. */
|
2007-08-14 11:56:13 +00:00
|
|
|
switch (id3->codectype)
|
2005-09-22 16:58:03 +00:00
|
|
|
{
|
|
|
|
case AFMT_MPA_L1:
|
|
|
|
case AFMT_MPA_L2:
|
|
|
|
case AFMT_MPA_L3:
|
2007-09-19 10:40:55 +00:00
|
|
|
if (!get_mp3_metadata(fd, id3, trackname))
|
2005-09-22 16:58:03 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-06-19 20:27:46 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
2005-06-19 20:27:46 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2005-09-22 16:58:03 +00:00
|
|
|
case AFMT_FLAC:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_flac_metadata(fd, id3))
|
2005-09-22 16:58:03 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-06-19 20:27:46 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
|
|
|
|
2007-07-03 09:25:36 +00:00
|
|
|
case AFMT_WMA:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_asf_metadata(fd, id3))
|
2007-07-03 09:25:36 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-06-05 16:58:29 +00:00
|
|
|
case AFMT_APE:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_monkeys_metadata(fd, id3))
|
2007-06-05 16:58:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2007-08-14 11:56:13 +00:00
|
|
|
read_ape_tags(fd, id3);
|
2007-06-05 16:58:29 +00:00
|
|
|
break;
|
|
|
|
|
2005-10-10 18:18:24 +00:00
|
|
|
case AFMT_MPC:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_musepack_metadata(fd, id3))
|
2005-11-04 21:34:03 +00:00
|
|
|
return false;
|
2007-08-14 11:56:13 +00:00
|
|
|
read_ape_tags(fd, id3);
|
2005-10-10 18:18:24 +00:00
|
|
|
break;
|
2005-11-04 21:34:03 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
case AFMT_OGG_VORBIS:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_vorbis_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
|
2007-02-09 10:06:53 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AFMT_SPEEX:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_speex_metadata(fd, id3))
|
2005-09-22 16:58:03 +00:00
|
|
|
{
|
|
|
|
return false;
|
2005-06-19 20:27:46 +00:00
|
|
|
}
|
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
2005-06-19 20:27:46 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
case AFMT_PCM_WAV:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_wave_metadata(fd, id3))
|
2005-09-22 16:58:03 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-05 08:43:36 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AFMT_WAVPACK:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_wavpack_metadata(fd, id3))
|
2005-07-05 08:43:36 +00:00
|
|
|
return false;
|
|
|
|
|
2007-08-14 11:56:13 +00:00
|
|
|
read_ape_tags(fd, id3); /* use any apetag info we find */
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AFMT_A52:
|
|
|
|
/* Use the trackname part of the id3 structure as a temporary buffer */
|
2007-08-14 11:56:13 +00:00
|
|
|
buf = (unsigned char *)id3->path;
|
2005-09-22 16:58:03 +00:00
|
|
|
|
|
|
|
if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 5) < 5))
|
|
|
|
{
|
|
|
|
return false;
|
2005-07-05 08:43:36 +00:00
|
|
|
}
|
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
if ((buf[0] != 0x0b) || (buf[1] != 0x77))
|
|
|
|
{
|
|
|
|
logf("%s is not an A52/AC3 file\n",trackname);
|
2005-07-05 08:43:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
i = buf[4] & 0x3e;
|
|
|
|
|
|
|
|
if (i > 36)
|
|
|
|
{
|
|
|
|
logf("A52: Invalid frmsizecod: %d\n",i);
|
|
|
|
return false;
|
2005-07-05 08:43:36 +00:00
|
|
|
}
|
2005-09-22 16:58:03 +00:00
|
|
|
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->bitrate = a52_bitrates[i >> 1];
|
|
|
|
id3->vbr = false;
|
|
|
|
id3->filesize = filesize(fd);
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
switch (buf[4] & 0xc0)
|
|
|
|
{
|
|
|
|
case 0x00:
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->frequency = 48000;
|
|
|
|
id3->bytesperframe=id3->bitrate * 2 * 2;
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x40:
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->frequency = 44100;
|
|
|
|
id3->bytesperframe = a52_441framesizes[i];
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x80:
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->frequency = 32000;
|
|
|
|
id3->bytesperframe = id3->bitrate * 3 * 2;
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
logf("A52: Invalid samplerate code: 0x%02x\n", buf[4] & 0xc0);
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One A52 frame contains 6 blocks, each containing 256 samples */
|
2007-08-14 11:56:13 +00:00
|
|
|
totalsamples = id3->filesize / id3->bytesperframe * 6 * 256;
|
|
|
|
id3->length = totalsamples / id3->frequency * 1000;
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
|
|
|
|
2005-09-22 21:55:37 +00:00
|
|
|
case AFMT_ALAC:
|
2005-10-31 20:56:29 +00:00
|
|
|
case AFMT_AAC:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_mp4_metadata(fd, id3))
|
2005-09-22 21:55:37 +00:00
|
|
|
{
|
2005-12-01 20:39:19 +00:00
|
|
|
return false;
|
2005-09-22 21:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2005-11-13 11:04:02 +00:00
|
|
|
case AFMT_SHN:
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->vbr = true;
|
|
|
|
id3->filesize = filesize(fd);
|
|
|
|
if (!skip_id3v2(fd, id3))
|
2005-11-13 11:04:02 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* TODO: read the id3v2 header if it exists */
|
|
|
|
break;
|
2006-07-18 18:33:12 +00:00
|
|
|
|
|
|
|
case AFMT_SID:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_sid_metadata(fd, id3))
|
2006-07-18 18:33:12 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2007-02-14 03:34:55 +00:00
|
|
|
case AFMT_SPC:
|
2007-08-14 11:56:13 +00:00
|
|
|
if(!get_spc_metadata(fd, id3))
|
2007-03-03 06:08:28 +00:00
|
|
|
{
|
|
|
|
DEBUGF("get_spc_metadata error\n");
|
|
|
|
}
|
|
|
|
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->filesize = filesize(fd);
|
|
|
|
id3->genre_string = id3_get_num_genre(36);
|
2007-02-14 03:49:09 +00:00
|
|
|
break;
|
2006-09-25 16:13:05 +00:00
|
|
|
case AFMT_ADX:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_adx_metadata(fd, id3))
|
2006-09-25 16:13:05 +00:00
|
|
|
{
|
|
|
|
DEBUGF("get_adx_metadata error\n");
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-03 06:08:28 +00:00
|
|
|
|
2006-09-25 16:13:05 +00:00
|
|
|
break;
|
2007-01-25 18:06:17 +00:00
|
|
|
case AFMT_NSF:
|
2007-08-14 11:56:13 +00:00
|
|
|
buf = (unsigned char *)id3->path;
|
2007-01-25 18:06:17 +00:00
|
|
|
if ((lseek(fd, 0, SEEK_SET) < 0) || ((read(fd, buf, 8)) < 8))
|
|
|
|
{
|
|
|
|
DEBUGF("lseek or read failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->vbr = false;
|
|
|
|
id3->filesize = filesize(fd);
|
2007-01-25 18:06:17 +00:00
|
|
|
if (memcmp(buf,"NESM",4) && memcmp(buf,"NSFE",4)) return false;
|
2007-03-03 06:08:28 +00:00
|
|
|
break;
|
2005-11-13 11:04:02 +00:00
|
|
|
|
2006-02-01 16:42:02 +00:00
|
|
|
case AFMT_AIFF:
|
2007-08-14 11:56:13 +00:00
|
|
|
if (!get_aiff_metadata(fd, id3))
|
2006-02-01 16:42:02 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2006-10-25 16:57:53 +00:00
|
|
|
#endif /* CONFIG_CODEC == SWCODEC */
|
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
default:
|
2005-12-01 20:39:19 +00:00
|
|
|
/* If we don't know how to read the metadata, assume we can't play
|
|
|
|
the file */
|
|
|
|
return false;
|
2005-09-22 16:58:03 +00:00
|
|
|
break;
|
2005-07-28 20:21:54 +00:00
|
|
|
}
|
|
|
|
|
2005-12-01 20:39:19 +00:00
|
|
|
/* We have successfully read the metadata from the file */
|
|
|
|
|
2007-03-04 16:47:33 +00:00
|
|
|
#ifndef __PCTOOL__
|
2007-05-28 23:18:31 +00:00
|
|
|
if (cuesheet_is_enabled() && look_for_cuesheet_file(trackname, NULL))
|
2007-02-14 14:40:24 +00:00
|
|
|
{
|
2007-08-14 11:56:13 +00:00
|
|
|
id3->cuesheet_type = 1;
|
2007-02-14 14:40:24 +00:00
|
|
|
}
|
2007-03-04 16:47:33 +00:00
|
|
|
#endif
|
|
|
|
|
2005-09-22 16:58:03 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2007-08-14 11:56:13 +00:00
|
|
|
strncpy(id3->path, trackname, sizeof(id3->path));
|
2005-09-22 16:58:03 +00:00
|
|
|
|
2005-07-05 08:43:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2007-11-28 05:13:05 +00:00
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2007-11-28 04:58:16 +00:00
|
|
|
void strip_tags(int handle_id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static const unsigned char tag[] = "TAG";
|
|
|
|
static const unsigned char apetag[] = "APETAGEX";
|
|
|
|
size_t len, version;
|
|
|
|
unsigned char *tail;
|
|
|
|
|
|
|
|
if (bufgettail(handle_id, 128, (void **)&tail) != 128)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(i = 0;i < 3;i++)
|
|
|
|
if(tail[i] != tag[i])
|
|
|
|
goto strip_ape_tag;
|
|
|
|
|
|
|
|
/* Skip id3v1 tag */
|
|
|
|
logf("Cutting off ID3v1 tag");
|
|
|
|
bufcuttail(handle_id, 128);
|
|
|
|
|
|
|
|
strip_ape_tag:
|
|
|
|
/* Check for APE tag (look for the APE tag footer) */
|
|
|
|
|
|
|
|
if (bufgettail(handle_id, 32, (void **)&tail) != 32)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(i = 0;i < 8;i++)
|
|
|
|
if(tail[i] != apetag[i])
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Read the version and length from the footer */
|
|
|
|
version = tail[8] | (tail[9] << 8) | (tail[10] << 16) | (tail[11] << 24);
|
|
|
|
len = tail[12] | (tail[13] << 8) | (tail[14] << 16) | (tail[15] << 24);
|
|
|
|
if (version == 2000)
|
|
|
|
len += 32; /* APEv2 has a 32 byte header */
|
|
|
|
|
|
|
|
/* Skip APE tag */
|
|
|
|
logf("Cutting off APE tag (%ldB)", len);
|
|
|
|
bufcuttail(handle_id, len);
|
|
|
|
}
|
2007-11-28 05:13:05 +00:00
|
|
|
#endif /* CONFIG_CODEC == SWCODEC */
|