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 codec->channels = avio_rl16(pb);
436 codec->frame_size = avio_rl16(pb);
438 case MKBETAG('C', 'P', 'R', 'V'):
440 ret = AVERROR(EINVAL);
443 enc = avcodec_find_encoder(codec->codec_id);
444 if (enc && enc->priv_data_size && enc->priv_class) {
445 buffer = av_malloc(size + 1);
447 ret = AVERROR(ENOMEM);
450 avio_get_str(pb, size, buffer, size + 1);
451 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
455 case MKBETAG('S', '2', 'V', 'I'):
456 if (f_stvi++ || !size) {
457 ret = AVERROR(EINVAL);
460 buffer = av_malloc(size);
462 ret = AVERROR(ENOMEM);
465 avio_get_str(pb, INT_MAX, buffer, size);
466 av_set_options_string(codec, buffer, "=", ",");
467 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
470 case MKBETAG('S', '2', 'A', 'U'):
471 if (f_stau++ || !size) {
472 ret = AVERROR(EINVAL);
475 buffer = av_malloc(size);
477 ret = AVERROR(ENOMEM);
480 avio_get_str(pb, INT_MAX, buffer, size);
481 av_set_options_string(codec, buffer, "=", ",");
482 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
486 avio_seek(pb, next, SEEK_SET);
489 for (i = 0; i < s->nb_streams; i++)
490 avcodec_parameters_from_context(s->streams[i]->codecpar, s->streams[i]->codec);
492 /* get until end of block reached */
493 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
496 /* init packet demux */
497 ffm->packet_ptr = ffm->packet;
498 ffm->packet_end = ffm->packet;
499 ffm->frame_offset = 0;
501 ffm->read_state = READ_HEADER;
502 ffm->first_packet = 1;
509 static int ffm_read_header(AVFormatContext *s)
511 FFMContext *ffm = s->priv_data;
513 AVIOContext *pb = s->pb;
514 AVCodecContext *codec;
515 const AVCodecDescriptor *codec_desc;
521 if (tag == MKTAG('F', 'F', 'M', '2'))
522 return ffm2_read_header(s);
523 if (tag != MKTAG('F', 'F', 'M', '1'))
525 ffm->packet_size = avio_rb32(pb);
526 if (ffm->packet_size != FFM_PACKET_SIZE)
528 ffm->write_index = avio_rb64(pb);
529 /* get also filesize */
531 ffm->file_size = avio_size(pb);
532 if (ffm->write_index && 0)
533 adjust_write_index(s);
535 ffm->file_size = (UINT64_C(1) << 63) - 1;
538 nb_streams = avio_rb32(pb);
539 avio_rb32(pb); /* total bitrate */
540 /* read each stream */
541 for(i=0;i<nb_streams;i++) {
544 st = avformat_new_stream(s, NULL);
548 avpriv_set_pts_info(st, 64, 1, 1000000);
552 codec->codec_id = avio_rb32(pb);
553 codec_desc = avcodec_descriptor_get(codec->codec_id);
555 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
556 codec->codec_id = AV_CODEC_ID_NONE;
559 codec->codec_type = avio_r8(pb); /* codec_type */
560 if (codec->codec_type != codec_desc->type) {
561 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
562 codec_desc->type, codec->codec_type);
563 codec->codec_id = AV_CODEC_ID_NONE;
564 codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
567 codec->bit_rate = avio_rb32(pb);
568 codec->flags = avio_rb32(pb);
569 codec->flags2 = avio_rb32(pb);
570 codec->debug = avio_rb32(pb);
572 switch(codec->codec_type) {
573 case AVMEDIA_TYPE_VIDEO:
574 codec->time_base.num = avio_rb32(pb);
575 codec->time_base.den = avio_rb32(pb);
576 if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
577 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
578 codec->time_base.num, codec->time_base.den);
581 codec->width = avio_rb16(pb);
582 codec->height = avio_rb16(pb);
583 codec->gop_size = avio_rb16(pb);
584 codec->pix_fmt = avio_rb32(pb);
585 if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
586 av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
587 codec->pix_fmt = AV_PIX_FMT_NONE;
590 codec->qmin = avio_r8(pb);
591 codec->qmax = avio_r8(pb);
592 codec->max_qdiff = avio_r8(pb);
593 codec->qcompress = avio_rb16(pb) / 10000.0;
594 codec->qblur = avio_rb16(pb) / 10000.0;
595 codec->bit_rate_tolerance = avio_rb32(pb);
596 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
597 codec->rc_eq = av_strdup(rc_eq_buf);
598 codec->rc_max_rate = avio_rb32(pb);
599 codec->rc_min_rate = avio_rb32(pb);
600 codec->rc_buffer_size = avio_rb32(pb);
601 codec->i_quant_factor = av_int2double(avio_rb64(pb));
602 codec->b_quant_factor = av_int2double(avio_rb64(pb));
603 codec->i_quant_offset = av_int2double(avio_rb64(pb));
604 codec->b_quant_offset = av_int2double(avio_rb64(pb));
605 codec->dct_algo = avio_rb32(pb);
606 codec->strict_std_compliance = avio_rb32(pb);
607 codec->max_b_frames = avio_rb32(pb);
608 codec->mpeg_quant = avio_rb32(pb);
609 codec->intra_dc_precision = avio_rb32(pb);
610 codec->me_method = avio_rb32(pb);
611 codec->mb_decision = avio_rb32(pb);
612 codec->nsse_weight = avio_rb32(pb);
613 codec->frame_skip_cmp = avio_rb32(pb);
614 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
615 codec->codec_tag = avio_rb32(pb);
616 codec->thread_count = avio_r8(pb);
617 codec->coder_type = avio_rb32(pb);
618 codec->me_cmp = avio_rb32(pb);
619 codec->me_subpel_quality = avio_rb32(pb);
620 codec->me_range = avio_rb32(pb);
621 codec->keyint_min = avio_rb32(pb);
622 codec->scenechange_threshold = avio_rb32(pb);
623 codec->b_frame_strategy = avio_rb32(pb);
624 codec->qcompress = av_int2double(avio_rb64(pb));
625 codec->qblur = av_int2double(avio_rb64(pb));
626 codec->max_qdiff = avio_rb32(pb);
627 codec->refs = avio_rb32(pb);
629 case AVMEDIA_TYPE_AUDIO:
630 codec->sample_rate = avio_rb32(pb);
631 codec->channels = avio_rl16(pb);
632 codec->frame_size = avio_rl16(pb);
637 if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
638 int size = avio_rb32(pb);
639 codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
640 if (!codec->extradata)
641 return AVERROR(ENOMEM);
642 codec->extradata_size = size;
643 avio_read(pb, codec->extradata, size);
646 avcodec_parameters_from_context(st->codecpar, codec);
649 /* get until end of block reached */
650 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
653 /* init packet demux */
654 ffm->packet_ptr = ffm->packet;
655 ffm->packet_end = ffm->packet;
656 ffm->frame_offset = 0;
658 ffm->read_state = READ_HEADER;
659 ffm->first_packet = 1;
666 /* return < 0 if eof */
667 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
670 FFMContext *ffm = s->priv_data;
673 switch(ffm->read_state) {
675 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
678 ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
679 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
680 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
683 if (ffm->header[1] & FLAG_DTS)
684 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
686 ffm->read_state = READ_DATA;
689 size = AV_RB24(ffm->header + 2);
690 if ((ret = ffm_is_avail_data(s, size)) < 0)
693 duration = AV_RB24(ffm->header + 5);
695 if (av_new_packet(pkt, size) < 0) {
696 return AVERROR(ENOMEM);
698 pkt->stream_index = ffm->header[0];
699 if ((unsigned)pkt->stream_index >= s->nb_streams) {
700 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
701 av_packet_unref(pkt);
702 ffm->read_state = READ_HEADER;
705 pkt->pos = avio_tell(s->pb);
706 if (ffm->header[1] & FLAG_KEY_FRAME)
707 pkt->flags |= AV_PKT_FLAG_KEY;
709 ffm->read_state = READ_HEADER;
710 if (ffm_read_data(s, pkt->data, size, 0) != size) {
711 /* bad case: desynchronized packet. we cancel all the packet loading */
712 av_packet_unref(pkt);
715 pkt->pts = AV_RB64(ffm->header+8);
716 if (ffm->header[1] & FLAG_DTS)
717 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
720 pkt->duration = duration;
726 /* seek to a given time in the file. The file read pointer is
727 positioned at or before pts. XXX: the following code is quite
729 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
731 FFMContext *ffm = s->priv_data;
732 int64_t pos_min, pos_max, pos;
733 int64_t pts_min, pts_max, pts;
736 ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
737 /* find the position using linear interpolation (better than
738 dichotomy in typical cases) */
739 if (ffm->write_index && ffm->write_index < ffm->file_size) {
740 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
741 pos_min = FFM_PACKET_SIZE;
742 pos_max = ffm->write_index - FFM_PACKET_SIZE;
744 pos_min = ffm->write_index;
745 pos_max = ffm->file_size - FFM_PACKET_SIZE;
748 pos_min = FFM_PACKET_SIZE;
749 pos_max = ffm->file_size - FFM_PACKET_SIZE;
751 while (pos_min <= pos_max) {
752 pts_min = get_dts(s, pos_min);
753 pts_max = get_dts(s, pos_max);
754 if (pts_min > wanted_pts || pts_max <= wanted_pts) {
755 pos = pts_min > wanted_pts ? pos_min : pos_max;
758 /* linear interpolation */
759 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
760 (double)(pts_max - pts_min);
761 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
764 else if (pos >= pos_max)
766 pts = get_dts(s, pos);
767 /* check if we are lucky */
768 if (pts == wanted_pts) {
770 } else if (pts > wanted_pts) {
771 pos_max = pos - FFM_PACKET_SIZE;
773 pos_min = pos + FFM_PACKET_SIZE;
776 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
779 if (ffm_seek1(s, pos) < 0)
782 /* reset read state */
783 ffm->read_state = READ_HEADER;
784 ffm->packet_ptr = ffm->packet;
785 ffm->packet_end = ffm->packet;
786 ffm->first_packet = 1;
791 static int ffm_probe(AVProbeData *p)
794 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
795 (p->buf[3] == '1' || p->buf[3] == '2'))
796 return AVPROBE_SCORE_MAX + 1;
800 static const AVOption options[] = {
801 {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
802 {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
803 {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
807 static const AVClass ffm_class = {
808 .class_name = "ffm demuxer",
809 .item_name = av_default_item_name,
811 .version = LIBAVUTIL_VERSION_INT,
813 AVInputFormat ff_ffm_demuxer = {
815 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
816 .priv_data_size = sizeof(FFMContext),
817 .read_probe = ffm_probe,
818 .read_header = ffm_read_header,
819 .read_packet = ffm_read_packet,
820 .read_close = ffm_close,
821 .read_seek = ffm_seek,
822 .priv_class = &ffm_class,