static void decoder_renderer_cleanup() { IOFreePhyMem(&ps_mem_desc); IOFreePhyMem(&slice_mem_desc); IOFreeVirtMem(&mem_desc); IOFreePhyMem(&mem_desc); vpu_UnInit(); }
int mediaBufferInit(struct mediaBuffer *medBuf, int size) { int err; if (medBuf == NULL) { err_msg("Media Buffer: cannot init\n"); return -1; } medBuf->desc.size = size; err = IOGetPhyMem(&medBuf->desc); if (err) { err_msg("Media buffer: phys allocation failure\n"); return -1; } medBuf->desc.virt_uaddr = IOGetVirtMem(&(medBuf->desc)); if (medBuf->desc.virt_uaddr <= 0) { IOFreePhyMem(&medBuf->desc); err_msg("Media buffer: IOGetVirtMem failed\n"); return -1; } info_msg("Media buffer: allocated new buffer with size of %d\n", size); medBuf->vBufOut = (unsigned char *)medBuf->desc.virt_uaddr; medBuf->pBufOut = (unsigned char *)medBuf->desc.phy_addr; return 0; }
struct frame_buf * vpu_framebuf_alloc(int strideY, int height) { struct frame_buf *fb; int size; fb = get_framebuf(); if (fb == NULL) return NULL; size = strideY * height; memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); fb->desc.size = (size * 3 / 2); if (IOGetPhyMem(&fb->desc)) { DBG("--- Frame buffer allocation failed ---\n"); memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); return NULL; } fb->addrY = fb->desc.phy_addr; fb->addrCb = fb->addrY + size; fb->addrCr = fb->addrCb + (size>>2); fb->desc.virt_uaddr = IOGetVirtMem(&(fb->desc)); if (fb->desc.virt_uaddr <= 0) { IOFreePhyMem(&fb->desc); memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); return NULL; } return fb; }
void vpu_close_encoding_instance(EncodingInstance* instance) { int i; EncOutputInfo outinfo = {}; RetCode ret; EncodingInstance ptr = *instance; for(i = 0; i < ptr->fbcount; i++) { framebuf_free(ptr->pfbpool[i]); } free(ptr->fb); free(ptr->pfbpool); ret = vpu_EncClose(ptr->handle); if(ret == RETCODE_FRAME_NOT_COMPLETE) { vpu_EncGetOutputInfo(ptr->handle, &outinfo); vpu_EncClose(ptr->handle); } IOFreeVirtMem(&(ptr->mem_desc)); IOFreePhyMem(&(ptr->mem_desc)); close(ptr->fd); free(*instance); *instance = NULL; }
int mediaBufferDeinit(struct mediaBuffer *medBuf) { if (medBuf == NULL) { err_msg("Media Buffer: cannot deinit\n"); return -1; } IOFreePhyMem(&medBuf->desc); return 0; }
static void enc_uninit(MSFilter *f){ EncData *d=(EncData*)f->data; IOFreeVirtMem(&d->mem_desc); IOFreePhyMem(&d->mem_desc); vpu_UnInit(); if (d->flv->fd>=0) rec_close(f,NULL); ms_free(d->flv->data); ms_free(d->flv); ms_free(d->enc); ms_free(d); }
static void x264_framebuf_free(struct frame_buf *fb) { if (fb->desc.virt_uaddr) { IOFreeVirtMem(&fb->desc); } if (fb->desc.phy_addr) { IOFreePhyMem(&fb->desc); } memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); x264_put_framebuf(fb); }
struct frame_buf *framebuf_alloc(int stdMode, int format, int strideY, int height) { struct frame_buf *fb; int err; int divX, divY; fb = get_framebuf(); if(fb == NULL) return NULL; divX = (format == MODE420 || format == MODE422) ? 2 : 1; divY = (format == MODE420 || format == MODE224) ? 2 : 1; memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); fb->desc.size = (strideY * height + strideY / divX * height / divY * 2); if(cpu_is_mx37() || cpu_is_mx5x()) fb->desc.size += strideY / divX * height / divY; err = IOGetPhyMem(&fb->desc); if(err) { printf("Frame buffer allocation failure\n"); memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); return NULL; } fb->addrY = fb->desc.phy_addr; fb->addrCb = fb->addrY + strideY * height; fb->addrCr = fb->addrCb + strideY / divX * height / divY; fb->strideY = strideY; fb->strideC = strideY / divX; if(cpu_is_mx37() || cpu_is_mx5x()) { if(stdMode == STD_MJPG) fb->mvColBuf = fb->addrCr; else fb->mvColBuf = fb->addrCr + strideY / divX * height / divY; } fb->desc.virt_uaddr = IOGetVirtMem(&(fb->desc)); if(fb->desc.virt_uaddr <= 0) { IOFreePhyMem(&fb->desc); memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); return NULL; } return fb; }
VpuDec::~VpuDec() { RetCode ret; ret = vpu_DecClose(handle); if (ret == RETCODE_FRAME_NOT_COMPLETE) { vpu_SWReset(handle, 0); ret = vpu_DecClose(handle); if (ret != RETCODE_SUCCESS) printf("vpu_DecClose failed\n"); } printf("vpu_DecClose success\n"); for (int i = 0; i < regfbcount; i++) { framebuf_free(&fbpool[i]); } if(dec_format == STD_AVC){ IOFreePhyMem(&slice_mem_desc); IOFreePhyMem(&ps_mem_desc); } freeHwBuffer(&Dec_bufZone); if (dec_fb) { free(dec_fb); dec_fb = NULL; } if (dec_pfbpool) { free(dec_pfbpool); dec_pfbpool = NULL; } }
static struct frame_buf *x264_framebuf_alloc(int stdMode, int format, int strideY, int height) { struct frame_buf *fb; int err; int divX, divY; fb = x264_get_framebuf(); if (fb == NULL) return NULL; divX = (format == MODE420 || format == MODE422) ? 2 : 1; divY = (format == MODE420 || format == MODE224) ? 2 : 1; memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); fb->desc.size = (strideY * height + strideY / divX * height / divY * 2); fb->desc.size += strideY / divX * height / divY; err = IOGetPhyMem(&fb->desc); if (err) { ms_error("MSH264Enc: Frame buffer allocation failure\n"); memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); return NULL; } fb->addrY = fb->desc.phy_addr; fb->addrCb = fb->addrY + strideY * height; fb->addrCr = fb->addrCb + strideY / divX * height / divY; fb->strideY = strideY; fb->strideC = strideY / divX; fb->mvColBuf = fb->addrCr + strideY / divX * height / divY; fb->desc.virt_uaddr = IOGetVirtMem(&(fb->desc)); if (fb->desc.virt_uaddr <= 0) { IOFreePhyMem(&fb->desc); memset(&(fb->desc), 0, sizeof(vpu_mem_desc)); return NULL; } return fb; }
static void enc_init(MSFilter *f){ EncData *d=ms_new(EncData,1); d->enc=NULL; d->keyframe_int=10; /*10 seconds */ d->mode=0; d->framenum=0; d->generate_keyframe=FALSE; d->packer=NULL; d->vconf_list = &x264_conf_list[0]; d->vconf = ms_video_find_best_configuration_for_bitrate(d->vconf_list, 384000); d->flv = newFLVStream(2*1024*1024); d->enc = ms_new0(struct x264_encode,1); f->data=d; if(vpu_Init(NULL)!=0) { ms_error("MSH264Enc: VPU Init error\n"); return ; } d->mem_desc.size = STREAM_BUF_SIZE; if(IOGetPhyMem(&d->mem_desc)!=0) { ms_error("MSH264Enc: Unable to obtain physical memory\n"); return ; } /* mmap that physical buffer */ d->enc->virt_bsbuf_addr = IOGetVirtMem(&d->mem_desc); if (d->enc->virt_bsbuf_addr <= 0) { IOFreePhyMem(&d->mem_desc); ms_error("MSH264Enc: Unable to map physical memory\n"); return ; } d->enc->phy_bsbuf_addr = d->mem_desc.phy_addr; }
void VpuDec::freeHwBuffer(bufZone*) { IOFreeVirtMem(&mem_desc); IOFreePhyMem(&mem_desc); }
int main(void){ int ret = 0, i; vpu_versioninfo ver; // vpu version information vpu_mem_desc bit_stream_buf; // input bit stream allocated memory vpu_mem_desc source_buf; // source buffer allocated memory FrameBuffer source_frame; // source framebuffer // VPU specific members defined in vpu_lib.h EncHandle *handle = malloc(sizeof(EncHandle)); EncOpenParam *encOP = malloc(sizeof(EncOpenParam)); EncInitialInfo *initialInfo = malloc(sizeof(EncInitialInfo)); EncOutputInfo *outputInfo = malloc(sizeof(EncOutputInfo)); EncParam *encParam = malloc(sizeof(EncParam)); // Set allocated memory to zero memset (initialInfo, 0, sizeof (EncInitialInfo)); memset (encParam, 0, sizeof (EncParam)); memset (encOP, 0, sizeof (EncOpenParam)); memset (outputInfo, 0, sizeof (EncOutputInfo)); memset (&bit_stream_buf, 0, sizeof (vpu_mem_desc)); // Init VPU if(vpu_Init (NULL) < 0) { printf(">> failed to call vpu_Init()\n"); ret = -1; goto free; } vpu_GetVersionInfo(&ver); printf("VPU Version: firmware %d.%d.%d; libvpu: %d.%d.%d \n", ver.fw_major, ver.fw_minor, ver.fw_release, ver.lib_major, ver.lib_minor, ver.lib_release); // Allocate input buffer bit_stream_buf.size = BUFF_FILL_SIZE; IOGetPhyMem (&bit_stream_buf); if(bit_stream_buf.phy_addr == 0) { printf(">> error allocating encoder bitstream buffer\n"); ret = -1; goto free; } IOGetVirtMem (&bit_stream_buf); // Set up encoder operation parameters encOP->bitstreamBuffer = bit_stream_buf.phy_addr; encOP->bitstreamBufferSize = BUFF_FILL_SIZE; encOP->bitstreamFormat = STD_AVC; encOP->gopSize = DEFAULT_GOP_SIZE; encOP->rcIntraQp = VPU_DEFAULT_H264_QP; encOP->userQpMaxEnable = 1; encOP->userQpMax = H264_QP_MAX; encOP->userQpMinEnable = 1; encOP->userQpMin = H264_QP_MIN; encOP->frameRateInfo = DEFAULT_FRAME_RATE; encOP->picWidth = DEFAULT_WIDTH; encOP->picHeight = DEFAULT_HEIGHT; encOP->ringBufferEnable = 0; // Open encoder vpu_EncOpen (handle, encOP); // Configure IRAM memory SearchRamParam search_pa = { 0 }; iram_t iram; int ram_size; memset (&iram, 0, sizeof (iram_t)); ram_size = ((DEFAULT_WIDTH + 15) & ~15) * 36 + 2048; IOGetIramBase (&iram); if ((iram.end - iram.start) < ram_size) ram_size = iram.end - iram.start; search_pa.searchRamAddr = iram.start; search_pa.SearchRamSize = ram_size; vpu_EncGiveCommand (*handle, ENC_SET_SEARCHRAM_PARAM, &search_pa); vpu_EncGetInitialInfo (*handle, initialInfo); // Disable rotation/mirroring vpu_EncGiveCommand (*handle, DISABLE_ROTATION, 0); vpu_EncGiveCommand (*handle, DISABLE_MIRRORING, 0); // Allocate memory for source frame buffer source_buf.size = DEFAULT_WIDTH * DEFAULT_HEIGHT * 3/2; IOGetPhyMem (&source_buf); if(source_buf.phy_addr == 0) { printf(">> error allocating source frame buffer\n"); ret = -1; goto close; } IOGetVirtMem(&source_buf); source_frame.strideY = DEFAULT_WIDTH; source_frame.strideC = DEFAULT_WIDTH >> 1; source_frame.bufY = source_buf.phy_addr; source_frame.bufCb = source_frame.bufY + DEFAULT_WIDTH * DEFAULT_HEIGHT; source_frame.bufCr = source_frame.bufCb + (DEFAULT_WIDTH * DEFAULT_HEIGHT >> 2); printf("source frame buffer %i phys(%p) virt(%p)\n", 0, source_buf.phy_addr, source_buf.virt_uaddr); // Get number of destination frame buffers and allocate int num = initialInfo->minFrameBufferCount; printf("number of needed frame buffers: %d\n", num); vpu_mem_desc *framedesc = malloc(sizeof(vpu_mem_desc) * num); FrameBuffer *frame = malloc(sizeof(FrameBuffer) * num); memset (framedesc, 0, (sizeof (vpu_mem_desc) * num)); memset (frame, 0, (sizeof (FrameBuffer) * num)); // Allocate each destination frame buffer for (i = 0; i < num; i++) { framedesc[i].size = DEFAULT_WIDTH * DEFAULT_HEIGHT * 3/2; IOGetPhyMem (&(framedesc[i])); if (framedesc[i].phy_addr == 0) { printf(">> error allocating destination frame buffers\n"); ret = -1; goto frame; } IOGetVirtMem (&(framedesc[i])); frame[i].strideY = DEFAULT_WIDTH; frame[i].strideC = DEFAULT_WIDTH >> 1; frame[i].bufY = framedesc[i].phy_addr; frame[i].bufCb = frame[i].bufY + (DEFAULT_WIDTH * DEFAULT_HEIGHT); frame[i].bufCr = frame[i].bufCb + ((DEFAULT_WIDTH * DEFAULT_HEIGHT) >> 2); printf("destination frame buffer %i phys(%p) virt(%p)\n", i, framedesc[i].phy_addr, framedesc[i].virt_uaddr); } // Register allocated frame buffers vpu_EncRegisterFrameBuffer (*handle, frame, num, DEFAULT_WIDTH, DEFAULT_WIDTH, 0, 0, NULL); encParam->forceIPicture = 0; encParam->skipPicture = 0; encParam->enableAutoSkip = 0; encParam->quantParam = VPU_DEFAULT_H264_QP; // Get encoding Headers EncHeaderParam enchdr_param = { 0 }; uint8_t *ptr; uint8_t *header[NUM_INPUT_BUF]; uint32_t headersize[NUM_INPUT_BUF]; // size for each header element enchdr_param.headerType = SPS_RBSP; vpu_EncGiveCommand (*handle, ENC_PUT_AVC_HEADER, &enchdr_param); headersize[SPS_HDR] = enchdr_param.size; header[SPS_HDR] = malloc (enchdr_param.size); if (header[SPS_HDR] == NULL) { printf (">> error in allocating memory for SPS_RBSP Header" ); ret = -1; goto frame; } ptr = (uint8_t*) (bit_stream_buf.virt_uaddr + enchdr_param.buf - bit_stream_buf.phy_addr); memcpy (header[SPS_HDR], ptr, enchdr_param.size); printf("header[SPS_HDR]: %d bytes\n", headersize[SPS_HDR]); enchdr_param.headerType = PPS_RBSP; vpu_EncGiveCommand (*handle, ENC_PUT_AVC_HEADER, &enchdr_param); headersize[PPS_HDR] = enchdr_param.size; header[PPS_HDR] = malloc (enchdr_param.size); if (header[PPS_HDR] == NULL) { printf (">> error in allocating memory for PPS_HDR Header" ); ret = -1; goto frame; } ptr = (uint8_t*) (bit_stream_buf.virt_uaddr + enchdr_param.buf - bit_stream_buf.phy_addr); memcpy (header[PPS_HDR], ptr, enchdr_param.size); printf("header[PPS_HDR]: %d bytes\n", headersize[PPS_HDR]); // Open output file and write headers FILE *out = fopen ("BigBuckBunny_640x360_small.h264", "w"); //stdout fwrite(header[SPS_HDR], 1, headersize[SPS_HDR], out); fwrite(header[PPS_HDR], 1, headersize[PPS_HDR], out); // Copy and Encode FILE *in = fopen ("BigBuckBunny_640x360_small.yuv", "r"); while(fread((void*) source_buf.virt_uaddr, 1, DEFAULT_WIDTH * DEFAULT_HEIGHT * 3/2, in)) { encParam->sourceFrame = &source_frame; // Encode a single frame vpu_EncStartOneFrame (*handle, encParam); while(vpu_IsBusy()) { vpu_WaitForInt (100); } vpu_EncGetOutputInfo (*handle, outputInfo); // Write bitstream to file fwrite((void*) bit_stream_buf.virt_uaddr, 1, outputInfo->bitstreamSize, out); } printf("\n"); fclose(in); fclose(out); frame: for (i = 0; i < num; i++) { IOFreeVirtMem(&(framedesc[i])); IOFreePhyMem(&(framedesc[i])); } free(frame); free(framedesc); close: // Close encoder vpu_EncClose (*handle); // Free alocated input buffer IOFreeVirtMem (&bit_stream_buf); IOFreePhyMem (&bit_stream_buf); free: free(encOP); free(initialInfo); free(outputInfo); free(encParam); free(handle); return ret; }
mjpeg_decoder_t::mjpeg_decoder_t(vpu_t &vpu) : w_(0) , h_(0) , ystride_(0) , uvstride_(0) , imgSize_(0) , handle_(0) , phy_bsbuf_addr(0) , phy_ps_buf(0) , phy_slice_buf(0) , phy_slicebuf_size(0) , virt_bsbuf_addr(0) , bsbuf_end(0) , numRead(0) , fbcount(0) , fb(0) , pfbpool(0) , mvcol_memdesc(0) , startedDecode_(false) , app_fbs(0) , decoder_fbs(0) , state_(ERR) { vpu_mem_desc mem_desc = {0}; mem_desc.size = STREAM_BUF_SIZE; int ret = IOGetPhyMem(&mem_desc); if (ret) { fprintf(stderr,"Unable to obtain physical mem\n"); return; } if (IOGetVirtMem(&mem_desc) <= 0) { fprintf(stderr,"Unable to obtain virtual mem\n"); IOFreePhyMem(&mem_desc); return; } vpu_mem_desc slice_mem_desc = {0}; phy_bsbuf_addr = mem_desc.phy_addr; virt_bsbuf_addr = mem_desc.virt_uaddr; bsbuf_end = virt_bsbuf_addr + mem_desc.size ; DecOpenParam oparam ; memset(&oparam,0,sizeof(oparam)); oparam.bitstreamFormat = STD_MJPG ; oparam.bitstreamBuffer = phy_bsbuf_addr; oparam.bitstreamBufferSize = STREAM_BUF_SIZE; oparam.reorderEnable = 1 ; oparam.mp4DeblkEnable = 0 ; oparam.dynamicAllocEnable = 1 ; oparam.psSaveBuffer = 0 ; oparam.psSaveBufferSize = 0 ; ret = vpu_DecOpen(&handle_, &oparam); if (ret != RETCODE_SUCCESS) { fprintf(stderr,"vpu_DecOpen failed: %d\n", ret); return ; } debugPrint("vpu_DecOpen success\n" ); ret = vpu_DecGiveCommand(handle_,DEC_SET_REPORT_USERDATA, &userData); debugPrint("vpu_DecGiveCommand: %d(DEC_SET_REPORT_USERDATA)/%d\n", DEC_SET_REPORT_USERDATA,ret); if (ret != RETCODE_SUCCESS) { fprintf(stderr, "Failed to set user data report, ret %d\n", ret ); return ; } else printf( "disabled userdata\n" ); /* Parse bitstream and get width/height/framerate etc */ ret = vpu_DecSetEscSeqInit(handle_, 1); if (ret != RETCODE_SUCCESS) { fprintf(stderr, "Failed to set Esc Seq, ret %d\n", ret ); return ; } else debugPrint("vpu_DecSetEscSeqInit(1): %d\n", ret); state_ = INIT ; }