• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/id3v2enc.c

Go to the documentation of this file.
00001 /*
00002  * ID3v2 header writer
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <stdint.h>
00022 
00023 #include "libavutil/dict.h"
00024 #include "libavutil/intreadwrite.h"
00025 #include "avformat.h"
00026 #include "avio.h"
00027 #include "id3v2.h"
00028 
00029 static void id3v2_put_size(AVFormatContext *s, int size)
00030 {
00031     avio_w8(s->pb, size >> 21 & 0x7f);
00032     avio_w8(s->pb, size >> 14 & 0x7f);
00033     avio_w8(s->pb, size >> 7  & 0x7f);
00034     avio_w8(s->pb, size       & 0x7f);
00035 }
00036 
00037 static int string_is_ascii(const uint8_t *str)
00038 {
00039     while (*str && *str < 128) str++;
00040     return !*str;
00041 }
00042 
00048 static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
00049                           uint32_t tag, enum ID3v2Encoding enc)
00050 {
00051     int len;
00052     uint8_t *pb;
00053     int (*put)(AVIOContext*, const char*);
00054     AVIOContext *dyn_buf;
00055     if (avio_open_dyn_buf(&dyn_buf) < 0)
00056         return AVERROR(ENOMEM);
00057 
00058     /* check if the strings are ASCII-only and use UTF16 only if
00059      * they're not */
00060     if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
00061         (!str2 || string_is_ascii(str2)))
00062         enc = ID3v2_ENCODING_ISO8859;
00063 
00064     avio_w8(dyn_buf, enc);
00065     if (enc == ID3v2_ENCODING_UTF16BOM) {
00066         avio_wl16(dyn_buf, 0xFEFF);      /* BOM */
00067         put = avio_put_str16le;
00068     } else
00069         put = avio_put_str;
00070 
00071     put(dyn_buf, str1);
00072     if (str2)
00073         put(dyn_buf, str2);
00074     len = avio_close_dyn_buf(dyn_buf, &pb);
00075 
00076     avio_wb32(s->pb, tag);
00077     id3v2_put_size(s, len);
00078     avio_wb16(s->pb, 0);
00079     avio_write(s->pb, pb, len);
00080 
00081     av_freep(&pb);
00082     return len + ID3v2_HEADER_SIZE;
00083 }
00084 
00085 static int id3v2_check_write_tag(AVFormatContext *s, AVDictionaryEntry *t, const char table[][4],
00086                                  enum ID3v2Encoding enc)
00087 {
00088     uint32_t tag;
00089     int i;
00090 
00091     if (t->key[0] != 'T' || strlen(t->key) != 4)
00092         return -1;
00093     tag = AV_RB32(t->key);
00094     for (i = 0; *table[i]; i++)
00095         if (tag == AV_RB32(table[i]))
00096             return id3v2_put_ttag(s, t->value, NULL, tag, enc);
00097     return -1;
00098 }
00099 
00100 int ff_id3v2_write(struct AVFormatContext *s, int id3v2_version,
00101                    const char *magic)
00102 {
00103     int64_t size_pos, cur_pos;
00104     AVDictionaryEntry *t = NULL;
00105 
00106     int totlen = 0, enc = id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
00107                                                ID3v2_ENCODING_UTF8;
00108 
00109 
00110     avio_wb32(s->pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
00111     avio_w8(s->pb, 0);
00112     avio_w8(s->pb, 0); /* flags */
00113 
00114     /* reserve space for size */
00115     size_pos = avio_tell(s->pb);
00116     avio_wb32(s->pb, 0);
00117 
00118     ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
00119     if (id3v2_version == 4)
00120         ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
00121 
00122     while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
00123         int ret;
00124 
00125         if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
00126             totlen += ret;
00127             continue;
00128         }
00129         if ((ret = id3v2_check_write_tag(s, t, id3v2_version == 3 ?
00130                                                ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
00131             totlen += ret;
00132             continue;
00133         }
00134 
00135         /* unknown tag, write as TXXX frame */
00136         if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
00137             return ret;
00138         totlen += ret;
00139     }
00140 
00141     cur_pos = avio_tell(s->pb);
00142     avio_seek(s->pb, size_pos, SEEK_SET);
00143     id3v2_put_size(s, totlen);
00144     avio_seek(s->pb, cur_pos, SEEK_SET);
00145     return 0;
00146 }
Generated on Thu Jan 24 2013 17:08:55 for Libav by doxygen 1.7.1