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
40extern "C" {
41#endif
42
43static inline enum pipe_video_format
44u_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      case PIPE_VIDEO_PROFILE_VP9_PROFILE0:
83      case PIPE_VIDEO_PROFILE_VP9_PROFILE2:
84         return PIPE_VIDEO_FORMAT_VP9;
85
86      case PIPE_VIDEO_PROFILE_AV1_MAIN:
87         return PIPE_VIDEO_FORMAT_AV1;
88
89      default:
90         return PIPE_VIDEO_FORMAT_UNKNOWN;
91   }
92}
93
94static inline void
95u_copy_nv12_to_yv12(void *const *destination_data,
96                    uint32_t const *destination_pitches,
97                    int src_plane, int src_field,
98                    int src_stride, int num_fields,
99                    uint8_t const *src,
100                    int width, int height)
101{
102   int x, y;
103   unsigned u_stride = destination_pitches[2] * num_fields;
104   unsigned v_stride = destination_pitches[1] * num_fields;
105   uint8_t *u_dst = (uint8_t *)destination_data[2] + destination_pitches[2] * src_field;
106   uint8_t *v_dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
107
108   /* TODO: SIMD */
109   for (y = 0; y < height; y++) {
110      for (x = 0; x < width; x++) {
111         u_dst[x] = src[2*x];
112         v_dst[x] = src[2*x+1];
113      }
114      u_dst += u_stride;
115      v_dst += v_stride;
116      src += src_stride;
117   }
118}
119
120/**
121 * \brief  Copy YV12 chroma data while converting it NV12
122 *
123 * Given a set of YV12 source pointers and -pitches, copy the data to a
124 * layout typical for NV12 video buffers.
125 *
126 * \param source data[in]  The plane data pointers. Array of 3.
127 * \param source_pitches[in]  The plane pitches. Array of 3.
128 * \param dst_plane[in]  The destination plane to copy to. For NV12 always 1.
129 * \param dst_field[in]  The destination field if interlaced.
130 * \param dst_stride[in]  The destination stride for this plane.
131 * \param num_fields[in]  The number of fields in the video buffer.
132 * \param dst[in]  The destination plane pointer.
133 * \param width[in]  The source plane width.
134 * \param height[in]  The source plane height.
135 */
136static inline void
137u_copy_nv12_from_yv12(const void *const *source_data,
138                      uint32_t const *source_pitches,
139                      int dst_plane, int dst_field,
140                      int dst_stride, int num_fields,
141                      uint8_t *dst,
142                      int width, int height)
143{
144   int x, y;
145   unsigned u_stride = source_pitches[2] * num_fields;
146   unsigned v_stride = source_pitches[1] * num_fields;
147   uint8_t *u_src = (uint8_t *)source_data[2] + source_pitches[2] * dst_field;
148   uint8_t *v_src = (uint8_t *)source_data[1] + source_pitches[1] * dst_field;
149
150   /* TODO: SIMD */
151   for (y = 0; y < height; y++) {
152      for (x = 0; x < width; x++) {
153         dst[2*x] = u_src[x];
154         dst[2*x+1] = v_src[x];
155      }
156      u_src += u_stride;
157      v_src += v_stride;
158      dst += dst_stride;
159   }
160}
161
162static inline void
163u_copy_yv12_to_nv12(void *const *destination_data,
164                    uint32_t const *destination_pitches,
165                    int src_plane, int src_field,
166                    int src_stride, int num_fields,
167                    uint8_t const *src,
168                    int width, int height)
169{
170   int x, y;
171   unsigned offset = 2 - src_plane;
172   unsigned stride = destination_pitches[1] * num_fields;
173   uint8_t *dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
174
175   /* TODO: SIMD */
176   for (y = 0; y < height; y++) {
177      for (x = 0; x < 2 * width; x += 2) {
178         dst[x+offset] = src[x>>1];
179      }
180      dst += stride;
181      src += src_stride;
182   }
183}
184
185static inline void
186u_copy_swap422_packed(void *const *destination_data,
187                       uint32_t const *destination_pitches,
188                       int src_plane, int src_field,
189                       int src_stride, int num_fields,
190                       uint8_t const *src,
191                       int width, int height)
192{
193   int x, y;
194   unsigned stride = destination_pitches[0] * num_fields;
195   uint8_t *dst = (uint8_t *)destination_data[0] + destination_pitches[0] * src_field;
196
197   /* TODO: SIMD */
198   for (y = 0; y < height; y++) {
199      for (x = 0; x < 4 * width; x += 4) {
200         dst[x+0] = src[x+1];
201         dst[x+1] = src[x+0];
202         dst[x+2] = src[x+3];
203         dst[x+3] = src[x+2];
204      }
205      dst += stride;
206      src += src_stride;
207   }
208}
209
210static inline uint32_t
211u_get_h264_level(uint32_t width, uint32_t height, uint32_t *max_reference)
212{
213   uint32_t max_dpb_mbs;
214
215   width = align(width, 16);
216   height = align(height, 16);
217
218   /* Max references will be used for caculation of number of DPB buffers
219      in the UVD driver, limitation of max references is 16. Some client
220      like mpv application for VA-API, it requires references more than that,
221      so we have to set max of references to 16 here. */
222   *max_reference = MIN2(*max_reference, 16);
223   max_dpb_mbs = (width / 16) * (height / 16) * *max_reference;
224
225   /* The calculation is based on "Decoded picture buffering" section
226      from http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC */
227   if (max_dpb_mbs <= 8100)
228      return 30;
229   else if (max_dpb_mbs <= 18000)
230      return 31;
231   else if (max_dpb_mbs <= 20480)
232      return 32;
233   else if (max_dpb_mbs <= 32768)
234      return 41;
235   else if (max_dpb_mbs <= 34816)
236      return 42;
237   else if (max_dpb_mbs <= 110400)
238      return 50;
239   else if (max_dpb_mbs <= 184320)
240      return 51;
241   else
242      return 52;
243}
244
245static inline uint32_t
246u_get_h264_profile_idc(enum pipe_video_profile profile)
247{
248   switch (profile) {
249      case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
250      case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
251         return 66;
252      case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
253         return 77;
254      case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
255         return 88;
256      case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
257         return 100;
258      case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
259         return 110;
260      case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422:
261         return 122;
262      case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444:
263         return 244;
264      default:
265         return 66; //use baseline profile instead
266   }
267}
268
269#ifdef __cplusplus
270}
271#endif
272
273#endif /* U_VIDEO_H */
274