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 "pipe/p_screen.h"
48 #include "pipe/p_video_codec.h"
49 #include "util/u_memory.h"
50 #include "util/u_surface.h"
51 #include "vl/vl_video_buffer.h"
52 #include "vl/vl_vlc.h"
53
54 #include "entrypoint.h"
55 #include "vid_dec.h"
56 #include "vid_omx_common.h"
57 #include "vid_dec_h264_common.h"
58
59 static OMX_ERRORTYPE vid_dec_Constructor(OMX_COMPONENTTYPE *comp, OMX_STRING name);
60 static OMX_ERRORTYPE vid_dec_Destructor(OMX_COMPONENTTYPE *comp);
61 static OMX_ERRORTYPE vid_dec_SetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param);
62 static OMX_ERRORTYPE vid_dec_GetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param);
63 static OMX_ERRORTYPE vid_dec_MessageHandler(OMX_COMPONENTTYPE *comp, internalRequestMessageType *msg);
64 static OMX_ERRORTYPE vid_dec_DecodeBuffer(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf);
65 static OMX_ERRORTYPE vid_dec_FreeDecBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf);
66 static void vid_dec_FrameDecoded(OMX_COMPONENTTYPE *comp, OMX_BUFFERHEADERTYPE* input, OMX_BUFFERHEADERTYPE* output);
67
vid_dec_LoaderComponent(stLoaderComponentType * comp)68 OMX_ERRORTYPE vid_dec_LoaderComponent(stLoaderComponentType *comp)
69 {
70 comp->componentVersion.s.nVersionMajor = 0;
71 comp->componentVersion.s.nVersionMinor = 0;
72 comp->componentVersion.s.nRevision = 0;
73 comp->componentVersion.s.nStep = 1;
74 comp->name_specific_length = 3;
75
76 comp->name = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
77 if (comp->name == NULL)
78 goto error;
79
80 comp->name_specific = CALLOC(comp->name_specific_length, sizeof(char *));
81 if (comp->name_specific == NULL)
82 goto error;
83
84 comp->role_specific = CALLOC(comp->name_specific_length, sizeof(char *));
85 if (comp->role_specific == NULL)
86 goto error;
87
88 comp->name_specific[0] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
89 if (comp->name_specific[0] == NULL)
90 goto error_specific;
91
92 comp->name_specific[1] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
93 if (comp->name_specific[1] == NULL)
94 goto error_specific;
95
96 comp->name_specific[2] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
97 if (comp->name_specific[2] == NULL)
98 goto error_specific;
99
100 comp->role_specific[0] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
101 if (comp->role_specific[0] == NULL)
102 goto error_specific;
103
104 comp->role_specific[1] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
105 if (comp->role_specific[1] == NULL)
106 goto error_specific;
107
108 comp->role_specific[2] = CALLOC(1, OMX_MAX_STRINGNAME_SIZE);
109 if (comp->role_specific[2] == NULL)
110 goto error_specific;
111
112 strcpy(comp->name, OMX_VID_DEC_BASE_NAME);
113 strcpy(comp->name_specific[0], OMX_VID_DEC_MPEG2_NAME);
114 strcpy(comp->name_specific[1], OMX_VID_DEC_AVC_NAME);
115 strcpy(comp->name_specific[2], OMX_VID_DEC_HEVC_NAME);
116
117 strcpy(comp->role_specific[0], OMX_VID_DEC_MPEG2_ROLE);
118 strcpy(comp->role_specific[1], OMX_VID_DEC_AVC_ROLE);
119 strcpy(comp->role_specific[2], OMX_VID_DEC_HEVC_ROLE);
120
121 comp->constructor = vid_dec_Constructor;
122
123 return OMX_ErrorNone;
124
125 error_specific:
126 FREE(comp->role_specific[2]);
127 FREE(comp->role_specific[1]);
128 FREE(comp->role_specific[0]);
129 FREE(comp->name_specific[2]);
130 FREE(comp->name_specific[1]);
131 FREE(comp->name_specific[0]);
132
133 error:
134 FREE(comp->role_specific);
135 FREE(comp->name_specific);
136
137 FREE(comp->name);
138
139 return OMX_ErrorInsufficientResources;
140 }
141
vid_dec_Constructor(OMX_COMPONENTTYPE * comp,OMX_STRING name)142 static OMX_ERRORTYPE vid_dec_Constructor(OMX_COMPONENTTYPE *comp, OMX_STRING name)
143 {
144 vid_dec_PrivateType *priv;
145 omx_base_video_PortType *port;
146 struct pipe_screen *screen;
147 OMX_ERRORTYPE r;
148 int i;
149
150 assert(!comp->pComponentPrivate);
151
152 priv = comp->pComponentPrivate = CALLOC(1, sizeof(vid_dec_PrivateType));
153 if (!priv)
154 return OMX_ErrorInsufficientResources;
155
156 r = omx_base_filter_Constructor(comp, name);
157 if (r)
158 return r;
159
160 priv->profile = PIPE_VIDEO_PROFILE_UNKNOWN;
161
162 if (!strcmp(name, OMX_VID_DEC_MPEG2_NAME))
163 priv->profile = PIPE_VIDEO_PROFILE_MPEG2_MAIN;
164
165 if (!strcmp(name, OMX_VID_DEC_AVC_NAME))
166 priv->profile = PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH;
167
168 if (!strcmp(name, OMX_VID_DEC_HEVC_NAME))
169 priv->profile = PIPE_VIDEO_PROFILE_HEVC_MAIN;
170
171 priv->BufferMgmtCallback = vid_dec_FrameDecoded;
172 priv->messageHandler = vid_dec_MessageHandler;
173 priv->destructor = vid_dec_Destructor;
174
175 comp->SetParameter = vid_dec_SetParameter;
176 comp->GetParameter = vid_dec_GetParameter;
177
178 priv->screen = omx_get_screen();
179 if (!priv->screen)
180 return OMX_ErrorInsufficientResources;
181
182 screen = priv->screen->pscreen;
183 priv->pipe = pipe_create_multimedia_context(screen);
184 if (!priv->pipe)
185 return OMX_ErrorInsufficientResources;
186
187 if (!vl_compositor_init(&priv->compositor, priv->pipe)) {
188 priv->pipe->destroy(priv->pipe);
189 priv->pipe = NULL;
190 return OMX_ErrorInsufficientResources;
191 }
192
193 if (!vl_compositor_init_state(&priv->cstate, priv->pipe)) {
194 vl_compositor_cleanup(&priv->compositor);
195 priv->pipe->destroy(priv->pipe);
196 priv->pipe = NULL;
197 return OMX_ErrorInsufficientResources;
198 }
199
200 priv->sPortTypesParam[OMX_PortDomainVideo].nStartPortNumber = 0;
201 priv->sPortTypesParam[OMX_PortDomainVideo].nPorts = 2;
202 priv->ports = CALLOC(2, sizeof(omx_base_PortType *));
203 if (!priv->ports)
204 return OMX_ErrorInsufficientResources;
205
206 for (i = 0; i < 2; ++i) {
207 priv->ports[i] = CALLOC(1, sizeof(omx_base_video_PortType));
208 if (!priv->ports[i])
209 return OMX_ErrorInsufficientResources;
210
211 base_video_port_Constructor(comp, &priv->ports[i], i, i == 0);
212 }
213
214 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
215 strcpy(port->sPortParam.format.video.cMIMEType,"video/MPEG2");
216 port->sPortParam.nBufferCountMin = 8;
217 port->sPortParam.nBufferCountActual = 8;
218 port->sPortParam.nBufferSize = DEFAULT_OUT_BUFFER_SIZE;
219 port->sPortParam.format.video.nFrameWidth = 176;
220 port->sPortParam.format.video.nFrameHeight = 144;
221 port->sPortParam.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG2;
222 port->sVideoParam.eCompressionFormat = OMX_VIDEO_CodingMPEG2;
223 port->Port_SendBufferFunction = vid_dec_DecodeBuffer;
224 port->Port_FreeBuffer = vid_dec_FreeDecBuffer;
225
226 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
227 port->sPortParam.nBufferCountActual = 8;
228 port->sPortParam.nBufferCountMin = 4;
229 port->sPortParam.format.video.nFrameWidth = 176;
230 port->sPortParam.format.video.nFrameHeight = 144;
231 port->sPortParam.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
232 port->sVideoParam.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
233
234 return OMX_ErrorNone;
235 }
236
vid_dec_Destructor(OMX_COMPONENTTYPE * comp)237 static OMX_ERRORTYPE vid_dec_Destructor(OMX_COMPONENTTYPE *comp)
238 {
239 vid_dec_PrivateType* priv = comp->pComponentPrivate;
240 int i;
241
242 if (priv->ports) {
243 for (i = 0; i < priv->sPortTypesParam[OMX_PortDomainVideo].nPorts; ++i) {
244 if(priv->ports[i])
245 priv->ports[i]->PortDestructor(priv->ports[i]);
246 }
247 FREE(priv->ports);
248 priv->ports=NULL;
249 }
250
251 if (priv->pipe) {
252 vl_compositor_cleanup_state(&priv->cstate);
253 vl_compositor_cleanup(&priv->compositor);
254 priv->pipe->destroy(priv->pipe);
255 }
256
257 if (priv->screen)
258 omx_put_screen();
259
260 return omx_workaround_Destructor(comp);
261 }
262
vid_dec_SetParameter(OMX_HANDLETYPE handle,OMX_INDEXTYPE idx,OMX_PTR param)263 static OMX_ERRORTYPE vid_dec_SetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param)
264 {
265 OMX_COMPONENTTYPE *comp = handle;
266 vid_dec_PrivateType *priv = comp->pComponentPrivate;
267 OMX_ERRORTYPE r;
268
269 if (!param)
270 return OMX_ErrorBadParameter;
271
272 switch(idx) {
273 case OMX_IndexParamPortDefinition: {
274 OMX_PARAM_PORTDEFINITIONTYPE *def = param;
275
276 r = omx_base_component_SetParameter(handle, idx, param);
277 if (r)
278 return r;
279
280 if (def->nPortIndex == OMX_BASE_FILTER_INPUTPORT_INDEX) {
281 omx_base_video_PortType *port;
282 unsigned framesize = def->format.video.nFrameWidth * def->format.video.nFrameHeight;
283
284 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
285 port->sPortParam.nBufferSize = framesize * 512 / (16*16);
286
287 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX];
288 port->sPortParam.format.video.nFrameWidth = def->format.video.nFrameWidth;
289 port->sPortParam.format.video.nFrameHeight = def->format.video.nFrameHeight;
290 port->sPortParam.format.video.nStride = def->format.video.nFrameWidth;
291 port->sPortParam.format.video.nSliceHeight = def->format.video.nFrameHeight;
292 port->sPortParam.nBufferSize = framesize*3/2;
293
294 priv->callbacks->EventHandler(comp, priv->callbackData, OMX_EventPortSettingsChanged,
295 OMX_BASE_FILTER_OUTPUTPORT_INDEX, 0, NULL);
296 }
297 break;
298 }
299 case OMX_IndexParamStandardComponentRole: {
300 OMX_PARAM_COMPONENTROLETYPE *role = param;
301
302 r = checkHeader(param, sizeof(OMX_PARAM_COMPONENTROLETYPE));
303 if (r)
304 return r;
305
306 if (!strcmp((char *)role->cRole, OMX_VID_DEC_MPEG2_ROLE)) {
307 priv->profile = PIPE_VIDEO_PROFILE_MPEG2_MAIN;
308 } else if (!strcmp((char *)role->cRole, OMX_VID_DEC_AVC_ROLE)) {
309 priv->profile = PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH;
310 } else if (!strcmp((char *)role->cRole, OMX_VID_DEC_HEVC_ROLE)) {
311 priv->profile = PIPE_VIDEO_PROFILE_HEVC_MAIN;
312 } else {
313 return OMX_ErrorBadParameter;
314 }
315
316 break;
317 }
318 case OMX_IndexParamVideoPortFormat: {
319 OMX_VIDEO_PARAM_PORTFORMATTYPE *format = param;
320 omx_base_video_PortType *port;
321
322 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
323 if (r)
324 return r;
325
326 if (format->nPortIndex > 1)
327 return OMX_ErrorBadPortIndex;
328
329 port = (omx_base_video_PortType *)priv->ports[format->nPortIndex];
330 memcpy(&port->sVideoParam, format, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
331 break;
332 }
333 default:
334 return omx_base_component_SetParameter(handle, idx, param);
335 }
336 return OMX_ErrorNone;
337 }
338
vid_dec_GetParameter(OMX_HANDLETYPE handle,OMX_INDEXTYPE idx,OMX_PTR param)339 static OMX_ERRORTYPE vid_dec_GetParameter(OMX_HANDLETYPE handle, OMX_INDEXTYPE idx, OMX_PTR param)
340 {
341 OMX_COMPONENTTYPE *comp = handle;
342 vid_dec_PrivateType *priv = comp->pComponentPrivate;
343 OMX_ERRORTYPE r;
344
345 if (!param)
346 return OMX_ErrorBadParameter;
347
348 switch(idx) {
349 case OMX_IndexParamStandardComponentRole: {
350 OMX_PARAM_COMPONENTROLETYPE *role = param;
351
352 r = checkHeader(param, sizeof(OMX_PARAM_COMPONENTROLETYPE));
353 if (r)
354 return r;
355
356 if (priv->profile == PIPE_VIDEO_PROFILE_MPEG2_MAIN)
357 strcpy((char *)role->cRole, OMX_VID_DEC_MPEG2_ROLE);
358 else if (priv->profile == PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH)
359 strcpy((char *)role->cRole, OMX_VID_DEC_AVC_ROLE);
360 else if (priv->profile == PIPE_VIDEO_PROFILE_HEVC_MAIN)
361 strcpy((char *)role->cRole, OMX_VID_DEC_HEVC_ROLE);
362
363 break;
364 }
365
366 case OMX_IndexParamVideoInit:
367 r = checkHeader(param, sizeof(OMX_PORT_PARAM_TYPE));
368 if (r)
369 return r;
370
371 memcpy(param, &priv->sPortTypesParam[OMX_PortDomainVideo], sizeof(OMX_PORT_PARAM_TYPE));
372 break;
373
374 case OMX_IndexParamVideoPortFormat: {
375 OMX_VIDEO_PARAM_PORTFORMATTYPE *format = param;
376 omx_base_video_PortType *port;
377
378 r = checkHeader(param, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
379 if (r)
380 return r;
381
382 if (format->nPortIndex > 1)
383 return OMX_ErrorBadPortIndex;
384
385 port = (omx_base_video_PortType *)priv->ports[format->nPortIndex];
386 memcpy(format, &port->sVideoParam, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
387 break;
388 }
389
390 default:
391 return omx_base_component_GetParameter(handle, idx, param);
392
393 }
394 return OMX_ErrorNone;
395 }
396
vid_dec_MessageHandler(OMX_COMPONENTTYPE * comp,internalRequestMessageType * msg)397 static OMX_ERRORTYPE vid_dec_MessageHandler(OMX_COMPONENTTYPE* comp, internalRequestMessageType *msg)
398 {
399 vid_dec_PrivateType* priv = comp->pComponentPrivate;
400
401 if (msg->messageType == OMX_CommandStateSet) {
402 if ((msg->messageParam == OMX_StateIdle ) && (priv->state == OMX_StateLoaded)) {
403 if (priv->profile == PIPE_VIDEO_PROFILE_MPEG2_MAIN)
404 vid_dec_mpeg12_Init(priv);
405 else if (priv->profile == PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH)
406 vid_dec_h264_Init(priv);
407 else if (priv->profile == PIPE_VIDEO_PROFILE_HEVC_MAIN)
408 vid_dec_h265_Init(priv);
409
410 } else if ((msg->messageParam == OMX_StateLoaded) && (priv->state == OMX_StateIdle)) {
411 if (priv->shadow) {
412 priv->shadow->destroy(priv->shadow);
413 priv->shadow = NULL;
414 }
415 if (priv->codec) {
416 priv->codec->destroy(priv->codec);
417 priv->codec = NULL;
418 }
419 }
420 }
421
422 return omx_base_component_MessageHandler(comp, msg);
423 }
424
vid_dec_DecodeBuffer(omx_base_PortType * port,OMX_BUFFERHEADERTYPE * buf)425 static OMX_ERRORTYPE vid_dec_DecodeBuffer(omx_base_PortType *port, OMX_BUFFERHEADERTYPE *buf)
426 {
427 OMX_COMPONENTTYPE* comp = port->standCompContainer;
428 vid_dec_PrivateType *priv = comp->pComponentPrivate;
429 unsigned i = priv->num_in_buffers++;
430 OMX_ERRORTYPE r;
431
432 priv->in_buffers[i] = buf;
433 priv->sizes[i] = buf->nFilledLen;
434 priv->inputs[i] = buf->pBuffer;
435 priv->timestamps[i] = buf->nTimeStamp;
436
437 while (priv->num_in_buffers > (!!(buf->nFlags & OMX_BUFFERFLAG_EOS) ? 0 : 1)) {
438 bool eos = !!(priv->in_buffers[0]->nFlags & OMX_BUFFERFLAG_EOS);
439 unsigned min_bits_left = eos ? 32 : MAX2(buf->nFilledLen * 8, 32);
440 struct vl_vlc vlc;
441
442 vl_vlc_init(&vlc, priv->num_in_buffers, priv->inputs, priv->sizes);
443
444 if (priv->slice)
445 priv->bytes_left = vl_vlc_bits_left(&vlc) / 8;
446
447 while (vl_vlc_bits_left(&vlc) > min_bits_left) {
448 priv->Decode(priv, &vlc, min_bits_left);
449 vl_vlc_fillbits(&vlc);
450 }
451
452 if (priv->slice) {
453 unsigned bytes = priv->bytes_left - vl_vlc_bits_left(&vlc) / 8;
454
455 priv->codec->decode_bitstream(priv->codec, priv->target, &priv->picture.base,
456 1, &priv->slice, &bytes);
457
458 if (priv->num_in_buffers)
459 priv->slice = priv->inputs[1];
460 else
461 priv->slice = NULL;
462 }
463
464 if (eos && priv->frame_started)
465 priv->EndFrame(priv);
466
467 if (priv->frame_finished) {
468 priv->frame_finished = false;
469 priv->in_buffers[0]->nFilledLen = priv->in_buffers[0]->nAllocLen;
470 r = base_port_SendBufferFunction(port, priv->in_buffers[0]);
471 } else if (eos) {
472 vid_dec_FreeInputPortPrivate(priv->in_buffers[0]);
473 priv->in_buffers[0]->nFilledLen = priv->in_buffers[0]->nAllocLen;
474 r = base_port_SendBufferFunction(port, priv->in_buffers[0]);
475 } else {
476 priv->in_buffers[0]->nFilledLen = 0;
477 r = port->ReturnBufferFunction(port, priv->in_buffers[0]);
478 }
479
480 if (--priv->num_in_buffers) {
481 unsigned delta = MIN2((min_bits_left - vl_vlc_bits_left(&vlc)) / 8, priv->sizes[1]);
482
483 priv->in_buffers[0] = priv->in_buffers[1];
484 priv->sizes[0] = priv->sizes[1] - delta;
485 priv->inputs[0] = priv->inputs[1] + delta;
486 priv->timestamps[0] = priv->timestamps[1];
487 }
488
489 if (r)
490 return r;
491 }
492
493 return OMX_ErrorNone;
494 }
495
vid_dec_FreeDecBuffer(omx_base_PortType * port,OMX_U32 idx,OMX_BUFFERHEADERTYPE * buf)496 static OMX_ERRORTYPE vid_dec_FreeDecBuffer(omx_base_PortType *port, OMX_U32 idx, OMX_BUFFERHEADERTYPE *buf)
497 {
498 vid_dec_FreeInputPortPrivate(buf);
499 return base_port_FreeBuffer(port, idx, buf);
500 }
501
vid_dec_FrameDecoded(OMX_COMPONENTTYPE * comp,OMX_BUFFERHEADERTYPE * input,OMX_BUFFERHEADERTYPE * output)502 static void vid_dec_FrameDecoded(OMX_COMPONENTTYPE *comp, OMX_BUFFERHEADERTYPE* input,
503 OMX_BUFFERHEADERTYPE* output)
504 {
505 vid_dec_PrivateType *priv = comp->pComponentPrivate;
506
507 vid_dec_FrameDecoded_common(priv, input, output);
508 }
509