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