FFmpeg
Loading...
Searching...
No Matches
vorbiscomment.c
Go to the documentation of this file.
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 */
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
44int 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 ret = ffio_open_null_buf(&avio_buf);
51 if (ret < 0)
52 return ret;
53
54 ret = ff_vorbiscomment_write(avio_buf, m, vendor_string, chapters, nb_chapters);
55 len = ffio_close_null_buf(avio_buf);
56 if (ret < 0)
57 return ret;
58
59 return len;
60}
61
63 const char *vendor_string,
64 AVChapter **chapters, unsigned int nb_chapters)
65{
66 size_t vendor_string_length = strlen(vendor_string);
67 int cm_count = 0;
68 avio_wl32(pb, vendor_string_length);
69 avio_write(pb, vendor_string, vendor_string_length);
70 /* Vorbis comment only supports 1000 chapters */
71 if (nb_chapters > 1000)
72 nb_chapters = 1000;
73 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 if (dict) {
79 int count = av_dict_count(dict) + cm_count;
80 const AVDictionaryEntry *tag = NULL;
81 avio_wl32(pb, count);
82 while ((tag = av_dict_iterate(dict, tag))) {
83 int64_t len1 = strlen(tag->key);
84 int64_t len2 = strlen(tag->value);
85 if (len1+1+len2 > UINT32_MAX)
86 return AVERROR(EINVAL);
87 avio_wl32(pb, len1 + 1 + len2);
88 avio_write(pb, tag->key, len1);
89 avio_w8(pb, '=');
90 avio_write(pb, tag->value, len2);
91 }
92 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 avio_wl32(pb, 0);
124 return 0;
125}
Main libavformat public API header.
Buffered I/O operations.
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition aviobuf.c:1462
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition aviobuf.c:1472
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public dictionary API.
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:37
#define AVERROR(e)
Definition error.h:45
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
internal metadata API header see avformat.h or the public API!
uint32_t tag
Definition movenc.c:2073
#define snprintf
Definition snprintf.h:34
int64_t start
Definition avformat.h:1295
AVRational time_base
time base in which the start/end timestamps are specified
Definition avformat.h:1294
Bytestream IO Context.
Definition avio.h:160
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int len
int ff_vorbiscomment_write(AVIOContext *pb, const AVDictionary *dict, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into an AVIOContext.
int ff_vorbiscomment_length(const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
const AVMetadataConv ff_vorbiscomment_metadata_conv[]
VorbisComment metadata conversion mapping.