Create Project for repo.or.cz
[vp.git] / src / mfc / SsbSipH264Decode.cpp
blobd8117197cab8e9500ab0432b7e126a6aba3eff99
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/ioctl.h>
6 #include <fcntl.h>
7 #include <ctype.h>
8 #include <unistd.h>
9 #include <sys/mman.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <sys/time.h>
14 #include "MfcDriver.h"
15 #include "MfcDrvParams.h"
17 #include "SsbSipH264Decode.h"
18 #include "SsbSipLogMsg.h"
20 #define _MFCLIB_H264_DEC_MAGIC_NUMBER 0x92241002
22 typedef struct
24 int magic;
25 int hOpen;
26 void *p_buf;
27 int size;
28 int fInit;
30 unsigned char *mapped_addr;
31 unsigned int width;
32 unsigned int height;
33 unsigned int buf_width;
34 unsigned int buf_height;
35 } _MFCLIB_H264_DEC;
38 void *SsbSipH264DecodeInit()
40 _MFCLIB_H264_DEC *pCTX;
41 int hOpen;
42 unsigned char *addr;
45 //////////////////////////////
46 ///// CreateFile /////
47 //////////////////////////////
48 hOpen = open(MFC_DEV_NAME, O_RDWR|O_NDELAY);
49 if (hOpen < 0) {
50 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeInit", "MFC Open failure.\n");
51 return NULL;
54 //////////////////////////////////////////
55 // Mapping the MFC Input/Output Buffer //
56 //////////////////////////////////////////
57 // mapping shared in/out buffer between application and MFC device driver
58 addr = (unsigned char *) mmap(0, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, hOpen, 0);
59 if (addr == NULL) {
60 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeInit", "MFC Mmap failure.\n");
61 return NULL;
64 pCTX = (_MFCLIB_H264_DEC *) malloc(sizeof(_MFCLIB_H264_DEC));
65 if (pCTX == NULL) {
66 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeInit", "malloc failed.\n");
67 close(hOpen);
68 return NULL;
70 memset(pCTX, 0, sizeof(_MFCLIB_H264_DEC));
72 pCTX->magic = _MFCLIB_H264_DEC_MAGIC_NUMBER;
73 pCTX->hOpen = hOpen;
74 pCTX->fInit = 0;
75 pCTX->mapped_addr = addr;
77 return (void *) pCTX;
81 int SsbSipH264DecodeExe(void *openHandle, long lengthBufFill)
83 _MFCLIB_H264_DEC *pCTX;
84 MFC_ARGS mfc_args;
85 int r;
88 ////////////////////////////////
89 // Input Parameter Checking //
90 ////////////////////////////////
91 if (openHandle == NULL) {
92 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeExe", "openHandle is NULL\n");
93 return SSBSIP_H264_DEC_RET_ERR_INVALID_HANDLE;
95 if ((lengthBufFill < 0) || (lengthBufFill > 0x100000)) {
96 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeExe", "lengthBufFill is invalid. (lengthBufFill=%d)\n", lengthBufFill);
97 return SSBSIP_H264_DEC_RET_ERR_INVALID_PARAM;
100 pCTX = (_MFCLIB_H264_DEC *) openHandle;
103 if (!pCTX->fInit) {
105 /////////////////////////////////////////////////
106 ///// (DeviceIoControl) /////
107 ///// IOCTL_MFC_H264_DEC_INIT /////
108 /////////////////////////////////////////////////
109 mfc_args.dec_init.in_strmSize = lengthBufFill;
110 r = ioctl(pCTX->hOpen, IOCTL_MFC_H264_DEC_INIT, &mfc_args);
111 if ((r < 0) || (mfc_args.dec_init.ret_code < 0)) {
112 return SSBSIP_H264_DEC_RET_ERR_CONFIG_FAIL;
115 // Output argument (width , height)
116 pCTX->width = mfc_args.dec_init.out_width;
117 pCTX->height = mfc_args.dec_init.out_height;
118 pCTX->buf_width = mfc_args.dec_init.out_buf_width;
119 pCTX->buf_height = mfc_args.dec_init.out_buf_height;
121 pCTX->fInit = 1;
123 return SSBSIP_H264_DEC_RET_OK;
127 /////////////////////////////////////////////////
128 ///// (DeviceIoControl) /////
129 ///// IOCTL_MFC_H264_DEC_EXE /////
130 /////////////////////////////////////////////////
131 mfc_args.dec_exe.in_strmSize = lengthBufFill;
132 r = ioctl(pCTX->hOpen, IOCTL_MFC_H264_DEC_EXE, &mfc_args);
133 if ((r < 0) || (mfc_args.dec_exe.ret_code < 0)) {
134 return SSBSIP_H264_DEC_RET_ERR_DECODE_FAIL;
137 return SSBSIP_H264_DEC_RET_OK;
141 int SsbSipH264DecodeDeInit(void *openHandle)
143 _MFCLIB_H264_DEC *pCTX;
146 ////////////////////////////////
147 // Input Parameter Checking //
148 ////////////////////////////////
149 if (openHandle == NULL) {
150 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeDeInit", "openHandle is NULL\n");
151 return SSBSIP_H264_DEC_RET_ERR_INVALID_HANDLE;
154 pCTX = (_MFCLIB_H264_DEC *) openHandle;
157 munmap(pCTX->mapped_addr, BUF_SIZE);
158 close(pCTX->hOpen);
161 return SSBSIP_H264_DEC_RET_OK;
165 void *SsbSipH264DecodeGetInBuf(void *openHandle, long size)
167 void *pStrmBuf;
168 int nStrmBufSize;
170 _MFCLIB_H264_DEC *pCTX;
171 MFC_ARGS mfc_args;
172 int r;
175 ////////////////////////////////
176 // Input Parameter Checking //
177 ////////////////////////////////
178 if (openHandle == NULL) {
179 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetInBuf", "openHandle is NULL\n");
180 return NULL;
182 if ((size < 0) || (size > 0x100000)) {
183 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetInBuf", "size is invalid. (size=%d)\n", size);
184 return NULL;
187 pCTX = (_MFCLIB_H264_DEC *) openHandle;
189 /////////////////////////////////////////////////
190 ///// (DeviceIoControl) /////
191 ///// IOCTL_MFC_GET_STRM_BUF_ADDR /////
192 /////////////////////////////////////////////////
193 mfc_args.get_buf_addr.in_usr_data = (int)pCTX->mapped_addr;
194 r = ioctl(pCTX->hOpen, IOCTL_MFC_GET_LINE_BUF_ADDR, &mfc_args);
195 if ((r < 0) || (mfc_args.get_buf_addr.ret_code < 0)) {
196 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetInBuf", "Failed in get LINE_BUF address\n");
197 return NULL;
200 // Output arguments
201 pStrmBuf = (void *) mfc_args.get_buf_addr.out_buf_addr;
202 nStrmBufSize = mfc_args.get_buf_addr.out_buf_size;
205 if ((long)nStrmBufSize < size) {
206 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetInBuf", \
207 "Requested size is greater than available buffer. (size=%d, avail=%d)\n", size, nStrmBufSize);
208 return NULL;
211 return pStrmBuf;
215 void *SsbSipH264DecodeGetOutBuf(void *openHandle, long *size)
217 void *pFramBuf;
218 int nFramBufSize;
220 _MFCLIB_H264_DEC *pCTX;
221 MFC_ARGS mfc_args;
222 int r;
225 ////////////////////////////////
226 // Input Parameter Checking //
227 ////////////////////////////////
228 if (openHandle == NULL) {
229 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetOutBuf", "openHandle is NULL\n");
230 return NULL;
232 if (size == NULL) {
233 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetOutBuf", "size is NULL\n");
234 return NULL;
237 pCTX = (_MFCLIB_H264_DEC *) openHandle;
241 /////////////////////////////////////////////////
242 ///// (DeviceIoControl) /////
243 ///// IOCTL_MFC_GET_FRAM_BUF_ADDR /////
244 /////////////////////////////////////////////////
245 mfc_args.get_buf_addr.in_usr_data = (int)pCTX->mapped_addr;
246 r = ioctl(pCTX->hOpen, IOCTL_MFC_GET_FRAM_BUF_ADDR, &mfc_args);
247 if ((r < 0) || (mfc_args.get_buf_addr.ret_code < 0)) {
248 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetOutBuf", "Failed in get FRAM_BUF address.\n");
249 return NULL;
252 // Output arguments
253 pFramBuf = (void *) mfc_args.get_buf_addr.out_buf_addr;
254 nFramBufSize = mfc_args.get_buf_addr.out_buf_size;
256 *size = nFramBufSize;
258 return pFramBuf;
262 int SsbSipH264DecodeSetConfig(void *openHandle, H264_DEC_CONF conf_type, void *value)
264 _MFCLIB_H264_DEC *pCTX;
265 int r = 0;
266 MFC_SET_CONFIG_ARG set_config;
269 ////////////////////////////////
270 // Input Parameter Checking //
271 ////////////////////////////////
272 if (openHandle == NULL) {
273 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeSetConfig", "openHandle is NULL\n");
274 return SSBSIP_H264_DEC_RET_ERR_INVALID_HANDLE;
276 if (value == NULL) {
277 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeSetConfig", "value is NULL\n");
278 return SSBSIP_H264_DEC_RET_ERR_INVALID_PARAM;
281 pCTX = (_MFCLIB_H264_DEC *) openHandle;
283 switch(conf_type) {
285 case H264_DEC_SETCONF_POST_ROTATE:
287 set_config.in_config_param = MFC_SET_CONFIG_DEC_ROTATE;
288 set_config.in_config_value[0] = *((unsigned int *) value);
289 set_config.in_config_value[1] = 0;
290 r = ioctl(pCTX->hOpen, IOCTL_MFC_SET_CONFIG, &set_config);
291 if ( (r < 0) || (set_config.ret_code < 0) ) {
292 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeSetConfig", "Error in H264_DEC_SETCONF_POST_ROTATE.\n");
293 return SSBSIP_H264_DEC_RET_ERR_SETCONF_FAIL;
295 break;
297 default:
298 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeSetConfig", "No such conf_type is supported.\n");
299 return SSBSIP_H264_DEC_RET_ERR_SETCONF_FAIL;
302 return SSBSIP_H264_DEC_RET_OK;
307 int SsbSipH264DecodeGetConfig(void *openHandle, H264_DEC_CONF conf_type, void *value)
309 _MFCLIB_H264_DEC *pCTX;
310 int r;
311 MFC_ARGS mfc_args;
312 ////////////////////////////////
313 // Input Parameter Checking //
314 ////////////////////////////////
315 if (openHandle == NULL) {
316 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetConfig", "openHandle is NULL\n");
317 return SSBSIP_H264_DEC_RET_ERR_INVALID_HANDLE;
320 pCTX = (_MFCLIB_H264_DEC *) openHandle;
323 switch (conf_type) {
325 case H264_DEC_GETCONF_STREAMINFO:
326 ((SSBSIP_H264_STREAM_INFO *)value)->width = pCTX->width;
327 ((SSBSIP_H264_STREAM_INFO *)value)->height = pCTX->height;
328 ((SSBSIP_H264_STREAM_INFO *)value)->buf_width = pCTX->buf_width;
329 ((SSBSIP_H264_STREAM_INFO *)value)->buf_height = pCTX->buf_height;
330 break;
332 case H264_DEC_GETCONF_PHYADDR_FRAM_BUF:
333 r = ioctl(pCTX->hOpen, IOCTL_MFC_GET_PHY_FRAM_BUF_ADDR, &mfc_args);
334 if ((r < 0) || (mfc_args.get_buf_addr.ret_code < 0)) {
335 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetConfig", "Failed in get FRAM_BUF physical address.\n");
336 return SSBSIP_H264_DEC_RET_ERR_GETCONF_FAIL;
338 ((unsigned int *) value)[0] = mfc_args.get_buf_addr.out_buf_addr;
339 ((unsigned int *) value)[1] = mfc_args.get_buf_addr.out_buf_size;
341 break;
343 case H264_DEC_GETCONF_FRAM_NEED_COUNT:
344 mfc_args.get_config.in_config_param = MFC_GET_CONFIG_DEC_FRAME_NEED_COUNT;
345 mfc_args.get_config.out_config_value[0] = 0;
346 mfc_args.get_config.out_config_value[1] = 0;
348 r = ioctl(pCTX->hOpen, IOCTL_MFC_GET_CONFIG, &mfc_args);
349 if ((r <0) || (mfc_args.get_config.ret_code < 0)) {
350 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetConfig", "Error in H264_DEC_GETCONF_FRAM_NEED_COUNT.\n");
351 return SSBSIP_H264_DEC_RET_ERR_GETCONF_FAIL;
353 ((int *) value)[0] = mfc_args.get_config.out_config_value[0];
355 break;
357 default:
358 LOG_MSG(LOG_ERROR, "SsbSipH264DecodeGetConfig", "No such conf_type is suppoted.\n");
359 return SSBSIP_H264_DEC_RET_ERR_GETCONF_FAIL;
362 return SSBSIP_H264_DEC_RET_OK;