FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rmdec.c
Date: 2024-04-24 02:45:42
Exec Total Coverage
Lines: 584 896 65.2%
Functions: 24 27 88.9%
Branches: 289 547 52.8%

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