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