options.c 16 KB

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