2 * "Real" compatible muxer and 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
24 #define BUFFER_DURATION 0
28 int packet_total_size;
30 /* codec related output */
33 int nb_frames; /* current frame number */
34 int total_frames; /* total number of frames */
40 StreamInfo streams[2];
41 StreamInfo *audio_stream, *video_stream;
42 int data_pos; /* position of the data after the header */
47 /// Audio descrambling matrix parameters
48 uint8_t *audiobuf; ///< place to store reordered audio data
49 int64_t audiotimestamp; ///< Audio packet timestamp
50 int sub_packet_cnt; // Subpacket counter, used while reading
51 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
52 int audio_stream_num; ///< Stream number for audio packets
53 int audio_pkt_cnt; ///< Output packet counter
54 int audio_framesize; /// Audio frame size from container
55 int sub_packet_lengths[16]; /// Length of each aac subpacket
59 static void put_str(ByteIOContext *s, const char *tag)
61 put_be16(s,strlen(tag));
67 static void put_str8(ByteIOContext *s, const char *tag)
69 put_byte(s, strlen(tag));
75 static void rv10_write_header(AVFormatContext *ctx,
76 int data_size, int index_pos)
78 RMContext *rm = ctx->priv_data;
79 ByteIOContext *s = &ctx->pb;
81 unsigned char *data_offset_ptr, *start_ptr;
82 const char *desc, *mimetype;
83 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
84 int bit_rate, v, duration, flags, data_pos;
86 start_ptr = s->buf_ptr;
89 put_be32(s,18); /* header size */
92 put_be32(s,4 + ctx->nb_streams); /* num headers */
98 packet_total_size = 0;
102 for(i=0;i<ctx->nb_streams;i++) {
103 StreamInfo *stream = &rm->streams[i];
104 bit_rate += stream->bit_rate;
105 if (stream->packet_max_size > packet_max_size)
106 packet_max_size = stream->packet_max_size;
107 nb_packets += stream->nb_packets;
108 packet_total_size += stream->packet_total_size;
109 /* select maximum duration */
110 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
114 put_be32(s, bit_rate); /* max bit rate */
115 put_be32(s, bit_rate); /* avg bit rate */
116 put_be32(s, packet_max_size); /* max packet size */
118 packet_avg_size = packet_total_size / nb_packets;
121 put_be32(s, packet_avg_size); /* avg packet size */
122 put_be32(s, nb_packets); /* num packets */
123 put_be32(s, duration); /* duration */
124 put_be32(s, BUFFER_DURATION); /* preroll */
125 put_be32(s, index_pos); /* index offset */
126 /* computation of data the data offset */
127 data_offset_ptr = s->buf_ptr;
128 put_be32(s, 0); /* data offset : will be patched after */
129 put_be16(s, ctx->nb_streams); /* num streams */
130 flags = 1 | 2; /* save allowed & perfect play */
131 if (url_is_streamed(s))
132 flags |= 4; /* live broadcast */
138 size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
139 strlen(ctx->comment) + 4 * 2 + 10;
142 put_str(s, ctx->title);
143 put_str(s, ctx->author);
144 put_str(s, ctx->copyright);
145 put_str(s, ctx->comment);
147 for(i=0;i<ctx->nb_streams;i++) {
150 stream = &rm->streams[i];
152 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
153 desc = "The Audio Stream";
154 mimetype = "audio/x-pn-realaudio";
155 codec_data_size = 73;
157 desc = "The Video Stream";
158 mimetype = "video/x-pn-realvideo";
159 codec_data_size = 34;
163 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
167 put_be16(s, i); /* stream number */
168 put_be32(s, stream->bit_rate); /* max bit rate */
169 put_be32(s, stream->bit_rate); /* avg bit rate */
170 put_be32(s, stream->packet_max_size); /* max packet size */
171 if (stream->nb_packets > 0)
172 packet_avg_size = stream->packet_total_size /
176 put_be32(s, packet_avg_size); /* avg packet size */
177 put_be32(s, 0); /* start time */
178 put_be32(s, BUFFER_DURATION); /* preroll */
180 if (url_is_streamed(s) || !stream->total_frames)
181 put_be32(s, (int)(3600 * 1000));
183 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
185 put_str8(s, mimetype);
186 put_be32(s, codec_data_size);
188 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
189 int coded_frame_size, fscode, sample_rate;
190 sample_rate = stream->enc->sample_rate;
191 coded_frame_size = (stream->enc->bit_rate *
192 stream->enc->frame_size) / (8 * sample_rate);
193 /* audio codec info */
196 put_be32(s, 0x00040000); /* version */
198 put_be32(s, 0x01b53530); /* stream length */
199 put_be16(s, 4); /* unknown */
200 put_be32(s, 0x39); /* header size */
202 switch(sample_rate) {
219 put_be16(s, fscode); /* codec additional info, for AC3, seems
220 to be a frequency code */
221 /* special hack to compensate rounding errors... */
222 if (coded_frame_size == 557)
224 put_be32(s, coded_frame_size); /* frame length */
225 put_be32(s, 0x51540); /* unknown */
226 put_be32(s, 0x249f0); /* unknown */
227 put_be32(s, 0x249f0); /* unknown */
229 /* frame length : seems to be very important */
230 put_be16(s, coded_frame_size);
231 put_be32(s, 0); /* unknown */
232 put_be16(s, stream->enc->sample_rate); /* sample rate */
233 put_be32(s, 0x10); /* unknown */
234 put_be16(s, stream->enc->channels);
235 put_str8(s, "Int0"); /* codec name */
236 put_str8(s, "dnet"); /* codec name */
237 put_be16(s, 0); /* title length */
238 put_be16(s, 0); /* author length */
239 put_be16(s, 0); /* copyright length */
240 put_byte(s, 0); /* end of header */
242 /* video codec info */
243 put_be32(s,34); /* size */
244 if(stream->enc->codec_id == CODEC_ID_RV10)
245 put_tag(s,"VIDORV10");
247 put_tag(s,"VIDORV20");
248 put_be16(s, stream->enc->width);
249 put_be16(s, stream->enc->height);
250 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
251 put_be32(s,0); /* unknown meaning */
252 put_be16(s, (int) stream->frame_rate); /* unknown meaning */
253 put_be32(s,0); /* unknown meaning */
254 put_be16(s, 8); /* unknown meaning */
255 /* Seems to be the codec version: only use basic H263. The next
256 versions seems to add a diffential DC coding as in
257 MPEG... nothing new under the sun */
258 if(stream->enc->codec_id == CODEC_ID_RV10)
259 put_be32(s,0x10000000);
261 put_be32(s,0x20103001);
262 //put_be32(s,0x10003000);
266 /* patch data offset field */
267 data_pos = s->buf_ptr - start_ptr;
268 rm->data_pos = data_pos;
269 data_offset_ptr[0] = data_pos >> 24;
270 data_offset_ptr[1] = data_pos >> 16;
271 data_offset_ptr[2] = data_pos >> 8;
272 data_offset_ptr[3] = data_pos;
276 put_be32(s,data_size + 10 + 8);
279 put_be32(s, nb_packets); /* number of packets */
280 put_be32(s,0); /* next data header */
283 static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
284 int length, int key_frame)
287 ByteIOContext *s = &ctx->pb;
289 stream->nb_packets++;
290 stream->packet_total_size += length;
291 if (length > stream->packet_max_size)
292 stream->packet_max_size = length;
294 put_be16(s,0); /* version */
295 put_be16(s,length + 12);
296 put_be16(s, stream->num); /* stream number */
297 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
298 put_be32(s, timestamp); /* timestamp */
299 put_byte(s, 0); /* reserved */
300 put_byte(s, key_frame ? 2 : 0); /* flags */
303 static int rm_write_header(AVFormatContext *s)
305 RMContext *rm = s->priv_data;
308 AVCodecContext *codec;
310 for(n=0;n<s->nb_streams;n++) {
311 s->streams[n]->id = n;
312 codec = s->streams[n]->codec;
313 stream = &rm->streams[n];
314 memset(stream, 0, sizeof(StreamInfo));
316 stream->bit_rate = codec->bit_rate;
319 switch(codec->codec_type) {
320 case CODEC_TYPE_AUDIO:
321 rm->audio_stream = stream;
322 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
323 /* XXX: dummy values */
324 stream->packet_max_size = 1024;
325 stream->nb_packets = 0;
326 stream->total_frames = stream->nb_packets;
328 case CODEC_TYPE_VIDEO:
329 rm->video_stream = stream;
330 stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
331 /* XXX: dummy values */
332 stream->packet_max_size = 4096;
333 stream->nb_packets = 0;
334 stream->total_frames = stream->nb_packets;
341 rv10_write_header(s, 0, 0);
342 put_flush_packet(&s->pb);
346 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
349 RMContext *rm = s->priv_data;
350 ByteIOContext *pb = &s->pb;
351 StreamInfo *stream = rm->audio_stream;
354 /* XXX: suppress this malloc */
355 buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
357 write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
359 /* for AC3, the words seems to be reversed */
360 for(i=0;i<size;i+=2) {
364 put_buffer(pb, buf1, size);
365 put_flush_packet(pb);
371 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
373 RMContext *rm = s->priv_data;
374 ByteIOContext *pb = &s->pb;
375 StreamInfo *stream = rm->video_stream;
376 int key_frame = !!(flags & PKT_FLAG_KEY);
378 /* XXX: this is incorrect: should be a parameter */
380 /* Well, I spent some time finding the meaning of these bits. I am
381 not sure I understood everything, but it works !! */
383 write_packet_header(s, stream, size + 7, key_frame);
384 /* bit 7: '1' if final packet of a frame converted in several packets */
386 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
387 frame starting from 1 */
393 put_be16(pb, 0x4000 + (size)); /* total frame size */
394 put_be16(pb, 0x4000 + (size)); /* offset from the start or the end */
397 write_packet_header(s, size + 6);
399 put_be16(pb, 0x4000 + size); /* total frame size */
400 put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
402 put_byte(pb, stream->nb_frames & 0xff);
404 put_buffer(pb, buf, size);
405 put_flush_packet(pb);
411 static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
413 if (s->streams[pkt->stream_index]->codec->codec_type ==
415 return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
417 return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
420 static int rm_write_trailer(AVFormatContext *s)
422 RMContext *rm = s->priv_data;
423 int data_size, index_pos, i;
424 ByteIOContext *pb = &s->pb;
426 if (!url_is_streamed(&s->pb)) {
427 /* end of file: finish to write header */
428 index_pos = url_fseek(pb, 0, SEEK_CUR);
429 data_size = index_pos - rm->data_pos;
433 put_be32(pb, 10 + 10 * s->nb_streams);
436 for(i=0;i<s->nb_streams;i++) {
437 put_be32(pb, 0); /* zero indices */
438 put_be16(pb, i); /* stream number */
439 put_be32(pb, 0); /* next index */
441 /* undocumented end header */
445 url_fseek(pb, 0, SEEK_SET);
446 for(i=0;i<s->nb_streams;i++)
447 rm->streams[i].total_frames = rm->streams[i].nb_frames;
448 rv10_write_header(s, data_size, index_pos);
450 /* undocumented end header */
454 put_flush_packet(pb);
457 #endif //CONFIG_MUXERS
459 /***************************************************/
461 static void get_str(ByteIOContext *pb, char *buf, int buf_size)
469 if (i < buf_size - 1)
475 static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
483 if (i < buf_size - 1)
489 static int rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
492 RMContext *rm = s->priv_data;
493 ByteIOContext *pb = &s->pb;
499 version = get_be32(pb); /* version */
500 if (((version >> 16) & 0xff) == 3) {
501 int64_t startpos = url_ftell(pb);
502 /* very old version */
503 for(i = 0; i < 14; i++)
505 get_str8(pb, s->title, sizeof(s->title));
506 get_str8(pb, s->author, sizeof(s->author));
507 get_str8(pb, s->copyright, sizeof(s->copyright));
508 get_str8(pb, s->comment, sizeof(s->comment));
509 if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
510 // fourcc (should always be "lpcJ")
512 get_str8(pb, buf, sizeof(buf));
514 // Skip extra header crap (this should never happen)
515 if ((startpos + (version & 0xffff)) > url_ftell(pb))
516 url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
517 st->codec->sample_rate = 8000;
518 st->codec->channels = 1;
519 st->codec->codec_type = CODEC_TYPE_AUDIO;
520 st->codec->codec_id = CODEC_ID_RA_144;
522 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
523 /* old version (4) */
524 get_be32(pb); /* .ra4 */
525 get_be32(pb); /* data size */
526 get_be16(pb); /* version2 */
527 get_be32(pb); /* header size */
528 flavor= get_be16(pb); /* add codec info / flavor */
529 rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
530 get_be32(pb); /* ??? */
531 get_be32(pb); /* ??? */
532 get_be32(pb); /* ??? */
533 rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
534 st->codec->block_align= get_be16(pb); /* frame size */
535 rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
536 get_be16(pb); /* ??? */
537 if (((version >> 16) & 0xff) == 5) {
538 get_be16(pb); get_be16(pb); get_be16(pb); }
539 st->codec->sample_rate = get_be16(pb);
541 st->codec->channels = get_be16(pb);
542 if (((version >> 16) & 0xff) == 5) {
544 buf[0] = get_byte(pb);
545 buf[1] = get_byte(pb);
546 buf[2] = get_byte(pb);
547 buf[3] = get_byte(pb);
550 get_str8(pb, buf, sizeof(buf)); /* desc */
551 get_str8(pb, buf, sizeof(buf)); /* desc */
553 st->codec->codec_type = CODEC_TYPE_AUDIO;
554 if (!strcmp(buf, "dnet")) {
555 st->codec->codec_id = CODEC_ID_AC3;
556 } else if (!strcmp(buf, "28_8")) {
557 st->codec->codec_id = CODEC_ID_RA_288;
558 st->codec->extradata_size= 0;
559 rm->audio_framesize = st->codec->block_align;
560 st->codec->block_align = coded_framesize;
562 if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
563 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
567 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
568 } else if (!strcmp(buf, "cook")) {
569 int codecdata_length, i;
570 get_be16(pb); get_byte(pb);
571 if (((version >> 16) & 0xff) == 5)
573 codecdata_length = get_be32(pb);
574 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
575 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
579 st->codec->codec_id = CODEC_ID_COOK;
580 st->codec->extradata_size= codecdata_length;
581 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
582 for(i = 0; i < codecdata_length; i++)
583 ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
584 rm->audio_framesize = st->codec->block_align;
585 st->codec->block_align = rm->sub_packet_size;
587 if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
588 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
592 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
593 } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
594 int codecdata_length, i;
595 get_be16(pb); get_byte(pb);
596 if (((version >> 16) & 0xff) == 5)
598 st->codec->codec_id = CODEC_ID_AAC;
599 codecdata_length = get_be32(pb);
600 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
601 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
604 if (codecdata_length >= 1) {
605 st->codec->extradata_size = codecdata_length - 1;
606 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
608 for(i = 0; i < st->codec->extradata_size; i++)
609 ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
612 st->codec->codec_id = CODEC_ID_NONE;
613 pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
621 get_str8(pb, s->title, sizeof(s->title));
622 get_str8(pb, s->author, sizeof(s->author));
623 get_str8(pb, s->copyright, sizeof(s->copyright));
624 get_str8(pb, s->comment, sizeof(s->comment));
630 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
632 RMContext *rm = s->priv_data;
636 st = av_new_stream(s, 0);
639 return rm_read_audio_stream_info(s, st, 1);
642 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
644 RMContext *rm = s->priv_data;
646 ByteIOContext *pb = &s->pb;
648 int tag_size, size, codec_data_size, i;
650 unsigned int start_time, duration;
655 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
656 /* very old .ra format */
657 return rm_read_header_old(s, ap);
658 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
662 get_be32(pb); /* header size */
665 get_be32(pb); /* number of headers */
671 tag_size = get_be32(pb);
674 printf("tag=%c%c%c%c (%08x) size=%d\n",
682 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
685 case MKTAG('P', 'R', 'O', 'P'):
687 get_be32(pb); /* max bit rate */
688 get_be32(pb); /* avg bit rate */
689 get_be32(pb); /* max packet size */
690 get_be32(pb); /* avg packet size */
691 get_be32(pb); /* nb packets */
692 get_be32(pb); /* duration */
693 get_be32(pb); /* preroll */
694 get_be32(pb); /* index offset */
695 get_be32(pb); /* data offset */
696 get_be16(pb); /* nb streams */
697 flags = get_be16(pb); /* flags */
699 case MKTAG('C', 'O', 'N', 'T'):
700 get_str(pb, s->title, sizeof(s->title));
701 get_str(pb, s->author, sizeof(s->author));
702 get_str(pb, s->copyright, sizeof(s->copyright));
703 get_str(pb, s->comment, sizeof(s->comment));
705 case MKTAG('M', 'D', 'P', 'R'):
706 st = av_new_stream(s, 0);
709 st->id = get_be16(pb);
710 get_be32(pb); /* max bit rate */
711 st->codec->bit_rate = get_be32(pb); /* bit rate */
712 get_be32(pb); /* max packet size */
713 get_be32(pb); /* avg packet size */
714 start_time = get_be32(pb); /* start time */
715 get_be32(pb); /* preroll */
716 duration = get_be32(pb); /* duration */
717 st->start_time = start_time;
718 st->duration = duration;
719 get_str8(pb, buf, sizeof(buf)); /* desc */
720 get_str8(pb, buf, sizeof(buf)); /* mimetype */
721 codec_data_size = get_be32(pb);
722 codec_pos = url_ftell(pb);
723 st->codec->codec_type = CODEC_TYPE_DATA;
724 av_set_pts_info(st, 64, 1, 1000);
727 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
729 if (rm_read_audio_stream_info(s, st, 0))
733 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
735 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
738 st->codec->codec_tag = get_le32(pb);
739 // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
740 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
741 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
742 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
743 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
745 st->codec->width = get_be16(pb);
746 st->codec->height = get_be16(pb);
747 st->codec->time_base.num= 1;
749 st->codec->codec_type = CODEC_TYPE_VIDEO;
754 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
756 if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
757 //check is redundant as get_buffer() will catch this
758 av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
761 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
762 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
764 // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
765 st->codec->time_base.den = fps * st->codec->time_base.num;
766 switch(((uint8_t*)st->codec->extradata)[4]>>4){
767 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
768 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
769 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
770 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
775 /* skip codec info */
776 size = url_ftell(pb) - codec_pos;
777 url_fskip(pb, codec_data_size - size);
779 case MKTAG('D', 'A', 'T', 'A'):
782 /* unknown tag: skip it */
783 url_fskip(pb, tag_size - 10);
788 rm->nb_packets = get_be32(pb); /* number of packets */
789 if (!rm->nb_packets && (flags & 4))
790 rm->nb_packets = 3600 * 25;
791 get_be32(pb); /* next data header */
795 for(i=0;i<s->nb_streams;i++) {
796 av_free(s->streams[i]);
801 static int get_num(ByteIOContext *pb, int *len)
812 return (n << 16) | n1;
816 /* multiple of 20 bytes for ra144 (ugly) */
817 #define RAW_PACKET_SIZE 1000
819 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
820 RMContext *rm = s->priv_data;
821 ByteIOContext *pb = &s->pb;
822 int len, num, res, i;
824 uint32_t state=0xFFFFFFFF;
826 while(!url_feof(pb)){
828 if(rm->remaining_len > 0){
829 num= rm->current_stream;
830 len= rm->remaining_len;
831 *timestamp = AV_NOPTS_VALUE;
834 state= (state<<8) + get_byte(pb);
836 if(state == MKBETAG('I', 'N', 'D', 'X')){
837 len = get_be16(pb) - 6;
843 if(state > (unsigned)0xFFFF || state < 12)
849 *timestamp = get_be32(pb);
850 res= get_byte(pb); /* reserved */
851 *flags = get_byte(pb); /* flags */
856 for(i=0;i<s->nb_streams;i++) {
861 if (i == s->nb_streams) {
863 /* skip packet if unknown number */
865 rm->remaining_len -= len;
875 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
877 RMContext *rm = s->priv_data;
878 ByteIOContext *pb = &s->pb;
881 int64_t timestamp, pos;
885 if (rm->audio_pkt_cnt) {
886 // If there are queued audio packet return them first
887 st = s->streams[rm->audio_stream_num];
888 if (st->codec->codec_id == CODEC_ID_AAC)
889 av_get_packet(pb, pkt, rm->sub_packet_lengths[rm->sub_packet_cnt - rm->audio_pkt_cnt]);
891 av_new_packet(pkt, st->codec->block_align);
892 memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
893 (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
894 st->codec->block_align);
898 pkt->stream_index = rm->audio_stream_num;
899 } else if (rm->old_format) {
901 if (st->codec->codec_id == CODEC_ID_RA_288) {
904 for (y = 0; y < rm->sub_packet_h; y++)
905 for (x = 0; x < rm->sub_packet_h/2; x++)
906 if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
908 rm->audio_stream_num = 0;
909 rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
910 // Release first audio packet
911 av_new_packet(pkt, st->codec->block_align);
912 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
913 pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
914 pkt->stream_index = 0;
916 /* just read raw bytes */
917 len = RAW_PACKET_SIZE;
918 len= av_get_packet(pb, pkt, len);
919 pkt->stream_index = 0;
928 len=sync(s, ×tamp, &flags, &i, &pos);
933 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
934 int h, pic_num, len2, pos;
936 h= get_byte(pb); len--;
938 seq = get_byte(pb); len--;
941 if((h & 0xc0) == 0x40){
944 len2 = get_num(pb, &len);
945 pos = get_num(pb, &len);
948 pic_num= get_byte(pb); len--;
949 rm->remaining_len= len;
950 rm->current_stream= st->id;
952 // av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
955 rm->remaining_len-= len;
956 av_get_packet(pb, pkt, len);
959 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
960 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
961 (st->codec->codec_id == CODEC_ID_COOK)) {
963 int sps = rm->sub_packet_size;
964 int cfs = rm->coded_framesize;
965 int h = rm->sub_packet_h;
966 int y = rm->sub_packet_cnt;
967 int w = rm->audio_framesize;
970 y = rm->sub_packet_cnt = 0;
972 rm->audiotimestamp = timestamp;
974 switch(st->codec->codec_id) {
975 case CODEC_ID_RA_288:
976 for (x = 0; x < h/2; x++)
977 get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
980 for (x = 0; x < w/sps; x++)
981 get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
985 if (++(rm->sub_packet_cnt) < h)
988 rm->sub_packet_cnt = 0;
989 rm->audio_stream_num = i;
990 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
991 // Release first audio packet
992 av_new_packet(pkt, st->codec->block_align);
993 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
994 timestamp = rm->audiotimestamp;
995 flags = 2; // Mark first packet as keyframe
997 } else if (st->codec->codec_id == CODEC_ID_AAC) {
999 rm->audio_stream_num = i;
1000 rm->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
1001 if (rm->sub_packet_cnt) {
1002 for (x = 0; x < rm->sub_packet_cnt; x++)
1003 rm->sub_packet_lengths[x] = get_be16(pb);
1004 // Release first audio packet
1005 rm->audio_pkt_cnt = rm->sub_packet_cnt - 1;
1006 av_get_packet(pb, pkt, rm->sub_packet_lengths[0]);
1007 flags = 2; // Mark first packet as keyframe
1010 av_get_packet(pb, pkt, len);
1013 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
1014 || st->discard >= AVDISCARD_ALL){
1015 av_free_packet(pkt);
1019 pkt->stream_index = i;
1022 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1023 if(st->codec->codec_id == CODEC_ID_RV20){
1024 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
1025 av_log(NULL, AV_LOG_DEBUG, "%d %"PRId64" %d\n", timestamp, timestamp*512LL/25, seq);
1027 seq |= (timestamp&~0x3FFF);
1028 if(seq - timestamp > 0x2000) seq -= 0x4000;
1029 if(seq - timestamp < -0x2000) seq += 0x4000;
1033 pkt->pts= timestamp;
1035 pkt->flags |= PKT_FLAG_KEY;
1037 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
1041 /* for AC3, needs to swap bytes */
1042 if (st->codec->codec_id == CODEC_ID_AC3) {
1044 for(j=0;j<len;j+=2) {
1054 static int rm_read_close(AVFormatContext *s)
1056 RMContext *rm = s->priv_data;
1058 av_free(rm->audiobuf);
1062 static int rm_probe(AVProbeData *p)
1064 /* check file header */
1065 if (p->buf_size <= 32)
1067 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1068 p->buf[2] == 'M' && p->buf[3] == 'F' &&
1069 p->buf[4] == 0 && p->buf[5] == 0) ||
1070 (p->buf[0] == '.' && p->buf[1] == 'r' &&
1071 p->buf[2] == 'a' && p->buf[3] == 0xfd))
1072 return AVPROBE_SCORE_MAX;
1077 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1078 int64_t *ppos, int64_t pos_limit)
1080 RMContext *rm = s->priv_data;
1082 int stream_index2, flags, len, h;
1087 return AV_NOPTS_VALUE;
1089 url_fseek(&s->pb, pos, SEEK_SET);
1090 rm->remaining_len=0;
1095 len=sync(s, &dts, &flags, &stream_index2, &pos);
1097 return AV_NOPTS_VALUE;
1099 st = s->streams[stream_index2];
1100 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1101 h= get_byte(&s->pb); len--;
1103 seq = get_byte(&s->pb); len--;
1107 if((flags&2) && (seq&0x7F) == 1){
1108 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
1109 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1110 if(stream_index2 == stream_index)
1114 url_fskip(&s->pb, len);
1120 #ifdef CONFIG_RM_DEMUXER
1121 AVInputFormat rm_demuxer = {
1133 #ifdef CONFIG_RM_MUXER
1134 AVOutputFormat rm_muxer = {
1137 "application/vnd.rn-realmedia",