FFmpeg coverage


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