00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "internal.h"
00029 #include "libavutil/avassert.h"
00030 #include "libavutil/mem.h"
00031 #include "libavutil/opt.h"
00032 #include <float.h>
00033 #include <string.h>
00034
00035 #include "options_table.h"
00036
00037 static const char* context_to_name(void* ptr) {
00038 AVCodecContext *avc= ptr;
00039
00040 if(avc && avc->codec && avc->codec->name)
00041 return avc->codec->name;
00042 else
00043 return "NULL";
00044 }
00045
00046 static void *codec_child_next(void *obj, void *prev)
00047 {
00048 AVCodecContext *s = obj;
00049 if (!prev && s->codec && s->codec->priv_class && s->priv_data)
00050 return s->priv_data;
00051 return NULL;
00052 }
00053
00054 static const AVClass *codec_child_class_next(const AVClass *prev)
00055 {
00056 AVCodec *c = NULL;
00057
00058
00059 while (prev && (c = av_codec_next(c)))
00060 if (c->priv_class == prev)
00061 break;
00062
00063
00064 while (c = av_codec_next(c))
00065 if (c->priv_class)
00066 return c->priv_class;
00067 return NULL;
00068 }
00069
00070 static AVClassCategory get_category(void *ptr)
00071 {
00072 AVCodecContext* avctx = ptr;
00073 if(avctx->codec && avctx->codec->decode) return AV_CLASS_CATEGORY_DECODER;
00074 else return AV_CLASS_CATEGORY_ENCODER;
00075 }
00076
00077 static const AVClass av_codec_context_class = {
00078 .class_name = "AVCodecContext",
00079 .item_name = context_to_name,
00080 .option = options,
00081 .version = LIBAVUTIL_VERSION_INT,
00082 .log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),
00083 .child_next = codec_child_next,
00084 .child_class_next = codec_child_class_next,
00085 .category = AV_CLASS_CATEGORY_ENCODER,
00086 .get_category = get_category,
00087 };
00088
00089 #if FF_API_ALLOC_CONTEXT
00090 void avcodec_get_context_defaults2(AVCodecContext *s, enum AVMediaType codec_type){
00091 AVCodec c= {0};
00092 c.type= codec_type;
00093 avcodec_get_context_defaults3(s, &c);
00094 }
00095 #endif
00096
00097 int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec)
00098 {
00099 int flags=0;
00100 memset(s, 0, sizeof(AVCodecContext));
00101
00102 s->av_class = &av_codec_context_class;
00103
00104 s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
00105 if(s->codec_type == AVMEDIA_TYPE_AUDIO)
00106 flags= AV_OPT_FLAG_AUDIO_PARAM;
00107 else if(s->codec_type == AVMEDIA_TYPE_VIDEO)
00108 flags= AV_OPT_FLAG_VIDEO_PARAM;
00109 else if(s->codec_type == AVMEDIA_TYPE_SUBTITLE)
00110 flags= AV_OPT_FLAG_SUBTITLE_PARAM;
00111 av_opt_set_defaults2(s, flags, flags);
00112
00113 s->time_base = (AVRational){0,1};
00114 s->get_buffer = avcodec_default_get_buffer;
00115 s->release_buffer = avcodec_default_release_buffer;
00116 s->get_format = avcodec_default_get_format;
00117 s->execute = avcodec_default_execute;
00118 s->execute2 = avcodec_default_execute2;
00119 s->sample_aspect_ratio = (AVRational){0,1};
00120 s->pix_fmt = AV_PIX_FMT_NONE;
00121 s->sample_fmt = AV_SAMPLE_FMT_NONE;
00122 s->timecode_frame_start = -1;
00123
00124 s->reget_buffer = avcodec_default_reget_buffer;
00125 s->reordered_opaque = AV_NOPTS_VALUE;
00126 if(codec && codec->priv_data_size){
00127 if(!s->priv_data){
00128 s->priv_data= av_mallocz(codec->priv_data_size);
00129 if (!s->priv_data) {
00130 return AVERROR(ENOMEM);
00131 }
00132 }
00133 if(codec->priv_class){
00134 *(const AVClass**)s->priv_data = codec->priv_class;
00135 av_opt_set_defaults(s->priv_data);
00136 }
00137 }
00138 if (codec && codec->defaults) {
00139 int ret;
00140 const AVCodecDefault *d = codec->defaults;
00141 while (d->key) {
00142 ret = av_opt_set(s, d->key, d->value, 0);
00143 av_assert0(ret >= 0);
00144 d++;
00145 }
00146 }
00147 return 0;
00148 }
00149
00150 AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)
00151 {
00152 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
00153
00154 if(avctx==NULL) return NULL;
00155
00156 if(avcodec_get_context_defaults3(avctx, codec) < 0){
00157 av_free(avctx);
00158 return NULL;
00159 }
00160
00161 return avctx;
00162 }
00163
00164 #if FF_API_ALLOC_CONTEXT
00165 AVCodecContext *avcodec_alloc_context2(enum AVMediaType codec_type){
00166 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
00167
00168 if(avctx==NULL) return NULL;
00169
00170 avcodec_get_context_defaults2(avctx, codec_type);
00171
00172 return avctx;
00173 }
00174
00175 void avcodec_get_context_defaults(AVCodecContext *s){
00176 avcodec_get_context_defaults2(s, AVMEDIA_TYPE_UNKNOWN);
00177 }
00178
00179 AVCodecContext *avcodec_alloc_context(void){
00180 return avcodec_alloc_context2(AVMEDIA_TYPE_UNKNOWN);
00181 }
00182 #endif
00183
00184 int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
00185 {
00186 if (avcodec_is_open(dest)) {
00187 av_log(dest, AV_LOG_ERROR,
00188 "Tried to copy AVCodecContext %p into already-initialized %p\n",
00189 src, dest);
00190 return AVERROR(EINVAL);
00191 }
00192 memcpy(dest, src, sizeof(*dest));
00193
00194
00195 dest->priv_data = NULL;
00196 dest->codec = NULL;
00197 dest->slice_offset = NULL;
00198 dest->hwaccel = NULL;
00199 dest->thread_opaque = NULL;
00200 dest->internal = NULL;
00201
00202
00203 dest->rc_eq = NULL;
00204 dest->extradata = NULL;
00205 dest->intra_matrix = NULL;
00206 dest->inter_matrix = NULL;
00207 dest->rc_override = NULL;
00208 if (src->rc_eq) {
00209 dest->rc_eq = av_strdup(src->rc_eq);
00210 if (!dest->rc_eq)
00211 return AVERROR(ENOMEM);
00212 }
00213
00214 #define alloc_and_copy_or_fail(obj, size, pad) \
00215 if (src->obj && size > 0) { \
00216 dest->obj = av_malloc(size + pad); \
00217 if (!dest->obj) \
00218 goto fail; \
00219 memcpy(dest->obj, src->obj, size); \
00220 if (pad) \
00221 memset(((uint8_t *) dest->obj) + size, 0, pad); \
00222 }
00223 alloc_and_copy_or_fail(extradata, src->extradata_size,
00224 FF_INPUT_BUFFER_PADDING_SIZE);
00225 alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
00226 alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
00227 alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
00228 #undef alloc_and_copy_or_fail
00229
00230 return 0;
00231
00232 fail:
00233 av_freep(&dest->rc_override);
00234 av_freep(&dest->intra_matrix);
00235 av_freep(&dest->inter_matrix);
00236 av_freep(&dest->extradata);
00237 av_freep(&dest->rc_eq);
00238 return AVERROR(ENOMEM);
00239 }
00240
00241 const AVClass *avcodec_get_class(void)
00242 {
00243 return &av_codec_context_class;
00244 }
00245
00246 #define FOFFSET(x) offsetof(AVFrame,x)
00247
00248 static const AVOption frame_options[]={
00249 {"best_effort_timestamp", "", FOFFSET(best_effort_timestamp), AV_OPT_TYPE_INT64, {.i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, 0},
00250 {"pkt_pos", "", FOFFSET(pkt_pos), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
00251 {"pkt_size", "", FOFFSET(pkt_size), AV_OPT_TYPE_INT64, {.i64 = -1 }, INT64_MIN, INT64_MAX, 0},
00252 {"sample_aspect_ratio", "", FOFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, {.dbl = 0 }, 0, INT_MAX, 0},
00253 {"width", "", FOFFSET(width), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00254 {"height", "", FOFFSET(height), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00255 {"format", "", FOFFSET(format), AV_OPT_TYPE_INT, {.i64 = -1 }, 0, INT_MAX, 0},
00256 {"channel_layout", "", FOFFSET(channel_layout), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, 0},
00257 {"sample_rate", "", FOFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00258 {NULL},
00259 };
00260
00261 static const AVClass av_frame_class = {
00262 .class_name = "AVFrame",
00263 .item_name = NULL,
00264 .option = frame_options,
00265 .version = LIBAVUTIL_VERSION_INT,
00266 };
00267
00268 const AVClass *avcodec_get_frame_class(void)
00269 {
00270 return &av_frame_class;
00271 }
00272
00273 #define SROFFSET(x) offsetof(AVSubtitleRect,x)
00274
00275 static const AVOption subtitle_rect_options[]={
00276 {"x", "", SROFFSET(x), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00277 {"y", "", SROFFSET(y), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00278 {"w", "", SROFFSET(w), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00279 {"h", "", SROFFSET(h), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00280 {"type", "", SROFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0},
00281 {"flags", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0, "flags"},
00282 {"forced", "", SROFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, 1, 0},
00283 {NULL},
00284 };
00285
00286 static const AVClass av_subtitle_rect_class = {
00287 .class_name = "AVSubtitleRect",
00288 .item_name = NULL,
00289 .option = subtitle_rect_options,
00290 .version = LIBAVUTIL_VERSION_INT,
00291 };
00292
00293 const AVClass *avcodec_get_subtitle_rect_class(void)
00294 {
00295 return &av_subtitle_rect_class;
00296 }