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.

483 lines
15KB

  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 <float.h> /* FLT_MIN, FLT_MAX */
  32. #include <string.h>
  33. FF_DISABLE_DEPRECATION_WARNINGS
  34. #include "options_table.h"
  35. FF_ENABLE_DEPRECATION_WARNINGS
  36. static const char* context_to_name(void* ptr) {
  37. AVCodecContext *avc= ptr;
  38. if(avc && avc->codec && avc->codec->name)
  39. return avc->codec->name;
  40. else
  41. return "NULL";
  42. }
  43. static void *codec_child_next(void *obj, void *prev)
  44. {
  45. AVCodecContext *s = obj;
  46. if (!prev && s->codec && s->codec->priv_class && s->priv_data)
  47. return s->priv_data;
  48. return NULL;
  49. }
  50. static const AVClass *codec_child_class_next(const AVClass *prev)
  51. {
  52. AVCodec *c = NULL;
  53. /* find the codec that corresponds to prev */
  54. while (prev && (c = av_codec_next(c)))
  55. if (c->priv_class == prev)
  56. break;
  57. /* find next codec with priv options */
  58. while (c = av_codec_next(c))
  59. if (c->priv_class)
  60. return c->priv_class;
  61. return NULL;
  62. }
  63. static AVClassCategory get_category(void *ptr)
  64. {
  65. AVCodecContext* avctx = ptr;
  66. if(avctx->codec && avctx->codec->decode) return AV_CLASS_CATEGORY_DECODER;
  67. else return AV_CLASS_CATEGORY_ENCODER;
  68. }
  69. static const AVClass av_codec_context_class = {
  70. .class_name = "AVCodecContext",
  71. .item_name = context_to_name,
  72. .option = avcodec_options,
  73. .version = LIBAVUTIL_VERSION_INT,
  74. .log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),
  75. .child_next = codec_child_next,
  76. .child_class_next = codec_child_class_next,
  77. .category = AV_CLASS_CATEGORY_ENCODER,
  78. .get_category = get_category,
  79. };
  80. int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec)
  81. {
  82. int flags=0;
  83. memset(s, 0, sizeof(AVCodecContext));
  84. s->av_class = &av_codec_context_class;
  85. s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
  86. if (codec) {
  87. s->codec = codec;
  88. s->codec_id = codec->id;
  89. }
  90. if(s->codec_type == AVMEDIA_TYPE_AUDIO)
  91. flags= AV_OPT_FLAG_AUDIO_PARAM;
  92. else if(s->codec_type == AVMEDIA_TYPE_VIDEO)
  93. flags= AV_OPT_FLAG_VIDEO_PARAM;
  94. else if(s->codec_type == AVMEDIA_TYPE_SUBTITLE)
  95. flags= AV_OPT_FLAG_SUBTITLE_PARAM;
  96. av_opt_set_defaults2(s, flags, flags);
  97. s->time_base = (AVRational){0,1};
  98. s->framerate = (AVRational){ 0, 1 };
  99. s->pkt_timebase = (AVRational){ 0, 1 };
  100. s->get_buffer2 = avcodec_default_get_buffer2;
  101. s->get_format = avcodec_default_get_format;
  102. s->execute = avcodec_default_execute;
  103. s->execute2 = avcodec_default_execute2;
  104. s->sample_aspect_ratio = (AVRational){0,1};
  105. s->pix_fmt = AV_PIX_FMT_NONE;
  106. s->sample_fmt = AV_SAMPLE_FMT_NONE;
  107. s->reordered_opaque = AV_NOPTS_VALUE;
  108. if(codec && codec->priv_data_size){
  109. if(!s->priv_data){
  110. s->priv_data= av_mallocz(codec->priv_data_size);
  111. if (!s->priv_data) {
  112. return AVERROR(ENOMEM);
  113. }
  114. }
  115. if(codec->priv_class){
  116. *(const AVClass**)s->priv_data = codec->priv_class;
  117. av_opt_set_defaults(s->priv_data);
  118. }
  119. }
  120. if (codec && codec->defaults) {
  121. int ret;
  122. const AVCodecDefault *d = codec->defaults;
  123. while (d->key) {
  124. ret = av_opt_set(s, d->key, d->value, 0);
  125. av_assert0(ret >= 0);
  126. d++;
  127. }
  128. }
  129. return 0;
  130. }
  131. AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)
  132. {
  133. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  134. if (!avctx)
  135. return NULL;
  136. if(avcodec_get_context_defaults3(avctx, codec) < 0){
  137. av_free(avctx);
  138. return NULL;
  139. }
  140. return avctx;
  141. }
  142. void avcodec_free_context(AVCodecContext **pavctx)
  143. {
  144. AVCodecContext *avctx = *pavctx;
  145. if (!avctx)
  146. return;
  147. avcodec_close(avctx);
  148. av_freep(&avctx->extradata);
  149. av_freep(&avctx->subtitle_header);
  150. av_freep(&avctx->intra_matrix);
  151. av_freep(&avctx->inter_matrix);
  152. av_freep(&avctx->rc_override);
  153. av_freep(pavctx);
  154. }
  155. int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
  156. {
  157. const AVCodec *orig_codec = dest->codec;
  158. uint8_t *orig_priv_data = dest->priv_data;
  159. if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
  160. av_log(dest, AV_LOG_ERROR,
  161. "Tried to copy AVCodecContext %p into already-initialized %p\n",
  162. src, dest);
  163. return AVERROR(EINVAL);
  164. }
  165. av_opt_free(dest);
  166. av_freep(&dest->rc_override);
  167. av_freep(&dest->intra_matrix);
  168. av_freep(&dest->inter_matrix);
  169. av_freep(&dest->extradata);
  170. av_freep(&dest->subtitle_header);
  171. memcpy(dest, src, sizeof(*dest));
  172. av_opt_copy(dest, src);
  173. dest->priv_data = orig_priv_data;
  174. dest->codec = orig_codec;
  175. if (orig_priv_data && src->codec && src->codec->priv_class &&
  176. dest->codec && dest->codec->priv_class)
  177. av_opt_copy(orig_priv_data, src->priv_data);
  178. /* set values specific to opened codecs back to their default state */
  179. dest->slice_offset = NULL;
  180. dest->hwaccel = NULL;
  181. dest->internal = NULL;
  182. dest->coded_frame = NULL;
  183. /* reallocate values that should be allocated separately */
  184. dest->extradata = NULL;
  185. dest->intra_matrix = NULL;
  186. dest->inter_matrix = NULL;
  187. dest->rc_override = NULL;
  188. dest->subtitle_header = NULL;
  189. #define alloc_and_copy_or_fail(obj, size, pad) \
  190. if (src->obj && size > 0) { \
  191. dest->obj = av_malloc(size + pad); \
  192. if (!dest->obj) \
  193. goto fail; \
  194. memcpy(dest->obj, src->obj, size); \
  195. if (pad) \
  196. memset(((uint8_t *) dest->obj) + size, 0, pad); \
  197. }
  198. alloc_and_copy_or_fail(extradata, src->extradata_size,
  199. FF_INPUT_BUFFER_PADDING_SIZE);
  200. dest->extradata_size = src->extradata_size;
  201. alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
  202. alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
  203. alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
  204. alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1);
  205. av_assert0(dest->subtitle_header_size == src->subtitle_header_size);
  206. #undef alloc_and_copy_or_fail
  207. return 0;
  208. fail:
  209. av_freep(&dest->rc_override);
  210. av_freep(&dest->intra_matrix);
  211. av_freep(&dest->inter_matrix);
  212. av_freep(&dest->extradata);
  213. av_freep(&dest->subtitle_header);
  214. dest->subtitle_header_size = 0;
  215. dest->extradata_size = 0;
  216. av_opt_free(dest);
  217. return AVERROR(ENOMEM);
  218. }
  219. const AVClass *avcodec_get_class(void)
  220. {
  221. return &av_codec_context_class;
  222. }
  223. #define FOFFSET(x) offsetof(AVFrame,x)
  224. static const AVOption frame_options[]={
  225. {"best_effort_timestamp", "", FOFFSET(best_effort_timestamp), AV_OPT_TYPE_INT64, {.i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, 0},
  226. {"pkt_pos", "", FOFFSET(pkt_pos), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
  227. {"pkt_size", "", FOFFSET(pkt_size), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
  228. {"sample_aspect_ratio", "", FOFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, {.dbl = 0 }, 0, INT_MAX, 0},
  229. {"width", "", FOFFSET(width), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  230. {"height", "", FOFFSET(height), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  231. {"format", "", FOFFSET(format), AV_OPT_TYPE_INT, {.i64 = -1 }, 0, INT_MAX, 0},
  232. {"channel_layout", "", FOFFSET(channel_layout), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, 0},
  233. {"sample_rate", "", FOFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  234. {NULL},
  235. };
  236. static const AVClass av_frame_class = {
  237. .class_name = "AVFrame",
  238. .item_name = NULL,
  239. .option = frame_options,
  240. .version = LIBAVUTIL_VERSION_INT,
  241. };
  242. const AVClass *avcodec_get_frame_class(void)
  243. {
  244. return &av_frame_class;
  245. }
  246. #define SROFFSET(x) offsetof(AVSubtitleRect,x)
  247. static const AVOption subtitle_rect_options[]={
  248. {"x", "", SROFFSET(x), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  249. {"y", "", SROFFSET(y), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  250. {"w", "", SROFFSET(w), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  251. {"h", "", SROFFSET(h), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  252. {"type", "", SROFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  253. {"flags", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0, "flags"},
  254. {"forced", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0},
  255. {NULL},
  256. };
  257. static const AVClass av_subtitle_rect_class = {
  258. .class_name = "AVSubtitleRect",
  259. .item_name = NULL,
  260. .option = subtitle_rect_options,
  261. .version = LIBAVUTIL_VERSION_INT,
  262. };
  263. const AVClass *avcodec_get_subtitle_rect_class(void)
  264. {
  265. return &av_subtitle_rect_class;
  266. }
  267. #ifdef TEST
  268. static int dummy_init(AVCodecContext *ctx)
  269. {
  270. //TODO: this code should set every possible pointer that could be set by codec and is not an option;
  271. ctx->extradata_size = 8;
  272. ctx->extradata = av_malloc(ctx->extradata_size);
  273. return 0;
  274. }
  275. static int dummy_close(AVCodecContext *ctx)
  276. {
  277. av_freep(&ctx->extradata);
  278. ctx->extradata_size = 0;
  279. return 0;
  280. }
  281. static int dummy_encode(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
  282. {
  283. return AVERROR(ENOSYS);
  284. }
  285. typedef struct Dummy12Context {
  286. AVClass *av_class;
  287. int num;
  288. char* str;
  289. } Dummy12Context;
  290. typedef struct Dummy3Context {
  291. void *fake_av_class;
  292. int num;
  293. char* str;
  294. } Dummy3Context;
  295. #define OFFSET(x) offsetof(Dummy12Context, x)
  296. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  297. static const AVOption dummy_options[] = {
  298. { "str", "set str", OFFSET(str), AV_OPT_TYPE_STRING, { .str = "i'm src default value" }, 0, 0, VE},
  299. { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 1500100900 }, 0, INT_MAX, VE},
  300. { NULL },
  301. };
  302. static const AVClass dummy_v1_class = {
  303. .class_name = "dummy_v1_class",
  304. .item_name = av_default_item_name,
  305. .option = dummy_options,
  306. .version = LIBAVUTIL_VERSION_INT,
  307. };
  308. static const AVClass dummy_v2_class = {
  309. .class_name = "dummy_v2_class",
  310. .item_name = av_default_item_name,
  311. .option = dummy_options,
  312. .version = LIBAVUTIL_VERSION_INT,
  313. };
  314. /* codec with options */
  315. AVCodec dummy_v1_encoder = {
  316. .name = "dummy_v1_codec",
  317. .type = AVMEDIA_TYPE_VIDEO,
  318. .id = AV_CODEC_ID_NONE - 1,
  319. .encode2 = dummy_encode,
  320. .init = dummy_init,
  321. .close = dummy_close,
  322. .priv_class = &dummy_v1_class,
  323. .priv_data_size = sizeof(Dummy12Context),
  324. };
  325. /* codec with options, different class */
  326. AVCodec dummy_v2_encoder = {
  327. .name = "dummy_v2_codec",
  328. .type = AVMEDIA_TYPE_VIDEO,
  329. .id = AV_CODEC_ID_NONE - 2,
  330. .encode2 = dummy_encode,
  331. .init = dummy_init,
  332. .close = dummy_close,
  333. .priv_class = &dummy_v2_class,
  334. .priv_data_size = sizeof(Dummy12Context),
  335. };
  336. /* codec with priv data, but no class */
  337. AVCodec dummy_v3_encoder = {
  338. .name = "dummy_v3_codec",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .id = AV_CODEC_ID_NONE - 3,
  341. .encode2 = dummy_encode,
  342. .init = dummy_init,
  343. .close = dummy_close,
  344. .priv_data_size = sizeof(Dummy3Context),
  345. };
  346. /* codec without priv data */
  347. AVCodec dummy_v4_encoder = {
  348. .name = "dummy_v4_codec",
  349. .type = AVMEDIA_TYPE_VIDEO,
  350. .id = AV_CODEC_ID_NONE - 4,
  351. .encode2 = dummy_encode,
  352. .init = dummy_init,
  353. .close = dummy_close,
  354. };
  355. static void test_copy_print_codec(const AVCodecContext *ctx)
  356. {
  357. printf("%-14s: %dx%d prv: %s",
  358. ctx->codec ? ctx->codec->name : "NULL",
  359. ctx->width, ctx->height,
  360. ctx->priv_data ? "set" : "null");
  361. if (ctx->codec && ctx->codec->priv_class && ctx->codec->priv_data_size) {
  362. int64_t i64;
  363. char *str = NULL;
  364. av_opt_get_int(ctx->priv_data, "num", 0, &i64);
  365. av_opt_get(ctx->priv_data, "str", 0, (uint8_t**)&str);
  366. printf(" opts: %"PRId64" %s", i64, str);
  367. av_free(str);
  368. }
  369. printf("\n");
  370. }
  371. static void test_copy(const AVCodec *c1, const AVCodec *c2)
  372. {
  373. AVCodecContext *ctx1, *ctx2;
  374. printf("%s -> %s\nclosed:\n", c1 ? c1->name : "NULL", c2 ? c2->name : "NULL");
  375. ctx1 = avcodec_alloc_context3(c1);
  376. ctx2 = avcodec_alloc_context3(c2);
  377. ctx1->width = ctx1->height = 128;
  378. if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
  379. av_opt_set(ctx2->priv_data, "num", "667", 0);
  380. av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
  381. }
  382. avcodec_copy_context(ctx2, ctx1);
  383. test_copy_print_codec(ctx1);
  384. test_copy_print_codec(ctx2);
  385. if (ctx1->codec) {
  386. printf("opened:\n");
  387. avcodec_open2(ctx1, ctx1->codec, NULL);
  388. if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
  389. av_opt_set(ctx2->priv_data, "num", "667", 0);
  390. av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
  391. }
  392. avcodec_copy_context(ctx2, ctx1);
  393. test_copy_print_codec(ctx1);
  394. test_copy_print_codec(ctx2);
  395. avcodec_close(ctx1);
  396. }
  397. avcodec_free_context(&ctx1);
  398. avcodec_free_context(&ctx2);
  399. }
  400. int main(void)
  401. {
  402. AVCodec *dummy_codec[] = {
  403. &dummy_v1_encoder,
  404. &dummy_v2_encoder,
  405. &dummy_v3_encoder,
  406. &dummy_v4_encoder,
  407. NULL,
  408. };
  409. int i, j;
  410. for (i = 0; dummy_codec[i]; i++)
  411. avcodec_register(dummy_codec[i]);
  412. printf("testing avcodec_copy_context()\n");
  413. for (i = 0; i < FF_ARRAY_ELEMS(dummy_codec); i++)
  414. for (j = 0; j < FF_ARRAY_ELEMS(dummy_codec); j++)
  415. test_copy(dummy_codec[i], dummy_codec[j]);
  416. return 0;
  417. }
  418. #endif