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