options.c 9.32 KB
Newer Older
1
/*
2
 * Copyright (c) 2001 Fabrice Bellard
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27
 * Options definition for AVCodecContext.
 */

#include "avcodec.h"
28 29
#include "internal.h"
#include "libavutil/avassert.h"
30
#include "libavutil/mem.h"
31
#include "libavutil/opt.h"
32
#include <float.h>              /* FLT_MIN, FLT_MAX */
33
#include <string.h>
34

35 36
#include "options_table.h"

37 38 39 40 41 42 43 44 45
static const char* context_to_name(void* ptr) {
    AVCodecContext *avc= ptr;

    if(avc && avc->codec && avc->codec->name)
        return avc->codec->name;
    else
        return "NULL";
}

46
static void *codec_child_next(void *obj, void *prev)
47 48
{
    AVCodecContext *s = obj;
49 50 51 52 53 54 55 56
    if (!prev && s->codec && s->codec->priv_class && s->priv_data)
        return s->priv_data;
    return NULL;
}

static const AVClass *codec_child_class_next(const AVClass *prev)
{
    AVCodec *c = NULL;
57

58 59 60 61
    /* find the codec that corresponds to prev */
    while (prev && (c = av_codec_next(c)))
        if (c->priv_class == prev)
            break;
62

63 64 65 66
    /* find next codec with priv options */
    while (c = av_codec_next(c))
        if (c->priv_class)
            return c->priv_class;
67 68 69
    return NULL;
}

70
static AVClassCategory get_category(void *ptr)
71
{
72
    AVCodecContext* avctx = ptr;
73 74 75 76
    if(avctx->codec && avctx->codec->decode) return AV_CLASS_CATEGORY_DECODER;
    else                                     return AV_CLASS_CATEGORY_ENCODER;
}

77 78 79
static const AVClass av_codec_context_class = {
    .class_name              = "AVCodecContext",
    .item_name               = context_to_name,
80
    .option                  = avcodec_options,
81
    .version                 = LIBAVUTIL_VERSION_INT,
82
    .log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),
83 84
    .child_next              = codec_child_next,
    .child_class_next        = codec_child_class_next,
85
    .category                = AV_CLASS_CATEGORY_ENCODER,
86
    .get_category            = get_category,
87
};
88

89 90
int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec)
{
91 92 93
    int flags=0;
    memset(s, 0, sizeof(AVCodecContext));

94
    s->av_class = &av_codec_context_class;
95

96
    s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
97 98 99
    if (codec)
        s->codec_id = codec->id;

100
    if(s->codec_type == AVMEDIA_TYPE_AUDIO)
101
        flags= AV_OPT_FLAG_AUDIO_PARAM;
102
    else if(s->codec_type == AVMEDIA_TYPE_VIDEO)
103
        flags= AV_OPT_FLAG_VIDEO_PARAM;
104
    else if(s->codec_type == AVMEDIA_TYPE_SUBTITLE)
105 106 107
        flags= AV_OPT_FLAG_SUBTITLE_PARAM;
    av_opt_set_defaults2(s, flags, flags);

108
    s->time_base           = (AVRational){0,1};
109
    s->get_buffer2         = avcodec_default_get_buffer2;
110 111 112 113
    s->get_format          = avcodec_default_get_format;
    s->execute             = avcodec_default_execute;
    s->execute2            = avcodec_default_execute2;
    s->sample_aspect_ratio = (AVRational){0,1};
114
    s->pix_fmt             = AV_PIX_FMT_NONE;
115
    s->sample_fmt          = AV_SAMPLE_FMT_NONE;
116
    s->timecode_frame_start = -1;
117

118
    s->reordered_opaque    = AV_NOPTS_VALUE;
119 120 121 122 123 124 125 126
    if(codec && codec->priv_data_size){
        if(!s->priv_data){
            s->priv_data= av_mallocz(codec->priv_data_size);
            if (!s->priv_data) {
                return AVERROR(ENOMEM);
            }
        }
        if(codec->priv_class){
127
            *(const AVClass**)s->priv_data = codec->priv_class;
128 129 130
            av_opt_set_defaults(s->priv_data);
        }
    }
131 132
    if (codec && codec->defaults) {
        int ret;
133
        const AVCodecDefault *d = codec->defaults;
134
        while (d->key) {
135
            ret = av_opt_set(s, d->key, d->value, 0);
136 137 138 139
            av_assert0(ret >= 0);
            d++;
        }
    }
140 141 142
    return 0;
}

143 144
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)
{
145 146 147 148 149 150 151 152 153 154 155 156
    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));

    if(avctx==NULL) return NULL;

    if(avcodec_get_context_defaults3(avctx, codec) < 0){
        av_free(avctx);
        return NULL;
    }

    return avctx;
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
void avcodec_free_context(AVCodecContext **pavctx)
{
    AVCodecContext *avctx = *pavctx;

    if (!avctx)
        return;

    avcodec_close(avctx);

    av_freep(&avctx->extradata);
    av_freep(&avctx->subtitle_header);

    av_freep(pavctx);
}

172 173
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
{
174 175 176
    const AVCodec *orig_codec = dest->codec;
    uint8_t *orig_priv_data = dest->priv_data;

177
    if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
178 179 180 181 182
        av_log(dest, AV_LOG_ERROR,
               "Tried to copy AVCodecContext %p into already-initialized %p\n",
               src, dest);
        return AVERROR(EINVAL);
    }
183 184 185 186

    av_opt_free(dest);
    av_free(dest->priv_data);

187 188
    memcpy(dest, src, sizeof(*dest));

189 190 191
    dest->priv_data       = orig_priv_data;
    dest->codec           = orig_codec;

192 193 194
    /* set values specific to opened codecs back to their default state */
    dest->slice_offset    = NULL;
    dest->hwaccel         = NULL;
195
    dest->internal        = NULL;
196 197 198 199 200 201 202

    /* reallocate values that should be allocated separately */
    dest->rc_eq           = NULL;
    dest->extradata       = NULL;
    dest->intra_matrix    = NULL;
    dest->inter_matrix    = NULL;
    dest->rc_override     = NULL;
203
    dest->subtitle_header = NULL;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    if (src->rc_eq) {
        dest->rc_eq = av_strdup(src->rc_eq);
        if (!dest->rc_eq)
            return AVERROR(ENOMEM);
    }

#define alloc_and_copy_or_fail(obj, size, pad) \
    if (src->obj && size > 0) { \
        dest->obj = av_malloc(size + pad); \
        if (!dest->obj) \
            goto fail; \
        memcpy(dest->obj, src->obj, size); \
        if (pad) \
            memset(((uint8_t *) dest->obj) + size, 0, pad); \
    }
    alloc_and_copy_or_fail(extradata,    src->extradata_size,
                           FF_INPUT_BUFFER_PADDING_SIZE);
    alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
    alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
    alloc_and_copy_or_fail(rc_override,  src->rc_override_count * sizeof(*src->rc_override), 0);
224
    alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1);
225
    av_assert0(dest->subtitle_header_size == src->subtitle_header_size);
226 227 228 229 230 231 232 233 234 235 236 237
#undef alloc_and_copy_or_fail

    return 0;

fail:
    av_freep(&dest->rc_override);
    av_freep(&dest->intra_matrix);
    av_freep(&dest->inter_matrix);
    av_freep(&dest->extradata);
    av_freep(&dest->rc_eq);
    return AVERROR(ENOMEM);
}
238 239 240 241 242

const AVClass *avcodec_get_class(void)
{
    return &av_codec_context_class;
}
243 244 245 246

#define FOFFSET(x) offsetof(AVFrame,x)

static const AVOption frame_options[]={
247 248
{"best_effort_timestamp", "", FOFFSET(best_effort_timestamp), AV_OPT_TYPE_INT64, {.i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, 0},
{"pkt_pos", "", FOFFSET(pkt_pos), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
249
{"pkt_size", "", FOFFSET(pkt_size), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
250
{"sample_aspect_ratio", "", FOFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, {.dbl = 0 }, 0, INT_MAX, 0},
251 252 253
{"width", "", FOFFSET(width), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
{"height", "", FOFFSET(height), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
{"format", "", FOFFSET(format), AV_OPT_TYPE_INT, {.i64 = -1 }, 0, INT_MAX, 0},
254
{"channel_layout", "", FOFFSET(channel_layout), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, 0},
255
{"sample_rate", "", FOFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
256 257 258 259 260 261 262 263 264 265 266 267 268 269
{NULL},
};

static const AVClass av_frame_class = {
    .class_name              = "AVFrame",
    .item_name               = NULL,
    .option                  = frame_options,
    .version                 = LIBAVUTIL_VERSION_INT,
};

const AVClass *avcodec_get_frame_class(void)
{
    return &av_frame_class;
}
270 271 272 273

#define SROFFSET(x) offsetof(AVSubtitleRect,x)

static const AVOption subtitle_rect_options[]={
274 275 276 277 278
{"x", "", SROFFSET(x), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
{"y", "", SROFFSET(y), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
{"w", "", SROFFSET(w), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
{"h", "", SROFFSET(h), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
{"type", "", SROFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
279 280
{"flags", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0, "flags"},
{"forced", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0},
281 282 283 284 285 286 287 288 289 290 291 292 293 294
{NULL},
};

static const AVClass av_subtitle_rect_class = {
    .class_name             = "AVSubtitleRect",
    .item_name              = NULL,
    .option                 = subtitle_rect_options,
    .version                = LIBAVUTIL_VERSION_INT,
};

const AVClass *avcodec_get_subtitle_rect_class(void)
{
    return &av_subtitle_rect_class;
}