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.

504 lines
16KB

  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. static int init_context_defaults(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. #if FF_API_GET_CONTEXT_DEFAULTS
  132. int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec)
  133. {
  134. return init_context_defaults(s, codec);
  135. }
  136. #endif
  137. AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)
  138. {
  139. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  140. if (!avctx)
  141. return NULL;
  142. if (init_context_defaults(avctx, codec) < 0) {
  143. av_free(avctx);
  144. return NULL;
  145. }
  146. return avctx;
  147. }
  148. void avcodec_free_context(AVCodecContext **pavctx)
  149. {
  150. AVCodecContext *avctx = *pavctx;
  151. if (!avctx)
  152. return;
  153. avcodec_close(avctx);
  154. av_freep(&avctx->extradata);
  155. av_freep(&avctx->subtitle_header);
  156. av_freep(&avctx->intra_matrix);
  157. av_freep(&avctx->inter_matrix);
  158. av_freep(&avctx->rc_override);
  159. av_freep(pavctx);
  160. }
  161. #if FF_API_COPY_CONTEXT
  162. static void copy_context_reset(AVCodecContext *avctx)
  163. {
  164. av_opt_free(avctx);
  165. av_freep(&avctx->rc_override);
  166. av_freep(&avctx->intra_matrix);
  167. av_freep(&avctx->inter_matrix);
  168. av_freep(&avctx->extradata);
  169. av_freep(&avctx->subtitle_header);
  170. av_buffer_unref(&avctx->hw_frames_ctx);
  171. avctx->subtitle_header_size = 0;
  172. avctx->extradata_size = 0;
  173. }
  174. int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
  175. {
  176. const AVCodec *orig_codec = dest->codec;
  177. uint8_t *orig_priv_data = dest->priv_data;
  178. if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
  179. av_log(dest, AV_LOG_ERROR,
  180. "Tried to copy AVCodecContext %p into already-initialized %p\n",
  181. src, dest);
  182. return AVERROR(EINVAL);
  183. }
  184. copy_context_reset(dest);
  185. memcpy(dest, src, sizeof(*dest));
  186. av_opt_copy(dest, src);
  187. dest->priv_data = orig_priv_data;
  188. dest->codec = orig_codec;
  189. if (orig_priv_data && src->codec && src->codec->priv_class &&
  190. dest->codec && dest->codec->priv_class)
  191. av_opt_copy(orig_priv_data, src->priv_data);
  192. /* set values specific to opened codecs back to their default state */
  193. dest->slice_offset = NULL;
  194. dest->hwaccel = NULL;
  195. dest->internal = NULL;
  196. #if FF_API_CODED_FRAME
  197. FF_DISABLE_DEPRECATION_WARNINGS
  198. dest->coded_frame = NULL;
  199. FF_ENABLE_DEPRECATION_WARNINGS
  200. #endif
  201. /* reallocate values that should be allocated separately */
  202. dest->extradata = NULL;
  203. dest->intra_matrix = NULL;
  204. dest->inter_matrix = NULL;
  205. dest->rc_override = NULL;
  206. dest->subtitle_header = NULL;
  207. dest->hw_frames_ctx = NULL;
  208. #define alloc_and_copy_or_fail(obj, size, pad) \
  209. if (src->obj && size > 0) { \
  210. dest->obj = av_malloc(size + pad); \
  211. if (!dest->obj) \
  212. goto fail; \
  213. memcpy(dest->obj, src->obj, size); \
  214. if (pad) \
  215. memset(((uint8_t *) dest->obj) + size, 0, pad); \
  216. }
  217. alloc_and_copy_or_fail(extradata, src->extradata_size,
  218. AV_INPUT_BUFFER_PADDING_SIZE);
  219. dest->extradata_size = src->extradata_size;
  220. alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
  221. alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
  222. alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
  223. alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1);
  224. av_assert0(dest->subtitle_header_size == src->subtitle_header_size);
  225. #undef alloc_and_copy_or_fail
  226. if (src->hw_frames_ctx) {
  227. dest->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  228. if (!dest->hw_frames_ctx)
  229. goto fail;
  230. }
  231. return 0;
  232. fail:
  233. copy_context_reset(dest);
  234. return AVERROR(ENOMEM);
  235. }
  236. #endif
  237. const AVClass *avcodec_get_class(void)
  238. {
  239. return &av_codec_context_class;
  240. }
  241. #define FOFFSET(x) offsetof(AVFrame,x)
  242. static const AVOption frame_options[]={
  243. {"best_effort_timestamp", "", FOFFSET(best_effort_timestamp), AV_OPT_TYPE_INT64, {.i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, 0},
  244. {"pkt_pos", "", FOFFSET(pkt_pos), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
  245. {"pkt_size", "", FOFFSET(pkt_size), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
  246. {"sample_aspect_ratio", "", FOFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, {.dbl = 0 }, 0, INT_MAX, 0},
  247. {"width", "", FOFFSET(width), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  248. {"height", "", FOFFSET(height), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  249. {"format", "", FOFFSET(format), AV_OPT_TYPE_INT, {.i64 = -1 }, 0, INT_MAX, 0},
  250. {"channel_layout", "", FOFFSET(channel_layout), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, 0},
  251. {"sample_rate", "", FOFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  252. {NULL},
  253. };
  254. static const AVClass av_frame_class = {
  255. .class_name = "AVFrame",
  256. .item_name = NULL,
  257. .option = frame_options,
  258. .version = LIBAVUTIL_VERSION_INT,
  259. };
  260. const AVClass *avcodec_get_frame_class(void)
  261. {
  262. return &av_frame_class;
  263. }
  264. #define SROFFSET(x) offsetof(AVSubtitleRect,x)
  265. static const AVOption subtitle_rect_options[]={
  266. {"x", "", SROFFSET(x), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  267. {"y", "", SROFFSET(y), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  268. {"w", "", SROFFSET(w), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  269. {"h", "", SROFFSET(h), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  270. {"type", "", SROFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
  271. {"flags", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0, "flags"},
  272. {"forced", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0},
  273. {NULL},
  274. };
  275. static const AVClass av_subtitle_rect_class = {
  276. .class_name = "AVSubtitleRect",
  277. .item_name = NULL,
  278. .option = subtitle_rect_options,
  279. .version = LIBAVUTIL_VERSION_INT,
  280. };
  281. const AVClass *avcodec_get_subtitle_rect_class(void)
  282. {
  283. return &av_subtitle_rect_class;
  284. }
  285. #ifdef TEST
  286. static int dummy_init(AVCodecContext *ctx)
  287. {
  288. //TODO: this code should set every possible pointer that could be set by codec and is not an option;
  289. ctx->extradata_size = 8;
  290. ctx->extradata = av_malloc(ctx->extradata_size);
  291. return 0;
  292. }
  293. static int dummy_close(AVCodecContext *ctx)
  294. {
  295. av_freep(&ctx->extradata);
  296. ctx->extradata_size = 0;
  297. return 0;
  298. }
  299. static int dummy_encode(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
  300. {
  301. return AVERROR(ENOSYS);
  302. }
  303. typedef struct Dummy12Context {
  304. AVClass *av_class;
  305. int num;
  306. char* str;
  307. } Dummy12Context;
  308. typedef struct Dummy3Context {
  309. void *fake_av_class;
  310. int num;
  311. char* str;
  312. } Dummy3Context;
  313. #define OFFSET(x) offsetof(Dummy12Context, x)
  314. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  315. static const AVOption dummy_options[] = {
  316. { "str", "set str", OFFSET(str), AV_OPT_TYPE_STRING, { .str = "i'm src default value" }, 0, 0, VE},
  317. { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 1500100900 }, 0, INT_MAX, VE},
  318. { NULL },
  319. };
  320. static const AVClass dummy_v1_class = {
  321. .class_name = "dummy_v1_class",
  322. .item_name = av_default_item_name,
  323. .option = dummy_options,
  324. .version = LIBAVUTIL_VERSION_INT,
  325. };
  326. static const AVClass dummy_v2_class = {
  327. .class_name = "dummy_v2_class",
  328. .item_name = av_default_item_name,
  329. .option = dummy_options,
  330. .version = LIBAVUTIL_VERSION_INT,
  331. };
  332. /* codec with options */
  333. static AVCodec dummy_v1_encoder = {
  334. .name = "dummy_v1_codec",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .id = AV_CODEC_ID_NONE - 1,
  337. .encode2 = dummy_encode,
  338. .init = dummy_init,
  339. .close = dummy_close,
  340. .priv_class = &dummy_v1_class,
  341. .priv_data_size = sizeof(Dummy12Context),
  342. };
  343. /* codec with options, different class */
  344. static AVCodec dummy_v2_encoder = {
  345. .name = "dummy_v2_codec",
  346. .type = AVMEDIA_TYPE_VIDEO,
  347. .id = AV_CODEC_ID_NONE - 2,
  348. .encode2 = dummy_encode,
  349. .init = dummy_init,
  350. .close = dummy_close,
  351. .priv_class = &dummy_v2_class,
  352. .priv_data_size = sizeof(Dummy12Context),
  353. };
  354. /* codec with priv data, but no class */
  355. static AVCodec dummy_v3_encoder = {
  356. .name = "dummy_v3_codec",
  357. .type = AVMEDIA_TYPE_VIDEO,
  358. .id = AV_CODEC_ID_NONE - 3,
  359. .encode2 = dummy_encode,
  360. .init = dummy_init,
  361. .close = dummy_close,
  362. .priv_data_size = sizeof(Dummy3Context),
  363. };
  364. /* codec without priv data */
  365. static AVCodec dummy_v4_encoder = {
  366. .name = "dummy_v4_codec",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .id = AV_CODEC_ID_NONE - 4,
  369. .encode2 = dummy_encode,
  370. .init = dummy_init,
  371. .close = dummy_close,
  372. };
  373. static void test_copy_print_codec(const AVCodecContext *ctx)
  374. {
  375. printf("%-14s: %dx%d prv: %s",
  376. ctx->codec ? ctx->codec->name : "NULL",
  377. ctx->width, ctx->height,
  378. ctx->priv_data ? "set" : "null");
  379. if (ctx->codec && ctx->codec->priv_class && ctx->codec->priv_data_size) {
  380. int64_t i64;
  381. char *str = NULL;
  382. av_opt_get_int(ctx->priv_data, "num", 0, &i64);
  383. av_opt_get(ctx->priv_data, "str", 0, (uint8_t**)&str);
  384. printf(" opts: %"PRId64" %s", i64, str);
  385. av_free(str);
  386. }
  387. printf("\n");
  388. }
  389. static void test_copy(const AVCodec *c1, const AVCodec *c2)
  390. {
  391. AVCodecContext *ctx1, *ctx2;
  392. printf("%s -> %s\nclosed:\n", c1 ? c1->name : "NULL", c2 ? c2->name : "NULL");
  393. ctx1 = avcodec_alloc_context3(c1);
  394. ctx2 = avcodec_alloc_context3(c2);
  395. ctx1->width = ctx1->height = 128;
  396. if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
  397. av_opt_set(ctx2->priv_data, "num", "667", 0);
  398. av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
  399. }
  400. avcodec_copy_context(ctx2, ctx1);
  401. test_copy_print_codec(ctx1);
  402. test_copy_print_codec(ctx2);
  403. if (ctx1->codec) {
  404. printf("opened:\n");
  405. avcodec_open2(ctx1, ctx1->codec, NULL);
  406. if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
  407. av_opt_set(ctx2->priv_data, "num", "667", 0);
  408. av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
  409. }
  410. avcodec_copy_context(ctx2, ctx1);
  411. test_copy_print_codec(ctx1);
  412. test_copy_print_codec(ctx2);
  413. avcodec_close(ctx1);
  414. }
  415. avcodec_free_context(&ctx1);
  416. avcodec_free_context(&ctx2);
  417. }
  418. int main(void)
  419. {
  420. AVCodec *dummy_codec[] = {
  421. &dummy_v1_encoder,
  422. &dummy_v2_encoder,
  423. &dummy_v3_encoder,
  424. &dummy_v4_encoder,
  425. NULL,
  426. };
  427. int i, j;
  428. for (i = 0; dummy_codec[i]; i++)
  429. avcodec_register(dummy_codec[i]);
  430. printf("testing avcodec_copy_context()\n");
  431. for (i = 0; i < FF_ARRAY_ELEMS(dummy_codec); i++)
  432. for (j = 0; j < FF_ARRAY_ELEMS(dummy_codec); j++)
  433. test_copy(dummy_codec[i], dummy_codec[j]);
  434. return 0;
  435. }
  436. #endif