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