Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / gallium / frontends / omx / bellagio / vid_enc.c
1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Christian K├Ânig <christian.koenig@amd.com>
31 *
32 */
33
34
35 #include <assert.h>
36
37 #include <OMX_Video.h>
38
39 /* bellagio defines a DEBUG macro that we don't want */
40 #ifndef DEBUG
41 #include <bellagio/omxcore.h>
42 #undef DEBUG
43 #else
44 #include <bellagio/omxcore.h>
45 #endif
46
47 #include <bellagio/omx_base_video_port.h>
48
49 #include "pipe/p_screen.h"
50 #include "pipe/p_video_codec.h"
51 #include "util/u_memory.h"
52
53 #include "entrypoint.h"
54 #include "vid_enc.h"
55 #include "vid_omx_common.h"
56 #include "vid_enc_common.h"
57
58 static OMX_ERRORTYPE vid_enc_Constructor(OMX_COMPONENTTYPE *comp, OMX_STRING name);
59 static OMX_ERRORTYPE vid_enc_Destructor(OMX_COMPONENTTYPE *comp);
60 static OMX_ERRORTYPE vid_enc_SetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param);
61 static OMX_ERRORTYPE vid_enc_GetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param);
62 static OMX_ERRORTYPE vid_enc_SetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config);
63 static OMX_ERRORTYPE vid_enc_GetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config);
64 static OMX_ERRORTYPE vid_enc_MessageHandler(OMX_COMPONENTTYPE *comp, internalRequestMessageType *msg);
65 static OMX_ERRORTYPE vid_enc_AllocateInBuffer(omx_base_PortType *port, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
66 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size);
67 static OMX_ERRORTYPE vid_enc_UseInBuffer(omx_base_PortType *port, OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx,
68 OMX_PTR private, OMX_U32 size, OMX_U8 *mem);
69 static OMX_ERRORTYPE vid_enc_FreeInBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf);
70 static OMX_ERRORTYPE vid_enc_EncodeFrame(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf);
71 static OMX_ERRORTYPE vid_enc_AllocateOutBuffer(omx_base_PortType *comp, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
72 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size);
73 static OMX_ERRORTYPE vid_enc_FreeOutBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf);
74 static void vid_enc_BufferEncoded(OMX_COMPONENTTYPE *comp, OMX_BUFFERHEADERTYPE* input, OMX_BUFFERHEADERTYPE* output);
75
76 OMX_ERRORTYPE vid_enc_LoaderComponent(stLoaderComponentType *comp)
77 {
78 comp->componentVersion.s.nVersionMajor = 0;
79 comp->componentVersion.s.nVersionMinor = 0;
80 comp->componentVersion.s.nRevision = 0;
81 comp->componentVersion.s.nStep = 1;
82 comp->name_specific_length = 1;
83 comp->constructor = vid_enc_Constructor;
84
85 comp->name = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
86 if (!comp->name)
87 return OMX_ErrorInsufficientResources;
88
89 comp->name_specific = CALLOC(1, sizeof(char *));
90 if (!comp->name_specific)
91 goto error_arrays;
92
93 comp->role_specific = CALLOC(1, sizeof(char *));
94 if (!comp->role_specific)
95 goto error_arrays;
96
97 comp->name_specific[0] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
98 if (comp->name_specific[0] == NULL)
99 goto error_specific;
100
101 comp->role_specific[0] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
102 if (comp->role_specific[0] == NULL)
103 goto error_specific;
104
105 strcpy(comp->name, OMX_VID_ENC_BASE_NAME);
106 strcpy(comp->name_specific[0], OMX_VID_ENC_AVC_NAME);
107 strcpy(comp->role_specific[0], OMX_VID_ENC_AVC_ROLE);
108
109 return OMX_ErrorNone;
110
111 error_specific:
112 FREE(comp->role_specific[0]);
113 FREE(comp->name_specific[0]);
114
115 error_arrays:
116 FREE(comp->role_specific);
117 FREE(comp->name_specific);
118
119 FREE(comp->name);
120
121 return OMX_ErrorInsufficientResources;
122 }
123
124 static OMX_ERRORTYPE vid_enc_Constructor(OMX_COMPONENTTYPE *comp, OMX_STRING name)
125 {
126 vid_enc_PrivateType *priv;
127 omx_base_video_PortType *port;
128 struct pipe_screen *screen;
129 OMX_ERRORTYPE r;
130 int i;
131
132 assert(!comp->pComponentPrivate);
133
134 priv = comp->pComponentPrivate = CALLOC(1, sizeof(vid_enc_PrivateType));
135 if (!priv)
136 return OMX_ErrorInsufficientResources;
137
138 r = omx_base_filter_Constructor(comp, name);
139 if (r)
140 return r;
141
142 priv->BufferMgmtCallback = vid_enc_BufferEncoded;
143 priv->messageHandler = vid_enc_MessageHandler;
144 priv->destructor = vid_enc_Destructor;
145
146 comp->SetParameter = vid_enc_SetParameter;
147 comp->GetParameter = vid_enc_GetParameter;
148 comp->GetConfig = vid_enc_GetConfig;
149 comp->SetConfig = vid_enc_SetConfig;
150
151 priv->screen = omx_get_screen();
152 if (!priv->screen)
153 return OMX_ErrorInsufficientResources;
154
155 screen = priv->screen->pscreen;
156 if (!screen->get_video_param(screen, PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH,
157 PIPE_VIDEO_ENTRYPOINT_ENCODE, PIPE_VIDEO_CAP_SUPPORTED))
158 return OMX_ErrorBadParameter;
159
160 priv->s_pipe = pipe_create_multimedia_context(screen);
161 if (!priv->s_pipe)
162 return OMX_ErrorInsufficientResources;
163
164 enc_InitCompute_common(priv);
165
166 if (!vl_compositor_init(&priv->compositor, priv->s_pipe)) {
167 priv->s_pipe->destroy(priv->s_pipe);
168 priv->s_pipe = NULL;
169 return OMX_ErrorInsufficientResources;
170 }
171
172 if (!vl_compositor_init_state(&priv->cstate, priv->s_pipe)) {
173 vl_compositor_cleanup(&priv->compositor);
174 priv->s_pipe->destroy(priv->s_pipe);
175 priv->s_pipe = NULL;
176 return OMX_ErrorInsufficientResources;
177 }
178
179 priv->t_pipe = pipe_create_multimedia_context(screen);
180 if (!priv->t_pipe)
181 return OMX_ErrorInsufficientResources;
182
183 priv->sPortTypesParam[OMX_PortDomainVideo].nStartPortNumber = 0;
184 priv->sPortTypesParam[OMX_PortDomainVideo].nPorts = 2;
185 priv->ports = CALLOC(2, sizeof(omx_base_PortType *));
186 if (!priv->ports)
187 return OMX_ErrorInsufficientResources;
188
189 for (i = 0; i < 2; ++i) {
190 priv->ports[i] = CALLOC(1, sizeof(omx_base_video_PortType));
191 if (!priv->ports[i])
192 return OMX_ErrorInsufficientResources;
193
194 base_video_port_Constructor(comp, &priv->ports[i], i, i == 0);
195 }
196
197 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
198 port->sPortParam.format.video.nFrameWidth = 176;
199 port->sPortParam.format.video.nFrameHeight = 144;
200 port->sPortParam.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
201 port->sVideoParam.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
202 port->sPortParam.nBufferCountActual = 8;
203 port->sPortParam.nBufferCountMin = 4;
204
205 port->Port_SendBufferFunction = vid_enc_EncodeFrame;
206 port->Port_AllocateBuffer = vid_enc_AllocateInBuffer;
207 port->Port_UseBuffer = vid_enc_UseInBuffer;
208 port->Port_FreeBuffer = vid_enc_FreeInBuffer;
209
210 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
211 strcpy(port->sPortParam.format.video.cMIMEType,"video/H264");
212 port->sPortParam.format.video.nFrameWidth = 176;
213 port->sPortParam.format.video.nFrameHeight = 144;
214 port->sPortParam.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
215 port->sVideoParam.eCompressionFormat = OMX_VIDEO_CodingAVC;
216
217 port->Port_AllocateBuffer = vid_enc_AllocateOutBuffer;
218 port->Port_FreeBuffer = vid_enc_FreeOutBuffer;
219
220 priv->bitrate.eControlRate = OMX_Video_ControlRateDisable;
221 priv->bitrate.nTargetBitrate = 0;
222
223 priv->quant.nQpI = OMX_VID_ENC_QUANT_I_FRAMES_DEFAULT;
224 priv->quant.nQpP = OMX_VID_ENC_QUANT_P_FRAMES_DEFAULT;
225 priv->quant.nQpB = OMX_VID_ENC_QUANT_B_FRAMES_DEFAULT;
226
227 priv->profile_level.eProfile = OMX_VIDEO_AVCProfileBaseline;
228 priv->profile_level.eLevel = OMX_VIDEO_AVCLevel51;
229
230 priv->force_pic_type.IntraRefreshVOP = OMX_FALSE;
231 priv->frame_num = 0;
232 priv->pic_order_cnt = 0;
233 priv->restricted_b_frames = debug_get_bool_option("OMX_USE_RESTRICTED_B_FRAMES", FALSE);
234
235 priv->scale.xWidth = OMX_VID_ENC_SCALING_WIDTH_DEFAULT;
236 priv->scale.xHeight = OMX_VID_ENC_SCALING_WIDTH_DEFAULT;
237
238 list_inithead(&priv->free_tasks);
239 list_inithead(&priv->used_tasks);
240 list_inithead(&priv->b_frames);
241 list_inithead(&priv->stacked_tasks);
242
243 return OMX_ErrorNone;
244 }
245
246 static OMX_ERRORTYPE vid_enc_Destructor(OMX_COMPONENTTYPE *comp)
247 {
248 vid_enc_PrivateType* priv = comp->pComponentPrivate;
249 int i;
250
251 enc_ReleaseTasks(&priv->free_tasks);
252 enc_ReleaseTasks(&priv->used_tasks);
253 enc_ReleaseTasks(&priv->b_frames);
254 enc_ReleaseTasks(&priv->stacked_tasks);
255
256 if (priv->ports) {
257 for (i = 0; i < priv->sPortTypesParam[OMX_PortDomainVideo].nPorts; ++i) {
258 if(priv->ports[i])
259 priv->ports[i]->PortDestructor(priv->ports[i]);
260 }
261 FREE(priv->ports);
262 priv->ports=NULL;
263 }
264
265 for (i = 0; i < OMX_VID_ENC_NUM_SCALING_BUFFERS; ++i)
266 if (priv->scale_buffer[i])
267 priv->scale_buffer[i]->destroy(priv->scale_buffer[i]);
268
269 if (priv->s_pipe) {
270 vl_compositor_cleanup_state(&priv->cstate);
271 vl_compositor_cleanup(&priv->compositor);
272 enc_ReleaseCompute_common(priv);
273 priv->s_pipe->destroy(priv->s_pipe);
274 }
275
276 if (priv->t_pipe)
277 priv->t_pipe->destroy(priv->t_pipe);
278
279 if (priv->screen)
280 omx_put_screen();
281
282 return omx_workaround_Destructor(comp);
283 }
284
285 static OMX_ERRORTYPE enc_AllocateBackTexture(omx_base_PortType *port,
286 struct pipe_resource **resource,
287 struct pipe_transfer **transfer,
288 OMX_U8 **map)
289 {
290 OMX_COMPONENTTYPE* comp = port->standCompContainer;
291 vid_enc_PrivateType *priv = comp->pComponentPrivate;
292 struct pipe_resource buf_templ;
293 struct pipe_box box = {};
294 OMX_U8 *ptr;
295
296 memset(&buf_templ, 0, sizeof buf_templ);
297 buf_templ.target = PIPE_TEXTURE_2D;
298 buf_templ.format = PIPE_FORMAT_I8_UNORM;
299 buf_templ.bind = PIPE_BIND_LINEAR;
300 buf_templ.usage = PIPE_USAGE_STAGING;
301 buf_templ.flags = 0;
302 buf_templ.width0 = port->sPortParam.format.video.nFrameWidth;
303 buf_templ.height0 = port->sPortParam.format.video.nFrameHeight * 3 / 2;
304 buf_templ.depth0 = 1;
305 buf_templ.array_size = 1;
306
307 *resource = priv->s_pipe->screen->resource_create(priv->s_pipe->screen, &buf_templ);
308 if (!*resource)
309 return OMX_ErrorInsufficientResources;
310
311 box.width = (*resource)->width0;
312 box.height = (*resource)->height0;
313 box.depth = (*resource)->depth0;
314 ptr = priv->s_pipe->transfer_map(priv->s_pipe, *resource, 0, PIPE_TRANSFER_WRITE, &box, transfer);
315 if (map)
316 *map = ptr;
317
318 return OMX_ErrorNone;
319 }
320
321 static OMX_ERRORTYPE vid_enc_SetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param)
322 {
323 OMX_COMPONENTTYPE *comp = handle;
324 vid_enc_PrivateType *priv = comp->pComponentPrivate;
325 OMX_ERRORTYPE r;
326
327 if (!param)
328 return OMX_ErrorBadParameter;
329
330 switch(idx) {
331 case OMX_IndexParamPortDefinition: {
332 OMX_PARAM_PORTDEFINITIONTYPE *def = param;
333
334 r = omx_base_component_SetParameter(handle, idx, param);
335 if (r)
336 return r;
337
338 if (def->nPortIndex == OMX_BASE_FILTER_INPUTPORT_INDEX) {
339 omx_base_video_PortType *port;
340 unsigned framesize;
341 struct pipe_resource *resource;
342 struct pipe_transfer *transfer;
343
344 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
345 enc_AllocateBackTexture(priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX],
346 &resource, &transfer, NULL);
347 port->sPortParam.format.video.nStride = transfer->stride;
348 pipe_transfer_unmap(priv->s_pipe, transfer);
349 pipe_resource_reference(&resource, NULL);
350
351 framesize = port->sPortParam.format.video.nStride *
352 port->sPortParam.format.video.nFrameHeight;
353 port->sPortParam.format.video.nSliceHeight = port->sPortParam.format.video.nFrameHeight;
354 port->sPortParam.nBufferSize = framesize * 3 / 2;
355
356 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
357 port->sPortParam.nBufferSize = framesize * 512 / (16*16);
358
359 priv->frame_rate = def->format.video.xFramerate;
360
361 priv->callbacks->EventHandler(comp, priv->callbackData, OMX_EventPortSettingsChanged,
362 OMX_BASE_FILTER_OUTPUTPORT_INDEX, 0, NULL);
363 }
364 break;
365 }
366 case OMX_IndexParamStandardComponentRole: {
367 OMX_PARAM_COMPONENTROLETYPE *role = param;
368
369 r = checkHeader(param, sizeof(OMX_PARAM_COMPONENTROLETYPE));
370 if (r)
371 return r;
372
373 if (strcmp((char *)role->cRole, OMX_VID_ENC_AVC_ROLE)) {
374 return OMX_ErrorBadParameter;
375 }
376
377 break;
378 }
379 case OMX_IndexParamVideoBitrate: {
380 OMX_VIDEO_PARAM_BITRATETYPE *bitrate = param;
381
382 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_BITRATETYPE));
383 if (r)
384 return r;
385
386 priv->bitrate = *bitrate;
387
388 break;
389 }
390 case OMX_IndexParamVideoQuantization: {
391 OMX_VIDEO_PARAM_QUANTIZATIONTYPE *quant = param;
392
393 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_QUANTIZATIONTYPE));
394 if (r)
395 return r;
396
397 priv->quant = *quant;
398
399 break;
400 }
401 case OMX_IndexParamVideoProfileLevelCurrent: {
402 OMX_VIDEO_PARAM_PROFILELEVELTYPE *profile_level = param;
403
404 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
405 if (r)
406 return r;
407
408 priv->profile_level = *profile_level;
409
410 break;
411 }
412 default:
413 return omx_base_component_SetParameter(handle, idx, param);
414 }
415 return OMX_ErrorNone;
416 }
417
418 static OMX_ERRORTYPE vid_enc_GetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param)
419 {
420 OMX_COMPONENTTYPE *comp = handle;
421 vid_enc_PrivateType *priv = comp->pComponentPrivate;
422 OMX_ERRORTYPE r;
423
424 if (!param)
425 return OMX_ErrorBadParameter;
426
427 switch(idx) {
428 case OMX_IndexParamStandardComponentRole: {
429 OMX_PARAM_COMPONENTROLETYPE *role = param;
430
431 r = checkHeader(param, sizeof(OMX_PARAM_COMPONENTROLETYPE));
432 if (r)
433 return r;
434
435 strcpy((char *)role->cRole, OMX_VID_ENC_AVC_ROLE);
436 break;
437 }
438 case OMX_IndexParamVideoInit:
439 r = checkHeader(param, sizeof(OMX_PORT_PARAM_TYPE));
440 if (r)
441 return r;
442
443 memcpy(param, &priv->sPortTypesParam[OMX_PortDomainVideo], sizeof(OMX_PORT_PARAM_TYPE));
444 break;
445
446 case OMX_IndexParamVideoPortFormat: {
447 OMX_VIDEO_PARAM_PORTFORMATTYPE *format = param;
448 omx_base_video_PortType *port;
449
450 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
451 if (r)
452 return r;
453
454 if (format->nPortIndex > 1)
455 return OMX_ErrorBadPortIndex;
456 if (format->nIndex >= 1)
457 return OMX_ErrorNoMore;
458
459 port = (omx_base_video_PortType *)priv->ports[format->nPortIndex];
460 memcpy(format, &port->sVideoParam, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
461 break;
462 }
463 case OMX_IndexParamVideoBitrate: {
464 OMX_VIDEO_PARAM_BITRATETYPE *bitrate = param;
465
466 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_BITRATETYPE));
467 if (r)
468 return r;
469
470 bitrate->eControlRate = priv->bitrate.eControlRate;
471 bitrate->nTargetBitrate = priv->bitrate.nTargetBitrate;
472
473 break;
474 }
475 case OMX_IndexParamVideoQuantization: {
476 OMX_VIDEO_PARAM_QUANTIZATIONTYPE *quant = param;
477
478 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_QUANTIZATIONTYPE));
479 if (r)
480 return r;
481
482 quant->nQpI = priv->quant.nQpI;
483 quant->nQpP = priv->quant.nQpP;
484 quant->nQpB = priv->quant.nQpB;
485
486 break;
487 }
488 case OMX_IndexParamVideoProfileLevelCurrent: {
489 OMX_VIDEO_PARAM_PROFILELEVELTYPE *profile_level = param;
490
491 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
492 if (r)
493 return r;
494
495 profile_level->eProfile = priv->profile_level.eProfile;
496 profile_level->eLevel = priv->profile_level.eLevel;
497
498 break;
499 }
500 default:
501 return omx_base_component_GetParameter(handle, idx, param);
502 }
503 return OMX_ErrorNone;
504 }
505
506 static OMX_ERRORTYPE vid_enc_SetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config)
507 {
508 OMX_COMPONENTTYPE *comp = handle;
509 vid_enc_PrivateType *priv = comp->pComponentPrivate;
510 OMX_ERRORTYPE r;
511 int i;
512
513 if (!config)
514 return OMX_ErrorBadParameter;
515
516 switch(idx) {
517 case OMX_IndexConfigVideoIntraVOPRefresh: {
518 OMX_CONFIG_INTRAREFRESHVOPTYPE *type = config;
519
520 r = checkHeader(config, sizeof(OMX_CONFIG_INTRAREFRESHVOPTYPE));
521 if (r)
522 return r;
523
524 priv->force_pic_type = *type;
525
526 break;
527 }
528 case OMX_IndexConfigCommonScale: {
529 OMX_CONFIG_SCALEFACTORTYPE *scale = config;
530
531 r = checkHeader(config, sizeof(OMX_CONFIG_SCALEFACTORTYPE));
532 if (r)
533 return r;
534
535 if (scale->xWidth < 176 || scale->xHeight < 144)
536 return OMX_ErrorBadParameter;
537
538 for (i = 0; i < OMX_VID_ENC_NUM_SCALING_BUFFERS; ++i) {
539 if (priv->scale_buffer[i]) {
540 priv->scale_buffer[i]->destroy(priv->scale_buffer[i]);
541 priv->scale_buffer[i] = NULL;
542 }
543 }
544
545 priv->scale = *scale;
546 if (priv->scale.xWidth != 0xffffffff && priv->scale.xHeight != 0xffffffff) {
547 struct pipe_video_buffer templat = {};
548
549 templat.buffer_format = PIPE_FORMAT_NV12;
550 templat.width = priv->scale.xWidth;
551 templat.height = priv->scale.xHeight;
552 templat.interlaced = false;
553 for (i = 0; i < OMX_VID_ENC_NUM_SCALING_BUFFERS; ++i) {
554 priv->scale_buffer[i] = priv->s_pipe->create_video_buffer(priv->s_pipe, &templat);
555 if (!priv->scale_buffer[i])
556 return OMX_ErrorInsufficientResources;
557 }
558 }
559
560 break;
561 }
562 default:
563 return omx_base_component_SetConfig(handle, idx, config);
564 }
565
566 return OMX_ErrorNone;
567 }
568
569 static OMX_ERRORTYPE vid_enc_GetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config)
570 {
571 OMX_COMPONENTTYPE *comp = handle;
572 vid_enc_PrivateType *priv = comp->pComponentPrivate;
573 OMX_ERRORTYPE r;
574
575 if (!config)
576 return OMX_ErrorBadParameter;
577
578 switch(idx) {
579 case OMX_IndexConfigCommonScale: {
580 OMX_CONFIG_SCALEFACTORTYPE *scale = config;
581
582 r = checkHeader(config, sizeof(OMX_CONFIG_SCALEFACTORTYPE));
583 if (r)
584 return r;
585
586 scale->xWidth = priv->scale.xWidth;
587 scale->xHeight = priv->scale.xHeight;
588
589 break;
590 }
591 default:
592 return omx_base_component_GetConfig(handle, idx, config);
593 }
594
595 return OMX_ErrorNone;
596 }
597
598 static OMX_ERRORTYPE vid_enc_MessageHandler(OMX_COMPONENTTYPE* comp, internalRequestMessageType *msg)
599 {
600 vid_enc_PrivateType* priv = comp->pComponentPrivate;
601
602 if (msg->messageType == OMX_CommandStateSet) {
603 if ((msg->messageParam == OMX_StateIdle ) && (priv->state == OMX_StateLoaded)) {
604
605 struct pipe_video_codec templat = {};
606 omx_base_video_PortType *port;
607
608 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
609
610 templat.profile = enc_TranslateOMXProfileToPipe(priv->profile_level.eProfile);
611 templat.level = enc_TranslateOMXLevelToPipe(priv->profile_level.eLevel);
612 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_ENCODE;
613 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
614 templat.width = priv->scale_buffer[priv->current_scale_buffer] ?
615 priv->scale.xWidth : port->sPortParam.format.video.nFrameWidth;
616 templat.height = priv->scale_buffer[priv->current_scale_buffer] ?
617 priv->scale.xHeight : port->sPortParam.format.video.nFrameHeight;
618
619 if (templat.profile == PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE) {
620 struct pipe_screen *screen = priv->screen->pscreen;
621 templat.max_references = 1;
622 priv->stacked_frames_num =
623 screen->get_video_param(screen,
624 PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH,
625 PIPE_VIDEO_ENTRYPOINT_ENCODE,
626 PIPE_VIDEO_CAP_STACKED_FRAMES);
627 } else {
628 templat.max_references = OMX_VID_ENC_P_PERIOD_DEFAULT;
629 priv->stacked_frames_num = 1;
630 }
631 priv->codec = priv->s_pipe->create_video_codec(priv->s_pipe, &templat);
632
633 } else if ((msg->messageParam == OMX_StateLoaded) && (priv->state == OMX_StateIdle)) {
634 if (priv->codec) {
635 priv->codec->destroy(priv->codec);
636 priv->codec = NULL;
637 }
638 }
639 }
640
641 return omx_base_component_MessageHandler(comp, msg);
642 }
643
644 static OMX_ERRORTYPE vid_enc_AllocateInBuffer(omx_base_PortType *port, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
645 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size)
646 {
647 struct input_buf_private *inp;
648 OMX_ERRORTYPE r;
649
650 r = base_port_AllocateBuffer(port, buf, idx, private, size);
651 if (r)
652 return r;
653
654 inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
655 if (!inp) {
656 base_port_FreeBuffer(port, idx, *buf);
657 return OMX_ErrorInsufficientResources;
658 }
659
660 list_inithead(&inp->tasks);
661
662 FREE((*buf)->pBuffer);
663 r = enc_AllocateBackTexture(port, &inp->resource, &inp->transfer, &(*buf)->pBuffer);
664 if (r) {
665 FREE(inp);
666 base_port_FreeBuffer(port, idx, *buf);
667 return r;
668 }
669
670 return OMX_ErrorNone;
671 }
672
673 static OMX_ERRORTYPE vid_enc_UseInBuffer(omx_base_PortType *port, OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx,
674 OMX_PTR private, OMX_U32 size, OMX_U8 *mem)
675 {
676 struct input_buf_private *inp;
677 OMX_ERRORTYPE r;
678
679 r = base_port_UseBuffer(port, buf, idx, private, size, mem);
680 if (r)
681 return r;
682
683 inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
684 if (!inp) {
685 base_port_FreeBuffer(port, idx, *buf);
686 return OMX_ErrorInsufficientResources;
687 }
688
689 list_inithead(&inp->tasks);
690
691 return OMX_ErrorNone;
692 }
693
694 static OMX_ERRORTYPE vid_enc_FreeInBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf)
695 {
696 OMX_COMPONENTTYPE* comp = port->standCompContainer;
697 vid_enc_PrivateType *priv = comp->pComponentPrivate;
698 struct input_buf_private *inp = buf->pInputPortPrivate;
699
700 if (inp) {
701 enc_ReleaseTasks(&inp->tasks);
702 if (inp->transfer)
703 pipe_transfer_unmap(priv->s_pipe, inp->transfer);
704 pipe_resource_reference(&inp->resource, NULL);
705 FREE(inp);
706 }
707 buf->pBuffer = NULL;
708
709 return base_port_FreeBuffer(port, idx, buf);
710 }
711
712 static OMX_ERRORTYPE vid_enc_AllocateOutBuffer(omx_base_PortType *port, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
713 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size)
714 {
715 OMX_ERRORTYPE r;
716
717 r = base_port_AllocateBuffer(port, buf, idx, private, size);
718 if (r)
719 return r;
720
721 FREE((*buf)->pBuffer);
722 (*buf)->pBuffer = NULL;
723 (*buf)->pOutputPortPrivate = CALLOC(1, sizeof(struct output_buf_private));
724 if (!(*buf)->pOutputPortPrivate) {
725 base_port_FreeBuffer(port, idx, *buf);
726 return OMX_ErrorInsufficientResources;
727 }
728
729 return OMX_ErrorNone;
730 }
731
732 static OMX_ERRORTYPE vid_enc_FreeOutBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf)
733 {
734 OMX_COMPONENTTYPE* comp = port->standCompContainer;
735 vid_enc_PrivateType *priv = comp->pComponentPrivate;
736
737 if (buf->pOutputPortPrivate) {
738 struct output_buf_private *outp = buf->pOutputPortPrivate;
739 if (outp->transfer)
740 pipe_transfer_unmap(priv->t_pipe, outp->transfer);
741 pipe_resource_reference(&outp->bitstream, NULL);
742 FREE(outp);
743 buf->pOutputPortPrivate = NULL;
744 }
745 buf->pBuffer = NULL;
746
747 return base_port_FreeBuffer(port, idx, buf);
748 }
749
750 static struct encode_task *enc_NeedTask(omx_base_PortType *port)
751 {
752 OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
753 OMX_COMPONENTTYPE* comp = port->standCompContainer;
754 vid_enc_PrivateType *priv = comp->pComponentPrivate;
755
756 return enc_NeedTask_common(priv, def);
757 }
758
759 static OMX_ERRORTYPE enc_LoadImage(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf,
760 struct pipe_video_buffer *vbuf)
761 {
762 OMX_COMPONENTTYPE* comp = port->standCompContainer;
763 vid_enc_PrivateType *priv = comp->pComponentPrivate;
764 OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
765 return enc_LoadImage_common(priv, def, buf, vbuf);
766 }
767
768 static void enc_ScaleInput(omx_base_PortType *port, struct pipe_video_buffer **vbuf, unsigned *size)
769 {
770 OMX_COMPONENTTYPE* comp = port->standCompContainer;
771 vid_enc_PrivateType *priv = comp->pComponentPrivate;
772 OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
773 enc_ScaleInput_common(priv, def, vbuf, size);
774 }
775
776 static void enc_ControlPicture(omx_base_PortType *port, struct pipe_h264_enc_picture_desc *picture)
777 {
778 OMX_COMPONENTTYPE* comp = port->standCompContainer;
779 vid_enc_PrivateType *priv = comp->pComponentPrivate;
780 enc_ControlPicture_common(priv, picture);
781 }
782
783 static void enc_HandleTask(omx_base_PortType *port, struct encode_task *task,
784 enum pipe_h264_enc_picture_type picture_type)
785 {
786 OMX_COMPONENTTYPE* comp = port->standCompContainer;
787 vid_enc_PrivateType *priv = comp->pComponentPrivate;
788 unsigned size = priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX]->sPortParam.nBufferSize;
789 struct pipe_video_buffer *vbuf = task->buf;
790 struct pipe_h264_enc_picture_desc picture = {};
791
792 /* -------------- scale input image --------- */
793 enc_ScaleInput(port, &vbuf, &size);
794 priv->s_pipe->flush(priv->s_pipe, NULL, 0);
795
796 /* -------------- allocate output buffer --------- */
797 task->bitstream = pipe_buffer_create(priv->s_pipe->screen,
798 PIPE_BIND_VERTEX_BUFFER,
799 PIPE_USAGE_STAGING, /* map for read */
800 size);
801
802 picture.picture_type = picture_type;
803 picture.pic_order_cnt = task->pic_order_cnt;
804 picture.base.profile = enc_TranslateOMXProfileToPipe(priv->profile_level.eProfile);
805 picture.base.entry_point = PIPE_VIDEO_ENTRYPOINT_ENCODE;
806 if (priv->restricted_b_frames && picture_type == PIPE_H264_ENC_PICTURE_TYPE_B)
807 picture.not_referenced = true;
808 enc_ControlPicture(port, &picture);
809
810 /* -------------- encode frame --------- */
811 priv->codec->begin_frame(priv->codec, vbuf, &picture.base);
812 priv->codec->encode_bitstream(priv->codec, vbuf, task->bitstream, &task->feedback);
813 priv->codec->end_frame(priv->codec, vbuf, &picture.base);
814 }
815
816 static void enc_ClearBframes(omx_base_PortType *port, struct input_buf_private *inp)
817 {
818 OMX_COMPONENTTYPE* comp = port->standCompContainer;
819 vid_enc_PrivateType *priv = comp->pComponentPrivate;
820 struct encode_task *task;
821
822 if (list_is_empty(&priv->b_frames))
823 return;
824
825 task = LIST_ENTRY(struct encode_task, priv->b_frames.prev, list);
826 list_del(&task->list);
827
828 /* promote last from to P frame */
829 priv->ref_idx_l0 = priv->ref_idx_l1;
830 enc_HandleTask(port, task, PIPE_H264_ENC_PICTURE_TYPE_P);
831 list_addtail(&task->list, &inp->tasks);
832 priv->ref_idx_l1 = priv->frame_num++;
833
834 /* handle B frames */
835 LIST_FOR_EACH_ENTRY(task, &priv->b_frames, list) {
836 enc_HandleTask(port, task, PIPE_H264_ENC_PICTURE_TYPE_B);
837 if (!priv->restricted_b_frames)
838 priv->ref_idx_l0 = priv->frame_num;
839 priv->frame_num++;
840 }
841
842 enc_MoveTasks(&priv->b_frames, &inp->tasks);
843 }
844
845 static OMX_ERRORTYPE vid_enc_EncodeFrame(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf)
846 {
847 OMX_COMPONENTTYPE* comp = port->standCompContainer;
848 vid_enc_PrivateType *priv = comp->pComponentPrivate;
849 struct input_buf_private *inp = buf->pInputPortPrivate;
850 enum pipe_h264_enc_picture_type picture_type;
851 struct encode_task *task;
852 unsigned stacked_num = 0;
853 OMX_ERRORTYPE err;
854
855 enc_MoveTasks(&inp->tasks, &priv->free_tasks);
856 task = enc_NeedTask(port);
857 if (!task)
858 return OMX_ErrorInsufficientResources;
859
860 if (buf->nFilledLen == 0) {
861 if (buf->nFlags & OMX_BUFFERFLAG_EOS) {
862 buf->nFilledLen = buf->nAllocLen;
863 enc_ClearBframes(port, inp);
864 enc_MoveTasks(&priv->stacked_tasks, &inp->tasks);
865 priv->codec->flush(priv->codec);
866 }
867 return base_port_SendBufferFunction(port, buf);
868 }
869
870 if (buf->pOutputPortPrivate) {
871 struct pipe_video_buffer *vbuf = buf->pOutputPortPrivate;
872 buf->pOutputPortPrivate = task->buf;
873 task->buf = vbuf;
874 } else {
875 /* ------- load input image into video buffer ---- */
876 err = enc_LoadImage(port, buf, task->buf);
877 if (err != OMX_ErrorNone) {
878 FREE(task);
879 return err;
880 }
881 }
882
883 /* -------------- determine picture type --------- */
884 if (!(priv->pic_order_cnt % OMX_VID_ENC_IDR_PERIOD_DEFAULT) ||
885 priv->force_pic_type.IntraRefreshVOP) {
886 enc_ClearBframes(port, inp);
887 picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
888 priv->force_pic_type.IntraRefreshVOP = OMX_FALSE;
889 priv->frame_num = 0;
890 } else if (priv->codec->profile == PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE ||
891 !(priv->pic_order_cnt % OMX_VID_ENC_P_PERIOD_DEFAULT) ||
892 (buf->nFlags & OMX_BUFFERFLAG_EOS)) {
893 picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
894 } else {
895 picture_type = PIPE_H264_ENC_PICTURE_TYPE_B;
896 }
897
898 task->pic_order_cnt = priv->pic_order_cnt++;
899
900 if (picture_type == PIPE_H264_ENC_PICTURE_TYPE_B) {
901 /* put frame at the tail of the queue */
902 list_addtail(&task->list, &priv->b_frames);
903 } else {
904 /* handle I or P frame */
905 priv->ref_idx_l0 = priv->ref_idx_l1;
906 enc_HandleTask(port, task, picture_type);
907 list_addtail(&task->list, &priv->stacked_tasks);
908 LIST_FOR_EACH_ENTRY(task, &priv->stacked_tasks, list) {
909 ++stacked_num;
910 }
911 if (stacked_num == priv->stacked_frames_num) {
912 struct encode_task *t;
913 t = LIST_ENTRY(struct encode_task, priv->stacked_tasks.next, list);
914 list_del(&t->list);
915 list_addtail(&t->list, &inp->tasks);
916 }
917 priv->ref_idx_l1 = priv->frame_num++;
918
919 /* handle B frames */
920 LIST_FOR_EACH_ENTRY(task, &priv->b_frames, list) {
921 enc_HandleTask(port, task, PIPE_H264_ENC_PICTURE_TYPE_B);
922 if (!priv->restricted_b_frames)
923 priv->ref_idx_l0 = priv->frame_num;
924 priv->frame_num++;
925 }
926
927 enc_MoveTasks(&priv->b_frames, &inp->tasks);
928 }
929
930 if (list_is_empty(&inp->tasks))
931 return port->ReturnBufferFunction(port, buf);
932 else
933 return base_port_SendBufferFunction(port, buf);
934 }
935
936 static void vid_enc_BufferEncoded(OMX_COMPONENTTYPE *comp, OMX_BUFFERHEADERTYPE* input, OMX_BUFFERHEADERTYPE* output)
937 {
938 vid_enc_PrivateType *priv = comp->pComponentPrivate;
939 vid_enc_BufferEncoded_common(priv, input, output);
940 }