fbc4d0cee6bb52068df43b399696581464146e54
[libav.git] / libavformat / rmdec.c
1 /*
2 * "Real" compatible demuxer.
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "avformat.h"
26 #include "riff.h"
27 #include "rm.h"
28
29 struct RMStream {
30 AVPacket pkt; ///< place to store merged video frame / reordered audio data
31 int videobufsize; ///< current assembled frame size
32 int videobufpos; ///< position for the next slice in the video buffer
33 int curpic_num; ///< picture number of current frame
34 int cur_slice, slices;
35 int64_t pktpos; ///< first slice position in file
36 /// Audio descrambling matrix parameters
37 int64_t audiotimestamp; ///< Audio packet timestamp
38 int sub_packet_cnt; // Subpacket counter, used while reading
39 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
40 int audio_framesize; /// Audio frame size from container
41 int sub_packet_lengths[16]; /// Length of each subpacket
42 };
43
44 typedef struct {
45 int nb_packets;
46 int old_format;
47 int current_stream;
48 int remaining_len;
49 int audio_stream_num; ///< Stream number for audio packets
50 int audio_pkt_cnt; ///< Output packet counter
51 } RMDemuxContext;
52
53 static const unsigned char sipr_swaps[38][2] = {
54 { 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 },
55 { 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 },
56 { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
57 { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
58 { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
59 { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
60 { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
61 { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
62 { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
63 { 67, 83 }, { 77, 80 }
64 };
65
66 const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 };
67
68 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
69 {
70 int i;
71 char *q, r;
72
73 q = buf;
74 for(i=0;i<len;i++) {
75 r = avio_r8(pb);
76 if (i < buf_size - 1)
77 *q++ = r;
78 }
79 if (buf_size > 0) *q = '\0';
80 }
81
82 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
83 {
84 get_strl(pb, buf, buf_size, avio_r8(pb));
85 }
86
87 static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
88 {
89 if (size >= 1<<24)
90 return -1;
91 avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
92 if (!avctx->extradata)
93 return AVERROR(ENOMEM);
94 avctx->extradata_size = avio_read(pb, avctx->extradata, size);
95 memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
96 if (avctx->extradata_size != size)
97 return AVERROR(EIO);
98 return 0;
99 }
100
101 static void rm_read_metadata(AVFormatContext *s, int wide)
102 {
103 char buf[1024];
104 int i;
105 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
106 int len = wide ? avio_rb16(s->pb) : avio_r8(s->pb);
107 get_strl(s->pb, buf, sizeof(buf), len);
108 av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
109 }
110 }
111
112 RMStream *ff_rm_alloc_rmstream (void)
113 {
114 RMStream *rms = av_mallocz(sizeof(RMStream));
115 rms->curpic_num = -1;
116 return rms;
117 }
118
119 void ff_rm_free_rmstream (RMStream *rms)
120 {
121 av_free_packet(&rms->pkt);
122 }
123
124 static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
125 AVStream *st, RMStream *ast, int read_all)
126 {
127 char buf[256];
128 uint32_t version;
129 int ret;
130
131 /* ra type header */
132 version = avio_rb16(pb); /* version */
133 if (version == 3) {
134 int header_size = avio_rb16(pb);
135 int64_t startpos = avio_tell(pb);
136 avio_skip(pb, 14);
137 rm_read_metadata(s, 0);
138 if ((startpos + header_size) >= avio_tell(pb) + 2) {
139 // fourcc (should always be "lpcJ")
140 avio_r8(pb);
141 get_str8(pb, buf, sizeof(buf));
142 }
143 // Skip extra header crap (this should never happen)
144 if ((startpos + header_size) > avio_tell(pb))
145 avio_skip(pb, header_size + startpos - avio_tell(pb));
146 st->codec->sample_rate = 8000;
147 st->codec->channels = 1;
148 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
149 st->codec->codec_id = CODEC_ID_RA_144;
150 } else {
151 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
152 int codecdata_length;
153 /* old version (4) */
154 avio_skip(pb, 2); /* unused */
155 avio_rb32(pb); /* .ra4 */
156 avio_rb32(pb); /* data size */
157 avio_rb16(pb); /* version2 */
158 avio_rb32(pb); /* header size */
159 flavor= avio_rb16(pb); /* add codec info / flavor */
160 ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
161 avio_rb32(pb); /* ??? */
162 avio_rb32(pb); /* ??? */
163 avio_rb32(pb); /* ??? */
164 ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
165 st->codec->block_align= avio_rb16(pb); /* frame size */
166 ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
167 avio_rb16(pb); /* ??? */
168 if (version == 5) {
169 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
170 }
171 st->codec->sample_rate = avio_rb16(pb);
172 avio_rb32(pb);
173 st->codec->channels = avio_rb16(pb);
174 if (version == 5) {
175 avio_rb32(pb);
176 avio_read(pb, buf, 4);
177 buf[4] = 0;
178 } else {
179 get_str8(pb, buf, sizeof(buf)); /* desc */
180 get_str8(pb, buf, sizeof(buf)); /* desc */
181 }
182 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
183 st->codec->codec_tag = AV_RL32(buf);
184 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
185 st->codec->codec_tag);
186 switch (st->codec->codec_id) {
187 case CODEC_ID_AC3:
188 st->need_parsing = AVSTREAM_PARSE_FULL;
189 break;
190 case CODEC_ID_RA_288:
191 st->codec->extradata_size= 0;
192 ast->audio_framesize = st->codec->block_align;
193 st->codec->block_align = coded_framesize;
194
195 if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
196 av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
197 return -1;
198 }
199
200 av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
201 break;
202 case CODEC_ID_COOK:
203 case CODEC_ID_ATRAC3:
204 case CODEC_ID_SIPR:
205 avio_rb16(pb); avio_r8(pb);
206 if (version == 5)
207 avio_r8(pb);
208 codecdata_length = avio_rb32(pb);
209 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
210 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
211 return -1;
212 }
213
214 ast->audio_framesize = st->codec->block_align;
215 if (st->codec->codec_id == CODEC_ID_SIPR) {
216 if (flavor > 3) {
217 av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
218 flavor);
219 return -1;
220 }
221 st->codec->block_align = ff_sipr_subpk_size[flavor];
222 } else {
223 if(sub_packet_size <= 0){
224 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
225 return -1;
226 }
227 st->codec->block_align = ast->sub_packet_size;
228 }
229 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
230 return ret;
231
232 if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
233 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
234 return -1;
235 }
236
237 av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
238 break;
239 case CODEC_ID_AAC:
240 avio_rb16(pb); avio_r8(pb);
241 if (version == 5)
242 avio_r8(pb);
243 codecdata_length = avio_rb32(pb);
244 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
245 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
246 return -1;
247 }
248 if (codecdata_length >= 1) {
249 avio_r8(pb);
250 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
251 return ret;
252 }
253 break;
254 default:
255 av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
256 }
257 if (read_all) {
258 avio_r8(pb);
259 avio_r8(pb);
260 avio_r8(pb);
261 rm_read_metadata(s, 0);
262 }
263 }
264 return 0;
265 }
266
267 int
268 ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
269 AVStream *st, RMStream *rst, int codec_data_size)
270 {
271 unsigned int v;
272 int size;
273 int64_t codec_pos;
274 int ret;
275
276 av_set_pts_info(st, 64, 1, 1000);
277 codec_pos = avio_tell(pb);
278 v = avio_rb32(pb);
279 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
280 /* ra type header */
281 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
282 return -1;
283 } else {
284 int fps;
285 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
286 fail1:
287 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
288 goto skip;
289 }
290 st->codec->codec_tag = avio_rl32(pb);
291 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
292 st->codec->codec_tag);
293 // av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
294 if (st->codec->codec_id == CODEC_ID_NONE)
295 goto fail1;
296 st->codec->width = avio_rb16(pb);
297 st->codec->height = avio_rb16(pb);
298 st->codec->time_base.num= 1;
299 fps= avio_rb16(pb);
300 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
301 avio_rb32(pb);
302 avio_skip(pb, 2);
303 avio_rb16(pb);
304
305 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
306 return ret;
307
308 // av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
309 st->codec->time_base.den = fps * st->codec->time_base.num;
310 //XXX: do we really need that?
311 switch(st->codec->extradata[4]>>4){
312 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
313 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
314 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
315 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
316 default:
317 av_log(st->codec, AV_LOG_ERROR, "extra:%02X %02X %02X %02X %02X\n", st->codec->extradata[0], st->codec->extradata[1], st->codec->extradata[2], st->codec->extradata[3], st->codec->extradata[4]);
318 goto fail1;
319 }
320 }
321
322 skip:
323 /* skip codec info */
324 size = avio_tell(pb) - codec_pos;
325 avio_skip(pb, codec_data_size - size);
326
327 return 0;
328 }
329
330 /** this function assumes that the demuxer has already seeked to the start
331 * of the INDX chunk, and will bail out if not. */
332 static int rm_read_index(AVFormatContext *s)
333 {
334 AVIOContext *pb = s->pb;
335 unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
336 AVStream *st;
337
338 do {
339 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
340 return -1;
341 size = avio_rb32(pb);
342 if (size < 20)
343 return -1;
344 avio_skip(pb, 2);
345 n_pkts = avio_rb32(pb);
346 str_id = avio_rb16(pb);
347 next_off = avio_rb32(pb);
348 for (n = 0; n < s->nb_streams; n++)
349 if (s->streams[n]->id == str_id) {
350 st = s->streams[n];
351 break;
352 }
353 if (n == s->nb_streams)
354 goto skip;
355
356 for (n = 0; n < n_pkts; n++) {
357 avio_skip(pb, 2);
358 pts = avio_rb32(pb);
359 pos = avio_rb32(pb);
360 avio_skip(pb, 4); /* packet no. */
361
362 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
363 }
364
365 skip:
366 if (next_off && avio_tell(pb) != next_off &&
367 avio_seek(pb, next_off, SEEK_SET) < 0)
368 return -1;
369 } while (next_off);
370
371 return 0;
372 }
373
374 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
375 {
376 RMDemuxContext *rm = s->priv_data;
377 AVStream *st;
378
379 rm->old_format = 1;
380 st = av_new_stream(s, 0);
381 if (!st)
382 return -1;
383 st->priv_data = ff_rm_alloc_rmstream();
384 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
385 }
386
387 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
388 {
389 RMDemuxContext *rm = s->priv_data;
390 AVStream *st;
391 AVIOContext *pb = s->pb;
392 unsigned int tag;
393 int tag_size;
394 unsigned int start_time, duration;
395 unsigned int data_off = 0, indx_off = 0;
396 char buf[128];
397 int flags = 0;
398
399 tag = avio_rl32(pb);
400 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
401 /* very old .ra format */
402 return rm_read_header_old(s, ap);
403 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
404 return AVERROR(EIO);
405 }
406
407 avio_rb32(pb); /* header size */
408 avio_rb16(pb);
409 avio_rb32(pb);
410 avio_rb32(pb); /* number of headers */
411
412 for(;;) {
413 if (pb->eof_reached)
414 return -1;
415 tag = avio_rl32(pb);
416 tag_size = avio_rb32(pb);
417 avio_rb16(pb);
418 av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
419 (tag ) & 0xff,
420 (tag >> 8) & 0xff,
421 (tag >> 16) & 0xff,
422 (tag >> 24) & 0xff,
423 tag,
424 tag_size);
425 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
426 return -1;
427 switch(tag) {
428 case MKTAG('P', 'R', 'O', 'P'):
429 /* file header */
430 avio_rb32(pb); /* max bit rate */
431 avio_rb32(pb); /* avg bit rate */
432 avio_rb32(pb); /* max packet size */
433 avio_rb32(pb); /* avg packet size */
434 avio_rb32(pb); /* nb packets */
435 avio_rb32(pb); /* duration */
436 avio_rb32(pb); /* preroll */
437 indx_off = avio_rb32(pb); /* index offset */
438 data_off = avio_rb32(pb); /* data offset */
439 avio_rb16(pb); /* nb streams */
440 flags = avio_rb16(pb); /* flags */
441 break;
442 case MKTAG('C', 'O', 'N', 'T'):
443 rm_read_metadata(s, 1);
444 break;
445 case MKTAG('M', 'D', 'P', 'R'):
446 st = av_new_stream(s, 0);
447 if (!st)
448 return AVERROR(ENOMEM);
449 st->id = avio_rb16(pb);
450 avio_rb32(pb); /* max bit rate */
451 st->codec->bit_rate = avio_rb32(pb); /* bit rate */
452 avio_rb32(pb); /* max packet size */
453 avio_rb32(pb); /* avg packet size */
454 start_time = avio_rb32(pb); /* start time */
455 avio_rb32(pb); /* preroll */
456 duration = avio_rb32(pb); /* duration */
457 st->start_time = start_time;
458 st->duration = duration;
459 get_str8(pb, buf, sizeof(buf)); /* desc */
460 get_str8(pb, buf, sizeof(buf)); /* mimetype */
461 st->codec->codec_type = AVMEDIA_TYPE_DATA;
462 st->priv_data = ff_rm_alloc_rmstream();
463 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
464 avio_rb32(pb)) < 0)
465 return -1;
466 break;
467 case MKTAG('D', 'A', 'T', 'A'):
468 goto header_end;
469 default:
470 /* unknown tag: skip it */
471 avio_skip(pb, tag_size - 10);
472 break;
473 }
474 }
475 header_end:
476 rm->nb_packets = avio_rb32(pb); /* number of packets */
477 if (!rm->nb_packets && (flags & 4))
478 rm->nb_packets = 3600 * 25;
479 avio_rb32(pb); /* next data header */
480
481 if (!data_off)
482 data_off = avio_tell(pb) - 18;
483 if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
484 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
485 rm_read_index(s);
486 avio_seek(pb, data_off + 18, SEEK_SET);
487 }
488
489 return 0;
490 }
491
492 static int get_num(AVIOContext *pb, int *len)
493 {
494 int n, n1;
495
496 n = avio_rb16(pb);
497 (*len)-=2;
498 n &= 0x7FFF;
499 if (n >= 0x4000) {
500 return n - 0x4000;
501 } else {
502 n1 = avio_rb16(pb);
503 (*len)-=2;
504 return (n << 16) | n1;
505 }
506 }
507
508 /* multiple of 20 bytes for ra144 (ugly) */
509 #define RAW_PACKET_SIZE 1000
510
511 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
512 RMDemuxContext *rm = s->priv_data;
513 AVIOContext *pb = s->pb;
514 AVStream *st;
515 uint32_t state=0xFFFFFFFF;
516
517 while(!pb->eof_reached){
518 int len, num, i;
519 *pos= avio_tell(pb) - 3;
520 if(rm->remaining_len > 0){
521 num= rm->current_stream;
522 len= rm->remaining_len;
523 *timestamp = AV_NOPTS_VALUE;
524 *flags= 0;
525 }else{
526 state= (state<<8) + avio_r8(pb);
527
528 if(state == MKBETAG('I', 'N', 'D', 'X')){
529 int n_pkts, expected_len;
530 len = avio_rb32(pb);
531 avio_skip(pb, 2);
532 n_pkts = avio_rb32(pb);
533 expected_len = 20 + n_pkts * 14;
534 if (len == 20)
535 /* some files don't add index entries to chunk size... */
536 len = expected_len;
537 else if (len != expected_len)
538 av_log(s, AV_LOG_WARNING,
539 "Index size %d (%d pkts) is wrong, should be %d.\n",
540 len, n_pkts, expected_len);
541 len -= 14; // we already read part of the index header
542 if(len<0)
543 continue;
544 goto skip;
545 } else if (state == MKBETAG('D','A','T','A')) {
546 av_log(s, AV_LOG_WARNING,
547 "DATA tag in middle of chunk, file may be broken.\n");
548 }
549
550 if(state > (unsigned)0xFFFF || state <= 12)
551 continue;
552 len=state - 12;
553 state= 0xFFFFFFFF;
554
555 num = avio_rb16(pb);
556 *timestamp = avio_rb32(pb);
557 avio_r8(pb); /* reserved */
558 *flags = avio_r8(pb); /* flags */
559 }
560 for(i=0;i<s->nb_streams;i++) {
561 st = s->streams[i];
562 if (num == st->id)
563 break;
564 }
565 if (i == s->nb_streams) {
566 skip:
567 /* skip packet if unknown number */
568 avio_skip(pb, len);
569 rm->remaining_len = 0;
570 continue;
571 }
572 *stream_index= i;
573
574 return len;
575 }
576 return -1;
577 }
578
579 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
580 RMDemuxContext *rm, RMStream *vst,
581 AVPacket *pkt, int len, int *pseq)
582 {
583 int hdr, seq, pic_num, len2, pos;
584 int type;
585
586 hdr = avio_r8(pb); len--;
587 type = hdr >> 6;
588
589 if(type != 3){ // not frame as a part of packet
590 seq = avio_r8(pb); len--;
591 }
592 if(type != 1){ // not whole frame
593 len2 = get_num(pb, &len);
594 pos = get_num(pb, &len);
595 pic_num = avio_r8(pb); len--;
596 }
597 if(len<0)
598 return -1;
599 rm->remaining_len = len;
600 if(type&1){ // frame, not slice
601 if(type == 3) // frame as a part of packet
602 len= len2;
603 if(rm->remaining_len < len)
604 return -1;
605 rm->remaining_len -= len;
606 if(av_new_packet(pkt, len + 9) < 0)
607 return AVERROR(EIO);
608 pkt->data[0] = 0;
609 AV_WL32(pkt->data + 1, 1);
610 AV_WL32(pkt->data + 5, 0);
611 avio_read(pb, pkt->data + 9, len);
612 return 0;
613 }
614 //now we have to deal with single slice
615
616 *pseq = seq;
617 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
618 vst->slices = ((hdr & 0x3F) << 1) + 1;
619 vst->videobufsize = len2 + 8*vst->slices + 1;
620 av_free_packet(&vst->pkt); //FIXME this should be output.
621 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
622 return AVERROR(ENOMEM);
623 vst->videobufpos = 8*vst->slices + 1;
624 vst->cur_slice = 0;
625 vst->curpic_num = pic_num;
626 vst->pktpos = avio_tell(pb);
627 }
628 if(type == 2)
629 len = FFMIN(len, pos);
630
631 if(++vst->cur_slice > vst->slices)
632 return 1;
633 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
634 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
635 if(vst->videobufpos + len > vst->videobufsize)
636 return 1;
637 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
638 return AVERROR(EIO);
639 vst->videobufpos += len;
640 rm->remaining_len-= len;
641
642 if(type == 2 || (vst->videobufpos) == vst->videobufsize){
643 vst->pkt.data[0] = vst->cur_slice-1;
644 *pkt= vst->pkt;
645 vst->pkt.data= NULL;
646 vst->pkt.size= 0;
647 if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
648 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
649 vst->videobufpos - 1 - 8*vst->slices);
650 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
651 pkt->pts = AV_NOPTS_VALUE;
652 pkt->pos = vst->pktpos;
653 vst->slices = 0;
654 return 0;
655 }
656
657 return 1;
658 }
659
660 static inline void
661 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
662 {
663 uint8_t *ptr;
664 int j;
665
666 if (st->codec->codec_id == CODEC_ID_AC3) {
667 ptr = pkt->data;
668 for (j=0;j<pkt->size;j+=2) {
669 FFSWAP(int, ptr[0], ptr[1]);
670 ptr += 2;
671 }
672 }
673 }
674
675 /**
676 * Perform 4-bit block reordering for SIPR data.
677 * @todo This can be optimized, e.g. use memcpy() if data blocks are aligned
678 */
679 void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
680 {
681 int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket
682
683 for (n = 0; n < 38; n++) {
684 int j;
685 int i = bs * sipr_swaps[n][0];
686 int o = bs * sipr_swaps[n][1];
687
688 /* swap 4bit-nibbles of block 'i' with 'o' */
689 for (j = 0; j < bs; j++, i++, o++) {
690 int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
691 y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
692
693 buf[o >> 1] = (x << (4 * (o & 1))) |
694 (buf[o >> 1] & (0xF << (4 * !(o & 1))));
695 buf[i >> 1] = (y << (4 * (i & 1))) |
696 (buf[i >> 1] & (0xF << (4 * !(i & 1))));
697 }
698 }
699 }
700
701 int
702 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
703 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
704 int *seq, int flags, int64_t timestamp)
705 {
706 RMDemuxContext *rm = s->priv_data;
707
708 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
709 rm->current_stream= st->id;
710 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq))
711 return -1; //got partial frame
712 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
713 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
714 (st->codec->codec_id == CODEC_ID_COOK) ||
715 (st->codec->codec_id == CODEC_ID_ATRAC3) ||
716 (st->codec->codec_id == CODEC_ID_SIPR)) {
717 int x;
718 int sps = ast->sub_packet_size;
719 int cfs = ast->coded_framesize;
720 int h = ast->sub_packet_h;
721 int y = ast->sub_packet_cnt;
722 int w = ast->audio_framesize;
723
724 if (flags & 2)
725 y = ast->sub_packet_cnt = 0;
726 if (!y)
727 ast->audiotimestamp = timestamp;
728
729 switch(st->codec->codec_id) {
730 case CODEC_ID_RA_288:
731 for (x = 0; x < h/2; x++)
732 avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
733 break;
734 case CODEC_ID_ATRAC3:
735 case CODEC_ID_COOK:
736 for (x = 0; x < w/sps; x++)
737 avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
738 break;
739 case CODEC_ID_SIPR:
740 avio_read(pb, ast->pkt.data + y * w, w);
741 break;
742 }
743
744 if (++(ast->sub_packet_cnt) < h)
745 return -1;
746 if (st->codec->codec_id == CODEC_ID_SIPR)
747 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
748
749 ast->sub_packet_cnt = 0;
750 rm->audio_stream_num = st->index;
751 rm->audio_pkt_cnt = h * w / st->codec->block_align;
752 } else if (st->codec->codec_id == CODEC_ID_AAC) {
753 int x;
754 rm->audio_stream_num = st->index;
755 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
756 if (ast->sub_packet_cnt) {
757 for (x = 0; x < ast->sub_packet_cnt; x++)
758 ast->sub_packet_lengths[x] = avio_rb16(pb);
759 rm->audio_pkt_cnt = ast->sub_packet_cnt;
760 ast->audiotimestamp = timestamp;
761 } else
762 return -1;
763 } else {
764 av_get_packet(pb, pkt, len);
765 rm_ac3_swap_bytes(st, pkt);
766 }
767 } else
768 av_get_packet(pb, pkt, len);
769
770 pkt->stream_index = st->index;
771
772 #if 0
773 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
774 if(st->codec->codec_id == CODEC_ID_RV20){
775 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
776 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
777
778 seq |= (timestamp&~0x3FFF);
779 if(seq - timestamp > 0x2000) seq -= 0x4000;
780 if(seq - timestamp < -0x2000) seq += 0x4000;
781 }
782 }
783 #endif
784
785 pkt->pts= timestamp;
786 if (flags & 2)
787 pkt->flags |= AV_PKT_FLAG_KEY;
788
789 return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
790 }
791
792 int
793 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
794 AVStream *st, RMStream *ast, AVPacket *pkt)
795 {
796 RMDemuxContext *rm = s->priv_data;
797
798 assert (rm->audio_pkt_cnt > 0);
799
800 if (st->codec->codec_id == CODEC_ID_AAC)
801 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
802 else {
803 av_new_packet(pkt, st->codec->block_align);
804 memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
805 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
806 st->codec->block_align);
807 }
808 rm->audio_pkt_cnt--;
809 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
810 ast->audiotimestamp = AV_NOPTS_VALUE;
811 pkt->flags = AV_PKT_FLAG_KEY;
812 } else
813 pkt->flags = 0;
814 pkt->stream_index = st->index;
815
816 return rm->audio_pkt_cnt;
817 }
818
819 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
820 {
821 RMDemuxContext *rm = s->priv_data;
822 AVStream *st;
823 int i, len, res, seq = 1;
824 int64_t timestamp, pos;
825 int flags;
826
827 for (;;) {
828 if (rm->audio_pkt_cnt) {
829 // If there are queued audio packet return them first
830 st = s->streams[rm->audio_stream_num];
831 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
832 flags = 0;
833 } else {
834 if (rm->old_format) {
835 RMStream *ast;
836
837 st = s->streams[0];
838 ast = st->priv_data;
839 timestamp = AV_NOPTS_VALUE;
840 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
841 ast->coded_framesize * ast->sub_packet_h / 2;
842 flags = (seq++ == 1) ? 2 : 0;
843 pos = avio_tell(s->pb);
844 } else {
845 len=sync(s, &timestamp, &flags, &i, &pos);
846 if (len > 0)
847 st = s->streams[i];
848 }
849
850 if(len<0 || s->pb->eof_reached)
851 return AVERROR(EIO);
852
853 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
854 &seq, flags, timestamp);
855 if((flags&2) && (seq&0x7F) == 1)
856 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
857 if (res)
858 continue;
859 }
860
861 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
862 || st->discard >= AVDISCARD_ALL){
863 av_free_packet(pkt);
864 } else
865 break;
866 }
867
868 return 0;
869 }
870
871 static int rm_read_close(AVFormatContext *s)
872 {
873 int i;
874
875 for (i=0;i<s->nb_streams;i++)
876 ff_rm_free_rmstream(s->streams[i]->priv_data);
877
878 return 0;
879 }
880
881 static int rm_probe(AVProbeData *p)
882 {
883 /* check file header */
884 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
885 p->buf[2] == 'M' && p->buf[3] == 'F' &&
886 p->buf[4] == 0 && p->buf[5] == 0) ||
887 (p->buf[0] == '.' && p->buf[1] == 'r' &&
888 p->buf[2] == 'a' && p->buf[3] == 0xfd))
889 return AVPROBE_SCORE_MAX;
890 else
891 return 0;
892 }
893
894 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
895 int64_t *ppos, int64_t pos_limit)
896 {
897 RMDemuxContext *rm = s->priv_data;
898 int64_t pos, dts;
899 int stream_index2, flags, len, h;
900
901 pos = *ppos;
902
903 if(rm->old_format)
904 return AV_NOPTS_VALUE;
905
906 avio_seek(s->pb, pos, SEEK_SET);
907 rm->remaining_len=0;
908 for(;;){
909 int seq=1;
910 AVStream *st;
911
912 len=sync(s, &dts, &flags, &stream_index2, &pos);
913 if(len<0)
914 return AV_NOPTS_VALUE;
915
916 st = s->streams[stream_index2];
917 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
918 h= avio_r8(s->pb); len--;
919 if(!(h & 0x40)){
920 seq = avio_r8(s->pb); len--;
921 }
922 }
923
924 if((flags&2) && (seq&0x7F) == 1){
925 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
926 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
927 if(stream_index2 == stream_index)
928 break;
929 }
930
931 avio_skip(s->pb, len);
932 }
933 *ppos = pos;
934 return dts;
935 }
936
937 AVInputFormat ff_rm_demuxer = {
938 "rm",
939 NULL_IF_CONFIG_SMALL("RealMedia format"),
940 sizeof(RMDemuxContext),
941 rm_probe,
942 rm_read_header,
943 rm_read_packet,
944 rm_read_close,
945 NULL,
946 rm_read_dts,
947 };
948
949 AVInputFormat ff_rdt_demuxer = {
950 "rdt",
951 NULL_IF_CONFIG_SMALL("RDT demuxer"),
952 sizeof(RMDemuxContext),
953 NULL,
954 NULL,
955 NULL,
956 rm_read_close,
957 };