Libav 0.7.1
libavformat/ffmdec.c
Go to the documentation of this file.
00001 /*
00002  * FFM (ffserver live feed) demuxer
00003  * Copyright (c) 2001 Fabrice Bellard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "ffm.h"
00025 #if CONFIG_FFSERVER
00026 #include <unistd.h>
00027 
00028 int64_t ffm_read_write_index(int fd)
00029 {
00030     uint8_t buf[8];
00031 
00032     lseek(fd, 8, SEEK_SET);
00033     if (read(fd, buf, 8) != 8)
00034         return AVERROR(EIO);
00035     return AV_RB64(buf);
00036 }
00037 
00038 int ffm_write_write_index(int fd, int64_t pos)
00039 {
00040     uint8_t buf[8];
00041     int i;
00042 
00043     for(i=0;i<8;i++)
00044         buf[i] = (pos >> (56 - i * 8)) & 0xff;
00045     lseek(fd, 8, SEEK_SET);
00046     if (write(fd, buf, 8) != 8)
00047         return AVERROR(EIO);
00048     return 8;
00049 }
00050 
00051 void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
00052 {
00053     FFMContext *ffm = s->priv_data;
00054     ffm->write_index = pos;
00055     ffm->file_size = file_size;
00056 }
00057 #endif // CONFIG_FFSERVER
00058 
00059 static int ffm_is_avail_data(AVFormatContext *s, int size)
00060 {
00061     FFMContext *ffm = s->priv_data;
00062     int64_t pos, avail_size;
00063     int len;
00064 
00065     len = ffm->packet_end - ffm->packet_ptr;
00066     if (size <= len)
00067         return 1;
00068     pos = avio_tell(s->pb);
00069     if (!ffm->write_index) {
00070         if (pos == ffm->file_size)
00071             return AVERROR_EOF;
00072         avail_size = ffm->file_size - pos;
00073     } else {
00074     if (pos == ffm->write_index) {
00075         /* exactly at the end of stream */
00076         return AVERROR(EAGAIN);
00077     } else if (pos < ffm->write_index) {
00078         avail_size = ffm->write_index - pos;
00079     } else {
00080         avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
00081     }
00082     }
00083     avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
00084     if (size <= avail_size)
00085         return 1;
00086     else
00087         return AVERROR(EAGAIN);
00088 }
00089 
00090 static int ffm_resync(AVFormatContext *s, int state)
00091 {
00092     av_log(s, AV_LOG_ERROR, "resyncing\n");
00093     while (state != PACKET_ID) {
00094         if (s->pb->eof_reached) {
00095             av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
00096             return -1;
00097         }
00098         state = (state << 8) | avio_r8(s->pb);
00099     }
00100     return 0;
00101 }
00102 
00103 /* first is true if we read the frame header */
00104 static int ffm_read_data(AVFormatContext *s,
00105                          uint8_t *buf, int size, int header)
00106 {
00107     FFMContext *ffm = s->priv_data;
00108     AVIOContext *pb = s->pb;
00109     int len, fill_size, size1, frame_offset, id;
00110 
00111     size1 = size;
00112     while (size > 0) {
00113     redo:
00114         len = ffm->packet_end - ffm->packet_ptr;
00115         if (len < 0)
00116             return -1;
00117         if (len > size)
00118             len = size;
00119         if (len == 0) {
00120             if (avio_tell(pb) == ffm->file_size)
00121                 avio_seek(pb, ffm->packet_size, SEEK_SET);
00122     retry_read:
00123             id = avio_rb16(pb); /* PACKET_ID */
00124             if (id != PACKET_ID)
00125                 if (ffm_resync(s, id) < 0)
00126                     return -1;
00127             fill_size = avio_rb16(pb);
00128             ffm->dts = avio_rb64(pb);
00129             frame_offset = avio_rb16(pb);
00130             avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
00131             ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
00132             if (ffm->packet_end < ffm->packet || frame_offset < 0)
00133                 return -1;
00134             /* if first packet or resynchronization packet, we must
00135                handle it specifically */
00136             if (ffm->first_packet || (frame_offset & 0x8000)) {
00137                 if (!frame_offset) {
00138                     /* This packet has no frame headers in it */
00139                     if (avio_tell(pb) >= ffm->packet_size * 3) {
00140                         avio_seek(pb, -ffm->packet_size * 2, SEEK_CUR);
00141                         goto retry_read;
00142                     }
00143                     /* This is bad, we cannot find a valid frame header */
00144                     return 0;
00145                 }
00146                 ffm->first_packet = 0;
00147                 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
00148                     return -1;
00149                 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
00150                 if (!header)
00151                     break;
00152             } else {
00153                 ffm->packet_ptr = ffm->packet;
00154             }
00155             goto redo;
00156         }
00157         memcpy(buf, ffm->packet_ptr, len);
00158         buf += len;
00159         ffm->packet_ptr += len;
00160         size -= len;
00161         header = 0;
00162     }
00163     return size1 - size;
00164 }
00165 
00166 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
00167    and file_size - FFM_PACKET_SIZE */
00168 static void ffm_seek1(AVFormatContext *s, int64_t pos1)
00169 {
00170     FFMContext *ffm = s->priv_data;
00171     AVIOContext *pb = s->pb;
00172     int64_t pos;
00173 
00174     pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
00175     pos = FFMAX(pos, FFM_PACKET_SIZE);
00176     av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
00177     avio_seek(pb, pos, SEEK_SET);
00178 }
00179 
00180 static int64_t get_dts(AVFormatContext *s, int64_t pos)
00181 {
00182     AVIOContext *pb = s->pb;
00183     int64_t dts;
00184 
00185     ffm_seek1(s, pos);
00186     avio_skip(pb, 4);
00187     dts = avio_rb64(pb);
00188     av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
00189     return dts;
00190 }
00191 
00192 static void adjust_write_index(AVFormatContext *s)
00193 {
00194     FFMContext *ffm = s->priv_data;
00195     AVIOContext *pb = s->pb;
00196     int64_t pts;
00197     //int64_t orig_write_index = ffm->write_index;
00198     int64_t pos_min, pos_max;
00199     int64_t pts_start;
00200     int64_t ptr = avio_tell(pb);
00201 
00202 
00203     pos_min = 0;
00204     pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
00205 
00206     pts_start = get_dts(s, pos_min);
00207 
00208     pts = get_dts(s, pos_max);
00209 
00210     if (pts - 100000 > pts_start)
00211         goto end;
00212 
00213     ffm->write_index = FFM_PACKET_SIZE;
00214 
00215     pts_start = get_dts(s, pos_min);
00216 
00217     pts = get_dts(s, pos_max);
00218 
00219     if (pts - 100000 <= pts_start) {
00220         while (1) {
00221             int64_t newpos;
00222             int64_t newpts;
00223 
00224             newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
00225 
00226             if (newpos == pos_min)
00227                 break;
00228 
00229             newpts = get_dts(s, newpos);
00230 
00231             if (newpts - 100000 <= pts) {
00232                 pos_max = newpos;
00233                 pts = newpts;
00234             } else {
00235                 pos_min = newpos;
00236             }
00237         }
00238         ffm->write_index += pos_max;
00239     }
00240 
00241     //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
00242     //printf("pts range %0.6f - %0.6f\n", get_dts(s, 0) / 1000000. , get_dts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
00243 
00244  end:
00245     avio_seek(pb, ptr, SEEK_SET);
00246 }
00247 
00248 
00249 static int ffm_close(AVFormatContext *s)
00250 {
00251     int i;
00252 
00253     for (i = 0; i < s->nb_streams; i++)
00254         av_freep(&s->streams[i]->codec->rc_eq);
00255 
00256     return 0;
00257 }
00258 
00259 
00260 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
00261 {
00262     FFMContext *ffm = s->priv_data;
00263     AVStream *st;
00264     AVIOContext *pb = s->pb;
00265     AVCodecContext *codec;
00266     int i, nb_streams;
00267     uint32_t tag;
00268 
00269     /* header */
00270     tag = avio_rl32(pb);
00271     if (tag != MKTAG('F', 'F', 'M', '1'))
00272         goto fail;
00273     ffm->packet_size = avio_rb32(pb);
00274     if (ffm->packet_size != FFM_PACKET_SIZE)
00275         goto fail;
00276     ffm->write_index = avio_rb64(pb);
00277     /* get also filesize */
00278     if (pb->seekable) {
00279         ffm->file_size = avio_size(pb);
00280         if (ffm->write_index)
00281             adjust_write_index(s);
00282     } else {
00283         ffm->file_size = (UINT64_C(1) << 63) - 1;
00284     }
00285 
00286     nb_streams = avio_rb32(pb);
00287     avio_rb32(pb); /* total bitrate */
00288     /* read each stream */
00289     for(i=0;i<nb_streams;i++) {
00290         char rc_eq_buf[128];
00291 
00292         st = av_new_stream(s, 0);
00293         if (!st)
00294             goto fail;
00295 
00296         av_set_pts_info(st, 64, 1, 1000000);
00297 
00298         codec = st->codec;
00299         /* generic info */
00300         codec->codec_id = avio_rb32(pb);
00301         codec->codec_type = avio_r8(pb); /* codec_type */
00302         codec->bit_rate = avio_rb32(pb);
00303         st->quality = avio_rb32(pb);
00304         codec->flags = avio_rb32(pb);
00305         codec->flags2 = avio_rb32(pb);
00306         codec->debug = avio_rb32(pb);
00307         /* specific info */
00308         switch(codec->codec_type) {
00309         case AVMEDIA_TYPE_VIDEO:
00310             codec->time_base.num = avio_rb32(pb);
00311             codec->time_base.den = avio_rb32(pb);
00312             codec->width = avio_rb16(pb);
00313             codec->height = avio_rb16(pb);
00314             codec->gop_size = avio_rb16(pb);
00315             codec->pix_fmt = avio_rb32(pb);
00316             codec->qmin = avio_r8(pb);
00317             codec->qmax = avio_r8(pb);
00318             codec->max_qdiff = avio_r8(pb);
00319             codec->qcompress = avio_rb16(pb) / 10000.0;
00320             codec->qblur = avio_rb16(pb) / 10000.0;
00321             codec->bit_rate_tolerance = avio_rb32(pb);
00322             avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
00323             codec->rc_eq = av_strdup(rc_eq_buf);
00324             codec->rc_max_rate = avio_rb32(pb);
00325             codec->rc_min_rate = avio_rb32(pb);
00326             codec->rc_buffer_size = avio_rb32(pb);
00327             codec->i_quant_factor = av_int2dbl(avio_rb64(pb));
00328             codec->b_quant_factor = av_int2dbl(avio_rb64(pb));
00329             codec->i_quant_offset = av_int2dbl(avio_rb64(pb));
00330             codec->b_quant_offset = av_int2dbl(avio_rb64(pb));
00331             codec->dct_algo = avio_rb32(pb);
00332             codec->strict_std_compliance = avio_rb32(pb);
00333             codec->max_b_frames = avio_rb32(pb);
00334             codec->luma_elim_threshold = avio_rb32(pb);
00335             codec->chroma_elim_threshold = avio_rb32(pb);
00336             codec->mpeg_quant = avio_rb32(pb);
00337             codec->intra_dc_precision = avio_rb32(pb);
00338             codec->me_method = avio_rb32(pb);
00339             codec->mb_decision = avio_rb32(pb);
00340             codec->nsse_weight = avio_rb32(pb);
00341             codec->frame_skip_cmp = avio_rb32(pb);
00342             codec->rc_buffer_aggressivity = av_int2dbl(avio_rb64(pb));
00343             codec->codec_tag = avio_rb32(pb);
00344             codec->thread_count = avio_r8(pb);
00345             codec->coder_type = avio_rb32(pb);
00346             codec->me_cmp = avio_rb32(pb);
00347             codec->partitions = avio_rb32(pb);
00348             codec->me_subpel_quality = avio_rb32(pb);
00349             codec->me_range = avio_rb32(pb);
00350             codec->keyint_min = avio_rb32(pb);
00351             codec->scenechange_threshold = avio_rb32(pb);
00352             codec->b_frame_strategy = avio_rb32(pb);
00353             codec->qcompress = av_int2dbl(avio_rb64(pb));
00354             codec->qblur = av_int2dbl(avio_rb64(pb));
00355             codec->max_qdiff = avio_rb32(pb);
00356             codec->refs = avio_rb32(pb);
00357             codec->directpred = avio_rb32(pb);
00358             break;
00359         case AVMEDIA_TYPE_AUDIO:
00360             codec->sample_rate = avio_rb32(pb);
00361             codec->channels = avio_rl16(pb);
00362             codec->frame_size = avio_rl16(pb);
00363             codec->sample_fmt = (int16_t) avio_rl16(pb);
00364             break;
00365         default:
00366             goto fail;
00367         }
00368         if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
00369             codec->extradata_size = avio_rb32(pb);
00370             codec->extradata = av_malloc(codec->extradata_size);
00371             if (!codec->extradata)
00372                 return AVERROR(ENOMEM);
00373             avio_read(pb, codec->extradata, codec->extradata_size);
00374         }
00375     }
00376 
00377     /* get until end of block reached */
00378     while ((avio_tell(pb) % ffm->packet_size) != 0)
00379         avio_r8(pb);
00380 
00381     /* init packet demux */
00382     ffm->packet_ptr = ffm->packet;
00383     ffm->packet_end = ffm->packet;
00384     ffm->frame_offset = 0;
00385     ffm->dts = 0;
00386     ffm->read_state = READ_HEADER;
00387     ffm->first_packet = 1;
00388     return 0;
00389  fail:
00390     ffm_close(s);
00391     return -1;
00392 }
00393 
00394 /* return < 0 if eof */
00395 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
00396 {
00397     int size;
00398     FFMContext *ffm = s->priv_data;
00399     int duration, ret;
00400 
00401     switch(ffm->read_state) {
00402     case READ_HEADER:
00403         if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
00404             return ret;
00405 
00406         av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
00407                avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
00408         if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
00409             FRAME_HEADER_SIZE)
00410             return -1;
00411         if (ffm->header[1] & FLAG_DTS)
00412             if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
00413                 return -1;
00414         ffm->read_state = READ_DATA;
00415         /* fall thru */
00416     case READ_DATA:
00417         size = AV_RB24(ffm->header + 2);
00418         if ((ret = ffm_is_avail_data(s, size)) < 0)
00419             return ret;
00420 
00421         duration = AV_RB24(ffm->header + 5);
00422 
00423         av_new_packet(pkt, size);
00424         pkt->stream_index = ffm->header[0];
00425         if ((unsigned)pkt->stream_index >= s->nb_streams) {
00426             av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
00427             av_free_packet(pkt);
00428             ffm->read_state = READ_HEADER;
00429             return -1;
00430         }
00431         pkt->pos = avio_tell(s->pb);
00432         if (ffm->header[1] & FLAG_KEY_FRAME)
00433             pkt->flags |= AV_PKT_FLAG_KEY;
00434 
00435         ffm->read_state = READ_HEADER;
00436         if (ffm_read_data(s, pkt->data, size, 0) != size) {
00437             /* bad case: desynchronized packet. we cancel all the packet loading */
00438             av_free_packet(pkt);
00439             return -1;
00440         }
00441         pkt->pts = AV_RB64(ffm->header+8);
00442         if (ffm->header[1] & FLAG_DTS)
00443             pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
00444         else
00445             pkt->dts = pkt->pts;
00446         pkt->duration = duration;
00447         break;
00448     }
00449     return 0;
00450 }
00451 
00452 /* seek to a given time in the file. The file read pointer is
00453    positioned at or before pts. XXX: the following code is quite
00454    approximative */
00455 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
00456 {
00457     FFMContext *ffm = s->priv_data;
00458     int64_t pos_min, pos_max, pos;
00459     int64_t pts_min, pts_max, pts;
00460     double pos1;
00461 
00462     av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
00463     /* find the position using linear interpolation (better than
00464        dichotomy in typical cases) */
00465     pos_min = FFM_PACKET_SIZE;
00466     pos_max = ffm->file_size - FFM_PACKET_SIZE;
00467     while (pos_min <= pos_max) {
00468         pts_min = get_dts(s, pos_min);
00469         pts_max = get_dts(s, pos_max);
00470         /* linear interpolation */
00471         pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
00472             (double)(pts_max - pts_min);
00473         pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
00474         if (pos <= pos_min)
00475             pos = pos_min;
00476         else if (pos >= pos_max)
00477             pos = pos_max;
00478         pts = get_dts(s, pos);
00479         /* check if we are lucky */
00480         if (pts == wanted_pts) {
00481             goto found;
00482         } else if (pts > wanted_pts) {
00483             pos_max = pos - FFM_PACKET_SIZE;
00484         } else {
00485             pos_min = pos + FFM_PACKET_SIZE;
00486         }
00487     }
00488     pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
00489 
00490  found:
00491     ffm_seek1(s, pos);
00492 
00493     /* reset read state */
00494     ffm->read_state = READ_HEADER;
00495     ffm->packet_ptr = ffm->packet;
00496     ffm->packet_end = ffm->packet;
00497     ffm->first_packet = 1;
00498 
00499     return 0;
00500 }
00501 
00502 static int ffm_probe(AVProbeData *p)
00503 {
00504     if (
00505         p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
00506         p->buf[3] == '1')
00507         return AVPROBE_SCORE_MAX + 1;
00508     return 0;
00509 }
00510 
00511 AVInputFormat ff_ffm_demuxer = {
00512     "ffm",
00513     NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
00514     sizeof(FFMContext),
00515     ffm_probe,
00516     ffm_read_header,
00517     ffm_read_packet,
00518     ffm_close,
00519     ffm_seek,
00520 };