文章目录

  • 0. 参考文献
  • 1. jpeg转yuv
  • 2. 源码下载

JPG和PNG等图片的解码方式和视频解码是一样的,因为视频是由一幅一幅的图片组成的,只不过视频的帧是会前后参考的,而JPG等图片是单独的一帧而已。

0. 参考文献

ffmpeg解码JPG和PNG等图片

1. jpeg转yuv

int main(int argc, char* argv[])
{int i;char szFileName[128] = { 0 };int decLen = 0;int frame = 0;AVCodecContext *pCodecCtx = NULL;AVFrame *pFrame = NULL;AVCodec *pCodec = NULL;AVFormatContext *pFormatCtx = NULL;/*if (argc != 3){fprintf(stderr, "ERROR:need 3 argument!n");exit(-1);}*/sprintf(szFileName, "%s", "H:/bmp2jpg/1.jpeg");#ifdef ENABLE_DEMUX_SAVEFILE* frvdemux = fopen("rvdemuxout.rm", "wb+");if (NULL == frvdemux){fprintf(stderr, "create rvdemuxout file failedn");exit(1);}
#endif/* output yuv file name */sprintf(ffrvout, "%s", "H:/bmp2jpg/1.yuv");pfout = fopen(ffrvout, "wb+");if (NULL == pfout){printf("create output file failedn");exit(1);}printf("==========> Begin test ffmpeg call ffmpeg rv decodern");av_register_all();/* Open input video file *///printf("before avformat_open_input [%s]n", szFileName);if (avformat_open_input(&pFormatCtx, szFileName, NULL, NULL) != 0){fprintf(stderr, "Couldn't open input filen");return -1;}//printf("after avformat_open_inputn");/* Retrieve stream information */if (avformat_find_stream_info(pFormatCtx, 0) < 0){printf("av_find_stream_info ERRORn");return -1;}//printf("after av_find_stream_info, n");/* Find the first video stream */int videoStream = -1;printf("==========> pFormatCtx->nb_streams = %dn", pFormatCtx->nb_streams);for (i = 0; i < pFormatCtx->nb_streams; i++) {if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {videoStream = i;printf("the first video stream index: videoStream = %dn", videoStream);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]->codec;printf("pCodecCtx->codec_id = %dn", pCodecCtx->codec_id);pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL) {fprintf(stderr, "can not find decoder!n");return -1;}/* Open codec */if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){printf("cannot open software codecn");return -1; // Could not open codec}printf("==========> Open software codec successn");pFrame = av_frame_alloc();if (pFrame == NULL){fprintf(stderr, "avcodec_alloc_frame() ERRORn");return -1;}/* flag whether we get a decoded yuv frame */int frameFinished;int packetno = 0;AVPacket packet;av_init_packet(&packet);while (av_read_frame(pFormatCtx, &packet) >= 0) {//printf("[main]avpkt->slice_count=%dn", packet.sliceNum);/* Is this a packet from the video stream? */if (packet.stream_index == videoStream) {packetno++;
#ifdef ENABLE_PRINT_FRAME_BYTESif (1) {int i;int size = packet.size < PRINT_BYTES ? packet.size : PRINT_BYTES;unsigned char *data = packet.data;printf("===>[%5d] [", packet.size);for (i = 0; i < size; i++)printf("%02x ", data[i]);printf("]n");}
#endif
#ifdef ENABLE_DEMUX_SAVEfwrite(packet.data, 1, packet.size, frvdemux);
#endif//printf("[the %d packet]packet.size = %dn", packetno++, packet.size);while (packet.size > 0) {decLen = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);//printf("[video_decode_example]after avcodec_decode_video2,decoded=%dn",decLen);if (decLen < 0) {fprintf(stderr, "[video_decode_example]Error while decoding frame %dn", frame);//exit(1);/* FIXME if decode one frame err, ignore this frame */decLen = packet.size;}if (frameFinished) {printf("got a yuv framen");//printf(stderr, "[video_decode_example]saving frame %3dn", frame);/* the picture is allocated by the decoder. no need to free it */if (frame == 0) {printf("[video_decode_example]picture->linesize[0]=%d, c->width=%d,c->height=%dn",pFrame->linesize[0], pCodecCtx->width, pCodecCtx->height);printf("===>YUV format = %dn", pFrame->format);}
#ifdef ENABLE_YUV_SAVE/* save yuv pic */if (frame < FRAME_NUM) {switch (pFrame->format) {case 0:    /* YUV420P */case 12:yuv420p_save(pFrame, pCodecCtx);break;case 2:    /* RGB24 */rgb24_save(pFrame, pCodecCtx);break;case 13:    /* YUVJ422P */yuv422p_save(pFrame, pCodecCtx);break;case 14:    /* YUVJ444P */yuv444p_save(pFrame, pCodecCtx);break;default:fprintf(stderr, "unsupport YUV format for savingn");break;}fprintf(stderr, "===>save pic successn");}
#endif/* frame index grow */frame++;}//printf("===========> %dn", decLen);/* left data in pkt , go on decoding */packet.data += decLen;packet.size -= decLen;}if (frame == FRAME_NUM) {printf("==========> decoded [%d pkt frames] ---> save [%d YUV frames], enough to stop!n", packetno, FRAME_NUM);break;}}/* FIXME no need free in this file *///printf("free packet that was allocated by av_read_framen");// Free the packet that was allocated by av_read_frame//av_free_packet(&packet);}printf("decoding job down! begin to freen");/* Free the YUV frame */av_free(pFrame);/* Close the codec */avcodec_close(pCodecCtx);/* Close the video file */avformat_free_context(pFormatCtx);fclose(pfout);printf("==========> END-OKn");return 0;
}

2. 源码下载

jpeg转换为yuv格式图像源码

FFmpeg开发实战(六):jpeg转换为yuv格式图像相关推荐

  1. 使用C++实现YUV格式图像与RGB格式图像之间相互转换

    使用C++实现YUV格式图像与RGB格式图像之间相互转换 一.RGB与YUV转换公式 1.RGB转YUV 1)RGB转换亮度与色差信号公试: 2)归一化为YUV的转化公试为: 2.YUV转RGB 二. ...

  2. FFmpeg开发实战(五):bmp转换为jpeg格式图像

    文章目录 1. bmp结构 2. bgr24转yuv420p 3. yuv420转jpeg 4. 下载 本文介绍了将bmp格式图像转换为jpeg格式图像的方法,附有详细的代码和图像示例. 1. bmp ...

  3. 视频编解码学习(六):YUV格式学习

    1.YUV格式详解 参考文章 : http://blog.csdn.NET/linweig/article/details/5515928 http://www.cnblogs.com/azraell ...

  4. FFmpeg开发实战(三):FFmpeg 打印音视频Meta信息

    在之前使用FFmpeg命令行的时候,我们经常看到FFmpeg命令行在输出音视频文件的会打印一下文件的Meta信息,类似如图: 那么我们如何通过代码的方式输出这些Meta信息呢? FFmpeg提供了一个 ...

  5. CrazyWing:Python自动化运维开发实战 六、流程控制

    Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python程序语言指定任何非0和非空(null)值为true,0 或者 nu ...

  6. python数据分析办公自动化实战(六):excel条件格式、单元格格式、数字格式

    #简介# 本篇是使用openpyxl库的一点经验总结,涉及到以下功能:操作excel.设置边框背景颜色居中等格式.设置数字格式(小数百分数).百分比条件格式.插入行列等,依然是代码+注释+总结. 任务 ...

  7. 微信小程序开发实战(24):选择图像

    wx:chooseImage方法用于从相册选择若干图像文件(1到n),或从相机拍摄图像,并返回被选中图像的临时路径,以便以后处理. wx:chooseImage方法有一个Object类型的参数,该参数 ...

  8. 使用C++实现多张BMP图片转换为YUV动画----附加淡入淡出转场(逐渐变明变暗),及垂直滑像转场(逐行渐变)

    使用C++实现多张BMP图片转换为YUV动画----附加淡入淡出转场(逐渐变明变暗),及垂直滑像转场(逐行渐变) 一.BMP图像简介 1.BMP图像是什么? 2.BMP图像文件结构 1)图象文件头 2 ...

  9. 小程序云开发实战步骤教程

    ####前言: 在学习云开发的时候将自己的学习过程记录下来了,放在了网上,收获了一波好评,今天下午在办公室没有事情,也发现之前有人在博客里面评论,你这个教程还有一半哩,可能是csdn的自动搬运功能出来 ...

  10. FFmpeg音视频开发实战5 iOS/Android/windows/Linux -陈超-专题视频课程

    FFmpeg音视频开发实战5 iOS/Android/windows/Linux -159618人已学习 课程介绍          咨询QQ: 347181469. 本课程适合中,从事音视频,网络通 ...

最新文章

  1. 维护表读写的权限对象
  2. Android开发--BroadcastReceiver2
  3. c语言程序设计B试题,c语言程序设计期末试题B(含答案)Word版
  4. MySQL失效情况(范围查询,字段运算)
  5. SAP Cloud for Customer里的individual customer OData服务
  6. 【遥感物候】Hants NDVI时间序列谐波分析法数据重构,植被生长季曲线效果可佳(附Hants软件下载)
  7. quartz中定时表达式详解
  8. ASP.Net缓存总结
  9. java web listener_JavaWeb:Listener
  10. 斯坦福机器学习公开课笔记--神经网络的学习
  11. alter table 加多个字段_多个单列索引和联合索引的区别详解
  12. 灾难恢复! 关于做过快照的AVHD文件合并成VHD .
  13. mysql中grade字段降序排列_Mysql order by 多个字段排序
  14. 如何创建控制台应用程序
  15. 自动化专业考研方向简介
  16. 软考软件设计师中级考试(二)——操作系统基本原理
  17. 20135202闫佳歆-第四章家庭作业-4.47+4.48
  18. 如何制作PE盘和系统启动盘
  19. SQL on log : 同比分析各种指标
  20. 人民币金额(数字)大写转换及金币单位切换

热门文章

  1. 百度电子商务平台“有啊”正式上线,中国的电子商务平台或将重新洗牌?
  2. 戴维斯分校 计算机硕士,2017年美国加州大学戴维斯分校研究生申请之计算机......
  3. Shader实现漫反射
  4. 万王之王手游服务器维护,万王之王手游-KOK-官方网站-腾讯游戏-一个世界的重新开启...
  5. 招商银行笔试题之漂流船问题
  6. java清除session_退出页面自动清除java session方法
  7. 第一章 DirectX 计算机图形学(上)
  8. Service Worker 学习笔记
  9. centos7中添加大硬盘(超过2T)分区
  10. Nuvoton M0518 之 记录数据到LDROM,数据掉电不丢失的方式