Libav
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
libavformat
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 "
avio_internal.h
"
27
#include "
apetag.h
"
28
#include "
internal.h
"
29
30
#define APE_TAG_VERSION 2000
31
#define APE_TAG_FOOTER_BYTES 32
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
37
static
int
ape_tag_read_field
(
AVFormatContext
*s)
38
{
39
AVIOContext
*pb = s->
pb
;
40
uint8_t
key[1024], *value;
41
uint32_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 >= UINT_MAX)
59
return
-1;
60
if
(flags &
APE_TAG_FLAG_IS_BINARY
) {
61
uint8_t
filename[1024];
62
enum
AVCodecID
id
;
63
AVStream
*st =
avformat_new_stream
(s,
NULL
);
64
if
(!st)
65
return
AVERROR
(ENOMEM);
66
67
size -=
avio_get_str
(pb, size, filename,
sizeof
(filename));
68
if
(size <= 0) {
69
av_log
(s,
AV_LOG_WARNING
,
"Skipping binary tag '%s'.\n"
, key);
70
return
0;
71
}
72
73
av_dict_set
(&st->
metadata
, key, filename, 0);
74
75
if
((
id
=
ff_guess_image2_codec
(filename)) !=
AV_CODEC_ID_NONE
) {
76
AVPacket
pkt;
77
int
ret;
78
79
ret =
av_get_packet
(s->
pb
, &pkt, size);
80
if
(ret < 0) {
81
av_log
(s,
AV_LOG_ERROR
,
"Error reading cover art.\n"
);
82
return
ret;
83
}
84
85
st->
disposition
|=
AV_DISPOSITION_ATTACHED_PIC
;
86
st->
codec
->
codec_type
=
AVMEDIA_TYPE_VIDEO
;
87
st->
codec
->
codec_id
=
id
;
88
89
st->
attached_pic
= pkt;
90
st->
attached_pic
.
stream_index
= st->
index
;
91
st->
attached_pic
.
flags
|=
AV_PKT_FLAG_KEY
;
92
}
else
{
93
st->
codec
->
extradata
=
av_malloc
(size +
FF_INPUT_BUFFER_PADDING_SIZE
);
94
if
(!st->
codec
->
extradata
)
95
return
AVERROR
(ENOMEM);
96
if
(
avio_read
(pb, st->
codec
->
extradata
, size) != size) {
97
av_freep
(&st->
codec
->
extradata
);
98
return
AVERROR
(EIO);
99
}
100
st->
codec
->
extradata_size
=
size
;
101
st->
codec
->
codec_type
=
AVMEDIA_TYPE_ATTACHMENT
;
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
118
int64_t
ff_ape_parse_tag
(
AVFormatContext
*s)
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,
"APETAGEX"
, 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 %u.\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 (%d)\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
int
ff_ape_write_tag
(
AVFormatContext
*s)
176
{
177
AVDictionaryEntry
*e =
NULL
;
178
int64_t start, end;
179
int
size
, count = 0;
180
181
if
(!s->
pb
->
seekable
)
182
return
0;
183
184
start =
avio_tell
(s->
pb
);
185
186
// header
187
avio_write
(s->
pb
,
"APETAGEX"
, 8);
// id
188
avio_wl32
(s->
pb
,
APE_TAG_VERSION
);
// version
189
avio_wl32
(s->
pb
, 0);
// reserve space for size
190
avio_wl32
(s->
pb
, 0);
// reserve space for tag count
191
192
// flags
193
avio_wl32
(s->
pb
,
APE_TAG_FLAG_CONTAINS_HEADER
|
APE_TAG_FLAG_CONTAINS_FOOTER
|
194
APE_TAG_FLAG_IS_HEADER
);
195
ffio_fill
(s->
pb
, 0, 8);
// reserved
196
197
while
((e =
av_dict_get
(s->
metadata
,
""
, e,
AV_DICT_IGNORE_SUFFIX
))) {
198
int
val_len = strlen(e->
value
);
199
200
avio_wl32
(s->
pb
, val_len);
// value length
201
avio_wl32
(s->
pb
, 0);
// item flags
202
avio_put_str
(s->
pb
, e->
key
);
// key
203
avio_write
(s->
pb
, e->
value
, val_len);
// value
204
count++;
205
}
206
207
size =
avio_tell
(s->
pb
) - start;
208
209
// footer
210
avio_write
(s->
pb
,
"APETAGEX"
, 8);
// id
211
avio_wl32
(s->
pb
,
APE_TAG_VERSION
);
// version
212
avio_wl32
(s->
pb
, size);
// size
213
avio_wl32
(s->
pb
, count);
// tag count
214
215
// flags
216
avio_wl32
(s->
pb
,
APE_TAG_FLAG_CONTAINS_HEADER
|
APE_TAG_FLAG_CONTAINS_FOOTER
);
217
ffio_fill
(s->
pb
, 0, 8);
// reserved
218
219
// update values in the header
220
end =
avio_tell
(s->
pb
);
221
avio_seek
(s->
pb
, start + 12, SEEK_SET);
222
avio_wl32
(s->
pb
, size);
223
avio_wl32
(s->
pb
, count);
224
avio_seek
(s->
pb
, end, SEEK_SET);
225
226
return
0;
227
}
Generated on Sun Jun 1 2014 17:55:35 for Libav by
1.8.1.2