[FFmpeg-devel] [PATCH 1/2] lavu/opt: add AV_OPT_TYPE_DURATION.

Nicolas George nicolas.george at normalesup.org
Sat Mar 16 16:44:05 CET 2013


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 doc/APIchanges      |    3 +++
 libavutil/opt.c     |   30 +++++++++++++++++++++++++++++-
 libavutil/opt.h     |    1 +
 libavutil/version.h |    2 +-
 4 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 2bde666..3f33e03 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2012-10-22
 
 API changes, most recent first:
 
+2013-03-16 - xxxxxxx - lavu 52.20.100 - opt.h
+  Add AV_OPT_TYPE_DURATION value to AVOptionType enum.
+
 2013-03-07 - xxxxxx - lavu 52.18.100 - avstring.h,bprint.h
   Add av_escape() and av_bprint_escape() API.
 
diff --git a/libavutil/opt.c b/libavutil/opt.c
index f91d18b..b4c2dc7 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -77,6 +77,7 @@ static int read_number(const AVOption *o, void *dst, double *num, int *den, int6
     case AV_OPT_TYPE_PIXEL_FMT:
     case AV_OPT_TYPE_SAMPLE_FMT:
     case AV_OPT_TYPE_INT:       *intnum = *(int         *)dst;return 0;
+    case AV_OPT_TYPE_DURATION:
     case AV_OPT_TYPE_INT64:     *intnum = *(int64_t     *)dst;return 0;
     case AV_OPT_TYPE_FLOAT:     *num    = *(float       *)dst;return 0;
     case AV_OPT_TYPE_DOUBLE:    *num    = *(double      *)dst;return 0;
@@ -101,6 +102,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int
     case AV_OPT_TYPE_PIXEL_FMT:
     case AV_OPT_TYPE_SAMPLE_FMT:
     case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
+    case AV_OPT_TYPE_DURATION:
     case AV_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
     case AV_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
     case AV_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
@@ -256,7 +258,7 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
         return AVERROR_OPTION_NOT_FOUND;
     if (!val && (o->type != AV_OPT_TYPE_STRING &&
                  o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
-                 o->type != AV_OPT_TYPE_IMAGE_SIZE))
+                 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_DURATION))
         return AVERROR(EINVAL);
 
     dst = ((uint8_t*)target_obj) + o->offset;
@@ -310,6 +312,15 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
         }
         *(enum AVSampleFormat *)dst = ret;
         return 0;
+    case AV_OPT_TYPE_DURATION:
+        if (!val) {
+            *(int64_t *)dst = 0;
+            return 0;
+        } else {
+            if ((ret = av_parse_time(dst, val, 1)) < 0)
+                av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
+            return ret;
+        }
     }
 
     av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
@@ -530,6 +541,7 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
     uint8_t *bin, buf[128];
     int len, i, ret;
+    int64_t i64;
 
     if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
         return AVERROR_OPTION_NOT_FOUND;
@@ -570,6 +582,12 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
     case AV_OPT_TYPE_SAMPLE_FMT:
         ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
         break;
+    case AV_OPT_TYPE_DURATION:
+        i64 = *(int64_t *)dst;
+        ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d",
+                       i64 / 3600000000, (int)((i64 / 60000000) % 60),
+                       (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
+        break;
     default:
         return AVERROR(EINVAL);
     }
@@ -815,6 +833,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit,
             case AV_OPT_TYPE_SAMPLE_FMT:
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
                 break;
+            case AV_OPT_TYPE_DURATION:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
+                break;
             case AV_OPT_TYPE_CONST:
             default:
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
@@ -891,6 +912,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags)
             case AV_OPT_TYPE_FLAGS:
             case AV_OPT_TYPE_INT:
             case AV_OPT_TYPE_INT64:
+            case AV_OPT_TYPE_DURATION:
                 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
             break;
             case AV_OPT_TYPE_DOUBLE:
@@ -1253,6 +1275,7 @@ int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const ch
     case AV_OPT_TYPE_SAMPLE_FMT:
     case AV_OPT_TYPE_FLOAT:
     case AV_OPT_TYPE_DOUBLE:
+    case AV_OPT_TYPE_DURATION:
         break;
     case AV_OPT_TYPE_STRING:
         range->component_min = 0;
@@ -1311,6 +1334,7 @@ typedef struct TestContext
     int w, h;
     enum AVPixelFormat pix_fmt;
     enum AVSampleFormat sample_fmt;
+    int64_t duration;
 } TestContext;
 
 #define OFFSET(x) offsetof(TestContext, x)
@@ -1331,6 +1355,7 @@ static const AVOption test_options[]= {
 {"size",     "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
 {"pix_fmt",  "set pixfmt",     OFFSET(pix_fmt),  AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
+{"duration", "set duration",   OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
 {NULL},
 };
 
@@ -1381,6 +1406,9 @@ int main(void)
             "sample_fmt=s16",
             "sample_fmt=2",
             "sample_fmt=bogus",
+            "duration=bogus",
+            "duration=123.45",
+            "duration=1\\:23\\:45.67",
         };
 
         test_ctx.class = &test_class;
diff --git a/libavutil/opt.h b/libavutil/opt.h
index baf1b82..1c86e6f 100644
--- a/libavutil/opt.h
+++ b/libavutil/opt.h
@@ -230,6 +230,7 @@ enum AVOptionType{
     AV_OPT_TYPE_IMAGE_SIZE = MKBETAG('S','I','Z','E'), ///< offset must point to two consecutive integers
     AV_OPT_TYPE_PIXEL_FMT  = MKBETAG('P','F','M','T'),
     AV_OPT_TYPE_SAMPLE_FMT = MKBETAG('S','F','M','T'),
+    AV_OPT_TYPE_DURATION   = MKBETAG('D','U','R',' '),
 #if FF_API_OLD_AVOPTIONS
     FF_OPT_TYPE_FLAGS = 0,
     FF_OPT_TYPE_INT,
diff --git a/libavutil/version.h b/libavutil/version.h
index d95a8a2..1ff9ffe 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -75,7 +75,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  52
-#define LIBAVUTIL_VERSION_MINOR  19
+#define LIBAVUTIL_VERSION_MINOR  20
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list