linux之x86裁剪移植---ffmpeg的H264解码显示(420、422)

在虚拟机上yuv420可以正常显示 ,而945(D525)模块上却无法显示 ,后来验证了directdraw的yuv420也无法显示 ,由此怀疑显卡不支持 ,后把420转换为422显示。

420显示如下:

/*
编译命令:arm-linux-gcc -o show2642 264showyuv2.c -I/usr/local/ffmpeg_arm/include/   -L/usr/local/ffmpeg_arm/lib/ -lswresample -lavformat -lavutil -lavcodec -lswscale -lx264   libSDL.agcc -o test test.c -I/usr/local/ffmpeg/include/   -L/usr/local/ffmpeg/lib/ -lswresample -lavformat -lavutil -lavcodec -lswscale  -lx264 -lSDL */
#include "stdio.h"
#include "stdlib.h"#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswresample/swresample.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/dict.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avstring.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.h"
#include "libavutil/bprint.h"
#include "libavutil/time.h"
#include "libavutil/threadmessage.h"
#include "SDL/SDL.h"//#include "libavfilter/avcodec.h"
#include "libavcodec/avcodec.h"#if HAVE_SYS_RESOURCE_H
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#elif HAVE_GETPROCESSTIMES
#include <windows.h>
#endif
#if HAVE_GETPROCESSMEMORYINFO
#include <windows.h>
#include <psapi.h>
#endif#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif#if HAVE_TERMIOS_H
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <termios.h>
#elif HAVE_KBHIT
#include <conio.h>
#endif#if HAVE_PTHREADS
#include <pthread.h>
#endif#include <time.h>#include "libavutil/avassert.h"#define MAX_LEN  1024 * 50此方法参考官网的例子
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,FILE *f)
{//  FILE *f;int i;// f = fopen(filename,"w");// fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);for (i = 0; i < ysize; i++);//   fwrite(buf + i * wrap, 1, xsize, f);//  fclose(f);
}int main()
{//下面初始化h264解码库//avcodec_init();int w = 720;int h = 576,retu;SDL_Rect rect;av_register_all();AVFrame *pFrame_ = NULL;/* find the video encoder */AVCodec *videoCodec = avcodec_find_decoder(AV_CODEC_ID_H264);//得到264的解码器类if(!videoCodec){printf("avcodec_find_decoder error\n");return -1;}AVCodecParserContext *avParserContext = av_parser_init(AV_CODEC_ID_H264);//得到解析帧类,主要用于后面的帧头查找if(!avParserContext){printf("av_parser_init  error\n");return -1;}AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);//解码会话层if(!codec_){printf("avcodec_alloc_context3  error\n");return -1;}//初始化参数,下面的参数应该由具体的业务决定codec_->time_base.num = 1;codec_->frame_number = 1; //每包一个视频帧codec_->codec_type = AVMEDIA_TYPE_VIDEO;codec_->bit_rate = 0;codec_->time_base.den = 25;//帧率codec_->width = 720;//视频宽codec_->height = 576;//视频高if(avcodec_open2(codec_, videoCodec, NULL) >= 0)//打开解码器{pFrame_ = av_frame_alloc();// Allocate video frame    成功打开解码器后, 此时可以分配帧内存, 当然你也可以在后面每次都分配、释放, 在此我省功夫, 只在开始分配一次if (!pFrame_) {fprintf(stderr, "Could not allocate video frame\n");exit(1);}}else{printf("avcodec_open2 error\n");return -1;}AVPacket packet = {0};int dwBufsize = 10;int frameFinished = dwBufsize;//这个是随便填入数字,没什么作用av_init_packet(&packet);packet.data = NULL;//这里填入一个指向完整H264数据帧的指针packet.size = 0;//这个填入H264数据帧的大小FILE *myH264 = fopen("1.264", "rb");//解码的文件264if(myH264 == NULL){perror("cant open 264 file\n");return -1;}FILE *yuvfile = fopen("my264.yuv", "wb");//成功解码后保存成的YUV文件, 可以用YUV工具打开浏览if(yuvfile == NULL){perror("cant open YUV file\n");return -1;}int readFileLen = 1;char readBuf[MAX_LEN];unsigned char *parseBuf = malloc(20*MAX_LEN);//这个地方浪费了我一个下午时间, 当时我用的是栈内存,即unsigned char parseBuf[20*MAX_LEN], 结果运行程序一直报错, 此处需要用堆内存才能正常解码int  parseBufLen = 0;int frameCount = 0;printf("begin...\n");printf("readBuf address  is %x\n", readBuf);
/SDL initSDL_Surface* hello = NULL;SDL_Surface* screen = NULL;//Start SDL// SDL_Init( SDL_INIT_EVERYTHING );SDL_Init(SDL_INIT_VIDEO);//Set up screenscreen = SDL_SetVideoMode( 1024, 768, 32, SDL_SWSURFACE );SDL_Overlay* overlay = SDL_CreateYUVOverlay(w, h, SDL_YV12_OVERLAY, screen);SDL_LockSurface(screen);SDL_LockYUVOverlay(overlay);
//while(readFileLen > 0)//开始解码工作{//printf("begin...\n");readFileLen = fread(readBuf, 1, sizeof(readBuf), myH264);//首先从文件里读出数据if(readFileLen <= 0){printf("read over\n");break;}else{int handleLen = 0;int handleFileLen = readFileLen;while(handleFileLen > 0){int nLength = av_parser_parse2(avParserContext, codec_, &parseBuf, &parseBufLen, readBuf + handleLen, handleFileLen, 0, 0, 0);//查找264帧头handleFileLen -= nLength;handleLen += nLength;if(parseBufLen <= 0)//当parseBufLen大于0时,说明查找到了帧头{continue;}packet.size = parseBufLen;//将查找到的帧长度送入packet.data = parseBuf;//将查找到的帧内存送入if(frameCount>100)break;//printf("parseBuf address is %x\n", parseBuf);while(packet.size > 0){//下面开始真正的解码int decodeLen = avcodec_decode_video2(codec_, pFrame_, &frameFinished, &packet);if(decodeLen < 0)break;packet.size -= decodeLen;packet.data += decodeLen;if(frameFinished > 0)//成功解码{int picSize = codec_->height * codec_->width;//int newSize = picSize * 1.5;//申请内存//unsigned char *buf = malloc(newSize);int height = pFrame_->height;int width = pFrame_->width;//printf("OK, get data\n");//printf("Frame height is %d\n", height);//printf("Frame width is %d\n", width);frameCount ++;printf("Frame count is %d\n", frameCount);pgm_save(pFrame_->data[0], pFrame_->linesize[0],//保存Ycodec_->width, codec_->height, yuvfile);pgm_save(pFrame_->data[1], pFrame_->linesize[1],//保存Ucodec_->width/2, codec_->height/2, yuvfile);pgm_save(pFrame_->data[2], pFrame_->linesize[2],//保存Vcodec_->width/2, codec_->height/2, yuvfile);///有了YUV数据, 后面可以用FFMPEG提供的转换方法,将其转成RGB数据,进行后续的显示或其它的图像处理工作sdlint i;for(i=0;i<576;i++){//fwrite(buf + i * wrap, 1, xsize, f);memcpy(overlay->pixels[0]+i*1280, pFrame_->data[0]+i*pFrame_->linesize[0], 720);                               }for(i=0;i<288;i++){memcpy(overlay->pixels[2]+i*640, pFrame_->data[1]+i*pFrame_->linesize[1], 360);memcpy(overlay->pixels[1]+i*640, pFrame_->data[2]+i*pFrame_->linesize[2], 360);                                                                      }SDL_UnlockYUVOverlay(overlay);SDL_UnlockSurface(screen);rect.w = w;rect.h = h;rect.x = rect.y = 0;SDL_DisplayYUVOverlay(overlay, &rect);//sdlSDL_Delay(40);}elseprintf("failed to decodec\n");}}}}//释放工作avcodec_close(codec_);av_free(codec_);av_free_packet(&packet);av_frame_free(&pFrame_);//SDLSDL_FreeYUVOverlay(overlay);SDL_FreeSurface(screen);//Quit SDLSDL_Quit();fclose(yuvfile);fclose(myH264);}

422显示如下:

/*
编译命令:arm-linux-gcc -o show2642 264showyuv2.c -I/usr/local/ffmpeg_arm/include/   -L/usr/local/ffmpeg_arm/lib/ -lswresample -lavformat -lavutil -lavcodec -lswscale -lx264   libSDL.agcc -o test test.c -I/usr/local/ffmpeg/include/   -L/usr/local/ffmpeg/lib/ -lswresample -lavformat -lavutil -lavcodec -lswscale  -lx264 -lSDL */
#include "stdio.h"
#include "stdlib.h"#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswresample/swresample.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/dict.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avstring.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.h"
#include "libavutil/bprint.h"
#include "libavutil/time.h"
#include "libavutil/threadmessage.h"
#include "SDL/SDL.h"//#include "libavfilter/avcodec.h"
#include "libavcodec/avcodec.h"#if HAVE_SYS_RESOURCE_H
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#elif HAVE_GETPROCESSTIMES
#include <windows.h>
#endif
#if HAVE_GETPROCESSMEMORYINFO
#include <windows.h>
#include <psapi.h>
#endif#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif#if HAVE_TERMIOS_H
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <termios.h>
#elif HAVE_KBHIT
#include <conio.h>
#endif#if HAVE_PTHREADS
#include <pthread.h>
#endif#include <time.h>#include "libavutil/avassert.h"#define MAX_LEN  1024 * 50此方法参考官网的例子此方法参考官网的例子
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,FILE *f)
{//  FILE *f;int i;// f = fopen(filename,"w");// fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);for (i = 0; i < ysize; i++);// fwrite(buf + i * wrap, 1, xsize, f);//  fclose(f);
}int main()
{//下面初始化h264解码库//avcodec_init();int w = 720;int h = 576,retu;SDL_Rect rect;av_register_all();AVFrame *pFrame_ = NULL;/* find the video encoder */AVCodec *videoCodec = avcodec_find_decoder(AV_CODEC_ID_H264);//得到264的解码器类if(!videoCodec){printf("avcodec_find_decoder error\n");return -1;}AVCodecParserContext *avParserContext = av_parser_init(AV_CODEC_ID_H264);//得到解析帧类,主要用于后面的帧头查找if(!avParserContext){printf("av_parser_init  error\n");return -1;}AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);//解码会话层if(!codec_){printf("avcodec_alloc_context3  error\n");return -1;}//初始化参数,下面的参数应该由具体的业务决定codec_->time_base.num = 1;codec_->frame_number = 1; //每包一个视频帧codec_->codec_type = AVMEDIA_TYPE_VIDEO;codec_->bit_rate = 0;codec_->time_base.den = 25;//帧率codec_->width = 720;//视频宽codec_->height = 576;//视频高if(avcodec_open2(codec_, videoCodec, NULL) >= 0)//打开解码器{pFrame_ = av_frame_alloc();// Allocate video frame    成功打开解码器后, 此时可以分配帧内存, 当然你也可以在后面每次都分配、释放, 在此我省功夫, 只在开始分配一次if (!pFrame_) {fprintf(stderr, "Could not allocate video frame\n");exit(1);}}else{printf("avcodec_open2 error\n");return -1;}AVPacket packet = {0};int dwBufsize = 10;int frameFinished = dwBufsize;//这个是随便填入数字,没什么作用av_init_packet(&packet);packet.data = NULL;//这里填入一个指向完整H264数据帧的指针packet.size = 0;//这个填入H264数据帧的大小FILE *myH264 = fopen("1.264", "rb");//解码的文件264if(myH264 == NULL){perror("cant open 264 file\n");return -1;}FILE *yuvfile = fopen("my264.yuv", "wb");//成功解码后保存成的YUV文件, 可以用YUV工具打开浏览if(yuvfile == NULL){perror("cant open YUV file\n");return -1;}int readFileLen = 1;char readBuf[MAX_LEN];unsigned char *parseBuf = malloc(20*MAX_LEN);//这个地方浪费了我一个下午时间, 当时我用的是栈内存,即unsigned char parseBuf[20*MAX_LEN], 结果运行程序一直报错, 此处需要用堆内存才能正常解码int  parseBufLen = 0;int frameCount = 0;printf("begin...\n");printf("readBuf address  is %x\n", readBuf);
/SDL initSDL_Surface* hello = NULL;SDL_Surface* screen = NULL;//Start SDL// SDL_Init( SDL_INIT_EVERYTHING );SDL_Init(SDL_INIT_VIDEO);//Set up screenscreen = SDL_SetVideoMode( 720, 576, 32, SDL_SWSURFACE );SDL_Overlay* overlay = SDL_CreateYUVOverlay(w, h, SDL_YUY2_OVERLAY, screen);SDL_LockSurface(screen);SDL_LockYUVOverlay(overlay);
unsigned char yuv422[768*576*2];
//while(readFileLen > 0)//开始解码工作{//printf("begin...\n");readFileLen = fread(readBuf, 1, sizeof(readBuf), myH264);//首先从文件里读出数据if(readFileLen <= 0){printf("read over\n");break;}else{int handleLen = 0;int handleFileLen = readFileLen;while(handleFileLen > 0){int nLength = av_parser_parse2(avParserContext, codec_, &parseBuf, &parseBufLen, readBuf + handleLen, handleFileLen, 0, 0, 0);//查找264帧头handleFileLen -= nLength;handleLen += nLength;if(parseBufLen <= 0)//当parseBufLen大于0时,说明查找到了帧头{continue;}packet.size = parseBufLen;//将查找到的帧长度送入packet.data = parseBuf;//将查找到的帧内存送入if(frameCount>100)break;//printf("parseBuf address is %x\n", parseBuf);while(packet.size > 0){//下面开始真正的解码int decodeLen = avcodec_decode_video2(codec_, pFrame_, &frameFinished, &packet);//if(decodeLen < 0)break;packet.size -= decodeLen;packet.data += decodeLen;if(frameFinished > 0)//成功解码{int picSize = codec_->height * codec_->width;//int newSize = picSize * 1.5;//申请内存//unsigned char *buf = malloc(newSize);int height = pFrame_->height;int width = pFrame_->width;//printf("OK, get data\n");//printf("Frame height is %d\n", height);//printf("Frame width is %d\n", width);frameCount ++;printf("Frame count is %d\n", frameCount);pgm_save(pFrame_->data[0], pFrame_->linesize[0],//保存Ycodec_->width, codec_->height, yuvfile);pgm_save(pFrame_->data[1], pFrame_->linesize[1],//保存Ucodec_->width/2, codec_->height/2, yuvfile);pgm_save(pFrame_->data[2], pFrame_->linesize[2],//保存Vcodec_->width/2, codec_->height/2, yuvfile);///有了YUV数据, 后面可以用FFMPEG提供的转换方法,将其转成RGB数据,进行后续的显示或其它的图像处理工作sdlint i;/*     for(i=0;i<576;i++){//fwrite(buf + i * wrap, 1, xsize, f);memcpy(overlay->pixels[0]+i*720, pFrame_->data[0]+i*pFrame_->linesize[0], 720);                               }for(i=0;i<288;i++){memcpy(overlay->pixels[2]+i*360, pFrame_->data[1]+i*pFrame_->linesize[1], 360);memcpy(overlay->pixels[1]+i*360, pFrame_->data[2]+i*pFrame_->linesize[2], 360);                                                                      }*/int k=0,y,x;	//yuv420  -> yuv422for( y=0;y<576;y++){for( x=0;x<720;x++){yuv422[k++] = pFrame_->data[0][y*pFrame_->linesize[0]+x];yuv422[k++] = x%2==0?pFrame_->data[1][(y/2)*pFrame_->linesize[1]+x/2]:pFrame_->data[2][(y/2)*pFrame_->linesize[2]+x/2];}}memcpy(overlay->pixels[0],yuv422, codec_->width*codec_->height*2);SDL_UnlockYUVOverlay(overlay);SDL_UnlockSurface(screen);rect.w = w;rect.h = h;rect.x = rect.y = 0;SDL_DisplayYUVOverlay(overlay, &rect);//sdlSDL_Delay(40);}elseprintf("failed to decodec\n");}}}}//释放工作avcodec_close(codec_);av_free(codec_);av_free_packet(&packet);av_frame_free(&pFrame_);//SDLSDL_FreeYUVOverlay(overlay);SDL_FreeSurface(screen);//Quit SDLSDL_Quit();fclose(yuvfile);fclose(myH264);}

采用sws_scale 实现的数据转换

/*
编译命令:arm-linux-gcc -o show2642 264showyuv2.c -I/usr/local/ffmpeg_arm/include/   -L/usr/local/ffmpeg_arm/lib/ -lswresample -lavformat -lavutil -lavcodec -lswscale -lx264   libSDL.agcc -o test test.c -I/usr/local/ffmpeg/include/   -L/usr/local/ffmpeg/lib/ -lswresample -lavformat -lavutil -lavcodec -lswscale  -lx264 -lSDL */
#include "stdio.h"
#include "stdlib.h"#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswresample/swresample.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/dict.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avstring.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.h"
#include "libavutil/bprint.h"
#include "libavutil/time.h"
#include "libavutil/threadmessage.h"
#include "SDL/SDL.h"//#include "libavfilter/avcodec.h"
#include "libavcodec/avcodec.h"#if HAVE_SYS_RESOURCE_H
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#elif HAVE_GETPROCESSTIMES
#include <windows.h>
#endif
#if HAVE_GETPROCESSMEMORYINFO
#include <windows.h>
#include <psapi.h>
#endif#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif#if HAVE_TERMIOS_H
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <termios.h>
#elif HAVE_KBHIT
#include <conio.h>
#endif#if HAVE_PTHREADS
#include <pthread.h>
#endif#include <time.h>#include "libavutil/avassert.h"#define MAX_LEN  1024 * 50此方法参考官网的例子
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,FILE *f)
{//  FILE *f;int i;// f = fopen(filename,"w");// fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);for (i = 0; i < ysize; i++);//   fwrite(buf + i * wrap, 1, xsize, f);//  fclose(f);
}int main()
{//下面初始化h264解码库//avcodec_init();int w = 720;int h = 576,retu;SDL_Rect rect;av_register_all();AVFrame *pFrame_ = NULL,*pFrameYUV;struct SwsContext *img_convert_ctx =NULL;pFrameYUV =av_frame_alloc();/* find the video encoder */AVCodec *videoCodec = avcodec_find_decoder(AV_CODEC_ID_H264);//得到264的解码器类if(!videoCodec){printf("avcodec_find_decoder error\n");return -1;}AVCodecParserContext *avParserContext = av_parser_init(AV_CODEC_ID_H264);//得到解析帧类,主要用于后面的帧头查找if(!avParserContext){printf("av_parser_init  error\n");return -1;}AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);//解码会话层if(!codec_){printf("avcodec_alloc_context3  error\n");return -1;}//初始化参数,下面的参数应该由具体的业务决定codec_->time_base.num = 1;codec_->frame_number = 1; //每包一个视频帧codec_->codec_type = AVMEDIA_TYPE_VIDEO;codec_->bit_rate = 0;codec_->time_base.den = 25;//帧率codec_->width = 720;//视频宽codec_->height = 576;//视频高if(avcodec_open2(codec_, videoCodec, NULL) >= 0)//打开解码器{pFrame_ = av_frame_alloc();// Allocate video frame    成功打开解码器后, 此时可以分配帧内存, 当然你也可以在后面每次都分配、释放, 在此我省功夫, 只在开始分配一次if (!pFrame_) {fprintf(stderr, "Could not allocate video frame\n");exit(1);}}else{printf("avcodec_open2 error\n");return -1;}AVPacket packet = {0};int dwBufsize = 10;int frameFinished = dwBufsize;//这个是随便填入数字,没什么作用av_init_packet(&packet);packet.data = NULL;//这里填入一个指向完整H264数据帧的指针packet.size = 0;//这个填入H264数据帧的大小FILE *myH264 = fopen("1.264", "rb");//解码的文件264if(myH264 == NULL){perror("cant open 264 file\n");return -1;}FILE *yuvfile = fopen("my264.yuv", "wb");//成功解码后保存成的YUV文件, 可以用YUV工具打开浏览if(yuvfile == NULL){perror("cant open YUV file\n");return -1;}int readFileLen = 1;char readBuf[MAX_LEN];unsigned char *parseBuf = malloc(20*MAX_LEN);//这个地方浪费了我一个下午时间, 当时我用的是栈内存,即unsigned char parseBuf[20*MAX_LEN], 结果运行程序一直报错, 此处需要用堆内存才能正常解码int  parseBufLen = 0;int frameCount = 0;printf("begin...\n");printf("readBuf address  is %x\n", readBuf);
/SDL initSDL_Surface* hello = NULL;SDL_Surface* screen = NULL;//Start SDL// SDL_Init( SDL_INIT_EVERYTHING );SDL_Init(SDL_INIT_VIDEO);//Set up screenscreen = SDL_SetVideoMode( 720, 576, 32, SDL_SWSURFACE );SDL_Overlay* overlay = SDL_CreateYUVOverlay(w, h, SDL_YUY2_OVERLAY, screen);SDL_LockSurface(screen);SDL_LockYUVOverlay(overlay);
//
//int numBytes = avpicture_get_size(AV_PIX_FMT_YUYV422, codec_->width,  codec_->height);  uint8_t* yuv422 = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));  avpicture_fill((AVPicture *)pFrameYUV, yuv422, AV_PIX_FMT_YUYV422,  codec_->width, codec_->height); 
///while(readFileLen > 0)//开始解码工作{//printf("begin...\n");readFileLen = fread(readBuf, 1, sizeof(readBuf), myH264);//首先从文件里读出数据if(readFileLen <= 0){printf("read over\n");break;}else{int handleLen = 0;int handleFileLen = readFileLen;while(handleFileLen > 0){int nLength = av_parser_parse2(avParserContext, codec_, &parseBuf, &parseBufLen, readBuf + handleLen, handleFileLen, 0, 0, 0);//查找264帧头handleFileLen -= nLength;handleLen += nLength;if(parseBufLen <= 0)//当parseBufLen大于0时,说明查找到了帧头{continue;}packet.size = parseBufLen;//将查找到的帧长度送入packet.data = parseBuf;//将查找到的帧内存送入if(frameCount>100)break;//printf("parseBuf address is %x\n", parseBuf);while(packet.size > 0){//下面开始真正的解码int decodeLen = avcodec_decode_video2(codec_, pFrame_, &frameFinished, &packet);if(decodeLen < 0)break;packet.size -= decodeLen;packet.data += decodeLen;if(frameFinished > 0)//成功解码{int picSize = codec_->height * codec_->width;//int newSize = picSize * 1.5;//申请内存//unsigned char *buf = malloc(newSize);int height = pFrame_->height;int width = pFrame_->width;//printf("OK, get data\n");//printf("Frame height is %d\n", height);//printf("Frame width is %d\n", width);frameCount ++;printf("Frame count is %d\n", frameCount);pgm_save(pFrame_->data[0], pFrame_->linesize[0],//保存Ycodec_->width, codec_->height, yuvfile);pgm_save(pFrame_->data[1], pFrame_->linesize[1],//保存Ucodec_->width/2, codec_->height/2, yuvfile);pgm_save(pFrame_->data[2], pFrame_->linesize[2],//保存Vcodec_->width/2, codec_->height/2, yuvfile);///有了YUV数据, 后面可以用FFMPEG提供的转换方法,将其转成RGB数据,进行后续的显示或其它的图像处理工作sdlint i;/*      for(i=0;i<576;i++){//fwrite(buf + i * wrap, 1, xsize, f);memcpy(overlay->pixels[0]+i*720, pFrame_->data[0]+i*pFrame_->linesize[0], 720);                               }for(i=0;i<288;i++){memcpy(overlay->pixels[2]+i*360, pFrame_->data[1]+i*pFrame_->linesize[1], 360);memcpy(overlay->pixels[1]+i*360, pFrame_->data[2]+i*pFrame_->linesize[2], 360);                                                                      }*/img_convert_ctx = sws_getContext(codec_->width, codec_->height, codec_->pix_fmt, codec_->width, codec_->height, AV_PIX_FMT_YUYV422, 2, NULL, NULL, NULL);   sws_scale(img_convert_ctx, (const uint8_t* const*) pFrame_->data,  pFrame_->linesize, 0, codec_->height, pFrameYUV->data,pFrameYUV->linesize);  memcpy(overlay->pixels[0],yuv422, codec_->width*codec_->height*2);SDL_UnlockYUVOverlay(overlay);SDL_UnlockSurface(screen);rect.w = w;rect.h = h;rect.x = rect.y = 0;SDL_DisplayYUVOverlay(overlay, &rect);//sdlSDL_Delay(40);}elseprintf("failed to decodec\n");}}}}//释放工作avcodec_close(codec_);av_free(codec_);av_free_packet(&packet);av_frame_free(&pFrame_);//SDLSDL_FreeYUVOverlay(overlay);SDL_FreeSurface(screen);//Quit SDLSDL_Quit();fclose(yuvfile);fclose(myH264);}



转载于:https://www.cnblogs.com/mao0504/p/5589743.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/374675.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring依赖注入技术的发展

回顾Spring框架的历史&#xff0c;您会发现实现依赖注入的方式在每个发行版中都在增加。 如果您使用该框架已经超过一个月&#xff0c;那么在这篇回顾性文章中可能不会发现任何有趣的东西。 除了Scala中的最后一个示例&#xff0c;没有其他希望&#xff0c;这种语言在Spring中意…

JS encode decode

网上查到的全都是escape&#xff0c;和需要的编码不是一回事&#xff0c;好不容易找到的结果 保存下来以备以后使用js对文字进行编码涉及3个函数&#xff1a;escape,encodeURI,encodeURIComponent&#xff0c;相应3个解码函数&#xff1a;unescape,decodeURI,decodeURIComponen…

流媒体服务器 笔记

1.sip服务器回SBC Port Unreachable 说明转码器接收RTCP的端口没有打开 转载于:https://www.cnblogs.com/luoyinjie/p/7219359.html

力扣151. 翻转字符串里的单词

给你一个字符串 s &#xff0c;逐个翻转字符串中的所有 单词 。 单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。 请你返回一个翻转 s 中单词顺序并用单个空格相连的字符串。 没思路&#xff0c;看到的官方给的&#xff0c;简洁明了&…

Spring 3 HornetQ 2.1集成教程

通过Spring框架使用JBoss的新超高性能消息传递系统。 HornetQ是一个开放源代码项目&#xff0c;用于构建多协议&#xff0c;可嵌入&#xff0c;非常高性能的集群异步消息传递系统。 它是用Java编写的&#xff0c;并且可以在具有Java 5或更高版本运行时的任何平台上运行。 Horn…

B/S和C/S架构图解

软件&#xff1a;B/S和C/S两种架构模式。接下来用三张图片解释&#xff0c;什么是B/S什么是C/S。 图片一&#xff1a;软件架构模式 图片二&#xff1a;C/S结构模式 图片三&#xff1a;B/S结构模式 相信图解胜过冗长文字的解释&#xff0c;什么是B/S什么是C/S一目了然。 转载于:…

557. 反转字符串中的单词 III

给定一个字符串&#xff0c;你需要反转字符串中每个单词的字符顺序&#xff0c;同时仍保留空格和单词的初始顺序。 class Solution {public String reverseWords(String s) {StringBuffer res new StringBuffer();int length s.length();int i 0;while(i < length){int …

休眠陷阱

我已经使用Hibernate已有一段时间了&#xff0c;当我一段时间不使用Hibernate项目时&#xff0c;发现自己犯的错误与上次相同。 因此&#xff0c;这是我的监视清单&#xff0c;希望对其他人也有用。 实现hashCode和equals 一般而言&#xff0c;应该始终实现这些方法&#xff…

HDU 5371 Hotaru's problem (Manacher,回文串)

题意&#xff1a;给一个序列&#xff0c;找出1个连续子序列&#xff0c;将其平分成前&#xff0c;中&#xff0c;后等长的3段子序列&#xff0c;要求【前】和【中】是回文&#xff0c;【中】和【后】是回文。求3段最长为多少&#xff1f;由于平分的关系&#xff0c;所以答案应该…

bash 与 dash

Ubuntu 的 bash和dash的区别 什么是bash &#xff1f; Bash(GNU Bourne-Again Shell)是许多Linux平台的内定Shell&#xff0c;事实上&#xff0c;还有许多传统UNIX上用的Shell&#xff0c;像tcsh、csh、ash、bsh、ksh等 等&#xff0c;Shell Script大致都类同&#xff0c;当您学…

350. 两个数组的交集 II

给你两个整数数组 nums1 和 nums2 &#xff0c;请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数&#xff0c;应与元素在两个数组中都出现的次数一致&#xff08;如果出现次数不一致&#xff0c;则考虑取较小值&#xff09;。可以不考虑输出结果的顺序。 来源&a…

Eclipse:如何附加Java源代码

在Eclipse中&#xff0c;当您按Ctrl按钮并单击任何类名称时&#xff0c;IDE会将您带到该类的源文件。 这是项目中具有的类的正常行为。 但是&#xff0c;如果您也希望Java核心类具有相同的行为&#xff0c;则可以通过将Java源代码附加到Eclipse IDE来实现。 一旦附加了源代码&a…

【树状数组】

问题的提出&#xff1a;是否可以用线性数据结构的方法解决动态统计子树权和的问题呢&#xff1f; 有的&#xff0c;树状数组。 假设当前数组为a[]&#xff0c;元素个数为n。 1. 子区间的权和数组为sum&#xff0c;那么数组a[]中 i 到 j这段区间的数组元素和为sum[i,j] a[k]的累…

2013VS快捷键

VS2013常用快捷键&#xff1a; 1.回到上一个光标位置/前进到下一个光标位置 1&#xff09;回到上一个光标位置&#xff1a;使用组合键“Ctrl -”&#xff1b; 2&#xff09;前进到下一个光标位置&#xff1a;“Ctrl Shift - ”。 2.复制/剪切/删除整行代码 1&#xff09;如果…

GWT,GWT-Ext(SmartGWT),GXT(Ext GWT)常见任务

我在我们的JCG合作伙伴之一UI-Programming博客上浏览了一些旧文章&#xff0c;并注意到有很多简短的文章&#xff0c;介绍了如何使用GWT&#xff0c;GWT-Ext&#xff08;SmartGWT&#xff09;和GXT&#xff08;Ext GWT&#xff09;执行一些常见任务。 &#xff09;。 我相信它们…

h.264 去块滤波

块效应及其产生原因 我们在观看视频的时候&#xff0c;在运动剧烈的场景常能观察到图像出现小方块&#xff0c;小方块在边界处呈现不连续的效果&#xff08;如下图&#xff09;&#xff0c;这种现象被称为块效应&#xff08;blocking artifact&#xff09;。 首先我们需要搞清楚…

android开发的知识点(一)

1.android中背景图的设置&#xff1a; 将背景图放入到项目中的res/drawable-hdpi或res/drawable-mdpi或res/drawable-xhdpi或res/drawable-xxhdpi等任一文件夹下。然后在layout的xml文件夹下使用android:background"drawable/背景图名"&#xff0c;其中背景图必须是p…

566. 重塑矩阵

在 MATLAB 中&#xff0c;有一个非常有用的函数 reshape &#xff0c;它可以将一个 m x n 矩阵重塑为另一个大小不同&#xff08;r x c&#xff09;的新矩阵&#xff0c;但保留其原始数据。 给你一个由二维数组 mat 表示的 m x n 矩阵&#xff0c;以及两个正整数 r 和 c &…

RabbitMQ播放模块! 构架

RabbitMQ提供了具有可预测且一致的吞吐量和延迟的高可用性&#xff0c;可伸缩和便携式消息系统。 RabbitMQ是AMQP &#xff08;业务消息传递的开放标准&#xff09;的领先实现 &#xff0c;并且通过适配器支持XMPP&#xff0c;SMTP&#xff0c;STOMP和HTTP来进行轻量级Web消息传…

Cyclic Nacklace - HDU 3746(next求循环节)

题目大意&#xff1a;给你一些串&#xff0c;问如果想让这个串里面的循环节至少循环两次&#xff0c;需要添加几个字符&#xff08;只能在最前面或者最后面添加&#xff09;。比如ababc 需要添加5个就是添加ababc。 分析&#xff1a;其实字符串的长度len-next[len] 最小循环节…