{"id":658,"date":"2013-07-16T21:21:46","date_gmt":"2013-07-16T13:21:46","guid":{"rendered":"http:\/\/guoh.ai\/lifelog\/?p=658"},"modified":"2013-07-16T21:21:46","modified_gmt":"2013-07-16T13:21:46","slug":"integrating-flac-encoder-into-stagefright","status":"publish","type":"post","link":"https:\/\/guoh.ai\/lifelog\/2013\/07\/integrating-flac-encoder-into-stagefright\/","title":{"rendered":"FLAC encoder\u662f\u5982\u4f55\u96c6\u6210\u5230Stagefright\u7684(OpenMAX IL)"},"content":{"rendered":"<p>\u4e3e\u4e2a\u4f8b\u5b50\u6765\u770b\u4e0b\uff0c<a href=\"http:\/\/en.wikipedia.org\/wiki\/Codec\" target=\"_blank\">codec<\/a>\u662f\u5982\u4f55\u548cOpenMAX IL\u6c9f\u901a\u7684\uff0c\u8fd9\u5176\u5b9e\u5e94\u8be5\u662f<a href=\"http:\/\/guoh.ai\/lifelog\/2013\/06\/interaction-between-stagefright-and-codec\/\" target=\"_blank\">Stagefright\u548ccodec\u7684\u4ea4\u4e92<\/a>\u8fd9\u7bc7\u535a\u5ba2\u5f53\u4e2d\u7684\uff0c\u60f3\u60f3\u6bd4\u8f83\u72ec\u7acb\uff0c\u53c8\u6bd4\u8f83\u957f\uff0c\u5e72\u8106\u5355\u72ec\u62ff\u51fa\u6765\u5199\u4e00\u4e0b\u3002<\/p>\n<p>\u901a\u5e38\u6765\u8bf4\u4e00\u4e2acodec(encoder\u6216\u8005decoder)\u65e0\u8bba\u600e\u4e48\u590d\u6742\uff0c\u5b83\u603b\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u6765\u662f\u6211\u4eec\u8f93\u5165\u4e00\u6bb5\u6570\u636e\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u6211\u4eec\u6765\u83b7\u53d6\u4e00\u6bb5\u6570\u636e\uff0c\u56e0\u4e3a\u5b83\u7684\u529f\u80fd\u5c31\u662f\u8fd9\u6837\u7684\u3002<br \/>\n\u6240\u4ee5\u6211\u4eec\u8981\u628a\u4e00\u4e2acodec\u4f7f\u7528OpenMAX IL\u7684\u65b9\u5f0f\u96c6\u6210\u8fdb\u6765\uff0c\u5bf9\u6211\u4eec\u6765\u8bf4\u5de5\u4f5c\u91cf\u4e0d\u7b97\u5f88\u5927\u3002\u8fd9\u91cc\u4ee5FLAC encoder\u6765\u4e3e\u4e2a\u4f8b\u5b50(\u56e0\u4e3a\u5b83\u6bd4\u8f83\u7b80\u5355)\uff0cFLAC\u672c\u8eab\u8fd9\u4e2acodec\u76f8\u5173\u7684\u4fe1\u606f\u8bf7\u53c2\u89c1<a href=\"http:\/\/xiph.org\/flac\/\" target=\"_blank\">http:\/\/xiph.org\/flac\/<\/a>\uff0c\u5728Android\u5f53\u4e2d\u6e90\u7801\u4f4d\u4e8e\/path\/to\/aosp\/external\/flac\/\uff0c\u5728Android\u5f53\u4e2dOpenMAX IL\u4e0e\u5b83\u4ea4\u4e92\u662f\u901a\u8fc7libstagefright_soft_flacenc.so\u8fd9\u4e2a\u4e1c\u897f\uff0c\u6211\u4eec\u4eca\u5929\u5c31\u91cd\u70b9\u5173\u6ce8\u8fd9\u4e2a\uff0c\u5b83\u7684\u6e90\u7801\u4f4d\u4e8e\/path\/to\/aosp\/frameworks\/av\/media\/libstagefright\/codecs\/flac\/enc\/\u3002<\/p>\n<p>\u5bf9Android\u5f53\u4e2dStagefright\u548cOpenMAX IL\u4e0d\u719f\u6089\u7684\u8bdd\uff0c\u8fd8\u662f\u5efa\u8bae\u5148\u770b\u6587\u7ae0\u5f00\u59cb\u63d0\u5230\u7684\u90a3\u7bc7\u3002<\/p>\n<p>\u9996\u5148SoftFlacEncoder\u7ee7\u627f\u81eaSimpleSoftOMXComponent\uff0c\u91cd\u5199\u4e864\u4e2a\u65b9\u6cd5\uff0c\u5206\u522b\u662f<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">virtual OMX_ERRORTYPE initCheck() const; \/\/ \u5728SoftOMXComponent\u5f53\u4e2d\u662f\u7a7a\u5b9e\u73b0\uff0c\u81ea\u5df1\u7684codec\u9700\u8981\u5b9e\u73b0\u6765\u8ba9\u4ed6\u4eba\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u6765\u63a2\u6d4b\u4f60\u7684codec\u662f\u5426\u6b63\u5e38\r\nvirtual OMX_ERRORTYPE internalGetParameter(\r\n        OMX_INDEXTYPE index, OMX_PTR params); \/\/ \u5c31\u662fOpenMax IL\u7ec4\u4ef6\u7684getParameter\r\n\r\nvirtual OMX_ERRORTYPE internalSetParameter(\r\n        OMX_INDEXTYPE index, const OMX_PTR params); \/\/ \u5c31\u662fOpenMax IL\u7ec4\u4ef6\u7684setParameter\r\n\r\nvirtual void onQueueFilled(OMX_U32 portIndex); \/\/ \u5c31\u662fOpenMax IL\u7ec4\u4ef6\u7684emptyThisBuffer\u548cfillThisBuffer\uff0c\u5982\u679c\u4f60\u4e0d\u6e05\u695a\uff0c\u5e76\u4e14\u8fd8\u5728\u770b\u8fd9\u7bc7\u6587\u7ae0\u7684\u8bdd\uff0c\u90a3\u4f60\u4e00\u5b9a\u662f\u5728\u5f53\u6563\u6587\u770b ^_^<\/pre>\n<p>\u8fd9\u56db\u4e2a\u65b9\u6cd5\u5982\u679c\u4f60\u4e0d\u660e\u767d\uff0c\u5c31\u5148\u7ffb\u7ffbOpenMax IL\/Stagefright\u76f8\u5173\u4ee3\u7801\u6216\u8005\u89c4\u8303\u3002<\/p>\n<p>\u53e6\u5916\u5b83\u8fd8\u6709\u51e0\u4e2a\u79c1\u6709\u65b9\u6cd5<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">void initPorts(); \/\/ OpenMAX IL\u901a\u4fe1\u9700\u8981port\r\n\r\nOMX_ERRORTYPE configureEncoder(); \/\/ FLAC\u672c\u8eab\u4e5f\u9700\u8981\u4e9b\u914d\u7f6e\uff0c\u6bd4\u5982\u628a\u4e0b\u9762\u7684callback\u8bbe\u7f6e\u8fdb\u53bb\r\n\r\n\/\/ FLAC encoder callbacks\r\n\/\/ maps to encoderEncodeFlac()\r\nstatic FLAC__StreamEncoderWriteStatus flacEncoderWriteCallback(\r\n        const FLAC__StreamEncoder *encoder, const FLAC__byte buffer&#x5B;],\r\n        size_t bytes, unsigned samples, unsigned current_frame, void *client_data); \/\/ \u8fd9\u4e2a\u662f\u5b9e\u9645\u4e22\u4e0b\u53bb\u7684callback\r\n\r\nFLAC__StreamEncoderWriteStatus onEncodedFlacAvailable(\r\n            const FLAC__byte buffer&#x5B;],\r\n            size_t bytes, unsigned samples, unsigned current_frame); \/\/ \u8fd9\u4e2a\u53ea\u662f\u505a\u4e86\u4e2aC\u5411C++\u7684\u8f6c\u6362\r\n<\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u662fcallback\u51fd\u6570\uff0c\u5176\u5b9e\u5c31\u4e00\u4e2acallback\uff0c\u56e0\u4e3a\u5982\u4e0b<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\/\/ static\r\nFLAC__StreamEncoderWriteStatus SoftFlacEncoder::flacEncoderWriteCallback(\r\n            const FLAC__StreamEncoder *encoder, const FLAC__byte buffer&#x5B;],\r\n            size_t bytes, unsigned samples, unsigned current_frame, void *client_data) {\r\n    return ((SoftFlacEncoder*) client_data)-&gt;onEncodedFlacAvailable(\r\n            buffer, bytes, samples, current_frame);\r\n}<\/pre>\n<p>\u4e3a\u4ec0\u4e48\u8981\u6709callback\uff1f\u4e4b\u524d\u6709\u8bb2\u8fc7\u4e00\u4e2acodec\u603b\u6709\u4e00\u4e2a\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u5728\u8fd9\u91ccFLAC\u6709\u7ed9\u6211\u4eec\u4e00\u4e2a\u51fd\u6570<strong><em>FLAC__stream_encoder_process_interleaved(&#8230;)<\/em><\/strong>\u6765\u8f93\u5165\u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2acallback\u6765\u8f93\u51fa\u6570\u636e\uff0c\u4ec5\u4ec5\u8fd9\u6837\uff01<\/p>\n<p>\u53ef\u4ee5\u53c2\u7167\u4e0bFLAC\u5934\u6587\u4ef6\u7684\u8bf4\u660e<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\/** Submit data for encoding.\r\n *  This version allows you to supply the input data where the channels\r\n *  are interleaved into a single array (i.e. channel0_sample0,\r\n *  channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).\r\n *  The samples need not be block-aligned but they must be\r\n *  sample-aligned, i.e. the first value should be channel0_sample0\r\n *  and the last value channelN_sampleM.  Each sample should be a signed\r\n *  integer, right-justified to the resolution set by\r\n *  FLAC__stream_encoder_set_bits_per_sample().  For example, if the\r\n *  resolution is 16 bits per sample, the samples should all be in the\r\n *  range &#x5B;-32768,32767].\r\n *\r\n *  For applications where channel order is important, channels must\r\n *  follow the order as described in the\r\n *  &lt;A HREF=&quot;..\/format.html#frame_header&quot;&gt;frame header&lt;\/A&gt;.\r\n *\r\n * \\param  encoder  An initialized encoder instance in the OK state.\r\n * \\param  buffer   An array of channel-interleaved data (see above).\r\n * \\param  samples  The number of samples in one channel, the same as for\r\n *                  FLAC__stream_encoder_process().  For example, if\r\n *                  encoding two channels, \\c 1000 \\a samples corresponds\r\n *                  to a \\a buffer of 2000 values.\r\n * \\assert\r\n *    \\code encoder != NULL \\endcode\r\n *    \\code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \\endcode\r\n * \\retval FLAC__bool\r\n *    \\c true if successful, else \\c false; in this case, check the\r\n *    encoder state with FLAC__stream_encoder_get_state() to see what\r\n *    went wrong.\r\n *\/\r\nFLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer&#x5B;], unsigned samples);<\/pre>\n<p>\u8fd9\u4e2a\u5c31\u662f\u6ce8\u518ccallback\u7684\u5730\u65b9\uff0c\u6211\u4eec\u6ce8\u518c\u7684\u662f<strong><em>FLAC__StreamEncoderWriteCallback<\/em><\/strong>\u8fd9\u4e2acallback\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\/** Initialize the encoder instance to encode native FLAC streams.\r\n *\r\n *  This flavor of initialization sets up the encoder to encode to a\r\n *  native FLAC stream. I\/O is performed via callbacks to the client.\r\n *  For encoding to a plain file via filename or open \\c FILE*,\r\n *  FLAC__stream_encoder_init_file() and FLAC__stream_encoder_init_FILE()\r\n *  provide a simpler interface.\r\n *\r\n *  This function should be called after FLAC__stream_encoder_new() and\r\n *  FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process()\r\n *  or FLAC__stream_encoder_process_interleaved().\r\n *  initialization succeeded.\r\n *\r\n *  The call to FLAC__stream_encoder_init_stream() currently will also\r\n *  immediately call the write callback several times, once with the \\c fLaC\r\n *  signature, and once for each encoded metadata block.\r\n *\r\n * \\param  encoder            An uninitialized encoder instance.\r\n * \\param  write_callback     See FLAC__StreamEncoderWriteCallback.  This\r\n *                            pointer must not be \\c NULL.\r\n * \\param  seek_callback      See FLAC__StreamEncoderSeekCallback.  This\r\n *                            pointer may be \\c NULL if seeking is not\r\n *                            supported.  The encoder uses seeking to go back\r\n *                            and write some some stream statistics to the\r\n *                            STREAMINFO block; this is recommended but not\r\n *                            necessary to create a valid FLAC stream.  If\r\n *                            \\a seek_callback is not \\c NULL then a\r\n *                            \\a tell_callback must also be supplied.\r\n *                            Alternatively, a dummy seek callback that just\r\n *                            returns \\c FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED\r\n *                            may also be supplied, all though this is slightly\r\n *                            less efficient for the encoder.\r\n * \\param  tell_callback      See FLAC__StreamEncoderTellCallback.  This\r\n *                            pointer may be \\c NULL if seeking is not\r\n *                            supported.  If \\a seek_callback is \\c NULL then\r\n *                            this argument will be ignored.  If\r\n *                            \\a seek_callback is not \\c NULL then a\r\n *                            \\a tell_callback must also be supplied.\r\n *                            Alternatively, a dummy tell callback that just\r\n *                            returns \\c FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED\r\n *                            may also be supplied, all though this is slightly\r\n *                            less efficient for the encoder.\r\n * \\param  metadata_callback  See FLAC__StreamEncoderMetadataCallback.  This\r\n *                            pointer may be \\c NULL if the callback is not\r\n *                            desired.  If the client provides a seek callback,\r\n *                            this function is not necessary as the encoder\r\n *                            will automatically seek back and update the\r\n *                            STREAMINFO block.  It may also be \\c NULL if the\r\n *                            client does not support seeking, since it will\r\n *                            have no way of going back to update the\r\n *                            STREAMINFO.  However the client can still supply\r\n *                            a callback if it would like to know the details\r\n *                            from the STREAMINFO.\r\n * \\param  client_data        This value will be supplied to callbacks in their\r\n *                            \\a client_data argument.\r\n * \\assert\r\n *    \\code encoder != NULL \\endcode\r\n * \\retval FLAC__StreamEncoderInitStatus\r\n *    \\c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful;\r\n *    see FLAC__StreamEncoderInitStatus for the meanings of other return values.\r\n *\/\r\nFLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data);\r\n<\/pre>\n<p>\u7b80\u5355\u4ecb\u7ecd\u4e0b\u8fd9\u4ee3\u7801\u7684\u610f\u601d<br \/>\ninitPorts()\u521d\u59cb\u5316\u4e24\u4e2aports\uff0c0\u4e3a\u8f93\u5165\uff0c1\u4e3a\u8f93\u51fa\uff0c\u8bb0\u4f4f\u4e00\u70b9\u5f88\u91cd\u8981\uff0c\u8c01\u7533\u8bf7\u7684\u5185\u5b58\uff0c\u8c01\u91ca\u653e\uff0c\u8fd9\u70b9\u5bf9\u4e8e\u7406\u89e3\u5c42\u6b21\u590d\u6742\u7684\u8fd9\u5957\u7cfb\u7edf\u6765\u8bf4\u5f88\u91cd\u8981\u3002<\/p>\n<p>\u6700\u91cd\u8981\u7684\u662f\u4e0b\u9762\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u4e0a\u5c42\u628abuffer\u6ee1\u4e4b\u540e\u4f1acall\u5230\u8fd9\u91cc\uff0c\u4e0a\u6b21\u9700\u8981\u83b7\u53d6\u5904\u7406\u597d\u7684buffer\u4e5f\u4f1acall\u5230\u8fd9\u91cc<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">void SoftFlacEncoder::onQueueFilled(OMX_U32 portIndex) {\r\n\r\n    ALOGV(&quot;SoftFlacEncoder::onQueueFilled(portIndex=%ld)&quot;, portIndex);\r\n\r\n    if (mSignalledError) { \/\/ \u51fa\u9519\u4e86\r\n        return;\r\n    }\r\n\r\n    List&lt;BufferInfo *&gt; &amp;inQueue = getPortQueue(0); \/\/ \u8f93\u5165\u53e3\uff0c\u597d\u5947\u8fd9\u91cc\u4e00\u6b21\u4f1a\u4e0d\u4f1a\u6709\u591a\u4e2abuffer\uff0c\u7406\u8bba\u4e0a\u6765\u540c\u4e00\u6b21\u5c31\u4e00\u4e2a\r\n    List&lt;BufferInfo *&gt; &amp;outQueue = getPortQueue(1); \/\/ \u8f93\u51fa\u7684\r\n\r\n    while (!inQueue.empty() &amp;&amp; !outQueue.empty()) {\r\n        BufferInfo *inInfo = *inQueue.begin();\r\n        OMX_BUFFERHEADERTYPE *inHeader = inInfo-&gt;mHeader;\r\n\r\n        BufferInfo *outInfo = *outQueue.begin();\r\n        OMX_BUFFERHEADERTYPE *outHeader = outInfo-&gt;mHeader;\r\n\r\n        if (inHeader-&gt;nFlags &amp; OMX_BUFFERFLAG_EOS) { \/\/ \u7f16\u89e3\u7801\u7ed3\u675f\u7684\u65f6\u5019\uff0c\u90fd\u6e05\u7a7a\u6807\u8bb0\uff0cnotifyEmptyBufferDone\u548cnotifyFillBufferDone\u90fd\u8fd4\u56de\r\n            inQueue.erase(inQueue.begin()); \/\/ \u8fd9\u662f\u6211\u4eec\u81ea\u5df1\u7533\u8bf7\u7684\u5185\u5b58\uff0c\u81ea\u5df1\u91ca\u653e\r\n            inInfo-&gt;mOwnedByUs = false;\r\n            notifyEmptyBufferDone(inHeader);\r\n\r\n            outHeader-&gt;nFilledLen = 0;\r\n            outHeader-&gt;nFlags = OMX_BUFFERFLAG_EOS;\r\n\r\n            outQueue.erase(outQueue.begin()); \/\/ \u8fd9\u662f\u6211\u4eec\u81ea\u5df1\u7533\u8bf7\u7684\u5185\u5b58\uff0c\u81ea\u5df1\u91ca\u653e\r\n            outInfo-&gt;mOwnedByUs = false;\r\n            notifyFillBufferDone(outHeader);\r\n\r\n            return;\r\n        }\r\n\r\n        if (inHeader-&gt;nFilledLen &gt; kMaxNumSamplesPerFrame * sizeof(FLAC__int32) * 2) {\r\n            ALOGE(&quot;input buffer too large (%ld).&quot;, inHeader-&gt;nFilledLen);\r\n            mSignalledError = true;\r\n            notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);\r\n            return;\r\n        }\r\n\r\n        assert(mNumChannels != 0);\r\n        mEncoderWriteData = true;\r\n        mEncoderReturnedEncodedData = false;\r\n        mEncoderReturnedNbBytes = 0;\r\n        mCurrentInputTimeStamp = inHeader-&gt;nTimeStamp;\r\n\r\n        const unsigned nbInputFrames = inHeader-&gt;nFilledLen \/ (2 * mNumChannels);\r\n        const unsigned nbInputSamples = inHeader-&gt;nFilledLen \/ 2;\r\n        const OMX_S16 * const pcm16 = reinterpret_cast&lt;OMX_S16 *&gt;(inHeader-&gt;pBuffer); \/\/ OMX_BUFFERHEADERTYPE\u5f53\u4e2dpBuffer\u9ed8\u8ba4\u4e3a8\u4f4d\uff0c\u8fd9\u91cc\u5f3a\u5236\u630916\u4e3a\u91cd\u65b0\u5206\u7ec4\uff0c\u6240\u4ee5\u4e00\u4e2abuffer\u5f53\u4e2dsamples\u7684\u6570\u91cf\u5c31\u662fnFilledLen\/2\r\n        \/\/ \u53ef\u662f\u4e3a\u4ec0\u4e48\u8981\u6309\u716716\u4f4d\u91cd\u65b0\u5206\u7ec4\uff1f\u56e0\u4e3a\u8bbe\u7f6e\u7ed9codec\u7684FLAC__stream_encoder_set_bits_per_sample(mFlacStreamEncoder, 16);\r\n\r\n        for (unsigned i=0 ; i &lt; nbInputSamples ; i++) {\r\n            mInputBufferPcm32&amp;#91;i&amp;#93; = (FLAC__int32) pcm16&amp;#91;i&amp;#93;; \/\/ \u630932\u4f4d\u89e3\u6790\uff0c\u56e0\u4e3aFLAC\u9700\u8981\r\n        }\r\n        ALOGV(&quot; about to encode %u samples per channel&quot;, nbInputFrames);\r\n        FLAC__bool ok = FLAC__stream_encoder_process_interleaved(\r\n                        mFlacStreamEncoder,\r\n                        mInputBufferPcm32,\r\n                        nbInputFrames \/*samples per channel*\/ ); \/\/ \u5728\u8fd9\u91cc\u7f16\u7801\uff0c\u56e0\u4e3aFLAC\u5bf9\u9f50\u7684\u5173\u7cfb\uff0c\u5bf9\u8f93\u5165\u7684buffer\u9700\u8981\u505a\u4e00\u5b9a\u7684\u8f6c\u6362\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u6807\u8bb0\u51fa\u6765\u7684\u4e24\u5904\r\n\r\n        if (ok) {\r\n            \/\/ \u4e4b\u524d\u6ce8\u518c\u7684callback\u5e94\u8be5\u662fblock\u7684\uff0c\u4e5f\u5c31\u662f\u5728FLAC__stream_encoder_process_interleaved\u8fd4\u56decallback\u56e0\u8be5\u4e5f\u8fd4\u56de\u4e86\r\n            if (mEncoderReturnedEncodedData &amp;&amp; (mEncoderReturnedNbBytes != 0)) {\r\n                ALOGV(&quot; dequeueing buffer on output port after writing data&quot;);\r\n                outInfo-&gt;mOwnedByUs = false;\r\n                outQueue.erase(outQueue.begin());\r\n                outInfo = NULL;\r\n                notifyFillBufferDone(outHeader); \/\/ \u7f16\u7801\u597d\u7684\u6570\u636e\u901a\u8fc7\u8fd9\u91cc\u5c31\u56de\u5230\u4e86\u4e0a\u5c42\r\n                outHeader = NULL;\r\n                mEncoderReturnedEncodedData = false;\r\n            } else {\r\n                ALOGV(&quot; encoder process_interleaved returned without data to write&quot;);\r\n            }\r\n        } else {\r\n            ALOGE(&quot; error encountered during encoding&quot;);\r\n            mSignalledError = true;\r\n            notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);\r\n            return;\r\n        }\r\n\r\n        inInfo-&gt;mOwnedByUs = false;\r\n        inQueue.erase(inQueue.begin());\r\n        inInfo = NULL;\r\n        notifyEmptyBufferDone(inHeader); \/\/ \u544a\u8bc9\u4e0a\u5c42\uff0cFLAC codec\u5df2\u7ecf\u53d6\u8d70\u4e86in buffer\u91cc\u9762\u7684\u6570\u636e\uff0c\u8fd9\u6837\u770b\u6765\u8fd9\u91cc\u5c31\u662f\u4e0a\u5c42\u9001\u6570\u636e\uff0ccodec\u7f16\u7801\uff0ccodec\u9001\u6570\u636e\uff0c\u7136\u540e\u5f00\u59cb\u7b2c\u4e8c\u8f6e\r\n        \/\/ \u4e0d\u77e5\u9053\u6709\u6ca1\u6709\u90a3\u79cd\u5b9e\u73b0\uff0c\u5c31\u662f\u4e0a\u5c42\u9001\u6570\u636e\uff0c\u53ea\u8981codec\u544a\u8bc9\u4e0a\u5c42\u5df2\u7ecf\u53d6\u8d70\u4e86\u6570\u636e\uff0c\u4e0a\u5c42\u4e5f\u53ef\u4ee5\u7ee7\u7eed\u9001\u6570\u636e\uff0c\u5373\u4f7fcodec\u8fd9\u4e2a\u65f6\u5019\u8fd8\u6ca1\u6709\u7f16\u7801\u5b8c\u6bd5\uff0c\u8fd8\u6ca1\u6709\u628a\u4e0a\u4e00\u6b21\u7684\u6570\u636e\u4f20\u9001\u7ed9\u4e0a\u5c42\uff0c\u4e5f\u8bb8\u8fd9\u6837\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u52a0\u5feb\u4e92\u7b49\u62f7\u8d1d\/\u51c6\u5907\u6570\u636e\u7684\u65f6\u95f4\uff0c\u4e0d\u8fc7\u8fd9\u662fsoft\u7f16\u7801\uff0c\u90fd\u4f1a\u5360\u7528CPU\u7684\u65f6\u95f4\uff0c\u6240\u4ee5\u591aCPU\u4e0a\u4e5f\u8bb8\u53ef\u4ee5\u6539\u8fdb\uff0c\u4f46\u90a3\u6837\u5e94\u8be5\u4f1a\u590d\u6742\u4e9b\r\n        inHeader = NULL;\r\n    }\r\n}\r\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u770bcallback\u91cc\u9762<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">FLAC__StreamEncoderWriteStatus SoftFlacEncoder::onEncodedFlacAvailable(\r\n            const FLAC__byte buffer&#x5B;],\r\n            size_t bytes, unsigned samples, unsigned current_frame) {\r\n    ALOGV(&quot;SoftFlacEncoder::onEncodedFlacAvailable(bytes=%d, samples=%d, curr_frame=%d)&quot;,\r\n            bytes, samples, current_frame);\r\n\r\n#ifdef WRITE_FLAC_HEADER_IN_FIRST_BUFFER\r\n    if (samples == 0) {\r\n        ALOGI(&quot; saving %d bytes of header&quot;, bytes);\r\n        memcpy(mHeader + mHeaderOffset, buffer, bytes);\r\n        mHeaderOffset += bytes;\/\/ will contain header size when finished receiving header\r\n        return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;\r\n    }\r\n\r\n#endif\r\n\r\n    if ((samples == 0) || !mEncoderWriteData) {\r\n        \/\/ called by the encoder because there&#039;s header data to save, but it&#039;s not the role\r\n        \/\/ of this component (unless WRITE_FLAC_HEADER_IN_FIRST_BUFFER is defined)\r\n        ALOGV(&quot;ignoring %d bytes of header data (samples=%d)&quot;, bytes, samples);\r\n        return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;\r\n    }\r\n\r\n    List&lt;BufferInfo *&gt; &amp;outQueue = getPortQueue(1);\r\n    CHECK(!outQueue.empty());\r\n    BufferInfo *outInfo = *outQueue.begin();\r\n    OMX_BUFFERHEADERTYPE *outHeader = outInfo-&gt;mHeader;\r\n\r\n#ifdef WRITE_FLAC_HEADER_IN_FIRST_BUFFER\r\n    if (!mWroteHeader) {\r\n        ALOGI(&quot; writing %d bytes of header on output port&quot;, mHeaderOffset);\r\n        memcpy(outHeader-&gt;pBuffer + outHeader-&gt;nOffset + outHeader-&gt;nFilledLen,\r\n                mHeader, mHeaderOffset);\r\n        outHeader-&gt;nFilledLen += mHeaderOffset;\r\n        outHeader-&gt;nOffset    += mHeaderOffset;\r\n        mWroteHeader = true;\r\n    }\r\n#endif\r\n\r\n    \/\/ write encoded data\r\n    ALOGV(&quot; writing %d bytes of encoded data on output port&quot;, bytes);\r\n    if (bytes &gt; outHeader-&gt;nAllocLen - outHeader-&gt;nOffset - outHeader-&gt;nFilledLen) {\r\n        ALOGE(&quot; not enough space left to write encoded data, dropping %u bytes&quot;, bytes);\r\n        \/\/ a fatal error would stop the encoding\r\n        return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;\r\n    }\r\n    memcpy(outHeader-&gt;pBuffer + outHeader-&gt;nOffset, buffer, bytes); \/\/ \u62f7\u8d1d\u6570\u636e\u5230OpenMAX IL\u7684buffer\u91cc\u9762\r\n\r\n    outHeader-&gt;nTimeStamp = mCurrentInputTimeStamp;\r\n    outHeader-&gt;nOffset = 0;\r\n    outHeader-&gt;nFilledLen += bytes;\r\n    outHeader-&gt;nFlags = 0;\r\n\r\n    mEncoderReturnedEncodedData = true;\r\n    mEncoderReturnedNbBytes += bytes;\r\n\r\n    return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;\r\n}<\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\/\/ FLAC takes samples aligned on 32bit boundaries, use this buffer for the conversion\r\n\/\/ before passing the input data to the encoder\r\nFLAC__int32* mInputBufferPcm32; \/\/ \u8fd9\u4e2a\u95ee\u9898\u4e0a\u9762\u5df2\u7ecf\u5206\u6790\u8fc7\u4e86<\/pre>\n<p>\u77ed\u77ed\u51e0\u767e\u884c\u4ee3\u7801\uff0c\u8fd9\u4e2a\u7b97\u7b80\u5355\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3e\u4e2a\u4f8b\u5b50\u6765\u770b\u4e0b\uff0ccodec\u662f\u5982\u4f55\u548cOpenMAX IL\u6c9f\u901a\u7684\uff0c\u8fd9\u5176\u5b9e\u5e94\u8be5\u662fStagefright\u548ccodec\u7684\u4ea4\u4e92\u8fd9\u7bc7\u535a\u5ba2\u5f53\u4e2d\u7684\uff0c\u60f3\u60f3\u6bd4\u8f83\u72ec\u7acb\uff0c\u53c8\u6bd4\u8f83\u957f\uff0c\u5e72\u8106\u5355\u72ec\u62ff\u51fa\u6765\u5199\u4e00\u4e0b\u3002 \u901a\u5e38\u6765\u8bf4\u4e00\u4e2acodec(encoder\u6216\u8005decoder)\u65e0\u8bba\u600e\u4e48\u590d\u6742\uff0c\u5b83\u603b\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u6765\u662f\u6211\u4eec\u8f93\u5165\u4e00\u6bb5\u6570\u636e\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u6211\u4eec\u6765\u83b7\u53d6\u4e00\u6bb5\u6570\u636e\uff0c\u56e0\u4e3a\u5b83\u7684\u529f\u80fd\u5c31\u662f\u8fd9\u6837\u7684\u3002 \u6240\u4ee5\u6211\u4eec\u8981\u628a\u4e00\u4e2acodec\u4f7f\u7528OpenMAX IL\u7684\u65b9\u5f0f\u96c6\u6210\u8fdb\u6765\uff0c\u5bf9\u6211\u4eec\u6765\u8bf4\u5de5\u4f5c\u91cf\u4e0d\u7b97\u5f88\u5927\u3002\u8fd9\u91cc\u4ee5FLAC encoder\u6765\u4e3e\u4e2a\u4f8b\u5b50(\u56e0\u4e3a\u5b83\u6bd4\u8f83\u7b80\u5355)\uff0cFLAC\u672c\u8eab\u8fd9\u4e2acodec\u76f8\u5173\u7684\u4fe1\u606f\u8bf7\u53c2\u89c1http:\/\/xiph.org\/flac\/\uff0c\u5728Android\u5f53\u4e2d\u6e90\u7801\u4f4d\u4e8e\/path\/to\/aosp\/external\/flac\/\uff0c\u5728Android\u5f53\u4e2dOpenMAX IL\u4e0e\u5b83\u4ea4\u4e92\u662f\u901a\u8fc7libstagefright_soft_flacenc.so\u8fd9\u4e2a\u4e1c\u897f\uff0c\u6211\u4eec\u4eca\u5929\u5c31\u91cd\u70b9\u5173\u6ce8\u8fd9\u4e2a\uff0c\u5b83\u7684\u6e90\u7801\u4f4d\u4e8e\/path\/to\/aosp\/frameworks\/av\/media\/libstagefright\/codecs\/flac\/enc\/\u3002 \u5bf9Android\u5f53\u4e2dStagefright\u548cOpenMAX IL\u4e0d\u719f\u6089\u7684\u8bdd\uff0c\u8fd8\u662f\u5efa\u8bae\u5148\u770b\u6587\u7ae0\u5f00\u59cb\u63d0\u5230\u7684\u90a3\u7bc7\u3002 \u9996\u5148SoftFlacEncoder\u7ee7\u627f\u81eaSimpleSoftOMXComponent\uff0c\u91cd\u5199\u4e864\u4e2a\u65b9\u6cd5\uff0c\u5206\u522b\u662f virtual OMX_ERRORTYPE initCheck() const; \/\/ \u5728SoftOMXComponent\u5f53\u4e2d\u662f\u7a7a\u5b9e\u73b0\uff0c\u81ea\u5df1\u7684codec\u9700\u8981\u5b9e\u73b0\u6765\u8ba9\u4ed6\u4eba\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u6765\u63a2\u6d4b\u4f60\u7684codec\u662f\u5426\u6b63\u5e38 virtual OMX_ERRORTYPE internalGetParameter( OMX_INDEXTYPE index, OMX_PTR params); \/\/ \u5c31\u662fOpenMax IL\u7ec4\u4ef6\u7684getParameter virtual OMX_ERRORTYPE internalSetParameter( OMX_INDEXTYPE index, const OMX_PTR params); \/\/ \u5c31\u662fOpenMax IL\u7ec4\u4ef6\u7684setParameter virtual void onQueueFilled(OMX_U32 portIndex); \/\/ \u5c31\u662fOpenMax IL\u7ec4\u4ef6\u7684emptyThisBuffer\u548cfillThisBuffer\uff0c\u5982\u679c\u4f60\u4e0d\u6e05\u695a\uff0c\u5e76\u4e14\u8fd8\u5728\u770b\u8fd9\u7bc7\u6587\u7ae0\u7684\u8bdd\uff0c\u90a3\u4f60\u4e00\u5b9a\u662f\u5728\u5f53\u6563\u6587\u770b ^_^ \u8fd9\u56db\u4e2a\u65b9\u6cd5\u5982\u679c\u4f60\u4e0d\u660e\u767d\uff0c\u5c31\u5148\u7ffb\u7ffbOpenMax IL\/Stagefright\u76f8\u5173\u4ee3\u7801\u6216\u8005\u89c4\u8303\u3002 \u53e6\u5916\u5b83\u8fd8\u6709\u51e0\u4e2a\u79c1\u6709\u65b9\u6cd5 void initPorts(); \/\/ OpenMAX IL\u901a\u4fe1\u9700\u8981port OMX_ERRORTYPE configureEncoder(); &hellip; <a href=\"https:\/\/guoh.ai\/lifelog\/2013\/07\/integrating-flac-encoder-into-stagefright\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">FLAC encoder\u662f\u5982\u4f55\u96c6\u6210\u5230Stagefright\u7684(OpenMAX IL)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_gfmr_meta_descriptions":[],"_gfmr_multilingual_taxonomy_terms":[],"footnotes":""},"categories":[7,28,34],"tags":[],"class_list":["post-658","post","type-post","status-publish","format-standard","hentry","category-android","category-cpp","category-multimedia"],"_links":{"self":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/comments?post=658"}],"version-history":[{"count":4,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/658\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/658\/revisions\/662"}],"wp:attachment":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/media?parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/categories?post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/tags?post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}