Now VBRFix correctly handles all types of files, with or without ID3 tags

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@3514 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Linus Nielsen Feltzing 2003-04-03 07:20:57 +00:00
parent 11edfa8d96
commit 6edea74ea6

View file

@ -117,16 +117,13 @@ static void xingupdate(int percent)
extern unsigned char mp3buf[]; extern unsigned char mp3buf[];
extern unsigned char mp3end[]; extern unsigned char mp3end[];
static int insert_space_in_file(char *fname, int num_bytes) static int insert_space_in_file(char *fname, int fpos, int num_bytes)
{ {
int readlen; int readlen;
int rc; int rc;
int orig_fd, fd; int orig_fd, fd;
char tmpname[MAX_PATH]; char tmpname[MAX_PATH];
/* Use the mp3 buffer to write 0's in the file */
memset(mp3buf, 0, num_bytes);
snprintf(tmpname, MAX_PATH, "%s.tmp", fname); snprintf(tmpname, MAX_PATH, "%s.tmp", fname);
orig_fd = open(fname, O_RDONLY); orig_fd = open(fname, O_RDONLY);
@ -142,12 +139,35 @@ static int insert_space_in_file(char *fname, int num_bytes)
return 10*fd - 2; return 10*fd - 2;
} }
/* First, copy the initial portion (the ID3 tag) */
if(fpos)
{
readlen = read(orig_fd, mp3buf, fpos);
if(readlen < 0)
{
close(fd);
close(orig_fd);
return 10*readlen - 3;
}
rc = write(fd, mp3buf, readlen);
if(rc < 0)
{
close(fd);
close(orig_fd);
return 10*rc - 4;
}
}
/* Now insert some 0's in the file */
memset(mp3buf, 0, num_bytes);
rc = write(fd, mp3buf, num_bytes); rc = write(fd, mp3buf, num_bytes);
if(rc < 0) if(rc < 0)
{ {
close(orig_fd); close(orig_fd);
close(fd); close(fd);
return 10*rc - 3; return 10*rc - 5;
} }
rc = lseek(orig_fd, 0, SEEK_SET); rc = lseek(orig_fd, 0, SEEK_SET);
@ -155,7 +175,7 @@ static int insert_space_in_file(char *fname, int num_bytes)
{ {
close(orig_fd); close(orig_fd);
close(fd); close(fd);
return 10*rc - 4; return 10*rc - 6;
} }
/* Copy the file */ /* Copy the file */
@ -166,7 +186,7 @@ static int insert_space_in_file(char *fname, int num_bytes)
{ {
close(fd); close(fd);
close(orig_fd); close(orig_fd);
return 10*readlen - 5; return 10*readlen - 7;
} }
rc = write(fd, mp3buf, readlen); rc = write(fd, mp3buf, readlen);
@ -174,7 +194,7 @@ static int insert_space_in_file(char *fname, int num_bytes)
{ {
close(fd); close(fd);
close(orig_fd); close(orig_fd);
return 10*rc - 6; return 10*rc - 8;
} }
} while(readlen > 0); } while(readlen > 0);
@ -185,14 +205,14 @@ static int insert_space_in_file(char *fname, int num_bytes)
rc = remove(fname); rc = remove(fname);
if(rc < 0) if(rc < 0)
{ {
return 10*rc - 7; return 10*rc - 9;
} }
/* Replace the old file with the new */ /* Replace the old file with the new */
rc = rename(tmpname, fname); rc = rename(tmpname, fname);
if(rc < 0) if(rc < 0)
{ {
return 10*rc - 8; return 10*rc - 9;
} }
return 0; return 0;
@ -207,6 +227,7 @@ static bool vbr_fix(void)
int flen; int flen;
int num_frames; int num_frames;
int fpos; int fpos;
int numbytes;
if(mpeg_status()) if(mpeg_status())
{ {
@ -222,11 +243,17 @@ static bool vbr_fix(void)
rc = mp3info(&entry, selected_file); rc = mp3info(&entry, selected_file);
if(rc < 0) if(rc < 0)
return rc * 10 - 1; {
splash(HZ*2, 0, true, "File error: %d", rc);
return true;
}
fd = open(selected_file, O_RDWR); fd = open(selected_file, O_RDWR);
if(fd < 0) if(fd < 0)
return fd * 10 - 2; {
splash(HZ*2, 0, true, "File error: %d", fd);
return true;
}
flen = lseek(fd, 0, SEEK_END); flen = lseek(fd, 0, SEEK_END);
@ -259,31 +286,33 @@ static bool vbr_fix(void)
} }
else else
{ {
/* If not, insert some space, but only if there is no ID3 /* If not, insert some space. If there is an ID3 tag in the
tag in the file */ file we only insert just enough to squeeze the Xing header
in. If not, we insert 4K. */
close(fd); close(fd);
if(entry.first_frame_offset == 0) if(entry.first_frame_offset)
{ numbytes = 417;
rc = insert_space_in_file(selected_file, 4096); else
numbytes = 4096;
rc = insert_space_in_file(selected_file,
entry.first_frame_offset, numbytes);
if(rc < 0) if(rc < 0)
{ {
splash(HZ*2, 0, true, "File error: %d", rc); splash(HZ*2, 0, true, "File error: %d", rc);
return rc * 10 - 3; return true;
} }
/* Reopen the file */ /* Reopen the file */
fd = open(selected_file, O_RDWR); fd = open(selected_file, O_RDWR);
if(fd < 0) if(fd < 0)
return fd * 10 - 4;
fpos = 4096-417;
}
else
{ {
splash(HZ*2, 0, true, splash(HZ*2, 0, true, "File reopen error: %d", fd);
"There is no room for a Xing header"); return true;
return -3;
} }
fpos = numbytes - 417;
} }
} }