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 /* very old version */
499 for(i = 0; i < 14; i++)
501 get_str8(pb, s->title, sizeof(s->title));
502 get_str8(pb, s->author, sizeof(s->author));
503 get_str8(pb, s->copyright, sizeof(s->copyright));
504 get_str8(pb, s->comment, sizeof(s->comment));
506 get_str8(pb, buf, sizeof(buf));
507 st->codec->sample_rate = 8000;
508 st->codec->channels = 1;
509 st->codec->codec_type = CODEC_TYPE_AUDIO;
510 st->codec->codec_id = CODEC_ID_RA_144;
512 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
513 /* old version (4) */
514 get_be32(pb); /* .ra4 */
515 get_be32(pb); /* data size */
516 get_be16(pb); /* version2 */
517 get_be32(pb); /* header size */
518 flavor= get_be16(pb); /* add codec info / flavor */
519 rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
520 get_be32(pb); /* ??? */
521 get_be32(pb); /* ??? */
522 get_be32(pb); /* ??? */
523 rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
524 st->codec->block_align= get_be16(pb); /* frame size */
525 rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
526 get_be16(pb); /* ??? */
527 if (((version >> 16) & 0xff) == 5) {
528 get_be16(pb); get_be16(pb); get_be16(pb); }
529 st->codec->sample_rate = get_be16(pb);
531 st->codec->channels = get_be16(pb);
532 if (((version >> 16) & 0xff) == 5) {
534 buf[0] = get_byte(pb);
535 buf[1] = get_byte(pb);
536 buf[2] = get_byte(pb);
537 buf[3] = get_byte(pb);
540 get_str8(pb, buf, sizeof(buf)); /* desc */
541 get_str8(pb, buf, sizeof(buf)); /* desc */
543 st->codec->codec_type = CODEC_TYPE_AUDIO;
544 if (!strcmp(buf, "dnet")) {
545 st->codec->codec_id = CODEC_ID_AC3;
546 } else if (!strcmp(buf, "28_8")) {
547 st->codec->codec_id = CODEC_ID_RA_288;
548 st->codec->extradata_size= 0;
549 rm->audio_framesize = st->codec->block_align;
550 st->codec->block_align = coded_framesize;
551 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
552 } else if (!strcmp(buf, "cook")) {
553 int codecdata_length, i;
554 get_be16(pb); get_byte(pb);
555 if (((version >> 16) & 0xff) == 5)
557 codecdata_length = get_be32(pb);
558 st->codec->codec_id = CODEC_ID_COOK;
559 st->codec->extradata_size= codecdata_length;
560 st->codec->extradata= av_mallocz(st->codec->extradata_size);
561 for(i = 0; i < codecdata_length; i++)
562 ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
563 rm->audio_framesize = st->codec->block_align;
564 st->codec->block_align = rm->sub_packet_size;
565 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
567 st->codec->codec_id = CODEC_ID_NONE;
568 pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
576 get_str8(pb, s->title, sizeof(s->title));
577 get_str8(pb, s->author, sizeof(s->author));
578 get_str8(pb, s->copyright, sizeof(s->copyright));
579 get_str8(pb, s->comment, sizeof(s->comment));
584 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
586 RMContext *rm = s->priv_data;
590 st = av_new_stream(s, 0);
593 rm_read_audio_stream_info(s, st, 1);
599 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
601 RMContext *rm = s->priv_data;
603 ByteIOContext *pb = &s->pb;
605 int tag_size, size, codec_data_size, i;
607 unsigned int h263_hack_version, start_time, duration;
612 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
613 /* very old .ra format */
614 return rm_read_header_old(s, ap);
615 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
619 get_be32(pb); /* header size */
622 get_be32(pb); /* number of headers */
628 tag_size = get_be32(pb);
631 printf("tag=%c%c%c%c (%08x) size=%d\n",
639 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
642 case MKTAG('P', 'R', 'O', 'P'):
644 get_be32(pb); /* max bit rate */
645 get_be32(pb); /* avg bit rate */
646 get_be32(pb); /* max packet size */
647 get_be32(pb); /* avg packet size */
648 get_be32(pb); /* nb packets */
649 get_be32(pb); /* duration */
650 get_be32(pb); /* preroll */
651 get_be32(pb); /* index offset */
652 get_be32(pb); /* data offset */
653 get_be16(pb); /* nb streams */
654 flags = get_be16(pb); /* flags */
656 case MKTAG('C', 'O', 'N', 'T'):
657 get_str(pb, s->title, sizeof(s->title));
658 get_str(pb, s->author, sizeof(s->author));
659 get_str(pb, s->copyright, sizeof(s->copyright));
660 get_str(pb, s->comment, sizeof(s->comment));
662 case MKTAG('M', 'D', 'P', 'R'):
663 st = av_new_stream(s, 0);
666 st->id = get_be16(pb);
667 get_be32(pb); /* max bit rate */
668 st->codec->bit_rate = get_be32(pb); /* bit rate */
669 get_be32(pb); /* max packet size */
670 get_be32(pb); /* avg packet size */
671 start_time = get_be32(pb); /* start time */
672 get_be32(pb); /* preroll */
673 duration = get_be32(pb); /* duration */
674 st->start_time = start_time;
675 st->duration = duration;
676 get_str8(pb, buf, sizeof(buf)); /* desc */
677 get_str8(pb, buf, sizeof(buf)); /* mimetype */
678 codec_data_size = get_be32(pb);
679 codec_pos = url_ftell(pb);
680 st->codec->codec_type = CODEC_TYPE_DATA;
681 av_set_pts_info(st, 64, 1, 1000);
684 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
686 rm_read_audio_stream_info(s, st, 0);
689 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
691 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
694 st->codec->codec_tag = get_le32(pb);
695 // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
696 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
697 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
698 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
699 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
701 st->codec->width = get_be16(pb);
702 st->codec->height = get_be16(pb);
703 st->codec->time_base.num= 1;
705 st->codec->codec_type = CODEC_TYPE_VIDEO;
710 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
711 st->codec->extradata= av_malloc(st->codec->extradata_size);
712 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
714 // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
715 st->codec->time_base.den = fps * st->codec->time_base.num;
716 /* modification of h263 codec version (!) */
717 #ifdef WORDS_BIGENDIAN
718 h263_hack_version = ((uint32_t*)st->codec->extradata)[1];
720 h263_hack_version = bswap_32(((uint32_t*)st->codec->extradata)[1]);
722 st->codec->sub_id = h263_hack_version;
723 switch((h263_hack_version>>28)){
724 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
725 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
726 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
727 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
732 /* skip codec info */
733 size = url_ftell(pb) - codec_pos;
734 url_fskip(pb, codec_data_size - size);
736 case MKTAG('D', 'A', 'T', 'A'):
739 /* unknown tag: skip it */
740 url_fskip(pb, tag_size - 10);
745 rm->nb_packets = get_be32(pb); /* number of packets */
746 if (!rm->nb_packets && (flags & 4))
747 rm->nb_packets = 3600 * 25;
748 get_be32(pb); /* next data header */
752 for(i=0;i<s->nb_streams;i++) {
753 av_free(s->streams[i]);
758 static int get_num(ByteIOContext *pb, int *len)
769 return (n << 16) | n1;
773 /* multiple of 20 bytes for ra144 (ugly) */
774 #define RAW_PACKET_SIZE 1000
776 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
777 RMContext *rm = s->priv_data;
778 ByteIOContext *pb = &s->pb;
779 int len, num, res, i;
781 uint32_t state=0xFFFFFFFF;
783 while(!url_feof(pb)){
785 if(rm->remaining_len > 0){
786 num= rm->current_stream;
787 len= rm->remaining_len;
788 *timestamp = AV_NOPTS_VALUE;
791 state= (state<<8) + get_byte(pb);
793 if(state == MKBETAG('I', 'N', 'D', 'X')){
794 len = get_be16(pb) - 6;
800 if(state > (unsigned)0xFFFF || state < 12)
806 *timestamp = get_be32(pb);
807 res= get_byte(pb); /* reserved */
808 *flags = get_byte(pb); /* flags */
813 for(i=0;i<s->nb_streams;i++) {
818 if (i == s->nb_streams) {
820 /* skip packet if unknown number */
822 rm->remaining_len -= len;
832 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
834 RMContext *rm = s->priv_data;
835 ByteIOContext *pb = &s->pb;
838 int64_t timestamp, pos;
842 if (rm->old_format) {
843 /* just read raw bytes */
844 len = RAW_PACKET_SIZE;
845 len= av_get_packet(pb, pkt, len);
846 pkt->stream_index = 0;
852 } else if (rm->audio_pkt_cnt) {
853 // If there are queued audio packet return them first
854 st = s->streams[rm->audio_stream_num];
855 av_new_packet(pkt, st->codec->block_align);
856 memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
857 (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
858 st->codec->block_align);
861 pkt->stream_index = rm->audio_stream_num;
865 len=sync(s, ×tamp, &flags, &i, &pos);
870 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
871 int h, pic_num, len2, pos;
873 h= get_byte(pb); len--;
875 seq = get_byte(pb); len--;
878 if((h & 0xc0) == 0x40){
881 len2 = get_num(pb, &len);
882 pos = get_num(pb, &len);
885 pic_num= get_byte(pb); len--;
886 rm->remaining_len= len;
887 rm->current_stream= st->id;
889 // av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
892 rm->remaining_len-= len;
893 av_get_packet(pb, pkt, len);
896 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
897 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
898 (st->codec->codec_id == CODEC_ID_COOK)) {
900 int sps = rm->sub_packet_size;
901 int cfs = rm->coded_framesize;
902 int h = rm->sub_packet_h;
903 int y = rm->sub_packet_cnt;
904 int w = rm->audio_framesize;
907 y = rm->sub_packet_cnt = 0;
909 rm->audiotimestamp = timestamp;
911 switch(st->codec->codec_id) {
912 case CODEC_ID_RA_288:
913 for (x = 0; x < h/2; x++)
914 get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
917 for (x = 0; x < w/sps; x++)
918 get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
922 if (++(rm->sub_packet_cnt) < h)
925 rm->sub_packet_cnt = 0;
926 rm->audio_stream_num = i;
927 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
928 // Release first audio packet
929 av_new_packet(pkt, st->codec->block_align);
930 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
931 timestamp = rm->audiotimestamp;
932 flags = 2; // Mark first packet as keyframe
935 av_get_packet(pb, pkt, len);
938 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
939 || st->discard >= AVDISCARD_ALL){
944 pkt->stream_index = i;
947 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
948 if(st->codec->codec_id == CODEC_ID_RV20){
949 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
950 av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
952 seq |= (timestamp&~0x3FFF);
953 if(seq - timestamp > 0x2000) seq -= 0x4000;
954 if(seq - timestamp < -0x2000) seq += 0x4000;
960 pkt->flags |= PKT_FLAG_KEY;
962 av_add_index_entry(st, pos, timestamp, 0, AVINDEX_KEYFRAME);
966 /* for AC3, needs to swap bytes */
967 if (st->codec->codec_id == CODEC_ID_AC3) {
969 for(j=0;j<len;j+=2) {
979 static int rm_read_close(AVFormatContext *s)
981 RMContext *rm = s->priv_data;
983 av_free(rm->audiobuf);
987 static int rm_probe(AVProbeData *p)
989 /* check file header */
990 if (p->buf_size <= 32)
992 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
993 p->buf[2] == 'M' && p->buf[3] == 'F' &&
994 p->buf[4] == 0 && p->buf[5] == 0) ||
995 (p->buf[0] == '.' && p->buf[1] == 'r' &&
996 p->buf[2] == 'a' && p->buf[3] == 0xfd))
997 return AVPROBE_SCORE_MAX;
1002 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1003 int64_t *ppos, int64_t pos_limit)
1005 RMContext *rm = s->priv_data;
1007 int stream_index2, flags, len, h;
1012 return AV_NOPTS_VALUE;
1014 url_fseek(&s->pb, pos, SEEK_SET);
1015 rm->remaining_len=0;
1020 len=sync(s, &dts, &flags, &stream_index2, &pos);
1022 return AV_NOPTS_VALUE;
1024 st = s->streams[stream_index2];
1025 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1026 h= get_byte(&s->pb); len--;
1028 seq = get_byte(&s->pb); len--;
1032 if((flags&2) && (seq&0x7F) == 1){
1033 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
1034 av_add_index_entry(st, pos, dts, 0, AVINDEX_KEYFRAME);
1035 if(stream_index2 == stream_index)
1039 url_fskip(&s->pb, len);
1045 static AVInputFormat rm_iformat = {
1057 #ifdef CONFIG_MUXERS
1058 static AVOutputFormat rm_oformat = {
1061 "application/vnd.rn-realmedia",
1070 #endif //CONFIG_MUXERS
1074 av_register_input_format(&rm_iformat);
1075 #ifdef CONFIG_MUXERS
1076 av_register_output_format(&rm_oformat);
1077 #endif //CONFIG_MUXERS