yuv420p转jpg图片

int yuv420p_to_jpeg(const char * filename, const char* pdata,int image_width,int image_height, int quality)
{   struct jpeg_compress_struct cinfo;  struct jpeg_error_mgr jerr;  cinfo.err = jpeg_std_error(&jerr);  jpeg_create_compress(&cinfo);  FILE * outfile;    // target file  if ((outfile = fopen(filename, "wb")) == NULL) {  fprintf(stderr, "can't open %s\n", filename);  exit(1);  }  jpeg_stdio_dest(&cinfo, outfile);  cinfo.image_width = image_width;  // image width and height, in pixels  cinfo.image_height = image_height;  cinfo.input_components = 3;    // # of color components per pixel  cinfo.in_color_space = JCS_YCbCr;  //colorspace of input image  jpeg_set_defaults(&cinfo);  jpeg_set_quality(&cinfo, quality, TRUE );  //  //  cinfo.raw_data_in = TRUE;  cinfo.jpeg_color_space = JCS_YCbCr;  cinfo.comp_info[0].h_samp_factor = 2;  cinfo.comp_info[0].v_samp_factor = 2;  /  jpeg_start_compress(&cinfo, TRUE);  JSAMPROW row_pointer[1];unsigned char *yuvbuf;if((yuvbuf=(unsigned char *)malloc(image_width*3))!=NULL)memset(yuvbuf,0,image_width*3);unsigned char *ybase,*ubase;ybase=pdata;ubase=pdata+image_width*image_height;  int j=0;while (cinfo.next_scanline < cinfo.image_height) {int idx=0;for(int i=0;i<image_width;i++){ yuvbuf[idx++]=ybase[i + j * image_width];yuvbuf[idx++]=ubase[j/2 * image_width+(i/2)*2];yuvbuf[idx++]=ubase[j/2 * image_width+(i/2)*2+1];    }row_pointer[0] = yuvbuf;jpeg_write_scanlines(&cinfo, row_pointer, 1);j++;}jpeg_finish_compress(&cinfo);  jpeg_destroy_compress(&cinfo);  fclose(outfile);  return 0;
}

从AVFrame提取yuv数据到char * buf

 //如果是视频else if (pstream_info[i].dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO){int new_videosize = pkt.size;int video_decode_size = avpicture_get_size(pstream_info->dec_ctx->pix_fmt, Zoom_Width,Zoom_Height);uint8_t * video_decode_buf =( uint8_t *)calloc(1,video_decode_size * 3 * sizeof(char)); //最大分配的空间,能满足yuv的各种格式// Decode video frameavcodec_decode_video2(pstream_info->dec_ctx, pDecodeFrame, &frameFinished,&pkt);if(frameFinished) {if (pstream_info->dec_ctx->pix_fmt == AV_PIX_FMT_YUV420P) //如果是yuv420p的{for(i = 0 ; i < pstream_info->dec_ctx->height ; i++){memcpy(video_decode_buf+pstream_info->dec_ctx->width*i,pDecodeFrame->data[0]+pDecodeFrame->linesize[0]*i,pstream_info->dec_ctx->width);}for(j = 0 ; j < pstream_info->dec_ctx->height/2 ; j++){memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/2*j,pDecodeFrame->data[1]+pDecodeFrame->linesize[1]*j,pstream_info->dec_ctx->width/2);}for(k  =0 ; k < pstream_info->dec_ctx->height/2 ; k++){memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/2*j+pstream_info->dec_ctx->width/2*k,pDecodeFrame->data[2]+pDecodeFrame->linesize[2]*k, pstream_info->dec_ctx->width/2);}}else if (pstream_info->dec_ctx->pix_fmt == AV_PIX_FMT_YUV422P)//如果是yuv422p的{for(i = 0 ; i < pstream_info->dec_ctx->height ; i++){memcpy(video_decode_buf+pstream_info->dec_ctx->width*i,pDecodeFrame->data[0]+pDecodeFrame->linesize[0]*i,pstream_info->dec_ctx->width);}for(j = 0 ; j < pstream_info->dec_ctx->height ; j++){memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/2*j,pDecodeFrame->data[1]+pDecodeFrame->linesize[1]*j,pstream_info->dec_ctx->width/2);}for(k  =0 ; k < pstream_info->dec_ctx->height ; k++){memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/2*j+pstream_info->dec_ctx->width/2*k,pDecodeFrame->data[2]+pDecodeFrame->linesize[2]*k, pstream_info->dec_ctx->width/2);}}else{//可扩展}video_decode_size = avpicture_get_size(pstream_info->dec_ctx->pix_fmt, pstream_info->dec_ctx->width,pstream_info->dec_ctx->height);new_videosize = video_decode_size; //缩放或格式转换if (pstream_info->dec_ctx->width != Zoom_Width || pstream_info->dec_ctx->height !=  Zoom_Height ||pstream_info->dec_ctx->pix_fmt != Zoom_pix_fmt){new_videosize = VideoScaleYuvZoom(Is_flip,pstream_info->dec_ctx->width ,pstream_info->dec_ctx->height,(int)pstream_info->dec_ctx->pix_fmt,Zoom_Width,Zoom_Height,Zoom_pix_fmt,video_decode_buf);}//这里可以取出数据frame_info->stream_idx = pstream_info->stream_idx;//frame_info->pts = pDecodeFrame->pkt_pts * 1000 * av_q2d(pstream_info->stream->time_base);  //转化成毫秒frame_info->pts = pDecodeFrame->pkt_pts ;frame_info->timebase_den = pstream_info->stream->time_base.den;  frame_info->timebase_num = pstream_info->stream->time_base.num; frame_info->bufsize = new_videosize;memcpy(frame_info->buf,video_decode_buf,new_videosize);}else{//缓存frame_info->stream_idx = pstream_info->stream_idx;frame_info->pts = 0;frame_info->timebase_den = 0;frame_info->timebase_num = 0;frame_info->bufsize = 0;memset(frame_info->buf,0,MAX_FRAME_SIZE);}if (video_decode_buf){free(video_decode_buf);video_decode_buf = NULL;}video_decode_size = 0;}

yuv420p转jpg图片相关推荐

  1. onPreviewFrame 相机输出格式转换yuv420p保存成图片

    在最近项目中,因为特殊需要,底层相机往外输出了i420 也就是yuv420p,输出的bytes 需要转成换h264,同时某个时间还需要保存一张图片,如何将i420 转jpeg ?可以ffmpeg 也可 ...

  2. 【音视频数据数据处理 1】【YUV篇】分离YUV420P像素数据中的Y、U、V分量

    [音视频数据数据处理 1][YUV篇]分离YUV420P像素数据中的Y.U.V分量 一.YUV420P 数据格式介绍 二.分离YUV420P像素数据中的Y.U.V分量 - 代码实现 三.查看生成的YU ...

  3. ffmpeg解码H.264视频数据,MFC播放视频

    ffmpeg 是一个完整的视频流解决方案,开源且有良好的跨平台性,ffmpeg具有强大的多媒体数据处理能力,能够实现视频的采集,多种视频格式间转换,给视频添加水印等多种功能,已被 VLC.Mplaye ...

  4. FFmpeg 常用命令汇总

    https://www.yuv420.com/2019/12/23/ffmpeg-chang-yong-ming-ling-hui-zong/ 引言 开源音视频处理工具FFMPEG以其强大的功能而在音 ...

  5. WebService SOAP XML 与 REST JSON 架构的比较

    一个采购订单对10005088物料收货20个,放2050仓库的SOAP XML报文 一般客户端访问服务器端web服务通常可以由HTTPService.WebService.RemoteObject等方 ...

  6. YUV420P、YUV420SP、NV12、NV21和RGB互相转换并存储为JPEG以及PNG图片

    音视频实践学习 android全平台编译ffmpeg以及x264与fdk-aac实践 ubuntu下使用nginx和nginx-rtmp-module配置直播推流服务器 android全平台编译ffm ...

  7. 如何将yuv420p图像数据转换为RGB数据并使用opencv保存为jpg图片

    yuv420是用4个byte存储4个Y的信息,用1个Byte存储U的信息,一个Byte存储V的信息, 这4个Y共用这2个U和V ,也就是用6个Byte 存储4个像素信息,也就是一个像素需要12个Bit ...

  8. YUV420P图片分离Y/U/V分量并显示验证

    背景 最近在学习音视频编解码相关知识,其中涉及到了YUV相关知识. 想要将一个YUV420P格式图片的三个分量分别保存出来并且显示. 参考 YUV格式的讲解: https://www.bilibili ...

  9. java yuv420sp转nv21_Android图片格式NV12(YUV420SP)转YUV420P

    Android设备调用摄像头采集数据时,Camera对象通过setPreviewCallback (Camera.PreviewCallback callback)函数,在onPreviewFrame ...

最新文章

  1. 卷积神经网络(CNN)在无人驾驶中的应用
  2. c语言如何输出无限小数,printf的格式控制(C语言)
  3. oracle join过滤数据,oracle join on 数据过滤问题
  4. 三、MySql 数据类型
  5. C/C++文件操作效率比较——FILE/fstream
  6. 点评2009年PHP十大图书(2)
  7. 容器,Docker和Azure Container Service
  8. 使用jQuery的load()进行页面模块化加载
  9. Web渗透测试学习路线图
  10. EXCEL 统计每日订单量(拉勾教育数据分析实战训练营学习笔记)
  11. 生成田字格模板(word)
  12. 【数据分析师3级】 数据挖掘方法论
  13. 在bluehost如何使用WHM面板和ssh链接添加附加IP
  14. 《关于我摸鱼一天后搞定PyCharm这档事》Python环境配置
  15. NYIST 113 字符串替换
  16. Android从零单排之免费短信验证
  17. [mini-css-extract-plugin] warning Conflicting order
  18. 图说人工智能的研究内容
  19. FFmpeg从入门到精通-云享读书会
  20. 塔望3W消费战略案丨聚焦川崎火锅料,回归赛道一梯队

热门文章

  1. 300美元复刻ChatGPT九成功力,GPT-4亲自监考,130亿参数开源模型「小羊驼」来了...
  2. Db2 sql summarize
  3. Android基础入门教程——2.5.3 AlertDialog(对话框)详解
  4. 2283156.html
  5. android glsurface 闪屏,浅谈SurfaceView与GLSurfaceView
  6. 使用Python3编写脚本一键备份MySQL数据库
  7. 听说拍人家违章月入十万?那我写了个自动检测车辆是否违章不发财了
  8. miui android耗电快,miui8耗电严重吗 小米miui8省电教程图解
  9. .NET反汇编工具 dnSpy逆向初探
  10. 【C#+Access+WindowsAPI】实现仿360的安全卫士 六:实用工具集合讲解(附源码和资源)