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.

197 lines
6.0KB

  1. /*
  2. * Copyright (c) 2001 Fabrice Bellard
  3. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/opt.h"
  29. #include <float.h> /* FLT_MIN, FLT_MAX */
  30. #include "options_table.h"
  31. static const char* context_to_name(void* ptr) {
  32. AVCodecContext *avc= ptr;
  33. if(avc && avc->codec && avc->codec->name)
  34. return avc->codec->name;
  35. else
  36. return "NULL";
  37. }
  38. static void *codec_child_next(void *obj, void *prev)
  39. {
  40. AVCodecContext *s = obj;
  41. if (!prev && s->codec && s->codec->priv_class && s->priv_data)
  42. return s->priv_data;
  43. return NULL;
  44. }
  45. static const AVClass *codec_child_class_next(const AVClass *prev)
  46. {
  47. AVCodec *c = NULL;
  48. /* find the codec that corresponds to prev */
  49. while (prev && (c = av_codec_next(c)))
  50. if (c->priv_class == prev)
  51. break;
  52. /* find next codec with priv options */
  53. while (c = av_codec_next(c))
  54. if (c->priv_class)
  55. return c->priv_class;
  56. return NULL;
  57. }
  58. static const AVClass av_codec_context_class = {
  59. .class_name = "AVCodecContext",
  60. .item_name = context_to_name,
  61. .option = options,
  62. .version = LIBAVUTIL_VERSION_INT,
  63. .log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),
  64. .child_next = codec_child_next,
  65. .child_class_next = codec_child_class_next,
  66. };
  67. int avcodec_get_context_defaults3(AVCodecContext *s, AVCodec *codec){
  68. memset(s, 0, sizeof(AVCodecContext));
  69. s->av_class = &av_codec_context_class;
  70. s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
  71. s->codec = codec;
  72. av_opt_set_defaults(s);
  73. s->time_base = (AVRational){0,1};
  74. s->get_buffer = avcodec_default_get_buffer;
  75. s->release_buffer = avcodec_default_release_buffer;
  76. s->get_format = avcodec_default_get_format;
  77. s->execute = avcodec_default_execute;
  78. s->execute2 = avcodec_default_execute2;
  79. s->sample_aspect_ratio = (AVRational){0,1};
  80. s->pix_fmt = PIX_FMT_NONE;
  81. s->sample_fmt = AV_SAMPLE_FMT_NONE;
  82. s->reget_buffer = avcodec_default_reget_buffer;
  83. s->reordered_opaque = AV_NOPTS_VALUE;
  84. if(codec && codec->priv_data_size){
  85. if(!s->priv_data){
  86. s->priv_data= av_mallocz(codec->priv_data_size);
  87. if (!s->priv_data) {
  88. return AVERROR(ENOMEM);
  89. }
  90. }
  91. if(codec->priv_class){
  92. *(const AVClass**)s->priv_data = codec->priv_class;
  93. av_opt_set_defaults(s->priv_data);
  94. }
  95. }
  96. if (codec && codec->defaults) {
  97. int ret;
  98. const AVCodecDefault *d = codec->defaults;
  99. while (d->key) {
  100. ret = av_opt_set(s, d->key, d->value, 0);
  101. av_assert0(ret >= 0);
  102. d++;
  103. }
  104. }
  105. return 0;
  106. }
  107. AVCodecContext *avcodec_alloc_context3(AVCodec *codec){
  108. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  109. if(avctx==NULL) return NULL;
  110. if(avcodec_get_context_defaults3(avctx, codec) < 0){
  111. av_free(avctx);
  112. return NULL;
  113. }
  114. return avctx;
  115. }
  116. int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
  117. {
  118. if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
  119. av_log(dest, AV_LOG_ERROR,
  120. "Tried to copy AVCodecContext %p into already-initialized %p\n",
  121. src, dest);
  122. return AVERROR(EINVAL);
  123. }
  124. memcpy(dest, src, sizeof(*dest));
  125. /* set values specific to opened codecs back to their default state */
  126. dest->priv_data = NULL;
  127. dest->codec = NULL;
  128. dest->slice_offset = NULL;
  129. dest->hwaccel = NULL;
  130. dest->thread_opaque = NULL;
  131. dest->internal = NULL;
  132. /* reallocate values that should be allocated separately */
  133. dest->rc_eq = NULL;
  134. dest->extradata = NULL;
  135. dest->intra_matrix = NULL;
  136. dest->inter_matrix = NULL;
  137. dest->rc_override = NULL;
  138. if (src->rc_eq) {
  139. dest->rc_eq = av_strdup(src->rc_eq);
  140. if (!dest->rc_eq)
  141. return AVERROR(ENOMEM);
  142. }
  143. #define alloc_and_copy_or_fail(obj, size, pad) \
  144. if (src->obj && size > 0) { \
  145. dest->obj = av_malloc(size + pad); \
  146. if (!dest->obj) \
  147. goto fail; \
  148. memcpy(dest->obj, src->obj, size); \
  149. if (pad) \
  150. memset(((uint8_t *) dest->obj) + size, 0, pad); \
  151. }
  152. alloc_and_copy_or_fail(extradata, src->extradata_size,
  153. FF_INPUT_BUFFER_PADDING_SIZE);
  154. alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
  155. alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
  156. alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
  157. #undef alloc_and_copy_or_fail
  158. return 0;
  159. fail:
  160. av_freep(&dest->rc_override);
  161. av_freep(&dest->intra_matrix);
  162. av_freep(&dest->inter_matrix);
  163. av_freep(&dest->extradata);
  164. av_freep(&dest->rc_eq);
  165. return AVERROR(ENOMEM);
  166. }
  167. const AVClass *avcodec_get_class(void)
  168. {
  169. return &av_codec_context_class;
  170. }