[vlc-devel] [PATCH 2/2] core: change var_InheritURational to output a vlc_rational_t

Steve Lhomme robux4 at videolabs.io
Wed Apr 12 12:55:20 CEST 2017


--
replaces https://patches.videolan.org/patch/16341/
- return the vlc_urational_t directly (0/0 in case of error)
---
 include/vlc_variables.h                  |  4 ++--
 modules/access/decklink.cpp              |  5 ++---
 modules/access/imem.c                    |  7 ++++---
 modules/access/timecode.c                |  5 ++---
 modules/access/v4l2/video.c              | 11 +++++------
 modules/demux/image.c                    |  6 +++---
 modules/demux/rawvid.c                   |  7 ++++---
 modules/stream_out/transcode/transcode.c |  3 ++-
 modules/video_filter/fps.c               |  3 ++-
 src/misc/variables.c                     | 23 ++++++++---------------
 src/video_output/video_output.c          |  5 ++---
 test/src/misc/variables.c                | 32 ++++++++++++++++----------------
 12 files changed, 52 insertions(+), 59 deletions(-)

diff --git a/include/vlc_variables.h b/include/vlc_variables.h
index 83752b171f..a38e2e2770 100644
--- a/include/vlc_variables.h
+++ b/include/vlc_variables.h
@@ -652,8 +652,8 @@ static inline void *var_InheritAddress( vlc_object_t *obj, const char *name )
 }
 #define var_InheritAddress(o, n) var_InheritAddress(VLC_OBJECT(o), n)
 
-VLC_API int var_InheritURational( vlc_object_t *, unsigned *num, unsigned *den, const char *var );
-#define var_InheritURational(a,b,c,d) var_InheritURational(VLC_OBJECT(a), b, c, d)
+VLC_API vlc_urational_t var_InheritURational( vlc_object_t *, const char *var );
+#define var_InheritURational(a,b) var_InheritURational(VLC_OBJECT(a), b)
 
 #define var_GetInteger(a,b)   var_GetInteger( VLC_OBJECT(a),b)
 #define var_GetBool(a,b)   var_GetBool( VLC_OBJECT(a),b)
diff --git a/modules/access/decklink.cpp b/modules/access/decklink.cpp
index 790afca1e6..20f0a1eab9 100644
--- a/modules/access/decklink.cpp
+++ b/modules/access/decklink.cpp
@@ -196,9 +196,8 @@ static es_format_t GetModeSettings(demux_t *demux, IDeckLinkDisplayMode *m)
     video_fmt.video.i_frame_rate_base = frame_duration;
     video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8;
 
-    vlc_urational_t aspect;
-    if (!var_InheritURational(demux, &aspect.num, &aspect.den, "decklink-aspect-ratio") &&
-         aspect.num > 0 && aspect.den > 0) {
+    vlc_urational_t aspect = var_InheritURational(demux, "decklink-aspect-ratio");
+    if (aspect.num != 0 && aspect.den != 0) {
         video_fmt.video.i_sar_num = aspect.num * video_fmt.video.i_height;
         video_fmt.video.i_sar_den = aspect.den * video_fmt.video.i_width;
     }
diff --git a/modules/access/imem.c b/modules/access/imem.c
index 8f8d38687a..8e1bf0339e 100644
--- a/modules/access/imem.c
+++ b/modules/access/imem.c
@@ -430,14 +430,15 @@ static int OpenDemux(vlc_object_t *object)
         fmt.i_cat = VIDEO_ES;
         fmt.video.i_width  = var_InheritInteger(object, "imem-width");
         fmt.video.i_height = var_InheritInteger(object, "imem-height");
-        vlc_urational_t rat;
-        if (!var_InheritURational(object, &rat.num, &rat.den, "imem-dar") && rat.num && rat.den) {
+        vlc_urational_t rat = var_InheritURational(object, "imem-dar");
+        if (rat.num && rat.den) {
             if (fmt.video.i_width != 0 && fmt.video.i_height != 0) {
                 fmt.video.i_sar_num = rat.num * fmt.video.i_height;
                 fmt.video.i_sar_den = rat.den * fmt.video.i_width;
             }
         }
-        if (!var_InheritURational(object, &rat.num, &rat.den, "imem-fps") && rat.num && rat.den) {
+        rat = var_InheritURational(object, "imem-fps");
+        if (rat.num && rat.den) {
             fmt.video.i_frame_rate      = rat.num;
             fmt.video.i_frame_rate_base = rat.den;
         }
diff --git a/modules/access/timecode.c b/modules/access/timecode.c
index 4cb2998b95..3d3748b6c4 100644
--- a/modules/access/timecode.c
+++ b/modules/access/timecode.c
@@ -177,9 +177,8 @@ static int Open (vlc_object_t *obj)
     es_format_Init (&fmt, SPU_ES, VLC_CODEC_ITU_T140);
     sys->es = es_out_Add (demux->out, &fmt);
 
-    vlc_urational_t fps;
-    if (var_InheritURational (demux, &fps.num, &fps.den, "timecode-fps")
-     || !fps.num || !fps.den)
+    vlc_urational_t fps = var_InheritURational (demux, "timecode-fps");
+    if (!fps.num || !fps.den)
     {
         msg_Err (demux, "invalid frame rate");
         free (sys);
diff --git a/modules/access/v4l2/video.c b/modules/access/v4l2/video.c
index a87222b100..b420767edf 100644
--- a/modules/access/v4l2/video.c
+++ b/modules/access/v4l2/video.c
@@ -437,13 +437,12 @@ int SetupFormat (vlc_object_t *obj, int fd, uint32_t fourcc,
     struct v4l2_fract best_it = infinity, min_it;
     uint64_t best_area = 0;
 
-    vlc_urational_t fps;
-    if (var_InheritURational(obj, &fps.den, &fps.num,
-                             CFG_PREFIX"fps") == VLC_SUCCESS)
+    vlc_urational_t fps = var_InheritURational(obj, CFG_PREFIX"fps");
+    if (fps.num != 0 && fps.den != 0)
     {
-        msg_Dbg (obj, " requested frame internal: %u/%u", fps.num, fps.den);
-        min_it.numerator   = fps.num;
-        min_it.denominator = fps.den;
+        msg_Dbg (obj, " requested frame internal: %u/%u", fps.den, fps.num);
+        min_it.numerator   = fps.den;
+        min_it.denominator = fps.num;
     }
     else
         min_it = zero;
diff --git a/modules/demux/image.c b/modules/demux/image.c
index 9ea0155f9a..191bbcc927 100644
--- a/modules/demux/image.c
+++ b/modules/demux/image.c
@@ -665,9 +665,9 @@ static int Open(vlc_object_t *object)
     }
     fmt.i_id    = var_InheritInteger(demux, "image-id");
     fmt.i_group = var_InheritInteger(demux, "image-group");
-    vlc_urational_t fps;
-    if (var_InheritURational(demux, &fps.num, &fps.den, "image-fps") ||
-        !fps.num || !fps.den) {
+    vlc_urational_t fps = var_InheritURational(demux, "image-fps");
+    if (!fps.num || !fps.den)
+    {
         msg_Err(demux, "Invalid frame rate, using 10/1 instead");
         fps.num      = 10;
         fps.den      = 1;
diff --git a/modules/demux/rawvid.c b/modules/demux/rawvid.c
index 8208abd073..b97f4dabb5 100644
--- a/modules/demux/rawvid.c
+++ b/modules/demux/rawvid.c
@@ -291,14 +291,15 @@ valid:
         free( psz_tmp );
     }
 
-    if( var_InheritURational( p_demux, &fps.num, &fps.den, "rawvid-fps" ) )
+    fps = var_InheritURational( p_demux, "rawvid-fps" );
+    if( fps.num == 0 || fps.den == 0 )
     {
         fps.num = 0;
         fps.den = 1;
     }
 
-    if( var_InheritURational( p_demux, &sar.num, &sar.den,
-                              "rawvid-aspect-ratio" ) )
+    sar = var_InheritURational( p_demux, "rawvid-aspect-ratio" );
+    if( sar.num == 0 || sar.den == 0 )
         sar.num = sar.den = 1;
 
     /* moan about anything wrong */
diff --git a/modules/stream_out/transcode/transcode.c b/modules/stream_out/transcode/transcode.c
index 0f91d252c6..badbb7a8aa 100644
--- a/modules/stream_out/transcode/transcode.c
+++ b/modules/stream_out/transcode/transcode.c
@@ -346,7 +346,8 @@ static int Open( vlc_object_t *p_this )
 
     p_sys->f_scale = var_GetFloat( p_stream, SOUT_CFG_PREFIX "scale" );
 
-    p_sys->b_master_sync = var_InheritURational( p_stream, &p_sys->fps.num, &p_sys->fps.den, SOUT_CFG_PREFIX "fps" ) == VLC_SUCCESS;
+    p_sys->fps = var_InheritURational( p_stream, SOUT_CFG_PREFIX "fps" );
+    p_sys->b_master_sync = p_sys->fps.num != 0 && p_sys->fps.den != 0;
 
     p_sys->i_width = var_GetInteger( p_stream, SOUT_CFG_PREFIX "width" );
 
diff --git a/modules/video_filter/fps.c b/modules/video_filter/fps.c
index 4126dd98a3..fb6f0558ff 100644
--- a/modules/video_filter/fps.c
+++ b/modules/video_filter/fps.c
@@ -151,7 +151,8 @@ static int Open( vlc_object_t *p_this)
     video_format_Copy( &p_filter->fmt_out.video, &p_filter->fmt_in.video );
 
     /* If we don't have fps option, use filter output values */
-    if( var_InheritURational( p_filter, &fps.num, &fps.den, CFG_PREFIX "fps" ) )
+    fps = var_InheritURational( p_filter, CFG_PREFIX "fps" );
+    if (fps.num == 0 || fps.den == 0)
     {
         fps.num = p_filter->fmt_in.video.i_frame_rate;
         fps.den = p_filter->fmt_in.video.i_frame_rate_base;
diff --git a/src/misc/variables.c b/src/misc/variables.c
index 72296111e1..1bd6add43e 100644
--- a/src/misc/variables.c
+++ b/src/misc/variables.c
@@ -1213,10 +1213,9 @@ int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
  * It returns an error if the rational number cannot be parsed (0/0 is valid).
  * The rational is already reduced.
  */
-int (var_InheritURational)(vlc_object_t *object,
-                           unsigned *num, unsigned *den,
-                           const char *var)
+vlc_urational_t (var_InheritURational)(vlc_object_t *object, const char *var)
 {
+    vlc_urational_t dst = {.num = 0, .den = 0};
     char *str = var_InheritString(object, var);
     if (str == NULL)
         goto error;
@@ -1260,24 +1259,18 @@ int (var_InheritURational)(vlc_object_t *object,
             goto error;
     }
 
-    free(str);
-
     if (n == 0) {
-        *num = 0;
-        *den = d ? 1 : 0;
+        dst.num = 0;
+        dst.den = d ? 1 : 0;
     } else if (d == 0) {
-        *num = 1;
-        *den = 0;
+        dst.num = 1;
+        dst.den = 0;
     } else
-        vlc_ureduce(num, den, n, d, 0);
-
-    return VLC_SUCCESS;
+        vlc_ureduce(&dst.num, &dst.den, n, d, 0);
 
 error:
     free(str);
-    *num = 0;
-    *den = 0;
-    return VLC_EGENERIC;
+    return dst;
 }
 
 /**
diff --git a/src/video_output/video_output.c b/src/video_output/video_output.c
index 2904e84e2b..346e870146 100644
--- a/src/video_output/video_output.c
+++ b/src/video_output/video_output.c
@@ -576,9 +576,8 @@ static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, cons
     cfg->display.width   = display_width > 0  ? display_width  : 0;
     cfg->display.height  = display_height > 0 ? display_height : 0;
     cfg->is_display_filled  = var_GetBool(vout, "autoscale");
-    if (var_InheritURational(vout, &cfg->display.sar.num, &cfg->display.sar.den,
-                             "monitor-par") |
-            !cfg->display.sar.num  || !cfg->display.sar.den) {
+    cfg->display.sar = var_InheritURational(vout, "monitor-par");
+    if (!cfg->display.sar.num  || !cfg->display.sar.den) {
         cfg->display.sar.num = 1;
         cfg->display.sar.den = 1;
     }
diff --git a/test/src/misc/variables.c b/test/src/misc/variables.c
index c58ac9a3d1..0773e29529 100644
--- a/test/src/misc/variables.c
+++ b/test/src/misc/variables.c
@@ -104,63 +104,63 @@ static void test_fracts( libvlc_int_t *p_libvlc )
     vlc_urational_t frac;
 
     var_Create( p_libvlc, name, VLC_VAR_STRING );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) != 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) != 0 );
 
     var_SetString( p_libvlc, name, "123garbage" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) != 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) != 0 );
 
     var_SetString( p_libvlc, name, "4/5garbage" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) != 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) != 0 );
 
     var_SetString( p_libvlc, name, "6.7garbage" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) != 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) != 0 );
 
     var_SetString( p_libvlc, name, "." );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 0 && frac.den == 1 );
 
     var_SetString( p_libvlc, name, "010" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 10 && frac.den == 1 );
 
     var_SetString( p_libvlc, name, "30" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 30 && frac.den == 1 );
 
     var_SetString( p_libvlc, name, "30.0" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 30 && frac.den == 1 );
 
     var_SetString( p_libvlc, name, "030.030" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 3003 && frac.den == 100 );
 
     var_SetString( p_libvlc, name, "60/2" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 30 && frac.den == 1 );
 
     var_SetString( p_libvlc, name, "29.97" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 2997 && frac.den == 100 );
 
     var_SetString( p_libvlc, name, "30000/1001" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 30000 && frac.den == 1001 );
 
     var_SetString( p_libvlc, name, ".125" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 1 && frac.den == 8 );
 
     var_SetString( p_libvlc, name, "12:9" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 4 && frac.den == 3 );
 
     var_SetString( p_libvlc, name, "000000/00000000" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 0 && frac.den == 0 );
 
     var_SetString( p_libvlc, name, "12345/0" );
-    assert( var_InheritURational( p_libvlc, &frac.num, &frac.den, name ) == 0 );
+    assert( var_InheritURational( p_libvlc, &frac, name ) == 0 );
     assert( frac.num == 1 && frac.den == 0 );
 
     var_Destroy( p_libvlc, name );
-- 
2.11.1



More information about the vlc-devel mailing list