]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/concat.c
avisynth: Simplify shared library name construction
[ffmpeg] / libavformat / concat.c
index 47f97ef1e7b7b7d5f8643c2313f8c1ba0fbe8b76..2fb3ba9059a857f8f92cb40d0d006940f70109aa 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "avformat.h"
 #include "libavutil/avstring.h"
 #include "libavutil/mem.h"
+
+#include "avformat.h"
 #include "url.h"
 
 #define AV_CAT_SEPARATOR "|"
@@ -50,40 +51,35 @@ static av_cold int concat_close(URLContext *h)
         err |= ffurl_close(nodes[i].uc);
 
     av_freep(&data->nodes);
-    av_freep(&h->priv_data);
 
     return err < 0 ? -1 : 0;
 }
 
 static av_cold int concat_open(URLContext *h, const char *uri, int flags)
 {
-    char *node_uri = NULL, *tmp_uri;
+    char *node_uri = NULL;
     int err = 0;
     int64_t size;
-    size_t  len, i;
+    size_t len, i;
     URLContext *uc;
-    struct concat_data  *data;
+    struct concat_data  *data = h->priv_data;
     struct concat_nodes *nodes;
 
     av_strstart(uri, "concat:", &uri);
 
-    /* creating data */
-    if (!(data = av_mallocz(sizeof(*data))))
-        return AVERROR(ENOMEM);
-    h->priv_data = data;
-
-    for (i = 0, len = 1; uri[i]; i++)
-        if (uri[i] == *AV_CAT_SEPARATOR)
+    for (i = 0, len = 1; uri[i]; i++) {
+        if (uri[i] == *AV_CAT_SEPARATOR) {
             /* integer overflow */
             if (++len == UINT_MAX / sizeof(*nodes)) {
                 av_freep(&h->priv_data);
                 return AVERROR(ENAMETOOLONG);
             }
+        }
+    }
 
-    if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
-        av_freep(&h->priv_data);
+    if (!(nodes = av_realloc(NULL, sizeof(*nodes) * len)))
         return AVERROR(ENOMEM);
-    else
+    else
         data->nodes = nodes;
 
     /* handle input */
@@ -92,16 +88,14 @@ static av_cold int concat_open(URLContext *h, const char *uri, int flags)
     for (i = 0; *uri; i++) {
         /* parsing uri */
         len = strcspn(uri, AV_CAT_SEPARATOR);
-        if (!(tmp_uri = av_realloc(node_uri, len+1))) {
-            err = AVERROR(ENOMEM);
+        if ((err = av_reallocp(&node_uri, len + 1)) < 0)
             break;
-        } else
-            node_uri = tmp_uri;
-        av_strlcpy(node_uri, uri, len+1);
-        uri += len + strspn(uri+len, AV_CAT_SEPARATOR);
+        av_strlcpy(node_uri, uri, len + 1);
+        uri += len + strspn(uri + len, AV_CAT_SEPARATOR);
 
         /* creating URLContext */
-        if ((err = ffurl_open(&uc, node_uri, flags, &h->interrupt_callback)) < 0)
+        if ((err = ffurl_open(&uc, node_uri, flags,
+                              &h->interrupt_callback, NULL)) < 0)
             break;
 
         /* creating size */
@@ -120,10 +114,9 @@ static av_cold int concat_open(URLContext *h, const char *uri, int flags)
 
     if (err < 0)
         concat_close(h);
-    else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
+    else if ((err = av_reallocp(&nodes, data->length * sizeof(*nodes))) < 0)
         concat_close(h);
-        err = AVERROR(ENOMEM);
-    } else
+    else
         data->nodes = nodes;
     return err;
 }
@@ -133,16 +126,17 @@ static int concat_read(URLContext *h, unsigned char *buf, int size)
     int result, total = 0;
     struct concat_data  *data  = h->priv_data;
     struct concat_nodes *nodes = data->nodes;
-    size_t i = data->current;
+    size_t i                   = data->current;
 
     while (size > 0) {
         result = ffurl_read(nodes[i].uc, buf, size);
         if (result < 0)
             return total ? total : result;
-        if (!result)
+        if (!result) {
             if (i + 1 == data->length ||
                 ffurl_seek(nodes[++i].uc, 0, SEEK_SET) < 0)
                 break;
+        }
         total += result;
         buf   += result;
         size  -= result;
@@ -160,9 +154,7 @@ static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
 
     switch (whence) {
     case SEEK_END:
-        for (i = data->length - 1;
-             i && pos < -nodes[i].size;
-             i--)
+        for (i = data->length - 1; i && pos < -nodes[i].size; i--)
             pos += nodes[i].size;
         break;
     case SEEK_CUR:
@@ -190,9 +182,10 @@ static int64_t concat_seek(URLContext *h, int64_t pos, int whence)
 }
 
 URLProtocol ff_concat_protocol = {
-    .name      = "concat",
-    .url_open  = concat_open,
-    .url_read  = concat_read,
-    .url_seek  = concat_seek,
-    .url_close = concat_close,
+    .name           = "concat",
+    .url_open       = concat_open,
+    .url_read       = concat_read,
+    .url_seek       = concat_seek,
+    .url_close      = concat_close,
+    .priv_data_size = sizeof(struct concat_data),
 };