Path: blob/21.2-virgl/src/gallium/auxiliary/vl/vl_mpeg12_decoder.c
4565 views
/**************************************************************************1*2* Copyright 2009 Younes Manton.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <math.h>28#include <assert.h>2930#include "util/u_memory.h"31#include "util/u_sampler.h"32#include "util/u_surface.h"33#include "util/u_video.h"3435#include "vl_mpeg12_decoder.h"36#include "vl_defines.h"3738#define SCALE_FACTOR_SNORM (32768.0f / 256.0f)39#define SCALE_FACTOR_SSCALED (1.0f / 256.0f)4041struct format_config {42enum pipe_format zscan_source_format;43enum pipe_format idct_source_format;44enum pipe_format mc_source_format;4546float idct_scale;47float mc_scale;48};4950static const struct format_config bitstream_format_config[] = {51// { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },52// { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },53{ PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },54{ PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }55};5657static const unsigned num_bitstream_format_configs =58sizeof(bitstream_format_config) / sizeof(struct format_config);5960static const struct format_config idct_format_config[] = {61// { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },62// { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },63{ PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },64{ PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }65};6667static const unsigned num_idct_format_configs =68sizeof(idct_format_config) / sizeof(struct format_config);6970static const struct format_config mc_format_config[] = {71//{ PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SSCALED, 0.0f, SCALE_FACTOR_SSCALED },72{ PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SNORM, 0.0f, SCALE_FACTOR_SNORM }73};7475static const unsigned num_mc_format_configs =76sizeof(mc_format_config) / sizeof(struct format_config);7778static const unsigned const_empty_block_mask_420[3][2][2] = {79{ { 0x20, 0x10 }, { 0x08, 0x04 } },80{ { 0x02, 0x02 }, { 0x02, 0x02 } },81{ { 0x01, 0x01 }, { 0x01, 0x01 } }82};8384struct video_buffer_private85{86struct list_head list;87struct pipe_video_buffer *video_buffer;8889struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];90struct pipe_surface *surfaces[VL_MAX_SURFACES];9192struct vl_mpeg12_buffer *buffer;93};9495static void96vl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf);9798static void99destroy_video_buffer_private(void *private)100{101struct video_buffer_private *priv = private;102unsigned i;103104list_del(&priv->list);105106for (i = 0; i < VL_NUM_COMPONENTS; ++i)107pipe_sampler_view_reference(&priv->sampler_view_planes[i], NULL);108109for (i = 0; i < VL_MAX_SURFACES; ++i)110pipe_surface_reference(&priv->surfaces[i], NULL);111112if (priv->buffer)113vl_mpeg12_destroy_buffer(priv->buffer);114115FREE(priv);116}117118static struct video_buffer_private *119get_video_buffer_private(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *buf)120{121struct pipe_context *pipe = dec->context;122struct video_buffer_private *priv;123struct pipe_sampler_view **sv;124struct pipe_surface **surf;125unsigned i;126127priv = vl_video_buffer_get_associated_data(buf, &dec->base);128if (priv)129return priv;130131priv = CALLOC_STRUCT(video_buffer_private);132133list_add(&priv->list, &dec->buffer_privates);134priv->video_buffer = buf;135136sv = buf->get_sampler_view_planes(buf);137for (i = 0; i < VL_NUM_COMPONENTS; ++i)138if (sv[i])139priv->sampler_view_planes[i] = pipe->create_sampler_view(pipe, sv[i]->texture, sv[i]);140141surf = buf->get_surfaces(buf);142for (i = 0; i < VL_MAX_SURFACES; ++i)143if (surf[i])144priv->surfaces[i] = pipe->create_surface(pipe, surf[i]->texture, surf[i]);145146vl_video_buffer_set_associated_data(buf, &dec->base, priv, destroy_video_buffer_private);147148return priv;149}150151static void152free_video_buffer_privates(struct vl_mpeg12_decoder *dec)153{154struct video_buffer_private *priv, *next;155156LIST_FOR_EACH_ENTRY_SAFE(priv, next, &dec->buffer_privates, list) {157struct pipe_video_buffer *buf = priv->video_buffer;158159vl_video_buffer_set_associated_data(buf, &dec->base, NULL, NULL);160}161}162163static bool164init_zscan_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)165{166struct pipe_resource *res, res_tmpl;167struct pipe_sampler_view sv_tmpl;168struct pipe_surface **destination;169170unsigned i;171172assert(dec && buffer);173174memset(&res_tmpl, 0, sizeof(res_tmpl));175res_tmpl.target = PIPE_TEXTURE_2D;176res_tmpl.format = dec->zscan_source_format;177res_tmpl.width0 = dec->blocks_per_line * VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;178res_tmpl.height0 = align(dec->num_blocks, dec->blocks_per_line) / dec->blocks_per_line;179res_tmpl.depth0 = 1;180res_tmpl.array_size = 1;181res_tmpl.usage = PIPE_USAGE_STREAM;182res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;183184res = dec->context->screen->resource_create(dec->context->screen, &res_tmpl);185if (!res)186goto error_source;187188189memset(&sv_tmpl, 0, sizeof(sv_tmpl));190u_sampler_view_default_template(&sv_tmpl, res, res->format);191sv_tmpl.swizzle_r = sv_tmpl.swizzle_g = sv_tmpl.swizzle_b = sv_tmpl.swizzle_a = PIPE_SWIZZLE_X;192buffer->zscan_source = dec->context->create_sampler_view(dec->context, res, &sv_tmpl);193pipe_resource_reference(&res, NULL);194if (!buffer->zscan_source)195goto error_sampler;196197if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)198destination = dec->idct_source->get_surfaces(dec->idct_source);199else200destination = dec->mc_source->get_surfaces(dec->mc_source);201202if (!destination)203goto error_surface;204205for (i = 0; i < VL_NUM_COMPONENTS; ++i)206if (!vl_zscan_init_buffer(i == 0 ? &dec->zscan_y : &dec->zscan_c,207&buffer->zscan[i], buffer->zscan_source, destination[i]))208goto error_plane;209210return true;211212error_plane:213for (; i > 0; --i)214vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]);215216error_surface:217error_sampler:218pipe_sampler_view_reference(&buffer->zscan_source, NULL);219220error_source:221return false;222}223224static void225cleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer)226{227unsigned i;228229assert(buffer);230231for (i = 0; i < VL_NUM_COMPONENTS; ++i)232vl_zscan_cleanup_buffer(&buffer->zscan[i]);233234pipe_sampler_view_reference(&buffer->zscan_source, NULL);235}236237static bool238init_idct_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)239{240struct pipe_sampler_view **idct_source_sv, **mc_source_sv;241242unsigned i;243244assert(dec && buffer);245246idct_source_sv = dec->idct_source->get_sampler_view_planes(dec->idct_source);247if (!idct_source_sv)248goto error_source_sv;249250mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);251if (!mc_source_sv)252goto error_mc_source_sv;253254for (i = 0; i < 3; ++i)255if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,256&buffer->idct[i], idct_source_sv[i],257mc_source_sv[i]))258goto error_plane;259260return true;261262error_plane:263for (; i > 0; --i)264vl_idct_cleanup_buffer(&buffer->idct[i - 1]);265266error_mc_source_sv:267error_source_sv:268return false;269}270271static void272cleanup_idct_buffer(struct vl_mpeg12_buffer *buf)273{274unsigned i;275276assert(buf);277278for (i = 0; i < 3; ++i)279vl_idct_cleanup_buffer(&buf->idct[i]);280}281282static bool283init_mc_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buf)284{285assert(dec && buf);286287if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0]))288goto error_mc_y;289290if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1]))291goto error_mc_cb;292293if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2]))294goto error_mc_cr;295296return true;297298error_mc_cr:299vl_mc_cleanup_buffer(&buf->mc[1]);300301error_mc_cb:302vl_mc_cleanup_buffer(&buf->mc[0]);303304error_mc_y:305return false;306}307308static void309cleanup_mc_buffer(struct vl_mpeg12_buffer *buf)310{311unsigned i;312313assert(buf);314315for (i = 0; i < VL_NUM_COMPONENTS; ++i)316vl_mc_cleanup_buffer(&buf->mc[i]);317}318319static inline void320MacroBlockTypeToPipeWeights(const struct pipe_mpeg12_macroblock *mb, unsigned weights[2])321{322assert(mb);323324switch (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {325case PIPE_MPEG12_MB_TYPE_MOTION_FORWARD:326weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;327weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;328break;329330case (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD):331weights[0] = PIPE_VIDEO_MV_WEIGHT_HALF;332weights[1] = PIPE_VIDEO_MV_WEIGHT_HALF;333break;334335case PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD:336weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;337weights[1] = PIPE_VIDEO_MV_WEIGHT_MAX;338break;339340default:341if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) {342weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;343weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;344} else {345/* no motion vector, but also not intra mb ->346just copy the old frame content */347weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;348weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;349}350break;351}352}353354static inline struct vl_motionvector355MotionVectorToPipe(const struct pipe_mpeg12_macroblock *mb, unsigned vector,356unsigned field_select_mask, unsigned weight)357{358struct vl_motionvector mv;359360assert(mb);361362if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {363switch (mb->macroblock_modes.bits.frame_motion_type) {364case PIPE_MPEG12_MO_TYPE_FRAME:365mv.top.x = mb->PMV[0][vector][0];366mv.top.y = mb->PMV[0][vector][1];367mv.top.field_select = PIPE_VIDEO_FRAME;368mv.top.weight = weight;369370mv.bottom.x = mb->PMV[0][vector][0];371mv.bottom.y = mb->PMV[0][vector][1];372mv.bottom.weight = weight;373mv.bottom.field_select = PIPE_VIDEO_FRAME;374break;375376case PIPE_MPEG12_MO_TYPE_FIELD:377mv.top.x = mb->PMV[0][vector][0];378mv.top.y = mb->PMV[0][vector][1];379mv.top.field_select = (mb->motion_vertical_field_select & field_select_mask) ?380PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;381mv.top.weight = weight;382383mv.bottom.x = mb->PMV[1][vector][0];384mv.bottom.y = mb->PMV[1][vector][1];385mv.bottom.field_select = (mb->motion_vertical_field_select & (field_select_mask << 2)) ?386PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;387mv.bottom.weight = weight;388break;389390default:391unreachable("TODO: Support DUALPRIME and 16x8");392}393} else {394mv.top.x = mv.top.y = 0;395mv.top.field_select = PIPE_VIDEO_FRAME;396mv.top.weight = weight;397398mv.bottom.x = mv.bottom.y = 0;399mv.bottom.field_select = PIPE_VIDEO_FRAME;400mv.bottom.weight = weight;401}402return mv;403}404405static inline void406UploadYcbcrBlocks(struct vl_mpeg12_decoder *dec,407struct vl_mpeg12_buffer *buf,408const struct pipe_mpeg12_macroblock *mb)409{410unsigned intra;411unsigned tb, x, y, num_blocks = 0;412413assert(dec && buf);414assert(mb);415416if (!mb->coded_block_pattern)417return;418419intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA ? 1 : 0;420421for (y = 0; y < 2; ++y) {422for (x = 0; x < 2; ++x) {423if (mb->coded_block_pattern & const_empty_block_mask_420[0][y][x]) {424425struct vl_ycbcr_block *stream = buf->ycbcr_stream[0];426stream->x = mb->x * 2 + x;427stream->y = mb->y * 2 + y;428stream->intra = intra;429stream->coding = mb->macroblock_modes.bits.dct_type;430stream->block_num = buf->block_num++;431432buf->num_ycbcr_blocks[0]++;433buf->ycbcr_stream[0]++;434435num_blocks++;436}437}438}439440/* TODO: Implement 422, 444 */441//assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);442443for (tb = 1; tb < 3; ++tb) {444if (mb->coded_block_pattern & const_empty_block_mask_420[tb][0][0]) {445446struct vl_ycbcr_block *stream = buf->ycbcr_stream[tb];447stream->x = mb->x;448stream->y = mb->y;449stream->intra = intra;450stream->coding = 0;451stream->block_num = buf->block_num++;452453buf->num_ycbcr_blocks[tb]++;454buf->ycbcr_stream[tb]++;455456num_blocks++;457}458}459460memcpy(buf->texels, mb->blocks, 64 * sizeof(short) * num_blocks);461buf->texels += 64 * num_blocks;462}463464static void465vl_mpeg12_destroy_buffer(struct vl_mpeg12_buffer *buf)466{467468assert(buf);469470cleanup_zscan_buffer(buf);471cleanup_idct_buffer(buf);472cleanup_mc_buffer(buf);473vl_vb_cleanup(&buf->vertex_stream);474475FREE(buf);476}477478static void479vl_mpeg12_destroy(struct pipe_video_codec *decoder)480{481struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;482unsigned i;483484assert(decoder);485486free_video_buffer_privates(dec);487488/* Asserted in softpipe_delete_fs_state() for some reason */489dec->context->bind_vs_state(dec->context, NULL);490dec->context->bind_fs_state(dec->context, NULL);491492dec->context->delete_depth_stencil_alpha_state(dec->context, dec->dsa);493dec->context->delete_sampler_state(dec->context, dec->sampler_ycbcr);494495vl_mc_cleanup(&dec->mc_y);496vl_mc_cleanup(&dec->mc_c);497dec->mc_source->destroy(dec->mc_source);498499if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {500vl_idct_cleanup(&dec->idct_y);501vl_idct_cleanup(&dec->idct_c);502dec->idct_source->destroy(dec->idct_source);503}504505vl_zscan_cleanup(&dec->zscan_y);506vl_zscan_cleanup(&dec->zscan_c);507508dec->context->delete_vertex_elements_state(dec->context, dec->ves_ycbcr);509dec->context->delete_vertex_elements_state(dec->context, dec->ves_mv);510511pipe_resource_reference(&dec->quads.buffer.resource, NULL);512pipe_resource_reference(&dec->pos.buffer.resource, NULL);513514pipe_sampler_view_reference(&dec->zscan_linear, NULL);515pipe_sampler_view_reference(&dec->zscan_normal, NULL);516pipe_sampler_view_reference(&dec->zscan_alternate, NULL);517518for (i = 0; i < 4; ++i)519if (dec->dec_buffers[i])520vl_mpeg12_destroy_buffer(dec->dec_buffers[i]);521522dec->context->destroy(dec->context);523524FREE(dec);525}526527static struct vl_mpeg12_buffer *528vl_mpeg12_get_decode_buffer(struct vl_mpeg12_decoder *dec, struct pipe_video_buffer *target)529{530struct video_buffer_private *priv;531struct vl_mpeg12_buffer *buffer;532533assert(dec);534535priv = get_video_buffer_private(dec, target);536if (priv->buffer)537return priv->buffer;538539buffer = dec->dec_buffers[dec->current_buffer];540if (buffer)541return buffer;542543buffer = CALLOC_STRUCT(vl_mpeg12_buffer);544if (!buffer)545return NULL;546547if (!vl_vb_init(&buffer->vertex_stream, dec->context,548dec->base.width / VL_MACROBLOCK_WIDTH,549dec->base.height / VL_MACROBLOCK_HEIGHT))550goto error_vertex_buffer;551552if (!init_mc_buffer(dec, buffer))553goto error_mc;554555if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)556if (!init_idct_buffer(dec, buffer))557goto error_idct;558559if (!init_zscan_buffer(dec, buffer))560goto error_zscan;561562if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM)563vl_mpg12_bs_init(&buffer->bs, &dec->base);564565if (dec->base.expect_chunked_decode)566priv->buffer = buffer;567else568dec->dec_buffers[dec->current_buffer] = buffer;569570return buffer;571572error_zscan:573cleanup_idct_buffer(buffer);574575error_idct:576cleanup_mc_buffer(buffer);577578error_mc:579vl_vb_cleanup(&buffer->vertex_stream);580581error_vertex_buffer:582FREE(buffer);583return NULL;584}585586static void587vl_mpeg12_begin_frame(struct pipe_video_codec *decoder,588struct pipe_video_buffer *target,589struct pipe_picture_desc *picture)590{591struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;592struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;593struct vl_mpeg12_buffer *buf;594595struct pipe_resource *tex;596struct pipe_box rect = { 0, 0, 0, 1, 1, 1 };597598uint8_t intra_matrix[64];599uint8_t non_intra_matrix[64];600601unsigned i;602603assert(dec && target && picture);604605buf = vl_mpeg12_get_decode_buffer(dec, target);606assert(buf);607608if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {609memcpy(intra_matrix, desc->intra_matrix, sizeof(intra_matrix));610memcpy(non_intra_matrix, desc->non_intra_matrix, sizeof(non_intra_matrix));611intra_matrix[0] = 1 << (7 - desc->intra_dc_precision);612} else {613memset(intra_matrix, 0x10, sizeof(intra_matrix));614memset(non_intra_matrix, 0x10, sizeof(non_intra_matrix));615}616617for (i = 0; i < VL_NUM_COMPONENTS; ++i) {618struct vl_zscan *zscan = i == 0 ? &dec->zscan_y : &dec->zscan_c;619vl_zscan_upload_quant(zscan, &buf->zscan[i], intra_matrix, true);620vl_zscan_upload_quant(zscan, &buf->zscan[i], non_intra_matrix, false);621}622623vl_vb_map(&buf->vertex_stream, dec->context);624625tex = buf->zscan_source->texture;626rect.width = tex->width0;627rect.height = tex->height0;628629buf->texels =630dec->context->texture_map(dec->context, tex, 0,631PIPE_MAP_WRITE |632PIPE_MAP_DISCARD_RANGE,633&rect, &buf->tex_transfer);634635buf->block_num = 0;636637for (i = 0; i < VL_NUM_COMPONENTS; ++i) {638buf->ycbcr_stream[i] = vl_vb_get_ycbcr_stream(&buf->vertex_stream, i);639buf->num_ycbcr_blocks[i] = 0;640}641642for (i = 0; i < VL_MAX_REF_FRAMES; ++i)643buf->mv_stream[i] = vl_vb_get_mv_stream(&buf->vertex_stream, i);644645if (dec->base.entrypoint >= PIPE_VIDEO_ENTRYPOINT_IDCT) {646for (i = 0; i < VL_NUM_COMPONENTS; ++i)647vl_zscan_set_layout(&buf->zscan[i], dec->zscan_linear);648}649}650651static void652vl_mpeg12_decode_macroblock(struct pipe_video_codec *decoder,653struct pipe_video_buffer *target,654struct pipe_picture_desc *picture,655const struct pipe_macroblock *macroblocks,656unsigned num_macroblocks)657{658struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;659const struct pipe_mpeg12_macroblock *mb = (const struct pipe_mpeg12_macroblock *)macroblocks;660struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;661struct vl_mpeg12_buffer *buf;662663unsigned i, j, mv_weights[2];664665assert(dec && target && picture);666assert(macroblocks && macroblocks->codec == PIPE_VIDEO_FORMAT_MPEG12);667668buf = vl_mpeg12_get_decode_buffer(dec, target);669assert(buf);670671for (; num_macroblocks > 0; --num_macroblocks) {672unsigned mb_addr = mb->y * dec->width_in_macroblocks + mb->x;673674if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_PATTERN | PIPE_MPEG12_MB_TYPE_INTRA))675UploadYcbcrBlocks(dec, buf, mb);676677MacroBlockTypeToPipeWeights(mb, mv_weights);678679for (i = 0; i < 2; ++i) {680if (!desc->ref[i]) continue;681682buf->mv_stream[i][mb_addr] = MotionVectorToPipe683(684mb, i,685i ? PIPE_MPEG12_FS_FIRST_BACKWARD : PIPE_MPEG12_FS_FIRST_FORWARD,686mv_weights[i]687);688}689690/* see section 7.6.6 of the spec */691if (mb->num_skipped_macroblocks > 0) {692struct vl_motionvector skipped_mv[2];693694if (desc->ref[0] && !desc->ref[1]) {695skipped_mv[0].top.x = skipped_mv[0].top.y = 0;696skipped_mv[0].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX;697} else {698skipped_mv[0] = buf->mv_stream[0][mb_addr];699skipped_mv[1] = buf->mv_stream[1][mb_addr];700}701skipped_mv[0].top.field_select = PIPE_VIDEO_FRAME;702skipped_mv[1].top.field_select = PIPE_VIDEO_FRAME;703704skipped_mv[0].bottom = skipped_mv[0].top;705skipped_mv[1].bottom = skipped_mv[1].top;706707++mb_addr;708for (i = 0; i < mb->num_skipped_macroblocks; ++i, ++mb_addr) {709for (j = 0; j < 2; ++j) {710if (!desc->ref[j]) continue;711buf->mv_stream[j][mb_addr] = skipped_mv[j];712713}714}715}716717++mb;718}719}720721static void722vl_mpeg12_decode_bitstream(struct pipe_video_codec *decoder,723struct pipe_video_buffer *target,724struct pipe_picture_desc *picture,725unsigned num_buffers,726const void * const *buffers,727const unsigned *sizes)728{729struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;730struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;731struct vl_mpeg12_buffer *buf;732733unsigned i;734735assert(dec && target && picture);736737buf = vl_mpeg12_get_decode_buffer(dec, target);738assert(buf);739740for (i = 0; i < VL_NUM_COMPONENTS; ++i)741vl_zscan_set_layout(&buf->zscan[i], desc->alternate_scan ?742dec->zscan_alternate : dec->zscan_normal);743744vl_mpg12_bs_decode(&buf->bs, target, desc, num_buffers, buffers, sizes);745}746747static void748vl_mpeg12_end_frame(struct pipe_video_codec *decoder,749struct pipe_video_buffer *target,750struct pipe_picture_desc *picture)751{752struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;753struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;754struct pipe_sampler_view **ref_frames[2];755struct pipe_sampler_view **mc_source_sv;756struct pipe_surface **target_surfaces;757struct pipe_vertex_buffer vb[3];758struct vl_mpeg12_buffer *buf;759760const unsigned *plane_order;761unsigned i, j, component;762unsigned nr_components;763764assert(dec && target && picture);765assert(!target->interlaced);766767buf = vl_mpeg12_get_decode_buffer(dec, target);768769vl_vb_unmap(&buf->vertex_stream, dec->context);770771if (buf->tex_transfer)772dec->context->texture_unmap(dec->context, buf->tex_transfer);773774vb[0] = dec->quads;775vb[1] = dec->pos;776777target_surfaces = get_video_buffer_private(dec, target)->surfaces;778779for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {780if (desc->ref[i])781ref_frames[i] = get_video_buffer_private(dec, desc->ref[i])->sampler_view_planes;782else783ref_frames[i] = NULL;784}785786dec->context->bind_vertex_elements_state(dec->context, dec->ves_mv);787for (i = 0; i < VL_NUM_COMPONENTS; ++i) {788if (!target_surfaces[i]) continue;789790vl_mc_set_surface(&buf->mc[i], target_surfaces[i]);791792for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {793if (!ref_frames[j] || !ref_frames[j][i]) continue;794795vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);796dec->context->set_vertex_buffers(dec->context, 0, 3, 0, false, vb);797798vl_mc_render_ref(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], ref_frames[j][i]);799}800}801802dec->context->bind_vertex_elements_state(dec->context, dec->ves_ycbcr);803for (i = 0; i < VL_NUM_COMPONENTS; ++i) {804if (!buf->num_ycbcr_blocks[i]) continue;805806vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);807dec->context->set_vertex_buffers(dec->context, 0, 2, 0, false, vb);808809vl_zscan_render(i ? &dec->zscan_c : & dec->zscan_y, &buf->zscan[i] , buf->num_ycbcr_blocks[i]);810811if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)812vl_idct_flush(i ? &dec->idct_c : &dec->idct_y, &buf->idct[i], buf->num_ycbcr_blocks[i]);813}814815plane_order = vl_video_buffer_plane_order(target->buffer_format);816mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);817for (i = 0, component = 0; component < VL_NUM_COMPONENTS; ++i) {818if (!target_surfaces[i]) continue;819820nr_components = util_format_get_nr_components(target_surfaces[i]->texture->format);821for (j = 0; j < nr_components; ++j, ++component) {822unsigned plane = plane_order[component];823if (!buf->num_ycbcr_blocks[plane]) continue;824825vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, plane);826dec->context->set_vertex_buffers(dec->context, 0, 2, 0, false, vb);827828if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)829vl_idct_prepare_stage2(i ? &dec->idct_c : &dec->idct_y, &buf->idct[plane]);830else {831dec->context->set_sampler_views(dec->context,832PIPE_SHADER_FRAGMENT, 0, 1, 0,833&mc_source_sv[plane]);834dec->context->bind_sampler_states(dec->context,835PIPE_SHADER_FRAGMENT,8360, 1, &dec->sampler_ycbcr);837}838vl_mc_render_ycbcr(i ? &dec->mc_c : &dec->mc_y, &buf->mc[i], j, buf->num_ycbcr_blocks[plane]);839}840}841dec->context->flush(dec->context, NULL, 0);842++dec->current_buffer;843dec->current_buffer %= 4;844}845846static void847vl_mpeg12_flush(struct pipe_video_codec *decoder)848{849assert(decoder);850851//Noop, for shaders it is much faster to flush everything in end_frame852}853854static bool855init_pipe_state(struct vl_mpeg12_decoder *dec)856{857struct pipe_depth_stencil_alpha_state dsa;858struct pipe_sampler_state sampler;859unsigned i;860861assert(dec);862863memset(&dsa, 0, sizeof dsa);864dsa.depth_enabled = 0;865dsa.depth_writemask = 0;866dsa.depth_func = PIPE_FUNC_ALWAYS;867for (i = 0; i < 2; ++i) {868dsa.stencil[i].enabled = 0;869dsa.stencil[i].func = PIPE_FUNC_ALWAYS;870dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;871dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;872dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;873dsa.stencil[i].valuemask = 0;874dsa.stencil[i].writemask = 0;875}876dsa.alpha_enabled = 0;877dsa.alpha_func = PIPE_FUNC_ALWAYS;878dsa.alpha_ref_value = 0;879dec->dsa = dec->context->create_depth_stencil_alpha_state(dec->context, &dsa);880dec->context->bind_depth_stencil_alpha_state(dec->context, dec->dsa);881882memset(&sampler, 0, sizeof(sampler));883sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;884sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;885sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER;886sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;887sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;888sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;889sampler.compare_mode = PIPE_TEX_COMPARE_NONE;890sampler.compare_func = PIPE_FUNC_ALWAYS;891sampler.normalized_coords = 1;892dec->sampler_ycbcr = dec->context->create_sampler_state(dec->context, &sampler);893if (!dec->sampler_ycbcr)894return false;895896return true;897}898899static const struct format_config*900find_format_config(struct vl_mpeg12_decoder *dec, const struct format_config configs[], unsigned num_configs)901{902struct pipe_screen *screen;903unsigned i;904905assert(dec);906907screen = dec->context->screen;908909for (i = 0; i < num_configs; ++i) {910if (!screen->is_format_supported(screen, configs[i].zscan_source_format, PIPE_TEXTURE_2D,9111, 1, PIPE_BIND_SAMPLER_VIEW))912continue;913914if (configs[i].idct_source_format != PIPE_FORMAT_NONE) {915if (!screen->is_format_supported(screen, configs[i].idct_source_format, PIPE_TEXTURE_2D,9161, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))917continue;918919if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_3D,9201, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))921continue;922} else {923if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_2D,9241, 1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))925continue;926}927return &configs[i];928}929930return NULL;931}932933static bool934init_zscan(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)935{936unsigned num_channels;937938assert(dec);939940dec->zscan_source_format = format_config->zscan_source_format;941dec->zscan_linear = vl_zscan_layout(dec->context, vl_zscan_linear, dec->blocks_per_line);942dec->zscan_normal = vl_zscan_layout(dec->context, vl_zscan_normal, dec->blocks_per_line);943dec->zscan_alternate = vl_zscan_layout(dec->context, vl_zscan_alternate, dec->blocks_per_line);944945num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;946947if (!vl_zscan_init(&dec->zscan_y, dec->context, dec->base.width, dec->base.height,948dec->blocks_per_line, dec->num_blocks, num_channels))949return false;950951if (!vl_zscan_init(&dec->zscan_c, dec->context, dec->chroma_width, dec->chroma_height,952dec->blocks_per_line, dec->num_blocks, num_channels))953return false;954955return true;956}957958static bool959init_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)960{961unsigned nr_of_idct_render_targets, max_inst;962enum pipe_format formats[3];963struct pipe_video_buffer templat;964965struct pipe_sampler_view *matrix = NULL;966967nr_of_idct_render_targets = dec->context->screen->get_param968(969dec->context->screen, PIPE_CAP_MAX_RENDER_TARGETS970);971972max_inst = dec->context->screen->get_shader_param973(974dec->context->screen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_MAX_INSTRUCTIONS975);976977// Just assume we need 32 inst per render target, not 100% true, but should work in most cases978if (nr_of_idct_render_targets >= 4 && max_inst >= 32*4)979// more than 4 render targets usually doesn't makes any seens980nr_of_idct_render_targets = 4;981else982nr_of_idct_render_targets = 1;983984formats[0] = formats[1] = formats[2] = format_config->idct_source_format;985memset(&templat, 0, sizeof(templat));986templat.width = dec->base.width / 4;987templat.height = dec->base.height;988dec->idct_source = vl_video_buffer_create_ex989(990dec->context, &templat,991formats, 1, 1, PIPE_USAGE_DEFAULT,992PIPE_VIDEO_CHROMA_FORMAT_420993);994995if (!dec->idct_source)996goto error_idct_source;997998formats[0] = formats[1] = formats[2] = format_config->mc_source_format;999memset(&templat, 0, sizeof(templat));1000templat.width = dec->base.width / nr_of_idct_render_targets;1001templat.height = dec->base.height / 4;1002dec->mc_source = vl_video_buffer_create_ex1003(1004dec->context, &templat,1005formats, nr_of_idct_render_targets, 1, PIPE_USAGE_DEFAULT,1006PIPE_VIDEO_CHROMA_FORMAT_4201007);10081009if (!dec->mc_source)1010goto error_mc_source;10111012if (!(matrix = vl_idct_upload_matrix(dec->context, format_config->idct_scale)))1013goto error_matrix;10141015if (!vl_idct_init(&dec->idct_y, dec->context, dec->base.width, dec->base.height,1016nr_of_idct_render_targets, matrix, matrix))1017goto error_y;10181019if(!vl_idct_init(&dec->idct_c, dec->context, dec->chroma_width, dec->chroma_height,1020nr_of_idct_render_targets, matrix, matrix))1021goto error_c;10221023pipe_sampler_view_reference(&matrix, NULL);10241025return true;10261027error_c:1028vl_idct_cleanup(&dec->idct_y);10291030error_y:1031pipe_sampler_view_reference(&matrix, NULL);10321033error_matrix:1034dec->mc_source->destroy(dec->mc_source);10351036error_mc_source:1037dec->idct_source->destroy(dec->idct_source);10381039error_idct_source:1040return false;1041}10421043static bool1044init_mc_source_widthout_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)1045{1046enum pipe_format formats[3];1047struct pipe_video_buffer templat;10481049formats[0] = formats[1] = formats[2] = format_config->mc_source_format;1050assert(pipe_format_to_chroma_format(formats[0]) == dec->base.chroma_format);1051memset(&templat, 0, sizeof(templat));1052templat.width = dec->base.width;1053templat.height = dec->base.height;1054dec->mc_source = vl_video_buffer_create_ex1055(1056dec->context, &templat,1057formats, 1, 1, PIPE_USAGE_DEFAULT,1058PIPE_VIDEO_CHROMA_FORMAT_4201059);10601061return dec->mc_source != NULL;1062}10631064static void1065mc_vert_shader_callback(void *priv, struct vl_mc *mc,1066struct ureg_program *shader,1067unsigned first_output,1068struct ureg_dst tex)1069{1070struct vl_mpeg12_decoder *dec = priv;1071struct ureg_dst o_vtex;10721073assert(priv && mc);1074assert(shader);10751076if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {1077struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;1078vl_idct_stage2_vert_shader(idct, shader, first_output, tex);1079} else {1080o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output);1081ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex));1082}1083}10841085static void1086mc_frag_shader_callback(void *priv, struct vl_mc *mc,1087struct ureg_program *shader,1088unsigned first_input,1089struct ureg_dst dst)1090{1091struct vl_mpeg12_decoder *dec = priv;1092struct ureg_src src, sampler;10931094assert(priv && mc);1095assert(shader);10961097if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {1098struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;1099vl_idct_stage2_frag_shader(idct, shader, first_input, dst);1100} else {1101src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR);1102sampler = ureg_DECL_sampler(shader, 0);1103ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler);1104}1105}11061107struct pipe_video_codec *1108vl_create_mpeg12_decoder(struct pipe_context *context,1109const struct pipe_video_codec *templat)1110{1111const unsigned block_size_pixels = VL_BLOCK_WIDTH * VL_BLOCK_HEIGHT;1112const struct format_config *format_config;1113struct vl_mpeg12_decoder *dec;11141115assert(u_reduce_video_profile(templat->profile) == PIPE_VIDEO_FORMAT_MPEG12);11161117dec = CALLOC_STRUCT(vl_mpeg12_decoder);11181119if (!dec)1120return NULL;11211122dec->base = *templat;1123dec->base.context = context;1124dec->context = pipe_create_multimedia_context(context->screen);11251126dec->base.destroy = vl_mpeg12_destroy;1127dec->base.begin_frame = vl_mpeg12_begin_frame;1128dec->base.decode_macroblock = vl_mpeg12_decode_macroblock;1129dec->base.decode_bitstream = vl_mpeg12_decode_bitstream;1130dec->base.end_frame = vl_mpeg12_end_frame;1131dec->base.flush = vl_mpeg12_flush;11321133dec->blocks_per_line = MAX2(util_next_power_of_two(dec->base.width) / block_size_pixels, 4);1134dec->num_blocks = (dec->base.width * dec->base.height) / block_size_pixels;1135dec->width_in_macroblocks = align(dec->base.width, VL_MACROBLOCK_WIDTH) / VL_MACROBLOCK_WIDTH;11361137/* TODO: Implement 422, 444 */1138assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);11391140if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {1141dec->chroma_width = dec->base.width / 2;1142dec->chroma_height = dec->base.height / 2;1143dec->num_blocks = dec->num_blocks * 2;1144} else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {1145dec->chroma_width = dec->base.width / 2;1146dec->chroma_height = dec->base.height;1147dec->num_blocks = dec->num_blocks * 2 + dec->num_blocks;1148} else {1149dec->chroma_width = dec->base.width;1150dec->chroma_height = dec->base.height;1151dec->num_blocks = dec->num_blocks * 3;1152}11531154dec->quads = vl_vb_upload_quads(dec->context);1155dec->pos = vl_vb_upload_pos(1156dec->context,1157dec->base.width / VL_MACROBLOCK_WIDTH,1158dec->base.height / VL_MACROBLOCK_HEIGHT1159);11601161dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->context);1162dec->ves_mv = vl_vb_get_ves_mv(dec->context);11631164switch (templat->entrypoint) {1165case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:1166format_config = find_format_config(dec, bitstream_format_config, num_bitstream_format_configs);1167break;11681169case PIPE_VIDEO_ENTRYPOINT_IDCT:1170format_config = find_format_config(dec, idct_format_config, num_idct_format_configs);1171break;11721173case PIPE_VIDEO_ENTRYPOINT_MC:1174format_config = find_format_config(dec, mc_format_config, num_mc_format_configs);1175break;11761177default:1178assert(0);1179FREE(dec);1180return NULL;1181}11821183if (!format_config) {1184FREE(dec);1185return NULL;1186}11871188if (!init_zscan(dec, format_config))1189goto error_zscan;11901191if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {1192if (!init_idct(dec, format_config))1193goto error_sources;1194} else {1195if (!init_mc_source_widthout_idct(dec, format_config))1196goto error_sources;1197}11981199if (!vl_mc_init(&dec->mc_y, dec->context, dec->base.width, dec->base.height,1200VL_MACROBLOCK_HEIGHT, format_config->mc_scale,1201mc_vert_shader_callback, mc_frag_shader_callback, dec))1202goto error_mc_y;12031204// TODO1205if (!vl_mc_init(&dec->mc_c, dec->context, dec->base.width, dec->base.height,1206VL_BLOCK_HEIGHT, format_config->mc_scale,1207mc_vert_shader_callback, mc_frag_shader_callback, dec))1208goto error_mc_c;12091210if (!init_pipe_state(dec))1211goto error_pipe_state;12121213list_inithead(&dec->buffer_privates);12141215return &dec->base;12161217error_pipe_state:1218vl_mc_cleanup(&dec->mc_c);12191220error_mc_c:1221vl_mc_cleanup(&dec->mc_y);12221223error_mc_y:1224if (templat->entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {1225vl_idct_cleanup(&dec->idct_y);1226vl_idct_cleanup(&dec->idct_c);1227dec->idct_source->destroy(dec->idct_source);1228}1229dec->mc_source->destroy(dec->mc_source);12301231error_sources:1232vl_zscan_cleanup(&dec->zscan_y);1233vl_zscan_cleanup(&dec->zscan_c);12341235error_zscan:1236FREE(dec);1237return NULL;1238}123912401241