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