]> git.sesse.net Git - vlc/blobdiff - src/misc/es_format.c
es_format: reorder two functions
[vlc] / src / misc / es_format.c
index 9864a0fb1a96055f9d855718b9ce886f9bf1354e..46094c51b48ace2511d15a2c3991a89c83b65b34 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * es_format.c : es_format_t helpers.
  *****************************************************************************
- * Copyright (C) 2008 the VideoLAN team
+ * Copyright (C) 2008 VLC authors and VideoLAN
  * $Id$
  *
  * Author: Laurent Aimar <fenrir@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program 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.
  *
  * This program 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 General Public License for more details.
+ * 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 General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -31,7 +31,6 @@
 
 #include <vlc_common.h>
 #include <vlc_es.h>
-#include <vlc_aout.h>
 
 
 /*****************************************************************************
@@ -131,13 +130,14 @@ void video_format_FixRgb( video_format_t *p_fmt )
 
 void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma,
                          int i_width, int i_height,
+                         int i_visible_width, int i_visible_height,
                          int i_sar_num, int i_sar_den )
 {
     p_fmt->i_chroma         = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
-    p_fmt->i_width          =
-    p_fmt->i_visible_width  = i_width;
-    p_fmt->i_height         =
-    p_fmt->i_visible_height = i_height;
+    p_fmt->i_width          = i_width;
+    p_fmt->i_visible_width  = i_visible_width;
+    p_fmt->i_height         = i_height;
+    p_fmt->i_visible_height = i_visible_height;
     p_fmt->i_x_offset       =
     p_fmt->i_y_offset       = 0;
     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
@@ -148,6 +148,12 @@ void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma,
     case VLC_CODEC_YUVA:
         p_fmt->i_bits_per_pixel = 32;
         break;
+    case VLC_CODEC_YUV420A:
+        p_fmt->i_bits_per_pixel = 20;
+        break;
+    case VLC_CODEC_YUV422A:
+        p_fmt->i_bits_per_pixel = 24;
+        break;
     case VLC_CODEC_I444:
     case VLC_CODEC_J444:
         p_fmt->i_bits_per_pixel = 24;
@@ -183,6 +189,7 @@ void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma,
 
     case VLC_CODEC_RGB32:
     case VLC_CODEC_RGBA:
+    case VLC_CODEC_ARGB:
         p_fmt->i_bits_per_pixel = 32;
         break;
     case VLC_CODEC_RGB24:
@@ -201,11 +208,204 @@ void video_format_Setup( video_format_t *p_fmt, vlc_fourcc_t i_chroma,
         p_fmt->i_bits_per_pixel = 8;
         break;
 
+    case VLC_CODEC_XYZ12:
+        p_fmt->i_bits_per_pixel = 48;
+        break;
+
     default:
         p_fmt->i_bits_per_pixel = 0;
         break;
     }
 }
+
+void video_format_CopyCrop( video_format_t *p_dst, const video_format_t *p_src )
+{
+    p_dst->i_x_offset       = p_src->i_x_offset;
+    p_dst->i_y_offset       = p_src->i_y_offset;
+    p_dst->i_visible_width  = p_src->i_visible_width;
+    p_dst->i_visible_height = p_src->i_visible_height;
+}
+
+void video_format_ScaleCropAr( video_format_t *p_dst, const video_format_t *p_src )
+{
+    p_dst->i_x_offset       = (uint64_t)p_src->i_x_offset       * p_dst->i_width  / p_src->i_width;
+    p_dst->i_y_offset       = (uint64_t)p_src->i_y_offset       * p_dst->i_height / p_src->i_height;
+    p_dst->i_visible_width  = (uint64_t)p_src->i_visible_width  * p_dst->i_width  / p_src->i_width;
+    p_dst->i_visible_height = (uint64_t)p_src->i_visible_height * p_dst->i_height / p_src->i_height;
+
+    p_dst->i_sar_num *= p_src->i_width;
+    p_dst->i_sar_den *= p_dst->i_width;
+    vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
+                p_dst->i_sar_num, p_dst->i_sar_den, 65536);
+
+    p_dst->i_sar_num *= p_dst->i_height;
+    p_dst->i_sar_den *= p_src->i_height;
+    vlc_ureduce(&p_dst->i_sar_num, &p_dst->i_sar_den,
+                p_dst->i_sar_num, p_dst->i_sar_den, 65536);
+}
+
+//Simplify transforms to have something more managable. Order: angle, hflip.
+static void transform_GetBasicOps( video_transform_t transform,
+                                   unsigned *restrict angle,
+                                   bool *restrict hflip )
+{
+    *hflip = ORIENT_IS_MIRROR(transform);
+
+    switch ( transform )
+    {
+        case TRANSFORM_R90:
+        case TRANSFORM_TRANSPOSE:
+            *angle = 90;
+            break;
+        case TRANSFORM_R180:
+        case TRANSFORM_VFLIP:
+            *angle = 180;
+            break;
+        case TRANSFORM_R270:
+        case TRANSFORM_ANTI_TRANSPOSE:
+            *angle = 270;
+            break;
+        case TRANSFORM_HFLIP:
+        case TRANSFORM_IDENTITY:
+            *angle = 0;
+            break;
+    }
+}
+
+static video_transform_t transform_FromBasicOps( unsigned angle, bool hflip )
+{
+    switch ( angle )
+    {
+        case 90:
+            return hflip ? TRANSFORM_TRANSPOSE : TRANSFORM_R90;
+        case 180:
+            return hflip ? TRANSFORM_VFLIP : TRANSFORM_R180;
+        case 270:
+            return hflip ? TRANSFORM_ANTI_TRANSPOSE : TRANSFORM_R270;
+        default:
+            return hflip ? TRANSFORM_HFLIP : TRANSFORM_IDENTITY;
+    }
+}
+
+video_transform_t video_format_GetTransform( video_orientation_t src,
+                                             video_orientation_t dst )
+{
+    unsigned angle1, angle2;
+    bool hflip1, hflip2;
+
+    transform_GetBasicOps(  (video_transform_t)src, &angle1, &hflip1 );
+    transform_GetBasicOps( transform_Inverse( (video_transform_t)dst ),
+                           &angle2, &hflip2 );
+
+    int angle = (angle1 + angle2) % 360;
+    bool hflip = hflip1 ^ hflip2;
+
+    return transform_FromBasicOps(angle, hflip);
+}
+
+void video_format_TransformBy( video_format_t *fmt, video_transform_t transform )
+{
+    /* Get destination orientation */
+    unsigned angle1, angle2;
+    bool hflip1, hflip2;
+
+    transform_GetBasicOps( transform, &angle1, &hflip1 );
+    transform_GetBasicOps( (video_transform_t)fmt->orientation, &angle2, &hflip2 );
+
+    unsigned angle = (angle2 - angle1 + 360) % 360;
+    bool hflip = hflip2 ^ hflip1;
+
+    video_orientation_t dst_orient = ORIENT_NORMAL;
+
+    if( hflip ) {
+
+        if( angle == 0 )
+            dst_orient = ORIENT_HFLIPPED;
+        else if( angle == 90 )
+            dst_orient = ORIENT_ANTI_TRANSPOSED;
+        else if( angle == 180 )
+            dst_orient = ORIENT_VFLIPPED;
+        else if( angle == 270 )
+            dst_orient = ORIENT_TRANSPOSED;
+    }
+    else {
+
+        if( angle == 90 )
+            dst_orient = ORIENT_ROTATED_90;
+        else if( angle == 180 )
+            dst_orient = ORIENT_ROTATED_180;
+        else if( angle == 270 )
+            dst_orient = ORIENT_ROTATED_270;
+    }
+
+    /* Apply transform */
+
+    video_format_t scratch = *fmt;
+
+    if( ORIENT_IS_SWAP( fmt->orientation ) != ORIENT_IS_SWAP( dst_orient )) {
+
+        fmt->i_width = scratch.i_height;
+        fmt->i_visible_width = scratch.i_visible_height;
+        fmt->i_height = scratch.i_width;
+        fmt->i_visible_height = scratch.i_visible_width;
+        fmt->i_sar_num = scratch.i_sar_den;
+        fmt->i_sar_den = scratch.i_sar_num;
+    }
+
+    unsigned int delta_y = scratch.i_height - scratch.i_visible_height - scratch.i_y_offset;
+    unsigned int delta_x = scratch.i_width - scratch.i_visible_width - scratch.i_x_offset;
+
+    switch ( transform )
+    {
+        case TRANSFORM_IDENTITY:
+            break;
+        case TRANSFORM_R90:
+            fmt->i_x_offset = delta_y;
+            fmt->i_y_offset = scratch.i_x_offset;
+            break;
+        case TRANSFORM_R180:
+            fmt->i_x_offset = delta_x;
+            fmt->i_y_offset = delta_y;
+            break;
+        case TRANSFORM_R270:
+            fmt->i_x_offset = scratch.i_y_offset;
+            fmt->i_y_offset = delta_x;
+            break;
+        case TRANSFORM_HFLIP:
+            fmt->i_x_offset = delta_x;
+            break;
+        case TRANSFORM_VFLIP:
+            fmt->i_y_offset = delta_y;
+            break;
+        case TRANSFORM_TRANSPOSE:
+            fmt->i_x_offset = scratch.i_y_offset;
+            fmt->i_y_offset = scratch.i_x_offset;
+            break;
+        case TRANSFORM_ANTI_TRANSPOSE:
+            fmt->i_x_offset = delta_y;
+            fmt->i_y_offset = delta_x;
+            break;
+    }
+
+    fmt->orientation = dst_orient;
+}
+
+void video_format_TransformTo( video_format_t *restrict fmt,
+                               video_orientation_t dst_orientation )
+{
+    video_transform_t transform = video_format_GetTransform(fmt->orientation,
+                                                            dst_orientation);
+    video_format_TransformBy(fmt, transform);
+}
+
+void video_format_ApplyRotation( video_format_t *restrict out,
+                                 const video_format_t *restrict in )
+{
+    *out = *in;
+
+    video_format_TransformTo(out, ORIENT_NORMAL);
+}
+
 bool video_format_IsSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
 {
     video_format_t v1 = *p_fmt1;
@@ -222,6 +422,9 @@ bool video_format_IsSimilar( const video_format_t *p_fmt1, const video_format_t
     if( v1.i_sar_num * v2.i_sar_den != v2.i_sar_num * v1.i_sar_den )
         return false;
 
+    if( v1.orientation != v2.orientation)
+        return false;
+
     if( v1.i_chroma == VLC_CODEC_RGB15 ||
         v1.i_chroma == VLC_CODEC_RGB16 ||
         v1.i_chroma == VLC_CODEC_RGB24 ||
@@ -260,7 +463,7 @@ void es_format_Init( es_format_t *fmt,
     fmt->i_level                = -1;
     fmt->i_id                   = -1;
     fmt->i_group                = 0;
-    fmt->i_priority             = 0;
+    fmt->i_priority             = ES_PRIORITY_SELECTABLE_MIN;
     fmt->psz_language           = NULL;
     fmt->psz_description        = NULL;
 
@@ -399,7 +602,7 @@ bool es_format_IsSimilar( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
         if( !v1.i_chroma )
             v1.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt1->i_codec );
         if( !v2.i_chroma )
-            v2.i_chroma = vlc_fourcc_GetCodec( p_fmt1->i_cat, p_fmt2->i_codec );
+            v2.i_chroma = vlc_fourcc_GetCodec( p_fmt2->i_cat, p_fmt2->i_codec );
         return video_format_IsSimilar( &p_fmt1->video, &p_fmt2->video );
     }