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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/intfloat.h"
27 #include "avio_internal.h"
29 static int ffm_is_avail_data(AVFormatContext *s, int size)
31 FFMContext *ffm = s->priv_data;
32 int64_t pos, avail_size;
35 len = ffm->packet_end - ffm->packet_ptr;
38 pos = avio_tell(s->pb);
39 if (!ffm->write_index) {
40 if (pos == ffm->file_size)
42 avail_size = ffm->file_size - pos;
44 if (pos == ffm->write_index) {
45 /* exactly at the end of stream */
46 return AVERROR(EAGAIN);
47 } else if (pos < ffm->write_index) {
48 avail_size = ffm->write_index - pos;
50 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
53 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
54 if (size <= avail_size)
57 return AVERROR(EAGAIN);
60 static int ffm_resync(AVFormatContext *s, int state)
62 av_log(s, AV_LOG_ERROR, "resyncing\n");
63 while (state != PACKET_ID) {
64 if (url_feof(s->pb)) {
65 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
68 state = (state << 8) | avio_r8(s->pb);
73 /* first is true if we read the frame header */
74 static int ffm_read_data(AVFormatContext *s,
75 uint8_t *buf, int size, int header)
77 FFMContext *ffm = s->priv_data;
78 AVIOContext *pb = s->pb;
79 int len, fill_size, size1, frame_offset, id;
84 len = ffm->packet_end - ffm->packet_ptr;
90 if (avio_tell(pb) == ffm->file_size)
91 avio_seek(pb, ffm->packet_size, SEEK_SET);
93 if (pb->buffer_size != ffm->packet_size) {
94 int64_t tell = avio_tell(pb);
95 ffio_set_buf_size(pb, ffm->packet_size);
96 avio_seek(pb, tell, SEEK_SET);
98 id = avio_rb16(pb); /* PACKET_ID */
100 if (ffm_resync(s, id) < 0)
102 fill_size = avio_rb16(pb);
103 ffm->dts = avio_rb64(pb);
104 frame_offset = avio_rb16(pb);
105 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
106 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
107 if (ffm->packet_end < ffm->packet || frame_offset < 0)
109 /* if first packet or resynchronization packet, we must
110 handle it specifically */
111 if (ffm->first_packet || (frame_offset & 0x8000)) {
113 /* This packet has no frame headers in it */
114 if (avio_tell(pb) >= ffm->packet_size * 3LL) {
115 avio_seek(pb, -ffm->packet_size * 2LL, SEEK_CUR);
118 /* This is bad, we cannot find a valid frame header */
121 ffm->first_packet = 0;
122 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
124 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
128 ffm->packet_ptr = ffm->packet;
132 memcpy(buf, ffm->packet_ptr, len);
134 ffm->packet_ptr += len;
141 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
142 and file_size - FFM_PACKET_SIZE */
143 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
145 FFMContext *ffm = s->priv_data;
146 AVIOContext *pb = s->pb;
149 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
150 pos = FFMAX(pos, FFM_PACKET_SIZE);
151 av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
152 return avio_seek(pb, pos, SEEK_SET);
155 static int64_t get_dts(AVFormatContext *s, int64_t pos)
157 AVIOContext *pb = s->pb;
163 av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
167 static void adjust_write_index(AVFormatContext *s)
169 FFMContext *ffm = s->priv_data;
170 AVIOContext *pb = s->pb;
172 //int64_t orig_write_index = ffm->write_index;
173 int64_t pos_min, pos_max;
175 int64_t ptr = avio_tell(pb);
179 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
181 pts_start = get_dts(s, pos_min);
183 pts = get_dts(s, pos_max);
185 if (pts - 100000 > pts_start)
188 ffm->write_index = FFM_PACKET_SIZE;
190 pts_start = get_dts(s, pos_min);
192 pts = get_dts(s, pos_max);
194 if (pts - 100000 <= pts_start) {
199 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
201 if (newpos == pos_min)
204 newpts = get_dts(s, newpos);
206 if (newpts - 100000 <= pts) {
213 ffm->write_index += pos_max;
217 avio_seek(pb, ptr, SEEK_SET);
221 static int ffm_close(AVFormatContext *s)
225 for (i = 0; i < s->nb_streams; i++)
226 av_freep(&s->streams[i]->codec->rc_eq);
232 static int ffm_read_header(AVFormatContext *s)
234 FFMContext *ffm = s->priv_data;
236 AVIOContext *pb = s->pb;
237 AVCodecContext *codec;
243 if (tag != MKTAG('F', 'F', 'M', '1'))
245 ffm->packet_size = avio_rb32(pb);
246 if (ffm->packet_size != FFM_PACKET_SIZE)
248 ffm->write_index = avio_rb64(pb);
249 /* get also filesize */
251 ffm->file_size = avio_size(pb);
252 if (ffm->write_index && 0)
253 adjust_write_index(s);
255 ffm->file_size = (UINT64_C(1) << 63) - 1;
258 nb_streams = avio_rb32(pb);
259 avio_rb32(pb); /* total bitrate */
260 /* read each stream */
261 for(i=0;i<nb_streams;i++) {
264 st = avformat_new_stream(s, NULL);
268 avpriv_set_pts_info(st, 64, 1, 1000000);
272 codec->codec_id = avio_rb32(pb);
273 codec->codec_type = avio_r8(pb); /* codec_type */
274 codec->bit_rate = avio_rb32(pb);
275 codec->flags = avio_rb32(pb);
276 codec->flags2 = avio_rb32(pb);
277 codec->debug = avio_rb32(pb);
279 switch(codec->codec_type) {
280 case AVMEDIA_TYPE_VIDEO:
281 codec->time_base.num = avio_rb32(pb);
282 codec->time_base.den = avio_rb32(pb);
283 codec->width = avio_rb16(pb);
284 codec->height = avio_rb16(pb);
285 codec->gop_size = avio_rb16(pb);
286 codec->pix_fmt = avio_rb32(pb);
287 codec->qmin = avio_r8(pb);
288 codec->qmax = avio_r8(pb);
289 codec->max_qdiff = avio_r8(pb);
290 codec->qcompress = avio_rb16(pb) / 10000.0;
291 codec->qblur = avio_rb16(pb) / 10000.0;
292 codec->bit_rate_tolerance = avio_rb32(pb);
293 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
294 codec->rc_eq = av_strdup(rc_eq_buf);
295 codec->rc_max_rate = avio_rb32(pb);
296 codec->rc_min_rate = avio_rb32(pb);
297 codec->rc_buffer_size = avio_rb32(pb);
298 codec->i_quant_factor = av_int2double(avio_rb64(pb));
299 codec->b_quant_factor = av_int2double(avio_rb64(pb));
300 codec->i_quant_offset = av_int2double(avio_rb64(pb));
301 codec->b_quant_offset = av_int2double(avio_rb64(pb));
302 codec->dct_algo = avio_rb32(pb);
303 codec->strict_std_compliance = avio_rb32(pb);
304 codec->max_b_frames = avio_rb32(pb);
305 codec->mpeg_quant = avio_rb32(pb);
306 codec->intra_dc_precision = avio_rb32(pb);
307 codec->me_method = avio_rb32(pb);
308 codec->mb_decision = avio_rb32(pb);
309 codec->nsse_weight = avio_rb32(pb);
310 codec->frame_skip_cmp = avio_rb32(pb);
311 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
312 codec->codec_tag = avio_rb32(pb);
313 codec->thread_count = avio_r8(pb);
314 codec->coder_type = avio_rb32(pb);
315 codec->me_cmp = avio_rb32(pb);
316 codec->me_subpel_quality = avio_rb32(pb);
317 codec->me_range = avio_rb32(pb);
318 codec->keyint_min = avio_rb32(pb);
319 codec->scenechange_threshold = avio_rb32(pb);
320 codec->b_frame_strategy = avio_rb32(pb);
321 codec->qcompress = av_int2double(avio_rb64(pb));
322 codec->qblur = av_int2double(avio_rb64(pb));
323 codec->max_qdiff = avio_rb32(pb);
324 codec->refs = avio_rb32(pb);
326 case AVMEDIA_TYPE_AUDIO:
327 codec->sample_rate = avio_rb32(pb);
328 codec->channels = avio_rl16(pb);
329 codec->frame_size = avio_rl16(pb);
334 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
335 codec->extradata_size = avio_rb32(pb);
336 codec->extradata = av_malloc(codec->extradata_size);
337 if (!codec->extradata)
338 return AVERROR(ENOMEM);
339 avio_read(pb, codec->extradata, codec->extradata_size);
343 /* get until end of block reached */
344 while ((avio_tell(pb) % ffm->packet_size) != 0)
347 /* init packet demux */
348 ffm->packet_ptr = ffm->packet;
349 ffm->packet_end = ffm->packet;
350 ffm->frame_offset = 0;
352 ffm->read_state = READ_HEADER;
353 ffm->first_packet = 1;
360 /* return < 0 if eof */
361 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
364 FFMContext *ffm = s->priv_data;
367 switch(ffm->read_state) {
369 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
372 av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
373 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
374 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
377 if (ffm->header[1] & FLAG_DTS)
378 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
380 ffm->read_state = READ_DATA;
383 size = AV_RB24(ffm->header + 2);
384 if ((ret = ffm_is_avail_data(s, size)) < 0)
387 duration = AV_RB24(ffm->header + 5);
389 if (av_new_packet(pkt, size) < 0) {
390 return AVERROR(ENOMEM);
392 pkt->stream_index = ffm->header[0];
393 if ((unsigned)pkt->stream_index >= s->nb_streams) {
394 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
396 ffm->read_state = READ_HEADER;
399 pkt->pos = avio_tell(s->pb);
400 if (ffm->header[1] & FLAG_KEY_FRAME)
401 pkt->flags |= AV_PKT_FLAG_KEY;
403 ffm->read_state = READ_HEADER;
404 if (ffm_read_data(s, pkt->data, size, 0) != size) {
405 /* bad case: desynchronized packet. we cancel all the packet loading */
409 pkt->pts = AV_RB64(ffm->header+8);
410 if (ffm->header[1] & FLAG_DTS)
411 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
414 pkt->duration = duration;
420 /* seek to a given time in the file. The file read pointer is
421 positioned at or before pts. XXX: the following code is quite
423 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
425 FFMContext *ffm = s->priv_data;
426 int64_t pos_min, pos_max, pos;
427 int64_t pts_min, pts_max, pts;
430 av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
431 /* find the position using linear interpolation (better than
432 dichotomy in typical cases) */
433 if (ffm->write_index && ffm->write_index < ffm->file_size) {
434 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
435 pos_min = FFM_PACKET_SIZE;
436 pos_max = ffm->write_index - FFM_PACKET_SIZE;
438 pos_min = ffm->write_index;
439 pos_max = ffm->file_size - FFM_PACKET_SIZE;
442 pos_min = FFM_PACKET_SIZE;
443 pos_max = ffm->file_size - FFM_PACKET_SIZE;
445 while (pos_min <= pos_max) {
446 pts_min = get_dts(s, pos_min);
447 pts_max = get_dts(s, pos_max);
448 if (pts_min > wanted_pts || pts_max <= wanted_pts) {
449 pos = pts_min > wanted_pts ? pos_min : pos_max;
452 /* linear interpolation */
453 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
454 (double)(pts_max - pts_min);
455 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
458 else if (pos >= pos_max)
460 pts = get_dts(s, pos);
461 /* check if we are lucky */
462 if (pts == wanted_pts) {
464 } else if (pts > wanted_pts) {
465 pos_max = pos - FFM_PACKET_SIZE;
467 pos_min = pos + FFM_PACKET_SIZE;
470 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
473 if (ffm_seek1(s, pos) < 0)
476 /* reset read state */
477 ffm->read_state = READ_HEADER;
478 ffm->packet_ptr = ffm->packet;
479 ffm->packet_end = ffm->packet;
480 ffm->first_packet = 1;
485 static int ffm_probe(AVProbeData *p)
488 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
490 return AVPROBE_SCORE_MAX + 1;
494 AVInputFormat ff_ffm_demuxer = {
496 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
497 .priv_data_size = sizeof(FFMContext),
498 .read_probe = ffm_probe,
499 .read_header = ffm_read_header,
500 .read_packet = ffm_read_packet,
501 .read_close = ffm_close,
502 .read_seek = ffm_seek,