Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffmenc.c @ 2940b38e

History | View | Annotate | Download (7.43 KB)

1
/*
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
/* disable pts hack for testing */
26
int ffm_nopts = 0;
27

    
28
static void flush_packet(AVFormatContext *s)
29
{
30
    FFMContext *ffm = s->priv_data;
31
    int fill_size, h;
32
    ByteIOContext *pb = s->pb;
33

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

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

    
40
    /* put header */
41
    put_be16(pb, PACKET_ID);
42
    put_be16(pb, fill_size);
43
    put_be64(pb, ffm->pts);
44
    h = ffm->frame_offset;
45
    if (ffm->first_packet)
46
        h |= 0x8000;
47
    put_be16(pb, h);
48
    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
49
    put_flush_packet(pb);
50

    
51
    /* prepare next packet */
52
    ffm->frame_offset = 0; /* no key frame */
53
    ffm->pts = 0; /* no pts */
54
    ffm->packet_ptr = ffm->packet;
55
    ffm->first_packet = 0;
56
}
57

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

    
66
    if (header && ffm->frame_offset == 0)
67
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
68
    if (header && ffm->pts == 0)
69
        ffm->pts = pts;
70

    
71
    /* write as many packets as needed */
72
    while (size > 0) {
73
        len = ffm->packet_end - ffm->packet_ptr;
74
        if (len > size)
75
            len = size;
76
        memcpy(ffm->packet_ptr, buf, len);
77

    
78
        ffm->packet_ptr += len;
79
        buf += len;
80
        size -= len;
81
        if (ffm->packet_ptr >= ffm->packet_end) {
82
            /* special case : no pts in packet : we leave the current one */
83
            if (ffm->pts == 0)
84
                ffm->pts = pts;
85

    
86
            flush_packet(s);
87
        }
88
    }
89
}
90

    
91
static int ffm_write_header(AVFormatContext *s)
92
{
93
    FFMContext *ffm = s->priv_data;
94
    AVStream *st;
95
    ByteIOContext *pb = s->pb;
96
    AVCodecContext *codec;
97
    int bit_rate, i;
98

    
99
    ffm->packet_size = FFM_PACKET_SIZE;
100

    
101
    /* header */
102
    put_le32(pb, MKTAG('F', 'F', 'M', '1'));
103
    put_be32(pb, ffm->packet_size);
104
    /* XXX: store write position in other file ? */
105
    put_be64(pb, ffm->packet_size); /* current write position */
106

    
107
    put_be32(pb, s->nb_streams);
108
    bit_rate = 0;
109
    for(i=0;i<s->nb_streams;i++) {
110
        st = s->streams[i];
111
        bit_rate += st->codec->bit_rate;
112
    }
113
    put_be32(pb, bit_rate);
114

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

    
120
        codec = st->codec;
121
        /* generic info */
122
        put_be32(pb, codec->codec_id);
123
        put_byte(pb, codec->codec_type);
124
        put_be32(pb, codec->bit_rate);
125
        put_be32(pb, st->quality);
126
        put_be32(pb, codec->flags);
127
        put_be32(pb, codec->flags2);
128
        put_be32(pb, codec->debug);
129
        /* specific info */
130
        switch(codec->codec_type) {
131
        case CODEC_TYPE_VIDEO:
132
            put_be32(pb, codec->time_base.num);
133
            put_be32(pb, codec->time_base.den);
134
            put_be16(pb, codec->width);
135
            put_be16(pb, codec->height);
136
            put_be16(pb, codec->gop_size);
137
            put_be32(pb, codec->pix_fmt);
138
            put_byte(pb, codec->qmin);
139
            put_byte(pb, codec->qmax);
140
            put_byte(pb, codec->max_qdiff);
141
            put_be16(pb, (int) (codec->qcompress * 10000.0));
142
            put_be16(pb, (int) (codec->qblur * 10000.0));
143
            put_be32(pb, codec->bit_rate_tolerance);
144
            put_strz(pb, codec->rc_eq);
145
            put_be32(pb, codec->rc_max_rate);
146
            put_be32(pb, codec->rc_min_rate);
147
            put_be32(pb, codec->rc_buffer_size);
148
            put_be64(pb, av_dbl2int(codec->i_quant_factor));
149
            put_be64(pb, av_dbl2int(codec->b_quant_factor));
150
            put_be64(pb, av_dbl2int(codec->i_quant_offset));
151
            put_be64(pb, av_dbl2int(codec->b_quant_offset));
152
            put_be32(pb, codec->dct_algo);
153
            put_be32(pb, codec->strict_std_compliance);
154
            put_be32(pb, codec->max_b_frames);
155
            put_be32(pb, codec->luma_elim_threshold);
156
            put_be32(pb, codec->chroma_elim_threshold);
157
            put_be32(pb, codec->mpeg_quant);
158
            put_be32(pb, codec->intra_dc_precision);
159
            put_be32(pb, codec->me_method);
160
            put_be32(pb, codec->mb_decision);
161
            put_be32(pb, codec->nsse_weight);
162
            put_be32(pb, codec->frame_skip_cmp);
163
            put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
164
            put_be32(pb, codec->codec_tag);
165
            break;
166
        case CODEC_TYPE_AUDIO:
167
            put_be32(pb, codec->sample_rate);
168
            put_le16(pb, codec->channels);
169
            put_le16(pb, codec->frame_size);
170
            break;
171
        default:
172
            return -1;
173
        }
174
    }
175

    
176
    /* hack to have real time */
177
    if (ffm_nopts)
178
        ffm->start_time = 0;
179
    else
180
        ffm->start_time = av_gettime();
181

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

    
186
    put_flush_packet(pb);
187

    
188
    /* init packet mux */
189
    ffm->packet_ptr = ffm->packet;
190
    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
191
    assert(ffm->packet_end >= ffm->packet);
192
    ffm->frame_offset = 0;
193
    ffm->pts = 0;
194
    ffm->first_packet = 1;
195

    
196
    return 0;
197
}
198

    
199
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
200
{
201
    FFMContext *ffm = s->priv_data;
202
    AVStream *st = s->streams[pkt->stream_index];
203
    int64_t pts;
204
    uint8_t header[FRAME_HEADER_SIZE];
205

    
206
    pts = ffm->start_time + pkt->pts;
207
    /* packet size & key_frame */
208
    header[0] = pkt->stream_index;
209
    header[1] = 0;
210
    if (pkt->flags & PKT_FLAG_KEY)
211
        header[1] |= FLAG_KEY_FRAME;
212
    AV_WB24(header+2, pkt->size);
213
    AV_WB24(header+5, pkt->duration);
214
    ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
215
    ffm_write_data(s, pkt->data, pkt->size, pts, 0);
216

    
217
    return 0;
218
}
219

    
220
static int ffm_write_trailer(AVFormatContext *s)
221
{
222
    ByteIOContext *pb = s->pb;
223
    FFMContext *ffm = s->priv_data;
224

    
225
    /* flush packets */
226
    if (ffm->packet_ptr > ffm->packet)
227
        flush_packet(s);
228

    
229
    put_flush_packet(pb);
230

    
231
    if (!url_is_streamed(pb)) {
232
        int64_t size;
233
        /* update the write offset */
234
        size = url_ftell(pb);
235
        url_fseek(pb, 8, SEEK_SET);
236
        put_be64(pb, size);
237
        put_flush_packet(pb);
238
    }
239

    
240
    return 0;
241
}
242

    
243
AVOutputFormat ffm_muxer = {
244
    "ffm",
245
    NULL_IF_CONFIG_SMALL("ffm format"),
246
    "",
247
    "ffm",
248
    sizeof(FFMContext),
249
    /* not really used */
250
    CODEC_ID_MP2,
251
    CODEC_ID_MPEG1VIDEO,
252
    ffm_write_header,
253
    ffm_write_packet,
254
    ffm_write_trailer,
255
};