2 * gstvaapiutils_mpeg2.c - MPEG-2 related utilities
4 * Copyright (C) 2011-2014 Intel Corporation
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
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.
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.
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
24 #include <gst/codecparsers/gstmpegvideoparser.h>
25 #include "gstvaapicompat.h"
26 #include "gstvaapiutils_mpeg2_priv.h"
29 #include "gstvaapidebug.h"
37 /* Profile string map */
38 static const struct map gst_vaapi_mpeg2_profile_map[] = {
40 { GST_VAAPI_PROFILE_MPEG2_SIMPLE, "simple" },
41 { GST_VAAPI_PROFILE_MPEG2_MAIN, "main" },
42 { GST_VAAPI_PROFILE_MPEG2_HIGH, "high" },
47 /* Level string map */
48 static const struct map gst_vaapi_mpeg2_level_map[] = {
50 { GST_VAAPI_LEVEL_MPEG2_LOW, "low" },
51 { GST_VAAPI_LEVEL_MPEG2_MAIN, "main" },
52 { GST_VAAPI_LEVEL_MPEG2_HIGH_1440, "high-1440" },
53 { GST_VAAPI_LEVEL_MPEG2_HIGH, "high" },
54 { GST_VAAPI_LEVEL_MPEG2_HIGHP, "highP" },
59 /* Table 8-10 to 8-13 (up to Main profile only) */
61 static const GstVaapiMPEG2LevelLimits gst_vaapi_mpeg2_level_limits[] = {
62 /* level h_size v_size fps samples kbps vbv_size */
63 { GST_VAAPI_LEVEL_MPEG2_LOW,
64 0x0a, 352, 288, 30, 3041280, 4000, 475136 },
65 { GST_VAAPI_LEVEL_MPEG2_MAIN,
66 0x08, 720, 576, 30, 1036800, 15000, 1835008 },
67 { GST_VAAPI_LEVEL_MPEG2_HIGH_1440,
68 0x06, 1440, 1152, 60, 47001600, 60000, 7340032 },
69 { GST_VAAPI_LEVEL_MPEG2_HIGH,
70 0x04, 1920, 1152, 60, 62668800, 80000, 9781248 },
71 /* Amendment 3: New level for 1080@50p/60p */
72 { GST_VAAPI_LEVEL_MPEG2_HIGHP,
73 0x02, 1920, 1152, 60, 125337600, 80000, 9781248 },
78 /* Lookup value in map */
79 static const struct map *
80 map_lookup_value (const struct map *m, guint value)
82 g_return_val_if_fail (m != NULL, NULL);
84 for (; m->name != NULL; m++) {
85 if (m->value == value)
91 /* Lookup name in map */
92 static const struct map *
93 map_lookup_name (const struct map *m, const gchar * name)
95 g_return_val_if_fail (m != NULL, NULL);
100 for (; m->name != NULL; m++) {
101 if (strcmp (m->name, name) == 0)
107 /** Returns a relative score for the supplied GstVaapiProfile */
109 gst_vaapi_utils_mpeg2_get_profile_score (GstVaapiProfile profile)
111 const struct map *const m =
112 map_lookup_value (gst_vaapi_mpeg2_profile_map, profile);
114 return m ? 1 + (m - gst_vaapi_mpeg2_profile_map) : 0;
117 /** Returns GstVaapiProfile from MPEG-2 profile_idc value */
119 gst_vaapi_utils_mpeg2_get_profile (guint8 profile_idc)
121 GstVaapiProfile profile;
123 switch (profile_idc) {
124 case GST_MPEG_VIDEO_PROFILE_SIMPLE:
125 profile = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
127 case GST_MPEG_VIDEO_PROFILE_MAIN:
128 profile = GST_VAAPI_PROFILE_MPEG2_MAIN;
130 case GST_MPEG_VIDEO_PROFILE_HIGH:
131 profile = GST_VAAPI_PROFILE_MPEG2_HIGH;
134 GST_DEBUG ("unsupported profile_idc value");
135 profile = GST_VAAPI_PROFILE_UNKNOWN;
141 /** Returns MPEG-2 profile_idc value from GstVaapiProfile */
143 gst_vaapi_utils_mpeg2_get_profile_idc (GstVaapiProfile profile)
148 case GST_VAAPI_PROFILE_MPEG2_SIMPLE:
149 profile_idc = GST_MPEG_VIDEO_PROFILE_SIMPLE;
151 case GST_VAAPI_PROFILE_MPEG2_MAIN:
152 profile_idc = GST_MPEG_VIDEO_PROFILE_MAIN;
154 case GST_VAAPI_PROFILE_MPEG2_HIGH:
155 profile_idc = GST_MPEG_VIDEO_PROFILE_HIGH;
158 GST_DEBUG ("unsupported GstVaapiProfile value");
165 /** Returns GstVaapiProfile from a string representation */
167 gst_vaapi_utils_mpeg2_get_profile_from_string (const gchar * str)
169 const struct map *const m =
170 map_lookup_name (gst_vaapi_mpeg2_profile_map, str);
172 return m ? (GstVaapiProfile) m->value : GST_VAAPI_PROFILE_UNKNOWN;
175 /** Returns a string representation for the supplied MPEG-2 profile */
177 gst_vaapi_utils_mpeg2_get_profile_string (GstVaapiProfile profile)
179 const struct map *const m =
180 map_lookup_value (gst_vaapi_mpeg2_profile_map, profile);
182 return m ? m->name : NULL;
185 /** Returns GstVaapiLevelMPEG2 from MPEG-2 level_idc value */
187 gst_vaapi_utils_mpeg2_get_level (guint8 level_idc)
189 const GstVaapiMPEG2LevelLimits *llp;
191 for (llp = gst_vaapi_mpeg2_level_limits; llp->level != 0; llp++) {
192 if (llp->level_idc == level_idc)
195 GST_DEBUG ("unsupported level_idc value");
196 return (GstVaapiLevelMPEG2) 0;
199 /** Returns MPEG-2 level_idc value from GstVaapiLevelMPEG2 */
201 gst_vaapi_utils_mpeg2_get_level_idc (GstVaapiLevelMPEG2 level)
203 const GstVaapiMPEG2LevelLimits *const llp =
204 gst_vaapi_utils_mpeg2_get_level_limits (level);
206 return llp ? llp->level_idc : 0;
209 /** Returns GstVaapiLevelMPEG2 from a string representation */
211 gst_vaapi_utils_mpeg2_get_level_from_string (const gchar * str)
213 const struct map *const m = map_lookup_name (gst_vaapi_mpeg2_level_map, str);
215 return (GstVaapiLevelMPEG2) (m ? m->value : 0);
218 /** Returns a string representation for the supplied MPEG-2 level */
220 gst_vaapi_utils_mpeg2_get_level_string (GstVaapiLevelMPEG2 level)
222 if (level < GST_VAAPI_LEVEL_MPEG2_LOW || level > GST_VAAPI_LEVEL_MPEG2_HIGHP)
224 return gst_vaapi_mpeg2_level_map[level - GST_VAAPI_LEVEL_MPEG2_LOW].name;
227 /** Returns level limits as specified in Tables 8-10 to 8-13 of the
229 const GstVaapiMPEG2LevelLimits *
230 gst_vaapi_utils_mpeg2_get_level_limits (GstVaapiLevelMPEG2 level)
232 if (level < GST_VAAPI_LEVEL_MPEG2_LOW || level > GST_VAAPI_LEVEL_MPEG2_HIGHP)
234 return &gst_vaapi_mpeg2_level_limits[level - GST_VAAPI_LEVEL_MPEG2_LOW];
237 /** Returns Tables 8-10 to 8-13 from the specification (up to High profile) */
238 const GstVaapiMPEG2LevelLimits *
239 gst_vaapi_utils_mpeg2_get_level_limits_table (guint * out_length_ptr)
242 *out_length_ptr = G_N_ELEMENTS (gst_vaapi_mpeg2_level_limits) - 1;
243 return gst_vaapi_mpeg2_level_limits;
246 /** Returns GstVaapiChromaType from MPEG-2 chroma_format_idc value */
248 gst_vaapi_utils_mpeg2_get_chroma_type (guint chroma_format_idc)
250 GstVaapiChromaType chroma_type;
252 switch (chroma_format_idc) {
253 case GST_MPEG_VIDEO_CHROMA_420:
254 chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
256 case GST_MPEG_VIDEO_CHROMA_422:
257 chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422;
259 case GST_MPEG_VIDEO_CHROMA_444:
260 chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444;
263 GST_DEBUG ("unsupported chroma_format_idc value");
264 chroma_type = (GstVaapiChromaType) 0;
270 /** Returns MPEG-2 chroma_format_idc value from GstVaapiChromaType */
272 gst_vaapi_utils_mpeg2_get_chroma_format_idc (GstVaapiChromaType chroma_type)
274 guint chroma_format_idc;
276 switch (chroma_type) {
277 case GST_VAAPI_CHROMA_TYPE_YUV420:
278 chroma_format_idc = GST_MPEG_VIDEO_CHROMA_420;
280 case GST_VAAPI_CHROMA_TYPE_YUV422:
281 chroma_format_idc = GST_MPEG_VIDEO_CHROMA_422;
283 case GST_VAAPI_CHROMA_TYPE_YUV444:
284 chroma_format_idc = GST_MPEG_VIDEO_CHROMA_444;
287 GST_DEBUG ("unsupported GstVaapiChromaType value");
288 chroma_format_idc = 1;
291 return chroma_format_idc;