2 * FFM (ffserver live feed) encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* The FFM file is made of blocks of fixed size */
23 #define FFM_HEADER_SIZE 14
24 #define PACKET_ID 0x666d
26 /* each packet contains frames (which can span several packets */
27 #define FRAME_HEADER_SIZE 8
28 #define FLAG_KEY_FRAME 0x01
30 typedef struct FFMStream {
39 typedef struct FFMContext {
40 /* only reading mode */
41 offset_t write_index, file_size;
43 uint8_t header[FRAME_HEADER_SIZE];
46 int first_packet; /* true if first packet, needed to set the discontinuity tag */
50 uint8_t *packet_ptr, *packet_end;
51 uint8_t packet[FFM_PACKET_SIZE];
54 /* disable pts hack for testing */
57 static void flush_packet(AVFormatContext *s)
59 FFMContext *ffm = s->priv_data;
61 ByteIOContext *pb = &s->pb;
63 fill_size = ffm->packet_end - ffm->packet_ptr;
64 memset(ffm->packet_ptr, 0, fill_size);
67 put_be16(pb, PACKET_ID);
68 put_be16(pb, fill_size);
69 put_be64(pb, ffm->pts);
70 h = ffm->frame_offset;
71 if (ffm->first_packet)
74 put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
76 /* prepare next packet */
77 ffm->frame_offset = 0; /* no key frame */
78 ffm->pts = 0; /* no pts */
79 ffm->packet_ptr = ffm->packet;
80 ffm->first_packet = 0;
83 /* 'first' is true if first data of a frame */
84 static void ffm_write_data(AVFormatContext *s,
85 const uint8_t *buf, int size,
86 int64_t pts, int first)
88 FFMContext *ffm = s->priv_data;
91 if (first && ffm->frame_offset == 0)
92 ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
93 if (first && ffm->pts == 0)
96 /* write as many packets as needed */
98 len = ffm->packet_end - ffm->packet_ptr;
101 memcpy(ffm->packet_ptr, buf, len);
103 ffm->packet_ptr += len;
106 if (ffm->packet_ptr >= ffm->packet_end) {
107 /* special case : no pts in packet : we leave the current one */
116 static int ffm_write_header(AVFormatContext *s)
118 FFMContext *ffm = s->priv_data;
121 ByteIOContext *pb = &s->pb;
122 AVCodecContext *codec;
125 ffm->packet_size = FFM_PACKET_SIZE;
129 put_be32(pb, ffm->packet_size);
130 /* XXX: store write position in other file ? */
131 put_be64(pb, ffm->packet_size); /* current write position */
133 put_be32(pb, s->nb_streams);
135 for(i=0;i<s->nb_streams;i++) {
137 bit_rate += st->codec.bit_rate;
139 put_be32(pb, bit_rate);
141 /* list of streams */
142 for(i=0;i<s->nb_streams;i++) {
144 fst = av_mallocz(sizeof(FFMStream));
151 put_be32(pb, codec->codec_id);
152 put_byte(pb, codec->codec_type);
153 put_be32(pb, codec->bit_rate);
154 put_be32(pb, st->quality);
155 put_be32(pb, codec->flags);
157 switch(codec->codec_type) {
158 case CODEC_TYPE_VIDEO:
159 put_be32(pb, codec->frame_rate_base);
160 put_be32(pb, codec->frame_rate);
161 put_be16(pb, codec->width);
162 put_be16(pb, codec->height);
163 put_be16(pb, codec->gop_size);
164 put_byte(pb, codec->qmin);
165 put_byte(pb, codec->qmax);
166 put_byte(pb, codec->max_qdiff);
167 put_be16(pb, (int) (codec->qcompress * 10000.0));
168 put_be16(pb, (int) (codec->qblur * 10000.0));
169 put_be32(pb, codec->bit_rate_tolerance);
170 put_strz(pb, codec->rc_eq);
171 put_be32(pb, codec->rc_max_rate);
172 put_be32(pb, codec->rc_min_rate);
173 put_be32(pb, codec->rc_buffer_size);
174 put_be64_double(pb, codec->i_quant_factor);
175 put_be64_double(pb, codec->b_quant_factor);
176 put_be64_double(pb, codec->i_quant_offset);
177 put_be64_double(pb, codec->b_quant_offset);
178 put_be32(pb, codec->dct_algo);
180 case CODEC_TYPE_AUDIO:
181 put_be32(pb, codec->sample_rate);
182 put_le16(pb, codec->channels);
183 put_le16(pb, codec->frame_size);
188 /* hack to have real time */
192 fst->pts = av_gettime();
195 /* flush until end of block reached */
196 while ((url_ftell(pb) % ffm->packet_size) != 0)
199 put_flush_packet(pb);
201 /* init packet mux */
202 ffm->packet_ptr = ffm->packet;
203 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
204 ffm->frame_offset = 0;
206 ffm->first_packet = 1;
210 for(i=0;i<s->nb_streams;i++) {
212 av_freep(&st->priv_data);
217 static int ffm_write_packet(AVFormatContext *s, int stream_index,
218 const uint8_t *buf, int size, int64_t force_pts)
220 AVStream *st = s->streams[stream_index];
221 FFMStream *fst = st->priv_data;
223 uint8_t header[FRAME_HEADER_SIZE];
226 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
227 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
229 duration = (1000000.0 * st->codec.frame_rate_base / (float)st->codec.frame_rate);
233 /* packet size & key_frame */
234 header[0] = stream_index;
236 if (st->codec.coded_frame->key_frame) //if st->codec.coded_frame==NULL then there is a bug somewhere else
237 header[1] |= FLAG_KEY_FRAME;
238 header[2] = (size >> 16) & 0xff;
239 header[3] = (size >> 8) & 0xff;
240 header[4] = size & 0xff;
241 header[5] = (duration >> 16) & 0xff;
242 header[6] = (duration >> 8) & 0xff;
243 header[7] = duration & 0xff;
244 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
245 ffm_write_data(s, buf, size, pts, 0);
247 fst->pts += duration;
251 static int ffm_write_trailer(AVFormatContext *s)
253 ByteIOContext *pb = &s->pb;
254 FFMContext *ffm = s->priv_data;
258 if (ffm->packet_ptr > ffm->packet)
261 put_flush_packet(pb);
263 if (!url_is_streamed(pb)) {
265 /* update the write offset */
266 size = url_ftell(pb);
267 url_fseek(pb, 8, SEEK_SET);
269 put_flush_packet(pb);
272 for(i=0;i<s->nb_streams;i++)
273 av_freep(&s->streams[i]->priv_data);
279 static int ffm_is_avail_data(AVFormatContext *s, int size)
281 FFMContext *ffm = s->priv_data;
282 offset_t pos, avail_size;
285 len = ffm->packet_end - ffm->packet_ptr;
287 /* XXX: I don't understand this test, so I disabled it for testing */
291 pos = url_ftell(&s->pb);
292 if (pos == ffm->write_index) {
293 /* exactly at the end of stream */
295 } else if (pos < ffm->write_index) {
296 avail_size = ffm->write_index - pos;
298 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
300 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
301 if (size <= avail_size)
307 /* first is true if we read the frame header */
308 static int ffm_read_data(AVFormatContext *s,
309 uint8_t *buf, int size, int first)
311 FFMContext *ffm = s->priv_data;
312 ByteIOContext *pb = &s->pb;
313 int len, fill_size, size1, frame_offset;
318 len = ffm->packet_end - ffm->packet_ptr;
322 if (url_ftell(pb) == ffm->file_size)
323 url_fseek(pb, ffm->packet_size, SEEK_SET);
325 get_be16(pb); /* PACKET_ID */
326 fill_size = get_be16(pb);
327 ffm->pts = get_be64(pb);
328 frame_offset = get_be16(pb);
329 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
330 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
331 /* if first packet or resynchronization packet, we must
332 handle it specifically */
333 if (ffm->first_packet || (frame_offset & 0x8000)) {
335 /* This packet has no frame headers in it */
336 if (url_ftell(pb) >= ffm->packet_size * 3) {
337 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
340 /* This is bad, we cannot find a valid frame header */
343 ffm->first_packet = 0;
344 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
346 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
350 ffm->packet_ptr = ffm->packet;
354 memcpy(buf, ffm->packet_ptr, len);
356 ffm->packet_ptr += len;
364 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
366 FFMContext *ffm = s->priv_data;
369 ByteIOContext *pb = &s->pb;
370 AVCodecContext *codec;
376 if (tag != MKTAG('F', 'F', 'M', '1'))
378 ffm->packet_size = get_be32(pb);
379 if (ffm->packet_size != FFM_PACKET_SIZE)
381 ffm->write_index = get_be64(pb);
382 /* get also filesize */
383 if (!url_is_streamed(pb)) {
384 ffm->file_size = url_filesize(url_fileno(pb));
386 ffm->file_size = (uint64_t_C(1) << 63) - 1;
389 nb_streams = get_be32(pb);
390 get_be32(pb); /* total bitrate */
391 /* read each stream */
392 for(i=0;i<nb_streams;i++) {
395 st = av_new_stream(s, 0);
398 fst = av_mallocz(sizeof(FFMStream));
405 st->codec.codec_id = get_be32(pb);
406 st->codec.codec_type = get_byte(pb); /* codec_type */
407 codec->bit_rate = get_be32(pb);
408 st->quality = get_be32(pb);
409 codec->flags = get_be32(pb);
411 switch(codec->codec_type) {
412 case CODEC_TYPE_VIDEO:
413 codec->frame_rate_base = get_be32(pb);
414 codec->frame_rate = get_be32(pb);
415 codec->width = get_be16(pb);
416 codec->height = get_be16(pb);
417 codec->gop_size = get_be16(pb);
418 codec->qmin = get_byte(pb);
419 codec->qmax = get_byte(pb);
420 codec->max_qdiff = get_byte(pb);
421 codec->qcompress = get_be16(pb) / 10000.0;
422 codec->qblur = get_be16(pb) / 10000.0;
423 codec->bit_rate_tolerance = get_be32(pb);
424 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
425 codec->rc_max_rate = get_be32(pb);
426 codec->rc_min_rate = get_be32(pb);
427 codec->rc_buffer_size = get_be32(pb);
428 codec->i_quant_factor = get_be64_double(pb);
429 codec->b_quant_factor = get_be64_double(pb);
430 codec->i_quant_offset = get_be64_double(pb);
431 codec->b_quant_offset = get_be64_double(pb);
432 codec->dct_algo = get_be32(pb);
434 case CODEC_TYPE_AUDIO:
435 codec->sample_rate = get_be32(pb);
436 codec->channels = get_le16(pb);
437 codec->frame_size = get_le16(pb);
445 /* get until end of block reached */
446 while ((url_ftell(pb) % ffm->packet_size) != 0)
449 /* init packet demux */
450 ffm->packet_ptr = ffm->packet;
451 ffm->packet_end = ffm->packet;
452 ffm->frame_offset = 0;
454 ffm->read_state = READ_HEADER;
455 ffm->first_packet = 1;
458 for(i=0;i<s->nb_streams;i++) {
461 av_freep(&st->priv_data);
468 /* return < 0 if eof */
469 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
472 FFMContext *ffm = s->priv_data;
475 switch(ffm->read_state) {
477 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
481 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
482 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
484 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
490 for(i=0;i<FRAME_HEADER_SIZE;i++)
491 printf("%02x ", ffm->header[i]);
495 ffm->read_state = READ_DATA;
498 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
499 if (!ffm_is_avail_data(s, size)) {
503 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
505 av_new_packet(pkt, size);
506 pkt->stream_index = ffm->header[0];
507 if (ffm->header[1] & FLAG_KEY_FRAME)
508 pkt->flags |= PKT_FLAG_KEY;
510 ffm->read_state = READ_HEADER;
511 if (ffm_read_data(s, pkt->data, size, 0) != size) {
512 /* bad case: desynchronized packet. we cancel all the packet loading */
517 pkt->duration = duration;
525 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
526 by the write position inside this function */
527 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
529 FFMContext *ffm = s->priv_data;
530 ByteIOContext *pb = &s->pb;
533 pos = pos1 + ffm->write_index;
534 if (pos >= ffm->file_size)
535 pos -= (ffm->file_size - FFM_PACKET_SIZE);
537 printf("seek to %Lx -> %Lx\n", pos1, pos);
539 url_fseek(pb, pos, SEEK_SET);
542 static int64_t get_pts(AVFormatContext *s, offset_t pos)
544 ByteIOContext *pb = &s->pb;
551 printf("pts=%0.6f\n", pts / 1000000.0);
556 /* seek to a given time in the file. The file read pointer is
557 positionned at or before pts. XXX: the following code is quite
559 static int ffm_seek(AVFormatContext *s, int64_t wanted_pts)
561 FFMContext *ffm = s->priv_data;
562 offset_t pos_min, pos_max, pos;
563 int64_t pts_min, pts_max, pts;
567 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
569 /* find the position using linear interpolation (better than
570 dichotomy in typical cases) */
572 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
573 while (pos_min <= pos_max) {
574 pts_min = get_pts(s, pos_min);
575 pts_max = get_pts(s, pos_max);
576 /* linear interpolation */
577 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
578 (double)(pts_max - pts_min);
579 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
582 else if (pos >= pos_max)
584 pts = get_pts(s, pos);
585 /* check if we are lucky */
586 if (pts == wanted_pts) {
588 } else if (pts > wanted_pts) {
589 pos_max = pos - FFM_PACKET_SIZE;
591 pos_min = pos + FFM_PACKET_SIZE;
596 pos -= FFM_PACKET_SIZE;
602 offset_t ffm_read_write_index(int fd)
608 lseek(fd, 8, SEEK_SET);
612 pos |= (int64_t)buf[i] << (56 - i * 8);
616 void ffm_write_write_index(int fd, offset_t pos)
622 buf[i] = (pos >> (56 - i * 8)) & 0xff;
623 lseek(fd, 8, SEEK_SET);
627 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
629 FFMContext *ffm = s->priv_data;
630 ffm->write_index = pos;
631 ffm->file_size = file_size;
634 static int ffm_read_close(AVFormatContext *s)
639 for(i=0;i<s->nb_streams;i++) {
641 av_freep(&st->priv_data);
646 static int ffm_probe(AVProbeData *p)
648 if (p->buf_size >= 4 &&
649 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
651 return AVPROBE_SCORE_MAX + 1;
655 static AVInputFormat ffm_iformat = {
666 static AVOutputFormat ffm_oformat = {
672 /* not really used */
682 av_register_input_format(&ffm_iformat);
683 av_register_output_format(&ffm_oformat);