4

there are a lot of questions asked regarding opencv + H.264 but none of them gave detailed explanation.

i am using openh264(openh264-1.4.0-win32msvc.dll) along with opencv 3.1(custom build with cmake having ffmpeg enabled) in visual studio, i wanted to save video coming from webcam in mp4 format with H.264 compression

VideoWriter write = VideoWriter("D:/movie.mp4", CV_FOURCC('H', '2', '6', '4'), 10.0, cv::Size(192, 144), true);

before using openh264, in console window i was seeing an warning message "Failed to load openh264 library : openh264-1.4.0-win32msvc.dll please check your environment and/or download from here: https://github.com/cisco/openh264/releases" (also video was not been saved)

so i downloaded the dll & kept in a folder with my program file(exe) now when i run the program, i'm seeing different error "[OpenH264] this = 0x0DE312C0, warning: bEnabledFrameSkip=0, bitrate can't be controlled for RC_QUALITY_MODE and RC_TIMESTAMP_MODE without enabling skip frame" (now video is saved, but size is very high! bit rate is around 1200 Kbps)

for me, the sole purpose of using h264 is to reduce the file size.. i think i may have to build openh264 myself with some changes to remove this error, can anyone guide me how? or tell me if there is a way to reduce bit rate somehow through code?

P.S: I tried giving -1 instead of CV_FOURCC(), 'installed codecs' window in my system showed up, i couldn't find h264 or x264 or h264vfw even though i have installed variety of codec packs & h264 from here

Thanks & regards

Prakash M
  • 659
  • 1
  • 12
  • 36

2 Answers2

2

If you want to control bitrate, You have to use both

encoderParemeters.iRCMode = RC_OFF_MODE;
encoderParemeters.bEnableFrameSkip = true; 


Here I am showing all the Openh264 Encoding parameters as an Example:

long nReturnedValueFromEncoder = WelsCreateSVCEncoder(&m_pSVCVideoEncoder);


    m_nVideoWidth = 352;
    m_nVideoHeight = 288;

    SEncParamExt encoderParemeters;

    memset(&encoderParemeters, 0, sizeof(SEncParamExt));

    m_pSVCVideoEncoder->GetDefaultParams(&encoderParemeters);

    encoderParemeters.iUsageType = CAMERA_VIDEO_REAL_TIME;
    encoderParemeters.iTemporalLayerNum = 0;
    encoderParemeters.uiIntraPeriod = 15;
    encoderParemeters.eSpsPpsIdStrategy = INCREASING_ID;
    encoderParemeters.bEnableSSEI = false;
    encoderParemeters.bEnableFrameCroppingFlag = true;
    encoderParemeters.iLoopFilterDisableIdc = 0;
    encoderParemeters.iLoopFilterAlphaC0Offset = 0;
    encoderParemeters.iLoopFilterBetaOffset = 0;
    encoderParemeters.iMultipleThreadIdc = 0;

    encoderParemeters.iRCMode = RC_BITRATE_MODE;
    encoderParemeters.iMinQp = 0;
    encoderParemeters.iMaxQp = 52;



    encoderParemeters.bEnableDenoise = false;
    encoderParemeters.bEnableSceneChangeDetect = false;
    encoderParemeters.bEnableBackgroundDetection = true;
    encoderParemeters.bEnableAdaptiveQuant = false;
    encoderParemeters.bEnableFrameSkip = true;
    encoderParemeters.bEnableLongTermReference = true;
    encoderParemeters.iLtrMarkPeriod = 20;
    encoderParemeters.bPrefixNalAddingCtrl = false;
    encoderParemeters.iSpatialLayerNum = 1;


    SSpatialLayerConfig *spartialLayerConfiguration = &encoderParemeters.sSpatialLayers[0];

    spartialLayerConfiguration->uiProfileIdc = PRO_BASELINE;//;

    encoderParemeters.iPicWidth = spartialLayerConfiguration->iVideoWidth = m_nVideoWidth;
    encoderParemeters.iPicHeight = spartialLayerConfiguration->iVideoHeight = m_nVideoHeight;
    encoderParemeters.fMaxFrameRate = spartialLayerConfiguration->fFrameRate = (float)30;

    encoderParemeters.iTargetBitrate = spartialLayerConfiguration->iSpatialBitrate = 500000;
    encoderParemeters.iTargetBitrate = spartialLayerConfiguration->iMaxSpatialBitrate = 500000;


    spartialLayerConfiguration->iDLayerQp = 24;
    //spartialLayerConfiguration->sSliceCfg.uiSliceMode = SM_SINGLE_SLICE;
    spartialLayerConfiguration->sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;


    nReturnedValueFromEncoder = m_pSVCVideoEncoder->InitializeExt(&encoderParemeters);

Hope it will help you.

RajibTheKing
  • 1,234
  • 1
  • 15
  • 35
  • 2
    Thanks for detailed answer, i think for that code, i'll have to add openh264 libraries & include files in visual studio. how to achieve this? do i need to build open264 libraries myself or can i get pre compiled libraries for windows. i'm searching but not getting much. tried [this](http://stackoverflow.com/questions/30045356/unable-to-build-openh264-lib-for-windows) but it didn't work(build failed in Visual studio) do u have any helpful links? Thanks – Prakash M Jul 19 '16 at 17:14
  • 3
    You have to build Openh264 using visual studio: i) Download OpenH264 source code provided by cisco ii) You will find visual studio compatible project /OpenH264/codec/build/win32/dec and /OpenH264/codec/build/win32/enc directory. iii) You will need to download NASM software iv) Then Add NASM executable path to all these visual studio projects v) Then You can either select static or dynamic library in general options. vi) Finally you will have 5 different project .lib or .dll files that is usable in any visual studio projects. – RajibTheKing Jul 20 '16 at 04:05
  • **How to do I input a video filename** that is `video_file.mp4` into`encoderParemeters` ? If not in `encoderParemeters`, where else can I tell encoder that this is my video file to encode data into. @RajibTheKing Can you please take a look at my question [here](https://stackoverflow.com/questions/46760839/paramvalidationext-error-with-welsinitencoderext-failed-while-setting-up-openh26). Very similar, related to setting up the encoder in OpenH264. thanks in advance – TheWaterProgrammer Oct 16 '17 at 05:55
1

You can simply controlled bit rate using RC_BITRATE_MODE and enabling bEnableFrameSkip in Openh264.

fahad_sust
  • 526
  • 1
  • 6
  • 21
  • Thanks for you answer. i have not used Openh264 before. like i said in question, i had just copied dll file in executable directory.. so i guess to enable bEnableSkip frame, i'll have to include open264 header files & static libraries in visual studio project ? any useful links for this ? tried [this](http://stackoverflow.com/questions/30045356/unable-to-build-openh264-lib-for-windows) but didn't work (build failed in visual studio) Thanks – Prakash M Jul 19 '16 at 17:13