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