mpegts.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. /*
  2. * MPEG2 transport stream (aka DVB) demuxer
  3. * Copyright (c) 2002-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/crc.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "mpegts.h"
  25. #include "internal.h"
  26. //#define DEBUG_SI
  27. //#define DEBUG_SEEK
  28. /* 1.0 second at 24Mbit/s */
  29. #define MAX_SCAN_PACKETS 32000
  30. /* maximum size in which we look for synchronisation if
  31. synchronisation is lost */
  32. #define MAX_RESYNC_SIZE 4096
  33. #define REGISTRATION_DESCRIPTOR 5
  34. typedef struct PESContext PESContext;
  35. static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type);
  36. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
  37. enum MpegTSFilterType {
  38. MPEGTS_PES,
  39. MPEGTS_SECTION,
  40. };
  41. typedef struct MpegTSFilter MpegTSFilter;
  42. typedef void PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
  43. typedef struct MpegTSPESFilter {
  44. PESCallback *pes_cb;
  45. void *opaque;
  46. } MpegTSPESFilter;
  47. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  48. typedef void SetServiceCallback(void *opaque, int ret);
  49. typedef struct MpegTSSectionFilter {
  50. int section_index;
  51. int section_h_size;
  52. uint8_t *section_buf;
  53. unsigned int check_crc:1;
  54. unsigned int end_of_section_reached:1;
  55. SectionCallback *section_cb;
  56. void *opaque;
  57. } MpegTSSectionFilter;
  58. struct MpegTSFilter {
  59. int pid;
  60. int last_cc; /* last cc code (-1 if first packet) */
  61. enum MpegTSFilterType type;
  62. union {
  63. MpegTSPESFilter pes_filter;
  64. MpegTSSectionFilter section_filter;
  65. } u;
  66. };
  67. #define MAX_PIDS_PER_PROGRAM 64
  68. struct Program {
  69. unsigned int id; //program id/service id
  70. unsigned int nb_pids;
  71. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  72. };
  73. struct MpegTSContext {
  74. /* user data */
  75. AVFormatContext *stream;
  76. /** raw packet size, including FEC if present */
  77. int raw_packet_size;
  78. int pos47;
  79. /** if true, all pids are analyzed to find streams */
  80. int auto_guess;
  81. /** compute exact PCR for each transport stream packet */
  82. int mpeg2ts_compute_pcr;
  83. int64_t cur_pcr; /**< used to estimate the exact PCR */
  84. int pcr_incr; /**< used to estimate the exact PCR */
  85. /* data needed to handle file based ts */
  86. /** stop parsing loop */
  87. int stop_parse;
  88. /** packet containing Audio/Video data */
  89. AVPacket *pkt;
  90. /******************************************/
  91. /* private mpegts data */
  92. /* scan context */
  93. /** structure to keep track of Program->pids mapping */
  94. unsigned int nb_prg;
  95. struct Program *prg;
  96. /** filters for various streams specified by PMT + for the PAT and PMT */
  97. MpegTSFilter *pids[NB_PID_MAX];
  98. };
  99. /* TS stream handling */
  100. enum MpegTSState {
  101. MPEGTS_HEADER = 0,
  102. MPEGTS_PESHEADER_FILL,
  103. MPEGTS_PAYLOAD,
  104. MPEGTS_SKIP,
  105. };
  106. /* enough for PES header + length */
  107. #define PES_START_SIZE 9
  108. #define MAX_PES_HEADER_SIZE (9 + 255)
  109. struct PESContext {
  110. int pid;
  111. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  112. int stream_type;
  113. MpegTSContext *ts;
  114. AVFormatContext *stream;
  115. AVStream *st;
  116. enum MpegTSState state;
  117. /* used to get the format */
  118. int data_index;
  119. int total_size;
  120. int pes_header_size;
  121. int64_t pts, dts;
  122. int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
  123. uint8_t header[MAX_PES_HEADER_SIZE];
  124. };
  125. extern AVInputFormat mpegts_demuxer;
  126. static void clear_program(MpegTSContext *ts, unsigned int programid)
  127. {
  128. int i;
  129. for(i=0; i<ts->nb_prg; i++)
  130. if(ts->prg[i].id == programid)
  131. ts->prg[i].nb_pids = 0;
  132. }
  133. static void clear_programs(MpegTSContext *ts)
  134. {
  135. av_freep(&ts->prg);
  136. ts->nb_prg=0;
  137. }
  138. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  139. {
  140. struct Program *p;
  141. void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
  142. if(!tmp)
  143. return;
  144. ts->prg = tmp;
  145. p = &ts->prg[ts->nb_prg];
  146. p->id = programid;
  147. p->nb_pids = 0;
  148. ts->nb_prg++;
  149. }
  150. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  151. {
  152. int i;
  153. struct Program *p = NULL;
  154. for(i=0; i<ts->nb_prg; i++) {
  155. if(ts->prg[i].id == programid) {
  156. p = &ts->prg[i];
  157. break;
  158. }
  159. }
  160. if(!p)
  161. return;
  162. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  163. return;
  164. p->pids[p->nb_pids++] = pid;
  165. }
  166. /**
  167. * \brief discard_pid() decides if the pid is to be discarded according
  168. * to caller's programs selection
  169. * \param ts : - TS context
  170. * \param pid : - pid
  171. * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  172. * 0 otherwise
  173. */
  174. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  175. {
  176. int i, j, k;
  177. int used = 0, discarded = 0;
  178. struct Program *p;
  179. for(i=0; i<ts->nb_prg; i++) {
  180. p = &ts->prg[i];
  181. for(j=0; j<p->nb_pids; j++) {
  182. if(p->pids[j] != pid)
  183. continue;
  184. //is program with id p->id set to be discarded?
  185. for(k=0; k<ts->stream->nb_programs; k++) {
  186. if(ts->stream->programs[k]->id == p->id) {
  187. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  188. discarded++;
  189. else
  190. used++;
  191. }
  192. }
  193. }
  194. }
  195. return !used && discarded;
  196. }
  197. /**
  198. * Assembles PES packets out of TS packets, and then calls the "section_cb"
  199. * function when they are complete.
  200. */
  201. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  202. const uint8_t *buf, int buf_size, int is_start)
  203. {
  204. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  205. int len;
  206. if (is_start) {
  207. memcpy(tss->section_buf, buf, buf_size);
  208. tss->section_index = buf_size;
  209. tss->section_h_size = -1;
  210. tss->end_of_section_reached = 0;
  211. } else {
  212. if (tss->end_of_section_reached)
  213. return;
  214. len = 4096 - tss->section_index;
  215. if (buf_size < len)
  216. len = buf_size;
  217. memcpy(tss->section_buf + tss->section_index, buf, len);
  218. tss->section_index += len;
  219. }
  220. /* compute section length if possible */
  221. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  222. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  223. if (len > 4096)
  224. return;
  225. tss->section_h_size = len;
  226. }
  227. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  228. tss->end_of_section_reached = 1;
  229. if (!tss->check_crc ||
  230. av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
  231. tss->section_buf, tss->section_h_size) == 0)
  232. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  233. }
  234. }
  235. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  236. SectionCallback *section_cb, void *opaque,
  237. int check_crc)
  238. {
  239. MpegTSFilter *filter;
  240. MpegTSSectionFilter *sec;
  241. #ifdef DEBUG_SI
  242. av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
  243. #endif
  244. if (pid >= NB_PID_MAX || ts->pids[pid])
  245. return NULL;
  246. filter = av_mallocz(sizeof(MpegTSFilter));
  247. if (!filter)
  248. return NULL;
  249. ts->pids[pid] = filter;
  250. filter->type = MPEGTS_SECTION;
  251. filter->pid = pid;
  252. filter->last_cc = -1;
  253. sec = &filter->u.section_filter;
  254. sec->section_cb = section_cb;
  255. sec->opaque = opaque;
  256. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  257. sec->check_crc = check_crc;
  258. if (!sec->section_buf) {
  259. av_free(filter);
  260. return NULL;
  261. }
  262. return filter;
  263. }
  264. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  265. PESCallback *pes_cb,
  266. void *opaque)
  267. {
  268. MpegTSFilter *filter;
  269. MpegTSPESFilter *pes;
  270. if (pid >= NB_PID_MAX || ts->pids[pid])
  271. return NULL;
  272. filter = av_mallocz(sizeof(MpegTSFilter));
  273. if (!filter)
  274. return NULL;
  275. ts->pids[pid] = filter;
  276. filter->type = MPEGTS_PES;
  277. filter->pid = pid;
  278. filter->last_cc = -1;
  279. pes = &filter->u.pes_filter;
  280. pes->pes_cb = pes_cb;
  281. pes->opaque = opaque;
  282. return filter;
  283. }
  284. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  285. {
  286. int pid;
  287. pid = filter->pid;
  288. if (filter->type == MPEGTS_SECTION)
  289. av_freep(&filter->u.section_filter.section_buf);
  290. else if (filter->type == MPEGTS_PES) {
  291. /* referenced private data will be freed later in
  292. * av_close_input_stream */
  293. if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
  294. av_freep(&filter->u.pes_filter.opaque);
  295. }
  296. }
  297. av_free(filter);
  298. ts->pids[pid] = NULL;
  299. }
  300. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  301. int stat[packet_size];
  302. int i;
  303. int x=0;
  304. int best_score=0;
  305. memset(stat, 0, packet_size*sizeof(int));
  306. for(x=i=0; i<size-3; i++){
  307. if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
  308. stat[x]++;
  309. if(stat[x] > best_score){
  310. best_score= stat[x];
  311. if(index) *index= x;
  312. }
  313. }
  314. x++;
  315. if(x == packet_size) x= 0;
  316. }
  317. return best_score;
  318. }
  319. /* autodetect fec presence. Must have at least 1024 bytes */
  320. static int get_packet_size(const uint8_t *buf, int size)
  321. {
  322. int score, fec_score, dvhs_score;
  323. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  324. return -1;
  325. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  326. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  327. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  328. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  329. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  330. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  331. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  332. else return -1;
  333. }
  334. typedef struct SectionHeader {
  335. uint8_t tid;
  336. uint16_t id;
  337. uint8_t version;
  338. uint8_t sec_num;
  339. uint8_t last_sec_num;
  340. } SectionHeader;
  341. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  342. {
  343. const uint8_t *p;
  344. int c;
  345. p = *pp;
  346. if (p >= p_end)
  347. return -1;
  348. c = *p++;
  349. *pp = p;
  350. return c;
  351. }
  352. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  353. {
  354. const uint8_t *p;
  355. int c;
  356. p = *pp;
  357. if ((p + 1) >= p_end)
  358. return -1;
  359. c = AV_RB16(p);
  360. p += 2;
  361. *pp = p;
  362. return c;
  363. }
  364. /* read and allocate a DVB string preceeded by its length */
  365. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  366. {
  367. int len;
  368. const uint8_t *p;
  369. char *str;
  370. p = *pp;
  371. len = get8(&p, p_end);
  372. if (len < 0)
  373. return NULL;
  374. if ((p + len) > p_end)
  375. return NULL;
  376. str = av_malloc(len + 1);
  377. if (!str)
  378. return NULL;
  379. memcpy(str, p, len);
  380. str[len] = '\0';
  381. p += len;
  382. *pp = p;
  383. return str;
  384. }
  385. static int parse_section_header(SectionHeader *h,
  386. const uint8_t **pp, const uint8_t *p_end)
  387. {
  388. int val;
  389. val = get8(pp, p_end);
  390. if (val < 0)
  391. return -1;
  392. h->tid = val;
  393. *pp += 2;
  394. val = get16(pp, p_end);
  395. if (val < 0)
  396. return -1;
  397. h->id = val;
  398. val = get8(pp, p_end);
  399. if (val < 0)
  400. return -1;
  401. h->version = (val >> 1) & 0x1f;
  402. val = get8(pp, p_end);
  403. if (val < 0)
  404. return -1;
  405. h->sec_num = val;
  406. val = get8(pp, p_end);
  407. if (val < 0)
  408. return -1;
  409. h->last_sec_num = val;
  410. return 0;
  411. }
  412. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  413. {
  414. MpegTSContext *ts = filter->u.section_filter.opaque;
  415. SectionHeader h1, *h = &h1;
  416. PESContext *pes;
  417. AVStream *st;
  418. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  419. int program_info_length, pcr_pid, pid, stream_type;
  420. int desc_list_len, desc_len, desc_tag;
  421. int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
  422. char language[4] = {0}; /* initialize to kill warnings */
  423. int has_hdmv_descr = 0;
  424. int has_dirac_descr = 0;
  425. #ifdef DEBUG_SI
  426. av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
  427. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  428. #endif
  429. p_end = section + section_len - 4;
  430. p = section;
  431. if (parse_section_header(h, &p, p_end) < 0)
  432. return;
  433. #ifdef DEBUG_SI
  434. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
  435. h->id, h->sec_num, h->last_sec_num);
  436. #endif
  437. if (h->tid != PMT_TID)
  438. return;
  439. clear_program(ts, h->id);
  440. pcr_pid = get16(&p, p_end) & 0x1fff;
  441. if (pcr_pid < 0)
  442. return;
  443. add_pid_to_pmt(ts, h->id, pcr_pid);
  444. #ifdef DEBUG_SI
  445. av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
  446. #endif
  447. program_info_length = get16(&p, p_end) & 0xfff;
  448. if (program_info_length < 0)
  449. return;
  450. while(program_info_length >= 2) {
  451. uint8_t tag, len;
  452. tag = get8(&p, p_end);
  453. len = get8(&p, p_end);
  454. if(len > program_info_length - 2)
  455. //something else is broken, exit the program_descriptors_loop
  456. break;
  457. program_info_length -= len + 2;
  458. if(tag == REGISTRATION_DESCRIPTOR && len >= 4) {
  459. uint8_t bytes[4];
  460. bytes[0] = get8(&p, p_end);
  461. bytes[1] = get8(&p, p_end);
  462. bytes[2] = get8(&p, p_end);
  463. bytes[3] = get8(&p, p_end);
  464. len -= 4;
  465. if(bytes[0] == 'H' && bytes[1] == 'D' &&
  466. bytes[2] == 'M' && bytes[3] == 'V')
  467. has_hdmv_descr = 1;
  468. }
  469. p += len;
  470. }
  471. p += program_info_length;
  472. if (p >= p_end)
  473. return;
  474. for(;;) {
  475. language[0] = 0;
  476. st = 0;
  477. stream_type = get8(&p, p_end);
  478. if (stream_type < 0)
  479. break;
  480. pid = get16(&p, p_end) & 0x1fff;
  481. if (pid < 0)
  482. break;
  483. desc_list_len = get16(&p, p_end) & 0xfff;
  484. if (desc_list_len < 0)
  485. break;
  486. desc_list_end = p + desc_list_len;
  487. if (desc_list_end > p_end)
  488. break;
  489. for(;;) {
  490. desc_tag = get8(&p, desc_list_end);
  491. if (desc_tag < 0)
  492. break;
  493. if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
  494. if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
  495. /*assume DVB AC-3 Audio*/
  496. stream_type = STREAM_TYPE_AUDIO_AC3;
  497. } else if(desc_tag == 0x7B) {
  498. /* DVB DTS audio */
  499. stream_type = STREAM_TYPE_AUDIO_DTS;
  500. }
  501. }
  502. desc_len = get8(&p, desc_list_end);
  503. desc_end = p + desc_len;
  504. if (desc_end > desc_list_end)
  505. break;
  506. #ifdef DEBUG_SI
  507. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  508. desc_tag, desc_len);
  509. #endif
  510. switch(desc_tag) {
  511. case DVB_SUBT_DESCID:
  512. if (stream_type == STREAM_TYPE_PRIVATE_DATA)
  513. stream_type = STREAM_TYPE_SUBTITLE_DVB;
  514. language[0] = get8(&p, desc_end);
  515. language[1] = get8(&p, desc_end);
  516. language[2] = get8(&p, desc_end);
  517. language[3] = 0;
  518. get8(&p, desc_end);
  519. comp_page = get16(&p, desc_end);
  520. anc_page = get16(&p, desc_end);
  521. break;
  522. case 0x0a: /* ISO 639 language descriptor */
  523. language[0] = get8(&p, desc_end);
  524. language[1] = get8(&p, desc_end);
  525. language[2] = get8(&p, desc_end);
  526. language[3] = 0;
  527. break;
  528. case REGISTRATION_DESCRIPTOR: /*MPEG-2 Registration descriptor */
  529. {
  530. uint8_t bytes[4];
  531. bytes[0] = get8(&p, desc_end);
  532. bytes[1] = get8(&p, desc_end);
  533. bytes[2] = get8(&p, desc_end);
  534. bytes[3] = get8(&p, desc_end);
  535. if(bytes[0] == 'd' && bytes[1] == 'r' &&
  536. bytes[2] == 'a' && bytes[3] == 'c')
  537. has_dirac_descr = 1;
  538. break;
  539. }
  540. default:
  541. break;
  542. }
  543. p = desc_end;
  544. }
  545. p = desc_list_end;
  546. #ifdef DEBUG_SI
  547. av_log(ts->stream, AV_LOG_DEBUG, "stream_type=%d pid=0x%x\n",
  548. stream_type, pid);
  549. #endif
  550. /* now create ffmpeg stream */
  551. switch(stream_type) {
  552. case STREAM_TYPE_AUDIO_MPEG1:
  553. case STREAM_TYPE_AUDIO_MPEG2:
  554. case STREAM_TYPE_VIDEO_MPEG1:
  555. case STREAM_TYPE_VIDEO_MPEG2:
  556. case STREAM_TYPE_VIDEO_MPEG4:
  557. case STREAM_TYPE_VIDEO_H264:
  558. case STREAM_TYPE_VIDEO_VC1:
  559. case STREAM_TYPE_VIDEO_DIRAC:
  560. case STREAM_TYPE_AUDIO_AAC:
  561. case STREAM_TYPE_AUDIO_AC3:
  562. case STREAM_TYPE_AUDIO_DTS:
  563. case STREAM_TYPE_AUDIO_HDMV_DTS:
  564. case STREAM_TYPE_SUBTITLE_DVB:
  565. if((stream_type == STREAM_TYPE_AUDIO_HDMV_DTS && !has_hdmv_descr)
  566. || (stream_type == STREAM_TYPE_VIDEO_DIRAC && !has_dirac_descr))
  567. break;
  568. if(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES){
  569. pes= ts->pids[pid]->u.pes_filter.opaque;
  570. st= pes->st;
  571. }else{
  572. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  573. pes = add_pes_stream(ts, pid, pcr_pid, stream_type);
  574. if (pes)
  575. st = new_pes_av_stream(pes, 0);
  576. }
  577. add_pid_to_pmt(ts, h->id, pid);
  578. if(st)
  579. av_program_add_stream_index(ts->stream, h->id, st->index);
  580. break;
  581. default:
  582. /* we ignore the other streams */
  583. break;
  584. }
  585. if (st) {
  586. if (language[0] != 0) {
  587. av_metadata_set(&st->metadata, "language", language);
  588. }
  589. if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
  590. st->codec->sub_id = (anc_page << 16) | comp_page;
  591. }
  592. }
  593. }
  594. /* all parameters are there */
  595. ts->stop_parse++;
  596. mpegts_close_filter(ts, filter);
  597. }
  598. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  599. {
  600. MpegTSContext *ts = filter->u.section_filter.opaque;
  601. SectionHeader h1, *h = &h1;
  602. const uint8_t *p, *p_end;
  603. int sid, pmt_pid;
  604. #ifdef DEBUG_SI
  605. av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
  606. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  607. #endif
  608. p_end = section + section_len - 4;
  609. p = section;
  610. if (parse_section_header(h, &p, p_end) < 0)
  611. return;
  612. if (h->tid != PAT_TID)
  613. return;
  614. clear_programs(ts);
  615. for(;;) {
  616. sid = get16(&p, p_end);
  617. if (sid < 0)
  618. break;
  619. pmt_pid = get16(&p, p_end) & 0x1fff;
  620. if (pmt_pid < 0)
  621. break;
  622. #ifdef DEBUG_SI
  623. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  624. #endif
  625. if (sid == 0x0000) {
  626. /* NIT info */
  627. } else {
  628. av_new_program(ts->stream, sid);
  629. ts->stop_parse--;
  630. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  631. add_pat_entry(ts, sid);
  632. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  633. add_pid_to_pmt(ts, sid, pmt_pid);
  634. }
  635. }
  636. /* not found */
  637. ts->stop_parse++;
  638. mpegts_close_filter(ts, filter);
  639. }
  640. static void mpegts_set_service(MpegTSContext *ts)
  641. {
  642. mpegts_open_section_filter(ts, PAT_PID,
  643. pat_cb, ts, 1);
  644. }
  645. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  646. {
  647. MpegTSContext *ts = filter->u.section_filter.opaque;
  648. SectionHeader h1, *h = &h1;
  649. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  650. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  651. char *name, *provider_name;
  652. #ifdef DEBUG_SI
  653. av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
  654. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  655. #endif
  656. p_end = section + section_len - 4;
  657. p = section;
  658. if (parse_section_header(h, &p, p_end) < 0)
  659. return;
  660. if (h->tid != SDT_TID)
  661. return;
  662. onid = get16(&p, p_end);
  663. if (onid < 0)
  664. return;
  665. val = get8(&p, p_end);
  666. if (val < 0)
  667. return;
  668. for(;;) {
  669. sid = get16(&p, p_end);
  670. if (sid < 0)
  671. break;
  672. val = get8(&p, p_end);
  673. if (val < 0)
  674. break;
  675. desc_list_len = get16(&p, p_end) & 0xfff;
  676. if (desc_list_len < 0)
  677. break;
  678. desc_list_end = p + desc_list_len;
  679. if (desc_list_end > p_end)
  680. break;
  681. for(;;) {
  682. desc_tag = get8(&p, desc_list_end);
  683. if (desc_tag < 0)
  684. break;
  685. desc_len = get8(&p, desc_list_end);
  686. desc_end = p + desc_len;
  687. if (desc_len < 0 || desc_end > desc_list_end)
  688. break;
  689. #ifdef DEBUG_SI
  690. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  691. desc_tag, desc_len);
  692. #endif
  693. switch(desc_tag) {
  694. case 0x48:
  695. service_type = get8(&p, p_end);
  696. if (service_type < 0)
  697. break;
  698. provider_name = getstr8(&p, p_end);
  699. if (!provider_name)
  700. break;
  701. name = getstr8(&p, p_end);
  702. if (name) {
  703. AVProgram *program = av_new_program(ts->stream, sid);
  704. if(program) {
  705. av_metadata_set(&program->metadata, "name", name);
  706. av_metadata_set(&program->metadata, "provider_name", provider_name);
  707. }
  708. }
  709. av_free(name);
  710. av_free(provider_name);
  711. break;
  712. default:
  713. break;
  714. }
  715. p = desc_end;
  716. }
  717. p = desc_list_end;
  718. }
  719. }
  720. /* scan services in a transport stream by looking at the SDT */
  721. static void mpegts_scan_sdt(MpegTSContext *ts)
  722. {
  723. mpegts_open_section_filter(ts, SDT_PID,
  724. sdt_cb, ts, 1);
  725. }
  726. static int64_t get_pts(const uint8_t *p)
  727. {
  728. int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  729. pts |= (AV_RB16(p + 1) >> 1) << 15;
  730. pts |= AV_RB16(p + 3) >> 1;
  731. return pts;
  732. }
  733. /* return non zero if a packet could be constructed */
  734. static void mpegts_push_data(MpegTSFilter *filter,
  735. const uint8_t *buf, int buf_size, int is_start,
  736. int64_t pos)
  737. {
  738. PESContext *pes = filter->u.pes_filter.opaque;
  739. MpegTSContext *ts = pes->ts;
  740. const uint8_t *p;
  741. int len, code;
  742. if(!ts->pkt)
  743. return;
  744. if (is_start) {
  745. pes->state = MPEGTS_HEADER;
  746. pes->data_index = 0;
  747. pes->ts_packet_pos = pos;
  748. }
  749. p = buf;
  750. while (buf_size > 0) {
  751. switch(pes->state) {
  752. case MPEGTS_HEADER:
  753. len = PES_START_SIZE - pes->data_index;
  754. if (len > buf_size)
  755. len = buf_size;
  756. memcpy(pes->header + pes->data_index, p, len);
  757. pes->data_index += len;
  758. p += len;
  759. buf_size -= len;
  760. if (pes->data_index == PES_START_SIZE) {
  761. /* we got all the PES or section header. We can now
  762. decide */
  763. #if 0
  764. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  765. #endif
  766. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  767. pes->header[2] == 0x01) {
  768. /* it must be an mpeg2 PES stream */
  769. code = pes->header[3] | 0x100;
  770. if (!((code >= 0x1c0 && code <= 0x1df) ||
  771. (code >= 0x1e0 && code <= 0x1ef) ||
  772. (code == 0x1bd) || (code == 0x1fd)))
  773. goto skip;
  774. if (!pes->st) {
  775. /* allocate stream */
  776. new_pes_av_stream(pes, code);
  777. }
  778. pes->state = MPEGTS_PESHEADER_FILL;
  779. pes->total_size = AV_RB16(pes->header + 4);
  780. /* NOTE: a zero total size means the PES size is
  781. unbounded */
  782. if (pes->total_size)
  783. pes->total_size += 6;
  784. pes->pes_header_size = pes->header[8] + 9;
  785. } else {
  786. /* otherwise, it should be a table */
  787. /* skip packet */
  788. skip:
  789. pes->state = MPEGTS_SKIP;
  790. continue;
  791. }
  792. }
  793. break;
  794. /**********************************************/
  795. /* PES packing parsing */
  796. case MPEGTS_PESHEADER_FILL:
  797. len = pes->pes_header_size - pes->data_index;
  798. if (len > buf_size)
  799. len = buf_size;
  800. memcpy(pes->header + pes->data_index, p, len);
  801. pes->data_index += len;
  802. p += len;
  803. buf_size -= len;
  804. if (pes->data_index == pes->pes_header_size) {
  805. const uint8_t *r;
  806. unsigned int flags;
  807. flags = pes->header[7];
  808. r = pes->header + 9;
  809. pes->pts = AV_NOPTS_VALUE;
  810. pes->dts = AV_NOPTS_VALUE;
  811. if ((flags & 0xc0) == 0x80) {
  812. pes->dts = pes->pts = get_pts(r);
  813. r += 5;
  814. } else if ((flags & 0xc0) == 0xc0) {
  815. pes->pts = get_pts(r);
  816. r += 5;
  817. pes->dts = get_pts(r);
  818. r += 5;
  819. }
  820. /* we got the full header. We parse it and get the payload */
  821. pes->state = MPEGTS_PAYLOAD;
  822. }
  823. break;
  824. case MPEGTS_PAYLOAD:
  825. if (pes->total_size) {
  826. len = pes->total_size - pes->data_index;
  827. if (len > buf_size)
  828. len = buf_size;
  829. } else {
  830. len = buf_size;
  831. }
  832. if (len > 0) {
  833. AVPacket *pkt = ts->pkt;
  834. if (pes->st && av_new_packet(pkt, len) == 0) {
  835. memcpy(pkt->data, p, len);
  836. pkt->stream_index = pes->st->index;
  837. pkt->pts = pes->pts;
  838. pkt->dts = pes->dts;
  839. /* store position of first TS packet of this PES packet */
  840. pkt->pos = pes->ts_packet_pos;
  841. /* reset pts values */
  842. pes->pts = AV_NOPTS_VALUE;
  843. pes->dts = AV_NOPTS_VALUE;
  844. ts->stop_parse = 1;
  845. return;
  846. }
  847. }
  848. buf_size = 0;
  849. break;
  850. case MPEGTS_SKIP:
  851. buf_size = 0;
  852. break;
  853. }
  854. }
  855. }
  856. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
  857. {
  858. AVStream *st;
  859. enum CodecID codec_id;
  860. enum CodecType codec_type;
  861. switch(pes->stream_type){
  862. case STREAM_TYPE_AUDIO_MPEG1:
  863. case STREAM_TYPE_AUDIO_MPEG2:
  864. codec_type = CODEC_TYPE_AUDIO;
  865. codec_id = CODEC_ID_MP3;
  866. break;
  867. case STREAM_TYPE_VIDEO_MPEG1:
  868. case STREAM_TYPE_VIDEO_MPEG2:
  869. codec_type = CODEC_TYPE_VIDEO;
  870. codec_id = CODEC_ID_MPEG2VIDEO;
  871. break;
  872. case STREAM_TYPE_VIDEO_MPEG4:
  873. codec_type = CODEC_TYPE_VIDEO;
  874. codec_id = CODEC_ID_MPEG4;
  875. break;
  876. case STREAM_TYPE_VIDEO_H264:
  877. codec_type = CODEC_TYPE_VIDEO;
  878. codec_id = CODEC_ID_H264;
  879. break;
  880. case STREAM_TYPE_VIDEO_VC1:
  881. codec_type = CODEC_TYPE_VIDEO;
  882. codec_id = CODEC_ID_VC1;
  883. break;
  884. case STREAM_TYPE_VIDEO_DIRAC:
  885. codec_type = CODEC_TYPE_VIDEO;
  886. codec_id = CODEC_ID_DIRAC;
  887. break;
  888. case STREAM_TYPE_AUDIO_AAC:
  889. codec_type = CODEC_TYPE_AUDIO;
  890. codec_id = CODEC_ID_AAC;
  891. break;
  892. case STREAM_TYPE_AUDIO_AC3:
  893. codec_type = CODEC_TYPE_AUDIO;
  894. codec_id = CODEC_ID_AC3;
  895. break;
  896. case STREAM_TYPE_AUDIO_DTS:
  897. case STREAM_TYPE_AUDIO_HDMV_DTS:
  898. codec_type = CODEC_TYPE_AUDIO;
  899. codec_id = CODEC_ID_DTS;
  900. break;
  901. case STREAM_TYPE_SUBTITLE_DVB:
  902. codec_type = CODEC_TYPE_SUBTITLE;
  903. codec_id = CODEC_ID_DVB_SUBTITLE;
  904. break;
  905. default:
  906. if (code >= 0x1c0 && code <= 0x1df) {
  907. codec_type = CODEC_TYPE_AUDIO;
  908. codec_id = CODEC_ID_MP2;
  909. } else if (code == 0x1bd) {
  910. codec_type = CODEC_TYPE_AUDIO;
  911. codec_id = CODEC_ID_AC3;
  912. } else {
  913. codec_type = CODEC_TYPE_VIDEO;
  914. codec_id = CODEC_ID_PROBE;
  915. }
  916. break;
  917. }
  918. st = av_new_stream(pes->stream, pes->pid);
  919. if (st) {
  920. av_set_pts_info(st, 33, 1, 90000);
  921. st->priv_data = pes;
  922. st->codec->codec_type = codec_type;
  923. st->codec->codec_id = codec_id;
  924. st->need_parsing = AVSTREAM_PARSE_FULL;
  925. pes->st = st;
  926. }
  927. return st;
  928. }
  929. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
  930. {
  931. MpegTSFilter *tss;
  932. PESContext *pes;
  933. /* if no pid found, then add a pid context */
  934. pes = av_mallocz(sizeof(PESContext));
  935. if (!pes)
  936. return 0;
  937. pes->ts = ts;
  938. pes->stream = ts->stream;
  939. pes->pid = pid;
  940. pes->pcr_pid = pcr_pid;
  941. pes->stream_type = stream_type;
  942. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  943. if (!tss) {
  944. av_free(pes);
  945. return 0;
  946. }
  947. return pes;
  948. }
  949. /* handle one TS packet */
  950. static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
  951. {
  952. AVFormatContext *s = ts->stream;
  953. MpegTSFilter *tss;
  954. int len, pid, cc, cc_ok, afc, is_start;
  955. const uint8_t *p, *p_end;
  956. int64_t pos;
  957. pid = AV_RB16(packet + 1) & 0x1fff;
  958. if(pid && discard_pid(ts, pid))
  959. return;
  960. is_start = packet[1] & 0x40;
  961. tss = ts->pids[pid];
  962. if (ts->auto_guess && tss == NULL && is_start) {
  963. add_pes_stream(ts, pid, -1, 0);
  964. tss = ts->pids[pid];
  965. }
  966. if (!tss)
  967. return;
  968. /* continuity check (currently not used) */
  969. cc = (packet[3] & 0xf);
  970. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  971. tss->last_cc = cc;
  972. /* skip adaptation field */
  973. afc = (packet[3] >> 4) & 3;
  974. p = packet + 4;
  975. if (afc == 0) /* reserved value */
  976. return;
  977. if (afc == 2) /* adaptation field only */
  978. return;
  979. if (afc == 3) {
  980. /* skip adapation field */
  981. p += p[0] + 1;
  982. }
  983. /* if past the end of packet, ignore */
  984. p_end = packet + TS_PACKET_SIZE;
  985. if (p >= p_end)
  986. return;
  987. pos = url_ftell(ts->stream->pb);
  988. ts->pos47= pos % ts->raw_packet_size;
  989. if (tss->type == MPEGTS_SECTION) {
  990. if (is_start) {
  991. /* pointer field present */
  992. len = *p++;
  993. if (p + len > p_end)
  994. return;
  995. if (len && cc_ok) {
  996. /* write remaining section bytes */
  997. write_section_data(s, tss,
  998. p, len, 0);
  999. /* check whether filter has been closed */
  1000. if (!ts->pids[pid])
  1001. return;
  1002. }
  1003. p += len;
  1004. if (p < p_end) {
  1005. write_section_data(s, tss,
  1006. p, p_end - p, 1);
  1007. }
  1008. } else {
  1009. if (cc_ok) {
  1010. write_section_data(s, tss,
  1011. p, p_end - p, 0);
  1012. }
  1013. }
  1014. } else {
  1015. // Note: The position here points actually behind the current packet.
  1016. tss->u.pes_filter.pes_cb(tss,
  1017. p, p_end - p, is_start, pos - ts->raw_packet_size);
  1018. }
  1019. }
  1020. /* XXX: try to find a better synchro over several packets (use
  1021. get_packet_size() ?) */
  1022. static int mpegts_resync(ByteIOContext *pb)
  1023. {
  1024. int c, i;
  1025. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1026. c = url_fgetc(pb);
  1027. if (c < 0)
  1028. return -1;
  1029. if (c == 0x47) {
  1030. url_fseek(pb, -1, SEEK_CUR);
  1031. return 0;
  1032. }
  1033. }
  1034. /* no sync found */
  1035. return -1;
  1036. }
  1037. /* return -1 if error or EOF. Return 0 if OK. */
  1038. static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
  1039. {
  1040. int skip, len;
  1041. for(;;) {
  1042. len = get_buffer(pb, buf, TS_PACKET_SIZE);
  1043. if (len != TS_PACKET_SIZE)
  1044. return AVERROR(EIO);
  1045. /* check paquet sync byte */
  1046. if (buf[0] != 0x47) {
  1047. /* find a new packet start */
  1048. url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1049. if (mpegts_resync(pb) < 0)
  1050. return AVERROR_INVALIDDATA;
  1051. else
  1052. continue;
  1053. } else {
  1054. skip = raw_packet_size - TS_PACKET_SIZE;
  1055. if (skip > 0)
  1056. url_fskip(pb, skip);
  1057. break;
  1058. }
  1059. }
  1060. return 0;
  1061. }
  1062. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1063. {
  1064. AVFormatContext *s = ts->stream;
  1065. ByteIOContext *pb = s->pb;
  1066. uint8_t packet[TS_PACKET_SIZE];
  1067. int packet_num, ret;
  1068. ts->stop_parse = 0;
  1069. packet_num = 0;
  1070. for(;;) {
  1071. if (ts->stop_parse>0)
  1072. break;
  1073. packet_num++;
  1074. if (nb_packets != 0 && packet_num >= nb_packets)
  1075. break;
  1076. ret = read_packet(pb, packet, ts->raw_packet_size);
  1077. if (ret != 0)
  1078. return ret;
  1079. handle_packet(ts, packet);
  1080. }
  1081. return 0;
  1082. }
  1083. static int mpegts_probe(AVProbeData *p)
  1084. {
  1085. #if 1
  1086. const int size= p->buf_size;
  1087. int score, fec_score, dvhs_score;
  1088. int check_count= size / TS_FEC_PACKET_SIZE;
  1089. #define CHECK_COUNT 10
  1090. if (check_count < CHECK_COUNT)
  1091. return -1;
  1092. score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1093. dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
  1094. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1095. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1096. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1097. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1098. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1099. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1100. else return -1;
  1101. #else
  1102. /* only use the extension for safer guess */
  1103. if (match_ext(p->filename, "ts"))
  1104. return AVPROBE_SCORE_MAX;
  1105. else
  1106. return 0;
  1107. #endif
  1108. }
  1109. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1110. (-1) if not available */
  1111. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1112. const uint8_t *packet)
  1113. {
  1114. int afc, len, flags;
  1115. const uint8_t *p;
  1116. unsigned int v;
  1117. afc = (packet[3] >> 4) & 3;
  1118. if (afc <= 1)
  1119. return -1;
  1120. p = packet + 4;
  1121. len = p[0];
  1122. p++;
  1123. if (len == 0)
  1124. return -1;
  1125. flags = *p++;
  1126. len--;
  1127. if (!(flags & 0x10))
  1128. return -1;
  1129. if (len < 6)
  1130. return -1;
  1131. v = AV_RB32(p);
  1132. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1133. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1134. return 0;
  1135. }
  1136. static int mpegts_read_header(AVFormatContext *s,
  1137. AVFormatParameters *ap)
  1138. {
  1139. MpegTSContext *ts = s->priv_data;
  1140. ByteIOContext *pb = s->pb;
  1141. uint8_t buf[5*1024];
  1142. int len;
  1143. int64_t pos;
  1144. if (ap) {
  1145. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1146. if(ap->mpeg2ts_raw){
  1147. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1148. return -1;
  1149. }
  1150. }
  1151. /* read the first 1024 bytes to get packet size */
  1152. pos = url_ftell(pb);
  1153. len = get_buffer(pb, buf, sizeof(buf));
  1154. if (len != sizeof(buf))
  1155. goto fail;
  1156. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1157. if (ts->raw_packet_size <= 0)
  1158. goto fail;
  1159. ts->stream = s;
  1160. ts->auto_guess = 0;
  1161. if (s->iformat == &mpegts_demuxer) {
  1162. /* normal demux */
  1163. /* first do a scaning to get all the services */
  1164. url_fseek(pb, pos, SEEK_SET);
  1165. mpegts_scan_sdt(ts);
  1166. mpegts_set_service(ts);
  1167. handle_packets(ts, s->probesize);
  1168. /* if could not find service, enable auto_guess */
  1169. ts->auto_guess = 1;
  1170. #ifdef DEBUG_SI
  1171. av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
  1172. #endif
  1173. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1174. } else {
  1175. AVStream *st;
  1176. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1177. int64_t pcrs[2], pcr_h;
  1178. int packet_count[2];
  1179. uint8_t packet[TS_PACKET_SIZE];
  1180. /* only read packets */
  1181. st = av_new_stream(s, 0);
  1182. if (!st)
  1183. goto fail;
  1184. av_set_pts_info(st, 60, 1, 27000000);
  1185. st->codec->codec_type = CODEC_TYPE_DATA;
  1186. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1187. /* we iterate until we find two PCRs to estimate the bitrate */
  1188. pcr_pid = -1;
  1189. nb_pcrs = 0;
  1190. nb_packets = 0;
  1191. for(;;) {
  1192. ret = read_packet(s->pb, packet, ts->raw_packet_size);
  1193. if (ret < 0)
  1194. return -1;
  1195. pid = AV_RB16(packet + 1) & 0x1fff;
  1196. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1197. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1198. pcr_pid = pid;
  1199. packet_count[nb_pcrs] = nb_packets;
  1200. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1201. nb_pcrs++;
  1202. if (nb_pcrs >= 2)
  1203. break;
  1204. }
  1205. nb_packets++;
  1206. }
  1207. /* NOTE1: the bitrate is computed without the FEC */
  1208. /* NOTE2: it is only the bitrate of the start of the stream */
  1209. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1210. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1211. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1212. st->codec->bit_rate = s->bit_rate;
  1213. st->start_time = ts->cur_pcr;
  1214. #if 0
  1215. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1216. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1217. #endif
  1218. }
  1219. url_fseek(pb, pos, SEEK_SET);
  1220. return 0;
  1221. fail:
  1222. return -1;
  1223. }
  1224. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1225. static int mpegts_raw_read_packet(AVFormatContext *s,
  1226. AVPacket *pkt)
  1227. {
  1228. MpegTSContext *ts = s->priv_data;
  1229. int ret, i;
  1230. int64_t pcr_h, next_pcr_h, pos;
  1231. int pcr_l, next_pcr_l;
  1232. uint8_t pcr_buf[12];
  1233. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1234. return AVERROR(ENOMEM);
  1235. pkt->pos= url_ftell(s->pb);
  1236. ret = read_packet(s->pb, pkt->data, ts->raw_packet_size);
  1237. if (ret < 0) {
  1238. av_free_packet(pkt);
  1239. return ret;
  1240. }
  1241. if (ts->mpeg2ts_compute_pcr) {
  1242. /* compute exact PCR for each packet */
  1243. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1244. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1245. pos = url_ftell(s->pb);
  1246. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1247. url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1248. get_buffer(s->pb, pcr_buf, 12);
  1249. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1250. /* XXX: not precise enough */
  1251. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1252. (i + 1);
  1253. break;
  1254. }
  1255. }
  1256. url_fseek(s->pb, pos, SEEK_SET);
  1257. /* no next PCR found: we use previous increment */
  1258. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1259. }
  1260. pkt->pts = ts->cur_pcr;
  1261. pkt->duration = ts->pcr_incr;
  1262. ts->cur_pcr += ts->pcr_incr;
  1263. }
  1264. pkt->stream_index = 0;
  1265. return 0;
  1266. }
  1267. static int mpegts_read_packet(AVFormatContext *s,
  1268. AVPacket *pkt)
  1269. {
  1270. MpegTSContext *ts = s->priv_data;
  1271. ts->pkt = pkt;
  1272. return handle_packets(ts, 0);
  1273. }
  1274. static int mpegts_read_close(AVFormatContext *s)
  1275. {
  1276. MpegTSContext *ts = s->priv_data;
  1277. int i;
  1278. clear_programs(ts);
  1279. for(i=0;i<NB_PID_MAX;i++)
  1280. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1281. return 0;
  1282. }
  1283. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1284. int64_t *ppos, int64_t pos_limit)
  1285. {
  1286. MpegTSContext *ts = s->priv_data;
  1287. int64_t pos, timestamp;
  1288. uint8_t buf[TS_PACKET_SIZE];
  1289. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1290. const int find_next= 1;
  1291. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1292. if (find_next) {
  1293. for(;;) {
  1294. url_fseek(s->pb, pos, SEEK_SET);
  1295. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1296. return AV_NOPTS_VALUE;
  1297. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1298. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1299. break;
  1300. }
  1301. pos += ts->raw_packet_size;
  1302. }
  1303. } else {
  1304. for(;;) {
  1305. pos -= ts->raw_packet_size;
  1306. if (pos < 0)
  1307. return AV_NOPTS_VALUE;
  1308. url_fseek(s->pb, pos, SEEK_SET);
  1309. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1310. return AV_NOPTS_VALUE;
  1311. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1312. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1313. break;
  1314. }
  1315. }
  1316. }
  1317. *ppos = pos;
  1318. return timestamp;
  1319. }
  1320. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1321. MpegTSContext *ts = s->priv_data;
  1322. uint8_t buf[TS_PACKET_SIZE];
  1323. int64_t pos;
  1324. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1325. return -1;
  1326. pos= url_ftell(s->pb);
  1327. for(;;) {
  1328. url_fseek(s->pb, pos, SEEK_SET);
  1329. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1330. return -1;
  1331. // pid = AV_RB16(buf + 1) & 0x1fff;
  1332. if(buf[1] & 0x40) break;
  1333. pos += ts->raw_packet_size;
  1334. }
  1335. url_fseek(s->pb, pos, SEEK_SET);
  1336. return 0;
  1337. }
  1338. /**************************************************************/
  1339. /* parsing functions - called from other demuxers such as RTP */
  1340. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1341. {
  1342. MpegTSContext *ts;
  1343. ts = av_mallocz(sizeof(MpegTSContext));
  1344. if (!ts)
  1345. return NULL;
  1346. /* no stream case, currently used by RTP */
  1347. ts->raw_packet_size = TS_PACKET_SIZE;
  1348. ts->stream = s;
  1349. ts->auto_guess = 1;
  1350. return ts;
  1351. }
  1352. /* return the consumed length if a packet was output, or -1 if no
  1353. packet is output */
  1354. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1355. const uint8_t *buf, int len)
  1356. {
  1357. int len1;
  1358. len1 = len;
  1359. ts->pkt = pkt;
  1360. ts->stop_parse = 0;
  1361. for(;;) {
  1362. if (ts->stop_parse>0)
  1363. break;
  1364. if (len < TS_PACKET_SIZE)
  1365. return -1;
  1366. if (buf[0] != 0x47) {
  1367. buf++;
  1368. len--;
  1369. } else {
  1370. handle_packet(ts, buf);
  1371. buf += TS_PACKET_SIZE;
  1372. len -= TS_PACKET_SIZE;
  1373. }
  1374. }
  1375. return len1 - len;
  1376. }
  1377. void mpegts_parse_close(MpegTSContext *ts)
  1378. {
  1379. int i;
  1380. for(i=0;i<NB_PID_MAX;i++)
  1381. av_free(ts->pids[i]);
  1382. av_free(ts);
  1383. }
  1384. AVInputFormat mpegts_demuxer = {
  1385. "mpegts",
  1386. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  1387. sizeof(MpegTSContext),
  1388. mpegts_probe,
  1389. mpegts_read_header,
  1390. mpegts_read_packet,
  1391. mpegts_read_close,
  1392. read_seek,
  1393. mpegts_get_pcr,
  1394. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1395. };
  1396. AVInputFormat mpegtsraw_demuxer = {
  1397. "mpegtsraw",
  1398. NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
  1399. sizeof(MpegTSContext),
  1400. NULL,
  1401. mpegts_read_header,
  1402. mpegts_raw_read_packet,
  1403. mpegts_read_close,
  1404. read_seek,
  1405. mpegts_get_pcr,
  1406. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1407. };