Home | History | Annotate | Download | only in util
      1 /**************************************************************************
      2  *
      3  * Copyright 2009 Younes Manton.
      4  * All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the
      8  * "Software"), to deal in the Software without restriction, including
      9  * without limitation the rights to use, copy, modify, merge, publish,
     10  * distribute, sub license, and/or sell copies of the Software, and to
     11  * permit persons to whom the Software is furnished to do so, subject to
     12  * the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
     22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  **************************************************************************/
     27 
     28 #ifndef U_VIDEO_H
     29 #define U_VIDEO_H
     30 
     31 #include "pipe/p_defines.h"
     32 #include "pipe/p_video_enums.h"
     33 
     34 /* u_reduce_video_profile() needs these */
     35 #include "pipe/p_compiler.h"
     36 #include "util/u_debug.h"
     37 #include "util/u_math.h"
     38 
     39 #ifdef __cplusplus
     40 extern "C" {
     41 #endif
     42 
     43 static inline enum pipe_video_format
     44 u_reduce_video_profile(enum pipe_video_profile profile)
     45 {
     46    switch (profile)
     47    {
     48       case PIPE_VIDEO_PROFILE_MPEG1:
     49       case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
     50       case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
     51          return PIPE_VIDEO_FORMAT_MPEG12;
     52 
     53       case PIPE_VIDEO_PROFILE_MPEG4_SIMPLE:
     54       case PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE:
     55          return PIPE_VIDEO_FORMAT_MPEG4;
     56 
     57       case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
     58       case PIPE_VIDEO_PROFILE_VC1_MAIN:
     59       case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
     60          return PIPE_VIDEO_FORMAT_VC1;
     61 
     62       case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
     63       case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
     64       case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
     65       case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
     66       case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
     67       case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
     68       case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422:
     69       case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444:
     70          return PIPE_VIDEO_FORMAT_MPEG4_AVC;
     71 
     72       case PIPE_VIDEO_PROFILE_HEVC_MAIN:
     73       case PIPE_VIDEO_PROFILE_HEVC_MAIN_10:
     74       case PIPE_VIDEO_PROFILE_HEVC_MAIN_STILL:
     75       case PIPE_VIDEO_PROFILE_HEVC_MAIN_12:
     76       case PIPE_VIDEO_PROFILE_HEVC_MAIN_444:
     77          return PIPE_VIDEO_FORMAT_HEVC;
     78 
     79       case PIPE_VIDEO_PROFILE_JPEG_BASELINE:
     80          return PIPE_VIDEO_FORMAT_JPEG;
     81 
     82       default:
     83          return PIPE_VIDEO_FORMAT_UNKNOWN;
     84    }
     85 }
     86 
     87 static inline void
     88 u_copy_nv12_to_yv12(void *const *destination_data,
     89                     uint32_t const *destination_pitches,
     90                     int src_plane, int src_field,
     91                     int src_stride, int num_fields,
     92                     uint8_t const *src,
     93                     int width, int height)
     94 {
     95    int x, y;
     96    unsigned u_stride = destination_pitches[2] * num_fields;
     97    unsigned v_stride = destination_pitches[1] * num_fields;
     98    uint8_t *u_dst = (uint8_t *)destination_data[2] + destination_pitches[2] * src_field;
     99    uint8_t *v_dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
    100 
    101    /* TODO: SIMD */
    102    for (y = 0; y < height; y++) {
    103       for (x = 0; x < width; x++) {
    104          u_dst[x] = src[2*x];
    105          v_dst[x] = src[2*x+1];
    106       }
    107       u_dst += u_stride;
    108       v_dst += v_stride;
    109       src += src_stride;
    110    }
    111 }
    112 
    113 /**
    114  * \brief  Copy YV12 chroma data while converting it NV12
    115  *
    116  * Given a set of YV12 source pointers and -pitches, copy the data to a
    117  * layout typical for NV12 video buffers.
    118  *
    119  * \param source data[in]  The plane data pointers. Array of 3.
    120  * \param source_pitches[in]  The plane pitches. Array of 3.
    121  * \param dst_plane[in]  The destination plane to copy to. For NV12 always 1.
    122  * \param dst_field[in]  The destination field if interlaced.
    123  * \param dst_stride[in]  The destination stride for this plane.
    124  * \param num_fields[in]  The number of fields in the video buffer.
    125  * \param dst[in]  The destination plane pointer.
    126  * \param width[in]  The source plane width.
    127  * \param height[in]  The source plane height.
    128  */
    129 static inline void
    130 u_copy_nv12_from_yv12(const void *const *source_data,
    131                       uint32_t const *source_pitches,
    132                       int dst_plane, int dst_field,
    133                       int dst_stride, int num_fields,
    134                       uint8_t *dst,
    135                       int width, int height)
    136 {
    137    int x, y;
    138    unsigned u_stride = source_pitches[2] * num_fields;
    139    unsigned v_stride = source_pitches[1] * num_fields;
    140    uint8_t *u_src = (uint8_t *)source_data[2] + source_pitches[2] * dst_field;
    141    uint8_t *v_src = (uint8_t *)source_data[1] + source_pitches[1] * dst_field;
    142 
    143    /* TODO: SIMD */
    144    for (y = 0; y < height; y++) {
    145       for (x = 0; x < width; x++) {
    146          dst[2*x] = u_src[x];
    147          dst[2*x+1] = v_src[x];
    148       }
    149       u_src += u_stride;
    150       v_src += v_stride;
    151       dst += dst_stride;
    152    }
    153 }
    154 
    155 static inline void
    156 u_copy_yv12_to_nv12(void *const *destination_data,
    157                     uint32_t const *destination_pitches,
    158                     int src_plane, int src_field,
    159                     int src_stride, int num_fields,
    160                     uint8_t const *src,
    161                     int width, int height)
    162 {
    163    int x, y;
    164    unsigned offset = 2 - src_plane;
    165    unsigned stride = destination_pitches[1] * num_fields;
    166    uint8_t *dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
    167 
    168    /* TODO: SIMD */
    169    for (y = 0; y < height; y++) {
    170       for (x = 0; x < 2 * width; x += 2) {
    171          dst[x+offset] = src[x>>1];
    172       }
    173       dst += stride;
    174       src += src_stride;
    175    }
    176 }
    177 
    178 static inline void
    179 u_copy_swap422_packed(void *const *destination_data,
    180                        uint32_t const *destination_pitches,
    181                        int src_plane, int src_field,
    182                        int src_stride, int num_fields,
    183                        uint8_t const *src,
    184                        int width, int height)
    185 {
    186    int x, y;
    187    unsigned stride = destination_pitches[0] * num_fields;
    188    uint8_t *dst = (uint8_t *)destination_data[0] + destination_pitches[0] * src_field;
    189 
    190    /* TODO: SIMD */
    191    for (y = 0; y < height; y++) {
    192       for (x = 0; x < 4 * width; x += 4) {
    193          dst[x+0] = src[x+1];
    194          dst[x+1] = src[x+0];
    195          dst[x+2] = src[x+3];
    196          dst[x+3] = src[x+2];
    197       }
    198       dst += stride;
    199       src += src_stride;
    200    }
    201 }
    202 
    203 static inline uint32_t
    204 u_get_h264_level(uint32_t width, uint32_t height, uint32_t *max_reference)
    205 {
    206    uint32_t max_dpb_mbs;
    207 
    208    width = align(width, 16);
    209    height = align(height, 16);
    210 
    211    /* Max references will be used for caculation of number of DPB buffers
    212       in the UVD driver, limitation of max references is 16. Some client
    213       like mpv application for VA-API, it requires references more than that,
    214       so we have to set max of references to 16 here. */
    215    *max_reference = MIN2(*max_reference, 16);
    216    max_dpb_mbs = (width / 16) * (height / 16) * *max_reference;
    217 
    218    /* The calculation is based on "Decoded picture buffering" section
    219       from http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC */
    220    if (max_dpb_mbs <= 8100)
    221       return 30;
    222    else if (max_dpb_mbs <= 18000)
    223       return 31;
    224    else if (max_dpb_mbs <= 20480)
    225       return 32;
    226    else if (max_dpb_mbs <= 32768)
    227       return 41;
    228    else if (max_dpb_mbs <= 34816)
    229       return 42;
    230    else if (max_dpb_mbs <= 110400)
    231       return 50;
    232    else if (max_dpb_mbs <= 184320)
    233       return 51;
    234    else
    235       return 52;
    236 }
    237 
    238 #ifdef __cplusplus
    239 }
    240 #endif
    241 
    242 #endif /* U_VIDEO_H */
    243