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 sub_packet_h <= 1 ||
00270 ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
00271 return AVERROR_INVALIDDATA;
00272 break;
00273 case DEINT_ID_GENR:
00274 if (ast->sub_packet_size <= 0 ||
00275 ast->sub_packet_size > ast->audio_framesize)
00276 return AVERROR_INVALIDDATA;
00277 break;
00278 case DEINT_ID_SIPR:
00279 case DEINT_ID_INT0:
00280 case DEINT_ID_VBRS:
00281 case DEINT_ID_VBRF:
00282 break;
00283 default:
00284 av_log(NULL,0,"Unknown interleaver %X\n", ast->deint_id);
00285 return AVERROR_INVALIDDATA;
00286 }
00287
00288 if (read_all) {
00289 avio_r8(pb);
00290 avio_r8(pb);
00291 avio_r8(pb);
00292 rm_read_metadata(s, 0);
00293 }
00294 }
00295 return 0;
00296 }
00297
00298 int
00299 ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
00300 AVStream *st, RMStream *rst, int codec_data_size)
00301 {
00302 unsigned int v;
00303 int size;
00304 int64_t codec_pos;
00305 int ret;
00306
00307 avpriv_set_pts_info(st, 64, 1, 1000);
00308 codec_pos = avio_tell(pb);
00309 v = avio_rb32(pb);
00310 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
00311
00312 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
00313 return -1;
00314 } else {
00315 int fps;
00316 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
00317 fail1:
00318 av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
00319 goto skip;
00320 }
00321 st->codec->codec_tag = avio_rl32(pb);
00322 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
00323 st->codec->codec_tag);
00324
00325 if (st->codec->codec_id == CODEC_ID_NONE)
00326 goto fail1;
00327 st->codec->width = avio_rb16(pb);
00328 st->codec->height = avio_rb16(pb);
00329 avio_skip(pb, 2);
00330 avio_skip(pb, 4);
00331 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00332 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
00333 fps = avio_rb32(pb);
00334
00335 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
00336 return ret;
00337
00338 av_reduce(&st->r_frame_rate.den, &st->r_frame_rate.num,
00339 0x10000, fps, (1 << 30) - 1);
00340 st->avg_frame_rate = st->r_frame_rate;
00341 }
00342
00343 skip:
00344
00345 size = avio_tell(pb) - codec_pos;
00346 avio_skip(pb, codec_data_size - size);
00347
00348 return 0;
00349 }
00350
00353 static int rm_read_index(AVFormatContext *s)
00354 {
00355 AVIOContext *pb = s->pb;
00356 unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
00357 AVStream *st;
00358
00359 do {
00360 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
00361 return -1;
00362 size = avio_rb32(pb);
00363 if (size < 20)
00364 return -1;
00365 avio_skip(pb, 2);
00366 n_pkts = avio_rb32(pb);
00367 str_id = avio_rb16(pb);
00368 next_off = avio_rb32(pb);
00369 for (n = 0; n < s->nb_streams; n++)
00370 if (s->streams[n]->id == str_id) {
00371 st = s->streams[n];
00372 break;
00373 }
00374 if (n == s->nb_streams) {
00375 av_log(s, AV_LOG_ERROR,
00376 "Invalid stream index %d for index at pos %"PRId64"\n",
00377 str_id, avio_tell(pb));
00378 goto skip;
00379 } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
00380 av_log(s, AV_LOG_ERROR,
00381 "Nr. of packets in packet index for stream index %d "
00382 "exceeds filesize (%"PRId64" at %"PRId64" = %d)\n",
00383 str_id, avio_size(pb), avio_tell(pb),
00384 (avio_size(pb) - avio_tell(pb)) / 14);
00385 goto skip;
00386 }
00387
00388 for (n = 0; n < n_pkts; n++) {
00389 avio_skip(pb, 2);
00390 pts = avio_rb32(pb);
00391 pos = avio_rb32(pb);
00392 avio_skip(pb, 4);
00393
00394 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
00395 }
00396
00397 skip:
00398 if (next_off && avio_tell(pb) < next_off &&
00399 avio_seek(pb, next_off, SEEK_SET) < 0) {
00400 av_log(s, AV_LOG_ERROR,
00401 "Non-linear index detected, not supported\n");
00402 return -1;
00403 }
00404 } while (next_off);
00405
00406 return 0;
00407 }
00408
00409 static int rm_read_header_old(AVFormatContext *s)
00410 {
00411 RMDemuxContext *rm = s->priv_data;
00412 AVStream *st;
00413
00414 rm->old_format = 1;
00415 st = avformat_new_stream(s, NULL);
00416 if (!st)
00417 return -1;
00418 st->priv_data = ff_rm_alloc_rmstream();
00419 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
00420 }
00421
00422 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
00423 {
00424 RMDemuxContext *rm = s->priv_data;
00425 AVStream *st;
00426 AVIOContext *pb = s->pb;
00427 unsigned int tag;
00428 int tag_size;
00429 unsigned int start_time, duration;
00430 unsigned int data_off = 0, indx_off = 0;
00431 char buf[128];
00432 int flags = 0;
00433
00434 tag = avio_rl32(pb);
00435 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
00436
00437 return rm_read_header_old(s);
00438 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
00439 return AVERROR(EIO);
00440 }
00441
00442 avio_rb32(pb);
00443 avio_rb16(pb);
00444 avio_rb32(pb);
00445 avio_rb32(pb);
00446
00447 for(;;) {
00448 if (url_feof(pb))
00449 return -1;
00450 tag = avio_rl32(pb);
00451 tag_size = avio_rb32(pb);
00452 avio_rb16(pb);
00453 av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
00454 (tag ) & 0xff,
00455 (tag >> 8) & 0xff,
00456 (tag >> 16) & 0xff,
00457 (tag >> 24) & 0xff,
00458 tag,
00459 tag_size);
00460 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
00461 return -1;
00462 switch(tag) {
00463 case MKTAG('P', 'R', 'O', 'P'):
00464
00465 avio_rb32(pb);
00466 avio_rb32(pb);
00467 avio_rb32(pb);
00468 avio_rb32(pb);
00469 avio_rb32(pb);
00470 avio_rb32(pb);
00471 avio_rb32(pb);
00472 indx_off = avio_rb32(pb);
00473 data_off = avio_rb32(pb);
00474 avio_rb16(pb);
00475 flags = avio_rb16(pb);
00476 break;
00477 case MKTAG('C', 'O', 'N', 'T'):
00478 rm_read_metadata(s, 1);
00479 break;
00480 case MKTAG('M', 'D', 'P', 'R'):
00481 st = avformat_new_stream(s, NULL);
00482 if (!st)
00483 return AVERROR(ENOMEM);
00484 st->id = avio_rb16(pb);
00485 avio_rb32(pb);
00486 st->codec->bit_rate = avio_rb32(pb);
00487 avio_rb32(pb);
00488 avio_rb32(pb);
00489 start_time = avio_rb32(pb);
00490 avio_rb32(pb);
00491 duration = avio_rb32(pb);
00492 st->start_time = start_time;
00493 st->duration = duration;
00494 get_str8(pb, buf, sizeof(buf));
00495 get_str8(pb, buf, sizeof(buf));
00496 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00497 st->priv_data = ff_rm_alloc_rmstream();
00498 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
00499 avio_rb32(pb)) < 0)
00500 return -1;
00501 break;
00502 case MKTAG('D', 'A', 'T', 'A'):
00503 goto header_end;
00504 default:
00505
00506 avio_skip(pb, tag_size - 10);
00507 break;
00508 }
00509 }
00510 header_end:
00511 rm->nb_packets = avio_rb32(pb);
00512 if (!rm->nb_packets && (flags & 4))
00513 rm->nb_packets = 3600 * 25;
00514 avio_rb32(pb);
00515
00516 if (!data_off)
00517 data_off = avio_tell(pb) - 18;
00518 if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
00519 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
00520 rm_read_index(s);
00521 avio_seek(pb, data_off + 18, SEEK_SET);
00522 }
00523
00524 return 0;
00525 }
00526
00527 static int get_num(AVIOContext *pb, int *len)
00528 {
00529 int n, n1;
00530
00531 n = avio_rb16(pb);
00532 (*len)-=2;
00533 n &= 0x7FFF;
00534 if (n >= 0x4000) {
00535 return n - 0x4000;
00536 } else {
00537 n1 = avio_rb16(pb);
00538 (*len)-=2;
00539 return (n << 16) | n1;
00540 }
00541 }
00542
00543
00544 #define RAW_PACKET_SIZE 1000
00545
00546 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
00547 RMDemuxContext *rm = s->priv_data;
00548 AVIOContext *pb = s->pb;
00549 AVStream *st;
00550 uint32_t state=0xFFFFFFFF;
00551
00552 while(!url_feof(pb)){
00553 int len, num, i;
00554 *pos= avio_tell(pb) - 3;
00555 if(rm->remaining_len > 0){
00556 num= rm->current_stream;
00557 len= rm->remaining_len;
00558 *timestamp = AV_NOPTS_VALUE;
00559 *flags= 0;
00560 }else{
00561 state= (state<<8) + avio_r8(pb);
00562
00563 if(state == MKBETAG('I', 'N', 'D', 'X')){
00564 int n_pkts, expected_len;
00565 len = avio_rb32(pb);
00566 avio_skip(pb, 2);
00567 n_pkts = avio_rb32(pb);
00568 expected_len = 20 + n_pkts * 14;
00569 if (len == 20)
00570
00571 len = expected_len;
00572 else if (len != expected_len)
00573 av_log(s, AV_LOG_WARNING,
00574 "Index size %d (%d pkts) is wrong, should be %d.\n",
00575 len, n_pkts, expected_len);
00576 len -= 14;
00577 if(len<0)
00578 continue;
00579 goto skip;
00580 } else if (state == MKBETAG('D','A','T','A')) {
00581 av_log(s, AV_LOG_WARNING,
00582 "DATA tag in middle of chunk, file may be broken.\n");
00583 }
00584
00585 if(state > (unsigned)0xFFFF || state <= 12)
00586 continue;
00587 len=state - 12;
00588 state= 0xFFFFFFFF;
00589
00590 num = avio_rb16(pb);
00591 *timestamp = avio_rb32(pb);
00592 avio_r8(pb);
00593 *flags = avio_r8(pb);
00594 }
00595 for(i=0;i<s->nb_streams;i++) {
00596 st = s->streams[i];
00597 if (num == st->id)
00598 break;
00599 }
00600 if (i == s->nb_streams) {
00601 skip:
00602
00603 avio_skip(pb, len);
00604 rm->remaining_len = 0;
00605 continue;
00606 }
00607 *stream_index= i;
00608
00609 return len;
00610 }
00611 return -1;
00612 }
00613
00614 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
00615 RMDemuxContext *rm, RMStream *vst,
00616 AVPacket *pkt, int len, int *pseq,
00617 int64_t *timestamp)
00618 {
00619 int hdr, seq, pic_num, len2, pos;
00620 int type;
00621
00622 hdr = avio_r8(pb); len--;
00623 type = hdr >> 6;
00624
00625 if(type != 3){
00626 seq = avio_r8(pb); len--;
00627 }
00628 if(type != 1){
00629 len2 = get_num(pb, &len);
00630 pos = get_num(pb, &len);
00631 pic_num = avio_r8(pb); len--;
00632 }
00633 if(len<0)
00634 return -1;
00635 rm->remaining_len = len;
00636 if(type&1){
00637 if(type == 3){
00638 len= len2;
00639 *timestamp = pos;
00640 }
00641 if(rm->remaining_len < len)
00642 return -1;
00643 rm->remaining_len -= len;
00644 if(av_new_packet(pkt, len + 9) < 0)
00645 return AVERROR(EIO);
00646 pkt->data[0] = 0;
00647 AV_WL32(pkt->data + 1, 1);
00648 AV_WL32(pkt->data + 5, 0);
00649 avio_read(pb, pkt->data + 9, len);
00650 return 0;
00651 }
00652
00653
00654 *pseq = seq;
00655 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
00656 vst->slices = ((hdr & 0x3F) << 1) + 1;
00657 vst->videobufsize = len2 + 8*vst->slices + 1;
00658 av_free_packet(&vst->pkt);
00659 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
00660 return AVERROR(ENOMEM);
00661 vst->videobufpos = 8*vst->slices + 1;
00662 vst->cur_slice = 0;
00663 vst->curpic_num = pic_num;
00664 vst->pktpos = avio_tell(pb);
00665 }
00666 if(type == 2)
00667 len = FFMIN(len, pos);
00668
00669 if(++vst->cur_slice > vst->slices)
00670 return 1;
00671 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
00672 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
00673 if(vst->videobufpos + len > vst->videobufsize)
00674 return 1;
00675 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
00676 return AVERROR(EIO);
00677 vst->videobufpos += len;
00678 rm->remaining_len-= len;
00679
00680 if (type == 2 || vst->videobufpos == vst->videobufsize) {
00681 vst->pkt.data[0] = vst->cur_slice-1;
00682 *pkt= vst->pkt;
00683 vst->pkt.data= NULL;
00684 vst->pkt.size= 0;
00685 if(vst->slices != vst->cur_slice)
00686 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
00687 vst->videobufpos - 1 - 8*vst->slices);
00688 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
00689 pkt->pts = AV_NOPTS_VALUE;
00690 pkt->pos = vst->pktpos;
00691 vst->slices = 0;
00692 return 0;
00693 }
00694
00695 return 1;
00696 }
00697
00698 static inline void
00699 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
00700 {
00701 uint8_t *ptr;
00702 int j;
00703
00704 if (st->codec->codec_id == CODEC_ID_AC3) {
00705 ptr = pkt->data;
00706 for (j=0;j<pkt->size;j+=2) {
00707 FFSWAP(int, ptr[0], ptr[1]);
00708 ptr += 2;
00709 }
00710 }
00711 }
00712
00717 void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
00718 {
00719 int n, bs = sub_packet_h * framesize * 2 / 96;
00720
00721 for (n = 0; n < 38; n++) {
00722 int j;
00723 int i = bs * sipr_swaps[n][0];
00724 int o = bs * sipr_swaps[n][1];
00725
00726
00727 for (j = 0; j < bs; j++, i++, o++) {
00728 int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
00729 y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
00730
00731 buf[o >> 1] = (x << (4 * (o & 1))) |
00732 (buf[o >> 1] & (0xF << (4 * !(o & 1))));
00733 buf[i >> 1] = (y << (4 * (i & 1))) |
00734 (buf[i >> 1] & (0xF << (4 * !(i & 1))));
00735 }
00736 }
00737 }
00738
00739 int
00740 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
00741 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
00742 int *seq, int flags, int64_t timestamp)
00743 {
00744 RMDemuxContext *rm = s->priv_data;
00745
00746 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00747 rm->current_stream= st->id;
00748 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, ×tamp))
00749 return -1;
00750 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00751 if ((ast->deint_id == DEINT_ID_GENR) ||
00752 (ast->deint_id == DEINT_ID_INT4) ||
00753 (ast->deint_id == DEINT_ID_SIPR)) {
00754 int x;
00755 int sps = ast->sub_packet_size;
00756 int cfs = ast->coded_framesize;
00757 int h = ast->sub_packet_h;
00758 int y = ast->sub_packet_cnt;
00759 int w = ast->audio_framesize;
00760
00761 if (flags & 2)
00762 y = ast->sub_packet_cnt = 0;
00763 if (!y)
00764 ast->audiotimestamp = timestamp;
00765
00766 switch (ast->deint_id) {
00767 case DEINT_ID_INT4:
00768 for (x = 0; x < h/2; x++)
00769 avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
00770 break;
00771 case DEINT_ID_GENR:
00772 for (x = 0; x < w/sps; x++)
00773 avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
00774 break;
00775 case DEINT_ID_SIPR:
00776 avio_read(pb, ast->pkt.data + y * w, w);
00777 break;
00778 }
00779
00780 if (++(ast->sub_packet_cnt) < h)
00781 return -1;
00782 if (ast->deint_id == DEINT_ID_SIPR)
00783 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
00784
00785 ast->sub_packet_cnt = 0;
00786 rm->audio_stream_num = st->index;
00787 rm->audio_pkt_cnt = h * w / st->codec->block_align;
00788 } else if ((ast->deint_id == DEINT_ID_VBRF) ||
00789 (ast->deint_id == DEINT_ID_VBRS)) {
00790 int x;
00791 rm->audio_stream_num = st->index;
00792 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
00793 if (ast->sub_packet_cnt) {
00794 for (x = 0; x < ast->sub_packet_cnt; x++)
00795 ast->sub_packet_lengths[x] = avio_rb16(pb);
00796 rm->audio_pkt_cnt = ast->sub_packet_cnt;
00797 ast->audiotimestamp = timestamp;
00798 } else
00799 return -1;
00800 } else {
00801 av_get_packet(pb, pkt, len);
00802 rm_ac3_swap_bytes(st, pkt);
00803 }
00804 } else
00805 av_get_packet(pb, pkt, len);
00806
00807 pkt->stream_index = st->index;
00808
00809 #if 0
00810 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00811 if(st->codec->codec_id == CODEC_ID_RV20){
00812 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
00813 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
00814
00815 seq |= (timestamp&~0x3FFF);
00816 if(seq - timestamp > 0x2000) seq -= 0x4000;
00817 if(seq - timestamp < -0x2000) seq += 0x4000;
00818 }
00819 }
00820 #endif
00821
00822 pkt->pts = timestamp;
00823 if (flags & 2)
00824 pkt->flags |= AV_PKT_FLAG_KEY;
00825
00826 return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
00827 }
00828
00829 int
00830 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
00831 AVStream *st, RMStream *ast, AVPacket *pkt)
00832 {
00833 RMDemuxContext *rm = s->priv_data;
00834
00835 assert (rm->audio_pkt_cnt > 0);
00836
00837 if (ast->deint_id == DEINT_ID_VBRF ||
00838 ast->deint_id == DEINT_ID_VBRS)
00839 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
00840 else {
00841 av_new_packet(pkt, st->codec->block_align);
00842 memcpy(pkt->data, ast->pkt.data + st->codec->block_align *
00843 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
00844 st->codec->block_align);
00845 }
00846 rm->audio_pkt_cnt--;
00847 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
00848 ast->audiotimestamp = AV_NOPTS_VALUE;
00849 pkt->flags = AV_PKT_FLAG_KEY;
00850 } else
00851 pkt->flags = 0;
00852 pkt->stream_index = st->index;
00853
00854 return rm->audio_pkt_cnt;
00855 }
00856
00857 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
00858 {
00859 RMDemuxContext *rm = s->priv_data;
00860 AVStream *st;
00861 int i, len, res, seq = 1;
00862 int64_t timestamp, pos;
00863 int flags;
00864
00865 for (;;) {
00866 if (rm->audio_pkt_cnt) {
00867
00868 st = s->streams[rm->audio_stream_num];
00869 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
00870 flags = 0;
00871 } else {
00872 if (rm->old_format) {
00873 RMStream *ast;
00874
00875 st = s->streams[0];
00876 ast = st->priv_data;
00877 timestamp = AV_NOPTS_VALUE;
00878 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
00879 ast->coded_framesize * ast->sub_packet_h / 2;
00880 flags = (seq++ == 1) ? 2 : 0;
00881 pos = avio_tell(s->pb);
00882 } else {
00883 len=sync(s, ×tamp, &flags, &i, &pos);
00884 if (len > 0)
00885 st = s->streams[i];
00886 }
00887
00888 if(len<0 || url_feof(s->pb))
00889 return AVERROR(EIO);
00890
00891 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
00892 &seq, flags, timestamp);
00893 if((flags&2) && (seq&0x7F) == 1)
00894 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
00895 if (res)
00896 continue;
00897 }
00898
00899 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
00900 || st->discard >= AVDISCARD_ALL){
00901 av_free_packet(pkt);
00902 } else
00903 break;
00904 }
00905
00906 return 0;
00907 }
00908
00909 static int rm_read_close(AVFormatContext *s)
00910 {
00911 int i;
00912
00913 for (i=0;i<s->nb_streams;i++)
00914 ff_rm_free_rmstream(s->streams[i]->priv_data);
00915
00916 return 0;
00917 }
00918
00919 static int rm_probe(AVProbeData *p)
00920 {
00921
00922 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
00923 p->buf[2] == 'M' && p->buf[3] == 'F' &&
00924 p->buf[4] == 0 && p->buf[5] == 0) ||
00925 (p->buf[0] == '.' && p->buf[1] == 'r' &&
00926 p->buf[2] == 'a' && p->buf[3] == 0xfd))
00927 return AVPROBE_SCORE_MAX;
00928 else
00929 return 0;
00930 }
00931
00932 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
00933 int64_t *ppos, int64_t pos_limit)
00934 {
00935 RMDemuxContext *rm = s->priv_data;
00936 int64_t pos, dts;
00937 int stream_index2, flags, len, h;
00938
00939 pos = *ppos;
00940
00941 if(rm->old_format)
00942 return AV_NOPTS_VALUE;
00943
00944 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
00945 return AV_NOPTS_VALUE;
00946
00947 rm->remaining_len=0;
00948 for(;;){
00949 int seq=1;
00950 AVStream *st;
00951
00952 len=sync(s, &dts, &flags, &stream_index2, &pos);
00953 if(len<0)
00954 return AV_NOPTS_VALUE;
00955
00956 st = s->streams[stream_index2];
00957 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00958 h= avio_r8(s->pb); len--;
00959 if(!(h & 0x40)){
00960 seq = avio_r8(s->pb); len--;
00961 }
00962 }
00963
00964 if((flags&2) && (seq&0x7F) == 1){
00965
00966 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
00967 if(stream_index2 == stream_index)
00968 break;
00969 }
00970
00971 avio_skip(s->pb, len);
00972 }
00973 *ppos = pos;
00974 return dts;
00975 }
00976
00977 AVInputFormat ff_rm_demuxer = {
00978 .name = "rm",
00979 .long_name = NULL_IF_CONFIG_SMALL("RealMedia format"),
00980 .priv_data_size = sizeof(RMDemuxContext),
00981 .read_probe = rm_probe,
00982 .read_header = rm_read_header,
00983 .read_packet = rm_read_packet,
00984 .read_close = rm_read_close,
00985 .read_timestamp = rm_read_dts,
00986 };
00987
00988 AVInputFormat ff_rdt_demuxer = {
00989 .name = "rdt",
00990 .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
00991 .priv_data_size = sizeof(RMDemuxContext),
00992 .read_close = rm_read_close,
00993 .flags = AVFMT_NOFILE,
00994 };