1 /**
2  * @copyright
3  *
4  *   Copyright (c) 2015, The Linux Foundation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright notice,
12  *     this list of conditions and the following disclaimer in the documentation
13  *     and/or other materials provided with the distribution.
14  *   * Neither the name of The Linux Foundation nor the names of its
15  *     contributors may be used to endorse or promote products derived from
16  *     this software without specific prior written permission.
17  *
18  *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
20  *   FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE DISCLAIMED.
21  *   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
22  *   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  *   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28  *   DAMAGE.
29  *
30  * @file
31  *
32  *   omx_swvdec.h
33  *
34  * @brief
35  *
36  *   OMX software video decoder component header.
37  */
38 
39 #ifndef _OMX_SWVDEC_H_
40 #define _OMX_SWVDEC_H_
41 
42 #include <pthread.h>
43 #include <semaphore.h>
44 
45 #include <linux/msm_ion.h>
46 #include <linux/msm_vidc_dec.h>
47 
48 #include "qc_omx_component.h"
49 
50 #include "omx_swvdec_utils.h"
51 
52 #include "swvdec_types.h"
53 
54 using namespace android;
55 
56 /// OMX SwVdec version date
57 #define OMX_SWVDEC_VERSION_DATE "2015-11-09T18:33:31+0530"
58 
59 #define OMX_SPEC_VERSION 0x00000101 ///< OMX specification version
60 
61 #define OMX_SWVDEC_NUM_INSTANCES 1 ///< number of OMX SwVdec instances
62 
63 // @todo Make this macro default and store in a variable?
64 #define OMX_SWVDEC_IP_BUFFER_COUNT 5 ///< OMX SwVdec input buffer count
65 
66 /// frame rate structure
67 typedef struct {
68     unsigned int fps_numerator;   ///< fps numerator
69     unsigned int fps_denominator; ///< fps denominator
70 } FRAME_RATE;
71 
72 #define DEFAULT_FPS_NUMERATOR   30 ///< default fps numerator
73 #define DEFAULT_FPS_DENOMINATOR  1 ///< default fps denominator
74 
75 /// frame dimensions structure
76 typedef struct {
77     unsigned int width;  ///< frame width
78     unsigned int height; ///< frame height
79 } FRAME_DIMENSIONS;
80 
81 /// frame attributes structure
82 typedef struct {
83     unsigned int stride;    ///< frame stride
84     unsigned int scanlines; ///< frame scanlines
85     unsigned int size;      ///< frame size
86 } FRAME_ATTRIBUTES;
87 
88 /// asynchronous thread structure
89 typedef struct {
90     sem_t     sem_thread_created; ///< thread created semaphore
91     sem_t     sem_event;          ///< event semaphore
92     pthread_t handle;             ///< thread handle
93     bool      created;            ///< thread created?
94     bool      exit;               ///< thread exit variable
95 } ASYNC_THREAD;
96 
97 /// @cond
98 
99 struct vdec_ion {
100     int                        ion_fd_device;
101     struct ion_fd_data         ion_fd_data;
102     struct ion_allocation_data ion_alloc_data;
103 };
104 
105 typedef struct {
106     OMX_BUFFERHEADERTYPE      buffer_header;
107     struct vdec_ion           ion_info;
108     struct vdec_bufferpayload buffer_payload;
109     SWVDEC_BUFFER             buffer_swvdec;
110     bool                      buffer_populated;
111 } OMX_SWVDEC_BUFFER_INFO;
112 
113 /// @endcond
114 
115 /// port structure
116 typedef struct {
117     OMX_PARAM_PORTDEFINITIONTYPE def;                 ///< definition
118     OMX_BOOL                     enabled;             ///< enabled?
119     OMX_BOOL                     populated;           ///< populated?
120     OMX_BOOL                     unpopulated;         ///< unpopulated?
121     OMX_BOOL                     flush_inprogress;    ///< flush inprogress?
122     unsigned int                 num_pending_buffers; ///< # of pending buffers
123 } OMX_SWVDEC_PORT;
124 
125 /// meta_buffer information structure
126 typedef struct {
127     unsigned int fd;        ///< file descriptor
128     unsigned int dup_fd;    ///< duplicate file descriptor
129     unsigned int offset;    ///< offset
130     unsigned int ref_count; ///< reference count
131 } OMX_SWVDEC_META_BUFFER_INFO;
132 
133 #define DEFAULT_FRAME_WIDTH  1920 ///< default frame width
134 #define DEFAULT_FRAME_HEIGHT 1088 ///< default frame height
135 
136 #define DEFAULT_ALIGNMENT_STRIDE      128 ///< default stride alignment
137 #define DEFAULT_ALIGNMENT_SCANLINES_Y  32 ///< default  Y scanlines alignment
138 #define DEFAULT_ALIGNMENT_SCANLINES_UV 16 ///< default UV scanlines alignment
139 #define DEFAULT_ALIGNMENT_SIZE       4096 ///< default size alignment
140 
141 #define MAX(x, y) (((x) > (y)) ? (x) : (y)) ///< maximum
142 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) ///< minimum
143 #define ALIGN(x, y) (((x) + ((y) - 1)) & (~((y) - 1)))
144                                   ///< align 'x' to next highest multiple of 'y'
145 
146 /// macro to print 'command type' string
147 #define OMX_COMMANDTYPE_STRING(x)                                 \
148     ((x == OMX_CommandStateSet) ? "OMX_CommandStateSet" :         \
149      ((x == OMX_CommandFlush) ? "OMX_CommandFlush" :              \
150       ((x == OMX_CommandPortDisable) ? "OMX_CommandPortDisable" : \
151        ((x == OMX_CommandPortEnable) ? "OMX_CommandPortEnable" :  \
152         "unknown"))))
153 
154 /// macro to print 'state type' string
155 #define OMX_STATETYPE_STRING(x)                                            \
156     ((x == OMX_StateInvalid) ? "OMX_StateInvalid" :                        \
157      ((x == OMX_StateLoaded) ? "OMX_StateLoaded" :                         \
158       ((x == OMX_StateIdle) ? "OMX_StateIdle" :                            \
159        ((x == OMX_StateExecuting) ? "OMX_StateExecuting" :                 \
160         ((x == OMX_StatePause) ? "OMX_StatePause" :                        \
161          ((x == OMX_StateWaitForResources) ? "OMX_StateWaitForResources" : \
162           "unknown"))))))
163 
164 enum {
165     OMX_CORE_PORT_INDEX_IP = 0, ///<  input port index
166     OMX_CORE_PORT_INDEX_OP = 1  ///< output port index
167 };
168 
169 extern "C" {
170     OMX_API void *get_omx_component_factory_fn(void);
171 };
172 
173 /// OMX SwVdec component class; derived from QC OMX component base class
174 class omx_swvdec : public qc_omx_component
175 {
176 public:
177 
178     omx_swvdec();
179 
180     virtual ~omx_swvdec();
181 
182     // derived class versions of base class pure virtual functions
183 
184     OMX_ERRORTYPE component_init(OMX_STRING cmp_name);
185     OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE cmp_handle);
186     OMX_ERRORTYPE get_component_version(OMX_HANDLETYPE   cmp_handle,
187                                         OMX_STRING       cmp_name,
188                                         OMX_VERSIONTYPE *p_cmp_version,
189                                         OMX_VERSIONTYPE *p_spec_version,
190                                         OMX_UUIDTYPE    *p_cmp_UUID);
191     OMX_ERRORTYPE send_command(OMX_HANDLETYPE  cmp_handle,
192                                OMX_COMMANDTYPE cmd,
193                                OMX_U32         param,
194                                OMX_PTR         p_cmd_data);
195     OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE cmp_handle,
196                                 OMX_INDEXTYPE  param_index,
197                                 OMX_PTR        p_param_data);
198     OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE cmp_handle,
199                                 OMX_INDEXTYPE  param_index,
200                                 OMX_PTR        p_param_data);
201     OMX_ERRORTYPE get_config(OMX_HANDLETYPE cmp_handle,
202                              OMX_INDEXTYPE  config_index,
203                              OMX_PTR        p_config_data);
204     OMX_ERRORTYPE set_config(OMX_HANDLETYPE cmp_handle,
205                              OMX_INDEXTYPE  config_index,
206                              OMX_PTR        p_config_data);
207     OMX_ERRORTYPE get_extension_index(OMX_HANDLETYPE cmp_handle,
208                                       OMX_STRING     param_name,
209                                       OMX_INDEXTYPE *p_index_type);
210     OMX_ERRORTYPE get_state(OMX_HANDLETYPE cmp_handle,
211                             OMX_STATETYPE *p_state);
212     OMX_ERRORTYPE component_tunnel_request(OMX_HANDLETYPE       cmp_handle,
213                                            OMX_U32              port,
214                                            OMX_HANDLETYPE       peer_component,
215                                            OMX_U32              peer_port,
216                                            OMX_TUNNELSETUPTYPE *p_tunnel_setup);
217     OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE         cmp_handle,
218                              OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
219                              OMX_U32                port,
220                              OMX_PTR                p_app_data,
221                              OMX_U32                bytes,
222                              OMX_U8                *p_buffer);
223     OMX_ERRORTYPE allocate_buffer(OMX_HANDLETYPE         cmp_handle,
224                                   OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
225                                   OMX_U32                port,
226                                   OMX_PTR                p_app_data,
227                                   OMX_U32                bytes);
228     OMX_ERRORTYPE free_buffer(OMX_HANDLETYPE        cmp_handle,
229                               OMX_U32               port,
230                               OMX_BUFFERHEADERTYPE *p_buffer);
231     OMX_ERRORTYPE empty_this_buffer(OMX_HANDLETYPE        cmp_handle,
232                                     OMX_BUFFERHEADERTYPE *p_buffer_hdr);
233     OMX_ERRORTYPE fill_this_buffer(OMX_HANDLETYPE        cmp_handle,
234                                    OMX_BUFFERHEADERTYPE *p_buffer_hdr);
235     OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE    cmp_handle,
236                                 OMX_CALLBACKTYPE *p_callbacks,
237                                 OMX_PTR           p_app_data);
238     OMX_ERRORTYPE use_EGL_image(OMX_HANDLETYPE         cmp_handle,
239                                 OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
240                                 OMX_U32                port,
241                                 OMX_PTR                p_app_data,
242                                 void                  *egl_image);
243     OMX_ERRORTYPE component_role_enum(OMX_HANDLETYPE cmp_handle,
244                                       OMX_U8        *p_role,
245                                       OMX_U32        index);
246 
247     // SwVdec callback functions
248 
249     static SWVDEC_STATUS swvdec_empty_buffer_done_callback(
250         SWVDEC_HANDLE  swvdec_handle,
251         SWVDEC_BUFFER *p_buffer_ip,
252         void          *p_client_handle);
253     static SWVDEC_STATUS swvdec_fill_buffer_done_callback(
254         SWVDEC_HANDLE  swvdec_handle,
255         SWVDEC_BUFFER *p_buffer_op,
256         void          *p_client_handle);
257     static SWVDEC_STATUS swvdec_event_handler_callback(
258         SWVDEC_HANDLE swvdec_handle,
259         SWVDEC_EVENT  event,
260         void         *p_data,
261         void         *p_client_handle);
262 
263 private:
264 
265     OMX_STATETYPE m_state; ///< component state
266 
267     unsigned int m_status_flags; ///< status flags
268 
269     char m_cmp_name[OMX_MAX_STRINGNAME_SIZE];  ///< component name
270     char m_role_name[OMX_MAX_STRINGNAME_SIZE]; ///< component role name
271 
272     SWVDEC_CODEC  m_swvdec_codec;   ///< SwVdec codec type
273     SWVDEC_HANDLE m_swvdec_handle;  ///< SwVdec handle
274     bool          m_swvdec_created; ///< SwVdec created?
275 
276     OMX_VIDEO_CODINGTYPE m_omx_video_codingtype; ///< OMX video coding type
277     OMX_COLOR_FORMATTYPE m_omx_color_formattype; ///< OMX color format type
278 
279     FRAME_RATE       m_frame_rate;       ///< frame rate
280     FRAME_DIMENSIONS m_frame_dimensions; ///< frame dimensions
281     FRAME_ATTRIBUTES m_frame_attributes; ///< frame attributes
282 
283     ASYNC_THREAD m_async_thread; ///< asynchronous thread
284 
285     omx_swvdec_queue m_queue_command; ///< command queue
286     omx_swvdec_queue m_queue_port_ip; ///<  input port queue for ETBs & EBDs
287     omx_swvdec_queue m_queue_port_op; ///< output port queue for FTBs & FBDs
288 
289     OMX_SWVDEC_PORT m_port_ip; ///<  input port
290     OMX_SWVDEC_PORT m_port_op; ///< output port
291 
292     OMX_CALLBACKTYPE m_callback; ///< IL client callback structure
293     OMX_PTR          m_app_data; ///< IL client app data pointer
294 
295     OMX_PRIORITYMGMTTYPE m_prio_mgmt; ///< priority management
296 
297     bool m_sync_frame_decoding_mode; ///< sync frame decoding mode enabled?
298     bool m_android_native_buffers;   ///< android native buffers enabled?
299     bool m_meta_buffer_mode;         ///< meta buffer mode enabled?
300 
301     bool m_port_reconfig_inprogress; ///< port reconfiguration in progress?
302 
303     sem_t m_sem_cmd; ///< semaphore for command processing
304 
305     OMX_SWVDEC_BUFFER_INFO *m_buffer_array_ip; ///<  input buffer info array
306     OMX_SWVDEC_BUFFER_INFO *m_buffer_array_op; ///< output buffer info array
307 
308     OMX_SWVDEC_META_BUFFER_INFO *m_meta_buffer_array; ///< metabuffer info array
309     pthread_mutex_t              m_meta_buffer_array_mutex;
310                                             ///< mutex for metabuffer info array
311 
312     omx_swvdec_ts_list m_ts_list; ///< timestamp list
313 
314     omx_swvdec_diag m_diag; ///< diagnostics class variable
315 
316     OMX_ERRORTYPE set_frame_dimensions(unsigned int width,
317                                        unsigned int height);
318     OMX_ERRORTYPE set_frame_attributes(unsigned int alignment_stride,
319                                        unsigned int alignment_scanlines_y,
320                                        unsigned int alignment_scanlines_uv,
321                                        unsigned int alignment_size);
322 
323     OMX_ERRORTYPE get_video_port_format(
324         OMX_VIDEO_PARAM_PORTFORMATTYPE *p_port_format);
325     OMX_ERRORTYPE set_video_port_format(
326         OMX_VIDEO_PARAM_PORTFORMATTYPE *p_port_format);
327 
328     OMX_ERRORTYPE get_port_definition(OMX_PARAM_PORTDEFINITIONTYPE *p_port_def);
329     OMX_ERRORTYPE set_port_definition(OMX_PARAM_PORTDEFINITIONTYPE *p_port_def);
330 
331     OMX_ERRORTYPE get_supported_profilelevel(
332         OMX_VIDEO_PARAM_PROFILELEVELTYPE *p_profilelevel);
333 
334     OMX_ERRORTYPE describe_color_format(DescribeColorFormatParams *p_params);
335 
336     OMX_ERRORTYPE set_port_definition_qcom(
337         OMX_QCOM_PARAM_PORTDEFINITIONTYPE *p_port_def);
338 
339     // functions to set SwVdec properties with OMX component properties
340 
341     OMX_ERRORTYPE set_frame_dimensions_swvdec();
342     OMX_ERRORTYPE set_frame_attributes_swvdec();
343 
344     // functions to get SwVdec properties and set OMX component properties
345 
346     OMX_ERRORTYPE get_frame_dimensions_swvdec();
347     OMX_ERRORTYPE get_frame_attributes_swvdec();
348     OMX_ERRORTYPE get_buffer_requirements_swvdec(unsigned int port_index);
349 
350     // buffer allocation & de-allocation functions
351     OMX_ERRORTYPE buffer_allocate_ip(OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
352                                      OMX_PTR                p_app_data,
353                                      OMX_U32                size);
354     OMX_ERRORTYPE buffer_allocate_op(OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
355                                      OMX_PTR                p_app_data,
356                                      OMX_U32                size);
357     OMX_ERRORTYPE buffer_allocate_ip_info_array();
358     OMX_ERRORTYPE buffer_allocate_op_info_array();
359     OMX_ERRORTYPE buffer_use_op(OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
360                                 OMX_PTR                p_app_data,
361                                 OMX_U32                size,
362                                 OMX_U8                *p_buffer);
363     OMX_ERRORTYPE buffer_deallocate_ip(OMX_BUFFERHEADERTYPE *p_buffer_hdr);
364     OMX_ERRORTYPE buffer_deallocate_op(OMX_BUFFERHEADERTYPE *p_buffer_hdr);
365     void          buffer_deallocate_ip_info_array();
366     void          buffer_deallocate_op_info_array();
367 
368     OMX_ERRORTYPE meta_buffer_array_allocate();
369     void          meta_buffer_array_deallocate();
370     void          meta_buffer_ref_add(unsigned int index,
371                                       unsigned int fd,
372                                       unsigned int offset);
373     void          meta_buffer_ref_remove(unsigned int fd, unsigned int offset);
374 
375     OMX_BOOL port_ip_populated();
376     OMX_BOOL port_op_populated();
377 
378     OMX_ERRORTYPE flush(unsigned int port_index);
379 
380     int  ion_memory_alloc_map(struct ion_allocation_data *p_alloc_data,
381                               struct ion_fd_data         *p_fd_data,
382                               OMX_U32                     size,
383                               OMX_U32                     alignment);
384     void ion_memory_free(struct vdec_ion *p_ion_buf_info);
385 
386     // component callback functions
387 
388     void swvdec_empty_buffer_done(SWVDEC_BUFFER *p_buffer_ip);
389     void swvdec_fill_buffer_done(SWVDEC_BUFFER *p_buffer_op);
390     void swvdec_event_handler(SWVDEC_EVENT event, void *p_data);
391 
392     OMX_ERRORTYPE retval_swvdec2omx(SWVDEC_STATUS retval_swvdec);
393 
394     // status bits for pending events
395     enum {
396         PENDING_STATE_LOADED_TO_IDLE,    ///< loaded to idle state
397         PENDING_STATE_EXECUTING_TO_IDLE, ///< executing to idle state
398         PENDING_STATE_IDLE_TO_LOADED,    ///< idle to loaded state
399         PENDING_PORT_ENABLE_IP,          ///< enablement of ip port
400         PENDING_PORT_ENABLE_OP,          ///< enablement of op port
401         PENDING_PORT_DISABLE_IP,         ///< disablement of ip port
402         PENDING_PORT_DISABLE_OP,         ///< disablement of op port
403         PENDING_PORT_FLUSH_IP,           ///< flush of ip port
404         PENDING_PORT_FLUSH_OP            ///< flush of op port
405     };
406 
407     // events raised internally
408     enum {
409         OMX_SWVDEC_EVENT_CMD,           ///< command event
410         OMX_SWVDEC_EVENT_CMD_ACK,       ///< command acknowledgement
411         OMX_SWVDEC_EVENT_ERROR,         ///< error event
412         OMX_SWVDEC_EVENT_ETB,           ///< ETB event
413         OMX_SWVDEC_EVENT_EBD,           ///< EBD event
414         OMX_SWVDEC_EVENT_FTB,           ///< FTB event
415         OMX_SWVDEC_EVENT_FBD,           ///< FBD event
416         OMX_SWVDEC_EVENT_EOS,           ///< EOS event
417         OMX_SWVDEC_EVENT_FLUSH_PORT_IP, ///< flush ip port event
418         OMX_SWVDEC_EVENT_FLUSH_PORT_OP, ///< flush op port event
419         OMX_SWVDEC_EVENT_PORT_RECONFIG  ///< port reconfig event
420     };
421 
422     OMX_ERRORTYPE async_thread_create();
423     void          async_thread_destroy();
424 
425     static void   async_thread(void *p_cmp);
426 
427     bool          async_post_event(unsigned long event_id,
428                                    unsigned long event_param1,
429                                    unsigned long event_param2);
430 
431     static void   async_process_event(void *p_cmp);
432 
433     OMX_ERRORTYPE async_process_event_cmd(OMX_COMMANDTYPE cmd, OMX_U32 param);
434     OMX_ERRORTYPE async_process_event_cmd_ack(OMX_COMMANDTYPE cmd,
435                                               OMX_U32         param);
436     OMX_ERRORTYPE async_process_event_error(OMX_ERRORTYPE error_code);
437     OMX_ERRORTYPE async_process_event_cmd_state_set(bool         *p_cmd_ack,
438                                                     OMX_STATETYPE state_new);
439     OMX_ERRORTYPE async_process_event_cmd_flush(unsigned int port_index);
440     OMX_ERRORTYPE async_process_event_cmd_port_disable(
441         bool         *p_cmd_ack,
442         unsigned int  port_index);
443     OMX_ERRORTYPE async_process_event_cmd_port_enable(bool         *p_cmd_ack,
444                                                       unsigned int  port_index);
445     OMX_ERRORTYPE async_process_event_etb(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
446                                           unsigned int          index);
447     OMX_ERRORTYPE async_process_event_ftb(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
448                                           unsigned int          index);
449     OMX_ERRORTYPE async_process_event_ebd(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
450                                           unsigned int          index);
451     OMX_ERRORTYPE async_process_event_fbd(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
452                                           unsigned int          index);
453     OMX_ERRORTYPE async_process_event_eos();
454     OMX_ERRORTYPE async_process_event_flush_port_ip();
455     OMX_ERRORTYPE async_process_event_flush_port_op();
456     OMX_ERRORTYPE async_process_event_port_reconfig();
457 };
458 
459 #endif // #ifndef _OMX_SWVDEC_H_
460