1 /*--------------------------------------------------------------------------
2 
3 Copyright (c) 2010,2014 The Linux Foundation. All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of The Linux Foundation nor
13       the names of its contributors may be used to endorse or promote
14       products derived from this software without specific prior written
15       permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 --------------------------------------------------------------------------*/
29 #ifndef _AMR_ENC_H_
30 #define _AMR_ENC_H_
31 /*============================================================================
32                     Audio Encoder
33 
34 @file omx_amr_aenc.h
35 This module contains the class definition for openMAX encoder component.
36 
37 
38 
39 ============================================================================*/
40 
41 //////////////////////////////////////////////////////////////////////////////
42 //                             Include Files
43 //////////////////////////////////////////////////////////////////////////////
44 
45 /* Uncomment out below line #define LOG_NDEBUG 0 if we want to see
46  *  all DEBUG_PRINT or LOGV messaging */
47 #include<stdlib.h>
48 #include <stdio.h>
49 #include <pthread.h>
50 #include <time.h>
51 #include <inttypes.h>
52 #include <unistd.h>
53 #include "QOMX_AudioExtensions.h"
54 #include "QOMX_AudioIndexExtensions.h"
55 #include "OMX_Core.h"
56 #include "OMX_Audio.h"
57 #include "aenc_svr.h"
58 #include "qc_omx_component.h"
59 #include "Map.h"
60 #include <semaphore.h>
61 #include <linux/msm_audio.h>
62 #include <linux/msm_audio_amrnb.h>
63 extern "C" {
64     void * get_omx_component_factory_fn(void);
65 }
66 
67 
68 //////////////////////////////////////////////////////////////////////////////
69 //                       Module specific globals
70 //////////////////////////////////////////////////////////////////////////////
71 
72 
73 
74 #define OMX_SPEC_VERSION  0x00000101
75 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
76 #define MAX(x,y) (x >= y?x:y)
77 
78 //////////////////////////////////////////////////////////////////////////////
79 //               Macros
80 //////////////////////////////////////////////////////////////////////////////
81 //
82 
83 
84 #define PrintFrameHdr(i,bufHdr) \
85                            DEBUG_PRINT("i=%d OMX bufHdr[%p]buf[%p]size[%d]TS[%lld]nFlags[0x%x]\n",\
86                            i,\
87                            bufHdr,                                     \
88                            ((OMX_BUFFERHEADERTYPE *)bufHdr)->pBuffer,   \
89                            (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nFilledLen,\
90                            ((OMX_BUFFERHEADERTYPE *)bufHdr)->nTimeStamp, \
91                            (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nFlags)
92 
93 
94 // BitMask Management logic
95 #define BITS_PER_BYTE 8
96 #define BITMASK_SIZE(mIndex) \
97             (((mIndex) + BITS_PER_BYTE - 1)/BITS_PER_BYTE)
98 #define BITMASK_OFFSET(mIndex)\
99             ((mIndex)/BITS_PER_BYTE)
100 #define BITMASK_FLAG(mIndex) \
101             (1 << ((mIndex) % BITS_PER_BYTE))
102 #define BITMASK_CLEAR(mArray,mIndex)\
103             (mArray)[BITMASK_OFFSET(mIndex)] &=  ~(BITMASK_FLAG(mIndex))
104 #define BITMASK_SET(mArray,mIndex)\
105             (mArray)[BITMASK_OFFSET(mIndex)] |=  BITMASK_FLAG(mIndex)
106 #define BITMASK_PRESENT(mArray,mIndex)\
107             ((mArray)[BITMASK_OFFSET(mIndex)] & BITMASK_FLAG(mIndex))
108 #define BITMASK_ABSENT(mArray,mIndex)\
109             (((mArray)[BITMASK_OFFSET(mIndex)] & \
110             BITMASK_FLAG(mIndex)) == 0x0)
111 
112 #define OMX_CORE_NUM_INPUT_BUFFERS    2
113 #define OMX_CORE_NUM_OUTPUT_BUFFERS   16
114 
115 #define OMX_CORE_INPUT_BUFFER_SIZE    8160 // Multiple of 160
116 #define OMX_CORE_CONTROL_CMDQ_SIZE   100
117 #define OMX_AENC_VOLUME_STEP         0x147
118 #define OMX_AENC_MIN                 0
119 #define OMX_AENC_MAX                 100
120 #define NON_TUNNEL                   1
121 #define TUNNEL                       0
122 #define IP_PORT_BITMASK                 0x02
123 #define OP_PORT_BITMASK                 0x01
124 #define IP_OP_PORT_BITMASK              0x03
125 
126 #define OMX_AMR_DEFAULT_SF            8000
127 #define OMX_AMR_DEFAULT_CH_CFG        1
128 #define OMX_AMR_DEFAULT_VOL         25
129 // 14 bytes for input meta data
130 #define OMX_AENC_SIZEOF_META_BUF     (OMX_CORE_INPUT_BUFFER_SIZE+14)
131 
132 #define TRUE 1
133 #define FALSE 0
134 
135 #define NUMOFFRAMES                   1
136 #define MAXFRAMELENGTH                32
137 #define OMX_AMR_OUTPUT_BUFFER_SIZE    ((NUMOFFRAMES * (sizeof(ENC_META_OUT) + MAXFRAMELENGTH) \
138                         + 1))
139 #define FRAMEDURATION                 20000
140 
141 class omx_amr_aenc;
142 
143 // OMX AMR audio encoder class
144 class omx_amr_aenc: public qc_omx_component
145 {
146 public:
147     omx_amr_aenc();                             // constructor
148     virtual ~omx_amr_aenc();                    // destructor
149 
150     OMX_ERRORTYPE allocate_buffer(OMX_HANDLETYPE             hComp,
151                                   OMX_BUFFERHEADERTYPE **bufferHdr,
152                                   OMX_U32                     port,
153                                   OMX_PTR                  appData,
154                                   OMX_U32                    bytes);
155 
156 
157     OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp);
158 
159     OMX_ERRORTYPE component_init(OMX_STRING role);
160 
161     OMX_ERRORTYPE component_role_enum(OMX_HANDLETYPE hComp,
162                                       OMX_U8         *role,
163                                       OMX_U32        index);
164 
165     OMX_ERRORTYPE component_tunnel_request(OMX_HANDLETYPE             hComp,
166                                            OMX_U32                     port,
167                                            OMX_HANDLETYPE     peerComponent,
168                                            OMX_U32                 peerPort,
169                                            OMX_TUNNELSETUPTYPE *tunnelSetup);
170 
171     OMX_ERRORTYPE empty_this_buffer(OMX_HANDLETYPE         hComp,
172                                     OMX_BUFFERHEADERTYPE *buffer);
173 
174 
175     OMX_ERRORTYPE empty_this_buffer_proxy(OMX_HANDLETYPE         hComp,
176                                           OMX_BUFFERHEADERTYPE *buffer);
177 
178 
179     OMX_ERRORTYPE fill_this_buffer(OMX_HANDLETYPE         hComp,
180                                    OMX_BUFFERHEADERTYPE *buffer);
181 
182 
183     OMX_ERRORTYPE free_buffer(OMX_HANDLETYPE         hComp,
184                               OMX_U32                 port,
185                               OMX_BUFFERHEADERTYPE *buffer);
186 
187     OMX_ERRORTYPE get_component_version(OMX_HANDLETYPE              hComp,
188                                         OMX_STRING          componentName,
189                                         OMX_VERSIONTYPE *componentVersion,
190                                         OMX_VERSIONTYPE *     specVersion,
191                                         OMX_UUIDTYPE       *componentUUID);
192 
193     OMX_ERRORTYPE get_config(OMX_HANDLETYPE      hComp,
194                              OMX_INDEXTYPE configIndex,
195                              OMX_PTR        configData);
196 
197     OMX_ERRORTYPE get_extension_index(OMX_HANDLETYPE     hComp,
198                                       OMX_STRING     paramName,
199                                       OMX_INDEXTYPE *indexType);
200 
201     OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE hComp,
202                                 OMX_INDEXTYPE paramIndex,
203                                 OMX_PTR paramData);
204 
205     OMX_ERRORTYPE get_state(OMX_HANDLETYPE hComp,
206                             OMX_STATETYPE *state);
207 
208     static void process_in_port_msg(void          *client_data,
209                                     unsigned char id);
210 
211     static void process_out_port_msg(void          *client_data,
212                                      unsigned char id);
213 
214     static void process_command_msg(void          *client_data,
215                                     unsigned char id);
216 
217     static void process_event_cb(void          *client_data,
218                                    unsigned char id);
219 
220 
221     OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE hComp,
222                                 OMX_CALLBACKTYPE *callbacks,
223                                 OMX_PTR appData);
224 
225     OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
226                              OMX_INDEXTYPE configIndex,
227                              OMX_PTR configData);
228 
229     OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
230                                 OMX_INDEXTYPE paramIndex,
231                                 OMX_PTR paramData);
232 
233     OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE             hComp,
234                              OMX_BUFFERHEADERTYPE **bufferHdr,
235                              OMX_U32                     port,
236                              OMX_PTR                  appData,
237                              OMX_U32                    bytes,
238                              OMX_U8                  *buffer);
239 
240     OMX_ERRORTYPE use_EGL_image(OMX_HANDLETYPE             hComp,
241                                 OMX_BUFFERHEADERTYPE **bufferHdr,
242                                 OMX_U32                     port,
243                                 OMX_PTR                  appData,
244                                 void *                  eglImage);
245 
246     bool post_command(unsigned int p1, unsigned int p2,
247         unsigned char id);
248 
249     // Deferred callback identifiers
250     enum
251     {
252         //Event Callbacks from the component thread context
253         OMX_COMPONENT_GENERATE_EVENT       = 0x1,
254         //Buffer Done callbacks from component thread context
255         OMX_COMPONENT_GENERATE_BUFFER_DONE = 0x2,
256         OMX_COMPONENT_GENERATE_ETB         = 0x3,
257         //Command
258         OMX_COMPONENT_GENERATE_COMMAND     = 0x4,
259         OMX_COMPONENT_GENERATE_FRAME_DONE  = 0x05,
260         OMX_COMPONENT_GENERATE_FTB         = 0x06,
261         OMX_COMPONENT_GENERATE_EOS         = 0x07,
262         OMX_COMPONENT_PORTSETTINGS_CHANGED = 0x08,
263         OMX_COMPONENT_SUSPEND              = 0x09,
264         OMX_COMPONENT_RESUME               = 0x0a
265     };
266 private:
267 
268     ///////////////////////////////////////////////////////////
269     // Type definitions
270     ///////////////////////////////////////////////////////////
271     // Bit Positions
272     enum flags_bit_positions
273     {
274         // Defer transition to IDLE
275         OMX_COMPONENT_IDLE_PENDING            =0x1,
276         // Defer transition to LOADING
277         OMX_COMPONENT_LOADING_PENDING         =0x2,
278 
279         OMX_COMPONENT_MUTED                   =0x3,
280 
281         // Defer transition to Enable
282         OMX_COMPONENT_INPUT_ENABLE_PENDING    =0x4,
283         // Defer transition to Enable
284         OMX_COMPONENT_OUTPUT_ENABLE_PENDING   =0x5,
285         // Defer transition to Disable
286         OMX_COMPONENT_INPUT_DISABLE_PENDING   =0x6,
287         // Defer transition to Disable
288         OMX_COMPONENT_OUTPUT_DISABLE_PENDING  =0x7
289     };
290 
291 
292     typedef Map<OMX_BUFFERHEADERTYPE*, OMX_BUFFERHEADERTYPE*>
293     input_buffer_map;
294 
295     typedef Map<OMX_BUFFERHEADERTYPE*, OMX_BUFFERHEADERTYPE*>
296     output_buffer_map;
297 
298     enum port_indexes
299     {
300         OMX_CORE_INPUT_PORT_INDEX        =0,
301         OMX_CORE_OUTPUT_PORT_INDEX       =1
302     };
303 
304     struct omx_event
305     {
306         unsigned long param1;
307         unsigned long param2;
308         unsigned char id;
309     };
310 
311     struct omx_cmd_queue
312     {
313         omx_event m_q[OMX_CORE_CONTROL_CMDQ_SIZE];
314         unsigned m_read;
315         unsigned m_write;
316         unsigned m_size;
317 
318         omx_cmd_queue();
319         ~omx_cmd_queue();
320         bool insert_entry(unsigned long p1, unsigned long p2, unsigned char id);
321         bool pop_entry(unsigned long *p1,unsigned long *p2, unsigned char *id);
322         bool get_msg_id(unsigned char *id);
323         bool get_msg_with_id(unsigned *p1,unsigned *p2, unsigned id);
324     };
325 
326     typedef struct TIMESTAMP
327     {
328         unsigned int LowPart;
329         unsigned int HighPart;
330     }__attribute__((packed)) TIMESTAMP;
331 
332     typedef struct metadata_input
333     {
334         unsigned short   offsetVal;
335         TIMESTAMP      nTimeStamp;
336         unsigned int   nFlags;
337     }__attribute__((packed)) META_IN;
338 
339     typedef struct enc_meta_out
340     {
341         unsigned int offset_to_frame;
342         unsigned int frame_size;
343         unsigned int encoded_pcm_samples;
344         unsigned int msw_ts;
345         unsigned int lsw_ts;
346         unsigned int nflags;
347     } __attribute__ ((packed))ENC_META_OUT;
348 
349     typedef struct
350     {
351         OMX_U32 tot_in_buf_len;
352         OMX_U32 tot_out_buf_len;
353         OMX_TICKS tot_pb_time;
354         OMX_U32 fbd_cnt;
355         OMX_U32 ftb_cnt;
356         OMX_U32 etb_cnt;
357         OMX_U32 ebd_cnt;
358     }AMR_PB_STATS;
359 
360     ///////////////////////////////////////////////////////////
361     // Member variables
362     ///////////////////////////////////////////////////////////
363     OMX_U8                         *m_tmp_meta_buf;
364     OMX_U8                         *m_tmp_out_meta_buf;
365     OMX_U8                         m_flush_cnt ;
366     OMX_U8                         m_comp_deinit;
367 
368     // the below var doesnt hold good if combo of use and alloc bufs are used
369     OMX_S32                        m_volume;//Unit to be determined
370     OMX_PTR                        m_app_data;// Application data
371     int                            nNumInputBuf;
372     int                            nNumOutputBuf;
373     int                            m_drv_fd;   // Kernel device node file handle
374     bool                           bFlushinprogress;
375     bool                           is_in_th_sleep;
376     bool                           is_out_th_sleep;
377     unsigned int                   m_flags;      //encapsulate the waiting states.
378     OMX_U64                        nTimestamp;
379     OMX_U64                        ts;
380     unsigned int                   pcm_input; //tunnel or non-tunnel
381     unsigned int                   m_inp_act_buf_count;    // Num of Input Buffers
382     unsigned int                   m_out_act_buf_count;    // Numb of Output Buffers
383     unsigned int                   m_inp_current_buf_count;    // Num of Input Buffers
384     unsigned int                   m_out_current_buf_count;    // Numb of Output Buffers
385     unsigned int                   output_buffer_size;
386     unsigned int                   input_buffer_size;
387     unsigned short                 m_session_id;
388     // store I/P PORT state
389     OMX_BOOL                       m_inp_bEnabled;
390     // store O/P PORT state
391     OMX_BOOL                       m_out_bEnabled;
392     //Input port Populated
393     OMX_BOOL                       m_inp_bPopulated;
394     //Output port Populated
395     OMX_BOOL                       m_out_bPopulated;
396     sem_t                          sem_States;
397     sem_t                          sem_read_msg;
398     sem_t                          sem_write_msg;
399 
400     volatile int                   m_is_event_done;
401     volatile int                   m_is_in_th_sleep;
402     volatile int                   m_is_out_th_sleep;
403     input_buffer_map               m_input_buf_hdrs;
404     output_buffer_map              m_output_buf_hdrs;
405     omx_cmd_queue                  m_input_q;
406     omx_cmd_queue                  m_input_ctrl_cmd_q;
407     omx_cmd_queue                  m_input_ctrl_ebd_q;
408     omx_cmd_queue                  m_command_q;
409     omx_cmd_queue                  m_output_q;
410     omx_cmd_queue                  m_output_ctrl_cmd_q;
411     omx_cmd_queue                  m_output_ctrl_fbd_q;
412     pthread_mutexattr_t            m_outputlock_attr;
413     pthread_mutexattr_t            m_commandlock_attr;
414     pthread_mutexattr_t            m_lock_attr;
415     pthread_mutexattr_t            m_state_attr;
416     pthread_mutexattr_t            m_flush_attr;
417     pthread_mutexattr_t            m_in_th_attr_1;
418     pthread_mutexattr_t            m_out_th_attr_1;
419     pthread_mutexattr_t            m_event_attr;
420     pthread_mutexattr_t            m_in_th_attr;
421     pthread_mutexattr_t            m_out_th_attr;
422     pthread_mutexattr_t            out_buf_count_lock_attr;
423     pthread_mutexattr_t            in_buf_count_lock_attr;
424     pthread_cond_t                 cond;
425     pthread_cond_t                 in_cond;
426     pthread_cond_t                 out_cond;
427     pthread_mutex_t                m_lock;
428     pthread_mutex_t                m_commandlock;
429     pthread_mutex_t                m_outputlock;
430     // Mutexes for state change
431     pthread_mutex_t                m_state_lock;
432     // Mutexes for  flush acks from input and output threads
433     pthread_mutex_t                m_flush_lock;
434     pthread_mutex_t                m_event_lock;
435     pthread_mutex_t                m_in_th_lock;
436     pthread_mutex_t                m_out_th_lock;
437     pthread_mutex_t                m_in_th_lock_1;
438     pthread_mutex_t                m_out_th_lock_1;
439     pthread_mutex_t                out_buf_count_lock;
440     pthread_mutex_t                in_buf_count_lock;
441 
442     OMX_STATETYPE                  m_state;      // OMX State
443     OMX_STATETYPE                  nState;
444     OMX_CALLBACKTYPE               m_cb;         // Application callbacks
445     AMR_PB_STATS                  m_amr_pb_stats;
446     struct amr_ipc_info           *m_ipc_to_in_th;    // for input thread
447     struct amr_ipc_info           *m_ipc_to_out_th;    // for output thread
448     struct amr_ipc_info           *m_ipc_to_cmd_th;    // for command thread
449     OMX_PRIORITYMGMTTYPE           m_priority_mgm ;
450     OMX_AUDIO_PARAM_AMRTYPE m_amr_param; // Cache AMR encoder parameter
451     OMX_AUDIO_PARAM_PCMMODETYPE    m_pcm_param;  // Cache pcm  parameter
452     OMX_PARAM_COMPONENTROLETYPE    component_Role;
453     OMX_PARAM_BUFFERSUPPLIERTYPE   m_buffer_supplier;
454 
455     ///////////////////////////////////////////////////////////
456     // Private methods
457     ///////////////////////////////////////////////////////////
458     OMX_ERRORTYPE allocate_output_buffer(OMX_HANDLETYPE       hComp,
459                                          OMX_BUFFERHEADERTYPE **bufferHdr,
460                                          OMX_U32 port,OMX_PTR appData,
461                                          OMX_U32              bytes);
462 
463     OMX_ERRORTYPE allocate_input_buffer(OMX_HANDLETYPE       hComp,
464                                         OMX_BUFFERHEADERTYPE **bufferHdr,
465                                         OMX_U32              port,
466                                         OMX_PTR              appData,
467                                         OMX_U32              bytes);
468 
469     OMX_ERRORTYPE use_input_buffer(OMX_IN OMX_HANDLETYPE          hComp,
470                                    OMX_INOUT OMX_BUFFERHEADERTYPE **bufHdr,
471                                    OMX_IN OMX_U32                 port,
472                                    OMX_IN OMX_PTR                 appData,
473                                    OMX_IN OMX_U32                 bytes,
474                                    OMX_IN OMX_U8*                 buffer);
475 
476     OMX_ERRORTYPE use_output_buffer(OMX_IN OMX_HANDLETYPE          hComp,
477                                     OMX_INOUT OMX_BUFFERHEADERTYPE **bufHdr,
478                                     OMX_IN OMX_U32                 port,
479                                     OMX_IN OMX_PTR                 appData,
480                                     OMX_IN OMX_U32                 bytes,
481                                     OMX_IN OMX_U8*                 buffer);
482 
483     OMX_ERRORTYPE fill_this_buffer_proxy(OMX_HANDLETYPE       hComp,
484                                          OMX_BUFFERHEADERTYPE *buffer);
485 
486     OMX_ERRORTYPE send_command_proxy(OMX_HANDLETYPE  hComp,
487                                      OMX_COMMANDTYPE cmd,
488                                      OMX_U32         param1,
489                                      OMX_PTR         cmdData);
490 
491     OMX_ERRORTYPE send_command(OMX_HANDLETYPE hComp,
492                                OMX_COMMANDTYPE  cmd,
493                                OMX_U32       param1,
494                                OMX_PTR      cmdData);
495 
496     bool allocate_done(void);
497 
498     bool release_done(OMX_U32         param1);
499 
500     bool execute_omx_flush(OMX_IN OMX_U32 param1, bool cmd_cmpl=true);
501 
502     bool execute_input_omx_flush(void);
503 
504     bool execute_output_omx_flush(void);
505 
506     bool search_input_bufhdr(OMX_BUFFERHEADERTYPE *buffer);
507 
508     bool search_output_bufhdr(OMX_BUFFERHEADERTYPE *buffer);
509 
510     bool post_input(unsigned long p1, unsigned long p2,
511                     unsigned char id);
512 
513     bool post_output(unsigned long p1, unsigned long p2,
514                      unsigned char id);
515 
516     void process_events(omx_amr_aenc *client_data);
517 
518     void buffer_done_cb(OMX_BUFFERHEADERTYPE *bufHdr);
519 
520     void frame_done_cb(OMX_BUFFERHEADERTYPE *bufHdr);
521 
522     void wait_for_event();
523 
524     void event_complete();
525 
526     void in_th_goto_sleep();
527 
528     void in_th_wakeup();
529 
530     void out_th_goto_sleep();
531 
532     void out_th_wakeup();
533 
534     void flush_ack();
535     void deinit_encoder();
536 
537 };
538 #endif
539