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.

195 lines
6.1KB

  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. #include "options.c"
  22. static int dummy_init(AVCodecContext *ctx)
  23. {
  24. //TODO: this code should set every possible pointer that could be set by codec and is not an option;
  25. ctx->extradata_size = 8;
  26. ctx->extradata = av_malloc(ctx->extradata_size);
  27. return 0;
  28. }
  29. static int dummy_close(AVCodecContext *ctx)
  30. {
  31. av_freep(&ctx->extradata);
  32. ctx->extradata_size = 0;
  33. return 0;
  34. }
  35. static int dummy_encode(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
  36. {
  37. return AVERROR(ENOSYS);
  38. }
  39. typedef struct Dummy12Context {
  40. AVClass *av_class;
  41. int num;
  42. char* str;
  43. } Dummy12Context;
  44. typedef struct Dummy3Context {
  45. void *fake_av_class;
  46. int num;
  47. char* str;
  48. } Dummy3Context;
  49. #define OFFSET(x) offsetof(Dummy12Context, x)
  50. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  51. static const AVOption dummy_options[] = {
  52. { "str", "set str", OFFSET(str), AV_OPT_TYPE_STRING, { .str = "i'm src default value" }, 0, 0, VE},
  53. { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 1500100900 }, 0, INT_MAX, VE},
  54. { NULL },
  55. };
  56. static const AVClass dummy_v1_class = {
  57. .class_name = "dummy_v1_class",
  58. .item_name = av_default_item_name,
  59. .option = dummy_options,
  60. .version = LIBAVUTIL_VERSION_INT,
  61. };
  62. static const AVClass dummy_v2_class = {
  63. .class_name = "dummy_v2_class",
  64. .item_name = av_default_item_name,
  65. .option = dummy_options,
  66. .version = LIBAVUTIL_VERSION_INT,
  67. };
  68. /* codec with options */
  69. static AVCodec dummy_v1_encoder = {
  70. .name = "dummy_v1_codec",
  71. .type = AVMEDIA_TYPE_VIDEO,
  72. .id = AV_CODEC_ID_NONE - 1,
  73. .encode2 = dummy_encode,
  74. .init = dummy_init,
  75. .close = dummy_close,
  76. .priv_class = &dummy_v1_class,
  77. .priv_data_size = sizeof(Dummy12Context),
  78. };
  79. /* codec with options, different class */
  80. static AVCodec dummy_v2_encoder = {
  81. .name = "dummy_v2_codec",
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. .id = AV_CODEC_ID_NONE - 2,
  84. .encode2 = dummy_encode,
  85. .init = dummy_init,
  86. .close = dummy_close,
  87. .priv_class = &dummy_v2_class,
  88. .priv_data_size = sizeof(Dummy12Context),
  89. };
  90. /* codec with priv data, but no class */
  91. static AVCodec dummy_v3_encoder = {
  92. .name = "dummy_v3_codec",
  93. .type = AVMEDIA_TYPE_VIDEO,
  94. .id = AV_CODEC_ID_NONE - 3,
  95. .encode2 = dummy_encode,
  96. .init = dummy_init,
  97. .close = dummy_close,
  98. .priv_data_size = sizeof(Dummy3Context),
  99. };
  100. /* codec without priv data */
  101. static AVCodec dummy_v4_encoder = {
  102. .name = "dummy_v4_codec",
  103. .type = AVMEDIA_TYPE_VIDEO,
  104. .id = AV_CODEC_ID_NONE - 4,
  105. .encode2 = dummy_encode,
  106. .init = dummy_init,
  107. .close = dummy_close,
  108. };
  109. static void test_copy_print_codec(const AVCodecContext *ctx)
  110. {
  111. printf("%-14s: %dx%d prv: %s",
  112. ctx->codec ? ctx->codec->name : "NULL",
  113. ctx->width, ctx->height,
  114. ctx->priv_data ? "set" : "null");
  115. if (ctx->codec && ctx->codec->priv_class && ctx->codec->priv_data_size) {
  116. int64_t i64;
  117. char *str = NULL;
  118. av_opt_get_int(ctx->priv_data, "num", 0, &i64);
  119. av_opt_get(ctx->priv_data, "str", 0, (uint8_t**)&str);
  120. printf(" opts: %"PRId64" %s", i64, str);
  121. av_free(str);
  122. }
  123. printf("\n");
  124. }
  125. static void test_copy(const AVCodec *c1, const AVCodec *c2)
  126. {
  127. AVCodecContext *ctx1, *ctx2;
  128. printf("%s -> %s\nclosed:\n", c1 ? c1->name : "NULL", c2 ? c2->name : "NULL");
  129. ctx1 = avcodec_alloc_context3(c1);
  130. ctx2 = avcodec_alloc_context3(c2);
  131. ctx1->width = ctx1->height = 128;
  132. if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
  133. av_opt_set(ctx2->priv_data, "num", "667", 0);
  134. av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
  135. }
  136. avcodec_copy_context(ctx2, ctx1);
  137. test_copy_print_codec(ctx1);
  138. test_copy_print_codec(ctx2);
  139. if (ctx1->codec) {
  140. int ret;
  141. printf("opened:\n");
  142. ret = avcodec_open2(ctx1, ctx1->codec, NULL);
  143. if (ret < 0) {
  144. fprintf(stderr, "avcodec_open2 failed\n");
  145. exit(1);
  146. }
  147. if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
  148. av_opt_set(ctx2->priv_data, "num", "667", 0);
  149. av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
  150. }
  151. avcodec_copy_context(ctx2, ctx1);
  152. test_copy_print_codec(ctx1);
  153. test_copy_print_codec(ctx2);
  154. avcodec_close(ctx1);
  155. }
  156. avcodec_free_context(&ctx1);
  157. avcodec_free_context(&ctx2);
  158. }
  159. int main(void)
  160. {
  161. AVCodec *dummy_codec[] = {
  162. &dummy_v1_encoder,
  163. &dummy_v2_encoder,
  164. &dummy_v3_encoder,
  165. &dummy_v4_encoder,
  166. NULL,
  167. };
  168. int i, j;
  169. for (i = 0; dummy_codec[i]; i++)
  170. avcodec_register(dummy_codec[i]);
  171. printf("testing avcodec_copy_context()\n");
  172. for (i = 0; i < FF_ARRAY_ELEMS(dummy_codec); i++)
  173. for (j = 0; j < FF_ARRAY_ELEMS(dummy_codec); j++)
  174. test_copy(dummy_codec[i], dummy_codec[j]);
  175. return 0;
  176. }