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