首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >libx264 x264_nal_encode溢出

libx264 x264_nal_encode溢出
EN

Stack Overflow用户
提问于 2014-05-09 02:51:10
回答 1查看 442关注 0票数 0

我的libH264应用程序正在对传入的摄像头数据进行编码,但它会不断积累数据,并且执行的编码操作越多,速度就会越慢。你知道为什么会发生这种事吗?

变量是在程序开始时分配的。我的序列是错误的吗?我怀疑我必须重新初始化一些结构,但我找不到怎么做……

以下是我在相机线程中的部分代码:

代码语言:javascript
运行
复制
    int nWidth = 640;
    int nHeight = 480;    

填充YUV数据

代码语言:javascript
运行
复制
    memcpy(p264Pic->img.plane[0],pic.buffer,nWidth*nHeight);  
    memcpy(p264Pic->img.plane[1],pic.buffer+nWidth*nHeight,nWidth*nHeight/4);  
    memcpy(p264Pic->img.plane[2],pic.buffer+nWidth*nHeight*5/4,nWidth*nHeight/4); 

填充PTS

代码语言:javascript
运行
复制
    p264Pic->i_pts = high_resolution_timer_tick_count();

//          if( nFramsInPack % 8 == 0){
//           p264Pic->i_type = X264_TYPE_I;
//          }else{
             p264Pic->i_type = X264_TYPE_AUTO;
//      }

    if( x264_encoder_encode( p264Handle, &p264Nal, &i264Nal, p264Pic ,&pic_out) < 0 ) {  
                fprintf( stderr, "x264_encoder_encode failed/n" );  
    }  
    printf("i264Nal %d\n", i264Nal );

    for( int i = 0; i < i264Nal; i++ )  
    {
     printf( "nal pass %d\n", i );
     int i_data = 1024*32;  

     printf( "nal encode\n" );

     x264_nal_encode( p264Handle, pNal, &p264Nal[i] );

     printf( "after nal encode\n" );

     int i_size = p264Nal[i].i_payload;     

     printf("i_size %d\n", i_size );

     if( i_size > 0 ) {  

       if ((pNal[4]&0x60)==0) {  
           continue;  
       }  
       if (pNal[4]==0x67) {  
           continue;  
       }  
       if (pNal[4]==0x68) {  
           continue;  
       }  
       memmove(pNal,pNal+4,i_size-4);  
       pNal+=i_size-4;  
     }  
     else if( i_size < 0 ) {  
       fprintf( stderr,"need to increase buffer size (size=%d)/n", -i_size );  
     }  
    }  

将数据打包为RTMP

代码语言:javascript
运行
复制
    unsigned int nSize=pNal-szNalBuffer;  
    packet.m_nBodySize=nSize+9;  
    if (i264Nal>1) {  
     szBodyBuffer[ 0]=0x17;  
    }  
    else {  
     szBodyBuffer[ 0]=0x27;  
    }  
    put_be32((char *)szBodyBuffer+5,nSize);  
    memcpy(szBodyBuffer+9,szNalBuffer,pNal-szNalBuffer);  

    printf( "rtmp send packet\n" );

    RTMP_SendPacket(rtmp,&packet,0);  
    newTick=high_resolution_timer_tick_count();  
    packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;  
    packet.m_nTimeStamp+=newTick-oldTick;  
    oldTick=newTick;  

以下是我的应用程序的输出:

代码语言:javascript
运行
复制
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 0
rtmp send packet
i264Nal 4
nal pass 0
nal encode
after nal encode
i_size 36
nal pass 1
nal encode
after nal encode
i_size 15
nal pass 2
nal encode
after nal encode
i_size 632
nal pass 3
nal encode
after nal encode
i_size 7151
rtmp send packet
i264Nal 1
nal pass 0
nal encode
after nal encode
i_size 8136
rtmp send packet
i264Nal 1
nal pass 0
nal encode
after nal encode
i_size 28955
rtmp send packet
^CThe pthread schedule_do end
^Cthread is end
i264Nal 1
nal pass 0
nal encode
after nal encode
EN

回答 1

Stack Overflow用户

发布于 2014-05-09 03:19:58

x264_encoder_encode(...)返回帧大小,您似乎没有考虑到这一点。您使用的是i_payload,但根据NAL类型的不同,这不是正确的值。

x264_nal_encode看起来像是被移除了,x264_encoder_encode现在返回编码到nal单元中的帧。返回int frame_size

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23550188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档