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