📄 id3.c
字号:
/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id: id3.c,v 1.84 2004/01/09 00:47:26 linusnielsen Exp $ * * Copyright (C) 2002 by Daniel Stenberg * * 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. * ****************************************************************************//* * Parts of this code has been stolen from the Ample project and was written * by David H鋜deman. It has since been extended and enhanced pretty much by * all sorts of friendly Rockbox people. * */ /* tagResolver and associated code copyright 2003 Thomas Paul Diffenbach */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <stdbool.h>#include <stddef.h>#include <ctype.h>#include "file.h"#include "debug.h"#include "atoi.h"#include "id3.h"#include "mp3data.h"#include "system.h"#define UNSYNC(b0,b1,b2,b3) (((b0 & 0x7F) << (3*7)) | \ ((b1 & 0x7F) << (2*7)) | \ ((b2 & 0x7F) << (1*7)) | \ ((b3 & 0x7F) << (0*7)))#define BYTES2INT(b0,b1,b2,b3) (((b0 & 0xFF) << (3*8)) | \ ((b1 & 0xFF) << (2*8)) | \ ((b2 & 0xFF) << (1*8)) | \ ((b3 & 0xFF) << (0*8)))/* HOW TO ADD ADDITIONAL ID3 VERSION 2 TAGS Code and comments by Thomas Paul Diffenbach To add another ID3v2 Tag, do the following: 1. add a char* named for the tag to struct mp3entry in id3.h, (I (tpd) prefer to use char* rather than ints, even for what seems like numerical values, for cases where a number won't do, e.g., YEAR: "circa 1765", "1790/1977" (composed/performed), "28 Feb 1969" TRACK: "1/12", "1 of 12", GENRE: "Freeform genre name" Text is more flexible, and as the main use of id3 data is to display it, converting it to an int just means reconverting to display it, at a runtime cost.) 2. If any special processing beyond copying the tag value from the Id3 block to the struct mp3entry is rrequired (such as converting to an int), write a function to perform this special processing. This function's prototype must match that of typedef tagPostProcessFunc, that is it must be: int func( struct mp3entry*, char* tag, int bufferpos ) the first argument is a pointer to the current mp3entry structure the second argument is a pointer to the null terminated string value of the tag found the third argument is the offset of the next free byte in the mp3entry's buffer your function should return the corrected offset; if you don't lengthen or shorten the tag string, you can return the third argument unchanged. Unless you have a good reason no to, make the function static. TO JUST COPY THE TAG NO SPECIAL PROCESSING FUNCTION IS NEEDED. 3. add one or more entries to the tagList array, using the format: char* ID3 Tag symbolic name -- see the ID3 specification for these, sizeof() that name minus 1, offsetof( struct mp3entry, variable_name_in_struct_mp3entry ), pointer to your special processing function or NULL if you need no special processing Many ID3 symbolic names come in more than one form. You can add both forms, each referencing the same variable in struct mp3entry. If both forms are present, the last found will be used. 4. Alternately, use the TAG_LIST_ENTRY macro with ID3 tag symbolic name, variable in struct mp3entry, special processing function address 5. Add code to wps-display.c function get_tag to assign a printf-like format specifier for the tag *//* Structure for ID3 Tag extraction information */struct tag_resolver { const char* tag; int tag_length; size_t offset; int (*ppFunc)(struct mp3entry*, char* tag, int bufferpos);};static bool global_ff_found;static int unsynchronize(char* tag, int len, bool *ff_found){ int i; unsigned char c; unsigned char *rp, *wp; wp = rp = tag; rp = (unsigned char *)tag; for(i = 0;i < len;i++) { /* Read the next byte and write it back, but don't increment the write pointer */ c = *rp++; *wp = c; if(*ff_found) { /* Increment the write pointer if it isn't an unsynch pattern */ if(c != 0) wp++; *ff_found = false; } else { if(c == 0xff) *ff_found = true; wp++; } } return (int)wp - (int)tag;}static int unsynchronize_frame(char* tag, int len){ bool ff_found = false; return unsynchronize(tag, len, &ff_found);}static int read_unsynched(int fd, void *buf, int len){ int i; int rc; int remaining = len; char *wp; char *rp; wp = buf; while(remaining) { rp = wp; rc = read(fd, rp, remaining); if(rc < 0) return rc; i = unsynchronize(wp, remaining, &global_ff_found); remaining -= i; wp += i; } return len;};static int skip_unsynched(int fd, int len){ int rc; int remaining = len; int rlen; char buf[32]; while(remaining) { rlen = MIN(sizeof(buf), (unsigned int)remaining); rc = read(fd, buf, rlen); if(rc < 0) return rc; remaining -= unsynchronize(buf, rlen, &global_ff_found); } return len;};/* parse numeric value from string */static int parsetracknum( struct mp3entry* entry, char* tag, int bufferpos ){ entry->tracknum = atoi( tag ); return bufferpos;}/* parse numeric value from string */static int parseyearnum( struct mp3entry* entry, char* tag, int bufferpos ){ entry->year = atoi( tag ); return bufferpos;}/* parse numeric genre from string, version 2.2 and 2.3 */static int parsegenre( struct mp3entry* entry, char* tag, int bufferpos ){ if(entry->id3version >= ID3_VER_2_4) { /* In version 2.4 and up, there are no parentheses, and the genre frame is a list of strings, either numbers or text. */ /* Is it a number? */ if(isdigit(tag[0])) { entry->genre = atoi( tag ); entry->genre_string = 0; return tag - entry->id3v2buf; } else { entry->genre = 0xFF; return bufferpos; } } else { if( tag[0] == '(' && tag[1] != '(' ) { entry->genre = atoi( tag + 1 ); entry->genre_string = 0; return tag - entry->id3v2buf; } else { entry->genre = 0xFF; return bufferpos; } }}static struct tag_resolver taglist[] = { { "TPE1", 4, offsetof(struct mp3entry, artist), NULL }, { "TP1", 3, offsetof(struct mp3entry, artist), NULL }, { "TIT2", 4, offsetof(struct mp3entry, title), NULL }, { "TT2", 3, offsetof(struct mp3entry, title), NULL }, { "TALB", 4, offsetof(struct mp3entry, album), NULL }, { "TAL", 3, offsetof(struct mp3entry, album), NULL }, { "TRK", 3, offsetof(struct mp3entry, track_string), &parsetracknum }, { "TRCK", 4, offsetof(struct mp3entry, track_string), &parsetracknum }, { "TYER", 4, offsetof(struct mp3entry, year_string), &parseyearnum }, { "TYE", 3, offsetof(struct mp3entry, year_string), &parseyearnum }, { "TCON", 4, offsetof(struct mp3entry, genre_string), &parsegenre }, { "TCO", 3, offsetof(struct mp3entry, genre_string), &parsegenre }, { "TCOM", 4, offsetof(struct mp3entry, composer), NULL }};#define TAGLIST_SIZE ((int)(sizeof(taglist) / sizeof(taglist[0])))/* Checks to see if the passed in string is a 16-bit wide Unicode v2 string. If it is, we attempt to convert it to a 8-bit ASCII string (for valid 8-bit ASCII characters). If it's not unicode, we leave it alone. At some point we should fully support unicode strings */static int unicode_munge(char** string, int *len) { int tmp; bool le = false; int i; char *str = *string; char *outstr = *string; bool bom = false; int outlen; if(str[0] > 0x03) { /* Plain old string */ return 0; } /* Type 0x00 is ordinary ISO 8859-1 */ if(str[0] == 0x00) { (*len)--; (*string)++; /* Skip the encoding type byte */ return 0; } /* Unicode with or without BOM */ if(str[0] == 0x01 || str[0] == 0x02) { (*len)--; str++; tmp = BYTES2INT(0, 0, str[0], str[1]); /* Now check if there is a BOM (zero-width non-breaking space, 0xfeff) and if it is in little or big endian format */ if(tmp == 0xfffe) { /* Little endian? */ bom = true; le = true; str += 2; (*len)-=2; } if(tmp == 0xfeff) { /* Big endian? */ bom = true; str += 2; (*len)-=2; } /* If there is no BOM (which is a specification violation), let's try to guess it. If one of the bytes is 0x00, it is probably the most significant one. */ if(!bom) { if(str[1] == 0) le = true; } i = 0; outlen = *len / 2; do { if(le) { if(str[1]) outstr[i++] = '.'; else outstr[i++] = str[0]; } else { if(str[0]) outstr[i++] = '.'; else outstr[i++] = str[1]; } str += 2; } while((str[0] || str[1]) && (i < outlen)); *len = i; outstr[i] = 0; /* Terminate the string */ return 0; } /* If we come here, the string was of an unsupported type */ *len = 1; outstr[0] = 0; return -1;}/* * Sets the title of an MP3 entry based on its ID3v1 tag. * * Arguments: file - the MP3 file to scen for a ID3v1 tag * entry - the entry to set the title in * * Returns: true if a title was found and created, else false */static bool setid3v1title(int fd, struct mp3entry *entry) { unsigned char buffer[128]; static char offsets[] = {3, 33, 63, 93, 125, 127}; int i, j; if (-1 == lseek(fd, -128, SEEK_END)) return false; if (read(fd, buffer, sizeof buffer) != sizeof buffer) return false; if (strncmp(buffer, "TAG", 3)) return false; entry->id3v1len = 128; entry->id3version = ID3_VER_1_0; for (i=0; i < (int)sizeof offsets; i++) { char* ptr = buffer + offsets[i]; if (i<3) { /* kill trailing space in strings */ for (j=29; j && ptr[j]==' '; j--) ptr[j] = 0; } switch(i) { case 0: strncpy(entry->id3v1buf[2], ptr, 30); entry->title = entry->id3v1buf[2]; break; case 1: strncpy(entry->id3v1buf[0], ptr, 30); entry->artist = entry->id3v1buf[0]; break; case 2: strncpy(entry->id3v1buf[1], ptr, 30); entry->album = entry->id3v1buf[1]; break; case 3: ptr[4] = 0; entry->year = atoi(ptr); break; case 4: /* id3v1.1 uses last two bytes of comment field for track number: first must be 0 and second is track num */ if (!ptr[0] && ptr[1]) { entry->tracknum = ptr[1]; entry->id3version = ID3_VER_1_1; } break; case 5: /* genre */ entry->genre = ptr[0]; break; } } return true;}/* * Sets the title of an MP3 entry based on its ID3v2 tag. * * Arguments: file - the MP3 file to scan for a ID3v2 tag * entry - the entry to set the title in * * Returns: true if a title was found and created, else false */static void setid3v2title(int fd, struct mp3entry *entry) { int minframesize; int size; int bufferpos = 0, totframelen, framelen; char header[10];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -