cosmetics: Fix ATRAC codec name spelling
[libav.git] / libavformat / rmdec.c
CommitLineData
de6d9b64 1/*
7b2a2ce5 2 * "Real" compatible demuxer.
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
245976da
DB
21
22#include "libavutil/avstring.h"
60a58530 23#include "libavutil/channel_layout.h"
7950e519 24#include "libavutil/internal.h"
6a5d31ac 25#include "libavutil/intreadwrite.h"
d2d67e42 26#include "libavutil/dict.h"
de6d9b64 27#include "avformat.h"
c3f9ebf7 28#include "internal.h"
7e68c91e 29#include "rmsipr.h"
ba614729 30#include "rm.h"
1edddc99 31
7df9e693 32#define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/ATRAC
10397215
KS
33#define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
34#define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
35#define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
36#define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
37#define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
38
ba614729 39struct RMStream {
0b4ff0c9 40 AVPacket pkt; ///< place to store merged video frame / reordered audio data
1edddc99
RB
41 int videobufsize; ///< current assembled frame size
42 int videobufpos; ///< position for the next slice in the video buffer
43 int curpic_num; ///< picture number of current frame
44 int cur_slice, slices;
45 int64_t pktpos; ///< first slice position in file
46 /// Audio descrambling matrix parameters
1edddc99
RB
47 int64_t audiotimestamp; ///< Audio packet timestamp
48 int sub_packet_cnt; // Subpacket counter, used while reading
49 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
ba614729
RB
50 int audio_framesize; /// Audio frame size from container
51 int sub_packet_lengths[16]; /// Length of each subpacket
10397215 52 int32_t deint_id; ///< deinterleaver used in audio stream
ba614729
RB
53};
54
55typedef struct {
56 int nb_packets;
57 int old_format;
58 int current_stream;
59 int remaining_len;
1edddc99
RB
60 int audio_stream_num; ///< Stream number for audio packets
61 int audio_pkt_cnt; ///< Output packet counter
1edddc99 62} RMDemuxContext;
de6d9b64 63
ae628ec1 64static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
de6d9b64 65{
36cf20f5 66 int i;
28263f5f 67 char *q, r;
de6d9b64 68
de6d9b64
FB
69 q = buf;
70 for(i=0;i<len;i++) {
b7effd4e 71 r = avio_r8(pb);
de6d9b64 72 if (i < buf_size - 1)
28263f5f 73 *q++ = r;
de6d9b64 74 }
28263f5f 75 if (buf_size > 0) *q = '\0';
de6d9b64
FB
76}
77
ae628ec1 78static void get_str8(AVIOContext *pb, char *buf, int buf_size)
36cf20f5 79{
b7effd4e 80 get_strl(pb, buf, buf_size, avio_r8(pb));
de6d9b64
FB
81}
82
ae628ec1 83static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
0dae3e13
KS
84{
85 if (size >= 1<<24)
86 return -1;
87 avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
88 if (!avctx->extradata)
2874c81c 89 return AVERROR(ENOMEM);
b7effd4e 90 avctx->extradata_size = avio_read(pb, avctx->extradata, size);
0dae3e13
KS
91 memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
92 if (avctx->extradata_size != size)
93 return AVERROR(EIO);
94 return 0;
95}
96
d35b6cd3 97static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
7379d5bc
AJ
98{
99 char buf[1024];
100 int i;
101 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
d35b6cd3
MN
102 int len = wide ? avio_rb16(pb) : avio_r8(pb);
103 get_strl(pb, buf, sizeof(buf), len);
d2d67e42 104 av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
7379d5bc
AJ
105 }
106}
107
ba614729
RB
108RMStream *ff_rm_alloc_rmstream (void)
109{
110 RMStream *rms = av_mallocz(sizeof(RMStream));
111 rms->curpic_num = -1;
112 return rms;
113}
114
115void ff_rm_free_rmstream (RMStream *rms)
116{
0b4ff0c9 117 av_free_packet(&rms->pkt);
ba614729
RB
118}
119
ae628ec1 120static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
886e89d0 121 AVStream *st, RMStream *ast, int read_all)
c9510803 122{
1e4668d1 123 char buf[256];
c9510803 124 uint32_t version;
0dae3e13 125 int ret;
c9510803
FB
126
127 /* ra type header */
b7effd4e 128 version = avio_rb16(pb); /* version */
119c61a3 129 if (version == 3) {
b7effd4e 130 int header_size = avio_rb16(pb);
a2704c97 131 int64_t startpos = avio_tell(pb);
45a8a02a 132 avio_skip(pb, 14);
d35b6cd3 133 rm_read_metadata(s, pb, 0);
a2704c97 134 if ((startpos + header_size) >= avio_tell(pb) + 2) {
76f940b2 135 // fourcc (should always be "lpcJ")
b7effd4e 136 avio_r8(pb);
76f940b2 137 get_str8(pb, buf, sizeof(buf));
1e4668d1
RT
138 }
139 // Skip extra header crap (this should never happen)
a2704c97 140 if ((startpos + header_size) > avio_tell(pb))
45a8a02a 141 avio_skip(pb, header_size + startpos - avio_tell(pb));
01f4895c
MN
142 st->codec->sample_rate = 8000;
143 st->codec->channels = 1;
60a58530 144 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
72415b2a 145 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
36ef5369 146 st->codec->codec_id = AV_CODEC_ID_RA_144;
10397215 147 ast->deint_id = DEINT_ID_INT0;
c9510803 148 } else {
e0f7e329 149 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
851ef875 150 int codecdata_length;
c9510803 151 /* old version (4) */
45a8a02a 152 avio_skip(pb, 2); /* unused */
b7effd4e
AK
153 avio_rb32(pb); /* .ra4 */
154 avio_rb32(pb); /* data size */
155 avio_rb16(pb); /* version2 */
156 avio_rb32(pb); /* header size */
157 flavor= avio_rb16(pb); /* add codec info / flavor */
158 ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
159 avio_rb32(pb); /* ??? */
160 avio_rb32(pb); /* ??? */
161 avio_rb32(pb); /* ??? */
162 ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
163 st->codec->block_align= avio_rb16(pb); /* frame size */
164 ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
165 avio_rb16(pb); /* ??? */
119c61a3 166 if (version == 5) {
b7effd4e 167 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
127ed7b0 168 }
b7effd4e
AK
169 st->codec->sample_rate = avio_rb16(pb);
170 avio_rb32(pb);
171 st->codec->channels = avio_rb16(pb);
119c61a3 172 if (version == 5) {
10397215 173 ast->deint_id = avio_rl32(pb);
b7effd4e 174 avio_read(pb, buf, 4);
bb270c08
DB
175 buf[4] = 0;
176 } else {
e344c1ea 177 get_str8(pb, buf, sizeof(buf)); /* desc */
10397215 178 ast->deint_id = AV_RL32(buf);
e344c1ea 179 get_str8(pb, buf, sizeof(buf)); /* desc */
bb270c08 180 }
72415b2a 181 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
461ef741 182 st->codec->codec_tag = AV_RL32(buf);
74e8ccad
FL
183 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
184 st->codec->codec_tag);
10397215 185
461ef741 186 switch (st->codec->codec_id) {
36ef5369 187 case AV_CODEC_ID_AC3:
bf415121 188 st->need_parsing = AVSTREAM_PARSE_FULL;
461ef741 189 break;
36ef5369 190 case AV_CODEC_ID_RA_288:
e0f7e329 191 st->codec->extradata_size= 0;
ba614729 192 ast->audio_framesize = st->codec->block_align;
e0f7e329 193 st->codec->block_align = coded_framesize;
461ef741 194 break;
36ef5369 195 case AV_CODEC_ID_COOK:
b0e9edc4 196 st->need_parsing = AVSTREAM_PARSE_HEADERS;
36ef5369
AK
197 case AV_CODEC_ID_ATRAC3:
198 case AV_CODEC_ID_SIPR:
b7effd4e 199 avio_rb16(pb); avio_r8(pb);
119c61a3 200 if (version == 5)
b7effd4e
AK
201 avio_r8(pb);
202 codecdata_length = avio_rb32(pb);
a443a253
MN
203 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
204 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
205 return -1;
206 }
207
cf16c17a 208 ast->audio_framesize = st->codec->block_align;
36ef5369 209 if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
cf16c17a
RB
210 if (flavor > 3) {
211 av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
212 flavor);
213 return -1;
214 }
adae9fa6 215 st->codec->block_align = ff_sipr_subpk_size[flavor];
cf16c17a
RB
216 } else {
217 if(sub_packet_size <= 0){
218 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
219 return -1;
220 }
221 st->codec->block_align = ast->sub_packet_size;
222 }
f9664ca0 223 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
0dae3e13 224 return ret;
461ef741 225 break;
36ef5369 226 case AV_CODEC_ID_AAC:
b7effd4e 227 avio_rb16(pb); avio_r8(pb);
119c61a3 228 if (version == 5)
b7effd4e
AK
229 avio_r8(pb);
230 codecdata_length = avio_rb32(pb);
a9d4a6ef
RT
231 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
232 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
233 return -1;
234 }
a194f595 235 if (codecdata_length >= 1) {
b7effd4e 236 avio_r8(pb);
f9664ca0 237 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
0dae3e13 238 return ret;
a194f595 239 }
461ef741
KS
240 break;
241 default:
75e61b0e 242 av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
c9510803 243 }
f06068bb
LA
244 if (ast->deint_id == DEINT_ID_INT4 ||
245 ast->deint_id == DEINT_ID_GENR ||
246 ast->deint_id == DEINT_ID_SIPR) {
247 if (st->codec->block_align <= 0 ||
248 ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
249 ast->audio_framesize * sub_packet_h < st->codec->block_align)
250 return AVERROR_INVALIDDATA;
251 if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
252 return AVERROR(ENOMEM);
253 }
254 switch (ast->deint_id) {
255 case DEINT_ID_INT4:
256 if (ast->coded_framesize > ast->audio_framesize ||
e30b3e59 257 sub_packet_h <= 1 ||
f06068bb
LA
258 ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
259 return AVERROR_INVALIDDATA;
260 break;
261 case DEINT_ID_GENR:
262 if (ast->sub_packet_size <= 0 ||
263 ast->sub_packet_size > ast->audio_framesize)
264 return AVERROR_INVALIDDATA;
265 break;
266 case DEINT_ID_SIPR:
267 case DEINT_ID_INT0:
268 case DEINT_ID_VBRS:
269 case DEINT_ID_VBRF:
270 break;
271 default:
272 av_log(NULL,0,"Unknown interleaver %X\n", ast->deint_id);
273 return AVERROR_INVALIDDATA;
274 }
275
c9510803 276 if (read_all) {
b7effd4e
AK
277 avio_r8(pb);
278 avio_r8(pb);
279 avio_r8(pb);
d35b6cd3 280 rm_read_metadata(s, pb, 0);
c9510803
FB
281 }
282 }
a9d4a6ef 283 return 0;
c9510803
FB
284}
285
0fd36362 286int
ae628ec1 287ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
886e89d0 288 AVStream *st, RMStream *rst, int codec_data_size)
ff1463dc 289{
ff1463dc 290 unsigned int v;
79d5045d 291 int size;
ff1463dc 292 int64_t codec_pos;
0dae3e13 293 int ret;
ff1463dc 294
c3f9ebf7 295 avpriv_set_pts_info(st, 64, 1, 1000);
a2704c97 296 codec_pos = avio_tell(pb);
b7effd4e 297 v = avio_rb32(pb);
ff1463dc
RB
298 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
299 /* ra type header */
886e89d0 300 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
ff1463dc 301 return -1;
b8560637
KS
302 } else if (v == MKBETAG('L', 'S', 'D', ':')) {
303 avio_seek(pb, -4, SEEK_CUR);
304 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size)) < 0)
305 return ret;
306
307 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
308 st->codec->codec_tag = AV_RL32(st->codec->extradata);
309 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
310 st->codec->codec_tag);
ff1463dc 311 } else {
e65ab9d9 312 int fps;
b7effd4e 313 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
ff1463dc
RB
314 fail1:
315 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
316 goto skip;
317 }
b7effd4e 318 st->codec->codec_tag = avio_rl32(pb);
74e8ccad
FL
319 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
320 st->codec->codec_tag);
72eaba5e 321 av_dlog(s, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
36ef5369 322 if (st->codec->codec_id == AV_CODEC_ID_NONE)
ff1463dc 323 goto fail1;
a43b1e74 324 st->codec->width = avio_rb16(pb);
b7effd4e 325 st->codec->height = avio_rb16(pb);
a43b1e74
KS
326 avio_skip(pb, 2); // looks like bits per sample
327 avio_skip(pb, 4); // always zero?
72415b2a 328 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
48ce8b8d 329 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
a43b1e74 330 fps = avio_rb32(pb);
ff1463dc 331
a2704c97 332 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
0dae3e13 333 return ret;
ff1463dc 334
aba232cf 335 av_reduce(&st->avg_frame_rate.den, &st->avg_frame_rate.num,
a43b1e74 336 0x10000, fps, (1 << 30) - 1);
ff1463dc
RB
337 }
338
ff1463dc
RB
339skip:
340 /* skip codec info */
a2704c97 341 size = avio_tell(pb) - codec_pos;
45a8a02a 342 avio_skip(pb, codec_data_size - size);
ff1463dc 343
36c9e40a 344 return 0;
ff1463dc
RB
345}
346
2d6f3014
RB
347/** this function assumes that the demuxer has already seeked to the start
348 * of the INDX chunk, and will bail out if not. */
349static int rm_read_index(AVFormatContext *s)
350{
ae628ec1 351 AVIOContext *pb = s->pb;
2d6f3014
RB
352 unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
353 AVStream *st;
354
355 do {
b7effd4e 356 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
2d6f3014 357 return -1;
b7effd4e 358 size = avio_rb32(pb);
2d6f3014
RB
359 if (size < 20)
360 return -1;
45a8a02a 361 avio_skip(pb, 2);
b7effd4e
AK
362 n_pkts = avio_rb32(pb);
363 str_id = avio_rb16(pb);
364 next_off = avio_rb32(pb);
2d6f3014
RB
365 for (n = 0; n < s->nb_streams; n++)
366 if (s->streams[n]->id == str_id) {
367 st = s->streams[n];
368 break;
369 }
aac07a7a
RB
370 if (n == s->nb_streams) {
371 av_log(s, AV_LOG_ERROR,
372 "Invalid stream index %d for index at pos %"PRId64"\n",
373 str_id, avio_tell(pb));
2d6f3014 374 goto skip;
aac07a7a
RB
375 } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
376 av_log(s, AV_LOG_ERROR,
377 "Nr. of packets in packet index for stream index %d "
75c553eb 378 "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
aac07a7a
RB
379 str_id, avio_size(pb), avio_tell(pb),
380 (avio_size(pb) - avio_tell(pb)) / 14);
381 goto skip;
382 }
2d6f3014
RB
383
384 for (n = 0; n < n_pkts; n++) {
45a8a02a 385 avio_skip(pb, 2);
b7effd4e
AK
386 pts = avio_rb32(pb);
387 pos = avio_rb32(pb);
45a8a02a 388 avio_skip(pb, 4); /* packet no. */
2d6f3014
RB
389
390 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
391 }
392
393skip:
aac07a7a
RB
394 if (next_off && avio_tell(pb) < next_off &&
395 avio_seek(pb, next_off, SEEK_SET) < 0) {
396 av_log(s, AV_LOG_ERROR,
397 "Non-linear index detected, not supported\n");
2d6f3014 398 return -1;
aac07a7a 399 }
2d6f3014
RB
400 } while (next_off);
401
402 return 0;
403}
ff1463dc 404
412b4367 405static int rm_read_header_old(AVFormatContext *s)
c9510803 406{
1edddc99 407 RMDemuxContext *rm = s->priv_data;
c9510803
FB
408 AVStream *st;
409
410 rm->old_format = 1;
3b3bbdd3 411 st = avformat_new_stream(s, NULL);
c9510803 412 if (!st)
a9d4a6ef 413 return -1;
886e89d0
RB
414 st->priv_data = ff_rm_alloc_rmstream();
415 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
c9510803
FB
416}
417
6e9651d1 418static int rm_read_header(AVFormatContext *s)
de6d9b64 419{
1edddc99 420 RMDemuxContext *rm = s->priv_data;
de6d9b64 421 AVStream *st;
ae628ec1 422 AVIOContext *pb = s->pb;
ff1463dc 423 unsigned int tag;
07cb073b 424 int tag_size;
36c32bdd 425 unsigned int start_time, duration;
2d6f3014 426 unsigned int data_off = 0, indx_off = 0;
de6d9b64 427 char buf[128];
5d6ef63f 428 int flags = 0;
de6d9b64 429
b7effd4e 430 tag = avio_rl32(pb);
c9510803
FB
431 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
432 /* very old .ra format */
412b4367 433 return rm_read_header_old(s);
c9510803 434 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
6f3e0b21 435 return AVERROR(EIO);
c9510803 436 }
de6d9b64 437
1697c29d
AC
438 tag_size = avio_rb32(pb);
439 avio_skip(pb, tag_size - 8);
115329f1 440
de6d9b64 441 for(;;) {
66e5b1df 442 if (pb->eof_reached)
ccafd472 443 return -1;
b7effd4e
AK
444 tag = avio_rl32(pb);
445 tag_size = avio_rb32(pb);
446 avio_rb16(pb);
045dd4b9
DB
447 av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
448 (tag ) & 0xff,
449 (tag >> 8) & 0xff,
450 (tag >> 16) & 0xff,
451 (tag >> 24) & 0xff,
452 tag,
453 tag_size);
9786bd4d 454 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
ccafd472 455 return -1;
de6d9b64
FB
456 switch(tag) {
457 case MKTAG('P', 'R', 'O', 'P'):
458 /* file header */
b7effd4e
AK
459 avio_rb32(pb); /* max bit rate */
460 avio_rb32(pb); /* avg bit rate */
461 avio_rb32(pb); /* max packet size */
462 avio_rb32(pb); /* avg packet size */
463 avio_rb32(pb); /* nb packets */
464 avio_rb32(pb); /* duration */
465 avio_rb32(pb); /* preroll */
466 indx_off = avio_rb32(pb); /* index offset */
467 data_off = avio_rb32(pb); /* data offset */
468 avio_rb16(pb); /* nb streams */
469 flags = avio_rb16(pb); /* flags */
de6d9b64
FB
470 break;
471 case MKTAG('C', 'O', 'N', 'T'):
d35b6cd3 472 rm_read_metadata(s, pb, 1);
de6d9b64
FB
473 break;
474 case MKTAG('M', 'D', 'P', 'R'):
3b3bbdd3 475 st = avformat_new_stream(s, NULL);
de6d9b64 476 if (!st)
ccafd472 477 return AVERROR(ENOMEM);
b7effd4e
AK
478 st->id = avio_rb16(pb);
479 avio_rb32(pb); /* max bit rate */
480 st->codec->bit_rate = avio_rb32(pb); /* bit rate */
481 avio_rb32(pb); /* max packet size */
482 avio_rb32(pb); /* avg packet size */
483 start_time = avio_rb32(pb); /* start time */
484 avio_rb32(pb); /* preroll */
485 duration = avio_rb32(pb); /* duration */
c0df9d75
MN
486 st->start_time = start_time;
487 st->duration = duration;
de6d9b64
FB
488 get_str8(pb, buf, sizeof(buf)); /* desc */
489 get_str8(pb, buf, sizeof(buf)); /* mimetype */
72415b2a 490 st->codec->codec_type = AVMEDIA_TYPE_DATA;
886e89d0
RB
491 st->priv_data = ff_rm_alloc_rmstream();
492 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
b7effd4e 493 avio_rb32(pb)) < 0)
ff1463dc 494 return -1;
de6d9b64
FB
495 break;
496 case MKTAG('D', 'A', 'T', 'A'):
497 goto header_end;
498 default:
499 /* unknown tag: skip it */
45a8a02a 500 avio_skip(pb, tag_size - 10);
de6d9b64
FB
501 break;
502 }
503 }
504 header_end:
b7effd4e 505 rm->nb_packets = avio_rb32(pb); /* number of packets */
5d6ef63f
PG
506 if (!rm->nb_packets && (flags & 4))
507 rm->nb_packets = 3600 * 25;
b7effd4e 508 avio_rb32(pb); /* next data header */
2d6f3014
RB
509
510 if (!data_off)
a2704c97 511 data_off = avio_tell(pb) - 18;
8978feda 512 if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
6b4aa5da 513 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
2d6f3014 514 rm_read_index(s);
6b4aa5da 515 avio_seek(pb, data_off + 18, SEEK_SET);
2d6f3014
RB
516 }
517
de6d9b64 518 return 0;
de6d9b64
FB
519}
520
ae628ec1 521static int get_num(AVIOContext *pb, int *len)
4c2bc159
MN
522{
523 int n, n1;
524
b7effd4e 525 n = avio_rb16(pb);
4c2bc159 526 (*len)-=2;
e3052ce7 527 n &= 0x7FFF;
4c2bc159
MN
528 if (n >= 0x4000) {
529 return n - 0x4000;
530 } else {
b7effd4e 531 n1 = avio_rb16(pb);
4c2bc159
MN
532 (*len)-=2;
533 return (n << 16) | n1;
534 }
535}
536
c9510803
FB
537/* multiple of 20 bytes for ra144 (ugly) */
538#define RAW_PACKET_SIZE 1000
539
37e85dac 540static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
1edddc99 541 RMDemuxContext *rm = s->priv_data;
ae628ec1 542 AVIOContext *pb = s->pb;
bc17df09 543 AVStream *st;
119cebf4 544 uint32_t state=0xFFFFFFFF;
bc17df09 545
66e5b1df 546 while(!pb->eof_reached){
ddf8e75e 547 int len, num, i;
a2704c97 548 *pos= avio_tell(pb) - 3;
bc17df09
MN
549 if(rm->remaining_len > 0){
550 num= rm->current_stream;
551 len= rm->remaining_len;
552 *timestamp = AV_NOPTS_VALUE;
553 *flags= 0;
554 }else{
b7effd4e 555 state= (state<<8) + avio_r8(pb);
115329f1 556
119cebf4 557 if(state == MKBETAG('I', 'N', 'D', 'X')){
b5b9ff2a 558 int n_pkts, expected_len;
b7effd4e 559 len = avio_rb32(pb);
45a8a02a 560 avio_skip(pb, 2);
b7effd4e 561 n_pkts = avio_rb32(pb);
b5b9ff2a
RB
562 expected_len = 20 + n_pkts * 14;
563 if (len == 20)
564 /* some files don't add index entries to chunk size... */
565 len = expected_len;
566 else if (len != expected_len)
567 av_log(s, AV_LOG_WARNING,
568 "Index size %d (%d pkts) is wrong, should be %d.\n",
569 len, n_pkts, expected_len);
570 len -= 14; // we already read part of the index header
119cebf4
MN
571 if(len<0)
572 continue;
573 goto skip;
98287358
RB
574 } else if (state == MKBETAG('D','A','T','A')) {
575 av_log(s, AV_LOG_WARNING,
576 "DATA tag in middle of chunk, file may be broken.\n");
119cebf4 577 }
115329f1 578
83c0ba3d 579 if(state > (unsigned)0xFFFF || state <= 12)
bc17df09 580 continue;
e75e603c 581 len=state - 12;
119cebf4
MN
582 state= 0xFFFFFFFF;
583
b7effd4e
AK
584 num = avio_rb16(pb);
585 *timestamp = avio_rb32(pb);
586 avio_r8(pb); /* reserved */
587 *flags = avio_r8(pb); /* flags */
bc17df09
MN
588 }
589 for(i=0;i<s->nb_streams;i++) {
590 st = s->streams[i];
591 if (num == st->id)
592 break;
593 }
594 if (i == s->nb_streams) {
119cebf4 595skip:
bc17df09 596 /* skip packet if unknown number */
45a8a02a 597 avio_skip(pb, len);
b8cc5a9f 598 rm->remaining_len = 0;
bc17df09
MN
599 continue;
600 }
601 *stream_index= i;
115329f1 602
bc17df09
MN
603 return len;
604 }
605 return -1;
606}
607
ae628ec1 608static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
ba614729 609 RMDemuxContext *rm, RMStream *vst,
7c1f6df4
KS
610 AVPacket *pkt, int len, int *pseq,
611 int64_t *timestamp)
383b123e 612{
383b123e
KS
613 int hdr, seq, pic_num, len2, pos;
614 int type;
383b123e 615
b7effd4e 616 hdr = avio_r8(pb); len--;
383b123e 617 type = hdr >> 6;
f48d5457
MN
618
619 if(type != 3){ // not frame as a part of packet
b7effd4e 620 seq = avio_r8(pb); len--;
f48d5457
MN
621 }
622 if(type != 1){ // not whole frame
383b123e 623 len2 = get_num(pb, &len);
f48d5457 624 pos = get_num(pb, &len);
b7effd4e 625 pic_num = avio_r8(pb); len--;
f48d5457
MN
626 }
627 if(len<0)
628 return -1;
629 rm->remaining_len = len;
630 if(type&1){ // frame, not slice
7c1f6df4 631 if(type == 3){ // frame as a part of packet
f48d5457 632 len= len2;
7c1f6df4
KS
633 *timestamp = pos;
634 }
f48d5457 635 if(rm->remaining_len < len)
e45673cb 636 return -1;
f48d5457 637 rm->remaining_len -= len;
383b123e
KS
638 if(av_new_packet(pkt, len + 9) < 0)
639 return AVERROR(EIO);
640 pkt->data[0] = 0;
641 AV_WL32(pkt->data + 1, 1);
642 AV_WL32(pkt->data + 5, 0);
b7effd4e 643 avio_read(pb, pkt->data + 9, len);
383b123e
KS
644 return 0;
645 }
646 //now we have to deal with single slice
647
7a160bca 648 *pseq = seq;
ba614729
RB
649 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
650 vst->slices = ((hdr & 0x3F) << 1) + 1;
651 vst->videobufsize = len2 + 8*vst->slices + 1;
0b4ff0c9
MN
652 av_free_packet(&vst->pkt); //FIXME this should be output.
653 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
52537534 654 return AVERROR(ENOMEM);
ba614729
RB
655 vst->videobufpos = 8*vst->slices + 1;
656 vst->cur_slice = 0;
657 vst->curpic_num = pic_num;
a2704c97 658 vst->pktpos = avio_tell(pb);
383b123e 659 }
e35033e6 660 if(type == 2)
383b123e 661 len = FFMIN(len, pos);
383b123e 662
ba614729 663 if(++vst->cur_slice > vst->slices)
383b123e 664 return 1;
0b4ff0c9
MN
665 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
666 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
ba614729 667 if(vst->videobufpos + len > vst->videobufsize)
383b123e 668 return 1;
b7effd4e 669 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
383b123e 670 return AVERROR(EIO);
ba614729 671 vst->videobufpos += len;
383b123e
KS
672 rm->remaining_len-= len;
673
3dc99a18 674 if (type == 2 || vst->videobufpos == vst->videobufsize) {
188e67ea
MN
675 vst->pkt.data[0] = vst->cur_slice-1;
676 *pkt= vst->pkt;
6fde7afa 677 vst->pkt.data= NULL;
188e67ea 678 vst->pkt.size= 0;
1afddbe5
AK
679 vst->pkt.buf = NULL;
680#if FF_API_DESTRUCT_PACKET
7950e519 681FF_DISABLE_DEPRECATION_WARNINGS
1afddbe5 682 vst->pkt.destruct = NULL;
7950e519 683FF_ENABLE_DEPRECATION_WARNINGS
1afddbe5 684#endif
188e67ea 685 if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
0b4ff0c9 686 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
ba614729 687 vst->videobufpos - 1 - 8*vst->slices);
ce0453aa 688 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
188e67ea
MN
689 pkt->pts = AV_NOPTS_VALUE;
690 pkt->pos = vst->pktpos;
cbb5da57 691 vst->slices = 0;
188e67ea 692 return 0;
383b123e
KS
693 }
694
695 return 1;
696}
697
5c470b91
RB
698static inline void
699rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
700{
701 uint8_t *ptr;
702 int j;
703
36ef5369 704 if (st->codec->codec_id == AV_CODEC_ID_AC3) {
5c470b91
RB
705 ptr = pkt->data;
706 for (j=0;j<pkt->size;j+=2) {
707 FFSWAP(int, ptr[0], ptr[1]);
708 ptr += 2;
709 }
710 }
711}
712
0fd36362 713int
ae628ec1 714ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
886e89d0 715 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
34bddc39 716 int *seq, int flags, int64_t timestamp)
afd302fa 717{
1edddc99 718 RMDemuxContext *rm = s->priv_data;
afd302fa 719
72415b2a 720 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
afd302fa 721 rm->current_stream= st->id;
7c1f6df4 722 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp))
afd302fa 723 return -1; //got partial frame
72415b2a 724 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
10397215
KS
725 if ((ast->deint_id == DEINT_ID_GENR) ||
726 (ast->deint_id == DEINT_ID_INT4) ||
727 (ast->deint_id == DEINT_ID_SIPR)) {
afd302fa 728 int x;
ba614729
RB
729 int sps = ast->sub_packet_size;
730 int cfs = ast->coded_framesize;
731 int h = ast->sub_packet_h;
732 int y = ast->sub_packet_cnt;
733 int w = ast->audio_framesize;
afd302fa 734
34bddc39 735 if (flags & 2)
ba614729 736 y = ast->sub_packet_cnt = 0;
afd302fa 737 if (!y)
34bddc39 738 ast->audiotimestamp = timestamp;
afd302fa 739
10397215
KS
740 switch (ast->deint_id) {
741 case DEINT_ID_INT4:
afd302fa 742 for (x = 0; x < h/2; x++)
b7effd4e 743 avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
afd302fa 744 break;
10397215 745 case DEINT_ID_GENR:
afd302fa 746 for (x = 0; x < w/sps; x++)
b7effd4e 747 avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
afd302fa 748 break;
10397215 749 case DEINT_ID_SIPR:
b7effd4e 750 avio_read(pb, ast->pkt.data + y * w, w);
cf16c17a 751 break;
afd302fa
RB
752 }
753
ba614729 754 if (++(ast->sub_packet_cnt) < h)
afd302fa 755 return -1;
10397215 756 if (ast->deint_id == DEINT_ID_SIPR)
1ab0406d 757 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
cf16c17a 758
508fe07a
RB
759 ast->sub_packet_cnt = 0;
760 rm->audio_stream_num = st->index;
761 rm->audio_pkt_cnt = h * w / st->codec->block_align;
10397215
KS
762 } else if ((ast->deint_id == DEINT_ID_VBRF) ||
763 (ast->deint_id == DEINT_ID_VBRS)) {
afd302fa
RB
764 int x;
765 rm->audio_stream_num = st->index;
b7effd4e 766 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
ba614729
RB
767 if (ast->sub_packet_cnt) {
768 for (x = 0; x < ast->sub_packet_cnt; x++)
b7effd4e 769 ast->sub_packet_lengths[x] = avio_rb16(pb);
c5efef7b 770 rm->audio_pkt_cnt = ast->sub_packet_cnt;
34bddc39 771 ast->audiotimestamp = timestamp;
c5efef7b
RB
772 } else
773 return -1;
badbe09a 774 } else {
afd302fa 775 av_get_packet(pb, pkt, len);
e29378ff 776 rm_ac3_swap_bytes(st, pkt);
badbe09a 777 }
afd302fa
RB
778 } else
779 av_get_packet(pb, pkt, len);
780
afd302fa
RB
781 pkt->stream_index = st->index;
782
783#if 0
72415b2a 784 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
36ef5369 785 if(st->codec->codec_id == AV_CODEC_ID_RV20){
afd302fa 786 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
0fd7c133 787 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
afd302fa 788
34bddc39
RB
789 seq |= (timestamp&~0x3FFF);
790 if(seq - timestamp > 0x2000) seq -= 0x4000;
791 if(seq - timestamp < -0x2000) seq += 0x4000;
afd302fa
RB
792 }
793 }
794#endif
795
7c1f6df4 796 pkt->pts = timestamp;
34bddc39 797 if (flags & 2)
cc947f04 798 pkt->flags |= AV_PKT_FLAG_KEY;
afd302fa 799
72415b2a 800 return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
afd302fa
RB
801}
802
a9f84821 803int
ae628ec1 804ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
886e89d0 805 AVStream *st, RMStream *ast, AVPacket *pkt)
d4d1b5fb 806{
1edddc99 807 RMDemuxContext *rm = s->priv_data;
d4d1b5fb
RB
808
809 assert (rm->audio_pkt_cnt > 0);
810
3e033da8
LA
811 if (ast->deint_id == DEINT_ID_VBRF ||
812 ast->deint_id == DEINT_ID_VBRS)
ba614729 813 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
d4d1b5fb
RB
814 else {
815 av_new_packet(pkt, st->codec->block_align);
0b4ff0c9 816 memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
ba614729 817 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
d4d1b5fb
RB
818 st->codec->block_align);
819 }
820 rm->audio_pkt_cnt--;
55d330f9
RB
821 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
822 ast->audiotimestamp = AV_NOPTS_VALUE;
cc947f04 823 pkt->flags = AV_PKT_FLAG_KEY;
55d330f9 824 } else
c8829279 825 pkt->flags = 0;
d4d1b5fb 826 pkt->stream_index = st->index;
a9f84821
RB
827
828 return rm->audio_pkt_cnt;
d4d1b5fb
RB
829}
830
de6d9b64
FB
831static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
832{
1edddc99 833 RMDemuxContext *rm = s->priv_data;
de6d9b64 834 AVStream *st;
6a2d3fc3 835 int i, len, res, seq = 1;
37e85dac 836 int64_t timestamp, pos;
7a4a64e7 837 int flags;
de6d9b64 838
6a2d3fc3 839 for (;;) {
7268f0bd
RB
840 if (rm->audio_pkt_cnt) {
841 // If there are queued audio packet return them first
842 st = s->streams[rm->audio_stream_num];
843 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
190413cf 844 flags = 0;
6a2d3fc3
RB
845 } else {
846 if (rm->old_format) {
7268f0bd 847 RMStream *ast;
7ccefd08 848
7268f0bd
RB
849 st = s->streams[0];
850 ast = st->priv_data;
6a2d3fc3
RB
851 timestamp = AV_NOPTS_VALUE;
852 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
853 ast->coded_framesize * ast->sub_packet_h / 2;
854 flags = (seq++ == 1) ? 2 : 0;
a2704c97 855 pos = avio_tell(s->pb);
7268f0bd
RB
856 } else {
857 len=sync(s, &timestamp, &flags, &i, &pos);
c3df4a3b
MR
858 if (len > 0)
859 st = s->streams[i];
6a2d3fc3
RB
860 }
861
66e5b1df 862 if(len<0 || s->pb->eof_reached)
6a2d3fc3 863 return AVERROR(EIO);
bc17df09 864
7268f0bd 865 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
34bddc39 866 &seq, flags, timestamp);
7a4a64e7 867 if((flags&2) && (seq&0x7F) == 1)
7268f0bd 868 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
6a2d3fc3
RB
869 if (res)
870 continue;
871 }
d880240e 872
dc3685e1
RB
873 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
874 || st->discard >= AVDISCARD_ALL){
875 av_free_packet(pkt);
6a2d3fc3
RB
876 } else
877 break;
5d6ef63f
PG
878 }
879
de6d9b64
FB
880 return 0;
881}
882
883static int rm_read_close(AVFormatContext *s)
884{
ba614729
RB
885 int i;
886
887 for (i=0;i<s->nb_streams;i++)
888 ff_rm_free_rmstream(s->streams[i]->priv_data);
e0f7e329 889
de6d9b64
FB
890 return 0;
891}
892
c9a65ca8
FB
893static int rm_probe(AVProbeData *p)
894{
895 /* check file header */
c9510803
FB
896 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
897 p->buf[2] == 'M' && p->buf[3] == 'F' &&
898 p->buf[4] == 0 && p->buf[5] == 0) ||
899 (p->buf[0] == '.' && p->buf[1] == 'r' &&
900 p->buf[2] == 'a' && p->buf[3] == 0xfd))
c9a65ca8
FB
901 return AVPROBE_SCORE_MAX;
902 else
903 return 0;
904}
905
115329f1 906static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
bc17df09
MN
907 int64_t *ppos, int64_t pos_limit)
908{
1edddc99 909 RMDemuxContext *rm = s->priv_data;
bc17df09 910 int64_t pos, dts;
37e85dac 911 int stream_index2, flags, len, h;
bc17df09
MN
912
913 pos = *ppos;
115329f1 914
bc17df09
MN
915 if(rm->old_format)
916 return AV_NOPTS_VALUE;
917
6b4aa5da 918 avio_seek(s->pb, pos, SEEK_SET);
bc17df09
MN
919 rm->remaining_len=0;
920 for(;;){
37e85dac
MN
921 int seq=1;
922 AVStream *st;
923
924 len=sync(s, &dts, &flags, &stream_index2, &pos);
bc17df09
MN
925 if(len<0)
926 return AV_NOPTS_VALUE;
37e85dac
MN
927
928 st = s->streams[stream_index2];
72415b2a 929 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
b7effd4e 930 h= avio_r8(s->pb); len--;
37e85dac 931 if(!(h & 0x40)){
b7effd4e 932 seq = avio_r8(s->pb); len--;
bc17df09
MN
933 }
934 }
115329f1 935
37e85dac 936 if((flags&2) && (seq&0x7F) == 1){
72eaba5e
DB
937 av_dlog(s, "%d %d-%d %"PRId64" %d\n",
938 flags, stream_index2, stream_index, dts, seq);
30a43f2d 939 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
37e85dac
MN
940 if(stream_index2 == stream_index)
941 break;
942 }
943
45a8a02a 944 avio_skip(s->pb, len);
bc17df09
MN
945 }
946 *ppos = pos;
947 return dts;
948}
949
c6610a21 950AVInputFormat ff_rm_demuxer = {
dfc2c4d9 951 .name = "rm",
6774247a 952 .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
dfc2c4d9
AK
953 .priv_data_size = sizeof(RMDemuxContext),
954 .read_probe = rm_probe,
955 .read_header = rm_read_header,
956 .read_packet = rm_read_packet,
957 .read_close = rm_read_close,
958 .read_timestamp = rm_read_dts,
c9a65ca8 959};
ff13ba92 960
c6610a21 961AVInputFormat ff_rdt_demuxer = {
dfc2c4d9
AK
962 .name = "rdt",
963 .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
964 .priv_data_size = sizeof(RMDemuxContext),
965 .read_close = rm_read_close,
08826891 966 .flags = AVFMT_NOFILE,
ff13ba92 967};