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