2 Star 8 Fork 6

孙文/live_camera_linux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
encode.c.bak 49.94 KB
一键复制 编辑 原始数据 按行查看 历史
孙文 提交于 2016-12-09 13:24 . 上传文件
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
/*
For the sake of simplicity, this example exits on error.
Very quick OpenMAX IL explanation:
- There are components. Each component performs an action. For example, the
OMX.broadcom.camera module captures images and videos and the
OMX.broadcom.image_encoder module encodes raw data from an image into multiple
formats. Each component has input and output ports and receives and sends
buffers with data. The main goal is to join these components to form a
pipeline and do complex tasks.
- There are two ways to connect components: with tunnels or manually. The
non-tunneled ports need to manually allocate the buffers with
OMX_AllocateBuffer() and free them with OMX_FreeBuffer().
- The components have states.
- There are at least two threads: the thread that uses the application (CPU) and
the thread that is used internally by OMX to execute the components (GPU).
- There are two types of functions: blocking and non-blocking. The blocking
functions are synchronous and the non-blocking are asynchronous. Being
asynchronous means that the function returns immediately but the result is
returned in a later time, so you need to wait until you receive an event. This
example uses two non-blocking functions: OMX_SendCommand and
OMX_FillThisBuffer.
Note: The camera component has two video ports: "preview" and "video". The
"preview" port must be enabled even if you're not using it (tunnel it to the
null_sink component) because it is used to run AGC (automatic gain control) and
AWB (auto white balance) algorithms.
*/
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <bcm_host.h>
#include <interface/vcos/vcos.h>
#include <IL/OMX_Broadcom.h>
#include <string.h>
#define OMX_INIT_STRUCTURE(x) \
memset (&(x), 0, sizeof (x)); \
(x).nSize = sizeof (x); \
(x).nVersion.nVersion = OMX_VERSION; \
(x).nVersion.s.nVersionMajor = OMX_VERSION_MAJOR; \
(x).nVersion.s.nVersionMinor = OMX_VERSION_MINOR; \
(x).nVersion.s.nRevision = OMX_VERSION_REVISION; \
(x).nVersion.s.nStep = OMX_VERSION_STEP
#define VIDEO_FRAMERATE 30
#define VIDEO_BITRATE 26000000
#define VIDEO_IDR_PERIOD 0 //Disabled
#define VIDEO_SEI OMX_FALSE
#define VIDEO_EEDE OMX_FALSE
#define VIDEO_EEDE_LOSS_RATE 0
#define VIDEO_QP OMX_FALSE
#define VIDEO_QP_I 0 //1 .. 51, 0 means off
#define VIDEO_QP_P 0 //1 .. 51, 0 means off
#define VIDEO_PROFILE OMX_VIDEO_AVCProfileHigh
#define VIDEO_INLINE_HEADERS OMX_TRUE
//Some settings doesn't work well
#define CAM_WIDTH 480
#define CAM_HEIGHT 360
#define CAM_SHARPNESS 0 //-100 .. 100
#define CAM_CONTRAST 1 //-100 .. 100
#define CAM_BRIGHTNESS 60 //0 .. 100
#define CAM_SATURATION 0 //-100 .. 100
#define CAM_SHUTTER_SPEED_AUTO OMX_TRUE
#define CAM_SHUTTER_SPEED 1.0/8.0
#define CAM_ISO_AUTO OMX_TRUE
#define CAM_ISO 100 //100 .. 800
#define CAM_EXPOSURE OMX_ExposureControlAuto
#define CAM_EXPOSURE_COMPENSATION 0 //-24 .. 24
#define CAM_MIRROR OMX_MirrorNone
#define CAM_ROTATION 0 //0 90 180 270
#define CAM_COLOR_ENABLE OMX_FALSE
#define CAM_COLOR_U 128 //0 .. 255
#define CAM_COLOR_V 128 //0 .. 255
#define CAM_NOISE_REDUCTION OMX_TRUE
#define CAM_FRAME_STABILIZATION OMX_TRUE
#define CAM_METERING OMX_MeteringModeAverage
#define CAM_WHITE_BALANCE OMX_WhiteBalControlAuto
//The gains are used if the white balance is set to off
#define CAM_WHITE_BALANCE_RED_GAIN 1000 //0 ..
#define CAM_WHITE_BALANCE_BLUE_GAIN 1000 //0 ..
#define CAM_IMAGE_FILTER OMX_ImageFilterNone
#define CAM_ROI_TOP 0 //0 .. 100
#define CAM_ROI_LEFT 0 //0 .. 100
#define CAM_ROI_WIDTH 100 //0 .. 100
#define CAM_ROI_HEIGHT 100 //0 .. 100
#define CAM_DRC OMX_DynRangeExpOff
const char* dump_OMX_COLOR_FORMATTYPE (OMX_COLOR_FORMATTYPE color);
const char* dump_OMX_OTHER_FORMATTYPE (OMX_OTHER_FORMATTYPE format);
const char* dump_OMX_AUDIO_CODINGTYPE (OMX_AUDIO_CODINGTYPE coding);
const char* dump_OMX_VIDEO_CODINGTYPE (OMX_VIDEO_CODINGTYPE coding);
const char* dump_OMX_IMAGE_CODINGTYPE (OMX_IMAGE_CODINGTYPE coding);
const char* dump_OMX_STATETYPE (OMX_STATETYPE state);
const char* dump_OMX_ERRORTYPE (OMX_ERRORTYPE error);
const char* dump_OMX_EVENTTYPE (OMX_EVENTTYPE event);
const char* dump_OMX_INDEXTYPE (OMX_INDEXTYPE type);
void dump_OMX_PARAM_PORTDEFINITIONTYPE (OMX_PARAM_PORTDEFINITIONTYPE* port);
void dump_OMX_IMAGE_PARAM_PORTFORMATTYPE (OMX_IMAGE_PARAM_PORTFORMATTYPE* port);
void dump_OMX_BUFFERHEADERTYPE (OMX_BUFFERHEADERTYPE* header);
#define DUMP_CASE(x) case x: return #x;
const char* dump_OMX_COLOR_FORMATTYPE (OMX_COLOR_FORMATTYPE color){
switch (color){
DUMP_CASE (OMX_COLOR_FormatUnused)
DUMP_CASE (OMX_COLOR_FormatMonochrome)
DUMP_CASE (OMX_COLOR_Format8bitRGB332)
DUMP_CASE (OMX_COLOR_Format12bitRGB444)
DUMP_CASE (OMX_COLOR_Format16bitARGB4444)
DUMP_CASE (OMX_COLOR_Format16bitARGB1555)
DUMP_CASE (OMX_COLOR_Format16bitRGB565)
DUMP_CASE (OMX_COLOR_Format16bitBGR565)
DUMP_CASE (OMX_COLOR_Format18bitRGB666)
DUMP_CASE (OMX_COLOR_Format18bitARGB1665)
DUMP_CASE (OMX_COLOR_Format19bitARGB1666)
DUMP_CASE (OMX_COLOR_Format24bitRGB888)
DUMP_CASE (OMX_COLOR_Format24bitBGR888)
DUMP_CASE (OMX_COLOR_Format24bitARGB1887)
DUMP_CASE (OMX_COLOR_Format25bitARGB1888)
DUMP_CASE (OMX_COLOR_Format32bitBGRA8888)
DUMP_CASE (OMX_COLOR_Format32bitARGB8888)
DUMP_CASE (OMX_COLOR_FormatYUV411Planar)
DUMP_CASE (OMX_COLOR_FormatYUV411PackedPlanar)
DUMP_CASE (OMX_COLOR_FormatYUV420Planar)
DUMP_CASE (OMX_COLOR_FormatYUV420PackedPlanar)
DUMP_CASE (OMX_COLOR_FormatYUV420SemiPlanar)
DUMP_CASE (OMX_COLOR_FormatYUV422Planar)
DUMP_CASE (OMX_COLOR_FormatYUV422PackedPlanar)
DUMP_CASE (OMX_COLOR_FormatYUV422SemiPlanar)
DUMP_CASE (OMX_COLOR_FormatYCbYCr)
DUMP_CASE (OMX_COLOR_FormatYCrYCb)
DUMP_CASE (OMX_COLOR_FormatCbYCrY)
DUMP_CASE (OMX_COLOR_FormatCrYCbY)
DUMP_CASE (OMX_COLOR_FormatYUV444Interleaved)
DUMP_CASE (OMX_COLOR_FormatRawBayer8bit)
DUMP_CASE (OMX_COLOR_FormatRawBayer10bit)
DUMP_CASE (OMX_COLOR_FormatRawBayer8bitcompressed)
DUMP_CASE (OMX_COLOR_FormatL2)
DUMP_CASE (OMX_COLOR_FormatL4)
DUMP_CASE (OMX_COLOR_FormatL8)
DUMP_CASE (OMX_COLOR_FormatL16)
DUMP_CASE (OMX_COLOR_FormatL24)
DUMP_CASE (OMX_COLOR_FormatL32)
DUMP_CASE (OMX_COLOR_FormatYUV420PackedSemiPlanar)
DUMP_CASE (OMX_COLOR_FormatYUV422PackedSemiPlanar)
DUMP_CASE (OMX_COLOR_Format18BitBGR666)
DUMP_CASE (OMX_COLOR_Format24BitARGB6666)
DUMP_CASE (OMX_COLOR_Format24BitABGR6666)
DUMP_CASE (OMX_COLOR_Format32bitABGR8888)
DUMP_CASE (OMX_COLOR_Format8bitPalette)
DUMP_CASE (OMX_COLOR_FormatYUVUV128)
DUMP_CASE (OMX_COLOR_FormatRawBayer12bit)
DUMP_CASE (OMX_COLOR_FormatBRCMEGL)
DUMP_CASE (OMX_COLOR_FormatBRCMOpaque)
DUMP_CASE (OMX_COLOR_FormatYVU420PackedPlanar)
DUMP_CASE (OMX_COLOR_FormatYVU420PackedSemiPlanar)
default: return "unknown OMX_COLOR_FORMATTYPE";
}
}
const char* dump_OMX_OTHER_FORMATTYPE (OMX_OTHER_FORMATTYPE format){
switch (format){
DUMP_CASE (OMX_OTHER_FormatTime);
DUMP_CASE (OMX_OTHER_FormatPower);
DUMP_CASE (OMX_OTHER_FormatStats);
DUMP_CASE (OMX_OTHER_FormatBinary);
DUMP_CASE (OMX_OTHER_FormatText);
DUMP_CASE (OMX_OTHER_FormatTextSKM2);
DUMP_CASE (OMX_OTHER_FormatText3GP5);
default: return "unknown OMX_OTHER_FORMATTYPE";
}
}
const char* dump_OMX_AUDIO_CODINGTYPE (OMX_AUDIO_CODINGTYPE coding){
switch (coding){
DUMP_CASE (OMX_AUDIO_CodingUnused)
DUMP_CASE (OMX_AUDIO_CodingAutoDetect)
DUMP_CASE (OMX_AUDIO_CodingPCM)
DUMP_CASE (OMX_AUDIO_CodingADPCM)
DUMP_CASE (OMX_AUDIO_CodingAMR)
DUMP_CASE (OMX_AUDIO_CodingGSMFR)
DUMP_CASE (OMX_AUDIO_CodingGSMEFR)
DUMP_CASE (OMX_AUDIO_CodingGSMHR)
DUMP_CASE (OMX_AUDIO_CodingPDCFR)
DUMP_CASE (OMX_AUDIO_CodingPDCEFR)
DUMP_CASE (OMX_AUDIO_CodingPDCHR)
DUMP_CASE (OMX_AUDIO_CodingTDMAFR)
DUMP_CASE (OMX_AUDIO_CodingTDMAEFR)
DUMP_CASE (OMX_AUDIO_CodingQCELP8)
DUMP_CASE (OMX_AUDIO_CodingQCELP13)
DUMP_CASE (OMX_AUDIO_CodingEVRC)
DUMP_CASE (OMX_AUDIO_CodingSMV)
DUMP_CASE (OMX_AUDIO_CodingG711)
DUMP_CASE (OMX_AUDIO_CodingG723)
DUMP_CASE (OMX_AUDIO_CodingG726)
DUMP_CASE (OMX_AUDIO_CodingG729)
DUMP_CASE (OMX_AUDIO_CodingAAC)
DUMP_CASE (OMX_AUDIO_CodingMP3)
DUMP_CASE (OMX_AUDIO_CodingSBC)
DUMP_CASE (OMX_AUDIO_CodingVORBIS)
DUMP_CASE (OMX_AUDIO_CodingWMA)
DUMP_CASE (OMX_AUDIO_CodingRA)
DUMP_CASE (OMX_AUDIO_CodingMIDI)
DUMP_CASE (OMX_AUDIO_CodingFLAC)
DUMP_CASE (OMX_AUDIO_CodingDDP)
DUMP_CASE (OMX_AUDIO_CodingDTS)
DUMP_CASE (OMX_AUDIO_CodingWMAPRO)
DUMP_CASE (OMX_AUDIO_CodingATRAC3)
DUMP_CASE (OMX_AUDIO_CodingATRACX)
DUMP_CASE (OMX_AUDIO_CodingATRACAAL)
default: return "unknown OMX_AUDIO_CODINGTYPE";
}
}
const char* dump_OMX_VIDEO_CODINGTYPE (OMX_VIDEO_CODINGTYPE coding){
switch (coding){
DUMP_CASE (OMX_VIDEO_CodingUnused)
DUMP_CASE (OMX_VIDEO_CodingAutoDetect)
DUMP_CASE (OMX_VIDEO_CodingMPEG2)
DUMP_CASE (OMX_VIDEO_CodingH263)
DUMP_CASE (OMX_VIDEO_CodingMPEG4)
DUMP_CASE (OMX_VIDEO_CodingWMV)
DUMP_CASE (OMX_VIDEO_CodingRV)
DUMP_CASE (OMX_VIDEO_CodingAVC)
DUMP_CASE (OMX_VIDEO_CodingMJPEG)
DUMP_CASE (OMX_VIDEO_CodingVP6)
DUMP_CASE (OMX_VIDEO_CodingVP7)
DUMP_CASE (OMX_VIDEO_CodingVP8)
DUMP_CASE (OMX_VIDEO_CodingYUV)
DUMP_CASE (OMX_VIDEO_CodingSorenson)
DUMP_CASE (OMX_VIDEO_CodingTheora)
DUMP_CASE (OMX_VIDEO_CodingMVC)
default: return "unknown OMX_VIDEO_CODINGTYPE";
}
}
const char* dump_OMX_IMAGE_CODINGTYPE (OMX_IMAGE_CODINGTYPE coding){
switch (coding){
DUMP_CASE (OMX_IMAGE_CodingUnused)
DUMP_CASE (OMX_IMAGE_CodingAutoDetect)
DUMP_CASE (OMX_IMAGE_CodingJPEG)
DUMP_CASE (OMX_IMAGE_CodingJPEG2K)
DUMP_CASE (OMX_IMAGE_CodingEXIF)
DUMP_CASE (OMX_IMAGE_CodingTIFF)
DUMP_CASE (OMX_IMAGE_CodingGIF)
DUMP_CASE (OMX_IMAGE_CodingPNG)
DUMP_CASE (OMX_IMAGE_CodingLZW)
DUMP_CASE (OMX_IMAGE_CodingBMP)
DUMP_CASE (OMX_IMAGE_CodingTGA)
DUMP_CASE (OMX_IMAGE_CodingPPM)
default: return "unknown OMX_IMAGE_CODINGTYPE";
}
}
const char* dump_OMX_STATETYPE (OMX_STATETYPE state){
switch (state){
DUMP_CASE (OMX_StateInvalid)
DUMP_CASE (OMX_StateLoaded)
DUMP_CASE (OMX_StateIdle)
DUMP_CASE (OMX_StateExecuting)
DUMP_CASE (OMX_StatePause)
DUMP_CASE (OMX_StateWaitForResources)
default: return "unknown OMX_STATETYPE";
}
}
const char* dump_OMX_ERRORTYPE (OMX_ERRORTYPE error){
switch (error){
DUMP_CASE (OMX_ErrorNone)
DUMP_CASE (OMX_ErrorInsufficientResources)
DUMP_CASE (OMX_ErrorUndefined)
DUMP_CASE (OMX_ErrorInvalidComponentName)
DUMP_CASE (OMX_ErrorComponentNotFound)
DUMP_CASE (OMX_ErrorInvalidComponent)
DUMP_CASE (OMX_ErrorBadParameter)
DUMP_CASE (OMX_ErrorNotImplemented)
DUMP_CASE (OMX_ErrorUnderflow)
DUMP_CASE (OMX_ErrorOverflow)
DUMP_CASE (OMX_ErrorHardware)
DUMP_CASE (OMX_ErrorInvalidState)
DUMP_CASE (OMX_ErrorStreamCorrupt)
DUMP_CASE (OMX_ErrorPortsNotCompatible)
DUMP_CASE (OMX_ErrorResourcesLost)
DUMP_CASE (OMX_ErrorNoMore)
DUMP_CASE (OMX_ErrorVersionMismatch)
DUMP_CASE (OMX_ErrorNotReady)
DUMP_CASE (OMX_ErrorTimeout)
DUMP_CASE (OMX_ErrorSameState)
DUMP_CASE (OMX_ErrorResourcesPreempted)
DUMP_CASE (OMX_ErrorPortUnresponsiveDuringAllocation)
DUMP_CASE (OMX_ErrorPortUnresponsiveDuringDeallocation)
DUMP_CASE (OMX_ErrorPortUnresponsiveDuringStop)
DUMP_CASE (OMX_ErrorIncorrectStateTransition)
DUMP_CASE (OMX_ErrorIncorrectStateOperation)
DUMP_CASE (OMX_ErrorUnsupportedSetting)
DUMP_CASE (OMX_ErrorUnsupportedIndex)
DUMP_CASE (OMX_ErrorBadPortIndex)
DUMP_CASE (OMX_ErrorPortUnpopulated)
DUMP_CASE (OMX_ErrorComponentSuspended)
DUMP_CASE (OMX_ErrorDynamicResourcesUnavailable)
DUMP_CASE (OMX_ErrorMbErrorsInFrame)
DUMP_CASE (OMX_ErrorFormatNotDetected)
DUMP_CASE (OMX_ErrorContentPipeOpenFailed)
DUMP_CASE (OMX_ErrorContentPipeCreationFailed)
DUMP_CASE (OMX_ErrorSeperateTablesUsed)
DUMP_CASE (OMX_ErrorTunnelingUnsupported)
DUMP_CASE (OMX_ErrorDiskFull)
DUMP_CASE (OMX_ErrorMaxFileSize)
DUMP_CASE (OMX_ErrorDrmUnauthorised)
DUMP_CASE (OMX_ErrorDrmExpired)
DUMP_CASE (OMX_ErrorDrmGeneral)
default: return "unknown OMX_ERRORTYPE";
}
}
const char* dump_OMX_EVENTTYPE (OMX_EVENTTYPE event){
switch (event){
DUMP_CASE (OMX_EventCmdComplete)
DUMP_CASE (OMX_EventError)
DUMP_CASE (OMX_EventMark)
DUMP_CASE (OMX_EventPortSettingsChanged)
DUMP_CASE (OMX_EventBufferFlag)
DUMP_CASE (OMX_EventResourcesAcquired)
DUMP_CASE (OMX_EventComponentResumed)
DUMP_CASE (OMX_EventDynamicResourcesAvailable)
DUMP_CASE (OMX_EventPortFormatDetected)
DUMP_CASE (OMX_EventParamOrConfigChanged)
default: return "unkonwn OMX_EVENTTYPE";
}
}
const char* dump_OMX_INDEXTYPE (OMX_INDEXTYPE type){
switch (type){
DUMP_CASE (OMX_IndexParamAudioInit)
DUMP_CASE (OMX_IndexParamVideoInit)
DUMP_CASE (OMX_IndexParamImageInit)
DUMP_CASE (OMX_IndexParamOtherInit)
default: return "other OMX_INDEXTYPE";
}
}
void dump_OMX_PARAM_PORTDEFINITIONTYPE (
OMX_PARAM_PORTDEFINITIONTYPE* port){
char domain[512];
char domain_info[512];
switch (port->eDomain){
case OMX_PortDomainAudio:
strcpy (domain, "OMX_PortDomainAudio");
sprintf (domain_info,
" cMIMEType: %s\n"
" bFlagErrorConcealment: %s\n"
" eEncoding: %s\n",
port->format.video.cMIMEType,
port->format.image.bFlagErrorConcealment ? "true" : "false",
dump_OMX_AUDIO_CODINGTYPE (
port->format.audio.eEncoding));
break;
case OMX_PortDomainVideo:
strcpy (domain, "OMX_PortDomainVideo");
sprintf (domain_info,
" cMIMEType: %s\n"
" nFrameWidth: %d\n"
" nFrameHeight: %d\n"
" nStride: %d\n"
" nSliceHeight: %d\n"
" nBitrate: %d\n"
" xFramerate: %d\n"
" bFlagErrorConcealment: %s\n"
" eCompressionFormat: %s\n"
" eColorFormat: %s\n"
, port->format.video.cMIMEType,
port->format.video.nFrameWidth,
port->format.video.nFrameHeight,
port->format.video.nStride,
port->format.video.nSliceHeight,
port->format.video.nBitrate,
port->format.video.xFramerate,
port->format.image.bFlagErrorConcealment ? "true" : "false",
dump_OMX_VIDEO_CODINGTYPE (
port->format.video.eCompressionFormat),
dump_OMX_COLOR_FORMATTYPE (
port->format.video.eColorFormat));
break;
case OMX_PortDomainImage:
strcpy (domain, "OMX_PortDomainImage");
sprintf (domain_info,
" cMIMEType: %s\n"
" nFrameWidth: %d\n"
" nFrameHeight: %d\n"
" nStride: %d\n"
" nSliceHeight: %d\n"
" bFlagErrorConcealment: %s\n"
" eCompressionFormat: %s\n"
" eColorFormat: %s\n"
, port->format.image.cMIMEType,
port->format.image.nFrameWidth,
port->format.image.nFrameHeight,
port->format.image.nStride,
port->format.image.nSliceHeight,
port->format.image.bFlagErrorConcealment ? "true" : "false",
dump_OMX_IMAGE_CODINGTYPE (
port->format.image.eCompressionFormat),
dump_OMX_COLOR_FORMATTYPE (
port->format.image.eColorFormat));
break;
case OMX_PortDomainOther:
strcpy (domain, "OMX_PortDomainOther");
sprintf (domain_info,
" eFormat: %s\n",
dump_OMX_OTHER_FORMATTYPE (
port->format.other.eFormat));
break;
default:
strcpy (domain, "unknown");
strcpy (domain_info, " unknown");
break;
}
printf (
"nSize: %d\n"
"nPortIndex: %d\n"
"eDir: %s\n"
"nBufferCountActual: %d\n"
"nBufferCountMin: %d\n"
"nBufferSize: %d\n"
"bEnabled: %s\n"
"bPopulated: %s\n"
"eDomain: %s\n"
" format:\n"
"%s"
"bBuffersContiguous: %s\n"
"nBufferAlignment: %d\n",
port->nSize,
port->nPortIndex,
port->eDir == OMX_DirInput ? "input" : "output",
port->nBufferCountActual,
port->nBufferCountMin,
port->nBufferSize,
port->bEnabled ? "true" : "false",
port->bPopulated ? "true" : "false",
domain,
domain_info,
port->bBuffersContiguous ? "true": "false",
port->nBufferAlignment);
}
void dump_OMX_IMAGE_PARAM_PORTFORMATTYPE (
OMX_IMAGE_PARAM_PORTFORMATTYPE* port){
printf (
"nSize: %d\n"
"nPortIndex: %d\n"
"nIndex: %d\n"
"eCompressionFormat: %s\n"
"eColorFormat: %s\n",
port->nSize,
port->nPortIndex,
port->nIndex,
dump_OMX_IMAGE_CODINGTYPE (port->eCompressionFormat),
dump_OMX_COLOR_FORMATTYPE (port->eColorFormat));
}
void dump_OMX_BUFFERHEADERTYPE (OMX_BUFFERHEADERTYPE* header){
long long int timestamp = (long long int)header->nTimeStamp.nHighPart;
timestamp = timestamp << 32;
timestamp |= (long long int)header->nTimeStamp.nLowPart;
printf (
"nSize: %d\n"
"nAllocLen: %d\n"
"nFilledLen: %d\n"
"nOffset: %d\n"
"hMarkTargetComponent: %s\n"
"nTickCount: %d\n"
"nTimeStamp: %lld\n"
"nFlags: %X\n"
"nOutputPortIndex: %d\n"
"nInputPortIndex: %d\n",
header->nSize,
header->nAllocLen,
header->nFilledLen,
header->nOffset,
header->hMarkTargetComponent ? "not null" : "null (no mark)",
header->nTickCount,
timestamp,
header->nFlags,
header->nOutputPortIndex,
header->nInputPortIndex);
}
/*
Possible values:
CAM_EXPOSURE
OMX_ExposureControlOff
OMX_ExposureControlAuto
OMX_ExposureControlNight
OMX_ExposureControlBackLight
OMX_ExposureControlSpotlight
OMX_ExposureControlSports
OMX_ExposureControlSnow
OMX_ExposureControlBeach
OMX_ExposureControlLargeAperture
OMX_ExposureControlSmallAperture
OMX_ExposureControlVeryLong
OMX_ExposureControlFixedFps
OMX_ExposureControlNightWithPreview
OMX_ExposureControlAntishake
OMX_ExposureControlFireworks
CAM_IMAGE_FILTER
OMX_ImageFilterNone
OMX_ImageFilterEmboss
OMX_ImageFilterNegative
OMX_ImageFilterSketch
OMX_ImageFilterOilPaint
OMX_ImageFilterHatch
OMX_ImageFilterGpen
OMX_ImageFilterSolarize
OMX_ImageFilterWatercolor
OMX_ImageFilterPastel
OMX_ImageFilterFilm
OMX_ImageFilterBlur
OMX_ImageFilterColourSwap
OMX_ImageFilterWashedOut
OMX_ImageFilterColourPoint
OMX_ImageFilterPosterise
OMX_ImageFilterColourBalance
OMX_ImageFilterCartoon
CAM_METERING
OMX_MeteringModeAverage
OMX_MeteringModeSpot
OMX_MeteringModeMatrix
OMX_MeteringModeBacklit
CAM_MIRROR
OMX_MirrorNone
OMX_MirrorHorizontal
OMX_MirrorVertical
OMX_MirrorBoth
CAM_WHITE_BALANCE
OMX_WhiteBalControlOff
OMX_WhiteBalControlAuto
OMX_WhiteBalControlSunLight
OMX_WhiteBalControlCloudy
OMX_WhiteBalControlShade
OMX_WhiteBalControlTungsten
OMX_WhiteBalControlFluorescent
OMX_WhiteBalControlIncandescent
OMX_WhiteBalControlFlash
OMX_WhiteBalControlHorizon
CAM_DRC
OMX_DynRangeExpOff
OMX_DynRangeExpLow
OMX_DynRangeExpMedium
OMX_DynRangeExpHigh
VIDEO_PROFILE
OMX_VIDEO_AVCProfileHigh
OMX_VIDEO_AVCProfileBaseline
OMX_VIDEO_AVCProfileMain
*/
//Data of each component
typedef struct {
//The handle is obtained with OMX_GetHandle() and is used on every function
//that needs to manipulate a component. It is released with OMX_FreeHandle()
OMX_HANDLETYPE handle;
//Bitwise OR of flags. Used for blocking the current thread and waiting an
//event. Used with vcos_event_flags_get() and vcos_event_flags_set()
VCOS_EVENT_FLAGS_T flags;
//The fullname of the component
OMX_STRING name;
} component_t;
//Events used with vcos_event_flags_get() and vcos_event_flags_set()
typedef enum {
EVENT_ERROR = 0x1,
EVENT_PORT_ENABLE = 0x2,
EVENT_PORT_DISABLE = 0x4,
EVENT_STATE_SET = 0x8,
EVENT_FLUSH = 0x10,
EVENT_MARK_BUFFER = 0x20,
EVENT_MARK = 0x40,
EVENT_PORT_SETTINGS_CHANGED = 0x80,
EVENT_PARAM_OR_CONFIG_CHANGED = 0x100,
EVENT_BUFFER_FLAG = 0x200,
EVENT_RESOURCES_ACQUIRED = 0x400,
EVENT_DYNAMIC_RESOURCES_AVAILABLE = 0x800,
EVENT_FILL_BUFFER_DONE = 0x1000,
EVENT_EMPTY_BUFFER_DONE = 0x2000,
} component_event;
OMX_ERRORTYPE error;
OMX_BUFFERHEADERTYPE* encoder_output_buffer;
component_t camera;
component_t encoder;
component_t null_sink;
OMX_CONFIG_PORTBOOLEANTYPE capture_st;
//Prototypes
OMX_ERRORTYPE event_handler (
OMX_IN OMX_HANDLETYPE comp,
OMX_IN OMX_PTR app_data,
OMX_IN OMX_EVENTTYPE event,
OMX_IN OMX_U32 data1,
OMX_IN OMX_U32 data2,
OMX_IN OMX_PTR event_data);
OMX_ERRORTYPE fill_buffer_done (
OMX_IN OMX_HANDLETYPE comp,
OMX_IN OMX_PTR app_data,
OMX_IN OMX_BUFFERHEADERTYPE* buffer);
void wake (component_t* component, VCOS_UNSIGNED event);
void wait (
component_t* component,
VCOS_UNSIGNED events,
VCOS_UNSIGNED* retrieved_events);
void init_component (component_t* component);
void deinit_component (component_t* component);
void load_camera_drivers (component_t* component);
void change_state (component_t* component, OMX_STATETYPE state);
void enable_port (component_t* component, OMX_U32 port);
void disable_port (component_t* component, OMX_U32 port);
void enable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE** encoder_output_buffer);
void disable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE* encoder_output_buffer);
void set_camera_settings (component_t* camera);
void set_h264_settings (component_t* encoder);
//Function that is called when a component receives an event from a secondary
//thread
OMX_ERRORTYPE event_handler (
OMX_IN OMX_HANDLETYPE comp,
OMX_IN OMX_PTR app_data,
OMX_IN OMX_EVENTTYPE event,
OMX_IN OMX_U32 data1,
OMX_IN OMX_U32 data2,
OMX_IN OMX_PTR event_data){
component_t* component = (component_t*)app_data;
switch (event){
case OMX_EventCmdComplete:
switch (data1){
case OMX_CommandStateSet:
printf ("event: %s, OMX_CommandStateSet, state: %s\n",
component->name, dump_OMX_STATETYPE (data2));
wake (component, EVENT_STATE_SET);
break;
case OMX_CommandPortDisable:
printf ("event: %s, OMX_CommandPortDisable, port: %d\n",
component->name, data2);
wake (component, EVENT_PORT_DISABLE);
break;
case OMX_CommandPortEnable:
printf ("event: %s, OMX_CommandPortEnable, port: %d\n",
component->name, data2);
wake (component, EVENT_PORT_ENABLE);
break;
case OMX_CommandFlush:
printf ("event: %s, OMX_CommandFlush, port: %d\n",
component->name, data2);
wake (component, EVENT_FLUSH);
break;
case OMX_CommandMarkBuffer:
printf ("event: %s, OMX_CommandMarkBuffer, port: %d\n",
component->name, data2);
wake (component, EVENT_MARK_BUFFER);
break;
}
break;
case OMX_EventError:
printf ("event: %s, %s\n", component->name, dump_OMX_ERRORTYPE (data1));
wake (component, EVENT_ERROR);
break;
case OMX_EventMark:
printf ("event: %s, OMX_EventMark\n", component->name);
wake (component, EVENT_MARK);
break;
case OMX_EventPortSettingsChanged:
printf ("event: %s, OMX_EventPortSettingsChanged, port: %d\n",
component->name, data1);
wake (component, EVENT_PORT_SETTINGS_CHANGED);
break;
case OMX_EventParamOrConfigChanged:
printf ("event: %s, OMX_EventParamOrConfigChanged, data1: %d, data2: "
"%X\n", component->name, data1, data2);
wake (component, EVENT_PARAM_OR_CONFIG_CHANGED);
break;
case OMX_EventBufferFlag:
printf ("event: %s, OMX_EventBufferFlag, port: %d\n",
component->name, data1);
wake (component, EVENT_BUFFER_FLAG);
break;
case OMX_EventResourcesAcquired:
printf ("event: %s, OMX_EventResourcesAcquired\n", component->name);
wake (component, EVENT_RESOURCES_ACQUIRED);
break;
case OMX_EventDynamicResourcesAvailable:
printf ("event: %s, OMX_EventDynamicResourcesAvailable\n",
component->name);
wake (component, EVENT_DYNAMIC_RESOURCES_AVAILABLE);
break;
default:
//This should never execute, just ignore
printf ("event: unknown (%X)\n", event);
break;
}
return OMX_ErrorNone;
}
//Function that is called when a component fills a buffer with data
OMX_ERRORTYPE fill_buffer_done (
OMX_IN OMX_HANDLETYPE comp,
OMX_IN OMX_PTR app_data,
OMX_IN OMX_BUFFERHEADERTYPE* buffer){
component_t* component = (component_t*)app_data;
wake (component, EVENT_FILL_BUFFER_DONE);
return OMX_ErrorNone;
}
void wake (component_t* component, VCOS_UNSIGNED event){
vcos_event_flags_set (&component->flags, event, VCOS_OR);
}
void wait (
component_t* component,
VCOS_UNSIGNED events,
VCOS_UNSIGNED* retrieved_events){
VCOS_UNSIGNED set;
if (vcos_event_flags_get (&component->flags, events | EVENT_ERROR,
VCOS_OR_CONSUME, VCOS_SUSPEND, &set)){
fprintf (stderr, "error: vcos_event_flags_get\n");
exit (1);
}
if (set == EVENT_ERROR){
exit (1);
}
if (retrieved_events){
*retrieved_events = set;
}
}
void init_component (component_t* component){
printf ("initializing component %s\n", component->name);
OMX_ERRORTYPE error;
//Create the event flags
if (vcos_event_flags_create (&component->flags, "component")){
fprintf (stderr, "error: vcos_event_flags_create\n");
exit (1);
}
//Each component has an event_handler and fill_buffer_done functions
OMX_CALLBACKTYPE callbacks_st;
callbacks_st.EventHandler = event_handler;
callbacks_st.FillBufferDone = fill_buffer_done;
//Get the handle
if ((error = OMX_GetHandle (&component->handle, component->name, component,
&callbacks_st))){
fprintf (stderr, "error: OMX_GetHandle: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Disable all the ports
OMX_INDEXTYPE types[] = {
OMX_IndexParamAudioInit,
OMX_IndexParamVideoInit,
OMX_IndexParamImageInit,
OMX_IndexParamOtherInit
};
OMX_PORT_PARAM_TYPE ports_st;
OMX_INIT_STRUCTURE (ports_st);
int i;
for (i=0; i<4; i++){
if ((error = OMX_GetParameter (component->handle, types[i], &ports_st))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
OMX_U32 port;
for (port=ports_st.nStartPortNumber;
port<ports_st.nStartPortNumber + ports_st.nPorts; port++){
//Disable the port
disable_port (component, port);
//Wait to the event
wait (component, EVENT_PORT_DISABLE, 0);
}
}
}
void deinit_component (component_t* component){
printf ("deinitializing component %s\n", component->name);
OMX_ERRORTYPE error;
vcos_event_flags_delete (&component->flags);
if ((error = OMX_FreeHandle (component->handle))){
fprintf (stderr, "error: OMX_FreeHandle: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void load_camera_drivers (component_t* component){
/*
This is a specific behaviour of the Broadcom's Raspberry Pi OpenMAX IL
implementation module because the OMX_SetConfig() and OMX_SetParameter() are
blocking functions but the drivers are loaded asynchronously, that is, an
event is fired to signal the completion. Basically, what you're saying is:
"When the parameter with index OMX_IndexParamCameraDeviceNumber is set, load
the camera drivers and emit an OMX_EventParamOrConfigChanged event"
The red LED of the camera will be turned on after this call.
*/
printf ("loading camera drivers\n");
OMX_ERRORTYPE error;
OMX_CONFIG_REQUESTCALLBACKTYPE cbs_st;
OMX_INIT_STRUCTURE (cbs_st);
cbs_st.nPortIndex = OMX_ALL;
cbs_st.nIndex = OMX_IndexParamCameraDeviceNumber;
cbs_st.bEnable = OMX_TRUE;
if ((error = OMX_SetConfig (component->handle, OMX_IndexConfigRequestCallback,
&cbs_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
OMX_PARAM_U32TYPE dev_st;
OMX_INIT_STRUCTURE (dev_st);
dev_st.nPortIndex = OMX_ALL;
//ID for the camera device
dev_st.nU32 = 0;
if ((error = OMX_SetParameter (component->handle,
OMX_IndexParamCameraDeviceNumber, &dev_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
wait (component, EVENT_PARAM_OR_CONFIG_CHANGED, 0);
}
void change_state (component_t* component, OMX_STATETYPE state){
printf ("changing %s state to %s\n", component->name,
dump_OMX_STATETYPE (state));
OMX_ERRORTYPE error;
if ((error = OMX_SendCommand (component->handle, OMX_CommandStateSet, state,
0))){
fprintf (stderr, "error: OMX_SendCommand: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void enable_port (component_t* component, OMX_U32 port){
printf ("enabling port %d (%s)\n", port, component->name);
OMX_ERRORTYPE error;
if ((error = OMX_SendCommand (component->handle, OMX_CommandPortEnable,
port, 0))){
fprintf (stderr, "error: OMX_SendCommand: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void disable_port (component_t* component, OMX_U32 port){
printf ("disabling port %d (%s)\n", port, component->name);
OMX_ERRORTYPE error;
if ((error = OMX_SendCommand (component->handle, OMX_CommandPortDisable,
port, 0))){
fprintf (stderr, "error: OMX_SendCommand: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void enable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE** encoder_output_buffer){
//The port is not enabled until the buffer is allocated
OMX_ERRORTYPE error;
enable_port (encoder, 201);
OMX_PARAM_PORTDEFINITIONTYPE port_st;
OMX_INIT_STRUCTURE (port_st);
port_st.nPortIndex = 201;
if ((error = OMX_GetParameter (encoder->handle, OMX_IndexParamPortDefinition,
&port_st))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
printf ("allocating %s output buffer\n", encoder->name);
if ((error = OMX_AllocateBuffer (encoder->handle, encoder_output_buffer, 201,
0, port_st.nBufferSize))){
fprintf (stderr, "error: OMX_AllocateBuffer: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
wait (encoder, EVENT_PORT_ENABLE, 0);
}
void disable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE* encoder_output_buffer){
//The port is not disabled until the buffer is released
OMX_ERRORTYPE error;
disable_port (encoder, 201);
//Free encoder output buffer
printf ("releasing %s output buffer\n", encoder->name);
if ((error = OMX_FreeBuffer (encoder->handle, 201, encoder_output_buffer))){
fprintf (stderr, "error: OMX_FreeBuffer: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
wait (encoder, EVENT_PORT_DISABLE, 0);
}
void set_camera_settings (component_t* camera){
printf ("configuring '%s' settings\n", camera->name);
OMX_ERRORTYPE error;
//Sharpness
OMX_CONFIG_SHARPNESSTYPE sharpness_st;
OMX_INIT_STRUCTURE (sharpness_st);
sharpness_st.nPortIndex = OMX_ALL;
sharpness_st.nSharpness = CAM_SHARPNESS;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonSharpness,
&sharpness_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Contrast
OMX_CONFIG_CONTRASTTYPE contrast_st;
OMX_INIT_STRUCTURE (contrast_st);
contrast_st.nPortIndex = OMX_ALL;
contrast_st.nContrast = CAM_CONTRAST;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonContrast,
&contrast_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Saturation
OMX_CONFIG_SATURATIONTYPE saturation_st;
OMX_INIT_STRUCTURE (saturation_st);
saturation_st.nPortIndex = OMX_ALL;
saturation_st.nSaturation = CAM_SATURATION;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonSaturation,
&saturation_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Brightness
OMX_CONFIG_BRIGHTNESSTYPE brightness_st;
OMX_INIT_STRUCTURE (brightness_st);
brightness_st.nPortIndex = OMX_ALL;
brightness_st.nBrightness = CAM_BRIGHTNESS;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonBrightness,
&brightness_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Exposure value
OMX_CONFIG_EXPOSUREVALUETYPE exposure_value_st;
OMX_INIT_STRUCTURE (exposure_value_st);
exposure_value_st.nPortIndex = OMX_ALL;
exposure_value_st.eMetering = CAM_METERING;
exposure_value_st.xEVCompensation =
(OMX_S32)((CAM_EXPOSURE_COMPENSATION<<16)/6.0);
exposure_value_st.nShutterSpeedMsec = (OMX_U32)((CAM_SHUTTER_SPEED)*1e6);
exposure_value_st.bAutoShutterSpeed = CAM_SHUTTER_SPEED_AUTO;
exposure_value_st.nSensitivity = CAM_ISO;
exposure_value_st.bAutoSensitivity = CAM_ISO_AUTO;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigCommonExposureValue, &exposure_value_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Exposure control
OMX_CONFIG_EXPOSURECONTROLTYPE exposure_control_st;
OMX_INIT_STRUCTURE (exposure_control_st);
exposure_control_st.nPortIndex = OMX_ALL;
exposure_control_st.eExposureControl = CAM_EXPOSURE;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonExposure,
&exposure_control_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Frame stabilisation
OMX_CONFIG_FRAMESTABTYPE frame_stabilisation_st;
OMX_INIT_STRUCTURE (frame_stabilisation_st);
frame_stabilisation_st.nPortIndex = OMX_ALL;
frame_stabilisation_st.bStab = CAM_FRAME_STABILIZATION;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigCommonFrameStabilisation, &frame_stabilisation_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//White balance
OMX_CONFIG_WHITEBALCONTROLTYPE white_balance_st;
OMX_INIT_STRUCTURE (white_balance_st);
white_balance_st.nPortIndex = OMX_ALL;
white_balance_st.eWhiteBalControl = CAM_WHITE_BALANCE;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonWhiteBalance,
&white_balance_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//White balance gains (if white balance is set to off)
if (!CAM_WHITE_BALANCE){
OMX_CONFIG_CUSTOMAWBGAINSTYPE white_balance_gains_st;
OMX_INIT_STRUCTURE (white_balance_gains_st);
white_balance_gains_st.xGainR = (CAM_WHITE_BALANCE_RED_GAIN << 16)/1000;
white_balance_gains_st.xGainB = (CAM_WHITE_BALANCE_BLUE_GAIN << 16)/1000;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCustomAwbGains,
&white_balance_gains_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
//Image filter
OMX_CONFIG_IMAGEFILTERTYPE image_filter_st;
OMX_INIT_STRUCTURE (image_filter_st);
image_filter_st.nPortIndex = OMX_ALL;
image_filter_st.eImageFilter = CAM_IMAGE_FILTER;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonImageFilter,
&image_filter_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Mirror
OMX_CONFIG_MIRRORTYPE mirror_st;
OMX_INIT_STRUCTURE (mirror_st);
mirror_st.nPortIndex = 71;
mirror_st.eMirror = CAM_MIRROR;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonMirror,
&mirror_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Rotation
OMX_CONFIG_ROTATIONTYPE rotation_st;
OMX_INIT_STRUCTURE (rotation_st);
rotation_st.nPortIndex = 71;
rotation_st.nRotation = CAM_ROTATION;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonRotate,
&rotation_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Color enhancement
OMX_CONFIG_COLORENHANCEMENTTYPE color_enhancement_st;
OMX_INIT_STRUCTURE (color_enhancement_st);
color_enhancement_st.nPortIndex = OMX_ALL;
color_enhancement_st.bColorEnhancement = CAM_COLOR_ENABLE;
color_enhancement_st.nCustomizedU = CAM_COLOR_U;
color_enhancement_st.nCustomizedV = CAM_COLOR_V;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigCommonColorEnhancement, &color_enhancement_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Denoise
OMX_CONFIG_BOOLEANTYPE denoise_st;
OMX_INIT_STRUCTURE (denoise_st);
denoise_st.bEnabled = CAM_NOISE_REDUCTION;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigStillColourDenoiseEnable, &denoise_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//ROI
OMX_CONFIG_INPUTCROPTYPE roi_st;
OMX_INIT_STRUCTURE (roi_st);
roi_st.nPortIndex = OMX_ALL;
roi_st.xLeft = (CAM_ROI_LEFT << 16)/100;
roi_st.xTop = (CAM_ROI_TOP << 16)/100;
roi_st.xWidth = (CAM_ROI_WIDTH << 16)/100;
roi_st.xHeight = (CAM_ROI_HEIGHT << 16)/100;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigInputCropPercentages, &roi_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//DRC
OMX_CONFIG_DYNAMICRANGEEXPANSIONTYPE drc_st;
OMX_INIT_STRUCTURE (drc_st);
drc_st.eMode = CAM_DRC;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigDynamicRangeExpansion, &drc_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void set_h264_settings (component_t* encoder){
printf ("configuring '%s' settings\n", encoder->name);
OMX_ERRORTYPE error;
if (!VIDEO_QP){
//Bitrate
OMX_VIDEO_PARAM_BITRATETYPE bitrate_st;
OMX_INIT_STRUCTURE (bitrate_st);
bitrate_st.eControlRate = OMX_Video_ControlRateVariable;
bitrate_st.nTargetBitrate = VIDEO_BITRATE;
bitrate_st.nPortIndex = 201;
if ((error = OMX_SetParameter (encoder->handle, OMX_IndexParamVideoBitrate,
&bitrate_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}else{
//Quantization parameters
OMX_VIDEO_PARAM_QUANTIZATIONTYPE quantization_st;
OMX_INIT_STRUCTURE (quantization_st);
quantization_st.nPortIndex = 201;
//nQpB returns an error, it cannot be modified
quantization_st.nQpI = VIDEO_QP_I;
quantization_st.nQpP = VIDEO_QP_P;
if ((error = OMX_SetParameter (encoder->handle,
OMX_IndexParamVideoQuantization, &quantization_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
//Codec
OMX_VIDEO_PARAM_PORTFORMATTYPE format_st;
OMX_INIT_STRUCTURE (format_st);
format_st.nPortIndex = 201;
//H.264/AVC
format_st.eCompressionFormat = OMX_VIDEO_CodingAVC;
if ((error = OMX_SetParameter (encoder->handle, OMX_IndexParamVideoPortFormat,
&format_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//IDR period
OMX_VIDEO_CONFIG_AVCINTRAPERIOD idr_st;
OMX_INIT_STRUCTURE (idr_st);
idr_st.nPortIndex = 201;
if ((error = OMX_GetConfig (encoder->handle,
OMX_IndexConfigVideoAVCIntraPeriod, &idr_st))){
fprintf (stderr, "error: OMX_GetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
idr_st.nIDRPeriod = VIDEO_IDR_PERIOD;
if ((error = OMX_SetConfig (encoder->handle,
OMX_IndexConfigVideoAVCIntraPeriod, &idr_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//SEI
OMX_PARAM_BRCMVIDEOAVCSEIENABLETYPE sei_st;
OMX_INIT_STRUCTURE (sei_st);
sei_st.nPortIndex = 201;
sei_st.bEnable = VIDEO_SEI;
if ((error = OMX_SetParameter (encoder->handle,
OMX_IndexParamBrcmVideoAVCSEIEnable, &sei_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//EEDE
OMX_VIDEO_EEDE_ENABLE eede_st;
OMX_INIT_STRUCTURE (eede_st);
eede_st.nPortIndex = 201;
eede_st.enable = VIDEO_EEDE;
if ((error = OMX_SetParameter (encoder->handle, OMX_IndexParamBrcmEEDEEnable,
&eede_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
OMX_VIDEO_EEDE_LOSSRATE eede_loss_rate_st;
OMX_INIT_STRUCTURE (eede_loss_rate_st);
eede_loss_rate_st.nPortIndex = 201;
eede_loss_rate_st.loss_rate = VIDEO_EEDE_LOSS_RATE;
if ((error = OMX_SetParameter (encoder->handle,
OMX_IndexParamBrcmEEDELossRate, &eede_loss_rate_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//AVC Profile
OMX_VIDEO_PARAM_AVCTYPE avc_st;
OMX_INIT_STRUCTURE (avc_st);
avc_st.nPortIndex = 201;
if ((error = OMX_GetParameter (encoder->handle,
OMX_IndexParamVideoAvc, &avc_st))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
avc_st.eProfile = VIDEO_PROFILE;
if ((error = OMX_SetParameter (encoder->handle,
OMX_IndexParamVideoAvc, &avc_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Inline SPS/PPS
OMX_CONFIG_PORTBOOLEANTYPE headers_st;
OMX_INIT_STRUCTURE (headers_st);
headers_st.nPortIndex = 201;
headers_st.bEnabled = VIDEO_INLINE_HEADERS;
if ((error = OMX_SetParameter (encoder->handle,
OMX_IndexParamBrcmVideoAVCInlineHeaderEnable, &headers_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Note: Motion vectors are not implemented in this program.
//See for further details
//https://github.com/gagle/raspberrypi-omxcam/blob/master/src/h264.c
//https://github.com/gagle/raspberrypi-omxcam/blob/master/src/video.c
}
int video_encode_test (){
/*
while (1){
//Get the buffer data
if ((error = OMX_FillThisBuffer (encoder.handle, encoder_output_buffer))){
fprintf (stderr, "error: OMX_FillThisBuffer: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Wait until it's filled
wait (&encoder, EVENT_FILL_BUFFER_DONE, 0);
//Append the buffer into the file
if (pwrite (fd, encoder_output_buffer->pBuffer,
encoder_output_buffer->nFilledLen,
encoder_output_buffer->nOffset) == -1){
fprintf (stderr, "error: pwrite\n");
exit (1);
}
clock_gettime (CLOCK_MONOTONIC, &spec);
if (spec.tv_sec*1000 + spec.tv_nsec/1.0e6 >= end) break;
}*/
if ((error = OMX_FillThisBuffer (encoder.handle, encoder_output_buffer))){
fprintf (stderr, "error: OMX_FillThisBuffer: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Wait until it's filled
wait (&encoder, EVENT_FILL_BUFFER_DONE, 0);
return 0;
}
void video_encode_init()
{
camera.name = "OMX.broadcom.camera";
encoder.name = "OMX.broadcom.video_encode";
null_sink.name = "OMX.broadcom.null_sink";
//Initialize Broadcom's VideoCore APIs
bcm_host_init ();
//Initialize OpenMAX IL
if ((error = OMX_Init ())){
fprintf (stderr, "error: OMX_Init: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Initialize components
init_component (&camera);
init_component (&encoder);
init_component (&null_sink);
//Initialize camera drivers
load_camera_drivers (&camera);
//Configure camera port definition
printf ("configuring %s port definition\n", camera.name);
OMX_PARAM_PORTDEFINITIONTYPE port_st;
OMX_INIT_STRUCTURE (port_st);
port_st.nPortIndex = 71;
if ((error = OMX_GetParameter (camera.handle, OMX_IndexParamPortDefinition,
&port_st))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
port_st.format.video.nFrameWidth = CAM_WIDTH;
port_st.format.video.nFrameHeight = CAM_HEIGHT;
port_st.format.video.nStride = CAM_WIDTH;
port_st.format.video.xFramerate = 0;//VIDEO_FRAMERATE << 16;
port_st.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
port_st.format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar;
if ((error = OMX_SetParameter (camera.handle, OMX_IndexParamPortDefinition,
&port_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Preview port
port_st.nPortIndex = 70;
if ((error = OMX_SetParameter (camera.handle, OMX_IndexParamPortDefinition,
&port_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
printf ("configuring %s framerate\n", camera.name);
OMX_CONFIG_FRAMERATETYPE framerate_st;
OMX_INIT_STRUCTURE (framerate_st);
framerate_st.nPortIndex = 71;
framerate_st.xEncodeFramerate = port_st.format.video.xFramerate;
if ((error = OMX_SetConfig (camera.handle, OMX_IndexConfigVideoFramerate,
&framerate_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Preview port
framerate_st.nPortIndex = 70;
if ((error = OMX_SetConfig (camera.handle, OMX_IndexConfigVideoFramerate,
&framerate_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Configure camera settings
set_camera_settings (&camera);
//Configure encoder port definition
printf ("configuring %s port definition\n", encoder.name);
OMX_INIT_STRUCTURE (port_st);
port_st.nPortIndex = 201;
if ((error = OMX_GetParameter (encoder.handle, OMX_IndexParamPortDefinition,
&port_st))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
port_st.format.video.nFrameWidth = CAM_WIDTH;
port_st.format.video.nFrameHeight = CAM_HEIGHT;
port_st.format.video.nStride = CAM_WIDTH;
port_st.format.video.xFramerate = VIDEO_FRAMERATE << 16;
//Despite being configured later, these two fields need to be set
port_st.format.video.nBitrate = VIDEO_QP ? 0 : VIDEO_BITRATE;
port_st.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
if ((error = OMX_SetParameter (encoder.handle, OMX_IndexParamPortDefinition,
&port_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Configure H264
set_h264_settings (&encoder);
//Setup tunnels: camera (video) -> video_encode, camera (preview) -> null_sink
printf ("configuring tunnels\n");
if ((error = OMX_SetupTunnel (camera.handle, 71, encoder.handle, 200))){
fprintf (stderr, "error: OMX_SetupTunnel: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
if ((error = OMX_SetupTunnel (camera.handle, 70, null_sink.handle, 240))){
fprintf (stderr, "error: OMX_SetupTunnel: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Change state to IDLE
change_state (&camera, OMX_StateIdle);
wait (&camera, EVENT_STATE_SET, 0);
change_state (&encoder, OMX_StateIdle);
wait (&encoder, EVENT_STATE_SET, 0);
change_state (&null_sink, OMX_StateIdle);
wait (&null_sink, EVENT_STATE_SET, 0);
//Enable the ports
enable_port (&camera, 71);
wait (&camera, EVENT_PORT_ENABLE, 0);
enable_port (&camera, 70);
wait (&camera, EVENT_PORT_ENABLE, 0);
enable_port (&null_sink, 240);
wait (&null_sink, EVENT_PORT_ENABLE, 0);
enable_port (&encoder, 200);
wait (&encoder, EVENT_PORT_ENABLE, 0);
enable_encoder_output_port (&encoder, &encoder_output_buffer);
//Change state to EXECUTING
change_state (&camera, OMX_StateExecuting);
wait (&camera, EVENT_STATE_SET, 0);
change_state (&encoder, OMX_StateExecuting);
wait (&encoder, EVENT_STATE_SET, 0);
wait (&encoder, EVENT_PORT_SETTINGS_CHANGED, 0);
change_state (&null_sink, OMX_StateExecuting);
wait (&null_sink, EVENT_STATE_SET, 0);
//Enable camera capture port. This basically says that the port 71 will be
//used to get data from the camera. If you're capturing a still, the port 72
//must be used
OMX_INIT_STRUCTURE (capture_st);
capture_st.nPortIndex = 71;
capture_st.bEnabled = OMX_TRUE;
if ((error = OMX_SetConfig (camera.handle, OMX_IndexConfigPortCapturing,
&capture_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void closeEncode()
{
//Disable camera capture port
printf ("disabling %s capture port\n", camera.name);
capture_st.bEnabled = OMX_FALSE;
if ((error = OMX_SetConfig (camera.handle, OMX_IndexConfigPortCapturing,
&capture_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Change state to IDLE
change_state (&camera, OMX_StateIdle);
wait (&camera, EVENT_STATE_SET, 0);
change_state (&encoder, OMX_StateIdle);
wait (&encoder, EVENT_STATE_SET, 0);
change_state (&null_sink, OMX_StateIdle);
wait (&null_sink, EVENT_STATE_SET, 0);
//Disable the tunnel ports
disable_port (&camera, 71);
wait (&camera, EVENT_PORT_DISABLE, 0);
disable_port (&camera, 70);
wait (&camera, EVENT_PORT_DISABLE, 0);
disable_port (&null_sink, 240);
wait (&null_sink, EVENT_PORT_DISABLE, 0);
disable_port (&encoder, 200);
wait (&encoder, EVENT_PORT_DISABLE, 0);
disable_encoder_output_port (&encoder, encoder_output_buffer);
//Change state to LOADED
change_state (&camera, OMX_StateLoaded);
wait (&camera, EVENT_STATE_SET, 0);
change_state (&encoder, OMX_StateLoaded);
wait (&encoder, EVENT_STATE_SET, 0);
change_state (&null_sink, OMX_StateLoaded);
wait (&null_sink, EVENT_STATE_SET, 0);
//Deinitialize components
deinit_component (&camera);
deinit_component (&encoder);
deinit_component (&null_sink);
//Deinitialize OpenMAX IL
if ((error = OMX_Deinit ())){
fprintf (stderr, "error: OMX_Deinit: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Deinitialize Broadcom's VideoCore APIs
bcm_host_deinit ();
printf (">>release resource done!\n");
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/secho1997/live_camera_linux.git
git@gitee.com:secho1997/live_camera_linux.git
secho1997
live_camera_linux
live_camera_linux
master

搜索帮助