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"
33 #include "avio_internal.h"
35 static int ffm_is_avail_data(AVFormatContext *s, int size)
37 FFMContext *ffm = s->priv_data;
38 int64_t pos, avail_size;
41 len = ffm->packet_end - ffm->packet_ptr;
44 pos = avio_tell(s->pb);
45 if (!ffm->write_index) {
46 if (pos == ffm->file_size)
48 avail_size = ffm->file_size - pos;
50 if (pos == ffm->write_index) {
51 /* exactly at the end of stream */
52 if (ffm->server_attached)
53 return AVERROR(EAGAIN);
55 return AVERROR_INVALIDDATA;
56 } else if (pos < ffm->write_index) {
57 avail_size = ffm->write_index - pos;
59 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
62 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
63 if (size <= avail_size)
65 else if (ffm->server_attached)
66 return AVERROR(EAGAIN);
68 return AVERROR_INVALIDDATA;
71 static int ffm_resync(AVFormatContext *s, uint32_t state)
73 av_log(s, AV_LOG_ERROR, "resyncing\n");
74 while (state != PACKET_ID) {
75 if (avio_feof(s->pb)) {
76 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
79 state = (state << 8) | avio_r8(s->pb);
84 /* first is true if we read the frame header */
85 static int ffm_read_data(AVFormatContext *s,
86 uint8_t *buf, int size, int header)
88 FFMContext *ffm = s->priv_data;
89 AVIOContext *pb = s->pb;
90 int len, fill_size, size1, frame_offset;
92 int64_t last_pos = -1;
97 len = ffm->packet_end - ffm->packet_ptr;
103 if (avio_tell(pb) == ffm->file_size) {
104 if (ffm->server_attached) {
105 avio_seek(pb, ffm->packet_size, SEEK_SET);
110 if (pb->buffer_size != ffm->packet_size) {
111 int64_t tell = avio_tell(pb);
112 int ret = ffio_set_buf_size(pb, ffm->packet_size);
115 avio_seek(pb, tell, SEEK_SET);
117 id = avio_rb16(pb); /* PACKET_ID */
118 if (id != PACKET_ID) {
119 if (ffm_resync(s, id) < 0)
121 last_pos = avio_tell(pb);
123 fill_size = avio_rb16(pb);
124 ffm->dts = avio_rb64(pb);
125 frame_offset = avio_rb16(pb);
126 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
127 if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
130 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
131 /* if first packet or resynchronization packet, we must
132 handle it specifically */
133 if (ffm->first_packet || (frame_offset & 0x8000)) {
135 /* This packet has no frame headers in it */
136 if (avio_tell(pb) >= ffm->packet_size * 3LL) {
137 int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
138 seekback = FFMAX(seekback, 0);
139 avio_seek(pb, -seekback, SEEK_CUR);
142 /* This is bad, we cannot find a valid frame header */
145 ffm->first_packet = 0;
146 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
147 ffm->packet_end = ffm->packet_ptr;
150 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
154 ffm->packet_ptr = ffm->packet;
158 memcpy(buf, ffm->packet_ptr, len);
160 ffm->packet_ptr += len;
167 /* ensure that actual seeking happens between FFM_PACKET_SIZE
168 and file_size - FFM_PACKET_SIZE */
169 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
171 FFMContext *ffm = s->priv_data;
172 AVIOContext *pb = s->pb;
175 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
176 pos = FFMAX(pos, FFM_PACKET_SIZE);
177 ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
178 return avio_seek(pb, pos, SEEK_SET);
181 static int64_t get_dts(AVFormatContext *s, int64_t pos)
183 AVIOContext *pb = s->pb;
189 ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
193 static void adjust_write_index(AVFormatContext *s)
195 FFMContext *ffm = s->priv_data;
196 AVIOContext *pb = s->pb;
198 //int64_t orig_write_index = ffm->write_index;
199 int64_t pos_min, pos_max;
201 int64_t ptr = avio_tell(pb);
205 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
207 pts_start = get_dts(s, pos_min);
209 pts = get_dts(s, pos_max);
211 if (pts - 100000 > pts_start)
214 ffm->write_index = FFM_PACKET_SIZE;
216 pts_start = get_dts(s, pos_min);
218 pts = get_dts(s, pos_max);
220 if (pts - 100000 <= pts_start) {
225 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
227 if (newpos == pos_min)
230 newpts = get_dts(s, newpos);
232 if (newpts - 100000 <= pts) {
239 ffm->write_index += pos_max;
243 avio_seek(pb, ptr, SEEK_SET);
247 static int ffm_close(AVFormatContext *s)
251 for (i = 0; i < s->nb_streams; i++)
252 av_freep(&s->streams[i]->codec->rc_eq);
257 static int ffm_append_recommended_configuration(AVStream *st, char **conf)
261 av_assert0(conf && st);
264 if (!st->recommended_encoder_configuration) {
265 st->recommended_encoder_configuration = *conf;
269 newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
270 if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
272 av_strlcat(st->recommended_encoder_configuration, ",", newsize);
273 av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
278 static int ffm2_read_header(AVFormatContext *s)
280 FFMContext *ffm = s->priv_data;
282 AVIOContext *pb = s->pb;
283 AVCodecContext *codec;
284 const AVCodecDescriptor *codec_desc;
286 int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
290 ffm->packet_size = avio_rb32(pb);
291 if (ffm->packet_size != FFM_PACKET_SIZE) {
292 av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
293 ffm->packet_size, FFM_PACKET_SIZE);
294 ret = AVERROR_INVALIDDATA;
298 ffm->write_index = avio_rb64(pb);
299 /* get also filesize */
301 ffm->file_size = avio_size(pb);
302 if (ffm->write_index && 0)
303 adjust_write_index(s);
305 ffm->file_size = (UINT64_C(1) << 63) - 1;
308 while(!avio_feof(pb)) {
309 unsigned id = avio_rb32(pb);
310 unsigned size = avio_rb32(pb);
311 int64_t next = avio_tell(pb) + size;
318 case MKBETAG('M', 'A', 'I', 'N'):
320 ret = AVERROR(EINVAL);
323 avio_rb32(pb); /* nb_streams */
324 avio_rb32(pb); /* total bitrate */
326 case MKBETAG('C', 'O', 'M', 'M'):
327 f_cprv = f_stvi = f_stau = 0;
328 st = avformat_new_stream(s, NULL);
330 ret = AVERROR(ENOMEM);
334 avpriv_set_pts_info(st, 64, 1, 1000000);
338 codec->codec_id = avio_rb32(pb);
339 codec_desc = avcodec_descriptor_get(codec->codec_id);
341 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
342 codec->codec_id = AV_CODEC_ID_NONE;
345 codec->codec_type = avio_r8(pb);
346 if (codec->codec_type != codec_desc->type) {
347 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
348 codec_desc->type, codec->codec_type);
349 codec->codec_id = AV_CODEC_ID_NONE;
350 codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
353 codec->bit_rate = avio_rb32(pb);
354 codec->flags = avio_rb32(pb);
355 codec->flags2 = avio_rb32(pb);
356 codec->debug = avio_rb32(pb);
357 if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
358 if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
359 return AVERROR(ENOMEM);
362 case MKBETAG('S', 'T', 'V', 'I'):
364 ret = AVERROR(EINVAL);
367 codec->time_base.num = avio_rb32(pb);
368 codec->time_base.den = avio_rb32(pb);
369 if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
370 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
371 codec->time_base.num, codec->time_base.den);
372 ret = AVERROR_INVALIDDATA;
375 codec->width = avio_rb16(pb);
376 codec->height = avio_rb16(pb);
377 codec->gop_size = avio_rb16(pb);
378 codec->pix_fmt = avio_rb32(pb);
379 codec->qmin = avio_r8(pb);
380 codec->qmax = avio_r8(pb);
381 codec->max_qdiff = avio_r8(pb);
382 codec->qcompress = avio_rb16(pb) / 10000.0;
383 codec->qblur = avio_rb16(pb) / 10000.0;
384 codec->bit_rate_tolerance = avio_rb32(pb);
385 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
386 codec->rc_eq = av_strdup(rc_eq_buf);
387 codec->rc_max_rate = avio_rb32(pb);
388 codec->rc_min_rate = avio_rb32(pb);
389 codec->rc_buffer_size = avio_rb32(pb);
390 codec->i_quant_factor = av_int2double(avio_rb64(pb));
391 codec->b_quant_factor = av_int2double(avio_rb64(pb));
392 codec->i_quant_offset = av_int2double(avio_rb64(pb));
393 codec->b_quant_offset = av_int2double(avio_rb64(pb));
394 codec->dct_algo = avio_rb32(pb);
395 codec->strict_std_compliance = avio_rb32(pb);
396 codec->max_b_frames = avio_rb32(pb);
397 codec->mpeg_quant = avio_rb32(pb);
398 codec->intra_dc_precision = avio_rb32(pb);
399 codec->me_method = avio_rb32(pb);
400 codec->mb_decision = avio_rb32(pb);
401 codec->nsse_weight = avio_rb32(pb);
402 codec->frame_skip_cmp = avio_rb32(pb);
403 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
404 codec->codec_tag = avio_rb32(pb);
405 codec->thread_count = avio_r8(pb);
406 codec->coder_type = avio_rb32(pb);
407 codec->me_cmp = avio_rb32(pb);
408 codec->me_subpel_quality = avio_rb32(pb);
409 codec->me_range = avio_rb32(pb);
410 codec->keyint_min = avio_rb32(pb);
411 codec->scenechange_threshold = avio_rb32(pb);
412 codec->b_frame_strategy = avio_rb32(pb);
413 codec->qcompress = av_int2double(avio_rb64(pb));
414 codec->qblur = av_int2double(avio_rb64(pb));
415 codec->max_qdiff = avio_rb32(pb);
416 codec->refs = avio_rb32(pb);
418 case MKBETAG('S', 'T', 'A', 'U'):
420 ret = AVERROR(EINVAL);
423 codec->sample_rate = avio_rb32(pb);
424 codec->channels = avio_rl16(pb);
425 codec->frame_size = avio_rl16(pb);
427 case MKBETAG('C', 'P', 'R', 'V'):
429 ret = AVERROR(EINVAL);
432 enc = avcodec_find_encoder(codec->codec_id);
433 if (enc && enc->priv_data_size && enc->priv_class) {
434 buffer = av_malloc(size + 1);
436 ret = AVERROR(ENOMEM);
439 avio_get_str(pb, size, buffer, size + 1);
440 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
444 case MKBETAG('S', '2', 'V', 'I'):
445 if (f_stvi++ || !size) {
446 ret = AVERROR(EINVAL);
449 buffer = av_malloc(size);
451 ret = AVERROR(ENOMEM);
454 avio_get_str(pb, INT_MAX, buffer, size);
455 av_set_options_string(codec, buffer, "=", ",");
456 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
459 case MKBETAG('S', '2', 'A', 'U'):
460 if (f_stau++ || !size) {
461 ret = AVERROR(EINVAL);
464 buffer = av_malloc(size);
466 ret = AVERROR(ENOMEM);
469 avio_get_str(pb, INT_MAX, buffer, size);
470 av_set_options_string(codec, buffer, "=", ",");
471 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
475 avio_seek(pb, next, SEEK_SET);
478 /* get until end of block reached */
479 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
482 /* init packet demux */
483 ffm->packet_ptr = ffm->packet;
484 ffm->packet_end = ffm->packet;
485 ffm->frame_offset = 0;
487 ffm->read_state = READ_HEADER;
488 ffm->first_packet = 1;
495 static int ffm_read_header(AVFormatContext *s)
497 FFMContext *ffm = s->priv_data;
499 AVIOContext *pb = s->pb;
500 AVCodecContext *codec;
501 const AVCodecDescriptor *codec_desc;
507 if (tag == MKTAG('F', 'F', 'M', '2'))
508 return ffm2_read_header(s);
509 if (tag != MKTAG('F', 'F', 'M', '1'))
511 ffm->packet_size = avio_rb32(pb);
512 if (ffm->packet_size != FFM_PACKET_SIZE)
514 ffm->write_index = avio_rb64(pb);
515 /* get also filesize */
517 ffm->file_size = avio_size(pb);
518 if (ffm->write_index && 0)
519 adjust_write_index(s);
521 ffm->file_size = (UINT64_C(1) << 63) - 1;
524 nb_streams = avio_rb32(pb);
525 avio_rb32(pb); /* total bitrate */
526 /* read each stream */
527 for(i=0;i<nb_streams;i++) {
530 st = avformat_new_stream(s, NULL);
534 avpriv_set_pts_info(st, 64, 1, 1000000);
538 codec->codec_id = avio_rb32(pb);
539 codec_desc = avcodec_descriptor_get(codec->codec_id);
541 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
542 codec->codec_id = AV_CODEC_ID_NONE;
545 codec->codec_type = avio_r8(pb); /* codec_type */
546 if (codec->codec_type != codec_desc->type) {
547 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
548 codec_desc->type, codec->codec_type);
549 codec->codec_id = AV_CODEC_ID_NONE;
550 codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
553 codec->bit_rate = avio_rb32(pb);
554 codec->flags = avio_rb32(pb);
555 codec->flags2 = avio_rb32(pb);
556 codec->debug = avio_rb32(pb);
558 switch(codec->codec_type) {
559 case AVMEDIA_TYPE_VIDEO:
560 codec->time_base.num = avio_rb32(pb);
561 codec->time_base.den = avio_rb32(pb);
562 if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
563 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
564 codec->time_base.num, codec->time_base.den);
567 codec->width = avio_rb16(pb);
568 codec->height = avio_rb16(pb);
569 codec->gop_size = avio_rb16(pb);
570 codec->pix_fmt = avio_rb32(pb);
571 codec->qmin = avio_r8(pb);
572 codec->qmax = avio_r8(pb);
573 codec->max_qdiff = avio_r8(pb);
574 codec->qcompress = avio_rb16(pb) / 10000.0;
575 codec->qblur = avio_rb16(pb) / 10000.0;
576 codec->bit_rate_tolerance = avio_rb32(pb);
577 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
578 codec->rc_eq = av_strdup(rc_eq_buf);
579 codec->rc_max_rate = avio_rb32(pb);
580 codec->rc_min_rate = avio_rb32(pb);
581 codec->rc_buffer_size = avio_rb32(pb);
582 codec->i_quant_factor = av_int2double(avio_rb64(pb));
583 codec->b_quant_factor = av_int2double(avio_rb64(pb));
584 codec->i_quant_offset = av_int2double(avio_rb64(pb));
585 codec->b_quant_offset = av_int2double(avio_rb64(pb));
586 codec->dct_algo = avio_rb32(pb);
587 codec->strict_std_compliance = avio_rb32(pb);
588 codec->max_b_frames = avio_rb32(pb);
589 codec->mpeg_quant = avio_rb32(pb);
590 codec->intra_dc_precision = avio_rb32(pb);
591 codec->me_method = avio_rb32(pb);
592 codec->mb_decision = avio_rb32(pb);
593 codec->nsse_weight = avio_rb32(pb);
594 codec->frame_skip_cmp = avio_rb32(pb);
595 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
596 codec->codec_tag = avio_rb32(pb);
597 codec->thread_count = avio_r8(pb);
598 codec->coder_type = avio_rb32(pb);
599 codec->me_cmp = avio_rb32(pb);
600 codec->me_subpel_quality = avio_rb32(pb);
601 codec->me_range = avio_rb32(pb);
602 codec->keyint_min = avio_rb32(pb);
603 codec->scenechange_threshold = avio_rb32(pb);
604 codec->b_frame_strategy = avio_rb32(pb);
605 codec->qcompress = av_int2double(avio_rb64(pb));
606 codec->qblur = av_int2double(avio_rb64(pb));
607 codec->max_qdiff = avio_rb32(pb);
608 codec->refs = avio_rb32(pb);
610 case AVMEDIA_TYPE_AUDIO:
611 codec->sample_rate = avio_rb32(pb);
612 codec->channels = avio_rl16(pb);
613 codec->frame_size = avio_rl16(pb);
618 if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
619 if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
620 return AVERROR(ENOMEM);
624 /* get until end of block reached */
625 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
628 /* init packet demux */
629 ffm->packet_ptr = ffm->packet;
630 ffm->packet_end = ffm->packet;
631 ffm->frame_offset = 0;
633 ffm->read_state = READ_HEADER;
634 ffm->first_packet = 1;
641 /* return < 0 if eof */
642 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
645 FFMContext *ffm = s->priv_data;
648 switch(ffm->read_state) {
650 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
653 ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
654 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
655 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
658 if (ffm->header[1] & FLAG_DTS)
659 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
661 ffm->read_state = READ_DATA;
664 size = AV_RB24(ffm->header + 2);
665 if ((ret = ffm_is_avail_data(s, size)) < 0)
668 duration = AV_RB24(ffm->header + 5);
670 if (av_new_packet(pkt, size) < 0) {
671 return AVERROR(ENOMEM);
673 pkt->stream_index = ffm->header[0];
674 if ((unsigned)pkt->stream_index >= s->nb_streams) {
675 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
676 av_packet_unref(pkt);
677 ffm->read_state = READ_HEADER;
680 pkt->pos = avio_tell(s->pb);
681 if (ffm->header[1] & FLAG_KEY_FRAME)
682 pkt->flags |= AV_PKT_FLAG_KEY;
684 ffm->read_state = READ_HEADER;
685 if (ffm_read_data(s, pkt->data, size, 0) != size) {
686 /* bad case: desynchronized packet. we cancel all the packet loading */
687 av_packet_unref(pkt);
690 pkt->pts = AV_RB64(ffm->header+8);
691 if (ffm->header[1] & FLAG_DTS)
692 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
695 pkt->duration = duration;
701 /* seek to a given time in the file. The file read pointer is
702 positioned at or before pts. XXX: the following code is quite
704 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
706 FFMContext *ffm = s->priv_data;
707 int64_t pos_min, pos_max, pos;
708 int64_t pts_min, pts_max, pts;
711 ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
712 /* find the position using linear interpolation (better than
713 dichotomy in typical cases) */
714 if (ffm->write_index && ffm->write_index < ffm->file_size) {
715 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
716 pos_min = FFM_PACKET_SIZE;
717 pos_max = ffm->write_index - FFM_PACKET_SIZE;
719 pos_min = ffm->write_index;
720 pos_max = ffm->file_size - FFM_PACKET_SIZE;
723 pos_min = FFM_PACKET_SIZE;
724 pos_max = ffm->file_size - FFM_PACKET_SIZE;
726 while (pos_min <= pos_max) {
727 pts_min = get_dts(s, pos_min);
728 pts_max = get_dts(s, pos_max);
729 if (pts_min > wanted_pts || pts_max <= wanted_pts) {
730 pos = pts_min > wanted_pts ? pos_min : pos_max;
733 /* linear interpolation */
734 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
735 (double)(pts_max - pts_min);
736 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
739 else if (pos >= pos_max)
741 pts = get_dts(s, pos);
742 /* check if we are lucky */
743 if (pts == wanted_pts) {
745 } else if (pts > wanted_pts) {
746 pos_max = pos - FFM_PACKET_SIZE;
748 pos_min = pos + FFM_PACKET_SIZE;
751 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
754 if (ffm_seek1(s, pos) < 0)
757 /* reset read state */
758 ffm->read_state = READ_HEADER;
759 ffm->packet_ptr = ffm->packet;
760 ffm->packet_end = ffm->packet;
761 ffm->first_packet = 1;
766 static int ffm_probe(AVProbeData *p)
769 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
770 (p->buf[3] == '1' || p->buf[3] == '2'))
771 return AVPROBE_SCORE_MAX + 1;
775 static const AVOption options[] = {
776 {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
777 {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
778 {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
782 static const AVClass ffm_class = {
783 .class_name = "ffm demuxer",
784 .item_name = av_default_item_name,
786 .version = LIBAVUTIL_VERSION_INT,
788 AVInputFormat ff_ffm_demuxer = {
790 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
791 .priv_data_size = sizeof(FFMContext),
792 .read_probe = ffm_probe,
793 .read_header = ffm_read_header,
794 .read_packet = ffm_read_packet,
795 .read_close = ffm_close,
796 .read_seek = ffm_seek,
797 .priv_class = &ffm_class,