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