100l (forgoten seeking functions)
[libav.git] / libavformat / ffm.c
CommitLineData
de6d9b64
FB
1/*
2 * FFM (ffserver live feed) encoder and decoder
66d2ff2a 3 * Copyright (c) 2001 Fabrice Bellard.
de6d9b64 4 *
66d2ff2a
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
66d2ff2a 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
66d2ff2a
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
66d2ff2a
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 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 */
20f01548 27#define FRAME_HEADER_SIZE 8
de6d9b64
FB
28#define FLAG_KEY_FRAME 0x01
29
30typedef struct FFMStream {
0c1a9eda 31 int64_t pts;
de6d9b64
FB
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;
0c1a9eda 43 uint8_t header[FRAME_HEADER_SIZE];
de6d9b64
FB
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;
0c1a9eda
ZK
49 int64_t pts;
50 uint8_t *packet_ptr, *packet_end;
51 uint8_t packet[FFM_PACKET_SIZE];
de6d9b64
FB
52} FFMContext;
53
91628427
PG
54static int64_t get_pts(AVFormatContext *s, offset_t pos);
55
c9a65ca8
FB
56/* disable pts hack for testing */
57int ffm_nopts = 0;
58
764ef400 59#ifdef CONFIG_ENCODERS
de6d9b64
FB
60static void flush_packet(AVFormatContext *s)
61{
62 FFMContext *ffm = s->priv_data;
63 int fill_size, h;
64 ByteIOContext *pb = &s->pb;
65
66 fill_size = ffm->packet_end - ffm->packet_ptr;
67 memset(ffm->packet_ptr, 0, fill_size);
4606ac8d 68
91628427
PG
69 if (url_ftell(pb) % ffm->packet_size)
70 av_abort();
71
de6d9b64 72 /* put header */
4606ac8d 73 put_be16(pb, PACKET_ID);
de6d9b64
FB
74 put_be16(pb, fill_size);
75 put_be64(pb, ffm->pts);
76 h = ffm->frame_offset;
77 if (ffm->first_packet)
78 h |= 0x8000;
79 put_be16(pb, h);
80 put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
81
82 /* prepare next packet */
83 ffm->frame_offset = 0; /* no key frame */
84 ffm->pts = 0; /* no pts */
85 ffm->packet_ptr = ffm->packet;
86 ffm->first_packet = 0;
87}
88
89/* 'first' is true if first data of a frame */
4606ac8d 90static void ffm_write_data(AVFormatContext *s,
49057904 91 const uint8_t *buf, int size,
0c1a9eda 92 int64_t pts, int first)
de6d9b64
FB
93{
94 FFMContext *ffm = s->priv_data;
95 int len;
96
97 if (first && ffm->frame_offset == 0)
98 ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
99 if (first && ffm->pts == 0)
100 ffm->pts = pts;
101
102 /* write as many packets as needed */
103 while (size > 0) {
104 len = ffm->packet_end - ffm->packet_ptr;
105 if (len > size)
106 len = size;
107 memcpy(ffm->packet_ptr, buf, len);
4606ac8d 108
de6d9b64
FB
109 ffm->packet_ptr += len;
110 buf += len;
111 size -= len;
112 if (ffm->packet_ptr >= ffm->packet_end) {
113 /* special case : no pts in packet : we leave the current one */
114 if (ffm->pts == 0)
115 ffm->pts = pts;
116
117 flush_packet(s);
118 }
119 }
120}
121
122static int ffm_write_header(AVFormatContext *s)
123{
c9a65ca8 124 FFMContext *ffm = s->priv_data;
de6d9b64
FB
125 AVStream *st;
126 FFMStream *fst;
de6d9b64
FB
127 ByteIOContext *pb = &s->pb;
128 AVCodecContext *codec;
129 int bit_rate, i;
130
de6d9b64 131 ffm->packet_size = FFM_PACKET_SIZE;
4606ac8d 132
de6d9b64
FB
133 /* header */
134 put_tag(pb, "FFM1");
135 put_be32(pb, ffm->packet_size);
136 /* XXX: store write position in other file ? */
137 put_be64(pb, ffm->packet_size); /* current write position */
138
139 put_be32(pb, s->nb_streams);
140 bit_rate = 0;
141 for(i=0;i<s->nb_streams;i++) {
142 st = s->streams[i];
143 bit_rate += st->codec.bit_rate;
144 }
145 put_be32(pb, bit_rate);
146
147 /* list of streams */
148 for(i=0;i<s->nb_streams;i++) {
149 st = s->streams[i];
16300e23 150 fst = av_mallocz(sizeof(FFMStream));
de6d9b64
FB
151 if (!fst)
152 goto fail;
53e2f9ca 153 av_set_pts_info(st, 64, 1, 1000000);
de6d9b64
FB
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);
1e491e29 161 put_be32(pb, st->quality);
ccac2e27 162 put_be32(pb, codec->flags);
de6d9b64
FB
163 /* specific info */
164 switch(codec->codec_type) {
165 case CODEC_TYPE_VIDEO:
14bea432
MN
166 put_be32(pb, codec->frame_rate_base);
167 put_be32(pb, codec->frame_rate);
de6d9b64
FB
168 put_be16(pb, codec->width);
169 put_be16(pb, codec->height);
20f01548
PG
170 put_be16(pb, codec->gop_size);
171 put_byte(pb, codec->qmin);
172 put_byte(pb, codec->qmax);
173 put_byte(pb, codec->max_qdiff);
174 put_be16(pb, (int) (codec->qcompress * 10000.0));
175 put_be16(pb, (int) (codec->qblur * 10000.0));
3884a3c3 176 put_be32(pb, codec->bit_rate_tolerance);
3b4b29dc 177 put_strz(pb, codec->rc_eq);
75bdb984
PG
178 put_be32(pb, codec->rc_max_rate);
179 put_be32(pb, codec->rc_min_rate);
180 put_be32(pb, codec->rc_buffer_size);
3b4b29dc
FB
181 put_be64_double(pb, codec->i_quant_factor);
182 put_be64_double(pb, codec->b_quant_factor);
183 put_be64_double(pb, codec->i_quant_offset);
184 put_be64_double(pb, codec->b_quant_offset);
75bdb984 185 put_be32(pb, codec->dct_algo);
de6d9b64
FB
186 break;
187 case CODEC_TYPE_AUDIO:
188 put_be32(pb, codec->sample_rate);
189 put_le16(pb, codec->channels);
20f01548 190 put_le16(pb, codec->frame_size);
de6d9b64 191 break;
20f01548 192 default:
71c32f19 193 return -1;
de6d9b64
FB
194 }
195 /* hack to have real time */
c9a65ca8
FB
196 if (ffm_nopts)
197 fst->pts = 0;
198 else
a11bf0bd 199 fst->pts = av_gettime();
de6d9b64
FB
200 }
201
202 /* flush until end of block reached */
203 while ((url_ftell(pb) % ffm->packet_size) != 0)
204 put_byte(pb, 0);
205
206 put_flush_packet(pb);
207
208 /* init packet mux */
209 ffm->packet_ptr = ffm->packet;
210 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
71c32f19 211 assert(ffm->packet_end >= ffm->packet);
de6d9b64
FB
212 ffm->frame_offset = 0;
213 ffm->pts = 0;
214 ffm->first_packet = 1;
215
216 return 0;
217 fail:
218 for(i=0;i<s->nb_streams;i++) {
219 st = s->streams[i];
789bee12 220 av_freep(&st->priv_data);
de6d9b64 221 }
de6d9b64
FB
222 return -1;
223}
224
e928649b 225static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64 226{
e928649b 227 AVStream *st = s->streams[pkt->stream_index];
de6d9b64 228 FFMStream *fst = st->priv_data;
0c1a9eda
ZK
229 int64_t pts;
230 uint8_t header[FRAME_HEADER_SIZE];
20f01548 231 int duration;
e928649b 232 int size= pkt->size;
20f01548 233
e928649b 234 //XXX/FIXME use duration from pkt
20f01548
PG
235 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
236 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
237 } else {
14bea432 238 duration = (1000000.0 * st->codec.frame_rate_base / (float)st->codec.frame_rate);
20f01548 239 }
de6d9b64
FB
240
241 pts = fst->pts;
242 /* packet size & key_frame */
e928649b 243 header[0] = pkt->stream_index;
de6d9b64 244 header[1] = 0;
e928649b 245 if (pkt->flags & PKT_FLAG_KEY)
de6d9b64
FB
246 header[1] |= FLAG_KEY_FRAME;
247 header[2] = (size >> 16) & 0xff;
248 header[3] = (size >> 8) & 0xff;
249 header[4] = size & 0xff;
20f01548
PG
250 header[5] = (duration >> 16) & 0xff;
251 header[6] = (duration >> 8) & 0xff;
252 header[7] = duration & 0xff;
de6d9b64 253 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
e928649b 254 ffm_write_data(s, pkt->data, size, pts, 0);
de6d9b64 255
20f01548 256 fst->pts += duration;
de6d9b64
FB
257 return 0;
258}
259
260static int ffm_write_trailer(AVFormatContext *s)
261{
262 ByteIOContext *pb = &s->pb;
263 FFMContext *ffm = s->priv_data;
264 int i;
265
266 /* flush packets */
267 if (ffm->packet_ptr > ffm->packet)
268 flush_packet(s);
269
270 put_flush_packet(pb);
271
66d2ff2a 272 if (!url_is_streamed(pb)) {
0c1a9eda 273 int64_t size;
66d2ff2a
FB
274 /* update the write offset */
275 size = url_ftell(pb);
276 url_fseek(pb, 8, SEEK_SET);
277 put_be64(pb, size);
278 put_flush_packet(pb);
279 }
280
de6d9b64
FB
281 return 0;
282}
764ef400 283#endif //CONFIG_ENCODERS
de6d9b64
FB
284
285/* ffm demux */
286
287static int ffm_is_avail_data(AVFormatContext *s, int size)
288{
289 FFMContext *ffm = s->priv_data;
290 offset_t pos, avail_size;
291 int len;
292
293 len = ffm->packet_end - ffm->packet_ptr;
66d2ff2a
FB
294 if (!ffm_nopts) {
295 /* XXX: I don't understand this test, so I disabled it for testing */
296 if (size <= len)
297 return 1;
298 }
de6d9b64
FB
299 pos = url_ftell(&s->pb);
300 if (pos == ffm->write_index) {
301 /* exactly at the end of stream */
302 return 0;
303 } else if (pos < ffm->write_index) {
304 avail_size = ffm->write_index - pos;
305 } else {
306 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
307 }
308 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
309 if (size <= avail_size)
310 return 1;
311 else
312 return 0;
313}
314
315/* first is true if we read the frame header */
4606ac8d 316static int ffm_read_data(AVFormatContext *s,
0c1a9eda 317 uint8_t *buf, int size, int first)
de6d9b64
FB
318{
319 FFMContext *ffm = s->priv_data;
320 ByteIOContext *pb = &s->pb;
321 int len, fill_size, size1, frame_offset;
322
323 size1 = size;
324 while (size > 0) {
325 redo:
326 len = ffm->packet_end - ffm->packet_ptr;
327 if (len > size)
328 len = size;
329 if (len == 0) {
330 if (url_ftell(pb) == ffm->file_size)
331 url_fseek(pb, ffm->packet_size, SEEK_SET);
5e57424d 332 retry_read:
de6d9b64
FB
333 get_be16(pb); /* PACKET_ID */
334 fill_size = get_be16(pb);
335 ffm->pts = get_be64(pb);
336 frame_offset = get_be16(pb);
337 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
338 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
91628427 339 if (ffm->packet_end < ffm->packet)
71c32f19 340 return -1;
de6d9b64
FB
341 /* if first packet or resynchronization packet, we must
342 handle it specifically */
343 if (ffm->first_packet || (frame_offset & 0x8000)) {
5e57424d
PG
344 if (!frame_offset) {
345 /* This packet has no frame headers in it */
346 if (url_ftell(pb) >= ffm->packet_size * 3) {
347 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
348 goto retry_read;
349 }
350 /* This is bad, we cannot find a valid frame header */
351 return 0;
352 }
de6d9b64 353 ffm->first_packet = 0;
5e57424d 354 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
71c32f19 355 return -1;
de6d9b64
FB
356 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
357 if (!first)
358 break;
359 } else {
360 ffm->packet_ptr = ffm->packet;
361 }
362 goto redo;
363 }
364 memcpy(buf, ffm->packet_ptr, len);
365 buf += len;
366 ffm->packet_ptr += len;
367 size -= len;
368 first = 0;
369 }
370 return size1 - size;
371}
372
373
91628427
PG
374static void adjust_write_index(AVFormatContext *s)
375{
376 FFMContext *ffm = s->priv_data;
377 ByteIOContext *pb = &s->pb;
378 int64_t pts;
379 //offset_t orig_write_index = ffm->write_index;
380 offset_t pos_min, pos_max;
381 int64_t pts_start;
382 offset_t ptr = url_ftell(pb);
383
384
385 pos_min = 0;
386 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
387
388 pts_start = get_pts(s, pos_min);
389
390 pts = get_pts(s, pos_max);
391
392 if (pts - 100000 > pts_start)
79292510 393 goto end;
91628427
PG
394
395 ffm->write_index = FFM_PACKET_SIZE;
396
397 pts_start = get_pts(s, pos_min);
398
399 pts = get_pts(s, pos_max);
400
401 if (pts - 100000 <= pts_start) {
402 while (1) {
403 offset_t newpos;
404 int64_t newpts;
405
406 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
407
408 if (newpos == pos_min)
409 break;
410
411 newpts = get_pts(s, newpos);
412
413 if (newpts - 100000 <= pts) {
414 pos_max = newpos;
415 pts = newpts;
416 } else {
417 pos_min = newpos;
418 }
419 }
420 ffm->write_index += pos_max;
421 }
422
423 //printf("Adjusted write index from %lld to %lld: pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
424 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
425
79292510 426 end:
91628427
PG
427 url_fseek(pb, ptr, SEEK_SET);
428}
429
430
de6d9b64
FB
431static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
432{
c9a65ca8 433 FFMContext *ffm = s->priv_data;
de6d9b64
FB
434 AVStream *st;
435 FFMStream *fst;
de6d9b64
FB
436 ByteIOContext *pb = &s->pb;
437 AVCodecContext *codec;
fa26a29d 438 int i, nb_streams;
0c1a9eda 439 uint32_t tag;
de6d9b64 440
de6d9b64
FB
441 /* header */
442 tag = get_le32(pb);
443 if (tag != MKTAG('F', 'F', 'M', '1'))
444 goto fail;
445 ffm->packet_size = get_be32(pb);
446 if (ffm->packet_size != FFM_PACKET_SIZE)
447 goto fail;
448 ffm->write_index = get_be64(pb);
449 /* get also filesize */
450 if (!url_is_streamed(pb)) {
451 ffm->file_size = url_filesize(url_fileno(pb));
91628427 452 adjust_write_index(s);
de6d9b64 453 } else {
0c1a9eda 454 ffm->file_size = (uint64_t_C(1) << 63) - 1;
de6d9b64
FB
455 }
456
fa26a29d 457 nb_streams = get_be32(pb);
de6d9b64
FB
458 get_be32(pb); /* total bitrate */
459 /* read each stream */
fa26a29d 460 for(i=0;i<nb_streams;i++) {
75bdb984
PG
461 char rc_eq_buf[128];
462
fa26a29d 463 st = av_new_stream(s, 0);
de6d9b64
FB
464 if (!st)
465 goto fail;
16300e23 466 fst = av_mallocz(sizeof(FFMStream));
de6d9b64
FB
467 if (!fst)
468 goto fail;
e928649b
MN
469
470 av_set_pts_info(st, 64, 1, 1000000);
471
de6d9b64
FB
472 st->priv_data = fst;
473
474 codec = &st->codec;
475 /* generic info */
476 st->codec.codec_id = get_be32(pb);
477 st->codec.codec_type = get_byte(pb); /* codec_type */
478 codec->bit_rate = get_be32(pb);
1e491e29 479 st->quality = get_be32(pb);
ccac2e27 480 codec->flags = get_be32(pb);
de6d9b64
FB
481 /* specific info */
482 switch(codec->codec_type) {
483 case CODEC_TYPE_VIDEO:
14bea432
MN
484 codec->frame_rate_base = get_be32(pb);
485 codec->frame_rate = get_be32(pb);
de6d9b64
FB
486 codec->width = get_be16(pb);
487 codec->height = get_be16(pb);
20f01548
PG
488 codec->gop_size = get_be16(pb);
489 codec->qmin = get_byte(pb);
490 codec->qmax = get_byte(pb);
491 codec->max_qdiff = get_byte(pb);
492 codec->qcompress = get_be16(pb) / 10000.0;
493 codec->qblur = get_be16(pb) / 10000.0;
3884a3c3 494 codec->bit_rate_tolerance = get_be32(pb);
e9a9e0c2 495 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
75bdb984
PG
496 codec->rc_max_rate = get_be32(pb);
497 codec->rc_min_rate = get_be32(pb);
498 codec->rc_buffer_size = get_be32(pb);
3b4b29dc
FB
499 codec->i_quant_factor = get_be64_double(pb);
500 codec->b_quant_factor = get_be64_double(pb);
501 codec->i_quant_offset = get_be64_double(pb);
502 codec->b_quant_offset = get_be64_double(pb);
75bdb984 503 codec->dct_algo = get_be32(pb);
de6d9b64
FB
504 break;
505 case CODEC_TYPE_AUDIO:
506 codec->sample_rate = get_be32(pb);
507 codec->channels = get_le16(pb);
20f01548 508 codec->frame_size = get_le16(pb);
de6d9b64 509 break;
20f01548 510 default:
75bdb984 511 goto fail;
de6d9b64
FB
512 }
513
514 }
515
516 /* get until end of block reached */
517 while ((url_ftell(pb) % ffm->packet_size) != 0)
518 get_byte(pb);
519
520 /* init packet demux */
521 ffm->packet_ptr = ffm->packet;
522 ffm->packet_end = ffm->packet;
523 ffm->frame_offset = 0;
524 ffm->pts = 0;
525 ffm->read_state = READ_HEADER;
526 ffm->first_packet = 1;
527 return 0;
528 fail:
529 for(i=0;i<s->nb_streams;i++) {
530 st = s->streams[i];
531 if (st) {
789bee12 532 av_freep(&st->priv_data);
1ea4f593 533 av_free(st);
de6d9b64
FB
534 }
535 }
de6d9b64
FB
536 return -1;
537}
538
539/* return < 0 if eof */
540static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
541{
542 int size;
543 FFMContext *ffm = s->priv_data;
20f01548 544 int duration;
de6d9b64
FB
545
546 switch(ffm->read_state) {
547 case READ_HEADER:
66d2ff2a 548 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
de6d9b64 549 return -EAGAIN;
66d2ff2a 550 }
de6d9b64 551#if 0
4606ac8d 552 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
de6d9b64
FB
553 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
554#endif
66d2ff2a
FB
555 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
556 FRAME_HEADER_SIZE)
de6d9b64 557 return -EAGAIN;
de6d9b64
FB
558#if 0
559 {
560 int i;
561 for(i=0;i<FRAME_HEADER_SIZE;i++)
562 printf("%02x ", ffm->header[i]);
563 printf("\n");
564 }
565#endif
566 ffm->read_state = READ_DATA;
567 /* fall thru */
568 case READ_DATA:
569 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
570 if (!ffm_is_avail_data(s, size)) {
571 return -EAGAIN;
572 }
573
20f01548
PG
574 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
575
de6d9b64
FB
576 av_new_packet(pkt, size);
577 pkt->stream_index = ffm->header[0];
578 if (ffm->header[1] & FLAG_KEY_FRAME)
579 pkt->flags |= PKT_FLAG_KEY;
4606ac8d 580
de6d9b64
FB
581 ffm->read_state = READ_HEADER;
582 if (ffm_read_data(s, pkt->data, size, 0) != size) {
583 /* bad case: desynchronized packet. we cancel all the packet loading */
584 av_free_packet(pkt);
585 return -EAGAIN;
586 }
20f01548
PG
587 pkt->pts = ffm->pts;
588 pkt->duration = duration;
de6d9b64
FB
589 break;
590 }
591 return 0;
592}
593
594//#define DEBUG_SEEK
595
596/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
597 by the write position inside this function */
598static void ffm_seek1(AVFormatContext *s, offset_t pos1)
599{
600 FFMContext *ffm = s->priv_data;
601 ByteIOContext *pb = &s->pb;
602 offset_t pos;
603
604 pos = pos1 + ffm->write_index;
605 if (pos >= ffm->file_size)
606 pos -= (ffm->file_size - FFM_PACKET_SIZE);
607#ifdef DEBUG_SEEK
608 printf("seek to %Lx -> %Lx\n", pos1, pos);
609#endif
610 url_fseek(pb, pos, SEEK_SET);
611}
612
0c1a9eda 613static int64_t get_pts(AVFormatContext *s, offset_t pos)
de6d9b64
FB
614{
615 ByteIOContext *pb = &s->pb;
0c1a9eda 616 int64_t pts;
de6d9b64 617
4606ac8d 618 ffm_seek1(s, pos);
de6d9b64
FB
619 url_fskip(pb, 4);
620 pts = get_be64(pb);
621#ifdef DEBUG_SEEK
622 printf("pts=%0.6f\n", pts / 1000000.0);
623#endif
624 return pts;
625}
626
627/* seek to a given time in the file. The file read pointer is
628 positionned at or before pts. XXX: the following code is quite
629 approximative */
7b3c1382 630static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
de6d9b64
FB
631{
632 FFMContext *ffm = s->priv_data;
633 offset_t pos_min, pos_max, pos;
0c1a9eda 634 int64_t pts_min, pts_max, pts;
de6d9b64
FB
635 double pos1;
636
637#ifdef DEBUG_SEEK
638 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
639#endif
640 /* find the position using linear interpolation (better than
641 dichotomy in typical cases) */
642 pos_min = 0;
643 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
644 while (pos_min <= pos_max) {
645 pts_min = get_pts(s, pos_min);
646 pts_max = get_pts(s, pos_max);
647 /* linear interpolation */
4606ac8d 648 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
de6d9b64 649 (double)(pts_max - pts_min);
0c1a9eda 650 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
de6d9b64
FB
651 if (pos <= pos_min)
652 pos = pos_min;
653 else if (pos >= pos_max)
654 pos = pos_max;
655 pts = get_pts(s, pos);
656 /* check if we are lucky */
657 if (pts == wanted_pts) {
658 goto found;
659 } else if (pts > wanted_pts) {
660 pos_max = pos - FFM_PACKET_SIZE;
661 } else {
662 pos_min = pos + FFM_PACKET_SIZE;
663 }
664 }
7b3c1382 665 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
de6d9b64
FB
666 if (pos > 0)
667 pos -= FFM_PACKET_SIZE;
668 found:
669 ffm_seek1(s, pos);
670 return 0;
671}
672
673offset_t ffm_read_write_index(int fd)
674{
0c1a9eda 675 uint8_t buf[8];
de6d9b64
FB
676 offset_t pos;
677 int i;
678
679 lseek(fd, 8, SEEK_SET);
680 read(fd, buf, 8);
681 pos = 0;
4606ac8d 682 for(i=0;i<8;i++)
fa26a29d 683 pos |= (int64_t)buf[i] << (56 - i * 8);
de6d9b64
FB
684 return pos;
685}
686
687void ffm_write_write_index(int fd, offset_t pos)
688{
0c1a9eda 689 uint8_t buf[8];
de6d9b64
FB
690 int i;
691
4606ac8d 692 for(i=0;i<8;i++)
de6d9b64
FB
693 buf[i] = (pos >> (56 - i * 8)) & 0xff;
694 lseek(fd, 8, SEEK_SET);
695 write(fd, buf, 8);
696}
697
698void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
699{
700 FFMContext *ffm = s->priv_data;
701 ffm->write_index = pos;
702 ffm->file_size = file_size;
703}
704
705static int ffm_read_close(AVFormatContext *s)
706{
707 AVStream *st;
708 int i;
709
710 for(i=0;i<s->nb_streams;i++) {
711 st = s->streams[i];
789bee12 712 av_freep(&st->priv_data);
de6d9b64 713 }
de6d9b64
FB
714 return 0;
715}
716
07c4ed85
PG
717static int ffm_probe(AVProbeData *p)
718{
66d2ff2a
FB
719 if (p->buf_size >= 4 &&
720 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
721 p->buf[3] == '1')
07c4ed85 722 return AVPROBE_SCORE_MAX + 1;
07c4ed85
PG
723 return 0;
724}
725
c18a2692 726static AVInputFormat ffm_iformat = {
c9a65ca8
FB
727 "ffm",
728 "ffm format",
66d2ff2a 729 sizeof(FFMContext),
07c4ed85 730 ffm_probe,
c9a65ca8
FB
731 ffm_read_header,
732 ffm_read_packet,
733 ffm_read_close,
734 ffm_seek,
c9a65ca8
FB
735};
736
764ef400 737#ifdef CONFIG_ENCODERS
c18a2692 738static AVOutputFormat ffm_oformat = {
de6d9b64
FB
739 "ffm",
740 "ffm format",
741 "",
742 "ffm",
66d2ff2a 743 sizeof(FFMContext),
de6d9b64
FB
744 /* not really used */
745 CODEC_ID_MP2,
746 CODEC_ID_MPEG1VIDEO,
747 ffm_write_header,
748 ffm_write_packet,
749 ffm_write_trailer,
de6d9b64 750};
764ef400 751#endif //CONFIG_ENCODERS
c9a65ca8
FB
752
753int ffm_init(void)
754{
755 av_register_input_format(&ffm_iformat);
764ef400 756#ifdef CONFIG_ENCODERS
c9a65ca8 757 av_register_output_format(&ffm_oformat);
764ef400 758#endif //CONFIG_ENCODERS
c9a65ca8
FB
759 return 0;
760}