SHOW:
|
|
- or go back to the newest paste.
1 | - | /* //this version of getString doesn't work right |
1 | + | |
2 | var s=""; | |
3 | - | var c,ps="",i=0,e=L%4,x="\\x",z="0"; L-=e; |
3 | + | while(bi!=L){ |
4 | - | while(i!=L){ |
4 | + | s+=String.fromCharCode(this.getUint8(bi)); |
5 | - | c=this.getUint32(bi+i).toString(16); |
5 | + | bi++; |
6 | - | ps+=x+c[0]+(c[1]||z)+x+(c[2]||z)+(c[3]||z)+x+(c[4]||z)+(c[5]||z)+x+(c[6]||z)+(c[7]||z); |
6 | + | |
7 | - | i+=4; |
7 | + | |
8 | }; | |
9 | - | while(e--){ ps+=x+this.getUint8(bi+i).toString(16); |
9 | + | |
10 | - | i++; |
10 | + | |
11 | ||
12 | - | return eval('"'+ps+'"'); |
12 | + | //mp4 file to arraybuffer (byteArray), then to dataview |
13 | ||
14 | ||
15 | self.parse_mp4=function parse_mp4(dv,ind,L){ | |
16 | - | //good version: |
16 | + | var rez=[],clen,cuc=0,bL=dv.byteLength; ind=ind||0,L=L||bL; |
17 | if(ind>bL||L>bL||L<8||ind<0){ return "input atom indexes are invalid"; } | |
18 | - | var i=0,s=""; |
18 | + | while(ind<L){ |
19 | - | while(i!=L){ |
19 | + | clen=dv.getUint32(ind); if((clen+ind)>L) console.log("error atom length overflow @ "+ind); |
20 | - | s+=String.fromCharCode(this.getUint8(bi+i)); |
20 | + | else if(clen<8){ console.log("atom length underflow: "+clen+", skipped 4 bits @ "+ind); clen=4; } |
21 | - | i++; |
21 | + | else rez.push([dv.getString(ind+4,ind+8),ind,clen]); |
22 | ind+=clen; | |
23 | cuc++; if(cuc>65535){ console.log("infinite loop error @ "+ind); break; } | |
24 | } | |
25 | return rez; | |
26 | }; | |
27 | - | DataView.prototype.setString=function(bi,s){ |
27 | + | |
28 | - | var i=0,L=s.length; |
28 | + | |
29 | - | while(i!=L){ |
29 | + | //not use? |
30 | - | this.setUint8(bi+i,s.charCodeAt(i)); |
30 | + | self.parse_atoms=function parse_atoms(ay,dv){ |
31 | - | i++; |
31 | + | var i2=ay.length,c; |
32 | if(i2>0)while(i2--){ c=ay[i2]; c.push(parse_mp4(dv,c[1]+8,c[2]+c[1])); } | |
33 | }; | |
34 | ||
35 | ||
36 | self.parse_mp4_iter=function parse_mp4_iter(dv,ind,L,L0,Pi){ if(L0>165){ return "somthin ain't right..."; } | |
37 | - | self.strNui8=function strNui8(s,ui8,u8i){ |
37 | + | var rez=[],clen,cuc=0,ye,bL=dv.byteLength; ind=ind||0,L=L||bL; L0=L0||0; Pi=Pi||0; |
38 | - | var L=s.length,i=L; u8i=u8i||0; |
38 | + | if(ind>bL||L>bL||L<8||ind<0){ return "input atom indexes are invalid"; } |
39 | - | while(i--) ui8[u8i+i]=s.charCodeAt(i); |
39 | + | while(ind<L){ |
40 | clen=dv.getUint32(ind); if((clen+ind)>L) console.log("error atom length overflow @ "+ind); | |
41 | else if(clen<8){clen=4; console.log("atom length underflow: "+clen+", skipped 4 bits @ "+ind);} | |
42 | else{ rez.push([dv.getString(ind+4,ind+8),ind,clen,Pi,L0]); rez=rez.concat(parse_mp4_iter(dv,ind+8,ind+clen,(L0)+1,ind)); } | |
43 | - | self.ui8Gstr=function Ui8Gstr(ui8,i,L){ |
43 | + | ind+=clen; |
44 | cuc++; if(cuc>65535){ console.log("infinite loop error @ "+ind); break; } | |
45 | - | while(i!=L){ s+=String.fromCharCode(ui8[i]); i++; } |
45 | + | } |
46 | - | return s; |
46 | + | return rez; |
47 | }; | |
48 | ||
49 | self.rMap=function reverseMap(parsa){ //input rez (output of parse_mp4_iter) | |
50 | var map={},i=parsa.length; | |
51 | while(i--){ map[parsa[i][1]]=i; } | |
52 | parsa.pm=map; //parent map; | |
53 | return map; | |
54 | }; | |
55 | ||
56 | ||
57 | self.parse_stco=function parse_stco(stco,mp4Dv){ //input stco entry array from parse_mp4_iter | |
58 | ||
59 | var si=stco[1],lim=si+stco[2], st=si+16,ne=mp4Dv.getUint32(si+12),i=st, | |
60 | ent=[]; //chunck index entries; | |
61 | ||
62 | while(i<lim){ ent.push(mp4Dv.getUint32(i)); i+=4; } | |
63 | if(ent.length!=ne)console.error("error, chunk entry count is wrong?"); | |
64 | return ent; | |
65 | }; | |
66 | ||
67 | ||
68 | //ex: | |
69 | //rez[i]=[name,RealIndex,RealLength] //don't forget to add 8 to RealIndex (the length of the length store plus the type store) | |
70 | ||
71 | ||
72 | ||
73 | ||
74 | ||
75 | //will need? https://web.archive.org/web/20201111155659im_/https://sole.github.io/Animated_GIF/dist/Animated_GIF.js | |
76 | ||
77 | /* | |
78 | finp=document.querySelector("input[type=file]"); | |
79 | fi=finp.files[0]; | |
80 | fi.arrayBuffer().then(function(a){ self.mp4Dv=new DataView(a); | |
81 | ||
82 | self.rez2=parse_mp4(mp4Dv); | |
83 | ||
84 | ||
85 | ||
86 | }); | |
87 | ||
88 | ||
89 | */ | |
90 |