1. 程式人生 > 其它 >輸出ppm檔案有誤,無法顯示

輸出ppm檔案有誤,無法顯示

技術標籤:筆記C/C++Windows

// 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 檔案