OSDN Git Service

make it faster.
[rec10/rec10-git.git] / jTsSplitter / trunk / src / jtssplitter / Tsfile.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package jtssplitter;
7 import java.util.ArrayList;
8 import java.io.*;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11 /**
12  *
13  * @author Administrator
14  */
15 public class Tsfile {
16     private ArrayList tsstream=new ArrayList();
17     public void readTs(String fpath){
18         byte[] tb=new byte[188];
19         int[] ib=new int[188];
20         ArrayList pat=new ArrayList();
21         ArrayList pmt=new ArrayList();
22         String[] sb=new String[188];
23         try {
24             FileInputStream in = new FileInputStream(fpath);
25             int[] PIDCount=new int[8200];
26             for (int i=0;i<3000;i++){
27                 in.read(tb);
28                 String last8;
29                 Mpeg2TSPacket m2tp;
30                 m2tp=new Mpeg2TSPacket();
31                 m2tp.setPAT(pat);
32                 m2tp.setPMT(pmt);
33                 m2tp.readTS(tb);
34                 pat=m2tp.getPAT();
35                 pmt=m2tp.getPMT();
36                 PIDCount[m2tp.getPID()]++;
37             }
38             for (int i=0;i<8200;i++){
39                 if (PIDCount[i]>0){
40                     //System.out.print(Integer.toString(i)+" : "+Integer.toString(PIDCount[i])+"\n");
41                 }
42                 //System.out.print(PIDCount[i]);
43             }
44             for (int i=0;i<188;i++){
45                 int it=tb[i]& 0xFF;
46                 ib[i]=it;
47                 String s=Integer.toBinaryString(it);
48                 if (s.length()<8){
49                     for (int i2=s.length();i2<8;i2++){
50                         s="0"+s;
51                     }
52                 sb[i]=s;
53                 //System.out.print(s+"\n");
54                 }
55             
56                 //s=s+"0"*(8-s.length());
57             }
58             //System.out.print(Integer.toHexString(ib[0])+"\n");
59             //System.out.print(s+"\n");
60         } catch (IOException ex) {
61             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
62         }
63
64     }
65     public void splitTS(String origpath,String destpath,int csch){
66         int[] pids=null;
67         int p_table=0;
68         if (csch>0){
69             p_table=csch;
70         }else{
71             p_table=getFirstP_Table(origpath);
72         }
73         int[] pidst=getTablePID(origpath,p_table);
74         p_table=pidst[0];
75         pids=new int[pidst.length-1];
76         for (int i=0;i<pidst.length-1;i++){
77             pids[i]=pidst[i+1];
78         }
79         byte[] tb=new byte[188];
80         int[] ib=new int[188];
81         ArrayList pat=new ArrayList();
82         ArrayList pmt=new ArrayList();
83         String[] sb=new String[188];
84         try {
85             FileInputStream in = new FileInputStream(origpath);
86             FileOutputStream fos=new FileOutputStream(destpath,true);
87             //int[] PIDCount=new int[8200];
88             byte[] wbyte=null;
89             for (int i=0;in.read(tb)!=-1;i++){
90                 wbyte=null;
91                 Mpeg2TSPacket m2tp;
92                 m2tp=new Mpeg2TSPacket();
93                 m2tp.readTS(tb);
94                 for (int j=0;j<pids.length;j++){
95                     if (m2tp.getPID()==pids[j]){
96                         if (m2tp.getPID()==0){
97                             Mpeg2TSPacket m2tp2=new Mpeg2TSPacket();
98                             //m2tp2.readTS(tb);
99                             wbyte=m2tp2.splitPAT(tb,p_table);
100                             //wbyte=tb;
101                         }else{
102                             wbyte=tb;
103                         }
104                     }
105                 }
106                 if (wbyte!=null){
107                     fos.write(wbyte);
108                 }
109             }
110             in.close();
111             fos.close();
112         } catch (IOException ex) {
113             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
114         }
115     }
116     private void writeTS(byte[] tspacket,String path){
117         FileOutputStream fos = null;
118         try {
119             fos = new FileOutputStream(path,true);
120             fos.write(tspacket);
121             fos.close();
122         } catch (IOException ex) {
123             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
124         } finally {
125             try {
126                 fos.close();
127             } catch (IOException ex) {
128                 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
129             }
130         }
131     }
132     private int[] getTablePID(String fpath,int p_table){
133         /**
134          * PIDを取得し、これと思われる必要なPIDを抜き出す。
135          * @return プログラム番号(return[0])とPIDのリスト(return[1-])
136          */
137         FileInputStream in = null;
138         int[] reti=null;
139         try {
140             ArrayList ret=new ArrayList();
141             byte[] tb = new byte[188];
142             int[] ib = new int[188];
143             ArrayList pat = new ArrayList();
144             ArrayList pmt = new ArrayList();
145             String[] sb = new String[188];
146             in = new FileInputStream(fpath);
147             //int[] PIDCount = new int[8200];
148             int imax=1000;
149             for (int i = 0; i < imax ; i++) {
150                 in.read(tb);
151                 String last8;
152                 Mpeg2TSPacket m2tp;
153                 m2tp = new Mpeg2TSPacket();
154                 m2tp.setPAT(pat);
155                 m2tp.setPMT(pmt);
156                 m2tp.readTS(tb);
157                 pat = m2tp.getPAT();
158                 for (int i2=0;i2<pat.size();i2++){
159                     int[] il2=(int [])pat.get(i2);
160                     if (il2[0]==p_table){
161                         pat.clear();
162                         pat.add(il2);
163                         m2tp.setPAT(pat);
164                     }
165                 }
166                 ArrayList pmtt=m2tp.getPMT();
167                 for (int i2=0;i2<pmtt.size();i2++){
168                     if (!(pmt.contains(pmtt.get(i2)))){
169                         pmt.add(pmtt.get(i2));
170                     }
171                 }
172                 if ((i==imax-1)&&(pmt.size()==0)){
173                     imax=imax+500;
174                 }
175                 for (int i2=0;i2<pmt.size();i2++){
176                     int[] il2=(int[])pmt.get(i2);
177                     int itt=ret.size();
178                     if (p_table==il2[2]){
179                         if (!(ret.contains(il2[1]))){
180                             ret.add(il2[1]);
181                             i=imax;
182                         }
183                     }
184                     if (ret.size()==itt){
185                         imax=imax+100;
186                     }
187                 }
188             }
189             int[] pat_det=new int[2];
190             for (int i=0;i<pat.size();i++){
191                 int[] ii2=(int[])pat.get(i);
192                 if (ii2[0]==p_table){
193                     pat_det[0]=ii2[0];
194                     pat_det[1]=ii2[1];
195                     i=pat.size();
196                 }
197             }
198             ret.add(0);
199             ret.add(pat_det[1]);
200             for (int i=0;i<pmt.size();i++){
201                 int[] il2=(int[])pmt.get(i);
202                 if (pat_det[0]==il2[2]){
203                     if (!(ret.contains(il2[1]))){
204                         ret.add(il2[1]);
205                     }
206                 }
207             }
208             reti=new int[ret.size()+1];
209             reti[0]=pat_det[0];
210             for (int i=0;i<ret.size();i++){
211                 int retit=(Integer)ret.get(i);
212                 reti[i+1]=retit;
213             }
214             in.close();
215         } catch (IOException ex) {
216             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
217         } finally {
218             try {
219                 in.close();
220             } catch (IOException ex) {
221                 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
222             }
223         }
224         return reti;
225     }
226     private int getFirstP_Table(String fpath){
227         /**
228          * PIDを取得し、これと思われる必要なPIDを抜き出す。
229          * @return プログラム番号(return[0])とPIDのリスト(return[1-])
230          */
231         FileInputStream in = null;
232         int[] reti=null;
233         int[] pat_det=new int[2];
234         pat_det[0]=0;
235         try {
236             ArrayList ret=new ArrayList();
237             byte[] tb = new byte[188];
238             int[] ib = new int[188];
239             ArrayList pat = new ArrayList();
240             ArrayList pmt = new ArrayList();
241             String[] sb = new String[188];
242             in = new FileInputStream(fpath);
243             //int[] PIDCount = new int[8200];
244             int imax=1000;
245             for (int i = 0; i < imax ; i++) {
246                 in.read(tb);
247                 String last8;
248                 Mpeg2TSPacket m2tp;
249                 m2tp = new Mpeg2TSPacket();
250                 m2tp.setPAT(pat);
251                 m2tp.setPMT(pmt);
252                 m2tp.readTS(tb);
253                 pat = m2tp.getPAT();
254                 pmt = m2tp.getPMT();
255                 //PIDCount[m2tp.getFirstP_Table()]++;
256                 if ((i==imax-1)&&(pmt.size()==0)){
257                     imax=imax+500;
258                 }
259             }
260             
261             for (int i=0;i<pat.size();i++){
262                 int[] ii2=(int[])pat.get(i);
263                 if (ii2[0]>0){
264                     pat_det[0]=ii2[0];
265                     pat_det[1]=ii2[1];
266                     i=pat.size();
267                 }
268             }
269             
270             /*ret.add(0);
271             ret.add(pat_det[1]);
272             for (int i=0;i<pmt.size();i++){
273                 int[] il2=(int[])pmt.get(i);
274                 if (pat_det[0]==il2[2]){
275                     if (!(ret.contains(il2[1]))){
276                         ret.add(il2[1]);
277                     }
278                 }
279             }
280             reti=new int[ret.size()+1];
281             reti[0]=pat_det[0];
282             for (int i=0;i<ret.size();i++){
283                 int retit=(Integer)ret.get(i);
284                 reti[i+1]=retit;
285             }
286             in.close();
287              */
288         } catch (IOException ex) {
289             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
290         } finally {
291             try {
292                 in.close();
293             } catch (IOException ex) {
294                 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
295             }
296         }
297         return pat_det[0];
298     }
299 }