Statistics
| Branch: | Revision:

ffmpeg / libavformat / oggenc.c @ 012dc2c4

History | View | Annotate | Download (11.9 KB)

1 5118cae6 Baptiste Coudurier
/*
2
 * Ogg muxer
3
 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
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 245976da Diego Biurrun
#include "libavutil/crc.h"
23
#include "libavcodec/xiph.h"
24
#include "libavcodec/bytestream.h"
25 59c6178a Justin Ruggles
#include "libavcodec/flac.h"
26 5118cae6 Baptiste Coudurier
#include "avformat.h"
27 1cc65cec Baptiste Coudurier
#include "internal.h"
28 5118cae6 Baptiste Coudurier
29
typedef struct {
30
    int64_t duration;
31
    unsigned page_counter;
32
    uint8_t *header[3];
33
    int header_len[3];
34
    /** for theora granule */
35
    int kfgshift;
36
    int64_t last_kf_pts;
37
    int vrev;
38 37b74c86 Baptiste Coudurier
    int eos;
39 012dc2c4 Baptiste Coudurier
    unsigned packet_count; ///< number of packet buffered
40 5118cae6 Baptiste Coudurier
} OGGStreamContext;
41
42 bc5c918e Diego Biurrun
static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset)
43 5118cae6 Baptiste Coudurier
{
44 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(s->pb);
45 899681cd Björn Axelsson
    uint32_t checksum = get_checksum(s->pb);
46
    url_fseek(s->pb, crc_offset, SEEK_SET);
47
    put_be32(s->pb, checksum);
48
    url_fseek(s->pb, pos, SEEK_SET);
49 5118cae6 Baptiste Coudurier
}
50
51
static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size,
52
                          int64_t granule, int stream_index, int flags)
53
{
54
    OGGStreamContext *oggstream = s->streams[stream_index]->priv_data;
55 bc5c918e Diego Biurrun
    int64_t crc_offset;
56 5118cae6 Baptiste Coudurier
    int page_segments, i;
57
58 2dd3c9e0 Baptiste Coudurier
    if (size >= 255*255) {
59
        granule = -1;
60
        size = 255*255;
61 37b74c86 Baptiste Coudurier
    } else if (oggstream->eos)
62
        flags |= 4;
63 2dd3c9e0 Baptiste Coudurier
64 5118cae6 Baptiste Coudurier
    page_segments = FFMIN((size/255)+!!size, 255);
65
66 899681cd Björn Axelsson
    init_checksum(s->pb, ff_crc04C11DB7_update, 0);
67
    put_tag(s->pb, "OggS");
68
    put_byte(s->pb, 0);
69
    put_byte(s->pb, flags);
70
    put_le64(s->pb, granule);
71
    put_le32(s->pb, stream_index);
72
    put_le32(s->pb, oggstream->page_counter++);
73
    crc_offset = url_ftell(s->pb);
74
    put_le32(s->pb, 0); // crc
75
    put_byte(s->pb, page_segments);
76 5118cae6 Baptiste Coudurier
    for (i = 0; i < page_segments-1; i++)
77 899681cd Björn Axelsson
        put_byte(s->pb, 255);
78 5118cae6 Baptiste Coudurier
    if (size) {
79 899681cd Björn Axelsson
        put_byte(s->pb, size - (page_segments-1)*255);
80
        put_buffer(s->pb, data, size);
81 5118cae6 Baptiste Coudurier
    }
82
    ogg_update_checksum(s, crc_offset);
83 899681cd Björn Axelsson
    put_flush_packet(s->pb);
84 5118cae6 Baptiste Coudurier
    return size;
85
}
86
87 87462968 Justin Ruggles
static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
88
                                        int *header_len)
89
{
90
    const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
91
    int size;
92
    uint8_t *p, *p0;
93
94
    size = offset + 4 + strlen(vendor) + 4;
95
    p = av_mallocz(size);
96
    if (!p)
97
        return NULL;
98
    p0 = p;
99
100
    p += offset;
101
    bytestream_put_le32(&p, strlen(vendor));
102
    bytestream_put_buffer(&p, vendor, strlen(vendor));
103
    bytestream_put_le32(&p, 0); // user comment list length
104
105
    *header_len = size;
106
    return p0;
107
}
108
109 59c6178a Justin Ruggles
static int ogg_build_flac_headers(AVCodecContext *avctx,
110 5118cae6 Baptiste Coudurier
                                  OGGStreamContext *oggstream, int bitexact)
111
{
112 59c6178a Justin Ruggles
    enum FLACExtradataFormat format;
113
    uint8_t *streaminfo;
114 5118cae6 Baptiste Coudurier
    uint8_t *p;
115 4315aa8b Justin Ruggles
116 59c6178a Justin Ruggles
    if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
117 5118cae6 Baptiste Coudurier
        return -1;
118 4315aa8b Justin Ruggles
119
    // first packet: STREAMINFO
120 6e330816 Justin Ruggles
    oggstream->header_len[0] = 51;
121
    oggstream->header[0] = av_mallocz(51); // per ogg flac specs
122 5118cae6 Baptiste Coudurier
    p = oggstream->header[0];
123 e9a32230 Justin Ruggles
    if (!p)
124
        return AVERROR_NOMEM;
125 5118cae6 Baptiste Coudurier
    bytestream_put_byte(&p, 0x7F);
126
    bytestream_put_buffer(&p, "FLAC", 4);
127
    bytestream_put_byte(&p, 1); // major version
128
    bytestream_put_byte(&p, 0); // minor version
129
    bytestream_put_be16(&p, 1); // headers packets without this one
130
    bytestream_put_buffer(&p, "fLaC", 4);
131
    bytestream_put_byte(&p, 0x00); // streaminfo
132
    bytestream_put_be24(&p, 34);
133 59c6178a Justin Ruggles
    bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
134 4315aa8b Justin Ruggles
135
    // second packet: VorbisComment
136 87462968 Justin Ruggles
    p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1]);
137 e9a32230 Justin Ruggles
    if (!p)
138
        return AVERROR_NOMEM;
139 87462968 Justin Ruggles
    oggstream->header[1] = p;
140 5118cae6 Baptiste Coudurier
    bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
141
    bytestream_put_be24(&p, oggstream->header_len[1] - 4);
142 87462968 Justin Ruggles
143
    return 0;
144
}
145
146
#define SPEEX_HEADER_SIZE 80
147
148
static int ogg_build_speex_headers(AVCodecContext *avctx,
149
                                   OGGStreamContext *oggstream, int bitexact)
150
{
151
    uint8_t *p;
152
153
    if (avctx->extradata_size < SPEEX_HEADER_SIZE)
154
        return -1;
155
156
    // first packet: Speex header
157
    p = av_mallocz(SPEEX_HEADER_SIZE);
158
    if (!p)
159
        return AVERROR_NOMEM;
160
    oggstream->header[0] = p;
161
    oggstream->header_len[0] = SPEEX_HEADER_SIZE;
162
    bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
163
    AV_WL32(&oggstream->header[0][68], 0);  // set extra_headers to 0
164
165
    // second packet: VorbisComment
166
    p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1]);
167
    if (!p)
168
        return AVERROR_NOMEM;
169
    oggstream->header[1] = p;
170 4315aa8b Justin Ruggles
171 5118cae6 Baptiste Coudurier
    return 0;
172
}
173
174
static int ogg_write_header(AVFormatContext *s)
175
{
176
    OGGStreamContext *oggstream;
177
    int i, j;
178
    for (i = 0; i < s->nb_streams; i++) {
179
        AVStream *st = s->streams[i];
180
        if (st->codec->codec_type == CODEC_TYPE_AUDIO)
181
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
182
        else if (st->codec->codec_type == CODEC_TYPE_VIDEO)
183
            av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
184
        if (st->codec->codec_id != CODEC_ID_VORBIS &&
185
            st->codec->codec_id != CODEC_ID_THEORA &&
186 87462968 Justin Ruggles
            st->codec->codec_id != CODEC_ID_SPEEX  &&
187 5118cae6 Baptiste Coudurier
            st->codec->codec_id != CODEC_ID_FLAC) {
188
            av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
189
            return -1;
190
        }
191
192
        if (!st->codec->extradata || !st->codec->extradata_size) {
193
            av_log(s, AV_LOG_ERROR, "No extradata present\n");
194
            return -1;
195
        }
196
        oggstream = av_mallocz(sizeof(*oggstream));
197
        st->priv_data = oggstream;
198
        if (st->codec->codec_id == CODEC_ID_FLAC) {
199 3f2eadf4 Justin Ruggles
            int err = ogg_build_flac_headers(st->codec, oggstream,
200
                                             st->codec->flags & CODEC_FLAG_BITEXACT);
201
            if (err) {
202 87b4dd3b Justin Ruggles
                av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
203 5118cae6 Baptiste Coudurier
                av_freep(&st->priv_data);
204 3f2eadf4 Justin Ruggles
                return err;
205 5118cae6 Baptiste Coudurier
            }
206 87462968 Justin Ruggles
        } else if (st->codec->codec_id == CODEC_ID_SPEEX) {
207
            int err = ogg_build_speex_headers(st->codec, oggstream,
208
                                              st->codec->flags & CODEC_FLAG_BITEXACT);
209
            if (err) {
210
                av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
211
                av_freep(&st->priv_data);
212
                return err;
213
            }
214 5118cae6 Baptiste Coudurier
        } else {
215
            if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
216
                                      st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
217
                                      oggstream->header, oggstream->header_len) < 0) {
218
                av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
219
                av_freep(&st->priv_data);
220
                return -1;
221
            }
222
            if (st->codec->codec_id == CODEC_ID_THEORA) {
223
                /** KFGSHIFT is the width of the less significant section of the granule position
224
                    The less significant section is the frame count since the last keyframe */
225
                oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
226
                oggstream->vrev = oggstream->header[0][9];
227
                av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
228
                       oggstream->kfgshift, oggstream->vrev);
229
            }
230
        }
231
    }
232
    for (i = 0; i < 3; i++) {
233
        for (j = 0; j < s->nb_streams; j++) {
234
            AVStream *st = s->streams[j];
235
            OGGStreamContext *oggstream = st->priv_data;
236
            if (oggstream && oggstream->header_len[i]) {
237
                ogg_write_page(s, oggstream->header[i], oggstream->header_len[i],
238
                               0, st->index, i ? 0 : 2); // bos
239
            }
240
        }
241
    }
242
    return 0;
243
}
244
245
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
246
{
247
    AVStream *st = s->streams[pkt->stream_index];
248
    OGGStreamContext *oggstream = st->priv_data;
249
    uint8_t *ptr = pkt->data;
250
    int ret, size = pkt->size;
251
    int64_t granule;
252
253
    if (st->codec->codec_id == CODEC_ID_THEORA) {
254
        int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
255
        int pframe_count;
256
        if (pkt->flags & PKT_FLAG_KEY)
257
            oggstream->last_kf_pts = pts;
258
        pframe_count = pts - oggstream->last_kf_pts;
259
        // prevent frame count from overflow if key frame flag is not set
260
        if (pframe_count >= (1<<oggstream->kfgshift)) {
261
            oggstream->last_kf_pts += pframe_count;
262
            pframe_count = 0;
263
        }
264
        granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
265
    } else
266
        granule = pkt->pts + pkt->duration;
267
    oggstream->duration = granule;
268
    do {
269
        ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data);
270
        ptr  += ret; size -= ret;
271
    } while (size > 0 || ret == 255*255); // need to output a last nil page
272
273
    return 0;
274
}
275
276 fd5583bc Baptiste Coudurier
static int ogg_compare_granule(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
277
{
278
    AVStream *st2 = s->streams[next->stream_index];
279
    AVStream *st  = s->streams[pkt ->stream_index];
280
281
    int64_t next_granule = av_rescale_q(next->pts + next->duration,
282
                                        st2->time_base, AV_TIME_BASE_Q);
283
    int64_t cur_granule  = av_rescale_q(pkt ->pts + pkt ->duration,
284
                                        st ->time_base, AV_TIME_BASE_Q);
285
    return next_granule > cur_granule;
286
}
287
288 34c65ac6 Baptiste Coudurier
static int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
289 37b74c86 Baptiste Coudurier
{
290 012dc2c4 Baptiste Coudurier
    OGGStreamContext *ogg;
291
    int i, stream_count = 0;
292 37b74c86 Baptiste Coudurier
    int interleaved = 0;
293
294
    if (pkt) {
295 fd5583bc Baptiste Coudurier
        ff_interleave_add_packet(s, pkt, ogg_compare_granule);
296 012dc2c4 Baptiste Coudurier
        ogg = s->streams[pkt->stream_index]->priv_data;
297
        ogg->packet_count++;
298 37b74c86 Baptiste Coudurier
    }
299
300 012dc2c4 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
301
        ogg = s->streams[i]->priv_data;
302
        stream_count += !!ogg->packet_count;
303
        interleaved += ogg->packet_count > 1;
304 37b74c86 Baptiste Coudurier
    }
305
306
    if ((s->nb_streams == stream_count && interleaved == stream_count) ||
307
        (flush && stream_count)) {
308 012dc2c4 Baptiste Coudurier
        AVPacketList *pktl= s->packet_buffer;
309 37b74c86 Baptiste Coudurier
        *out= pktl->pkt;
310
        s->packet_buffer = pktl->next;
311 012dc2c4 Baptiste Coudurier
312
        ogg = s->streams[out->stream_index]->priv_data;
313
        if (flush && ogg->packet_count == 1)
314 37b74c86 Baptiste Coudurier
            ogg->eos = 1;
315 012dc2c4 Baptiste Coudurier
        ogg->packet_count--;
316
317 e07b882b Michael Niedermayer
        if(!s->packet_buffer)
318
            s->packet_buffer_end= NULL;
319
320
        if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
321
            s->streams[out->stream_index]->last_in_packet_buffer= NULL;
322
323 37b74c86 Baptiste Coudurier
        av_freep(&pktl);
324
        return 1;
325
    } else {
326
        av_init_packet(out);
327
        return 0;
328
    }
329
}
330 5118cae6 Baptiste Coudurier
331
static int ogg_write_trailer(AVFormatContext *s)
332
{
333
    int i;
334
    for (i = 0; i < s->nb_streams; i++) {
335
        AVStream *st = s->streams[i];
336
        OGGStreamContext *oggstream = st->priv_data;
337 87462968 Justin Ruggles
        if (st->codec->codec_id == CODEC_ID_FLAC ||
338
            st->codec->codec_id == CODEC_ID_SPEEX) {
339 5118cae6 Baptiste Coudurier
            av_free(oggstream->header[0]);
340
            av_free(oggstream->header[1]);
341
        }
342
        av_freep(&st->priv_data);
343
    }
344
    return 0;
345
}
346
347
AVOutputFormat ogg_muxer = {
348
    "ogg",
349 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Ogg"),
350 5118cae6 Baptiste Coudurier
    "application/ogg",
351 87462968 Justin Ruggles
    "ogg,ogv,spx",
352 5118cae6 Baptiste Coudurier
    0,
353
    CODEC_ID_FLAC,
354
    CODEC_ID_THEORA,
355
    ogg_write_header,
356
    ogg_write_packet,
357
    ogg_write_trailer,
358 37b74c86 Baptiste Coudurier
    .interleave_packet = ogg_interleave_per_granule,
359 5118cae6 Baptiste Coudurier
};