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