A kind of method of statistics of multiplex transmission stream
Technical field
The present invention relates to a kind of method of statistics of multiplex transmission stream.
Background technology
The ripe now digital audio/video encoding and decoding standard of using, as MPEG-2, and a collection of representative international standard that occurs in recent years, as MPEG-4, H.264/MPEG-4 AVC (a kind of), the autonomous standard A VS that formulates of China etc. by the common video compression coding international standard of formulating of ITU-T and ISO/IEC MPEG, all some is called components of system as directed in these standards, and how this part mainly discussed synthetic single channel and the multichannel combined-flow storing and transmit of being suitable for of single channel or multichannel elementary audio and video flowing and other data sets.Transport stream is exactly a kind of data flow of components of system as directed definition, and it will have one or more programs of one or more different clocks benchmark and form a single stream, its speed can be constant also can be variable.Transport stream defines at carrying out program transmission in the environment that may occur medium errors (often showing as faults or lost packets) at those, for example satellite transmits.
There are the multiplexing and two kinds of methods of statistic multiplexing of cbr (constant bit rate) at present in MPEG-2 or AVS transport stream, each road program is according to cbr (constant bit rate) (Constant Bit Rate, CBR) absolute coding and then carry out multiplexing this method and implement simpler, and guarantee transfer rate constant of transport stream easily, but this method is to variable bit rate (the Variable Bit Rate of MPEG-2 or AVS video, VBR) code stream carries out when multiplexing, will cause very big bandwidth waste, because the VBR coding fixed quantisation factor, guaranteed the steady quality of image, but because the amount of exercise of each two field picture is different, caused bit rate output to change greatly the complexity that makes multiplexing change.Statistic multiplexing can be good at finishing multiplexing to the video code flow of VBR coding, and it can be learnt from other's strong points to offset one's weaknesses to multi-channel program, avoids the code check of each road program to reach maximum or minimum peak value simultaneously, and statistic multiplexing has improved the utilance of channel greatly.
The statistical multiplexer that exists some renowned company's research and development to produce in the market, this class multiplexer majority is to realize with hardware, and in the process of statistic multiplexing, the process of communicating by letter with encoder is all arranged, promptly in a period of time, check the bit number of the code stream of the multi-channel program of importing, then require encoder that during this period of time program is carried out recompile till meeting the requirements if surpass certain scope, such implementation method is suitable for encoding and the multiplexing situation that realizes simultaneously, exist for the sort of video code flow, when requirement is multiplexed into the constant transport stream of transfer rate with these video-frequency basic flows, this multiplexer just can not well be finished statistic multiplexing, because under the undesirable situation of code stream, it is had no idea and encoder communicating requirement encoder recompile.And in the statistic multiplexing method of present transport stream, great majority also are not break away from encoder, are not to carry out statistic multiplexing towards the basic stream file of the multi-channel video of having finished.
Summary of the invention
The method of a kind of statistics of multiplex transmission stream provided by the invention provides a kind of advantages of simplicity and high efficiency, breaks away from encoder fully, and the video-frequency basic flow file is carried out the method that multichannel is added up, and the transfer rate of the multiplexing transport stream that obtains can be constant fully.
In order to achieve the above object, the invention provides a kind of method of statistics of multiplex transmission stream, it comprises following steps:
Step 1, initialization;
Step 1.1, initialization module is opened the video-frequency basic flow file and the basic stream file of audio frequency of input, obtain the type of the code stream in the file, wherein video type is MPEG-2 or AVS (advanced audio/video encoding standard, the video encoding standard of China's independent research), audio types is the layer 1 (layer 1) of MPEG-1, layer 2 (layer 2), layer 3 (layer 3) or AC-3 (the digital audio compression standard of regulation among the U.S. ATSC (AdvancedTelevision Systems Committee)), perhaps AVS obtains some essential informations of the video and the audio frequency on each road simultaneously;
Described essential information comprises following: the size that image is arranged of video aspect, frame rate, bit rate etc., the code check that the audio frequency aspect has audio frequency to flow substantially, sample rate etc.;
Step 1.2, initialization module application be inner some internal memories that use when doing statistic multiplexing;
Step 1.3, initialization module are according to the transfer rate of wanting multiplexing transport stream, and pre-set this time span of in one section continuous Fixed Time Interval, input code flow being carried out statistic multiplexing, calculate at this section in the period, transport stream should have the output of what transport stream packets:
nPacket_salframe=(TS_BITRATE/(188*1000*8/ps->pict_duration))*SAL_frame_no;
Wherein:
NPacket_salframe is in the regular hour section, and transport stream requires the number of transport stream TS (Transport Stream) bag of output;
TS_BITRATE is the transfer rate of the predefined transport stream of user;
Ps-〉pict_duration is the inverse of the frame rate of video-frequency basic flow in the transport stream, represents the demonstration time of frame data;
SAL_frame_no is a unit of statistics;
Step 2, statistics input file information;
The size of step 2.1, the every frame of statistics input file information module statistics video file, frame type, and according to the frame type of this video sequence, frame rate and each frame particular location hereof calculate the PTS (Presentation Time Stamp Presentation Time Stamp) of each frame;
Step 2.1.1, judge whether frame_no<frame_no_max sets up, if, carry out step 2.1.2, if not, withdraw from;
Wherein,
Frame_no is the sequence number of frame of video, represents which frame this frame is in the video-frequency basic flow file;
Frame_no_max represents total how many frames in this video-frequency basic flow file;
Step 2.1.2, judge F_T[frame_no]=whether I set up, if, carry out step 2.1.3, if not, carry out step 2.1.4;
Wherein,
F_T[frame_no] frame type of expression frame_no frame, promptly this frame is I frame (intraframe coding), B frame (two-way interframe encode) or P frame (prediction interframe encode);
Step 2.1.3, judge F_T[frame_no+1]=whether B set up, if, carry out step 2.1.5, if not, carry out step 2.1.6;
Step 2.1.4, judge F_T[frame_no]=whether P set up, if, carry out step 2.1.7, if not, carry out step 2.1.9;
The PTS of step 2.1.5, calculating present frame;
F_PTS[frame_no]=frame_no*pict_duration;
Wherein,
F_PTS[frame_no] PTS of expression frame_no frame;
Pict_duration is the inverse of the frame rate of video-frequency basic flow in the transport stream, represents the demonstration time of frame data;
Obtain after the PTS of present frame, the output result adds 1 with frame number, frame_no
++, carry out step 2.1.1;
Step 2.1.6, execution Count_B
++, the value of Count_B is added 1, carry out step 2.1.7;
Wherein,
Count_B represents between an I frame or P frame and adjacent another I frame or the P frame what B frames are arranged;
Step 2.1.7, judge F_T[++frame_no]=whether B set up, if, carry out step 2.1.6, if not, carry out step 2.1.8;
Step 2.1.8, calculating PTS:
F_PTS[frame_no-1-Count_B]=(frame_no-1)*pict_duration;
The output result carries out step 2.1.9;
Step 2.1.9, judge whether Count_B=0 sets up, if, carry out step 2.1.1, if not, carry out step 2.1.10;
Step 2.1.10, calculating PTS:
F_PTS[frame_no-Count_B]=(frame_no-1-Count_B)*pict_duration;
Count_B
-, the value of Count_B is subtracted 1;
The output result carries out step 2.1.8;
Step 2.2, statistics input file information module be for the audio file of AVS, and the size of adding up its each frame calculates the PTS of each audio frame:
ap->audio_frame[frame_no].audioframe_PTS=frame_no*(1024.0/pack_stream->ap->sr)*1000
Wherein,
Ap-〉audio_frame[frame_no] .audioframe_PTS is the demonstration timestamp of frame_no audio frame, frame_no is the frame number of audio frame, pack_stream-〉ap-〉sr is the sample rate of audio frequency;
Step 3, statistics of multiplex transmission stream;
Step 3.1, statistics of multiplex transmission stream module generate PSI (Program Specific Information) information table;
Video-frequency basic flow and the number that flows substantially of audio frequency according to input, generate the Program Specific Information in the transport stream, as the Program Association Table of partly stipulating at MPEG-2 or AVS modular system (PAT), Program Map Table (PMT), network information table (NIT), conditional access table (CAT) and transport stream description list etc.;
Step 3.2, pre-statistics;
The statistics of multiplex transmission stream module is according to the information that obtains in the statistics input file information module, code stream in one section continuous Fixed Time Interval that will carry out statistic multiplexing is analyzed, the size of the bit number of each road program in the estimation during this period of time, estimate that these bit numbers can satisfy the requirement of the transfer rate of transport stream, and carry out respective handling;
The summation audio_video_packets of the number of step 3.2.1, video TS (transport stream) bag that calculates all road programs in the interior during this period of time transport stream and audio TS bag;
The number padding_packets_num of the filling bag in step 3.2.2, the interior transport stream of section computing time;
Step 3.2.3, judge padding_packets_num〉0 whether set up, if, carry out step 3.3, if not, carry out step 3.2.4;
Whether also have the B frame in the frame of video that step 3.2.4, judgement are added up, if, carry out step 3.2.5, if not, carry out step 3.2.6;
Step 3.2.5, lose a frame B frame of that minimum road program of priority, the BP_throw_flag that this frame is set is 1;
Here, if the BP_throw_flag of a certain frame sign equals 1, to abandon this frame when then being illustrated in statistic multiplexing;
Whether also have the P frame in the frame of video that step 3.2.6, judgement are added up, if, carry out step 3.2.7, if not, carry out step 3.2.8;
Step 3.2.7 loses a frame P frame of that minimum road program of priority, and the BP_throw_flag that this frame is set is 1;
Step 3.2.8, report an error and withdraw from;
Step 3.3, statistics of multiplex transmission stream module have judged whether that one road program finishes, if, finish to withdraw from, if not, carry out step 3.4;
Step 3.4, statistics of multiplex transmission stream module are played PES (Packetized Elementary Stream Packet Elementary Stream) grouping bag;
With the frame is the unit packing: for video data, it is each frame (I frame, P frame or B frame) data are as the data division of video PES grouping bag, for the mpeg audio data, be will with the data division of the pairing voice data of one-frame video data as audio frequency PES grouping bag, the size of this part data of mpeg audio PES grouping bag is generally fixed, it and the frame rate of video data and the sample frequency of voice data, the coded format of bit rate and voice data is relevant, for the AVS voice data, each frame data is as the pay(useful) load of an audio frequency PES grouping bag;
The data division of PES grouping bag is added PES grouping bag stem and has just been formed complete PES grouping bag; PES grouping bag stem has comprised Decoding Time Stamp DTS (Decoding Time Stamp) and the Presentation Time Stamp PTS (Presentation Time Stamp) that is used for the decoder synchronized playback, and PTS has calculated in step 2;
Step 35, statistics of multiplex transmission stream module are played the TS bag, calculate the PCR value;
Step 3.5.1, video PES grouping bag is played the TS bag;
Step 3.5.1.1, judge vp-〉v_packet_num % vp-〉whether pcr_demand_perpackets_v=0 (complementation computing) set up, if, carry out step 3.5.1.2, if not, carry out step 3.5.1.3;
Wherein,
Vp-〉v_packet_num represents the sequence number of the video packets of this road program;
Vp-〉pcr_demand_perpackets_v represents this road program every several video packets, has a video packets and contains PCR information;
Step 3.5.1.2, judge whether length-nPesUsedByte<176 set up, if, carry out step 3.5.1.4, if not, carry out step 3.5.1.5;
Wherein,
Length is the length of this frame of video;
NPesUsedByte represents treated byte number in this frame video data;
Step 3.5.1.3, judge whether length-nPesUsedByte<184 set up, if, carry out step 3.5.1.4, if not, carry out step 3.5.1.6;
Step 3.5.1.4, the adaptation_field_control=11 of this PES grouping bag is set;
adaptation_field_length=184-(length-nPesUsedByte)-1;
Carry out step 3.5.1.7;
Wherein,
Adaptation_field_control represents whether this TS bag contains adjustment field and pay(useful) load;
Adaptation_field_length represents to adjust the byte number in the field;
Step 3.5.1.5, the adaptation_field_control=11 of this PES grouping bag is set;
on_field_length=0X07;
176 bytes in the PES grouping bag are squeezed in 176 bytes in end of current TS bag: nPesUsedByte+=176;
The pcr_flag=1 of this PES grouping bag is set;
Carry out step 3.5.1.1;
Wherein,
Pcr_flag: represent whether this TS bag contains PCR information;
Step 3.5.1.6, the adaptation_field_control=00 of this PES grouping bag is set;
184 bytes in the grouping bag are squeezed in end 184 bytes of current TS bag: nPesUsedByte+=184;
The pcr_flag=0 of this PES grouping bag is set;
Carry out step 3.5.1.1;
Step 3.5.1.7, judge vp-〉v_packet_num % vp-〉whether pcr_demand_perpackets_v=0 set up, if, carry out step 3.5.1.8, if not, carry out step 3.5.1.9;
Step 3.5.1.8, (176-(length-nPesUsedByte)) the individual byte of padding in the PES grouping bag is squeezed in the current TS bag;
Carry out step 3.5.1.10;
Step 3.5.1.9, (184-(length-nPesUsedByte)) the individual byte of padding in the PES grouping bag is squeezed in the current TS bag;
Carry out step 3.5.1.10;
Step 3.5.1.10,
(length-nPesUsedByte) individual byte in the PES grouping bag is squeezed in the individual byte in end (length-nPesUsedByte) of current TS bag;
Output nTsPacketNum;
Wherein,
NTsPacketNum represents that this PES grouping bag has been packaged into what TS bag altogether;
Step 3.5.2, audio frequency PES grouping bag is played the TS bag;
Because do not comprise PCR information in the audio TS bag,, the order of the data that the syntax format of the TS bag of the transport stream of stipulating in only needing according to standard when bag that audio frequency PES is divided into groups is packaged into the TS bag wraps according to PES gets final product so writing them in the TS bag;
Step 3.5.3, calculating PCR value;
Equaling to fill in 1 the video TS bag PCR information toward pcr_flag is at the audio frequency and video TS bag with each road, PSI packets of information and fill bag and write these TS bags in the output file uniformly and write and finish in the output file process, finish the video PES on each road and audio frequency PES be packaged into the TS bag after, the video TS bag on each road and the number of audio TS bag have just been known, also have the PSI packets of information and fill the number of wrapping, so just can be very easily write these TS bags of this time period in the output file uniformly, to calculate PCR in the process that writes, in certain multiplexing time period, the computational methods of the PCR of TsNum bag are as follows:
int64_t?timestampll=(((nPacket_salframe*pc_jisuan+TsNum)*188)/(TS_BITRATE/1000.0))*8.0;
pcr=(int64_t)(timestampll*90.0+0.5);
Wherein:
Int64_t timestampll represents the value of the Pgmtime reference information PCR of TsNum bag, nPacket_sadframe calculates in the step 1, promptly according to the transfer rate of wanting multiplexing transport stream, and this time span of in the pre-set time period input code flow being carried out statistic multiplexing how long, calculate at this section in the period, transport stream should have the output of what transport stream packets;
Pcr_jisuan represents it is which time period, whenever finishes the multiplexing of a time period, and this value is from adding 1;
TsNum represents that this video packets is which TS bag in this time period;
TS_BITRATE represents the transfer rate of the TS file of this output;
Step 3.6, statistics of multiplex transmission stream module judge whether that all PES divide into groups to wrap to handle to be over, if, carry out step 3.1, if not, carry out step 3.5;
Step 4, judged whether that one road input file finishes dealing with, if, finish to withdraw from, if not, carry out step 2.
The invention provides a kind of method of statistics of multiplex transmission stream, this method is a kind of method of the simple and statistics of multiplex transmission stream that efficient is high, and following some advantage is arranged:
1) this statistic multiplexing algorithm is under the state that does not communicate with encoder, carries out the method for multichannel statistics to looking the basic stream file of audio frequency;
2) can effectively improve the utilance of channel;
3) can make the speed of the multiplexing transport stream that obtains constant fully;
4) it is simple to look the algorithm of audio sync, and the method for promptly calculating PTS/DTS is simple;
5) frame that to be unit with the frame make decoder to obtain handling very easily in this information of the PES_packet_length of PES grouping bag stem (PES grouping bag is long) to the method for elementary stream packing is looked the end frequently or the size of voice data, can root this value better controlled VBV_buffer (video buffer detector buffering area), make its neither also underflow not of overflow;
6) can be according to the characteristics of input code flow and the requirement of output transport stream, when the instantaneous code check of input file is too high, priority according to input file, the B frame that loss priority is low, the P frame satisfies the requirement of the transfer rate of transport stream up to the instantaneous code check of input file, the advantage of Chu Liing is like this, under the Rate Control of input code flow is not very perfect situation, also can finish the multiplexing of multi-channel program, and the multiplexing transport stream that goes out mosaic can not appear when playing.
Description of drawings
Fig. 1 is the flow chart of the method for a kind of statistics of multiplex transmission stream provided by the invention;
Fig. 2 is the flow chart of step 2.1 of the method for a kind of statistics of multiplex transmission stream provided by the invention;
Fig. 3 is the flow chart of step 3 of the method for a kind of statistics of multiplex transmission stream provided by the invention;
Fig. 4 is the flow chart of step 3.2 of the method for a kind of statistics of multiplex transmission stream provided by the invention;
Fig. 5 is the flow chart of step 3.5.1 of the method for a kind of statistics of multiplex transmission stream provided by the invention.
Concrete real execution mode
Following according to Fig. 1-Fig. 5, specify a kind of better embodiment of the present invention:
As shown in Figure 1, utilize the method for a kind of statistics of multiplex transmission stream provided by the invention, (code check is that the audio stream of 1.5M~2.0M) and 10 road mpeg formats is multiplexed into the transport stream that a transmission rate is constantly equal to Trans_rate (for example Trans_rate=20Mbps) with the video-frequency basic flow of 10 road AVS forms of input;
Step 1, initialization module;
Step 1.1, the video flowing of AVS of resolving input and the sequence head among the MPEG-2 obtain frame rate Frame_rate=25 frame/second of video flowing;
Step 1.2, application be inner some internal memories that use when doing statistic multiplexing;
Step 1.3, according to the input SAL_frame_No=5, ps-〉pict_duration=1000ms/25 frame=40ms, obtain this section in the period, what transport stream packets are transport stream should have export: nPacket_salframe=(TS_BITRATE/ (188*1000*8/ps-〉pict_duration)) * SAL_frame_no=(20000000/ (188*1000*8/40)) * 5=2655;
Wherein:
NPacket_salframe is in the regular hour section, and transport stream requires the number of the TS bag of output;
TS_BITRATE is the transfer rate of the predefined transport stream of user;
Ps-〉pict_duration is the inverse of the frame rate of video-frequency basic flow in the transport stream, represents the demonstration time of frame data;
SAL_frame_no is a unit of statistics;
Step 2, statistics input file information module;
As shown in Figure 2, step 2.1, calculate the PTS (Presentation Time Stamp Presentation Time Stamp) of each frame;
Step 2.1.1, judge whether frame_no<frame_no_max sets up, if, carry out step 2.1.2, if not, withdraw from;
Wherein,
Frame_no is the sequence number of frame of video, represents which frame this frame is in the video-frequency basic flow file;
Frame_no_max represents total how many frames in this video-frequency basic flow file;
Step 2.1.2, judge F_T[frame_no]=whether I set up, if, carry out step 2.1.3, if not, carry out step 2.1.4;
Wherein,
F_T[frame_no] frame type of expression frame_no frame, promptly this frame is I frame (intraframe coding), B frame (two-way interframe encode) or P frame (prediction interframe encode);
Step 2.1.3, judge F_T[frame_no+1]=whether B set up, if, carry out step 2.1.5, if not, carry out step 2.1.6;
Step 2.1.4, judge F_T[frame_no]=whether P set up, if, carry out step 2.1.7, if not, carry out step 2.1.9;
The PTS of step 2.1.5, calculating present frame;
F_PTS[frame_no]=frame_no*pict_duration;
Wherein,
F_PTS[frame_no] PTS of expression frame_no frame;
Pict_duration is the inverse of the frame rate of video-frequency basic flow in the transport stream, represents the demonstration time of frame data;
Obtain after the PTS of present frame, the output result adds 1 with frame number, frame_no
++, carry out step 2.1.1;
Step 2.1.6, execution Count_B
++, the value of Count_B is added 1, carry out step 2.1.7;
Wherein,
Count_B represents between an I frame or P frame and adjacent another I frame or the P frame what B frames are arranged;
Step 2.1.7, judge F_T[++frame_no]=whether B set up, if, carry out step 2.1.6, if not, carry out step 2.1.8;
Step 2.1.8, calculating PTS:
F_PTS[frame_no-1-Count_B]=(frame_no-1)*pict_duration;
The output result carries out step 2.1.9;
Step 2.1.9, judge whether Count_B=0 sets up, if, carry out step 2.1.1, if not, carry out step 2.1.10;
Step 2.1.10, calculating PTS:
F_PTS[frame_no-Count_B]=(frame_no-1-Count_B)*pict_duration;
Count_B
-, the value of Count_B is subtracted 1;
The output result carries out step 2.1.8;
Step 2.2, for the audio file of AVS, add up the size of its each frame, calculate the PTS of each audio frame:
ap->audio_frame[frame_no].audioframe_PTS=frame_no*(1024.0/pack_stream->ap->sr)*1000
Wherein,
Ap-〉autio_frame[frame_no] .audioframe_PTS is the demonstration timestamp of frame_no audio frame, frame_no is the frame number of audio frame, pack_stream-〉ap-〉sr is the sample rate of audio frequency;
As shown in Figure 3, step 3, statistics of multiplex transmission stream module;
Step 3.1, generation PSI (Program Specific Information) information table;
Video-frequency basic flow and the number that flows substantially of audio frequency according to input, generate the Program Specific Information in the transport stream, as the Program Association Table of partly stipulating at MPEG-2 or AVS modular system (PAT), Program Map Table (PMT), network information table (NIT), conditional access table (CAT) and transport stream description list etc.;
As shown in Figure 4, step 3.2, pre-statistics;
According to the information that obtains in the statistics input file information module, code stream in one section continuous Fixed Time Interval that will carry out statistic multiplexing is analyzed, the size of the bit number of each road program in the estimation during this period of time, estimate that these bit numbers can satisfy the requirement of the transfer rate of transport stream, and carry out respective handling;
The summation audio_video_packets of the number of step 3.2.1, video TS (transport stream) bag that calculates all road programs in the interior during this period of time transport stream and audio TS bag;
The number padding_packets_num of the filling bag in step 3.2.2, the interior transport stream of section computing time;
Step 3.2.3, judge padding_packets_num〉0 whether set up, if, carry out step 3.3, if not, carry out step 3.2.4;
Whether also have the B frame in the frame of video that step 3.2.4, judgement are added up, if, carry out step 3.2.5, if not, carry out step 3.2.6;
Step 3.2.5, lose a frame B frame of that minimum road program of priority, the BP_throw_flag that this frame is set is 1;
Here, if the BP_throw_flag of a certain frame sign equals 1, to abandon this frame when then being illustrated in statistic multiplexing;
Whether also have the P frame in the frame of video that step 3.2.6, judgement are added up, if, carry out step 3.2.7, if not, carry out step 3.2.8;
Step 3.2.7 loses a frame P frame of that minimum road program of priority, and the BP_throw_flag that this frame is set is 1;
Step 3.2.8, report an error and withdraw from;
Step 3.3, judged whether that one road program finishes, if, finish to withdraw from, if not, carry out step 3.4;
Step 3.4, play PES (Packetized Elementary Stream Packet Elementary Stream) bag;
With the frame is the unit packing: for video data, it is each frame (I frame, P frame or B frame) data are as the data division of video PES grouping bag, for the mpeg audio data, be will with the data division of the pairing voice data of one-frame video data as audio frequency PES grouping bag, the size of this part data of mpeg audio PES grouping bag is generally fixed, it and the frame rate of video data and the sample frequency of voice data, the coded format of bit rate and voice data is relevant, for the AVS voice data, each frame data is as the pay(useful) load of an audio frequency PES grouping bag;
The data division of PES grouping bag is added PES grouping bag stem and has just been formed complete PES grouping bag; PES grouping bag stem has comprised Decoding Time Stamp DTS (Decoding Time Stamp) and the Presentation Time Stamp PTS (Presentation Time Stamp) that is used for the decoder synchronized playback, and PTS has calculated in step 2;
Step 3.5, play TS bag, calculate the PCR value;
As shown in Figure 5, step 3.5.1, video PES grouping bag is played the TS bag;
Step 3.5.1.1, judge vp-〉v_packet_num % vp-〉whether pcr_demand_perpackets_v=0 (complementation computing) set up, if, carry out step 3.5.1.2, if not, carry out step 3.5.1.3;
Wherein,
Vp-〉v_packet_num represents the sequence number of the video packets of this road program;
Vp-〉pcr_demand_perpackets_v represents this road program every several video packets, has a video packets and contains PCR information;
Step 3.5.1.2, judge whether length-nPesUsedByte<176 set up, if, carry out step 3.5.1.4, if not, carry out step 3.5.1.5;
Wherein,
Length is the length of this frame of video;
NPesUsedByte represents treated byte number in this frame video data;
Step 3.5.1.3, judge whether length-nPesUsedByte<184 set up, if, carry out step 3.5.1.4, if not, carry out step 3.5.1.6;
Step 3.5.1.4, the adaptation_field_control=11 of this PES grouping bag is set;
adaptation_field_length=184-(length-nPesUsedByte)-1;
Carry out step 3.5.1.7;
Wherein,
Adaptaation_field_control represents whether this TS bag contains adjustment field and pay(useful) load;
Adaptation_field_length represents to adjust the byte number in the field;
Step 3.5.1.5, the adaptation_field_control=11 of this PES grouping bag is set;
adaptation_field_length=0X07;
176 bytes in the PES grouping bag are squeezed in 176 bytes in end of current TS bag: nPesUsedByte+=176;
The pcr_flag=1 of this PE8 grouping bag is set;
Carry out step 3.5.1.1;
Wherein,
Pcr_flag: represent whether this TS bag contains PCR information;
Step 3.5.1.6, the adaptation_field_control=00 of this PES grouping bag is set;
184 bytes in the PES grouping bag are squeezed in 184 bytes in end of current TS bag: nPesUsedByte+=184;
The pcr_flag=0 of this PES grouping bag is set;
Carry out step 3.5.1.1;
Step 3.5.1.7, judge vp-〉v_packet_num % vp-〉whether pcr_demand_perpackets_v=0 set up, if, carry out step 3.5.1.8, if not, carry out step 3.5.1.9;
Step 3.5.1.8, (176-(length-nPesUsedByte)) the individual byte of padding in the PES grouping bag is squeezed in the current TS bag;
Carry out step 3.5.1.10;
Step 3.5.1.9, (184-(length-nPesUsedByte)) the individual byte of padding in the PES grouping bag is squeezed in the current TS bag;
Carry out step 3.5.1.10;
Step 3.5.1.10,
(length-nPesUsedByte) individual byte in the PES grouping bag is squeezed in the individual byte in end (length-nPesUsedByte) of current TS bag;
Output nTsPacketNum;
Wherein,
NTsPacketNum represents that this PES grouping bag has been packaged into what TS bag altogether;
Step 3.5.2, audio frequency PES grouping bag is played the TS bag;
Because do not comprise PCR information in the audio TS bag,, the order of the data that the syntax format of the TS bag of the transport stream of stipulating in only needing according to standard when bag that audio frequency PES is divided into groups is packaged into the TS bag wraps according to PES gets final product so writing them in the TS bag;
Step 3.5.3, calculating PCR value;
Equaling to fill in 1 the video TS bag PCR information toward pcr_flag is at the audio frequency and video TS bag with each road, PSI packets of information and fill bag and write these TS bags in the output file uniformly and write and finish in the output file process, finish the video PES on each road and audio frequency PES be packaged into the TS bag after, the video TS bag on each road and the number of audio TS bag have just been known, also have the PSI packets of information and fill the number of wrapping, so just can be very easily write these TS bags of this time period in the output file uniformly, to calculate PCR in the process that writes, in certain multiplexing time period, the computational methods of the PCR of TsNum bag are as follows:
int64_t?timestampll=(((nPacket_sal?frame*pcr_jisuan+TsNum)*188)/(TS_BITRATE/1000.0))*8.0;
pcr=(int64_t)(timestampll*90.0+0.5);
Wherein:
NPacket_salframe calculates in the step 1, promptly according to the transfer rate of wanting multiplexing transport stream, and this time span of in the pre-set time period input code flow being carried out statistic multiplexing how long, calculate at this section in the period, transport stream should have the output of what transport stream packets;
Pcr_jisuan represents it is which time period, whenever finishes the multiplexing of a time period, and this value is from adding 1;
TsNum represents that this video packets is which TS bag in this time period;
TS_BITRATE represents the transfer rate of the TS file of this output;
Step 3.6, judge whether that all PES grouping bag is all handled and be over, if, carry out step 3.1, if not, carry out step 3.5;
Step 4, judged whether that one road input file finishes dealing with, if, finish to withdraw from, if not, carry out step 2.
The invention provides a kind of method of statistics of multiplex transmission stream, the method is a kind of method of the simple and statistics of multiplex transmission stream that efficient is high, and following some advantage is arranged:
1) this statistic multiplexing algorithm is under the state that does not communicate with encoder, to looking the audio frequency Basic Flow File carries out the method for multichannel statistics;
2) can effectively improve the utilization rate of channel;
3) can make the speed of the multiplexing MPTS that obtains fully constant;
4) it is simple to look the algorithm of audio sync, and the method for namely calculating PTS/DTS is simple;
The method of 5) take frame as unit elementary stream being packed is so that decoder can be very easily at PES Obtain and to process in this information of the PES_packet_length of grouping bag stem (PES grouping bag is long) A frame video or the size of voice data, can according to this value well control VBV_buffer (depending on Frequently buffer detector buffering area), make its neither also underflow not of overflow;
6) can be according to the characteristics of input code flow and the requirement of output transport stream, at the instantaneous code of input file When rate is too high, according to the priority of input file, the B frame that loss priority is low, the P frame, until The instantaneous code check of input file satisfies the requirement of the transfer rate of MPTS, and the advantage of processing like this is, defeated The Rate Control that enters code stream is not in the very perfect situation, can finish the multiplexing of multi-channel program yet, and The multiplexing MPTS that goes out mosaic can not occur when playing.