FFmpeg
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 "avformat.h"
23 #include "metadata.h"
24 #include "vorbiscomment.h"
25 #include "libavcodec/bytestream.h"
26 #include "libavutil/dict.h"
27 
28 /**
29  * VorbisComment metadata conversion mapping.
30  * from Ogg Vorbis I format specification: comment field and header specification
31  * http://xiph.org/vorbis/doc/v-comment.html
32  */
34  { "ALBUMARTIST", "album_artist"},
35  { "TRACKNUMBER", "track" },
36  { "DISCNUMBER", "disc" },
37  { "DESCRIPTION", "comment" },
38  { 0 }
39 };
40 
41 int64_t ff_vorbiscomment_length(AVDictionary *m, const char *vendor_string,
42  AVChapter **chapters, unsigned int nb_chapters)
43 {
44  int64_t len = 8;
45  len += strlen(vendor_string);
46  if (chapters && nb_chapters) {
47  for (int i = 0; i < nb_chapters; i++) {
49  len += 4 + 12 + 1 + 10;
50  while ((tag = av_dict_get(chapters[i]->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
51  int64_t len1 = !strcmp(tag->key, "title") ? 4 : strlen(tag->key);
52  len += 4 + 10 + len1 + 1 + strlen(tag->value);
53  }
54  }
55  }
56  if (m) {
58  while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX))) {
59  len += 4 +strlen(tag->key) + 1 + strlen(tag->value);
60  }
61  }
62  return len;
63 }
64 
66  const char *vendor_string,
67  AVChapter **chapters, unsigned int nb_chapters)
68 {
69  int cm_count = 0;
70  bytestream_put_le32(p, strlen(vendor_string));
71  bytestream_put_buffer(p, vendor_string, strlen(vendor_string));
72  if (chapters && nb_chapters) {
73  for (int i = 0; i < nb_chapters; i++) {
74  cm_count += av_dict_count(chapters[i]->metadata) + 1;
75  }
76  }
77  if (*m) {
78  int count = av_dict_count(*m) + cm_count;
80  bytestream_put_le32(p, count);
81  while ((tag = av_dict_get(*m, "", tag, AV_DICT_IGNORE_SUFFIX))) {
82  int64_t len1 = strlen(tag->key);
83  int64_t len2 = strlen(tag->value);
84  if (len1+1+len2 > UINT32_MAX)
85  return AVERROR(EINVAL);
86  bytestream_put_le32(p, len1+1+len2);
87  bytestream_put_buffer(p, tag->key, len1);
88  bytestream_put_byte(p, '=');
89  bytestream_put_buffer(p, tag->value, len2);
90  }
91  for (int i = 0; i < nb_chapters; i++) {
92  AVChapter *chp = chapters[i];
93  char chapter_time[13];
94  char chapter_number[4];
95  int h, m, s, ms;
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  snprintf(chapter_number, sizeof(chapter_number), "%03d", i);
103  snprintf(chapter_time, sizeof(chapter_time), "%02d:%02d:%02d.%03d", h, m, s, ms);
104  bytestream_put_le32(p, 10+1+12);
105  bytestream_put_buffer(p, "CHAPTER", 7);
106  bytestream_put_buffer(p, chapter_number, 3);
107  bytestream_put_byte(p, '=');
108  bytestream_put_buffer(p, chapter_time, 12);
109 
110  tag = NULL;
111  while ((tag = av_dict_get(chapters[i]->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
112  int64_t len1 = !strcmp(tag->key, "title") ? 4 : strlen(tag->key);
113  int64_t len2 = strlen(tag->value);
114  if (len1+1+len2+10 > UINT32_MAX)
115  return AVERROR(EINVAL);
116  bytestream_put_le32(p, 10+len1+1+len2);
117  bytestream_put_buffer(p, "CHAPTER", 7);
118  bytestream_put_buffer(p, chapter_number, 3);
119  if (!strcmp(tag->key, "title"))
120  bytestream_put_buffer(p, "NAME", 4);
121  else
122  bytestream_put_buffer(p, tag->key, len1);
123  bytestream_put_byte(p, '=');
124  bytestream_put_buffer(p, tag->value, len2);
125  }
126  }
127  } else
128  bytestream_put_le32(p, 0);
129  return 0;
130 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_dict_count
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:35
count
void INT64 INT64 count
Definition: avisynth_c.h:767
vorbiscomment.h
AVChapter::start
int64_t start
Definition: avformat.h:1302
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
AVMetadataConv
Definition: metadata.h:34
AVDictionary
Definition: dict.c:30
AVChapter
Definition: avformat.h:1299
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
ff_vorbiscomment_metadata_conv
const AVMetadataConv ff_vorbiscomment_metadata_conv[]
VorbisComment metadata conversion mapping.
Definition: vorbiscomment.c:33
NULL
#define NULL
Definition: coverity.c:32
ff_vorbiscomment_length
int64_t ff_vorbiscomment_length(AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
Definition: vorbiscomment.c:41
ff_vorbiscomment_write
int ff_vorbiscomment_write(uint8_t **p, AVDictionary **m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into a buffer.
Definition: vorbiscomment.c:65
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
uint8_t
uint8_t
Definition: audio_convert.c:194
len
int len
Definition: vorbis_enc_data.h:452
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
tag
uint32_t tag
Definition: movenc.c:1496
metadata.h
avformat.h
dict.h
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVDictionaryEntry
Definition: dict.h:81
bytestream.h
h
h
Definition: vp9dsp_template.c:2038
AVChapter::time_base
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1301
snprintf
#define snprintf
Definition: snprintf.h:34