VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavformat/rm.c@ 11537

Last change on this file since 11537 was 5776, checked in by vboxsync, 17 years ago

ffmpeg: exported to OSE

File size: 38.1 KB
Line 
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
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 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
57static 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
65static 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
73static 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
281static 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
301static 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
344static 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
369static 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
409static 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
418static 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
459static 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
473static 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
487static 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
628static 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
640static 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 h263_hack_version, 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 /* modification of h263 codec version (!) */
765#ifdef WORDS_BIGENDIAN
766 h263_hack_version = ((uint32_t*)st->codec->extradata)[1];
767#else
768 h263_hack_version = bswap_32(((uint32_t*)st->codec->extradata)[1]);
769#endif
770 st->codec->sub_id = h263_hack_version;
771 switch((h263_hack_version>>28)){
772 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
773 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
774 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
775 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
776 default: goto fail1;
777 }
778 }
779skip:
780 /* skip codec info */
781 size = url_ftell(pb) - codec_pos;
782 url_fskip(pb, codec_data_size - size);
783 break;
784 case MKTAG('D', 'A', 'T', 'A'):
785 goto header_end;
786 default:
787 /* unknown tag: skip it */
788 url_fskip(pb, tag_size - 10);
789 break;
790 }
791 }
792 header_end:
793 rm->nb_packets = get_be32(pb); /* number of packets */
794 if (!rm->nb_packets && (flags & 4))
795 rm->nb_packets = 3600 * 25;
796 get_be32(pb); /* next data header */
797 return 0;
798
799 fail:
800 for(i=0;i<s->nb_streams;i++) {
801 av_free(s->streams[i]);
802 }
803 return AVERROR_IO;
804}
805
806static int get_num(ByteIOContext *pb, int *len)
807{
808 int n, n1;
809
810 n = get_be16(pb);
811 (*len)-=2;
812 if (n >= 0x4000) {
813 return n - 0x4000;
814 } else {
815 n1 = get_be16(pb);
816 (*len)-=2;
817 return (n << 16) | n1;
818 }
819}
820
821/* multiple of 20 bytes for ra144 (ugly) */
822#define RAW_PACKET_SIZE 1000
823
824static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
825 RMContext *rm = s->priv_data;
826 ByteIOContext *pb = &s->pb;
827 int len, num, res, i;
828 AVStream *st;
829 uint32_t state=0xFFFFFFFF;
830
831 while(!url_feof(pb)){
832 *pos= url_ftell(pb);
833 if(rm->remaining_len > 0){
834 num= rm->current_stream;
835 len= rm->remaining_len;
836 *timestamp = AV_NOPTS_VALUE;
837 *flags= 0;
838 }else{
839 state= (state<<8) + get_byte(pb);
840
841 if(state == MKBETAG('I', 'N', 'D', 'X')){
842 len = get_be16(pb) - 6;
843 if(len<0)
844 continue;
845 goto skip;
846 }
847
848 if(state > (unsigned)0xFFFF || state < 12)
849 continue;
850 len=state;
851 state= 0xFFFFFFFF;
852
853 num = get_be16(pb);
854 *timestamp = get_be32(pb);
855 res= get_byte(pb); /* reserved */
856 *flags = get_byte(pb); /* flags */
857
858
859 len -= 12;
860 }
861 for(i=0;i<s->nb_streams;i++) {
862 st = s->streams[i];
863 if (num == st->id)
864 break;
865 }
866 if (i == s->nb_streams) {
867skip:
868 /* skip packet if unknown number */
869 url_fskip(pb, len);
870 rm->remaining_len -= len;
871 continue;
872 }
873 *stream_index= i;
874
875 return len;
876 }
877 return -1;
878}
879
880static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
881{
882 RMContext *rm = s->priv_data;
883 ByteIOContext *pb = &s->pb;
884 AVStream *st;
885 int i, len, tmp, j;
886 int64_t timestamp, pos;
887 uint8_t *ptr;
888 int flags;
889
890 if (rm->audio_pkt_cnt) {
891 // If there are queued audio packet return them first
892 st = s->streams[rm->audio_stream_num];
893 if (st->codec->codec_id == CODEC_ID_AAC)
894 av_get_packet(pb, pkt, rm->sub_packet_lengths[rm->sub_packet_cnt - rm->audio_pkt_cnt]);
895 else {
896 av_new_packet(pkt, st->codec->block_align);
897 memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
898 (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
899 st->codec->block_align);
900 }
901 rm->audio_pkt_cnt--;
902 pkt->flags = 0;
903 pkt->stream_index = rm->audio_stream_num;
904 } else if (rm->old_format) {
905 st = s->streams[0];
906 if (st->codec->codec_id == CODEC_ID_RA_288) {
907 int x, y;
908
909 for (y = 0; y < rm->sub_packet_h; y++)
910 for (x = 0; x < rm->sub_packet_h/2; x++)
911 if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
912 return AVERROR_IO;
913 rm->audio_stream_num = 0;
914 rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
915 // Release first audio packet
916 av_new_packet(pkt, st->codec->block_align);
917 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
918 pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
919 pkt->stream_index = 0;
920 } else {
921 /* just read raw bytes */
922 len = RAW_PACKET_SIZE;
923 len= av_get_packet(pb, pkt, len);
924 pkt->stream_index = 0;
925 if (len <= 0) {
926 return AVERROR_IO;
927 }
928 pkt->size = len;
929 }
930 } else {
931 int seq=1;
932resync:
933 len=sync(s, &timestamp, &flags, &i, &pos);
934 if(len<0)
935 return AVERROR_IO;
936 st = s->streams[i];
937
938 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
939 int h, pic_num, len2, pos;
940
941 h= get_byte(pb); len--;
942 if(!(h & 0x40)){
943 seq = get_byte(pb); len--;
944 }
945
946 if((h & 0xc0) == 0x40){
947 len2= pos= 0;
948 }else{
949 len2 = get_num(pb, &len);
950 pos = get_num(pb, &len);
951 }
952 /* picture number */
953 pic_num= get_byte(pb); len--;
954 rm->remaining_len= len;
955 rm->current_stream= st->id;
956
957// av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
958 if(len2 && len2<len)
959 len=len2;
960 rm->remaining_len-= len;
961 av_get_packet(pb, pkt, len);
962 }
963
964 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
965 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
966 (st->codec->codec_id == CODEC_ID_COOK)) {
967 int x;
968 int sps = rm->sub_packet_size;
969 int cfs = rm->coded_framesize;
970 int h = rm->sub_packet_h;
971 int y = rm->sub_packet_cnt;
972 int w = rm->audio_framesize;
973
974 if (flags & 2)
975 y = rm->sub_packet_cnt = 0;
976 if (!y)
977 rm->audiotimestamp = timestamp;
978
979 switch(st->codec->codec_id) {
980 case CODEC_ID_RA_288:
981 for (x = 0; x < h/2; x++)
982 get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
983 break;
984 case CODEC_ID_COOK:
985 for (x = 0; x < w/sps; x++)
986 get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
987 break;
988 }
989
990 if (++(rm->sub_packet_cnt) < h)
991 goto resync;
992 else {
993 rm->sub_packet_cnt = 0;
994 rm->audio_stream_num = i;
995 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
996 // Release first audio packet
997 av_new_packet(pkt, st->codec->block_align);
998 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
999 timestamp = rm->audiotimestamp;
1000 flags = 2; // Mark first packet as keyframe
1001 }
1002 } else if (st->codec->codec_id == CODEC_ID_AAC) {
1003 int x;
1004 rm->audio_stream_num = i;
1005 rm->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
1006 if (rm->sub_packet_cnt) {
1007 for (x = 0; x < rm->sub_packet_cnt; x++)
1008 rm->sub_packet_lengths[x] = get_be16(pb);
1009 // Release first audio packet
1010 rm->audio_pkt_cnt = rm->sub_packet_cnt - 1;
1011 av_get_packet(pb, pkt, rm->sub_packet_lengths[0]);
1012 flags = 2; // Mark first packet as keyframe
1013 }
1014 } else
1015 av_get_packet(pb, pkt, len);
1016 }
1017
1018 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
1019 || st->discard >= AVDISCARD_ALL){
1020 av_free_packet(pkt);
1021 goto resync;
1022 }
1023
1024 pkt->stream_index = i;
1025
1026#if 0
1027 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1028 if(st->codec->codec_id == CODEC_ID_RV20){
1029 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
1030 av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
1031
1032 seq |= (timestamp&~0x3FFF);
1033 if(seq - timestamp > 0x2000) seq -= 0x4000;
1034 if(seq - timestamp < -0x2000) seq += 0x4000;
1035 }
1036 }
1037#endif
1038 pkt->pts= timestamp;
1039 if(flags&2){
1040 pkt->flags |= PKT_FLAG_KEY;
1041 if((seq&0x7F) == 1)
1042 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
1043 }
1044 }
1045
1046 /* for AC3, needs to swap bytes */
1047 if (st->codec->codec_id == CODEC_ID_AC3) {
1048 ptr = pkt->data;
1049 for(j=0;j<len;j+=2) {
1050 tmp = ptr[0];
1051 ptr[0] = ptr[1];
1052 ptr[1] = tmp;
1053 ptr += 2;
1054 }
1055 }
1056 return 0;
1057}
1058
1059static int rm_read_close(AVFormatContext *s)
1060{
1061 RMContext *rm = s->priv_data;
1062
1063 av_free(rm->audiobuf);
1064 return 0;
1065}
1066
1067static int rm_probe(AVProbeData *p)
1068{
1069 /* check file header */
1070 if (p->buf_size <= 32)
1071 return 0;
1072 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1073 p->buf[2] == 'M' && p->buf[3] == 'F' &&
1074 p->buf[4] == 0 && p->buf[5] == 0) ||
1075 (p->buf[0] == '.' && p->buf[1] == 'r' &&
1076 p->buf[2] == 'a' && p->buf[3] == 0xfd))
1077 return AVPROBE_SCORE_MAX;
1078 else
1079 return 0;
1080}
1081
1082static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1083 int64_t *ppos, int64_t pos_limit)
1084{
1085 RMContext *rm = s->priv_data;
1086 int64_t pos, dts;
1087 int stream_index2, flags, len, h;
1088
1089 pos = *ppos;
1090
1091 if(rm->old_format)
1092 return AV_NOPTS_VALUE;
1093
1094 url_fseek(&s->pb, pos, SEEK_SET);
1095 rm->remaining_len=0;
1096 for(;;){
1097 int seq=1;
1098 AVStream *st;
1099
1100 len=sync(s, &dts, &flags, &stream_index2, &pos);
1101 if(len<0)
1102 return AV_NOPTS_VALUE;
1103
1104 st = s->streams[stream_index2];
1105 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1106 h= get_byte(&s->pb); len--;
1107 if(!(h & 0x40)){
1108 seq = get_byte(&s->pb); len--;
1109 }
1110 }
1111
1112 if((flags&2) && (seq&0x7F) == 1){
1113// av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
1114 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1115 if(stream_index2 == stream_index)
1116 break;
1117 }
1118
1119 url_fskip(&s->pb, len);
1120 }
1121 *ppos = pos;
1122 return dts;
1123}
1124
1125static AVInputFormat rm_demuxer = {
1126 "rm",
1127 "rm format",
1128 sizeof(RMContext),
1129 rm_probe,
1130 rm_read_header,
1131 rm_read_packet,
1132 rm_read_close,
1133 NULL,
1134 rm_read_dts,
1135};
1136
1137#ifdef CONFIG_MUXERS
1138static AVOutputFormat rm_muxer = {
1139 "rm",
1140 "rm format",
1141 "application/vnd.rn-realmedia",
1142 "rm,ra",
1143 sizeof(RMContext),
1144 CODEC_ID_AC3,
1145 CODEC_ID_RV10,
1146 rm_write_header,
1147 rm_write_packet,
1148 rm_write_trailer,
1149};
1150#endif //CONFIG_MUXERS
1151
1152int rm_init(void)
1153{
1154 av_register_input_format(&rm_demuxer);
1155#ifdef CONFIG_MUXERS
1156 av_register_output_format(&rm_muxer);
1157#endif //CONFIG_MUXERS
1158 return 0;
1159}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette