ffmpeg源码简析(三)av_malloc(),AVIOContext,AVFrame,avio_open2()等

本文深入探讨了FFmpeg中的内存管理函数,包括av_malloc(), av_realloc(), av_mallocz()和av_calloc()的实现原理,它们是对系统内存分配函数的封装并提供了额外的安全检查。此外,还分析了avio_open2()函数在libavformataviobuf.c中的作用,它用于初始化URLContext和AVIOContext,为不同的协议读写提供支持。" 130525774,7764378,Cesium指南针详解与实现,"['cesium篇', 'cesium指南针', '3D地图', '地图开发', 'JavaScript']
摘要由CSDN通过智能技术生成

内存操作的常见函数位于libavutil\mem.c

av_malloc()

av_malloc()是FFmpeg中最常见的内存分配函数。

av_malloc()的代码可以简化成如下形式。

void *av_malloc(size_t size)  
{  
    void *ptr = NULL;  
    /* let's disallow possibly ambiguous cases */  
    if (size > (max_alloc_size - 32))  
        return NULL;  
    ptr = malloc(size);  
    if(!ptr && !size) {  
        size = 1;  
        ptr= av_malloc(1);  
    }  
    return ptr;  
} 

可以看出,此时的av_malloc()就是简单的封装了系统函数malloc(),并做了一些错误检查工作。
关于size_t
size _t 这个类型在FFmpeg中多次出现,简单解释一下其作用。size _t是为了增强程序的可移植性而定义的。不同系统上,定义size_t可能不一样。它实际上就是unsigned int。

av_realloc()

av_realloc()用于对申请的内存的大小进行调整。

    void *av_realloc(void *ptr, size_t size)  
    {  
    #if CONFIG_MEMALIGN_HACK  
        int diff;  
    #endif  


        /* let's disallow possibly ambiguous cases */  
        if (size > (max_alloc_size - 32))  
            return NULL;  


    
以下是在MFC中使用FFmpeg将YUY2格式的字节流转换为图片的代码示例: ```c++ #include <Windows.h> #include <iostream> #include <fstream> #include <vector> #include <string> #include <exception> #include <algorithm> #include <functional> #include <codecvt> #include <locale> #include "ffmpeg.h" // YUV420P转RGB24 void YUV420P_to_RGB24(unsigned char* yuvData, unsigned char* rgbData, int width, int height) { unsigned char* yData = yuvData; unsigned char* uData = yuvData + width * height; unsigned char* vData = yuvData + width * height * 5 / 4; int r, g, b, y, u, v; for (int i = 0, j = 0; i < width * height; i++, j += 3) { y = (int)(yData[i] - 16); u = (int)(uData[i / 4] - 128); v = (int)(vData[i / 4] - 128); r = (int)(1.164 * y + 1.596 * v); g = (int)(1.164 * y - 0.813 * v - 0.391 * u); b = (int)(1.164 * y + 2.018 * u); r = std::max(0, std::min(255, r)); g = std::max(0, std::min(255, g)); b = std::max(0, std::min(255, b)); rgbData[j] = r; rgbData[j + 1] = g; rgbData[j + 2] = b; } } // YUY2转RGB24 void YUY2_to_RGB24(unsigned char* yuy2Data, unsigned char* rgbData, int width, int height) { unsigned char* yData = yuy2Data; unsigned char* uData = yuy2Data + 1; unsigned char* vData = yuy2Data + 3; int r, g, b, y, u, v; for (int i = 0, j = 0; i < width * height / 2; i++, j += 6) { y = (int)(yData[i * 2] - 16); u = (int)(uData[i] - 128); v = (int)(vData[i] - 128); r = (int)(1.164 * y + 1.596 * v); g = (int)(1.164 * y - 0.813 * v - 0.391 * u); b = (int)(1.164 * y + 2.018 * u); r = std::max(0, std::min(255, r)); g = std::max(0, std::min(255, g)); b = std::max(0, std::min(255, b)); rgbData[j] = r; rgbData[j + 1] = g; rgbData[j + 2] = b; y = (int)(yData[i * 2 + 1] - 16); r = (int)(1.164 * y + 1.596 * v); g = (int)(1.164 * y - 0.813 * v - 0.391 * u); b = (int)(1.164 * y + 2.018 * u); r = std::max(0, std::min(255, r)); g = std::max(0, std::min(255, g)); b = std::max(0, std::min(255, b)); rgbData[j + 3] = r; rgbData[j + 4] = g; rgbData[j + 5] = b; } } // YUY2字节流转换为图片 bool YUY2ToImage(const char* yuy2Data, int dataSize, int width, int height, const char* imageFile) { bool ret = false; try { // 初始化FFmpeg av_register_all(); avcodec_register_all(); // 获取YUY2格式解码器 AVCodec* codec = avcodec_find_decoder(AV_CODEC_ID_YUYV422); if (!codec) { throw std::exception("avcodec_find_decoder failed"); } // 创建解码器上下文 AVCodecContext* codecCtx = avcodec_alloc_context3(codec); if (!codecCtx) { throw std::exception("avcodec_alloc_context3 failed"); } // 打开解码器 if (avcodec_open2(codecCtx, codec, NULL) < 0) { throw std::exception("avcodec_open2 failed"); } // 创建AVPacket和AVFrame AVPacket pkt; av_init_packet(&pkt); pkt.data = (uint8_t*)yuy2Data; pkt.size = dataSize; AVFrame* frame = av_frame_alloc(); if (!frame) { throw std::exception("av_frame_alloc failed"); } // 填充AVFrame数据 frame->width = width; frame->height = height; frame->format = AV_PIX_FMT_YUYV422; avpicture_fill((AVPicture*)frame, (uint8_t*)yuy2Data, AV_PIX_FMT_YUYV422, width, height); // 创建AVFrame用于存储转换后的RGB24数据 AVFrame* rgbFrame = av_frame_alloc(); if (!rgbFrame) { throw std::exception("av_frame_alloc failed"); } // 设置AVFrame的参数 rgbFrame->width = width; rgbFrame->height = height; rgbFrame->format = AV_PIX_FMT_RGB24; int numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, width, height); uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t)); avpicture_fill((AVPicture*)rgbFrame, buffer, AV_PIX_FMT_RGB24, width, height); // 转换YUY2到RGB24 YUY2_to_RGB24(yuy2Data, rgbFrame->data[0], width, height); // 创建输出文件 FILE* outFile = NULL; fopen_s(&outFile, imageFile, "wb"); if (!outFile) { throw std::exception("open output file failed"); } // 初始化AVFormatContext AVFormatContext* formatCtx = avformat_alloc_context(); if (!formatCtx) { throw std::exception("avformat_alloc_context failed"); } // 设置输出格式 AVOutputFormat* outputFmt = av_guess_format(NULL, imageFile, NULL); if (!outputFmt) { throw std::exception("av_guess_format failed"); } formatCtx->oformat = outputFmt; // 创建AVIOContext if (avio_open(&formatCtx->pb, imageFile, AVIO_FLAG_WRITE) < 0) { throw std::exception("avio_open failed"); } // 创建AVStream AVStream* stream = avformat_new_stream(formatCtx, codec); if (!stream) { throw std::exception("avformat_new_stream failed"); } // 设置AVCodecContext AVCodecContext* outCodecCtx = stream->codec; outCodecCtx->codec = codec; outCodecCtx->codec_id = codec->id; outCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; outCodecCtx->width = width; outCodecCtx->height = height; outCodecCtx->pix_fmt = AV_PIX_FMT_RGB24; outCodecCtx->time_base.num = 1; outCodecCtx->time_base.den = 25; // 写入文件头 avformat_write_header(formatCtx, NULL); // 写入视频帧 AVPacket packet; av_init_packet(&packet); packet.data = rgbFrame->data[0]; packet.size = numBytes; packet.pts = 0; packet.dts = 0; packet.duration = 1; packet.stream_index = stream->index; av_interleaved_write_frame(formatCtx, &packet); // 写入文件尾 av_write_trailer(formatCtx); // 释放资源 av_frame_free(&rgbFrame); av_free(buffer); av_frame_free(&frame); avcodec_close(codecCtx); avcodec_free_context(&codecCtx); avformat_free_context(formatCtx); fclose(outFile); ret = true; } catch (std::exception& e) { std::cout << e.what() << std::endl; } return ret; } ``` 在函数 `YUY2ToImage` 中,首先初始化FFmpeg库,并获取YUY2格式的解码器。然后创建解码器上下文,并打开解码器。接着创建AVPacket和AVFrame,填充AVFrame数据,并创建AVFrame用于存储转换后的RGB24数据。通过调用转换函数`YUY2_to_RGB24`,将YUY2格式的字节流转换为RGB24格式。然后创建输出文件,并初始化AVFormatContext,设置输出格式和AVIOContext。接着创建AVStream,设置AVCodecContext,并写入文件头。最后,将转换后的RGB24数据写入文件,并写入文件尾,最后释放资源。 请注意,在使用这个函数之前,在项目属性中添加FFmpeg库和头文件路径,以及链接FFmpeg库。 希望这可以帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值