在Android程序中获取avc中的数据


在Android程序中获取avc中的数据,也就是AVCDecoderConfigurationRecord 。

你可以先录制一小段视频保存在sd卡中。

然后根据以下代码来获取数据。这样就可以根据不同的手机或者不同的分辨率来调整sps pps或者avc了

  1. package com.ppmeet.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9.   
  10. import android.util.Log;  
  11.   
  12. /** 
  13.  * class name:MyDetect<BR> 
  14.  * class description:get avc data<BR> 
  15.  * PS: <BR> 
  16.  *  
  17.  * @version 1.00 2011/09/21 
  18.  * @author CODYY)peijiangping 
  19.  */  
  20. public class MyDetect {  
  21.     // We should check 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x**, 'A', 'v', 'c', 'C',   
  22.     // 0x01   
  23.     final static int MAX_HEADER_LENGTH = 128;  
  24.     public static byte[] spsData = new byte[MAX_HEADER_LENGTH];  
  25.     public static int spsDataLength = 0;  
  26.     public static byte[] ppsData = new byte[MAX_HEADER_LENGTH];  
  27.     public static int ppsDataLength = 0;  
  28.     public static int mdataPosition = 32;  
  29.   
  30.     private static byte[] checkBuffer = new byte[1024 * 4];  
  31.     private static int headerLength;  
  32.   
  33.     public static void writeConfig(String discFile) {  
  34.         File newFile = new File(discFile);  
  35.         try {  
  36.             OutputStream os = new FileOutputStream(newFile.getAbsolutePath());  
  37.             os.write(checkBuffer, 0, headerLength);  
  38.             os.flush();  
  39.             os.close();  
  40.         } catch (IOException e) {  
  41.   
  42.         }  
  43.     }  
  44.   
  45.     public static void getHeaderData(byte[] buffer) {  
  46.         /* 
  47.          * put_byte(pb, 1); // version put_byte(pb, sps[1]); // profile 
  48.          * put_byte(pb, sps[2]); // profile compat put_byte(pb, sps[3]); // 
  49.          * level put_byte(pb, 0xff); // 6 bits reserved (111111) + 2 bits nal 
  50.          * size length - 1 (11) 
  51.          *  
  52.          * put_byte(pb, 0xe1); // 3 bits reserved (111) + 5 bits number of sps 
  53.          * (00001) 
  54.          *  
  55.          *  
  56.          * put_be16(pb, sps_size); put_buffer(pb, sps, sps_size); put_byte(pb, 
  57.          * 1); // number of pps put_be16(pb, pps_size); put_buffer(pb, pps, 
  58.          * pps_size); 
  59.          */  
  60.         mdataPosition = (int) (buffer[0] & 0xFF) * 256  
  61.                 + (int) (buffer[1] & 0xFF);  
  62.         spsDataLength = (int) (buffer[6] & 0xFF) * 256  
  63.                 + (int) (buffer[7] & 0xFF);  
  64.         for (int i = 0; i < spsDataLength; i++)  
  65.             spsData[i] = buffer[8 + i];  
  66.   
  67.         ppsDataLength = (int) (buffer[9 + spsDataLength] & 0xFF) * 256  
  68.                 + (int) (buffer[10 + spsDataLength] & 0xFF);  
  69.         for (int i = 0; i < ppsDataLength; i++)  
  70.             ppsData[i] = buffer[11 + spsDataLength + i];  
  71.   
  72.         Log.d("TEAONLY""spsDataLength = " + spsDataLength  
  73.                 + " ppsDataLength = " + ppsDataLength);  
  74.   
  75.         headerLength = 6 + 2 + 2 + 1 + spsDataLength + ppsDataLength;  
  76.         for (int i = 0; i < headerLength; i++) {  
  77.             checkBuffer[i] = buffer[i];  
  78.         }  
  79.   
  80.         checkBuffer[0] = (byte) ((mdataPosition >> 8) & 0xFF);  
  81.         checkBuffer[1] = (byte) (mdataPosition & 0xFF);  
  82.   
  83.     }  
  84.   
  85.     public static boolean checkMP4_MDAT(String fileName) throws IOException {  
  86.         File fin = new File(fileName);  
  87.         if (fin.isFile()) {  
  88.             InputStream is = new FileInputStream(fin.getAbsolutePath());  
  89.             int pos;  
  90.             int fms = 0;  
  91.             boolean isOK;  
  92.             int n;  
  93.             int fpos = 0;  
  94.             while (true) {  
  95.                 isOK = false;  
  96.   
  97.                 n = is.read(checkBuffer);  
  98.                 if (n < 0) {  
  99.                     break;  
  100.                 }  
  101.   
  102.                 for (pos = 0; pos < n; pos++) {  
  103.                     fpos++;  
  104.                     byte tmp = checkBuffer[pos];  
  105.                     switch (tmp) {  
  106.                     case (byte'm':  
  107.                         if (fms == 0)  
  108.                             fms = 1;  
  109.                         else  
  110.                             fms = 0;  
  111.                         break;  
  112.   
  113.                     case (byte'd':  
  114.                         if (fms == 1)  
  115.                             fms = 2;  
  116.                         else  
  117.                             fms = 0;  
  118.                         break;  
  119.   
  120.                     case (byte'a':  
  121.                         if (fms == 2)  
  122.                             fms = 3;  
  123.                         else  
  124.                             fms = 0;  
  125.                         break;  
  126.   
  127.                     case (byte't':  
  128.                         if (fms == 3)  
  129.                             fms = 4;  
  130.                         else  
  131.                             fms = 0;  
  132.                         break;  
  133.   
  134.                     default:  
  135.                         fms = 0;  
  136.                         break;  
  137.                     }  
  138.                     if (fms == 4) {  
  139.                         isOK = true;  
  140.                         break;  
  141.                     }  
  142.                 }  
  143.                 if (isOK) {  
  144.                     Log.d("TEAONLY",  
  145.                             "MP4 file MDAT position is OK.**************");  
  146.                     mdataPosition = fpos;  
  147.                     return true;  
  148.                 }  
  149.             }  
  150.         }  
  151.         return false;  
  152.     }  
  153.   
  154.     public static boolean checkMP4_MOOV(String fileName) throws IOException {  
  155.         File fin = new File(fileName);  
  156.   
  157.         if (fin.isFile()) {  
  158.             InputStream is = new FileInputStream(fin.getAbsolutePath());  
  159.             int pos;  
  160.             int fms = 0;  
  161.             boolean isOK;  
  162.             int n;  
  163.             while (true) {  
  164.                 isOK = false;  
  165.   
  166.                 n = is.read(checkBuffer);  
  167.                 if (n < 0) {  
  168.                     break;  
  169.                 }  
  170.   
  171.                 for (pos = 0; pos < n; pos++) {  
  172.                     byte tmp = checkBuffer[pos];  
  173.                     switch (tmp) {  
  174.                     case (byte'a':  
  175.                         if (fms == 0)  
  176.                             fms = 1;  
  177.                         else  
  178.                             fms = 0;  
  179.                         break;  
  180.   
  181.                     case (byte'v':  
  182.                         if (fms == 1)  
  183.                             fms = 2;  
  184.                         else  
  185.                             fms = 0;  
  186.                         break;  
  187.   
  188.                     case (byte'c':  
  189.                         if (fms == 2)  
  190.                             fms = 3;  
  191.                         else  
  192.                             fms = 0;  
  193.                         break;  
  194.   
  195.                     case (byte'C':  
  196.                         if (fms == 3)  
  197.                             fms = 4;  
  198.                         else  
  199.                             fms = 0;  
  200.                         break;  
  201.                     case (byte0x01:  
  202.                         if (fms == 4)  
  203.                             fms = 5;  
  204.                         else  
  205.                             fms = 0;  
  206.                         break;  
  207.                     default:  
  208.                         fms = 0;  
  209.                         break;  
  210.                     }  
  211.                     if (fms == 5) {  
  212.                         isOK = true;  
  213.                         break;  
  214.                     }  
  215.                 }  
  216.                 if (isOK) {  
  217.                     Log.d("TEAONLY""MP4 file SPS PPS is OK.**************");  
  218.                     // ��ʼ��ȡPPS�Լ�SPS���   
  219.                     for (int i = 0; i < checkBuffer.length - pos; i++) {  
  220.                         checkBuffer[i] = checkBuffer[i + pos];  
  221.                     }  
  222.                     if (pos > checkBuffer.length - MAX_HEADER_LENGTH) {  
  223.                         is.read(checkBuffer, checkBuffer.length - pos,  
  224.                                 MAX_HEADER_LENGTH);  
  225.                     }  
  226.   
  227.                     getHeaderData(checkBuffer);  
  228.                     return true;  
  229.                 }  
  230.             }  
  231.         }  
  232.         return false;  
  233.     }  
  234.   
  235. }  

相关内容