[FFmpeg-devel] [PATCH] af_format: prefer strtok_r() over strsep()

Stefano Sabatini stefano.sabatini-lala at poste.it
Tue Aug 16 02:08:36 CEST 2011


strsep() is not POSIX compliant, and thus not supported on some platform.
Fix compilation on Solaris.
---
 configure                |    1 +
 libavfilter/af_aformat.c |   57 ++++++++++++++++++++++++++++++++-------------
 2 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/configure b/configure
index 700c959..26f6d21 100755
--- a/configure
+++ b/configure
@@ -1501,6 +1501,7 @@ tcp_protocol_deps="network"
 udp_protocol_deps="network"
 
 # filters
+aformat_filter_deps="strtok_r"
 blackframe_filter_deps="gpl"
 boxblur_filter_deps="gpl"
 cropdetect_filter_deps="gpl"
diff --git a/libavfilter/af_aformat.c b/libavfilter/af_aformat.c
index 59a2d5a..c753ea7 100644
--- a/libavfilter/af_aformat.c
+++ b/libavfilter/af_aformat.c
@@ -23,8 +23,8 @@
  * format audio filter
  */
 
-#define _BSD_SOURCE
 #include "libavutil/audioconvert.h"
+#include "libavutil/avstring.h"
 #include "avfilter.h"
 #include "internal.h"
 
@@ -35,51 +35,74 @@ typedef struct {
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     AFormatContext * const aformat = ctx->priv;
-    char *arg, *fmt_str;
+    char *fmts_str = NULL, *fmt_str, *ptr = NULL;
     int64_t fmt;
     int ret;
 
-    arg = strsep(&args, ":");
-    if (!arg) goto arg_fail;
-    if (!strcmp(arg, "all")) {
+    if (!args)
+        goto arg_fail;
+
+    fmts_str = av_get_token(&args, ":");
+    if (!fmts_str || !*fmts_str)
+        goto arg_fail;
+    if (!strcmp(fmts_str, "all")) {
         aformat->formats = avfilter_all_formats(AVMEDIA_TYPE_AUDIO);
     } else {
-        while (fmt_str = strsep(&arg, ",")) {
-            if ((ret = ff_parse_sample_format((int*)&fmt, fmt_str, ctx)) < 0)
+        for (fmt_str = fmts_str;
+             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
+            if ((ret = ff_parse_sample_format((int*)&fmt, fmt_str, ctx)) < 0) {
+                av_freep(&fmts_str);
                 return ret;
+            }
             avfilter_add_format(&aformat->formats, fmt);
         }
     }
+    av_freep(&fmts_str);
 
-    arg = strsep(&args, ":");
-    if (!arg) goto arg_fail;
-    if (!strcmp(arg, "all")) {
+    if (*args)
+        args++;
+    fmts_str = av_get_token(&args, ":");
+    if (!fmts_str || !*fmts_str)
+        goto arg_fail;
+    if (!strcmp(fmts_str, "all")) {
         aformat->chlayouts = avfilter_all_channel_layouts();
     } else {
-        while (fmt_str = strsep(&arg, ",")) {
-            if ((ret = ff_parse_channel_layout(&fmt, fmt_str, ctx)) < 0)
+        for (fmt_str = fmts_str;
+             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
+            if ((ret = ff_parse_channel_layout(&fmt, fmt_str, ctx)) < 0) {
+                av_freep(&fmts_str);
                 return ret;
+            }
             avfilter_add_format(&aformat->chlayouts, fmt);
         }
     }
+    av_freep(&fmts_str);
 
-    arg = strsep(&args, ":");
-    if (!arg) goto arg_fail;
-    if (!strcmp(arg, "all")) {
+    if (*args)
+        args++;
+    fmts_str = av_get_token(&args, ":");
+    if (!fmts_str || !*fmts_str)
+        goto arg_fail;
+    if (!strcmp(fmts_str, "all")) {
         aformat->packing = avfilter_all_packing_formats();
     } else {
-        while (fmt_str = strsep(&arg, ",")) {
-            if ((ret = ff_parse_packing_format((int*)&fmt, fmt_str, ctx)) < 0)
+        for (fmt_str = fmts_str;
+             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {
+            if ((ret = ff_parse_packing_format((int*)&fmt, fmt_str, ctx)) < 0) {
+                av_freep(&fmts_str);
                 return ret;
+            }
             avfilter_add_format(&aformat->packing, fmt);
         }
     }
+    av_freep(&fmts_str);
 
     return 0;
 
 arg_fail:
     av_log(ctx, AV_LOG_ERROR, "Invalid arguments, they must be of the form "
                               "sample_fmts:channel_layouts:packing_fmts\n");
+    av_freep(&fmts_str);
     return AVERROR(EINVAL);
 }
 
-- 
1.7.2.5



More information about the ffmpeg-devel mailing list