mpeg2 ts をパースする
hls のセグメントを結合したいので mpeg2 ts のパースをしたくなった 愚直に結合しただけの ts ファイルでは VLC で上手く再生できない
後述の PCR が正しくないと再生できない
MPEG-2 TS ではすべてのデータを 188 Byte の固定長パケットに分割する.
パケットは大きなひとまとまりの単位
ヘッダ + ペイロードで構成される
ヘッダとペイロードを合わせると通常 188 Bytes になる (0xbc)
パケットの種類
PAT (Program Association Table)
Program Association Table は、プログラム番号ごとに PMT の PID を指定しています
code:pat(c)
program_association_section() {
table_id 8 uimsbf
section_syntax_indicator 1 bslbf
'0' 1 bslbf
reserved 2 bslbf
section_length 12 uimsbf
transport_stream_id 16 uimsbf
reserved 2 bslbf
version_number 5 uimsbf
current_next_indicator 1 bslbf
section_number 8 uimsbf
last_section_number 8 uimsbf
for (i = 0; i < N; i++) {
// N = (section_length - 固定長部分(40 / 8) - CRC (32 / 8)) bytes / 4 bytes program_number 16 uimsbf
reserved 3 bslbf
if (program_number == '0') {
network_PID 13 uimsbf
} else {
program_map_PID 13 uimsbf
}
}
CRC_32 32 rpchof
}
PMT (Program Map Table)
動画、音声、その他もろもろの PID が何番なのかを指定するテーブルです。
コンバートを行うには PMT をパースして、どこが動画や音声のパートなのか探す
PID がそれを指し示す
https://cdn-ak.f.st-hatena.com/images/fotolife/a/aton-kish/20181204/20181204004327.png
ペイロードの種類は大別して三種
ES (Elementary Stream)
映像・音声などの符号化されたデータをエレメンタリストリーム (ES: Elementary Stream) という.
PES (Packetized Elementary Stream)
ES を大まかに(たいていは意味のある)かたまりで分割してパケット化したものを PES (Packetized Elementary Stream) といい,さらに MPEG-2 TS の複数のパケットに分割して伝送する.
PSI (Program Specific Information)
PSI (Program Specific Information) は ES の種類やプログラムとの対応関係を与える.
MPEG-2 TS では複数のプログラムを扱うことができるが,映像・音声などの ES の正しい組み合わせを与えるためには複数ある ES がそれぞれ何者であるのか,どのプログラムと対応するのかを知る必要がある.
SI (Service Information)
テレビ放送などではプログラム(番組)の制御・属性などの情報である SI (Service Information) が送出されている.
これをもとに番組表を構築したりできる.
MPEG-2 System規格ではPSIのみが記述されているがMPEG-2 TSを採用する多くのデジタル放送規格ではPSIを拡張し、番組情報などを含めたSI(Service Information)が規定されている。
SIはPSIの一部であると見なして、PSI/SI情報としてまとめて扱う場合が多い。
ヘッダーは 32 bits (== 4 Bytes)
ヘッダーの図解
https://gyazo.com/4e6b47942d42d940a1924c7f8bc31827
PID (Packet IDentifier)
ヘッダに含まれている。
各パケットに含まれる ES, PSI, SI の種類がわかる
Program Clock Reference
PCR (Program Clock Reference)
ペイロードの中にある Adaptation Field に含まれている。
MPEG-2 TSの送信側と受信側で同期をとるためのクロック
27,000,000=1秒で表される。MPEG-2 TSストリーム中に100ms間隔で出現する事が推奨されている。
PCRは42Bitのデータ
Base 33 Bit
Extension 9 Bit
PCR = pcr_base × 300 + pcrxtension
PCR を TS ヘッダから読み取る方法
PCR は Adaptation Field の中にある pcr_flag が 1 の時に存在する
program_clock_reference_base; program_clock_reference_extension
The program_clock_reference (PCR) is a 42-bit field coded in two parts.
The first part, program_clock_reference_base, is a 33-bit field whose value is given by PCR_base(i), as given in equation 2-2.
The second part, program_clock_reference_extension, is a 9-bit field whose value is given by PCR_ext(i), as given in equation 2-3.
The PCR indicates the intended time of arrival of the byte containing the last bit of the program_clock_reference_base at the input of the system target decoder.
adaptation field control
1 bit 目はアダプテーションフィールドの存在を示すフラグで, 2 bit 目はペイロードの存在を示すフラグである. つまり,
'01' のとき「アダプテーションフィールドはなく,ペイロードのみ存在」,
'10' のとき「アダプテーションフィールドのみ存在,ペイロードはない」,
'11' のとき「アダプテーション,ペイロードともに存在」
である. ちなみに '00' は将来のために予約されている.
When the adaptation_field_control value is '11', the value of the adaptation_field_length shall be in the range 0 to 182.
When the adaptation_field_control value is '10', the value of the adaptation_field_length shall be 183
2.4.3.5 Semantic definition of fields in adaptation field
Adaptation Field
https://gyazo.com/e0922aa0346075477bf8b4a1ac527961
https://cdn-ak.f.st-hatena.com/images/fotolife/a/aton-kish/20181204/20181204003758.png
code:pcr(c)
adaptation_field(){
adaptation_field_length 8 uimsbf
if (adaptation_field_length > 0){
if (PCR_flag == '1') {
program_clock_reference_base 33 uimsbf
}
}
}