lavc: Consistently prefix input buffer defines
[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
d92024f1
DB
28#include <inttypes.h>
29
5f14b565 30#include "avformat.h"
c3f9ebf7 31#include "internal.h"
5f14b565 32#include "isom.h"
3bab7cd1 33#include "mov_chan.h"
5f14b565 34#include "libavutil/intreadwrite.h"
3383a53e 35#include "libavutil/intfloat.h"
d2d67e42 36#include "libavutil/dict.h"
5f14b565
PR
37#include "caf.h"
38
daf8cf35 39typedef struct CafContext {
5f14b565
PR
40 int bytes_per_packet; ///< bytes in a packet, or 0 if variable
41 int frames_per_packet; ///< frames in a packet, or 0 if variable
42 int64_t num_bytes; ///< total number of bytes in stream
43
44 int64_t packet_cnt; ///< packet counter
45 int64_t frame_cnt; ///< frame counter
46
47 int64_t data_start; ///< data start position, in bytes
48 int64_t data_size; ///< raw data size, in bytes
4d5b99da 49} CafContext;
5f14b565
PR
50
51static int probe(AVProbeData *p)
52{
53 if (AV_RB32(p->buf) == MKBETAG('c','a','f','f') && AV_RB16(&p->buf[4]) == 1)
54 return AVPROBE_SCORE_MAX;
55 return 0;
56}
57
58/** Read audio description chunk */
59static int read_desc_chunk(AVFormatContext *s)
60{
ae628ec1 61 AVIOContext *pb = s->pb;
4d5b99da 62 CafContext *caf = s->priv_data;
5f14b565
PR
63 AVStream *st;
64 int flags;
65
66 /* new audio stream */
3b3bbdd3 67 st = avformat_new_stream(s, NULL);
5f14b565 68 if (!st)
2874c81c 69 return AVERROR(ENOMEM);
5f14b565
PR
70
71 /* parse format description */
72415b2a 72 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
3383a53e 73 st->codec->sample_rate = av_int2double(avio_rb64(pb));
b7effd4e
AK
74 st->codec->codec_tag = avio_rb32(pb);
75 flags = avio_rb32(pb);
76 caf->bytes_per_packet = avio_rb32(pb);
5f14b565 77 st->codec->block_align = caf->bytes_per_packet;
b7effd4e
AK
78 caf->frames_per_packet = avio_rb32(pb);
79 st->codec->channels = avio_rb32(pb);
80 st->codec->bits_per_coded_sample = avio_rb32(pb);
5f14b565
PR
81
82 /* calculate bit rate for constant size packets */
83 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
84 st->codec->bit_rate = (uint64_t)st->codec->sample_rate * (uint64_t)caf->bytes_per_packet * 8
85 / (uint64_t)caf->frames_per_packet;
86 } else {
87 st->codec->bit_rate = 0;
88 }
89
90 /* determine codec */
91 if (st->codec->codec_tag == MKBETAG('l','p','c','m'))
92 st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, (flags ^ 0x2) | 0x4);
93 else
94 st->codec->codec_id = ff_codec_get_id(ff_codec_caf_tags, st->codec->codec_tag);
95 return 0;
96}
97
98/** Read magic cookie chunk */
99static int read_kuki_chunk(AVFormatContext *s, int64_t size)
100{
ae628ec1 101 AVIOContext *pb = s->pb;
5f14b565
PR
102 AVStream *st = s->streams[0];
103
059a9348 104 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
5f14b565
PR
105 return -1;
106
36ef5369 107 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
5f14b565
PR
108 /* The magic cookie format for AAC is an mp4 esds atom.
109 The lavc AAC decoder requires the data from the codec specific
110 description as extradata input. */
111 int strt, skip;
5f14b565 112
a2704c97 113 strt = avio_tell(pb);
86dfcfd0 114 ff_mov_read_esds(s, pb);
a2704c97 115 skip = size - (avio_tell(pb) - strt);
5f14b565 116 if (skip < 0 || !st->codec->extradata ||
36ef5369 117 st->codec->codec_id != AV_CODEC_ID_AAC) {
5f14b565
PR
118 av_log(s, AV_LOG_ERROR, "invalid AAC magic cookie\n");
119 return AVERROR_INVALIDDATA;
120 }
45a8a02a 121 avio_skip(pb, skip);
36ef5369 122 } else if (st->codec->codec_id == AV_CODEC_ID_ALAC) {
1e6f8d53
PR
123#define ALAC_PREAMBLE 12
124#define ALAC_HEADER 36
b0b77b9c
JR
125#define ALAC_NEW_KUKI 24
126 uint8_t preamble[12];
1749e12f 127 if (size < ALAC_NEW_KUKI) {
1e6f8d53 128 av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n");
45a8a02a 129 avio_skip(pb, size);
1e6f8d53
PR
130 return AVERROR_INVALIDDATA;
131 }
b0b77b9c
JR
132 avio_read(pb, preamble, ALAC_PREAMBLE);
133
059a9348 134 st->codec->extradata = av_mallocz(ALAC_HEADER + AV_INPUT_BUFFER_PADDING_SIZE);
1e6f8d53
PR
135 if (!st->codec->extradata)
136 return AVERROR(ENOMEM);
b0b77b9c
JR
137
138 /* For the old style cookie, we skip 12 bytes, then read 36 bytes.
139 * The new style cookie only contains the last 24 bytes of what was
140 * 36 bytes in the old style cookie, so we fabricate the first 12 bytes
141 * in that case to maintain compatibility. */
142 if (!memcmp(&preamble[4], "frmaalac", 8)) {
143 if (size < ALAC_PREAMBLE + ALAC_HEADER) {
144 av_log(s, AV_LOG_ERROR, "invalid ALAC magic cookie\n");
145 av_freep(&st->codec->extradata);
146 return AVERROR_INVALIDDATA;
147 }
148 avio_read(pb, st->codec->extradata, ALAC_HEADER);
149 avio_skip(pb, size - ALAC_PREAMBLE - ALAC_HEADER);
150 } else {
151 AV_WB32(st->codec->extradata, 36);
152 memcpy(&st->codec->extradata[4], "alac", 4);
153 AV_WB32(&st->codec->extradata[8], 0);
154 memcpy(&st->codec->extradata[12], preamble, 12);
155 avio_read(pb, &st->codec->extradata[24], ALAC_NEW_KUKI - 12);
156 avio_skip(pb, size - ALAC_NEW_KUKI);
157 }
1e6f8d53 158 st->codec->extradata_size = ALAC_HEADER;
5f14b565 159 } else {
059a9348 160 st->codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
5f14b565
PR
161 if (!st->codec->extradata)
162 return AVERROR(ENOMEM);
b7effd4e 163 avio_read(pb, st->codec->extradata, size);
5f14b565
PR
164 st->codec->extradata_size = size;
165 }
166
167 return 0;
168}
169
170/** Read packet table chunk */
171static int read_pakt_chunk(AVFormatContext *s, int64_t size)
172{
ae628ec1 173 AVIOContext *pb = s->pb;
5f14b565 174 AVStream *st = s->streams[0];
4d5b99da 175 CafContext *caf = s->priv_data;
c0196a14
JR
176 int64_t pos = 0, ccount, num_packets;
177 int i;
5f14b565 178
a2704c97 179 ccount = avio_tell(pb);
5f14b565 180
b7effd4e 181 num_packets = avio_rb64(pb);
5f14b565
PR
182 if (num_packets < 0 || INT32_MAX / sizeof(AVIndexEntry) < num_packets)
183 return AVERROR_INVALIDDATA;
184
b7effd4e
AK
185 st->nb_frames = avio_rb64(pb); /* valid frames */
186 st->nb_frames += avio_rb32(pb); /* priming frames */
187 st->nb_frames += avio_rb32(pb); /* remainder frames */
5f14b565
PR
188
189 st->duration = 0;
190 for (i = 0; i < num_packets; i++) {
191 av_add_index_entry(s->streams[0], pos, st->duration, 0, 0, AVINDEX_KEYFRAME);
192 pos += caf->bytes_per_packet ? caf->bytes_per_packet : ff_mp4_read_descr_len(pb);
193 st->duration += caf->frames_per_packet ? caf->frames_per_packet : ff_mp4_read_descr_len(pb);
194 }
195
f5a9c35f 196 if (avio_tell(pb) - ccount > size) {
5f14b565 197 av_log(s, AV_LOG_ERROR, "error reading packet table\n");
f5a9c35f 198 return AVERROR_INVALIDDATA;
5f14b565 199 }
f5a9c35f 200 avio_skip(pb, ccount + size - avio_tell(pb));
5f14b565
PR
201
202 caf->num_bytes = pos;
203 return 0;
204}
205
206/** Read information chunk */
207static void read_info_chunk(AVFormatContext *s, int64_t size)
208{
ae628ec1 209 AVIOContext *pb = s->pb;
5f14b565 210 unsigned int i;
b7effd4e 211 unsigned int nb_entries = avio_rb32(pb);
5f14b565
PR
212 for (i = 0; i < nb_entries; i++) {
213 char key[32];
214 char value[1024];
e16ead07
AK
215 avio_get_str(pb, INT_MAX, key, sizeof(key));
216 avio_get_str(pb, INT_MAX, value, sizeof(value));
d2d67e42 217 av_dict_set(&s->metadata, key, value, 0);
5f14b565
PR
218 }
219}
220
6e9651d1 221static int read_header(AVFormatContext *s)
5f14b565 222{
ae628ec1 223 AVIOContext *pb = s->pb;
4d5b99da 224 CafContext *caf = s->priv_data;
5f14b565
PR
225 AVStream *st;
226 uint32_t tag = 0;
227 int found_data, ret;
228 int64_t size;
229
45a8a02a 230 avio_skip(pb, 8); /* magic, version, file flags */
5f14b565
PR
231
232 /* audio description chunk */
b7effd4e 233 if (avio_rb32(pb) != MKBETAG('d','e','s','c')) {
5f14b565
PR
234 av_log(s, AV_LOG_ERROR, "desc chunk not present\n");
235 return AVERROR_INVALIDDATA;
236 }
b7effd4e 237 size = avio_rb64(pb);
5f14b565
PR
238 if (size != 32)
239 return AVERROR_INVALIDDATA;
240
241 ret = read_desc_chunk(s);
242 if (ret)
243 return ret;
244 st = s->streams[0];
245
246 /* parse each chunk */
247 found_data = 0;
66e5b1df 248 while (!pb->eof_reached) {
5f14b565
PR
249
250 /* stop at data chunk if seeking is not supported or
251 data chunk size is unknown */
8978feda 252 if (found_data && (caf->data_size < 0 || !pb->seekable))
5f14b565
PR
253 break;
254
b7effd4e
AK
255 tag = avio_rb32(pb);
256 size = avio_rb64(pb);
66e5b1df 257 if (pb->eof_reached)
5f14b565
PR
258 break;
259
260 switch (tag) {
261 case MKBETAG('d','a','t','a'):
45a8a02a 262 avio_skip(pb, 4); /* edit count */
a2704c97 263 caf->data_start = avio_tell(pb);
5f14b565 264 caf->data_size = size < 0 ? -1 : size - 4;
8978feda 265 if (caf->data_size > 0 && pb->seekable)
45a8a02a 266 avio_skip(pb, caf->data_size);
5f14b565
PR
267 found_data = 1;
268 break;
269
3bab7cd1 270 case MKBETAG('c','h','a','n'):
9afb7061 271 if ((ret = ff_mov_read_chan(s, s->pb, st, size)) < 0)
3bab7cd1
JR
272 return ret;
273 break;
274
5f14b565
PR
275 /* magic cookie chunk */
276 case MKBETAG('k','u','k','i'):
277 if (read_kuki_chunk(s, size))
278 return AVERROR_INVALIDDATA;
279 break;
280
281 /* packet table chunk */
282 case MKBETAG('p','a','k','t'):
283 if (read_pakt_chunk(s, size))
284 return AVERROR_INVALIDDATA;
285 break;
286
287 case MKBETAG('i','n','f','o'):
288 read_info_chunk(s, size);
289 break;
290
291 default:
292#define _(x) ((x) >= ' ' ? (x) : ' ')
d92024f1 293 av_log(s, AV_LOG_WARNING,
942269fd 294 "skipping CAF chunk: %08"PRIX32" (%"PRIu32"%"PRIu32"%"PRIu32"%"PRIu32")\n",
5f14b565
PR
295 tag, _(tag>>24), _((tag>>16)&0xFF), _((tag>>8)&0xFF), _(tag&0xFF));
296#undef _
297 case MKBETAG('f','r','e','e'):
298 if (size < 0)
299 return AVERROR_INVALIDDATA;
45a8a02a 300 avio_skip(pb, size);
5f14b565
PR
301 break;
302 }
303 }
304
305 if (!found_data)
306 return AVERROR_INVALIDDATA;
307
308 if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
309 if (caf->data_size > 0)
310 st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet;
311 } else if (st->nb_index_entries) {
312 st->codec->bit_rate = st->codec->sample_rate * caf->data_size * 8 /
313 st->duration;
314 } else {
315 av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when "
316 "block size or frame size are variable.\n");
317 return AVERROR_INVALIDDATA;
318 }
5f14b565 319
c3f9ebf7 320 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
5f14b565
PR
321 st->start_time = 0;
322
323 /* position the stream at the start of data */
324 if (caf->data_size >= 0)
6b4aa5da 325 avio_seek(pb, caf->data_start, SEEK_SET);
5f14b565
PR
326
327 return 0;
328}
329
330#define CAF_MAX_PKT_SIZE 4096
331
332static int read_packet(AVFormatContext *s, AVPacket *pkt)
333{
ae628ec1 334 AVIOContext *pb = s->pb;
5f14b565 335 AVStream *st = s->streams[0];
4d5b99da 336 CafContext *caf = s->priv_data;
5f14b565
PR
337 int res, pkt_size = 0, pkt_frames = 0;
338 int64_t left = CAF_MAX_PKT_SIZE;
339
66e5b1df 340 if (pb->eof_reached)
5ae092ee 341 return AVERROR(EIO);
5f14b565
PR
342
343 /* don't read past end of data chunk */
344 if (caf->data_size > 0) {
a2704c97 345 left = (caf->data_start + caf->data_size) - avio_tell(pb);
5f14b565 346 if (left <= 0)
5ae092ee 347 return AVERROR(EIO);
5f14b565
PR
348 }
349
350 pkt_frames = caf->frames_per_packet;
351 pkt_size = caf->bytes_per_packet;
352
353 if (pkt_size > 0 && pkt_frames == 1) {
354 pkt_size = (CAF_MAX_PKT_SIZE / pkt_size) * pkt_size;
355 pkt_size = FFMIN(pkt_size, left);
356 pkt_frames = pkt_size / caf->bytes_per_packet;
357 } else if (st->nb_index_entries) {
358 if (caf->packet_cnt < st->nb_index_entries - 1) {
359 pkt_size = st->index_entries[caf->packet_cnt + 1].pos - st->index_entries[caf->packet_cnt].pos;
360 pkt_frames = st->index_entries[caf->packet_cnt + 1].timestamp - st->index_entries[caf->packet_cnt].timestamp;
361 } else if (caf->packet_cnt == st->nb_index_entries - 1) {
362 pkt_size = caf->num_bytes - st->index_entries[caf->packet_cnt].pos;
363 pkt_frames = st->duration - st->index_entries[caf->packet_cnt].timestamp;
364 } else {
5ae092ee 365 return AVERROR(EIO);
5f14b565
PR
366 }
367 }
368
369 if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left)
5ae092ee 370 return AVERROR(EIO);
5f14b565
PR
371
372 res = av_get_packet(pb, pkt, pkt_size);
373 if (res < 0)
374 return res;
375
376 pkt->size = res;
377 pkt->stream_index = 0;
378 pkt->dts = pkt->pts = caf->frame_cnt;
379
380 caf->packet_cnt++;
381 caf->frame_cnt += pkt_frames;
382
383 return 0;
384}
385
386static int read_seek(AVFormatContext *s, int stream_index,
387 int64_t timestamp, int flags)
388{
389 AVStream *st = s->streams[0];
4d5b99da 390 CafContext *caf = s->priv_data;
af701d42 391 int64_t pos, packet_cnt, frame_cnt;
5f14b565
PR
392
393 timestamp = FFMAX(timestamp, 0);
394
395 if (caf->frames_per_packet > 0 && caf->bytes_per_packet > 0) {
396 /* calculate new byte position based on target frame position */
397 pos = caf->bytes_per_packet * timestamp / caf->frames_per_packet;
398 if (caf->data_size > 0)
399 pos = FFMIN(pos, caf->data_size);
af701d42
JP
400 packet_cnt = pos / caf->bytes_per_packet;
401 frame_cnt = caf->frames_per_packet * packet_cnt;
5f14b565 402 } else if (st->nb_index_entries) {
af701d42
JP
403 packet_cnt = av_index_search_timestamp(st, timestamp, flags);
404 frame_cnt = st->index_entries[packet_cnt].timestamp;
405 pos = st->index_entries[packet_cnt].pos;
5f14b565
PR
406 } else {
407 return -1;
408 }
409
af701d42
JP
410 if (avio_seek(s->pb, pos + caf->data_start, SEEK_SET) < 0)
411 return -1;
412
413 caf->packet_cnt = packet_cnt;
414 caf->frame_cnt = frame_cnt;
415
5f14b565
PR
416 return 0;
417}
418
c6610a21 419AVInputFormat ff_caf_demuxer = {
dfc2c4d9 420 .name = "caf",
0177b7d2 421 .long_name = NULL_IF_CONFIG_SMALL("Apple CAF (Core Audio Format)"),
4d5b99da 422 .priv_data_size = sizeof(CafContext),
dfc2c4d9
AK
423 .read_probe = probe,
424 .read_header = read_header,
425 .read_packet = read_packet,
426 .read_seek = read_seek,
67ffcb96 427 .codec_tag = (const AVCodecTag* const []){ ff_codec_caf_tags, 0 },
5f14b565 428};