summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMagnus Holmgren <magnushol@gmail.com>2007-06-16 13:00:52 +0000
committerMagnus Holmgren <magnushol@gmail.com>2007-06-16 13:00:52 +0000
commitc3206a455a455fadb282d09f9af482c66b6bdf8e (patch)
treeebdddf413b84612d55291c8d64ee233f725dc316
parent93af4feae9f7890846934f4293d1291daa2901c7 (diff)
downloadrockbox-c3206a455a455fadb282d09f9af482c66b6bdf8e.tar.gz
rockbox-c3206a455a455fadb282d09f9af482c66b6bdf8e.zip
AAC: Add support for iTunes-style gapless playback.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13636 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/codecs/aac.c54
-rw-r--r--apps/metadata.c50
2 files changed, 102 insertions, 2 deletions
diff --git a/apps/codecs/aac.c b/apps/codecs/aac.c
index 11a9d2888d..d4f051c09c 100644
--- a/apps/codecs/aac.c
+++ b/apps/codecs/aac.c
@@ -42,6 +42,8 @@ enum codec_status codec_main(void)
uint32_t sample_duration;
uint32_t sample_byte_size;
int file_offset;
+ int framelength;
+ int lead_trim = 0;
unsigned int i;
unsigned char* buffer;
static NeAACDecFrameInfo frame_info;
@@ -117,6 +119,11 @@ next_track:
sound_samples_done = 0;
}
}
+
+ if (i == 0)
+ {
+ lead_trim = ci->id3->lead_trim;
+ }
/* The main decoding loop */
while (i < demux_res.num_sample_byte_sizes) {
@@ -133,6 +140,11 @@ next_track:
&sound_samples_done, (int*) &i)) {
elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
ci->set_elapsed(elapsed_time);
+
+ if (i == 0)
+ {
+ lead_trim = ci->id3->lead_trim;
+ }
}
ci->seek_complete();
}
@@ -183,8 +195,46 @@ next_track:
/* Output the audio */
ci->yield();
- ci->pcmbuf_insert(decoder->time_out[0], decoder->time_out[1],
- frame_info.samples >> 1);
+
+ framelength = (frame_info.samples >> 1) - lead_trim;
+
+ if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
+ {
+ /* Currently limited to at most one frame of tail_trim.
+ * Seems to be enough.
+ */
+ if (ci->id3->tail_trim == 0
+ && sample_duration < (frame_info.samples >> 1))
+ {
+ /* Subtract lead_trim just in case we decode a file with
+ * only one audio frame with actual data.
+ */
+ framelength = sample_duration - lead_trim;
+ }
+ else
+ {
+ framelength -= ci->id3->tail_trim;
+ }
+ }
+
+ if (framelength > 0)
+ {
+ ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
+ &decoder->time_out[1][lead_trim],
+ framelength);
+ }
+
+ if (lead_trim > 0)
+ {
+ /* frame_info.samples can be 0 for the first frame */
+ lead_trim -= (i > 0 || frame_info.samples)
+ ? (frame_info.samples >> 1) : sample_duration;
+
+ if (lead_trim < 0 || ci->id3->lead_trim == 0)
+ {
+ lead_trim = 0;
+ }
+ }
/* Update the elapsed-time indicator */
sound_samples_done += sample_duration;
diff --git a/apps/metadata.c b/apps/metadata.c
index 357c5e649e..a0c2f31eee 100644
--- a/apps/metadata.c
+++ b/apps/metadata.c
@@ -206,6 +206,43 @@ static long get_slong(void* buf)
return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
+
+static char* skip_space(char* str)
+{
+ while (isspace(*str))
+ {
+ str++;
+ }
+
+ return str;
+}
+
+static unsigned long get_itunes_int32(char* value, int count)
+{
+ static const char hexdigits[] = "0123456789ABCDEF";
+ const char* c;
+ int r = 0;
+
+ while (count-- > 0)
+ {
+ value = skip_space(value);
+
+ while (*value && !isspace(*value))
+ {
+ value++;
+ }
+ }
+
+ value = skip_space(value);
+
+ while (*value && ((c = strchr(hexdigits, toupper(*value))) != NULL))
+ {
+ r = (r << 4) | (c - hexdigits);
+ value++;
+ }
+
+ return r;
+}
/* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.
* String values to keep are written to buf. Returns number of bytes written
@@ -1520,6 +1557,19 @@ static bool read_mp4_tags(int fd, struct mp3entry* id3,
{
read_mp4_tag_string(fd, size, &buffer, &buffer_left,
&id3->composer);
+ }
+ else if (strcasecmp(tag_name, "iTunSMPB") == 0)
+ {
+ char value[TAG_VALUE_LENGTH];
+ char* value_p = value;
+ char* any;
+ unsigned int length = sizeof(value);
+
+ read_mp4_tag_string(fd, size, &value_p, &length, &any);
+ id3->lead_trim = get_itunes_int32(value, 1);
+ id3->tail_trim = get_itunes_int32(value, 2);
+ DEBUGF("AAC: lead_trim %d, tail_trim %d\n",
+ id3->lead_trim, id3->tail_trim);
}
else
{