fftools/ffmpeg_mux_init.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Muxer/output file setup. | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <string.h> | ||
| 22 | |||
| 23 | #include "cmdutils.h" | ||
| 24 | #include "ffmpeg.h" | ||
| 25 | #include "ffmpeg_mux.h" | ||
| 26 | #include "ffmpeg_sched.h" | ||
| 27 | #include "fopen_utf8.h" | ||
| 28 | |||
| 29 | #include "libavformat/avformat.h" | ||
| 30 | #include "libavformat/avio.h" | ||
| 31 | |||
| 32 | #include "libavcodec/avcodec.h" | ||
| 33 | |||
| 34 | #include "libavfilter/avfilter.h" | ||
| 35 | |||
| 36 | #include "libavutil/avassert.h" | ||
| 37 | #include "libavutil/avstring.h" | ||
| 38 | #include "libavutil/avutil.h" | ||
| 39 | #include "libavutil/bprint.h" | ||
| 40 | #include "libavutil/dict.h" | ||
| 41 | #include "libavutil/display.h" | ||
| 42 | #include "libavutil/getenv_utf8.h" | ||
| 43 | #include "libavutil/iamf.h" | ||
| 44 | #include "libavutil/intreadwrite.h" | ||
| 45 | #include "libavutil/log.h" | ||
| 46 | #include "libavutil/mem.h" | ||
| 47 | #include "libavutil/opt.h" | ||
| 48 | #include "libavutil/parseutils.h" | ||
| 49 | #include "libavutil/pixdesc.h" | ||
| 50 | #include "libavutil/stereo3d.h" | ||
| 51 | |||
| 52 | #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass" | ||
| 53 | |||
| 54 | 7098 | static int check_opt_bitexact(void *ctx, const AVDictionary *opts, | |
| 55 | const char *opt_name, int flag) | ||
| 56 | { | ||
| 57 | 7098 | const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0); | |
| 58 | |||
| 59 | 7098 | if (e) { | |
| 60 | 5968 | const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0); | |
| 61 | 5968 | int val = 0; | |
| 62 | 5968 | if (!o) | |
| 63 | ✗ | return 0; | |
| 64 | 5968 | av_opt_eval_flags(ctx, o, e->value, &val); | |
| 65 | 5968 | return !!(val & flag); | |
| 66 | } | ||
| 67 | 1130 | return 0; | |
| 68 | } | ||
| 69 | |||
| 70 | 9924 | static int choose_encoder(const OptionsContext *o, AVFormatContext *s, | |
| 71 | MuxStream *ms, const AVCodec **enc) | ||
| 72 | { | ||
| 73 | 9924 | OutputStream *ost = &ms->ost; | |
| 74 | 9924 | enum AVMediaType type = ost->type; | |
| 75 | 9924 | const char *codec_name = NULL; | |
| 76 | |||
| 77 | 9924 | *enc = NULL; | |
| 78 | |||
| 79 | 9924 | opt_match_per_stream_str(ost, &o->codec_names, s, ost->st, &codec_name); | |
| 80 | |||
| 81 | 9924 | if (type != AVMEDIA_TYPE_VIDEO && | |
| 82 | 111 | type != AVMEDIA_TYPE_AUDIO && | |
| 83 | type != AVMEDIA_TYPE_SUBTITLE) { | ||
| 84 | 13 | if (codec_name && strcmp(codec_name, "copy")) { | |
| 85 | ✗ | const char *type_str = av_get_media_type_string(type); | |
| 86 | ✗ | av_log(ost, AV_LOG_FATAL, | |
| 87 | "Encoder '%s' specified, but only '-codec copy' supported " | ||
| 88 | "for %s streams\n", codec_name, type_str); | ||
| 89 | ✗ | return AVERROR(ENOSYS); | |
| 90 | } | ||
| 91 | 13 | return 0; | |
| 92 | } | ||
| 93 | |||
| 94 | 9911 | if (!codec_name) { | |
| 95 | 4371 | ms->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type); | |
| 96 | 4371 | *enc = avcodec_find_encoder(ms->par_in->codec_id); | |
| 97 | 4371 | if (!*enc) { | |
| 98 | ✗ | av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed " | |
| 99 | "Default encoder for format %s (codec %s) is " | ||
| 100 | "probably disabled. Please choose an encoder manually.\n", | ||
| 101 | ✗ | s->oformat->name, avcodec_get_name(ms->par_in->codec_id)); | |
| 102 | ✗ | return AVERROR_ENCODER_NOT_FOUND; | |
| 103 | } | ||
| 104 | 5540 | } else if (strcmp(codec_name, "copy")) { | |
| 105 | 4480 | int ret = find_codec(ost, codec_name, ost->type, 1, enc); | |
| 106 | 4480 | if (ret < 0) | |
| 107 | ✗ | return ret; | |
| 108 | 4480 | ms->par_in->codec_id = (*enc)->id; | |
| 109 | } | ||
| 110 | |||
| 111 | 9911 | return 0; | |
| 112 | } | ||
| 113 | |||
| 114 | ✗ | static char *get_line(AVIOContext *s, AVBPrint *bprint) | |
| 115 | { | ||
| 116 | char c; | ||
| 117 | |||
| 118 | ✗ | while ((c = avio_r8(s)) && c != '\n') | |
| 119 | ✗ | av_bprint_chars(bprint, c, 1); | |
| 120 | |||
| 121 | ✗ | if (!av_bprint_is_complete(bprint)) | |
| 122 | ✗ | return NULL; | |
| 123 | |||
| 124 | ✗ | return bprint->str; | |
| 125 | } | ||
| 126 | |||
| 127 | ✗ | static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s) | |
| 128 | { | ||
| 129 | ✗ | int i, ret = -1; | |
| 130 | char filename[1000]; | ||
| 131 | ✗ | char *env_avconv_datadir = getenv_utf8("AVCONV_DATADIR"); | |
| 132 | ✗ | char *env_home = getenv_utf8("HOME"); | |
| 133 | ✗ | const char *base[3] = { env_avconv_datadir, | |
| 134 | env_home, | ||
| 135 | AVCONV_DATADIR, | ||
| 136 | }; | ||
| 137 | |||
| 138 | ✗ | for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) { | |
| 139 | ✗ | if (!base[i]) | |
| 140 | ✗ | continue; | |
| 141 | ✗ | if (codec_name) { | |
| 142 | ✗ | snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i], | |
| 143 | i != 1 ? "" : "/.avconv", codec_name, preset_name); | ||
| 144 | ✗ | ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL); | |
| 145 | } | ||
| 146 | ✗ | if (ret < 0) { | |
| 147 | ✗ | snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i], | |
| 148 | i != 1 ? "" : "/.avconv", preset_name); | ||
| 149 | ✗ | ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL); | |
| 150 | } | ||
| 151 | } | ||
| 152 | ✗ | freeenv_utf8(env_home); | |
| 153 | ✗ | freeenv_utf8(env_avconv_datadir); | |
| 154 | ✗ | return ret; | |
| 155 | } | ||
| 156 | |||
| 157 | typedef struct EncStatsFile { | ||
| 158 | char *path; | ||
| 159 | AVIOContext *io; | ||
| 160 | } EncStatsFile; | ||
| 161 | |||
| 162 | static EncStatsFile *enc_stats_files; | ||
| 163 | static int nb_enc_stats_files; | ||
| 164 | |||
| 165 | ✗ | static int enc_stats_get_file(AVIOContext **io, const char *path) | |
| 166 | { | ||
| 167 | EncStatsFile *esf; | ||
| 168 | int ret; | ||
| 169 | |||
| 170 | ✗ | for (int i = 0; i < nb_enc_stats_files; i++) | |
| 171 | ✗ | if (!strcmp(path, enc_stats_files[i].path)) { | |
| 172 | ✗ | *io = enc_stats_files[i].io; | |
| 173 | ✗ | return 0; | |
| 174 | } | ||
| 175 | |||
| 176 | ✗ | ret = GROW_ARRAY(enc_stats_files, nb_enc_stats_files); | |
| 177 | ✗ | if (ret < 0) | |
| 178 | ✗ | return ret; | |
| 179 | |||
| 180 | ✗ | esf = &enc_stats_files[nb_enc_stats_files - 1]; | |
| 181 | |||
| 182 | ✗ | ret = avio_open2(&esf->io, path, AVIO_FLAG_WRITE, &int_cb, NULL); | |
| 183 | ✗ | if (ret < 0) { | |
| 184 | ✗ | av_log(NULL, AV_LOG_ERROR, "Error opening stats file '%s': %s\n", | |
| 185 | ✗ | path, av_err2str(ret)); | |
| 186 | ✗ | return ret; | |
| 187 | } | ||
| 188 | |||
| 189 | ✗ | esf->path = av_strdup(path); | |
| 190 | ✗ | if (!esf->path) | |
| 191 | ✗ | return AVERROR(ENOMEM); | |
| 192 | |||
| 193 | ✗ | *io = esf->io; | |
| 194 | |||
| 195 | ✗ | return 0; | |
| 196 | } | ||
| 197 | |||
| 198 | 9220 | void of_enc_stats_close(void) | |
| 199 | { | ||
| 200 | 9220 | for (int i = 0; i < nb_enc_stats_files; i++) { | |
| 201 | ✗ | av_freep(&enc_stats_files[i].path); | |
| 202 | ✗ | avio_closep(&enc_stats_files[i].io); | |
| 203 | } | ||
| 204 | 9220 | av_freep(&enc_stats_files); | |
| 205 | 9220 | nb_enc_stats_files = 0; | |
| 206 | 9220 | } | |
| 207 | |||
| 208 | ✗ | static int unescape(char **pdst, size_t *dst_len, | |
| 209 | const char **pstr, char delim) | ||
| 210 | { | ||
| 211 | ✗ | const char *str = *pstr; | |
| 212 | char *dst; | ||
| 213 | size_t len, idx; | ||
| 214 | |||
| 215 | ✗ | *pdst = NULL; | |
| 216 | |||
| 217 | ✗ | len = strlen(str); | |
| 218 | ✗ | if (!len) | |
| 219 | ✗ | return 0; | |
| 220 | |||
| 221 | ✗ | dst = av_malloc(len + 1); | |
| 222 | ✗ | if (!dst) | |
| 223 | ✗ | return AVERROR(ENOMEM); | |
| 224 | |||
| 225 | ✗ | for (idx = 0; *str; idx++, str++) { | |
| 226 | ✗ | if (str[0] == '\\' && str[1]) | |
| 227 | ✗ | str++; | |
| 228 | ✗ | else if (*str == delim) | |
| 229 | ✗ | break; | |
| 230 | |||
| 231 | ✗ | dst[idx] = *str; | |
| 232 | } | ||
| 233 | ✗ | if (!idx) { | |
| 234 | ✗ | av_freep(&dst); | |
| 235 | ✗ | return 0; | |
| 236 | } | ||
| 237 | |||
| 238 | ✗ | dst[idx] = 0; | |
| 239 | |||
| 240 | ✗ | *pdst = dst; | |
| 241 | ✗ | *dst_len = idx; | |
| 242 | ✗ | *pstr = str; | |
| 243 | |||
| 244 | ✗ | return 0; | |
| 245 | } | ||
| 246 | |||
| 247 | ✗ | static int enc_stats_init(OutputStream *ost, EncStats *es, int pre, | |
| 248 | const char *path, const char *fmt_spec) | ||
| 249 | { | ||
| 250 | static const struct { | ||
| 251 | enum EncStatsType type; | ||
| 252 | const char *str; | ||
| 253 | unsigned pre_only:1; | ||
| 254 | unsigned post_only:1; | ||
| 255 | unsigned need_input_data:1; | ||
| 256 | } fmt_specs[] = { | ||
| 257 | { ENC_STATS_FILE_IDX, "fidx" }, | ||
| 258 | { ENC_STATS_STREAM_IDX, "sidx" }, | ||
| 259 | { ENC_STATS_FRAME_NUM, "n" }, | ||
| 260 | { ENC_STATS_FRAME_NUM_IN, "ni", 0, 0, 1 }, | ||
| 261 | { ENC_STATS_TIMEBASE, "tb" }, | ||
| 262 | { ENC_STATS_TIMEBASE_IN, "tbi", 0, 0, 1 }, | ||
| 263 | { ENC_STATS_PTS, "pts" }, | ||
| 264 | { ENC_STATS_PTS_TIME, "t" }, | ||
| 265 | { ENC_STATS_PTS_IN, "ptsi", 0, 0, 1 }, | ||
| 266 | { ENC_STATS_PTS_TIME_IN, "ti", 0, 0, 1 }, | ||
| 267 | { ENC_STATS_DTS, "dts", 0, 1 }, | ||
| 268 | { ENC_STATS_DTS_TIME, "dt", 0, 1 }, | ||
| 269 | { ENC_STATS_SAMPLE_NUM, "sn", 1 }, | ||
| 270 | { ENC_STATS_NB_SAMPLES, "samp", 1 }, | ||
| 271 | { ENC_STATS_PKT_SIZE, "size", 0, 1 }, | ||
| 272 | { ENC_STATS_BITRATE, "br", 0, 1 }, | ||
| 273 | { ENC_STATS_AVG_BITRATE, "abr", 0, 1 }, | ||
| 274 | { ENC_STATS_KEYFRAME, "key", 0, 1 }, | ||
| 275 | }; | ||
| 276 | ✗ | const char *next = fmt_spec; | |
| 277 | |||
| 278 | int ret; | ||
| 279 | |||
| 280 | ✗ | while (*next) { | |
| 281 | EncStatsComponent *c; | ||
| 282 | char *val; | ||
| 283 | size_t val_len; | ||
| 284 | |||
| 285 | // get the sequence up until next opening brace | ||
| 286 | ✗ | ret = unescape(&val, &val_len, &next, '{'); | |
| 287 | ✗ | if (ret < 0) | |
| 288 | ✗ | return ret; | |
| 289 | |||
| 290 | ✗ | if (val) { | |
| 291 | ✗ | ret = GROW_ARRAY(es->components, es->nb_components); | |
| 292 | ✗ | if (ret < 0) { | |
| 293 | ✗ | av_freep(&val); | |
| 294 | ✗ | return ret; | |
| 295 | } | ||
| 296 | |||
| 297 | ✗ | c = &es->components[es->nb_components - 1]; | |
| 298 | ✗ | c->type = ENC_STATS_LITERAL; | |
| 299 | ✗ | c->str = val; | |
| 300 | ✗ | c->str_len = val_len; | |
| 301 | } | ||
| 302 | |||
| 303 | ✗ | if (!*next) | |
| 304 | ✗ | break; | |
| 305 | ✗ | next++; | |
| 306 | |||
| 307 | // get the part inside braces | ||
| 308 | ✗ | ret = unescape(&val, &val_len, &next, '}'); | |
| 309 | ✗ | if (ret < 0) | |
| 310 | ✗ | return ret; | |
| 311 | |||
| 312 | ✗ | if (!val) { | |
| 313 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
| 314 | "Empty formatting directive in: %s\n", fmt_spec); | ||
| 315 | ✗ | return AVERROR(EINVAL); | |
| 316 | } | ||
| 317 | |||
| 318 | ✗ | if (!*next) { | |
| 319 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
| 320 | "Missing closing brace in: %s\n", fmt_spec); | ||
| 321 | ✗ | ret = AVERROR(EINVAL); | |
| 322 | ✗ | goto fail; | |
| 323 | } | ||
| 324 | ✗ | next++; | |
| 325 | |||
| 326 | ✗ | ret = GROW_ARRAY(es->components, es->nb_components); | |
| 327 | ✗ | if (ret < 0) | |
| 328 | ✗ | goto fail; | |
| 329 | |||
| 330 | ✗ | c = &es->components[es->nb_components - 1]; | |
| 331 | |||
| 332 | ✗ | for (size_t i = 0; i < FF_ARRAY_ELEMS(fmt_specs); i++) { | |
| 333 | ✗ | if (!strcmp(val, fmt_specs[i].str)) { | |
| 334 | ✗ | if ((pre && fmt_specs[i].post_only) || (!pre && fmt_specs[i].pre_only)) { | |
| 335 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
| 336 | "Format directive '%s' may only be used %s-encoding\n", | ||
| 337 | val, pre ? "post" : "pre"); | ||
| 338 | ✗ | ret = AVERROR(EINVAL); | |
| 339 | ✗ | goto fail; | |
| 340 | } | ||
| 341 | |||
| 342 | ✗ | c->type = fmt_specs[i].type; | |
| 343 | |||
| 344 | ✗ | if (fmt_specs[i].need_input_data && !ost->ist) { | |
| 345 | ✗ | av_log(ost, AV_LOG_WARNING, | |
| 346 | "Format directive '%s' is unavailable, because " | ||
| 347 | "this output stream has no associated input stream\n", | ||
| 348 | val); | ||
| 349 | } | ||
| 350 | |||
| 351 | ✗ | break; | |
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | ✗ | if (!c->type) { | |
| 356 | ✗ | av_log(NULL, AV_LOG_ERROR, "Invalid format directive: %s\n", val); | |
| 357 | ✗ | ret = AVERROR(EINVAL); | |
| 358 | ✗ | goto fail; | |
| 359 | } | ||
| 360 | |||
| 361 | ✗ | fail: | |
| 362 | ✗ | av_freep(&val); | |
| 363 | ✗ | if (ret < 0) | |
| 364 | ✗ | return ret; | |
| 365 | } | ||
| 366 | |||
| 367 | ✗ | ret = pthread_mutex_init(&es->lock, NULL); | |
| 368 | ✗ | if (ret) | |
| 369 | ✗ | return AVERROR(ret); | |
| 370 | ✗ | es->lock_initialized = 1; | |
| 371 | |||
| 372 | ✗ | ret = enc_stats_get_file(&es->io, path); | |
| 373 | ✗ | if (ret < 0) | |
| 374 | ✗ | return ret; | |
| 375 | |||
| 376 | ✗ | return 0; | |
| 377 | } | ||
| 378 | |||
| 379 | 2864 | static const char *output_stream_item_name(void *obj) | |
| 380 | { | ||
| 381 | 2864 | const MuxStream *ms = obj; | |
| 382 | |||
| 383 | 2864 | return ms->log_name; | |
| 384 | } | ||
| 385 | |||
| 386 | static const AVClass output_stream_class = { | ||
| 387 | .class_name = "OutputStream", | ||
| 388 | .version = LIBAVUTIL_VERSION_INT, | ||
| 389 | .item_name = output_stream_item_name, | ||
| 390 | .category = AV_CLASS_CATEGORY_MUXER, | ||
| 391 | }; | ||
| 392 | |||
| 393 | 9924 | static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type) | |
| 394 | { | ||
| 395 | 9924 | const char *type_str = av_get_media_type_string(type); | |
| 396 | MuxStream *ms; | ||
| 397 | |||
| 398 | 9924 | ms = allocate_array_elem(&mux->of.streams, sizeof(*ms), &mux->of.nb_streams); | |
| 399 | 9924 | if (!ms) | |
| 400 | ✗ | return NULL; | |
| 401 | |||
| 402 | 9924 | ms->ost.file = &mux->of; | |
| 403 | 9924 | ms->ost.index = mux->of.nb_streams - 1; | |
| 404 | 9924 | ms->ost.type = type; | |
| 405 | |||
| 406 | 9924 | ms->ost.class = &output_stream_class; | |
| 407 | |||
| 408 | 9924 | ms->sch_idx = -1; | |
| 409 | 9924 | ms->sch_idx_enc = -1; | |
| 410 | |||
| 411 | 9924 | snprintf(ms->log_name, sizeof(ms->log_name), "%cost#%d:%d", | |
| 412 | 9924 | type_str ? *type_str : '?', mux->of.index, ms->ost.index); | |
| 413 | |||
| 414 | 9924 | return ms; | |
| 415 | } | ||
| 416 | |||
| 417 | 8806 | static int ost_get_filters(const OptionsContext *o, AVFormatContext *oc, | |
| 418 | OutputStream *ost, char **dst) | ||
| 419 | { | ||
| 420 | 8806 | const char *filters = NULL; | |
| 421 | 8806 | opt_match_per_stream_str(ost, &o->filters, oc, ost->st, &filters); | |
| 422 | |||
| 423 | 8806 | if (!ost->ist) { | |
| 424 | 1565 | if (filters) { | |
| 425 | ✗ | av_log(ost, AV_LOG_ERROR, | |
| 426 | "Filtergraph '%s' was specified for a stream fed from a complex " | ||
| 427 | "filtergraph. Simple and complex filtering cannot be used " | ||
| 428 | "together for the same stream.\n", filters); | ||
| 429 | ✗ | return AVERROR(EINVAL); | |
| 430 | } | ||
| 431 | 1565 | return 0; | |
| 432 | } | ||
| 433 | |||
| 434 | 7241 | if (filters) | |
| 435 | 4109 | *dst = av_strdup(filters); | |
| 436 | else | ||
| 437 | 3132 | *dst = av_strdup(ost->type == AVMEDIA_TYPE_VIDEO ? "null" : "anull"); | |
| 438 | 7241 | return *dst ? 0 : AVERROR(ENOMEM); | |
| 439 | } | ||
| 440 | |||
| 441 | ✗ | static int parse_matrix_coeffs(void *logctx, uint16_t *dest, const char *str) | |
| 442 | { | ||
| 443 | ✗ | const char *p = str; | |
| 444 | ✗ | for (int i = 0;; i++) { | |
| 445 | ✗ | dest[i] = atoi(p); | |
| 446 | ✗ | if (i == 63) | |
| 447 | ✗ | break; | |
| 448 | ✗ | p = strchr(p, ','); | |
| 449 | ✗ | if (!p) { | |
| 450 | ✗ | av_log(logctx, AV_LOG_FATAL, | |
| 451 | "Syntax error in matrix \"%s\" at coeff %d\n", str, i); | ||
| 452 | ✗ | return AVERROR(EINVAL); | |
| 453 | } | ||
| 454 | ✗ | p++; | |
| 455 | } | ||
| 456 | |||
| 457 | ✗ | return 0; | |
| 458 | } | ||
| 459 | |||
| 460 | 662 | static int pixfmt_in_list(const enum AVPixelFormat *formats, enum AVPixelFormat format) | |
| 461 | { | ||
| 462 | 3730 | for (; *formats != AV_PIX_FMT_NONE; formats++) | |
| 463 | 3730 | if (*formats == format) | |
| 464 | 662 | return 1; | |
| 465 | ✗ | return 0; | |
| 466 | } | ||
| 467 | |||
| 468 | static enum AVPixelFormat | ||
| 469 | ✗ | choose_pixel_fmt(const AVCodecContext *avctx, enum AVPixelFormat target) | |
| 470 | { | ||
| 471 | const enum AVPixelFormat *p; | ||
| 472 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target); | |
| 473 | //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented | ||
| 474 | ✗ | int has_alpha = desc ? desc->nb_components % 2 == 0 : 0; | |
| 475 | ✗ | enum AVPixelFormat best= AV_PIX_FMT_NONE; | |
| 476 | int ret; | ||
| 477 | |||
| 478 | ✗ | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT, | |
| 479 | 0, (const void **) &p, NULL); | ||
| 480 | ✗ | if (ret < 0) | |
| 481 | ✗ | return AV_PIX_FMT_NONE; | |
| 482 | |||
| 483 | ✗ | for (; *p != AV_PIX_FMT_NONE; p++) { | |
| 484 | ✗ | best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL); | |
| 485 | ✗ | if (*p == target) | |
| 486 | ✗ | break; | |
| 487 | } | ||
| 488 | ✗ | if (*p == AV_PIX_FMT_NONE) { | |
| 489 | ✗ | if (target != AV_PIX_FMT_NONE) | |
| 490 | ✗ | av_log(NULL, AV_LOG_WARNING, | |
| 491 | "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n", | ||
| 492 | av_get_pix_fmt_name(target), | ||
| 493 | ✗ | avctx->codec->name, | |
| 494 | av_get_pix_fmt_name(best)); | ||
| 495 | ✗ | return best; | |
| 496 | } | ||
| 497 | ✗ | return target; | |
| 498 | } | ||
| 499 | |||
| 500 | 5476 | static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name) | |
| 501 | { | ||
| 502 | const enum AVPixelFormat *fmts; | ||
| 503 | enum AVPixelFormat fmt; | ||
| 504 | int ret; | ||
| 505 | |||
| 506 | 5476 | fmt = av_get_pix_fmt(name); | |
| 507 | 5476 | if (fmt == AV_PIX_FMT_NONE) { | |
| 508 | ✗ | av_log(ost, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", name); | |
| 509 | ✗ | return AV_PIX_FMT_NONE; | |
| 510 | } | ||
| 511 | |||
| 512 | 5476 | ret = avcodec_get_supported_config(ost->enc->enc_ctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT, | |
| 513 | 0, (const void **) &fmts, NULL); | ||
| 514 | 5476 | if (ret < 0) | |
| 515 | ✗ | return AV_PIX_FMT_NONE; | |
| 516 | |||
| 517 | /* when the user specified-format is an alias for an endianness-specific | ||
| 518 | * one (e.g. rgb48 -> rgb48be/le), it gets translated into the native | ||
| 519 | * endianness by av_get_pix_fmt(); | ||
| 520 | * the following code handles the case when the native endianness is not | ||
| 521 | * supported by the encoder, but the other one is */ | ||
| 522 | 5476 | if (fmts && !pixfmt_in_list(fmts, fmt)) { | |
| 523 | ✗ | const char *name_canonical = av_get_pix_fmt_name(fmt); | |
| 524 | ✗ | int len = strlen(name_canonical); | |
| 525 | |||
| 526 | ✗ | if (strcmp(name, name_canonical) && | |
| 527 | ✗ | (!strcmp(name_canonical + len - 2, "le") || | |
| 528 | ✗ | !strcmp(name_canonical + len - 2, "be"))) { | |
| 529 | char name_other[64]; | ||
| 530 | enum AVPixelFormat fmt_other; | ||
| 531 | |||
| 532 | ✗ | snprintf(name_other, sizeof(name_other), "%s%ce", | |
| 533 | ✗ | name, name_canonical[len - 2] == 'l' ? 'b' : 'l'); | |
| 534 | ✗ | fmt_other = av_get_pix_fmt(name_other); | |
| 535 | ✗ | if (fmt_other != AV_PIX_FMT_NONE && pixfmt_in_list(fmts, fmt_other)) { | |
| 536 | ✗ | av_log(ost, AV_LOG_VERBOSE, "Mapping pixel format %s->%s\n", | |
| 537 | name, name_other); | ||
| 538 | ✗ | fmt = fmt_other; | |
| 539 | } | ||
| 540 | } | ||
| 541 | } | ||
| 542 | |||
| 543 | 5476 | if (fmts && !pixfmt_in_list(fmts, fmt)) | |
| 544 | ✗ | fmt = choose_pixel_fmt(ost->enc->enc_ctx, fmt); | |
| 545 | |||
| 546 | 5476 | return fmt; | |
| 547 | } | ||
| 548 | |||
| 549 | 5 | static int parse_stereo3d_type(void *logctx, const char *arg, int *type) | |
| 550 | { | ||
| 551 | static const struct { | ||
| 552 | const char *name; | ||
| 553 | int type; | ||
| 554 | } aliases[] = { | ||
| 555 | { "2d", AV_STEREO3D_2D }, | ||
| 556 | { "mono", AV_STEREO3D_2D }, | ||
| 557 | { "sbs", AV_STEREO3D_SIDEBYSIDE }, | ||
| 558 | { "sbsl", AV_STEREO3D_SIDEBYSIDE }, | ||
| 559 | { "tb", AV_STEREO3D_TOPBOTTOM }, | ||
| 560 | { "tbl", AV_STEREO3D_TOPBOTTOM }, | ||
| 561 | }; | ||
| 562 | static const enum AVStereo3DType v1_types[] = { | ||
| 563 | AV_STEREO3D_2D, | ||
| 564 | AV_STEREO3D_SIDEBYSIDE, | ||
| 565 | AV_STEREO3D_TOPBOTTOM, | ||
| 566 | }; | ||
| 567 | |||
| 568 | 21 | for (int i = 0; i < FF_ARRAY_ELEMS(aliases); i++) { | |
| 569 | 21 | if (!av_strcasecmp(arg, aliases[i].name)) { | |
| 570 | 5 | *type = aliases[i].type; | |
| 571 | 5 | return 0; | |
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | ✗ | for (int i = 0; i < FF_ARRAY_ELEMS(v1_types); i++) { | |
| 576 | ✗ | const char *name = av_stereo3d_type_name(v1_types[i]); | |
| 577 | ✗ | if (!av_strcasecmp(arg, name)) { | |
| 578 | ✗ | *type = v1_types[i]; | |
| 579 | ✗ | return 0; | |
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | ✗ | av_log(logctx, AV_LOG_ERROR, | |
| 584 | "Invalid stereoscopic 3D layout '%s'. " | ||
| 585 | "Valid values are: 2d, mono, sbs, sbsl, side by side, " | ||
| 586 | "tb, tbl, top and bottom.\n", arg); | ||
| 587 | ✗ | return AVERROR(EINVAL); | |
| 588 | } | ||
| 589 | |||
| 590 | 9222 | static int check_stereo3d_leftovers(Muxer *mux, const OptionsContext *o) | |
| 591 | { | ||
| 592 | 9222 | AVFormatContext *oc = mux->fc; | |
| 593 | |||
| 594 | 9227 | for (int i = 0; i < o->stereo3ds.nb_opt; i++) { | |
| 595 | 5 | const SpecifierOpt *so = &o->stereo3ds.opt[i]; | |
| 596 | 5 | int matched_video = 0, matched_other = 0; | |
| 597 | |||
| 598 | /* Empty specifier applies to video only; ignore other stream types. */ | ||
| 599 | 5 | if (!so->specifier[0]) | |
| 600 | ✗ | continue; | |
| 601 | |||
| 602 | 11 | for (unsigned j = 0; j < oc->nb_streams; j++) { | |
| 603 | 6 | AVStream *st = oc->streams[j]; | |
| 604 | 6 | if (!stream_specifier_match(&so->stream_spec, oc, st, mux)) | |
| 605 | 1 | continue; | |
| 606 | 5 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) | |
| 607 | 5 | matched_video++; | |
| 608 | else | ||
| 609 | ✗ | matched_other++; | |
| 610 | } | ||
| 611 | |||
| 612 | 5 | if (matched_other) { | |
| 613 | ✗ | av_log(mux, AV_LOG_ERROR, | |
| 614 | "-stereo3d is only valid for video streams (specifier '%s').\n", | ||
| 615 | ✗ | so->specifier); | |
| 616 | ✗ | return AVERROR(EINVAL); | |
| 617 | } | ||
| 618 | 5 | if (!matched_video) { | |
| 619 | ✗ | av_log(mux, AV_LOG_ERROR, | |
| 620 | "Stream specifier '%s' for -stereo3d matches no video streams.\n", | ||
| 621 | ✗ | so->specifier); | |
| 622 | ✗ | return AVERROR(EINVAL); | |
| 623 | } | ||
| 624 | } | ||
| 625 | |||
| 626 | 9222 | return 0; | |
| 627 | } | ||
| 628 | |||
| 629 | 7626 | static int new_stream_video(Muxer *mux, const OptionsContext *o, | |
| 630 | OutputStream *ost, int *keep_pix_fmt, | ||
| 631 | enum VideoSyncMethod *vsync_method) | ||
| 632 | { | ||
| 633 | 7626 | MuxStream *ms = ms_from_ost(ost); | |
| 634 | 7626 | AVFormatContext *oc = mux->fc; | |
| 635 | AVStream *st; | ||
| 636 | 7626 | const char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL; | |
| 637 | 7626 | const char *stereo3d = NULL; | |
| 638 | 7626 | int ret = 0; | |
| 639 | |||
| 640 | 7626 | st = ost->st; | |
| 641 | |||
| 642 | 7626 | opt_match_per_stream_str(ost, &o->frame_rates, oc, st, &frame_rate); | |
| 643 | 7626 | if (frame_rate && av_parse_video_rate(&ms->frame_rate, frame_rate) < 0) { | |
| 644 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate); | |
| 645 | ✗ | return AVERROR(EINVAL); | |
| 646 | } | ||
| 647 | |||
| 648 | 7626 | opt_match_per_stream_str(ost, &o->max_frame_rates, oc, st, &max_frame_rate); | |
| 649 | 7626 | if (max_frame_rate && av_parse_video_rate(&ms->max_frame_rate, max_frame_rate) < 0) { | |
| 650 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid maximum framerate value: %s\n", max_frame_rate); | |
| 651 | ✗ | return AVERROR(EINVAL); | |
| 652 | } | ||
| 653 | |||
| 654 | 7626 | if (frame_rate && max_frame_rate) { | |
| 655 | ✗ | av_log(ost, AV_LOG_ERROR, "Only one of -fpsmax and -r can be set for a stream.\n"); | |
| 656 | ✗ | return AVERROR(EINVAL); | |
| 657 | } | ||
| 658 | |||
| 659 | 7626 | opt_match_per_stream_str(ost, &o->frame_aspect_ratios, oc, st, &frame_aspect_ratio); | |
| 660 | 7626 | if (frame_aspect_ratio) { | |
| 661 | AVRational q; | ||
| 662 | ✗ | if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 || | |
| 663 | ✗ | q.num <= 0 || q.den <= 0) { | |
| 664 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio); | |
| 665 | ✗ | return AVERROR(EINVAL); | |
| 666 | } | ||
| 667 | ✗ | ost->frame_aspect_ratio = q; | |
| 668 | } | ||
| 669 | |||
| 670 | 7626 | opt_match_per_stream_str(ost, &o->stereo3ds, oc, st, &stereo3d); | |
| 671 | 7626 | if (stereo3d) { | |
| 672 | 5 | ret = parse_stereo3d_type(ost, stereo3d, &ms->stereo3d_type); | |
| 673 | 5 | if (ret < 0) | |
| 674 | ✗ | return ret; | |
| 675 | 5 | ms->stereo3d_set = 1; | |
| 676 | } | ||
| 677 | |||
| 678 | 7626 | if (ost->enc) { | |
| 679 | 7109 | AVCodecContext *video_enc = ost->enc->enc_ctx; | |
| 680 | 7109 | const char *p = NULL, *fps_mode = NULL; | |
| 681 | 7109 | const char *frame_size = NULL; | |
| 682 | 7109 | const char *frame_pix_fmt = NULL; | |
| 683 | 7109 | const char *intra_matrix = NULL, *inter_matrix = NULL; | |
| 684 | 7109 | const char *chroma_intra_matrix = NULL; | |
| 685 | int i; | ||
| 686 | |||
| 687 | 7109 | opt_match_per_stream_str(ost, &o->frame_sizes, oc, st, &frame_size); | |
| 688 | 7109 | if (frame_size) { | |
| 689 | 1350 | ret = av_parse_video_size(&video_enc->width, &video_enc->height, frame_size); | |
| 690 | 1350 | if (ret < 0) { | |
| 691 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size); | |
| 692 | ✗ | return AVERROR(EINVAL); | |
| 693 | } | ||
| 694 | } | ||
| 695 | |||
| 696 | 7109 | opt_match_per_stream_str(ost, &o->frame_pix_fmts, oc, st, &frame_pix_fmt); | |
| 697 | 7109 | if (frame_pix_fmt && *frame_pix_fmt == '+') { | |
| 698 | ✗ | *keep_pix_fmt = 1; | |
| 699 | ✗ | if (!*++frame_pix_fmt) | |
| 700 | ✗ | frame_pix_fmt = NULL; | |
| 701 | } | ||
| 702 | 7109 | if (frame_pix_fmt) { | |
| 703 | 5476 | video_enc->pix_fmt = pix_fmt_parse(ost, frame_pix_fmt); | |
| 704 | 5476 | if (video_enc->pix_fmt == AV_PIX_FMT_NONE) | |
| 705 | ✗ | return AVERROR(EINVAL); | |
| 706 | } | ||
| 707 | |||
| 708 | 7109 | opt_match_per_stream_str(ost, &o->intra_matrices, oc, st, &intra_matrix); | |
| 709 | 7109 | if (intra_matrix) { | |
| 710 | ✗ | if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) | |
| 711 | ✗ | return AVERROR(ENOMEM); | |
| 712 | |||
| 713 | ✗ | ret = parse_matrix_coeffs(ost, video_enc->intra_matrix, intra_matrix); | |
| 714 | ✗ | if (ret < 0) | |
| 715 | ✗ | return ret; | |
| 716 | } | ||
| 717 | 7109 | opt_match_per_stream_str(ost, &o->chroma_intra_matrices, oc, st, &chroma_intra_matrix); | |
| 718 | 7109 | if (chroma_intra_matrix) { | |
| 719 | ✗ | if (!(video_enc->chroma_intra_matrix = av_mallocz(sizeof(*video_enc->chroma_intra_matrix) * 64))) | |
| 720 | ✗ | return AVERROR(ENOMEM); | |
| 721 | ✗ | ret = parse_matrix_coeffs(ost, video_enc->chroma_intra_matrix, chroma_intra_matrix); | |
| 722 | ✗ | if (ret < 0) | |
| 723 | ✗ | return ret; | |
| 724 | } | ||
| 725 | 7109 | opt_match_per_stream_str(ost, &o->inter_matrices, oc, st, &inter_matrix); | |
| 726 | 7109 | if (inter_matrix) { | |
| 727 | ✗ | if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) | |
| 728 | ✗ | return AVERROR(ENOMEM); | |
| 729 | ✗ | ret = parse_matrix_coeffs(ost, video_enc->inter_matrix, inter_matrix); | |
| 730 | ✗ | if (ret < 0) | |
| 731 | ✗ | return ret; | |
| 732 | } | ||
| 733 | |||
| 734 | 7109 | opt_match_per_stream_str(ost, &o->rc_overrides, oc, st, &p); | |
| 735 | 7109 | for (i = 0; p; i++) { | |
| 736 | int start, end, q; | ||
| 737 | ✗ | int e = sscanf(p, "%d,%d,%d", &start, &end, &q); | |
| 738 | ✗ | if (e != 3) { | |
| 739 | ✗ | av_log(ost, AV_LOG_FATAL, "error parsing rc_override\n"); | |
| 740 | ✗ | return AVERROR(EINVAL); | |
| 741 | } | ||
| 742 | ✗ | video_enc->rc_override = | |
| 743 | ✗ | av_realloc_array(video_enc->rc_override, | |
| 744 | ✗ | i + 1, sizeof(RcOverride)); | |
| 745 | ✗ | if (!video_enc->rc_override) { | |
| 746 | ✗ | av_log(ost, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n"); | |
| 747 | ✗ | return AVERROR(ENOMEM); | |
| 748 | } | ||
| 749 | ✗ | video_enc->rc_override[i].start_frame = start; | |
| 750 | ✗ | video_enc->rc_override[i].end_frame = end; | |
| 751 | ✗ | if (q > 0) { | |
| 752 | ✗ | video_enc->rc_override[i].qscale = q; | |
| 753 | ✗ | video_enc->rc_override[i].quality_factor = 1.0; | |
| 754 | } | ||
| 755 | else { | ||
| 756 | ✗ | video_enc->rc_override[i].qscale = 0; | |
| 757 | ✗ | video_enc->rc_override[i].quality_factor = -q/100.0; | |
| 758 | } | ||
| 759 | ✗ | p = strchr(p, '/'); | |
| 760 | ✗ | if (p) p++; | |
| 761 | } | ||
| 762 | 7109 | video_enc->rc_override_count = i; | |
| 763 | |||
| 764 | 7109 | opt_match_per_stream_int(ost, &o->force_fps, oc, st, &ms->force_fps); | |
| 765 | |||
| 766 | 7109 | *vsync_method = VSYNC_AUTO; | |
| 767 | 7109 | opt_match_per_stream_str(ost, &o->fps_mode, oc, st, &fps_mode); | |
| 768 | 7109 | if (fps_mode) { | |
| 769 | 544 | ret = parse_and_set_vsync(fps_mode, vsync_method, ost->file->index, ost->index); | |
| 770 | 544 | if (ret < 0) | |
| 771 | ✗ | return ret; | |
| 772 | } | ||
| 773 | |||
| 774 | 7109 | if ((ms->frame_rate.num || ms->max_frame_rate.num) && | |
| 775 | 21 | !(*vsync_method == VSYNC_AUTO || | |
| 776 | 1 | *vsync_method == VSYNC_CFR || *vsync_method == VSYNC_VSCFR)) { | |
| 777 | ✗ | av_log(ost, AV_LOG_FATAL, "One of -r/-fpsmax was specified " | |
| 778 | "together a non-CFR -vsync/-fps_mode. This is contradictory.\n"); | ||
| 779 | ✗ | return AVERROR(EINVAL); | |
| 780 | } | ||
| 781 | |||
| 782 | 7109 | if (*vsync_method == VSYNC_AUTO) { | |
| 783 | 6565 | if (ms->frame_rate.num || ms->max_frame_rate.num) { | |
| 784 | 20 | *vsync_method = VSYNC_CFR; | |
| 785 | 6545 | } else if (!strcmp(oc->oformat->name, "avi")) { | |
| 786 | 305 | *vsync_method = VSYNC_VFR; | |
| 787 | } else { | ||
| 788 | 6240 | *vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ? | |
| 789 | 4738 | ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ? | |
| 790 | 6240 | VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR; | |
| 791 | } | ||
| 792 | |||
| 793 | 6565 | if (ost->ist && *vsync_method == VSYNC_CFR) { | |
| 794 | 413 | const InputFile *ifile = ost->ist->file; | |
| 795 | |||
| 796 | 413 | if (ifile->nb_streams == 1 && ifile->input_ts_offset == 0) | |
| 797 | 368 | *vsync_method = VSYNC_VSCFR; | |
| 798 | } | ||
| 799 | |||
| 800 | 6565 | if (*vsync_method == VSYNC_CFR && copy_ts) { | |
| 801 | ✗ | *vsync_method = VSYNC_VSCFR; | |
| 802 | } | ||
| 803 | } | ||
| 804 | } | ||
| 805 | |||
| 806 | 7626 | return 0; | |
| 807 | } | ||
| 808 | |||
| 809 | 2187 | static int new_stream_audio(Muxer *mux, const OptionsContext *o, | |
| 810 | OutputStream *ost) | ||
| 811 | { | ||
| 812 | 2187 | MuxStream *ms = ms_from_ost(ost); | |
| 813 | 2187 | AVFormatContext *oc = mux->fc; | |
| 814 | 2187 | AVStream *st = ost->st; | |
| 815 | |||
| 816 | 2187 | if (ost->enc) { | |
| 817 | 1697 | AVCodecContext *audio_enc = ost->enc->enc_ctx; | |
| 818 | 1697 | int channels = 0; | |
| 819 | 1697 | const char *layout = NULL; | |
| 820 | 1697 | const char *sample_fmt = NULL; | |
| 821 | |||
| 822 | 1697 | opt_match_per_stream_int(ost, &o->audio_channels, oc, st, &channels); | |
| 823 | 1697 | if (channels) { | |
| 824 | 10 | audio_enc->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | |
| 825 | 10 | audio_enc->ch_layout.nb_channels = channels; | |
| 826 | } | ||
| 827 | |||
| 828 | 1697 | opt_match_per_stream_str(ost, &o->audio_ch_layouts, oc, st, &layout); | |
| 829 | 1697 | if (layout && av_channel_layout_from_string(&audio_enc->ch_layout, layout) < 0) { | |
| 830 | ✗ | av_log(ost, AV_LOG_FATAL, "Unknown channel layout: %s\n", layout); | |
| 831 | ✗ | return AVERROR(EINVAL); | |
| 832 | } | ||
| 833 | |||
| 834 | 1697 | opt_match_per_stream_str(ost, &o->sample_fmts, oc, st, &sample_fmt); | |
| 835 | 1697 | if (sample_fmt && | |
| 836 | 9 | (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) { | |
| 837 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt); | |
| 838 | ✗ | return AVERROR(EINVAL); | |
| 839 | } | ||
| 840 | |||
| 841 | 1697 | opt_match_per_stream_int(ost, &o->audio_sample_rate, oc, st, &audio_enc->sample_rate); | |
| 842 | 1697 | opt_match_per_stream_str(ost, &o->apad, oc, st, &ms->apad); | |
| 843 | } | ||
| 844 | |||
| 845 | 2187 | return 0; | |
| 846 | } | ||
| 847 | |||
| 848 | 98 | static int new_stream_subtitle(Muxer *mux, const OptionsContext *o, | |
| 849 | OutputStream *ost) | ||
| 850 | { | ||
| 851 | AVStream *st; | ||
| 852 | |||
| 853 | 98 | st = ost->st; | |
| 854 | |||
| 855 | 98 | if (ost->enc) { | |
| 856 | 45 | AVCodecContext *subtitle_enc = ost->enc->enc_ctx; | |
| 857 | |||
| 858 | AVCodecDescriptor const *input_descriptor = | ||
| 859 | 45 | avcodec_descriptor_get(ost->ist->par->codec_id); | |
| 860 | AVCodecDescriptor const *output_descriptor = | ||
| 861 | 45 | avcodec_descriptor_get(subtitle_enc->codec_id); | |
| 862 | 45 | int input_props = 0, output_props = 0; | |
| 863 | |||
| 864 | 45 | const char *frame_size = NULL; | |
| 865 | |||
| 866 | 45 | opt_match_per_stream_str(ost, &o->frame_sizes, mux->fc, st, &frame_size); | |
| 867 | 45 | if (frame_size) { | |
| 868 | ✗ | int ret = av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size); | |
| 869 | ✗ | if (ret < 0) { | |
| 870 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size); | |
| 871 | ✗ | return ret; | |
| 872 | } | ||
| 873 | } | ||
| 874 | 45 | if (input_descriptor) | |
| 875 | 45 | input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB); | |
| 876 | 45 | if (output_descriptor) | |
| 877 | 45 | output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB); | |
| 878 | 45 | if (input_props && output_props && input_props != output_props) { | |
| 879 | ✗ | av_log(ost, AV_LOG_ERROR, | |
| 880 | "Subtitle encoding currently only possible from text to text " | ||
| 881 | "or bitmap to bitmap\n"); | ||
| 882 | ✗ | return AVERROR(EINVAL); | |
| 883 | } | ||
| 884 | } | ||
| 885 | |||
| 886 | 98 | return 0; | |
| 887 | } | ||
| 888 | |||
| 889 | static int | ||
| 890 | 8806 | ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter, | |
| 891 | const OptionsContext *o, | ||
| 892 | AVRational enc_tb, enum VideoSyncMethod vsync_method, | ||
| 893 | int keep_pix_fmt, int autoscale, int threads_manual, | ||
| 894 | const ViewSpecifier *vs, | ||
| 895 | SchedulerNode *src) | ||
| 896 | { | ||
| 897 | 8806 | OutputStream *ost = &ms->ost; | |
| 898 | 8806 | AVCodecContext *enc_ctx = ost->enc->enc_ctx; | |
| 899 | char name[16]; | ||
| 900 | 8806 | char *filters = NULL; | |
| 901 | int ret; | ||
| 902 | |||
| 903 | 35224 | OutputFilterOptions opts = { | |
| 904 | 8806 | .enc = enc_ctx->codec, | |
| 905 | .name = name, | ||
| 906 | 8806 | .format = (ost->type == AVMEDIA_TYPE_VIDEO) ? | |
| 907 | 8806 | enc_ctx->pix_fmt : enc_ctx->sample_fmt, | |
| 908 | 8806 | .width = enc_ctx->width, | |
| 909 | 8806 | .height = enc_ctx->height, | |
| 910 | 8806 | .color_space = enc_ctx->colorspace, | |
| 911 | 8806 | .color_range = enc_ctx->color_range, | |
| 912 | 8806 | .alpha_mode = enc_ctx->alpha_mode, | |
| 913 | 8806 | .chroma_location = enc_ctx->chroma_sample_location, | |
| 914 | .vsync_method = vsync_method, | ||
| 915 | .frame_rate = ms->frame_rate, | ||
| 916 | .max_frame_rate = ms->max_frame_rate, | ||
| 917 | 8806 | .sample_rate = enc_ctx->sample_rate, | |
| 918 | .ch_layout = enc_ctx->ch_layout, | ||
| 919 | 8806 | .sws_opts = o->g->sws_dict, | |
| 920 | 8806 | .swr_opts = o->g->swr_opts, | |
| 921 | .output_tb = enc_tb, | ||
| 922 | 8806 | .trim_start_us = mux->of.start_time, | |
| 923 | 8806 | .trim_duration_us = mux->of.recording_time, | |
| 924 | 8806 | .ts_offset = mux->of.start_time == AV_NOPTS_VALUE ? | |
| 925 | 8806 | 0 : mux->of.start_time, | |
| 926 | .vs = vs, | ||
| 927 | .nb_threads = -1, | ||
| 928 | 8806 | .reinit_opts = ost->enc->reinit_opts, | |
| 929 | |||
| 930 | 17612 | .flags = OFILTER_FLAG_DISABLE_CONVERT * !!keep_pix_fmt | | |
| 931 | 17612 | OFILTER_FLAG_AUTOSCALE * !!autoscale | | |
| 932 | 8806 | OFILTER_FLAG_AUDIO_24BIT * !!(av_get_exact_bits_per_sample(enc_ctx->codec_id) == 24), | |
| 933 | }; | ||
| 934 | |||
| 935 | 8806 | snprintf(name, sizeof(name), "#%d:%d", mux->of.index, ost->index); | |
| 936 | |||
| 937 | 8806 | if (ost->type == AVMEDIA_TYPE_VIDEO) { | |
| 938 | 7109 | if (!keep_pix_fmt) { | |
| 939 | 7109 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 940 | AV_CODEC_CONFIG_PIX_FORMAT, 0, | ||
| 941 | (const void **) &opts.pix_fmts, NULL); | ||
| 942 | 7109 | if (ret < 0) | |
| 943 | ✗ | return ret; | |
| 944 | } | ||
| 945 | 7109 | if (!ms->force_fps) { | |
| 946 | 7108 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 947 | AV_CODEC_CONFIG_FRAME_RATE, 0, | ||
| 948 | (const void **) &opts.frame_rates, NULL); | ||
| 949 | 7108 | if (ret < 0) | |
| 950 | ✗ | return ret; | |
| 951 | } | ||
| 952 | 7109 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 953 | AV_CODEC_CONFIG_COLOR_SPACE, 0, | ||
| 954 | (const void **) &opts.color_spaces, NULL); | ||
| 955 | 7109 | if (ret < 0) | |
| 956 | ✗ | return ret; | |
| 957 | 7109 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 958 | AV_CODEC_CONFIG_COLOR_RANGE, 0, | ||
| 959 | (const void **) &opts.color_ranges, NULL); | ||
| 960 | 7109 | if (ret < 0) | |
| 961 | ✗ | return ret; | |
| 962 | 7109 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 963 | AV_CODEC_CONFIG_ALPHA_MODE, 0, | ||
| 964 | (const void **) &opts.alpha_modes, NULL); | ||
| 965 | 7109 | if (ret < 0) | |
| 966 | ✗ | return ret; | |
| 967 | 7109 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 968 | AV_CODEC_CONFIG_CHROMA_LOCATION, 0, | ||
| 969 | (const void **) &opts.chroma_locations, NULL); | ||
| 970 | 7109 | if (ret < 0) | |
| 971 | ✗ | return ret; | |
| 972 | } else { | ||
| 973 | 1697 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 974 | AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, | ||
| 975 | (const void **) &opts.sample_fmts, NULL); | ||
| 976 | 1697 | if (ret < 0) | |
| 977 | ✗ | return ret; | |
| 978 | 1697 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 979 | AV_CODEC_CONFIG_SAMPLE_RATE, 0, | ||
| 980 | (const void **) &opts.sample_rates, NULL); | ||
| 981 | 1697 | if (ret < 0) | |
| 982 | ✗ | return ret; | |
| 983 | 1697 | ret = avcodec_get_supported_config(enc_ctx, NULL, | |
| 984 | AV_CODEC_CONFIG_CHANNEL_LAYOUT, 0, | ||
| 985 | (const void **) &opts.ch_layouts, NULL); | ||
| 986 | 1697 | if (ret < 0) | |
| 987 | ✗ | return ret; | |
| 988 | } | ||
| 989 | |||
| 990 | 8806 | if (threads_manual) { | |
| 991 | 5626 | ret = av_opt_get_int(enc_ctx, "threads", 0, &opts.nb_threads); | |
| 992 | 5626 | if (ret < 0) | |
| 993 | ✗ | return ret; | |
| 994 | } | ||
| 995 | |||
| 996 | 8806 | ret = ost_get_filters(o, mux->fc, ost, &filters); | |
| 997 | 8806 | if (ret < 0) | |
| 998 | ✗ | return ret; | |
| 999 | |||
| 1000 | 8806 | if (ofilter) { | |
| 1001 | 1565 | av_assert0(!filters); | |
| 1002 | 1565 | ost->filter = ofilter; | |
| 1003 | 1565 | ret = ofilter_bind_enc(ofilter, ms->sch_idx_enc, &opts); | |
| 1004 | } else { | ||
| 1005 | 7241 | ret = fg_create_simple(&ost->fg_simple, ost->ist, &filters, | |
| 1006 | 7241 | mux->sch, ms->sch_idx_enc, &opts); | |
| 1007 | 7241 | if (ret >= 0) | |
| 1008 | 7240 | ost->filter = ost->fg_simple->outputs[0]; | |
| 1009 | |||
| 1010 | } | ||
| 1011 | 8806 | if (ret < 0) | |
| 1012 | 1 | return ret; | |
| 1013 | |||
| 1014 | 8805 | *src = SCH_ENC(ms->sch_idx_enc); | |
| 1015 | |||
| 1016 | 8805 | return 0; | |
| 1017 | } | ||
| 1018 | |||
| 1019 | 1072 | static int streamcopy_init(const OptionsContext *o, const Muxer *mux, | |
| 1020 | OutputStream *ost, AVDictionary **encoder_opts) | ||
| 1021 | { | ||
| 1022 | 1072 | MuxStream *ms = ms_from_ost(ost); | |
| 1023 | |||
| 1024 | 1072 | const InputStream *ist = ost->ist; | |
| 1025 | 1072 | const InputFile *ifile = ist->file; | |
| 1026 | |||
| 1027 | 1072 | AVCodecParameters *par = ms->par_in; | |
| 1028 | 1072 | uint32_t codec_tag = par->codec_tag; | |
| 1029 | |||
| 1030 | 1072 | AVCodecContext *codec_ctx = NULL; | |
| 1031 | |||
| 1032 | 1072 | AVRational fr = ms->frame_rate; | |
| 1033 | |||
| 1034 | 1072 | int ret = 0; | |
| 1035 | |||
| 1036 | 1072 | const char *filters = NULL; | |
| 1037 | 1072 | opt_match_per_stream_str(ost, &o->filters, mux->fc, ost->st, &filters); | |
| 1038 | |||
| 1039 | 1072 | if (filters) { | |
| 1040 | ✗ | av_log(ost, AV_LOG_ERROR, | |
| 1041 | "Filtergraph '%s' was specified, but codec copy was selected. " | ||
| 1042 | "Filtering and streamcopy cannot be used together.\n", filters); | ||
| 1043 | ✗ | return AVERROR(EINVAL); | |
| 1044 | } | ||
| 1045 | |||
| 1046 | 1072 | codec_ctx = avcodec_alloc_context3(NULL); | |
| 1047 | 1072 | if (!codec_ctx) | |
| 1048 | ✗ | return AVERROR(ENOMEM); | |
| 1049 | |||
| 1050 | 1072 | ret = avcodec_parameters_to_context(codec_ctx, ist->par); | |
| 1051 | 1072 | if (ret >= 0) | |
| 1052 | 1072 | ret = av_opt_set_dict(codec_ctx, encoder_opts); | |
| 1053 | 1072 | if (ret < 0) { | |
| 1054 | ✗ | av_log(ost, AV_LOG_FATAL, | |
| 1055 | "Error setting up codec context options.\n"); | ||
| 1056 | ✗ | goto fail; | |
| 1057 | } | ||
| 1058 | |||
| 1059 | 1072 | ret = avcodec_parameters_from_context(par, codec_ctx); | |
| 1060 | 1072 | if (ret < 0) { | |
| 1061 | ✗ | av_log(ost, AV_LOG_FATAL, | |
| 1062 | "Error getting reference codec parameters.\n"); | ||
| 1063 | ✗ | goto fail; | |
| 1064 | } | ||
| 1065 | |||
| 1066 | 1072 | if (!codec_tag) { | |
| 1067 | 1065 | const struct AVCodecTag * const *ct = mux->fc->oformat->codec_tag; | |
| 1068 | unsigned int codec_tag_tmp; | ||
| 1069 | 1207 | if (!ct || av_codec_get_id (ct, par->codec_tag) == par->codec_id || | |
| 1070 | 142 | !av_codec_get_tag2(ct, par->codec_id, &codec_tag_tmp)) | |
| 1071 | 939 | codec_tag = par->codec_tag; | |
| 1072 | } | ||
| 1073 | |||
| 1074 | 1072 | par->codec_tag = codec_tag; | |
| 1075 | |||
| 1076 | 1072 | if (!fr.num) | |
| 1077 | 1071 | fr = ist->framerate; | |
| 1078 | |||
| 1079 | 1072 | if (fr.num) | |
| 1080 | 2 | ost->st->avg_frame_rate = fr; | |
| 1081 | else | ||
| 1082 | 1070 | ost->st->avg_frame_rate = ist->st->avg_frame_rate; | |
| 1083 | |||
| 1084 | // copy timebase while removing common factors | ||
| 1085 | 1072 | if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) { | |
| 1086 | 1072 | if (fr.num) | |
| 1087 | 2 | ost->st->time_base = av_inv_q(fr); | |
| 1088 | else | ||
| 1089 | 1070 | ost->st->time_base = av_add_q(ist->st->time_base, (AVRational){0, 1}); | |
| 1090 | } | ||
| 1091 | |||
| 1092 | 1072 | if (!ms->copy_prior_start) { | |
| 1093 | ✗ | ms->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ? | |
| 1094 | ✗ | 0 : mux->of.start_time; | |
| 1095 | ✗ | if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) { | |
| 1096 | ✗ | ms->ts_copy_start = FFMAX(ms->ts_copy_start, | |
| 1097 | ifile->start_time + ifile->ts_offset); | ||
| 1098 | } | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | 1206 | for (int i = 0; i < ist->st->codecpar->nb_coded_side_data; i++) { | |
| 1102 | 134 | const AVPacketSideData *sd_src = &ist->st->codecpar->coded_side_data[i]; | |
| 1103 | AVPacketSideData *sd_dst; | ||
| 1104 | |||
| 1105 | 134 | sd_dst = av_packet_side_data_new(&ost->st->codecpar->coded_side_data, | |
| 1106 | 134 | &ost->st->codecpar->nb_coded_side_data, | |
| 1107 | 134 | sd_src->type, sd_src->size, 0); | |
| 1108 | 134 | if (!sd_dst) { | |
| 1109 | ✗ | ret = AVERROR(ENOMEM); | |
| 1110 | ✗ | goto fail; | |
| 1111 | } | ||
| 1112 | 134 | memcpy(sd_dst->data, sd_src->data, sd_src->size); | |
| 1113 | } | ||
| 1114 | |||
| 1115 | 1072 | switch (par->codec_type) { | |
| 1116 | 490 | case AVMEDIA_TYPE_AUDIO: | |
| 1117 | 490 | if ((par->block_align == 1 || par->block_align == 1152 || par->block_align == 576) && | |
| 1118 | 5 | par->codec_id == AV_CODEC_ID_MP3) | |
| 1119 | ✗ | par->block_align = 0; | |
| 1120 | 490 | if (par->codec_id == AV_CODEC_ID_AC3) | |
| 1121 | 15 | par->block_align = 0; | |
| 1122 | 490 | break; | |
| 1123 | 517 | case AVMEDIA_TYPE_VIDEO: { | |
| 1124 | AVRational sar; | ||
| 1125 | 517 | if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option | |
| 1126 | sar = | ||
| 1127 | ✗ | av_mul_q(ost->frame_aspect_ratio, | |
| 1128 | ✗ | (AVRational){ par->height, par->width }); | |
| 1129 | ✗ | av_log(ost, AV_LOG_WARNING, "Overriding aspect ratio " | |
| 1130 | "with stream copy may produce invalid files\n"); | ||
| 1131 | } | ||
| 1132 | 517 | else if (ist->st->sample_aspect_ratio.num) | |
| 1133 | 139 | sar = ist->st->sample_aspect_ratio; | |
| 1134 | else | ||
| 1135 | 378 | sar = par->sample_aspect_ratio; | |
| 1136 | 517 | ost->st->sample_aspect_ratio = par->sample_aspect_ratio = sar; | |
| 1137 | 517 | ost->st->r_frame_rate = ist->st->r_frame_rate; | |
| 1138 | 517 | break; | |
| 1139 | } | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | 1072 | fail: | |
| 1143 | 1072 | avcodec_free_context(&codec_ctx); | |
| 1144 | 1072 | return ret; | |
| 1145 | } | ||
| 1146 | |||
| 1147 | 8851 | static int set_encoder_id(OutputStream *ost, const AVCodec *codec) | |
| 1148 | { | ||
| 1149 | 8851 | const char *cname = codec->name; | |
| 1150 | uint8_t *encoder_string; | ||
| 1151 | int encoder_string_len; | ||
| 1152 | |||
| 1153 | 8851 | encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2; | |
| 1154 | 8851 | encoder_string = av_mallocz(encoder_string_len); | |
| 1155 | 8851 | if (!encoder_string) | |
| 1156 | ✗ | return AVERROR(ENOMEM); | |
| 1157 | |||
| 1158 | 8851 | if (!ost->file->bitexact && !ost->bitexact) | |
| 1159 | 947 | av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len); | |
| 1160 | else | ||
| 1161 | 7904 | av_strlcpy(encoder_string, "Lavc ", encoder_string_len); | |
| 1162 | 8851 | av_strlcat(encoder_string, cname, encoder_string_len); | |
| 1163 | 8851 | av_dict_set(&ost->st->metadata, "encoder", encoder_string, | |
| 1164 | AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE); | ||
| 1165 | |||
| 1166 | 8851 | return 0; | |
| 1167 | } | ||
| 1168 | |||
| 1169 | 9924 | static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, | |
| 1170 | InputStream *ist, OutputFilter *ofilter, const ViewSpecifier *vs, | ||
| 1171 | OutputStream **post) | ||
| 1172 | { | ||
| 1173 | 9924 | AVFormatContext *oc = mux->fc; | |
| 1174 | MuxStream *ms; | ||
| 1175 | OutputStream *ost; | ||
| 1176 | const AVCodec *enc; | ||
| 1177 | AVStream *st; | ||
| 1178 | 9924 | SchedulerNode src = { .type = SCH_NODE_TYPE_NONE }; | |
| 1179 | 9924 | AVDictionary *encoder_opts = NULL; | |
| 1180 | 9924 | int ret = 0, keep_pix_fmt = 0, autoscale = 1; | |
| 1181 | 9924 | int threads_manual = 0; | |
| 1182 | 9924 | AVRational enc_tb = { 0, 0 }; | |
| 1183 | 9924 | enum VideoSyncMethod vsync_method = VSYNC_AUTO; | |
| 1184 | 9924 | const char *bsfs = NULL, *time_base = NULL, *codec_tag = NULL, *manual_disp = NULL; | |
| 1185 | char *next; | ||
| 1186 | 9924 | double qscale = -1; | |
| 1187 | |||
| 1188 | 9924 | st = avformat_new_stream(oc, NULL); | |
| 1189 | 9924 | if (!st) | |
| 1190 | ✗ | return AVERROR(ENOMEM); | |
| 1191 | |||
| 1192 | 9924 | ms = mux_stream_alloc(mux, type); | |
| 1193 | 9924 | if (!ms) | |
| 1194 | ✗ | return AVERROR(ENOMEM); | |
| 1195 | |||
| 1196 | // only streams with sources (i.e. not attachments) | ||
| 1197 | // are handled by the scheduler | ||
| 1198 | 9924 | if (ist || ofilter) { | |
| 1199 | 9923 | ret = GROW_ARRAY(mux->sch_stream_idx, mux->nb_sch_stream_idx); | |
| 1200 | 9923 | if (ret < 0) | |
| 1201 | ✗ | return ret; | |
| 1202 | |||
| 1203 | 9923 | ret = sch_add_mux_stream(mux->sch, mux->sch_idx); | |
| 1204 | 9923 | if (ret < 0) | |
| 1205 | ✗ | return ret; | |
| 1206 | |||
| 1207 | 9923 | av_assert0(ret == mux->nb_sch_stream_idx - 1); | |
| 1208 | 9923 | mux->sch_stream_idx[ret] = ms->ost.index; | |
| 1209 | 9923 | ms->sch_idx = ret; | |
| 1210 | } | ||
| 1211 | |||
| 1212 | 9924 | ost = &ms->ost; | |
| 1213 | |||
| 1214 | 9924 | if (o->streamid) { | |
| 1215 | AVDictionaryEntry *e; | ||
| 1216 | char idx[16], *p; | ||
| 1217 | 85 | snprintf(idx, sizeof(idx), "%d", ost->index); | |
| 1218 | |||
| 1219 | 85 | e = av_dict_get(o->streamid, idx, NULL, 0); | |
| 1220 | 85 | if (e) { | |
| 1221 | 85 | st->id = strtol(e->value, &p, 0); | |
| 1222 | 85 | if (!e->value[0] || *p) { | |
| 1223 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value); | |
| 1224 | ✗ | return AVERROR(EINVAL); | |
| 1225 | } | ||
| 1226 | } | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | 9924 | ms->par_in = avcodec_parameters_alloc(); | |
| 1230 | 9924 | if (!ms->par_in) | |
| 1231 | ✗ | return AVERROR(ENOMEM); | |
| 1232 | |||
| 1233 | 9924 | ms->last_mux_dts = AV_NOPTS_VALUE; | |
| 1234 | |||
| 1235 | 9924 | ost->st = st; | |
| 1236 | 9924 | ost->ist = ist; | |
| 1237 | 9924 | ost->kf.ref_pts = AV_NOPTS_VALUE; | |
| 1238 | 9924 | ms->par_in->codec_type = type; | |
| 1239 | 9924 | st->codecpar->codec_type = type; | |
| 1240 | |||
| 1241 | 9924 | if (ost->type == AVMEDIA_TYPE_VIDEO) { | |
| 1242 | 7626 | if (ost->ist) | |
| 1243 | 6333 | ost->st->disposition = ost->ist->st->disposition; | |
| 1244 | |||
| 1245 | 7626 | opt_match_per_stream_str(ost, &o->disposition, oc, st, &manual_disp); | |
| 1246 | 7626 | if (manual_disp) { | |
| 1247 | 8 | ret = av_opt_set(ost->st, "disposition", manual_disp, 0); | |
| 1248 | 8 | if (ret < 0) | |
| 1249 | ✗ | return ret; | |
| 1250 | } | ||
| 1251 | |||
| 1252 | 7626 | ost->st->disposition &= AV_DISPOSITION_ATTACHED_PIC; | |
| 1253 | } | ||
| 1254 | |||
| 1255 | 9924 | ret = choose_encoder(o, oc, ms, &enc); | |
| 1256 | 9924 | if (ret < 0) { | |
| 1257 | ✗ | av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n"); | |
| 1258 | ✗ | return ret; | |
| 1259 | } | ||
| 1260 | |||
| 1261 | 9924 | if (enc) { | |
| 1262 | 8851 | ret = sch_add_enc(mux->sch, encoder_thread, ost, | |
| 1263 | 8851 | ost->type == AVMEDIA_TYPE_SUBTITLE ? NULL : enc_open); | |
| 1264 | 8851 | if (ret < 0) | |
| 1265 | ✗ | return ret; | |
| 1266 | 8851 | ms->sch_idx_enc = ret; | |
| 1267 | |||
| 1268 | 8851 | ret = enc_alloc(&ost->enc, enc, mux->sch, ms->sch_idx_enc, ost); | |
| 1269 | 8851 | if (ret < 0) | |
| 1270 | ✗ | return ret; | |
| 1271 | |||
| 1272 | 8851 | av_strlcat(ms->log_name, "/", sizeof(ms->log_name)); | |
| 1273 | 8851 | av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name)); | |
| 1274 | } else { | ||
| 1275 | 1073 | if (ofilter) { | |
| 1276 | ✗ | av_log(ost, AV_LOG_ERROR, | |
| 1277 | "Streamcopy requested for output stream fed " | ||
| 1278 | "from a complex filtergraph. Filtering and streamcopy " | ||
| 1279 | "cannot be used together.\n"); | ||
| 1280 | ✗ | return AVERROR(EINVAL); | |
| 1281 | } | ||
| 1282 | |||
| 1283 | 1073 | av_strlcat(ms->log_name, "/copy", sizeof(ms->log_name)); | |
| 1284 | } | ||
| 1285 | |||
| 1286 | 9924 | av_log(ost, AV_LOG_VERBOSE, "Created %s stream from ", | |
| 1287 | av_get_media_type_string(type)); | ||
| 1288 | 9924 | if (ist) | |
| 1289 | 8358 | av_log(ost, AV_LOG_VERBOSE, "input stream %d:%d", | |
| 1290 | 8358 | ist->file->index, ist->index); | |
| 1291 | 1566 | else if (ofilter) | |
| 1292 | 1565 | av_log(ost, AV_LOG_VERBOSE, "complex filtergraph %d:[%s]", | |
| 1293 | 1565 | ofilter->graph->index, ofilter->name); | |
| 1294 | 1 | else if (type == AVMEDIA_TYPE_ATTACHMENT) | |
| 1295 | 1 | av_log(ost, AV_LOG_VERBOSE, "attached file"); | |
| 1296 | ✗ | else av_assert0(0); | |
| 1297 | 9924 | av_log(ost, AV_LOG_VERBOSE, "\n"); | |
| 1298 | |||
| 1299 | 9924 | ms->pkt = av_packet_alloc(); | |
| 1300 | 9924 | if (!ms->pkt) | |
| 1301 | ✗ | return AVERROR(ENOMEM); | |
| 1302 | |||
| 1303 | 9924 | if (ost->enc) { | |
| 1304 | 8851 | AVIOContext *s = NULL; | |
| 1305 | 8851 | char *buf = NULL, *arg = NULL; | |
| 1306 | 8851 | const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL; | |
| 1307 | 8851 | const char *enc_time_base = NULL, *enc_reinit_opts = NULL, *preset = NULL; | |
| 1308 | |||
| 1309 | 8851 | ret = filter_codec_opts(o->g->codec_opts, enc->id, | |
| 1310 | oc, st, enc, &encoder_opts, | ||
| 1311 | &mux->enc_opts_used); | ||
| 1312 | 8851 | if (ret < 0) | |
| 1313 | ✗ | goto fail; | |
| 1314 | |||
| 1315 | 8851 | opt_match_per_stream_str(ost, &o->presets, oc, st, &preset); | |
| 1316 | 8851 | opt_match_per_stream_int(ost, &o->autoscale, oc, st, &autoscale); | |
| 1317 | 8851 | if (preset && (!(ret = get_preset_file_2(preset, enc->name, &s)))) { | |
| 1318 | AVBPrint bprint; | ||
| 1319 | ✗ | av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 1320 | do { | ||
| 1321 | ✗ | av_bprint_clear(&bprint); | |
| 1322 | ✗ | buf = get_line(s, &bprint); | |
| 1323 | ✗ | if (!buf) { | |
| 1324 | ✗ | ret = AVERROR(ENOMEM); | |
| 1325 | ✗ | break; | |
| 1326 | } | ||
| 1327 | |||
| 1328 | ✗ | if (!buf[0] || buf[0] == '#') | |
| 1329 | ✗ | continue; | |
| 1330 | ✗ | if (!(arg = strchr(buf, '='))) { | |
| 1331 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid line found in the preset file.\n"); | |
| 1332 | ✗ | ret = AVERROR(EINVAL); | |
| 1333 | ✗ | break; | |
| 1334 | } | ||
| 1335 | ✗ | *arg++ = 0; | |
| 1336 | ✗ | av_dict_set(&encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE); | |
| 1337 | ✗ | } while (!s->eof_reached); | |
| 1338 | ✗ | av_bprint_finalize(&bprint, NULL); | |
| 1339 | ✗ | avio_closep(&s); | |
| 1340 | } | ||
| 1341 | 8851 | if (ret) { | |
| 1342 | ✗ | av_log(ost, AV_LOG_FATAL, | |
| 1343 | "Preset %s specified, but could not be opened.\n", preset); | ||
| 1344 | ✗ | goto fail; | |
| 1345 | } | ||
| 1346 | |||
| 1347 | 8851 | opt_match_per_stream_str(ost, &o->enc_reinit_opts, oc, st, &enc_reinit_opts); | |
| 1348 | 8851 | if (enc_reinit_opts && | |
| 1349 | ✗ | (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) { | |
| 1350 | ✗ | ost->enc->reinit_opts = av_strdup(enc_reinit_opts); | |
| 1351 | ✗ | if (!ost->enc->reinit_opts) { | |
| 1352 | ✗ | ret = AVERROR(ENOMEM); | |
| 1353 | ✗ | goto fail; | |
| 1354 | } | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | 8851 | opt_match_per_stream_str(ost, &o->enc_stats_pre, oc, st, &enc_stats_pre); | |
| 1358 | 8851 | if (enc_stats_pre && | |
| 1359 | ✗ | (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) { | |
| 1360 | ✗ | const char *format = "{fidx} {sidx} {n} {t}"; | |
| 1361 | |||
| 1362 | ✗ | opt_match_per_stream_str(ost, &o->enc_stats_pre_fmt, oc, st, &format); | |
| 1363 | |||
| 1364 | ✗ | ret = enc_stats_init(ost, &ost->enc_stats_pre, 1, enc_stats_pre, format); | |
| 1365 | ✗ | if (ret < 0) | |
| 1366 | ✗ | goto fail; | |
| 1367 | } | ||
| 1368 | |||
| 1369 | 8851 | opt_match_per_stream_str(ost, &o->enc_stats_post, oc, st, &enc_stats_post); | |
| 1370 | 8851 | if (enc_stats_post && | |
| 1371 | ✗ | (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) { | |
| 1372 | ✗ | const char *format = "{fidx} {sidx} {n} {t}"; | |
| 1373 | |||
| 1374 | ✗ | opt_match_per_stream_str(ost, &o->enc_stats_post_fmt, oc, st, &format); | |
| 1375 | |||
| 1376 | ✗ | ret = enc_stats_init(ost, &ost->enc_stats_post, 0, enc_stats_post, format); | |
| 1377 | ✗ | if (ret < 0) | |
| 1378 | ✗ | goto fail; | |
| 1379 | } | ||
| 1380 | |||
| 1381 | 8851 | opt_match_per_stream_str(ost, &o->mux_stats, oc, st, &mux_stats); | |
| 1382 | 8851 | if (mux_stats && | |
| 1383 | ✗ | (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) { | |
| 1384 | ✗ | const char *format = "{fidx} {sidx} {n} {t}"; | |
| 1385 | |||
| 1386 | ✗ | opt_match_per_stream_str(ost, &o->mux_stats_fmt, oc, st, &format); | |
| 1387 | |||
| 1388 | ✗ | ret = enc_stats_init(ost, &ms->stats, 0, mux_stats, format); | |
| 1389 | ✗ | if (ret < 0) | |
| 1390 | ✗ | goto fail; | |
| 1391 | } | ||
| 1392 | |||
| 1393 | 8851 | opt_match_per_stream_str(ost, &o->enc_time_bases, oc, st, &enc_time_base); | |
| 1394 | 8851 | if (enc_time_base && type == AVMEDIA_TYPE_SUBTITLE) | |
| 1395 | ✗ | av_log(ost, AV_LOG_WARNING, | |
| 1396 | "-enc_time_base not supported for subtitles, ignoring\n"); | ||
| 1397 | 8851 | else if (enc_time_base) { | |
| 1398 | AVRational q; | ||
| 1399 | |||
| 1400 | 2 | if (!strcmp(enc_time_base, "demux")) { | |
| 1401 | 1 | q = (AVRational){ ENC_TIME_BASE_DEMUX, 0 }; | |
| 1402 | 1 | } else if (!strcmp(enc_time_base, "filter")) { | |
| 1403 | ✗ | q = (AVRational){ ENC_TIME_BASE_FILTER, 0 }; | |
| 1404 | } else { | ||
| 1405 | 1 | ret = av_parse_ratio(&q, enc_time_base, INT_MAX, 0, NULL); | |
| 1406 | 1 | if (ret < 0 || q.den <= 0 || q.num < 0) { | |
| 1407 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", enc_time_base); | |
| 1408 | ✗ | ret = ret < 0 ? ret : AVERROR(EINVAL); | |
| 1409 | ✗ | goto fail; | |
| 1410 | } | ||
| 1411 | } | ||
| 1412 | |||
| 1413 | 2 | enc_tb = q; | |
| 1414 | } | ||
| 1415 | |||
| 1416 | 8851 | threads_manual = !!av_dict_get(encoder_opts, "threads", NULL, 0); | |
| 1417 | |||
| 1418 | 8851 | ret = av_dict_copy(&ost->enc->encoder_opts, encoder_opts, 0); | |
| 1419 | 8851 | if (ret < 0) | |
| 1420 | ✗ | goto fail; | |
| 1421 | 8851 | ret = av_opt_set_dict2(ost->enc->enc_ctx, &encoder_opts, AV_OPT_SEARCH_CHILDREN); | |
| 1422 | 8851 | if (ret < 0) { | |
| 1423 | ✗ | av_log(ost, AV_LOG_ERROR, "Error applying encoder options: %s\n", | |
| 1424 | ✗ | av_err2str(ret)); | |
| 1425 | ✗ | goto fail; | |
| 1426 | } | ||
| 1427 | |||
| 1428 | 8851 | ret = check_avoptions(encoder_opts); | |
| 1429 | 8851 | if (ret < 0) | |
| 1430 | ✗ | goto fail; | |
| 1431 | |||
| 1432 | // default to automatic thread count | ||
| 1433 | 8851 | if (!threads_manual) | |
| 1434 | 3225 | ost->enc->enc_ctx->thread_count = 0; | |
| 1435 | } else { | ||
| 1436 | 1073 | ret = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, | |
| 1437 | NULL, &encoder_opts, | ||
| 1438 | &mux->enc_opts_used); | ||
| 1439 | 1073 | if (ret < 0) | |
| 1440 | ✗ | goto fail; | |
| 1441 | } | ||
| 1442 | |||
| 1443 | |||
| 1444 | 9924 | if (o->bitexact) { | |
| 1445 | 2369 | ost->bitexact = 1; | |
| 1446 | 7555 | } else if (ost->enc) { | |
| 1447 | 6681 | ost->bitexact = !!(ost->enc->enc_ctx->flags & AV_CODEC_FLAG_BITEXACT); | |
| 1448 | } | ||
| 1449 | |||
| 1450 | 9924 | if (enc) { | |
| 1451 | 8851 | ret = set_encoder_id(ost, enc); | |
| 1452 | 8851 | if (ret < 0) | |
| 1453 | ✗ | return ret; | |
| 1454 | } | ||
| 1455 | |||
| 1456 | 9924 | opt_match_per_stream_str(ost, &o->time_bases, oc, st, &time_base); | |
| 1457 | 9924 | if (time_base) { | |
| 1458 | AVRational q; | ||
| 1459 | 4 | if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 || | |
| 1460 | 4 | q.num <= 0 || q.den <= 0) { | |
| 1461 | ✗ | av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base); | |
| 1462 | ✗ | ret = AVERROR(EINVAL); | |
| 1463 | ✗ | goto fail; | |
| 1464 | } | ||
| 1465 | 4 | st->time_base = q; | |
| 1466 | } | ||
| 1467 | |||
| 1468 | 9924 | ms->max_frames = INT64_MAX; | |
| 1469 | 9924 | opt_match_per_stream_int64(ost, &o->max_frames, oc, st, &ms->max_frames); | |
| 1470 | 15599 | for (int i = 0; i < o->max_frames.nb_opt; i++) { | |
| 1471 | 5681 | char *p = o->max_frames.opt[i].specifier; | |
| 1472 | 5681 | if (!*p && type != AVMEDIA_TYPE_VIDEO) { | |
| 1473 | 6 | av_log(ost, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n"); | |
| 1474 | 6 | break; | |
| 1475 | } | ||
| 1476 | } | ||
| 1477 | |||
| 1478 | 9924 | ms->copy_prior_start = -1; | |
| 1479 | 9924 | opt_match_per_stream_int(ost, &o->copy_prior_start, oc, st, &ms->copy_prior_start); | |
| 1480 | 9924 | opt_match_per_stream_str(ost, &o->bitstream_filters, oc, st, &bsfs); | |
| 1481 | 9924 | if (bsfs && *bsfs) { | |
| 1482 | 209 | ret = av_bsf_list_parse_str(bsfs, &ms->bsf_ctx); | |
| 1483 | 209 | if (ret < 0) { | |
| 1484 | ✗ | av_log(ost, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret)); | |
| 1485 | ✗ | goto fail; | |
| 1486 | } | ||
| 1487 | } | ||
| 1488 | |||
| 1489 | 9924 | opt_match_per_stream_str(ost, &o->codec_tags, oc, st, &codec_tag); | |
| 1490 | 9924 | if (codec_tag) { | |
| 1491 | 9 | uint32_t tag = strtol(codec_tag, &next, 0); | |
| 1492 | 9 | if (*next) { | |
| 1493 | 9 | uint8_t buf[4] = { 0 }; | |
| 1494 | 9 | memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag))); | |
| 1495 | 9 | tag = AV_RL32(buf); | |
| 1496 | } | ||
| 1497 | 9 | ost->st->codecpar->codec_tag = tag; | |
| 1498 | 9 | ms->par_in->codec_tag = tag; | |
| 1499 | 9 | if (ost->enc) | |
| 1500 | 2 | ost->enc->codec_tag = tag; | |
| 1501 | } | ||
| 1502 | |||
| 1503 | 9924 | opt_match_per_stream_dbl(ost, &o->qscale, oc, st, &qscale); | |
| 1504 | 9924 | if (ost->enc) { | |
| 1505 | 8851 | if (qscale >= 0) { | |
| 1506 | 283 | ost->enc->flags |= AV_CODEC_FLAG_QSCALE; | |
| 1507 | 283 | ost->enc->global_quality = FF_QP2LAMBDA * qscale; | |
| 1508 | } else | ||
| 1509 | 8568 | ost->enc->global_quality = ost->enc->enc_ctx->global_quality; | |
| 1510 | } | ||
| 1511 | |||
| 1512 | 9924 | if (ms->sch_idx >= 0) { | |
| 1513 | 9923 | int max_muxing_queue_size = 128; | |
| 1514 | 9923 | int muxing_queue_data_threshold = 50 * 1024 * 1024; | |
| 1515 | |||
| 1516 | 9923 | opt_match_per_stream_int(ost, &o->max_muxing_queue_size, oc, st, | |
| 1517 | &max_muxing_queue_size); | ||
| 1518 | 9923 | opt_match_per_stream_int(ost, &o->muxing_queue_data_threshold, | |
| 1519 | oc, st, &muxing_queue_data_threshold); | ||
| 1520 | |||
| 1521 | 9923 | sch_mux_stream_buffering(mux->sch, mux->sch_idx, ms->sch_idx, | |
| 1522 | max_muxing_queue_size, muxing_queue_data_threshold); | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | 9924 | opt_match_per_stream_int(ost, &o->bits_per_raw_sample, oc, st, | |
| 1526 | &ost->bits_per_raw_sample); | ||
| 1527 | |||
| 1528 | 9924 | opt_match_per_stream_int(ost, &o->fix_sub_duration_heartbeat, | |
| 1529 | 9924 | oc, st, &ost->fix_sub_duration_heartbeat); | |
| 1530 | |||
| 1531 | 9924 | if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc) | |
| 1532 | 3352 | ost->enc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; | |
| 1533 | 9924 | if (oc->oformat->flags & AVFMT_FIXED_FRAMESIZE && ost->enc) | |
| 1534 | 32 | ost->enc->flags2 |= AV_CODEC_FLAG2_FIXED_FRAME_SIZE; | |
| 1535 | |||
| 1536 | 9924 | opt_match_per_stream_int(ost, &o->copy_initial_nonkeyframes, | |
| 1537 | oc, st, &ms->copy_initial_nonkeyframes); | ||
| 1538 | 9924 | switch (type) { | |
| 1539 | 7626 | case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost, &keep_pix_fmt, &vsync_method); break; | |
| 1540 | 2187 | case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break; | |
| 1541 | 98 | case AVMEDIA_TYPE_SUBTITLE: ret = new_stream_subtitle (mux, o, ost); break; | |
| 1542 | } | ||
| 1543 | 9924 | if (ret < 0) | |
| 1544 | ✗ | goto fail; | |
| 1545 | |||
| 1546 | |||
| 1547 | 9924 | if (ost->enc) { | |
| 1548 | 8851 | AVCodecContext *enc = ost->enc->enc_ctx; | |
| 1549 | 8851 | int do_pass = 0; | |
| 1550 | |||
| 1551 | /* two pass mode */ | ||
| 1552 | 8851 | opt_match_per_stream_int(ost, &o->pass, oc, st, &do_pass); | |
| 1553 | 8851 | if (do_pass) { | |
| 1554 | 16 | if (do_pass & 1) | |
| 1555 | 8 | enc->flags |= AV_CODEC_FLAG_PASS1; | |
| 1556 | 16 | if (do_pass & 2) | |
| 1557 | 8 | enc->flags |= AV_CODEC_FLAG_PASS2; | |
| 1558 | } | ||
| 1559 | |||
| 1560 | 8851 | opt_match_per_stream_str(ost, &o->passlogfiles, oc, st, &ost->logfile_prefix); | |
| 1561 | 8851 | if (ost->logfile_prefix && | |
| 1562 | 16 | !(ost->logfile_prefix = av_strdup(ost->logfile_prefix))) | |
| 1563 | ✗ | return AVERROR(ENOMEM); | |
| 1564 | |||
| 1565 | 8851 | if (do_pass) { | |
| 1566 | 16 | int ost_idx = -1; | |
| 1567 | char logfilename[1024]; | ||
| 1568 | FILE *f; | ||
| 1569 | |||
| 1570 | /* compute this stream's global index */ | ||
| 1571 | 32 | for (int idx = 0; idx <= ost->file->index; idx++) | |
| 1572 | 16 | ost_idx += output_files[idx]->nb_streams; | |
| 1573 | |||
| 1574 | 16 | snprintf(logfilename, sizeof(logfilename), "%s-%d.log", | |
| 1575 | 16 | ost->logfile_prefix ? ost->logfile_prefix : | |
| 1576 | DEFAULT_PASS_LOGFILENAME_PREFIX, | ||
| 1577 | ost_idx); | ||
| 1578 | 16 | if (!strcmp(enc->codec->name, "libx264") || !strcmp(enc->codec->name, "libvvenc")) { | |
| 1579 | ✗ | if (av_opt_is_set_to_default_by_name(enc, "stats", | |
| 1580 | AV_OPT_SEARCH_CHILDREN) > 0) | ||
| 1581 | ✗ | av_opt_set(enc, "stats", logfilename, | |
| 1582 | AV_OPT_SEARCH_CHILDREN); | ||
| 1583 | 16 | } else if (!strcmp(enc->codec->name, "libx265")) { | |
| 1584 | ✗ | if (av_opt_is_set_to_default_by_name(enc, "x265-stats", | |
| 1585 | AV_OPT_SEARCH_CHILDREN) > 0) | ||
| 1586 | ✗ | av_opt_set(enc, "x265-stats", logfilename, | |
| 1587 | AV_OPT_SEARCH_CHILDREN); | ||
| 1588 | } else { | ||
| 1589 | 16 | if (enc->flags & AV_CODEC_FLAG_PASS2) { | |
| 1590 | 8 | char *logbuffer = read_file_to_string(logfilename); | |
| 1591 | |||
| 1592 | 8 | if (!logbuffer) { | |
| 1593 | ✗ | av_log(ost, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n", | |
| 1594 | logfilename); | ||
| 1595 | ✗ | return AVERROR(EIO); | |
| 1596 | } | ||
| 1597 | 8 | enc->stats_in = logbuffer; | |
| 1598 | } | ||
| 1599 | 16 | if (enc->flags & AV_CODEC_FLAG_PASS1) { | |
| 1600 | 8 | f = fopen_utf8(logfilename, "wb"); | |
| 1601 | 8 | if (!f) { | |
| 1602 | ✗ | av_log(ost, AV_LOG_FATAL, | |
| 1603 | "Cannot write log file '%s' for pass-1 encoding: %s\n", | ||
| 1604 | ✗ | logfilename, strerror(errno)); | |
| 1605 | ✗ | return AVERROR(errno); | |
| 1606 | } | ||
| 1607 | 8 | ost->logfile = f; | |
| 1608 | } | ||
| 1609 | } | ||
| 1610 | } | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | 9924 | if (ost->enc && | |
| 1614 | 1742 | (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) { | |
| 1615 | 8806 | ret = ost_bind_filter(mux, ms, ofilter, o, enc_tb, vsync_method, | |
| 1616 | keep_pix_fmt, autoscale, threads_manual, vs, &src); | ||
| 1617 | 8806 | if (ret < 0) | |
| 1618 | 1 | goto fail; | |
| 1619 | 1118 | } else if (ost->ist) { | |
| 1620 | 1117 | ret = ist_use(ost->ist, !!ost->enc, NULL, &src); | |
| 1621 | 1117 | if (ret < 0) { | |
| 1622 | ✗ | av_log(ost, AV_LOG_ERROR, | |
| 1623 | "Error binding an input stream\n"); | ||
| 1624 | ✗ | goto fail; | |
| 1625 | } | ||
| 1626 | 1117 | ms->sch_idx_src = src.idx; | |
| 1627 | |||
| 1628 | // src refers to a decoder for transcoding, demux stream otherwise | ||
| 1629 | 1117 | if (ost->enc) { | |
| 1630 | 45 | ret = sch_connect(mux->sch, | |
| 1631 | 45 | src, SCH_ENC(ms->sch_idx_enc)); | |
| 1632 | 45 | if (ret < 0) | |
| 1633 | ✗ | goto fail; | |
| 1634 | 45 | src = SCH_ENC(ms->sch_idx_enc); | |
| 1635 | } | ||
| 1636 | } | ||
| 1637 | |||
| 1638 | // Audio/video filters and subtitle encoding subtract the output start | ||
| 1639 | // time before scheduling; streamcopy does so later in the muxer. | ||
| 1640 | 9923 | if (ost->enc && mux->of.start_time != AV_NOPTS_VALUE && ms->sch_idx >= 0) | |
| 1641 | 3 | sch_mux_stream_set_dts_pacing_offset(mux->sch, mux->sch_idx, | |
| 1642 | 3 | ms->sch_idx, mux->of.start_time); | |
| 1643 | |||
| 1644 | 9923 | if (src.type != SCH_NODE_TYPE_NONE) { | |
| 1645 | 9922 | ret = sch_connect(mux->sch, | |
| 1646 | 9922 | src, SCH_MSTREAM(mux->sch_idx, ms->sch_idx)); | |
| 1647 | 9922 | if (ret < 0) | |
| 1648 | ✗ | goto fail; | |
| 1649 | } else { | ||
| 1650 | // only attachment streams don't have a source | ||
| 1651 | 1 | av_assert0(type == AVMEDIA_TYPE_ATTACHMENT && ms->sch_idx < 0); | |
| 1652 | } | ||
| 1653 | |||
| 1654 | 9923 | if (ost->ist && !ost->enc) { | |
| 1655 | 1072 | ret = streamcopy_init(o, mux, ost, &encoder_opts); | |
| 1656 | 1072 | if (ret < 0) | |
| 1657 | ✗ | goto fail; | |
| 1658 | } | ||
| 1659 | |||
| 1660 | // copy estimated duration as a hint to the muxer | ||
| 1661 | 9923 | if (ost->ist && ost->ist->st->duration > 0) { | |
| 1662 | 6467 | ms->stream_duration = ist->st->duration; | |
| 1663 | 6467 | ms->stream_duration_tb = ist->st->time_base; | |
| 1664 | } | ||
| 1665 | |||
| 1666 | 9923 | if (post) | |
| 1667 | 1 | *post = ost; | |
| 1668 | |||
| 1669 | 9923 | ret = 0; | |
| 1670 | |||
| 1671 | 9924 | fail: | |
| 1672 | 9924 | av_dict_free(&encoder_opts); | |
| 1673 | |||
| 1674 | 9924 | return ret; | |
| 1675 | } | ||
| 1676 | |||
| 1677 | 2 | static int64_t score_new_packets_event_flag(const AVStreamGroup *stg) | |
| 1678 | { | ||
| 1679 | 2 | int64_t score = 0; | |
| 1680 | |||
| 1681 | 2 | for (int j = 0; j < stg->nb_entries; j++) { | |
| 1682 | 2 | if (stg->entries[j]->type != AV_STREAM_GROUP_ENTRY_STREAM) | |
| 1683 | ✗ | continue; | |
| 1684 | |||
| 1685 | 2 | AVStream *st = stg->entries[j]->stream; | |
| 1686 | |||
| 1687 | 2 | if (st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) { | |
| 1688 | 2 | score += 100000000; | |
| 1689 | 2 | break; | |
| 1690 | } | ||
| 1691 | } | ||
| 1692 | |||
| 1693 | 2 | return score; | |
| 1694 | } | ||
| 1695 | |||
| 1696 | 7430 | static int map_auto_video(Muxer *mux, const OptionsContext *o) | |
| 1697 | { | ||
| 1698 | 7430 | AVFormatContext *oc = mux->fc; | |
| 1699 | 7430 | InputStreamGroup *best_istg = NULL; | |
| 1700 | 7430 | InputStream *best_ist = NULL; | |
| 1701 | 7430 | int64_t best_score = 0; | |
| 1702 | int qcr; | ||
| 1703 | |||
| 1704 | /* video: highest resolution */ | ||
| 1705 | 7430 | if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_VIDEO) == AV_CODEC_ID_NONE) | |
| 1706 | 806 | return 0; | |
| 1707 | |||
| 1708 | 6624 | qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0); | |
| 1709 | 13289 | for (int j = 0; j < nb_input_files; j++) { | |
| 1710 | 6665 | InputFile *ifile = input_files[j]; | |
| 1711 | 6665 | InputStreamGroup *file_best_istg = NULL; | |
| 1712 | 6665 | InputStream *file_best_ist = NULL; | |
| 1713 | 6665 | int64_t file_best_score = 0; | |
| 1714 | 6688 | for (int i = 0; i < ifile->nb_stream_groups; i++) { | |
| 1715 | 23 | InputStreamGroup *istg = ifile->stream_groups[i]; | |
| 1716 | int64_t score; | ||
| 1717 | |||
| 1718 | 23 | if (!istg->fg) | |
| 1719 | 21 | continue; | |
| 1720 | |||
| 1721 | 2 | score = score_new_packets_event_flag(istg->stg); | |
| 1722 | |||
| 1723 | 2 | switch (istg->stg->type) { | |
| 1724 | 1 | case AV_STREAM_GROUP_PARAMS_TILE_GRID: { | |
| 1725 | 1 | const AVStreamGroupTileGrid *tg = istg->stg->params.tile_grid; | |
| 1726 | 2 | score += tg->width * (int64_t)tg->height | |
| 1727 | 1 | + 5000000*!!(istg->stg->disposition & AV_DISPOSITION_DEFAULT); | |
| 1728 | 1 | break; | |
| 1729 | } | ||
| 1730 | 1 | case AV_STREAM_GROUP_PARAMS_ALPHA: { | |
| 1731 | 1 | const AVStreamGroupLayeredVideo *layered_video = istg->stg->params.layered_video; | |
| 1732 | 3 | for (int k = 0; k < istg->stg->nb_entries; k++) { | |
| 1733 | 2 | const AVStreamGroupEntry *entry = istg->stg->entries[k]; | |
| 1734 | 4 | int disposition = entry->type == AV_STREAM_GROUP_ENTRY_STREAM | |
| 1735 | 2 | ? entry->stream->disposition | |
| 1736 | 2 | : entry->stream_group->disposition; | |
| 1737 | 2 | if (istg->stg->entries[k]->type == AV_STREAM_GROUP_ENTRY_STREAM_GROUP) | |
| 1738 | ✗ | score += score_new_packets_event_flag(entry->stream_group); | |
| 1739 | 2 | score += 5000000*!!(disposition & AV_DISPOSITION_DEFAULT); | |
| 1740 | } | ||
| 1741 | 2 | score += layered_video->width * (int64_t)layered_video->height | |
| 1742 | 1 | + 5000000*!!(istg->stg->disposition & AV_DISPOSITION_DEFAULT); | |
| 1743 | 1 | break; | |
| 1744 | } | ||
| 1745 | ✗ | default: | |
| 1746 | ✗ | continue; | |
| 1747 | } | ||
| 1748 | |||
| 1749 | 2 | if (score > file_best_score) { | |
| 1750 | 2 | file_best_score = score; | |
| 1751 | 2 | file_best_istg = istg; | |
| 1752 | } | ||
| 1753 | } | ||
| 1754 | 13620 | for (int i = 0; i < ifile->nb_streams; i++) { | |
| 1755 | 6955 | InputStream *ist = ifile->streams[i]; | |
| 1756 | 6955 | const AVCodecDescriptor *desc = avcodec_descriptor_get(ist->st->codecpar->codec_id); | |
| 1757 | int64_t score; | ||
| 1758 | |||
| 1759 | 6955 | if (ist->user_set_discard == AVDISCARD_ALL || | |
| 1760 | 6954 | ist->st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO || | |
| 1761 | 6072 | (desc && (ifile->nb_streams > 1 || nb_input_files > 1) && | |
| 1762 | 284 | (desc->props & AV_CODEC_PROP_ENHANCEMENT))) | |
| 1763 | 883 | continue; | |
| 1764 | |||
| 1765 | 12144 | score = ist->st->codecpar->width * (int64_t)ist->st->codecpar->height | |
| 1766 | 6072 | + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) | |
| 1767 | 6072 | + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT); | |
| 1768 | 6072 | if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) | |
| 1769 | 9 | score = 1; | |
| 1770 | |||
| 1771 | 6072 | if (score > file_best_score) { | |
| 1772 | 6065 | if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) | |
| 1773 | ✗ | continue; | |
| 1774 | 6065 | file_best_score = score; | |
| 1775 | 6065 | file_best_ist = ist; | |
| 1776 | 6065 | file_best_istg = NULL; | |
| 1777 | } | ||
| 1778 | } | ||
| 1779 | 6665 | if (file_best_istg) { | |
| 1780 | 2 | file_best_score -= 5000000*!!(file_best_istg->stg->disposition & AV_DISPOSITION_DEFAULT); | |
| 1781 | 2 | if (file_best_score > best_score) { | |
| 1782 | 2 | best_score = file_best_score; | |
| 1783 | 2 | best_istg = file_best_istg; | |
| 1784 | 2 | best_ist = NULL; | |
| 1785 | } | ||
| 1786 | } | ||
| 1787 | 6665 | if (file_best_ist) { | |
| 1788 | 6065 | if((qcr == MKTAG('A', 'P', 'I', 'C')) || | |
| 1789 | 6065 | !(file_best_ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) | |
| 1790 | 6057 | file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT); | |
| 1791 | 6065 | if (file_best_score > best_score) { | |
| 1792 | 6065 | best_score = file_best_score; | |
| 1793 | 6065 | best_ist = file_best_ist; | |
| 1794 | 6065 | best_istg = NULL; | |
| 1795 | } | ||
| 1796 | } | ||
| 1797 | } | ||
| 1798 | 6624 | if (best_istg) { | |
| 1799 | 2 | FilterGraph *fg = best_istg->fg; | |
| 1800 | 2 | OutputFilter *ofilter = fg->outputs[0]; | |
| 1801 | |||
| 1802 | 2 | av_assert0(fg->nb_outputs == 1); | |
| 1803 | 2 | av_log(mux, AV_LOG_VERBOSE, "Creating output stream from stream group derived complex filtergraph %d.\n", fg->index); | |
| 1804 | |||
| 1805 | 2 | return ost_add(mux, o, AVMEDIA_TYPE_VIDEO, NULL, ofilter, NULL, NULL); | |
| 1806 | } | ||
| 1807 | 6622 | if (best_ist) | |
| 1808 | 6065 | return ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist, NULL, NULL, NULL); | |
| 1809 | |||
| 1810 | 557 | return 0; | |
| 1811 | } | ||
| 1812 | |||
| 1813 | 8710 | static int map_auto_audio(Muxer *mux, const OptionsContext *o) | |
| 1814 | { | ||
| 1815 | 8710 | AVFormatContext *oc = mux->fc; | |
| 1816 | 8710 | InputStream *best_ist = NULL; | |
| 1817 | 8710 | int best_score = 0; | |
| 1818 | |||
| 1819 | /* audio: most channels */ | ||
| 1820 | 8710 | if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_AUDIO) == AV_CODEC_ID_NONE) | |
| 1821 | 2007 | return 0; | |
| 1822 | |||
| 1823 | 13325 | for (int j = 0; j < nb_input_files; j++) { | |
| 1824 | 6622 | InputFile *ifile = input_files[j]; | |
| 1825 | 6622 | InputStream *file_best_ist = NULL; | |
| 1826 | 6622 | int file_best_score = 0; | |
| 1827 | 13454 | for (int i = 0; i < ifile->nb_streams; i++) { | |
| 1828 | 6832 | InputStream *ist = ifile->streams[i]; | |
| 1829 | int score; | ||
| 1830 | |||
| 1831 | 6832 | if (ist->user_set_discard == AVDISCARD_ALL || | |
| 1832 | 6831 | ist->st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) | |
| 1833 | 5210 | continue; | |
| 1834 | |||
| 1835 | 3244 | score = ist->st->codecpar->ch_layout.nb_channels | |
| 1836 | 1622 | + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) | |
| 1837 | 1622 | + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT); | |
| 1838 | 1622 | if (score > file_best_score) { | |
| 1839 | 1612 | file_best_score = score; | |
| 1840 | 1612 | file_best_ist = ist; | |
| 1841 | } | ||
| 1842 | } | ||
| 1843 | 6622 | if (file_best_ist) { | |
| 1844 | 1612 | file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT); | |
| 1845 | 1612 | if (file_best_score > best_score) { | |
| 1846 | 1612 | best_score = file_best_score; | |
| 1847 | 1612 | best_ist = file_best_ist; | |
| 1848 | } | ||
| 1849 | } | ||
| 1850 | } | ||
| 1851 | 6703 | if (best_ist) | |
| 1852 | 1612 | return ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist, NULL, NULL, NULL); | |
| 1853 | |||
| 1854 | 5091 | return 0; | |
| 1855 | } | ||
| 1856 | |||
| 1857 | 8885 | static int map_auto_subtitle(Muxer *mux, const OptionsContext *o) | |
| 1858 | { | ||
| 1859 | 8885 | AVFormatContext *oc = mux->fc; | |
| 1860 | 8885 | const char *subtitle_codec_name = NULL; | |
| 1861 | |||
| 1862 | /* subtitles: pick first */ | ||
| 1863 | 8885 | subtitle_codec_name = opt_match_per_type_str(&o->codec_names, 's'); | |
| 1864 | 8885 | if (!avcodec_find_encoder(oc->oformat->subtitle_codec) && !subtitle_codec_name) | |
| 1865 | 8796 | return 0; | |
| 1866 | |||
| 1867 | 138 | for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) | |
| 1868 | 102 | if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { | |
| 1869 | AVCodecDescriptor const *input_descriptor = | ||
| 1870 | 53 | avcodec_descriptor_get(ist->st->codecpar->codec_id); | |
| 1871 | 53 | AVCodecDescriptor const *output_descriptor = NULL; | |
| 1872 | AVCodec const *output_codec = | ||
| 1873 | 53 | avcodec_find_encoder(oc->oformat->subtitle_codec); | |
| 1874 | 53 | int input_props = 0, output_props = 0; | |
| 1875 | 53 | if (ist->user_set_discard == AVDISCARD_ALL) | |
| 1876 | ✗ | continue; | |
| 1877 | 53 | if (output_codec) | |
| 1878 | 45 | output_descriptor = avcodec_descriptor_get(output_codec->id); | |
| 1879 | 53 | if (input_descriptor) | |
| 1880 | 53 | input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB); | |
| 1881 | 53 | if (output_descriptor) | |
| 1882 | 45 | output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB); | |
| 1883 | 53 | if (subtitle_codec_name || | |
| 1884 | 36 | input_props & output_props || | |
| 1885 | // Map dvb teletext which has neither property to any output subtitle encoder | ||
| 1886 | ✗ | input_descriptor && output_descriptor && | |
| 1887 | ✗ | (!input_descriptor->props || | |
| 1888 | ✗ | !output_descriptor->props)) { | |
| 1889 | 53 | return ost_add(mux, o, AVMEDIA_TYPE_SUBTITLE, ist, NULL, NULL, NULL); | |
| 1890 | } | ||
| 1891 | } | ||
| 1892 | |||
| 1893 | 36 | return 0; | |
| 1894 | } | ||
| 1895 | |||
| 1896 | 8886 | static int map_auto_data(Muxer *mux, const OptionsContext *o) | |
| 1897 | { | ||
| 1898 | 8886 | AVFormatContext *oc = mux->fc; | |
| 1899 | /* Data only if codec id match */ | ||
| 1900 | 8886 | enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_DATA); | |
| 1901 | |||
| 1902 | 8886 | if (codec_id == AV_CODEC_ID_NONE) | |
| 1903 | 8886 | return 0; | |
| 1904 | |||
| 1905 | ✗ | for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) { | |
| 1906 | ✗ | if (ist->user_set_discard == AVDISCARD_ALL) | |
| 1907 | ✗ | continue; | |
| 1908 | ✗ | if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA && | |
| 1909 | ✗ | ist->st->codecpar->codec_id == codec_id) { | |
| 1910 | ✗ | int ret = ost_add(mux, o, AVMEDIA_TYPE_DATA, ist, NULL, NULL, NULL); | |
| 1911 | ✗ | if (ret < 0) | |
| 1912 | ✗ | return ret; | |
| 1913 | } | ||
| 1914 | } | ||
| 1915 | |||
| 1916 | ✗ | return 0; | |
| 1917 | } | ||
| 1918 | |||
| 1919 | 719 | static int map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map) | |
| 1920 | { | ||
| 1921 | InputStream *ist; | ||
| 1922 | int ret; | ||
| 1923 | |||
| 1924 | 719 | if (map->disabled) | |
| 1925 | ✗ | return 0; | |
| 1926 | |||
| 1927 | 719 | if (map->linklabel) { | |
| 1928 | FilterGraph *fg; | ||
| 1929 | 91 | OutputFilter *ofilter = NULL; | |
| 1930 | int j, k; | ||
| 1931 | |||
| 1932 | 92 | for (j = 0; j < nb_filtergraphs; j++) { | |
| 1933 | 92 | fg = filtergraphs[j]; | |
| 1934 | 291 | for (k = 0; k < fg->nb_outputs; k++) { | |
| 1935 | 290 | const char *linklabel = fg->outputs[k]->linklabel; | |
| 1936 | 290 | if (linklabel && !strcmp(linklabel, map->linklabel)) { | |
| 1937 | 91 | ofilter = fg->outputs[k]; | |
| 1938 | 91 | goto loop_end; | |
| 1939 | } | ||
| 1940 | } | ||
| 1941 | } | ||
| 1942 | ✗ | loop_end: | |
| 1943 | 91 | if (!ofilter) { | |
| 1944 | ✗ | av_log(mux, AV_LOG_FATAL, "Output with label '%s' does not exist " | |
| 1945 | ✗ | "in any defined filter graph, or was already used elsewhere.\n", map->linklabel); | |
| 1946 | ✗ | return AVERROR(EINVAL); | |
| 1947 | } | ||
| 1948 | |||
| 1949 | 91 | av_log(mux, AV_LOG_VERBOSE, "Creating output stream from an explicitly " | |
| 1950 | 91 | "mapped complex filtergraph %d, output [%s]\n", fg->index, map->linklabel); | |
| 1951 | |||
| 1952 | 91 | ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL, NULL); | |
| 1953 | 91 | if (ret < 0) | |
| 1954 | ✗ | return ret; | |
| 1955 | } else { | ||
| 1956 | 628 | const ViewSpecifier *vs = map->vs.type == VIEW_SPECIFIER_TYPE_NONE ? | |
| 1957 | 628 | NULL : &map->vs; | |
| 1958 | |||
| 1959 | 628 | ist = input_files[map->file_index]->streams[map->stream_index]; | |
| 1960 | 628 | if (ist->user_set_discard == AVDISCARD_ALL) { | |
| 1961 | ✗ | av_log(mux, AV_LOG_FATAL, "Stream #%d:%d is disabled and cannot be mapped.\n", | |
| 1962 | ✗ | map->file_index, map->stream_index); | |
| 1963 | ✗ | return AVERROR(EINVAL); | |
| 1964 | } | ||
| 1965 | 628 | if(o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) | |
| 1966 | ✗ | return 0; | |
| 1967 | 628 | if(o-> audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) | |
| 1968 | ✗ | return 0; | |
| 1969 | 628 | if(o-> video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) | |
| 1970 | ✗ | return 0; | |
| 1971 | 628 | if(o-> data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA) | |
| 1972 | ✗ | return 0; | |
| 1973 | |||
| 1974 | 628 | if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN && | |
| 1975 | ✗ | !copy_unknown_streams) { | |
| 1976 | ✗ | av_log(mux, ignore_unknown_streams ? AV_LOG_WARNING : AV_LOG_FATAL, | |
| 1977 | "Cannot map stream #%d:%d - unsupported type.\n", | ||
| 1978 | ✗ | map->file_index, map->stream_index); | |
| 1979 | ✗ | if (!ignore_unknown_streams) { | |
| 1980 | ✗ | av_log(mux, AV_LOG_FATAL, | |
| 1981 | "If you want unsupported types ignored instead " | ||
| 1982 | "of failing, please use the -ignore_unknown option\n" | ||
| 1983 | "If you want them copied, please use -copy_unknown\n"); | ||
| 1984 | ✗ | return AVERROR(EINVAL); | |
| 1985 | } | ||
| 1986 | ✗ | return 0; | |
| 1987 | } | ||
| 1988 | |||
| 1989 | 628 | if (vs && ist->st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) { | |
| 1990 | ✗ | av_log(mux, AV_LOG_ERROR, | |
| 1991 | "View specifier given for mapping a %s input stream\n", | ||
| 1992 | ✗ | av_get_media_type_string(ist->st->codecpar->codec_type)); | |
| 1993 | ✗ | return AVERROR(EINVAL); | |
| 1994 | } | ||
| 1995 | |||
| 1996 | 628 | ret = ost_add(mux, o, ist->st->codecpar->codec_type, ist, NULL, vs, NULL); | |
| 1997 | 628 | if (ret < 0) | |
| 1998 | ✗ | return ret; | |
| 1999 | } | ||
| 2000 | |||
| 2001 | 719 | return 0; | |
| 2002 | } | ||
| 2003 | |||
| 2004 | 9222 | static int of_add_attachments(Muxer *mux, const OptionsContext *o) | |
| 2005 | { | ||
| 2006 | MuxStream *ms; | ||
| 2007 | OutputStream *ost; | ||
| 2008 | int err; | ||
| 2009 | |||
| 2010 | 9223 | for (int i = 0; i < o->nb_attachments; i++) { | |
| 2011 | AVIOContext *pb; | ||
| 2012 | uint8_t *attachment; | ||
| 2013 | char *attachment_filename; | ||
| 2014 | const char *p; | ||
| 2015 | int64_t len; | ||
| 2016 | |||
| 2017 | 1 | if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) { | |
| 2018 | ✗ | av_log(mux, AV_LOG_FATAL, "Could not open attachment file %s.\n", | |
| 2019 | ✗ | o->attachments[i]); | |
| 2020 | ✗ | return err; | |
| 2021 | } | ||
| 2022 | 1 | if ((len = avio_size(pb)) <= 0) { | |
| 2023 | ✗ | av_log(mux, AV_LOG_FATAL, "Could not get size of the attachment %s.\n", | |
| 2024 | ✗ | o->attachments[i]); | |
| 2025 | ✗ | err = len ? len : AVERROR_INVALIDDATA; | |
| 2026 | ✗ | goto read_fail; | |
| 2027 | } | ||
| 2028 | 1 | if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) { | |
| 2029 | ✗ | av_log(mux, AV_LOG_FATAL, "Attachment %s too large.\n", | |
| 2030 | ✗ | o->attachments[i]); | |
| 2031 | ✗ | err = AVERROR(ERANGE); | |
| 2032 | ✗ | goto read_fail; | |
| 2033 | } | ||
| 2034 | |||
| 2035 | 1 | attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 2036 | 1 | if (!attachment) { | |
| 2037 | ✗ | err = AVERROR(ENOMEM); | |
| 2038 | ✗ | goto read_fail; | |
| 2039 | } | ||
| 2040 | |||
| 2041 | 1 | err = avio_read(pb, attachment, len); | |
| 2042 | 1 | if (err < 0) | |
| 2043 | ✗ | av_log(mux, AV_LOG_FATAL, "Error reading attachment file %s: %s\n", | |
| 2044 | ✗ | o->attachments[i], av_err2str(err)); | |
| 2045 | 1 | else if (err != len) { | |
| 2046 | ✗ | av_log(mux, AV_LOG_FATAL, "Could not read all %"PRId64" bytes for " | |
| 2047 | ✗ | "attachment file %s\n", len, o->attachments[i]); | |
| 2048 | ✗ | err = AVERROR(EIO); | |
| 2049 | } | ||
| 2050 | |||
| 2051 | 1 | read_fail: | |
| 2052 | 1 | avio_closep(&pb); | |
| 2053 | 1 | if (err < 0) | |
| 2054 | ✗ | return err; | |
| 2055 | |||
| 2056 | 1 | memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 2057 | |||
| 2058 | 1 | av_log(mux, AV_LOG_VERBOSE, "Creating attachment stream from file %s\n", | |
| 2059 | 1 | o->attachments[i]); | |
| 2060 | |||
| 2061 | 1 | attachment_filename = av_strdup(o->attachments[i]); | |
| 2062 | 1 | if (!attachment_filename) { | |
| 2063 | ✗ | av_free(attachment); | |
| 2064 | ✗ | return AVERROR(ENOMEM); | |
| 2065 | } | ||
| 2066 | |||
| 2067 | 1 | err = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL, NULL, NULL, &ost); | |
| 2068 | 1 | if (err < 0) { | |
| 2069 | ✗ | av_free(attachment_filename); | |
| 2070 | ✗ | av_freep(&attachment); | |
| 2071 | ✗ | return err; | |
| 2072 | } | ||
| 2073 | |||
| 2074 | 1 | ms = ms_from_ost(ost); | |
| 2075 | |||
| 2076 | 1 | ost->attachment_filename = attachment_filename; | |
| 2077 | 1 | ms->par_in->extradata = attachment; | |
| 2078 | 1 | ms->par_in->extradata_size = len; | |
| 2079 | |||
| 2080 | 1 | p = strrchr(o->attachments[i], '/'); | |
| 2081 | 1 | av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE); | |
| 2082 | } | ||
| 2083 | |||
| 2084 | 9222 | return 0; | |
| 2085 | } | ||
| 2086 | |||
| 2087 | 9223 | static int create_streams(Muxer *mux, const OptionsContext *o) | |
| 2088 | { | ||
| 2089 | static int (* const map_func[])(Muxer *mux, const OptionsContext *o) = { | ||
| 2090 | [AVMEDIA_TYPE_VIDEO] = map_auto_video, | ||
| 2091 | [AVMEDIA_TYPE_AUDIO] = map_auto_audio, | ||
| 2092 | [AVMEDIA_TYPE_SUBTITLE] = map_auto_subtitle, | ||
| 2093 | [AVMEDIA_TYPE_DATA] = map_auto_data, | ||
| 2094 | }; | ||
| 2095 | |||
| 2096 | 9223 | AVFormatContext *oc = mux->fc; | |
| 2097 | |||
| 2098 | 9223 | int auto_disable = | |
| 2099 | 9223 | o->video_disable * (1 << AVMEDIA_TYPE_VIDEO) | | |
| 2100 | 9223 | o->audio_disable * (1 << AVMEDIA_TYPE_AUDIO) | | |
| 2101 | 9223 | o->subtitle_disable * (1 << AVMEDIA_TYPE_SUBTITLE) | | |
| 2102 | 9223 | o->data_disable * (1 << AVMEDIA_TYPE_DATA); | |
| 2103 | |||
| 2104 | int ret; | ||
| 2105 | |||
| 2106 | /* create streams for all unlabeled output pads */ | ||
| 2107 | 10564 | for (int i = 0; i < nb_filtergraphs; i++) { | |
| 2108 | 1341 | FilterGraph *fg = filtergraphs[i]; | |
| 2109 | 2921 | for (int j = 0; j < fg->nb_outputs; j++) { | |
| 2110 | 1580 | OutputFilter *ofilter = fg->outputs[j]; | |
| 2111 | |||
| 2112 | 1580 | if (ofilter->linklabel || ofilter->bound) | |
| 2113 | 108 | continue; | |
| 2114 | |||
| 2115 | 1472 | auto_disable |= 1 << ofilter->type; | |
| 2116 | |||
| 2117 | 1472 | av_log(mux, AV_LOG_VERBOSE, "Creating output stream from unlabeled " | |
| 2118 | "output of complex filtergraph %d.", fg->index); | ||
| 2119 | 1472 | if (!o->nb_stream_maps) | |
| 2120 | 1472 | av_log(mux, AV_LOG_VERBOSE, " This overrides automatic %s mapping.", | |
| 2121 | av_get_media_type_string(ofilter->type)); | ||
| 2122 | 1472 | av_log(mux, AV_LOG_VERBOSE, "\n"); | |
| 2123 | |||
| 2124 | 1472 | ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL, NULL); | |
| 2125 | 1472 | if (ret < 0) | |
| 2126 | ✗ | return ret; | |
| 2127 | } | ||
| 2128 | } | ||
| 2129 | |||
| 2130 | 9223 | if (!o->nb_stream_maps) { | |
| 2131 | 8887 | av_log(mux, AV_LOG_VERBOSE, "No explicit maps, mapping streams automatically...\n"); | |
| 2132 | |||
| 2133 | /* pick the "best" stream of each type */ | ||
| 2134 | 44432 | for (int i = 0; i < FF_ARRAY_ELEMS(map_func); i++) { | |
| 2135 | 35546 | if (!map_func[i] || auto_disable & (1 << i)) | |
| 2136 | 1635 | continue; | |
| 2137 | 33911 | ret = map_func[i](mux, o); | |
| 2138 | 33911 | if (ret < 0) | |
| 2139 | 1 | return ret; | |
| 2140 | } | ||
| 2141 | } else { | ||
| 2142 | 336 | av_log(mux, AV_LOG_VERBOSE, "Adding streams from explicit maps...\n"); | |
| 2143 | |||
| 2144 | 1055 | for (int i = 0; i < o->nb_stream_maps; i++) { | |
| 2145 | 719 | ret = map_manual(mux, o, &o->stream_maps[i]); | |
| 2146 | 719 | if (ret < 0) | |
| 2147 | ✗ | return ret; | |
| 2148 | } | ||
| 2149 | } | ||
| 2150 | |||
| 2151 | 9222 | ret = of_add_attachments(mux, o); | |
| 2152 | 9222 | if (ret < 0) | |
| 2153 | ✗ | return ret; | |
| 2154 | |||
| 2155 | // setup fix_sub_duration_heartbeat mappings | ||
| 2156 | 19145 | for (unsigned i = 0; i < oc->nb_streams; i++) { | |
| 2157 | 9923 | MuxStream *src = ms_from_ost(mux->of.streams[i]); | |
| 2158 | |||
| 2159 | 9923 | if (!src->ost.fix_sub_duration_heartbeat) | |
| 2160 | 9922 | continue; | |
| 2161 | |||
| 2162 | 3 | for (unsigned j = 0; j < oc->nb_streams; j++) { | |
| 2163 | 2 | MuxStream *dst = ms_from_ost(mux->of.streams[j]); | |
| 2164 | |||
| 2165 | 2 | if (src == dst || dst->ost.type != AVMEDIA_TYPE_SUBTITLE || | |
| 2166 | 1 | !dst->ost.enc || !dst->ost.ist || !dst->ost.ist->fix_sub_duration) | |
| 2167 | 1 | continue; | |
| 2168 | |||
| 2169 | 1 | ret = sch_mux_sub_heartbeat_add(mux->sch, mux->sch_idx, src->sch_idx, | |
| 2170 | 1 | dst->sch_idx_src); | |
| 2171 | |||
| 2172 | } | ||
| 2173 | } | ||
| 2174 | |||
| 2175 | // handle -apad | ||
| 2176 | 9222 | if (o->shortest) { | |
| 2177 | 5 | int have_video = 0; | |
| 2178 | |||
| 2179 | 5 | for (unsigned i = 0; i < mux->of.nb_streams; i++) | |
| 2180 | 5 | if (mux->of.streams[i]->type == AVMEDIA_TYPE_VIDEO) { | |
| 2181 | 5 | have_video = 1; | |
| 2182 | 5 | break; | |
| 2183 | } | ||
| 2184 | |||
| 2185 | 15 | for (unsigned i = 0; have_video && i < mux->of.nb_streams; i++) { | |
| 2186 | 10 | MuxStream *ms = ms_from_ost(mux->of.streams[i]); | |
| 2187 | 10 | OutputFilter *ofilter = ms->ost.filter; | |
| 2188 | |||
| 2189 | 10 | if (ms->ost.type != AVMEDIA_TYPE_AUDIO || !ms->apad || !ofilter) | |
| 2190 | 10 | continue; | |
| 2191 | |||
| 2192 | ✗ | ofilter->apad = av_strdup(ms->apad); | |
| 2193 | ✗ | if (!ofilter->apad) | |
| 2194 | ✗ | return AVERROR(ENOMEM); | |
| 2195 | } | ||
| 2196 | } | ||
| 2197 | 19145 | for (unsigned i = 0; i < mux->of.nb_streams; i++) { | |
| 2198 | 9923 | MuxStream *ms = ms_from_ost(mux->of.streams[i]); | |
| 2199 | 9923 | ms->apad = NULL; | |
| 2200 | } | ||
| 2201 | |||
| 2202 | 9222 | if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) { | |
| 2203 | ✗ | av_dump_format(oc, nb_output_files - 1, oc->url, 1); | |
| 2204 | ✗ | av_log(mux, AV_LOG_ERROR, "Output file does not contain any stream\n"); | |
| 2205 | ✗ | return AVERROR(EINVAL); | |
| 2206 | } | ||
| 2207 | |||
| 2208 | 9222 | return check_stereo3d_leftovers(mux, o); | |
| 2209 | } | ||
| 2210 | |||
| 2211 | 9219 | static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, | |
| 2212 | int64_t buf_size_us, int shortest) | ||
| 2213 | { | ||
| 2214 | 9219 | OutputFile *of = &mux->of; | |
| 2215 | 9219 | int nb_av_enc = 0, nb_audio_fs = 0, nb_interleaved = 0; | |
| 2216 | 9219 | int limit_frames = 0, limit_frames_av_enc = 0; | |
| 2217 | |||
| 2218 | #define IS_AV_ENC(ost, type) \ | ||
| 2219 | (ost->enc && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) | ||
| 2220 | #define IS_INTERLEAVED(type) (type != AVMEDIA_TYPE_ATTACHMENT) | ||
| 2221 | |||
| 2222 | 19139 | for (int i = 0; i < oc->nb_streams; i++) { | |
| 2223 | 9920 | OutputStream *ost = of->streams[i]; | |
| 2224 | 9920 | MuxStream *ms = ms_from_ost(ost); | |
| 2225 | 9920 | enum AVMediaType type = ost->type; | |
| 2226 | |||
| 2227 | 9920 | ms->sq_idx_mux = -1; | |
| 2228 | |||
| 2229 | 9920 | nb_interleaved += IS_INTERLEAVED(type); | |
| 2230 | 9920 | nb_av_enc += IS_AV_ENC(ost, type); | |
| 2231 | 11615 | nb_audio_fs += (ost->enc && type == AVMEDIA_TYPE_AUDIO && | |
| 2232 | 1695 | (!(ost->enc->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) || | |
| 2233 | 1319 | (ost->enc->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE))); | |
| 2234 | |||
| 2235 | 9920 | limit_frames |= ms->max_frames < INT64_MAX; | |
| 2236 | 9920 | limit_frames_av_enc |= (ms->max_frames < INT64_MAX) && IS_AV_ENC(ost, type); | |
| 2237 | } | ||
| 2238 | |||
| 2239 | 15204 | if (!((nb_interleaved > 1 && shortest) || | |
| 2240 | 9214 | (nb_interleaved > 0 && limit_frames) || | |
| 2241 | nb_audio_fs)) | ||
| 2242 | 5775 | return 0; | |
| 2243 | |||
| 2244 | /* we use a sync queue before encoding when: | ||
| 2245 | * - 'shortest' is in effect and we have two or more encoded audio/video | ||
| 2246 | * streams | ||
| 2247 | * - at least one encoded audio/video stream is frame-limited, since | ||
| 2248 | * that has similar semantics to 'shortest' | ||
| 2249 | * - at least one audio encoder requires constant frame sizes | ||
| 2250 | * | ||
| 2251 | * Note that encoding sync queues are handled in the scheduler, because | ||
| 2252 | * different encoders run in different threads and need external | ||
| 2253 | * synchronization, while muxer sync queues can be handled inside the muxer | ||
| 2254 | */ | ||
| 2255 | 3444 | if ((shortest && nb_av_enc > 1) || limit_frames_av_enc || nb_audio_fs) { | |
| 2256 | int sq_idx, ret; | ||
| 2257 | |||
| 2258 | 3410 | sq_idx = sch_add_sq_enc(mux->sch, buf_size_us, mux); | |
| 2259 | 3410 | if (sq_idx < 0) | |
| 2260 | ✗ | return sq_idx; | |
| 2261 | |||
| 2262 | 6996 | for (int i = 0; i < oc->nb_streams; i++) { | |
| 2263 | 3586 | OutputStream *ost = of->streams[i]; | |
| 2264 | 3586 | MuxStream *ms = ms_from_ost(ost); | |
| 2265 | 3586 | enum AVMediaType type = ost->type; | |
| 2266 | |||
| 2267 | 3586 | if (!IS_AV_ENC(ost, type)) | |
| 2268 | 7 | continue; | |
| 2269 | |||
| 2270 | 3579 | ret = sch_sq_add_enc(mux->sch, sq_idx, ms->sch_idx_enc, | |
| 2271 | 3573 | shortest || ms->max_frames < INT64_MAX, | |
| 2272 | 3579 | ms->max_frames); | |
| 2273 | 3579 | if (ret < 0) | |
| 2274 | ✗ | return ret; | |
| 2275 | } | ||
| 2276 | } | ||
| 2277 | |||
| 2278 | /* if there are any additional interleaved streams, then ALL the streams | ||
| 2279 | * are also synchronized before sending them to the muxer */ | ||
| 2280 | 3444 | if (nb_interleaved > nb_av_enc) { | |
| 2281 | 38 | mux->sq_mux = sq_alloc(SYNC_QUEUE_PACKETS, buf_size_us, mux); | |
| 2282 | 38 | if (!mux->sq_mux) | |
| 2283 | ✗ | return AVERROR(ENOMEM); | |
| 2284 | |||
| 2285 | 38 | mux->sq_pkt = av_packet_alloc(); | |
| 2286 | 38 | if (!mux->sq_pkt) | |
| 2287 | ✗ | return AVERROR(ENOMEM); | |
| 2288 | |||
| 2289 | 140 | for (int i = 0; i < oc->nb_streams; i++) { | |
| 2290 | 102 | OutputStream *ost = of->streams[i]; | |
| 2291 | 102 | MuxStream *ms = ms_from_ost(ost); | |
| 2292 | 102 | enum AVMediaType type = ost->type; | |
| 2293 | |||
| 2294 | 102 | if (!IS_INTERLEAVED(type)) | |
| 2295 | ✗ | continue; | |
| 2296 | |||
| 2297 | 198 | ms->sq_idx_mux = sq_add_stream(mux->sq_mux, | |
| 2298 | 96 | shortest || ms->max_frames < INT64_MAX); | |
| 2299 | 102 | if (ms->sq_idx_mux < 0) | |
| 2300 | ✗ | return ms->sq_idx_mux; | |
| 2301 | |||
| 2302 | 102 | if (ms->max_frames != INT64_MAX) | |
| 2303 | 86 | sq_limit_frames(mux->sq_mux, ms->sq_idx_mux, ms->max_frames); | |
| 2304 | } | ||
| 2305 | } | ||
| 2306 | |||
| 2307 | #undef IS_AV_ENC | ||
| 2308 | #undef IS_INTERLEAVED | ||
| 2309 | |||
| 2310 | 3444 | return 0; | |
| 2311 | } | ||
| 2312 | |||
| 2313 | 17 | static int of_parse_iamf_audio_element_layers(Muxer *mux, AVStreamGroup *stg, char *ptr) | |
| 2314 | { | ||
| 2315 | 17 | AVIAMFAudioElement *audio_element = stg->params.iamf_audio_element; | |
| 2316 | 17 | AVDictionary *dict = NULL; | |
| 2317 | const char *token; | ||
| 2318 | 17 | int ret = 0; | |
| 2319 | |||
| 2320 | 17 | audio_element->demixing_info = | |
| 2321 | 17 | av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_DEMIXING, 1, NULL); | |
| 2322 | 17 | audio_element->recon_gain_info = | |
| 2323 | 17 | av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN, 1, NULL); | |
| 2324 | |||
| 2325 | 17 | if (!audio_element->demixing_info || | |
| 2326 | 17 | !audio_element->recon_gain_info) | |
| 2327 | ✗ | return AVERROR(ENOMEM); | |
| 2328 | |||
| 2329 | /* process manually set layers and parameters */ | ||
| 2330 | 17 | token = av_strtok(NULL, ",", &ptr); | |
| 2331 | 72 | while (token) { | |
| 2332 | const AVDictionaryEntry *e; | ||
| 2333 | 55 | int demixing = 0, recon_gain = 0; | |
| 2334 | 55 | int layer = 0; | |
| 2335 | |||
| 2336 | 55 | if (ptr) | |
| 2337 | 52 | ptr += strspn(ptr, " \n\t\r"); | |
| 2338 | 55 | if (av_strstart(token, "layer=", &token)) | |
| 2339 | 33 | layer = 1; | |
| 2340 | 22 | else if (av_strstart(token, "demixing=", &token)) | |
| 2341 | 11 | demixing = 1; | |
| 2342 | 11 | else if (av_strstart(token, "recon_gain=", &token)) | |
| 2343 | 11 | recon_gain = 1; | |
| 2344 | |||
| 2345 | 55 | av_dict_free(&dict); | |
| 2346 | 55 | ret = av_dict_parse_string(&dict, token, "=", ":", 0); | |
| 2347 | 55 | if (ret < 0) { | |
| 2348 | ✗ | av_log(mux, AV_LOG_ERROR, "Error parsing audio element specification %s\n", token); | |
| 2349 | ✗ | goto fail; | |
| 2350 | } | ||
| 2351 | |||
| 2352 | 55 | if (layer) { | |
| 2353 | 33 | AVIAMFLayer *audio_layer = av_iamf_audio_element_add_layer(audio_element); | |
| 2354 | 33 | if (!audio_layer) { | |
| 2355 | ✗ | av_log(mux, AV_LOG_ERROR, "Error adding layer to stream group %d\n", stg->index); | |
| 2356 | ✗ | ret = AVERROR(ENOMEM); | |
| 2357 | ✗ | goto fail; | |
| 2358 | } | ||
| 2359 | 33 | av_opt_set_dict(audio_layer, &dict); | |
| 2360 | 22 | } else if (demixing || recon_gain) { | |
| 2361 | 22 | AVIAMFParamDefinition *param = demixing ? audio_element->demixing_info | |
| 2362 | 22 | : audio_element->recon_gain_info; | |
| 2363 | 22 | void *subblock = av_iamf_param_definition_get_subblock(param, 0); | |
| 2364 | |||
| 2365 | 22 | av_opt_set_dict(param, &dict); | |
| 2366 | 22 | av_opt_set_dict(subblock, &dict); | |
| 2367 | } | ||
| 2368 | |||
| 2369 | // make sure that no entries are left in the dict | ||
| 2370 | 55 | e = NULL; | |
| 2371 | 55 | if (e = av_dict_iterate(dict, e)) { | |
| 2372 | ✗ | av_log(mux, AV_LOG_FATAL, "Unknown layer key %s.\n", e->key); | |
| 2373 | ✗ | ret = AVERROR(EINVAL); | |
| 2374 | ✗ | goto fail; | |
| 2375 | } | ||
| 2376 | 55 | token = av_strtok(NULL, ",", &ptr); | |
| 2377 | } | ||
| 2378 | |||
| 2379 | 17 | fail: | |
| 2380 | 17 | av_dict_free(&dict); | |
| 2381 | 17 | if (!ret && !audio_element->nb_layers) { | |
| 2382 | ✗ | av_log(mux, AV_LOG_ERROR, "No layer in audio element specification\n"); | |
| 2383 | ✗ | ret = AVERROR(EINVAL); | |
| 2384 | } | ||
| 2385 | |||
| 2386 | 17 | return ret; | |
| 2387 | } | ||
| 2388 | |||
| 2389 | 15 | static int of_parse_iamf_submixes(Muxer *mux, AVStreamGroup *stg, char *ptr) | |
| 2390 | { | ||
| 2391 | 15 | AVFormatContext *oc = mux->fc; | |
| 2392 | 15 | AVIAMFMixPresentation *mix = stg->params.iamf_mix_presentation; | |
| 2393 | 15 | AVDictionary *dict = NULL; | |
| 2394 | const char *token; | ||
| 2395 | 15 | char *submix_str = NULL; | |
| 2396 | 15 | int ret = 0; | |
| 2397 | |||
| 2398 | /* process manually set submixes */ | ||
| 2399 | 15 | token = av_strtok(NULL, ",", &ptr); | |
| 2400 | 32 | while (token) { | |
| 2401 | 17 | AVIAMFSubmix *submix = NULL; | |
| 2402 | const char *subtoken; | ||
| 2403 | 17 | char *subptr = NULL; | |
| 2404 | |||
| 2405 | 17 | if (ptr) | |
| 2406 | 14 | ptr += strspn(ptr, " \n\t\r"); | |
| 2407 | 17 | if (!av_strstart(token, "submix=", &token)) { | |
| 2408 | ✗ | av_log(mux, AV_LOG_ERROR, "No submix in mix presentation specification \"%s\"\n", token); | |
| 2409 | ✗ | goto fail; | |
| 2410 | } | ||
| 2411 | |||
| 2412 | 17 | submix_str = av_strdup(token); | |
| 2413 | 17 | if (!submix_str) | |
| 2414 | ✗ | goto fail; | |
| 2415 | |||
| 2416 | 17 | submix = av_iamf_mix_presentation_add_submix(mix); | |
| 2417 | 17 | if (!submix) { | |
| 2418 | ✗ | av_log(mux, AV_LOG_ERROR, "Error adding submix to stream group %d\n", stg->index); | |
| 2419 | ✗ | ret = AVERROR(ENOMEM); | |
| 2420 | ✗ | goto fail; | |
| 2421 | } | ||
| 2422 | 17 | submix->output_mix_config = | |
| 2423 | 17 | av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN, 0, NULL); | |
| 2424 | 17 | if (!submix->output_mix_config) { | |
| 2425 | ✗ | ret = AVERROR(ENOMEM); | |
| 2426 | ✗ | goto fail; | |
| 2427 | } | ||
| 2428 | |||
| 2429 | 17 | subptr = NULL; | |
| 2430 | 17 | subtoken = av_strtok(submix_str, "|", &subptr); | |
| 2431 | 88 | while (subtoken) { | |
| 2432 | const AVDictionaryEntry *e; | ||
| 2433 | 71 | int element = 0, layout = 0; | |
| 2434 | |||
| 2435 | 71 | if (subptr) | |
| 2436 | 54 | subptr += strspn(subptr, " \n\t\r"); | |
| 2437 | 71 | if (av_strstart(subtoken, "element=", &subtoken)) | |
| 2438 | 19 | element = 1; | |
| 2439 | 52 | else if (av_strstart(subtoken, "layout=", &subtoken)) | |
| 2440 | 35 | layout = 1; | |
| 2441 | |||
| 2442 | 71 | av_dict_free(&dict); | |
| 2443 | 71 | ret = av_dict_parse_string(&dict, subtoken, "=", ":", 0); | |
| 2444 | 71 | if (ret < 0) { | |
| 2445 | ✗ | av_log(mux, AV_LOG_ERROR, "Error parsing submix specification \"%s\"\n", subtoken); | |
| 2446 | ✗ | goto fail; | |
| 2447 | } | ||
| 2448 | |||
| 2449 | 71 | if (element) { | |
| 2450 | AVIAMFSubmixElement *submix_element; | ||
| 2451 | 19 | char *endptr = NULL; | |
| 2452 | 19 | int64_t idx = -1; | |
| 2453 | |||
| 2454 | 19 | if (e = av_dict_get(dict, "stg", NULL, 0)) | |
| 2455 | 19 | idx = strtoll(e->value, &endptr, 0); | |
| 2456 | 19 | if (!endptr || *endptr || idx < 0 || idx >= oc->nb_stream_groups - 1 || | |
| 2457 | 19 | oc->stream_groups[idx]->type != AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT) { | |
| 2458 | ✗ | av_log(mux, AV_LOG_ERROR, "Invalid or missing stream group index in " | |
| 2459 | "submix element specification \"%s\"\n", subtoken); | ||
| 2460 | ✗ | ret = AVERROR(EINVAL); | |
| 2461 | ✗ | goto fail; | |
| 2462 | } | ||
| 2463 | 19 | submix_element = av_iamf_submix_add_element(submix); | |
| 2464 | 19 | if (!submix_element) { | |
| 2465 | ✗ | av_log(mux, AV_LOG_ERROR, "Error adding element to submix\n"); | |
| 2466 | ✗ | ret = AVERROR(ENOMEM); | |
| 2467 | ✗ | goto fail; | |
| 2468 | } | ||
| 2469 | |||
| 2470 | 19 | submix_element->audio_element_id = oc->stream_groups[idx]->id; | |
| 2471 | |||
| 2472 | 19 | submix_element->element_mix_config = | |
| 2473 | 19 | av_iamf_param_definition_alloc(AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN, 0, NULL); | |
| 2474 | 19 | if (!submix_element->element_mix_config) | |
| 2475 | ✗ | ret = AVERROR(ENOMEM); | |
| 2476 | 19 | av_dict_set(&dict, "stg", NULL, 0); | |
| 2477 | 19 | av_opt_set_dict2(submix_element, &dict, AV_OPT_SEARCH_CHILDREN); | |
| 2478 | 52 | } else if (layout) { | |
| 2479 | 35 | AVIAMFSubmixLayout *submix_layout = av_iamf_submix_add_layout(submix); | |
| 2480 | 35 | if (!submix_layout) { | |
| 2481 | ✗ | av_log(mux, AV_LOG_ERROR, "Error adding layout to submix\n"); | |
| 2482 | ✗ | ret = AVERROR(ENOMEM); | |
| 2483 | ✗ | goto fail; | |
| 2484 | } | ||
| 2485 | 35 | av_opt_set_dict(submix_layout, &dict); | |
| 2486 | } else | ||
| 2487 | 17 | av_opt_set_dict2(submix, &dict, AV_OPT_SEARCH_CHILDREN); | |
| 2488 | |||
| 2489 | 71 | if (ret < 0) { | |
| 2490 | ✗ | goto fail; | |
| 2491 | } | ||
| 2492 | |||
| 2493 | // make sure that no entries are left in the dict | ||
| 2494 | 71 | e = NULL; | |
| 2495 | 71 | while (e = av_dict_iterate(dict, e)) { | |
| 2496 | ✗ | av_log(mux, AV_LOG_FATAL, "Unknown submix key %s.\n", e->key); | |
| 2497 | ✗ | ret = AVERROR(EINVAL); | |
| 2498 | ✗ | goto fail; | |
| 2499 | } | ||
| 2500 | 71 | subtoken = av_strtok(NULL, "|", &subptr); | |
| 2501 | } | ||
| 2502 | 17 | av_freep(&submix_str); | |
| 2503 | |||
| 2504 | 17 | if (!submix->nb_elements) { | |
| 2505 | ✗ | av_log(mux, AV_LOG_ERROR, "No audio elements in submix specification \"%s\"\n", token); | |
| 2506 | ✗ | ret = AVERROR(EINVAL); | |
| 2507 | } | ||
| 2508 | 17 | token = av_strtok(NULL, ",", &ptr); | |
| 2509 | } | ||
| 2510 | |||
| 2511 | 15 | fail: | |
| 2512 | 15 | av_dict_free(&dict); | |
| 2513 | 15 | av_free(submix_str); | |
| 2514 | |||
| 2515 | 15 | return ret; | |
| 2516 | } | ||
| 2517 | |||
| 2518 | 27 | static int of_serialize_options(Muxer *mux, void *obj, AVBPrint *bp) | |
| 2519 | { | ||
| 2520 | char *ptr; | ||
| 2521 | int ret; | ||
| 2522 | |||
| 2523 | 27 | ret = av_opt_serialize(obj, AV_OPT_FLAG_DEPRECATED, | |
| 2524 | AV_OPT_SERIALIZE_SKIP_DEFAULTS | | ||
| 2525 | AV_OPT_SERIALIZE_SEARCH_CHILDREN | | ||
| 2526 | AV_OPT_SERIALIZE_OPT_FLAGS_EXCLUDE, | ||
| 2527 | &ptr, '=', ':'); | ||
| 2528 | 27 | if (ret < 0) { | |
| 2529 | ✗ | av_log(mux, AV_LOG_ERROR, "Failed to serialize group\n"); | |
| 2530 | ✗ | return ret; | |
| 2531 | } | ||
| 2532 | |||
| 2533 | 27 | av_bprintf(bp, "%s", ptr); | |
| 2534 | 27 | ret = strlen(ptr); | |
| 2535 | 27 | av_free(ptr); | |
| 2536 | |||
| 2537 | 27 | return ret; | |
| 2538 | } | ||
| 2539 | |||
| 2540 | #define SERIALIZE(parent, child) do { \ | ||
| 2541 | ret = of_serialize_options(mux, parent->child, bp); \ | ||
| 2542 | if (ret < 0) \ | ||
| 2543 | return ret; \ | ||
| 2544 | } while (0) | ||
| 2545 | |||
| 2546 | #define SERIALIZE_LOOP_SUBBLOCK(obj) do { \ | ||
| 2547 | for (int k = 0; k < obj->nb_subblocks; k++) { \ | ||
| 2548 | ret = of_serialize_options(mux, \ | ||
| 2549 | av_iamf_param_definition_get_subblock(obj, k), bp); \ | ||
| 2550 | if (ret < 0) \ | ||
| 2551 | return ret; \ | ||
| 2552 | } \ | ||
| 2553 | } while (0) | ||
| 2554 | |||
| 2555 | #define SERIALIZE_LOOP(parent, child, suffix, separator) do { \ | ||
| 2556 | for (int j = 0; j < parent->nb_## child ## suffix; j++) { \ | ||
| 2557 | av_bprintf(bp, separator#child "="); \ | ||
| 2558 | SERIALIZE(parent, child ## suffix[j]); \ | ||
| 2559 | } \ | ||
| 2560 | } while (0) | ||
| 2561 | |||
| 2562 | 3 | static int64_t get_stream_group_index_from_id(Muxer *mux, int64_t id) | |
| 2563 | { | ||
| 2564 | 3 | AVFormatContext *oc = mux->fc; | |
| 2565 | |||
| 2566 | 3 | for (unsigned i = 0; i < oc->nb_stream_groups; i++) | |
| 2567 | 3 | if (oc->stream_groups[i]->id == id) | |
| 2568 | 3 | return oc->stream_groups[i]->index; | |
| 2569 | |||
| 2570 | ✗ | return AVERROR(EINVAL); | |
| 2571 | } | ||
| 2572 | |||
| 2573 | 8 | static int of_map_group(Muxer *mux, AVDictionary **dict, AVBPrint *bp, const char *map) | |
| 2574 | { | ||
| 2575 | AVStreamGroup *stg; | ||
| 2576 | int ret, file_idx, stream_idx; | ||
| 2577 | char *ptr; | ||
| 2578 | |||
| 2579 | 8 | file_idx = strtol(map, &ptr, 0); | |
| 2580 | 8 | if (file_idx >= nb_input_files || file_idx < 0 || map == ptr) { | |
| 2581 | ✗ | av_log(mux, AV_LOG_ERROR, "Invalid input file index: %d.\n", file_idx); | |
| 2582 | ✗ | return AVERROR(EINVAL); | |
| 2583 | } | ||
| 2584 | |||
| 2585 | 8 | stream_idx = strtol(*ptr == '=' ? ptr + 1 : ptr, &ptr, 0); | |
| 2586 | 8 | if (*ptr || stream_idx >= input_files[file_idx]->ctx->nb_stream_groups || stream_idx < 0) { | |
| 2587 | ✗ | av_log(mux, AV_LOG_ERROR, "Invalid input stream group index: %d.\n", stream_idx); | |
| 2588 | ✗ | return AVERROR(EINVAL); | |
| 2589 | } | ||
| 2590 | |||
| 2591 | 8 | stg = input_files[file_idx]->ctx->stream_groups[stream_idx]; | |
| 2592 | 8 | ret = of_serialize_options(mux, stg, bp); | |
| 2593 | 8 | if (ret < 0) | |
| 2594 | ✗ | return ret; | |
| 2595 | |||
| 2596 | 8 | ret = av_dict_parse_string(dict, bp->str, "=", ":", 0); | |
| 2597 | 8 | if (ret < 0) | |
| 2598 | ✗ | av_log(mux, AV_LOG_ERROR, "Error parsing mapped group specification %s\n", ptr); | |
| 2599 | 8 | av_dict_set_int(dict, "type", stg->type, 0); | |
| 2600 | |||
| 2601 | 8 | av_bprint_clear(bp); | |
| 2602 | 8 | switch(stg->type) { | |
| 2603 | 3 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: { | |
| 2604 | 3 | AVIAMFAudioElement *audio_element = stg->params.iamf_audio_element; | |
| 2605 | |||
| 2606 | 3 | if (audio_element->demixing_info) { | |
| 2607 | 1 | AVIAMFParamDefinition *demixing_info = audio_element->demixing_info; | |
| 2608 | 1 | av_bprintf(bp, ",demixing="); | |
| 2609 | 1 | SERIALIZE(audio_element, demixing_info); | |
| 2610 | 1 | if (ret && demixing_info->nb_subblocks) | |
| 2611 | 1 | av_bprintf(bp, ":"); | |
| 2612 | 2 | SERIALIZE_LOOP_SUBBLOCK(demixing_info); | |
| 2613 | } | ||
| 2614 | 3 | if (audio_element->recon_gain_info) { | |
| 2615 | 1 | AVIAMFParamDefinition *recon_gain_info = audio_element->recon_gain_info; | |
| 2616 | 1 | av_bprintf(bp, ",recon_gain="); | |
| 2617 | 1 | SERIALIZE(audio_element, recon_gain_info); | |
| 2618 | 1 | if (ret && recon_gain_info->nb_subblocks) | |
| 2619 | 1 | av_bprintf(bp, ":"); | |
| 2620 | 2 | SERIALIZE_LOOP_SUBBLOCK(recon_gain_info); | |
| 2621 | } | ||
| 2622 | 7 | SERIALIZE_LOOP(audio_element, layer, s, ","); | |
| 2623 | 3 | break; | |
| 2624 | } | ||
| 2625 | 3 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: { | |
| 2626 | 3 | AVIAMFMixPresentation *mix = stg->params.iamf_mix_presentation; | |
| 2627 | |||
| 2628 | 6 | for (int i = 0; i < mix->nb_submixes; i++) { | |
| 2629 | 3 | AVIAMFSubmix *submix = mix->submixes[i]; | |
| 2630 | 3 | AVIAMFParamDefinition *output_mix_config = submix->output_mix_config; | |
| 2631 | |||
| 2632 | 3 | av_bprintf(bp, ",submix="); | |
| 2633 | 3 | SERIALIZE(mix, submixes[i]); | |
| 2634 | 3 | if (ret && output_mix_config->nb_subblocks) | |
| 2635 | ✗ | av_bprintf(bp, ":"); | |
| 2636 | 3 | SERIALIZE_LOOP_SUBBLOCK(output_mix_config); | |
| 2637 | 6 | for (int j = 0; j < submix->nb_elements; j++) { | |
| 2638 | 3 | AVIAMFSubmixElement *element = submix->elements[j]; | |
| 2639 | 3 | AVIAMFParamDefinition *element_mix_config = element->element_mix_config; | |
| 2640 | 3 | int64_t id = get_stream_group_index_from_id(mux, element->audio_element_id); | |
| 2641 | |||
| 2642 | 3 | if (id < 0) { | |
| 2643 | ✗ | av_log(mux, AV_LOG_ERROR, "Invalid or missing stream group index in" | |
| 2644 | "submix element"); | ||
| 2645 | ✗ | return id; | |
| 2646 | } | ||
| 2647 | |||
| 2648 | 3 | av_bprintf(bp, "|element="); | |
| 2649 | 3 | SERIALIZE(submix, elements[j]); | |
| 2650 | 3 | if (ret && element_mix_config->nb_subblocks) | |
| 2651 | ✗ | av_bprintf(bp, ":"); | |
| 2652 | 3 | SERIALIZE_LOOP_SUBBLOCK(element_mix_config); | |
| 2653 | 3 | if (ret) | |
| 2654 | 3 | av_bprintf(bp, ":"); | |
| 2655 | 3 | av_bprintf(bp, "stg=%"PRId64, id); | |
| 2656 | } | ||
| 2657 | 8 | SERIALIZE_LOOP(submix, layout, s, "|"); | |
| 2658 | } | ||
| 2659 | 3 | break; | |
| 2660 | } | ||
| 2661 | 2 | case AV_STREAM_GROUP_PARAMS_LCEVC: | |
| 2662 | case AV_STREAM_GROUP_PARAMS_TREF: | ||
| 2663 | case AV_STREAM_GROUP_PARAMS_DOLBY_VISION: | ||
| 2664 | case AV_STREAM_GROUP_PARAMS_GAIN_MAP: | ||
| 2665 | 2 | break; | |
| 2666 | ✗ | default: | |
| 2667 | ✗ | av_log(mux, AV_LOG_ERROR, "Unsupported mapped group type %d.\n", stg->type); | |
| 2668 | ✗ | ret = AVERROR(EINVAL); | |
| 2669 | ✗ | break; | |
| 2670 | } | ||
| 2671 | 8 | return 0; | |
| 2672 | } | ||
| 2673 | |||
| 2674 | 35 | static int of_parse_group_token(Muxer *mux, const char *token, char *ptr) | |
| 2675 | { | ||
| 2676 | 35 | AVFormatContext *oc = mux->fc; | |
| 2677 | AVStreamGroup *stg; | ||
| 2678 | 35 | AVDictionary *dict = NULL, *tmp = NULL; | |
| 2679 | 35 | char *mapped_string = NULL; | |
| 2680 | const AVDictionaryEntry *e; | ||
| 2681 | 35 | const AVOption opts[] = { | |
| 2682 | { "type", "Set group type", offsetof(AVStreamGroup, type), AV_OPT_TYPE_INT, | ||
| 2683 | { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "type" }, | ||
| 2684 | { "iamf_audio_element", NULL, 0, AV_OPT_TYPE_CONST, | ||
| 2685 | { .i64 = AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT }, .unit = "type" }, | ||
| 2686 | { "iamf_mix_presentation", NULL, 0, AV_OPT_TYPE_CONST, | ||
| 2687 | { .i64 = AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION }, .unit = "type" }, | ||
| 2688 | { "lcevc", NULL, 0, AV_OPT_TYPE_CONST, | ||
| 2689 | { .i64 = AV_STREAM_GROUP_PARAMS_LCEVC }, .unit = "type" }, | ||
| 2690 | { "tref", NULL, 0, AV_OPT_TYPE_CONST, | ||
| 2691 | { .i64 = AV_STREAM_GROUP_PARAMS_TREF }, .unit = "type" }, | ||
| 2692 | { "gain_map", NULL, 0, AV_OPT_TYPE_CONST, | ||
| 2693 | { .i64 = AV_STREAM_GROUP_PARAMS_GAIN_MAP }, .unit = "type" }, | ||
| 2694 | { NULL }, | ||
| 2695 | }; | ||
| 2696 | 35 | const AVClass class = { | |
| 2697 | .class_name = "StreamGroupType", | ||
| 2698 | .item_name = av_default_item_name, | ||
| 2699 | .option = opts, | ||
| 2700 | .version = LIBAVUTIL_VERSION_INT, | ||
| 2701 | }; | ||
| 2702 | 35 | const AVClass *pclass = &class; | |
| 2703 | int type, ret; | ||
| 2704 | |||
| 2705 | 35 | ret = av_dict_parse_string(&dict, token, "=", ":", AV_DICT_MULTIKEY); | |
| 2706 | 35 | if (ret < 0) { | |
| 2707 | ✗ | av_log(mux, AV_LOG_ERROR, "Error parsing group specification %s\n", token); | |
| 2708 | ✗ | return ret; | |
| 2709 | } | ||
| 2710 | |||
| 2711 | 35 | av_dict_copy(&tmp, dict, 0); | |
| 2712 | 35 | e = av_dict_get(dict, "map", NULL, 0); | |
| 2713 | 35 | if (e) { | |
| 2714 | AVBPrint bp; | ||
| 2715 | |||
| 2716 | 8 | if (ptr) { | |
| 2717 | ✗ | av_log(mux, AV_LOG_ERROR, "Unexpected extra parameters when mapping a" | |
| 2718 | " stream group\n"); | ||
| 2719 | ✗ | ret = AVERROR(EINVAL); | |
| 2720 | ✗ | goto end; | |
| 2721 | } | ||
| 2722 | |||
| 2723 | 8 | av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC); | |
| 2724 | 8 | ret = of_map_group(mux, &tmp, &bp, e->value); | |
| 2725 | 8 | if (ret < 0) { | |
| 2726 | ✗ | av_bprint_finalize(&bp, NULL); | |
| 2727 | ✗ | goto end; | |
| 2728 | } | ||
| 2729 | |||
| 2730 | 8 | av_bprint_finalize(&bp, &mapped_string); | |
| 2731 | 8 | ptr = mapped_string; | |
| 2732 | } | ||
| 2733 | |||
| 2734 | // "type" is not a user settable AVOption in AVStreamGroup, so handle it here | ||
| 2735 | 35 | e = av_dict_get(tmp, "type", NULL, 0); | |
| 2736 | 35 | if (!e) { | |
| 2737 | ✗ | av_log(mux, AV_LOG_ERROR, "No type specified for Stream Group in \"%s\"\n", token); | |
| 2738 | ✗ | ret = AVERROR(EINVAL); | |
| 2739 | ✗ | goto end; | |
| 2740 | } | ||
| 2741 | |||
| 2742 | 35 | ret = av_opt_eval_int(&pclass, opts, e->value, &type); | |
| 2743 | 35 | if (!ret && type == AV_STREAM_GROUP_PARAMS_NONE) | |
| 2744 | ✗ | ret = AVERROR(EINVAL); | |
| 2745 | 35 | if (ret < 0) { | |
| 2746 | ✗ | av_log(mux, AV_LOG_ERROR, "Invalid group type \"%s\"\n", e->value); | |
| 2747 | ✗ | goto end; | |
| 2748 | } | ||
| 2749 | |||
| 2750 | 35 | stg = avformat_stream_group_create(oc, type, &tmp); | |
| 2751 | 35 | if (!stg) { | |
| 2752 | ✗ | ret = AVERROR(ENOMEM); | |
| 2753 | ✗ | goto end; | |
| 2754 | } | ||
| 2755 | |||
| 2756 | 35 | e = NULL; | |
| 2757 | 124 | while (e = av_dict_get(dict, "st", e, 0)) { | |
| 2758 | char *endptr; | ||
| 2759 | 89 | int64_t idx = strtoll(e->value, &endptr, 0); | |
| 2760 | 89 | if (*endptr || idx < 0 || idx >= oc->nb_streams) { | |
| 2761 | ✗ | av_log(mux, AV_LOG_ERROR, "Invalid stream index %"PRId64"\n", idx); | |
| 2762 | ✗ | ret = AVERROR(EINVAL); | |
| 2763 | ✗ | goto end; | |
| 2764 | } | ||
| 2765 | 89 | ret = avformat_stream_group_add_stream(stg, oc->streams[idx]); | |
| 2766 | 89 | if (ret < 0) | |
| 2767 | ✗ | goto end; | |
| 2768 | 89 | OutputStream *ost = mux->of.streams[idx]; | |
| 2769 | 89 | if (ost->enc && (type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT || | |
| 2770 | 2 | type == AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION)) | |
| 2771 | 59 | ost->enc->flags2 |= AV_CODEC_FLAG2_FIXED_FRAME_SIZE; | |
| 2772 | } | ||
| 2773 | 51 | while (e = av_dict_get(dict, "stg", e, 0)) { | |
| 2774 | char *endptr; | ||
| 2775 | 16 | int64_t idx = strtoll(e->value, &endptr, 0); | |
| 2776 | 16 | if (*endptr || idx < 0 || idx >= oc->nb_stream_groups - 1) { | |
| 2777 | ✗ | av_log(mux, AV_LOG_ERROR, "Invalid stream group index %"PRId64"\n", idx); | |
| 2778 | ✗ | ret = AVERROR(EINVAL); | |
| 2779 | ✗ | goto end; | |
| 2780 | } | ||
| 2781 | 98 | for (unsigned i = 0; i < oc->stream_groups[idx]->nb_entries; i++) { | |
| 2782 | 82 | ret = avformat_stream_group_add_stream(stg, oc->stream_groups[idx]->entries[i]->stream); | |
| 2783 | 82 | if (ret < 0) | |
| 2784 | ✗ | goto end; | |
| 2785 | } | ||
| 2786 | } | ||
| 2787 | |||
| 2788 | 35 | switch(type) { | |
| 2789 | 17 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: | |
| 2790 | 17 | ret = of_parse_iamf_audio_element_layers(mux, stg, ptr); | |
| 2791 | 17 | break; | |
| 2792 | 15 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: | |
| 2793 | 15 | ret = of_parse_iamf_submixes(mux, stg, ptr); | |
| 2794 | 15 | break; | |
| 2795 | 3 | default: | |
| 2796 | 3 | break; | |
| 2797 | } | ||
| 2798 | |||
| 2799 | 35 | if (ret < 0) | |
| 2800 | ✗ | goto end; | |
| 2801 | |||
| 2802 | // make sure that nothing but "st" and "stg" entries are left in the dict | ||
| 2803 | 35 | e = NULL; | |
| 2804 | 35 | av_dict_set(&tmp, "map", NULL, 0); | |
| 2805 | 35 | av_dict_set(&tmp, "type", NULL, 0); | |
| 2806 | 70 | while (e = av_dict_iterate(tmp, e)) { | |
| 2807 | 35 | if (!strcmp(e->key, "st") || !strcmp(e->key, "stg")) | |
| 2808 | 35 | continue; | |
| 2809 | |||
| 2810 | ✗ | av_log(mux, AV_LOG_FATAL, "Unknown group key %s.\n", e->key); | |
| 2811 | ✗ | ret = AVERROR(EINVAL); | |
| 2812 | ✗ | goto end; | |
| 2813 | } | ||
| 2814 | |||
| 2815 | 35 | ret = 0; | |
| 2816 | 35 | end: | |
| 2817 | 35 | av_free(mapped_string); | |
| 2818 | 35 | av_dict_free(&dict); | |
| 2819 | 35 | av_dict_free(&tmp); | |
| 2820 | |||
| 2821 | 35 | return ret; | |
| 2822 | } | ||
| 2823 | |||
| 2824 | 9219 | static int of_add_groups(Muxer *mux, const OptionsContext *o) | |
| 2825 | { | ||
| 2826 | /* process manually set groups */ | ||
| 2827 | 9254 | for (int i = 0; i < o->stream_groups.nb_opt; i++) { | |
| 2828 | const char *token; | ||
| 2829 | 35 | char *str, *ptr = NULL; | |
| 2830 | 35 | int ret = 0; | |
| 2831 | |||
| 2832 | 35 | str = av_strdup(o->stream_groups.opt[i].u.str); | |
| 2833 | 35 | if (!str) | |
| 2834 | ✗ | return ret; | |
| 2835 | |||
| 2836 | 35 | token = av_strtok(str, ",", &ptr); | |
| 2837 | 35 | if (token) { | |
| 2838 | 35 | if (ptr) | |
| 2839 | 26 | ptr += strspn(ptr, " \n\t\r"); | |
| 2840 | 35 | ret = of_parse_group_token(mux, token, ptr); | |
| 2841 | } | ||
| 2842 | |||
| 2843 | 35 | av_free(str); | |
| 2844 | 35 | if (ret < 0) | |
| 2845 | ✗ | return ret; | |
| 2846 | } | ||
| 2847 | |||
| 2848 | 9219 | return 0; | |
| 2849 | } | ||
| 2850 | |||
| 2851 | 9219 | static int of_add_programs(Muxer *mux, const OptionsContext *o) | |
| 2852 | { | ||
| 2853 | 9219 | AVFormatContext *oc = mux->fc; | |
| 2854 | /* process manually set programs */ | ||
| 2855 | 9219 | for (int i = 0; i < o->program.nb_opt; i++) { | |
| 2856 | ✗ | AVDictionary *dict = NULL; | |
| 2857 | const AVDictionaryEntry *e; | ||
| 2858 | AVProgram *program; | ||
| 2859 | ✗ | int ret, progid = i + 1; | |
| 2860 | |||
| 2861 | ✗ | ret = av_dict_parse_string(&dict, o->program.opt[i].u.str, "=", ":", | |
| 2862 | AV_DICT_MULTIKEY); | ||
| 2863 | ✗ | if (ret < 0) { | |
| 2864 | ✗ | av_log(mux, AV_LOG_ERROR, "Error parsing program specification %s\n", | |
| 2865 | ✗ | o->program.opt[i].u.str); | |
| 2866 | ✗ | return ret; | |
| 2867 | } | ||
| 2868 | |||
| 2869 | ✗ | e = av_dict_get(dict, "program_num", NULL, 0); | |
| 2870 | ✗ | if (e) { | |
| 2871 | ✗ | progid = strtol(e->value, NULL, 0); | |
| 2872 | ✗ | av_dict_set(&dict, e->key, NULL, 0); | |
| 2873 | } | ||
| 2874 | |||
| 2875 | ✗ | program = av_new_program(oc, progid); | |
| 2876 | ✗ | if (!program) { | |
| 2877 | ✗ | ret = AVERROR(ENOMEM); | |
| 2878 | ✗ | goto fail; | |
| 2879 | } | ||
| 2880 | |||
| 2881 | ✗ | e = av_dict_get(dict, "title", NULL, 0); | |
| 2882 | ✗ | if (e) { | |
| 2883 | ✗ | av_dict_set(&program->metadata, e->key, e->value, 0); | |
| 2884 | ✗ | av_dict_set(&dict, e->key, NULL, 0); | |
| 2885 | } | ||
| 2886 | |||
| 2887 | ✗ | e = NULL; | |
| 2888 | ✗ | while (e = av_dict_get(dict, "st", e, 0)) { | |
| 2889 | ✗ | int st_num = strtol(e->value, NULL, 0); | |
| 2890 | ✗ | av_program_add_stream_index(oc, progid, st_num); | |
| 2891 | } | ||
| 2892 | |||
| 2893 | // make sure that nothing but "st" entries are left in the dict | ||
| 2894 | ✗ | e = NULL; | |
| 2895 | ✗ | while (e = av_dict_iterate(dict, e)) { | |
| 2896 | ✗ | if (!strcmp(e->key, "st")) | |
| 2897 | ✗ | continue; | |
| 2898 | |||
| 2899 | ✗ | av_log(mux, AV_LOG_FATAL, "Unknown program key %s.\n", e->key); | |
| 2900 | ✗ | ret = AVERROR(EINVAL); | |
| 2901 | ✗ | goto fail; | |
| 2902 | } | ||
| 2903 | |||
| 2904 | ✗ | fail: | |
| 2905 | ✗ | av_dict_free(&dict); | |
| 2906 | ✗ | if (ret < 0) | |
| 2907 | ✗ | return ret; | |
| 2908 | } | ||
| 2909 | |||
| 2910 | 9219 | return 0; | |
| 2911 | } | ||
| 2912 | |||
| 2913 | /** | ||
| 2914 | * Parse a metadata specifier passed as 'arg' parameter. | ||
| 2915 | * @param arg metadata string to parse | ||
| 2916 | * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram) | ||
| 2917 | * @param index for type c/p, chapter/program index is written here | ||
| 2918 | * @param stream_spec for type s, the stream specifier is written here | ||
| 2919 | */ | ||
| 2920 | 460 | static int parse_meta_type(void *logctx, const char *arg, | |
| 2921 | char *type, int *index, const char **stream_spec) | ||
| 2922 | { | ||
| 2923 | 460 | if (*arg) { | |
| 2924 | 192 | *type = *arg; | |
| 2925 | 192 | switch (*arg) { | |
| 2926 | 27 | case 'g': | |
| 2927 | 27 | break; | |
| 2928 | 152 | case 's': | |
| 2929 | 152 | if (*(++arg) && *arg != ':') { | |
| 2930 | ✗ | av_log(logctx, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg); | |
| 2931 | ✗ | return AVERROR(EINVAL); | |
| 2932 | } | ||
| 2933 | 152 | *stream_spec = *arg == ':' ? arg + 1 : ""; | |
| 2934 | 152 | break; | |
| 2935 | 12 | case 'c': | |
| 2936 | case 'p': | ||
| 2937 | 12 | if (*(++arg) == ':') | |
| 2938 | 11 | *index = strtol(++arg, NULL, 0); | |
| 2939 | 12 | break; | |
| 2940 | 1 | default: | |
| 2941 | 1 | av_log(logctx, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg); | |
| 2942 | 1 | return AVERROR(EINVAL); | |
| 2943 | } | ||
| 2944 | } else | ||
| 2945 | 268 | *type = 'g'; | |
| 2946 | |||
| 2947 | 459 | return 0; | |
| 2948 | } | ||
| 2949 | |||
| 2950 | 9219 | static int of_add_metadata(OutputFile *of, AVFormatContext *oc, | |
| 2951 | const OptionsContext *o) | ||
| 2952 | { | ||
| 2953 | 9485 | for (int i = 0; i < o->metadata.nb_opt; i++) { | |
| 2954 | AVDictionary **m; | ||
| 2955 | char type, *val; | ||
| 2956 | const char *stream_spec; | ||
| 2957 | 266 | int index = 0, ret = 0; | |
| 2958 | |||
| 2959 | 266 | val = strchr(o->metadata.opt[i].u.str, '='); | |
| 2960 | 266 | if (!val) { | |
| 2961 | ✗ | av_log(of, AV_LOG_FATAL, "No '=' character in metadata string %s.\n", | |
| 2962 | ✗ | o->metadata.opt[i].u.str); | |
| 2963 | ✗ | return AVERROR(EINVAL); | |
| 2964 | } | ||
| 2965 | 266 | *val++ = 0; | |
| 2966 | |||
| 2967 | 266 | ret = parse_meta_type(of, o->metadata.opt[i].specifier, &type, &index, &stream_spec); | |
| 2968 | 266 | if (ret < 0) | |
| 2969 | ✗ | return ret; | |
| 2970 | |||
| 2971 | 266 | if (type == 's') { | |
| 2972 | 274 | for (int j = 0; j < oc->nb_streams; j++) { | |
| 2973 | 226 | if ((ret = check_stream_specifier(oc, oc->streams[j], stream_spec)) > 0) { | |
| 2974 | 47 | av_dict_set(&oc->streams[j]->metadata, o->metadata.opt[i].u.str, *val ? val : NULL, 0); | |
| 2975 | 179 | } else if (ret < 0) | |
| 2976 | ✗ | return ret; | |
| 2977 | } | ||
| 2978 | } else { | ||
| 2979 | 218 | switch (type) { | |
| 2980 | 213 | case 'g': | |
| 2981 | 213 | m = &oc->metadata; | |
| 2982 | 213 | break; | |
| 2983 | 5 | case 'c': | |
| 2984 | 5 | if (index < 0 || index >= oc->nb_chapters) { | |
| 2985 | ✗ | av_log(of, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index); | |
| 2986 | ✗ | return AVERROR(EINVAL); | |
| 2987 | } | ||
| 2988 | 5 | m = &oc->chapters[index]->metadata; | |
| 2989 | 5 | break; | |
| 2990 | ✗ | case 'p': | |
| 2991 | ✗ | if (index < 0 || index >= oc->nb_programs) { | |
| 2992 | ✗ | av_log(of, AV_LOG_FATAL, "Invalid program index %d in metadata specifier.\n", index); | |
| 2993 | ✗ | return AVERROR(EINVAL); | |
| 2994 | } | ||
| 2995 | ✗ | m = &oc->programs[index]->metadata; | |
| 2996 | ✗ | break; | |
| 2997 | ✗ | default: | |
| 2998 | ✗ | av_log(of, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata.opt[i].specifier); | |
| 2999 | ✗ | return AVERROR(EINVAL); | |
| 3000 | } | ||
| 3001 | 218 | av_dict_set(m, o->metadata.opt[i].u.str, *val ? val : NULL, 0); | |
| 3002 | } | ||
| 3003 | } | ||
| 3004 | |||
| 3005 | 9219 | return 0; | |
| 3006 | } | ||
| 3007 | |||
| 3008 | 42 | static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os, | |
| 3009 | int copy_metadata) | ||
| 3010 | { | ||
| 3011 | 42 | AVFormatContext *is = ifile->ctx; | |
| 3012 | AVChapter **tmp; | ||
| 3013 | |||
| 3014 | 42 | tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters, sizeof(*os->chapters)); | |
| 3015 | 42 | if (!tmp) | |
| 3016 | ✗ | return AVERROR(ENOMEM); | |
| 3017 | 42 | os->chapters = tmp; | |
| 3018 | |||
| 3019 | 124 | for (int i = 0; i < is->nb_chapters; i++) { | |
| 3020 | 86 | AVChapter *in_ch = is->chapters[i], *out_ch; | |
| 3021 | 86 | int64_t start_time = (ofile->start_time == AV_NOPTS_VALUE) ? 0 : ofile->start_time; | |
| 3022 | 86 | int64_t ts_off = av_rescale_q(start_time - ifile->ts_offset, | |
| 3023 | 86 | AV_TIME_BASE_Q, in_ch->time_base); | |
| 3024 | 86 | int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX : | |
| 3025 | 27 | av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base); | |
| 3026 | |||
| 3027 | |||
| 3028 | 86 | if (in_ch->end < ts_off) | |
| 3029 | ✗ | continue; | |
| 3030 | 86 | if (rt != INT64_MAX && in_ch->start > rt + ts_off) | |
| 3031 | 4 | break; | |
| 3032 | |||
| 3033 | 82 | out_ch = av_mallocz(sizeof(AVChapter)); | |
| 3034 | 82 | if (!out_ch) | |
| 3035 | ✗ | return AVERROR(ENOMEM); | |
| 3036 | |||
| 3037 | 82 | out_ch->id = in_ch->id; | |
| 3038 | 82 | out_ch->time_base = in_ch->time_base; | |
| 3039 | 82 | out_ch->start = FFMAX(0, in_ch->start - ts_off); | |
| 3040 | 82 | out_ch->end = FFMIN(rt, in_ch->end - ts_off); | |
| 3041 | |||
| 3042 | 82 | if (copy_metadata) | |
| 3043 | 82 | av_dict_copy(&out_ch->metadata, in_ch->metadata, 0); | |
| 3044 | |||
| 3045 | 82 | os->chapters[os->nb_chapters++] = out_ch; | |
| 3046 | } | ||
| 3047 | 42 | return 0; | |
| 3048 | } | ||
| 3049 | |||
| 3050 | 28 | static int copy_metadata(Muxer *mux, AVFormatContext *ic, | |
| 3051 | const char *outspec, const char *inspec, | ||
| 3052 | int *metadata_global_manual, int *metadata_streams_manual, | ||
| 3053 | int *metadata_chapters_manual) | ||
| 3054 | { | ||
| 3055 | 28 | AVFormatContext *oc = mux->fc; | |
| 3056 | 28 | AVDictionary **meta_in = NULL; | |
| 3057 | 28 | AVDictionary **meta_out = NULL; | |
| 3058 | 28 | int i, ret = 0; | |
| 3059 | char type_in, type_out; | ||
| 3060 | 28 | const char *istream_spec = NULL, *ostream_spec = NULL; | |
| 3061 | 28 | int idx_in = 0, idx_out = 0; | |
| 3062 | |||
| 3063 | 28 | ret = parse_meta_type(mux, inspec, &type_in, &idx_in, &istream_spec); | |
| 3064 | 28 | if (ret >= 0) | |
| 3065 | 28 | ret = parse_meta_type(mux, outspec, &type_out, &idx_out, &ostream_spec); | |
| 3066 | 28 | if (ret < 0) | |
| 3067 | ✗ | return ret; | |
| 3068 | |||
| 3069 | 28 | if (type_in == 'g' || type_out == 'g' || (!*outspec && !ic)) | |
| 3070 | 25 | *metadata_global_manual = 1; | |
| 3071 | 28 | if (type_in == 's' || type_out == 's' || (!*outspec && !ic)) | |
| 3072 | 22 | *metadata_streams_manual = 1; | |
| 3073 | 28 | if (type_in == 'c' || type_out == 'c' || (!*outspec && !ic)) | |
| 3074 | 18 | *metadata_chapters_manual = 1; | |
| 3075 | |||
| 3076 | /* ic is NULL when just disabling automatic mappings */ | ||
| 3077 | 28 | if (!ic) | |
| 3078 | 20 | return 0; | |
| 3079 | |||
| 3080 | #define METADATA_CHECK_INDEX(index, nb_elems, desc)\ | ||
| 3081 | if ((index) < 0 || (index) >= (nb_elems)) {\ | ||
| 3082 | av_log(mux, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\ | ||
| 3083 | (desc), (index));\ | ||
| 3084 | return AVERROR(EINVAL);\ | ||
| 3085 | } | ||
| 3086 | |||
| 3087 | #define SET_DICT(type, meta, context, index)\ | ||
| 3088 | switch (type) {\ | ||
| 3089 | case 'g':\ | ||
| 3090 | meta = &context->metadata;\ | ||
| 3091 | break;\ | ||
| 3092 | case 'c':\ | ||
| 3093 | METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\ | ||
| 3094 | meta = &context->chapters[index]->metadata;\ | ||
| 3095 | break;\ | ||
| 3096 | case 'p':\ | ||
| 3097 | METADATA_CHECK_INDEX(index, context->nb_programs, "program")\ | ||
| 3098 | meta = &context->programs[index]->metadata;\ | ||
| 3099 | break;\ | ||
| 3100 | case 's':\ | ||
| 3101 | break; /* handled separately below */ \ | ||
| 3102 | default: av_assert0(0);\ | ||
| 3103 | }\ | ||
| 3104 | |||
| 3105 | 8 | SET_DICT(type_in, meta_in, ic, idx_in); | |
| 3106 | 8 | SET_DICT(type_out, meta_out, oc, idx_out); | |
| 3107 | |||
| 3108 | /* for input streams choose first matching stream */ | ||
| 3109 | 8 | if (type_in == 's') { | |
| 3110 | 4 | for (i = 0; i < ic->nb_streams; i++) { | |
| 3111 | 4 | if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) { | |
| 3112 | 3 | meta_in = &ic->streams[i]->metadata; | |
| 3113 | 3 | break; | |
| 3114 | 1 | } else if (ret < 0) | |
| 3115 | ✗ | return ret; | |
| 3116 | } | ||
| 3117 | 3 | if (!meta_in) { | |
| 3118 | ✗ | av_log(mux, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec); | |
| 3119 | ✗ | return AVERROR(EINVAL); | |
| 3120 | } | ||
| 3121 | } | ||
| 3122 | |||
| 3123 | 8 | if (type_out == 's') { | |
| 3124 | 13 | for (i = 0; i < oc->nb_streams; i++) { | |
| 3125 | 9 | if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) { | |
| 3126 | 4 | meta_out = &oc->streams[i]->metadata; | |
| 3127 | 4 | av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE); | |
| 3128 | 5 | } else if (ret < 0) | |
| 3129 | ✗ | return ret; | |
| 3130 | } | ||
| 3131 | } else | ||
| 3132 | 4 | av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE); | |
| 3133 | |||
| 3134 | 8 | return 0; | |
| 3135 | } | ||
| 3136 | |||
| 3137 | #define REENC_MASK(type) (1u << (type)) | ||
| 3138 | #define REENC_AUDIO_ONLY REENC_MASK(AVMEDIA_TYPE_AUDIO) | ||
| 3139 | #define REENC_VIDEO_ONLY REENC_MASK(AVMEDIA_TYPE_VIDEO) | ||
| 3140 | #define REENC_ANY (REENC_AUDIO_ONLY | REENC_VIDEO_ONLY) | ||
| 3141 | |||
| 3142 | static const struct { | ||
| 3143 | const char *key; | ||
| 3144 | int flags; | ||
| 3145 | unsigned reenc_mask; /* 0 = always delete; non-zero = bit-mask of | ||
| 3146 | * REENC_* values whose re-encoding triggers | ||
| 3147 | * deletion of this key */ | ||
| 3148 | } reenc_delete_keys[] = { | ||
| 3149 | // Note: "encoder" is intentionally absent — set_encoder_id() stamps it | ||
| 3150 | // after copy_meta() runs, so it always reflects the current encoder. | ||
| 3151 | |||
| 3152 | // iTunes gapless playback: encoder-specific sample counts and padding; | ||
| 3153 | // gapless_playback is the M4A/MOV equivalent of iTunPGAP | ||
| 3154 | { "iTunPGAP", 0, REENC_AUDIO_ONLY }, | ||
| 3155 | { "comment-iTunPGAP-eng", 0, REENC_AUDIO_ONLY }, | ||
| 3156 | { "iTunSMPB", 0, REENC_AUDIO_ONLY }, | ||
| 3157 | { "comment-iTunSMPB-eng", 0, REENC_AUDIO_ONLY }, | ||
| 3158 | { "gapless_playback", 0, REENC_AUDIO_ONLY }, | ||
| 3159 | // iTunes Sound Check: peak amplitude computed from the original waveform | ||
| 3160 | { "iTunNORM", 0, REENC_AUDIO_ONLY }, | ||
| 3161 | { "comment-iTunNORM-eng", 0, REENC_AUDIO_ONLY }, | ||
| 3162 | // encoding provenance: describe the original encoder, not the new one | ||
| 3163 | { "encoded_by", 0, REENC_ANY }, | ||
| 3164 | { "encoding_tool", 0, REENC_ANY }, | ||
| 3165 | // MOV/MP4 stream: vendor 4CC identifies the original encoder | ||
| 3166 | { "vendor_id", 0, REENC_ANY }, | ||
| 3167 | // Matroska stream stats written by mkvmerge; all are invalidated by re-encoding | ||
| 3168 | { "BPS", AV_DICT_IGNORE_SUFFIX, REENC_ANY }, | ||
| 3169 | { "DURATION", AV_DICT_IGNORE_SUFFIX, REENC_ANY }, | ||
| 3170 | { "NUMBER_OF_BYTES", AV_DICT_IGNORE_SUFFIX, REENC_ANY }, | ||
| 3171 | { "NUMBER_OF_FRAMES", AV_DICT_IGNORE_SUFFIX, REENC_ANY }, | ||
| 3172 | { "_STATISTICS_TAGS", AV_DICT_IGNORE_SUFFIX, REENC_ANY }, | ||
| 3173 | { "_STATISTICS_WRITING_APP", AV_DICT_IGNORE_SUFFIX, REENC_ANY }, | ||
| 3174 | { "_STATISTICS_WRITING_DATE_UTC", AV_DICT_IGNORE_SUFFIX, REENC_ANY }, | ||
| 3175 | |||
| 3176 | // MOV/MP4: source container brand written by the demuxer; muxer ignores | ||
| 3177 | // these keys and writes its own ftyp, so they always describe the source file | ||
| 3178 | { "major_brand", 0, 0 }, | ||
| 3179 | { "minor_version", 0, 0 }, | ||
| 3180 | { "compatible_brands", 0, 0 }, | ||
| 3181 | { NULL } | ||
| 3182 | }; | ||
| 3183 | |||
| 3184 | 127 | static int meta_spec_matches(void *log_ctx, AVFormatContext *oc, AVStream *st, | |
| 3185 | const char *spec) | ||
| 3186 | { | ||
| 3187 | char type; | ||
| 3188 | 127 | int index = 0; | |
| 3189 | 127 | const char *stream_spec = NULL; | |
| 3190 | |||
| 3191 | 127 | if (parse_meta_type(log_ctx, spec, &type, &index, &stream_spec) < 0) | |
| 3192 | ✗ | return 0; | |
| 3193 | |||
| 3194 | 127 | if (*spec) { | |
| 3195 | 101 | if (type == 'g' && st) | |
| 3196 | ✗ | return 0; | |
| 3197 | 101 | if (type == 's') { | |
| 3198 | int ret; | ||
| 3199 | 90 | if (!st) | |
| 3200 | 48 | return 0; | |
| 3201 | 42 | ret = check_stream_specifier(oc, st, stream_spec); | |
| 3202 | 42 | return ret < 0 ? ret : ret > 0; | |
| 3203 | } | ||
| 3204 | 11 | if (type == 'c' || type == 'p') | |
| 3205 | 7 | return 0; | |
| 3206 | } | ||
| 3207 | 30 | return 1; | |
| 3208 | } | ||
| 3209 | |||
| 3210 | 1959 | static int reenc_delete_metadata_key(const OptionsContext *o, void *log_ctx, | |
| 3211 | AVFormatContext *oc, AVStream *st, | ||
| 3212 | const char *family, const char *key, int flags) | ||
| 3213 | { | ||
| 3214 | 2008 | for (int i = 0; i < o->keep_metadata.nb_opt; i++) { | |
| 3215 | 54 | int ret = meta_spec_matches(log_ctx, oc, st, o->keep_metadata.opt[i].specifier); | |
| 3216 | 54 | if (ret < 0) | |
| 3217 | ✗ | return ret; | |
| 3218 | 54 | if (!ret) | |
| 3219 | 18 | continue; | |
| 3220 | 36 | const char *keep = o->keep_metadata.opt[i].u.str; | |
| 3221 | 36 | if (flags & AV_DICT_IGNORE_SUFFIX) { | |
| 3222 | /* accept the unsuffixed family name (e.g. NUMBER_OF_BYTES keeps | ||
| 3223 | * NUMBER_OF_BYTES-eng) or the exact matched key; an arbitrary | ||
| 3224 | * prefix like NUMBER does not match NUMBER_OF_BYTES-eng */ | ||
| 3225 | 28 | if (!strcmp(keep, family) || !strcmp(keep, key)) | |
| 3226 | 3 | return 0; | |
| 3227 | } else { | ||
| 3228 | 8 | if (!strcmp(key, keep)) | |
| 3229 | 2 | return 0; | |
| 3230 | } | ||
| 3231 | } | ||
| 3232 | 2027 | for (int i = 0; i < o->metadata.nb_opt; i++) { | |
| 3233 | int ret; | ||
| 3234 | /* plain -metadata (empty specifier) applies to global metadata only, | ||
| 3235 | * matching of_add_metadata(); don't let it suppress stream-level pruning */ | ||
| 3236 | 74 | if (!*o->metadata.opt[i].specifier && st) | |
| 3237 | 1 | continue; | |
| 3238 | 73 | ret = meta_spec_matches(log_ctx, oc, st, o->metadata.opt[i].specifier); | |
| 3239 | 73 | if (ret < 0) | |
| 3240 | ✗ | return ret; | |
| 3241 | 73 | if (!ret) | |
| 3242 | 58 | continue; | |
| 3243 | 15 | size_t klen = strcspn(o->metadata.opt[i].u.str, "="); | |
| 3244 | 15 | if (!strncmp(key, o->metadata.opt[i].u.str, klen) && key[klen] == '\0') | |
| 3245 | 1 | return 0; | |
| 3246 | } | ||
| 3247 | 1953 | return 1; | |
| 3248 | } | ||
| 3249 | |||
| 3250 | 19139 | static int reenc_delete_stale_metadata(const OptionsContext *o, void *log_ctx, | |
| 3251 | AVFormatContext *oc, AVStream *st, | ||
| 3252 | AVDictionary **dict, const char *kind, | ||
| 3253 | unsigned reenc_mask) | ||
| 3254 | { | ||
| 3255 | 401919 | for (int i = 0; reenc_delete_keys[i].key; i++) { | |
| 3256 | 382780 | const char *key = reenc_delete_keys[i].key; | |
| 3257 | 382780 | int flags = reenc_delete_keys[i].flags; | |
| 3258 | const AVDictionaryEntry *e; | ||
| 3259 | int ret; | ||
| 3260 | |||
| 3261 | 382780 | if (reenc_delete_keys[i].reenc_mask && !(reenc_delete_keys[i].reenc_mask & reenc_mask)) | |
| 3262 | 130789 | continue; | |
| 3263 | |||
| 3264 | 251991 | e = NULL; | |
| 3265 | 253950 | while ((e = av_dict_get(*dict, key, e, flags | AV_DICT_MATCH_CASE))) { | |
| 3266 | 1959 | ret = reenc_delete_metadata_key(o, log_ctx, oc, st, key, e->key, flags); | |
| 3267 | 1959 | if (ret < 0) | |
| 3268 | ✗ | return ret; | |
| 3269 | 1959 | if (!ret) | |
| 3270 | 6 | continue; | |
| 3271 | 1953 | if (reenc_mask & reenc_delete_keys[i].reenc_mask) | |
| 3272 | 418 | av_log(log_ctx, AV_LOG_WARNING, | |
| 3273 | "Discarding %s metadata '%s' because %s stream is being " | ||
| 3274 | "re-encoded. Use '-keep_metadata %s' to keep it.\n", | ||
| 3275 | 418 | kind, e->key, | |
| 3276 | 418 | !strcmp(kind, "stream") ? "the" : "a", e->key); | |
| 3277 | 1953 | av_dict_set(dict, e->key, NULL, 0); | |
| 3278 | 1953 | e = NULL; | |
| 3279 | } | ||
| 3280 | } | ||
| 3281 | 19139 | return 0; | |
| 3282 | } | ||
| 3283 | |||
| 3284 | 9221 | static int copy_meta(Muxer *mux, const OptionsContext *o) | |
| 3285 | { | ||
| 3286 | 9221 | OutputFile *of = &mux->of; | |
| 3287 | 9221 | AVFormatContext *oc = mux->fc; | |
| 3288 | 9221 | int chapters_input_file = o->chapters_input_file; | |
| 3289 | 9221 | int metadata_global_manual = 0; | |
| 3290 | 9221 | int metadata_streams_manual = 0; | |
| 3291 | 9221 | int metadata_chapters_manual = 0; | |
| 3292 | int ret; | ||
| 3293 | |||
| 3294 | /* copy metadata */ | ||
| 3295 | 9249 | for (int i = 0; i < o->metadata_map.nb_opt; i++) { | |
| 3296 | char *p; | ||
| 3297 | 28 | int in_file_index = strtol(o->metadata_map.opt[i].u.str, &p, 0); | |
| 3298 | |||
| 3299 | 28 | if (in_file_index >= nb_input_files) { | |
| 3300 | ✗ | av_log(mux, AV_LOG_FATAL, "Invalid input file index %d while " | |
| 3301 | "processing metadata maps\n", in_file_index); | ||
| 3302 | ✗ | return AVERROR(EINVAL); | |
| 3303 | } | ||
| 3304 | 51 | ret = copy_metadata(mux, | |
| 3305 | 8 | in_file_index >= 0 ? input_files[in_file_index]->ctx : NULL, | |
| 3306 | 51 | o->metadata_map.opt[i].specifier, *p ? p + 1 : p, | |
| 3307 | &metadata_global_manual, &metadata_streams_manual, | ||
| 3308 | &metadata_chapters_manual); | ||
| 3309 | 28 | if (ret < 0) | |
| 3310 | ✗ | return ret; | |
| 3311 | } | ||
| 3312 | |||
| 3313 | /* copy chapters */ | ||
| 3314 | 9221 | if (chapters_input_file >= nb_input_files) { | |
| 3315 | 9219 | if (chapters_input_file == INT_MAX) { | |
| 3316 | /* copy chapters from the first input file that has them*/ | ||
| 3317 | 9219 | chapters_input_file = -1; | |
| 3318 | 17263 | for (int i = 0; i < nb_input_files; i++) | |
| 3319 | 8084 | if (input_files[i]->ctx->nb_chapters) { | |
| 3320 | 40 | chapters_input_file = i; | |
| 3321 | 40 | break; | |
| 3322 | } | ||
| 3323 | } else { | ||
| 3324 | ✗ | av_log(mux, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n", | |
| 3325 | chapters_input_file); | ||
| 3326 | ✗ | return AVERROR(EINVAL); | |
| 3327 | } | ||
| 3328 | } | ||
| 3329 | 9221 | if (chapters_input_file >= 0) | |
| 3330 | 42 | copy_chapters(input_files[chapters_input_file], of, oc, | |
| 3331 | !metadata_chapters_manual); | ||
| 3332 | |||
| 3333 | /* copy global metadata by default */ | ||
| 3334 | 9221 | if (!metadata_global_manual && nb_input_files){ | |
| 3335 | 7949 | av_dict_copy(&oc->metadata, input_files[0]->ctx->metadata, | |
| 3336 | AV_DICT_DONT_OVERWRITE); | ||
| 3337 | 7949 | if (of->recording_time != INT64_MAX) | |
| 3338 | 249 | av_dict_set(&oc->metadata, "duration", NULL, 0); | |
| 3339 | 7949 | av_dict_set(&oc->metadata, "creation_time", NULL, 0); | |
| 3340 | 7949 | av_dict_set(&oc->metadata, "company_name", NULL, 0); | |
| 3341 | 7949 | av_dict_set(&oc->metadata, "product_name", NULL, 0); | |
| 3342 | 7949 | av_dict_set(&oc->metadata, "product_version", NULL, 0); | |
| 3343 | |||
| 3344 | } | ||
| 3345 | 9230 | for (int i = 0; i < o->keep_metadata.nb_opt; i++) { | |
| 3346 | char type; | ||
| 3347 | 11 | int index = 0; | |
| 3348 | 11 | const char *stream_spec = NULL; | |
| 3349 | 11 | const char *spec = o->keep_metadata.opt[i].specifier; | |
| 3350 | 11 | ret = parse_meta_type(mux, spec, &type, &index, &stream_spec); | |
| 3351 | 11 | if (ret < 0) | |
| 3352 | 2 | return ret; | |
| 3353 | 10 | if (type == 'c' || type == 'p') { | |
| 3354 | ✗ | av_log(mux, AV_LOG_WARNING, | |
| 3355 | "-keep_metadata:%s: chapter and program metadata filtering " | ||
| 3356 | "is not supported and will be ignored.\n", spec); | ||
| 3357 | 10 | } else if (type == 's') { | |
| 3358 | 13 | for (int j = 0; j < oc->nb_streams; j++) { | |
| 3359 | 7 | ret = check_stream_specifier(oc, oc->streams[j], stream_spec); | |
| 3360 | 7 | if (ret < 0) | |
| 3361 | 1 | return ret; | |
| 3362 | } | ||
| 3363 | } | ||
| 3364 | } | ||
| 3365 | |||
| 3366 | /* reenc_mask keys only apply when an audio/video stream is re-encoded; | ||
| 3367 | * subtitle/attachment re-encodes do not affect format-level tags. | ||
| 3368 | * Runs unconditionally so -map_metadata does not bypass the pruning. */ | ||
| 3369 | { | ||
| 3370 | 9219 | unsigned reenc_mask = 0; | |
| 3371 | 19139 | for (int i = 0; i < of->nb_streams; i++) { | |
| 3372 | 9920 | OutputStream *ost = of->streams[i]; | |
| 3373 | 9920 | if (ost->enc) | |
| 3374 | 8849 | reenc_mask |= REENC_MASK(ost->st->codecpar->codec_type); | |
| 3375 | } | ||
| 3376 | 9219 | ret = reenc_delete_stale_metadata(o, mux, oc, NULL, &oc->metadata, "format", reenc_mask); | |
| 3377 | 9219 | if (ret < 0) | |
| 3378 | ✗ | return ret; | |
| 3379 | } | ||
| 3380 | 9219 | if (!metadata_streams_manual) | |
| 3381 | 19094 | for (int i = 0; i < of->nb_streams; i++) { | |
| 3382 | 9896 | OutputStream *ost = of->streams[i]; | |
| 3383 | |||
| 3384 | 9896 | if (!ost->ist) /* this is true e.g. for attached files */ | |
| 3385 | 1565 | continue; | |
| 3386 | 8331 | av_dict_copy(&ost->st->metadata, ost->ist->st->metadata, AV_DICT_DONT_OVERWRITE); | |
| 3387 | } | ||
| 3388 | /* runs unconditionally so -map_metadata does not bypass the pruning; | ||
| 3389 | * applies to all output streams, including filter outputs without ost->ist */ | ||
| 3390 | 19139 | for (int i = 0; i < of->nb_streams; i++) { | |
| 3391 | 9920 | OutputStream *ost = of->streams[i]; | |
| 3392 | |||
| 3393 | 9920 | ret = reenc_delete_stale_metadata(o, ost, oc, ost->st, &ost->st->metadata, "stream", | |
| 3394 | 9920 | ost->enc ? REENC_MASK(ost->st->codecpar->codec_type) : 0); | |
| 3395 | 9920 | if (ret < 0) | |
| 3396 | ✗ | return ret; | |
| 3397 | } | ||
| 3398 | |||
| 3399 | 9219 | return 0; | |
| 3400 | } | ||
| 3401 | |||
| 3402 | 9219 | static int set_dispositions(Muxer *mux, const OptionsContext *o) | |
| 3403 | { | ||
| 3404 | 9219 | OutputFile *of = &mux->of; | |
| 3405 | 9219 | AVFormatContext *ctx = mux->fc; | |
| 3406 | |||
| 3407 | // indexed by type+1, because AVMEDIA_TYPE_UNKNOWN=-1 | ||
| 3408 | 9219 | int nb_streams[AVMEDIA_TYPE_NB + 1] = { 0 }; | |
| 3409 | 9219 | int have_default[AVMEDIA_TYPE_NB + 1] = { 0 }; | |
| 3410 | 9219 | int have_manual = 0; | |
| 3411 | 9219 | int ret = 0; | |
| 3412 | |||
| 3413 | const char **dispositions; | ||
| 3414 | |||
| 3415 | 9219 | dispositions = av_calloc(ctx->nb_streams, sizeof(*dispositions)); | |
| 3416 | 9219 | if (!dispositions) | |
| 3417 | ✗ | return AVERROR(ENOMEM); | |
| 3418 | |||
| 3419 | // reset any apic flag set for option stream-spec matching in ost_add | ||
| 3420 | 19139 | for (int i = 0; i < ctx->nb_streams; i++) { | |
| 3421 | 9920 | of->streams[i]->st->disposition = 0; | |
| 3422 | } | ||
| 3423 | |||
| 3424 | // first, copy the input dispositions | ||
| 3425 | 19139 | for (int i = 0; i < ctx->nb_streams; i++) { | |
| 3426 | 9920 | OutputStream *ost = of->streams[i]; | |
| 3427 | |||
| 3428 | 9920 | nb_streams[ost->type + 1]++; | |
| 3429 | |||
| 3430 | 9920 | opt_match_per_stream_str(ost, &o->disposition, ctx, ost->st, &dispositions[i]); | |
| 3431 | |||
| 3432 | 9920 | have_manual |= !!dispositions[i]; | |
| 3433 | |||
| 3434 | 9920 | if (ost->ist) { | |
| 3435 | 8354 | ost->st->disposition = ost->ist->st->disposition; | |
| 3436 | |||
| 3437 | 8354 | if (ost->st->disposition & AV_DISPOSITION_DEFAULT) | |
| 3438 | 965 | have_default[ost->type + 1] = 1; | |
| 3439 | } | ||
| 3440 | } | ||
| 3441 | |||
| 3442 | 9219 | if (have_manual) { | |
| 3443 | // process manually set dispositions - they override the above copy | ||
| 3444 | 38 | for (int i = 0; i < ctx->nb_streams; i++) { | |
| 3445 | 27 | OutputStream *ost = of->streams[i]; | |
| 3446 | 27 | const char *disp = dispositions[i]; | |
| 3447 | |||
| 3448 | 27 | if (!disp) | |
| 3449 | 8 | continue; | |
| 3450 | |||
| 3451 | 19 | ret = av_opt_set(ost->st, "disposition", disp, 0); | |
| 3452 | 19 | if (ret < 0) | |
| 3453 | ✗ | goto finish; | |
| 3454 | } | ||
| 3455 | } else { | ||
| 3456 | // For each media type with more than one stream, find a suitable stream to | ||
| 3457 | // mark as default, unless one is already marked default. | ||
| 3458 | // "Suitable" means the first of that type, skipping attached pictures. | ||
| 3459 | 19101 | for (int i = 0; i < ctx->nb_streams; i++) { | |
| 3460 | 9893 | OutputStream *ost = of->streams[i]; | |
| 3461 | 9893 | enum AVMediaType type = ost->type; | |
| 3462 | |||
| 3463 | 9893 | if (nb_streams[type + 1] < 2 || have_default[type + 1] || | |
| 3464 | 97 | ost->st->disposition & AV_DISPOSITION_ATTACHED_PIC) | |
| 3465 | 9828 | continue; | |
| 3466 | |||
| 3467 | 65 | ost->st->disposition |= AV_DISPOSITION_DEFAULT; | |
| 3468 | 65 | have_default[type + 1] = 1; | |
| 3469 | } | ||
| 3470 | } | ||
| 3471 | |||
| 3472 | 9208 | finish: | |
| 3473 | 9219 | av_freep(&dispositions); | |
| 3474 | |||
| 3475 | 9219 | return ret; | |
| 3476 | } | ||
| 3477 | |||
| 3478 | static const char *const forced_keyframes_const_names[] = { | ||
| 3479 | "n", | ||
| 3480 | "n_forced", | ||
| 3481 | "prev_forced_n", | ||
| 3482 | "prev_forced_t", | ||
| 3483 | "t", | ||
| 3484 | NULL | ||
| 3485 | }; | ||
| 3486 | |||
| 3487 | 1 | static int compare_int64(const void *a, const void *b) | |
| 3488 | { | ||
| 3489 | 1 | return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b); | |
| 3490 | } | ||
| 3491 | |||
| 3492 | 1 | static int parse_forced_key_frames(void *log, KeyframeForceCtx *kf, | |
| 3493 | const Muxer *mux, const char *spec) | ||
| 3494 | { | ||
| 3495 | 1 | int n = 1, i, ret, size, index = 0; | |
| 3496 | int64_t t, *pts; | ||
| 3497 | |||
| 3498 | 14 | for (const char *p = spec; *p; p++) | |
| 3499 | 13 | if (*p == ',') | |
| 3500 | 1 | n++; | |
| 3501 | 1 | size = n; | |
| 3502 | |||
| 3503 | 1 | char *spec_dup = av_strdup(spec); | |
| 3504 | 1 | pts = av_malloc_array(size, sizeof(*pts)); | |
| 3505 | 1 | if (!spec_dup || !pts) { | |
| 3506 | ✗ | ret = AVERROR(ENOMEM); | |
| 3507 | ✗ | goto fail; | |
| 3508 | } | ||
| 3509 | |||
| 3510 | 1 | char *p = spec_dup; | |
| 3511 | 3 | for (i = 0; i < n; i++) { | |
| 3512 | 2 | char *next = strchr(p, ','); | |
| 3513 | |||
| 3514 | 2 | if (next) | |
| 3515 | 1 | *next++ = 0; | |
| 3516 | |||
| 3517 | 2 | if (strstr(p, "chapters") == p) { | |
| 3518 | ✗ | AVChapter * const *ch = mux->fc->chapters; | |
| 3519 | ✗ | unsigned int nb_ch = mux->fc->nb_chapters; | |
| 3520 | int j; | ||
| 3521 | |||
| 3522 | ✗ | if (nb_ch > INT_MAX - size) { | |
| 3523 | ✗ | ret = AVERROR(ERANGE); | |
| 3524 | ✗ | goto fail; | |
| 3525 | } | ||
| 3526 | ✗ | size += nb_ch - 1; | |
| 3527 | ✗ | pts = av_realloc_f(pts, size, sizeof(*pts)); | |
| 3528 | ✗ | if (!pts) { | |
| 3529 | ✗ | ret = AVERROR(ENOMEM); | |
| 3530 | ✗ | goto fail; | |
| 3531 | } | ||
| 3532 | |||
| 3533 | ✗ | if (p[8]) { | |
| 3534 | ✗ | ret = av_parse_time(&t, p + 8, 1); | |
| 3535 | ✗ | if (ret < 0) { | |
| 3536 | ✗ | av_log(log, AV_LOG_ERROR, | |
| 3537 | "Invalid chapter time offset: %s\n", p + 8); | ||
| 3538 | ✗ | goto fail; | |
| 3539 | } | ||
| 3540 | } else | ||
| 3541 | ✗ | t = 0; | |
| 3542 | |||
| 3543 | ✗ | for (j = 0; j < nb_ch; j++) { | |
| 3544 | ✗ | const AVChapter *c = ch[j]; | |
| 3545 | av_assert1(index < size); | ||
| 3546 | ✗ | pts[index++] = av_rescale_q(c->start, c->time_base, | |
| 3547 | ✗ | AV_TIME_BASE_Q) + t; | |
| 3548 | } | ||
| 3549 | |||
| 3550 | } else { | ||
| 3551 | av_assert1(index < size); | ||
| 3552 | 2 | ret = av_parse_time(&t, p, 1); | |
| 3553 | 2 | if (ret < 0) { | |
| 3554 | ✗ | av_log(log, AV_LOG_ERROR, "Invalid keyframe time: %s\n", p); | |
| 3555 | ✗ | goto fail; | |
| 3556 | } | ||
| 3557 | |||
| 3558 | 2 | pts[index++] = t; | |
| 3559 | } | ||
| 3560 | |||
| 3561 | 2 | p = next; | |
| 3562 | } | ||
| 3563 | |||
| 3564 | 1 | av_assert0(index == size); | |
| 3565 | 1 | qsort(pts, size, sizeof(*pts), compare_int64); | |
| 3566 | 1 | kf->nb_pts = size; | |
| 3567 | 1 | kf->pts = pts; | |
| 3568 | |||
| 3569 | 1 | av_freep(&spec_dup); | |
| 3570 | |||
| 3571 | 1 | return 0; | |
| 3572 | ✗ | fail: | |
| 3573 | ✗ | av_freep(&spec_dup); | |
| 3574 | ✗ | av_freep(&pts); | |
| 3575 | ✗ | return ret; | |
| 3576 | } | ||
| 3577 | |||
| 3578 | 9219 | static int process_forced_keyframes(Muxer *mux, const OptionsContext *o) | |
| 3579 | { | ||
| 3580 | 19139 | for (int i = 0; i < mux->of.nb_streams; i++) { | |
| 3581 | 9920 | OutputStream *ost = mux->of.streams[i]; | |
| 3582 | 9920 | const char *forced_keyframes = NULL; | |
| 3583 | |||
| 3584 | 9920 | opt_match_per_stream_str(ost, &o->forced_key_frames, | |
| 3585 | mux->fc, ost->st, &forced_keyframes); | ||
| 3586 | |||
| 3587 | 9920 | if (!(ost->type == AVMEDIA_TYPE_VIDEO && | |
| 3588 | 7626 | ost->enc && forced_keyframes)) | |
| 3589 | 9915 | continue; | |
| 3590 | |||
| 3591 | 5 | if (!strncmp(forced_keyframes, "expr:", 5)) { | |
| 3592 | ✗ | int ret = av_expr_parse(&ost->kf.pexpr, forced_keyframes + 5, | |
| 3593 | forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL); | ||
| 3594 | ✗ | if (ret < 0) { | |
| 3595 | ✗ | av_log(ost, AV_LOG_ERROR, | |
| 3596 | "Invalid force_key_frames expression '%s'\n", forced_keyframes + 5); | ||
| 3597 | ✗ | return ret; | |
| 3598 | } | ||
| 3599 | ✗ | ost->kf.expr_const_values[FKF_N] = 0; | |
| 3600 | ✗ | ost->kf.expr_const_values[FKF_N_FORCED] = 0; | |
| 3601 | ✗ | ost->kf.expr_const_values[FKF_PREV_FORCED_N] = NAN; | |
| 3602 | ✗ | ost->kf.expr_const_values[FKF_PREV_FORCED_T] = NAN; | |
| 3603 | |||
| 3604 | // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes', | ||
| 3605 | // parse it only for static kf timings | ||
| 3606 | 5 | } else if (!strcmp(forced_keyframes, "source")) { | |
| 3607 | 3 | ost->kf.type = KF_FORCE_SOURCE; | |
| 3608 | 2 | } else if (!strcmp(forced_keyframes, "scd_metadata")) { | |
| 3609 | 1 | ost->kf.type = KF_FORCE_SCD_METADATA; | |
| 3610 | } else { | ||
| 3611 | 1 | int ret = parse_forced_key_frames(ost, &ost->kf, mux, forced_keyframes); | |
| 3612 | 1 | if (ret < 0) | |
| 3613 | ✗ | return ret; | |
| 3614 | } | ||
| 3615 | } | ||
| 3616 | |||
| 3617 | 9219 | return 0; | |
| 3618 | } | ||
| 3619 | |||
| 3620 | 9771 | static const char *output_file_item_name(void *obj) | |
| 3621 | { | ||
| 3622 | 9771 | const Muxer *mux = obj; | |
| 3623 | |||
| 3624 | 9771 | return mux->log_name; | |
| 3625 | } | ||
| 3626 | |||
| 3627 | static const AVClass output_file_class = { | ||
| 3628 | .class_name = "OutputFile", | ||
| 3629 | .version = LIBAVUTIL_VERSION_INT, | ||
| 3630 | .item_name = output_file_item_name, | ||
| 3631 | .category = AV_CLASS_CATEGORY_MUXER, | ||
| 3632 | }; | ||
| 3633 | |||
| 3634 | 9224 | static Muxer *mux_alloc(void) | |
| 3635 | { | ||
| 3636 | 9224 | Muxer *mux = allocate_array_elem(&output_files, sizeof(*mux), &nb_output_files); | |
| 3637 | |||
| 3638 | 9224 | if (!mux) | |
| 3639 | ✗ | return NULL; | |
| 3640 | |||
| 3641 | 9224 | mux->of.class = &output_file_class; | |
| 3642 | 9224 | mux->of.index = nb_output_files - 1; | |
| 3643 | |||
| 3644 | 9224 | snprintf(mux->log_name, sizeof(mux->log_name), "out#%d", mux->of.index); | |
| 3645 | |||
| 3646 | 9224 | return mux; | |
| 3647 | } | ||
| 3648 | |||
| 3649 | 9224 | int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) | |
| 3650 | { | ||
| 3651 | Muxer *mux; | ||
| 3652 | AVFormatContext *oc; | ||
| 3653 | int err; | ||
| 3654 | OutputFile *of; | ||
| 3655 | |||
| 3656 | 9224 | int64_t recording_time = o->recording_time; | |
| 3657 | 9224 | int64_t stop_time = o->stop_time; | |
| 3658 | |||
| 3659 | 9224 | mux = mux_alloc(); | |
| 3660 | 9224 | if (!mux) | |
| 3661 | ✗ | return AVERROR(ENOMEM); | |
| 3662 | |||
| 3663 | 9224 | of = &mux->of; | |
| 3664 | |||
| 3665 | 9224 | if (stop_time != INT64_MAX && recording_time != INT64_MAX) { | |
| 3666 | ✗ | stop_time = INT64_MAX; | |
| 3667 | ✗ | av_log(mux, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n"); | |
| 3668 | } | ||
| 3669 | |||
| 3670 | 9224 | if (stop_time != INT64_MAX && recording_time == INT64_MAX) { | |
| 3671 | ✗ | int64_t start_time = o->start_time == AV_NOPTS_VALUE ? 0 : o->start_time; | |
| 3672 | ✗ | if (stop_time <= start_time) { | |
| 3673 | ✗ | av_log(mux, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n"); | |
| 3674 | ✗ | return AVERROR(EINVAL); | |
| 3675 | } else { | ||
| 3676 | ✗ | recording_time = stop_time - start_time; | |
| 3677 | } | ||
| 3678 | } | ||
| 3679 | |||
| 3680 | 9224 | if (recording_time < 0) { | |
| 3681 | 1 | av_log(mux, AV_LOG_ERROR, "-t value must be non-negative; aborting.\n"); | |
| 3682 | 1 | return AVERROR(EINVAL); | |
| 3683 | } | ||
| 3684 | |||
| 3685 | 9223 | of->recording_time = recording_time; | |
| 3686 | 9223 | of->start_time = o->start_time; | |
| 3687 | |||
| 3688 | 9223 | mux->limit_filesize = o->limit_filesize; | |
| 3689 | 9223 | av_dict_copy(&mux->opts, o->g->format_opts, 0); | |
| 3690 | |||
| 3691 | 9223 | if (!strcmp(filename, "-")) | |
| 3692 | 3143 | filename = "pipe:"; | |
| 3693 | |||
| 3694 | 9223 | err = avformat_alloc_output_context2(&oc, NULL, o->format, filename); | |
| 3695 | 9223 | if (!oc) { | |
| 3696 | ✗ | av_log(mux, AV_LOG_FATAL, "Error initializing the muxer for %s: %s\n", | |
| 3697 | ✗ | filename, av_err2str(err)); | |
| 3698 | ✗ | return err; | |
| 3699 | } | ||
| 3700 | 9223 | mux->fc = oc; | |
| 3701 | |||
| 3702 | 9223 | av_strlcat(mux->log_name, "/", sizeof(mux->log_name)); | |
| 3703 | 9223 | av_strlcat(mux->log_name, oc->oformat->name, sizeof(mux->log_name)); | |
| 3704 | |||
| 3705 | |||
| 3706 | 9223 | if (recording_time != INT64_MAX) | |
| 3707 | 1362 | oc->duration = recording_time; | |
| 3708 | |||
| 3709 | 9223 | oc->interrupt_callback = int_cb; | |
| 3710 | |||
| 3711 | 9223 | if (o->bitexact) { | |
| 3712 | 2125 | oc->flags |= AVFMT_FLAG_BITEXACT; | |
| 3713 | 2125 | of->bitexact = 1; | |
| 3714 | } else { | ||
| 3715 | 7098 | of->bitexact = check_opt_bitexact(oc, mux->opts, "fflags", | |
| 3716 | AVFMT_FLAG_BITEXACT); | ||
| 3717 | } | ||
| 3718 | |||
| 3719 | 9223 | err = sch_add_mux(sch, muxer_thread, mux_check_init, mux, | |
| 3720 | 9223 | !strcmp(oc->oformat->name, "rtp"), o->thread_queue_size); | |
| 3721 | 9223 | if (err < 0) | |
| 3722 | ✗ | return err; | |
| 3723 | 9223 | mux->sch = sch; | |
| 3724 | 9223 | mux->sch_idx = err; | |
| 3725 | |||
| 3726 | /* create all output streams for this file */ | ||
| 3727 | 9223 | err = create_streams(mux, o); | |
| 3728 | 9223 | if (err < 0) | |
| 3729 | 1 | return err; | |
| 3730 | |||
| 3731 | /* check if all codec options have been used */ | ||
| 3732 | 9222 | err = check_avoptions_used(o->g->codec_opts, mux->enc_opts_used, mux, 0); | |
| 3733 | 9222 | av_dict_free(&mux->enc_opts_used); | |
| 3734 | 9222 | if (err < 0) | |
| 3735 | ✗ | return err; | |
| 3736 | |||
| 3737 | /* check filename in case of an image number is expected */ | ||
| 3738 | 9222 | if (oc->oformat->flags & AVFMT_NEEDNUMBER && !av_filename_number_test(oc->url)) { | |
| 3739 | ✗ | av_log(mux, AV_LOG_FATAL, | |
| 3740 | "Output filename '%s' does not contain a numeric pattern like " | ||
| 3741 | "'%%d', which is required by output format '%s'.\n", | ||
| 3742 | ✗ | oc->url, oc->oformat->name); | |
| 3743 | ✗ | return AVERROR(EINVAL); | |
| 3744 | } | ||
| 3745 | |||
| 3746 | 9222 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { | |
| 3747 | /* test if it already exists to avoid losing precious files */ | ||
| 3748 | 9050 | err = assert_file_overwrite(filename); | |
| 3749 | 9050 | if (err < 0) | |
| 3750 | 1 | return err; | |
| 3751 | |||
| 3752 | /* open the file */ | ||
| 3753 | 9049 | if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE, | |
| 3754 | 9049 | &oc->interrupt_callback, | |
| 3755 | &mux->opts)) < 0) { | ||
| 3756 | ✗ | av_log(mux, AV_LOG_FATAL, "Error opening output %s: %s\n", | |
| 3757 | ✗ | filename, av_err2str(err)); | |
| 3758 | ✗ | return err; | |
| 3759 | } | ||
| 3760 | 172 | } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename)) { | |
| 3761 | 12 | err = assert_file_overwrite(filename); | |
| 3762 | 12 | if (err < 0) | |
| 3763 | ✗ | return err; | |
| 3764 | } | ||
| 3765 | |||
| 3766 | 9221 | if (o->mux_preload) { | |
| 3767 | ✗ | av_dict_set_int(&mux->opts, "preload", o->mux_preload*AV_TIME_BASE, 0); | |
| 3768 | } | ||
| 3769 | 9221 | oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE); | |
| 3770 | |||
| 3771 | /* copy metadata and chapters from input files */ | ||
| 3772 | 9221 | err = copy_meta(mux, o); | |
| 3773 | 9221 | if (err < 0) | |
| 3774 | 2 | return err; | |
| 3775 | |||
| 3776 | 9219 | err = of_add_groups(mux, o); | |
| 3777 | 9219 | if (err < 0) | |
| 3778 | ✗ | return err; | |
| 3779 | |||
| 3780 | 9219 | err = of_add_programs(mux, o); | |
| 3781 | 9219 | if (err < 0) | |
| 3782 | ✗ | return err; | |
| 3783 | |||
| 3784 | 9219 | err = of_add_metadata(of, oc, o); | |
| 3785 | 9219 | if (err < 0) | |
| 3786 | ✗ | return err; | |
| 3787 | |||
| 3788 | 9219 | err = set_dispositions(mux, o); | |
| 3789 | 9219 | if (err < 0) { | |
| 3790 | ✗ | av_log(mux, AV_LOG_FATAL, "Error setting output stream dispositions\n"); | |
| 3791 | ✗ | return err; | |
| 3792 | } | ||
| 3793 | |||
| 3794 | // parse forced keyframe specifications; | ||
| 3795 | // must be done after chapters are created | ||
| 3796 | 9219 | err = process_forced_keyframes(mux, o); | |
| 3797 | 9219 | if (err < 0) { | |
| 3798 | ✗ | av_log(mux, AV_LOG_FATAL, "Error processing forced keyframes\n"); | |
| 3799 | ✗ | return err; | |
| 3800 | } | ||
| 3801 | |||
| 3802 | 9219 | err = setup_sync_queues(mux, oc, o->shortest_buf_duration * AV_TIME_BASE, | |
| 3803 | 9219 | o->shortest); | |
| 3804 | 9219 | if (err < 0) { | |
| 3805 | ✗ | av_log(mux, AV_LOG_FATAL, "Error setting up output sync queues\n"); | |
| 3806 | ✗ | return err; | |
| 3807 | } | ||
| 3808 | |||
| 3809 | 9219 | of->url = filename; | |
| 3810 | |||
| 3811 | /* initialize streamcopy streams. */ | ||
| 3812 | 19139 | for (int i = 0; i < of->nb_streams; i++) { | |
| 3813 | 9920 | OutputStream *ost = of->streams[i]; | |
| 3814 | |||
| 3815 | 9920 | if (!ost->enc) { | |
| 3816 | 1071 | err = of_stream_init(of, ost, NULL); | |
| 3817 | 1071 | if (err < 0) | |
| 3818 | ✗ | return err; | |
| 3819 | } | ||
| 3820 | } | ||
| 3821 | |||
| 3822 | 9219 | return 0; | |
| 3823 | } | ||
| 3824 |