30 #include <OMX_Component.h>
52 s.nLowPart =
value & 0xffffffff;
58 return (((int64_t)
value.nHighPart) << 32) |
value.nLowPart;
61 #define to_omx_ticks(x) (x)
62 #define from_omx_ticks(x) (x)
65 #define INIT_STRUCT(x) do { \
66 x.nSize = sizeof(x); \
67 x.nVersion = s->version; \
69 #define CHECK(x) do { \
70 if (x != OMX_ErrorNone) { \
71 av_log(avctx, AV_LOG_ERROR, \
72 "err %x (%d) on line %d\n", x, x, __LINE__); \
73 return AVERROR_UNKNOWN; \
83 OMX_ERRORTYPE (*
ptr_GetHandle)(OMX_HANDLETYPE*, OMX_STRING, OMX_PTR, OMX_CALLBACKTYPE*);
93 snprintf(buf,
sizeof(buf),
"%s%s", prefix ? prefix :
"", symbol);
94 return dlsym(handle, buf);
98 const char *libname,
const char *prefix,
102 s->lib2 = dlopen(libname2, RTLD_NOW | RTLD_GLOBAL);
107 s->host_init = dlsym(
s->lib2,
"bcm_host_init");
115 s->lib = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
122 s->ptr_ComponentNameEnum =
dlsym_prefixed(
s->lib,
"OMX_ComponentNameEnum", prefix);
125 s->ptr_GetComponentsOfRole =
dlsym_prefixed(
s->lib,
"OMX_GetComponentsOfRole", prefix);
126 s->ptr_GetRolesOfComponent =
dlsym_prefixed(
s->lib,
"OMX_GetRolesOfComponent", prefix);
127 if (!
s->ptr_Init || !
s->ptr_Deinit || !
s->ptr_ComponentNameEnum ||
128 !
s->ptr_GetHandle || !
s->ptr_FreeHandle ||
129 !
s->ptr_GetComponentsOfRole || !
s->ptr_GetRolesOfComponent) {
143 static const char *
const libnames[] = {
145 "/opt/vc/lib/libopenmaxil.so",
"/opt/vc/lib/libbcm_host.so",
147 "libOMX_Core.so",
NULL,
148 "libOmxCore.so",
NULL,
152 const char*
const* nameptr;
156 omx_context =
av_mallocz(
sizeof(*omx_context));
166 for (nameptr = libnames; *nameptr; nameptr += 2)
167 if (!(
ret =
omx_try_load(omx_context, logctx, nameptr[0], prefix, nameptr[1])))
186 dlclose(omx_context->
lib);
233 #define NB_MUTEX_CONDS 6
234 #define OFF(field) offsetof(OMXCodecContext, field)
236 (
OFF(input_mutex),
OFF(output_mutex),
OFF(state_mutex)),
237 (
OFF(input_cond),
OFF(output_cond),
OFF(state_cond)));
240 int* array_size, OMX_BUFFERHEADERTYPE **
array,
241 OMX_BUFFERHEADERTYPE *
buffer)
250 int* array_size, OMX_BUFFERHEADERTYPE **
array,
253 OMX_BUFFERHEADERTYPE *
buffer;
259 if (*array_size > 0) {
262 memmove(&
array[0], &
array[1], (*array_size) *
sizeof(OMX_BUFFERHEADERTYPE*));
270 static OMX_ERRORTYPE
event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event,
271 OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data)
285 case OMX_EventCmdComplete:
286 if (data1 == OMX_CommandStateSet) {
292 }
else if (data1 == OMX_CommandPortDisable) {
294 }
else if (data1 == OMX_CommandPortEnable) {
298 (uint32_t) data1, (uint32_t) data2);
301 case OMX_EventPortSettingsChanged:
306 event, (uint32_t) data1, (uint32_t) data2);
309 return OMX_ErrorNone;
313 OMX_BUFFERHEADERTYPE *
buffer)
316 if (
s->input_zerocopy) {
317 if (
buffer->pAppPrivate) {
318 if (
buffer->pOutputPortPrivate)
326 &
s->num_free_in_buffers,
s->free_in_buffers,
buffer);
327 return OMX_ErrorNone;
331 OMX_BUFFERHEADERTYPE *
buffer)
335 &
s->num_done_out_buffers,
s->done_out_buffers,
buffer);
336 return OMX_ErrorNone;
346 const char *role,
char *str,
int str_size)
354 av_strlcpy(str,
"OMX.broadcom.video_encode", str_size);
363 components =
av_calloc(num,
sizeof(*components));
366 for (
i = 0;
i < num;
i++) {
367 components[
i] =
av_mallocz(OMX_MAX_STRINGNAME_SIZE);
368 if (!components[
i]) {
376 for (
i = 0;
i < num;
i++)
386 while (
s->state !=
state &&
s->error == OMX_ErrorNone)
388 if (
s->error != OMX_ErrorNone)
397 OMX_PARAM_COMPONENTROLETYPE role_params = { 0 };
398 OMX_PORT_PARAM_TYPE video_port_params = { 0 };
399 OMX_PARAM_PORTDEFINITIONTYPE in_port_params = { 0 }, out_port_params = { 0 };
400 OMX_VIDEO_PARAM_PORTFORMATTYPE video_port_format = { 0 };
401 OMX_VIDEO_PARAM_BITRATETYPE vid_param_bitrate = { 0 };
405 s->version.s.nVersionMajor = 1;
406 s->version.s.nVersionMinor = 1;
407 s->version.s.nRevision = 2;
409 err =
s->omx_context->ptr_GetHandle(&
s->handle,
s->component_name,
s, (OMX_CALLBACKTYPE*) &
callbacks);
410 if (err != OMX_ErrorNone) {
417 av_strlcpy(role_params.cRole, role,
sizeof(role_params.cRole));
419 OMX_SetParameter(
s->handle, OMX_IndexParamStandardComponentRole, &role_params);
422 err = OMX_GetParameter(
s->handle, OMX_IndexParamVideoInit, &video_port_params);
425 s->in_port =
s->out_port = -1;
426 for (
i = 0;
i < video_port_params.nPorts;
i++) {
427 int port = video_port_params.nStartPortNumber +
i;
428 OMX_PARAM_PORTDEFINITIONTYPE port_params = { 0 };
430 port_params.nPortIndex = port;
431 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &port_params);
432 if (err != OMX_ErrorNone) {
436 if (port_params.eDir == OMX_DirInput &&
s->in_port < 0) {
437 in_port_params = port_params;
439 }
else if (port_params.eDir == OMX_DirOutput &&
s->out_port < 0) {
440 out_port_params = port_params;
444 if (
s->in_port < 0 ||
s->out_port < 0) {
445 av_log(avctx,
AV_LOG_ERROR,
"No in or out port found (in %d out %d)\n",
s->in_port,
s->out_port);
452 video_port_format.nIndex =
i;
453 video_port_format.nPortIndex =
s->in_port;
454 if (OMX_GetParameter(
s->handle, OMX_IndexParamVideoPortFormat, &video_port_format) != OMX_ErrorNone)
456 if (video_port_format.eColorFormat == OMX_COLOR_FormatYUV420Planar ||
457 video_port_format.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar) {
458 s->color_format = video_port_format.eColorFormat;
462 if (
s->color_format == 0) {
467 in_port_params.bEnabled = OMX_TRUE;
468 in_port_params.bPopulated = OMX_FALSE;
469 in_port_params.eDomain = OMX_PortDomainVideo;
471 in_port_params.format.video.pNativeRender =
NULL;
472 in_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
473 in_port_params.format.video.eColorFormat =
s->color_format;
478 in_port_params.format.video.nStride =
s->stride;
479 in_port_params.format.video.nSliceHeight =
s->plane_size;
480 in_port_params.format.video.nFrameWidth = avctx->
width;
481 in_port_params.format.video.nFrameHeight = avctx->
height;
487 err = OMX_SetParameter(
s->handle, OMX_IndexParamPortDefinition, &in_port_params);
489 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &in_port_params);
491 s->stride = in_port_params.format.video.nStride;
492 s->plane_size = in_port_params.format.video.nSliceHeight;
493 s->num_in_buffers = in_port_params.nBufferCountActual;
495 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &out_port_params);
496 out_port_params.bEnabled = OMX_TRUE;
497 out_port_params.bPopulated = OMX_FALSE;
498 out_port_params.eDomain = OMX_PortDomainVideo;
499 out_port_params.format.video.pNativeRender =
NULL;
500 out_port_params.format.video.nFrameWidth = avctx->
width;
501 out_port_params.format.video.nFrameHeight = avctx->
height;
502 out_port_params.format.video.nStride = 0;
503 out_port_params.format.video.nSliceHeight = 0;
504 out_port_params.format.video.nBitrate = avctx->
bit_rate;
505 out_port_params.format.video.xFramerate = in_port_params.format.video.xFramerate;
506 out_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
508 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
510 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
512 err = OMX_SetParameter(
s->handle, OMX_IndexParamPortDefinition, &out_port_params);
514 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &out_port_params);
516 s->num_out_buffers = out_port_params.nBufferCountActual;
519 vid_param_bitrate.nPortIndex =
s->out_port;
520 vid_param_bitrate.eControlRate = OMX_Video_ControlRateVariable;
521 vid_param_bitrate.nTargetBitrate = avctx->
bit_rate;
522 err = OMX_SetParameter(
s->handle, OMX_IndexParamVideoBitrate, &vid_param_bitrate);
523 if (err != OMX_ErrorNone)
527 OMX_VIDEO_PARAM_AVCTYPE avc = { 0 };
529 avc.nPortIndex =
s->out_port;
530 err = OMX_GetParameter(
s->handle, OMX_IndexParamVideoAvc, &avc);
536 avc.eProfile = OMX_VIDEO_AVCProfileBaseline;
539 avc.eProfile = OMX_VIDEO_AVCProfileMain;
542 avc.eProfile = OMX_VIDEO_AVCProfileHigh;
547 err = OMX_SetParameter(
s->handle, OMX_IndexParamVideoAvc, &avc);
551 err = OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateIdle,
NULL);
554 s->in_buffer_headers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_in_buffers);
555 s->free_in_buffers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_in_buffers);
556 s->out_buffer_headers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_out_buffers);
557 s->done_out_buffers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_out_buffers);
558 if (!
s->in_buffer_headers || !
s->free_in_buffers || !
s->out_buffer_headers || !
s->done_out_buffers)
560 for (
i = 0;
i <
s->num_in_buffers && err == OMX_ErrorNone;
i++) {
561 if (
s->input_zerocopy)
562 err = OMX_UseBuffer(
s->handle, &
s->in_buffer_headers[
i],
s->in_port,
s, in_port_params.nBufferSize,
NULL);
564 err = OMX_AllocateBuffer(
s->handle, &
s->in_buffer_headers[
i],
s->in_port,
s, in_port_params.nBufferSize);
565 if (err == OMX_ErrorNone)
566 s->in_buffer_headers[
i]->pAppPrivate =
s->in_buffer_headers[
i]->pOutputPortPrivate =
NULL;
569 s->num_in_buffers =
i;
570 for (
i = 0;
i <
s->num_out_buffers && err == OMX_ErrorNone;
i++)
571 err = OMX_AllocateBuffer(
s->handle, &
s->out_buffer_headers[
i],
s->out_port,
s, out_port_params.nBufferSize);
573 s->num_out_buffers =
i;
579 err = OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateExecuting,
NULL);
586 for (
i = 0;
i <
s->num_out_buffers && err == OMX_ErrorNone;
i++)
587 err = OMX_FillThisBuffer(
s->handle,
s->out_buffer_headers[
i]);
588 if (err != OMX_ErrorNone) {
589 for (;
i <
s->num_out_buffers;
i++)
590 s->done_out_buffers[
s->num_done_out_buffers++] =
s->out_buffer_headers[
i];
592 for (
i = 0;
i <
s->num_in_buffers;
i++)
593 s->free_in_buffers[
s->num_free_in_buffers++] =
s->in_buffer_headers[
i];
605 executing =
s->state == OMX_StateExecuting;
609 OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateIdle,
NULL);
611 OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateLoaded,
NULL);
612 for (
int i = 0;
i <
s->num_in_buffers;
i++) {
614 &
s->num_free_in_buffers,
s->free_in_buffers, 1);
615 if (
s->input_zerocopy)
617 OMX_FreeBuffer(
s->handle,
s->in_port,
buffer);
619 for (
int i = 0;
i <
s->num_out_buffers;
i++) {
621 &
s->num_done_out_buffers,
s->done_out_buffers, 1);
622 OMX_FreeBuffer(
s->handle,
s->out_port,
buffer);
627 s->omx_context->ptr_FreeHandle(
s->handle);
632 s->omx_context =
NULL;
647 OMX_BUFFERHEADERTYPE *
buffer;
654 s->omx_context =
omx_init(avctx,
s->libname,
s->libprefix);
659 s->state = OMX_StateLoaded;
660 s->error = OMX_ErrorNone;
664 role =
"video_encoder.mpeg4";
667 role =
"video_encoder.avc";
673 if ((
ret =
find_component(
s->omx_context, avctx, role,
s->component_name,
sizeof(
s->component_name))) < 0)
684 &
s->num_done_out_buffers,
s->done_out_buffers, 1);
685 if (
buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
694 err = OMX_FillThisBuffer(
s->handle,
buffer);
695 if (err != OMX_ErrorNone) {
697 &
s->num_done_out_buffers,
s->done_out_buffers,
buffer);
706 int nals[32] = { 0 };
736 OMX_BUFFERHEADERTYPE*
buffer;
745 &
s->num_free_in_buffers,
s->free_in_buffers, 1);
749 if (
s->input_zerocopy) {
767 buffer->pAppPrivate = local;
777 if (image_buffer_size >= 0)
784 buffer->pAppPrivate = buf;
786 buffer->pOutputPortPrivate = (
void*) 1;
798 buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
805 OMX_CONFIG_BOOLEANTYPE
config = {0, };
807 config.bEnabled = OMX_TRUE;
808 err = OMX_SetConfig(
s->handle, OMX_IndexConfigBrcmVideoRequestIFrame, &
config);
809 if (err != OMX_ErrorNone) {
813 OMX_CONFIG_INTRAREFRESHVOPTYPE
config = {0, };
815 config.nPortIndex =
s->out_port;
816 config.IntraRefreshVOP = OMX_TRUE;
817 err = OMX_SetConfig(
s->handle, OMX_IndexConfigVideoIntraVOPRefresh, &
config);
818 if (err != OMX_ErrorNone) {
823 err = OMX_EmptyThisBuffer(
s->handle,
buffer);
824 if (err != OMX_ErrorNone) {
829 }
else if (!
s->eos_sent) {
831 &
s->num_free_in_buffers,
s->free_in_buffers, 1);
834 buffer->nFlags = OMX_BUFFERFLAG_EOS;
836 err = OMX_EmptyThisBuffer(
s->handle,
buffer);
837 if (err != OMX_ErrorNone) {
845 while (!*got_packet &&
ret == 0 && !
s->got_eos) {
850 &
s->num_done_out_buffers,
s->done_out_buffers,
851 !
frame || had_partial);
855 if (
buffer->nFlags & OMX_BUFFERFLAG_EOS)
869 s->output_buf_size = 0;
872 memcpy(
s->output_buf +
s->output_buf_size,
buffer->pBuffer +
buffer->nOffset,
buffer->nFilledLen);
873 s->output_buf_size +=
buffer->nFilledLen;
874 if (
buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
878 s->output_buf_size = 0;
881 s->output_buf =
NULL;
882 s->output_buf_size = 0;
888 if (
buffer->nFlags & OMX_BUFFERFLAG_SYNCFRAME)
898 err = OMX_FillThisBuffer(
s->handle,
buffer);
899 if (err != OMX_ErrorNone) {
916 #define OFFSET(x) offsetof(OMXCodecContext, x)
917 #define VDE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM
918 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
922 {
"zerocopy",
"Try to avoid copying input frames if possible",
OFFSET(input_zerocopy),
AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1,
VE },
941 .
p.
name =
"mpeg4_omx",
962 .
p.
name =
"h264_omx",