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