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