Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / libavformat / oggenc.c
1 /*
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 #include <stdint.h>
23
24 #include "libavutil/crc.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/random_seed.h"
28 #include "libavcodec/xiph.h"
29 #include "libavcodec/bytestream.h"
30 #include "libavcodec/flac.h"
31 #include "avformat.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34 #include "vorbiscomment.h"
35
36 #define MAX_PAGE_SIZE 65025
37
38 typedef struct {
39     int64_t start_granule;
40     int64_t granule;
41     int stream_index;
42     uint8_t flags;
43     uint8_t segments_count;
44     uint8_t segments[255];
45     uint8_t data[MAX_PAGE_SIZE];
46     uint16_t size;
47 } OGGPage;
48
49 typedef struct {
50     unsigned page_counter;
51     uint8_t *header[3];
52     int header_len[3];
53     /** for theora granule */
54     int kfgshift;
55     int64_t last_kf_pts;
56     int vrev;
57     int eos;
58     unsigned page_count; ///< number of page buffered
59     OGGPage page; ///< current page
60     unsigned serial_num; ///< serial number
61     int64_t last_granule; ///< last packet granule
62 } OGGStreamContext;
63
64 typedef struct OGGPageList {
65     OGGPage page;
66     struct OGGPageList *next;
67 } OGGPageList;
68
69 typedef struct {
70     const AVClass *class;
71     OGGPageList *page_list;
72     int pref_size; ///< preferred page size (0 => fill all segments)
73     int64_t pref_duration;      ///< preferred page duration (0 => fill all segments)
74 } OGGContext;
75
76 #define OFFSET(x) offsetof(OGGContext, x)
77 #define PARAM AV_OPT_FLAG_ENCODING_PARAM
78
79 static const AVOption options[] = {
80     { "oggpagesize", "Set preferred Ogg page size.",
81       offsetof(OGGContext, pref_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, MAX_PAGE_SIZE, AV_OPT_FLAG_ENCODING_PARAM},
82     { "pagesize", "preferred page size in bytes (deprecated)",
83         OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
84     { "page_duration", "preferred page duration, in microseconds",
85         OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
86     { NULL },
87 };
88
89 static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
90 {
91     int64_t pos = avio_tell(pb);
92     uint32_t checksum = ffio_get_checksum(pb);
93     avio_seek(pb, crc_offset, SEEK_SET);
94     avio_wb32(pb, checksum);
95     avio_seek(pb, pos, SEEK_SET);
96 }
97
98 static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
99 {
100     OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
101     AVIOContext *pb;
102     int64_t crc_offset;
103     int ret, size;
104     uint8_t *buf;
105
106     ret = avio_open_dyn_buf(&pb);
107     if (ret < 0)
108         return ret;
109     ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
110     ffio_wfourcc(pb, "OggS");
111     avio_w8(pb, 0);
112     avio_w8(pb, page->flags | extra_flags);
113     avio_wl64(pb, page->granule);
114     avio_wl32(pb, oggstream->serial_num);
115     avio_wl32(pb, oggstream->page_counter++);
116     crc_offset = avio_tell(pb);
117     avio_wl32(pb, 0); // crc
118     avio_w8(pb, page->segments_count);
119     avio_write(pb, page->segments, page->segments_count);
120     avio_write(pb, page->data, page->size);
121
122     ogg_update_checksum(s, pb, crc_offset);
123     avio_flush(pb);
124
125     size = avio_close_dyn_buf(pb, &buf);
126     if (size < 0)
127         return size;
128
129     avio_write(s->pb, buf, size);
130     avio_flush(s->pb);
131     av_free(buf);
132     oggstream->page_count--;
133     return 0;
134 }
135
136 static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
137 {
138     return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
139 }
140
141 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
142 {
143     if (oggstream->kfgshift)
144         return (granule>>oggstream->kfgshift) +
145             (granule & ((1<<oggstream->kfgshift)-1));
146     else
147         return granule;
148 }
149
150 static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
151 {
152     AVStream *st2 = s->streams[next->stream_index];
153     AVStream *st  = s->streams[page->stream_index];
154     int64_t next_granule, cur_granule;
155
156     if (next->granule == -1 || page->granule == -1)
157         return 0;
158
159     next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
160                                 st2->time_base, AV_TIME_BASE_Q);
161     cur_granule  = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
162                                 st ->time_base, AV_TIME_BASE_Q);
163     return next_granule > cur_granule;
164 }
165
166 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
167 {
168     oggstream->page.granule = -1;
169     oggstream->page.flags = 0;
170     oggstream->page.segments_count = 0;
171     oggstream->page.size = 0;
172     return 0;
173 }
174
175 static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
176 {
177     OGGContext *ogg = s->priv_data;
178     OGGPageList **p = &ogg->page_list;
179     OGGPageList *l = av_mallocz(sizeof(*l));
180
181     if (!l)
182         return AVERROR(ENOMEM);
183     l->page = oggstream->page;
184
185     oggstream->page.start_granule = oggstream->page.granule;
186     oggstream->page_count++;
187     ogg_reset_cur_page(oggstream);
188
189     while (*p) {
190         if (ogg_compare_granule(s, &(*p)->page, &l->page))
191             break;
192         p = &(*p)->next;
193     }
194     l->next = *p;
195     *p = l;
196
197     return 0;
198 }
199
200 static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
201                            uint8_t *data, unsigned size, int64_t granule,
202                            int header)
203 {
204     OGGStreamContext *oggstream = st->priv_data;
205     OGGContext *ogg = s->priv_data;
206     int total_segments = size / 255 + 1;
207     uint8_t *p = data;
208     int i, segments, len, flush = 0;
209
210     // Handles VFR by flushing page because this frame needs to have a timestamp
211     // For theora, keyframes also need to have a timestamp to correctly mark
212     // them as such, otherwise seeking will not work correctly at the very
213     // least with old libogg versions.
214     // Do not try to flush header packets though, that will create broken files.
215     if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
216         (ogg_granule_to_timestamp(oggstream, granule) >
217          ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
218          ogg_key_granule(oggstream, granule))) {
219         if (oggstream->page.granule != -1)
220             ogg_buffer_page(s, oggstream);
221         flush = 1;
222     }
223
224     // avoid a continued page
225     if (!header && oggstream->page.size > 0 &&
226         MAX_PAGE_SIZE - oggstream->page.size < size) {
227         ogg_buffer_page(s, oggstream);
228     }
229
230     for (i = 0; i < total_segments; ) {
231         OGGPage *page = &oggstream->page;
232
233         segments = FFMIN(total_segments - i, 255 - page->segments_count);
234
235         if (i && !page->segments_count)
236             page->flags |= 1; // continued packet
237
238         memset(page->segments+page->segments_count, 255, segments - 1);
239         page->segments_count += segments - 1;
240
241         len = FFMIN(size, segments*255);
242         page->segments[page->segments_count++] = len - (segments-1)*255;
243         memcpy(page->data+page->size, p, len);
244         p += len;
245         size -= len;
246         i += segments;
247         page->size += len;
248
249         if (i == total_segments)
250             page->granule = granule;
251
252         if (!header) {
253             AVStream *st = s->streams[page->stream_index];
254
255             int64_t start = av_rescale_q(page->start_granule, st->time_base,
256                                          AV_TIME_BASE_Q);
257             int64_t next  = av_rescale_q(page->granule, st->time_base,
258                                          AV_TIME_BASE_Q);
259
260             if (page->segments_count == 255 ||
261                 (ogg->pref_size     > 0 && page->size   >= ogg->pref_size) ||
262                 (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
263                 ogg_buffer_page(s, oggstream);
264             }
265         }
266     }
267
268     if (flush && oggstream->page.granule != -1)
269         ogg_buffer_page(s, oggstream);
270
271     return 0;
272 }
273
274 static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
275                                         int *header_len, AVDictionary **m, int framing_bit)
276 {
277     const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
278     int size;
279     uint8_t *p, *p0;
280
281     ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
282
283     size = offset + ff_vorbiscomment_length(*m, vendor) + framing_bit;
284     p = av_mallocz(size);
285     if (!p)
286         return NULL;
287     p0 = p;
288
289     p += offset;
290     ff_vorbiscomment_write(&p, m, vendor);
291     if (framing_bit)
292         bytestream_put_byte(&p, 1);
293
294     *header_len = size;
295     return p0;
296 }
297
298 static int ogg_build_flac_headers(AVCodecContext *avctx,
299                                   OGGStreamContext *oggstream, int bitexact,
300                                   AVDictionary **m)
301 {
302     enum FLACExtradataFormat format;
303     uint8_t *streaminfo;
304     uint8_t *p;
305
306     if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
307         return -1;
308
309     // first packet: STREAMINFO
310     oggstream->header_len[0] = 51;
311     oggstream->header[0] = av_mallocz(51); // per ogg flac specs
312     p = oggstream->header[0];
313     if (!p)
314         return AVERROR(ENOMEM);
315     bytestream_put_byte(&p, 0x7F);
316     bytestream_put_buffer(&p, "FLAC", 4);
317     bytestream_put_byte(&p, 1); // major version
318     bytestream_put_byte(&p, 0); // minor version
319     bytestream_put_be16(&p, 1); // headers packets without this one
320     bytestream_put_buffer(&p, "fLaC", 4);
321     bytestream_put_byte(&p, 0x00); // streaminfo
322     bytestream_put_be24(&p, 34);
323     bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
324
325     // second packet: VorbisComment
326     p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
327     if (!p)
328         return AVERROR(ENOMEM);
329     oggstream->header[1] = p;
330     bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
331     bytestream_put_be24(&p, oggstream->header_len[1] - 4);
332
333     return 0;
334 }
335
336 #define SPEEX_HEADER_SIZE 80
337
338 static int ogg_build_speex_headers(AVCodecContext *avctx,
339                                    OGGStreamContext *oggstream, int bitexact,
340                                    AVDictionary **m)
341 {
342     uint8_t *p;
343
344     if (avctx->extradata_size < SPEEX_HEADER_SIZE)
345         return -1;
346
347     // first packet: Speex header
348     p = av_mallocz(SPEEX_HEADER_SIZE);
349     if (!p)
350         return AVERROR(ENOMEM);
351     oggstream->header[0] = p;
352     oggstream->header_len[0] = SPEEX_HEADER_SIZE;
353     bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
354     AV_WL32(&oggstream->header[0][68], 0);  // set extra_headers to 0
355
356     // second packet: VorbisComment
357     p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
358     if (!p)
359         return AVERROR(ENOMEM);
360     oggstream->header[1] = p;
361
362     return 0;
363 }
364
365 #define OPUS_HEADER_SIZE 19
366
367 static int ogg_build_opus_headers(AVCodecContext *avctx,
368                                   OGGStreamContext *oggstream, int bitexact,
369                                   AVDictionary **m)
370 {
371     uint8_t *p;
372
373     if (avctx->extradata_size < OPUS_HEADER_SIZE)
374         return -1;
375
376     /* first packet: Opus header */
377     p = av_mallocz(avctx->extradata_size);
378     if (!p)
379         return AVERROR(ENOMEM);
380     oggstream->header[0] = p;
381     oggstream->header_len[0] = avctx->extradata_size;
382     bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
383
384     /* second packet: VorbisComment */
385     p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
386     if (!p)
387         return AVERROR(ENOMEM);
388     oggstream->header[1] = p;
389     bytestream_put_buffer(&p, "OpusTags", 8);
390
391     return 0;
392 }
393
394 static void ogg_write_pages(AVFormatContext *s, int flush)
395 {
396     OGGContext *ogg = s->priv_data;
397     OGGPageList *next, *p;
398
399     if (!ogg->page_list)
400         return;
401
402     for (p = ogg->page_list; p; ) {
403         OGGStreamContext *oggstream =
404             s->streams[p->page.stream_index]->priv_data;
405         if (oggstream->page_count < 2 && !flush)
406             break;
407         ogg_write_page(s, &p->page,
408                        flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
409         next = p->next;
410         av_freep(&p);
411         p = next;
412     }
413     ogg->page_list = p;
414 }
415
416 static int ogg_write_header(AVFormatContext *s)
417 {
418     OGGContext *ogg = s->priv_data;
419     OGGStreamContext *oggstream = NULL;
420     int i, j;
421
422     if (ogg->pref_size)
423         av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
424
425     for (i = 0; i < s->nb_streams; i++) {
426         AVStream *st = s->streams[i];
427         unsigned serial_num = i;
428
429         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
430             if (st->codec->codec_id == AV_CODEC_ID_OPUS)
431                 /* Opus requires a fixed 48kHz clock */
432                 avpriv_set_pts_info(st, 64, 1, 48000);
433             else
434                 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
435         }
436
437         if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
438             st->codec->codec_id != AV_CODEC_ID_THEORA &&
439             st->codec->codec_id != AV_CODEC_ID_SPEEX  &&
440             st->codec->codec_id != AV_CODEC_ID_FLAC   &&
441             st->codec->codec_id != AV_CODEC_ID_OPUS) {
442             av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
443             return -1;
444         }
445
446         if (!st->codec->extradata || !st->codec->extradata_size) {
447             av_log(s, AV_LOG_ERROR, "No extradata present\n");
448             return -1;
449         }
450         oggstream = av_mallocz(sizeof(*oggstream));
451         if (!oggstream)
452             return AVERROR(ENOMEM);
453
454         oggstream->page.stream_index = i;
455
456         if (!(s->flags & AVFMT_FLAG_BITEXACT))
457             do {
458                 serial_num = av_get_random_seed();
459                 for (j = 0; j < i; j++) {
460                     OGGStreamContext *sc = s->streams[j]->priv_data;
461                     if (serial_num == sc->serial_num)
462                         break;
463                 }
464             } while (j < i);
465         oggstream->serial_num = serial_num;
466
467         av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
468
469         st->priv_data = oggstream;
470         if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
471             int err = ogg_build_flac_headers(st->codec, oggstream,
472                                              s->flags & AVFMT_FLAG_BITEXACT,
473                                              &st->metadata);
474             if (err) {
475                 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
476                 av_freep(&st->priv_data);
477                 return err;
478             }
479         } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
480             int err = ogg_build_speex_headers(st->codec, oggstream,
481                                               s->flags & AVFMT_FLAG_BITEXACT,
482                                               &st->metadata);
483             if (err) {
484                 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
485                 av_freep(&st->priv_data);
486                 return err;
487             }
488         } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
489             int err = ogg_build_opus_headers(st->codec, oggstream,
490                                              s->flags & AVFMT_FLAG_BITEXACT,
491                                              &st->metadata);
492             if (err) {
493                 av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
494                 av_freep(&st->priv_data);
495                 return err;
496             }
497         } else {
498             uint8_t *p;
499             const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
500             int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
501             int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
502
503             if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
504                                       st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
505                                       oggstream->header, oggstream->header_len) < 0) {
506                 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
507                 av_freep(&st->priv_data);
508                 return -1;
509             }
510
511             p = ogg_write_vorbiscomment(7, s->flags & AVFMT_FLAG_BITEXACT,
512                                         &oggstream->header_len[1], &st->metadata,
513                                         framing_bit);
514             oggstream->header[1] = p;
515             if (!p)
516                 return AVERROR(ENOMEM);
517
518             bytestream_put_byte(&p, header_type);
519             bytestream_put_buffer(&p, cstr, 6);
520
521             if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
522                 /** KFGSHIFT is the width of the less significant section of the granule position
523                     The less significant section is the frame count since the last keyframe */
524                 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
525                 oggstream->vrev = oggstream->header[0][9];
526                 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
527                        oggstream->kfgshift, oggstream->vrev);
528             }
529         }
530     }
531
532     for (j = 0; j < s->nb_streams; j++) {
533         OGGStreamContext *oggstream = s->streams[j]->priv_data;
534         ogg_buffer_data(s, s->streams[j], oggstream->header[0],
535                         oggstream->header_len[0], 0, 1);
536         oggstream->page.flags |= 2; // bos
537         ogg_buffer_page(s, oggstream);
538     }
539     for (j = 0; j < s->nb_streams; j++) {
540         AVStream *st = s->streams[j];
541         OGGStreamContext *oggstream = st->priv_data;
542         for (i = 1; i < 3; i++) {
543             if (oggstream->header_len[i])
544                 ogg_buffer_data(s, st, oggstream->header[i],
545                                 oggstream->header_len[i], 0, 1);
546         }
547         ogg_buffer_page(s, oggstream);
548     }
549
550     oggstream->page.start_granule = AV_NOPTS_VALUE;
551
552     ogg_write_pages(s, 2);
553
554     return 0;
555 }
556
557 static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
558 {
559     AVStream *st = s->streams[pkt->stream_index];
560     OGGStreamContext *oggstream = st->priv_data;
561     int ret;
562     int64_t granule;
563
564     if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
565         int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
566         int pframe_count;
567         if (pkt->flags & AV_PKT_FLAG_KEY)
568             oggstream->last_kf_pts = pts;
569         pframe_count = pts - oggstream->last_kf_pts;
570         // prevent frame count from overflow if key frame flag is not set
571         if (pframe_count >= (1<<oggstream->kfgshift)) {
572             oggstream->last_kf_pts += pframe_count;
573             pframe_count = 0;
574         }
575         granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
576     } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
577         granule = pkt->pts + pkt->duration + av_rescale_q(st->codec->delay, (AVRational){ 1, st->codec->sample_rate }, st->time_base);
578     else
579         granule = pkt->pts + pkt->duration;
580
581     if (oggstream->page.start_granule == AV_NOPTS_VALUE)
582         oggstream->page.start_granule = pkt->pts;
583
584     ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
585     if (ret < 0)
586         return ret;
587
588     ogg_write_pages(s, 0);
589
590     oggstream->last_granule = granule;
591
592     return 0;
593 }
594
595 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
596 {
597     int i;
598
599     if (pkt)
600         return ogg_write_packet_internal(s, pkt);
601
602     for (i = 0; i < s->nb_streams; i++) {
603         OGGStreamContext *oggstream = s->streams[i]->priv_data;
604         if (oggstream->page.segments_count)
605             ogg_buffer_page(s, oggstream);
606     }
607
608     ogg_write_pages(s, 2);
609     return 0;
610 }
611
612 static int ogg_write_trailer(AVFormatContext *s)
613 {
614     int i;
615
616     /* flush current page if needed */
617     for (i = 0; i < s->nb_streams; i++) {
618         OGGStreamContext *oggstream = s->streams[i]->priv_data;
619
620         if (oggstream->page.size > 0)
621             ogg_buffer_page(s, oggstream);
622     }
623
624     ogg_write_pages(s, 1);
625
626     for (i = 0; i < s->nb_streams; i++) {
627         AVStream *st = s->streams[i];
628         OGGStreamContext *oggstream = st->priv_data;
629         if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
630             st->codec->codec_id == AV_CODEC_ID_SPEEX ||
631             st->codec->codec_id == AV_CODEC_ID_OPUS) {
632             av_freep(&oggstream->header[0]);
633         }
634         av_freep(&oggstream->header[1]);
635         av_freep(&st->priv_data);
636     }
637     return 0;
638 }
639
640 #if CONFIG_OGG_MUXER
641 static const AVClass ogg_muxer_class = {
642     .class_name = "Ogg muxer",
643     .item_name  = av_default_item_name,
644     .option     = options,
645     .version    = LIBAVUTIL_VERSION_INT,
646 };
647
648 AVOutputFormat ff_ogg_muxer = {
649     .name              = "ogg",
650     .long_name         = NULL_IF_CONFIG_SMALL("Ogg"),
651     .mime_type         = "application/ogg",
652     .extensions        = "ogg,ogv"
653 #if !CONFIG_SPEEX_MUXER
654                          ",spx"
655 #endif
656 #if !CONFIG_OPUS_MUXER
657                          ",opus"
658 #endif
659                          ,
660     .priv_data_size    = sizeof(OGGContext),
661     .audio_codec       = CONFIG_LIBVORBIS_ENCODER ?
662                          AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
663     .video_codec       = AV_CODEC_ID_THEORA,
664     .write_header      = ogg_write_header,
665     .write_packet      = ogg_write_packet,
666     .write_trailer     = ogg_write_trailer,
667     .flags             = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
668     .priv_class        = &ogg_muxer_class,
669 };
670 #endif
671
672 #if CONFIG_OGA_MUXER
673 static const AVClass oga_muxer_class = {
674     .class_name = "Ogg audio muxer",
675     .item_name  = av_default_item_name,
676     .option     = options,
677     .version    = LIBAVUTIL_VERSION_INT,
678 };
679
680 AVOutputFormat ff_oga_muxer = {
681     .name              = "oga",
682     .long_name         = NULL_IF_CONFIG_SMALL("Ogg audio"),
683     .mime_type         = "audio/ogg",
684     .extensions        = "oga",
685     .priv_data_size    = sizeof(OGGContext),
686     .audio_codec       = AV_CODEC_ID_VORBIS,
687     .video_codec       = AV_CODEC_ID_NONE,
688     .write_header      = ogg_write_header,
689     .write_packet      = ogg_write_packet,
690     .write_trailer     = ogg_write_trailer,
691     .flags             = AVFMT_TS_NEGATIVE,
692     .priv_class        = &oga_muxer_class,
693 };
694 #endif
695
696 #if CONFIG_SPEEX_MUXER
697 static const AVClass speex_muxer_class = {
698     .class_name = "Speex muxer",
699     .item_name  = av_default_item_name,
700     .option     = options,
701     .version    = LIBAVUTIL_VERSION_INT,
702 };
703
704 AVOutputFormat ff_speex_muxer = {
705     .name              = "speex",
706     .long_name         = NULL_IF_CONFIG_SMALL("Speex"),
707     .mime_type         = "audio/ogg",
708     .extensions        = "spx",
709     .priv_data_size    = sizeof(OGGContext),
710     .audio_codec       = AV_CODEC_ID_SPEEX,
711      .video_codec      = AV_CODEC_ID_NONE,
712     .write_header      = ogg_write_header,
713     .write_packet      = ogg_write_packet,
714     .write_trailer     = ogg_write_trailer,
715     .flags             = AVFMT_TS_NEGATIVE,
716     .priv_class        = &speex_muxer_class,
717 };
718 #endif
719
720 #if CONFIG_OPUS_MUXER
721 static const AVClass opus_muxer_class = {
722     .class_name = "Opus muxer",
723     .item_name  = av_default_item_name,
724     .option     = options,
725     .version    = LIBAVUTIL_VERSION_INT,
726 };
727
728 AVOutputFormat ff_opus_muxer = {
729     .name              = "opus",
730     .long_name         = NULL_IF_CONFIG_SMALL("Opus"),
731     .mime_type         = "audio/ogg",
732     .extensions        = "opus",
733     .priv_data_size    = sizeof(OGGContext),
734     .audio_codec       = AV_CODEC_ID_OPUS,
735     .video_codec       = AV_CODEC_ID_NONE,
736     .write_header      = ogg_write_header,
737     .write_packet      = ogg_write_packet,
738     .write_trailer     = ogg_write_trailer,
739     .flags             = AVFMT_TS_NEGATIVE,
740     .priv_class        = &opus_muxer_class,
741 };
742 #endif