Code stream circular buffer management method with verification and exception handling
Through the introduction of verification and abnormal processing mechanisms in the management and abnormal processing mechanism in the management of the code flow circulation buffer, dynamically adjust the pointer and repair the error data, solve the problem that is not flexible in the existing technology, and achieve efficient and real -time data transmission.
Patent Information
- Application Number
- CN202411526762.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-30
- Publication Date
- 2025-01-21
AI Technical Summary
The existing code stream buffer management technology lacks a flexible and effective exception handling mechanism when facing data transmission errors, resulting in a waste of transmission delay and bandwidth, which makes it difficult to meet real-time requirements.
Adopt a code stream circular buffer management method with verification and exception handling, and introduce dynamic adjustment of temporary read pointers, skipping or repairing of erroneous data and other exception handling methods to ensure that the system can quickly return to normal when data transmission errors occur. data flow.
Under the premise of ensuring data integrity and real -time, the system's fault -tolerant capacity and transmission efficiency are improved, and it is suitable for big data, image processing and video transmission.
Smart Images

Figure CN119342243A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of code stream transmission control and relates to a code stream circular buffer management method with verification and exception processing. Background Art
[0002] In today's modern communication systems, with the rapid development of applications such as images, videos, and big data, the reliability and real-time requirements of data transmission are increasing. As an indispensable intermediate link in the data transmission process, the stream buffer is responsible for caching and scheduling data, ensuring the smooth transmission of data streams between devices or processes. Especially in big data and multimedia applications, the stream buffer plays a key role. However, with the continuous increase in data transmission volume and the increasingly complex transmission environment, data may face various challenges during transmission, such as loss, damage, delay, etc., especially when it comes to the transmission of data such as images and videos, data integrity becomes crucial. Therefore, how to efficiently manage the stream buffer and promptly detect and handle anomalies during data transmission has become one of the research focuses in the current technology field.
[0003] Existing stream buffer management technologies usually use a simple FIFO (first in, first out) structure or a ring buffer structure to ensure that data is read in the order in which it is written. However, in these traditional buffer management methods, if errors or packet loss occur during data transmission, the system often lacks effective countermeasures and can only choose to discard erroneous data or interrupt transmission, which not only reduces transmission efficiency but also has a serious impact on data integrity. This processing method is often difficult to meet actual needs in application scenarios with high real-time requirements, especially in image and video transmission scenarios. Because once data transmission is interrupted, the entire video or image stream may be interrupted, affecting user experience or the normal operation of the system.
[0004] Although some existing stream buffer management technologies have introduced verification mechanisms, such as CRC (cyclic redundancy check) or MD5 algorithms to verify data integrity, data integrity verification through checksums can, to a certain extent, detect whether the data has been damaged or tampered with during transmission. However, these technologies usually lack flexible and effective exception handling mechanisms when facing transmission errors. When the system detects a verification error, it can usually only repair the erroneous data by triggering a retransmission operation. Although retransmission can ensure the correctness of the data, its cost is high and often causes problems such as transmission delays and bandwidth waste. Especially in scenarios with very high real-time requirements, such as video conferencing, online games, real-time monitoring, etc., the retransmission mechanism is difficult to meet the low latency requirements of the system.
[0005] Therefore, the present invention studies and develops a more efficient code stream buffer management method with flexible exception handling function. By introducing a variety of exception handling means, such as dynamic adjustment of temporary read pointer, skipping or repairing of erroneous data, etc., it can not only effectively manage a large amount of data transmission, but also has a more intelligent exception handling mechanism; thereby ensuring that even in the case of data transmission errors, the system can quickly restore the normal data flow; while ensuring the system transmission efficiency, the integrity and real-time performance of the data are ensured. It can be applied to communication systems in the fields of big data, image processing, and video streaming transmission, coordinate the processing flow between the upper and lower layers or the front and back ends, and improve the operation efficiency. Summary of the invention
[0006] In view of the technical problems existing in the background technology, the present invention proposes a code stream circular buffer management method with verification and exception processing.
[0007] The present invention specifically comprises the following steps: Step 1: Create shared memory: Step 1.1, apply to the Linux kernel for the memory space shared by the upper-layer program and the lower-layer program, that is, apply for shared memory, and obtain the unique identification number ID of the shared memory segment. The shared memory includes the circular buffer CycleBuffer and the shared structure SharedStruct. The circular buffer CycleBuffer is used to store image data, and the shared structure SharedStruct is used to store the shared variables that control the circular buffer and the interaction between the upper and lower-layer programs.
[0008] The size of the circular buffer is a value agreed upon in advance by the upper-layer program and the lower-layer program. The size is set according to the specific needs of the application to ensure that there is enough storage space when data is transmitted at a high frequency. The shared variables contained in the shared structure SharedStruct include the write pointer index WriteIndex, the read pointer index ReadIndex, the head pointer index HeadIndex, the tail pointer index TailIndex and the flag Create indicating whether the buffer has been created.
[0009] Step 1.2, the upper-layer program / lower-layer program calls the shared memory function to map the shared memory to the upper-layer program / lower-layer program based on the unique identification number ID, allocates the shared memory, distinguishes the circular buffer and the shared structure SharedStruct, and completes the creation of the circular buffer CycleBuffer.
[0010] Step 1.3 calls the memory block initialization function to clear the memory segment, initializes the shared structure SharedStruct, assigns the write pointer index WriteIndex to 0, the read pointer index ReadIndex to 0, the head pointer index HeadIndex to 0, the tail pointer index TailIndex to CycleBufferSize, and assigns the buffer creation flag Create to 1.
[0011] Step 2: After the upper-layer program encrypts the acquired image, it writes the image frame into the CycleBuffer, specifically: Step 2.1, collect a frame of image data, preprocess it, and read the preprocessed image data into the temporary buffer Buffer_upper of the upper program.
[0012] Step 2.2: Initialize the image header structure PicHeadStruct and encrypt the image based on the image data in the temporary buffer Buffer_upper of the upper-layer program. The image header structure PicHeadStruct stores basic information of the image.
[0013] Step 2.3: Write the initialized image header structure PicHeadStruct and the image data in the temporary buffer Buffer_upper of the upper-layer program into the circular buffer CycleBuffer as image frames.
[0014] Step 3: The lower-level program reads the image frame in the CycleBuffer: Step 3.1, read a certain image frame written in the cycle buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program. The specific steps of reading are as follows: The lower-level program calls the temporary read pointer index TempReadIndex, sets the temporary read pointer index TempReadIndex equal to the read pointer index ReadIndex, and the temporary readable byte number TempReadableLen equal to the readable byte number ReadableLen.
[0015] Calculate the number of readable bytes ReadableLen according to the position of the read / write pointer: If the write pointer index WriteIndex is greater than or equal to the read pointer index ReadIndex, calculate the number of readable bytes ReadableLen = WriteIndex -ReadIndex; when the write pointer index WriteIndex is less than the read pointer index ReadIndex, the number of bytes that can be read is calculated as ReadableLen = CycleBufferSize – (ReadIndex – WriteIndex).
[0016] If the temporary readable byte number TempReadableLen is greater than or equal to the number of bytes occupied by the image header structure PicHeadStruct, it is determined that there is an image frame that can be read; if the temporary readable byte number TempReadableLen is less than the number of bytes occupied by the image header structure PicHeadStruct, it is determined that there is no image frame that can be read.
[0017] The specific steps for reading image frames are as follows: Read the image header structure PicHeadStruct: The number of bytes between the temporary read pointer and the tail pointer k_lower = TailIndex – TempReadIndex is calculated through the tail pointer index TailIndex and the temporary read pointer index TempReadIndex, that is, the size of the memory space on the right side of the temporary read pointer is calculated.
[0018] If k_lower >= sizeof(PicHeadStruct), the memory copy function is called to read the image header structure PicHeadStruct with a byte number of sizeof(PicHeadStruct) from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program at one time, and update the temporary read pointer index value: TempReadIndex = TempReadIndex + sizeof(PicHeadStruct).
[0019] If k_lower < sizeof(PicHeadStruct), split the data with the number of bytes of sizeof(PicHeadStruct) into two parts with the number of bytes of k_lower and sizeof(PicHeadStruct) – k_lower respectively, call the memory copy function (memcpy function) twice, read the data from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program twice, and update the temporary read pointer index value: TempReadIndex = sizeof(PicHeadStruct) - k_lower.
[0020] By reading the image header structure PicHeadStruct of the image frame to be read, the image information of the image frame to be read is obtained. The encryption method agreed in advance with the upper and lower layer programs is used for verification. If the verification is correct, the image data in the image frame is obtained, and the image data is post-processed according to the needs of the actual project.
[0021] If the check is wrong, perform exception handling. The specific steps of exception handling are as follows: The temporary read pointer index is incremented by one: TempReadIndex = TempReadIndex + 1; the temporary read pointer index TempReadIndex is modulo CycleBufferSize and assigned to itself to avoid out-of-bounds situations; the temporary readable byte number TempReadableLen is decremented by one: TempReadableLen = TempReadableLen - 1.
[0022] If the temporary readable byte number TempReadableLen after decrementing by one is greater than or equal to the number of bytes occupied by the image header structure PicHeadStruct, it means that there is enough space, and the image frame reading operation is performed again; if the temporary readable byte number TempReadableLen is less than the number of bytes occupied by the image header structure PicHeadStruct, it means that there is insufficient readable space, and the system program completely runs away, and the camera system is automatically restarted.
[0023] Step 3.2: Update the read pointer index ReadIndex: ReadIndex = TempReadIndex, and read the next image frame.
[0024] Furthermore, the specific steps for applying for shared memory described in step 1 are as follows: Step B1, set the project engineering path Path, depending on the project engineering path Path, call the format conversion function (ftok function) to obtain the unique key value key used for inter-process communication.
[0025] Step B2, the size of the circular buffer CycleBufferSize and the number of bytes occupied by the shared structure SharedStruct are added together to calculate the number of bytes of shared memory SharedMemorySize that needs to be applied for: SharedMemorySize = CycleBufferSize + sizeof(SharedStruct).
[0026] Among them, the sizeof keyword is used to calculate the number of bytes occupied by the shared structure SharedStruct.
[0027] Step B3, using the key value key obtained in step B1 and the number of bytes SharedMemorySize required to apply for shared memory obtained in step B2, calling the shared memory function to apply for shared memory from the Linux kernel, and obtaining the unique identification number ID of the shared memory.
[0028] Furthermore, the preprocessing described in step 2.1 is: grayscale processing is performed on each frame of the acquired image, the three-channel RGB image is converted into a single-channel grayscale image, and then the grayscale image data is compressed using the H.264 encoding standard. Correspondingly, the H.264 standard is used to decode the read image data when reading the image frame.
[0029] Furthermore, the method for encrypting the image described in step 2.2 is: perform an exclusive OR (XOR) operation on each byte of the image data, combine it with a key value or key sequence agreed upon in advance by the upper and lower layer programs, calculate and generate a check code and store it in the image header structure. The corresponding verification method described in step 3.1 is: the same as the encryption operation method, use the XOR operation, combine the key value or key sequence, and recalculate the check code. Determine whether the check code is the same as the check code in the image header structure.
[0030] Furthermore, the specific steps of writing the image frame into the circular buffer in step 2.3 are as follows: The number of bytes to be written to the image frame DataLen_upper = sizeof (PicHeadStruct) + PicHeadStruct. PicSize.
[0031] Among them, the sizeof keyword is used to calculate the number of bytes occupied by the image header structure PicHeadStruct, and PicHeadStruct. PicSize is the variable PicSize in the image header structure PicHeadStruct used to record the number of image data bytes.
[0032] Calculate the number of bytes that can be written WritableLen according to the index of the read-write pointer: If the write pointer index WriteIndex is greater than or equal to the read pointer ReadInde index, calculate the number of bytes that can be written WritableLen = CycleBufferSize -(WriteIndex - ReadIndex); when the write pointer index WriteIndex is less than the read pointer index ReadIndex, calculate the number of writable bytes WritableLen = ReadIndex – WriteIndex.
[0033] If the number of writable bytes WritableLen is greater than or equal to the number of bytes DataLen_upper of the image frame to be written, the image frame to be written is successfully written into the cycle buffer CycleBuffer.
[0034] If the number of writable bytes WritableLen is less than the number of bytes of the image frame to be written DataLen_upper, the writing of the image frame to be written fails. After sleeping for Time microseconds, the number of writable bytes WritableLen is repeatedly calculated until the lower-level program reads the corresponding image data and frees up space, so that the number of writable bytes WritableLen is greater than or equal to the number of bytes of the image frame to be written DataLen_upper, and the image frame to be written is successfully written to the cycle buffer CycleBuffer.
[0035] Furthermore, the specific steps of writing the image frame to be written into the circular buffer are as follows: The number of bytes between the write pointer and the tail pointer is calculated through the tail pointer index TailIndex and the write pointer index WriteIndex. k_upper = TailIndex – WriteIndex.
[0036] If k_upper >= DataLen_upper, the memory copy function is called to write the image frame to be written into the cycle buffer CycleBuffer at one time, and the write pointer index value WriteIndex = WriteIndex + DataLen_upper is updated to continue writing the next image frame.
[0037] If k_upper < DataLen_upper, the image frame to be written with the byte number DataLen_upper is split into two parts with the byte numbers k_upper and DataLen_upper – k_upper respectively, and the memory copy function is called twice to write the circular buffer CycleBuffer twice, and the write pointer index value WriteIndex = DataLen_upper- k_upper is updated to continue writing the next image frame.
[0038] Furthermore, the specific steps of reading the image data described in step 3.1 are as follows: Recalculate the number of bytes between the temporary read pointer index TempReadIndex and the tail pointer index TailIndex k_lower = TailIndex - TempReadIndex: If k_lower >= PicSize, the memory copy function is called to read the image data with the number of bytes PicSize from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program at one time, and modify the temporary read pointer index to the new position: TempReadIndex = TempReadIndex + PicSize to complete the reading of the image data.
[0039] If k_lower < PicSize, the image data with the byte number of PicSize is split into two parts with the byte numbers of k_lower and PicSize - k_lower respectively, and the memory copy function is called twice, from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program twice, and the temporary read pointer index is modified to the new position: TempReadIndex = PicSize - k_lower, completing the reading of the image data.
[0040] The present invention uses the above method to optimize the verification and exception problems in the data transmission and processing process. When a frame of image written into the circular buffer is inconsistent with the verification result when reading, the lower-level program repositions by moving the temporary read pointer and tries to find the correct data frame matching it in the circular buffer. It not only reduces the situation of discarding data due to verification errors, but also improves the fault tolerance of the system under the premise of ensuring data integrity. This innovative exception handling mechanism helps to avoid error detection and data repair in data transmission, and is particularly suitable for scenarios requiring high reliability and real-time performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Figure 1 is a flow chart of the overall process of the present invention; Figure 2 It is a flow chart of an upper-layer program writing an image frame into a circular buffer in an embodiment; Figure 3 It is a flowchart of the lower-level program reading image frames from the circular buffer in the embodiment. DETAILED DESCRIPTION
[0042] The method of the present invention is further described below in conjunction with the accompanying drawings.
[0043] In this embodiment, the resolution of the camera is set to 1080P, that is, the resolution of each frame of the collected image is 1920 * 1080, and the collected image is stored in the RGB888 three-channel format. The reading and writing processes are independent of each other and executed in parallel. In this embodiment, the execution order of the writing process is prioritized over the reading process, and the operating environment of the program is set to the Linux system. When the camera is powered on, the upper-level program (writing process) and the lower-level program (reading process) start running.
[0044] like Figure 1 As shown, a code stream circular buffer management method with verification and exception handling specifically includes the following steps: Step 1: Create shared memory: Step 1.1, apply to the Linux kernel for memory space shared by the upper-layer program and the lower-layer program, that is, apply for shared memory, and obtain the unique identification number ID (generated by the system) of this shared memory segment. Shared memory includes the circular buffer CycleBuffer and the shared structure SharedStruct. The circular buffer CycleBuffer is used to store image data, and the shared structure SharedStruct is used to store shared variables that control the circular buffer and the interaction between the upper and lower-layer programs.
[0045] The size of the circular buffer is a value agreed upon in advance by the upper-layer program and the lower-layer program. It is usually 5 to 10 times the size of the image data packet. The size is set according to the specific needs of the application to ensure that there is enough storage space when the data is transmitted at a high frequency. The shared variables contained in the shared structure SharedStruct include the write pointer index WriteIndex, the read pointer index ReadIndex, the head pointer index HeadIndex, the tail pointer index TailIndex, and the flag Create indicating whether the buffer has been created.
[0046] The specific steps to apply for shared memory are as follows: Step B1, set the project engineering path Path, depending on the project engineering path Path, call the format conversion function (ftok function) to obtain the unique key value key used for inter-process communication.
[0047] Step B2, the size of the circular buffer CycleBufferSize and the number of bytes occupied by the shared structure SharedStruct are added together to calculate the number of bytes of shared memory SharedMemorySize that needs to be applied for: SharedMemorySize = CycleBufferSize + sizeof(SharedStruct).
[0048] Among them, the sizeof keyword is used to calculate the number of bytes occupied by the shared structure SharedStruct.
[0049] Step B3, the key value key obtained in step B1 and the number of bytes SharedMemorySize required to apply for shared memory obtained in step B2, call the shared memory acquisition function (shmget function) to apply for shared memory from the Linux kernel, and obtain the unique identification number ID of the shared memory.
[0050] Step 1.2: The upper-layer program / lower-layer program calls the shared memory mapping function (shmat function) to map the shared memory to the upper-layer program / lower-layer program based on the unique identification number ID, allocates the shared memory, distinguishes the circular buffer and the shared structure SharedStruct, and completes the creation of the circular buffer CycleBuffer.
[0051] Step 1.3 calls the memory block initialization function (memset function) to clear the memory segment, initialize the shared structure SharedStruct, assign the write pointer index WriteIndex to 0, the read pointer index ReadIndex to 0, the head pointer index HeadIndex to 0, the tail pointer index TailIndex to CycleBufferSize, and the buffer has been created flag Create to 1.
[0052] Step 2: After the upper-layer program encrypts the acquired image, it writes the image frame into the CycleBuffer, specifically: Step 2.1, collect a frame of image data, and after preprocessing it according to actual needs, read the preprocessed image data into the temporary buffer Buffer_upper of the upper program.
[0053] In this embodiment, each frame of the captured image is grayed out to convert the three-channel RGB image into a single-channel grayscale image, in order to reduce the amount of data and improve transmission efficiency, which is particularly suitable for monitoring scenarios that are not sensitive to color. Then, the grayed image data is compressed using the H.264 encoding standard to further reduce the size of the data frame, ensuring that more frames can be transmitted within limited bandwidth or storage space. The compressed image data size is set to 500,000 bytes, completing the preprocessing work.
[0054] Step 2.2: Initialize the image header structure PicHeadStruct and encrypt the image based on the image data in the temporary buffer Buffer_upper of the upper-layer program. The image header structure PicHeadStruct contains a series of image information, including image width Width, image height Height, image check code CheckCode, image byte number PicSize and other basic information of the image; it also includes image type Type, image shooting time Time and other image information. In actual operation, the image header structure PicHeadStruct can also add other fields according to actual needs.
[0055] The specific steps of initializing the image header structure in this embodiment are as follows: According to the resolution (1080P) when the camera captures the image, the image width is assigned to 1920, and the image height is assigned to 1080. Through encryption calculation, the check code with the key value or key sequence agreed in advance by the upper and lower layer programs is obtained, and the pre-processing type Type is assigned to "Gray_scale", and the image byte number PicSize is assigned to the number of bytes after encoding and compression, 500,000.
[0056] In this embodiment, an exclusive OR (XOR) operation is performed on each byte of the image data, which is combined with a key value or key sequence agreed upon in advance by the upper and lower layer programs. Different encryption algorithms can be used to calculate and generate a check code (in this embodiment, the check code value is "12345") according to different application scenarios and store it in the image header structure, and the image check code CheckCode is assigned a value of "12345".
[0057] Step 2.3: Write the initialized image header structure PicHeadStruct and the image data in the temporary buffer Buffer_upper of the upper-layer program into the circular buffer CycleBuffer as image frames.
[0058] like Figure 2 As shown, the specific steps for writing image frames into the circular buffer are as follows: The number of bytes to be written to the image frame DataLen_upper = sizeof (PicHeadStruct) + PicHeadStruct. PicSize.
[0059] Among them, the sizeof keyword is used to calculate the number of bytes occupied by the image header structure PicHeadStruct, and PicHeadStruct. PicSize is the variable PicSize in the image header structure PicHeadStruct used to record the number of image data bytes.
[0060] Calculate the number of bytes that can be written WritableLen according to the index of the read-write pointer: If the write pointer index WriteIndex is greater than or equal to the read pointer ReadInde index, calculate the number of bytes that can be written WritableLen = CycleBufferSize -(WriteIndex - ReadIndex); when the write pointer index WriteIndex is less than the read pointer index ReadIndex, calculate the number of writable bytes WritableLen = ReadIndex – WriteIndex.
[0061] If the number of writable bytes WritableLen is greater than or equal to the number of bytes of the image frame to be written DataLen_upper, the image frame to be written (including the image header structure and image data) is successfully written into the cycle buffer CycleBuffer.
[0062] If the number of writable bytes WritableLen is less than the number of bytes of the image frame to be written DataLen_upper, the writing of the image frame to be written fails. After sleeping for Time microseconds, the number of writable bytes WritableLen is repeatedly calculated until the lower-level program reads the corresponding image data and frees up space, so that the number of writable bytes WritableLen is greater than or equal to the number of bytes of the image frame to be written DataLen_upper, and the image frame to be written (including the image header structure and image data) is successfully written to the cycle buffer CycleBuffer; the general value of Time is 10 ~ 100.
[0063] The specific steps of writing the image frame to be written into the circular buffer are as follows: The number of bytes between the write pointer and the tail pointer is calculated through the tail pointer index TailIndex and the write pointer index WriteIndex, k_upper = TailIndex – WriteIndex, that is, the size of the memory space to the right of the write pointer is calculated.
[0064] If k_upper >= DataLen_upper, the memory copy function (memcpy function) is called to write the image frame to be written into the circular buffer CycleBuffer_upper at one time, and the write pointer index value WriteIndex = WriteIndex + DataLen_upper is updated to continue writing the next image frame.
[0065] If k_upper < DataLen_upper, the image frame to be written with the byte number DataLen_upper is split into two parts with the byte numbers k_upper and DataLen_upper – k_upper respectively. The memory copy function (memcpy function) is called twice to split the two parts and write them into the circular buffer CycleBuffer_upper of the upper-layer program. The write pointer index value WriteIndex = DataLen_upper – k_upper is updated to continue writing the next image frame.
[0066] Step 3: The lower-level program reads the image frame in the CycleBuffer: Step 3.1, read a certain image frame (including image header structure and image data) written in the cycle buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program. Figure 3 As shown, the specific steps for reading are as follows:
[0067] The lower-level program calls the temporary read pointer index TempReadIndex, sets the temporary read pointer index TempReadIndex equal to the read pointer index ReadIndex, and the temporary readable byte number TempReadableLen equal to the readable byte number ReadableLen.
[0068] Calculate the number of readable bytes ReadableLen according to the position of the read / write pointer: If the write pointer index WriteIndex is greater than or equal to the read pointer index ReadIndex, calculate the number of readable bytes ReadableLen = WriteIndex -ReadIndex; when the write pointer index WriteIndex is less than the read pointer index ReadIndex, the number of bytes that can be read is calculated as ReadableLen = CycleBufferSize – (ReadIndex – WriteIndex).
[0069] If the temporary readable byte number TempReadableLen is greater than or equal to the number of bytes occupied by the image header structure PicHeadStruct, it is determined that there is an image frame that can be read; if the temporary readable byte number TempReadableLen is less than the number of bytes occupied by the image header structure PicHeadStruct, it is determined that there is no image frame that can be read.
[0070] The specific steps for reading image frames are as follows: Read the image header structure PicHeadStruct: The number of bytes between the temporary read pointer and the tail pointer k_lower = TailIndex – TempReadIndex is calculated through the tail pointer index TailIndex and the temporary read pointer index TempReadIndex, that is, the size of the memory space on the right side of the temporary read pointer is calculated.
[0071] If k_lower >= sizeof(PicHeadStruct), the memory copy function (memcpy function) is called to read the image header structure PicHeadStruct with the number of bytes of sizeof(PicHeadStruct) from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program at one time, and update the temporary read pointer index value: TempReadIndex = TempReadIndex + sizeof(PicHeadStruct).
[0072] If k_lower < sizeof(PicHeadStruct), split the image header structure PicHeadStruct with the byte number of sizeof(PicHeadStruct) into two parts with the byte numbers of k_lower and sizeof(PicHeadStruct) – k_lower respectively, call the memory copy function (memcpy function) twice, read the data from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower program separately, and update the temporary read pointer index value: TempReadIndex =sizeof(PicHeadStruct) - k_lower.
[0073] By reading the image header structure PicHeadStruct of the image frame, the image information of the image frame to be read is obtained (including the check code CheckCode = 12345, the number of bytes of image data PicSize = 500,000, the image width Width = 1920, and the image height Height = 1080).
[0074] The key value or key sequence agreed upon in advance by the upper and lower layer programs is used to recalculate the check code by using the XOR operation and combining the key value or key sequence. The check code is checked by comparing the calculated check code with the check code in the image header structure ("12345" in this embodiment). If the check code is correct (equal), the image data in the image frame is read and the image data is post-processed according to the needs of the actual project.
[0075] In this embodiment, the H.264 standard is used to decode the image data, so that the compressed image data can be completely restored, so that the system can provide it to the subsequent image processing module for analysis and display. Next, image enhancement processing is performed: adjusting the contrast and edge sharpening. This processing step can enhance the details and clarity in the image, so that the system can more effectively perform target recognition or other image analysis tasks. Finally, intelligent algorithms such as motion detection and perimeter protection can be added to process the image data.
[0076] The image data reading process is as follows: The number of bytes between the temporary read pointer index TempReadIndex and the tail pointer index TailIndex is recalculated k_lower = TailIndex - TempReadIndex; in this embodiment, the number of bytes of image data in each image frame PicSize = 500,000.
[0077] If k_lower >= PicSize, the memory copy function (memcpy function) is called to read the image data with the number of bytes PicSize from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program at one time, and modify the temporary read pointer index to the new position: TempReadIndex = TempReadIndex + PicSize to complete the reading of the image data.
[0078] If k_lower < PicSize, the image data with the byte number of PicSize is split into two parts with the byte numbers of k_lower and PicSize - k_lower respectively, and the memory copy function (memcpy function) is called twice to read from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program respectively, and modify the temporary read pointer index to the new position: TempReadIndex = PicSize - k_lower, thus completing the reading of the image data.
[0079] If the checksum is wrong (not equal), exception handling is performed. The specific steps of exception handling are as follows: The temporary read pointer index is incremented by one: TempReadIndex = TempReadIndex + 1; the temporary read pointer index TempReadIndex is modulo CycleBufferSize and assigned to itself to avoid out-of-bounds situations; the temporary readable byte number TempReadableLen is decremented by one: TempReadableLen = TempReadableLen - 1.
[0080] If the temporary readable byte number TempReadableLen after decrementing by one is greater than or equal to the number of bytes occupied by the image header structure PicHeadStruct, it means that there is enough space, and the image frame reading operation is performed again; if the temporary readable byte number TempReadableLen is less than the number of bytes occupied by the image header structure PicHeadStruct, it means that there is insufficient readable space, and the system program completely runs away, and the camera system is automatically restarted.
[0081] Step 3.2: Update the read pointer index ReadIndex: ReadIndex = TempReadIndex, and read the next image frame.
[0082] The above description only points out the preferred implementation and operation scheme of the present invention; for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A code stream circular buffer management method with verification and exception handling, specifically comprising the following steps: Step 1: Create shared memory: Step 1.1, apply to the Linux kernel for memory space shared by the upper-layer program and the lower-layer program, that is, apply for shared memory, and obtain the unique identification number ID of the shared memory segment; the shared memory includes a circular buffer CycleBuffer and a shared structure SharedStruct, the circular buffer CycleBuffer is used to store image data, and the shared structure SharedStruct is used to store shared variables that control the circular buffer and the interaction between the upper and lower-layer programs; The size of the circular buffer is a value agreed upon in advance by the upper-layer program and the lower-layer program. The size is set according to the specific needs of the application to ensure that there is enough storage space when data is transmitted at a high frequency. The shared variables contained in the shared structure SharedStruct include the write pointer index WriteIndex, the read pointer index ReadIndex, the head pointer index HeadIndex, the tail pointer index TailIndex and the flag Create of whether the buffer has been created; Step 1.2, the upper-layer program / lower-layer program calls the shared memory function to map the shared memory to the upper-layer program / lower-layer program according to the unique identification number ID, allocates the shared memory, distinguishes the circular buffer and the shared structure SharedStruct, and completes the creation of the circular buffer CycleBuffer; Step 1.3 calls the memory block initialization function to clear the memory segment, initializes the shared structure SharedStruct, assigns the write pointer index WriteIndex to 0, the read pointer index ReadIndex to 0, the head pointer index HeadIndex to 0, the tail pointer index TailIndex to CycleBufferSize, and assigns the buffer creation flag Create to 1; Step 2: After the upper-layer program encrypts the acquired image, it writes the image frame into the CycleBuffer, specifically: Step 2.1, collect a frame of image data, pre-process it, and read the pre-processed image data into the temporary buffer Buffer_upper of the upper program; Step 2.2, based on the image data in the temporary buffer Buffer_upper of the upper-layer program, initialize the image header structure PicHeadStruct and encrypt the image; the image header structure PicHeadStruct stores basic information of the image; Step 2.3, write the image data in the initialized image header structure PicHeadStruct and the temporary buffer Buffer_upper of the upper-layer program into the circular buffer CycleBuffer as image frames; Step 3: The lower-level program reads the image frame in the CycleBuffer: Step 3.1, read a certain image frame written in the cycle buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program. The specific steps of reading are as follows: The lower-level program calls the temporary read pointer index TempReadIndex, sets the temporary read pointer index TempReadIndex equal to the read pointer index ReadIndex, and the temporary readable byte number TempReadableLen equal to the readable byte number ReadableLen; Calculate the number of readable bytes ReadableLen according to the position of the read and write pointers: If the write pointer index WriteIndex is greater than or equal to the read pointer index ReadIndex, calculate the number of readable bytes ReadableLen = WriteIndex-ReadIndex; when the write pointer index WriteIndex is less than the read pointer index ReadIndex, calculate the number of readable bytes ReadableLen = CycleBufferSize – (ReadIndex– WriteIndex); If the temporary readable byte number TempReadableLen is greater than or equal to the number of bytes occupied by the image header structure PicHeadStruct, it is determined that there is an image frame that can be read; if the temporary readable byte number TempReadableLen is less than the number of bytes occupied by the image header structure PicHeadStruct, it is determined that there is no image frame that can be read; The specific steps for reading image frames are as follows: Read the image header structure PicHeadStruct: The number of bytes between the temporary read pointer and the tail pointer is calculated through the tail pointer index TailIndex and the temporary read pointer index TempReadIndex, k_lower = TailIndex – TempReadIndex, that is, the size of the memory space to the right of the temporary read pointer is calculated; If k_lower >= sizeof(PicHeadStruct), the memory copy function is called to read the image header structure PicHeadStruct with the number of bytes of sizeof(PicHeadStruct) from the circular buffer CycleBuffer_lower to the temporary buffer Buffer_lower of the lower program at one time, and update the temporary read pointer index value: TempReadIndex = TempReadIndex + sizeof(PicHeadStruct); If k_lower < sizeof(PicHeadStruct), split the image header structure PicHeadStruct with the byte number of sizeof(PicHeadStruct) into two parts with the byte numbers of k_lower and sizeof(PicHeadStruct) – k_lower respectively, call the memory copy function (memcpy function) twice, read from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program twice, and update the temporary read pointer index value: TempReadIndex =sizeof(PicHeadStruct) - k_lower; By reading the image header structure PicHeadStruct of the image frame to be read, the image information of the image frame to be read is obtained; the encryption method agreed in advance with the upper and lower layer programs is used for verification. If the verification is correct, the image data in the image frame is obtained, and the image data is post-processed according to the needs of the actual project; If the check is wrong, perform exception handling. The specific steps of exception handling are as follows: The temporary read pointer index is incremented by one: TempReadIndex = TempReadIndex + 1; the temporary read pointer index TempReadIndex is modulo CycleBufferSize and assigned to itself to avoid out-of-bounds situations; the temporary readable byte number TempReadableLen is decremented by one: TempReadableLen = TempReadableLen - 1; If the temporary readable byte number TempReadableLen after decrement is greater than or equal to the number of bytes occupied by the image header structure PicHeadStruct, it means that there is enough space, and the image frame reading operation is performed again; if the temporary readable byte number TempReadableLen is less than the number of bytes occupied by the image header structure PicHeadStruct, it means that the readable space is insufficient, and the system program is completely out of order, and the camera system is automatically restarted; Step 3.2: Update the read pointer index ReadIndex: ReadIndex = TempReadIndex, and read the next image frame.
2. The code stream circular buffer management method with checksum and exception handling as claimed in claim 1, characterized in that: The specific steps for applying for shared memory described in step 1 are as follows: Step B1, set the project engineering path Path, depending on the project engineering path Path, call the format conversion function (ftok function) to obtain the unique key value key used for inter-process communication; Step B2, the size of the circular buffer CycleBufferSize and the number of bytes occupied by the shared structure SharedStruct are added together to calculate the number of bytes of shared memory SharedMemorySize that needs to be applied for: SharedMemorySize = CycleBufferSize + sizeof(SharedStruct); Among them, the sizeof keyword is used to calculate the number of bytes occupied by the shared structure SharedStruct; Step B3, using the key value key obtained in step B1 and the number of bytes SharedMemorySize required to apply for shared memory obtained in step B2, calling the shared memory function to apply for shared memory from the Linux kernel, and obtaining the unique identification number ID of the shared memory.
3. The code stream circular buffer management method with checksum and exception handling as claimed in claim 1, characterized in that: The preprocessing described in step 2.1 is: grayscale each frame of the acquired image, convert the three-channel RGB image into a single-channel grayscale image, and then compress the grayscale image data using the H.264 encoding standard.
4. The code stream circular buffer management method with checksum and exception handling as claimed in claim 1, characterized in that: The method for encrypting the image described in step 2.2 is: perform an XOR operation on each byte of the image data, combine it with a key value or key sequence agreed upon in advance by the upper and lower layer programs, calculate and generate a checksum and store it in the image header structure.
5. The code stream circular buffer management method with checksum and exception handling as claimed in claim 1, characterized in that: The specific steps of writing the image frame into the circular buffer in step 2.3 are as follows: The number of bytes to be written into the image frame DataLen_upper = sizeof (PicHeadStruct) + PicHeadStruct. PicSize; Among them, the sizeof keyword is used to calculate the number of bytes occupied by the image header structure PicHeadStruct, and PicHeadStruct. PicSize is the variable PicSize in the image header structure PicHeadStruct used to record the number of bytes of image data; Calculate the number of writable bytes WritableLen according to the index of the read and write pointers: If the write pointer index WriteIndex is greater than or equal to the read pointer ReadInde index, calculate the number of writable bytes WritableLen = CycleBufferSize-(WriteIndex- ReadIndex); when the write pointer index WriteIndex is less than the ReadIndex read pointer index, calculate the number of writable bytes WritableLen = ReadIndex- WriteIndex; If the number of writable bytes WritableLen is greater than or equal to the number of bytes of the image frame to be written DataLen_upper, the image frame to be written is successfully written into the cycle buffer CycleBuffer; If the number of writable bytes WritableLen is less than the number of bytes of the image frame to be written DataLen_upper, the writing of the image frame to be written fails. After sleeping for Time microseconds, the number of writable bytes WritableLen is repeatedly calculated until the lower-level program reads the corresponding image frame to free up space, so that the number of writable bytes WritableLen is greater than or equal to the number of bytes of the image frame to be written DataLen_upper, and the image frame to be written is successfully written to the circular buffer CycleBuffer.
6. The code stream circular buffer management method with checksum and exception handling as claimed in claim 5, characterized in that: The specific steps of writing the image frame to be written into the circular buffer are as follows: The number of bytes between the write pointer and the tail pointer is calculated through the tail pointer index TailIndex and the write pointer index WriteIndex: k_upper = TailIndex – WriteIndex; If k_upper >= DataLen_upper, call the memory copy function to write the image frame to be written into the cycle buffer CycleBuffer at one time, and update the write pointer index value WriteIndex = WriteIndex + DataLen_upper, and continue writing the next image frame; If k_upper < DataLen_upper, split the image frame to be written with the byte number DataLen_upper into two parts with the byte numbers k_upper and DataLen_upper – k_upper respectively, call the memory copy function twice, write the circular buffer CycleBuffer twice, and update the write pointer index value WriteIndex = DataLen_upper – k_upper, and continue writing the next image frame.
7. The code stream circular buffer management method with checksum and exception handling as claimed in claim 3, characterized in that: When reading the image frame, the H.264 standard is used to decode the read image data.
8. The code stream circular buffer management method with checksum and exception handling as claimed in claim 1, characterized in that: The specific steps for reading image data described in step 3.1 are as follows: Recalculate the number of bytes between the temporary read pointer index TempReadIndex and the tail pointer index TailIndex k_lower = TailIndex - TempReadIndex: If k_lower >= PicSize, the memory copy function is called to read the image data with the number of bytes PicSize from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program at one time, and modify the temporary read pointer index to the new position: TempReadIndex = TempReadIndex + PicSize to complete the reading of the image data; If k_lower < PicSize, the image data with the byte number of PicSize is split into two parts with the byte numbers of k_lower and PicSize - k_lower respectively, and the memory copy function is called twice to read the data from the circular buffer CycleBuffer to the temporary buffer Buffer_lower of the lower-level program in two times, and modify the temporary read pointer index to the new position: TempReadIndex = PicSize - k_lower, thus completing the reading of the image data.
9. The code stream circular buffer management method with checksum and exception handling as claimed in claim 4, characterized in that: The verification method described in step 3.1 is: using the XOR operation, combined with the key value or key sequence, to recalculate the check code; and determining whether the check code is the same as the check code in the image header structure.