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