最简单ffmpeg解码
2015-06-19 11:59
357 查看
1、概述
用ffmpeg解码的例子,把解码数据保存为yuv文件。2、代码
/* *最简单的FFmpeg的解码器 * *缪国凯 Mickel *821486004@qq.com * *本程序实现了视频解码保存为yuv,写yuv是用文件方式写的,没用到muxer */ #include "stdafx.h" #ifdef __cplusplus extern "C" { #endif #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" //#include "libavfilter/avfiltergraph.h" //#include "libavfilter/avcodec.h" //#include "libavfilter/buffersink.h" //#include "libavfilter/buffersrc.h" //#include "libavutil/avutil.h" //#include "libavutil/opt.h" //#include "libavutil/pixdesc.h" #pragma comment(lib, "avcodec.lib") #pragma comment(lib, "avformat.lib") #pragma comment(lib, "avutil.lib") //#pragma comment(lib, "avdevice.lib") //#pragma comment(lib, "avfilter.lib") //#pragma comment(lib, "postproc.lib") //#pragma comment(lib, "swresample.lib") //#pragma comment(lib, "swscale.lib") #ifdef __cplusplus }; #endif ***FormatContext *ifmt_ctx = NULL; #include <stdio.h> int openinputfile(const char* filename) { int ret = 0; //open the input if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) { printf("can not open input"); return ret; } if ((ret = avformat_find_stream_info(ifmt_ctx, NULL))) { printf("can not find input stream info"); return ret; } //open the decoder for (int i = 0; i < ifmt_ctx->nb_streams; i++) { if (ifmt_ctx->streams[i]->codec->codec_type == ***MEDIA_TYPE_VIDEO) { ret = avcodec_open2(ifmt_ctx->streams[i]->codec, avcodec_find_decoder(ifmt_ctx->streams[i]->codec->codec_id), NULL); if (ret < 0) { printf("can not open decoder"); return ret; } } } return 0; } int _tmain(int argc, _TCHAR* argv[]) { if (argc < 2) { return -1; } ***Packet pkt_in, pkt_out; ***Frame *frame = NULL; unsigned int stream_index; av_register_all(); if (openinputfile(argv[1]) < 0) { printf("failed to open input file"); goto end; } FILE *p = NULL; p = fopen("test.yuv", "a+b"); while(1) { if (av_read_frame(ifmt_ctx, &pkt_in) < 0) { break; } pkt_out.data = NULL; pkt_out.size = 0; av_init_packet(&pkt_out); stream_index = pkt_in.stream_index; frame = av_frame_alloc(); int got_frame = -1; int ret = -1; if (ifmt_ctx->streams[stream_index]->codec->codec_type == ***MEDIA_TYPE_VIDEO) { ret = avcodec_decode_video2(ifmt_ctx->streams[stream_index]->codec, frame, &got_frame, &pkt_in); if (ret < 0) { av_frame_free(&frame); printf("decoding video stream failed\n"); break; } if (got_frame) { if (NULL != p) { int j; int height = ifmt_ctx->streams[stream_index]->codec->height; int width = ifmt_ctx->streams[stream_index]->codec->width; for(j=0; j<height; j++) fwrite(frame->data[0] + j * frame->linesize[0], 1, width, p); for(j=0; j<height/2; j++) fwrite(frame->data[1] + j * frame->linesize[1], 1, width/2, p); for(j=0; j<height/2; j++) fwrite(frame->data[2] + j * frame->linesize[2], 1, width/2, p); } } } } fclose(p); end: avformat_close_input(&ifmt_ctx); getchar(); return 0; }
3、工程下载地址
http://download.csdn.net/detail/dancing_night/8822057相关文章推荐
- java基础入门-iterator迭代器与集合下标的使用
- java基础入门-iterator迭代器与集合下标的使用
- .NET对象判等归纳与总结
- Flask-babel简单实例(修正原文部分错误版)
- hdu 1057 (simulation, use sentinel to avoid boudary testing, use swap trick to avoid extra copy.)
- 快速关断开关(用三极管来做开关)
- 将博客搬至CSDN
- php session 阻塞 过期不自动清除session文件
- poj2924---高斯求和
- Struts2 Interview Questions and Answers
- LeetCode之“排序”:Largest Number
- (转载)copy,对象自定义copy 什么是property
- 15. C#数据结构与算法 -- 栈
- VS 远程调试
- actionbar--那些需要注意的事
- iOS如何判断应用是否开启摄像头权限
- LeetCode——Balanced Binary Tree
- C++文件读写(转载)
- 代码之髓-当你不知道该学习什么时
- linux 命令——52 ifconfig(转)