• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @copyright
3  *
4  *   Copyright (c) 2015-2017, 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 //#undef NDEBUG // uncomment to enable assertions
43 
44 #include <pthread.h>
45 #include <semaphore.h>
46 
47 #include <linux/msm_ion.h>
48 
49 #include "qc_omx_component.h"
50 
51 #include "omx_swvdec_utils.h"
52 
53 #include "swvdec_types.h"
54 
55 using namespace android;
56 
57 /// OMX SwVdec version date
58 #define OMX_SWVDEC_VERSION_DATE "2016-10-24T17:37:33+0530"
59 
60 #define OMX_SPEC_VERSION 0x00000101 ///< OMX specification version
61 
62 #define OMX_SWVDEC_NUM_INSTANCES 1 ///< number of OMX SwVdec instances
63 
64 #define OMX_SWVDEC_IP_BUFFER_COUNT_MIN 5 ///< OMX SwVdec minimum ip buffer count
65 
66 #define OMX_SWVDEC_MAX_FRAMES_PER_ETB 2 ///< maximum number of frames per ETB
67 
68 /// frame dimensions structure
69 typedef struct {
70     unsigned int width;  ///< frame width
71     unsigned int height; ///< frame height
72 } FRAME_DIMENSIONS;
73 
74 /// frame attributes structure
75 typedef struct {
76     unsigned int stride;    ///< frame stride
77     unsigned int scanlines; ///< frame scanlines
78     unsigned int size;      ///< frame size
79 } FRAME_ATTRIBUTES;
80 
81 /// asynchronous thread structure
82 typedef struct {
83     sem_t     sem_thread_created; ///< thread created semaphore
84     sem_t     sem_event;          ///< event semaphore
85     pthread_t handle;             ///< thread handle
86     bool      created;            ///< thread created?
87     bool      exit;               ///< thread exit variable
88 } ASYNC_THREAD;
89 
90 /// @cond
91 
92 struct vdec_ion {
93     int                        ion_fd_device;
94     struct ion_fd_data         ion_fd_data;
95     struct ion_allocation_data ion_alloc_data;
96 };
97 
98 struct vdec_bufferpayload {
99 	void *bufferaddr;
100 	size_t buffer_len;
101 	int pmem_fd;
102 	size_t offset;
103 	size_t mmaped_size;
104 };
105 
106 typedef struct {
107     OMX_BUFFERHEADERTYPE      buffer_header;
108     struct vdec_ion           ion_info;
109     struct vdec_bufferpayload buffer_payload;
110     SWVDEC_BUFFER             buffer_swvdec;
111     bool                      buffer_populated;
112     unsigned int              split_count;
113 } OMX_SWVDEC_BUFFER_INFO;
114 
115 /// @endcond
116 
117 /// port structure
118 typedef struct {
119     OMX_PARAM_PORTDEFINITIONTYPE def;                 ///< definition
120     OMX_BOOL                     enabled;             ///< enabled?
121     OMX_BOOL                     populated;           ///< populated?
122     OMX_BOOL                     unpopulated;         ///< unpopulated?
123     OMX_BOOL                     flush_inprogress;    ///< flush inprogress?
124     unsigned int                 num_pending_buffers; ///< # of pending buffers
125 } OMX_SWVDEC_PORT;
126 
127 /// meta_buffer information structure
128 typedef struct {
129     int fd;        ///< file descriptor
130     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 1080 ///< default frame height
135 
136 #define MAX(x, y) (((x) > (y)) ? (x) : (y)) ///< maximum
137 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) ///< minimum
138 #define ALIGN(x, y) (((x) + ((y) - 1)) & (~((y) - 1)))
139                                   ///< align 'x' to next highest multiple of 'y'
140 
141 /// macro to print 'command type' string
142 #define OMX_COMMANDTYPE_STRING(x)                                 \
143     ((x == OMX_CommandStateSet) ? "OMX_CommandStateSet" :         \
144      ((x == OMX_CommandFlush) ? "OMX_CommandFlush" :              \
145       ((x == OMX_CommandPortDisable) ? "OMX_CommandPortDisable" : \
146        ((x == OMX_CommandPortEnable) ? "OMX_CommandPortEnable" :  \
147         "unknown"))))
148 
149 /// macro to print 'state type' string
150 #define OMX_STATETYPE_STRING(x)                                            \
151     ((x == OMX_StateInvalid) ? "OMX_StateInvalid" :                        \
152      ((x == OMX_StateLoaded) ? "OMX_StateLoaded" :                         \
153       ((x == OMX_StateIdle) ? "OMX_StateIdle" :                            \
154        ((x == OMX_StateExecuting) ? "OMX_StateExecuting" :                 \
155         ((x == OMX_StatePause) ? "OMX_StatePause" :                        \
156          ((x == OMX_StateWaitForResources) ? "OMX_StateWaitForResources" : \
157           "unknown"))))))
158 
159 enum {
160     OMX_CORE_PORT_INDEX_IP = 0, ///<  input port index
161     OMX_CORE_PORT_INDEX_OP = 1  ///< output port index
162 };
163 
164 extern "C" {
165     OMX_API void *get_omx_component_factory_fn(void);
166 };
167 
168 /// OMX SwVdec component class; derived from QC OMX component base class
169 class omx_swvdec : public qc_omx_component
170 {
171 public:
172 
173     omx_swvdec();
174 
175     virtual ~omx_swvdec();
176 
177     // derived class versions of base class pure virtual functions
178 
179     OMX_ERRORTYPE component_init(OMX_STRING cmp_name);
180     OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE cmp_handle);
181     OMX_ERRORTYPE get_component_version(OMX_HANDLETYPE   cmp_handle,
182                                         OMX_STRING       cmp_name,
183                                         OMX_VERSIONTYPE *p_cmp_version,
184                                         OMX_VERSIONTYPE *p_spec_version,
185                                         OMX_UUIDTYPE    *p_cmp_UUID);
186     OMX_ERRORTYPE send_command(OMX_HANDLETYPE  cmp_handle,
187                                OMX_COMMANDTYPE cmd,
188                                OMX_U32         param,
189                                OMX_PTR         p_cmd_data);
190     OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE cmp_handle,
191                                 OMX_INDEXTYPE  param_index,
192                                 OMX_PTR        p_param_data);
193     OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE cmp_handle,
194                                 OMX_INDEXTYPE  param_index,
195                                 OMX_PTR        p_param_data);
196     OMX_ERRORTYPE get_config(OMX_HANDLETYPE cmp_handle,
197                              OMX_INDEXTYPE  config_index,
198                              OMX_PTR        p_config_data);
199     OMX_ERRORTYPE set_config(OMX_HANDLETYPE cmp_handle,
200                              OMX_INDEXTYPE  config_index,
201                              OMX_PTR        p_config_data);
202     OMX_ERRORTYPE get_extension_index(OMX_HANDLETYPE cmp_handle,
203                                       OMX_STRING     param_name,
204                                       OMX_INDEXTYPE *p_index_type);
205     OMX_ERRORTYPE get_state(OMX_HANDLETYPE cmp_handle,
206                             OMX_STATETYPE *p_state);
207     OMX_ERRORTYPE component_tunnel_request(OMX_HANDLETYPE       cmp_handle,
208                                            OMX_U32              port,
209                                            OMX_HANDLETYPE       peer_component,
210                                            OMX_U32              peer_port,
211                                            OMX_TUNNELSETUPTYPE *p_tunnel_setup);
212     OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE         cmp_handle,
213                              OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
214                              OMX_U32                port,
215                              OMX_PTR                p_app_data,
216                              OMX_U32                bytes,
217                              OMX_U8                *p_buffer);
218     OMX_ERRORTYPE allocate_buffer(OMX_HANDLETYPE         cmp_handle,
219                                   OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
220                                   OMX_U32                port,
221                                   OMX_PTR                p_app_data,
222                                   OMX_U32                bytes);
223     OMX_ERRORTYPE free_buffer(OMX_HANDLETYPE        cmp_handle,
224                               OMX_U32               port,
225                               OMX_BUFFERHEADERTYPE *p_buffer);
226     OMX_ERRORTYPE empty_this_buffer(OMX_HANDLETYPE        cmp_handle,
227                                     OMX_BUFFERHEADERTYPE *p_buffer_hdr);
228     OMX_ERRORTYPE fill_this_buffer(OMX_HANDLETYPE        cmp_handle,
229                                    OMX_BUFFERHEADERTYPE *p_buffer_hdr);
230     OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE    cmp_handle,
231                                 OMX_CALLBACKTYPE *p_callbacks,
232                                 OMX_PTR           p_app_data);
233     OMX_ERRORTYPE use_EGL_image(OMX_HANDLETYPE         cmp_handle,
234                                 OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
235                                 OMX_U32                port,
236                                 OMX_PTR                p_app_data,
237                                 void                  *egl_image);
238     OMX_ERRORTYPE component_role_enum(OMX_HANDLETYPE cmp_handle,
239                                       OMX_U8        *p_role,
240                                       OMX_U32        index);
241 
242     // SwVdec callback functions
243 
244     static SWVDEC_STATUS swvdec_empty_buffer_done_callback(
245         SWVDEC_HANDLE  swvdec_handle,
246         SWVDEC_BUFFER *p_buffer_ip,
247         void          *p_client_handle);
248     static SWVDEC_STATUS swvdec_fill_buffer_done_callback(
249         SWVDEC_HANDLE  swvdec_handle,
250         SWVDEC_BUFFER *p_buffer_op,
251         void          *p_client_handle);
252     static SWVDEC_STATUS swvdec_event_handler_callback(
253         SWVDEC_HANDLE swvdec_handle,
254         SWVDEC_EVENT  event,
255         void         *p_data,
256         void         *p_client_handle);
257 
258 private:
259 
260     OMX_STATETYPE m_state; ///< component state
261 
262     unsigned int m_status_flags; ///< status flags
263 
264     char m_cmp_name[OMX_MAX_STRINGNAME_SIZE];  ///< component name
265     char m_role_name[OMX_MAX_STRINGNAME_SIZE]; ///< component role name
266 
267     SWVDEC_CODEC  m_swvdec_codec;   ///< SwVdec codec type
268     SWVDEC_HANDLE m_swvdec_handle;  ///< SwVdec handle
269     bool          m_swvdec_created; ///< SwVdec created?
270 
271     OMX_VIDEO_CODINGTYPE m_omx_video_codingtype; ///< OMX video coding type
272     OMX_COLOR_FORMATTYPE m_omx_color_formattype; ///< OMX color format type
273 
274     FRAME_DIMENSIONS m_frame_dimensions; ///< frame dimensions
275     FRAME_ATTRIBUTES m_frame_attributes; ///< frame attributes
276 
277     FRAME_DIMENSIONS m_frame_dimensions_max;
278                                  ///< max frame dimensions for adaptive playback
279 
280     ASYNC_THREAD m_async_thread; ///< asynchronous thread
281 
282     omx_swvdec_queue m_queue_command; ///< command queue
283     omx_swvdec_queue m_queue_port_ip; ///<  input port queue for ETBs & EBDs
284     omx_swvdec_queue m_queue_port_op; ///< output port queue for FTBs & FBDs
285 
286     OMX_SWVDEC_PORT m_port_ip; ///<  input port
287     OMX_SWVDEC_PORT m_port_op; ///< output port
288 
289     OMX_CALLBACKTYPE m_callback; ///< IL client callback structure
290     OMX_PTR          m_app_data; ///< IL client app data pointer
291 
292     OMX_PRIORITYMGMTTYPE m_prio_mgmt; ///< priority management
293 
294     bool m_sync_frame_decoding_mode; ///< sync frame decoding mode enabled?
295     bool m_android_native_buffers;   ///< android native buffers enabled?
296 
297     bool m_meta_buffer_mode_disabled; ///< meta buffer mode disabled?
298     bool m_meta_buffer_mode;          ///< meta buffer mode enabled?
299     bool m_adaptive_playback_mode;    ///< adaptive playback mode enabled?
300     bool m_arbitrary_bytes_mode;      ///< arbitrary bytes mode enabled?
301 
302     bool m_port_reconfig_inprogress; ///< port reconfiguration in progress?
303 
304     bool m_dimensions_update_inprogress; ///< dimensions update in progress?
305 
306     sem_t m_sem_cmd; ///< semaphore for command processing
307 
308     OMX_SWVDEC_BUFFER_INFO *m_buffer_array_ip; ///<  input buffer info array
309     OMX_SWVDEC_BUFFER_INFO *m_buffer_array_op; ///< output buffer info array
310 
311     OMX_SWVDEC_META_BUFFER_INFO *m_meta_buffer_array; ///< metabuffer info array
312     pthread_mutex_t              m_meta_buffer_array_mutex;
313                                             ///< mutex for metabuffer info array
314 
315     std::priority_queue <OMX_TICKS,
316                          std::vector<OMX_TICKS>,
317                          std::greater<OMX_TICKS> > m_queue_timestamp;
318                                                    ///< timestamp priority queue
319 
320     omx_swvdec_diag m_diag; ///< diagnostics class variable
321 
322     OMX_ERRORTYPE set_frame_dimensions(unsigned int width,
323                                        unsigned int height);
324     OMX_ERRORTYPE set_frame_attributes(OMX_COLOR_FORMATTYPE color_format);
325     OMX_ERRORTYPE set_adaptive_playback(unsigned int max_width,
326                                         unsigned int max_height);
327 
328     OMX_ERRORTYPE get_video_port_format(
329         OMX_VIDEO_PARAM_PORTFORMATTYPE *p_port_format);
330     OMX_ERRORTYPE set_video_port_format(
331         OMX_VIDEO_PARAM_PORTFORMATTYPE *p_port_format);
332 
333     OMX_ERRORTYPE get_port_definition(OMX_PARAM_PORTDEFINITIONTYPE *p_port_def);
334     OMX_ERRORTYPE set_port_definition(OMX_PARAM_PORTDEFINITIONTYPE *p_port_def);
335 
336     OMX_ERRORTYPE get_supported_profilelevel(
337         OMX_VIDEO_PARAM_PROFILELEVELTYPE *p_profilelevel);
338 
339     OMX_ERRORTYPE describe_color_format(DescribeColorFormatParams *p_params);
340 
341     OMX_ERRORTYPE set_port_definition_qcom(
342         OMX_QCOM_PARAM_PORTDEFINITIONTYPE *p_port_def);
343 
344     // functions to set SwVdec properties with OMX component properties
345 
346     OMX_ERRORTYPE set_frame_dimensions_swvdec();
347     OMX_ERRORTYPE set_frame_attributes_swvdec();
348     OMX_ERRORTYPE set_adaptive_playback_swvdec();
349 
350     // functions to get SwVdec properties and set OMX component properties
351 
352     OMX_ERRORTYPE get_frame_dimensions_swvdec();
353     OMX_ERRORTYPE get_frame_attributes_swvdec();
354     OMX_ERRORTYPE get_buffer_requirements_swvdec(unsigned int port_index);
355 
356     // buffer allocation & de-allocation functions
357     OMX_ERRORTYPE buffer_allocate_ip(OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
358                                      OMX_PTR                p_app_data,
359                                      OMX_U32                size);
360     OMX_ERRORTYPE buffer_allocate_op(OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
361                                      OMX_PTR                p_app_data,
362                                      OMX_U32                size);
363     OMX_ERRORTYPE buffer_allocate_ip_info_array();
364     OMX_ERRORTYPE buffer_allocate_op_info_array();
365     OMX_ERRORTYPE buffer_use_op(OMX_BUFFERHEADERTYPE **pp_buffer_hdr,
366                                 OMX_PTR                p_app_data,
367                                 OMX_U32                size,
368                                 OMX_U8                *p_buffer);
369     OMX_ERRORTYPE buffer_deallocate_ip(OMX_BUFFERHEADERTYPE *p_buffer_hdr);
370     OMX_ERRORTYPE buffer_deallocate_op(OMX_BUFFERHEADERTYPE *p_buffer_hdr);
371     void          buffer_deallocate_ip_info_array();
372     void          buffer_deallocate_op_info_array();
373 
374     OMX_ERRORTYPE meta_buffer_array_allocate();
375     void          meta_buffer_array_deallocate();
376     void          meta_buffer_ref_add(unsigned int index, int fd);
377     void          meta_buffer_ref_remove(unsigned int index);
378 
379     OMX_BOOL port_ip_populated();
380     OMX_BOOL port_op_populated();
381 
382     OMX_ERRORTYPE flush(unsigned int port_index);
383 
384     int  ion_memory_alloc_map(struct ion_allocation_data *p_alloc_data,
385                               struct ion_fd_data         *p_fd_data,
386                               OMX_U32                     size,
387                               OMX_U32                     alignment);
388     void ion_memory_free(struct vdec_ion *p_ion_buf_info);
389     void ion_flush_op(unsigned int index);
390 
391     // component callback functions
392 
393     void swvdec_empty_buffer_done(SWVDEC_BUFFER *p_buffer_ip);
394     void swvdec_fill_buffer_done(SWVDEC_BUFFER *p_buffer_op);
395     void swvdec_event_handler(SWVDEC_EVENT event, void *p_data);
396 
397     OMX_ERRORTYPE retval_swvdec2omx(SWVDEC_STATUS retval_swvdec);
398 
399     // status bits for pending events
400     enum {
401         PENDING_STATE_LOADED_TO_IDLE,    ///< loaded to idle state
402         PENDING_STATE_EXECUTING_TO_IDLE, ///< executing to idle state
403         PENDING_STATE_IDLE_TO_LOADED,    ///< idle to loaded state
404         PENDING_PORT_ENABLE_IP,          ///< enablement of ip port
405         PENDING_PORT_ENABLE_OP,          ///< enablement of op port
406         PENDING_PORT_DISABLE_IP,         ///< disablement of ip port
407         PENDING_PORT_DISABLE_OP,         ///< disablement of op port
408         PENDING_PORT_FLUSH_IP,           ///< flush of ip port
409         PENDING_PORT_FLUSH_OP            ///< flush of op port
410     };
411 
412     // events raised internally
413     enum {
414         OMX_SWVDEC_EVENT_CMD,               ///< command event
415         OMX_SWVDEC_EVENT_CMD_ACK,           ///< command acknowledgement
416         OMX_SWVDEC_EVENT_ERROR,             ///< error event
417         OMX_SWVDEC_EVENT_ETB,               ///< ETB event
418         OMX_SWVDEC_EVENT_EBD,               ///< EBD event
419         OMX_SWVDEC_EVENT_FTB,               ///< FTB event
420         OMX_SWVDEC_EVENT_FBD,               ///< FBD event
421         OMX_SWVDEC_EVENT_EOS,               ///< EOS event
422         OMX_SWVDEC_EVENT_FLUSH_PORT_IP,     ///< flush ip port event
423         OMX_SWVDEC_EVENT_FLUSH_PORT_OP,     ///< flush op port event
424         OMX_SWVDEC_EVENT_PORT_RECONFIG,     ///< port reconfig event
425         OMX_SWVDEC_EVENT_DIMENSIONS_UPDATED ///< dimensions updated event
426     };
427 
428     OMX_ERRORTYPE async_thread_create();
429     void          async_thread_destroy();
430 
431     static void   async_thread(void *p_cmp);
432 
433     void          async_post_event(unsigned long event_id,
434                                    unsigned long event_param1,
435                                    unsigned long event_param2);
436 
437     static void   async_process_event(void *p_cmp);
438 
439     OMX_ERRORTYPE async_process_event_cmd(OMX_COMMANDTYPE cmd, OMX_U32 param);
440     OMX_ERRORTYPE async_process_event_cmd_ack(OMX_COMMANDTYPE cmd,
441                                               OMX_U32         param);
442     OMX_ERRORTYPE async_process_event_error(OMX_ERRORTYPE error_code);
443     OMX_ERRORTYPE async_process_event_cmd_state_set(bool         *p_cmd_ack,
444                                                     OMX_STATETYPE state_new);
445     OMX_ERRORTYPE async_process_event_cmd_flush(unsigned int port_index);
446     OMX_ERRORTYPE async_process_event_cmd_port_disable(
447         bool         *p_cmd_ack,
448         unsigned int  port_index);
449     OMX_ERRORTYPE async_process_event_cmd_port_enable(bool        *p_cmd_ack,
450                                                       unsigned int port_index);
451     OMX_ERRORTYPE async_process_event_etb(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
452                                           unsigned int          index);
453     OMX_ERRORTYPE async_process_event_ftb(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
454                                           unsigned int          index);
455     OMX_ERRORTYPE async_process_event_ebd(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
456                                           unsigned int          index);
457     OMX_ERRORTYPE async_process_event_fbd(OMX_BUFFERHEADERTYPE *p_buffer_hdr,
458                                           unsigned int          index);
459     OMX_ERRORTYPE async_process_event_eos();
460     OMX_ERRORTYPE async_process_event_flush_port_ip();
461     OMX_ERRORTYPE async_process_event_flush_port_op();
462     OMX_ERRORTYPE async_process_event_port_reconfig();
463     OMX_ERRORTYPE async_process_event_dimensions_updated();
464 };
465 
466 #endif // #ifndef _OMX_SWVDEC_H_
467