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 "riff.h"
00026 #include "rm.h"
00027
00028 struct RMStream {
00029 AVPacket pkt;
00030 int videobufsize;
00031 int videobufpos;
00032 int curpic_num;
00033 int cur_slice, slices;
00034 int64_t pktpos;
00035
00036 int64_t audiotimestamp;
00037 int sub_packet_cnt;
00038 int sub_packet_size, sub_packet_h, coded_framesize;
00039 int audio_framesize;
00040 int sub_packet_lengths[16];
00041 };
00042
00043 typedef struct {
00044 int nb_packets;
00045 int old_format;
00046 int current_stream;
00047 int remaining_len;
00048 int audio_stream_num;
00049 int audio_pkt_cnt;
00050 } RMDemuxContext;
00051
00052 static const unsigned char sipr_swaps[38][2] = {
00053 { 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 },
00054 { 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 },
00055 { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
00056 { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
00057 { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
00058 { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
00059 { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
00060 { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
00061 { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
00062 { 67, 83 }, { 77, 80 }
00063 };
00064
00065 const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 };
00066
00067 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
00068 {
00069 int i;
00070 char *q, r;
00071
00072 q = buf;
00073 for(i=0;i<len;i++) {
00074 r = avio_r8(pb);
00075 if (i < buf_size - 1)
00076 *q++ = r;
00077 }
00078 if (buf_size > 0) *q = '\0';
00079 }
00080
00081 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
00082 {
00083 get_strl(pb, buf, buf_size, avio_r8(pb));
00084 }
00085
00086 static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
00087 {
00088 if (size >= 1<<24)
00089 return -1;
00090 avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00091 if (!avctx->extradata)
00092 return AVERROR(ENOMEM);
00093 avctx->extradata_size = avio_read(pb, avctx->extradata, size);
00094 memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00095 if (avctx->extradata_size != size)
00096 return AVERROR(EIO);
00097 return 0;
00098 }
00099
00100 static void rm_read_metadata(AVFormatContext *s, int wide)
00101 {
00102 char buf[1024];
00103 int i;
00104 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
00105 int len = wide ? avio_rb16(s->pb) : avio_r8(s->pb);
00106 get_strl(s->pb, buf, sizeof(buf), len);
00107 av_metadata_set2(&s->metadata, ff_rm_metadata[i], buf, 0);
00108 }
00109 }
00110
00111 RMStream *ff_rm_alloc_rmstream (void)
00112 {
00113 RMStream *rms = av_mallocz(sizeof(RMStream));
00114 rms->curpic_num = -1;
00115 return rms;
00116 }
00117
00118 void ff_rm_free_rmstream (RMStream *rms)
00119 {
00120 av_free_packet(&rms->pkt);
00121 }
00122
00123 static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
00124 AVStream *st, RMStream *ast, int read_all)
00125 {
00126 char buf[256];
00127 uint32_t version;
00128 int ret;
00129
00130
00131 version = avio_rb16(pb);
00132 if (version == 3) {
00133 int header_size = avio_rb16(pb);
00134 int64_t startpos = avio_tell(pb);
00135 avio_seek(pb, 14, SEEK_CUR);
00136 rm_read_metadata(s, 0);
00137 if ((startpos + header_size) >= avio_tell(pb) + 2) {
00138
00139 avio_r8(pb);
00140 get_str8(pb, buf, sizeof(buf));
00141 }
00142
00143 if ((startpos + header_size) > avio_tell(pb))
00144 avio_seek(pb, header_size + startpos - avio_tell(pb), SEEK_CUR);
00145 st->codec->sample_rate = 8000;
00146 st->codec->channels = 1;
00147 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00148 st->codec->codec_id = CODEC_ID_RA_144;
00149 } else {
00150 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
00151 int codecdata_length;
00152
00153 avio_seek(pb, 2, SEEK_CUR);
00154 avio_rb32(pb);
00155 avio_rb32(pb);
00156 avio_rb16(pb);
00157 avio_rb32(pb);
00158 flavor= avio_rb16(pb);
00159 ast->coded_framesize = coded_framesize = avio_rb32(pb);
00160 avio_rb32(pb);
00161 avio_rb32(pb);
00162 avio_rb32(pb);
00163 ast->sub_packet_h = sub_packet_h = avio_rb16(pb);
00164 st->codec->block_align= avio_rb16(pb);
00165 ast->sub_packet_size = sub_packet_size = avio_rb16(pb);
00166 avio_rb16(pb);
00167 if (version == 5) {
00168 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
00169 }
00170 st->codec->sample_rate = avio_rb16(pb);
00171 avio_rb32(pb);
00172 st->codec->channels = avio_rb16(pb);
00173 if (version == 5) {
00174 avio_rb32(pb);
00175 avio_read(pb, buf, 4);
00176 buf[4] = 0;
00177 } else {
00178 get_str8(pb, buf, sizeof(buf));
00179 get_str8(pb, buf, sizeof(buf));
00180 }
00181 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00182 st->codec->codec_tag = AV_RL32(buf);
00183 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
00184 st->codec->codec_tag);
00185 switch (st->codec->codec_id) {
00186 case CODEC_ID_AC3:
00187 st->need_parsing = AVSTREAM_PARSE_FULL;
00188 break;
00189 case CODEC_ID_RA_288:
00190 st->codec->extradata_size= 0;
00191 ast->audio_framesize = st->codec->block_align;
00192 st->codec->block_align = coded_framesize;
00193
00194 if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
00195 av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
00196 return -1;
00197 }
00198
00199 av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
00200 break;
00201 case CODEC_ID_COOK:
00202 case CODEC_ID_ATRAC3:
00203 case CODEC_ID_SIPR:
00204 avio_rb16(pb); avio_r8(pb);
00205 if (version == 5)
00206 avio_r8(pb);
00207 codecdata_length = avio_rb32(pb);
00208 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
00209 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
00210 return -1;
00211 }
00212
00213 ast->audio_framesize = st->codec->block_align;
00214 if (st->codec->codec_id == CODEC_ID_SIPR) {
00215 if (flavor > 3) {
00216 av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
00217 flavor);
00218 return -1;
00219 }
00220 st->codec->block_align = ff_sipr_subpk_size[flavor];
00221 } else {
00222 if(sub_packet_size <= 0){
00223 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
00224 return -1;
00225 }
00226 st->codec->block_align = ast->sub_packet_size;
00227 }
00228 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
00229 return ret;
00230
00231 if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
00232 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
00233 return -1;
00234 }
00235
00236 av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
00237 break;
00238 case CODEC_ID_AAC:
00239 avio_rb16(pb); avio_r8(pb);
00240 if (version == 5)
00241 avio_r8(pb);
00242 codecdata_length = avio_rb32(pb);
00243 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
00244 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
00245 return -1;
00246 }
00247 if (codecdata_length >= 1) {
00248 avio_r8(pb);
00249 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
00250 return ret;
00251 }
00252 break;
00253 default:
00254 av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
00255 }
00256 if (read_all) {
00257 avio_r8(pb);
00258 avio_r8(pb);
00259 avio_r8(pb);
00260 rm_read_metadata(s, 0);
00261 }
00262 }
00263 return 0;
00264 }
00265
00266 int
00267 ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
00268 AVStream *st, RMStream *rst, int codec_data_size)
00269 {
00270 unsigned int v;
00271 int size;
00272 int64_t codec_pos;
00273 int ret;
00274
00275 av_set_pts_info(st, 64, 1, 1000);
00276 codec_pos = avio_tell(pb);
00277 v = avio_rb32(pb);
00278 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
00279
00280 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
00281 return -1;
00282 } else {
00283 int fps, fps2;
00284 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
00285 fail1:
00286 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
00287 goto skip;
00288 }
00289 st->codec->codec_tag = avio_rl32(pb);
00290 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
00291 st->codec->codec_tag);
00292
00293 if (st->codec->codec_id == CODEC_ID_NONE)
00294 goto fail1;
00295 st->codec->width = avio_rb16(pb);
00296 st->codec->height = avio_rb16(pb);
00297 st->codec->time_base.num= 1;
00298 fps= avio_rb16(pb);
00299 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00300 avio_rb32(pb);
00301 fps2= avio_rb16(pb);
00302 avio_rb16(pb);
00303
00304 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
00305 return ret;
00306
00307
00308 st->codec->time_base.den = fps * st->codec->time_base.num;
00309
00310 switch(st->codec->extradata[4]>>4){
00311 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
00312 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
00313 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
00314 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
00315 default:
00316 av_log(st->codec, AV_LOG_ERROR, "extra:%02X %02X %02X %02X %02X\n", st->codec->extradata[0], st->codec->extradata[1], st->codec->extradata[2], st->codec->extradata[3], st->codec->extradata[4]);
00317 goto fail1;
00318 }
00319 }
00320
00321 skip:
00322
00323 size = avio_tell(pb) - codec_pos;
00324 avio_seek(pb, codec_data_size - size, SEEK_CUR);
00325
00326 return 0;
00327 }
00328
00331 static int rm_read_index(AVFormatContext *s)
00332 {
00333 AVIOContext *pb = s->pb;
00334 unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
00335 AVStream *st;
00336
00337 do {
00338 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
00339 return -1;
00340 size = avio_rb32(pb);
00341 if (size < 20)
00342 return -1;
00343 avio_seek(pb, 2, SEEK_CUR);
00344 n_pkts = avio_rb32(pb);
00345 str_id = avio_rb16(pb);
00346 next_off = avio_rb32(pb);
00347 for (n = 0; n < s->nb_streams; n++)
00348 if (s->streams[n]->id == str_id) {
00349 st = s->streams[n];
00350 break;
00351 }
00352 if (n == s->nb_streams)
00353 goto skip;
00354
00355 for (n = 0; n < n_pkts; n++) {
00356 avio_seek(pb, 2, SEEK_CUR);
00357 pts = avio_rb32(pb);
00358 pos = avio_rb32(pb);
00359 avio_seek(pb, 4, SEEK_CUR);
00360
00361 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
00362 }
00363
00364 skip:
00365 if (next_off && avio_tell(pb) != next_off &&
00366 avio_seek(pb, next_off, SEEK_SET) < 0)
00367 return -1;
00368 } while (next_off);
00369
00370 return 0;
00371 }
00372
00373 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
00374 {
00375 RMDemuxContext *rm = s->priv_data;
00376 AVStream *st;
00377
00378 rm->old_format = 1;
00379 st = av_new_stream(s, 0);
00380 if (!st)
00381 return -1;
00382 st->priv_data = ff_rm_alloc_rmstream();
00383 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
00384 }
00385
00386 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
00387 {
00388 RMDemuxContext *rm = s->priv_data;
00389 AVStream *st;
00390 AVIOContext *pb = s->pb;
00391 unsigned int tag;
00392 int tag_size;
00393 unsigned int start_time, duration;
00394 unsigned int data_off = 0, indx_off = 0;
00395 char buf[128];
00396 int flags = 0;
00397
00398 tag = avio_rl32(pb);
00399 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
00400
00401 return rm_read_header_old(s, ap);
00402 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
00403 return AVERROR(EIO);
00404 }
00405
00406 avio_rb32(pb);
00407 avio_rb16(pb);
00408 avio_rb32(pb);
00409 avio_rb32(pb);
00410
00411 for(;;) {
00412 if (url_feof(pb))
00413 return -1;
00414 tag = avio_rl32(pb);
00415 tag_size = avio_rb32(pb);
00416 avio_rb16(pb);
00417 #if 0
00418 printf("tag=%c%c%c%c (%08x) size=%d\n",
00419 (tag) & 0xff,
00420 (tag >> 8) & 0xff,
00421 (tag >> 16) & 0xff,
00422 (tag >> 24) & 0xff,
00423 tag,
00424 tag_size);
00425 #endif
00426 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
00427 return -1;
00428 switch(tag) {
00429 case MKTAG('P', 'R', 'O', 'P'):
00430
00431 avio_rb32(pb);
00432 avio_rb32(pb);
00433 avio_rb32(pb);
00434 avio_rb32(pb);
00435 avio_rb32(pb);
00436 avio_rb32(pb);
00437 avio_rb32(pb);
00438 indx_off = avio_rb32(pb);
00439 data_off = avio_rb32(pb);
00440 avio_rb16(pb);
00441 flags = avio_rb16(pb);
00442 break;
00443 case MKTAG('C', 'O', 'N', 'T'):
00444 rm_read_metadata(s, 1);
00445 break;
00446 case MKTAG('M', 'D', 'P', 'R'):
00447 st = av_new_stream(s, 0);
00448 if (!st)
00449 return AVERROR(ENOMEM);
00450 st->id = avio_rb16(pb);
00451 avio_rb32(pb);
00452 st->codec->bit_rate = avio_rb32(pb);
00453 avio_rb32(pb);
00454 avio_rb32(pb);
00455 start_time = avio_rb32(pb);
00456 avio_rb32(pb);
00457 duration = avio_rb32(pb);
00458 st->start_time = start_time;
00459 st->duration = duration;
00460 get_str8(pb, buf, sizeof(buf));
00461 get_str8(pb, buf, sizeof(buf));
00462 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00463 st->priv_data = ff_rm_alloc_rmstream();
00464 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
00465 avio_rb32(pb)) < 0)
00466 return -1;
00467 break;
00468 case MKTAG('D', 'A', 'T', 'A'):
00469 goto header_end;
00470 default:
00471
00472 avio_seek(pb, tag_size - 10, SEEK_CUR);
00473 break;
00474 }
00475 }
00476 header_end:
00477 rm->nb_packets = avio_rb32(pb);
00478 if (!rm->nb_packets && (flags & 4))
00479 rm->nb_packets = 3600 * 25;
00480 avio_rb32(pb);
00481
00482 if (!data_off)
00483 data_off = avio_tell(pb) - 18;
00484 if (indx_off && !url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX) &&
00485 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
00486 rm_read_index(s);
00487 avio_seek(pb, data_off + 18, SEEK_SET);
00488 }
00489
00490 return 0;
00491 }
00492
00493 static int get_num(AVIOContext *pb, int *len)
00494 {
00495 int n, n1;
00496
00497 n = avio_rb16(pb);
00498 (*len)-=2;
00499 n &= 0x7FFF;
00500 if (n >= 0x4000) {
00501 return n - 0x4000;
00502 } else {
00503 n1 = avio_rb16(pb);
00504 (*len)-=2;
00505 return (n << 16) | n1;
00506 }
00507 }
00508
00509
00510 #define RAW_PACKET_SIZE 1000
00511
00512 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
00513 RMDemuxContext *rm = s->priv_data;
00514 AVIOContext *pb = s->pb;
00515 AVStream *st;
00516 uint32_t state=0xFFFFFFFF;
00517
00518 while(!url_feof(pb)){
00519 int len, num, i;
00520 *pos= avio_tell(pb) - 3;
00521 if(rm->remaining_len > 0){
00522 num= rm->current_stream;
00523 len= rm->remaining_len;
00524 *timestamp = AV_NOPTS_VALUE;
00525 *flags= 0;
00526 }else{
00527 state= (state<<8) + avio_r8(pb);
00528
00529 if(state == MKBETAG('I', 'N', 'D', 'X')){
00530 int n_pkts, expected_len;
00531 len = avio_rb32(pb);
00532 avio_seek(pb, 2, SEEK_CUR);
00533 n_pkts = avio_rb32(pb);
00534 expected_len = 20 + n_pkts * 14;
00535 if (len == 20)
00536
00537 len = expected_len;
00538 else if (len != expected_len)
00539 av_log(s, AV_LOG_WARNING,
00540 "Index size %d (%d pkts) is wrong, should be %d.\n",
00541 len, n_pkts, expected_len);
00542 len -= 14;
00543 if(len<0)
00544 continue;
00545 goto skip;
00546 } else if (state == MKBETAG('D','A','T','A')) {
00547 av_log(s, AV_LOG_WARNING,
00548 "DATA tag in middle of chunk, file may be broken.\n");
00549 }
00550
00551 if(state > (unsigned)0xFFFF || state <= 12)
00552 continue;
00553 len=state - 12;
00554 state= 0xFFFFFFFF;
00555
00556 num = avio_rb16(pb);
00557 *timestamp = avio_rb32(pb);
00558 avio_r8(pb);
00559 *flags = avio_r8(pb);
00560 }
00561 for(i=0;i<s->nb_streams;i++) {
00562 st = s->streams[i];
00563 if (num == st->id)
00564 break;
00565 }
00566 if (i == s->nb_streams) {
00567 skip:
00568
00569 avio_seek(pb, len, SEEK_CUR);
00570 rm->remaining_len = 0;
00571 continue;
00572 }
00573 *stream_index= i;
00574
00575 return len;
00576 }
00577 return -1;
00578 }
00579
00580 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
00581 RMDemuxContext *rm, RMStream *vst,
00582 AVPacket *pkt, int len, int *pseq)
00583 {
00584 int hdr, seq, pic_num, len2, pos;
00585 int type;
00586
00587 hdr = avio_r8(pb); len--;
00588 type = hdr >> 6;
00589
00590 if(type != 3){
00591 seq = avio_r8(pb); len--;
00592 }
00593 if(type != 1){
00594 len2 = get_num(pb, &len);
00595 pos = get_num(pb, &len);
00596 pic_num = avio_r8(pb); len--;
00597 }
00598 if(len<0)
00599 return -1;
00600 rm->remaining_len = len;
00601 if(type&1){
00602 if(type == 3)
00603 len= len2;
00604 if(rm->remaining_len < len)
00605 return -1;
00606 rm->remaining_len -= len;
00607 if(av_new_packet(pkt, len + 9) < 0)
00608 return AVERROR(EIO);
00609 pkt->data[0] = 0;
00610 AV_WL32(pkt->data + 1, 1);
00611 AV_WL32(pkt->data + 5, 0);
00612 avio_read(pb, pkt->data + 9, len);
00613 return 0;
00614 }
00615
00616
00617 *pseq = seq;
00618 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
00619 vst->slices = ((hdr & 0x3F) << 1) + 1;
00620 vst->videobufsize = len2 + 8*vst->slices + 1;
00621 av_free_packet(&vst->pkt);
00622 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
00623 return AVERROR(ENOMEM);
00624 vst->videobufpos = 8*vst->slices + 1;
00625 vst->cur_slice = 0;
00626 vst->curpic_num = pic_num;
00627 vst->pktpos = avio_tell(pb);
00628 }
00629 if(type == 2)
00630 len = FFMIN(len, pos);
00631
00632 if(++vst->cur_slice > vst->slices)
00633 return 1;
00634 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
00635 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
00636 if(vst->videobufpos + len > vst->videobufsize)
00637 return 1;
00638 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
00639 return AVERROR(EIO);
00640 vst->videobufpos += len;
00641 rm->remaining_len-= len;
00642
00643 if(type == 2 || (vst->videobufpos) == vst->videobufsize){
00644 vst->pkt.data[0] = vst->cur_slice-1;
00645 *pkt= vst->pkt;
00646 vst->pkt.data= NULL;
00647 vst->pkt.size= 0;
00648 if(vst->slices != vst->cur_slice)
00649 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
00650 vst->videobufpos - 1 - 8*vst->slices);
00651 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
00652 pkt->pts = AV_NOPTS_VALUE;
00653 pkt->pos = vst->pktpos;
00654 vst->slices = 0;
00655 return 0;
00656 }
00657
00658 return 1;
00659 }
00660
00661 static inline void
00662 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
00663 {
00664 uint8_t *ptr;
00665 int j;
00666
00667 if (st->codec->codec_id == CODEC_ID_AC3) {
00668 ptr = pkt->data;
00669 for (j=0;j<pkt->size;j+=2) {
00670 FFSWAP(int, ptr[0], ptr[1]);
00671 ptr += 2;
00672 }
00673 }
00674 }
00675
00680 void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
00681 {
00682 int n, bs = sub_packet_h * framesize * 2 / 96;
00683
00684 for (n = 0; n < 38; n++) {
00685 int j;
00686 int i = bs * sipr_swaps[n][0];
00687 int o = bs * sipr_swaps[n][1];
00688
00689
00690 for (j = 0; j < bs; j++, i++, o++) {
00691 int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
00692 y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
00693
00694 buf[o >> 1] = (x << (4 * (o & 1))) |
00695 (buf[o >> 1] & (0xF << (4 * !(o & 1))));
00696 buf[i >> 1] = (y << (4 * (i & 1))) |
00697 (buf[i >> 1] & (0xF << (4 * !(i & 1))));
00698 }
00699 }
00700 }
00701
00702 int
00703 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
00704 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
00705 int *seq, int flags, int64_t timestamp)
00706 {
00707 RMDemuxContext *rm = s->priv_data;
00708
00709 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00710 rm->current_stream= st->id;
00711 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq))
00712 return -1;
00713 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00714 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
00715 (st->codec->codec_id == CODEC_ID_COOK) ||
00716 (st->codec->codec_id == CODEC_ID_ATRAC3) ||
00717 (st->codec->codec_id == CODEC_ID_SIPR)) {
00718 int x;
00719 int sps = ast->sub_packet_size;
00720 int cfs = ast->coded_framesize;
00721 int h = ast->sub_packet_h;
00722 int y = ast->sub_packet_cnt;
00723 int w = ast->audio_framesize;
00724
00725 if (flags & 2)
00726 y = ast->sub_packet_cnt = 0;
00727 if (!y)
00728 ast->audiotimestamp = timestamp;
00729
00730 switch(st->codec->codec_id) {
00731 case CODEC_ID_RA_288:
00732 for (x = 0; x < h/2; x++)
00733 avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
00734 break;
00735 case CODEC_ID_ATRAC3:
00736 case CODEC_ID_COOK:
00737 for (x = 0; x < w/sps; x++)
00738 avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
00739 break;
00740 case CODEC_ID_SIPR:
00741 avio_read(pb, ast->pkt.data + y * w, w);
00742 break;
00743 }
00744
00745 if (++(ast->sub_packet_cnt) < h)
00746 return -1;
00747 if (st->codec->codec_id == CODEC_ID_SIPR)
00748 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
00749
00750 ast->sub_packet_cnt = 0;
00751 rm->audio_stream_num = st->index;
00752 rm->audio_pkt_cnt = h * w / st->codec->block_align;
00753 } else if (st->codec->codec_id == CODEC_ID_AAC) {
00754 int x;
00755 rm->audio_stream_num = st->index;
00756 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
00757 if (ast->sub_packet_cnt) {
00758 for (x = 0; x < ast->sub_packet_cnt; x++)
00759 ast->sub_packet_lengths[x] = avio_rb16(pb);
00760 rm->audio_pkt_cnt = ast->sub_packet_cnt;
00761 ast->audiotimestamp = timestamp;
00762 } else
00763 return -1;
00764 } else {
00765 av_get_packet(pb, pkt, len);
00766 rm_ac3_swap_bytes(st, pkt);
00767 }
00768 } else
00769 av_get_packet(pb, pkt, len);
00770
00771 pkt->stream_index = st->index;
00772
00773 #if 0
00774 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00775 if(st->codec->codec_id == CODEC_ID_RV20){
00776 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
00777 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
00778
00779 seq |= (timestamp&~0x3FFF);
00780 if(seq - timestamp > 0x2000) seq -= 0x4000;
00781 if(seq - timestamp < -0x2000) seq += 0x4000;
00782 }
00783 }
00784 #endif
00785
00786 pkt->pts= timestamp;
00787 if (flags & 2)
00788 pkt->flags |= AV_PKT_FLAG_KEY;
00789
00790 return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
00791 }
00792
00793 int
00794 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
00795 AVStream *st, RMStream *ast, AVPacket *pkt)
00796 {
00797 RMDemuxContext *rm = s->priv_data;
00798
00799 assert (rm->audio_pkt_cnt > 0);
00800
00801 if (st->codec->codec_id == CODEC_ID_AAC)
00802 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
00803 else {
00804 av_new_packet(pkt, st->codec->block_align);
00805 memcpy(pkt->data, ast->pkt.data + st->codec->block_align *
00806 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
00807 st->codec->block_align);
00808 }
00809 rm->audio_pkt_cnt--;
00810 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
00811 ast->audiotimestamp = AV_NOPTS_VALUE;
00812 pkt->flags = AV_PKT_FLAG_KEY;
00813 } else
00814 pkt->flags = 0;
00815 pkt->stream_index = st->index;
00816
00817 return rm->audio_pkt_cnt;
00818 }
00819
00820 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
00821 {
00822 RMDemuxContext *rm = s->priv_data;
00823 AVStream *st;
00824 int i, len, res, seq = 1;
00825 int64_t timestamp, pos;
00826 int flags;
00827
00828 for (;;) {
00829 if (rm->audio_pkt_cnt) {
00830
00831 st = s->streams[rm->audio_stream_num];
00832 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
00833 flags = 0;
00834 } else {
00835 if (rm->old_format) {
00836 RMStream *ast;
00837
00838 st = s->streams[0];
00839 ast = st->priv_data;
00840 timestamp = AV_NOPTS_VALUE;
00841 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
00842 ast->coded_framesize * ast->sub_packet_h / 2;
00843 flags = (seq++ == 1) ? 2 : 0;
00844 pos = avio_tell(s->pb);
00845 } else {
00846 len=sync(s, ×tamp, &flags, &i, &pos);
00847 if (len > 0)
00848 st = s->streams[i];
00849 }
00850
00851 if(len<0 || url_feof(s->pb))
00852 return AVERROR(EIO);
00853
00854 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
00855 &seq, flags, timestamp);
00856 if((flags&2) && (seq&0x7F) == 1)
00857 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
00858 if (res)
00859 continue;
00860 }
00861
00862 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
00863 || st->discard >= AVDISCARD_ALL){
00864 av_free_packet(pkt);
00865 } else
00866 break;
00867 }
00868
00869 return 0;
00870 }
00871
00872 static int rm_read_close(AVFormatContext *s)
00873 {
00874 int i;
00875
00876 for (i=0;i<s->nb_streams;i++)
00877 ff_rm_free_rmstream(s->streams[i]->priv_data);
00878
00879 return 0;
00880 }
00881
00882 static int rm_probe(AVProbeData *p)
00883 {
00884
00885 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
00886 p->buf[2] == 'M' && p->buf[3] == 'F' &&
00887 p->buf[4] == 0 && p->buf[5] == 0) ||
00888 (p->buf[0] == '.' && p->buf[1] == 'r' &&
00889 p->buf[2] == 'a' && p->buf[3] == 0xfd))
00890 return AVPROBE_SCORE_MAX;
00891 else
00892 return 0;
00893 }
00894
00895 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
00896 int64_t *ppos, int64_t pos_limit)
00897 {
00898 RMDemuxContext *rm = s->priv_data;
00899 int64_t pos, dts;
00900 int stream_index2, flags, len, h;
00901
00902 pos = *ppos;
00903
00904 if(rm->old_format)
00905 return AV_NOPTS_VALUE;
00906
00907 avio_seek(s->pb, pos, SEEK_SET);
00908 rm->remaining_len=0;
00909 for(;;){
00910 int seq=1;
00911 AVStream *st;
00912
00913 len=sync(s, &dts, &flags, &stream_index2, &pos);
00914 if(len<0)
00915 return AV_NOPTS_VALUE;
00916
00917 st = s->streams[stream_index2];
00918 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00919 h= avio_r8(s->pb); len--;
00920 if(!(h & 0x40)){
00921 seq = avio_r8(s->pb); len--;
00922 }
00923 }
00924
00925 if((flags&2) && (seq&0x7F) == 1){
00926
00927 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
00928 if(stream_index2 == stream_index)
00929 break;
00930 }
00931
00932 avio_seek(s->pb, len, SEEK_CUR);
00933 }
00934 *ppos = pos;
00935 return dts;
00936 }
00937
00938 AVInputFormat ff_rm_demuxer = {
00939 "rm",
00940 NULL_IF_CONFIG_SMALL("RealMedia format"),
00941 sizeof(RMDemuxContext),
00942 rm_probe,
00943 rm_read_header,
00944 rm_read_packet,
00945 rm_read_close,
00946 NULL,
00947 rm_read_dts,
00948 };
00949
00950 AVInputFormat ff_rdt_demuxer = {
00951 "rdt",
00952 NULL_IF_CONFIG_SMALL("RDT demuxer"),
00953 sizeof(RMDemuxContext),
00954 NULL,
00955 NULL,
00956 NULL,
00957 rm_read_close,
00958 };