Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <ti/sdo/ce/CERuntime.h>
- #include <xdc/std.h>
- #include <ti/sdo/ce/Engine.h>
- #include <ti/sdo/ce/osal/Memory.h>
- #include <ti/sdo/ce/video2/viddec2.h>
- #include <ti/xdais/dm/ivideo.h>
- #include <ti/xdais/dm/ividdec2.h>
- #include <ti/xdais/xdas.h>
- #include <ti/xdais/dm/xdm.h>
- #include <ti/xdais/dm/ivideo.h>
- #include <ti/xdais/dm/ividdec2.h>
- #include <ti/sdo/ce/video/videnc.h>
- #include <ti/sdo/ce/trace/gt.h>
- //#include <ti/sdo/fc/utils/api/alg.h>
- //#include <ti/xdais/ialg.h>
- #include <string.h> /* for memset */
- #include <stdio.h>
- #include <stdlib.h>
- //#include "idsptry.h"
- #include "h264vdec.h"
- #include "h264vdec_ti.h"
- #include "cmem.h"
- #include <libavcodec/avcodec.h>
- #include "frame.h"
- #include "codec.h"
- #include "util.h"
- #define PADX 32
- #define PADY 24
- static String progName = "ce";
- static String decoderName = "h264dec";
- static String engineName = "ceEngine";
- AVCodecContext *avc;
- AVBitStreamFilterContext *bsf;
- static Engine_Handle ce;
- static VIDDEC2_Handle dec;
- IH264VDEC_Params params;
- IH264VDEC_DynamicParams dynamicParams;
- VIDDEC2_Status status;
- XDM1_BufDesc *inbufs;
- XDM_BufDesc *outbufs;
- VIDDEC2_InArgs in_args;
- VIDDEC2_OutArgs out_args;
- uint8_t *input_buf = NULL;
- int input_size;
- int frame_size;
- static CMEM_AllocParams cma;
- XDAS_UInt8* pOutputBuf [XDM_MAX_IO_BUFFERS];
- XDAS_Int32 outBufSize [XDM_MAX_IO_BUFFERS];
- void dsp_dummy_func() { }
- static int alloc_input(int size)
- {
- uint8_t *buf;
- if (size <= input_size)
- return 0;
- buf = CMEM_alloc(size, &cma);
- if (!buf)
- return -1;
- if (input_buf)
- CMEM_free(input_buf, &cma);
- input_buf = buf;
- input_size = size;
- return 0;
- }
- static int ce_open(const char *name, AVCodecContext *cc,
- struct frame_format *ff)
- {
- XDAS_Int32 err;
- if (cc->codec_id != CODEC_ID_H264) {
- fprintf(stderr, "CE: unsupported codec %d\n", cc->codec_id);
- return -1;
- }
- //TODO: add a check against resolution (WVGA/480p is the maximum)
- // TODO: Check what the bitstream filter is used for
- if (cc->extradata && cc->extradata_size > 0 && cc->extradata[0] == 1) {
- bsf = av_bitstream_filter_init("h264_mp4toannexb");
- if (!bsf)
- return -1;
- avc = cc;
- }
- ff->width = ALIGN(cc->width + 2*PADX, 16);
- ff->height = ALIGN(cc->height + 4*PADY, 16);
- ff->disp_x = 0;
- ff->disp_y = 0;
- ff->disp_w = cc->width;
- ff->disp_h = cc->height;
- ff->pixfmt = PIX_FMT_YUYV422;
- int buf_w = ff->width, buf_h = ff->height;
- buf_w += 16 - (buf_w % 16);
- frame_size = buf_w * buf_h * 2;
- /* init Codec Engine */
- CERuntime_init();
- printf("This is the DSP replying!\n");
- if ((ce = Engine_open(engineName, NULL, NULL)) == NULL) {
- fprintf(stderr, "error: can't open engine ceEngine\n");
- goto err;
- } else {
- printf( "SUCCESS: opened engine %s\n", engineName);
- }
- if (CMEM_init()) {
- printf("ERROR: Couldn't perform CMEM_init()\n");
- return -1;
- }
- cma.type = CMEM_HEAP;
- cma.alignment = 16;
- /* Init video decoder parameters */
- params.viddecParams.maxFrameRate = 30000;
- params.viddecParams.maxBitRate = 10485760;
- params.viddecParams.dataEndianness = XDM_BYTE;
- params.viddecParams.maxHeight = ALIGN(ff->disp_h, 16);
- params.viddecParams.maxWidth = ALIGN(ff->disp_w, 16);
- params.viddecParams.forceChromaFormat = 4;
- params.inputStreamFormat = 0;
- params.maxDisplayDelay = 0;
- params.viddecParams.size = sizeof(IH264VDEC_Params);
- status.size = sizeof(VIDDEC2_Status);
- dynamicParams.viddecDynamicParams.size = sizeof(IH264VDEC_DynamicParams);
- in_args.size = sizeof(VIDDEC2_InArgs);
- out_args.size = sizeof(VIDDEC2_OutArgs);
- printf( "Done initialiazing, creating decoder handle\n");
- /* allocate and initialize video decoder on the engine */
- dec = VIDDEC2_create(ce, decoderName, &(params.viddecParams));
- if (dec == NULL) {
- printf( "App-> ERROR: can't open codec %s\n", decoderName);
- goto err;
- } else {
- printf( "SUCCESS: opened codec %s\n", decoderName);
- }
- /* Set the dynamic parameters */
- dynamicParams.viddecDynamicParams.decodeHeader = XDM_DECODE_AU;
- dynamicParams.viddecDynamicParams.displayWidth = 0;
- dynamicParams.viddecDynamicParams.frameSkipMode = IVIDEO_NO_SKIP;
- dynamicParams.viddecDynamicParams.frameOrder = IVIDDEC2_DISPLAY_ORDER;
- dynamicParams.viddecDynamicParams.newFrameFlag = XDAS_FALSE;
- dynamicParams.viddecDynamicParams.mbDataFlag = XDAS_FALSE;
- IH264VDEC_DynamicParams *extParams = (IH264VDEC_DynamicParams *)&dynamicParams.viddecDynamicParams;
- extParams->mbErrorBufFlag = FALSE; //TRUE;
- //extParams->mbErrorBufSize = ((IMAGE_WIDTH * IMAGE_HEIGHT)/256);
- extParams->Sei_Vui_parse_flag = FALSE; //TRUE;
- extParams->numNALunits = 0;
- err = VIDDEC2_control(dec, XDM_SETPARAMS, (IVIDDEC2_DynamicParams *)extParams, &status);
- if (err) {
- fprintf(stderr, "VIDDEC2_control(XDM_SETPARAMS) failed %d\n", err);
- goto err;
- }
- /* Init buffers and context information */
- err = VIDDEC2_control(dec, XDM_GETBUFINFO, (IVIDDEC2_DynamicParams *)extParams, &status);
- if (err) {
- fprintf(stderr, "VIDDEC2_control(XDM_GETBUFINFO) failed %d\n", err);
- goto err;
- }
- inbufs = malloc(sizeof(*inbufs));
- outbufs = malloc(sizeof(*outbufs));
- inbufs->numBufs = 1;
- outbufs->numBufs = 1;
- printf("INFO: Successfully finished ce_open()\n");
- return 0;
- err:
- printf("ERROR: An error has occured during ce_open() method\n");
- CMEM_exit();
- return -1;
- }
- static int ce_decode(AVPacket *p)
- {
- struct frame *f;
- uint8_t *buf;
- int bufsize;
- int err;
- int i;
- if (bsf) {
- if (av_bitstream_filter_filter(bsf, avc, NULL, &buf, &bufsize,
- p->data, p->size, 0) < 0) {
- fprintf(stderr, "CE: bsf error\n");
- return -1;
- }
- } else {
- buf = p->data;
- bufsize = p->size;
- }
- /* Allocate a buffer input on CMEM */
- if (alloc_input(bufsize)) {
- printf("ERROR: error allocating CMEM buffer of size: %d\n", bufsize);
- return -1;
- }
- /* Copy the compressed buffer (buf) to the allocated CMEM input buffer (input_buf) */
- memcpy(input_buf, buf, bufsize);
- if (bsf)
- av_free(buf);
- f = ofbp_get_frame();
- printf("INFO_CE: Setting in args\n");
- in_args.inputID = (XDAS_Int32)f;
- in_args.numBytes = bufsize;
- inbufs->descs[0].buf = input_buf;
- inbufs->descs[0].bufSize = (XDAS_Int32) bufsize;
- out_args.outBufsInUseFlag = 0;
- pOutputBuf[0] = (uint8_t*)f->virt[0];
- outBufSize[0] = frame_size;
- outbufs->bufs = (XDAS_Int8 **)pOutputBuf;
- outbufs->bufSizes = (XDAS_Int32 *)outBufSize ;
- err = VIDDEC2_process(dec, (XDM1_BufDesc *)inbufs, (XDM_BufDesc *)outbufs, &in_args, &out_args);
- if (err) {
- fprintf(stderr, "VIDDEC2_process() error %d\n", err);
- return -1;
- }
- for (i = 0; out_args.freeBufID[i]; i++)
- ofbp_put_frame((struct frame *)out_args.freeBufID[i]);
- return 0;
- }
- static void ce_close(void)
- {
- if (dec) VIDDEC2_delete(dec); dec = NULL;
- if (ce) Engine_close(ce); ce = NULL;
- if (inbufs) free(inbufs); inbufs = NULL;
- if (outbufs) free(outbufs); outbufs = NULL;
- if (input_buf) CMEM_free(input_buf, &cma); input_buf = NULL;
- if (bsf) av_bitstream_filter_close(bsf); bsf = NULL;
- CMEM_exit();
- }
- CODEC(avcodec) = {
- .name = "dce",
- .flags = OFBP_PHYS_MEM,
- .open = ce_open,
- .decode = ce_decode,
- .close = ce_close,
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement