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