在Android的解码原始H264流? [英] Decoding Raw H264 stream in android?

查看:879
本文介绍了在Android的解码原始H264流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我已经要求显示Android的视频流,数据流是原始的H.264和我连接到服务器并接收来自服务器的字节流。

I have a project where I have been asked to display a video stream in android, the stream is raw H.264 and I am connecting to a server and will receive a byte stream from the server.

基本上,我不知道有没有办法来原始字节发送到去codeR在Android和在表面上显示呢?

Basically I'm wondering is there a way to send raw bytes to a decoder in android and display it on a surface?

我已经成功地包裹在MP4容器使用新媒体codeC和MediaExtractor API中的Andr​​oid 4.1,解码H264不幸的是我还没有找到一个办法去codeA原始H264文件或使用这些流API的。

I have been successful in decoding H264 wrapped in an mp4 container using the new MediaCodec and MediaExtractor API in android 4.1, unfortunately I have not found a way to decode a raw H264 file or stream using these API's.

据我所知,一个办法是编译和使用FFmpeg的,但我宁愿使用一个内置的,可以使用硬件加速的方法。我也明白RTSP流支持在Android的,但是,这是不是一种选择。 Android的版本是不是一个问题。

I understand that one way is to compile and use FFmpeg but I'd rather use a built in method that can use HW acceleration. I also understand RTSP streaming is supported in android but this is not an option. Android version is not an issue.

推荐答案

我不能提供任何code这很不幸,但我会尽我所能的基础上我是如何得到它的工作来解释它。

I can't provide any code for this unfortunately, but I'll do my best to explain it based on how I got it to work.

因此​​,这里是我的我是怎么生的H.264连接codeD的视频使用媒体codec类工作概述(<一href="http://developer.android.com/reference/android/media/Media$c$cc.html">http://developer.android.com/reference/android/media/Media$c$cc.html).

So here is my overview of how I got raw H.264 encoded video to work using the MediaCodec class (http://developer.android.com/reference/android/media/MediaCodec.html).

使用上面的链接也得到德codeR设置的例子,以及如何使用它,你需要将其设置为H264解码AVC

Using the link above there is an example of getting the decoder setup and how to use it, you will need to set it up for decoding H264 AVC.

的H.264的格式是,它是由NAL单元,每个开始的三个字节与值0×00,0x00时,0×01起始preFIX并且每个单元具有不同的类型取决于的值这3个字节开始后的第4个字节的权利。一个NAL单元的不是视频中的一帧,每一帧由一些NAL单元。

The format of H.264 is that it’s made up of NAL Units, each starting with a start prefix of three bytes with the values 0x00, 0x00, 0x01 and each unit has a different type depending on the value of the 4th byte right after these 3 starting bytes. One NAL Unit IS NOT one frame in the video, each frame is made up of a number of NAL Units.

基本上我写的,发现每个单元,并将其传递到去codeR方法(一个NAL单元作为出发preFIX有任何字节后,直到下次启动时preFIX)。

Basically I wrote a method that finds each individual unit and passes it to the decoder (one NAL Unit being the starting prefix and any bytes there after up until the next starting prefix).

现在,如果你有去codeR设置为H.264 AVC解码,并有INPUTBUFFER从去codeR那么你就准备好了。您需要填写此INPUTBUFFER与NAL单元,并将其传递回德codeR,并继续这样做的流的长度。 但是,为了使这项工作,我必须通过CR一个SPS(序列参数集)NAL单元去$ C $第一。本机具有0x67后出发preFIX(4字节)一个字节的值,在某些设备上的德codeR会崩溃,除非它收到了单位第一。 基本上,直到你找到这个单位,忽略所有其他NAL单元,并保持解析流,直到你得到这个单位,那么你可以通过所有其他单位去codeR。

Now if you have the decoder setup for decoding H.264 AVC and have an InputBuffer from the decoder then you are ready to go. You need to fill this InputBuffer with a NAL Unit and pass it back to the decoder and continue doing this for the length of the stream. But, to make this work I had to pass the decoder a SPS (Sequence Parameter Set) NAL Unit first. This unit has a byte value of 0x67 after the starting prefix (the 4th byte), on some devices the decoder would crash unless it received this Unit first. Basically until you find this unit, ignore all other NAL Units and keep parsing the stream until you get this unit, then you can pass all other units to the decoder.

有些设备不首先需要的SPS和一些做的,但你传递给它的第一个更好的。

Some devices didn't need the SPS first and some did, but you are better of passing it in first.

现在,如果你有你传递给德codeR,当你配置它,然后一旦得到足够的NAL单元的框架应该在表面上显示它的表面。

Now if you had a surface that you passed to the decoder when you configured it then once it gets enough NAL units for a frame it should display it on the surface.

这篇关于在Android的解码原始H264流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆