GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — OpenMaxIL-cpp/src/VideoDecode.cpp at master · dridri/OpenMaxIL-cpp Expand file tree/ Copy pathVideoDecode.cppMore file actions 325 lines (268 loc) · 8.99 KB / Copy pathVideoDecode.cppFile metadata and controls325 lines (268 loc) · 8.99 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 #include "VideoDecode.h" #include <unistd.h> #include <IL/OMX_Types.h> #include <IL/OMX_Component.h> #include <IL/OMX_Broadcom.h> #include <iostream> using namespace IL; static void PrintBuffer( void* _buf, int size ); VideoDecode::VideoDecode( uint32_t fps, const CodingType& coding_type, bool verbose ) : Component( "OMX.broadcom.video_decode", { PortInit( 130, Video ) }, { PortInit( 131, Video ) }, verbose ) , mCodingType( coding_type ) , mBuffer( nullptr ) , mOutputBuffer( nullptr ) , mFirstData( true ) , mDecoderValid( false ) , mVideoRunning( false ) , mNeedData( false ) { OMX_VIDEO_PARAM_PORTFORMATTYPE pfmt; OMX_INIT_STRUCTURE( pfmt ); pfmt.nPortIndex = 130; // pfmt.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar; pfmt.eCompressionFormat = (OMX_VIDEO_CODINGTYPE)coding_type; pfmt.xFramerate = fps << 16; SetParameter( OMX_IndexParamVideoPortFormat, &pfmt ); OMX_CONFIG_BOOLEANTYPE interpolate_timestamp; OMX_INIT_STRUCTURE( interpolate_timestamp ); interpolate_timestamp.bEnabled = OMX_FALSE; SetParameter( OMX_IndexParamBrcmInterpolateMissingTimestamps, &interpolate_timestamp ); if ( coding_type == CodingAVC ) { OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE errconceal; OMX_INIT_STRUCTURE( errconceal ); errconceal.bStartWithValidFrame = OMX_FALSE; SetParameter( OMX_IndexParamBrcmVideoDecodeErrorConcealment, &errconceal ); OMX_PARAM_DATAUNITTYPE unit; OMX_INIT_STRUCTURE( unit ); unit.nPortIndex = 130; unit.eUnitType = OMX_DataUnitCodedPicture; unit.eEncapsulationType = OMX_DataEncapsulationElementaryStream; SetParameter( OMX_IndexParamBrcmDataUnit, &unit ); } SendCommand( OMX_CommandStateSet, OMX_StateIdle, nullptr ); } VideoDecode::VideoDecode( uint32_t width, uint32_t height, uint32_t fps, const CodingType& coding_type, bool verbose ) : VideoDecode( fps, coding_type, verbose ) { OMX_PARAM_PORTDEFINITIONTYPE def; OMX_INIT_STRUCTURE( def ); def.nPortIndex = 130; GetParameter( OMX_IndexParamPortDefinition, &def ); def.format.video.nFrameWidth = width; def.format.video.nFrameHeight = height; def.format.video.nStride = width; def.format.video.nSliceHeight = height; def.format.video.xFramerate = fps << 16; def.format.video.eCompressionFormat = (OMX_VIDEO_CODINGTYPE)coding_type; def.format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar; SetParameter( OMX_IndexParamPortDefinition, &def ); } VideoDecode::~VideoDecode() { } void VideoDecode::setRGB565Mode( bool en ) { OMX_PARAM_PORTDEFINITIONTYPE def; OMX_INIT_STRUCTURE( def ); def.nPortIndex = 131; GetParameter( OMX_IndexParamPortDefinition, &def ); def.format.video.eColorFormat = en ? OMX_COLOR_Format16bitRGB565 : OMX_COLOR_FormatYUV420PackedPlanar; SetParameter( OMX_IndexParamPortDefinition, &def ); } OMX_ERRORTYPE VideoDecode::SetupTunnel( Component* next, uint8_t port_input ) { return Component::SetupTunnel( 131, next, port_input ); } OMX_ERRORTYPE VideoDecode::SetState( const Component::State& st ) { if ( mOutputPorts[130].bTunneled == false and mBuffer == nullptr ) { AllocateBuffers( &mBuffer, 130, true ); mBufferPtr = mBuffer->pBuffer; mOutputPorts[130].bEnabled = true; memcpy( &mBufferCopy, mBuffer, sizeof(mBufferCopy) ); } if ( mBuffer ) { mNeedData = true; } OMX_ERRORTYPE ret = IL::Component::SetState(st); return ret; } OMX_ERRORTYPE VideoDecode::EventHandler( OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2, OMX_PTR eventdata ) { if ( event == OMX_EventPortSettingsChanged and not mDecoderValid ) { mFirstData = true; mDecoderValid = true; } return IL::Component::EventHandler( event, data1, data2, eventdata ); } OMX_ERRORTYPE VideoDecode::EmptyBufferDone( OMX_BUFFERHEADERTYPE* buf ) { // std::cout << "EmptyBufferDone()\n"; mNeedData = true; return OMX_ErrorNone; } const bool VideoDecode::needData() const { return mNeedData; } const bool VideoDecode::valid() { return width() > 0 and height() > 0; } const uint32_t VideoDecode::width() { if ( mDecoderValid ) { OMX_PARAM_PORTDEFINITIONTYPE def; OMX_INIT_STRUCTURE( def ); def.nPortIndex = 131; GetParameter( OMX_IndexParamPortDefinition, &def ); return def.format.video.nFrameWidth; } return 0; } const uint32_t VideoDecode::height() { if ( mDecoderValid ) { OMX_PARAM_PORTDEFINITIONTYPE def; OMX_INIT_STRUCTURE( def ); def.nPortIndex = 131; GetParameter( OMX_IndexParamPortDefinition, &def ); return def.format.video.nFrameHeight; } return 0; } void VideoDecode::fillInput( uint8_t* pBuf, uint32_t len, bool corrupted ) { if ( not mVideoRunning and mDecoderValid ) { mVideoRunning = true; if ( mOutputPorts[131].bTunneled and mOutputPorts[131].pTunnel ) { mOutputPorts[131].pTunnel->SetState( StateIdle ); mOutputPorts[131].pTunnel->SendCommand( OMX_CommandPortDisable, mOutputPorts[131].nTunnelPort, nullptr ); SendCommand( OMX_CommandPortDisable, 131, nullptr ); OMX_PARAM_PORTDEFINITIONTYPE def; OMX_INIT_STRUCTURE( def ); def.nPortIndex = 131; GetParameter( OMX_IndexParamPortDefinition, &def ); def.eDomain = OMX_PortDomainVideo; def.nPortIndex = mOutputPorts[131].nTunnelPort; mOutputPorts[131].pTunnel->SetParameter( OMX_IndexParamPortDefinition, &def ); Component::SetupTunnel( 131, mOutputPorts[131].pTunnel, mOutputPorts[131].nTunnelPort ); mOutputPorts[131].pTunnel->SendCommand( OMX_CommandPortEnable, mOutputPorts[131].nTunnelPort, nullptr ); mOutputPorts[131].pTunnel->SetState( StateExecuting ); SendCommand( OMX_CommandPortEnable, 131, nullptr ); } } if ( mBuffer ) { /* uint64_t wait_start = ticks64(); bool broken = false; uint64_t broken_time = 0; while ( mFirstData == false and mNeedData == false ) { if ( ticks64() - wait_start >= 50 * 1000 ) { // EmptyBufferDone() has not been called in the last 50ms, decoder has probably stalled broken_time = ticks64() - wait_start; broken = true; break; } usleep( 20 ); } if ( broken ) { // If the decoder stalled, flush both input and output ports printf( "Decoder broken (took %llu ms), flushing..\n", broken_time / 1000 ); SendCommand( OMX_CommandFlush, 130, nullptr ); SendCommand( OMX_CommandFlush, 131, nullptr ); mFirstData = true; } mNeedData = false; */ // Ensure that everything is ok if ( len > mBuffer->nAllocLen ) { mDebugCallback( 0, "LEAK : %d > %d\n", len, mBuffer->nAllocLen ); len = mBuffer->nAllocLen; PrintBuffer( pBuf, len ); fflush( stdout ); } if ( mBuffer->pBuffer != mBufferPtr ) { mDebugCallback( 0, "LEAK : %p != %p\n", mBuffer->pBuffer, mBufferPtr ); // mBuffer->pBuffer = mBufferPtr; memcpy( mBuffer, &mBufferCopy, sizeof(mBufferCopy) ); PrintBuffer( pBuf, len ); fflush( stdout ); } // Manually copy buffer, since libcofi_rpi's memcpy causes random segfault (missalign?) uint32_t* start = (uint32_t*)mBufferPtr; uint32_t* end = start + ( len >> 2 ) + 1; uint32_t* copy = (uint32_t*)pBuf; while ( start < end ) { *(start++) = *(copy++); } // Send buffer to GPU mBuffer->nTimeStamp = { 0, 0 }; mBuffer->nFilledLen = len; mBuffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME | ( corrupted ? OMX_BUFFERFLAG_DATACORRUPT : 0 ) | ( mFirstData ? OMX_BUFFERFLAG_STARTTIME : OMX_BUFFERFLAG_TIME_UNKNOWN ); // printf( "Filling buffer\n" ); OMX_ERRORTYPE err = ((OMX_COMPONENTTYPE*)mHandle)->EmptyThisBuffer( mHandle, mBuffer ); static int err_cnt = 0; if ( err != OMX_ErrorNone ) { mDebugCallback( 0, "EmptyThisBuffer error (#%d) : 0x%08X (bufSize : %d ; corrupted : %d)\n", err_cnt, (uint32_t)err, len, corrupted ); PrintBuffer( pBuf, len ); fflush( stdout ); err_cnt++; } else { // printf( "Filling buffer OK\n" ); } mFirstData = false; } } OMX_ERRORTYPE VideoDecode::FillBufferDone( OMX_BUFFERHEADERTYPE* buf ) { std::unique_lock<std::mutex> locker( mDataAvailableMutex ); mDataAvailable = true; mDataAvailableCond.notify_all(); return OMX_ErrorNone; } const bool VideoDecode::dataAvailable() const { return mDataAvailable; } int32_t VideoDecode::getOutputData( uint8_t* pBuf, bool wait ) { if ( mOutputPorts[131].bTunneled == false and mOutputBuffer == nullptr ) { AllocateBuffers( &mOutputBuffer, 131, true ); mOutputPorts[131].bEnabled = true; } uint32_t datalen = 0; std::unique_lock<std::mutex> locker( mDataAvailableMutex ); if ( mDataAvailable ) { memcpy( pBuf, mOutputBuffer->pBuffer, mOutputBuffer->nFilledLen ); datalen = mOutputBuffer->nFilledLen; mDataAvailable = false; } else if ( wait ) { mDataAvailableCond.wait( locker ); memcpy( pBuf, mOutputBuffer->pBuffer, mOutputBuffer->nFilledLen ); datalen = mOutputBuffer->nFilledLen; mDataAvailable = false; } else { return OMX_ErrorOverflow; } locker.unlock(); if ( mOutputBuffer ) { OMX_ERRORTYPE err = ((OMX_COMPONENTTYPE*)mHandle)->FillThisBuffer( mHandle, mOutputBuffer ); if ( err != OMX_ErrorNone ) { return err; } } return datalen; } static void PrintBuffer( void* _buf, int size ) { unsigned char* buf = (unsigned char*)_buf; int b; for(b=0; b<size; b++){ printf( "%02X ", buf[b]); if(b > 0 && (b+1) % 16 == 0){ printf( "\n"); } } printf( "\n"); } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |