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