diff --git a/doc/APIchanges b/doc/APIchanges
index 33f1a1b777261e2b63a367bbc4b78b6356a256c4..9df0582e8869feca7a048199b65e82d666ad4384 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -16,6 +16,9 @@ libavutil:     2012-10-22
 API changes, most recent first:
 
 
+2013-06-05 - fc962d4 - lavu 52.13.0 - mem.h
+  Add av_realloc_array and av_reallocp_array
+
 2013-05-24 - xxxxxxx - lavfi 3.70.100 - avfilter.h
   Add support for slice multithreading to lavfi. Filters supporting threading
   are marked with AVFILTER_FLAG_SLICE_THREADS.
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 2787e453b94ae11dc0abc37811a5f7e32094c452..40465e80fca04a6359aaceb0c188de309f786219 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -3208,10 +3208,12 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
         memcpy(trk->vos_data, pkt->data, size);
     }
 
-    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
-        trk->cluster = av_realloc_f(trk->cluster, sizeof(*trk->cluster), (trk->entry + MOV_INDEX_CLUSTER_SIZE));
-        if (!trk->cluster)
-            return -1;
+    if (trk->entry >= trk->cluster_capacity) {
+        unsigned new_capacity = trk->entry + MOV_INDEX_CLUSTER_SIZE;
+        if (av_reallocp_array(&trk->cluster, new_capacity,
+                              sizeof(*trk->cluster)))
+            return AVERROR(ENOMEM);
+        trk->cluster_capacity = new_capacity;
     }
 
     trk->cluster[trk->entry].pos = avio_tell(pb) - size;
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 2c57834d107e81a7cb424abe14092ed8f5b18ca0..6b583d5577a686fd19eb7562d0d051032b4f3b69 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -102,6 +102,7 @@ typedef struct MOVTrack {
     int         vos_len;
     uint8_t     *vos_data;
     MOVIentry   *cluster;
+    unsigned    cluster_capacity;
     int         audio_vbr;
     int         height; ///< active picture (w/o VBI) height for D-10/IMX
     uint32_t    tref_tag;
diff --git a/libavutil/mem.c b/libavutil/mem.c
index 66502eb467bd3c097775af4757358c77cb1a44ff..1f2b0c3dd33d568e106a99e35e20abe7ef9a0f8e 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -180,6 +180,32 @@ void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
     return r;
 }
 
+void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
+{
+    if (size <= 0 || nmemb >= INT_MAX / size)
+        return NULL;
+    return av_realloc(ptr, nmemb * size);
+}
+
+int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
+{
+    void **ptrptr = ptr;
+    void *ret;
+    if (size <= 0 || nmemb >= INT_MAX / size)
+        return AVERROR(ENOMEM);
+    if (nmemb <= 0) {
+        av_freep(ptr);
+        return 0;
+    }
+    ret = av_realloc(*ptrptr, nmemb * size);
+    if (!ret) {
+        av_freep(ptr);
+        return AVERROR(ENOMEM);
+    }
+    *ptrptr = ret;
+    return 0;
+}
+
 void av_free(void *ptr)
 {
 #if CONFIG_MEMALIGN_HACK
diff --git a/libavutil/mem.h b/libavutil/mem.h
index a3294690cf97c32bd05a24f8535a77153b729727..fb23a690948ad833c5bc3fc3fc19515695751b10 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -122,6 +122,32 @@ void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
  */
 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
 
+/**
+ * Allocate or reallocate an array.
+ * If ptr is NULL and nmemb > 0, allocate a new block. If
+ * nmemb is zero, free the memory block pointed to by ptr.
+ * @param ptr Pointer to a memory block already allocated with
+ * av_malloc(z)() or av_realloc() or NULL.
+ * @param nmemb Number of elements
+ * @param size Size of the single element
+ * @return Pointer to a newly reallocated block or NULL if the block
+ * cannot be reallocated or the function is used to free the memory block.
+ */
+av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
+
+/**
+ * Allocate or reallocate an array.
+ * If *ptr is NULL and nmemb > 0, allocate a new block. If
+ * nmemb is zero, free the memory block pointed to by ptr.
+ * @param ptr Pointer to a pointer to a memory block already allocated
+ * with av_malloc(z)() or av_realloc(), or pointer to a pointer to NULL.
+ * The pointer is updated on success, or freed on failure.
+ * @param nmemb Number of elements
+ * @param size Size of the single element
+ * @return Zero on success, an AVERROR error code on failure.
+ */
+av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
+
 /**
  * Free a memory block which has been allocated with av_malloc(z)() or
  * av_realloc().
diff --git a/libavutil/version.h b/libavutil/version.h
index e234296b4106852eac6b45dce897f7d56b69c2a0..9941229bbc68246636951e38313d6e6bd474ee9b 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -75,7 +75,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  52
-#define LIBAVUTIL_VERSION_MINOR  34
+#define LIBAVUTIL_VERSION_MINOR  35
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \