Newest 'libx264' Questions - Stack Overflow

http://stackoverflow.com/questions/tagged/libx264

Les articles publiés sur le site

  • ffmpeg libx264 AVCodecContext settings

    21 mai 2014, par integra753

    I am using a recent windows (Jan 2011) ffmpeg build and trying to record video in H264. It is recording fine in MPEG4 using the following settings:

    c->codec_id = CODEC_ID_MPEG4;
    c->codec_type = AVMEDIA_TYPE_VIDEO;
    c->width = VIDEO_WIDTH;
    c->height = VIDEO_HEIGHT;
    c->bit_rate = c->width * c->height * 4;
    c->time_base.den = FRAME_RATE;
    c->time_base.num = 1;
    c->gop_size = 12;
    c->pix_fmt = PIX_FMT_YUV420P;
    

    Simply changing CODEC Id to H264 causes avcodec_open() to fail (-1). I found a list of possible settings Encoding h.264 with libavcodec/x264. I have tried these, without setting pix_fmt, avcodec_open() still fails but if I additionally set c->pix_fmt = PIX_FMT_YUV420P; then I get a divide by zero exception.

    I then came across a few posts on here that say I should set nothing (with exception of code_id, codec_type, width, height and perhaps bit_rate and pix_fmt) as the library now chooses the best settings itself. I have tried various combinations, still avcode_open() fails.

    Does anyone have some advice on what to do or some settings that are current?

    Thanks.

    Here are one set of H264 settings which give the issue I describe:

    static AVStream* AddVideoStream(AVFormatContext *pOutputFmtCtx, 
    int frameWidth, int frameHeight, int fps)
    {
    AVCodecContext* ctx;
    AVStream* stream;
    
    stream = av_new_stream(pOutputFmtCtx, 0);
    if (!stream) 
    {
        return NULL;
    }
    
    ctx = stream->codec;
    
    ctx->codec_id = pOutputFmtCtx->oformat->video_codec; //CODEC_ID_H264
    ctx->codec_type = AVMEDIA_TYPE_VIDEO;
    ctx->width = frameWidth;             //704
    ctx->height = frameHeight;           //576
    ctx->bit_rate = frameWidth * frameHeight * 4;
    
    ctx->coder_type = 1;  // coder = 1
    ctx->flags|=CODEC_FLAG_LOOP_FILTER;   // flags=+loop
    ctx->me_cmp|= 1;  // cmp=+chroma, where CHROMA = 1
    ctx->partitions|=X264_PART_I8X8+X264_PART_I4X4+X264_PART_P8X8+X264_PART_B8X8; // partitions=+parti8x8+parti4x4+partp8x8+partb8x8
    ctx->me_method=ME_HEX;    // me_method=hex
    ctx->me_subpel_quality = 7;   // subq=7
    ctx->me_range = 16;   // me_range=16
    ctx->gop_size = 250;  // g=250
    ctx->keyint_min = 25; // keyint_min=25
    ctx->scenechange_threshold = 40;  // sc_threshold=40
    ctx->i_quant_factor = 0.71; // i_qfactor=0.71
    ctx->b_frame_strategy = 1;  // b_strategy=1
    ctx->qcompress = 0.6; // qcomp=0.6
    ctx->qmin = 10;   // qmin=10
    ctx->qmax = 51;   // qmax=51
    ctx->max_qdiff = 4;   // qdiff=4
    ctx->max_b_frames = 3;    // bf=3
    ctx->refs = 3;    // refs=3
    ctx->directpred = 1;  // directpred=1
    ctx->trellis = 1; // trellis=1
           ctx->flags2|=CODEC_FLAG2_BPYRAMID+CODEC_FLAG2_MIXED_REFS+CODEC_FLAG2_WPRED+CODEC_FLAG2_8X8DCT+CODEC_FLAG2_FASTPSKIP;  // flags2=+bpyramid+mixed_refs+wpred+dct8x8+fastpskip
    ctx->weighted_p_pred = 2; // wpredp=2
    // libx264-main.ffpreset preset
    ctx->flags2|=CODEC_FLAG2_8X8DCT;
    ctx->flags2^=CODEC_FLAG2_8X8DCT;    // flags2=-dct8x8
    
    // if set this get divide by 0 error on avcodec_open()
    // if don't set it get -1 error on avcodec_open()
    //ctx->pix_fmt = PIX_FMT_YUV420P; 
    
    return stream;
    

    }

  • How to use x264 for encoding with ffmpeg ?

    21 mai 2014, par mmmaaak

    I tryed to use ffmpeg for encoding video/ But it fails on initialization of AVCodecContext annd AVCodec. What I do:

    _codec = avcodec_find_encoder(CODEC_ID_H264);
    _codecContext = avcodec_alloc_context3(_codec);
    _codecContext->coder_type = 0;
    _codecContext->me_cmp|= 1;
    _codecContext->me_method=ME_HEX;
    _codecContext->me_subpel_quality = 0;
    _codecContext->me_range = 16;
    _codecContext->gop_size = 12;
    _codecContext->scenechange_threshold = 40;
    _codecContext->i_quant_factor = 0.71;
    _codecContext->b_frame_strategy = 1;
    _codecContext->qcompress = 0.5;
    _codecContext->qmin = 2;
    _codecContext->qmax = 31;
    _codecContext->max_qdiff = 4;
    _codecContext->max_b_frames = 3;
    _codecContext->refs = 3;
    _codecContext->trellis = 1;
    _codecContext->width = format.biWidth;
    _codecContext->height = format.biHeight;
    _codecContext->time_base.num = 1;
    _codecContext->time_base.den = 30;
    _codecContext->pix_fmt = PIX_FMT_YUV420P; 
    _codecContext->chromaoffset = 0;
    _codecContext->thread_count =1;
    _codecContext->bit_rate = (int)(128000.f * 0.80f);
    _codecContext->bit_rate_tolerance = (int) (128000.f * 0.20f);
    int error = avcodec_open2(_codecContext, _codec, NULL);
    if(error<   )
    {
        std::cout<<"Open codec fail. Error "<code>

    In such way ii fails on avopen_codec2() with:

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread 0xae1fdb70 (LWP 30675)]
    0xb2eb2cbb in x264_param_default () from /usr/lib/libx264.so.120
    

    If i comment all AVCodecContext parameters settins - I have :

    [libx264 @ 0xac75edd0] invalid width x height (0x0)
    

    And avcodec_open retunrs negative value. Which steps, I'm doing, are wrong?

    Thanks for any help (ffmpeg 0.10 && libx264 daily snapshot for yesterday)

  • libx264 x264_nal_encode overflowing

    8 mai 2014, par Ulterior

    My libH264 app is performing encoding of incomming camera data, but it keeps accumulating data and slows down the more encode operations it performs. Any idea's why is this happening?

    Variables are allocated at the start of a program. Is my sequence incorrect? I suspect I have to reinitilize some structure, but I cant find out how to do it...

    Here is part of my code in camera thread:

        int nWidth = 640;
        int nHeight = 480;    
    

    fill YUV data

        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); 
    

    fill PTS

        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 );  
         }  
        }  
    

    Pack data to RTMP

        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;  
    

    Here is the output of my app:

    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
    
  • ffmpeg syntax has changed

    8 mai 2014, par Budgierless

    Just like what was talked about in this post: FFmpeg cannot recognize a preset even though it does exsist Ubuntu 12.04

    I have help, with deblockalpha which has been depreciated from ffmpeg for along time, i am no good with all this coding business, but would like someone to tell me how i can fix this outdated syntax, as i am getting this error:

    Unrecognized option 'deblockalpha' Failed to set value '0' for option 'deblockalpha'

    please see code below and advise?

    $ffmpeg -i $input -r 30 -vcodec libx264 -s 512x288 -aspect 16:9 -b 1550k -maxrate 1800k -bufsize 4M -bt 1600k -flags +loop -cmp +chroma -deblockalpha 0 -deblockbeta 0 -refs 1 -coder 0 -me_method full -me_range 16 -subq 5 -partitions +parti4x4+parti8x8+partp8x8 -g 250 -keyint_min 25 -level 30 -trellis 2 -sc_threshold 40 -i_qfactor 0.71 -acodec libfaac -ab 128k -ar 44100 -ac 2 $addpre $output";
    

    thanks

  • X264 : How to access NAL units from encoder ?

    18 avril 2014, par user1884325

    When I call

    frame_size = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
    

    and subsequently write each NAL to a file like this:

         if (frame_size >= 0)
         {
            int i;
            int j;
    
            for (i = 0; i < i_nals; i++)
            {
               printf("******************* NAL %d (%d bytes) *******************\n", i, nals[i].i_payload);
               fwrite(&(nals[i].p_payload[0]), 1, nals[i].i_payload, fid);
            }
         }
    

    then I get this

    Beginning of NAL file

    My questions are:

    1) Is it normal that there's readable parameters in the beginning of the file?

    2) How do I configure the X264 encoder so that the encoder returns frames that I can send via UDP without the packet getting fragmented (size must be below 1390 or somewhere around that).

    3) With the x264.exe I pass in these options:

    "--threads 1 --profile baseline --level 3.2 --preset ultrafast --bframes 0 --force-cfr --no-mbtree --sync-lookahead 0 --rc-lookahead 0 --keyint 1000 --intra-refresh" 
    

    How do I map those to the settings in the X264 parameters structure ? (x264_param_t)

    4) I have been told that the x264 static library doesn't support bitmap input to the encoder and that I have to use libswscale for conversion of the 24bit RGB input bitmap to YUV2. The encoder, supposedly, only takes YUV2 as input? Is this true? If so, how do I build libswscale for the x264 static library?