options.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "avio_internal.h"
  22. #include "demux.h"
  23. #include "internal.h"
  24. #include "libavcodec/avcodec.h"
  25. #include "libavcodec/codec_par.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/intmath.h"
  29. #include "libavutil/opt.h"
  30. /**
  31. * @file
  32. * Options definition for AVFormatContext.
  33. */
  34. FF_DISABLE_DEPRECATION_WARNINGS
  35. #include "options_table.h"
  36. FF_ENABLE_DEPRECATION_WARNINGS
  37. static const char* format_to_name(void* ptr)
  38. {
  39. AVFormatContext* fc = (AVFormatContext*) ptr;
  40. if(fc->iformat) return fc->iformat->name;
  41. else if(fc->oformat) return fc->oformat->name;
  42. else return "NULL";
  43. }
  44. static void *format_child_next(void *obj, void *prev)
  45. {
  46. AVFormatContext *s = obj;
  47. if (!prev && s->priv_data &&
  48. ((s->iformat && s->iformat->priv_class) ||
  49. s->oformat && s->oformat->priv_class))
  50. return s->priv_data;
  51. if (s->pb && s->pb->av_class && prev != s->pb)
  52. return s->pb;
  53. return NULL;
  54. }
  55. enum {
  56. CHILD_CLASS_ITER_AVIO = 0,
  57. CHILD_CLASS_ITER_MUX,
  58. CHILD_CLASS_ITER_DEMUX,
  59. CHILD_CLASS_ITER_DONE,
  60. };
  61. #define ITER_STATE_SHIFT 16
  62. static const AVClass *format_child_class_iterate(void **iter)
  63. {
  64. // we use the low 16 bits of iter as the value to be passed to
  65. // av_(de)muxer_iterate()
  66. void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
  67. unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
  68. const AVClass *ret = NULL;
  69. if (state == CHILD_CLASS_ITER_AVIO) {
  70. ret = &ff_avio_class;
  71. state++;
  72. goto finish;
  73. }
  74. if (state == CHILD_CLASS_ITER_MUX) {
  75. const AVOutputFormat *ofmt;
  76. while ((ofmt = av_muxer_iterate(&val))) {
  77. ret = ofmt->priv_class;
  78. if (ret)
  79. goto finish;
  80. }
  81. val = NULL;
  82. state++;
  83. }
  84. if (state == CHILD_CLASS_ITER_DEMUX) {
  85. const AVInputFormat *ifmt;
  86. while ((ifmt = av_demuxer_iterate(&val))) {
  87. ret = ifmt->priv_class;
  88. if (ret)
  89. goto finish;
  90. }
  91. val = NULL;
  92. state++;
  93. }
  94. finish:
  95. // make sure none av_(de)muxer_iterate does not set the high bits of val
  96. av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
  97. *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
  98. return ret;
  99. }
  100. static AVClassCategory get_category(void *ptr)
  101. {
  102. AVFormatContext* s = ptr;
  103. if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
  104. else return AV_CLASS_CATEGORY_MUXER;
  105. }
  106. static const AVClass av_format_context_class = {
  107. .class_name = "AVFormatContext",
  108. .item_name = format_to_name,
  109. .option = avformat_options,
  110. .version = LIBAVUTIL_VERSION_INT,
  111. .child_next = format_child_next,
  112. .child_class_iterate = format_child_class_iterate,
  113. .category = AV_CLASS_CATEGORY_MUXER,
  114. .get_category = get_category,
  115. };
  116. static int io_open_default(AVFormatContext *s, AVIOContext **pb,
  117. const char *url, int flags, AVDictionary **options)
  118. {
  119. int loglevel;
  120. if (!strcmp(url, s->url) ||
  121. s->iformat && !strcmp(s->iformat->name, "image2") ||
  122. s->oformat && !strcmp(s->oformat->name, "image2")
  123. ) {
  124. loglevel = AV_LOG_DEBUG;
  125. } else
  126. loglevel = AV_LOG_INFO;
  127. av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
  128. return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
  129. }
  130. void ff_format_io_close_default(AVFormatContext *s, AVIOContext *pb)
  131. {
  132. avio_close(pb);
  133. }
  134. static int io_close2_default(AVFormatContext *s, AVIOContext *pb)
  135. {
  136. return avio_close(pb);
  137. }
  138. AVFormatContext *avformat_alloc_context(void)
  139. {
  140. FFFormatContext *const si = av_mallocz(sizeof(*si));
  141. AVFormatContext *s;
  142. if (!si)
  143. return NULL;
  144. s = &si->pub;
  145. s->av_class = &av_format_context_class;
  146. s->io_open = io_open_default;
  147. s->io_close = ff_format_io_close_default;
  148. s->io_close2= io_close2_default;
  149. av_opt_set_defaults(s);
  150. si->pkt = av_packet_alloc();
  151. si->parse_pkt = av_packet_alloc();
  152. if (!si->pkt || !si->parse_pkt) {
  153. avformat_free_context(s);
  154. return NULL;
  155. }
  156. si->shortest_end = AV_NOPTS_VALUE;
  157. return s;
  158. }
  159. enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
  160. {
  161. return ctx->duration_estimation_method;
  162. }
  163. const AVClass *avformat_get_class(void)
  164. {
  165. return &av_format_context_class;
  166. }
  167. static const AVOption stream_options[] = {
  168. { "disposition", NULL, offsetof(AVStream, disposition), AV_OPT_TYPE_FLAGS, { .i64 = 0 },
  169. .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "disposition" },
  170. { "default", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT }, .unit = "disposition" },
  171. { "dub", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB }, .unit = "disposition" },
  172. { "original", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL }, .unit = "disposition" },
  173. { "comment", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT }, .unit = "disposition" },
  174. { "lyrics", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS }, .unit = "disposition" },
  175. { "karaoke", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE }, .unit = "disposition" },
  176. { "forced", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED }, .unit = "disposition" },
  177. { "hearing_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED }, .unit = "disposition" },
  178. { "visual_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED }, .unit = "disposition" },
  179. { "clean_effects", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS }, .unit = "disposition" },
  180. { "attached_pic", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC }, .unit = "disposition" },
  181. { "timed_thumbnails", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_TIMED_THUMBNAILS }, .unit = "disposition" },
  182. { "captions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, .unit = "disposition" },
  183. { "descriptions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, .unit = "disposition" },
  184. { "metadata", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, .unit = "disposition" },
  185. { "dependent", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT }, .unit = "disposition" },
  186. { "still_image", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_STILL_IMAGE }, .unit = "disposition" },
  187. { NULL }
  188. };
  189. static const AVClass stream_class = {
  190. .class_name = "AVStream",
  191. .item_name = av_default_item_name,
  192. .version = LIBAVUTIL_VERSION_INT,
  193. .option = stream_options,
  194. };
  195. const AVClass *av_stream_get_class(void)
  196. {
  197. return &stream_class;
  198. }
  199. AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
  200. {
  201. FFFormatContext *const si = ffformatcontext(s);
  202. FFStream *sti;
  203. AVStream *st;
  204. AVStream **streams;
  205. if (s->nb_streams >= s->max_streams) {
  206. av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter"
  207. " (%d), see the documentation if you wish to increase it\n",
  208. s->max_streams);
  209. return NULL;
  210. }
  211. streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
  212. if (!streams)
  213. return NULL;
  214. s->streams = streams;
  215. sti = av_mallocz(sizeof(*sti));
  216. if (!sti)
  217. return NULL;
  218. st = &sti->pub;
  219. #if FF_API_AVSTREAM_CLASS
  220. st->av_class = &stream_class;
  221. #endif
  222. st->codecpar = avcodec_parameters_alloc();
  223. if (!st->codecpar)
  224. goto fail;
  225. sti->avctx = avcodec_alloc_context3(NULL);
  226. if (!sti->avctx)
  227. goto fail;
  228. if (s->iformat) {
  229. sti->info = av_mallocz(sizeof(*sti->info));
  230. if (!sti->info)
  231. goto fail;
  232. #if FF_API_R_FRAME_RATE
  233. sti->info->last_dts = AV_NOPTS_VALUE;
  234. #endif
  235. sti->info->fps_first_dts = AV_NOPTS_VALUE;
  236. sti->info->fps_last_dts = AV_NOPTS_VALUE;
  237. /* default pts setting is MPEG-like */
  238. avpriv_set_pts_info(st, 33, 1, 90000);
  239. /* we set the current DTS to 0 so that formats without any timestamps
  240. * but durations get some timestamps, formats with some unknown
  241. * timestamps have their first few packets buffered and the
  242. * timestamps corrected before they are returned to the user */
  243. sti->cur_dts = RELATIVE_TS_BASE;
  244. } else {
  245. sti->cur_dts = AV_NOPTS_VALUE;
  246. }
  247. st->index = s->nb_streams;
  248. st->start_time = AV_NOPTS_VALUE;
  249. st->duration = AV_NOPTS_VALUE;
  250. sti->first_dts = AV_NOPTS_VALUE;
  251. sti->probe_packets = s->max_probe_packets;
  252. sti->pts_wrap_reference = AV_NOPTS_VALUE;
  253. sti->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
  254. sti->last_IP_pts = AV_NOPTS_VALUE;
  255. sti->last_dts_for_order_check = AV_NOPTS_VALUE;
  256. for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
  257. sti->pts_buffer[i] = AV_NOPTS_VALUE;
  258. st->sample_aspect_ratio = (AVRational) { 0, 1 };
  259. sti->inject_global_side_data = si->inject_global_side_data;
  260. sti->need_context_update = 1;
  261. s->streams[s->nb_streams++] = st;
  262. return st;
  263. fail:
  264. ff_free_stream(&st);
  265. return NULL;
  266. }
  267. static int option_is_disposition(const AVOption *opt)
  268. {
  269. return opt->type == AV_OPT_TYPE_CONST &&
  270. opt->unit && !strcmp(opt->unit, "disposition");
  271. }
  272. int av_disposition_from_string(const char *disp)
  273. {
  274. for (const AVOption *opt = stream_options; opt->name; opt++)
  275. if (option_is_disposition(opt) && !strcmp(disp, opt->name))
  276. return opt->default_val.i64;
  277. return AVERROR(EINVAL);
  278. }
  279. const char *av_disposition_to_string(int disposition)
  280. {
  281. int val;
  282. if (disposition <= 0)
  283. return NULL;
  284. val = 1 << ff_ctz(disposition);
  285. for (const AVOption *opt = stream_options; opt->name; opt++)
  286. if (option_is_disposition(opt) && opt->default_val.i64 == val)
  287. return opt->name;
  288. return NULL;
  289. }