FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/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_FLAG_CONTAINS_HEADER (1 << 31)
33 #define APE_TAG_FLAG_CONTAINS_FOOTER (1 << 30)
34 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
35 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
36 
38 {
39  AVIOContext *pb = s->pb;
40  uint8_t key[1024], *value;
41  int64_t size, flags;
42  int i, c;
43 
44  size = avio_rl32(pb); /* field size */
45  flags = avio_rl32(pb); /* field flags */
46  for (i = 0; i < sizeof(key) - 1; i++) {
47  c = avio_r8(pb);
48  if (c < 0x20 || c > 0x7E)
49  break;
50  else
51  key[i] = c;
52  }
53  key[i] = 0;
54  if (c != 0) {
55  av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
56  return -1;
57  }
58  if (size > INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
59  av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
60  return AVERROR_INVALIDDATA;
61  }
62  if (flags & APE_TAG_FLAG_IS_BINARY) {
63  uint8_t filename[1024];
64  enum AVCodecID id;
65  int ret;
67  if (!st)
68  return AVERROR(ENOMEM);
69 
70  ret = avio_get_str(pb, size, filename, sizeof(filename));
71  if (ret < 0)
72  return ret;
73  if (size <= ret) {
74  av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
75  return 0;
76  }
77  size -= ret;
78 
79  av_dict_set(&st->metadata, key, filename, 0);
80 
81  if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
82  AVPacket pkt;
83  int ret;
84 
85  ret = av_get_packet(s->pb, &pkt, size);
86  if (ret < 0) {
87  av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
88  return ret;
89  }
90 
93  st->codecpar->codec_id = id;
94 
95  st->attached_pic = pkt;
98  } else {
99  if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
100  return AVERROR(ENOMEM);
102  }
103  } else {
104  value = av_malloc(size+1);
105  if (!value)
106  return AVERROR(ENOMEM);
107  c = avio_read(pb, value, size);
108  if (c < 0) {
109  av_free(value);
110  return c;
111  }
112  value[c] = 0;
113  av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
114  }
115  return 0;
116 }
117 
119 {
120  AVIOContext *pb = s->pb;
121  int64_t file_size = avio_size(pb);
122  uint32_t val, fields, tag_bytes;
123  uint8_t buf[8];
124  int64_t tag_start;
125  int i;
126 
127  if (file_size < APE_TAG_FOOTER_BYTES)
128  return 0;
129 
130  avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
131 
132  avio_read(pb, buf, 8); /* APETAGEX */
133  if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
134  return 0;
135  }
136 
137  val = avio_rl32(pb); /* APE tag version */
138  if (val > APE_TAG_VERSION) {
139  av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
140  return 0;
141  }
142 
143  tag_bytes = avio_rl32(pb); /* tag size */
144  if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
145  av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
146  return 0;
147  }
148 
149  if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
150  av_log(s, AV_LOG_ERROR, "Invalid tag size %"PRIu32".\n", tag_bytes);
151  return 0;
152  }
153  tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
154 
155  fields = avio_rl32(pb); /* number of fields */
156  if (fields > 65536) {
157  av_log(s, AV_LOG_ERROR, "Too many tag fields (%"PRIu32")\n", fields);
158  return 0;
159  }
160 
161  val = avio_rl32(pb); /* flags */
162  if (val & APE_TAG_FLAG_IS_HEADER) {
163  av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
164  return 0;
165  }
166 
167  avio_seek(pb, file_size - tag_bytes, SEEK_SET);
168 
169  for (i=0; i<fields; i++)
170  if (ape_tag_read_field(s) < 0) break;
171 
172  return tag_start;
173 }
174 
175 static int string_is_ascii(const uint8_t *str)
176 {
177  while (*str && *str >= 0x20 && *str <= 0x7e ) str++;
178  return !*str;
179 }
180 
182 {
183  AVDictionaryEntry *e = NULL;
184  int size, ret, count = 0;
185  AVIOContext *dyn_bc = NULL;
186  uint8_t *dyn_buf = NULL;
187 
188  if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
189  goto end;
190 
191  // flags
194  ffio_fill(dyn_bc, 0, 8); // reserved
195 
197  while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
198  int val_len;
199 
200  if (!string_is_ascii(e->key)) {
201  av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n");
202  continue;
203  }
204 
205  val_len = strlen(e->value);
206  avio_wl32(dyn_bc, val_len); // value length
207  avio_wl32(dyn_bc, 0); // item flags
208  avio_put_str(dyn_bc, e->key); // key
209  avio_write(dyn_bc, e->value, val_len); // value
210  count++;
211  }
212  if (!count)
213  goto end;
214 
215  size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
216  if (size <= 0)
217  goto end;
218  size += 20;
219 
220  // header
221  avio_write(s->pb, "APETAGEX", 8); // id
222  avio_wl32(s->pb, APE_TAG_VERSION); // version
223  avio_wl32(s->pb, size);
224  avio_wl32(s->pb, count);
225 
226  avio_write(s->pb, dyn_buf, size - 20);
227 
228  // footer
229  avio_write(s->pb, "APETAGEX", 8); // id
230  avio_wl32(s->pb, APE_TAG_VERSION); // version
231  avio_wl32(s->pb, size); // size
232  avio_wl32(s->pb, count); // tag count
233 
234  // flags
236  ffio_fill(s->pb, 0, 8); // reserved
237 
238 end:
239  if (dyn_bc && !dyn_buf)
240  avio_close_dyn_buf(dyn_bc, &dyn_buf);
241  av_freep(&dyn_buf);
242 
243  return ret;
244 }
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:309
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1272
enum AVCodecID id
Definition: mxfenc.c:104
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define APE_TAG_FLAG_CONTAINS_FOOTER
Definition: apetag.c:33
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3939
int index
stream index in AVFormatContext
Definition: avformat.h:877
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
static AVPacket pkt
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1260
Format I/O context.
Definition: avformat.h:1325
Public dictionary API.
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:346
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4131
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:39
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:260
ptrdiff_t size
Definition: opengl_enc.c:101
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:598
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1620
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:189
#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:1539
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:720
#define AVERROR(e)
Definition: error.h:43
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3935
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: utils.c:5206
GLsizei count
Definition: opengl_enc.c:109
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:118
#define APE_TAG_PREAMBLE
Definition: apetag.h:28
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1594
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:589
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:190
static int string_is_ascii(const uint8_t *str)
Definition: apetag.c:175
#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:76
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define APE_TAG_FLAG_CONTAINS_HEADER
Definition: apetag.c:32
#define APE_TAG_VERSION
Definition: apetag.h:29
#define APE_TAG_FLAG_IS_BINARY
Definition: apetag.c:35
AVDictionary * metadata
Definition: avformat.h:945
Opaque data information usually sparse.
Definition: avutil.h:197
#define APE_TAG_FOOTER_BYTES
Definition: apetag.h:30
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:851
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:362
Stream structure.
Definition: avformat.h:876
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
void * buf
Definition: avisynth_c.h:553
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:69
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:181
static int flags
Definition: cpu.c:47
#define APE_TAG_FLAG_IS_HEADER
Definition: apetag.c:34
Main libavformat public API header.
static double c[64]
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:934
int ff_get_extradata(AVFormatContext *s, 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: utils.c:3137
char * key
Definition: dict.h:86
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:99
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:731
#define av_free(p)
char * value
Definition: dict.h:87
static int ape_tag_read_field(AVFormatContext *s)
Definition: apetag.c:37
#define av_freep(p)
#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
Definition: avformat.h:1228
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:776
int stream_index
Definition: avcodec.h:1590
This structure stores compressed data.
Definition: avcodec.h:1565
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:963