FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/oggenc.c
Date: 2026-10-14 03:02:30
Exec Total Coverage
Lines: 390 475 82.1%
Functions: 22 23 95.7%
Branches: 209 292 71.6%

Line Branch Exec Source
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 "config_components.h"
23
24 #include <stdint.h>
25
26 #include "libavutil/crc.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/random_seed.h"
31 #include "libavcodec/xiph.h"
32 #include "libavcodec/bytestream.h"
33 #include "libavcodec/flac.h"
34 #include "avformat.h"
35 #include "avio_internal.h"
36 #include "internal.h"
37 #include "mux.h"
38 #include "version.h"
39 #include "vorbiscomment.h"
40
41 #define MAX_PAGE_SIZE 65025
42
43 typedef struct OGGPage {
44 int64_t start_granule;
45 int64_t granule;
46 int stream_index;
47 uint8_t flags;
48 uint8_t segments_count;
49 uint8_t segments[255];
50 uint8_t data[MAX_PAGE_SIZE];
51 uint16_t size;
52 } OGGPage;
53
54 typedef struct OGGStreamContext {
55 unsigned page_counter;
56 uint8_t *header[3];
57 int header_len[3];
58 /** for theora granule */
59 int kfgshift;
60 int64_t last_kf_pts;
61 int vrev;
62 /* for VP8 granule */
63 int isvp8;
64 int eos;
65 unsigned page_count; ///< number of page buffered
66 OGGPage page; ///< current page
67 unsigned serial_num; ///< serial number
68 int64_t last_granule; ///< last packet granule
69 int packet_seen; ///< true when packets have been submitted
70 } OGGStreamContext;
71
72 typedef struct OGGPageList {
73 OGGPage page;
74 struct OGGPageList *next;
75 } OGGPageList;
76
77 typedef struct OGGContext {
78 const AVClass *class;
79 OGGPageList *page_list;
80 int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
81 int serial_offset;
82 int failed; // if true all packet submission will fail.
83 } OGGContext;
84
85 #define OFFSET(x) offsetof(OGGContext, x)
86 #define PARAM AV_OPT_FLAG_ENCODING_PARAM
87
88 static int ogg_write_trailer(AVFormatContext *s);
89
90 static const AVOption options[] = {
91 { "serial_offset", "serial number offset, -1 to derive it from the stream headers",
92 OFFSET(serial_offset), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, PARAM },
93 { "page_duration", "preferred page duration, in microseconds",
94 OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
95 { NULL },
96 };
97
98 static const AVClass ogg_muxer_class = {
99 .class_name = "Ogg (audio/video/Speex/Opus) muxer",
100 .item_name = av_default_item_name,
101 .option = options,
102 .version = LIBAVUTIL_VERSION_INT,
103 };
104
105 17 static void ogg_cleanup_stream(AVStream *st)
106 {
107 17 OGGStreamContext *oggstream = st->priv_data;
108
109
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7 times.
17 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
110
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 st->codecpar->codec_id == AV_CODEC_ID_SPEEX ||
111
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 st->codecpar->codec_id == AV_CODEC_ID_OPUS ||
112
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 st->codecpar->codec_id == AV_CODEC_ID_VP8) {
113 10 av_freep(&oggstream->header[0]);
114 }
115
116 17 av_freep(&oggstream->header[1]);
117 17 }
118
119 77 static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
120 {
121 77 OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
122 77 uint8_t buf[4 + 1 + 1 + 8 + 4 + 4 + 4 + 1 + 255], *ptr = buf, *crc_pos;
123 77 const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE);
124 uint32_t crc;
125
126 77 bytestream_put_le32(&ptr, MKTAG('O', 'g', 'g', 'S'));
127 77 bytestream_put_byte(&ptr, 0);
128 77 bytestream_put_byte(&ptr, page->flags | extra_flags);
129 77 bytestream_put_le64(&ptr, page->granule);
130 77 bytestream_put_le32(&ptr, oggstream->serial_num);
131 77 bytestream_put_le32(&ptr, oggstream->page_counter++);
132 77 crc_pos = ptr;
133 77 bytestream_put_le32(&ptr, 0);
134 77 bytestream_put_byte(&ptr, page->segments_count);
135 77 bytestream_put_buffer(&ptr, page->segments, page->segments_count);
136
137 77 crc = av_crc(crc_table, 0, buf, ptr - buf);
138 77 crc = av_crc(crc_table, crc, page->data, page->size);
139 77 bytestream_put_be32(&crc_pos, crc);
140
141 77 avio_write(s->pb, buf, ptr - buf);
142 77 avio_write(s->pb, page->data, page->size);
143 77 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT);
144 77 oggstream->page_count--;
145 77 }
146
147 119 static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
148 {
149
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1 times.
237 return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
150
4/4
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 110 times.
118 (oggstream->isvp8 && !((granule >> 3) & 0x07ffffff));
151 }
152
153 1564 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
154 {
155
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 1519 times.
1564 if (oggstream->kfgshift)
156 45 return (granule>>oggstream->kfgshift) +
157 45 (granule & ((1<<oggstream->kfgshift)-1));
158
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 1147 times.
1519 else if (oggstream->isvp8)
159 372 return granule >> 32;
160 else
161 1147 return granule;
162 }
163
164 70 static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
165 {
166 70 AVStream *st2 = s->streams[next->stream_index];
167 70 AVStream *st = s->streams[page->stream_index];
168 int64_t next_granule, cur_granule;
169
170
3/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 55 times.
70 if (next->granule == -1 || page->granule == -1)
171 15 return 0;
172
173 110 next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
174 55 st2->time_base, AV_TIME_BASE_Q);
175 110 cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
176 55 st ->time_base, AV_TIME_BASE_Q);
177 55 return next_granule > cur_granule;
178 }
179
180 77 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
181 {
182 77 oggstream->page.granule = -1;
183 77 oggstream->page.flags = 0;
184 77 oggstream->page.segments_count = 0;
185 77 oggstream->page.size = 0;
186 77 return 0;
187 }
188
189 77 static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
190 {
191 77 OGGContext *ogg = s->priv_data;
192 77 OGGPageList **p = &ogg->page_list;
193 77 OGGPageList *l = av_mallocz(sizeof(*l));
194
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (!l) {
196 ✗ ogg_reset_cur_page(oggstream);
197 ✗ return AVERROR(ENOMEM);
198 }
199 77 l->page = oggstream->page;
200
201 77 oggstream->page.start_granule = ogg_granule_to_timestamp(oggstream, oggstream->page.granule);
202 77 oggstream->page_count++;
203 77 ogg_reset_cur_page(oggstream);
204
205
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 74 times.
144 while (*p) {
206
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 67 times.
70 if (ogg_compare_granule(s, &(*p)->page, &l->page))
207 3 break;
208 67 p = &(*p)->next;
209 }
210 77 l->next = *p;
211 77 *p = l;
212
213 77 return 0;
214 }
215
216 1134 static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
217 const uint8_t *data, unsigned size, int64_t granule,
218 int header)
219 {
220 1134 OGGStreamContext *oggstream = st->priv_data;
221 1134 OGGContext *ogg = s->priv_data;
222 1134 int total_segments = size / 255 + 1;
223 1134 const uint8_t *p = data;
224 1134 int i, segments, len, ret, flush = 0;
225
226 // Handles VFR by flushing page because this frame needs to have a timestamp
227 // For theora and VP8, keyframes also need to have a timestamp to correctly mark
228 // them as such, otherwise seeking will not work correctly at the very
229 // least with old libogg versions.
230 // Do not try to flush header packets though, that will create broken files.
231
6/6
✓ Branch 0 taken 1123 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 113 times.
✓ Branch 3 taken 1010 times.
✓ Branch 4 taken 119 times.
✓ Branch 5 taken 5 times.
1134 if ((st->codecpar->codec_id == AV_CODEC_ID_THEORA || st->codecpar->codec_id == AV_CODEC_ID_VP8) && !header &&
232 119 (ogg_granule_to_timestamp(oggstream, granule) >
233
3/4
✓ Branch 1 taken 119 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 117 times.
238 ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
234 119 ogg_key_granule(oggstream, granule))) {
235
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (oggstream->page.granule != -1 &&
236 ✗ (ret = ogg_buffer_page(s, oggstream)) < 0)
237 ✗ return ret;
238 2 flush = 1;
239 }
240
241 // avoid a continued page
242
4/4
✓ Branch 0 taken 1093 times.
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 1055 times.
✓ Branch 3 taken 38 times.
1134 if (!header && oggstream->page.size > 0 &&
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1055 times.
1055 MAX_PAGE_SIZE - oggstream->page.size < size) {
244 ✗ if ((ret = ogg_buffer_page(s, oggstream)) < 0)
245 ✗ return ret;
246 }
247
248
2/2
✓ Branch 0 taken 1139 times.
✓ Branch 1 taken 1134 times.
2273 for (i = 0; i < total_segments; ) {
249 1139 OGGPage *page = &oggstream->page;
250
251 1139 segments = FFMIN(total_segments - i, 255 - page->segments_count);
252
253
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1134 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
1139 if (i && !page->segments_count)
254 5 page->flags |= 1; // continued packet
255
256 1139 memset(page->segments+page->segments_count, 255, segments - 1);
257 1139 page->segments_count += segments - 1;
258
259 1139 len = FFMIN(size, segments*255);
260 1139 page->segments[page->segments_count++] = len - (segments-1)*255;
261
1/2
✓ Branch 0 taken 1139 times.
✗ Branch 1 not taken.
1139 if (len)
262 1139 memcpy(page->data+page->size, p, len);
263 1139 p += len;
264 1139 size -= len;
265 1139 i += segments;
266 1139 page->size += len;
267
268
2/2
✓ Branch 0 taken 1134 times.
✓ Branch 1 taken 5 times.
1139 if (i == total_segments)
269 1134 page->granule = granule;
270
271 {
272 1139 AVRational time_base = s->streams[page->stream_index]->time_base;
273
274 1139 int64_t start = av_rescale_q(page->start_granule, time_base,
275 1139 AV_TIME_BASE_Q);
276 2278 int64_t next = av_rescale_q(ogg_granule_to_timestamp(oggstream, page->granule),
277 1139 time_base, AV_TIME_BASE_Q);
278
279
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1134 times.
1139 if (page->segments_count == 255) {
280
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
281 ✗ return ret;
282
2/2
✓ Branch 0 taken 1093 times.
✓ Branch 1 taken 41 times.
1134 } else if (!header) {
283
3/4
✓ Branch 0 taken 1093 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 1073 times.
1093 if (ogg->pref_duration > 0 && next - start >= ogg->pref_duration) {
284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if ((ret = ogg_buffer_page(s, oggstream)) < 0)
285 ✗ return ret;
286 }
287 }
288 }
289 }
290
291
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1132 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
1136 if (flush && oggstream->page.granule != -1 &&
292 2 (ret = ogg_buffer_page(s, oggstream)) < 0)
293 ✗ return ret;
294
295 1134 return 0;
296 }
297
298 17 static uint8_t *ogg_write_vorbiscomment(int64_t offset, int bitexact,
299 int *header_len, AVDictionary **m, int framing_bit,
300 AVChapter **chapters, unsigned int nb_chapters)
301 {
302
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7 times.
17 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
303 FFIOContext pb;
304 int64_t size;
305 uint8_t *p;
306
307 17 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
308
309 17 size = ff_vorbiscomment_length(*m, vendor, chapters, nb_chapters);
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (size < 0)
311 ✗ return NULL;
312 17 size += offset + framing_bit;
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (size > INT_MAX)
314 ✗ return NULL;
315 17 p = av_mallocz(size);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!p)
317 ✗ return NULL;
318
319 17 ffio_init_write_context(&pb, p + offset, size - offset);
320 17 ff_vorbiscomment_write(&pb.pub, *m, vendor, chapters, nb_chapters);
321
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 11 times.
17 if (framing_bit)
322 6 avio_w8(&pb.pub, 1);
323
324 17 *header_len = size;
325 17 return p;
326 }
327
328 7 static int ogg_build_flac_headers(AVCodecParameters *par,
329 OGGStreamContext *oggstream, int bitexact,
330 AVDictionary **m)
331 {
332 uint8_t *p;
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (par->extradata_size < FLAC_STREAMINFO_SIZE)
335 ✗ return AVERROR(EINVAL);
336
337 // first packet: STREAMINFO
338 7 oggstream->header_len[0] = 51;
339 7 oggstream->header[0] = av_mallocz(51); // per ogg flac specs
340 7 p = oggstream->header[0];
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!p)
342 ✗ return AVERROR(ENOMEM);
343 7 bytestream_put_byte(&p, 0x7F);
344 7 bytestream_put_buffer(&p, "FLAC", 4);
345 7 bytestream_put_byte(&p, 1); // major version
346 7 bytestream_put_byte(&p, 0); // minor version
347 7 bytestream_put_be16(&p, 1); // headers packets without this one
348 7 bytestream_put_buffer(&p, "fLaC", 4);
349 7 bytestream_put_byte(&p, 0x00); // streaminfo
350 7 bytestream_put_be24(&p, 34);
351 7 bytestream_put_buffer(&p, par->extradata, FLAC_STREAMINFO_SIZE);
352
353 // second packet: VorbisComment
354 7 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!p)
356 ✗ return AVERROR(ENOMEM);
357 7 oggstream->header[1] = p;
358 7 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
359 7 bytestream_put_be24(&p, oggstream->header_len[1] - 4);
360
361 7 return 0;
362 }
363
364 #define SPEEX_HEADER_SIZE 80
365
366 ✗ static int ogg_build_speex_headers(AVCodecParameters *par,
367 OGGStreamContext *oggstream, int bitexact,
368 AVDictionary **m)
369 {
370 uint8_t *p;
371
372 ✗ if (par->extradata_size < SPEEX_HEADER_SIZE)
373 ✗ return AVERROR_INVALIDDATA;
374
375 // first packet: Speex header
376 ✗ p = av_mallocz(SPEEX_HEADER_SIZE);
377 ✗ if (!p)
378 ✗ return AVERROR(ENOMEM);
379 ✗ oggstream->header[0] = p;
380 ✗ oggstream->header_len[0] = SPEEX_HEADER_SIZE;
381 ✗ bytestream_put_buffer(&p, par->extradata, SPEEX_HEADER_SIZE);
382 ✗ AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
383
384 // second packet: VorbisComment
385 ✗ p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0, NULL, 0);
386 ✗ if (!p)
387 ✗ return AVERROR(ENOMEM);
388 ✗ oggstream->header[1] = p;
389
390 ✗ return 0;
391 }
392
393 #define OPUS_HEADER_SIZE 19
394
395 2 static int ogg_build_opus_headers(AVCodecParameters *par,
396 OGGStreamContext *oggstream, int bitexact,
397 AVDictionary **m, AVChapter **chapters,
398 unsigned int nb_chapters)
399 {
400 uint8_t *p;
401
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (par->extradata_size < OPUS_HEADER_SIZE)
403 ✗ return AVERROR_INVALIDDATA;
404
405 /* first packet: Opus header */
406 2 p = av_mallocz(par->extradata_size);
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!p)
408 ✗ return AVERROR(ENOMEM);
409 2 oggstream->header[0] = p;
410 2 oggstream->header_len[0] = par->extradata_size;
411 2 bytestream_put_buffer(&p, par->extradata, par->extradata_size);
412
413 /* second packet: VorbisComment */
414 2 p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0, chapters, nb_chapters);
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!p)
416 ✗ return AVERROR(ENOMEM);
417 2 oggstream->header[1] = p;
418 2 bytestream_put_buffer(&p, "OpusTags", 8);
419
420 2 return 0;
421 }
422
423 #define VP8_HEADER_SIZE 26
424
425 1 static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st,
426 OGGStreamContext *oggstream, int bitexact)
427 {
428 1 AVCodecParameters *par = st->codecpar;
429 uint8_t *p;
430
431 /* first packet: VP8 header */
432 1 p = av_mallocz(VP8_HEADER_SIZE);
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!p)
434 ✗ return AVERROR(ENOMEM);
435 1 oggstream->header[0] = p;
436 1 oggstream->header_len[0] = VP8_HEADER_SIZE;
437 1 bytestream_put_byte(&p, 0x4f); // HDRID
438 1 bytestream_put_buffer(&p, "VP80", 4); // Identifier
439 1 bytestream_put_byte(&p, 1); // HDRTYP
440 1 bytestream_put_byte(&p, 1); // VMAJ
441 1 bytestream_put_byte(&p, 0); // VMIN
442 1 bytestream_put_be16(&p, par->width);
443 1 bytestream_put_be16(&p, par->height);
444 1 bytestream_put_be24(&p, par->sample_aspect_ratio.num);
445 1 bytestream_put_be24(&p, par->sample_aspect_ratio.den);
446
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
447 // OggVP8 requires pts to increase by 1 per visible frame, so use the least common
448 // multiple framerate if available.
449 1 av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
450 st->time_base.num, st->time_base.den,
451 st->r_frame_rate.den, st->r_frame_rate.num);
452 1 avpriv_set_pts_info(st, 64, st->r_frame_rate.den, st->r_frame_rate.num);
453 }
454 1 bytestream_put_be32(&p, st->time_base.den);
455 1 bytestream_put_be32(&p, st->time_base.num);
456
457 /* optional second packet: VorbisComment */
458
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_dict_count(st->metadata)) {
459 1 p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0, NULL, 0);
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!p)
461 ✗ return AVERROR(ENOMEM);
462 1 oggstream->header[1] = p;
463 1 bytestream_put_byte(&p, 0x4f); // HDRID
464 1 bytestream_put_buffer(&p, "VP80", 4); // Identifier
465 1 bytestream_put_byte(&p, 2); // HDRTYP
466 1 bytestream_put_byte(&p, 0x20);
467 }
468
469 1 oggstream->isvp8 = 1;
470
471 1 return 0;
472 }
473
474 1125 static void ogg_write_pages(AVFormatContext *s, int flush)
475 {
476 1125 OGGContext *ogg = s->priv_data;
477 OGGPageList *next, *p;
478
479
2/2
✓ Branch 0 taken 197 times.
✓ Branch 1 taken 928 times.
1125 if (!ogg->page_list)
480 197 return;
481
482
2/2
✓ Branch 0 taken 973 times.
✓ Branch 1 taken 32 times.
1005 for (p = ogg->page_list; p; ) {
483 973 OGGStreamContext *oggstream =
484 973 s->streams[p->page.stream_index]->priv_data;
485
4/4
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 896 times.
✓ Branch 3 taken 34 times.
973 if (oggstream->page_count < 2 && !flush)
486 896 break;
487
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 54 times.
100 ogg_write_page(s, &p->page,
488
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6 times.
23 flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
489 77 next = p->next;
490 77 av_freep(&p);
491 77 p = next;
492 }
493 928 ogg->page_list = p;
494 }
495
496 /**
497 * Derive a bitexact serial number from the stream's header packets, which
498 * makes links muxed apart unlikely to share one, though not certain not to.
499 */
500 8 static unsigned ogg_bitexact_serial(const OGGStreamContext *oggstream, unsigned seed)
501 {
502 8 const AVCRC *table = av_crc_get_table(AV_CRC_32_IEEE);
503 8 uint32_t crc = seed;
504
505
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 for (int i = 0; i < 3; i++)
506
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 4 times.
24 if (oggstream->header_len[i])
507 20 crc = av_crc(table, crc, oggstream->header[i], oggstream->header_len[i]);
508
509 8 return crc;
510 }
511
512 17 static int ogg_serial_in_use(AVFormatContext *s, int nb_streams, unsigned serial_num)
513 {
514
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
18 for (int i = 0; i < nb_streams; i++) {
515 1 const OGGStreamContext *oggstream = s->streams[i]->priv_data;
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (oggstream->serial_num == serial_num)
517 ✗ return 1;
518 }
519 17 return 0;
520 }
521
522 // This function can be used on an initialized context to reinitialize the
523 // streams.
524 16 static int ogg_init(AVFormatContext *s)
525 {
526 16 OGGContext *ogg = s->priv_data;
527 16 OGGStreamContext *oggstream = NULL;
528 int i;
529
530
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 16 times.
33 for (i = 0; i < s->nb_streams; i++) {
531 17 AVStream *st = s->streams[i];
532 unsigned serial_num;
533
534
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
535
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
536 /* Opus requires a fixed 48kHz clock */
537 2 avpriv_set_pts_info(st, 64, 1, 48000);
538 else
539 13 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
540 }
541
542
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6 times.
17 if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
543
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 st->codecpar->codec_id != AV_CODEC_ID_THEORA &&
544
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 st->codecpar->codec_id != AV_CODEC_ID_SPEEX &&
545
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7 times.
10 st->codecpar->codec_id != AV_CODEC_ID_FLAC &&
546
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 st->codecpar->codec_id != AV_CODEC_ID_OPUS &&
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 st->codecpar->codec_id != AV_CODEC_ID_VP8) {
548 ✗ av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
549 ✗ return AVERROR(EINVAL);
550 }
551
552
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
17 if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 st->codecpar->codec_id != AV_CODEC_ID_VP8) {
554 ✗ av_log(s, AV_LOG_ERROR, "No extradata present\n");
555 ✗ return AVERROR_INVALIDDATA;
556 }
557 17 oggstream = st->priv_data;
558
559
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5 times.
17 if (!oggstream) {
560 12 oggstream = av_mallocz(sizeof(*oggstream));
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!oggstream)
562 ✗ return AVERROR(ENOMEM);
563 12 st->priv_data = oggstream;
564 } else {
565 5 ogg_cleanup_stream(st);
566 5 memset(oggstream, 0, sizeof(*oggstream));
567 }
568
569 17 oggstream->page.stream_index = i;
570
571 17 av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
572
573
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
17 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC) {
574 7 int err = ogg_build_flac_headers(st->codecpar, oggstream,
575 7 s->flags & AVFMT_FLAG_BITEXACT,
576 &st->metadata);
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (err) {
578 ✗ av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
579 ✗ return err;
580 }
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 } else if (st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
582 ✗ int err = ogg_build_speex_headers(st->codecpar, oggstream,
583 ✗ s->flags & AVFMT_FLAG_BITEXACT,
584 &st->metadata);
585 ✗ if (err) {
586 ✗ av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
587 ✗ return err;
588 }
589
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
590 2 int err = ogg_build_opus_headers(st->codecpar, oggstream,
591 2 s->flags & AVFMT_FLAG_BITEXACT,
592 &st->metadata, s->chapters, s->nb_chapters);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err) {
594 ✗ av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
595 ✗ return err;
596 }
597
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
598 1 int err = ogg_build_vp8_headers(s, st, oggstream,
599 1 s->flags & AVFMT_FLAG_BITEXACT);
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err) {
601 ✗ av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
602 ✗ return err;
603 }
604 } else {
605 uint8_t *p;
606
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 const char *cstr = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
607
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 int header_type = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
608 7 int framing_bit = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
609
610
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (avpriv_split_xiph_headers(st->codecpar->extradata, st->codecpar->extradata_size,
611 7 st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
612
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 (const uint8_t**)oggstream->header, oggstream->header_len) < 0) {
613 ✗ av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
614 ✗ oggstream->header[1] = NULL;
615 ✗ return AVERROR_INVALIDDATA;
616 }
617
618 7 p = ogg_write_vorbiscomment(7, s->flags & AVFMT_FLAG_BITEXACT,
619 &oggstream->header_len[1], &st->metadata,
620 framing_bit, NULL, 0);
621 7 oggstream->header[1] = p;
622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!p)
623 ✗ return AVERROR(ENOMEM);
624
625 7 bytestream_put_byte(&p, header_type);
626 7 bytestream_put_buffer(&p, cstr, 6);
627
628
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
629 1 int den = AV_RB32(oggstream->header[0] + 22), num = AV_RB32(oggstream->header[0] + 26);
630 /* Make sure to use time base stored in the Theora stream header to write
631 correct timestamps */
632
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (st->time_base.num != num || st->time_base.den != den) {
633 1 av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
634 st->time_base.num, st->time_base.den, num, den);
635 1 avpriv_set_pts_info(st, 64, num, den);
636 }
637 /** KFGSHIFT is the width of the less significant section of the granule position
638 The less significant section is the frame count since the last keyframe */
639 1 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
640 1 oggstream->vrev = oggstream->header[0][9];
641 1 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
642 oggstream->kfgshift, oggstream->vrev);
643 }
644 }
645
646
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
17 if (s->flags & AVFMT_FLAG_BITEXACT && ogg->serial_offset >= 0)
647 2 serial_num = i + ogg->serial_offset;
648
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
15 else if (s->flags & AVFMT_FLAG_BITEXACT)
649 8 serial_num = ogg_bitexact_serial(oggstream, i);
650 else
651 7 serial_num = av_get_random_seed();
652
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 while (ogg_serial_in_use(s, i, serial_num))
653 ✗ serial_num = s->flags & AVFMT_FLAG_BITEXACT ? serial_num + 1
654 ✗ : av_get_random_seed();
655 17 oggstream->serial_num = serial_num;
656 }
657
658 16 return 0;
659 }
660
661 16 static int ogg_write_header(AVFormatContext *s)
662 {
663 16 OGGStreamContext *oggstream = NULL;
664 int i, j, ret;
665
666
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 16 times.
33 for (j = 0; j < s->nb_streams; j++) {
667 17 oggstream = s->streams[j]->priv_data;
668 17 ret = ogg_buffer_data(s, s->streams[j], oggstream->header[0],
669 17 oggstream->header_len[0], 0, 1);
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
671 ✗ return ret;
672 17 oggstream->page.flags |= 2; // bos
673 17 ret = ogg_buffer_page(s, oggstream);
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
675 ✗ return ret;
676 }
677
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 16 times.
33 for (j = 0; j < s->nb_streams; j++) {
678 17 AVStream *st = s->streams[j];
679 17 oggstream = st->priv_data;
680
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 17 times.
51 for (i = 1; i < 3; i++) {
681
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
34 if (oggstream->header_len[i]) {
682 24 ret = ogg_buffer_data(s, st, oggstream->header[i],
683 24 oggstream->header_len[i], 0, 1);
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
685 ✗ return ret;
686 }
687 }
688 17 ret = ogg_buffer_page(s, oggstream);
689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
690 ✗ return ret;
691 }
692
693 16 oggstream->page.start_granule = AV_NOPTS_VALUE;
694
695 16 ogg_write_pages(s, 2);
696
697 16 return 0;
698 }
699
700 1093 static int ogg_check_new_metadata(AVFormatContext *s, AVPacket *pkt)
701 {
702 1093 int ret = 0;
703 size_t size;
704 1093 OGGContext *oggcontext = s->priv_data;
705 1093 AVStream *st = s->streams[pkt->stream_index];
706 1093 OGGStreamContext *oggstream = st->priv_data;
707 1093 const uint8_t *side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
708
709
2/2
✓ Branch 0 taken 1087 times.
✓ Branch 1 taken 6 times.
1093 if (!side_metadata)
710 1087 return 0;
711
712 // Don't restart on first packet.
713
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (!oggstream->packet_seen)
714 1 return 0;
715
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (s->nb_streams > 1) {
717 ✗ av_log(s, AV_LOG_WARNING, "Multiple streams present: cannot insert new metadata!\n");
718 ✗ return 0;
719 }
720
721
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
722
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 st->codecpar->codec_id != AV_CODEC_ID_FLAC &&
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 st->codecpar->codec_id != AV_CODEC_ID_OPUS) {
724 ✗ av_log(s, AV_LOG_WARNING, "Inserting metadata is only supported for vorbis, flac and opus streams!\n");
725 ✗ return 0;
726 }
727
728 5 ret = ogg_write_trailer(s);
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
730 ✗ goto end;
731
732 5 av_dict_free(&st->metadata);
733 5 ret = av_packet_unpack_dictionary(side_metadata, size, &st->metadata);
734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
735 ✗ goto end;
736
737 5 ret = ogg_init(s);
738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
739 ✗ goto end;
740
741 5 ret = ogg_write_header(s);
742
743 5 end:
744 5 oggcontext->failed = ret < 0;
745 5 return ret;
746 }
747
748 1093 static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
749 {
750 1093 AVStream *st = s->streams[pkt->stream_index];
751 1093 OGGContext *oggcontext = s->priv_data;
752 1093 OGGStreamContext *oggstream = st->priv_data;
753 int ret;
754 int64_t granule;
755
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1093 times.
1093 if (oggcontext->failed)
757 ✗ return AVERROR_INVALIDDATA;
758
759 1093 ret = ogg_check_new_metadata(s, pkt);
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1093 times.
1093 if (ret < 0)
761 ✗ return ret;
762
763
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1085 times.
1093 if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) {
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
765 int pframe_count;
766
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (pkt->flags & AV_PKT_FLAG_KEY)
767 1 oggstream->last_kf_pts = pts;
768 8 pframe_count = pts - oggstream->last_kf_pts;
769 // prevent frame count from overflow if key frame flag is not set
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (pframe_count >= (1<<oggstream->kfgshift)) {
771 ✗ oggstream->last_kf_pts += pframe_count;
772 ✗ pframe_count = 0;
773 }
774 8 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
775
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1073 times.
1085 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
776 12 granule = pkt->pts + pkt->duration +
777 12 av_rescale_q(st->codecpar->initial_padding,
778 12 (AVRational){ 1, st->codecpar->sample_rate },
779 st->time_base);
780
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 962 times.
1073 else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
781 int64_t pts, invcnt, dist;
782 int visible;
783
784 111 visible = (pkt->data[0] >> 4) & 1;
785 111 pts = pkt->pts + pkt->duration;
786 111 invcnt = (oggstream->last_granule >> 30) & 3;
787
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 107 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
111 invcnt = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
788
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 1 times.
111 dist = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
789
790 111 granule = (pts << 32) | (invcnt << 30) | (dist << 3);
791 } else
792 962 granule = pkt->pts + pkt->duration;
793
794
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1077 times.
1093 if (oggstream->page.start_granule == AV_NOPTS_VALUE)
795 16 oggstream->page.start_granule = pkt->pts;
796
797 1093 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1093 times.
1093 if (ret < 0)
799 ✗ return ret;
800
801 1093 ogg_write_pages(s, 0);
802
803 1093 oggstream->last_granule = granule;
804 1093 oggstream->packet_seen = 1;
805
806 1093 return 0;
807 }
808
809 1094 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
810 {
811 int i, ret;
812
813
1/2
✓ Branch 0 taken 1094 times.
✗ Branch 1 not taken.
1094 if (pkt)
814
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1093 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1094 return pkt->size || !pkt->side_data_elems ? ogg_write_packet_internal(s, pkt) : 0;
815
816 ✗ for (i = 0; i < s->nb_streams; i++) {
817 ✗ OGGStreamContext *oggstream = s->streams[i]->priv_data;
818 ✗ if (oggstream->page.segments_count &&
819 ✗ (ret = ogg_buffer_page(s, oggstream)) < 0)
820 ✗ return ret;
821 }
822
823 ✗ ogg_write_pages(s, 2);
824 ✗ return 1;
825 }
826
827 16 static int ogg_write_trailer(AVFormatContext *s)
828 {
829 int i, ret;
830
831 /* flush current page if needed */
832
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 16 times.
33 for (i = 0; i < s->nb_streams; i++) {
833 17 OGGStreamContext *oggstream = s->streams[i]->priv_data;
834
835
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
33 if (oggstream->page.segments_count &&
836 16 (ret = ogg_buffer_page(s, oggstream)) < 0)
837 ✗ return ret;
838 }
839
840 16 ogg_write_pages(s, 1);
841
842 16 return 0;
843 }
844
845 11 static void ogg_free(AVFormatContext *s)
846 {
847 11 OGGContext *ogg = s->priv_data;
848 11 OGGPageList *p = ogg->page_list;
849 int i;
850
851
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
23 for (i = 0; i < s->nb_streams; i++) {
852 12 AVStream *st = s->streams[i];
853
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (st->priv_data)
854 12 ogg_cleanup_stream(st);
855 }
856
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 while (p) {
858 ✗ OGGPageList *next = p->next;
859 ✗ av_free(p);
860 ✗ p = next;
861 }
862 11 ogg->page_list = NULL;
863 11 }
864
865 #if CONFIG_OGG_MUXER
866 const FFOutputFormat ff_ogg_muxer = {
867 .p.name = "ogg",
868 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg"),
869 .p.mime_type = "application/ogg",
870 .p.extensions = "ogg"
871 #if !CONFIG_OGV_MUXER
872 ",ogv"
873 #endif
874 #if !CONFIG_SPX_MUXER
875 ",spx"
876 #endif
877 #if !CONFIG_OPUS_MUXER
878 ",opus"
879 #endif
880 ,
881 .priv_data_size = sizeof(OGGContext),
882 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
883 AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
884 .p.video_codec = AV_CODEC_ID_THEORA,
885 .init = ogg_init,
886 .write_header = ogg_write_header,
887 .write_packet = ogg_write_packet,
888 .write_trailer = ogg_write_trailer,
889 .deinit = ogg_free,
890 .p.flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT,
891 .p.priv_class = &ogg_muxer_class,
892 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
893 };
894 #endif
895
896 #if CONFIG_OGA_MUXER
897 const FFOutputFormat ff_oga_muxer = {
898 .p.name = "oga",
899 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
900 .p.mime_type = "audio/ogg",
901 .p.extensions = "oga",
902 .priv_data_size = sizeof(OGGContext),
903 .p.audio_codec = AV_CODEC_ID_FLAC,
904 .init = ogg_init,
905 .write_header = ogg_write_header,
906 .write_packet = ogg_write_packet,
907 .write_trailer = ogg_write_trailer,
908 .deinit = ogg_free,
909 .p.flags = AVFMT_TS_NEGATIVE,
910 .p.priv_class = &ogg_muxer_class,
911 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
912 };
913 #endif
914
915 #if CONFIG_OGV_MUXER
916 const FFOutputFormat ff_ogv_muxer = {
917 .p.name = "ogv",
918 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Video"),
919 .p.mime_type = "video/ogg",
920 .p.extensions = "ogv",
921 .priv_data_size = sizeof(OGGContext),
922 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
923 AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
924 .p.video_codec = CONFIG_LIBTHEORA_ENCODER ?
925 AV_CODEC_ID_THEORA : AV_CODEC_ID_VP8,
926 .init = ogg_init,
927 .write_header = ogg_write_header,
928 .write_packet = ogg_write_packet,
929 .write_trailer = ogg_write_trailer,
930 .deinit = ogg_free,
931 .p.flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT,
932 .p.priv_class = &ogg_muxer_class,
933 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
934 };
935 #endif
936
937 #if CONFIG_SPX_MUXER
938 const FFOutputFormat ff_spx_muxer = {
939 .p.name = "spx",
940 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
941 .p.mime_type = "audio/ogg",
942 .p.extensions = "spx",
943 .priv_data_size = sizeof(OGGContext),
944 .p.audio_codec = AV_CODEC_ID_SPEEX,
945 .init = ogg_init,
946 .write_header = ogg_write_header,
947 .write_packet = ogg_write_packet,
948 .write_trailer = ogg_write_trailer,
949 .deinit = ogg_free,
950 .p.flags = AVFMT_TS_NEGATIVE,
951 .p.priv_class = &ogg_muxer_class,
952 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
953 };
954 #endif
955
956 #if CONFIG_OPUS_MUXER
957 const FFOutputFormat ff_opus_muxer = {
958 .p.name = "opus",
959 .p.long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
960 .p.mime_type = "audio/ogg",
961 .p.extensions = "opus",
962 .priv_data_size = sizeof(OGGContext),
963 .p.audio_codec = AV_CODEC_ID_OPUS,
964 .init = ogg_init,
965 .write_header = ogg_write_header,
966 .write_packet = ogg_write_packet,
967 .write_trailer = ogg_write_trailer,
968 .deinit = ogg_free,
969 .p.flags = AVFMT_TS_NEGATIVE,
970 .p.priv_class = &ogg_muxer_class,
971 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
972 };
973 #endif
974