1,獲取所有的組件
av_register_all();
2.獲取上下文
AVFormatContext *pContext=avformat_alloc_context();
把pContext 賦值
if(avformat_open_input(&pContext, inputStr, NULL, NULL)<0) {
LOGE("ERROR_CODE %d inputStr %s",avformat_open_input(&pContext, inputStr, NULL, NULL),inputStr)
LOGE("打開失敗 ");
return;
}
2.獲取到解碼器上下文
int vedio_stream_idx=-1;
// 找到視頻流
for (int i = 0; i < pContext->nb_streams; ++i) {
LOGE("循環(huán) %d", i);
// codec 每一個(gè)流 對(duì)應(yīng)的解碼上下文 codec_type 流的類型
if (pContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
vedio_stream_idx = i;
}
}
// 獲取到解碼器上下文
AVCodecContext* pCodecCtx=pContext->streams[vedio_stream_idx]->codec;
3.讀數(shù)據(jù)
av_read_frame(pContext, packet)
注釋:
// 分配內(nèi)存 malloc AVPacket 1 2
AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));
// 初始化結(jié)構(gòu)體
av_init_packet(packet);
4視頻節(jié)封裝 解碼
// 分配內(nèi)存 malloc AVPacket 1 2
AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));
// 初始化結(jié)構(gòu)體
av_init_packet(packet);
//還是不夠
AVFrame *frame = av_frame_alloc();
// 聲明一個(gè)yuvframe
AVFrame *yuvFrame = av_frame_alloc();
// 給yuvframe 的緩沖區(qū) 初始化
uint8_t *out_buffer= (uint8_t *) av_malloc
(avpicture_get_size(AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height));
int re=avpicture_fill((AVPicture *) yuvFrame,
out_buffer, AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height);
LOGE("寬 %d 高 %d",pCodecCtx->width,pCodecCtx->height);
// mp4 的上下文pCodecCtx->pix_fmt
SwsContext *swsContext=sws_getContext(pCodecCtx->width,pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,AV_PIX_FMT_YUV420P
,SWS_BILINEAR,NULL,NULL,NULL
);
int frameCount = 0;
FILE *fp_yuv = fopen(outStr, "wb");
//packet入?yún)?出參對(duì)象 轉(zhuǎn)換上下文
int got_frame;
while (av_read_frame(pContext, packet) >= 0) {
// 節(jié)封裝
// 根據(jù)frame 進(jìn)行原生繪制 bitmap window
avcodec_decode_video2(pCodecCtx, frame, &got_frame, packet);
// frame 的數(shù)據(jù)拿到 視頻像素?cái)?shù)據(jù) yuv 三個(gè)rgb r g b 數(shù)據(jù)量大 三個(gè)通道
LOGE("解碼%d ",frameCount++);
if (got_frame > 0) {
sws_scale(swsContext, (const uint8_t *const *)
frame->data, frame->linesize,
0, frame->height, yuvFrame->data,
yuvFrame->linesize
);
int y_size = pCodecCtx->width * pCodecCtx->height;
// y 亮度信息寫完了
fwrite(yuvFrame->data[0], 1, y_size, fp_yuv);
fwrite(yuvFrame->data[1], 1, y_size/4, fp_yuv);
fwrite(yuvFrame->data[2], 1, y_size/4, fp_yuv);
}
av_free_packet(packet);
}
方法的解釋
- int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);
ps:函數(shù)調(diào)用成功之后處理過的AVFormatContext結(jié)構(gòu)體器罐。
file:打開的視音頻流的URL师郑。
fmt:強(qiáng)制指定AVFormatContext中AVInputFormat的。這個(gè)參數(shù)一般情況下可以設(shè)置為NULL,這樣FFmpeg可以自動(dòng)檢測(cè)AVInputFormat。
dictionay:附加的一些選項(xiàng),一般情況下可以設(shè)置為NULL贸诚。
函數(shù)執(zhí)行成功的話,其返回值大于等于0。
2.AVCodec *avcodec_find_decoder(enum AVCodecID id);
函數(shù)的參數(shù)是一個(gè)解碼器的ID酱固,返回查找到的解碼器(沒有找到就返回NULL)械念。
3.int av_read_frame(AVFormatContext *s, AVPacket *pkt);
av_read_frame()使用方法在注釋中寫得很詳細(xì),用中文簡(jiǎn)單描述一下它的兩個(gè)參數(shù):
s:輸入的AVFormatContext
pkt:輸出的AVPacket
如果返回0則說明讀取正常运悲。
4 .
- @return On error a negative value is returned, otherwise the number of bytes
- used or zero if no frame could be decompressed.
*/
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
int *got_picture_ptr,
const AVPacket *avpkt);
avcodec_decode_video2()的作用是解碼一幀視頻數(shù)據(jù)龄减。輸入一個(gè)壓縮編碼的結(jié)構(gòu)體AVPacket,輸出一個(gè)解碼后的結(jié)構(gòu)體AVFrame
利用ffmpeg進(jìn)行圖像數(shù)據(jù)格式的轉(zhuǎn)換以及圖片的縮放應(yīng)用中班眯,主要用到了swscale.h文件中的三個(gè)函數(shù)希停,分別是:
struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
int dstW, int dstH, enum AVPixelFormat dstFormat,
int flags, SwsFilter *srcFilter,
SwsFilter *dstFilter, const double *param);
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],
const int srcStride[], int srcSliceY, int srcSliceH,
uint8_t *const dst[], const int dstStride[]);
void sws_freeContext(struct SwsContext *swsContext);
sws_getContext函數(shù)可以看做是初始化函數(shù),它的參數(shù)定義分別為:
int srcW署隘,int srcH 為原始圖像數(shù)據(jù)的高和寬宠能;
int dstW,int dstH 為輸出圖像數(shù)據(jù)的高和寬磁餐;
enum AVPixelFormat srcFormat 為輸入和輸出圖片數(shù)據(jù)的類型违崇;eg:AV_PIX_FMT_YUV420、PAV_PIX_FMT_RGB24诊霹;
int flags 為scale算法種類羞延;eg:SWS_BICUBIC、SWS_BICUBLIN脾还、SWS_POINT伴箩、SWS_SINC;
SwsFilter *srcFilter 荠呐,SwsFilter *dstFilter赛蔫,const double *param 可以不用管砂客,全為NULL即可泥张;
sws_scale函數(shù)則為執(zhí)行函數(shù),它的參數(shù)定義分別為:
struct SwsContext *c 為sws_getContext函數(shù)返回的值鞠值;
const uint8_t *const srcSlice[]媚创,uint8_t *const dst[] 為輸入輸出圖像數(shù)據(jù)各顏色通道的buffer指針數(shù)組;
const int srcStride[]彤恶,const int dstStride[] 為輸入輸出圖像數(shù)據(jù)各顏色通道每行存儲(chǔ)的字節(jié)數(shù)數(shù)組钞钙;
int srcSliceY 為從輸入圖像數(shù)據(jù)的第多少列開始逐行掃描,通常設(shè)為0声离;
int srcSliceH 為需要掃描多少行芒炼,通常為輸入圖像數(shù)據(jù)的高度;
sws_freeContext函數(shù)為結(jié)束函數(shù)术徊,它的參數(shù)即為sws_getContext函數(shù)返回的值本刽;
參考雷神的微博
http://blog.csdn.net/leixiaohua1020/article/category/1360795