Libav
options.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001 Fabrice Bellard
3  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "internal.h"
29 #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 #include "options_table.h"
36 
37 static const char* context_to_name(void* ptr) {
38  AVCodecContext *avc= ptr;
39 
40  if(avc && avc->codec && avc->codec->name)
41  return avc->codec->name;
42  else
43  return "NULL";
44 }
45 
46 static void *codec_child_next(void *obj, void *prev)
47 {
48  AVCodecContext *s = obj;
49  if (!prev && s->codec && s->codec->priv_class && s->priv_data)
50  return s->priv_data;
51  return NULL;
52 }
53 
54 static const AVClass *codec_child_class_next(const AVClass *prev)
55 {
56  AVCodec *c = NULL;
57 
58  /* find the codec that corresponds to prev */
59  while (prev && (c = av_codec_next(c)))
60  if (c->priv_class == prev)
61  break;
62 
63  /* find next codec with priv options */
64  while (c = av_codec_next(c))
65  if (c->priv_class)
66  return c->priv_class;
67  return NULL;
68 }
69 
71  .class_name = "AVCodecContext",
72  .item_name = context_to_name,
73  .option = avcodec_options,
74  .version = LIBAVUTIL_VERSION_INT,
75  .log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),
76  .child_next = codec_child_next,
77  .child_class_next = codec_child_class_next,
78 };
79 
81 {
82  memset(s, 0, sizeof(AVCodecContext));
83 
85 
86  s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
87  s->codec = codec;
89 
90  s->time_base = (AVRational){0,1};
95  s->sample_aspect_ratio = (AVRational){0,1};
98 
100  if(codec && codec->priv_data_size){
101  if(!s->priv_data){
102  s->priv_data= av_mallocz(codec->priv_data_size);
103  if (!s->priv_data) {
104  return AVERROR(ENOMEM);
105  }
106  }
107  if(codec->priv_class){
108  *(const AVClass**)s->priv_data = codec->priv_class;
110  }
111  }
112  if (codec && codec->defaults) {
113  int ret;
114  const AVCodecDefault *d = codec->defaults;
115  while (d->key) {
116  ret = av_opt_set(s, d->key, d->value, 0);
117  av_assert0(ret >= 0);
118  d++;
119  }
120  }
121  return 0;
122 }
123 
125 {
126  AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
127 
128  if (!avctx)
129  return NULL;
130 
131  if(avcodec_get_context_defaults3(avctx, codec) < 0){
132  av_free(avctx);
133  return NULL;
134  }
135 
136  return avctx;
137 }
138 
140 {
141  AVCodecContext *avctx = *pavctx;
142 
143  if (!avctx)
144  return;
145 
146  avcodec_close(avctx);
147 
148  av_freep(&avctx->extradata);
149  av_freep(&avctx->subtitle_header);
150 
151  av_freep(pavctx);
152 }
153 
155 {
156  const AVCodec *orig_codec = dest->codec;
157  uint8_t *orig_priv_data = dest->priv_data;
158 
159  if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
160  av_log(dest, AV_LOG_ERROR,
161  "Tried to copy AVCodecContext %p into already-initialized %p\n",
162  src, dest);
163  return AVERROR(EINVAL);
164  }
165  memcpy(dest, src, sizeof(*dest));
166 
167  dest->priv_data = orig_priv_data;
168  dest->codec = orig_codec;
169 
170  /* set values specific to opened codecs back to their default state */
171  dest->slice_offset = NULL;
172  dest->hwaccel = NULL;
173  dest->internal = NULL;
174 
175  /* reallocate values that should be allocated separately */
176  dest->rc_eq = NULL;
177  dest->extradata = NULL;
178  dest->intra_matrix = NULL;
179  dest->inter_matrix = NULL;
180  dest->rc_override = NULL;
181  dest->subtitle_header = NULL;
182  if (src->rc_eq) {
183  dest->rc_eq = av_strdup(src->rc_eq);
184  if (!dest->rc_eq)
185  return AVERROR(ENOMEM);
186  }
187 
188 #define alloc_and_copy_or_fail(obj, size, pad) \
189  if (src->obj && size > 0) { \
190  dest->obj = av_malloc(size + pad); \
191  if (!dest->obj) \
192  goto fail; \
193  memcpy(dest->obj, src->obj, size); \
194  if (pad) \
195  memset(((uint8_t *) dest->obj) + size, 0, pad); \
196  }
197  alloc_and_copy_or_fail(extradata, src->extradata_size,
199  alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
200  alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
201  alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0);
202  alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 0);
204 #undef alloc_and_copy_or_fail
205 
206  return 0;
207 
208 fail:
209  av_freep(&dest->rc_override);
210  av_freep(&dest->intra_matrix);
211  av_freep(&dest->inter_matrix);
212  av_freep(&dest->extradata);
213  av_freep(&dest->rc_eq);
214  return AVERROR(ENOMEM);
215 }
216 
218 {
219  return &av_codec_context_class;
220 }
const struct AVCodec * codec
Definition: avcodec.h:1059
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int avcodec_default_execute2(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2, int, int), void *arg, int *ret, int count)
memory handling functions
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:544
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1429
static const AVOption avcodec_options[]
Definition: options_table.h:43
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
enum AVMediaType type
Definition: avcodec.h:2809
AVCodec.
Definition: avcodec.h:2796
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:154
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
const AVClass * avcodec_get_class(void)
Get the AVClass for AVCodecContext.
Definition: options.c:217
static const AVClass av_codec_context_class
Definition: options.c:70
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2432
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Definition: utils.c:882
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
uint8_t
AVOptions.
int subtitle_header_size
Definition: avcodec.h:2738
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
const AVClass * av_class
information on struct for av_log
Definition: avcodec.h:1055
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:1710
const AVCodecDefault * defaults
Private codec-specific defaults.
Definition: avcodec.h:2859
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
AVCodec * av_codec_next(const AVCodec *c)
If c is NULL, returns the first registered codec, if c is non-NULL, returns the next registered codec...
Definition: utils.c:75
int avcodec_is_open(AVCodecContext *s)
Definition: utils.c:2389
static void * codec_child_next(void *obj, void *prev)
Definition: options.c:46
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
const char * rc_eq
rate control equation
Definition: avcodec.h:2120
static const char * context_to_name(void *ptr)
Definition: options.c:37
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
int rc_override_count
ratecontrol override, see RcOverride
Definition: avcodec.h:2112
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:124
int priv_data_size
Definition: avcodec.h:2834
static const AVClass * codec_child_class_next(const AVClass *prev)
Definition: options.c:54
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
Definition: avcodec.h:2425
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:186
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
RcOverride * rc_override
Definition: avcodec.h:2113
NULL
Definition: eval.c:55
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:509
Libavcodec external API header.
enum AVMediaType codec_type
Definition: avcodec.h:1058
void avcodec_free_context(AVCodecContext **pavctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:139
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
main external API structure.
Definition: avcodec.h:1050
int extradata_size
Definition: avcodec.h:1165
uint16_t * intra_matrix
custom intra quantization matrix
Definition: avcodec.h:1591
Describe the class of an AVClass context structure.
Definition: log.h:33
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:1301
rational number numerator/denominator
Definition: rational.h:43
int(* get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags)
This callback is called at the beginning of each frame to get data buffer(s) for it.
Definition: avcodec.h:2049
uint16_t * inter_matrix
custom inter quantization matrix
Definition: avcodec.h:1598
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:2824
const uint8_t * key
Definition: internal.h:106
const uint8_t * value
Definition: internal.h:107
common internal api header.
int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec)
Set the fields of the given AVCodecContext to default values corresponding to the given codec (defaul...
Definition: options.c:80
void * priv_data
Definition: avcodec.h:1092
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2580
#define alloc_and_copy_or_fail(obj, size, pad)
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:2600
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1100
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:1420
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:210
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
Definition: utils.c:852
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:2737