最近在做基于ffmpeg的数字电视信号直播推流的工作。在参考大神的博客文章后,http://blog.csdn.net/leixiaohua1020/article/details/39803457,中间碰到点问题,总结一下。
1.推送内存中的视频数据
要完成的工作是arm从dvb_ip网关FPGA寄存器中读取数字电视ts流,进行直播推流。所以要从内存地址中读取输入的ts流,采取回调函数进行,相关工作基本参考大神文章 http://blog.csdn.net/leixiaohua1020/article/details/12980423,关键要在avformat_open_input()之前初始化一个AVIOContext,而且将原本的AVFormatContext的指针pb(AVIOContext类型)指向这个自行初始化AVIOContext。
- unsigned char * iobuffer=(unsigned char *)av_malloc(32768);
- AVIOContext *avio =avio_alloc_context(iobuffer, 32768,0,NULL,fill_iobuffer,NULL,NULL);
- ic->pb=avio;
- err = avformat_open_input(&ic, "nothing", NULL, NULL);
而主要在于avio_alloc_context回调函数。
2
3
4
5
6
7
8
9
|
AVIOContext *avio_alloc_context(
unsigned
char
*buffer,
int
buffer_size,
int
write_flag,
void
*opaque,
int
(*read_packet)(
void
*opaque, uint8_t *buf,
int
buf_size),
int
(*write_packet)(
void
*opaque, uint8_t *buf,
int
buf_size),
int64_t (*seek)(
void
*opaque, int64_t offset,
int
whence));
|
extern int fill_iobuffer(void *opaque, unsigned char *buf, int buf_size)
{
int totSize = 0;
int i;
unsigned short *len_addr,*data_addr;
len_addr = (unsigned short*)(*(unsigned int*)opaque);
data_addr = (unsigned short*)(*((unsigned int*)opaque+1));
while(totSize < buf_size)
{
unsigned short len = *len_addr;
for (i = 0; i < len & (totSize < buf_size); i++)
{
unsigned short data = *data_addr;
buf[totSize++] = (unsigned char)(data >> 8);
buf[totSize++] = (unsigned char)(data);
}
}
//printf("the size is%d\n",totSize);
return totSize;
}
将buf_size大小的数据读到buf中,并返回buf大小,buf大小参照VLC设置为32k,自己可以设置。(读取过程中,出现一级错误,可能FPGA寄存器和读取速度不匹配导致,或者GPMC被占用,速率不足导致,具体原因后续分析)。
2.推流程序
基本与参考文章中一致。主要修改了2个部分。
(1)去除了流控延时部分
由于是实时流,就取消了延时部分。并且由于GPMC速率登问题,采集的原始stream会有一级错误,在做延时处理的时候,会报错而无法推流成功。
(2)屏蔽dts>pts的错误
在计算dts的时候,开始dts的计算简单地等于pts,由于B帧的存在,dts是小于pts的。稍作改进,下一帧的dts是上一帧的pts就可以了。
程序在调试的时候会出现这样的错误,application provided invalid,non monotonically increasing dts to muxer in stream 0
网上有说音视频的duration不同而导致的,有说出现的包导致dts>pts。错误处理程序compute_pkt_fields2(),函数的定义位于libavformat\mux.c,错误处理部分如下所示。
从中可以看到,ffmpeg在检查dts、pts合理性时,若出现dts>pts的情况,会报错,停止推流。简单的处理,就是规避合理性检查。从源头中解决出现这种错误,还需再做研究。