FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rmdec.c
Date: 2026-05-05 22:00:13
Exec Total Coverage
Lines: 601 913 65.8%
Functions: 24 27 88.9%
Branches: 314 579 54.2%

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