How to open AVCodec? - c++

I'm tired of searching the solution about this theme. Can anybody help?Types:
AVOutputFormat* m_outFormat;
AVFormatContext* m_formatContext;
AVCodecContext* m_videoCodecContext;
AVCodec* m_videoCodec;
Code:
avcodec_register_all();
av_register_all();
m_outFormat = av_guess_format(NULL,filePath().toUtf8().constData(),NULL);
//filePath ended like ".mp4"
if (!m_outFormat)
return; //all is fine
avformat_alloc_output_context2(&m_formatContext,NULL,NULL,filePath().toUtf8().constData());
m_formatContext->oformat->video_id = CODEC_ID_H264;
m_outFormat=m_formatContext->oformat;
////////////////////////////////////////////////////////////////////
m_videoCodec=avcodec_find_encoder(CODEC_ID_H264);
m_videoStream = avformat_new_stream(m_formatContext,m_videoCodec);
if (m_videoStream)
return; //all is fine
m_videoCodecContext = avcodec_alloc_context3(m_videoCodec);
m_videoCodecContext->codec_id = CODEC_ID_H264;
m_videoCodecContext->width = 1280;
m_videoCodecContext->height = 720;
m_videoCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
m_videoCodecContext->pix_fmt = PIX_FMT_YUV420P;
av_codec_open2(M_videoCodecContext,m_videoCodec,NULL);
I'm getting an error: [libx264 #.....] Codec type or id mismathes.
av_codec_open2(..) returned (-22 error). Where I did mistake?More info:
last ffmpeg
Mac Os x 10.10
libx264 installed
after av_guess_format(...) I've got audio_codec = CODEC_ID_H264, video_codec = CODEC_ID_NONE, long_name MP4(MPEG-4 Part 14) in m_outFormat.
after avformat_alloc_context3(...) I've got audio_codec_id = video_codec_id = CODEC_ID_NONE in m_formatContext.
after avcodec_find_encoder(CODEC_ID_H264) I've got name = "libx264", id = CODEC_ID_MPEG1VIDEO in m_videoCodec.
I can share more info if You can say, what do You exactly need.

I've found the answer... It's so stupid... I had old headers, from old ffmpeg sources, but with the libs from new sources.. So I replaced new headers and all errors has gone! I don't change code above.

Related

Assimp Export (C++) FBX file makes too much size

I am creating an FBX file through Assimp which makes too much size.
Sample source code:
scene->mRootNode = new aiNode();
scene->mMaterials = new aiMaterial*[1];
scene->mMaterials[0] = nullptr;
scene->mNumMaterials = 1;
scene->mMaterials[0] = new aiMaterial();
scene->mMeshes = new aiMesh*[numMesh];
scene->mNumMeshes = numMesh;
scene->mRootNode->mMeshes = new unsigned int[numMesh];
scene->mRootNode->mNumMeshes = numMesh;
Please let me know if anyone has encountered the same problem.

Core Audio specify which audio track to decode

I am able to successfully get the decoded PCM data of an audio file using Core Audio API. Below is the reduced code that shows how do I do that:
CFStringRef urlStr = CFStringCreateWithCString(kCFAllocatorDefault, "file.m4a", kCFStringEncodingUTF8);
CFURLRef urlRef = CFURLCreateWithFileSystemPath(NULL, urlStr, kCFURLPOSIXPathStyle, false);
ExtAudioFileOpenURL(urlRef, &m_audioFile);
bzero(&m_outputFormat, sizeof(AudioStreamBasicDescription));
m_outputFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
m_outputFormat.mSampleRate = m_inputFormat.mSampleRate;
m_outputFormat.mFormatID = kAudioFormatLinearPCM;
m_outputFormat.mChannelsPerFrame = m_inputFormat.mChannelsPerFrame;
m_outputFormat.mBytesPerFrame = sizeof(short) * m_outputFormat.mChannelsPerFrame;
m_outputFormat.mBitsPerChannel = sizeof(short) * 8;
m_outputFormat.mFramesPerPacket = 1;
m_outputFormat.mBytesPerPacket = m_outputFormat.mBytesPerFrame * m_outputFormat.mFramesPerPacket;
ExtAudioFileSetProperty(m_audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(m_outputFormat), &m_outputFormat)
short* transformData = new short[sampleCount];
AudioBufferList fillBufList;
fillBufList.mNumberBuffers = 1;
fillBufList.mBuffers[0].mNumberChannels = channels;
fillBufList.mBuffers[0].mDataByteSize = m_sampleCount * sizeof(short);
fillBufList.mBuffers[0].mData = (void*)(&transformData[0]);
ExtAudioFileRead(m_audioFile, &m_frameCount, &fillBufList);
I am interested in how can I specify the audio track I want to decode (suppose that media file contains more than one)?
One method is to decode all tracks and then extract (copy) the desired track data (every other sample for interleaved stereo, etc.) into another buffer, array, or file. Compared to the decode time, the extra copy time is insignificant.

openh264 - bEnableFrameSkip=0, bitrate can't be controlled

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
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.
You can simply controlled bit rate using RC_BITRATE_MODE and enabling bEnableFrameSkip in Openh264.

Segmentation Fault error when trying to compare two videos with pHash library and its ruby bindings

I have set up my system with the latest ffmpeg and pHash libraries (ffmpeg-2.2.1 and pHash-0.9.6) as well as the pHash ruby gem (https://github.com/toy/pHash).
I am using ruby and attempting to compare two video files like this:
require 'phash/video'
video1 = Phash::Video.new('video1.mp4')
video2 = Phash::Video.new('video2.mp4')
video1 % video2
Executing this script results in a Segmentation fault:
..../gems/pHash-1.1.4/lib/phash/video.rb:20: [BUG] Segmentation fault
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-darwin13.1.0]
-- Control frame information -----------------------------------------------
c:0008 p:---- s:0029 b:0029 l:000028 d:000028 CFUNC :ph_dct_videohash
c:0007 p:0042 s:0024 b:0024 l:000023 d:000023 METHOD .../gems/pHash-1.1.4/lib/phash/video.rb:20
c:0006 p:0038 s:0017 b:0017 l:000016 d:000016 METHOD .../gems/pHash-1.1.4/lib/phash.rb:43
c:0005 p:0025 s:0014 b:0014 l:000013 d:000013 METHOD .../gems/pHash-1.1.4/lib/phash.rb:39
c:0004 p:0011 s:0011 b:0011 l:000010 d:000010 METHOD .../gems/pHash-1.1.4/lib/phash.rb:48
c:0003 p:0050 s:0006 b:0006 l:000128 d:0011b8 EVAL video_test_phash.rb:3
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:000128 d:000128 TOP
-- Ruby level backtrace information ----------------------------------------
video_test_phash.rb:3:in `<main>'
.../gems/pHash-1.1.4/lib/phash.rb:48:in `similarity'
.../gems/pHash-1.1.4/lib/phash.rb:39:in `phash'
.../gems/pHash-1.1.4/lib/phash.rb:43:in `compute_phash'
.../gems/pHash-1.1.4/lib/phash/video.rb:20:in `video_hash'
.../gems/pHash-1.1.4/lib/phash/video.rb:20:in `ph_dct_videohash'
...
Abort trap: 6
It appears that the crash happens in the ph_dct_videohash function which is part of the pHash library. The function is in file pHash.cpp. I am copying it here in case it would make sense to someone:
ulong64* ph_dct_videohash(const char *filename, int &Length){
CImgList<uint8_t> *keyframes = ph_getKeyFramesFromVideo(filename);
if (keyframes == NULL)
return NULL;
Length = keyframes->size();
ulong64 *hash = (ulong64*)malloc(sizeof(ulong64)*Length);
CImg<float> *C = ph_dct_matrix(32);
CImg<float> Ctransp = C->get_transpose();
CImg<float> dctImage;
CImg<float> subsec;
CImg<uint8_t> currentframe;
for (unsigned int i=0;i < keyframes->size(); i++){
currentframe = keyframes->at(i);
currentframe.blur(1.0);
dctImage = (*C)*(currentframe)*Ctransp;
subsec = dctImage.crop(1,1,8,8).unroll('x');
float med = subsec.median();
hash[i] = 0x0000000000000000;
ulong64 one = 0x0000000000000001;
for (int j=0;j<64;j++){
if (subsec(j) > med)
hash[i] |= one;
one = one << 1;
}
}
keyframes->clear();
delete keyframes;
keyframes = NULL;
delete C;
C = NULL;
return hash;
}
Any help is very much appreciated!
In the latest versions of ffmpeg, some functions (like "avformat_open_input" in this case) segfault when given an uninitialized pointer. Someone on the pHash support mailing list has shown how to modify the pHash source in order to initialize the pointers, and prevent the segfaults.
To fix the segmentation faults, lines 365 and 411 in pHash-0.9.6/src/cimgffmpeg.cpp must be changed from AVFormatContext *pFormatCtx; to AVFormatContext *pFormatCtx = NULL;, and then the source code must be recompiled and installed.
Note that there still seem to be some problems with video hashes: for example, many (non-.mp4) video formats are unsupported, and cause segmentation faults.

Why I get OutOfMemory exception when trying create a decoder

I'm trying to get ID3D11VideoDecoder Decoder with h264 decoder profile but catching exception on Windows Phone 8.
Using this code:
DX::ThrowIfFailed(device.Get()->QueryInterface(__uuidof(ID3D11VideoDevice), (void**)&videoDevice));
GUID guid = {0x1b81be68, 0xa0c7,0x11d3,{0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}};
D3D11_VIDEO_DECODER_DESC *desc = new D3D11_VIDEO_DECODER_DESC();
desc->Guid = guid;
desc->OutputFormat = DXGI_FORMAT_420_OPAQUE;
desc->SampleHeight = 480;
desc->SampleWidth = 800;
D3D11_VIDEO_DECODER_CONFIG *conf = new D3D11_VIDEO_DECODER_CONFIG();
ID3D11VideoDecoder *decoder;
DX::ThrowIfFailed(videoDevice.Get()->CreateVideoDecoder(desc, conf, &decoder));
PS. I tried SharpDX for this and got the same issue.
It seems you didn't pass in a valid D3D11_VIDEO_DECODER_CONFIG variable. you just define a pointer of struct D3D11_VIDEO_DECODER_CONFIG and didn't set the values of it before calling function CreateVideoDecoder.
D3D11_VIDEO_DECODER_CONFIG *conf = new D3D11_VIDEO_DECODER_CONFIG();
DX::ThrowIfFailed(videoDevice.Get()->CreateVideoDecoder(desc, conf, &decoder));
You can try it as below.
D3D11_VIDEO_DECODER_CONFIG conf
ZeroMemory(&conf, sizeof(conf));
conf.guidConfigBitstreamEncryption = xxx;
...
conf.ConfigDecoderSpecific = xxx;
DX::ThrowIfFailed(videoDevice.Get()->CreateVideoDecoder(desc, conf, &decoder));
So, I found solution.
for H264 decoder ConfigBitstreamRaw field must be "2". Not "1", not "0". Only "2". Like this
VideoDecoderDescription decoderDescription = new VideoDecoderDescription();
decoderDescription.OutputFormat = Format.Opaque420;
decoderDescription.SampleHeight = 480;
decoderDescription.SampleWidth = 800;
decoderDescription.Guid = _formatGuid;
VideoDecoderConfig config = new VideoDecoderConfig();
config.ConfigMinRenderTargetBuffCount = 1;
config.ConfigBitstreamRaw = 2;