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