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.width = priv->codec->width;
56      templat.height = priv->codec->height;
57      templat.buffer_format = pscreen->get_video_param(
58            pscreen,
59            priv->profile,
60            PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
61            PIPE_VIDEO_CAP_PREFERED_FORMAT
62      );
63      templat.interlaced = pscreen->get_video_param(
64          pscreen,
65          priv->profile,
66          PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
67          PIPE_VIDEO_CAP_PREFERS_INTERLACED
68      );
69
70      priv->target = priv->pipe->create_video_buffer(priv->pipe, &templat);
71   }
72}
73
74void vid_dec_FillOutput(vid_dec_PrivateType *priv, struct pipe_video_buffer *buf,
75                        OMX_BUFFERHEADERTYPE* output)
76{
77#if ENABLE_ST_OMX_TIZONIA
78   tiz_port_t *out_port = tiz_krn_get_port(tiz_get_krn(handleOf(priv)),
79                                           OMX_VID_DEC_AVC_OUTPUT_PORT_INDEX);
80   OMX_VIDEO_PORTDEFINITIONTYPE *def = &out_port->portdef_.format.video;
81#else
82   omx_base_PortType *port = priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
83   OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
84#endif
85
86   struct pipe_sampler_view **views;
87   unsigned i, j;
88   unsigned width, height;
89
90   views = buf->get_sampler_view_planes(buf);
91
92#if ENABLE_ST_OMX_TIZONIA
93   if (!output->pBuffer) {
94      struct pipe_video_buffer *dst_buf = NULL;
95      struct pipe_surface **dst_surface = NULL;
96      struct u_rect src_rect;
97      struct u_rect dst_rect;
98      struct vl_compositor *compositor = &priv->compositor;
99      struct vl_compositor_state *s = &priv->cstate;
100      enum vl_compositor_deinterlace deinterlace = VL_COMPOSITOR_WEAVE;
101
102      dst_buf = util_hash_table_get(priv->video_buffer_map, output);
103      assert(dst_buf);
104
105      dst_surface = dst_buf->get_surfaces(dst_buf);
106      assert(views);
107
108      src_rect.x0 = 0;
109      src_rect.y0 = 0;
110      src_rect.x1 = def->nFrameWidth;
111      src_rect.y1 = def->nFrameHeight;
112
113      dst_rect.x0 = 0;
114      dst_rect.y0 = 0;
115      dst_rect.x1 = def->nFrameWidth;
116      dst_rect.y1 = def->nFrameHeight;
117
118      vl_compositor_clear_layers(s);
119      vl_compositor_set_buffer_layer(s, compositor, 0, buf,
120              &src_rect, NULL, deinterlace);
121      vl_compositor_set_layer_dst_area(s, 0, &dst_rect);
122      vl_compositor_render(s, compositor, dst_surface[0], NULL, false);
123
124      priv->pipe->flush(priv->pipe, NULL, 0);
125
126      return;
127   }
128#endif
129
130   for (i = 0; i < 2 /* NV12 */; i++) {
131      if (!views[i]) continue;
132      width = def->nFrameWidth;
133      height = def->nFrameHeight;
134      vl_video_buffer_adjust_size(&width, &height, i,
135                                  pipe_format_to_chroma_format(buf->buffer_format),
136                                  buf->interlaced);
137      for (j = 0; j < views[i]->texture->array_size; ++j) {
138         struct pipe_box box = {0, 0, j, width, height, 1};
139         struct pipe_transfer *transfer;
140         uint8_t *map, *dst;
141         map = priv->pipe->texture_map(priv->pipe, views[i]->texture, 0,
142                  PIPE_MAP_READ, &box, &transfer);
143         if (!map)
144            return;
145
146         dst = ((uint8_t*)output->pBuffer + output->nOffset) + j * def->nStride +
147               i * def->nFrameWidth * def->nFrameHeight;
148         util_copy_rect(dst,
149            views[i]->texture->format,
150            def->nStride * views[i]->texture->array_size, 0, 0,
151            box.width, box.height, map, transfer->stride, 0, 0);
152
153         pipe_texture_unmap(priv->pipe, transfer);
154      }
155   }
156}
157