/**
* 最简单的基于FFmpeg的内存读写例子(内存转码器)
* Simplest FFmpeg mem Transcoder
*
* 雷霄骅,张晖
* leixiaohua1020@126.com
* 中国传媒大学/数字电视技术
* Communication University of China / Digital TV Technology
* http://blog.csdn.net/leixiaohua1020
*
* 本程序实现了任意格式视频数据(例如MPEG2)转码为H.264码流数据。
* 本程序并不是对文件进行处理,而是对内存中的视频数据进行处理。
* 它从内存读取数据,并且将转码后的数据输出到内存中。
* 是最简单的使用FFmpeg读写内存的例子。
*
* This software convert video bitstream (Such as MPEG2) to H.264
* bitstream. It read video bitstream from memory (not from a file),
* convert it to H.264 bitstream, and finally output to another memory.
* It's the simplest example to use FFmpeg to read (or write) from
* memory.
*
*/
#include <stdio.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
};
FILE *fp_open;
FILE *fp_write;
//Read File
int read_buffer(void *opaque, uint8_t *buf, int buf_size){
if(!feof(fp_open)){
int true_size=fread(buf,1,buf_size,fp_open);
return true_size;
}else{
return -1;
}
}
//Write File
int write_buffer(void *opaque, uint8_t *buf, int buf_size){
if(!feof(fp_write)){
int true_size=fwrite(buf,1,buf_size,fp_write);
return true_size;
}else{
return -1;
}
}
int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index)
{
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
CODEC_CAP_DELAY))
return 0;
while (1) {
av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
//ret = encode_write_frame(NULL, stream_index, &got_frame);
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
NULL, &got_frame);
av_frame_free(NULL);
if (ret < 0)
break;
if (!got_frame)
{ret=0;break;}
/* prepare packet for muxing */
enc_pkt.stream_index = stream_index;
enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts,
fmt_ctx->streams[stream_index]->codec->time_base,
fmt_ctx->streams[stream_index]->time_base,
(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts,
fmt_ctx->streams[stream_index]->codec->time_base,
fmt_ctx->streams[stream_index]->time_base,
(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
enc_pkt.duration = av_rescale_q(enc_pkt.duration,
fmt_ctx->streams[stream_index]->codec->time_base,
fmt_ctx->streams[stream_index]->time_base);
av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
/* mux encoded frame */
ret = av_write_frame(fmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
return ret;
}
int main(int argc, char* argv[])
{
int ret;
AVFormatContext* ifmt_ctx=NULL;
AVFormatContext* ofmt_ctx=NULL;
AVPacket packet,enc_pkt;
AVFrame *frame = NULL;
enum AVMediaType type;
unsigned int stream_index;
unsigned int i=0;
int got_frame,enc_got_frame;
AVStream *out_stream;
AVStream *in_stream;
AVCodecContext *dec_ctx, *enc_ctx;
AVCodec *encoder;
fp_open = fopen("cuc60anniversary_start.ts", "rb"); //视频源文件
fp_write=fopen("cuc60anniversary_start.h264","wb+"); //输出文件
av_register_all();
ifmt_ctx=avformat_alloc_context();
avformat_alloc_output_context2(&ofmt_ctx, NULL, "h264", NULL);
unsigned char* inbuffer=NULL;
unsigned char* outbuffer=NULL;
inbuffer=(unsigned char*)av_malloc(32768);
outbuffer=(unsigned char*)av_malloc(32768);
/*open input file*/
AVIOContext *avio_in =avio_alloc_context(inbuffer, 32768,0,NULL,read_buffer,NULL,NULL);
if(avio_in==NULL)
goto end;
ifmt_ctx->pb=avio_in;
ifmt_ctx->flags=AVFMT_FLAG_CUSTOM_IO;
if ((ret = avformat_open_input(&ifmt_ctx, "whatever", NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
AVStream *stream;
AVCodecContext *codec_ctx;
stream = ifmt_ctx->streams[i];
codec_ctx = stream->codec;
/* Reencode video & audio and remux subtitles etc. */
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO){
/* Open decoder */
ret = avcodec_open2(codec_ctx,
avcodec_find_decoder(codec_ctx->codec_id), NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
return ret;
}
}
}
//av_dump_format(ifmt_ctx, 0, "whatever", 0);
/*open output file*/
AVIOContext *avio_out =avio_alloc_context(outbuffer, 32768,0,NULL,NULL,write_buffer,NULL);
if(avio_out==NULL)
goto end;
//avio_out->write_packet=write_packet;
ofmt_ctx->pb=avio_out;
ofmt_ctx->flags=AVFMT_FLAG_CUSTOM_IO;
for (i = 0; i < 1; i++) {
out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream) {
av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
return AVERROR_UNKNOWN;
}
in_stream = ifmt_ctx->streams[i];
dec_ctx = in_stream->codec;
enc_ctx = out_stream->codec;
if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
{
encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
enc_ctx->height = dec_ctx->height;
enc_ctx->width = dec_ctx->width;
enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
enc_ctx->pix_fmt = encoder->pix_fmts[0];
enc_ctx->time_base = dec_ctx->time_base;
//enc_ctx->time_base.num = 1;
//enc_ctx->time_base.den = 25;
//H264的必备选项,没有就会错
enc_ctx->me_range=16;
enc_ctx->max_qdiff = 4;
enc_ctx->qmin = 10;
enc_ctx->qmax = 51;
enc_ctx->qcompress = 0.6;
enc_ctx->refs=3;
enc_ctx->bit_rate = 500000;
ret = avcodec_open2(enc_ctx, encoder, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
return ret;
}
}
else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
return AVERROR_INVALIDDATA;
} else {
/* if this stream must be remuxed */
ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
ifmt_ctx->streams[i]->codec);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Copying stream context failed\n");
return ret;
}
}
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
//av_dump_format(ofmt_ctx, 0, "whatever", 1);
/* init muxer, write output file header */
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
return ret;
}
i=0;
/* read all packets */
while (1) {
i++;
if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
break;
stream_index = packet.stream_index;
if(stream_index!=0)
continue;
type = ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
stream_index);
av_log(NULL, AV_LOG_DEBUG, "Going to reencode the frame\n");
frame = av_frame_alloc();
if (!frame) {
ret = AVERROR(ENOMEM);
break;
}
packet.dts = av_rescale_q_rnd(packet.dts,
ifmt_ctx->streams[stream_index]->time_base,
ifmt_ctx->streams[stream_index]->codec->time_base,
(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
packet.pts = av_rescale_q_rnd(packet.pts,
ifmt_ctx->streams[stream_index]->time_base,
ifmt_ctx->streams[stream_index]->codec->time_base,
(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
ret = avcodec_decode_video2(ifmt
没有合适的资源?快使用搜索试试~ 我知道了~
最简单的基于FFmpeg的内存读写的例子
共268个文件
h:215个
lib:18个
dll:17个
5星 · 超过95%的资源 需积分: 0 731 下载量 80 浏览量
2014-10-05
12:13:11
上传
评论 5
收藏 25.44MB 7Z 举报
温馨提示
本程序实现了FFmpeg对内存中的视频数据的读写。包含两个工程: simplest_ffmpeg_mem_player:播放内存中视频数据的播放器。 simplest_ffmpeg_mem_transcoder:转码内存中数据的转码器。
资源推荐
资源详情
资源评论
收起资源包目录
最简单的基于FFmpeg的内存读写的例子 (268个子文件)
simplest_ffmpeg_mem_transcoder.cpp 10KB
simplest_ffmpeg_mem_player.cpp 5KB
avcodec-55.dll 18.06MB
avcodec-55.dll 18.06MB
avformat-55.dll 5.1MB
avformat-55.dll 5.1MB
avfilter-4.dll 1.94MB
avfilter-4.dll 1.94MB
avdevice-55.dll 1.28MB
avdevice-55.dll 1.28MB
swscale-2.dll 424KB
swscale-2.dll 424KB
avutil-52.dll 409KB
avutil-52.dll 409KB
SDL.dll 297KB
swresample-0.dll 270KB
swresample-0.dll 270KB
postproc-52.dll 119KB
postproc-52.dll 119KB
simplest_ffmpeg_mem_transcoder.vcxproj.filters 1KB
simplest_ffmpeg_mem_player.vcxproj.filters 1KB
.gitignore 46B
.gitignore 18B
.gitignore 18B
SDL_opengl.h 329KB
avcodec.h 171KB
avcodec.h 171KB
avformat.h 97KB
avformat.h 97KB
avfilter.h 56KB
avfilter.h 56KB
SDL_video.h 37KB
opt.h 34KB
opt.h 34KB
pixfmt.h 28KB
pixfmt.h 28KB
frame.h 22KB
frame.h 22KB
avio.h 18KB
avio.h 18KB
intreadwrite.h 18KB
intreadwrite.h 18KB
avdevice.h 16KB
avdevice.h 16KB
SDL_stdinc.h 16KB
old_pix_fmts.h 14KB
old_pix_fmts.h 14KB
mem.h 14KB
mem.h 14KB
common.h 14KB
common.h 14KB
pixdesc.h 13KB
pixdesc.h 13KB
SDL_events.h 13KB
avstring.h 12KB
avstring.h 12KB
swscale.h 12KB
swscale.h 12KB
swresample.h 12KB
swresample.h 12KB
SDL_audio.h 11KB
old_codec_ids.h 10KB
old_codec_ids.h 10KB
samplefmt.h 10KB
samplefmt.h 10KB
log.h 10KB
log.h 10KB
buffer.h 10KB
buffer.h 10KB
channel_layout.h 9KB
channel_layout.h 9KB
avutil.h 8KB
avutil.h 8KB
_mingw.h 8KB
_mingw.h 8KB
imgutils.h 8KB
imgutils.h 8KB
bprint.h 8KB
bprint.h 8KB
buffersink.h 7KB
buffersink.h 7KB
SDL_keysym.h 7KB
parseutils.h 7KB
parseutils.h 7KB
dict.h 6KB
dict.h 6KB
SDL_syswm.h 6KB
vdpau.h 6KB
vdpau.h 6KB
xvmc.h 6KB
xvmc.h 6KB
SDL_endian.h 6KB
SDL_cdrom.h 6KB
stdint.h 6KB
stdint.h 6KB
SDL_mutex.h 6KB
inttypes.h 6KB
inttypes.h 6KB
version.h 5KB
version.h 5KB
共 268 条
- 1
- 2
- 3
雷霄骅
- 粉丝: 4w+
- 资源: 141
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Python实现的VisionTransformer架构设计与源码学习
- 基于Java语言的Arduino开源电子原型平台设计源码
- 基于Java语言的PetShop电商平台设计源码
- 基于Java语言的大学生社团管理系统Server端设计源码
- 基于Java语言的Zzyl-Together合作智慧养老项目设计源码
- 基于Thinkphp5框架的Java插件设计源码
- 基于Python、JavaScript和Vue的“大道无形,生育天地”主题网站设计源码
- 基于Netty4与Spring、MyBatis等流行框架的轻量级RESTful HTTP服务器设计源码
- 基于Jupyter Notebook的Python与Shell脚本分享设计源码
- 基于Java的Android平台Ecg绘图设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页