利用 FFMPEG库将one frame YUV to one frame PPM

PPM:   header  +  RGBRGBRGB......

 

code:

 

#include<stdio.h>
#include<ffmpeg/avcodec.h>
#include<ffmpeg/avformat.h>
#include<stdlib.h>

#define WIDTH 352
#define HEIGHT 288

int main()
{
    av_register_all();
    FILE *fin=fopen("test.yuv","rb");
    FILE *fout=fopen("test.ppm","wb");
   
    uint8_t *buffer_in;
    uint8_t *buffer_out;

    AVFrame  *pFrameYUV ,*pFrameRGB;
   

    buffer_in=(uint8_t *)malloc(WIDTH*HEIGHT*1.5*sizeof(uint8_t));
    buffer_out=(uint8_t *)malloc(WIDTH*HEIGHT*3*sizeof(uint8_t) );

    fread(buffer_in,sizeof(uint8_t),WIDTH*HEIGHT*1.5,fin);
     pFrameYUV=avcodec_alloc_frame();
    avpicture_fill((AVPicture *)pFrameYUV, buffer_in, 0,WIDTH,HEIGHT);

     pFrameRGB=avcodec_alloc_frame();
    avpicture_fill((AVPicture *)pFrameRGB, buffer_out, 2,WIDTH,HEIGHT);

    img_convert( (AVPicture *)pFrameRGB,2,(AVPicture *)pFrameYUV,0,WIDTH,HEIGHT);

    fprintf(fout,"P6/n%d %d/n 255/n",WIDTH,HEIGHT);

    int i;
    for(i=0;i<HEIGHT;i++)
    {
        fwrite( pFrameRGB->data[0]+i*WIDTH*3,1,WIDTH*3,fout);
    }
   

    free(buffer_in);
    free(buffer_out);

    fclose(fin);
    fclose(fout);
   


    return 0;
}

 

gcc -o test test.c -lavutil -lavformat -lavcodec

 

你可能感兴趣的:(File,header,gcc,buffer)