30 #include <OMX_Component.h>
51 s.nLowPart = value & 0xffffffff;
52 s.nHighPart = value >> 32;
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);
114 s->
lib = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
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);
232 int* array_size, OMX_BUFFERHEADERTYPE **
array,
233 OMX_BUFFERHEADERTYPE *
buffer)
236 array[(*array_size)++] =
buffer;
242 int* array_size, OMX_BUFFERHEADERTYPE **
array,
245 OMX_BUFFERHEADERTYPE *
buffer;
251 if (*array_size > 0) {
254 memmove(&array[0], &array[1], (*array_size) *
sizeof(OMX_BUFFERHEADERTYPE*));
262 static OMX_ERRORTYPE
event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event,
263 OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data)
277 case OMX_EventCmdComplete:
278 if (data1 == OMX_CommandStateSet) {
284 }
else if (data1 == OMX_CommandPortDisable) {
286 }
else if (data1 == OMX_CommandPortEnable) {
290 (uint32_t) data1, (uint32_t) data2);
293 case OMX_EventPortSettingsChanged:
298 event, (uint32_t) data1, (uint32_t) data2);
301 return OMX_ErrorNone;
305 OMX_BUFFERHEADERTYPE *
buffer)
309 if (buffer->pAppPrivate) {
310 if (buffer->pOutputPortPrivate)
314 buffer->pAppPrivate =
NULL;
319 return OMX_ErrorNone;
323 OMX_BUFFERHEADERTYPE *
buffer)
328 return OMX_ErrorNone;
338 const char *role,
char *str,
int str_size)
346 av_strlcpy(str,
"OMX.broadcom.video_encode", str_size);
358 for (i = 0; i < num; i++) {
359 components[i] =
av_mallocz(OMX_MAX_STRINGNAME_SIZE);
368 for (i = 0; i < num; i++)
378 while (s->
state != state && s->
error == OMX_ErrorNone)
380 if (s->
error != OMX_ErrorNone)
389 OMX_PARAM_COMPONENTROLETYPE role_params = { 0 };
390 OMX_PORT_PARAM_TYPE video_port_params = { 0 };
391 OMX_PARAM_PORTDEFINITIONTYPE in_port_params = { 0 }, out_port_params = { 0 };
392 OMX_VIDEO_PARAM_PORTFORMATTYPE video_port_format = { 0 };
393 OMX_VIDEO_PARAM_BITRATETYPE vid_param_bitrate = { 0 };
397 s->
version.s.nVersionMajor = 1;
398 s->
version.s.nVersionMinor = 1;
402 if (err != OMX_ErrorNone) {
409 av_strlcpy(role_params.cRole, role,
sizeof(role_params.cRole));
411 OMX_SetParameter(s->
handle, OMX_IndexParamStandardComponentRole, &role_params);
414 err = OMX_GetParameter(s->
handle, OMX_IndexParamVideoInit, &video_port_params);
418 for (i = 0; i < video_port_params.nPorts; i++) {
419 int port = video_port_params.nStartPortNumber + i;
420 OMX_PARAM_PORTDEFINITIONTYPE port_params = { 0 };
422 port_params.nPortIndex = port;
423 err = OMX_GetParameter(s->
handle, OMX_IndexParamPortDefinition, &port_params);
424 if (err != OMX_ErrorNone) {
428 if (port_params.eDir == OMX_DirInput && s->
in_port < 0) {
429 in_port_params = port_params;
431 }
else if (port_params.eDir == OMX_DirOutput && s->
out_port < 0) {
432 out_port_params = port_params;
444 video_port_format.nIndex = i;
445 video_port_format.nPortIndex = s->
in_port;
446 if (OMX_GetParameter(s->
handle, OMX_IndexParamVideoPortFormat, &video_port_format) != OMX_ErrorNone)
448 if (video_port_format.eColorFormat == OMX_COLOR_FormatYUV420Planar ||
449 video_port_format.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar) {
455 av_log(avctx,
AV_LOG_ERROR,
"No supported pixel formats (%d formats available)\n", i);
459 in_port_params.bEnabled = OMX_TRUE;
460 in_port_params.bPopulated = OMX_FALSE;
461 in_port_params.eDomain = OMX_PortDomainVideo;
463 in_port_params.format.video.pNativeRender =
NULL;
464 in_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
465 in_port_params.format.video.eColorFormat = s->
color_format;
470 in_port_params.format.video.nStride = s->
stride;
471 in_port_params.format.video.nSliceHeight = s->
plane_size;
472 in_port_params.format.video.nFrameWidth = avctx->
width;
473 in_port_params.format.video.nFrameHeight = avctx->
height;
479 err = OMX_SetParameter(s->
handle, OMX_IndexParamPortDefinition, &in_port_params);
481 err = OMX_GetParameter(s->
handle, OMX_IndexParamPortDefinition, &in_port_params);
483 s->
stride = in_port_params.format.video.nStride;
484 s->
plane_size = in_port_params.format.video.nSliceHeight;
487 err = OMX_GetParameter(s->
handle, OMX_IndexParamPortDefinition, &out_port_params);
488 out_port_params.bEnabled = OMX_TRUE;
489 out_port_params.bPopulated = OMX_FALSE;
490 out_port_params.eDomain = OMX_PortDomainVideo;
491 out_port_params.format.video.pNativeRender =
NULL;
492 out_port_params.format.video.nFrameWidth = avctx->
width;
493 out_port_params.format.video.nFrameHeight = avctx->
height;
494 out_port_params.format.video.nStride = 0;
495 out_port_params.format.video.nSliceHeight = 0;
496 out_port_params.format.video.nBitrate = avctx->
bit_rate;
497 out_port_params.format.video.xFramerate = in_port_params.format.video.xFramerate;
498 out_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
500 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
502 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
504 err = OMX_SetParameter(s->
handle, OMX_IndexParamPortDefinition, &out_port_params);
506 err = OMX_GetParameter(s->
handle, OMX_IndexParamPortDefinition, &out_port_params);
511 vid_param_bitrate.nPortIndex = s->
out_port;
512 vid_param_bitrate.eControlRate = OMX_Video_ControlRateVariable;
513 vid_param_bitrate.nTargetBitrate = avctx->
bit_rate;
514 err = OMX_SetParameter(s->
handle, OMX_IndexParamVideoBitrate, &vid_param_bitrate);
515 if (err != OMX_ErrorNone)
519 OMX_VIDEO_PARAM_AVCTYPE avc = { 0 };
522 err = OMX_GetParameter(s->
handle, OMX_IndexParamVideoAvc, &avc);
526 err = OMX_SetParameter(s->
handle, OMX_IndexParamVideoAvc, &avc);
530 err = OMX_SendCommand(s->
handle, OMX_CommandStateSet, OMX_StateIdle,
NULL);
544 if (err == OMX_ErrorNone)
558 err = OMX_SendCommand(s->
handle, OMX_CommandStateSet, OMX_StateExecuting,
NULL);
567 if (err != OMX_ErrorNone) {
581 executing = s->
state == OMX_StateExecuting;
585 OMX_SendCommand(s->
handle, OMX_CommandStateSet, OMX_StateIdle,
NULL);
587 OMX_SendCommand(s->
handle, OMX_CommandStateSet, OMX_StateLoaded,
NULL);
592 buffer->pBuffer =
NULL;
630 OMX_BUFFERHEADERTYPE *
buffer;
649 s->
state = OMX_StateLoaded;
650 s->
error = OMX_ErrorNone;
654 role =
"video_encoder.mpeg4";
657 role =
"video_encoder.avc";
675 if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
684 err = OMX_FillThisBuffer(s->
handle, buffer);
685 if (err != OMX_ErrorNone) {
696 int nals[32] = { 0 };
726 OMX_BUFFERHEADERTYPE*
buffer;
742 if (frame->
linesize[0] == src_linesize[0] &&
743 frame->
linesize[1] == src_linesize[1] &&
744 frame->
linesize[2] == src_linesize[2] &&
745 frame->
data[1] == src[1] &&
746 frame->
data[2] == src[2]) {
756 buffer->pAppPrivate = local;
757 buffer->pOutputPortPrivate =
NULL;
758 buffer->pBuffer = local->
data[0];
770 buffer->pAppPrivate =
buf;
772 buffer->pOutputPortPrivate = (
void*) 1;
773 buffer->pBuffer =
buf;
783 buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
788 err = OMX_EmptyThisBuffer(s->
handle, buffer);
789 if (err != OMX_ErrorNone) {
797 while (!*got_packet && ret == 0) {
814 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME)
816 if (!(buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) || !pkt->
data) {
826 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
843 memcpy(pkt->
data + s->
output_buf_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
847 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
853 if (buffer->nFlags & OMX_BUFFERFLAG_SYNCFRAME)
859 err = OMX_FillThisBuffer(s->
handle, buffer);
860 if (err != OMX_ErrorNone) {
877 #define OFFSET(x) offsetof(OMXCodecContext, x)
878 #define VDE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM
879 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
883 {
"zerocopy",
"Try to avoid copying input frames if possible",
OFFSET(input_zerocopy),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
VE },
909 .priv_class = &omx_mpeg4enc_class,
930 .priv_class = &omx_h264enc_class,
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AVERROR_ENCODER_NOT_FOUND
Encoder not found.
const struct AVCodec * codec
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
This structure describes decoded (raw) audio or video data.
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
static av_cold int find_component(OMXContext *omx_context, void *logctx, const char *role, char *str, int str_size)
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
OMX_BUFFERHEADERTYPE ** in_buffer_headers
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], const uint8_t *src, enum AVPixelFormat pix_fmt, int width, int height, int align)
Setup the data pointers and linesizes based on the specified image parameters and the provided array...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Convenience header that includes libavutil's core.
static OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_BUFFERHEADERTYPE *buffer)
static av_cold int omx_encode_end(AVCodecContext *avctx)
static OMX_BUFFERHEADERTYPE * get_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond, int *array_size, OMX_BUFFERHEADERTYPE **array, int wait)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
static OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_BUFFERHEADERTYPE *buffer)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
OMX_ERRORTYPE(* ptr_ComponentNameEnum)(OMX_STRING, OMX_U32, OMX_U32)
static enum AVPixelFormat omx_encoder_pix_fmts[]
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
OMX_ERRORTYPE(* ptr_GetRolesOfComponent)(OMX_STRING, OMX_U32 *, OMX_U8 **)
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
pthread_cond_t input_cond
static av_cold int end(AVCodecContext *avctx)
OMX_BUFFERHEADERTYPE ** done_out_buffers
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
H.264 common definitions.
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
pthread_cond_t state_cond
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_cold int wait_for_state(OMXCodecContext *s, OMX_STATETYPE state)
int flags
AV_CODEC_FLAG_*.
pthread_mutex_t output_mutex
const char * name
Name of the codec implementation.
OMX_COLOR_FORMATTYPE color_format
static void append_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond, int *array_size, OMX_BUFFERHEADERTYPE **array, OMX_BUFFERHEADERTYPE *buffer)
AVCodec ff_mpeg4_omx_encoder
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
static int omx_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
int flags
A combination of AV_PKT_FLAG values.
static av_cold void * dlsym_prefixed(void *handle, const char *symbol, const char *prefix)
OMX_BUFFERHEADERTYPE ** out_buffer_headers
OMX_ERRORTYPE(* ptr_GetHandle)(OMX_HANDLETYPE *, OMX_STRING, OMX_PTR, OMX_CALLBACKTYPE *)
OMX_BUFFERHEADERTYPE ** free_in_buffers
int width
picture width / height.
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
GLsizei GLboolean const GLfloat * value
static av_cold int omx_encode_init(AVCodecContext *avctx)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
static const AVOption options[]
pthread_cond_t output_cond
static av_cold int omx_try_load(OMXContext *s, void *logctx, const char *libname, const char *prefix, const char *libname2)
static pthread_mutex_t * mutex
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
static const AVClass omx_mpeg4enc_class
#define AV_LOG_INFO
Standard information.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Libavcodec external API header.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
Describe the class of an AVClass context structure.
char component_name[OMX_MAX_STRINGNAME_SIZE]
static av_cold void omx_deinit(OMXContext *omx_context)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
static enum AVPixelFormat pix_fmts[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
static const OMX_CALLBACKTYPE callbacks
static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data)
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
common internal and external API header
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
pthread_mutex_t input_mutex
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
pthread_mutex_t state_mutex
#define from_omx_ticks(x)
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
static const AVClass omx_h264enc_class
OMX_ERRORTYPE(* ptr_FreeHandle)(OMX_HANDLETYPE)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static int array[MAX_W *MAX_W]
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
OMX_ERRORTYPE(* ptr_Deinit)(void)
static av_cold void cleanup(OMXCodecContext *s)
AVPixelFormat
Pixel format.
This structure stores compressed data.
AVCodec ff_h264_omx_encoder
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
OMX_ERRORTYPE(* ptr_Init)(void)
OMX_ERRORTYPE(* ptr_GetComponentsOfRole)(OMX_STRING, OMX_U32 *, OMX_U8 **)
static av_cold OMXContext * omx_init(void *logctx, const char *libname, const char *prefix)