wav: remove a use of deprecated AV_METADATA_ macro
[libav.git] / libavformat / cafdec.c
CommitLineData
5f14b565
PR
1/*
2 * Core Audio Format demuxer
3 * Copyright (c) 2007 Justin Ruggles
4 * Copyright (c) 2009 Peter Ross
5 *
2912e87a 6 * This file is part of Libav.
5f14b565 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
5f14b565
PR
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
5f14b565
PR
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
5f14b565
PR
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
ba87f080 24 * @file
5f14b565
PR
25 * Core Audio Format demuxer
26 */
27
28#include "avformat.h"
29#include "riff.h"
30#include "isom.h"
31#include "libavutil/intreadwrite.h"
ee8aecd2 32#include "libavutil/intfloat_readwrite.h"
d2d67e42 33#include "libavutil/dict.h"
5f14b565
PR
34#include "caf.h"
35
36typedef struct {
37 int bytes_per_packet; ///< bytes in a packet, or 0 if variable
38 int frames_per_packet; ///< frames in a packet, or 0 if variable
39 int64_t num_bytes; ///< total number of bytes in stream
40
41 int64_t packet_cnt; ///< packet counter
42 int64_t frame_cnt; ///< frame counter
43
44 int64_t data_start; ///< data start position, in bytes
45 int64_t data_size; ///< raw data size, in bytes
46} CaffContext;
47
48static int probe(AVProbeData *p)
49{
50 if (AV_RB32(p->buf) == MKBETAG('c','a','f','f') && AV_RB16(&p->buf[4]) == 1)
51 return AVPROBE_SCORE_MAX;
52 return 0;
53}
54
55/** Read audio description chunk */
56static int read_desc_chunk(AVFormatContext *s)
57{
ae628ec1 58 AVIOContext *pb = s->pb;
5f14b565
PR
59 CaffContext *caf = s->priv_data;
60 AVStream *st;
61 int flags;
62
63 /* new audio stream */
64 st = av_new_stream(s, 0);
65 if (!st)
2874c81c 66 return AVERROR(ENOMEM);
5f14b565
PR
67
68 /* parse format description */
72415b2a 69 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
b7effd4e
AK
70 st->codec->sample_rate = av_int2dbl(avio_rb64(pb));
71 st->codec->codec_tag = avio_rb32(pb);
72 flags = avio_rb32(pb);
73 caf->bytes_per_packet = avio_rb32(pb);
5f14b565 74 st->codec->block_align = caf->bytes_per_packet;
b7effd4e
AK
75 caf->frames_per_packet = avio_rb32(pb);
76 st->codec->channels = avio_rb32(pb);
77 st->codec->bits_per_coded_sample = avio_rb32(pb);
5f14b565
PR
78
79 /* calculate bit rate for constant size packets */
80 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
81 st->codec->bit_rate = (uint64_t)st->codec->sample_rate * (uint64_t)caf->bytes_per_packet * 8
82 / (uint64_t)caf->frames_per_packet;
83 } else {
84 st->codec->bit_rate = 0;
85 }
86
87 /* determine codec */
88 if (st->codec->codec_tag == MKBETAG('l','p','c','m'))
89 st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, (flags ^ 0x2) | 0x4);
90 else
91 st->codec->codec_id = ff_codec_get_id(ff_codec_caf_tags, st->codec->codec_tag);
92 return 0;
93}
94
95/** Read magic cookie chunk */
96static int read_kuki_chunk(AVFormatContext *s, int64_t size)
97{
ae628ec1 98 AVIOContext *pb = s->pb;
5f14b565
PR
99 AVStream *st = s->streams[0];
100
101 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
102 return -1;
103
104 if (st->codec->codec_id == CODEC_ID_AAC) {
105 /* The magic cookie format for AAC is an mp4 esds atom.
106 The lavc AAC decoder requires the data from the codec specific
107 description as extradata input. */
108 int strt, skip;
109 MOVAtom atom;
110
a2704c97 111 strt = avio_tell(pb);
5f14b565 112 ff_mov_read_esds(s, pb, atom);
a2704c97 113 skip = size - (avio_tell(pb) - strt);
5f14b565
PR
114 if (skip < 0 || !st->codec->extradata ||
115 st->codec->codec_id != CODEC_ID_AAC) {
116 av_log(s, AV_LOG_ERROR, "invalid AAC magic cookie\n");
117 return AVERROR_INVALIDDATA;
118 }
45a8a02a 119 avio_skip(pb, skip);
1e6f8d53
PR
120 } else if (st->codec->codec_id == CODEC_ID_ALAC) {
121#define ALAC_PREAMBLE 12
122#define ALAC_HEADER 36
123 if (size < ALAC_PREAMBLE + ALAC_HEADER) {
124 av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n");
45a8a02a 125 avio_skip(pb, size);
1e6f8d53
PR
126 return AVERROR_INVALIDDATA;
127 }
45a8a02a 128 avio_skip(pb, ALAC_PREAMBLE);
1e6f8d53
PR
129 st->codec->extradata = av_mallocz(ALAC_HEADER + FF_INPUT_BUFFER_PADDING_SIZE);
130 if (!st->codec->extradata)
131 return AVERROR(ENOMEM);
b7effd4e 132 avio_read(pb, st->codec->extradata, ALAC_HEADER);
1e6f8d53 133 st->codec->extradata_size = ALAC_HEADER;
45a8a02a 134 avio_skip(pb, size - ALAC_PREAMBLE - ALAC_HEADER);
5f14b565
PR
135 } else {
136 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
137 if (!st->codec->extradata)
138 return AVERROR(ENOMEM);
b7effd4e 139 avio_read(pb, st->codec->extradata, size);
5f14b565
PR
140 st->codec->extradata_size = size;
141 }
142
143 return 0;
144}
145
146/** Read packet table chunk */
147static int read_pakt_chunk(AVFormatContext *s, int64_t size)
148{
ae628ec1 149 AVIOContext *pb = s->pb;
5f14b565
PR
150 AVStream *st = s->streams[0];
151 CaffContext *caf = s->priv_data;
152 int64_t pos = 0, ccount;
153 int num_packets, i;
154
a2704c97 155 ccount = avio_tell(pb);
5f14b565 156
b7effd4e 157 num_packets = avio_rb64(pb);
5f14b565
PR
158 if (num_packets < 0 || INT32_MAX / sizeof(AVIndexEntry) < num_packets)
159 return AVERROR_INVALIDDATA;
160
b7effd4e
AK
161 st->nb_frames = avio_rb64(pb); /* valid frames */
162 st->nb_frames += avio_rb32(pb); /* priming frames */
163 st->nb_frames += avio_rb32(pb); /* remainder frames */
5f14b565
PR
164
165 st->duration = 0;
166 for (i = 0; i < num_packets; i++) {
167 av_add_index_entry(s->streams[0], pos, st->duration, 0, 0, AVINDEX_KEYFRAME);
168 pos += caf->bytes_per_packet ? caf->bytes_per_packet : ff_mp4_read_descr_len(pb);
169 st->duration += caf->frames_per_packet ? caf->frames_per_packet : ff_mp4_read_descr_len(pb);
170 }
171
a2704c97 172 if (avio_tell(pb) - ccount != size) {
5f14b565
PR
173 av_log(s, AV_LOG_ERROR, "error reading packet table\n");
174 return -1;
175 }
176
177 caf->num_bytes = pos;
178 return 0;
179}
180
181/** Read information chunk */
182static void read_info_chunk(AVFormatContext *s, int64_t size)
183{
ae628ec1 184 AVIOContext *pb = s->pb;
5f14b565 185 unsigned int i;
b7effd4e 186 unsigned int nb_entries = avio_rb32(pb);
5f14b565
PR
187 for (i = 0; i < nb_entries; i++) {
188 char key[32];
189 char value[1024];
e16ead07
AK
190 avio_get_str(pb, INT_MAX, key, sizeof(key));
191 avio_get_str(pb, INT_MAX, value, sizeof(value));
d2d67e42 192 av_dict_set(&s->metadata, key, value, 0);
5f14b565
PR
193 }
194}
195
196static int read_header(AVFormatContext *s,
197 AVFormatParameters *ap)
198{
ae628ec1 199 AVIOContext *pb = s->pb;
5f14b565
PR
200 CaffContext *caf = s->priv_data;
201 AVStream *st;
202 uint32_t tag = 0;
203 int found_data, ret;
204 int64_t size;
205
45a8a02a 206 avio_skip(pb, 8); /* magic, version, file flags */
5f14b565
PR
207
208 /* audio description chunk */
b7effd4e 209 if (avio_rb32(pb) != MKBETAG('d','e','s','c')) {
5f14b565
PR
210 av_log(s, AV_LOG_ERROR, "desc chunk not present\n");
211 return AVERROR_INVALIDDATA;
212 }
b7effd4e 213 size = avio_rb64(pb);
5f14b565
PR
214 if (size != 32)
215 return AVERROR_INVALIDDATA;
216
217 ret = read_desc_chunk(s);
218 if (ret)
219 return ret;
220 st = s->streams[0];
221
222 /* parse each chunk */
223 found_data = 0;
66e5b1df 224 while (!pb->eof_reached) {
5f14b565
PR
225
226 /* stop at data chunk if seeking is not supported or
227 data chunk size is unknown */
8978feda 228 if (found_data && (caf->data_size < 0 || !pb->seekable))
5f14b565
PR
229 break;
230
b7effd4e
AK
231 tag = avio_rb32(pb);
232 size = avio_rb64(pb);
66e5b1df 233 if (pb->eof_reached)
5f14b565
PR
234 break;
235
236 switch (tag) {
237 case MKBETAG('d','a','t','a'):
45a8a02a 238 avio_skip(pb, 4); /* edit count */
a2704c97 239 caf->data_start = avio_tell(pb);
5f14b565 240 caf->data_size = size < 0 ? -1 : size - 4;
8978feda 241 if (caf->data_size > 0 && pb->seekable)
45a8a02a 242 avio_skip(pb, caf->data_size);
5f14b565
PR
243 found_data = 1;
244 break;
245
246 /* magic cookie chunk */
247 case MKBETAG('k','u','k','i'):
248 if (read_kuki_chunk(s, size))
249 return AVERROR_INVALIDDATA;
250 break;
251
252 /* packet table chunk */
253 case MKBETAG('p','a','k','t'):
254 if (read_pakt_chunk(s, size))
255 return AVERROR_INVALIDDATA;
256 break;
257
258 case MKBETAG('i','n','f','o'):
259 read_info_chunk(s, size);
260 break;
261
262 default:
263#define _(x) ((x) >= ' ' ? (x) : ' ')
264 av_log(s, AV_LOG_WARNING, "skipping CAF chunk: %08X (%c%c%c%c)\n",
265 tag, _(tag>>24), _((tag>>16)&0xFF), _((tag>>8)&0xFF), _(tag&0xFF));
266#undef _
267 case MKBETAG('f','r','e','e'):
268 if (size < 0)
269 return AVERROR_INVALIDDATA;
45a8a02a 270 avio_skip(pb, size);
5f14b565
PR
271 break;
272 }
273 }
274
275 if (!found_data)
276 return AVERROR_INVALIDDATA;
277
278 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
279 if (caf->data_size > 0)
280 st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
281 } else if (st->nb_index_entries) {
282 st->codec->bit_rate = st->codec->sample_rate * caf->data_size * 8 /
283 st->duration;
284 } else {
285 av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when "
286 "block size or frame size are variable.\n");
287 return AVERROR_INVALIDDATA;
288 }
76aa876e 289 s->file_size = avio_size(pb);
5f14b565
PR
290 s->file_size = FFMAX(0, s->file_size);
291
292 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
293 st->start_time = 0;
294
295 /* position the stream at the start of data */
296 if (caf->data_size >= 0)
6b4aa5da 297 avio_seek(pb, caf->data_start, SEEK_SET);
5f14b565
PR
298
299 return 0;
300}
301
302#define CAF_MAX_PKT_SIZE 4096
303
304static int read_packet(AVFormatContext *s, AVPacket *pkt)
305{
ae628ec1 306 AVIOContext *pb = s->pb;
5f14b565
PR
307 AVStream *st = s->streams[0];
308 CaffContext *caf = s->priv_data;
309 int res, pkt_size = 0, pkt_frames = 0;
310 int64_t left = CAF_MAX_PKT_SIZE;
311
66e5b1df 312 if (pb->eof_reached)
5ae092ee 313 return AVERROR(EIO);
5f14b565
PR
314
315 /* don't read past end of data chunk */
316 if (caf->data_size > 0) {
a2704c97 317 left = (caf->data_start + caf->data_size) - avio_tell(pb);
5f14b565 318 if (left <= 0)
5ae092ee 319 return AVERROR(EIO);
5f14b565
PR
320 }
321
322 pkt_frames = caf->frames_per_packet;
323 pkt_size = caf->bytes_per_packet;
324
325 if (pkt_size > 0 && pkt_frames == 1) {
326 pkt_size = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
327 pkt_size = FFMIN(pkt_size, left);
328 pkt_frames = pkt_size / caf->bytes_per_packet;
329 } else if (st->nb_index_entries) {
330 if (caf->packet_cnt < st->nb_index_entries - 1) {
331 pkt_size = st->index_entries[caf->packet_cnt + 1].pos - st->index_entries[caf->packet_cnt].pos;
332 pkt_frames = st->index_entries[caf->packet_cnt + 1].timestamp - st->index_entries[caf->packet_cnt].timestamp;
333 } else if (caf->packet_cnt == st->nb_index_entries - 1) {
334 pkt_size = caf->num_bytes - st->index_entries[caf->packet_cnt].pos;
335 pkt_frames = st->duration - st->index_entries[caf->packet_cnt].timestamp;
336 } else {
5ae092ee 337 return AVERROR(EIO);
5f14b565
PR
338 }
339 }
340
341 if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
5ae092ee 342 return AVERROR(EIO);
5f14b565
PR
343
344 res = av_get_packet(pb, pkt, pkt_size);
345 if (res < 0)
346 return res;
347
348 pkt->size = res;
349 pkt->stream_index = 0;
350 pkt->dts = pkt->pts = caf->frame_cnt;
351
352 caf->packet_cnt++;
353 caf->frame_cnt += pkt_frames;
354
355 return 0;
356}
357
358static int read_seek(AVFormatContext *s, int stream_index,
359 int64_t timestamp, int flags)
360{
361 AVStream *st = s->streams[0];
362 CaffContext *caf = s->priv_data;
363 int64_t pos;
364
365 timestamp = FFMAX(timestamp, 0);
366
367 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
368 /* calculate new byte position based on target frame position */
369 pos = caf->bytes_per_packet * timestamp / caf->frames_per_packet;
370 if (caf->data_size > 0)
371 pos = FFMIN(pos, caf->data_size);
372 caf->packet_cnt = pos / caf->bytes_per_packet;
373 caf->frame_cnt = caf->frames_per_packet * caf->packet_cnt;
374 } else if (st->nb_index_entries) {
375 caf->packet_cnt = av_index_search_timestamp(st, timestamp, flags);
376 caf->frame_cnt = st->index_entries[caf->packet_cnt].timestamp;
377 pos = st->index_entries[caf->packet_cnt].pos;
378 } else {
379 return -1;
380 }
381
6b4aa5da 382 avio_seek(s->pb, pos + caf->data_start, SEEK_SET);
5f14b565
PR
383 return 0;
384}
385
c6610a21 386AVInputFormat ff_caf_demuxer = {
5f14b565
PR
387 "caf",
388 NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
389 sizeof(CaffContext),
390 probe,
391 read_header,
392 read_packet,
393 NULL,
394 read_seek,
395 .codec_tag = (const AVCodecTag*[]){ff_codec_caf_tags, 0},
396};