2
0

ts_info.cc 62 KB

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