Saturday 15 January 2011

How to use Android MediaCodec encode Camera data(YUV420sp) -


Thanks for your focus! I want to use the Android MediaCode API to encode the video frame received from the camera, unfortunately, I have not had success in doing so! I'm still not familiar with MediaCodec APIA ???? Follow my code, I need your help to figure out what I should do.

1 A "Camera Settings:

  parameter = mCamera.getParameters (); parameters.setPreviewFormat (ImageFormat.NV21); Parameters.setPreviewSize (320, 240); mCamera.setParameters (parameters);   

2 A ??? encoder set:

  private void initCodec () {try fos = new FileOutputStream (mVideoFile , false);} catch (FileNotFoundException e) {e.printStackTrace ();} MMediaCodec = MediaCodec.createEncoderByType ( "Video / AVC"); Meediaformet Meediaformet = Meediaformetkkradit Veedioformet ( "Video / AVC", 320, 240); MediaFormat.setInteger (MediaFormat.KEY_BIT_RATE, 125000); MediaFormat.setInteger (MediaFormat.KEY_FRAME_RATE, 15); MediaFo RmatksetInteger (Meediaformet. KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar); MediaFormat.setInteger (MediaFormat.KEY_I_FRAME_INTERVAL, 5); Mdiaidiaidisisisi configured (Meediaformet, blank, blank, Meediakod. CONFIGURE_FLAG_ENCODE); mMediaCodec.start (); Inputbfrs = Mdiaidikod. Incubuters (); Output buffers = MDID code. Output buffers (); } Private Zero Encoded (byte [] data) {int inputBufferIndex = mMediaCodec.dequeueInputBuffer (0); If (inputbufferedx> = 0) {Bitfuffer inputbuffer = inputbufers [inputfounderx]; InputBuffer.clear (); InputBuffer.put (data); MMediaCodec.queueInputBuffer (inputfoundX, 0, data length, 0, 0); } And {return; } MediaCodec.BufferInfo buffer INFO = New MediaCodec.BufferInfo (); Int outputbufferindex = MDIDODEUUUutput buffer (buffinfo, 0); Logs. I (tag, "outputbuffer index -> gt;" + outputbuffer index;); Please {if (outputBufferIndex & gt; = 0) {Bytebuff Outbuffer = Output Buffers [OutputBuffer Index]; System.out.println ( "buffer information - & gt;" + buffer Infoaf + + + "-" + buffer Infusaijh + "-" + buffer Infofrfs + "-" + buffer Infoteshn presentation time Yujh); Byte [] Outside Data = New Byte [Buffer Infosys]; OutBuffer.get (outData); Try {if (buffer infofeb !! = 0) {fos.write (outdata, buffer info.offet, outdata.lambi - buffer infooffet); } Other {fos.write (outdata, 0, outdata.line); } Fos.flush (); Logs. I (tag, "out data -> gt; + outdata.line); MMediaCodec.releaseOutputBuffer (outputBufferIndex, Incorrect); OutputBufferIndex = mMediaCodec.dequeueOutputBuffer (BufferInfo, 0); } Hold (IOException e) {e.printStackTrace (); }} Else if (output Bfrindaks == Meediakod .INFO_OUTPUT_BUFFERS_CHANGED) {outputbuffers = mMediaCodec.getOutputBuffers (); } And if (output buffer index == mediacode .INFO_OUTPUT_FORMAT_CHANGED) {MediaFormat format = mMediaCodec.getOutputFormat (); }} While (outputbufferindex> = 0); }   

I think that was a problem in the encoder method, using this method will be Kailado preview callback, such as

  initCodec (); //mCamera.setPreviewCallback (new MyPreviewCallback ()); MCamera.setPreviewCallback (New PreviewCollectionBack) {preview preview preview on public Zero (byte [], camera camera) {encode (data);}});   

I have not figured out how to get it right from the MediaCode API. Can you give me some advice or links about this?

Thank you!

I have solved this problem. As

  Private Synchronized Zero (Byte [] data) {inputBuffers = mMediaCodec.getInputBuffers (); // here outputBuffers = mMediaCodec.getOutputBuffers () changes; Int inputbufferindex = MDIDODEDUEUUU inputbuffer (-1); Logs. I (tag, "inputbuffer index -> gt;" + inputbuffer index "); // For more information, please go to here     

Further, you will find that the color of your encoded video is not correct

No comments:

Post a Comment