FFmpeg
apetag.c
Go to the documentation of this file.
1 /*
2  * APE tag handling
3  * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4  * based upon libdemac from Dave Chapman.
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <inttypes.h>
24 
25 #include "libavutil/dict.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "apetag.h"
29 #include "demux.h"
30 #include "internal.h"
31 #include "mux.h"
32 
33 #define APE_TAG_FLAG_CONTAINS_HEADER (1U << 31)
34 #define APE_TAG_FLAG_LACKS_FOOTER (1 << 30)
35 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
36 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
37 
39 {
40  AVIOContext *pb = s->pb;
41  uint8_t key[1024], *value;
42  int64_t size, flags;
43  int i, c;
44 
45  size = avio_rl32(pb); /* field size */
46  flags = avio_rl32(pb); /* field flags */
47  for (i = 0; i < sizeof(key) - 1; i++) {
48  c = avio_r8(pb);
49  if (c < 0x20 || c > 0x7E)
50  break;
51  else
52  key[i] = c;
53  }
54  key[i] = 0;
55  if (c != 0) {
56  av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
57  return -1;
58  }
59  if (size > INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
60  av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
61  return AVERROR_INVALIDDATA;
62  }
64  uint8_t filename[1024];
65  enum AVCodecID id;
66  int ret;
68  if (!st)
69  return AVERROR(ENOMEM);
70 
71  ret = avio_get_str(pb, size, filename, sizeof(filename));
72  if (ret < 0)
73  return ret;
74  if (size <= ret) {
75  av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
76  return 0;
77  }
78  size -= ret;
79 
80  av_dict_set(&st->metadata, key, filename, 0);
81 
82  if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
83  int ret = ff_add_attached_pic(s, st, s->pb, NULL, size);
84  if (ret < 0) {
85  av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
86  return ret;
87  }
88  st->codecpar->codec_id = id;
89  } else {
90  if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
91  return ret;
93  }
94  } else {
95  value = av_malloc(size+1);
96  if (!value)
97  return AVERROR(ENOMEM);
98  c = avio_read(pb, value, size);
99  if (c < 0) {
100  av_free(value);
101  return c;
102  }
103  value[c] = 0;
105  }
106  return 0;
107 }
108 
110 {
111  AVIOContext *pb = s->pb;
112  int64_t file_size = avio_size(pb);
113  uint32_t val, fields, tag_bytes;
114  uint8_t buf[8];
115  int64_t tag_start;
116  int i;
117 
118  if (file_size < APE_TAG_FOOTER_BYTES)
119  return 0;
120 
121  avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
122 
123  avio_read(pb, buf, 8); /* APETAGEX */
124  if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
125  return 0;
126  }
127 
128  val = avio_rl32(pb); /* APE tag version */
129  if (val > APE_TAG_VERSION) {
130  av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
131  return 0;
132  }
133 
134  tag_bytes = avio_rl32(pb); /* tag size */
135  if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
136  av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
137  return 0;
138  }
139 
140  if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
141  av_log(s, AV_LOG_ERROR, "Invalid tag size %"PRIu32".\n", tag_bytes);
142  return 0;
143  }
144 
145  fields = avio_rl32(pb); /* number of fields */
146  if (fields > 65536) {
147  av_log(s, AV_LOG_ERROR, "Too many tag fields (%"PRIu32")\n", fields);
148  return 0;
149  }
150 
151  val = avio_rl32(pb); /* flags */
152  if (val & APE_TAG_FLAG_IS_HEADER) {
153  av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
154  return 0;
155  }
156 
157  avio_seek(pb, file_size - tag_bytes, SEEK_SET);
158 
160  tag_bytes += APE_TAG_HEADER_BYTES;
161 
162  tag_start = file_size - tag_bytes;
163 
164  for (i=0; i<fields; i++)
165  if (ape_tag_read_field(s) < 0) break;
166 
167  return tag_start;
168 }
169 
170 static int string_is_ascii(const uint8_t *str)
171 {
172  while (*str && *str >= 0x20 && *str <= 0x7e ) str++;
173  return !*str;
174 }
175 
177 {
178  AVDictionaryEntry *e = NULL;
179  int size, ret, count = 0;
180  AVIOContext *dyn_bc;
181  uint8_t *dyn_buf;
182 
183  if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
184  return ret;
185 
187  while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
188  int val_len;
189 
190  if (!string_is_ascii(e->key)) {
191  av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n");
192  continue;
193  }
194 
195  val_len = strlen(e->value);
196  avio_wl32(dyn_bc, val_len); // value length
197  avio_wl32(dyn_bc, 0); // item flags
198  avio_put_str(dyn_bc, e->key); // key
199  avio_write(dyn_bc, e->value, val_len); // value
200  count++;
201  }
202  if (!count)
203  goto end;
204 
205  size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
206  if (size <= 0)
207  goto end;
209 
210  // header
211  avio_write(s->pb, "APETAGEX", 8); // id
212  avio_wl32(s->pb, APE_TAG_VERSION); // version
213  avio_wl32(s->pb, size);
214  avio_wl32(s->pb, count);
215 
216  // flags
218  ffio_fill(s->pb, 0, 8); // reserved
219 
220  avio_write(s->pb, dyn_buf, size - APE_TAG_FOOTER_BYTES);
221 
222  // footer
223  avio_write(s->pb, "APETAGEX", 8); // id
224  avio_wl32(s->pb, APE_TAG_VERSION); // version
225  avio_wl32(s->pb, size); // size
226  avio_wl32(s->pb, count); // tag count
227 
228  // flags
230  ffio_fill(s->pb, 0, 8); // reserved
231 
232 end:
233  ffio_free_dyn_buf(&dyn_bc);
234 
235  return ret;
236 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:237
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:57
apetag.h
ff_guess_image2_codec
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:113
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:68
APE_TAG_FLAG_IS_BINARY
#define APE_TAG_FLAG_IS_BINARY
Definition: apetag.c:36
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:352
avio_get_dyn_buf
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1495
APE_TAG_PREAMBLE
#define APE_TAG_PREAMBLE
Definition: apetag.h:28
ff_ape_parse_tag
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:109
APE_TAG_VERSION
#define APE_TAG_VERSION
Definition: apetag.h:29
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
ff_get_extradata
int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: demux_utils.c:355
string_is_ascii
static int string_is_ascii(const uint8_t *str)
Definition: apetag.c:170
ape_tag_read_field
static int ape_tag_read_field(AVFormatContext *s)
Definition: apetag.c:38
val
static double val(void *priv, double ch)
Definition: aeval.c:77
APE_TAG_FLAG_CONTAINS_HEADER
#define APE_TAG_FLAG_CONTAINS_HEADER
Definition: apetag.c:33
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:72
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1483
ff_add_attached_pic
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
Definition: demux_utils.c:116
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVDictionaryEntry::key
char * key
Definition: dict.h:80
key
const char * key
Definition: hwcontext_opencl.c:174
ff_ape_write_tag
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:176
fields
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the then the processing requires a frame on this link and the filter is expected to make efforts in that direction The status of input links is stored by the fifo and status_out fields
Definition: filter_design.txt:155
APE_TAG_FLAG_IS_HEADER
#define APE_TAG_FLAG_IS_HEADER
Definition: apetag.c:35
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
NULL
#define NULL
Definition: coverity.c:32
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:1019
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:218
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:324
ff_standardize_creation_time
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: mux_utils.c:169
avio_get_str
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:895
size
int size
Definition: twinvq_data.h:10344
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:386
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
avio_internal.h
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVMEDIA_TYPE_ATTACHMENT
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition: avutil.h:205
demux.h
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1556
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
APE_TAG_HEADER_BYTES
#define APE_TAG_HEADER_BYTES
Definition: apetag.h:31
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
avformat.h
dict.h
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:641
APE_TAG_FOOTER_BYTES
#define APE_TAG_FOOTER_BYTES
Definition: apetag.h:30
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVDictionaryEntry
Definition: dict.h:79
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
av_dict_set
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
convert_header.str
string str
Definition: convert_header.py:20
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVDictionaryEntry::value
char * value
Definition: dict.h:81
avio_put_str
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:402
mux.h