Add AV_PKT_DATA_DISPLAYMATRIX and AV_FRAME_DATA_DISPLAYMATRIX as stream and
frame side data (respectively) to describe a display transformation matrix
for linear transformation operations on the decoded video.

Add APIs to easily access values such as rotation, scaling and translation and
to convert rotation angles to matrix.
---
 Changelog            |   1 +
 doc/APIchanges       |   7 +++
 libavcodec/avcodec.h |  19 +++++++
 libavcodec/utils.c   |   9 ++++
 libavcodec/version.h |   2 +-
 libavutil/Makefile   |   2 +
 libavutil/display.c  | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/display.h  |  64 ++++++++++++++++++++++++
 libavutil/frame.h    |   6 +++
 libavutil/version.h  |   2 +-
 10 files changed, 248 insertions(+), 2 deletions(-)
 create mode 100644 libavutil/display.c
 create mode 100644 libavutil/display.h

diff --git a/Changelog b/Changelog
index 55216e8..41f47c2 100644
--- a/Changelog
+++ b/Changelog
@@ -19,6 +19,7 @@ version <next>:
 - LucasArts SMUSH demuxer
 - MP2 encoding via TwoLAME
 - asettb filter
+- transformation matrix export and rotation/flip/translation api
 
 
 version 10:
diff --git a/doc/APIchanges b/doc/APIchanges
index ec1816a..f50a0ea 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,13 @@ libavutil:     2013-12-xx
 
 API changes, most recent first:
 
+2014-02-xx - xxxxxxx - lavu 53.12.0 - frame.h, display.h
+  Add AV_FRAME_DATA_DISPLAYMATRIX for exporting further
+  spatial rendering the video should do for proper display.
+
+2014-02-11 - xxxxxxx - lavc 55.47.0 - avcodec.h
+  Add AV_PKT_DATA_DISPLAYMATRIX for exporting the transformation matrix.
+
 2014-04-xx - xxxxxxx - lavu 53.11.0 - pixfmt.h
   Add AV_PIX_FMT_YVYU422 pixel format.
 
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 622eac3..eda06d8 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -937,6 +937,25 @@ enum AVPacketSideDataType {
      * ReplayGain information in form of the AVReplayGain struct.
      */
     AV_PKT_DATA_REPLAYGAIN,
+
+    /**
+     * This side data contains a 3x3 matrix describing two dimensional
+     * transformations to be applied on the decoded video.
+     *
+     * Each element is stored as a s32le.
+     * All the values in a matrix are stored as 16.16 fixed-point values,
+     * except for u, v and w, which are stored as 2.30 fixed-point values.
+     *
+     * The matrix has to be used as in the following example:
+     * the point (p, q) is transformed into (p', q') by multiplication.
+     *               | a b u |
+     *   (p, q, 1) * | c d v | = z * (p', q', 1)
+     *               | x y w |
+     *   p' = (a * p + c * q + x) / z;
+     *   q' = (b * p + d * q + y) / z;
+     *   z  = u * p + v * q + w
+     */
+    AV_PKT_DATA_DISPLAYMATRIX,
 };
 
 typedef struct AVPacketSideData {
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index d9832e2..9229413 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -597,6 +597,15 @@ int ff_decode_frame_props(AVCodecContext *avctx, AVFrame 
*frame)
 
         memcpy(frame_sd->data, packet_sd, size);
     }
+    /* copy the displaymatrix to the output frame */
+    packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
+    if (packet_sd) {
+        frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, 
size);
+        if (!frame_sd)
+            return AVERROR(ENOMEM);
+
+        memcpy(frame_sd->data, packet_sd, size);
+    }
 
     return 0;
 }
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 4a8febc..234d409 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR 55
-#define LIBAVCODEC_VERSION_MINOR 46
+#define LIBAVCODEC_VERSION_MINOR 47
 #define LIBAVCODEC_VERSION_MICRO  0
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/libavutil/Makefile b/libavutil/Makefile
index d5c1636..0f8ed08 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -16,6 +16,7 @@ HEADERS = adler32.h                                           
          \
           common.h                                                      \
           cpu.h                                                         \
           crc.h                                                         \
+          display.h                                                     \
           downmix_info.h                                                \
           error.h                                                       \
           eval.h                                                        \
@@ -69,6 +70,7 @@ OBJS = adler32.o                                              
          \
        cpu.o                                                            \
        crc.o                                                            \
        des.o                                                            \
+       display.o                                                        \
        downmix_info.o                                                   \
        error.o                                                          \
        eval.o                                                           \
diff --git a/libavutil/display.c b/libavutil/display.c
new file mode 100644
index 0000000..3713cc3
--- /dev/null
+++ b/libavutil/display.c
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2014 Vittorio Giovara <[email protected]>
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdint.h>
+#include <string.h>
+#include <math.h>
+
+#include "display.h"
+#include "intreadwrite.h"
+#include "mem.h"
+
+// fixed point to double
+#define CONV_FP(x) ((double) (x)) / (1 << 16)
+
+// double to fixed point
+#define CONV_DB(x) (int32_t) ((x) * (1 << 16))
+
+uint8_t *av_display_matrix_to_data(const int32_t matrix[3][3])
+{
+    int i, j;
+    uint8_t *buf = av_mallocz(sizeof(int32_t) * 3 * 3);
+    uint8_t *src = buf;
+
+    if (!buf)
+        return NULL;
+
+    for (i = 0; i < 3; i++) {
+        for (j = 0; j < 3; j++) {
+            AV_WL32(src, matrix[j][i]);
+            src += 4;
+        }
+    }
+
+    return buf;
+}
+
+int av_display_rotation_angle(const uint8_t *matrix)
+{
+    double rotationf, scale[2];
+    int32_t display_matrix[3][3];
+    const uint8_t *buf;
+    int i, j;
+
+    buf = matrix;
+    for (i = 0; i < 3; i++) {
+        for (j = 0; j < 3; j++) {
+            display_matrix[j][i] = AV_RL32(buf);
+            buf += 4;
+        }
+    }
+
+    scale[0] = sqrt(CONV_FP(display_matrix[0][0]) * 
CONV_FP(display_matrix[0][0]) +
+                    CONV_FP(display_matrix[1][0]) * 
CONV_FP(display_matrix[1][0]));
+    scale[1] = sqrt(CONV_FP(display_matrix[0][1]) * 
CONV_FP(display_matrix[0][1]) +
+                    CONV_FP(display_matrix[1][1]) * 
CONV_FP(display_matrix[1][1]));
+
+    rotationf = atan2(CONV_FP(display_matrix[0][1]) / scale[1],
+                      CONV_FP(display_matrix[0][0]) / scale[0]) * 180 / M_PI;
+
+    return (int) floor(rotationf);
+}
+
+int av_display_hflip(const uint8_t *matrix)
+{
+    int32_t display_matrix[3][3];
+    const uint8_t *buf;
+    int i, j;
+
+    buf = matrix;
+    for (i = 0; i < 3; i++) {
+        for (j = 0; j < 3; j++) {
+            display_matrix[j][i] = AV_RL32(buf);
+            buf += 4;
+        }
+    }
+
+    if ( display_matrix[0][0] < 0 &&
+        !display_matrix[1][0] &&
+        !display_matrix[2][0])
+        return 1;
+
+    return 0;
+}
+
+int av_display_vflip(const uint8_t *matrix)
+{
+    int32_t display_matrix[3][3];
+    const uint8_t *buf;
+    int i, j;
+
+    buf = matrix;
+    for (i = 0; i < 3; i++) {
+        for (j = 0; j < 3; j++) {
+            display_matrix[j][i] = AV_RL32(buf);
+            buf += 4;
+        }
+    }
+
+    if (!display_matrix[0][1] &&
+         display_matrix[1][1] < 0 &&
+        !display_matrix[2][1])
+        return 1;
+
+    return 0;
+}
+
+uint8_t *av_display_angle_to_matrix(double angle)
+{
+    int32_t display_matrix[3][3];
+    double radians = angle * M_PI / 180.0f;
+
+    memset(display_matrix, 0, sizeof(int32_t) * 3 * 3);
+
+    display_matrix[0][0] = CONV_DB(cos(radians));
+    display_matrix[0][1] = CONV_DB(-sin(radians));
+    display_matrix[1][0] = CONV_DB(sin(radians));
+    display_matrix[1][1] = CONV_DB(cos(radians));
+    display_matrix[2][2] = 1 << 30;
+
+    return av_display_matrix_to_data(display_matrix);
+}
diff --git a/libavutil/display.h b/libavutil/display.h
new file mode 100644
index 0000000..02fd7e0
--- /dev/null
+++ b/libavutil/display.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2014 Vittorio Giovara <[email protected]>
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_DISPLAY_H
+#define AVUTIL_DISPLAY_H
+
+#include <stdint.h>
+
+#include "libavutil/rational.h"
+
+/**
+ * @return the rotation angle in degrees.
+ */
+int av_display_rotation_angle(const uint8_t *matrix);
+
+/**
+ * @return 0 no hflip, 1 hflip
+ */
+int av_display_hflip(const uint8_t *matrix);
+
+/**
+ * @return 0 no vflip, 1 vflip
+ */
+int av_display_vflip(const uint8_t *matrix);
+
+/**
+ * Convert a 3x3 matrix into an array to be stored in a side data.
+ *
+ * @return a newly allocated array.
+ *
+ * @note   it is the caller's responsibility to take care of returned memory.
+ */
+uint8_t *av_display_matrix_to_data(const int32_t matrix[3][3]);
+
+/**
+ * Convert an angle expressed in degrees to a 3x3 matrix in fixed point
+ * numbers (16.16 for columns 1 and 2 and 2.30 for column 3).
+ * The array can be immediately stored in a side data; in order to
+ * access the elements it is necessary to read them as little endian.
+ *
+ * @return a newly allocated array.
+ *
+ * @note   it is the caller's responsibility to take care of returned memory.
+ */
+uint8_t *av_display_angle_to_matrix(const double angle);
+
+#endif /* AVUTIL_DISPLAY_H */
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 3bec8e5..764b661 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -73,6 +73,12 @@ enum AVFrameSideDataType {
      * ReplayGain information in the form of the AVReplayGain struct.
      */
     AV_FRAME_DATA_REPLAYGAIN,
+    /**
+     * This side data contains a 3x2 matrix (math notation) describing
+     * two dimensional transformations to be applied on the decoded video.
+     * Elements are represented as s32le, fixed point 16.16.
+     */
+    AV_FRAME_DATA_DISPLAYMATRIX,
 };
 
 typedef struct AVFrameSideData {
diff --git a/libavutil/version.h b/libavutil/version.h
index 8fd90bc..f32e8d0 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -54,7 +54,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 53
-#define LIBAVUTIL_VERSION_MINOR 11
+#define LIBAVUTIL_VERSION_MINOR 12
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
1.8.3.4 (Apple Git-47)

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to