Mercurial > libavformat.hg
annotate rm.c @ 22:65433f1b2549 libavformat
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
author | michaelni |
---|---|
date | Sat, 11 Jan 2003 20:34:38 +0000 |
parents | b167760cd0aa |
children | 3eff1f2dcf71 |
rev | line source |
---|---|
0 | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 } RMContext; | |
43 | |
44 static 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 | |
52 static 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 | |
60 static 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 */ | |
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)); | |
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); | |
232 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */ | |
233 put_be32(s,0); /* unknown meaning */ | |
234 put_be16(s, (int) stream->frame_rate); /* unknown meaning */ | |
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 | |
262 static 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 | |
282 static int rm_write_header(AVFormatContext *s) | |
283 { | |
284 RMContext *rm = s->priv_data; | |
285 StreamInfo *stream; | |
286 int n; | |
287 AVCodecContext *codec; | |
288 | |
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; | |
315 default: | |
316 av_abort(); | |
317 } | |
318 } | |
319 | |
320 rv10_write_header(s, 0, 0); | |
321 put_flush_packet(&s->pb); | |
322 return 0; | |
323 } | |
324 | |
325 static int rm_write_audio(AVFormatContext *s, UINT8 *buf, int size) | |
326 { | |
327 UINT8 *buf1; | |
328 RMContext *rm = s->priv_data; | |
329 ByteIOContext *pb = &s->pb; | |
330 StreamInfo *stream = rm->audio_stream; | |
331 int i; | |
332 | |
333 /* XXX: suppress this malloc */ | |
334 buf1= (UINT8*) av_malloc( size * sizeof(UINT8) ); | |
335 | |
7 | 336 write_packet_header(s, stream, size, stream->enc->coded_frame->key_frame); |
0 | 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++; | |
346 av_free(buf1); | |
347 return 0; | |
348 } | |
349 | |
350 static 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; | |
7 | 355 int key_frame = stream->enc->coded_frame->key_frame; |
0 | 356 |
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 | |
390 static int rm_write_packet(AVFormatContext *s, int stream_index, | |
391 UINT8 *buf, int size, int force_pts) | |
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 | |
400 static 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); | |
435 return 0; | |
436 } | |
437 | |
438 /***************************************************/ | |
439 | |
440 static 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 | |
454 static 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 | |
468 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
469 { | |
470 RMContext *rm = s->priv_data; | |
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]; | |
478 int flags = 0; | |
479 | |
480 if (get_le32(pb) != MKTAG('.', 'R', 'M', 'F')) | |
481 return -EIO; | |
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 */ | |
518 flags = get_be16(pb); /* flags */ | |
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; | |
5 | 530 avcodec_get_context_defaults(&st->codec); |
0 | 531 s->streams[s->nb_streams++] = st; |
532 st->id = get_be16(pb); | |
533 get_be32(pb); /* max bit rate */ | |
534 st->codec.bit_rate = get_be32(pb); /* bit rate */ | |
535 get_be32(pb); /* max packet size */ | |
536 get_be32(pb); /* avg packet size */ | |
537 get_be32(pb); /* start time */ | |
538 get_be32(pb); /* preroll */ | |
539 get_be32(pb); /* duration */ | |
540 get_str8(pb, buf, sizeof(buf)); /* desc */ | |
541 get_str8(pb, buf, sizeof(buf)); /* mimetype */ | |
542 codec_data_size = get_be32(pb); | |
543 codec_pos = url_ftell(pb); | |
544 | |
545 v = get_be32(pb); | |
546 if (v == MKTAG(0xfd, 'a', 'r', '.')) { | |
547 /* ra type header */ | |
548 get_be32(pb); /* version */ | |
549 get_be32(pb); /* .ra4 */ | |
550 get_be32(pb); | |
551 get_be16(pb); | |
552 get_be32(pb); /* header size */ | |
553 get_be16(pb); /* add codec info */ | |
554 get_be32(pb); /* coded frame size */ | |
555 get_be32(pb); /* ??? */ | |
556 get_be32(pb); /* ??? */ | |
557 get_be32(pb); /* ??? */ | |
558 get_be16(pb); /* 1 */ | |
559 get_be16(pb); /* coded frame size */ | |
560 get_be32(pb); | |
561 st->codec.sample_rate = get_be16(pb); | |
562 get_be32(pb); | |
563 st->codec.channels = get_be16(pb); | |
564 get_str8(pb, buf, sizeof(buf)); /* desc */ | |
565 get_str8(pb, buf, sizeof(buf)); /* desc */ | |
566 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
567 if (!strcmp(buf, "dnet")) { | |
568 st->codec.codec_id = CODEC_ID_AC3; | |
569 } else { | |
570 st->codec.codec_id = CODEC_ID_NONE; | |
571 pstrcpy(st->codec.codec_name, sizeof(st->codec.codec_name), | |
572 buf); | |
573 } | |
574 } else { | |
575 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) { | |
576 fail1: | |
577 fprintf(stderr, "Unsupported video codec\n"); | |
578 goto fail; | |
579 } | |
580 st->codec.codec_tag = get_le32(pb); | |
581 if (st->codec.codec_tag != MKTAG('R', 'V', '1', '0')) | |
582 goto fail1; | |
583 st->codec.width = get_be16(pb); | |
584 st->codec.height = get_be16(pb); | |
585 st->codec.frame_rate = get_be16(pb) * FRAME_RATE_BASE; | |
586 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
587 get_be32(pb); | |
588 get_be16(pb); | |
589 get_be32(pb); | |
590 get_be16(pb); | |
591 /* modification of h263 codec version (!) */ | |
592 h263_hack_version = get_be32(pb); | |
593 switch(h263_hack_version) { | |
594 case 0x10000000: | |
595 case 0x10003000: | |
596 case 0x10003001: | |
597 st->codec.sub_id = h263_hack_version; | |
598 st->codec.codec_id = CODEC_ID_RV10; | |
599 break; | |
600 default: | |
601 /* not handled */ | |
602 st->codec.codec_id = CODEC_ID_NONE; | |
603 break; | |
604 } | |
605 } | |
606 /* skip codec info */ | |
607 size = url_ftell(pb) - codec_pos; | |
608 url_fskip(pb, codec_data_size - size); | |
609 break; | |
610 case MKTAG('D', 'A', 'T', 'A'): | |
611 goto header_end; | |
612 default: | |
613 /* unknown tag: skip it */ | |
614 url_fskip(pb, tag_size - 10); | |
615 break; | |
616 } | |
617 } | |
618 header_end: | |
619 rm->nb_packets = get_be32(pb); /* number of packets */ | |
620 if (!rm->nb_packets && (flags & 4)) | |
621 rm->nb_packets = 3600 * 25; | |
622 get_be32(pb); /* next data header */ | |
623 return 0; | |
624 | |
625 fail: | |
626 for(i=0;i<s->nb_streams;i++) { | |
627 av_free(s->streams[i]); | |
628 } | |
629 return -EIO; | |
630 } | |
631 | |
632 static int get_num(ByteIOContext *pb, int *len) | |
633 { | |
634 int n, n1; | |
635 | |
636 n = get_be16(pb); | |
637 (*len)-=2; | |
638 if (n >= 0x4000) { | |
639 return n - 0x4000; | |
640 } else { | |
641 n1 = get_be16(pb); | |
642 (*len)-=2; | |
643 return (n << 16) | n1; | |
644 } | |
645 } | |
646 | |
647 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt) | |
648 { | |
649 RMContext *rm = s->priv_data; | |
650 ByteIOContext *pb = &s->pb; | |
651 AVStream *st; | |
652 int len, num, timestamp, i, tmp, j; | |
653 UINT8 *ptr; | |
654 int flags; | |
655 | |
656 redo: | |
657 if (rm->nb_packets == 0) | |
658 return -EIO; | |
659 get_be16(pb); | |
660 len = get_be16(pb); | |
661 if (len < 12) | |
662 return -EIO; | |
663 num = get_be16(pb); | |
664 timestamp = get_be32(pb); | |
665 get_byte(pb); /* reserved */ | |
666 flags = get_byte(pb); /* flags */ | |
667 rm->nb_packets--; | |
668 len -= 12; | |
669 | |
670 st = NULL; | |
671 for(i=0;i<s->nb_streams;i++) { | |
672 st = s->streams[i]; | |
673 if (num == st->id) | |
674 break; | |
675 } | |
676 if (i == s->nb_streams) { | |
677 /* skip packet if unknown number */ | |
678 url_fskip(pb, len); | |
679 goto redo; | |
680 } | |
681 | |
682 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
683 int full_frame, h, pic_num; | |
684 | |
685 h= get_byte(pb); | |
686 if ((h & 0xc0) == 0xc0) { | |
687 int len2, pos; | |
688 full_frame = 1; | |
689 len2= get_num(pb, &len); | |
690 pos = get_num(pb, &len); | |
691 //printf("pos:%d\n",len); | |
692 len -= 2; | |
693 } else { | |
694 int seq, frame_size, pos; | |
695 full_frame = 0; | |
696 seq = get_byte(pb); | |
697 frame_size = get_num(pb, &len); | |
698 pos = get_num(pb, &len); | |
699 //printf("seq:%d, size:%d, pos:%d\n",seq,frame_size,pos); | |
700 len -= 3; | |
701 } | |
702 /* picture number */ | |
703 pic_num= get_byte(pb); | |
704 | |
705 //XXX/FIXME/HACK, demuxer should be fixed to send complete frames ... | |
706 if(st->codec.slice_offset==NULL) st->codec.slice_offset= (int*)malloc(sizeof(int)); | |
707 st->codec.slice_count= full_frame; | |
708 st->codec.slice_offset[0]= 0; | |
709 } | |
710 | |
711 av_new_packet(pkt, len); | |
712 pkt->stream_index = i; | |
713 get_buffer(pb, pkt->data, len); | |
714 | |
715 /* for AC3, needs to swap bytes */ | |
716 if (st->codec.codec_id == CODEC_ID_AC3) { | |
717 ptr = pkt->data; | |
718 for(j=0;j<len;j+=2) { | |
719 tmp = ptr[0]; | |
720 ptr[0] = ptr[1]; | |
721 ptr[1] = tmp; | |
722 ptr += 2; | |
723 } | |
724 } | |
725 return 0; | |
726 } | |
727 | |
728 static int rm_read_close(AVFormatContext *s) | |
729 { | |
730 return 0; | |
731 } | |
732 | |
733 static int rm_probe(AVProbeData *p) | |
734 { | |
735 /* check file header */ | |
736 if (p->buf_size <= 32) | |
737 return 0; | |
738 if (p->buf[0] == '.' && p->buf[1] == 'R' && | |
739 p->buf[2] == 'M' && p->buf[3] == 'F' && | |
740 p->buf[4] == 0 && p->buf[5] == 0) | |
741 return AVPROBE_SCORE_MAX; | |
742 else | |
743 return 0; | |
744 } | |
745 | |
746 static AVInputFormat rm_iformat = { | |
747 "rm", | |
748 "rm format", | |
749 sizeof(RMContext), | |
750 rm_probe, | |
751 rm_read_header, | |
752 rm_read_packet, | |
753 rm_read_close, | |
754 }; | |
755 | |
756 static AVOutputFormat rm_oformat = { | |
757 "rm", | |
758 "rm format", | |
14
b167760cd0aa
mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents:
7
diff
changeset
|
759 "application/vnd.rn-realmedia", |
0 | 760 "rm,ra", |
761 sizeof(RMContext), | |
762 CODEC_ID_AC3, | |
763 CODEC_ID_RV10, | |
764 rm_write_header, | |
765 rm_write_packet, | |
766 rm_write_trailer, | |
767 }; | |
768 | |
769 int rm_init(void) | |
770 { | |
771 av_register_input_format(&rm_iformat); | |
772 av_register_output_format(&rm_oformat); | |
773 return 0; | |
774 } |