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