天天看點

了解網絡視訊H.264的播放

H.264  流

視訊捕捉得到的圖檔壓縮為H264流媒體,其包含很多 NAL Units

了解網絡視訊H.264的播放

An H.264/MPEG-4 AVC-compliant bitstream consists of a sequence of Network Abstract Layer Units (NAL Units or NALUs). Examples of NALUs are Sequence Parameter Set (SPS), Picture Parameter Set (PPS), Supplemental Enhancement Information (SEI), and slice. The SPS and PPS NALUs contain information for the correct decoding of a sequence of pictures. A SEI nalu contains information that is not needed by the decoding process; it can be seen as embedded metadata. Pictures consist of one or more slices. Each slice contains a slice header and slice data.

SPS, PPS 說明視訊資訊的 NALu, 解碼器要知道這些才能正确解碼, 而每一個 Slice 是也一個 NALu

Slice

什麼是 Slice是? 看下面的圖就知道了,一個圖檔可能由多個 Slices 組成, H.264比 MPEG2支援更細的 slice

了解網絡視訊H.264的播放

H.264的個幀可能有依賴關系,比如前後兩幀,隻有輛小"汽車”移動了一點,那個後一幀可隻用一個size很小的slice 更新汽車移動的那一小片區域

如果後幀相對前幀有兩個區域發生變化,那麼需要用兩個 slices才能更新目前畫面

IDR

還有一種叫 IDR 的 NALu, 叫 i slice  要求完全重畫幀,并且此 IDR 之前的幀都無效。這是因為使用者要做 seek, 如果解碼中後續幀依賴整個視訊的第一幀,那們必須要從頭計算才能生成目前的畫面,這是不可能,是以IDR是頻繁穿插在視訊流的。

An IDR frame is a special type of I-frame in H.264. An IDR frame specifies that no frame after the IDR frame can reference any frame before it. This makes seeking the H.264 file easier and more responsive in the player.

不考略idr,得到的圖檔可能如下:

了解網絡視訊H.264的播放

HTTP 網絡視訊播放

現在有了NALu, 可以直接用 Http 協定把 Http server 上的視訊mp4檔案,傳輸用戶端的 MediaPlayer中,(在browser能播放是因為浏覽器調用了MediaPlayer), 原理如下:

mp4 檔案是一種包裹視訊流的box, 其頭部有 sample table (就是索引資訊,内容是時間到檔案位置的映射 ) 

一般 Media Player 會緩沖索引檔案,當使用者 seek的時候,從索引檔案中找到時間對應檔案位置,而後用 http 協定通路,比如下面通路 mp4 檔案位置 2556

http://pdl.googletv.cnbc.com/VCPS/Y2013/M09D12/3000198565/5ED2-KR-FBlock0912_H264_720x405_30p_1M.mp4 @2556

而後再讀取 IDR重構畫面

在播放過程中,不斷 fetch 後續位置的資料,直到緩沖上限

ref:

http://multimedialab.elis.ugent.be/BFlavor/samples.html

http://www.tvro.com.tw/main5/maimtxt/H.264%E5%A0%B1%E5%B0%8E%EF%BC%8D%E5%B7%A5%E7%A0%94%E9%99%A2.htm

繼續閱讀