d618fff1784577a9389c8a8a83e0889f622a54d5
[mesa.git] / src / gallium / state_trackers / vdpau / decode.c
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 "util/u_memory.h"
29 #include "util/u_math.h"
30 #include "util/u_debug.h"
31 #include "util/u_video.h"
32
33 #include "vdpau_private.h"
34
35 /**
36 * Create a VdpDecoder.
37 */
38 VdpStatus
39 vlVdpDecoderCreate(VdpDevice device,
40 VdpDecoderProfile profile,
41 uint32_t width, uint32_t height,
42 uint32_t max_references,
43 VdpDecoder *decoder)
44 {
45 enum pipe_video_profile p_profile;
46 struct pipe_context *pipe;
47 struct pipe_screen *screen;
48 vlVdpDevice *dev;
49 vlVdpDecoder *vldecoder;
50 VdpStatus ret;
51 bool supported;
52
53 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Creating decoder\n");
54
55 if (!decoder)
56 return VDP_STATUS_INVALID_POINTER;
57 *decoder = 0;
58
59 if (!(width && height))
60 return VDP_STATUS_INVALID_VALUE;
61
62 p_profile = ProfileToPipe(profile);
63 if (p_profile == PIPE_VIDEO_PROFILE_UNKNOWN)
64 return VDP_STATUS_INVALID_DECODER_PROFILE;
65
66 dev = vlGetDataHTAB(device);
67 if (!dev)
68 return VDP_STATUS_INVALID_HANDLE;
69
70 pipe = dev->context->pipe;
71 screen = dev->vscreen->pscreen;
72 supported = screen->get_video_param
73 (
74 screen,
75 p_profile,
76 PIPE_VIDEO_CAP_SUPPORTED
77 );
78 if (!supported)
79 return VDP_STATUS_INVALID_DECODER_PROFILE;
80
81 vldecoder = CALLOC(1,sizeof(vlVdpDecoder));
82 if (!vldecoder)
83 return VDP_STATUS_RESOURCES;
84
85 vldecoder->device = dev;
86
87 vldecoder->decoder = pipe->create_video_decoder
88 (
89 pipe, p_profile,
90 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
91 PIPE_VIDEO_CHROMA_FORMAT_420,
92 width, height, max_references,
93 false
94 );
95
96 if (!vldecoder->decoder) {
97 ret = VDP_STATUS_ERROR;
98 goto error_decoder;
99 }
100
101 *decoder = vlAddDataHTAB(vldecoder);
102 if (*decoder == 0) {
103 ret = VDP_STATUS_ERROR;
104 goto error_handle;
105 }
106
107 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder created succesfully\n");
108
109 return VDP_STATUS_OK;
110
111 error_handle:
112
113 vldecoder->decoder->destroy(vldecoder->decoder);
114
115 error_decoder:
116 FREE(vldecoder);
117 return ret;
118 }
119
120 /**
121 * Destroy a VdpDecoder.
122 */
123 VdpStatus
124 vlVdpDecoderDestroy(VdpDecoder decoder)
125 {
126 vlVdpDecoder *vldecoder;
127
128 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Destroying decoder\n");
129
130 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
131 if (!vldecoder)
132 return VDP_STATUS_INVALID_HANDLE;
133
134 vldecoder->decoder->destroy(vldecoder->decoder);
135
136 FREE(vldecoder);
137
138 return VDP_STATUS_OK;
139 }
140
141 /**
142 * Retrieve the parameters used to create a VdpBitmapSurface.
143 */
144 VdpStatus
145 vlVdpDecoderGetParameters(VdpDecoder decoder,
146 VdpDecoderProfile *profile,
147 uint32_t *width,
148 uint32_t *height)
149 {
150 vlVdpDecoder *vldecoder;
151
152 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder get parameters called\n");
153
154 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
155 if (!vldecoder)
156 return VDP_STATUS_INVALID_HANDLE;
157
158 *profile = PipeToProfile(vldecoder->decoder->profile);
159 *width = vldecoder->decoder->width;
160 *height = vldecoder->decoder->height;
161
162 return VDP_STATUS_OK;
163 }
164
165 static VdpStatus
166 vlVdpGetReferenceFrame(VdpVideoSurface handle, struct pipe_video_buffer **ref_frame)
167 {
168 vlVdpSurface *surface;
169
170 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
171 if (handle == VDP_INVALID_HANDLE) {
172 *ref_frame = NULL;
173 return VDP_STATUS_OK;
174 }
175
176 surface = vlGetDataHTAB(handle);
177 if (!surface)
178 return VDP_STATUS_INVALID_HANDLE;
179
180 *ref_frame = surface->video_buffer;
181 if (!*ref_frame)
182 return VDP_STATUS_INVALID_HANDLE;
183
184 return VDP_STATUS_OK;
185 }
186
187 /**
188 * Decode a mpeg 1/2 video.
189 */
190 static VdpStatus
191 vlVdpDecoderRenderMpeg12(struct pipe_mpeg12_picture_desc *picture,
192 VdpPictureInfoMPEG1Or2 *picture_info)
193 {
194 VdpStatus r;
195
196 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG12\n");
197
198 r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
199 if (r != VDP_STATUS_OK)
200 return r;
201
202 r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
203 if (r != VDP_STATUS_OK)
204 return r;
205
206 picture->picture_coding_type = picture_info->picture_coding_type;
207 picture->picture_structure = picture_info->picture_structure;
208 picture->frame_pred_frame_dct = picture_info->frame_pred_frame_dct;
209 picture->q_scale_type = picture_info->q_scale_type;
210 picture->alternate_scan = picture_info->alternate_scan;
211 picture->intra_vlc_format = picture_info->intra_vlc_format;
212 picture->concealment_motion_vectors = picture_info->concealment_motion_vectors;
213 picture->intra_dc_precision = picture_info->intra_dc_precision;
214 picture->f_code[0][0] = picture_info->f_code[0][0] - 1;
215 picture->f_code[0][1] = picture_info->f_code[0][1] - 1;
216 picture->f_code[1][0] = picture_info->f_code[1][0] - 1;
217 picture->f_code[1][1] = picture_info->f_code[1][1] - 1;
218 picture->num_slices = picture_info->slice_count;
219 picture->top_field_first = picture_info->top_field_first;
220 picture->full_pel_forward_vector = picture_info->full_pel_forward_vector;
221 picture->full_pel_backward_vector = picture_info->full_pel_backward_vector;
222 picture->intra_matrix = picture_info->intra_quantizer_matrix;
223 picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;
224
225 return VDP_STATUS_OK;
226 }
227
228 /**
229 * Decode a mpeg 4 video.
230 */
231 static VdpStatus
232 vlVdpDecoderRenderMpeg4(struct pipe_mpeg4_picture_desc *picture,
233 VdpPictureInfoMPEG4Part2 *picture_info)
234 {
235 VdpStatus r;
236 unsigned i;
237
238 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG4\n");
239
240 r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
241 if (r != VDP_STATUS_OK)
242 return r;
243
244 r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
245 if (r != VDP_STATUS_OK)
246 return r;
247
248 for (i = 0; i < 2; ++i) {
249 picture->trd[i] = picture_info->trd[i];
250 picture->trb[i] = picture_info->trb[i];
251 }
252 picture->vop_time_increment_resolution = picture_info->vop_time_increment_resolution;
253 picture->vop_coding_type = picture_info->vop_coding_type;
254 picture->vop_fcode_forward = picture_info->vop_fcode_forward;
255 picture->vop_fcode_backward = picture_info->vop_fcode_backward;
256 picture->resync_marker_disable = picture_info->resync_marker_disable;
257 picture->interlaced = picture_info->interlaced;
258 picture->quant_type = picture_info->quant_type;
259 picture->quarter_sample = picture_info->quarter_sample;
260 picture->short_video_header = picture_info->short_video_header;
261 picture->rounding_control = picture_info->rounding_control;
262 picture->alternate_vertical_scan_flag = picture_info->alternate_vertical_scan_flag;
263 picture->top_field_first = picture_info->top_field_first;
264 picture->intra_matrix = picture_info->intra_quantizer_matrix;
265 picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;
266
267 return VDP_STATUS_OK;
268 }
269
270 static VdpStatus
271 vlVdpDecoderRenderVC1(struct pipe_vc1_picture_desc *picture,
272 VdpPictureInfoVC1 *picture_info)
273 {
274 VdpStatus r;
275
276 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding VC-1\n");
277
278 r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
279 if (r != VDP_STATUS_OK)
280 return r;
281
282 r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
283 if (r != VDP_STATUS_OK)
284 return r;
285
286 picture->slice_count = picture_info->slice_count;
287 picture->picture_type = picture_info->picture_type;
288 picture->frame_coding_mode = picture_info->frame_coding_mode;
289 picture->postprocflag = picture_info->postprocflag;
290 picture->pulldown = picture_info->pulldown;
291 picture->interlace = picture_info->interlace;
292 picture->tfcntrflag = picture_info->tfcntrflag;
293 picture->finterpflag = picture_info->finterpflag;
294 picture->psf = picture_info->psf;
295 picture->dquant = picture_info->dquant;
296 picture->panscan_flag = picture_info->panscan_flag;
297 picture->refdist_flag = picture_info->refdist_flag;
298 picture->quantizer = picture_info->quantizer;
299 picture->extended_mv = picture_info->extended_mv;
300 picture->extended_dmv = picture_info->extended_dmv;
301 picture->overlap = picture_info->overlap;
302 picture->vstransform = picture_info->vstransform;
303 picture->loopfilter = picture_info->loopfilter;
304 picture->fastuvmc = picture_info->fastuvmc;
305 picture->range_mapy_flag = picture_info->range_mapy_flag;
306 picture->range_mapy = picture_info->range_mapy;
307 picture->range_mapuv_flag = picture_info->range_mapuv_flag;
308 picture->range_mapuv = picture_info->range_mapuv;
309 picture->multires = picture_info->multires;
310 picture->syncmarker = picture_info->syncmarker;
311 picture->rangered = picture_info->rangered;
312 picture->maxbframes = picture_info->maxbframes;
313 picture->deblockEnable = picture_info->deblockEnable;
314 picture->pquant = picture_info->pquant;
315
316 return VDP_STATUS_OK;
317 }
318
319 static VdpStatus
320 vlVdpDecoderRenderH264(struct pipe_h264_picture_desc *picture,
321 VdpPictureInfoH264 *picture_info)
322 {
323 unsigned i;
324
325 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding H264\n");
326
327 picture->slice_count = picture_info->slice_count;
328 picture->field_order_cnt[0] = picture_info->field_order_cnt[0];
329 picture->field_order_cnt[1] = picture_info->field_order_cnt[1];
330 picture->is_reference = picture_info->is_reference;
331 picture->frame_num = picture_info->frame_num;
332 picture->field_pic_flag = picture_info->field_pic_flag;
333 picture->bottom_field_flag = picture_info->bottom_field_flag;
334 picture->num_ref_frames = picture_info->num_ref_frames;
335 picture->mb_adaptive_frame_field_flag = picture_info->mb_adaptive_frame_field_flag;
336 picture->constrained_intra_pred_flag = picture_info->constrained_intra_pred_flag;
337 picture->weighted_pred_flag = picture_info->weighted_pred_flag;
338 picture->weighted_bipred_idc = picture_info->weighted_bipred_idc;
339 picture->frame_mbs_only_flag = picture_info->frame_mbs_only_flag;
340 picture->transform_8x8_mode_flag = picture_info->transform_8x8_mode_flag;
341 picture->chroma_qp_index_offset = picture_info->chroma_qp_index_offset;
342 picture->second_chroma_qp_index_offset = picture_info->second_chroma_qp_index_offset;
343 picture->pic_init_qp_minus26 = picture_info->pic_init_qp_minus26;
344 picture->num_ref_idx_l0_active_minus1 = picture_info->num_ref_idx_l0_active_minus1;
345 picture->num_ref_idx_l1_active_minus1 = picture_info->num_ref_idx_l1_active_minus1;
346 picture->log2_max_frame_num_minus4 = picture_info->log2_max_frame_num_minus4;
347 picture->pic_order_cnt_type = picture_info->pic_order_cnt_type;
348 picture->log2_max_pic_order_cnt_lsb_minus4 = picture_info->log2_max_pic_order_cnt_lsb_minus4;
349 picture->delta_pic_order_always_zero_flag = picture_info->delta_pic_order_always_zero_flag;
350 picture->direct_8x8_inference_flag = picture_info->direct_8x8_inference_flag;
351 picture->entropy_coding_mode_flag = picture_info->entropy_coding_mode_flag;
352 picture->pic_order_present_flag = picture_info->pic_order_present_flag;
353 picture->deblocking_filter_control_present_flag = picture_info->deblocking_filter_control_present_flag;
354 picture->redundant_pic_cnt_present_flag = picture_info->redundant_pic_cnt_present_flag;
355
356 memcpy(picture->scaling_lists_4x4, picture_info->scaling_lists_4x4, 6*16);
357 memcpy(picture->scaling_lists_8x8, picture_info->scaling_lists_8x8, 2*64);
358
359 for (i = 0; i < 16; ++i) {
360 VdpStatus ret = vlVdpGetReferenceFrame
361 (
362 picture_info->referenceFrames[i].surface,
363 &picture->ref[i]
364 );
365 if (ret != VDP_STATUS_OK)
366 return ret;
367
368 picture->is_long_term[i] = picture_info->referenceFrames[i].is_long_term;
369 picture->top_is_reference[i] = picture_info->referenceFrames[i].top_is_reference;
370 picture->bottom_is_reference[i] = picture_info->referenceFrames[i].bottom_is_reference;
371 picture->field_order_cnt_list[i][0] = picture_info->referenceFrames[i].field_order_cnt[0];
372 picture->field_order_cnt_list[i][1] = picture_info->referenceFrames[i].field_order_cnt[1];
373 picture->frame_num_list[i] = picture_info->referenceFrames[i].frame_idx;
374 }
375
376 return VDP_STATUS_OK;
377 }
378
379 /**
380 * Decode a compressed field/frame and render the result into a VdpVideoSurface.
381 */
382 VdpStatus
383 vlVdpDecoderRender(VdpDecoder decoder,
384 VdpVideoSurface target,
385 VdpPictureInfo const *picture_info,
386 uint32_t bitstream_buffer_count,
387 VdpBitstreamBuffer const *bitstream_buffers)
388 {
389 const void * buffers[bitstream_buffer_count];
390 unsigned sizes[bitstream_buffer_count];
391 vlVdpDecoder *vldecoder;
392 vlVdpSurface *vlsurf;
393 VdpStatus ret;
394 struct pipe_video_decoder *dec;
395 unsigned i;
396 union {
397 struct pipe_picture_desc base;
398 struct pipe_mpeg12_picture_desc mpeg12;
399 struct pipe_mpeg4_picture_desc mpeg4;
400 struct pipe_vc1_picture_desc vc1;
401 struct pipe_h264_picture_desc h264;
402 } desc;
403
404 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding\n");
405
406 if (!(picture_info && bitstream_buffers))
407 return VDP_STATUS_INVALID_POINTER;
408
409 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
410 if (!vldecoder)
411 return VDP_STATUS_INVALID_HANDLE;
412 dec = vldecoder->decoder;
413
414 vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);
415 if (!vlsurf)
416 return VDP_STATUS_INVALID_HANDLE;
417
418 if (vlsurf->device != vldecoder->device)
419 return VDP_STATUS_HANDLE_DEVICE_MISMATCH;
420
421 if (vlsurf->video_buffer->chroma_format != dec->chroma_format)
422 // TODO: Recreate decoder with correct chroma
423 return VDP_STATUS_INVALID_CHROMA_TYPE;
424
425 memset(&desc, 0, sizeof(desc));
426 desc.base.profile = dec->profile;
427 switch (u_reduce_video_profile(dec->profile)) {
428 case PIPE_VIDEO_CODEC_MPEG12:
429 ret = vlVdpDecoderRenderMpeg12(&desc.mpeg12, (VdpPictureInfoMPEG1Or2 *)picture_info);
430 break;
431 case PIPE_VIDEO_CODEC_MPEG4:
432 ret = vlVdpDecoderRenderMpeg4(&desc.mpeg4, (VdpPictureInfoMPEG4Part2 *)picture_info);
433 break;
434 case PIPE_VIDEO_CODEC_VC1:
435 ret = vlVdpDecoderRenderVC1(&desc.vc1, (VdpPictureInfoVC1 *)picture_info);
436 break;
437 case PIPE_VIDEO_CODEC_MPEG4_AVC:
438 ret = vlVdpDecoderRenderH264(&desc.h264, (VdpPictureInfoH264 *)picture_info);
439 break;
440 default:
441 return VDP_STATUS_INVALID_DECODER_PROFILE;
442 }
443 if (ret != VDP_STATUS_OK)
444 return ret;
445
446 for (i = 0; i < bitstream_buffer_count; ++i) {
447 buffers[i] = bitstream_buffers[i].bitstream;
448 sizes[i] = bitstream_buffers[i].bitstream_bytes;
449 }
450
451 dec->begin_frame(dec, vlsurf->video_buffer, &desc.base);
452 dec->decode_bitstream(dec, vlsurf->video_buffer, &desc.base, bitstream_buffer_count, buffers, sizes);
453 dec->end_frame(dec, vlsurf->video_buffer, &desc.base);
454 return ret;
455 }