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.

201 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/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 = 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_buffer = avcodec_default_get_buffer;
  78. s->release_buffer = avcodec_default_release_buffer;
  79. s->get_format = avcodec_default_get_format;
  80. s->execute = avcodec_default_execute;
  81. s->execute2 = avcodec_default_execute2;
  82. s->sample_aspect_ratio = (AVRational){0,1};
  83. s->pix_fmt = AV_PIX_FMT_NONE;
  84. s->sample_fmt = AV_SAMPLE_FMT_NONE;
  85. s->reget_buffer = avcodec_default_reget_buffer;
  86. s->reordered_opaque = AV_NOPTS_VALUE;
  87. if(codec && codec->priv_data_size){
  88. if(!s->priv_data){
  89. s->priv_data= av_mallocz(codec->priv_data_size);
  90. if (!s->priv_data) {
  91. return AVERROR(ENOMEM);
  92. }
  93. }
  94. if(codec->priv_class){
  95. *(const AVClass**)s->priv_data = codec->priv_class;
  96. av_opt_set_defaults(s->priv_data);
  97. }
  98. }
  99. if (codec && codec->defaults) {
  100. int ret;
  101. const AVCodecDefault *d = codec->defaults;
  102. while (d->key) {
  103. ret = av_opt_set(s, d->key, d->value, 0);
  104. av_assert0(ret >= 0);
  105. d++;
  106. }
  107. }
  108. return 0;
  109. }
  110. AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)
  111. {
  112. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  113. if(avctx==NULL) return NULL;
  114. if(avcodec_get_context_defaults3(avctx, codec) < 0){
  115. av_free(avctx);
  116. return NULL;
  117. }
  118. return avctx;
  119. }
  120. int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
  121. {
  122. if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
  123. av_log(dest, AV_LOG_ERROR,
  124. "Tried to copy AVCodecContext %p into already-initialized %p\n",
  125. src, dest);
  126. return AVERROR(EINVAL);
  127. }
  128. memcpy(dest, src, sizeof(*dest));
  129. /* set values specific to opened codecs back to their default state */
  130. dest->priv_data = NULL;
  131. dest->codec = NULL;
  132. dest->slice_offset = NULL;
  133. dest->hwaccel = NULL;
  134. dest->thread_opaque = NULL;
  135. dest->internal = NULL;
  136. /* reallocate values that should be allocated separately */
  137. dest->rc_eq = NULL;
  138. dest->extradata = NULL;
  139. dest->intra_matrix = NULL;
  140. dest->inter_matrix = NULL;
  141. dest->rc_override = NULL;
  142. if (src->rc_eq) {
  143. dest->rc_eq = av_strdup(src->rc_eq);
  144. if (!dest->rc_eq)
  145. return AVERROR(ENOMEM);
  146. }
  147. #define alloc_and_copy_or_fail(obj, size, pad) \
  148. if (src->obj && size > 0) { \
  149. dest->obj = av_malloc(size + pad); \
  150. if (!dest->obj) \
  151. goto fail; \
  152. memcpy(dest->obj, src->obj, size); \
  153. if (pad) \
  154. memset(((uint8_t *) dest->obj) + size, 0, pad); \
  155. }
  156. alloc_and_copy_or_fail(extradata, src->extradata_size,
  157. FF_INPUT_BUFFER_PADDING_SIZE);
  158. alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
  159. alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
  160. alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
  161. #undef alloc_and_copy_or_fail
  162. return 0;
  163. fail:
  164. av_freep(&dest->rc_override);
  165. av_freep(&dest->intra_matrix);
  166. av_freep(&dest->inter_matrix);
  167. av_freep(&dest->extradata);
  168. av_freep(&dest->rc_eq);
  169. return AVERROR(ENOMEM);
  170. }
  171. const AVClass *avcodec_get_class(void)
  172. {
  173. return &av_codec_context_class;
  174. }