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.

198 lines
5.9KB

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