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.

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