Multi-process coding method, system and device based on event and shared drive mechanism
Technical field:
The present invention relates to coding and decoding video fields, it particularly relates to a kind of based on event and shared drive mechanism
Multi-process coding method.
Background technique:
With mobile Internet, the development of big data, artificial intelligence, video processing especially Video coding processing is straight
Broadcast, network transmission, security protection, traffic monitoring, the demand of the various fields such as game it is growing day by day.How video coding system is realized
How high-performance, high throughput, high energy efficiency, the easily characteristics such as extension support that the real-time processing of massive video flow data is the field face
The new challenge faced.Traditional coding method is generally encoded using softwares such as ffmpeg, causes coding real-time lower, and function
Consume larger, used coding method is easy to produce load imbalance due to lacking efficient scheduling mechanism and unstable asks
Topic, and be not easy to be adapted to specific coding and decoding video hardware accelerator, hardware poor expandability.
Summary of the invention:
Present invention aim to address the code efficiencies encountered when massive video coding, and number that is low, supporting is few, power consumption is big,
The problems such as making full use of each encoding device cannot be changed with customer flow.In order to solve these deficiencies of conventional method, this
Invention by various coding methods and hardware interface researched and proposed it is a kind of based on event and shared drive mechanism mostly into
Journey coding method is realized between the different user of upper layer by increasing by one layer of virtual level between upper layer application and bottom hardware
Isolation, improve coded system stability, and propose and a kind of process is improved by shared drive and mutual exclusion lock as far as possible
Between concurrency dispatching method, thus support multi-user can and meanwhile efficiently access bottom encoding device, bottom coding set
It is standby to realize multi-channel video using hardware mechanisms while encoding, to sufficiently excavate the performance of each encoding device.Including with
Lower content:
On the one hand, the present invention provides a kind of multi-process coding method based on event and shared drive mechanism, the side
Method includes:
Virtual level is set between upper layer application and bottom hardware;
Every road coding entity is divided into and reads data to be encoded stream, updates coding parameter, coding, closes four steps,
Upper layer application successively calls the reading data to be encoded stream, updates coding parameter, coding, closes that four steps are corresponding connects
Mouthful, virtual level is successively called according to described, manages bottom hardware coding resource, realizes multi-process coding;
Wherein, every road coding entity requirement sequence call the readings data to be encoded stream, update coding parameter, coding,
Close the corresponding interface of four steps.
Preferably, the reading data to be encoded flow step, including coding entity all the way is opened, it initializes and fills coding
Parameter;
The update coding parameter step, including in an encoding process, by changing current coding parameter, realize upper layer
The consumer process of application changes coding mode;
The coding step, including according to the coding parameter being arranged, the consumer process recursive call of the upper layer application step
Rapid corresponding interface is encoded;
The closing step, including the road coding entity is closed, virtual level discharges corresponding hardware encoding resource.
Preferably, mutual exclusive call between coding entity of not going the same way reads data to be encoded stream, updates coding parameter, closes institute
Corresponding interface.
Preferably, the virtual level allows multiplex coding entity to be performed simultaneously coding step, when new coding entity all the way
When executing reading data to be encoded flow step, virtual level creates a new thread;
Synchronization is realized between thread in a manner of event, wherein producer's process generates data-triggered event, wakes up consumption
Person's process enables the process enter dormant state if consumer process obtains less than event.
Preferably, the virtual level allows multiplex coding entity concurrently to execute coding step;
Thread pool is set, the encoding tasks concurrently executed are passed into the thread pool;
When there is free idle thread in thread pool, then an encoding tasks is distributed into the idle thread and executed.
Preferably, in the thread pool, the encoding tasks are inserted into an obstruction queue, the thread in the thread pool
The encoding tasks are obtained from the obstruction queue.
Preferably, it is realized in coding step in a manner of shared drive, data to be encoded and volume is transmitted between multiple processes
Code stream after the completion of code;
It wherein, is mutual exclusion to the access of a given memory block between multiple processes.
Preferably, creation control lock executes the mutual exclusive call;
Wherein, when the bottom hardware is encoded, different disengaging layers needs to obtain access region control lock first,
It can access and read data to be encoded stream, update coding parameter, close the critical resource in step;When the access operation is completed
Afterwards, release access region control lock.
Preferably, the shared drive mode further include: after data are written to shared region of memory in a process, share
Other processes of this region of memory are visible to the data;
It is mutual exclusion to the access of a given memory block between the multiple process, comprising: when a process is to shared
Region of memory when writing data forbid other process reading and writing data before it completes write operation.
On the other hand, the present invention also provides a kind of multi-process coded system based on event and shared drive mechanism, institute
The system of stating includes: upper application module, virtual level module, bottom hardware module;
The upper application module successively calls coding entity is corresponding to read number to be encoded for the interaction with user
According to stream, update coding parameter, coding, the corresponding interface of four steps of closing;
The virtual level module is used to successively call according to described, manages the coding resource of bottom hardware module, realizes more
Process coding;
The bottom hardware module is for realizing operation and coding.
Preferably, the virtual level module creation control lock,
When the bottom hardware module, which executes, to be encoded, different processes needs to obtain access region control lock, ability first
Access reads data to be encoded stream, updates coding parameter, closes the critical resource in step;After the completion of the access operation,
Discharge access region control lock.
Preferably, the system also includes memory module, the memory module includes share and access area;
After data are written to shared region of memory in a process, other processes of this region of memory are shared to the data
It can be seen that;
When a process is when writing data to shared region of memory, before it completes write operation, forbid other process reading and writing
The data.
In another aspect, the present invention also provides a kind of multi-process code device based on event and shared drive mechanism,
It is characterized in that, described device includes at least one processor and at least one read-write storage device;
It include a shared access region in the read-write storage device;The share and access area meets: when a process is to altogether
After the region of memory write-in data enjoyed, other processes for sharing this region of memory are visible to the data;When a process is to altogether
When the region of memory enjoyed writes data, before it completes write operation, forbid other process reading and writing data;
The read-write storage device is stored with instruction code, and the processor calls described instruction code, to execute such as
Method either described in upper.
Compared with the prior art, technical solution of the present invention has high-performance, high throughput, high energy efficiency, easy to maintain, easy expansion
Exhibition, the easily characteristics such as optimization.The isolation between the different user of upper layer is realized, coded system stability is improved, supports multi-user
Bottom encoding device can be efficiently accessed simultaneously, and bottom encoding device is realized multi-channel video using hardware mechanisms while compiled
Code, to sufficiently excavate the performance of each encoding device.
Detailed description of the invention:
Fig. 1 is the life cycle of the road implementation process Zhong Mei of the present invention video;
Fig. 2 is addressable port call flow chart in the present invention;
Fig. 3 is coding framework figure in the present invention;
Fig. 4 is high-throughput scheduling flow description figure in implementation process of the present invention.
Specific embodiment:
Following will be combined with the drawings in the embodiments of the present invention, and technical solution in the embodiment of the present invention carries out clear, complete
Site preparation description, it is clear that the described embodiment is only a part of the embodiment of the present invention, rather than whole embodiments.Based on this hair
Embodiment in bright, those of ordinary skill in the art under the premise of not making the creative labor it is obtained it is all its
His embodiment, shall fall within the protection scope of the present invention.
It should be pointed out that being that can mutually use for reference or mutually between embodiment in all embodiments of the invention
Be used in combination, each embodiment is only illustrated uses as preferred mode, unless explicitly pointed out in this specification, otherwise it is following respectively
The step of technical solution between embodiment or embodiment can be called mutually or be executed jointly.
Embodiment 1
In a specific embodiment, by taking coding entity is Video coding object as an example, skill of the invention is elaborated
Art scheme embodiment.Although the data processing object being pointed out that in the present embodiment is video data, this hair
The data processing object that bright technical solution is applicable in is not limited to video stream data, can be applicable to other multiplex coding objects
Data processing.
As shown in Figure 1, the life cycle of every road coding entity be abstracted as read data to be encoded stream, update coding parameter,
It encoded, close four steps of this road video, and be encapsulated as open101, update102, encode103, close104 tetra-
A interface.
Wherein open101 is to open encoded video all the way, initializes and fill coding parameter, parameter includes that video type is
H.264 or mpeg2, image quality rank profile, the high information of width of each frame, bit rate mode are that regular coding CBR still can be changed
Piece (slice) number, interframe space (gop size) size that code rate V BR, video frame rate, each frame include etc..The parameter only exists
Setting is primary when initialization coding, will not be modified in an encoding process.
Update102 is to update coding parameter, in an encoding process if it is desired to change coding mode can pass through update
Go change coding parameter, the parameter that can be changed have pixel format, coded image width, coded image height, whether will be current
Frame is encoded to I frame, encoder bit rate, which can repeatedly call, and at least calls once when encoded video initialization, with
Parameter can be set repeatedly afterwards.
Encode103 is encoded for calling underlying device, such as the farsighted core AI chip of section in use, powerful abundant
API, the solution support a variety of graphic platforms (graphics platforms), realize general utility functions, can regard to number
Frequency pre-processed, encoding and decoding and the conversion of different coding format, can be used as bottom encoding device processing Video coding.
Close104 be complete this road video coding after close this video all the way.
The call flow chart of this four interfaces is as shown in Fig. 2, coding first calls open interface 201 to open wait compile when starting
The data flow of code at least calls a update interface 202 when encoded video initialization, then when coding parameter changes
Update interface 202 is called when change again, if you do not need to changing coding parameter then calls directly encode addressable port
203 are encoded, and call close interface 204 to close this road video when encoding and completing.
Coding first calls open interface 201 to open data flow to be encoded when starting, when encoded video initialization
A update interface 202 is at least called then to call update interface 202 again when coding parameter changes, if not
It needs to change coding parameter and then calls directly encode addressable port 203 and encoded, call close interface when encoding and completing
204 close this road video.
As shown in Figure 3, the general frame of system is deployed as passing through between bottom encoding device 301 and consumer process 304
The channel PCIe 302 is in communication with each other.Virtual level 303, which is realized, between the channel PCIe 302 and consumer process 304 allows multi-channel video
Encode coding is carried out simultaneously.Wherein open, update, close interface only call primary, update in entirely coding process
Although interface repeatedly calls but only carries out coding parameter transmitting few in number, therefore the volume of transmitted data of these three interfaces
It is very limited, the channel PCIe 302 can be shared, we can by way of mutual exclusion lock the channel exclusive reference PCIe 302.
And encode interface need multi-channel video and meanwhile carry out yuv data transmission also need to return coding complete BS code stream, therefore
One or more PCIe accesses 302 are used alone.
Fig. 4 is that the detailed process of whole system describes figure, and coding step is divided into and reads data to be encoded stream, more newly organized
Code parameter is encoded, closes four steps of this road video, and being encapsulated as open () 401, update () 402, encode
404 4 () 403, close () interfaces.I-th road client process sequence call open401, update402, encode403,
404 interface of close, the corresponding Video coding all the way of the i-th road client process, life cycle open401, update402,
encode403,close404.Equally, the life cycle in bottom encoding service process per video all the way be also open412,
Update413, encode414, close415, also require that sequence call open412, update413, encode414,
Close415 interface.
It is required that mutual exclusive call open412, update413, close415 interface is needed between video of not going the same way, bottom coding
The bottom layer realizations such as driving determine that the resource that open412, update413, close415 are used is critical resource, need to guarantee not
With carrying out exclusive reference between process.
It does not go the same way and needs mutual exclusive call open412, update413, close415 interface between video, for the demand,
Creation mutual exclusion lock is needed exist for, for exclusive reference critical resource between process.It does not go the same way and needs first to obtain control zone between video
Access lock 411, only takes lock, could access the critical resource in open412, update413, close415.When having encoded
At when close calling terminate after, release the control area access lock 416.
Guarantee to call aforementioned four interface with video sequences all the way and video of not going the same way between exclusive reference basis
On, the concurrency as far as possible between raising process.Bottom coding realizes that allowing multi-channel video to carry out encode 414 simultaneously encodes,
Whenever the new open 412 of video all the way, bottom encoding device will create a new thread, dedicated for client into
Data interaction when row coding.Cataloged procedure such as Fig. 4, upper-layer user's process is as follows:
Step the s401: the i-th road client process calls encode, transmits yuv data to virtual level service thread.
Step s402: triggering the i-th road of bottom encoding device YUV video writes complete event.Bottom encoding device is waken up to carry out
I-th road Video coding, allows it to handle YUV.
Step s403: client process enters dormant state and the coding of the i-th road of bottom encoding device video is waited to complete thing
Part.
Step s404: the thread of the i-th road of bottom encoding device video waits the YUV of the i-th road client process to write and succeeds
Part, if the thread that can not receive this i-th road of event bottom encoding device video, which will be blocked, subsequently enters dormant state, thus
Reduce resource consumption.It waits and writes complete event until receiving the YUV of the i-th road client process.
Step s405: it wakes up the thread of the i-th road of bottom encoding device video and carries out coded treatment.
Step s406: the i-th road Video coding of triggering writes complete event after coding is completed.
Step the s407: the i-th road Video coding complete event wake up the i-th road suspend mode Zhong client process, the i-th tunnel client into
Journey returns BS code stream to upper layer application, completes the coding of a frame data.
It does not go the same way and encodes and can concurrently execute between video, therefore for one can be initiated per Video coding all the way
Thread, for the ease of realizing that thread management, the present invention go to manage numerous threads by way of thread pool.
The present invention is realized by way of shared drive transmits bulk between bottom encoding service host process and client process
Data, this mode is a kind of most fast method of process data sharing, needs to cooperate process exclusive, Process Synchronization to use.
Shared drive is divided according to the number of process, each process exclusively enjoys a piece of memory, and BS/YUV number is monopolized on the road i in as Fig. 4
According to area 406,407 ... ..., because being that two processes access to a given memory block, if a process is to altogether
It enjoys memory field and writes data, then before it finishes this single stepping, another process should not go these data of reading and writing.So needing
A control zone 405 is wanted to go to guarantee the exclusive reference to each data field.
Embodiment 2
The embodiment still with Video coding as an example, the implementation principle that the present invention is further explained.
Specific embodiment 1: every road coding entity is abstract.The life cycle of every road coding entity is abstracted as and is read wait compile
Code data flow, update coding parameter, encoded, close four steps of this road video, and be encapsulated as open (), update (),
Four encode (), close () interfaces, four interfaces are successively called by upper-layer user's process, and virtual level is according to the life on every road
Period is managed bottom coding resource.
Open interface is to open encoded video all the way, initializes and fill coding parameter, parameter includes that video type is
H.264 or mpeg2, image quality rank profile, the high information of width of each frame, bit rate mode are that regular coding CBR still can be changed
Piece (slice) number, interframe space (gop size) size that code rate V BR, video frame rate, each frame include etc..The parameter only exists
Setting is primary when initialization coding, will not be modified in an encoding process.
Update interface is to update coding parameter, and upper-layer user's process is if it is desired to change coding mode can in an encoding process
To go change present encoding parameter by update, the parameter that can be changed includes pixel format, coded image width, code pattern
Whether image height degree by present frame is encoded to I frame, encoder bit rate, which can repeatedly call, in encoded video initialization
It waits and at least calls once, subsequent parameter can be set repeatedly.
Encode interface realizes cataloged procedure, and according to the coding parameter being set, upper-layer user's course cycles call should
Interface is encoded, and virtual level one frame initial data of every reception (YUV, RGB etc.) is encoded to accordingly by bottom encoding device
BS code stream, and the video code flow finished is returned into consumer process.
Close is to close this video all the way after the coding for completing this road video, and virtual level discharges corresponding hardware encoding money
Source.
Coding first calls open interface to open data flow to be encoded when starting, when encoded video initialization at least
A update interface is called, then calls update interface again when coding parameter changes, if you do not need to changing
Coding parameter then calls directly encode addressable port and is encoded, and close interface is called to close this road view when encoding and completing
Frequently.
It is required that need guarantee sequence to call open, update, encode, close interface with video all the way, each user into
The corresponding Video coding all the way of journey, life cycle open, update, encode, close.Equally, bottom encoding service into
Also it is open, update, encode, close per the life cycle of video all the way in journey, also requires that every video needs all the way
Guarantee sequence calls open, update, encode, close interface.
Specific embodiment 2: needing mutual exclusive call open, update, close interface between video of not going the same way, because this
A three periods accounted in the coding life cycle of every road ratio is very small, it is few with the traffic of coded hardware, it is hard in order to improve
The utilization rate in part channel realizes higher performance, these three stages of different coding number are transmitted using a channel, bottom
The bottom layer realizations such as layer coding driving determine that the resource that open, update, close are used is critical resource, it is therefore desirable to guarantee
These three stages can be with the access channel resource of mutual exclusion.For the demand, virtual level needs to create mutual exclusion lock, for mutual between process
Reprimand access critical resource.The host process of virtual level can create corresponding coding thread according to the number of consumer process, per all the way
Video corresponds to a thread, needs first to obtain control zone access lock between difference process when bottom encoding device is encoded, only
Lock is taken, the critical resource in open, update, close could be accessed.Access region control is discharged after the completion of access operation
Lockmaking, other processes could access critical resource.
Specific embodiment 3: realizing the synchronization between process by way of event, and producer's process generates data-triggered thing
Part wakes up consumer process, it is allowed to handle the data.If consumer process obtains and will be blocked less than event,
Into dormant state, process resource consumption is reduced.
Guarantee to call aforementioned four interface with video sequences all the way and video of not going the same way between exclusive reference basis
On, the concurrency as far as possible between raising process.Virtual level, which is realized, allows multi-channel video to carry out encode coding simultaneously.Whenever new
The open of video all the way when, virtual level will create a new thread, and data when dedicated for being encoded with client are handed over
Mutually.Process is as follows:
I-th road client process calls encode, first prepares yuv data, when the yuv data of the i-th road client process is ready to
Afterwards, triggering the i-th road of bottom encoding device YUV video writes complete event, wakes up bottom encoding device and carries out the i-th road Video coding,
It is allowed to handle YUV, subsequent client process enters dormant state and waits the coding of the i-th road of bottom encoding device video complete
At event.
The thread of the i-th road of bottom encoding device video waits the YUV of the i-th road client process to write complete event, if received not
Thread to this i-th road of event bottom encoding device video will be blocked and subsequently enter dormant state, disappear to reduce resource
Consumption.Thread and the progress of the i-th road of bottom encoding device video are waken up after the YUV for receiving the i-th road client process writes complete event
Coded treatment, the i-th road Video coding of triggering writes complete event after coding is completed.
I-th road Video coding writes complete event and wakes up the i-th road suspend mode Zhong client process, and the i-th road client process is to upper layer
Using passback BS code stream, the coding of this frame data is completed.
Specific embodiment 4: coding can be executed concurrently between video of not going the same way, therefore for compiling per video all the way
Code can initiate a thread, and when our encoding tasks numbers to be processed are smaller, we can create several threads ourselves
Corresponding task is handled, but when there are a large amount of encoding tasks, since creation, destroying threads need very big expense, used
Thread pool these problems just greatly alleviate, and in order to reduce expense while be easy to implement thread management, the present invention passes through thread
The mode in pond goes to manage numerous coding threads.
We can pass to the encoding tasks concurrently executed one thread pool, to be replaced by each times concurrently executed
Business all creates a new thread.As long as available free thread in pond, encoding tasks will distribute to a thread and execute.Online
The inside of Cheng Chi, encoding tasks are inserted into an obstruction queue (Blocking Queue), and the thread in thread pool can go to take
Task in this queue.As soon as success will follow takes out from queue times for one idle thread when the insertion queue of new task
It is engaged in and executes it.Thread pool (Thread Pool) is very useful for the Thread Count of synchronization operation in limitation application program,
Because one new thread of every starting can all have corresponding performance cost, per thread requires to distribute some memories etc. to stack.
Specific embodiment 5: the present invention realized by way of shared drive bottom encoding service host process and client into
The code stream after the completion of data to be encoded and coding is transmitted between journey, shared drive is a kind of most fast of process data sharing
Method, access shared drive region and access it is fast as the exclusive region of memory of process, need not move through system calling or
Other processes for needing to cut kernel are completed, while it also avoids the various unnecessary duplications to data.One process
Data are written with to shared region of memory, all processes for sharing this region of memory can be immediately seen in therein
Hold.Using shared drive it should be noted that the mutual exclusion accessed between multiple processes a given memory block, it is therefore desirable to cooperate
Process exclusive, Process Synchronization use because being that multiple processes access to a given memory block, if a process to
Shared section key writes data, then before it finishes this single stepping, other process should not go these data of reading and writing.
Embodiment 3
In yet another embodiment, the present invention also provides a kind of, and the multi-process based on event and shared drive mechanism encodes
System, the system can execute the specific method as in embodiment 1, embodiment 2.The system comprises: upper application module, void
Quasi- layer module, bottom hardware module;
The upper application module successively calls coding entity is corresponding to read number to be encoded for the interaction with user
According to stream, update coding parameter, coding, the corresponding interface of four steps of closing;
The virtual level module is used to successively call according to described, manages the coding resource of bottom hardware module, realizes more
Process coding;
The bottom hardware module is for realizing operation and coding.
Preferably, the virtual level module creation control lock,
When the bottom hardware module, which executes, to be encoded, different processes needs to obtain access region control lock, ability first
Access reads data to be encoded stream, updates coding parameter, closes the critical resource in step;After the completion of the access operation,
Discharge access region control lock.
Preferably, the system also includes memory module, the memory module includes share and access area;
After data are written to shared region of memory in a process, other processes of this region of memory are shared to the content
It can be seen that;
When a process is when writing data to shared region of memory, before it completes write operation, forbid other process reading and writing
The data.
Embodiment 4
In another embodiment, the present invention also provides a kind of, and the multi-process based on event and shared drive mechanism encodes
Device, described device include at least one processor and at least one read-write storage device;
It include a shared access region in the read-write storage device;The share and access area meets: when a process is to altogether
After the region of memory write-in data enjoyed, other processes for sharing this region of memory are visible to the content;When a process is to altogether
When the region of memory enjoyed writes data, before it completes write operation, forbid other process reading and writing data;
The read-write storage device is stored with instruction code, and the processor calls described instruction code, to execute such as
The method of upper the embodiment described 1,2.
Those of ordinary skill in the art will appreciate that realizing all or part of the process in above-described embodiment method, being can be with
Relevant hardware is instructed to complete by computer program, the program can be stored in a computer-readable storage medium
In, the program is when being executed, it may include such as the process of the embodiment of above-mentioned each method.Wherein, the storage medium can be magnetic
Dish, CD, read-only memory (Read-Only Memory, ROM) or random access memory (Random Access
Memory, RAM) etc..
The present invention is described referring to the method for the embodiment of the present invention, block diagram, line chart, analogous diagram, described above
Only the embodiment of the present invention, does not limit the present invention, and anyone skilled in the art takes off in the present invention
In the technical scope of dew, any changes or substitutions that can be easily thought of, should be covered by the protection scope of the present invention.Therefore, originally
The protection scope of invention should be subject to the protection scope in claims.