Fix the WAIT_FEED problem. It turns out that when you open up an FFM
[libav.git] / libav / ffm.c
CommitLineData
de6d9b64
FB
1/*
2 * FFM (ffserver live feed) encoder and decoder
3 * Copyright (c) 2001 Gerard Lantau.
4 *
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.
9 *
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.
14 *
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.
18 */
de6d9b64 19#include "avformat.h"
8be1c656 20#include <unistd.h>
de6d9b64
FB
21
22/* The FFM file is made of blocks of fixed size */
23#define FFM_HEADER_SIZE 14
24#define PACKET_ID 0x666d
25
26/* each packet contains frames (which can span several packets */
20f01548 27#define FRAME_HEADER_SIZE 8
de6d9b64
FB
28#define FLAG_KEY_FRAME 0x01
29
30typedef struct FFMStream {
31 INT64 pts;
32} FFMStream;
33
34enum {
35 READ_HEADER,
36 READ_DATA,
37};
38
39typedef struct FFMContext {
40 /* only reading mode */
41 offset_t write_index, file_size;
4606ac8d 42 int read_state;
de6d9b64
FB
43 UINT8 header[FRAME_HEADER_SIZE];
44
45 /* read and write */
46 int first_packet; /* true if first packet, needed to set the discontinuity tag */
47 int packet_size;
48 int frame_offset;
49 INT64 pts;
50 UINT8 *packet_ptr, *packet_end;
51 UINT8 packet[1]; /* must be last */
52} FFMContext;
53
54static void flush_packet(AVFormatContext *s)
55{
56 FFMContext *ffm = s->priv_data;
57 int fill_size, h;
58 ByteIOContext *pb = &s->pb;
59
60 fill_size = ffm->packet_end - ffm->packet_ptr;
61 memset(ffm->packet_ptr, 0, fill_size);
4606ac8d 62
de6d9b64 63 /* put header */
4606ac8d 64 put_be16(pb, PACKET_ID);
de6d9b64
FB
65 put_be16(pb, fill_size);
66 put_be64(pb, ffm->pts);
67 h = ffm->frame_offset;
68 if (ffm->first_packet)
69 h |= 0x8000;
70 put_be16(pb, h);
71 put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
72
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;
78}
79
80/* 'first' is true if first data of a frame */
4606ac8d
ZK
81static void ffm_write_data(AVFormatContext *s,
82 UINT8 *buf, int size,
de6d9b64
FB
83 INT64 pts, int first)
84{
85 FFMContext *ffm = s->priv_data;
86 int len;
87
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)
91 ffm->pts = pts;
92
93 /* write as many packets as needed */
94 while (size > 0) {
95 len = ffm->packet_end - ffm->packet_ptr;
96 if (len > size)
97 len = size;
98 memcpy(ffm->packet_ptr, buf, len);
4606ac8d 99
de6d9b64
FB
100 ffm->packet_ptr += len;
101 buf += len;
102 size -= len;
103 if (ffm->packet_ptr >= ffm->packet_end) {
104 /* special case : no pts in packet : we leave the current one */
105 if (ffm->pts == 0)
106 ffm->pts = pts;
107
108 flush_packet(s);
109 }
110 }
111}
112
113static int ffm_write_header(AVFormatContext *s)
114{
115 AVStream *st;
116 FFMStream *fst;
117 FFMContext *ffm;
118 ByteIOContext *pb = &s->pb;
119 AVCodecContext *codec;
120 int bit_rate, i;
121
122 ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
123 if (!ffm)
124 return -1;
125
126 s->priv_data = ffm;
127 ffm->packet_size = FFM_PACKET_SIZE;
4606ac8d 128
de6d9b64
FB
129 /* header */
130 put_tag(pb, "FFM1");
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 */
134
135 put_be32(pb, s->nb_streams);
136 bit_rate = 0;
137 for(i=0;i<s->nb_streams;i++) {
138 st = s->streams[i];
139 bit_rate += st->codec.bit_rate;
140 }
141 put_be32(pb, bit_rate);
142
143 /* list of streams */
144 for(i=0;i<s->nb_streams;i++) {
145 st = s->streams[i];
146 fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
147 if (!fst)
148 goto fail;
149 st->priv_data = fst;
150
151 codec = &st->codec;
152 /* generic info */
153 put_be32(pb, codec->codec_id);
154 put_byte(pb, codec->codec_type);
155 put_be32(pb, codec->bit_rate);
ccac2e27 156 put_be32(pb, codec->flags);
de6d9b64
FB
157 /* specific info */
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);
20f01548
PG
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));
3884a3c3 169 put_be32(pb, codec->bit_rate_tolerance);
de6d9b64
FB
170 break;
171 case CODEC_TYPE_AUDIO:
172 put_be32(pb, codec->sample_rate);
173 put_le16(pb, codec->channels);
20f01548 174 put_le16(pb, codec->frame_size);
de6d9b64 175 break;
20f01548
PG
176 default:
177 abort();
de6d9b64
FB
178 }
179 /* hack to have real time */
180 fst->pts = gettime();
181 }
182
183 /* flush until end of block reached */
184 while ((url_ftell(pb) % ffm->packet_size) != 0)
185 put_byte(pb, 0);
186
187 put_flush_packet(pb);
188
189 /* init packet mux */
190 ffm->packet_ptr = ffm->packet;
191 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
192 ffm->frame_offset = 0;
193 ffm->pts = 0;
194 ffm->first_packet = 1;
195
196 return 0;
197 fail:
198 for(i=0;i<s->nb_streams;i++) {
199 st = s->streams[i];
200 fst = st->priv_data;
201 if (fst)
202 free(fst);
203 }
204 free(ffm);
205 return -1;
206}
207
208static int ffm_write_packet(AVFormatContext *s, int stream_index,
10bb7023 209 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
210{
211 AVStream *st = s->streams[stream_index];
212 FFMStream *fst = st->priv_data;
213 INT64 pts;
214 UINT8 header[FRAME_HEADER_SIZE];
20f01548
PG
215 int duration;
216
217 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
218 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
219 } else {
220 duration = (1000000.0 * FRAME_RATE_BASE / (float)st->codec.frame_rate);
221 }
de6d9b64
FB
222
223 pts = fst->pts;
224 /* packet size & key_frame */
225 header[0] = stream_index;
226 header[1] = 0;
227 if (st->codec.key_frame)
228 header[1] |= FLAG_KEY_FRAME;
229 header[2] = (size >> 16) & 0xff;
230 header[3] = (size >> 8) & 0xff;
231 header[4] = size & 0xff;
20f01548
PG
232 header[5] = (duration >> 16) & 0xff;
233 header[6] = (duration >> 8) & 0xff;
234 header[7] = duration & 0xff;
de6d9b64
FB
235 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
236 ffm_write_data(s, buf, size, pts, 0);
237
20f01548 238 fst->pts += duration;
de6d9b64
FB
239 return 0;
240}
241
242static int ffm_write_trailer(AVFormatContext *s)
243{
244 ByteIOContext *pb = &s->pb;
245 FFMContext *ffm = s->priv_data;
246 int i;
247
248 /* flush packets */
249 if (ffm->packet_ptr > ffm->packet)
250 flush_packet(s);
251
252 put_flush_packet(pb);
253
254 for(i=0;i<s->nb_streams;i++)
255 free(s->streams[i]->priv_data);
256 free(ffm);
257 return 0;
258}
259
260/* ffm demux */
261
262static int ffm_is_avail_data(AVFormatContext *s, int size)
263{
264 FFMContext *ffm = s->priv_data;
265 offset_t pos, avail_size;
266 int len;
267
268 len = ffm->packet_end - ffm->packet_ptr;
269 if (size <= len)
270 return 1;
271 pos = url_ftell(&s->pb);
272 if (pos == ffm->write_index) {
273 /* exactly at the end of stream */
274 return 0;
275 } else if (pos < ffm->write_index) {
276 avail_size = ffm->write_index - pos;
277 } else {
278 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
279 }
280 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
281 if (size <= avail_size)
282 return 1;
283 else
284 return 0;
285}
286
287/* first is true if we read the frame header */
4606ac8d 288static int ffm_read_data(AVFormatContext *s,
de6d9b64
FB
289 UINT8 *buf, int size, int first)
290{
291 FFMContext *ffm = s->priv_data;
292 ByteIOContext *pb = &s->pb;
293 int len, fill_size, size1, frame_offset;
294
295 size1 = size;
296 while (size > 0) {
297 redo:
298 len = ffm->packet_end - ffm->packet_ptr;
299 if (len > size)
300 len = size;
301 if (len == 0) {
302 if (url_ftell(pb) == ffm->file_size)
303 url_fseek(pb, ffm->packet_size, SEEK_SET);
5e57424d 304 retry_read:
de6d9b64
FB
305 get_be16(pb); /* PACKET_ID */
306 fill_size = get_be16(pb);
307 ffm->pts = get_be64(pb);
308 frame_offset = get_be16(pb);
309 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
310 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
311 /* if first packet or resynchronization packet, we must
312 handle it specifically */
313 if (ffm->first_packet || (frame_offset & 0x8000)) {
5e57424d
PG
314 if (!frame_offset) {
315 /* This packet has no frame headers in it */
316 if (url_ftell(pb) >= ffm->packet_size * 3) {
317 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
318 goto retry_read;
319 }
320 /* This is bad, we cannot find a valid frame header */
321 return 0;
322 }
de6d9b64 323 ffm->first_packet = 0;
5e57424d
PG
324 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
325 abort();
de6d9b64
FB
326 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
327 if (!first)
328 break;
329 } else {
330 ffm->packet_ptr = ffm->packet;
331 }
332 goto redo;
333 }
334 memcpy(buf, ffm->packet_ptr, len);
335 buf += len;
336 ffm->packet_ptr += len;
337 size -= len;
338 first = 0;
339 }
340 return size1 - size;
341}
342
343
344static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
345{
346 AVStream *st;
347 FFMStream *fst;
348 FFMContext *ffm;
349 ByteIOContext *pb = &s->pb;
350 AVCodecContext *codec;
351 int i;
352 UINT32 tag;
353
354 ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
355 if (!ffm)
356 return -1;
357
358 s->priv_data = ffm;
359
360 /* header */
361 tag = get_le32(pb);
362 if (tag != MKTAG('F', 'F', 'M', '1'))
363 goto fail;
364 ffm->packet_size = get_be32(pb);
365 if (ffm->packet_size != FFM_PACKET_SIZE)
366 goto fail;
367 ffm->write_index = get_be64(pb);
368 /* get also filesize */
369 if (!url_is_streamed(pb)) {
370 ffm->file_size = url_filesize(url_fileno(pb));
371 } else {
8be1c656 372 ffm->file_size = (UINT64_C(1) << 63) - 1;
de6d9b64
FB
373 }
374
375 s->nb_streams = get_be32(pb);
376 get_be32(pb); /* total bitrate */
377 /* read each stream */
378 for(i=0;i<s->nb_streams;i++) {
379 st = av_mallocz(sizeof(AVStream));
380 if (!st)
381 goto fail;
382 s->streams[i] = st;
383 fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
384 if (!fst)
385 goto fail;
386 st->priv_data = fst;
387
388 codec = &st->codec;
389 /* generic info */
390 st->codec.codec_id = get_be32(pb);
391 st->codec.codec_type = get_byte(pb); /* codec_type */
392 codec->bit_rate = get_be32(pb);
ccac2e27 393 codec->flags = get_be32(pb);
de6d9b64
FB
394 /* specific info */
395 switch(codec->codec_type) {
396 case CODEC_TYPE_VIDEO:
397 codec->frame_rate = ((INT64)get_be32(pb) * FRAME_RATE_BASE) / 1000;
398 codec->width = get_be16(pb);
399 codec->height = get_be16(pb);
20f01548
PG
400 codec->gop_size = get_be16(pb);
401 codec->qmin = get_byte(pb);
402 codec->qmax = get_byte(pb);
403 codec->max_qdiff = get_byte(pb);
404 codec->qcompress = get_be16(pb) / 10000.0;
405 codec->qblur = get_be16(pb) / 10000.0;
3884a3c3 406 codec->bit_rate_tolerance = get_be32(pb);
de6d9b64
FB
407 break;
408 case CODEC_TYPE_AUDIO:
409 codec->sample_rate = get_be32(pb);
410 codec->channels = get_le16(pb);
20f01548 411 codec->frame_size = get_le16(pb);
de6d9b64 412 break;
20f01548
PG
413 default:
414 abort();
de6d9b64
FB
415 }
416
417 }
418
419 /* get until end of block reached */
420 while ((url_ftell(pb) % ffm->packet_size) != 0)
421 get_byte(pb);
422
423 /* init packet demux */
424 ffm->packet_ptr = ffm->packet;
425 ffm->packet_end = ffm->packet;
426 ffm->frame_offset = 0;
427 ffm->pts = 0;
428 ffm->read_state = READ_HEADER;
429 ffm->first_packet = 1;
430 return 0;
431 fail:
432 for(i=0;i<s->nb_streams;i++) {
433 st = s->streams[i];
434 if (st) {
435 fst = st->priv_data;
436 if (fst)
437 free(fst);
438 free(st);
439 }
440 }
441 if (ffm)
442 free(ffm);
443 return -1;
444}
445
446/* return < 0 if eof */
447static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
448{
449 int size;
450 FFMContext *ffm = s->priv_data;
20f01548 451 int duration;
de6d9b64
FB
452
453 switch(ffm->read_state) {
454 case READ_HEADER:
455 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE))
456 return -EAGAIN;
457#if 0
4606ac8d 458 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
de6d9b64
FB
459 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
460#endif
461 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != FRAME_HEADER_SIZE)
462 return -EAGAIN;
4606ac8d 463
de6d9b64
FB
464#if 0
465 {
466 int i;
467 for(i=0;i<FRAME_HEADER_SIZE;i++)
468 printf("%02x ", ffm->header[i]);
469 printf("\n");
470 }
471#endif
472 ffm->read_state = READ_DATA;
473 /* fall thru */
474 case READ_DATA:
475 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
476 if (!ffm_is_avail_data(s, size)) {
477 return -EAGAIN;
478 }
479
20f01548
PG
480 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
481
de6d9b64
FB
482 av_new_packet(pkt, size);
483 pkt->stream_index = ffm->header[0];
484 if (ffm->header[1] & FLAG_KEY_FRAME)
485 pkt->flags |= PKT_FLAG_KEY;
4606ac8d 486
de6d9b64
FB
487 ffm->read_state = READ_HEADER;
488 if (ffm_read_data(s, pkt->data, size, 0) != size) {
489 /* bad case: desynchronized packet. we cancel all the packet loading */
490 av_free_packet(pkt);
491 return -EAGAIN;
492 }
20f01548
PG
493 pkt->pts = ffm->pts;
494 pkt->duration = duration;
de6d9b64
FB
495 break;
496 }
497 return 0;
498}
499
500//#define DEBUG_SEEK
501
502/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
503 by the write position inside this function */
504static void ffm_seek1(AVFormatContext *s, offset_t pos1)
505{
506 FFMContext *ffm = s->priv_data;
507 ByteIOContext *pb = &s->pb;
508 offset_t pos;
509
510 pos = pos1 + ffm->write_index;
511 if (pos >= ffm->file_size)
512 pos -= (ffm->file_size - FFM_PACKET_SIZE);
513#ifdef DEBUG_SEEK
514 printf("seek to %Lx -> %Lx\n", pos1, pos);
515#endif
516 url_fseek(pb, pos, SEEK_SET);
517}
518
519static INT64 get_pts(AVFormatContext *s, offset_t pos)
520{
521 ByteIOContext *pb = &s->pb;
522 INT64 pts;
523
4606ac8d 524 ffm_seek1(s, pos);
de6d9b64
FB
525 url_fskip(pb, 4);
526 pts = get_be64(pb);
527#ifdef DEBUG_SEEK
528 printf("pts=%0.6f\n", pts / 1000000.0);
529#endif
530 return pts;
531}
532
533/* seek to a given time in the file. The file read pointer is
534 positionned at or before pts. XXX: the following code is quite
535 approximative */
536static int ffm_seek(AVFormatContext *s, INT64 wanted_pts)
537{
538 FFMContext *ffm = s->priv_data;
539 offset_t pos_min, pos_max, pos;
540 INT64 pts_min, pts_max, pts;
541 double pos1;
542
543#ifdef DEBUG_SEEK
544 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
545#endif
546 /* find the position using linear interpolation (better than
547 dichotomy in typical cases) */
548 pos_min = 0;
549 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
550 while (pos_min <= pos_max) {
551 pts_min = get_pts(s, pos_min);
552 pts_max = get_pts(s, pos_max);
553 /* linear interpolation */
4606ac8d 554 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
de6d9b64
FB
555 (double)(pts_max - pts_min);
556 pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
557 if (pos <= pos_min)
558 pos = pos_min;
559 else if (pos >= pos_max)
560 pos = pos_max;
561 pts = get_pts(s, pos);
562 /* check if we are lucky */
563 if (pts == wanted_pts) {
564 goto found;
565 } else if (pts > wanted_pts) {
566 pos_max = pos - FFM_PACKET_SIZE;
567 } else {
568 pos_min = pos + FFM_PACKET_SIZE;
569 }
570 }
571 pos = pos_min;
572 if (pos > 0)
573 pos -= FFM_PACKET_SIZE;
574 found:
575 ffm_seek1(s, pos);
576 return 0;
577}
578
579offset_t ffm_read_write_index(int fd)
580{
581 UINT8 buf[8];
582 offset_t pos;
583 int i;
584
585 lseek(fd, 8, SEEK_SET);
586 read(fd, buf, 8);
587 pos = 0;
4606ac8d 588 for(i=0;i<8;i++)
de6d9b64
FB
589 pos |= buf[i] << (56 - i * 8);
590 return pos;
591}
592
593void ffm_write_write_index(int fd, offset_t pos)
594{
595 UINT8 buf[8];
596 int i;
597
4606ac8d 598 for(i=0;i<8;i++)
de6d9b64
FB
599 buf[i] = (pos >> (56 - i * 8)) & 0xff;
600 lseek(fd, 8, SEEK_SET);
601 write(fd, buf, 8);
602}
603
604void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
605{
606 FFMContext *ffm = s->priv_data;
607 ffm->write_index = pos;
608 ffm->file_size = file_size;
609}
610
611static int ffm_read_close(AVFormatContext *s)
612{
613 AVStream *st;
614 int i;
615
616 for(i=0;i<s->nb_streams;i++) {
617 st = s->streams[i];
618 free(st->priv_data);
619 }
620 free(s->priv_data);
621 return 0;
622}
623
624AVFormat ffm_format = {
625 "ffm",
626 "ffm format",
627 "",
628 "ffm",
629 /* not really used */
630 CODEC_ID_MP2,
631 CODEC_ID_MPEG1VIDEO,
632 ffm_write_header,
633 ffm_write_packet,
634 ffm_write_trailer,
635 ffm_read_header,
636 ffm_read_packet,
637 ffm_read_close,
638 ffm_seek,
639};