Newest 'libx264' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Why codecs x264/x265 ignores pts and dts of input frame ?

    1er mars 2018, par Iceman

    I'm trying to encode images from a webcam with libx265 (libx264 tried earlier) ...
    The webcam can not shoot with stable FPS because of the different amount of light entering the matrix and, as a result, different delays. Therefore, I count the fps and dts of the incoming frame and set these values for the corresponding parameters of the x265_image object, and init the encoder fpsNum with 1000 and fpsDenom with 1 (for millisecond timebase).
    The problem is that the encoder ignores pts and dts of input image and encodes at 1000 fps! The same trick with timebase produces smooth record with libvpx. Why it does not work with x264/x265 codecs?

    Here is parameters initialization:

    ...
        error = (x265_param_default_preset(param, "fast", "zerolatency") != 0);
        if(!error){
            param->sourceWidth = width;
            param->sourceHeight = height;
            param->frameNumThreads = 1;
            param->fpsNum = 1000;
            param->fpsDenom =  1;
            // Intra refres:
            param->keyframeMax = 15;
            param->intraRefine = 1;
            // Rate control:
            param->rc.rateControlMode = X265_RC_CQP;
            param->rc.rfConstant = 12;
            param->rc.rfConstantMax = 48;
            // For streaming:
            param->bRepeatHeaders = 1;
            param->bAnnexB = 1;
            encoder = x265_encoder_open(param);
            ...
        }
    ...
    

    Here is frame adding function:

    bool hevc::Push(unsigned char *data){
        if(!error){
            std::lock_guard lock(m_framestack);
            if( timer > 0){
                framestack.back()->dts = clock() - timer;
                timer+= framestack.back()->dts;
            }
            else{timer = clock();}
            x265_picture *picture = x265_picture_alloc();
            if( picture){
                x265_picture_init(param, picture);
                picture->height = param->sourceHeight;
                picture->stride[0] = param->sourceWidth;
                picture->stride[1] = picture->stride[2] = picture->stride[0] / 2;
                picture->planes[0] = new char[  luma_size];
                picture->planes[1] = new char[chroma_size];
                picture->planes[2] = new char[chroma_size];
    
                colorspaces::BGRtoI420(param->sourceWidth, param->sourceHeight, data, (byte*)picture->planes[0], (byte*)picture->planes[1], (byte*)picture->planes[2]);
                picture->pts = picture->dts = 0;
                framestack.emplace_back(picture);
            }
            else{error = true;}
        }
        return !error;
    }
    

    Global PTS is increasing right after x265_encoder_encode call: pts+= pic_in->dts; and sets as a pts of new image from framestack queue when it comes to encoder.

    Can the x265/x264 codecs encode at variable fps? How to configure it if yes?

  • Encoding H.264 CBR videos with FFmpeg

    3 février 2018, par Cornstalks

    I'm trying to encode a video with ffmpeg into H.264 (via the libx264 library) with a constant bit rate. I know, I know, VBR is often preferred, but for this specific job I'm required to use CBR (just as long as it's so many kilobytes per second; it doesn't have to be an exact kilobytes per frame, afaik). My sample video I'm using to test is from here: http://a1408.g.akamai.net/5/1408/1388/2005110403/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_iTunes.mov.zip (it comes from http://support.apple.com/kb/HT1425)

    I can get a constant bit rate when encoding the video with MPEG-4 Video (using the commands ffmpeg -i sample_iTunes.mov -b 819968 -minrate 819968 -maxrate 819968 out.mov), and the bit rate is as expected. Reading the video's specs via the QuickTime Inspector, it's got a data rate of 844.94 kbit/s. Cool.

    However, when I change the codec to libx264, it seems to completely ignore my bitrate requests! The command I'm trying is "ffmpeg -i sample_iTunes.mov -vcodec libx264 -vpre medium -b 819968 -vb 819968 -minrate 819968 -maxrate 819968 -bufsize 400000 test.mov". But when I check the video's specs via the QuickTime Inspector, it's got a data rate of 254.74 kbit/s. WTF? That's not even close!

    I've tried changing so many parameters and adding tons of different things, and I've spent 2 days googling this, but I can't seem to get it to work. If I encode the video with the MainConcept H.264 encoder, I can get a constant bitrate, but I need this to work with ffmpeg.

    If someone can help me figure out how to do CBR H.264 encoding with FFmpeg, I will love you forever!

  • ffmpeg on Ubuntu-Linux - compile fails

    3 janvier 2018, par A.b.t.13

    (First of all i would like to sorry for any grammar mistakes, and to mention that I'm pretty much new to Linux :)) I'm having a really hard time trying to compile all the files related to the 'ffmpeg' command-line tool in my Ubuntu-16.04 environment.

    I've download the ffmpeg files from this link - http://www.ffmpeg.org/ extracted the files, and installed it by the instruction that supplied in the INSTALL.md file inside the folder.

    The process went well, but when i tried to run by this example :

    ffmpeg -i band_8k8k_3dv_projection.mp4 -filter_complex "[0:v]split=13[pr1][pr2][pr3][pr4][pr5][pr6][pr7][pr8][pr9][pr10][pr11][pr12][pr13];[pr1]crop=2048:2048:0:0[crp1];[pr2]crop=2048:2048:2048:0[crp2];[pr3]crop=2048:2048:4096:0[crp3];[pr4]crop=2048:2048:0:2048[crp4];[pr5]crop=2048:2048:2048:2048[crp5];[pr6]crop=2048:2048:4096:2048[crp6];[pr7]crop=2048:2048:0:4096[crp7];[pr8]crop=2048:2048:2048:4096[crp8];[pr9]crop=2048:2048:4096:4096[crp9];[pr10]crop=2048:2048:0:6144[crp10];[pr11]crop=2048:2048:2048:6144[crp11];[pr12]crop=2048:2048:4096:6144[crp12];[pr13]scale=6144:h=8192[pr13s];[pr13s][crp1]overlay=4096:0[ov1];[ov1][crp2]overlay=0:0[ov2];[ov2][crp3]overlay=4096:2048[ov3];[ov3][crp4]overlay=2048:2048[ov4];[ov4][crp5]overlay=2048:0[ov5];[ov5][crp6]overlay=0:2048[ov6];[ov6][crp7]overlay=4096:4096[ov7];[ov7][crp8]overlay=0:4096[ov8];[ov8][crp9]overlay=4096:6144[ov9];[ov9][crp10]overlay=2048:6144[ov10];[ov10][crp11]overlay=2048:4096[ov11];[ov11][crp12]overlay=0:6144[first];[first]split=22[prj1][prj2][prj3][prj4][prj5][prj6][prj7][prj8][prj9][prj10][prj11][prj12][prj13][prj14][prj15][prj16][prj17][prj18][prj19][prj20][prj21][prj22];[prj1]crop=1034:1034:0:0,pad=iw*2:ih[pd1]; [prj2]crop=1034:1034:0:0[cp1]; [pd1][cp1]overlay=w, scale=1504:h=464,setsar=1/1[crop1];[prj3]crop=1034:1034:0:0,pad=iw*2:ih[pd2]; [prj4]crop=1034:1034:0:0[cp2]; [pd2][cp2]overlay=w, scale=1504:h=464,setsar=1/1[crop2];[prj5]crop=1034:1034:0:0,pad=iw*2:ih[pd3]; [prj6]crop=1034:1034:0:0[cp3]; [pd3][cp3]overlay=w, scale=1504:h=464,setsar=1/1[crop3];[prj7]crop=1034:1034:0:0,pad=iw*2:ih[pd4]; [prj8]crop=1034:1034:0:4096[cp4]; [pd4][cp4]overlay=w, scale=1504:h=464,setsar=1/1[crop4];[prj9]crop=1034:1034:1014:0,pad=iw*2:ih[pd5]; [prj10]crop=1034:1034:1014:4096[cp5]; [pd5][cp5]overlay=w, scale=1504:h=464,setsar=1/1[crop5];[prj11]crop=1034:1034:2048:0,pad=iw*2:ih[pd6]; [prj12]crop=1034:1034:2048:4096[cp6]; [pd6][cp6]overlay=w, scale=1504:h=464,setsar=1/1[crop6];[prj13]crop=1034:1034:3062:0,pad=iw*2:ih[pd7]; [prj14]crop=1034:1034:3062:4096[cp7]; [pd7][cp7]overlay=w, scale=1504:h=464,setsar=1/1[crop7];[prj15]crop=1034:1034:4096:0,pad=iw*2:ih[pd8]; [prj16]crop=1034:1034:4096:4096[cp8]; [pd8][cp8]overlay=w, scale=1504:h=464,setsar=1/1[crop8];[prj17]crop=1034:1034:5110:0,pad=iw*2:ih[pd9]; [prj18]crop=1034:1034:5110:4096[cp9]; [pd9][cp9]overlay=w, scale=1504:h=464,setsar=1/1[crop9];[prj19]crop=1034:1034:0:1014,pad=iw*2:ih[pd10]; [prj20]crop=1034:1034:0:5110[cp10]; [pd10][cp10]overlay=w, scale=1504:h=464,setsar=1/1[crop10];[prj21]crop=1034:1034:1014:1014,pad=iw*2:ih[pd11]; [prj22]crop=1034:1034:1014:5110[cp11]; [pd11][cp11]overlay=w, scale=1504:h=464,setsar=1/1[crop11];[crop1]pad=iw:480*11,split=2[paded1][paded1a];[paded1][crop2]overlay=0:480*1[paded2];[paded2][crop3]overlay=0:480*2[paded3];[paded3][crop4]overlay=0:480*3[paded4];[paded4][crop5]overlay=0:480*4[paded5];[paded5][crop6]overlay=0:480*5[paded6];[paded6][crop7]overlay=0:480*6[paded7];[paded7][crop8]overlay=0:480*7[paded8];[paded8][crop9]overlay=0:480*8[paded9];[paded9][crop10]overlay=0:480*9[paded10];[paded10][crop11]overlay=0:480*10[out_no_black0];[paded1a]crop=1504:480,split=3[black0][black1][black2];[out_no_black0][black0]overlay=0:480*0[out_no_black1];[out_no_black1][black1]overlay=0:480*1[out_no_black2];[out_no_black2][black2]overlay=0:480*2[out_no_black3];[out_no_black3]crop=in_w:in_h" -preset slow -r 30 -pix_fmt yuv420p -movflags +faststart -coder 1 -cmp chroma -me_range 48 -me_method umh -subq 9 -refs 6 -b_strategy 2 -bf 16 -flags +loop+mv4+cgop -flags2 +local_header -forced-idr 1 -threads 0 -c:v libx264 -profile:v high -level 5.1 -x264opts "keyint=30:no-scenecut:min-keyint=30:fps=30" -b:v 70000k -maxrate 70000k -bufsize 140000k -slices 11 -c:a copy -f mp4 "band_8k8k_3dv_line_0_70000k.mp4" -y

    I've got this error: enter image description here

    I've looked for this problem, and found out that this unrecognized option may related to the lack of the library called "libx264". According to instruction i've found in this link - http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

    I configured the ffmpeg again with those flags --enable-gpl --enable-libx264 , installed th libx264 by this command - sudo apt-get install libx264-dev, all of those action went well, but when i try to compile it all together - i do it by running the command of 'make -j 10' or just 'make', when i'm inside the folder where all of the ffmpeg files are extracted to, i face this error - enter image description here

    Thanks for anyone who could help !

  • ffmpeg on Ubuntu-Linux - Cross-compile fails

    3 janvier 2018, par A.b.t.13

    (First of all i would like to sorry for any grammar mistakes, and to mention that I'm pretty much new to Linux :)) I'm having a really hard time trying to compile all the files related to the 'ffmpeg' command-line tool in my Ubuntu-16.04 environment.

    I've download the ffmpeg files from this link - http://www.ffmpeg.org/ extracted the files, and installed it by the instruction that supplied in the INSTALL.md file inside the folder.

    The process went well, but when i tried to run by this example :

    ffmpeg -i band_8k8k_3dv_projection.mp4 -filter_complex "[0:v]split=13[pr1][pr2][pr3][pr4][pr5][pr6][pr7][pr8][pr9][pr10][pr11][pr12][pr13];[pr1]crop=2048:2048:0:0[crp1];[pr2]crop=2048:2048:2048:0[crp2];[pr3]crop=2048:2048:4096:0[crp3];[pr4]crop=2048:2048:0:2048[crp4];[pr5]crop=2048:2048:2048:2048[crp5];[pr6]crop=2048:2048:4096:2048[crp6];[pr7]crop=2048:2048:0:4096[crp7];[pr8]crop=2048:2048:2048:4096[crp8];[pr9]crop=2048:2048:4096:4096[crp9];[pr10]crop=2048:2048:0:6144[crp10];[pr11]crop=2048:2048:2048:6144[crp11];[pr12]crop=2048:2048:4096:6144[crp12];[pr13]scale=6144:h=8192[pr13s];[pr13s][crp1]overlay=4096:0[ov1];[ov1][crp2]overlay=0:0[ov2];[ov2][crp3]overlay=4096:2048[ov3];[ov3][crp4]overlay=2048:2048[ov4];[ov4][crp5]overlay=2048:0[ov5];[ov5][crp6]overlay=0:2048[ov6];[ov6][crp7]overlay=4096:4096[ov7];[ov7][crp8]overlay=0:4096[ov8];[ov8][crp9]overlay=4096:6144[ov9];[ov9][crp10]overlay=2048:6144[ov10];[ov10][crp11]overlay=2048:4096[ov11];[ov11][crp12]overlay=0:6144[first];[first]split=22[prj1][prj2][prj3][prj4][prj5][prj6][prj7][prj8][prj9][prj10][prj11][prj12][prj13][prj14][prj15][prj16][prj17][prj18][prj19][prj20][prj21][prj22];[prj1]crop=1034:1034:0:0,pad=iw*2:ih[pd1]; [prj2]crop=1034:1034:0:0[cp1]; [pd1][cp1]overlay=w, scale=1504:h=464,setsar=1/1[crop1];[prj3]crop=1034:1034:0:0,pad=iw*2:ih[pd2]; [prj4]crop=1034:1034:0:0[cp2]; [pd2][cp2]overlay=w, scale=1504:h=464,setsar=1/1[crop2];[prj5]crop=1034:1034:0:0,pad=iw*2:ih[pd3]; [prj6]crop=1034:1034:0:0[cp3]; [pd3][cp3]overlay=w, scale=1504:h=464,setsar=1/1[crop3];[prj7]crop=1034:1034:0:0,pad=iw*2:ih[pd4]; [prj8]crop=1034:1034:0:4096[cp4]; [pd4][cp4]overlay=w, scale=1504:h=464,setsar=1/1[crop4];[prj9]crop=1034:1034:1014:0,pad=iw*2:ih[pd5]; [prj10]crop=1034:1034:1014:4096[cp5]; [pd5][cp5]overlay=w, scale=1504:h=464,setsar=1/1[crop5];[prj11]crop=1034:1034:2048:0,pad=iw*2:ih[pd6]; [prj12]crop=1034:1034:2048:4096[cp6]; [pd6][cp6]overlay=w, scale=1504:h=464,setsar=1/1[crop6];[prj13]crop=1034:1034:3062:0,pad=iw*2:ih[pd7]; [prj14]crop=1034:1034:3062:4096[cp7]; [pd7][cp7]overlay=w, scale=1504:h=464,setsar=1/1[crop7];[prj15]crop=1034:1034:4096:0,pad=iw*2:ih[pd8]; [prj16]crop=1034:1034:4096:4096[cp8]; [pd8][cp8]overlay=w, scale=1504:h=464,setsar=1/1[crop8];[prj17]crop=1034:1034:5110:0,pad=iw*2:ih[pd9]; [prj18]crop=1034:1034:5110:4096[cp9]; [pd9][cp9]overlay=w, scale=1504:h=464,setsar=1/1[crop9];[prj19]crop=1034:1034:0:1014,pad=iw*2:ih[pd10]; [prj20]crop=1034:1034:0:5110[cp10]; [pd10][cp10]overlay=w, scale=1504:h=464,setsar=1/1[crop10];[prj21]crop=1034:1034:1014:1014,pad=iw*2:ih[pd11]; [prj22]crop=1034:1034:1014:5110[cp11]; [pd11][cp11]overlay=w, scale=1504:h=464,setsar=1/1[crop11];[crop1]pad=iw:480*11,split=2[paded1][paded1a];[paded1][crop2]overlay=0:480*1[paded2];[paded2][crop3]overlay=0:480*2[paded3];[paded3][crop4]overlay=0:480*3[paded4];[paded4][crop5]overlay=0:480*4[paded5];[paded5][crop6]overlay=0:480*5[paded6];[paded6][crop7]overlay=0:480*6[paded7];[paded7][crop8]overlay=0:480*7[paded8];[paded8][crop9]overlay=0:480*8[paded9];[paded9][crop10]overlay=0:480*9[paded10];[paded10][crop11]overlay=0:480*10[out_no_black0];[paded1a]crop=1504:480,split=3[black0][black1][black2];[out_no_black0][black0]overlay=0:480*0[out_no_black1];[out_no_black1][black1]overlay=0:480*1[out_no_black2];[out_no_black2][black2]overlay=0:480*2[out_no_black3];[out_no_black3]crop=in_w:in_h" -preset slow -r 30 -pix_fmt yuv420p -movflags +faststart -coder 1 -cmp chroma -me_range 48 -me_method umh -subq 9 -refs 6 -b_strategy 2 -bf 16 -flags +loop+mv4+cgop -flags2 +local_header -forced-idr 1 -threads 0 -c:v libx264 -profile:v high -level 5.1 -x264opts "keyint=30:no-scenecut:min-keyint=30:fps=30" -b:v 70000k -maxrate 70000k -bufsize 140000k -slices 11 -c:a copy -f mp4 "band_8k8k_3dv_line_0_70000k.mp4" -y

    I've got this error: enter image description here

    I've looked for this problem, and found out that this unrecognized option may related to the lack of the library called "libx264". According to instruction i've found in this link - http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

    I configured the ffmpeg again with those flags --enable-gpl --enable-libx264 , installed th libx264 by this command - sudo apt-get install libx264-dev, all of those action went well, but when i try to compile it all together - i do it by running the command of 'make -j 10' or just 'make', when i'm inside the folder where all of the ffmpeg files are extracted to, i face this error - enter image description here

    Thanks for anyone who could help !

  • How to set variable FPS in libx264 and what encoder parameters to use ?

    20 décembre 2017, par Iceman

    I'm trying to encode a webcam frames with libx264 in realtime, and face with one problem - the resulting video length is exactly what I set, but camera is delays somtimes and the real capture time is more than video length. As a result the picture in video changes to fast.
    I think it is due to constant FPS in x264 settings, so I need to make it dynamic somehow. Is it possible? If I wrong about FPS, so what I need to do, to synchronize capturing and writing?

    Also I would like to know what are the optimal encoder parameters for streaming via internet and for recording to disk (the client is streaming from camera or screen, and the server is recording)?

    Here is console logs screenshot and my code:

    #include 
    #include "stringf.h"
    #include "Capture.h"
    #include "x264.h"
    
    int main( int argc, char **argv ){
        Camera instance;
        if(!instance.Enable(0)){printf("Camera not available\n");return 1;}
        // Initializing metrics and buffer of frame
        unsigned int width, height, size = instance.GetMetrics(width, height);
        unsigned char *data = (unsigned char *)malloc(size);
        // Setting encoder (I'm not sure about all parameters)
        x264_param_t param;
        x264_param_default_preset(&param, "ultrafast", "zerolatency");
        param.i_threads = 1;
        param.i_width = width;
        param.i_height = height;
        param.i_fps_num = 20;
        param.i_fps_den = 1;
        // Intra refres:
        param.i_keyint_max = 8;
        param.b_intra_refresh = 1;
        // Rate control:
        param.rc.i_rc_method = X264_RC_CRF;
        param.rc.f_rf_constant = 25;
        param.rc.f_rf_constant_max = 35;
        // For streaming:
        param.b_repeat_headers = 1;
        param.b_annexb = 1;
    
        x264_param_apply_profile(&param, "baseline");
        x264_t* encoder = x264_encoder_open(&param);
    
        int seconds, expected_time, operation_start, i_nals, frame_size, frames_count;
        expected_time = 1000/param.i_fps_num;
        operation_start = 0;
        seconds = 1;
        frames_count = param.i_fps_num * seconds;
        int *Timings = new int[frames_count];
    
        x264_picture_t pic_in, pic_out;
        x264_nal_t* nals;
        x264_picture_alloc(&pic_in, X264_CSP_I420, param.i_width, param.i_height);
        // Capture-Encode-Write loop
        for(int i = 0; i < frames_count; i++){
            operation_start = GetTickCount();
            size = instance.GrabBGR(&data);
            instance.BGRtoI420(data, &pic_in.img.plane[0], &pic_in.img.plane[1], &pic_in.img.plane[2], param.i_width, param.i_height);
            frame_size = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
            if( frame_size > 0){
                stringf::WriteBufferToFile("test.h264",std::string(reinterpret_cast(nals->p_payload), frame_size),1);
            }
            Timings[i] = GetTickCount() - operation_start;
        }
        while( x264_encoder_delayed_frames( encoder ) ){ // Flush delayed frames
            frame_size = x264_encoder_encode(encoder, &nals, &i_nals, NULL, &pic_out);
            if( frame_size > 0 ){stringf::WriteBufferToFile("test.h264",std::string(reinterpret_cast(nals->p_payload), frame_size),1);}
        }
        unsigned int total_time = 0;
        printf("Expected operation time was %d ms per frame at %u FPS\n",expected_time, param.i_fps_num);
        for(unsigned int i = 0; i < frames_count; i++){
            total_time += Timings[i];
            printf("Frame %u takes %d ms\n",(i+1), Timings[i]);
        }
        printf("Record takes %u ms\n",total_time);
        free(data);
        x264_encoder_close( encoder );
        x264_picture_clean( &pic_in );
        return 0;
    }
    

    log_x264 The capture takes 1453 ms and the output file plays exactly 1 sec.

    So, in general, the video length must be the same as a capture time, but not as encoder "wants".
    How to do it?