diff mbox

[FFmpeg-devel] avfilter/formats: optimize ff_all_formats

Message ID 20191206170056.24783-1-quinkblack@foxmail.com
State Superseded
Headers show

Commit Message

zhilizhao Dec. 6, 2019, 5 p.m. UTC
This is a micro-optimization. Saving almost 200 relocations makes it
worth a try.
---
Can lavf depends on the sequential properties of AVPixelFormat and
AVSampleFormat? Or should limited to pixdesc.h/samplefmt.h API?

 libavfilter/formats.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)
diff mbox

Patch

diff --git a/libavfilter/formats.c b/libavfilter/formats.c
index 33c64668a0..1af7a1cedd 100644
--- a/libavfilter/formats.c
+++ b/libavfilter/formats.c
@@ -348,23 +348,30 @@  int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
 
 AVFilterFormats *ff_all_formats(enum AVMediaType type)
 {
-    AVFilterFormats *ret = NULL;
+    AVFilterFormats *ret;
+    int i, count;
 
-    if (type == AVMEDIA_TYPE_VIDEO) {
-        const AVPixFmtDescriptor *desc = NULL;
-        while ((desc = av_pix_fmt_desc_next(desc))) {
-            if (ff_add_format(&ret, av_pix_fmt_desc_get_id(desc)) < 0)
-                return NULL;
-        }
-    } else if (type == AVMEDIA_TYPE_AUDIO) {
-        enum AVSampleFormat fmt = 0;
-        while (av_get_sample_fmt_name(fmt)) {
-            if (ff_add_format(&ret, fmt) < 0)
-                return NULL;
-            fmt++;
-        }
+    if (type == AVMEDIA_TYPE_VIDEO)
+        count = AV_PIX_FMT_NB;
+    else if (type == AVMEDIA_TYPE_AUDIO)
+        count = AV_SAMPLE_FMT_NB;
+    else
+        return NULL;
+
+    ret = av_mallocz(sizeof(*ret));
+    if (!ret)
+        return NULL;
+
+    ret->nb_formats = count;
+    ret->formats = av_malloc_array(count, sizeof(*ret->formats));
+    if (!ret->formats) {
+        av_free(ret);
+        return NULL;
     }
 
+    for (i = 0; i < count; i++)
+        ret->formats[i] = i;
+
     return ret;
 }