2010年2月1日 星期一

JPEG emit_bits_s

emit_bits_s()  called by encode_one_block() in jchuff.c
所做的事就是單純地把bit 輸出

put_buffer 32bit 只用到右邊24bit
每次進來的bits 最多是16bit
再加上之前殘餘的最多不超過8bit( <= 7bit )
所以24bit 就足夠了


At most 16 bits can be passed to emit_bits

 in one call, and we never retain more than 7 bits in put_buffer
 between calls, so 24 bits are sufficient.

前次殘餘的bit + 這次進來的 bit
超過8bit 就輸出一個byte

1. 進來的code
2. 往左shift 靠在第24bit的位置
3. OR 之前的bit
4. 每8個bit 輸出一個byte 直到資料小於8bit

MP4 Audio Mux & AAC header format

用 DVBPortal (mp4creator) mux AVC & AAC
總共有2個trak
2個 mdat section




AAC Header
(參考 http://mp4tech.net/document/audiocomp/0000207.asp )

 http://www.hydrogenaudio.org/forums/lofiversion/index.php/t21617.html



AAC的音频文件格式有以下两种:
ADIF:Audio Data Interchange Format 音频数据交换格式。这种格式的特征是可以确定的找到这个音频数据的开始,不需进行在音频数据流中间开始的解码,即它的解码必须在明确定义的开始处进行。故这种格式常用在磁盘文件中。

ADTS:Audio Data Transport Stream 音频数据传输流。这种格式的特征是它是一个有同步字的比特流,解码可以在这个流中任何位置开始。它的特征类似于mp3数据流格式。


Mux 完在mp4 mdat section 中的aac 比原始的aac 少了n個 7bytes的header
這些header 的格式為 FF F1 DC 40 XX XX XX

emphasis field 2 bit 現在沒有用了,所以是 Fixed header( 28bits) + Variable header(28bits) = 7 bytes

sync word  12bit : 1111 1111 1111  所以是 FF F

ID 1     0: MPEG-4, 1: MPEG-2

layer 2 always: '00'


protection_absent 1

profile 2

sampling_frequency_index 4

private_bit 1

channel_configuration 3

original/copy 1

home 1

ADTS Variable header: these can change from frame to frame


copyright_identification_bit 1

copyright_identification_start 1

aac_frame_length 13 length of the frame including header (in bytes)

adts_buffer_fullness 11 0x7FF indicates VBR

no_raw_data_blocks_in_frame 2

ADTS Error check

crc_check 16 only if protection_absent == 0

After that come (no_raw_data_blocks_in_frame+1) raw_data_blocks.

Some elaborations:

profile

bits ID == 1 (MPEG-2 profile) ID == 0 (MPEG-4 Object type)

00 (0) Main profile AAC MAIN

01 (1) Low Complexity profile (LC) AAC LC

10 (2) Scalable Sample Rate profile (SSR) AAC SSR

11 (3) (reserved) AAC LTP



 
ADTS詳細格式請參考 14496-3
由上可知 AAC Frame Length 為13bit, 所以一個AAC Frame最大值為8K
ADIF格式主要是應用在一般的檔案資料中,而ADTS格式則主要利用在網路的傳輸 上,為了避免傳輸產生的錯誤,ADTS格式在AAC檔案的每一個音框(frame)中都傳一次檔 頭;而ADIF格式只有一個檔頭。


ADTS 每一個frame前都有加檔頭 7bytes,
然後檔頭後面的格式 為raw format
有七種格式
常見的為
CPE
請參考 14496-3
http://www.aeroquartet.com/movierepair/aac.html


CPE 格式

21 0B CF or 21 0B CD

0010 0001 0000 1011 1100


1. 前3個bit 代表格式 在這邊 001 代表 此frame 為CPE ( Channel Pair Element )
其它還有 000   SCE ( Single Channel Element )
                 010  CCE
                 011  LFE ....
2. 再來 4個bit 為 element_instance_tag
3. 再來1個bit 為 common_window
4. 如果 common_windows 為1 在CPE中 接下來就是 ics_info
5. window_sequence 如果等於 EIGHT_SHORT_SEQUENCE (10) 就 ...
在此我們可以看到 window_sequence 不等於 EIGHT_SHORT_SEQUENCE
6. maxsfb 在此為 6bit 101111 (47)  剛好是呼叫 AACQuantize() 時 參數num_cb 的值