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_readwrite.h"
29 int64_t ffm_read_write_index(int fd)
33 lseek(fd, 8, SEEK_SET);
34 if (read(fd, buf, 8) != 8)
39 int ffm_write_write_index(int fd, int64_t pos)
45 buf[i] = (pos >> (56 - i * 8)) & 0xff;
46 lseek(fd, 8, SEEK_SET);
47 if (write(fd, buf, 8) != 8)
52 void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
54 FFMContext *ffm = s->priv_data;
55 ffm->write_index = pos;
56 ffm->file_size = file_size;
58 #endif // CONFIG_FFSERVER
60 static int ffm_is_avail_data(AVFormatContext *s, int size)
62 FFMContext *ffm = s->priv_data;
63 int64_t pos, avail_size;
66 len = ffm->packet_end - ffm->packet_ptr;
69 pos = avio_tell(s->pb);
70 if (!ffm->write_index) {
71 if (pos == ffm->file_size)
73 avail_size = ffm->file_size - pos;
75 if (pos == ffm->write_index) {
76 /* exactly at the end of stream */
77 return AVERROR(EAGAIN);
78 } else if (pos < ffm->write_index) {
79 avail_size = ffm->write_index - pos;
81 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
84 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
85 if (size <= avail_size)
88 return AVERROR(EAGAIN);
91 static int ffm_resync(AVFormatContext *s, int state)
93 av_log(s, AV_LOG_ERROR, "resyncing\n");
94 while (state != PACKET_ID) {
95 if (url_feof(s->pb)) {
96 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
99 state = (state << 8) | avio_r8(s->pb);
104 /* first is true if we read the frame header */
105 static int ffm_read_data(AVFormatContext *s,
106 uint8_t *buf, int size, int header)
108 FFMContext *ffm = s->priv_data;
109 AVIOContext *pb = s->pb;
110 int len, fill_size, size1, frame_offset, id;
115 len = ffm->packet_end - ffm->packet_ptr;
121 if (avio_tell(pb) == ffm->file_size)
122 avio_seek(pb, ffm->packet_size, SEEK_SET);
124 if (pb->buffer_size != ffm->packet_size) {
125 int64_t tell = avio_tell(pb);
126 url_setbufsize(pb, ffm->packet_size);
127 avio_seek(pb, tell, SEEK_SET);
129 id = avio_rb16(pb); /* PACKET_ID */
131 if (ffm_resync(s, id) < 0)
133 fill_size = avio_rb16(pb);
134 ffm->dts = avio_rb64(pb);
135 frame_offset = avio_rb16(pb);
136 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
137 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
138 if (ffm->packet_end < ffm->packet || frame_offset < 0)
140 /* if first packet or resynchronization packet, we must
141 handle it specifically */
142 if (ffm->first_packet || (frame_offset & 0x8000)) {
144 /* This packet has no frame headers in it */
145 if (avio_tell(pb) >= ffm->packet_size * 3) {
146 avio_seek(pb, -ffm->packet_size * 2, SEEK_CUR);
149 /* This is bad, we cannot find a valid frame header */
152 ffm->first_packet = 0;
153 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
155 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
159 ffm->packet_ptr = ffm->packet;
163 memcpy(buf, ffm->packet_ptr, len);
165 ffm->packet_ptr += len;
172 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
173 and file_size - FFM_PACKET_SIZE */
174 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
176 FFMContext *ffm = s->priv_data;
177 AVIOContext *pb = s->pb;
180 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
181 pos = FFMAX(pos, FFM_PACKET_SIZE);
182 av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
183 return avio_seek(pb, pos, SEEK_SET);
186 static int64_t get_dts(AVFormatContext *s, int64_t pos)
188 AVIOContext *pb = s->pb;
194 av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
198 static void adjust_write_index(AVFormatContext *s)
200 FFMContext *ffm = s->priv_data;
201 AVIOContext *pb = s->pb;
203 //int64_t orig_write_index = ffm->write_index;
204 int64_t pos_min, pos_max;
206 int64_t ptr = avio_tell(pb);
210 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
212 pts_start = get_dts(s, pos_min);
214 pts = get_dts(s, pos_max);
216 if (pts - 100000 > pts_start)
219 ffm->write_index = FFM_PACKET_SIZE;
221 pts_start = get_dts(s, pos_min);
223 pts = get_dts(s, pos_max);
225 if (pts - 100000 <= pts_start) {
230 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
232 if (newpos == pos_min)
235 newpts = get_dts(s, newpos);
237 if (newpts - 100000 <= pts) {
244 ffm->write_index += pos_max;
247 //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
248 //printf("pts range %0.6f - %0.6f\n", get_dts(s, 0) / 1000000. , get_dts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
251 avio_seek(pb, ptr, SEEK_SET);
255 static int ffm_close(AVFormatContext *s)
259 for (i = 0; i < s->nb_streams; i++)
260 av_freep(&s->streams[i]->codec->rc_eq);
266 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
268 FFMContext *ffm = s->priv_data;
270 AVIOContext *pb = s->pb;
271 AVCodecContext *codec;
277 if (tag != MKTAG('F', 'F', 'M', '1'))
279 ffm->packet_size = avio_rb32(pb);
280 if (ffm->packet_size != FFM_PACKET_SIZE)
282 ffm->write_index = avio_rb64(pb);
283 /* get also filesize */
285 ffm->file_size = avio_size(pb);
286 if (ffm->write_index)
287 adjust_write_index(s);
289 ffm->file_size = (UINT64_C(1) << 63) - 1;
292 nb_streams = avio_rb32(pb);
293 avio_rb32(pb); /* total bitrate */
294 /* read each stream */
295 for(i=0;i<nb_streams;i++) {
298 st = avformat_new_stream(s, NULL);
302 av_set_pts_info(st, 64, 1, 1000000);
306 codec->codec_id = avio_rb32(pb);
307 codec->codec_type = avio_r8(pb); /* codec_type */
308 codec->bit_rate = avio_rb32(pb);
309 codec->flags = avio_rb32(pb);
310 codec->flags2 = avio_rb32(pb);
311 codec->debug = avio_rb32(pb);
313 switch(codec->codec_type) {
314 case AVMEDIA_TYPE_VIDEO:
315 codec->time_base.num = avio_rb32(pb);
316 codec->time_base.den = avio_rb32(pb);
317 codec->width = avio_rb16(pb);
318 codec->height = avio_rb16(pb);
319 codec->gop_size = avio_rb16(pb);
320 codec->pix_fmt = avio_rb32(pb);
321 codec->qmin = avio_r8(pb);
322 codec->qmax = avio_r8(pb);
323 codec->max_qdiff = avio_r8(pb);
324 codec->qcompress = avio_rb16(pb) / 10000.0;
325 codec->qblur = avio_rb16(pb) / 10000.0;
326 codec->bit_rate_tolerance = avio_rb32(pb);
327 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
328 codec->rc_max_rate = avio_rb32(pb);
329 codec->rc_min_rate = avio_rb32(pb);
330 codec->rc_buffer_size = avio_rb32(pb);
331 codec->i_quant_factor = av_int2dbl(avio_rb64(pb));
332 codec->b_quant_factor = av_int2dbl(avio_rb64(pb));
333 codec->i_quant_offset = av_int2dbl(avio_rb64(pb));
334 codec->b_quant_offset = av_int2dbl(avio_rb64(pb));
335 codec->dct_algo = avio_rb32(pb);
336 codec->strict_std_compliance = avio_rb32(pb);
337 codec->max_b_frames = avio_rb32(pb);
338 codec->luma_elim_threshold = avio_rb32(pb);
339 codec->chroma_elim_threshold = avio_rb32(pb);
340 codec->mpeg_quant = avio_rb32(pb);
341 codec->intra_dc_precision = avio_rb32(pb);
342 codec->me_method = avio_rb32(pb);
343 codec->mb_decision = avio_rb32(pb);
344 codec->nsse_weight = avio_rb32(pb);
345 codec->frame_skip_cmp = avio_rb32(pb);
346 codec->rc_buffer_aggressivity = av_int2dbl(avio_rb64(pb));
347 codec->codec_tag = avio_rb32(pb);
348 codec->thread_count = avio_r8(pb);
349 codec->coder_type = avio_rb32(pb);
350 codec->me_cmp = avio_rb32(pb);
351 codec->partitions = avio_rb32(pb);
352 codec->me_subpel_quality = avio_rb32(pb);
353 codec->me_range = avio_rb32(pb);
354 codec->keyint_min = avio_rb32(pb);
355 codec->scenechange_threshold = avio_rb32(pb);
356 codec->b_frame_strategy = avio_rb32(pb);
357 codec->qcompress = av_int2dbl(avio_rb64(pb));
358 codec->qblur = av_int2dbl(avio_rb64(pb));
359 codec->max_qdiff = avio_rb32(pb);
360 codec->refs = avio_rb32(pb);
361 codec->directpred = avio_rb32(pb);
363 case AVMEDIA_TYPE_AUDIO:
364 codec->sample_rate = avio_rb32(pb);
365 codec->channels = avio_rl16(pb);
366 codec->frame_size = avio_rl16(pb);
367 codec->sample_fmt = (int16_t) avio_rl16(pb);
372 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
373 codec->extradata_size = avio_rb32(pb);
374 codec->extradata = av_malloc(codec->extradata_size);
375 if (!codec->extradata)
376 return AVERROR(ENOMEM);
377 avio_read(pb, codec->extradata, codec->extradata_size);
381 /* get until end of block reached */
382 while ((avio_tell(pb) % ffm->packet_size) != 0)
385 /* init packet demux */
386 ffm->packet_ptr = ffm->packet;
387 ffm->packet_end = ffm->packet;
388 ffm->frame_offset = 0;
390 ffm->read_state = READ_HEADER;
391 ffm->first_packet = 1;
398 /* return < 0 if eof */
399 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
402 FFMContext *ffm = s->priv_data;
405 switch(ffm->read_state) {
407 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
410 av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
411 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
412 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
415 if (ffm->header[1] & FLAG_DTS)
416 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
418 ffm->read_state = READ_DATA;
421 size = AV_RB24(ffm->header + 2);
422 if ((ret = ffm_is_avail_data(s, size)) < 0)
425 duration = AV_RB24(ffm->header + 5);
427 av_new_packet(pkt, size);
428 pkt->stream_index = ffm->header[0];
429 if ((unsigned)pkt->stream_index >= s->nb_streams) {
430 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
432 ffm->read_state = READ_HEADER;
435 pkt->pos = avio_tell(s->pb);
436 if (ffm->header[1] & FLAG_KEY_FRAME)
437 pkt->flags |= AV_PKT_FLAG_KEY;
439 ffm->read_state = READ_HEADER;
440 if (ffm_read_data(s, pkt->data, size, 0) != size) {
441 /* bad case: desynchronized packet. we cancel all the packet loading */
445 pkt->pts = AV_RB64(ffm->header+8);
446 if (ffm->header[1] & FLAG_DTS)
447 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
450 pkt->duration = duration;
456 /* seek to a given time in the file. The file read pointer is
457 positioned at or before pts. XXX: the following code is quite
459 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
461 FFMContext *ffm = s->priv_data;
462 int64_t pos_min, pos_max, pos;
463 int64_t pts_min, pts_max, pts;
466 av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
467 /* find the position using linear interpolation (better than
468 dichotomy in typical cases) */
469 if (ffm->write_index && ffm->write_index < ffm->file_size) {
470 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
471 pos_min = FFM_PACKET_SIZE;
472 pos_max = ffm->write_index - FFM_PACKET_SIZE;
474 pos_min = ffm->write_index;
475 pos_max = ffm->file_size - FFM_PACKET_SIZE;
478 pos_min = FFM_PACKET_SIZE;
479 pos_max = ffm->file_size - FFM_PACKET_SIZE;
481 while (pos_min <= pos_max) {
482 pts_min = get_dts(s, pos_min);
483 pts_max = get_dts(s, pos_max);
484 if (pts_min > wanted_pts || pts_max < wanted_pts) {
485 pos = pts_min > wanted_pts ? pos_min : pos_max;
488 /* linear interpolation */
489 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
490 (double)(pts_max - pts_min);
491 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
494 else if (pos >= pos_max)
496 pts = get_dts(s, pos);
497 /* check if we are lucky */
498 if (pts == wanted_pts) {
500 } else if (pts > wanted_pts) {
501 pos_max = pos - FFM_PACKET_SIZE;
503 pos_min = pos + FFM_PACKET_SIZE;
506 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
509 if (ffm_seek1(s, pos) < 0)
512 /* reset read state */
513 ffm->read_state = READ_HEADER;
514 ffm->packet_ptr = ffm->packet;
515 ffm->packet_end = ffm->packet;
516 ffm->first_packet = 1;
521 static int ffm_probe(AVProbeData *p)
524 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
526 return AVPROBE_SCORE_MAX + 1;
530 AVInputFormat ff_ffm_demuxer = {
532 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
533 .priv_data_size = sizeof(FFMContext),
534 .read_probe = ffm_probe,
535 .read_header = ffm_read_header,
536 .read_packet = ffm_read_packet,
537 .read_close = ffm_close,
538 .read_seek = ffm_seek,