Statistics
| Branch: | Revision:

ffmpeg / libavformat / oggenc.c @ f59d8ff8

History | View | Annotate | Download (16.2 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 ce2a9bc3 Baptiste Coudurier
#include "libavutil/random_seed.h"
24 245976da Diego Biurrun
#include "libavcodec/xiph.h"
25
#include "libavcodec/bytestream.h"
26 59c6178a Justin Ruggles
#include "libavcodec/flac.h"
27 5118cae6 Baptiste Coudurier
#include "avformat.h"
28 32442930 Anton Khirnov
#include "avio_internal.h"
29 1cc65cec Baptiste Coudurier
#include "internal.h"
30 aeef3ec6 James Darnley
#include "vorbiscomment.h"
31 5118cae6 Baptiste Coudurier
32 95ca3b1e Baptiste Coudurier
#define MAX_PAGE_SIZE 65025
33
34
typedef struct {
35
    int64_t granule;
36
    int stream_index;
37
    uint8_t flags;
38
    uint8_t segments_count;
39
    uint8_t segments[255];
40
    uint8_t data[MAX_PAGE_SIZE];
41
    uint16_t size;
42
} OGGPage;
43
44 5118cae6 Baptiste Coudurier
typedef struct {
45
    unsigned page_counter;
46
    uint8_t *header[3];
47
    int header_len[3];
48
    /** for theora granule */
49
    int kfgshift;
50
    int64_t last_kf_pts;
51
    int vrev;
52 37b74c86 Baptiste Coudurier
    int eos;
53 95ca3b1e Baptiste Coudurier
    unsigned page_count; ///< number of page buffered
54
    OGGPage page; ///< current page
55 ce2a9bc3 Baptiste Coudurier
    unsigned serial_num; ///< serial number
56 ab043374 Baptiste Coudurier
    int64_t last_granule; ///< last packet granule
57 5118cae6 Baptiste Coudurier
} OGGStreamContext;
58
59 95ca3b1e Baptiste Coudurier
typedef struct OGGPageList {
60
    OGGPage page;
61
    struct OGGPageList *next;
62
} OGGPageList;
63
64
typedef struct {
65
    OGGPageList *page_list;
66
} OGGContext;
67
68 471fe57e Anton Khirnov
static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
69 5118cae6 Baptiste Coudurier
{
70 22fe18c1 Baptiste Coudurier
    int64_t pos = url_ftell(pb);
71
    uint32_t checksum = get_checksum(pb);
72 f59d8ff8 Anton Khirnov
    avio_seek(pb, crc_offset, SEEK_SET);
73 e9eb8d0b Anton Khirnov
    avio_wb32(pb, checksum);
74 f59d8ff8 Anton Khirnov
    avio_seek(pb, pos, SEEK_SET);
75 5118cae6 Baptiste Coudurier
}
76
77 22fe18c1 Baptiste Coudurier
static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
78 5118cae6 Baptiste Coudurier
{
79 95ca3b1e Baptiste Coudurier
    OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
80 471fe57e Anton Khirnov
    AVIOContext *pb;
81 bc5c918e Diego Biurrun
    int64_t crc_offset;
82 22fe18c1 Baptiste Coudurier
    int ret, size;
83
    uint8_t *buf;
84 5118cae6 Baptiste Coudurier
85 22fe18c1 Baptiste Coudurier
    ret = url_open_dyn_buf(&pb);
86
    if (ret < 0)
87
        return ret;
88
    init_checksum(pb, ff_crc04C11DB7_update, 0);
89 32442930 Anton Khirnov
    ffio_wfourcc(pb, "OggS");
90 e9eb8d0b Anton Khirnov
    avio_w8(pb, 0);
91
    avio_w8(pb, page->flags | extra_flags);
92
    avio_wl64(pb, page->granule);
93
    avio_wl32(pb, oggstream->serial_num);
94
    avio_wl32(pb, oggstream->page_counter++);
95 22fe18c1 Baptiste Coudurier
    crc_offset = url_ftell(pb);
96 e9eb8d0b Anton Khirnov
    avio_wl32(pb, 0); // crc
97
    avio_w8(pb, page->segments_count);
98
    avio_write(pb, page->segments, page->segments_count);
99
    avio_write(pb, page->data, page->size);
100 22fe18c1 Baptiste Coudurier
101
    ogg_update_checksum(s, pb, crc_offset);
102
    put_flush_packet(pb);
103
104
    size = url_close_dyn_buf(pb, &buf);
105
    if (size < 0)
106
        return size;
107
108 e9eb8d0b Anton Khirnov
    avio_write(s->pb, buf, size);
109 899681cd Björn Axelsson
    put_flush_packet(s->pb);
110 5c327555 Baptiste Coudurier
    av_free(buf);
111 95ca3b1e Baptiste Coudurier
    oggstream->page_count--;
112 22fe18c1 Baptiste Coudurier
    return 0;
113 95ca3b1e Baptiste Coudurier
}
114
115 ab043374 Baptiste Coudurier
static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
116 95ca3b1e Baptiste Coudurier
{
117
    if (oggstream->kfgshift)
118 ab043374 Baptiste Coudurier
        return (granule>>oggstream->kfgshift) +
119
            (granule & ((1<<oggstream->kfgshift)-1));
120 95ca3b1e Baptiste Coudurier
    else
121 ab043374 Baptiste Coudurier
        return granule;
122 95ca3b1e Baptiste Coudurier
}
123
124
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
125
{
126
    AVStream *st2 = s->streams[next->stream_index];
127
    AVStream *st  = s->streams[page->stream_index];
128
    int64_t next_granule, cur_granule;
129
130
    if (next->granule == -1 || page->granule == -1)
131
        return 0;
132
133 ab043374 Baptiste Coudurier
    next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
134 95ca3b1e Baptiste Coudurier
                                st2->time_base, AV_TIME_BASE_Q);
135 ab043374 Baptiste Coudurier
    cur_granule  = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
136 95ca3b1e Baptiste Coudurier
                                st ->time_base, AV_TIME_BASE_Q);
137
    return next_granule > cur_granule;
138
}
139
140
static int ogg_reset_cur_page(OGGStreamContext *oggstream)
141
{
142
    oggstream->page.granule = -1;
143
    oggstream->page.flags = 0;
144
    oggstream->page.segments_count = 0;
145
    oggstream->page.size = 0;
146
    return 0;
147
}
148
149
static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
150
{
151
    OGGContext *ogg = s->priv_data;
152
    OGGPageList **p = &ogg->page_list;
153
    OGGPageList *l = av_mallocz(sizeof(*l));
154
155
    if (!l)
156
        return AVERROR(ENOMEM);
157
    l->page = oggstream->page;
158
159
    oggstream->page_count++;
160
    ogg_reset_cur_page(oggstream);
161
162
    while (*p) {
163
        if (ogg_compare_granule(s, &(*p)->page, &l->page))
164
            break;
165
        p = &(*p)->next;
166
    }
167
    l->next = *p;
168
    *p = l;
169
170
    return 0;
171
}
172
173
static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
174
                           uint8_t *data, unsigned size, int64_t granule)
175
{
176
    OGGStreamContext *oggstream = st->priv_data;
177
    int total_segments = size / 255 + 1;
178
    uint8_t *p = data;
179 ab043374 Baptiste Coudurier
    int i, segments, len, flush = 0;
180
181
    // Handles VFR by flushing page because this frame needs to have a timestamp
182
    if (st->codec->codec_id == CODEC_ID_THEORA &&
183
        ogg_granule_to_timestamp(oggstream, granule) >
184
        ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1) {
185
        if (oggstream->page.granule != -1)
186
            ogg_buffer_page(s, oggstream);
187
        flush = 1;
188
    }
189 95ca3b1e Baptiste Coudurier
190
    for (i = 0; i < total_segments; ) {
191
        OGGPage *page = &oggstream->page;
192
193
        segments = FFMIN(total_segments - i, 255 - page->segments_count);
194
195
        if (i && !page->segments_count)
196
            page->flags |= 1; // continued packet
197
198
        memset(page->segments+page->segments_count, 255, segments - 1);
199
        page->segments_count += segments - 1;
200
201
        len = FFMIN(size, segments*255);
202
        page->segments[page->segments_count++] = len - (segments-1)*255;
203
        memcpy(page->data+page->size, p, len);
204
        p += len;
205
        size -= len;
206
        i += segments;
207
        page->size += len;
208
209
        if (i == total_segments)
210
            page->granule = granule;
211
212
        if (page->segments_count == 255) {
213
            ogg_buffer_page(s, oggstream);
214
        }
215
    }
216 ab043374 Baptiste Coudurier
217
    if (flush && oggstream->page.granule != -1)
218
        ogg_buffer_page(s, oggstream);
219
220 95ca3b1e Baptiste Coudurier
    return 0;
221 5118cae6 Baptiste Coudurier
}
222
223 87462968 Justin Ruggles
static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
224 81e5ff7a Anton Khirnov
                                        int *header_len, AVMetadata **m, int framing_bit)
225 87462968 Justin Ruggles
{
226
    const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
227
    int size;
228
    uint8_t *p, *p0;
229 aeef3ec6 James Darnley
    unsigned int count;
230 87462968 Justin Ruggles
231 042ca05f Anton Khirnov
    ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
232
233 81e5ff7a Anton Khirnov
    size = offset + ff_vorbiscomment_length(*m, vendor, &count) + framing_bit;
234 87462968 Justin Ruggles
    p = av_mallocz(size);
235
    if (!p)
236
        return NULL;
237
    p0 = p;
238
239
    p += offset;
240 aeef3ec6 James Darnley
    ff_vorbiscomment_write(&p, m, vendor, count);
241 521d434f James Darnley
    if (framing_bit)
242
        bytestream_put_byte(&p, 1);
243 87462968 Justin Ruggles
244
    *header_len = size;
245
    return p0;
246
}
247
248 59c6178a Justin Ruggles
static int ogg_build_flac_headers(AVCodecContext *avctx,
249 aeef3ec6 James Darnley
                                  OGGStreamContext *oggstream, int bitexact,
250 81e5ff7a Anton Khirnov
                                  AVMetadata **m)
251 5118cae6 Baptiste Coudurier
{
252 59c6178a Justin Ruggles
    enum FLACExtradataFormat format;
253
    uint8_t *streaminfo;
254 5118cae6 Baptiste Coudurier
    uint8_t *p;
255 4315aa8b Justin Ruggles
256 59c6178a Justin Ruggles
    if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
257 5118cae6 Baptiste Coudurier
        return -1;
258 4315aa8b Justin Ruggles
259
    // first packet: STREAMINFO
260 6e330816 Justin Ruggles
    oggstream->header_len[0] = 51;
261
    oggstream->header[0] = av_mallocz(51); // per ogg flac specs
262 5118cae6 Baptiste Coudurier
    p = oggstream->header[0];
263 e9a32230 Justin Ruggles
    if (!p)
264 2874c81c Stefano Sabatini
        return AVERROR(ENOMEM);
265 5118cae6 Baptiste Coudurier
    bytestream_put_byte(&p, 0x7F);
266
    bytestream_put_buffer(&p, "FLAC", 4);
267
    bytestream_put_byte(&p, 1); // major version
268
    bytestream_put_byte(&p, 0); // minor version
269
    bytestream_put_be16(&p, 1); // headers packets without this one
270
    bytestream_put_buffer(&p, "fLaC", 4);
271
    bytestream_put_byte(&p, 0x00); // streaminfo
272
    bytestream_put_be24(&p, 34);
273 59c6178a Justin Ruggles
    bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
274 4315aa8b Justin Ruggles
275
    // second packet: VorbisComment
276 521d434f James Darnley
    p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
277 e9a32230 Justin Ruggles
    if (!p)
278 2874c81c Stefano Sabatini
        return AVERROR(ENOMEM);
279 87462968 Justin Ruggles
    oggstream->header[1] = p;
280 5118cae6 Baptiste Coudurier
    bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
281
    bytestream_put_be24(&p, oggstream->header_len[1] - 4);
282 87462968 Justin Ruggles
283
    return 0;
284
}
285
286
#define SPEEX_HEADER_SIZE 80
287
288
static int ogg_build_speex_headers(AVCodecContext *avctx,
289 aeef3ec6 James Darnley
                                   OGGStreamContext *oggstream, int bitexact,
290 81e5ff7a Anton Khirnov
                                   AVMetadata **m)
291 87462968 Justin Ruggles
{
292
    uint8_t *p;
293
294
    if (avctx->extradata_size < SPEEX_HEADER_SIZE)
295
        return -1;
296
297
    // first packet: Speex header
298
    p = av_mallocz(SPEEX_HEADER_SIZE);
299
    if (!p)
300 2874c81c Stefano Sabatini
        return AVERROR(ENOMEM);
301 87462968 Justin Ruggles
    oggstream->header[0] = p;
302
    oggstream->header_len[0] = SPEEX_HEADER_SIZE;
303
    bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
304
    AV_WL32(&oggstream->header[0][68], 0);  // set extra_headers to 0
305
306
    // second packet: VorbisComment
307 521d434f James Darnley
    p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
308 87462968 Justin Ruggles
    if (!p)
309 2874c81c Stefano Sabatini
        return AVERROR(ENOMEM);
310 87462968 Justin Ruggles
    oggstream->header[1] = p;
311 4315aa8b Justin Ruggles
312 5118cae6 Baptiste Coudurier
    return 0;
313
}
314
315
static int ogg_write_header(AVFormatContext *s)
316
{
317
    OGGStreamContext *oggstream;
318
    int i, j;
319 ce2a9bc3 Baptiste Coudurier
320 5118cae6 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
321
        AVStream *st = s->streams[i];
322 ce2a9bc3 Baptiste Coudurier
        unsigned serial_num = i;
323
324 72415b2a Stefano Sabatini
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
325 5118cae6 Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
326 72415b2a Stefano Sabatini
        else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
327 5118cae6 Baptiste Coudurier
            av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
328
        if (st->codec->codec_id != CODEC_ID_VORBIS &&
329
            st->codec->codec_id != CODEC_ID_THEORA &&
330 87462968 Justin Ruggles
            st->codec->codec_id != CODEC_ID_SPEEX  &&
331 5118cae6 Baptiste Coudurier
            st->codec->codec_id != CODEC_ID_FLAC) {
332
            av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
333
            return -1;
334
        }
335
336
        if (!st->codec->extradata || !st->codec->extradata_size) {
337
            av_log(s, AV_LOG_ERROR, "No extradata present\n");
338
            return -1;
339
        }
340
        oggstream = av_mallocz(sizeof(*oggstream));
341 95ca3b1e Baptiste Coudurier
        oggstream->page.stream_index = i;
342 ce2a9bc3 Baptiste Coudurier
343
        if (!(st->codec->flags & CODEC_FLAG_BITEXACT))
344
            do {
345
                serial_num = av_get_random_seed();
346
                for (j = 0; j < i; j++) {
347
                    OGGStreamContext *sc = s->streams[j]->priv_data;
348
                    if (serial_num == sc->serial_num)
349
                        break;
350
                }
351
            } while (j < i);
352
        oggstream->serial_num = serial_num;
353
354 5118cae6 Baptiste Coudurier
        st->priv_data = oggstream;
355
        if (st->codec->codec_id == CODEC_ID_FLAC) {
356 3f2eadf4 Justin Ruggles
            int err = ogg_build_flac_headers(st->codec, oggstream,
357 aeef3ec6 James Darnley
                                             st->codec->flags & CODEC_FLAG_BITEXACT,
358 81e5ff7a Anton Khirnov
                                             &s->metadata);
359 3f2eadf4 Justin Ruggles
            if (err) {
360 87b4dd3b Justin Ruggles
                av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
361 5118cae6 Baptiste Coudurier
                av_freep(&st->priv_data);
362 3f2eadf4 Justin Ruggles
                return err;
363 5118cae6 Baptiste Coudurier
            }
364 87462968 Justin Ruggles
        } else if (st->codec->codec_id == CODEC_ID_SPEEX) {
365
            int err = ogg_build_speex_headers(st->codec, oggstream,
366 aeef3ec6 James Darnley
                                              st->codec->flags & CODEC_FLAG_BITEXACT,
367 81e5ff7a Anton Khirnov
                                              &s->metadata);
368 87462968 Justin Ruggles
            if (err) {
369
                av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
370
                av_freep(&st->priv_data);
371
                return err;
372
            }
373 5118cae6 Baptiste Coudurier
        } else {
374 521d434f James Darnley
            uint8_t *p;
375 d256d21e Alex Converse
            const char *cstr = st->codec->codec_id == CODEC_ID_VORBIS ? "vorbis" : "theora";
376 521d434f James Darnley
            int header_type = st->codec->codec_id == CODEC_ID_VORBIS ? 3 : 0x81;
377
            int framing_bit = st->codec->codec_id == CODEC_ID_VORBIS ? 1 : 0;
378
379 5118cae6 Baptiste Coudurier
            if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
380
                                      st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
381
                                      oggstream->header, oggstream->header_len) < 0) {
382
                av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
383
                av_freep(&st->priv_data);
384
                return -1;
385
            }
386 521d434f James Darnley
387
            p = ogg_write_vorbiscomment(7, st->codec->flags & CODEC_FLAG_BITEXACT,
388 81e5ff7a Anton Khirnov
                                        &oggstream->header_len[1], &s->metadata,
389 521d434f James Darnley
                                        framing_bit);
390
            if (!p)
391
                return AVERROR(ENOMEM);
392
393
            oggstream->header[1] = p;
394
            bytestream_put_byte(&p, header_type);
395
            bytestream_put_buffer(&p, cstr, 6);
396
397 5118cae6 Baptiste Coudurier
            if (st->codec->codec_id == CODEC_ID_THEORA) {
398
                /** KFGSHIFT is the width of the less significant section of the granule position
399
                    The less significant section is the frame count since the last keyframe */
400
                oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
401
                oggstream->vrev = oggstream->header[0][9];
402
                av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
403
                       oggstream->kfgshift, oggstream->vrev);
404
            }
405
        }
406
    }
407 95ca3b1e Baptiste Coudurier
408
    for (j = 0; j < s->nb_streams; j++) {
409
        OGGStreamContext *oggstream = s->streams[j]->priv_data;
410
        ogg_buffer_data(s, s->streams[j], oggstream->header[0],
411
                        oggstream->header_len[0], 0);
412
        oggstream->page.flags |= 2; // bos
413
        ogg_buffer_page(s, oggstream);
414
    }
415
    for (j = 0; j < s->nb_streams; j++) {
416
        AVStream *st = s->streams[j];
417
        OGGStreamContext *oggstream = st->priv_data;
418
        for (i = 1; i < 3; i++) {
419
            if (oggstream && oggstream->header_len[i])
420
                ogg_buffer_data(s, st, oggstream->header[i],
421
                                oggstream->header_len[i], 0);
422 5118cae6 Baptiste Coudurier
        }
423 95ca3b1e Baptiste Coudurier
        ogg_buffer_page(s, oggstream);
424 5118cae6 Baptiste Coudurier
    }
425
    return 0;
426
}
427
428 95ca3b1e Baptiste Coudurier
static void ogg_write_pages(AVFormatContext *s, int flush)
429
{
430
    OGGContext *ogg = s->priv_data;
431
    OGGPageList *next, *p;
432
433
    if (!ogg->page_list)
434
        return;
435
436
    for (p = ogg->page_list; p; ) {
437
        OGGStreamContext *oggstream =
438
            s->streams[p->page.stream_index]->priv_data;
439
        if (oggstream->page_count < 2 && !flush)
440
            break;
441
        ogg_write_page(s, &p->page,
442
                       flush && oggstream->page_count == 1 ? 4 : 0); // eos
443
        next = p->next;
444
        av_freep(&p);
445
        p = next;
446
    }
447
    ogg->page_list = p;
448
}
449
450 5118cae6 Baptiste Coudurier
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
451
{
452
    AVStream *st = s->streams[pkt->stream_index];
453
    OGGStreamContext *oggstream = st->priv_data;
454 95ca3b1e Baptiste Coudurier
    int ret;
455 5118cae6 Baptiste Coudurier
    int64_t granule;
456
457
    if (st->codec->codec_id == CODEC_ID_THEORA) {
458
        int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
459
        int pframe_count;
460 cc947f04 Jean-Daniel Dupas
        if (pkt->flags & AV_PKT_FLAG_KEY)
461 5118cae6 Baptiste Coudurier
            oggstream->last_kf_pts = pts;
462
        pframe_count = pts - oggstream->last_kf_pts;
463
        // prevent frame count from overflow if key frame flag is not set
464
        if (pframe_count >= (1<<oggstream->kfgshift)) {
465
            oggstream->last_kf_pts += pframe_count;
466
            pframe_count = 0;
467
        }
468
        granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
469
    } else
470
        granule = pkt->pts + pkt->duration;
471
472 95ca3b1e Baptiste Coudurier
    ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule);
473
    if (ret < 0)
474
        return ret;
475 5118cae6 Baptiste Coudurier
476 95ca3b1e Baptiste Coudurier
    ogg_write_pages(s, 0);
477 fd5583bc Baptiste Coudurier
478 ab043374 Baptiste Coudurier
    oggstream->last_granule = granule;
479
480 95ca3b1e Baptiste Coudurier
    return 0;
481 fd5583bc Baptiste Coudurier
}
482
483 95ca3b1e Baptiste Coudurier
static int ogg_write_trailer(AVFormatContext *s)
484 37b74c86 Baptiste Coudurier
{
485 95ca3b1e Baptiste Coudurier
    int i;
486 012dc2c4 Baptiste Coudurier
487 95ca3b1e Baptiste Coudurier
    /* flush current page */
488
    for (i = 0; i < s->nb_streams; i++)
489
        ogg_buffer_page(s, s->streams[i]->priv_data);
490 e07b882b Michael Niedermayer
491 95ca3b1e Baptiste Coudurier
    ogg_write_pages(s, 1);
492 e07b882b Michael Niedermayer
493 5118cae6 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
494
        AVStream *st = s->streams[i];
495
        OGGStreamContext *oggstream = st->priv_data;
496 87462968 Justin Ruggles
        if (st->codec->codec_id == CODEC_ID_FLAC ||
497
            st->codec->codec_id == CODEC_ID_SPEEX) {
498 5118cae6 Baptiste Coudurier
            av_free(oggstream->header[0]);
499
            av_free(oggstream->header[1]);
500
        }
501
        av_freep(&st->priv_data);
502
    }
503
    return 0;
504
}
505
506 66355be3 Diego Elio Pettenò
AVOutputFormat ff_ogg_muxer = {
507 5118cae6 Baptiste Coudurier
    "ogg",
508 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Ogg"),
509 5118cae6 Baptiste Coudurier
    "application/ogg",
510 87462968 Justin Ruggles
    "ogg,ogv,spx",
511 95ca3b1e Baptiste Coudurier
    sizeof(OGGContext),
512 5118cae6 Baptiste Coudurier
    CODEC_ID_FLAC,
513
    CODEC_ID_THEORA,
514
    ogg_write_header,
515
    ogg_write_packet,
516
    ogg_write_trailer,
517
};