1/**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
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 THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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#if ENABLE_ST_OMX_TIZONIA
29#include <tizkernel.h>
30#endif
31
32#include "util/u_memory.h"
33#include "vl/vl_winsys.h"
34#include "vl/vl_video_buffer.h"
35#include "util/u_surface.h"
36
37#include "vid_dec_common.h"
38#include "vid_dec_h264_common.h"
39
40void vid_dec_NeedTarget(vid_dec_PrivateType *priv)
41{
42   struct pipe_video_buffer templat = {};
43   struct vl_screen *omx_screen;
44   struct pipe_screen *pscreen;
45
46   omx_screen = priv->screen;
47   assert(omx_screen);
48
49   pscreen = omx_screen->pscreen;
50   assert(pscreen);
51
52   if (!priv->target) {
53      memset(&templat, 0, sizeof(templat));
54
55      templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
56      templat.width = priv->codec->width;
57      templat.height = priv->codec->height;
58      templat.buffer_format = pscreen->get_video_param(
59            pscreen,
60            PIPE_VIDEO_PROFILE_UNKNOWN,
61            PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
62            PIPE_VIDEO_CAP_PREFERED_FORMAT
63      );
64      templat.interlaced = pscreen->get_video_param(
65          pscreen,
66          PIPE_VIDEO_PROFILE_UNKNOWN,
67          PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
68          PIPE_VIDEO_CAP_PREFERS_INTERLACED
69      );
70
71      priv->target = priv->pipe->create_video_buffer(priv->pipe, &templat);
72   }
73}
74
75void vid_dec_FillOutput(vid_dec_PrivateType *priv, struct pipe_video_buffer *buf,
76                        OMX_BUFFERHEADERTYPE* output)
77{
78#if ENABLE_ST_OMX_TIZONIA
79   tiz_port_t *out_port = tiz_krn_get_port(tiz_get_krn(handleOf(priv)),
80                                           OMX_VID_DEC_AVC_OUTPUT_PORT_INDEX);
81   OMX_VIDEO_PORTDEFINITIONTYPE *def = &out_port->portdef_.format.video;
82#else
83   omx_base_PortType *port = priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
84   OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
85#endif
86
87   struct pipe_sampler_view **views;
88   unsigned i, j;
89   unsigned width, height;
90
91   views = buf->get_sampler_view_planes(buf);
92
93#if ENABLE_ST_OMX_TIZONIA
94   if (!output->pBuffer) {
95      struct pipe_video_buffer *dst_buf = NULL;
96      struct pipe_surface **dst_surface = NULL;
97      struct u_rect src_rect;
98      struct u_rect dst_rect;
99      struct vl_compositor *compositor = &priv->compositor;
100      struct vl_compositor_state *s = &priv->cstate;
101      enum vl_compositor_deinterlace deinterlace = VL_COMPOSITOR_WEAVE;
102
103      dst_buf = util_hash_table_get(priv->video_buffer_map, output);
104      assert(dst_buf);
105
106      dst_surface = dst_buf->get_surfaces(dst_buf);
107      assert(views);
108
109      src_rect.x0 = 0;
110      src_rect.y0 = 0;
111      src_rect.x1 = def->nFrameWidth;
112      src_rect.y1 = def->nFrameHeight;
113
114      dst_rect.x0 = 0;
115      dst_rect.y0 = 0;
116      dst_rect.x1 = def->nFrameWidth;
117      dst_rect.y1 = def->nFrameHeight;
118
119      vl_compositor_clear_layers(s);
120      vl_compositor_set_buffer_layer(s, compositor, 0, buf,
121              &src_rect, NULL, deinterlace);
122      vl_compositor_set_layer_dst_area(s, 0, &dst_rect);
123      vl_compositor_render(s, compositor, dst_surface[0], NULL, false);
124
125      priv->pipe->flush(priv->pipe, NULL, 0);
126
127      return;
128   }
129#endif
130
131   for (i = 0; i < 2 /* NV12 */; i++) {
132      if (!views[i]) continue;
133      width = def->nFrameWidth;
134      height = def->nFrameHeight;
135      vl_video_buffer_adjust_size(&width, &height, i, buf->chroma_format, buf->interlaced);
136      for (j = 0; j < views[i]->texture->array_size; ++j) {
137         struct pipe_box box = {0, 0, j, width, height, 1};
138         struct pipe_transfer *transfer;
139         uint8_t *map, *dst;
140         map = priv->pipe->transfer_map(priv->pipe, views[i]->texture, 0,
141                  PIPE_TRANSFER_READ, &box, &transfer);
142         if (!map)
143            return;
144
145         dst = ((uint8_t*)output->pBuffer + output->nOffset) + j * def->nStride +
146               i * def->nFrameWidth * def->nFrameHeight;
147         util_copy_rect(dst,
148            views[i]->texture->format,
149            def->nStride * views[i]->texture->array_size, 0, 0,
150            box.width, box.height, map, transfer->stride, 0, 0);
151
152         pipe_transfer_unmap(priv->pipe, transfer);
153      }
154   }
155}
156