3 * Copyright (C) 2011 Martin Storsjo
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <OMX_Component.h>
36 #include "libavutil/avstring.h"
37 #include "libavutil/avutil.h"
38 #include "libavutil/common.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/log.h"
41 #include "libavutil/opt.h"
48 static OMX_TICKS to_omx_ticks(int64_t value)
51 s.nLowPart = value & 0xffffffff;
52 s.nHighPart = value >> 32;
55 static int64_t from_omx_ticks(OMX_TICKS value)
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; \
76 typedef struct OMXContext {
79 OMX_ERRORTYPE (*ptr_Init)(void);
80 OMX_ERRORTYPE (*ptr_Deinit)(void);
81 OMX_ERRORTYPE (*ptr_ComponentNameEnum)(OMX_STRING, OMX_U32, OMX_U32);
82 OMX_ERRORTYPE (*ptr_GetHandle)(OMX_HANDLETYPE*, OMX_STRING, OMX_PTR, OMX_CALLBACKTYPE*);
83 OMX_ERRORTYPE (*ptr_FreeHandle)(OMX_HANDLETYPE);
84 OMX_ERRORTYPE (*ptr_GetComponentsOfRole)(OMX_STRING, OMX_U32*, OMX_U8**);
85 OMX_ERRORTYPE (*ptr_GetRolesOfComponent)(OMX_STRING, OMX_U32*, OMX_U8**);
86 void (*host_init)(void);
89 static av_cold void *dlsym_prefixed(void *handle, const char *symbol, const char *prefix)
92 snprintf(buf, sizeof(buf), "%s%s", prefix ? prefix : "", symbol);
93 return dlsym(handle, buf);
96 static av_cold int omx_try_load(OMXContext *s, void *logctx,
97 const char *libname, const char *prefix,
101 s->lib2 = dlopen(libname2, RTLD_NOW | RTLD_GLOBAL);
103 av_log(logctx, AV_LOG_WARNING, "%s not found\n", libname);
104 return AVERROR_ENCODER_NOT_FOUND;
106 s->host_init = dlsym(s->lib2, "bcm_host_init");
108 av_log(logctx, AV_LOG_WARNING, "bcm_host_init not found\n");
111 return AVERROR_ENCODER_NOT_FOUND;
114 s->lib = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
116 av_log(logctx, AV_LOG_WARNING, "%s not found\n", libname);
117 return AVERROR_ENCODER_NOT_FOUND;
119 s->ptr_Init = dlsym_prefixed(s->lib, "OMX_Init", prefix);
120 s->ptr_Deinit = dlsym_prefixed(s->lib, "OMX_Deinit", prefix);
121 s->ptr_ComponentNameEnum = dlsym_prefixed(s->lib, "OMX_ComponentNameEnum", prefix);
122 s->ptr_GetHandle = dlsym_prefixed(s->lib, "OMX_GetHandle", prefix);
123 s->ptr_FreeHandle = dlsym_prefixed(s->lib, "OMX_FreeHandle", 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) {
129 av_log(logctx, AV_LOG_WARNING, "Not all functions found in %s\n", libname);
135 return AVERROR_ENCODER_NOT_FOUND;
140 static av_cold OMXContext *omx_init(void *logctx, const char *libname, const char *prefix)
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;
152 int ret = AVERROR_ENCODER_NOT_FOUND;
153 OMXContext *omx_context;
155 omx_context = av_mallocz(sizeof(*omx_context));
159 ret = omx_try_load(omx_context, logctx, libname, prefix, NULL);
161 av_free(omx_context);
165 for (nameptr = libnames; *nameptr; nameptr += 2)
166 if (!(ret = omx_try_load(omx_context, logctx, nameptr[0], prefix, nameptr[1])))
169 av_free(omx_context);
174 if (omx_context->host_init)
175 omx_context->host_init();
176 omx_context->ptr_Init();
180 static av_cold void omx_deinit(OMXContext *omx_context)
184 omx_context->ptr_Deinit();
185 dlclose(omx_context->lib);
186 av_free(omx_context);
189 typedef struct OMXCodecContext {
190 const AVClass *class;
193 OMXContext *omx_context;
195 AVCodecContext *avctx;
197 char component_name[OMX_MAX_STRINGNAME_SIZE];
198 OMX_VERSIONTYPE version;
199 OMX_HANDLETYPE handle;
200 int in_port, out_port;
201 OMX_COLOR_FORMATTYPE color_format;
202 int stride, plane_size;
204 int num_in_buffers, num_out_buffers;
205 OMX_BUFFERHEADERTYPE **in_buffer_headers;
206 OMX_BUFFERHEADERTYPE **out_buffer_headers;
207 int num_free_in_buffers;
208 OMX_BUFFERHEADERTYPE **free_in_buffers;
209 int num_done_out_buffers;
210 OMX_BUFFERHEADERTYPE **done_out_buffers;
211 pthread_mutex_t input_mutex;
212 pthread_cond_t input_cond;
213 pthread_mutex_t output_mutex;
214 pthread_cond_t output_cond;
216 pthread_mutex_t state_mutex;
217 pthread_cond_t state_cond;
221 int mutex_cond_inited;
223 int num_in_frames, num_out_frames;
232 static void append_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond,
233 int* array_size, OMX_BUFFERHEADERTYPE **array,
234 OMX_BUFFERHEADERTYPE *buffer)
236 pthread_mutex_lock(mutex);
237 array[(*array_size)++] = buffer;
238 pthread_cond_broadcast(cond);
239 pthread_mutex_unlock(mutex);
242 static OMX_BUFFERHEADERTYPE *get_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond,
243 int* array_size, OMX_BUFFERHEADERTYPE **array,
246 OMX_BUFFERHEADERTYPE *buffer;
247 pthread_mutex_lock(mutex);
250 pthread_cond_wait(cond, mutex);
252 if (*array_size > 0) {
255 memmove(&array[0], &array[1], (*array_size) * sizeof(OMX_BUFFERHEADERTYPE*));
259 pthread_mutex_unlock(mutex);
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)
266 OMXCodecContext *s = app_data;
267 // This uses casts in the printfs, since OMX_U32 actually is a typedef for
268 // unsigned long in official header versions (but there are also modified
269 // versions where it is something else).
272 pthread_mutex_lock(&s->state_mutex);
273 av_log(s->avctx, AV_LOG_ERROR, "OMX error %"PRIx32"\n", (uint32_t) data1);
275 pthread_cond_broadcast(&s->state_cond);
276 pthread_mutex_unlock(&s->state_mutex);
278 case OMX_EventCmdComplete:
279 if (data1 == OMX_CommandStateSet) {
280 pthread_mutex_lock(&s->state_mutex);
282 av_log(s->avctx, AV_LOG_VERBOSE, "OMX state changed to %"PRIu32"\n", (uint32_t) data2);
283 pthread_cond_broadcast(&s->state_cond);
284 pthread_mutex_unlock(&s->state_mutex);
285 } else if (data1 == OMX_CommandPortDisable) {
286 av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" disabled\n", (uint32_t) data2);
287 } else if (data1 == OMX_CommandPortEnable) {
288 av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" enabled\n", (uint32_t) data2);
290 av_log(s->avctx, AV_LOG_VERBOSE, "OMX command complete, command %"PRIu32", value %"PRIu32"\n",
291 (uint32_t) data1, (uint32_t) data2);
294 case OMX_EventPortSettingsChanged:
295 av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" settings changed\n", (uint32_t) data1);
298 av_log(s->avctx, AV_LOG_VERBOSE, "OMX event %d %"PRIx32" %"PRIx32"\n",
299 event, (uint32_t) data1, (uint32_t) data2);
302 return OMX_ErrorNone;
305 static OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data,
306 OMX_BUFFERHEADERTYPE *buffer)
308 OMXCodecContext *s = app_data;
309 if (s->input_zerocopy) {
310 if (buffer->pAppPrivate) {
311 if (buffer->pOutputPortPrivate)
312 av_free(buffer->pAppPrivate);
314 av_frame_free((AVFrame**)&buffer->pAppPrivate);
315 buffer->pAppPrivate = NULL;
318 append_buffer(&s->input_mutex, &s->input_cond,
319 &s->num_free_in_buffers, s->free_in_buffers, buffer);
320 return OMX_ErrorNone;
323 static OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data,
324 OMX_BUFFERHEADERTYPE *buffer)
326 OMXCodecContext *s = app_data;
327 append_buffer(&s->output_mutex, &s->output_cond,
328 &s->num_done_out_buffers, s->done_out_buffers, buffer);
329 return OMX_ErrorNone;
332 static const OMX_CALLBACKTYPE callbacks = {
338 static av_cold int find_component(OMXContext *omx_context, void *logctx,
339 const char *role, char *str, int str_size)
346 if (av_strstart(role, "video_encoder.", NULL)) {
347 av_strlcpy(str, "OMX.broadcom.video_encode", str_size);
351 omx_context->ptr_GetComponentsOfRole((OMX_STRING) role, &num, NULL);
353 av_log(logctx, AV_LOG_WARNING, "No component for role %s found\n", role);
354 return AVERROR_ENCODER_NOT_FOUND;
356 components = av_mallocz_array(num, sizeof(*components));
358 return AVERROR(ENOMEM);
359 for (i = 0; i < num; i++) {
360 components[i] = av_mallocz(OMX_MAX_STRINGNAME_SIZE);
361 if (!components[i]) {
362 ret = AVERROR(ENOMEM);
366 omx_context->ptr_GetComponentsOfRole((OMX_STRING) role, &num, (OMX_U8**) components);
367 av_strlcpy(str, components[0], str_size);
369 for (i = 0; i < num; i++)
370 av_free(components[i]);
375 static av_cold int wait_for_state(OMXCodecContext *s, OMX_STATETYPE state)
378 pthread_mutex_lock(&s->state_mutex);
379 while (s->state != state && s->error == OMX_ErrorNone)
380 pthread_cond_wait(&s->state_cond, &s->state_mutex);
381 if (s->error != OMX_ErrorNone)
382 ret = AVERROR_ENCODER_NOT_FOUND;
383 pthread_mutex_unlock(&s->state_mutex);
387 static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
389 OMXCodecContext *s = avctx->priv_data;
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) {
404 av_log(avctx, AV_LOG_ERROR, "OMX_GetHandle(%s) failed: %x\n", s->component_name, err);
405 return AVERROR_UNKNOWN;
408 // This one crashes the mediaserver on qcom, if used over IOMX
409 INIT_STRUCT(role_params);
410 av_strlcpy(role_params.cRole, role, sizeof(role_params.cRole));
411 // Intentionally ignore errors on this one
412 OMX_SetParameter(s->handle, OMX_IndexParamStandardComponentRole, &role_params);
414 INIT_STRUCT(video_port_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 };
422 INIT_STRUCT(port_params);
423 port_params.nPortIndex = port;
424 err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &port_params);
425 if (err != OMX_ErrorNone) {
426 av_log(avctx, AV_LOG_WARNING, "port %d error %x\n", port, err);
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);
439 return AVERROR_UNKNOWN;
444 INIT_STRUCT(video_port_format);
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) {
456 av_log(avctx, AV_LOG_ERROR, "No supported pixel formats (%d formats available)\n", i);
457 return AVERROR_UNKNOWN;
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;
467 s->stride = avctx->width;
468 s->plane_size = avctx->height;
469 // If specific codecs need to manually override the stride/plane_size,
470 // that can be done here.
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;
475 if (avctx->framerate.den > 0 && avctx->framerate.num > 0)
476 in_port_params.format.video.xFramerate = (1 << 16) * avctx->framerate.num / avctx->framerate.den;
478 in_port_params.format.video.xFramerate = (1 << 16) * avctx->time_base.den / avctx->time_base.num;
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;
500 if (avctx->codec->id == AV_CODEC_ID_MPEG4)
501 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
502 else if (avctx->codec->id == AV_CODEC_ID_H264)
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;
511 INIT_STRUCT(vid_param_bitrate);
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)
517 av_log(avctx, AV_LOG_WARNING, "Unable to set video bitrate parameter\n");
519 if (avctx->codec->id == AV_CODEC_ID_H264) {
520 OMX_VIDEO_PARAM_AVCTYPE avc = { 0 };
522 avc.nPortIndex = s->out_port;
523 err = OMX_GetParameter(s->handle, OMX_IndexParamVideoAvc, &avc);
526 avc.nPFrames = avctx->gop_size - 1;
527 switch (s->profile == FF_PROFILE_UNKNOWN ? avctx->profile : s->profile) {
528 case FF_PROFILE_H264_BASELINE:
529 avc.eProfile = OMX_VIDEO_AVCProfileBaseline;
531 case FF_PROFILE_H264_MAIN:
532 avc.eProfile = OMX_VIDEO_AVCProfileMain;
534 case FF_PROFILE_H264_HIGH:
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)
552 return AVERROR(ENOMEM);
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;
568 if (wait_for_state(s, OMX_StateIdle) < 0) {
569 av_log(avctx, AV_LOG_ERROR, "Didn't get OMX_StateIdle\n");
570 return AVERROR_UNKNOWN;
572 err = OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
574 if (wait_for_state(s, OMX_StateExecuting) < 0) {
575 av_log(avctx, AV_LOG_ERROR, "Didn't get OMX_StateExecuting\n");
576 return AVERROR_UNKNOWN;
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];
587 return err != OMX_ErrorNone ? AVERROR_UNKNOWN : 0;
590 static av_cold void cleanup(OMXCodecContext *s)
594 pthread_mutex_lock(&s->state_mutex);
595 executing = s->state == OMX_StateExecuting;
596 pthread_mutex_unlock(&s->state_mutex);
599 OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
600 wait_for_state(s, OMX_StateIdle);
601 OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateLoaded, NULL);
602 for (i = 0; i < s->num_in_buffers; i++) {
603 OMX_BUFFERHEADERTYPE *buffer = get_buffer(&s->input_mutex, &s->input_cond,
604 &s->num_free_in_buffers, s->free_in_buffers, 1);
605 if (s->input_zerocopy)
606 buffer->pBuffer = NULL;
607 OMX_FreeBuffer(s->handle, s->in_port, buffer);
609 for (i = 0; i < s->num_out_buffers; i++) {
610 OMX_BUFFERHEADERTYPE *buffer = get_buffer(&s->output_mutex, &s->output_cond,
611 &s->num_done_out_buffers, s->done_out_buffers, 1);
612 OMX_FreeBuffer(s->handle, s->out_port, buffer);
614 wait_for_state(s, OMX_StateLoaded);
617 s->omx_context->ptr_FreeHandle(s->handle);
621 omx_deinit(s->omx_context);
622 s->omx_context = NULL;
623 if (s->mutex_cond_inited) {
624 pthread_cond_destroy(&s->state_cond);
625 pthread_mutex_destroy(&s->state_mutex);
626 pthread_cond_destroy(&s->input_cond);
627 pthread_mutex_destroy(&s->input_mutex);
628 pthread_cond_destroy(&s->output_cond);
629 pthread_mutex_destroy(&s->output_mutex);
630 s->mutex_cond_inited = 0;
632 av_freep(&s->in_buffer_headers);
633 av_freep(&s->out_buffer_headers);
634 av_freep(&s->free_in_buffers);
635 av_freep(&s->done_out_buffers);
636 av_freep(&s->output_buf);
639 static av_cold int omx_encode_init(AVCodecContext *avctx)
641 OMXCodecContext *s = avctx->priv_data;
642 int ret = AVERROR_ENCODER_NOT_FOUND;
644 OMX_BUFFERHEADERTYPE *buffer;
648 s->input_zerocopy = 1;
651 s->omx_context = omx_init(avctx, s->libname, s->libprefix);
653 return AVERROR_ENCODER_NOT_FOUND;
655 pthread_mutex_init(&s->state_mutex, NULL);
656 pthread_cond_init(&s->state_cond, NULL);
657 pthread_mutex_init(&s->input_mutex, NULL);
658 pthread_cond_init(&s->input_cond, NULL);
659 pthread_mutex_init(&s->output_mutex, NULL);
660 pthread_cond_init(&s->output_cond, NULL);
661 s->mutex_cond_inited = 1;
663 s->state = OMX_StateLoaded;
664 s->error = OMX_ErrorNone;
666 switch (avctx->codec->id) {
667 case AV_CODEC_ID_MPEG4:
668 role = "video_encoder.mpeg4";
670 case AV_CODEC_ID_H264:
671 role = "video_encoder.avc";
674 return AVERROR(ENOSYS);
677 if ((ret = find_component(s->omx_context, avctx, role, s->component_name, sizeof(s->component_name))) < 0)
680 av_log(avctx, AV_LOG_INFO, "Using %s\n", s->component_name);
682 if ((ret = omx_component_init(avctx, role)) < 0)
685 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
687 buffer = get_buffer(&s->output_mutex, &s->output_cond,
688 &s->num_done_out_buffers, s->done_out_buffers, 1);
689 if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
690 if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
691 avctx->extradata_size = 0;
694 memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
695 avctx->extradata_size += buffer->nFilledLen;
696 memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
698 err = OMX_FillThisBuffer(s->handle, buffer);
699 if (err != OMX_ErrorNone) {
700 append_buffer(&s->output_mutex, &s->output_cond,
701 &s->num_done_out_buffers, s->done_out_buffers, buffer);
702 av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err);
703 ret = AVERROR_UNKNOWN;
706 if (avctx->codec->id == AV_CODEC_ID_H264) {
707 // For H.264, the extradata can be returned in two separate buffers
708 // (the videocore encoder on raspberry pi does this);
709 // therefore check that we have got both SPS and PPS before continuing.
710 int nals[32] = { 0 };
712 for (i = 0; i + 4 < avctx->extradata_size; i++) {
713 if (!avctx->extradata[i + 0] &&
714 !avctx->extradata[i + 1] &&
715 !avctx->extradata[i + 2] &&
716 avctx->extradata[i + 3] == 1) {
717 nals[avctx->extradata[i + 4] & 0x1f]++;
720 if (nals[H264_NAL_SPS] && nals[H264_NAL_PPS])
723 if (avctx->extradata_size > 0)
735 static int omx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
736 const AVFrame *frame, int *got_packet)
738 OMXCodecContext *s = avctx->priv_data;
740 OMX_BUFFERHEADERTYPE* buffer;
747 buffer = get_buffer(&s->input_mutex, &s->input_cond,
748 &s->num_free_in_buffers, s->free_in_buffers, 1);
750 buffer->nFilledLen = av_image_fill_arrays(dst, linesize, buffer->pBuffer, avctx->pix_fmt, s->stride, s->plane_size, 1);
752 if (s->input_zerocopy) {
753 uint8_t *src[4] = { NULL };
755 av_image_fill_arrays(src, src_linesize, frame->data[0], avctx->pix_fmt, s->stride, s->plane_size, 1);
756 if (frame->linesize[0] == src_linesize[0] &&
757 frame->linesize[1] == src_linesize[1] &&
758 frame->linesize[2] == src_linesize[2] &&
759 frame->data[1] == src[1] &&
760 frame->data[2] == src[2]) {
761 // If the input frame happens to have all planes stored contiguously,
762 // with the right strides, just clone the frame and set the OMX
763 // buffer header to point to it
764 AVFrame *local = av_frame_clone(frame);
766 // Return the buffer to the queue so it's not lost
767 append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer);
768 return AVERROR(ENOMEM);
770 buffer->pAppPrivate = local;
771 buffer->pOutputPortPrivate = NULL;
772 buffer->pBuffer = local->data[0];
776 // If not, we need to allocate a new buffer with the right
777 // size and copy the input frame into it.
779 int image_buffer_size = av_image_get_buffer_size(avctx->pix_fmt, s->stride, s->plane_size, 1);
780 if (image_buffer_size >= 0)
781 buf = av_malloc(image_buffer_size);
783 // Return the buffer to the queue so it's not lost
784 append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer);
785 return AVERROR(ENOMEM);
787 buffer->pAppPrivate = buf;
788 // Mark that pAppPrivate is an av_malloc'ed buffer, not an AVFrame
789 buffer->pOutputPortPrivate = (void*) 1;
790 buffer->pBuffer = buf;
792 buffer->nFilledLen = av_image_fill_arrays(dst, linesize, buffer->pBuffer, avctx->pix_fmt, s->stride, s->plane_size, 1);
799 av_image_copy(dst, linesize, (const uint8_t**) frame->data, frame->linesize, avctx->pix_fmt, avctx->width, avctx->height);
800 buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
802 // Convert the timestamps to microseconds; some encoders can ignore
803 // the framerate and do VFR bit allocation based on timestamps.
804 buffer->nTimeStamp = to_omx_ticks(av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q));
805 err = OMX_EmptyThisBuffer(s->handle, buffer);
806 if (err != OMX_ErrorNone) {
807 append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer);
808 av_log(avctx, AV_LOG_ERROR, "OMX_EmptyThisBuffer failed: %x\n", err);
809 return AVERROR_UNKNOWN;
814 while (!*got_packet && ret == 0) {
815 // Only wait for output if flushing and not all frames have been output
816 buffer = get_buffer(&s->output_mutex, &s->output_cond,
817 &s->num_done_out_buffers, s->done_out_buffers,
818 !frame && s->num_out_frames < s->num_in_frames);
822 if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG && avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
823 if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
824 avctx->extradata_size = 0;
827 memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
828 avctx->extradata_size += buffer->nFilledLen;
829 memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
831 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME)
833 if (!(buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) || !pkt->data) {
834 // If the output packet isn't preallocated, just concatenate everything in our
836 int newsize = s->output_buf_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE;
837 if ((ret = av_reallocp(&s->output_buf, newsize)) < 0) {
838 s->output_buf_size = 0;
841 memcpy(s->output_buf + s->output_buf_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
842 s->output_buf_size += buffer->nFilledLen;
843 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
844 if ((ret = av_packet_from_data(pkt, s->output_buf, s->output_buf_size)) < 0) {
845 av_freep(&s->output_buf);
846 s->output_buf_size = 0;
849 s->output_buf = NULL;
850 s->output_buf_size = 0;
853 // End of frame, and the caller provided a preallocated frame
854 if ((ret = ff_alloc_packet2(avctx, pkt, s->output_buf_size + buffer->nFilledLen, 0)) < 0) {
855 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n",
856 (int)(s->output_buf_size + buffer->nFilledLen));
859 memcpy(pkt->data, s->output_buf, s->output_buf_size);
860 memcpy(pkt->data + s->output_buf_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
861 av_freep(&s->output_buf);
862 s->output_buf_size = 0;
864 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
865 pkt->pts = av_rescale_q(from_omx_ticks(buffer->nTimeStamp), AV_TIME_BASE_Q, avctx->time_base);
866 // We don't currently enable B-frames for the encoders, so set
867 // pkt->dts = pkt->pts. (The calling code behaves worse if the encoder
868 // doesn't set the dts).
870 if (buffer->nFlags & OMX_BUFFERFLAG_SYNCFRAME)
871 pkt->flags |= AV_PKT_FLAG_KEY;
876 err = OMX_FillThisBuffer(s->handle, buffer);
877 if (err != OMX_ErrorNone) {
878 append_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, buffer);
879 av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err);
880 ret = AVERROR_UNKNOWN;
886 static av_cold int omx_encode_end(AVCodecContext *avctx)
888 OMXCodecContext *s = avctx->priv_data;
894 #define OFFSET(x) offsetof(OMXCodecContext, x)
895 #define VDE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM
896 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
897 static const AVOption options[] = {
898 { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
899 { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
900 { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
901 { "profile", "Set the encoding profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, FF_PROFILE_H264_HIGH, VE, "profile" },
902 { "baseline", "", 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_H264_BASELINE }, 0, 0, VE, "profile" },
903 { "main", "", 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_H264_MAIN }, 0, 0, VE, "profile" },
904 { "high", "", 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_H264_HIGH }, 0, 0, VE, "profile" },
908 static const enum AVPixelFormat omx_encoder_pix_fmts[] = {
909 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
912 static const AVClass omx_mpeg4enc_class = {
913 .class_name = "mpeg4_omx",
914 .item_name = av_default_item_name,
916 .version = LIBAVUTIL_VERSION_INT,
918 AVCodec ff_mpeg4_omx_encoder = {
920 .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL MPEG-4 video encoder"),
921 .type = AVMEDIA_TYPE_VIDEO,
922 .id = AV_CODEC_ID_MPEG4,
923 .priv_data_size = sizeof(OMXCodecContext),
924 .init = omx_encode_init,
925 .encode2 = omx_encode_frame,
926 .close = omx_encode_end,
927 .pix_fmts = omx_encoder_pix_fmts,
928 .capabilities = AV_CODEC_CAP_DELAY,
929 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
930 .priv_class = &omx_mpeg4enc_class,
933 static const AVClass omx_h264enc_class = {
934 .class_name = "h264_omx",
935 .item_name = av_default_item_name,
937 .version = LIBAVUTIL_VERSION_INT,
939 AVCodec ff_h264_omx_encoder = {
941 .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL H.264 video encoder"),
942 .type = AVMEDIA_TYPE_VIDEO,
943 .id = AV_CODEC_ID_H264,
944 .priv_data_size = sizeof(OMXCodecContext),
945 .init = omx_encode_init,
946 .encode2 = omx_encode_frame,
947 .close = omx_encode_end,
948 .pix_fmts = omx_encoder_pix_fmts,
949 .capabilities = AV_CODEC_CAP_DELAY,
950 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
951 .priv_class = &omx_h264enc_class,