本文主要分析通过FFMPEG函数将音视频容器格式转换成另一种格式。在转换中涉及到h264_mp4toannexb的转换,本文会针对此种情况重点说明下,重点是看ffmpeg_av_remuxer.c。
本文在将MP4文件转为avi格式文件时出现以下错误:
图1-1
出现此问题的原因 log也提示了,在bitstream中未找到startcode,建议我们通过添加h264_mp4toannexb filter来修正此问题。
那我们接下来解释下为什么会出现此问题:
这个主要是MP4中的h264编码有两种封装:
1、annexb模式,传统模式,有startcode,SPS和PPS是在ES中(即基本码流,包含音频、视频或数据的连续码流)。
2、MP4模式,一般mp4、mkv、avi会没有startcode,SPS和PPS以及其它信息被封装在container中,每一个frame前面是这个frame
的长度。
很多解码器只支持annexb这种模式,所以针对mp4模式需要做转换,ffmpeg中可通过h264_mp4toannexb_filter进行转换。
网上一般使用的转换方式代码如下:
这样转换是有问题的。在转换过程中还是会提示上面的问题。后经过查阅FFMPEG源码,对此进行了修改之后,就可以进行正常的转码,并正常的播放。修改代码及代码的注释可参考下面的源代码。
#ifndef _FFMPEG_AV_REMUXER_H_
#define _FFMPEG_AV_REMUXER_H_
#include
#include
#endif
#ifndef _IO_FILE_H_
#define _IO_FILE_H_
typedef struct _IOFile
{
const char *inputName;
const char *outputName;
}IOFile;
#endif
#ifndef _COMMON_H_
#define _COMMON_H_
#include "ffmpeg_av_remuxer.h"
#include "IOFile.h"
#endif
#include "common.h"
typedef int bool;
#define false 0
#define true 1
static bool hello(int argc, char **argv, IOFile *io_param)
{
if ( argc != 3 )
{
printf("Error:commond line error.\n");
return false;
}
io_param->inputName = argv[1];
io_param->outputName = argv[2];
return true;
}
int main(int argc, char **argv)
{
IOFile io_param;
if ( !hello(argc,argv,&io_param) )
{
return -1;
}
AVOutputFormat *ofmt = NULL;
AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
AVBitStreamFilterContext *vbsf = NULL;
AVBitStreamFilterContext *vvbsf = NULL;
AVPacket pkt = {0};
int ret = 0;
int i;
int a;
int video_index;
/*注册编解码器相关信息*/
av_register_all();
/*按封装格式打开输入视频*/
if ( (ret = avformat_open_input(&ifmt_ctx, io_param.inputName,NULL,NULL)) < 0 )
{
printf("Error: open input file failed.\n");
goto FF_END;
}
/*获取输入视频文件中的流信息*/
if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0)
{
printf("Error: Failed to retrieve input stream information.\n");
goto FF_END;
}
av_dump_format(ifmt_ctx, 0, io_param.inputName, 0);
/*按照文件名获取输出文件的句柄*/
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, io_param.outputName);
if (!ofmt_ctx)
{
printf("Error:Could not create output context\n");
goto FF_END;
}
ofmt = ofmt_ctx->oformat;
/*在我们获取到了输入文件中的流信息后,保持输入流中的codec不变,并以其为依据添加到输出文件中*/
for ( i = 0 ; i < ifmt_ctx->nb_streams; i++ )
{
AVStream *inStream = ifmt_ctx->streams[i];
AVStream *outStream = avformat_new_stream(ofmt_ctx, inStream->codec->codec);
if ( !outStream )
{
printf("Error: Could not allocate output stream.\n");
goto FF_END;
}
if (inStream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
video_index = i;
}
/*将inStream中的AVCodecContext内容拷贝一份到outStream中*/
ret = avcodec_copy_context(outStream->codec, inStream->codec);
outStream->codec->codec_tag = 0;
if ( ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
{
outStream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
/*mp4 to annexb*/
vbsf = av_bitstream_filter_init("h264_mp4toannexb");
if(vbsf == NULL)
{
printf("av_bitstream_filter fail\n");
exit(1);
}
av_dump_format(ofmt_ctx, 0, io_param.outputName, 1);
if (!(ofmt->flags & AVFMT_NOFILE))
{
ret = avio_open(&ofmt_ctx->pb, io_param.outputName, AVIO_FLAG_WRITE);
if ( ret < 0 )
{
printf("Error:Could not open output file.\n");
goto FF_END;
}
}
/*开始写入文件头信息*/
ret = avformat_write_header(ofmt_ctx, NULL);
if ( ret < 0 )
{
printf("Error: Could not write file header.\n");
goto FF_END;
}
while(1)
{
AVStream *in_stream = NULL, *out_stream = NULL;
ret = av_read_frame(ifmt_ctx, &pkt);
if ( ret < 0 )
{
printf("Error: read pkt from ifmt_ctx fail ret:%d\n",ret);
break;
}
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
/*将时间戳参考时间单位从inputfile stream转换到outputfile stream*/
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1; /*流中字节位置,不知道的话就写-1*/
printf("stream_index:%d\n",pkt.stream_index);
//if(pkt.stream_index == 1)
if(pkt.stream_index == video_index)
{
printf("vbsf:%p\n",vbsf);
while(vbsf)
{
AVPacket new_pkt=pkt;
a = av_bitstream_filter_filter(vbsf,out_stream->codec,NULL,&new_pkt.data,&new_pkt.size,pkt.data,pkt.size,pkt.flags&AV_PKT_FLAG_KEY);
if(a==0&&new_pkt.data!=pkt.data)
{
unsigned char *t=av_malloc(new_pkt.size+AV_INPUT_BUFFER_PADDING_SIZE);
if(t)
{
memcpy(t,new_pkt.data,new_pkt.size);
memset(t+new_pkt.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
new_pkt.data = t;
new_pkt.buf = NULL;
a = 1;
}
else
{
printf("av malloc t fail\n");
exit(1);
}
}
if(a > 0)
{
new_pkt.buf = av_buffer_create(new_pkt.data,new_pkt.size,av_buffer_default_free,NULL,0);
if(new_pkt.buf)
{
pkt.side_data = NULL;
pkt.side_data_elems = 0;
av_packet_unref(&pkt);
}
else
{
av_freep(&new_pkt.data);
printf("new pkt buf is null\n");
exit(1);
}
}
if(a < 0)
{
printf("failted to open bitstream filter\n");
ret = a;
break;
}
pkt = new_pkt;
vbsf = vbsf->next;
}
}
#if 0
if(pkt.stream_index == 1)
{
AVPacket fpkt = pkt;
a = av_bitstream_filter_filter(vbsf, out_stream->codec, NULL, &fpkt.data, &fpkt.size, pkt.data,pkt.size,pkt.flags&AV_PKT_FLAG_KEY);
fpkt.buf=av_buffer_create(fpkt.data,fpkt.size,av_buffer_default_free,NULL, 0);
if(!fpkt.buf)
{
printf("fpkt.buf is NULL\n");
exit(1);
}
pkt.data = fpkt.data;
pkt.size = fpkt.size;
}
#endif
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if ( ret < 0 )
{
printf("Error: muxing packet.\n");
break;
}
/*av_packet_unref替换av_free_packet使用,因为av_free_packet有可能存在内存泄露的问题*/
av_packet_unref(&pkt);
//av_free_packet(&pkt);
}
av_write_trailer(ofmt_ctx);
FF_END:
/*资源释放*/
avformat_close_input(&ifmt_ctx);
if ( ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE) )
{
avio_closep(&ofmt_ctx->pb);
}
avformat_free_context(ofmt_ctx);
av_bitstream_filter_close(vbsf);
vbsf=NULL;
if ( ret < 0 && ret != AVERROR_EOF )
{
printf("Error: failed to write packet to output file.\n");
return 1;
}
return 0;
}