30 #include <OMX_Component.h>
51 s.nLowPart =
value & 0xffffffff;
57 return (((int64_t)
value.nHighPart) << 32) |
value.nLowPart;
60 #define to_omx_ticks(x) (x)
61 #define from_omx_ticks(x) (x)
64 #define INIT_STRUCT(x) do { \
65 x.nSize = sizeof(x); \
66 x.nVersion = s->version; \
68 #define CHECK(x) do { \
69 if (x != OMX_ErrorNone) { \
70 av_log(avctx, AV_LOG_ERROR, \
71 "err %x (%d) on line %d\n", x, x, __LINE__); \
72 return AVERROR_UNKNOWN; \
82 OMX_ERRORTYPE (*
ptr_GetHandle)(OMX_HANDLETYPE*, OMX_STRING, OMX_PTR, OMX_CALLBACKTYPE*);
92 snprintf(buf,
sizeof(buf),
"%s%s", prefix ? prefix :
"", symbol);
93 return dlsym(handle, buf);
97 const char *libname,
const char *prefix,
101 s->lib2 = dlopen(libname2, RTLD_NOW | RTLD_GLOBAL);
106 s->host_init = dlsym(
s->lib2,
"bcm_host_init");
114 s->lib = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
121 s->ptr_ComponentNameEnum =
dlsym_prefixed(
s->lib,
"OMX_ComponentNameEnum", prefix);
124 s->ptr_GetComponentsOfRole =
dlsym_prefixed(
s->lib,
"OMX_GetComponentsOfRole", prefix);
125 s->ptr_GetRolesOfComponent =
dlsym_prefixed(
s->lib,
"OMX_GetRolesOfComponent", prefix);
126 if (!
s->ptr_Init || !
s->ptr_Deinit || !
s->ptr_ComponentNameEnum ||
127 !
s->ptr_GetHandle || !
s->ptr_FreeHandle ||
128 !
s->ptr_GetComponentsOfRole || !
s->ptr_GetRolesOfComponent) {
142 static const char *
const libnames[] = {
144 "/opt/vc/lib/libopenmaxil.so",
"/opt/vc/lib/libbcm_host.so",
146 "libOMX_Core.so",
NULL,
147 "libOmxCore.so",
NULL,
151 const char*
const* nameptr;
155 omx_context =
av_mallocz(
sizeof(*omx_context));
165 for (nameptr = libnames; *nameptr; nameptr += 2)
166 if (!(
ret =
omx_try_load(omx_context, logctx, nameptr[0], prefix, nameptr[1])))
185 dlclose(omx_context->
lib);
233 int* array_size, OMX_BUFFERHEADERTYPE **
array,
234 OMX_BUFFERHEADERTYPE *
buffer)
243 int* array_size, OMX_BUFFERHEADERTYPE **
array,
246 OMX_BUFFERHEADERTYPE *
buffer;
252 if (*array_size > 0) {
255 memmove(&
array[0], &
array[1], (*array_size) *
sizeof(OMX_BUFFERHEADERTYPE*));
263 static OMX_ERRORTYPE
event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event,
264 OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data)
278 case OMX_EventCmdComplete:
279 if (data1 == OMX_CommandStateSet) {
285 }
else if (data1 == OMX_CommandPortDisable) {
287 }
else if (data1 == OMX_CommandPortEnable) {
291 (uint32_t) data1, (uint32_t) data2);
294 case OMX_EventPortSettingsChanged:
299 event, (uint32_t) data1, (uint32_t) data2);
302 return OMX_ErrorNone;
306 OMX_BUFFERHEADERTYPE *
buffer)
309 if (
s->input_zerocopy) {
310 if (
buffer->pAppPrivate) {
311 if (
buffer->pOutputPortPrivate)
319 &
s->num_free_in_buffers,
s->free_in_buffers,
buffer);
320 return OMX_ErrorNone;
324 OMX_BUFFERHEADERTYPE *
buffer)
328 &
s->num_done_out_buffers,
s->done_out_buffers,
buffer);
329 return OMX_ErrorNone;
339 const char *role,
char *
str,
int str_size)
359 for (
i = 0;
i < num;
i++) {
360 components[
i] =
av_mallocz(OMX_MAX_STRINGNAME_SIZE);
361 if (!components[
i]) {
369 for (
i = 0;
i < num;
i++)
379 while (
s->state !=
state &&
s->error == OMX_ErrorNone)
381 if (
s->error != OMX_ErrorNone)
390 OMX_PARAM_COMPONENTROLETYPE role_params = { 0 };
391 OMX_PORT_PARAM_TYPE video_port_params = { 0 };
392 OMX_PARAM_PORTDEFINITIONTYPE in_port_params = { 0 }, out_port_params = { 0 };
393 OMX_VIDEO_PARAM_PORTFORMATTYPE video_port_format = { 0 };
394 OMX_VIDEO_PARAM_BITRATETYPE vid_param_bitrate = { 0 };
398 s->version.s.nVersionMajor = 1;
399 s->version.s.nVersionMinor = 1;
400 s->version.s.nRevision = 2;
402 err =
s->omx_context->ptr_GetHandle(&
s->handle,
s->component_name,
s, (OMX_CALLBACKTYPE*) &
callbacks);
403 if (err != OMX_ErrorNone) {
410 av_strlcpy(role_params.cRole, role,
sizeof(role_params.cRole));
412 OMX_SetParameter(
s->handle, OMX_IndexParamStandardComponentRole, &role_params);
415 err = OMX_GetParameter(
s->handle, OMX_IndexParamVideoInit, &video_port_params);
418 s->in_port =
s->out_port = -1;
419 for (
i = 0;
i < video_port_params.nPorts;
i++) {
420 int port = video_port_params.nStartPortNumber +
i;
421 OMX_PARAM_PORTDEFINITIONTYPE port_params = { 0 };
423 port_params.nPortIndex = port;
424 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &port_params);
425 if (err != OMX_ErrorNone) {
429 if (port_params.eDir == OMX_DirInput &&
s->in_port < 0) {
430 in_port_params = port_params;
432 }
else if (port_params.eDir == OMX_DirOutput &&
s->out_port < 0) {
433 out_port_params = port_params;
437 if (
s->in_port < 0 ||
s->out_port < 0) {
438 av_log(avctx,
AV_LOG_ERROR,
"No in or out port found (in %d out %d)\n",
s->in_port,
s->out_port);
445 video_port_format.nIndex =
i;
446 video_port_format.nPortIndex =
s->in_port;
447 if (OMX_GetParameter(
s->handle, OMX_IndexParamVideoPortFormat, &video_port_format) != OMX_ErrorNone)
449 if (video_port_format.eColorFormat == OMX_COLOR_FormatYUV420Planar ||
450 video_port_format.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar) {
451 s->color_format = video_port_format.eColorFormat;
455 if (
s->color_format == 0) {
460 in_port_params.bEnabled = OMX_TRUE;
461 in_port_params.bPopulated = OMX_FALSE;
462 in_port_params.eDomain = OMX_PortDomainVideo;
464 in_port_params.format.video.pNativeRender =
NULL;
465 in_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
466 in_port_params.format.video.eColorFormat =
s->color_format;
471 in_port_params.format.video.nStride =
s->stride;
472 in_port_params.format.video.nSliceHeight =
s->plane_size;
473 in_port_params.format.video.nFrameWidth = avctx->
width;
474 in_port_params.format.video.nFrameHeight = avctx->
height;
480 err = OMX_SetParameter(
s->handle, OMX_IndexParamPortDefinition, &in_port_params);
482 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &in_port_params);
484 s->stride = in_port_params.format.video.nStride;
485 s->plane_size = in_port_params.format.video.nSliceHeight;
486 s->num_in_buffers = in_port_params.nBufferCountActual;
488 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &out_port_params);
489 out_port_params.bEnabled = OMX_TRUE;
490 out_port_params.bPopulated = OMX_FALSE;
491 out_port_params.eDomain = OMX_PortDomainVideo;
492 out_port_params.format.video.pNativeRender =
NULL;
493 out_port_params.format.video.nFrameWidth = avctx->
width;
494 out_port_params.format.video.nFrameHeight = avctx->
height;
495 out_port_params.format.video.nStride = 0;
496 out_port_params.format.video.nSliceHeight = 0;
497 out_port_params.format.video.nBitrate = avctx->
bit_rate;
498 out_port_params.format.video.xFramerate = in_port_params.format.video.xFramerate;
499 out_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
501 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
503 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
505 err = OMX_SetParameter(
s->handle, OMX_IndexParamPortDefinition, &out_port_params);
507 err = OMX_GetParameter(
s->handle, OMX_IndexParamPortDefinition, &out_port_params);
509 s->num_out_buffers = out_port_params.nBufferCountActual;
512 vid_param_bitrate.nPortIndex =
s->out_port;
513 vid_param_bitrate.eControlRate = OMX_Video_ControlRateVariable;
514 vid_param_bitrate.nTargetBitrate = avctx->
bit_rate;
515 err = OMX_SetParameter(
s->handle, OMX_IndexParamVideoBitrate, &vid_param_bitrate);
516 if (err != OMX_ErrorNone)
520 OMX_VIDEO_PARAM_AVCTYPE avc = { 0 };
522 avc.nPortIndex =
s->out_port;
523 err = OMX_GetParameter(
s->handle, OMX_IndexParamVideoAvc, &avc);
529 avc.eProfile = OMX_VIDEO_AVCProfileBaseline;
532 avc.eProfile = OMX_VIDEO_AVCProfileMain;
535 avc.eProfile = OMX_VIDEO_AVCProfileHigh;
540 err = OMX_SetParameter(
s->handle, OMX_IndexParamVideoAvc, &avc);
544 err = OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateIdle,
NULL);
547 s->in_buffer_headers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_in_buffers);
548 s->free_in_buffers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_in_buffers);
549 s->out_buffer_headers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_out_buffers);
550 s->done_out_buffers =
av_mallocz(
sizeof(OMX_BUFFERHEADERTYPE*) *
s->num_out_buffers);
551 if (!
s->in_buffer_headers || !
s->free_in_buffers || !
s->out_buffer_headers || !
s->done_out_buffers)
553 for (
i = 0;
i <
s->num_in_buffers && err == OMX_ErrorNone;
i++) {
554 if (
s->input_zerocopy)
555 err = OMX_UseBuffer(
s->handle, &
s->in_buffer_headers[
i],
s->in_port,
s, in_port_params.nBufferSize,
NULL);
557 err = OMX_AllocateBuffer(
s->handle, &
s->in_buffer_headers[
i],
s->in_port,
s, in_port_params.nBufferSize);
558 if (err == OMX_ErrorNone)
559 s->in_buffer_headers[
i]->pAppPrivate =
s->in_buffer_headers[
i]->pOutputPortPrivate =
NULL;
562 s->num_in_buffers =
i;
563 for (
i = 0;
i <
s->num_out_buffers && err == OMX_ErrorNone;
i++)
564 err = OMX_AllocateBuffer(
s->handle, &
s->out_buffer_headers[
i],
s->out_port,
s, out_port_params.nBufferSize);
566 s->num_out_buffers =
i;
572 err = OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateExecuting,
NULL);
579 for (
i = 0;
i <
s->num_out_buffers && err == OMX_ErrorNone;
i++)
580 err = OMX_FillThisBuffer(
s->handle,
s->out_buffer_headers[
i]);
581 if (err != OMX_ErrorNone) {
582 for (;
i <
s->num_out_buffers;
i++)
583 s->done_out_buffers[
s->num_done_out_buffers++] =
s->out_buffer_headers[
i];
585 for (
i = 0;
i <
s->num_in_buffers;
i++)
586 s->free_in_buffers[
s->num_free_in_buffers++] =
s->in_buffer_headers[
i];
595 executing =
s->state == OMX_StateExecuting;
599 OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateIdle,
NULL);
601 OMX_SendCommand(
s->handle, OMX_CommandStateSet, OMX_StateLoaded,
NULL);
602 for (
i = 0;
i <
s->num_in_buffers;
i++) {
604 &
s->num_free_in_buffers,
s->free_in_buffers, 1);
605 if (
s->input_zerocopy)
607 OMX_FreeBuffer(
s->handle,
s->in_port,
buffer);
609 for (
i = 0;
i <
s->num_out_buffers;
i++) {
611 &
s->num_done_out_buffers,
s->done_out_buffers, 1);
612 OMX_FreeBuffer(
s->handle,
s->out_port,
buffer);
617 s->omx_context->ptr_FreeHandle(
s->handle);
622 s->omx_context =
NULL;
623 if (
s->mutex_cond_inited) {
630 s->mutex_cond_inited = 0;
644 OMX_BUFFERHEADERTYPE *
buffer;
647 s->omx_context =
omx_init(avctx,
s->libname,
s->libprefix);
657 s->mutex_cond_inited = 1;
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) {
753 if (
frame->linesize[0] == src_linesize[0] &&
754 frame->linesize[1] == src_linesize[1] &&
755 frame->linesize[2] == src_linesize[2] &&
767 buffer->pAppPrivate = local;
777 if (image_buffer_size >= 0)
784 buffer->pAppPrivate = buf;
786 buffer->pOutputPortPrivate = (
void*) 1;
797 buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
804 OMX_CONFIG_BOOLEANTYPE config = {0, };
806 config.bEnabled = OMX_TRUE;
807 err = OMX_SetConfig(
s->handle, OMX_IndexConfigBrcmVideoRequestIFrame, &config);
808 if (err != OMX_ErrorNone) {
812 OMX_CONFIG_INTRAREFRESHVOPTYPE config = {0, };
814 config.nPortIndex =
s->out_port;
815 config.IntraRefreshVOP = OMX_TRUE;
816 err = OMX_SetConfig(
s->handle, OMX_IndexConfigVideoIntraVOPRefresh, &config);
817 if (err != OMX_ErrorNone) {
822 err = OMX_EmptyThisBuffer(
s->handle,
buffer);
823 if (err != OMX_ErrorNone) {
828 }
else if (!
s->eos_sent) {
830 &
s->num_free_in_buffers,
s->free_in_buffers, 1);
833 buffer->nFlags = OMX_BUFFERFLAG_EOS;
835 err = OMX_EmptyThisBuffer(
s->handle,
buffer);
836 if (err != OMX_ErrorNone) {
844 while (!*got_packet &&
ret == 0 && !
s->got_eos) {
849 &
s->num_done_out_buffers,
s->done_out_buffers,
850 !
frame || had_partial);
854 if (
buffer->nFlags & OMX_BUFFERFLAG_EOS)
866 if (!(
buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) || !
pkt->
data) {
871 s->output_buf_size = 0;
874 memcpy(
s->output_buf +
s->output_buf_size,
buffer->pBuffer +
buffer->nOffset,
buffer->nFilledLen);
875 s->output_buf_size +=
buffer->nFilledLen;
876 if (
buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
879 s->output_buf_size = 0;
882 s->output_buf =
NULL;
883 s->output_buf_size = 0;
892 (
int)(
s->output_buf_size +
buffer->nFilledLen));
895 memcpy(
pkt->
data,
s->output_buf,
s->output_buf_size);
898 s->output_buf_size = 0;
900 if (
buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
906 if (
buffer->nFlags & OMX_BUFFERFLAG_SYNCFRAME)
912 err = OMX_FillThisBuffer(
s->handle,
buffer);
913 if (err != OMX_ErrorNone) {
930 #define OFFSET(x) offsetof(OMXCodecContext, x)
931 #define VDE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM
932 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
936 {
"zerocopy",
"Try to avoid copying input frames if possible",
OFFSET(input_zerocopy),
AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1,
VE },