ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

输出ppm文件有误,无法显示

2021-02-04 19:58:44  阅读:305  来源: 互联网

标签:输出 pCodecCtx packet height ppm 有误 av NULL pFrameRGB


// FFmpegDemo1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<iostream>
#include<output>

using namespace xiuye;

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
    //#include <ffmpeg/swscale.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>

}

//保存的ppm文件 无法显示!!!
void SaveFrame(AVFrame* pFrame, int width, int height, int iFrame) {
    FILE* pFile;
    char szFilename[32];
    int  y;

    // Open file
    sprintf_s(szFilename, "frame%d.ppm", iFrame);
    fopen_s(&pFile,szFilename, "wb");
    if (pFile == NULL)
        return;

    // Write header
    fprintf(pFile, "P6\n%d %d\n255\n", width, height);

    // Write pixel data
    for (y = 0; y < height; y++)
        fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, width * 3, pFile);

    // Close file
    fclose(pFile);
}


int main(int argc, char* argv[]) {
    //新的ffmpeg库不需要集中初始化的组建
    //::av_register_all();

    AVFormatContext* pFormatCtx = NULL;
    //文件工作路径是项目路径 并不是输出debug 或者 release的路径OK!!!
    if (avformat_open_input(&pFormatCtx, "D:/codes/windows/FFmpegDemo1/x64/Release/888.mp4", nullptr, nullptr) != 0) {
        std::cout << "failed to open input" << std::endl;
        return -1;
    }
    
    if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
        std::cout << "failed to find stream info" << std::endl;
        return -2;
    }
    
    //if (argc < 2) {
    //    println("input parameters less than 2");
    //    return -1;
    //}

    println(argv[0]);
    //log(argv[1]);

    // Dump information about file onto standard error
    av_dump_format(pFormatCtx, 0, argv[1], 0);

    int i;
    AVCodecContext* pCodecCtxOrig = NULL;
    AVCodecParameters* pCodecCtx = NULL;

    // Find the first video stream
    int videoStream = -1;
    for (i = 0; i < pFormatCtx->nb_streams; i++)
        if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoStream = i;
            break;
        }
    if (videoStream == -1)
        return -1; // Didn't find a video stream

      // Get a pointer to the codec context for the video stream
    pCodecCtx = pFormatCtx->streams[videoStream]->codecpar;

    AVCodec* pCodec = NULL;

    // Find the decoder for the video stream
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL) {
        fprintf(stderr, "Unsupported codec!\n");
        return -1; // Codec not found
    }
    // Copy context
    AVCodecContext*  pCodecCtx2 = avcodec_alloc_context3(pCodec);
    //if (avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
    //if (avcodec_parameters_to_context(pCodecCtxOrig,pCodecCtx) != 0) {//这一句有问题,原因待查!
    //    fprintf(stderr, "Couldn't copy codec context");
    //    return -1; // Error copying codec context
    //}
    // Open codec
    if (avcodec_open2(pCodecCtx2, pCodec,nullptr) < 0)
        return -1; // Could not open codec


    AVFrame* pFrame = NULL;

    // Allocate video frame
    pFrame = av_frame_alloc();

    // Allocate an AVFrame structure
    AVFrame* pFrameRGB = av_frame_alloc();
    if (pFrameRGB == NULL)
        return -1;

    uint8_t* buffer = NULL;
    int numBytes;
    // Determine required buffer size and allocate buffer
    //numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
    numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height,1);
    buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));


    // Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
/*    avpicture_fill((AVPicture*)pFrameRGB, buffer, AV_PIX_FMT_RGB24,
        pCodecCtx->width, pCodecCtx->height);*/    
    
    
    av_image_fill_arrays(
        pFrameRGB->data,
        pFrameRGB->linesize, 
        buffer, AV_PIX_FMT_RGB24,
        pCodecCtx->width, 
        pCodecCtx->height,
        1);


    struct SwsContext* sws_ctx = NULL;
    int frameFinished;
    AVPacket packet;
    // initialize SWS context for software scaling
    sws_ctx = sws_getContext(pCodecCtx->width,
        pCodecCtx->height,
        AVPixelFormat(pCodecCtx->format),
        pCodecCtx->width,
        pCodecCtx->height,
        AV_PIX_FMT_RGB24,
        SWS_BILINEAR,
        NULL,
        NULL,
        NULL
    );

    i = 0;
    while (av_read_frame(pFormatCtx, &packet) >= 0) {
        // Is this a packet from the video stream?
        if (packet.stream_index == videoStream) {
            // Decode video frame
            //avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
            frameFinished = av_read_frame(pFormatCtx, &packet);

            // Did we get a video frame?
            if (!frameFinished) {
                // Convert the image from its native format to RGB
                sws_scale(sws_ctx, (uint8_t const* const*)pFrame->data,
                    pFrame->linesize, 0, pCodecCtx->height,
                    pFrameRGB->data, pFrameRGB->linesize);

                // Save the frame to disk
                if (++i <= 5)
                    SaveFrame(pFrameRGB, pCodecCtx->width,
                        pCodecCtx->height, i);
            }
        }

        // Free the packet that was allocated by av_read_frame
        //av_free_packet(&packet);//old
        av_packet_unref(&packet);
    }

    // Free the RGB image
    av_free(buffer);
    av_free(pFrameRGB);

    // Free the YUV frame
    av_free(pFrame);

    // Close the codecs
    //av_free(pCodecCtx);
    //avcodec_close(pCodecCtx);
    avcodec_close(pCodecCtxOrig);

    // Close the video file
    avformat_close_input(&pFormatCtx);



    std::cout << "Hello World!\n";


}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

 

标签:输出,pCodecCtx,packet,height,ppm,有误,av,NULL,pFrameRGB
来源: https://blog.csdn.net/xiuye2015/article/details/113663286

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有