Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/intreadwrite.h"
26 #include "libavutil/dict.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "apetag.h"
30 #include "internal.h"
31 
32 #define APE_TAG_VERSION 2000
33 #define APE_TAG_FOOTER_BYTES 32
34 #define APE_TAG_HEADER_BYTES 32
35 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
36 #define APE_TAG_FLAG_LACKS_FOOTER (1 << 30)
37 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
38 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
39 
41 {
42  AVIOContext *pb = s->pb;
43  uint8_t key[1024], *value;
44  int64_t size, flags;
45  int i, c;
46 
47  size = avio_rl32(pb); /* field size */
48  flags = avio_rl32(pb); /* field flags */
49  for (i = 0; i < sizeof(key) - 1; i++) {
50  c = avio_r8(pb);
51  if (c < 0x20 || c > 0x7E)
52  break;
53  else
54  key[i] = c;
55  }
56  key[i] = 0;
57  if (c != 0) {
58  av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
59  return -1;
60  }
61  if (size > INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
62  av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
63  return AVERROR_INVALIDDATA;
64  }
65  if (flags & APE_TAG_FLAG_IS_BINARY) {
66  uint8_t filename[1024];
67  enum AVCodecID id;
69  if (!st)
70  return AVERROR(ENOMEM);
71 
72  size -= avio_get_str(pb, size, filename, sizeof(filename));
73  if (size <= 0) {
74  av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
75  return 0;
76  }
77 
78  av_dict_set(&st->metadata, key, filename, 0);
79 
80  if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
81  AVPacket pkt;
82  int ret;
83 
84  ret = av_get_packet(s->pb, &pkt, size);
85  if (ret < 0) {
86  av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
87  return ret;
88  }
89 
92  st->codecpar->codec_id = id;
93 
94  st->attached_pic = pkt;
97  } else {
99  if (!st->codecpar->extradata)
100  return AVERROR(ENOMEM);
101  if (avio_read(pb, st->codecpar->extradata, size) != size) {
102  av_freep(&st->codecpar->extradata);
103  return AVERROR(EIO);
104  }
107  }
108  } else {
109  value = av_malloc(size+1);
110  if (!value)
111  return AVERROR(ENOMEM);
112  c = avio_read(pb, value, size);
113  if (c < 0) {
114  av_free(value);
115  return c;
116  }
117  value[c] = 0;
118  av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
119  }
120  return 0;
121 }
122 
124 {
125  AVIOContext *pb = s->pb;
126  int64_t file_size = avio_size(pb);
127  uint32_t val, fields, tag_bytes;
128  uint8_t buf[8];
129  int64_t tag_start;
130  int i;
131 
132  if (file_size < APE_TAG_FOOTER_BYTES)
133  return 0;
134 
135  avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
136 
137  avio_read(pb, buf, 8); /* APETAGEX */
138  if (strncmp(buf, "APETAGEX", 8)) {
139  return 0;
140  }
141 
142  val = avio_rl32(pb); /* APE tag version */
143  if (val > APE_TAG_VERSION) {
144  av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
145  return 0;
146  }
147 
148  tag_bytes = avio_rl32(pb); /* tag size */
149  if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
150  av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
151  return 0;
152  }
153 
154  if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
155  av_log(s, AV_LOG_ERROR, "Invalid tag size %"PRIu32".\n", tag_bytes);
156  return 0;
157  }
158 
159  fields = avio_rl32(pb); /* number of fields */
160  if (fields > 65536) {
161  av_log(s, AV_LOG_ERROR, "Too many tag fields (%"PRIu32")\n", fields);
162  return 0;
163  }
164 
165  val = avio_rl32(pb); /* flags */
166  if (val & APE_TAG_FLAG_IS_HEADER) {
167  av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
168  return 0;
169  }
170 
171  avio_seek(pb, file_size - tag_bytes, SEEK_SET);
172 
174  tag_bytes += APE_TAG_HEADER_BYTES;
175 
176  tag_start = file_size - tag_bytes;
177 
178  for (i=0; i<fields; i++)
179  if (ape_tag_read_field(s) < 0) break;
180 
181  return tag_start;
182 }
183 
185 {
186  AVDictionaryEntry *e = NULL;
187  int64_t start, end;
188  int size, count = 0;
189 
190  if (!s->pb->seekable)
191  return 0;
192 
193  start = avio_tell(s->pb);
194 
195  // header
196  avio_write(s->pb, "APETAGEX", 8); // id
197  avio_wl32 (s->pb, APE_TAG_VERSION); // version
198  avio_wl32(s->pb, 0); // reserve space for size
199  avio_wl32(s->pb, 0); // reserve space for tag count
200 
201  // flags
203  ffio_fill(s->pb, 0, 8); // reserved
204 
205  while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
206  int val_len = strlen(e->value);
207 
208  avio_wl32(s->pb, val_len); // value length
209  avio_wl32(s->pb, 0); // item flags
210  avio_put_str(s->pb, e->key); // key
211  avio_write(s->pb, e->value, val_len); // value
212  count++;
213  }
214 
215  size = avio_tell(s->pb) - start;
216 
217  // footer
218  avio_write(s->pb, "APETAGEX", 8); // id
219  avio_wl32 (s->pb, APE_TAG_VERSION); // version
220  avio_wl32(s->pb, size); // size
221  avio_wl32(s->pb, count); // tag count
222 
223  // flags
225  ffio_fill(s->pb, 0, 8); // reserved
226 
227  // update values in the header
228  end = avio_tell(s->pb);
229  avio_seek(s->pb, start + 12, SEEK_SET);
230  avio_wl32(s->pb, size);
231  avio_wl32(s->pb, count);
232  avio_seek(s->pb, end, SEEK_SET);
233 
234  return 0;
235 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:104
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
int size
enum AVCodecID id
Definition: mxfenc.c:85
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int index
stream index in AVFormatContext
Definition: avformat.h:706
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
Public dictionary API.
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:316
uint8_t
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
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:38
static int flags
Definition: log.c:50
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#define AVERROR(e)
Definition: error.h:43
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:123
#define APE_TAG_HEADER_BYTES
Definition: apetag.c:34
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:207
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and children.
Definition: dict.h:64
#define APE_TAG_FLAG_CONTAINS_HEADER
Definition: apetag.c:35
#define APE_TAG_FLAG_IS_BINARY
Definition: apetag.c:38
AVDictionary * metadata
Definition: avformat.h:772
Opaque data information usually sparse.
Definition: avutil.h:198
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:694
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:332
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
#define APE_TAG_FOOTER_BYTES
Definition: apetag.c:33
AVIOContext * pb
I/O context.
Definition: avformat.h:982
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:68
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:184
#define APE_TAG_FLAG_IS_HEADER
Definition: apetag.c:37
Main libavformat public API header.
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:761
char * key
Definition: dict.h:73
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:101
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
char * value
Definition: dict.h:74
static int ape_tag_read_field(AVFormatContext *s)
Definition: apetag.c:40
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:60
AVCodecParameters * codecpar
Definition: avformat.h:831
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:719
int stream_index
Definition: avcodec.h:1348
#define APE_TAG_VERSION
Definition: apetag.c:32
This structure stores compressed data.
Definition: avcodec.h:1323
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:790