decode.c revision 7d2bdc2d4db8321a72edcc921a0fcfa4e4d41ef9
1/************************************************************************** 2 * 3 * Copyright 2010 Thomas Balling Sørensen. 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 TUNGSTEN GRAPHICS 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#include "vdpau_private.h" 29#include "mpeg2_bitstream_parser.h" 30#include <util/u_memory.h> 31#include <util/u_math.h> 32#include <pipe/p_video_context.h> 33#include <util/u_debug.h> 34 35VdpStatus 36vlVdpDecoderCreate ( VdpDevice device, 37 VdpDecoderProfile profile, 38 uint32_t width, uint32_t height, 39 uint32_t max_references, 40 VdpDecoder *decoder 41) 42{ 43 enum pipe_video_profile p_profile = PIPE_VIDEO_PROFILE_UNKNOWN; 44 VdpStatus ret = VDP_STATUS_OK; 45 vlVdpDecoder *vldecoder = NULL; 46 47 debug_printf("[VDPAU] Creating decoder\n"); 48 49 if (!decoder) 50 return VDP_STATUS_INVALID_POINTER; 51 52 if (!(width && height)) 53 return VDP_STATUS_INVALID_VALUE; 54 55 vlVdpDevice *dev = vlGetDataHTAB(device); 56 if (!dev) { 57 ret = VDP_STATUS_INVALID_HANDLE; 58 goto inv_device; 59 } 60 61 vldecoder = CALLOC(1,sizeof(vlVdpDecoder)); 62 if (!vldecoder) { 63 ret = VDP_STATUS_RESOURCES; 64 goto no_decoder; 65 } 66 67 p_profile = ProfileToPipe(profile); 68 if (p_profile == PIPE_VIDEO_PROFILE_UNKNOWN) { 69 ret = VDP_STATUS_INVALID_DECODER_PROFILE; 70 goto inv_profile; 71 } 72 73 // TODO: Define max_references. Used mainly for H264 74 75 vldecoder->profile = p_profile; 76 vldecoder->height = height; 77 vldecoder->width = width; 78 vldecoder->device = dev; 79 vldecoder->vctx = NULL; 80 81 *decoder = vlAddDataHTAB(vldecoder); 82 if (*decoder == 0) { 83 ret = VDP_STATUS_ERROR; 84 goto no_handle; 85 } 86 debug_printf("[VDPAU] Decoder created succesfully\n"); 87 88 return VDP_STATUS_OK; 89 90 no_handle: 91 FREE(vldecoder); 92 inv_profile: 93 no_screen: 94 no_decoder: 95 inv_device: 96 return ret; 97} 98 99VdpStatus 100vlVdpDecoderDestroy (VdpDecoder decoder 101) 102{ 103 debug_printf("[VDPAU] Destroying decoder\n"); 104 vlVdpDecoder *vldecoder; 105 106 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder); 107 if (!vldecoder) { 108 return VDP_STATUS_INVALID_HANDLE; 109 } 110 111 if (vldecoder->vctx) 112 { 113 if (vldecoder->vctx->vscreen) 114 vl_screen_destroy(vldecoder->vctx->vscreen); 115 } 116 117 if (vldecoder->vctx) 118 vl_video_destroy(vldecoder->vctx); 119 120 FREE(vldecoder); 121 122 return VDP_STATUS_OK; 123} 124 125VdpStatus 126vlVdpCreateSurfaceTarget (vlVdpDecoder *vldecoder, 127 vlVdpSurface *vlsurf 128) 129{ 130 struct pipe_resource tmplt; 131 struct pipe_resource *surf_tex; 132 struct pipe_video_context *vctx; 133 134 debug_printf("[VDPAU] Creating surface\n"); 135 136 if(!(vldecoder && vlsurf)) 137 return VDP_STATUS_INVALID_POINTER; 138 139 vctx = vldecoder->vctx->vpipe; 140 141 memset(&tmplt, 0, sizeof(struct pipe_resource)); 142 tmplt.target = PIPE_TEXTURE_2D; 143 tmplt.format = vctx->get_param(vctx,PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT); 144 tmplt.last_level = 0; 145 146 if (vctx->is_format_supported(vctx, tmplt.format, 147 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET, 148 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO)) { 149 tmplt.width0 = vlsurf->width; 150 tmplt.height0 = vlsurf->height; 151 } 152 else { 153 assert(vctx->is_format_supported(vctx, tmplt.format, 154 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET, 155 PIPE_TEXTURE_GEOM_NON_SQUARE)); 156 tmplt.width0 = util_next_power_of_two(vlsurf->width); 157 tmplt.height0 = util_next_power_of_two(vlsurf->height); 158 } 159 160 tmplt.depth0 = 1; 161 tmplt.usage = PIPE_USAGE_DEFAULT; 162 tmplt.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 163 tmplt.flags = 0; 164 165 surf_tex = vctx->screen->resource_create(vctx->screen, &tmplt); 166 167 vlsurf->psurface = vctx->screen->get_tex_surface(vctx->screen, surf_tex, 0, 0, 0, 168 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 169 170 pipe_resource_reference(&surf_tex, NULL); 171 172 if (!vlsurf->psurface) 173 return VDP_STATUS_RESOURCES; 174 debug_printf("[VDPAU] Done creating surface\n"); 175 176 return VDP_STATUS_OK; 177} 178 179VdpStatus 180vlVdpDecoderRenderMpeg2 (vlVdpDecoder *vldecoder, 181 vlVdpSurface *vlsurf, 182 VdpPictureInfoMPEG1Or2 *picture_info, 183 uint32_t bitstream_buffer_count, 184 VdpBitstreamBuffer const *bitstream_buffers 185 ) 186{ 187 struct pipe_video_context *vpipe; 188 vlVdpSurface *t_vdp_surf; 189 vlVdpSurface *p_vdp_surf; 190 vlVdpSurface *f_vdp_surf; 191 struct pipe_surface *t_surf; 192 struct pipe_surface *p_surf; 193 struct pipe_surface *f_surf; 194 uint32_t num_macroblocks; 195 struct pipe_mpeg12_macroblock *pipe_macroblocks; 196 VdpStatus ret; 197 198 debug_printf("[VDPAU] Decoding MPEG2\n"); 199 200 t_vdp_surf = vlsurf; 201 202 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */ 203 if (picture_info->backward_reference == VDP_INVALID_HANDLE) 204 p_vdp_surf = NULL; 205 else { 206 p_vdp_surf = (vlVdpSurface *)vlGetDataHTAB(picture_info->backward_reference); 207 if (!p_vdp_surf) 208 return VDP_STATUS_INVALID_HANDLE; 209 } 210 211 if (picture_info->forward_reference == VDP_INVALID_HANDLE) 212 f_vdp_surf = NULL; 213 else { 214 f_vdp_surf = (vlVdpSurface *)vlGetDataHTAB(picture_info->forward_reference); 215 if (!f_vdp_surf) 216 return VDP_STATUS_INVALID_HANDLE; 217 } 218 219 220 if (f_vdp_surf == VDP_INVALID_HANDLE) f_vdp_surf = NULL; 221 222 ret = vlVdpCreateSurfaceTarget(vldecoder,t_vdp_surf); 223 224 vpipe = vldecoder->vctx->vpipe; 225 226 if (vlVdpMPEG2BitstreamToMacroblock(vpipe->screen, bitstream_buffers, bitstream_buffer_count, 227 &num_macroblocks, &pipe_macroblocks)) 228 { 229 debug_printf("[VDPAU] Error in frame-header. Skipping.\n"); 230 231 ret = VDP_STATUS_OK; 232 goto skip_frame; 233 } 234 235 vpipe->set_decode_target(vpipe,t_surf); 236 vpipe->decode_macroblocks(vpipe, p_surf, f_surf, num_macroblocks, (struct pipe_macroblock *)pipe_macroblocks, NULL); 237 238 skip_frame: 239 return ret; 240} 241 242VdpStatus 243vlVdpDecoderRender (VdpDecoder decoder, 244 VdpVideoSurface target, 245 VdpPictureInfo const *picture_info, 246 uint32_t bitstream_buffer_count, 247 VdpBitstreamBuffer const *bitstream_buffers 248) 249{ 250 vlVdpDecoder *vldecoder; 251 vlVdpSurface *vlsurf; 252 struct vl_screen *vscreen; 253 VdpStatus ret; 254 debug_printf("[VDPAU] Decoding\n"); 255 256 if (!(picture_info && bitstream_buffers)) 257 return VDP_STATUS_INVALID_POINTER; 258 259 260 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder); 261 if (!vldecoder) 262 return VDP_STATUS_INVALID_HANDLE; 263 264 vlsurf = (vlVdpSurface *)vlGetDataHTAB(target); 265 if (!vlsurf) 266 return VDP_STATUS_INVALID_HANDLE; 267 268 if (vlsurf->device != vldecoder->device) 269 return VDP_STATUS_HANDLE_DEVICE_MISMATCH; 270 271 /* Test doesn't make sence */ 272 /*if (vlsurf->chroma_format != vldecoder->chroma_format) 273 return VDP_STATUS_INVALID_CHROMA_TYPE;*/ 274 275 vscreen = vl_screen_create(vldecoder->device->display, vldecoder->device->screen); 276 if (!vscreen) 277 return VDP_STATUS_RESOURCES; 278 279 vldecoder->vctx = vl_video_create(vscreen, vldecoder->profile, vlsurf->chroma_format, vldecoder->width, vldecoder->height); 280 if (!vldecoder->vctx) 281 return VDP_STATUS_RESOURCES; 282 283 // TODO: Right now only mpeg2 is supported. 284 switch (vldecoder->vctx->vpipe->profile) { 285 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE: 286 case PIPE_VIDEO_PROFILE_MPEG2_MAIN: 287 ret = vlVdpDecoderRenderMpeg2(vldecoder,vlsurf,(VdpPictureInfoMPEG1Or2 *)picture_info, 288 bitstream_buffer_count,bitstream_buffers); 289 break; 290 default: 291 return VDP_STATUS_INVALID_DECODER_PROFILE; 292 } 293 assert(0); 294 295 return ret; 296} 297 298VdpStatus 299vlVdpGenerateCSCMatrix( 300 VdpProcamp *procamp, 301 VdpColorStandard standard, 302 VdpCSCMatrix *csc_matrix) 303{ 304 debug_printf("[VDPAU] Generating CSCMatrix\n"); 305 if (!(csc_matrix && procamp)) 306 return VDP_STATUS_INVALID_POINTER; 307 308 return VDP_STATUS_OK; 309}