History

opt: Add av_opt_copy()

This includes documentation and other modifications by Lukasz Marek and Martin Storsjö.

Michael Niedermayer committed 12 years ago

Commit 564b4591 parent 8833f154 files · +84 −2Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavutil/opt.c +66 −0
  3. libavutil/opt.h +13 −0
  4. libavutil/version.h +2 −2
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2015-08-28 · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2016-xx-xx - xxxxxxx - lavu 55.10.0 - opt.h
17+ Add av_opt_copy().
18+
1619 2016-xx-xx - xxxxxxx - lavc 57.16.0 - avcodec.h
1720 Add a new audio/video encoding and decoding API with decoupled input
1821 and output -- avcodec_send_packet(), avcodec_receive_frame(),
libavutil/opt.c+66−0
@@ -757,6 +757,72 @@ const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *pre · 756 unmodified lines
757757 return NULL;
758758 }
759759
760+static int opt_size(enum AVOptionType type)
761+{
762+ switch(type) {
763+ case AV_OPT_TYPE_INT:
764+ case AV_OPT_TYPE_FLAGS: return sizeof(int);
765+ case AV_OPT_TYPE_INT64: return sizeof(int64_t);
766+ case AV_OPT_TYPE_DOUBLE: return sizeof(double);
767+ case AV_OPT_TYPE_FLOAT: return sizeof(float);
768+ case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
769+ case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
770+ case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
771+ }
772+ return AVERROR(EINVAL);
773+}
774+
775+int av_opt_copy(void *dst, const void *src)
776+{
777+ const AVOption *o = NULL;
778+ const AVClass *c;
779+ int ret = 0;
780+
781+ if (!src)
782+ return AVERROR(EINVAL);
783+
784+ c = *(AVClass**)src;
785+ if (!c || c != *(AVClass**)dst)
786+ return AVERROR(EINVAL);
787+
788+ while ((o = av_opt_next(src, o))) {
789+ void *field_dst = ((uint8_t*)dst) + o->offset;
790+ void *field_src = ((uint8_t*)src) + o->offset;
791+ uint8_t **field_dst8 = (uint8_t**)field_dst;
792+ uint8_t **field_src8 = (uint8_t**)field_src;
793+
794+ if (o->type == AV_OPT_TYPE_STRING) {
795+ set_string(dst, o, *field_src8, field_dst8);
796+ if (*field_src8 && !*field_dst8)
797+ ret = AVERROR(ENOMEM);
798+ } else if (o->type == AV_OPT_TYPE_BINARY) {
799+ int len = *(int*)(field_src8 + 1);
800+ if (*field_dst8 != *field_src8)
801+ av_freep(field_dst8);
802+ if (len) {
803+ *field_dst8 = av_malloc(len);
804+ if (!*field_dst8) {
805+ ret = AVERROR(ENOMEM);
806+ len = 0;
807+ }
808+ memcpy(*field_dst8, *field_src8, len);
809+ } else {
810+ *field_dst8 = NULL;
811+ }
812+ *(int*)(field_dst8 + 1) = len;
813+ } else if (o->type == AV_OPT_TYPE_CONST) {
814+ // do nothing
815+ } else {
816+ int size = opt_size(o->type);
817+ if (size < 0)
818+ ret = size;
819+ else
820+ memcpy(field_dst, field_src, size);
821+ }
822+ }
823+ return ret;
824+}
825+
760826 #ifdef TEST
761827
762828 typedef struct TestContext
libavutil/opt.h+13−0
@@ -530,6 +530,19 @@ int av_opt_get_q (void *obj, const char *name, int search_flags, AVRationa · 529 unmodified lines
530530 * be freed with av_dict_free() by the caller
531531 */
532532 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val);
533+
534+/**
535+ * Copy options from src object into dest object.
536+ *
537+ * Options that require memory allocation (e.g. string or binary) are malloc'ed in dest object.
538+ * Original memory allocated for such options is freed unless both src and dest options points to the same memory.
539+ *
540+ * @param dest Object to copy from
541+ * @param src Object to copy into
542+ * @return 0 on success, negative on error
543+ */
544+int av_opt_copy(void *dest, const void *src);
545+
533546 /**
534547 * @}
535548 * @}
libavutil/version.h+2−2
@@ -54,8 +54,8 @@ · 53 unmodified lines
5454 */
5555
5656 #define LIBAVUTIL_VERSION_MAJOR 55
57−#define LIBAVUTIL_VERSION_MINOR 9
58−#define LIBAVUTIL_VERSION_MICRO 1
57+#define LIBAVUTIL_VERSION_MINOR 10
58+#define LIBAVUTIL_VERSION_MICRO 0
5959
6060 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
6161 LIBAVUTIL_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t