• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 #ifndef PVMF_OMX_ENC_NODE_H_INCLUDED
19 #define PVMF_OMX_ENC_NODE_H_INCLUDED
20 
21 #ifndef OSCL_BASE_H_INCLUDED
22 #include "oscl_base.h"
23 #endif
24 
25 #ifndef OSCL_SCHEDULER_AO_H_INCLUDED
26 #include "oscl_scheduler_ao.h"
27 #endif
28 
29 #ifndef PVMF_FORMAT_TYPE_H_INCLUDED
30 #include "pvmf_format_type.h"
31 #endif
32 
33 #ifndef PVMF_NODE_INTERFACE_H_INCLUDED
34 #include "pvmf_node_interface.h"
35 #endif
36 
37 #ifndef OSCL_PRIQUEUE_H_INCLUDED
38 #include "oscl_priqueue.h"
39 #endif
40 
41 #ifndef PVMF_MEDIA_DATA_H_INCLUDED
42 #include "pvmf_media_data.h"
43 #endif
44 
45 #ifndef OSCL_MEM_MEMPOOL_H_INCLUDED
46 #include "oscl_mem_mempool.h"
47 #endif
48 #ifndef PVMF_MEMPOOL_H_INCLUDED
49 #include "pvmf_mempool.h"
50 #endif
51 
52 #ifndef PVMF_SIMPLE_MEDIA_BUFFER_H_INCLUDED
53 #include "pvmf_simple_media_buffer.h"
54 #endif
55 
56 #ifndef PVMF_MEDIA_FRAG_GROUP_H_INCLUDED
57 #include "pvmf_media_frag_group.h"
58 #endif
59 
60 #ifndef PVMF_POOL_BUFFER_ALLOCATOR_H_INCLUDED
61 #include "pvmf_pool_buffer_allocator.h"
62 #endif
63 
64 #ifndef PVMF_VIDEO_H_INCLUDED
65 #include "pvmf_video.h"
66 #endif
67 
68 #ifndef PVMF_POOL_BUFFER_ALLOCATOR_H_INCLUDED
69 #include "pvmf_pool_buffer_allocator.h"
70 #endif
71 
72 #ifndef PVMF_NODE_UTILS_H_INCLUDED
73 #include "pvmf_node_utils.h"
74 #endif
75 
76 #ifndef PVMF_OMX_ENC_PORT_H_INCLUDED
77 #include "pvmf_omx_enc_port.h"
78 #endif
79 
80 #ifndef PVMF_OMX_ENC_NODE_EXTENSION_INTERFACE_H_INCLUDED
81 #include "pvmf_omx_enc_node_extension_interface.h"
82 #endif
83 
84 #ifndef PV_MP4_H263_ENC_EXTENSION_H_INCLUDED
85 #include "pvmp4h263encextension.h"
86 #endif
87 
88 #ifndef PVMF_AUDIO_ENCNODE_EXTENSION_H_INCLUDED
89 #include "pvmf_audio_encnode_extension.h"
90 #endif
91 
92 // DV: NOTE - this needs to be fixed
93 //#ifndef __PVM4VENCODER_H
94 //#include "pvm4vencoder.h"
95 //#endif
96 
97 #ifndef PVMF_META_DATA_EXTENSION_H_INCLUDED
98 #include "pvmf_meta_data_extension.h"
99 #endif
100 
101 #ifndef PV_MIME_STRING_UTILS_H_INCLUDED
102 #include "pv_mime_string_utils.h"
103 #endif
104 
105 #ifndef OMX_Core_h
106 #include "OMX_Core.h"
107 #endif
108 
109 #ifndef OMX_Component_h
110 #include "OMX_Component.h"
111 #endif
112 
113 #ifndef PVMF_OMX_ENC_CALLBACKS_H_INCLUDED
114 #include "pvmf_omx_enc_callbacks.h"
115 #endif
116 
117 #ifndef PV_OMXDEFS_H_INCLUDED
118 #include "pv_omxdefs.h"
119 #endif
120 
121 #ifndef OSCLCONFIG_IO_H_INCLUDED
122 #include "osclconfig_io.h"
123 #endif
124 #ifndef OSCL_MEM_H_INCLUDED
125 #include "oscl_mem.h"
126 #endif
127 
128 #if (PVLOGGER_INST_LEVEL >= PVLOGMSG_INST_REL)
129 #ifndef PVMF_MEDIA_CLOCK_H_INCLUDED
130 #include "pvmf_media_clock.h"
131 #endif
132 #endif
133 
134 #define PVMFOMXENCNODE_NUM_CMD_IN_POOL 8
135 #define PVOMXENCMAXNUMDPBFRAMESPLUS1 17
136 
137 
138 typedef struct OutputBufCtrlStruct
139 {
140     OMX_BUFFERHEADERTYPE *pBufHdr;
141 } OutputBufCtrlStruct;
142 
143 typedef struct InputBufCtrlStruct
144 {
145     OMX_BUFFERHEADERTYPE *pBufHdr;
146     PVMFSharedMediaDataPtr pMediaData;
147 } InputBufCtrlStruct;
148 
149 
150 // fwd class declaration
151 class PVLogger;
152 //memory allocator type for this node.
153 typedef OsclMemAllocator PVMFOMXEncNodeAllocator;
154 typedef OsclMemAllocDestructDealloc<uint8> PVMFOMXEncNodeAllocDestructDealloc;
155 
156 
157 // CALLBACK PROTOTYPES
158 OMX_ERRORTYPE CallbackEventHandlerEnc(OMX_OUT OMX_HANDLETYPE aComponent,
159                                       OMX_OUT OMX_PTR aAppData,
160                                       OMX_OUT OMX_EVENTTYPE aEvent,
161                                       OMX_OUT OMX_U32 aData1,
162                                       OMX_OUT OMX_U32 aData2,
163                                       OMX_OUT OMX_PTR aEventData);
164 
165 OMX_ERRORTYPE CallbackEmptyBufferDoneEnc(OMX_OUT OMX_HANDLETYPE aComponent,
166         OMX_OUT OMX_PTR aAppData,
167         OMX_OUT OMX_BUFFERHEADERTYPE* aBuffer);
168 
169 OMX_ERRORTYPE CallbackFillBufferDoneEnc(OMX_OUT OMX_HANDLETYPE aComponent,
170                                         OMX_OUT OMX_PTR aAppData,
171                                         OMX_OUT OMX_BUFFERHEADERTYPE* aBuffer);
172 
173 
174 
175 //Default values for number of Input/Output buffers. If the component needs more than this, it will be
176 // negotiated. If the component does not need more than this number, the default is used
177 #define NUMBER_INPUT_BUFFER 5
178 #define NUMBER_OUTPUT_BUFFER 9
179 
180 // for AMR encoder
181 #define MAX_NUM_AMR_FRAMES_PER_BUFFER 25
182 #define PVMF_AMRENC_DEFAULT_SAMPLING_RATE 8000
183 #define PVMF_AMRENC_DEFAULT_NUM_CHANNELS 1
184 #define PVMF_AMRENC_DEFAULT_BITSPERSAMPLE 16
185 // in milliseconds
186 #define PVMF_AMRENC_DEFAULT_FRAME_DURATION 20
187 
188 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
189 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
190 /////////////////////////
191 /////////////////////////
192 /////////////////////////
193 // CUSTOM DEALLOCATOR FOR MEDIA DATA SHARED PTR WRAPPER:
194 //                      1) Deallocates the underlying output buffer
195 //                      2) Deallocates the pvci buffer wrapper and the rest of accompanying structures
196 //                    Deallocator is created as part of the wrapper, and travels with the buffer wrapper
197 
198 class PVOMXEncBufferSharedPtrWrapperCombinedCleanupDA : public OsclDestructDealloc
199 {
200     public:
PVOMXEncBufferSharedPtrWrapperCombinedCleanupDA(Oscl_DefAlloc * allocator,void * pMempoolData)201         PVOMXEncBufferSharedPtrWrapperCombinedCleanupDA(Oscl_DefAlloc* allocator, void *pMempoolData) :
202                 buf_alloc(allocator), ptr_to_data_to_dealloc(pMempoolData) {};
~PVOMXEncBufferSharedPtrWrapperCombinedCleanupDA()203         virtual ~PVOMXEncBufferSharedPtrWrapperCombinedCleanupDA() {};
204 
destruct_and_dealloc(OsclAny * ptr)205         virtual void destruct_and_dealloc(OsclAny* ptr)
206         {
207             // call buffer deallocator
208             if (buf_alloc != NULL)
209             {
210                 buf_alloc->deallocate(ptr_to_data_to_dealloc);
211             }
212 
213             // this is needed to completely free PVMFMediaDataImpl, since it allocates memory for the frag list
214             ((PVMFMediaDataImpl*)ptr)->~PVMFMediaDataImpl();
215             oscl_free(ptr);
216         }
217 
218     private:
219         Oscl_DefAlloc* buf_alloc;
220         void *ptr_to_data_to_dealloc;
221 };
222 
223 
224 
225 #define PVMFOMXEncNodeCommandBase PVMFGenericNodeCommand<PVMFOMXEncNodeAllocator>
226 
227 class PVMFOMXEncNodeCommand: public PVMFOMXEncNodeCommandBase
228 {
229     public:
230         //constructor for Custom2 command
Construct(PVMFSessionId s,int32 cmd,int32 arg1,int32 arg2,int32 & arg3,const OsclAny * aContext)231         void Construct(PVMFSessionId s, int32 cmd, int32 arg1, int32 arg2, int32& arg3, const OsclAny*aContext)
232         {
233             PVMFOMXEncNodeCommandBase::Construct(s, cmd, aContext);
234             iParam1 = (OsclAny*)arg1;
235             iParam2 = (OsclAny*)arg2;
236             iParam3 = (OsclAny*) & arg3;
237         }
Parse(int32 & arg1,int32 & arg2,int32 * & arg3)238         void Parse(int32&arg1, int32&arg2, int32*&arg3)
239         {
240             arg1 = (int32)iParam1;
241             arg2 = (int32)iParam2;
242             arg3 = (int32*)iParam3;
243         }
244 
Construct(PVMFSessionId s,int32 cmd,PVMFMetadataList * aKeyList,uint32 aStartIndex,int32 aMaxEntries,char * aQueryKey,const OsclAny * aContext)245         void Construct(PVMFSessionId s, int32 cmd, PVMFMetadataList* aKeyList, uint32 aStartIndex, int32 aMaxEntries, char* aQueryKey, const OsclAny* aContext)
246         {
247             PVMFOMXEncNodeCommandBase::Construct(s, cmd, aContext);
248             iStartIndex = aStartIndex;
249             iMaxEntries = aMaxEntries;
250 
251             if (aQueryKey == NULL)
252             {
253                 query_key[0] = 0;
254             }
255             else
256             {
257                 if (aQueryKey != NULL)
258                     oscl_strncpy(query_key, aQueryKey, oscl_strlen(aQueryKey) + 1);
259             }
260 
261             iParam1 = (OsclAny*)aKeyList;
262             iParam2 = NULL;
263             iParam3 = NULL;
264             iParam4 = NULL;
265             iParam5 = NULL;
266         }
267 
Parse(PVMFMetadataList * & MetaDataListPtr,uint32 & aStartingIndex,int32 & aMaxEntries,char * & aQueryKey)268         void Parse(PVMFMetadataList*& MetaDataListPtr, uint32 &aStartingIndex, int32 &aMaxEntries, char*&aQueryKey)
269         {
270             MetaDataListPtr = (PVMFMetadataList*)iParam1;
271             aStartingIndex = iStartIndex;
272             aMaxEntries = iMaxEntries;
273             if (query_key[0] == 0)
274             {
275                 aQueryKey = NULL;
276             }
277             else
278             {
279                 aQueryKey = query_key;
280             }
281         }
282 
283         // Constructor and parser for GetNodeMetadataValue
Construct(PVMFSessionId s,int32 cmd,PVMFMetadataList * aKeyList,Oscl_Vector<PvmiKvp,OsclMemAllocator> * aValueList,uint32 aStartIndex,int32 aMaxEntries,const OsclAny * aContext)284         void Construct(PVMFSessionId s, int32 cmd, PVMFMetadataList* aKeyList, Oscl_Vector<PvmiKvp, OsclMemAllocator>* aValueList, uint32 aStartIndex, int32 aMaxEntries, const OsclAny* aContext)
285         {
286             PVMFOMXEncNodeCommandBase::Construct(s, cmd, aContext);
287             iParam1 = (OsclAny*)aKeyList;
288             iParam2 = (OsclAny*)aValueList;
289 
290             iStartIndex = aStartIndex;
291             iMaxEntries = aMaxEntries;
292 
293             iParam3 = NULL;
294             iParam4 = NULL;
295             iParam5 = NULL;
296         }
Parse(PVMFMetadataList * & aKeyList,Oscl_Vector<PvmiKvp,OsclMemAllocator> * & aValueList,uint32 & aStartingIndex,int32 & aMaxEntries)297         void Parse(PVMFMetadataList* &aKeyList, Oscl_Vector<PvmiKvp, OsclMemAllocator>* &aValueList, uint32 &aStartingIndex, int32 &aMaxEntries)
298         {
299             aKeyList = (PVMFMetadataList*)iParam1;
300             aValueList = (Oscl_Vector<PvmiKvp, OsclMemAllocator>*)iParam2;
301             aStartingIndex = iStartIndex;
302             aMaxEntries = iMaxEntries;
303         }
304 
305 
hipri()306         virtual bool hipri()
307         {
308             //this routine identifies commands that need to
309             //go at the front of the queue.  derived command
310             //classes can override it if needed.
311             return (iCmd == PVOMXENC_NODE_CMD_CANCELALL
312                     || iCmd == PVOMXENC_NODE_CMD_CANCELCMD);
313         }
314 
315         enum PVOMXEncNodeCmdType
316         {
317             PVOMXENC_NODE_CMD_QUERYUUID,
318             PVOMXENC_NODE_CMD_QUERYINTERFACE,
319             PVOMXENC_NODE_CMD_REQUESTPORT,
320             PVOMXENC_NODE_CMD_RELEASEPORT,
321             PVOMXENC_NODE_CMD_INIT,
322             PVOMXENC_NODE_CMD_PREPARE,
323             PVOMXENC_NODE_CMD_START,
324             PVOMXENC_NODE_CMD_STOP,
325             PVOMXENC_NODE_CMD_FLUSH,
326             PVOMXENC_NODE_CMD_PAUSE,
327             PVOMXENC_NODE_CMD_RESET,
328             PVOMXENC_NODE_CMD_CANCELALL,
329             PVOMXENC_NODE_CMD_CANCELCMD,
330             PVOMXENC_NODE_CMD_GETNODEMETADATAKEY,
331             PVOMXENC_NODE_CMD_GETNODEMETADATAVALUE,
332             PVOMXENC_NODE_CMD_INVALID
333         };
334 
335 
336 
337 
338     private:
339         uint32 iStartIndex;
340         uint32 iMaxEntries;
341         char query_key[256];
342 
343 };
344 
345 //Default vector reserve size
346 #define PVMF_OMXENC_NODE_COMMAND_VECTOR_RESERVE 10
347 
348 //Starting value for command IDs
349 #define PVMF_OMXENC_NODE_COMMAND_ID_START 6000
350 
351 ////////////////////////////////////////////////////////////////////////////////////////////////////
352 //////////////////////////////////// STRUCTURES FOR ENCODING PARAMETERS ////////////////////////////
353 ////////////////////////////////////////////////////////////////////////////////////////////////////
354 #define MAX_LAYER 1
355 
356 /** Contains supported input format */
357 enum EncVideoInputFormat
358 {
359     EI_RGB24,
360     EI_RGB12,
361     EI_YUV420,
362     EI_UYVY
363 };
364 
365 /** Type of contents for optimal encoding mode. */
366 enum EncContentType
367 {
368     /** Content is to be streamed in real-time. */
369     EI_M4V_STREAMING,
370 
371     /** Content is to be downloaded and playbacked later.*/
372     EI_M4V_DOWNLOAD,
373 
374     /** Content is to be 3gpp baseline compliant. */
375     EI_H263,
376 
377     EI_H264
378 };
379 
380 /** Targeted profile and level to encode. */
381 enum EncM4VProfileLevel
382 {
383     /* Non-scalable profile */
384     EI_SIMPLE_LEVEL0 = 0,
385     EI_SIMPLE_LEVEL1,
386     EI_SIMPLE_LEVEL2,
387     EI_SIMPLE_LEVEL3,
388     EI_CORE_LEVEL1,
389     EI_CORE_LEVEL2,
390 
391     /* Scalable profile */
392     EI_SIMPLE_SCALABLE_LEVEL0 = 6,
393     EI_SIMPLE_SCALABLE_LEVEL1,
394     EI_SIMPLE_SCALABLE_LEVEL2,
395 
396     EI_CORE_SCALABLE_LEVEL1 = 10,
397     EI_CORE_SCALABLE_LEVEL2,
398     EI_CORE_SCALABLE_LEVEL3
399 };
400 
401 /** Targeted profile to encode. */
402 enum EncAVCProfile
403 {
404     /* Non-scalable profile */
405     EI_PROFILE_DEFAULT,
406     EI_PROFILE_BASELINE,
407     EI_PROFILE_MAIN,
408     EI_PROFILE_EXTENDED,
409     EI_PROFILE_HIGH,
410     EI_PROFILE_HIGH10,
411     EI_PROFILE_HIGH422,
412     EI_PROFILE_HIGH444
413 };
414 
415 /** Targeted level to encode. */
416 enum EncAVCLevel
417 {
418     EI_LEVEL_AUTODETECT,
419     EI_LEVEL_1,
420     EI_LEVEL_1B,
421     EI_LEVEL_11,
422     EI_LEVEL_12,
423     EI_LEVEL_13,
424     EI_LEVEL_2,
425     EI_LEVEL_21,
426     EI_LEVEL_22,
427     EI_LEVEL_3,
428     EI_LEVEL_31,
429     EI_LEVEL_32,
430     EI_LEVEL_4,
431     EI_LEVEL_41,
432     EI_LEVEL_42,
433     EI_LEVEL_5,
434     EI_LEVEL_51,
435 };
436 
437 /** Output format */
438 enum TAVCEIOutputFormat
439 {
440     /** output in byte stream format according to Annex B */
441     EAVCEI_OUTPUT_ANNEXB,
442 
443     /** output for MP4 file format */
444     EAVCEI_OUTPUT_MP4,
445 
446     /** output in RTP format according to RFC 3984 */
447     EAVCEI_OUTPUT_RTP
448 };
449 
450 
451 
452 /** Type of contents for optimal encoding mode. */
453 enum EncEncodingMode
454 {
455     /** Content is encoded as fast as possible with error protection */
456     EI_ENCMODE_TWOWAY,
457 
458     /** Content is encoded as fast as possible without error protection */
459     EI_ENCMODE_RECORDER,
460 
461     /** Content is encoded with better quality (slow) with error protection */
462     EI_ENCMODE_STREAMING,
463 
464     /** Content is encoded with better quality (slow) without error protection */
465     EI_ENCMODE_DOWNLOAD
466 };
467 
468 
469 /** This structure contains encoder settings. */
470 typedef struct PV_VideoEncodeParam
471 {
472 
473 ////////////////////////////////////////////////////////////////////////////////////
474 ////////////////////////// COMMON PARAMETERS ///////////////////////////////////////
475 ////////////////////////////////////////////////////////////////////////////////////
476 
477     /** Specifies an  ID that will be used to specify this encoder while returning
478     the bitstream in asynchronous mode. */
479     uint32              iEncodeID;
480 
481 
482     /** Specifies whether base only (iNumLayer = 1) or base + enhancement layer
483     (iNumLayer =2 ) is to be used. */
484     int32               iNumLayer;
485 
486     /** Specifies the width in pixels of the encoded frames. IFrameWidth[0] is for
487     base layer and iFrameWidth[1] is for enhanced layer. */
488     int                 iFrameWidth[MAX_LAYER];
489 
490     /** Specifies the height in pixels of the encoded frames. IFrameHeight[0] is for
491     base layer and iFrameHeight[1] is for enhanced layer. */
492     int                 iFrameHeight[MAX_LAYER];
493 
494     /** Specifies the cumulative bit rate in bit per second. IBitRate[0] is for base
495     layer and iBitRate[1] is for base+enhanced layer.*/
496     int                 iBitRate[MAX_LAYER];
497 
498     /** Specifies the cumulative frame rate in frame per second. IFrameRate[0] is for
499     base layer and iFrameRate[1] is for base+enhanced layer. */
500     float               iFrameRate[MAX_LAYER];
501 
502     /** Specifies the rate control algorithm among one of the following constant Q,
503     CBR and VBR.  The structure EncRateControlType is defined above.*/
504     PVMFVENRateControlType iRateControlType;
505 
506     /** Specifies the initial quantization parameter for the first I-frame. If constant Q
507     rate control is used, this QP will be used for all the I-frames. This number must be
508     set between 1 and 31, otherwise, Initialize() will fail. */
509     int                 iIquant[2];
510 
511     /** Specifies the initial quantization parameter for the first P-frame. If constant Q
512     rate control is used, this QP will be used for all the P-frames. This number must be
513     set between 1 and 31, otherwise, Initialize() will fail. */
514     int                 iPquant[2];
515 
516     /** Specifies the initial quantization parameter for the first B-frame. If constant Q
517     rate control is used, this QP will be used for all the B-frames. This number must be
518     set between 1 and 31, otherwise, Initialize() will fail. */
519     int                 iBquant[2];
520 
521 
522     /** Specifies the maximum number of P-frames between 2 INTRA frames. An INTRA mode is
523     forced to a frame once this interval is reached. When there is only one I-frame is present
524     at the beginning of the clip, iIFrameInterval should be set to -1. */
525     int32               iIFrameInterval;
526 
527     /** According to iIFrameInterval setting, the minimum number of intra MB per frame is
528     optimally calculated for error resiliency. However, when iIFrameInterval is set to -1,
529     iNumIntraMBRefresh must be specified to guarantee the minimum number of intra
530     macroblocks per frame.*/
531     uint32              iNumIntraMBRefresh;
532 
533     /** Specifies automatic scene detection where I-frame will be used the the first frame
534     in a new scene. */
535     bool                iSceneDetection;
536 
537     /** Specifies the packet size in bytes which represents the number of bytes between two resync markers.
538     For EI_M4V_DOWNLOAD and EI_H263, if iPacketSize is set to 0, there will be no resync markers in the bitstream.
539     For EI_M4V_STREAMING is parameter must be set to a value greater than 0. For AVC, this value specifies
540     the packet size in bytes which represents the desired number of bytes per NAL.
541     If this number is set to 0, the AVC encoder will encode the entire slice group as one NAL. */
542     uint32              iPacketSize;
543 
544     /** Specifies the VBV buffer size which determines the end-to-end delay between the
545     encoder and the decoder.  The size is in unit of seconds. For download application,
546     the buffer size can be larger than the streaming application. For 2-way application,
547     this buffer shall be kept minimal. For a special case, in VBR mode, iBufferDelay will
548     be set to -1 to allow buffer underflow. */
549     float               iBufferDelay;
550 
551 
552     /** Specifies the duration of the clip in millisecond.*/
553     int32               iClipDuration;
554 
555 
556 ////////////////////////////////////////////////////////////////////////////////////////
557 /////////////////////////// H263/M4V specific parameters ///////////////////////////////
558 ////////////////////////////////////////////////////////////////////////////////////////
559 
560     /** Specifies the profile and level used to encode the bitstream. When present,
561     other settings will be checked against the range allowable by this target profile
562     and level. Fail may be returned from the Initialize call. */
563     EncM4VProfileLevel  iProfileLevel;
564 
565     /** Specifies the picture quality factor on the scale of 1 to 10. It trades off
566     the picture quality with the frame rate. Higher frame quality means lower frame rate.
567     Lower frame quality for higher frame rate.*/
568     int32               iFrameQuality;
569 
570 
571     /** Enable the use of iFrameQuality to determine the frame rate. If it is false,
572     the encoder will try to meet the specified frame rate regardless of the frame quality.*/
573     bool                iEnableFrameQuality;
574 
575 
576     /** Specifies high quality but also high complexity mode for rate control. */
577     bool                iRDOptimal;
578 
579 
580     /** Specifies the search range in pixel unit for motion vector. The range of the
581     motion vector will be of dimension [-iSearchRange.5, +iSearchRange.0]. */
582     int32               iSearchRange;
583 
584     /** Specifies the use of 8x8 motion vectors. */
585     bool                iMV8x8;
586 
587     /** Specifies the use of half-pel motion vectors. */
588     bool                iMVHalfPel;
589 
590     /** Specifies whether the current frame skipping decision is allowed after encoding
591     the current frame. If there is no memory of what has been coded for the current frame,
592     iNoCurrentSkip has to be on. */
593     bool                iNoCurrentSkip;
594 
595     /** Specifies that no frame skipping is allowed. Frame skipping is a tool used to
596     control the average number of bits spent to meet the target bit rate. */
597     bool                iNoFrameSkip;
598 
599 
600     /** Specify short header mode in MPEG4 */
601     bool                                iShortHeader;
602 
603     /** Specifies whether data partitioning mode is used or not. Has no meaning if encoding H.263 or short header */
604     bool                                iDataPartitioning;
605 
606 
607     /** Specifies whether Resync markers are used or not Has no meaning if iDataPartitioning is on */
608     bool                                iResyncMarker;
609 
610 
611     /** Specifies whether RVLC (reversible VLC) is to be used or not.
612     */
613     bool                iRVLCEnable;
614 
615 ////////////////////////////////////////////////////////////////////////////////////////
616 /////////////////////////// H264 specific parameters ///////////////////////////////
617 ////////////////////////////////////////////////////////////////////////////////////////
618 
619     /** Specifies the targeted profile, and will also specifies available tools for iEncMode.
620     If default is used, encoder will choose its own preferred profile. If autodetect is used, encoder
621     will check other settings and choose the right profile that doesn't have any conflicts. */
622     EncAVCProfile       iAVCProfile;
623 
624     /** Specifies the target level  When present,
625     other settings will be checked against the range allowable by this target level.
626     Fail will returned upon Initialize call. If not known, users must set it to autodetect. Encoder will
627     calculate the right level that doesn't conflict with other settings. */
628     EncAVCLevel         iAVCLevel;
629 
630     /** Specifies the encoding mode. This translates to the complexity of encoding modes and
631     error resilient tools. This is for future use - enc mode will be used to set other parameters to appropriate
632     values.
633     */
634 
635     EncEncodingMode     iEncMode;
636 
637     /** Specifies that SPS and PPS are retrieved first and sent out-of-band */
638     //bool              iOutOfBandParamSet;
639 
640 
641 } PV_VideoEncodeParam;
642 
643 
644 
645 /** Structure for input format information */
646 typedef struct PV_VideoInputFormat
647 {
648     /** Contains the width in pixels of the input frame. */
649     int32           iFrameWidth;
650 
651     /** Contains the height in pixels of the input frame. */
652     int32           iFrameHeight;
653 
654     /** Contains the input frame rate in the unit of frame per second. */
655     float           iFrameRate;
656 
657     /** Contains Frame Orientation. Used for RGB input. 1 means Bottom_UP RGB, 0 means Top_Down RGB, -1 for video formats other than RGB*/
658     int             iFrameOrientation;
659 
660     /** Contains the format of the input video, e.g., YUV 4:2:0, UYVY, RGB24, etc. */
661     EncVideoInputFormat     iVideoFormat;
662 } PV_VideoInputFormat;
663 
664 
665 /** Structure for audio encode information */
666 typedef struct PV_AudioEncodeParam
667 {
668 
669     // DESCRIPTION: Final sampling frequency used when encoding in Hz.
670     //              For example, "44100" Hz.
671     // USAGE:       If the input sampling rate is not appropriate (e.g.,
672     //              the codec requires a different sampling frequency),
673     //              the CAEI will fill this in with the final sampling
674     //              rate. The CAEI will perform resampling if the
675     //              input sampling frequency is not the same as the output
676     //              sampling frequency.
677     uint32 iOutputSamplingRate;
678 
679     // DESCRIPTION: Number of output channels:1=Mono,2=Stereo. (Mono uses 1
680     //              channel; Stereo uses 2 channels).
681     // USAGE:       The CAEI will fill this in if it needs to convert
682     //              the input samples to what is required by the codec.
683     uint8 iOutputNumChannels;
684 
685     // DESCRIPTION: Currently, output bitrate is expressed as GSM-AMR type rate
686     PVMF_GSMAMR_Rate iAMRBitrate;
687 
688     // DESCRIPTION: output bitrate in bits per second for non-AMR codecs
689     uint32  iOutputBitrate;
690 
691     //
692     uint32  iMaxNumOutputFramesPerBuffer;
693 
694 
695 } PV_AudioEncodeParam;
696 
697 enum EInterleaveMode
698 {
699     EINTERLEAVE_LR,        // interleaved left-right
700     EGROUPED_LR,           // non-interleaved left-right
701     ENUM_INTERLEAVE_MODES  // number of modes supported
702 };
703 
704 /** Structure for input format information */
705 typedef struct PV_AudioInputFormat
706 {
707 
708 
709     // DESCRIPTION: Number of bits per sample. For example, set it to "16"
710     //              bits for PCM.
711     // USAGE:       The authoring application is required to fill this in.
712     //
713     int32 iInputBitsPerSample;
714 
715     // DESCRIPTION: Sampling rate of the input samples in Hz.
716     //              For example, set it to "22050" Hz. AMRNB is always 8khz
717     // USAGE:       The authoring application is required to fill this in.
718     //              If sampling rate is not known until CAEI is initialized,
719     //              use '0'.
720     uint32 iInputSamplingRate;
721 
722 
723     // DESCRIPTION: Number of input channels:1=Mono,2=Stereo.(Mono uses 1 channel;
724     //              Stereo uses 2 channels).
725     // USAGE:       The authoring application is required to fill this in.
726     //
727     uint8 iInputNumChannels;
728 
729     // DESCRIPTION: Whether to interleave or not the multi-channel input samples:
730     //              EINTERLEAVE_LR  = LRLRLRLRLR    (interleaved left-right)
731     //              EGROUPED_LR = LLLLLL...RRRRRR   (non-interleaved left-right)
732     // USAGE:       The authoring application is required to fill this in.
733     //
734     EInterleaveMode iInputInterleaveMode;
735 
736 
737 
738 } PV_AudioInputFormat;
739 
740 
741 
742 
743 
744 
745 
746 
747 ///////////////////////////////////////////////////////////////////////////////////////////////////////
748 //CAPABILITY AND CONFIG
749 
750 // Structure to hold the key string info for
751 // encnode's capability-and-config
752 struct PVOMXEncNodeKeyStringData
753 {
754     char iString[64];
755     PvmiKvpType iType;
756     PvmiKvpValueType iValueType;
757 };
758 
759 // The number of characters to allocate for the key string
760 #define PVOMXENCNODECONFIG_KEYSTRING_SIZE 128
761 
762 
763 // Key string info at the base level ("x-pvmf/encoder/video")
764 //                                or ("x-pvmf/encoder/audio")
765 
766 #ifdef _TEST_AE_ERROR_HANDLING
767 #define PVOMXENCNODECONFIG_BASE_NUMKEYS 8
768 #else
769 #define PVOMXENCNODECONFIG_BASE_NUMKEYS 3
770 #endif
771 
772 const PVOMXEncNodeKeyStringData PVOMXEncNodeConfigBaseKeys[PVOMXENCNODECONFIG_BASE_NUMKEYS] =
773 {
774     {"sampling_rate", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_UINT32},
775     {"channels", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_UINT32},
776     {"encoding_mode", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_UINT32}
777 #ifdef _TEST_AE_ERROR_HANDLING
778     , {"error_start_init", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_BOOL}
779     , {"error-encode", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_UINT32}
780     , {"error-node-cmd", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_UINT32}
781     , {"error-config-header", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_BOOL}
782     , {"data-path-stall", PVMI_KVPTYPE_VALUE, PVMI_KVPVALTYPE_UINT32}
783 #endif
784 };
785 
786 enum BaseKeys_IndexMapType
787 {
788     SAMPLING_RATE = 0, // audio
789     CHANNELS,           // audio
790     ENCODING_MODE       // avc
791 #ifdef _TEST_AE_ERROR_HANDLING
792     , ERROR_START_INIT
793     , ERROR_ENCODE
794     , ERROR_NODE_CMD
795     , ERROR_CONFIG_HEADER
796     , ERROR_DATAPATH_STALL
797 #endif
798 };
799 
800 // default encode parameters
801 #define DEFAULT_BITRATE 32000
802 #define DEFAULT_FRAME_WIDTH 176
803 #define DEFAULT_FRAME_HEIGHT 144
804 #define DEFAULT_FRAME_RATE 5
805 #define DEFAULT_I_FRAME_INTERVAL 10  // encode one I frame every 10 seconds.
806 #define DEFAULT_OMX_MP4ENC_QPI 15
807 #define DEFAULT_OMX_MP4ENC_QPP 12
808 #define DEFAULT_OMX_MP4ENC_QPB 12
809 
810 #define DEFAULT_OMX_AVCENC_QPI 25
811 #define DEFAULT_OMX_AVCENC_QPP 30
812 #define DEFAULT_OMX_AVCENC_QPB 33
813 
814 #define PVMF_AVCENC_NODE_SPS_VECTOR_RESERVE 16  // max allowed by standard
815 #define PVMF_AVCENC_NODE_PPS_VECTOR_RESERVE 256 // max allowed by standard
816 #define DEFAULT_PARAMS_SET_LENGTH 256
817 
818 typedef enum
819 {
820     PV_MPEG_VIDEO_SIMPLE_PROFILE = 0,
821     PV_MPEG_VIDEO_SIMPLE_SCALABLE_PROFILE,
822     PV_MPEG_VIDEO_CORE_PROFILE,
823     PV_MPEG_VIDEO_MAIN_PROFILE,
824     PV_MPEG_VIDEO_N_BIT_PROFILE,
825     PV_MPEG_VIDEO_SCALABLE_TEXTURE_PROFILE,
826     PV_MPEG_VIDEO_SIMPLE_FACE_ANIMATION_PROFILE,
827     PV_MPEG_VIDEO_SIMPLE_FBA_PROFILE,
828     PV_MPEG_VIDEO_BASIC_ANIMATED_TEXTURE_PROFILE,
829     PV_MPEG_VIDEO_HYBRID_PROFILE,
830     PV_MPEG_VIDEO_ADVANCE_REAL_TIME_SIMPLE_PROFILE,
831     PV_MPEG_VIDEO_CORE_SCALABLE_PROFILE,
832     PV_MPEG_VIDEO_ADVANCE_CODING_EFFICIENCY_PROFILE,
833     PV_MPEG_VIDEO_ADVANCE_CORE_PROFILE,
834     PV_MPEG_VIDEO_ADVANCE_SCALABLE_TEXTURE_PROFILE,
835     PV_MPEG_VIDEO_SIMPLE_STUDIO_PROFILE,
836     PV_MPEG_VIDEO_CORE_STUDIO_PROFILE,
837     PV_MPEG_VIDEO_ADVANCE_SIMPLE_PROFILE,
838     PV_MPEG_VIDEO_FINE_GRANUALITY_SCALABLE_PROFILE,
839     PV_MPEG_VIDEO_RESERVED_PROFILE
840 } PVMF_MPEGVideoProfileType;
841 
842 typedef enum
843 {
844     PV_MPEG_VIDEO_LEVEL0 = 0,
845     PV_MPEG_VIDEO_LEVEL1,
846     PV_MPEG_VIDEO_LEVEL2,
847     PV_MPEG_VIDEO_LEVEL3,
848     PV_MPEG_VIDEO_LEVEL4,
849     PV_MPEG_VIDEO_LEVEL5,
850     PV_MPEG_VIDEO_LEVEL_UNKNOWN
851 
852 } PVMF_MPEGVideoLevelType;
853 
854 /////////////////////////////////////////////////////////////////////////////////////////
855 
856 
857 
858 
859 
860 //Mimetypes for the custom interface
861 #define PVMF_OMX_ENC_NODE_MIMETYPE "pvxxx/OMXEncNode"
862 #define PVMF_BASEMIMETYPE "pvxxx"
863 
864 //Command queue type
865 typedef PVMFNodeCommandQueue<PVMFOMXEncNodeCommand, PVMFOMXEncNodeAllocator> PVMFOMXEncNodeCmdQ;
866 
867 
868 class PVMFOMXEncNode
869         : public OsclActiveObject
870         , public PVMFNodeInterface
871         , public OsclMemPoolFixedChunkAllocatorObserver
872         , public PVMFOMXEncNodeExtensionInterface
873         , public PVMFMetadataExtensionInterface
874         , public PvmiCapabilityAndConfig
875         , public PVMp4H263EncExtensionInterface
876         , public PVAudioEncExtensionInterface
877 {
878     public:
879         PVMFOMXEncNode(int32 aPriority);
880         ~PVMFOMXEncNode();
881 
882         // From PVMFNodeInterface
883         PVMFStatus ThreadLogon();
884         PVMFStatus ThreadLogoff();
885         PVMFStatus GetCapability(PVMFNodeCapability& aNodeCapability);
886         PVMFPortIter* GetPorts(const PVMFPortFilter* aFilter = NULL);
887         PVMFCommandId QueryUUID(PVMFSessionId, const PvmfMimeString& aMimeType,
888                                 Oscl_Vector<PVUuid, PVMFOMXEncNodeAllocator>& aUuids,
889                                 bool aExactUuidsOnly = false,
890                                 const OsclAny* aContext = NULL);
891         PVMFCommandId QueryInterface(PVMFSessionId, const PVUuid& aUuid,
892                                      PVInterface*& aInterfacePtr,
893                                      const OsclAny* aContext = NULL);
894         PVMFCommandId RequestPort(PVMFSessionId
895                                   , int32 aPortTag, const PvmfMimeString* aPortConfig = NULL, const OsclAny* aContext = NULL);
896         PVMFCommandId ReleasePort(PVMFSessionId, PVMFPortInterface& aPort, const OsclAny* aContext = NULL);
897         PVMFCommandId Init(PVMFSessionId, const OsclAny* aContext = NULL);
898         PVMFCommandId Prepare(PVMFSessionId, const OsclAny* aContext = NULL);
899         PVMFCommandId Start(PVMFSessionId, const OsclAny* aContext = NULL);
900         PVMFCommandId Stop(PVMFSessionId, const OsclAny* aContext = NULL);
901         PVMFCommandId Flush(PVMFSessionId, const OsclAny* aContext = NULL);
902         PVMFCommandId Pause(PVMFSessionId, const OsclAny* aContext = NULL);
903         PVMFCommandId Reset(PVMFSessionId, const OsclAny* aContext = NULL);
904         PVMFCommandId CancelAllCommands(PVMFSessionId, const OsclAny* aContextData = NULL);
905         PVMFCommandId CancelCommand(PVMFSessionId, PVMFCommandId aCmdId, const OsclAny* aContextData = NULL);
906 
907         // From PVMFPortActivityHandler
908         void HandlePortActivity(const PVMFPortActivity& aActivity);
909 
910         // From PVInterface
911         virtual void addRef();
912         virtual void removeRef();
913         virtual bool queryInterface(const PVUuid& uuid, PVInterface*& iface);
914 
915 
916         //**********begin PVMFMetadataExtensionInterface
917         uint32 GetNumMetadataKeys(char* query_key = NULL);
918         uint32 GetNumMetadataValues(PVMFMetadataList& aKeyList);
919         PVMFCommandId GetNodeMetadataKeys(PVMFSessionId aSessionId, PVMFMetadataList& aKeyList, uint32 starting_index, int32 max_entries,
920                                           char* query_key = NULL, const OsclAny* aContextData = NULL);
921         PVMFCommandId GetNodeMetadataValues(PVMFSessionId aSessionId, PVMFMetadataList& aKeyList,
922                                             Oscl_Vector<PvmiKvp, OsclMemAllocator>& aValueList, uint32 starting_index, int32 max_entries, const OsclAny* aContextData = NULL);
923         PVMFStatus ReleaseNodeMetadataKeys(PVMFMetadataList& aKeyList, uint32 starting_index, uint32 end_index);
924         PVMFStatus ReleaseNodeMetadataValues(Oscl_Vector<PvmiKvp, OsclMemAllocator>& aValueList, uint32 starting_index, uint32 end_index);
925         //**********End PVMFMetadataExtensionInterface
926 
927         bool VerifyParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters, int num_elements);
928 
929         //********** CB Functions to serve OpenMAX Encoder
930 
931         //Process callback functions. They will be executed in testapp thread context
932         //  These callbacks are used only in the Multithreaded component case
933         OsclReturnCode ProcessCallbackEventHandler_MultiThreaded(OsclAny* P);
934         OsclReturnCode ProcessCallbackEmptyBufferDone_MultiThreaded(OsclAny* P);
935         OsclReturnCode ProcessCallbackFillBufferDone_MultiThreaded(OsclAny* P);
936 
937         //Callback objects - again, these are used only in the case of Multithreaded component
938         EventHandlerThreadSafeCallbackAOEnc*     iThreadSafeHandlerEventHandler;
939         EmptyBufferDoneThreadSafeCallbackAOEnc* iThreadSafeHandlerEmptyBufferDone;
940         FillBufferDoneThreadSafeCallbackAOEnc*  iThreadSafeHandlerFillBufferDone;
941 
942         OMX_CALLBACKTYPE       iCallbacks; // structure that contains callback ptrs.
943         // OMX CALLBACKS
944         // 1) AO OMX component running in the same thread as the OMX node
945         //  In this case, the callbacks can be called directly from the component
946         //  The callback: OMX Component->CallbackEventHandler->EventHandlerProcessing
947         //  The callback can perform do RunIfNotReady
948 
949         // 2) Multithreaded component
950         //  In this case, the callback is made using the threadsafe callback (TSCB) AO
951         //  Component thread : OMX Component->CallbackEventHandler->TSCB(ReceiveEvent)
952         //  Node thread      : TSCB(ProcessEvent)->ProcessCallbackEventHandler_MultiThreaded->EventHandlerProcessing
953 
954 
955         //==============================================================================
956 
957         OMX_ERRORTYPE EventHandlerProcessing(OMX_OUT OMX_HANDLETYPE aComponent,
958                                              OMX_OUT OMX_PTR aAppData,
959                                              OMX_OUT OMX_EVENTTYPE aEvent,
960                                              OMX_OUT OMX_U32 aData1,
961                                              OMX_OUT OMX_U32 aData2,
962                                              OMX_OUT OMX_PTR aEventData);
963 
964         OMX_ERRORTYPE EmptyBufferDoneProcessing(OMX_OUT OMX_HANDLETYPE aComponent,
965                                                 OMX_OUT OMX_PTR aAppData,
966                                                 OMX_OUT OMX_BUFFERHEADERTYPE* aBuffer);
967 
968         OMX_ERRORTYPE FillBufferDoneProcessing(OMX_OUT OMX_HANDLETYPE aComponent,
969                                                OMX_OUT OMX_PTR aAppData,
970                                                OMX_OUT OMX_BUFFERHEADERTYPE* aBuffer);
971 
972 
973 
IsComponentMultiThreaded()974         bool IsComponentMultiThreaded()
975         {
976             return iIsOMXComponentMultiThreaded;
977         };
978 
979 
980         // From PvmiCapabilityAndConfig
981         void setObserver(PvmiConfigAndCapabilityCmdObserver* aObserver);
982         PVMFStatus getParametersSync(PvmiMIOSession aSession, PvmiKeyType aIdentifier, PvmiKvp*& aParameters, int& aNumParamElements, PvmiCapabilityContext aContext);
983         PVMFStatus releaseParameters(PvmiMIOSession aSession, PvmiKvp* aParameters, int aNumElements);
984         void createContext(PvmiMIOSession aSession, PvmiCapabilityContext& aContext);
985         void setContextParameters(PvmiMIOSession aSession, PvmiCapabilityContext& aContext, PvmiKvp* aParameters, int aNumParamElements);
986         void DeleteContext(PvmiMIOSession aSession, PvmiCapabilityContext& aContext);
987         void setParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters, int aNumElements, PvmiKvp* &aRetKVP);
988         PVMFCommandId setParametersAsync(PvmiMIOSession aSession, PvmiKvp* aParameters, int aNumElements, PvmiKvp*& aRetKVP, OsclAny* aContext = NULL);
989         uint32 getCapabilityMetric(PvmiMIOSession aSession);
990         PVMFStatus verifyParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters, int aNumElements);
991 
992         // Virtual functions of PVMp4H263EncExtensionInterface
993 //  OSCL_IMPORT_REF void addRef();
994 //  OSCL_IMPORT_REF void removeRef();
995 //  OSCL_IMPORT_REF bool queryInterface(const PVUuid& uuid, PVInterface*& iface);
996         OSCL_IMPORT_REF bool SetNumLayers(uint32 aNumLayers);
997         OSCL_IMPORT_REF bool SetOutputBitRate(uint32 aLayer, uint32 aBitRate);
998         OSCL_IMPORT_REF bool SetOutputFrameSize(uint32 aLayer, uint32 aWidth, uint32 aHeight);
999         OSCL_IMPORT_REF bool SetOutputFrameRate(uint32 aLayer, OsclFloat aFrameRate);
1000         OSCL_IMPORT_REF bool SetSegmentTargetSize(uint32 aLayer, uint32 aSizeBytes);
1001         OSCL_IMPORT_REF bool SetRateControlType(uint32 aLayer, PVMFVENRateControlType aRateControl);
1002         OSCL_IMPORT_REF bool SetDataPartitioning(bool aDataPartitioning);
1003         OSCL_IMPORT_REF bool SetRVLC(bool aRVLC);
1004         OSCL_IMPORT_REF bool SetIFrameInterval(uint32 aIFrameInterval);
1005         OSCL_IMPORT_REF bool GetVolHeader(OsclRefCounterMemFrag& aVolHeader);
1006         OSCL_IMPORT_REF bool RequestIFrame();
1007         OSCL_IMPORT_REF bool SetSceneDetection(bool aSCD);
1008         OSCL_IMPORT_REF bool SetCodec(PVMFFormatType aCodec);
1009 
1010 
1011 
1012         PVMFStatus SetCodecType(PVMFFormatType aCodec);
1013 
1014         OSCL_IMPORT_REF virtual bool SetFSIParam(uint8* aFSIBuff, int aFSIBuffLength);
1015 
1016         // from AudioEncExtensionInterface
1017         OSCL_IMPORT_REF PVMFStatus SetOutputBitRate(PVMF_GSMAMR_Rate aBitRate);
1018         OSCL_IMPORT_REF PVMFStatus SetMaxNumOutputFramesPerBuffer(uint32 aNumOutputFrames);
1019         OSCL_IMPORT_REF PVMFStatus SetOutputBitRate(uint32 aBitRate);
1020         OSCL_IMPORT_REF PVMFStatus SetOutputNumChannel(uint32 aNumChannel);
1021         OSCL_IMPORT_REF PVMFStatus SetOutputSamplingRate(uint32 aSamplingRate);
1022 
1023 
1024         PVMFStatus SetInputSamplingRate(uint32 aSamplingRate);
1025         PVMFStatus SetInputBitsPerSample(uint32 aBitsPerSample);
1026         PVMFStatus SetInputNumChannels(uint32 aNumChannels);
1027         uint32 GetOutputBitRate(); // for audio - void
1028         // encoder input format setting
1029         /////////////////////////////////////////////////////
1030         //      Encoder settings routine
1031         /////////////////////////////////////////////////////
1032         PVMFStatus SetInputFormat(PVMFFormatType aFormat);
1033         PVMFStatus SetInputFrameSize(uint32 aWidth, uint32 aHeight, uint8 aFrmOrient = 0);
1034         PVMFStatus SetInputFrameRate(OsclFloat aFrameRate);
1035 
1036 
1037         PVMFFormatType GetCodecType();
1038         uint32 GetOutputBitRate(uint32 aLayer);
1039         OsclFloat GetOutputFrameRate(uint32 aLayer);
1040         PVMFStatus GetOutputFrameSize(uint32 aLayer, uint32& aWidth, uint32& aHeight);
1041         uint32 GetIFrameInterval();
1042         uint32 GetOutputSamplingRate();
1043         uint32 GetOutputNumChannels();
1044 
1045     private:
1046         void CommandComplete(PVMFOMXEncNodeCmdQ& aCmdQ, PVMFOMXEncNodeCommand& aCmd, PVMFStatus aStatus, OsclAny* aEventData = NULL);
1047 
1048         void DoQueryUuid(PVMFOMXEncNodeCommand&);
1049         void DoQueryInterface(PVMFOMXEncNodeCommand&);
1050         void DoRequestPort(PVMFOMXEncNodeCommand&);
1051         void DoReleasePort(PVMFOMXEncNodeCommand&);
1052         void DoInit(PVMFOMXEncNodeCommand&);
1053         void DoPrepare(PVMFOMXEncNodeCommand&);
1054         void DoStart(PVMFOMXEncNodeCommand&);
1055         void DoStop(PVMFOMXEncNodeCommand&);
1056         void DoPause(PVMFOMXEncNodeCommand&);
1057         void DoReset(PVMFOMXEncNodeCommand&);
1058         void DoFlush(PVMFOMXEncNodeCommand&);
1059         PVMFStatus DoGetNodeMetadataKey(PVMFOMXEncNodeCommand&);
1060         PVMFStatus DoGetNodeMetadataValue(PVMFOMXEncNodeCommand&);
1061         void DoCancelAllCommands(PVMFOMXEncNodeCommand&);
1062         void DoCancelCommand(PVMFOMXEncNodeCommand&);
1063 
1064         void Run();
1065         bool ProcessCommand(PVMFOMXEncNodeCommand& aCmd);
1066         bool ProcessIncomingMsg(PVMFPortInterface* aPort);
1067         bool ProcessOutgoingMsg(PVMFPortInterface* aPort);
1068         PVMFStatus HandleProcessingState();
1069 
1070 
1071 
1072 
1073         // DV:
1074         bool NegotiateVideoComponentParameters();
1075         bool SetMP4EncoderParameters();
1076         bool SetH263EncoderParameters();
1077         bool SetH264EncoderParameters();
1078 
1079         bool NegotiateAudioComponentParameters();
1080         bool SetAMREncoderParameters();
1081         bool SetAACEncoderParameters();
1082 
1083         bool SetDefaultCapabilityFlags();
1084         bool CreateOutMemPool(uint32 num);
1085         bool CreateInputMemPool(uint32 num);
1086         bool ProvideBuffersToComponent(OsclMemPoolFixedChunkAllocator *aMemPool, // allocator
1087                                        uint32 aAllocSize,    // size to allocate from pool (hdr only or hdr+ buffer)
1088                                        uint32 aNumBuffers,    // number of buffers
1089                                        uint32 aActualBufferSize, // aactual buffer size
1090                                        uint32 aPortIndex,      // port idx
1091                                        bool aUseBufferOK,   // can component use OMX_UseBuffer?
1092                                        bool aIsThisInputBuffer // is this input or output
1093                                       );
1094 
1095         bool FreeBuffersFromComponent(OsclMemPoolFixedChunkAllocator *aMemPool, // allocator
1096                                       uint32 aAllocSize,     // size to allocate from pool (hdr only or hdr+ buffer)
1097                                       uint32 aNumBuffers,    // number of buffers
1098                                       uint32 aPortIndex,      // port idx
1099                                       bool  aIsThisInputBuffer      // is this input or output
1100                                      );
1101 
1102         OsclSharedPtr<class PVMFMediaDataImpl> WrapOutputBuffer(uint8 *pData, uint32 aDataLen, OsclAny *pContext);
1103         bool QueueOutputBuffer(OsclSharedPtr<PVMFMediaDataImpl> &mediadataimplout, uint32 aDataLen);
1104 
1105         bool SendOutputBufferToOMXComponent();
1106         bool SendInputBufferToOMXComponent();
1107 
1108 
1109         bool SendEOSBufferToOMXComponent();
1110 
1111         bool SendBeginOfMediaStreamCommand(void);
1112         bool SendEndOfTrackCommand(void);
1113 
1114 
1115 
1116         bool ReleaseAllPorts();
1117         bool DeleteOMXEncoder();
1118 
1119         void ChangeNodeState(TPVMFNodeInterfaceState aNewState);
1120 
1121         void HandleComponentStateChange(OMX_U32 encoder_state);
1122 
1123         // Capability And Config Helper Methods
1124         PVMFStatus DoCapConfigGetParametersSync(PvmiKeyType aIdentifier, PvmiKvp*& aParameters, int& aNumParamElements, PvmiCapabilityContext aContext);
1125         PVMFStatus DoCapConfigReleaseParameters(PvmiKvp* aParameters, int aNumElements);
1126         void DoCapConfigSetParameters(PvmiKvp* aParameters, int aNumElements, PvmiKvp* &aRetKVP);
1127         PVMFStatus DoCapConfigVerifyParameters(PvmiKvp* aParameters, int aNumElements);
1128 
1129         PVMFStatus GetConfigParameter(PvmiKvp*& aParameters, int& aNumParamElements, int32 aIndex, PvmiKvpAttr aReqattr);
1130         PVMFStatus VerifyAndSetConfigParameter(PvmiKvp& aParameter, bool aSetParam);
1131 
1132         PVMFStatus GetProfileAndLevel(PVMF_MPEGVideoProfileType& aProfile, PVMF_MPEGVideoLevelType& aLevel);
1133 
1134 
1135 
1136         void freechunkavailable(OsclAny*);
1137 
1138         PVMFCommandId QueueCommandL(PVMFOMXEncNodeCommand& aCmd);
1139 
1140 
1141         int32 PushBackKeyVal(Oscl_Vector<PvmiKvp, OsclMemAllocator>*&, PvmiKvp&);
1142         int32 Push_Back_MetadataKeys(const char*);
1143         int32 Push_Back_MetadataKeys(PVMFMetadataList*&, uint32);
1144         int32 CreateNewArray(char*&, int32);
1145         int32 MemAllocate(OsclAny*& , OsclMemPoolFixedChunkAllocator*, uint32);
1146 
1147         bool ParseFullAVCFramesIntoNALs(OMX_BUFFERHEADERTYPE* aOutputBuffer);
1148         bool AVCAnnexBGetNALUnit(uint8 *bitstream, uint8 **nal_unit, int32 *size, bool getPtrOnly);
1149         bool CheckM4vVopStartCode(uint8* data, int* len);
1150 
1151         friend class PVMFOMXEncPort;
1152 
1153         // Ports pointers
1154         PVMFPortInterface* iInPort;
1155         PVMFPortInterface* iOutPort;
1156 
1157         // Commands
1158         PVMFOMXEncNodeCmdQ iInputCommands;
1159         PVMFOMXEncNodeCmdQ iCurrentCommand;
1160 
1161         // Shared pointer for Media Msg.Input buffer
1162         PVMFSharedMediaDataPtr iDataIn;
1163 
1164 
1165         // OUTPUT BUFFER RELATED MEMBERS
1166 
1167 
1168         // Output buffer memory pool
1169         OsclMemPoolFixedChunkAllocator *iOutBufMemoryPool;
1170 
1171         // Memory pool for simple media data
1172         OsclMemPoolFixedChunkAllocator *iMediaDataMemPool;
1173 
1174         // Size of output buffer (negotiated with component)
1175         uint32 iOMXComponentOutputBufferSize;
1176 
1177         // size of output to allocate (OMX_ALLOCATE_BUFFER =  size of buf header )
1178         // (OMX_USE_BUFFER = size of buf header + iOMXCoponentOutputBufferSize)
1179         uint32 iOutputAllocSize;
1180 
1181         // Number of output buffers (negotiated with component)
1182         uint32 iNumOutputBuffers;
1183 
1184         // Number of output buffers in possession of the component or downstream,
1185         // namely, number of unavailable buffers
1186         uint32 iNumOutstandingOutputBuffers;
1187 
1188         // flag to prevent sending output buffers downstream during flushing etc.
1189         bool iDoNotSendOutputBuffersDownstreamFlag;
1190 
1191         // flag to prevent freeing the buffers twice
1192         bool iOutputBuffersFreed;
1193 
1194 
1195         // INPUT BUFFER RELATED MEMBERS
1196         OsclMemPoolFixedChunkAllocator *iInBufMemoryPool;
1197         uint32 iOMXComponentInputBufferSize; // size of input buffer that the component sees (negotiated with the component)
1198         uint32 iInputAllocSize;     // size of input buffer to allocate (OMX_ALLOCATE_BUFFER =  size of buf header )
1199         // (OMX_USE_BUFFER = size of buf header + iOMXCoponentInputBufferSize)
1200         uint32 iNumInputBuffers; // total num of input buffers (negotiated with component)
1201 
1202         uint32 iNumOutstandingInputBuffers; // number of input buffers in use (i.e. unavailable)
1203 
1204         bool iDoNotSaveInputBuffersFlag;
1205 
1206         // flag to prevent freeing buffers twice
1207         bool iInputBuffersFreed;
1208 
1209         // input buffer fragmentation etc.
1210         uint32 iCopyPosition;               // for copying memfrag data into a buffer
1211         uint32 iFragmentSizeRemainingToCopy;
1212         bool    iIsNewDataFragment;
1213 
1214         // input data info
1215         uint32 iCurrFragNum;
1216         uint32 iCodecSeqNum;    // sequence number tracking
1217         uint32 iInPacketSeqNum;
1218 
1219         uint32 iInTimestamp;
1220         uint32 iInDuration;
1221         uint32 iInNumFrags;
1222         uint32 iCurrentMsgMarkerBit;
1223 
1224         // DYNAMIC PORT RE-CONFIGURATION
1225         uint32 iInputPortIndex;
1226         uint32 iOutputPortIndex;
1227         OMX_PARAM_PORTDEFINITIONTYPE iParamPort;
1228         uint32 iPortIndexForDynamicReconfig;
1229         bool iSecondPortReportedChange;
1230         bool iDynamicReconfigInProgress;
1231         uint32 iSecondPortToReconfig;
1232 
1233 
1234 
1235         OMX_BUFFERHEADERTYPE *iInputBufferToResendToComponent; // ptr to input buffer that is not empty, but that the OMX component returned
1236         // we need to resend this same buffer back to the component
1237 
1238 
1239         ////////////////// OMX COMPONENT CAPABILITY RELATED MEMBERS
1240         bool iOMXComponentSupportsExternalOutputBufferAlloc;
1241         bool iOMXComponentSupportsExternalInputBufferAlloc;
1242         bool iOMXComponentSupportsMovableInputBuffers;
1243         bool iOMXComponentUsesNALStartCodes;
1244         bool iOMXComponentSupportsPartialFrames;
1245         bool iOMXComponentCanHandleIncompleteFrames;
1246         bool iOMXComponentUsesFullAVCFrames;
1247 
1248         bool iSetMarkerBitForEveryFrag;
1249         bool iIsOMXComponentMultiThreaded;
1250 
1251         // State definitions for HandleProcessingState() state machine
1252         typedef enum
1253         {
1254             EPVMFOMXEncNodeProcessingState_Idle,                  //default state after constraction/reset
1255             EPVMFOMXEncNodeProcessingState_ReadyToEncode,         //nornal operation state of the encoder
1256             EPVMFOMXEncNodeProcessingState_WaitForOutputBuffer,   //wait state for avalible media output buffer
1257             EPVMFOMXEncNodeProcessingState_WaitForOutputPort,     //wait state, output port is busy
1258             EPVMFOMXEncNodeProcessingState_WaitForOutgoingQueue,   //wait state, outgoing queue
1259             EPVMFOMXEncNodeProcessingState_PortReconfig,            // Dynamic Port Reconfiguration - step 1
1260             EPVMFOMXEncNodeProcessingState_WaitForBufferReturn,     //  step 2
1261             EPVMFOMXEncNodeProcessingState_WaitForPortDisable,      // Dynamic Port Reconfiguration - step 3
1262             EPVMFOMXEncNodeProcessingState_PortReEnable,            // Dynamic Port Reconfiguration - step 4
1263             EPVMFOMXEncNodeProcessingState_WaitForPortEnable,       // step 5
1264             EPVMFOMXEncNodeProcessingState_Stopping             // when STOP command is issued, the node has to wait for component to transition into
1265             // idle state. The buffers keep coming back , the node is rescheduled
1266             // to run. Prevent the node from sending buffers back
1267         } PVMFOMXEncNode_ProcessingState;
1268 
1269         // State of HandleProcessingState() state machine
1270         PVMFOMXEncNode_ProcessingState iProcessingState;
1271 
1272         // Handle of OMX Component
1273         OMX_HANDLETYPE iOMXEncoder;
1274 
1275         // Current State of the component
1276         OMX_STATETYPE iCurrentEncoderState;
1277 
1278         // BOS
1279         bool iSendBOS;
1280         uint32 iStreamID;
1281         uint32 iBOSTimestamp;
1282 
1283         //EOS control flags
1284         bool iIsEOSSentToComponent;
1285         bool iIsEOSReceivedFromComponent;
1286 
1287         // Send Fsi configuration flag
1288         bool    sendYuvFsi;
1289 
1290         // Width of encoded frame
1291         int32 iYUVWidth;
1292         // Height of encoded frame
1293         int32 iYUVHeight;
1294 
1295 
1296 
1297         // Pointer to input data fragment
1298         uint8* iBitstreamBuffer;
1299         // Size of input data fragment
1300         int32 iBitstreamSize;
1301 
1302         // Output frame sequence counter
1303         uint32 iSeqNum;
1304 
1305         // Input frame sequence counter
1306         uint32 iSeqNum_In;
1307 
1308         // Added to Scheduler Flag
1309         bool iIsAdded;
1310 
1311         // Log related
1312         PVLogger* iLogger;
1313         PVLogger* iDataPathLogger;
1314         PVLogger* iClockLogger;
1315         PVLogger *iRunlLogger;
1316         const char *iNodeTypeId;
1317 
1318         // Counter of fragment read from current Media Msg.Input buffer
1319         uint fragnum;
1320         // Number of fragments in the Media Msg.Input buffer
1321         uint numfrags;
1322 
1323 
1324         // Node configuration update
1325         //PVMFOMXEncNodeConfig iNodeConfig;
1326 
1327         // Capability exchange
1328         PVMFNodeCapability iCapability;
1329 
1330         // Reference counter for extension
1331         uint32 iExtensionRefCount;
1332 
1333         // Vector for KVP
1334         Oscl_Vector<OSCL_HeapString<OsclMemAllocator>, OsclMemAllocator> iAvailableMetadataKeys;
1335 
1336         // EOS flag
1337         bool iEndOfDataReached;
1338         // Time stame upon EOS
1339         PVMFTimestamp iEndOfDataTimestamp;
1340 
1341         /* Diagnostic log related */
1342         PVLogger* iDiagnosticsLogger;
1343         bool iDiagnosticsLogged;
1344         void LogDiagnostics();
1345 
1346         uint32 iFrameCounter;
1347 
1348 
1349 
1350         uint32 iAvgBitrateValue;
1351         bool iResetInProgress;
1352         bool iResetMsgSent;
1353         bool iStopInResetMsgSent;
1354 
1355         PV_VideoEncodeParam iVideoEncodeParam;
1356         PV_VideoInputFormat iVideoInputFormat;
1357         PV_AudioEncodeParam iAudioEncodeParam;
1358         PV_AudioInputFormat iAudioInputFormat;
1359 
1360         PVMFFormatType iInFormat;
1361         PVMFFormatType iOutFormat;
1362 
1363         PVMFOMXEncNodeAllocDestructDealloc iAlloc;
1364         OsclRefCounterMemFrag iVolHeader; /** Vol header */
1365         OsclRefCounterMemFrag iConfigHeader;
1366 
1367         OsclRefCounterMemFrag iParamSet; /* place holder for sps,pps values */
1368         OsclMemoryFragment iSPSs[PVMF_AVCENC_NODE_SPS_VECTOR_RESERVE]; // just ptrs
1369         OsclMemoryFragment iPPSs[PVMF_AVCENC_NODE_PPS_VECTOR_RESERVE]; // just ptrs
1370         int   iNumSPSs;
1371         int   iNumPPSs;
1372         bool iSpsPpsSequenceOver;
1373         OsclSharedPtr<PVMFMediaDataImpl> iPreviousMediaData;
1374         bool iFirstNAL;
1375         uint32* iNALSizeArray;
1376         uint8** iNALPtrArray;
1377         uint32 iNALSizeArrayMaxElems;
1378         uint32 iNumNALs;
1379         uint32 iFirstNALStartCodeSize;
1380 
1381         uint32 iEndOfFrameFlagPrevious;
1382         uint32 iKeyFrameFlagPrevious;
1383         uint32 iEndOfNALFlagPrevious;
1384         uint32 iTimeStampPrevious;
1385         uint32 iBufferLenPrevious;
1386 
1387         uint32 iEndOfFrameFlagOut;
1388         uint32 iKeyFrameFlagOut;
1389         uint32 iEndOfNALFlagOut;
1390         uint32 iTimeStampOut;
1391         uint32 iBufferLenOut;
1392         OsclAny **out_ctrl_struct_ptr ;
1393         OsclAny **out_buff_hdr_ptr ;
1394         OsclAny **in_ctrl_struct_ptr ;
1395         OsclAny **in_buff_hdr_ptr ;
1396 
1397 
1398 
1399 #ifdef _TEST_AE_ERROR_HANDLING
1400         bool   iErrorHandlingInit;
1401         uint32 iErrorHandlingEncodeCount;
1402         int    iCountFrames;
1403         uint32 iErrorNodeCmd;
1404         bool iErrorConfigHeader;
1405         uint32 iErrorEncodeFlag;
1406         uint32 iErrorDataPathStall;
1407 #endif
1408         bool bIsQCOMOmxComp;
1409 };
1410 
1411 
1412 #endif // PVMF_OMXENC_NODE_H_INCLUDED
1413 
1414