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