FFmpeg
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 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 #include "libavcodec/options.c"
23 
25 {
26  //TODO: this code should set every possible pointer that could be set by codec and is not an option;
27  ctx->extradata_size = 8;
28  ctx->extradata = av_malloc(ctx->extradata_size);
29  return 0;
30 }
31 
33 {
34  av_freep(&ctx->extradata);
35  ctx->extradata_size = 0;
36  return 0;
37 }
38 
39 static int dummy_encode(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
40 {
41  return AVERROR(ENOSYS);
42 }
43 
44 typedef struct Dummy12Context {
46  int num;
47  char* str;
49 
50 typedef struct Dummy3Context {
52  int num;
53  char* str;
55 
56 #define OFFSET(x) offsetof(Dummy12Context, x)
57 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
58 static const AVOption dummy_options[] = {
59  { "str", "set str", OFFSET(str), AV_OPT_TYPE_STRING, { .str = "i'm src default value" }, 0, 0, VE},
60  { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 1500100900 }, 0, INT_MAX, VE},
61  { NULL },
62 };
63 
64 static const AVClass dummy_v1_class = {
65  .class_name = "dummy_v1_class",
66  .item_name = av_default_item_name,
67  .option = dummy_options,
68  .version = LIBAVUTIL_VERSION_INT,
69 };
70 
71 static const AVClass dummy_v2_class = {
72  .class_name = "dummy_v2_class",
73  .item_name = av_default_item_name,
74  .option = dummy_options,
75  .version = LIBAVUTIL_VERSION_INT,
76 };
77 
78 /* codec with options */
80  .name = "dummy_v1_codec",
81  .type = AVMEDIA_TYPE_VIDEO,
82  .id = AV_CODEC_ID_NONE - 1,
83  .encode2 = dummy_encode,
84  .init = dummy_init,
85  .close = dummy_close,
86  .priv_class = &dummy_v1_class,
87  .priv_data_size = sizeof(Dummy12Context),
88 };
89 
90 /* codec with options, different class */
92  .name = "dummy_v2_codec",
93  .type = AVMEDIA_TYPE_VIDEO,
94  .id = AV_CODEC_ID_NONE - 2,
95  .encode2 = dummy_encode,
96  .init = dummy_init,
97  .close = dummy_close,
98  .priv_class = &dummy_v2_class,
99  .priv_data_size = sizeof(Dummy12Context),
100 };
101 
102 /* codec with priv data, but no class */
104  .name = "dummy_v3_codec",
105  .type = AVMEDIA_TYPE_VIDEO,
106  .id = AV_CODEC_ID_NONE - 3,
107  .encode2 = dummy_encode,
108  .init = dummy_init,
109  .close = dummy_close,
110  .priv_data_size = sizeof(Dummy3Context),
111 };
112 
113 /* codec without priv data */
115  .name = "dummy_v4_codec",
116  .type = AVMEDIA_TYPE_VIDEO,
117  .id = AV_CODEC_ID_NONE - 4,
118  .encode2 = dummy_encode,
119  .init = dummy_init,
120  .close = dummy_close,
121 };
122 
124 {
125  printf("%-14s: %dx%d prv: %s",
126  ctx->codec ? ctx->codec->name : "NULL",
127  ctx->width, ctx->height,
128  ctx->priv_data ? "set" : "null");
129  if (ctx->codec && ctx->codec->priv_class && ctx->codec->priv_data_size) {
130  int64_t i64;
131  char *str = NULL;
132  av_opt_get_int(ctx->priv_data, "num", 0, &i64);
133  av_opt_get(ctx->priv_data, "str", 0, (uint8_t**)&str);
134  printf(" opts: %"PRId64" %s", i64, str);
135  av_free(str);
136  }
137  printf("\n");
138 }
139 
140 static void test_copy(const AVCodec *c1, const AVCodec *c2)
141 {
142  AVCodecContext *ctx1, *ctx2;
143  printf("%s -> %s\nclosed:\n", c1 ? c1->name : "NULL", c2 ? c2->name : "NULL");
144  ctx1 = avcodec_alloc_context3(c1);
145  ctx2 = avcodec_alloc_context3(c2);
146  ctx1->width = ctx1->height = 128;
147  ctx1->time_base = (AVRational){12,34};
148  if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
149  av_opt_set(ctx2->priv_data, "num", "667", 0);
150  av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
151  }
152  avcodec_copy_context(ctx2, ctx1);
153  test_copy_print_codec(ctx1);
154  test_copy_print_codec(ctx2);
155  if (ctx1->codec) {
156  int ret;
157  printf("opened:\n");
158  ret = avcodec_open2(ctx1, ctx1->codec, NULL);
159  if (ret < 0) {
160  fprintf(stderr, "avcodec_open2 failed\n");
161  exit(1);
162  }
163  if (ctx2->codec && ctx2->codec->priv_class && ctx2->codec->priv_data_size) {
164  av_opt_set(ctx2->priv_data, "num", "667", 0);
165  av_opt_set(ctx2->priv_data, "str", "i'm dest value before copy", 0);
166  }
167  avcodec_copy_context(ctx2, ctx1);
168  test_copy_print_codec(ctx1);
169  test_copy_print_codec(ctx2);
170  avcodec_close(ctx1);
171  }
172  avcodec_free_context(&ctx1);
173  avcodec_free_context(&ctx2);
174 }
175 
176 int main(void)
177 {
178  AVCodec *dummy_codec[] = {
183  NULL,
184  };
185  int i, j;
186 
187  for (i = 0; dummy_codec[i]; i++)
188  avcodec_register(dummy_codec[i]);
189 
190  printf("testing avcodec_copy_context()\n");
191  for (i = 0; i < FF_ARRAY_ELEMS(dummy_codec); i++)
192  for (j = 0; j < FF_ARRAY_ELEMS(dummy_codec); j++)
193  test_copy(dummy_codec[i], dummy_codec[j]);
194  return 0;
195 }
avcodec_close
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:1117
Dummy3Context
Definition: options.c:50
AVCodec
AVCodec.
Definition: avcodec.h:3481
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
Dummy12Context::av_class
AVClass * av_class
Definition: options.c:45
dummy_options
static const AVOption dummy_options[]
Definition: options.c:58
AVCodec::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:3507
dummy_v4_encoder
static AVCodec dummy_v4_encoder
Definition: options.c:114
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption
AVOption.
Definition: opt.h:246
c1
static const uint64_t c1
Definition: murmur3.c:49
dummy_v1_encoder
static AVCodec dummy_v1_encoder
Definition: options.c:79
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:1574
dummy_v3_encoder
static AVCodec dummy_v3_encoder
Definition: options.c:103
av_opt_set
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:449
avcodec_copy_context
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:215
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:156
ctx
AVFormatContext * ctx
Definition: movenc.c:48
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
dummy_v2_encoder
static AVCodec dummy_v2_encoder
Definition: options.c:91
avcodec_free_context
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:171
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
Dummy3Context::fake_av_class
void * fake_av_class
Definition: options.c:51
dummy_close
static int dummy_close(AVCodecContext *ctx)
Definition: options.c:32
avcodec_open2
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:565
av_opt_get_int
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:888
dummy_init
static int dummy_init(AVCodecContext *ctx)
Definition: options.c:24
VE
#define VE
Definition: options.c:57
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:1688
dummy_v1_class
static const AVClass dummy_v1_class
Definition: options.c:64
Dummy3Context::num
int num
Definition: options.c:52
options.c
printf
printf("static const uint8_t my_array[100] = {\n")
Dummy12Context::str
char * str
Definition: options.c:47
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: avcodec.h:216
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
avcodec_register
av_cold void avcodec_register(AVCodec *codec)
Register the codec codec and initialize libavcodec.
Definition: allcodecs.c:833
OFFSET
#define OFFSET(x)
Definition: options.c:56
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
AVCodecContext::height
int height
Definition: avcodec.h:1738
AVCodec::priv_data_size
int priv_data_size
Definition: avcodec.h:3529
Dummy12Context
Definition: options.c:44
ret
ret
Definition: filter_design.txt:187
Dummy3Context::str
char * str
Definition: options.c:53
AVClass::class_name
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:72
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
dummy_v2_class
static const AVClass dummy_v2_class
Definition: options.c:71
test_copy
static void test_copy(const AVCodec *c1, const AVCodec *c2)
Definition: options.c:140
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
c2
static const uint64_t c2
Definition: murmur3.c:50
Dummy12Context::num
int num
Definition: options.c:46
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
dummy_encode
static int dummy_encode(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: options.c:39
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1738
av_opt_get
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:761
test_copy_print_codec
static void test_copy_print_codec(const AVCodecContext *ctx)
Definition: options.c:123
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:227
main
int main(void)
Definition: options.c:176
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370