2 * FFM (ffserver live feed) demuxer
3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/intfloat.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/pixdesc.h"
34 #include "avio_internal.h"
36 static int ffm_is_avail_data(AVFormatContext *s, int size)
38 FFMContext *ffm = s->priv_data;
39 int64_t pos, avail_size;
42 len = ffm->packet_end - ffm->packet_ptr;
45 pos = avio_tell(s->pb);
46 if (!ffm->write_index) {
47 if (pos == ffm->file_size)
49 avail_size = ffm->file_size - pos;
51 if (pos == ffm->write_index) {
52 /* exactly at the end of stream */
53 if (ffm->server_attached)
54 return AVERROR(EAGAIN);
56 return AVERROR_INVALIDDATA;
57 } else if (pos < ffm->write_index) {
58 avail_size = ffm->write_index - pos;
60 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
63 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
64 if (size <= avail_size)
66 else if (ffm->server_attached)
67 return AVERROR(EAGAIN);
69 return AVERROR_INVALIDDATA;
72 static int ffm_resync(AVFormatContext *s, uint32_t state)
74 av_log(s, AV_LOG_ERROR, "resyncing\n");
75 while (state != PACKET_ID) {
76 if (avio_feof(s->pb)) {
77 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
80 state = (state << 8) | avio_r8(s->pb);
85 /* first is true if we read the frame header */
86 static int ffm_read_data(AVFormatContext *s,
87 uint8_t *buf, int size, int header)
89 FFMContext *ffm = s->priv_data;
90 AVIOContext *pb = s->pb;
91 int fill_size, size1, frame_offset;
94 int64_t last_pos = -1;
99 len = ffm->packet_end - ffm->packet_ptr;
105 if (avio_tell(pb) == ffm->file_size) {
106 if (ffm->server_attached) {
107 avio_seek(pb, ffm->packet_size, SEEK_SET);
112 if (pb->buffer_size != ffm->packet_size) {
113 int64_t tell = avio_tell(pb);
114 int ret = ffio_set_buf_size(pb, ffm->packet_size);
117 avio_seek(pb, tell, SEEK_SET);
119 id = avio_rb16(pb); /* PACKET_ID */
120 if (id != PACKET_ID) {
121 if (ffm_resync(s, id) < 0)
123 last_pos = avio_tell(pb);
125 fill_size = avio_rb16(pb);
126 ffm->dts = avio_rb64(pb);
127 frame_offset = avio_rb16(pb);
128 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
129 if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
132 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
133 /* if first packet or resynchronization packet, we must
134 handle it specifically */
135 if (ffm->first_packet || (frame_offset & 0x8000)) {
137 /* This packet has no frame headers in it */
138 if (avio_tell(pb) >= ffm->packet_size * 3LL) {
139 int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
140 seekback = FFMAX(seekback, 0);
141 avio_seek(pb, -seekback, SEEK_CUR);
144 /* This is bad, we cannot find a valid frame header */
147 ffm->first_packet = 0;
148 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
149 ffm->packet_end = ffm->packet_ptr;
152 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
156 ffm->packet_ptr = ffm->packet;
160 memcpy(buf, ffm->packet_ptr, len);
162 ffm->packet_ptr += len;
169 /* ensure that actual seeking happens between FFM_PACKET_SIZE
170 and file_size - FFM_PACKET_SIZE */
171 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
173 FFMContext *ffm = s->priv_data;
174 AVIOContext *pb = s->pb;
177 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
178 pos = FFMAX(pos, FFM_PACKET_SIZE);
179 ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
180 return avio_seek(pb, pos, SEEK_SET);
183 static int64_t get_dts(AVFormatContext *s, int64_t pos)
185 AVIOContext *pb = s->pb;
191 ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
195 static void adjust_write_index(AVFormatContext *s)
197 FFMContext *ffm = s->priv_data;
198 AVIOContext *pb = s->pb;
200 //int64_t orig_write_index = ffm->write_index;
201 int64_t pos_min, pos_max;
203 int64_t ptr = avio_tell(pb);
207 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
209 pts_start = get_dts(s, pos_min);
211 pts = get_dts(s, pos_max);
213 if (pts - 100000 > pts_start)
216 ffm->write_index = FFM_PACKET_SIZE;
218 pts_start = get_dts(s, pos_min);
220 pts = get_dts(s, pos_max);
222 if (pts - 100000 <= pts_start) {
227 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
229 if (newpos == pos_min)
232 newpts = get_dts(s, newpos);
234 if (newpts - 100000 <= pts) {
241 ffm->write_index += pos_max;
245 avio_seek(pb, ptr, SEEK_SET);
249 static int ffm_close(AVFormatContext *s)
253 for (i = 0; i < s->nb_streams; i++)
254 av_freep(&s->streams[i]->codec->rc_eq);
259 static int ffm_append_recommended_configuration(AVStream *st, char **conf)
263 av_assert0(conf && st);
266 if (!st->recommended_encoder_configuration) {
267 st->recommended_encoder_configuration = *conf;
271 newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
272 if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
274 av_strlcat(st->recommended_encoder_configuration, ",", newsize);
275 av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
280 static int ffm2_read_header(AVFormatContext *s)
282 FFMContext *ffm = s->priv_data;
284 AVIOContext *pb = s->pb;
285 AVCodecContext *codec;
286 const AVCodecDescriptor *codec_desc;
288 int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
292 ffm->packet_size = avio_rb32(pb);
293 if (ffm->packet_size != FFM_PACKET_SIZE) {
294 av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
295 ffm->packet_size, FFM_PACKET_SIZE);
296 ret = AVERROR_INVALIDDATA;
300 ffm->write_index = avio_rb64(pb);
301 /* get also filesize */
303 ffm->file_size = avio_size(pb);
304 if (ffm->write_index && 0)
305 adjust_write_index(s);
307 ffm->file_size = (UINT64_C(1) << 63) - 1;
310 while(!avio_feof(pb)) {
311 unsigned id = avio_rb32(pb);
312 unsigned size = avio_rb32(pb);
313 int64_t next = avio_tell(pb) + size;
320 case MKBETAG('M', 'A', 'I', 'N'):
322 ret = AVERROR(EINVAL);
325 avio_rb32(pb); /* nb_streams */
326 avio_rb32(pb); /* total bitrate */
328 case MKBETAG('C', 'O', 'M', 'M'):
329 f_cprv = f_stvi = f_stau = 0;
330 st = avformat_new_stream(s, NULL);
332 ret = AVERROR(ENOMEM);
336 avpriv_set_pts_info(st, 64, 1, 1000000);
340 codec->codec_id = avio_rb32(pb);
341 codec_desc = avcodec_descriptor_get(codec->codec_id);
343 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
344 codec->codec_id = AV_CODEC_ID_NONE;
347 codec->codec_type = avio_r8(pb);
348 if (codec->codec_type != codec_desc->type) {
349 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
350 codec_desc->type, codec->codec_type);
351 codec->codec_id = AV_CODEC_ID_NONE;
352 codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
355 codec->bit_rate = avio_rb32(pb);
356 codec->flags = avio_rb32(pb);
357 codec->flags2 = avio_rb32(pb);
358 codec->debug = avio_rb32(pb);
359 if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
360 int size = avio_rb32(pb);
361 codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
362 if (!codec->extradata)
363 return AVERROR(ENOMEM);
364 codec->extradata_size = size;
365 avio_read(pb, codec->extradata, size);
368 case MKBETAG('S', 'T', 'V', 'I'):
370 ret = AVERROR(EINVAL);
373 codec->time_base.num = avio_rb32(pb);
374 codec->time_base.den = avio_rb32(pb);
375 if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
376 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
377 codec->time_base.num, codec->time_base.den);
378 ret = AVERROR_INVALIDDATA;
381 codec->width = avio_rb16(pb);
382 codec->height = avio_rb16(pb);
383 codec->gop_size = avio_rb16(pb);
384 codec->pix_fmt = avio_rb32(pb);
385 if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
386 av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
387 codec->pix_fmt = AV_PIX_FMT_NONE;
390 codec->qmin = avio_r8(pb);
391 codec->qmax = avio_r8(pb);
392 codec->max_qdiff = avio_r8(pb);
393 codec->qcompress = avio_rb16(pb) / 10000.0;
394 codec->qblur = avio_rb16(pb) / 10000.0;
395 codec->bit_rate_tolerance = avio_rb32(pb);
396 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
397 codec->rc_eq = av_strdup(rc_eq_buf);
398 codec->rc_max_rate = avio_rb32(pb);
399 codec->rc_min_rate = avio_rb32(pb);
400 codec->rc_buffer_size = avio_rb32(pb);
401 codec->i_quant_factor = av_int2double(avio_rb64(pb));
402 codec->b_quant_factor = av_int2double(avio_rb64(pb));
403 codec->i_quant_offset = av_int2double(avio_rb64(pb));
404 codec->b_quant_offset = av_int2double(avio_rb64(pb));
405 codec->dct_algo = avio_rb32(pb);
406 codec->strict_std_compliance = avio_rb32(pb);
407 codec->max_b_frames = avio_rb32(pb);
408 codec->mpeg_quant = avio_rb32(pb);
409 codec->intra_dc_precision = avio_rb32(pb);
410 codec->me_method = avio_rb32(pb);
411 codec->mb_decision = avio_rb32(pb);
412 codec->nsse_weight = avio_rb32(pb);
413 codec->frame_skip_cmp = avio_rb32(pb);
414 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
415 codec->codec_tag = avio_rb32(pb);
416 codec->thread_count = avio_r8(pb);
417 codec->coder_type = avio_rb32(pb);
418 codec->me_cmp = avio_rb32(pb);
419 codec->me_subpel_quality = avio_rb32(pb);
420 codec->me_range = avio_rb32(pb);
421 codec->keyint_min = avio_rb32(pb);
422 codec->scenechange_threshold = avio_rb32(pb);
423 codec->b_frame_strategy = avio_rb32(pb);
424 codec->qcompress = av_int2double(avio_rb64(pb));
425 codec->qblur = av_int2double(avio_rb64(pb));
426 codec->max_qdiff = avio_rb32(pb);
427 codec->refs = avio_rb32(pb);
429 case MKBETAG('S', 'T', 'A', 'U'):
431 ret = AVERROR(EINVAL);
434 codec->sample_rate = avio_rb32(pb);
435 if (codec->sample_rate <= 0) {
436 av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", codec->sample_rate);
437 ret = AVERROR_INVALIDDATA;
440 codec->channels = avio_rl16(pb);
441 codec->frame_size = avio_rl16(pb);
443 case MKBETAG('C', 'P', 'R', 'V'):
445 ret = AVERROR(EINVAL);
448 enc = avcodec_find_encoder(codec->codec_id);
449 if (enc && enc->priv_data_size && enc->priv_class) {
450 buffer = av_malloc(size + 1);
452 ret = AVERROR(ENOMEM);
455 avio_get_str(pb, size, buffer, size + 1);
456 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
460 case MKBETAG('S', '2', 'V', 'I'):
461 if (f_stvi++ || !size) {
462 ret = AVERROR(EINVAL);
465 buffer = av_malloc(size);
467 ret = AVERROR(ENOMEM);
470 avio_get_str(pb, INT_MAX, buffer, size);
471 av_set_options_string(codec, buffer, "=", ",");
472 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
475 case MKBETAG('S', '2', 'A', 'U'):
476 if (f_stau++ || !size) {
477 ret = AVERROR(EINVAL);
480 buffer = av_malloc(size);
482 ret = AVERROR(ENOMEM);
485 avio_get_str(pb, INT_MAX, buffer, size);
486 av_set_options_string(codec, buffer, "=", ",");
487 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
491 avio_seek(pb, next, SEEK_SET);
494 for (i = 0; i < s->nb_streams; i++)
495 avcodec_parameters_from_context(s->streams[i]->codecpar, s->streams[i]->codec);
497 /* get until end of block reached */
498 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
501 /* init packet demux */
502 ffm->packet_ptr = ffm->packet;
503 ffm->packet_end = ffm->packet;
504 ffm->frame_offset = 0;
506 ffm->read_state = READ_HEADER;
507 ffm->first_packet = 1;
514 static int ffm_read_header(AVFormatContext *s)
516 FFMContext *ffm = s->priv_data;
518 AVIOContext *pb = s->pb;
519 AVCodecContext *codec;
520 const AVCodecDescriptor *codec_desc;
526 if (tag == MKTAG('F', 'F', 'M', '2'))
527 return ffm2_read_header(s);
528 if (tag != MKTAG('F', 'F', 'M', '1'))
530 ffm->packet_size = avio_rb32(pb);
531 if (ffm->packet_size != FFM_PACKET_SIZE)
533 ffm->write_index = avio_rb64(pb);
534 /* get also filesize */
536 ffm->file_size = avio_size(pb);
537 if (ffm->write_index && 0)
538 adjust_write_index(s);
540 ffm->file_size = (UINT64_C(1) << 63) - 1;
543 nb_streams = avio_rb32(pb);
544 avio_rb32(pb); /* total bitrate */
545 /* read each stream */
546 for(i=0;i<nb_streams;i++) {
549 st = avformat_new_stream(s, NULL);
553 avpriv_set_pts_info(st, 64, 1, 1000000);
557 codec->codec_id = avio_rb32(pb);
558 codec_desc = avcodec_descriptor_get(codec->codec_id);
560 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
561 codec->codec_id = AV_CODEC_ID_NONE;
564 codec->codec_type = avio_r8(pb); /* codec_type */
565 if (codec->codec_type != codec_desc->type) {
566 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
567 codec_desc->type, codec->codec_type);
568 codec->codec_id = AV_CODEC_ID_NONE;
569 codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
572 codec->bit_rate = avio_rb32(pb);
573 codec->flags = avio_rb32(pb);
574 codec->flags2 = avio_rb32(pb);
575 codec->debug = avio_rb32(pb);
577 switch(codec->codec_type) {
578 case AVMEDIA_TYPE_VIDEO:
579 codec->time_base.num = avio_rb32(pb);
580 codec->time_base.den = avio_rb32(pb);
581 if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
582 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
583 codec->time_base.num, codec->time_base.den);
586 codec->width = avio_rb16(pb);
587 codec->height = avio_rb16(pb);
588 codec->gop_size = avio_rb16(pb);
589 codec->pix_fmt = avio_rb32(pb);
590 if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
591 av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
592 codec->pix_fmt = AV_PIX_FMT_NONE;
595 codec->qmin = avio_r8(pb);
596 codec->qmax = avio_r8(pb);
597 codec->max_qdiff = avio_r8(pb);
598 codec->qcompress = avio_rb16(pb) / 10000.0;
599 codec->qblur = avio_rb16(pb) / 10000.0;
600 codec->bit_rate_tolerance = avio_rb32(pb);
601 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
602 codec->rc_eq = av_strdup(rc_eq_buf);
603 codec->rc_max_rate = avio_rb32(pb);
604 codec->rc_min_rate = avio_rb32(pb);
605 codec->rc_buffer_size = avio_rb32(pb);
606 codec->i_quant_factor = av_int2double(avio_rb64(pb));
607 codec->b_quant_factor = av_int2double(avio_rb64(pb));
608 codec->i_quant_offset = av_int2double(avio_rb64(pb));
609 codec->b_quant_offset = av_int2double(avio_rb64(pb));
610 codec->dct_algo = avio_rb32(pb);
611 codec->strict_std_compliance = avio_rb32(pb);
612 codec->max_b_frames = avio_rb32(pb);
613 codec->mpeg_quant = avio_rb32(pb);
614 codec->intra_dc_precision = avio_rb32(pb);
615 codec->me_method = avio_rb32(pb);
616 codec->mb_decision = avio_rb32(pb);
617 codec->nsse_weight = avio_rb32(pb);
618 codec->frame_skip_cmp = avio_rb32(pb);
619 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
620 codec->codec_tag = avio_rb32(pb);
621 codec->thread_count = avio_r8(pb);
622 codec->coder_type = avio_rb32(pb);
623 codec->me_cmp = avio_rb32(pb);
624 codec->me_subpel_quality = avio_rb32(pb);
625 codec->me_range = avio_rb32(pb);
626 codec->keyint_min = avio_rb32(pb);
627 codec->scenechange_threshold = avio_rb32(pb);
628 codec->b_frame_strategy = avio_rb32(pb);
629 codec->qcompress = av_int2double(avio_rb64(pb));
630 codec->qblur = av_int2double(avio_rb64(pb));
631 codec->max_qdiff = avio_rb32(pb);
632 codec->refs = avio_rb32(pb);
634 case AVMEDIA_TYPE_AUDIO:
635 codec->sample_rate = avio_rb32(pb);
636 if (codec->sample_rate <= 0) {
637 av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", codec->sample_rate);
640 codec->channels = avio_rl16(pb);
641 codec->frame_size = avio_rl16(pb);
646 if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
647 int size = avio_rb32(pb);
648 codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
649 if (!codec->extradata)
650 return AVERROR(ENOMEM);
651 codec->extradata_size = size;
652 avio_read(pb, codec->extradata, size);
655 avcodec_parameters_from_context(st->codecpar, codec);
658 /* get until end of block reached */
659 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
662 /* init packet demux */
663 ffm->packet_ptr = ffm->packet;
664 ffm->packet_end = ffm->packet;
665 ffm->frame_offset = 0;
667 ffm->read_state = READ_HEADER;
668 ffm->first_packet = 1;
675 /* return < 0 if eof */
676 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
679 FFMContext *ffm = s->priv_data;
682 switch(ffm->read_state) {
684 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
687 ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
688 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
689 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
692 if (ffm->header[1] & FLAG_DTS)
693 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
695 ffm->read_state = READ_DATA;
698 size = AV_RB24(ffm->header + 2);
699 if ((ret = ffm_is_avail_data(s, size)) < 0)
702 duration = AV_RB24(ffm->header + 5);
704 if (av_new_packet(pkt, size) < 0) {
705 return AVERROR(ENOMEM);
707 pkt->stream_index = ffm->header[0];
708 if ((unsigned)pkt->stream_index >= s->nb_streams) {
709 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
710 av_packet_unref(pkt);
711 ffm->read_state = READ_HEADER;
714 pkt->pos = avio_tell(s->pb);
715 if (ffm->header[1] & FLAG_KEY_FRAME)
716 pkt->flags |= AV_PKT_FLAG_KEY;
718 ffm->read_state = READ_HEADER;
719 if (ffm_read_data(s, pkt->data, size, 0) != size) {
720 /* bad case: desynchronized packet. we cancel all the packet loading */
721 av_packet_unref(pkt);
724 pkt->pts = AV_RB64(ffm->header+8);
725 if (ffm->header[1] & FLAG_DTS)
726 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
729 pkt->duration = duration;
735 /* seek to a given time in the file. The file read pointer is
736 positioned at or before pts. XXX: the following code is quite
738 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
740 FFMContext *ffm = s->priv_data;
741 int64_t pos_min, pos_max, pos;
742 int64_t pts_min, pts_max, pts;
745 ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
746 /* find the position using linear interpolation (better than
747 dichotomy in typical cases) */
748 if (ffm->write_index && ffm->write_index < ffm->file_size) {
749 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
750 pos_min = FFM_PACKET_SIZE;
751 pos_max = ffm->write_index - FFM_PACKET_SIZE;
753 pos_min = ffm->write_index;
754 pos_max = ffm->file_size - FFM_PACKET_SIZE;
757 pos_min = FFM_PACKET_SIZE;
758 pos_max = ffm->file_size - FFM_PACKET_SIZE;
760 while (pos_min <= pos_max) {
761 pts_min = get_dts(s, pos_min);
762 pts_max = get_dts(s, pos_max);
763 if (pts_min > wanted_pts || pts_max <= wanted_pts) {
764 pos = pts_min > wanted_pts ? pos_min : pos_max;
767 /* linear interpolation */
768 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
769 (double)(pts_max - pts_min);
770 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
773 else if (pos >= pos_max)
775 pts = get_dts(s, pos);
776 /* check if we are lucky */
777 if (pts == wanted_pts) {
779 } else if (pts > wanted_pts) {
780 pos_max = pos - FFM_PACKET_SIZE;
782 pos_min = pos + FFM_PACKET_SIZE;
785 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
788 if (ffm_seek1(s, pos) < 0)
791 /* reset read state */
792 ffm->read_state = READ_HEADER;
793 ffm->packet_ptr = ffm->packet;
794 ffm->packet_end = ffm->packet;
795 ffm->first_packet = 1;
800 static int ffm_probe(AVProbeData *p)
803 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
804 (p->buf[3] == '1' || p->buf[3] == '2'))
805 return AVPROBE_SCORE_MAX + 1;
809 static const AVOption options[] = {
810 {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
811 {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
812 {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
816 static const AVClass ffm_class = {
817 .class_name = "ffm demuxer",
818 .item_name = av_default_item_name,
820 .version = LIBAVUTIL_VERSION_INT,
822 AVInputFormat ff_ffm_demuxer = {
824 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
825 .priv_data_size = sizeof(FFMContext),
826 .read_probe = ffm_probe,
827 .read_header = ffm_read_header,
828 .read_packet = ffm_read_packet,
829 .read_close = ffm_close,
830 .read_seek = ffm_seek,
831 .priv_class = &ffm_class,