avcodec/x86/pngdsp: add missing emms at the end of add_png_paeth_prediction
[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/avstring.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/dict.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "rmsipr.h"
34 #include "rm.h"
35
36 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/ATRAC
37 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
38 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
39 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
40 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
41 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
42
43 struct RMStream {
44 AVPacket pkt; ///< place to store merged video frame / reordered audio data
45 int videobufsize; ///< current assembled frame size
46 int videobufpos; ///< position for the next slice in the video buffer
47 int curpic_num; ///< picture number of current frame
48 int cur_slice, slices;
49 int64_t pktpos; ///< first slice position in file
50 /// Audio descrambling matrix parameters
51 int64_t audiotimestamp; ///< Audio packet timestamp
52 int sub_packet_cnt; // Subpacket counter, used while reading
53 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
54 int audio_framesize; /// Audio frame size from container
55 int sub_packet_lengths[16]; /// Length of each subpacket
56 int32_t deint_id; ///< deinterleaver used in audio stream
57 };
58
59 typedef struct RMDemuxContext {
60 int nb_packets;
61 int old_format;
62 int current_stream;
63 int remaining_len;
64 int audio_stream_num; ///< Stream number for audio packets
65 int audio_pkt_cnt; ///< Output packet counter
66 int data_end;
67 } RMDemuxContext;
68
69 static int rm_read_close(AVFormatContext *s);
70
71 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
72 {
73 int read = avio_get_str(pb, len, buf, buf_size);
74
75 if (read > 0)
76 avio_skip(pb, len - read);
77 }
78
79 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
80 {
81 get_strl(pb, buf, buf_size, avio_r8(pb));
82 }
83
84 static int rm_read_extradata(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, unsigned size)
85 {
86 if (size >= 1<<24) {
87 av_log(s, AV_LOG_ERROR, "extradata size %u too large\n", size);
88 return -1;
89 }
90 return ff_get_extradata(s, par, pb, size);
91 }
92
93 static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
94 {
95 char buf[1024];
96 int i;
97
98 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
99 int len = wide ? avio_rb16(pb) : avio_r8(pb);
100 if (len > 0) {
101 get_strl(pb, buf, sizeof(buf), len);
102 av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
103 }
104 }
105 }
106
107 RMStream *ff_rm_alloc_rmstream (void)
108 {
109 RMStream *rms = av_mallocz(sizeof(RMStream));
110 if (!rms)
111 return NULL;
112 rms->curpic_num = -1;
113 return rms;
114 }
115
116 void ff_rm_free_rmstream (RMStream *rms)
117 {
118 if (!rms)
119 return;
120
121 av_packet_unref(&rms->pkt);
122 }
123
124 static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
125 AVStream *st, RMStream *ast, int read_all)
126 {
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->channels = 1;
153 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
154 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
155 st->codecpar->codec_id = AV_CODEC_ID_RA_144;
156 ast->deint_id = DEINT_ID_INT0;
157 } else {
158 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
159 int codecdata_length;
160 unsigned bytes_per_minute;
161 /* old version (4) */
162 avio_skip(pb, 2); /* unused */
163 avio_rb32(pb); /* .ra4 */
164 avio_rb32(pb); /* data size */
165 avio_rb16(pb); /* version2 */
166 avio_rb32(pb); /* header size */
167 flavor= avio_rb16(pb); /* add codec info / flavor */
168 coded_framesize = avio_rb32(pb); /* coded frame size */
169 if (coded_framesize < 0)
170 return AVERROR_INVALIDDATA;
171 ast->coded_framesize = coded_framesize;
172
173 avio_rb32(pb); /* ??? */
174 bytes_per_minute = avio_rb32(pb);
175 if (version == 4) {
176 if (bytes_per_minute)
177 st->codecpar->bit_rate = 8LL * bytes_per_minute / 60;
178 }
179 avio_rb32(pb); /* ??? */
180 ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
181 st->codecpar->block_align= avio_rb16(pb); /* frame size */
182 ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
183 avio_rb16(pb); /* ??? */
184 if (version == 5) {
185 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
186 }
187 st->codecpar->sample_rate = avio_rb16(pb);
188 avio_rb32(pb);
189 st->codecpar->channels = avio_rb16(pb);
190 if (version == 5) {
191 ast->deint_id = avio_rl32(pb);
192 if (avio_read(pb, buf, 4) != 4)
193 return AVERROR_INVALIDDATA;
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 st->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 st->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 st->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_free_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 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, n_pkts, str_id, next_off, n, pos, pts;
435 AVStream *st;
436
437 do {
438 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
439 return -1;
440 size = avio_rb32(pb);
441 if (size < 20)
442 return -1;
443 avio_skip(pb, 2);
444 n_pkts = avio_rb32(pb);
445 str_id = avio_rb16(pb);
446 next_off = avio_rb32(pb);
447 for (n = 0; n < s->nb_streams; n++)
448 if (s->streams[n]->id == str_id) {
449 st = s->streams[n];
450 break;
451 }
452 if (n == s->nb_streams) {
453 av_log(s, AV_LOG_ERROR,
454 "Invalid stream index %d for index at pos %"PRId64"\n",
455 str_id, avio_tell(pb));
456 goto skip;
457 } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
458 av_log(s, AV_LOG_ERROR,
459 "Nr. of packets in packet index for stream index %d "
460 "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
461 str_id, avio_size(pb), avio_tell(pb),
462 (avio_size(pb) - avio_tell(pb)) / 14);
463 goto skip;
464 }
465
466 for (n = 0; n < n_pkts; n++) {
467 if (avio_feof(pb))
468 return AVERROR_INVALIDDATA;
469 avio_skip(pb, 2);
470 pts = avio_rb32(pb);
471 pos = avio_rb32(pb);
472 avio_skip(pb, 4); /* packet no. */
473
474 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
475 }
476
477 skip:
478 if (next_off && avio_tell(pb) < next_off &&
479 avio_seek(pb, next_off, SEEK_SET) < 0) {
480 av_log(s, AV_LOG_ERROR,
481 "Non-linear index detected, not supported\n");
482 return -1;
483 }
484 } while (next_off);
485
486 return 0;
487 }
488
489 static int rm_read_header_old(AVFormatContext *s)
490 {
491 RMDemuxContext *rm = s->priv_data;
492 AVStream *st;
493
494 rm->old_format = 1;
495 st = avformat_new_stream(s, NULL);
496 if (!st)
497 return -1;
498 st->priv_data = ff_rm_alloc_rmstream();
499 if (!st->priv_data)
500 return AVERROR(ENOMEM);
501 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
502 }
503
504 static int rm_read_multi(AVFormatContext *s, AVIOContext *pb,
505 AVStream *st, char *mime)
506 {
507 int number_of_streams = avio_rb16(pb);
508 int number_of_mdpr;
509 int i, ret;
510 unsigned size2;
511 for (i = 0; i<number_of_streams; i++)
512 avio_rb16(pb);
513 number_of_mdpr = avio_rb16(pb);
514 if (number_of_mdpr != 1) {
515 avpriv_request_sample(s, "MLTI with multiple (%d) MDPR", number_of_mdpr);
516 }
517 for (i = 0; i < number_of_mdpr; i++) {
518 AVStream *st2;
519 if (i > 0) {
520 st2 = avformat_new_stream(s, NULL);
521 if (!st2) {
522 ret = AVERROR(ENOMEM);
523 return ret;
524 }
525 st2->id = st->id + (i<<16);
526 st2->codecpar->bit_rate = st->codecpar->bit_rate;
527 st2->start_time = st->start_time;
528 st2->duration = st->duration;
529 st2->codecpar->codec_type = AVMEDIA_TYPE_DATA;
530 st2->priv_data = ff_rm_alloc_rmstream();
531 if (!st2->priv_data)
532 return AVERROR(ENOMEM);
533 } else
534 st2 = st;
535
536 size2 = avio_rb32(pb);
537 ret = ff_rm_read_mdpr_codecdata(s, s->pb, st2, st2->priv_data,
538 size2, NULL);
539 if (ret < 0)
540 return ret;
541 }
542 return 0;
543 }
544
545 static int rm_read_header(AVFormatContext *s)
546 {
547 RMDemuxContext *rm = s->priv_data;
548 AVStream *st;
549 AVIOContext *pb = s->pb;
550 unsigned int tag;
551 int tag_size;
552 unsigned int start_time, duration;
553 unsigned int data_off = 0, indx_off = 0;
554 char buf[128], mime[128];
555 int flags = 0;
556 int ret;
557 unsigned size, v;
558 int64_t codec_pos;
559
560 tag = avio_rl32(pb);
561 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
562 /* very old .ra format */
563 return rm_read_header_old(s);
564 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
565 return AVERROR(EIO);
566 }
567
568 tag_size = avio_rb32(pb);
569 if (tag_size < 0)
570 return AVERROR_INVALIDDATA;
571 avio_skip(pb, tag_size - 8);
572
573 for(;;) {
574 ret = AVERROR_INVALIDDATA;
575 if (avio_feof(pb))
576 goto fail;
577 tag = avio_rl32(pb);
578 tag_size = avio_rb32(pb);
579 avio_rb16(pb);
580 av_log(s, AV_LOG_TRACE, "tag=%s size=%d\n",
581 av_fourcc2str(tag), tag_size);
582 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
583 goto fail;
584 switch(tag) {
585 case MKTAG('P', 'R', 'O', 'P'):
586 /* file header */
587 avio_rb32(pb); /* max bit rate */
588 avio_rb32(pb); /* avg bit rate */
589 avio_rb32(pb); /* max packet size */
590 avio_rb32(pb); /* avg packet size */
591 avio_rb32(pb); /* nb packets */
592 duration = avio_rb32(pb); /* duration */
593 s->duration = av_rescale(duration, AV_TIME_BASE, 1000);
594 avio_rb32(pb); /* preroll */
595 indx_off = avio_rb32(pb); /* index offset */
596 data_off = avio_rb32(pb); /* data offset */
597 avio_rb16(pb); /* nb streams */
598 flags = avio_rb16(pb); /* flags */
599 break;
600 case MKTAG('C', 'O', 'N', 'T'):
601 rm_read_metadata(s, pb, 1);
602 break;
603 case MKTAG('M', 'D', 'P', 'R'):
604 st = avformat_new_stream(s, NULL);
605 if (!st) {
606 ret = AVERROR(ENOMEM);
607 goto fail;
608 }
609 st->id = avio_rb16(pb);
610 avio_rb32(pb); /* max bit rate */
611 st->codecpar->bit_rate = avio_rb32(pb); /* bit rate */
612 avio_rb32(pb); /* max packet size */
613 avio_rb32(pb); /* avg packet size */
614 start_time = avio_rb32(pb); /* start time */
615 avio_rb32(pb); /* preroll */
616 duration = avio_rb32(pb); /* duration */
617 st->start_time = start_time;
618 st->duration = duration;
619 if(duration>0)
620 s->duration = AV_NOPTS_VALUE;
621 get_str8(pb, buf, sizeof(buf)); /* desc */
622 get_str8(pb, mime, sizeof(mime)); /* mimetype */
623 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
624 st->priv_data = ff_rm_alloc_rmstream();
625 if (!st->priv_data)
626 return AVERROR(ENOMEM);
627
628 size = avio_rb32(pb);
629 codec_pos = avio_tell(pb);
630
631 ffio_ensure_seekback(pb, 4);
632 v = avio_rb32(pb);
633 if (v == MKBETAG('M', 'L', 'T', 'I')) {
634 ret = rm_read_multi(s, s->pb, st, mime);
635 if (ret < 0)
636 goto fail;
637 avio_seek(pb, codec_pos + size, SEEK_SET);
638 } else {
639 avio_skip(pb, -4);
640 ret = ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
641 size, mime);
642 if (ret < 0)
643 goto fail;
644 }
645
646 break;
647 case MKTAG('D', 'A', 'T', 'A'):
648 goto header_end;
649 default:
650 /* unknown tag: skip it */
651 avio_skip(pb, tag_size - 10);
652 break;
653 }
654 }
655 header_end:
656 rm->nb_packets = avio_rb32(pb); /* number of packets */
657 if (!rm->nb_packets && (flags & 4))
658 rm->nb_packets = 3600 * 25;
659 avio_rb32(pb); /* next data header */
660
661 if (!data_off)
662 data_off = avio_tell(pb) - 18;
663 if (indx_off && (pb->seekable & AVIO_SEEKABLE_NORMAL) &&
664 !(s->flags & AVFMT_FLAG_IGNIDX) &&
665 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
666 rm_read_index(s);
667 avio_seek(pb, data_off + 18, SEEK_SET);
668 }
669
670 return 0;
671
672 fail:
673 rm_read_close(s);
674 return ret;
675 }
676
677 static int get_num(AVIOContext *pb, int *len)
678 {
679 int n, n1;
680
681 n = avio_rb16(pb);
682 (*len)-=2;
683 n &= 0x7FFF;
684 if (n >= 0x4000) {
685 return n - 0x4000;
686 } else {
687 n1 = avio_rb16(pb);
688 (*len)-=2;
689 return (n << 16) | n1;
690 }
691 }
692
693 /* multiple of 20 bytes for ra144 (ugly) */
694 #define RAW_PACKET_SIZE 1000
695
696 static int rm_sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
697 RMDemuxContext *rm = s->priv_data;
698 AVIOContext *pb = s->pb;
699 AVStream *st;
700 uint32_t state=0xFFFFFFFF;
701
702 while(!avio_feof(pb)){
703 int len, num, i;
704 int mlti_id;
705 *pos= avio_tell(pb) - 3;
706 if(rm->remaining_len > 0){
707 num= rm->current_stream;
708 mlti_id = 0;
709 len= rm->remaining_len;
710 *timestamp = AV_NOPTS_VALUE;
711 *flags= 0;
712 }else{
713 state= (state<<8) + avio_r8(pb);
714
715 if(state == MKBETAG('I', 'N', 'D', 'X')){
716 int n_pkts;
717 int64_t expected_len;
718 len = avio_rb32(pb);
719 avio_skip(pb, 2);
720 n_pkts = avio_rb32(pb);
721 expected_len = 20 + n_pkts * 14LL;
722
723 if (len == 20 && expected_len <= INT_MAX)
724 /* some files don't add index entries to chunk size... */
725 len = expected_len;
726 else if (len != expected_len)
727 av_log(s, AV_LOG_WARNING,
728 "Index size %d (%d pkts) is wrong, should be %"PRId64".\n",
729 len, n_pkts, expected_len);
730 if(len < 14)
731 continue;
732 len -= 14; // we already read part of the index header
733 goto skip;
734 } else if (state == MKBETAG('D','A','T','A')) {
735 av_log(s, AV_LOG_WARNING,
736 "DATA tag in middle of chunk, file may be broken.\n");
737 }
738
739 if(state > (unsigned)0xFFFF || state <= 12)
740 continue;
741 len=state - 12;
742 state= 0xFFFFFFFF;
743
744 num = avio_rb16(pb);
745 *timestamp = avio_rb32(pb);
746 mlti_id = (avio_r8(pb) >> 1) - 1;
747 mlti_id = FFMAX(mlti_id, 0) << 16;
748 *flags = avio_r8(pb); /* flags */
749 }
750 for(i=0;i<s->nb_streams;i++) {
751 st = s->streams[i];
752 if (mlti_id + num == st->id)
753 break;
754 }
755 if (i == s->nb_streams) {
756 skip:
757 /* skip packet if unknown number */
758 avio_skip(pb, len);
759 rm->remaining_len = 0;
760 continue;
761 }
762 *stream_index= i;
763
764 return len;
765 }
766 return -1;
767 }
768
769 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
770 RMDemuxContext *rm, RMStream *vst,
771 AVPacket *pkt, int len, int *pseq,
772 int64_t *timestamp)
773 {
774 int hdr;
775 int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silence compiler warning
776 int type;
777 int ret;
778
779 hdr = avio_r8(pb); len--;
780 type = hdr >> 6;
781
782 if(type != 3){ // not frame as a part of packet
783 seq = avio_r8(pb); len--;
784 }
785 if(type != 1){ // not whole frame
786 len2 = get_num(pb, &len);
787 pos = get_num(pb, &len);
788 pic_num = avio_r8(pb); len--;
789 }
790 if(len<0) {
791 av_log(s, AV_LOG_ERROR, "Insufficient data\n");
792 return -1;
793 }
794 rm->remaining_len = len;
795 if(type&1){ // frame, not slice
796 if(type == 3){ // frame as a part of packet
797 len= len2;
798 *timestamp = pos;
799 }
800 if(rm->remaining_len < len) {
801 av_log(s, AV_LOG_ERROR, "Insufficient remaining len\n");
802 return -1;
803 }
804 rm->remaining_len -= len;
805 if ((ret = av_new_packet(pkt, len + 9)) < 0)
806 return ret;
807 pkt->data[0] = 0;
808 AV_WL32(pkt->data + 1, 1);
809 AV_WL32(pkt->data + 5, 0);
810 if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
811 av_packet_unref(pkt);
812 av_log(s, AV_LOG_ERROR, "Failed to read %d bytes\n", len);
813 return ret < 0 ? ret : AVERROR(EIO);
814 }
815 return 0;
816 }
817 //now we have to deal with single slice
818
819 *pseq = seq;
820 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
821 if (len2 > ffio_limit(pb, len2)) {
822 av_log(s, AV_LOG_ERROR, "Impossibly sized packet\n");
823 return AVERROR_INVALIDDATA;
824 }
825 vst->slices = ((hdr & 0x3F) << 1) + 1;
826 vst->videobufsize = len2 + 8*vst->slices + 1;
827 av_packet_unref(&vst->pkt); //FIXME this should be output.
828 if ((ret = av_new_packet(&vst->pkt, vst->videobufsize)) < 0)
829 return ret;
830 memset(vst->pkt.data, 0, vst->pkt.size);
831 vst->videobufpos = 8*vst->slices + 1;
832 vst->cur_slice = 0;
833 vst->curpic_num = pic_num;
834 vst->pktpos = avio_tell(pb);
835 }
836 if(type == 2)
837 len = FFMIN(len, pos);
838
839 if(++vst->cur_slice > vst->slices) {
840 av_log(s, AV_LOG_ERROR, "cur slice %d, too large\n", vst->cur_slice);
841 return 1;
842 }
843 if(!vst->pkt.data)
844 return AVERROR(ENOMEM);
845 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
846 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
847 if(vst->videobufpos + len > vst->videobufsize) {
848 av_log(s, AV_LOG_ERROR, "outside videobufsize\n");
849 return 1;
850 }
851 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
852 return AVERROR(EIO);
853 vst->videobufpos += len;
854 rm->remaining_len-= len;
855
856 if (type == 2 || vst->videobufpos == vst->videobufsize) {
857 vst->pkt.data[0] = vst->cur_slice-1;
858 av_packet_move_ref(pkt, &vst->pkt);
859 if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
860 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
861 vst->videobufpos - 1 - 8*vst->slices);
862 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
863 pkt->pts = AV_NOPTS_VALUE;
864 pkt->pos = vst->pktpos;
865 vst->slices = 0;
866 return 0;
867 }
868
869 return 1;
870 }
871
872 static inline void
873 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
874 {
875 uint8_t *ptr;
876 int j;
877
878 if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
879 ptr = pkt->data;
880 for (j=0;j<pkt->size;j+=2) {
881 FFSWAP(int, ptr[0], ptr[1]);
882 ptr += 2;
883 }
884 }
885 }
886
887 static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n) {
888 int ret = avio_read(pb, dst, n);
889 if (ret != n) {
890 if (ret >= 0) memset(dst + ret, 0, n - ret);
891 else memset(dst , 0, n);
892 av_log(s, AV_LOG_ERROR, "Failed to fully read block\n");
893 }
894 return ret;
895 }
896
897 int
898 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
899 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
900 int *seq, int flags, int64_t timestamp)
901 {
902 RMDemuxContext *rm = s->priv_data;
903 int ret;
904
905 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
906 rm->current_stream= st->id;
907 ret = rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp);
908 if(ret)
909 return ret < 0 ? ret : -1; //got partial frame or error
910 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
911 if ((ast->deint_id == DEINT_ID_GENR) ||
912 (ast->deint_id == DEINT_ID_INT4) ||
913 (ast->deint_id == DEINT_ID_SIPR)) {
914 int x;
915 int sps = ast->sub_packet_size;
916 int cfs = ast->coded_framesize;
917 int h = ast->sub_packet_h;
918 int y = ast->sub_packet_cnt;
919 int w = ast->audio_framesize;
920
921 if (flags & 2)
922 y = ast->sub_packet_cnt = 0;
923 if (!y)
924 ast->audiotimestamp = timestamp;
925
926 switch (ast->deint_id) {
927 case DEINT_ID_INT4:
928 for (x = 0; x < h/2; x++)
929 readfull(s, pb, ast->pkt.data+x*2*w+y*cfs, cfs);
930 break;
931 case DEINT_ID_GENR:
932 for (x = 0; x < w/sps; x++)
933 readfull(s, pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
934 break;
935 case DEINT_ID_SIPR:
936 readfull(s, pb, ast->pkt.data + y * w, w);
937 break;
938 }
939
940 if (++(ast->sub_packet_cnt) < h)
941 return -1;
942 if (ast->deint_id == DEINT_ID_SIPR)
943 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
944
945 ast->sub_packet_cnt = 0;
946 rm->audio_stream_num = st->index;
947 if (st->codecpar->block_align <= 0) {
948 av_log(s, AV_LOG_ERROR, "Invalid block alignment %d\n", st->codecpar->block_align);
949 return AVERROR_INVALIDDATA;
950 }
951 rm->audio_pkt_cnt = h * w / st->codecpar->block_align;
952 } else if ((ast->deint_id == DEINT_ID_VBRF) ||
953 (ast->deint_id == DEINT_ID_VBRS)) {
954 int x;
955 rm->audio_stream_num = st->index;
956 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
957 if (ast->sub_packet_cnt) {
958 for (x = 0; x < ast->sub_packet_cnt; x++)
959 ast->sub_packet_lengths[x] = avio_rb16(pb);
960 rm->audio_pkt_cnt = ast->sub_packet_cnt;
961 ast->audiotimestamp = timestamp;
962 } else
963 return -1;
964 } else {
965 ret = av_get_packet(pb, pkt, len);
966 if (ret < 0)
967 return ret;
968 rm_ac3_swap_bytes(st, pkt);
969 }
970 } else {
971 ret = av_get_packet(pb, pkt, len);
972 if (ret < 0)
973 return ret;
974 }
975
976 pkt->stream_index = st->index;
977
978 pkt->pts = timestamp;
979 if (flags & 2)
980 pkt->flags |= AV_PKT_FLAG_KEY;
981
982 return st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
983 }
984
985 int
986 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
987 AVStream *st, RMStream *ast, AVPacket *pkt)
988 {
989 RMDemuxContext *rm = s->priv_data;
990 int ret;
991
992 av_assert0 (rm->audio_pkt_cnt > 0);
993
994 if (ast->deint_id == DEINT_ID_VBRF ||
995 ast->deint_id == DEINT_ID_VBRS) {
996 ret = av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
997 if (ret < 0)
998 return ret;
999 } else {
1000 ret = av_new_packet(pkt, st->codecpar->block_align);
1001 if (ret < 0)
1002 return ret;
1003 memcpy(pkt->data, ast->pkt.data + st->codecpar->block_align * //FIXME avoid this
1004 (ast->sub_packet_h * ast->audio_framesize / st->codecpar->block_align - rm->audio_pkt_cnt),
1005 st->codecpar->block_align);
1006 }
1007 rm->audio_pkt_cnt--;
1008 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
1009 ast->audiotimestamp = AV_NOPTS_VALUE;
1010 pkt->flags = AV_PKT_FLAG_KEY;
1011 } else
1012 pkt->flags = 0;
1013 pkt->stream_index = st->index;
1014
1015 return rm->audio_pkt_cnt;
1016 }
1017
1018 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
1019 {
1020 RMDemuxContext *rm = s->priv_data;
1021 AVStream *st = NULL; // init to silence compiler warning
1022 int i, res, seq = 1;
1023 int64_t timestamp, pos, len;
1024 int flags;
1025
1026 for (;;) {
1027 if (rm->audio_pkt_cnt) {
1028 // If there are queued audio packet return them first
1029 st = s->streams[rm->audio_stream_num];
1030 res = ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
1031 if(res < 0)
1032 return res;
1033 flags = 0;
1034 } else {
1035 if (rm->old_format) {
1036 RMStream *ast;
1037
1038 st = s->streams[0];
1039 ast = st->priv_data;
1040 timestamp = AV_NOPTS_VALUE;
1041 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
1042 ast->coded_framesize * (int64_t)ast->sub_packet_h / 2;
1043 if (len > INT_MAX)
1044 return AVERROR_INVALIDDATA;
1045 flags = (seq++ == 1) ? 2 : 0;
1046 pos = avio_tell(s->pb);
1047 } else {
1048 len = rm_sync(s, &timestamp, &flags, &i, &pos);
1049 if (len > 0)
1050 st = s->streams[i];
1051 }
1052
1053 if (avio_feof(s->pb))
1054 return AVERROR_EOF;
1055 if (len <= 0)
1056 return AVERROR(EIO);
1057
1058 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
1059 &seq, flags, timestamp);
1060 if (res < -1)
1061 return res;
1062 if((flags&2) && (seq&0x7F) == 1)
1063 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
1064 if (res)
1065 continue;
1066 }
1067
1068 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
1069 || st->discard >= AVDISCARD_ALL){
1070 av_packet_unref(pkt);
1071 } else
1072 break;
1073 }
1074
1075 return 0;
1076 }
1077
1078 static int rm_read_close(AVFormatContext *s)
1079 {
1080 int i;
1081
1082 for (i=0;i<s->nb_streams;i++)
1083 ff_rm_free_rmstream(s->streams[i]->priv_data);
1084
1085 return 0;
1086 }
1087
1088 static int rm_probe(const AVProbeData *p)
1089 {
1090 /* check file header */
1091 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1092 p->buf[2] == 'M' && p->buf[3] == 'F' &&
1093 p->buf[4] == 0 && p->buf[5] == 0) ||
1094 (p->buf[0] == '.' && p->buf[1] == 'r' &&
1095 p->buf[2] == 'a' && p->buf[3] == 0xfd))
1096 return AVPROBE_SCORE_MAX;
1097 else
1098 return 0;
1099 }
1100
1101 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1102 int64_t *ppos, int64_t pos_limit)
1103 {
1104 RMDemuxContext *rm = s->priv_data;
1105 int64_t pos, dts;
1106 int stream_index2, flags, len, h;
1107
1108 pos = *ppos;
1109
1110 if(rm->old_format)
1111 return AV_NOPTS_VALUE;
1112
1113 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1114 return AV_NOPTS_VALUE;
1115
1116 rm->remaining_len=0;
1117 for(;;){
1118 int seq=1;
1119 AVStream *st;
1120
1121 len = rm_sync(s, &dts, &flags, &stream_index2, &pos);
1122 if(len<0)
1123 return AV_NOPTS_VALUE;
1124
1125 st = s->streams[stream_index2];
1126 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1127 h= avio_r8(s->pb); len--;
1128 if(!(h & 0x40)){
1129 seq = avio_r8(s->pb); len--;
1130 }
1131 }
1132
1133 if((flags&2) && (seq&0x7F) == 1){
1134 av_log(s, AV_LOG_TRACE, "%d %d-%d %"PRId64" %d\n",
1135 flags, stream_index2, stream_index, dts, seq);
1136 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1137 if(stream_index2 == stream_index)
1138 break;
1139 }
1140
1141 avio_skip(s->pb, len);
1142 }
1143 *ppos = pos;
1144 return dts;
1145 }
1146
1147 static int rm_read_seek(AVFormatContext *s, int stream_index,
1148 int64_t pts, int flags)
1149 {
1150 RMDemuxContext *rm = s->priv_data;
1151
1152 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1153 return -1;
1154 rm->audio_pkt_cnt = 0;
1155 return 0;
1156 }
1157
1158
1159 AVInputFormat ff_rm_demuxer = {
1160 .name = "rm",
1161 .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
1162 .priv_data_size = sizeof(RMDemuxContext),
1163 .read_probe = rm_probe,
1164 .read_header = rm_read_header,
1165 .read_packet = rm_read_packet,
1166 .read_close = rm_read_close,
1167 .read_timestamp = rm_read_dts,
1168 .read_seek = rm_read_seek,
1169 };
1170
1171 AVInputFormat ff_rdt_demuxer = {
1172 .name = "rdt",
1173 .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
1174 .priv_data_size = sizeof(RMDemuxContext),
1175 .read_close = rm_read_close,
1176 .flags = AVFMT_NOFILE,
1177 };
1178
1179 static int ivr_probe(const AVProbeData *p)
1180 {
1181 if (memcmp(p->buf, ".R1M\x0\x1\x1", 7) &&
1182 memcmp(p->buf, ".REC", 4))
1183 return 0;
1184
1185 return AVPROBE_SCORE_MAX;
1186 }
1187
1188 static int ivr_read_header(AVFormatContext *s)
1189 {
1190 unsigned tag, type, len, tlen, value;
1191 int i, j, n, count, nb_streams = 0, ret;
1192 uint8_t key[256], val[256];
1193 AVIOContext *pb = s->pb;
1194 AVStream *st;
1195 int64_t pos, offset=0, temp;
1196
1197 pos = avio_tell(pb);
1198 tag = avio_rl32(pb);
1199 if (tag == MKTAG('.','R','1','M')) {
1200 if (avio_rb16(pb) != 1)
1201 return AVERROR_INVALIDDATA;
1202 if (avio_r8(pb) != 1)
1203 return AVERROR_INVALIDDATA;
1204 len = avio_rb32(pb);
1205 avio_skip(pb, len);
1206 avio_skip(pb, 5);
1207 temp = avio_rb64(pb);
1208 while (!avio_feof(pb) && temp) {
1209 offset = temp;
1210 temp = avio_rb64(pb);
1211 }
1212 if (offset <= 0)
1213 return AVERROR_INVALIDDATA;
1214 avio_skip(pb, offset - avio_tell(pb));
1215 if (avio_r8(pb) != 1)
1216 return AVERROR_INVALIDDATA;
1217 len = avio_rb32(pb);
1218 avio_skip(pb, len);
1219 if (avio_r8(pb) != 2)
1220 return AVERROR_INVALIDDATA;
1221 avio_skip(pb, 16);
1222 pos = avio_tell(pb);
1223 tag = avio_rl32(pb);
1224 }
1225
1226 if (tag != MKTAG('.','R','E','C'))
1227 return AVERROR_INVALIDDATA;
1228
1229 if (avio_r8(pb) != 0)
1230 return AVERROR_INVALIDDATA;
1231 count = avio_rb32(pb);
1232 for (i = 0; i < count; i++) {
1233 if (avio_feof(pb))
1234 return AVERROR_INVALIDDATA;
1235
1236 type = avio_r8(pb);
1237 tlen = avio_rb32(pb);
1238 avio_get_str(pb, tlen, key, sizeof(key));
1239 len = avio_rb32(pb);
1240 if (type == 5) {
1241 avio_get_str(pb, len, val, sizeof(val));
1242 av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1243 } else if (type == 4) {
1244 av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1245 for (j = 0; j < len; j++) {
1246 if (avio_feof(pb))
1247 return AVERROR_INVALIDDATA;
1248 av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1249 }
1250 av_log(s, AV_LOG_DEBUG, "'\n");
1251 } else if (len == 4 && type == 3 && !strncmp(key, "StreamCount", tlen)) {
1252 nb_streams = value = avio_rb32(pb);
1253 } else if (len == 4 && type == 3) {
1254 value = avio_rb32(pb);
1255 av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1256 } else {
1257 av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1258 avio_skip(pb, len);
1259 }
1260 }
1261
1262 for (n = 0; n < nb_streams; n++) {
1263 st = avformat_new_stream(s, NULL);
1264 if (!st)
1265 return AVERROR(ENOMEM);
1266 st->priv_data = ff_rm_alloc_rmstream();
1267 if (!st->priv_data)
1268 return AVERROR(ENOMEM);
1269
1270 if (avio_r8(pb) != 1)
1271 return AVERROR_INVALIDDATA;
1272
1273 count = avio_rb32(pb);
1274 for (i = 0; i < count; i++) {
1275 if (avio_feof(pb))
1276 return AVERROR_INVALIDDATA;
1277
1278 type = avio_r8(pb);
1279 tlen = avio_rb32(pb);
1280 avio_get_str(pb, tlen, key, sizeof(key));
1281 len = avio_rb32(pb);
1282 if (type == 5) {
1283 avio_get_str(pb, len, val, sizeof(val));
1284 av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1285 } else if (type == 4 && !strncmp(key, "OpaqueData", tlen)) {
1286 ret = ffio_ensure_seekback(pb, 4);
1287 if (ret < 0)
1288 return ret;
1289 if (avio_rb32(pb) == MKBETAG('M', 'L', 'T', 'I')) {
1290 ret = rm_read_multi(s, pb, st, NULL);
1291 } else {
1292 if (avio_feof(pb))
1293 return AVERROR_INVALIDDATA;
1294 avio_seek(pb, -4, SEEK_CUR);
1295 ret = ff_rm_read_mdpr_codecdata(s, pb, st, st->priv_data, len, NULL);
1296 }
1297
1298 if (ret < 0)
1299 return ret;
1300 } else if (type == 4) {
1301 int j;
1302
1303 av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1304 for (j = 0; j < len; j++) {
1305 if (avio_feof(pb))
1306 return AVERROR_INVALIDDATA;
1307 av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1308 }
1309 av_log(s, AV_LOG_DEBUG, "'\n");
1310 } else if (len == 4 && type == 3 && !strncmp(key, "Duration", tlen)) {
1311 st->duration = avio_rb32(pb);
1312 } else if (len == 4 && type == 3) {
1313 value = avio_rb32(pb);
1314 av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1315 } else {
1316 av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1317 avio_skip(pb, len);
1318 }
1319 }
1320 }
1321
1322 if (avio_r8(pb) != 6)
1323 return AVERROR_INVALIDDATA;
1324 avio_skip(pb, 12);
1325 avio_skip(pb, avio_rb64(pb) + pos - avio_tell(s->pb));
1326 if (avio_r8(pb) != 8)
1327 return AVERROR_INVALIDDATA;
1328 avio_skip(pb, 8);
1329
1330 return 0;
1331 }
1332
1333 static int ivr_read_packet(AVFormatContext *s, AVPacket *pkt)
1334 {
1335 RMDemuxContext *rm = s->priv_data;
1336 int ret = AVERROR_EOF, opcode;
1337 AVIOContext *pb = s->pb;
1338 unsigned size, index;
1339 int64_t pos, pts;
1340
1341 if (avio_feof(pb) || rm->data_end)
1342 return AVERROR_EOF;
1343
1344 pos = avio_tell(pb);
1345
1346 for (;;) {
1347 if (rm->audio_pkt_cnt) {
1348 // If there are queued audio packet return them first
1349 AVStream *st;
1350
1351 st = s->streams[rm->audio_stream_num];
1352 ret = ff_rm_retrieve_cache(s, pb, st, st->priv_data, pkt);
1353 if (ret < 0) {
1354 return ret;
1355 }
1356 } else {
1357 if (rm->remaining_len) {
1358 avio_skip(pb, rm->remaining_len);
1359 rm->remaining_len = 0;
1360 }
1361
1362 if (avio_feof(pb))
1363 return AVERROR_EOF;
1364
1365 opcode = avio_r8(pb);
1366 if (opcode == 2) {
1367 AVStream *st;
1368 int seq = 1;
1369
1370 pts = avio_rb32(pb);
1371 index = avio_rb16(pb);
1372 if (index >= s->nb_streams)
1373 return AVERROR_INVALIDDATA;
1374
1375 avio_skip(pb, 4);
1376 size = avio_rb32(pb);
1377 avio_skip(pb, 4);
1378
1379 if (size < 1 || size > INT_MAX/4) {
1380 av_log(s, AV_LOG_ERROR, "size %u is invalid\n", size);
1381 return AVERROR_INVALIDDATA;
1382 }
1383
1384 st = s->streams[index];
1385 ret = ff_rm_parse_packet(s, pb, st, st->priv_data, size, pkt,
1386 &seq, 0, pts);
1387 if (ret < -1) {
1388 return ret;
1389 } else if (ret) {
1390 continue;
1391 }
1392
1393 pkt->pos = pos;
1394 pkt->pts = pts;
1395 pkt->stream_index = index;
1396 } else if (opcode == 7) {
1397 pos = avio_rb64(pb);
1398 if (!pos) {
1399 rm->data_end = 1;
1400 return AVERROR_EOF;
1401 }
1402 } else {
1403 av_log(s, AV_LOG_ERROR, "Unsupported opcode=%d at %"PRIX64"\n", opcode, avio_tell(pb) - 1);
1404 return AVERROR(EIO);
1405 }
1406 }
1407
1408 break;
1409 }
1410
1411 return ret;
1412 }
1413
1414 AVInputFormat ff_ivr_demuxer = {
1415 .name = "ivr",
1416 .long_name = NULL_IF_CONFIG_SMALL("IVR (Internet Video Recording)"),
1417 .priv_data_size = sizeof(RMDemuxContext),
1418 .read_probe = ivr_probe,
1419 .read_header = ivr_read_header,
1420 .read_packet = ivr_read_packet,
1421 .read_close = rm_read_close,
1422 .extensions = "ivr",
1423 };