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