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