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