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