* Change extern inline to static inline. This improves compiling without optimization.
[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 */
27#define FRAME_HEADER_SIZE 5
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);
156 /* specific info */
157 switch(codec->codec_type) {
158 case CODEC_TYPE_VIDEO:
159 put_be32(pb, (codec->frame_rate * 1000) / FRAME_RATE_BASE);
160 put_be16(pb, codec->width);
161 put_be16(pb, codec->height);
4606ac8d
ZK
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));
de6d9b64
FB
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,
10bb7023 203 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
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 */
4606ac8d 276static int ffm_read_data(AVFormatContext *s,
de6d9b64
FB
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 {
8be1c656 348 ffm->file_size = (UINT64_C(1) << 63) - 1;
de6d9b64
FB
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);
4606ac8d
ZK
375 codec->qmin = get_byte(pb);
376 codec->qmax = get_byte(pb);
377 codec->max_qdiff = get_byte(pb);
378 codec->qcompress = get_be16(pb) / 10000.0;
379 codec->qblur = get_be16(pb) / 10000.0;
de6d9b64
FB
380 break;
381 case CODEC_TYPE_AUDIO:
382 codec->sample_rate = get_be32(pb);
383 codec->channels = get_le16(pb);
384 break;
385 }
386
387 }
388
389 /* get until end of block reached */
390 while ((url_ftell(pb) % ffm->packet_size) != 0)
391 get_byte(pb);
392
393 /* init packet demux */
394 ffm->packet_ptr = ffm->packet;
395 ffm->packet_end = ffm->packet;
396 ffm->frame_offset = 0;
397 ffm->pts = 0;
398 ffm->read_state = READ_HEADER;
399 ffm->first_packet = 1;
400 return 0;
401 fail:
402 for(i=0;i<s->nb_streams;i++) {
403 st = s->streams[i];
404 if (st) {
405 fst = st->priv_data;
406 if (fst)
407 free(fst);
408 free(st);
409 }
410 }
411 if (ffm)
412 free(ffm);
413 return -1;
414}
415
416/* return < 0 if eof */
417static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
418{
419 int size;
420 FFMContext *ffm = s->priv_data;
421
422 switch(ffm->read_state) {
423 case READ_HEADER:
424 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE))
425 return -EAGAIN;
426#if 0
4606ac8d 427 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
de6d9b64
FB
428 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
429#endif
430 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != FRAME_HEADER_SIZE)
431 return -EAGAIN;
4606ac8d 432
de6d9b64
FB
433#if 0
434 {
435 int i;
436 for(i=0;i<FRAME_HEADER_SIZE;i++)
437 printf("%02x ", ffm->header[i]);
438 printf("\n");
439 }
440#endif
441 ffm->read_state = READ_DATA;
442 /* fall thru */
443 case READ_DATA:
444 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
445 if (!ffm_is_avail_data(s, size)) {
446 return -EAGAIN;
447 }
448
449 av_new_packet(pkt, size);
450 pkt->stream_index = ffm->header[0];
451 if (ffm->header[1] & FLAG_KEY_FRAME)
452 pkt->flags |= PKT_FLAG_KEY;
4606ac8d 453
de6d9b64
FB
454 ffm->read_state = READ_HEADER;
455 if (ffm_read_data(s, pkt->data, size, 0) != size) {
456 /* bad case: desynchronized packet. we cancel all the packet loading */
457 av_free_packet(pkt);
458 return -EAGAIN;
459 }
460 break;
461 }
462 return 0;
463}
464
465//#define DEBUG_SEEK
466
467/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
468 by the write position inside this function */
469static void ffm_seek1(AVFormatContext *s, offset_t pos1)
470{
471 FFMContext *ffm = s->priv_data;
472 ByteIOContext *pb = &s->pb;
473 offset_t pos;
474
475 pos = pos1 + ffm->write_index;
476 if (pos >= ffm->file_size)
477 pos -= (ffm->file_size - FFM_PACKET_SIZE);
478#ifdef DEBUG_SEEK
479 printf("seek to %Lx -> %Lx\n", pos1, pos);
480#endif
481 url_fseek(pb, pos, SEEK_SET);
482}
483
484static INT64 get_pts(AVFormatContext *s, offset_t pos)
485{
486 ByteIOContext *pb = &s->pb;
487 INT64 pts;
488
4606ac8d 489 ffm_seek1(s, pos);
de6d9b64
FB
490 url_fskip(pb, 4);
491 pts = get_be64(pb);
492#ifdef DEBUG_SEEK
493 printf("pts=%0.6f\n", pts / 1000000.0);
494#endif
495 return pts;
496}
497
498/* seek to a given time in the file. The file read pointer is
499 positionned at or before pts. XXX: the following code is quite
500 approximative */
501static int ffm_seek(AVFormatContext *s, INT64 wanted_pts)
502{
503 FFMContext *ffm = s->priv_data;
504 offset_t pos_min, pos_max, pos;
505 INT64 pts_min, pts_max, pts;
506 double pos1;
507
508#ifdef DEBUG_SEEK
509 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
510#endif
511 /* find the position using linear interpolation (better than
512 dichotomy in typical cases) */
513 pos_min = 0;
514 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
515 while (pos_min <= pos_max) {
516 pts_min = get_pts(s, pos_min);
517 pts_max = get_pts(s, pos_max);
518 /* linear interpolation */
4606ac8d 519 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
de6d9b64
FB
520 (double)(pts_max - pts_min);
521 pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
522 if (pos <= pos_min)
523 pos = pos_min;
524 else if (pos >= pos_max)
525 pos = pos_max;
526 pts = get_pts(s, pos);
527 /* check if we are lucky */
528 if (pts == wanted_pts) {
529 goto found;
530 } else if (pts > wanted_pts) {
531 pos_max = pos - FFM_PACKET_SIZE;
532 } else {
533 pos_min = pos + FFM_PACKET_SIZE;
534 }
535 }
536 pos = pos_min;
537 if (pos > 0)
538 pos -= FFM_PACKET_SIZE;
539 found:
540 ffm_seek1(s, pos);
541 return 0;
542}
543
544offset_t ffm_read_write_index(int fd)
545{
546 UINT8 buf[8];
547 offset_t pos;
548 int i;
549
550 lseek(fd, 8, SEEK_SET);
551 read(fd, buf, 8);
552 pos = 0;
4606ac8d 553 for(i=0;i<8;i++)
de6d9b64
FB
554 pos |= buf[i] << (56 - i * 8);
555 return pos;
556}
557
558void ffm_write_write_index(int fd, offset_t pos)
559{
560 UINT8 buf[8];
561 int i;
562
4606ac8d 563 for(i=0;i<8;i++)
de6d9b64
FB
564 buf[i] = (pos >> (56 - i * 8)) & 0xff;
565 lseek(fd, 8, SEEK_SET);
566 write(fd, buf, 8);
567}
568
569void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
570{
571 FFMContext *ffm = s->priv_data;
572 ffm->write_index = pos;
573 ffm->file_size = file_size;
574}
575
576static int ffm_read_close(AVFormatContext *s)
577{
578 AVStream *st;
579 int i;
580
581 for(i=0;i<s->nb_streams;i++) {
582 st = s->streams[i];
583 free(st->priv_data);
584 }
585 free(s->priv_data);
586 return 0;
587}
588
589AVFormat ffm_format = {
590 "ffm",
591 "ffm format",
592 "",
593 "ffm",
594 /* not really used */
595 CODEC_ID_MP2,
596 CODEC_ID_MPEG1VIDEO,
597 ffm_write_header,
598 ffm_write_packet,
599 ffm_write_trailer,
600 ffm_read_header,
601 ffm_read_packet,
602 ffm_read_close,
603 ffm_seek,
604};