uint8_t Usart3Get(void){ uint8_t ch; //check if buffer is empty while (BufferIsEmpty(U3Rx) ==SUCCESS); BufferGet(&U3Rx, &ch); return ch; }
uint16_t BufferCount(Buffer_t* buffer) { if(BufferIsEmpty(buffer)) return 0;//Se il buffer è vuoto restituisco 0 if(BufferIsFull(buffer)) return buffer->size;//Se il buffer è pieno restituisco la dimensione del buffer if(buffer->firstElement < buffer->firstSpace) return buffer->firstSpace - buffer->firstElement;//Se il puntatore al primo elemento si trova prima del primo spazio libero restituisco la differenza tra i due else return buffer->size - (buffer->firstElement - buffer->firstSpace);//Se il puntatore al primo elemento di trova dopo il primo spazio libero restituisco la dimensione del buffer meno la differenza tra il puntatore al primo elemento e il primo spazio libero }
char BufferPop(Buffer_t* buffer) { if(BufferIsEmpty(buffer)) return 0;//Se il buffer è vuoto restituisco 0 if (buffer->firstSpace == buffer->array) buffer->firstSpace += buffer->size - 1;//Se il puntatore al primo spazio libero punta alla prima cella allocata allora lo sposto all'ultima cella allocata else buffer->firstSpace--;//In caso contrario sposto il puntatore al primo spazio libero indietro di una cella if(buffer->firstElement == buffer->firstSpace) buffer->firstElement = NULL;//Se i due puntatori finisco a puntare alla stessa cella come se il buffer fosse pieno reimposto il puntatore al primo elemento a null return *buffer->firstSpace;//Restituisco il valore }
uint8_t BufferPush(Buffer_t* buffer, char value) { if (BufferIsFull(buffer)) return 0;//Se il buffer è pieno restituisco false if (BufferIsEmpty(buffer)) buffer->firstElement = buffer->firstSpace;//Se il buffer è vuoto imposto il puntatore al primo elemento al primo spazio libero *buffer->firstSpace = value;//Imposto il valore nel primo spazio disponibile nel buffer buffer->firstSpace++;//Sposto il puntatore una cella in avanti if(buffer->array + buffer->size == buffer->firstSpace) buffer->firstSpace = buffer->array;//Se il puntatore sconfina in una zona al di fuori del buffer lo si reimposta alla prima cella allocata return 1;//Restituisco true se il valore è stato inserito correttamente }
char BufferPull(Buffer_t* buffer) { char value; if(BufferIsEmpty(buffer)) return 0;//Se il buffer è vuoto restituisco 0 value = *buffer->firstElement;//Copio il valore nella variabile che verrà restituita buffer->firstElement++;//Sposto il puntatore una cella in avanti if(buffer->array + buffer->size == buffer->firstElement) buffer->firstElement = buffer->array;//Se il puntatore sconfina in una zona al di fuori del buffer lo si reimposta alla prima cella allocata if(buffer->firstElement == buffer->firstSpace) buffer->firstElement = NULL;//Se i due puntatori finisco a puntare alla stessa cella come se il buffer fosse pieno reimposto il puntatore al primo elemento a null return value;//Restituisco il valore }
static void k_read(char *buff, int size) { int i=0; _Cli(); while( !BufferIsEmpty() && i< size ) { buff[i] = GetFromBuffer() ; i++; } _Sti(); }
uint8_t comm_get(void) { #ifdef BUFFERED uint8_t ch; //check if buffer is empty while (BufferIsEmpty(U1Rx) ==SUCCESS); BufferGet(&U1Rx, &ch); return ch; #else while ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); return (uint8_t)USART_ReceiveData(USART1); #endif /* while(USART_GetFlagStatus(g_usart, USART_FLAG_RXNE) == RESET) { ; } return (char)USART_ReceiveData(g_usart);*/ }
void SendNextByte() { //take next byte from the buffer and put it in UDR if (BufferIsEmpty()) { currentlyTransmitting = 0; } else if (BufferGetCount() == 1) { currentlyTransmitting = 1; UDR0 = Buffer.Buffer[Buffer.Start]; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { Buffer.Count--; } }
int Consume(struct Products* products) { int item; pthread_mutex_lock(&products->locker); /*为空时持续等待,无数据可读*/ while (BufferIsEmpty(products)) { pthread_cond_wait(&products->notEmpty, &products->locker); } /*提取数据*/ item = products->buffer[products->posReadFrom]; products->posReadFrom++; /*如果到末尾,从头读取*/ if (products->posReadFrom >= BUFFER_SIZE) products->posReadFrom = 0; pthread_cond_signal(&products->notFull); pthread_mutex_unlock(&products->locker); return item; }
/******************************************************************************* * @fn encodeProcess * @brief Encode an input video file and output encoded H.264 video file * @param[in] encodeHandle : Hanlde for the encoder * @param[out] outFile : output encoded H.264 video file * @param[in] oveContext : Hanlde to the encoder context * @param[in] deviceID : Device on which encoder context to be created * @param[in] pConfig : OvConfigCtrl * @return bool : true if successful; otherwise false. ******************************************************************************/ bool encodeProcess(OVEncodeHandle *encodeHandle, char *outFile, OPContextHandle oveContext, unsigned int deviceId, OvConfigCtrl *pConfig) { cl_int err; unsigned int numEventInWaitList = 0; OPMemHandle inputSurface; OVresult res = 0; // Initilizes encoder session & buffers // Create an OVE Session (Platform context, id, mode, profile, format, ...) // encode task priority. FOR POSSIBLY LOW LATENCY OVE_ENCODE_TASK_PRIORITY_LEVEL2 */ encodeHandle->session = OVEncodeCreateSession(oveContext, deviceId, pConfig->encodeMode, pConfig->profileLevel, pConfig->pictFormat, info->width, info->height, pConfig->priority); if (encodeHandle->session == NULL) { fprintf(stderr, "OVEncodeCreateSession failed.\n"); return false; } // Configure the encoding engine based upon the config file specifications res = setEncodeConfig(encodeHandle->session, pConfig); if (!res) { fprintf(stderr, "OVEncodeSendConfig returned error\n"); return false; } // Create a command queue encodeHandle->clCmdQueue = clCreateCommandQueue((cl_context)oveContext, clDeviceID, 0, &err); if(err != CL_SUCCESS) { fprintf(stderr, "Create command queue failed! Error :%d\n", err); return false; } for(int i = 0; i < MAX_INPUT_SURFACE; i++) { encodeHandle->inputSurfaces[i] = clCreateBuffer((cl_context)oveContext, CL_MEM_READ_WRITE, hostPtrSize, NULL, &err); if (err != CL_SUCCESS) { fprintf(stderr, "clCreateBuffer returned error %d\n", err); return false; } } // Output file handle FILE *fw = fopen(outFile, "wb"); if (fw == NULL) { printf("Error opening the output file %s\n", outFile); return false; } // Setup the picture parameters OVE_ENCODE_PARAMETERS_H264 pictureParameter; unsigned int numEncodeTaskInputBuffers = 1; OVE_INPUT_DESCRIPTION *encodeTaskInputBufferList = (OVE_INPUT_DESCRIPTION *) malloc(sizeof(OVE_INPUT_DESCRIPTION) * numEncodeTaskInputBuffers); OVE_OUTPUT_DESCRIPTION taskDescriptionList = {sizeof(OVE_OUTPUT_DESCRIPTION), 0, OVE_TASK_STATUS_NONE, 0, 0}; // Setup the picture parameters memset(&pictureParameter, 0, sizeof(OVE_ENCODE_PARAMETERS_H264)); pictureParameter.size = sizeof(OVE_ENCODE_PARAMETERS_H264); pictureParameter.flags.value = 0; pictureParameter.flags.flags.reserved = 0; pictureParameter.insertSPS = (OVE_BOOL)(currentFrame == 0); pictureParameter.pictureStructure = OVE_PICTURE_STRUCTURE_H264_FRAME; pictureParameter.forceRefreshMap = (OVE_BOOL)true; pictureParameter.forceIMBPeriod = 0; pictureParameter.forcePicType = OVE_PICTURE_TYPE_H264_NONE; // Go! for (currentFrame = 0; currentFrame < (unsigned)info->num_frames; currentFrame++) { if (GetAsyncKeyState(VK_F8)) break; while (BufferIsEmpty(frameBuffer)) Sleep(250); // only bad if encodes more than 1000fps inputSurface = encodeHandle->inputSurfaces[currentFrame % MAX_INPUT_SURFACE]; cl_int status; cl_event inMapEvt, unmapEvent; void* mapPtr = clEnqueueMapBuffer(encodeHandle->clCmdQueue, (cl_mem)inputSurface, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, hostPtrSize, 0, NULL, &inMapEvt, &status); clFlush(encodeHandle->clCmdQueue); waitForEvent(inMapEvt); clReleaseEvent(inMapEvt); //Read into the input surface buffer BufferType pBuf = 0; BufferRead(frameBuffer, &pBuf); memcpy((BYTE*)mapPtr, (BYTE*)pBuf, hostPtrSize); free(pBuf); clEnqueueUnmapMemObject(encodeHandle->clCmdQueue, (cl_mem)inputSurface, mapPtr, 0, NULL, &unmapEvent); clFlush(encodeHandle->clCmdQueue); waitForEvent(unmapEvent); clReleaseEvent(unmapEvent); // use the input surface buffer as our Picture encodeTaskInputBufferList[0].bufferType = OVE_BUFFER_TYPE_PICTURE; encodeTaskInputBufferList[0].buffer.pPicture = (OVE_SURFACE_HANDLE) inputSurface; // Encode a single picture. // calling VCE for frame encode // http://stackoverflow.com/questions/9618369/h-264-over-rtp-identify-sps-and-pps-frames // pictureParameter.insertSPS = (OVE_BOOL)(currentFrame == 0); OPEventHandle event; unsigned int iTaskID; res = OVEncodeTask(encodeHandle->session, numEncodeTaskInputBuffers, encodeTaskInputBufferList, &pictureParameter, &iTaskID, numEventInWaitList, NULL, &event); if (!res) { fprintf(stderr, "OVEncodeTask returned error %d\n", res); return false; } // Wait for Encode session completes err = clWaitForEvents(1, (cl_event*)&(event)); if (err != CL_SUCCESS) { fprintf(stderr, "clWaitForEvents returned error %d\n", err); return false; } // Query output unsigned int numTaskDescriptionsRequested = 1; unsigned int numTaskDescriptionsReturned = 0; //do //{ res = OVEncodeQueryTaskDescription(encodeHandle->session, numTaskDescriptionsRequested, &numTaskDescriptionsReturned, &taskDescriptionList); if (!res) { fprintf(stderr, "OVEncodeQueryTaskDescription returned error %d\n", err); return false; } //} //while (taskDescriptionList.status == OVE_TASK_STATUS_NONE); #ifdef DEBUG if (numTaskDescriptionsReturned > 1) fprintf(stderr, "Warning: numTaskDescriptions returned: %d\n", numTaskDescriptionsReturned); if (taskDescriptionList.status != OVE_TASK_STATUS_COMPLETE) fprintf(stderr, "Warning: taskDescriptionList.status returned: %d\n", taskDescriptionList.status); #endif // Write compressed frame to the output file if (taskDescriptionList.status == OVE_TASK_STATUS_COMPLETE && taskDescriptionList.size_of_bitstream_data > 0) { // Write output data fwrite(taskDescriptionList.bitstream_data, 1, taskDescriptionList.size_of_bitstream_data, fw); OVEncodeReleaseTask(encodeHandle->session, taskDescriptionList.taskID); } if (event) clReleaseEvent((cl_event) event); } // Free memory resources fclose(fw); free(encodeTaskInputBufferList); return true; }