ffmpeg新接口之体验

       许久不使用ffmpeg了,最近一年一直是使用gstreamer在做媒体处理(因为产品采用开源框架缘故),考虑gstreamter过于庞大,过于晦涩(依赖glib,各种插件,面向对象的C框架等),自研框架还是优先开用ffmpeg来处理。将3年前的ffmpeg解码器例子用最新ffmpeg(4.2.3)来编译,问题一大堆。

首先:相关宏定义的头文件路径变了(虽然通过头文件引入相关调整解决了)。
其次:API接口变了。编译被中止了。

                              

                                                                  ffmpeg老接口编译中止

咋看,新版本ffmpeg很多接口已经降级不能使用了。主要变动如下:
1.    初始方式变了。
以前ffmpeg初始化相关av_regigser_all,av_register_xxx都废弃了,新的ffmpeg版本不需要av_regigser这个方式注册这个流程了。
2.    解码编码相关接口改了。
以前ffmpeg需要使用avcodec_encode_video2,avcodec_decode_video2,avcodec_decode_audio4, avcodec_encode_audio2等一系列相关avcodec_encode_xxxx, avcodec_decode_xxxx编解码器接口全部废弃,取而代之是avcodec_send_frame,avcodec_receive_packet ,avcodec_send_packe,avcodec_receive_frame 四个新接口统一了。
3.    其次结构体某些参数可能也改了。查看相关说明即可。
虽然新的ffmpeg的API和相关结构体参数发生了改变,好在ffmpeg注释比较明白,老的工程移植过来总体难度不大。
下面以一个h264解码生成YUV的例子来展示一下老工程在新的API框架下的使用

typedef struct Scale_Context
{
    struct SwsContext *Sctx;
    int *dstStride;
    uint8_t **DstData;
};
static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
    const char *filename, Scale_Context *pScx)
{
    char buf[1024];
    int ret;
    ret = avcodec_send_packet(dec_ctx, pkt);
    if (ret < 0) {
        fprintf(stderr, "Error sending a packet for decoding\n");
        exit(1);
    }
    int iGot_Frame = 0;
    while (ret >= 0) {
        //ret = avcodec_decode_video2(dec_ctx, frame, &iGot_Frame,pkt); 老的接口已经废弃
        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr, "Error during decoding\n");
            exit(1);
        }

        if (pScx->Sctx != NULL)
        {
             sws_scale(pScx->Sctx, frame->data,
             frame->linesize, 0, 1080, pScx->DstData, pScx->dstStride);
        }
        //I420ToYUY2() libyuv
        printf("saving frame %3d\n", dec_ctx->frame_number);
        fflush(stdout);
        /* the picture is allocated by the decoder. no need to
        free it */
        int linesize = frame->linesize[0];
        if (pScx->Sctx == NULL)
        {
            _snprintf(buf, sizeof(buf), "%s-%d.yuv420", filename, dec_ctx->frame_number);
            yuv420_save(frame->data, frame->linesize,
                frame->width, frame->height, buf);
        }
        else
        {
            _snprintf(buf, sizeof(buf), "%s-%d.yuyv422", filename, dec_ctx->frame_number);
            yuyv422_save(pScx->DstData[0], pScx->dstStride[0], frame->width, frame->height, buf);
        }

    }
}

Decode 接口采用新的API解码。avcodec_send_packet将AVpacket送入解码器,avcodec_receive_frame从解码器读出解码后的原始帧数据YUV。

int _tmain(int argc, _TCHAR* argv[])
{
    const char *filename, *outfilename;
    const AVCodec *codec;
    AVCodecParserContext *parser;
    AVCodecContext *c = NULL;
    FILE *f;
    AVFrame *frame;
    uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    uint8_t *data;
    size_t   data_size;
    int ret;
    AVPacket *pkt;
    Scale_Context SctxObj;
    struct SwsContext *sws_ctx = NULL;
    uint8_t *src_data[4], *dst_data[4];
    int src_linesize[4], dst_linesize[4];
    int type = 0;
    if (argc <= 2) {
        fprintf(stderr, "Usage: %s <input file> <output file> [type]\n", argv[0]);
        exit(0);
    }
    filename = argv[1];
    outfilename = argv[2];
    if (argc>3)
     type = atoi(argv[3]);
    pkt = av_packet_alloc();
    if (!pkt)
        exit(1);
   //av_register_all();新框架注册接口已经废弃
    /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
    memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);

    /* find the MPEG-1 video decoder */
    codec = avcodec_find_decoder(AV_CODEC_ID_H264);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }
    parser = av_parser_init(codec->id);
    if (!parser) {
        fprintf(stderr, "parser not found\n");
        exit(1);
    }
    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }
    /* For some codecs, such as msmpeg4 and mpeg4, width and height
    MUST be initialized there because this information is not
    available in the bitstream. */
    /* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }
    if (type > 0)
    {

        int src_w = 1920, src_h = 1080, dst_w = 1920, dst_h = 1080;
        /* create scaling context  1080P的YUV420转换成YUYV422 */
        sws_ctx = sws_getContext(src_w, src_h, AV_PIX_FMT_YUV420P,
            dst_w, dst_h, AV_PIX_FMT_YUYV422,
            SWS_BILINEAR, NULL, NULL, NULL);
        if (!sws_ctx) {
            fprintf(stderr,
                "Impossible to create scale context for the conversion ");
            ret = AVERROR(EINVAL);
            exit(1);
        }

        /* buffer is going to be written to rawvideo file, no alignment */
        if ((ret = av_image_alloc(dst_data, dst_linesize,
            dst_w, dst_h, AV_PIX_FMT_YUYV422, 1)) < 0) {
            fprintf(stderr, "Could not allocate destination image\n");
            exit(1);
        }
    }
    SctxObj.Sctx = sws_ctx;
    SctxObj.dstStride = dst_linesize;
    SctxObj.DstData = dst_data;
    while (!feof(f)) {
        /* read raw data from the input file */
        data_size = fread(inbuf, 1, INBUF_SIZE, f);
        if (!data_size)
            break;

        /* use the parser to split the data into frames */
        data = inbuf;
        while (data_size > 0) {
            ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
                data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
            if (ret < 0) {
                fprintf(stderr, "Error while parsing\n");
                exit(1);
            }
            data += ret;
            data_size -= ret;

            if (pkt->size)
                decode(c, frame, pkt, outfilename, &SctxObj);
        }
    }
    /* flush the decoder */
    decode(c, frame, NULL, outfilename, &SctxObj);
    fclose(f);
    av_parser_close(parser);
    avcodec_free_context(&c);
    av_frame_free(&frame);
    av_packet_free(&pkt);
    getchar();
    return 0;
}

main函数传入3个参数,第一个参数是输入264文件名,第二个参数是输出YUV的文件名,第3个参数指定YUV的格式。type大于0,输入为yuyv422,否则为YUV420格式。在这里我们演示了通过ffmpeg自带的libswscale完成图像数据的转换。
libswscale:主要功能:
图像格式互转,图像缩放,前后图像滤波处理等。如:YUV和RGB各种格式互转,YUV或RGB图像的缩放,图像各种滤波处理等。libswscale不足之处是转换效率不高,如果对性能有更高追求,建议采用opencv或libyuv来完成图像转换,缩放。
编译,运行该程序,生成的YUV420和YUYV422格式如下:

                                     

YUV420和YUYV422格式显示如下:

                           

                                                                                   YUV420格式的预览

                              

                                                                       YUV422的预览

更多更详细资源请关注公众号:AV_Chat

猜你喜欢

转载自blog.csdn.net/fengliang191/article/details/106892567