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