资源简介
基于ffmpeg windows下编译 H264文件转YUV格式 加详细注释 适合新手学习使用 需要的下
代码片段和文件信息
// H264_yuv.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include
#include
#include
#include
#ifdef __cplusplus
extern “C“
{
#endif
#include “libavutil/imgutils.h“
#include “libavutil/opt.h“
#include “libavcodec/avcodec.h“
#include “libavutil/mathematics.h“
#include “libavutil/samplefmt.h“
#ifdef __cplusplus
#pragma comment(lib “avcodec.lib“)
#pragma comment(lib “avutil.lib“)
}
#endif
#define INBUF_SIZE (4096)
static void pgm_save(unsigned char *buf int wrap int xsize int ysize FILE *f)
{
int i;
//buf += 64;
//fprintf(f“P5\n%d %d\n%d\n“xsizeysize255);
for(i=0;i fwrite(buf + i * wrap1xsizef);
}
static int _find_head(unsigned char *buffer int len)
{
int i; int j;
for (i=512;i {
if (buffer[i] == 0 && buffer[i+1] == 0 && buffer[i+2] == 0 && buffer[i+3] == 1)
break;
}
if (i == len)
return 0;
if (i == 512)
return 0;
return i;
}
#define FILE_READING_BUFFER (1*1024*1024)
static void build_avpkt(AVPacket *avpkt FILE *fp)
{
#if 0
int len;
static unsigned char buffer[INBUF_SIZE];
len = fread(buffer 1 INBUF_SIZE fp);
avpkt->data = buffer;
avpkt->size = len;
#else
static unsigned char buffer[1*1024*1024];
static int readptr = 0;
static int writeptr = 0;
int lentoread;
int nexthead;
if (writeptr - readptr < 200 * 1024)
{
memmove(buffer &buffer[readptr] writeptr - readptr);
writeptr -= readptr;
readptr = 0;
toread = FILE_READING_BUFFER - writeptr;
len = fread(&buffer[writeptr] 1 toread fp);
writeptr += len;
}
nexthead = _find_head(&buffer[readptr] writeptr-readptr);
if (nexthead == 0)
{
printf(“failed find next head...\n“);
nexthead = writeptr - readptr;
}
avpkt->size = nexthead;
avpkt->data = &buffer[readptr];
readptr += nexthead;
#endif
}
static void video_decode_example(const char *outfilename const char *filename)
{
AVCodec *codec;
AVCodecContext *c= NULL;
int frame got_picture len;
FILE *f *fout;
AVframe *picture;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
char buf[1024];
AVPacket avpkt;
//AVDictionary *opts;
av_init_packet(&avpkt); //使用默认值初始化avpacket
/* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
memset(inbuf + INBUF_SIZE 0 FF_INPUT_BUFFER_PADDING_SIZE);
printf(“Video decoding\n“);
//opts = NULL;
/* find the H264 video decoder */
codec = avcodec_find_decoder(CODEC_ID_H264); //查找对应解码器
if (!codec) {
fprintf(stderr “codec not found\n“);
exit(1);
}
c = avcodec_alloc_context3(codec); //分配编码解码器上下文
if (!c) {
fprintf(stderr “Could not allocate video codec c
- 上一篇:MFC深入浅出带目录完整版
- 下一篇:超市进销存管理系统——以及使用说明书
评论
共有 条评论