Advertisement
Guest User

Untitled

a guest
Aug 31st, 2011
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. //#include <ti/sdo/ce/CERuntime.h>
  2. #include <xdc/std.h>
  3.  
  4. #include <ti/sdo/ce/Engine.h>
  5. #include <ti/sdo/ce/osal/Memory.h>
  6. #include <ti/sdo/ce/video2/viddec2.h>
  7. #include <ti/xdais/dm/ivideo.h>
  8. #include <ti/xdais/dm/ividdec2.h>
  9. #include <ti/xdais/xdas.h>
  10. #include <ti/xdais/dm/xdm.h>
  11. #include <ti/xdais/dm/ivideo.h>
  12. #include <ti/xdais/dm/ividdec2.h>
  13.  
  14. #include <ti/sdo/ce/video/videnc.h>
  15. #include <ti/sdo/ce/trace/gt.h>
  16.  
  17. //#include <ti/sdo/fc/utils/api/alg.h>
  18. //#include <ti/xdais/ialg.h>
  19.  
  20. #include <string.h> /* for memset */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. //#include "idsptry.h"
  25. #include "h264vdec.h"
  26. #include "h264vdec_ti.h"
  27.  
  28. #include "cmem.h"
  29. #include <libavcodec/avcodec.h>
  30. #include "frame.h"
  31. #include "codec.h"
  32. #include "util.h"
  33.  
  34. #define PADX 32
  35. #define PADY 24
  36.  
  37. static String progName = "ce";
  38. static String decoderName = "h264dec";
  39. static String engineName = "ceEngine";
  40.  
  41. AVCodecContext *avc;
  42. AVBitStreamFilterContext *bsf;
  43.  
  44. static Engine_Handle ce;
  45. static VIDDEC2_Handle dec;
  46.  
  47. IH264VDEC_Params params;
  48. IH264VDEC_DynamicParams dynamicParams;
  49.  
  50. VIDDEC2_Status status;
  51. XDM1_BufDesc *inbufs;
  52. XDM_BufDesc *outbufs;
  53. VIDDEC2_InArgs in_args;
  54. VIDDEC2_OutArgs out_args;
  55.  
  56. uint8_t *input_buf = NULL;
  57.  
  58. int input_size;
  59. int frame_size;
  60.  
  61. static CMEM_AllocParams cma;
  62.  
  63.  
  64. XDAS_UInt8* pOutputBuf [XDM_MAX_IO_BUFFERS];
  65. XDAS_Int32 outBufSize [XDM_MAX_IO_BUFFERS];
  66.  
  67. void dsp_dummy_func() { }
  68.  
  69. static int alloc_input(int size)
  70. {
  71. uint8_t *buf;
  72.  
  73. if (size <= input_size)
  74. return 0;
  75.  
  76. buf = CMEM_alloc(size, &cma);
  77. if (!buf)
  78. return -1;
  79.  
  80. if (input_buf)
  81. CMEM_free(input_buf, &cma);
  82.  
  83. input_buf = buf;
  84. input_size = size;
  85.  
  86. return 0;
  87. }
  88.  
  89.  
  90. static int ce_open(const char *name, AVCodecContext *cc,
  91. struct frame_format *ff)
  92. {
  93. XDAS_Int32 err;
  94.  
  95. if (cc->codec_id != CODEC_ID_H264) {
  96. fprintf(stderr, "CE: unsupported codec %d\n", cc->codec_id);
  97. return -1;
  98. }
  99.  
  100. //TODO: add a check against resolution (WVGA/480p is the maximum)
  101.  
  102.  
  103. // TODO: Check what the bitstream filter is used for
  104. if (cc->extradata && cc->extradata_size > 0 && cc->extradata[0] == 1) {
  105. bsf = av_bitstream_filter_init("h264_mp4toannexb");
  106. if (!bsf)
  107. return -1;
  108. avc = cc;
  109. }
  110.  
  111. ff->width = ALIGN(cc->width + 2*PADX, 16);
  112. ff->height = ALIGN(cc->height + 4*PADY, 16);
  113. ff->disp_x = 0;
  114. ff->disp_y = 0;
  115. ff->disp_w = cc->width;
  116. ff->disp_h = cc->height;
  117. ff->pixfmt = PIX_FMT_YUYV422;
  118.  
  119.  
  120. int buf_w = ff->width, buf_h = ff->height;
  121. buf_w += 16 - (buf_w % 16);
  122. frame_size = buf_w * buf_h * 2;
  123.  
  124. /* init Codec Engine */
  125. CERuntime_init();
  126. printf("This is the DSP replying!\n");
  127.  
  128. if ((ce = Engine_open(engineName, NULL, NULL)) == NULL) {
  129. fprintf(stderr, "error: can't open engine ceEngine\n");
  130. goto err;
  131. } else {
  132. printf( "SUCCESS: opened engine %s\n", engineName);
  133. }
  134.  
  135. if (CMEM_init()) {
  136. printf("ERROR: Couldn't perform CMEM_init()\n");
  137. return -1;
  138. }
  139.  
  140. cma.type = CMEM_HEAP;
  141. cma.alignment = 16;
  142.  
  143.  
  144. /* Init video decoder parameters */
  145. params.viddecParams.maxFrameRate = 30000;
  146. params.viddecParams.maxBitRate = 10485760;
  147. params.viddecParams.dataEndianness = XDM_BYTE;
  148. params.viddecParams.maxHeight = ALIGN(ff->disp_h, 16);
  149. params.viddecParams.maxWidth = ALIGN(ff->disp_w, 16);
  150. params.viddecParams.forceChromaFormat = 4;
  151. params.inputStreamFormat = 0;
  152. params.maxDisplayDelay = 0;
  153.  
  154. params.viddecParams.size = sizeof(IH264VDEC_Params);
  155. status.size = sizeof(VIDDEC2_Status);
  156. dynamicParams.viddecDynamicParams.size = sizeof(IH264VDEC_DynamicParams);
  157. in_args.size = sizeof(VIDDEC2_InArgs);
  158. out_args.size = sizeof(VIDDEC2_OutArgs);
  159.  
  160. printf( "Done initialiazing, creating decoder handle\n");
  161.  
  162. /* allocate and initialize video decoder on the engine */
  163. dec = VIDDEC2_create(ce, decoderName, &(params.viddecParams));
  164. if (dec == NULL) {
  165. printf( "App-> ERROR: can't open codec %s\n", decoderName);
  166. goto err;
  167. } else {
  168. printf( "SUCCESS: opened codec %s\n", decoderName);
  169. }
  170.  
  171. /* Set the dynamic parameters */
  172. dynamicParams.viddecDynamicParams.decodeHeader = XDM_DECODE_AU;
  173. dynamicParams.viddecDynamicParams.displayWidth = 0;
  174. dynamicParams.viddecDynamicParams.frameSkipMode = IVIDEO_NO_SKIP;
  175. dynamicParams.viddecDynamicParams.frameOrder = IVIDDEC2_DISPLAY_ORDER;
  176. dynamicParams.viddecDynamicParams.newFrameFlag = XDAS_FALSE;
  177. dynamicParams.viddecDynamicParams.mbDataFlag = XDAS_FALSE;
  178.  
  179. IH264VDEC_DynamicParams *extParams = (IH264VDEC_DynamicParams *)&dynamicParams.viddecDynamicParams;
  180. extParams->mbErrorBufFlag = FALSE; //TRUE;
  181. //extParams->mbErrorBufSize = ((IMAGE_WIDTH * IMAGE_HEIGHT)/256);
  182. extParams->Sei_Vui_parse_flag = FALSE; //TRUE;
  183. extParams->numNALunits = 0;
  184.  
  185. err = VIDDEC2_control(dec, XDM_SETPARAMS, (IVIDDEC2_DynamicParams *)extParams, &status);
  186. if (err) {
  187. fprintf(stderr, "VIDDEC2_control(XDM_SETPARAMS) failed %d\n", err);
  188. goto err;
  189. }
  190.  
  191.  
  192. /* Init buffers and context information */
  193. err = VIDDEC2_control(dec, XDM_GETBUFINFO, (IVIDDEC2_DynamicParams *)extParams, &status);
  194. if (err) {
  195. fprintf(stderr, "VIDDEC2_control(XDM_GETBUFINFO) failed %d\n", err);
  196. goto err;
  197. }
  198.  
  199. inbufs = malloc(sizeof(*inbufs));
  200. outbufs = malloc(sizeof(*outbufs));
  201.  
  202. inbufs->numBufs = 1;
  203. outbufs->numBufs = 1;
  204.  
  205. printf("INFO: Successfully finished ce_open()\n");
  206. return 0;
  207.  
  208. err:
  209. printf("ERROR: An error has occured during ce_open() method\n");
  210. CMEM_exit();
  211. return -1;
  212. }
  213.  
  214.  
  215.  
  216. static int ce_decode(AVPacket *p)
  217. {
  218. struct frame *f;
  219. uint8_t *buf;
  220. int bufsize;
  221. int err;
  222. int i;
  223.  
  224. if (bsf) {
  225. if (av_bitstream_filter_filter(bsf, avc, NULL, &buf, &bufsize,
  226. p->data, p->size, 0) < 0) {
  227. fprintf(stderr, "CE: bsf error\n");
  228. return -1;
  229. }
  230. } else {
  231. buf = p->data;
  232. bufsize = p->size;
  233. }
  234.  
  235. /* Allocate a buffer input on CMEM */
  236. if (alloc_input(bufsize)) {
  237. printf("ERROR: error allocating CMEM buffer of size: %d\n", bufsize);
  238. return -1;
  239. }
  240.  
  241. /* Copy the compressed buffer (buf) to the allocated CMEM input buffer (input_buf) */
  242. memcpy(input_buf, buf, bufsize);
  243.  
  244. if (bsf)
  245. av_free(buf);
  246.  
  247. f = ofbp_get_frame();
  248.  
  249. printf("INFO_CE: Setting in args\n");
  250. in_args.inputID = (XDAS_Int32)f;
  251. in_args.numBytes = bufsize;
  252.  
  253.  
  254. inbufs->descs[0].buf = input_buf;
  255. inbufs->descs[0].bufSize = (XDAS_Int32) bufsize;
  256.  
  257. out_args.outBufsInUseFlag = 0;
  258.  
  259. pOutputBuf[0] = (uint8_t*)f->virt[0];
  260. outBufSize[0] = frame_size;
  261.  
  262. outbufs->bufs = (XDAS_Int8 **)pOutputBuf;
  263. outbufs->bufSizes = (XDAS_Int32 *)outBufSize ;
  264.  
  265. err = VIDDEC2_process(dec, (XDM1_BufDesc *)inbufs, (XDM_BufDesc *)outbufs, &in_args, &out_args);
  266. if (err) {
  267. fprintf(stderr, "VIDDEC2_process() error %d\n", err);
  268. return -1;
  269. }
  270.  
  271. for (i = 0; out_args.freeBufID[i]; i++)
  272. ofbp_put_frame((struct frame *)out_args.freeBufID[i]);
  273.  
  274. return 0;
  275. }
  276.  
  277.  
  278. static void ce_close(void)
  279. {
  280. if (dec) VIDDEC2_delete(dec); dec = NULL;
  281. if (ce) Engine_close(ce); ce = NULL;
  282. if (inbufs) free(inbufs); inbufs = NULL;
  283. if (outbufs) free(outbufs); outbufs = NULL;
  284. if (input_buf) CMEM_free(input_buf, &cma); input_buf = NULL;
  285. if (bsf) av_bitstream_filter_close(bsf); bsf = NULL;
  286. CMEM_exit();
  287. }
  288.  
  289.  
  290. CODEC(avcodec) = {
  291. .name = "dce",
  292. .flags = OFBP_PHYS_MEM,
  293. .open = ce_open,
  294. .decode = ce_decode,
  295. .close = ce_close,
  296. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement