android视频编解码中buffer赋值的问题

遗天补 发布于 2014/11/13 15:34
阅读 1K+
收藏 0

问题是ByteBuffer类的buffer对象只是在ByteBuffer buffer = ByteBuffer.wrap( mPixel );中初始化了,而其他没发现哪里对其有操作,为什么每次在循环体里执行这句话的时候    VideoBit.copyPixelsFromBuffer(buffer);//makeBuffer(data565, N));buffer都会不同。


private void playVideo() {

                     new Thread(Video).start();

              }

             

              private boolean flagIframe=false;

              private int GetNum=0;

              /**

               * 视频解码播放线程:opencore

               */

             

               

               byte [] mPixel = new byte[176*144*2]; 

           ByteBuffer buffer = ByteBuffer.wrap( mPixel );//ffmpeg

           ffmpeg codec = new ffmpeg();//good ffmpeg

          

           Bitmap VideoBit = Bitmap.createBitmap(176,144, Config.RGB_565);

 

           Runnable Video = new Runnable(){

                     public void run(){

                             int iTemp=0;

                            byte nal[]=new byte[50000];

                            codec.init(176,144);

                            int Buffer_length = 0;                     

                         byte pps_soft[] = {0x00, 0x00, 0x00, 0x01 ,0x27,0x42,0x10,0x09,(byte) 0x96,0x35,0x05,(byte) 0x89,(byte) 0xc8};

                            byte sps_soft[] = {0x00, 0x00, 0x00, 0x01 ,0x28,(byte) 0xce,0x02,(byte) 0xfc,(byte) 0x80}; 

                        

                         iTemp=codec.DecoderNal(pps_soft,13,mPixel); //设置解码器

                         iTemp=codec.DecoderNal(sps_soft,9,mPixel);                                    

                            while(Decoding) {

                                   Info.NalBuffer[GetNum].IsReadAble(); 

                                   Buffer_length=Info.NalBuffer[GetNum].getNalLen();  //ffmpeg 

                                   nal=Info.NalBuffer[GetNum].getReadable_NalBuf();                             

                                   if(!flagIframe){   //没有收到主帧

                                       if((nal[4]&0xff) == 0x25){    //收到主帧,进行置位;更改flag标志位,下次将不再进行主帧判断,直接进行解码

                                              flagIframe=true;

                                       }

                                       else{    //没有收到主帧,清空缓存,继续接收

                                              Log.d("zzframe","else");

                                              Info.NalBuffer[GetNum].cleanNalBuf();

                                              Info.NalBuffer[GetNum].setNalLen(0);

                                              Info.NalBuffer[GetNum].ReadLock();

                                       GetNum++;

                                       if(GetNum==200)

                                       {GetNum=0; }

                                       continue;

                                 }

                                }

                            try{

                                       //NativeH264Decoder.DecodeAndConvert(nal, frame);

                                   iTemp=codec.DecoderNal(nal,Buffer_length,mPixel);//good ffmpeg

                                   System.out.println("is>>>>>jiema");

                                   if(iTemp > 0){

                                              Log.e("qqqqqq", "I'm coming too");

                                              dosurfacedraw1();}

                                   Info.NalBuffer[GetNum].cleanNalBuf();

                                   Info.NalBuffer[GetNum].setNalLen(0);

                                   Info.NalBuffer[GetNum].ReadLock();

                                      GetNum++;

                                      if(GetNum==200)

                                             GetNum=0;  

                                       }

                                catch(Exception e){

                                       e.printStackTrace();

                                }                  

                        }

                            Uninit_Decoder();  }

                      

              };

             

              private void dosurfacedraw1() {

             

                     VideoBit.copyPixelsFromBuffer(buffer);//makeBuffer(data565, N));

              

                     int surfaceViewWidth = sfv.getWidth();

                     int surfaceViewHeight = sfv.getHeight();

                     int bmpWidth = VideoBit.getWidth();

                     int bmpHeight = VideoBit.getHeight();

               Matrix matrix = new Matrix();

               float scalewh;

               float scalehh;

               matrix.setRotate(270);

                  if(surfaceViewWidth > surfaceViewHeight){

                         scalewh = (float)surfaceViewWidth/bmpHeight;

                         scalehh = (float)surfaceViewWidth/bmpHeight;

                  }else{

                      scalewh = (float)surfaceViewHeight/bmpWidth;

                      scalehh = (float)surfaceViewHeight/bmpWidth;

                  }

             

               matrix.postScale(scalewh, scalehh);

              

               Bitmap resizeBmp = Bitmap.createBitmap(VideoBit,0 ,0 ,bmpWidth,bmpHeight,matrix,true);

               Canvas canvas = sfh.lockCanvas();

               canvas.drawBitmap( resizeBmp, 0, 0, null);      

               sfh.unlockCanvasAndPost(canvas);             

                            }

      

             

              /**

               * 结束解码设置

               */

           public void Uninit_Decoder(){

                  this.Decoding=false;

           }    

              

加载中
返回顶部
顶部