fixing rv10, this isnt the cleanest solution (parsing the packet header in the codec...
[libav.git] / libav / 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;
42} RMContext;
43
44static void put_str(ByteIOContext *s, const char *tag)
45{
46 put_be16(s,strlen(tag));
47 while (*tag) {
48 put_byte(s, *tag++);
49 }
50}
51
52static void put_str8(ByteIOContext *s, const char *tag)
53{
54 put_byte(s, strlen(tag));
55 while (*tag) {
56 put_byte(s, *tag++);
57 }
58}
59
60static void rv10_write_header(AVFormatContext *ctx,
61 int data_size, int index_pos)
62{
63 RMContext *rm = ctx->priv_data;
64 ByteIOContext *s = &ctx->pb;
65 StreamInfo *stream;
66 unsigned char *data_offset_ptr, *start_ptr;
67 const char *desc, *mimetype;
68 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
69 int bit_rate, v, duration, flags, data_pos;
70
71 start_ptr = s->buf_ptr;
72
73 put_tag(s, ".RMF");
74 put_be32(s,18); /* header size */
75 put_be16(s,0);
76 put_be32(s,0);
77 put_be32(s,4 + ctx->nb_streams); /* num headers */
78
79 put_tag(s,"PROP");
80 put_be32(s, 50);
81 put_be16(s, 0);
82 packet_max_size = 0;
83 packet_total_size = 0;
84 nb_packets = 0;
85 bit_rate = 0;
86 duration = 0;
87 for(i=0;i<ctx->nb_streams;i++) {
88 StreamInfo *stream = &rm->streams[i];
89 bit_rate += stream->bit_rate;
90 if (stream->packet_max_size > packet_max_size)
91 packet_max_size = stream->packet_max_size;
92 nb_packets += stream->nb_packets;
93 packet_total_size += stream->packet_total_size;
94 /* select maximum duration */
95 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
96 if (v > duration)
97 duration = v;
98 }
99 put_be32(s, bit_rate); /* max bit rate */
100 put_be32(s, bit_rate); /* avg bit rate */
101 put_be32(s, packet_max_size); /* max packet size */
102 if (nb_packets > 0)
103 packet_avg_size = packet_total_size / nb_packets;
104 else
105 packet_avg_size = 0;
106 put_be32(s, packet_avg_size); /* avg packet size */
107 put_be32(s, nb_packets); /* num packets */
108 put_be32(s, duration); /* duration */
109 put_be32(s, BUFFER_DURATION); /* preroll */
110 put_be32(s, index_pos); /* index offset */
111 /* computation of data the data offset */
112 data_offset_ptr = s->buf_ptr;
113 put_be32(s, 0); /* data offset : will be patched after */
114 put_be16(s, ctx->nb_streams); /* num streams */
115 flags = 1 | 2; /* save allowed & perfect play */
116 if (url_is_streamed(s))
117 flags |= 4; /* live broadcast */
118 put_be16(s, flags);
119
120 /* comments */
121
122 put_tag(s,"CONT");
123 size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
124 strlen(ctx->comment) + 4 * 2 + 10;
125 put_be32(s,size);
126 put_be16(s,0);
127 put_str(s, ctx->title);
128 put_str(s, ctx->author);
129 put_str(s, ctx->copyright);
130 put_str(s, ctx->comment);
131
132 for(i=0;i<ctx->nb_streams;i++) {
133 int codec_data_size;
134
135 stream = &rm->streams[i];
136
137 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
138 desc = "The Audio Stream";
139 mimetype = "audio/x-pn-realaudio";
140 codec_data_size = 73;
141 } else {
142 desc = "The Video Stream";
143 mimetype = "video/x-pn-realvideo";
144 codec_data_size = 34;
145 }
146
147 put_tag(s,"MDPR");
148 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
149 put_be32(s, size);
150 put_be16(s, 0);
151
152 put_be16(s, i); /* stream number */
153 put_be32(s, stream->bit_rate); /* max bit rate */
154 put_be32(s, stream->bit_rate); /* avg bit rate */
155 put_be32(s, stream->packet_max_size); /* max packet size */
156 if (stream->nb_packets > 0)
157 packet_avg_size = stream->packet_total_size /
158 stream->nb_packets;
159 else
160 packet_avg_size = 0;
161 put_be32(s, packet_avg_size); /* avg packet size */
162 put_be32(s, 0); /* start time */
163 put_be32(s, BUFFER_DURATION); /* preroll */
164 /* duration */
5d6ef63f
PG
165 if (url_is_streamed(s) || !stream->total_frames)
166 put_be32(s, (int)(3600 * 1000));
167 else
168 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
de6d9b64
FB
169 put_str8(s, desc);
170 put_str8(s, mimetype);
171 put_be32(s, codec_data_size);
172
173 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
174 int coded_frame_size, fscode, sample_rate;
175 sample_rate = stream->enc->sample_rate;
176 coded_frame_size = (stream->enc->bit_rate *
177 stream->enc->frame_size) / (8 * sample_rate);
178 /* audio codec info */
179 put_tag(s, ".ra");
180 put_byte(s, 0xfd);
181 put_be32(s, 0x00040000); /* version */
182 put_tag(s, ".ra4");
183 put_be32(s, 0x01b53530); /* stream length */
184 put_be16(s, 4); /* unknown */
185 put_be32(s, 0x39); /* header size */
186
187 switch(sample_rate) {
188 case 48000:
189 case 24000:
190 case 12000:
191 fscode = 1;
192 break;
193 default:
194 case 44100:
195 case 22050:
196 case 11025:
197 fscode = 2;
198 break;
199 case 32000:
200 case 16000:
201 case 8000:
202 fscode = 3;
203 }
204 put_be16(s, fscode); /* codec additional info, for AC3, seems
205 to be a frequency code */
206 /* special hack to compensate rounding errors... */
207 if (coded_frame_size == 557)
208 coded_frame_size--;
209 put_be32(s, coded_frame_size); /* frame length */
210 put_be32(s, 0x51540); /* unknown */
211 put_be32(s, 0x249f0); /* unknown */
212 put_be32(s, 0x249f0); /* unknown */
213 put_be16(s, 0x01);
214 /* frame length : seems to be very important */
215 put_be16(s, coded_frame_size);
216 put_be32(s, 0); /* unknown */
217 put_be16(s, stream->enc->sample_rate); /* sample rate */
218 put_be32(s, 0x10); /* unknown */
219 put_be16(s, stream->enc->channels);
220 put_str8(s, "Int0"); /* codec name */
221 put_str8(s, "dnet"); /* codec name */
222 put_be16(s, 0); /* title length */
223 put_be16(s, 0); /* author length */
224 put_be16(s, 0); /* copyright length */
225 put_byte(s, 0); /* end of header */
226 } else {
227 /* video codec info */
228 put_be32(s,34); /* size */
229 put_tag(s,"VIDORV10");
230 put_be16(s, stream->enc->width);
231 put_be16(s, stream->enc->height);
5d6ef63f 232 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
de6d9b64 233 put_be32(s,0); /* unknown meaning */
5d6ef63f 234 put_be16(s, (int) stream->frame_rate); /* unknown meaning */
de6d9b64
FB
235 put_be32(s,0); /* unknown meaning */
236 put_be16(s, 8); /* unknown meaning */
237 /* Seems to be the codec version: only use basic H263. The next
238 versions seems to add a diffential DC coding as in
239 MPEG... nothing new under the sun */
240 put_be32(s,0x10000000);
241 //put_be32(s,0x10003000);
242 }
243 }
244
245 /* patch data offset field */
246 data_pos = s->buf_ptr - start_ptr;
247 rm->data_pos = data_pos;
248 data_offset_ptr[0] = data_pos >> 24;
249 data_offset_ptr[1] = data_pos >> 16;
250 data_offset_ptr[2] = data_pos >> 8;
251 data_offset_ptr[3] = data_pos;
252
253 /* data stream */
254 put_tag(s,"DATA");
255 put_be32(s,data_size + 10 + 8);
256 put_be16(s,0);
257
258 put_be32(s, nb_packets); /* number of packets */
259 put_be32(s,0); /* next data header */
260}
261
262static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
263 int length, int key_frame)
264{
265 int timestamp;
266 ByteIOContext *s = &ctx->pb;
267
268 stream->nb_packets++;
269 stream->packet_total_size += length;
270 if (length > stream->packet_max_size)
271 stream->packet_max_size = length;
272
273 put_be16(s,0); /* version */
274 put_be16(s,length + 12);
275 put_be16(s, stream->num); /* stream number */
276 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
277 put_be32(s, timestamp); /* timestamp */
278 put_byte(s, 0); /* reserved */
279 put_byte(s, key_frame ? 2 : 0); /* flags */
280}
281
282static int rm_write_header(AVFormatContext *s)
283{
c9a65ca8 284 RMContext *rm = s->priv_data;
de6d9b64 285 StreamInfo *stream;
de6d9b64
FB
286 int n;
287 AVCodecContext *codec;
288
de6d9b64
FB
289 for(n=0;n<s->nb_streams;n++) {
290 s->streams[n]->id = n;
291 codec = &s->streams[n]->codec;
292 stream = &rm->streams[n];
293 memset(stream, 0, sizeof(StreamInfo));
294 stream->num = n;
295 stream->bit_rate = codec->bit_rate;
296 stream->enc = codec;
297
298 switch(codec->codec_type) {
299 case CODEC_TYPE_AUDIO:
300 rm->audio_stream = stream;
301 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
302 /* XXX: dummy values */
303 stream->packet_max_size = 1024;
304 stream->nb_packets = 0;
305 stream->total_frames = stream->nb_packets;
306 break;
307 case CODEC_TYPE_VIDEO:
308 rm->video_stream = stream;
309 stream->frame_rate = (float)codec->frame_rate / (float)FRAME_RATE_BASE;
310 /* XXX: dummy values */
311 stream->packet_max_size = 4096;
312 stream->nb_packets = 0;
313 stream->total_frames = stream->nb_packets;
314 break;
5d6ef63f 315 default:
42343f7e 316 av_abort();
de6d9b64
FB
317 }
318 }
319
320 rv10_write_header(s, 0, 0);
321 put_flush_packet(&s->pb);
322 return 0;
323}
324
325static int rm_write_audio(AVFormatContext *s, UINT8 *buf, int size)
326{
8be1c656 327 UINT8 *buf1;
de6d9b64
FB
328 RMContext *rm = s->priv_data;
329 ByteIOContext *pb = &s->pb;
330 StreamInfo *stream = rm->audio_stream;
331 int i;
332
8be1c656 333 /* XXX: suppress this malloc */
1ea4f593 334 buf1= (UINT8*) av_malloc( size * sizeof(UINT8) );
8be1c656 335
de6d9b64
FB
336 write_packet_header(s, stream, size, stream->enc->key_frame);
337
338 /* for AC3, the words seems to be reversed */
339 for(i=0;i<size;i+=2) {
340 buf1[i] = buf[i+1];
341 buf1[i+1] = buf[i];
342 }
343 put_buffer(pb, buf1, size);
344 put_flush_packet(pb);
345 stream->nb_frames++;
1ea4f593 346 av_free(buf1);
de6d9b64
FB
347 return 0;
348}
349
350static int rm_write_video(AVFormatContext *s, UINT8 *buf, int size)
351{
352 RMContext *rm = s->priv_data;
353 ByteIOContext *pb = &s->pb;
354 StreamInfo *stream = rm->video_stream;
355 int key_frame = stream->enc->key_frame;
5d6ef63f 356
de6d9b64
FB
357 /* XXX: this is incorrect: should be a parameter */
358
359 /* Well, I spent some time finding the meaning of these bits. I am
360 not sure I understood everything, but it works !! */
361#if 1
362 write_packet_header(s, stream, size + 7, key_frame);
363 /* bit 7: '1' if final packet of a frame converted in several packets */
364 put_byte(pb, 0x81);
365 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
366 frame starting from 1 */
367 if (key_frame) {
368 put_byte(pb, 0x81);
369 } else {
370 put_byte(pb, 0x01);
371 }
372 put_be16(pb, 0x4000 | (size)); /* total frame size */
373 put_be16(pb, 0x4000 | (size)); /* offset from the start or the end */
374#else
375 /* full frame */
376 write_packet_header(s, size + 6);
377 put_byte(pb, 0xc0);
378 put_be16(pb, 0x4000 | size); /* total frame size */
379 put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
380#endif
381 put_byte(pb, stream->nb_frames & 0xff);
382
383 put_buffer(pb, buf, size);
384 put_flush_packet(pb);
385
386 stream->nb_frames++;
387 return 0;
388}
389
390static int rm_write_packet(AVFormatContext *s, int stream_index,
10bb7023 391 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
392{
393 if (s->streams[stream_index]->codec.codec_type ==
394 CODEC_TYPE_AUDIO)
395 return rm_write_audio(s, buf, size);
396 else
397 return rm_write_video(s, buf, size);
398}
399
400static int rm_write_trailer(AVFormatContext *s)
401{
402 RMContext *rm = s->priv_data;
403 int data_size, index_pos, i;
404 ByteIOContext *pb = &s->pb;
405
406 if (!url_is_streamed(&s->pb)) {
407 /* end of file: finish to write header */
408 index_pos = url_fseek(pb, 0, SEEK_CUR);
409 data_size = index_pos - rm->data_pos;
410
411 /* index */
412 put_tag(pb, "INDX");
413 put_be32(pb, 10 + 10 * s->nb_streams);
414 put_be16(pb, 0);
415
416 for(i=0;i<s->nb_streams;i++) {
417 put_be32(pb, 0); /* zero indices */
418 put_be16(pb, i); /* stream number */
419 put_be32(pb, 0); /* next index */
420 }
421 /* undocumented end header */
422 put_be32(pb, 0);
423 put_be32(pb, 0);
424
425 url_fseek(pb, 0, SEEK_SET);
426 for(i=0;i<s->nb_streams;i++)
427 rm->streams[i].total_frames = rm->streams[i].nb_frames;
428 rv10_write_header(s, data_size, index_pos);
429 } else {
430 /* undocumented end header */
431 put_be32(pb, 0);
432 put_be32(pb, 0);
433 }
434 put_flush_packet(pb);
de6d9b64
FB
435 return 0;
436}
437
438/***************************************************/
439
440static void get_str(ByteIOContext *pb, char *buf, int buf_size)
441{
442 int len, i;
443 char *q;
444
445 len = get_be16(pb);
446 q = buf;
447 for(i=0;i<len;i++) {
448 if (i < buf_size - 1)
449 *q++ = get_byte(pb);
450 }
451 *q = '\0';
452}
453
454static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
455{
456 int len, i;
457 char *q;
458
459 len = get_byte(pb);
460 q = buf;
461 for(i=0;i<len;i++) {
462 if (i < buf_size - 1)
463 *q++ = get_byte(pb);
464 }
465 *q = '\0';
466}
467
468static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
469{
c9a65ca8 470 RMContext *rm = s->priv_data;
de6d9b64
FB
471 AVStream *st;
472 ByteIOContext *pb = &s->pb;
473 unsigned int tag, v;
474 int tag_size, size, codec_data_size, i;
475 INT64 codec_pos;
476 unsigned int h263_hack_version;
477 char buf[128];
5d6ef63f 478 int flags = 0;
de6d9b64
FB
479
480 if (get_le32(pb) != MKTAG('.', 'R', 'M', 'F'))
481 return -EIO;
de6d9b64
FB
482
483 get_be32(pb); /* header size */
484 get_be16(pb);
485 get_be32(pb);
486 get_be32(pb); /* number of headers */
487
488 for(;;) {
489 if (url_feof(pb))
490 goto fail;
491 tag = get_le32(pb);
492 tag_size = get_be32(pb);
493 get_be16(pb);
494#if 0
495 printf("tag=%c%c%c%c (%08x) size=%d\n",
496 (tag) & 0xff,
497 (tag >> 8) & 0xff,
498 (tag >> 16) & 0xff,
499 (tag >> 24) & 0xff,
500 tag,
501 tag_size);
502#endif
503 if (tag_size < 10)
504 goto fail;
505 switch(tag) {
506 case MKTAG('P', 'R', 'O', 'P'):
507 /* file header */
508 get_be32(pb); /* max bit rate */
509 get_be32(pb); /* avg bit rate */
510 get_be32(pb); /* max packet size */
511 get_be32(pb); /* avg packet size */
512 get_be32(pb); /* nb packets */
513 get_be32(pb); /* duration */
514 get_be32(pb); /* preroll */
515 get_be32(pb); /* index offset */
516 get_be32(pb); /* data offset */
517 get_be16(pb); /* nb streams */
5d6ef63f 518 flags = get_be16(pb); /* flags */
de6d9b64
FB
519 break;
520 case MKTAG('C', 'O', 'N', 'T'):
521 get_str(pb, s->title, sizeof(s->title));
522 get_str(pb, s->author, sizeof(s->author));
523 get_str(pb, s->copyright, sizeof(s->copyright));
524 get_str(pb, s->comment, sizeof(s->comment));
525 break;
526 case MKTAG('M', 'D', 'P', 'R'):
527 st = av_mallocz(sizeof(AVStream));
528 if (!st)
529 goto fail;
530 s->streams[s->nb_streams++] = st;
531 st->id = get_be16(pb);
532 get_be32(pb); /* max bit rate */
533 st->codec.bit_rate = get_be32(pb); /* bit rate */
534 get_be32(pb); /* max packet size */
535 get_be32(pb); /* avg packet size */
536 get_be32(pb); /* start time */
537 get_be32(pb); /* preroll */
538 get_be32(pb); /* duration */
539 get_str8(pb, buf, sizeof(buf)); /* desc */
540 get_str8(pb, buf, sizeof(buf)); /* mimetype */
541 codec_data_size = get_be32(pb);
542 codec_pos = url_ftell(pb);
543
544 v = get_be32(pb);
545 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
546 /* ra type header */
547 get_be32(pb); /* version */
548 get_be32(pb); /* .ra4 */
549 get_be32(pb);
550 get_be16(pb);
551 get_be32(pb); /* header size */
552 get_be16(pb); /* add codec info */
553 get_be32(pb); /* coded frame size */
554 get_be32(pb); /* ??? */
555 get_be32(pb); /* ??? */
556 get_be32(pb); /* ??? */
557 get_be16(pb); /* 1 */
558 get_be16(pb); /* coded frame size */
559 get_be32(pb);
560 st->codec.sample_rate = get_be16(pb);
561 get_be32(pb);
562 st->codec.channels = get_be16(pb);
563 get_str8(pb, buf, sizeof(buf)); /* desc */
564 get_str8(pb, buf, sizeof(buf)); /* desc */
565 st->codec.codec_type = CODEC_TYPE_AUDIO;
566 if (!strcmp(buf, "dnet")) {
567 st->codec.codec_id = CODEC_ID_AC3;
568 } else {
569 st->codec.codec_id = CODEC_ID_NONE;
c9a65ca8 570 pstrcpy(st->codec.codec_name, sizeof(st->codec.codec_name),
de6d9b64
FB
571 buf);
572 }
573 } else {
574 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
575 fail1:
576 fprintf(stderr, "Unsupported video codec\n");
577 goto fail;
578 }
579 st->codec.codec_tag = get_le32(pb);
580 if (st->codec.codec_tag != MKTAG('R', 'V', '1', '0'))
581 goto fail1;
582 st->codec.width = get_be16(pb);
583 st->codec.height = get_be16(pb);
584 st->codec.frame_rate = get_be16(pb) * FRAME_RATE_BASE;
585 st->codec.codec_type = CODEC_TYPE_VIDEO;
586 get_be32(pb);
587 get_be16(pb);
588 get_be32(pb);
589 get_be16(pb);
590 /* modification of h263 codec version (!) */
591 h263_hack_version = get_be32(pb);
592 switch(h263_hack_version) {
593 case 0x10000000:
594 st->codec.sub_id = 0;
595 st->codec.codec_id = CODEC_ID_RV10;
596 break;
597 case 0x10003000:
598 case 0x10003001:
599 st->codec.sub_id = 3;
600 st->codec.codec_id = CODEC_ID_RV10;
601 break;
602 default:
603 /* not handled */
604 st->codec.codec_id = CODEC_ID_NONE;
605 break;
606 }
607 }
608 /* skip codec info */
609 size = url_ftell(pb) - codec_pos;
610 url_fskip(pb, codec_data_size - size);
611 break;
612 case MKTAG('D', 'A', 'T', 'A'):
613 goto header_end;
614 default:
615 /* unknown tag: skip it */
616 url_fskip(pb, tag_size - 10);
617 break;
618 }
619 }
620 header_end:
621 rm->nb_packets = get_be32(pb); /* number of packets */
5d6ef63f
PG
622 if (!rm->nb_packets && (flags & 4))
623 rm->nb_packets = 3600 * 25;
de6d9b64
FB
624 get_be32(pb); /* next data header */
625 return 0;
626
627 fail:
628 for(i=0;i<s->nb_streams;i++) {
1ea4f593 629 av_free(s->streams[i]);
de6d9b64
FB
630 }
631 return -EIO;
632}
633
4c2bc159
MN
634static int get_num(ByteIOContext *pb, int *len)
635{
636 int n, n1;
637
638 n = get_be16(pb);
639 (*len)-=2;
640 if (n >= 0x4000) {
641 return n - 0x4000;
642 } else {
643 n1 = get_be16(pb);
644 (*len)-=2;
645 return (n << 16) | n1;
646 }
647}
648
de6d9b64
FB
649static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
650{
651 RMContext *rm = s->priv_data;
652 ByteIOContext *pb = &s->pb;
653 AVStream *st;
654 int len, num, timestamp, i, tmp, j;
655 UINT8 *ptr;
5d6ef63f 656 int flags;
de6d9b64
FB
657
658 redo:
659 if (rm->nb_packets == 0)
660 return -EIO;
661 get_be16(pb);
662 len = get_be16(pb);
663 if (len < 12)
664 return -EIO;
665 num = get_be16(pb);
666 timestamp = get_be32(pb);
667 get_byte(pb); /* reserved */
5d6ef63f 668 flags = get_byte(pb); /* flags */
de6d9b64
FB
669 rm->nb_packets--;
670 len -= 12;
671
672 st = NULL;
673 for(i=0;i<s->nb_streams;i++) {
674 st = s->streams[i];
675 if (num == st->id)
676 break;
677 }
678 if (i == s->nb_streams) {
679 /* skip packet if unknown number */
680 url_fskip(pb, len);
681 goto redo;
682 }
5d6ef63f 683
4c2bc159 684#if 0 // XXX/FIXME this is done in the codec currently, but should be done here ...
5d6ef63f 685 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
4c2bc159
MN
686 int full_frame, h, pic_num;
687
688 h= get_byte(pb);
689 if ((h & 0xc0) == 0xc0) {
690 int len2, pos;
691 full_frame = 1;
692 len2= get_num(pb, &len);
693 pos = get_num(pb, &len);
694 //printf("pos:%d\n",len);
695 len -= 2;
696 } else {
697 int seq, frame_size, pos;
698 full_frame = 0;
699 seq = get_byte(pb);
700 frame_size = get_num(pb, &len);
701 pos = get_num(pb, &len);
702 //printf("seq:%d, size:%d, pos:%d\n",seq,frame_size,pos);
703 len -= 3;
704 }
705 /* picture number */
706 pic_num= get_byte(pb);
707
708 av_new_packet(pkt, len+1);
709 pkt->stream_index = i;
710
711 //XXX/FIXME: is this a good idea?
712 pkt->data[0]= h; //store header, its needed for decoding
713
714 get_buffer(pb, pkt->data+1, len);
5d6ef63f 715 }
4c2bc159 716#endif
5d6ef63f 717
de6d9b64
FB
718 av_new_packet(pkt, len);
719 pkt->stream_index = i;
720 get_buffer(pb, pkt->data, len);
4c2bc159 721
de6d9b64
FB
722 /* for AC3, needs to swap bytes */
723 if (st->codec.codec_id == CODEC_ID_AC3) {
724 ptr = pkt->data;
725 for(j=0;j<len;j+=2) {
726 tmp = ptr[0];
727 ptr[0] = ptr[1];
728 ptr[1] = tmp;
729 ptr += 2;
730 }
731 }
732 return 0;
733}
734
735static int rm_read_close(AVFormatContext *s)
736{
de6d9b64
FB
737 return 0;
738}
739
c9a65ca8
FB
740static int rm_probe(AVProbeData *p)
741{
742 /* check file header */
743 if (p->buf_size <= 32)
744 return 0;
745 if (p->buf[0] == '.' && p->buf[1] == 'R' &&
746 p->buf[2] == 'M' && p->buf[3] == 'F' &&
747 p->buf[4] == 0 && p->buf[5] == 0)
748 return AVPROBE_SCORE_MAX;
749 else
750 return 0;
751}
752
753AVInputFormat rm_iformat = {
754 "rm",
755 "rm format",
756 sizeof(RMContext),
757 rm_probe,
758 rm_read_header,
759 rm_read_packet,
760 rm_read_close,
761};
762
763AVOutputFormat rm_oformat = {
de6d9b64
FB
764 "rm",
765 "rm format",
766 "audio/x-pn-realaudio",
767 "rm,ra",
c9a65ca8 768 sizeof(RMContext),
de6d9b64
FB
769 CODEC_ID_AC3,
770 CODEC_ID_RV10,
771 rm_write_header,
772 rm_write_packet,
773 rm_write_trailer,
de6d9b64 774};
c9a65ca8
FB
775
776int rm_init(void)
777{
778 av_register_input_format(&rm_iformat);
779 av_register_output_format(&rm_oformat);
780 return 0;
781}