OSDN Git Service

e43f9b3034da53f6276789e290e51a7219fe6ae0
[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=m2tpp.readPMTglobal_byte(tb, pmt_pid);
109                                 if (pmtss.size()>0){
110                                     int[] new_pids=new int[pmtss.size()+3];
111                                     new_pids[pmtss.size()]=0;
112                                     new_pids[pmtss.size()+1]=pmt_pid;
113                                     new_pids[pmtss.size()+2]=pmtss.get(0).PCR_PID;
114                                     for (int i=0;i<pmtss.size();i++){
115                                         new_pids[i]=pmtss.get(i).Elementary_PID;
116                                     }
117                                     if (!Arrays.equals(pids, new_pids)){
118                                         pids=new_pids;
119                                     }
120                                 }
121
122                             /*}else if ((ii==0x0012)&&(readti>187)){
123                                 ArrayList<EITData> eitss=m2tpp.readEIT(tb);
124                              */
125                             }else if (readti>187){
126                                 wbyte=tb;
127
128                             }else {
129                                 readend=true;
130                             }
131                             k=pids.length;
132                         }
133                     }
134                 ik++;
135             }
136             if (alreadyreaded==false){
137                 bis.skip(188);
138             }
139                 if (wbyte!=null){
140                     bos.write(wbyte);
141                 }
142             }
143             bis.close();
144             bos.flush();
145             bos.close();
146             in.close();
147             fos.close();
148
149         } catch (IOException ex) {
150             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
151         }
152     }
153     private PIDs getTablePID_byte(String fpath,int p_table){
154         /**
155          * PIDを取得し、これと思われる必要なPIDを抜き出す。
156          * @return プログラム番号(return[0])とPIDのリスト(return[1-])
157          */
158         FileInputStream in = null;
159         PIDs pids=new PIDs();
160         int[] reti=null;
161         try {
162             ArrayList<Integer> ret=new ArrayList<Integer>();
163             byte[] tb = new byte[188];
164             int[] ib = new int[188];
165             ArrayList<PATData> pat = new ArrayList<PATData>();
166             ArrayList<PMTData> pmt = new ArrayList<PMTData>();
167             String[] sb = new String[188];
168             in = new FileInputStream(fpath);
169             int imax=1000;
170             for (int i = 0; i < imax ; i++) {
171                 if (in.read(tb)==-1){
172                     in.close();
173                     System.out.println("Program Table and PMT not found.");
174                     System.exit(1);
175                 }
176                 String last8;
177                 Mpeg2TSPacket m2tp;
178                 m2tp = new Mpeg2TSPacket();
179                 m2tp.setPAT(pat);
180                 m2tp.setPMT(pmt);
181                 m2tp.readTS_byte(tb);
182                 pat = m2tp.getPAT();
183                 for (int i2=0;i2<pat.size();i2++){
184                     PATData patdd=pat.get(i2);
185                     if (pat.get(i2).Program_TABLE==p_table){
186                         pat.clear();
187                         pat.add(patdd);
188                         m2tp.setPAT(pat);
189                     }
190                 }
191                 ArrayList<PMTData> pmtt=m2tp.getPMT();
192                 for (int i2=0;i2<pmtt.size();i2++){
193                     if (!(pmt.contains(pmtt.get(i2)))){
194                         pmt.add(pmtt.get(i2));
195                     }
196                 }
197                 if ((i==imax-1)&&(pmt.size()==0)){
198                     imax=imax+500;
199                 }
200                 for (int i2=0;i2<pmt.size();i2++){
201                     int itt=ret.size();
202                     if (p_table==pmt.get(i2).Program_Table){
203                         if (!(ret.contains(pmt.get(i2).Elementary_PID))){
204                             ret.add(pmt.get(i2).Elementary_PID);
205                             i=imax;
206                         }
207                     }
208                     if (ret.size()==itt){
209                         imax=imax+100;
210                     }
211                 }
212             }
213             PATData patd_det=new PATData();
214             for (int i=0;i<pat.size();i++){
215                 if (pat.get(i).Program_TABLE==p_table){
216                     patd_det=pat.get(i);
217                     i=pat.size();
218                 }
219             }
220             ret.add(0);
221             ret.add(patd_det.PID);
222             ret.add(pmt.get(0).Elementary_PID);
223             for (int i=0;i<pmt.size();i++){
224                 if (patd_det.Program_TABLE==pmt.get(i).Program_Table){
225                     if (!(ret.contains(pmt.get(i).Elementary_PID))){
226                         if ((pmt.get(i).Stream_Type==0x02)||(pmt.get(i).Stream_Type==0x0f)){
227                             ret.add(pmt.get(i).Elementary_PID);
228                         }
229                     }
230                 }
231             }
232             pids.PMT_PID=patd_det.PID;
233             pids.Program_Table=patd_det.Program_TABLE;
234             pids.PIDs=new int[ret.size()];
235             for (int i=0;i<ret.size();i++){
236                 pids.PIDs[i]=ret.get(i);
237             }
238             in.close();
239         } catch (IOException ex) {
240             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
241         } finally {
242             try {
243                 in.close();
244             } catch (IOException ex) {
245                 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
246             }
247         }
248         return pids;
249     }
250     public Integer[] getProgramNum_byte(String fpath){
251             /**
252              * PIDを取得し、これと思われる必要なPIDを抜き出す。
253              * @return プログラム番号(return[0])とPIDのリスト(return[1-])
254              */
255             FileInputStream in = null;
256             ArrayList<Integer> retti = new ArrayList<Integer>();
257             byte[] tb = new byte[188];
258             ArrayList<PATData> pat = new ArrayList<PATData>();
259             ArrayList<PATData> pat_ok = new ArrayList<PATData>();
260             ArrayList<PMTData> pmt = new ArrayList<PMTData>();
261             try {
262             in = new FileInputStream(fpath);
263             int imax = 7000;
264             int mmax = 20000;
265             for (int i = 0; i < imax; i++) {
266                 if (in.read(tb) == -1) {
267                     in.close();
268                     System.out.println("Program Table not found.");
269                     System.exit(1);
270                 }
271                 String last8;
272                 Mpeg2TSPacket m2tp;
273                 m2tp = new Mpeg2TSPacket();
274                 m2tp.setPAT(pat);
275                 m2tp.setPMT(pmt);
276                 m2tp.readTS_byte(tb);
277                 pat = m2tp.getPAT();
278                 pmt = m2tp.getPMT();
279                 ArrayList<PMTData> pmtt=m2tp.getPMT();
280                 for (int i2=0;i2<pmtt.size();i2++){
281                     if (!(pmt.contains(pmtt.get(i2)))){
282                         pmt.add(pmtt.get(i2));
283                     }
284                 }
285
286                 if (i>imax-10 && pat_ok.size()==0 && i<mmax){
287                     imax=imax+100;
288                 }
289                 for (int i3=0;i3<pmt.size();i3++){
290                     if (pmt.get(i3).Stream_Type==0x02){
291                         for (int i4=0;i4<pat.size();i4++){
292                             if (pmt.get(i3).Program_Table==pat.get(i4).Program_TABLE && !(pat_ok.contains(pat.get(i4)))){
293                                 pat_ok.add(pat.get(i4));
294                             }
295                         }
296                     }
297                 }
298             }
299             for (int i5 = 0; i5 < pat_ok.size(); i5++) {
300                 PATData patdd = pat_ok.get(i5);
301                 if ((patdd.Program_TABLE != 0) &&(!(retti.contains(patdd.Program_TABLE)))){
302                     retti.add(patdd.Program_TABLE);
303                 }
304             }
305         } catch (IOException ex) {
306             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
307         }
308         Integer[] rt=retti.toArray(new Integer[retti.size()]);
309         return rt;
310     }
311     private int getFirstP_Table_byte(String fpath){
312         /**
313          * PIDを取得し、これと思われる必要なPIDを抜き出す。
314          * @return プログラム番号(return[0])とPIDのリスト(return[1-])
315          */
316         FileInputStream in = null;
317         int[] reti=null;
318         int[] pat_det=new int[2];
319         //pat_det[0]=0;
320         PATData patdd=new PATData();
321         patdd.PID=0;
322         try {
323             ArrayList ret=new ArrayList();
324             byte[] tb = new byte[188];
325             int[] ib = new int[188];
326             ArrayList<PATData> pat = new ArrayList<PATData>();
327             ArrayList<PMTData> pmt = new ArrayList<PMTData>();
328             String[] sb = new String[188];
329             in = new FileInputStream(fpath);
330             //int[] PIDCount = new int[8200];
331             int imax=1000;
332             calc cal=new calc();
333             for (int i = 0; i < imax ; i++) {
334                 in.read(tb);
335                 String last8;
336                 Mpeg2TSPacket m2tp;
337                 m2tp = new Mpeg2TSPacket();
338                 m2tp.setPAT(pat);
339                 m2tp.setPMT(pmt);
340                 m2tp.readTS_byte(tb);
341                 pat = m2tp.getPAT();
342                 pmt = m2tp.getPMT();
343                 if ((i==imax-1)&&(pmt.size()==0)){
344                     imax=imax+500;
345                 }
346             }
347
348             for (int i=0;i<pat.size();i++){
349                 if (pat.get(i).Program_TABLE>0){
350                     patdd=pat.get(i);
351                     i=pat.size();
352                 }
353             }
354         } catch (IOException ex) {
355             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
356         } finally {
357             try {
358                 in.close();
359             } catch (IOException ex) {
360                 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
361             }
362         }
363         return patdd.Program_TABLE;
364     }
365 }