OSDN Git Service

add chlist output option.
[rec10/rec10-git.git] / jTsSplitter / trunk / src / jtssplitter / Tsfile.java
index e70791c..dfa2b74 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
+ * jTsSplitter - java based mpeg2ts splitter.
+ * Copyright (C) 2009-2010 Yukikaze
  */
 
 package jtssplitter;
@@ -58,74 +58,78 @@ public class Tsfile {
                 //}
                 boolean alreadyreaded=false;
                 for (int k=0;k<pids.length;k++){
-                    if (ii==pids[k]){
-                        alreadyreaded=true;
-                        int readti=bis.read(tb);
-                        if ((ii==0)&&(readti>187)){
-                            Mpeg2TSPacket m2tp2=new Mpeg2TSPacket();
-                            m2tp2.readTS_byte(tb);
-                            ArrayList<PATData> pats=new ArrayList<PATData>();
-                            pats=m2tp2.getPAT();
-                            boolean containp_table=false;
-                            for (int iii=0;iii<pats.size();iii++){
-                                if (pats.get(iii).Program_TABLE==p_table){
-                                    containp_table=true;
-                                }
-                            }
-                            if (!containp_table){
+                    if (alreadyreaded==false){
+                        if (ii==pids[k]){
+                            alreadyreaded=true;
+                            int readti=bis.read(tb);
+                            if ((ii==0)&&(readti>187)){
+                                Mpeg2TSPacket m2tp2=new Mpeg2TSPacket();
+                                m2tp2.readTS_byte(tb);
+                                ArrayList<PATData> pats=new ArrayList<PATData>();
+                                pats=m2tp2.getPAT();
+                                boolean containp_table=false;
                                 for (int iii=0;iii<pats.size();iii++){
-                                    if (pats.get(iii).Program_TABLE>0){
-                                        System.out.println("Program Tableの変更を検知"+Integer.toString(p_table)+" to "+Integer.toString(pats.get(iii).Program_TABLE));
-                                        p_table=pats.get(iii).Program_TABLE;
-                                        for (int it=0;it<pids.length;it++){
-                                            if (pids[it]==pmt_pid){
-                                                pmt_pid=pats.get(iii).PID;
-                                                pids[it]=pmt_pid;
+                                    if (pats.get(iii).Program_TABLE==p_table){
+                                        containp_table=true;
+                                    }else if (csch > 0){
+                                        containp_table=true;
+                                    }
+                                }
+                                if (!containp_table){
+                                    for (int iii=0;iii<pats.size();iii++){
+                                        if (pats.get(iii).Program_TABLE>0){
+                                            System.out.println("Program Tableの変更を検知"+Integer.toString(p_table)+" to "+Integer.toString(pats.get(iii).Program_TABLE));
+                                            p_table=pats.get(iii).Program_TABLE;
+                                            for (int it=0;it<pids.length;it++){
+                                                if (pids[it]==pmt_pid){
+                                                    pmt_pid=pats.get(iii).PID;
+                                                    pids[it]=pmt_pid;
+                                                }
                                             }
+                                            iii=pats.size();
                                         }
-                                        iii=pats.size();
                                     }
                                 }
-                            }
 
-                            wbyte=m2tpp.splitPAT_byte(tb,p_table);
-                            //wbyte=m2tpp.splitPAT(tb, p_table);
-                            /*byte[] wbytet=m2tpp.splitPAT(tb, p_table);
-                            for (int kk=0;kk<wbyte.length;kk++){
-                                if (wbyte[kk]==wbytet[kk]){
-                                    //System.out.println(":match");
-                                }else{
-                                    System.out.print(kk);
-                                    System.out.println(":not match");
-                                }
-                            }*/
-                            //wbyte=wbytet;*/
-                        }else if ((ii==pmt_pid)&&(readti>187)){
-                            wbyte=tb;
-                            ArrayList<PMTData> pmtss=m2tpp.readPMTglobal_byte(tb, pmt_pid);
-                            if (pmtss.size()>0){
-                                int[] new_pids=new int[pmtss.size()+3];
-                                new_pids[pmtss.size()]=0;
-                                new_pids[pmtss.size()+1]=pmt_pid;
-                                new_pids[pmtss.size()+2]=pmtss.get(0).PCR_PID;
-                                for (int i=0;i<pmtss.size();i++){
-                                    new_pids[i]=pmtss.get(i).Elementary_PID;
-                                }
-                                if (!Arrays.equals(pids, new_pids)){
-                                    pids=new_pids;
+                                wbyte=m2tpp.splitPAT_byte(tb,p_table);
+                                //wbyte=m2tpp.splitPAT(tb, p_table);
+                                /*byte[] wbytet=m2tpp.splitPAT(tb, p_table);
+                                for (int kk=0;kk<wbyte.length;kk++){
+                                    if (wbyte[kk]==wbytet[kk]){
+                                        //System.out.println(":match");
+                                    }else{
+                                        System.out.print(kk);
+                                        System.out.println(":not match");
+                                    }
+                                }*/
+                                //wbyte=wbytet;*/
+                            }else if ((ii==pmt_pid)&&(readti>187)){
+                                wbyte=tb;
+                                ArrayList<PMTData> pmtss=m2tpp.readPMTglobal_byte(tb, pmt_pid);
+                                if (pmtss.size()>0){
+                                    int[] new_pids=new int[pmtss.size()+3];
+                                    new_pids[pmtss.size()]=0;
+                                    new_pids[pmtss.size()+1]=pmt_pid;
+                                    new_pids[pmtss.size()+2]=pmtss.get(0).PCR_PID;
+                                    for (int i=0;i<pmtss.size();i++){
+                                        new_pids[i]=pmtss.get(i).Elementary_PID;
+                                    }
+                                    if (!Arrays.equals(pids, new_pids)){
+                                        pids=new_pids;
+                                    }
                                 }
-                            }
 
-                        /*}else if ((ii==0x0012)&&(readti>187)){
-                            ArrayList<EITData> eitss=m2tpp.readEIT(tb);
-                         */
-                        }else if (readti>187){
-                            wbyte=tb;
+                            /*}else if ((ii==0x0012)&&(readti>187)){
+                                ArrayList<EITData> eitss=m2tpp.readEIT(tb);
+                             */
+                            }else if (readti>187){
+                                wbyte=tb;
 
-                        }else {
-                            readend=true;
+                            }else {
+                                readend=true;
+                            }
+                            k=pids.length;
                         }
-                        k=pids.length;
                     }
                 ik++;
             }
@@ -162,16 +166,18 @@ public class Tsfile {
             ArrayList<PMTData> pmt = new ArrayList<PMTData>();
             String[] sb = new String[188];
             in = new FileInputStream(fpath);
-            //int[] PIDCount = new int[8200];
             int imax=1000;
             for (int i = 0; i < imax ; i++) {
-                in.read(tb);
+                if (in.read(tb)==-1){
+                    in.close();
+                    System.out.println("Program Table and PMT not found.");
+                    System.exit(1);
+                }
                 String last8;
                 Mpeg2TSPacket m2tp;
                 m2tp = new Mpeg2TSPacket();
                 m2tp.setPAT(pat);
                 m2tp.setPMT(pmt);
-                //m2tp.readTS(tb);
                 m2tp.readTS_byte(tb);
                 pat = m2tp.getPAT();
                 for (int i2=0;i2<pat.size();i2++){
@@ -241,6 +247,43 @@ public class Tsfile {
         }
         return pids;
     }
+    public Integer[] getProgramNum_byte(String fpath){
+            /**
+             * PIDを取得し、これと思われる必要なPIDを抜き出す。
+             * @return プログラム番号(return[0])とPIDのリスト(return[1-])
+             */
+            FileInputStream in = null;
+            ArrayList<Integer> retti = new ArrayList<Integer>();
+            byte[] tb = new byte[188];
+            ArrayList<PATData> pat = new ArrayList<PATData>();
+            try {
+            in = new FileInputStream(fpath);
+            int imax = 1000;
+            for (int i = 0; i < imax; i++) {
+                if (in.read(tb) == -1) {
+                    in.close();
+                    System.out.println("Program Table not found.");
+                    System.exit(1);
+                }
+                String last8;
+                Mpeg2TSPacket m2tp;
+                m2tp = new Mpeg2TSPacket();
+                m2tp.setPAT(pat);
+                m2tp.readTS_byte(tb);
+                pat = m2tp.getPAT();
+                for (int i2 = 0; i2 < pat.size(); i2++) {
+                    PATData patdd = pat.get(i2);
+                    if ((patdd.Program_TABLE != 0) &&(!(retti.contains(patdd.Program_TABLE)))){
+                        retti.add(patdd.Program_TABLE);
+                    }
+                }
+            }
+        } catch (IOException ex) {
+            Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
+        }
+        Integer[] rt=retti.toArray(new Integer[retti.size()]);
+        return rt;
+    }
     private int getFirstP_Table_byte(String fpath){
         /**
          * PIDを取得し、これと思われる必要なPIDを抜き出す。