bool Init(VirtualSink* sink, AVCodec* avCodec, const char* sprops){ //if it has been initialized before, we should do cleanup first Cleanup(); avCodecContext = avcodec_alloc_context(); if (!avCodecContext) { //failed to allocate codec context Cleanup(); return false; } uint8_t startCode[] = {0x00, 0x00, 0x01}; if(sprops != NULL){ unsigned spropCount; SPropRecord* spropRecords = parseSPropParameterSets(sprops, spropCount); try{ for (unsigned i = 0; i < spropCount; ++i) { AddExtraData(startCode, sizeof(startCode)); AddExtraData(spropRecords[i].sPropBytes, spropRecords[i].sPropLength); } }catch(void*){ //extradata exceeds size limit delete[] spropRecords; Cleanup(); return false; } delete[] spropRecords; avCodecContext->extradata = extraDataBuffer; avCodecContext->extradata_size = extraDataSize; } AddExtraData(startCode, sizeof(startCode)); avCodecContext->flags = 0; if (avcodec_open(avCodecContext, avCodec) < 0) { //failed to open codec Cleanup(); return false; } if (avCodecContext->codec_id == CODEC_ID_H264){ avCodecContext->flags2 |= CODEC_FLAG2_CHUNKS; //avCodecContext->flags2 |= CODEC_FLAG2_SHOW_ALL; } avFrame = avcodec_alloc_frame(); if (!avFrame){ //failed to allocate frame Cleanup(); return false; } return true; }
inline int CBE_EnemyShip::GetNewExtraDataIndex() { size_t i, num_extra_data = m_vecExtraData.size(); for( i=0; i<num_extra_data; i++ ) { if( !m_vecExtraData[i].m_bInUse ) return (int)i; } // add new extra data AddExtraData(); return (int)num_extra_data; }