You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.5KB

  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 "internal.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/internal.h"
  25. #include "libavutil/opt.h"
  26. /**
  27. * @file
  28. * Options definition for AVFormatContext.
  29. */
  30. FF_DISABLE_DEPRECATION_WARNINGS
  31. #include "options_table.h"
  32. FF_ENABLE_DEPRECATION_WARNINGS
  33. static const char* format_to_name(void* ptr)
  34. {
  35. AVFormatContext* fc = (AVFormatContext*) ptr;
  36. if(fc->iformat) return fc->iformat->name;
  37. else if(fc->oformat) return fc->oformat->name;
  38. else return "NULL";
  39. }
  40. static void *format_child_next(void *obj, void *prev)
  41. {
  42. AVFormatContext *s = obj;
  43. if (!prev && s->priv_data &&
  44. ((s->iformat && s->iformat->priv_class) ||
  45. s->oformat && s->oformat->priv_class))
  46. return s->priv_data;
  47. if (s->pb && s->pb->av_class && prev != s->pb)
  48. return s->pb;
  49. return NULL;
  50. }
  51. #if FF_API_CHILD_CLASS_NEXT
  52. static const AVClass *format_child_class_next(const AVClass *prev)
  53. {
  54. const AVInputFormat *ifmt = NULL;
  55. const AVOutputFormat *ofmt = NULL;
  56. void *ifmt_iter = NULL, *ofmt_iter = NULL;
  57. if (!prev)
  58. return &ff_avio_class;
  59. while ((ifmt = av_demuxer_iterate(&ifmt_iter)))
  60. if (ifmt->priv_class == prev)
  61. break;
  62. if (!ifmt) {
  63. ifmt_iter = NULL;
  64. while ((ofmt = av_muxer_iterate(&ofmt_iter)))
  65. if (ofmt->priv_class == prev)
  66. break;
  67. }
  68. if (!ofmt) {
  69. ofmt_iter = NULL;
  70. while ((ifmt = av_demuxer_iterate(&ifmt_iter)))
  71. if (ifmt->priv_class)
  72. return ifmt->priv_class;
  73. }
  74. while ((ofmt = av_muxer_iterate(&ofmt_iter)))
  75. if (ofmt->priv_class)
  76. return ofmt->priv_class;
  77. return NULL;
  78. }
  79. #endif
  80. enum {
  81. CHILD_CLASS_ITER_AVIO = 0,
  82. CHILD_CLASS_ITER_MUX,
  83. CHILD_CLASS_ITER_DEMUX,
  84. CHILD_CLASS_ITER_DONE,
  85. };
  86. #define ITER_STATE_SHIFT 16
  87. static const AVClass *format_child_class_iterate(void **iter)
  88. {
  89. // we use the low 16 bits of iter as the value to be passed to
  90. // av_(de)muxer_iterate()
  91. void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
  92. unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
  93. const AVClass *ret = NULL;
  94. if (state == CHILD_CLASS_ITER_AVIO) {
  95. ret = &ff_avio_class;
  96. state++;
  97. goto finish;
  98. }
  99. if (state == CHILD_CLASS_ITER_MUX) {
  100. const AVOutputFormat *ofmt;
  101. while ((ofmt = av_muxer_iterate(&val))) {
  102. ret = ofmt->priv_class;
  103. if (ret)
  104. goto finish;
  105. }
  106. val = NULL;
  107. state++;
  108. }
  109. if (state == CHILD_CLASS_ITER_DEMUX) {
  110. const AVInputFormat *ifmt;
  111. while ((ifmt = av_demuxer_iterate(&val))) {
  112. ret = ifmt->priv_class;
  113. if (ret)
  114. goto finish;
  115. }
  116. val = NULL;
  117. state++;
  118. }
  119. finish:
  120. // make sure none av_(de)muxer_iterate does not set the high bits of val
  121. av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
  122. *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
  123. return ret;
  124. }
  125. static AVClassCategory get_category(void *ptr)
  126. {
  127. AVFormatContext* s = ptr;
  128. if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
  129. else return AV_CLASS_CATEGORY_MUXER;
  130. }
  131. static const AVClass av_format_context_class = {
  132. .class_name = "AVFormatContext",
  133. .item_name = format_to_name,
  134. .option = avformat_options,
  135. .version = LIBAVUTIL_VERSION_INT,
  136. .child_next = format_child_next,
  137. #if FF_API_CHILD_CLASS_NEXT
  138. .child_class_next = format_child_class_next,
  139. #endif
  140. .child_class_iterate = format_child_class_iterate,
  141. .category = AV_CLASS_CATEGORY_MUXER,
  142. .get_category = get_category,
  143. };
  144. static int io_open_default(AVFormatContext *s, AVIOContext **pb,
  145. const char *url, int flags, AVDictionary **options)
  146. {
  147. int loglevel;
  148. if (!strcmp(url, s->url) ||
  149. s->iformat && !strcmp(s->iformat->name, "image2") ||
  150. s->oformat && !strcmp(s->oformat->name, "image2")
  151. ) {
  152. loglevel = AV_LOG_DEBUG;
  153. } else
  154. loglevel = AV_LOG_INFO;
  155. av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
  156. #if FF_API_OLD_OPEN_CALLBACKS
  157. FF_DISABLE_DEPRECATION_WARNINGS
  158. if (s->open_cb)
  159. return s->open_cb(s, pb, url, flags, &s->interrupt_callback, options);
  160. FF_ENABLE_DEPRECATION_WARNINGS
  161. #endif
  162. return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
  163. }
  164. static void io_close_default(AVFormatContext *s, AVIOContext *pb)
  165. {
  166. avio_close(pb);
  167. }
  168. static void avformat_get_context_defaults(AVFormatContext *s)
  169. {
  170. memset(s, 0, sizeof(AVFormatContext));
  171. s->av_class = &av_format_context_class;
  172. s->io_open = io_open_default;
  173. s->io_close = io_close_default;
  174. av_opt_set_defaults(s);
  175. }
  176. AVFormatContext *avformat_alloc_context(void)
  177. {
  178. AVFormatContext *ic;
  179. AVFormatInternal *internal;
  180. ic = av_malloc(sizeof(AVFormatContext));
  181. if (!ic) return ic;
  182. internal = av_mallocz(sizeof(*internal));
  183. if (!internal) {
  184. av_free(ic);
  185. return NULL;
  186. }
  187. internal->pkt = av_packet_alloc();
  188. if (!internal->pkt) {
  189. av_free(internal);
  190. av_free(ic);
  191. return NULL;
  192. }
  193. avformat_get_context_defaults(ic);
  194. ic->internal = internal;
  195. ic->internal->offset = AV_NOPTS_VALUE;
  196. ic->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
  197. ic->internal->shortest_end = AV_NOPTS_VALUE;
  198. return ic;
  199. }
  200. enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
  201. {
  202. return ctx->duration_estimation_method;
  203. }
  204. const AVClass *avformat_get_class(void)
  205. {
  206. return &av_format_context_class;
  207. }