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