Mercurial > libavformat.hg
annotate ffm.c @ 181:20cd73ef6ead libavformat
ATI VCR1 decoder
author | michaelni |
---|---|
date | Thu, 24 Jul 2003 12:18:46 +0000 |
parents | 25062c9b1f86 |
children | 2fa5e94ba716 |
rev | line source |
---|---|
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 { | |
65 | 31 int64_t pts; |
0 | 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; | |
65 | 43 uint8_t header[FRAME_HEADER_SIZE]; |
0 | 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; | |
65 | 49 int64_t pts; |
50 uint8_t *packet_ptr, *packet_end; | |
51 uint8_t packet[FFM_PACKET_SIZE]; | |
0 | 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, | |
65 | 85 uint8_t *buf, int size, |
86 int64_t pts, int first) | |
0 | 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); | |
5 | 154 put_be32(pb, st->quality); |
0 | 155 put_be32(pb, codec->flags); |
156 /* specific info */ | |
157 switch(codec->codec_type) { | |
158 case CODEC_TYPE_VIDEO: | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
79
diff
changeset
|
159 put_be32(pb, codec->frame_rate_base); |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
79
diff
changeset
|
160 put_be32(pb, codec->frame_rate); |
0 | 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 put_be32(pb, codec->bit_rate_tolerance); | |
170 put_strz(pb, codec->rc_eq); | |
171 put_be32(pb, codec->rc_max_rate); | |
172 put_be32(pb, codec->rc_min_rate); | |
173 put_be32(pb, codec->rc_buffer_size); | |
174 put_be64_double(pb, codec->i_quant_factor); | |
175 put_be64_double(pb, codec->b_quant_factor); | |
176 put_be64_double(pb, codec->i_quant_offset); | |
177 put_be64_double(pb, codec->b_quant_offset); | |
178 put_be32(pb, codec->dct_algo); | |
179 break; | |
180 case CODEC_TYPE_AUDIO: | |
181 put_be32(pb, codec->sample_rate); | |
182 put_le16(pb, codec->channels); | |
183 put_le16(pb, codec->frame_size); | |
184 break; | |
185 default: | |
186 av_abort(); | |
187 } | |
188 /* hack to have real time */ | |
189 if (ffm_nopts) | |
190 fst->pts = 0; | |
191 else | |
192 fst->pts = av_gettime(); | |
193 } | |
194 | |
195 /* flush until end of block reached */ | |
196 while ((url_ftell(pb) % ffm->packet_size) != 0) | |
197 put_byte(pb, 0); | |
198 | |
199 put_flush_packet(pb); | |
200 | |
201 /* init packet mux */ | |
202 ffm->packet_ptr = ffm->packet; | |
203 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE; | |
204 ffm->frame_offset = 0; | |
205 ffm->pts = 0; | |
206 ffm->first_packet = 1; | |
207 | |
208 return 0; | |
209 fail: | |
210 for(i=0;i<s->nb_streams;i++) { | |
211 st = s->streams[i]; | |
212 av_freep(&st->priv_data); | |
213 } | |
214 return -1; | |
215 } | |
216 | |
217 static int ffm_write_packet(AVFormatContext *s, int stream_index, | |
65 | 218 uint8_t *buf, int size, int force_pts) |
0 | 219 { |
220 AVStream *st = s->streams[stream_index]; | |
221 FFMStream *fst = st->priv_data; | |
65 | 222 int64_t pts; |
223 uint8_t header[FRAME_HEADER_SIZE]; | |
0 | 224 int duration; |
225 | |
226 if (st->codec.codec_type == CODEC_TYPE_AUDIO) { | |
227 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0); | |
228 } else { | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
79
diff
changeset
|
229 duration = (1000000.0 * st->codec.frame_rate_base / (float)st->codec.frame_rate); |
0 | 230 } |
231 | |
232 pts = fst->pts; | |
233 /* packet size & key_frame */ | |
234 header[0] = stream_index; | |
235 header[1] = 0; | |
7 | 236 if (st->codec.coded_frame->key_frame) //if st->codec.coded_frame==NULL then there is a bug somewhere else |
0 | 237 header[1] |= FLAG_KEY_FRAME; |
238 header[2] = (size >> 16) & 0xff; | |
239 header[3] = (size >> 8) & 0xff; | |
240 header[4] = size & 0xff; | |
241 header[5] = (duration >> 16) & 0xff; | |
242 header[6] = (duration >> 8) & 0xff; | |
243 header[7] = duration & 0xff; | |
244 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1); | |
245 ffm_write_data(s, buf, size, pts, 0); | |
246 | |
247 fst->pts += duration; | |
248 return 0; | |
249 } | |
250 | |
251 static int ffm_write_trailer(AVFormatContext *s) | |
252 { | |
253 ByteIOContext *pb = &s->pb; | |
254 FFMContext *ffm = s->priv_data; | |
255 int i; | |
256 | |
257 /* flush packets */ | |
258 if (ffm->packet_ptr > ffm->packet) | |
259 flush_packet(s); | |
260 | |
261 put_flush_packet(pb); | |
262 | |
263 if (!url_is_streamed(pb)) { | |
65 | 264 int64_t size; |
0 | 265 /* update the write offset */ |
266 size = url_ftell(pb); | |
267 url_fseek(pb, 8, SEEK_SET); | |
268 put_be64(pb, size); | |
269 put_flush_packet(pb); | |
270 } | |
271 | |
272 for(i=0;i<s->nb_streams;i++) | |
273 av_freep(&s->streams[i]->priv_data); | |
274 return 0; | |
275 } | |
276 | |
277 /* ffm demux */ | |
278 | |
279 static int ffm_is_avail_data(AVFormatContext *s, int size) | |
280 { | |
281 FFMContext *ffm = s->priv_data; | |
282 offset_t pos, avail_size; | |
283 int len; | |
284 | |
285 len = ffm->packet_end - ffm->packet_ptr; | |
286 if (!ffm_nopts) { | |
287 /* XXX: I don't understand this test, so I disabled it for testing */ | |
288 if (size <= len) | |
289 return 1; | |
290 } | |
291 pos = url_ftell(&s->pb); | |
292 if (pos == ffm->write_index) { | |
293 /* exactly at the end of stream */ | |
294 return 0; | |
295 } else if (pos < ffm->write_index) { | |
296 avail_size = ffm->write_index - pos; | |
297 } else { | |
298 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE); | |
299 } | |
300 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len; | |
301 if (size <= avail_size) | |
302 return 1; | |
303 else | |
304 return 0; | |
305 } | |
306 | |
307 /* first is true if we read the frame header */ | |
308 static int ffm_read_data(AVFormatContext *s, | |
65 | 309 uint8_t *buf, int size, int first) |
0 | 310 { |
311 FFMContext *ffm = s->priv_data; | |
312 ByteIOContext *pb = &s->pb; | |
313 int len, fill_size, size1, frame_offset; | |
314 | |
315 size1 = size; | |
316 while (size > 0) { | |
317 redo: | |
318 len = ffm->packet_end - ffm->packet_ptr; | |
319 if (len > size) | |
320 len = size; | |
321 if (len == 0) { | |
322 if (url_ftell(pb) == ffm->file_size) | |
323 url_fseek(pb, ffm->packet_size, SEEK_SET); | |
324 retry_read: | |
325 get_be16(pb); /* PACKET_ID */ | |
326 fill_size = get_be16(pb); | |
327 ffm->pts = get_be64(pb); | |
328 frame_offset = get_be16(pb); | |
329 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE); | |
330 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size); | |
331 /* if first packet or resynchronization packet, we must | |
332 handle it specifically */ | |
333 if (ffm->first_packet || (frame_offset & 0x8000)) { | |
334 if (!frame_offset) { | |
335 /* This packet has no frame headers in it */ | |
336 if (url_ftell(pb) >= ffm->packet_size * 3) { | |
337 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR); | |
338 goto retry_read; | |
339 } | |
340 /* This is bad, we cannot find a valid frame header */ | |
341 return 0; | |
342 } | |
343 ffm->first_packet = 0; | |
344 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE) | |
345 av_abort(); | |
346 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE; | |
347 if (!first) | |
348 break; | |
349 } else { | |
350 ffm->packet_ptr = ffm->packet; | |
351 } | |
352 goto redo; | |
353 } | |
354 memcpy(buf, ffm->packet_ptr, len); | |
355 buf += len; | |
356 ffm->packet_ptr += len; | |
357 size -= len; | |
358 first = 0; | |
359 } | |
360 return size1 - size; | |
361 } | |
362 | |
363 | |
364 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
365 { | |
366 FFMContext *ffm = s->priv_data; | |
367 AVStream *st; | |
368 FFMStream *fst; | |
369 ByteIOContext *pb = &s->pb; | |
370 AVCodecContext *codec; | |
371 int i; | |
65 | 372 uint32_t tag; |
0 | 373 |
374 /* header */ | |
375 tag = get_le32(pb); | |
376 if (tag != MKTAG('F', 'F', 'M', '1')) | |
377 goto fail; | |
378 ffm->packet_size = get_be32(pb); | |
379 if (ffm->packet_size != FFM_PACKET_SIZE) | |
380 goto fail; | |
381 ffm->write_index = get_be64(pb); | |
382 /* get also filesize */ | |
383 if (!url_is_streamed(pb)) { | |
384 ffm->file_size = url_filesize(url_fileno(pb)); | |
385 } else { | |
65 | 386 ffm->file_size = (uint64_t_C(1) << 63) - 1; |
0 | 387 } |
388 | |
389 s->nb_streams = get_be32(pb); | |
390 get_be32(pb); /* total bitrate */ | |
391 /* read each stream */ | |
392 for(i=0;i<s->nb_streams;i++) { | |
393 char rc_eq_buf[128]; | |
394 | |
395 st = av_mallocz(sizeof(AVStream)); | |
396 if (!st) | |
397 goto fail; | |
5 | 398 avcodec_get_context_defaults(&st->codec); |
0 | 399 s->streams[i] = st; |
400 fst = av_mallocz(sizeof(FFMStream)); | |
401 if (!fst) | |
402 goto fail; | |
403 st->priv_data = fst; | |
404 | |
405 codec = &st->codec; | |
406 /* generic info */ | |
407 st->codec.codec_id = get_be32(pb); | |
408 st->codec.codec_type = get_byte(pb); /* codec_type */ | |
409 codec->bit_rate = get_be32(pb); | |
5 | 410 st->quality = get_be32(pb); |
0 | 411 codec->flags = get_be32(pb); |
412 /* specific info */ | |
413 switch(codec->codec_type) { | |
414 case CODEC_TYPE_VIDEO: | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
79
diff
changeset
|
415 codec->frame_rate_base = get_be32(pb); |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
79
diff
changeset
|
416 codec->frame_rate = get_be32(pb); |
0 | 417 codec->width = get_be16(pb); |
418 codec->height = get_be16(pb); | |
419 codec->gop_size = get_be16(pb); | |
420 codec->qmin = get_byte(pb); | |
421 codec->qmax = get_byte(pb); | |
422 codec->max_qdiff = get_byte(pb); | |
423 codec->qcompress = get_be16(pb) / 10000.0; | |
424 codec->qblur = get_be16(pb) / 10000.0; | |
425 codec->bit_rate_tolerance = get_be32(pb); | |
34 | 426 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf))); |
0 | 427 codec->rc_max_rate = get_be32(pb); |
428 codec->rc_min_rate = get_be32(pb); | |
429 codec->rc_buffer_size = get_be32(pb); | |
430 codec->i_quant_factor = get_be64_double(pb); | |
431 codec->b_quant_factor = get_be64_double(pb); | |
432 codec->i_quant_offset = get_be64_double(pb); | |
433 codec->b_quant_offset = get_be64_double(pb); | |
434 codec->dct_algo = get_be32(pb); | |
435 break; | |
436 case CODEC_TYPE_AUDIO: | |
437 codec->sample_rate = get_be32(pb); | |
438 codec->channels = get_le16(pb); | |
439 codec->frame_size = get_le16(pb); | |
440 break; | |
441 default: | |
442 goto fail; | |
443 } | |
444 | |
445 } | |
446 | |
447 /* get until end of block reached */ | |
448 while ((url_ftell(pb) % ffm->packet_size) != 0) | |
449 get_byte(pb); | |
450 | |
451 /* init packet demux */ | |
452 ffm->packet_ptr = ffm->packet; | |
453 ffm->packet_end = ffm->packet; | |
454 ffm->frame_offset = 0; | |
455 ffm->pts = 0; | |
456 ffm->read_state = READ_HEADER; | |
457 ffm->first_packet = 1; | |
458 return 0; | |
459 fail: | |
460 for(i=0;i<s->nb_streams;i++) { | |
461 st = s->streams[i]; | |
462 if (st) { | |
463 av_freep(&st->priv_data); | |
464 av_free(st); | |
465 } | |
466 } | |
467 return -1; | |
468 } | |
469 | |
470 /* return < 0 if eof */ | |
471 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt) | |
472 { | |
473 int size; | |
474 FFMContext *ffm = s->priv_data; | |
475 int duration; | |
476 | |
477 switch(ffm->read_state) { | |
478 case READ_HEADER: | |
479 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) { | |
480 return -EAGAIN; | |
481 } | |
482 #if 0 | |
483 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n", | |
484 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size); | |
485 #endif | |
486 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != | |
487 FRAME_HEADER_SIZE) | |
488 return -EAGAIN; | |
489 #if 0 | |
490 { | |
491 int i; | |
492 for(i=0;i<FRAME_HEADER_SIZE;i++) | |
493 printf("%02x ", ffm->header[i]); | |
494 printf("\n"); | |
495 } | |
496 #endif | |
497 ffm->read_state = READ_DATA; | |
498 /* fall thru */ | |
499 case READ_DATA: | |
500 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4]; | |
501 if (!ffm_is_avail_data(s, size)) { | |
502 return -EAGAIN; | |
503 } | |
504 | |
505 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7]; | |
506 | |
507 av_new_packet(pkt, size); | |
508 pkt->stream_index = ffm->header[0]; | |
509 if (ffm->header[1] & FLAG_KEY_FRAME) | |
510 pkt->flags |= PKT_FLAG_KEY; | |
511 | |
512 ffm->read_state = READ_HEADER; | |
513 if (ffm_read_data(s, pkt->data, size, 0) != size) { | |
514 /* bad case: desynchronized packet. we cancel all the packet loading */ | |
515 av_free_packet(pkt); | |
516 return -EAGAIN; | |
517 } | |
518 pkt->pts = ffm->pts; | |
519 pkt->duration = duration; | |
520 break; | |
521 } | |
522 return 0; | |
523 } | |
524 | |
525 //#define DEBUG_SEEK | |
526 | |
527 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated | |
528 by the write position inside this function */ | |
529 static void ffm_seek1(AVFormatContext *s, offset_t pos1) | |
530 { | |
531 FFMContext *ffm = s->priv_data; | |
532 ByteIOContext *pb = &s->pb; | |
533 offset_t pos; | |
534 | |
535 pos = pos1 + ffm->write_index; | |
536 if (pos >= ffm->file_size) | |
537 pos -= (ffm->file_size - FFM_PACKET_SIZE); | |
538 #ifdef DEBUG_SEEK | |
539 printf("seek to %Lx -> %Lx\n", pos1, pos); | |
540 #endif | |
541 url_fseek(pb, pos, SEEK_SET); | |
542 } | |
543 | |
65 | 544 static int64_t get_pts(AVFormatContext *s, offset_t pos) |
0 | 545 { |
546 ByteIOContext *pb = &s->pb; | |
65 | 547 int64_t pts; |
0 | 548 |
549 ffm_seek1(s, pos); | |
550 url_fskip(pb, 4); | |
551 pts = get_be64(pb); | |
552 #ifdef DEBUG_SEEK | |
553 printf("pts=%0.6f\n", pts / 1000000.0); | |
554 #endif | |
555 return pts; | |
556 } | |
557 | |
558 /* seek to a given time in the file. The file read pointer is | |
559 positionned at or before pts. XXX: the following code is quite | |
560 approximative */ | |
65 | 561 static int ffm_seek(AVFormatContext *s, int64_t wanted_pts) |
0 | 562 { |
563 FFMContext *ffm = s->priv_data; | |
564 offset_t pos_min, pos_max, pos; | |
65 | 565 int64_t pts_min, pts_max, pts; |
0 | 566 double pos1; |
567 | |
568 #ifdef DEBUG_SEEK | |
569 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0); | |
570 #endif | |
571 /* find the position using linear interpolation (better than | |
572 dichotomy in typical cases) */ | |
573 pos_min = 0; | |
574 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE; | |
575 while (pos_min <= pos_max) { | |
576 pts_min = get_pts(s, pos_min); | |
577 pts_max = get_pts(s, pos_max); | |
578 /* linear interpolation */ | |
579 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) / | |
580 (double)(pts_max - pts_min); | |
65 | 581 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE; |
0 | 582 if (pos <= pos_min) |
583 pos = pos_min; | |
584 else if (pos >= pos_max) | |
585 pos = pos_max; | |
586 pts = get_pts(s, pos); | |
587 /* check if we are lucky */ | |
588 if (pts == wanted_pts) { | |
589 goto found; | |
590 } else if (pts > wanted_pts) { | |
591 pos_max = pos - FFM_PACKET_SIZE; | |
592 } else { | |
593 pos_min = pos + FFM_PACKET_SIZE; | |
594 } | |
595 } | |
596 pos = pos_min; | |
597 if (pos > 0) | |
598 pos -= FFM_PACKET_SIZE; | |
599 found: | |
600 ffm_seek1(s, pos); | |
601 return 0; | |
602 } | |
603 | |
604 offset_t ffm_read_write_index(int fd) | |
605 { | |
65 | 606 uint8_t buf[8]; |
0 | 607 offset_t pos; |
608 int i; | |
609 | |
610 lseek(fd, 8, SEEK_SET); | |
611 read(fd, buf, 8); | |
612 pos = 0; | |
613 for(i=0;i<8;i++) | |
614 pos |= buf[i] << (56 - i * 8); | |
615 return pos; | |
616 } | |
617 | |
618 void ffm_write_write_index(int fd, offset_t pos) | |
619 { | |
65 | 620 uint8_t buf[8]; |
0 | 621 int i; |
622 | |
623 for(i=0;i<8;i++) | |
624 buf[i] = (pos >> (56 - i * 8)) & 0xff; | |
625 lseek(fd, 8, SEEK_SET); | |
626 write(fd, buf, 8); | |
627 } | |
628 | |
629 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size) | |
630 { | |
631 FFMContext *ffm = s->priv_data; | |
632 ffm->write_index = pos; | |
633 ffm->file_size = file_size; | |
634 } | |
635 | |
636 static int ffm_read_close(AVFormatContext *s) | |
637 { | |
638 AVStream *st; | |
639 int i; | |
640 | |
641 for(i=0;i<s->nb_streams;i++) { | |
642 st = s->streams[i]; | |
643 av_freep(&st->priv_data); | |
644 } | |
645 return 0; | |
646 } | |
647 | |
648 static int ffm_probe(AVProbeData *p) | |
649 { | |
650 if (p->buf_size >= 4 && | |
651 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' && | |
652 p->buf[3] == '1') | |
653 return AVPROBE_SCORE_MAX + 1; | |
654 return 0; | |
655 } | |
656 | |
657 static AVInputFormat ffm_iformat = { | |
658 "ffm", | |
659 "ffm format", | |
660 sizeof(FFMContext), | |
661 ffm_probe, | |
662 ffm_read_header, | |
663 ffm_read_packet, | |
664 ffm_read_close, | |
665 ffm_seek, | |
666 }; | |
667 | |
668 static AVOutputFormat ffm_oformat = { | |
669 "ffm", | |
670 "ffm format", | |
671 "", | |
672 "ffm", | |
673 sizeof(FFMContext), | |
674 /* not really used */ | |
675 CODEC_ID_MP2, | |
676 CODEC_ID_MPEG1VIDEO, | |
677 ffm_write_header, | |
678 ffm_write_packet, | |
679 ffm_write_trailer, | |
680 }; | |
681 | |
682 int ffm_init(void) | |
683 { | |
684 av_register_input_format(&ffm_iformat); | |
685 av_register_output_format(&ffm_oformat); | |
686 return 0; | |
687 } |