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 #ifdef CONFIG_ENCODERS
46 static void put_str(ByteIOContext *s, const char *tag)
48 put_be16(s,strlen(tag));
54 static void put_str8(ByteIOContext *s, const char *tag)
56 put_byte(s, strlen(tag));
62 static void rv10_write_header(AVFormatContext *ctx,
63 int data_size, int index_pos)
65 RMContext *rm = ctx->priv_data;
66 ByteIOContext *s = &ctx->pb;
68 unsigned char *data_offset_ptr, *start_ptr;
69 const char *desc, *mimetype;
70 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
71 int bit_rate, v, duration, flags, data_pos;
73 start_ptr = s->buf_ptr;
76 put_be32(s,18); /* header size */
79 put_be32(s,4 + ctx->nb_streams); /* num headers */
85 packet_total_size = 0;
89 for(i=0;i<ctx->nb_streams;i++) {
90 StreamInfo *stream = &rm->streams[i];
91 bit_rate += stream->bit_rate;
92 if (stream->packet_max_size > packet_max_size)
93 packet_max_size = stream->packet_max_size;
94 nb_packets += stream->nb_packets;
95 packet_total_size += stream->packet_total_size;
96 /* select maximum duration */
97 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
101 put_be32(s, bit_rate); /* max bit rate */
102 put_be32(s, bit_rate); /* avg bit rate */
103 put_be32(s, packet_max_size); /* max packet size */
105 packet_avg_size = packet_total_size / nb_packets;
108 put_be32(s, packet_avg_size); /* avg packet size */
109 put_be32(s, nb_packets); /* num packets */
110 put_be32(s, duration); /* duration */
111 put_be32(s, BUFFER_DURATION); /* preroll */
112 put_be32(s, index_pos); /* index offset */
113 /* computation of data the data offset */
114 data_offset_ptr = s->buf_ptr;
115 put_be32(s, 0); /* data offset : will be patched after */
116 put_be16(s, ctx->nb_streams); /* num streams */
117 flags = 1 | 2; /* save allowed & perfect play */
118 if (url_is_streamed(s))
119 flags |= 4; /* live broadcast */
125 size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
126 strlen(ctx->comment) + 4 * 2 + 10;
129 put_str(s, ctx->title);
130 put_str(s, ctx->author);
131 put_str(s, ctx->copyright);
132 put_str(s, ctx->comment);
134 for(i=0;i<ctx->nb_streams;i++) {
137 stream = &rm->streams[i];
139 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
140 desc = "The Audio Stream";
141 mimetype = "audio/x-pn-realaudio";
142 codec_data_size = 73;
144 desc = "The Video Stream";
145 mimetype = "video/x-pn-realvideo";
146 codec_data_size = 34;
150 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
154 put_be16(s, i); /* stream number */
155 put_be32(s, stream->bit_rate); /* max bit rate */
156 put_be32(s, stream->bit_rate); /* avg bit rate */
157 put_be32(s, stream->packet_max_size); /* max packet size */
158 if (stream->nb_packets > 0)
159 packet_avg_size = stream->packet_total_size /
163 put_be32(s, packet_avg_size); /* avg packet size */
164 put_be32(s, 0); /* start time */
165 put_be32(s, BUFFER_DURATION); /* preroll */
167 if (url_is_streamed(s) || !stream->total_frames)
168 put_be32(s, (int)(3600 * 1000));
170 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
172 put_str8(s, mimetype);
173 put_be32(s, codec_data_size);
175 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
176 int coded_frame_size, fscode, sample_rate;
177 sample_rate = stream->enc->sample_rate;
178 coded_frame_size = (stream->enc->bit_rate *
179 stream->enc->frame_size) / (8 * sample_rate);
180 /* audio codec info */
183 put_be32(s, 0x00040000); /* version */
185 put_be32(s, 0x01b53530); /* stream length */
186 put_be16(s, 4); /* unknown */
187 put_be32(s, 0x39); /* header size */
189 switch(sample_rate) {
206 put_be16(s, fscode); /* codec additional info, for AC3, seems
207 to be a frequency code */
208 /* special hack to compensate rounding errors... */
209 if (coded_frame_size == 557)
211 put_be32(s, coded_frame_size); /* frame length */
212 put_be32(s, 0x51540); /* unknown */
213 put_be32(s, 0x249f0); /* unknown */
214 put_be32(s, 0x249f0); /* unknown */
216 /* frame length : seems to be very important */
217 put_be16(s, coded_frame_size);
218 put_be32(s, 0); /* unknown */
219 put_be16(s, stream->enc->sample_rate); /* sample rate */
220 put_be32(s, 0x10); /* unknown */
221 put_be16(s, stream->enc->channels);
222 put_str8(s, "Int0"); /* codec name */
223 put_str8(s, "dnet"); /* codec name */
224 put_be16(s, 0); /* title length */
225 put_be16(s, 0); /* author length */
226 put_be16(s, 0); /* copyright length */
227 put_byte(s, 0); /* end of header */
229 /* video codec info */
230 put_be32(s,34); /* size */
231 put_tag(s,"VIDORV10");
232 put_be16(s, stream->enc->width);
233 put_be16(s, stream->enc->height);
234 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
235 put_be32(s,0); /* unknown meaning */
236 put_be16(s, (int) stream->frame_rate); /* unknown meaning */
237 put_be32(s,0); /* unknown meaning */
238 put_be16(s, 8); /* unknown meaning */
239 /* Seems to be the codec version: only use basic H263. The next
240 versions seems to add a diffential DC coding as in
241 MPEG... nothing new under the sun */
242 put_be32(s,0x10000000);
243 //put_be32(s,0x10003000);
247 /* patch data offset field */
248 data_pos = s->buf_ptr - start_ptr;
249 rm->data_pos = data_pos;
250 data_offset_ptr[0] = data_pos >> 24;
251 data_offset_ptr[1] = data_pos >> 16;
252 data_offset_ptr[2] = data_pos >> 8;
253 data_offset_ptr[3] = data_pos;
257 put_be32(s,data_size + 10 + 8);
260 put_be32(s, nb_packets); /* number of packets */
261 put_be32(s,0); /* next data header */
264 static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
265 int length, int key_frame)
268 ByteIOContext *s = &ctx->pb;
270 stream->nb_packets++;
271 stream->packet_total_size += length;
272 if (length > stream->packet_max_size)
273 stream->packet_max_size = length;
275 put_be16(s,0); /* version */
276 put_be16(s,length + 12);
277 put_be16(s, stream->num); /* stream number */
278 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
279 put_be32(s, timestamp); /* timestamp */
280 put_byte(s, 0); /* reserved */
281 put_byte(s, key_frame ? 2 : 0); /* flags */
284 static int rm_write_header(AVFormatContext *s)
286 RMContext *rm = s->priv_data;
289 AVCodecContext *codec;
291 for(n=0;n<s->nb_streams;n++) {
292 s->streams[n]->id = n;
293 codec = &s->streams[n]->codec;
294 stream = &rm->streams[n];
295 memset(stream, 0, sizeof(StreamInfo));
297 stream->bit_rate = codec->bit_rate;
300 switch(codec->codec_type) {
301 case CODEC_TYPE_AUDIO:
302 rm->audio_stream = stream;
303 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
304 /* XXX: dummy values */
305 stream->packet_max_size = 1024;
306 stream->nb_packets = 0;
307 stream->total_frames = stream->nb_packets;
309 case CODEC_TYPE_VIDEO:
310 rm->video_stream = stream;
311 stream->frame_rate = (float)codec->frame_rate / (float)codec->frame_rate_base;
312 /* XXX: dummy values */
313 stream->packet_max_size = 4096;
314 stream->nb_packets = 0;
315 stream->total_frames = stream->nb_packets;
322 rv10_write_header(s, 0, 0);
323 put_flush_packet(&s->pb);
327 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size)
330 RMContext *rm = s->priv_data;
331 ByteIOContext *pb = &s->pb;
332 StreamInfo *stream = rm->audio_stream;
335 /* XXX: suppress this malloc */
336 buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
338 write_packet_header(s, stream, size, stream->enc->coded_frame->key_frame);
340 /* for AC3, the words seems to be reversed */
341 for(i=0;i<size;i+=2) {
345 put_buffer(pb, buf1, size);
346 put_flush_packet(pb);
352 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size)
354 RMContext *rm = s->priv_data;
355 ByteIOContext *pb = &s->pb;
356 StreamInfo *stream = rm->video_stream;
357 int key_frame = stream->enc->coded_frame->key_frame;
359 /* XXX: this is incorrect: should be a parameter */
361 /* Well, I spent some time finding the meaning of these bits. I am
362 not sure I understood everything, but it works !! */
364 write_packet_header(s, stream, size + 7, key_frame);
365 /* bit 7: '1' if final packet of a frame converted in several packets */
367 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
368 frame starting from 1 */
374 put_be16(pb, 0x4000 | (size)); /* total frame size */
375 put_be16(pb, 0x4000 | (size)); /* offset from the start or the end */
378 write_packet_header(s, size + 6);
380 put_be16(pb, 0x4000 | size); /* total frame size */
381 put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
383 put_byte(pb, stream->nb_frames & 0xff);
385 put_buffer(pb, buf, size);
386 put_flush_packet(pb);
392 static int rm_write_packet(AVFormatContext *s, int stream_index,
393 const uint8_t *buf, int size, int64_t pts)
395 if (s->streams[stream_index]->codec.codec_type ==
397 return rm_write_audio(s, buf, size);
399 return rm_write_video(s, buf, size);
402 static int rm_write_trailer(AVFormatContext *s)
404 RMContext *rm = s->priv_data;
405 int data_size, index_pos, i;
406 ByteIOContext *pb = &s->pb;
408 if (!url_is_streamed(&s->pb)) {
409 /* end of file: finish to write header */
410 index_pos = url_fseek(pb, 0, SEEK_CUR);
411 data_size = index_pos - rm->data_pos;
415 put_be32(pb, 10 + 10 * s->nb_streams);
418 for(i=0;i<s->nb_streams;i++) {
419 put_be32(pb, 0); /* zero indices */
420 put_be16(pb, i); /* stream number */
421 put_be32(pb, 0); /* next index */
423 /* undocumented end header */
427 url_fseek(pb, 0, SEEK_SET);
428 for(i=0;i<s->nb_streams;i++)
429 rm->streams[i].total_frames = rm->streams[i].nb_frames;
430 rv10_write_header(s, data_size, index_pos);
432 /* undocumented end header */
436 put_flush_packet(pb);
439 #endif //CONFIG_ENCODERS
441 /***************************************************/
443 static void get_str(ByteIOContext *pb, char *buf, int buf_size)
451 if (i < buf_size - 1)
457 static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
465 if (i < buf_size - 1)
471 static void rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
474 ByteIOContext *pb = &s->pb;
480 version = get_be32(pb); /* version */
481 if (((version >> 16) & 0xff) == 3) {
482 /* very old version */
483 for(i = 0; i < 14; i++)
485 get_str8(pb, s->title, sizeof(s->title));
486 get_str8(pb, s->author, sizeof(s->author));
487 get_str8(pb, s->copyright, sizeof(s->copyright));
488 get_str8(pb, s->comment, sizeof(s->comment));
490 get_str8(pb, buf, sizeof(buf));
491 st->codec.sample_rate = 8000;
492 st->codec.channels = 1;
493 st->codec.codec_type = CODEC_TYPE_AUDIO;
494 st->codec.codec_id = CODEC_ID_RA_144;
496 /* old version (4) */
497 get_be32(pb); /* .ra4 */
500 get_be32(pb); /* header size */
501 get_be16(pb); /* add codec info */
502 get_be32(pb); /* coded frame size */
503 get_be32(pb); /* ??? */
504 get_be32(pb); /* ??? */
505 get_be32(pb); /* ??? */
506 get_be16(pb); /* 1 */
507 get_be16(pb); /* coded frame size */
509 st->codec.sample_rate = get_be16(pb);
511 st->codec.channels = get_be16(pb);
512 get_str8(pb, buf, sizeof(buf)); /* desc */
513 get_str8(pb, buf, sizeof(buf)); /* desc */
514 st->codec.codec_type = CODEC_TYPE_AUDIO;
515 if (!strcmp(buf, "dnet")) {
516 st->codec.codec_id = CODEC_ID_AC3;
518 st->codec.codec_id = CODEC_ID_NONE;
519 pstrcpy(st->codec.codec_name, sizeof(st->codec.codec_name),
527 get_str8(pb, s->title, sizeof(s->title));
528 get_str8(pb, s->author, sizeof(s->author));
529 get_str8(pb, s->copyright, sizeof(s->copyright));
530 get_str8(pb, s->comment, sizeof(s->comment));
535 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
537 RMContext *rm = s->priv_data;
541 st = av_new_stream(s, 0);
544 rm_read_audio_stream_info(s, st, 1);
550 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
552 RMContext *rm = s->priv_data;
554 ByteIOContext *pb = &s->pb;
556 int tag_size, size, codec_data_size, i;
558 unsigned int h263_hack_version, start_time, duration;
563 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
564 /* very old .ra format */
565 return rm_read_header_old(s, ap);
566 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
570 get_be32(pb); /* header size */
573 get_be32(pb); /* number of headers */
579 tag_size = get_be32(pb);
582 printf("tag=%c%c%c%c (%08x) size=%d\n",
593 case MKTAG('P', 'R', 'O', 'P'):
595 get_be32(pb); /* max bit rate */
596 get_be32(pb); /* avg bit rate */
597 get_be32(pb); /* max packet size */
598 get_be32(pb); /* avg packet size */
599 get_be32(pb); /* nb packets */
600 get_be32(pb); /* duration */
601 get_be32(pb); /* preroll */
602 get_be32(pb); /* index offset */
603 get_be32(pb); /* data offset */
604 get_be16(pb); /* nb streams */
605 flags = get_be16(pb); /* flags */
607 case MKTAG('C', 'O', 'N', 'T'):
608 get_str(pb, s->title, sizeof(s->title));
609 get_str(pb, s->author, sizeof(s->author));
610 get_str(pb, s->copyright, sizeof(s->copyright));
611 get_str(pb, s->comment, sizeof(s->comment));
613 case MKTAG('M', 'D', 'P', 'R'):
614 st = av_new_stream(s, 0);
617 st->id = get_be16(pb);
618 get_be32(pb); /* max bit rate */
619 st->codec.bit_rate = get_be32(pb); /* bit rate */
620 get_be32(pb); /* max packet size */
621 get_be32(pb); /* avg packet size */
622 start_time = get_be32(pb); /* start time */
623 get_be32(pb); /* preroll */
624 duration = get_be32(pb); /* duration */
625 st->start_time = start_time * (AV_TIME_BASE / 1000);
626 st->duration = duration * (AV_TIME_BASE / 1000);
627 get_str8(pb, buf, sizeof(buf)); /* desc */
628 get_str8(pb, buf, sizeof(buf)); /* mimetype */
629 codec_data_size = get_be32(pb);
630 codec_pos = url_ftell(pb);
633 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
635 rm_read_audio_stream_info(s, st, 0);
637 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
639 av_log(&st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
642 st->codec.codec_tag = get_le32(pb);
643 if (st->codec.codec_tag != MKTAG('R', 'V', '1', '0'))
645 st->codec.width = get_be16(pb);
646 st->codec.height = get_be16(pb);
647 st->codec.frame_rate_base= 1;
648 st->codec.frame_rate = get_be16(pb) * st->codec.frame_rate_base;
649 st->codec.codec_type = CODEC_TYPE_VIDEO;
654 /* modification of h263 codec version (!) */
655 h263_hack_version = get_be32(pb);
656 switch(h263_hack_version) {
660 st->codec.sub_id = h263_hack_version;
661 st->codec.codec_id = CODEC_ID_RV10;
665 st->codec.codec_id = CODEC_ID_NONE;
669 /* skip codec info */
670 size = url_ftell(pb) - codec_pos;
671 url_fskip(pb, codec_data_size - size);
673 case MKTAG('D', 'A', 'T', 'A'):
676 /* unknown tag: skip it */
677 url_fskip(pb, tag_size - 10);
682 rm->nb_packets = get_be32(pb); /* number of packets */
683 if (!rm->nb_packets && (flags & 4))
684 rm->nb_packets = 3600 * 25;
685 get_be32(pb); /* next data header */
689 for(i=0;i<s->nb_streams;i++) {
690 av_free(s->streams[i]);
695 static int get_num(ByteIOContext *pb, int *len)
706 return (n << 16) | n1;
710 /* multiple of 20 bytes for ra144 (ugly) */
711 #define RAW_PACKET_SIZE 1000
713 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
715 RMContext *rm = s->priv_data;
716 ByteIOContext *pb = &s->pb;
718 int len, num, timestamp, i, tmp, j;
722 if (rm->old_format) {
723 /* just read raw bytes */
724 len = RAW_PACKET_SIZE;
725 av_new_packet(pkt, len);
726 pkt->stream_index = 0;
727 len = get_buffer(pb, pkt->data, len);
736 if (rm->nb_packets == 0)
743 timestamp = get_be32(pb);
744 get_byte(pb); /* reserved */
745 flags = get_byte(pb); /* flags */
750 for(i=0;i<s->nb_streams;i++) {
755 if (i == s->nb_streams) {
756 /* skip packet if unknown number */
761 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
762 int full_frame, h, pic_num;
765 if ((h & 0xc0) == 0xc0) {
768 len2= get_num(pb, &len);
769 pos = get_num(pb, &len);
770 //printf("pos:%d\n",len);
773 int seq, frame_size, pos;
776 frame_size = get_num(pb, &len);
777 pos = get_num(pb, &len);
778 //printf("seq:%d, size:%d, pos:%d\n",seq,frame_size,pos);
782 pic_num= get_byte(pb);
784 //XXX/FIXME/HACK, demuxer should be fixed to send complete frames ...
785 if(st->codec.slice_offset==NULL)
786 st->codec.slice_offset= (int*)av_malloc(sizeof(int));
787 st->codec.slice_count= full_frame;
788 st->codec.slice_offset[0]= 0;
791 av_new_packet(pkt, len);
792 pkt->stream_index = i;
793 get_buffer(pb, pkt->data, len);
796 /* for AC3, needs to swap bytes */
797 if (st->codec.codec_id == CODEC_ID_AC3) {
799 for(j=0;j<len;j+=2) {
809 static int rm_read_close(AVFormatContext *s)
814 static int rm_probe(AVProbeData *p)
816 /* check file header */
817 if (p->buf_size <= 32)
819 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
820 p->buf[2] == 'M' && p->buf[3] == 'F' &&
821 p->buf[4] == 0 && p->buf[5] == 0) ||
822 (p->buf[0] == '.' && p->buf[1] == 'r' &&
823 p->buf[2] == 'a' && p->buf[3] == 0xfd))
824 return AVPROBE_SCORE_MAX;
829 static AVInputFormat rm_iformat = {
839 #ifdef CONFIG_ENCODERS
840 static AVOutputFormat rm_oformat = {
843 "application/vnd.rn-realmedia",
852 #endif //CONFIG_ENCODERS
856 av_register_input_format(&rm_iformat);
857 #ifdef CONFIG_ENCODERS
858 av_register_output_format(&rm_oformat);
859 #endif //CONFIG_ENCODERS