comparison ffmdec.c @ 3348:4d492fccf79b libavformat

split ffm de/muxer
author bcoudurier
date Mon, 26 May 2008 03:44:31 +0000
parents ffm.c@a6449316fad1
children e092bf9b744f
comparison
equal deleted inserted replaced
3347:a6449316fad1 3348:4d492fccf79b
1 /*
2 * FFM (ffserver live feed) demuxer
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "avformat.h"
23 #include "ffm.h"
24 #include <unistd.h>
25
26 static int64_t get_pts(AVFormatContext *s, offset_t pos);
27
28 static int ffm_is_avail_data(AVFormatContext *s, int size)
29 {
30 FFMContext *ffm = s->priv_data;
31 offset_t pos, avail_size;
32 int len;
33
34 len = ffm->packet_end - ffm->packet_ptr;
35 if (size <= len)
36 return 1;
37 pos = url_ftell(s->pb);
38 if (pos == ffm->write_index) {
39 /* exactly at the end of stream */
40 return 0;
41 } else if (pos < ffm->write_index) {
42 avail_size = ffm->write_index - pos;
43 } else {
44 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
45 }
46 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
47 if (size <= avail_size)
48 return 1;
49 else
50 return 0;
51 }
52
53 /* first is true if we read the frame header */
54 static int ffm_read_data(AVFormatContext *s,
55 uint8_t *buf, int size, int first)
56 {
57 FFMContext *ffm = s->priv_data;
58 ByteIOContext *pb = s->pb;
59 int len, fill_size, size1, frame_offset;
60
61 size1 = size;
62 while (size > 0) {
63 redo:
64 len = ffm->packet_end - ffm->packet_ptr;
65 if (len > size)
66 len = size;
67 if (len == 0) {
68 if (url_ftell(pb) == ffm->file_size)
69 url_fseek(pb, ffm->packet_size, SEEK_SET);
70 retry_read:
71 get_be16(pb); /* PACKET_ID */
72 fill_size = get_be16(pb);
73 ffm->pts = get_be64(pb);
74 ffm->first_frame_in_packet = 1;
75 frame_offset = get_be16(pb);
76 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
77 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
78 if (ffm->packet_end < ffm->packet)
79 return -1;
80 /* if first packet or resynchronization packet, we must
81 handle it specifically */
82 if (ffm->first_packet || (frame_offset & 0x8000)) {
83 if (!frame_offset) {
84 /* This packet has no frame headers in it */
85 if (url_ftell(pb) >= ffm->packet_size * 3) {
86 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
87 goto retry_read;
88 }
89 /* This is bad, we cannot find a valid frame header */
90 return 0;
91 }
92 ffm->first_packet = 0;
93 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
94 return -1;
95 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
96 if (!first)
97 break;
98 } else {
99 ffm->packet_ptr = ffm->packet;
100 }
101 goto redo;
102 }
103 memcpy(buf, ffm->packet_ptr, len);
104 buf += len;
105 ffm->packet_ptr += len;
106 size -= len;
107 first = 0;
108 }
109 return size1 - size;
110 }
111
112
113 static void adjust_write_index(AVFormatContext *s)
114 {
115 FFMContext *ffm = s->priv_data;
116 ByteIOContext *pb = s->pb;
117 int64_t pts;
118 //offset_t orig_write_index = ffm->write_index;
119 offset_t pos_min, pos_max;
120 int64_t pts_start;
121 offset_t ptr = url_ftell(pb);
122
123
124 pos_min = 0;
125 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
126
127 pts_start = get_pts(s, pos_min);
128
129 pts = get_pts(s, pos_max);
130
131 if (pts - 100000 > pts_start)
132 goto end;
133
134 ffm->write_index = FFM_PACKET_SIZE;
135
136 pts_start = get_pts(s, pos_min);
137
138 pts = get_pts(s, pos_max);
139
140 if (pts - 100000 <= pts_start) {
141 while (1) {
142 offset_t newpos;
143 int64_t newpts;
144
145 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
146
147 if (newpos == pos_min)
148 break;
149
150 newpts = get_pts(s, newpos);
151
152 if (newpts - 100000 <= pts) {
153 pos_max = newpos;
154 pts = newpts;
155 } else {
156 pos_min = newpos;
157 }
158 }
159 ffm->write_index += pos_max;
160 }
161
162 //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
163 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
164
165 end:
166 url_fseek(pb, ptr, SEEK_SET);
167 }
168
169
170 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
171 {
172 FFMContext *ffm = s->priv_data;
173 AVStream *st;
174 FFMStream *fst;
175 ByteIOContext *pb = s->pb;
176 AVCodecContext *codec;
177 int i, nb_streams;
178 uint32_t tag;
179
180 /* header */
181 tag = get_le32(pb);
182 if (tag != MKTAG('F', 'F', 'M', '1'))
183 goto fail;
184 ffm->packet_size = get_be32(pb);
185 if (ffm->packet_size != FFM_PACKET_SIZE)
186 goto fail;
187 ffm->write_index = get_be64(pb);
188 /* get also filesize */
189 if (!url_is_streamed(pb)) {
190 ffm->file_size = url_fsize(pb);
191 adjust_write_index(s);
192 } else {
193 ffm->file_size = (UINT64_C(1) << 63) - 1;
194 }
195
196 nb_streams = get_be32(pb);
197 get_be32(pb); /* total bitrate */
198 /* read each stream */
199 for(i=0;i<nb_streams;i++) {
200 char rc_eq_buf[128];
201
202 st = av_new_stream(s, 0);
203 if (!st)
204 goto fail;
205 fst = av_mallocz(sizeof(FFMStream));
206 if (!fst)
207 goto fail;
208 s->streams[i] = st;
209
210 av_set_pts_info(st, 64, 1, 1000000);
211
212 st->priv_data = fst;
213
214 codec = st->codec;
215 /* generic info */
216 codec->codec_id = get_be32(pb);
217 codec->codec_type = get_byte(pb); /* codec_type */
218 codec->bit_rate = get_be32(pb);
219 st->quality = get_be32(pb);
220 codec->flags = get_be32(pb);
221 codec->flags2 = get_be32(pb);
222 codec->debug = get_be32(pb);
223 /* specific info */
224 switch(codec->codec_type) {
225 case CODEC_TYPE_VIDEO:
226 codec->time_base.num = get_be32(pb);
227 codec->time_base.den = get_be32(pb);
228 codec->width = get_be16(pb);
229 codec->height = get_be16(pb);
230 codec->gop_size = get_be16(pb);
231 codec->pix_fmt = get_be32(pb);
232 codec->qmin = get_byte(pb);
233 codec->qmax = get_byte(pb);
234 codec->max_qdiff = get_byte(pb);
235 codec->qcompress = get_be16(pb) / 10000.0;
236 codec->qblur = get_be16(pb) / 10000.0;
237 codec->bit_rate_tolerance = get_be32(pb);
238 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
239 codec->rc_max_rate = get_be32(pb);
240 codec->rc_min_rate = get_be32(pb);
241 codec->rc_buffer_size = get_be32(pb);
242 codec->i_quant_factor = av_int2dbl(get_be64(pb));
243 codec->b_quant_factor = av_int2dbl(get_be64(pb));
244 codec->i_quant_offset = av_int2dbl(get_be64(pb));
245 codec->b_quant_offset = av_int2dbl(get_be64(pb));
246 codec->dct_algo = get_be32(pb);
247 codec->strict_std_compliance = get_be32(pb);
248 codec->max_b_frames = get_be32(pb);
249 codec->luma_elim_threshold = get_be32(pb);
250 codec->chroma_elim_threshold = get_be32(pb);
251 codec->mpeg_quant = get_be32(pb);
252 codec->intra_dc_precision = get_be32(pb);
253 codec->me_method = get_be32(pb);
254 codec->mb_decision = get_be32(pb);
255 codec->nsse_weight = get_be32(pb);
256 codec->frame_skip_cmp = get_be32(pb);
257 codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
258 codec->codec_tag = get_be32(pb);
259 break;
260 case CODEC_TYPE_AUDIO:
261 codec->sample_rate = get_be32(pb);
262 codec->channels = get_le16(pb);
263 codec->frame_size = get_le16(pb);
264 break;
265 default:
266 goto fail;
267 }
268
269 }
270
271 /* get until end of block reached */
272 while ((url_ftell(pb) % ffm->packet_size) != 0)
273 get_byte(pb);
274
275 /* init packet demux */
276 ffm->packet_ptr = ffm->packet;
277 ffm->packet_end = ffm->packet;
278 ffm->frame_offset = 0;
279 ffm->pts = 0;
280 ffm->read_state = READ_HEADER;
281 ffm->first_packet = 1;
282 return 0;
283 fail:
284 for(i=0;i<s->nb_streams;i++) {
285 st = s->streams[i];
286 if (st) {
287 av_freep(&st->priv_data);
288 av_free(st);
289 }
290 }
291 return -1;
292 }
293
294 /* return < 0 if eof */
295 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
296 {
297 int size;
298 FFMContext *ffm = s->priv_data;
299 int duration;
300
301 switch(ffm->read_state) {
302 case READ_HEADER:
303 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
304 return AVERROR(EAGAIN);
305 }
306 #if 0
307 printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
308 url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
309 #endif
310 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
311 FRAME_HEADER_SIZE)
312 return AVERROR(EAGAIN);
313 #if 0
314 {
315 int i;
316 for(i=0;i<FRAME_HEADER_SIZE;i++)
317 printf("%02x ", ffm->header[i]);
318 printf("\n");
319 }
320 #endif
321 ffm->read_state = READ_DATA;
322 /* fall thru */
323 case READ_DATA:
324 size = AV_RB24(ffm->header + 2);
325 if (!ffm_is_avail_data(s, size)) {
326 return AVERROR(EAGAIN);
327 }
328
329 duration = AV_RB24(ffm->header + 5);
330
331 av_new_packet(pkt, size);
332 pkt->stream_index = ffm->header[0];
333 if ((unsigned)pkt->stream_index >= s->nb_streams) {
334 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
335 av_free_packet(pkt);
336 ffm->read_state = READ_HEADER;
337 return AVERROR(EAGAIN);
338 }
339 pkt->pos = url_ftell(s->pb);
340 if (ffm->header[1] & FLAG_KEY_FRAME)
341 pkt->flags |= PKT_FLAG_KEY;
342
343 ffm->read_state = READ_HEADER;
344 if (ffm_read_data(s, pkt->data, size, 0) != size) {
345 /* bad case: desynchronized packet. we cancel all the packet loading */
346 av_free_packet(pkt);
347 return AVERROR(EAGAIN);
348 }
349 if (ffm->first_frame_in_packet)
350 {
351 pkt->pts = ffm->pts;
352 ffm->first_frame_in_packet = 0;
353 }
354 pkt->duration = duration;
355 break;
356 }
357 return 0;
358 }
359
360 //#define DEBUG_SEEK
361
362 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
363 by the write position inside this function */
364 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
365 {
366 FFMContext *ffm = s->priv_data;
367 ByteIOContext *pb = s->pb;
368 offset_t pos;
369
370 pos = pos1 + ffm->write_index;
371 if (pos >= ffm->file_size)
372 pos -= (ffm->file_size - FFM_PACKET_SIZE);
373 #ifdef DEBUG_SEEK
374 printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
375 #endif
376 url_fseek(pb, pos, SEEK_SET);
377 }
378
379 static int64_t get_pts(AVFormatContext *s, offset_t pos)
380 {
381 ByteIOContext *pb = s->pb;
382 int64_t pts;
383
384 ffm_seek1(s, pos);
385 url_fskip(pb, 4);
386 pts = get_be64(pb);
387 #ifdef DEBUG_SEEK
388 printf("pts=%0.6f\n", pts / 1000000.0);
389 #endif
390 return pts;
391 }
392
393 /* seek to a given time in the file. The file read pointer is
394 positioned at or before pts. XXX: the following code is quite
395 approximative */
396 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
397 {
398 FFMContext *ffm = s->priv_data;
399 offset_t pos_min, pos_max, pos;
400 int64_t pts_min, pts_max, pts;
401 double pos1;
402
403 #ifdef DEBUG_SEEK
404 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
405 #endif
406 /* find the position using linear interpolation (better than
407 dichotomy in typical cases) */
408 pos_min = 0;
409 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
410 while (pos_min <= pos_max) {
411 pts_min = get_pts(s, pos_min);
412 pts_max = get_pts(s, pos_max);
413 /* linear interpolation */
414 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
415 (double)(pts_max - pts_min);
416 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
417 if (pos <= pos_min)
418 pos = pos_min;
419 else if (pos >= pos_max)
420 pos = pos_max;
421 pts = get_pts(s, pos);
422 /* check if we are lucky */
423 if (pts == wanted_pts) {
424 goto found;
425 } else if (pts > wanted_pts) {
426 pos_max = pos - FFM_PACKET_SIZE;
427 } else {
428 pos_min = pos + FFM_PACKET_SIZE;
429 }
430 }
431 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
432 if (pos > 0)
433 pos -= FFM_PACKET_SIZE;
434 found:
435 ffm_seek1(s, pos);
436 return 0;
437 }
438
439 #ifdef CONFIG_FFSERVER
440 offset_t ffm_read_write_index(int fd)
441 {
442 uint8_t buf[8];
443
444 lseek(fd, 8, SEEK_SET);
445 read(fd, buf, 8);
446 return AV_RB64(buf);
447 }
448
449 void ffm_write_write_index(int fd, offset_t pos)
450 {
451 uint8_t buf[8];
452 int i;
453
454 for(i=0;i<8;i++)
455 buf[i] = (pos >> (56 - i * 8)) & 0xff;
456 lseek(fd, 8, SEEK_SET);
457 write(fd, buf, 8);
458 }
459
460 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
461 {
462 FFMContext *ffm = s->priv_data;
463 ffm->write_index = pos;
464 ffm->file_size = file_size;
465 }
466 #endif // CONFIG_FFSERVER
467
468 static int ffm_read_close(AVFormatContext *s)
469 {
470 AVStream *st;
471 int i;
472
473 for(i=0;i<s->nb_streams;i++) {
474 st = s->streams[i];
475 av_freep(&st->priv_data);
476 }
477 return 0;
478 }
479
480 static int ffm_probe(AVProbeData *p)
481 {
482 if (
483 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
484 p->buf[3] == '1')
485 return AVPROBE_SCORE_MAX + 1;
486 return 0;
487 }
488
489 AVInputFormat ffm_demuxer = {
490 "ffm",
491 "ffm format",
492 sizeof(FFMContext),
493 ffm_probe,
494 ffm_read_header,
495 ffm_read_packet,
496 ffm_read_close,
497 ffm_seek,
498 };