1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2009 Younes Manton.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include <math.h>
29bf215546Sopenharmony_ci#include <assert.h>
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "util/u_memory.h"
32bf215546Sopenharmony_ci#include "util/u_sampler.h"
33bf215546Sopenharmony_ci#include "util/u_surface.h"
34bf215546Sopenharmony_ci#include "util/u_video.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "vl_mpeg12_decoder.h"
37bf215546Sopenharmony_ci#include "vl_defines.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#define SCALE_FACTOR_SNORM (32768.0f / 256.0f)
40bf215546Sopenharmony_ci#define SCALE_FACTOR_SSCALED (1.0f / 256.0f)
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistruct format_config {
43bf215546Sopenharmony_ci   enum pipe_format zscan_source_format;
44bf215546Sopenharmony_ci   enum pipe_format idct_source_format;
45bf215546Sopenharmony_ci   enum pipe_format mc_source_format;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   float idct_scale;
48bf215546Sopenharmony_ci   float mc_scale;
49bf215546Sopenharmony_ci};
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cistatic const struct format_config bitstream_format_config[] = {
52bf215546Sopenharmony_ci//   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },
53bf215546Sopenharmony_ci//   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },
54bf215546Sopenharmony_ci   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },
55bf215546Sopenharmony_ci   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }
56bf215546Sopenharmony_ci};
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_cistatic const unsigned num_bitstream_format_configs =
59bf215546Sopenharmony_ci   sizeof(bitstream_format_config) / sizeof(struct format_config);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_cistatic const struct format_config idct_format_config[] = {
62bf215546Sopenharmony_ci//   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },
63bf215546Sopenharmony_ci//   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },
64bf215546Sopenharmony_ci   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },
65bf215546Sopenharmony_ci   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }
66bf215546Sopenharmony_ci};
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_cistatic const unsigned num_idct_format_configs =
69bf215546Sopenharmony_ci   sizeof(idct_format_config) / sizeof(struct format_config);
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic const struct format_config mc_format_config[] = {
72bf215546Sopenharmony_ci   //{ PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SSCALED, 0.0f, SCALE_FACTOR_SSCALED },
73bf215546Sopenharmony_ci   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SNORM, 0.0f, SCALE_FACTOR_SNORM }
74bf215546Sopenharmony_ci};
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_cistatic const unsigned num_mc_format_configs =
77bf215546Sopenharmony_ci   sizeof(mc_format_config) / sizeof(struct format_config);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistatic const unsigned const_empty_block_mask_420[3][2][2] = {
80bf215546Sopenharmony_ci   { { 0x20, 0x10 },  { 0x08, 0x04 } },
81bf215546Sopenharmony_ci   { { 0x02, 0x02 },  { 0x02, 0x02 } },
82bf215546Sopenharmony_ci   { { 0x01, 0x01 },  { 0x01, 0x01 } }
83bf215546Sopenharmony_ci};
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_cistruct video_buffer_private
86bf215546Sopenharmony_ci{
87bf215546Sopenharmony_ci   struct list_head list;
88bf215546Sopenharmony_ci   struct pipe_video_buffer *video_buffer;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];
91bf215546Sopenharmony_ci   struct pipe_surface      *surfaces[VL_MAX_SURFACES];
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   struct vl_mpeg12_buffer *buffer;
94bf215546Sopenharmony_ci};
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_cistatic void
97bf215546Sopenharmony_civl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf);
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_cistatic void
100bf215546Sopenharmony_cidestroy_video_buffer_private(void *private)
101bf215546Sopenharmony_ci{
102bf215546Sopenharmony_ci   struct video_buffer_private *priv = private;
103bf215546Sopenharmony_ci   unsigned i;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   list_del(&priv->list);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
108bf215546Sopenharmony_ci      pipe_sampler_view_reference(&priv->sampler_view_planes[i], NULL);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   for (i = 0; i < VL_MAX_SURFACES; ++i)
111bf215546Sopenharmony_ci      pipe_surface_reference(&priv->surfaces[i], NULL);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   if (priv->buffer)
114bf215546Sopenharmony_ci      vl_mpeg12_destroy_buffer(priv->buffer);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   FREE(priv);
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_cistatic struct video_buffer_private *
120bf215546Sopenharmony_ciget_video_buffer_private(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *buf)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   struct pipe_context *pipe = dec->context;
123bf215546Sopenharmony_ci   struct video_buffer_private *priv;
124bf215546Sopenharmony_ci   struct pipe_sampler_view **sv;
125bf215546Sopenharmony_ci   struct pipe_surface **surf;
126bf215546Sopenharmony_ci   unsigned i;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   priv = vl_video_buffer_get_associated_data(buf, &dec->base);
129bf215546Sopenharmony_ci   if (priv)
130bf215546Sopenharmony_ci      return priv;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   priv = CALLOC_STRUCT(video_buffer_private);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   list_add(&priv->list, &dec->buffer_privates);
135bf215546Sopenharmony_ci   priv->video_buffer = buf;
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   sv = buf->get_sampler_view_planes(buf);
138bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
139bf215546Sopenharmony_ci      if (sv[i])
140bf215546Sopenharmony_ci         priv->sampler_view_planes[i] = pipe->create_sampler_view(pipe, sv[i]->texture, sv[i]);
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   surf = buf->get_surfaces(buf);
143bf215546Sopenharmony_ci   for (i = 0; i < VL_MAX_SURFACES; ++i)
144bf215546Sopenharmony_ci      if (surf[i])
145bf215546Sopenharmony_ci         priv->surfaces[i] = pipe->create_surface(pipe, surf[i]->texture, surf[i]);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   vl_video_buffer_set_associated_data(buf, &dec->base, priv, destroy_video_buffer_private);
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   return priv;
150bf215546Sopenharmony_ci}
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_cistatic void
153bf215546Sopenharmony_cifree_video_buffer_privates(struct vl_mpeg12_decoder *dec)
154bf215546Sopenharmony_ci{
155bf215546Sopenharmony_ci   struct video_buffer_private *priv, *next;
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci   LIST_FOR_EACH_ENTRY_SAFE(priv, next, &dec->buffer_privates, list) {
158bf215546Sopenharmony_ci      struct pipe_video_buffer *buf = priv->video_buffer;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci      vl_video_buffer_set_associated_data(buf, &dec->base, NULL, NULL);
161bf215546Sopenharmony_ci   }
162bf215546Sopenharmony_ci}
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_cistatic bool
165bf215546Sopenharmony_ciinit_zscan_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
166bf215546Sopenharmony_ci{
167bf215546Sopenharmony_ci   struct pipe_resource *res, res_tmpl;
168bf215546Sopenharmony_ci   struct pipe_sampler_view sv_tmpl;
169bf215546Sopenharmony_ci   struct pipe_surface **destination;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   unsigned i;
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   assert(dec && buffer);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   memset(&res_tmpl, 0, sizeof(res_tmpl));
176bf215546Sopenharmony_ci   res_tmpl.target = PIPE_TEXTURE_2D;
177bf215546Sopenharmony_ci   res_tmpl.format = dec->zscan_source_format;
178bf215546Sopenharmony_ci   res_tmpl.width0 = dec->blocks_per_line * VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;
179bf215546Sopenharmony_ci   res_tmpl.height0 = align(dec->num_blocks, dec->blocks_per_line) / dec->blocks_per_line;
180bf215546Sopenharmony_ci   res_tmpl.depth0 = 1;
181bf215546Sopenharmony_ci   res_tmpl.array_size = 1;
182bf215546Sopenharmony_ci   res_tmpl.usage = PIPE_USAGE_STREAM;
183bf215546Sopenharmony_ci   res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   res = dec->context->screen->resource_create(dec->context->screen, &res_tmpl);
186bf215546Sopenharmony_ci   if (!res)
187bf215546Sopenharmony_ci      goto error_source;
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci   memset(&sv_tmpl, 0, sizeof(sv_tmpl));
191bf215546Sopenharmony_ci   u_sampler_view_default_template(&sv_tmpl, res, res->format);
192bf215546Sopenharmony_ci   sv_tmpl.swizzle_r = sv_tmpl.swizzle_g = sv_tmpl.swizzle_b = sv_tmpl.swizzle_a = PIPE_SWIZZLE_X;
193bf215546Sopenharmony_ci   buffer->zscan_source = dec->context->create_sampler_view(dec->context, res, &sv_tmpl);
194bf215546Sopenharmony_ci   pipe_resource_reference(&res, NULL);
195bf215546Sopenharmony_ci   if (!buffer->zscan_source)
196bf215546Sopenharmony_ci      goto error_sampler;
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
199bf215546Sopenharmony_ci      destination = dec->idct_source->get_surfaces(dec->idct_source);
200bf215546Sopenharmony_ci   else
201bf215546Sopenharmony_ci      destination = dec->mc_source->get_surfaces(dec->mc_source);
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci   if (!destination)
204bf215546Sopenharmony_ci      goto error_surface;
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
207bf215546Sopenharmony_ci      if (!vl_zscan_init_buffer(i == 0 ? &dec->zscan_y : &dec->zscan_c,
208bf215546Sopenharmony_ci                                &buffer->zscan[i], buffer->zscan_source, destination[i]))
209bf215546Sopenharmony_ci         goto error_plane;
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   return true;
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_cierror_plane:
214bf215546Sopenharmony_ci   for (; i > 0; --i)
215bf215546Sopenharmony_ci      vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_cierror_surface:
218bf215546Sopenharmony_cierror_sampler:
219bf215546Sopenharmony_ci   pipe_sampler_view_reference(&buffer->zscan_source, NULL);
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_cierror_source:
222bf215546Sopenharmony_ci   return false;
223bf215546Sopenharmony_ci}
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_cistatic void
226bf215546Sopenharmony_cicleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer)
227bf215546Sopenharmony_ci{
228bf215546Sopenharmony_ci   unsigned i;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   assert(buffer);
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
233bf215546Sopenharmony_ci      vl_zscan_cleanup_buffer(&buffer->zscan[i]);
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci   pipe_sampler_view_reference(&buffer->zscan_source, NULL);
236bf215546Sopenharmony_ci}
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_cistatic bool
239bf215546Sopenharmony_ciinit_idct_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
240bf215546Sopenharmony_ci{
241bf215546Sopenharmony_ci   struct pipe_sampler_view **idct_source_sv, **mc_source_sv;
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   unsigned i;
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci   assert(dec && buffer);
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   idct_source_sv = dec->idct_source->get_sampler_view_planes(dec->idct_source);
248bf215546Sopenharmony_ci   if (!idct_source_sv)
249bf215546Sopenharmony_ci      goto error_source_sv;
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_ci   mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
252bf215546Sopenharmony_ci   if (!mc_source_sv)
253bf215546Sopenharmony_ci      goto error_mc_source_sv;
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   for (i = 0; i < 3; ++i)
256bf215546Sopenharmony_ci      if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,
257bf215546Sopenharmony_ci                               &buffer->idct[i], idct_source_sv[i],
258bf215546Sopenharmony_ci                               mc_source_sv[i]))
259bf215546Sopenharmony_ci         goto error_plane;
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_ci   return true;
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_cierror_plane:
264bf215546Sopenharmony_ci   for (; i > 0; --i)
265bf215546Sopenharmony_ci      vl_idct_cleanup_buffer(&buffer->idct[i - 1]);
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_cierror_mc_source_sv:
268bf215546Sopenharmony_cierror_source_sv:
269bf215546Sopenharmony_ci   return false;
270bf215546Sopenharmony_ci}
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_cistatic void
273bf215546Sopenharmony_cicleanup_idct_buffer(struct vl_mpeg12_buffer *buf)
274bf215546Sopenharmony_ci{
275bf215546Sopenharmony_ci   unsigned i;
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci   assert(buf);
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci   for (i = 0; i < 3; ++i)
280bf215546Sopenharmony_ci      vl_idct_cleanup_buffer(&buf->idct[i]);
281bf215546Sopenharmony_ci}
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_cistatic bool
284bf215546Sopenharmony_ciinit_mc_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buf)
285bf215546Sopenharmony_ci{
286bf215546Sopenharmony_ci   assert(dec && buf);
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci   if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0]))
289bf215546Sopenharmony_ci      goto error_mc_y;
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1]))
292bf215546Sopenharmony_ci      goto error_mc_cb;
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2]))
295bf215546Sopenharmony_ci      goto error_mc_cr;
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci   return true;
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_cierror_mc_cr:
300bf215546Sopenharmony_ci   vl_mc_cleanup_buffer(&buf->mc[1]);
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_cierror_mc_cb:
303bf215546Sopenharmony_ci   vl_mc_cleanup_buffer(&buf->mc[0]);
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_cierror_mc_y:
306bf215546Sopenharmony_ci   return false;
307bf215546Sopenharmony_ci}
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_cistatic void
310bf215546Sopenharmony_cicleanup_mc_buffer(struct vl_mpeg12_buffer *buf)
311bf215546Sopenharmony_ci{
312bf215546Sopenharmony_ci   unsigned i;
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   assert(buf);
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
317bf215546Sopenharmony_ci      vl_mc_cleanup_buffer(&buf->mc[i]);
318bf215546Sopenharmony_ci}
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_cistatic inline void
321bf215546Sopenharmony_ciMacroBlockTypeToPipeWeights(const struct pipe_mpeg12_macroblock *mb, unsigned weights[2])
322bf215546Sopenharmony_ci{
323bf215546Sopenharmony_ci   assert(mb);
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci   switch (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {
326bf215546Sopenharmony_ci   case PIPE_MPEG12_MB_TYPE_MOTION_FORWARD:
327bf215546Sopenharmony_ci      weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
328bf215546Sopenharmony_ci      weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
329bf215546Sopenharmony_ci      break;
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci   case (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD):
332bf215546Sopenharmony_ci      weights[0] = PIPE_VIDEO_MV_WEIGHT_HALF;
333bf215546Sopenharmony_ci      weights[1] = PIPE_VIDEO_MV_WEIGHT_HALF;
334bf215546Sopenharmony_ci      break;
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   case PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD:
337bf215546Sopenharmony_ci      weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
338bf215546Sopenharmony_ci      weights[1] = PIPE_VIDEO_MV_WEIGHT_MAX;
339bf215546Sopenharmony_ci      break;
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci   default:
342bf215546Sopenharmony_ci      if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) {
343bf215546Sopenharmony_ci         weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
344bf215546Sopenharmony_ci         weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
345bf215546Sopenharmony_ci      } else {
346bf215546Sopenharmony_ci         /* no motion vector, but also not intra mb ->
347bf215546Sopenharmony_ci            just copy the old frame content */
348bf215546Sopenharmony_ci         weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
349bf215546Sopenharmony_ci         weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
350bf215546Sopenharmony_ci      }
351bf215546Sopenharmony_ci      break;
352bf215546Sopenharmony_ci   }
353bf215546Sopenharmony_ci}
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_cistatic inline struct vl_motionvector
356bf215546Sopenharmony_ciMotionVectorToPipe(const struct pipe_mpeg12_macroblock *mb, unsigned vector,
357bf215546Sopenharmony_ci                   unsigned field_select_mask, unsigned weight)
358bf215546Sopenharmony_ci{
359bf215546Sopenharmony_ci   struct vl_motionvector mv;
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci   assert(mb);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci   if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {
364bf215546Sopenharmony_ci      switch (mb->macroblock_modes.bits.frame_motion_type) {
365bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_FRAME:
366bf215546Sopenharmony_ci         mv.top.x = mb->PMV[0][vector][0];
367bf215546Sopenharmony_ci         mv.top.y = mb->PMV[0][vector][1];
368bf215546Sopenharmony_ci         mv.top.field_select = PIPE_VIDEO_FRAME;
369bf215546Sopenharmony_ci         mv.top.weight = weight;
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci         mv.bottom.x = mb->PMV[0][vector][0];
372bf215546Sopenharmony_ci         mv.bottom.y = mb->PMV[0][vector][1];
373bf215546Sopenharmony_ci         mv.bottom.weight = weight;
374bf215546Sopenharmony_ci         mv.bottom.field_select = PIPE_VIDEO_FRAME;
375bf215546Sopenharmony_ci         break;
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci      case PIPE_MPEG12_MO_TYPE_FIELD:
378bf215546Sopenharmony_ci         mv.top.x = mb->PMV[0][vector][0];
379bf215546Sopenharmony_ci         mv.top.y = mb->PMV[0][vector][1];
380bf215546Sopenharmony_ci         mv.top.field_select = (mb->motion_vertical_field_select & field_select_mask) ?
381bf215546Sopenharmony_ci            PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
382bf215546Sopenharmony_ci         mv.top.weight = weight;
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci         mv.bottom.x = mb->PMV[1][vector][0];
385bf215546Sopenharmony_ci         mv.bottom.y = mb->PMV[1][vector][1];
386bf215546Sopenharmony_ci         mv.bottom.field_select = (mb->motion_vertical_field_select & (field_select_mask << 2)) ?
387bf215546Sopenharmony_ci            PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
388bf215546Sopenharmony_ci         mv.bottom.weight = weight;
389bf215546Sopenharmony_ci         break;
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci      default:
392bf215546Sopenharmony_ci         unreachable("TODO: Support DUALPRIME and 16x8");
393bf215546Sopenharmony_ci      }
394bf215546Sopenharmony_ci   } else {
395bf215546Sopenharmony_ci      mv.top.x = mv.top.y = 0;
396bf215546Sopenharmony_ci      mv.top.field_select = PIPE_VIDEO_FRAME;
397bf215546Sopenharmony_ci      mv.top.weight = weight;
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci      mv.bottom.x = mv.bottom.y = 0;
400bf215546Sopenharmony_ci      mv.bottom.field_select = PIPE_VIDEO_FRAME;
401bf215546Sopenharmony_ci      mv.bottom.weight = weight;
402bf215546Sopenharmony_ci   }
403bf215546Sopenharmony_ci   return mv;
404bf215546Sopenharmony_ci}
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_cistatic inline void
407bf215546Sopenharmony_ciUploadYcbcrBlocks(struct vl_mpeg12_decoder *dec,
408bf215546Sopenharmony_ci                  struct vl_mpeg12_buffer *buf,
409bf215546Sopenharmony_ci                  const struct pipe_mpeg12_macroblock *mb)
410bf215546Sopenharmony_ci{
411bf215546Sopenharmony_ci   unsigned intra;
412bf215546Sopenharmony_ci   unsigned tb, x, y, num_blocks = 0;
413bf215546Sopenharmony_ci
414bf215546Sopenharmony_ci   assert(dec && buf);
415bf215546Sopenharmony_ci   assert(mb);
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_ci   if (!mb->coded_block_pattern)
418bf215546Sopenharmony_ci      return;
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA ? 1 : 0;
421bf215546Sopenharmony_ci
422bf215546Sopenharmony_ci   for (y = 0; y < 2; ++y) {
423bf215546Sopenharmony_ci      for (x = 0; x < 2; ++x) {
424bf215546Sopenharmony_ci         if (mb->coded_block_pattern & const_empty_block_mask_420[0][y][x]) {
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci            struct vl_ycbcr_block *stream = buf->ycbcr_stream[0];
427bf215546Sopenharmony_ci            stream->x = mb->x * 2 + x;
428bf215546Sopenharmony_ci            stream->y = mb->y * 2 + y;
429bf215546Sopenharmony_ci            stream->intra = intra;
430bf215546Sopenharmony_ci            stream->coding = mb->macroblock_modes.bits.dct_type;
431bf215546Sopenharmony_ci            stream->block_num = buf->block_num++;
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci            buf->num_ycbcr_blocks[0]++;
434bf215546Sopenharmony_ci            buf->ycbcr_stream[0]++;
435bf215546Sopenharmony_ci
436bf215546Sopenharmony_ci            num_blocks++;
437bf215546Sopenharmony_ci         }
438bf215546Sopenharmony_ci      }
439bf215546Sopenharmony_ci   }
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci   /* TODO: Implement 422, 444 */
442bf215546Sopenharmony_ci   //assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ci   for (tb = 1; tb < 3; ++tb) {
445bf215546Sopenharmony_ci      if (mb->coded_block_pattern & const_empty_block_mask_420[tb][0][0]) {
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci         struct vl_ycbcr_block *stream = buf->ycbcr_stream[tb];
448bf215546Sopenharmony_ci         stream->x = mb->x;
449bf215546Sopenharmony_ci         stream->y = mb->y;
450bf215546Sopenharmony_ci         stream->intra = intra;
451bf215546Sopenharmony_ci         stream->coding = 0;
452bf215546Sopenharmony_ci         stream->block_num = buf->block_num++;
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci         buf->num_ycbcr_blocks[tb]++;
455bf215546Sopenharmony_ci         buf->ycbcr_stream[tb]++;
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_ci         num_blocks++;
458bf215546Sopenharmony_ci      }
459bf215546Sopenharmony_ci   }
460bf215546Sopenharmony_ci
461bf215546Sopenharmony_ci   memcpy(buf->texels, mb->blocks, 64 * sizeof(short) * num_blocks);
462bf215546Sopenharmony_ci   buf->texels += 64 * num_blocks;
463bf215546Sopenharmony_ci}
464bf215546Sopenharmony_ci
465bf215546Sopenharmony_cistatic void
466bf215546Sopenharmony_civl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf)
467bf215546Sopenharmony_ci{
468bf215546Sopenharmony_ci
469bf215546Sopenharmony_ci   assert(buf);
470bf215546Sopenharmony_ci
471bf215546Sopenharmony_ci   cleanup_zscan_buffer(buf);
472bf215546Sopenharmony_ci   cleanup_idct_buffer(buf);
473bf215546Sopenharmony_ci   cleanup_mc_buffer(buf);
474bf215546Sopenharmony_ci   vl_vb_cleanup(&buf->vertex_stream);
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci   FREE(buf);
477bf215546Sopenharmony_ci}
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_cistatic void
480bf215546Sopenharmony_civl_mpeg12_destroy(struct pipe_video_codec *decoder)
481bf215546Sopenharmony_ci{
482bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
483bf215546Sopenharmony_ci   unsigned i;
484bf215546Sopenharmony_ci
485bf215546Sopenharmony_ci   assert(decoder);
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci   free_video_buffer_privates(dec);
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_ci   /* Asserted in softpipe_delete_fs_state() for some reason */
490bf215546Sopenharmony_ci   dec->context->bind_vs_state(dec->context, NULL);
491bf215546Sopenharmony_ci   dec->context->bind_fs_state(dec->context, NULL);
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci   dec->context->delete_depth_stencil_alpha_state(dec->context, dec->dsa);
494bf215546Sopenharmony_ci   dec->context->delete_sampler_state(dec->context, dec->sampler_ycbcr);
495bf215546Sopenharmony_ci
496bf215546Sopenharmony_ci   vl_mc_cleanup(&dec->mc_y);
497bf215546Sopenharmony_ci   vl_mc_cleanup(&dec->mc_c);
498bf215546Sopenharmony_ci   dec->mc_source->destroy(dec->mc_source);
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
501bf215546Sopenharmony_ci      vl_idct_cleanup(&dec->idct_y);
502bf215546Sopenharmony_ci      vl_idct_cleanup(&dec->idct_c);
503bf215546Sopenharmony_ci      dec->idct_source->destroy(dec->idct_source);
504bf215546Sopenharmony_ci   }
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci   vl_zscan_cleanup(&dec->zscan_y);
507bf215546Sopenharmony_ci   vl_zscan_cleanup(&dec->zscan_c);
508bf215546Sopenharmony_ci
509bf215546Sopenharmony_ci   dec->context->delete_vertex_elements_state(dec->context, dec->ves_ycbcr);
510bf215546Sopenharmony_ci   dec->context->delete_vertex_elements_state(dec->context, dec->ves_mv);
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   pipe_resource_reference(&dec->quads.buffer.resource, NULL);
513bf215546Sopenharmony_ci   pipe_resource_reference(&dec->pos.buffer.resource, NULL);
514bf215546Sopenharmony_ci
515bf215546Sopenharmony_ci   pipe_sampler_view_reference(&dec->zscan_linear, NULL);
516bf215546Sopenharmony_ci   pipe_sampler_view_reference(&dec->zscan_normal, NULL);
517bf215546Sopenharmony_ci   pipe_sampler_view_reference(&dec->zscan_alternate, NULL);
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci   for (i = 0; i < 4; ++i)
520bf215546Sopenharmony_ci      if (dec->dec_buffers[i])
521bf215546Sopenharmony_ci         vl_mpeg12_destroy_buffer(dec->dec_buffers[i]);
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci   dec->context->destroy(dec->context);
524bf215546Sopenharmony_ci
525bf215546Sopenharmony_ci   FREE(dec);
526bf215546Sopenharmony_ci}
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_cistatic struct vl_mpeg12_buffer *
529bf215546Sopenharmony_civl_mpeg12_get_decode_buffer(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *target)
530bf215546Sopenharmony_ci{
531bf215546Sopenharmony_ci   struct video_buffer_private *priv;
532bf215546Sopenharmony_ci   struct vl_mpeg12_buffer *buffer;
533bf215546Sopenharmony_ci
534bf215546Sopenharmony_ci   assert(dec);
535bf215546Sopenharmony_ci
536bf215546Sopenharmony_ci   priv = get_video_buffer_private(dec, target);
537bf215546Sopenharmony_ci   if (priv->buffer)
538bf215546Sopenharmony_ci      return priv->buffer;
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci   buffer = dec->dec_buffers[dec->current_buffer];
541bf215546Sopenharmony_ci   if (buffer)
542bf215546Sopenharmony_ci      return buffer;
543bf215546Sopenharmony_ci
544bf215546Sopenharmony_ci   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
545bf215546Sopenharmony_ci   if (!buffer)
546bf215546Sopenharmony_ci      return NULL;
547bf215546Sopenharmony_ci
548bf215546Sopenharmony_ci   if (!vl_vb_init(&buffer->vertex_stream, dec->context,
549bf215546Sopenharmony_ci                   dec->base.width / VL_MACROBLOCK_WIDTH,
550bf215546Sopenharmony_ci                   dec->base.height / VL_MACROBLOCK_HEIGHT))
551bf215546Sopenharmony_ci      goto error_vertex_buffer;
552bf215546Sopenharmony_ci
553bf215546Sopenharmony_ci   if (!init_mc_buffer(dec, buffer))
554bf215546Sopenharmony_ci      goto error_mc;
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_ci   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
557bf215546Sopenharmony_ci      if (!init_idct_buffer(dec, buffer))
558bf215546Sopenharmony_ci         goto error_idct;
559bf215546Sopenharmony_ci
560bf215546Sopenharmony_ci   if (!init_zscan_buffer(dec, buffer))
561bf215546Sopenharmony_ci      goto error_zscan;
562bf215546Sopenharmony_ci
563bf215546Sopenharmony_ci   if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
564bf215546Sopenharmony_ci      vl_mpg12_bs_init(&buffer->bs, &dec->base);
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci   if (dec->base.expect_chunked_decode)
567bf215546Sopenharmony_ci      priv->buffer = buffer;
568bf215546Sopenharmony_ci   else
569bf215546Sopenharmony_ci      dec->dec_buffers[dec->current_buffer] = buffer;
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci   return buffer;
572bf215546Sopenharmony_ci
573bf215546Sopenharmony_cierror_zscan:
574bf215546Sopenharmony_ci   cleanup_idct_buffer(buffer);
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_cierror_idct:
577bf215546Sopenharmony_ci   cleanup_mc_buffer(buffer);
578bf215546Sopenharmony_ci
579bf215546Sopenharmony_cierror_mc:
580bf215546Sopenharmony_ci   vl_vb_cleanup(&buffer->vertex_stream);
581bf215546Sopenharmony_ci
582bf215546Sopenharmony_cierror_vertex_buffer:
583bf215546Sopenharmony_ci   FREE(buffer);
584bf215546Sopenharmony_ci   return NULL;
585bf215546Sopenharmony_ci}
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_cistatic void
588bf215546Sopenharmony_civl_mpeg12_begin_frame(struct pipe_video_codec *decoder,
589bf215546Sopenharmony_ci                      struct pipe_video_buffer *target,
590bf215546Sopenharmony_ci                      struct pipe_picture_desc *picture)
591bf215546Sopenharmony_ci{
592bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
593bf215546Sopenharmony_ci   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
594bf215546Sopenharmony_ci   struct vl_mpeg12_buffer *buf;
595bf215546Sopenharmony_ci
596bf215546Sopenharmony_ci   struct pipe_resource *tex;
597bf215546Sopenharmony_ci   struct pipe_box rect = { 0, 0, 0, 1, 1, 1 };
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_ci   uint8_t intra_matrix[64];
600bf215546Sopenharmony_ci   uint8_t non_intra_matrix[64];
601bf215546Sopenharmony_ci
602bf215546Sopenharmony_ci   unsigned i;
603bf215546Sopenharmony_ci
604bf215546Sopenharmony_ci   assert(dec && target && picture);
605bf215546Sopenharmony_ci
606bf215546Sopenharmony_ci   buf = vl_mpeg12_get_decode_buffer(dec, target);
607bf215546Sopenharmony_ci   assert(buf);
608bf215546Sopenharmony_ci
609bf215546Sopenharmony_ci   if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
610bf215546Sopenharmony_ci      memcpy(intra_matrix, desc->intra_matrix, sizeof(intra_matrix));
611bf215546Sopenharmony_ci      memcpy(non_intra_matrix, desc->non_intra_matrix, sizeof(non_intra_matrix));
612bf215546Sopenharmony_ci      intra_matrix[0] = 1 << (7 - desc->intra_dc_precision);
613bf215546Sopenharmony_ci   } else {
614bf215546Sopenharmony_ci      memset(intra_matrix, 0x10, sizeof(intra_matrix));
615bf215546Sopenharmony_ci      memset(non_intra_matrix, 0x10, sizeof(non_intra_matrix));
616bf215546Sopenharmony_ci   }
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
619bf215546Sopenharmony_ci      struct vl_zscan *zscan = i == 0 ? &dec->zscan_y : &dec->zscan_c;
620bf215546Sopenharmony_ci      vl_zscan_upload_quant(zscan, &buf->zscan[i], intra_matrix, true);
621bf215546Sopenharmony_ci      vl_zscan_upload_quant(zscan, &buf->zscan[i], non_intra_matrix, false);
622bf215546Sopenharmony_ci   }
623bf215546Sopenharmony_ci
624bf215546Sopenharmony_ci   vl_vb_map(&buf->vertex_stream, dec->context);
625bf215546Sopenharmony_ci
626bf215546Sopenharmony_ci   tex = buf->zscan_source->texture;
627bf215546Sopenharmony_ci   rect.width = tex->width0;
628bf215546Sopenharmony_ci   rect.height = tex->height0;
629bf215546Sopenharmony_ci
630bf215546Sopenharmony_ci   buf->texels =
631bf215546Sopenharmony_ci      dec->context->texture_map(dec->context, tex, 0,
632bf215546Sopenharmony_ci                                 PIPE_MAP_WRITE |
633bf215546Sopenharmony_ci                                 PIPE_MAP_DISCARD_RANGE,
634bf215546Sopenharmony_ci                                 &rect, &buf->tex_transfer);
635bf215546Sopenharmony_ci
636bf215546Sopenharmony_ci   buf->block_num = 0;
637bf215546Sopenharmony_ci
638bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
639bf215546Sopenharmony_ci      buf->ycbcr_stream[i] = vl_vb_get_ycbcr_stream(&buf->vertex_stream, i);
640bf215546Sopenharmony_ci      buf->num_ycbcr_blocks[i] = 0;
641bf215546Sopenharmony_ci   }
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_ci   for (i = 0; i < VL_MAX_REF_FRAMES; ++i)
644bf215546Sopenharmony_ci      buf->mv_stream[i] = vl_vb_get_mv_stream(&buf->vertex_stream, i);
645bf215546Sopenharmony_ci
646bf215546Sopenharmony_ci   if (dec->base.entrypoint >= PIPE_VIDEO_ENTRYPOINT_IDCT) {
647bf215546Sopenharmony_ci      for (i = 0; i < VL_NUM_COMPONENTS; ++i)
648bf215546Sopenharmony_ci         vl_zscan_set_layout(&buf->zscan[i], dec->zscan_linear);
649bf215546Sopenharmony_ci   }
650bf215546Sopenharmony_ci}
651bf215546Sopenharmony_ci
652bf215546Sopenharmony_cistatic void
653bf215546Sopenharmony_civl_mpeg12_decode_macroblock(struct pipe_video_codec *decoder,
654bf215546Sopenharmony_ci                            struct pipe_video_buffer *target,
655bf215546Sopenharmony_ci                            struct pipe_picture_desc *picture,
656bf215546Sopenharmony_ci                            const struct pipe_macroblock *macroblocks,
657bf215546Sopenharmony_ci                            unsigned num_macroblocks)
658bf215546Sopenharmony_ci{
659bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
660bf215546Sopenharmony_ci   const struct pipe_mpeg12_macroblock *mb = (const struct pipe_mpeg12_macroblock *)macroblocks;
661bf215546Sopenharmony_ci   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
662bf215546Sopenharmony_ci   struct vl_mpeg12_buffer *buf;
663bf215546Sopenharmony_ci
664bf215546Sopenharmony_ci   unsigned i, j, mv_weights[2];
665bf215546Sopenharmony_ci
666bf215546Sopenharmony_ci   assert(dec && target && picture);
667bf215546Sopenharmony_ci   assert(macroblocks && macroblocks->codec == PIPE_VIDEO_FORMAT_MPEG12);
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci   buf = vl_mpeg12_get_decode_buffer(dec, target);
670bf215546Sopenharmony_ci   assert(buf);
671bf215546Sopenharmony_ci
672bf215546Sopenharmony_ci   for (; num_macroblocks > 0; --num_macroblocks) {
673bf215546Sopenharmony_ci      unsigned mb_addr = mb->y * dec->width_in_macroblocks + mb->x;
674bf215546Sopenharmony_ci
675bf215546Sopenharmony_ci      if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_PATTERN | PIPE_MPEG12_MB_TYPE_INTRA))
676bf215546Sopenharmony_ci         UploadYcbcrBlocks(dec, buf, mb);
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci      MacroBlockTypeToPipeWeights(mb, mv_weights);
679bf215546Sopenharmony_ci
680bf215546Sopenharmony_ci      for (i = 0; i < 2; ++i) {
681bf215546Sopenharmony_ci          if (!desc->ref[i]) continue;
682bf215546Sopenharmony_ci
683bf215546Sopenharmony_ci         buf->mv_stream[i][mb_addr] = MotionVectorToPipe
684bf215546Sopenharmony_ci         (
685bf215546Sopenharmony_ci            mb, i,
686bf215546Sopenharmony_ci            i ? PIPE_MPEG12_FS_FIRST_BACKWARD : PIPE_MPEG12_FS_FIRST_FORWARD,
687bf215546Sopenharmony_ci            mv_weights[i]
688bf215546Sopenharmony_ci         );
689bf215546Sopenharmony_ci      }
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci      /* see section 7.6.6 of the spec */
692bf215546Sopenharmony_ci      if (mb->num_skipped_macroblocks > 0) {
693bf215546Sopenharmony_ci         struct vl_motionvector skipped_mv[2];
694bf215546Sopenharmony_ci
695bf215546Sopenharmony_ci         if (desc->ref[0] && !desc->ref[1]) {
696bf215546Sopenharmony_ci            skipped_mv[0].top.x = skipped_mv[0].top.y = 0;
697bf215546Sopenharmony_ci            skipped_mv[0].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
698bf215546Sopenharmony_ci         } else {
699bf215546Sopenharmony_ci           skipped_mv[0] = buf->mv_stream[0][mb_addr];
700bf215546Sopenharmony_ci           skipped_mv[1] = buf->mv_stream[1][mb_addr];
701bf215546Sopenharmony_ci         }
702bf215546Sopenharmony_ci         skipped_mv[0].top.field_select = PIPE_VIDEO_FRAME;
703bf215546Sopenharmony_ci         skipped_mv[1].top.field_select = PIPE_VIDEO_FRAME;
704bf215546Sopenharmony_ci
705bf215546Sopenharmony_ci         skipped_mv[0].bottom = skipped_mv[0].top;
706bf215546Sopenharmony_ci         skipped_mv[1].bottom = skipped_mv[1].top;
707bf215546Sopenharmony_ci
708bf215546Sopenharmony_ci         ++mb_addr;
709bf215546Sopenharmony_ci         for (i = 0; i < mb->num_skipped_macroblocks; ++i, ++mb_addr) {
710bf215546Sopenharmony_ci            for (j = 0; j < 2; ++j) {
711bf215546Sopenharmony_ci               if (!desc->ref[j]) continue;
712bf215546Sopenharmony_ci               buf->mv_stream[j][mb_addr] = skipped_mv[j];
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci            }
715bf215546Sopenharmony_ci         }
716bf215546Sopenharmony_ci      }
717bf215546Sopenharmony_ci
718bf215546Sopenharmony_ci      ++mb;
719bf215546Sopenharmony_ci   }
720bf215546Sopenharmony_ci}
721bf215546Sopenharmony_ci
722bf215546Sopenharmony_cistatic void
723bf215546Sopenharmony_civl_mpeg12_decode_bitstream(struct pipe_video_codec *decoder,
724bf215546Sopenharmony_ci                           struct pipe_video_buffer *target,
725bf215546Sopenharmony_ci                           struct pipe_picture_desc *picture,
726bf215546Sopenharmony_ci                           unsigned num_buffers,
727bf215546Sopenharmony_ci                           const void * const *buffers,
728bf215546Sopenharmony_ci                           const unsigned *sizes)
729bf215546Sopenharmony_ci{
730bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
731bf215546Sopenharmony_ci   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
732bf215546Sopenharmony_ci   struct vl_mpeg12_buffer *buf;
733bf215546Sopenharmony_ci
734bf215546Sopenharmony_ci   unsigned i;
735bf215546Sopenharmony_ci
736bf215546Sopenharmony_ci   assert(dec && target && picture);
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci   buf = vl_mpeg12_get_decode_buffer(dec, target);
739bf215546Sopenharmony_ci   assert(buf);
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
742bf215546Sopenharmony_ci      vl_zscan_set_layout(&buf->zscan[i], desc->alternate_scan ?
743bf215546Sopenharmony_ci                          dec->zscan_alternate : dec->zscan_normal);
744bf215546Sopenharmony_ci
745bf215546Sopenharmony_ci   vl_mpg12_bs_decode(&buf->bs, target, desc, num_buffers, buffers, sizes);
746bf215546Sopenharmony_ci}
747bf215546Sopenharmony_ci
748bf215546Sopenharmony_cistatic void
749bf215546Sopenharmony_civl_mpeg12_end_frame(struct pipe_video_codec *decoder,
750bf215546Sopenharmony_ci                    struct pipe_video_buffer *target,
751bf215546Sopenharmony_ci                    struct pipe_picture_desc *picture)
752bf215546Sopenharmony_ci{
753bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
754bf215546Sopenharmony_ci   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
755bf215546Sopenharmony_ci   struct pipe_sampler_view **ref_frames[2];
756bf215546Sopenharmony_ci   struct pipe_sampler_view **mc_source_sv;
757bf215546Sopenharmony_ci   struct pipe_surface **target_surfaces;
758bf215546Sopenharmony_ci   struct pipe_vertex_buffer vb[3];
759bf215546Sopenharmony_ci   struct vl_mpeg12_buffer *buf;
760bf215546Sopenharmony_ci
761bf215546Sopenharmony_ci   const unsigned *plane_order;
762bf215546Sopenharmony_ci   unsigned i, j, component;
763bf215546Sopenharmony_ci   unsigned nr_components;
764bf215546Sopenharmony_ci
765bf215546Sopenharmony_ci   assert(dec && target && picture);
766bf215546Sopenharmony_ci   assert(!target->interlaced);
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_ci   buf = vl_mpeg12_get_decode_buffer(dec, target);
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_ci   vl_vb_unmap(&buf->vertex_stream, dec->context);
771bf215546Sopenharmony_ci
772bf215546Sopenharmony_ci   if (buf->tex_transfer)
773bf215546Sopenharmony_ci      dec->context->texture_unmap(dec->context, buf->tex_transfer);
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci   vb[0] = dec->quads;
776bf215546Sopenharmony_ci   vb[1] = dec->pos;
777bf215546Sopenharmony_ci
778bf215546Sopenharmony_ci   target_surfaces = get_video_buffer_private(dec, target)->surfaces;
779bf215546Sopenharmony_ci
780bf215546Sopenharmony_ci   for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
781bf215546Sopenharmony_ci      if (desc->ref[i])
782bf215546Sopenharmony_ci         ref_frames[i] = get_video_buffer_private(dec, desc->ref[i])->sampler_view_planes;
783bf215546Sopenharmony_ci      else
784bf215546Sopenharmony_ci         ref_frames[i] = NULL;
785bf215546Sopenharmony_ci   }
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci   dec->context->bind_vertex_elements_state(dec->context, dec->ves_mv);
788bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
789bf215546Sopenharmony_ci      if (!target_surfaces[i]) continue;
790bf215546Sopenharmony_ci
791bf215546Sopenharmony_ci      vl_mc_set_surface(&buf->mc[i], target_surfaces[i]);
792bf215546Sopenharmony_ci
793bf215546Sopenharmony_ci      for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {
794bf215546Sopenharmony_ci         if (!ref_frames[j] || !ref_frames[j][i]) continue;
795bf215546Sopenharmony_ci
796bf215546Sopenharmony_ci         vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);
797bf215546Sopenharmony_ci         dec->context->set_vertex_buffers(dec->context, 0, 3, 0, false, vb);
798bf215546Sopenharmony_ci
799bf215546Sopenharmony_ci         vl_mc_render_ref(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], ref_frames[j][i]);
800bf215546Sopenharmony_ci      }
801bf215546Sopenharmony_ci   }
802bf215546Sopenharmony_ci
803bf215546Sopenharmony_ci   dec->context->bind_vertex_elements_state(dec->context, dec->ves_ycbcr);
804bf215546Sopenharmony_ci   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
805bf215546Sopenharmony_ci      if (!buf->num_ycbcr_blocks[i]) continue;
806bf215546Sopenharmony_ci
807bf215546Sopenharmony_ci      vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
808bf215546Sopenharmony_ci      dec->context->set_vertex_buffers(dec->context, 0, 2, 0, false, vb);
809bf215546Sopenharmony_ci
810bf215546Sopenharmony_ci      vl_zscan_render(i ? &dec->zscan_c : & dec->zscan_y, &buf->zscan[i] , buf->num_ycbcr_blocks[i]);
811bf215546Sopenharmony_ci
812bf215546Sopenharmony_ci      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
813bf215546Sopenharmony_ci         vl_idct_flush(i ? &dec->idct_c : &dec->idct_y, &buf->idct[i], buf->num_ycbcr_blocks[i]);
814bf215546Sopenharmony_ci   }
815bf215546Sopenharmony_ci
816bf215546Sopenharmony_ci   plane_order = vl_video_buffer_plane_order(target->buffer_format);
817bf215546Sopenharmony_ci   mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
818bf215546Sopenharmony_ci   for (i = 0, component = 0; component < VL_NUM_COMPONENTS; ++i) {
819bf215546Sopenharmony_ci      if (!target_surfaces[i]) continue;
820bf215546Sopenharmony_ci
821bf215546Sopenharmony_ci      nr_components = util_format_get_nr_components(target_surfaces[i]->texture->format);
822bf215546Sopenharmony_ci      for (j = 0; j < nr_components; ++j, ++component) {
823bf215546Sopenharmony_ci         unsigned plane = plane_order[component];
824bf215546Sopenharmony_ci         if (!buf->num_ycbcr_blocks[plane]) continue;
825bf215546Sopenharmony_ci
826bf215546Sopenharmony_ci         vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, plane);
827bf215546Sopenharmony_ci         dec->context->set_vertex_buffers(dec->context, 0, 2, 0, false, vb);
828bf215546Sopenharmony_ci
829bf215546Sopenharmony_ci         if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
830bf215546Sopenharmony_ci            vl_idct_prepare_stage2(i ? &dec->idct_c : &dec->idct_y, &buf->idct[plane]);
831bf215546Sopenharmony_ci         else {
832bf215546Sopenharmony_ci            dec->context->set_sampler_views(dec->context,
833bf215546Sopenharmony_ci                                            PIPE_SHADER_FRAGMENT, 0, 1, 0, false,
834bf215546Sopenharmony_ci                                            &mc_source_sv[plane]);
835bf215546Sopenharmony_ci            dec->context->bind_sampler_states(dec->context,
836bf215546Sopenharmony_ci                                              PIPE_SHADER_FRAGMENT,
837bf215546Sopenharmony_ci                                              0, 1, &dec->sampler_ycbcr);
838bf215546Sopenharmony_ci         }
839bf215546Sopenharmony_ci         vl_mc_render_ycbcr(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], j, buf->num_ycbcr_blocks[plane]);
840bf215546Sopenharmony_ci      }
841bf215546Sopenharmony_ci   }
842bf215546Sopenharmony_ci   dec->context->flush(dec->context, NULL, 0);
843bf215546Sopenharmony_ci   ++dec->current_buffer;
844bf215546Sopenharmony_ci   dec->current_buffer %= 4;
845bf215546Sopenharmony_ci}
846bf215546Sopenharmony_ci
847bf215546Sopenharmony_cistatic void
848bf215546Sopenharmony_civl_mpeg12_flush(struct pipe_video_codec *decoder)
849bf215546Sopenharmony_ci{
850bf215546Sopenharmony_ci   assert(decoder);
851bf215546Sopenharmony_ci
852bf215546Sopenharmony_ci   //Noop, for shaders it is much faster to flush everything in end_frame
853bf215546Sopenharmony_ci}
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_cistatic bool
856bf215546Sopenharmony_ciinit_pipe_state(struct vl_mpeg12_decoder *dec)
857bf215546Sopenharmony_ci{
858bf215546Sopenharmony_ci   struct pipe_depth_stencil_alpha_state dsa;
859bf215546Sopenharmony_ci   struct pipe_sampler_state sampler;
860bf215546Sopenharmony_ci   unsigned i;
861bf215546Sopenharmony_ci
862bf215546Sopenharmony_ci   assert(dec);
863bf215546Sopenharmony_ci
864bf215546Sopenharmony_ci   memset(&dsa, 0, sizeof dsa);
865bf215546Sopenharmony_ci   dsa.depth_enabled = 0;
866bf215546Sopenharmony_ci   dsa.depth_writemask = 0;
867bf215546Sopenharmony_ci   dsa.depth_func = PIPE_FUNC_ALWAYS;
868bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i) {
869bf215546Sopenharmony_ci      dsa.stencil[i].enabled = 0;
870bf215546Sopenharmony_ci      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
871bf215546Sopenharmony_ci      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
872bf215546Sopenharmony_ci      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
873bf215546Sopenharmony_ci      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
874bf215546Sopenharmony_ci      dsa.stencil[i].valuemask = 0;
875bf215546Sopenharmony_ci      dsa.stencil[i].writemask = 0;
876bf215546Sopenharmony_ci   }
877bf215546Sopenharmony_ci   dsa.alpha_enabled = 0;
878bf215546Sopenharmony_ci   dsa.alpha_func = PIPE_FUNC_ALWAYS;
879bf215546Sopenharmony_ci   dsa.alpha_ref_value = 0;
880bf215546Sopenharmony_ci   dec->dsa = dec->context->create_depth_stencil_alpha_state(dec->context, &dsa);
881bf215546Sopenharmony_ci   dec->context->bind_depth_stencil_alpha_state(dec->context, dec->dsa);
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci   memset(&sampler, 0, sizeof(sampler));
884bf215546Sopenharmony_ci   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
885bf215546Sopenharmony_ci   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
886bf215546Sopenharmony_ci   sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
887bf215546Sopenharmony_ci   sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
888bf215546Sopenharmony_ci   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
889bf215546Sopenharmony_ci   sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
890bf215546Sopenharmony_ci   sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
891bf215546Sopenharmony_ci   sampler.compare_func = PIPE_FUNC_ALWAYS;
892bf215546Sopenharmony_ci   sampler.normalized_coords = 1;
893bf215546Sopenharmony_ci   dec->sampler_ycbcr = dec->context->create_sampler_state(dec->context, &sampler);
894bf215546Sopenharmony_ci   if (!dec->sampler_ycbcr)
895bf215546Sopenharmony_ci      return false;
896bf215546Sopenharmony_ci
897bf215546Sopenharmony_ci   return true;
898bf215546Sopenharmony_ci}
899bf215546Sopenharmony_ci
900bf215546Sopenharmony_cistatic const struct format_config*
901bf215546Sopenharmony_cifind_format_config(struct vl_mpeg12_decoder *dec, const struct format_config configs[], unsigned num_configs)
902bf215546Sopenharmony_ci{
903bf215546Sopenharmony_ci   struct pipe_screen *screen;
904bf215546Sopenharmony_ci   unsigned i;
905bf215546Sopenharmony_ci
906bf215546Sopenharmony_ci   assert(dec);
907bf215546Sopenharmony_ci
908bf215546Sopenharmony_ci   screen = dec->context->screen;
909bf215546Sopenharmony_ci
910bf215546Sopenharmony_ci   for (i = 0; i < num_configs; ++i) {
911bf215546Sopenharmony_ci      if (!screen->is_format_supported(screen, configs[i].zscan_source_format, PIPE_TEXTURE_2D,
912bf215546Sopenharmony_ci                                       1, 1, PIPE_BIND_SAMPLER_VIEW))
913bf215546Sopenharmony_ci         continue;
914bf215546Sopenharmony_ci
915bf215546Sopenharmony_ci      if (configs[i].idct_source_format != PIPE_FORMAT_NONE) {
916bf215546Sopenharmony_ci         if (!screen->is_format_supported(screen, configs[i].idct_source_format, PIPE_TEXTURE_2D,
917bf215546Sopenharmony_ci                                          1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
918bf215546Sopenharmony_ci            continue;
919bf215546Sopenharmony_ci
920bf215546Sopenharmony_ci         if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_3D,
921bf215546Sopenharmony_ci                                          1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
922bf215546Sopenharmony_ci            continue;
923bf215546Sopenharmony_ci      } else {
924bf215546Sopenharmony_ci         if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_2D,
925bf215546Sopenharmony_ci                                          1, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
926bf215546Sopenharmony_ci            continue;
927bf215546Sopenharmony_ci      }
928bf215546Sopenharmony_ci      return &configs[i];
929bf215546Sopenharmony_ci   }
930bf215546Sopenharmony_ci
931bf215546Sopenharmony_ci   return NULL;
932bf215546Sopenharmony_ci}
933bf215546Sopenharmony_ci
934bf215546Sopenharmony_cistatic bool
935bf215546Sopenharmony_ciinit_zscan(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
936bf215546Sopenharmony_ci{
937bf215546Sopenharmony_ci   unsigned num_channels;
938bf215546Sopenharmony_ci
939bf215546Sopenharmony_ci   assert(dec);
940bf215546Sopenharmony_ci
941bf215546Sopenharmony_ci   dec->zscan_source_format = format_config->zscan_source_format;
942bf215546Sopenharmony_ci   dec->zscan_linear = vl_zscan_layout(dec->context, vl_zscan_linear, dec->blocks_per_line);
943bf215546Sopenharmony_ci   dec->zscan_normal = vl_zscan_layout(dec->context, vl_zscan_normal, dec->blocks_per_line);
944bf215546Sopenharmony_ci   dec->zscan_alternate = vl_zscan_layout(dec->context, vl_zscan_alternate, dec->blocks_per_line);
945bf215546Sopenharmony_ci
946bf215546Sopenharmony_ci   num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
947bf215546Sopenharmony_ci
948bf215546Sopenharmony_ci   if (!vl_zscan_init(&dec->zscan_y, dec->context, dec->base.width, dec->base.height,
949bf215546Sopenharmony_ci                      dec->blocks_per_line, dec->num_blocks, num_channels))
950bf215546Sopenharmony_ci      return false;
951bf215546Sopenharmony_ci
952bf215546Sopenharmony_ci   if (!vl_zscan_init(&dec->zscan_c, dec->context, dec->chroma_width, dec->chroma_height,
953bf215546Sopenharmony_ci                      dec->blocks_per_line, dec->num_blocks, num_channels))
954bf215546Sopenharmony_ci      return false;
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci   return true;
957bf215546Sopenharmony_ci}
958bf215546Sopenharmony_ci
959bf215546Sopenharmony_cistatic bool
960bf215546Sopenharmony_ciinit_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
961bf215546Sopenharmony_ci{
962bf215546Sopenharmony_ci   unsigned nr_of_idct_render_targets, max_inst;
963bf215546Sopenharmony_ci   enum pipe_format formats[3];
964bf215546Sopenharmony_ci   struct pipe_video_buffer templat;
965bf215546Sopenharmony_ci
966bf215546Sopenharmony_ci   struct pipe_sampler_view *matrix = NULL;
967bf215546Sopenharmony_ci
968bf215546Sopenharmony_ci   nr_of_idct_render_targets = dec->context->screen->get_param
969bf215546Sopenharmony_ci   (
970bf215546Sopenharmony_ci      dec->context->screen, PIPE_CAP_MAX_RENDER_TARGETS
971bf215546Sopenharmony_ci   );
972bf215546Sopenharmony_ci
973bf215546Sopenharmony_ci   max_inst = dec->context->screen->get_shader_param
974bf215546Sopenharmony_ci   (
975bf215546Sopenharmony_ci      dec->context->screen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_MAX_INSTRUCTIONS
976bf215546Sopenharmony_ci   );
977bf215546Sopenharmony_ci
978bf215546Sopenharmony_ci   // Just assume we need 32 inst per render target, not 100% true, but should work in most cases
979bf215546Sopenharmony_ci   if (nr_of_idct_render_targets >= 4 && max_inst >= 32*4)
980bf215546Sopenharmony_ci      // more than 4 render targets usually doesn't makes any seens
981bf215546Sopenharmony_ci      nr_of_idct_render_targets = 4;
982bf215546Sopenharmony_ci   else
983bf215546Sopenharmony_ci      nr_of_idct_render_targets = 1;
984bf215546Sopenharmony_ci
985bf215546Sopenharmony_ci   formats[0] = formats[1] = formats[2] = format_config->idct_source_format;
986bf215546Sopenharmony_ci   memset(&templat, 0, sizeof(templat));
987bf215546Sopenharmony_ci   templat.width = dec->base.width / 4;
988bf215546Sopenharmony_ci   templat.height = dec->base.height;
989bf215546Sopenharmony_ci   dec->idct_source = vl_video_buffer_create_ex
990bf215546Sopenharmony_ci   (
991bf215546Sopenharmony_ci      dec->context, &templat,
992bf215546Sopenharmony_ci      formats, 1, 1, PIPE_USAGE_DEFAULT,
993bf215546Sopenharmony_ci      PIPE_VIDEO_CHROMA_FORMAT_420
994bf215546Sopenharmony_ci   );
995bf215546Sopenharmony_ci
996bf215546Sopenharmony_ci   if (!dec->idct_source)
997bf215546Sopenharmony_ci      goto error_idct_source;
998bf215546Sopenharmony_ci
999bf215546Sopenharmony_ci   formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
1000bf215546Sopenharmony_ci   memset(&templat, 0, sizeof(templat));
1001bf215546Sopenharmony_ci   templat.width = dec->base.width / nr_of_idct_render_targets;
1002bf215546Sopenharmony_ci   templat.height = dec->base.height / 4;
1003bf215546Sopenharmony_ci   dec->mc_source = vl_video_buffer_create_ex
1004bf215546Sopenharmony_ci   (
1005bf215546Sopenharmony_ci      dec->context, &templat,
1006bf215546Sopenharmony_ci      formats, nr_of_idct_render_targets, 1, PIPE_USAGE_DEFAULT,
1007bf215546Sopenharmony_ci      PIPE_VIDEO_CHROMA_FORMAT_420
1008bf215546Sopenharmony_ci   );
1009bf215546Sopenharmony_ci
1010bf215546Sopenharmony_ci   if (!dec->mc_source)
1011bf215546Sopenharmony_ci      goto error_mc_source;
1012bf215546Sopenharmony_ci
1013bf215546Sopenharmony_ci   if (!(matrix = vl_idct_upload_matrix(dec->context, format_config->idct_scale)))
1014bf215546Sopenharmony_ci      goto error_matrix;
1015bf215546Sopenharmony_ci
1016bf215546Sopenharmony_ci   if (!vl_idct_init(&dec->idct_y, dec->context, dec->base.width, dec->base.height,
1017bf215546Sopenharmony_ci                     nr_of_idct_render_targets, matrix, matrix))
1018bf215546Sopenharmony_ci      goto error_y;
1019bf215546Sopenharmony_ci
1020bf215546Sopenharmony_ci   if(!vl_idct_init(&dec->idct_c, dec->context, dec->chroma_width, dec->chroma_height,
1021bf215546Sopenharmony_ci                    nr_of_idct_render_targets, matrix, matrix))
1022bf215546Sopenharmony_ci      goto error_c;
1023bf215546Sopenharmony_ci
1024bf215546Sopenharmony_ci   pipe_sampler_view_reference(&matrix, NULL);
1025bf215546Sopenharmony_ci
1026bf215546Sopenharmony_ci   return true;
1027bf215546Sopenharmony_ci
1028bf215546Sopenharmony_cierror_c:
1029bf215546Sopenharmony_ci   vl_idct_cleanup(&dec->idct_y);
1030bf215546Sopenharmony_ci
1031bf215546Sopenharmony_cierror_y:
1032bf215546Sopenharmony_ci   pipe_sampler_view_reference(&matrix, NULL);
1033bf215546Sopenharmony_ci
1034bf215546Sopenharmony_cierror_matrix:
1035bf215546Sopenharmony_ci   dec->mc_source->destroy(dec->mc_source);
1036bf215546Sopenharmony_ci
1037bf215546Sopenharmony_cierror_mc_source:
1038bf215546Sopenharmony_ci   dec->idct_source->destroy(dec->idct_source);
1039bf215546Sopenharmony_ci
1040bf215546Sopenharmony_cierror_idct_source:
1041bf215546Sopenharmony_ci   return false;
1042bf215546Sopenharmony_ci}
1043bf215546Sopenharmony_ci
1044bf215546Sopenharmony_cistatic bool
1045bf215546Sopenharmony_ciinit_mc_source_widthout_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
1046bf215546Sopenharmony_ci{
1047bf215546Sopenharmony_ci   enum pipe_format formats[3];
1048bf215546Sopenharmony_ci   struct pipe_video_buffer templat;
1049bf215546Sopenharmony_ci
1050bf215546Sopenharmony_ci   formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
1051bf215546Sopenharmony_ci   assert(pipe_format_to_chroma_format(formats[0]) == dec->base.chroma_format);
1052bf215546Sopenharmony_ci   memset(&templat, 0, sizeof(templat));
1053bf215546Sopenharmony_ci   templat.width = dec->base.width;
1054bf215546Sopenharmony_ci   templat.height = dec->base.height;
1055bf215546Sopenharmony_ci   dec->mc_source = vl_video_buffer_create_ex
1056bf215546Sopenharmony_ci   (
1057bf215546Sopenharmony_ci      dec->context, &templat,
1058bf215546Sopenharmony_ci      formats, 1, 1, PIPE_USAGE_DEFAULT,
1059bf215546Sopenharmony_ci      PIPE_VIDEO_CHROMA_FORMAT_420
1060bf215546Sopenharmony_ci   );
1061bf215546Sopenharmony_ci
1062bf215546Sopenharmony_ci   return dec->mc_source != NULL;
1063bf215546Sopenharmony_ci}
1064bf215546Sopenharmony_ci
1065bf215546Sopenharmony_cistatic void
1066bf215546Sopenharmony_cimc_vert_shader_callback(void *priv, struct vl_mc *mc,
1067bf215546Sopenharmony_ci                        struct ureg_program *shader,
1068bf215546Sopenharmony_ci                        unsigned first_output,
1069bf215546Sopenharmony_ci                        struct ureg_dst tex)
1070bf215546Sopenharmony_ci{
1071bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec = priv;
1072bf215546Sopenharmony_ci   struct ureg_dst o_vtex;
1073bf215546Sopenharmony_ci
1074bf215546Sopenharmony_ci   assert(priv && mc);
1075bf215546Sopenharmony_ci   assert(shader);
1076bf215546Sopenharmony_ci
1077bf215546Sopenharmony_ci   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1078bf215546Sopenharmony_ci      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1079bf215546Sopenharmony_ci      vl_idct_stage2_vert_shader(idct, shader, first_output, tex);
1080bf215546Sopenharmony_ci   } else {
1081bf215546Sopenharmony_ci      o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output);
1082bf215546Sopenharmony_ci      ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex));
1083bf215546Sopenharmony_ci   }
1084bf215546Sopenharmony_ci}
1085bf215546Sopenharmony_ci
1086bf215546Sopenharmony_cistatic void
1087bf215546Sopenharmony_cimc_frag_shader_callback(void *priv, struct vl_mc *mc,
1088bf215546Sopenharmony_ci                        struct ureg_program *shader,
1089bf215546Sopenharmony_ci                        unsigned first_input,
1090bf215546Sopenharmony_ci                        struct ureg_dst dst)
1091bf215546Sopenharmony_ci{
1092bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec = priv;
1093bf215546Sopenharmony_ci   struct ureg_src src, sampler;
1094bf215546Sopenharmony_ci
1095bf215546Sopenharmony_ci   assert(priv && mc);
1096bf215546Sopenharmony_ci   assert(shader);
1097bf215546Sopenharmony_ci
1098bf215546Sopenharmony_ci   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1099bf215546Sopenharmony_ci      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1100bf215546Sopenharmony_ci      vl_idct_stage2_frag_shader(idct, shader, first_input, dst);
1101bf215546Sopenharmony_ci   } else {
1102bf215546Sopenharmony_ci      src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR);
1103bf215546Sopenharmony_ci      sampler = ureg_DECL_sampler(shader, 0);
1104bf215546Sopenharmony_ci      ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler);
1105bf215546Sopenharmony_ci   }
1106bf215546Sopenharmony_ci}
1107bf215546Sopenharmony_ci
1108bf215546Sopenharmony_cistruct pipe_video_codec *
1109bf215546Sopenharmony_civl_create_mpeg12_decoder(struct pipe_context *context,
1110bf215546Sopenharmony_ci                         const struct pipe_video_codec *templat)
1111bf215546Sopenharmony_ci{
1112bf215546Sopenharmony_ci   const unsigned block_size_pixels = VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;
1113bf215546Sopenharmony_ci   const struct format_config *format_config;
1114bf215546Sopenharmony_ci   struct vl_mpeg12_decoder *dec;
1115bf215546Sopenharmony_ci
1116bf215546Sopenharmony_ci   assert(u_reduce_video_profile(templat->profile) == PIPE_VIDEO_FORMAT_MPEG12);
1117bf215546Sopenharmony_ci
1118bf215546Sopenharmony_ci   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
1119bf215546Sopenharmony_ci
1120bf215546Sopenharmony_ci   if (!dec)
1121bf215546Sopenharmony_ci      return NULL;
1122bf215546Sopenharmony_ci
1123bf215546Sopenharmony_ci   dec->base = *templat;
1124bf215546Sopenharmony_ci   dec->base.context = context;
1125bf215546Sopenharmony_ci   dec->context = pipe_create_multimedia_context(context->screen);
1126bf215546Sopenharmony_ci
1127bf215546Sopenharmony_ci   dec->base.destroy = vl_mpeg12_destroy;
1128bf215546Sopenharmony_ci   dec->base.begin_frame = vl_mpeg12_begin_frame;
1129bf215546Sopenharmony_ci   dec->base.decode_macroblock = vl_mpeg12_decode_macroblock;
1130bf215546Sopenharmony_ci   dec->base.decode_bitstream = vl_mpeg12_decode_bitstream;
1131bf215546Sopenharmony_ci   dec->base.end_frame = vl_mpeg12_end_frame;
1132bf215546Sopenharmony_ci   dec->base.flush = vl_mpeg12_flush;
1133bf215546Sopenharmony_ci
1134bf215546Sopenharmony_ci   dec->blocks_per_line = MAX2(util_next_power_of_two(dec->base.width) / block_size_pixels, 4);
1135bf215546Sopenharmony_ci   dec->num_blocks = (dec->base.width * dec->base.height) / block_size_pixels;
1136bf215546Sopenharmony_ci   dec->width_in_macroblocks = align(dec->base.width, VL_MACROBLOCK_WIDTH) / VL_MACROBLOCK_WIDTH;
1137bf215546Sopenharmony_ci
1138bf215546Sopenharmony_ci   /* TODO: Implement 422, 444 */
1139bf215546Sopenharmony_ci   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
1140bf215546Sopenharmony_ci
1141bf215546Sopenharmony_ci   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
1142bf215546Sopenharmony_ci      dec->chroma_width = dec->base.width / 2;
1143bf215546Sopenharmony_ci      dec->chroma_height = dec->base.height / 2;
1144bf215546Sopenharmony_ci      dec->num_blocks = dec->num_blocks * 2;
1145bf215546Sopenharmony_ci   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
1146bf215546Sopenharmony_ci      dec->chroma_width = dec->base.width / 2;
1147bf215546Sopenharmony_ci      dec->chroma_height = dec->base.height;
1148bf215546Sopenharmony_ci      dec->num_blocks = dec->num_blocks * 2 + dec->num_blocks;
1149bf215546Sopenharmony_ci   } else {
1150bf215546Sopenharmony_ci      dec->chroma_width = dec->base.width;
1151bf215546Sopenharmony_ci      dec->chroma_height = dec->base.height;
1152bf215546Sopenharmony_ci      dec->num_blocks = dec->num_blocks * 3;
1153bf215546Sopenharmony_ci   }
1154bf215546Sopenharmony_ci
1155bf215546Sopenharmony_ci   dec->quads = vl_vb_upload_quads(dec->context);
1156bf215546Sopenharmony_ci   dec->pos = vl_vb_upload_pos(
1157bf215546Sopenharmony_ci      dec->context,
1158bf215546Sopenharmony_ci      dec->base.width / VL_MACROBLOCK_WIDTH,
1159bf215546Sopenharmony_ci      dec->base.height / VL_MACROBLOCK_HEIGHT
1160bf215546Sopenharmony_ci   );
1161bf215546Sopenharmony_ci
1162bf215546Sopenharmony_ci   dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->context);
1163bf215546Sopenharmony_ci   dec->ves_mv = vl_vb_get_ves_mv(dec->context);
1164bf215546Sopenharmony_ci
1165bf215546Sopenharmony_ci   switch (templat->entrypoint) {
1166bf215546Sopenharmony_ci   case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:
1167bf215546Sopenharmony_ci      format_config = find_format_config(dec, bitstream_format_config, num_bitstream_format_configs);
1168bf215546Sopenharmony_ci      break;
1169bf215546Sopenharmony_ci
1170bf215546Sopenharmony_ci   case PIPE_VIDEO_ENTRYPOINT_IDCT:
1171bf215546Sopenharmony_ci      format_config = find_format_config(dec, idct_format_config, num_idct_format_configs);
1172bf215546Sopenharmony_ci      break;
1173bf215546Sopenharmony_ci
1174bf215546Sopenharmony_ci   case PIPE_VIDEO_ENTRYPOINT_MC:
1175bf215546Sopenharmony_ci      format_config = find_format_config(dec, mc_format_config, num_mc_format_configs);
1176bf215546Sopenharmony_ci      break;
1177bf215546Sopenharmony_ci
1178bf215546Sopenharmony_ci   default:
1179bf215546Sopenharmony_ci      assert(0);
1180bf215546Sopenharmony_ci      FREE(dec);
1181bf215546Sopenharmony_ci      return NULL;
1182bf215546Sopenharmony_ci   }
1183bf215546Sopenharmony_ci
1184bf215546Sopenharmony_ci   if (!format_config) {
1185bf215546Sopenharmony_ci      FREE(dec);
1186bf215546Sopenharmony_ci      return NULL;
1187bf215546Sopenharmony_ci   }
1188bf215546Sopenharmony_ci
1189bf215546Sopenharmony_ci   if (!init_zscan(dec, format_config))
1190bf215546Sopenharmony_ci      goto error_zscan;
1191bf215546Sopenharmony_ci
1192bf215546Sopenharmony_ci   if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1193bf215546Sopenharmony_ci      if (!init_idct(dec, format_config))
1194bf215546Sopenharmony_ci         goto error_sources;
1195bf215546Sopenharmony_ci   } else {
1196bf215546Sopenharmony_ci      if (!init_mc_source_widthout_idct(dec, format_config))
1197bf215546Sopenharmony_ci         goto error_sources;
1198bf215546Sopenharmony_ci   }
1199bf215546Sopenharmony_ci
1200bf215546Sopenharmony_ci   if (!vl_mc_init(&dec->mc_y, dec->context, dec->base.width, dec->base.height,
1201bf215546Sopenharmony_ci                   VL_MACROBLOCK_HEIGHT, format_config->mc_scale,
1202bf215546Sopenharmony_ci                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
1203bf215546Sopenharmony_ci      goto error_mc_y;
1204bf215546Sopenharmony_ci
1205bf215546Sopenharmony_ci   // TODO
1206bf215546Sopenharmony_ci   if (!vl_mc_init(&dec->mc_c, dec->context, dec->base.width, dec->base.height,
1207bf215546Sopenharmony_ci                   VL_BLOCK_HEIGHT, format_config->mc_scale,
1208bf215546Sopenharmony_ci                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
1209bf215546Sopenharmony_ci      goto error_mc_c;
1210bf215546Sopenharmony_ci
1211bf215546Sopenharmony_ci   if (!init_pipe_state(dec))
1212bf215546Sopenharmony_ci      goto error_pipe_state;
1213bf215546Sopenharmony_ci
1214bf215546Sopenharmony_ci   list_inithead(&dec->buffer_privates);
1215bf215546Sopenharmony_ci
1216bf215546Sopenharmony_ci   return &dec->base;
1217bf215546Sopenharmony_ci
1218bf215546Sopenharmony_cierror_pipe_state:
1219bf215546Sopenharmony_ci   vl_mc_cleanup(&dec->mc_c);
1220bf215546Sopenharmony_ci
1221bf215546Sopenharmony_cierror_mc_c:
1222bf215546Sopenharmony_ci   vl_mc_cleanup(&dec->mc_y);
1223bf215546Sopenharmony_ci
1224bf215546Sopenharmony_cierror_mc_y:
1225bf215546Sopenharmony_ci   if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1226bf215546Sopenharmony_ci      vl_idct_cleanup(&dec->idct_y);
1227bf215546Sopenharmony_ci      vl_idct_cleanup(&dec->idct_c);
1228bf215546Sopenharmony_ci      dec->idct_source->destroy(dec->idct_source);
1229bf215546Sopenharmony_ci   }
1230bf215546Sopenharmony_ci   dec->mc_source->destroy(dec->mc_source);
1231bf215546Sopenharmony_ci
1232bf215546Sopenharmony_cierror_sources:
1233bf215546Sopenharmony_ci   vl_zscan_cleanup(&dec->zscan_y);
1234bf215546Sopenharmony_ci   vl_zscan_cleanup(&dec->zscan_c);
1235bf215546Sopenharmony_ci
1236bf215546Sopenharmony_cierror_zscan:
1237bf215546Sopenharmony_ci   FREE(dec);
1238bf215546Sopenharmony_ci   return NULL;
1239bf215546Sopenharmony_ci}
1240