Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffmenc.c @ 470bce2b

History | View | Annotate | Download (7.45 KB)

1 85a6b01d Baptiste Coudurier
/*
2
 * FFM (ffserver live feed) muxer
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 "avformat.h"
23
#include "ffm.h"
24
25
static void flush_packet(AVFormatContext *s)
26
{
27
    FFMContext *ffm = s->priv_data;
28
    int fill_size, h;
29
    ByteIOContext *pb = s->pb;
30
31
    fill_size = ffm->packet_end - ffm->packet_ptr;
32
    memset(ffm->packet_ptr, 0, fill_size);
33
34
    if (url_ftell(pb) % ffm->packet_size)
35
        av_abort();
36
37
    /* put header */
38
    put_be16(pb, PACKET_ID);
39
    put_be16(pb, fill_size);
40 fabb990e Baptiste Coudurier
    put_be64(pb, ffm->dts);
41 85a6b01d Baptiste Coudurier
    h = ffm->frame_offset;
42
    if (ffm->first_packet)
43
        h |= 0x8000;
44
    put_be16(pb, h);
45
    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
46
    put_flush_packet(pb);
47
48
    /* prepare next packet */
49
    ffm->frame_offset = 0; /* no key frame */
50
    ffm->packet_ptr = ffm->packet;
51
    ffm->first_packet = 0;
52
}
53
54
/* 'first' is true if first data of a frame */
55
static void ffm_write_data(AVFormatContext *s,
56
                           const uint8_t *buf, int size,
57 fabb990e Baptiste Coudurier
                           int64_t dts, int header)
58 85a6b01d Baptiste Coudurier
{
59
    FFMContext *ffm = s->priv_data;
60
    int len;
61
62 54e5cf13 Baptiste Coudurier
    if (header && ffm->frame_offset == 0) {
63 85a6b01d Baptiste Coudurier
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
64 fabb990e Baptiste Coudurier
        ffm->dts = dts;
65 54e5cf13 Baptiste Coudurier
    }
66 85a6b01d Baptiste Coudurier
67
    /* write as many packets as needed */
68
    while (size > 0) {
69
        len = ffm->packet_end - ffm->packet_ptr;
70
        if (len > size)
71
            len = size;
72
        memcpy(ffm->packet_ptr, buf, len);
73
74
        ffm->packet_ptr += len;
75
        buf += len;
76
        size -= len;
77 5e5f75cf Baptiste Coudurier
        if (ffm->packet_ptr >= ffm->packet_end)
78 85a6b01d Baptiste Coudurier
            flush_packet(s);
79
    }
80
}
81
82
static int ffm_write_header(AVFormatContext *s)
83
{
84
    FFMContext *ffm = s->priv_data;
85
    AVStream *st;
86
    ByteIOContext *pb = s->pb;
87
    AVCodecContext *codec;
88
    int bit_rate, i;
89
90
    ffm->packet_size = FFM_PACKET_SIZE;
91
92
    /* header */
93
    put_le32(pb, MKTAG('F', 'F', 'M', '1'));
94
    put_be32(pb, ffm->packet_size);
95
    /* XXX: store write position in other file ? */
96
    put_be64(pb, ffm->packet_size); /* current write position */
97
98
    put_be32(pb, s->nb_streams);
99
    bit_rate = 0;
100
    for(i=0;i<s->nb_streams;i++) {
101
        st = s->streams[i];
102
        bit_rate += st->codec->bit_rate;
103
    }
104
    put_be32(pb, bit_rate);
105
106
    /* list of streams */
107
    for(i=0;i<s->nb_streams;i++) {
108
        st = s->streams[i];
109
        av_set_pts_info(st, 64, 1, 1000000);
110
111
        codec = st->codec;
112
        /* generic info */
113
        put_be32(pb, codec->codec_id);
114
        put_byte(pb, codec->codec_type);
115
        put_be32(pb, codec->bit_rate);
116
        put_be32(pb, st->quality);
117
        put_be32(pb, codec->flags);
118
        put_be32(pb, codec->flags2);
119
        put_be32(pb, codec->debug);
120
        /* specific info */
121
        switch(codec->codec_type) {
122
        case CODEC_TYPE_VIDEO:
123
            put_be32(pb, codec->time_base.num);
124
            put_be32(pb, codec->time_base.den);
125
            put_be16(pb, codec->width);
126
            put_be16(pb, codec->height);
127
            put_be16(pb, codec->gop_size);
128
            put_be32(pb, codec->pix_fmt);
129
            put_byte(pb, codec->qmin);
130
            put_byte(pb, codec->qmax);
131
            put_byte(pb, codec->max_qdiff);
132
            put_be16(pb, (int) (codec->qcompress * 10000.0));
133
            put_be16(pb, (int) (codec->qblur * 10000.0));
134
            put_be32(pb, codec->bit_rate_tolerance);
135 3cffbe09 Michael Niedermayer
            put_strz(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
136 85a6b01d Baptiste Coudurier
            put_be32(pb, codec->rc_max_rate);
137
            put_be32(pb, codec->rc_min_rate);
138
            put_be32(pb, codec->rc_buffer_size);
139
            put_be64(pb, av_dbl2int(codec->i_quant_factor));
140
            put_be64(pb, av_dbl2int(codec->b_quant_factor));
141
            put_be64(pb, av_dbl2int(codec->i_quant_offset));
142
            put_be64(pb, av_dbl2int(codec->b_quant_offset));
143
            put_be32(pb, codec->dct_algo);
144
            put_be32(pb, codec->strict_std_compliance);
145
            put_be32(pb, codec->max_b_frames);
146
            put_be32(pb, codec->luma_elim_threshold);
147
            put_be32(pb, codec->chroma_elim_threshold);
148
            put_be32(pb, codec->mpeg_quant);
149
            put_be32(pb, codec->intra_dc_precision);
150
            put_be32(pb, codec->me_method);
151
            put_be32(pb, codec->mb_decision);
152
            put_be32(pb, codec->nsse_weight);
153
            put_be32(pb, codec->frame_skip_cmp);
154
            put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
155
            put_be32(pb, codec->codec_tag);
156 dbedf2aa Baptiste Coudurier
            put_byte(pb, codec->thread_count);
157 85a6b01d Baptiste Coudurier
            break;
158
        case CODEC_TYPE_AUDIO:
159
            put_be32(pb, codec->sample_rate);
160
            put_le16(pb, codec->channels);
161
            put_le16(pb, codec->frame_size);
162
            break;
163
        default:
164
            return -1;
165
        }
166 7080cbe2 Baptiste Coudurier
        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
167
            put_be32(pb, codec->extradata_size);
168
            put_buffer(pb, codec->extradata, codec->extradata_size);
169
        }
170 85a6b01d Baptiste Coudurier
    }
171
172
    /* flush until end of block reached */
173
    while ((url_ftell(pb) % ffm->packet_size) != 0)
174
        put_byte(pb, 0);
175
176
    put_flush_packet(pb);
177
178
    /* init packet mux */
179
    ffm->packet_ptr = ffm->packet;
180
    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
181
    assert(ffm->packet_end >= ffm->packet);
182
    ffm->frame_offset = 0;
183 fabb990e Baptiste Coudurier
    ffm->dts = 0;
184 85a6b01d Baptiste Coudurier
    ffm->first_packet = 1;
185
186
    return 0;
187
}
188
189
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
190
{
191 148c8d80 Baptiste Coudurier
    int64_t dts;
192 6d083dd7 Stefan Gehrer
    uint8_t header[FRAME_HEADER_SIZE+4];
193 3c6a9f66 Baptiste Coudurier
    int header_size = FRAME_HEADER_SIZE;
194 85a6b01d Baptiste Coudurier
195 148c8d80 Baptiste Coudurier
    dts = s->timestamp + pkt->dts;
196 85a6b01d Baptiste Coudurier
    /* packet size & key_frame */
197
    header[0] = pkt->stream_index;
198
    header[1] = 0;
199
    if (pkt->flags & PKT_FLAG_KEY)
200
        header[1] |= FLAG_KEY_FRAME;
201
    AV_WB24(header+2, pkt->size);
202
    AV_WB24(header+5, pkt->duration);
203 148c8d80 Baptiste Coudurier
    AV_WB64(header+8, s->timestamp + pkt->pts);
204 3c6a9f66 Baptiste Coudurier
    if (pkt->pts != pkt->dts) {
205
        header[1] |= FLAG_DTS;
206
        AV_WB32(header+16, pkt->pts - pkt->dts);
207
        header_size += 4;
208
    }
209 148c8d80 Baptiste Coudurier
    ffm_write_data(s, header, header_size, dts, 1);
210
    ffm_write_data(s, pkt->data, pkt->size, dts, 0);
211 85a6b01d Baptiste Coudurier
212
    return 0;
213
}
214
215
static int ffm_write_trailer(AVFormatContext *s)
216
{
217
    ByteIOContext *pb = s->pb;
218
    FFMContext *ffm = s->priv_data;
219
220
    /* flush packets */
221
    if (ffm->packet_ptr > ffm->packet)
222
        flush_packet(s);
223
224
    put_flush_packet(pb);
225
226
    if (!url_is_streamed(pb)) {
227
        int64_t size;
228
        /* update the write offset */
229
        size = url_ftell(pb);
230
        url_fseek(pb, 8, SEEK_SET);
231
        put_be64(pb, size);
232
        put_flush_packet(pb);
233
    }
234
235
    return 0;
236
}
237
238
AVOutputFormat ffm_muxer = {
239
    "ffm",
240 af274fd1 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
241 85a6b01d Baptiste Coudurier
    "",
242
    "ffm",
243
    sizeof(FFMContext),
244
    /* not really used */
245
    CODEC_ID_MP2,
246
    CODEC_ID_MPEG1VIDEO,
247
    ffm_write_header,
248
    ffm_write_packet,
249
    ffm_write_trailer,
250
};