st/omx_bellagio: add picture profile and entry point
[mesa.git] / src / gallium / state_trackers / 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 #include "vl/vl_video_buffer.h"
53
54 #include "entrypoint.h"
55 #include "vid_enc.h"
56
57 struct encode_task {
58 struct list_head list;
59
60 struct pipe_video_buffer *buf;
61 unsigned pic_order_cnt;
62 struct pipe_resource *bitstream;
63 void *feedback;
64 };
65
66 struct input_buf_private {
67 struct list_head tasks;
68
69 struct pipe_resource *resource;
70 struct pipe_transfer *transfer;
71 };
72
73 struct output_buf_private {
74 struct pipe_resource *bitstream;
75 struct pipe_transfer *transfer;
76 };
77
78 static OMX_ERRORTYPE vid_enc_Constructor(OMX_COMPONENTTYPE *comp, OMX_STRING name);
79 static OMX_ERRORTYPE vid_enc_Destructor(OMX_COMPONENTTYPE *comp);
80 static OMX_ERRORTYPE vid_enc_SetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param);
81 static OMX_ERRORTYPE vid_enc_GetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param);
82 static OMX_ERRORTYPE vid_enc_SetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config);
83 static OMX_ERRORTYPE vid_enc_GetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config);
84 static OMX_ERRORTYPE vid_enc_MessageHandler(OMX_COMPONENTTYPE *comp, internalRequestMessageType *msg);
85 static OMX_ERRORTYPE vid_enc_AllocateInBuffer(omx_base_PortType *port, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
86 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size);
87 static OMX_ERRORTYPE vid_enc_UseInBuffer(omx_base_PortType *port, OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx,
88 OMX_PTR private, OMX_U32 size, OMX_U8 *mem);
89 static OMX_ERRORTYPE vid_enc_FreeInBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf);
90 static OMX_ERRORTYPE vid_enc_EncodeFrame(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf);
91 static OMX_ERRORTYPE vid_enc_AllocateOutBuffer(omx_base_PortType *comp, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
92 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size);
93 static OMX_ERRORTYPE vid_enc_FreeOutBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf);
94 static void vid_enc_BufferEncoded(OMX_COMPONENTTYPE *comp, OMX_BUFFERHEADERTYPE* input, OMX_BUFFERHEADERTYPE* output);
95
96 static void enc_ReleaseTasks(struct list_head *head);
97
98 OMX_ERRORTYPE vid_enc_LoaderComponent(stLoaderComponentType *comp)
99 {
100 comp->componentVersion.s.nVersionMajor = 0;
101 comp->componentVersion.s.nVersionMinor = 0;
102 comp->componentVersion.s.nRevision = 0;
103 comp->componentVersion.s.nStep = 1;
104 comp->name_specific_length = 1;
105 comp->constructor = vid_enc_Constructor;
106
107 comp->name = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
108 if (!comp->name)
109 return OMX_ErrorInsufficientResources;
110
111 comp->name_specific = CALLOC(1, sizeof(char *));
112 if (!comp->name_specific)
113 goto error_arrays;
114
115 comp->role_specific = CALLOC(1, sizeof(char *));
116 if (!comp->role_specific)
117 goto error_arrays;
118
119 comp->name_specific[0] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
120 if (comp->name_specific[0] == NULL)
121 goto error_specific;
122
123 comp->role_specific[0] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
124 if (comp->role_specific[0] == NULL)
125 goto error_specific;
126
127 strcpy(comp->name, OMX_VID_ENC_BASE_NAME);
128 strcpy(comp->name_specific[0], OMX_VID_ENC_AVC_NAME);
129 strcpy(comp->role_specific[0], OMX_VID_ENC_AVC_ROLE);
130
131 return OMX_ErrorNone;
132
133 error_specific:
134 FREE(comp->role_specific[0]);
135 FREE(comp->name_specific[0]);
136
137 error_arrays:
138 FREE(comp->role_specific);
139 FREE(comp->name_specific);
140
141 FREE(comp->name);
142
143 return OMX_ErrorInsufficientResources;
144 }
145
146 static OMX_ERRORTYPE vid_enc_Constructor(OMX_COMPONENTTYPE *comp, OMX_STRING name)
147 {
148 vid_enc_PrivateType *priv;
149 omx_base_video_PortType *port;
150 struct pipe_screen *screen;
151 OMX_ERRORTYPE r;
152 int i;
153
154 assert(!comp->pComponentPrivate);
155
156 priv = comp->pComponentPrivate = CALLOC(1, sizeof(vid_enc_PrivateType));
157 if (!priv)
158 return OMX_ErrorInsufficientResources;
159
160 r = omx_base_filter_Constructor(comp, name);
161 if (r)
162 return r;
163
164 priv->BufferMgmtCallback = vid_enc_BufferEncoded;
165 priv->messageHandler = vid_enc_MessageHandler;
166 priv->destructor = vid_enc_Destructor;
167
168 comp->SetParameter = vid_enc_SetParameter;
169 comp->GetParameter = vid_enc_GetParameter;
170 comp->GetConfig = vid_enc_GetConfig;
171 comp->SetConfig = vid_enc_SetConfig;
172
173 priv->screen = omx_get_screen();
174 if (!priv->screen)
175 return OMX_ErrorInsufficientResources;
176
177 screen = priv->screen->pscreen;
178 if (!screen->get_video_param(screen, PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH,
179 PIPE_VIDEO_ENTRYPOINT_ENCODE, PIPE_VIDEO_CAP_SUPPORTED))
180 return OMX_ErrorBadParameter;
181
182 priv->s_pipe = screen->context_create(screen, NULL, 0);
183 if (!priv->s_pipe)
184 return OMX_ErrorInsufficientResources;
185
186 if (!vl_compositor_init(&priv->compositor, priv->s_pipe)) {
187 priv->s_pipe->destroy(priv->s_pipe);
188 priv->s_pipe = NULL;
189 return OMX_ErrorInsufficientResources;
190 }
191
192 if (!vl_compositor_init_state(&priv->cstate, priv->s_pipe)) {
193 vl_compositor_cleanup(&priv->compositor);
194 priv->s_pipe->destroy(priv->s_pipe);
195 priv->s_pipe = NULL;
196 return OMX_ErrorInsufficientResources;
197 }
198
199 priv->t_pipe = screen->context_create(screen, NULL, 0);
200 if (!priv->t_pipe)
201 return OMX_ErrorInsufficientResources;
202
203 priv->sPortTypesParam[OMX_PortDomainVideo].nStartPortNumber = 0;
204 priv->sPortTypesParam[OMX_PortDomainVideo].nPorts = 2;
205 priv->ports = CALLOC(2, sizeof(omx_base_PortType *));
206 if (!priv->ports)
207 return OMX_ErrorInsufficientResources;
208
209 for (i = 0; i < 2; ++i) {
210 priv->ports[i] = CALLOC(1, sizeof(omx_base_video_PortType));
211 if (!priv->ports[i])
212 return OMX_ErrorInsufficientResources;
213
214 base_video_port_Constructor(comp, &priv->ports[i], i, i == 0);
215 }
216
217 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
218 port->sPortParam.format.video.nFrameWidth = 176;
219 port->sPortParam.format.video.nFrameHeight = 144;
220 port->sPortParam.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
221 port->sVideoParam.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
222 port->sPortParam.nBufferCountActual = 8;
223 port->sPortParam.nBufferCountMin = 4;
224
225 port->Port_SendBufferFunction = vid_enc_EncodeFrame;
226 port->Port_AllocateBuffer = vid_enc_AllocateInBuffer;
227 port->Port_UseBuffer = vid_enc_UseInBuffer;
228 port->Port_FreeBuffer = vid_enc_FreeInBuffer;
229
230 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
231 strcpy(port->sPortParam.format.video.cMIMEType,"video/H264");
232 port->sPortParam.format.video.nFrameWidth = 176;
233 port->sPortParam.format.video.nFrameHeight = 144;
234 port->sPortParam.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
235 port->sVideoParam.eCompressionFormat = OMX_VIDEO_CodingAVC;
236
237 port->Port_AllocateBuffer = vid_enc_AllocateOutBuffer;
238 port->Port_FreeBuffer = vid_enc_FreeOutBuffer;
239
240 priv->bitrate.eControlRate = OMX_Video_ControlRateDisable;
241 priv->bitrate.nTargetBitrate = 0;
242
243 priv->quant.nQpI = OMX_VID_ENC_QUANT_I_FRAMES_DEFAULT;
244 priv->quant.nQpP = OMX_VID_ENC_QUANT_P_FRAMES_DEFAULT;
245 priv->quant.nQpB = OMX_VID_ENC_QUANT_B_FRAMES_DEFAULT;
246
247 priv->profile_level.eProfile = OMX_VIDEO_AVCProfileBaseline;
248 priv->profile_level.eLevel = OMX_VIDEO_AVCLevel51;
249
250 priv->force_pic_type.IntraRefreshVOP = OMX_FALSE;
251 priv->frame_num = 0;
252 priv->pic_order_cnt = 0;
253 priv->restricted_b_frames = debug_get_bool_option("OMX_USE_RESTRICTED_B_FRAMES", FALSE);
254
255 priv->scale.xWidth = OMX_VID_ENC_SCALING_WIDTH_DEFAULT;
256 priv->scale.xHeight = OMX_VID_ENC_SCALING_WIDTH_DEFAULT;
257
258 LIST_INITHEAD(&priv->free_tasks);
259 LIST_INITHEAD(&priv->used_tasks);
260 LIST_INITHEAD(&priv->b_frames);
261 LIST_INITHEAD(&priv->stacked_tasks);
262
263 return OMX_ErrorNone;
264 }
265
266 static OMX_ERRORTYPE vid_enc_Destructor(OMX_COMPONENTTYPE *comp)
267 {
268 vid_enc_PrivateType* priv = comp->pComponentPrivate;
269 int i;
270
271 enc_ReleaseTasks(&priv->free_tasks);
272 enc_ReleaseTasks(&priv->used_tasks);
273 enc_ReleaseTasks(&priv->b_frames);
274 enc_ReleaseTasks(&priv->stacked_tasks);
275
276 if (priv->ports) {
277 for (i = 0; i < priv->sPortTypesParam[OMX_PortDomainVideo].nPorts; ++i) {
278 if(priv->ports[i])
279 priv->ports[i]->PortDestructor(priv->ports[i]);
280 }
281 FREE(priv->ports);
282 priv->ports=NULL;
283 }
284
285 for (i = 0; i < OMX_VID_ENC_NUM_SCALING_BUFFERS; ++i)
286 if (priv->scale_buffer[i])
287 priv->scale_buffer[i]->destroy(priv->scale_buffer[i]);
288
289 if (priv->s_pipe) {
290 vl_compositor_cleanup_state(&priv->cstate);
291 vl_compositor_cleanup(&priv->compositor);
292 priv->s_pipe->destroy(priv->s_pipe);
293 }
294
295 if (priv->t_pipe)
296 priv->t_pipe->destroy(priv->t_pipe);
297
298 if (priv->screen)
299 omx_put_screen();
300
301 return omx_workaround_Destructor(comp);
302 }
303
304 static OMX_ERRORTYPE enc_AllocateBackTexture(omx_base_PortType *port,
305 struct pipe_resource **resource,
306 struct pipe_transfer **transfer,
307 OMX_U8 **map)
308 {
309 OMX_COMPONENTTYPE* comp = port->standCompContainer;
310 vid_enc_PrivateType *priv = comp->pComponentPrivate;
311 struct pipe_resource buf_templ;
312 struct pipe_box box = {};
313 OMX_U8 *ptr;
314
315 memset(&buf_templ, 0, sizeof buf_templ);
316 buf_templ.target = PIPE_TEXTURE_2D;
317 buf_templ.format = PIPE_FORMAT_I8_UNORM;
318 buf_templ.bind = PIPE_BIND_LINEAR;
319 buf_templ.usage = PIPE_USAGE_STAGING;
320 buf_templ.flags = 0;
321 buf_templ.width0 = port->sPortParam.format.video.nFrameWidth;
322 buf_templ.height0 = port->sPortParam.format.video.nFrameHeight * 3 / 2;
323 buf_templ.depth0 = 1;
324 buf_templ.array_size = 1;
325
326 *resource = priv->s_pipe->screen->resource_create(priv->s_pipe->screen, &buf_templ);
327 if (!*resource)
328 return OMX_ErrorInsufficientResources;
329
330 box.width = (*resource)->width0;
331 box.height = (*resource)->height0;
332 box.depth = (*resource)->depth0;
333 ptr = priv->s_pipe->transfer_map(priv->s_pipe, *resource, 0, PIPE_TRANSFER_WRITE, &box, transfer);
334 if (map)
335 *map = ptr;
336
337 return OMX_ErrorNone;
338 }
339
340 static OMX_ERRORTYPE vid_enc_SetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param)
341 {
342 OMX_COMPONENTTYPE *comp = handle;
343 vid_enc_PrivateType *priv = comp->pComponentPrivate;
344 OMX_ERRORTYPE r;
345
346 if (!param)
347 return OMX_ErrorBadParameter;
348
349 switch(idx) {
350 case OMX_IndexParamPortDefinition: {
351 OMX_PARAM_PORTDEFINITIONTYPE *def = param;
352
353 r = omx_base_component_SetParameter(handle, idx, param);
354 if (r)
355 return r;
356
357 if (def->nPortIndex == OMX_BASE_FILTER_INPUTPORT_INDEX) {
358 omx_base_video_PortType *port;
359 unsigned framesize;
360 struct pipe_resource *resource;
361 struct pipe_transfer *transfer;
362
363 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
364 enc_AllocateBackTexture(priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX],
365 &resource, &transfer, NULL);
366 port->sPortParam.format.video.nStride = transfer->stride;
367 pipe_transfer_unmap(priv->s_pipe, transfer);
368 pipe_resource_reference(&resource, NULL);
369
370 framesize = port->sPortParam.format.video.nStride *
371 port->sPortParam.format.video.nFrameHeight;
372 port->sPortParam.format.video.nSliceHeight = port->sPortParam.format.video.nFrameHeight;
373 port->sPortParam.nBufferSize = framesize * 3 / 2;
374
375 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
376 port->sPortParam.nBufferSize = framesize * 512 / (16*16);
377
378 priv->frame_rate = def->format.video.xFramerate;
379
380 priv->callbacks->EventHandler(comp, priv->callbackData, OMX_EventPortSettingsChanged,
381 OMX_BASE_FILTER_OUTPUTPORT_INDEX, 0, NULL);
382 }
383 break;
384 }
385 case OMX_IndexParamStandardComponentRole: {
386 OMX_PARAM_COMPONENTROLETYPE *role = param;
387
388 r = checkHeader(param, sizeof(OMX_PARAM_COMPONENTROLETYPE));
389 if (r)
390 return r;
391
392 if (strcmp((char *)role->cRole, OMX_VID_ENC_AVC_ROLE)) {
393 return OMX_ErrorBadParameter;
394 }
395
396 break;
397 }
398 case OMX_IndexParamVideoBitrate: {
399 OMX_VIDEO_PARAM_BITRATETYPE *bitrate = param;
400
401 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_BITRATETYPE));
402 if (r)
403 return r;
404
405 priv->bitrate = *bitrate;
406
407 break;
408 }
409 case OMX_IndexParamVideoQuantization: {
410 OMX_VIDEO_PARAM_QUANTIZATIONTYPE *quant = param;
411
412 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_QUANTIZATIONTYPE));
413 if (r)
414 return r;
415
416 priv->quant = *quant;
417
418 break;
419 }
420 case OMX_IndexParamVideoProfileLevelCurrent: {
421 OMX_VIDEO_PARAM_PROFILELEVELTYPE *profile_level = param;
422
423 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
424 if (r)
425 return r;
426
427 priv->profile_level = *profile_level;
428
429 break;
430 }
431 default:
432 return omx_base_component_SetParameter(handle, idx, param);
433 }
434 return OMX_ErrorNone;
435 }
436
437 static OMX_ERRORTYPE vid_enc_GetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param)
438 {
439 OMX_COMPONENTTYPE *comp = handle;
440 vid_enc_PrivateType *priv = comp->pComponentPrivate;
441 OMX_ERRORTYPE r;
442
443 if (!param)
444 return OMX_ErrorBadParameter;
445
446 switch(idx) {
447 case OMX_IndexParamStandardComponentRole: {
448 OMX_PARAM_COMPONENTROLETYPE *role = param;
449
450 r = checkHeader(param, sizeof(OMX_PARAM_COMPONENTROLETYPE));
451 if (r)
452 return r;
453
454 strcpy((char *)role->cRole, OMX_VID_ENC_AVC_ROLE);
455 break;
456 }
457 case OMX_IndexParamVideoInit:
458 r = checkHeader(param, sizeof(OMX_PORT_PARAM_TYPE));
459 if (r)
460 return r;
461
462 memcpy(param, &priv->sPortTypesParam[OMX_PortDomainVideo], sizeof(OMX_PORT_PARAM_TYPE));
463 break;
464
465 case OMX_IndexParamVideoPortFormat: {
466 OMX_VIDEO_PARAM_PORTFORMATTYPE *format = param;
467 omx_base_video_PortType *port;
468
469 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
470 if (r)
471 return r;
472
473 if (format->nPortIndex > 1)
474 return OMX_ErrorBadPortIndex;
475 if (format->nIndex >= 1)
476 return OMX_ErrorNoMore;
477
478 port = (omx_base_video_PortType *)priv->ports[format->nPortIndex];
479 memcpy(format, &port->sVideoParam, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
480 break;
481 }
482 case OMX_IndexParamVideoBitrate: {
483 OMX_VIDEO_PARAM_BITRATETYPE *bitrate = param;
484
485 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_BITRATETYPE));
486 if (r)
487 return r;
488
489 bitrate->eControlRate = priv->bitrate.eControlRate;
490 bitrate->nTargetBitrate = priv->bitrate.nTargetBitrate;
491
492 break;
493 }
494 case OMX_IndexParamVideoQuantization: {
495 OMX_VIDEO_PARAM_QUANTIZATIONTYPE *quant = param;
496
497 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_QUANTIZATIONTYPE));
498 if (r)
499 return r;
500
501 quant->nQpI = priv->quant.nQpI;
502 quant->nQpP = priv->quant.nQpP;
503 quant->nQpB = priv->quant.nQpB;
504
505 break;
506 }
507 case OMX_IndexParamVideoProfileLevelCurrent: {
508 OMX_VIDEO_PARAM_PROFILELEVELTYPE *profile_level = param;
509
510 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
511 if (r)
512 return r;
513
514 profile_level->eProfile = priv->profile_level.eProfile;
515 profile_level->eLevel = priv->profile_level.eLevel;
516
517 break;
518 }
519 default:
520 return omx_base_component_GetParameter(handle, idx, param);
521 }
522 return OMX_ErrorNone;
523 }
524
525 static OMX_ERRORTYPE vid_enc_SetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config)
526 {
527 OMX_COMPONENTTYPE *comp = handle;
528 vid_enc_PrivateType *priv = comp->pComponentPrivate;
529 OMX_ERRORTYPE r;
530 int i;
531
532 if (!config)
533 return OMX_ErrorBadParameter;
534
535 switch(idx) {
536 case OMX_IndexConfigVideoIntraVOPRefresh: {
537 OMX_CONFIG_INTRAREFRESHVOPTYPE *type = config;
538
539 r = checkHeader(config, sizeof(OMX_CONFIG_INTRAREFRESHVOPTYPE));
540 if (r)
541 return r;
542
543 priv->force_pic_type = *type;
544
545 break;
546 }
547 case OMX_IndexConfigCommonScale: {
548 OMX_CONFIG_SCALEFACTORTYPE *scale = config;
549
550 r = checkHeader(config, sizeof(OMX_CONFIG_SCALEFACTORTYPE));
551 if (r)
552 return r;
553
554 if (scale->xWidth < 176 || scale->xHeight < 144)
555 return OMX_ErrorBadParameter;
556
557 for (i = 0; i < OMX_VID_ENC_NUM_SCALING_BUFFERS; ++i) {
558 if (priv->scale_buffer[i]) {
559 priv->scale_buffer[i]->destroy(priv->scale_buffer[i]);
560 priv->scale_buffer[i] = NULL;
561 }
562 }
563
564 priv->scale = *scale;
565 if (priv->scale.xWidth != 0xffffffff && priv->scale.xHeight != 0xffffffff) {
566 struct pipe_video_buffer templat = {};
567
568 templat.buffer_format = PIPE_FORMAT_NV12;
569 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
570 templat.width = priv->scale.xWidth;
571 templat.height = priv->scale.xHeight;
572 templat.interlaced = false;
573 for (i = 0; i < OMX_VID_ENC_NUM_SCALING_BUFFERS; ++i) {
574 priv->scale_buffer[i] = priv->s_pipe->create_video_buffer(priv->s_pipe, &templat);
575 if (!priv->scale_buffer[i])
576 return OMX_ErrorInsufficientResources;
577 }
578 }
579
580 break;
581 }
582 default:
583 return omx_base_component_SetConfig(handle, idx, config);
584 }
585
586 return OMX_ErrorNone;
587 }
588
589 static OMX_ERRORTYPE vid_enc_GetConfig(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR config)
590 {
591 OMX_COMPONENTTYPE *comp = handle;
592 vid_enc_PrivateType *priv = comp->pComponentPrivate;
593 OMX_ERRORTYPE r;
594
595 if (!config)
596 return OMX_ErrorBadParameter;
597
598 switch(idx) {
599 case OMX_IndexConfigCommonScale: {
600 OMX_CONFIG_SCALEFACTORTYPE *scale = config;
601
602 r = checkHeader(config, sizeof(OMX_CONFIG_SCALEFACTORTYPE));
603 if (r)
604 return r;
605
606 scale->xWidth = priv->scale.xWidth;
607 scale->xHeight = priv->scale.xHeight;
608
609 break;
610 }
611 default:
612 return omx_base_component_GetConfig(handle, idx, config);
613 }
614
615 return OMX_ErrorNone;
616 }
617
618 static enum pipe_video_profile enc_TranslateOMXProfileToPipe(unsigned omx_profile)
619 {
620 switch (omx_profile) {
621 case OMX_VIDEO_AVCProfileBaseline:
622 return PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE;
623 case OMX_VIDEO_AVCProfileMain:
624 return PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN;
625 case OMX_VIDEO_AVCProfileExtended:
626 return PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED;
627 case OMX_VIDEO_AVCProfileHigh:
628 return PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH;
629 case OMX_VIDEO_AVCProfileHigh10:
630 return PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10;
631 case OMX_VIDEO_AVCProfileHigh422:
632 return PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422;
633 case OMX_VIDEO_AVCProfileHigh444:
634 return PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444;
635 default:
636 return PIPE_VIDEO_PROFILE_UNKNOWN;
637 }
638 }
639
640 static unsigned enc_TranslateOMXLevelToPipe(unsigned omx_level)
641 {
642 switch (omx_level) {
643 case OMX_VIDEO_AVCLevel1:
644 case OMX_VIDEO_AVCLevel1b:
645 return 10;
646 case OMX_VIDEO_AVCLevel11:
647 return 11;
648 case OMX_VIDEO_AVCLevel12:
649 return 12;
650 case OMX_VIDEO_AVCLevel13:
651 return 13;
652 case OMX_VIDEO_AVCLevel2:
653 return 20;
654 case OMX_VIDEO_AVCLevel21:
655 return 21;
656 case OMX_VIDEO_AVCLevel22:
657 return 22;
658 case OMX_VIDEO_AVCLevel3:
659 return 30;
660 case OMX_VIDEO_AVCLevel31:
661 return 31;
662 case OMX_VIDEO_AVCLevel32:
663 return 32;
664 case OMX_VIDEO_AVCLevel4:
665 return 40;
666 case OMX_VIDEO_AVCLevel41:
667 return 41;
668 default:
669 case OMX_VIDEO_AVCLevel42:
670 return 42;
671 case OMX_VIDEO_AVCLevel5:
672 return 50;
673 case OMX_VIDEO_AVCLevel51:
674 return 51;
675 }
676 }
677
678 static OMX_ERRORTYPE vid_enc_MessageHandler(OMX_COMPONENTTYPE* comp, internalRequestMessageType *msg)
679 {
680 vid_enc_PrivateType* priv = comp->pComponentPrivate;
681
682 if (msg->messageType == OMX_CommandStateSet) {
683 if ((msg->messageParam == OMX_StateIdle ) && (priv->state == OMX_StateLoaded)) {
684
685 struct pipe_video_codec templat = {};
686 omx_base_video_PortType *port;
687
688 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
689
690 templat.profile = enc_TranslateOMXProfileToPipe(priv->profile_level.eProfile);
691 templat.level = enc_TranslateOMXLevelToPipe(priv->profile_level.eLevel);
692 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_ENCODE;
693 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
694 templat.width = priv->scale_buffer[priv->current_scale_buffer] ?
695 priv->scale.xWidth : port->sPortParam.format.video.nFrameWidth;
696 templat.height = priv->scale_buffer[priv->current_scale_buffer] ?
697 priv->scale.xHeight : port->sPortParam.format.video.nFrameHeight;
698
699 if (templat.profile == PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE) {
700 struct pipe_screen *screen = priv->screen->pscreen;
701 templat.max_references = 1;
702 priv->stacked_frames_num =
703 screen->get_video_param(screen,
704 PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH,
705 PIPE_VIDEO_ENTRYPOINT_ENCODE,
706 PIPE_VIDEO_CAP_STACKED_FRAMES);
707 } else {
708 templat.max_references = OMX_VID_ENC_P_PERIOD_DEFAULT;
709 priv->stacked_frames_num = 1;
710 }
711 priv->codec = priv->s_pipe->create_video_codec(priv->s_pipe, &templat);
712
713 } else if ((msg->messageParam == OMX_StateLoaded) && (priv->state == OMX_StateIdle)) {
714 if (priv->codec) {
715 priv->codec->destroy(priv->codec);
716 priv->codec = NULL;
717 }
718 }
719 }
720
721 return omx_base_component_MessageHandler(comp, msg);
722 }
723
724 static OMX_ERRORTYPE vid_enc_AllocateInBuffer(omx_base_PortType *port, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
725 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size)
726 {
727 struct input_buf_private *inp;
728 OMX_ERRORTYPE r;
729
730 r = base_port_AllocateBuffer(port, buf, idx, private, size);
731 if (r)
732 return r;
733
734 inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
735 if (!inp) {
736 base_port_FreeBuffer(port, idx, *buf);
737 return OMX_ErrorInsufficientResources;
738 }
739
740 LIST_INITHEAD(&inp->tasks);
741
742 FREE((*buf)->pBuffer);
743 r = enc_AllocateBackTexture(port, &inp->resource, &inp->transfer, &(*buf)->pBuffer);
744 if (r) {
745 FREE(inp);
746 base_port_FreeBuffer(port, idx, *buf);
747 return r;
748 }
749
750 return OMX_ErrorNone;
751 }
752
753 static OMX_ERRORTYPE vid_enc_UseInBuffer(omx_base_PortType *port, OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx,
754 OMX_PTR private, OMX_U32 size, OMX_U8 *mem)
755 {
756 struct input_buf_private *inp;
757 OMX_ERRORTYPE r;
758
759 r = base_port_UseBuffer(port, buf, idx, private, size, mem);
760 if (r)
761 return r;
762
763 inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
764 if (!inp) {
765 base_port_FreeBuffer(port, idx, *buf);
766 return OMX_ErrorInsufficientResources;
767 }
768
769 LIST_INITHEAD(&inp->tasks);
770
771 return OMX_ErrorNone;
772 }
773
774 static OMX_ERRORTYPE vid_enc_FreeInBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf)
775 {
776 OMX_COMPONENTTYPE* comp = port->standCompContainer;
777 vid_enc_PrivateType *priv = comp->pComponentPrivate;
778 struct input_buf_private *inp = buf->pInputPortPrivate;
779
780 if (inp) {
781 enc_ReleaseTasks(&inp->tasks);
782 if (inp->transfer)
783 pipe_transfer_unmap(priv->s_pipe, inp->transfer);
784 pipe_resource_reference(&inp->resource, NULL);
785 FREE(inp);
786 }
787 buf->pBuffer = NULL;
788
789 return base_port_FreeBuffer(port, idx, buf);
790 }
791
792 static OMX_ERRORTYPE vid_enc_AllocateOutBuffer(omx_base_PortType *port, OMX_INOUT OMX_BUFFERHEADERTYPE **buf,
793 OMX_IN OMX_U32 idx, OMX_IN OMX_PTR private, OMX_IN OMX_U32 size)
794 {
795 OMX_ERRORTYPE r;
796
797 r = base_port_AllocateBuffer(port, buf, idx, private, size);
798 if (r)
799 return r;
800
801 FREE((*buf)->pBuffer);
802 (*buf)->pBuffer = NULL;
803 (*buf)->pOutputPortPrivate = CALLOC(1, sizeof(struct output_buf_private));
804 if (!(*buf)->pOutputPortPrivate) {
805 base_port_FreeBuffer(port, idx, *buf);
806 return OMX_ErrorInsufficientResources;
807 }
808
809 return OMX_ErrorNone;
810 }
811
812 static OMX_ERRORTYPE vid_enc_FreeOutBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf)
813 {
814 OMX_COMPONENTTYPE* comp = port->standCompContainer;
815 vid_enc_PrivateType *priv = comp->pComponentPrivate;
816
817 if (buf->pOutputPortPrivate) {
818 struct output_buf_private *outp = buf->pOutputPortPrivate;
819 if (outp->transfer)
820 pipe_transfer_unmap(priv->t_pipe, outp->transfer);
821 pipe_resource_reference(&outp->bitstream, NULL);
822 FREE(outp);
823 buf->pOutputPortPrivate = NULL;
824 }
825 buf->pBuffer = NULL;
826
827 return base_port_FreeBuffer(port, idx, buf);
828 }
829
830 static struct encode_task *enc_NeedTask(omx_base_PortType *port)
831 {
832 OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
833 OMX_COMPONENTTYPE* comp = port->standCompContainer;
834 vid_enc_PrivateType *priv = comp->pComponentPrivate;
835
836 struct pipe_video_buffer templat = {};
837 struct encode_task *task;
838
839 if (!LIST_IS_EMPTY(&priv->free_tasks)) {
840 task = LIST_ENTRY(struct encode_task, priv->free_tasks.next, list);
841 LIST_DEL(&task->list);
842 return task;
843 }
844
845 /* allocate a new one */
846 task = CALLOC_STRUCT(encode_task);
847 if (!task)
848 return NULL;
849
850 templat.buffer_format = PIPE_FORMAT_NV12;
851 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
852 templat.width = def->nFrameWidth;
853 templat.height = def->nFrameHeight;
854 templat.interlaced = false;
855
856 task->buf = priv->s_pipe->create_video_buffer(priv->s_pipe, &templat);
857 if (!task->buf) {
858 FREE(task);
859 return NULL;
860 }
861
862 return task;
863 }
864
865 static void enc_MoveTasks(struct list_head *from, struct list_head *to)
866 {
867 to->prev->next = from->next;
868 from->next->prev = to->prev;
869 from->prev->next = to;
870 to->prev = from->prev;
871 LIST_INITHEAD(from);
872 }
873
874 static void enc_ReleaseTasks(struct list_head *head)
875 {
876 struct encode_task *i, *next;
877
878 if (!head || !head->next)
879 return;
880
881 LIST_FOR_EACH_ENTRY_SAFE(i, next, head, list) {
882 pipe_resource_reference(&i->bitstream, NULL);
883 i->buf->destroy(i->buf);
884 FREE(i);
885 }
886 }
887
888 static OMX_ERRORTYPE enc_LoadImage(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf,
889 struct pipe_video_buffer *vbuf)
890 {
891 OMX_COMPONENTTYPE* comp = port->standCompContainer;
892 vid_enc_PrivateType *priv = comp->pComponentPrivate;
893 OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
894 struct pipe_box box = {};
895 struct input_buf_private *inp = buf->pInputPortPrivate;
896
897 if (!inp->resource) {
898 struct pipe_sampler_view **views;
899 void *ptr;
900
901 views = vbuf->get_sampler_view_planes(vbuf);
902 if (!views)
903 return OMX_ErrorInsufficientResources;
904
905 ptr = buf->pBuffer;
906 box.width = def->nFrameWidth;
907 box.height = def->nFrameHeight;
908 box.depth = 1;
909 priv->s_pipe->texture_subdata(priv->s_pipe, views[0]->texture, 0,
910 PIPE_TRANSFER_WRITE, &box,
911 ptr, def->nStride, 0);
912 ptr = ((uint8_t*)buf->pBuffer) + (def->nStride * box.height);
913 box.width = def->nFrameWidth / 2;
914 box.height = def->nFrameHeight / 2;
915 box.depth = 1;
916 priv->s_pipe->texture_subdata(priv->s_pipe, views[1]->texture, 0,
917 PIPE_TRANSFER_WRITE, &box,
918 ptr, def->nStride, 0);
919 } else {
920 struct pipe_blit_info blit;
921 struct vl_video_buffer *dst_buf = (struct vl_video_buffer *)vbuf;
922
923 pipe_transfer_unmap(priv->s_pipe, inp->transfer);
924
925 box.width = def->nFrameWidth;
926 box.height = def->nFrameHeight;
927 box.depth = 1;
928
929 priv->s_pipe->resource_copy_region(priv->s_pipe,
930 dst_buf->resources[0],
931 0, 0, 0, 0, inp->resource, 0, &box);
932
933 memset(&blit, 0, sizeof(blit));
934 blit.src.resource = inp->resource;
935 blit.src.format = inp->resource->format;
936
937 blit.src.box.x = 0;
938 blit.src.box.y = def->nFrameHeight;
939 blit.src.box.width = def->nFrameWidth;
940 blit.src.box.height = def->nFrameHeight / 2 ;
941 blit.src.box.depth = 1;
942
943 blit.dst.resource = dst_buf->resources[1];
944 blit.dst.format = blit.dst.resource->format;
945
946 blit.dst.box.width = def->nFrameWidth / 2;
947 blit.dst.box.height = def->nFrameHeight / 2;
948 blit.dst.box.depth = 1;
949 blit.filter = PIPE_TEX_FILTER_NEAREST;
950
951 blit.mask = PIPE_MASK_G;
952 priv->s_pipe->blit(priv->s_pipe, &blit);
953
954 blit.src.box.x = 1;
955 blit.mask = PIPE_MASK_R;
956 priv->s_pipe->blit(priv->s_pipe, &blit);
957 priv->s_pipe->flush(priv->s_pipe, NULL, 0);
958
959 box.width = inp->resource->width0;
960 box.height = inp->resource->height0;
961 box.depth = inp->resource->depth0;
962 buf->pBuffer = priv->s_pipe->transfer_map(priv->s_pipe, inp->resource, 0,
963 PIPE_TRANSFER_WRITE, &box,
964 &inp->transfer);
965 }
966
967 return OMX_ErrorNone;
968 }
969
970 static void enc_ScaleInput(omx_base_PortType *port, struct pipe_video_buffer **vbuf, unsigned *size)
971 {
972 OMX_COMPONENTTYPE* comp = port->standCompContainer;
973 vid_enc_PrivateType *priv = comp->pComponentPrivate;
974 OMX_VIDEO_PORTDEFINITIONTYPE *def = &port->sPortParam.format.video;
975 struct pipe_video_buffer *src_buf = *vbuf;
976 struct vl_compositor *compositor = &priv->compositor;
977 struct vl_compositor_state *s = &priv->cstate;
978 struct pipe_sampler_view **views;
979 struct pipe_surface **dst_surface;
980 unsigned i;
981
982 if (!priv->scale_buffer[priv->current_scale_buffer])
983 return;
984
985 views = src_buf->get_sampler_view_planes(src_buf);
986 dst_surface = priv->scale_buffer[priv->current_scale_buffer]->get_surfaces
987 (priv->scale_buffer[priv->current_scale_buffer]);
988 vl_compositor_clear_layers(s);
989
990 for (i = 0; i < VL_MAX_SURFACES; ++i) {
991 struct u_rect src_rect;
992 if (!views[i] || !dst_surface[i])
993 continue;
994 src_rect.x0 = 0;
995 src_rect.y0 = 0;
996 src_rect.x1 = def->nFrameWidth;
997 src_rect.y1 = def->nFrameHeight;
998 if (i > 0) {
999 src_rect.x1 /= 2;
1000 src_rect.y1 /= 2;
1001 }
1002 vl_compositor_set_rgba_layer(s, compositor, 0, views[i], &src_rect, NULL, NULL);
1003 vl_compositor_render(s, compositor, dst_surface[i], NULL, false);
1004 }
1005 *size = priv->scale.xWidth * priv->scale.xHeight * 2;
1006 *vbuf = priv->scale_buffer[priv->current_scale_buffer++];
1007 priv->current_scale_buffer %= OMX_VID_ENC_NUM_SCALING_BUFFERS;
1008 }
1009
1010 static void enc_GetPictureParamPreset(struct pipe_h264_enc_picture_desc *picture)
1011 {
1012 picture->motion_est.enc_disable_sub_mode = 0x000000fe;
1013 picture->motion_est.enc_ime2_search_range_x = 0x00000001;
1014 picture->motion_est.enc_ime2_search_range_y = 0x00000001;
1015 picture->pic_ctrl.enc_constraint_set_flags = 0x00000040;
1016 }
1017
1018 static void enc_ControlPicture(omx_base_PortType *port, struct pipe_h264_enc_picture_desc *picture)
1019 {
1020 OMX_COMPONENTTYPE* comp = port->standCompContainer;
1021 vid_enc_PrivateType *priv = comp->pComponentPrivate;
1022 struct pipe_h264_enc_rate_control *rate_ctrl = &picture->rate_ctrl;
1023
1024 switch (priv->bitrate.eControlRate) {
1025 case OMX_Video_ControlRateVariable:
1026 rate_ctrl->rate_ctrl_method = PIPE_H264_ENC_RATE_CONTROL_METHOD_VARIABLE;
1027 break;
1028 case OMX_Video_ControlRateConstant:
1029 rate_ctrl->rate_ctrl_method = PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT;
1030 break;
1031 case OMX_Video_ControlRateVariableSkipFrames:
1032 rate_ctrl->rate_ctrl_method = PIPE_H264_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP;
1033 break;
1034 case OMX_Video_ControlRateConstantSkipFrames:
1035 rate_ctrl->rate_ctrl_method = PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP;
1036 break;
1037 default:
1038 rate_ctrl->rate_ctrl_method = PIPE_H264_ENC_RATE_CONTROL_METHOD_DISABLE;
1039 break;
1040 }
1041
1042 rate_ctrl->frame_rate_den = OMX_VID_ENC_CONTROL_FRAME_RATE_DEN_DEFAULT;
1043 rate_ctrl->frame_rate_num = ((priv->frame_rate) >> 16) * rate_ctrl->frame_rate_den;
1044
1045 if (rate_ctrl->rate_ctrl_method != PIPE_H264_ENC_RATE_CONTROL_METHOD_DISABLE) {
1046 if (priv->bitrate.nTargetBitrate < OMX_VID_ENC_BITRATE_MIN)
1047 rate_ctrl->target_bitrate = OMX_VID_ENC_BITRATE_MIN;
1048 else if (priv->bitrate.nTargetBitrate < OMX_VID_ENC_BITRATE_MAX)
1049 rate_ctrl->target_bitrate = priv->bitrate.nTargetBitrate;
1050 else
1051 rate_ctrl->target_bitrate = OMX_VID_ENC_BITRATE_MAX;
1052 rate_ctrl->peak_bitrate = rate_ctrl->target_bitrate;
1053 if (rate_ctrl->target_bitrate < OMX_VID_ENC_BITRATE_MEDIAN)
1054 rate_ctrl->vbv_buffer_size = MIN2((rate_ctrl->target_bitrate * 2.75), OMX_VID_ENC_BITRATE_MEDIAN);
1055 else
1056 rate_ctrl->vbv_buffer_size = rate_ctrl->target_bitrate;
1057
1058 if (rate_ctrl->frame_rate_num) {
1059 unsigned long long t = rate_ctrl->target_bitrate;
1060 t *= rate_ctrl->frame_rate_den;
1061 rate_ctrl->target_bits_picture = t / rate_ctrl->frame_rate_num;
1062 } else {
1063 rate_ctrl->target_bits_picture = rate_ctrl->target_bitrate;
1064 }
1065 rate_ctrl->peak_bits_picture_integer = rate_ctrl->target_bits_picture;
1066 rate_ctrl->peak_bits_picture_fraction = 0;
1067 }
1068
1069 picture->quant_i_frames = priv->quant.nQpI;
1070 picture->quant_p_frames = priv->quant.nQpP;
1071 picture->quant_b_frames = priv->quant.nQpB;
1072
1073 picture->frame_num = priv->frame_num;
1074 picture->ref_idx_l0 = priv->ref_idx_l0;
1075 picture->ref_idx_l1 = priv->ref_idx_l1;
1076 picture->enable_vui = (picture->rate_ctrl.frame_rate_num != 0);
1077 enc_GetPictureParamPreset(picture);
1078 }
1079
1080 static void enc_HandleTask(omx_base_PortType *port, struct encode_task *task,
1081 enum pipe_h264_enc_picture_type picture_type)
1082 {
1083 OMX_COMPONENTTYPE* comp = port->standCompContainer;
1084 vid_enc_PrivateType *priv = comp->pComponentPrivate;
1085 unsigned size = priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX]->sPortParam.nBufferSize;
1086 struct pipe_video_buffer *vbuf = task->buf;
1087 struct pipe_h264_enc_picture_desc picture = {};
1088
1089 /* -------------- scale input image --------- */
1090 enc_ScaleInput(port, &vbuf, &size);
1091 priv->s_pipe->flush(priv->s_pipe, NULL, 0);
1092
1093 /* -------------- allocate output buffer --------- */
1094 task->bitstream = pipe_buffer_create(priv->s_pipe->screen,
1095 PIPE_BIND_VERTEX_BUFFER,
1096 PIPE_USAGE_STAGING, /* map for read */
1097 size);
1098
1099 picture.picture_type = picture_type;
1100 picture.pic_order_cnt = task->pic_order_cnt;
1101 picture.base.profile = enc_TranslateOMXProfileToPipe(priv->profile_level.eProfile);
1102 picture.base.entry_point = PIPE_VIDEO_ENTRYPOINT_ENCODE;
1103 if (priv->restricted_b_frames && picture_type == PIPE_H264_ENC_PICTURE_TYPE_B)
1104 picture.not_referenced = true;
1105 enc_ControlPicture(port, &picture);
1106
1107 /* -------------- encode frame --------- */
1108 priv->codec->begin_frame(priv->codec, vbuf, &picture.base);
1109 priv->codec->encode_bitstream(priv->codec, vbuf, task->bitstream, &task->feedback);
1110 priv->codec->end_frame(priv->codec, vbuf, &picture.base);
1111 }
1112
1113 static void enc_ClearBframes(omx_base_PortType *port, struct input_buf_private *inp)
1114 {
1115 OMX_COMPONENTTYPE* comp = port->standCompContainer;
1116 vid_enc_PrivateType *priv = comp->pComponentPrivate;
1117 struct encode_task *task;
1118
1119 if (LIST_IS_EMPTY(&priv->b_frames))
1120 return;
1121
1122 task = LIST_ENTRY(struct encode_task, priv->b_frames.prev, list);
1123 LIST_DEL(&task->list);
1124
1125 /* promote last from to P frame */
1126 priv->ref_idx_l0 = priv->ref_idx_l1;
1127 enc_HandleTask(port, task, PIPE_H264_ENC_PICTURE_TYPE_P);
1128 LIST_ADDTAIL(&task->list, &inp->tasks);
1129 priv->ref_idx_l1 = priv->frame_num++;
1130
1131 /* handle B frames */
1132 LIST_FOR_EACH_ENTRY(task, &priv->b_frames, list) {
1133 enc_HandleTask(port, task, PIPE_H264_ENC_PICTURE_TYPE_B);
1134 if (!priv->restricted_b_frames)
1135 priv->ref_idx_l0 = priv->frame_num;
1136 priv->frame_num++;
1137 }
1138
1139 enc_MoveTasks(&priv->b_frames, &inp->tasks);
1140 }
1141
1142 static OMX_ERRORTYPE vid_enc_EncodeFrame(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf)
1143 {
1144 OMX_COMPONENTTYPE* comp = port->standCompContainer;
1145 vid_enc_PrivateType *priv = comp->pComponentPrivate;
1146 struct input_buf_private *inp = buf->pInputPortPrivate;
1147 enum pipe_h264_enc_picture_type picture_type;
1148 struct encode_task *task;
1149 unsigned stacked_num = 0;
1150 OMX_ERRORTYPE err;
1151
1152 enc_MoveTasks(&inp->tasks, &priv->free_tasks);
1153 task = enc_NeedTask(port);
1154 if (!task)
1155 return OMX_ErrorInsufficientResources;
1156
1157 if (buf->nFilledLen == 0) {
1158 if (buf->nFlags & OMX_BUFFERFLAG_EOS) {
1159 buf->nFilledLen = buf->nAllocLen;
1160 enc_ClearBframes(port, inp);
1161 enc_MoveTasks(&priv->stacked_tasks, &inp->tasks);
1162 priv->codec->flush(priv->codec);
1163 }
1164 return base_port_SendBufferFunction(port, buf);
1165 }
1166
1167 if (buf->pOutputPortPrivate) {
1168 struct pipe_video_buffer *vbuf = buf->pOutputPortPrivate;
1169 buf->pOutputPortPrivate = task->buf;
1170 task->buf = vbuf;
1171 } else {
1172 /* ------- load input image into video buffer ---- */
1173 err = enc_LoadImage(port, buf, task->buf);
1174 if (err != OMX_ErrorNone) {
1175 FREE(task);
1176 return err;
1177 }
1178 }
1179
1180 /* -------------- determine picture type --------- */
1181 if (!(priv->pic_order_cnt % OMX_VID_ENC_IDR_PERIOD_DEFAULT) ||
1182 priv->force_pic_type.IntraRefreshVOP) {
1183 enc_ClearBframes(port, inp);
1184 picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
1185 priv->force_pic_type.IntraRefreshVOP = OMX_FALSE;
1186 priv->frame_num = 0;
1187 } else if (priv->codec->profile == PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE ||
1188 !(priv->pic_order_cnt % OMX_VID_ENC_P_PERIOD_DEFAULT) ||
1189 (buf->nFlags & OMX_BUFFERFLAG_EOS)) {
1190 picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
1191 } else {
1192 picture_type = PIPE_H264_ENC_PICTURE_TYPE_B;
1193 }
1194
1195 task->pic_order_cnt = priv->pic_order_cnt++;
1196
1197 if (picture_type == PIPE_H264_ENC_PICTURE_TYPE_B) {
1198 /* put frame at the tail of the queue */
1199 LIST_ADDTAIL(&task->list, &priv->b_frames);
1200 } else {
1201 /* handle I or P frame */
1202 priv->ref_idx_l0 = priv->ref_idx_l1;
1203 enc_HandleTask(port, task, picture_type);
1204 LIST_ADDTAIL(&task->list, &priv->stacked_tasks);
1205 LIST_FOR_EACH_ENTRY(task, &priv->stacked_tasks, list) {
1206 ++stacked_num;
1207 }
1208 if (stacked_num == priv->stacked_frames_num) {
1209 struct encode_task *t;
1210 t = LIST_ENTRY(struct encode_task, priv->stacked_tasks.next, list);
1211 LIST_DEL(&t->list);
1212 LIST_ADDTAIL(&t->list, &inp->tasks);
1213 }
1214 priv->ref_idx_l1 = priv->frame_num++;
1215
1216 /* handle B frames */
1217 LIST_FOR_EACH_ENTRY(task, &priv->b_frames, list) {
1218 enc_HandleTask(port, task, PIPE_H264_ENC_PICTURE_TYPE_B);
1219 if (!priv->restricted_b_frames)
1220 priv->ref_idx_l0 = priv->frame_num;
1221 priv->frame_num++;
1222 }
1223
1224 enc_MoveTasks(&priv->b_frames, &inp->tasks);
1225 }
1226
1227 if (LIST_IS_EMPTY(&inp->tasks))
1228 return port->ReturnBufferFunction(port, buf);
1229 else
1230 return base_port_SendBufferFunction(port, buf);
1231 }
1232
1233 static void vid_enc_BufferEncoded(OMX_COMPONENTTYPE *comp, OMX_BUFFERHEADERTYPE* input, OMX_BUFFERHEADERTYPE* output)
1234 {
1235 vid_enc_PrivateType *priv = comp->pComponentPrivate;
1236 struct output_buf_private *outp = output->pOutputPortPrivate;
1237 struct input_buf_private *inp = input->pInputPortPrivate;
1238 struct encode_task *task;
1239 struct pipe_box box = {};
1240 unsigned size;
1241
1242 if (!inp || LIST_IS_EMPTY(&inp->tasks)) {
1243 input->nFilledLen = 0; /* mark buffer as empty */
1244 enc_MoveTasks(&priv->used_tasks, &inp->tasks);
1245 return;
1246 }
1247
1248 task = LIST_ENTRY(struct encode_task, inp->tasks.next, list);
1249 LIST_DEL(&task->list);
1250 LIST_ADDTAIL(&task->list, &priv->used_tasks);
1251
1252 if (!task->bitstream)
1253 return;
1254
1255 /* ------------- map result buffer ----------------- */
1256
1257 if (outp->transfer)
1258 pipe_transfer_unmap(priv->t_pipe, outp->transfer);
1259
1260 pipe_resource_reference(&outp->bitstream, task->bitstream);
1261 pipe_resource_reference(&task->bitstream, NULL);
1262
1263 box.width = outp->bitstream->width0;
1264 box.height = outp->bitstream->height0;
1265 box.depth = outp->bitstream->depth0;
1266
1267 output->pBuffer = priv->t_pipe->transfer_map(priv->t_pipe, outp->bitstream, 0,
1268 PIPE_TRANSFER_READ_WRITE,
1269 &box, &outp->transfer);
1270
1271 /* ------------- get size of result ----------------- */
1272
1273 priv->codec->get_feedback(priv->codec, task->feedback, &size);
1274
1275 output->nOffset = 0;
1276 output->nFilledLen = size; /* mark buffer as full */
1277
1278 /* all output buffers contain exactly one frame */
1279 output->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1280 }