ffmenc.c 7.85 KB
Newer Older
Baptiste Coudurier's avatar
Baptiste Coudurier committed
1 2
/*
 * FFM (ffserver live feed) muxer
3
 * Copyright (c) 2001 Fabrice Bellard
Baptiste Coudurier's avatar
Baptiste Coudurier committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/intreadwrite.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "avformat.h"
#include "ffm.h"

static void flush_packet(AVFormatContext *s)
{
    FFMContext *ffm = s->priv_data;
    int fill_size, h;
    ByteIOContext *pb = s->pb;

    fill_size = ffm->packet_end - ffm->packet_ptr;
    memset(ffm->packet_ptr, 0, fill_size);

    if (url_ftell(pb) % ffm->packet_size)
        av_abort();

    /* put header */
    put_be16(pb, PACKET_ID);
    put_be16(pb, fill_size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
41
    put_be64(pb, ffm->dts);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    h = ffm->frame_offset;
    if (ffm->first_packet)
        h |= 0x8000;
    put_be16(pb, h);
    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
    put_flush_packet(pb);

    /* prepare next packet */
    ffm->frame_offset = 0; /* no key frame */
    ffm->packet_ptr = ffm->packet;
    ffm->first_packet = 0;
}

/* 'first' is true if first data of a frame */
static void ffm_write_data(AVFormatContext *s,
                           const uint8_t *buf, int size,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
58
                           int64_t dts, int header)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
59 60 61 62
{
    FFMContext *ffm = s->priv_data;
    int len;

63
    if (header && ffm->frame_offset == 0) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
64
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
65
        ffm->dts = dts;
66
    }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
67 68 69 70 71 72 73 74 75 76 77

    /* write as many packets as needed */
    while (size > 0) {
        len = ffm->packet_end - ffm->packet_ptr;
        if (len > size)
            len = size;
        memcpy(ffm->packet_ptr, buf, len);

        ffm->packet_ptr += len;
        buf += len;
        size -= len;
78
        if (ffm->packet_ptr >= ffm->packet_end)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            flush_packet(s);
    }
}

static int ffm_write_header(AVFormatContext *s)
{
    FFMContext *ffm = s->priv_data;
    AVStream *st;
    ByteIOContext *pb = s->pb;
    AVCodecContext *codec;
    int bit_rate, i;

    ffm->packet_size = FFM_PACKET_SIZE;

    /* header */
    put_le32(pb, MKTAG('F', 'F', 'M', '1'));
    put_be32(pb, ffm->packet_size);
96
    put_be64(pb, 0); /* current write position */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    put_be32(pb, s->nb_streams);
    bit_rate = 0;
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
        bit_rate += st->codec->bit_rate;
    }
    put_be32(pb, bit_rate);

    /* list of streams */
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
        av_set_pts_info(st, 64, 1, 1000000);

        codec = st->codec;
        /* generic info */
        put_be32(pb, codec->codec_id);
        put_byte(pb, codec->codec_type);
        put_be32(pb, codec->bit_rate);
        put_be32(pb, st->quality);
        put_be32(pb, codec->flags);
        put_be32(pb, codec->flags2);
        put_be32(pb, codec->debug);
        /* specific info */
        switch(codec->codec_type) {
        case CODEC_TYPE_VIDEO:
            put_be32(pb, codec->time_base.num);
            put_be32(pb, codec->time_base.den);
            put_be16(pb, codec->width);
            put_be16(pb, codec->height);
            put_be16(pb, codec->gop_size);
            put_be32(pb, codec->pix_fmt);
            put_byte(pb, codec->qmin);
            put_byte(pb, codec->qmax);
            put_byte(pb, codec->max_qdiff);
            put_be16(pb, (int) (codec->qcompress * 10000.0));
            put_be16(pb, (int) (codec->qblur * 10000.0));
            put_be32(pb, codec->bit_rate_tolerance);
135
            put_strz(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            put_be32(pb, codec->rc_max_rate);
            put_be32(pb, codec->rc_min_rate);
            put_be32(pb, codec->rc_buffer_size);
            put_be64(pb, av_dbl2int(codec->i_quant_factor));
            put_be64(pb, av_dbl2int(codec->b_quant_factor));
            put_be64(pb, av_dbl2int(codec->i_quant_offset));
            put_be64(pb, av_dbl2int(codec->b_quant_offset));
            put_be32(pb, codec->dct_algo);
            put_be32(pb, codec->strict_std_compliance);
            put_be32(pb, codec->max_b_frames);
            put_be32(pb, codec->luma_elim_threshold);
            put_be32(pb, codec->chroma_elim_threshold);
            put_be32(pb, codec->mpeg_quant);
            put_be32(pb, codec->intra_dc_precision);
            put_be32(pb, codec->me_method);
            put_be32(pb, codec->mb_decision);
            put_be32(pb, codec->nsse_weight);
            put_be32(pb, codec->frame_skip_cmp);
            put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
            put_be32(pb, codec->codec_tag);
156
            put_byte(pb, codec->thread_count);
157 158 159 160 161 162 163 164 165 166 167 168 169
            put_be32(pb, codec->coder_type);
            put_be32(pb, codec->me_cmp);
            put_be32(pb, codec->partitions);
            put_be32(pb, codec->me_subpel_quality);
            put_be32(pb, codec->me_range);
            put_be32(pb, codec->keyint_min);
            put_be32(pb, codec->scenechange_threshold);
            put_be32(pb, codec->b_frame_strategy);
            put_be64(pb, av_dbl2int(codec->qcompress));
            put_be64(pb, av_dbl2int(codec->qblur));
            put_be32(pb, codec->max_qdiff);
            put_be32(pb, codec->refs);
            put_be32(pb, codec->directpred);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
170 171 172 173 174
            break;
        case CODEC_TYPE_AUDIO:
            put_be32(pb, codec->sample_rate);
            put_le16(pb, codec->channels);
            put_le16(pb, codec->frame_size);
175
            put_le16(pb, codec->sample_fmt);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
176 177 178 179
            break;
        default:
            return -1;
        }
180 181 182 183
        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
            put_be32(pb, codec->extradata_size);
            put_buffer(pb, codec->extradata, codec->extradata_size);
        }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
184 185 186 187 188 189 190 191 192 193 194 195 196
    }

    /* flush until end of block reached */
    while ((url_ftell(pb) % ffm->packet_size) != 0)
        put_byte(pb, 0);

    put_flush_packet(pb);

    /* init packet mux */
    ffm->packet_ptr = ffm->packet;
    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
    assert(ffm->packet_end >= ffm->packet);
    ffm->frame_offset = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
197
    ffm->dts = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
198 199 200 201 202 203 204
    ffm->first_packet = 1;

    return 0;
}

static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
{
205
    int64_t dts;
Stefan Gehrer's avatar
Stefan Gehrer committed
206
    uint8_t header[FRAME_HEADER_SIZE+4];
207
    int header_size = FRAME_HEADER_SIZE;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
208

209
    dts = s->timestamp + pkt->dts;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
210 211 212 213 214 215 216
    /* packet size & key_frame */
    header[0] = pkt->stream_index;
    header[1] = 0;
    if (pkt->flags & PKT_FLAG_KEY)
        header[1] |= FLAG_KEY_FRAME;
    AV_WB24(header+2, pkt->size);
    AV_WB24(header+5, pkt->duration);
217
    AV_WB64(header+8, s->timestamp + pkt->pts);
218 219 220 221 222
    if (pkt->pts != pkt->dts) {
        header[1] |= FLAG_DTS;
        AV_WB32(header+16, pkt->pts - pkt->dts);
        header_size += 4;
    }
223 224
    ffm_write_data(s, header, header_size, dts, 1);
    ffm_write_data(s, pkt->data, pkt->size, dts, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

    return 0;
}

static int ffm_write_trailer(AVFormatContext *s)
{
    ByteIOContext *pb = s->pb;
    FFMContext *ffm = s->priv_data;

    /* flush packets */
    if (ffm->packet_ptr > ffm->packet)
        flush_packet(s);

    put_flush_packet(pb);

    return 0;
}

AVOutputFormat ffm_muxer = {
    "ffm",
245
    NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
Baptiste Coudurier's avatar
Baptiste Coudurier committed
246 247 248 249 250 251 252 253 254 255
    "",
    "ffm",
    sizeof(FFMContext),
    /* not really used */
    CODEC_ID_MP2,
    CODEC_ID_MPEG1VIDEO,
    ffm_write_header,
    ffm_write_packet,
    ffm_write_trailer,
};