a419af8ec2767f2957bfe50126e63a88098841c3
[platform/upstream/gstreamer.git] / subprojects / gstreamer-vaapi / gst-libs / gst / vaapi / gstvaapiutils_h265.c
1 /*
2  *  gstvaapiutils_h265.c - H.265 related utilities
3  *
4  *  Copyright (C) 2015 Intel Corporation
5  *    Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22
23 #include "sysdeps.h"
24 #include <gst/codecparsers/gsth265parser.h>
25 #include "gstvaapicompat.h"
26 #include "gstvaapiutils_h265_priv.h"
27
28 #define DEBUG 1
29 #include "gstvaapidebug.h"
30
31 struct map
32 {
33   guint value;
34   const gchar *name;
35 };
36
37 /* Profile string map */
38 static const struct map gst_vaapi_h265_profile_map[] = {
39 /* *INDENT-OFF* */
40   { GST_VAAPI_PROFILE_H265_MAIN,                 "main"                 },
41   { GST_VAAPI_PROFILE_H265_MAIN10,               "main-10"              },
42   { GST_VAAPI_PROFILE_H265_MAIN_STILL_PICTURE,   "main-still-picture"   },
43   { GST_VAAPI_PROFILE_H265_MAIN_444,             "main-444"             },
44   { GST_VAAPI_PROFILE_H265_MAIN_444_10,          "main-444-10"          },
45   { GST_VAAPI_PROFILE_H265_MAIN_422_10,          "main-422-10"          },
46   { GST_VAAPI_PROFILE_H265_MAIN12,               "main-12"              },
47   { GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN,        "screen-extended-main"       },
48   { GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_10,     "screen-extended-main-10"    },
49   { GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_444,    "screen-extended-main-444"   },
50   { GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_444_10, "screen-extended-main-444-10"},
51   { 0, NULL }
52 /* *INDENT-ON* */
53 };
54
55 /* Tier string map */
56 static const struct map gst_vaapi_h265_tier_map[] = {
57 /* *INDENT-OFF* */
58   { GST_VAAPI_TIER_H265_MAIN,    "main" },
59   { GST_VAAPI_TIER_H265_HIGH,    "high"},
60   { GST_VAAPI_TIER_H265_UNKNOWN, "unknown"}
61 /* *INDENT-ON* */
62 };
63
64 /* Level string map */
65 static const struct map gst_vaapi_h265_level_map[] = {
66 /* *INDENT-OFF* */
67   { GST_VAAPI_LEVEL_H265_L1,    "1"     },
68   { GST_VAAPI_LEVEL_H265_L2,    "2"     },
69   { GST_VAAPI_LEVEL_H265_L2_1,  "2.1"   },
70   { GST_VAAPI_LEVEL_H265_L3,    "3"     },
71   { GST_VAAPI_LEVEL_H265_L3_1,  "3.1"   },
72   { GST_VAAPI_LEVEL_H265_L4,    "4"     },
73   { GST_VAAPI_LEVEL_H265_L4_1,  "4.1"   },
74   { GST_VAAPI_LEVEL_H265_L5,    "5"     },
75   { GST_VAAPI_LEVEL_H265_L5_1,  "5.1"   },
76   { GST_VAAPI_LEVEL_H265_L5_2,  "5.2"   },
77   { GST_VAAPI_LEVEL_H265_L6,    "6"     },
78   { GST_VAAPI_LEVEL_H265_L6_1,  "6.1"   },
79   { GST_VAAPI_LEVEL_H265_L6_2,  "6.2"   },
80   { 0, NULL }
81 /* *INDENT-ON* */
82 };
83
84 /* Table A-1 - Level limits */
85 /* *INDENT-OFF* */
86 static const GstVaapiH265LevelLimits gst_vaapi_h265_level_limits[] = {
87   /* level                     idc   MaxLumaPs  MCPBMt  MCPBHt MSlSeg MTR MTC   MaxLumaSr   MBRMt   MBRHt MinCr*/
88   { GST_VAAPI_LEVEL_H265_L1,    30,     36864,    350,      0,    16,  1,  1,     552960,    128,      0,  2},
89   { GST_VAAPI_LEVEL_H265_L2,    60,    122880,   1500,      0,    16,  1,  1,    3686400,   1500,      0,  2},
90   { GST_VAAPI_LEVEL_H265_L2_1,  63,    245760,   3000,      0,    20,  1,  1,    7372800,   3000,      0,  2},
91   { GST_VAAPI_LEVEL_H265_L3,    90,    552960,   6000,      0,    30,  2,  2,   16588800,   6000,      0,  2},
92   { GST_VAAPI_LEVEL_H265_L3_1,  93,    983040,  10000,      0,    40,  3,  3,   33177600,  10000,      0,  2},
93   { GST_VAAPI_LEVEL_H265_L4,    120,  2228224,  12000,  30000,    75,  5,  5,   66846720,  12000,  30000,  4},
94   { GST_VAAPI_LEVEL_H265_L4_1,  123,  2228224,  20000,  50000,    75,  5,  5,  133693440,  20000,  50000,  4},
95   { GST_VAAPI_LEVEL_H265_L5,    150,  8912896,  25000, 100000,   200, 11, 10,  267386880,  25000, 100000,  6},
96   { GST_VAAPI_LEVEL_H265_L5_1,  153,  8912896,  40000, 160000,   200, 11, 10,  534773760,  40000, 160000,  8},
97   { GST_VAAPI_LEVEL_H265_L5_2,  156,  8912896,  60000, 240000,   200, 11, 10, 1069547520,  60000, 240000,  8},
98   { GST_VAAPI_LEVEL_H265_L6,    180, 35651584,  60000, 240000,   600, 22, 20, 1069547520,  60000, 240000,  8},
99   { GST_VAAPI_LEVEL_H265_L6_1,  183, 35651584, 120000, 480000,   600, 22, 20, 2139095040, 120000, 480000,  8},
100   { GST_VAAPI_LEVEL_H265_L6_2,  186, 35651584, 240000, 800000,   600, 22, 20, 4278190080, 240000, 800000,  6},
101   { 0, }
102 };
103 /* *INDENT-ON* */
104
105 /* Lookup value in map */
106 static const struct map *
107 map_lookup_value (const struct map *m, guint value)
108 {
109   g_return_val_if_fail (m != NULL, NULL);
110
111   for (; m->name != NULL; m++) {
112     if (m->value == value)
113       return m;
114   }
115   return NULL;
116 }
117
118 /* Lookup name in map */
119 static const struct map *
120 map_lookup_name (const struct map *m, const gchar * name)
121 {
122   g_return_val_if_fail (m != NULL, NULL);
123
124   if (!name)
125     return NULL;
126
127   for (; m->name != NULL; m++) {
128     if (strcmp (m->name, name) == 0)
129       return m;
130   }
131   return NULL;
132 }
133
134 /** Returns a relative score for the supplied GstVaapiProfile */
135 guint
136 gst_vaapi_utils_h265_get_profile_score (GstVaapiProfile profile)
137 {
138   const struct map *const m =
139       map_lookup_value (gst_vaapi_h265_profile_map, profile);
140
141   return m ? 1 + (m - gst_vaapi_h265_profile_map) : 0;
142 }
143
144 /** Returns GstVaapiProfile from H.265 profile_idc value */
145 GstVaapiProfile
146 gst_vaapi_utils_h265_get_profile (GstH265SPS * sps)
147 {
148   GstVaapiProfile vaapi_profile;
149   GstH265Profile profile;
150
151   g_return_val_if_fail (sps != NULL, GST_VAAPI_PROFILE_UNKNOWN);
152
153   profile = gst_h265_get_profile_from_sps (sps);
154   switch (profile) {
155     case GST_H265_PROFILE_MAIN:
156       /* Main Intra, recognize it as MAIN */
157     case GST_H265_PROFILE_MAIN_INTRA:
158       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN;
159       break;
160     case GST_H265_PROFILE_MAIN_10:
161       /* Main 10 Intra, recognize it as MAIN10 */
162     case GST_H265_PROFILE_MAIN_10_INTRA:
163       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN10;
164       break;
165     case GST_H265_PROFILE_MAIN_12:
166       /* Main 12 Intra, recognize it as MAIN_12 */
167     case GST_H265_PROFILE_MAIN_12_INTRA:
168       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN12;
169       break;
170     case GST_H265_PROFILE_MAIN_STILL_PICTURE:
171       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN_STILL_PICTURE;
172       break;
173     case GST_H265_PROFILE_MAIN_422_10:
174       /* Main 422_10 Intra, recognize it as MAIN_422_10 */
175     case GST_H265_PROFILE_MAIN_422_10_INTRA:
176       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN_422_10;
177       break;
178     case GST_H265_PROFILE_MAIN_422_12:
179       /* Main 422_12 Intra, recognize it as MAIN_422_12 */
180     case GST_H265_PROFILE_MAIN_422_12_INTRA:
181       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN_422_12;
182       break;
183     case GST_H265_PROFILE_MAIN_444:
184       /* Main 444 Intra, recognize it as MAIN_444 */
185     case GST_H265_PROFILE_MAIN_444_INTRA:
186       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN_444;
187       break;
188     case GST_H265_PROFILE_MAIN_444_10:
189       /* Main 444_10 Intra, recognize it as MAIN_444_10 */
190     case GST_H265_PROFILE_MAIN_444_10_INTRA:
191       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN_444_10;
192       break;
193     case GST_H265_PROFILE_MAIN_444_12:
194       /* Main 444_12 Intra, recognize it as MAIN_444_12 */
195     case GST_H265_PROFILE_MAIN_444_12_INTRA:
196       vaapi_profile = GST_VAAPI_PROFILE_H265_MAIN_444_12;
197       break;
198     case GST_H265_PROFILE_SCREEN_EXTENDED_MAIN:
199       vaapi_profile = GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN;
200       break;
201     case GST_H265_PROFILE_SCREEN_EXTENDED_MAIN_10:
202       vaapi_profile = GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_10;
203       break;
204     case GST_H265_PROFILE_SCREEN_EXTENDED_MAIN_444:
205       vaapi_profile = GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_444;
206       break;
207     case GST_H265_PROFILE_SCREEN_EXTENDED_MAIN_444_10:
208       vaapi_profile = GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_444_10;
209       break;
210     default:
211       GST_DEBUG ("unsupported profile_idc value");
212       vaapi_profile = GST_VAAPI_PROFILE_UNKNOWN;
213       break;
214   }
215   return vaapi_profile;
216 }
217
218 /** Returns H.265 profile_idc value from GstVaapiProfile */
219 guint8
220 gst_vaapi_utils_h265_get_profile_idc (GstVaapiProfile profile)
221 {
222   guint8 profile_idc;
223
224   switch (profile) {
225     case GST_VAAPI_PROFILE_H265_MAIN:
226       profile_idc = GST_H265_PROFILE_IDC_MAIN;
227       break;
228     case GST_VAAPI_PROFILE_H265_MAIN10:
229       profile_idc = GST_H265_PROFILE_IDC_MAIN_10;
230       break;
231     case GST_VAAPI_PROFILE_H265_MAIN_STILL_PICTURE:
232       profile_idc = GST_H265_PROFILE_IDC_MAIN_STILL_PICTURE;
233       break;
234     case GST_VAAPI_PROFILE_H265_MAIN_422_10:
235       /* Fall through */
236     case GST_VAAPI_PROFILE_H265_MAIN_444:
237       /* Fall through */
238     case GST_VAAPI_PROFILE_H265_MAIN_444_10:
239       /* Fall through */
240     case GST_VAAPI_PROFILE_H265_MAIN12:
241       profile_idc = GST_H265_PROFILE_IDC_FORMAT_RANGE_EXTENSION;
242       break;
243     case GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN:
244       /* Fall through */
245     case GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_10:
246       /* Fall through */
247     case GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_444:
248       /* Fall through */
249     case GST_VAAPI_PROFILE_H265_SCREEN_EXTENDED_MAIN_444_10:
250       profile_idc = GST_H265_PROFILE_IDC_SCREEN_CONTENT_CODING;
251       break;
252     default:
253       GST_DEBUG ("unsupported GstVaapiProfile value");
254       profile_idc = 0;
255       break;
256   }
257   return profile_idc;
258 }
259
260 /** Returns GstVaapiProfile from a string representation */
261 GstVaapiProfile
262 gst_vaapi_utils_h265_get_profile_from_string (const gchar * str)
263 {
264   const struct map *const m = map_lookup_name (gst_vaapi_h265_profile_map, str);
265
266   return m ? (GstVaapiProfile) m->value : GST_VAAPI_PROFILE_UNKNOWN;
267 }
268
269 /** Returns a string representation for the supplied H.265 profile */
270 const gchar *
271 gst_vaapi_utils_h265_get_profile_string (GstVaapiProfile profile)
272 {
273   const struct map *const m =
274       map_lookup_value (gst_vaapi_h265_profile_map, profile);
275
276   return m ? m->name : NULL;
277 }
278
279 /** Returns GstVaapiLevelH265 from H.265 level_idc value */
280 GstVaapiLevelH265
281 gst_vaapi_utils_h265_get_level (guint8 level_idc)
282 {
283   const GstVaapiH265LevelLimits *llp;
284
285   for (llp = gst_vaapi_h265_level_limits; llp->level != 0; llp++) {
286     if (llp->level_idc == level_idc)
287       return llp->level;
288   }
289   GST_DEBUG ("unsupported level_idc value");
290   return (GstVaapiLevelH265) 0;
291 }
292
293 /** Returns H.265 level_idc value from GstVaapiLevelH265 */
294 guint8
295 gst_vaapi_utils_h265_get_level_idc (GstVaapiLevelH265 level)
296 {
297   const GstVaapiH265LevelLimits *const llp =
298       gst_vaapi_utils_h265_get_level_limits (level);
299
300   return llp ? llp->level_idc : 0;
301 }
302
303 /** Returns GstVaapiLevelH265 from a string representation */
304 GstVaapiLevelH265
305 gst_vaapi_utils_h265_get_level_from_string (const gchar * str)
306 {
307   gint v, level_idc = 0;
308
309   if (!str || !str[0])
310     goto not_found;
311
312   v = g_ascii_digit_value (str[0]);
313   if (v < 0)
314     goto not_found;
315   level_idc = v * 30;
316
317   switch (str[1]) {
318     case '\0':
319       break;
320     case '.':
321       v = g_ascii_digit_value (str[2]);
322       if (v < 0 || str[3] != '\0')
323         goto not_found;
324       level_idc += v;
325       break;
326     default:
327       goto not_found;
328   }
329   return gst_vaapi_utils_h265_get_level (level_idc);
330
331 not_found:
332   return (GstVaapiLevelH265) 0;
333 }
334
335 /** Returns a string representation for the supplied H.265 level */
336 const gchar *
337 gst_vaapi_utils_h265_get_level_string (GstVaapiLevelH265 level)
338 {
339   if (level < GST_VAAPI_LEVEL_H265_L1 || level > GST_VAAPI_LEVEL_H265_L6_2)
340     return NULL;
341   return gst_vaapi_h265_level_map[level - GST_VAAPI_LEVEL_H265_L1].name;
342 }
343
344 /** Returns level limits as specified in Table A-1 of the H.265 standard */
345 const GstVaapiH265LevelLimits *
346 gst_vaapi_utils_h265_get_level_limits (GstVaapiLevelH265 level)
347 {
348   if (level < GST_VAAPI_LEVEL_H265_L1 || level > GST_VAAPI_LEVEL_H265_L6_2)
349     return NULL;
350   return &gst_vaapi_h265_level_limits[level - GST_VAAPI_LEVEL_H265_L1];
351 }
352
353 /** Returns the Table A-1 & A-2 specification */
354 const GstVaapiH265LevelLimits *
355 gst_vaapi_utils_h265_get_level_limits_table (guint * out_length_ptr)
356 {
357   if (out_length_ptr)
358     *out_length_ptr = G_N_ELEMENTS (gst_vaapi_h265_level_limits) - 1;
359   return gst_vaapi_h265_level_limits;
360 }
361
362 /** Returns GstVaapiChromaType from H.265 chroma_format_idc value */
363 GstVaapiChromaType
364 gst_vaapi_utils_h265_get_chroma_type (guint chroma_format_idc,
365     guint luma_bit_depth, guint chroma_bit_depth)
366 {
367   GstVaapiChromaType chroma_type = (GstVaapiChromaType) 0;
368   guint depth = 0;
369
370   if (luma_bit_depth < 8 || chroma_bit_depth < 8 ||
371       luma_bit_depth > 16 || chroma_bit_depth > 16) {
372     GST_WARNING ("invalid luma_bit_depth or chroma_bit_depth value");
373     return chroma_type;
374   }
375
376   depth = MAX (luma_bit_depth, chroma_bit_depth);
377
378   switch (chroma_format_idc) {
379     case 0:
380       chroma_type = GST_VAAPI_CHROMA_TYPE_YUV400;
381       break;
382     case 1:
383       if (depth == 8)
384         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
385       else if (depth > 8 && depth <= 10)
386         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420_10BPP;
387       else if (depth > 10 && depth <= 12)
388         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420_12BPP;
389       break;
390     case 2:
391       if (depth == 8)
392         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422;
393       else if (depth > 8 && depth <= 10)
394         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422_10BPP;
395       else if (depth > 10 && depth <= 12)
396         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422_12BPP;
397       break;
398     case 3:
399       if (depth == 8)
400         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444;
401       else if (depth > 8 && depth <= 10)
402         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444_10BPP;
403       else if (depth > 10 && depth <= 12)
404         chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444_12BPP;
405       break;
406     default:
407       break;
408   }
409
410   if (chroma_type == (GstVaapiChromaType) 0)
411     GST_DEBUG ("unsupported chroma_format_idc value");
412
413   return chroma_type;
414 }
415
416 /** Returns H.265 chroma_format_idc value from GstVaapiChromaType */
417 guint
418 gst_vaapi_utils_h265_get_chroma_format_idc (GstVaapiChromaType chroma_type)
419 {
420   guint chroma_format_idc;
421
422   switch (chroma_type) {
423     case GST_VAAPI_CHROMA_TYPE_YUV400:
424       chroma_format_idc = 0;
425       break;
426     case GST_VAAPI_CHROMA_TYPE_YUV420:
427     case GST_VAAPI_CHROMA_TYPE_YUV420_10BPP:
428     case GST_VAAPI_CHROMA_TYPE_YUV420_12BPP:
429       chroma_format_idc = 1;
430       break;
431     case GST_VAAPI_CHROMA_TYPE_YUV422:
432     case GST_VAAPI_CHROMA_TYPE_YUV422_10BPP:
433     case GST_VAAPI_CHROMA_TYPE_YUV422_12BPP:
434       chroma_format_idc = 2;
435       break;
436     case GST_VAAPI_CHROMA_TYPE_YUV444:
437     case GST_VAAPI_CHROMA_TYPE_YUV444_10BPP:
438     case GST_VAAPI_CHROMA_TYPE_YUV444_12BPP:
439       chroma_format_idc = 3;
440       break;
441     default:
442       GST_DEBUG ("unsupported GstVaapiChromaType value");
443       chroma_format_idc = 1;
444       break;
445   }
446   return chroma_format_idc;
447 }
448
449 /** Returns GstVaapiTierH265 from a string representation */
450 GstVaapiTierH265
451 gst_vaapi_utils_h265_get_tier_from_string (const gchar * str)
452 {
453   const struct map *const m = map_lookup_name (gst_vaapi_h265_tier_map, str);
454
455   return m ? (GstVaapiTierH265) m->value : GST_VAAPI_TIER_H265_UNKNOWN;
456 }
457
458 /** Returns a string representation for the supplied H.265 tier */
459 const gchar *
460 gst_vaapi_utils_h265_get_tier_string (GstVaapiTierH265 tier)
461 {
462   const struct map *const m = map_lookup_value (gst_vaapi_h265_tier_map, tier);
463
464   return m ? m->name : NULL;
465 }