decode.c revision ea78480029450c019287c2a94d7c42a6a1d12dc3
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
32#include "vdpau_private.h"
33
34VdpStatus
35vlVdpDecoderCreate(VdpDevice device,
36                   VdpDecoderProfile profile,
37                   uint32_t width, uint32_t height,
38                   uint32_t max_references,
39                   VdpDecoder *decoder)
40{
41   enum pipe_video_profile p_profile;
42   struct pipe_context *pipe;
43   vlVdpDevice *dev;
44   vlVdpDecoder *vldecoder;
45   VdpStatus ret;
46   unsigned i;
47
48   VDPAU_MSG(VDPAU_TRACE, "[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   p_profile = ProfileToPipe(profile);
57   if (p_profile == PIPE_VIDEO_PROFILE_UNKNOWN)
58      return VDP_STATUS_INVALID_DECODER_PROFILE;
59
60   dev = vlGetDataHTAB(device);
61   if (!dev)
62      return VDP_STATUS_INVALID_HANDLE;
63
64   pipe = dev->context->pipe;
65
66   vldecoder = CALLOC(1,sizeof(vlVdpDecoder));
67   if (!vldecoder)
68      return VDP_STATUS_RESOURCES;
69
70   vldecoder->device = dev;
71
72   // TODO: Define max_references. Used mainly for H264
73   vldecoder->decoder = pipe->create_video_decoder
74   (
75      pipe, p_profile,
76      PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
77      PIPE_VIDEO_CHROMA_FORMAT_420,
78      width, height
79   );
80   if (!vldecoder->decoder) {
81      ret = VDP_STATUS_ERROR;
82      goto error_decoder;
83   }
84
85   vldecoder->cur_buffer = 0;
86
87   for (i = 0; i < VL_NUM_DECODE_BUFFERS; ++i) {
88      vldecoder->buffer[i] = vldecoder->decoder->create_buffer(vldecoder->decoder);
89      if (!vldecoder->buffer[i]) {
90         ret = VDP_STATUS_ERROR;
91         goto error_buffer;
92      }
93   }
94
95   *decoder = vlAddDataHTAB(vldecoder);
96   if (*decoder == 0) {
97      ret = VDP_STATUS_ERROR;
98      goto error_handle;
99   }
100
101   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder created succesfully\n");
102
103   return VDP_STATUS_OK;
104
105error_handle:
106error_buffer:
107
108   for (i = 0; i < VL_NUM_DECODE_BUFFERS; ++i)
109      if (vldecoder->buffer[i])
110         vldecoder->buffer[i]->destroy(vldecoder->buffer[i]);
111
112   vldecoder->decoder->destroy(vldecoder->decoder);
113
114error_decoder:
115   FREE(vldecoder);
116   return ret;
117}
118
119VdpStatus
120vlVdpDecoderDestroy(VdpDecoder decoder)
121{
122   vlVdpDecoder *vldecoder;
123   unsigned i;
124
125   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Destroying decoder\n");
126
127   vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
128   if (!vldecoder)
129      return VDP_STATUS_INVALID_HANDLE;
130
131   for (i = 0; i < VL_NUM_DECODE_BUFFERS; ++i)
132      if (vldecoder->buffer[i])
133         vldecoder->buffer[i]->destroy(vldecoder->buffer[i]);
134
135   vldecoder->decoder->destroy(vldecoder->decoder);
136
137   FREE(vldecoder);
138
139   return VDP_STATUS_OK;
140}
141
142VdpStatus
143vlVdpDecoderGetParameters(VdpDecoder decoder,
144                          VdpDecoderProfile *profile,
145                          uint32_t *width,
146                          uint32_t *height)
147{
148   return VDP_STATUS_OK;
149}
150
151static VdpStatus
152vlVdpDecoderRenderMpeg2(struct pipe_video_decoder *decoder,
153                        struct pipe_video_decode_buffer *buffer,
154                        struct pipe_video_buffer *target,
155                        VdpPictureInfoMPEG1Or2 *picture_info,
156                        uint32_t bitstream_buffer_count,
157                        VdpBitstreamBuffer const *bitstream_buffers)
158{
159   struct pipe_mpeg12_picture_desc picture;
160   struct pipe_video_buffer *ref_frames[2];
161   uint8_t intra_quantizer_matrix[64];
162   unsigned num_ycbcr_blocks[3] = { 0, 0, 0 };
163   unsigned i;
164
165   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG2\n");
166
167   /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
168   if (picture_info->forward_reference ==  VDP_INVALID_HANDLE)
169      ref_frames[0] = NULL;
170   else {
171      ref_frames[0] = ((vlVdpSurface *)vlGetDataHTAB(picture_info->forward_reference))->video_buffer;
172      if (!ref_frames[0])
173         return VDP_STATUS_INVALID_HANDLE;
174   }
175
176   if (picture_info->backward_reference ==  VDP_INVALID_HANDLE)
177      ref_frames[1] = NULL;
178   else {
179      ref_frames[1] = ((vlVdpSurface *)vlGetDataHTAB(picture_info->backward_reference))->video_buffer;
180      if (!ref_frames[1])
181         return VDP_STATUS_INVALID_HANDLE;
182   }
183
184   memset(&picture, 0, sizeof(picture));
185   picture.base.profile = decoder->profile;
186   picture.picture_coding_type = picture_info->picture_coding_type;
187   picture.picture_structure = picture_info->picture_structure;
188   picture.frame_pred_frame_dct = picture_info->frame_pred_frame_dct;
189   picture.q_scale_type = picture_info->q_scale_type;
190   picture.alternate_scan = picture_info->alternate_scan;
191   picture.intra_vlc_format = picture_info->intra_vlc_format;
192   picture.concealment_motion_vectors = picture_info->concealment_motion_vectors;
193   picture.f_code[0][0] = picture_info->f_code[0][0] - 1;
194   picture.f_code[0][1] = picture_info->f_code[0][1] - 1;
195   picture.f_code[1][0] = picture_info->f_code[1][0] - 1;
196   picture.f_code[1][1] = picture_info->f_code[1][1] - 1;
197
198   buffer->begin_frame(buffer);
199
200   memcpy(intra_quantizer_matrix, picture_info->intra_quantizer_matrix, sizeof(intra_quantizer_matrix));
201   intra_quantizer_matrix[0] = 1 << (7 - picture_info->intra_dc_precision);
202   buffer->set_quant_matrix(buffer, intra_quantizer_matrix, picture_info->non_intra_quantizer_matrix);
203
204   for (i = 0; i < bitstream_buffer_count; ++i)
205      buffer->decode_bitstream(buffer, bitstream_buffers[i].bitstream_bytes,
206                               bitstream_buffers[i].bitstream, &picture, num_ycbcr_blocks);
207
208   buffer->end_frame(buffer);
209
210   decoder->flush_buffer(buffer, num_ycbcr_blocks, ref_frames, target);
211
212   return VDP_STATUS_OK;
213}
214
215VdpStatus
216vlVdpDecoderRender(VdpDecoder decoder,
217                   VdpVideoSurface target,
218                   VdpPictureInfo const *picture_info,
219                   uint32_t bitstream_buffer_count,
220                   VdpBitstreamBuffer const *bitstream_buffers)
221{
222   vlVdpDecoder *vldecoder;
223   vlVdpSurface *vlsurf;
224
225   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding\n");
226
227   if (!(picture_info && bitstream_buffers))
228      return VDP_STATUS_INVALID_POINTER;
229
230   vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
231   if (!vldecoder)
232      return VDP_STATUS_INVALID_HANDLE;
233
234   vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);
235   if (!vlsurf)
236      return VDP_STATUS_INVALID_HANDLE;
237
238   if (vlsurf->device != vldecoder->device)
239      return VDP_STATUS_HANDLE_DEVICE_MISMATCH;
240
241   if (vlsurf->video_buffer->chroma_format != vldecoder->decoder->chroma_format)
242      // TODO: Recreate decoder with correct chroma
243      return VDP_STATUS_INVALID_CHROMA_TYPE;
244
245   // TODO: Right now only mpeg2 is supported.
246   switch (vldecoder->decoder->profile)   {
247   case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
248   case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
249      ++vldecoder->cur_buffer;
250      vldecoder->cur_buffer %= VL_NUM_DECODE_BUFFERS;
251      return vlVdpDecoderRenderMpeg2(vldecoder->decoder,
252                                     vldecoder->buffer[vldecoder->cur_buffer],
253                                     vlsurf->video_buffer,
254                                     (VdpPictureInfoMPEG1Or2 *)picture_info,
255                                     bitstream_buffer_count,bitstream_buffers);
256      break;
257
258   default:
259      return VDP_STATUS_INVALID_DECODER_PROFILE;
260   }
261}
262