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 static void ffm_seek1(AVFormatContext *s, int64_t pos1)
00169 {
00170 FFMContext *ffm = s->priv_data;
00171 AVIOContext *pb = s->pb;
00172 int64_t pos;
00173
00174 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
00175 pos = FFMAX(pos, FFM_PACKET_SIZE);
00176 av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
00177 avio_seek(pb, pos, SEEK_SET);
00178 }
00179
00180 static int64_t get_dts(AVFormatContext *s, int64_t pos)
00181 {
00182 AVIOContext *pb = s->pb;
00183 int64_t dts;
00184
00185 ffm_seek1(s, pos);
00186 avio_skip(pb, 4);
00187 dts = avio_rb64(pb);
00188 av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
00189 return dts;
00190 }
00191
00192 static void adjust_write_index(AVFormatContext *s)
00193 {
00194 FFMContext *ffm = s->priv_data;
00195 AVIOContext *pb = s->pb;
00196 int64_t pts;
00197
00198 int64_t pos_min, pos_max;
00199 int64_t pts_start;
00200 int64_t ptr = avio_tell(pb);
00201
00202
00203 pos_min = 0;
00204 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
00205
00206 pts_start = get_dts(s, pos_min);
00207
00208 pts = get_dts(s, pos_max);
00209
00210 if (pts - 100000 > pts_start)
00211 goto end;
00212
00213 ffm->write_index = FFM_PACKET_SIZE;
00214
00215 pts_start = get_dts(s, pos_min);
00216
00217 pts = get_dts(s, pos_max);
00218
00219 if (pts - 100000 <= pts_start) {
00220 while (1) {
00221 int64_t newpos;
00222 int64_t newpts;
00223
00224 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
00225
00226 if (newpos == pos_min)
00227 break;
00228
00229 newpts = get_dts(s, newpos);
00230
00231 if (newpts - 100000 <= pts) {
00232 pos_max = newpos;
00233 pts = newpts;
00234 } else {
00235 pos_min = newpos;
00236 }
00237 }
00238 ffm->write_index += pos_max;
00239 }
00240
00241
00242
00243
00244 end:
00245 avio_seek(pb, ptr, SEEK_SET);
00246 }
00247
00248
00249 static int ffm_close(AVFormatContext *s)
00250 {
00251 int i;
00252
00253 for (i = 0; i < s->nb_streams; i++)
00254 av_freep(&s->streams[i]->codec->rc_eq);
00255
00256 return 0;
00257 }
00258
00259
00260 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
00261 {
00262 FFMContext *ffm = s->priv_data;
00263 AVStream *st;
00264 AVIOContext *pb = s->pb;
00265 AVCodecContext *codec;
00266 int i, nb_streams;
00267 uint32_t tag;
00268
00269
00270 tag = avio_rl32(pb);
00271 if (tag != MKTAG('F', 'F', 'M', '1'))
00272 goto fail;
00273 ffm->packet_size = avio_rb32(pb);
00274 if (ffm->packet_size != FFM_PACKET_SIZE)
00275 goto fail;
00276 ffm->write_index = avio_rb64(pb);
00277
00278 if (pb->seekable) {
00279 ffm->file_size = avio_size(pb);
00280 if (ffm->write_index)
00281 adjust_write_index(s);
00282 } else {
00283 ffm->file_size = (UINT64_C(1) << 63) - 1;
00284 }
00285
00286 nb_streams = avio_rb32(pb);
00287 avio_rb32(pb);
00288
00289 for(i=0;i<nb_streams;i++) {
00290 char rc_eq_buf[128];
00291
00292 st = av_new_stream(s, 0);
00293 if (!st)
00294 goto fail;
00295
00296 av_set_pts_info(st, 64, 1, 1000000);
00297
00298 codec = st->codec;
00299
00300 codec->codec_id = avio_rb32(pb);
00301 codec->codec_type = avio_r8(pb);
00302 codec->bit_rate = avio_rb32(pb);
00303 st->quality = avio_rb32(pb);
00304 codec->flags = avio_rb32(pb);
00305 codec->flags2 = avio_rb32(pb);
00306 codec->debug = avio_rb32(pb);
00307
00308 switch(codec->codec_type) {
00309 case AVMEDIA_TYPE_VIDEO:
00310 codec->time_base.num = avio_rb32(pb);
00311 codec->time_base.den = avio_rb32(pb);
00312 codec->width = avio_rb16(pb);
00313 codec->height = avio_rb16(pb);
00314 codec->gop_size = avio_rb16(pb);
00315 codec->pix_fmt = avio_rb32(pb);
00316 codec->qmin = avio_r8(pb);
00317 codec->qmax = avio_r8(pb);
00318 codec->max_qdiff = avio_r8(pb);
00319 codec->qcompress = avio_rb16(pb) / 10000.0;
00320 codec->qblur = avio_rb16(pb) / 10000.0;
00321 codec->bit_rate_tolerance = avio_rb32(pb);
00322 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
00323 codec->rc_max_rate = avio_rb32(pb);
00324 codec->rc_min_rate = avio_rb32(pb);
00325 codec->rc_buffer_size = avio_rb32(pb);
00326 codec->i_quant_factor = av_int2dbl(avio_rb64(pb));
00327 codec->b_quant_factor = av_int2dbl(avio_rb64(pb));
00328 codec->i_quant_offset = av_int2dbl(avio_rb64(pb));
00329 codec->b_quant_offset = av_int2dbl(avio_rb64(pb));
00330 codec->dct_algo = avio_rb32(pb);
00331 codec->strict_std_compliance = avio_rb32(pb);
00332 codec->max_b_frames = avio_rb32(pb);
00333 codec->luma_elim_threshold = avio_rb32(pb);
00334 codec->chroma_elim_threshold = avio_rb32(pb);
00335 codec->mpeg_quant = avio_rb32(pb);
00336 codec->intra_dc_precision = avio_rb32(pb);
00337 codec->me_method = avio_rb32(pb);
00338 codec->mb_decision = avio_rb32(pb);
00339 codec->nsse_weight = avio_rb32(pb);
00340 codec->frame_skip_cmp = avio_rb32(pb);
00341 codec->rc_buffer_aggressivity = av_int2dbl(avio_rb64(pb));
00342 codec->codec_tag = avio_rb32(pb);
00343 codec->thread_count = avio_r8(pb);
00344 codec->coder_type = avio_rb32(pb);
00345 codec->me_cmp = avio_rb32(pb);
00346 codec->partitions = avio_rb32(pb);
00347 codec->me_subpel_quality = avio_rb32(pb);
00348 codec->me_range = avio_rb32(pb);
00349 codec->keyint_min = avio_rb32(pb);
00350 codec->scenechange_threshold = avio_rb32(pb);
00351 codec->b_frame_strategy = avio_rb32(pb);
00352 codec->qcompress = av_int2dbl(avio_rb64(pb));
00353 codec->qblur = av_int2dbl(avio_rb64(pb));
00354 codec->max_qdiff = avio_rb32(pb);
00355 codec->refs = avio_rb32(pb);
00356 codec->directpred = avio_rb32(pb);
00357 break;
00358 case AVMEDIA_TYPE_AUDIO:
00359 codec->sample_rate = avio_rb32(pb);
00360 codec->channels = avio_rl16(pb);
00361 codec->frame_size = avio_rl16(pb);
00362 codec->sample_fmt = (int16_t) avio_rl16(pb);
00363 break;
00364 default:
00365 goto fail;
00366 }
00367 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
00368 codec->extradata_size = avio_rb32(pb);
00369 codec->extradata = av_malloc(codec->extradata_size);
00370 if (!codec->extradata)
00371 return AVERROR(ENOMEM);
00372 avio_read(pb, codec->extradata, codec->extradata_size);
00373 }
00374 }
00375
00376
00377 while ((avio_tell(pb) % ffm->packet_size) != 0)
00378 avio_r8(pb);
00379
00380
00381 ffm->packet_ptr = ffm->packet;
00382 ffm->packet_end = ffm->packet;
00383 ffm->frame_offset = 0;
00384 ffm->dts = 0;
00385 ffm->read_state = READ_HEADER;
00386 ffm->first_packet = 1;
00387 return 0;
00388 fail:
00389 ffm_close(s);
00390 return -1;
00391 }
00392
00393
00394 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
00395 {
00396 int size;
00397 FFMContext *ffm = s->priv_data;
00398 int duration, ret;
00399
00400 switch(ffm->read_state) {
00401 case READ_HEADER:
00402 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
00403 return ret;
00404
00405 av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
00406 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
00407 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
00408 FRAME_HEADER_SIZE)
00409 return -1;
00410 if (ffm->header[1] & FLAG_DTS)
00411 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
00412 return -1;
00413 ffm->read_state = READ_DATA;
00414
00415 case READ_DATA:
00416 size = AV_RB24(ffm->header + 2);
00417 if ((ret = ffm_is_avail_data(s, size)) < 0)
00418 return ret;
00419
00420 duration = AV_RB24(ffm->header + 5);
00421
00422 av_new_packet(pkt, size);
00423 pkt->stream_index = ffm->header[0];
00424 if ((unsigned)pkt->stream_index >= s->nb_streams) {
00425 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
00426 av_free_packet(pkt);
00427 ffm->read_state = READ_HEADER;
00428 return -1;
00429 }
00430 pkt->pos = avio_tell(s->pb);
00431 if (ffm->header[1] & FLAG_KEY_FRAME)
00432 pkt->flags |= AV_PKT_FLAG_KEY;
00433
00434 ffm->read_state = READ_HEADER;
00435 if (ffm_read_data(s, pkt->data, size, 0) != size) {
00436
00437 av_free_packet(pkt);
00438 return -1;
00439 }
00440 pkt->pts = AV_RB64(ffm->header+8);
00441 if (ffm->header[1] & FLAG_DTS)
00442 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
00443 else
00444 pkt->dts = pkt->pts;
00445 pkt->duration = duration;
00446 break;
00447 }
00448 return 0;
00449 }
00450
00451
00452
00453
00454 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
00455 {
00456 FFMContext *ffm = s->priv_data;
00457 int64_t pos_min, pos_max, pos;
00458 int64_t pts_min, pts_max, pts;
00459 double pos1;
00460
00461 av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
00462
00463
00464 pos_min = FFM_PACKET_SIZE;
00465 pos_max = ffm->file_size - FFM_PACKET_SIZE;
00466 while (pos_min <= pos_max) {
00467 pts_min = get_dts(s, pos_min);
00468 pts_max = get_dts(s, pos_max);
00469
00470 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
00471 (double)(pts_max - pts_min);
00472 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
00473 if (pos <= pos_min)
00474 pos = pos_min;
00475 else if (pos >= pos_max)
00476 pos = pos_max;
00477 pts = get_dts(s, pos);
00478
00479 if (pts == wanted_pts) {
00480 goto found;
00481 } else if (pts > wanted_pts) {
00482 pos_max = pos - FFM_PACKET_SIZE;
00483 } else {
00484 pos_min = pos + FFM_PACKET_SIZE;
00485 }
00486 }
00487 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
00488
00489 found:
00490 ffm_seek1(s, pos);
00491
00492
00493 ffm->read_state = READ_HEADER;
00494 ffm->packet_ptr = ffm->packet;
00495 ffm->packet_end = ffm->packet;
00496 ffm->first_packet = 1;
00497
00498 return 0;
00499 }
00500
00501 static int ffm_probe(AVProbeData *p)
00502 {
00503 if (
00504 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
00505 p->buf[3] == '1')
00506 return AVPROBE_SCORE_MAX + 1;
00507 return 0;
00508 }
00509
00510 AVInputFormat ff_ffm_demuxer = {
00511 "ffm",
00512 NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
00513 sizeof(FFMContext),
00514 ffm_probe,
00515 ffm_read_header,
00516 ffm_read_packet,
00517 ffm_close,
00518 ffm_seek,
00519 };