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.

369 lines
11KB

  1. /*
  2. * Copyright (c) 2001 Fabrice Bellard
  3. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Options definition for AVCodecContext.
  24. */
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/opt.h"
  31. #include <string.h>
  32. FF_DISABLE_DEPRECATION_WARNINGS
  33. #include "options_table.h"
  34. FF_ENABLE_DEPRECATION_WARNINGS
  35. static const char* context_to_name(void* ptr) {
  36. AVCodecContext *avc= ptr;
  37. if(avc && avc->codec && avc->codec->name)
  38. return avc->codec->name;
  39. else
  40. return "NULL";
  41. }
  42. static void *codec_child_next(void *obj, void *prev)
  43. {
  44. AVCodecContext *s = obj;
  45. if (!prev && s->codec && s->codec->priv_class && s->priv_data)
  46. return s->priv_data;
  47. return NULL;
  48. }
  49. #if FF_API_CHILD_CLASS_NEXT
  50. static const AVClass *codec_child_class_next(const AVClass *prev)
  51. {
  52. void *iter = NULL;
  53. const AVCodec *c = NULL;
  54. /* find the codec that corresponds to prev */
  55. while (prev && (c = av_codec_iterate(&iter)))
  56. if (c->priv_class == prev)
  57. break;
  58. /* find next codec with priv options */
  59. while (c = av_codec_iterate(&iter))
  60. if (c->priv_class)
  61. return c->priv_class;
  62. return NULL;
  63. }
  64. #endif
  65. static const AVClass *codec_child_class_iterate(void **iter)
  66. {
  67. const AVCodec *c;
  68. /* find next codec with priv options */
  69. while (c = av_codec_iterate(iter))
  70. if (c->priv_class)
  71. return c->priv_class;
  72. return NULL;
  73. }
  74. static AVClassCategory get_category(void *ptr)
  75. {
  76. AVCodecContext* avctx = ptr;
  77. if(avctx->codec && avctx->codec->decode) return AV_CLASS_CATEGORY_DECODER;
  78. else return AV_CLASS_CATEGORY_ENCODER;
  79. }
  80. static const AVClass av_codec_context_class = {
  81. .class_name = "AVCodecContext",
  82. .item_name = context_to_name,
  83. .option = avcodec_options,
  84. .version = LIBAVUTIL_VERSION_INT,
  85. .log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),
  86. .child_next = codec_child_next,
  87. #if FF_API_CHILD_CLASS_NEXT
  88. .child_class_next = codec_child_class_next,
  89. #endif
  90. .child_class_iterate = codec_child_class_iterate,
  91. .category = AV_CLASS_CATEGORY_ENCODER,
  92. .get_category = get_category,
  93. };
  94. static int init_context_defaults(AVCodecContext *s, const AVCodec *codec)
  95. {
  96. int flags=0;
  97. memset(s, 0, sizeof(AVCodecContext));
  98. s->av_class = &av_codec_context_class;
  99. s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
  100. if (codec) {
  101. s->codec = codec;
  102. s->codec_id = codec->id;
  103. }
  104. if(s->codec_type == AVMEDIA_TYPE_AUDIO)
  105. flags= AV_OPT_FLAG_AUDIO_PARAM;
  106. else if(s->codec_type == AVMEDIA_TYPE_VIDEO)
  107. flags= AV_OPT_FLAG_VIDEO_PARAM;
  108. else if(s->codec_type == AVMEDIA_TYPE_SUBTITLE)
  109. flags= AV_OPT_FLAG_SUBTITLE_PARAM;
  110. av_opt_set_defaults2(s, flags, flags);
  111. s->time_base = (AVRational){0,1};
  112. s->framerate = (AVRational){ 0, 1 };
  113. s->pkt_timebase = (AVRational){ 0, 1 };
  114. s->get_buffer2 = avcodec_default_get_buffer2;
  115. s->get_format = avcodec_default_get_format;
  116. s->get_encode_buffer = avcodec_default_get_encode_buffer;
  117. s->execute = avcodec_default_execute;
  118. s->execute2 = avcodec_default_execute2;
  119. s->sample_aspect_ratio = (AVRational){0,1};
  120. s->pix_fmt = AV_PIX_FMT_NONE;
  121. s->sw_pix_fmt = AV_PIX_FMT_NONE;
  122. s->sample_fmt = AV_SAMPLE_FMT_NONE;
  123. s->reordered_opaque = AV_NOPTS_VALUE;
  124. if(codec && codec->priv_data_size){
  125. if(!s->priv_data){
  126. s->priv_data= av_mallocz(codec->priv_data_size);
  127. if (!s->priv_data) {
  128. return AVERROR(ENOMEM);
  129. }
  130. }
  131. if(codec->priv_class){
  132. *(const AVClass**)s->priv_data = codec->priv_class;
  133. av_opt_set_defaults(s->priv_data);
  134. }
  135. }
  136. if (codec && codec->defaults) {
  137. int ret;
  138. const AVCodecDefault *d = codec->defaults;
  139. while (d->key) {
  140. ret = av_opt_set(s, d->key, d->value, 0);
  141. av_assert0(ret >= 0);
  142. d++;
  143. }
  144. }
  145. return 0;
  146. }
  147. #if FF_API_GET_CONTEXT_DEFAULTS
  148. int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec)
  149. {
  150. return init_context_defaults(s, codec);
  151. }
  152. #endif
  153. AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)
  154. {
  155. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  156. if (!avctx)
  157. return NULL;
  158. if (init_context_defaults(avctx, codec) < 0) {
  159. av_free(avctx);
  160. return NULL;
  161. }
  162. return avctx;
  163. }
  164. void avcodec_free_context(AVCodecContext **pavctx)
  165. {
  166. AVCodecContext *avctx = *pavctx;
  167. if (!avctx)
  168. return;
  169. avcodec_close(avctx);
  170. av_freep(&avctx->extradata);
  171. av_freep(&avctx->subtitle_header);
  172. av_freep(&avctx->intra_matrix);
  173. av_freep(&avctx->inter_matrix);
  174. av_freep(&avctx->rc_override);
  175. av_freep(pavctx);
  176. }
  177. #if FF_API_COPY_CONTEXT
  178. static void copy_context_reset(AVCodecContext *avctx)
  179. {
  180. int i;
  181. av_opt_free(avctx);
  182. #if FF_API_CODED_FRAME
  183. FF_DISABLE_DEPRECATION_WARNINGS
  184. av_frame_free(&avctx->coded_frame);
  185. FF_ENABLE_DEPRECATION_WARNINGS
  186. #endif
  187. av_freep(&avctx->rc_override);
  188. av_freep(&avctx->intra_matrix);
  189. av_freep(&avctx->inter_matrix);
  190. av_freep(&avctx->extradata);
  191. av_freep(&avctx->subtitle_header);
  192. av_buffer_unref(&avctx->hw_frames_ctx);
  193. av_buffer_unref(&avctx->hw_device_ctx);
  194. for (i = 0; i < avctx->nb_coded_side_data; i++)
  195. av_freep(&avctx->coded_side_data[i].data);
  196. av_freep(&avctx->coded_side_data);
  197. avctx->subtitle_header_size = 0;
  198. avctx->nb_coded_side_data = 0;
  199. avctx->extradata_size = 0;
  200. }
  201. int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
  202. {
  203. const AVCodec *orig_codec = dest->codec;
  204. uint8_t *orig_priv_data = dest->priv_data;
  205. if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
  206. av_log(dest, AV_LOG_ERROR,
  207. "Tried to copy AVCodecContext %p into already-initialized %p\n",
  208. src, dest);
  209. return AVERROR(EINVAL);
  210. }
  211. copy_context_reset(dest);
  212. memcpy(dest, src, sizeof(*dest));
  213. av_opt_copy(dest, src);
  214. dest->priv_data = orig_priv_data;
  215. dest->codec = orig_codec;
  216. if (orig_priv_data && src->codec && src->codec->priv_class &&
  217. dest->codec && dest->codec->priv_class)
  218. av_opt_copy(orig_priv_data, src->priv_data);
  219. /* set values specific to opened codecs back to their default state */
  220. dest->slice_offset = NULL;
  221. dest->hwaccel = NULL;
  222. dest->internal = NULL;
  223. #if FF_API_CODED_FRAME
  224. FF_DISABLE_DEPRECATION_WARNINGS
  225. dest->coded_frame = NULL;
  226. FF_ENABLE_DEPRECATION_WARNINGS
  227. #endif
  228. /* reallocate values that should be allocated separately */
  229. dest->extradata = NULL;
  230. dest->coded_side_data = NULL;
  231. dest->intra_matrix = NULL;
  232. dest->inter_matrix = NULL;
  233. dest->rc_override = NULL;
  234. dest->subtitle_header = NULL;
  235. dest->hw_frames_ctx = NULL;
  236. dest->hw_device_ctx = NULL;
  237. dest->nb_coded_side_data = 0;
  238. #define alloc_and_copy_or_fail(obj, size, pad) \
  239. if (src->obj && size > 0) { \
  240. dest->obj = av_malloc(size + pad); \
  241. if (!dest->obj) \
  242. goto fail; \
  243. memcpy(dest->obj, src->obj, size); \
  244. if (pad) \
  245. memset(((uint8_t *) dest->obj) + size, 0, pad); \
  246. }
  247. alloc_and_copy_or_fail(extradata, src->extradata_size,
  248. AV_INPUT_BUFFER_PADDING_SIZE);
  249. dest->extradata_size = src->extradata_size;
  250. alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
  251. alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
  252. alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
  253. alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1);
  254. av_assert0(dest->subtitle_header_size == src->subtitle_header_size);
  255. #undef alloc_and_copy_or_fail
  256. if (src->hw_frames_ctx) {
  257. dest->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  258. if (!dest->hw_frames_ctx)
  259. goto fail;
  260. }
  261. return 0;
  262. fail:
  263. copy_context_reset(dest);
  264. return AVERROR(ENOMEM);
  265. }
  266. #endif
  267. const AVClass *avcodec_get_class(void)
  268. {
  269. return &av_codec_context_class;
  270. }
  271. #if FF_API_GET_FRAME_CLASS
  272. #define FOFFSET(x) offsetof(AVFrame,x)
  273. static const AVOption frame_options[]={
  274. {"best_effort_timestamp", "", FOFFSET(best_effort_timestamp), AV_OPT_TYPE_INT64, {.i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, 0},
  275. {"pkt_pos", "", FOFFSET(pkt_pos), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
  276. {"pkt_size", "", FOFFSET(pkt_size), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
  277. {"sample_aspect_ratio", "", FOFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, {.dbl = 0 }, 0, INT_MAX, 0},
  278. {"width", "", FOFFSET(width), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  279. {"height", "", FOFFSET(height), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  280. {"format", "", FOFFSET(format), AV_OPT_TYPE_INT, {.i64 = -1 }, 0, INT_MAX, 0},
  281. {"channel_layout", "", FOFFSET(channel_layout), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, 0},
  282. {"sample_rate", "", FOFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  283. {NULL},
  284. };
  285. static const AVClass av_frame_class = {
  286. .class_name = "AVFrame",
  287. .item_name = NULL,
  288. .option = frame_options,
  289. .version = LIBAVUTIL_VERSION_INT,
  290. };
  291. const AVClass *avcodec_get_frame_class(void)
  292. {
  293. return &av_frame_class;
  294. }
  295. #endif
  296. #define SROFFSET(x) offsetof(AVSubtitleRect,x)
  297. static const AVOption subtitle_rect_options[]={
  298. {"x", "", SROFFSET(x), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  299. {"y", "", SROFFSET(y), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  300. {"w", "", SROFFSET(w), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  301. {"h", "", SROFFSET(h), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  302. {"type", "", SROFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  303. {"flags", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0, "flags"},
  304. {"forced", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0},
  305. {NULL},
  306. };
  307. static const AVClass av_subtitle_rect_class = {
  308. .class_name = "AVSubtitleRect",
  309. .item_name = NULL,
  310. .option = subtitle_rect_options,
  311. .version = LIBAVUTIL_VERSION_INT,
  312. };
  313. const AVClass *avcodec_get_subtitle_rect_class(void)
  314. {
  315. return &av_subtitle_rect_class;
  316. }