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/imgutils.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/intfloat.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavcodec/internal.h"
36 #include "avio_internal.h"
38 static int ffm_is_avail_data(AVFormatContext *s, int size)
40 FFMContext *ffm = s->priv_data;
41 int64_t pos, avail_size;
44 len = ffm->packet_end - ffm->packet_ptr;
47 pos = avio_tell(s->pb);
48 if (!ffm->write_index) {
49 if (pos == ffm->file_size)
51 avail_size = ffm->file_size - pos;
53 if (pos == ffm->write_index) {
54 /* exactly at the end of stream */
55 if (ffm->server_attached)
56 return AVERROR(EAGAIN);
58 return AVERROR_INVALIDDATA;
59 } else if (pos < ffm->write_index) {
60 avail_size = ffm->write_index - pos;
62 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
65 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
66 if (size <= avail_size)
68 else if (ffm->server_attached)
69 return AVERROR(EAGAIN);
71 return AVERROR_INVALIDDATA;
74 static int ffm_resync(AVFormatContext *s, uint32_t state)
76 av_log(s, AV_LOG_ERROR, "resyncing\n");
77 while (state != PACKET_ID) {
78 if (avio_feof(s->pb)) {
79 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
82 state = (state << 8) | avio_r8(s->pb);
87 /* first is true if we read the frame header */
88 static int ffm_read_data(AVFormatContext *s,
89 uint8_t *buf, int size, int header)
91 FFMContext *ffm = s->priv_data;
92 AVIOContext *pb = s->pb;
93 int fill_size, size1, frame_offset;
96 int64_t last_pos = -1;
101 len = ffm->packet_end - ffm->packet_ptr;
107 if (avio_tell(pb) == ffm->file_size) {
108 if (ffm->server_attached) {
109 avio_seek(pb, ffm->packet_size, SEEK_SET);
114 if (pb->buffer_size != ffm->packet_size) {
115 int64_t tell = avio_tell(pb);
116 int ret = ffio_set_buf_size(pb, ffm->packet_size);
119 avio_seek(pb, tell, SEEK_SET);
121 id = avio_rb16(pb); /* PACKET_ID */
122 if (id != PACKET_ID) {
123 if (ffm_resync(s, id) < 0)
125 last_pos = avio_tell(pb);
127 fill_size = avio_rb16(pb);
128 ffm->dts = avio_rb64(pb);
129 frame_offset = avio_rb16(pb);
130 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
131 if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
134 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
135 /* if first packet or resynchronization packet, we must
136 handle it specifically */
137 if (ffm->first_packet || (frame_offset & 0x8000)) {
139 /* This packet has no frame headers in it */
140 if (avio_tell(pb) >= ffm->packet_size * 3LL) {
141 int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
142 seekback = FFMAX(seekback, 0);
143 avio_seek(pb, -seekback, SEEK_CUR);
146 /* This is bad, we cannot find a valid frame header */
149 ffm->first_packet = 0;
150 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
151 ffm->packet_end = ffm->packet_ptr;
154 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
158 ffm->packet_ptr = ffm->packet;
162 memcpy(buf, ffm->packet_ptr, len);
164 ffm->packet_ptr += len;
171 /* ensure that actual seeking happens between FFM_PACKET_SIZE
172 and file_size - FFM_PACKET_SIZE */
173 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
175 FFMContext *ffm = s->priv_data;
176 AVIOContext *pb = s->pb;
179 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
180 pos = FFMAX(pos, FFM_PACKET_SIZE);
181 ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
182 return avio_seek(pb, pos, SEEK_SET);
185 static int64_t get_dts(AVFormatContext *s, int64_t pos)
187 AVIOContext *pb = s->pb;
193 ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
197 static void adjust_write_index(AVFormatContext *s)
199 FFMContext *ffm = s->priv_data;
200 AVIOContext *pb = s->pb;
202 //int64_t orig_write_index = ffm->write_index;
203 int64_t pos_min, pos_max;
205 int64_t ptr = avio_tell(pb);
209 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
211 pts_start = get_dts(s, pos_min);
213 pts = get_dts(s, pos_max);
215 if (pts - 100000 > pts_start)
218 ffm->write_index = FFM_PACKET_SIZE;
220 pts_start = get_dts(s, pos_min);
222 pts = get_dts(s, pos_max);
224 if (pts - 100000 <= pts_start) {
229 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
231 if (newpos == pos_min)
234 newpts = get_dts(s, newpos);
236 if (newpts - 100000 <= pts) {
243 ffm->write_index += pos_max;
247 avio_seek(pb, ptr, SEEK_SET);
251 static int ffm_append_recommended_configuration(AVStream *st, char **conf)
255 av_assert0(conf && st);
258 if (!st->recommended_encoder_configuration) {
259 st->recommended_encoder_configuration = *conf;
263 newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
264 if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
266 av_strlcat(st->recommended_encoder_configuration, ",", newsize);
267 av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
272 #define VALIDATE_PARAMETER(parameter, name, check) { \
274 av_log(s, AV_LOG_ERROR, "Invalid " name " %d\n", codecpar->parameter); \
275 ret = AVERROR_INVALIDDATA; \
280 static int ffm2_read_header(AVFormatContext *s)
282 FFMContext *ffm = s->priv_data;
284 AVIOContext *pb = s->pb;
285 AVCodecContext *dummy_codec = NULL;
286 AVCodecParameters *codecpar = NULL;
287 const AVCodecDescriptor *codec_desc;
289 int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
293 ffm->packet_size = avio_rb32(pb);
294 if (ffm->packet_size != FFM_PACKET_SIZE) {
295 av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
296 ffm->packet_size, FFM_PACKET_SIZE);
297 ret = AVERROR_INVALIDDATA;
301 ffm->write_index = avio_rb64(pb);
302 /* get also filesize */
303 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
304 ffm->file_size = avio_size(pb);
305 if (ffm->write_index && 0)
306 adjust_write_index(s);
308 ffm->file_size = (UINT64_C(1) << 63) - 1;
310 dummy_codec = avcodec_alloc_context3(NULL);
312 while(!avio_feof(pb)) {
313 unsigned id = avio_rb32(pb);
314 unsigned size = avio_rb32(pb);
315 int64_t next = avio_tell(pb) + size;
323 case MKBETAG('M', 'A', 'I', 'N'):
325 ret = AVERROR(EINVAL);
328 avio_rb32(pb); /* nb_streams */
329 avio_rb32(pb); /* total bitrate */
331 case MKBETAG('C', 'O', 'M', 'M'):
332 f_cprv = f_stvi = f_stau = 0;
333 st = avformat_new_stream(s, NULL);
335 ret = AVERROR(ENOMEM);
339 avpriv_set_pts_info(st, 64, 1, 1000000);
341 codecpar = st->codecpar;
343 codecpar->codec_id = avio_rb32(pb);
344 codec_desc = avcodec_descriptor_get(codecpar->codec_id);
346 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
347 codecpar->codec_id = AV_CODEC_ID_NONE;
348 ret = AVERROR_INVALIDDATA;
351 codecpar->codec_type = avio_r8(pb);
352 if (codecpar->codec_type != codec_desc->type) {
353 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
354 codec_desc->type, codecpar->codec_type);
355 codecpar->codec_id = AV_CODEC_ID_NONE;
356 codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
357 ret = AVERROR_INVALIDDATA;
360 codecpar->bit_rate = avio_rb32(pb);
361 if (codecpar->bit_rate < 0) {
362 av_log(s, AV_LOG_ERROR, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
363 ret = AVERROR_INVALIDDATA;
366 flags = avio_rb32(pb);
367 #if FF_API_LAVF_AVCTX
368 FF_DISABLE_DEPRECATION_WARNINGS
369 st->codec->flags = flags;
370 FF_ENABLE_DEPRECATION_WARNINGS
372 avio_rb32(pb); // flags2
373 avio_rb32(pb); // debug
374 if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
375 int size = avio_rb32(pb);
376 if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
377 av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
378 ret = AVERROR_INVALIDDATA;
381 codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
382 if (!codecpar->extradata) {
383 ret = AVERROR(ENOMEM);
386 codecpar->extradata_size = size;
387 avio_read(pb, codecpar->extradata, size);
390 case MKBETAG('S', 'T', 'V', 'I'):
391 if (f_stvi++ || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
392 ret = AVERROR(EINVAL);
395 avio_rb32(pb); // time_base.num
396 avio_rb32(pb); // time_base.den
397 codecpar->width = avio_rb16(pb);
398 codecpar->height = avio_rb16(pb);
399 ret = av_image_check_size(codecpar->width, codecpar->height, 0, s);
402 avio_rb16(pb); // gop_size
403 codecpar->format = avio_rb32(pb);
404 if (!av_pix_fmt_desc_get(codecpar->format)) {
405 av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
406 codecpar->format = AV_PIX_FMT_NONE;
407 ret = AVERROR_INVALIDDATA;
412 avio_r8(pb); // max_qdiff
413 avio_rb16(pb); // qcompress / 10000.0
414 avio_rb16(pb); // qblur / 10000.0
415 avio_rb32(pb); // bit_rate_tolerance
416 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
418 avio_rb32(pb); // rc_max_rate
419 avio_rb32(pb); // rc_min_rate
420 avio_rb32(pb); // rc_buffer_size
421 avio_rb64(pb); // i_quant_factor
422 avio_rb64(pb); // b_quant_factor
423 avio_rb64(pb); // i_quant_offset
424 avio_rb64(pb); // b_quant_offset
425 avio_rb32(pb); // dct_algo
426 avio_rb32(pb); // strict_std_compliance
427 avio_rb32(pb); // max_b_frames
428 avio_rb32(pb); // mpeg_quant
429 avio_rb32(pb); // intra_dc_precision
430 avio_rb32(pb); // me_method
431 avio_rb32(pb); // mb_decision
432 avio_rb32(pb); // nsse_weight
433 avio_rb32(pb); // frame_skip_cmp
434 avio_rb64(pb); // rc_buffer_aggressivity
435 codecpar->codec_tag = avio_rb32(pb);
436 avio_r8(pb); // thread_count
437 avio_rb32(pb); // coder_type
438 avio_rb32(pb); // me_cmp
439 avio_rb32(pb); // me_subpel_quality
440 avio_rb32(pb); // me_range
441 avio_rb32(pb); // keyint_min
442 avio_rb32(pb); // scenechange_threshold
443 avio_rb32(pb); // b_frame_strategy
444 avio_rb64(pb); // qcompress
445 avio_rb64(pb); // qblur
446 avio_rb32(pb); // max_qdiff
447 avio_rb32(pb); // refs
449 case MKBETAG('S', 'T', 'A', 'U'):
450 if (f_stau++ || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
451 ret = AVERROR(EINVAL);
454 codecpar->sample_rate = avio_rb32(pb);
455 VALIDATE_PARAMETER(sample_rate, "sample rate", codecpar->sample_rate < 0)
456 codecpar->channels = avio_rl16(pb);
457 VALIDATE_PARAMETER(channels, "number of channels", codecpar->channels < 0)
458 codecpar->frame_size = avio_rl16(pb);
459 VALIDATE_PARAMETER(frame_size, "frame size", codecpar->frame_size < 0)
461 case MKBETAG('C', 'P', 'R', 'V'):
463 ret = AVERROR(EINVAL);
466 enc = avcodec_find_encoder(codecpar->codec_id);
467 if (enc && enc->priv_data_size && enc->priv_class) {
468 buffer = av_malloc(size + 1);
470 ret = AVERROR(ENOMEM);
473 avio_get_str(pb, size, buffer, size + 1);
474 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
478 case MKBETAG('S', '2', 'V', 'I'):
479 if (f_stvi++ || !size || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
480 ret = AVERROR(EINVAL);
483 buffer = av_malloc(size);
485 ret = AVERROR(ENOMEM);
488 avio_get_str(pb, INT_MAX, buffer, size);
489 // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
490 avcodec_parameters_to_context(dummy_codec, codecpar);
491 av_set_options_string(dummy_codec, buffer, "=", ",");
492 avcodec_parameters_from_context(codecpar, dummy_codec);
493 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
496 case MKBETAG('S', '2', 'A', 'U'):
497 if (f_stau++ || !size || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
498 ret = AVERROR(EINVAL);
501 buffer = av_malloc(size);
503 ret = AVERROR(ENOMEM);
506 avio_get_str(pb, INT_MAX, buffer, size);
507 // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
508 avcodec_parameters_to_context(dummy_codec, codecpar);
509 av_set_options_string(dummy_codec, buffer, "=", ",");
510 avcodec_parameters_from_context(codecpar, dummy_codec);
511 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
515 avio_seek(pb, next, SEEK_SET);
518 /* get until end of block reached */
519 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
522 /* init packet demux */
523 ffm->packet_ptr = ffm->packet;
524 ffm->packet_end = ffm->packet;
525 ffm->frame_offset = 0;
527 ffm->read_state = READ_HEADER;
528 ffm->first_packet = 1;
529 avcodec_free_context(&dummy_codec);
532 avcodec_free_context(&dummy_codec);
536 static int ffm_read_header(AVFormatContext *s)
538 FFMContext *ffm = s->priv_data;
540 AVIOContext *pb = s->pb;
541 AVCodecContext *dummy_codec = NULL;
542 AVCodecParameters *codecpar;
543 const AVCodecDescriptor *codec_desc;
544 int i, nb_streams, ret;
549 if (tag == MKTAG('F', 'F', 'M', '2'))
550 return ffm2_read_header(s);
551 if (tag != MKTAG('F', 'F', 'M', '1')) {
552 ret = AVERROR_INVALIDDATA;
555 ffm->packet_size = avio_rb32(pb);
556 if (ffm->packet_size != FFM_PACKET_SIZE) {
557 ret = AVERROR_INVALIDDATA;
560 ffm->write_index = avio_rb64(pb);
561 /* get also filesize */
562 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
563 ffm->file_size = avio_size(pb);
564 if (ffm->write_index && 0)
565 adjust_write_index(s);
567 ffm->file_size = (UINT64_C(1) << 63) - 1;
569 dummy_codec = avcodec_alloc_context3(NULL);
571 nb_streams = avio_rb32(pb);
572 avio_rb32(pb); /* total bitrate */
573 /* read each stream */
574 for(i=0;i<nb_streams;i++) {
578 st = avformat_new_stream(s, NULL);
580 ret = AVERROR(ENOMEM);
584 avpriv_set_pts_info(st, 64, 1, 1000000);
586 codecpar = st->codecpar;
588 codecpar->codec_id = avio_rb32(pb);
589 codec_desc = avcodec_descriptor_get(codecpar->codec_id);
591 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
592 codecpar->codec_id = AV_CODEC_ID_NONE;
593 ret = AVERROR_INVALIDDATA;
596 codecpar->codec_type = avio_r8(pb); /* codec_type */
597 if (codecpar->codec_type != codec_desc->type) {
598 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
599 codec_desc->type, codecpar->codec_type);
600 codecpar->codec_id = AV_CODEC_ID_NONE;
601 codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
602 ret = AVERROR_INVALIDDATA;
605 codecpar->bit_rate = avio_rb32(pb);
606 if (codecpar->bit_rate < 0) {
607 av_log(s, AV_LOG_WARNING, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
608 ret = AVERROR_INVALIDDATA;
611 flags = avio_rb32(pb);
612 #if FF_API_LAVF_AVCTX
613 FF_DISABLE_DEPRECATION_WARNINGS
614 st->codec->flags = flags;
615 FF_ENABLE_DEPRECATION_WARNINGS
617 avio_rb32(pb); // flags2
618 avio_rb32(pb); // debug
620 switch(codecpar->codec_type) {
621 case AVMEDIA_TYPE_VIDEO:
622 avio_rb32(pb); // time_base.num
623 avio_rb32(pb); // time_base.den
624 codecpar->width = avio_rb16(pb);
625 codecpar->height = avio_rb16(pb);
626 if ((ret = av_image_check_size(codecpar->width, codecpar->height, 0, s)) < 0)
628 avio_rb16(pb); // gop_size
629 codecpar->format = avio_rb32(pb);
630 if (!av_pix_fmt_desc_get(codecpar->format)) {
631 av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
632 codecpar->format = AV_PIX_FMT_NONE;
633 ret = AVERROR_INVALIDDATA;
638 avio_r8(pb); // max_qdiff
639 avio_rb16(pb); // qcompress / 10000.0
640 avio_rb16(pb); // qblur / 10000.0
641 avio_rb32(pb); // bit_rate_tolerance
642 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
644 avio_rb32(pb); // rc_max_rate
645 avio_rb32(pb); // rc_min_rate
646 avio_rb32(pb); // rc_buffer_size
647 avio_rb64(pb); // i_quant_factor
648 avio_rb64(pb); // b_quant_factor
649 avio_rb64(pb); // i_quant_offset
650 avio_rb64(pb); // b_quant_offset
651 avio_rb32(pb); // dct_algo
652 avio_rb32(pb); // strict_std_compliance
653 avio_rb32(pb); // max_b_frames
654 avio_rb32(pb); // mpeg_quant
655 avio_rb32(pb); // intra_dc_precision
656 avio_rb32(pb); // me_method
657 avio_rb32(pb); // mb_decision
658 avio_rb32(pb); // nsse_weight
659 avio_rb32(pb); // frame_skip_cmp
660 avio_rb64(pb); // rc_buffer_aggressivity
661 codecpar->codec_tag = avio_rb32(pb);
662 avio_r8(pb); // thread_count
663 avio_rb32(pb); // coder_type
664 avio_rb32(pb); // me_cmp
665 avio_rb32(pb); // me_subpel_quality
666 avio_rb32(pb); // me_range
667 avio_rb32(pb); // keyint_min
668 avio_rb32(pb); // scenechange_threshold
669 avio_rb32(pb); // b_frame_strategy
670 avio_rb64(pb); // qcompress
671 avio_rb64(pb); // qblur
672 avio_rb32(pb); // max_qdiff
673 avio_rb32(pb); // refs
675 case AVMEDIA_TYPE_AUDIO:
676 codecpar->sample_rate = avio_rb32(pb);
677 VALIDATE_PARAMETER(sample_rate, "sample rate", codecpar->sample_rate < 0)
678 codecpar->channels = avio_rl16(pb);
679 VALIDATE_PARAMETER(channels, "number of channels", codecpar->channels < 0)
680 codecpar->frame_size = avio_rl16(pb);
681 VALIDATE_PARAMETER(frame_size, "frame size", codecpar->frame_size < 0)
684 ret = AVERROR_INVALIDDATA;
687 if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
688 int size = avio_rb32(pb);
689 if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
690 av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
691 ret = AVERROR_INVALIDDATA;
694 codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
695 if (!codecpar->extradata) {
696 ret = AVERROR(ENOMEM);
699 codecpar->extradata_size = size;
700 avio_read(pb, codecpar->extradata, size);
704 /* get until end of block reached */
705 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
708 /* init packet demux */
709 ffm->packet_ptr = ffm->packet;
710 ffm->packet_end = ffm->packet;
711 ffm->frame_offset = 0;
713 ffm->read_state = READ_HEADER;
714 ffm->first_packet = 1;
715 avcodec_free_context(&dummy_codec);
718 avcodec_free_context(&dummy_codec);
722 /* return < 0 if eof */
723 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
726 FFMContext *ffm = s->priv_data;
729 switch(ffm->read_state) {
731 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
734 ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
735 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
736 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
739 if (ffm->header[1] & FLAG_DTS)
740 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
742 ffm->read_state = READ_DATA;
745 size = AV_RB24(ffm->header + 2);
746 if ((ret = ffm_is_avail_data(s, size)) < 0)
749 duration = AV_RB24(ffm->header + 5);
751 if (av_new_packet(pkt, size) < 0) {
752 return AVERROR(ENOMEM);
754 pkt->stream_index = ffm->header[0];
755 if ((unsigned)pkt->stream_index >= s->nb_streams) {
756 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
757 av_packet_unref(pkt);
758 ffm->read_state = READ_HEADER;
761 pkt->pos = avio_tell(s->pb);
762 if (ffm->header[1] & FLAG_KEY_FRAME)
763 pkt->flags |= AV_PKT_FLAG_KEY;
765 ffm->read_state = READ_HEADER;
766 if (ffm_read_data(s, pkt->data, size, 0) != size) {
767 /* bad case: desynchronized packet. we cancel all the packet loading */
768 av_packet_unref(pkt);
771 pkt->pts = AV_RB64(ffm->header+8);
772 if (ffm->header[1] & FLAG_DTS)
773 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
776 pkt->duration = duration;
782 /* seek to a given time in the file. The file read pointer is
783 positioned at or before pts. XXX: the following code is quite
785 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
787 FFMContext *ffm = s->priv_data;
788 int64_t pos_min, pos_max, pos;
789 int64_t pts_min, pts_max, pts;
792 ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
793 /* find the position using linear interpolation (better than
794 dichotomy in typical cases) */
795 if (ffm->write_index && ffm->write_index < ffm->file_size) {
796 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
797 pos_min = FFM_PACKET_SIZE;
798 pos_max = ffm->write_index - FFM_PACKET_SIZE;
800 pos_min = ffm->write_index;
801 pos_max = ffm->file_size - FFM_PACKET_SIZE;
804 pos_min = FFM_PACKET_SIZE;
805 pos_max = ffm->file_size - FFM_PACKET_SIZE;
807 while (pos_min <= pos_max) {
808 pts_min = get_dts(s, pos_min);
809 pts_max = get_dts(s, pos_max);
810 if (pts_min > wanted_pts || pts_max <= wanted_pts) {
811 pos = pts_min > wanted_pts ? pos_min : pos_max;
814 /* linear interpolation */
815 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
816 (double)(pts_max - pts_min);
817 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
820 else if (pos >= pos_max)
822 pts = get_dts(s, pos);
823 /* check if we are lucky */
824 if (pts == wanted_pts) {
826 } else if (pts > wanted_pts) {
827 pos_max = pos - FFM_PACKET_SIZE;
829 pos_min = pos + FFM_PACKET_SIZE;
832 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
835 if (ffm_seek1(s, pos) < 0)
838 /* reset read state */
839 ffm->read_state = READ_HEADER;
840 ffm->packet_ptr = ffm->packet;
841 ffm->packet_end = ffm->packet;
842 ffm->first_packet = 1;
847 static int ffm_probe(AVProbeData *p)
850 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
851 (p->buf[3] == '1' || p->buf[3] == '2'))
852 return AVPROBE_SCORE_MAX + 1;
856 static const AVOption options[] = {
857 {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
858 {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
859 {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
863 static const AVClass ffm_class = {
864 .class_name = "ffm demuxer",
865 .item_name = av_default_item_name,
867 .version = LIBAVUTIL_VERSION_INT,
869 AVInputFormat ff_ffm_demuxer = {
871 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
872 .priv_data_size = sizeof(FFMContext),
873 .read_probe = ffm_probe,
874 .read_header = ffm_read_header,
875 .read_packet = ffm_read_packet,
876 .read_seek = ffm_seek,
877 .priv_class = &ffm_class,