Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. File extStorage = Environment.getExternalStorageDirectory();
  2. File media = new File(extStorage,"video.h264");
  3. BufferedInputStream in = new BufferedInputStream(new FileInputStream(media));
  4. codec = MediaCodec.createDecoderByType("video/avc");
  5. MediaFormat format = MediaFormat.createVideoFormat("video/avc", 480, 384);
  6. format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 480 * 384);
  7. format.setString(MediaFormat.KEY_MIME, "video/avc");
  8. codec.configure(format, videoSurface, null, 0);
  9. codec.start();
  10. //Get arrays of our codec buffers
  11. ByteBuffer[] inputBuffers = codec.getInputBuffers();
  12. ByteBuffer[] outputBuffers = codec.getOutputBuffers();
  13. long timeoutUs = 3000;
  14. BYTES_READ = new byte[1500];
  15. while (in.read(BYTES_READ) != -1) {
  16. for ( byte b : BYTES_READ) {
  17. nalUnit.write(b);
  18. if ( String.format("%02X", b).equals("00") && hdrIndex < 3 ) {
  19. NAL_HEADER[hdrIndex++]=b;
  20. } else if ( hdrIndex == 3 && String.format("%02X", b).equals("01") ) {
  21. NAL_HEADER[hdrIndex++]=b;
  22. } else if ( hdrIndex == 4 ) {
  23. NAL_HEADER[hdrIndex++]=b;
  24. if (nalUnitIndxS == -1) {
  25. nalUnitIndxS=0;
  26. nalUnitIndxE=nalUnit.size()-5;
  27. } else if (nalUnitIndxS >= 0){
  28. nalUnitIndxE=nalUnit.size()-5;
  29. }
  30. if (nalUnitIndxE > 0 ) {
  31. Log.d(TAG,"Attempting to write NAL unit to codec buffer... SIZE:"+nalUnit.size()+" IndxStart: "+nalUnitIndxS+" IndxEnd: "+nalUnitIndxE);
  32. Log.d(TAG,"NAL Unit Type: "+String.format("%02X", nalUnit.toByteArray()[4]));
  33. /*
  34. * Get an input buffer
  35. */
  36. int inputBufferIndex=-1;
  37. for ( int x = 0; x < 4; x++ ) {
  38. inputBufferIndex = codec.dequeueInputBuffer(timeoutUs);
  39. if ( inputBufferIndex >= 0 ) {
  40. break;
  41. } else {
  42. Thread.sleep(250);
  43. }
  44. }
  45. if (inputBufferIndex >= 0) {
  46. // fill inputBuffers[inputBufferIndex] with valid data
  47. long presentationTimeUs = Calendar.getInstance().getTimeInMillis();
  48. int nalUnitLen=nalUnitIndxE-nalUnitIndxS;
  49. inputBuffers[inputBufferIndex].put(nalUnit.toByteArray(), nalUnitIndxS, nalUnitLen);
  50. if ( configPacket ) {
  51. Log.d(TAG,"Writing payload as configuration to codec...");
  52. codec.queueInputBuffer(inputBufferIndex,0,nalUnitLen,presentationTimeUs,MediaCodec.BUFFER_FLAG_CODEC_CONFIG);
  53. } else {
  54. codec.queueInputBuffer(inputBufferIndex,0,nalUnitLen,presentationTimeUs,0);
  55. //deQueue the Output Buffer
  56. MediaCodec.BufferInfo bufInfo = new MediaCodec.BufferInfo();
  57. int outputBufferIndex = codec.dequeueOutputBuffer(bufInfo, timeoutUs);
  58. if (outputBufferIndex >= 0) {
  59. Log.d(TAG,"OutputBuffer is ready to be processed or rendered.");
  60. codec.releaseOutputBuffer(outputBufferIndex,true);
  61. } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
  62. Log.d(TAG,"Output Buffers Changed!");
  63. outputBuffers = codec.getOutputBuffers();
  64. } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
  65. // Subsequent data will conform to new format.
  66. Log.d(TAG,"Output Format Changed! Updating format...");
  67. format = codec.getOutputFormat();
  68. } else {
  69. Log.w(TAG,"Did not understand OutputBuffer Index Response: "+outputBufferIndex);
  70. }
  71. }
  72. nalUnit.reset();
  73. nalUnit.write(NAL_HEADER,0,5);
  74. nalUnitIndxS=0;
  75. nalUnitIndxE=0;
  76. } else {
  77. Log.w(TAG, "We did not get a buffer!");
  78. }
  79. }
  80. } else {
  81. hdrIndex=0;
  82. }
  83. if ( hdrIndex == 5 && ( String.format("%02X", NAL_HEADER[4]).equals("21") || String.format("%02X", NAL_HEADER[4]).equals("25") ) ) {
  84. configPacket=false;
  85. hdrIndex=0;
  86. } else if ( hdrIndex == 5 ){
  87. configPacket=true;
  88. hdrIndex=0;
  89. }
  90. }
  91. }
  92. Log.d(TAG,"Cleaning up Codec and Socket....");
  93. codec.stop();
  94. codec.release();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement