[FFmpeg-devel] [PATCH 5/5] avfilter/drawutils: support native-endian instead of little endian

Muhammad Faiz mfcc64 at gmail.com
Sun Jan 27 11:36:19 EET 2019


Signed-off-by: Muhammad Faiz <mfcc64 at gmail.com>
---
Old thread is here: https://ffmpeg.org/pipermail/ffmpeg-devel/2016-June/195941.html
Need someone test it on big-endian machine.

 libavfilter/drawutils.c     | 48 +++++++++++++++++++++----------------
 libavfilter/vf_rotate.c     | 26 ++++++++++----------
 libavfilter/vf_tinterlace.c | 30 +++++++++++------------
 3 files changed, 54 insertions(+), 50 deletions(-)

diff --git a/libavfilter/drawutils.c b/libavfilter/drawutils.c
index 5f4cb548f0..12bdfcb900 100644
--- a/libavfilter/drawutils.c
+++ b/libavfilter/drawutils.c
@@ -175,6 +175,17 @@ void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
     }
 }
 
+static int is_native_endian(const AVPixFmtDescriptor *desc)
+{
+    int len = strlen(desc->name);
+
+    if (!strcmp(desc->name + len - 2, "be"))
+        return HAVE_BIGENDIAN;
+    if (!strcmp(desc->name + len - 2, "le"))
+        return !HAVE_BIGENDIAN;
+    return 1;
+}
+
 int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
 {
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
@@ -185,20 +196,20 @@ int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
 
     if (!desc || !desc->name)
         return AVERROR(EINVAL);
-    if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | FF_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA))
+    if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | FF_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA | AV_PIX_FMT_FLAG_BE))
         return AVERROR(ENOSYS);
     if (format == AV_PIX_FMT_P010LE || format == AV_PIX_FMT_P010BE || format == AV_PIX_FMT_P016LE || format == AV_PIX_FMT_P016BE)
         return AVERROR(ENOSYS);
     if (format == AV_PIX_FMT_YUVJ420P || format == AV_PIX_FMT_YUVJ422P || format == AV_PIX_FMT_YUVJ444P ||
         format == AV_PIX_FMT_YUVJ411P || format == AV_PIX_FMT_YUVJ440P)
         full_range = 1;
+    if (!is_native_endian(desc))
+        return AVERROR(ENOSYS);
     for (i = 0; i < desc->nb_components; i++) {
         c = &desc->comp[i];
         /* for now, only 8-16 bits formats */
         if (c->depth < 8 || c->depth > 16)
             return AVERROR(ENOSYS);
-        if (desc->flags & AV_PIX_FMT_FLAG_BE)
-            return AVERROR(ENOSYS);
         if (c->plane >= MAX_PLANES)
             return AVERROR(ENOSYS);
         /* strange interleaving */
@@ -268,11 +279,11 @@ void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4
         EXPAND(1);
         EXPAND(0);
     } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A ||
-               draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE ||
-               draw->format == AV_PIX_FMT_GRAY9LE  ||
-               draw->format == AV_PIX_FMT_GRAY10LE ||
-               draw->format == AV_PIX_FMT_GRAY12LE ||
-               draw->format == AV_PIX_FMT_GRAY14LE) {
+               draw->format == AV_PIX_FMT_GRAY16 || draw->format == AV_PIX_FMT_YA16 ||
+               draw->format == AV_PIX_FMT_GRAY9  ||
+               draw->format == AV_PIX_FMT_GRAY10 ||
+               draw->format == AV_PIX_FMT_GRAY12 ||
+               draw->format == AV_PIX_FMT_GRAY14) {
         const AVPixFmtDescriptor *desc = draw->desc;
         color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
         EXPAND(0);
@@ -331,11 +342,6 @@ void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
             return;
         p = p0;
 
-        if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
-            for (x = 0; 2*x < draw->pixelstep[plane]; x++)
-                color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
-        }
-
         /* copy first line from color */
         for (x = 0; x < wp; x++) {
             memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
@@ -426,19 +432,19 @@ static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
 
     if (left) {
         unsigned suba = (left * alpha) >> hsub;
-        uint16_t value = AV_RL16(dst);
-        AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
+        uint16_t value = AV_RN16A(dst);
+        AV_WN16A(dst, (value * (0x10001 - suba) + src * suba) >> 16);
         dst += dx;
     }
     for (x = 0; x < w; x++) {
-        uint16_t value = AV_RL16(dst);
-        AV_WL16(dst, (value * tau + asrc) >> 16);
+        uint16_t value = AV_RN16A(dst);
+        AV_WN16A(dst, (value * tau + asrc) >> 16);
         dst += dx;
     }
     if (right) {
         unsigned suba = (right * alpha) >> hsub;
-        uint16_t value = AV_RL16(dst);
-        AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
+        uint16_t value = AV_RN16A(dst);
+        AV_WN16A(dst, (value * (0x10001 - suba) + src * suba) >> 16);
     }
 }
 
@@ -531,7 +537,7 @@ static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
     unsigned xmmod = 7 >> l2depth;
     unsigned mbits = (1 << (1 << l2depth)) - 1;
     unsigned mmult = 255 / mbits;
-    uint16_t value = AV_RL16(dst);
+    uint16_t value = AV_RN16A(dst);
 
     for (y = 0; y < h; y++) {
         xm = xm0;
@@ -543,7 +549,7 @@ static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
         mask += mask_linesize;
     }
     alpha = (t >> shift) * alpha;
-    AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
+    AV_WN16A(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
 }
 
 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
diff --git a/libavfilter/vf_rotate.c b/libavfilter/vf_rotate.c
index 371ff7f722..0ed8ebff5b 100644
--- a/libavfilter/vf_rotate.c
+++ b/libavfilter/vf_rotate.c
@@ -145,14 +145,14 @@ static int query_formats(AVFilterContext *ctx)
         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
-        AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
-        AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
-        AV_PIX_FMT_YUV420P12LE,
-        AV_PIX_FMT_YUV444P12LE,
-        AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
-        AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
-        AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
-        AV_PIX_FMT_YUV420P9LE, AV_PIX_FMT_YUVA420P9LE,
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA420P10,
+        AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10,
+        AV_PIX_FMT_YUV420P12,
+        AV_PIX_FMT_YUV444P12,
+        AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUVA420P16,
+        AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUVA444P9,
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUVA420P9,
         AV_PIX_FMT_NONE
     };
 
@@ -272,14 +272,14 @@ static uint8_t *interpolate_bilinear16(uint8_t *dst_color,
     int int_y1 = FFMIN(int_y+1, max_y);
 
     for (i = 0; i < src_linestep; i+=2) {
-        int s00 = AV_RL16(&src[src_linestep * int_x  + i + src_linesize * int_y ]);
-        int s01 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y ]);
-        int s10 = AV_RL16(&src[src_linestep * int_x  + i + src_linesize * int_y1]);
-        int s11 = AV_RL16(&src[src_linestep * int_x1 + i + src_linesize * int_y1]);
+        int s00 = AV_RN16A(&src[src_linestep * int_x  + i + src_linesize * int_y ]);
+        int s01 = AV_RN16A(&src[src_linestep * int_x1 + i + src_linesize * int_y ]);
+        int s10 = AV_RN16A(&src[src_linestep * int_x  + i + src_linesize * int_y1]);
+        int s11 = AV_RN16A(&src[src_linestep * int_x1 + i + src_linesize * int_y1]);
         int s0 = (((1<<16) - frac_x)*s00 + frac_x*s01);
         int s1 = (((1<<16) - frac_x)*s10 + frac_x*s11);
 
-        AV_WL16(&dst_color[i], ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32);
+        AV_WN16A(&dst_color[i], ((int64_t)((1<<16) - frac_y)*s0 + (int64_t)frac_y*s1) >> 32);
     }
 
     return dst_color;
diff --git a/libavfilter/vf_tinterlace.c b/libavfilter/vf_tinterlace.c
index 22746ebfb1..9a579211d3 100644
--- a/libavfilter/vf_tinterlace.c
+++ b/libavfilter/vf_tinterlace.c
@@ -92,12 +92,12 @@ static int query_formats(AVFilterContext *ctx)
         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
-        AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE,
-        AV_PIX_FMT_YUV440P10LE, AV_PIX_FMT_YUV444P10LE,
-        AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE,
-        AV_PIX_FMT_YUV440P12LE, AV_PIX_FMT_YUV444P12LE,
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10,
+        AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV444P10,
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12,
+        AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV444P12,
         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
-        AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE,
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
         AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
         AV_PIX_FMT_NONE
     };
@@ -134,9 +134,8 @@ static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src
         // this calculation is an integer representation of
         // '0.5 * current + 0.25 * above + 0.25 * below'
         // '1 +' is for rounding.
-        src_x   = av_le2ne16(srcp[i]) << 1;
-        dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
-                             + av_le2ne16(srcp_below[i])) >> 2);
+        src_x   = srcp[i] << 1;
+        dstp[i] = (1 + src_x + srcp_above[i] + srcp_below[i]) >> 2;
     }
 }
 
@@ -181,24 +180,23 @@ static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t width, const uint
         // this calculation is an integer representation of
         // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
         // '4 +' is for rounding.
-        src_le = av_le2ne16(srcp[i]);
+        src_le = srcp[i];
         src_x  = src_le << 1;
-        src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
+        src_ab = srcp_above[i] + srcp_below[i];
         dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
-                         - av_le2ne16(srcp_above2[i])
-                         - av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
+                         - srcp_above2[i] - srcp_below2[i]) >> 3, 0, clip_max);
         // Prevent over-sharpening:
         // dst must not exceed src when the average of above and below
         // is less than src. And the other way around.
         if (src_ab > src_x) {
             if (dst_le < src_le)
-                dstp[i] = av_le2ne16(src_le);
+                dstp[i] = src_le;
             else
-                dstp[i] = av_le2ne16(dst_le);
+                dstp[i] = dst_le;
         } else if (dst_le > src_le) {
-            dstp[i] = av_le2ne16(src_le);
+            dstp[i] = src_le;
         } else
-            dstp[i] = av_le2ne16(dst_le);
+            dstp[i] = dst_le;
     }
 }
 
-- 
2.17.2



More information about the ffmpeg-devel mailing list