si_uvd.c revision 786af2f963925df2c2a6fb60b29a83e8340f03c7
15b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/**************************************************************************
25b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
35b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * Copyright 2011 Advanced Micro Devices, Inc.
45b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * All Rights Reserved.
55b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
65b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * Permission is hereby granted, free of charge, to any person obtaining a
75b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * copy of this software and associated documentation files (the
85b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * "Software"), to deal in the Software without restriction, including
95b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * without limitation the rights to use, copy, modify, merge, publish,
105b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * distribute, sub license, and/or sell copies of the Software, and to
115b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * permit persons to whom the Software is furnished to do so, subject to
125b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * the following conditions:
135b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
145b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * The above copyright notice and this permission notice (including the
155b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * next paragraph) shall be included in all copies or substantial portions
165b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * of the Software.
175b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
185b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
195b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
205b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
215b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
225b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
235b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
245b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
255b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
265b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König **************************************************************************/
275b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
285b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/*
295b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * Authors:
305b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *      Christian König <christian.koenig@amd.com>
315b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
325b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König */
335b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
345b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include <sys/types.h>
355b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include <assert.h>
365b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include <errno.h>
375b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include <unistd.h>
385b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
39f2f7064e560a83fc78d0e5b1d3a7d4aaac119a49Christian König#include "pipe/p_video_codec.h"
405b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
415b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include "util/u_memory.h"
425b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include "util/u_video.h"
435b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
445b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include "vl/vl_defines.h"
455b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include "vl/vl_mpeg12_decoder.h"
465b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
47786af2f963925df2c2a6fb60b29a83e8340f03c7Andreas Hartmetz#include "si_pipe.h"
485b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include "radeon/radeon_uvd.h"
495b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include "sid.h"
505b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
515b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/**
525b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * creates an video buffer with an UVD compatible memory layout
535b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König */
545b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian Königstruct pipe_video_buffer *radeonsi_video_buffer_create(struct pipe_context *pipe,
555b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König						       const struct pipe_video_buffer *tmpl)
565b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König{
575b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct r600_context *ctx = (struct r600_context *)pipe;
58363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	struct r600_texture *resources[VL_NUM_COMPONENTS] = {};
595b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct radeon_surface *surfaces[VL_NUM_COMPONENTS] = {};
605b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct pb_buffer **pbs[VL_NUM_COMPONENTS] = {};
615b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	const enum pipe_format *resource_formats;
625b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct pipe_video_buffer template;
635b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct pipe_resource templ;
647d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	unsigned i, array_size;
655b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
665b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	assert(pipe);
675b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
685b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	/* first create the needed resources as "normal" textures */
695b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	resource_formats = vl_video_buffer_formats(pipe->screen, tmpl->buffer_format);
705b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (!resource_formats)
715b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		return NULL;
725b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
737d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	array_size = tmpl->interlaced ? 2 : 1;
745b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	template = *tmpl;
755b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	template.width = align(tmpl->width, VL_MACROBLOCK_WIDTH);
767d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	template.height = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
775b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
7885b0880a17a2cffb4b1116267656b33e79e1518fChristian König	vl_video_buffer_template(&templ, &template, resource_formats[0], 1, array_size, PIPE_USAGE_STATIC, 0);
79270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy	/* TODO: get tiling working */
80270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy	templ.bind = PIPE_BIND_LINEAR;
81363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	resources[0] = (struct r600_texture *)
825b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		pipe->screen->resource_create(pipe->screen, &templ);
835b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (!resources[0])
845b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		goto error;
855b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
865b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (resource_formats[1] != PIPE_FORMAT_NONE) {
8785b0880a17a2cffb4b1116267656b33e79e1518fChristian König		vl_video_buffer_template(&templ, &template, resource_formats[1], 1, array_size, PIPE_USAGE_STATIC, 1);
88270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy		templ.bind = PIPE_BIND_LINEAR;
89363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák		resources[1] = (struct r600_texture *)
905b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			pipe->screen->resource_create(pipe->screen, &templ);
915b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[1])
925b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			goto error;
935b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
945b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
955b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (resource_formats[2] != PIPE_FORMAT_NONE) {
9685b0880a17a2cffb4b1116267656b33e79e1518fChristian König		vl_video_buffer_template(&templ, &template, resource_formats[2], 1, array_size, PIPE_USAGE_STATIC, 2);
97270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy		templ.bind = PIPE_BIND_LINEAR;
98363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák		resources[2] = (struct r600_texture *)
995b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			pipe->screen->resource_create(pipe->screen, &templ);
1005b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[2])
1015b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			goto error;
1025b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
1035b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1045b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
1055b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[i])
1065b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			continue;
1075b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1085b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		surfaces[i] = & resources[i]->surface;
1095b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		pbs[i] = &resources[i]->resource.buf;
1105b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
1115b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
112a81c3e00fee0626e63b1fb8ebb4c2cef3fb23367Marek Olšák	ruvd_join_surfaces(ctx->b.ws, templ.bind, pbs, surfaces);
1135b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1145b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
1155b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[i])
1165b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			continue;
1175b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1185b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		/* recreate the CS handle */
119a81c3e00fee0626e63b1fb8ebb4c2cef3fb23367Marek Olšák		resources[i]->resource.cs_buf = ctx->b.ws->buffer_get_cs_handle(
1205b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			resources[i]->resource.buf);
1215b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
1225b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1237d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	template.height *= array_size;
1245b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	return vl_video_buffer_create_ex2(pipe, &template, (struct pipe_resource **)resources);
1255b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1265b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian Königerror:
1275b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	for (i = 0; i < VL_NUM_COMPONENTS; ++i)
1285b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		pipe_resource_reference((struct pipe_resource **)&resources[i], NULL);
1295b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1305b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	return NULL;
1315b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König}
1325b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1335b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/* set the decoding target buffer offsets */
1345b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian Königstatic struct radeon_winsys_cs_handle* radeonsi_uvd_set_dtb(struct ruvd_msg *msg, struct vl_video_buffer *buf)
1355b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König{
136363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	struct r600_texture *luma = (struct r600_texture *)buf->resources[0];
137363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	struct r600_texture *chroma = (struct r600_texture *)buf->resources[1];
1385b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1392c2c54b819d8e0bd940f56501a2c3699d7cc2397Christian König	msg->body.decode.dt_field_mode = buf->base.interlaced;
1405b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1415b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	ruvd_set_dt_surfaces(msg, &luma->surface, &chroma->surface);
1425b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1435b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	return luma->resource.cs_buf;
1445b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König}
1455b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1465b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/**
1475b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * creates an UVD compatible decoder
1485b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König */
149f2f7064e560a83fc78d0e5b1d3a7d4aaac119a49Christian Königstruct pipe_video_codec *radeonsi_uvd_create_decoder(struct pipe_context *context,
150f2f7064e560a83fc78d0e5b1d3a7d4aaac119a49Christian König						     const struct pipe_video_codec *templ)
1515b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König{
1522487324591c1629492288a814e1d8a3046d37b15Christian König	return ruvd_create_decoder(context, templ, radeonsi_uvd_set_dtb);
1535b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König}
154