Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffmenc.c @ 2912e87a

History | View | Annotate | Download (7.89 KB)

1
/*
2
 * FFM (ffserver live feed) muxer
3
 * Copyright (c) 2001 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/intreadwrite.h"
23
#include "avformat.h"
24
#include "ffm.h"
25

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

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

    
35
    if (avio_tell(pb) % ffm->packet_size)
36
        av_abort();
37

    
38
    /* put header */
39
    avio_wb16(pb, PACKET_ID);
40
    avio_wb16(pb, fill_size);
41
    avio_wb64(pb, ffm->dts);
42
    h = ffm->frame_offset;
43
    if (ffm->first_packet)
44
        h |= 0x8000;
45
    avio_wb16(pb, h);
46
    avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
47
    avio_flush(pb);
48

    
49
    /* prepare next packet */
50
    ffm->frame_offset = 0; /* no key frame */
51
    ffm->packet_ptr = ffm->packet;
52
    ffm->first_packet = 0;
53
}
54

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

    
63
    if (header && ffm->frame_offset == 0) {
64
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
65
        ffm->dts = dts;
66
    }
67

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

    
75
        ffm->packet_ptr += len;
76
        buf += len;
77
        size -= len;
78
        if (ffm->packet_ptr >= ffm->packet_end)
79
            flush_packet(s);
80
    }
81
}
82

    
83
static int ffm_write_header(AVFormatContext *s)
84
{
85
    FFMContext *ffm = s->priv_data;
86
    AVStream *st;
87
    AVIOContext *pb = s->pb;
88
    AVCodecContext *codec;
89
    int bit_rate, i;
90

    
91
    ffm->packet_size = FFM_PACKET_SIZE;
92

    
93
    /* header */
94
    avio_wl32(pb, MKTAG('F', 'F', 'M', '1'));
95
    avio_wb32(pb, ffm->packet_size);
96
    avio_wb64(pb, 0); /* current write position */
97

    
98
    avio_wb32(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
    avio_wb32(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
        avio_wb32(pb, codec->codec_id);
114
        avio_w8(pb, codec->codec_type);
115
        avio_wb32(pb, codec->bit_rate);
116
        avio_wb32(pb, st->quality);
117
        avio_wb32(pb, codec->flags);
118
        avio_wb32(pb, codec->flags2);
119
        avio_wb32(pb, codec->debug);
120
        /* specific info */
121
        switch(codec->codec_type) {
122
        case AVMEDIA_TYPE_VIDEO:
123
            avio_wb32(pb, codec->time_base.num);
124
            avio_wb32(pb, codec->time_base.den);
125
            avio_wb16(pb, codec->width);
126
            avio_wb16(pb, codec->height);
127
            avio_wb16(pb, codec->gop_size);
128
            avio_wb32(pb, codec->pix_fmt);
129
            avio_w8(pb, codec->qmin);
130
            avio_w8(pb, codec->qmax);
131
            avio_w8(pb, codec->max_qdiff);
132
            avio_wb16(pb, (int) (codec->qcompress * 10000.0));
133
            avio_wb16(pb, (int) (codec->qblur * 10000.0));
134
            avio_wb32(pb, codec->bit_rate_tolerance);
135
            avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
136
            avio_wb32(pb, codec->rc_max_rate);
137
            avio_wb32(pb, codec->rc_min_rate);
138
            avio_wb32(pb, codec->rc_buffer_size);
139
            avio_wb64(pb, av_dbl2int(codec->i_quant_factor));
140
            avio_wb64(pb, av_dbl2int(codec->b_quant_factor));
141
            avio_wb64(pb, av_dbl2int(codec->i_quant_offset));
142
            avio_wb64(pb, av_dbl2int(codec->b_quant_offset));
143
            avio_wb32(pb, codec->dct_algo);
144
            avio_wb32(pb, codec->strict_std_compliance);
145
            avio_wb32(pb, codec->max_b_frames);
146
            avio_wb32(pb, codec->luma_elim_threshold);
147
            avio_wb32(pb, codec->chroma_elim_threshold);
148
            avio_wb32(pb, codec->mpeg_quant);
149
            avio_wb32(pb, codec->intra_dc_precision);
150
            avio_wb32(pb, codec->me_method);
151
            avio_wb32(pb, codec->mb_decision);
152
            avio_wb32(pb, codec->nsse_weight);
153
            avio_wb32(pb, codec->frame_skip_cmp);
154
            avio_wb64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
155
            avio_wb32(pb, codec->codec_tag);
156
            avio_w8(pb, codec->thread_count);
157
            avio_wb32(pb, codec->coder_type);
158
            avio_wb32(pb, codec->me_cmp);
159
            avio_wb32(pb, codec->partitions);
160
            avio_wb32(pb, codec->me_subpel_quality);
161
            avio_wb32(pb, codec->me_range);
162
            avio_wb32(pb, codec->keyint_min);
163
            avio_wb32(pb, codec->scenechange_threshold);
164
            avio_wb32(pb, codec->b_frame_strategy);
165
            avio_wb64(pb, av_dbl2int(codec->qcompress));
166
            avio_wb64(pb, av_dbl2int(codec->qblur));
167
            avio_wb32(pb, codec->max_qdiff);
168
            avio_wb32(pb, codec->refs);
169
            avio_wb32(pb, codec->directpred);
170
            break;
171
        case AVMEDIA_TYPE_AUDIO:
172
            avio_wb32(pb, codec->sample_rate);
173
            avio_wl16(pb, codec->channels);
174
            avio_wl16(pb, codec->frame_size);
175
            avio_wl16(pb, codec->sample_fmt);
176
            break;
177
        default:
178
            return -1;
179
        }
180
        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
181
            avio_wb32(pb, codec->extradata_size);
182
            avio_write(pb, codec->extradata, codec->extradata_size);
183
        }
184
    }
185

    
186
    /* flush until end of block reached */
187
    while ((avio_tell(pb) % ffm->packet_size) != 0)
188
        avio_w8(pb, 0);
189

    
190
    avio_flush(pb);
191

    
192
    /* init packet mux */
193
    ffm->packet_ptr = ffm->packet;
194
    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
195
    assert(ffm->packet_end >= ffm->packet);
196
    ffm->frame_offset = 0;
197
    ffm->dts = 0;
198
    ffm->first_packet = 1;
199

    
200
    return 0;
201
}
202

    
203
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
204
{
205
    int64_t dts;
206
    uint8_t header[FRAME_HEADER_SIZE+4];
207
    int header_size = FRAME_HEADER_SIZE;
208

    
209
    dts = s->timestamp + pkt->dts;
210
    /* packet size & key_frame */
211
    header[0] = pkt->stream_index;
212
    header[1] = 0;
213
    if (pkt->flags & AV_PKT_FLAG_KEY)
214
        header[1] |= FLAG_KEY_FRAME;
215
    AV_WB24(header+2, pkt->size);
216
    AV_WB24(header+5, pkt->duration);
217
    AV_WB64(header+8, s->timestamp + pkt->pts);
218
    if (pkt->pts != pkt->dts) {
219
        header[1] |= FLAG_DTS;
220
        AV_WB32(header+16, pkt->pts - pkt->dts);
221
        header_size += 4;
222
    }
223
    ffm_write_data(s, header, header_size, dts, 1);
224
    ffm_write_data(s, pkt->data, pkt->size, dts, 0);
225

    
226
    return 0;
227
}
228

    
229
static int ffm_write_trailer(AVFormatContext *s)
230
{
231
    AVIOContext *pb = s->pb;
232
    FFMContext *ffm = s->priv_data;
233

    
234
    /* flush packets */
235
    if (ffm->packet_ptr > ffm->packet)
236
        flush_packet(s);
237

    
238
    avio_flush(pb);
239

    
240
    return 0;
241
}
242

    
243
AVOutputFormat ff_ffm_muxer = {
244
    "ffm",
245
    NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) 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
};