OSDN Git Service

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