FFmpeg
id3v2enc.c
Go to the documentation of this file.
1 /*
2  * ID3v2 header writer
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdint.h>
22 #include <string.h>
23 
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avformat.h"
28 #include "avio.h"
29 #include "avio_internal.h"
30 #include "id3v2.h"
31 
32 static void id3v2_put_size(AVIOContext *pb, int size)
33 {
34  avio_w8(pb, size >> 21 & 0x7f);
35  avio_w8(pb, size >> 14 & 0x7f);
36  avio_w8(pb, size >> 7 & 0x7f);
37  avio_w8(pb, size & 0x7f);
38 }
39 
40 static int string_is_ascii(const uint8_t *str)
41 {
42  while (*str && *str < 128) str++;
43  return !*str;
44 }
45 
46 static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
47  enum ID3v2Encoding enc)
48 {
49  int (*put)(AVIOContext*, const char*);
50 
51  if (enc == ID3v2_ENCODING_UTF16BOM) {
52  avio_wl16(pb, 0xFEFF); /* BOM */
53  put = avio_put_str16le;
54  } else
55  put = avio_put_str;
56 
57  put(pb, str);
58 }
59 
60 /**
61  * Write a text frame with one (normal frames) or two (TXXX frames) strings
62  * according to encoding (only UTF-8 or UTF-16+BOM supported).
63  * @return number of bytes written or a negative error code.
64  */
65 static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
66  uint32_t tag, enum ID3v2Encoding enc)
67 {
68  int len, ret;
69  uint8_t *pb;
70  AVIOContext *dyn_buf;
71  if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
72  return ret;
73 
74  /* check if the strings are ASCII-only and use UTF16 only if
75  * they're not */
76  if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
77  (!str2 || string_is_ascii(str2)))
79 
80  avio_w8(dyn_buf, enc);
81  id3v2_encode_string(dyn_buf, str1, enc);
82  if (str2)
83  id3v2_encode_string(dyn_buf, str2, enc);
84  len = avio_get_dyn_buf(dyn_buf, &pb);
85 
86  avio_wb32(avioc, tag);
87  /* ID3v2.3 frame size is not sync-safe */
88  if (id3->version == 3)
89  avio_wb32(avioc, len);
90  else
91  id3v2_put_size(avioc, len);
92  avio_wb16(avioc, 0);
93  avio_write(avioc, pb, len);
94 
95  ffio_free_dyn_buf(&dyn_buf);
96  return len + ID3v2_HEADER_SIZE;
97 }
98 
99 /**
100  * Write a priv frame with owner and data. 'key' is the owner prepended with
101  * ID3v2_PRIV_METADATA_PREFIX. 'data' is provided as a string. Any \xXX
102  * (where 'X' is a valid hex digit) will be unescaped to the byte value.
103  */
104 static int id3v2_put_priv(ID3v2EncContext *id3, AVIOContext *avioc, const char *key, const char *data)
105 {
106  int len, ret;
107  uint8_t *pb;
108  AVIOContext *dyn_buf;
109 
110  if (!av_strstart(key, ID3v2_PRIV_METADATA_PREFIX, &key)) {
111  return 0;
112  }
113 
114  if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
115  return ret;
116 
117  // owner + null byte.
118  avio_write(dyn_buf, key, strlen(key) + 1);
119 
120  while (*data) {
121  if (av_strstart(data, "\\x", &data)) {
122  if (data[0] && data[1] && av_isxdigit(data[0]) && av_isxdigit(data[1])) {
123  char digits[] = {data[0], data[1], 0};
124  avio_w8(dyn_buf, strtol(digits, NULL, 16));
125  data += 2;
126  } else {
127  ffio_free_dyn_buf(&dyn_buf);
128  av_log(avioc, AV_LOG_ERROR, "Invalid escape '\\x%.2s' in metadata tag '"
129  ID3v2_PRIV_METADATA_PREFIX "%s'.\n", data, key);
130  return AVERROR(EINVAL);
131  }
132  } else {
133  avio_write(dyn_buf, data++, 1);
134  }
135  }
136 
137  len = avio_get_dyn_buf(dyn_buf, &pb);
138 
139  avio_wb32(avioc, MKBETAG('P', 'R', 'I', 'V'));
140  if (id3->version == 3)
141  avio_wb32(avioc, len);
142  else
143  id3v2_put_size(avioc, len);
144  avio_wb16(avioc, 0);
145  avio_write(avioc, pb, len);
146 
147  ffio_free_dyn_buf(&dyn_buf);
148 
149  return len + ID3v2_HEADER_SIZE;
150 }
151 
153  const char table[][4], enum ID3v2Encoding enc)
154 {
155  uint32_t tag;
156  int i;
157 
158  if (t->key[0] != 'T' || strlen(t->key) != 4)
159  return -1;
160  tag = AV_RB32(t->key);
161  for (i = 0; *table[i]; i++)
162  if (tag == AV_RB32(table[i]))
163  return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
164  return -1;
165 }
166 
168 {
169  AVDictionaryEntry *mtag = NULL;
170  AVDictionary *dst = NULL;
171  const char *key, *value;
172  char year[5] = {0}, day_month[5] = {0};
173  int i;
174 
175  while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
176  key = mtag->key;
177  if (!av_strcasecmp(key, "date")) {
178  /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
179  value = mtag->value;
180  i = 0;
181  while (value[i] >= '0' && value[i] <= '9') i++;
182  if (value[i] == '\0' || value[i] == '-') {
183  av_strlcpy(year, value, sizeof(year));
184  av_dict_set(&dst, "TYER", year, 0);
185 
186  if (value[i] == '-' &&
187  value[i+1] >= '0' && value[i+1] <= '1' &&
188  value[i+2] >= '0' && value[i+2] <= '9' &&
189  value[i+3] == '-' &&
190  value[i+4] >= '0' && value[i+4] <= '3' &&
191  value[i+5] >= '0' && value[i+5] <= '9' &&
192  (value[i+6] == '\0' || value[i+6] == ' ')) {
193  snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
194  av_dict_set(&dst, "TDAT", day_month, 0);
195  }
196  } else
197  av_dict_set(&dst, key, value, 0);
198  } else
199  av_dict_set(&dst, key, mtag->value, 0);
200  }
201  av_dict_free(pm);
202  *pm = dst;
203 }
204 
205 void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
206  const char *magic)
207 {
208  id3->version = id3v2_version;
209 
210  avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
211  avio_w8(pb, 0);
212  avio_w8(pb, 0); /* flags */
213 
214  /* reserve space for size */
215  id3->size_pos = avio_tell(pb);
216  avio_wb32(pb, 0);
217 }
218 
219 static int write_metadata(AVIOContext *pb, AVDictionary **metadata,
220  ID3v2EncContext *id3, int enc)
221 {
222  AVDictionaryEntry *t = NULL;
223  int ret;
224 
226  if (id3->version == 3)
227  id3v2_3_metadata_split_date(metadata);
228  else if (id3->version == 4)
230 
231  while ((t = av_dict_get(*metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
232  if ((ret = id3v2_check_write_tag(id3, pb, t, ff_id3v2_tags, enc)) > 0) {
233  id3->len += ret;
234  continue;
235  }
236  if ((ret = id3v2_check_write_tag(id3, pb, t, id3->version == 3 ?
237  ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
238  id3->len += ret;
239  continue;
240  }
241 
242  if ((ret = id3v2_put_priv(id3, pb, t->key, t->value)) > 0) {
243  id3->len += ret;
244  continue;
245  } else if (ret < 0) {
246  return ret;
247  }
248 
249  /* unknown tag, write as TXXX frame */
250  if ((ret = id3v2_put_ttag(id3, pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
251  return ret;
252  id3->len += ret;
253  }
254 
255  return 0;
256 }
257 
258 static int write_ctoc(AVFormatContext *s, ID3v2EncContext *id3, int enc)
259 {
260  uint8_t *dyn_buf;
261  AVIOContext *dyn_bc;
262  char name[123];
263  int len, ret;
264 
265  if (s->nb_chapters == 0)
266  return 0;
267 
268  if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
269  return ret;
270 
271  id3->len += avio_put_str(dyn_bc, "toc");
272  avio_w8(dyn_bc, 0x03);
273  avio_w8(dyn_bc, s->nb_chapters);
274  for (int i = 0; i < s->nb_chapters; i++) {
275  snprintf(name, 122, "ch%d", i);
276  id3->len += avio_put_str(dyn_bc, name);
277  }
278  len = avio_get_dyn_buf(dyn_bc, &dyn_buf);
279  id3->len += 16 + ID3v2_HEADER_SIZE;
280 
281  avio_wb32(s->pb, MKBETAG('C', 'T', 'O', 'C'));
282  avio_wb32(s->pb, len);
283  avio_wb16(s->pb, 0);
284  avio_write(s->pb, dyn_buf, len);
285 
286  ffio_free_dyn_buf(&dyn_bc);
287 
288  return ret;
289 }
290 
291 static int write_chapter(AVFormatContext *s, ID3v2EncContext *id3, int id, int enc)
292 {
293  const AVRational time_base = {1, 1000};
294  AVChapter *ch = s->chapters[id];
295  uint8_t *dyn_buf;
296  AVIOContext *dyn_bc;
297  char name[123];
298  int len, start, end, ret;
299 
300  if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
301  return ret;
302 
303  start = av_rescale_q(ch->start, ch->time_base, time_base);
304  end = av_rescale_q(ch->end, ch->time_base, time_base);
305 
306  snprintf(name, 122, "ch%d", id);
307  id3->len += avio_put_str(dyn_bc, name);
308  avio_wb32(dyn_bc, start);
309  avio_wb32(dyn_bc, end);
310  avio_wb32(dyn_bc, 0xFFFFFFFFu);
311  avio_wb32(dyn_bc, 0xFFFFFFFFu);
312 
313  if ((ret = write_metadata(dyn_bc, &ch->metadata, id3, enc)) < 0)
314  goto fail;
315 
316  len = avio_get_dyn_buf(dyn_bc, &dyn_buf);
317  id3->len += 16 + ID3v2_HEADER_SIZE;
318 
319  avio_wb32(s->pb, MKBETAG('C', 'H', 'A', 'P'));
320  avio_wb32(s->pb, len);
321  avio_wb16(s->pb, 0);
322  avio_write(s->pb, dyn_buf, len);
323 
324 fail:
325  ffio_free_dyn_buf(&dyn_bc);
326 
327  return ret;
328 }
329 
331 {
332  int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
334  int i, ret;
335 
337  if ((ret = write_metadata(s->pb, &s->metadata, id3, enc)) < 0)
338  return ret;
339 
340  if ((ret = write_ctoc(s, id3, enc)) < 0)
341  return ret;
342 
343  for (i = 0; i < s->nb_chapters; i++) {
344  if ((ret = write_chapter(s, id3, i, enc)) < 0)
345  return ret;
346  }
347 
348  return 0;
349 }
350 
352 {
353  AVStream *st = s->streams[pkt->stream_index];
355 
356  AVIOContext *dyn_buf;
357  uint8_t *buf;
358  const CodecMime *mime = ff_id3v2_mime_tags;
359  const char *mimetype = NULL, *desc = "";
360  int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
362  int i, len, type = 0, ret;
363 
364  /* get the mimetype*/
365  while (mime->id != AV_CODEC_ID_NONE) {
366  if (mime->id == st->codecpar->codec_id) {
367  mimetype = mime->str;
368  break;
369  }
370  mime++;
371  }
372  if (!mimetype) {
373  av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
374  "write an attached picture.\n", st->index);
375  return AVERROR(EINVAL);
376  }
377 
378  /* get the picture type */
379  e = av_dict_get(st->metadata, "comment", NULL, 0);
380  for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
382  type = i;
383  break;
384  }
385  }
386 
387  /* get the description */
388  if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
389  desc = e->value;
390 
391  /* use UTF16 only for non-ASCII strings */
392  if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(desc))
394 
395  /* start writing */
396  if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
397  return ret;
398 
399  avio_w8(dyn_buf, enc);
400  avio_put_str(dyn_buf, mimetype);
401  avio_w8(dyn_buf, type);
402  id3v2_encode_string(dyn_buf, desc, enc);
403  avio_write(dyn_buf, pkt->data, pkt->size);
404  len = avio_get_dyn_buf(dyn_buf, &buf);
405 
406  avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
407  if (id3->version == 3)
408  avio_wb32(s->pb, len);
409  else
410  id3v2_put_size(s->pb, len);
411  avio_wb16(s->pb, 0);
412  avio_write(s->pb, buf, len);
413  ffio_free_dyn_buf(&dyn_buf);
414 
415  id3->len += len + ID3v2_HEADER_SIZE;
416 
417  return 0;
418 }
419 
421  int padding_bytes)
422 {
423  int64_t cur_pos;
424 
425  if (padding_bytes < 0)
426  padding_bytes = 10;
427 
428  /* The ID3v2.3 specification states that 28 bits are used to represent the
429  * size of the whole tag. Therefore the current size of the tag needs to be
430  * subtracted from the upper limit of 2^28-1 to clip the value correctly. */
431  /* The minimum of 10 is an arbitrary amount of padding at the end of the tag
432  * to fix cover art display with some software such as iTunes, Traktor,
433  * Serato, Torq. */
434  padding_bytes = av_clip(padding_bytes, 10, 268435455 - id3->len);
435  ffio_fill(pb, 0, padding_bytes);
436  id3->len += padding_bytes;
437 
438  cur_pos = avio_tell(pb);
439  avio_seek(pb, id3->size_pos, SEEK_SET);
440  id3v2_put_size(pb, id3->len);
441  avio_seek(pb, cur_pos, SEEK_SET);
442 }
443 
444 int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
445  const char *magic)
446 {
447  ID3v2EncContext id3 = { 0 };
448  int ret;
449 
450  ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
451  if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
452  return ret;
454 
455  return 0;
456 }
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1587
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:472
Bytestream IO Context.
Definition: avio.h:161
Buffered I/O operations.
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int len
size of the tag written so far
Definition: id3v2.h:54
const char * desc
Definition: nvenc.c:68
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition: id3v2enc.c:330
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3971
int index
stream index in AVFormatContext
Definition: avformat.h:882
int size
Definition: avcodec.h:1484
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
#define ID3v2_PRIV_METADATA_PREFIX
Definition: id3v2.h:42
GLint GLenum type
Definition: opengl_enc.c:104
const char ff_id3v2_4_tags[][4]
ID3v2.4-only text information frames.
Definition: id3v2.c:96
const char * key
static AVPacket pkt
AVDictionary * metadata
Definition: avformat.h:1319
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1430
Format I/O context.
Definition: avformat.h:1358
ID3v2Encoding
Definition: id3v2.h:44
char str[32]
Definition: internal.h:48
Public dictionary API.
static int write_ctoc(AVFormatContext *s, ID3v2EncContext *id3, int enc)
Definition: id3v2enc.c:258
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition: id3v2enc.c:205
#define ID3v2_HEADER_SIZE
Definition: id3v2.h:30
const AVMetadataConv ff_id3v2_34_metadata_conv[]
Definition: id3v2.c:45
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
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
uint8_t * data
Definition: avcodec.h:1483
uint32_t tag
Definition: movenc.c:1531
enum AVCodecID id
Definition: internal.h:49
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
#define av_log(a,...)
static const uint16_t table[]
Definition: prosumer.c:206
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
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1598
const AVMetadataConv ff_id3v2_4_metadata_conv[]
Definition: id3v2.c:64
static int string_is_ascii(const uint8_t *str)
Definition: id3v2enc.c:40
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
AVChapter ** chapters
Definition: avformat.h:1588
static int write_chapter(AVFormatContext *s, ID3v2EncContext *id3, int id, int enc)
Definition: id3v2enc.c:291
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: utils.c:5713
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:122
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:131
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:204
int64_t size_pos
offset of the tag total size
Definition: id3v2.h:53
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
#define s(width, name)
Definition: cbs_vp9.c:257
AVDictionary * metadata
Definition: avformat.h:945
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition: id3v2enc.c:420
#define FF_ARRAY_ELEMS(a)
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1494
if(ret)
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:388
int metadata_header_padding
Number of bytes to be written as padding in a metadata header.
Definition: avformat.h:1858
Stream structure.
Definition: avformat.h:881
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1318
const char ff_id3v2_tags[][4]
A list of text information frames allowed in both ID3 v2.3 and v2.4 http://www.id3.org/id3v2.4.0-frames http://www.id3.org/id3v2.4.0-changes.
Definition: id3v2.c:88
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t, const char table[][4], enum ID3v2Encoding enc)
Definition: id3v2enc.c:152
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:196
void * buf
Definition: avisynth_c.h:766
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static int write_metadata(AVIOContext *pb, AVDictionary **metadata, ID3v2EncContext *id3, int enc)
Definition: id3v2enc.c:219
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str, enum ID3v2Encoding enc)
Definition: id3v2enc.c:46
#define snprintf
Definition: snprintf.h:34
static av_const int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition: avstring.h:251
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63)))#define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};static void cpy1(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, len);}static void cpy2(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 2 *len);}static void cpy4(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 4 *len);}static void cpy8(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;}void swri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len){int ch;int off=0;const int os=(out->planar?1:out->ch_count)*out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){int planes=in->planar?in->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){int planes=out->planar?out->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){int planes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version, const char *magic)
Write an ID3v2 tag containing all global metadata from s.
Definition: id3v2enc.c:444
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:478
int64_t start
Definition: avformat.h:1318
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2, uint32_t tag, enum ID3v2Encoding enc)
Write a text frame with one (normal frames) or two (TXXX frames) strings according to encoding (only ...
Definition: id3v2enc.c:65
Main libavformat public API header.
int
static void id3v2_put_size(AVIOContext *pb, int size)
Definition: id3v2enc.c:32
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1317
char * key
Definition: dict.h:86
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
#define MKBETAG(a, b, c, d)
Definition: common.h:367
const char ff_id3v2_3_tags[][4]
ID3v2.3-only text information frames.
Definition: id3v2.c:102
char * value
Definition: dict.h:87
static int id3v2_put_priv(ID3v2EncContext *id3, AVIOContext *avioc, const char *key, const char *data)
Write a priv frame with owner and data.
Definition: id3v2enc.c:104
int len
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition: id3v2enc.c:351
static void id3v2_3_metadata_split_date(AVDictionary **pm)
Definition: id3v2enc.c:167
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:380
void INT64 start
Definition: avisynth_c.h:766
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
const char *const ff_id3v2_picture_types[21]
Definition: id3v2.c:107
int stream_index
Definition: avcodec.h:1485
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
int version
ID3v2 minor version, either 3 or 4.
Definition: id3v2.h:52
enum AVCodecID id
This structure stores compressed data.
Definition: avcodec.h:1460
const char * name
Definition: opengl_enc.c:102
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1442