[FFmpeg-devel] [PATCH 1/3] Implement av_get_image_linesize() and use it in ff_get_plane_bytewidth().

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


The new implementation is more generic, more compact and more correct.
---
 libavcodec/imgconvert.c |   48 +----------------------------------------------
 libavcore/imgutils.c    |   24 +++++++++++++++++++++++
 libavcore/imgutils.h    |    8 +++++++
 3 files changed, 33 insertions(+), 47 deletions(-)

diff --git a/libavcodec/imgconvert.c b/libavcodec/imgconvert.c
index aacc4f8..65b40fd 100644
--- a/libavcodec/imgconvert.c
+++ b/libavcodec/imgconvert.c
@@ -796,53 +796,7 @@ void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
 
 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
 {
-    int bits;
-    const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
-    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
-
-    pf = &pix_fmt_info[pix_fmt];
-    switch(pf->pixel_type) {
-    case FF_PIXEL_PACKED:
-        switch(pix_fmt) {
-        case PIX_FMT_YUYV422:
-        case PIX_FMT_UYVY422:
-        case PIX_FMT_RGB565BE:
-        case PIX_FMT_RGB565LE:
-        case PIX_FMT_RGB555BE:
-        case PIX_FMT_RGB555LE:
-        case PIX_FMT_RGB444BE:
-        case PIX_FMT_RGB444LE:
-        case PIX_FMT_BGR565BE:
-        case PIX_FMT_BGR565LE:
-        case PIX_FMT_BGR555BE:
-        case PIX_FMT_BGR555LE:
-        case PIX_FMT_BGR444BE:
-        case PIX_FMT_BGR444LE:
-            bits = 16;
-            break;
-        case PIX_FMT_UYYVYY411:
-            bits = 12;
-            break;
-        default:
-            bits = pf->depth * pf->nb_channels;
-            break;
-        }
-        return (width * bits + 7) >> 3;
-        break;
-    case FF_PIXEL_PLANAR:
-            if ((pix_fmt != PIX_FMT_NV12 && pix_fmt != PIX_FMT_NV21) &&
-                (plane == 1 || plane == 2))
-                width= -((-width)>>desc->log2_chroma_w);
-
-            return (width * pf->depth + 7) >> 3;
-        break;
-    case FF_PIXEL_PALETTE:
-        if (plane == 0)
-            return width;
-        break;
-    }
-
-    return -1;
+    return av_get_image_linesize(pix_fmt, width, plane);
 }
 
 void av_picture_data_copy(uint8_t *dst_data[4], int dst_linesize[4],
diff --git a/libavcore/imgutils.c b/libavcore/imgutils.c
index f3d75ee..84db01a 100644
--- a/libavcore/imgutils.c
+++ b/libavcore/imgutils.c
@@ -24,6 +24,30 @@
 #include "imgutils.h"
 #include "libavutil/pixdesc.h"
 
+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;
+
+    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;
+        }
+    }
+
+    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);
+}
+
 int av_fill_image_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
 {
     int i;
diff --git a/libavcore/imgutils.h b/libavcore/imgutils.h
index be6886c..c2cf6eb 100644
--- a/libavcore/imgutils.h
+++ b/libavcore/imgutils.h
@@ -28,6 +28,14 @@
 #include "avcore.h"
 
 /**
+ * Compute the size of an image line with format pix_fmt and width
+ * width for the plane plane.
+ *
+ * @return the computed size in bytes
+ */
+int av_get_image_linesize(enum PixelFormat pix_fmt, int width, int plane);
+
+/**
  * Fill plane linesizes for an image with pixel format pix_fmt and
  * width width.
  *
-- 
1.7.0.4




More information about the ffmpeg-devel mailing list