* more warning cleanups
[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;
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
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);
62
63 /* put header */
64 put_be16(pb, PACKET_ID);
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 */
81static void ffm_write_data(AVFormatContext *s,
82 UINT8 *buf, int size,
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);
99
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;
128
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);
162 break;
163 case CODEC_TYPE_AUDIO:
164 put_be32(pb, codec->sample_rate);
165 put_le16(pb, codec->channels);
166 break;
167 }
168 /* hack to have real time */
169 fst->pts = gettime();
170 }
171
172 /* flush until end of block reached */
173 while ((url_ftell(pb) % ffm->packet_size) != 0)
174 put_byte(pb, 0);
175
176 put_flush_packet(pb);
177
178 /* init packet mux */
179 ffm->packet_ptr = ffm->packet;
180 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
181 ffm->frame_offset = 0;
182 ffm->pts = 0;
183 ffm->first_packet = 1;
184
185 return 0;
186 fail:
187 for(i=0;i<s->nb_streams;i++) {
188 st = s->streams[i];
189 fst = st->priv_data;
190 if (fst)
191 free(fst);
192 }
193 free(ffm);
194 return -1;
195}
196
197static int ffm_write_packet(AVFormatContext *s, int stream_index,
10bb7023 198 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
199{
200 AVStream *st = s->streams[stream_index];
201 FFMStream *fst = st->priv_data;
202 INT64 pts;
203 UINT8 header[FRAME_HEADER_SIZE];
204
205 pts = fst->pts;
206 /* packet size & key_frame */
207 header[0] = stream_index;
208 header[1] = 0;
209 if (st->codec.key_frame)
210 header[1] |= FLAG_KEY_FRAME;
211 header[2] = (size >> 16) & 0xff;
212 header[3] = (size >> 8) & 0xff;
213 header[4] = size & 0xff;
214 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
215 ffm_write_data(s, buf, size, pts, 0);
216
217 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
218 fst->pts += (INT64)((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
219 } else {
220 fst->pts += (INT64)(1000000.0 * FRAME_RATE_BASE / (float)st->codec.frame_rate);
221 }
222 return 0;
223}
224
225static int ffm_write_trailer(AVFormatContext *s)
226{
227 ByteIOContext *pb = &s->pb;
228 FFMContext *ffm = s->priv_data;
229 int i;
230
231 /* flush packets */
232 if (ffm->packet_ptr > ffm->packet)
233 flush_packet(s);
234
235 put_flush_packet(pb);
236
237 for(i=0;i<s->nb_streams;i++)
238 free(s->streams[i]->priv_data);
239 free(ffm);
240 return 0;
241}
242
243/* ffm demux */
244
245static int ffm_is_avail_data(AVFormatContext *s, int size)
246{
247 FFMContext *ffm = s->priv_data;
248 offset_t pos, avail_size;
249 int len;
250
251 len = ffm->packet_end - ffm->packet_ptr;
252 if (size <= len)
253 return 1;
254 pos = url_ftell(&s->pb);
255 if (pos == ffm->write_index) {
256 /* exactly at the end of stream */
257 return 0;
258 } else if (pos < ffm->write_index) {
259 avail_size = ffm->write_index - pos;
260 } else {
261 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
262 }
263 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
264 if (size <= avail_size)
265 return 1;
266 else
267 return 0;
268}
269
270/* first is true if we read the frame header */
271static int ffm_read_data(AVFormatContext *s,
272 UINT8 *buf, int size, int first)
273{
274 FFMContext *ffm = s->priv_data;
275 ByteIOContext *pb = &s->pb;
276 int len, fill_size, size1, frame_offset;
277
278 size1 = size;
279 while (size > 0) {
280 redo:
281 len = ffm->packet_end - ffm->packet_ptr;
282 if (len > size)
283 len = size;
284 if (len == 0) {
285 if (url_ftell(pb) == ffm->file_size)
286 url_fseek(pb, ffm->packet_size, SEEK_SET);
287 get_be16(pb); /* PACKET_ID */
288 fill_size = get_be16(pb);
289 ffm->pts = get_be64(pb);
290 frame_offset = get_be16(pb);
291 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
292 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
293 /* if first packet or resynchronization packet, we must
294 handle it specifically */
295 if (ffm->first_packet || (frame_offset & 0x8000)) {
296 ffm->first_packet = 0;
297 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
298 if (!first)
299 break;
300 } else {
301 ffm->packet_ptr = ffm->packet;
302 }
303 goto redo;
304 }
305 memcpy(buf, ffm->packet_ptr, len);
306 buf += len;
307 ffm->packet_ptr += len;
308 size -= len;
309 first = 0;
310 }
311 return size1 - size;
312}
313
314
315static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
316{
317 AVStream *st;
318 FFMStream *fst;
319 FFMContext *ffm;
320 ByteIOContext *pb = &s->pb;
321 AVCodecContext *codec;
322 int i;
323 UINT32 tag;
324
325 ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
326 if (!ffm)
327 return -1;
328
329 s->priv_data = ffm;
330
331 /* header */
332 tag = get_le32(pb);
333 if (tag != MKTAG('F', 'F', 'M', '1'))
334 goto fail;
335 ffm->packet_size = get_be32(pb);
336 if (ffm->packet_size != FFM_PACKET_SIZE)
337 goto fail;
338 ffm->write_index = get_be64(pb);
339 /* get also filesize */
340 if (!url_is_streamed(pb)) {
341 ffm->file_size = url_filesize(url_fileno(pb));
342 } else {
8be1c656 343 ffm->file_size = (UINT64_C(1) << 63) - 1;
de6d9b64
FB
344 }
345
346 s->nb_streams = get_be32(pb);
347 get_be32(pb); /* total bitrate */
348 /* read each stream */
349 for(i=0;i<s->nb_streams;i++) {
350 st = av_mallocz(sizeof(AVStream));
351 if (!st)
352 goto fail;
353 s->streams[i] = st;
354 fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
355 if (!fst)
356 goto fail;
357 st->priv_data = fst;
358
359 codec = &st->codec;
360 /* generic info */
361 st->codec.codec_id = get_be32(pb);
362 st->codec.codec_type = get_byte(pb); /* codec_type */
363 codec->bit_rate = get_be32(pb);
364 /* specific info */
365 switch(codec->codec_type) {
366 case CODEC_TYPE_VIDEO:
367 codec->frame_rate = ((INT64)get_be32(pb) * FRAME_RATE_BASE) / 1000;
368 codec->width = get_be16(pb);
369 codec->height = get_be16(pb);
370 break;
371 case CODEC_TYPE_AUDIO:
372 codec->sample_rate = get_be32(pb);
373 codec->channels = get_le16(pb);
374 break;
375 }
376
377 }
378
379 /* get until end of block reached */
380 while ((url_ftell(pb) % ffm->packet_size) != 0)
381 get_byte(pb);
382
383 /* init packet demux */
384 ffm->packet_ptr = ffm->packet;
385 ffm->packet_end = ffm->packet;
386 ffm->frame_offset = 0;
387 ffm->pts = 0;
388 ffm->read_state = READ_HEADER;
389 ffm->first_packet = 1;
390 return 0;
391 fail:
392 for(i=0;i<s->nb_streams;i++) {
393 st = s->streams[i];
394 if (st) {
395 fst = st->priv_data;
396 if (fst)
397 free(fst);
398 free(st);
399 }
400 }
401 if (ffm)
402 free(ffm);
403 return -1;
404}
405
406/* return < 0 if eof */
407static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
408{
409 int size;
410 FFMContext *ffm = s->priv_data;
411
412 switch(ffm->read_state) {
413 case READ_HEADER:
414 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE))
415 return -EAGAIN;
416#if 0
417 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
418 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
419#endif
420 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != FRAME_HEADER_SIZE)
421 return -EAGAIN;
422
423#if 0
424 {
425 int i;
426 for(i=0;i<FRAME_HEADER_SIZE;i++)
427 printf("%02x ", ffm->header[i]);
428 printf("\n");
429 }
430#endif
431 ffm->read_state = READ_DATA;
432 /* fall thru */
433 case READ_DATA:
434 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
435 if (!ffm_is_avail_data(s, size)) {
436 return -EAGAIN;
437 }
438
439 av_new_packet(pkt, size);
440 pkt->stream_index = ffm->header[0];
441 if (ffm->header[1] & FLAG_KEY_FRAME)
442 pkt->flags |= PKT_FLAG_KEY;
443
444 ffm->read_state = READ_HEADER;
445 if (ffm_read_data(s, pkt->data, size, 0) != size) {
446 /* bad case: desynchronized packet. we cancel all the packet loading */
447 av_free_packet(pkt);
448 return -EAGAIN;
449 }
450 break;
451 }
452 return 0;
453}
454
455//#define DEBUG_SEEK
456
457/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
458 by the write position inside this function */
459static void ffm_seek1(AVFormatContext *s, offset_t pos1)
460{
461 FFMContext *ffm = s->priv_data;
462 ByteIOContext *pb = &s->pb;
463 offset_t pos;
464
465 pos = pos1 + ffm->write_index;
466 if (pos >= ffm->file_size)
467 pos -= (ffm->file_size - FFM_PACKET_SIZE);
468#ifdef DEBUG_SEEK
469 printf("seek to %Lx -> %Lx\n", pos1, pos);
470#endif
471 url_fseek(pb, pos, SEEK_SET);
472}
473
474static INT64 get_pts(AVFormatContext *s, offset_t pos)
475{
476 ByteIOContext *pb = &s->pb;
477 INT64 pts;
478
479 ffm_seek1(s, pos);
480 url_fskip(pb, 4);
481 pts = get_be64(pb);
482#ifdef DEBUG_SEEK
483 printf("pts=%0.6f\n", pts / 1000000.0);
484#endif
485 return pts;
486}
487
488/* seek to a given time in the file. The file read pointer is
489 positionned at or before pts. XXX: the following code is quite
490 approximative */
491static int ffm_seek(AVFormatContext *s, INT64 wanted_pts)
492{
493 FFMContext *ffm = s->priv_data;
494 offset_t pos_min, pos_max, pos;
495 INT64 pts_min, pts_max, pts;
496 double pos1;
497
498#ifdef DEBUG_SEEK
499 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
500#endif
501 /* find the position using linear interpolation (better than
502 dichotomy in typical cases) */
503 pos_min = 0;
504 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
505 while (pos_min <= pos_max) {
506 pts_min = get_pts(s, pos_min);
507 pts_max = get_pts(s, pos_max);
508 /* linear interpolation */
509 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
510 (double)(pts_max - pts_min);
511 pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
512 if (pos <= pos_min)
513 pos = pos_min;
514 else if (pos >= pos_max)
515 pos = pos_max;
516 pts = get_pts(s, pos);
517 /* check if we are lucky */
518 if (pts == wanted_pts) {
519 goto found;
520 } else if (pts > wanted_pts) {
521 pos_max = pos - FFM_PACKET_SIZE;
522 } else {
523 pos_min = pos + FFM_PACKET_SIZE;
524 }
525 }
526 pos = pos_min;
527 if (pos > 0)
528 pos -= FFM_PACKET_SIZE;
529 found:
530 ffm_seek1(s, pos);
531 return 0;
532}
533
534offset_t ffm_read_write_index(int fd)
535{
536 UINT8 buf[8];
537 offset_t pos;
538 int i;
539
540 lseek(fd, 8, SEEK_SET);
541 read(fd, buf, 8);
542 pos = 0;
543 for(i=0;i<8;i++)
544 pos |= buf[i] << (56 - i * 8);
545 return pos;
546}
547
548void ffm_write_write_index(int fd, offset_t pos)
549{
550 UINT8 buf[8];
551 int i;
552
553 for(i=0;i<8;i++)
554 buf[i] = (pos >> (56 - i * 8)) & 0xff;
555 lseek(fd, 8, SEEK_SET);
556 write(fd, buf, 8);
557}
558
559void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
560{
561 FFMContext *ffm = s->priv_data;
562 ffm->write_index = pos;
563 ffm->file_size = file_size;
564}
565
566static int ffm_read_close(AVFormatContext *s)
567{
568 AVStream *st;
569 int i;
570
571 for(i=0;i<s->nb_streams;i++) {
572 st = s->streams[i];
573 free(st->priv_data);
574 }
575 free(s->priv_data);
576 return 0;
577}
578
579AVFormat ffm_format = {
580 "ffm",
581 "ffm format",
582 "",
583 "ffm",
584 /* not really used */
585 CODEC_ID_MP2,
586 CODEC_ID_MPEG1VIDEO,
587 ffm_write_header,
588 ffm_write_packet,
589 ffm_write_trailer,
590 ffm_read_header,
591 ffm_read_packet,
592 ffm_read_close,
593 ffm_seek,
594};