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