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