星期二, 4月 26, 2005

MP4Live Internal 翻譯

程式控制

由沒有GUI的運作模式來看mp4live 比較容易瞭解。 在這個模式下, mp4live 讀 Config file, 然後根據Config file Create " Media flow",當"Media flow"啟動一些Thread之後main program 進入while loop,sleep一段時間,check是否要termintate。結束程式。


Media flow and Node

"Media flow" (media_flow.h) 是最高層,控制所有 mp4live 的活動的class。 Mediaflow 由許多Media Node集合而成(media_node.h), 每個node間都規定好互相傳送資料的方式。 每個 Node2. 都是mp4live 的一個thread process。 每個Node Thread都有一個message queue作為溝通的interface。 目前只有start,stop command經由這些queue來控制,還有當一比(一個media frame) ready時,利用的個queue來通知該thread可以開始處理了。Node之間的其他合作關係世界由shared configuration information data來達成。


Media Source

" Media Source" (media_source.h)是處理raw image data的node的Class,她根據configuration file將raw data轉換為需要的格式。"Media Source"可以用來產生(轉換)Video或Audio。Media Source可以同時將raw data轉換為多種格式,例如將video data轉為YUV 和MPEG-4。

因 為大部分的Media process都和Media的來源無關, Media Source class(media_source.cpp) 包含encode media ,加入同步time tag的code。現在的Media Process有點過度設計,可以用來轉換檔案和Capture device的輸出。

Audio 的Media Process有:

* Reframing PCM audio sampling。例如:將1152 PCM的MP3 reframed 為 1024 PCM 的AAC
* Resampling,降低sampling rate 和bit rate。
* Channel adoption : 將 立體轉為單音。
* 經由media codec作 encoding
* 將未處理的frame 轉送給其他process

Video的Media Process有:

* 將Colorspace 轉換從RGB24 到YUV12 。 一些Vedio capture只能輸出RGB,不能輸出YUV。
* Frame rate adoptin: down sampling 29.97 fps到24 fps
* Image resizing:將352x288 轉換為320x240的影像
* Image cropping: 將4:3 轉到到16:9 長寬比
* 經由media codec作encoding
* 將未處理的frame轉送給其他process


Midea Codecs


有 二種已定義的Media codecs (encoders):Video,Audio。定義在audio_encoder.h 和video_encoder.h,這兩個都是abstrat class,提供一個和encoder library溝通的inteface。Media Source使用Media Codec Class來作encode,將raw , uncompressed data轉換為encoded media。

每一個實際的codec都是由適合的 abstrator media codecs class derives自已的class,implement自己的interface method。

每 一個encoder都實作許多method,用來設定rtp transmission的變數或是設定將結果存為mp4檔案,各種編碼器類型並且有一定數量的電話設置各種各樣的可變物為 rtp 傳輸或挽救對mp4 文件。 這些method都宣告 audio_encoder.h 和video_encoder.h

將新的 encoder class加入到video_encoder_tables.h就可以增加新的video encoder,同樣的,將audio encoder加入audio_encoder_tables可以增加audio encoder。除了修改table之外,還要將hook code寫入audio_encoder.cpp和video_encoder.cpp中。


Media Frame


Media Source的輸出是Media Frame (media_frame.h) 。 這是一個被reference counted structure。包含 :一個指向malloc'ed 的Media Data,Media Data的長度,Media的Type,Media Frame的time tag,Media Frame duration(用在audio frame),Media frame的time scale。當create自己的Media Source,要在source間加入time tag。

Media Source在處理取得的media frame時,會產生很多media frame。 這些產生的Media frame會被包在message送到每一個向這個Source註冊的Media Sink中。要注意的是, 如果有 N Media Sink註冊需要這個Message,會有N個Message被create,但是都指向同一個Media Frame。Media frame會keep一個reference count,當有Sink free掉,count就減一,當count =0時,Media fram data會被free掉,這個frame也會被destroy。


Media Sinks


"Media Sink" (media_sink.h)從Media Source取得Media frame,並且把這些frame送到目的地(譬如file或network socket)。 迄今Media Sink對Media frame幾乎沒做處理。Media Sink的主要目的是利用獨立的Thread來避免寫入Media Frame到device時被block住,影響到Media frame的讀取(buffer用)。 例如:一個很糟的情況是Media Source不能跟上Video Capture的動作,Video Capture是不能"暫停"來等待Media Source讀取。要注意的情況是, 當Media Sink在對目標device(例如file或socket)寫入而被block時,新的Media frame會一直持續的放入她的queue中,如Media Sink沒有是當處置,最後系統 記憶將被耗盡。 同理,當Media Sink的動作跟不上realtime的Media flow速度,系統記憶體也會慢慢堆在Media Sink的queue中,最後耗盡。所以在Implement自己的Media Sink Class時要注意,要有write timeout功能,將memory release,避免導致程式不正常結束。

目前一個registered Media Sink會接受所有Media Source output的Media type,所以Media Sink要在class內部檢查Media的格式,reject (free) 它不想要的Media type。


Media Source List

V4L video_v4l_source.cpp
::從V4L (錄影為Linux) 設備獲取YUV12 圖像
V4L2 video_v4l2_source.cpp
::從V4L2 (錄影為Linux) 設備獲取YUV12 圖像(建議使用)
OSS audio_oss_source.cpp
::從OSS (開放伴音系統) 設備獲取PCM16 Audio Sample
self loop_source.cpp
::從Main process取得的raw的YUV 或PCM


Codecs List


Lame-> MP3 - audio_lame.cpp, installed from3.92 or later

FAAC -> AAC - audio_faac.cpp, installed from faac version 1.21 or later

ffmeg-> Mpeg layer 2, AMR NB, AMR WB, installed from ffmpeg (0.4.7 or later)

Xvid -> MPEG4 - video_xvid.cpp, mpeg4.ip/lib/xvid, or xvidcode 0.92 or later

Xvid 1.0 -> MPEG4 - video_xvid10.cpp,從xvidcode 1.0 RC3 or later

ffmpeg-> MPEG4, MPEG2, H263 - video_ffmpeg.cpp, installed from ffmpeg version0.4.7 or later(參拷README) 。


Media Sink List

MP4file_mp4_recorder.cpp
Writes media frames to an mp4 file
RTPrtp_transmitter.cpp
Transmits media frames via RTP/UDP/IP
Implements media specific RTP payloads as defined in IETF RFCs

An adjunct to the RTP transmitter is the SDP file writer (sdp_file.cpp) which constructs an SDP file that can be used to tune into the RTP streams

SDL Previewervideo_sdl_preview.cpp
Display video frames to a local video display
via the SDL multi-platform library
Raw Sinkfile_raw_sink.cpp
Writes raw media frames (YUV12 and PCM16) to a local named pipe
This enables sharing of the capture devices between mp4live and another application.

沒有留言:

網誌存檔