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/intreadwrite.h"
25 #include "libavutil/intfloat.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
32 #include "avio_internal.h"
34 static int ffm_is_avail_data(AVFormatContext *s, int size)
36 FFMContext *ffm = s->priv_data;
37 int64_t pos, avail_size;
40 len = ffm->packet_end - ffm->packet_ptr;
43 pos = avio_tell(s->pb);
44 if (!ffm->write_index) {
45 if (pos == ffm->file_size)
47 avail_size = ffm->file_size - pos;
49 if (pos == ffm->write_index) {
50 /* exactly at the end of stream */
51 return AVERROR(EAGAIN);
52 } else if (pos < ffm->write_index) {
53 avail_size = ffm->write_index - pos;
55 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
58 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
59 if (size <= avail_size)
62 return AVERROR(EAGAIN);
65 static int ffm_resync(AVFormatContext *s, int state)
67 av_log(s, AV_LOG_ERROR, "resyncing\n");
68 while (state != PACKET_ID) {
69 if (avio_feof(s->pb)) {
70 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
73 state = (state << 8) | avio_r8(s->pb);
78 /* first is true if we read the frame header */
79 static int ffm_read_data(AVFormatContext *s,
80 uint8_t *buf, int size, int header)
82 FFMContext *ffm = s->priv_data;
83 AVIOContext *pb = s->pb;
84 int len, fill_size, size1, frame_offset, id;
85 int64_t last_pos = -1;
90 len = ffm->packet_end - ffm->packet_ptr;
96 if (avio_tell(pb) == ffm->file_size)
97 avio_seek(pb, ffm->packet_size, SEEK_SET);
99 if (pb->buffer_size != ffm->packet_size) {
100 int64_t tell = avio_tell(pb);
101 int ret = ffio_set_buf_size(pb, ffm->packet_size);
104 avio_seek(pb, tell, SEEK_SET);
106 id = avio_rb16(pb); /* PACKET_ID */
107 if (id != PACKET_ID) {
108 if (ffm_resync(s, id) < 0)
110 last_pos = avio_tell(pb);
112 fill_size = avio_rb16(pb);
113 ffm->dts = avio_rb64(pb);
114 frame_offset = avio_rb16(pb);
115 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
116 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
117 if (ffm->packet_end < ffm->packet || frame_offset < 0)
119 /* if first packet or resynchronization packet, we must
120 handle it specifically */
121 if (ffm->first_packet || (frame_offset & 0x8000)) {
123 /* This packet has no frame headers in it */
124 if (avio_tell(pb) >= ffm->packet_size * 3LL) {
125 int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
126 seekback = FFMAX(seekback, 0);
127 avio_seek(pb, -seekback, SEEK_CUR);
130 /* This is bad, we cannot find a valid frame header */
133 ffm->first_packet = 0;
134 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
136 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
140 ffm->packet_ptr = ffm->packet;
144 memcpy(buf, ffm->packet_ptr, len);
146 ffm->packet_ptr += len;
153 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
154 and file_size - FFM_PACKET_SIZE */
155 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
157 FFMContext *ffm = s->priv_data;
158 AVIOContext *pb = s->pb;
161 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
162 pos = FFMAX(pos, FFM_PACKET_SIZE);
163 av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
164 return avio_seek(pb, pos, SEEK_SET);
167 static int64_t get_dts(AVFormatContext *s, int64_t pos)
169 AVIOContext *pb = s->pb;
175 av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
179 static void adjust_write_index(AVFormatContext *s)
181 FFMContext *ffm = s->priv_data;
182 AVIOContext *pb = s->pb;
184 //int64_t orig_write_index = ffm->write_index;
185 int64_t pos_min, pos_max;
187 int64_t ptr = avio_tell(pb);
191 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
193 pts_start = get_dts(s, pos_min);
195 pts = get_dts(s, pos_max);
197 if (pts - 100000 > pts_start)
200 ffm->write_index = FFM_PACKET_SIZE;
202 pts_start = get_dts(s, pos_min);
204 pts = get_dts(s, pos_max);
206 if (pts - 100000 <= pts_start) {
211 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
213 if (newpos == pos_min)
216 newpts = get_dts(s, newpos);
218 if (newpts - 100000 <= pts) {
225 ffm->write_index += pos_max;
229 avio_seek(pb, ptr, SEEK_SET);
233 static int ffm_close(AVFormatContext *s)
237 for (i = 0; i < s->nb_streams; i++)
238 av_freep(&s->streams[i]->codec->rc_eq);
243 static int ffm_append_recommended_configuration(AVStream *st, char **conf)
247 av_assert0(conf && st);
250 if (!st->recommended_encoder_configuration) {
251 st->recommended_encoder_configuration = *conf;
255 newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
256 if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
258 av_strlcat(st->recommended_encoder_configuration, ",", newsize);
259 av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
264 static int ffm2_read_header(AVFormatContext *s)
266 FFMContext *ffm = s->priv_data;
268 AVIOContext *pb = s->pb;
269 AVCodecContext *codec;
271 int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
275 ffm->packet_size = avio_rb32(pb);
276 if (ffm->packet_size != FFM_PACKET_SIZE) {
277 av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
278 ffm->packet_size, FFM_PACKET_SIZE);
279 ret = AVERROR_INVALIDDATA;
283 ffm->write_index = avio_rb64(pb);
284 /* get also filesize */
286 ffm->file_size = avio_size(pb);
287 if (ffm->write_index && 0)
288 adjust_write_index(s);
290 ffm->file_size = (UINT64_C(1) << 63) - 1;
293 while(!avio_feof(pb)) {
294 unsigned id = avio_rb32(pb);
295 unsigned size = avio_rb32(pb);
296 int64_t next = avio_tell(pb) + size;
303 case MKBETAG('M', 'A', 'I', 'N'):
305 ret = AVERROR(EINVAL);
308 avio_rb32(pb); /* nb_streams */
309 avio_rb32(pb); /* total bitrate */
311 case MKBETAG('C', 'O', 'M', 'M'):
312 f_cprv = f_stvi = f_stau = 0;
313 st = avformat_new_stream(s, NULL);
315 ret = AVERROR(ENOMEM);
319 avpriv_set_pts_info(st, 64, 1, 1000000);
323 codec->codec_id = avio_rb32(pb);
324 codec->codec_type = avio_r8(pb);
325 codec->bit_rate = avio_rb32(pb);
326 codec->flags = avio_rb32(pb);
327 codec->flags2 = avio_rb32(pb);
328 codec->debug = avio_rb32(pb);
329 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
330 if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
331 return AVERROR(ENOMEM);
334 case MKBETAG('S', 'T', 'V', 'I'):
336 ret = AVERROR(EINVAL);
339 codec->time_base.num = avio_rb32(pb);
340 codec->time_base.den = avio_rb32(pb);
341 if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
342 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
343 codec->time_base.num, codec->time_base.den);
344 ret = AVERROR_INVALIDDATA;
347 codec->width = avio_rb16(pb);
348 codec->height = avio_rb16(pb);
349 codec->gop_size = avio_rb16(pb);
350 codec->pix_fmt = avio_rb32(pb);
351 codec->qmin = avio_r8(pb);
352 codec->qmax = avio_r8(pb);
353 codec->max_qdiff = avio_r8(pb);
354 codec->qcompress = avio_rb16(pb) / 10000.0;
355 codec->qblur = avio_rb16(pb) / 10000.0;
356 codec->bit_rate_tolerance = avio_rb32(pb);
357 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
358 codec->rc_eq = av_strdup(rc_eq_buf);
359 codec->rc_max_rate = avio_rb32(pb);
360 codec->rc_min_rate = avio_rb32(pb);
361 codec->rc_buffer_size = avio_rb32(pb);
362 codec->i_quant_factor = av_int2double(avio_rb64(pb));
363 codec->b_quant_factor = av_int2double(avio_rb64(pb));
364 codec->i_quant_offset = av_int2double(avio_rb64(pb));
365 codec->b_quant_offset = av_int2double(avio_rb64(pb));
366 codec->dct_algo = avio_rb32(pb);
367 codec->strict_std_compliance = avio_rb32(pb);
368 codec->max_b_frames = avio_rb32(pb);
369 codec->mpeg_quant = avio_rb32(pb);
370 codec->intra_dc_precision = avio_rb32(pb);
371 codec->me_method = avio_rb32(pb);
372 codec->mb_decision = avio_rb32(pb);
373 codec->nsse_weight = avio_rb32(pb);
374 codec->frame_skip_cmp = avio_rb32(pb);
375 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
376 codec->codec_tag = avio_rb32(pb);
377 codec->thread_count = avio_r8(pb);
378 codec->coder_type = avio_rb32(pb);
379 codec->me_cmp = avio_rb32(pb);
380 codec->me_subpel_quality = avio_rb32(pb);
381 codec->me_range = avio_rb32(pb);
382 codec->keyint_min = avio_rb32(pb);
383 codec->scenechange_threshold = avio_rb32(pb);
384 codec->b_frame_strategy = avio_rb32(pb);
385 codec->qcompress = av_int2double(avio_rb64(pb));
386 codec->qblur = av_int2double(avio_rb64(pb));
387 codec->max_qdiff = avio_rb32(pb);
388 codec->refs = avio_rb32(pb);
390 case MKBETAG('S', 'T', 'A', 'U'):
392 ret = AVERROR(EINVAL);
395 codec->sample_rate = avio_rb32(pb);
396 codec->channels = avio_rl16(pb);
397 codec->frame_size = avio_rl16(pb);
399 case MKBETAG('C', 'P', 'R', 'V'):
401 ret = AVERROR(EINVAL);
404 enc = avcodec_find_encoder(codec->codec_id);
405 if (enc && enc->priv_data_size && enc->priv_class) {
406 buffer = av_malloc(size + 1);
408 ret = AVERROR(ENOMEM);
411 avio_get_str(pb, size, buffer, size + 1);
412 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
416 case MKBETAG('S', '2', 'V', 'I'):
418 ret = AVERROR(EINVAL);
421 buffer = av_malloc(size);
423 ret = AVERROR(ENOMEM);
426 avio_get_str(pb, INT_MAX, buffer, size);
427 av_set_options_string(codec, buffer, "=", ",");
428 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
431 case MKBETAG('S', '2', 'A', 'U'):
433 ret = AVERROR(EINVAL);
436 buffer = av_malloc(size);
438 ret = AVERROR(ENOMEM);
441 avio_get_str(pb, INT_MAX, buffer, size);
442 av_set_options_string(codec, buffer, "=", ",");
443 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
447 avio_seek(pb, next, SEEK_SET);
450 /* get until end of block reached */
451 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
454 /* init packet demux */
455 ffm->packet_ptr = ffm->packet;
456 ffm->packet_end = ffm->packet;
457 ffm->frame_offset = 0;
459 ffm->read_state = READ_HEADER;
460 ffm->first_packet = 1;
467 static int ffm_read_header(AVFormatContext *s)
469 FFMContext *ffm = s->priv_data;
471 AVIOContext *pb = s->pb;
472 AVCodecContext *codec;
478 if (tag == MKTAG('F', 'F', 'M', '2'))
479 return ffm2_read_header(s);
480 if (tag != MKTAG('F', 'F', 'M', '1'))
482 ffm->packet_size = avio_rb32(pb);
483 if (ffm->packet_size != FFM_PACKET_SIZE)
485 ffm->write_index = avio_rb64(pb);
486 /* get also filesize */
488 ffm->file_size = avio_size(pb);
489 if (ffm->write_index && 0)
490 adjust_write_index(s);
492 ffm->file_size = (UINT64_C(1) << 63) - 1;
495 nb_streams = avio_rb32(pb);
496 avio_rb32(pb); /* total bitrate */
497 /* read each stream */
498 for(i=0;i<nb_streams;i++) {
501 st = avformat_new_stream(s, NULL);
505 avpriv_set_pts_info(st, 64, 1, 1000000);
509 codec->codec_id = avio_rb32(pb);
510 codec->codec_type = avio_r8(pb); /* codec_type */
511 codec->bit_rate = avio_rb32(pb);
512 codec->flags = avio_rb32(pb);
513 codec->flags2 = avio_rb32(pb);
514 codec->debug = avio_rb32(pb);
516 switch(codec->codec_type) {
517 case AVMEDIA_TYPE_VIDEO:
518 codec->time_base.num = avio_rb32(pb);
519 codec->time_base.den = avio_rb32(pb);
520 if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
521 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
522 codec->time_base.num, codec->time_base.den);
525 codec->width = avio_rb16(pb);
526 codec->height = avio_rb16(pb);
527 codec->gop_size = avio_rb16(pb);
528 codec->pix_fmt = avio_rb32(pb);
529 codec->qmin = avio_r8(pb);
530 codec->qmax = avio_r8(pb);
531 codec->max_qdiff = avio_r8(pb);
532 codec->qcompress = avio_rb16(pb) / 10000.0;
533 codec->qblur = avio_rb16(pb) / 10000.0;
534 codec->bit_rate_tolerance = avio_rb32(pb);
535 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
536 codec->rc_eq = av_strdup(rc_eq_buf);
537 codec->rc_max_rate = avio_rb32(pb);
538 codec->rc_min_rate = avio_rb32(pb);
539 codec->rc_buffer_size = avio_rb32(pb);
540 codec->i_quant_factor = av_int2double(avio_rb64(pb));
541 codec->b_quant_factor = av_int2double(avio_rb64(pb));
542 codec->i_quant_offset = av_int2double(avio_rb64(pb));
543 codec->b_quant_offset = av_int2double(avio_rb64(pb));
544 codec->dct_algo = avio_rb32(pb);
545 codec->strict_std_compliance = avio_rb32(pb);
546 codec->max_b_frames = avio_rb32(pb);
547 codec->mpeg_quant = avio_rb32(pb);
548 codec->intra_dc_precision = avio_rb32(pb);
549 codec->me_method = avio_rb32(pb);
550 codec->mb_decision = avio_rb32(pb);
551 codec->nsse_weight = avio_rb32(pb);
552 codec->frame_skip_cmp = avio_rb32(pb);
553 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
554 codec->codec_tag = avio_rb32(pb);
555 codec->thread_count = avio_r8(pb);
556 codec->coder_type = avio_rb32(pb);
557 codec->me_cmp = avio_rb32(pb);
558 codec->me_subpel_quality = avio_rb32(pb);
559 codec->me_range = avio_rb32(pb);
560 codec->keyint_min = avio_rb32(pb);
561 codec->scenechange_threshold = avio_rb32(pb);
562 codec->b_frame_strategy = avio_rb32(pb);
563 codec->qcompress = av_int2double(avio_rb64(pb));
564 codec->qblur = av_int2double(avio_rb64(pb));
565 codec->max_qdiff = avio_rb32(pb);
566 codec->refs = avio_rb32(pb);
568 case AVMEDIA_TYPE_AUDIO:
569 codec->sample_rate = avio_rb32(pb);
570 codec->channels = avio_rl16(pb);
571 codec->frame_size = avio_rl16(pb);
576 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
577 if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
578 return AVERROR(ENOMEM);
582 /* get until end of block reached */
583 while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
586 /* init packet demux */
587 ffm->packet_ptr = ffm->packet;
588 ffm->packet_end = ffm->packet;
589 ffm->frame_offset = 0;
591 ffm->read_state = READ_HEADER;
592 ffm->first_packet = 1;
599 /* return < 0 if eof */
600 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
603 FFMContext *ffm = s->priv_data;
606 switch(ffm->read_state) {
608 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
611 av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
612 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
613 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
616 if (ffm->header[1] & FLAG_DTS)
617 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
619 ffm->read_state = READ_DATA;
622 size = AV_RB24(ffm->header + 2);
623 if ((ret = ffm_is_avail_data(s, size)) < 0)
626 duration = AV_RB24(ffm->header + 5);
628 if (av_new_packet(pkt, size) < 0) {
629 return AVERROR(ENOMEM);
631 pkt->stream_index = ffm->header[0];
632 if ((unsigned)pkt->stream_index >= s->nb_streams) {
633 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
635 ffm->read_state = READ_HEADER;
638 pkt->pos = avio_tell(s->pb);
639 if (ffm->header[1] & FLAG_KEY_FRAME)
640 pkt->flags |= AV_PKT_FLAG_KEY;
642 ffm->read_state = READ_HEADER;
643 if (ffm_read_data(s, pkt->data, size, 0) != size) {
644 /* bad case: desynchronized packet. we cancel all the packet loading */
648 pkt->pts = AV_RB64(ffm->header+8);
649 if (ffm->header[1] & FLAG_DTS)
650 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
653 pkt->duration = duration;
659 /* seek to a given time in the file. The file read pointer is
660 positioned at or before pts. XXX: the following code is quite
662 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
664 FFMContext *ffm = s->priv_data;
665 int64_t pos_min, pos_max, pos;
666 int64_t pts_min, pts_max, pts;
669 av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
670 /* find the position using linear interpolation (better than
671 dichotomy in typical cases) */
672 if (ffm->write_index && ffm->write_index < ffm->file_size) {
673 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
674 pos_min = FFM_PACKET_SIZE;
675 pos_max = ffm->write_index - FFM_PACKET_SIZE;
677 pos_min = ffm->write_index;
678 pos_max = ffm->file_size - FFM_PACKET_SIZE;
681 pos_min = FFM_PACKET_SIZE;
682 pos_max = ffm->file_size - FFM_PACKET_SIZE;
684 while (pos_min <= pos_max) {
685 pts_min = get_dts(s, pos_min);
686 pts_max = get_dts(s, pos_max);
687 if (pts_min > wanted_pts || pts_max <= wanted_pts) {
688 pos = pts_min > wanted_pts ? pos_min : pos_max;
691 /* linear interpolation */
692 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
693 (double)(pts_max - pts_min);
694 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
697 else if (pos >= pos_max)
699 pts = get_dts(s, pos);
700 /* check if we are lucky */
701 if (pts == wanted_pts) {
703 } else if (pts > wanted_pts) {
704 pos_max = pos - FFM_PACKET_SIZE;
706 pos_min = pos + FFM_PACKET_SIZE;
709 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
712 if (ffm_seek1(s, pos) < 0)
715 /* reset read state */
716 ffm->read_state = READ_HEADER;
717 ffm->packet_ptr = ffm->packet;
718 ffm->packet_end = ffm->packet;
719 ffm->first_packet = 1;
724 static int ffm_probe(AVProbeData *p)
727 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
728 (p->buf[3] == '1' || p->buf[3] == '2'))
729 return AVPROBE_SCORE_MAX + 1;
733 AVInputFormat ff_ffm_demuxer = {
735 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
736 .priv_data_size = sizeof(FFMContext),
737 .read_probe = ffm_probe,
738 .read_header = ffm_read_header,
739 .read_packet = ffm_read_packet,
740 .read_close = ffm_close,
741 .read_seek = ffm_seek,