FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/oggenc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 317 401 79.1%
Functions: 17 19 89.5%
Branches: 166 240 69.2%

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