2 * FFM (ffserver live feed) encoder and decoder
3 * Copyright (c) 2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 header[FRAME_HEADER_SIZE];
46 int first_packet; /* true if first packet, needed to set the discontinuity tag */
50 UINT8 *packet_ptr, *packet_end;
51 UINT8 packet[1]; /* must be last */
54 static void flush_packet(AVFormatContext *s)
56 FFMContext *ffm = s->priv_data;
58 ByteIOContext *pb = &s->pb;
60 fill_size = ffm->packet_end - ffm->packet_ptr;
61 memset(ffm->packet_ptr, 0, fill_size);
64 put_be16(pb, PACKET_ID);
65 put_be16(pb, fill_size);
66 put_be64(pb, ffm->pts);
67 h = ffm->frame_offset;
68 if (ffm->first_packet)
71 put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
73 /* prepare next packet */
74 ffm->frame_offset = 0; /* no key frame */
75 ffm->pts = 0; /* no pts */
76 ffm->packet_ptr = ffm->packet;
77 ffm->first_packet = 0;
80 /* 'first' is true if first data of a frame */
81 static void ffm_write_data(AVFormatContext *s,
85 FFMContext *ffm = s->priv_data;
88 if (first && ffm->frame_offset == 0)
89 ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
90 if (first && ffm->pts == 0)
93 /* write as many packets as needed */
95 len = ffm->packet_end - ffm->packet_ptr;
98 memcpy(ffm->packet_ptr, buf, len);
100 ffm->packet_ptr += len;
103 if (ffm->packet_ptr >= ffm->packet_end) {
104 /* special case : no pts in packet : we leave the current one */
113 static int ffm_write_header(AVFormatContext *s)
118 ByteIOContext *pb = &s->pb;
119 AVCodecContext *codec;
122 ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
127 ffm->packet_size = FFM_PACKET_SIZE;
131 put_be32(pb, ffm->packet_size);
132 /* XXX: store write position in other file ? */
133 put_be64(pb, ffm->packet_size); /* current write position */
135 put_be32(pb, s->nb_streams);
137 for(i=0;i<s->nb_streams;i++) {
139 bit_rate += st->codec.bit_rate;
141 put_be32(pb, bit_rate);
143 /* list of streams */
144 for(i=0;i<s->nb_streams;i++) {
146 fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
153 put_be32(pb, codec->codec_id);
154 put_byte(pb, codec->codec_type);
155 put_be32(pb, codec->bit_rate);
156 put_be32(pb, codec->flags);
158 switch(codec->codec_type) {
159 case CODEC_TYPE_VIDEO:
160 put_be32(pb, (codec->frame_rate * 1000) / FRAME_RATE_BASE);
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));
170 case CODEC_TYPE_AUDIO:
171 put_be32(pb, codec->sample_rate);
172 put_le16(pb, codec->channels);
173 put_le16(pb, codec->frame_size);
178 /* hack to have real time */
179 fst->pts = gettime();
182 /* flush until end of block reached */
183 while ((url_ftell(pb) % ffm->packet_size) != 0)
186 put_flush_packet(pb);
188 /* init packet mux */
189 ffm->packet_ptr = ffm->packet;
190 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
191 ffm->frame_offset = 0;
193 ffm->first_packet = 1;
197 for(i=0;i<s->nb_streams;i++) {
207 static int ffm_write_packet(AVFormatContext *s, int stream_index,
208 UINT8 *buf, int size, int force_pts)
210 AVStream *st = s->streams[stream_index];
211 FFMStream *fst = st->priv_data;
213 UINT8 header[FRAME_HEADER_SIZE];
216 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
217 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
219 duration = (1000000.0 * FRAME_RATE_BASE / (float)st->codec.frame_rate);
223 /* packet size & key_frame */
224 header[0] = stream_index;
226 if (st->codec.key_frame)
227 header[1] |= FLAG_KEY_FRAME;
228 header[2] = (size >> 16) & 0xff;
229 header[3] = (size >> 8) & 0xff;
230 header[4] = size & 0xff;
231 header[5] = (duration >> 16) & 0xff;
232 header[6] = (duration >> 8) & 0xff;
233 header[7] = duration & 0xff;
234 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
235 ffm_write_data(s, buf, size, pts, 0);
237 fst->pts += duration;
241 static int ffm_write_trailer(AVFormatContext *s)
243 ByteIOContext *pb = &s->pb;
244 FFMContext *ffm = s->priv_data;
248 if (ffm->packet_ptr > ffm->packet)
251 put_flush_packet(pb);
253 for(i=0;i<s->nb_streams;i++)
254 free(s->streams[i]->priv_data);
261 static int ffm_is_avail_data(AVFormatContext *s, int size)
263 FFMContext *ffm = s->priv_data;
264 offset_t pos, avail_size;
267 len = ffm->packet_end - ffm->packet_ptr;
270 pos = url_ftell(&s->pb);
271 if (pos == ffm->write_index) {
272 /* exactly at the end of stream */
274 } else if (pos < ffm->write_index) {
275 avail_size = ffm->write_index - pos;
277 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
279 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
280 if (size <= avail_size)
286 /* first is true if we read the frame header */
287 static int ffm_read_data(AVFormatContext *s,
288 UINT8 *buf, int size, int first)
290 FFMContext *ffm = s->priv_data;
291 ByteIOContext *pb = &s->pb;
292 int len, fill_size, size1, frame_offset;
297 len = ffm->packet_end - ffm->packet_ptr;
301 if (url_ftell(pb) == ffm->file_size)
302 url_fseek(pb, ffm->packet_size, SEEK_SET);
303 get_be16(pb); /* PACKET_ID */
304 fill_size = get_be16(pb);
305 ffm->pts = get_be64(pb);
306 frame_offset = get_be16(pb);
307 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
308 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
309 /* if first packet or resynchronization packet, we must
310 handle it specifically */
311 if (ffm->first_packet || (frame_offset & 0x8000)) {
312 ffm->first_packet = 0;
313 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
317 ffm->packet_ptr = ffm->packet;
321 memcpy(buf, ffm->packet_ptr, len);
323 ffm->packet_ptr += len;
331 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
336 ByteIOContext *pb = &s->pb;
337 AVCodecContext *codec;
341 ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
349 if (tag != MKTAG('F', 'F', 'M', '1'))
351 ffm->packet_size = get_be32(pb);
352 if (ffm->packet_size != FFM_PACKET_SIZE)
354 ffm->write_index = get_be64(pb);
355 /* get also filesize */
356 if (!url_is_streamed(pb)) {
357 ffm->file_size = url_filesize(url_fileno(pb));
359 ffm->file_size = (UINT64_C(1) << 63) - 1;
362 s->nb_streams = get_be32(pb);
363 get_be32(pb); /* total bitrate */
364 /* read each stream */
365 for(i=0;i<s->nb_streams;i++) {
366 st = av_mallocz(sizeof(AVStream));
370 fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
377 st->codec.codec_id = get_be32(pb);
378 st->codec.codec_type = get_byte(pb); /* codec_type */
379 codec->bit_rate = get_be32(pb);
380 codec->flags = get_be32(pb);
382 switch(codec->codec_type) {
383 case CODEC_TYPE_VIDEO:
384 codec->frame_rate = ((INT64)get_be32(pb) * FRAME_RATE_BASE) / 1000;
385 codec->width = get_be16(pb);
386 codec->height = get_be16(pb);
387 codec->gop_size = get_be16(pb);
388 codec->qmin = get_byte(pb);
389 codec->qmax = get_byte(pb);
390 codec->max_qdiff = get_byte(pb);
391 codec->qcompress = get_be16(pb) / 10000.0;
392 codec->qblur = get_be16(pb) / 10000.0;
394 case CODEC_TYPE_AUDIO:
395 codec->sample_rate = get_be32(pb);
396 codec->channels = get_le16(pb);
397 codec->frame_size = get_le16(pb);
405 /* get until end of block reached */
406 while ((url_ftell(pb) % ffm->packet_size) != 0)
409 /* init packet demux */
410 ffm->packet_ptr = ffm->packet;
411 ffm->packet_end = ffm->packet;
412 ffm->frame_offset = 0;
414 ffm->read_state = READ_HEADER;
415 ffm->first_packet = 1;
418 for(i=0;i<s->nb_streams;i++) {
432 /* return < 0 if eof */
433 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
436 FFMContext *ffm = s->priv_data;
439 switch(ffm->read_state) {
441 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE))
444 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
445 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
447 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != FRAME_HEADER_SIZE)
453 for(i=0;i<FRAME_HEADER_SIZE;i++)
454 printf("%02x ", ffm->header[i]);
458 ffm->read_state = READ_DATA;
461 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
462 if (!ffm_is_avail_data(s, size)) {
466 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
468 av_new_packet(pkt, size);
469 pkt->stream_index = ffm->header[0];
470 if (ffm->header[1] & FLAG_KEY_FRAME)
471 pkt->flags |= PKT_FLAG_KEY;
473 ffm->read_state = READ_HEADER;
474 if (ffm_read_data(s, pkt->data, size, 0) != size) {
475 /* bad case: desynchronized packet. we cancel all the packet loading */
480 pkt->duration = duration;
488 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
489 by the write position inside this function */
490 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
492 FFMContext *ffm = s->priv_data;
493 ByteIOContext *pb = &s->pb;
496 pos = pos1 + ffm->write_index;
497 if (pos >= ffm->file_size)
498 pos -= (ffm->file_size - FFM_PACKET_SIZE);
500 printf("seek to %Lx -> %Lx\n", pos1, pos);
502 url_fseek(pb, pos, SEEK_SET);
505 static INT64 get_pts(AVFormatContext *s, offset_t pos)
507 ByteIOContext *pb = &s->pb;
514 printf("pts=%0.6f\n", pts / 1000000.0);
519 /* seek to a given time in the file. The file read pointer is
520 positionned at or before pts. XXX: the following code is quite
522 static int ffm_seek(AVFormatContext *s, INT64 wanted_pts)
524 FFMContext *ffm = s->priv_data;
525 offset_t pos_min, pos_max, pos;
526 INT64 pts_min, pts_max, pts;
530 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
532 /* find the position using linear interpolation (better than
533 dichotomy in typical cases) */
535 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
536 while (pos_min <= pos_max) {
537 pts_min = get_pts(s, pos_min);
538 pts_max = get_pts(s, pos_max);
539 /* linear interpolation */
540 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
541 (double)(pts_max - pts_min);
542 pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
545 else if (pos >= pos_max)
547 pts = get_pts(s, pos);
548 /* check if we are lucky */
549 if (pts == wanted_pts) {
551 } else if (pts > wanted_pts) {
552 pos_max = pos - FFM_PACKET_SIZE;
554 pos_min = pos + FFM_PACKET_SIZE;
559 pos -= FFM_PACKET_SIZE;
565 offset_t ffm_read_write_index(int fd)
571 lseek(fd, 8, SEEK_SET);
575 pos |= buf[i] << (56 - i * 8);
579 void ffm_write_write_index(int fd, offset_t pos)
585 buf[i] = (pos >> (56 - i * 8)) & 0xff;
586 lseek(fd, 8, SEEK_SET);
590 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
592 FFMContext *ffm = s->priv_data;
593 ffm->write_index = pos;
594 ffm->file_size = file_size;
597 static int ffm_read_close(AVFormatContext *s)
602 for(i=0;i<s->nb_streams;i++) {
610 AVFormat ffm_format = {
615 /* not really used */