[FFmpeg-devel] [PATCH 3/3] Implement inline function av_fill_image_max_step() and use it for factorizing code.

Stefano Sabatini stefano.sabatini-lala
Thu Aug 12 01:23:30 CEST 2010


---
 libavcore/imgutils.c   |   24 +++---------------------
 libavcore/imgutils.h   |   31 ++++++++++++++++++++++++++++++-
 libavfilter/avfilter.c |    1 +
 libavfilter/vf_crop.c  |   11 ++---------
 4 files changed, 36 insertions(+), 31 deletions(-)

diff --git a/libavcore/imgutils.c b/libavcore/imgutils.c
index 84db01a..d79c9f1 100644
--- a/libavcore/imgutils.c
+++ b/libavcore/imgutils.c
@@ -29,21 +29,12 @@ int av_get_image_linesize(enum PixelFormat pix_fmt, int width, int plane)
     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
     int max_step     [4];       /* max pixel step for each plane */
     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
-    int s, i;
+    int s;
 
     if (desc->flags & PIX_FMT_BITSTREAM)
         return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
 
-    memset(max_step     , 0, sizeof(max_step     ));
-    memset(max_step_comp, 0, sizeof(max_step_comp));
-    for (i = 0; i < 4; i++) {
-        const AVComponentDescriptor *comp = &(desc->comp[i]);
-        if ((comp->step_minus1+1) > max_step[comp->plane]) {
-            max_step     [comp->plane] = comp->step_minus1+1;
-            max_step_comp[comp->plane] = i;
-        }
-    }
-
+    av_fill_image_max_step(max_step, max_step_comp, desc);
     s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
     return max_step[plane] * (((width + (1 << s) - 1)) >> s);
 }
@@ -65,16 +56,7 @@ int av_fill_image_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int widt
         return 0;
     }
 
-    memset(max_step     , 0, sizeof(max_step     ));
-    memset(max_step_comp, 0, sizeof(max_step_comp));
-    for (i = 0; i < 4; i++) {
-        const AVComponentDescriptor *comp = &(desc->comp[i]);
-        if ((comp->step_minus1+1) > max_step[comp->plane]) {
-            max_step     [comp->plane] = comp->step_minus1+1;
-            max_step_comp[comp->plane] = i;
-        }
-    }
-
+    av_fill_image_max_step(max_step, max_step_comp, desc);
     for (i = 0; i < 4; i++) {
         int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
         linesizes[i] = max_step[i] * (((width + (1 << s) - 1)) >> s);
diff --git a/libavcore/imgutils.h b/libavcore/imgutils.h
index c2cf6eb..2f141bf 100644
--- a/libavcore/imgutils.h
+++ b/libavcore/imgutils.h
@@ -24,10 +24,39 @@
  * misc image utilities
  */
 
-#include "libavutil/pixfmt.h"
+#include "libavutil/pixdesc.h"
 #include "avcore.h"
 
 /**
+ * Compute the max pixel step for each plane of an image with a
+ * format described by pix_desc.
+ *
+ * The pixel step is the number of bytes between the bytes of a pixel
+ * component which describe a pixel. Since a plane may contain
+ * different pixel components, the maximum step is relative to the
+ * component with the max step.
+ *
+ * @param max_step_comp put here the array containing the component
+ * for each plane which has the max pixel step. May be NULL.
+ */
+static inline void av_fill_image_max_step(int max_step[4], int max_step_comp[4], const AVPixFmtDescriptor *pix_desc)
+{
+    int i;
+    memset(max_step     , 0, 4*sizeof(max_step     [0]));
+    if (max_step_comp)
+        memset(max_step_comp, 0, 4*sizeof(max_step_comp[0]));
+
+    for (i = 0; i < 4; i++) {
+        const AVComponentDescriptor *comp = &(pix_desc->comp[i]);
+        if ((comp->step_minus1+1) > max_step[comp->plane]) {
+            max_step     [comp->plane] = comp->step_minus1+1;
+            if (max_step_comp)
+                max_step_comp[comp->plane] = i;
+        }
+    }
+}
+
+/**
  * Compute the size of an image line with format pix_fmt and width
  * width for the plane plane.
  *
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index a6bc248..bc0bf6b 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -23,6 +23,7 @@
 
 #include "libavcodec/imgconvert.h"
 #include "libavutil/pixdesc.h"
+#include "libavcore/imgutils.h"
 #include "avfilter.h"
 #include "internal.h"
 
diff --git a/libavfilter/vf_crop.c b/libavfilter/vf_crop.c
index f861651..db9fd03 100644
--- a/libavfilter/vf_crop.c
+++ b/libavfilter/vf_crop.c
@@ -24,7 +24,7 @@
  */
 
 #include "avfilter.h"
-#include "libavutil/pixdesc.h"
+#include "libavcore/imgutils.h"
 
 typedef struct {
     int  x;             ///< x offset of the non-cropped area with respect to the input area
@@ -83,18 +83,11 @@ static int config_input(AVFilterLink *link)
     AVFilterContext *ctx = link->dst;
     CropContext *crop = ctx->priv;
     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
-    int i;
-
-    memset(crop->max_step, 0, sizeof(crop->max_step));
-    for (i = 0; i < 4; i++) {
-        const AVComponentDescriptor *comp = &(pix_desc->comp[i]);
-        if ((comp->step_minus1+1) > crop->max_step[comp->plane])
-            crop->max_step[comp->plane] = comp->step_minus1+1;
-    }
 
     crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
     crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
 
+    av_fill_image_max_step(crop->max_step, NULL, pix_desc);
     if (crop->w == 0)
         crop->w = link->w - crop->x;
     if (crop->h == 0)
-- 
1.7.0.4




More information about the ffmpeg-devel mailing list