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