Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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 <stdint.h>
  23.  
  24. #include "libavutil/internal.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/intfloat.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "ffm.h"
  33. #include "avio_internal.h"
  34.  
  35. static int ffm_is_avail_data(AVFormatContext *s, int size)
  36. {
  37.     FFMContext *ffm = s->priv_data;
  38.     int64_t pos, avail_size;
  39.     int len;
  40.  
  41.     len = ffm->packet_end - ffm->packet_ptr;
  42.     if (size <= len)
  43.         return 1;
  44.     pos = avio_tell(s->pb);
  45.     if (!ffm->write_index) {
  46.         if (pos == ffm->file_size)
  47.             return AVERROR_EOF;
  48.         avail_size = ffm->file_size - pos;
  49.     } else {
  50.     if (pos == ffm->write_index) {
  51.         /* exactly at the end of stream */
  52.         return AVERROR(EAGAIN);
  53.     } else if (pos < ffm->write_index) {
  54.         avail_size = ffm->write_index - pos;
  55.     } else {
  56.         avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  57.     }
  58.     }
  59.     avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  60.     if (size <= avail_size)
  61.         return 1;
  62.     else
  63.         return AVERROR(EAGAIN);
  64. }
  65.  
  66. static int ffm_resync(AVFormatContext *s, int state)
  67. {
  68.     av_log(s, AV_LOG_ERROR, "resyncing\n");
  69.     while (state != PACKET_ID) {
  70.         if (avio_feof(s->pb)) {
  71.             av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
  72.             return -1;
  73.         }
  74.         state = (state << 8) | avio_r8(s->pb);
  75.     }
  76.     return 0;
  77. }
  78.  
  79. /* first is true if we read the frame header */
  80. static int ffm_read_data(AVFormatContext *s,
  81.                          uint8_t *buf, int size, int header)
  82. {
  83.     FFMContext *ffm = s->priv_data;
  84.     AVIOContext *pb = s->pb;
  85.     int len, fill_size, size1, frame_offset, id;
  86.     int64_t last_pos = -1;
  87.  
  88.     size1 = size;
  89.     while (size > 0) {
  90.     redo:
  91.         len = ffm->packet_end - ffm->packet_ptr;
  92.         if (len < 0)
  93.             return -1;
  94.         if (len > size)
  95.             len = size;
  96.         if (len == 0) {
  97.             if (avio_tell(pb) == ffm->file_size)
  98.                 avio_seek(pb, ffm->packet_size, SEEK_SET);
  99.     retry_read:
  100.             if (pb->buffer_size != ffm->packet_size) {
  101.                 int64_t tell = avio_tell(pb);
  102.                 int ret = ffio_set_buf_size(pb, ffm->packet_size);
  103.                 if (ret < 0)
  104.                     return ret;
  105.                 avio_seek(pb, tell, SEEK_SET);
  106.             }
  107.             id = avio_rb16(pb); /* PACKET_ID */
  108.             if (id != PACKET_ID) {
  109.                 if (ffm_resync(s, id) < 0)
  110.                     return -1;
  111.                 last_pos = avio_tell(pb);
  112.             }
  113.             fill_size = avio_rb16(pb);
  114.             ffm->dts = avio_rb64(pb);
  115.             frame_offset = avio_rb16(pb);
  116.             avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  117.             ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  118.             if (ffm->packet_end < ffm->packet || frame_offset < 0)
  119.                 return -1;
  120.             /* if first packet or resynchronization packet, we must
  121.                handle it specifically */
  122.             if (ffm->first_packet || (frame_offset & 0x8000)) {
  123.                 if (!frame_offset) {
  124.                     /* This packet has no frame headers in it */
  125.                     if (avio_tell(pb) >= ffm->packet_size * 3LL) {
  126.                         int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
  127.                         seekback = FFMAX(seekback, 0);
  128.                         avio_seek(pb, -seekback, SEEK_CUR);
  129.                         goto retry_read;
  130.                     }
  131.                     /* This is bad, we cannot find a valid frame header */
  132.                     return 0;
  133.                 }
  134.                 ffm->first_packet = 0;
  135.                 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
  136.                     return -1;
  137.                 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  138.                 if (!header)
  139.                     break;
  140.             } else {
  141.                 ffm->packet_ptr = ffm->packet;
  142.             }
  143.             goto redo;
  144.         }
  145.         memcpy(buf, ffm->packet_ptr, len);
  146.         buf += len;
  147.         ffm->packet_ptr += len;
  148.         size -= len;
  149.         header = 0;
  150.     }
  151.     return size1 - size;
  152. }
  153.  
  154. /* ensure that actual seeking happens between FFM_PACKET_SIZE
  155.    and file_size - FFM_PACKET_SIZE */
  156. static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
  157. {
  158.     FFMContext *ffm = s->priv_data;
  159.     AVIOContext *pb = s->pb;
  160.     int64_t pos;
  161.  
  162.     pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
  163.     pos = FFMAX(pos, FFM_PACKET_SIZE);
  164.     ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  165.     return avio_seek(pb, pos, SEEK_SET);
  166. }
  167.  
  168. static int64_t get_dts(AVFormatContext *s, int64_t pos)
  169. {
  170.     AVIOContext *pb = s->pb;
  171.     int64_t dts;
  172.  
  173.     ffm_seek1(s, pos);
  174.     avio_skip(pb, 4);
  175.     dts = avio_rb64(pb);
  176.     ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
  177.     return dts;
  178. }
  179.  
  180. static void adjust_write_index(AVFormatContext *s)
  181. {
  182.     FFMContext *ffm = s->priv_data;
  183.     AVIOContext *pb = s->pb;
  184.     int64_t pts;
  185.     //int64_t orig_write_index = ffm->write_index;
  186.     int64_t pos_min, pos_max;
  187.     int64_t pts_start;
  188.     int64_t ptr = avio_tell(pb);
  189.  
  190.  
  191.     pos_min = 0;
  192.     pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  193.  
  194.     pts_start = get_dts(s, pos_min);
  195.  
  196.     pts = get_dts(s, pos_max);
  197.  
  198.     if (pts - 100000 > pts_start)
  199.         goto end;
  200.  
  201.     ffm->write_index = FFM_PACKET_SIZE;
  202.  
  203.     pts_start = get_dts(s, pos_min);
  204.  
  205.     pts = get_dts(s, pos_max);
  206.  
  207.     if (pts - 100000 <= pts_start) {
  208.         while (1) {
  209.             int64_t newpos;
  210.             int64_t newpts;
  211.  
  212.             newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  213.  
  214.             if (newpos == pos_min)
  215.                 break;
  216.  
  217.             newpts = get_dts(s, newpos);
  218.  
  219.             if (newpts - 100000 <= pts) {
  220.                 pos_max = newpos;
  221.                 pts = newpts;
  222.             } else {
  223.                 pos_min = newpos;
  224.             }
  225.         }
  226.         ffm->write_index += pos_max;
  227.     }
  228.  
  229.  end:
  230.     avio_seek(pb, ptr, SEEK_SET);
  231. }
  232.  
  233.  
  234. static int ffm_close(AVFormatContext *s)
  235. {
  236.     int i;
  237.  
  238.     for (i = 0; i < s->nb_streams; i++)
  239.         av_freep(&s->streams[i]->codec->rc_eq);
  240.  
  241.     return 0;
  242. }
  243.  
  244. static int ffm_append_recommended_configuration(AVStream *st, char **conf)
  245. {
  246.     int ret;
  247.     size_t newsize;
  248.     av_assert0(conf && st);
  249.     if (!*conf)
  250.         return 0;
  251.     if (!st->recommended_encoder_configuration) {
  252.         st->recommended_encoder_configuration = *conf;
  253.         *conf = 0;
  254.         return 0;
  255.     }
  256.     newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
  257.     if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
  258.         return ret;
  259.     av_strlcat(st->recommended_encoder_configuration, ",", newsize);
  260.     av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
  261.     av_freep(conf);
  262.     return 0;
  263. }
  264.  
  265. static int ffm2_read_header(AVFormatContext *s)
  266. {
  267.     FFMContext *ffm = s->priv_data;
  268.     AVStream *st;
  269.     AVIOContext *pb = s->pb;
  270.     AVCodecContext *codec;
  271.     const AVCodecDescriptor *codec_desc;
  272.     int ret;
  273.     int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
  274.     AVCodec *enc;
  275.     char *buffer;
  276.  
  277.     ffm->packet_size = avio_rb32(pb);
  278.     if (ffm->packet_size != FFM_PACKET_SIZE) {
  279.         av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
  280.                ffm->packet_size, FFM_PACKET_SIZE);
  281.         ret = AVERROR_INVALIDDATA;
  282.         goto fail;
  283.     }
  284.  
  285.     ffm->write_index = avio_rb64(pb);
  286.     /* get also filesize */
  287.     if (pb->seekable) {
  288.         ffm->file_size = avio_size(pb);
  289.         if (ffm->write_index && 0)
  290.             adjust_write_index(s);
  291.     } else {
  292.         ffm->file_size = (UINT64_C(1) << 63) - 1;
  293.     }
  294.  
  295.     while(!avio_feof(pb)) {
  296.         unsigned id = avio_rb32(pb);
  297.         unsigned size = avio_rb32(pb);
  298.         int64_t next = avio_tell(pb) + size;
  299.         char rc_eq_buf[128];
  300.  
  301.         if(!id)
  302.             break;
  303.  
  304.         switch(id) {
  305.         case MKBETAG('M', 'A', 'I', 'N'):
  306.             if (f_main++) {
  307.                 ret = AVERROR(EINVAL);
  308.                 goto fail;
  309.             }
  310.             avio_rb32(pb); /* nb_streams */
  311.             avio_rb32(pb); /* total bitrate */
  312.             break;
  313.         case MKBETAG('C', 'O', 'M', 'M'):
  314.             f_cprv = f_stvi = f_stau = 0;
  315.             st = avformat_new_stream(s, NULL);
  316.             if (!st) {
  317.                 ret = AVERROR(ENOMEM);
  318.                 goto fail;
  319.             }
  320.  
  321.             avpriv_set_pts_info(st, 64, 1, 1000000);
  322.  
  323.             codec = st->codec;
  324.             /* generic info */
  325.             codec->codec_id = avio_rb32(pb);
  326.             codec_desc = avcodec_descriptor_get(codec->codec_id);
  327.             if (!codec_desc) {
  328.                 av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
  329.                 codec->codec_id = AV_CODEC_ID_NONE;
  330.                 goto fail;
  331.             }
  332.             codec->codec_type = avio_r8(pb);
  333.             if (codec->codec_type != codec_desc->type) {
  334.                 av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  335.                        codec_desc->type, codec->codec_type);
  336.                 codec->codec_id = AV_CODEC_ID_NONE;
  337.                 codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
  338.                 goto fail;
  339.             }
  340.             codec->bit_rate = avio_rb32(pb);
  341.             codec->flags = avio_rb32(pb);
  342.             codec->flags2 = avio_rb32(pb);
  343.             codec->debug = avio_rb32(pb);
  344.             if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  345.                 if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  346.                     return AVERROR(ENOMEM);
  347.             }
  348.             break;
  349.         case MKBETAG('S', 'T', 'V', 'I'):
  350.             if (f_stvi++) {
  351.                 ret = AVERROR(EINVAL);
  352.                 goto fail;
  353.             }
  354.             codec->time_base.num = avio_rb32(pb);
  355.             codec->time_base.den = avio_rb32(pb);
  356.             if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  357.                 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  358.                        codec->time_base.num, codec->time_base.den);
  359.                 ret = AVERROR_INVALIDDATA;
  360.                 goto fail;
  361.             }
  362.             codec->width = avio_rb16(pb);
  363.             codec->height = avio_rb16(pb);
  364.             codec->gop_size = avio_rb16(pb);
  365.             codec->pix_fmt = avio_rb32(pb);
  366.             codec->qmin = avio_r8(pb);
  367.             codec->qmax = avio_r8(pb);
  368.             codec->max_qdiff = avio_r8(pb);
  369.             codec->qcompress = avio_rb16(pb) / 10000.0;
  370.             codec->qblur = avio_rb16(pb) / 10000.0;
  371.             codec->bit_rate_tolerance = avio_rb32(pb);
  372.             avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  373.             codec->rc_eq = av_strdup(rc_eq_buf);
  374.             codec->rc_max_rate = avio_rb32(pb);
  375.             codec->rc_min_rate = avio_rb32(pb);
  376.             codec->rc_buffer_size = avio_rb32(pb);
  377.             codec->i_quant_factor = av_int2double(avio_rb64(pb));
  378.             codec->b_quant_factor = av_int2double(avio_rb64(pb));
  379.             codec->i_quant_offset = av_int2double(avio_rb64(pb));
  380.             codec->b_quant_offset = av_int2double(avio_rb64(pb));
  381.             codec->dct_algo = avio_rb32(pb);
  382.             codec->strict_std_compliance = avio_rb32(pb);
  383.             codec->max_b_frames = avio_rb32(pb);
  384.             codec->mpeg_quant = avio_rb32(pb);
  385.             codec->intra_dc_precision = avio_rb32(pb);
  386.             codec->me_method = avio_rb32(pb);
  387.             codec->mb_decision = avio_rb32(pb);
  388.             codec->nsse_weight = avio_rb32(pb);
  389.             codec->frame_skip_cmp = avio_rb32(pb);
  390.             codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  391.             codec->codec_tag = avio_rb32(pb);
  392.             codec->thread_count = avio_r8(pb);
  393.             codec->coder_type = avio_rb32(pb);
  394.             codec->me_cmp = avio_rb32(pb);
  395.             codec->me_subpel_quality = avio_rb32(pb);
  396.             codec->me_range = avio_rb32(pb);
  397.             codec->keyint_min = avio_rb32(pb);
  398.             codec->scenechange_threshold = avio_rb32(pb);
  399.             codec->b_frame_strategy = avio_rb32(pb);
  400.             codec->qcompress = av_int2double(avio_rb64(pb));
  401.             codec->qblur = av_int2double(avio_rb64(pb));
  402.             codec->max_qdiff = avio_rb32(pb);
  403.             codec->refs = avio_rb32(pb);
  404.             break;
  405.         case MKBETAG('S', 'T', 'A', 'U'):
  406.             if (f_stau++) {
  407.                 ret = AVERROR(EINVAL);
  408.                 goto fail;
  409.             }
  410.             codec->sample_rate = avio_rb32(pb);
  411.             codec->channels = avio_rl16(pb);
  412.             codec->frame_size = avio_rl16(pb);
  413.             break;
  414.         case MKBETAG('C', 'P', 'R', 'V'):
  415.             if (f_cprv++) {
  416.                 ret = AVERROR(EINVAL);
  417.                 goto fail;
  418.             }
  419.             enc = avcodec_find_encoder(codec->codec_id);
  420.             if (enc && enc->priv_data_size && enc->priv_class) {
  421.                 buffer = av_malloc(size + 1);
  422.                 if (!buffer) {
  423.                     ret = AVERROR(ENOMEM);
  424.                     goto fail;
  425.                 }
  426.                 avio_get_str(pb, size, buffer, size + 1);
  427.                 if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  428.                     goto fail;
  429.             }
  430.             break;
  431.         case MKBETAG('S', '2', 'V', 'I'):
  432.             if (f_stvi++ || !size) {
  433.                 ret = AVERROR(EINVAL);
  434.                 goto fail;
  435.             }
  436.             buffer = av_malloc(size);
  437.             if (!buffer) {
  438.                 ret = AVERROR(ENOMEM);
  439.                 goto fail;
  440.             }
  441.             avio_get_str(pb, INT_MAX, buffer, size);
  442.             av_set_options_string(codec, buffer, "=", ",");
  443.             if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  444.                 goto fail;
  445.             break;
  446.         case MKBETAG('S', '2', 'A', 'U'):
  447.             if (f_stau++ || !size) {
  448.                 ret = AVERROR(EINVAL);
  449.                 goto fail;
  450.             }
  451.             buffer = av_malloc(size);
  452.             if (!buffer) {
  453.                 ret = AVERROR(ENOMEM);
  454.                 goto fail;
  455.             }
  456.             avio_get_str(pb, INT_MAX, buffer, size);
  457.             av_set_options_string(codec, buffer, "=", ",");
  458.             if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  459.                 goto fail;
  460.             break;
  461.         }
  462.         avio_seek(pb, next, SEEK_SET);
  463.     }
  464.  
  465.     /* get until end of block reached */
  466.     while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  467.         avio_r8(pb);
  468.  
  469.     /* init packet demux */
  470.     ffm->packet_ptr = ffm->packet;
  471.     ffm->packet_end = ffm->packet;
  472.     ffm->frame_offset = 0;
  473.     ffm->dts = 0;
  474.     ffm->read_state = READ_HEADER;
  475.     ffm->first_packet = 1;
  476.     return 0;
  477.  fail:
  478.     ffm_close(s);
  479.     return ret;
  480. }
  481.  
  482. static int ffm_read_header(AVFormatContext *s)
  483. {
  484.     FFMContext *ffm = s->priv_data;
  485.     AVStream *st;
  486.     AVIOContext *pb = s->pb;
  487.     AVCodecContext *codec;
  488.     const AVCodecDescriptor *codec_desc;
  489.     int i, nb_streams;
  490.     uint32_t tag;
  491.  
  492.     /* header */
  493.     tag = avio_rl32(pb);
  494.     if (tag == MKTAG('F', 'F', 'M', '2'))
  495.         return ffm2_read_header(s);
  496.     if (tag != MKTAG('F', 'F', 'M', '1'))
  497.         goto fail;
  498.     ffm->packet_size = avio_rb32(pb);
  499.     if (ffm->packet_size != FFM_PACKET_SIZE)
  500.         goto fail;
  501.     ffm->write_index = avio_rb64(pb);
  502.     /* get also filesize */
  503.     if (pb->seekable) {
  504.         ffm->file_size = avio_size(pb);
  505.         if (ffm->write_index && 0)
  506.             adjust_write_index(s);
  507.     } else {
  508.         ffm->file_size = (UINT64_C(1) << 63) - 1;
  509.     }
  510.  
  511.     nb_streams = avio_rb32(pb);
  512.     avio_rb32(pb); /* total bitrate */
  513.     /* read each stream */
  514.     for(i=0;i<nb_streams;i++) {
  515.         char rc_eq_buf[128];
  516.  
  517.         st = avformat_new_stream(s, NULL);
  518.         if (!st)
  519.             goto fail;
  520.  
  521.         avpriv_set_pts_info(st, 64, 1, 1000000);
  522.  
  523.         codec = st->codec;
  524.         /* generic info */
  525.         codec->codec_id = avio_rb32(pb);
  526.         codec_desc = avcodec_descriptor_get(codec->codec_id);
  527.         if (!codec_desc) {
  528.             av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
  529.             codec->codec_id = AV_CODEC_ID_NONE;
  530.             goto fail;
  531.         }
  532.         codec->codec_type = avio_r8(pb); /* codec_type */
  533.         if (codec->codec_type != codec_desc->type) {
  534.             av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  535.                    codec_desc->type, codec->codec_type);
  536.             codec->codec_id = AV_CODEC_ID_NONE;
  537.             codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
  538.             goto fail;
  539.         }
  540.         codec->bit_rate = avio_rb32(pb);
  541.         codec->flags = avio_rb32(pb);
  542.         codec->flags2 = avio_rb32(pb);
  543.         codec->debug = avio_rb32(pb);
  544.         /* specific info */
  545.         switch(codec->codec_type) {
  546.         case AVMEDIA_TYPE_VIDEO:
  547.             codec->time_base.num = avio_rb32(pb);
  548.             codec->time_base.den = avio_rb32(pb);
  549.             if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  550.                 av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  551.                        codec->time_base.num, codec->time_base.den);
  552.                 goto fail;
  553.             }
  554.             codec->width = avio_rb16(pb);
  555.             codec->height = avio_rb16(pb);
  556.             codec->gop_size = avio_rb16(pb);
  557.             codec->pix_fmt = avio_rb32(pb);
  558.             codec->qmin = avio_r8(pb);
  559.             codec->qmax = avio_r8(pb);
  560.             codec->max_qdiff = avio_r8(pb);
  561.             codec->qcompress = avio_rb16(pb) / 10000.0;
  562.             codec->qblur = avio_rb16(pb) / 10000.0;
  563.             codec->bit_rate_tolerance = avio_rb32(pb);
  564.             avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  565.             codec->rc_eq = av_strdup(rc_eq_buf);
  566.             codec->rc_max_rate = avio_rb32(pb);
  567.             codec->rc_min_rate = avio_rb32(pb);
  568.             codec->rc_buffer_size = avio_rb32(pb);
  569.             codec->i_quant_factor = av_int2double(avio_rb64(pb));
  570.             codec->b_quant_factor = av_int2double(avio_rb64(pb));
  571.             codec->i_quant_offset = av_int2double(avio_rb64(pb));
  572.             codec->b_quant_offset = av_int2double(avio_rb64(pb));
  573.             codec->dct_algo = avio_rb32(pb);
  574.             codec->strict_std_compliance = avio_rb32(pb);
  575.             codec->max_b_frames = avio_rb32(pb);
  576.             codec->mpeg_quant = avio_rb32(pb);
  577.             codec->intra_dc_precision = avio_rb32(pb);
  578.             codec->me_method = avio_rb32(pb);
  579.             codec->mb_decision = avio_rb32(pb);
  580.             codec->nsse_weight = avio_rb32(pb);
  581.             codec->frame_skip_cmp = avio_rb32(pb);
  582.             codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  583.             codec->codec_tag = avio_rb32(pb);
  584.             codec->thread_count = avio_r8(pb);
  585.             codec->coder_type = avio_rb32(pb);
  586.             codec->me_cmp = avio_rb32(pb);
  587.             codec->me_subpel_quality = avio_rb32(pb);
  588.             codec->me_range = avio_rb32(pb);
  589.             codec->keyint_min = avio_rb32(pb);
  590.             codec->scenechange_threshold = avio_rb32(pb);
  591.             codec->b_frame_strategy = avio_rb32(pb);
  592.             codec->qcompress = av_int2double(avio_rb64(pb));
  593.             codec->qblur = av_int2double(avio_rb64(pb));
  594.             codec->max_qdiff = avio_rb32(pb);
  595.             codec->refs = avio_rb32(pb);
  596.             break;
  597.         case AVMEDIA_TYPE_AUDIO:
  598.             codec->sample_rate = avio_rb32(pb);
  599.             codec->channels = avio_rl16(pb);
  600.             codec->frame_size = avio_rl16(pb);
  601.             break;
  602.         default:
  603.             goto fail;
  604.         }
  605.         if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  606.             if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  607.                 return AVERROR(ENOMEM);
  608.         }
  609.     }
  610.  
  611.     /* get until end of block reached */
  612.     while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  613.         avio_r8(pb);
  614.  
  615.     /* init packet demux */
  616.     ffm->packet_ptr = ffm->packet;
  617.     ffm->packet_end = ffm->packet;
  618.     ffm->frame_offset = 0;
  619.     ffm->dts = 0;
  620.     ffm->read_state = READ_HEADER;
  621.     ffm->first_packet = 1;
  622.     return 0;
  623.  fail:
  624.     ffm_close(s);
  625.     return -1;
  626. }
  627.  
  628. /* return < 0 if eof */
  629. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  630. {
  631.     int size;
  632.     FFMContext *ffm = s->priv_data;
  633.     int duration, ret;
  634.  
  635.     switch(ffm->read_state) {
  636.     case READ_HEADER:
  637.         if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  638.             return ret;
  639.  
  640.         ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  641.                avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  642.         if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  643.             FRAME_HEADER_SIZE)
  644.             return -1;
  645.         if (ffm->header[1] & FLAG_DTS)
  646.             if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  647.                 return -1;
  648.         ffm->read_state = READ_DATA;
  649.         /* fall through */
  650.     case READ_DATA:
  651.         size = AV_RB24(ffm->header + 2);
  652.         if ((ret = ffm_is_avail_data(s, size)) < 0)
  653.             return ret;
  654.  
  655.         duration = AV_RB24(ffm->header + 5);
  656.  
  657.         if (av_new_packet(pkt, size) < 0) {
  658.             return AVERROR(ENOMEM);
  659.         }
  660.         pkt->stream_index = ffm->header[0];
  661.         if ((unsigned)pkt->stream_index >= s->nb_streams) {
  662.             av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  663.             av_free_packet(pkt);
  664.             ffm->read_state = READ_HEADER;
  665.             return -1;
  666.         }
  667.         pkt->pos = avio_tell(s->pb);
  668.         if (ffm->header[1] & FLAG_KEY_FRAME)
  669.             pkt->flags |= AV_PKT_FLAG_KEY;
  670.  
  671.         ffm->read_state = READ_HEADER;
  672.         if (ffm_read_data(s, pkt->data, size, 0) != size) {
  673.             /* bad case: desynchronized packet. we cancel all the packet loading */
  674.             av_free_packet(pkt);
  675.             return -1;
  676.         }
  677.         pkt->pts = AV_RB64(ffm->header+8);
  678.         if (ffm->header[1] & FLAG_DTS)
  679.             pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  680.         else
  681.             pkt->dts = pkt->pts;
  682.         pkt->duration = duration;
  683.         break;
  684.     }
  685.     return 0;
  686. }
  687.  
  688. /* seek to a given time in the file. The file read pointer is
  689.    positioned at or before pts. XXX: the following code is quite
  690.    approximative */
  691. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  692. {
  693.     FFMContext *ffm = s->priv_data;
  694.     int64_t pos_min, pos_max, pos;
  695.     int64_t pts_min, pts_max, pts;
  696.     double pos1;
  697.  
  698.     ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  699.     /* find the position using linear interpolation (better than
  700.        dichotomy in typical cases) */
  701.     if (ffm->write_index && ffm->write_index < ffm->file_size) {
  702.         if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  703.             pos_min = FFM_PACKET_SIZE;
  704.             pos_max = ffm->write_index - FFM_PACKET_SIZE;
  705.         } else {
  706.             pos_min = ffm->write_index;
  707.             pos_max = ffm->file_size - FFM_PACKET_SIZE;
  708.         }
  709.     } else {
  710.         pos_min = FFM_PACKET_SIZE;
  711.         pos_max = ffm->file_size - FFM_PACKET_SIZE;
  712.     }
  713.     while (pos_min <= pos_max) {
  714.         pts_min = get_dts(s, pos_min);
  715.         pts_max = get_dts(s, pos_max);
  716.         if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  717.             pos = pts_min > wanted_pts ? pos_min : pos_max;
  718.             goto found;
  719.         }
  720.         /* linear interpolation */
  721.         pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  722.             (double)(pts_max - pts_min);
  723.         pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  724.         if (pos <= pos_min)
  725.             pos = pos_min;
  726.         else if (pos >= pos_max)
  727.             pos = pos_max;
  728.         pts = get_dts(s, pos);
  729.         /* check if we are lucky */
  730.         if (pts == wanted_pts) {
  731.             goto found;
  732.         } else if (pts > wanted_pts) {
  733.             pos_max = pos - FFM_PACKET_SIZE;
  734.         } else {
  735.             pos_min = pos + FFM_PACKET_SIZE;
  736.         }
  737.     }
  738.     pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  739.  
  740.  found:
  741.     if (ffm_seek1(s, pos) < 0)
  742.         return -1;
  743.  
  744.     /* reset read state */
  745.     ffm->read_state = READ_HEADER;
  746.     ffm->packet_ptr = ffm->packet;
  747.     ffm->packet_end = ffm->packet;
  748.     ffm->first_packet = 1;
  749.  
  750.     return 0;
  751. }
  752.  
  753. static int ffm_probe(AVProbeData *p)
  754. {
  755.     if (
  756.         p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  757.         (p->buf[3] == '1' || p->buf[3] == '2'))
  758.         return AVPROBE_SCORE_MAX + 1;
  759.     return 0;
  760. }
  761.  
  762. AVInputFormat ff_ffm_demuxer = {
  763.     .name           = "ffm",
  764.     .long_name      = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  765.     .priv_data_size = sizeof(FFMContext),
  766.     .read_probe     = ffm_probe,
  767.     .read_header    = ffm_read_header,
  768.     .read_packet    = ffm_read_packet,
  769.     .read_close     = ffm_close,
  770.     .read_seek      = ffm_seek,
  771. };
  772.