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 "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "avformat.h"
26 #include "apetag.h"
27 #include "internal.h"
28 
29 #define APE_TAG_VERSION 2000
30 #define APE_TAG_FOOTER_BYTES 32
31 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
32 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
33 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
34 
36 {
37  AVIOContext *pb = s->pb;
38  uint8_t key[1024], *value;
39  uint32_t size, flags;
40  int i, c;
41 
42  size = avio_rl32(pb); /* field size */
43  flags = avio_rl32(pb); /* field flags */
44  for (i = 0; i < sizeof(key) - 1; i++) {
45  c = avio_r8(pb);
46  if (c < 0x20 || c > 0x7E)
47  break;
48  else
49  key[i] = c;
50  }
51  key[i] = 0;
52  if (c != 0) {
53  av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
54  return -1;
55  }
56  if (size >= UINT_MAX)
57  return -1;
58  if (flags & APE_TAG_FLAG_IS_BINARY) {
59  uint8_t filename[1024];
60  enum AVCodecID id;
62  if (!st)
63  return AVERROR(ENOMEM);
64 
65  size -= avio_get_str(pb, size, filename, sizeof(filename));
66  if (size <= 0) {
67  av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
68  return 0;
69  }
70 
71  av_dict_set(&st->metadata, key, filename, 0);
72 
73  if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
74  AVPacket pkt;
75  int ret;
76 
77  ret = av_get_packet(s->pb, &pkt, size);
78  if (ret < 0) {
79  av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
80  return ret;
81  }
82 
85  st->codec->codec_id = id;
86 
87  st->attached_pic = pkt;
90  } else {
92  if (!st->codec->extradata)
93  return AVERROR(ENOMEM);
94  if (avio_read(pb, st->codec->extradata, size) != size) {
95  av_freep(&st->codec->extradata);
96  return AVERROR(EIO);
97  }
98  st->codec->extradata_size = size;
100  }
101  } else {
102  value = av_malloc(size+1);
103  if (!value)
104  return AVERROR(ENOMEM);
105  c = avio_read(pb, value, size);
106  if (c < 0) {
107  av_free(value);
108  return c;
109  }
110  value[c] = 0;
111  av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
112  }
113  return 0;
114 }
115 
117 {
118  AVIOContext *pb = s->pb;
119  int64_t file_size = avio_size(pb);
120  uint32_t val, fields, tag_bytes;
121  uint8_t buf[8];
122  int64_t tag_start;
123  int i;
124 
125  if (file_size < APE_TAG_FOOTER_BYTES)
126  return 0;
127 
128  avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
129 
130  avio_read(pb, buf, 8); /* APETAGEX */
131  if (strncmp(buf, "APETAGEX", 8)) {
132  return 0;
133  }
134 
135  val = avio_rl32(pb); /* APE tag version */
136  if (val > APE_TAG_VERSION) {
137  av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
138  return 0;
139  }
140 
141  tag_bytes = avio_rl32(pb); /* tag size */
142  if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
143  av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
144  return 0;
145  }
146 
147  if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
148  av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes);
149  return 0;
150  }
151  tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
152 
153  fields = avio_rl32(pb); /* number of fields */
154  if (fields > 65536) {
155  av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
156  return 0;
157  }
158 
159  val = avio_rl32(pb); /* flags */
160  if (val & APE_TAG_FLAG_IS_HEADER) {
161  av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
162  return 0;
163  }
164 
165  avio_seek(pb, file_size - tag_bytes, SEEK_SET);
166 
167  for (i=0; i<fields; i++)
168  if (ape_tag_read_field(s) < 0) break;
169 
170  return tag_start;
171 }
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:61
Bytestream IO Context.
Definition: avio.h:68
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:241
int size
enum AVCodecID id
Definition: mxfenc.c:85
int index
stream index in AVFormatContext
Definition: avformat.h:623
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
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:151
Format I/O context.
Definition: avformat.h:828
Public dictionary API.
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
static int flags
Definition: log.c:42
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:218
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:446
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:95
AVDictionary * metadata
Definition: avformat.h:972
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:546
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2736
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:116
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
#define APE_TAG_FLAG_IS_BINARY
Definition: apetag.c:33
AVDictionary * metadata
Definition: avformat.h:699
Opaque data information usually sparse.
Definition: avutil.h:183
Stream structure.
Definition: avformat.h:622
NULL
Definition: eval.c:52
enum AVMediaType codec_type
Definition: avcodec.h:1347
#define APE_TAG_FOOTER_BYTES
Definition: apetag.c:30
enum AVCodecID codec_id
Definition: avcodec.h:1350
AVIOContext * pb
I/O context.
Definition: avformat.h:861
int extradata_size
Definition: avcodec.h:1455
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:63
#define APE_TAG_FLAG_IS_HEADER
Definition: apetag.c:32
Main libavformat public API header.
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:688
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:88
static int ape_tag_read_field(AVFormatContext *s)
Definition: apetag.c:35
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:600
int stream_index
Definition: avcodec.h:917
#define APE_TAG_VERSION
Definition: apetag.c:29
This structure stores compressed data.
Definition: avcodec.h:898
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:713