1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2013 Advanced Micro Devices, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#if ENABLE_ST_OMX_TIZONIA
29bf215546Sopenharmony_ci#include <tizkernel.h>
30bf215546Sopenharmony_ci#endif
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "util/u_memory.h"
33bf215546Sopenharmony_ci#include "vl/vl_winsys.h"
34bf215546Sopenharmony_ci#include "vl/vl_video_buffer.h"
35bf215546Sopenharmony_ci#include "util/u_surface.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "vid_dec_common.h"
38bf215546Sopenharmony_ci#include "vid_dec_h264_common.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_civoid vid_dec_NeedTarget(vid_dec_PrivateType *priv)
41bf215546Sopenharmony_ci{
42bf215546Sopenharmony_ci   struct pipe_video_buffer templat = {};
43bf215546Sopenharmony_ci   struct vl_screen *omx_screen;
44bf215546Sopenharmony_ci   struct pipe_screen *pscreen;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   omx_screen = priv->screen;
47bf215546Sopenharmony_ci   assert(omx_screen);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   pscreen = omx_screen->pscreen;
50bf215546Sopenharmony_ci   assert(pscreen);
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   if (!priv->target) {
53bf215546Sopenharmony_ci      memset(&templat, 0, sizeof(templat));
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci      templat.width = priv->codec->width;
56bf215546Sopenharmony_ci      templat.height = priv->codec->height;
57bf215546Sopenharmony_ci      templat.buffer_format = pscreen->get_video_param(
58bf215546Sopenharmony_ci            pscreen,
59bf215546Sopenharmony_ci            priv->profile,
60bf215546Sopenharmony_ci            PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
61bf215546Sopenharmony_ci            PIPE_VIDEO_CAP_PREFERED_FORMAT
62bf215546Sopenharmony_ci      );
63bf215546Sopenharmony_ci      templat.interlaced = pscreen->get_video_param(
64bf215546Sopenharmony_ci          pscreen,
65bf215546Sopenharmony_ci          priv->profile,
66bf215546Sopenharmony_ci          PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
67bf215546Sopenharmony_ci          PIPE_VIDEO_CAP_PREFERS_INTERLACED
68bf215546Sopenharmony_ci      );
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci      priv->target = priv->pipe->create_video_buffer(priv->pipe, &templat);
71bf215546Sopenharmony_ci   }
72bf215546Sopenharmony_ci}
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_civoid vid_dec_FillOutput(vid_dec_PrivateType *priv, struct pipe_video_buffer *buf,
75bf215546Sopenharmony_ci                        OMX_BUFFERHEADERTYPE* output)
76bf215546Sopenharmony_ci{
77bf215546Sopenharmony_ci#if ENABLE_ST_OMX_TIZONIA
78bf215546Sopenharmony_ci   tiz_port_t *out_port = tiz_krn_get_port(tiz_get_krn(handleOf(priv)),
79bf215546Sopenharmony_ci                                           OMX_VID_DEC_AVC_OUTPUT_PORT_INDEX);
80bf215546Sopenharmony_ci   OMX_VIDEO_PORTDEFINITIONTYPE *def = &out_port->portdef_.format.video;
81bf215546Sopenharmony_ci#else
82bf215546Sopenharmony_ci   omx_base_PortType *port = priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
83bf215546Sopenharmony_ci   OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
84bf215546Sopenharmony_ci#endif
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   struct pipe_sampler_view **views;
87bf215546Sopenharmony_ci   unsigned i, j;
88bf215546Sopenharmony_ci   unsigned width, height;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   views = buf->get_sampler_view_planes(buf);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci#if ENABLE_ST_OMX_TIZONIA
93bf215546Sopenharmony_ci   if (!output->pBuffer) {
94bf215546Sopenharmony_ci      struct pipe_video_buffer *dst_buf = NULL;
95bf215546Sopenharmony_ci      struct pipe_surface **dst_surface = NULL;
96bf215546Sopenharmony_ci      struct u_rect src_rect;
97bf215546Sopenharmony_ci      struct u_rect dst_rect;
98bf215546Sopenharmony_ci      struct vl_compositor *compositor = &priv->compositor;
99bf215546Sopenharmony_ci      struct vl_compositor_state *s = &priv->cstate;
100bf215546Sopenharmony_ci      enum vl_compositor_deinterlace deinterlace = VL_COMPOSITOR_WEAVE;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci      dst_buf = util_hash_table_get(priv->video_buffer_map, output);
103bf215546Sopenharmony_ci      assert(dst_buf);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      dst_surface = dst_buf->get_surfaces(dst_buf);
106bf215546Sopenharmony_ci      assert(views);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci      src_rect.x0 = 0;
109bf215546Sopenharmony_ci      src_rect.y0 = 0;
110bf215546Sopenharmony_ci      src_rect.x1 = def->nFrameWidth;
111bf215546Sopenharmony_ci      src_rect.y1 = def->nFrameHeight;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci      dst_rect.x0 = 0;
114bf215546Sopenharmony_ci      dst_rect.y0 = 0;
115bf215546Sopenharmony_ci      dst_rect.x1 = def->nFrameWidth;
116bf215546Sopenharmony_ci      dst_rect.y1 = def->nFrameHeight;
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci      vl_compositor_clear_layers(s);
119bf215546Sopenharmony_ci      vl_compositor_set_buffer_layer(s, compositor, 0, buf,
120bf215546Sopenharmony_ci              &src_rect, NULL, deinterlace);
121bf215546Sopenharmony_ci      vl_compositor_set_layer_dst_area(s, 0, &dst_rect);
122bf215546Sopenharmony_ci      vl_compositor_render(s, compositor, dst_surface[0], NULL, false);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci      priv->pipe->flush(priv->pipe, NULL, 0);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci      return;
127bf215546Sopenharmony_ci   }
128bf215546Sopenharmony_ci#endif
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   for (i = 0; i < 2 /* NV12 */; i++) {
131bf215546Sopenharmony_ci      if (!views[i]) continue;
132bf215546Sopenharmony_ci      width = def->nFrameWidth;
133bf215546Sopenharmony_ci      height = def->nFrameHeight;
134bf215546Sopenharmony_ci      vl_video_buffer_adjust_size(&width, &height, i,
135bf215546Sopenharmony_ci                                  pipe_format_to_chroma_format(buf->buffer_format),
136bf215546Sopenharmony_ci                                  buf->interlaced);
137bf215546Sopenharmony_ci      for (j = 0; j < views[i]->texture->array_size; ++j) {
138bf215546Sopenharmony_ci         struct pipe_box box = {0, 0, j, width, height, 1};
139bf215546Sopenharmony_ci         struct pipe_transfer *transfer;
140bf215546Sopenharmony_ci         uint8_t *map, *dst;
141bf215546Sopenharmony_ci         map = priv->pipe->texture_map(priv->pipe, views[i]->texture, 0,
142bf215546Sopenharmony_ci                  PIPE_MAP_READ, &box, &transfer);
143bf215546Sopenharmony_ci         if (!map)
144bf215546Sopenharmony_ci            return;
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci         dst = ((uint8_t*)output->pBuffer + output->nOffset) + j * def->nStride +
147bf215546Sopenharmony_ci               i * def->nFrameWidth * def->nFrameHeight;
148bf215546Sopenharmony_ci         util_copy_rect(dst,
149bf215546Sopenharmony_ci            views[i]->texture->format,
150bf215546Sopenharmony_ci            def->nStride * views[i]->texture->array_size, 0, 0,
151bf215546Sopenharmony_ci            box.width, box.height, map, transfer->stride, 0, 0);
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci         pipe_texture_unmap(priv->pipe, transfer);
154bf215546Sopenharmony_ci      }
155bf215546Sopenharmony_ci   }
156bf215546Sopenharmony_ci}
157