在linux下实现用ffmpeg把YUV420帧保存成图片

在网上搜了很久相关的问题,但是好像没有一个在linux下跑得比较完整的例子,不过经过自己一番搜索和总结,终于做出来了,哈哈,看下面的代码吧。

这个例子可以保存成bmp或者jpeg格式的图片


下面的结构是保存bmp图片是用到的结构:

  1. //把内存对齐定义为2个字节,  
  2. //这个可以避免BITMAPFILEHEADER  
  3. //出现4字节的对齐而使bmp位图的头出错  
  4. #pragma pack(2)  
  5.   
  6. //下面两个结构是位图的结构  
  7. typedef struct BITMAPFILEHEADER  
  8. {   
  9.     u_int16_t bfType;   
  10.     u_int32_t bfSize;   
  11.     u_int16_t bfReserved1;   
  12.     u_int16_t bfReserved2;   
  13.     u_int32_t bfOffBits;   
  14. }BITMAPFILEHEADER;   
  15.   
  16. typedef struct BITMAPINFOHEADER  
  17. {   
  18.     u_int32_t biSize;   
  19.     u_int32_t biWidth;   
  20.     u_int32_t biHeight;   
  21.     u_int16_t biPlanes;   
  22.     u_int16_t biBitCount;   
  23.     u_int32_t biCompression;   
  24.     u_int32_t biSizeImage;   
  25.     u_int32_t biXPelsPerMeter;   
  26.     u_int32_t biYPelsPerMeter;   
  27.     u_int32_t biClrUsed;   
  28.     u_int32_t biClrImportant;   
  29. }BITMAPINFODEADER;  

下面两段代码是把yuv420的数据转成rgb24的代码:

下面的是用ffmpeg的库转的。

  1. //===================把yuv帧数据转为rgb===========  
  2.     unsigned char *rgbBuf = new unsigned char[width*height*3];    
  3.   
  4.     struct SwsContext* img_convert_ctx = 0;  
  5.     int linesize[4] = {3*width, 0, 0, 0};  
  6.   
  7.     img_convert_ctx = sws_getContext(width, height,  
  8.         PIX_FMT_YUV420P,  
  9.         width,  
  10.         height,  
  11.         PIX_FMT_RGB24, SWS_FAST_BILINEAR, 0, 0, 0);  
  12.     if (img_convert_ctx != 0)  
  13.     {  
  14.         sws_scale(img_convert_ctx, pict->data, pict->linesize, 0, height, (uint8_t**)&rgbBuf, linesize);  
  15.         sws_freeContext(img_convert_ctx);  
  16.     saveFrame(rgbBuf, currentFrameNum_/(frameNum_/pictureNum), width, height);//生成图片  
  17.     }  
  18.     delete [] rgbBuf;  
  19.     //==============================================  


下面给出自己在网上找到的把yuv420转成rgb24的代码:

  1. const int Table_fv1[256]={ -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153, -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137, -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118, -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79, -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64, -62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37, -36, -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148, 150, 151, 152, 154, 155, 157, 158, 159, 161, 162, 164, 165, 166, 168, 169, 171, 172, 173, 175, 176, 178 };  
  2. const int Table_fv2[256]={ -92, -91, -91, -90, -89, -88, -88, -87, -86, -86, -85, -84, -83, -83, -82, -81, -81, -80, -79, -78, -78, -77, -76, -76, -75, -74, -73, -73, -72, -71, -71, -70, -69, -68, -68, -67, -66, -66, -65, -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, -54, -53, -53, -52, -51, -51, -50, -49, -48, -48, -47, -46, -46, -45, -44, -43, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, -34, -33, -33, -32, -31, -31, -30, -29, -28, -28, -27, -26, -26, -25, -24, -23, -23, -22, -21, -21, -20, -19, -18, -18, -17, -16, -16, -15, -14, -13, -13, -12, -11, -11, -10, -9, -8, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 62, 63, 64, 65, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 72, 73, 74, 75, 75, 76, 77, 77, 78, 79, 80, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 87, 88, 89, 90, 90 };  
  3. const int Table_fu1[256]={ -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39, -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -33, -33, -33, -32, -32, -32, -31, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26, -25, -25, -25, -24, -24, -24, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43 };  
  4. const int Table_fu2[256]={ -227, -226, -224, -222, -220, -219, -217, -215, -213, -212, -210, -208, -206, -204, -203, -201, -199, -197, -196, -194, -192, -190, -188, -187, -185, -183, -181, -180, -178, -176, -174, -173, -171, -169, -167, -165, -164, -162, -160, -158, -157, -155, -153, -151, -149, -148, -146, -144, -142, -141, -139, -137, -135, -134, -132, -130, -128, -126, -125, -123, -121, -119, -118, -116, -114, -112, -110, -109, -107, -105, -103, -102, -100, -98, -96, -94, -93, -91, -89, -87, -86, -84, -82, -80, -79, -77, -75, -73, -71, -70, -68, -66, -64, -63, -61, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -40, -38, -36, -34, -32, -31, -29, -27, -25, -24, -22, -20, -18, -16, -15, -13, -11, -9, -8, -6, -4, -2, 0, 1, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 21, 23, 24, 26, 28, 30, 31, 33, 35, 37, 39, 40, 42, 44, 46, 47, 49, 51, 53, 54, 56, 58, 60, 62, 63, 65, 67, 69, 70, 72, 74, 76, 78, 79, 81, 83, 85, 86, 88, 90, 92, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, 115, 117, 118, 120, 122, 124, 125, 127, 129, 131, 133, 134, 136, 138, 140, 141, 143, 145, 147, 148, 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 173, 175, 177, 179, 180, 182, 184, 186, 187, 189, 191, 193, 195, 196, 198, 200, 202, 203, 205, 207, 209, 211, 212, 214, 216, 218, 219, 221, 223, 225 };  
  5.   
  6.   
  7. bool YV12_To_RGB24(const unsigned char* pYV12, unsigned char* &pRGB24, const int& width, const int& height)  
  8. {  
  9.     if(NULL == pYV12)  
  10.         return false;  
  11.   
  12.     long buffsize = width * 24 * height;  
  13.   
  14.     pRGB24 = new unsigned char[buffsize];  
  15.   
  16.     if(!pYV12 || !pRGB24)  
  17.         return false;  
  18.   
  19.     const long nYLen = long(width*height);  
  20.     const int nHfWidth = (width>>1);  
  21.   
  22.     if(nYLen<1 || nHfWidth<1)  
  23.         return false;  
  24.   
  25.     // Y data  
  26.     const unsigned char* yData = pYV12;  
  27.     // v data  
  28.     const unsigned char* vData = &yData[nYLen];  
  29.     // u data  
  30.     const unsigned char* uData = &vData[nYLen>>2];  
  31.   
  32.     if(!uData || !vData)  
  33.         return false;  
  34.   
  35.     int rgb[3];  
  36.     int i, j, m, n, x, y, py, rdif, invgdif, bdif;  
  37.     m = -width;  
  38.     n = -nHfWidth;  
  39.   
  40.     bool addhalf = true;  
  41.     for(y=0; y<height;y++) {  
  42.         m += width;  
  43.         if( addhalf ){  
  44.             n+=nHfWidth;  
  45.             addhalf = false;  
  46.         } else {  
  47.             addhalf = true;  
  48.         }  
  49.         for(x=0; x<width;x++)  {  
  50.             i = m + x;  
  51.             j = n + (x>>1);  
  52.   
  53.             py = yData[i];  
  54.   
  55.             // search tables to get rdif invgdif and bidif  
  56.             rdif = Table_fv1[vData[j]];    // fv1  
  57.             invgdif = Table_fu1[uData[j]] + Table_fv2[vData[j]]; // fu1+fv2  
  58.             bdif = Table_fu2[uData[j]]; // fu2  
  59.   
  60.             rgb[2] = py+rdif;    // R  
  61.             rgb[1] = py-invgdif; // G  
  62.             rgb[0] = py+bdif;    // B  
  63.   
  64.             //j = nYLen - width - m + x;//Õâžö»áÍŒÏñµßµ¹  
  65.             j = m + x;  
  66.             i = (j<<1) + j;  
  67.   
  68.             // copy this pixel to rgb data  
  69.             for(j=0; j<3; j++){  
  70.                 if(rgb[j]>=0 && rgb[j]<=255){  
  71.                     pRGB24[i + j] = rgb[j];  
  72.                 }else{  
  73.                     pRGB24[i + j] = (rgb[j] < 0)? 0 : 255;  
  74.                 }  
  75.             }  
  76.         }  
  77.     }  
  78.     return true;  
  79. }  


下面是实际执行生成图片的代码,输入rgb24的数据。注释了的是生成bmp的代码,没注释的是生成jpeg图片的代码。

  1. void ipcToFile::saveFrame(uint8_t *pRGBBuffer, int iFrame, int width, int height)  
  2. {  
  3.     /*BITMAPFILEHEADER bmpheader; 
  4.     BITMAPINFOHEADER bmpinfo; 
  5.     FILE *fp; 
  6.  
  7.     unsigned int uiTmp, uiTmp2; 
  8.     unsigned char *ucTmp = NULL; 
  9.     unsigned char ucRGB; 
  10.     int i; 
  11.  
  12.     uiTmp = (width*3+3)/4*4*height; 
  13.     uiTmp2 = width*height*3; 
  14.  
  15.     char szFilename[1024]; 
  16.     sprintf(szFilename, "test%d.bmp",  iFrame);//图片名字为视频名+号码 
  17.     fp=fopen(szFilename, "wb"); 
  18.     if(fp==NULL) 
  19.             return; 
  20.  
  21.     //文件标识"BM"(即0x4D42)表示位图 
  22.     bmpheader.bfType = 0x4D42; 
  23.     //保留。设置为0 
  24.     bmpheader.bfReserved1 = 0; 
  25.     //保留。设置为0 
  26.     bmpheader.bfReserved2 = 0; 
  27.     //从文件开始到位图数据的偏移量(单位:字节) 
  28.     bmpheader.bfOffBits = sizeof(bmpheader) + sizeof(bmpinfo); 
  29.     //整个文件的大小(单位:字节) 
  30.     bmpheader.bfSize = bmpheader.bfOffBits + uiTmp; 
  31.  
  32.     //信息头长度(单位:字节)。典型值为28 
  33.     bmpinfo.biSize = 0x28; 
  34.     //位图宽度(单位:像素) 
  35.     bmpinfo.biWidth = width; 
  36.     //位图高度(单位:像素)。若其为正,表示倒向的位图。若为负,表示正向的位图 
  37.     bmpinfo.biHeight = height; 
  38.     //位图的面数(为1) 
  39.     bmpinfo.biPlanes = 1; 
  40.     //每个像素的位数 
  41.     bmpinfo.biBitCount = 24; 
  42.     //压缩说明。0(BI_RGB)表示不压缩 
  43.     bmpinfo.biCompression = 0; 
  44.     //用字节数表示的位图数据的大小(为4的位数) 
  45.     bmpinfo.biSizeImage = uiTmp; 
  46.     //水平分辨率(单位:像素/米) 
  47.     bmpinfo.biXPelsPerMeter = 0; 
  48.     //垂直分辨率(单位:像素/米) 
  49.     bmpinfo.biYPelsPerMeter = 0; 
  50.     //位图使用的颜色数 
  51.     bmpinfo.biClrUsed = 0; 
  52.     //重要的颜色数 
  53.     bmpinfo.biClrImportant = 0; 
  54.  
  55.     fwrite(&bmpheader,sizeof(bmpheader),1,fp); 
  56.     fwrite(&bmpinfo,sizeof(bmpinfo),1,fp); 
  57.  
  58.     //把图像数据倒置 
  59.     uint8_t tmp[width*3];//临时数据 
  60.     for(int i = 0; i < height/2; i++) 
  61.     { 
  62.         memcpy(tmp, &(pRGBBuffer[width*i*3]), width*3); 
  63.         memcpy(&(pRGBBuffer[width*i*3]), &(pRGBBuffer[width*(height-1-i)*3]), width*3); 
  64.         memcpy(&(pRGBBuffer[width*(height-1-i)*3]), tmp, width*3); 
  65.     } 
  66.  
  67.     fwrite(pRGBBuffer,width*height*3,1,fp); 
  68.     fclose(fp);*/  
  69.   
  70.     struct jpeg_compress_struct jcs;  
  71.   
  72.     // 声明错误处理器,并赋值给jcs.err域  
  73.     struct jpeg_error_mgr jem;  
  74.     jcs.err = jpeg_std_error(&jem);  
  75.   
  76.     jpeg_create_compress(&jcs);  
  77.   
  78.     char szFilename[1024];  
  79.     sprintf(szFilename, "test%d.jpg", iFrame);//图片名字为视频名+号码  
  80.     FILE *fp = fopen(szFilename, "wb");  
  81.     if(fp == NULL)  
  82.             return;  
  83.   
  84.     jpeg_stdio_dest(&jcs, fp);  
  85.   
  86.     jcs.image_width = width;    // 为图的宽和高,单位为像素   
  87.     jcs.image_height = height;  
  88.     jcs.input_components = 3;   // 在此为1,表示灰度图, 如果是彩色位图,则为3   
  89.     jcs.in_color_space = JCS_RGB; //JCS_GRAYSCALE表示灰度图,JCS_RGB表示彩色图像  
  90.   
  91.     jpeg_set_defaults(&jcs);   
  92.     jpeg_set_quality (&jcs, 80, true);  
  93.   
  94.     jpeg_start_compress(&jcs, TRUE);  
  95.   
  96.     JSAMPROW row_pointer[1];   // 一行位图  
  97.     int row_stride = jcs.image_width * 3;//每一行的字节数,如果不是索引图,此处需要乘以3  
  98.   
  99.     // 对每一行进行压缩  
  100.     while (jcs.next_scanline < jcs.image_height) {  
  101.         row_pointer[0] = &(pRGBBuffer[jcs.next_scanline * row_stride]);  
  102.         jpeg_write_scanlines(&jcs, row_pointer, 1);  
  103.     }  
  104.   
  105.     jpeg_finish_compress(&jcs);  
  106.     jpeg_destroy_compress(&jcs);  
  107.   
  108.     fclose(fp);  
  109. }  
原文地址:https://www.cnblogs.com/lidabo/p/3326493.html