libavformat/id3v2enc.c
Go to the documentation of this file.
00001 /*
00002  * ID3v2 header writer
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "libavutil/avstring.h"
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 static void id3v2_3_metadata_split_date(AVDictionary **pm)
00101 {
00102     AVDictionaryEntry *mtag = NULL;
00103     AVDictionary *dst = NULL;
00104     const char *key, *value;
00105     char year[5] = {0}, day_month[5] = {0};
00106     int i;
00107 
00108     while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
00109         key = mtag->key;
00110         if (!av_strcasecmp(key, "date")) {
00111             /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
00112             value = mtag->value;
00113             i = 0;
00114             while (value[i] >= '0' && value[i] <= '9') i++;
00115             if (value[i] == '\0' || value[i] == '-') {
00116                 av_strlcpy(year, value, sizeof(year));
00117                 av_dict_set(&dst, "TYER", year, 0);
00118 
00119                 if (value[i] == '-' &&
00120                     value[i+1] >= '0' && value[i+1] <= '1' &&
00121                     value[i+2] >= '0' && value[i+2] <= '9' &&
00122                     value[i+3] == '-' &&
00123                     value[i+4] >= '0' && value[i+4] <= '3' &&
00124                     value[i+5] >= '0' && value[i+5] <= '9' &&
00125                     (value[i+6] == '\0' || value[i+6] == ' ')) {
00126                     snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
00127                     av_dict_set(&dst, "TDAT", day_month, 0);
00128                 }
00129             } else
00130                 av_dict_set(&dst, key, value, 0);
00131         } else
00132             av_dict_set(&dst, key, mtag->value, 0);
00133     }
00134     av_dict_free(pm);
00135     *pm = dst;
00136 }
00137 
00138 int ff_id3v2_write(struct AVFormatContext *s, int id3v2_version,
00139                    const char *magic)
00140 {
00141     int64_t size_pos, cur_pos;
00142     AVDictionaryEntry *t = NULL;
00143 
00144     int totlen = 0, enc = id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
00145                                                ID3v2_ENCODING_UTF8;
00146 
00147 
00148     avio_wb32(s->pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
00149     avio_w8(s->pb, 0);
00150     avio_w8(s->pb, 0); /* flags */
00151 
00152     /* reserve space for size */
00153     size_pos = avio_tell(s->pb);
00154     avio_wb32(s->pb, 0);
00155 
00156     ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
00157     if (id3v2_version == 3)
00158         id3v2_3_metadata_split_date(&s->metadata);
00159     else if (id3v2_version == 4)
00160         ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
00161 
00162     while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
00163         int ret;
00164 
00165         if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
00166             totlen += ret;
00167             continue;
00168         }
00169         if ((ret = id3v2_check_write_tag(s, t, id3v2_version == 3 ?
00170                                                ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
00171             totlen += ret;
00172             continue;
00173         }
00174 
00175         /* unknown tag, write as TXXX frame */
00176         if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
00177             return ret;
00178         totlen += ret;
00179     }
00180 
00181     cur_pos = avio_tell(s->pb);
00182     avio_seek(s->pb, size_pos, SEEK_SET);
00183     id3v2_put_size(s, totlen);
00184     avio_seek(s->pb, cur_pos, SEEK_SET);
00185     return 0;
00186 }