FFmpeg内存操作(三)内存转码器
相关博客列表 :
FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析
本文代码来自于自雷霄骅的《最简单的基于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>
- #define __STDC_CONSTANT_MACROS
- #include "avcodec.h"
- #include "avformat.h"
- #include "avutil.h"
- #include "opt.h"
- #include "pixdesc.h"
- #include "mathematics.h"
- FILEFILE *fp_open;
- FILEFILE *fp_write;
- //Read File
- int read_buffer(voidvoid *opaque, uint8_t *buf, int buf_size){
- int true_size;
- if(!feof(fp_open)){
- true_size=fread(buf,1,buf_size,fp_open);
- printf("read_buffer buf_size = %d\n",buf_size);
- return true_size;
- }else{
- return -1;
- }
- }
- //Write File
- int write_buffer(voidvoid *opaque, uint8_t *buf, int buf_size){
- int true_size;
- if(!feof(fp_write)){
- true_size=fwrite(buf,1,buf_size,fp_write);
- printf("write_buffer buf_size = %d\n",buf_size);
- 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));
- (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,
- (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
- //(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;
- AVIOContext *avio_in=NULL;
- AVIOContext *avio_out=NULL;
- unsigned char* inbuffer=NULL;
- unsigned char* outbuffer=NULL;
- AVFrame *frame = NULL;
- AVPacket packet;
- AVPacket enc_pkt;
- AVStream *out_stream;
- AVStream *in_stream;
- AVCodecContext *dec_ctx;
- AVCodecContext *enc_ctx;
- AVCodec *encoder;
- AVStream *stream;
- AVCodecContext *codec_ctx;
- enum AVMediaType type;
- unsigned int stream_index;
- unsigned int i=0;
- int got_frame;
- int enc_got_frame;
- fp_open = fopen("cuc60anniversary_start.ts", "rb"); //视频源文件
- fp_write=fopen("cuc60anniversary_start.h264","wb+"); //输出文件
- av_register_all();
- ifmt_ctx=avformat_alloc_context(); /* Allocate an AVFormatContext. */
- avformat_alloc_output_context2(&ofmt_ctx, NULL, "h264", NULL); /* Allocate an AVFormatContext for an output format. */
- inbuffer=(unsigned char*)av_malloc(32768);
- outbuffer=(unsigned char*)av_malloc(32768);
- /*open input file*/
- avio_in =avio_alloc_context(inbuffer, 32768,0,NULL,read_buffer,NULL,NULL);
- if(avio_in==NULL)
- goto end;
- /*open output file*/
- avio_out =avio_alloc_context(outbuffer, 32768,1,NULL,NULL,write_buffer,NULL);
- if(avio_out==NULL)
- goto end;
- ifmt_ctx->pb=avio_in; /* I/O context. input output context */
- ifmt_ctx->flags=AVFMT_FLAG_CUSTOM_IO; /* The caller has supplied a custom AVIOContext, don't avio_close() it */
- 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) { /* Read packets of a media file to get stream information */
- av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
- return ret;
- }
- for (i = 0; i < ifmt_ctx->nb_streams; i++) {
- 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 */
- /* Initialize the AVCodecContext to use the given AVCodec */
- 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);
- //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); /* Add a new stream to a media file. */
- 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 */
- /* Copy the settings of the source AVCodecContext into the destination AVCodecContext */
- 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) /* Return the next frame of a stream */
- 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));
- (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));
- (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
- /* Decode the video frame of size avpkt->size from avpkt->data into picture 解码输入文件 */
- ret = avcodec_decode_video2(ifmt_ctx->streams[stream_index]->codec, frame, &got_frame, &packet);
- printf("Decode 1 Packet\tsize:%d\tpts:%lld\n",packet.size,packet.pts);
- if (ret < 0) {
- av_frame_free(&frame);
- av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
- break;
- }
- if (got_frame) {
- frame->pts = av_frame_get_best_effort_timestamp(frame);
- frame->pict_type=AV_PICTURE_TYPE_NONE;
- /* Initialize optional fields of a packet with default values */
- enc_pkt.data = NULL;
- enc_pkt.size = 0;
- av_init_packet(&enc_pkt);
- /* Takes input raw video data from frame and writes the next output packet, if available, to avpkt */
- ret = avcodec_encode_video2 (ofmt_ctx->streams[stream_index]->codec, &enc_pkt, frame, &enc_got_frame);
- printf("Encode 1 Packet\tsize:%d\tpts:%lld\n",enc_pkt.size,enc_pkt.pts);
- av_frame_free(&frame);
- if (ret < 0)
- goto end;
- if (!enc_got_frame)
- continue;
- /* prepare packet for muxing */
- enc_pkt.stream_index = stream_index;
- enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts,
- ofmt_ctx->streams[stream_index]->codec->time_base,
- ofmt_ctx->streams[stream_index]->time_base,
- //(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
- (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
- enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts,
- ofmt_ctx->streams[stream_index]->codec->time_base,
- ofmt_ctx->streams[stream_index]->time_base,
- //(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
- (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
- enc_pkt.duration = av_rescale_q(enc_pkt.duration,
- ofmt_ctx->streams[stream_index]->codec->time_base,
- ofmt_ctx->streams[stream_index]->time_base);
- av_log(NULL, AV_LOG_INFO, "Muxing frame %d\n",i);
- /* mux encoded frame */
- /* Write a packet to an output media file */
- av_write_frame(ofmt_ctx,&enc_pkt);
- if (ret < 0)
- goto end;
- } else {
- av_frame_free(&frame);
- }
- av_free_packet(&packet);
- }
- /* flush encoders */
- for (i = 0; i < 1; i++) {
- /* flush encoder */
- ret = flush_encoder(ofmt_ctx,i);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
- goto end;
- }
- }
- av_write_trailer(ofmt_ctx);
- end:
- av_freep(avio_in);
- av_freep(avio_out);
- av_free(inbuffer);
- av_free(outbuffer);
- av_free_packet(&packet);
- av_frame_free(&frame);
- avformat_close_input(&ifmt_ctx);
- avformat_free_context(ofmt_ctx);
- fclose(fp_open);
- if (ret < 0)
- av_log(NULL, AV_LOG_ERROR, "Error occurred\n");
- return (ret? 1:0);
- }
- from:http://blog.csdn.net/li_wen01/article/details/64905959
FFmpeg内存操作(三)内存转码器的更多相关文章
- 共享内存操作类(C#源码)
原文 http://blog.csdn.net/yefanqiu/article/details/1717458 VC++的共享内存操作代码实现起来相对比较容易,但是用C#语言来实现,就有一定难度,由 ...
- Linux内存初始化(三) 内存布局
一.前言 同样的,本文是内存初始化文章的一份补充文档,希望能够通过这样的一份文档,细致的展示在初始化阶段,Linux 4.4.6内核如何从device tree中提取信息,完成内存布局的任务.具体的c ...
- 【嵌入式开发】裸机引导操作系统和ARM 内存操作 ( DRAM SRAM 类型 简介 | Logical Bank | 内存地址空间介绍 | 内存芯片连接方式 | 内存初始化 | 汇编代码示例 )
[嵌入式开发]ARM 内存操作 ( DRAM SRAM 类型 简介 | Logical Bank | 内存地址空间介绍 | 内存芯片连接方式 | 内存初始化 | 汇编代码示例 ) 一. 内存 ...
- 最简单的基于FFmpeg的内存读写的例子:内存转码器
===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...
- FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析
相关博客列表 : FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析 FFMPEG内存操作(二)从内存中读取数及数据格式的转换 FFmpeg内存操作(三)内存转码器 在F ...
- FFMPEG内存操作(二)从内存中读取数及数据格式的转换
相关博客列表: FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析 FFMPEG内存操作(二)从内存中读取数及数据格式的转换 FFmpeg内存操作(三)内存转码器 在雷神 ...
- Android For JNI(二)——C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器
Android For JNI(二)--C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器 当我们把Hello World写完之后,我们就可以迈入C的大门了,今天就来讲讲基本的一些数据类型 ...
- ffmpeg转码器移植VC的工程:ffmpeg for MFC
本文介绍一个自己做的FFMPEG移植到VC下的开源工程:ffmpeg for MFC.本工程将ffmpeg工程中的ffmpeg转码器(ffmpeg.c)移植到了VC环境下.并且使用MFC做了一套简单的 ...
- ffmpeg转码器移植VC的project:ffmpeg for MFC
本文介绍一个自己做的FFMPEG移植到VC下的开源project:ffmpeg for MFC.本project将ffmpegproject中的ffmpeg转码器(ffmpeg.c)移植到了VC环境下 ...
随机推荐
- (转)深入理解Java内存模型之系列篇
原文地址: http://blog.csdn.net/ccit0519/article/details/11241403 深入理解Java内存模型(一)——基础 并发编程模型的分类 在并发编程中,我们 ...
- 九度OJ 1192:回文字符串 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3807 解决:1778 题目描述: 给出一个长度不超过1000的字符串,判断它是不是回文(顺读,逆读均相同)的. 输入: 输入包括一行字符串 ...
- AFN errorCode对应的状态码
转 http://blog.csdn.NET/wangyanchang21/article/details/50932191 在很多时候都会遇到错误, 还会带有一些 Error Code , 比如在各 ...
- 【网络与系统安全】20179209 wireshark和nmap实验
TCP三次握手包 在进行实验前,先梳理一遍TCP三次握手的过程,这个图是我本科学网络时画过不少于十遍的图,我觉得非常有纪念意义. 稍作解释,第一次握手,客户端发起请求连接,发送一个标志位为SYN的ip ...
- git本地分支管理
查看分支:git branch创建分支:git branch dev重命名分支:git branch -m dev dev1删除分支:git branch -d dev切换分支:git checkou ...
- Bootstrap学习5--bootstrap中的模态框(modal,弹出层)
bootstrap中的模态框(modal),不同于Tooltips,模态框以弹出对话框的形式出现,具有最小和最实用的功能集. 务必将模态框的 HTML 代码放在文档的最高层级内(也就是说,尽量作为 b ...
- Excel控制IE
---恢复内容开始--- 1.初始化and连接http网页 Set ie = CreateObject("InternetExplorer.Application") ie.Vis ...
- C#调用大漠插件
大漠插件是一个很不错的东西,在按键精灵和易语言里面用得很多,可以后台找图找字,写游戏自动脚本用得特别多.前面写一个微信的自动脚本,查了一些资料,易语言不太熟悉,按键精灵功能上可能不好实现,就找了些资料 ...
- Linux服务器上的tomcat中部署web项目
首先了解一下下面几个概念,讲得不太准确:1.JVMJVM是class以及jar(实际上就是很多个class压缩在一起)的运行环境,特征就是java和javaw命令,通过这两个命令,你可以执行class ...
- 导出数据到表格PHP
导出数据到表格 public function excel(){ $filename = '导出表格'; $header = ['编号','名称']; $index = ['id','name']; ...