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