2 * "Real" compatible mux and demux.
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define BUFFER_DURATION 0
26 int packet_total_size;
28 /* codec related output */
31 int nb_frames; /* current frame number */
32 int total_frames; /* total number of frames */
38 StreamInfo streams[2];
39 StreamInfo *audio_stream, *video_stream;
40 int data_pos; /* position of the data after the header */
45 /// Audio descrambling matrix parameters
46 uint8_t *audiobuf; ///< place to store reordered audio data
47 int64_t audiotimestamp; ///< Audio packet timestamp
48 int sub_packet_cnt; // Subpacket counter, used while reading
49 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
50 int audio_stream_num; ///< Stream number for audio packets
51 int audio_pkt_cnt; ///< Output packet counter
52 int audio_framesize; /// Audio frame size from container
56 static void put_str(ByteIOContext *s, const char *tag)
58 put_be16(s,strlen(tag));
64 static void put_str8(ByteIOContext *s, const char *tag)
66 put_byte(s, strlen(tag));
72 static void rv10_write_header(AVFormatContext *ctx,
73 int data_size, int index_pos)
75 RMContext *rm = ctx->priv_data;
76 ByteIOContext *s = &ctx->pb;
78 unsigned char *data_offset_ptr, *start_ptr;
79 const char *desc, *mimetype;
80 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
81 int bit_rate, v, duration, flags, data_pos;
83 start_ptr = s->buf_ptr;
86 put_be32(s,18); /* header size */
89 put_be32(s,4 + ctx->nb_streams); /* num headers */
95 packet_total_size = 0;
99 for(i=0;i<ctx->nb_streams;i++) {
100 StreamInfo *stream = &rm->streams[i];
101 bit_rate += stream->bit_rate;
102 if (stream->packet_max_size > packet_max_size)
103 packet_max_size = stream->packet_max_size;
104 nb_packets += stream->nb_packets;
105 packet_total_size += stream->packet_total_size;
106 /* select maximum duration */
107 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
111 put_be32(s, bit_rate); /* max bit rate */
112 put_be32(s, bit_rate); /* avg bit rate */
113 put_be32(s, packet_max_size); /* max packet size */
115 packet_avg_size = packet_total_size / nb_packets;
118 put_be32(s, packet_avg_size); /* avg packet size */
119 put_be32(s, nb_packets); /* num packets */
120 put_be32(s, duration); /* duration */
121 put_be32(s, BUFFER_DURATION); /* preroll */
122 put_be32(s, index_pos); /* index offset */
123 /* computation of data the data offset */
124 data_offset_ptr = s->buf_ptr;
125 put_be32(s, 0); /* data offset : will be patched after */
126 put_be16(s, ctx->nb_streams); /* num streams */
127 flags = 1 | 2; /* save allowed & perfect play */
128 if (url_is_streamed(s))
129 flags |= 4; /* live broadcast */
135 size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
136 strlen(ctx->comment) + 4 * 2 + 10;
139 put_str(s, ctx->title);
140 put_str(s, ctx->author);
141 put_str(s, ctx->copyright);
142 put_str(s, ctx->comment);
144 for(i=0;i<ctx->nb_streams;i++) {
147 stream = &rm->streams[i];
149 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
150 desc = "The Audio Stream";
151 mimetype = "audio/x-pn-realaudio";
152 codec_data_size = 73;
154 desc = "The Video Stream";
155 mimetype = "video/x-pn-realvideo";
156 codec_data_size = 34;
160 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
164 put_be16(s, i); /* stream number */
165 put_be32(s, stream->bit_rate); /* max bit rate */
166 put_be32(s, stream->bit_rate); /* avg bit rate */
167 put_be32(s, stream->packet_max_size); /* max packet size */
168 if (stream->nb_packets > 0)
169 packet_avg_size = stream->packet_total_size /
173 put_be32(s, packet_avg_size); /* avg packet size */
174 put_be32(s, 0); /* start time */
175 put_be32(s, BUFFER_DURATION); /* preroll */
177 if (url_is_streamed(s) || !stream->total_frames)
178 put_be32(s, (int)(3600 * 1000));
180 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
182 put_str8(s, mimetype);
183 put_be32(s, codec_data_size);
185 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
186 int coded_frame_size, fscode, sample_rate;
187 sample_rate = stream->enc->sample_rate;
188 coded_frame_size = (stream->enc->bit_rate *
189 stream->enc->frame_size) / (8 * sample_rate);
190 /* audio codec info */
193 put_be32(s, 0x00040000); /* version */
195 put_be32(s, 0x01b53530); /* stream length */
196 put_be16(s, 4); /* unknown */
197 put_be32(s, 0x39); /* header size */
199 switch(sample_rate) {
216 put_be16(s, fscode); /* codec additional info, for AC3, seems
217 to be a frequency code */
218 /* special hack to compensate rounding errors... */
219 if (coded_frame_size == 557)
221 put_be32(s, coded_frame_size); /* frame length */
222 put_be32(s, 0x51540); /* unknown */
223 put_be32(s, 0x249f0); /* unknown */
224 put_be32(s, 0x249f0); /* unknown */
226 /* frame length : seems to be very important */
227 put_be16(s, coded_frame_size);
228 put_be32(s, 0); /* unknown */
229 put_be16(s, stream->enc->sample_rate); /* sample rate */
230 put_be32(s, 0x10); /* unknown */
231 put_be16(s, stream->enc->channels);
232 put_str8(s, "Int0"); /* codec name */
233 put_str8(s, "dnet"); /* codec name */
234 put_be16(s, 0); /* title length */
235 put_be16(s, 0); /* author length */
236 put_be16(s, 0); /* copyright length */
237 put_byte(s, 0); /* end of header */
239 /* video codec info */
240 put_be32(s,34); /* size */
241 if(stream->enc->codec_id == CODEC_ID_RV10)
242 put_tag(s,"VIDORV10");
244 put_tag(s,"VIDORV20");
245 put_be16(s, stream->enc->width);
246 put_be16(s, stream->enc->height);
247 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
248 put_be32(s,0); /* unknown meaning */
249 put_be16(s, (int) stream->frame_rate); /* unknown meaning */
250 put_be32(s,0); /* unknown meaning */
251 put_be16(s, 8); /* unknown meaning */
252 /* Seems to be the codec version: only use basic H263. The next
253 versions seems to add a diffential DC coding as in
254 MPEG... nothing new under the sun */
255 if(stream->enc->codec_id == CODEC_ID_RV10)
256 put_be32(s,0x10000000);
258 put_be32(s,0x20103001);
259 //put_be32(s,0x10003000);
263 /* patch data offset field */
264 data_pos = s->buf_ptr - start_ptr;
265 rm->data_pos = data_pos;
266 data_offset_ptr[0] = data_pos >> 24;
267 data_offset_ptr[1] = data_pos >> 16;
268 data_offset_ptr[2] = data_pos >> 8;
269 data_offset_ptr[3] = data_pos;
273 put_be32(s,data_size + 10 + 8);
276 put_be32(s, nb_packets); /* number of packets */
277 put_be32(s,0); /* next data header */
280 static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
281 int length, int key_frame)
284 ByteIOContext *s = &ctx->pb;
286 stream->nb_packets++;
287 stream->packet_total_size += length;
288 if (length > stream->packet_max_size)
289 stream->packet_max_size = length;
291 put_be16(s,0); /* version */
292 put_be16(s,length + 12);
293 put_be16(s, stream->num); /* stream number */
294 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
295 put_be32(s, timestamp); /* timestamp */
296 put_byte(s, 0); /* reserved */
297 put_byte(s, key_frame ? 2 : 0); /* flags */
300 static int rm_write_header(AVFormatContext *s)
302 RMContext *rm = s->priv_data;
305 AVCodecContext *codec;
307 for(n=0;n<s->nb_streams;n++) {
308 s->streams[n]->id = n;
309 codec = s->streams[n]->codec;
310 stream = &rm->streams[n];
311 memset(stream, 0, sizeof(StreamInfo));
313 stream->bit_rate = codec->bit_rate;
316 switch(codec->codec_type) {
317 case CODEC_TYPE_AUDIO:
318 rm->audio_stream = stream;
319 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
320 /* XXX: dummy values */
321 stream->packet_max_size = 1024;
322 stream->nb_packets = 0;
323 stream->total_frames = stream->nb_packets;
325 case CODEC_TYPE_VIDEO:
326 rm->video_stream = stream;
327 stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
328 /* XXX: dummy values */
329 stream->packet_max_size = 4096;
330 stream->nb_packets = 0;
331 stream->total_frames = stream->nb_packets;
338 rv10_write_header(s, 0, 0);
339 put_flush_packet(&s->pb);
343 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
346 RMContext *rm = s->priv_data;
347 ByteIOContext *pb = &s->pb;
348 StreamInfo *stream = rm->audio_stream;
351 /* XXX: suppress this malloc */
352 buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
354 write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
356 /* for AC3, the words seems to be reversed */
357 for(i=0;i<size;i+=2) {
361 put_buffer(pb, buf1, size);
362 put_flush_packet(pb);
368 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
370 RMContext *rm = s->priv_data;
371 ByteIOContext *pb = &s->pb;
372 StreamInfo *stream = rm->video_stream;
373 int key_frame = !!(flags & PKT_FLAG_KEY);
375 /* XXX: this is incorrect: should be a parameter */
377 /* Well, I spent some time finding the meaning of these bits. I am
378 not sure I understood everything, but it works !! */
380 write_packet_header(s, stream, size + 7, key_frame);
381 /* bit 7: '1' if final packet of a frame converted in several packets */
383 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
384 frame starting from 1 */
390 put_be16(pb, 0x4000 + (size)); /* total frame size */
391 put_be16(pb, 0x4000 + (size)); /* offset from the start or the end */
394 write_packet_header(s, size + 6);
396 put_be16(pb, 0x4000 + size); /* total frame size */
397 put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
399 put_byte(pb, stream->nb_frames & 0xff);
401 put_buffer(pb, buf, size);
402 put_flush_packet(pb);
408 static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
410 if (s->streams[pkt->stream_index]->codec->codec_type ==
412 return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
414 return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
417 static int rm_write_trailer(AVFormatContext *s)
419 RMContext *rm = s->priv_data;
420 int data_size, index_pos, i;
421 ByteIOContext *pb = &s->pb;
423 if (!url_is_streamed(&s->pb)) {
424 /* end of file: finish to write header */
425 index_pos = url_fseek(pb, 0, SEEK_CUR);
426 data_size = index_pos - rm->data_pos;
430 put_be32(pb, 10 + 10 * s->nb_streams);
433 for(i=0;i<s->nb_streams;i++) {
434 put_be32(pb, 0); /* zero indices */
435 put_be16(pb, i); /* stream number */
436 put_be32(pb, 0); /* next index */
438 /* undocumented end header */
442 url_fseek(pb, 0, SEEK_SET);
443 for(i=0;i<s->nb_streams;i++)
444 rm->streams[i].total_frames = rm->streams[i].nb_frames;
445 rv10_write_header(s, data_size, index_pos);
447 /* undocumented end header */
451 put_flush_packet(pb);
454 #endif //CONFIG_MUXERS
456 /***************************************************/
458 static void get_str(ByteIOContext *pb, char *buf, int buf_size)
466 if (i < buf_size - 1)
472 static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
480 if (i < buf_size - 1)
486 static void rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
489 RMContext *rm = s->priv_data;
490 ByteIOContext *pb = &s->pb;
496 version = get_be32(pb); /* version */
497 if (((version >> 16) & 0xff) == 3) {
498 int64_t startpos = url_ftell(pb);
499 /* very old version */
500 for(i = 0; i < 14; i++)
502 get_str8(pb, s->title, sizeof(s->title));
503 get_str8(pb, s->author, sizeof(s->author));
504 get_str8(pb, s->copyright, sizeof(s->copyright));
505 get_str8(pb, s->comment, sizeof(s->comment));
506 if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
507 // fourcc (should always be "lpcJ")
509 get_str8(pb, buf, sizeof(buf));
511 // Skip extra header crap (this should never happen)
512 if ((startpos + (version & 0xffff)) > url_ftell(pb))
513 url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
514 st->codec->sample_rate = 8000;
515 st->codec->channels = 1;
516 st->codec->codec_type = CODEC_TYPE_AUDIO;
517 st->codec->codec_id = CODEC_ID_RA_144;
519 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
520 /* old version (4) */
521 get_be32(pb); /* .ra4 */
522 get_be32(pb); /* data size */
523 get_be16(pb); /* version2 */
524 get_be32(pb); /* header size */
525 flavor= get_be16(pb); /* add codec info / flavor */
526 rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
527 get_be32(pb); /* ??? */
528 get_be32(pb); /* ??? */
529 get_be32(pb); /* ??? */
530 rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
531 st->codec->block_align= get_be16(pb); /* frame size */
532 rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
533 get_be16(pb); /* ??? */
534 if (((version >> 16) & 0xff) == 5) {
535 get_be16(pb); get_be16(pb); get_be16(pb); }
536 st->codec->sample_rate = get_be16(pb);
538 st->codec->channels = get_be16(pb);
539 if (((version >> 16) & 0xff) == 5) {
541 buf[0] = get_byte(pb);
542 buf[1] = get_byte(pb);
543 buf[2] = get_byte(pb);
544 buf[3] = get_byte(pb);
547 get_str8(pb, buf, sizeof(buf)); /* desc */
548 get_str8(pb, buf, sizeof(buf)); /* desc */
550 st->codec->codec_type = CODEC_TYPE_AUDIO;
551 if (!strcmp(buf, "dnet")) {
552 st->codec->codec_id = CODEC_ID_AC3;
553 } else if (!strcmp(buf, "28_8")) {
554 st->codec->codec_id = CODEC_ID_RA_288;
555 st->codec->extradata_size= 0;
556 rm->audio_framesize = st->codec->block_align;
557 st->codec->block_align = coded_framesize;
558 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
559 } else if (!strcmp(buf, "cook")) {
560 int codecdata_length, i;
561 get_be16(pb); get_byte(pb);
562 if (((version >> 16) & 0xff) == 5)
564 codecdata_length = get_be32(pb);
565 st->codec->codec_id = CODEC_ID_COOK;
566 st->codec->extradata_size= codecdata_length;
567 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
568 for(i = 0; i < codecdata_length; i++)
569 ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
570 rm->audio_framesize = st->codec->block_align;
571 st->codec->block_align = rm->sub_packet_size;
572 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
574 st->codec->codec_id = CODEC_ID_NONE;
575 pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
583 get_str8(pb, s->title, sizeof(s->title));
584 get_str8(pb, s->author, sizeof(s->author));
585 get_str8(pb, s->copyright, sizeof(s->copyright));
586 get_str8(pb, s->comment, sizeof(s->comment));
591 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
593 RMContext *rm = s->priv_data;
597 st = av_new_stream(s, 0);
600 rm_read_audio_stream_info(s, st, 1);
606 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
608 RMContext *rm = s->priv_data;
610 ByteIOContext *pb = &s->pb;
612 int tag_size, size, codec_data_size, i;
614 unsigned int h263_hack_version, start_time, duration;
619 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
620 /* very old .ra format */
621 return rm_read_header_old(s, ap);
622 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
626 get_be32(pb); /* header size */
629 get_be32(pb); /* number of headers */
635 tag_size = get_be32(pb);
638 printf("tag=%c%c%c%c (%08x) size=%d\n",
646 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
649 case MKTAG('P', 'R', 'O', 'P'):
651 get_be32(pb); /* max bit rate */
652 get_be32(pb); /* avg bit rate */
653 get_be32(pb); /* max packet size */
654 get_be32(pb); /* avg packet size */
655 get_be32(pb); /* nb packets */
656 get_be32(pb); /* duration */
657 get_be32(pb); /* preroll */
658 get_be32(pb); /* index offset */
659 get_be32(pb); /* data offset */
660 get_be16(pb); /* nb streams */
661 flags = get_be16(pb); /* flags */
663 case MKTAG('C', 'O', 'N', 'T'):
664 get_str(pb, s->title, sizeof(s->title));
665 get_str(pb, s->author, sizeof(s->author));
666 get_str(pb, s->copyright, sizeof(s->copyright));
667 get_str(pb, s->comment, sizeof(s->comment));
669 case MKTAG('M', 'D', 'P', 'R'):
670 st = av_new_stream(s, 0);
673 st->id = get_be16(pb);
674 get_be32(pb); /* max bit rate */
675 st->codec->bit_rate = get_be32(pb); /* bit rate */
676 get_be32(pb); /* max packet size */
677 get_be32(pb); /* avg packet size */
678 start_time = get_be32(pb); /* start time */
679 get_be32(pb); /* preroll */
680 duration = get_be32(pb); /* duration */
681 st->start_time = start_time;
682 st->duration = duration;
683 get_str8(pb, buf, sizeof(buf)); /* desc */
684 get_str8(pb, buf, sizeof(buf)); /* mimetype */
685 codec_data_size = get_be32(pb);
686 codec_pos = url_ftell(pb);
687 st->codec->codec_type = CODEC_TYPE_DATA;
688 av_set_pts_info(st, 64, 1, 1000);
691 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
693 rm_read_audio_stream_info(s, st, 0);
696 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
698 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
701 st->codec->codec_tag = get_le32(pb);
702 // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
703 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
704 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
705 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
706 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
708 st->codec->width = get_be16(pb);
709 st->codec->height = get_be16(pb);
710 st->codec->time_base.num= 1;
712 st->codec->codec_type = CODEC_TYPE_VIDEO;
717 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
718 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
719 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
721 // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
722 st->codec->time_base.den = fps * st->codec->time_base.num;
723 /* modification of h263 codec version (!) */
724 #ifdef WORDS_BIGENDIAN
725 h263_hack_version = ((uint32_t*)st->codec->extradata)[1];
727 h263_hack_version = bswap_32(((uint32_t*)st->codec->extradata)[1]);
729 st->codec->sub_id = h263_hack_version;
730 switch((h263_hack_version>>28)){
731 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
732 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
733 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
734 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
739 /* skip codec info */
740 size = url_ftell(pb) - codec_pos;
741 url_fskip(pb, codec_data_size - size);
743 case MKTAG('D', 'A', 'T', 'A'):
746 /* unknown tag: skip it */
747 url_fskip(pb, tag_size - 10);
752 rm->nb_packets = get_be32(pb); /* number of packets */
753 if (!rm->nb_packets && (flags & 4))
754 rm->nb_packets = 3600 * 25;
755 get_be32(pb); /* next data header */
759 for(i=0;i<s->nb_streams;i++) {
760 av_free(s->streams[i]);
765 static int get_num(ByteIOContext *pb, int *len)
776 return (n << 16) | n1;
780 /* multiple of 20 bytes for ra144 (ugly) */
781 #define RAW_PACKET_SIZE 1000
783 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
784 RMContext *rm = s->priv_data;
785 ByteIOContext *pb = &s->pb;
786 int len, num, res, i;
788 uint32_t state=0xFFFFFFFF;
790 while(!url_feof(pb)){
792 if(rm->remaining_len > 0){
793 num= rm->current_stream;
794 len= rm->remaining_len;
795 *timestamp = AV_NOPTS_VALUE;
798 state= (state<<8) + get_byte(pb);
800 if(state == MKBETAG('I', 'N', 'D', 'X')){
801 len = get_be16(pb) - 6;
807 if(state > (unsigned)0xFFFF || state < 12)
813 *timestamp = get_be32(pb);
814 res= get_byte(pb); /* reserved */
815 *flags = get_byte(pb); /* flags */
820 for(i=0;i<s->nb_streams;i++) {
825 if (i == s->nb_streams) {
827 /* skip packet if unknown number */
829 rm->remaining_len -= len;
839 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
841 RMContext *rm = s->priv_data;
842 ByteIOContext *pb = &s->pb;
845 int64_t timestamp, pos;
849 if (rm->old_format) {
850 /* just read raw bytes */
851 len = RAW_PACKET_SIZE;
852 len= av_get_packet(pb, pkt, len);
853 pkt->stream_index = 0;
859 } else if (rm->audio_pkt_cnt) {
860 // If there are queued audio packet return them first
861 st = s->streams[rm->audio_stream_num];
862 av_new_packet(pkt, st->codec->block_align);
863 memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
864 (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
865 st->codec->block_align);
868 pkt->stream_index = rm->audio_stream_num;
872 len=sync(s, ×tamp, &flags, &i, &pos);
877 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
878 int h, pic_num, len2, pos;
880 h= get_byte(pb); len--;
882 seq = get_byte(pb); len--;
885 if((h & 0xc0) == 0x40){
888 len2 = get_num(pb, &len);
889 pos = get_num(pb, &len);
892 pic_num= get_byte(pb); len--;
893 rm->remaining_len= len;
894 rm->current_stream= st->id;
896 // av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
899 rm->remaining_len-= len;
900 av_get_packet(pb, pkt, len);
903 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
904 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
905 (st->codec->codec_id == CODEC_ID_COOK)) {
907 int sps = rm->sub_packet_size;
908 int cfs = rm->coded_framesize;
909 int h = rm->sub_packet_h;
910 int y = rm->sub_packet_cnt;
911 int w = rm->audio_framesize;
914 y = rm->sub_packet_cnt = 0;
916 rm->audiotimestamp = timestamp;
918 switch(st->codec->codec_id) {
919 case CODEC_ID_RA_288:
920 for (x = 0; x < h/2; x++)
921 get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
924 for (x = 0; x < w/sps; x++)
925 get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
929 if (++(rm->sub_packet_cnt) < h)
932 rm->sub_packet_cnt = 0;
933 rm->audio_stream_num = i;
934 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
935 // Release first audio packet
936 av_new_packet(pkt, st->codec->block_align);
937 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
938 timestamp = rm->audiotimestamp;
939 flags = 2; // Mark first packet as keyframe
942 av_get_packet(pb, pkt, len);
945 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
946 || st->discard >= AVDISCARD_ALL){
951 pkt->stream_index = i;
954 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
955 if(st->codec->codec_id == CODEC_ID_RV20){
956 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
957 av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
959 seq |= (timestamp&~0x3FFF);
960 if(seq - timestamp > 0x2000) seq -= 0x4000;
961 if(seq - timestamp < -0x2000) seq += 0x4000;
967 pkt->flags |= PKT_FLAG_KEY;
969 av_add_index_entry(st, pos, timestamp, 0, AVINDEX_KEYFRAME);
973 /* for AC3, needs to swap bytes */
974 if (st->codec->codec_id == CODEC_ID_AC3) {
976 for(j=0;j<len;j+=2) {
986 static int rm_read_close(AVFormatContext *s)
988 RMContext *rm = s->priv_data;
990 av_free(rm->audiobuf);
994 static int rm_probe(AVProbeData *p)
996 /* check file header */
997 if (p->buf_size <= 32)
999 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1000 p->buf[2] == 'M' && p->buf[3] == 'F' &&
1001 p->buf[4] == 0 && p->buf[5] == 0) ||
1002 (p->buf[0] == '.' && p->buf[1] == 'r' &&
1003 p->buf[2] == 'a' && p->buf[3] == 0xfd))
1004 return AVPROBE_SCORE_MAX;
1009 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1010 int64_t *ppos, int64_t pos_limit)
1012 RMContext *rm = s->priv_data;
1014 int stream_index2, flags, len, h;
1019 return AV_NOPTS_VALUE;
1021 url_fseek(&s->pb, pos, SEEK_SET);
1022 rm->remaining_len=0;
1027 len=sync(s, &dts, &flags, &stream_index2, &pos);
1029 return AV_NOPTS_VALUE;
1031 st = s->streams[stream_index2];
1032 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1033 h= get_byte(&s->pb); len--;
1035 seq = get_byte(&s->pb); len--;
1039 if((flags&2) && (seq&0x7F) == 1){
1040 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
1041 av_add_index_entry(st, pos, dts, 0, AVINDEX_KEYFRAME);
1042 if(stream_index2 == stream_index)
1046 url_fskip(&s->pb, len);
1052 static AVInputFormat rm_iformat = {
1064 #ifdef CONFIG_MUXERS
1065 static AVOutputFormat rm_oformat = {
1068 "application/vnd.rn-realmedia",
1077 #endif //CONFIG_MUXERS
1081 av_register_input_format(&rm_iformat);
1082 #ifdef CONFIG_MUXERS
1083 av_register_output_format(&rm_oformat);
1084 #endif //CONFIG_MUXERS