FFmpeg過時方法的替換

尋找視頻流的第一幀

int videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
    if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO
       && videoStream < 0) {
        videoStream = i;
    }
}

======》

int videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
    if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && videoStream < 0){
        videoStream = i;
    }
}

獲取codec上下文指針

AVCodecContext  * pCodecCtx = pFormatCtx->streams[videoStream]->codec;

======》

AVCodecContext *pCodecCtx = avcodec_alloc_context3(NULL);
if (pCodecCtx == NULL)
{
    printf("Could not allocate AVCodecContext\n");
    return -1;
}
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar);

對該幀進(jìn)行解碼

while(av_read_frame(pFormatCtx, &packet)>=0) {
    //判斷是否為視頻流
    if(packet.stream_index==videoStream) {
        //對該幀進(jìn)行解碼,該方法已經(jīng)過時棄用
        avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
        if (frameFinished) {
            // lock native window
            ANativeWindow_lock(nativeWindow, &windowBuffer, 0);
            // 格式轉(zhuǎn)換
            sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
                      pFrame->linesize, 0, pCodecCtx->height,
                      pFrameRGBA->data, pFrameRGBA->linesize);
            // 獲取stride
            uint8_t * dst = windowBuffer.bits;
            int dstStride = windowBuffer.stride * 4;
            uint8_t * src = pFrameRGBA->data[0];
            int srcStride = pFrameRGBA->linesize[0];
            // 由于window的stride和幀的stride不同,因此需要逐行復(fù)制
            int h;
            for (h = 0; h < videoHeight; h++) {
                memcpy(dst + h * dstStride, src + h * srcStride, (size_t) srcStride);
            }
            ANativeWindow_unlockAndPost(nativeWindow);
        }
        //延遲等待
        usleep((unsigned long) (1000 * 40 * play_rate));
    }
    av_packet_unref(&packet);
}

======》

while (av_read_frame(pFormatCtx,&packet) >= 0){
    //判斷是否為視頻流
    if (packet.stream_index == videoStream) {
        //解碼該幀
        if (avcodec_send_packet(pCodecCtx,&packet) == 0) {
            // 一個avPacket可能包含多幀數(shù)據(jù),所以需要使用while循環(huán)一直讀取
            while (avcodec_receive_frame(pCodecCtx,pFrame) == 0){
                // 1.lock window
                // 設(shè)置緩沖區(qū)的屬性:寬高、像素格式(需要與Java層的格式一致)
                ANativeWindow_setBuffersGeometry(nativeWindow,videoWidth,videoHeight,WINDOW_FORMAT_RGBA_8888);
                ANativeWindow_lock(nativeWindow,&windowBuffer,NULL);
                // 格式轉(zhuǎn)換
                sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
                          pFrame->linesize, 0, pCodecCtx->height,
                          pFrameRGBA->data, pFrameRGBA->linesize);

                // 獲取stride
                uint8_t *dst =(uint8_t *) windowBuffer.bits;
                int dstStride = windowBuffer.stride * 4;
                uint8_t * src = pFrameRGBA->data[0];
                int srcStride = pFrameRGBA->linesize[0];
                // 由于window的stride和幀的stride不同,因此需要逐行復(fù)制
                int h;
                for (h = 0; h < videoHeight; h++) {
                    memcpy(dst + h * dstStride, src + h * srcStride, (size_t) srcStride);
                }
                ANativeWindow_unlockAndPost(nativeWindow);
            }
        } else {
            LOGE("Error sending a packet for decoding");
            return -1;
        }
        // 每繪制一幀便休眠16毫秒,避免繪制過快導(dǎo)致播放的視頻速度加快
        usleep(1000 * 16);
    }
    av_packet_unref(&packet);
}

編碼參數(shù)上下文的拷貝

ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if (ret < 0){
    printf("Failed to copy context from input to output stream codec context\n");
    goto end;
}
 
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
    out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

======》

AVCodecContext *codec_ctx = avcodec_alloc_context3(in_codec);
ret = avcodec_parameters_to_context(codec_ctx, in_stream->codecpar);
if (ret < 0){
    printf("Failed to copy in_stream codecpar to codec context\n");
    goto end;
}
 
codec_ctx->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
    codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 
ret = avcodec_parameters_from_context(out_stream->codecpar, codec_ctx);
if (ret < 0){
    printf("Failed to copy codec context to out_stream codecpar context\n");
    goto end;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容