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