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 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 uint8_t *buf, int size, int 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 s->nb_streams = get_be32(pb);
390 get_be32(pb); /* total bitrate */
391 /* read each stream */
392 for(i=0;i<s->nb_streams;i++) {
395 st = av_mallocz(sizeof(AVStream));
398 avcodec_get_context_defaults(&st->codec);
400 fst = av_mallocz(sizeof(FFMStream));
407 st->codec.codec_id = get_be32(pb);
408 st->codec.codec_type = get_byte(pb); /* codec_type */
409 codec->bit_rate = get_be32(pb);
410 st->quality = get_be32(pb);
411 codec->flags = get_be32(pb);
413 switch(codec->codec_type) {
414 case CODEC_TYPE_VIDEO:
415 codec->frame_rate_base = get_be32(pb);
416 codec->frame_rate = get_be32(pb);
417 codec->width = get_be16(pb);
418 codec->height = get_be16(pb);
419 codec->gop_size = get_be16(pb);
420 codec->qmin = get_byte(pb);
421 codec->qmax = get_byte(pb);
422 codec->max_qdiff = get_byte(pb);
423 codec->qcompress = get_be16(pb) / 10000.0;
424 codec->qblur = get_be16(pb) / 10000.0;
425 codec->bit_rate_tolerance = get_be32(pb);
426 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
427 codec->rc_max_rate = get_be32(pb);
428 codec->rc_min_rate = get_be32(pb);
429 codec->rc_buffer_size = get_be32(pb);
430 codec->i_quant_factor = get_be64_double(pb);
431 codec->b_quant_factor = get_be64_double(pb);
432 codec->i_quant_offset = get_be64_double(pb);
433 codec->b_quant_offset = get_be64_double(pb);
434 codec->dct_algo = get_be32(pb);
436 case CODEC_TYPE_AUDIO:
437 codec->sample_rate = get_be32(pb);
438 codec->channels = get_le16(pb);
439 codec->frame_size = get_le16(pb);
447 /* get until end of block reached */
448 while ((url_ftell(pb) % ffm->packet_size) != 0)
451 /* init packet demux */
452 ffm->packet_ptr = ffm->packet;
453 ffm->packet_end = ffm->packet;
454 ffm->frame_offset = 0;
456 ffm->read_state = READ_HEADER;
457 ffm->first_packet = 1;
460 for(i=0;i<s->nb_streams;i++) {
463 av_freep(&st->priv_data);
470 /* return < 0 if eof */
471 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
474 FFMContext *ffm = s->priv_data;
477 switch(ffm->read_state) {
479 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
483 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
484 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
486 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
492 for(i=0;i<FRAME_HEADER_SIZE;i++)
493 printf("%02x ", ffm->header[i]);
497 ffm->read_state = READ_DATA;
500 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
501 if (!ffm_is_avail_data(s, size)) {
505 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
507 av_new_packet(pkt, size);
508 pkt->stream_index = ffm->header[0];
509 if (ffm->header[1] & FLAG_KEY_FRAME)
510 pkt->flags |= PKT_FLAG_KEY;
512 ffm->read_state = READ_HEADER;
513 if (ffm_read_data(s, pkt->data, size, 0) != size) {
514 /* bad case: desynchronized packet. we cancel all the packet loading */
519 pkt->duration = duration;
527 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
528 by the write position inside this function */
529 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
531 FFMContext *ffm = s->priv_data;
532 ByteIOContext *pb = &s->pb;
535 pos = pos1 + ffm->write_index;
536 if (pos >= ffm->file_size)
537 pos -= (ffm->file_size - FFM_PACKET_SIZE);
539 printf("seek to %Lx -> %Lx\n", pos1, pos);
541 url_fseek(pb, pos, SEEK_SET);
544 static int64_t get_pts(AVFormatContext *s, offset_t pos)
546 ByteIOContext *pb = &s->pb;
553 printf("pts=%0.6f\n", pts / 1000000.0);
558 /* seek to a given time in the file. The file read pointer is
559 positionned at or before pts. XXX: the following code is quite
561 static int ffm_seek(AVFormatContext *s, int64_t wanted_pts)
563 FFMContext *ffm = s->priv_data;
564 offset_t pos_min, pos_max, pos;
565 int64_t pts_min, pts_max, pts;
569 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
571 /* find the position using linear interpolation (better than
572 dichotomy in typical cases) */
574 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
575 while (pos_min <= pos_max) {
576 pts_min = get_pts(s, pos_min);
577 pts_max = get_pts(s, pos_max);
578 /* linear interpolation */
579 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
580 (double)(pts_max - pts_min);
581 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
584 else if (pos >= pos_max)
586 pts = get_pts(s, pos);
587 /* check if we are lucky */
588 if (pts == wanted_pts) {
590 } else if (pts > wanted_pts) {
591 pos_max = pos - FFM_PACKET_SIZE;
593 pos_min = pos + FFM_PACKET_SIZE;
598 pos -= FFM_PACKET_SIZE;
604 offset_t ffm_read_write_index(int fd)
610 lseek(fd, 8, SEEK_SET);
614 pos |= buf[i] << (56 - i * 8);
618 void ffm_write_write_index(int fd, offset_t pos)
624 buf[i] = (pos >> (56 - i * 8)) & 0xff;
625 lseek(fd, 8, SEEK_SET);
629 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
631 FFMContext *ffm = s->priv_data;
632 ffm->write_index = pos;
633 ffm->file_size = file_size;
636 static int ffm_read_close(AVFormatContext *s)
641 for(i=0;i<s->nb_streams;i++) {
643 av_freep(&st->priv_data);
648 static int ffm_probe(AVProbeData *p)
650 if (p->buf_size >= 4 &&
651 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
653 return AVPROBE_SCORE_MAX + 1;
657 static AVInputFormat ffm_iformat = {
668 static AVOutputFormat ffm_oformat = {
674 /* not really used */
684 av_register_input_format(&ffm_iformat);
685 av_register_output_format(&ffm_oformat);