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 #ifdef CONFIG_FFSERVER
27 offset_t ffm_read_write_index(int fd)
31 lseek(fd, 8, SEEK_SET);
36 void ffm_write_write_index(int fd, offset_t pos)
42 buf[i] = (pos >> (56 - i * 8)) & 0xff;
43 lseek(fd, 8, SEEK_SET);
47 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
49 FFMContext *ffm = s->priv_data;
50 ffm->write_index = pos;
51 ffm->file_size = file_size;
53 #endif // CONFIG_FFSERVER
55 static int ffm_is_avail_data(AVFormatContext *s, int size)
57 FFMContext *ffm = s->priv_data;
58 offset_t pos, avail_size;
61 len = ffm->packet_end - ffm->packet_ptr;
64 pos = url_ftell(s->pb);
65 if (pos == ffm->write_index) {
66 /* exactly at the end of stream */
68 } else if (pos < ffm->write_index) {
69 avail_size = ffm->write_index - pos;
71 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
73 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
74 if (size <= avail_size)
80 /* first is true if we read the frame header */
81 static int ffm_read_data(AVFormatContext *s,
82 uint8_t *buf, int size, int first)
84 FFMContext *ffm = s->priv_data;
85 ByteIOContext *pb = s->pb;
86 int len, fill_size, size1, frame_offset;
91 len = ffm->packet_end - ffm->packet_ptr;
95 if (url_ftell(pb) == ffm->file_size)
96 url_fseek(pb, ffm->packet_size, SEEK_SET);
98 get_be16(pb); /* PACKET_ID */
99 fill_size = get_be16(pb);
100 ffm->pts = get_be64(pb);
101 ffm->first_frame_in_packet = 1;
102 frame_offset = get_be16(pb);
103 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
104 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
105 if (ffm->packet_end < ffm->packet)
107 /* if first packet or resynchronization packet, we must
108 handle it specifically */
109 if (ffm->first_packet || (frame_offset & 0x8000)) {
111 /* This packet has no frame headers in it */
112 if (url_ftell(pb) >= ffm->packet_size * 3) {
113 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
116 /* This is bad, we cannot find a valid frame header */
119 ffm->first_packet = 0;
120 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
122 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
126 ffm->packet_ptr = ffm->packet;
130 memcpy(buf, ffm->packet_ptr, len);
132 ffm->packet_ptr += len;
141 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
142 by the write position inside this function */
143 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
145 FFMContext *ffm = s->priv_data;
146 ByteIOContext *pb = s->pb;
149 pos = pos1 + ffm->write_index;
150 if (pos >= ffm->file_size)
151 pos -= (ffm->file_size - FFM_PACKET_SIZE);
153 printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
155 url_fseek(pb, pos, SEEK_SET);
158 static int64_t get_pts(AVFormatContext *s, offset_t pos)
160 ByteIOContext *pb = s->pb;
167 printf("pts=%0.6f\n", pts / 1000000.0);
172 static void adjust_write_index(AVFormatContext *s)
174 FFMContext *ffm = s->priv_data;
175 ByteIOContext *pb = s->pb;
177 //offset_t orig_write_index = ffm->write_index;
178 offset_t pos_min, pos_max;
180 offset_t ptr = url_ftell(pb);
184 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
186 pts_start = get_pts(s, pos_min);
188 pts = get_pts(s, pos_max);
190 if (pts - 100000 > pts_start)
193 ffm->write_index = FFM_PACKET_SIZE;
195 pts_start = get_pts(s, pos_min);
197 pts = get_pts(s, pos_max);
199 if (pts - 100000 <= pts_start) {
204 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
206 if (newpos == pos_min)
209 newpts = get_pts(s, newpos);
211 if (newpts - 100000 <= pts) {
218 ffm->write_index += pos_max;
221 //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
222 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
225 url_fseek(pb, ptr, SEEK_SET);
229 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
231 FFMContext *ffm = s->priv_data;
234 ByteIOContext *pb = s->pb;
235 AVCodecContext *codec;
241 if (tag != MKTAG('F', 'F', 'M', '1'))
243 ffm->packet_size = get_be32(pb);
244 if (ffm->packet_size != FFM_PACKET_SIZE)
246 ffm->write_index = get_be64(pb);
247 /* get also filesize */
248 if (!url_is_streamed(pb)) {
249 ffm->file_size = url_fsize(pb);
250 adjust_write_index(s);
252 ffm->file_size = (UINT64_C(1) << 63) - 1;
255 nb_streams = get_be32(pb);
256 get_be32(pb); /* total bitrate */
257 /* read each stream */
258 for(i=0;i<nb_streams;i++) {
261 st = av_new_stream(s, 0);
264 fst = av_mallocz(sizeof(FFMStream));
269 av_set_pts_info(st, 64, 1, 1000000);
275 codec->codec_id = get_be32(pb);
276 codec->codec_type = get_byte(pb); /* codec_type */
277 codec->bit_rate = get_be32(pb);
278 st->quality = get_be32(pb);
279 codec->flags = get_be32(pb);
280 codec->flags2 = get_be32(pb);
281 codec->debug = get_be32(pb);
283 switch(codec->codec_type) {
284 case CODEC_TYPE_VIDEO:
285 codec->time_base.num = get_be32(pb);
286 codec->time_base.den = get_be32(pb);
287 codec->width = get_be16(pb);
288 codec->height = get_be16(pb);
289 codec->gop_size = get_be16(pb);
290 codec->pix_fmt = get_be32(pb);
291 codec->qmin = get_byte(pb);
292 codec->qmax = get_byte(pb);
293 codec->max_qdiff = get_byte(pb);
294 codec->qcompress = get_be16(pb) / 10000.0;
295 codec->qblur = get_be16(pb) / 10000.0;
296 codec->bit_rate_tolerance = get_be32(pb);
297 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
298 codec->rc_max_rate = get_be32(pb);
299 codec->rc_min_rate = get_be32(pb);
300 codec->rc_buffer_size = get_be32(pb);
301 codec->i_quant_factor = av_int2dbl(get_be64(pb));
302 codec->b_quant_factor = av_int2dbl(get_be64(pb));
303 codec->i_quant_offset = av_int2dbl(get_be64(pb));
304 codec->b_quant_offset = av_int2dbl(get_be64(pb));
305 codec->dct_algo = get_be32(pb);
306 codec->strict_std_compliance = get_be32(pb);
307 codec->max_b_frames = get_be32(pb);
308 codec->luma_elim_threshold = get_be32(pb);
309 codec->chroma_elim_threshold = get_be32(pb);
310 codec->mpeg_quant = get_be32(pb);
311 codec->intra_dc_precision = get_be32(pb);
312 codec->me_method = get_be32(pb);
313 codec->mb_decision = get_be32(pb);
314 codec->nsse_weight = get_be32(pb);
315 codec->frame_skip_cmp = get_be32(pb);
316 codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
317 codec->codec_tag = get_be32(pb);
319 case CODEC_TYPE_AUDIO:
320 codec->sample_rate = get_be32(pb);
321 codec->channels = get_le16(pb);
322 codec->frame_size = get_le16(pb);
330 /* get until end of block reached */
331 while ((url_ftell(pb) % ffm->packet_size) != 0)
334 /* init packet demux */
335 ffm->packet_ptr = ffm->packet;
336 ffm->packet_end = ffm->packet;
337 ffm->frame_offset = 0;
339 ffm->read_state = READ_HEADER;
340 ffm->first_packet = 1;
343 for(i=0;i<s->nb_streams;i++) {
346 av_freep(&st->priv_data);
353 /* return < 0 if eof */
354 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
357 FFMContext *ffm = s->priv_data;
360 switch(ffm->read_state) {
362 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
363 return AVERROR(EAGAIN);
366 printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
367 url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
369 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
371 return AVERROR(EAGAIN);
375 for(i=0;i<FRAME_HEADER_SIZE;i++)
376 printf("%02x ", ffm->header[i]);
380 ffm->read_state = READ_DATA;
383 size = AV_RB24(ffm->header + 2);
384 if (!ffm_is_avail_data(s, size)) {
385 return AVERROR(EAGAIN);
388 duration = AV_RB24(ffm->header + 5);
390 av_new_packet(pkt, size);
391 pkt->stream_index = ffm->header[0];
392 if ((unsigned)pkt->stream_index >= s->nb_streams) {
393 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
395 ffm->read_state = READ_HEADER;
396 return AVERROR(EAGAIN);
398 pkt->pos = url_ftell(s->pb);
399 if (ffm->header[1] & FLAG_KEY_FRAME)
400 pkt->flags |= PKT_FLAG_KEY;
402 ffm->read_state = READ_HEADER;
403 if (ffm_read_data(s, pkt->data, size, 0) != size) {
404 /* bad case: desynchronized packet. we cancel all the packet loading */
406 return AVERROR(EAGAIN);
408 if (ffm->first_frame_in_packet)
411 ffm->first_frame_in_packet = 0;
413 pkt->duration = duration;
419 /* seek to a given time in the file. The file read pointer is
420 positioned at or before pts. XXX: the following code is quite
422 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
424 FFMContext *ffm = s->priv_data;
425 offset_t pos_min, pos_max, pos;
426 int64_t pts_min, pts_max, pts;
430 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
432 /* find the position using linear interpolation (better than
433 dichotomy in typical cases) */
435 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
436 while (pos_min <= pos_max) {
437 pts_min = get_pts(s, pos_min);
438 pts_max = get_pts(s, pos_max);
439 /* linear interpolation */
440 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
441 (double)(pts_max - pts_min);
442 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
445 else if (pos >= pos_max)
447 pts = get_pts(s, pos);
448 /* check if we are lucky */
449 if (pts == wanted_pts) {
451 } else if (pts > wanted_pts) {
452 pos_max = pos - FFM_PACKET_SIZE;
454 pos_min = pos + FFM_PACKET_SIZE;
457 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
459 pos -= FFM_PACKET_SIZE;
465 static int ffm_read_close(AVFormatContext *s)
470 for(i=0;i<s->nb_streams;i++) {
472 av_freep(&st->priv_data);
477 static int ffm_probe(AVProbeData *p)
480 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
482 return AVPROBE_SCORE_MAX + 1;
486 AVInputFormat ffm_demuxer = {