[PATCH] Prepare for detecting offset of trailing metadata in =

David Byron none dbyron
Mon Sep 27 10:19:10 CEST 2010


=0A=
- use ID3v1_TAG_SIZE instead of 128 in ff_id3v1_read=0A=
- add ff_id3v1_offset=0A=
---=0A=
 libavformat/id3v1.c |   51 =
++++++++++++++++++++++++++++++++++++++++++++++-----=0A=
 libavformat/id3v1.h |   15 +++++++++++++++=0A=
 2 files changed, 61 insertions(+), 5 deletions(-)=0A=
=0A=
diff --git a/libavformat/id3v1.c b/libavformat/id3v1.c=0A=
index c72fca4..e4d73fd 100644=0A=
--- a/libavformat/id3v1.c=0A=
+++ b/libavformat/id3v1.c=0A=
@@ -174,6 +174,13 @@ const char * const =
ff_id3v1_genre_str[ID3v1_GENRE_MAX + 1] =3D {=0A=
     [147] =3D "SynthPop",=0A=
 };=0A=
 =0A=
+static int is_id3v1(const uint8_t *buf)=0A=
+{=0A=
+    return (buf[0] =3D=3D 'T' &&=0A=
+            buf[1] =3D=3D 'A' &&=0A=
+            buf[2] =3D=3D 'G');=0A=
+}=0A=
+=0A=
 static void get_string(AVFormatContext *s, const char *key,=0A=
                        const uint8_t *buf, int buf_size)=0A=
 {=0A=
@@ -204,9 +211,7 @@ static int parse_tag(AVFormatContext *s, const =
uint8_t *buf)=0A=
 {=0A=
     int genre;=0A=
 =0A=
-    if (!(buf[0] =3D=3D 'T' &&=0A=
-          buf[1] =3D=3D 'A' &&=0A=
-          buf[2] =3D=3D 'G'))=0A=
+    if (!is_id3v1(buf))=0A=
         return -1;=0A=
     get_string(s, "title",   buf +  3, 30);=0A=
     get_string(s, "artist",  buf + 33, 30);=0A=
@@ -229,8 +234,8 @@ void ff_id3v1_read(AVFormatContext *s)=0A=
     if (!url_is_streamed(s->pb)) {=0A=
         /* XXX: change that */=0A=
         filesize =3D url_fsize(s->pb);=0A=
-        if (filesize > 128) {=0A=
-            url_fseek(s->pb, filesize - 128, SEEK_SET);=0A=
+        if (filesize > ID3v1_TAG_SIZE) {=0A=
+            url_fseek(s->pb, filesize - ID3v1_TAG_SIZE, SEEK_SET);=0A=
             ret =3D get_buffer(s->pb, buf, ID3v1_TAG_SIZE);=0A=
             if (ret =3D=3D ID3v1_TAG_SIZE) {=0A=
                 parse_tag(s, buf);=0A=
@@ -239,3 +244,39 @@ void ff_id3v1_read(AVFormatContext *s)=0A=
         }=0A=
     }=0A=
 }=0A=
+=0A=
+int ff_id3v1_offset(AVFormatContext *s,=0A=
+                    int64_t *offset)=0A=
+{=0A=
+    uint8_t buf[3];=0A=
+    int64_t filesize;=0A=
+    int ret;=0A=
+=0A=
+    /* until we find the offset assume there isn't one */=0A=
+    *offset =3D -1;=0A=
+=0A=
+    filesize =3D url_fsize(s->pb);=0A=
+    if (filesize < 0)=0A=
+        return filesize; /* here filesize is actually an error */=0A=
+=0A=
+    if (filesize < ID3v1_TAG_SIZE)=0A=
+        return 0; /* file too small for id3v1 */=0A=
+=0A=
+    ret =3D url_fseek(s->pb, filesize - ID3v1_TAG_SIZE, SEEK_SET);=0A=
+    if (ret < 0)=0A=
+        return ret;=0A=
+=0A=
+    ret =3D get_buffer(s->pb, buf, 3);=0A=
+    if (ret < 0)=0A=
+        return ret;=0A=
+=0A=
+    /* since we checked the file size above, if get_buffer=0A=
+       succeeded it better have given us the all the bytes=0A=
+       we asked for */=0A=
+    assert(ret =3D=3D 3);=0A=
+=0A=
+    if (is_id3v1(buf))=0A=
+        *offset =3D filesize - ID3v1_TAG_SIZE;=0A=
+=0A=
+    return 0;=0A=
+}=0A=
diff --git a/libavformat/id3v1.h b/libavformat/id3v1.h=0A=
index 8eb58be..2f22616 100644=0A=
--- a/libavformat/id3v1.h=0A=
+++ b/libavformat/id3v1.h=0A=
@@ -38,5 +38,20 @@ extern const char * const =
ff_id3v1_genre_str[ID3v1_GENRE_MAX + 1];=0A=
  */=0A=
 void ff_id3v1_read(AVFormatContext *s);=0A=
 =0A=
+/**=0A=
+ * Determine the offset of an id3v1 tag=0A=
+ *=0A=
+ * @param offset populated with -1 if no id3v1 tag,=0A=
+ * otherwise the offset of the id3v1 tag=0A=
+ *=0A=
+ * @retval 0 successfully determined offset of id3v1 tag (or=0A=
+ * that there isn't one).  The stream position is undefined.=0A=
+ *=0A=
+ * @retval < 0 error looking for offset of id3v1 tag.  The=0A=
+ * stream position is undefined.=0A=
+ */=0A=
+int ff_id3v1_offset(AVFormatContext *s,=0A=
+                    int64_t *offset);=0A=
+=0A=
 #endif /* AVFORMAT_ID3V1_H */=0A=
 =0A=
-- =0A=
1.6.0.4=0A=
=0A=

------=_NextPart_000_016F_01CB5E3E.615FC240
Content-Type: application/octet-stream;
	name="0003-detect-where-trailing-metadata-begins-in-mp3-files-s.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="0003-detect-where-trailing-metadata-begins-in-mp3-files-s.patch"




More information about the ffmpeg-devel mailing list