static void OpenVideo(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg) { int ret = 0; AVCodecContext *c = ost->st->codec; AVDictionary *opt = NULL; av_dict_copy(&opt, opt_arg, 0); // コーデックを初期化 ret = avcodec_open2(c, codec, &opt); av_dict_free(&opt); if (ret < 0) { fprintf(stderr, "Could not open video codec: %s\n", MakeErrorString(ret)); return; } // フレームを初期化 ost->frame = AllocPicture(c->pix_fmt, c->width, c->height); if (!ost->frame) { fprintf(stderr, "Could not allocate video frame\n"); return; } // 画像フォーマットの変換元(OSD_GetWindowImage)のフォーマットがBGR0(0は未使用)であることを想定 ost->tmp_frame = NULL; ost->tmp_frame = AllocPicture(AV_PIX_FMT_BGR0, c->width, c->height); if (!ost->tmp_frame) { fprintf(stderr, "Could not allocate temporary picture\n"); return; } }
bool AVFormatWriter::OpenVideo(void) { AVCodec *codec; AVCodecContext *c; c = m_videoStream->codec; codec = avcodec_find_encoder(c->codec_id); if (!codec) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): avcodec_find_encoder() failed"); return false; } if (avcodec_open(c, codec) < 0) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): avcodec_open() failed"); return false; } m_videoOutBuf = NULL; if (!(m_ctx->oformat->flags & AVFMT_RAWPICTURE)) { m_videoOutBufSize = 200000; m_videoOutBuf = (unsigned char *)av_malloc(m_videoOutBufSize); } m_picture = AllocPicture(c->pix_fmt); if (!m_picture) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): AllocPicture() failed"); return false; } m_tmpPicture = NULL; if (c->pix_fmt != PIX_FMT_YUV420P) { m_tmpPicture = AllocPicture(PIX_FMT_YUV420P); if (!m_tmpPicture) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): m_tmpPicture AllocPicture() failed"); return false; } } return true; }
bool AVFormatWriter::OpenVideo(void) { AVCodecContext *c; c = m_videoStream->codec; if (!m_width || !m_height) return false; if (avcodec_open2(c, NULL, NULL) < 0) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): avcodec_open() failed"); return false; } if (!m_picture) { m_picture = AllocPicture(c->pix_fmt); if (!m_picture) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): AllocPicture() failed"); return false; } } else { av_frame_unref(m_picture); } return true; }
int32_t CWelsPreProcess::AllocSpatialPictures (sWelsEncCtx* pCtx, SWelsSvcCodingParam* pParam) { CMemoryAlign* pMa = pCtx->pMemAlign; const int32_t kiDlayerCount = pParam->iSpatialLayerNum; int32_t iDlayerIndex = 0; // spatial pictures iDlayerIndex = 0; do { const int32_t kiPicWidth = pParam->sDependencyLayers[iDlayerIndex].iFrameWidth; const int32_t kiPicHeight = pParam->sDependencyLayers[iDlayerIndex].iFrameHeight; const uint8_t kuiLayerInTemporal = 2 + WELS_MAX (pParam->sDependencyLayers[iDlayerIndex].iHighestTemporalId, 1); const uint8_t kuiRefNumInTemporal = kuiLayerInTemporal + pParam->iLTRRefNum; uint8_t i = 0; do { SPicture* pPic = AllocPicture (pMa, kiPicWidth, kiPicHeight, false); WELS_VERIFY_RETURN_IF(1, (NULL == pPic)) m_pSpatialPic[iDlayerIndex][i] = pPic; ++ i; } while (i < kuiRefNumInTemporal); m_uiSpatialLayersInTemporal[iDlayerIndex] = kuiLayerInTemporal; m_uiSpatialPicNum[iDlayerIndex] = kuiRefNumInTemporal; ++ iDlayerIndex; } while (iDlayerIndex < kiDlayerCount); return 0; }
static int32_t CreatePicBuff (PWelsDecoderContext pCtx, PPicBuff* ppPicBuf, const int32_t kiSize, const int32_t kiPicWidth, const int32_t kiPicHeight) { PPicBuff pPicBuf = NULL; int32_t iPicIdx = 0; if (kiSize <= 0 || kiPicWidth <= 0 || kiPicHeight <= 0) { return 1; } pPicBuf = (PPicBuff)WelsMalloc (sizeof (SPicBuff), "PPicBuff"); if (NULL == pPicBuf) { return 1; } pPicBuf->ppPic = (PPicture*)WelsMalloc (kiSize * sizeof (PPicture), "PPicture*"); if (NULL == pPicBuf->ppPic) { return 1; } for (iPicIdx = 0; iPicIdx < kiSize; ++ iPicIdx) { PPicture pPic = AllocPicture (pCtx, kiPicWidth, kiPicHeight); if (NULL == pPic) { return 1; } pPicBuf->ppPic[iPicIdx] = pPic; } // initialize context in queue pPicBuf->iCapacity = kiSize; pPicBuf->iCurrentIdx = 0; *ppPicBuf = pPicBuf; return 0; }
int32_t WelsInitScaledPic (SWelsSvcCodingParam* pParam, Scaled_Picture* pScaledPicture, CMemoryAlign* pMemoryAlign) { bool_t bInputPicNeedScaling = JudgeNeedOfScaling (pParam, pScaledPicture); if (bInputPicNeedScaling) { pScaledPicture->pScaledInputPicture = AllocPicture (pMemoryAlign, pParam->SUsedPicRect.iWidth, pParam->SUsedPicRect.iHeight, false); if (pScaledPicture->pScaledInputPicture == NULL) return -1; } return 0; }
bool AVFormatWriter::OpenVideo(void) { AVCodecContext *c; c = m_videoStream->codec; if (m_width && m_height) m_videoOutBuf = (unsigned char *)av_malloc(m_width * m_height * 2 + 10); else return false; if (avcodec_open2(c, NULL, NULL) < 0) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): avcodec_open() failed"); return false; } m_picture = AllocPicture(c->pix_fmt); if (!m_picture) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): AllocPicture() failed"); return false; } m_tmpPicture = NULL; if (c->pix_fmt != PIX_FMT_YUV420P) { m_tmpPicture = AllocPicture(PIX_FMT_YUV420P); if (!m_tmpPicture) { LOG(VB_RECORD, LOG_ERR, LOC + "OpenVideo(): m_tmpPicture AllocPicture() failed"); return false; } } return true; }
static int32_t CreatePicBuff (PWelsDecoderContext pCtx, PPicBuff* ppPicBuf, const int32_t kiSize, const int32_t kiPicWidth, const int32_t kiPicHeight) { PPicBuff pPicBuf = NULL; int32_t iPicIdx = 0; if (kiSize <= 0 || kiPicWidth <= 0 || kiPicHeight <= 0) { return 1; } CMemoryAlign* pMa = pCtx->pMemAlign; pPicBuf = (PPicBuff)pMa->WelsMallocz (sizeof (SPicBuff), "PPicBuff"); if (NULL == pPicBuf) { return 1; } pPicBuf->ppPic = (PPicture*)pMa->WelsMallocz (kiSize * sizeof (PPicture), "PPicture*"); if (NULL == pPicBuf->ppPic) { pPicBuf->iCapacity = 0; DestroyPicBuff (&pPicBuf, pMa); return 1; } for (iPicIdx = 0; iPicIdx < kiSize; ++ iPicIdx) { PPicture pPic = AllocPicture (pCtx, kiPicWidth, kiPicHeight); if (NULL == pPic) { // init capacity first for free memory pPicBuf->iCapacity = iPicIdx; DestroyPicBuff (&pPicBuf, pMa); return 1; } pPicBuf->ppPic[iPicIdx] = pPic; } // initialize context in queue pPicBuf->iCapacity = kiSize; pPicBuf->iCurrentIdx = 0; * ppPicBuf = pPicBuf; return 0; }
static int32_t IncreasePicBuff (PWelsDecoderContext pCtx, PPicBuff* ppPicBuf, const int32_t kiOldSize, const int32_t kiPicWidth, const int32_t kiPicHeight, const int32_t kiNewSize) { PPicBuff pPicOldBuf = *ppPicBuf; PPicBuff pPicNewBuf = NULL; int32_t iPicIdx = 0; if (kiOldSize <= 0 || kiNewSize <= 0 || kiPicWidth <= 0 || kiPicHeight <= 0) { return 1; } pPicNewBuf = (PPicBuff)WelsMallocz (sizeof (SPicBuff), "PPicBuff"); if (NULL == pPicNewBuf) { return 1; } pPicNewBuf->ppPic = (PPicture*)WelsMallocz (kiNewSize * sizeof (PPicture), "PPicture*"); if (NULL == pPicNewBuf->ppPic) { pPicNewBuf->iCapacity = 0; DestroyPicBuff (&pPicNewBuf); return 1; } // increase new PicBuf for (iPicIdx = kiOldSize; iPicIdx < kiNewSize; ++ iPicIdx) { PPicture pPic = AllocPicture (pCtx, kiPicWidth, kiPicHeight); if (NULL == pPic) { // Set maximum capacity as the new malloc memory at the tail pPicNewBuf->iCapacity = iPicIdx; DestroyPicBuff (&pPicNewBuf); return 1; } pPicNewBuf->ppPic[iPicIdx] = pPic; } // copy old PicBuf to new PicBuf memcpy (pPicNewBuf->ppPic, pPicOldBuf->ppPic, kiOldSize * sizeof (PPicture)); // initialize context in queue pPicNewBuf->iCapacity = kiNewSize; pPicNewBuf->iCurrentIdx = pPicOldBuf->iCurrentIdx; * ppPicBuf = pPicNewBuf; for (int32_t i = 0; i < pPicNewBuf->iCapacity; i++) { pPicNewBuf->ppPic[i]->bUsedAsRef = false; pPicNewBuf->ppPic[i]->bIsLongRef = false; pPicNewBuf->ppPic[i]->uiRefCount = 0; pPicNewBuf->ppPic[i]->bAvailableFlag = true; pPicNewBuf->ppPic[i]->bIsComplete = false; } // remove old PicBuf if (pPicOldBuf->ppPic != NULL) { WelsFree (pPicOldBuf->ppPic, "pPicOldBuf->queue"); pPicOldBuf->ppPic = NULL; } pPicOldBuf->iCapacity = 0; pPicOldBuf->iCurrentIdx = 0; WelsFree (pPicOldBuf, "pPicOldBuf"); pPicOldBuf = NULL; return 0; }
UINT CFlvUtils::OpenVideoStream() { HRESULT hr = S_OK; AVCodec *avCodec; AVCodecContext *avCodecContext; avCodecContext = &m_pAVStreamVideo->codec; if (SUCCEEDED(hr)) { // Find the audio encoder avCodec = avcodec_find_encoder(avCodecContext->codec_id); if (!avCodec) { _ftprintf(stderr, _T("Error in CFlvUtils::OpenVideoStream():\n Codec not found!\n")); hr = E_FAIL; // TODO: error handling? } } if (SUCCEEDED(hr)) { // Open it if (avcodec_open(avCodecContext, avCodec) < 0) { _ftprintf(stderr, _T("Error in CFlvUtils::OpenVideoStream():\n Could not open codec!\n")); hr = E_FAIL; // TODO: error handling? } } if (SUCCEEDED(hr)) { m_pVideoOutbuf = NULL; if (!(m_pAVFormatContext->oformat->flags & AVFMT_RAWPICTURE)) { // Allocate output buffer // XXX: API change will be done m_nVideoOutbufSize = 200000; m_pVideoOutbuf = (uint8_t*)malloc(m_nVideoOutbufSize * sizeof(uint8_t)); } // Allocate the encoded raw picture // m_pAVFramePicture = AllocPicture(avCodecContext->pix_fmt, avCodecContext->width, avCodecContext->height); m_pAVFramePicture = AllocPicture(avCodecContext->pix_fmt, m_nDestVideoWidth, m_nDestVideoHeight); if (!m_pAVFramePicture) { _ftprintf(stderr, _T("Error in CFlvUtils::OpenVideoStream():\n Could not allocate picture!\n")); hr = E_FAIL; // TODO: error handling? } } if (SUCCEEDED(hr)) { // A temporary RGB24 picture is needed too. // It is then converted to the required output format m_pAVFrameTmpPicture = NULL; // m_pAVFrameTmpPicture = AllocPicture(PIX_FMT_RGB24, avCodecContext->width, avCodecContext->height); m_pAVFrameTmpPicture = AllocPicture(PIX_FMT_RGB24, m_nSrcVideoWidth, m_nSrcVideoHeight); if (!m_pAVFrameTmpPicture) { _ftprintf(stderr, _T("Error in CFlvUtils::OpenVideoStream():\n Could not allocate temporary picture!\n")); hr = E_FAIL; // TODO: error handling? } } if (SUCCEEDED(hr)) { // Is resampling necessary? if ((m_nDestVideoWidth != m_nSrcVideoWidth) || (m_nDestVideoHeight != m_nSrcVideoHeight)) { AVCodecContext *avCodecContext = &m_pAVStreamVideo->codec; m_pAVFrameTmpPicture2 = NULL; // m_pAVFrameTmpPicture2 = AllocPicture(PIX_FMT_RGB24, m_nDestVideoWidth, m_nDestVideoHeight); m_pAVFrameTmpPicture2 = AllocPicture(avCodecContext->pix_fmt, m_nSrcVideoWidth, m_nSrcVideoHeight); if (!m_pAVFrameTmpPicture2) { _ftprintf(stderr, _T("Error in CFlvUtils::OpenVideoStream():\n Could not allocate temporary picture 2!\n")); hr = E_FAIL; // TODO: error handling? } } } return hr; }