FFmpeg coverage


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