OSDN Git Service

941bb9a5b7ae01fc4d37e2f3ec33206fd0941871
[rec10/rec10-git.git] / jTsSplitter / trunk / src / jtssplitter / Tsfile.java
1 /*
2  * jTsSplitter - java based mpeg2ts splitter.
3  * Copyright (C) 2009-2012 Yukikaze
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 import java.util.Arrays;
12 import jtssplitter.data.PATData;
13 import jtssplitter.data.PIDs;
14 import jtssplitter.data.PMTData;
15 /**
16  *
17  * @author Administrator
18  */
19 public class Tsfile {
20     public void splitTS_byte(String origpath,String destpath,int csch){
21         int[] pids=null;
22         int p_table=0;
23         if (csch>0){
24             p_table=csch;
25         }else{
26             p_table=getFirstP_Table_byte(origpath);
27         }
28         PIDs pidss=getTablePID_byte(origpath,p_table);
29         p_table=pidss.Program_Table;
30         pids=pidss.PIDs;
31         int pmt_pid=pidss.PMT_PID;
32         System.out.println("番組の同定終了");
33         System.out.println("Program Table : "+Integer.toString(p_table));
34         byte[] tb=new byte[188];
35         try {
36             FileInputStream in = new FileInputStream(origpath);
37             FileOutputStream fos=new FileOutputStream(destpath);
38             BufferedInputStream bis=new BufferedInputStream(in,128*188);
39             BufferedOutputStream bos=new BufferedOutputStream(fos);
40             byte[] wbyte=null;
41             boolean end=false;
42             int ik=0;
43             boolean readend=false;
44             Mpeg2TSPacket m2tpp;
45             m2tpp=new Mpeg2TSPacket();
46             while (readend==false){
47                 byte[] tstt=new byte[3];
48                 bis.mark(188*2);
49                 bis.read(tstt);
50                 bis.reset();
51                 wbyte=null;
52                 Mpeg2TSPacket m2tp;
53                 m2tp=new Mpeg2TSPacket();
54                 //int ii=m2tpp.getPIDFirst(tstt);
55                 int ii=m2tpp.getPIDFirst_byte(tstt);
56                 //if (ii!=i2){
57                 //    System.out.println("not match");
58                 //}
59                 boolean alreadyreaded=false;
60                 for (int k=0;k<pids.length;k++){
61                     if (alreadyreaded==false){
62                         if (ii==pids[k]){
63                             alreadyreaded=true;
64                             int readti=bis.read(tb);
65                             if ((ii==0)&&(readti>187)){
66                                 Mpeg2TSPacket m2tp2=new Mpeg2TSPacket();
67                                 m2tp2.readTS_byte(tb);
68                                 ArrayList<PATData> pats=new ArrayList<PATData>();
69                                 pats=m2tp2.getPAT();
70                                 boolean containp_table=false;
71                                 for (int iii=0;iii<pats.size();iii++){
72                                     if (pats.get(iii).Program_TABLE==p_table){
73                                         containp_table=true;
74                                     }else if (csch > 0){
75                                         containp_table=true;
76                                     }
77                                 }
78                                 if (!containp_table){
79                                     for (int iii=0;iii<pats.size();iii++){
80                                         if (pats.get(iii).Program_TABLE>0){
81                                             System.out.println("Program Tableの変更を検知"+Integer.toString(p_table)+" to "+Integer.toString(pats.get(iii).Program_TABLE));
82                                             p_table=pats.get(iii).Program_TABLE;
83                                             for (int it=0;it<pids.length;it++){
84                                                 if (pids[it]==pmt_pid){
85                                                     pmt_pid=pats.get(iii).PID;
86                                                     pids[it]=pmt_pid;
87                                                 }
88                                             }
89                                             iii=pats.size();
90                                         }
91                                     }
92                                 }
93
94                                 wbyte=m2tpp.splitPAT_byte(tb,p_table);
95                                 //wbyte=m2tpp.splitPAT(tb, p_table);
96                                 /*byte[] wbytet=m2tpp.splitPAT(tb, p_table);
97                                 for (int kk=0;kk<wbyte.length;kk++){
98                                     if (wbyte[kk]==wbytet[kk]){
99                                         //System.out.println(":match");
100                                     }else{
101                                         System.out.print(kk);
102                                         System.out.println(":not match");
103                                     }
104                                 }*/
105                                 //wbyte=wbytet;*/
106                             }else if ((ii==pmt_pid)&&(readti>187)){
107                                 wbyte=tb;
108                                 ArrayList<PMTData> pmtss;
109                                 try{
110                                     pmtss=m2tpp.readPMTglobal_byte(tb, pmt_pid);
111                                 }catch(NullPointerException e){
112                                     pmtss=new ArrayList<PMTData>();
113                                 }
114                                 if (pmtss.size()>0){
115                                     int[] new_pids=new int[pmtss.size()+3];
116                                     new_pids[pmtss.size()]=0;
117                                     new_pids[pmtss.size()+1]=pmt_pid;
118                                     new_pids[pmtss.size()+2]=pmtss.get(0).PCR_PID;
119                                     for (int i=0;i<pmtss.size();i++){
120                                         new_pids[i]=pmtss.get(i).Elementary_PID;
121                                     }
122                                     if (!Arrays.equals(pids, new_pids)){
123                                         pids=new_pids;
124                                     }
125                                 }
126
127                             /*}else if ((ii==0x0012)&&(readti>187)){
128                                 ArrayList<EITData> eitss=m2tpp.readEIT(tb);
129                              */
130                             }else if (readti>187){
131                                 wbyte=tb;
132
133                             }else {
134                                 readend=true;
135                             }
136                             k=pids.length;
137                         }
138                     }
139                 ik++;
140             }
141             if (alreadyreaded==false){
142                 bis.skip(188);
143             }
144                 if (wbyte!=null){
145                     bos.write(wbyte);
146                 }
147             }
148             bis.close();
149             bos.flush();
150             bos.close();
151             in.close();
152             fos.close();
153
154         } catch (IOException ex) {
155             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
156         }
157     }
158     private PIDs getTablePID_byte(String fpath,int p_table){
159         /**
160          * PIDを取得し、これと思われる必要なPIDを抜き出す。
161          * @return プログラム番号(return[0])とPIDのリスト(return[1-])
162          */
163         FileInputStream in = null;
164         PIDs pids=new PIDs();
165         int[] reti=null;
166         try {
167             ArrayList<Integer> ret=new ArrayList<Integer>();
168             byte[] tb = new byte[188];
169             int[] ib = new int[188];
170             ArrayList<PATData> pat = new ArrayList<PATData>();
171             ArrayList<PMTData> pmt = new ArrayList<PMTData>();
172             String[] sb = new String[188];
173             in = new FileInputStream(fpath);
174             int imax=1000;
175             for (int i = 0; i < imax ; i++) {
176                 if (in.read(tb)==-1){
177                     in.close();
178                     System.out.println("Program Table and PMT not found.");
179                     System.exit(1);
180                 }
181                 String last8;
182                 Mpeg2TSPacket m2tp;
183                 m2tp = new Mpeg2TSPacket();
184                 m2tp.setPAT(pat);
185                 m2tp.setPMT(pmt);
186                 m2tp.readTS_byte(tb);
187                 pat = m2tp.getPAT();
188                 for (int i2=0;i2<pat.size();i2++){
189                     PATData patdd=pat.get(i2);
190                     if (pat.get(i2).Program_TABLE==p_table){
191                         pat.clear();
192                         pat.add(patdd);
193                         m2tp.setPAT(pat);
194                     }
195                 }
196                 ArrayList<PMTData> pmtt=m2tp.getPMT();
197                 for (int i2=0;i2<pmtt.size();i2++){
198                     if (!(pmt.contains(pmtt.get(i2)))){
199                         pmt.add(pmtt.get(i2));
200                     }
201                 }
202                 if ((i==imax-1)&&(pmt.size()==0)){
203                     imax=imax+500;
204                 }
205                 for (int i2=0;i2<pmt.size();i2++){
206                     int itt=ret.size();
207                     if (p_table==pmt.get(i2).Program_Table){
208                         if (!(ret.contains(pmt.get(i2).Elementary_PID))){
209                             ret.add(pmt.get(i2).Elementary_PID);
210                             i=imax;
211                         }
212                     }
213                     if (ret.size()==itt){
214                         imax=imax+100;
215                     }
216                 }
217             }
218             PATData patd_det=new PATData();
219             for (int i=0;i<pat.size();i++){
220                 if (pat.get(i).Program_TABLE==p_table){
221                     patd_det=pat.get(i);
222                     i=pat.size();
223                 }
224             }
225             ret.add(0);
226             ret.add(patd_det.PID);
227             ret.add(pmt.get(0).Elementary_PID);
228             for (int i=0;i<pmt.size();i++){
229                 if (patd_det.Program_TABLE==pmt.get(i).Program_Table){
230                     if (!(ret.contains(pmt.get(i).Elementary_PID))){
231                         if ((pmt.get(i).Stream_Type==0x02)||(pmt.get(i).Stream_Type==0x0f)){
232                             ret.add(pmt.get(i).Elementary_PID);
233                         }
234                     }
235                 }
236             }
237             pids.PMT_PID=patd_det.PID;
238             pids.Program_Table=patd_det.Program_TABLE;
239             pids.PIDs=new int[ret.size()];
240             for (int i=0;i<ret.size();i++){
241                 pids.PIDs[i]=ret.get(i);
242             }
243             in.close();
244         } catch (IOException ex) {
245             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
246         } finally {
247             try {
248                 in.close();
249             } catch (IOException ex) {
250                 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
251             }
252         }
253         return pids;
254     }
255     public Integer[] getProgramNum_byte(String fpath){
256             /**
257              * PIDを取得し、これと思われる必要なPIDを抜き出す。
258              * @return プログラム番号(return[0])とPIDのリスト(return[1-])
259              */
260             FileInputStream in = null;
261             ArrayList<Integer> retti = new ArrayList<Integer>();
262             byte[] tb = new byte[188];
263             ArrayList<PATData> pat = new ArrayList<PATData>();
264             ArrayList<PATData> pat_ok = new ArrayList<PATData>();
265             ArrayList<PMTData> pmt = new ArrayList<PMTData>();
266             try {
267             in = new FileInputStream(fpath);
268             int imax = 7000;
269             int mmax = 20000;
270             for (int i = 0; i < imax; i++) {
271                 if (in.read(tb) == -1) {
272                     in.close();
273                     System.out.println("Program Table not found.");
274                     System.exit(1);
275                 }
276                 String last8;
277                 Mpeg2TSPacket m2tp;
278                 m2tp = new Mpeg2TSPacket();
279                 m2tp.setPAT(pat);
280                 m2tp.setPMT(pmt);
281                 m2tp.readTS_byte(tb);
282                 pat = m2tp.getPAT();
283                 pmt = m2tp.getPMT();
284                 ArrayList<PMTData> pmtt=m2tp.getPMT();
285                 for (int i2=0;i2<pmtt.size();i2++){
286                     if (!(pmt.contains(pmtt.get(i2)))){
287                         pmt.add(pmtt.get(i2));
288                     }
289                 }
290
291                 if (i>imax-10 && pat_ok.size()==0 && i<mmax){
292                     imax=imax+100;
293                 }
294                 for (int i3=0;i3<pmt.size();i3++){
295                     if (pmt.get(i3).Stream_Type==0x02){
296                         for (int i4=0;i4<pat.size();i4++){
297                             if (pmt.get(i3).Program_Table==pat.get(i4).Program_TABLE && !(pat_ok.contains(pat.get(i4)))){
298                                 pat_ok.add(pat.get(i4));
299                             }
300                         }
301                     }
302                 }
303             }
304             for (int i5 = 0; i5 < pat_ok.size(); i5++) {
305                 PATData patdd = pat_ok.get(i5);
306                 if ((patdd.Program_TABLE != 0) &&(!(retti.contains(patdd.Program_TABLE)))){
307                     retti.add(patdd.Program_TABLE);
308                 }
309             }
310         } catch (IOException ex) {
311             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
312         }
313         Integer[] rt=retti.toArray(new Integer[retti.size()]);
314         return rt;
315     }
316     private int getFirstP_Table_byte(String fpath){
317         /**
318          * PIDを取得し、これと思われる必要なPIDを抜き出す。
319          * @return プログラム番号(return[0])とPIDのリスト(return[1-])
320          */
321         FileInputStream in = null;
322         int[] reti=null;
323         int[] pat_det=new int[2];
324         //pat_det[0]=0;
325         PATData patdd=new PATData();
326         patdd.PID=0;
327         try {
328             ArrayList ret=new ArrayList();
329             byte[] tb = new byte[188];
330             int[] ib = new int[188];
331             ArrayList<PATData> pat = new ArrayList<PATData>();
332             ArrayList<PMTData> pmt = new ArrayList<PMTData>();
333             String[] sb = new String[188];
334             in = new FileInputStream(fpath);
335             //int[] PIDCount = new int[8200];
336             int imax=1000;
337             calc cal=new calc();
338             for (int i = 0; i < imax ; i++) {
339                 in.read(tb);
340                 String last8;
341                 Mpeg2TSPacket m2tp;
342                 m2tp = new Mpeg2TSPacket();
343                 m2tp.setPAT(pat);
344                 m2tp.setPMT(pmt);
345                 m2tp.readTS_byte(tb);
346                 pat = m2tp.getPAT();
347                 pmt = m2tp.getPMT();
348                 if ((i==imax-1)&&(pmt.size()==0)){
349                     imax=imax+500;
350                 }
351             }
352
353             for (int i=0;i<pat.size();i++){
354                 if (pat.get(i).Program_TABLE>0){
355                     patdd=pat.get(i);
356                     i=pat.size();
357                 }
358             }
359         } catch (IOException ex) {
360             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
361         } finally {
362             try {
363                 in.close();
364             } catch (IOException ex) {
365                 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
366             }
367         }
368         return patdd.Program_TABLE;
369     }
370 }