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