avcodec/x86/h264_idct: Fix ff_h264_luma_dc_dequant_idct_sse2 checkasm failures
[ffmpeg.git] / libavformat / rmdec.c
1 /*
2 * "Real" compatible demuxer.
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <inttypes.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/mem.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "demux.h"
33 #include "internal.h"
34 #include "rmsipr.h"
35 #include "rm.h"
36
37 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/ATRAC
38 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
39 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
40 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
41 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
42 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
43
44 struct RMStream {
45 AVPacket pkt; ///< place to store merged video frame / reordered audio data
46 int videobufsize; ///< current assembled frame size
47 int videobufpos; ///< position for the next slice in the video buffer
48 int curpic_num; ///< picture number of current frame
49 int cur_slice, slices;
50 int64_t pktpos; ///< first slice position in file
51 /// Audio descrambling matrix parameters
52 int64_t audiotimestamp; ///< Audio packet timestamp
53 int sub_packet_cnt; // Subpacket counter, used while reading
54 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
55 int audio_framesize; ///< Audio frame size from container
56 int sub_packet_lengths[16]; ///< Length of each subpacket
57 int32_t deint_id; ///< deinterleaver used in audio stream
58 };
59
60 typedef struct RMDemuxContext {
61 int nb_packets;
62 int old_format;
63 int current_stream;
64 int remaining_len;
65 int audio_stream_num; ///< Stream number for audio packets
66 int audio_pkt_cnt; ///< Output packet counter
67 int data_end;
68 } RMDemuxContext;
69
70 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
71 {
72 int read = avio_get_str(pb, len, buf, buf_size);
73
74 if (read > 0)
75 avio_skip(pb, len - read);
76 }
77
78 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
79 {
80 get_strl(pb, buf, buf_size, avio_r8(pb));
81 }
82
83 static int rm_read_extradata(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, unsigned size)
84 {
85 if (size >= 1<<24) {
86 av_log(s, AV_LOG_ERROR, "extradata size %u too large\n", size);
87 return -1;
88 }
89 return ff_get_extradata(s, par, pb, size);
90 }
91
92 static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
93 {
94 char buf[1024];
95 int i;
96
97 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
98 int len = wide ? avio_rb16(pb) : avio_r8(pb);
99 if (len > 0) {
100 get_strl(pb, buf, sizeof(buf), len);
101 av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
102 }
103 }
104 }
105
106 RMStream *ff_rm_alloc_rmstream (void)
107 {
108 RMStream *rms = av_mallocz(sizeof(RMStream));
109 if (!rms)
110 return NULL;
111 rms->curpic_num = -1;
112 return rms;
113 }
114
115 void ff_rm_free_rmstream (RMStream *rms)
116 {
117 if (!rms)
118 return;
119
120 av_packet_unref(&rms->pkt);
121 }
122
123 static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
124 AVStream *st, RMStream *ast, int read_all)
125 {
126 FFStream *const sti = ffstream(st);
127 char buf[256];
128 uint32_t version;
129 int ret;
130
131 /* ra type header */
132 version = avio_rb16(pb); /* version */
133 if (version == 3) {
134 unsigned bytes_per_minute;
135 int header_size = avio_rb16(pb);
136 int64_t startpos = avio_tell(pb);
137 avio_skip(pb, 8);
138 bytes_per_minute = avio_rb16(pb);
139 avio_skip(pb, 4);
140 rm_read_metadata(s, pb, 0);
141 if ((startpos + header_size) >= avio_tell(pb) + 2) {
142 // fourcc (should always be "lpcJ")
143 avio_r8(pb);
144 get_str8(pb, buf, sizeof(buf));
145 }
146 // Skip extra header crap (this should never happen)
147 if ((startpos + header_size) > avio_tell(pb))
148 avio_skip(pb, header_size + startpos - avio_tell(pb));
149 if (bytes_per_minute)
150 st->codecpar->bit_rate = 8LL * bytes_per_minute / 60;
151 st->codecpar->sample_rate = 8000;
152 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
153 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
154 st->codecpar->codec_id = AV_CODEC_ID_RA_144;
155 ast->deint_id = DEINT_ID_INT0;
156 } else {
157 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
158 int codecdata_length;
159 unsigned bytes_per_minute;
160 /* old version (4) */
161 avio_skip(pb, 2); /* unused */
162 avio_rb32(pb); /* .ra4 */
163 avio_rb32(pb); /* data size */
164 avio_rb16(pb); /* version2 */
165 avio_rb32(pb); /* header size */
166 flavor= avio_rb16(pb); /* add codec info / flavor */
167 coded_framesize = avio_rb32(pb); /* coded frame size */
168 if (coded_framesize < 0)
169 return AVERROR_INVALIDDATA;
170 ast->coded_framesize = coded_framesize;
171
172 avio_rb32(pb); /* ??? */
173 bytes_per_minute = avio_rb32(pb);
174 if (version == 4) {
175 if (bytes_per_minute)
176 st->codecpar->bit_rate = 8LL * bytes_per_minute / 60;
177 }
178 avio_rb32(pb); /* ??? */
179 ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
180 st->codecpar->block_align= avio_rb16(pb); /* frame size */
181 ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
182 avio_rb16(pb); /* ??? */
183 if (version == 5) {
184 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
185 }
186 st->codecpar->sample_rate = avio_rb16(pb);
187 avio_rb32(pb);
188 st->codecpar->ch_layout.nb_channels = avio_rb16(pb);
189 if (version == 5) {
190 ast->deint_id = avio_rl32(pb);
191 ret = ffio_read_size(pb, buf, 4);
192 if (ret < 0)
193 return ret;
194 buf[4] = 0;
195 } else {
196 AV_WL32(buf, 0);
197 get_str8(pb, buf, sizeof(buf)); /* desc */
198 ast->deint_id = AV_RL32(buf);
199 get_str8(pb, buf, sizeof(buf)); /* desc */
200 }
201 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
202 st->codecpar->codec_tag = AV_RL32(buf);
203 st->codecpar->codec_id = ff_codec_get_id(ff_rm_codec_tags,
204 st->codecpar->codec_tag);
205
206 switch (st->codecpar->codec_id) {
207 case AV_CODEC_ID_AC3:
208 sti->need_parsing = AVSTREAM_PARSE_FULL;
209 break;
210 case AV_CODEC_ID_RA_288:
211 st->codecpar->extradata_size= 0;
212 av_freep(&st->codecpar->extradata);
213 ast->audio_framesize = st->codecpar->block_align;
214 st->codecpar->block_align = coded_framesize;
215 break;
216 case AV_CODEC_ID_COOK:
217 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
218 case AV_CODEC_ID_ATRAC3:
219 case AV_CODEC_ID_SIPR:
220 if (read_all) {
221 codecdata_length = 0;
222 } else {
223 avio_rb16(pb); avio_r8(pb);
224 if (version == 5)
225 avio_r8(pb);
226 codecdata_length = avio_rb32(pb);
227 if((unsigned)codecdata_length > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE){
228 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
229 return -1;
230 }
231 }
232
233 ast->audio_framesize = st->codecpar->block_align;
234 if (st->codecpar->codec_id == AV_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 }
240 st->codecpar->block_align = ff_sipr_subpk_size[flavor];
241 sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
242 } else {
243 if(sub_packet_size <= 0){
244 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
245 return -1;
246 }
247 st->codecpar->block_align = ast->sub_packet_size;
248 }
249 if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length)) < 0)
250 return ret;
251
252 break;
253 case AV_CODEC_ID_AAC:
254 avio_rb16(pb); avio_r8(pb);
255 if (version == 5)
256 avio_r8(pb);
257 codecdata_length = avio_rb32(pb);
258 if((unsigned)codecdata_length > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE){
259 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
260 return -1;
261 }
262 if (codecdata_length >= 1) {
263 avio_r8(pb);
264 if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length - 1)) < 0)
265 return ret;
266 }
267 break;
268 }
269 switch (ast->deint_id) {
270 case DEINT_ID_INT4:
271 if (ast->coded_framesize > ast->audio_framesize ||
272 sub_packet_h <= 1 ||
273 ast->coded_framesize * (uint64_t)sub_packet_h > (2LL + (sub_packet_h & 1)) * ast->audio_framesize)
274 return AVERROR_INVALIDDATA;
275 if (ast->coded_framesize * (uint64_t)sub_packet_h != 2LL*ast->audio_framesize) {
276 avpriv_request_sample(s, "mismatching interleaver parameters");
277 return AVERROR_INVALIDDATA;
278 }
279 break;
280 case DEINT_ID_GENR:
281 if (ast->sub_packet_size <= 0 ||
282 ast->sub_packet_size > ast->audio_framesize)
283 return AVERROR_INVALIDDATA;
284 if (ast->audio_framesize % ast->sub_packet_size)
285 return AVERROR_INVALIDDATA;
286 break;
287 case DEINT_ID_SIPR:
288 case DEINT_ID_INT0:
289 case DEINT_ID_VBRS:
290 case DEINT_ID_VBRF:
291 break;
292 default:
293 av_log(s, AV_LOG_ERROR ,"Unknown interleaver %"PRIX32"\n", ast->deint_id);
294 return AVERROR_INVALIDDATA;
295 }
296 if (ast->deint_id == DEINT_ID_INT4 ||
297 ast->deint_id == DEINT_ID_GENR ||
298 ast->deint_id == DEINT_ID_SIPR) {
299 if (st->codecpar->block_align <= 0 ||
300 ast->audio_framesize * (uint64_t)sub_packet_h > (unsigned)INT_MAX ||
301 ast->audio_framesize * sub_packet_h < st->codecpar->block_align)
302 return AVERROR_INVALIDDATA;
303 if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
304 return AVERROR(ENOMEM);
305 }
306
307 if (read_all) {
308 avio_r8(pb);
309 avio_r8(pb);
310 avio_r8(pb);
311 rm_read_metadata(s, pb, 0);
312 }
313 }
314 return 0;
315 }
316
317 int ff_rm_read_mdpr_codecdata(AVFormatContext *s, AVIOContext *pb,
318 AVStream *st, RMStream *rst,
319 unsigned int codec_data_size, const uint8_t *mime)
320 {
321 unsigned int v;
322 int size;
323 int64_t codec_pos;
324 int ret;
325
326 if (codec_data_size > INT_MAX)
327 return AVERROR_INVALIDDATA;
328 if (codec_data_size == 0)
329 return 0;
330
331 // Duplicate tags
332 if ( st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN
333 && st->codecpar->codec_type != AVMEDIA_TYPE_DATA)
334 return AVERROR_INVALIDDATA;
335
336 avpriv_set_pts_info(st, 64, 1, 1000);
337 codec_pos = avio_tell(pb);
338 v = avio_rb32(pb);
339
340 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
341 /* ra type header */
342 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
343 return -1;
344 } else if (v == MKBETAG('L', 'S', 'D', ':')) {
345 avio_seek(pb, -4, SEEK_CUR);
346 if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size)) < 0)
347 return ret;
348
349 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
350 st->codecpar->codec_tag = AV_RL32(st->codecpar->extradata);
351 st->codecpar->codec_id = ff_codec_get_id(ff_rm_codec_tags,
352 st->codecpar->codec_tag);
353 } else if(mime && !strcmp(mime, "logical-fileinfo")){
354 int stream_count, rule_count, property_count, i;
355 ff_remove_stream(s, st);
356 if (avio_rb16(pb) != 0) {
357 av_log(s, AV_LOG_WARNING, "Unsupported version\n");
358 goto skip;
359 }
360 stream_count = avio_rb16(pb);
361 avio_skip(pb, 6*stream_count);
362 rule_count = avio_rb16(pb);
363 avio_skip(pb, 2*rule_count);
364 property_count = avio_rb16(pb);
365 for(i=0; i<property_count; i++){
366 uint8_t name[128], val[128];
367 avio_rb32(pb);
368 if (avio_rb16(pb) != 0) {
369 av_log(s, AV_LOG_WARNING, "Unsupported Name value property version\n");
370 goto skip; //FIXME skip just this one
371 }
372 get_str8(pb, name, sizeof(name));
373 switch(avio_rb32(pb)) {
374 case 2: get_strl(pb, val, sizeof(val), avio_rb16(pb));
375 av_dict_set(&s->metadata, name, val, 0);
376 break;
377 default: avio_skip(pb, avio_rb16(pb));
378 }
379 }
380 } else {
381 int fps;
382 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
383 fail1:
384 av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
385 goto skip;
386 }
387 st->codecpar->codec_tag = avio_rl32(pb);
388 st->codecpar->codec_id = ff_codec_get_id(ff_rm_codec_tags,
389 st->codecpar->codec_tag);
390 av_log(s, AV_LOG_TRACE, "%"PRIX32" %X\n",
391 st->codecpar->codec_tag, MKTAG('R', 'V', '2', '0'));
392 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
393 goto fail1;
394 st->codecpar->width = avio_rb16(pb);
395 st->codecpar->height = avio_rb16(pb);
396 avio_skip(pb, 2); // looks like bits per sample
397 avio_skip(pb, 4); // always zero?
398 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
399 ffstream(st)->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
400 fps = avio_rb32(pb);
401
402 if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
403 return ret;
404
405 if (fps > 0) {
406 av_reduce(&st->avg_frame_rate.den, &st->avg_frame_rate.num,
407 0x10000, fps, (1 << 30) - 1);
408 #if FF_API_R_FRAME_RATE
409 st->r_frame_rate = st->avg_frame_rate;
410 #endif
411 } else if (s->error_recognition & AV_EF_EXPLODE) {
412 av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
413 return AVERROR_INVALIDDATA;
414 }
415 }
416
417 skip:
418 /* skip codec info */
419 size = avio_tell(pb) - codec_pos;
420 if (codec_data_size >= size) {
421 avio_skip(pb, codec_data_size - size);
422 } else {
423 av_log(s, AV_LOG_WARNING, "codec_data_size %u < size %d\n", codec_data_size, size);
424 }
425
426 return 0;
427 }
428
429 /** this function assumes that the demuxer has already seeked to the start
430 * of the INDX chunk, and will bail out if not. */
431 static int rm_read_index(AVFormatContext *s)
432 {
433 AVIOContext *pb = s->pb;
434 unsigned int size, ver, n_pkts, str_id, next_off, n, pts;
435 uint64_t pos;
436 AVStream *st;
437
438 do {
439 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
440 return -1;
441 size = avio_rb32(pb);
442 if (size < 20)
443 return -1;
444 ver = avio_rb16(pb);
445 if (ver != 0 && ver != 2)
446 return AVERROR_INVALIDDATA;
447 n_pkts = avio_rb32(pb);
448 str_id = avio_rb16(pb);
449 next_off = avio_rb32(pb);
450 if (ver == 2)
451 avio_skip(pb, 4);
452 for (n = 0; n < s->nb_streams; n++)
453 if (s->streams[n]->id == str_id) {
454 st = s->streams[n];
455 break;
456 }
457 if (n == s->nb_streams) {
458 av_log(s, AV_LOG_ERROR,
459 "Invalid stream index %d for index at pos %"PRId64"\n",
460 str_id, avio_tell(pb));
461 goto skip;
462 } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
463 av_log(s, AV_LOG_ERROR,
464 "Nr. of packets in packet index for stream index %d "
465 "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
466 str_id, avio_size(pb), avio_tell(pb),
467 (avio_size(pb) - avio_tell(pb)) / 14);
468 goto skip;
469 }
470
471 for (n = 0; n < n_pkts; n++) {
472 if (avio_feof(pb))
473 return AVERROR_INVALIDDATA;
474 avio_skip(pb, 2);
475 pts = avio_rb32(pb);
476 pos = (ver == 0) ? avio_rb32(pb) : avio_rb64(pb);
477 avio_skip(pb, 4); /* packet no. */
478
479 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
480 }
481
482 skip:
483 if (next_off && avio_tell(pb) < next_off &&
484 avio_seek(pb, next_off, SEEK_SET) < 0) {
485 av_log(s, AV_LOG_ERROR,
486 "Non-linear index detected, not supported\n");
487 return -1;
488 }
489 } while (next_off);
490
491 return 0;
492 }
493
494 static int rm_read_header_old(AVFormatContext *s)
495 {
496 RMDemuxContext *rm = s->priv_data;
497 AVStream *st;
498
499 rm->old_format = 1;
500 st = avformat_new_stream(s, NULL);
501 if (!st)
502 return -1;
503 st->priv_data = ff_rm_alloc_rmstream();
504 if (!st->priv_data)
505 return AVERROR(ENOMEM);
506 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
507 }
508
509 static int rm_read_multi(AVFormatContext *s, AVIOContext *pb,
510 AVStream *st, char *mime)
511 {
512 int number_of_streams = avio_rb16(pb);
513 int number_of_mdpr;
514 int i, ret;
515 unsigned size2;
516 for (i = 0; i<number_of_streams; i++)
517 avio_rb16(pb);
518 number_of_mdpr = avio_rb16(pb);
519 if (number_of_mdpr != 1) {
520 avpriv_request_sample(s, "MLTI with multiple (%d) MDPR", number_of_mdpr);
521 }
522 for (i = 0; i < number_of_mdpr; i++) {
523 AVStream *st2;
524 if (i > 0) {
525 st2 = avformat_new_stream(s, NULL);
526 if (!st2) {
527 ret = AVERROR(ENOMEM);
528 return ret;
529 }
530 st2->id = st->id + (i<<16);
531 st2->codecpar->bit_rate = st->codecpar->bit_rate;
532 st2->start_time = st->start_time;
533 st2->duration = st->duration;
534 st2->codecpar->codec_type = AVMEDIA_TYPE_DATA;
535 st2->priv_data = ff_rm_alloc_rmstream();
536 if (!st2->priv_data)
537 return AVERROR(ENOMEM);
538 } else
539 st2 = st;
540
541 size2 = avio_rb32(pb);
542 ret = ff_rm_read_mdpr_codecdata(s, s->pb, st2, st2->priv_data,
543 size2, NULL);
544 if (ret < 0)
545 return ret;
546 }
547 return 0;
548 }
549
550 static int rm_read_header(AVFormatContext *s)
551 {
552 RMDemuxContext *rm = s->priv_data;
553 AVStream *st;
554 AVIOContext *pb = s->pb;
555 unsigned int tag;
556 int tag_size;
557 int ver;
558 unsigned int start_time, duration;
559 unsigned int data_off = 0;
560 uint64_t indx_off = 0;
561 char buf[128], mime[128];
562 int flags = 0;
563 int ret;
564 unsigned size, v;
565 int64_t codec_pos;
566
567 tag = avio_rl32(pb);
568 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
569 /* very old .ra format */
570 return rm_read_header_old(s);
571 } else if (tag != MKTAG('.', 'R', 'M', 'F') && tag != MKTAG('.', 'R', 'M', 'P')) {
572 return AVERROR_INVALIDDATA;
573 }
574
575 tag_size = avio_rb32(pb);
576 if (tag_size < 0)
577 return AVERROR_INVALIDDATA;
578 avio_skip(pb, tag_size - 8);
579
580 for(;;) {
581 if (avio_feof(pb))
582 return AVERROR_INVALIDDATA;
583 tag = avio_rl32(pb);
584 tag_size = avio_rb32(pb);
585 ver = avio_rb16(pb);
586 av_log(s, AV_LOG_TRACE, "tag=%s size=%d\n",
587 av_fourcc2str(tag), tag_size);
588 if ((tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A')) ||
589 (ver != 0 && ver != 2))
590 return AVERROR_INVALIDDATA;
591 switch(tag) {
592 case MKTAG('P', 'R', 'O', 'P'):
593 /* file header */
594 avio_rb32(pb); /* max bit rate */
595 avio_rb32(pb); /* avg bit rate */
596 avio_rb32(pb); /* max packet size */
597 avio_rb32(pb); /* avg packet size */
598 avio_rb32(pb); /* nb packets */
599 duration = avio_rb32(pb); /* duration */
600 s->duration = av_rescale(duration, AV_TIME_BASE, 1000);
601 avio_rb32(pb); /* preroll */
602 indx_off = (ver == 0) ? avio_rb32(pb) : avio_rb64(pb); /* index offset */
603 data_off = avio_rb32(pb); /* data offset */
604 avio_rb16(pb); /* nb streams */
605 flags = avio_rb16(pb); /* flags */
606 break;
607 case MKTAG('C', 'O', 'N', 'T'):
608 rm_read_metadata(s, pb, 1);
609 break;
610 case MKTAG('M', 'D', 'P', 'R'):
611 st = avformat_new_stream(s, NULL);
612 if (!st)
613 return AVERROR(ENOMEM);
614 st->id = avio_rb16(pb);
615 avio_rb32(pb); /* max bit rate */
616 st->codecpar->bit_rate = avio_rb32(pb); /* bit rate */
617 avio_rb32(pb); /* max packet size */
618 avio_rb32(pb); /* avg packet size */
619 start_time = avio_rb32(pb); /* start time */
620 avio_rb32(pb); /* preroll */
621 duration = avio_rb32(pb); /* duration */
622 st->start_time = start_time;
623 st->duration = duration;
624 if(duration>0)
625 s->duration = AV_NOPTS_VALUE;
626 get_str8(pb, buf, sizeof(buf)); /* desc */
627 get_str8(pb, mime, sizeof(mime)); /* mimetype */
628 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
629 st->priv_data = ff_rm_alloc_rmstream();
630 if (!st->priv_data)
631 return AVERROR(ENOMEM);
632
633 size = avio_rb32(pb);
634 codec_pos = avio_tell(pb);
635
636 ffio_ensure_seekback(pb, 4);
637 v = avio_rb32(pb);
638 if (v == MKBETAG('M', 'L', 'T', 'I')) {
639 ret = rm_read_multi(s, s->pb, st, mime);
640 if (ret < 0)
641 return ret;
642 avio_seek(pb, codec_pos + size, SEEK_SET);
643 } else {
644 avio_skip(pb, -4);
645 ret = ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
646 size, mime);
647 if (ret < 0)
648 return ret;
649 }
650
651 break;
652 case MKTAG('D', 'A', 'T', 'A'):
653 goto header_end;
654 default:
655 /* unknown tag: skip it */
656 avio_skip(pb, tag_size - 10);
657 break;
658 }
659 }
660 header_end:
661 rm->nb_packets = avio_rb32(pb); /* number of packets */
662 if (!rm->nb_packets && (flags & 4))
663 rm->nb_packets = 3600 * 25;
664 if (ver == 2)
665 avio_skip(pb, 12);
666 avio_rb32(pb); /* next data header */
667
668 if (!data_off)
669 data_off = avio_tell(pb) - (ver == 0 ? 18 : 30);
670 if (indx_off && (pb->seekable & AVIO_SEEKABLE_NORMAL) &&
671 !(s->flags & AVFMT_FLAG_IGNIDX) &&
672 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
673 rm_read_index(s);
674 avio_seek(pb, data_off + (ver == 0 ? 18 : 30), SEEK_SET);
675 }
676
677 return 0;
678 }
679
680 static int get_num(AVIOContext *pb, int *len)
681 {
682 int n, n1;
683
684 n = avio_rb16(pb);
685 (*len)-=2;
686 n &= 0x7FFF;
687 if (n >= 0x4000) {
688 return n - 0x4000;
689 } else {
690 n1 = avio_rb16(pb);
691 (*len)-=2;
692 return (n << 16) | n1;
693 }
694 }
695
696 /* multiple of 20 bytes for ra144 (ugly) */
697 #define RAW_PACKET_SIZE 1000
698
699 static int rm_sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
700 RMDemuxContext *rm = s->priv_data;
701 AVIOContext *pb = s->pb;
702 AVStream *st;
703 uint32_t state=0xFFFFFFFF;
704
705 while(!avio_feof(pb)){
706 int len, num, i;
707 int mlti_id;
708 *pos= avio_tell(pb) - 3;
709 if(rm->remaining_len > 0){
710 num= rm->current_stream;
711 mlti_id = 0;
712 len= rm->remaining_len;
713 *timestamp = AV_NOPTS_VALUE;
714 *flags= 0;
715 }else{
716 state= (state<<8) + avio_r8(pb);
717
718 if(state == MKBETAG('I', 'N', 'D', 'X')){
719 int ver;
720 int n_pkts;
721 int64_t expected_len;
722 len = avio_rb32(pb);
723 ver = avio_rb16(pb);
724 if (ver != 0 && ver != 2)
725 return AVERROR_INVALIDDATA;
726 n_pkts = avio_rb32(pb);
727
728 if (ver == 0)
729 expected_len = 20 + n_pkts * 14LL;
730 else if (ver == 2)
731 expected_len = 24 + n_pkts * 18LL;
732
733 if (len == 20 && expected_len <= INT_MAX)
734 /* some files don't add index entries to chunk size... */
735 len = expected_len;
736 else if (len != expected_len)
737 av_log(s, AV_LOG_WARNING,
738 "Index size %d (%d pkts) is wrong, should be %"PRId64".\n",
739 len, n_pkts, expected_len);
740 if(len < 14)
741 continue;
742 len -= 14; // we already read part of the index header
743 goto skip;
744 } else if (state == MKBETAG('D','A','T','A')) {
745 av_log(s, AV_LOG_WARNING,
746 "DATA tag in middle of chunk, file may be broken.\n");
747 }
748
749 if(state > (unsigned)0xFFFF || state <= 12)
750 continue;
751 len=state - 12;
752 state= 0xFFFFFFFF;
753
754 num = avio_rb16(pb);
755 *timestamp = avio_rb32(pb);
756 mlti_id = (avio_r8(pb) >> 1) - 1;
757 mlti_id = FFMAX(mlti_id, 0) << 16;
758 *flags = avio_r8(pb); /* flags */
759 }
760 for(i=0;i<s->nb_streams;i++) {
761 st = s->streams[i];
762 if (mlti_id + num == st->id)
763 break;
764 }
765 if (i == s->nb_streams) {
766 skip:
767 /* skip packet if unknown number */
768 avio_skip(pb, len);
769 rm->remaining_len = 0;
770 continue;
771 }
772 *stream_index= i;
773
774 return len;
775 }
776 return -1;
777 }
778
779 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
780 RMDemuxContext *rm, RMStream *vst,
781 AVPacket *pkt, int len, int *pseq,
782 int64_t *timestamp)
783 {
784 int hdr;
785 int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silence compiler warning
786 int type;
787 int ret;
788
789 hdr = avio_r8(pb); len--;
790 type = hdr >> 6;
791
792 if(type != 3){ // not frame as a part of packet
793 seq = avio_r8(pb); len--;
794 }
795 if(type != 1){ // not whole frame
796 len2 = get_num(pb, &len);
797 pos = get_num(pb, &len);
798 pic_num = avio_r8(pb); len--;
799 }
800 if(len<0) {
801 av_log(s, AV_LOG_ERROR, "Insufficient data\n");
802 return -1;
803 }
804 rm->remaining_len = len;
805 if(type&1){ // frame, not slice
806 if(type == 3){ // frame as a part of packet
807 len= len2;
808 *timestamp = pos;
809 }
810 if(rm->remaining_len < len) {
811 av_log(s, AV_LOG_ERROR, "Insufficient remaining len\n");
812 return -1;
813 }
814 rm->remaining_len -= len;
815 if ((ret = av_new_packet(pkt, len + 9)) < 0)
816 return ret;
817 pkt->data[0] = 0;
818 AV_WL32(pkt->data + 1, 1);
819 AV_WL32(pkt->data + 5, 0);
820 ret = ffio_read_size(pb, pkt->data + 9, len);
821 if (ret < 0) {
822 av_packet_unref(pkt);
823 av_log(s, AV_LOG_ERROR, "Failed to read %d bytes\n", len);
824 return ret;
825 }
826 return 0;
827 }
828 //now we have to deal with single slice
829
830 *pseq = seq;
831 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
832 if (len2 > ffio_limit(pb, len2)) {
833 av_log(s, AV_LOG_ERROR, "Impossibly sized packet\n");
834 return AVERROR_INVALIDDATA;
835 }
836 vst->slices = ((hdr & 0x3F) << 1) + 1;
837 vst->videobufsize = len2 + 8*vst->slices + 1;
838 av_packet_unref(&vst->pkt); //FIXME this should be output.
839 if ((ret = av_new_packet(&vst->pkt, vst->videobufsize)) < 0)
840 return ret;
841 vst->videobufpos = 8*vst->slices + 1;
842 vst->cur_slice = 0;
843 vst->curpic_num = pic_num;
844 vst->pktpos = avio_tell(pb);
845 }
846 if(type == 2)
847 len = FFMIN(len, pos);
848
849 if(++vst->cur_slice > vst->slices) {
850 av_log(s, AV_LOG_ERROR, "cur slice %d, too large\n", vst->cur_slice);
851 return 1;
852 }
853 if(!vst->pkt.data)
854 return AVERROR(ENOMEM);
855 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
856 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
857 if(vst->videobufpos + len > vst->videobufsize) {
858 av_log(s, AV_LOG_ERROR, "outside videobufsize\n");
859 return 1;
860 }
861 ret = ffio_read_size(pb, vst->pkt.data + vst->videobufpos, len);
862 if (ret < 0)
863 return ret;
864 vst->videobufpos += len;
865 rm->remaining_len-= len;
866
867 if (type == 2 || vst->videobufpos == vst->videobufsize) {
868 vst->pkt.data[0] = vst->cur_slice-1;
869 av_packet_move_ref(pkt, &vst->pkt);
870 if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
871 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
872 vst->videobufpos - 1 - 8*vst->slices);
873 av_shrink_packet(pkt, vst->videobufpos + 8*(vst->cur_slice - vst->slices));
874 pkt->pts = AV_NOPTS_VALUE;
875 pkt->pos = vst->pktpos;
876 vst->slices = 0;
877 return 0;
878 }
879
880 return 1;
881 }
882
883 static inline void
884 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
885 {
886 uint8_t *ptr;
887 int j;
888
889 if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
890 ptr = pkt->data;
891 for (j=0;j<pkt->size;j+=2) {
892 FFSWAP(int, ptr[0], ptr[1]);
893 ptr += 2;
894 }
895 }
896 }
897
898 static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n) {
899 int ret = avio_read(pb, dst, n);
900 if (ret != n) {
901 if (ret >= 0) memset(dst + ret, 0, n - ret);
902 else memset(dst , 0, n);
903 av_log(s, AV_LOG_ERROR, "Failed to fully read block\n");
904 }
905 return ret;
906 }
907
908 int
909 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
910 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
911 int *seq, int flags, int64_t timestamp)
912 {
913 RMDemuxContext *rm = s->priv_data;
914 int ret;
915
916 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
917 rm->current_stream= st->id;
918 ret = rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp);
919 if(ret)
920 return ret < 0 ? ret : -1; //got partial frame or error
921 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
922 if ((ast->deint_id == DEINT_ID_GENR) ||
923 (ast->deint_id == DEINT_ID_INT4) ||
924 (ast->deint_id == DEINT_ID_SIPR)) {
925 int x;
926 int sps = ast->sub_packet_size;
927 int cfs = ast->coded_framesize;
928 int h = ast->sub_packet_h;
929 int y = ast->sub_packet_cnt;
930 int w = ast->audio_framesize;
931
932 if (flags & 2)
933 y = ast->sub_packet_cnt = 0;
934 if (!y)
935 ast->audiotimestamp = timestamp;
936
937 switch (ast->deint_id) {
938 case DEINT_ID_INT4:
939 for (x = 0; x < h/2; x++)
940 readfull(s, pb, ast->pkt.data+x*2*w+y*cfs, cfs);
941 break;
942 case DEINT_ID_GENR:
943 for (x = 0; x < w/sps; x++)
944 readfull(s, pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
945 break;
946 case DEINT_ID_SIPR:
947 readfull(s, pb, ast->pkt.data + y * w, w);
948 break;
949 }
950
951 if (++(ast->sub_packet_cnt) < h)
952 return -1;
953 if (ast->deint_id == DEINT_ID_SIPR)
954 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
955
956 ast->sub_packet_cnt = 0;
957 rm->audio_stream_num = st->index;
958 if (st->codecpar->block_align <= 0) {
959 av_log(s, AV_LOG_ERROR, "Invalid block alignment %d\n", st->codecpar->block_align);
960 return AVERROR_INVALIDDATA;
961 }
962 rm->audio_pkt_cnt = h * w / st->codecpar->block_align;
963 } else if ((ast->deint_id == DEINT_ID_VBRF) ||
964 (ast->deint_id == DEINT_ID_VBRS)) {
965 int x;
966 rm->audio_stream_num = st->index;
967 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
968 if (ast->sub_packet_cnt) {
969 for (x = 0; x < ast->sub_packet_cnt; x++)
970 ast->sub_packet_lengths[x] = avio_rb16(pb);
971 rm->audio_pkt_cnt = ast->sub_packet_cnt;
972 ast->audiotimestamp = timestamp;
973 } else
974 return -1;
975 } else {
976 ret = av_get_packet(pb, pkt, len);
977 if (ret < 0)
978 return ret;
979 rm_ac3_swap_bytes(st, pkt);
980 }
981 } else {
982 ret = av_get_packet(pb, pkt, len);
983 if (ret < 0)
984 return ret;
985 }
986
987 pkt->stream_index = st->index;
988
989 pkt->pts = timestamp;
990 if (flags & 2)
991 pkt->flags |= AV_PKT_FLAG_KEY;
992
993 return st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
994 }
995
996 int
997 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
998 AVStream *st, RMStream *ast, AVPacket *pkt)
999 {
1000 RMDemuxContext *rm = s->priv_data;
1001 int ret;
1002
1003 av_assert0 (rm->audio_pkt_cnt > 0);
1004
1005 if (ast->deint_id == DEINT_ID_VBRF ||
1006 ast->deint_id == DEINT_ID_VBRS) {
1007 ret = av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
1008 if (ret < 0)
1009 return ret;
1010 } else {
1011 ret = av_new_packet(pkt, st->codecpar->block_align);
1012 if (ret < 0)
1013 return ret;
1014 memcpy(pkt->data, ast->pkt.data + st->codecpar->block_align * //FIXME avoid this
1015 (ast->sub_packet_h * ast->audio_framesize / st->codecpar->block_align - rm->audio_pkt_cnt),
1016 st->codecpar->block_align);
1017 }
1018 rm->audio_pkt_cnt--;
1019 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
1020 ast->audiotimestamp = AV_NOPTS_VALUE;
1021 pkt->flags = AV_PKT_FLAG_KEY;
1022 } else
1023 pkt->flags = 0;
1024 pkt->stream_index = st->index;
1025
1026 return rm->audio_pkt_cnt;
1027 }
1028
1029 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
1030 {
1031 RMDemuxContext *rm = s->priv_data;
1032 AVStream *st = NULL; // init to silence compiler warning
1033 int i, res, seq = 1;
1034 int64_t timestamp, pos, len;
1035 int flags;
1036
1037 for (;;) {
1038 if (rm->audio_pkt_cnt) {
1039 // If there are queued audio packet return them first
1040 st = s->streams[rm->audio_stream_num];
1041 res = ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
1042 if(res < 0)
1043 return res;
1044 flags = 0;
1045 } else {
1046 if (rm->old_format) {
1047 RMStream *ast;
1048
1049 st = s->streams[0];
1050 ast = st->priv_data;
1051 timestamp = AV_NOPTS_VALUE;
1052 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
1053 ast->coded_framesize * (int64_t)ast->sub_packet_h / 2;
1054 if (len > INT_MAX)
1055 return AVERROR_INVALIDDATA;
1056 flags = (seq++ == 1) ? 2 : 0;
1057 pos = avio_tell(s->pb);
1058 } else {
1059 len = rm_sync(s, &timestamp, &flags, &i, &pos);
1060 if (len > 0)
1061 st = s->streams[i];
1062 }
1063
1064 if (avio_feof(s->pb))
1065 return AVERROR_EOF;
1066 if (len <= 0)
1067 return AVERROR_INVALIDDATA;
1068
1069 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
1070 &seq, flags, timestamp);
1071 if (res < -1)
1072 return res;
1073 if((flags&2) && (seq&0x7F) == 1)
1074 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
1075 if (res)
1076 continue;
1077 }
1078
1079 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
1080 || st->discard >= AVDISCARD_ALL){
1081 av_packet_unref(pkt);
1082 } else
1083 break;
1084 }
1085
1086 return 0;
1087 }
1088
1089 static int rm_read_close(AVFormatContext *s)
1090 {
1091 int i;
1092
1093 for (i=0;i<s->nb_streams;i++)
1094 ff_rm_free_rmstream(s->streams[i]->priv_data);
1095
1096 return 0;
1097 }
1098
1099 static int rm_probe(const AVProbeData *p)
1100 {
1101 /* check file header */
1102 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1103 p->buf[2] == 'M' && (p->buf[3] == 'F' || p->buf[3] == 'P') &&
1104 p->buf[4] == 0 && p->buf[5] == 0) ||
1105 (p->buf[0] == '.' && p->buf[1] == 'r' &&
1106 p->buf[2] == 'a' && p->buf[3] == 0xfd))
1107 return AVPROBE_SCORE_MAX;
1108 else
1109 return 0;
1110 }
1111
1112 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1113 int64_t *ppos, int64_t pos_limit)
1114 {
1115 RMDemuxContext *rm = s->priv_data;
1116 int64_t pos, dts;
1117 int stream_index2, flags, len, h;
1118
1119 pos = *ppos;
1120
1121 if(rm->old_format)
1122 return AV_NOPTS_VALUE;
1123
1124 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1125 return AV_NOPTS_VALUE;
1126
1127 rm->remaining_len=0;
1128 for(;;){
1129 int seq=1;
1130 AVStream *st;
1131
1132 len = rm_sync(s, &dts, &flags, &stream_index2, &pos);
1133 if(len<0)
1134 return AV_NOPTS_VALUE;
1135
1136 st = s->streams[stream_index2];
1137 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1138 h= avio_r8(s->pb); len--;
1139 if(!(h & 0x40)){
1140 seq = avio_r8(s->pb); len--;
1141 }
1142 }
1143
1144 if((flags&2) && (seq&0x7F) == 1){
1145 av_log(s, AV_LOG_TRACE, "%d %d-%d %"PRId64" %d\n",
1146 flags, stream_index2, stream_index, dts, seq);
1147 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1148 if(stream_index2 == stream_index)
1149 break;
1150 }
1151
1152 avio_skip(s->pb, len);
1153 }
1154 *ppos = pos;
1155 return dts;
1156 }
1157
1158 static int rm_read_seek(AVFormatContext *s, int stream_index,
1159 int64_t pts, int flags)
1160 {
1161 RMDemuxContext *rm = s->priv_data;
1162
1163 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1164 return -1;
1165 rm->audio_pkt_cnt = 0;
1166 return 0;
1167 }
1168
1169
1170 const FFInputFormat ff_rm_demuxer = {
1171 .p.name = "rm",
1172 .p.long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
1173 .priv_data_size = sizeof(RMDemuxContext),
1174 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
1175 .read_probe = rm_probe,
1176 .read_header = rm_read_header,
1177 .read_packet = rm_read_packet,
1178 .read_close = rm_read_close,
1179 .read_timestamp = rm_read_dts,
1180 .read_seek = rm_read_seek,
1181 };
1182
1183 const FFInputFormat ff_rdt_demuxer = {
1184 .p.name = "rdt",
1185 .p.long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
1186 .p.flags = AVFMT_NOFILE,
1187 .priv_data_size = sizeof(RMDemuxContext),
1188 .read_close = rm_read_close,
1189 };
1190
1191 static int ivr_probe(const AVProbeData *p)
1192 {
1193 if (memcmp(p->buf, ".R1M\x0\x1\x1", 7) &&
1194 memcmp(p->buf, ".REC", 4))
1195 return 0;
1196
1197 return AVPROBE_SCORE_MAX;
1198 }
1199
1200 static int ivr_read_header(AVFormatContext *s)
1201 {
1202 unsigned tag, type, len, tlen, value;
1203 int i, j, n, count, nb_streams = 0, ret;
1204 uint8_t key[256], val[256];
1205 AVIOContext *pb = s->pb;
1206 AVStream *st;
1207 int64_t pos, offset=0, temp;
1208
1209 pos = avio_tell(pb);
1210 tag = avio_rl32(pb);
1211 if (tag == MKTAG('.','R','1','M')) {
1212 if (avio_rb16(pb) != 1)
1213 return AVERROR_INVALIDDATA;
1214 if (avio_r8(pb) != 1)
1215 return AVERROR_INVALIDDATA;
1216 len = avio_rb32(pb);
1217 avio_skip(pb, len);
1218 avio_skip(pb, 5);
1219 temp = avio_rb64(pb);
1220 while (!avio_feof(pb) && temp) {
1221 offset = temp;
1222 temp = avio_rb64(pb);
1223 }
1224 if (offset <= 0)
1225 return AVERROR_INVALIDDATA;
1226 avio_skip(pb, offset - avio_tell(pb));
1227 if (avio_r8(pb) != 1)
1228 return AVERROR_INVALIDDATA;
1229 len = avio_rb32(pb);
1230 avio_skip(pb, len);
1231 if (avio_r8(pb) != 2)
1232 return AVERROR_INVALIDDATA;
1233 avio_skip(pb, 16);
1234 pos = avio_tell(pb);
1235 tag = avio_rl32(pb);
1236 }
1237
1238 if (tag != MKTAG('.','R','E','C'))
1239 return AVERROR_INVALIDDATA;
1240
1241 if (avio_r8(pb) != 0)
1242 return AVERROR_INVALIDDATA;
1243 count = avio_rb32(pb);
1244 for (i = 0; i < count; i++) {
1245 if (avio_feof(pb))
1246 return AVERROR_INVALIDDATA;
1247
1248 type = avio_r8(pb);
1249 tlen = avio_rb32(pb);
1250 avio_get_str(pb, tlen, key, sizeof(key));
1251 len = avio_rb32(pb);
1252 if (type == 5) {
1253 avio_get_str(pb, len, val, sizeof(val));
1254 av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1255 } else if (type == 4) {
1256 av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1257 for (j = 0; j < len; j++) {
1258 if (avio_feof(pb))
1259 return AVERROR_INVALIDDATA;
1260 av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1261 }
1262 av_log(s, AV_LOG_DEBUG, "'\n");
1263 } else if (len == 4 && type == 3 && !strncmp(key, "StreamCount", tlen)) {
1264 nb_streams = value = avio_rb32(pb);
1265 } else if (len == 4 && type == 3) {
1266 value = avio_rb32(pb);
1267 av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1268 } else {
1269 av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1270 avio_skip(pb, len);
1271 }
1272 }
1273
1274 for (n = 0; n < nb_streams; n++) {
1275 if (!(st = avformat_new_stream(s, NULL)) ||
1276 !(st->priv_data = ff_rm_alloc_rmstream()))
1277 return AVERROR(ENOMEM);
1278
1279 if (avio_r8(pb) != 1)
1280 return AVERROR_INVALIDDATA;
1281
1282 count = avio_rb32(pb);
1283 for (i = 0; i < count; i++) {
1284 if (avio_feof(pb))
1285 return AVERROR_INVALIDDATA;
1286
1287 type = avio_r8(pb);
1288 tlen = avio_rb32(pb);
1289 avio_get_str(pb, tlen, key, sizeof(key));
1290 len = avio_rb32(pb);
1291 if (type == 5) {
1292 avio_get_str(pb, len, val, sizeof(val));
1293 av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1294 } else if (type == 4 && !strncmp(key, "OpaqueData", tlen)) {
1295 ret = ffio_ensure_seekback(pb, 4);
1296 if (ret < 0)
1297 return ret;
1298 if (avio_rb32(pb) == MKBETAG('M', 'L', 'T', 'I')) {
1299 ret = rm_read_multi(s, pb, st, NULL);
1300 } else {
1301 if (avio_feof(pb))
1302 return AVERROR_INVALIDDATA;
1303 avio_seek(pb, -4, SEEK_CUR);
1304 ret = ff_rm_read_mdpr_codecdata(s, pb, st, st->priv_data, len, NULL);
1305 }
1306
1307 if (ret < 0)
1308 return ret;
1309 } else if (type == 4) {
1310 int j;
1311
1312 av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1313 for (j = 0; j < len; j++) {
1314 if (avio_feof(pb))
1315 return AVERROR_INVALIDDATA;
1316 av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1317 }
1318 av_log(s, AV_LOG_DEBUG, "'\n");
1319 } else if (len == 4 && type == 3 && !strncmp(key, "Duration", tlen)) {
1320 st->duration = avio_rb32(pb);
1321 } else if (len == 4 && type == 3) {
1322 value = avio_rb32(pb);
1323 av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1324 } else {
1325 av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1326 avio_skip(pb, len);
1327 }
1328 }
1329 }
1330
1331 if (avio_r8(pb) != 6)
1332 return AVERROR_INVALIDDATA;
1333 avio_skip(pb, 12);
1334 avio_seek(pb, avio_rb64(pb) + pos, SEEK_SET);
1335 if (avio_r8(pb) != 8)
1336 return AVERROR_INVALIDDATA;
1337 avio_skip(pb, 8);
1338
1339 return 0;
1340 }
1341
1342 static int ivr_read_packet(AVFormatContext *s, AVPacket *pkt)
1343 {
1344 RMDemuxContext *rm = s->priv_data;
1345 int ret = AVERROR_EOF, opcode;
1346 AVIOContext *pb = s->pb;
1347 unsigned size, index;
1348 int64_t pos, pts;
1349
1350 if (avio_feof(pb) || rm->data_end)
1351 return AVERROR_EOF;
1352
1353 pos = avio_tell(pb);
1354
1355 for (;;) {
1356 if (rm->audio_pkt_cnt) {
1357 // If there are queued audio packet return them first
1358 AVStream *st;
1359
1360 st = s->streams[rm->audio_stream_num];
1361 ret = ff_rm_retrieve_cache(s, pb, st, st->priv_data, pkt);
1362 if (ret < 0) {
1363 return ret;
1364 }
1365 } else {
1366 if (rm->remaining_len) {
1367 avio_skip(pb, rm->remaining_len);
1368 rm->remaining_len = 0;
1369 }
1370
1371 if (avio_feof(pb))
1372 return AVERROR_EOF;
1373
1374 opcode = avio_r8(pb);
1375 if (opcode == 2) {
1376 AVStream *st;
1377 int seq = 1;
1378
1379 pts = avio_rb32(pb);
1380 index = avio_rb16(pb);
1381 if (index >= s->nb_streams)
1382 return AVERROR_INVALIDDATA;
1383
1384 avio_skip(pb, 4);
1385 size = avio_rb32(pb);
1386 avio_skip(pb, 4);
1387
1388 if (size < 1 || size > INT_MAX/4) {
1389 av_log(s, AV_LOG_ERROR, "size %u is invalid\n", size);
1390 return AVERROR_INVALIDDATA;
1391 }
1392
1393 st = s->streams[index];
1394 ret = ff_rm_parse_packet(s, pb, st, st->priv_data, size, pkt,
1395 &seq, 0, pts);
1396 if (ret < -1) {
1397 return ret;
1398 } else if (ret) {
1399 continue;
1400 }
1401
1402 pkt->pos = pos;
1403 pkt->pts = pts;
1404 pkt->stream_index = index;
1405 } else if (opcode == 7) {
1406 pos = avio_rb64(pb);
1407 if (!pos) {
1408 rm->data_end = 1;
1409 return AVERROR_EOF;
1410 }
1411 } else {
1412 av_log(s, AV_LOG_ERROR, "Unsupported opcode=%d at %"PRIX64"\n", opcode, avio_tell(pb) - 1);
1413 return AVERROR_INVALIDDATA;
1414 }
1415 }
1416
1417 break;
1418 }
1419
1420 return ret;
1421 }
1422
1423 const FFInputFormat ff_ivr_demuxer = {
1424 .p.name = "ivr",
1425 .p.long_name = NULL_IF_CONFIG_SMALL("IVR (Internet Video Recording)"),
1426 .p.extensions = "ivr",
1427 .priv_data_size = sizeof(RMDemuxContext),
1428 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
1429 .read_probe = ivr_probe,
1430 .read_header = ivr_read_header,
1431 .read_packet = ivr_read_packet,
1432 .read_close = rm_read_close,
1433 };