updated for my short term goals
[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);
304 get_be16(pb); /* PACKET_ID */
305 fill_size = get_be16(pb);
306 ffm->pts = get_be64(pb);
307 frame_offset = get_be16(pb);
308 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
309 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
310 /* if first packet or resynchronization packet, we must
311 handle it specifically */
312 if (ffm->first_packet || (frame_offset & 0x8000)) {
313 ffm->first_packet = 0;
314 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
315 if (!first)
316 break;
317 } else {
318 ffm->packet_ptr = ffm->packet;
319 }
320 goto redo;
321 }
322 memcpy(buf, ffm->packet_ptr, len);
323 buf += len;
324 ffm->packet_ptr += len;
325 size -= len;
326 first = 0;
327 }
328 return size1 - size;
329}
330
331
332static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
333{
334 AVStream *st;
335 FFMStream *fst;
336 FFMContext *ffm;
337 ByteIOContext *pb = &s->pb;
338 AVCodecContext *codec;
339 int i;
340 UINT32 tag;
341
342 ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
343 if (!ffm)
344 return -1;
345
346 s->priv_data = ffm;
347
348 /* header */
349 tag = get_le32(pb);
350 if (tag != MKTAG('F', 'F', 'M', '1'))
351 goto fail;
352 ffm->packet_size = get_be32(pb);
353 if (ffm->packet_size != FFM_PACKET_SIZE)
354 goto fail;
355 ffm->write_index = get_be64(pb);
356 /* get also filesize */
357 if (!url_is_streamed(pb)) {
358 ffm->file_size = url_filesize(url_fileno(pb));
359 } else {
8be1c656 360 ffm->file_size = (UINT64_C(1) << 63) - 1;
de6d9b64
FB
361 }
362
363 s->nb_streams = get_be32(pb);
364 get_be32(pb); /* total bitrate */
365 /* read each stream */
366 for(i=0;i<s->nb_streams;i++) {
367 st = av_mallocz(sizeof(AVStream));
368 if (!st)
369 goto fail;
370 s->streams[i] = st;
371 fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
372 if (!fst)
373 goto fail;
374 st->priv_data = fst;
375
376 codec = &st->codec;
377 /* generic info */
378 st->codec.codec_id = get_be32(pb);
379 st->codec.codec_type = get_byte(pb); /* codec_type */
380 codec->bit_rate = get_be32(pb);
ccac2e27 381 codec->flags = get_be32(pb);
de6d9b64
FB
382 /* specific info */
383 switch(codec->codec_type) {
384 case CODEC_TYPE_VIDEO:
385 codec->frame_rate = ((INT64)get_be32(pb) * FRAME_RATE_BASE) / 1000;
386 codec->width = get_be16(pb);
387 codec->height = get_be16(pb);
20f01548
PG
388 codec->gop_size = get_be16(pb);
389 codec->qmin = get_byte(pb);
390 codec->qmax = get_byte(pb);
391 codec->max_qdiff = get_byte(pb);
392 codec->qcompress = get_be16(pb) / 10000.0;
393 codec->qblur = get_be16(pb) / 10000.0;
3884a3c3 394 codec->bit_rate_tolerance = get_be32(pb);
de6d9b64
FB
395 break;
396 case CODEC_TYPE_AUDIO:
397 codec->sample_rate = get_be32(pb);
398 codec->channels = get_le16(pb);
20f01548 399 codec->frame_size = get_le16(pb);
de6d9b64 400 break;
20f01548
PG
401 default:
402 abort();
de6d9b64
FB
403 }
404
405 }
406
407 /* get until end of block reached */
408 while ((url_ftell(pb) % ffm->packet_size) != 0)
409 get_byte(pb);
410
411 /* init packet demux */
412 ffm->packet_ptr = ffm->packet;
413 ffm->packet_end = ffm->packet;
414 ffm->frame_offset = 0;
415 ffm->pts = 0;
416 ffm->read_state = READ_HEADER;
417 ffm->first_packet = 1;
418 return 0;
419 fail:
420 for(i=0;i<s->nb_streams;i++) {
421 st = s->streams[i];
422 if (st) {
423 fst = st->priv_data;
424 if (fst)
425 free(fst);
426 free(st);
427 }
428 }
429 if (ffm)
430 free(ffm);
431 return -1;
432}
433
434/* return < 0 if eof */
435static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
436{
437 int size;
438 FFMContext *ffm = s->priv_data;
20f01548 439 int duration;
de6d9b64
FB
440
441 switch(ffm->read_state) {
442 case READ_HEADER:
443 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE))
444 return -EAGAIN;
445#if 0
4606ac8d 446 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
de6d9b64
FB
447 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
448#endif
449 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != FRAME_HEADER_SIZE)
450 return -EAGAIN;
4606ac8d 451
de6d9b64
FB
452#if 0
453 {
454 int i;
455 for(i=0;i<FRAME_HEADER_SIZE;i++)
456 printf("%02x ", ffm->header[i]);
457 printf("\n");
458 }
459#endif
460 ffm->read_state = READ_DATA;
461 /* fall thru */
462 case READ_DATA:
463 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
464 if (!ffm_is_avail_data(s, size)) {
465 return -EAGAIN;
466 }
467
20f01548
PG
468 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
469
de6d9b64
FB
470 av_new_packet(pkt, size);
471 pkt->stream_index = ffm->header[0];
472 if (ffm->header[1] & FLAG_KEY_FRAME)
473 pkt->flags |= PKT_FLAG_KEY;
4606ac8d 474
de6d9b64
FB
475 ffm->read_state = READ_HEADER;
476 if (ffm_read_data(s, pkt->data, size, 0) != size) {
477 /* bad case: desynchronized packet. we cancel all the packet loading */
478 av_free_packet(pkt);
479 return -EAGAIN;
480 }
20f01548
PG
481 pkt->pts = ffm->pts;
482 pkt->duration = duration;
de6d9b64
FB
483 break;
484 }
485 return 0;
486}
487
488//#define DEBUG_SEEK
489
490/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
491 by the write position inside this function */
492static void ffm_seek1(AVFormatContext *s, offset_t pos1)
493{
494 FFMContext *ffm = s->priv_data;
495 ByteIOContext *pb = &s->pb;
496 offset_t pos;
497
498 pos = pos1 + ffm->write_index;
499 if (pos >= ffm->file_size)
500 pos -= (ffm->file_size - FFM_PACKET_SIZE);
501#ifdef DEBUG_SEEK
502 printf("seek to %Lx -> %Lx\n", pos1, pos);
503#endif
504 url_fseek(pb, pos, SEEK_SET);
505}
506
507static INT64 get_pts(AVFormatContext *s, offset_t pos)
508{
509 ByteIOContext *pb = &s->pb;
510 INT64 pts;
511
4606ac8d 512 ffm_seek1(s, pos);
de6d9b64
FB
513 url_fskip(pb, 4);
514 pts = get_be64(pb);
515#ifdef DEBUG_SEEK
516 printf("pts=%0.6f\n", pts / 1000000.0);
517#endif
518 return pts;
519}
520
521/* seek to a given time in the file. The file read pointer is
522 positionned at or before pts. XXX: the following code is quite
523 approximative */
524static int ffm_seek(AVFormatContext *s, INT64 wanted_pts)
525{
526 FFMContext *ffm = s->priv_data;
527 offset_t pos_min, pos_max, pos;
528 INT64 pts_min, pts_max, pts;
529 double pos1;
530
531#ifdef DEBUG_SEEK
532 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
533#endif
534 /* find the position using linear interpolation (better than
535 dichotomy in typical cases) */
536 pos_min = 0;
537 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
538 while (pos_min <= pos_max) {
539 pts_min = get_pts(s, pos_min);
540 pts_max = get_pts(s, pos_max);
541 /* linear interpolation */
4606ac8d 542 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
de6d9b64
FB
543 (double)(pts_max - pts_min);
544 pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
545 if (pos <= pos_min)
546 pos = pos_min;
547 else if (pos >= pos_max)
548 pos = pos_max;
549 pts = get_pts(s, pos);
550 /* check if we are lucky */
551 if (pts == wanted_pts) {
552 goto found;
553 } else if (pts > wanted_pts) {
554 pos_max = pos - FFM_PACKET_SIZE;
555 } else {
556 pos_min = pos + FFM_PACKET_SIZE;
557 }
558 }
559 pos = pos_min;
560 if (pos > 0)
561 pos -= FFM_PACKET_SIZE;
562 found:
563 ffm_seek1(s, pos);
564 return 0;
565}
566
567offset_t ffm_read_write_index(int fd)
568{
569 UINT8 buf[8];
570 offset_t pos;
571 int i;
572
573 lseek(fd, 8, SEEK_SET);
574 read(fd, buf, 8);
575 pos = 0;
4606ac8d 576 for(i=0;i<8;i++)
de6d9b64
FB
577 pos |= buf[i] << (56 - i * 8);
578 return pos;
579}
580
581void ffm_write_write_index(int fd, offset_t pos)
582{
583 UINT8 buf[8];
584 int i;
585
4606ac8d 586 for(i=0;i<8;i++)
de6d9b64
FB
587 buf[i] = (pos >> (56 - i * 8)) & 0xff;
588 lseek(fd, 8, SEEK_SET);
589 write(fd, buf, 8);
590}
591
592void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
593{
594 FFMContext *ffm = s->priv_data;
595 ffm->write_index = pos;
596 ffm->file_size = file_size;
597}
598
599static int ffm_read_close(AVFormatContext *s)
600{
601 AVStream *st;
602 int i;
603
604 for(i=0;i<s->nb_streams;i++) {
605 st = s->streams[i];
606 free(st->priv_data);
607 }
608 free(s->priv_data);
609 return 0;
610}
611
612AVFormat ffm_format = {
613 "ffm",
614 "ffm format",
615 "",
616 "ffm",
617 /* not really used */
618 CODEC_ID_MP2,
619 CODEC_ID_MPEG1VIDEO,
620 ffm_write_header,
621 ffm_write_packet,
622 ffm_write_trailer,
623 ffm_read_header,
624 ffm_read_packet,
625 ffm_read_close,
626 ffm_seek,
627};