| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | * muxing functions for use within FFmpeg | ||
| 3 | * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | ||
| 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 "avformat.h" | ||
| 23 | #include "avformat_internal.h" | ||
| 24 | #include "internal.h" | ||
| 25 | #include "mux.h" | ||
| 26 | #include "version.h" | ||
| 27 | #include "libavcodec/bsf.h" | ||
| 28 | #include "libavcodec/codec_desc.h" | ||
| 29 | #include "libavcodec/internal.h" | ||
| 30 | #include "libavcodec/packet_internal.h" | ||
| 31 | #include "libavutil/mem.h" | ||
| 32 | #include "libavutil/opt.h" | ||
| 33 | #include "libavutil/dict.h" | ||
| 34 | #include "libavutil/timestamp.h" | ||
| 35 | #include "libavutil/avassert.h" | ||
| 36 | #include "libavutil/frame.h" | ||
| 37 | #include "libavutil/internal.h" | ||
| 38 | #include "libavutil/mathematics.h" | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @file | ||
| 42 | * muxing functions for use within libavformat | ||
| 43 | */ | ||
| 44 | |||
| 45 | /* fraction handling */ | ||
| 46 | |||
| 47 | /** | ||
| 48 | * f = val + (num / den) + 0.5. | ||
| 49 | * | ||
| 50 | * 'num' is normalized so that it is such as 0 <= num < den. | ||
| 51 | * | ||
| 52 | * @param f fractional number | ||
| 53 | * @param val integer value | ||
| 54 | * @param num must be >= 0 | ||
| 55 | * @param den must be >= 1 | ||
| 56 | */ | ||
| 57 | 9077 | static void frac_init(FFFrac *f, int64_t val, int64_t num, int64_t den) | |
| 58 | { | ||
| 59 | 9077 | num += (den >> 1); | |
| 60 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 9077 times. 
         | 
      9077 | if (num >= den) { | 
| 61 | ✗ | val += num / den; | |
| 62 | ✗ | num = num % den; | |
| 63 | } | ||
| 64 | 9077 | f->val = val; | |
| 65 | 9077 | f->num = num; | |
| 66 | 9077 | f->den = den; | |
| 67 | 9077 | } | |
| 68 | |||
| 69 | /** | ||
| 70 | * Fractional addition to f: f = f + (incr / f->den). | ||
| 71 | * | ||
| 72 | * @param f fractional number | ||
| 73 | * @param incr increment, can be positive or negative | ||
| 74 | */ | ||
| 75 | 528281 | static void frac_add(FFFrac *f, int64_t incr) | |
| 76 | { | ||
| 77 | int64_t num, den; | ||
| 78 | |||
| 79 | 528281 | num = f->num + incr; | |
| 80 | 528281 | den = f->den; | |
| 81 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 528281 times. 
         | 
      528281 | if (num < 0) { | 
| 82 | ✗ | f->val += num / den; | |
| 83 | ✗ | num = num % den; | |
| 84 | ✗ | if (num < 0) { | |
| 85 | ✗ | num += den; | |
| 86 | ✗ | f->val--; | |
| 87 | } | ||
| 88 | 
        2/2✓ Branch 0 taken 511503 times. 
          ✓ Branch 1 taken 16778 times. 
         | 
      528281 | } else if (num >= den) { | 
| 89 | 511503 | f->val += num / den; | |
| 90 | 511503 | num = num % den; | |
| 91 | } | ||
| 92 | 528281 | f->num = num; | |
| 93 | 528281 | } | |
| 94 | |||
| 95 | 8657 | int avformat_alloc_output_context2(AVFormatContext **avctx, const AVOutputFormat *oformat, | |
| 96 | const char *format, const char *filename) | ||
| 97 | { | ||
| 98 | 8657 | AVFormatContext *s = avformat_alloc_context(); | |
| 99 | 8657 | int ret = 0; | |
| 100 | |||
| 101 | 8657 | *avctx = NULL; | |
| 102 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 8657 times. 
         | 
      8657 | if (!s) | 
| 103 | ✗ | goto nomem; | |
| 104 | |||
| 105 | 
        2/2✓ Branch 0 taken 8627 times. 
          ✓ Branch 1 taken 30 times. 
         | 
      8657 | if (!oformat) { | 
| 106 | 
        2/2✓ Branch 0 taken 8447 times. 
          ✓ Branch 1 taken 180 times. 
         | 
      8627 | if (format) { | 
| 107 | 8447 | oformat = av_guess_format(format, NULL, NULL); | |
| 108 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 8447 times. 
         | 
      8447 | if (!oformat) { | 
| 109 | ✗ | av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not known.\n", format); | |
| 110 | ✗ | ret = AVERROR(EINVAL); | |
| 111 | ✗ | goto error; | |
| 112 | } | ||
| 113 | } else { | ||
| 114 | 180 | oformat = av_guess_format(NULL, filename, NULL); | |
| 115 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 180 times. 
         | 
      180 | if (!oformat) { | 
| 116 | ✗ | ret = AVERROR(EINVAL); | |
| 117 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 118 | "Unable to choose an output format for '%s'; " | ||
| 119 | "use a standard extension for the filename or specify " | ||
| 120 | "the format manually.\n", filename); | ||
| 121 | ✗ | goto error; | |
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | 8657 | s->oformat = oformat; | |
| 127 | 
        2/2✓ Branch 1 taken 4847 times. 
          ✓ Branch 2 taken 3810 times. 
         | 
      8657 | if (ffofmt(s->oformat)->priv_data_size > 0) { | 
| 128 | 4847 | s->priv_data = av_mallocz(ffofmt(s->oformat)->priv_data_size); | |
| 129 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 4847 times. 
         | 
      4847 | if (!s->priv_data) | 
| 130 | ✗ | goto nomem; | |
| 131 | 
        2/2✓ Branch 0 taken 4525 times. 
          ✓ Branch 1 taken 322 times. 
         | 
      4847 | if (s->oformat->priv_class) { | 
| 132 | 4525 | *(const AVClass**)s->priv_data= s->oformat->priv_class; | |
| 133 | 4525 | av_opt_set_defaults(s->priv_data); | |
| 134 | } | ||
| 135 | } else | ||
| 136 | 3810 | s->priv_data = NULL; | |
| 137 | |||
| 138 | 
        2/2✓ Branch 0 taken 8605 times. 
          ✓ Branch 1 taken 52 times. 
         | 
      8657 | if (filename) { | 
| 139 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 8605 times. 
         | 
      8605 | if (!(s->url = av_strdup(filename))) | 
| 140 | ✗ | goto nomem; | |
| 141 | |||
| 142 | } | ||
| 143 | 8657 | *avctx = s; | |
| 144 | 8657 | return 0; | |
| 145 | ✗ | nomem: | |
| 146 | ✗ | av_log(s, AV_LOG_ERROR, "Out of memory\n"); | |
| 147 | ✗ | ret = AVERROR(ENOMEM); | |
| 148 | ✗ | error: | |
| 149 | ✗ | avformat_free_context(s); | |
| 150 | ✗ | return ret; | |
| 151 | } | ||
| 152 | |||
| 153 | 2315 | static int validate_codec_tag(const AVFormatContext *s, const AVStream *st) | |
| 154 | { | ||
| 155 | const AVCodecTag *avctag; | ||
| 156 | 2315 | enum AVCodecID id = AV_CODEC_ID_NONE; | |
| 157 | 2315 | unsigned uppercase_tag = ff_toupper4(st->codecpar->codec_tag); | |
| 158 | 2315 | int64_t tag = -1; | |
| 159 | |||
| 160 | /** | ||
| 161 | * Check that tag + id is in the table | ||
| 162 | * If neither is in the table -> OK | ||
| 163 | * If tag is in the table with another id -> FAIL | ||
| 164 | * If id is in the table with another tag -> FAIL unless strict < normal | ||
| 165 | */ | ||
| 166 | 
        2/2✓ Branch 0 taken 3368 times. 
          ✓ Branch 1 taken 36 times. 
         | 
      3404 | for (int n = 0; s->oformat->codec_tag[n]; n++) { | 
| 167 | 3368 | avctag = s->oformat->codec_tag[n]; | |
| 168 | 
        2/2✓ Branch 0 taken 300321 times. 
          ✓ Branch 1 taken 1089 times. 
         | 
      301410 | while (avctag->id != AV_CODEC_ID_NONE) { | 
| 169 | 
        2/2✓ Branch 1 taken 2279 times. 
          ✓ Branch 2 taken 298042 times. 
         | 
      300321 | if (ff_toupper4(avctag->tag) == uppercase_tag) { | 
| 170 | 2279 | id = avctag->id; | |
| 171 | 
        1/2✓ Branch 0 taken 2279 times. 
          ✗ Branch 1 not taken. 
         | 
      2279 | if (id == st->codecpar->codec_id) | 
| 172 | 2279 | return 1; | |
| 173 | } | ||
| 174 | 
        2/2✓ Branch 0 taken 191058 times. 
          ✓ Branch 1 taken 106984 times. 
         | 
      298042 | if (avctag->id == st->codecpar->codec_id) | 
| 175 | 191058 | tag = avctag->tag; | |
| 176 | 298042 | avctag++; | |
| 177 | } | ||
| 178 | } | ||
| 179 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 36 times. 
         | 
      36 | if (id != AV_CODEC_ID_NONE) | 
| 180 | ✗ | return 0; | |
| 181 | 
        3/4✓ Branch 0 taken 27 times. 
          ✓ Branch 1 taken 9 times. 
          ✓ Branch 2 taken 27 times. 
          ✗ Branch 3 not taken. 
         | 
      36 | if (tag >= 0 && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL)) | 
| 182 | 27 | return 0; | |
| 183 | 9 | return 1; | |
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | 8732 | static int init_muxer(AVFormatContext *s, AVDictionary **options) | |
| 188 | { | ||
| 189 | 8732 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 190 | 8732 | AVDictionary *tmp = NULL; | |
| 191 | 8732 | const FFOutputFormat *of = ffofmt(s->oformat); | |
| 192 | AVDictionaryEntry *e; | ||
| 193 | static const unsigned default_codec_offsets[] = { | ||
| 194 | [AVMEDIA_TYPE_VIDEO] = offsetof(AVOutputFormat, video_codec), | ||
| 195 | [AVMEDIA_TYPE_AUDIO] = offsetof(AVOutputFormat, audio_codec), | ||
| 196 | [AVMEDIA_TYPE_SUBTITLE] = offsetof(AVOutputFormat, subtitle_codec), | ||
| 197 | }; | ||
| 198 | 8732 | unsigned nb_type[FF_ARRAY_ELEMS(default_codec_offsets)] = { 0 }; | |
| 199 | 8732 | int ret = 0; | |
| 200 | |||
| 201 | 
        2/2✓ Branch 0 taken 8704 times. 
          ✓ Branch 1 taken 28 times. 
         | 
      8732 | if (options) | 
| 202 | 8704 | av_dict_copy(&tmp, *options, 0); | |
| 203 | |||
| 204 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 8732 times. 
         | 
      8732 | if ((ret = av_opt_set_dict(s, &tmp)) < 0) | 
| 205 | ✗ | goto fail; | |
| 206 | 
        6/8✓ Branch 0 taken 4847 times. 
          ✓ Branch 1 taken 3885 times. 
          ✓ Branch 2 taken 4525 times. 
          ✓ Branch 3 taken 322 times. 
          ✓ Branch 4 taken 4525 times. 
          ✗ Branch 5 not taken. 
          ✗ Branch 6 not taken. 
          ✓ Branch 7 taken 4525 times. 
         | 
      13257 | if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class && | 
| 207 | 4525 | (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0) | |
| 208 | ✗ | goto fail; | |
| 209 | |||
| 210 | 
        3/4✓ Branch 0 taken 58 times. 
          ✓ Branch 1 taken 8674 times. 
          ✗ Branch 3 not taken. 
          ✓ Branch 4 taken 58 times. 
         | 
      8732 | if (!s->url && !(s->url = av_strdup(""))) { | 
| 211 | ✗ | ret = AVERROR(ENOMEM); | |
| 212 | ✗ | goto fail; | |
| 213 | } | ||
| 214 | |||
| 215 | // some sanity checks | ||
| 216 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 8732 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      8732 | if (s->nb_streams == 0 && !(of->p.flags & AVFMT_NOSTREAMS)) { | 
| 217 | ✗ | av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n"); | |
| 218 | ✗ | ret = AVERROR(EINVAL); | |
| 219 | ✗ | goto fail; | |
| 220 | } | ||
| 221 | |||
| 222 | 
        2/2✓ Branch 0 taken 9252 times. 
          ✓ Branch 1 taken 8732 times. 
         | 
      17984 | for (unsigned i = 0; i < s->nb_streams; i++) { | 
| 223 | 9252 | AVStream *const st = s->streams[i]; | |
| 224 | 9252 | FFStream *const sti = ffstream(st); | |
| 225 | 9252 | AVCodecParameters *const par = st->codecpar; | |
| 226 | const AVCodecDescriptor *desc; | ||
| 227 | |||
| 228 | 
        2/2✓ Branch 0 taken 5 times. 
          ✓ Branch 1 taken 9247 times. 
         | 
      9252 | if (!st->time_base.num) { | 
| 229 | /* fall back on the default timebase values */ | ||
| 230 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 5 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      5 | if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate) | 
| 231 | ✗ | avpriv_set_pts_info(st, 64, 1, par->sample_rate); | |
| 232 | else | ||
| 233 | 5 | avpriv_set_pts_info(st, 33, 1, 90000); | |
| 234 | } | ||
| 235 | |||
| 236 | 
        3/3✓ Branch 0 taken 1685 times. 
          ✓ Branch 1 taken 7392 times. 
          ✓ Branch 2 taken 175 times. 
         | 
      9252 | switch (par->codec_type) { | 
| 237 | 1685 | case AVMEDIA_TYPE_AUDIO: | |
| 238 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1685 times. 
         | 
      1685 | if (par->sample_rate <= 0) { | 
| 239 | ✗ | av_log(s, AV_LOG_ERROR, "sample rate not set\n"); | |
| 240 | ✗ | ret = AVERROR(EINVAL); | |
| 241 | ✗ | goto fail; | |
| 242 | } | ||
| 243 | |||
| 244 | 
        2/2✓ Branch 0 taken 448 times. 
          ✓ Branch 1 taken 1237 times. 
         | 
      1685 | if (!par->block_align) | 
| 245 | 896 | par->block_align = par->ch_layout.nb_channels * | |
| 246 | 448 | av_get_bits_per_sample(par->codec_id) >> 3; | |
| 247 | 1685 | break; | |
| 248 | 7392 | case AVMEDIA_TYPE_VIDEO: | |
| 249 | 
        2/4✓ Branch 0 taken 7392 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 7392 times. 
         | 
      7392 | if ((par->width <= 0 || par->height <= 0) && | 
| 250 | ✗ | !(of->p.flags & AVFMT_NODIMENSIONS)) { | |
| 251 | ✗ | av_log(s, AV_LOG_ERROR, "dimensions not set\n"); | |
| 252 | ✗ | ret = AVERROR(EINVAL); | |
| 253 | ✗ | goto fail; | |
| 254 | } | ||
| 255 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 7392 times. 
         | 
      7392 | if (av_cmp_q(st->sample_aspect_ratio, par->sample_aspect_ratio) | 
| 256 | ✗ | && fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(par->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio) | |
| 257 | ) { | ||
| 258 | ✗ | if (st->sample_aspect_ratio.num != 0 && | |
| 259 | ✗ | st->sample_aspect_ratio.den != 0 && | |
| 260 | ✗ | par->sample_aspect_ratio.num != 0 && | |
| 261 | ✗ | par->sample_aspect_ratio.den != 0) { | |
| 262 | ✗ | av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer " | |
| 263 | "(%d/%d) and encoder layer (%d/%d)\n", | ||
| 264 | st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, | ||
| 265 | par->sample_aspect_ratio.num, | ||
| 266 | par->sample_aspect_ratio.den); | ||
| 267 | ✗ | ret = AVERROR(EINVAL); | |
| 268 | ✗ | goto fail; | |
| 269 | } | ||
| 270 | } | ||
| 271 | 7392 | break; | |
| 272 | } | ||
| 273 | 
        2/2✓ Branch 0 taken 1113 times. 
          ✓ Branch 1 taken 8139 times. 
         | 
      9252 | if (of->flags_internal & (FF_OFMT_FLAG_MAX_ONE_OF_EACH | FF_OFMT_FLAG_ONLY_DEFAULT_CODECS)) { | 
| 274 | 1113 | enum AVCodecID default_codec_id = AV_CODEC_ID_NONE; | |
| 275 | unsigned nb; | ||
| 276 | 
        1/2✓ Branch 0 taken 1113 times. 
          ✗ Branch 1 not taken. 
         | 
      1113 | if ((unsigned)par->codec_type < FF_ARRAY_ELEMS(default_codec_offsets)) { | 
| 277 | 1113 | nb = ++nb_type[par->codec_type]; | |
| 278 | 
        1/2✓ Branch 0 taken 1113 times. 
          ✗ Branch 1 not taken. 
         | 
      1113 | if (default_codec_offsets[par->codec_type]) | 
| 279 | 1113 | default_codec_id = *(const enum AVCodecID*)((const char*)of + default_codec_offsets[par->codec_type]); | |
| 280 | } | ||
| 281 | 
        3/4✓ Branch 0 taken 567 times. 
          ✓ Branch 1 taken 546 times. 
          ✓ Branch 2 taken 567 times. 
          ✗ Branch 3 not taken. 
         | 
      1113 | if (of->flags_internal & FF_OFMT_FLAG_ONLY_DEFAULT_CODECS && | 
| 282 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 567 times. 
         | 
      567 | default_codec_id != AV_CODEC_ID_NONE && par->codec_id != default_codec_id) { | 
| 283 | ✗ | av_log(s, AV_LOG_ERROR, "%s muxer supports only codec %s for type %s\n", | |
| 284 | ✗ | of->p.name, avcodec_get_name(default_codec_id), av_get_media_type_string(par->codec_type)); | |
| 285 | ✗ | ret = AVERROR(EINVAL); | |
| 286 | ✗ | goto fail; | |
| 287 | 
        1/2✓ Branch 0 taken 1113 times. 
          ✗ Branch 1 not taken. 
         | 
      1113 | } else if (default_codec_id == AV_CODEC_ID_NONE || | 
| 288 | 
        2/4✓ Branch 0 taken 1113 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 1113 times. 
         | 
      1113 | (of->flags_internal & FF_OFMT_FLAG_MAX_ONE_OF_EACH && nb > 1)) { | 
| 289 | ✗ | const char *type = av_get_media_type_string(par->codec_type); | |
| 290 | ✗ | av_log(s, AV_LOG_ERROR, "%s muxer does not support %s stream of type %s\n", | |
| 291 | ✗ | of->p.name, default_codec_id == AV_CODEC_ID_NONE ? "any" : "more than one", | |
| 292 | type ? type : "unknown"); | ||
| 293 | ✗ | ret = AVERROR(EINVAL); | |
| 294 | ✗ | goto fail; | |
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | 9252 | desc = avcodec_descriptor_get(par->codec_id); | |
| 299 | 
        4/4✓ Branch 0 taken 9189 times. 
          ✓ Branch 1 taken 63 times. 
          ✓ Branch 2 taken 387 times. 
          ✓ Branch 3 taken 8802 times. 
         | 
      9252 | if (desc && desc->props & AV_CODEC_PROP_REORDER) | 
| 300 | 387 | sti->reorder = 1; | |
| 301 | |||
| 302 | 9252 | sti->is_intra_only = ff_is_intra_only(par->codec_id); | |
| 303 | |||
| 304 | 
        2/2✓ Branch 0 taken 4206 times. 
          ✓ Branch 1 taken 5046 times. 
         | 
      9252 | if (of->p.codec_tag) { | 
| 305 | 
        2/2✓ Branch 0 taken 2299 times. 
          ✓ Branch 1 taken 1907 times. 
         | 
      4206 | if ( par->codec_tag | 
| 306 | 
        2/2✓ Branch 0 taken 2205 times. 
          ✓ Branch 1 taken 94 times. 
         | 
      2299 | && par->codec_id == AV_CODEC_ID_RAWVIDEO | 
| 307 | 
        2/2✓ Branch 1 taken 2179 times. 
          ✓ Branch 2 taken 26 times. 
         | 
      2205 | && ( av_codec_get_tag(of->p.codec_tag, par->codec_id) == 0 | 
| 308 | 
        2/2✓ Branch 1 taken 17 times. 
          ✓ Branch 2 taken 2162 times. 
         | 
      2179 | || av_codec_get_tag(of->p.codec_tag, par->codec_id) == MKTAG('r', 'a', 'w', ' ')) | 
| 309 | 
        2/2✓ Branch 1 taken 27 times. 
          ✓ Branch 2 taken 16 times. 
         | 
      43 | && !validate_codec_tag(s, st)) { | 
| 310 | // the current rawvideo encoding system ends up setting | ||
| 311 | // the wrong codec_tag for avi/mov, we override it here | ||
| 312 | 27 | par->codec_tag = 0; | |
| 313 | } | ||
| 314 | 
        2/2✓ Branch 0 taken 2272 times. 
          ✓ Branch 1 taken 1934 times. 
         | 
      4206 | if (par->codec_tag) { | 
| 315 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 2272 times. 
         | 
      2272 | if (!validate_codec_tag(s, st)) { | 
| 316 | ✗ | const uint32_t otag = av_codec_get_tag(s->oformat->codec_tag, par->codec_id); | |
| 317 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 318 | "Tag %s incompatible with output codec id '%d' (%s)\n", | ||
| 319 | ✗ | av_fourcc2str(par->codec_tag), par->codec_id, av_fourcc2str(otag)); | |
| 320 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 321 | ✗ | goto fail; | |
| 322 | } | ||
| 323 | } else | ||
| 324 | 1934 | par->codec_tag = av_codec_get_tag(of->p.codec_tag, par->codec_id); | |
| 325 | } | ||
| 326 | |||
| 327 | 
        2/2✓ Branch 0 taken 9251 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      9252 | if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT && | 
| 328 | 
        1/2✓ Branch 0 taken 9251 times. 
          ✗ Branch 1 not taken. 
         | 
      9251 | par->codec_id != AV_CODEC_ID_SMPTE_2038) | 
| 329 | 9251 | fci->nb_interleaved_streams++; | |
| 330 | } | ||
| 331 | 8732 | fci->interleave_packet = of->interleave_packet; | |
| 332 | 
        2/2✓ Branch 0 taken 8692 times. 
          ✓ Branch 1 taken 40 times. 
         | 
      8732 | if (!fci->interleave_packet) | 
| 333 | 
        2/2✓ Branch 0 taken 309 times. 
          ✓ Branch 1 taken 8383 times. 
         | 
      8692 | fci->interleave_packet = fci->nb_interleaved_streams > 1 ? | 
| 334 | ff_interleave_packet_per_dts : | ||
| 335 | ff_interleave_packet_passthrough; | ||
| 336 | |||
| 337 | 
        4/4✓ Branch 0 taken 3885 times. 
          ✓ Branch 1 taken 4847 times. 
          ✓ Branch 2 taken 75 times. 
          ✓ Branch 3 taken 3810 times. 
         | 
      8732 | if (!s->priv_data && of->priv_data_size > 0) { | 
| 338 | 75 | s->priv_data = av_mallocz(of->priv_data_size); | |
| 339 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 75 times. 
         | 
      75 | if (!s->priv_data) { | 
| 340 | ✗ | ret = AVERROR(ENOMEM); | |
| 341 | ✗ | goto fail; | |
| 342 | } | ||
| 343 | 
        1/2✓ Branch 0 taken 75 times. 
          ✗ Branch 1 not taken. 
         | 
      75 | if (of->p.priv_class) { | 
| 344 | 75 | *(const AVClass **)s->priv_data = of->p.priv_class; | |
| 345 | 75 | av_opt_set_defaults(s->priv_data); | |
| 346 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 75 times. 
         | 
      75 | if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0) | 
| 347 | ✗ | goto fail; | |
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | /* set muxer identification string */ | ||
| 352 | 
        2/2✓ Branch 0 taken 1088 times. 
          ✓ Branch 1 taken 7644 times. 
         | 
      8732 | if (!(s->flags & AVFMT_FLAG_BITEXACT)) { | 
| 353 | 1088 | av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0); | |
| 354 | } else { | ||
| 355 | 7644 | av_dict_set(&s->metadata, "encoder", NULL, 0); | |
| 356 | } | ||
| 357 | |||
| 358 | 
        2/2✓ Branch 1 taken 4 times. 
          ✓ Branch 2 taken 8732 times. 
         | 
      8736 | for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) { | 
| 359 | 4 | av_dict_set(&s->metadata, e->key, NULL, 0); | |
| 360 | } | ||
| 361 | |||
| 362 | 
        2/2✓ Branch 0 taken 8704 times. 
          ✓ Branch 1 taken 28 times. 
         | 
      8732 | if (options) { | 
| 363 | 8704 | av_dict_free(options); | |
| 364 | 8704 | *options = tmp; | |
| 365 | } | ||
| 366 | |||
| 367 | 
        2/2✓ Branch 0 taken 1236 times. 
          ✓ Branch 1 taken 7496 times. 
         | 
      8732 | if (of->init) { | 
| 368 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1236 times. 
         | 
      1236 | if ((ret = of->init(s)) < 0) { | 
| 369 | ✗ | if (of->deinit) | |
| 370 | ✗ | of->deinit(s); | |
| 371 | ✗ | return ret; | |
| 372 | } | ||
| 373 | 1236 | return ret == 0; | |
| 374 | } | ||
| 375 | |||
| 376 | 7496 | return 0; | |
| 377 | |||
| 378 | ✗ | fail: | |
| 379 | ✗ | av_dict_free(&tmp); | |
| 380 | ✗ | return ret; | |
| 381 | } | ||
| 382 | |||
| 383 | 8732 | static int init_pts(AVFormatContext *s) | |
| 384 | { | ||
| 385 | 8732 | FFFormatContext *const si = ffformatcontext(s); | |
| 386 | |||
| 387 | /* init PTS generation */ | ||
| 388 | 
        2/2✓ Branch 0 taken 9252 times. 
          ✓ Branch 1 taken 8732 times. 
         | 
      17984 | for (unsigned i = 0; i < s->nb_streams; i++) { | 
| 389 | 9252 | AVStream *const st = s->streams[i]; | |
| 390 | 9252 | FFStream *const sti = ffstream(st); | |
| 391 | 9252 | int64_t den = AV_NOPTS_VALUE; | |
| 392 | |||
| 393 | 
        3/3✓ Branch 0 taken 1685 times. 
          ✓ Branch 1 taken 7392 times. 
          ✓ Branch 2 taken 175 times. 
         | 
      9252 | switch (st->codecpar->codec_type) { | 
| 394 | 1685 | case AVMEDIA_TYPE_AUDIO: | |
| 395 | 1685 | den = (int64_t)st->time_base.num * st->codecpar->sample_rate; | |
| 396 | 1685 | break; | |
| 397 | 7392 | case AVMEDIA_TYPE_VIDEO: | |
| 398 | 7392 | den = (int64_t)st->time_base.num * st->time_base.den; | |
| 399 | 7392 | break; | |
| 400 | 175 | default: | |
| 401 | 175 | break; | |
| 402 | } | ||
| 403 | |||
| 404 | 
        2/2✓ Branch 0 taken 9077 times. 
          ✓ Branch 1 taken 175 times. 
         | 
      9252 | if (den != AV_NOPTS_VALUE) { | 
| 405 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 9077 times. 
         | 
      9077 | if (den <= 0) | 
| 406 | ✗ | return AVERROR_INVALIDDATA; | |
| 407 | |||
| 408 | 9077 | frac_init(&sti->priv_pts, 0, 0, den); | |
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | 8732 | si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_UNKNOWN; | |
| 413 | 
        2/2✓ Branch 0 taken 5699 times. 
          ✓ Branch 1 taken 3033 times. 
         | 
      8732 | if (s->avoid_negative_ts < 0) { | 
| 414 | av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO); | ||
| 415 | 
        2/2✓ Branch 0 taken 4703 times. 
          ✓ Branch 1 taken 996 times. 
         | 
      5699 | if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) { | 
| 416 | 4703 | s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_DISABLED; | |
| 417 | 4703 | si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED; | |
| 418 | } else | ||
| 419 | 996 | s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE; | |
| 420 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 3031 times. 
         | 
      3033 | } else if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_DISABLED) | 
| 421 | 2 | si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED; | |
| 422 | |||
| 423 | 8732 | return 0; | |
| 424 | } | ||
| 425 | |||
| 426 | 527539 | static void flush_if_needed(AVFormatContext *s) | |
| 427 | { | ||
| 428 | 
        3/4✓ Branch 0 taken 527401 times. 
          ✓ Branch 1 taken 138 times. 
          ✓ Branch 2 taken 527401 times. 
          ✗ Branch 3 not taken. 
         | 
      527539 | if (s->pb && s->pb->error >= 0) { | 
| 429 | 
        2/4✓ Branch 0 taken 527401 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 527401 times. 
         | 
      527401 | if (s->flush_packets == 1 || s->flags & AVFMT_FLAG_FLUSH_PACKETS) | 
| 430 | ✗ | avio_flush(s->pb); | |
| 431 | 
        2/4✓ Branch 0 taken 527401 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 527401 times. 
          ✗ Branch 3 not taken. 
         | 
      527401 | else if (s->flush_packets && !(s->oformat->flags & AVFMT_NOFILE)) | 
| 432 | 527401 | avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT); | |
| 433 | } | ||
| 434 | 527539 | } | |
| 435 | |||
| 436 | 8732 | static void deinit_muxer(AVFormatContext *s) | |
| 437 | { | ||
| 438 | 8732 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 439 | 8732 | const FFOutputFormat *const of = ffofmt(s->oformat); | |
| 440 | 
        4/6✓ Branch 0 taken 8732 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 4520 times. 
          ✓ Branch 3 taken 4212 times. 
          ✓ Branch 4 taken 4520 times. 
          ✗ Branch 5 not taken. 
         | 
      8732 | if (of && of->deinit && fci->initialized) | 
| 441 | 4520 | of->deinit(s); | |
| 442 | 8732 | fci->initialized = | |
| 443 | 8732 | fci->streams_initialized = 0; | |
| 444 | 8732 | } | |
| 445 | |||
| 446 | 8732 | int avformat_init_output(AVFormatContext *s, AVDictionary **options) | |
| 447 | { | ||
| 448 | 8732 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 449 | 8732 | int ret = 0; | |
| 450 | |||
| 451 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 8732 times. 
         | 
      8732 | if ((ret = init_muxer(s, options)) < 0) | 
| 452 | ✗ | return ret; | |
| 453 | |||
| 454 | 8732 | fci->initialized = 1; | |
| 455 | 8732 | fci->streams_initialized = ret; | |
| 456 | |||
| 457 | 
        4/4✓ Branch 1 taken 1236 times. 
          ✓ Branch 2 taken 7496 times. 
          ✓ Branch 3 taken 926 times. 
          ✓ Branch 4 taken 310 times. 
         | 
      8732 | if (ffofmt(s->oformat)->init && ret) { | 
| 458 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 926 times. 
         | 
      926 | if ((ret = init_pts(s)) < 0) | 
| 459 | ✗ | return ret; | |
| 460 | |||
| 461 | 926 | return AVSTREAM_INIT_IN_INIT_OUTPUT; | |
| 462 | } | ||
| 463 | |||
| 464 | 7806 | return AVSTREAM_INIT_IN_WRITE_HEADER; | |
| 465 | } | ||
| 466 | |||
| 467 | 8732 | int avformat_write_header(AVFormatContext *s, AVDictionary **options) | |
| 468 | { | ||
| 469 | 8732 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 470 | 8732 | int already_initialized = fci->initialized; | |
| 471 | 8732 | int streams_already_initialized = fci->streams_initialized; | |
| 472 | 8732 | int ret = 0; | |
| 473 | |||
| 474 | 
        2/2✓ Branch 0 taken 8716 times. 
          ✓ Branch 1 taken 16 times. 
         | 
      8732 | if (!already_initialized) | 
| 475 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 8716 times. 
         | 
      8716 | if ((ret = avformat_init_output(s, options)) < 0) | 
| 476 | ✗ | return ret; | |
| 477 | |||
| 478 | 
        2/2✓ Branch 1 taken 6305 times. 
          ✓ Branch 2 taken 2427 times. 
         | 
      8732 | if (ffofmt(s->oformat)->write_header) { | 
| 479 | 
        3/4✓ Branch 0 taken 6173 times. 
          ✓ Branch 1 taken 132 times. 
          ✓ Branch 2 taken 6173 times. 
          ✗ Branch 3 not taken. 
         | 
      6305 | if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb) | 
| 480 | 6173 | avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER); | |
| 481 | 6305 | ret = ffofmt(s->oformat)->write_header(s); | |
| 482 | 
        4/6✓ Branch 0 taken 6305 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 6173 times. 
          ✓ Branch 3 taken 132 times. 
          ✗ Branch 4 not taken. 
          ✓ Branch 5 taken 6173 times. 
         | 
      6305 | if (ret >= 0 && s->pb && s->pb->error < 0) | 
| 483 | ✗ | ret = s->pb->error; | |
| 484 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 6305 times. 
         | 
      6305 | if (ret < 0) | 
| 485 | ✗ | goto fail; | |
| 486 | 6305 | flush_if_needed(s); | |
| 487 | } | ||
| 488 | 
        3/4✓ Branch 0 taken 8580 times. 
          ✓ Branch 1 taken 152 times. 
          ✓ Branch 2 taken 8580 times. 
          ✗ Branch 3 not taken. 
         | 
      8732 | if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb) | 
| 489 | 8580 | avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN); | |
| 490 | |||
| 491 | 
        2/2✓ Branch 0 taken 7806 times. 
          ✓ Branch 1 taken 926 times. 
         | 
      8732 | if (!fci->streams_initialized) { | 
| 492 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 7806 times. 
         | 
      7806 | if ((ret = init_pts(s)) < 0) | 
| 493 | ✗ | goto fail; | |
| 494 | } | ||
| 495 | |||
| 496 | 8732 | return streams_already_initialized; | |
| 497 | |||
| 498 | ✗ | fail: | |
| 499 | ✗ | deinit_muxer(s); | |
| 500 | ✗ | return ret; | |
| 501 | } | ||
| 502 | |||
| 503 | #define AV_PKT_FLAG_UNCODED_FRAME 0x2000 | ||
| 504 | |||
| 505 | |||
| 506 | #if FF_API_COMPUTE_PKT_FIELDS2 | ||
| 507 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 508 | //FIXME merge with compute_pkt_fields | ||
| 509 | 530943 | static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
| 510 | { | ||
| 511 | 530943 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 512 | 530943 | FFStream *const sti = ffstream(st); | |
| 513 | 530943 | int delay = st->codecpar->video_delay; | |
| 514 | int frame_size; | ||
| 515 | |||
| 516 | 
        2/2✓ Branch 0 taken 529775 times. 
          ✓ Branch 1 taken 1168 times. 
         | 
      530943 | if (!fci->missing_ts_warning && | 
| 517 | 
        2/2✓ Branch 0 taken 239766 times. 
          ✓ Branch 1 taken 290009 times. 
         | 
      529775 | !(s->oformat->flags & AVFMT_NOTIMESTAMPS) && | 
| 518 | 
        3/4✓ Branch 0 taken 23 times. 
          ✓ Branch 1 taken 239743 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 23 times. 
         | 
      239766 | (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC) || (st->disposition & AV_DISPOSITION_TIMED_THUMBNAILS)) && | 
| 519 | 
        3/4✓ Branch 0 taken 239727 times. 
          ✓ Branch 1 taken 16 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 239727 times. 
         | 
      239743 | (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) { | 
| 520 | 16 | av_log(s, AV_LOG_WARNING, | |
| 521 | "Timestamps are unset in a packet for stream %d. " | ||
| 522 | "This is deprecated and will stop working in the future. " | ||
| 523 | "Fix your code to set the timestamps properly\n", st->index); | ||
| 524 | 16 | fci->missing_ts_warning = 1; | |
| 525 | } | ||
| 526 | |||
| 527 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530943 times. 
         | 
      530943 | if (s->debug & FF_FDEBUG_TS) | 
| 528 | ✗ | av_log(s, AV_LOG_DEBUG, "compute_muxer_pkt_fields: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n", | |
| 529 | ✗ | av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), delay, pkt->size, pkt->stream_index); | |
| 530 | |||
| 531 | 
        6/6✓ Branch 0 taken 8078 times. 
          ✓ Branch 1 taken 522865 times. 
          ✓ Branch 2 taken 8077 times. 
          ✓ Branch 3 taken 1 times. 
          ✓ Branch 4 taken 3820 times. 
          ✓ Branch 5 taken 4257 times. 
         | 
      530943 | if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0) | 
| 532 | 3820 | pkt->pts = pkt->dts; | |
| 533 | |||
| 534 | //XXX/FIXME this is a temporary hack until all encoders output pts | ||
| 535 | 
        7/8✓ Branch 0 taken 521823 times. 
          ✓ Branch 1 taken 9120 times. 
          ✓ Branch 2 taken 4258 times. 
          ✓ Branch 3 taken 517565 times. 
          ✓ Branch 4 taken 1 times. 
          ✓ Branch 5 taken 13377 times. 
          ✓ Branch 6 taken 1 times. 
          ✗ Branch 7 not taken. 
         | 
      530943 | if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) { | 
| 536 | static int warned; | ||
| 537 | 
        1/2✓ Branch 0 taken 1 times. 
          ✗ Branch 1 not taken. 
         | 
      1 | if (!warned) { | 
| 538 | 1 | av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n"); | |
| 539 | 1 | warned = 1; | |
| 540 | } | ||
| 541 | 1 | pkt->dts = | |
| 542 | // pkt->pts= st->cur_dts; | ||
| 543 | 1 | pkt->pts = sti->priv_pts.val; | |
| 544 | } | ||
| 545 | |||
| 546 | //calculate dts from pts | ||
| 547 | 
        3/6✓ Branch 0 taken 526686 times. 
          ✓ Branch 1 taken 4257 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 526686 times. 
          ✗ Branch 4 not taken. 
          ✗ Branch 5 not taken. 
         | 
      530943 | if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) { | 
| 548 | ✗ | sti->pts_buffer[0] = pkt->pts; | |
| 549 | ✗ | for (int i = 1; i < delay + 1 && sti->pts_buffer[i] == AV_NOPTS_VALUE; i++) | |
| 550 | ✗ | sti->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration; | |
| 551 | ✗ | for (int i = 0; i<delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++) | |
| 552 | ✗ | FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]); | |
| 553 | |||
| 554 | ✗ | pkt->dts = sti->pts_buffer[0]; | |
| 555 | } | ||
| 556 | |||
| 557 | 
        4/4✓ Branch 0 taken 524817 times. 
          ✓ Branch 1 taken 6126 times. 
          ✓ Branch 2 taken 515667 times. 
          ✓ Branch 3 taken 9150 times. 
         | 
      530943 | if (sti->cur_dts && sti->cur_dts != AV_NOPTS_VALUE && | 
| 558 | 
        2/2✓ Branch 0 taken 375928 times. 
          ✓ Branch 1 taken 139739 times. 
         | 
      515667 | ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && | 
| 559 | 
        2/2✓ Branch 0 taken 375486 times. 
          ✓ Branch 1 taken 442 times. 
         | 
      375928 | st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE && | 
| 560 | 
        2/2✓ Branch 0 taken 375416 times. 
          ✓ Branch 1 taken 70 times. 
         | 
      375486 | st->codecpar->codec_type != AVMEDIA_TYPE_DATA && | 
| 561 | 
        3/4✓ Branch 0 taken 375374 times. 
          ✓ Branch 1 taken 42 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 515625 times. 
         | 
      515667 | sti->cur_dts >= pkt->dts) || sti->cur_dts > pkt->dts)) { | 
| 562 | 42 | av_log(s, AV_LOG_ERROR, | |
| 563 | "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n", | ||
| 564 | 42 | st->index, av_ts2str(sti->cur_dts), av_ts2str(pkt->dts)); | |
| 565 | 42 | return AVERROR(EINVAL); | |
| 566 | } | ||
| 567 | 
        4/6✓ Branch 0 taken 530901 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 526644 times. 
          ✓ Branch 3 taken 4257 times. 
          ✗ Branch 4 not taken. 
          ✓ Branch 5 taken 526644 times. 
         | 
      530901 | if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) { | 
| 568 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 569 | "pts (%s) < dts (%s) in stream %d\n", | ||
| 570 | ✗ | av_ts2str(pkt->pts), av_ts2str(pkt->dts), | |
| 571 | st->index); | ||
| 572 | ✗ | return AVERROR(EINVAL); | |
| 573 | } | ||
| 574 | |||
| 575 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530901 times. 
         | 
      530901 | if (s->debug & FF_FDEBUG_TS) | 
| 576 | ✗ | av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%s dts2:%s\n", | |
| 577 | ✗ | av_ts2str(pkt->pts), av_ts2str(pkt->dts)); | |
| 578 | |||
| 579 | 530901 | sti->cur_dts = pkt->dts; | |
| 580 | 530901 | sti->priv_pts.val = pkt->dts; | |
| 581 | |||
| 582 | /* update pts */ | ||
| 583 | 
        3/3✓ Branch 0 taken 366309 times. 
          ✓ Branch 1 taken 161972 times. 
          ✓ Branch 2 taken 2620 times. 
         | 
      530901 | switch (st->codecpar->codec_type) { | 
| 584 | 366309 | case AVMEDIA_TYPE_AUDIO: | |
| 585 | 732618 | frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ? | |
| 586 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 366309 times. 
         | 
      366309 | (*(AVFrame **)pkt->data)->nb_samples : | 
| 587 | 366309 | av_get_audio_frame_duration2(st->codecpar, pkt->size); | |
| 588 | |||
| 589 | /* HACK/FIXME, we skip the initial 0 size packets as they are most | ||
| 590 | * likely equal to the encoder delay, but it would be better if we | ||
| 591 | * had the real timestamps from the encoder */ | ||
| 592 | 
        5/8✓ Branch 0 taken 366309 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 72 times. 
          ✓ Branch 3 taken 366237 times. 
          ✓ Branch 4 taken 72 times. 
          ✗ Branch 5 not taken. 
          ✓ Branch 6 taken 72 times. 
          ✗ Branch 7 not taken. 
         | 
      366309 | if (frame_size >= 0 && (pkt->size || sti->priv_pts.num != sti->priv_pts.den >> 1 || sti->priv_pts.val)) { | 
| 593 | 366309 | frac_add(&sti->priv_pts, (int64_t)st->time_base.den * frame_size); | |
| 594 | } | ||
| 595 | 366309 | break; | |
| 596 | 161972 | case AVMEDIA_TYPE_VIDEO: | |
| 597 | 161972 | frac_add(&sti->priv_pts, (int64_t)st->time_base.den * st->time_base.num); | |
| 598 | 161972 | break; | |
| 599 | } | ||
| 600 | 530901 | return 0; | |
| 601 | } | ||
| 602 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 603 | #endif | ||
| 604 | |||
| 605 | 530943 | static void guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
| 606 | { | ||
| 607 | 
        3/4✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 530942 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 1 times. 
         | 
      530943 | if (pkt->duration < 0 && st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) { | 
| 608 | ✗ | av_log(s, AV_LOG_WARNING, "Packet with invalid duration %"PRId64" in stream %d\n", | |
| 609 | pkt->duration, pkt->stream_index); | ||
| 610 | ✗ | pkt->duration = 0; | |
| 611 | } | ||
| 612 | |||
| 613 | 
        2/2✓ Branch 0 taken 511023 times. 
          ✓ Branch 1 taken 19920 times. 
         | 
      530943 | if (pkt->duration) | 
| 614 | 511023 | return; | |
| 615 | |||
| 616 | 
        3/3✓ Branch 0 taken 18234 times. 
          ✓ Branch 1 taken 1251 times. 
          ✓ Branch 2 taken 435 times. 
         | 
      19920 | switch (st->codecpar->codec_type) { | 
| 617 | 18234 | case AVMEDIA_TYPE_VIDEO: | |
| 618 | 
        3/4✓ Branch 0 taken 17386 times. 
          ✓ Branch 1 taken 848 times. 
          ✓ Branch 2 taken 17386 times. 
          ✗ Branch 3 not taken. 
         | 
      18234 | if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0) { | 
| 619 | 17386 | pkt->duration = av_rescale_q(1, av_inv_q(st->avg_frame_rate), | |
| 620 | st->time_base); | ||
| 621 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 848 times. 
         | 
      848 | } else if (st->time_base.num * 1000LL > st->time_base.den) | 
| 622 | ✗ | pkt->duration = 1; | |
| 623 | 18234 | break; | |
| 624 | 1251 | case AVMEDIA_TYPE_AUDIO: { | |
| 625 | 1251 | int frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size); | |
| 626 | 
        3/4✓ Branch 0 taken 277 times. 
          ✓ Branch 1 taken 974 times. 
          ✓ Branch 2 taken 277 times. 
          ✗ Branch 3 not taken. 
         | 
      1251 | if (frame_size && st->codecpar->sample_rate) { | 
| 627 | 277 | pkt->duration = av_rescale_q(frame_size, | |
| 628 | 277 | (AVRational){1, st->codecpar->sample_rate}, | |
| 629 | st->time_base); | ||
| 630 | } | ||
| 631 | 1251 | break; | |
| 632 | } | ||
| 633 | } | ||
| 634 | } | ||
| 635 | |||
| 636 | 530930 | static void handle_avoid_negative_ts(FFFormatContext *si, FFStream *sti, | |
| 637 | AVPacket *pkt) | ||
| 638 | { | ||
| 639 | 530930 | AVFormatContext *const s = &si->pub; | |
| 640 | int64_t offset; | ||
| 641 | |||
| 642 | 
        2/2✓ Branch 0 taken 401518 times. 
          ✓ Branch 1 taken 129412 times. 
         | 
      530930 | if (!AVOID_NEGATIVE_TS_ENABLED(si->avoid_negative_ts_status)) | 
| 643 | 401518 | return; | |
| 644 | |||
| 645 | 
        2/2✓ Branch 0 taken 4020 times. 
          ✓ Branch 1 taken 125392 times. 
         | 
      129412 | if (si->avoid_negative_ts_status == AVOID_NEGATIVE_TS_UNKNOWN) { | 
| 646 | 4020 | int use_pts = si->avoid_negative_ts_use_pts; | |
| 647 | 
        2/2✓ Branch 0 taken 40 times. 
          ✓ Branch 1 taken 3980 times. 
         | 
      4020 | int64_t ts = use_pts ? pkt->pts : pkt->dts; | 
| 648 | 4020 | AVRational tb = sti->pub.time_base; | |
| 649 | |||
| 650 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 4020 times. 
         | 
      4020 | if (ts == AV_NOPTS_VALUE) | 
| 651 | ✗ | return; | |
| 652 | |||
| 653 | 4020 | ts -= sti->lowest_ts_allowed; | |
| 654 | |||
| 655 | /* Peek into the muxing queue to improve our estimate | ||
| 656 | * of the lowest timestamp if av_interleaved_write_frame() is used. */ | ||
| 657 | 4020 | for (const PacketListEntry *pktl = si->packet_buffer.head; | |
| 658 | 
        2/2✓ Branch 0 taken 1093 times. 
          ✓ Branch 1 taken 4020 times. 
         | 
      5113 | pktl; pktl = pktl->next) { | 
| 659 | 1093 | AVRational cmp_tb = s->streams[pktl->pkt.stream_index]->time_base; | |
| 660 | 
        2/2✓ Branch 0 taken 154 times. 
          ✓ Branch 1 taken 939 times. 
         | 
      1093 | int64_t cmp_ts = use_pts ? pktl->pkt.pts : pktl->pkt.dts; | 
| 661 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1093 times. 
         | 
      1093 | if (cmp_ts == AV_NOPTS_VALUE) | 
| 662 | ✗ | continue; | |
| 663 | 1093 | cmp_ts -= ffstream(s->streams[pktl->pkt.stream_index])->lowest_ts_allowed; | |
| 664 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 1092 times. 
         | 
      1093 | if (s->output_ts_offset) | 
| 665 | 1 | cmp_ts += av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, cmp_tb); | |
| 666 | 
        2/2✓ Branch 1 taken 1 times. 
          ✓ Branch 2 taken 1092 times. 
         | 
      1093 | if (av_compare_ts(cmp_ts, cmp_tb, ts, tb) < 0) { | 
| 667 | 1 | ts = cmp_ts; | |
| 668 | 1 | tb = cmp_tb; | |
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | 
        4/4✓ Branch 0 taken 3963 times. 
          ✓ Branch 1 taken 57 times. 
          ✓ Branch 2 taken 103 times. 
          ✓ Branch 3 taken 3860 times. 
         | 
      4020 | if (ts < 0 || | 
| 673 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 101 times. 
         | 
      103 | ts > 0 && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) { | 
| 674 | 
        2/2✓ Branch 0 taken 80 times. 
          ✓ Branch 1 taken 59 times. 
         | 
      139 | for (unsigned i = 0; i < s->nb_streams; i++) { | 
| 675 | 80 | AVStream *const st2 = s->streams[i]; | |
| 676 | 80 | FFStream *const sti2 = ffstream(st2); | |
| 677 | 80 | sti2->mux_ts_offset = av_rescale_q_rnd(-ts, tb, | |
| 678 | st2->time_base, | ||
| 679 | AV_ROUND_UP); | ||
| 680 | } | ||
| 681 | } | ||
| 682 | 4020 | si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_KNOWN; | |
| 683 | } | ||
| 684 | |||
| 685 | 129412 | offset = sti->mux_ts_offset; | |
| 686 | |||
| 687 | 
        1/2✓ Branch 0 taken 129412 times. 
          ✗ Branch 1 not taken. 
         | 
      129412 | if (pkt->dts != AV_NOPTS_VALUE) | 
| 688 | 129412 | pkt->dts += offset; | |
| 689 | 
        2/2✓ Branch 0 taken 129321 times. 
          ✓ Branch 1 taken 91 times. 
         | 
      129412 | if (pkt->pts != AV_NOPTS_VALUE) | 
| 690 | 129321 | pkt->pts += offset; | |
| 691 | |||
| 692 | 
        2/2✓ Branch 0 taken 5537 times. 
          ✓ Branch 1 taken 123875 times. 
         | 
      129412 | if (si->avoid_negative_ts_use_pts) { | 
| 693 | 
        2/4✓ Branch 0 taken 5537 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 5537 times. 
         | 
      5537 | if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < sti->lowest_ts_allowed) { | 
| 694 | ✗ | av_log(s, AV_LOG_WARNING, "failed to avoid negative " | |
| 695 | "pts %s in stream %d.\n" | ||
| 696 | "Try -avoid_negative_ts 1 as a possible workaround.\n", | ||
| 697 | ✗ | av_ts2str(pkt->pts), | |
| 698 | pkt->stream_index | ||
| 699 | ); | ||
| 700 | } | ||
| 701 | } else { | ||
| 702 | 
        2/4✓ Branch 0 taken 123875 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 123875 times. 
         | 
      123875 | if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < sti->lowest_ts_allowed) { | 
| 703 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 704 | "Packets poorly interleaved, failed to avoid negative " | ||
| 705 | "timestamp %s in stream %d.\n" | ||
| 706 | "Try -max_interleave_delta 0 as a possible workaround.\n", | ||
| 707 | ✗ | av_ts2str(pkt->dts), | |
| 708 | pkt->stream_index | ||
| 709 | ); | ||
| 710 | } | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | /** | ||
| 715 | * Shift timestamps and call muxer; the original pts/dts are not kept. | ||
| 716 | * | ||
| 717 | * FIXME: this function should NEVER get undefined pts/dts beside when the | ||
| 718 | * AVFMT_NOTIMESTAMPS is set. | ||
| 719 | * Those additional safety checks should be dropped once the correct checks | ||
| 720 | * are set in the callers. | ||
| 721 | */ | ||
| 722 | 530930 | static int write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 723 | { | ||
| 724 | 530930 | FFFormatContext *const si = ffformatcontext(s); | |
| 725 | 530930 | AVStream *const st = s->streams[pkt->stream_index]; | |
| 726 | 530930 | FFStream *const sti = ffstream(st); | |
| 727 | int ret; | ||
| 728 | |||
| 729 | // If the timestamp offsetting below is adjusted, adjust | ||
| 730 | // ff_interleaved_peek similarly. | ||
| 731 | 
        2/2✓ Branch 0 taken 804 times. 
          ✓ Branch 1 taken 530126 times. 
         | 
      530930 | if (s->output_ts_offset) { | 
| 732 | 804 | int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base); | |
| 733 | |||
| 734 | 
        1/2✓ Branch 0 taken 804 times. 
          ✗ Branch 1 not taken. 
         | 
      804 | if (pkt->dts != AV_NOPTS_VALUE) | 
| 735 | 804 | pkt->dts += offset; | |
| 736 | 
        1/2✓ Branch 0 taken 804 times. 
          ✗ Branch 1 not taken. 
         | 
      804 | if (pkt->pts != AV_NOPTS_VALUE) | 
| 737 | 804 | pkt->pts += offset; | |
| 738 | } | ||
| 739 | 530930 | handle_avoid_negative_ts(si, sti, pkt); | |
| 740 | |||
| 741 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530930 times. 
         | 
      530930 | if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) { | 
| 742 | ✗ | AVFrame **frame = (AVFrame **)pkt->data; | |
| 743 | ✗ | av_assert0(pkt->size == sizeof(*frame)); | |
| 744 | ✗ | ret = ffofmt(s->oformat)->write_uncoded_frame(s, pkt->stream_index, frame, 0); | |
| 745 | } else { | ||
| 746 | 530930 | ret = ffofmt(s->oformat)->write_packet(s, pkt); | |
| 747 | } | ||
| 748 | |||
| 749 | 
        3/4✓ Branch 0 taken 521068 times. 
          ✓ Branch 1 taken 9862 times. 
          ✓ Branch 2 taken 521068 times. 
          ✗ Branch 3 not taken. 
         | 
      530930 | if (s->pb && ret >= 0) { | 
| 750 | 521068 | flush_if_needed(s); | |
| 751 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 521068 times. 
         | 
      521068 | if (s->pb->error < 0) | 
| 752 | ✗ | ret = s->pb->error; | |
| 753 | } | ||
| 754 | |||
| 755 | 
        2/2✓ Branch 0 taken 530885 times. 
          ✓ Branch 1 taken 45 times. 
         | 
      530930 | if (ret >= 0) | 
| 756 | 530885 | st->nb_frames++; | |
| 757 | |||
| 758 | 530930 | return ret; | |
| 759 | } | ||
| 760 | |||
| 761 | 530816 | static int check_packet(AVFormatContext *s, AVPacket *pkt) | |
| 762 | { | ||
| 763 | 
        2/4✓ Branch 0 taken 530816 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 530816 times. 
         | 
      530816 | if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) { | 
| 764 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n", | |
| 765 | pkt->stream_index); | ||
| 766 | ✗ | return AVERROR(EINVAL); | |
| 767 | } | ||
| 768 | |||
| 769 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530816 times. 
         | 
      530816 | if (s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) { | 
| 770 | ✗ | av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n"); | |
| 771 | ✗ | return AVERROR(EINVAL); | |
| 772 | } | ||
| 773 | |||
| 774 | 530816 | return 0; | |
| 775 | } | ||
| 776 | |||
| 777 | 530816 | static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
| 778 | { | ||
| 779 | 530816 | FFStream *const sti = ffstream(st); | |
| 780 | #if !FF_API_COMPUTE_PKT_FIELDS2 | ||
| 781 | /* sanitize the timestamps */ | ||
| 782 | if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { | ||
| 783 | |||
| 784 | /* when there is no reordering (so dts is equal to pts), but | ||
| 785 | * only one of them is set, set the other as well */ | ||
| 786 | if (!sti->reorder) { | ||
| 787 | if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE) | ||
| 788 | pkt->pts = pkt->dts; | ||
| 789 | if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE) | ||
| 790 | pkt->dts = pkt->pts; | ||
| 791 | } | ||
| 792 | |||
| 793 | /* check that the timestamps are set */ | ||
| 794 | if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) { | ||
| 795 | av_log(s, AV_LOG_ERROR, | ||
| 796 | "Timestamps are unset in a packet for stream %d\n", st->index); | ||
| 797 | return AVERROR(EINVAL); | ||
| 798 | } | ||
| 799 | |||
| 800 | /* check that the dts are increasing (or at least non-decreasing, | ||
| 801 | * if the format allows it */ | ||
| 802 | if (sti->cur_dts != AV_NOPTS_VALUE && | ||
| 803 | ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && sti->cur_dts >= pkt->dts) || | ||
| 804 | sti->cur_dts > pkt->dts)) { | ||
| 805 | av_log(s, AV_LOG_ERROR, | ||
| 806 | "Application provided invalid, non monotonically increasing " | ||
| 807 | "dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n", | ||
| 808 | st->index, sti->cur_dts, pkt->dts); | ||
| 809 | return AVERROR(EINVAL); | ||
| 810 | } | ||
| 811 | |||
| 812 | if (pkt->pts < pkt->dts) { | ||
| 813 | av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n", | ||
| 814 | pkt->pts, pkt->dts, st->index); | ||
| 815 | return AVERROR(EINVAL); | ||
| 816 | } | ||
| 817 | } | ||
| 818 | #endif | ||
| 819 | /* update flags */ | ||
| 820 | 
        2/2✓ Branch 0 taken 474274 times. 
          ✓ Branch 1 taken 56542 times. 
         | 
      530816 | if (sti->is_intra_only) | 
| 821 | 474274 | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 822 | |||
| 823 | 
        4/4✓ Branch 0 taken 74 times. 
          ✓ Branch 1 taken 530742 times. 
          ✓ Branch 2 taken 4 times. 
          ✓ Branch 3 taken 70 times. 
         | 
      530816 | if (!pkt->data && !pkt->side_data_elems) { | 
| 824 | /* Such empty packets signal EOS for the BSF API; so sanitize | ||
| 825 | * the packet by allocating data of size 0 (+ padding). */ | ||
| 826 | 4 | av_buffer_unref(&pkt->buf); | |
| 827 | 4 | return av_packet_make_refcounted(pkt); | |
| 828 | } | ||
| 829 | |||
| 830 | 530812 | return 0; | |
| 831 | } | ||
| 832 | |||
| 833 | #define CHUNK_START 0x1000 | ||
| 834 | |||
| 835 | 28815 | int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, | |
| 836 | int (*compare)(AVFormatContext *, const AVPacket *, const AVPacket *)) | ||
| 837 | { | ||
| 838 | int ret; | ||
| 839 | 28815 | FFFormatContext *const si = ffformatcontext(s); | |
| 840 | PacketListEntry **next_point, *this_pktl; | ||
| 841 | 28815 | AVStream *st = s->streams[pkt->stream_index]; | |
| 842 | 28815 | FFStream *const sti = ffstream(st); | |
| 843 | 
        2/4✓ Branch 0 taken 28815 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 28815 times. 
         | 
      28815 | int chunked = s->max_chunk_size || s->max_chunk_duration; | 
| 844 | |||
| 845 | 28815 | this_pktl = av_malloc(sizeof(*this_pktl)); | |
| 846 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 28815 times. 
         | 
      28815 | if (!this_pktl) { | 
| 847 | ✗ | av_packet_unref(pkt); | |
| 848 | ✗ | return AVERROR(ENOMEM); | |
| 849 | } | ||
| 850 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 28815 times. 
         | 
      28815 | if ((ret = av_packet_make_refcounted(pkt)) < 0) { | 
| 851 | ✗ | av_free(this_pktl); | |
| 852 | ✗ | av_packet_unref(pkt); | |
| 853 | ✗ | return ret; | |
| 854 | } | ||
| 855 | |||
| 856 | 28815 | av_packet_move_ref(&this_pktl->pkt, pkt); | |
| 857 | 28815 | pkt = &this_pktl->pkt; | |
| 858 | |||
| 859 | 
        2/2✓ Branch 0 taken 15755 times. 
          ✓ Branch 1 taken 13060 times. 
         | 
      28815 | if (sti->last_in_packet_buffer) { | 
| 860 | 15755 | next_point = &(sti->last_in_packet_buffer->next); | |
| 861 | } else { | ||
| 862 | 13060 | next_point = &si->packet_buffer.head; | |
| 863 | } | ||
| 864 | |||
| 865 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 28815 times. 
         | 
      28815 | if (chunked) { | 
| 866 | ✗ | uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP); | |
| 867 | ✗ | sti->interleaver_chunk_size += pkt->size; | |
| 868 | ✗ | sti->interleaver_chunk_duration += pkt->duration; | |
| 869 | ✗ | if ( (s->max_chunk_size && sti->interleaver_chunk_size > s->max_chunk_size) | |
| 870 | ✗ | || (max && sti->interleaver_chunk_duration > max)) { | |
| 871 | ✗ | sti->interleaver_chunk_size = 0; | |
| 872 | ✗ | pkt->flags |= CHUNK_START; | |
| 873 | ✗ | if (max && sti->interleaver_chunk_duration > max) { | |
| 874 | ✗ | int64_t syncoffset = (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)*max/2; | |
| 875 | ✗ | int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset; | |
| 876 | |||
| 877 | ✗ | sti->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max; | |
| 878 | } else | ||
| 879 | ✗ | sti->interleaver_chunk_duration = 0; | |
| 880 | } | ||
| 881 | } | ||
| 882 | 
        2/2✓ Branch 0 taken 14091 times. 
          ✓ Branch 1 taken 14724 times. 
         | 
      28815 | if (*next_point) { | 
| 883 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 14091 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      14091 | if (chunked && !(pkt->flags & CHUNK_START)) | 
| 884 | ✗ | goto next_non_null; | |
| 885 | |||
| 886 | 
        2/2✓ Branch 1 taken 8171 times. 
          ✓ Branch 2 taken 5920 times. 
         | 
      14091 | if (compare(s, &si->packet_buffer.tail->pkt, pkt)) { | 
| 887 | 8171 | while ( *next_point | |
| 888 | 
        2/6✓ Branch 0 taken 19793 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 19793 times. 
          ✗ Branch 4 not taken. 
          ✗ Branch 5 not taken. 
         | 
      19793 | && ((chunked && !((*next_point)->pkt.flags&CHUNK_START)) | 
| 889 | 
        2/2✓ Branch 1 taken 11622 times. 
          ✓ Branch 2 taken 8171 times. 
         | 
      19793 | || !compare(s, &(*next_point)->pkt, pkt))) | 
| 890 | 11622 | next_point = &(*next_point)->next; | |
| 891 | 
        1/2✓ Branch 0 taken 8171 times. 
          ✗ Branch 1 not taken. 
         | 
      8171 | if (*next_point) | 
| 892 | 8171 | goto next_non_null; | |
| 893 | } else { | ||
| 894 | 5920 | next_point = &(si->packet_buffer.tail->next); | |
| 895 | } | ||
| 896 | } | ||
| 897 | av_assert1(!*next_point); | ||
| 898 | |||
| 899 | 20644 | si->packet_buffer.tail = this_pktl; | |
| 900 | 28815 | next_non_null: | |
| 901 | |||
| 902 | 28815 | this_pktl->next = *next_point; | |
| 903 | |||
| 904 | 28815 | sti->last_in_packet_buffer = *next_point = this_pktl; | |
| 905 | |||
| 906 | 28815 | return 0; | |
| 907 | } | ||
| 908 | |||
| 909 | 33205 | static int interleave_compare_dts(AVFormatContext *s, const AVPacket *next, | |
| 910 | const AVPacket *pkt) | ||
| 911 | { | ||
| 912 | 33205 | AVStream *st = s->streams[pkt->stream_index]; | |
| 913 | 33205 | AVStream *st2 = s->streams[next->stream_index]; | |
| 914 | 33205 | int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts, | |
| 915 | st->time_base); | ||
| 916 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 33205 times. 
         | 
      33205 | if (s->audio_preload) { | 
| 917 | ✗ | int preload = st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO; | |
| 918 | ✗ | int preload2 = st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO; | |
| 919 | ✗ | if (preload != preload2) { | |
| 920 | int64_t ts, ts2; | ||
| 921 | ✗ | preload *= s->audio_preload; | |
| 922 | ✗ | preload2 *= s->audio_preload; | |
| 923 | ✗ | ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - preload; | |
| 924 | ✗ | ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - preload2; | |
| 925 | ✗ | if (ts == ts2) { | |
| 926 | ✗ | ts = ((uint64_t)pkt ->dts*st ->time_base.num*AV_TIME_BASE - (uint64_t)preload *st ->time_base.den)*st2->time_base.den | |
| 927 | ✗ | - ((uint64_t)next->dts*st2->time_base.num*AV_TIME_BASE - (uint64_t)preload2*st2->time_base.den)*st ->time_base.den; | |
| 928 | ✗ | ts2 = 0; | |
| 929 | } | ||
| 930 | ✗ | comp = (ts2 > ts) - (ts2 < ts); | |
| 931 | } | ||
| 932 | } | ||
| 933 | |||
| 934 | 
        2/2✓ Branch 0 taken 5811 times. 
          ✓ Branch 1 taken 27394 times. 
         | 
      33205 | if (comp == 0) | 
| 935 | 5811 | return pkt->stream_index < next->stream_index; | |
| 936 | 27394 | return comp > 0; | |
| 937 | } | ||
| 938 | |||
| 939 | 56852 | int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *pkt, | |
| 940 | int flush, int has_packet) | ||
| 941 | { | ||
| 942 | 56852 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 943 | 56852 | FFFormatContext *const si = &fci->fc; | |
| 944 | 56852 | int stream_count = 0; | |
| 945 | 56852 | int noninterleaved_count = 0; | |
| 946 | int ret; | ||
| 947 | |||
| 948 | 
        2/2✓ Branch 0 taken 28184 times. 
          ✓ Branch 1 taken 28668 times. 
         | 
      56852 | if (has_packet) { | 
| 949 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 28184 times. 
         | 
      28184 | if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0) | 
| 950 | ✗ | return ret; | |
| 951 | } | ||
| 952 | |||
| 953 | 
        2/2✓ Branch 0 taken 164481 times. 
          ✓ Branch 1 taken 56852 times. 
         | 
      221333 | for (unsigned i = 0; i < s->nb_streams; i++) { | 
| 954 | 164481 | const AVStream *const st = s->streams[i]; | |
| 955 | 164481 | const FFStream *const sti = cffstream(st); | |
| 956 | 164481 | const AVCodecParameters *const par = st->codecpar; | |
| 957 | 
        2/2✓ Branch 0 taken 123422 times. 
          ✓ Branch 1 taken 41059 times. 
         | 
      164481 | if (sti->last_in_packet_buffer) { | 
| 958 | 123422 | ++stream_count; | |
| 959 | 
        2/2✓ Branch 0 taken 40930 times. 
          ✓ Branch 1 taken 129 times. 
         | 
      41059 | } else if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT && | 
| 960 | 
        2/2✓ Branch 0 taken 40559 times. 
          ✓ Branch 1 taken 371 times. 
         | 
      40930 | par->codec_id != AV_CODEC_ID_VP8 && | 
| 961 | 
        2/2✓ Branch 0 taken 40483 times. 
          ✓ Branch 1 taken 76 times. 
         | 
      40559 | par->codec_id != AV_CODEC_ID_VP9 && | 
| 962 | 
        1/2✓ Branch 0 taken 40483 times. 
          ✗ Branch 1 not taken. 
         | 
      40483 | par->codec_id != AV_CODEC_ID_SMPTE_2038) { | 
| 963 | 40483 | ++noninterleaved_count; | |
| 964 | } | ||
| 965 | } | ||
| 966 | |||
| 967 | 
        2/2✓ Branch 0 taken 24115 times. 
          ✓ Branch 1 taken 32737 times. 
         | 
      56852 | if (fci->nb_interleaved_streams == stream_count) | 
| 968 | 24115 | flush = 1; | |
| 969 | |||
| 970 | 
        2/2✓ Branch 0 taken 56723 times. 
          ✓ Branch 1 taken 129 times. 
         | 
      56852 | if (s->max_interleave_delta > 0 && | 
| 971 | 
        2/2✓ Branch 0 taken 56412 times. 
          ✓ Branch 1 taken 311 times. 
         | 
      56723 | si->packet_buffer.head && | 
| 972 | 
        3/4✓ Branch 0 taken 56412 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 29268 times. 
          ✓ Branch 3 taken 27144 times. 
         | 
      56412 | si->packet_buffer.head->pkt.dts != AV_NOPTS_VALUE && | 
| 973 | 29268 | !flush && | |
| 974 | 
        2/2✓ Branch 0 taken 28857 times. 
          ✓ Branch 1 taken 411 times. 
         | 
      29268 | fci->nb_interleaved_streams == stream_count+noninterleaved_count | 
| 975 | ) { | ||
| 976 | 28857 | AVPacket *const top_pkt = &si->packet_buffer.head->pkt; | |
| 977 | 28857 | int64_t delta_dts = INT64_MIN; | |
| 978 | 28857 | int64_t top_dts = av_rescale_q(top_pkt->dts, | |
| 979 | 28857 | s->streams[top_pkt->stream_index]->time_base, | |
| 980 | 28857 | AV_TIME_BASE_Q); | |
| 981 | |||
| 982 | 
        2/2✓ Branch 0 taken 83409 times. 
          ✓ Branch 1 taken 28857 times. 
         | 
      112266 | for (unsigned i = 0; i < s->nb_streams; i++) { | 
| 983 | 83409 | const AVStream *const st = s->streams[i]; | |
| 984 | 83409 | const FFStream *const sti = cffstream(st); | |
| 985 | 83409 | const PacketListEntry *const last = sti->last_in_packet_buffer; | |
| 986 | int64_t last_dts; | ||
| 987 | |||
| 988 | 
        4/4✓ Branch 0 taken 50273 times. 
          ✓ Branch 1 taken 33136 times. 
          ✓ Branch 2 taken 1828 times. 
          ✓ Branch 3 taken 48445 times. 
         | 
      83409 | if (!last || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) | 
| 989 | 34964 | continue; | |
| 990 | |||
| 991 | 48445 | last_dts = av_rescale_q(last->pkt.dts, | |
| 992 | st->time_base, | ||
| 993 | 48445 | AV_TIME_BASE_Q); | |
| 994 | 48445 | delta_dts = FFMAX(delta_dts, last_dts - top_dts); | |
| 995 | } | ||
| 996 | |||
| 997 | 
        2/2✓ Branch 0 taken 1062 times. 
          ✓ Branch 1 taken 27795 times. 
         | 
      28857 | if (delta_dts > s->max_interleave_delta) { | 
| 998 | 1062 | av_log(s, AV_LOG_DEBUG, | |
| 999 | "Delay between the first packet and last packet in the " | ||
| 1000 | "muxing queue is %"PRId64" > %"PRId64": forcing output\n", | ||
| 1001 | delta_dts, s->max_interleave_delta); | ||
| 1002 | 1062 | flush = 1; | |
| 1003 | } | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | 
        4/4✓ Branch 0 taken 56540 times. 
          ✓ Branch 1 taken 312 times. 
          ✓ Branch 2 taken 28270 times. 
          ✓ Branch 3 taken 28270 times. 
         | 
      56852 | if (stream_count && flush) { | 
| 1007 | 28270 | PacketListEntry *pktl = si->packet_buffer.head; | |
| 1008 | 28270 | AVStream *const st = s->streams[pktl->pkt.stream_index]; | |
| 1009 | 28270 | FFStream *const sti = ffstream(st); | |
| 1010 | |||
| 1011 | 
        2/2✓ Branch 0 taken 12717 times. 
          ✓ Branch 1 taken 15553 times. 
         | 
      28270 | if (sti->last_in_packet_buffer == pktl) | 
| 1012 | 12717 | sti->last_in_packet_buffer = NULL; | |
| 1013 | 28270 | avpriv_packet_list_get(&si->packet_buffer, pkt); | |
| 1014 | |||
| 1015 | 28270 | return 1; | |
| 1016 | } else { | ||
| 1017 | 28582 | return 0; | |
| 1018 | } | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | 986497 | int ff_interleave_packet_passthrough(AVFormatContext *s, AVPacket *pkt, | |
| 1022 | int flush, int has_packet) | ||
| 1023 | { | ||
| 1024 | 986497 | return has_packet; | |
| 1025 | } | ||
| 1026 | |||
| 1027 | 6 | int ff_get_muxer_ts_offset(AVFormatContext *s, int stream_index, int64_t *offset) | |
| 1028 | { | ||
| 1029 | AVStream *st; | ||
| 1030 | |||
| 1031 | 
        2/4✓ Branch 0 taken 6 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 6 times. 
         | 
      6 | if (stream_index < 0 || stream_index >= s->nb_streams) | 
| 1032 | ✗ | return AVERROR(EINVAL); | |
| 1033 | |||
| 1034 | 6 | st = s->streams[stream_index]; | |
| 1035 | 6 | *offset = ffstream(st)->mux_ts_offset; | |
| 1036 | |||
| 1037 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 6 times. 
         | 
      6 | if (s->output_ts_offset) | 
| 1038 | ✗ | *offset += av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base); | |
| 1039 | |||
| 1040 | 6 | return 0; | |
| 1041 | } | ||
| 1042 | |||
| 1043 | 162 | const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream) | |
| 1044 | { | ||
| 1045 | 162 | FFFormatContext *const si = ffformatcontext(s); | |
| 1046 | 162 | PacketListEntry *pktl = si->packet_buffer.head; | |
| 1047 | 
        2/2✓ Branch 0 taken 64 times. 
          ✓ Branch 1 taken 156 times. 
         | 
      220 | while (pktl) { | 
| 1048 | 
        2/2✓ Branch 0 taken 6 times. 
          ✓ Branch 1 taken 58 times. 
         | 
      64 | if (pktl->pkt.stream_index == stream) { | 
| 1049 | 6 | return &pktl->pkt; | |
| 1050 | } | ||
| 1051 | 58 | pktl = pktl->next; | |
| 1052 | } | ||
| 1053 | 156 | return NULL; | |
| 1054 | } | ||
| 1055 | |||
| 1056 | 530816 | static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt) | |
| 1057 | { | ||
| 1058 | int ret; | ||
| 1059 | |||
| 1060 | 
        2/2✓ Branch 0 taken 7894 times. 
          ✓ Branch 1 taken 522922 times. 
         | 
      530816 | if (!(s->flags & AVFMT_FLAG_AUTO_BSF)) | 
| 1061 | 7894 | return 1; | |
| 1062 | |||
| 1063 | 
        2/2✓ Branch 1 taken 38463 times. 
          ✓ Branch 2 taken 484459 times. 
         | 
      522922 | if (ffofmt(s->oformat)->check_bitstream) { | 
| 1064 | 
        2/2✓ Branch 0 taken 465 times. 
          ✓ Branch 1 taken 37998 times. 
         | 
      38463 | if (!sti->bitstream_checked) { | 
| 1065 | 
        1/2✗ Branch 2 not taken. 
          ✓ Branch 3 taken 465 times. 
         | 
      465 | if ((ret = ffofmt(s->oformat)->check_bitstream(s, &sti->pub, pkt)) < 0) | 
| 1066 | ✗ | return ret; | |
| 1067 | 
        1/2✓ Branch 0 taken 465 times. 
          ✗ Branch 1 not taken. 
         | 
      465 | else if (ret == 1) | 
| 1068 | 465 | sti->bitstream_checked = 1; | |
| 1069 | } | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | 522922 | return 1; | |
| 1073 | } | ||
| 1074 | |||
| 1075 | 526594 | static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, | |
| 1076 | int flush, int has_packet) | ||
| 1077 | { | ||
| 1078 | 526594 | FormatContextInternal *const fci = ff_fc_internal(s); | |
| 1079 | |||
| 1080 | 517849 | for (;; ) { | |
| 1081 | 1044443 | int ret = fci->interleave_packet(s, pkt, flush, has_packet); | |
| 1082 | 
        2/2✓ Branch 0 taken 526594 times. 
          ✓ Branch 1 taken 517849 times. 
         | 
      1044443 | if (ret <= 0) | 
| 1083 | 526594 | return ret; | |
| 1084 | |||
| 1085 | 517849 | has_packet = 0; | |
| 1086 | |||
| 1087 | 517849 | ret = write_packet(s, pkt); | |
| 1088 | 517849 | av_packet_unref(pkt); | |
| 1089 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 517849 times. 
         | 
      517849 | if (ret < 0) | 
| 1090 | ✗ | return ret; | |
| 1091 | } | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | 530943 | static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved) | |
| 1095 | { | ||
| 1096 | int ret; | ||
| 1097 | |||
| 1098 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530943 times. 
         | 
      530943 | if (s->debug & FF_FDEBUG_TS) | 
| 1099 | ✗ | av_log(s, AV_LOG_DEBUG, "%s size:%d dts:%s pts:%s\n", __func__, | |
| 1100 | ✗ | pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts)); | |
| 1101 | |||
| 1102 | 530943 | guess_pkt_duration(s, st, pkt); | |
| 1103 | |||
| 1104 | #if FF_API_COMPUTE_PKT_FIELDS2 | ||
| 1105 | 
        3/4✓ Branch 1 taken 42 times. 
          ✓ Branch 2 taken 530901 times. 
          ✗ Branch 3 not taken. 
          ✓ Branch 4 taken 42 times. 
         | 
      530943 | if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) | 
| 1106 | ✗ | return ret; | |
| 1107 | #endif | ||
| 1108 | |||
| 1109 | 
        2/2✓ Branch 0 taken 517862 times. 
          ✓ Branch 1 taken 13081 times. 
         | 
      530943 | if (interleaved) { | 
| 1110 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 517862 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      517862 | if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) | 
| 1111 | ✗ | return AVERROR(EINVAL); | |
| 1112 | 517862 | return interleaved_write_packet(s, pkt, 0, 1); | |
| 1113 | } else { | ||
| 1114 | 13081 | return write_packet(s, pkt); | |
| 1115 | } | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | 1122 | static int write_packets_from_bsfs(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved) | |
| 1119 | { | ||
| 1120 | 1122 | FFStream *const sti = ffstream(st); | |
| 1121 | 1122 | AVBSFContext *const bsfc = sti->bsfc; | |
| 1122 | int ret; | ||
| 1123 | |||
| 1124 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1122 times. 
         | 
      1122 | if ((ret = av_bsf_send_packet(bsfc, pkt)) < 0) { | 
| 1125 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 1126 | "Failed to send packet to filter %s for stream %d\n", | ||
| 1127 | ✗ | bsfc->filter->name, st->index); | |
| 1128 | ✗ | return ret; | |
| 1129 | } | ||
| 1130 | |||
| 1131 | do { | ||
| 1132 | 2326 | ret = av_bsf_receive_packet(bsfc, pkt); | |
| 1133 | 
        2/2✓ Branch 0 taken 1122 times. 
          ✓ Branch 1 taken 1204 times. 
         | 
      2326 | if (ret < 0) { | 
| 1134 | 
        3/4✓ Branch 0 taken 45 times. 
          ✓ Branch 1 taken 1077 times. 
          ✓ Branch 2 taken 45 times. 
          ✗ Branch 3 not taken. 
         | 
      1122 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) | 
| 1135 | 1122 | return 0; | |
| 1136 | ✗ | av_log(s, AV_LOG_ERROR, "Error applying bitstream filters to an output " | |
| 1137 | ✗ | "packet for stream #%d: %s\n", st->index, av_err2str(ret)); | |
| 1138 | ✗ | if (!(s->error_recognition & AV_EF_EXPLODE) && ret != AVERROR(ENOMEM)) | |
| 1139 | ✗ | continue; | |
| 1140 | ✗ | return ret; | |
| 1141 | } | ||
| 1142 | 1204 | av_packet_rescale_ts(pkt, bsfc->time_base_out, st->time_base); | |
| 1143 | 1204 | ret = write_packet_common(s, st, pkt, interleaved); | |
| 1144 | 
        2/4✓ Branch 0 taken 1204 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 1204 times. 
         | 
      1204 | if (ret >= 0 && !interleaved) // a successful write_packet_common already unrefed pkt for interleaved | 
| 1145 | ✗ | av_packet_unref(pkt); | |
| 1146 | 
        1/2✓ Branch 0 taken 1204 times. 
          ✗ Branch 1 not taken. 
         | 
      1204 | } while (ret >= 0); | 
| 1147 | |||
| 1148 | ✗ | return ret; | |
| 1149 | } | ||
| 1150 | |||
| 1151 | 530816 | static int write_packets_common(AVFormatContext *s, AVPacket *pkt, int interleaved) | |
| 1152 | { | ||
| 1153 | AVStream *st; | ||
| 1154 | FFStream *sti; | ||
| 1155 | 530816 | int ret = check_packet(s, pkt); | |
| 1156 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530816 times. 
         | 
      530816 | if (ret < 0) | 
| 1157 | ✗ | return ret; | |
| 1158 | 530816 | st = s->streams[pkt->stream_index]; | |
| 1159 | 530816 | sti = ffstream(st); | |
| 1160 | |||
| 1161 | 530816 | ret = prepare_input_packet(s, st, pkt); | |
| 1162 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530816 times. 
         | 
      530816 | if (ret < 0) | 
| 1163 | ✗ | return ret; | |
| 1164 | |||
| 1165 | 530816 | ret = check_bitstream(s, sti, pkt); | |
| 1166 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 530816 times. 
         | 
      530816 | if (ret < 0) | 
| 1167 | ✗ | return ret; | |
| 1168 | |||
| 1169 | 
        2/2✓ Branch 0 taken 1077 times. 
          ✓ Branch 1 taken 529739 times. 
         | 
      530816 | if (sti->bsfc) { | 
| 1170 | 1077 | return write_packets_from_bsfs(s, st, pkt, interleaved); | |
| 1171 | } else { | ||
| 1172 | 529739 | return write_packet_common(s, st, pkt, interleaved); | |
| 1173 | } | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | 13247 | int av_write_frame(AVFormatContext *s, AVPacket *in) | |
| 1177 | { | ||
| 1178 | 13247 | FFFormatContext *const si = ffformatcontext(s); | |
| 1179 | 13247 | AVPacket *pkt = si->parse_pkt; | |
| 1180 | int ret; | ||
| 1181 | |||
| 1182 | 
        2/2✓ Branch 0 taken 166 times. 
          ✓ Branch 1 taken 13081 times. 
         | 
      13247 | if (!in) { | 
| 1183 | 
        1/2✓ Branch 1 taken 166 times. 
          ✗ Branch 2 not taken. 
         | 
      166 | if (ffofmt(s->oformat)->flags_internal & FF_OFMT_FLAG_ALLOW_FLUSH) { | 
| 1184 | 166 | ret = ffofmt(s->oformat)->write_packet(s, NULL); | |
| 1185 | 166 | flush_if_needed(s); | |
| 1186 | 
        4/6✓ Branch 0 taken 166 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 160 times. 
          ✓ Branch 3 taken 6 times. 
          ✗ Branch 4 not taken. 
          ✓ Branch 5 taken 160 times. 
         | 
      166 | if (ret >= 0 && s->pb && s->pb->error < 0) | 
| 1187 | ✗ | ret = s->pb->error; | |
| 1188 | 166 | return ret; | |
| 1189 | } | ||
| 1190 | ✗ | return 1; | |
| 1191 | } | ||
| 1192 | |||
| 1193 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 13081 times. 
         | 
      13081 | if (in->flags & AV_PKT_FLAG_UNCODED_FRAME) { | 
| 1194 | ✗ | pkt = in; | |
| 1195 | } else { | ||
| 1196 | /* We don't own in, so we have to make sure not to modify it. | ||
| 1197 | * (ff_write_chained() relies on this fact.) | ||
| 1198 | * The following avoids copying in's data unnecessarily. | ||
| 1199 | * Copying side data is unavoidable as a bitstream filter | ||
| 1200 | * may change it, e.g. free it on errors. */ | ||
| 1201 | 13081 | pkt->data = in->data; | |
| 1202 | 13081 | pkt->size = in->size; | |
| 1203 | 13081 | ret = av_packet_copy_props(pkt, in); | |
| 1204 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 13081 times. 
         | 
      13081 | if (ret < 0) | 
| 1205 | ✗ | return ret; | |
| 1206 | 
        2/2✓ Branch 0 taken 8392 times. 
          ✓ Branch 1 taken 4689 times. 
         | 
      13081 | if (in->buf) { | 
| 1207 | 8392 | pkt->buf = av_buffer_ref(in->buf); | |
| 1208 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 8392 times. 
         | 
      8392 | if (!pkt->buf) { | 
| 1209 | ✗ | ret = AVERROR(ENOMEM); | |
| 1210 | ✗ | goto fail; | |
| 1211 | } | ||
| 1212 | } | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | 13081 | ret = write_packets_common(s, pkt, 0/*non-interleaved*/); | |
| 1216 | |||
| 1217 | 13081 | fail: | |
| 1218 | // Uncoded frames using the noninterleaved codepath are also freed here | ||
| 1219 | 13081 | av_packet_unref(pkt); | |
| 1220 | 13081 | return ret; | |
| 1221 | } | ||
| 1222 | |||
| 1223 | 517735 | int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) | |
| 1224 | { | ||
| 1225 | int ret; | ||
| 1226 | |||
| 1227 | 
        1/2✓ Branch 0 taken 517735 times. 
          ✗ Branch 1 not taken. 
         | 
      517735 | if (pkt) { | 
| 1228 | 517735 | ret = write_packets_common(s, pkt, 1/*interleaved*/); | |
| 1229 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 517735 times. 
         | 
      517735 | if (ret < 0) | 
| 1230 | ✗ | av_packet_unref(pkt); | |
| 1231 | 517735 | return ret; | |
| 1232 | } else { | ||
| 1233 | ✗ | av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n"); | |
| 1234 | ✗ | return interleaved_write_packet(s, ffformatcontext(s)->parse_pkt, 1/*flush*/, 0); | |
| 1235 | } | ||
| 1236 | } | ||
| 1237 | |||
| 1238 | 8732 | int av_write_trailer(AVFormatContext *s) | |
| 1239 | { | ||
| 1240 | 8732 | FFFormatContext *const si = ffformatcontext(s); | |
| 1241 | 8732 | AVPacket *const pkt = si->parse_pkt; | |
| 1242 | 8732 | int ret1, ret = 0; | |
| 1243 | |||
| 1244 | 
        2/2✓ Branch 0 taken 9252 times. 
          ✓ Branch 1 taken 8732 times. 
         | 
      17984 | for (unsigned i = 0; i < s->nb_streams; i++) { | 
| 1245 | 9252 | AVStream *const st = s->streams[i]; | |
| 1246 | 9252 | FFStream *const sti = ffstream(st); | |
| 1247 | 
        2/2✓ Branch 0 taken 45 times. 
          ✓ Branch 1 taken 9207 times. 
         | 
      9252 | if (sti->bsfc) { | 
| 1248 | 45 | ret1 = write_packets_from_bsfs(s, st, pkt, 1/*interleaved*/); | |
| 1249 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 45 times. 
         | 
      45 | if (ret1 < 0) | 
| 1250 | ✗ | av_packet_unref(pkt); | |
| 1251 | 
        1/2✓ Branch 0 taken 45 times. 
          ✗ Branch 1 not taken. 
         | 
      45 | if (ret >= 0) | 
| 1252 | 45 | ret = ret1; | |
| 1253 | } | ||
| 1254 | } | ||
| 1255 | 8732 | ret1 = interleaved_write_packet(s, pkt, 1, 0); | |
| 1256 | 
        1/2✓ Branch 0 taken 8732 times. 
          ✗ Branch 1 not taken. 
         | 
      8732 | if (ret >= 0) | 
| 1257 | 8732 | ret = ret1; | |
| 1258 | |||
| 1259 | 
        2/2✓ Branch 1 taken 4468 times. 
          ✓ Branch 2 taken 4264 times. 
         | 
      8732 | if (ffofmt(s->oformat)->write_trailer) { | 
| 1260 | 
        3/4✓ Branch 0 taken 4397 times. 
          ✓ Branch 1 taken 71 times. 
          ✓ Branch 2 taken 4397 times. 
          ✗ Branch 3 not taken. 
         | 
      4468 | if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb) | 
| 1261 | 4397 | avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER); | |
| 1262 | 4468 | ret1 = ffofmt(s->oformat)->write_trailer(s); | |
| 1263 | 
        1/2✓ Branch 0 taken 4468 times. 
          ✗ Branch 1 not taken. 
         | 
      4468 | if (ret >= 0) | 
| 1264 | 4468 | ret = ret1; | |
| 1265 | } | ||
| 1266 | |||
| 1267 | 8732 | deinit_muxer(s); | |
| 1268 | |||
| 1269 | 
        2/2✓ Branch 0 taken 8580 times. 
          ✓ Branch 1 taken 152 times. 
         | 
      8732 | if (s->pb) | 
| 1270 | 8580 | avio_flush(s->pb); | |
| 1271 | 
        2/2✓ Branch 0 taken 8696 times. 
          ✓ Branch 1 taken 36 times. 
         | 
      8732 | if (ret == 0) | 
| 1272 | 
        2/2✓ Branch 0 taken 8544 times. 
          ✓ Branch 1 taken 152 times. 
         | 
      8696 | ret = s->pb ? s->pb->error : 0; | 
| 1273 | 
        2/2✓ Branch 0 taken 9252 times. 
          ✓ Branch 1 taken 8732 times. 
         | 
      17984 | for (unsigned i = 0; i < s->nb_streams; i++) { | 
| 1274 | 9252 | av_freep(&s->streams[i]->priv_data); | |
| 1275 | 9252 | av_freep(&ffstream(s->streams[i])->index_entries); | |
| 1276 | } | ||
| 1277 | 
        2/2✓ Branch 0 taken 4600 times. 
          ✓ Branch 1 taken 4132 times. 
         | 
      8732 | if (s->oformat->priv_class) | 
| 1278 | 4600 | av_opt_free(s->priv_data); | |
| 1279 | 8732 | av_freep(&s->priv_data); | |
| 1280 | 8732 | av_packet_unref(si->pkt); | |
| 1281 | 8732 | return ret; | |
| 1282 | } | ||
| 1283 | |||
| 1284 | ✗ | int av_get_output_timestamp(struct AVFormatContext *s, int stream, | |
| 1285 | int64_t *dts, int64_t *wall) | ||
| 1286 | { | ||
| 1287 | ✗ | const FFOutputFormat *const of = ffofmt(s->oformat); | |
| 1288 | ✗ | if (!of || !of->get_output_timestamp) | |
| 1289 | ✗ | return AVERROR(ENOSYS); | |
| 1290 | ✗ | of->get_output_timestamp(s, stream, dts, wall); | |
| 1291 | ✗ | return 0; | |
| 1292 | } | ||
| 1293 | |||
| 1294 | 45 | int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args) | |
| 1295 | { | ||
| 1296 | int ret; | ||
| 1297 | const AVBitStreamFilter *bsf; | ||
| 1298 | 45 | FFStream *const sti = ffstream(st); | |
| 1299 | AVBSFContext *bsfc; | ||
| 1300 | |||
| 1301 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 45 times. 
         | 
      45 | av_assert0(!sti->bsfc); | 
| 1302 | |||
| 1303 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 45 times. 
         | 
      45 | if (!(bsf = av_bsf_get_by_name(name))) { | 
| 1304 | ✗ | av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name); | |
| 1305 | ✗ | return AVERROR_BSF_NOT_FOUND; | |
| 1306 | } | ||
| 1307 | |||
| 1308 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 45 times. 
         | 
      45 | if ((ret = av_bsf_alloc(bsf, &bsfc)) < 0) | 
| 1309 | ✗ | return ret; | |
| 1310 | |||
| 1311 | 45 | bsfc->time_base_in = st->time_base; | |
| 1312 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 45 times. 
         | 
      45 | if ((ret = avcodec_parameters_copy(bsfc->par_in, st->codecpar)) < 0) { | 
| 1313 | ✗ | av_bsf_free(&bsfc); | |
| 1314 | ✗ | return ret; | |
| 1315 | } | ||
| 1316 | |||
| 1317 | 
        3/4✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 25 times. 
          ✓ Branch 2 taken 20 times. 
          ✗ Branch 3 not taken. 
         | 
      45 | if (args && bsfc->filter->priv_class) { | 
| 1318 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 20 times. 
         | 
      20 | if ((ret = av_set_options_string(bsfc->priv_data, args, "=", ":")) < 0) { | 
| 1319 | ✗ | av_bsf_free(&bsfc); | |
| 1320 | ✗ | return ret; | |
| 1321 | } | ||
| 1322 | } | ||
| 1323 | |||
| 1324 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 45 times. 
         | 
      45 | if ((ret = av_bsf_init(bsfc)) < 0) { | 
| 1325 | ✗ | av_bsf_free(&bsfc); | |
| 1326 | ✗ | return ret; | |
| 1327 | } | ||
| 1328 | |||
| 1329 | 45 | sti->bsfc = bsfc; | |
| 1330 | |||
| 1331 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 25 times. 
         | 
      45 | av_log(NULL, AV_LOG_VERBOSE, | 
| 1332 | "Automatically inserted bitstream filter '%s'; args='%s'\n", | ||
| 1333 | name, args ? args : ""); | ||
| 1334 | 45 | return 1; | |
| 1335 | } | ||
| 1336 | |||
| 1337 | 8062 | int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, | |
| 1338 | AVFormatContext *src, int interleave) | ||
| 1339 | { | ||
| 1340 | 8062 | int64_t pts = pkt->pts, dts = pkt->dts, duration = pkt->duration; | |
| 1341 | 8062 | int stream_index = pkt->stream_index; | |
| 1342 | 8062 | AVRational time_base = pkt->time_base; | |
| 1343 | int ret; | ||
| 1344 | |||
| 1345 | 8062 | pkt->stream_index = dst_stream; | |
| 1346 | |||
| 1347 | 8062 | av_packet_rescale_ts(pkt, | |
| 1348 | 8062 | src->streams[stream_index]->time_base, | |
| 1349 | 8062 | dst->streams[dst_stream]->time_base); | |
| 1350 | |||
| 1351 | 
        1/2✓ Branch 0 taken 8062 times. 
          ✗ Branch 1 not taken. 
         | 
      8062 | if (!interleave) { | 
| 1352 | 8062 | ret = av_write_frame(dst, pkt); | |
| 1353 | /* We only have to backup and restore the fields that | ||
| 1354 | * we changed ourselves, because av_write_frame() does not | ||
| 1355 | * modify the packet given to it. */ | ||
| 1356 | 8062 | pkt->pts = pts; | |
| 1357 | 8062 | pkt->dts = dts; | |
| 1358 | 8062 | pkt->duration = duration; | |
| 1359 | 8062 | pkt->stream_index = stream_index; | |
| 1360 | 8062 | pkt->time_base = time_base; | |
| 1361 | } else | ||
| 1362 | ✗ | ret = av_interleaved_write_frame(dst, pkt); | |
| 1363 | |||
| 1364 | 8062 | return ret; | |
| 1365 | } | ||
| 1366 | |||
| 1367 | ✗ | static void uncoded_frame_free(void *unused, uint8_t *data) | |
| 1368 | { | ||
| 1369 | ✗ | av_frame_free((AVFrame **)data); | |
| 1370 | ✗ | av_free(data); | |
| 1371 | ✗ | } | |
| 1372 | |||
| 1373 | ✗ | static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, | |
| 1374 | AVFrame *frame, int interleaved) | ||
| 1375 | { | ||
| 1376 | ✗ | FFFormatContext *const si = ffformatcontext(s); | |
| 1377 | ✗ | AVPacket *pkt = si->parse_pkt; | |
| 1378 | |||
| 1379 | ✗ | av_assert0(s->oformat); | |
| 1380 | ✗ | if (!ffofmt(s->oformat)->write_uncoded_frame) { | |
| 1381 | ✗ | av_frame_free(&frame); | |
| 1382 | ✗ | return AVERROR(ENOSYS); | |
| 1383 | } | ||
| 1384 | |||
| 1385 | ✗ | if (!frame) { | |
| 1386 | ✗ | pkt = NULL; | |
| 1387 | } else { | ||
| 1388 | ✗ | size_t bufsize = sizeof(frame) + AV_INPUT_BUFFER_PADDING_SIZE; | |
| 1389 | ✗ | AVFrame **framep = av_mallocz(bufsize); | |
| 1390 | |||
| 1391 | ✗ | if (!framep) | |
| 1392 | ✗ | goto fail; | |
| 1393 | ✗ | pkt->buf = av_buffer_create((void *)framep, bufsize, | |
| 1394 | uncoded_frame_free, NULL, 0); | ||
| 1395 | ✗ | if (!pkt->buf) { | |
| 1396 | ✗ | av_free(framep); | |
| 1397 | ✗ | fail: | |
| 1398 | ✗ | av_frame_free(&frame); | |
| 1399 | ✗ | return AVERROR(ENOMEM); | |
| 1400 | } | ||
| 1401 | ✗ | *framep = frame; | |
| 1402 | |||
| 1403 | ✗ | pkt->data = (void *)framep; | |
| 1404 | ✗ | pkt->size = sizeof(frame); | |
| 1405 | ✗ | pkt->pts = | |
| 1406 | ✗ | pkt->dts = frame->pts; | |
| 1407 | ✗ | pkt->duration = frame->duration; | |
| 1408 | ✗ | pkt->stream_index = stream_index; | |
| 1409 | ✗ | pkt->flags |= AV_PKT_FLAG_UNCODED_FRAME; | |
| 1410 | } | ||
| 1411 | |||
| 1412 | ✗ | return interleaved ? av_interleaved_write_frame(s, pkt) : | |
| 1413 | ✗ | av_write_frame(s, pkt); | |
| 1414 | } | ||
| 1415 | |||
| 1416 | ✗ | int av_write_uncoded_frame(AVFormatContext *s, int stream_index, | |
| 1417 | AVFrame *frame) | ||
| 1418 | { | ||
| 1419 | ✗ | return write_uncoded_frame_internal(s, stream_index, frame, 0); | |
| 1420 | } | ||
| 1421 | |||
| 1422 | ✗ | int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index, | |
| 1423 | AVFrame *frame) | ||
| 1424 | { | ||
| 1425 | ✗ | return write_uncoded_frame_internal(s, stream_index, frame, 1); | |
| 1426 | } | ||
| 1427 | |||
| 1428 | ✗ | int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index) | |
| 1429 | { | ||
| 1430 | ✗ | const FFOutputFormat *const of = ffofmt(s->oformat); | |
| 1431 | ✗ | av_assert0(of); | |
| 1432 | ✗ | if (!of->write_uncoded_frame) | |
| 1433 | ✗ | return AVERROR(ENOSYS); | |
| 1434 | ✗ | return of->write_uncoded_frame(s, stream_index, NULL, | |
| 1435 | AV_WRITE_UNCODED_FRAME_QUERY); | ||
| 1436 | } | ||
| 1437 |