07 - FFmpeg 更改视频分辨率 - 保存 yuv420p

07-16 1125阅读

---------------------------------------------------------------- 更改视频格式流程 ----------------------------------------------------------------

07 - FFmpeg 更改视频分辨率 - 保存 yuv420p
(图片来源网络,侵删)

解码后的数据存储在data[0]、data[1]、data[2]中,

data[0]存储了linesize[0] * height个数据,

为对齐解码器的CPU及其他优化等原因,导致linesize[0],实际上并不等于宽度width, 而是比宽度大。

linesize[0] - Y分量对应的一个宽度

1、av_parse_video_size -- 解析分辨率

2、sws_getContext -- 获得分辨率切换的上下文

3、av_frame_alloc -- 图像转换后的数据需要存储起来

4、av_image_get_buffer_size -- 共创建多少字节的空间

5、av_malloc -- 创建【申请空间】

6、av_image_fill_arrays -- 格式化分配空间

7、sws_scale -- 视频处理

sws_scale() 函数 -- 清空无效数据

1、图像色彩空间转换;

2、分辨率缩放

3、前后图像滤波处理

int ChangeResolutionInterface(AVCodecContext *codecCtx, AVPacket *packet, struct SwsContext *swsCtx, int destWidth, int destHeight, AVFrame *destFrame, FILE *dest_fp, int *frameCount)
{
 
    AVFrame *frame = av_frame_alloc();
    if (frame == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "frame alloc failed!\n");
    }
    int ret = avcodec_send_packet(codecCtx, packet);
    if (ret != 0)
    {
        av_log(NULL, AV_LOG_ERROR, "send packet failed:%s\n", av_err2str(ret));
        av_packet_unref(packet);
        return -1;
    }
    while (avcodec_receive_frame(codecCtx, frame) == 0)
    {
        sws_scale(swsCtx, (const uint8_t *const *)frame->data, frame->linesize, 0, codecCtx->height, destFrame->data, destFrame->linesize); // 视频处理
        // 保存 sws_scale 处理后的 帧
        fwrite(destFrame->data[0], 1, destWidth * destHeight, dest_fp);
        fwrite(destFrame->data[1], 1, destWidth * destHeight / 4, dest_fp);
        fwrite(destFrame->data[2], 1, destWidth * destHeight / 4, dest_fp);
        (*frameCount)++;
        av_log(NULL, AV_LOG_DEBUG, "frameCount = %d, linesize[0] = %d, linesize[1] = %d, linesize[2] = %d, width = %d, heigth = %d\n",
               *frameCount, destFrame->linesize[0], destFrame->linesize[1], destFrame->linesize[2], destWidth, destHeight);
    }
    if (frame)
    {
        av_frame_free(&frame);
    }
}
int ChangeResolutionDecodeVideoYUV(const char *inFileName, const char *outFileName, char *destVideoSizeString)
{
    int frameCount = 0;
    /**********************************************************************************/
    FILE *dest_fp = fopen(outFileName, "wb+");
    if (dest_fp == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "open outfile:%s failed!\n", outFileName);
        return -1;
    }
    /**********************************************************************************/
    // 分析分辨率
    int destWidth ,destHeight;
    int ret = av_parse_video_size(&destWidth, &destHeight, destVideoSizeString); // 解析分辨率
    if (ret streams[videoStreamIndex]->codecpar);
    AVCodec *decoder = avcodec_find_decoder(codecCtx->codec_id);
    if (decoder == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "find decoder failed,codec_id:%d\n", codecCtx->codec_id);
        ret = -1;
        goto fail;
    }
    ret = avcodec_open2(codecCtx, decoder, NULL);
    if (ret != 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open decoder failed:%s \n", av_err2str(ret));
        ret = -1;
        goto fail;
    }
    // 获得分辨率切换的上下文
    enum AVPixelFormat destPixFormat = codecCtx->pix_fmt;
    struct SwsContext *swsCtx = sws_getContext(codecCtx->width, codecCtx->height, codecCtx->pix_fmt,
destWidth, destHeight, destPixFormat,
SWS_FAST_BILINEAR, NULL, NULL, NULL);
    if (swsCtx == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "get sws context failed!\n");
        ret = -1;
        goto fail;
    }
    AVFrame *destFrame = av_frame_alloc(); // 图像转换后的数据需要存储起来
    uint8_t *outBuffer = av_malloc(av_image_get_buffer_size(destPixFormat, destWidth, destHeight, 1) /*共创建多少字节的空间*/);
    av_image_fill_arrays(destFrame->data, destFrame->linesize, outBuffer, destPixFormat, destWidth, destHeight, 1); // 格式化分配空间
    AVPacket packet;
    av_init_packet(&packet);
    while (av_read_frame(inFmtCtx, &packet) >= 0)
    {
        if (packet.stream_index == videoStreamIndex)
        {
            if (ChangeResolutionInterface(codecCtx, &packet, swsCtx, destWidth, destHeight, destFrame, dest_fp, &frameCount) == -1)
            {
                ret = -1;
                av_packet_unref(&packet);
                goto fail;
            }
        }
        av_packet_unref(&packet);
    }
    // flush decoder
    ChangeResolutionInterface(codecCtx, NULL, swsCtx, destWidth, destHeight, destFrame, dest_fp, &frameCount);
fail:
    if (inFmtCtx)
    {
        av_log(NULL, AV_LOG_DEBUG, "inFmtCtx format close input\n");
        avformat_close_input(&inFmtCtx);
    }
    if (codecCtx)
    {
        av_log(NULL, AV_LOG_DEBUG, "codecCtx codec free context\n");
        avcodec_free_context(&codecCtx);
    }
    if (dest_fp)
    {
        av_log(NULL, AV_LOG_DEBUG, "dest_fp fclose\n");
        fclose(dest_fp);
    }
    if (destFrame)
    {
        av_log(NULL, AV_LOG_DEBUG, "destFrame av frame free\n");
        av_frame_free(&destFrame);
    }
    if (outBuffer)
    {
        av_log(NULL, AV_LOG_DEBUG, "outBuffer free\n");
        av_free(outBuffer);
    }
    return ret;
}
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]