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