-
大小: 12KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2024-01-19
- 語言: C/C++
- 標(biāo)簽: H264轉(zhuǎn)YUV??
資源簡介
基于ffmpeg windows下編譯 H264文件轉(zhuǎn)YUV格式 加詳細(xì)注釋 適合新手學(xué)習(xí)使用 需要的下
代碼片段和文件信息
//?H264_yuv.cpp?:?定義控制臺應(yīng)用程序的入口點(diǎn)。
//
#include?“stdafx.h“
#include?
#include?
#include?
#include??
#ifdef?__cplusplus?
extern?“C“
{?
#endif?
#include?“l(fā)ibavutil/imgutils.h“?
#include?“l(fā)ibavutil/opt.h“?
#include?“l(fā)ibavcodec/avcodec.h“?
#include?“l(fā)ibavutil/mathematics.h“?
#include?“l(fā)ibavutil/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);??//使用默認(rèn)值初始化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);?????//查找對應(yīng)解碼器
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
評論
共有 條評論