00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "ffm.h"
00025 #if CONFIG_FFSERVER
00026 #include <unistd.h>
00027
00028 int64_t ffm_read_write_index(int fd)
00029 {
00030 uint8_t buf[8];
00031
00032 lseek(fd, 8, SEEK_SET);
00033 if (read(fd, buf, 8) != 8)
00034 return AVERROR(EIO);
00035 return AV_RB64(buf);
00036 }
00037
00038 int ffm_write_write_index(int fd, int64_t pos)
00039 {
00040 uint8_t buf[8];
00041 int i;
00042
00043 for(i=0;i<8;i++)
00044 buf[i] = (pos >> (56 - i * 8)) & 0xff;
00045 lseek(fd, 8, SEEK_SET);
00046 if (write(fd, buf, 8) != 8)
00047 return AVERROR(EIO);
00048 return 8;
00049 }
00050
00051 void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
00052 {
00053 FFMContext *ffm = s->priv_data;
00054 ffm->write_index = pos;
00055 ffm->file_size = file_size;
00056 }
00057 #endif // CONFIG_FFSERVER
00058
00059 static int ffm_is_avail_data(AVFormatContext *s, int size)
00060 {
00061 FFMContext *ffm = s->priv_data;
00062 int64_t pos, avail_size;
00063 int len;
00064
00065 len = ffm->packet_end - ffm->packet_ptr;
00066 if (size <= len)
00067 return 1;
00068 pos = avio_tell(s->pb);
00069 if (!ffm->write_index) {
00070 if (pos == ffm->file_size)
00071 return AVERROR_EOF;
00072 avail_size = ffm->file_size - pos;
00073 } else {
00074 if (pos == ffm->write_index) {
00075
00076 return AVERROR(EAGAIN);
00077 } else if (pos < ffm->write_index) {
00078 avail_size = ffm->write_index - pos;
00079 } else {
00080 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
00081 }
00082 }
00083 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
00084 if (size <= avail_size)
00085 return 1;
00086 else
00087 return AVERROR(EAGAIN);
00088 }
00089
00090 static int ffm_resync(AVFormatContext *s, int state)
00091 {
00092 av_log(s, AV_LOG_ERROR, "resyncing\n");
00093 while (state != PACKET_ID) {
00094 if (url_feof(s->pb)) {
00095 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
00096 return -1;
00097 }
00098 state = (state << 8) | avio_r8(s->pb);
00099 }
00100 return 0;
00101 }
00102
00103
00104 static int ffm_read_data(AVFormatContext *s,
00105 uint8_t *buf, int size, int header)
00106 {
00107 FFMContext *ffm = s->priv_data;
00108 AVIOContext *pb = s->pb;
00109 int len, fill_size, size1, frame_offset, id;
00110
00111 size1 = size;
00112 while (size > 0) {
00113 redo:
00114 len = ffm->packet_end - ffm->packet_ptr;
00115 if (len < 0)
00116 return -1;
00117 if (len > size)
00118 len = size;
00119 if (len == 0) {
00120 if (avio_tell(pb) == ffm->file_size)
00121 avio_seek(pb, ffm->packet_size, SEEK_SET);
00122 retry_read:
00123 id = avio_rb16(pb);
00124 if (id != PACKET_ID)
00125 if (ffm_resync(s, id) < 0)
00126 return -1;
00127 fill_size = avio_rb16(pb);
00128 ffm->dts = avio_rb64(pb);
00129 frame_offset = avio_rb16(pb);
00130 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
00131 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
00132 if (ffm->packet_end < ffm->packet || frame_offset < 0)
00133 return -1;
00134
00135
00136 if (ffm->first_packet || (frame_offset & 0x8000)) {
00137 if (!frame_offset) {
00138
00139 if (avio_tell(pb) >= ffm->packet_size * 3) {
00140 avio_seek(pb, -ffm->packet_size * 2, SEEK_CUR);
00141 goto retry_read;
00142 }
00143
00144 return 0;
00145 }
00146 ffm->first_packet = 0;
00147 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
00148 return -1;
00149 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
00150 if (!header)
00151 break;
00152 } else {
00153 ffm->packet_ptr = ffm->packet;
00154 }
00155 goto redo;
00156 }
00157 memcpy(buf, ffm->packet_ptr, len);
00158 buf += len;
00159 ffm->packet_ptr += len;
00160 size -= len;
00161 header = 0;
00162 }
00163 return size1 - size;
00164 }
00165
00166
00167
00168
00169
00170 static void ffm_seek1(AVFormatContext *s, int64_t pos1)
00171 {
00172 FFMContext *ffm = s->priv_data;
00173 AVIOContext *pb = s->pb;
00174 int64_t pos;
00175
00176 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
00177 pos = FFMAX(pos, FFM_PACKET_SIZE);
00178 #ifdef DEBUG_SEEK
00179 av_log(s, AV_LOG_DEBUG, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
00180 #endif
00181 avio_seek(pb, pos, SEEK_SET);
00182 }
00183
00184 static int64_t get_dts(AVFormatContext *s, int64_t pos)
00185 {
00186 AVIOContext *pb = s->pb;
00187 int64_t dts;
00188
00189 ffm_seek1(s, pos);
00190 avio_seek(pb, 4, SEEK_CUR);
00191 dts = avio_rb64(pb);
00192 #ifdef DEBUG_SEEK
00193 av_log(s, AV_LOG_DEBUG, "dts=%0.6f\n", dts / 1000000.0);
00194 #endif
00195 return dts;
00196 }
00197
00198 static void adjust_write_index(AVFormatContext *s)
00199 {
00200 FFMContext *ffm = s->priv_data;
00201 AVIOContext *pb = s->pb;
00202 int64_t pts;
00203
00204 int64_t pos_min, pos_max;
00205 int64_t pts_start;
00206 int64_t ptr = avio_tell(pb);
00207
00208
00209 pos_min = 0;
00210 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
00211
00212 pts_start = get_dts(s, pos_min);
00213
00214 pts = get_dts(s, pos_max);
00215
00216 if (pts - 100000 > pts_start)
00217 goto end;
00218
00219 ffm->write_index = FFM_PACKET_SIZE;
00220
00221 pts_start = get_dts(s, pos_min);
00222
00223 pts = get_dts(s, pos_max);
00224
00225 if (pts - 100000 <= pts_start) {
00226 while (1) {
00227 int64_t newpos;
00228 int64_t newpts;
00229
00230 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
00231
00232 if (newpos == pos_min)
00233 break;
00234
00235 newpts = get_dts(s, newpos);
00236
00237 if (newpts - 100000 <= pts) {
00238 pos_max = newpos;
00239 pts = newpts;
00240 } else {
00241 pos_min = newpos;
00242 }
00243 }
00244 ffm->write_index += pos_max;
00245 }
00246
00247
00248
00249
00250 end:
00251 avio_seek(pb, ptr, SEEK_SET);
00252 }
00253
00254
00255 static int ffm_close(AVFormatContext *s)
00256 {
00257 int i;
00258
00259 for (i = 0; i < s->nb_streams; i++)
00260 av_freep(&s->streams[i]->codec->rc_eq);
00261
00262 return 0;
00263 }
00264
00265
00266 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
00267 {
00268 FFMContext *ffm = s->priv_data;
00269 AVStream *st;
00270 AVIOContext *pb = s->pb;
00271 AVCodecContext *codec;
00272 int i, nb_streams;
00273 uint32_t tag;
00274
00275
00276 tag = avio_rl32(pb);
00277 if (tag != MKTAG('F', 'F', 'M', '1'))
00278 goto fail;
00279 ffm->packet_size = avio_rb32(pb);
00280 if (ffm->packet_size != FFM_PACKET_SIZE)
00281 goto fail;
00282 ffm->write_index = avio_rb64(pb);
00283
00284 if (!url_is_streamed(pb)) {
00285 ffm->file_size = avio_size(pb);
00286 if (ffm->write_index)
00287 adjust_write_index(s);
00288 } else {
00289 ffm->file_size = (UINT64_C(1) << 63) - 1;
00290 }
00291
00292 nb_streams = avio_rb32(pb);
00293 avio_rb32(pb);
00294
00295 for(i=0;i<nb_streams;i++) {
00296 char rc_eq_buf[128];
00297
00298 st = av_new_stream(s, 0);
00299 if (!st)
00300 goto fail;
00301
00302 av_set_pts_info(st, 64, 1, 1000000);
00303
00304 codec = st->codec;
00305
00306 codec->codec_id = avio_rb32(pb);
00307 codec->codec_type = avio_r8(pb);
00308 codec->bit_rate = avio_rb32(pb);
00309 st->quality = avio_rb32(pb);
00310 codec->flags = avio_rb32(pb);
00311 codec->flags2 = avio_rb32(pb);
00312 codec->debug = avio_rb32(pb);
00313
00314 switch(codec->codec_type) {
00315 case AVMEDIA_TYPE_VIDEO:
00316 codec->time_base.num = avio_rb32(pb);
00317 codec->time_base.den = avio_rb32(pb);
00318 codec->width = avio_rb16(pb);
00319 codec->height = avio_rb16(pb);
00320 codec->gop_size = avio_rb16(pb);
00321 codec->pix_fmt = avio_rb32(pb);
00322 codec->qmin = avio_r8(pb);
00323 codec->qmax = avio_r8(pb);
00324 codec->max_qdiff = avio_r8(pb);
00325 codec->qcompress = avio_rb16(pb) / 10000.0;
00326 codec->qblur = avio_rb16(pb) / 10000.0;
00327 codec->bit_rate_tolerance = avio_rb32(pb);
00328 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
00329 codec->rc_eq = av_strdup(rc_eq_buf);
00330 codec->rc_max_rate = avio_rb32(pb);
00331 codec->rc_min_rate = avio_rb32(pb);
00332 codec->rc_buffer_size = avio_rb32(pb);
00333 codec->i_quant_factor = av_int2dbl(avio_rb64(pb));
00334 codec->b_quant_factor = av_int2dbl(avio_rb64(pb));
00335 codec->i_quant_offset = av_int2dbl(avio_rb64(pb));
00336 codec->b_quant_offset = av_int2dbl(avio_rb64(pb));
00337 codec->dct_algo = avio_rb32(pb);
00338 codec->strict_std_compliance = avio_rb32(pb);
00339 codec->max_b_frames = avio_rb32(pb);
00340 codec->luma_elim_threshold = avio_rb32(pb);
00341 codec->chroma_elim_threshold = avio_rb32(pb);
00342 codec->mpeg_quant = avio_rb32(pb);
00343 codec->intra_dc_precision = avio_rb32(pb);
00344 codec->me_method = avio_rb32(pb);
00345 codec->mb_decision = avio_rb32(pb);
00346 codec->nsse_weight = avio_rb32(pb);
00347 codec->frame_skip_cmp = avio_rb32(pb);
00348 codec->rc_buffer_aggressivity = av_int2dbl(avio_rb64(pb));
00349 codec->codec_tag = avio_rb32(pb);
00350 codec->thread_count = avio_r8(pb);
00351 codec->coder_type = avio_rb32(pb);
00352 codec->me_cmp = avio_rb32(pb);
00353 codec->partitions = avio_rb32(pb);
00354 codec->me_subpel_quality = avio_rb32(pb);
00355 codec->me_range = avio_rb32(pb);
00356 codec->keyint_min = avio_rb32(pb);
00357 codec->scenechange_threshold = avio_rb32(pb);
00358 codec->b_frame_strategy = avio_rb32(pb);
00359 codec->qcompress = av_int2dbl(avio_rb64(pb));
00360 codec->qblur = av_int2dbl(avio_rb64(pb));
00361 codec->max_qdiff = avio_rb32(pb);
00362 codec->refs = avio_rb32(pb);
00363 codec->directpred = avio_rb32(pb);
00364 break;
00365 case AVMEDIA_TYPE_AUDIO:
00366 codec->sample_rate = avio_rb32(pb);
00367 codec->channels = avio_rl16(pb);
00368 codec->frame_size = avio_rl16(pb);
00369 codec->sample_fmt = (int16_t) avio_rl16(pb);
00370 break;
00371 default:
00372 goto fail;
00373 }
00374 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
00375 codec->extradata_size = avio_rb32(pb);
00376 codec->extradata = av_malloc(codec->extradata_size);
00377 if (!codec->extradata)
00378 return AVERROR(ENOMEM);
00379 avio_read(pb, codec->extradata, codec->extradata_size);
00380 }
00381 }
00382
00383
00384 while ((avio_tell(pb) % ffm->packet_size) != 0)
00385 avio_r8(pb);
00386
00387
00388 ffm->packet_ptr = ffm->packet;
00389 ffm->packet_end = ffm->packet;
00390 ffm->frame_offset = 0;
00391 ffm->dts = 0;
00392 ffm->read_state = READ_HEADER;
00393 ffm->first_packet = 1;
00394 return 0;
00395 fail:
00396 ffm_close(s);
00397 return -1;
00398 }
00399
00400
00401 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
00402 {
00403 int size;
00404 FFMContext *ffm = s->priv_data;
00405 int duration, ret;
00406
00407 switch(ffm->read_state) {
00408 case READ_HEADER:
00409 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
00410 return ret;
00411
00412 av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
00413 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
00414 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
00415 FRAME_HEADER_SIZE)
00416 return -1;
00417 if (ffm->header[1] & FLAG_DTS)
00418 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
00419 return -1;
00420 #if 0
00421 av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
00422 #endif
00423 ffm->read_state = READ_DATA;
00424
00425 case READ_DATA:
00426 size = AV_RB24(ffm->header + 2);
00427 if ((ret = ffm_is_avail_data(s, size)) < 0)
00428 return ret;
00429
00430 duration = AV_RB24(ffm->header + 5);
00431
00432 av_new_packet(pkt, size);
00433 pkt->stream_index = ffm->header[0];
00434 if ((unsigned)pkt->stream_index >= s->nb_streams) {
00435 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
00436 av_free_packet(pkt);
00437 ffm->read_state = READ_HEADER;
00438 return -1;
00439 }
00440 pkt->pos = avio_tell(s->pb);
00441 if (ffm->header[1] & FLAG_KEY_FRAME)
00442 pkt->flags |= AV_PKT_FLAG_KEY;
00443
00444 ffm->read_state = READ_HEADER;
00445 if (ffm_read_data(s, pkt->data, size, 0) != size) {
00446
00447 av_free_packet(pkt);
00448 return -1;
00449 }
00450 pkt->pts = AV_RB64(ffm->header+8);
00451 if (ffm->header[1] & FLAG_DTS)
00452 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
00453 else
00454 pkt->dts = pkt->pts;
00455 pkt->duration = duration;
00456 break;
00457 }
00458 return 0;
00459 }
00460
00461
00462
00463
00464 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
00465 {
00466 FFMContext *ffm = s->priv_data;
00467 int64_t pos_min, pos_max, pos;
00468 int64_t pts_min, pts_max, pts;
00469 double pos1;
00470
00471 #ifdef DEBUG_SEEK
00472 av_log(s, AV_LOG_DEBUG, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
00473 #endif
00474
00475
00476 pos_min = FFM_PACKET_SIZE;
00477 pos_max = ffm->file_size - FFM_PACKET_SIZE;
00478 while (pos_min <= pos_max) {
00479 pts_min = get_dts(s, pos_min);
00480 pts_max = get_dts(s, pos_max);
00481
00482 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
00483 (double)(pts_max - pts_min);
00484 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
00485 if (pos <= pos_min)
00486 pos = pos_min;
00487 else if (pos >= pos_max)
00488 pos = pos_max;
00489 pts = get_dts(s, pos);
00490
00491 if (pts == wanted_pts) {
00492 goto found;
00493 } else if (pts > wanted_pts) {
00494 pos_max = pos - FFM_PACKET_SIZE;
00495 } else {
00496 pos_min = pos + FFM_PACKET_SIZE;
00497 }
00498 }
00499 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
00500
00501 found:
00502 ffm_seek1(s, pos);
00503
00504
00505 ffm->read_state = READ_HEADER;
00506 ffm->packet_ptr = ffm->packet;
00507 ffm->packet_end = ffm->packet;
00508 ffm->first_packet = 1;
00509
00510 return 0;
00511 }
00512
00513 static int ffm_probe(AVProbeData *p)
00514 {
00515 if (
00516 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
00517 p->buf[3] == '1')
00518 return AVPROBE_SCORE_MAX + 1;
00519 return 0;
00520 }
00521
00522 AVInputFormat ff_ffm_demuxer = {
00523 "ffm",
00524 NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
00525 sizeof(FFMContext),
00526 ffm_probe,
00527 ffm_read_header,
00528 ffm_read_packet,
00529 ffm_close,
00530 ffm_seek,
00531 };