libavcodec: Add H264/MPEG4 encoders based on OpenMAX IL
[libav.git] / libavcodec / omx.c
1 /*
2 * OMX Video encoder
3 * Copyright (C) 2011 Martin Storsjo
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <dlfcn.h>
23 #include <OMX_Core.h>
24 #include <OMX_Component.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/time.h>
29
30 #include "libavutil/avstring.h"
31 #include "libavutil/avutil.h"
32 #include "libavutil/common.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/log.h"
35 #include "libavutil/opt.h"
36
37 #include "avcodec.h"
38 #include "h264.h"
39 #include "internal.h"
40
41 #ifdef OMX_SKIP64BIT
42 static OMX_TICKS to_omx_ticks(int64_t value)
43 {
44 OMX_TICKS s;
45 s.nLowPart = value & 0xffffffff;
46 s.nHighPart = value >> 32;
47 return s;
48 }
49 static int64_t from_omx_ticks(OMX_TICKS value)
50 {
51 return (((int64_t)value.nHighPart) << 32) | value.nLowPart;
52 }
53 #else
54 #define to_omx_ticks(x) (x)
55 #define from_omx_ticks(x) (x)
56 #endif
57
58 #define INIT_STRUCT(x) do { \
59 x.nSize = sizeof(x); \
60 x.nVersion = s->version; \
61 } while (0)
62 #define CHECK(x) do { \
63 if (x != OMX_ErrorNone) { \
64 av_log(avctx, AV_LOG_ERROR, \
65 "err %x (%d) on line %d\n", x, x, __LINE__); \
66 return AVERROR_UNKNOWN; \
67 } \
68 } while (0)
69
70 typedef struct OMXContext {
71 void *lib;
72 OMX_ERRORTYPE (*ptr_Init)(void);
73 OMX_ERRORTYPE (*ptr_Deinit)(void);
74 OMX_ERRORTYPE (*ptr_ComponentNameEnum)(OMX_STRING, OMX_U32, OMX_U32);
75 OMX_ERRORTYPE (*ptr_GetHandle)(OMX_HANDLETYPE*, OMX_STRING, OMX_PTR, OMX_CALLBACKTYPE*);
76 OMX_ERRORTYPE (*ptr_FreeHandle)(OMX_HANDLETYPE);
77 OMX_ERRORTYPE (*ptr_GetComponentsOfRole)(OMX_STRING, OMX_U32*, OMX_U8**);
78 OMX_ERRORTYPE (*ptr_GetRolesOfComponent)(OMX_STRING, OMX_U32*, OMX_U8**);
79 } OMXContext;
80
81 static av_cold void *dlsym_prefixed(void *handle, const char *symbol, const char *prefix)
82 {
83 char buf[50];
84 snprintf(buf, sizeof(buf), "%s%s", prefix ? prefix : "", symbol);
85 return dlsym(handle, buf);
86 }
87
88 static av_cold int omx_try_load(OMXContext *s, void *logctx,
89 const char *libname, const char *prefix)
90 {
91 s->lib = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
92 if (!s->lib) {
93 av_log(logctx, AV_LOG_WARNING, "%s not found\n", libname);
94 return AVERROR_ENCODER_NOT_FOUND;
95 }
96 s->ptr_Init = dlsym_prefixed(s->lib, "OMX_Init", prefix);
97 s->ptr_Deinit = dlsym_prefixed(s->lib, "OMX_Deinit", prefix);
98 s->ptr_ComponentNameEnum = dlsym_prefixed(s->lib, "OMX_ComponentNameEnum", prefix);
99 s->ptr_GetHandle = dlsym_prefixed(s->lib, "OMX_GetHandle", prefix);
100 s->ptr_FreeHandle = dlsym_prefixed(s->lib, "OMX_FreeHandle", prefix);
101 s->ptr_GetComponentsOfRole = dlsym_prefixed(s->lib, "OMX_GetComponentsOfRole", prefix);
102 s->ptr_GetRolesOfComponent = dlsym_prefixed(s->lib, "OMX_GetRolesOfComponent", prefix);
103 if (!s->ptr_Init || !s->ptr_Deinit || !s->ptr_ComponentNameEnum ||
104 !s->ptr_GetHandle || !s->ptr_FreeHandle ||
105 !s->ptr_GetComponentsOfRole || !s->ptr_GetRolesOfComponent) {
106 av_log(logctx, AV_LOG_WARNING, "Not all functions found in %s\n", libname);
107 dlclose(s->lib);
108 s->lib = NULL;
109 return AVERROR_ENCODER_NOT_FOUND;
110 }
111 return 0;
112 }
113
114 static av_cold OMXContext *omx_init(void *logctx, const char *libname, const char *prefix)
115 {
116 static const char * const libnames[] = {
117 "libOMX_Core.so",
118 "libOmxCore.so",
119 NULL
120 };
121 const char* const* nameptr;
122 int ret = AVERROR_ENCODER_NOT_FOUND;
123 OMXContext *omx_context;
124
125 omx_context = av_mallocz(sizeof(*omx_context));
126 if (!omx_context)
127 return NULL;
128 if (libname) {
129 ret = omx_try_load(omx_context, logctx, libname, prefix);
130 if (ret < 0) {
131 av_free(omx_context);
132 return NULL;
133 }
134 } else {
135 for (nameptr = libnames; *nameptr; nameptr++)
136 if (!(ret = omx_try_load(omx_context, logctx, *nameptr, prefix)))
137 break;
138 if (!*nameptr) {
139 av_free(omx_context);
140 return NULL;
141 }
142 }
143
144 omx_context->ptr_Init();
145 return omx_context;
146 }
147
148 static av_cold void omx_deinit(OMXContext *omx_context)
149 {
150 if (!omx_context)
151 return;
152 omx_context->ptr_Deinit();
153 dlclose(omx_context->lib);
154 av_free(omx_context);
155 }
156
157 typedef struct OMXCodecContext {
158 const AVClass *class;
159 char *libname;
160 char *libprefix;
161 OMXContext *omx_context;
162
163 AVCodecContext *avctx;
164
165 char component_name[OMX_MAX_STRINGNAME_SIZE];
166 OMX_VERSIONTYPE version;
167 OMX_HANDLETYPE handle;
168 int in_port, out_port;
169 OMX_COLOR_FORMATTYPE color_format;
170 int stride, plane_size;
171
172 int num_in_buffers, num_out_buffers;
173 OMX_BUFFERHEADERTYPE **in_buffer_headers;
174 OMX_BUFFERHEADERTYPE **out_buffer_headers;
175 int num_free_in_buffers;
176 OMX_BUFFERHEADERTYPE **free_in_buffers;
177 int num_done_out_buffers;
178 OMX_BUFFERHEADERTYPE **done_out_buffers;
179 pthread_mutex_t input_mutex;
180 pthread_cond_t input_cond;
181 pthread_mutex_t output_mutex;
182 pthread_cond_t output_cond;
183
184 pthread_mutex_t state_mutex;
185 pthread_cond_t state_cond;
186 OMX_STATETYPE state;
187 OMX_ERRORTYPE error;
188
189 int mutex_cond_inited;
190
191 int num_in_frames, num_out_frames;
192
193 uint8_t *output_buf;
194 int output_buf_size;
195 } OMXCodecContext;
196
197 static void append_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond,
198 int* array_size, OMX_BUFFERHEADERTYPE **array,
199 OMX_BUFFERHEADERTYPE *buffer)
200 {
201 pthread_mutex_lock(mutex);
202 array[(*array_size)++] = buffer;
203 pthread_cond_broadcast(cond);
204 pthread_mutex_unlock(mutex);
205 }
206
207 static OMX_BUFFERHEADERTYPE *get_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond,
208 int* array_size, OMX_BUFFERHEADERTYPE **array,
209 int wait)
210 {
211 OMX_BUFFERHEADERTYPE *buffer;
212 pthread_mutex_lock(mutex);
213 if (wait) {
214 while (!*array_size)
215 pthread_cond_wait(cond, mutex);
216 }
217 if (*array_size > 0) {
218 buffer = array[0];
219 (*array_size)--;
220 memmove(&array[0], &array[1], (*array_size) * sizeof(OMX_BUFFERHEADERTYPE*));
221 } else {
222 buffer = NULL;
223 }
224 pthread_mutex_unlock(mutex);
225 return buffer;
226 }
227
228 static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event,
229 OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data)
230 {
231 OMXCodecContext *s = app_data;
232 // This uses casts in the printfs, since OMX_U32 actually is a typedef for
233 // unsigned long in official header versions (but there are also modified
234 // versions where it is something else).
235 switch (event) {
236 case OMX_EventError:
237 pthread_mutex_lock(&s->state_mutex);
238 av_log(s->avctx, AV_LOG_ERROR, "OMX error %"PRIx32"\n", (uint32_t) data1);
239 s->error = data1;
240 pthread_cond_broadcast(&s->state_cond);
241 pthread_mutex_unlock(&s->state_mutex);
242 break;
243 case OMX_EventCmdComplete:
244 if (data1 == OMX_CommandStateSet) {
245 pthread_mutex_lock(&s->state_mutex);
246 s->state = data2;
247 av_log(s->avctx, AV_LOG_VERBOSE, "OMX state changed to %"PRIu32"\n", (uint32_t) data2);
248 pthread_cond_broadcast(&s->state_cond);
249 pthread_mutex_unlock(&s->state_mutex);
250 } else if (data1 == OMX_CommandPortDisable) {
251 av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" disabled\n", (uint32_t) data2);
252 } else if (data1 == OMX_CommandPortEnable) {
253 av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" enabled\n", (uint32_t) data2);
254 } else {
255 av_log(s->avctx, AV_LOG_VERBOSE, "OMX command complete, command %"PRIu32", value %"PRIu32"\n",
256 (uint32_t) data1, (uint32_t) data2);
257 }
258 break;
259 case OMX_EventPortSettingsChanged:
260 av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" settings changed\n", (uint32_t) data1);
261 break;
262 default:
263 av_log(s->avctx, AV_LOG_VERBOSE, "OMX event %d %"PRIx32" %"PRIx32"\n",
264 event, (uint32_t) data1, (uint32_t) data2);
265 break;
266 }
267 return OMX_ErrorNone;
268 }
269
270 static OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data,
271 OMX_BUFFERHEADERTYPE *buffer)
272 {
273 OMXCodecContext *s = app_data;
274 append_buffer(&s->input_mutex, &s->input_cond,
275 &s->num_free_in_buffers, s->free_in_buffers, buffer);
276 return OMX_ErrorNone;
277 }
278
279 static OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data,
280 OMX_BUFFERHEADERTYPE *buffer)
281 {
282 OMXCodecContext *s = app_data;
283 append_buffer(&s->output_mutex, &s->output_cond,
284 &s->num_done_out_buffers, s->done_out_buffers, buffer);
285 return OMX_ErrorNone;
286 }
287
288 static const OMX_CALLBACKTYPE callbacks = {
289 event_handler,
290 empty_buffer_done,
291 fill_buffer_done
292 };
293
294 static av_cold int find_component(OMXContext *omx_context, void *logctx,
295 const char *role, char *str, int str_size)
296 {
297 OMX_U32 i, num = 0;
298 char **components;
299 int ret = 0;
300
301 omx_context->ptr_GetComponentsOfRole((OMX_STRING) role, &num, NULL);
302 if (!num) {
303 av_log(logctx, AV_LOG_WARNING, "No component for role %s found\n", role);
304 return AVERROR_ENCODER_NOT_FOUND;
305 }
306 components = av_mallocz(sizeof(char*) * num);
307 if (!components)
308 return AVERROR(ENOMEM);
309 for (i = 0; i < num; i++) {
310 components[i] = av_mallocz(OMX_MAX_STRINGNAME_SIZE);
311 if (!components) {
312 ret = AVERROR(ENOMEM);
313 goto end;
314 }
315 }
316 omx_context->ptr_GetComponentsOfRole((OMX_STRING) role, &num, (OMX_U8**) components);
317 av_strlcpy(str, components[0], str_size);
318 end:
319 for (i = 0; i < num; i++)
320 av_free(components[i]);
321 av_free(components);
322 return ret;
323 }
324
325 static av_cold int wait_for_state(OMXCodecContext *s, OMX_STATETYPE state)
326 {
327 int ret = 0;
328 pthread_mutex_lock(&s->state_mutex);
329 while (s->state != state && s->error == OMX_ErrorNone)
330 pthread_cond_wait(&s->state_cond, &s->state_mutex);
331 if (s->error != OMX_ErrorNone)
332 ret = AVERROR_ENCODER_NOT_FOUND;
333 pthread_mutex_unlock(&s->state_mutex);
334 return ret;
335 }
336
337 static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
338 {
339 OMXCodecContext *s = avctx->priv_data;
340 OMX_PARAM_COMPONENTROLETYPE role_params = { 0 };
341 OMX_PORT_PARAM_TYPE video_port_params = { 0 };
342 OMX_PARAM_PORTDEFINITIONTYPE in_port_params = { 0 }, out_port_params = { 0 };
343 OMX_VIDEO_PARAM_PORTFORMATTYPE video_port_format = { 0 };
344 OMX_VIDEO_PARAM_BITRATETYPE vid_param_bitrate = { 0 };
345 OMX_ERRORTYPE err;
346 int i;
347
348 s->version.s.nVersionMajor = 1;
349 s->version.s.nVersionMinor = 1;
350 s->version.s.nRevision = 2;
351
352 err = s->omx_context->ptr_GetHandle(&s->handle, s->component_name, s, (OMX_CALLBACKTYPE*) &callbacks);
353 if (err != OMX_ErrorNone) {
354 av_log(avctx, AV_LOG_ERROR, "OMX_GetHandle(%s) failed: %x\n", s->component_name, err);
355 return AVERROR_UNKNOWN;
356 }
357
358 // This one crashes the mediaserver on qcom, if used over IOMX
359 INIT_STRUCT(role_params);
360 av_strlcpy(role_params.cRole, role, sizeof(role_params.cRole));
361 // Intentionally ignore errors on this one
362 OMX_SetParameter(s->handle, OMX_IndexParamStandardComponentRole, &role_params);
363
364 INIT_STRUCT(video_port_params);
365 err = OMX_GetParameter(s->handle, OMX_IndexParamVideoInit, &video_port_params);
366 CHECK(err);
367
368 s->in_port = s->out_port = -1;
369 for (i = 0; i < video_port_params.nPorts; i++) {
370 int port = video_port_params.nStartPortNumber + i;
371 OMX_PARAM_PORTDEFINITIONTYPE port_params = { 0 };
372 INIT_STRUCT(port_params);
373 port_params.nPortIndex = port;
374 err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &port_params);
375 if (err != OMX_ErrorNone) {
376 av_log(avctx, AV_LOG_WARNING, "port %d error %x\n", port, err);
377 break;
378 }
379 if (port_params.eDir == OMX_DirInput && s->in_port < 0) {
380 in_port_params = port_params;
381 s->in_port = port;
382 } else if (port_params.eDir == OMX_DirOutput && s->out_port < 0) {
383 out_port_params = port_params;
384 s->out_port = port;
385 }
386 }
387 if (s->in_port < 0 || s->out_port < 0) {
388 av_log(avctx, AV_LOG_ERROR, "No in or out port found (in %d out %d)\n", s->in_port, s->out_port);
389 return AVERROR_UNKNOWN;
390 }
391
392 s->color_format = 0;
393 for (i = 0; ; i++) {
394 INIT_STRUCT(video_port_format);
395 video_port_format.nIndex = i;
396 video_port_format.nPortIndex = s->in_port;
397 if (OMX_GetParameter(s->handle, OMX_IndexParamVideoPortFormat, &video_port_format) != OMX_ErrorNone)
398 break;
399 if (video_port_format.eColorFormat == OMX_COLOR_FormatYUV420Planar ||
400 video_port_format.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar) {
401 s->color_format = video_port_format.eColorFormat;
402 break;
403 }
404 }
405 if (s->color_format == 0) {
406 av_log(avctx, AV_LOG_ERROR, "No supported pixel formats (%d formats available)\n", i);
407 return AVERROR_UNKNOWN;
408 }
409
410 in_port_params.bEnabled = OMX_TRUE;
411 in_port_params.bPopulated = OMX_FALSE;
412 in_port_params.eDomain = OMX_PortDomainVideo;
413
414 in_port_params.format.video.pNativeRender = NULL;
415 in_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
416 in_port_params.format.video.eColorFormat = s->color_format;
417 s->stride = avctx->width;
418 s->plane_size = avctx->height;
419 // If specific codecs need to manually override the stride/plane_size,
420 // that can be done here.
421 in_port_params.format.video.nStride = s->stride;
422 in_port_params.format.video.nSliceHeight = s->plane_size;
423 in_port_params.format.video.nFrameWidth = avctx->width;
424 in_port_params.format.video.nFrameHeight = avctx->height;
425 if (avctx->framerate.den > 0 && avctx->framerate.num > 0)
426 in_port_params.format.video.xFramerate = (1 << 16) * avctx->framerate.num / avctx->framerate.den;
427 else
428 in_port_params.format.video.xFramerate = (1 << 16) * avctx->time_base.den / avctx->time_base.num;
429
430 err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &in_port_params);
431 CHECK(err);
432 err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &in_port_params);
433 CHECK(err);
434 s->stride = in_port_params.format.video.nStride;
435 s->plane_size = in_port_params.format.video.nSliceHeight;
436 s->num_in_buffers = in_port_params.nBufferCountActual;
437
438 err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
439 out_port_params.bEnabled = OMX_TRUE;
440 out_port_params.bPopulated = OMX_FALSE;
441 out_port_params.eDomain = OMX_PortDomainVideo;
442 out_port_params.format.video.pNativeRender = NULL;
443 out_port_params.format.video.nFrameWidth = avctx->width;
444 out_port_params.format.video.nFrameHeight = avctx->height;
445 out_port_params.format.video.nStride = 0;
446 out_port_params.format.video.nSliceHeight = 0;
447 out_port_params.format.video.nBitrate = avctx->bit_rate;
448 out_port_params.format.video.xFramerate = in_port_params.format.video.xFramerate;
449 out_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
450 if (avctx->codec->id == AV_CODEC_ID_MPEG4)
451 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
452 else if (avctx->codec->id == AV_CODEC_ID_H264)
453 out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
454
455 err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
456 CHECK(err);
457 err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
458 CHECK(err);
459 s->num_out_buffers = out_port_params.nBufferCountActual;
460
461 INIT_STRUCT(vid_param_bitrate);
462 vid_param_bitrate.nPortIndex = s->out_port;
463 vid_param_bitrate.eControlRate = OMX_Video_ControlRateVariable;
464 vid_param_bitrate.nTargetBitrate = avctx->bit_rate;
465 err = OMX_SetParameter(s->handle, OMX_IndexParamVideoBitrate, &vid_param_bitrate);
466 if (err != OMX_ErrorNone)
467 av_log(avctx, AV_LOG_WARNING, "Unable to set video bitrate parameter\n");
468
469 if (avctx->codec->id == AV_CODEC_ID_H264) {
470 OMX_VIDEO_PARAM_AVCTYPE avc = { 0 };
471 INIT_STRUCT(avc);
472 avc.nPortIndex = s->out_port;
473 err = OMX_GetParameter(s->handle, OMX_IndexParamVideoAvc, &avc);
474 CHECK(err);
475 avc.nBFrames = 0;
476 avc.nPFrames = avctx->gop_size - 1;
477 err = OMX_SetParameter(s->handle, OMX_IndexParamVideoAvc, &avc);
478 CHECK(err);
479 }
480
481 err = OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
482 CHECK(err);
483
484 s->in_buffer_headers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_in_buffers);
485 s->free_in_buffers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_in_buffers);
486 s->out_buffer_headers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_out_buffers);
487 s->done_out_buffers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_out_buffers);
488 if (!s->in_buffer_headers || !s->free_in_buffers || !s->out_buffer_headers || !s->done_out_buffers)
489 return AVERROR(ENOMEM);
490 for (i = 0; i < s->num_in_buffers && err == OMX_ErrorNone; i++)
491 err = OMX_AllocateBuffer(s->handle, &s->in_buffer_headers[i], s->in_port, s, in_port_params.nBufferSize);
492 CHECK(err);
493 s->num_in_buffers = i;
494 for (i = 0; i < s->num_out_buffers && err == OMX_ErrorNone; i++)
495 err = OMX_AllocateBuffer(s->handle, &s->out_buffer_headers[i], s->out_port, s, out_port_params.nBufferSize);
496 CHECK(err);
497 s->num_out_buffers = i;
498
499 if (wait_for_state(s, OMX_StateIdle) < 0) {
500 av_log(avctx, AV_LOG_ERROR, "Didn't get OMX_StateIdle\n");
501 return AVERROR_UNKNOWN;
502 }
503 err = OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
504 CHECK(err);
505 if (wait_for_state(s, OMX_StateExecuting) < 0) {
506 av_log(avctx, AV_LOG_ERROR, "Didn't get OMX_StateExecuting\n");
507 return AVERROR_UNKNOWN;
508 }
509
510 for (i = 0; i < s->num_out_buffers && err == OMX_ErrorNone; i++)
511 err = OMX_FillThisBuffer(s->handle, s->out_buffer_headers[i]);
512 if (err != OMX_ErrorNone) {
513 for (; i < s->num_out_buffers; i++)
514 s->done_out_buffers[s->num_done_out_buffers++] = s->out_buffer_headers[i];
515 }
516 for (i = 0; i < s->num_in_buffers; i++)
517 s->free_in_buffers[s->num_free_in_buffers++] = s->in_buffer_headers[i];
518 return err != OMX_ErrorNone ? AVERROR_UNKNOWN : 0;
519 }
520
521 static av_cold void cleanup(OMXCodecContext *s)
522 {
523 int i, executing;
524
525 pthread_mutex_lock(&s->state_mutex);
526 executing = s->state == OMX_StateExecuting;
527 pthread_mutex_unlock(&s->state_mutex);
528
529 if (executing) {
530 OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
531 wait_for_state(s, OMX_StateIdle);
532 OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateLoaded, NULL);
533 for (i = 0; i < s->num_in_buffers; i++) {
534 OMX_BUFFERHEADERTYPE *buffer = get_buffer(&s->input_mutex, &s->input_cond,
535 &s->num_free_in_buffers, s->free_in_buffers, 1);
536 OMX_FreeBuffer(s->handle, s->in_port, buffer);
537 }
538 for (i = 0; i < s->num_out_buffers; i++) {
539 OMX_BUFFERHEADERTYPE *buffer = get_buffer(&s->output_mutex, &s->output_cond,
540 &s->num_done_out_buffers, s->done_out_buffers, 1);
541 OMX_FreeBuffer(s->handle, s->out_port, buffer);
542 }
543 wait_for_state(s, OMX_StateLoaded);
544 }
545 if (s->handle) {
546 s->omx_context->ptr_FreeHandle(s->handle);
547 s->handle = NULL;
548 }
549
550 omx_deinit(s->omx_context);
551 s->omx_context = NULL;
552 if (s->mutex_cond_inited) {
553 pthread_cond_destroy(&s->state_cond);
554 pthread_mutex_destroy(&s->state_mutex);
555 pthread_cond_destroy(&s->input_cond);
556 pthread_mutex_destroy(&s->input_mutex);
557 pthread_cond_destroy(&s->output_cond);
558 pthread_mutex_destroy(&s->output_mutex);
559 s->mutex_cond_inited = 0;
560 }
561 av_freep(&s->in_buffer_headers);
562 av_freep(&s->out_buffer_headers);
563 av_freep(&s->free_in_buffers);
564 av_freep(&s->done_out_buffers);
565 av_freep(&s->output_buf);
566 }
567
568 static av_cold int omx_encode_init(AVCodecContext *avctx)
569 {
570 OMXCodecContext *s = avctx->priv_data;
571 int ret = AVERROR_ENCODER_NOT_FOUND;
572 const char *role;
573 OMX_BUFFERHEADERTYPE *buffer;
574 OMX_ERRORTYPE err;
575
576 s->omx_context = omx_init(avctx, s->libname, s->libprefix);
577 if (!s->omx_context)
578 return AVERROR_ENCODER_NOT_FOUND;
579
580 pthread_mutex_init(&s->state_mutex, NULL);
581 pthread_cond_init(&s->state_cond, NULL);
582 pthread_mutex_init(&s->input_mutex, NULL);
583 pthread_cond_init(&s->input_cond, NULL);
584 pthread_mutex_init(&s->output_mutex, NULL);
585 pthread_cond_init(&s->output_cond, NULL);
586 s->mutex_cond_inited = 1;
587 s->avctx = avctx;
588 s->state = OMX_StateLoaded;
589 s->error = OMX_ErrorNone;
590
591 switch (avctx->codec->id) {
592 case AV_CODEC_ID_MPEG4:
593 role = "video_encoder.mpeg4";
594 break;
595 case AV_CODEC_ID_H264:
596 role = "video_encoder.avc";
597 break;
598 default:
599 return AVERROR(ENOSYS);
600 }
601
602 if ((ret = find_component(s->omx_context, avctx, role, s->component_name, sizeof(s->component_name))) < 0)
603 goto fail;
604
605 av_log(avctx, AV_LOG_INFO, "Using %s\n", s->component_name);
606
607 if ((ret = omx_component_init(avctx, role)) < 0)
608 goto fail;
609
610 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
611 while (1) {
612 buffer = get_buffer(&s->output_mutex, &s->output_cond,
613 &s->num_done_out_buffers, s->done_out_buffers, 1);
614 if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
615 if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
616 avctx->extradata_size = 0;
617 goto fail;
618 }
619 memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
620 avctx->extradata_size += buffer->nFilledLen;
621 memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
622 }
623 err = OMX_FillThisBuffer(s->handle, buffer);
624 if (err != OMX_ErrorNone) {
625 append_buffer(&s->output_mutex, &s->output_cond,
626 &s->num_done_out_buffers, s->done_out_buffers, buffer);
627 av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err);
628 ret = AVERROR_UNKNOWN;
629 goto fail;
630 }
631 if (avctx->codec->id == AV_CODEC_ID_H264) {
632 // For H264, the extradata can be returned in two separate buffers
633 // (the videocore encoder on raspberry pi does this);
634 // therefore check that we have got both SPS and PPS before continuing.
635 int nals[32] = { 0 };
636 int i;
637 for (i = 0; i + 4 < avctx->extradata_size; i++) {
638 if (!avctx->extradata[i + 0] &&
639 !avctx->extradata[i + 1] &&
640 !avctx->extradata[i + 2] &&
641 avctx->extradata[i + 3] == 1) {
642 nals[avctx->extradata[i + 4] & 0x1f]++;
643 }
644 }
645 if (nals[NAL_SPS] && nals[NAL_PPS])
646 break;
647 } else {
648 if (avctx->extradata_size > 0)
649 break;
650 }
651 }
652 }
653
654 return 0;
655 fail:
656 return ret;
657 }
658
659
660 static int omx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
661 const AVFrame *frame, int *got_packet)
662 {
663 OMXCodecContext *s = avctx->priv_data;
664 int ret = 0;
665 OMX_BUFFERHEADERTYPE* buffer;
666 OMX_ERRORTYPE err;
667
668 if (frame) {
669 uint8_t *dst[4];
670 int linesize[4];
671 buffer = get_buffer(&s->input_mutex, &s->input_cond,
672 &s->num_free_in_buffers, s->free_in_buffers, 1);
673
674 buffer->nFilledLen = av_image_fill_arrays(dst, linesize, buffer->pBuffer, avctx->pix_fmt, s->stride, s->plane_size, 1);
675 av_image_copy(dst, linesize, (const uint8_t**) frame->data, frame->linesize, avctx->pix_fmt, avctx->width, avctx->height);
676 buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
677 buffer->nOffset = 0;
678 // Convert the timestamps to microseconds; some encoders can ignore
679 // the framerate and do VFR bit allocation based on timestamps.
680 buffer->nTimeStamp = to_omx_ticks(av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q));
681 err = OMX_EmptyThisBuffer(s->handle, buffer);
682 if (err != OMX_ErrorNone) {
683 append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer);
684 av_log(avctx, AV_LOG_ERROR, "OMX_EmptyThisBuffer failed: %x\n", err);
685 return AVERROR_UNKNOWN;
686 }
687 s->num_in_frames++;
688 }
689
690 while (!*got_packet && ret == 0) {
691 // Only wait for output if flushing and not all frames have been output
692 buffer = get_buffer(&s->output_mutex, &s->output_cond,
693 &s->num_done_out_buffers, s->done_out_buffers,
694 !frame && s->num_out_frames < s->num_in_frames);
695 if (!buffer)
696 break;
697
698 if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG && avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
699 if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
700 avctx->extradata_size = 0;
701 goto end;
702 }
703 memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
704 avctx->extradata_size += buffer->nFilledLen;
705 memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
706 } else {
707 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME)
708 s->num_out_frames++;
709 if (!(buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) || !pkt->data) {
710 // If the output packet isn't preallocated, just concatenate everything in our
711 // own buffer
712 int newsize = s->output_buf_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE;
713 if ((ret = av_reallocp(&s->output_buf, newsize)) < 0) {
714 s->output_buf_size = 0;
715 goto end;
716 }
717 memcpy(s->output_buf + s->output_buf_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
718 s->output_buf_size += buffer->nFilledLen;
719 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
720 if ((ret = av_packet_from_data(pkt, s->output_buf, s->output_buf_size)) < 0) {
721 av_freep(&s->output_buf);
722 s->output_buf_size = 0;
723 goto end;
724 }
725 s->output_buf = NULL;
726 s->output_buf_size = 0;
727 }
728 } else {
729 // End of frame, and the caller provided a preallocated frame
730 if ((ret = ff_alloc_packet(pkt, s->output_buf_size + buffer->nFilledLen)) < 0) {
731 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n",
732 (int)(s->output_buf_size + buffer->nFilledLen));
733 goto end;
734 }
735 memcpy(pkt->data, s->output_buf, s->output_buf_size);
736 memcpy(pkt->data + s->output_buf_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
737 av_freep(&s->output_buf);
738 s->output_buf_size = 0;
739 }
740 if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
741 ret = pkt->size;
742 pkt->pts = av_rescale_q(from_omx_ticks(buffer->nTimeStamp), AV_TIME_BASE_Q, avctx->time_base);
743 // We don't currently enable b-frames for the encoders, so set
744 // pkt->dts = pkt->pts. (The calling code behaves worse if the encoder
745 // doesn't set the dts).
746 pkt->dts = pkt->pts;
747 if (buffer->nFlags & OMX_BUFFERFLAG_SYNCFRAME)
748 pkt->flags |= AV_PKT_FLAG_KEY;
749 *got_packet = 1;
750 }
751 }
752 end:
753 err = OMX_FillThisBuffer(s->handle, buffer);
754 if (err != OMX_ErrorNone) {
755 append_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, buffer);
756 av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err);
757 ret = AVERROR_UNKNOWN;
758 }
759 }
760 return ret;
761 }
762
763 static av_cold int omx_encode_end(AVCodecContext *avctx)
764 {
765 OMXCodecContext *s = avctx->priv_data;
766
767 cleanup(s);
768 return 0;
769 }
770
771 #define OFFSET(x) offsetof(OMXCodecContext, x)
772 #define VDE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM
773 static const AVOption options[] = {
774 { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
775 { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
776 { NULL }
777 };
778
779 static const enum AVPixelFormat omx_encoder_pix_fmts[] = {
780 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
781 };
782
783 static const AVClass omx_mpeg4enc_class = {
784 .class_name = "mpeg4_omx",
785 .item_name = av_default_item_name,
786 .option = options,
787 .version = LIBAVUTIL_VERSION_INT,
788 };
789 AVCodec ff_mpeg4_omx_encoder = {
790 .name = "mpeg4_omx",
791 .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL MPEG4 video encoder"),
792 .type = AVMEDIA_TYPE_VIDEO,
793 .id = AV_CODEC_ID_MPEG4,
794 .priv_data_size = sizeof(OMXCodecContext),
795 .init = omx_encode_init,
796 .encode2 = omx_encode_frame,
797 .close = omx_encode_end,
798 .pix_fmts = omx_encoder_pix_fmts,
799 .capabilities = AV_CODEC_CAP_DELAY,
800 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
801 .priv_class = &omx_mpeg4enc_class,
802 };
803
804 static const AVClass omx_h264enc_class = {
805 .class_name = "h264_omx",
806 .item_name = av_default_item_name,
807 .option = options,
808 .version = LIBAVUTIL_VERSION_INT,
809 };
810 AVCodec ff_h264_omx_encoder = {
811 .name = "h264_omx",
812 .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL H264 video encoder"),
813 .type = AVMEDIA_TYPE_VIDEO,
814 .id = AV_CODEC_ID_H264,
815 .priv_data_size = sizeof(OMXCodecContext),
816 .init = omx_encode_init,
817 .encode2 = omx_encode_frame,
818 .close = omx_encode_end,
819 .pix_fmts = omx_encoder_pix_fmts,
820 .capabilities = AV_CODEC_CAP_DELAY,
821 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
822 .priv_class = &omx_h264enc_class,
823 };