LCOV - code coverage report
Current view: top level - libavformat - id3v2enc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 67 190 35.3 %
Date: 2017-12-18 20:14:19 Functions: 8 13 61.5 %

          Line data    Source code
       1             : /*
       2             :  * ID3v2 header writer
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <stdint.h>
      22             : #include <string.h>
      23             : 
      24             : #include "libavutil/avstring.h"
      25             : #include "libavutil/dict.h"
      26             : #include "libavutil/intreadwrite.h"
      27             : #include "avformat.h"
      28             : #include "avio.h"
      29             : #include "avio_internal.h"
      30             : #include "id3v2.h"
      31             : 
      32           2 : static void id3v2_put_size(AVIOContext *pb, int size)
      33             : {
      34           2 :     avio_w8(pb, size >> 21 & 0x7f);
      35           2 :     avio_w8(pb, size >> 14 & 0x7f);
      36           2 :     avio_w8(pb, size >> 7  & 0x7f);
      37           2 :     avio_w8(pb, size       & 0x7f);
      38           2 : }
      39             : 
      40           0 : static int string_is_ascii(const uint8_t *str)
      41             : {
      42           0 :     while (*str && *str < 128) str++;
      43           0 :     return !*str;
      44             : }
      45             : 
      46           1 : static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
      47             :                                enum ID3v2Encoding enc)
      48             : {
      49             :     int (*put)(AVIOContext*, const char*);
      50             : 
      51           1 :     if (enc == ID3v2_ENCODING_UTF16BOM) {
      52           0 :         avio_wl16(pb, 0xFEFF);      /* BOM */
      53           0 :         put = avio_put_str16le;
      54             :     } else
      55           1 :         put = avio_put_str;
      56             : 
      57           1 :     put(pb, str);
      58           1 : }
      59             : 
      60             : /**
      61             :  * Write a text frame with one (normal frames) or two (TXXX frames) strings
      62             :  * according to encoding (only UTF-8 or UTF-16+BOM supported).
      63             :  * @return number of bytes written or a negative error code.
      64             :  */
      65           1 : static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
      66             :                           uint32_t tag, enum ID3v2Encoding enc)
      67             : {
      68             :     int len;
      69             :     uint8_t *pb;
      70             :     AVIOContext *dyn_buf;
      71           1 :     if (avio_open_dyn_buf(&dyn_buf) < 0)
      72           0 :         return AVERROR(ENOMEM);
      73             : 
      74             :     /* check if the strings are ASCII-only and use UTF16 only if
      75             :      * they're not */
      76           1 :     if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
      77           0 :         (!str2 || string_is_ascii(str2)))
      78           0 :         enc = ID3v2_ENCODING_ISO8859;
      79             : 
      80           1 :     avio_w8(dyn_buf, enc);
      81           1 :     id3v2_encode_string(dyn_buf, str1, enc);
      82           1 :     if (str2)
      83           0 :         id3v2_encode_string(dyn_buf, str2, enc);
      84           1 :     len = avio_close_dyn_buf(dyn_buf, &pb);
      85             : 
      86           1 :     avio_wb32(avioc, tag);
      87             :     /* ID3v2.3 frame size is not sync-safe */
      88           1 :     if (id3->version == 3)
      89           0 :         avio_wb32(avioc, len);
      90             :     else
      91           1 :         id3v2_put_size(avioc, len);
      92           1 :     avio_wb16(avioc, 0);
      93           1 :     avio_write(avioc, pb, len);
      94             : 
      95           1 :     av_freep(&pb);
      96           1 :     return len + ID3v2_HEADER_SIZE;
      97             : }
      98             : 
      99           1 : static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t,
     100             :                                  const char table[][4], enum ID3v2Encoding enc)
     101             : {
     102             :     uint32_t tag;
     103             :     int i;
     104             : 
     105           1 :     if (t->key[0] != 'T' || strlen(t->key) != 4)
     106           0 :         return -1;
     107           1 :     tag = AV_RB32(t->key);
     108          11 :     for (i = 0; *table[i]; i++)
     109          11 :         if (tag == AV_RB32(table[i]))
     110           1 :             return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
     111           0 :     return -1;
     112             : }
     113             : 
     114           0 : static void id3v2_3_metadata_split_date(AVDictionary **pm)
     115             : {
     116           0 :     AVDictionaryEntry *mtag = NULL;
     117           0 :     AVDictionary *dst = NULL;
     118             :     const char *key, *value;
     119           0 :     char year[5] = {0}, day_month[5] = {0};
     120             :     int i;
     121             : 
     122           0 :     while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
     123           0 :         key = mtag->key;
     124           0 :         if (!av_strcasecmp(key, "date")) {
     125             :             /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
     126           0 :             value = mtag->value;
     127           0 :             i = 0;
     128           0 :             while (value[i] >= '0' && value[i] <= '9') i++;
     129           0 :             if (value[i] == '\0' || value[i] == '-') {
     130           0 :                 av_strlcpy(year, value, sizeof(year));
     131           0 :                 av_dict_set(&dst, "TYER", year, 0);
     132             : 
     133           0 :                 if (value[i] == '-' &&
     134           0 :                     value[i+1] >= '0' && value[i+1] <= '1' &&
     135           0 :                     value[i+2] >= '0' && value[i+2] <= '9' &&
     136           0 :                     value[i+3] == '-' &&
     137           0 :                     value[i+4] >= '0' && value[i+4] <= '3' &&
     138           0 :                     value[i+5] >= '0' && value[i+5] <= '9' &&
     139           0 :                     (value[i+6] == '\0' || value[i+6] == ' ')) {
     140           0 :                     snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
     141           0 :                     av_dict_set(&dst, "TDAT", day_month, 0);
     142             :                 }
     143             :             } else
     144           0 :                 av_dict_set(&dst, key, value, 0);
     145             :         } else
     146           0 :             av_dict_set(&dst, key, mtag->value, 0);
     147             :     }
     148           0 :     av_dict_free(pm);
     149           0 :     *pm = dst;
     150           0 : }
     151             : 
     152           1 : void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
     153             :                     const char *magic)
     154             : {
     155           1 :     id3->version = id3v2_version;
     156             : 
     157           1 :     avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
     158           1 :     avio_w8(pb, 0);
     159           1 :     avio_w8(pb, 0); /* flags */
     160             : 
     161             :     /* reserve space for size */
     162           1 :     id3->size_pos = avio_tell(pb);
     163           1 :     avio_wb32(pb, 0);
     164           1 : }
     165             : 
     166           1 : static int write_metadata(AVIOContext *pb, AVDictionary **metadata,
     167             :                           ID3v2EncContext *id3, int enc)
     168             : {
     169           1 :     AVDictionaryEntry *t = NULL;
     170             :     int ret;
     171             : 
     172           1 :     ff_metadata_conv(metadata, ff_id3v2_34_metadata_conv, NULL);
     173           1 :     if (id3->version == 3)
     174           0 :         id3v2_3_metadata_split_date(metadata);
     175           1 :     else if (id3->version == 4)
     176           1 :         ff_metadata_conv(metadata, ff_id3v2_4_metadata_conv, NULL);
     177             : 
     178           3 :     while ((t = av_dict_get(*metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
     179           1 :         if ((ret = id3v2_check_write_tag(id3, pb, t, ff_id3v2_tags, enc)) > 0) {
     180           1 :             id3->len += ret;
     181           1 :             continue;
     182             :         }
     183           0 :         if ((ret = id3v2_check_write_tag(id3, pb, t, id3->version == 3 ?
     184             :                                          ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
     185           0 :             id3->len += ret;
     186           0 :             continue;
     187             :         }
     188             : 
     189             :         /* unknown tag, write as TXXX frame */
     190           0 :         if ((ret = id3v2_put_ttag(id3, pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
     191           0 :             return ret;
     192           0 :         id3->len += ret;
     193             :     }
     194             : 
     195           1 :     return 0;
     196             : }
     197             : 
     198           0 : static int write_chapter(AVFormatContext *s, ID3v2EncContext *id3, int id, int enc)
     199             : {
     200             :     const AVRational time_base = {1, 1000};
     201           0 :     AVChapter *ch = s->chapters[id];
     202           0 :     uint8_t *dyn_buf = NULL;
     203           0 :     AVIOContext *dyn_bc = NULL;
     204             :     char name[123];
     205             :     int len, start, end, ret;
     206             : 
     207           0 :     if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
     208           0 :         goto fail;
     209             : 
     210           0 :     start = av_rescale_q(ch->start, ch->time_base, time_base);
     211           0 :     end   = av_rescale_q(ch->end,   ch->time_base, time_base);
     212             : 
     213           0 :     snprintf(name, 122, "ch%d", id);
     214           0 :     id3->len += avio_put_str(dyn_bc, name);
     215           0 :     avio_wb32(dyn_bc, start);
     216           0 :     avio_wb32(dyn_bc, end);
     217           0 :     avio_wb32(dyn_bc, 0xFFFFFFFFu);
     218           0 :     avio_wb32(dyn_bc, 0xFFFFFFFFu);
     219             : 
     220           0 :     if ((ret = write_metadata(dyn_bc, &ch->metadata, id3, enc)) < 0)
     221           0 :         goto fail;
     222             : 
     223           0 :     len = avio_close_dyn_buf(dyn_bc, &dyn_buf);
     224           0 :     id3->len += 16 + ID3v2_HEADER_SIZE;
     225             : 
     226           0 :     avio_wb32(s->pb, MKBETAG('C', 'H', 'A', 'P'));
     227           0 :     avio_wb32(s->pb, len);
     228           0 :     avio_wb16(s->pb, 0);
     229           0 :     avio_write(s->pb, dyn_buf, len);
     230             : 
     231           0 : fail:
     232           0 :     if (dyn_bc && !dyn_buf)
     233           0 :         avio_close_dyn_buf(dyn_bc, &dyn_buf);
     234           0 :     av_freep(&dyn_buf);
     235             : 
     236           0 :     return ret;
     237             : }
     238             : 
     239           1 : int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
     240             : {
     241           1 :     int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
     242             :                                   ID3v2_ENCODING_UTF8;
     243             :     int i, ret;
     244             : 
     245           1 :     ff_standardize_creation_time(s);
     246           1 :     if ((ret = write_metadata(s->pb, &s->metadata, id3, enc)) < 0)
     247           0 :         return ret;
     248             : 
     249           1 :     for (i = 0; i < s->nb_chapters; i++) {
     250           0 :         if ((ret = write_chapter(s, id3, i, enc)) < 0)
     251           0 :             return ret;
     252             :     }
     253             : 
     254           1 :     return 0;
     255             : }
     256             : 
     257           0 : int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
     258             : {
     259           0 :     AVStream *st = s->streams[pkt->stream_index];
     260             :     AVDictionaryEntry *e;
     261             : 
     262             :     AVIOContext *dyn_buf;
     263             :     uint8_t     *buf;
     264           0 :     const CodecMime *mime = ff_id3v2_mime_tags;
     265           0 :     const char  *mimetype = NULL, *desc = "";
     266           0 :     int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
     267             :                                   ID3v2_ENCODING_UTF8;
     268           0 :     int i, len, type = 0;
     269             : 
     270             :     /* get the mimetype*/
     271           0 :     while (mime->id != AV_CODEC_ID_NONE) {
     272           0 :         if (mime->id == st->codecpar->codec_id) {
     273           0 :             mimetype = mime->str;
     274           0 :             break;
     275             :         }
     276           0 :         mime++;
     277             :     }
     278           0 :     if (!mimetype) {
     279           0 :         av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
     280             :                "write an attached picture.\n", st->index);
     281           0 :         return AVERROR(EINVAL);
     282             :     }
     283             : 
     284             :     /* get the picture type */
     285           0 :     e = av_dict_get(st->metadata, "comment", NULL, 0);
     286           0 :     for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
     287           0 :         if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
     288           0 :             type = i;
     289           0 :             break;
     290             :         }
     291             :     }
     292             : 
     293             :     /* get the description */
     294           0 :     if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
     295           0 :         desc = e->value;
     296             : 
     297             :     /* use UTF16 only for non-ASCII strings */
     298           0 :     if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(desc))
     299           0 :         enc = ID3v2_ENCODING_ISO8859;
     300             : 
     301             :     /* start writing */
     302           0 :     if (avio_open_dyn_buf(&dyn_buf) < 0)
     303           0 :         return AVERROR(ENOMEM);
     304             : 
     305           0 :     avio_w8(dyn_buf, enc);
     306           0 :     avio_put_str(dyn_buf, mimetype);
     307           0 :     avio_w8(dyn_buf, type);
     308           0 :     id3v2_encode_string(dyn_buf, desc, enc);
     309           0 :     avio_write(dyn_buf, pkt->data, pkt->size);
     310           0 :     len = avio_close_dyn_buf(dyn_buf, &buf);
     311             : 
     312           0 :     avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
     313           0 :     if (id3->version == 3)
     314           0 :         avio_wb32(s->pb, len);
     315             :     else
     316           0 :         id3v2_put_size(s->pb, len);
     317           0 :     avio_wb16(s->pb, 0);
     318           0 :     avio_write(s->pb, buf, len);
     319           0 :     av_freep(&buf);
     320             : 
     321           0 :     id3->len += len + ID3v2_HEADER_SIZE;
     322             : 
     323           0 :     return 0;
     324             : }
     325             : 
     326           1 : void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb,
     327             :                      int padding_bytes)
     328             : {
     329             :     int64_t cur_pos;
     330             : 
     331           1 :     if (padding_bytes < 0)
     332           1 :         padding_bytes = 10;
     333             : 
     334             :     /* The ID3v2.3 specification states that 28 bits are used to represent the
     335             :      * size of the whole tag.  Therefore the current size of the tag needs to be
     336             :      * subtracted from the upper limit of 2^28-1 to clip the value correctly. */
     337             :     /* The minimum of 10 is an arbitrary amount of padding at the end of the tag
     338             :      * to fix cover art display with some software such as iTunes, Traktor,
     339             :      * Serato, Torq. */
     340           1 :     padding_bytes = av_clip(padding_bytes, 10, 268435455 - id3->len);
     341           1 :     ffio_fill(pb, 0, padding_bytes);
     342           1 :     id3->len += padding_bytes;
     343             : 
     344           1 :     cur_pos = avio_tell(pb);
     345           1 :     avio_seek(pb, id3->size_pos, SEEK_SET);
     346           1 :     id3v2_put_size(pb, id3->len);
     347           1 :     avio_seek(pb, cur_pos, SEEK_SET);
     348           1 : }
     349             : 
     350           0 : int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
     351             :                           const char *magic)
     352             : {
     353           0 :     ID3v2EncContext id3 = { 0 };
     354             :     int ret;
     355             : 
     356           0 :     ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
     357           0 :     if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
     358           0 :         return ret;
     359           0 :     ff_id3v2_finish(&id3, s->pb, s->metadata_header_padding);
     360             : 
     361           0 :     return 0;
     362             : }

Generated by: LCOV version 1.13