You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
13 KiB
1 lines
13 KiB
"use strict";function H264SPSParser(){function a(){w=0,x=new Map}function b(a,b){var c=b,d=w+c>>k;return c=w+b&h,a[d]>>i-(c&i)&1}function c(a,c){var d=0,e=0,f=0;if(1===c)e=b(a,d);else for(var g=0;c>g;g++)f=b(a,g),e=(e<<1)+f;return w+=c,e}function d(a,c){for(var d=0,e=0,f=0,g=c;w+g<8*a.length&&!(e=b(a,g++));)d++;if(0===d)return w+=1,0;f=1<<d;for(var h=d-1;h>=0;h--,g++)e=b(a,g),f|=e<<h;var i=d*j+1;return w+=i,f-1}function e(a,b){var c=d(a,b);return 1&c?(c+1)/j:-c/j}function f(a){x.put("cpb_cnt_minus1",d(a,0)),x.put("bit_rate_scale",c(a,l)),x.put("cpb_size_scale",c(a,l));for(var b=x.get("cpb_cnt_minus1"),e=new Array(b),f=new Array(b),g=new Array(b),h=0;b>=h;h++)e[h]=d(a,0),f[h]=d(a,0),g[h]=c(a,1);x.put("bit_rate_value_minus1",e),x.put("cpb_size_value_minus1",f),x.put("cbr_flag",g),x.put("initial_cpb_removal_delay_length_minus1",c(a,m)),x.put("cpb_removal_delay_length_minus1",c(a,m)),x.put("dpb_output_delay_length_minus1",c(a,m)),x.put("time_offset_length",c(a,m))}function g(a){x.put("aspect_ratio_info_present_flag",c(a,1)),x.get("aspect_ratio_info_present_flag")&&(x.put("aspect_ratio_idc",c(a,o)),x.get("aspect_ratio_idc")===u&&(x.put("sar_width",c(a,r)),x.put("sar_height",c(a,r)))),x.put("overscan_info_present_flag",c(a,1)),x.get("overscan_info_present_flag")&&x.put("overscan_appropriate_flag",c(a,1)),x.put("video_signal_type_present_flag",c(a,1)),x.get("video_signal_type_present_flag")&&(x.put("video_format",c(a,k)),x.put("video_full_range_flag",c(a,1)),x.put("colour_description_present_flag",c(a,1)),x.get("colour_description_present_flag")&&(x.put("colour_primaries",c(a,o)),x.put("transfer_characteristics",c(a,o)),x.put("matrix_coefficients",c(a,o)))),x.put("chroma_loc_info_present_flag",c(a,1)),x.get("chroma_loc_info_present_flag")&&(x.put("chroma_sample_loc_type_top_field",d(a,0)),x.put("chroma_sample_loc_type_bottom_field",d(a,0))),x.put("timing_info_present_flag",c(a,1)),x.get("timing_info_present_flag")&&(x.put("num_units_in_tick",c(a,s)),x.put("time_scale",c(a,s)),x.put("fixed_frame_rate_flag",c(a,1))),x.put("nal_hrd_parameters_present_flag",c(a,1)),x.get("nal_hrd_parameters_present_flag")&&f(a),x.put("vcl_hrd_parameters_present_flag",c(a,1)),x.get("vcl_hrd_parameters_present_flag")&&f(a),(x.get("nal_hrd_parameters_present_flag")||x.get("vcl_hrd_parameters_present_flag"))&&x.put("low_delay_hrd_flag",c(a,1)),x.put("pic_struct_present_flag",c(a,1)),x.put("bitstream_restriction_flag",c(a,1)),x.get("bitstream_restriction_flag")&&(x.put("motion_vectors_over_pic_boundaries_flag",c(a,1)),x.put("max_bytes_per_pic_denom",d(a,0)),x.put("max_bits_per_mb_denom",d(a,0)))}var h=7,i=7,j=2,k=3,l=4,m=5,n=6,o=8,p=12,q=15,r=16,s=32,t=64,u=255,v=256,w=0,x=null;return a.prototype={parse:function(a){w=0,x.clear(),x.put("forbidden_zero_bit",c(a,1)),x.put("nal_ref_idc",c(a,j)),x.put("nal_unit_type",c(a,m)),x.put("profile_idc",c(a,o)),x.put("profile_compatibility",c(a,o)),x.put("level_idc",c(a,o)),x.put("seq_parameter_set_id",d(a,0));var b=x.get("profile_idc"),f=100,h=110,i=122,l=244,q=44,s=83,u=86,y=118,z=128,A=138,B=139,C=134;if((b===f||b===h||b===i||b===l||b===q||b===s||b===u||b===y||b===z||b===A||b===B||b===C)&&(x.put("chroma_format_idc",d(a,0)),x.get("chroma_format_idc")===k&&x.put("separate_colour_plane_flag",c(a,1)),x.put("bit_depth_luma_minus8",d(a,0)),x.put("bit_depth_chroma_minus8",d(a,0)),x.put("qpprime_y_zero_transform_bypass_flag",c(a,1)),x.put("seq_scaling_matrix_present_flag",c(a,1)),x.get("seq_scaling_matrix_present_flag"))){for(var D=x.get("chroma_format_idc")!==k?o:p,E=new Array(D),F=0;D>F;F++)if(E[F]=c(a,1),E[F])for(var G=n>F?r:t,H=8,I=8,J=0,K=0;G>K;K++)I&&(J=e(a,0),I=(H+J+v)%v),H=0===I?H:I;x.put("seq_scaling_list_present_flag",E)}if(x.put("log2_max_frame_num_minus4",d(a,0)),x.put("pic_order_cnt_type",d(a,0)),0===x.get("pic_order_cnt_type"))x.put("log2_max_pic_order_cnt_lsb_minus4",d(a,0));else if(1===x.get("pic_order_cnt_type")){x.put("delta_pic_order_always_zero_flag",c(a,1)),x.put("offset_for_non_ref_pic",e(a,0)),x.put("offset_for_top_to_bottom_field",e(a,0)),x.put("num_ref_frames_in_pic_order_cnt_cycle",d(a,0));for(var L=0;L<x.get("num_ref_frames_in_pic_order_cnt_cycle");L++)x.put("num_ref_frames_in_pic_order_cnt_cycle",e(a,0))}return x.put("num_ref_frames",d(a,0)),x.put("gaps_in_frame_num_value_allowed_flag",c(a,1)),x.put("pic_width_in_mbs_minus1",d(a,0)),x.put("pic_height_in_map_units_minus1",d(a,0)),x.put("frame_mbs_only_flag",c(a,1)),0===x.get("frame_mbs_only_flag")&&x.put("mb_adaptive_frame_field_flag",c(a,1)),x.put("direct_8x8_interence_flag",c(a,1)),x.put("frame_cropping_flag",c(a,1)),1===x.get("frame_cropping_flag")&&(x.put("frame_cropping_rect_left_offset",d(a,0)),x.put("frame_cropping_rect_right_offset",d(a,0)),x.put("frame_cropping_rect_top_offset",d(a,0)),x.put("frame_cropping_rect_bottom_offset",d(a,0))),x.put("vui_parameters_present_flag",c(a,1)),x.get("vui_parameters_present_flag")&&g(a),!0},getSizeInfo:function(){var a=0,b=0;0===x.get("chroma_format_idc")?a=b=0:1===x.get("chroma_format_idc")?a=b=j:x.get("chroma_format_idc")===j?(a=j,b=1):x.get("chroma_format_idc")===k&&(0===x.get("separate_colour_plane_flag")?a=b=1:1===x.get("separate_colour_plane_flag")&&(a=b=0));var c=x.get("pic_width_in_mbs_minus1")+1,d=x.get("pic_height_in_map_units_minus1")+1,e=(j-x.get("frame_mbs_only_flag"))*d,f=0,g=0,h=0,i=0;1===x.get("frame_cropping_flag")&&(f=x.get("frame_cropping_rect_left_offset"),g=x.get("frame_cropping_rect_right_offset"),h=x.get("frame_cropping_rect_top_offset"),i=x.get("frame_cropping_rect_bottom_offset"));var l=c*r*e*r,m=c*r-a*(f+g),n=e*r-b*(j-x.get("frame_mbs_only_flag"))*(h+i),o={width:m,height:n,decodeSize:l};return o},getSpsValue:function(a){return x.get(a)},getCodecInfo:function(){var a=x.get("profile_idc").toString(r),b=x.get("profile_compatibility")<q?"0"+x.get("profile_compatibility").toString(r):x.get("profile_compatibility").toString(r),c=x.get("level_idc").toString(r);return a+b+c}},new a}var H264Session=function(){function a(a){a!==I&&("video"===a?I="video":(I="canvas",o=!0,p=0,q.frameData.firstFrame=!0))}function b(a,b,c){var d="";return d=a*b>921600&&c===!1?"video":"canvas"}function c(){this.decoder=new H264Decoder,this.firstDiffTime=0,this.firstTime=0,this.lastMSW=0}var d=0,e=0,f=0,g=1048576,h=!1,i=(new Uint8Array(g),new Uint8Array(["0x00","0x00","0x00","0x01"]),new H264SPSParser),j=0,k=null,l=null,m=0,n=8e3,o=!1,p=0,q={frameData:null,timeStamp:null,initSegmentData:null,mediaSample:null,dropPercent:0,dropCount:0,codecInfo:"",playback:!1},r={timestamp:null,timezone:null},s={},t=null,u=null,v=!1,w=!1,x=0,y=0,z=786432,A=0,B=0,C=!1,D=0,E=null,F=0,G=null,H=0,I="",J=null,K=0,L=0,M=0,N=0,O={width:0,height:0},P=null;return c.prototype={setReturnCallback:function(a){this.rtpReturnCallback=a},setBufferfullCallback:function(a){null!==this.videoBufferList&&this.videoBufferList.setBufferFullCallback(a)},getVideoBuffer:function(a){return null!==this.videoBufferList?this.videoBufferList.searchNodeAt(a):void 0},clearBuffer:function(){null!==this.videoBufferList&&this.videoBufferList.clear()},findCurrent:function(){null!==this.videoBufferList&&this.videoBufferList.searchTimestamp(this.getTimeStamp())},setTimeStamp:function(a){this.timeData=a},getTimeStamp:function(){return this.timeData},ntohl:function(a){return(a[0]<<24)+(a[1]<<16)+(a[2]<<8)+a[3]>>>0},appendBuffer:function(a,b,c){var d=1048576;if(c+b.length>=a.length){var e=new Uint8Array(a.length+d);e.set(a,0),a=e}return a.set(b,c),a},getFramerate:function(){return M},setGovLength:function(a){J=a},getGovLength:function(){return J},setDecodingTime:function(a){this.decodingTime=a},getDropPercent:function(){return K},getDropCount:function(){return L},initStartTime:function(){this.firstDiffTime=0,this.calcGov=0},setCheckDelay:function(a){this.checkDelay=a},init:function(a){v=!1,h=!1,I=a,this.decoder.setIsFirstFrame(!1),this.videoBufferList=new VideoBufferList,this.firstDiffTime=0,this.checkDelay=!0,this.timeData=null},setFramerate:function(a){a>0&&"undefined"!=typeof a&&(M=a,null!==this.videoBufferList&&(this.videoBufferList.setMaxLength(6*M),this.videoBufferList.setBUFFERING(4*M)))},parseRTPData:function(c,g,J,K,L){{var M=null,Q={},R=(g[19]<<24)+(g[18]<<16)+(g[17]<<8)+g[16]>>>0,S=Date.UTC("20"+(R>>26),(R>>22&15)-1,R>>17&31,R>>12&31,R>>6&63,63&R)/1e3;L.timeStampmsw}if(S-=28800,I||253!==g[4]||(P=0===g[5]?!1:!0,I=b(L.width,L.height,P)),""!==I){if(0==this.firstTime)this.firstTime=S,this.lastMSW=0,G=(g[21]<<8)+g[20],r={timestamp:this.firstTime,timestamp_usec:0};else{var T,U=(g[21]<<8)+g[20];T=U>G?U-G:U+65535-G,this.lastMSW+=T,S>this.firstTime&&(this.lastMSW-=1e3),this.firstTime=S,r={timestamp:S,timestamp_usec:this.lastMSW},G=U}0!==this.getFramerate()&&"undefined"!=typeof this.getFramerate()||"undefined"==typeof this.getTimeStamp()||(this.setFramerate(Math.round(1e3/((r.timestamp-this.getTimeStamp().timestamp===0?0:1e3)+(r.timestamp_usec-this.getTimeStamp().timestamp_usec)))),debug.log("setFramerate"+Math.round(1e3/((r.timestamp-this.getTimeStamp().timestamp===0?0:1e3)+(r.timestamp_usec-this.getTimeStamp().timestamp_usec))))),this.setTimeStamp(r);for(var V=g[22],W=g.subarray(24+V,g.length-8),X=g.subarray(g.length-8,g.length),Y=((X[7]<<24)+(X[6]<<16)+(X[5]<<8)+X[4],[]),Z=0;Z<=W.length;)if(0==W[Z])if(0==W[Z+1])if(1==W[Z+2]){if(Y.push(Z),Z+=3,5==(31&W[Z])||1==(31&W[Z]))break}else 0==W[Z+2]?Z++:Z+=3;else Z+=2;else Z+=1;var $="P";d=(g[21]<<8)+g[20];for(var _,ab=0,Z=0;Z<Y.length;Z++)switch(M=W.subarray(Y[Z]+3,Y[Z+1]),_=31&W[Y[Z]+3]){default:break;case 1:$="P",ab=Y[Z]-1;break;case 5:$="I",ab=Y[Z]-1;break;case 28:break;case 7:i.parse(M);var bb=L;j=i.getSizeInfo().decodeSize,(null===k||null===l||k.width!==bb.width||k.height!==bb.height||l!==i.getCodecInfo())&&(v=!1,k=bb,l=i.getCodecInfo(),this.decoder.setIsFirstFrame(!1)),A=x=bb.width,B=y=bb.height,t=M,(O.width!=bb.width||O.height!=bb.height)&&(0!=O.width?(O.width=bb.width,O.height=bb.height,Q.resolution=O,Q.resolution.decodeMode=b(O.width,O.height,P),Q.resolution.encodeMode="h264"):(O.width=bb.width,O.height=bb.height,Q.decodeStart=O,Q.decodeStart.decodeMode=I,Q.decodeStart.encodeMode="h264"));break;case 8:u=M;break;case 6:break;case 9:}if(J&&o===!1&&(Q.backupData={stream:new Uint8Array(W),frameType:$,width:A,height:B,codecType:"h264"},null!==r.timestamp&&"undefined"!=typeof r.timestamp?Q.backupData.timestamp_usec=r.timestamp_usec:Q.backupData.timestamp=(d/90).toFixed(0)),"canvas"===I){var cb=1e3*r.timestamp+r.timestamp_usec;if(0==this.firstDiffTime?(m=0,this.firstDiffTime=Date.now()-cb,debug.log("firstDiff: "+N)):(0>cb-E&&(this.firstDiffTime=m+(Date.now()-cb).toFixed(0)),m=Date.now()-cb-this.firstDiffTime,0>m&&(this.firstDiffTime=0,m=0),m>n&&(Q.error={errorCode:101},this.rtpReturnCallback(Q))),E=cb,f!==j&&(debug.log("outputSize: "+f),f=j,this.decoder.setOutputSize(f)),o===!0&&"P"===$)return void(e=0);o===!0&&(o=!1),"I"===$&&2>p&&p++,q.frameData=null,(J!==!0||h!==!0)&&(q.frameData=this.decoder.decode(W)),q.timeStamp=null,e=0,r=null===r.timestamp?this.getTimeStamp():r,q.timeStamp=r}else{var db=null;if(v)q.initSegmentData=null;else{v=!0;var L={id:1,width:x,height:y,type:"video",profileIdc:i.getSpsValue("profile_idc"),profileCompatibility:0,levelIdc:i.getSpsValue("level_idc"),sps:[t],pps:[u],timescale:1e3,fps:this.getFramerate()};debug.log(JSON.stringify(L)),q.initSegmentData=mp4Remux.initSegment(L),q.codecInfo=i.getCodecInfo()}if(ab||debug.log("11111111111111111111111111111111111111111"),"I"===$){var eb=ab;db=W.subarray(eb,W.length)}else db=W.subarray(ab,W.length);var fb=db.length-4;db[0]=(4278190080&fb)>>>24,db[1]=(16711680&fb)>>>16,db[2]=(65280&fb)>>>8,db[3]=255&fb;var gb=this.getFramerate(),hb={duration:Math.round(1/gb*1e3),size:db.length,frame_time_stamp:null,frameDuration:null};if(h)hb.frame_time_stamp=d,q.frameData=new Uint8Array(db),q.mediaSample=hb;else{if(J===!1){if(hb.frame_time_stamp=1e3*r.timestamp+r.timestamp_usec-N,w===!1)hb.frame_time_stamp=0,N=1e3*r.timestamp+r.timestamp_usec,hb.frameDuration=0,s=hb,w=!0;else{var ib=s.frame_time_stamp,jb=hb.frame_time_stamp;hb.frameDuration=Math.abs(jb-ib),hb.frameDuration>3e3&&(hb.frameDuration=0),s=hb}q.frameData=new Uint8Array(db),q.mediaSample=hb}r=null===r.timestamp?this.getTimeStamp():r,q.timeStamp=r}e=0}var kb=A*B;if(h===!0){var lb=(H/F*100).toFixed(0)<60;kb>z?5e3>F?(a("video"),Q.decodeMode="video"):lb===!0?(a("video"),Q.decodeMode="video"):(a("canvas"),Q.decodeMode="canvas"):(a("canvas"),Q.decodeMode="canvas")}return q.playback=h,Q.decodedData=q,C===!0?("I"===$&&D++,2===D&&(D=0,C=!1),void debug.info("H264Session::stop")):void this.rtpReturnCallback(Q)}},findIFrame:function(){if(null!==this.videoBufferList){var a=this.videoBufferList.findIFrame();if(null===a||"undefined"==typeof a)return!1;var b={};return this.setTimeStamp(a.timeStamp),b.frameData=this.decoder.decode(a.buffer),b.timeStamp=a.timeStamp,b}},setInitSegment:function(){v=!1,k=null,l=null}},new c};
|