changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext...
[libav.git] / libavformat / rm.c
CommitLineData
de6d9b64
FB
1/*
2 * "Real" compatible mux and demux.
19720f15 3 * Copyright (c) 2000, 2001 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20
21/* in ms */
22#define BUFFER_DURATION 0
23
24typedef struct {
25 int nb_packets;
26 int packet_total_size;
27 int packet_max_size;
28 /* codec related output */
29 int bit_rate;
30 float frame_rate;
31 int nb_frames; /* current frame number */
32 int total_frames; /* total number of frames */
33 int num;
34 AVCodecContext *enc;
35} StreamInfo;
36
37typedef struct {
38 StreamInfo streams[2];
39 StreamInfo *audio_stream, *video_stream;
40 int data_pos; /* position of the data after the header */
41 int nb_packets;
c9510803 42 int old_format;
a8b81ec1
MN
43 int current_stream;
44 int remaining_len;
de6d9b64
FB
45} RMContext;
46
764ef400 47#ifdef CONFIG_ENCODERS
de6d9b64
FB
48static void put_str(ByteIOContext *s, const char *tag)
49{
50 put_be16(s,strlen(tag));
51 while (*tag) {
52 put_byte(s, *tag++);
53 }
54}
55
56static void put_str8(ByteIOContext *s, const char *tag)
57{
58 put_byte(s, strlen(tag));
59 while (*tag) {
60 put_byte(s, *tag++);
61 }
62}
63
64static void rv10_write_header(AVFormatContext *ctx,
65 int data_size, int index_pos)
66{
67 RMContext *rm = ctx->priv_data;
68 ByteIOContext *s = &ctx->pb;
69 StreamInfo *stream;
70 unsigned char *data_offset_ptr, *start_ptr;
71 const char *desc, *mimetype;
72 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
73 int bit_rate, v, duration, flags, data_pos;
74
75 start_ptr = s->buf_ptr;
76
77 put_tag(s, ".RMF");
78 put_be32(s,18); /* header size */
79 put_be16(s,0);
80 put_be32(s,0);
81 put_be32(s,4 + ctx->nb_streams); /* num headers */
82
83 put_tag(s,"PROP");
84 put_be32(s, 50);
85 put_be16(s, 0);
86 packet_max_size = 0;
87 packet_total_size = 0;
88 nb_packets = 0;
89 bit_rate = 0;
90 duration = 0;
91 for(i=0;i<ctx->nb_streams;i++) {
92 StreamInfo *stream = &rm->streams[i];
93 bit_rate += stream->bit_rate;
94 if (stream->packet_max_size > packet_max_size)
95 packet_max_size = stream->packet_max_size;
96 nb_packets += stream->nb_packets;
97 packet_total_size += stream->packet_total_size;
98 /* select maximum duration */
99 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
100 if (v > duration)
101 duration = v;
102 }
103 put_be32(s, bit_rate); /* max bit rate */
104 put_be32(s, bit_rate); /* avg bit rate */
105 put_be32(s, packet_max_size); /* max packet size */
106 if (nb_packets > 0)
107 packet_avg_size = packet_total_size / nb_packets;
108 else
109 packet_avg_size = 0;
110 put_be32(s, packet_avg_size); /* avg packet size */
111 put_be32(s, nb_packets); /* num packets */
112 put_be32(s, duration); /* duration */
113 put_be32(s, BUFFER_DURATION); /* preroll */
114 put_be32(s, index_pos); /* index offset */
115 /* computation of data the data offset */
116 data_offset_ptr = s->buf_ptr;
117 put_be32(s, 0); /* data offset : will be patched after */
118 put_be16(s, ctx->nb_streams); /* num streams */
119 flags = 1 | 2; /* save allowed & perfect play */
120 if (url_is_streamed(s))
121 flags |= 4; /* live broadcast */
122 put_be16(s, flags);
123
124 /* comments */
125
126 put_tag(s,"CONT");
127 size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
128 strlen(ctx->comment) + 4 * 2 + 10;
129 put_be32(s,size);
130 put_be16(s,0);
131 put_str(s, ctx->title);
132 put_str(s, ctx->author);
133 put_str(s, ctx->copyright);
134 put_str(s, ctx->comment);
135
136 for(i=0;i<ctx->nb_streams;i++) {
137 int codec_data_size;
138
139 stream = &rm->streams[i];
140
141 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
142 desc = "The Audio Stream";
143 mimetype = "audio/x-pn-realaudio";
144 codec_data_size = 73;
145 } else {
146 desc = "The Video Stream";
147 mimetype = "video/x-pn-realvideo";
148 codec_data_size = 34;
149 }
150
151 put_tag(s,"MDPR");
152 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
153 put_be32(s, size);
154 put_be16(s, 0);
155
156 put_be16(s, i); /* stream number */
157 put_be32(s, stream->bit_rate); /* max bit rate */
158 put_be32(s, stream->bit_rate); /* avg bit rate */
159 put_be32(s, stream->packet_max_size); /* max packet size */
160 if (stream->nb_packets > 0)
161 packet_avg_size = stream->packet_total_size /
162 stream->nb_packets;
163 else
164 packet_avg_size = 0;
165 put_be32(s, packet_avg_size); /* avg packet size */
166 put_be32(s, 0); /* start time */
167 put_be32(s, BUFFER_DURATION); /* preroll */
168 /* duration */
5d6ef63f
PG
169 if (url_is_streamed(s) || !stream->total_frames)
170 put_be32(s, (int)(3600 * 1000));
171 else
172 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
de6d9b64
FB
173 put_str8(s, desc);
174 put_str8(s, mimetype);
175 put_be32(s, codec_data_size);
176
177 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
178 int coded_frame_size, fscode, sample_rate;
179 sample_rate = stream->enc->sample_rate;
180 coded_frame_size = (stream->enc->bit_rate *
181 stream->enc->frame_size) / (8 * sample_rate);
182 /* audio codec info */
183 put_tag(s, ".ra");
184 put_byte(s, 0xfd);
185 put_be32(s, 0x00040000); /* version */
186 put_tag(s, ".ra4");
187 put_be32(s, 0x01b53530); /* stream length */
188 put_be16(s, 4); /* unknown */
189 put_be32(s, 0x39); /* header size */
190
191 switch(sample_rate) {
192 case 48000:
193 case 24000:
194 case 12000:
195 fscode = 1;
196 break;
197 default:
198 case 44100:
199 case 22050:
200 case 11025:
201 fscode = 2;
202 break;
203 case 32000:
204 case 16000:
205 case 8000:
206 fscode = 3;
207 }
208 put_be16(s, fscode); /* codec additional info, for AC3, seems
209 to be a frequency code */
210 /* special hack to compensate rounding errors... */
211 if (coded_frame_size == 557)
212 coded_frame_size--;
213 put_be32(s, coded_frame_size); /* frame length */
214 put_be32(s, 0x51540); /* unknown */
215 put_be32(s, 0x249f0); /* unknown */
216 put_be32(s, 0x249f0); /* unknown */
217 put_be16(s, 0x01);
218 /* frame length : seems to be very important */
219 put_be16(s, coded_frame_size);
220 put_be32(s, 0); /* unknown */
221 put_be16(s, stream->enc->sample_rate); /* sample rate */
222 put_be32(s, 0x10); /* unknown */
223 put_be16(s, stream->enc->channels);
224 put_str8(s, "Int0"); /* codec name */
225 put_str8(s, "dnet"); /* codec name */
226 put_be16(s, 0); /* title length */
227 put_be16(s, 0); /* author length */
228 put_be16(s, 0); /* copyright length */
229 put_byte(s, 0); /* end of header */
230 } else {
231 /* video codec info */
232 put_be32(s,34); /* size */
d0271e8a
MN
233 if(stream->enc->codec_id == CODEC_ID_RV10)
234 put_tag(s,"VIDORV10");
235 else
236 put_tag(s,"VIDORV20");
de6d9b64
FB
237 put_be16(s, stream->enc->width);
238 put_be16(s, stream->enc->height);
5d6ef63f 239 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
de6d9b64 240 put_be32(s,0); /* unknown meaning */
5d6ef63f 241 put_be16(s, (int) stream->frame_rate); /* unknown meaning */
de6d9b64
FB
242 put_be32(s,0); /* unknown meaning */
243 put_be16(s, 8); /* unknown meaning */
244 /* Seems to be the codec version: only use basic H263. The next
245 versions seems to add a diffential DC coding as in
246 MPEG... nothing new under the sun */
d0271e8a
MN
247 if(stream->enc->codec_id == CODEC_ID_RV10)
248 put_be32(s,0x10000000);
249 else
250 put_be32(s,0x20103001);
de6d9b64
FB
251 //put_be32(s,0x10003000);
252 }
253 }
254
255 /* patch data offset field */
256 data_pos = s->buf_ptr - start_ptr;
257 rm->data_pos = data_pos;
258 data_offset_ptr[0] = data_pos >> 24;
259 data_offset_ptr[1] = data_pos >> 16;
260 data_offset_ptr[2] = data_pos >> 8;
261 data_offset_ptr[3] = data_pos;
262
263 /* data stream */
264 put_tag(s,"DATA");
265 put_be32(s,data_size + 10 + 8);
266 put_be16(s,0);
267
268 put_be32(s, nb_packets); /* number of packets */
269 put_be32(s,0); /* next data header */
270}
271
272static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
273 int length, int key_frame)
274{
275 int timestamp;
276 ByteIOContext *s = &ctx->pb;
277
278 stream->nb_packets++;
279 stream->packet_total_size += length;
280 if (length > stream->packet_max_size)
281 stream->packet_max_size = length;
282
283 put_be16(s,0); /* version */
284 put_be16(s,length + 12);
285 put_be16(s, stream->num); /* stream number */
286 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
287 put_be32(s, timestamp); /* timestamp */
288 put_byte(s, 0); /* reserved */
289 put_byte(s, key_frame ? 2 : 0); /* flags */
290}
291
292static int rm_write_header(AVFormatContext *s)
293{
c9a65ca8 294 RMContext *rm = s->priv_data;
de6d9b64 295 StreamInfo *stream;
de6d9b64
FB
296 int n;
297 AVCodecContext *codec;
298
de6d9b64
FB
299 for(n=0;n<s->nb_streams;n++) {
300 s->streams[n]->id = n;
01f4895c 301 codec = s->streams[n]->codec;
de6d9b64
FB
302 stream = &rm->streams[n];
303 memset(stream, 0, sizeof(StreamInfo));
304 stream->num = n;
305 stream->bit_rate = codec->bit_rate;
306 stream->enc = codec;
307
308 switch(codec->codec_type) {
309 case CODEC_TYPE_AUDIO:
310 rm->audio_stream = stream;
311 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
312 /* XXX: dummy values */
313 stream->packet_max_size = 1024;
314 stream->nb_packets = 0;
315 stream->total_frames = stream->nb_packets;
316 break;
317 case CODEC_TYPE_VIDEO:
318 rm->video_stream = stream;
c0df9d75 319 stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
de6d9b64
FB
320 /* XXX: dummy values */
321 stream->packet_max_size = 4096;
322 stream->nb_packets = 0;
323 stream->total_frames = stream->nb_packets;
324 break;
5d6ef63f 325 default:
71c32f19 326 return -1;
de6d9b64
FB
327 }
328 }
329
330 rv10_write_header(s, 0, 0);
331 put_flush_packet(&s->pb);
332 return 0;
333}
334
3c895fc0 335static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
de6d9b64 336{
0c1a9eda 337 uint8_t *buf1;
de6d9b64
FB
338 RMContext *rm = s->priv_data;
339 ByteIOContext *pb = &s->pb;
340 StreamInfo *stream = rm->audio_stream;
341 int i;
342
8be1c656 343 /* XXX: suppress this malloc */
0c1a9eda 344 buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
8be1c656 345
3c895fc0 346 write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
de6d9b64
FB
347
348 /* for AC3, the words seems to be reversed */
349 for(i=0;i<size;i+=2) {
350 buf1[i] = buf[i+1];
351 buf1[i+1] = buf[i];
352 }
353 put_buffer(pb, buf1, size);
354 put_flush_packet(pb);
355 stream->nb_frames++;
1ea4f593 356 av_free(buf1);
de6d9b64
FB
357 return 0;
358}
359
3c895fc0 360static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
de6d9b64
FB
361{
362 RMContext *rm = s->priv_data;
363 ByteIOContext *pb = &s->pb;
364 StreamInfo *stream = rm->video_stream;
3c895fc0 365 int key_frame = !!(flags & PKT_FLAG_KEY);
5d6ef63f 366
de6d9b64
FB
367 /* XXX: this is incorrect: should be a parameter */
368
369 /* Well, I spent some time finding the meaning of these bits. I am
370 not sure I understood everything, but it works !! */
371#if 1
372 write_packet_header(s, stream, size + 7, key_frame);
373 /* bit 7: '1' if final packet of a frame converted in several packets */
374 put_byte(pb, 0x81);
375 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
376 frame starting from 1 */
377 if (key_frame) {
378 put_byte(pb, 0x81);
379 } else {
380 put_byte(pb, 0x01);
381 }
80016c48
MN
382 put_be16(pb, 0x4000 + (size)); /* total frame size */
383 put_be16(pb, 0x4000 + (size)); /* offset from the start or the end */
de6d9b64
FB
384#else
385 /* full frame */
386 write_packet_header(s, size + 6);
387 put_byte(pb, 0xc0);
80016c48 388 put_be16(pb, 0x4000 + size); /* total frame size */
de6d9b64
FB
389 put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
390#endif
391 put_byte(pb, stream->nb_frames & 0xff);
392
393 put_buffer(pb, buf, size);
394 put_flush_packet(pb);
395
396 stream->nb_frames++;
397 return 0;
398}
399
e928649b 400static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64 401{
01f4895c 402 if (s->streams[pkt->stream_index]->codec->codec_type ==
de6d9b64 403 CODEC_TYPE_AUDIO)
3c895fc0 404 return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
de6d9b64 405 else
3c895fc0 406 return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
de6d9b64
FB
407}
408
409static int rm_write_trailer(AVFormatContext *s)
410{
411 RMContext *rm = s->priv_data;
412 int data_size, index_pos, i;
413 ByteIOContext *pb = &s->pb;
414
415 if (!url_is_streamed(&s->pb)) {
416 /* end of file: finish to write header */
417 index_pos = url_fseek(pb, 0, SEEK_CUR);
418 data_size = index_pos - rm->data_pos;
419
420 /* index */
421 put_tag(pb, "INDX");
422 put_be32(pb, 10 + 10 * s->nb_streams);
423 put_be16(pb, 0);
424
425 for(i=0;i<s->nb_streams;i++) {
426 put_be32(pb, 0); /* zero indices */
427 put_be16(pb, i); /* stream number */
428 put_be32(pb, 0); /* next index */
429 }
430 /* undocumented end header */
431 put_be32(pb, 0);
432 put_be32(pb, 0);
433
434 url_fseek(pb, 0, SEEK_SET);
435 for(i=0;i<s->nb_streams;i++)
436 rm->streams[i].total_frames = rm->streams[i].nb_frames;
437 rv10_write_header(s, data_size, index_pos);
438 } else {
439 /* undocumented end header */
440 put_be32(pb, 0);
441 put_be32(pb, 0);
442 }
443 put_flush_packet(pb);
de6d9b64
FB
444 return 0;
445}
764ef400 446#endif //CONFIG_ENCODERS
de6d9b64
FB
447
448/***************************************************/
449
450static void get_str(ByteIOContext *pb, char *buf, int buf_size)
451{
452 int len, i;
453 char *q;
454
455 len = get_be16(pb);
456 q = buf;
457 for(i=0;i<len;i++) {
458 if (i < buf_size - 1)
459 *q++ = get_byte(pb);
460 }
461 *q = '\0';
462}
463
464static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
465{
466 int len, i;
467 char *q;
468
469 len = get_byte(pb);
470 q = buf;
471 for(i=0;i<len;i++) {
472 if (i < buf_size - 1)
473 *q++ = get_byte(pb);
474 }
475 *q = '\0';
476}
477
c9510803
FB
478static void rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
479 int read_all)
480{
481 ByteIOContext *pb = &s->pb;
482 char buf[128];
483 uint32_t version;
484 int i;
485
486 /* ra type header */
487 version = get_be32(pb); /* version */
488 if (((version >> 16) & 0xff) == 3) {
489 /* very old version */
490 for(i = 0; i < 14; i++)
491 get_byte(pb);
492 get_str8(pb, s->title, sizeof(s->title));
493 get_str8(pb, s->author, sizeof(s->author));
494 get_str8(pb, s->copyright, sizeof(s->copyright));
495 get_str8(pb, s->comment, sizeof(s->comment));
496 get_byte(pb);
497 get_str8(pb, buf, sizeof(buf));
01f4895c
MN
498 st->codec->sample_rate = 8000;
499 st->codec->channels = 1;
500 st->codec->codec_type = CODEC_TYPE_AUDIO;
501 st->codec->codec_id = CODEC_ID_RA_144;
c9510803 502 } else {
72aef198 503 int flavor, sub_packet_h, coded_framesize;
c9510803
FB
504 /* old version (4) */
505 get_be32(pb); /* .ra4 */
72aef198
MN
506 get_be32(pb); /* data size */
507 get_be16(pb); /* version2 */
c9510803 508 get_be32(pb); /* header size */
72aef198
MN
509 flavor= get_be16(pb); /* add codec info / flavor */
510 coded_framesize= get_be32(pb); /* coded frame size */
c9510803
FB
511 get_be32(pb); /* ??? */
512 get_be32(pb); /* ??? */
513 get_be32(pb); /* ??? */
72aef198 514 sub_packet_h= get_be16(pb); /* 1 */
01f4895c 515 st->codec->block_align= get_be16(pb); /* frame size */
72aef198
MN
516 get_be16(pb); /* sub packet size */
517 get_be16(pb); /* ??? */
01f4895c 518 st->codec->sample_rate = get_be16(pb);
c9510803 519 get_be32(pb);
01f4895c 520 st->codec->channels = get_be16(pb);
c9510803
FB
521 get_str8(pb, buf, sizeof(buf)); /* desc */
522 get_str8(pb, buf, sizeof(buf)); /* desc */
01f4895c 523 st->codec->codec_type = CODEC_TYPE_AUDIO;
c9510803 524 if (!strcmp(buf, "dnet")) {
01f4895c 525 st->codec->codec_id = CODEC_ID_AC3;
72aef198 526 } else if (!strcmp(buf, "28_8")) {
01f4895c
MN
527 st->codec->codec_id = CODEC_ID_RA_288;
528 st->codec->extradata_size= 10;
529 st->codec->extradata= av_mallocz(st->codec->extradata_size);
72aef198
MN
530 /* this is completly braindead and broken, the idiot who added this codec and endianness
531 specific reordering to mplayer and libavcodec/ra288.c should be drowned in a see of cola */
532 //FIXME pass the unpermutated extradata
01f4895c
MN
533 ((uint16_t*)st->codec->extradata)[1]= sub_packet_h;
534 ((uint16_t*)st->codec->extradata)[2]= flavor;
535 ((uint16_t*)st->codec->extradata)[3]= coded_framesize;
c9510803 536 } else {
01f4895c
MN
537 st->codec->codec_id = CODEC_ID_NONE;
538 pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
c9510803
FB
539 buf);
540 }
541 if (read_all) {
542 get_byte(pb);
543 get_byte(pb);
544 get_byte(pb);
545
546 get_str8(pb, s->title, sizeof(s->title));
547 get_str8(pb, s->author, sizeof(s->author));
548 get_str8(pb, s->copyright, sizeof(s->copyright));
549 get_str8(pb, s->comment, sizeof(s->comment));
550 }
551 }
552}
553
554static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
555{
556 RMContext *rm = s->priv_data;
557 AVStream *st;
558
559 rm->old_format = 1;
560 st = av_new_stream(s, 0);
561 if (!st)
562 goto fail;
563 rm_read_audio_stream_info(s, st, 1);
564 return 0;
565 fail:
566 return -1;
567}
568
de6d9b64
FB
569static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
570{
c9a65ca8 571 RMContext *rm = s->priv_data;
de6d9b64
FB
572 AVStream *st;
573 ByteIOContext *pb = &s->pb;
574 unsigned int tag, v;
575 int tag_size, size, codec_data_size, i;
0c1a9eda 576 int64_t codec_pos;
247eadca 577 unsigned int h263_hack_version, start_time, duration;
de6d9b64 578 char buf[128];
5d6ef63f 579 int flags = 0;
de6d9b64 580
c9510803
FB
581 tag = get_le32(pb);
582 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
583 /* very old .ra format */
584 return rm_read_header_old(s, ap);
585 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
0bd586c5 586 return AVERROR_IO;
c9510803 587 }
de6d9b64
FB
588
589 get_be32(pb); /* header size */
590 get_be16(pb);
591 get_be32(pb);
592 get_be32(pb); /* number of headers */
593
594 for(;;) {
595 if (url_feof(pb))
596 goto fail;
597 tag = get_le32(pb);
598 tag_size = get_be32(pb);
599 get_be16(pb);
600#if 0
601 printf("tag=%c%c%c%c (%08x) size=%d\n",
602 (tag) & 0xff,
603 (tag >> 8) & 0xff,
604 (tag >> 16) & 0xff,
605 (tag >> 24) & 0xff,
606 tag,
607 tag_size);
608#endif
9786bd4d 609 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
de6d9b64
FB
610 goto fail;
611 switch(tag) {
612 case MKTAG('P', 'R', 'O', 'P'):
613 /* file header */
614 get_be32(pb); /* max bit rate */
615 get_be32(pb); /* avg bit rate */
616 get_be32(pb); /* max packet size */
617 get_be32(pb); /* avg packet size */
618 get_be32(pb); /* nb packets */
619 get_be32(pb); /* duration */
620 get_be32(pb); /* preroll */
621 get_be32(pb); /* index offset */
622 get_be32(pb); /* data offset */
623 get_be16(pb); /* nb streams */
5d6ef63f 624 flags = get_be16(pb); /* flags */
de6d9b64
FB
625 break;
626 case MKTAG('C', 'O', 'N', 'T'):
627 get_str(pb, s->title, sizeof(s->title));
628 get_str(pb, s->author, sizeof(s->author));
629 get_str(pb, s->copyright, sizeof(s->copyright));
630 get_str(pb, s->comment, sizeof(s->comment));
631 break;
632 case MKTAG('M', 'D', 'P', 'R'):
247eadca 633 st = av_new_stream(s, 0);
de6d9b64
FB
634 if (!st)
635 goto fail;
de6d9b64
FB
636 st->id = get_be16(pb);
637 get_be32(pb); /* max bit rate */
01f4895c 638 st->codec->bit_rate = get_be32(pb); /* bit rate */
de6d9b64
FB
639 get_be32(pb); /* max packet size */
640 get_be32(pb); /* avg packet size */
247eadca 641 start_time = get_be32(pb); /* start time */
de6d9b64 642 get_be32(pb); /* preroll */
247eadca 643 duration = get_be32(pb); /* duration */
c0df9d75
MN
644 st->start_time = start_time;
645 st->duration = duration;
de6d9b64
FB
646 get_str8(pb, buf, sizeof(buf)); /* desc */
647 get_str8(pb, buf, sizeof(buf)); /* mimetype */
648 codec_data_size = get_be32(pb);
649 codec_pos = url_ftell(pb);
01f4895c 650 st->codec->codec_type = CODEC_TYPE_DATA;
d880240e 651 av_set_pts_info(st, 64, 1, 1000);
de6d9b64
FB
652
653 v = get_be32(pb);
654 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
655 /* ra type header */
c9510803 656 rm_read_audio_stream_info(s, st, 0);
de6d9b64 657 } else {
423962dc 658 int fps, fps2;
de6d9b64
FB
659 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
660 fail1:
01f4895c 661 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
f7ff7bc6 662 goto skip;
de6d9b64 663 }
01f4895c
MN
664 st->codec->codec_tag = get_le32(pb);
665// av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
666 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
667 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
668 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
669 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
de6d9b64 670 goto fail1;
01f4895c
MN
671 st->codec->width = get_be16(pb);
672 st->codec->height = get_be16(pb);
673 st->codec->time_base.num= 1;
423962dc 674 fps= get_be16(pb);
01f4895c 675 st->codec->codec_type = CODEC_TYPE_VIDEO;
de6d9b64 676 get_be32(pb);
423962dc 677 fps2= get_be16(pb);
de6d9b64 678 get_be16(pb);
423962dc 679
01f4895c
MN
680 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
681 st->codec->extradata= av_malloc(st->codec->extradata_size);
682 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
423962dc
MN
683
684// av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
01f4895c 685 st->codec->time_base.den = fps * st->codec->time_base.num;
de6d9b64 686 /* modification of h263 codec version (!) */
423962dc 687#ifdef WORDS_BIGENDIAN
01f4895c 688 h263_hack_version = ((uint32_t*)st->codec->extradata)[1];
423962dc 689#else
01f4895c 690 h263_hack_version = bswap_32(((uint32_t*)st->codec->extradata)[1]);
423962dc 691#endif
01f4895c 692 st->codec->sub_id = h263_hack_version;
3f05305d 693 switch((h263_hack_version>>28)){
01f4895c
MN
694 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
695 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
696 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
697 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
3f05305d
MN
698 default: goto fail1;
699 }
de6d9b64 700 }
f7ff7bc6 701skip:
de6d9b64
FB
702 /* skip codec info */
703 size = url_ftell(pb) - codec_pos;
704 url_fskip(pb, codec_data_size - size);
705 break;
706 case MKTAG('D', 'A', 'T', 'A'):
707 goto header_end;
708 default:
709 /* unknown tag: skip it */
710 url_fskip(pb, tag_size - 10);
711 break;
712 }
713 }
714 header_end:
715 rm->nb_packets = get_be32(pb); /* number of packets */
5d6ef63f
PG
716 if (!rm->nb_packets && (flags & 4))
717 rm->nb_packets = 3600 * 25;
de6d9b64
FB
718 get_be32(pb); /* next data header */
719 return 0;
720
721 fail:
722 for(i=0;i<s->nb_streams;i++) {
1ea4f593 723 av_free(s->streams[i]);
de6d9b64 724 }
0bd586c5 725 return AVERROR_IO;
de6d9b64
FB
726}
727
4c2bc159
MN
728static int get_num(ByteIOContext *pb, int *len)
729{
730 int n, n1;
731
732 n = get_be16(pb);
733 (*len)-=2;
734 if (n >= 0x4000) {
735 return n - 0x4000;
736 } else {
737 n1 = get_be16(pb);
738 (*len)-=2;
739 return (n << 16) | n1;
740 }
741}
742
c9510803
FB
743/* multiple of 20 bytes for ra144 (ugly) */
744#define RAW_PACKET_SIZE 1000
745
37e85dac 746static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
bc17df09
MN
747 RMContext *rm = s->priv_data;
748 ByteIOContext *pb = &s->pb;
749 int len, num, res, i;
750 AVStream *st;
119cebf4 751 uint32_t state=0xFFFFFFFF;
bc17df09
MN
752
753 while(!url_feof(pb)){
37e85dac 754 *pos= url_ftell(pb);
bc17df09
MN
755 if(rm->remaining_len > 0){
756 num= rm->current_stream;
757 len= rm->remaining_len;
758 *timestamp = AV_NOPTS_VALUE;
759 *flags= 0;
760 }else{
119cebf4
MN
761 state= (state<<8) + get_byte(pb);
762
763 if(state == MKBETAG('I', 'N', 'D', 'X')){
764 len = get_be16(pb) - 6;
765 if(len<0)
766 continue;
767 goto skip;
768 }
769
770 if(state > (unsigned)0xFFFF || state < 12)
bc17df09 771 continue;
119cebf4
MN
772 len=state;
773 state= 0xFFFFFFFF;
774
bc17df09
MN
775 num = get_be16(pb);
776 *timestamp = get_be32(pb);
777 res= get_byte(pb); /* reserved */
778 *flags = get_byte(pb); /* flags */
37e85dac 779
bc17df09
MN
780
781 len -= 12;
782 }
783 for(i=0;i<s->nb_streams;i++) {
784 st = s->streams[i];
785 if (num == st->id)
786 break;
787 }
788 if (i == s->nb_streams) {
119cebf4 789skip:
bc17df09
MN
790 /* skip packet if unknown number */
791 url_fskip(pb, len);
792 rm->remaining_len -= len;
793 continue;
794 }
795 *stream_index= i;
796
797 return len;
798 }
799 return -1;
800}
801
de6d9b64
FB
802static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
803{
804 RMContext *rm = s->priv_data;
805 ByteIOContext *pb = &s->pb;
806 AVStream *st;
bc17df09 807 int i, len, tmp, j;
37e85dac 808 int64_t timestamp, pos;
0c1a9eda 809 uint8_t *ptr;
bc17df09 810 int flags;
de6d9b64 811
c9510803
FB
812 if (rm->old_format) {
813 /* just read raw bytes */
814 len = RAW_PACKET_SIZE;
2692067a 815 len= av_get_packet(pb, pkt, len);
c9510803 816 pkt->stream_index = 0;
c9510803 817 if (len <= 0) {
0bd586c5 818 return AVERROR_IO;
4c2bc159 819 }
c9510803
FB
820 pkt->size = len;
821 st = s->streams[0];
822 } else {
37e85dac 823 int seq=1;
b9866ebc 824resync:
37e85dac 825 len=sync(s, &timestamp, &flags, &i, &pos);
bc17df09 826 if(len<0)
012cd15f 827 return AVERROR_IO;
bc17df09
MN
828 st = s->streams[i];
829
01f4895c 830 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
a8b81ec1
MN
831 int h, pic_num, len2, pos;
832
833 h= get_byte(pb); len--;
834 if(!(h & 0x40)){
37e85dac 835 seq = get_byte(pb); len--;
a8b81ec1
MN
836 }
837
838 if((h & 0xc0) == 0x40){
839 len2= pos= 0;
840 }else{
841 len2 = get_num(pb, &len);
c9510803 842 pos = get_num(pb, &len);
c9510803
FB
843 }
844 /* picture number */
a8b81ec1
MN
845 pic_num= get_byte(pb); len--;
846 rm->remaining_len= len;
847 rm->current_stream= st->id;
848
849// av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
850 if(len2 && len2<len)
851 len=len2;
852 rm->remaining_len-= len;
c9510803 853 }
b9866ebc 854
f3356e9c
MN
855 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
856 || st->discard >= AVDISCARD_ALL){
b9866ebc
MN
857 url_fskip(pb, len);
858 goto resync;
859 }
c9510803 860
2692067a 861 av_get_packet(pb, pkt, len);
c9510803 862 pkt->stream_index = i;
d880240e
MN
863
864#if 0
01f4895c
MN
865 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
866 if(st->codec->codec_id == CODEC_ID_RV20){
d880240e
MN
867 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
868 av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
869
870 seq |= (timestamp&~0x3FFF);
871 if(seq - timestamp > 0x2000) seq -= 0x4000;
872 if(seq - timestamp < -0x2000) seq += 0x4000;
873 }
874 }
875#endif
876 pkt->pts= timestamp;
37e85dac 877 if(flags&2){
d880240e 878 pkt->flags |= PKT_FLAG_KEY;
37e85dac
MN
879 if((seq&0x7F) == 1)
880 av_add_index_entry(st, pos, timestamp, 0, AVINDEX_KEYFRAME);
881 }
5d6ef63f
PG
882 }
883
de6d9b64 884 /* for AC3, needs to swap bytes */
01f4895c 885 if (st->codec->codec_id == CODEC_ID_AC3) {
de6d9b64
FB
886 ptr = pkt->data;
887 for(j=0;j<len;j+=2) {
888 tmp = ptr[0];
889 ptr[0] = ptr[1];
890 ptr[1] = tmp;
891 ptr += 2;
892 }
893 }
894 return 0;
895}
896
897static int rm_read_close(AVFormatContext *s)
898{
de6d9b64
FB
899 return 0;
900}
901
c9a65ca8
FB
902static int rm_probe(AVProbeData *p)
903{
904 /* check file header */
905 if (p->buf_size <= 32)
906 return 0;
c9510803
FB
907 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
908 p->buf[2] == 'M' && p->buf[3] == 'F' &&
909 p->buf[4] == 0 && p->buf[5] == 0) ||
910 (p->buf[0] == '.' && p->buf[1] == 'r' &&
911 p->buf[2] == 'a' && p->buf[3] == 0xfd))
c9a65ca8
FB
912 return AVPROBE_SCORE_MAX;
913 else
914 return 0;
915}
916
bc17df09
MN
917static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
918 int64_t *ppos, int64_t pos_limit)
919{
920 RMContext *rm = s->priv_data;
921 int64_t pos, dts;
37e85dac 922 int stream_index2, flags, len, h;
bc17df09
MN
923
924 pos = *ppos;
925
926 if(rm->old_format)
927 return AV_NOPTS_VALUE;
928
929 url_fseek(&s->pb, pos, SEEK_SET);
930 rm->remaining_len=0;
931 for(;;){
37e85dac
MN
932 int seq=1;
933 AVStream *st;
934
935 len=sync(s, &dts, &flags, &stream_index2, &pos);
bc17df09
MN
936 if(len<0)
937 return AV_NOPTS_VALUE;
37e85dac
MN
938
939 st = s->streams[stream_index2];
01f4895c 940 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
37e85dac
MN
941 h= get_byte(&s->pb); len--;
942 if(!(h & 0x40)){
943 seq = get_byte(&s->pb); len--;
bc17df09
MN
944 }
945 }
37e85dac
MN
946
947 if((flags&2) && (seq&0x7F) == 1){
948// av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
949 av_add_index_entry(st, pos, dts, 0, AVINDEX_KEYFRAME);
950 if(stream_index2 == stream_index)
951 break;
952 }
953
bc17df09
MN
954 url_fskip(&s->pb, len);
955 }
956 *ppos = pos;
957 return dts;
958}
959
c18a2692 960static AVInputFormat rm_iformat = {
c9a65ca8
FB
961 "rm",
962 "rm format",
963 sizeof(RMContext),
964 rm_probe,
965 rm_read_header,
966 rm_read_packet,
967 rm_read_close,
bc17df09
MN
968 NULL,
969 rm_read_dts,
c9a65ca8
FB
970};
971
764ef400 972#ifdef CONFIG_ENCODERS
c18a2692 973static AVOutputFormat rm_oformat = {
de6d9b64
FB
974 "rm",
975 "rm format",
c6c11cb6 976 "application/vnd.rn-realmedia",
de6d9b64 977 "rm,ra",
c9a65ca8 978 sizeof(RMContext),
de6d9b64
FB
979 CODEC_ID_AC3,
980 CODEC_ID_RV10,
981 rm_write_header,
982 rm_write_packet,
983 rm_write_trailer,
de6d9b64 984};
764ef400 985#endif //CONFIG_ENCODERS
c9a65ca8
FB
986
987int rm_init(void)
988{
989 av_register_input_format(&rm_iformat);
764ef400 990#ifdef CONFIG_ENCODERS
c9a65ca8 991 av_register_output_format(&rm_oformat);
764ef400 992#endif //CONFIG_ENCODERS
c9a65ca8
FB
993 return 0;
994}