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
26 static int64_t get_pts(AVFormatContext *s, offset_t pos);
28 static int ffm_is_avail_data(AVFormatContext *s, int size)
30 FFMContext *ffm = s->priv_data;
31 offset_t pos, avail_size;
34 len = ffm->packet_end - ffm->packet_ptr;
37 pos = url_ftell(s->pb);
38 if (pos == ffm->write_index) {
39 /* exactly at the end of stream */
41 } else if (pos < ffm->write_index) {
42 avail_size = ffm->write_index - pos;
44 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
46 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
47 if (size <= avail_size)
53 /* first is true if we read the frame header */
54 static int ffm_read_data(AVFormatContext *s,
55 uint8_t *buf, int size, int first)
57 FFMContext *ffm = s->priv_data;
58 ByteIOContext *pb = s->pb;
59 int len, fill_size, size1, frame_offset;
64 len = ffm->packet_end - ffm->packet_ptr;
68 if (url_ftell(pb) == ffm->file_size)
69 url_fseek(pb, ffm->packet_size, SEEK_SET);
71 get_be16(pb); /* PACKET_ID */
72 fill_size = get_be16(pb);
73 ffm->pts = get_be64(pb);
74 ffm->first_frame_in_packet = 1;
75 frame_offset = get_be16(pb);
76 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
77 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
78 if (ffm->packet_end < ffm->packet)
80 /* if first packet or resynchronization packet, we must
81 handle it specifically */
82 if (ffm->first_packet || (frame_offset & 0x8000)) {
84 /* This packet has no frame headers in it */
85 if (url_ftell(pb) >= ffm->packet_size * 3) {
86 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
89 /* This is bad, we cannot find a valid frame header */
92 ffm->first_packet = 0;
93 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
95 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
99 ffm->packet_ptr = ffm->packet;
103 memcpy(buf, ffm->packet_ptr, len);
105 ffm->packet_ptr += len;
113 static void adjust_write_index(AVFormatContext *s)
115 FFMContext *ffm = s->priv_data;
116 ByteIOContext *pb = s->pb;
118 //offset_t orig_write_index = ffm->write_index;
119 offset_t pos_min, pos_max;
121 offset_t ptr = url_ftell(pb);
125 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
127 pts_start = get_pts(s, pos_min);
129 pts = get_pts(s, pos_max);
131 if (pts - 100000 > pts_start)
134 ffm->write_index = FFM_PACKET_SIZE;
136 pts_start = get_pts(s, pos_min);
138 pts = get_pts(s, pos_max);
140 if (pts - 100000 <= pts_start) {
145 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
147 if (newpos == pos_min)
150 newpts = get_pts(s, newpos);
152 if (newpts - 100000 <= pts) {
159 ffm->write_index += pos_max;
162 //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
163 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
166 url_fseek(pb, ptr, SEEK_SET);
170 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
172 FFMContext *ffm = s->priv_data;
175 ByteIOContext *pb = s->pb;
176 AVCodecContext *codec;
182 if (tag != MKTAG('F', 'F', 'M', '1'))
184 ffm->packet_size = get_be32(pb);
185 if (ffm->packet_size != FFM_PACKET_SIZE)
187 ffm->write_index = get_be64(pb);
188 /* get also filesize */
189 if (!url_is_streamed(pb)) {
190 ffm->file_size = url_fsize(pb);
191 adjust_write_index(s);
193 ffm->file_size = (UINT64_C(1) << 63) - 1;
196 nb_streams = get_be32(pb);
197 get_be32(pb); /* total bitrate */
198 /* read each stream */
199 for(i=0;i<nb_streams;i++) {
202 st = av_new_stream(s, 0);
205 fst = av_mallocz(sizeof(FFMStream));
210 av_set_pts_info(st, 64, 1, 1000000);
216 codec->codec_id = get_be32(pb);
217 codec->codec_type = get_byte(pb); /* codec_type */
218 codec->bit_rate = get_be32(pb);
219 st->quality = get_be32(pb);
220 codec->flags = get_be32(pb);
221 codec->flags2 = get_be32(pb);
222 codec->debug = get_be32(pb);
224 switch(codec->codec_type) {
225 case CODEC_TYPE_VIDEO:
226 codec->time_base.num = get_be32(pb);
227 codec->time_base.den = get_be32(pb);
228 codec->width = get_be16(pb);
229 codec->height = get_be16(pb);
230 codec->gop_size = get_be16(pb);
231 codec->pix_fmt = get_be32(pb);
232 codec->qmin = get_byte(pb);
233 codec->qmax = get_byte(pb);
234 codec->max_qdiff = get_byte(pb);
235 codec->qcompress = get_be16(pb) / 10000.0;
236 codec->qblur = get_be16(pb) / 10000.0;
237 codec->bit_rate_tolerance = get_be32(pb);
238 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
239 codec->rc_max_rate = get_be32(pb);
240 codec->rc_min_rate = get_be32(pb);
241 codec->rc_buffer_size = get_be32(pb);
242 codec->i_quant_factor = av_int2dbl(get_be64(pb));
243 codec->b_quant_factor = av_int2dbl(get_be64(pb));
244 codec->i_quant_offset = av_int2dbl(get_be64(pb));
245 codec->b_quant_offset = av_int2dbl(get_be64(pb));
246 codec->dct_algo = get_be32(pb);
247 codec->strict_std_compliance = get_be32(pb);
248 codec->max_b_frames = get_be32(pb);
249 codec->luma_elim_threshold = get_be32(pb);
250 codec->chroma_elim_threshold = get_be32(pb);
251 codec->mpeg_quant = get_be32(pb);
252 codec->intra_dc_precision = get_be32(pb);
253 codec->me_method = get_be32(pb);
254 codec->mb_decision = get_be32(pb);
255 codec->nsse_weight = get_be32(pb);
256 codec->frame_skip_cmp = get_be32(pb);
257 codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
258 codec->codec_tag = get_be32(pb);
260 case CODEC_TYPE_AUDIO:
261 codec->sample_rate = get_be32(pb);
262 codec->channels = get_le16(pb);
263 codec->frame_size = get_le16(pb);
271 /* get until end of block reached */
272 while ((url_ftell(pb) % ffm->packet_size) != 0)
275 /* init packet demux */
276 ffm->packet_ptr = ffm->packet;
277 ffm->packet_end = ffm->packet;
278 ffm->frame_offset = 0;
280 ffm->read_state = READ_HEADER;
281 ffm->first_packet = 1;
284 for(i=0;i<s->nb_streams;i++) {
287 av_freep(&st->priv_data);
294 /* return < 0 if eof */
295 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
298 FFMContext *ffm = s->priv_data;
301 switch(ffm->read_state) {
303 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
304 return AVERROR(EAGAIN);
307 printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
308 url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
310 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
312 return AVERROR(EAGAIN);
316 for(i=0;i<FRAME_HEADER_SIZE;i++)
317 printf("%02x ", ffm->header[i]);
321 ffm->read_state = READ_DATA;
324 size = AV_RB24(ffm->header + 2);
325 if (!ffm_is_avail_data(s, size)) {
326 return AVERROR(EAGAIN);
329 duration = AV_RB24(ffm->header + 5);
331 av_new_packet(pkt, size);
332 pkt->stream_index = ffm->header[0];
333 if ((unsigned)pkt->stream_index >= s->nb_streams) {
334 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
336 ffm->read_state = READ_HEADER;
337 return AVERROR(EAGAIN);
339 pkt->pos = url_ftell(s->pb);
340 if (ffm->header[1] & FLAG_KEY_FRAME)
341 pkt->flags |= PKT_FLAG_KEY;
343 ffm->read_state = READ_HEADER;
344 if (ffm_read_data(s, pkt->data, size, 0) != size) {
345 /* bad case: desynchronized packet. we cancel all the packet loading */
347 return AVERROR(EAGAIN);
349 if (ffm->first_frame_in_packet)
352 ffm->first_frame_in_packet = 0;
354 pkt->duration = duration;
362 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
363 by the write position inside this function */
364 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
366 FFMContext *ffm = s->priv_data;
367 ByteIOContext *pb = s->pb;
370 pos = pos1 + ffm->write_index;
371 if (pos >= ffm->file_size)
372 pos -= (ffm->file_size - FFM_PACKET_SIZE);
374 printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
376 url_fseek(pb, pos, SEEK_SET);
379 static int64_t get_pts(AVFormatContext *s, offset_t pos)
381 ByteIOContext *pb = s->pb;
388 printf("pts=%0.6f\n", pts / 1000000.0);
393 /* seek to a given time in the file. The file read pointer is
394 positioned at or before pts. XXX: the following code is quite
396 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
398 FFMContext *ffm = s->priv_data;
399 offset_t pos_min, pos_max, pos;
400 int64_t pts_min, pts_max, pts;
404 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
406 /* find the position using linear interpolation (better than
407 dichotomy in typical cases) */
409 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
410 while (pos_min <= pos_max) {
411 pts_min = get_pts(s, pos_min);
412 pts_max = get_pts(s, pos_max);
413 /* linear interpolation */
414 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
415 (double)(pts_max - pts_min);
416 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
419 else if (pos >= pos_max)
421 pts = get_pts(s, pos);
422 /* check if we are lucky */
423 if (pts == wanted_pts) {
425 } else if (pts > wanted_pts) {
426 pos_max = pos - FFM_PACKET_SIZE;
428 pos_min = pos + FFM_PACKET_SIZE;
431 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
433 pos -= FFM_PACKET_SIZE;
439 #ifdef CONFIG_FFSERVER
440 offset_t ffm_read_write_index(int fd)
444 lseek(fd, 8, SEEK_SET);
449 void ffm_write_write_index(int fd, offset_t pos)
455 buf[i] = (pos >> (56 - i * 8)) & 0xff;
456 lseek(fd, 8, SEEK_SET);
460 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
462 FFMContext *ffm = s->priv_data;
463 ffm->write_index = pos;
464 ffm->file_size = file_size;
466 #endif // CONFIG_FFSERVER
468 static int ffm_read_close(AVFormatContext *s)
473 for(i=0;i<s->nb_streams;i++) {
475 av_freep(&st->priv_data);
480 static int ffm_probe(AVProbeData *p)
483 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
485 return AVPROBE_SCORE_MAX + 1;
489 AVInputFormat ffm_demuxer = {