options.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  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. #include "avformat.h"
  21. #include "avformat_internal.h"
  22. #include "avio_internal.h"
  23. #include "demux.h"
  24. #include "internal.h"
  25. #include "libavcodec/avcodec.h"
  26. #include "libavcodec/codec_par.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/iamf.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/intmath.h"
  31. #include "libavutil/mem.h"
  32. #include "libavutil/opt.h"
  33. /**
  34. * @file
  35. * Options definition for AVFormatContext.
  36. */
  37. FF_DISABLE_DEPRECATION_WARNINGS
  38. #include "options_table.h"
  39. FF_ENABLE_DEPRECATION_WARNINGS
  40. static const char* format_to_name(void* ptr)
  41. {
  42. AVFormatContext* fc = (AVFormatContext*) ptr;
  43. if(fc->iformat) return fc->iformat->name;
  44. else if(fc->oformat) return fc->oformat->name;
  45. else return fc->av_class->class_name;
  46. }
  47. static void *format_child_next(void *obj, void *prev)
  48. {
  49. AVFormatContext *s = obj;
  50. if (!prev && s->priv_data &&
  51. ((s->iformat && s->iformat->priv_class) ||
  52. s->oformat && s->oformat->priv_class))
  53. return s->priv_data;
  54. if (s->pb && s->pb->av_class && prev != s->pb)
  55. return s->pb;
  56. return NULL;
  57. }
  58. enum {
  59. CHILD_CLASS_ITER_AVIO = 0,
  60. CHILD_CLASS_ITER_MUX,
  61. CHILD_CLASS_ITER_DEMUX,
  62. CHILD_CLASS_ITER_DONE,
  63. };
  64. #define ITER_STATE_SHIFT 16
  65. static const AVClass *format_child_class_iterate(void **iter)
  66. {
  67. // we use the low 16 bits of iter as the value to be passed to
  68. // av_(de)muxer_iterate()
  69. void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
  70. unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
  71. const AVClass *ret = NULL;
  72. if (state == CHILD_CLASS_ITER_AVIO) {
  73. ret = &ff_avio_class;
  74. state++;
  75. goto finish;
  76. }
  77. if (state == CHILD_CLASS_ITER_MUX) {
  78. const AVOutputFormat *ofmt;
  79. while ((ofmt = av_muxer_iterate(&val))) {
  80. ret = ofmt->priv_class;
  81. if (ret)
  82. goto finish;
  83. }
  84. val = NULL;
  85. state++;
  86. }
  87. if (state == CHILD_CLASS_ITER_DEMUX) {
  88. const AVInputFormat *ifmt;
  89. while ((ifmt = av_demuxer_iterate(&val))) {
  90. ret = ifmt->priv_class;
  91. if (ret)
  92. goto finish;
  93. }
  94. val = NULL;
  95. state++;
  96. }
  97. finish:
  98. // make sure none av_(de)muxer_iterate does not set the high bits of val
  99. av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
  100. *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
  101. return ret;
  102. }
  103. static AVClassCategory get_category(void *ptr)
  104. {
  105. AVFormatContext* s = ptr;
  106. if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
  107. else return AV_CLASS_CATEGORY_MUXER;
  108. }
  109. static const AVClass av_format_context_class = {
  110. .class_name = "AVFormatContext",
  111. .item_name = format_to_name,
  112. .option = avformat_options,
  113. .version = LIBAVUTIL_VERSION_INT,
  114. .child_next = format_child_next,
  115. .child_class_iterate = format_child_class_iterate,
  116. .category = AV_CLASS_CATEGORY_MUXER,
  117. .get_category = get_category,
  118. };
  119. static int io_open_default(AVFormatContext *s, AVIOContext **pb,
  120. const char *url, int flags, AVDictionary **options)
  121. {
  122. int loglevel;
  123. if (!strcmp(url, s->url) ||
  124. s->iformat && !strcmp(s->iformat->name, "image2") ||
  125. s->oformat && !strcmp(s->oformat->name, "image2")
  126. ) {
  127. loglevel = AV_LOG_DEBUG;
  128. } else
  129. loglevel = AV_LOG_INFO;
  130. av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
  131. return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
  132. }
  133. static int io_close2_default(AVFormatContext *s, AVIOContext *pb)
  134. {
  135. return avio_close(pb);
  136. }
  137. AVFormatContext *avformat_alloc_context(void)
  138. {
  139. FFFormatContext *const si = av_mallocz(sizeof(*si));
  140. AVFormatContext *s;
  141. if (!si)
  142. return NULL;
  143. s = &si->pub;
  144. s->av_class = &av_format_context_class;
  145. s->io_open = io_open_default;
  146. s->io_close2= io_close2_default;
  147. av_opt_set_defaults(s);
  148. si->pkt = av_packet_alloc();
  149. si->parse_pkt = av_packet_alloc();
  150. if (!si->pkt || !si->parse_pkt) {
  151. avformat_free_context(s);
  152. return NULL;
  153. }
  154. #if FF_API_LAVF_SHORTEST
  155. si->shortest_end = AV_NOPTS_VALUE;
  156. #endif
  157. return s;
  158. }
  159. #if FF_API_GET_DUR_ESTIMATE_METHOD
  160. enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
  161. {
  162. return ctx->duration_estimation_method;
  163. }
  164. #endif
  165. const AVClass *avformat_get_class(void)
  166. {
  167. return &av_format_context_class;
  168. }
  169. #define DISPOSITION_OPT(ctx) \
  170. { "disposition", NULL, offsetof(ctx, disposition), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, \
  171. .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "disposition" }, \
  172. { "default", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT }, .unit = "disposition" }, \
  173. { "dub", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB }, .unit = "disposition" }, \
  174. { "original", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL }, .unit = "disposition" }, \
  175. { "comment", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT }, .unit = "disposition" }, \
  176. { "lyrics", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS }, .unit = "disposition" }, \
  177. { "karaoke", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE }, .unit = "disposition" }, \
  178. { "forced", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED }, .unit = "disposition" }, \
  179. { "hearing_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED }, .unit = "disposition" }, \
  180. { "visual_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED }, .unit = "disposition" }, \
  181. { "clean_effects", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS }, .unit = "disposition" }, \
  182. { "attached_pic", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC }, .unit = "disposition" }, \
  183. { "timed_thumbnails", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_TIMED_THUMBNAILS }, .unit = "disposition" }, \
  184. { "non_diegetic", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_NON_DIEGETIC }, .unit = "disposition" }, \
  185. { "captions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, .unit = "disposition" }, \
  186. { "descriptions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, .unit = "disposition" }, \
  187. { "metadata", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, .unit = "disposition" }, \
  188. { "dependent", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT }, .unit = "disposition" }, \
  189. { "still_image", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_STILL_IMAGE }, .unit = "disposition" }, \
  190. { "multilayer", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_MULTILAYER }, .unit = "disposition" }
  191. static const AVOption stream_options[] = {
  192. DISPOSITION_OPT(AVStream),
  193. { "discard", NULL, offsetof(AVStream, discard), AV_OPT_TYPE_INT, { .i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX,
  194. .flags = AV_OPT_FLAG_DECODING_PARAM, .unit = "avdiscard" },
  195. { "none", .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONE }, .unit = "avdiscard" },
  196. { "default", .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_DEFAULT }, .unit = "avdiscard" },
  197. { "noref", .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONREF }, .unit = "avdiscard" },
  198. { "bidir", .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_BIDIR }, .unit = "avdiscard" },
  199. { "nointra", .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONINTRA }, .unit = "avdiscard" },
  200. { "nokey", .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONKEY }, .unit = "avdiscard" },
  201. { "all", .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_ALL }, .unit = "avdiscard" },
  202. { NULL }
  203. };
  204. static const AVClass stream_class = {
  205. .class_name = "AVStream",
  206. .item_name = av_default_item_name,
  207. .version = LIBAVUTIL_VERSION_INT,
  208. .option = stream_options,
  209. };
  210. const AVClass *av_stream_get_class(void)
  211. {
  212. return &stream_class;
  213. }
  214. AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
  215. {
  216. FFFormatContext *const si = ffformatcontext(s);
  217. FFStream *sti;
  218. AVStream *st;
  219. AVStream **streams;
  220. if (s->nb_streams >= s->max_streams) {
  221. av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter"
  222. " (%d), see the documentation if you wish to increase it\n",
  223. s->max_streams);
  224. return NULL;
  225. }
  226. streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
  227. if (!streams)
  228. return NULL;
  229. s->streams = streams;
  230. sti = av_mallocz(sizeof(*sti));
  231. if (!sti)
  232. return NULL;
  233. st = &sti->pub;
  234. st->av_class = &stream_class;
  235. st->codecpar = avcodec_parameters_alloc();
  236. if (!st->codecpar)
  237. goto fail;
  238. sti->fmtctx = s;
  239. if (s->iformat) {
  240. sti->avctx = avcodec_alloc_context3(NULL);
  241. if (!sti->avctx)
  242. goto fail;
  243. sti->info = av_mallocz(sizeof(*sti->info));
  244. if (!sti->info)
  245. goto fail;
  246. #if FF_API_R_FRAME_RATE
  247. sti->info->last_dts = AV_NOPTS_VALUE;
  248. #endif
  249. sti->info->fps_first_dts = AV_NOPTS_VALUE;
  250. sti->info->fps_last_dts = AV_NOPTS_VALUE;
  251. /* default pts setting is MPEG-like */
  252. avpriv_set_pts_info(st, 33, 1, 90000);
  253. /* we set the current DTS to 0 so that formats without any timestamps
  254. * but durations get some timestamps, formats with some unknown
  255. * timestamps have their first few packets buffered and the
  256. * timestamps corrected before they are returned to the user */
  257. sti->cur_dts = RELATIVE_TS_BASE;
  258. } else {
  259. sti->cur_dts = AV_NOPTS_VALUE;
  260. }
  261. st->index = s->nb_streams;
  262. st->start_time = AV_NOPTS_VALUE;
  263. st->duration = AV_NOPTS_VALUE;
  264. sti->first_dts = AV_NOPTS_VALUE;
  265. sti->probe_packets = s->max_probe_packets;
  266. sti->pts_wrap_reference = AV_NOPTS_VALUE;
  267. sti->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
  268. sti->last_IP_pts = AV_NOPTS_VALUE;
  269. sti->last_dts_for_order_check = AV_NOPTS_VALUE;
  270. for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
  271. sti->pts_buffer[i] = AV_NOPTS_VALUE;
  272. st->sample_aspect_ratio = (AVRational) { 0, 1 };
  273. #if FF_API_INTERNAL_TIMING
  274. sti->transferred_mux_tb = (AVRational) { 0, 1 };;
  275. #endif
  276. #if FF_API_AVSTREAM_SIDE_DATA
  277. sti->inject_global_side_data = si->inject_global_side_data;
  278. #endif
  279. sti->need_context_update = 1;
  280. s->streams[s->nb_streams++] = st;
  281. return st;
  282. fail:
  283. ff_free_stream(&st);
  284. return NULL;
  285. }
  286. #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  287. #define OFFSET(x) offsetof(AVStreamGroupTileGrid, x)
  288. static const AVOption tile_grid_options[] = {
  289. { "grid_size", "size of the output canvas", OFFSET(coded_width),
  290. AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
  291. { "output_size", "size of valid pixels in output image meant for presentation", OFFSET(width),
  292. AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
  293. { "background_color", "set a background color for unused pixels",
  294. OFFSET(background), AV_OPT_TYPE_COLOR, { .str = "black"}, 0, 0, FLAGS },
  295. { "horizontal_offset", NULL, OFFSET(horizontal_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  296. { "vertical_offset", NULL, OFFSET(vertical_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  297. { NULL },
  298. };
  299. #undef OFFSET
  300. static const AVClass tile_grid_class = {
  301. .class_name = "AVStreamGroupTileGrid",
  302. .version = LIBAVUTIL_VERSION_INT,
  303. .option = tile_grid_options,
  304. };
  305. #define OFFSET(x) offsetof(AVStreamGroupLCEVC, x)
  306. static const AVOption lcevc_options[] = {
  307. { "video_size", "size of video after LCEVC enhancement has been applied", OFFSET(width),
  308. AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
  309. { NULL },
  310. };
  311. #undef OFFSET
  312. static const AVClass lcevc_class = {
  313. .class_name = "AVStreamGroupLCEVC",
  314. .version = LIBAVUTIL_VERSION_INT,
  315. .option = lcevc_options,
  316. };
  317. static void *stream_group_child_next(void *obj, void *prev)
  318. {
  319. AVStreamGroup *stg = obj;
  320. if (!prev) {
  321. switch(stg->type) {
  322. case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
  323. return stg->params.iamf_audio_element;
  324. case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
  325. return stg->params.iamf_mix_presentation;
  326. case AV_STREAM_GROUP_PARAMS_TILE_GRID:
  327. return stg->params.tile_grid;
  328. case AV_STREAM_GROUP_PARAMS_LCEVC:
  329. return stg->params.lcevc;
  330. default:
  331. break;
  332. }
  333. }
  334. return NULL;
  335. }
  336. #undef FLAGS
  337. static const AVClass *stream_group_child_iterate(void **opaque)
  338. {
  339. uintptr_t i = (uintptr_t)*opaque;
  340. const AVClass *ret = NULL;
  341. switch(i) {
  342. case AV_STREAM_GROUP_PARAMS_NONE:
  343. i++;
  344. // fall-through
  345. case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
  346. ret = av_iamf_audio_element_get_class();
  347. break;
  348. case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
  349. ret = av_iamf_mix_presentation_get_class();
  350. break;
  351. case AV_STREAM_GROUP_PARAMS_TILE_GRID:
  352. ret = &tile_grid_class;
  353. break;
  354. case AV_STREAM_GROUP_PARAMS_LCEVC:
  355. ret = &lcevc_class;
  356. break;
  357. default:
  358. break;
  359. }
  360. if (ret)
  361. *opaque = (void*)(i + 1);
  362. return ret;
  363. }
  364. static const AVOption stream_group_options[] = {
  365. DISPOSITION_OPT(AVStreamGroup),
  366. {"id", "Set group id", offsetof(AVStreamGroup, id), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  367. { NULL }
  368. };
  369. static const AVClass stream_group_class = {
  370. .class_name = "AVStreamGroup",
  371. .item_name = av_default_item_name,
  372. .version = LIBAVUTIL_VERSION_INT,
  373. .option = stream_group_options,
  374. .child_next = stream_group_child_next,
  375. .child_class_iterate = stream_group_child_iterate,
  376. };
  377. const AVClass *av_stream_group_get_class(void)
  378. {
  379. return &stream_group_class;
  380. }
  381. AVStreamGroup *avformat_stream_group_create(AVFormatContext *s,
  382. enum AVStreamGroupParamsType type,
  383. AVDictionary **options)
  384. {
  385. AVStreamGroup **stream_groups;
  386. AVStreamGroup *stg;
  387. FFStreamGroup *stgi;
  388. stream_groups = av_realloc_array(s->stream_groups, s->nb_stream_groups + 1,
  389. sizeof(*stream_groups));
  390. if (!stream_groups)
  391. return NULL;
  392. s->stream_groups = stream_groups;
  393. stgi = av_mallocz(sizeof(*stgi));
  394. if (!stgi)
  395. return NULL;
  396. stg = &stgi->pub;
  397. stg->av_class = &stream_group_class;
  398. av_opt_set_defaults(stg);
  399. stg->type = type;
  400. switch (type) {
  401. case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
  402. stg->params.iamf_audio_element = av_iamf_audio_element_alloc();
  403. if (!stg->params.iamf_audio_element)
  404. goto fail;
  405. break;
  406. case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
  407. stg->params.iamf_mix_presentation = av_iamf_mix_presentation_alloc();
  408. if (!stg->params.iamf_mix_presentation)
  409. goto fail;
  410. break;
  411. case AV_STREAM_GROUP_PARAMS_TILE_GRID:
  412. stg->params.tile_grid = av_mallocz(sizeof(*stg->params.tile_grid));
  413. if (!stg->params.tile_grid)
  414. goto fail;
  415. stg->params.tile_grid->av_class = &tile_grid_class;
  416. av_opt_set_defaults(stg->params.tile_grid);
  417. break;
  418. case AV_STREAM_GROUP_PARAMS_LCEVC:
  419. stg->params.lcevc = av_mallocz(sizeof(*stg->params.lcevc));
  420. if (!stg->params.lcevc)
  421. goto fail;
  422. stg->params.lcevc->av_class = &lcevc_class;
  423. av_opt_set_defaults(stg->params.lcevc);
  424. break;
  425. default:
  426. goto fail;
  427. }
  428. if (options) {
  429. if (av_opt_set_dict2(stg, options, AV_OPT_SEARCH_CHILDREN))
  430. goto fail;
  431. }
  432. stgi->fmtctx = s;
  433. stg->index = s->nb_stream_groups;
  434. s->stream_groups[s->nb_stream_groups++] = stg;
  435. return stg;
  436. fail:
  437. ff_free_stream_group(&stg);
  438. return NULL;
  439. }
  440. static int stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
  441. {
  442. AVStream **streams = av_realloc_array(stg->streams, stg->nb_streams + 1,
  443. sizeof(*stg->streams));
  444. if (!streams)
  445. return AVERROR(ENOMEM);
  446. stg->streams = streams;
  447. stg->streams[stg->nb_streams++] = st;
  448. return 0;
  449. }
  450. int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
  451. {
  452. const FFStreamGroup *stgi = cffstreamgroup(stg);
  453. const FFStream *sti = cffstream(st);
  454. if (stgi->fmtctx != sti->fmtctx)
  455. return AVERROR(EINVAL);
  456. for (int i = 0; i < stg->nb_streams; i++)
  457. if (stg->streams[i]->index == st->index)
  458. return AVERROR(EEXIST);
  459. return stream_group_add_stream(stg, st);
  460. }
  461. static int option_is_disposition(const AVOption *opt)
  462. {
  463. return opt->type == AV_OPT_TYPE_CONST &&
  464. opt->unit && !strcmp(opt->unit, "disposition");
  465. }
  466. int av_disposition_from_string(const char *disp)
  467. {
  468. for (const AVOption *opt = stream_options; opt->name; opt++)
  469. if (option_is_disposition(opt) && !strcmp(disp, opt->name))
  470. return opt->default_val.i64;
  471. return AVERROR(EINVAL);
  472. }
  473. const char *av_disposition_to_string(int disposition)
  474. {
  475. int val;
  476. if (disposition <= 0)
  477. return NULL;
  478. val = 1 << ff_ctz(disposition);
  479. for (const AVOption *opt = stream_options; opt->name; opt++)
  480. if (option_is_disposition(opt) && opt->default_val.i64 == val)
  481. return opt->name;
  482. return NULL;
  483. }