| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * VorbisComment writer | ||
| 3 | * Copyright (c) 2009 James Darnley | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "avio.h" | ||
| 23 | #include "avio_internal.h" | ||
| 24 | #include "avformat.h" | ||
| 25 | #include "metadata.h" | ||
| 26 | #include "vorbiscomment.h" | ||
| 27 | #include "libavutil/dict.h" | ||
| 28 | |||
| 29 | /** | ||
| 30 | * VorbisComment metadata conversion mapping. | ||
| 31 | * from Ogg Vorbis I format specification: comment field and header specification | ||
| 32 | * http://xiph.org/vorbis/doc/v-comment.html | ||
| 33 | */ | ||
| 34 | const AVMetadataConv ff_vorbiscomment_metadata_conv[] = { | ||
| 35 | { "ALBUMARTIST", "album_artist"}, | ||
| 36 | { "TRACKNUMBER", "track" }, | ||
| 37 | { "DISCNUMBER", "disc" }, | ||
| 38 | { "DESCRIPTION", "comment" }, | ||
| 39 | { 0 } | ||
| 40 | }; | ||
| 41 | |||
| 42 | 30 | int ff_vorbiscomment_length(const AVDictionary *m, const char *vendor_string, | |
| 43 | AVChapter **chapters, unsigned int nb_chapters) | ||
| 44 | { | ||
| 45 | AVIOContext *avio_buf; | ||
| 46 | int ret, len; | ||
| 47 | |||
| 48 | 30 | ret = ffio_open_null_buf(&avio_buf); | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (ret < 0) |
| 50 | ✗ | return ret; | |
| 51 | |||
| 52 | 30 | ret = ff_vorbiscomment_write(avio_buf, m, vendor_string, chapters, nb_chapters); | |
| 53 | 30 | len = ffio_close_null_buf(avio_buf); | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (ret < 0) |
| 55 | ✗ | return ret; | |
| 56 | |||
| 57 | 30 | return len; | |
| 58 | } | ||
| 59 | |||
| 60 | 60 | int ff_vorbiscomment_write(AVIOContext *pb, const AVDictionary *dict, | |
| 61 | const char *vendor_string, | ||
| 62 | AVChapter **chapters, unsigned int nb_chapters) | ||
| 63 | { | ||
| 64 | 60 | size_t vendor_string_length = strlen(vendor_string); | |
| 65 | 60 | int cm_count = 0; | |
| 66 | 60 | avio_wl32(pb, vendor_string_length); | |
| 67 | 60 | avio_write(pb, vendor_string, vendor_string_length); | |
| 68 | /* Vorbis comment only supports 1000 chapters */ | ||
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | if (nb_chapters > 1000) |
| 70 | ✗ | nb_chapters = 1000; | |
| 71 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
60 | if (chapters && nb_chapters) { |
| 72 | ✗ | for (int i = 0; i < nb_chapters; i++) { | |
| 73 | ✗ | cm_count += av_dict_count(chapters[i]->metadata) + 1; | |
| 74 | } | ||
| 75 | } | ||
| 76 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 4 times.
|
60 | if (dict) { |
| 77 | 56 | int count = av_dict_count(dict) + cm_count; | |
| 78 | 56 | const AVDictionaryEntry *tag = NULL; | |
| 79 | 56 | avio_wl32(pb, count); | |
| 80 |
2/2✓ Branch 1 taken 112 times.
✓ Branch 2 taken 56 times.
|
168 | while ((tag = av_dict_iterate(dict, tag))) { |
| 81 | 112 | int64_t len1 = strlen(tag->key); | |
| 82 | 112 | int64_t len2 = strlen(tag->value); | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (len1+1+len2 > UINT32_MAX) |
| 84 | ✗ | return AVERROR(EINVAL); | |
| 85 | 112 | avio_wl32(pb, len1 + 1 + len2); | |
| 86 | 112 | avio_write(pb, tag->key, len1); | |
| 87 | 112 | avio_w8(pb, '='); | |
| 88 | 112 | avio_write(pb, tag->value, len2); | |
| 89 | } | ||
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | for (int i = 0; i < nb_chapters; i++) { |
| 91 | ✗ | AVChapter *chp = chapters[i]; | |
| 92 | char chapter_time[64]; | ||
| 93 | int h, m, s, ms, len; | ||
| 94 | |||
| 95 | ✗ | s = av_rescale(chp->start, chp->time_base.num, chp->time_base.den); | |
| 96 | ✗ | h = s / 3600; | |
| 97 | ✗ | m = (s / 60) % 60; | |
| 98 | ✗ | ms = av_rescale_q(chp->start, chp->time_base, av_make_q( 1, 1000)) % 1000; | |
| 99 | ✗ | s = s % 60; | |
| 100 | ✗ | len = snprintf(chapter_time, sizeof(chapter_time), "CHAPTER%03d=%02d:%02d:%02d.%03d", i, h, m, s, ms); | |
| 101 | ✗ | avio_wl32(pb, len); | |
| 102 | ✗ | avio_write(pb, chapter_time, len); | |
| 103 | |||
| 104 | ✗ | tag = NULL; | |
| 105 | ✗ | while ((tag = av_dict_iterate(chapters[i]->metadata, tag))) { | |
| 106 | ✗ | int64_t len1 = !strcmp(tag->key, "title") ? 4 : strlen(tag->key); | |
| 107 | ✗ | int64_t len2 = strlen(tag->value); | |
| 108 | ✗ | if (len1+1+len2+10 > UINT32_MAX) | |
| 109 | ✗ | return AVERROR(EINVAL); | |
| 110 | ✗ | avio_wl32(pb, 10 + len1 + 1 + len2); | |
| 111 | ✗ | avio_write(pb, chapter_time, 10); | |
| 112 | ✗ | if (!strcmp(tag->key, "title")) | |
| 113 | ✗ | avio_write(pb, "NAME", 4); | |
| 114 | else | ||
| 115 | ✗ | avio_write(pb, tag->key, len1); | |
| 116 | ✗ | avio_w8(pb, '='); | |
| 117 | ✗ | avio_write(pb, tag->value, len2); | |
| 118 | } | ||
| 119 | } | ||
| 120 | } else | ||
| 121 | 4 | avio_wl32(pb, 0); | |
| 122 | 60 | return 0; | |
| 123 | } | ||
| 124 |