void mjpeg_decoder_t::startDecode(void)
{
	if( !startedDecode_&& (DECODING == state_) ){
		unsigned idx = ffs(decoder_fbs);
		if (0 < idx) {
			idx-- ;
#if 1
			RetCode ret = vpu_DecGiveCommand(handle_, SET_ROTATOR_OUTPUT, (void *)&fb[idx]);
debugPrint("vpu_DecGiveCommand: %d(SET_ROTATOR_OUTPUT)/%d, fb0 (%p)\n", SET_ROTATOR_OUTPUT,ret, &fb[idx]);
			if (ret != RETCODE_SUCCESS) {
				fprintf(stderr,"rotout %d\n", ret);
				return ;
			}
#endif
			DecParam decparam = {0};
			decparam.chunkSize = numRead ;
			numRead = 0 ;
			decparam.picStartByteOffset = 0 ;
			decparam.picStreamBufferAddr = phy_bsbuf_addr ;
			ret = vpu_DecStartOneFrame(handle_, &decparam);
					debugPrint("vpu_DecStartOneFrame: %d: %d,%d,%d,%d,%d,%d,%d,%d,%lx\n",
						ret,
						decparam.prescanEnable,
						decparam.prescanMode,
						decparam.dispReorderBuf,
						decparam.iframeSearchEnable,
						decparam.skipframeMode,
						decparam.skipframeNum,
						decparam.chunkSize,
						decparam.picStartByteOffset,
						decparam.picStreamBufferAddr
					);
			if (ret == RETCODE_SUCCESS) {
				startedDecode_ = true ;
			} else {
				fprintf(stderr, "DecStartOneFrame failed: %d\n", ret);
			}
		} else {
			debugPrint( "%s: no fbs available\n", __func__ );
		}
	}
	else
		debugPrint( "%s: not ready\n", __func__ );
}
Esempio n. 2
0
static int decoder_renderer_submit_decode_unit(PDECODE_UNIT decodeUnit) {
  Uint32 space;
  PhysicalAddress pa_read_ptr, pa_write_ptr;
  if (vpu_DecGetBitstreamBuffer(handle, &pa_read_ptr, &pa_write_ptr, &space) != RETCODE_SUCCESS) {
    fprintf(stderr, "Can't get video decoder buffer\n");
    exit(EXIT_FAILURE);
  }
  Uint32 target_addr = mem_desc.virt_uaddr + (pa_write_ptr - mem_desc.phy_addr);

  if (space < decodeUnit->fullLength) {
    fprintf(stderr, "Not enough space in buffer %d/%d\n", decodeUnit->fullLength, space);
  }

  PLENTRY entry = decodeUnit->bufferList;
  int written = 0;
  while (entry != NULL) {
    if ( (target_addr + entry->length) > mem_desc.virt_uaddr + STREAM_BUF_SIZE) {
      int room = mem_desc.virt_uaddr + STREAM_BUF_SIZE - target_addr;
      memcpy((void *)target_addr, entry->data, room);
      memcpy((void *)mem_desc.virt_uaddr, entry->data + room, entry->length - room);
      target_addr = mem_desc.virt_uaddr + entry->length - room;
    } else {
      memcpy((void *)target_addr, entry->data, entry->length);
      target_addr += entry->length;
    }

    entry = entry->next;
  }
  vpu_DecUpdateBitstreamBuffer(handle, decodeUnit->fullLength);

  if (!initialized) {
    initialized = true;
    DecInitialInfo info = {0};
    vpu_DecSetEscSeqInit(handle, 1);
    vpu_DecGetInitialInfo(handle, &info);
    vpu_DecSetEscSeqInit(handle, 0);
    if (vpu_DecRegisterFrameBuffer(handle, fb, regfbcount, stride, &bufinfo) != RETCODE_SUCCESS) {
      fprintf(stderr, "Can't register decoder to framebuffer\n");
      exit(EXIT_FAILURE);
    }
  }

  if (!decoding) {
    if (vpu_DecStartOneFrame(handle, &decparam) != RETCODE_SUCCESS) {
      fprintf(stderr, "Can't start decoding\n");
      exit(EXIT_FAILURE);
    }
    decoding = true;
  }

  int loop_id = 0;
  while (vpu_IsBusy()) {
    if (loop_id > 50) {
      vpu_SWReset(handle, 0);
      fprintf(stderr, "VPU is too long busy\n");
      exit(EXIT_FAILURE);
    }
    vpu_WaitForInt(100);
    loop_id++;
  }

  if (decoding) {
    decoding = 0;

    DecOutputInfo outinfo = {0};
    if (vpu_DecGetOutputInfo(handle, &outinfo) != RETCODE_SUCCESS) {
      fprintf(stderr, "Can't get output info\n");
      exit(EXIT_FAILURE);
    }

    if (outinfo.decodingSuccess & 0x10) {
      return DR_OK;
    } else if (outinfo.notSufficientPsBuffer) {
      fprintf(stderr, "Not enough space in stream buffer\n");
      exit(EXIT_FAILURE);
    } else if (outinfo.notSufficientSliceBuffer) {
      fprintf(stderr, "Not enough space in slice buffer\n");
      exit(EXIT_FAILURE);
    }

    if (outinfo.indexFrameDisplay >= 0) {
      struct timeval tv;
      gettimeofday(&tv, 0);
      dbuf.timestamp.tv_sec = tv.tv_sec;
      dbuf.timestamp.tv_usec = tv.tv_usec;
      dbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
      dbuf.memory = V4L2_MEMORY_MMAP;

      dbuf.index = outinfo.indexFrameDisplay;
      if (ioctl(fd, VIDIOC_QUERYBUF, &dbuf) < 0) {
        fprintf(stderr, "Can't get output buffer\n");
        exit(EXIT_FAILURE);
      }

      dbuf.index = outinfo.indexFrameDisplay;
      dbuf.field =  V4L2_FIELD_NONE;
      if (ioctl(fd, VIDIOC_QBUF, &dbuf) < 0) {
        fprintf(stderr, "Can't get output buffer\n");
        exit(EXIT_FAILURE);
      }

      if (!displaying) {
        int type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
        if (ioctl(fd, VIDIOC_STREAMON, &type) < 0) {
          fprintf(stderr, "Failed to output video\n");
          exit(EXIT_FAILURE);
        }
        displaying = true;
      }

      queued_count++;

      if (queued_count > THRESHOLD) {
        dbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
        dbuf.memory = V4L2_MEMORY_MMAP;
        if (ioctl(fd, VIDIOC_DQBUF, &dbuf) < 0) {
          fprintf(stderr, "Failed to dequeue buffer\n");
          exit(EXIT_FAILURE);
        } else
          queued_count--;
      }

      if (disp_clr_index >= 0)
        vpu_DecClrDispFlag(handle, disp_clr_index);
      
      disp_clr_index = outinfo.indexFrameDisplay;
    } else if (outinfo.indexFrameDisplay == -1) {
      fprintf(stderr, "Failed to decode frame\n");
      exit(EXIT_FAILURE);
    }
  }

  return DR_OK;
}
Esempio n. 3
0
int VpuDec::decode(VencBlk * src, ImgBlk * dest)//blocking
{	
	int ret = 1; 
	int loop_id = 0;
	int imageSize;
	DecOutputInfo outinfo = {0};
	DecParam decparam = {0};

	if(dec_fb == NULL || dec_pfbpool == NULL){
		if (write_Src_Ringbuf(src) == -1 || get_DecInitialInfo(src) == -1 || calloc_FrameBuff(src) == -1){
			printf("Config VPU error:VencBlk codec 0x%x, %dx%d, frameSize %d, frameType %d\n",src->codec,src->width,src->height,src->frameSize,src->frameType);			
			return -1;
		}
		decparam.dispReorderBuf = 0;
		decparam.skipframeMode = 0;
		decparam.skipframeNum = 0;
		decparam.iframeSearchEnable = 0;

		if(dec_format == STD_MJPG)
			vpu_DecGiveCommand(handle, SET_ROTATOR_STRIDE, &src->width);
		
	}else{
		ret = write_Src_Ringbuf(src);
			if (ret < 0) {
				printf("read data failed\n");
				return -1;
			}

	}
	if(dec_format == STD_MJPG){
			decparam.chunkSize = src->frameSize;
			decparam.phyJpgChunkBase = Dec_bufZone.pStartAddr;
			decparam.virtJpgChunkBase = (unsigned char*)Dec_bufZone.vStartaddr;
			vpu_DecGiveCommand(handle, SET_ROTATOR_OUTPUT,(void *)&dec_fb[0]);
	}

	imageSize = src->width * src->height * 3 / 2;
	
	ret = vpu_DecStartOneFrame(handle, &decparam);
	if (ret != RETCODE_SUCCESS) {
			printf("DecStartOneFrame failed, ret=%d,LINE:%d\n", ret,__LINE__);
			return -1;
	}

			loop_id = 0;

	while (vpu_IsBusy()) {
			
			if (loop_id == 50) {
				ret = vpu_SWReset(handle, 0);
				return -1;
			}

			vpu_WaitForInt(100);
			loop_id ++;
	}

	ret = vpu_DecGetOutputInfo(handle, &outinfo);
	if (ret != RETCODE_SUCCESS) {
			printf("vpu_DecGetOutputInfo failed Err code is %d\n"
				"\tframe_id = %d\n", ret, (int)frame_id);
			return -1;
		}
	
	if (cpu_is_mx6x() && (outinfo.decodingSuccess & 0x10)) {
			printf("vpu needs more bitstream in rollback mode\n"
				"\tframe_id = %d\n", (int)frame_id);

			return -1;
	}

	if (outinfo.notSufficientPsBuffer) {
		printf("PS Buffer overflow,LINE:%d\n",__LINE__);
		return -1;
	}

	if (outinfo.notSufficientSliceBuffer) {
		printf("Slice Buffer overflow,LINE:%d\n",__LINE__);
		return -1;
	}

	if (outinfo.indexFrameDisplay == -1){
			printf("VPU has no more display data,LINE:%d\n",__LINE__);
			return -1;
	}

	if ((outinfo.indexFrameDisplay == -3) ||(outinfo.indexFrameDisplay == -2)) {
		printf("VPU doesn't have picture to be displayed.\n"
				"\toutinfo.indexFrameDisplay = %d\n",
					outinfo.indexFrameDisplay);

			if (disp_clr_index >= 0) {
				ret = vpu_DecClrDispFlag(handle, disp_clr_index);
				if (ret)
					printf("vpu_DecClrDispFlag failed Error code"
								" %d\n", ret);
			}
			disp_clr_index = outinfo.indexFrameDisplay;		
		}
		
	dis_index = outinfo.indexFrameDisplay;
	dest->vaddr = (void *)(dec_pfbpool[dis_index]->addrY + (dec_pfbpool[dis_index]->desc.virt_uaddr- dec_pfbpool[dis_index]->desc.phy_addr)); 
	dest->paddr = dec_pfbpool[dis_index]->desc.phy_addr; //physical address
	dest->size = imageSize;
	dest->height = src->height;
	dest->width = src->width;
	dest->fourcc = PIX_FMT_YUV420P;//src->codec;
	dest->pts = src->pts;

	//printf("dis_index:%d,frame_id:%d,disp_clr_index:%d\n",dis_index,frame_id,disp_clr_index);

	if(dec_format == STD_AVC){
		if (disp_clr_index >= 0) {
			ret = vpu_DecClrDispFlag(handle,disp_clr_index);
			if (ret)
				printf("vpu_DecClrDispFlag failed Error code%d,LINE:%d\n", ret,__LINE__);
		}
	}
	
	disp_clr_index = outinfo.indexFrameDisplay;
	frame_id++;
	return 1;

}
Esempio n. 4
0
static int decoder_renderer_submit_decode_unit(PDECODE_UNIT decodeUnit) {
  PLENTRY entry = decodeUnit->bufferList;
  while (entry != NULL) {
    Uint32 space;
    PhysicalAddress pa_read_ptr, pa_write_ptr;
    if (vpu_DecGetBitstreamBuffer(handle, &pa_read_ptr, &pa_write_ptr, &space) != RETCODE_SUCCESS) {
      fprintf(stderr, "Can't get video decoder buffer\n");
      exit(EXIT_FAILURE);
    }

    Uint32 target_addr = mem_desc.virt_uaddr + (pa_write_ptr - mem_desc.phy_addr);

    if ( (target_addr + entry->length) > mem_desc.virt_uaddr + STREAM_BUF_SIZE) {
      int room = mem_desc.virt_uaddr + STREAM_BUF_SIZE - target_addr;
      memcpy((void *)target_addr, entry->data, room);
      memcpy((void *)mem_desc.virt_uaddr, entry->data + room, entry->length - room);
    } else {
      memcpy((void *)target_addr, entry->data, entry->length);
    }
    vpu_DecUpdateBitstreamBuffer(handle, entry->length);
  }

  if (!initialized) {
    initialized = true;
    
    vpu_DecSetEscSeqInit(handle, 1);
    DecInitialInfo initinfo = {0};
    if (vpu_DecGetInitialInfo(handle, &initinfo) != RETCODE_SUCCESS) {
      fprintf(stderr, "Can't get initial info\n");
      exit(EXIT_FAILURE);
    }
    vpu_DecSetEscSeqInit(handle, 0);
    
    int regfbcount = initinfo.minFrameBufferCount + 2;
    threshold = regfbcount - initinfo.minFrameBufferCount;
    int picWidth = ((initinfo.picWidth + 15) & ~15);
    int picHeight = ((initinfo.picHeight + 15) & ~15);
    int stride = picWidth;

    int phy_slicebuf_size = initinfo.worstSliceSize * 1024;
    
    slice_mem_desc.size = phy_slicebuf_size;
    if (IOGetPhyMem(&slice_mem_desc)){
      fprintf(stderr, "Can't get slice physical address\n");
      exit(EXIT_FAILURE);
    }

    fb = calloc(regfbcount, sizeof(FrameBuffer));
    if (fb == NULL) {
      fprintf(stderr, "Can't allocate framebuffers\n");
      exit(EXIT_FAILURE);
    }

    char v4l_device[16], node[8];
    sprintf(node, "%d", 17);
    strcpy(v4l_device, "/dev/video");
    strcat(v4l_device, node);
    fd = open(v4l_device, O_RDWR, 0);
    struct v4l2_format fmt = {0};
    fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    fmt.fmt.pix.width = picWidth;
    fmt.fmt.pix.height = picHeight;
    fmt.fmt.pix.bytesperline = picWidth;
    fmt.fmt.pix.field = V4L2_FIELD_ANY;
    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
    if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0) {
      fprintf(stderr, "Can't set source video format\n");
      exit(EXIT_FAILURE);
    }

    if (ioctl(fd, VIDIOC_G_FMT, &fmt) < 0) {
      fprintf(stderr, "Can't set output video format\n");
      exit(EXIT_FAILURE);
    }

    struct v4l2_requestbuffers reqbuf = {0};
    reqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    reqbuf.memory = V4L2_MEMORY_MMAP;
    reqbuf.count = regfbcount;

    struct v4l_buf* buffers[regfbcount];

    if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
      fprintf(stderr, "Can't get video buffers\n");
      exit(EXIT_FAILURE);
    }

    if (reqbuf.count < regfbcount) {
      fprintf(stderr, "Not enough video buffers\n");
      exit(EXIT_FAILURE);
    }
    
    for (int i = 0; i < regfbcount; i++) {
      struct v4l2_buffer buffer = {0};
      struct v4l_buf *buf;

      buf = calloc(1, sizeof(struct v4l_buf));
      if (buf == NULL) {
        return -9;
      }

      buffers[i] = buf;

      buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
      buffer.memory = V4L2_MEMORY_MMAP;
      buffer.index = i;

      if (ioctl(fd, VIDIOC_QUERYBUF, &buffer) < 0) {
        fprintf(stderr, "Can't get video buffer\n");
        exit(EXIT_FAILURE);
      }
      buf->start = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buffer.m.offset);

      /*
      * Workaround for new V4L interface change, this change
      * will be removed after V4L driver is updated for this.
      * Need to call QUERYBUF ioctl again after mmap.
      */
      if (ioctl(fd, VIDIOC_QUERYBUF, &buffer) < 0) {
        fprintf(stderr, "Can't set source video format\n");
        exit(EXIT_FAILURE);
      }

      buf->offset = buffer.m.offset;
      buf->length = buffer.length;

      if (buf->start == MAP_FAILED) {
        fprintf(stderr, "Failed to map video buffer\n");
        exit(EXIT_FAILURE);
      }
    }
    
    int img_size = stride * picHeight;
    vpu_mem_desc *mvcol_md = NULL;
    
    int mjpg_fmt = MODE420;
    int divX = (mjpg_fmt == MODE420 || mjpg_fmt == MODE422) ? 2 : 1;
    int divY = (mjpg_fmt == MODE420 || mjpg_fmt == MODE224) ? 2 : 1;

    mvcol_md = calloc(regfbcount, sizeof(vpu_mem_desc));

    for (int i = 0; i < regfbcount; i++) {
      fb[i].myIndex = i;
      fb[i].bufY = buffers[i]->offset;
      fb[i].bufCb = fb[i].bufY + img_size;
      fb[i].bufCr = fb[i].bufCb + (img_size / divX / divY);

      /* allocate MvCol buffer here */
      memset(&mvcol_md[i], 0, sizeof(vpu_mem_desc));
      mvcol_md[i].size = img_size / divX / divY;
      if (IOGetPhyMem(&mvcol_md[i])) {
        fprintf(stderr, "Can't get physical address of colomn buffer\n");
        exit(EXIT_FAILURE);
      }
      fb[i].bufMvCol = mvcol_md[i].phy_addr;
    }
    
    DecBufInfo bufinfo;
    bufinfo.avcSliceBufInfo.bufferBase = slice_mem_desc.phy_addr;
    bufinfo.avcSliceBufInfo.bufferSize = phy_slicebuf_size;
    
    bufinfo.maxDecFrmInfo.maxMbX = stride / 16;
    bufinfo.maxDecFrmInfo.maxMbY = picHeight / 16;
    bufinfo.maxDecFrmInfo.maxMbNum = stride * picHeight / 256;
    
    int delay = -1;
    vpu_DecGiveCommand(handle, DEC_SET_FRAME_DELAY, &delay);

    if (vpu_DecRegisterFrameBuffer(handle, fb, regfbcount, stride, &bufinfo) != RETCODE_SUCCESS) {
      fprintf(stderr, "Can't register decoder to framebuffer\n");
      exit(EXIT_FAILURE);
    }
    
    if (!decoding) {
      if (vpu_DecStartOneFrame(handle, &decparam) != RETCODE_SUCCESS) {
        fprintf(stderr, "Can't start decoding\n");
        exit(EXIT_FAILURE);
      }
      decoding = true;
    }
  }
  
  int loop_id = 0;
  while (vpu_IsBusy()) {
    if (loop_id > 50) {
      vpu_SWReset(handle, 0);
      fprintf(stderr, "VPU is too long busy\n");
      exit(EXIT_FAILURE);
    }
    vpu_WaitForInt(100);
    loop_id++;
  }
  loop_id = 0;

  if (decoding) {
    decoding = 0;

    DecOutputInfo outinfo = {0};
    if (vpu_DecGetOutputInfo(handle, &outinfo) != RETCODE_SUCCESS) {
      fprintf(stderr, "Can't get output info\n");
      exit(EXIT_FAILURE);
    }

    if (outinfo.decodingSuccess & 0x10) {
      return 0;
    } else if (outinfo.notSufficientPsBuffer) {
      fprintf(stderr, "Not enough space in stream buffer\n");
      exit(EXIT_FAILURE);
    } else if (outinfo.notSufficientSliceBuffer) {
      fprintf(stderr, "Not enough space in slice buffer\n");
      exit(EXIT_FAILURE);
    }

    if (outinfo.indexFrameDisplay >= 0) {
      struct timeval tv;
      gettimeofday(&tv, 0);
      dbuf.timestamp.tv_sec = tv.tv_sec;
      dbuf.timestamp.tv_usec = tv.tv_usec;
      dbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
      dbuf.memory = V4L2_MEMORY_MMAP;

      dbuf.index = outinfo.indexFrameDisplay;
      if (ioctl(fd, VIDIOC_QUERYBUF, &dbuf) < 0) {
        fprintf(stderr, "Can't get output buffer\n");
        exit(EXIT_FAILURE);
      }

      dbuf.index = outinfo.indexFrameDisplay;
      dbuf.field =  V4L2_FIELD_NONE;
      if (ioctl(fd, VIDIOC_QBUF, &dbuf) < 0) {
        fprintf(stderr, "Can't get output buffer\n");
        exit(EXIT_FAILURE);
      }

      if (!displaying) {
        int type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
        if (ioctl(fd, VIDIOC_STREAMON, &type) < 0) {
          fprintf(stderr, "Failed to output video\n");
          exit(EXIT_FAILURE);
        }
        displaying = true;
      }

      queued_count++;

      if (queued_count > threshold) {
        dbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
        dbuf.memory = V4L2_MEMORY_MMAP;
        if (ioctl(fd, VIDIOC_DQBUF, &dbuf) < 0) {
          fprintf(stderr, "Failed to dequeue buffer\n");
          exit(EXIT_FAILURE);
        } else
          queued_count--;
      } else
        dbuf.index = -1;
      
      if (disp_clr_index >= 0)
        vpu_DecClrDispFlag(handle, disp_clr_index);
      
      disp_clr_index = outinfo.indexFrameDisplay;
    } else if (outinfo.indexFrameDisplay == -1) {
      fprintf(stderr, "Failed to decode frame\n");
      exit(EXIT_FAILURE);
    }
  }

  return DR_OK;
}