OSDN Git Service

fix bugs.
[rec10/rec10-git.git] / jTsSplitter / trunk / src / jtssplitter / Mpeg2TSPacket.java
index fabb24b..4dd7216 100644 (file)
@@ -1,3 +1,7 @@
+/*
+ * jTsSplitter - java based mpeg2ts splitter.
+ * Copyright (C) 2009-2010 Yukikaze
+ */
 package jtssplitter;
 
 import java.util.ArrayList;
@@ -7,10 +11,6 @@ import jtssplitter.data.PMTData;
 import jtssplitter.data.Descriptor;
 import jtssplitter.data.EITData;
 import java.io.ByteArrayOutputStream;
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
 
 /**
  *
@@ -118,8 +118,13 @@ public class Mpeg2TSPacket {
         if (payload_byte != null) {
             if ((PID == 0) && (begin_payload_unit)) {
                 //cal.showPAT(tsbyte);
-                pat_list_now = readPAT_byte(payload_byte);
-                pat_list_all.addAll(pat_list_now);
+                if (isPAT(payload_byte)){
+                    pat_list_now = readPAT_byte(payload_byte);
+                    if (pat_list_now.size()>0){
+                        pat_list_all=pat_list_now;
+                    }
+                    //pat_list_all.addAll(pat_list_now);
+                }
             }
             for (int i = 0; i < pat_list_now.size(); i++) {
                 if ((PID == pat_list_now.get(i).PID) && (PID != 0)) {
@@ -182,7 +187,7 @@ public class Mpeg2TSPacket {
 
 
                 PMTData pmtd = new PMTData();
-                if ((pmt_stream_type == 0x02) || (pmt_stream_type == 0x0f)) {
+                if ((pmt_stream_type == 0x02) || (pmt_stream_type == 0x0f)||(pmt_stream_type == 0x06)) {
                     pmtd.Stream_Type = pmt_stream_type;
                     pmtd.Program_Table = PAT_TABLE;
                     pmtd.Elementary_PID = elementary_PID;
@@ -194,6 +199,9 @@ public class Mpeg2TSPacket {
                 if ((cur_point > section_length * 8 - 1) || (cur_point > payload_temp.length * 8 - 11)) {
                     end = true;
                 }
+                if (payload_temp.length * 8 < cur_point + 40){
+                    end = true;
+                }
             } else {
                 end = true;
             }
@@ -289,14 +297,23 @@ public class Mpeg2TSPacket {
             payload_byte = null;
         }
         if ((PID == 0) && (begin_payload_unit) && (payload_byte != null)) {
-            payload_byte = makePAT_byte(pointer_byte, payload_byte, p_table);
-            bPAT_payload = payload_byte;
+            byte[] new_pointer=new byte[1];
+            new_pointer[0]=0;
+            if (isPAT(payload_byte)){
+                payload_byte = makePAT_byte(new_pointer, payload_byte, p_table);
+                if (payload_byte.length>0){
+                    bPAT_payload = payload_byte;
+                }
+            }
         }
-        if (payload_byte != null) {
+        if ((payload_byte != null)&& (begin_payload_unit)) {
             ByteArrayOutputStream baos = new ByteArrayOutputStream(ts.length);
             baos.write(header_byte, 0, header_byte.length);
             baos.write(pointer_byte, 0, pointer_byte.length);
             baos.write(payload_byte, 0, payload_byte.length);
+            for (int ir = 0; ir < 188-baos.size(); ir++) {
+                baos.write(0xFF);
+            }
             return baos.toByteArray();
         } else {
             return null;
@@ -332,7 +349,8 @@ public class Mpeg2TSPacket {
         ByteArrayOutputStream baoscrc = new ByteArrayOutputStream(nowt + 1);
         baoscrc.write(pointer_field[0]);
         baoscrc.write(baos.toByteArray(), 0, baos.size());
-        byte[] crc = getCRC32_byte(baoscrc.toByteArray(), 1);
+        byte[] crc = cal.getCRC32_byte(baoscrc.toByteArray(), 1);
+        //byte[] crc = getCRC32_byte(baoscrc.toByteArray(), 1);
         baos.write(crc, 0, crc.length);
         int ill3 = payload_temp.length - baos.size();
         for (int ir = 0; ir < ill3; ir++) {
@@ -340,7 +358,22 @@ public class Mpeg2TSPacket {
         }
         return baos.toByteArray();
     }
-
+    public boolean isPAT(byte[] byte_tmp){
+        calc cal=new calc();
+        if ((cal.byte2int(byte_tmp,0,8)==0)&&(cal.byte2int(byte_tmp,9,3)==3)&&(cal.byte2int(byte_tmp, 40, 2)==3)){
+            return true;
+        }else{
+            return false;
+        }
+    }
+    public boolean isPMT(byte[] byte_tmp){
+        calc cal=new calc();
+        if ((cal.byte2int(byte_tmp,9,3)==3)&&(cal.byte2int(byte_tmp, 40, 2)==3)&&(cal.byte2int(byte_tmp,64,3)==3)&&(cal.byte2int(byte_tmp,80,4)==15)){
+            return true;
+        }else{
+            return false;
+        }
+    }
     private String getCRC32(String s) {
         return getCRC32(String2Byte(s), 1);
     }