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 #include <assert.h>
29 #include <string.h>
30 #include <limits.h>
31 
32 #include <tizplatform.h>
33 #include <tizkernel.h>
34 
35 #include "vl/vl_winsys.h"
36 
37 #include "h264e.h"
38 #include "h264einport.h"
39 #include "h264einport_decls.h"
40 #include "vid_enc_common.h"
41 
enc_AllocateBackTexture(OMX_HANDLETYPE ap_hdl,OMX_U32 idx,struct pipe_resource ** resource,struct pipe_transfer ** transfer,OMX_U8 ** map)42 static OMX_ERRORTYPE enc_AllocateBackTexture(OMX_HANDLETYPE ap_hdl,
43                                              OMX_U32 idx,
44                                              struct pipe_resource **resource,
45                                              struct pipe_transfer **transfer,
46                                              OMX_U8 **map)
47 {
48    vid_enc_PrivateType * priv = tiz_get_prc(ap_hdl);
49    tiz_port_t * port = tiz_krn_get_port(tiz_get_krn(ap_hdl), idx);
50    struct pipe_resource buf_templ;
51    struct pipe_box box = {};
52    OMX_U8 *ptr;
53 
54    memset(&buf_templ, 0, sizeof buf_templ);
55    buf_templ.target = PIPE_TEXTURE_2D;
56    buf_templ.format = PIPE_FORMAT_I8_UNORM;
57    buf_templ.bind = PIPE_BIND_LINEAR;
58    buf_templ.usage = PIPE_USAGE_STAGING;
59    buf_templ.flags = 0;
60    buf_templ.width0 = port->portdef_.format.video.nFrameWidth;
61    buf_templ.height0 = port->portdef_.format.video.nFrameHeight * 3 / 2;
62    buf_templ.depth0 = 1;
63    buf_templ.array_size = 1;
64 
65    *resource = priv->s_pipe->screen->resource_create(priv->s_pipe->screen, &buf_templ);
66    if (!*resource)
67       return OMX_ErrorInsufficientResources;
68 
69    box.width = (*resource)->width0;
70    box.height = (*resource)->height0;
71    box.depth = (*resource)->depth0;
72    ptr = priv->s_pipe->transfer_map(priv->s_pipe, *resource, 0, PIPE_MAP_WRITE, &box, transfer);
73    if (map)
74       *map = ptr;
75 
76    return OMX_ErrorNone;
77 }
78 
79 /*
80  * h264einport class
81  */
82 
h264e_inport_ctor(void * ap_obj,va_list * app)83 static void * h264e_inport_ctor(void * ap_obj, va_list * app)
84 {
85    return super_ctor(typeOf(ap_obj, "h264einport"), ap_obj, app);
86 }
87 
h264e_inport_dtor(void * ap_obj)88 static void * h264e_inport_dtor(void * ap_obj)
89 {
90    return super_dtor(typeOf(ap_obj, "h264einport"), ap_obj);
91 }
92 
93 /*
94  * from tiz_api
95  */
96 
h264e_inport_AllocateBuffer(const void * ap_obj,OMX_HANDLETYPE ap_hdl,OMX_BUFFERHEADERTYPE ** buf,OMX_U32 idx,OMX_PTR private,OMX_U32 size)97 static OMX_ERRORTYPE h264e_inport_AllocateBuffer(const void * ap_obj, OMX_HANDLETYPE ap_hdl,
98                                                  OMX_BUFFERHEADERTYPE ** buf, OMX_U32 idx,
99                                                  OMX_PTR private, OMX_U32 size)
100 {
101    struct input_buf_private *inp;
102    OMX_ERRORTYPE r;
103 
104    r = super_UseBuffer(typeOf(ap_obj, "h264einport"), ap_obj, ap_hdl,
105                        buf, idx, private, size, NULL);
106    if (r)
107      return r;
108 
109    inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
110    if (!inp) {
111      super_FreeBuffer(typeOf(ap_obj, "h264einport"), ap_obj, ap_hdl, idx, *buf);
112      return OMX_ErrorInsufficientResources;
113    }
114 
115    list_inithead(&inp->tasks);
116 
117    r = enc_AllocateBackTexture(ap_hdl, idx, &inp->resource, &inp->transfer, &(*buf)->pBuffer);
118 
119    if (r) {
120      FREE(inp);
121      super_FreeBuffer(typeOf(ap_obj, "h264einport"), ap_obj, ap_hdl, idx, *buf);
122      return r;
123    }
124 
125    return OMX_ErrorNone;
126 }
127 
h264e_inport_UseBuffer(const void * ap_obj,OMX_HANDLETYPE ap_hdl,OMX_BUFFERHEADERTYPE ** buf,OMX_U32 idx,OMX_PTR private,OMX_U32 size,OMX_U8 * mem)128 static OMX_ERRORTYPE h264e_inport_UseBuffer(const void * ap_obj, OMX_HANDLETYPE ap_hdl,
129                                             OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx,
130                                             OMX_PTR private, OMX_U32 size, OMX_U8 *mem)
131 {
132    struct input_buf_private *inp;
133    OMX_ERRORTYPE r;
134 
135    r = super_UseBuffer(typeOf(ap_obj, "h264einport"), ap_obj, ap_hdl,
136                        buf, idx, private, size, mem);
137    if (r)
138      return r;
139 
140    inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
141    if (!inp) {
142      super_FreeBuffer(typeOf(ap_obj, "h264einport"), ap_obj, ap_hdl, idx, *buf);
143      return OMX_ErrorInsufficientResources;
144    }
145 
146    list_inithead(&inp->tasks);
147 
148    return OMX_ErrorNone;
149 }
150 
h264e_inport_FreeBuffer(const void * ap_obj,OMX_HANDLETYPE ap_hdl,OMX_U32 idx,OMX_BUFFERHEADERTYPE * buf)151 static OMX_ERRORTYPE h264e_inport_FreeBuffer(const void * ap_obj, OMX_HANDLETYPE ap_hdl,
152                                              OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf)
153 {
154    vid_enc_PrivateType *priv = tiz_get_prc(ap_hdl);
155    struct input_buf_private *inp = buf->pInputPortPrivate;
156 
157    if (inp) {
158      enc_ReleaseTasks(&inp->tasks);
159      if (inp->transfer)
160        pipe_transfer_unmap(priv->s_pipe, inp->transfer);
161      pipe_resource_reference(&inp->resource, NULL);
162      FREE(inp);
163    }
164 
165    return super_FreeBuffer(typeOf(ap_obj, "h264einport"), ap_obj, ap_hdl, idx, buf);
166 }
167 
168 /*
169  * h264einport_class
170  */
171 
h264e_inport_class_ctor(void * ap_obj,va_list * app)172 static void * h264e_inport_class_ctor(void * ap_obj, va_list * app)
173 {
174    /* NOTE: Class methods might be added in the future. None for now. */
175    return super_ctor (typeOf (ap_obj, "h264einport_class"), ap_obj, app);
176 }
177 
178 /*
179  * initialization
180  */
181 
h264e_inport_class_init(void * ap_tos,void * ap_hdl)182 void * h264e_inport_class_init(void * ap_tos, void * ap_hdl)
183 {
184    void * tizvideoport = tiz_get_type(ap_hdl, "tizvideoport");
185    void * h264einport_class
186      = factory_new(classOf(tizvideoport), "h264einport_class",
187                    classOf(tizvideoport), sizeof(h264e_inport_class_t),
188                    ap_tos, ap_hdl, ctor, h264e_inport_class_ctor, 0);
189    return h264einport_class;
190 }
191 
h264e_inport_init(void * ap_tos,void * ap_hdl)192 void * h264e_inport_init(void * ap_tos, void * ap_hdl)
193 {
194    void * tizvideoport = tiz_get_type (ap_hdl, "tizvideoport");
195    void * h264einport_class = tiz_get_type (ap_hdl, "h264einport_class");
196    void * h264einport = factory_new
197      /* TIZ_CLASS_COMMENT: class type, class name, parent, size */
198      (h264einport_class, "h264einport", tizvideoport,
199       sizeof (h264e_inport_t),
200       /* TIZ_CLASS_COMMENT: class constructor */
201       ap_tos, ap_hdl,
202       /* TIZ_CLASS_COMMENT: class constructor */
203       ctor, h264e_inport_ctor,
204       /* TIZ_CLASS_COMMENT: class destructor */
205       dtor, h264e_inport_dtor,
206       /* TIZ_CLASS_COMMENT: */
207       tiz_api_AllocateBuffer, h264e_inport_AllocateBuffer,
208       /* TIZ_CLASS_COMMENT: */
209       tiz_api_UseBuffer, h264e_inport_UseBuffer,
210       /* TIZ_CLASS_COMMENT: */
211       tiz_api_FreeBuffer, h264e_inport_FreeBuffer,
212       /* TIZ_CLASS_COMMENT: stop value*/
213       0);
214 
215    return h264einport;
216 }
217