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