00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "avformat.h"
00025 #include "rm.h"
00026
00027 struct RMStream {
00028 AVPacket pkt;
00029 int videobufsize;
00030 int videobufpos;
00031 int curpic_num;
00032 int cur_slice, slices;
00033 int64_t pktpos;
00034
00035 int64_t audiotimestamp;
00036 int sub_packet_cnt;
00037 int sub_packet_size, sub_packet_h, coded_framesize;
00038 int audio_framesize;
00039 int sub_packet_lengths[16];
00040 };
00041
00042 typedef struct {
00043 int nb_packets;
00044 int old_format;
00045 int current_stream;
00046 int remaining_len;
00047 int audio_stream_num;
00048 int audio_pkt_cnt;
00049 } RMDemuxContext;
00050
00051 static inline void get_strl(ByteIOContext *pb, char *buf, int buf_size, int len)
00052 {
00053 int i;
00054 char *q, r;
00055
00056 q = buf;
00057 for(i=0;i<len;i++) {
00058 r = get_byte(pb);
00059 if (i < buf_size - 1)
00060 *q++ = r;
00061 }
00062 if (buf_size > 0) *q = '\0';
00063 }
00064
00065 static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
00066 {
00067 get_strl(pb, buf, buf_size, get_byte(pb));
00068 }
00069
00070 static void rm_read_metadata(AVFormatContext *s, int wide)
00071 {
00072 char buf[1024];
00073 int i;
00074 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
00075 int len = wide ? get_be16(s->pb) : get_byte(s->pb);
00076 get_strl(s->pb, buf, sizeof(buf), len);
00077 av_metadata_set(&s->metadata, ff_rm_metadata[i], buf);
00078 }
00079 }
00080
00081 RMStream *ff_rm_alloc_rmstream (void)
00082 {
00083 RMStream *rms = av_mallocz(sizeof(RMStream));
00084 rms->curpic_num = -1;
00085 return rms;
00086 }
00087
00088 void ff_rm_free_rmstream (RMStream *rms)
00089 {
00090 av_free_packet(&rms->pkt);
00091 }
00092
00093 static int rm_read_audio_stream_info(AVFormatContext *s, ByteIOContext *pb,
00094 AVStream *st, RMStream *ast, int read_all)
00095 {
00096 char buf[256];
00097 uint32_t version;
00098
00099
00100 version = get_be32(pb);
00101 if (((version >> 16) & 0xff) == 3) {
00102 int64_t startpos = url_ftell(pb);
00103 url_fskip(pb, 14);
00104 rm_read_metadata(s, 0);
00105 if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
00106
00107 get_byte(pb);
00108 get_str8(pb, buf, sizeof(buf));
00109 }
00110
00111 if ((startpos + (version & 0xffff)) > url_ftell(pb))
00112 url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
00113 st->codec->sample_rate = 8000;
00114 st->codec->channels = 1;
00115 st->codec->codec_type = CODEC_TYPE_AUDIO;
00116 st->codec->codec_id = CODEC_ID_RA_144;
00117 } else {
00118 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
00119
00120 get_be32(pb);
00121 get_be32(pb);
00122 get_be16(pb);
00123 get_be32(pb);
00124 flavor= get_be16(pb);
00125 ast->coded_framesize = coded_framesize = get_be32(pb);
00126 get_be32(pb);
00127 get_be32(pb);
00128 get_be32(pb);
00129 ast->sub_packet_h = sub_packet_h = get_be16(pb);
00130 st->codec->block_align= get_be16(pb);
00131 ast->sub_packet_size = sub_packet_size = get_be16(pb);
00132 get_be16(pb);
00133 if (((version >> 16) & 0xff) == 5) {
00134 get_be16(pb); get_be16(pb); get_be16(pb);
00135 }
00136 st->codec->sample_rate = get_be16(pb);
00137 get_be32(pb);
00138 st->codec->channels = get_be16(pb);
00139 if (((version >> 16) & 0xff) == 5) {
00140 get_be32(pb);
00141 get_buffer(pb, buf, 4);
00142 buf[4] = 0;
00143 } else {
00144 get_str8(pb, buf, sizeof(buf));
00145 get_str8(pb, buf, sizeof(buf));
00146 }
00147 st->codec->codec_type = CODEC_TYPE_AUDIO;
00148 if (!strcmp(buf, "dnet")) {
00149 st->codec->codec_id = CODEC_ID_AC3;
00150 st->need_parsing = AVSTREAM_PARSE_FULL;
00151 } else if (!strcmp(buf, "28_8")) {
00152 st->codec->codec_id = CODEC_ID_RA_288;
00153 st->codec->extradata_size= 0;
00154 ast->audio_framesize = st->codec->block_align;
00155 st->codec->block_align = coded_framesize;
00156
00157 if (ast->audio_framesize <= 0 || sub_packet_h <= 0 ||
00158 ast->audio_framesize >= UINT_MAX / sub_packet_h){
00159 av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h is invalid\n");
00160 return -1;
00161 }
00162
00163 av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
00164 } else if ((!strcmp(buf, "cook")) || (!strcmp(buf, "atrc")) || (!strcmp(buf, "sipr"))) {
00165 int codecdata_length;
00166 get_be16(pb); get_byte(pb);
00167 if (((version >> 16) & 0xff) == 5)
00168 get_byte(pb);
00169 codecdata_length = get_be32(pb);
00170 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
00171 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
00172 return -1;
00173 }
00174
00175 if(sub_packet_size <= 0){
00176 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
00177 return -1;
00178 }
00179
00180 if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
00181 else if (!strcmp(buf, "sipr")) st->codec->codec_id = CODEC_ID_SIPR;
00182 else st->codec->codec_id = CODEC_ID_ATRAC3;
00183 st->codec->extradata_size= codecdata_length;
00184 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00185 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
00186 ast->audio_framesize = st->codec->block_align;
00187 st->codec->block_align = ast->sub_packet_size;
00188
00189 if (ast->audio_framesize <= 0 || sub_packet_h <= 0 ||
00190 ast->audio_framesize >= UINT_MAX / sub_packet_h){
00191 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h is invalid\n");
00192 return -1;
00193 }
00194
00195 av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
00196 } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
00197 int codecdata_length;
00198 get_be16(pb); get_byte(pb);
00199 if (((version >> 16) & 0xff) == 5)
00200 get_byte(pb);
00201 st->codec->codec_id = CODEC_ID_AAC;
00202 codecdata_length = get_be32(pb);
00203 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
00204 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
00205 return -1;
00206 }
00207 if (codecdata_length >= 1) {
00208 st->codec->extradata_size = codecdata_length - 1;
00209 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00210 get_byte(pb);
00211 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
00212 }
00213 } else {
00214 st->codec->codec_id = CODEC_ID_NONE;
00215 av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
00216 }
00217 if (read_all) {
00218 get_byte(pb);
00219 get_byte(pb);
00220 get_byte(pb);
00221 rm_read_metadata(s, 0);
00222 }
00223 }
00224 return 0;
00225 }
00226
00227 int
00228 ff_rm_read_mdpr_codecdata (AVFormatContext *s, ByteIOContext *pb,
00229 AVStream *st, RMStream *rst, int codec_data_size)
00230 {
00231 unsigned int v;
00232 int size;
00233 int64_t codec_pos;
00234
00235 av_set_pts_info(st, 64, 1, 1000);
00236 codec_pos = url_ftell(pb);
00237 v = get_be32(pb);
00238 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
00239
00240 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
00241 return -1;
00242 } else {
00243 int fps, fps2;
00244 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
00245 fail1:
00246 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
00247 goto skip;
00248 }
00249 st->codec->codec_tag = get_le32(pb);
00250
00251 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
00252 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
00253 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
00254 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0')
00255 && st->codec->codec_tag != MKTAG('R', 'V', 'T', 'R'))
00256 goto fail1;
00257 st->codec->width = get_be16(pb);
00258 st->codec->height = get_be16(pb);
00259 st->codec->time_base.num= 1;
00260 fps= get_be16(pb);
00261 st->codec->codec_type = CODEC_TYPE_VIDEO;
00262 get_be32(pb);
00263 fps2= get_be16(pb);
00264 get_be16(pb);
00265
00266 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
00267
00268 if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
00269
00270 av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
00271 return -1;
00272 }
00273 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00274 if (!st->codec->extradata)
00275 return AVERROR(ENOMEM);
00276 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
00277
00278
00279 st->codec->time_base.den = fps * st->codec->time_base.num;
00280 switch(((uint8_t*)st->codec->extradata)[4]>>4){
00281 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
00282 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
00283 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
00284 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
00285 default: goto fail1;
00286 }
00287 }
00288
00289 skip:
00290
00291 size = url_ftell(pb) - codec_pos;
00292 url_fskip(pb, codec_data_size - size);
00293
00294 return 0;
00295 }
00296
00297
00298 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
00299 {
00300 RMDemuxContext *rm = s->priv_data;
00301 AVStream *st;
00302
00303 rm->old_format = 1;
00304 st = av_new_stream(s, 0);
00305 if (!st)
00306 return -1;
00307 st->priv_data = ff_rm_alloc_rmstream();
00308 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
00309 }
00310
00311 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
00312 {
00313 RMDemuxContext *rm = s->priv_data;
00314 AVStream *st;
00315 ByteIOContext *pb = s->pb;
00316 unsigned int tag;
00317 int tag_size;
00318 unsigned int start_time, duration;
00319 char buf[128];
00320 int flags = 0;
00321
00322 tag = get_le32(pb);
00323 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
00324
00325 return rm_read_header_old(s, ap);
00326 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
00327 return AVERROR(EIO);
00328 }
00329
00330 get_be32(pb);
00331 get_be16(pb);
00332 get_be32(pb);
00333 get_be32(pb);
00334
00335 for(;;) {
00336 if (url_feof(pb))
00337 return -1;
00338 tag = get_le32(pb);
00339 tag_size = get_be32(pb);
00340 get_be16(pb);
00341 #if 0
00342 printf("tag=%c%c%c%c (%08x) size=%d\n",
00343 (tag) & 0xff,
00344 (tag >> 8) & 0xff,
00345 (tag >> 16) & 0xff,
00346 (tag >> 24) & 0xff,
00347 tag,
00348 tag_size);
00349 #endif
00350 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
00351 return -1;
00352 switch(tag) {
00353 case MKTAG('P', 'R', 'O', 'P'):
00354
00355 get_be32(pb);
00356 get_be32(pb);
00357 get_be32(pb);
00358 get_be32(pb);
00359 get_be32(pb);
00360 get_be32(pb);
00361 get_be32(pb);
00362 get_be32(pb);
00363 get_be32(pb);
00364 get_be16(pb);
00365 flags = get_be16(pb);
00366 break;
00367 case MKTAG('C', 'O', 'N', 'T'):
00368 rm_read_metadata(s, 1);
00369 break;
00370 case MKTAG('M', 'D', 'P', 'R'):
00371 st = av_new_stream(s, 0);
00372 if (!st)
00373 return AVERROR(ENOMEM);
00374 st->id = get_be16(pb);
00375 get_be32(pb);
00376 st->codec->bit_rate = get_be32(pb);
00377 get_be32(pb);
00378 get_be32(pb);
00379 start_time = get_be32(pb);
00380 get_be32(pb);
00381 duration = get_be32(pb);
00382 st->start_time = start_time;
00383 st->duration = duration;
00384 get_str8(pb, buf, sizeof(buf));
00385 get_str8(pb, buf, sizeof(buf));
00386 st->codec->codec_type = CODEC_TYPE_DATA;
00387 st->priv_data = ff_rm_alloc_rmstream();
00388 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
00389 get_be32(pb)) < 0)
00390 return -1;
00391 break;
00392 case MKTAG('D', 'A', 'T', 'A'):
00393 goto header_end;
00394 default:
00395
00396 url_fskip(pb, tag_size - 10);
00397 break;
00398 }
00399 }
00400 header_end:
00401 rm->nb_packets = get_be32(pb);
00402 if (!rm->nb_packets && (flags & 4))
00403 rm->nb_packets = 3600 * 25;
00404 get_be32(pb);
00405 return 0;
00406 }
00407
00408 static int get_num(ByteIOContext *pb, int *len)
00409 {
00410 int n, n1;
00411
00412 n = get_be16(pb);
00413 (*len)-=2;
00414 n &= 0x7FFF;
00415 if (n >= 0x4000) {
00416 return n - 0x4000;
00417 } else {
00418 n1 = get_be16(pb);
00419 (*len)-=2;
00420 return (n << 16) | n1;
00421 }
00422 }
00423
00424
00425 #define RAW_PACKET_SIZE 1000
00426
00427 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
00428 RMDemuxContext *rm = s->priv_data;
00429 ByteIOContext *pb = s->pb;
00430 int len, num, res, i;
00431 AVStream *st;
00432 uint32_t state=0xFFFFFFFF;
00433
00434 while(!url_feof(pb)){
00435 *pos= url_ftell(pb) - 3;
00436 if(rm->remaining_len > 0){
00437 num= rm->current_stream;
00438 len= rm->remaining_len;
00439 *timestamp = AV_NOPTS_VALUE;
00440 *flags= 0;
00441 }else{
00442 state= (state<<8) + get_byte(pb);
00443
00444 if(state == MKBETAG('I', 'N', 'D', 'X')){
00445 len = get_be16(pb) - 6;
00446 if(len<0)
00447 continue;
00448 goto skip;
00449 }
00450
00451 if(state > (unsigned)0xFFFF || state < 12)
00452 continue;
00453 len=state;
00454 state= 0xFFFFFFFF;
00455
00456 num = get_be16(pb);
00457 *timestamp = get_be32(pb);
00458 res= get_byte(pb);
00459 *flags = get_byte(pb);
00460
00461
00462 len -= 12;
00463 }
00464 for(i=0;i<s->nb_streams;i++) {
00465 st = s->streams[i];
00466 if (num == st->id)
00467 break;
00468 }
00469 if (i == s->nb_streams) {
00470 skip:
00471
00472 url_fskip(pb, len);
00473 rm->remaining_len -= len;
00474 continue;
00475 }
00476 *stream_index= i;
00477
00478 return len;
00479 }
00480 return -1;
00481 }
00482
00483 static int rm_assemble_video_frame(AVFormatContext *s, ByteIOContext *pb,
00484 RMDemuxContext *rm, RMStream *vst,
00485 AVPacket *pkt, int len)
00486 {
00487 int hdr, seq, pic_num, len2, pos;
00488 int type;
00489
00490 hdr = get_byte(pb); len--;
00491 type = hdr >> 6;
00492
00493 if(type != 3){
00494 seq = get_byte(pb); len--;
00495 }
00496 if(type != 1){
00497 len2 = get_num(pb, &len);
00498 pos = get_num(pb, &len);
00499 pic_num = get_byte(pb); len--;
00500 }
00501 if(len<0)
00502 return -1;
00503 rm->remaining_len = len;
00504 if(type&1){
00505 if(type == 3)
00506 len= len2;
00507 if(rm->remaining_len < len)
00508 return -1;
00509 rm->remaining_len -= len;
00510 if(av_new_packet(pkt, len + 9) < 0)
00511 return AVERROR(EIO);
00512 pkt->data[0] = 0;
00513 AV_WL32(pkt->data + 1, 1);
00514 AV_WL32(pkt->data + 5, 0);
00515 get_buffer(pb, pkt->data + 9, len);
00516 return 0;
00517 }
00518
00519
00520 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
00521 vst->slices = ((hdr & 0x3F) << 1) + 1;
00522 vst->videobufsize = len2 + 8*vst->slices + 1;
00523 av_free_packet(&vst->pkt);
00524 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
00525 return AVERROR(ENOMEM);
00526 vst->videobufpos = 8*vst->slices + 1;
00527 vst->cur_slice = 0;
00528 vst->curpic_num = pic_num;
00529 vst->pktpos = url_ftell(pb);
00530 }
00531 if(type == 2)
00532 len = FFMIN(len, pos);
00533
00534 if(++vst->cur_slice > vst->slices)
00535 return 1;
00536 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
00537 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
00538 if(vst->videobufpos + len > vst->videobufsize)
00539 return 1;
00540 if (get_buffer(pb, vst->pkt.data + vst->videobufpos, len) != len)
00541 return AVERROR(EIO);
00542 vst->videobufpos += len;
00543 rm->remaining_len-= len;
00544
00545 if(type == 2 || (vst->videobufpos) == vst->videobufsize){
00546 vst->pkt.data[0] = vst->cur_slice-1;
00547 *pkt= vst->pkt;
00548 vst->pkt.data= NULL;
00549 vst->pkt.size= 0;
00550 if(vst->slices != vst->cur_slice)
00551 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
00552 vst->videobufpos - 1 - 8*vst->slices);
00553 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
00554 pkt->pts = AV_NOPTS_VALUE;
00555 pkt->pos = vst->pktpos;
00556 return 0;
00557 }
00558
00559 return 1;
00560 }
00561
00562 static inline void
00563 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
00564 {
00565 uint8_t *ptr;
00566 int j;
00567
00568 if (st->codec->codec_id == CODEC_ID_AC3) {
00569 ptr = pkt->data;
00570 for (j=0;j<pkt->size;j+=2) {
00571 FFSWAP(int, ptr[0], ptr[1]);
00572 ptr += 2;
00573 }
00574 }
00575 }
00576
00577 int
00578 ff_rm_parse_packet (AVFormatContext *s, ByteIOContext *pb,
00579 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
00580 int *seq, int *flags, int64_t *timestamp)
00581 {
00582 RMDemuxContext *rm = s->priv_data;
00583
00584 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
00585 rm->current_stream= st->id;
00586 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len))
00587 return -1;
00588 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
00589 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
00590 (st->codec->codec_id == CODEC_ID_COOK) ||
00591 (st->codec->codec_id == CODEC_ID_ATRAC3) ||
00592 (st->codec->codec_id == CODEC_ID_SIPR)) {
00593 int x;
00594 int sps = ast->sub_packet_size;
00595 int cfs = ast->coded_framesize;
00596 int h = ast->sub_packet_h;
00597 int y = ast->sub_packet_cnt;
00598 int w = ast->audio_framesize;
00599
00600 if (*flags & 2)
00601 y = ast->sub_packet_cnt = 0;
00602 if (!y)
00603 ast->audiotimestamp = *timestamp;
00604
00605 switch(st->codec->codec_id) {
00606 case CODEC_ID_RA_288:
00607 for (x = 0; x < h/2; x++)
00608 get_buffer(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
00609 break;
00610 case CODEC_ID_ATRAC3:
00611 case CODEC_ID_COOK:
00612 for (x = 0; x < w/sps; x++)
00613 get_buffer(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
00614 break;
00615 }
00616
00617 if (++(ast->sub_packet_cnt) < h)
00618 return -1;
00619 else {
00620 ast->sub_packet_cnt = 0;
00621 rm->audio_stream_num = st->index;
00622 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
00623
00624 av_new_packet(pkt, st->codec->block_align);
00625 memcpy(pkt->data, ast->pkt.data, st->codec->block_align);
00626 *timestamp = ast->audiotimestamp;
00627 *flags = 2;
00628 }
00629 } else if (st->codec->codec_id == CODEC_ID_AAC) {
00630 int x;
00631 rm->audio_stream_num = st->index;
00632 ast->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
00633 if (ast->sub_packet_cnt) {
00634 for (x = 0; x < ast->sub_packet_cnt; x++)
00635 ast->sub_packet_lengths[x] = get_be16(pb);
00636
00637 rm->audio_pkt_cnt = ast->sub_packet_cnt - 1;
00638 av_get_packet(pb, pkt, ast->sub_packet_lengths[0]);
00639 *flags = 2;
00640 }
00641 } else {
00642 av_get_packet(pb, pkt, len);
00643 rm_ac3_swap_bytes(st, pkt);
00644 }
00645 } else
00646 av_get_packet(pb, pkt, len);
00647
00648 if( (st->discard >= AVDISCARD_NONKEY && !(*flags&2))
00649 || st->discard >= AVDISCARD_ALL){
00650 av_free_packet(pkt);
00651 return -1;
00652 }
00653
00654 pkt->stream_index = st->index;
00655
00656 #if 0
00657 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
00658 if(st->codec->codec_id == CODEC_ID_RV20){
00659 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
00660 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
00661
00662 seq |= (*timestamp&~0x3FFF);
00663 if(seq - *timestamp > 0x2000) seq -= 0x4000;
00664 if(seq - *timestamp < -0x2000) seq += 0x4000;
00665 }
00666 }
00667 #endif
00668
00669 pkt->pts= *timestamp;
00670 if (*flags & 2)
00671 pkt->flags |= PKT_FLAG_KEY;
00672
00673 return st->codec->codec_type == CODEC_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
00674 }
00675
00676 int
00677 ff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,
00678 AVStream *st, RMStream *ast, AVPacket *pkt)
00679 {
00680 RMDemuxContext *rm = s->priv_data;
00681
00682 assert (rm->audio_pkt_cnt > 0);
00683
00684 if (st->codec->codec_id == CODEC_ID_AAC)
00685 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
00686 else {
00687 av_new_packet(pkt, st->codec->block_align);
00688 memcpy(pkt->data, ast->pkt.data + st->codec->block_align *
00689 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
00690 st->codec->block_align);
00691 }
00692 rm->audio_pkt_cnt--;
00693 pkt->flags = 0;
00694 pkt->stream_index = st->index;
00695
00696 return rm->audio_pkt_cnt;
00697 }
00698
00699 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
00700 {
00701 RMDemuxContext *rm = s->priv_data;
00702 ByteIOContext *pb = s->pb;
00703 AVStream *st;
00704 int i, len;
00705 int64_t timestamp, pos;
00706 int flags;
00707
00708 if (rm->audio_pkt_cnt) {
00709
00710 st = s->streams[rm->audio_stream_num];
00711 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
00712 } else if (rm->old_format) {
00713 RMStream *ast;
00714
00715 st = s->streams[0];
00716 ast = st->priv_data;
00717 if (st->codec->codec_id == CODEC_ID_RA_288) {
00718 int x, y;
00719
00720 for (y = 0; y < ast->sub_packet_h; y++)
00721 for (x = 0; x < ast->sub_packet_h/2; x++)
00722 if (get_buffer(pb, ast->pkt.data+x*2*ast->audio_framesize+y*ast->coded_framesize, ast->coded_framesize) <= 0)
00723 return AVERROR(EIO);
00724 rm->audio_stream_num = 0;
00725 rm->audio_pkt_cnt = ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - 1;
00726
00727 av_new_packet(pkt, st->codec->block_align);
00728 memcpy(pkt->data, ast->pkt.data, st->codec->block_align);
00729 pkt->flags |= PKT_FLAG_KEY;
00730 pkt->stream_index = 0;
00731 } else {
00732
00733 len = RAW_PACKET_SIZE;
00734 len= av_get_packet(pb, pkt, len);
00735 pkt->stream_index = 0;
00736 if (len <= 0) {
00737 return AVERROR(EIO);
00738 }
00739 pkt->size = len;
00740 }
00741 rm_ac3_swap_bytes(st, pkt);
00742 } else {
00743 int seq=1;
00744 resync:
00745 len=sync(s, ×tamp, &flags, &i, &pos);
00746 if(len<0)
00747 return AVERROR(EIO);
00748 st = s->streams[i];
00749
00750 if (ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
00751 &seq, &flags, ×tamp) < 0)
00752 goto resync;
00753
00754 if((flags&2) && (seq&0x7F) == 1)
00755 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
00756 }
00757
00758 return 0;
00759 }
00760
00761 static int rm_read_close(AVFormatContext *s)
00762 {
00763 int i;
00764
00765 for (i=0;i<s->nb_streams;i++)
00766 ff_rm_free_rmstream(s->streams[i]->priv_data);
00767
00768 return 0;
00769 }
00770
00771 static int rm_probe(AVProbeData *p)
00772 {
00773
00774 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
00775 p->buf[2] == 'M' && p->buf[3] == 'F' &&
00776 p->buf[4] == 0 && p->buf[5] == 0) ||
00777 (p->buf[0] == '.' && p->buf[1] == 'r' &&
00778 p->buf[2] == 'a' && p->buf[3] == 0xfd))
00779 return AVPROBE_SCORE_MAX;
00780 else
00781 return 0;
00782 }
00783
00784 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
00785 int64_t *ppos, int64_t pos_limit)
00786 {
00787 RMDemuxContext *rm = s->priv_data;
00788 int64_t pos, dts;
00789 int stream_index2, flags, len, h;
00790
00791 pos = *ppos;
00792
00793 if(rm->old_format)
00794 return AV_NOPTS_VALUE;
00795
00796 url_fseek(s->pb, pos, SEEK_SET);
00797 rm->remaining_len=0;
00798 for(;;){
00799 int seq=1;
00800 AVStream *st;
00801
00802 len=sync(s, &dts, &flags, &stream_index2, &pos);
00803 if(len<0)
00804 return AV_NOPTS_VALUE;
00805
00806 st = s->streams[stream_index2];
00807 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
00808 h= get_byte(s->pb); len--;
00809 if(!(h & 0x40)){
00810 seq = get_byte(s->pb); len--;
00811 }
00812 }
00813
00814 if((flags&2) && (seq&0x7F) == 1){
00815
00816 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
00817 if(stream_index2 == stream_index)
00818 break;
00819 }
00820
00821 url_fskip(s->pb, len);
00822 }
00823 *ppos = pos;
00824 return dts;
00825 }
00826
00827 AVInputFormat rm_demuxer = {
00828 "rm",
00829 NULL_IF_CONFIG_SMALL("RealMedia format"),
00830 sizeof(RMDemuxContext),
00831 rm_probe,
00832 rm_read_header,
00833 rm_read_packet,
00834 rm_read_close,
00835 NULL,
00836 rm_read_dts,
00837 };
00838
00839 AVInputFormat rdt_demuxer = {
00840 "rdt",
00841 NULL_IF_CONFIG_SMALL("RDT demuxer"),
00842 sizeof(RMDemuxContext),
00843 NULL,
00844 NULL,
00845 NULL,
00846 rm_read_close,
00847 };