1eb509d4fbbeb5c6c99bcf050b5543972074c351
[libav.git] / libavformat / rmdec.c
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 "libavutil/avstring.h"
23 #include "avformat.h"
24
25 typedef struct {
26 int nb_packets;
27 int old_format;
28 int current_stream;
29 int remaining_len;
30 uint8_t *videobuf; ///< place to store merged video frame
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 uint8_t *audiobuf; ///< place to store reordered audio data
38 int64_t audiotimestamp; ///< Audio packet timestamp
39 int sub_packet_cnt; // Subpacket counter, used while reading
40 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
41 int audio_stream_num; ///< Stream number for audio packets
42 int audio_pkt_cnt; ///< Output packet counter
43 int audio_framesize; /// Audio frame size from container
44 int sub_packet_lengths[16]; /// Length of each aac subpacket
45 } RMDemuxContext;
46
47 static inline void get_strl(ByteIOContext *pb, char *buf, int buf_size, int len)
48 {
49 int i;
50 char *q, r;
51
52 q = buf;
53 for(i=0;i<len;i++) {
54 r = get_byte(pb);
55 if (i < buf_size - 1)
56 *q++ = r;
57 }
58 if (buf_size > 0) *q = '\0';
59 }
60
61 static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
62 {
63 get_strl(pb, buf, buf_size, get_be16(pb));
64 }
65
66 static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
67 {
68 get_strl(pb, buf, buf_size, get_byte(pb));
69 }
70
71 static int rm_read_audio_stream_info(AVFormatContext *s, ByteIOContext *pb,
72 AVStream *st, int read_all)
73 {
74 RMDemuxContext *rm = s->priv_data;
75 char buf[256];
76 uint32_t version;
77
78 /* ra type header */
79 version = get_be32(pb); /* version */
80 if (((version >> 16) & 0xff) == 3) {
81 int64_t startpos = url_ftell(pb);
82 url_fskip(pb, 14);
83 get_str8(pb, s->title, sizeof(s->title));
84 get_str8(pb, s->author, sizeof(s->author));
85 get_str8(pb, s->copyright, sizeof(s->copyright));
86 get_str8(pb, s->comment, sizeof(s->comment));
87 if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
88 // fourcc (should always be "lpcJ")
89 get_byte(pb);
90 get_str8(pb, buf, sizeof(buf));
91 }
92 // Skip extra header crap (this should never happen)
93 if ((startpos + (version & 0xffff)) > url_ftell(pb))
94 url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
95 st->codec->sample_rate = 8000;
96 st->codec->channels = 1;
97 st->codec->codec_type = CODEC_TYPE_AUDIO;
98 st->codec->codec_id = CODEC_ID_RA_144;
99 } else {
100 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
101 /* old version (4) */
102 get_be32(pb); /* .ra4 */
103 get_be32(pb); /* data size */
104 get_be16(pb); /* version2 */
105 get_be32(pb); /* header size */
106 flavor= get_be16(pb); /* add codec info / flavor */
107 rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
108 get_be32(pb); /* ??? */
109 get_be32(pb); /* ??? */
110 get_be32(pb); /* ??? */
111 rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
112 st->codec->block_align= get_be16(pb); /* frame size */
113 rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
114 get_be16(pb); /* ??? */
115 if (((version >> 16) & 0xff) == 5) {
116 get_be16(pb); get_be16(pb); get_be16(pb);
117 }
118 st->codec->sample_rate = get_be16(pb);
119 get_be32(pb);
120 st->codec->channels = get_be16(pb);
121 if (((version >> 16) & 0xff) == 5) {
122 get_be32(pb);
123 get_buffer(pb, buf, 4);
124 buf[4] = 0;
125 } else {
126 get_str8(pb, buf, sizeof(buf)); /* desc */
127 get_str8(pb, buf, sizeof(buf)); /* desc */
128 }
129 st->codec->codec_type = CODEC_TYPE_AUDIO;
130 if (!strcmp(buf, "dnet")) {
131 st->codec->codec_id = CODEC_ID_AC3;
132 st->need_parsing = AVSTREAM_PARSE_FULL;
133 } else if (!strcmp(buf, "28_8")) {
134 st->codec->codec_id = CODEC_ID_RA_288;
135 st->codec->extradata_size= 0;
136 rm->audio_framesize = st->codec->block_align;
137 st->codec->block_align = coded_framesize;
138
139 if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
140 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
141 return -1;
142 }
143
144 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
145 } else if ((!strcmp(buf, "cook")) || (!strcmp(buf, "atrc")) || (!strcmp(buf, "sipr"))) {
146 int codecdata_length;
147 get_be16(pb); get_byte(pb);
148 if (((version >> 16) & 0xff) == 5)
149 get_byte(pb);
150 codecdata_length = get_be32(pb);
151 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
152 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
153 return -1;
154 }
155
156 if(sub_packet_size <= 0){
157 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
158 return -1;
159 }
160
161 if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
162 else if (!strcmp(buf, "sipr")) st->codec->codec_id = CODEC_ID_SIPR;
163 else st->codec->codec_id = CODEC_ID_ATRAC3;
164 st->codec->extradata_size= codecdata_length;
165 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
166 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
167 rm->audio_framesize = st->codec->block_align;
168 st->codec->block_align = rm->sub_packet_size;
169
170 if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
171 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
172 return -1;
173 }
174
175 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
176 } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
177 int codecdata_length;
178 get_be16(pb); get_byte(pb);
179 if (((version >> 16) & 0xff) == 5)
180 get_byte(pb);
181 st->codec->codec_id = CODEC_ID_AAC;
182 codecdata_length = get_be32(pb);
183 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
184 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
185 return -1;
186 }
187 if (codecdata_length >= 1) {
188 st->codec->extradata_size = codecdata_length - 1;
189 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
190 get_byte(pb);
191 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
192 }
193 } else {
194 st->codec->codec_id = CODEC_ID_NONE;
195 av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
196 }
197 if (read_all) {
198 get_byte(pb);
199 get_byte(pb);
200 get_byte(pb);
201
202 get_str8(pb, s->title, sizeof(s->title));
203 get_str8(pb, s->author, sizeof(s->author));
204 get_str8(pb, s->copyright, sizeof(s->copyright));
205 get_str8(pb, s->comment, sizeof(s->comment));
206 }
207 }
208 return 0;
209 }
210
211 int
212 ff_rm_read_mdpr_codecdata (AVFormatContext *s, ByteIOContext *pb,
213 AVStream *st, int codec_data_size)
214 {
215 unsigned int v;
216 int size;
217 int64_t codec_pos;
218
219 av_set_pts_info(st, 64, 1, 1000);
220 codec_pos = url_ftell(pb);
221 v = get_be32(pb);
222 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
223 /* ra type header */
224 if (rm_read_audio_stream_info(s, pb, st, 0))
225 return -1;
226 } else {
227 int fps, fps2;
228 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
229 fail1:
230 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
231 goto skip;
232 }
233 st->codec->codec_tag = get_le32(pb);
234 // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
235 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
236 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
237 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
238 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0')
239 && st->codec->codec_tag != MKTAG('R', 'V', 'T', 'R'))
240 goto fail1;
241 st->codec->width = get_be16(pb);
242 st->codec->height = get_be16(pb);
243 st->codec->time_base.num= 1;
244 fps= get_be16(pb);
245 st->codec->codec_type = CODEC_TYPE_VIDEO;
246 get_be32(pb);
247 fps2= get_be16(pb);
248 get_be16(pb);
249
250 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
251
252 if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
253 //check is redundant as get_buffer() will catch this
254 av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
255 return -1;
256 }
257 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
258 if (!st->codec->extradata)
259 return AVERROR(ENOMEM);
260 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
261
262 // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
263 st->codec->time_base.den = fps * st->codec->time_base.num;
264 switch(((uint8_t*)st->codec->extradata)[4]>>4){
265 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
266 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
267 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
268 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
269 default: goto fail1;
270 }
271 }
272
273 skip:
274 /* skip codec info */
275 size = url_ftell(pb) - codec_pos;
276 url_fskip(pb, codec_data_size - size);
277
278 return 0;
279 }
280
281
282 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
283 {
284 RMDemuxContext *rm = s->priv_data;
285 AVStream *st;
286
287 rm->old_format = 1;
288 st = av_new_stream(s, 0);
289 if (!st)
290 return -1;
291 return rm_read_audio_stream_info(s, s->pb, st, 1);
292 }
293
294 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
295 {
296 RMDemuxContext *rm = s->priv_data;
297 AVStream *st;
298 ByteIOContext *pb = s->pb;
299 unsigned int tag;
300 int tag_size;
301 unsigned int start_time, duration;
302 char buf[128];
303 int flags = 0;
304
305 tag = get_le32(pb);
306 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
307 /* very old .ra format */
308 return rm_read_header_old(s, ap);
309 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
310 return AVERROR(EIO);
311 }
312
313 get_be32(pb); /* header size */
314 get_be16(pb);
315 get_be32(pb);
316 get_be32(pb); /* number of headers */
317
318 for(;;) {
319 if (url_feof(pb))
320 return -1;
321 tag = get_le32(pb);
322 tag_size = get_be32(pb);
323 get_be16(pb);
324 #if 0
325 printf("tag=%c%c%c%c (%08x) size=%d\n",
326 (tag) & 0xff,
327 (tag >> 8) & 0xff,
328 (tag >> 16) & 0xff,
329 (tag >> 24) & 0xff,
330 tag,
331 tag_size);
332 #endif
333 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
334 return -1;
335 switch(tag) {
336 case MKTAG('P', 'R', 'O', 'P'):
337 /* file header */
338 get_be32(pb); /* max bit rate */
339 get_be32(pb); /* avg bit rate */
340 get_be32(pb); /* max packet size */
341 get_be32(pb); /* avg packet size */
342 get_be32(pb); /* nb packets */
343 get_be32(pb); /* duration */
344 get_be32(pb); /* preroll */
345 get_be32(pb); /* index offset */
346 get_be32(pb); /* data offset */
347 get_be16(pb); /* nb streams */
348 flags = get_be16(pb); /* flags */
349 break;
350 case MKTAG('C', 'O', 'N', 'T'):
351 get_str16(pb, s->title, sizeof(s->title));
352 get_str16(pb, s->author, sizeof(s->author));
353 get_str16(pb, s->copyright, sizeof(s->copyright));
354 get_str16(pb, s->comment, sizeof(s->comment));
355 break;
356 case MKTAG('M', 'D', 'P', 'R'):
357 st = av_new_stream(s, 0);
358 if (!st)
359 return AVERROR(ENOMEM);
360 st->id = get_be16(pb);
361 get_be32(pb); /* max bit rate */
362 st->codec->bit_rate = get_be32(pb); /* bit rate */
363 get_be32(pb); /* max packet size */
364 get_be32(pb); /* avg packet size */
365 start_time = get_be32(pb); /* start time */
366 get_be32(pb); /* preroll */
367 duration = get_be32(pb); /* duration */
368 st->start_time = start_time;
369 st->duration = duration;
370 get_str8(pb, buf, sizeof(buf)); /* desc */
371 get_str8(pb, buf, sizeof(buf)); /* mimetype */
372 st->codec->codec_type = CODEC_TYPE_DATA;
373 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, get_be32(pb)) < 0)
374 return -1;
375 break;
376 case MKTAG('D', 'A', 'T', 'A'):
377 goto header_end;
378 default:
379 /* unknown tag: skip it */
380 url_fskip(pb, tag_size - 10);
381 break;
382 }
383 }
384 header_end:
385 rm->nb_packets = get_be32(pb); /* number of packets */
386 if (!rm->nb_packets && (flags & 4))
387 rm->nb_packets = 3600 * 25;
388 get_be32(pb); /* next data header */
389 rm->curpic_num = -1;
390 return 0;
391 }
392
393 static int get_num(ByteIOContext *pb, int *len)
394 {
395 int n, n1;
396
397 n = get_be16(pb);
398 (*len)-=2;
399 n &= 0x7FFF;
400 if (n >= 0x4000) {
401 return n - 0x4000;
402 } else {
403 n1 = get_be16(pb);
404 (*len)-=2;
405 return (n << 16) | n1;
406 }
407 }
408
409 /* multiple of 20 bytes for ra144 (ugly) */
410 #define RAW_PACKET_SIZE 1000
411
412 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
413 RMDemuxContext *rm = s->priv_data;
414 ByteIOContext *pb = s->pb;
415 int len, num, res, i;
416 AVStream *st;
417 uint32_t state=0xFFFFFFFF;
418
419 while(!url_feof(pb)){
420 *pos= url_ftell(pb) - 3;
421 if(rm->remaining_len > 0){
422 num= rm->current_stream;
423 len= rm->remaining_len;
424 *timestamp = AV_NOPTS_VALUE;
425 *flags= 0;
426 }else{
427 state= (state<<8) + get_byte(pb);
428
429 if(state == MKBETAG('I', 'N', 'D', 'X')){
430 len = get_be16(pb) - 6;
431 if(len<0)
432 continue;
433 goto skip;
434 }
435
436 if(state > (unsigned)0xFFFF || state < 12)
437 continue;
438 len=state;
439 state= 0xFFFFFFFF;
440
441 num = get_be16(pb);
442 *timestamp = get_be32(pb);
443 res= get_byte(pb); /* reserved */
444 *flags = get_byte(pb); /* flags */
445
446
447 len -= 12;
448 }
449 for(i=0;i<s->nb_streams;i++) {
450 st = s->streams[i];
451 if (num == st->id)
452 break;
453 }
454 if (i == s->nb_streams) {
455 skip:
456 /* skip packet if unknown number */
457 url_fskip(pb, len);
458 rm->remaining_len -= len;
459 continue;
460 }
461 *stream_index= i;
462
463 return len;
464 }
465 return -1;
466 }
467
468 static int rm_assemble_video_frame(AVFormatContext *s, ByteIOContext *pb,
469 RMDemuxContext *rm, AVPacket *pkt, int len)
470 {
471 int hdr, seq, pic_num, len2, pos;
472 int type;
473
474 hdr = get_byte(pb); len--;
475 type = hdr >> 6;
476 switch(type){
477 case 0: // slice
478 case 2: // last slice
479 seq = get_byte(pb); len--;
480 len2 = get_num(pb, &len);
481 pos = get_num(pb, &len);
482 pic_num = get_byte(pb); len--;
483 rm->remaining_len = len;
484 break;
485 case 1: //whole frame
486 seq = get_byte(pb); len--;
487 if(av_new_packet(pkt, len + 9) < 0)
488 return AVERROR(EIO);
489 pkt->data[0] = 0;
490 AV_WL32(pkt->data + 1, 1);
491 AV_WL32(pkt->data + 5, 0);
492 get_buffer(pb, pkt->data + 9, len);
493 rm->remaining_len = 0;
494 return 0;
495 case 3: //frame as a part of packet
496 len2 = get_num(pb, &len);
497 pos = get_num(pb, &len);
498 pic_num = get_byte(pb); len--;
499 rm->remaining_len = len - len2;
500 if(av_new_packet(pkt, len2 + 9) < 0)
501 return AVERROR(EIO);
502 pkt->data[0] = 0;
503 AV_WL32(pkt->data + 1, 1);
504 AV_WL32(pkt->data + 5, 0);
505 get_buffer(pb, pkt->data + 9, len2);
506 return 0;
507 }
508 //now we have to deal with single slice
509
510 if((seq & 0x7F) == 1 || rm->curpic_num != pic_num){
511 rm->slices = ((hdr & 0x3F) << 1) + 1;
512 rm->videobufsize = len2 + 8*rm->slices + 1;
513 av_free(rm->videobuf);
514 if(!(rm->videobuf = av_malloc(rm->videobufsize)))
515 return AVERROR(ENOMEM);
516 rm->videobufpos = 8*rm->slices + 1;
517 rm->cur_slice = 0;
518 rm->curpic_num = pic_num;
519 rm->pktpos = url_ftell(pb);
520 }
521 if(type == 2)
522 len = FFMIN(len, pos);
523
524 if(++rm->cur_slice > rm->slices)
525 return 1;
526 AV_WL32(rm->videobuf - 7 + 8*rm->cur_slice, 1);
527 AV_WL32(rm->videobuf - 3 + 8*rm->cur_slice, rm->videobufpos - 8*rm->slices - 1);
528 if(rm->videobufpos + len > rm->videobufsize)
529 return 1;
530 if (get_buffer(pb, rm->videobuf + rm->videobufpos, len) != len)
531 return AVERROR(EIO);
532 rm->videobufpos += len;
533 rm->remaining_len-= len;
534
535 if(type == 2 || (rm->videobufpos) == rm->videobufsize){
536 rm->videobuf[0] = rm->cur_slice-1;
537 if(av_new_packet(pkt, rm->videobufpos - 8*(rm->slices - rm->cur_slice)) < 0)
538 return AVERROR(ENOMEM);
539 memcpy(pkt->data, rm->videobuf, 1 + 8*rm->cur_slice);
540 memcpy(pkt->data + 1 + 8*rm->cur_slice, rm->videobuf + 1 + 8*rm->slices,
541 rm->videobufpos - 1 - 8*rm->slices);
542 pkt->pts = AV_NOPTS_VALUE;
543 pkt->pos = rm->pktpos;
544 return 0;
545 }
546
547 return 1;
548 }
549
550 static inline void
551 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
552 {
553 uint8_t *ptr;
554 int j;
555
556 if (st->codec->codec_id == CODEC_ID_AC3) {
557 ptr = pkt->data;
558 for (j=0;j<pkt->size;j+=2) {
559 FFSWAP(int, ptr[0], ptr[1]);
560 ptr += 2;
561 }
562 }
563 }
564
565 int
566 ff_rm_parse_packet (AVFormatContext *s, ByteIOContext *pb,
567 AVStream *st, int len, AVPacket *pkt,
568 int *seq, int *flags, int64_t *timestamp)
569 {
570 RMDemuxContext *rm = s->priv_data;
571
572 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
573 rm->current_stream= st->id;
574 if(rm_assemble_video_frame(s, pb, rm, pkt, len) == 1)
575 return -1; //got partial frame
576 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
577 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
578 (st->codec->codec_id == CODEC_ID_COOK) ||
579 (st->codec->codec_id == CODEC_ID_ATRAC3) ||
580 (st->codec->codec_id == CODEC_ID_SIPR)) {
581 int x;
582 int sps = rm->sub_packet_size;
583 int cfs = rm->coded_framesize;
584 int h = rm->sub_packet_h;
585 int y = rm->sub_packet_cnt;
586 int w = rm->audio_framesize;
587
588 if (*flags & 2)
589 y = rm->sub_packet_cnt = 0;
590 if (!y)
591 rm->audiotimestamp = *timestamp;
592
593 switch(st->codec->codec_id) {
594 case CODEC_ID_RA_288:
595 for (x = 0; x < h/2; x++)
596 get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
597 break;
598 case CODEC_ID_ATRAC3:
599 case CODEC_ID_COOK:
600 for (x = 0; x < w/sps; x++)
601 get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
602 break;
603 }
604
605 if (++(rm->sub_packet_cnt) < h)
606 return -1;
607 else {
608 rm->sub_packet_cnt = 0;
609 rm->audio_stream_num = st->index;
610 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
611 // Release first audio packet
612 av_new_packet(pkt, st->codec->block_align);
613 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
614 *timestamp = rm->audiotimestamp;
615 *flags = 2; // Mark first packet as keyframe
616 }
617 } else if (st->codec->codec_id == CODEC_ID_AAC) {
618 int x;
619 rm->audio_stream_num = st->index;
620 rm->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
621 if (rm->sub_packet_cnt) {
622 for (x = 0; x < rm->sub_packet_cnt; x++)
623 rm->sub_packet_lengths[x] = get_be16(pb);
624 // Release first audio packet
625 rm->audio_pkt_cnt = rm->sub_packet_cnt - 1;
626 av_get_packet(pb, pkt, rm->sub_packet_lengths[0]);
627 *flags = 2; // Mark first packet as keyframe
628 }
629 } else {
630 av_get_packet(pb, pkt, len);
631 rm_ac3_swap_bytes(st, pkt);
632 }
633 } else
634 av_get_packet(pb, pkt, len);
635
636 if( (st->discard >= AVDISCARD_NONKEY && !(*flags&2))
637 || st->discard >= AVDISCARD_ALL){
638 av_free_packet(pkt);
639 return -1;
640 }
641
642 pkt->stream_index = st->index;
643
644 #if 0
645 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
646 if(st->codec->codec_id == CODEC_ID_RV20){
647 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
648 av_log(NULL, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
649
650 seq |= (*timestamp&~0x3FFF);
651 if(seq - *timestamp > 0x2000) seq -= 0x4000;
652 if(seq - *timestamp < -0x2000) seq += 0x4000;
653 }
654 }
655 #endif
656
657 pkt->pts= *timestamp;
658 if (*flags & 2)
659 pkt->flags |= PKT_FLAG_KEY;
660
661 return st->codec->codec_type == CODEC_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
662 }
663
664 void
665 ff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,
666 AVStream *st, AVPacket *pkt)
667 {
668 RMDemuxContext *rm = s->priv_data;
669
670 assert (rm->audio_pkt_cnt > 0);
671
672 if (st->codec->codec_id == CODEC_ID_AAC)
673 av_get_packet(pb, pkt, rm->sub_packet_lengths[rm->sub_packet_cnt - rm->audio_pkt_cnt]);
674 else {
675 av_new_packet(pkt, st->codec->block_align);
676 memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
677 (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
678 st->codec->block_align);
679 }
680 rm->audio_pkt_cnt--;
681 pkt->flags = 0;
682 pkt->stream_index = st->index;
683 }
684
685 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
686 {
687 RMDemuxContext *rm = s->priv_data;
688 ByteIOContext *pb = s->pb;
689 AVStream *st;
690 int i, len;
691 int64_t timestamp, pos;
692 int flags;
693
694 if (rm->audio_pkt_cnt) {
695 // If there are queued audio packet return them first
696 st = s->streams[rm->audio_stream_num];
697 ff_rm_retrieve_cache(s, s->pb, st, pkt);
698 } else if (rm->old_format) {
699 st = s->streams[0];
700 if (st->codec->codec_id == CODEC_ID_RA_288) {
701 int x, y;
702
703 for (y = 0; y < rm->sub_packet_h; y++)
704 for (x = 0; x < rm->sub_packet_h/2; x++)
705 if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
706 return AVERROR(EIO);
707 rm->audio_stream_num = 0;
708 rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
709 // Release first audio packet
710 av_new_packet(pkt, st->codec->block_align);
711 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
712 pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
713 pkt->stream_index = 0;
714 } else {
715 /* just read raw bytes */
716 len = RAW_PACKET_SIZE;
717 len= av_get_packet(pb, pkt, len);
718 pkt->stream_index = 0;
719 if (len <= 0) {
720 return AVERROR(EIO);
721 }
722 pkt->size = len;
723 }
724 rm_ac3_swap_bytes(st, pkt);
725 } else {
726 int seq=1;
727 resync:
728 len=sync(s, &timestamp, &flags, &i, &pos);
729 if(len<0)
730 return AVERROR(EIO);
731 st = s->streams[i];
732
733 if (ff_rm_parse_packet (s, s->pb, st, len, pkt, &seq, &flags, &timestamp) < 0)
734 goto resync;
735
736 if((flags&2) && (seq&0x7F) == 1)
737 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
738 }
739
740 return 0;
741 }
742
743 static int rm_read_close(AVFormatContext *s)
744 {
745 RMDemuxContext *rm = s->priv_data;
746
747 av_free(rm->audiobuf);
748 av_free(rm->videobuf);
749 return 0;
750 }
751
752 static int rm_probe(AVProbeData *p)
753 {
754 /* check file header */
755 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
756 p->buf[2] == 'M' && p->buf[3] == 'F' &&
757 p->buf[4] == 0 && p->buf[5] == 0) ||
758 (p->buf[0] == '.' && p->buf[1] == 'r' &&
759 p->buf[2] == 'a' && p->buf[3] == 0xfd))
760 return AVPROBE_SCORE_MAX;
761 else
762 return 0;
763 }
764
765 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
766 int64_t *ppos, int64_t pos_limit)
767 {
768 RMDemuxContext *rm = s->priv_data;
769 int64_t pos, dts;
770 int stream_index2, flags, len, h;
771
772 pos = *ppos;
773
774 if(rm->old_format)
775 return AV_NOPTS_VALUE;
776
777 url_fseek(s->pb, pos, SEEK_SET);
778 rm->remaining_len=0;
779 for(;;){
780 int seq=1;
781 AVStream *st;
782
783 len=sync(s, &dts, &flags, &stream_index2, &pos);
784 if(len<0)
785 return AV_NOPTS_VALUE;
786
787 st = s->streams[stream_index2];
788 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
789 h= get_byte(s->pb); len--;
790 if(!(h & 0x40)){
791 seq = get_byte(s->pb); len--;
792 }
793 }
794
795 if((flags&2) && (seq&0x7F) == 1){
796 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
797 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
798 if(stream_index2 == stream_index)
799 break;
800 }
801
802 url_fskip(s->pb, len);
803 }
804 *ppos = pos;
805 return dts;
806 }
807
808 AVInputFormat rm_demuxer = {
809 "rm",
810 NULL_IF_CONFIG_SMALL("RM format"),
811 sizeof(RMDemuxContext),
812 rm_probe,
813 rm_read_header,
814 rm_read_packet,
815 rm_read_close,
816 NULL,
817 rm_read_dts,
818 };
819
820 AVInputFormat rdt_demuxer = {
821 "rdt",
822 NULL_IF_CONFIG_SMALL("RDT demuxer"),
823 sizeof(RMDemuxContext),
824 NULL, NULL, NULL, rm_read_close, NULL, NULL
825 };