2
0

ts_info.cc 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013-2015 SRS(ossrs)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. /**
  20. g++ -o ts_info ts_info.cc -g -O0 -ansi
  21. */
  22. #if 1
  23. // for int64_t print using PRId64 format.
  24. #ifndef __STDC_FORMAT_MACROS
  25. #define __STDC_FORMAT_MACROS
  26. #endif
  27. #include <inttypes.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <assert.h>
  36. #include <vector>
  37. #include <map>
  38. #define trace(msg, ...) printf(msg"\n", ##__VA_ARGS__);
  39. #define srs_freep(p) delete p; p = NULL
  40. #define srs_assert(p) assert(p)
  41. #define srs_min(a, b) ((a)<(b)? (a):(b))
  42. #endif
  43. /**
  44. ISO/IEC 13818-1:2000(E)
  45. Introduction
  46. Intro. 1 Transport Stream
  47. Intro. 2 Program Stream
  48. Intro. 4 Packetized Elementary Stream
  49. SECTION 2 – TECHNICAL ELEMENTS
  50. 2.4 Transport Stream bitstream requirements
  51. 2.4.1 Transport Stream coding structure and parameters
  52. 2.4.2 Transport Stream system target decoder
  53. 2.4.3 Specification of the Transport Stream syntax and semantics
  54. 2.4.3.1 Transport Stream
  55. 2.4.3.2 Transport Stream packet layer
  56. 2.4.3.3 Semantic definition of fields in Transport Stream packet layer
  57. 2.4.3.5 Semantic definition of fields in adaptation field
  58. 2.4.3.6 PES packet
  59. 2.4.3.7 Semantic definition of fields in PES packet
  60. 2.4.4 Program specific information
  61. 2.4.4.5 Semantic definition of fields in program association section
  62. 2.4.4.6 Conditional access Table
  63. 2.5 Program Stream bitstream requirements
  64. 2.6 Program and program element descriptors
  65. 2.7 Restrictions on the multiplexed stream semantics
  66. Annex A CRC Decoder Model
  67. */
  68. #if 1
  69. // Transport Stream packets are 188 bytes in length.
  70. #define TS_PACKET_SIZE 188
  71. enum TSPidTable
  72. {
  73. // Program Association Table(see Table 2-25).
  74. TSPidTablePAT = 0x00,
  75. // Conditional Access Table (see Table 2-27).
  76. TSPidTableCAT = 0x01,
  77. // Transport Stream Description Table
  78. TSPidTableTSDT = 0x02,
  79. // null packets (see Table 2-3)
  80. TSPidTableNULL = 0x01FFF,
  81. };
  82. /*adaptation_field_control*/
  83. /**
  84. * Table 2-5 – Adaptation field control values, page 38.
  85. */
  86. enum TSAdaptionType
  87. {
  88. // Reserved for future use by ISO/IEC
  89. TSAdaptionTypeReserved = 0x00,
  90. // No adaptation_field, payload only
  91. TSAdaptionTypePayloadOnly = 0x01,
  92. // Adaptation_field only, no payload
  93. TSAdaptionTypeAdaptionOnly = 0x02,
  94. // Adaptation_field followed by payload
  95. TSAdaptionTypeBoth = 0x03,
  96. };
  97. #endif
  98. // Table 2-29 – Stream type assignments. page 66.
  99. enum TSStreamType
  100. {
  101. // ITU-T | ISO/IEC Reserved
  102. TSStreamTypeReserved = 0x00,
  103. /*defined by ffmpeg*/
  104. TSStreamTypeVideoMpeg1 = 0x01,
  105. TSStreamTypeVideoMpeg2 = 0x02,
  106. TSStreamTypeAudioMpeg1 = 0x03,
  107. TSStreamTypeAudioMpeg2 = 0x04,
  108. TSStreamTypePrivateSection = 0x05,
  109. TSStreamTypePrivateData = 0x06,
  110. TSStreamTypeAudioAAC = 0x0f,
  111. TSStreamTypeVideoMpeg4 = 0x10,
  112. TSStreamTypeVideoH264 = 0x1b,
  113. TSStreamTypeAudioAC3 = 0x81,
  114. TSStreamTypeAudioDTS = 0x8a,
  115. };
  116. /**
  117. * the actually parsed type.
  118. */
  119. enum TSPidType
  120. {
  121. TSPidTypeReserved = 0, // TSPidTypeReserved, nothing parsed, used reserved.
  122. TSPidTypePAT, // Program associtate table
  123. TSPidTypePMT, // Program map table.
  124. TSPidTypeVideo, // only for H264 video
  125. TSPidTypeAudio, // only for AAC audio
  126. };
  127. // forward declares.
  128. class TSHeader;
  129. class TSAdaptionField;
  130. class TSPayload;
  131. class TSPayloadReserved;
  132. class TSPayloadPAT;
  133. class TSPayloadPMT;
  134. class TSPayloadPES;
  135. class TSContext;
  136. class TSMessage;
  137. // TSPacket declares.
  138. class TSPacket
  139. {
  140. public:
  141. TSHeader* header;
  142. TSAdaptionField* adaption_field;
  143. TSPayload* payload;
  144. TSPacket();
  145. virtual ~TSPacket();
  146. int demux(TSContext* ctx, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  147. int finish();
  148. };
  149. /**
  150. * 2.4.3.2 Transport Stream packet layer. page 36.
  151. */
  152. class TSHeader
  153. {
  154. public:
  155. // 1B
  156. int8_t sync_byte; //8bits
  157. // 2B
  158. int8_t transport_error_indicator; //1bit
  159. int8_t payload_unit_start_indicator; //1bit
  160. int8_t transport_priority; //1bit
  161. TSPidTable pid; //13bits
  162. // 1B
  163. int8_t transport_scrambling_control; //2bits
  164. TSAdaptionType adaption_field_control; //2bits
  165. u_int8_t continuity_counter; //4bits
  166. TSHeader();
  167. virtual ~TSHeader();
  168. int get_size();
  169. int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  170. };
  171. // variant ts packet adation field. page 40.
  172. class TSAdaptionField
  173. {
  174. public:
  175. // 1B
  176. u_int8_t adaption_field_length; //8bits
  177. // 1B
  178. int8_t discontinuity_indicator; //1bit
  179. int8_t random_access_indicator; //1bit
  180. int8_t elementary_stream_priority_indicator; //1bit
  181. int8_t PCR_flag; //1bit
  182. int8_t OPCR_flag; //1bit
  183. int8_t splicing_point_flag; //1bit
  184. int8_t transport_private_data_flag; //1bit
  185. int8_t adaptation_field_extension_flag; //1bit
  186. // if PCR_flag, 6B
  187. int64_t program_clock_reference_base; //33bits
  188. //6bits reserved.
  189. int16_t program_clock_reference_extension; //9bits
  190. // if OPCR_flag, 6B
  191. int64_t original_program_clock_reference_base; //33bits
  192. //6bits reserved.
  193. int16_t original_program_clock_reference_extension; //9bits
  194. // if splicing_point_flag, 1B
  195. int8_t splice_countdown; //8bits
  196. // if transport_private_data_flag, 1+p[0] B
  197. u_int8_t transport_private_data_length; //8bits
  198. char* transport_private_data; //[transport_private_data_length]bytes
  199. // if adaptation_field_extension_flag, 2+x bytes
  200. u_int8_t adaptation_field_extension_length; //8bits
  201. int8_t ltw_flag; //1bit
  202. int8_t piecewise_rate_flag; //1bit
  203. int8_t seamless_splice_flag; //1bit
  204. //5bits reserved
  205. // if ltw_flag, 2B
  206. int8_t ltw_valid_flag; //1bit
  207. int16_t ltw_offset; //15bits
  208. // if piecewise_rate_flag, 3B
  209. //2bits reserved
  210. int32_t piecewise_rate; //22bits
  211. // if seamless_splice_flag, 5B
  212. int8_t splice_type; //4bits
  213. int8_t DTS_next_AU0; //3bits
  214. int8_t marker_bit0; //1bit
  215. int16_t DTS_next_AU1; //15bits
  216. int8_t marker_bit1; //1bit
  217. int16_t DTS_next_AU2; //15bits
  218. int8_t marker_bit2; //1bit
  219. // left bytes.
  220. char* af_ext_reserved;
  221. // left bytes.
  222. char* af_reserved;
  223. // user defined total adaption field size.
  224. int __field_size;
  225. // logic pcr/original_pcr
  226. int64_t pcr;
  227. int64_t original_pcr;
  228. TSAdaptionField();
  229. virtual ~TSAdaptionField();
  230. int get_size();
  231. int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  232. };
  233. // variant ts packet payload.
  234. // PES packet or PSI table.
  235. // TSPayloadPAT: page 61.
  236. class TSPayload
  237. {
  238. public:
  239. /**
  240. * the size of payload(payload plush the 1byte pointer_field).
  241. */
  242. int size;
  243. int pointer_field_size;
  244. TSPidType type;
  245. /**
  246. * 2.4.4.2 Semantics definition of fields in pointer syntax
  247. */
  248. u_int8_t pointer_field;
  249. TSPayloadReserved* reserved;
  250. TSPayloadPAT* pat;
  251. TSPayloadPMT* pmt;
  252. TSPayloadPES* pes;
  253. /**
  254. * 2.4.3.6 PES packet. page 49.
  255. */
  256. TSPayload();
  257. virtual ~TSPayload();;
  258. void read_pointer_field(TSPacket* pkt, u_int8_t*& p);
  259. int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  260. };
  261. /**
  262. * 2.4.4.3 Program association Table. page 61.
  263. */
  264. class TSPayloadPAT
  265. {
  266. public:
  267. // 1B
  268. u_int8_t table_id; //8bits
  269. // 2B
  270. int8_t section_syntax_indicator; //1bit
  271. int8_t const0_value; //1bit
  272. // 2bits reserved.
  273. u_int16_t section_length; //12bits
  274. // 2B
  275. u_int16_t transport_stream_id; //16bits
  276. // 1B
  277. // 2bits reerverd.
  278. int8_t version_number; //5bits
  279. int8_t current_next_indicator; //1bit
  280. // 1B
  281. u_int8_t section_number; //8bits
  282. // 1B
  283. u_int8_t last_section_number; //8bits
  284. // multiple 4B program data.
  285. // program_number 16bits
  286. // reserved 2bits
  287. // 13bits data: 0x1FFF
  288. // if program_number program_map_PID 13bits
  289. // else network_PID 13bytes.
  290. int program_size;
  291. int32_t* programs; //32bits
  292. // 4B
  293. int32_t CRC_32; //32bits
  294. TSPayloadPAT();
  295. virtual ~TSPayloadPAT();
  296. int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  297. };
  298. class TSPMTESInfo
  299. {
  300. public:
  301. // 1B
  302. u_int8_t stream_type; //8bits
  303. // 2B
  304. // 3bits reserved
  305. int16_t elementary_PID; //13bits
  306. // 2B
  307. // 4bits reserved
  308. int16_t ES_info_length; //12bits
  309. char* ES_info; //[ES_info_length] bytes.
  310. TSPMTESInfo();
  311. virtual ~TSPMTESInfo();
  312. };
  313. /**
  314. * 2.4.4.8 Program Map Table. page 64.
  315. */
  316. class TSPayloadPMT
  317. {
  318. public:
  319. // 1B
  320. u_int8_t table_id; //8bits
  321. // 2B
  322. int8_t section_syntax_indicator; //1bit
  323. int8_t const0_value; //1bit
  324. // 2bits reserved.
  325. u_int16_t section_length; //12bits
  326. // 2B
  327. u_int16_t program_number; //16bits
  328. // 1B
  329. // 2bits reerverd.
  330. int8_t version_number; //5bits
  331. int8_t current_next_indicator; //1bit
  332. // 1B
  333. u_int8_t section_number; //8bits
  334. // 1B
  335. u_int8_t last_section_number; //8bits
  336. // 2B
  337. // 2bits reserved.
  338. int16_t PCR_PID; //16bits
  339. // 2B
  340. // 4bits reserved.
  341. int16_t program_info_length; //12bits
  342. char* program_info_desc; //[program_info_length]bytes
  343. // array of TSPMTESInfo.
  344. std::vector<TSPMTESInfo*> ES_info;
  345. // 4B
  346. int32_t CRC_32; //32bits
  347. TSPayloadPMT();
  348. virtual ~TSPayloadPMT();
  349. TSPMTESInfo* at(int index);
  350. int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  351. };
  352. /**
  353. * Table 2-18 – Stream_id assignments. page 52.
  354. */
  355. enum TSPESStreamId
  356. {
  357. PES_program_stream_map = 0xbc, // 0b10111100
  358. PES_private_stream_1 = 0xbd, // 0b10111101
  359. PES_padding_stream = 0xbe, // 0b10111110
  360. PES_private_stream_2 = 0xbf, // 0b10111111
  361. // 110x xxxx
  362. // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC
  363. // 14496-3 audio stream number x xxxx
  364. // (stream_id>>5)&0x07 == PES_audio_prefix
  365. PES_audio_prefix = 0x06, // 0b110
  366. // 1110 xxxx
  367. // ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC
  368. // 14496-2 video stream number xxxx
  369. // (stream_id>>4)&0x0f == PES_audio_prefix
  370. PES_video_prefix = 0x0e, // 0b1110
  371. PES_ECM_stream = 0xf0, // 0b11110000
  372. PES_EMM_stream = 0xf1, // 0b11110001
  373. PES_DSMCC_stream = 0xf2, // 0b11110010
  374. PES_13522_stream = 0xf3, // 0b11110011
  375. PES_H_222_1_type_A = 0xf4, // 0b11110100
  376. PES_H_222_1_type_B = 0xf5, // 0b11110101
  377. PES_H_222_1_type_C = 0xf6, // 0b11110110
  378. PES_H_222_1_type_D = 0xf7, // 0b11110111
  379. PES_H_222_1_type_E = 0xf8, // 0b11111000
  380. PES_ancillary_stream = 0xf9, // 0b11111001
  381. PES_SL_packetized_stream = 0xfa, // 0b11111010
  382. PES_FlexMux_stream = 0xfb, // 0b11111011
  383. // reserved data stream
  384. // 1111 1100 … 1111 1110
  385. PES_program_stream_directory= 0xff, // 0b11111111
  386. };
  387. /**
  388. * 2.4.3.7 Semantic definition of fields in PES packet. page 49.
  389. */
  390. class TSPayloadPES
  391. {
  392. public:
  393. // 3B
  394. int32_t packet_start_code_prefix; //24bits
  395. // 1B
  396. u_int8_t stream_id; //8bits
  397. // 2B
  398. u_int16_t PES_packet_length; //16bits
  399. // 1B
  400. // 2bits const '10'
  401. int8_t PES_scrambling_control; //2bits
  402. int8_t PES_priority; //1bit
  403. int8_t data_alignment_indicator; //1bit
  404. int8_t copyright; //1bit
  405. int8_t original_or_copy; //1bit
  406. // 1B
  407. int8_t PTS_DTS_flags; //2bits
  408. int8_t ESCR_flag; //1bit
  409. int8_t ES_rate_flag; //1bit
  410. int8_t DSM_trick_mode_flag; //1bit
  411. int8_t additional_copy_info_flag; //1bit
  412. int8_t PES_CRC_flag; //1bit
  413. int8_t PES_extension_flag; //1bit
  414. // 1B
  415. u_int8_t PES_header_data_length; //8bits
  416. int64_t pts; // 33bits
  417. int64_t dts; // 33bits
  418. int16_t ESCR_extension; //9bits
  419. int64_t ESCR_base; //33bits
  420. int32_t ES_rate; //22bits
  421. int8_t trick_mode_control; //3bits
  422. int8_t trick_mode_value; //5bits
  423. int8_t additional_copy_info; //7bits
  424. int16_t previous_PES_packet_CRC; //16bits
  425. int8_t PES_private_data_flag; //1bit
  426. int8_t pack_header_field_flag; //1bit
  427. int8_t program_packet_sequence_counter_flag; //1bit
  428. int8_t P_STD_buffer_flag; //1bit
  429. // reserved 3bits
  430. int8_t PES_extension_flag_2; //1bit
  431. // 16B
  432. char* PES_private_data; //128bits
  433. int8_t pack_field_length; //8bits
  434. char* pack_field; //[pack_field_length] bytes
  435. int8_t program_packet_sequence_counter; //7bits
  436. int8_t MPEG1_MPEG2_identifier; //1bit
  437. int8_t original_stuff_length; //6bits
  438. int8_t P_STD_buffer_scale; //1bit
  439. int16_t P_STD_buffer_size; //13bits
  440. int8_t PES_extension_field_length; //7bits
  441. char* PES_extension_field; //[PES_extension_field_length] bytes
  442. int stuffing_size;
  443. char* stuffing_byte;
  444. TSPayloadPES();
  445. virtual ~TSPayloadPES();
  446. int64_t decode_33bits_int(u_int8_t*& p, int64_t& temp);
  447. int64_t decode_33bits_int(int64_t& temp);
  448. int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  449. };
  450. class TSPayloadReserved
  451. {
  452. public:
  453. int size;
  454. char* bytes;
  455. TSPayloadReserved();
  456. virtual ~TSPayloadReserved();
  457. int demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg);
  458. };
  459. /**
  460. * logic ts pid.
  461. */
  462. struct TSPid
  463. {
  464. TSPidType type;
  465. // page 66.
  466. TSStreamType stream_type;
  467. // page 36
  468. TSPidTable pid;
  469. // page 36
  470. u_int8_t continuity_counter;
  471. };
  472. /**
  473. * logic audio/video message
  474. */
  475. class TSMessage
  476. {
  477. public:
  478. // 2.4.3.2 Transport Stream packet layer. page 36
  479. // the pid of PES packet.
  480. TSPidTable pid;
  481. // the type of pid.
  482. TSPidType type;
  483. // the type of stream, codec type.
  484. TSStreamType stream_type;
  485. // page 36
  486. u_int8_t continuity_counter;
  487. // 2.4.3.7 Semantic definition of fields in PES packet. page 49
  488. // PES packet header size plus data size.
  489. u_int16_t PES_packet_length; //16bits
  490. // the stream id.
  491. u_int8_t stream_id;
  492. // 2.4.3.7 Semantic definition of fields in PES packet. page 49.
  493. int32_t packet_start_code_prefix;
  494. int64_t pts; // 33bits
  495. int64_t dts; // 33bits
  496. int64_t pcr;
  497. // header size.
  498. int packet_header_size;
  499. // the parsed packet size.
  500. int parsed_packet_size;
  501. // total packet size.
  502. int packet_data_size;
  503. char* packet_data;
  504. // for avc.
  505. u_int8_t nal_ref_idc;
  506. u_int8_t nal_unit_type;
  507. TSMessage();
  508. virtual ~TSMessage();
  509. void append(u_int8_t*& p, int size);
  510. void detach(TSContext* ctx, TSMessage*& pmsg);
  511. bool is_video();
  512. };
  513. // ts context
  514. class TSContext
  515. {
  516. public:
  517. /**
  518. * consumed pids.
  519. */
  520. int pid_size;
  521. TSPid* pids;
  522. int64_t ts_packet_count;
  523. std::map<TSPidTable, TSMessage*> msgs;
  524. TSContext();
  525. virtual ~TSContext();
  526. bool exists(TSPidTable pid);
  527. TSPid* get(TSPidTable pid);
  528. void push(TSPidTable pid, TSStreamType stream_type, TSPidType type, u_int8_t continuity_counter);
  529. TSMessage* get_msg(TSPidTable pid);
  530. void detach(TSMessage* msg);
  531. };
  532. TSContext::TSContext()
  533. {
  534. pid_size = 0;
  535. ts_packet_count = 0;
  536. pids = NULL;
  537. }
  538. TSContext::~TSContext()
  539. {
  540. srs_freep(pids);
  541. std::map<TSPidTable, TSMessage*>::iterator it;
  542. for (it = msgs.begin(); it != msgs.end(); ++it) {
  543. TSMessage* msg = it->second;
  544. srs_freep(msg);
  545. }
  546. msgs.clear();
  547. }
  548. bool TSContext::exists(TSPidTable pid)
  549. {
  550. for (int i = 0; i < pid_size; i++) {
  551. if (pid == pids[i].pid) {
  552. return true;
  553. }
  554. }
  555. return false;
  556. }
  557. TSPid* TSContext::get(TSPidTable pid)
  558. {
  559. for (int i = 0; i < pid_size; i++) {
  560. if (pid == pids[i].pid) {
  561. return &pids[i];
  562. }
  563. }
  564. return NULL;
  565. }
  566. void TSContext::push(TSPidTable pid, TSStreamType stream_type, TSPidType type, u_int8_t continuity_counter)
  567. {
  568. TSPid* p = get(pid);
  569. if (!p) {
  570. p = new TSPid[pid_size + 1];
  571. memcpy(p, pids, sizeof(TSPid) * pid_size);
  572. p[pid_size] = (TSPid){type, stream_type, pid, continuity_counter};
  573. pid_size++;
  574. srs_freep(pids);
  575. pids = p;
  576. }
  577. p->continuity_counter = continuity_counter;
  578. }
  579. TSMessage* TSContext::get_msg(TSPidTable pid)
  580. {
  581. if (msgs[pid] == NULL) {
  582. TSMessage* msg = new TSMessage();
  583. msg->pid = pid;
  584. msgs[pid] = msg;
  585. }
  586. return msgs[pid];
  587. }
  588. void TSContext::detach(TSMessage* msg)
  589. {
  590. msgs[msg->pid] = NULL;
  591. }
  592. TSMessage::TSMessage()
  593. {
  594. pid = TSPidTablePAT;
  595. type = TSPidTypeReserved;
  596. stream_type = TSStreamTypeReserved;
  597. stream_id = 0;
  598. packet_start_code_prefix = 0;
  599. pts = dts = pcr = 0;
  600. PES_packet_length = 0;
  601. packet_header_size = 0;
  602. parsed_packet_size = 0;
  603. packet_data_size = 0;
  604. packet_data = NULL;
  605. nal_ref_idc = 0;
  606. nal_unit_type = 0;
  607. }
  608. TSMessage::~TSMessage()
  609. {
  610. srs_freep(packet_data);
  611. }
  612. void TSMessage::append(u_int8_t*& p, int size)
  613. {
  614. if (size <= 0) {
  615. return;
  616. }
  617. // for PES_packet_length is 0, the size is varient.
  618. if (packet_data_size - parsed_packet_size < size) {
  619. int realloc_size = size - (packet_data_size - parsed_packet_size);
  620. packet_data = (char*)realloc(packet_data, packet_data_size + realloc_size);
  621. packet_data_size += realloc_size;
  622. }
  623. memcpy(packet_data + parsed_packet_size, p, size);
  624. p += size;
  625. parsed_packet_size += size;
  626. }
  627. void TSMessage::detach(TSContext* ctx, TSMessage*& pmsg)
  628. {
  629. if (parsed_packet_size >= packet_data_size) {
  630. ctx->detach(this);
  631. pmsg = this;
  632. }
  633. }
  634. bool TSMessage::is_video()
  635. {
  636. return type == TSPidTypeVideo;
  637. }
  638. TSAdaptionField::TSAdaptionField()
  639. {
  640. adaption_field_length = 0;
  641. discontinuity_indicator = 0;
  642. random_access_indicator = 0;
  643. elementary_stream_priority_indicator = 0;
  644. PCR_flag = 0;
  645. OPCR_flag = 0;
  646. splicing_point_flag = 0;
  647. transport_private_data_flag = 0;
  648. adaptation_field_extension_flag = 0;
  649. program_clock_reference_base = 0;
  650. program_clock_reference_extension = 0;
  651. original_program_clock_reference_base = 0;
  652. original_program_clock_reference_extension = 0;
  653. splice_countdown = 0;
  654. transport_private_data_length = 0;
  655. transport_private_data = NULL;
  656. adaptation_field_extension_length = 0;
  657. ltw_flag = 0;
  658. piecewise_rate_flag = 0;
  659. seamless_splice_flag = 0;
  660. ltw_valid_flag = 0;
  661. ltw_offset = 0;
  662. piecewise_rate = 0;
  663. splice_type = 0;
  664. DTS_next_AU0 = 0;
  665. marker_bit0 = 0;
  666. DTS_next_AU1 = 0;
  667. marker_bit1 = 0;
  668. DTS_next_AU2 = 0;
  669. marker_bit2 = 0;
  670. af_ext_reserved = NULL;
  671. af_reserved = NULL;
  672. __field_size = 0;
  673. pcr = 0;
  674. original_pcr = 0;
  675. }
  676. TSAdaptionField::~TSAdaptionField()
  677. {
  678. srs_freep(transport_private_data);
  679. srs_freep(af_ext_reserved);
  680. srs_freep(af_reserved);
  681. }
  682. int TSAdaptionField::get_size()
  683. {
  684. return __field_size;
  685. }
  686. int TSAdaptionField::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  687. {
  688. int ret = 0;
  689. adaption_field_length = *p++;
  690. u_int8_t* pos_af = p;
  691. __field_size = 1 + adaption_field_length;
  692. if (adaption_field_length <= 0) {
  693. trace("ts+af empty af decoded.");
  694. return ret;
  695. }
  696. int8_t value = *p++;
  697. discontinuity_indicator = (value >> 7) & 0x01;
  698. random_access_indicator = (value >> 6) & 0x01;
  699. elementary_stream_priority_indicator = (value >> 5) & 0x01;
  700. PCR_flag = (value >> 4) & 0x01;
  701. OPCR_flag = (value >> 3) & 0x01;
  702. splicing_point_flag = (value >> 2) & 0x01;
  703. transport_private_data_flag = (value >> 1) & 0x01;
  704. adaptation_field_extension_flag = (value >> 0) & 0x01;
  705. char* pp = NULL;
  706. if (PCR_flag) {
  707. pp = (char*)&program_clock_reference_base;
  708. pp[5] = *p++;
  709. pp[4] = *p++;
  710. pp[3] = *p++;
  711. pp[2] = *p++;
  712. pp[1] = *p++;
  713. pp[0] = *p++;
  714. program_clock_reference_extension = program_clock_reference_base & 0x1ff;
  715. program_clock_reference_base = (program_clock_reference_base >> 15) & 0x1ffffffffLL;
  716. // high 9bits
  717. pcr = program_clock_reference_extension;
  718. pcr = (pcr << 33) & 0x3fe00000000LL;
  719. // low 33bits
  720. pcr |= program_clock_reference_base;
  721. }
  722. if (OPCR_flag) {
  723. pp = (char*)&original_program_clock_reference_base;
  724. pp[5] = *p++;
  725. pp[4] = *p++;
  726. pp[3] = *p++;
  727. pp[2] = *p++;
  728. pp[1] = *p++;
  729. pp[0] = *p++;
  730. original_program_clock_reference_extension = original_program_clock_reference_base & 0x1ff;
  731. original_program_clock_reference_base = (original_program_clock_reference_base >> 15) & 0x1ffffffffLL;
  732. // high 9bits
  733. original_pcr = program_clock_reference_extension;
  734. original_pcr = (original_pcr << 33) & 0x3fe00000000LL;
  735. // low 33bits
  736. original_pcr |= program_clock_reference_base;
  737. }
  738. if (splicing_point_flag) {
  739. splice_countdown = *p++;
  740. }
  741. if (transport_private_data_flag) {
  742. transport_private_data_length = *p++;
  743. transport_private_data = new char[transport_private_data_length];
  744. for (int i = 0; i < transport_private_data_length; i++) {
  745. transport_private_data[i] = *p++;
  746. }
  747. }
  748. if (adaptation_field_extension_flag) {
  749. adaptation_field_extension_length = *p++;
  750. u_int8_t* pos_af_ext = p;
  751. ltw_flag = *p++;
  752. piecewise_rate_flag = (ltw_flag >> 6) & 0x01;
  753. seamless_splice_flag = (ltw_flag >> 5) & 0x01;
  754. ltw_flag = (ltw_flag >> 7) & 0x01;
  755. if (ltw_flag) {
  756. pp = (char*)&ltw_offset;
  757. pp[1] = *p++;
  758. pp[0] = *p++;
  759. ltw_valid_flag = (ltw_offset >> 15) &0x01;
  760. ltw_offset &= 0x7FFF;
  761. }
  762. if (piecewise_rate_flag) {
  763. pp = (char*)&piecewise_rate;
  764. pp[2] = *p++;
  765. pp[1] = *p++;
  766. pp[0] = *p++;
  767. piecewise_rate &= 0x3FFFFF;
  768. }
  769. if (seamless_splice_flag) {
  770. // 1B
  771. marker_bit0 = *p++;
  772. splice_type = (marker_bit0 >> 4) & 0x0F;
  773. DTS_next_AU0 = (marker_bit0 >> 1) & 0x07;
  774. marker_bit0 &= 0x01;
  775. // 2B
  776. pp = (char*)&DTS_next_AU1;
  777. pp[1] = *p++;
  778. pp[0] = *p++;
  779. marker_bit1 = DTS_next_AU1 & 0x01;
  780. DTS_next_AU1 = (DTS_next_AU1 >> 1) & 0x7FFF;
  781. // 2B
  782. pp = (char*)&DTS_next_AU2;
  783. pp[1] = *p++;
  784. pp[0] = *p++;
  785. marker_bit2 = DTS_next_AU2 & 0x01;
  786. DTS_next_AU2 = (DTS_next_AU2 >> 1) & 0x7FFF;
  787. }
  788. // af_ext_reserved
  789. int ext_size = adaptation_field_extension_length - (p - pos_af_ext);
  790. if (ext_size > 0) {
  791. af_ext_reserved = new char[ext_size];
  792. memcpy(af_ext_reserved, p, ext_size);
  793. p += ext_size;
  794. }
  795. }
  796. // af_reserved
  797. int af_size = adaption_field_length - (p - pos_af);
  798. if (af_size > 0) {
  799. af_reserved = new char[af_size];
  800. memcpy(af_reserved, p, af_size);
  801. p += af_size;
  802. }
  803. trace("ts+af af flags parsed, discontinuity: %d random: %d priority: %d PCR: %d OPCR: %d slicing: %d private: %d extension: %d pcr: %"PRId64" opcr: %"PRId64"",
  804. discontinuity_indicator, random_access_indicator, elementary_stream_priority_indicator, PCR_flag, OPCR_flag, splicing_point_flag,
  805. transport_private_data_flag, adaptation_field_extension_flag, pcr, original_pcr);
  806. return ret;
  807. }
  808. TSPayloadReserved::TSPayloadReserved()
  809. {
  810. size = 0;
  811. bytes = NULL;
  812. }
  813. TSPayloadReserved::~TSPayloadReserved()
  814. {
  815. srs_freep(bytes);
  816. }
  817. int TSPayloadReserved::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  818. {
  819. int ret = 0;
  820. size = pkt->payload->size - pkt->payload->pointer_field_size;
  821. // not parsed bytes.
  822. if (size > 0) {
  823. bytes = new char[size];
  824. memcpy(bytes, p, size);
  825. p += size;
  826. }
  827. return ret;
  828. }
  829. TSPayloadPAT::TSPayloadPAT()
  830. {
  831. table_id = 0;
  832. section_syntax_indicator = 0;
  833. const0_value = 0;
  834. section_length = 0;
  835. transport_stream_id = 0;
  836. version_number = 0;
  837. current_next_indicator = 0;
  838. section_number = 0;
  839. last_section_number = 0;
  840. program_size = 0;
  841. programs = NULL;
  842. CRC_32 = 0;
  843. }
  844. TSPayloadPAT::~TSPayloadPAT()
  845. {
  846. srs_freep(programs);
  847. }
  848. int TSPayloadPAT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  849. {
  850. int ret = 0;
  851. table_id = *p++;
  852. char* pp = (char*)&section_length;
  853. pp[1] = *p++;
  854. pp[0] = *p++;
  855. u_int8_t* pos = p;
  856. section_syntax_indicator = (section_length >> 15) & 0x01;
  857. const0_value = (section_length >> 14) & 0x01;
  858. section_length &= 0x0FFF;
  859. pp = (char*)&transport_stream_id;
  860. pp[1] = *p++;
  861. pp[0] = *p++;
  862. current_next_indicator = *p++;
  863. version_number = (current_next_indicator >> 1) & 0x1F;
  864. current_next_indicator &= 0x01;
  865. section_number = *p++;
  866. last_section_number = *p++;
  867. // 4 is crc size.
  868. int program_bytes = section_length - 4 - (p - pos);
  869. program_size = program_bytes / 4;
  870. if (program_size > 0) {
  871. programs = new int32_t[program_size];
  872. for (int i = 0; i < program_size; i++) {
  873. pp = (char*)&programs[i];
  874. pp[3] = *p++;
  875. pp[2] = *p++;
  876. pp[1] = *p++;
  877. pp[0] = *p++;
  878. int16_t pid = programs[i] & 0x1FFF;
  879. ctx->push((TSPidTable)pid, TSStreamTypeReserved, TSPidTypePMT, pkt->header->continuity_counter);
  880. }
  881. }
  882. pp = (char*)&CRC_32;
  883. pp[3] = *p++;
  884. pp[2] = *p++;
  885. pp[1] = *p++;
  886. pp[0] = *p++;
  887. return ret;
  888. }
  889. TSPMTESInfo::TSPMTESInfo()
  890. {
  891. stream_type = 0;
  892. elementary_PID = 0;
  893. ES_info_length = 0;
  894. ES_info = NULL;
  895. }
  896. TSPMTESInfo::~TSPMTESInfo()
  897. {
  898. srs_freep(ES_info);
  899. }
  900. TSPayloadPMT::TSPayloadPMT()
  901. {
  902. table_id = 0;
  903. section_syntax_indicator = 0;
  904. const0_value = 0;
  905. section_length = 0;
  906. program_number = 0;
  907. version_number = 0;
  908. current_next_indicator = 0;
  909. section_number = 0;
  910. last_section_number = 0;
  911. PCR_PID = 0;
  912. program_info_length = 0;
  913. program_info_desc = NULL;
  914. CRC_32 = 0;
  915. }
  916. TSPayloadPMT::~TSPayloadPMT()
  917. {
  918. srs_freep(program_info_desc);
  919. for (std::vector<TSPMTESInfo*>::iterator it = ES_info.begin(); it != ES_info.end(); ++it) {
  920. TSPMTESInfo* info = *it;
  921. srs_freep(info);
  922. }
  923. ES_info.clear();
  924. }
  925. TSPMTESInfo* TSPayloadPMT::at(int index)
  926. {
  927. return ES_info.at(index);
  928. }
  929. int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  930. {
  931. int ret = 0;
  932. table_id = *p++;
  933. char* pp = (char*)&section_length;
  934. pp[1] = *p++;
  935. pp[0] = *p++;
  936. u_int8_t* pos = p;
  937. section_syntax_indicator = (section_length >> 15) & 0x01;
  938. const0_value = (section_length >> 14) & 0x01;
  939. section_length &= 0x0FFF;
  940. pp = (char*)&program_number;
  941. pp[1] = *p++;
  942. pp[0] = *p++;
  943. current_next_indicator = *p++;
  944. version_number = (current_next_indicator >> 1) & 0x1F;
  945. current_next_indicator &= 0x01;
  946. section_number = *p++;
  947. last_section_number = *p++;
  948. pp = (char*)&PCR_PID;
  949. pp[1] = *p++;
  950. pp[0] = *p++;
  951. PCR_PID &= 0x1FFF;
  952. pp = (char*)&program_info_length;
  953. pp[1] = *p++;
  954. pp[0] = *p++;
  955. program_info_length &= 0xFFF;
  956. if (program_info_length > 0) {
  957. program_info_desc = new char[program_info_length];
  958. memcpy(program_info_desc, p, program_info_length);
  959. p += program_info_length;
  960. }
  961. // [section_length] - 4(CRC) - 9B - [program_info_length]
  962. int ES_bytes = section_length - 4 - 9 - program_info_length;
  963. while (ES_bytes > 0) {
  964. TSPMTESInfo* info = new TSPMTESInfo();
  965. info->stream_type = *p++;
  966. ES_bytes--;
  967. pp = (char*)&info->elementary_PID;
  968. pp[1] = *p++;
  969. pp[0] = *p++;
  970. ES_bytes -= 2;
  971. info->elementary_PID &= 0x1FFF;
  972. pp = (char*)&info->ES_info_length;
  973. pp[1] = *p++;
  974. pp[0] = *p++;
  975. ES_bytes -= 2;
  976. info->ES_info_length &= 0x0FFF;
  977. if (info->ES_info_length > 0) {
  978. info->ES_info = new char[info->ES_info_length];
  979. memcpy(info->ES_info, p, info->ES_info_length);
  980. p += info->ES_info_length;
  981. ES_bytes -= info->ES_info_length;
  982. }
  983. ES_info.push_back(info);
  984. if (info->stream_type == TSStreamTypeVideoH264) {
  985. // TODO: support more video type.
  986. ctx->push((TSPidTable)info->elementary_PID, (TSStreamType)info->stream_type, TSPidTypeVideo, pkt->header->continuity_counter);
  987. trace("ts+pmt add pid: %d, type: H264 video", info->elementary_PID);
  988. } else if (info->stream_type == TSStreamTypeAudioAAC) {
  989. // TODO: support more audio type.
  990. // see aac: 6.2 Audio Data Transport Stream, ADTS
  991. ctx->push((TSPidTable)info->elementary_PID, (TSStreamType)info->stream_type, TSPidTypeAudio, pkt->header->continuity_counter);
  992. trace("ts+pmt add pid: %d, type: AAC audio", info->elementary_PID);
  993. } else {
  994. trace("ts+pmt ignore the stream type: %d", info->stream_type);
  995. }
  996. }
  997. pp = (char*)&CRC_32;
  998. pp[3] = *p++;
  999. pp[2] = *p++;
  1000. pp[1] = *p++;
  1001. pp[0] = *p++;
  1002. return ret;
  1003. }
  1004. TSPayloadPES::TSPayloadPES()
  1005. {
  1006. packet_start_code_prefix = 0;
  1007. stream_id = 0;
  1008. PES_packet_length = 0;
  1009. PES_scrambling_control = 0;
  1010. PES_priority = 0;
  1011. data_alignment_indicator = 0;
  1012. copyright = 0;
  1013. original_or_copy = 0;
  1014. PTS_DTS_flags = 0;
  1015. ESCR_flag = 0;
  1016. ES_rate_flag = 0;
  1017. DSM_trick_mode_flag = 0;
  1018. additional_copy_info_flag = 0;
  1019. PES_CRC_flag = 0;
  1020. PES_extension_flag = 0;
  1021. PES_header_data_length = 0;
  1022. pts = 0;
  1023. dts = 0;
  1024. ESCR_extension = 0;
  1025. ESCR_base = 0;
  1026. ES_rate = 0;
  1027. trick_mode_control = 0;
  1028. trick_mode_value = 0;
  1029. additional_copy_info = 0;
  1030. previous_PES_packet_CRC = 0;
  1031. PES_private_data_flag = 0;
  1032. pack_header_field_flag = 0;
  1033. program_packet_sequence_counter_flag = 0;
  1034. P_STD_buffer_flag = 0;
  1035. PES_extension_flag_2 = 0;
  1036. PES_private_data = NULL;
  1037. pack_field_length = 0;
  1038. pack_field = NULL;
  1039. program_packet_sequence_counter = 0;
  1040. MPEG1_MPEG2_identifier = 0;
  1041. original_stuff_length = 0;
  1042. P_STD_buffer_scale = 0;
  1043. P_STD_buffer_size = 0;
  1044. PES_extension_field_length = 0;
  1045. PES_extension_field = NULL;
  1046. stuffing_size = 0;
  1047. stuffing_byte = NULL;
  1048. }
  1049. TSPayloadPES::~TSPayloadPES()
  1050. {
  1051. srs_freep(PES_private_data);
  1052. srs_freep(pack_field);
  1053. srs_freep(PES_extension_field);
  1054. srs_freep(stuffing_byte);
  1055. }
  1056. int64_t TSPayloadPES::decode_33bits_int(u_int8_t*& p, int64_t& temp)
  1057. {
  1058. char* pp = (char*)&temp;
  1059. pp[4] = *p++;
  1060. pp[3] = *p++;
  1061. pp[2] = *p++;
  1062. pp[1] = *p++;
  1063. pp[0] = *p++;
  1064. return decode_33bits_int(temp);
  1065. }
  1066. int64_t TSPayloadPES::decode_33bits_int(int64_t& temp)
  1067. {
  1068. int64_t ret = 0;
  1069. // marker_bit 1bit
  1070. temp = temp >> 1;
  1071. // PTS [14..0] 15bits
  1072. ret |= temp & 0x7fff;
  1073. // marker_bit 1bit
  1074. temp = temp >> 1;
  1075. // PTS [29..15] 15bits, 15zero, 29-15+1one
  1076. ret |= temp & 0x3fff8000LL;
  1077. // marker_bit 1bit
  1078. temp = temp >> 1;
  1079. // PTS [32..30] 3bits
  1080. ret |= temp & 0x1c0000000LL;
  1081. temp = temp >> 33;
  1082. return ret;
  1083. }
  1084. int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  1085. {
  1086. int ret = 0;
  1087. char* pp = (char*)&packet_start_code_prefix;
  1088. pp[2] = *p++;
  1089. pp[1] = *p++;
  1090. pp[0] = *p++;
  1091. packet_start_code_prefix &= 0xFFFFFF;
  1092. if (packet_start_code_prefix != 0x01) {
  1093. trace("ts+pes decode unit start packet error, msg is empty.");
  1094. return -1;
  1095. }
  1096. stream_id = *p++;
  1097. pp = (char*)&PES_packet_length;
  1098. pp[1] = *p++;
  1099. pp[0] = *p++;
  1100. u_int8_t* pos_packet = p;
  1101. if (stream_id != PES_program_stream_map
  1102. && stream_id != PES_padding_stream
  1103. && stream_id != PES_private_stream_2
  1104. && stream_id != PES_ECM_stream
  1105. && stream_id != PES_EMM_stream
  1106. && stream_id != PES_program_stream_directory
  1107. && stream_id != PES_DSMCC_stream
  1108. && stream_id != PES_H_222_1_type_E
  1109. ) {
  1110. original_or_copy = *p++;
  1111. //int8_t const2bits = (original_or_copy >> 6) & 0x03;
  1112. PES_scrambling_control = (original_or_copy >> 4) & 0x03;
  1113. PES_priority = (original_or_copy >> 3) & 0x01;
  1114. data_alignment_indicator = (original_or_copy >> 2) & 0x01;
  1115. copyright = (original_or_copy >> 1) & 0x01;
  1116. original_or_copy &= 0x01;
  1117. PES_extension_flag = *p++;
  1118. PTS_DTS_flags = (PES_extension_flag >> 6) & 0x03;
  1119. ESCR_flag = (PES_extension_flag >> 5) & 0x01;
  1120. ES_rate_flag = (PES_extension_flag >> 4) & 0x01;
  1121. DSM_trick_mode_flag = (PES_extension_flag >> 3) & 0x01;
  1122. additional_copy_info_flag = (PES_extension_flag >> 2) & 0x01;
  1123. PES_CRC_flag = (PES_extension_flag >> 1) & 0x01;
  1124. PES_extension_flag &= 0x01;
  1125. PES_header_data_length = *p++;
  1126. u_int8_t* pos_header = p;
  1127. int64_t temp = 0;
  1128. if (PTS_DTS_flags == 0x2) {
  1129. pts = decode_33bits_int(p, temp);
  1130. // '0010' 4bits
  1131. //int8_t const4bits = temp & 0x0F;
  1132. }
  1133. if (PTS_DTS_flags == 0x3) {
  1134. pts = decode_33bits_int(p, temp);
  1135. // '0011' 4bits
  1136. //int8_t const4bits = temp & 0x0F;
  1137. dts = decode_33bits_int(p, temp);
  1138. // '0001' 4bits
  1139. //int8_t const4bits = temp & 0x0F;
  1140. }
  1141. if (ESCR_flag) {
  1142. pp = (char*)&temp;
  1143. pp[5] = *p++;
  1144. pp[4] = *p++;
  1145. pp[3] = *p++;
  1146. pp[2] = *p++;
  1147. pp[1] = *p++;
  1148. pp[0] = *p++;
  1149. // marker_bit 1bit
  1150. temp = temp >> 1;
  1151. // ESCR_extension 9bits
  1152. ESCR_extension = temp & 0x1f;
  1153. temp = temp >> 9;
  1154. ESCR_base = decode_33bits_int(temp);
  1155. // reserved 2bits
  1156. //int8_t reserved2bits = temp & 0x03;
  1157. }
  1158. if (ES_rate_flag) {
  1159. pp = (char*)&ES_rate;
  1160. pp[2] = *p++;
  1161. pp[1] = *p++;
  1162. pp[0] = *p++;
  1163. ES_rate = ES_rate >> 1;
  1164. ES_rate &= 0x3FFFFF;
  1165. }
  1166. if (DSM_trick_mode_flag) {
  1167. trick_mode_control = *p++;
  1168. trick_mode_value = trick_mode_control & 0x1f;
  1169. trick_mode_control = (trick_mode_control >> 5) & 0x03;
  1170. }
  1171. if (additional_copy_info_flag) {
  1172. additional_copy_info = *p++;
  1173. additional_copy_info &= 0x7f;
  1174. }
  1175. if (PES_CRC_flag) {
  1176. pp = (char*)&previous_PES_packet_CRC;
  1177. pp[1] = *p++;
  1178. pp[0] = *p++;
  1179. }
  1180. if (PES_extension_flag) {
  1181. PES_extension_flag_2 = *p++;
  1182. PES_private_data_flag = (PES_extension_flag_2 >> 7) & 0x01;
  1183. pack_header_field_flag = (PES_extension_flag_2 >> 6) & 0x01;
  1184. program_packet_sequence_counter_flag = (PES_extension_flag_2 >> 5) & 0x01;
  1185. P_STD_buffer_flag = (PES_extension_flag_2 >> 4) & 0x01;
  1186. PES_extension_flag_2 &= PES_extension_flag_2 & 0x01;
  1187. if (PES_private_data_flag) {
  1188. PES_private_data = new char[16];
  1189. memcpy(PES_private_data, p, 16);
  1190. p += 16;
  1191. }
  1192. if (pack_header_field_flag) {
  1193. pack_field_length = *p++;
  1194. if (pack_field_length > 0) {
  1195. pack_field = new char[pack_field_length];
  1196. memcpy(pack_field, p, pack_field_length);
  1197. p += pack_field_length;
  1198. }
  1199. }
  1200. if (program_packet_sequence_counter_flag) {
  1201. program_packet_sequence_counter = *p++;
  1202. program_packet_sequence_counter &= 0x7f;
  1203. original_stuff_length = *p++;
  1204. MPEG1_MPEG2_identifier = (original_stuff_length >> 6) & 0x01;
  1205. original_stuff_length &= 0x3f;
  1206. }
  1207. if (P_STD_buffer_flag) {
  1208. pp = (char*)&P_STD_buffer_size;
  1209. pp[1] = *p++;
  1210. pp[0] = *p++;
  1211. // '01'
  1212. //int8_t const2bits = (P_STD_buffer_scale >>14) & 0x03;
  1213. P_STD_buffer_scale = (P_STD_buffer_scale >>13) & 0x01;
  1214. P_STD_buffer_size &= 0x1FFF;
  1215. }
  1216. if (PES_extension_flag_2) {
  1217. PES_extension_field_length = *p++;
  1218. PES_extension_field_length &= 0x07;
  1219. if (PES_extension_field_length > 0) {
  1220. PES_extension_field = new char[PES_extension_field_length];
  1221. memcpy(PES_extension_field, p, PES_extension_field_length);
  1222. p += PES_extension_field_length;
  1223. }
  1224. }
  1225. }
  1226. // stuffing_byte
  1227. int stuffing_size = PES_header_data_length - (p - pos_header);
  1228. if (stuffing_size > 0) {
  1229. stuffing_byte = new char[stuffing_size];
  1230. memcpy(stuffing_byte, p, stuffing_size);
  1231. p += stuffing_size;
  1232. }
  1233. // get the pid.
  1234. TSPid* pid = ctx->get(pkt->header->pid);
  1235. if (!pid) {
  1236. trace("ts+pes pid: %d type is invalid.", pkt->header->pid);
  1237. }
  1238. // get the message to build from the chunks(PES packets).
  1239. TSMessage* msg = ctx->get_msg(pid->pid);
  1240. msg->type = pid->type;
  1241. msg->stream_type = pid->stream_type;
  1242. msg->continuity_counter = pid->continuity_counter;
  1243. msg->stream_id = stream_id;
  1244. msg->packet_start_code_prefix = packet_start_code_prefix;
  1245. msg->dts = dts;
  1246. msg->pts = pts;
  1247. // PES_packet_data_byte, page58.
  1248. // the packet size contains the header size.
  1249. // The number of PES_packet_data_bytes, N, is specified by the
  1250. // PES_packet_length field. N shall be equal to the value
  1251. // indicated in the PES_packet_length minus the number of bytes
  1252. // between the last byte of the PES_packet_length field and the
  1253. // first PES_packet_data_byte.
  1254. msg->PES_packet_length = PES_packet_length;
  1255. msg->packet_header_size = p - pos_packet;
  1256. msg->packet_data_size = PES_packet_length - msg->packet_header_size;
  1257. /**
  1258. * when actual packet length > 0xffff(65535),
  1259. * which exceed the max u_int16_t packet length,
  1260. * use 0 packet length, the next unit start indicates the end of packet.
  1261. */
  1262. if (PES_packet_length == 0) {
  1263. msg->packet_data_size = last - p - msg->packet_header_size;
  1264. }
  1265. if (msg->packet_data_size > 0) {
  1266. msg->packet_data = new char[msg->packet_data_size];
  1267. }
  1268. // PES_packet_data_byte
  1269. int size = srs_min(msg->packet_data_size, last - p);
  1270. if (size > 0) {
  1271. msg->append(p, size);
  1272. }
  1273. if (PES_packet_length > 0) {
  1274. msg->detach(ctx, pmsg);
  1275. }
  1276. trace("ts+pes stream_id: %d size: %d pts: %"PRId64" dts: %"PRId64" total: %d header: %d packet_size: %d parsed_size: %d",
  1277. stream_id, PES_packet_length, pts, dts, msg->PES_packet_length, msg->packet_header_size, msg->packet_data_size, msg->parsed_packet_size);
  1278. } else if (stream_id == PES_program_stream_map
  1279. || stream_id == PES_private_stream_2
  1280. || stream_id == PES_ECM_stream
  1281. || stream_id == PES_EMM_stream
  1282. || stream_id == PES_program_stream_directory
  1283. || stream_id == PES_DSMCC_stream
  1284. || stream_id == PES_H_222_1_type_E
  1285. ) {
  1286. // for (i = 0; i < PES_packet_length; i++) {
  1287. // PES_packet_data_byte
  1288. // }
  1289. // TODO: FIXME: implements it.
  1290. } else if (stream_id == PES_padding_stream) {
  1291. // for (i = 0; i < PES_packet_length; i++) {
  1292. // padding_byte
  1293. // }
  1294. }
  1295. return ret;
  1296. }
  1297. /**
  1298. * 2.4.3.6 PES packet. page 49.
  1299. */
  1300. TSPayload::TSPayload()
  1301. {
  1302. size = 0;
  1303. pointer_field_size = 0;
  1304. type = TSPidTypeReserved;
  1305. pointer_field = 0;
  1306. reserved = NULL;
  1307. pat = NULL;
  1308. pmt = NULL;
  1309. pes = NULL;
  1310. }
  1311. TSPayload::~TSPayload()
  1312. {
  1313. srs_freep(reserved);
  1314. srs_freep(pat);
  1315. srs_freep(pmt);
  1316. srs_freep(pes);
  1317. }
  1318. void TSPayload::read_pointer_field(TSPacket* pkt, u_int8_t*& p)
  1319. {
  1320. if (pkt->header->payload_unit_start_indicator) {
  1321. pointer_field = *p++;
  1322. pointer_field_size = 1;
  1323. }
  1324. }
  1325. int TSPayload::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  1326. {
  1327. int ret = 0;
  1328. if (pkt->header->pid == TSPidTablePAT) {
  1329. read_pointer_field(pkt, p);
  1330. type = TSPidTypePAT;
  1331. pat = new TSPayloadPAT();
  1332. return pat->demux(ctx, pkt, start, last, p, pmsg);
  1333. }
  1334. TSPid* pid = ctx->get(pkt->header->pid);
  1335. if (pid && pid->type == TSPidTypePMT) {
  1336. read_pointer_field(pkt, p);
  1337. type = pid->type;
  1338. pmt = new TSPayloadPMT();
  1339. return pmt->demux(ctx, pkt, start, last, p, pmsg);
  1340. }
  1341. if (pid && (pid->type == TSPidTypeVideo || pid->type == TSPidTypeAudio)) {
  1342. TSMessage* msg = ctx->get_msg(pkt->header->pid);
  1343. if (pkt->adaption_field->pcr > 0) {
  1344. msg->pcr = pkt->adaption_field->pcr;
  1345. }
  1346. // flush previous PES_packet_length(0) packets.
  1347. if (msg->packet_start_code_prefix == 0x01
  1348. && pkt->header->payload_unit_start_indicator == 1
  1349. && msg->PES_packet_length == 0
  1350. ) {
  1351. msg->detach(ctx, pmsg);
  1352. // reparse current message
  1353. p = start;
  1354. return ret;
  1355. }
  1356. // parse continous packet.
  1357. if (!pkt->header->payload_unit_start_indicator) {
  1358. if (msg->packet_start_code_prefix != 0x01) {
  1359. trace("ts+pes decode continous packet error, msg is empty.");
  1360. return -1;
  1361. }
  1362. msg->append(p, last - p);
  1363. // for PES_packet_length is 0, donot attach it.
  1364. if (msg->PES_packet_length > 0) {
  1365. msg->detach(ctx, pmsg);
  1366. }
  1367. return ret;
  1368. }
  1369. type = pid->type;
  1370. pes = new TSPayloadPES();
  1371. return pes->demux(ctx, pkt, start, last, p, pmsg);
  1372. }
  1373. // not parsed bytes.
  1374. type = TSPidTypeReserved;
  1375. reserved = new TSPayloadReserved();
  1376. if ((ret = reserved->demux(ctx, pkt, start, last, p, pmsg)) != 0) {
  1377. return ret;
  1378. }
  1379. return ret;
  1380. }
  1381. TSPacket::TSPacket()
  1382. {
  1383. header = new TSHeader();
  1384. adaption_field = new TSAdaptionField();
  1385. payload = new TSPayload();
  1386. }
  1387. TSPacket::~TSPacket()
  1388. {
  1389. srs_freep(header);
  1390. srs_freep(adaption_field);
  1391. srs_freep(payload);
  1392. }
  1393. int TSPacket::demux(TSContext* ctx, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  1394. {
  1395. int ret = 0;
  1396. if ((ret = header->demux(ctx, this, start, last, p, pmsg)) != 0) {
  1397. return ret;
  1398. }
  1399. if (header->adaption_field_control == TSAdaptionTypeAdaptionOnly || header->adaption_field_control == TSAdaptionTypeBoth) {
  1400. if ((ret = adaption_field->demux(ctx, this, start, last, p, pmsg)) != 0) {
  1401. trace("ts+header af(adaption field) decode error. ret=%d", ret);
  1402. return ret;
  1403. }
  1404. trace("ts+header af(adaption field) decoded.");
  1405. }
  1406. // calc the user defined data size for payload.
  1407. payload->size = TS_PACKET_SIZE - header->get_size() - adaption_field->get_size();
  1408. if (header->adaption_field_control == TSAdaptionTypePayloadOnly || header->adaption_field_control == TSAdaptionTypeBoth) {
  1409. // parse new packet.
  1410. if ((ret = payload->demux(ctx, this, start, last, p, pmsg)) != 0) {
  1411. trace("ts+header payload decode error. ret=%d", ret);
  1412. return ret;
  1413. }
  1414. trace("ts+header payload decoded.");
  1415. }
  1416. ctx->ts_packet_count++;
  1417. trace("ts+header parsed %"PRId64" packets finished. parsed: %d left: %d header: %d payload: %d(%d+%d)",
  1418. ctx->ts_packet_count, (int)(p - start), (int)(last - p), header->get_size(), payload->size, payload->pointer_field_size,
  1419. payload->size - payload->pointer_field_size);
  1420. return finish();
  1421. }
  1422. int TSPacket::finish()
  1423. {
  1424. return 0;
  1425. }
  1426. TSHeader::TSHeader()
  1427. {
  1428. sync_byte = 0;
  1429. transport_error_indicator = 0;
  1430. payload_unit_start_indicator = 0;
  1431. transport_priority = 0;
  1432. pid = TSPidTablePAT;
  1433. transport_scrambling_control = 0;
  1434. adaption_field_control = TSAdaptionTypeReserved;
  1435. continuity_counter = 0;
  1436. }
  1437. TSHeader::~TSHeader()
  1438. {
  1439. }
  1440. int TSHeader::get_size()
  1441. {
  1442. return 4;
  1443. }
  1444. int TSHeader::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
  1445. {
  1446. int ret = 0;
  1447. // ts packet header.
  1448. sync_byte = *p++;
  1449. if (sync_byte != 0x47) {
  1450. trace("ts+sync_bytes invalid sync_bytes: %#x, expect is 0x47", sync_byte);
  1451. return -1;
  1452. }
  1453. int16_t _pid = 0;
  1454. char* pp = (char*)&_pid;
  1455. pp[1] = *p++;
  1456. pp[0] = *p++;
  1457. transport_error_indicator = (_pid >> 15) & 0x01;
  1458. payload_unit_start_indicator = (_pid >> 14) & 0x01;
  1459. transport_priority = (_pid >> 13) & 0x01;
  1460. _pid &= 0x1FFF;
  1461. pid = (TSPidTable)_pid;
  1462. continuity_counter = *p++;
  1463. transport_scrambling_control = (continuity_counter >> 6) & 0x03;
  1464. int8_t _adaption_field_control = (continuity_counter >> 4) & 0x03;
  1465. adaption_field_control = (TSAdaptionType)_adaption_field_control;
  1466. continuity_counter &= 0x0F;
  1467. ctx->push(pid, TSStreamTypeReserved, TSPidTypePAT, continuity_counter);
  1468. trace("ts+header sync: %#x error: %d unit_start: %d priotiry: %d pid: %d scrambling: %d adaption: %d counter: %d",
  1469. sync_byte, transport_error_indicator, payload_unit_start_indicator, transport_priority, pid,
  1470. transport_scrambling_control, adaption_field_control, continuity_counter);
  1471. return ret;
  1472. }
  1473. /**
  1474. * Annex B Byte stream format, in page 211.
  1475. */
  1476. class TSH264Codec
  1477. {
  1478. public:
  1479. u_int8_t* raw_data;
  1480. int size;
  1481. TSH264Codec()
  1482. {
  1483. size = 0;
  1484. raw_data = NULL;
  1485. }
  1486. u_int8_t at(int index)
  1487. {
  1488. if (index >= size) {
  1489. return 0;
  1490. }
  1491. return raw_data[index];
  1492. }
  1493. int parse(TSMessage* msg, char* last, char*& p)
  1494. {
  1495. int ret = 0;
  1496. srs_assert(p);
  1497. while (next_start_code_prefix(p) != 0x000001) {
  1498. char ch = *p++;
  1499. if (ch != 0x00) {
  1500. trace("ts+h264 parse msg failed, "
  1501. "expect 0x00 before start-code. actual is: %#x", (u_int8_t)ch);
  1502. return -1;
  1503. }
  1504. }
  1505. if (p >= last) {
  1506. trace("ts+h264 parse msg finished, no start-code.");
  1507. return ret;
  1508. }
  1509. // start_code_prefix_one_3bytes /* equal to 0x000001 */
  1510. p += 3;
  1511. if (p < last) {
  1512. raw_data = (u_int8_t*)p;
  1513. }
  1514. while (p < last - 3) {
  1515. if (match_start_code_prefix(p)) {
  1516. break;
  1517. }
  1518. p++;
  1519. }
  1520. if (raw_data) {
  1521. size = (u_int8_t*)p - raw_data;
  1522. if (p == last - 3) {
  1523. size = (u_int8_t*)last - raw_data;
  1524. p = last;
  1525. }
  1526. }
  1527. trace("ts+h264 parse msg finished");
  1528. return ret;
  1529. }
  1530. bool match_start_code_prefix(char*p)
  1531. {
  1532. return p[0] == 0x00 && p[1] == 0x00 && (p[2] == 0x00 || p[2] == 0x01);
  1533. }
  1534. int32_t next_start_code_prefix(char* p)
  1535. {
  1536. int32_t value = 0;
  1537. char* pp = (char*)&value;
  1538. pp[2] = p[0];
  1539. pp[1] = p[1];
  1540. pp[0] = p[2];
  1541. return value;
  1542. }
  1543. };
  1544. /**
  1545. * Table 35 – Sampling frequency dependent on
  1546. * sampling_frequency_index. in page 46.
  1547. */
  1548. enum TSAacSampleFrequency
  1549. {
  1550. TSAacSampleFrequency96000 = 0x00,
  1551. TSAacSampleFrequency88200 = 0x01,
  1552. TSAacSampleFrequency64000 = 0x02,
  1553. TSAacSampleFrequency48000 = 0x03,
  1554. TSAacSampleFrequency44100 = 0x04,
  1555. TSAacSampleFrequency32000 = 0x05,
  1556. TSAacSampleFrequency24000 = 0x06,
  1557. TSAacSampleFrequency22050 = 0x07,
  1558. TSAacSampleFrequency16000 = 0x08,
  1559. TSAacSampleFrequency12000 = 0x09,
  1560. TSAacSampleFrequency11025 = 0x0a,
  1561. TSAacSampleFrequency8000 = 0x0b,
  1562. TSAacSampleFrequencyReserved0 = 0x0c,
  1563. TSAacSampleFrequencyReserved1 = 0x0d,
  1564. TSAacSampleFrequencyReserved2 = 0x0e,
  1565. TSAacSampleFrequencyReserved3 = 0x0f,
  1566. };
  1567. /**
  1568. * 6.2 Audio Data Transport Stream, ADTS, in page 26.
  1569. */
  1570. class TSAacAdts
  1571. {
  1572. public:
  1573. // adts_fixed_header
  1574. // 2B, 16bits
  1575. int16_t syncword; //12bits
  1576. int8_t ID; //1bit
  1577. int8_t layer; //2bits
  1578. int8_t protection_absent; //1bit
  1579. // 12bits
  1580. int8_t profile; //2bit
  1581. TSAacSampleFrequency sampling_frequency_index; //4bits
  1582. int8_t private_bit; //1bit
  1583. int8_t channel_configuration; //3bits
  1584. int8_t original_or_copy; //1bit
  1585. int8_t home; //1bit
  1586. // adts_variable_header
  1587. // 28bits
  1588. int8_t copyright_identification_bit; //1bit
  1589. int8_t copyright_identification_start; //1bit
  1590. int16_t frame_length; //13bits
  1591. int16_t adts_buffer_fullness; //11bits
  1592. int8_t number_of_raw_data_blocks_in_frame; //2bits
  1593. u_int8_t* raw_data;
  1594. int size;
  1595. TSAacAdts()
  1596. {
  1597. syncword = 0;
  1598. ID = 0;
  1599. layer = 0;
  1600. protection_absent = 0;
  1601. profile = 0;
  1602. sampling_frequency_index = TSAacSampleFrequencyReserved0;
  1603. private_bit = 0;
  1604. channel_configuration = 0;
  1605. original_or_copy = 0;
  1606. home = 0;
  1607. copyright_identification_bit = 0;
  1608. copyright_identification_start = 0;
  1609. frame_length = 0;
  1610. adts_buffer_fullness = 0;
  1611. number_of_raw_data_blocks_in_frame = 0;
  1612. size = 0;
  1613. raw_data = NULL;
  1614. }
  1615. u_int8_t at(int index)
  1616. {
  1617. if (index >= size) {
  1618. return 0;
  1619. }
  1620. return raw_data[index];
  1621. }
  1622. int parse(TSMessage* msg, char*& p)
  1623. {
  1624. int ret = 0;
  1625. srs_assert(p);
  1626. char* start = p;
  1627. // adts_fixed_header
  1628. char* pp = (char*)&syncword;
  1629. pp[1] = *p++;
  1630. pp[0] = *p++;
  1631. protection_absent = syncword & 0x01;
  1632. layer = (syncword >> 1) & 0x03;
  1633. ID = (syncword >> 3) & 0x01;
  1634. syncword = (syncword >> 4) & 0x0FFF;
  1635. if (syncword != 0xfff) {
  1636. trace("ts+aac invalid sync word. expect 0xfff, actual %#x", syncword);
  1637. return -1;
  1638. }
  1639. // adts_variable_header
  1640. int64_t temp = 0;
  1641. pp = (char*)&temp;
  1642. pp[4] = *p++;
  1643. pp[3] = *p++;
  1644. pp[2] = *p++;
  1645. pp[1] = *p++;
  1646. pp[0] = *p++;
  1647. number_of_raw_data_blocks_in_frame = temp & 0x03;
  1648. temp = temp >> 2;
  1649. adts_buffer_fullness = temp & 0x7FF;
  1650. temp = temp >> 11;
  1651. frame_length = temp & 0x1FFF;
  1652. temp = temp >> 13;
  1653. copyright_identification_start = temp & 0x01;
  1654. temp = temp >> 1;
  1655. copyright_identification_bit = temp & 0x01;
  1656. temp = temp >> 1;
  1657. // adts_fixed_header
  1658. home = temp & 0x01;
  1659. temp = temp >> 1;
  1660. original_or_copy = temp & 0x01;
  1661. temp = temp >> 1;
  1662. channel_configuration = temp & 0x07;
  1663. temp = temp >> 3;
  1664. private_bit = temp & 0x01;
  1665. temp = temp >> 1;
  1666. sampling_frequency_index = (TSAacSampleFrequency)(temp & 0x0F);
  1667. temp = temp >> 4;
  1668. profile = temp & 0x03;
  1669. temp = temp >> 2;
  1670. if (!number_of_raw_data_blocks_in_frame) {
  1671. // adts_error_check
  1672. if (!protection_absent) {
  1673. // crc_check
  1674. trace("ts+aac TODO: crc_check.");
  1675. }
  1676. // raw_data_block
  1677. raw_data = (u_int8_t*)p;
  1678. size = frame_length - (p - start);
  1679. p += size;
  1680. } else {
  1681. trace("ts+aac TODO: parse multiple blocks.");
  1682. }
  1683. return ret;
  1684. }
  1685. };
  1686. class AacMuxer
  1687. {
  1688. public:
  1689. int fd;
  1690. const char* file;
  1691. AacMuxer()
  1692. {
  1693. file = NULL;
  1694. fd = 0;
  1695. }
  1696. virtual ~AacMuxer()
  1697. {
  1698. if (fd > 0) {
  1699. close(fd);
  1700. }
  1701. }
  1702. int open(const char* _file)
  1703. {
  1704. file = _file;
  1705. if ((fd = ::open(file, O_CREAT|O_WRONLY|O_TRUNC,
  1706. S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH)) < 0
  1707. ) {
  1708. return -1;
  1709. }
  1710. return 0;
  1711. }
  1712. int write_audio(char* data, int size)
  1713. {
  1714. if (size > 0 && write(fd, data, size) != size) {
  1715. return -1;
  1716. }
  1717. return 0;
  1718. }
  1719. int write_video(char* data, int size)
  1720. {
  1721. return 0;
  1722. }
  1723. };
  1724. int consume(TSMessage* msg, AacMuxer* aac_muxer)
  1725. {
  1726. int ret = 0;
  1727. char* p = msg->packet_data;
  1728. if (!p) {
  1729. trace("ts+aac+h264 ignore empty message.");
  1730. return ret;
  1731. }
  1732. char* last = msg->packet_data + msg->packet_data_size;
  1733. if (!msg->is_video()) {
  1734. // write AAC raw audio.
  1735. if (aac_muxer && (ret = aac_muxer->write_audio((char*)msg->packet_data, msg->packet_data_size)) != 0) {
  1736. return ret;
  1737. }
  1738. // parse AAC audio.
  1739. int64_t dts = -1;
  1740. while (p < last) {
  1741. TSAacAdts aac;
  1742. if ((ret = aac.parse(msg, p)) != 0) {
  1743. return ret;
  1744. }
  1745. trace("ts+aac audio raw data parsed, size: %d, 0x%02x 0x%02x 0x%02x 0x%02x",
  1746. aac.size, aac.at(0), aac.at(1), aac.at(2), aac.at(3));
  1747. if (dts == -1) {
  1748. dts = (msg->dts == 0)? msg->pts : msg->dts;
  1749. } else {
  1750. // see ffmpeg: avpriv_aac_parse_header
  1751. // rdb = get_bits(gbc, 2); /* number_of_raw_data_blocks_in_frame */
  1752. // hdr->samples = (rdb + 1) * 1024;
  1753. int samples = (aac.number_of_raw_data_blocks_in_frame + 1) * 1024;
  1754. static int sample_rates[] = {
  1755. 96000, 88200, 64000, 48000, 44100, 32000,
  1756. 24000, 22050, 16000, 12000, 11025, 8000,
  1757. 1, 1, 1, 1
  1758. };
  1759. int sample_rate = sample_rates[aac.sampling_frequency_index];
  1760. dts += samples * 90000 / sample_rate;
  1761. }
  1762. trace("ts+aac+h264+data %s pts:%"PRId64" dts:%"PRId64" size: %d",
  1763. (msg->type == TSPidTypeVideo)? "video":"audio", dts, dts, aac.frame_length);
  1764. // TODO: process audio.
  1765. }
  1766. } else {
  1767. trace("ts+aac+h264+data %s pts:%"PRId64" dts:%"PRId64" size: %d",
  1768. (msg->type == TSPidTypeVideo)? "video":"audio", msg->pts,
  1769. (msg->dts == 0)? msg->pts : msg->dts, msg->packet_data_size);
  1770. // parse H264 video.
  1771. bool first = true;
  1772. while (p < last) {
  1773. TSH264Codec h264;
  1774. if ((ret = h264.parse(msg, last, p)) != 0) {
  1775. return ret;
  1776. }
  1777. trace("ts+h264 video raw data parsed, size: %d, 0x%02x 0x%02x 0x%02x 0x%02x",
  1778. h264.size, h264.at(0), h264.at(1), h264.at(2), h264.at(3));
  1779. // first?
  1780. if (!first) {
  1781. continue;
  1782. }
  1783. first = false;
  1784. // TODO: process video.
  1785. // directly check the sequence header for test_22m.flv
  1786. if (h264.at(0) == 0x67 && h264.at(1) == 0x00 && h264.at(2) == 0x1f && h264.at(3) == 0xac) {
  1787. trace("ts+h264 directly find the sequence header for test_22m.flv");
  1788. }
  1789. // 7.3.1 NAL unit syntax, hls-mpeg-ts-iso13818-1.pdf, page 44
  1790. char* pp = (char*)h264.raw_data;
  1791. int8_t nal_unit_type = *pp++;
  1792. int8_t nal_ref_idc = (nal_unit_type >> 5) & 0x03;
  1793. nal_unit_type &= 0x1f;
  1794. msg->nal_ref_idc = nal_ref_idc;
  1795. msg->nal_unit_type = nal_unit_type;
  1796. if (nal_ref_idc != 0) {
  1797. trace("ts+h264 got an SPS or PPS.");
  1798. }
  1799. if (nal_unit_type == 7) {
  1800. trace("ts+h264 got an SPS.");
  1801. } else if (nal_unit_type == 5) {
  1802. trace("ts+h264 got an Coded slice of an IDR picture.");
  1803. } else if (nal_unit_type == 8) {
  1804. trace("ts+h264 got an PPS.");
  1805. } else if (nal_unit_type == 9) {
  1806. trace("ts+h264 got an Picture delimiter.");
  1807. int8_t pic_type = *pp++;
  1808. pic_type = (pic_type >> 6) & 0x07;
  1809. if (pic_type == 0) {
  1810. trace("ts+h264 got an I picture.");
  1811. } else if (pic_type == 1) {
  1812. trace("ts+h264 got an I,P picture.");
  1813. } else if (pic_type == 2) {
  1814. trace("ts+h264 got an I,P,B picture.");
  1815. } else if (pic_type == 3) {
  1816. trace("ts+h264 got an SI picture.");
  1817. } else if (pic_type == 4) {
  1818. trace("ts+h264 got an SI,SP picture.");
  1819. } else if (pic_type == 5) {
  1820. trace("ts+h264 got an I,SI picture.");
  1821. } else if (pic_type == 6) {
  1822. trace("ts+h264 got an I,SI,P,SP picture.");
  1823. } else if (pic_type == 7) {
  1824. trace("ts+h264 got an I,SI,P,SP,B picture.");
  1825. }
  1826. } else {
  1827. trace("ts+h264 got an unknown unit type: %d.", nal_unit_type);
  1828. }
  1829. }
  1830. }
  1831. return ret;
  1832. }
  1833. int main(int argc, char** argv)
  1834. {
  1835. const char* file = "livestream-1347.ts";
  1836. const char* output_aac_file = "output.aac";
  1837. if (argc > 2) {
  1838. file = argv[1];
  1839. output_aac_file = argv[2];
  1840. }
  1841. int fd = open(file, O_RDONLY);
  1842. AacMuxer aac_muxer;
  1843. int ret = 0;
  1844. if ((ret = aac_muxer.open(output_aac_file)) != 0) {
  1845. trace("aac_muxer+open open flv file failed.");
  1846. return ret;
  1847. }
  1848. trace("demuxer+read packet count offset T+0 T+1 T+2 T+3 T+x T+L2 T+L1 T+L0");
  1849. TSContext ctx;
  1850. for (int i = 0, offset = 0; ; i++) {
  1851. u_int8_t ts_packet[TS_PACKET_SIZE];
  1852. memset(ts_packet, 0, sizeof(ts_packet));
  1853. int nread = read(fd, ts_packet, sizeof(ts_packet));
  1854. if (nread == 0) {
  1855. trace("demuxer+read got EOF, read completed, offset: %07d.", offset);
  1856. break;
  1857. }
  1858. if (nread != TS_PACKET_SIZE) {
  1859. trace("demuxer+read error to read ts packet. nread=%d", nread);
  1860. break;
  1861. }
  1862. trace("demuxer+read packet %04d %07d 0x%02x 0x%02x 0x%02x 0x%02x ... 0x%02x 0x%02x 0x%02x",
  1863. i, offset, ts_packet[0], ts_packet[1], ts_packet[2], ts_packet[3],
  1864. ts_packet[TS_PACKET_SIZE - 3], ts_packet[TS_PACKET_SIZE - 2], ts_packet[TS_PACKET_SIZE - 1]);
  1865. u_int8_t* p = ts_packet;
  1866. u_int8_t* start = ts_packet;
  1867. u_int8_t* last = ts_packet + TS_PACKET_SIZE;
  1868. // maybe need to parse multiple times for the PES_packet_length(0) packets.
  1869. while (p == start) {
  1870. TSPacket pkt;
  1871. TSMessage* msg = NULL;
  1872. if ((ret = pkt.demux(&ctx, start, last, p, msg)) != 0) {
  1873. trace("demuxer+read decode ts packet error. ret=%d", ret);
  1874. return ret;
  1875. }
  1876. offset += nread;
  1877. if (!msg) {
  1878. continue;
  1879. }
  1880. if ((ret = consume(msg, &aac_muxer)) != 0) {
  1881. trace("demuxer+consume parse and consume message failed. ret=%d", ret);
  1882. return -1;
  1883. }
  1884. int64_t pts = msg->pts;
  1885. int64_t dts = (msg->dts == 0)? msg->pts : msg->dts;
  1886. int64_t pcr = msg->pcr;
  1887. static int64_t last_pcr_dts = 0;
  1888. trace("demuxer+report id=%d, type=%s, size=%d, dts=%d, pts=%d, cts=%d, pcr=%d, dts-pcr=%d, ref=%d, unit=%d, dts(diff-pcr)=%d",
  1889. (int)ctx.ts_packet_count, (msg->type == TSPidTypeVideo)? "video":"audio",
  1890. (int)msg->parsed_packet_size, (int)dts, (int)pts, (int)(pts - dts), (int)pcr, (int)(pcr? dts - pcr : 0),
  1891. (int)msg->nal_ref_idc, (int)msg->nal_unit_type, (int)(pcr? dts - last_pcr_dts: 0));
  1892. if (pcr > 0) {
  1893. last_pcr_dts = dts;
  1894. }
  1895. srs_freep(msg);
  1896. }
  1897. }
  1898. close(fd);
  1899. return ret;
  1900. }