FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/vorbiscomment.c
Date: 2026-08-23 00:09:23
Exec Total Coverage
Lines: 29 57 50.9%
Functions: 2 2 100.0%
Branches: 10 28 35.7%

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 /* Adapted from <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#disc-subtitle> */
39 { "DISCSUBTITLE", "disc_subtitle" },
40 { "DESCRIPTION", "comment" },
41 { 0 }
42 };
43
44 31 int ff_vorbiscomment_length(const AVDictionary *m, const char *vendor_string,
45 AVChapter **chapters, unsigned int nb_chapters)
46 {
47 AVIOContext *avio_buf;
48 int ret, len;
49
50 31 ret = ffio_open_null_buf(&avio_buf);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
52 return ret;
53
54 31 ret = ff_vorbiscomment_write(avio_buf, m, vendor_string, chapters, nb_chapters);
55 31 len = ffio_close_null_buf(avio_buf);
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
57 return ret;
58
59 31 return len;
60 }
61
62 62 int ff_vorbiscomment_write(AVIOContext *pb, const AVDictionary *dict,
63 const char *vendor_string,
64 AVChapter **chapters, unsigned int nb_chapters)
65 {
66 62 size_t vendor_string_length = strlen(vendor_string);
67 62 int cm_count = 0;
68 62 avio_wl32(pb, vendor_string_length);
69 62 avio_write(pb, vendor_string, vendor_string_length);
70 /* Vorbis comment only supports 1000 chapters */
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (nb_chapters > 1000)
72 nb_chapters = 1000;
73
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
62 if (chapters && nb_chapters) {
74 for (int i = 0; i < nb_chapters; i++) {
75 cm_count += av_dict_count(chapters[i]->metadata) + 1;
76 }
77 }
78
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 4 times.
62 if (dict) {
79 58 int count = av_dict_count(dict) + cm_count;
80 58 const AVDictionaryEntry *tag = NULL;
81 58 avio_wl32(pb, count);
82
2/2
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 58 times.
192 while ((tag = av_dict_iterate(dict, tag))) {
83 134 int64_t len1 = strlen(tag->key);
84 134 int64_t len2 = strlen(tag->value);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if (len1+1+len2 > UINT32_MAX)
86 return AVERROR(EINVAL);
87 134 avio_wl32(pb, len1 + 1 + len2);
88 134 avio_write(pb, tag->key, len1);
89 134 avio_w8(pb, '=');
90 134 avio_write(pb, tag->value, len2);
91 }
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 for (int i = 0; i < nb_chapters; i++) {
93 AVChapter *chp = chapters[i];
94 char chapter_time[64];
95 int h, m, s, ms, len;
96
97 s = av_rescale(chp->start, chp->time_base.num, chp->time_base.den);
98 h = s / 3600;
99 m = (s / 60) % 60;
100 ms = av_rescale_q(chp->start, chp->time_base, av_make_q( 1, 1000)) % 1000;
101 s = s % 60;
102 len = snprintf(chapter_time, sizeof(chapter_time), "CHAPTER%03d=%02d:%02d:%02d.%03d", i, h, m, s, ms);
103 avio_wl32(pb, len);
104 avio_write(pb, chapter_time, len);
105
106 tag = NULL;
107 while ((tag = av_dict_iterate(chapters[i]->metadata, tag))) {
108 int64_t len1 = !strcmp(tag->key, "title") ? 4 : strlen(tag->key);
109 int64_t len2 = strlen(tag->value);
110 if (len1+1+len2+10 > UINT32_MAX)
111 return AVERROR(EINVAL);
112 avio_wl32(pb, 10 + len1 + 1 + len2);
113 avio_write(pb, chapter_time, 10);
114 if (!strcmp(tag->key, "title"))
115 avio_write(pb, "NAME", 4);
116 else
117 avio_write(pb, tag->key, len1);
118 avio_w8(pb, '=');
119 avio_write(pb, tag->value, len2);
120 }
121 }
122 } else
123 4 avio_wl32(pb, 0);
124 62 return 0;
125 }
126