mpegts.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  1. /*
  2. * MPEG2 transport stream (aka DVB) demuxer
  3. * Copyright (c) 2002-2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. //#define DEBUG
  22. //#define DEBUG_SEEK
  23. //#define USE_SYNCPOINT_SEARCH
  24. #include "libavutil/crc.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavcodec/bytestream.h"
  27. #include "avformat.h"
  28. #include "mpegts.h"
  29. #include "internal.h"
  30. #include "avio_internal.h"
  31. #include "seek.h"
  32. #include "mpeg.h"
  33. #include "isom.h"
  34. /* maximum size in which we look for synchronisation if
  35. synchronisation is lost */
  36. #define MAX_RESYNC_SIZE 65536
  37. #define MAX_PES_PAYLOAD 200*1024
  38. enum MpegTSFilterType {
  39. MPEGTS_PES,
  40. MPEGTS_SECTION,
  41. };
  42. typedef struct MpegTSFilter MpegTSFilter;
  43. typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
  44. typedef struct MpegTSPESFilter {
  45. PESCallback *pes_cb;
  46. void *opaque;
  47. } MpegTSPESFilter;
  48. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  49. typedef void SetServiceCallback(void *opaque, int ret);
  50. typedef struct MpegTSSectionFilter {
  51. int section_index;
  52. int section_h_size;
  53. uint8_t *section_buf;
  54. unsigned int check_crc:1;
  55. unsigned int end_of_section_reached:1;
  56. SectionCallback *section_cb;
  57. void *opaque;
  58. } MpegTSSectionFilter;
  59. struct MpegTSFilter {
  60. int pid;
  61. int last_cc; /* last cc code (-1 if first packet) */
  62. enum MpegTSFilterType type;
  63. union {
  64. MpegTSPESFilter pes_filter;
  65. MpegTSSectionFilter section_filter;
  66. } u;
  67. };
  68. #define MAX_PIDS_PER_PROGRAM 64
  69. struct Program {
  70. unsigned int id; //program id/service id
  71. unsigned int nb_pids;
  72. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  73. };
  74. struct MpegTSContext {
  75. /* user data */
  76. AVFormatContext *stream;
  77. /** raw packet size, including FEC if present */
  78. int raw_packet_size;
  79. int pos47;
  80. /** if true, all pids are analyzed to find streams */
  81. int auto_guess;
  82. /** compute exact PCR for each transport stream packet */
  83. int mpeg2ts_compute_pcr;
  84. int64_t cur_pcr; /**< used to estimate the exact PCR */
  85. int pcr_incr; /**< used to estimate the exact PCR */
  86. /* data needed to handle file based ts */
  87. /** stop parsing loop */
  88. int stop_parse;
  89. /** packet containing Audio/Video data */
  90. AVPacket *pkt;
  91. /** to detect seek */
  92. int64_t last_pos;
  93. /******************************************/
  94. /* private mpegts data */
  95. /* scan context */
  96. /** structure to keep track of Program->pids mapping */
  97. unsigned int nb_prg;
  98. struct Program *prg;
  99. /** filters for various streams specified by PMT + for the PAT and PMT */
  100. MpegTSFilter *pids[NB_PID_MAX];
  101. };
  102. /* TS stream handling */
  103. enum MpegTSState {
  104. MPEGTS_HEADER = 0,
  105. MPEGTS_PESHEADER,
  106. MPEGTS_PESHEADER_FILL,
  107. MPEGTS_PAYLOAD,
  108. MPEGTS_SKIP,
  109. };
  110. /* enough for PES header + length */
  111. #define PES_START_SIZE 6
  112. #define PES_HEADER_SIZE 9
  113. #define MAX_PES_HEADER_SIZE (9 + 255)
  114. typedef struct PESContext {
  115. int pid;
  116. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  117. int stream_type;
  118. MpegTSContext *ts;
  119. AVFormatContext *stream;
  120. AVStream *st;
  121. AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
  122. enum MpegTSState state;
  123. /* used to get the format */
  124. int data_index;
  125. int total_size;
  126. int pes_header_size;
  127. int extended_stream_id;
  128. int64_t pts, dts;
  129. int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
  130. uint8_t header[MAX_PES_HEADER_SIZE];
  131. uint8_t *buffer;
  132. } PESContext;
  133. extern AVInputFormat ff_mpegts_demuxer;
  134. static void clear_program(MpegTSContext *ts, unsigned int programid)
  135. {
  136. int i;
  137. for(i=0; i<ts->nb_prg; i++)
  138. if(ts->prg[i].id == programid)
  139. ts->prg[i].nb_pids = 0;
  140. }
  141. static void clear_programs(MpegTSContext *ts)
  142. {
  143. av_freep(&ts->prg);
  144. ts->nb_prg=0;
  145. }
  146. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  147. {
  148. struct Program *p;
  149. void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
  150. if(!tmp)
  151. return;
  152. ts->prg = tmp;
  153. p = &ts->prg[ts->nb_prg];
  154. p->id = programid;
  155. p->nb_pids = 0;
  156. ts->nb_prg++;
  157. }
  158. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  159. {
  160. int i;
  161. struct Program *p = NULL;
  162. for(i=0; i<ts->nb_prg; i++) {
  163. if(ts->prg[i].id == programid) {
  164. p = &ts->prg[i];
  165. break;
  166. }
  167. }
  168. if(!p)
  169. return;
  170. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  171. return;
  172. p->pids[p->nb_pids++] = pid;
  173. }
  174. /**
  175. * \brief discard_pid() decides if the pid is to be discarded according
  176. * to caller's programs selection
  177. * \param ts : - TS context
  178. * \param pid : - pid
  179. * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  180. * 0 otherwise
  181. */
  182. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  183. {
  184. int i, j, k;
  185. int used = 0, discarded = 0;
  186. struct Program *p;
  187. for(i=0; i<ts->nb_prg; i++) {
  188. p = &ts->prg[i];
  189. for(j=0; j<p->nb_pids; j++) {
  190. if(p->pids[j] != pid)
  191. continue;
  192. //is program with id p->id set to be discarded?
  193. for(k=0; k<ts->stream->nb_programs; k++) {
  194. if(ts->stream->programs[k]->id == p->id) {
  195. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  196. discarded++;
  197. else
  198. used++;
  199. }
  200. }
  201. }
  202. }
  203. return !used && discarded;
  204. }
  205. /**
  206. * Assemble PES packets out of TS packets, and then call the "section_cb"
  207. * function when they are complete.
  208. */
  209. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  210. const uint8_t *buf, int buf_size, int is_start)
  211. {
  212. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  213. int len;
  214. if (is_start) {
  215. memcpy(tss->section_buf, buf, buf_size);
  216. tss->section_index = buf_size;
  217. tss->section_h_size = -1;
  218. tss->end_of_section_reached = 0;
  219. } else {
  220. if (tss->end_of_section_reached)
  221. return;
  222. len = 4096 - tss->section_index;
  223. if (buf_size < len)
  224. len = buf_size;
  225. memcpy(tss->section_buf + tss->section_index, buf, len);
  226. tss->section_index += len;
  227. }
  228. /* compute section length if possible */
  229. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  230. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  231. if (len > 4096)
  232. return;
  233. tss->section_h_size = len;
  234. }
  235. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  236. tss->end_of_section_reached = 1;
  237. if (!tss->check_crc ||
  238. av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
  239. tss->section_buf, tss->section_h_size) == 0)
  240. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  241. }
  242. }
  243. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  244. SectionCallback *section_cb, void *opaque,
  245. int check_crc)
  246. {
  247. MpegTSFilter *filter;
  248. MpegTSSectionFilter *sec;
  249. av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
  250. if (pid >= NB_PID_MAX || ts->pids[pid])
  251. return NULL;
  252. filter = av_mallocz(sizeof(MpegTSFilter));
  253. if (!filter)
  254. return NULL;
  255. ts->pids[pid] = filter;
  256. filter->type = MPEGTS_SECTION;
  257. filter->pid = pid;
  258. filter->last_cc = -1;
  259. sec = &filter->u.section_filter;
  260. sec->section_cb = section_cb;
  261. sec->opaque = opaque;
  262. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  263. sec->check_crc = check_crc;
  264. if (!sec->section_buf) {
  265. av_free(filter);
  266. return NULL;
  267. }
  268. return filter;
  269. }
  270. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  271. PESCallback *pes_cb,
  272. void *opaque)
  273. {
  274. MpegTSFilter *filter;
  275. MpegTSPESFilter *pes;
  276. if (pid >= NB_PID_MAX || ts->pids[pid])
  277. return NULL;
  278. filter = av_mallocz(sizeof(MpegTSFilter));
  279. if (!filter)
  280. return NULL;
  281. ts->pids[pid] = filter;
  282. filter->type = MPEGTS_PES;
  283. filter->pid = pid;
  284. filter->last_cc = -1;
  285. pes = &filter->u.pes_filter;
  286. pes->pes_cb = pes_cb;
  287. pes->opaque = opaque;
  288. return filter;
  289. }
  290. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  291. {
  292. int pid;
  293. pid = filter->pid;
  294. if (filter->type == MPEGTS_SECTION)
  295. av_freep(&filter->u.section_filter.section_buf);
  296. else if (filter->type == MPEGTS_PES) {
  297. PESContext *pes = filter->u.pes_filter.opaque;
  298. av_freep(&pes->buffer);
  299. /* referenced private data will be freed later in
  300. * av_close_input_stream */
  301. if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
  302. av_freep(&filter->u.pes_filter.opaque);
  303. }
  304. }
  305. av_free(filter);
  306. ts->pids[pid] = NULL;
  307. }
  308. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  309. int stat[TS_MAX_PACKET_SIZE];
  310. int i;
  311. int x=0;
  312. int best_score=0;
  313. memset(stat, 0, packet_size*sizeof(int));
  314. for(x=i=0; i<size-3; i++){
  315. if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
  316. stat[x]++;
  317. if(stat[x] > best_score){
  318. best_score= stat[x];
  319. if(index) *index= x;
  320. }
  321. }
  322. x++;
  323. if(x == packet_size) x= 0;
  324. }
  325. return best_score;
  326. }
  327. /* autodetect fec presence. Must have at least 1024 bytes */
  328. static int get_packet_size(const uint8_t *buf, int size)
  329. {
  330. int score, fec_score, dvhs_score;
  331. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  332. return -1;
  333. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  334. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  335. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  336. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  337. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  338. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  339. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  340. else return -1;
  341. }
  342. typedef struct SectionHeader {
  343. uint8_t tid;
  344. uint16_t id;
  345. uint8_t version;
  346. uint8_t sec_num;
  347. uint8_t last_sec_num;
  348. } SectionHeader;
  349. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  350. {
  351. const uint8_t *p;
  352. int c;
  353. p = *pp;
  354. if (p >= p_end)
  355. return -1;
  356. c = *p++;
  357. *pp = p;
  358. return c;
  359. }
  360. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  361. {
  362. const uint8_t *p;
  363. int c;
  364. p = *pp;
  365. if ((p + 1) >= p_end)
  366. return -1;
  367. c = AV_RB16(p);
  368. p += 2;
  369. *pp = p;
  370. return c;
  371. }
  372. /* read and allocate a DVB string preceeded by its length */
  373. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  374. {
  375. int len;
  376. const uint8_t *p;
  377. char *str;
  378. p = *pp;
  379. len = get8(&p, p_end);
  380. if (len < 0)
  381. return NULL;
  382. if ((p + len) > p_end)
  383. return NULL;
  384. str = av_malloc(len + 1);
  385. if (!str)
  386. return NULL;
  387. memcpy(str, p, len);
  388. str[len] = '\0';
  389. p += len;
  390. *pp = p;
  391. return str;
  392. }
  393. static int parse_section_header(SectionHeader *h,
  394. const uint8_t **pp, const uint8_t *p_end)
  395. {
  396. int val;
  397. val = get8(pp, p_end);
  398. if (val < 0)
  399. return -1;
  400. h->tid = val;
  401. *pp += 2;
  402. val = get16(pp, p_end);
  403. if (val < 0)
  404. return -1;
  405. h->id = val;
  406. val = get8(pp, p_end);
  407. if (val < 0)
  408. return -1;
  409. h->version = (val >> 1) & 0x1f;
  410. val = get8(pp, p_end);
  411. if (val < 0)
  412. return -1;
  413. h->sec_num = val;
  414. val = get8(pp, p_end);
  415. if (val < 0)
  416. return -1;
  417. h->last_sec_num = val;
  418. return 0;
  419. }
  420. typedef struct {
  421. uint32_t stream_type;
  422. enum AVMediaType codec_type;
  423. enum CodecID codec_id;
  424. } StreamType;
  425. static const StreamType ISO_types[] = {
  426. { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
  427. { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
  428. { 0x03, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
  429. { 0x04, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
  430. { 0x0f, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC },
  431. { 0x10, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG4 },
  432. { 0x11, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC_LATM }, /* LATM syntax */
  433. { 0x1b, AVMEDIA_TYPE_VIDEO, CODEC_ID_H264 },
  434. { 0xd1, AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
  435. { 0xea, AVMEDIA_TYPE_VIDEO, CODEC_ID_VC1 },
  436. { 0 },
  437. };
  438. static const StreamType HDMV_types[] = {
  439. { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
  440. { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  441. { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  442. { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
  443. { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
  444. { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
  445. { 0 },
  446. };
  447. /* ATSC ? */
  448. static const StreamType MISC_types[] = {
  449. { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  450. { 0x8a, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  451. { 0 },
  452. };
  453. static const StreamType REGD_types[] = {
  454. { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
  455. { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  456. { 0 },
  457. };
  458. /* descriptor present */
  459. static const StreamType DESC_types[] = {
  460. { 0x6a, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
  461. { 0x7a, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  462. { 0x7b, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  463. { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
  464. { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  465. { 0 },
  466. };
  467. static void mpegts_find_stream_type(AVStream *st,
  468. uint32_t stream_type, const StreamType *types)
  469. {
  470. for (; types->stream_type; types++) {
  471. if (stream_type == types->stream_type) {
  472. st->codec->codec_type = types->codec_type;
  473. st->codec->codec_id = types->codec_id;
  474. return;
  475. }
  476. }
  477. }
  478. static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
  479. uint32_t stream_type, uint32_t prog_reg_desc)
  480. {
  481. av_set_pts_info(st, 33, 1, 90000);
  482. st->priv_data = pes;
  483. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  484. st->codec->codec_id = CODEC_ID_NONE;
  485. st->need_parsing = AVSTREAM_PARSE_FULL;
  486. pes->st = st;
  487. pes->stream_type = stream_type;
  488. av_log(pes->stream, AV_LOG_DEBUG,
  489. "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
  490. st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
  491. st->codec->codec_tag = pes->stream_type;
  492. mpegts_find_stream_type(st, pes->stream_type, ISO_types);
  493. if (prog_reg_desc == AV_RL32("HDMV") &&
  494. st->codec->codec_id == CODEC_ID_NONE) {
  495. mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
  496. if (pes->stream_type == 0x83) {
  497. // HDMV TrueHD streams also contain an AC3 coded version of the
  498. // audio track - add a second stream for this
  499. AVStream *sub_st;
  500. // priv_data cannot be shared between streams
  501. PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
  502. if (!sub_pes)
  503. return AVERROR(ENOMEM);
  504. memcpy(sub_pes, pes, sizeof(*sub_pes));
  505. sub_st = av_new_stream(pes->stream, pes->pid);
  506. if (!sub_st) {
  507. av_free(sub_pes);
  508. return AVERROR(ENOMEM);
  509. }
  510. av_set_pts_info(sub_st, 33, 1, 90000);
  511. sub_st->priv_data = sub_pes;
  512. sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  513. sub_st->codec->codec_id = CODEC_ID_AC3;
  514. sub_st->need_parsing = AVSTREAM_PARSE_FULL;
  515. sub_pes->sub_st = pes->sub_st = sub_st;
  516. }
  517. }
  518. if (st->codec->codec_id == CODEC_ID_NONE)
  519. mpegts_find_stream_type(st, pes->stream_type, MISC_types);
  520. return 0;
  521. }
  522. static void new_pes_packet(PESContext *pes, AVPacket *pkt)
  523. {
  524. av_init_packet(pkt);
  525. pkt->destruct = av_destruct_packet;
  526. pkt->data = pes->buffer;
  527. pkt->size = pes->data_index;
  528. memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  529. // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
  530. if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
  531. pkt->stream_index = pes->sub_st->index;
  532. else
  533. pkt->stream_index = pes->st->index;
  534. pkt->pts = pes->pts;
  535. pkt->dts = pes->dts;
  536. /* store position of first TS packet of this PES packet */
  537. pkt->pos = pes->ts_packet_pos;
  538. /* reset pts values */
  539. pes->pts = AV_NOPTS_VALUE;
  540. pes->dts = AV_NOPTS_VALUE;
  541. pes->buffer = NULL;
  542. pes->data_index = 0;
  543. }
  544. /* return non zero if a packet could be constructed */
  545. static int mpegts_push_data(MpegTSFilter *filter,
  546. const uint8_t *buf, int buf_size, int is_start,
  547. int64_t pos)
  548. {
  549. PESContext *pes = filter->u.pes_filter.opaque;
  550. MpegTSContext *ts = pes->ts;
  551. const uint8_t *p;
  552. int len, code;
  553. if(!ts->pkt)
  554. return 0;
  555. if (is_start) {
  556. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  557. new_pes_packet(pes, ts->pkt);
  558. ts->stop_parse = 1;
  559. }
  560. pes->state = MPEGTS_HEADER;
  561. pes->data_index = 0;
  562. pes->ts_packet_pos = pos;
  563. }
  564. p = buf;
  565. while (buf_size > 0) {
  566. switch(pes->state) {
  567. case MPEGTS_HEADER:
  568. len = PES_START_SIZE - pes->data_index;
  569. if (len > buf_size)
  570. len = buf_size;
  571. memcpy(pes->header + pes->data_index, p, len);
  572. pes->data_index += len;
  573. p += len;
  574. buf_size -= len;
  575. if (pes->data_index == PES_START_SIZE) {
  576. /* we got all the PES or section header. We can now
  577. decide */
  578. #if 0
  579. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  580. #endif
  581. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  582. pes->header[2] == 0x01) {
  583. /* it must be an mpeg2 PES stream */
  584. code = pes->header[3] | 0x100;
  585. av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
  586. if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
  587. code == 0x1be) /* padding_stream */
  588. goto skip;
  589. #if FF_API_MAX_STREAMS
  590. if (!pes->st && pes->stream->nb_streams == MAX_STREAMS)
  591. goto skip;
  592. #endif
  593. /* stream not present in PMT */
  594. if (!pes->st) {
  595. pes->st = av_new_stream(ts->stream, pes->pid);
  596. if (!pes->st)
  597. return AVERROR(ENOMEM);
  598. mpegts_set_stream_info(pes->st, pes, 0, 0);
  599. }
  600. pes->total_size = AV_RB16(pes->header + 4);
  601. /* NOTE: a zero total size means the PES size is
  602. unbounded */
  603. if (!pes->total_size)
  604. pes->total_size = MAX_PES_PAYLOAD;
  605. /* allocate pes buffer */
  606. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  607. if (!pes->buffer)
  608. return AVERROR(ENOMEM);
  609. if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
  610. code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
  611. code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
  612. code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
  613. pes->state = MPEGTS_PESHEADER;
  614. if (pes->st->codec->codec_id == CODEC_ID_NONE) {
  615. av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
  616. pes->pid, pes->stream_type);
  617. pes->st->codec->codec_id = CODEC_ID_PROBE;
  618. }
  619. } else {
  620. pes->state = MPEGTS_PAYLOAD;
  621. pes->data_index = 0;
  622. }
  623. } else {
  624. /* otherwise, it should be a table */
  625. /* skip packet */
  626. skip:
  627. pes->state = MPEGTS_SKIP;
  628. continue;
  629. }
  630. }
  631. break;
  632. /**********************************************/
  633. /* PES packing parsing */
  634. case MPEGTS_PESHEADER:
  635. len = PES_HEADER_SIZE - pes->data_index;
  636. if (len < 0)
  637. return -1;
  638. if (len > buf_size)
  639. len = buf_size;
  640. memcpy(pes->header + pes->data_index, p, len);
  641. pes->data_index += len;
  642. p += len;
  643. buf_size -= len;
  644. if (pes->data_index == PES_HEADER_SIZE) {
  645. pes->pes_header_size = pes->header[8] + 9;
  646. pes->state = MPEGTS_PESHEADER_FILL;
  647. }
  648. break;
  649. case MPEGTS_PESHEADER_FILL:
  650. len = pes->pes_header_size - pes->data_index;
  651. if (len < 0)
  652. return -1;
  653. if (len > buf_size)
  654. len = buf_size;
  655. memcpy(pes->header + pes->data_index, p, len);
  656. pes->data_index += len;
  657. p += len;
  658. buf_size -= len;
  659. if (pes->data_index == pes->pes_header_size) {
  660. const uint8_t *r;
  661. unsigned int flags, pes_ext, skip;
  662. flags = pes->header[7];
  663. r = pes->header + 9;
  664. pes->pts = AV_NOPTS_VALUE;
  665. pes->dts = AV_NOPTS_VALUE;
  666. if ((flags & 0xc0) == 0x80) {
  667. pes->dts = pes->pts = ff_parse_pes_pts(r);
  668. r += 5;
  669. } else if ((flags & 0xc0) == 0xc0) {
  670. pes->pts = ff_parse_pes_pts(r);
  671. r += 5;
  672. pes->dts = ff_parse_pes_pts(r);
  673. r += 5;
  674. }
  675. pes->extended_stream_id = -1;
  676. if (flags & 0x01) { /* PES extension */
  677. pes_ext = *r++;
  678. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  679. skip = (pes_ext >> 4) & 0xb;
  680. skip += skip & 0x9;
  681. r += skip;
  682. if ((pes_ext & 0x41) == 0x01 &&
  683. (r + 2) <= (pes->header + pes->pes_header_size)) {
  684. /* PES extension 2 */
  685. if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
  686. pes->extended_stream_id = r[1];
  687. }
  688. }
  689. /* we got the full header. We parse it and get the payload */
  690. pes->state = MPEGTS_PAYLOAD;
  691. pes->data_index = 0;
  692. }
  693. break;
  694. case MPEGTS_PAYLOAD:
  695. if (buf_size > 0 && pes->buffer) {
  696. if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
  697. new_pes_packet(pes, ts->pkt);
  698. pes->total_size = MAX_PES_PAYLOAD;
  699. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  700. if (!pes->buffer)
  701. return AVERROR(ENOMEM);
  702. ts->stop_parse = 1;
  703. } else if (pes->data_index == 0 && buf_size > pes->total_size) {
  704. // pes packet size is < ts size packet and pes data is padded with 0xff
  705. // not sure if this is legal in ts but see issue #2392
  706. buf_size = pes->total_size;
  707. }
  708. memcpy(pes->buffer+pes->data_index, p, buf_size);
  709. pes->data_index += buf_size;
  710. }
  711. buf_size = 0;
  712. /* emit complete packets with known packet size
  713. * decreases demuxer delay for infrequent packets like subtitles from
  714. * a couple of seconds to milliseconds for properly muxed files.
  715. * total_size is the number of bytes following pes_packet_length
  716. * in the pes header, i.e. not counting the first 6 bytes */
  717. if (pes->total_size < MAX_PES_PAYLOAD &&
  718. pes->pes_header_size + pes->data_index == pes->total_size + 6) {
  719. ts->stop_parse = 1;
  720. new_pes_packet(pes, ts->pkt);
  721. }
  722. break;
  723. case MPEGTS_SKIP:
  724. buf_size = 0;
  725. break;
  726. }
  727. }
  728. return 0;
  729. }
  730. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
  731. {
  732. MpegTSFilter *tss;
  733. PESContext *pes;
  734. /* if no pid found, then add a pid context */
  735. pes = av_mallocz(sizeof(PESContext));
  736. if (!pes)
  737. return 0;
  738. pes->ts = ts;
  739. pes->stream = ts->stream;
  740. pes->pid = pid;
  741. pes->pcr_pid = pcr_pid;
  742. pes->state = MPEGTS_SKIP;
  743. pes->pts = AV_NOPTS_VALUE;
  744. pes->dts = AV_NOPTS_VALUE;
  745. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  746. if (!tss) {
  747. av_free(pes);
  748. return 0;
  749. }
  750. return pes;
  751. }
  752. static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
  753. int *es_id, uint8_t **dec_config_descr,
  754. int *dec_config_descr_size)
  755. {
  756. AVIOContext pb;
  757. int tag;
  758. unsigned len;
  759. ffio_init_context(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
  760. len = ff_mp4_read_descr(s, &pb, &tag);
  761. if (tag == MP4IODescrTag) {
  762. avio_rb16(&pb); // ID
  763. avio_r8(&pb);
  764. avio_r8(&pb);
  765. avio_r8(&pb);
  766. avio_r8(&pb);
  767. avio_r8(&pb);
  768. len = ff_mp4_read_descr(s, &pb, &tag);
  769. if (tag == MP4ESDescrTag) {
  770. *es_id = avio_rb16(&pb); /* ES_ID */
  771. av_dlog(s, "ES_ID %#x\n", *es_id);
  772. avio_r8(&pb); /* priority */
  773. len = ff_mp4_read_descr(s, &pb, &tag);
  774. if (tag == MP4DecConfigDescrTag) {
  775. *dec_config_descr = av_malloc(len);
  776. if (!*dec_config_descr)
  777. return AVERROR(ENOMEM);
  778. *dec_config_descr_size = len;
  779. avio_read(&pb, *dec_config_descr, len);
  780. }
  781. }
  782. }
  783. return 0;
  784. }
  785. int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
  786. const uint8_t **pp, const uint8_t *desc_list_end,
  787. int mp4_dec_config_descr_len, int mp4_es_id, int pid,
  788. uint8_t *mp4_dec_config_descr)
  789. {
  790. const uint8_t *desc_end;
  791. int desc_len, desc_tag;
  792. char language[252];
  793. int i;
  794. desc_tag = get8(pp, desc_list_end);
  795. if (desc_tag < 0)
  796. return -1;
  797. desc_len = get8(pp, desc_list_end);
  798. if (desc_len < 0)
  799. return -1;
  800. desc_end = *pp + desc_len;
  801. if (desc_end > desc_list_end)
  802. return -1;
  803. av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
  804. if (st->codec->codec_id == CODEC_ID_NONE &&
  805. stream_type == STREAM_TYPE_PRIVATE_DATA)
  806. mpegts_find_stream_type(st, desc_tag, DESC_types);
  807. switch(desc_tag) {
  808. case 0x1F: /* FMC descriptor */
  809. get16(pp, desc_end);
  810. if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
  811. mp4_dec_config_descr_len && mp4_es_id == pid) {
  812. AVIOContext pb;
  813. ffio_init_context(&pb, mp4_dec_config_descr,
  814. mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  815. ff_mp4_read_dec_config_descr(fc, st, &pb);
  816. if (st->codec->codec_id == CODEC_ID_AAC &&
  817. st->codec->extradata_size > 0)
  818. st->need_parsing = 0;
  819. }
  820. break;
  821. case 0x56: /* DVB teletext descriptor */
  822. language[0] = get8(pp, desc_end);
  823. language[1] = get8(pp, desc_end);
  824. language[2] = get8(pp, desc_end);
  825. language[3] = 0;
  826. av_metadata_set2(&st->metadata, "language", language, 0);
  827. break;
  828. case 0x59: /* subtitling descriptor */
  829. language[0] = get8(pp, desc_end);
  830. language[1] = get8(pp, desc_end);
  831. language[2] = get8(pp, desc_end);
  832. language[3] = 0;
  833. /* hearing impaired subtitles detection */
  834. switch(get8(pp, desc_end)) {
  835. case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
  836. case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
  837. case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
  838. case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
  839. case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
  840. case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
  841. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  842. break;
  843. }
  844. if (st->codec->extradata) {
  845. if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
  846. av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
  847. } else {
  848. st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  849. if (st->codec->extradata) {
  850. st->codec->extradata_size = 4;
  851. memcpy(st->codec->extradata, *pp, 4);
  852. }
  853. }
  854. *pp += 4;
  855. av_metadata_set2(&st->metadata, "language", language, 0);
  856. break;
  857. case 0x0a: /* ISO 639 language descriptor */
  858. for (i = 0; i + 4 <= desc_len; i += 4) {
  859. language[i + 0] = get8(pp, desc_end);
  860. language[i + 1] = get8(pp, desc_end);
  861. language[i + 2] = get8(pp, desc_end);
  862. language[i + 3] = ',';
  863. switch (get8(pp, desc_end)) {
  864. case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
  865. case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
  866. case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
  867. }
  868. }
  869. if (i) {
  870. language[i - 1] = 0;
  871. av_metadata_set2(&st->metadata, "language", language, 0);
  872. }
  873. break;
  874. case 0x05: /* registration descriptor */
  875. st->codec->codec_tag = bytestream_get_le32(pp);
  876. av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
  877. if (st->codec->codec_id == CODEC_ID_NONE &&
  878. stream_type == STREAM_TYPE_PRIVATE_DATA)
  879. mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
  880. break;
  881. default:
  882. break;
  883. }
  884. *pp = desc_end;
  885. return 0;
  886. }
  887. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  888. {
  889. MpegTSContext *ts = filter->u.section_filter.opaque;
  890. SectionHeader h1, *h = &h1;
  891. PESContext *pes;
  892. AVStream *st;
  893. const uint8_t *p, *p_end, *desc_list_end;
  894. int program_info_length, pcr_pid, pid, stream_type;
  895. int desc_list_len;
  896. uint32_t prog_reg_desc = 0; /* registration descriptor */
  897. uint8_t *mp4_dec_config_descr = NULL;
  898. int mp4_dec_config_descr_len = 0;
  899. int mp4_es_id = 0;
  900. #ifdef DEBUG
  901. av_dlog(ts->stream, "PMT: len %i\n", section_len);
  902. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  903. #endif
  904. p_end = section + section_len - 4;
  905. p = section;
  906. if (parse_section_header(h, &p, p_end) < 0)
  907. return;
  908. av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
  909. h->id, h->sec_num, h->last_sec_num);
  910. if (h->tid != PMT_TID)
  911. return;
  912. clear_program(ts, h->id);
  913. pcr_pid = get16(&p, p_end) & 0x1fff;
  914. if (pcr_pid < 0)
  915. return;
  916. add_pid_to_pmt(ts, h->id, pcr_pid);
  917. av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
  918. program_info_length = get16(&p, p_end) & 0xfff;
  919. if (program_info_length < 0)
  920. return;
  921. while(program_info_length >= 2) {
  922. uint8_t tag, len;
  923. tag = get8(&p, p_end);
  924. len = get8(&p, p_end);
  925. av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
  926. if(len > program_info_length - 2)
  927. //something else is broken, exit the program_descriptors_loop
  928. break;
  929. program_info_length -= len + 2;
  930. if (tag == 0x1d) { // IOD descriptor
  931. get8(&p, p_end); // scope
  932. get8(&p, p_end); // label
  933. len -= 2;
  934. mp4_read_iods(ts->stream, p, len, &mp4_es_id,
  935. &mp4_dec_config_descr, &mp4_dec_config_descr_len);
  936. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  937. prog_reg_desc = bytestream_get_le32(&p);
  938. len -= 4;
  939. }
  940. p += len;
  941. }
  942. p += program_info_length;
  943. if (p >= p_end)
  944. goto out;
  945. // stop parsing after pmt, we found header
  946. if (!ts->stream->nb_streams)
  947. ts->stop_parse = 1;
  948. for(;;) {
  949. st = 0;
  950. stream_type = get8(&p, p_end);
  951. if (stream_type < 0)
  952. break;
  953. pid = get16(&p, p_end) & 0x1fff;
  954. if (pid < 0)
  955. break;
  956. /* now create ffmpeg stream */
  957. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  958. pes = ts->pids[pid]->u.pes_filter.opaque;
  959. if (!pes->st)
  960. pes->st = av_new_stream(pes->stream, pes->pid);
  961. st = pes->st;
  962. } else {
  963. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  964. pes = add_pes_stream(ts, pid, pcr_pid);
  965. if (pes)
  966. st = av_new_stream(pes->stream, pes->pid);
  967. }
  968. if (!st)
  969. goto out;
  970. if (!pes->stream_type)
  971. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  972. add_pid_to_pmt(ts, h->id, pid);
  973. ff_program_add_stream_index(ts->stream, h->id, st->index);
  974. desc_list_len = get16(&p, p_end) & 0xfff;
  975. if (desc_list_len < 0)
  976. break;
  977. desc_list_end = p + desc_list_len;
  978. if (desc_list_end > p_end)
  979. break;
  980. for(;;) {
  981. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
  982. mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
  983. break;
  984. if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
  985. ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
  986. pes->sub_st->codec->codec_tag = st->codec->codec_tag;
  987. }
  988. }
  989. p = desc_list_end;
  990. }
  991. out:
  992. av_free(mp4_dec_config_descr);
  993. }
  994. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  995. {
  996. MpegTSContext *ts = filter->u.section_filter.opaque;
  997. SectionHeader h1, *h = &h1;
  998. const uint8_t *p, *p_end;
  999. int sid, pmt_pid;
  1000. #ifdef DEBUG
  1001. av_dlog(ts->stream, "PAT:\n");
  1002. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  1003. #endif
  1004. p_end = section + section_len - 4;
  1005. p = section;
  1006. if (parse_section_header(h, &p, p_end) < 0)
  1007. return;
  1008. if (h->tid != PAT_TID)
  1009. return;
  1010. clear_programs(ts);
  1011. for(;;) {
  1012. sid = get16(&p, p_end);
  1013. if (sid < 0)
  1014. break;
  1015. pmt_pid = get16(&p, p_end) & 0x1fff;
  1016. if (pmt_pid < 0)
  1017. break;
  1018. av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1019. if (sid == 0x0000) {
  1020. /* NIT info */
  1021. } else {
  1022. av_new_program(ts->stream, sid);
  1023. if (ts->pids[pmt_pid])
  1024. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1025. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1026. add_pat_entry(ts, sid);
  1027. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  1028. add_pid_to_pmt(ts, sid, pmt_pid);
  1029. }
  1030. }
  1031. }
  1032. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1033. {
  1034. MpegTSContext *ts = filter->u.section_filter.opaque;
  1035. SectionHeader h1, *h = &h1;
  1036. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1037. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1038. char *name, *provider_name;
  1039. #ifdef DEBUG
  1040. av_dlog(ts->stream, "SDT:\n");
  1041. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  1042. #endif
  1043. p_end = section + section_len - 4;
  1044. p = section;
  1045. if (parse_section_header(h, &p, p_end) < 0)
  1046. return;
  1047. if (h->tid != SDT_TID)
  1048. return;
  1049. onid = get16(&p, p_end);
  1050. if (onid < 0)
  1051. return;
  1052. val = get8(&p, p_end);
  1053. if (val < 0)
  1054. return;
  1055. for(;;) {
  1056. sid = get16(&p, p_end);
  1057. if (sid < 0)
  1058. break;
  1059. val = get8(&p, p_end);
  1060. if (val < 0)
  1061. break;
  1062. desc_list_len = get16(&p, p_end) & 0xfff;
  1063. if (desc_list_len < 0)
  1064. break;
  1065. desc_list_end = p + desc_list_len;
  1066. if (desc_list_end > p_end)
  1067. break;
  1068. for(;;) {
  1069. desc_tag = get8(&p, desc_list_end);
  1070. if (desc_tag < 0)
  1071. break;
  1072. desc_len = get8(&p, desc_list_end);
  1073. desc_end = p + desc_len;
  1074. if (desc_end > desc_list_end)
  1075. break;
  1076. av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
  1077. desc_tag, desc_len);
  1078. switch(desc_tag) {
  1079. case 0x48:
  1080. service_type = get8(&p, p_end);
  1081. if (service_type < 0)
  1082. break;
  1083. provider_name = getstr8(&p, p_end);
  1084. if (!provider_name)
  1085. break;
  1086. name = getstr8(&p, p_end);
  1087. if (name) {
  1088. AVProgram *program = av_new_program(ts->stream, sid);
  1089. if(program) {
  1090. av_metadata_set2(&program->metadata, "service_name", name, 0);
  1091. av_metadata_set2(&program->metadata, "service_provider", provider_name, 0);
  1092. }
  1093. }
  1094. av_free(name);
  1095. av_free(provider_name);
  1096. break;
  1097. default:
  1098. break;
  1099. }
  1100. p = desc_end;
  1101. }
  1102. p = desc_list_end;
  1103. }
  1104. }
  1105. /* handle one TS packet */
  1106. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1107. {
  1108. AVFormatContext *s = ts->stream;
  1109. MpegTSFilter *tss;
  1110. int len, pid, cc, cc_ok, afc, is_start;
  1111. const uint8_t *p, *p_end;
  1112. int64_t pos;
  1113. pid = AV_RB16(packet + 1) & 0x1fff;
  1114. if(pid && discard_pid(ts, pid))
  1115. return 0;
  1116. is_start = packet[1] & 0x40;
  1117. tss = ts->pids[pid];
  1118. if (ts->auto_guess && tss == NULL && is_start) {
  1119. add_pes_stream(ts, pid, -1);
  1120. tss = ts->pids[pid];
  1121. }
  1122. if (!tss)
  1123. return 0;
  1124. /* continuity check (currently not used) */
  1125. cc = (packet[3] & 0xf);
  1126. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  1127. tss->last_cc = cc;
  1128. /* skip adaptation field */
  1129. afc = (packet[3] >> 4) & 3;
  1130. p = packet + 4;
  1131. if (afc == 0) /* reserved value */
  1132. return 0;
  1133. if (afc == 2) /* adaptation field only */
  1134. return 0;
  1135. if (afc == 3) {
  1136. /* skip adapation field */
  1137. p += p[0] + 1;
  1138. }
  1139. /* if past the end of packet, ignore */
  1140. p_end = packet + TS_PACKET_SIZE;
  1141. if (p >= p_end)
  1142. return 0;
  1143. pos = avio_tell(ts->stream->pb);
  1144. ts->pos47= pos % ts->raw_packet_size;
  1145. if (tss->type == MPEGTS_SECTION) {
  1146. if (is_start) {
  1147. /* pointer field present */
  1148. len = *p++;
  1149. if (p + len > p_end)
  1150. return 0;
  1151. if (len && cc_ok) {
  1152. /* write remaining section bytes */
  1153. write_section_data(s, tss,
  1154. p, len, 0);
  1155. /* check whether filter has been closed */
  1156. if (!ts->pids[pid])
  1157. return 0;
  1158. }
  1159. p += len;
  1160. if (p < p_end) {
  1161. write_section_data(s, tss,
  1162. p, p_end - p, 1);
  1163. }
  1164. } else {
  1165. if (cc_ok) {
  1166. write_section_data(s, tss,
  1167. p, p_end - p, 0);
  1168. }
  1169. }
  1170. } else {
  1171. int ret;
  1172. // Note: The position here points actually behind the current packet.
  1173. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1174. pos - ts->raw_packet_size)) < 0)
  1175. return ret;
  1176. }
  1177. return 0;
  1178. }
  1179. /* XXX: try to find a better synchro over several packets (use
  1180. get_packet_size() ?) */
  1181. static int mpegts_resync(AVFormatContext *s)
  1182. {
  1183. AVIOContext *pb = s->pb;
  1184. int c, i;
  1185. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1186. c = avio_r8(pb);
  1187. if (pb->eof_reached)
  1188. return -1;
  1189. if (c == 0x47) {
  1190. avio_seek(pb, -1, SEEK_CUR);
  1191. return 0;
  1192. }
  1193. }
  1194. av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
  1195. /* no sync found */
  1196. return -1;
  1197. }
  1198. /* return -1 if error or EOF. Return 0 if OK. */
  1199. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
  1200. {
  1201. AVIOContext *pb = s->pb;
  1202. int skip, len;
  1203. for(;;) {
  1204. len = avio_read(pb, buf, TS_PACKET_SIZE);
  1205. if (len != TS_PACKET_SIZE)
  1206. return AVERROR(EIO);
  1207. /* check paquet sync byte */
  1208. if (buf[0] != 0x47) {
  1209. /* find a new packet start */
  1210. avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1211. if (mpegts_resync(s) < 0)
  1212. return AVERROR(EAGAIN);
  1213. else
  1214. continue;
  1215. } else {
  1216. skip = raw_packet_size - TS_PACKET_SIZE;
  1217. if (skip > 0)
  1218. avio_skip(pb, skip);
  1219. break;
  1220. }
  1221. }
  1222. return 0;
  1223. }
  1224. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1225. {
  1226. AVFormatContext *s = ts->stream;
  1227. uint8_t packet[TS_PACKET_SIZE];
  1228. int packet_num, ret;
  1229. ts->stop_parse = 0;
  1230. packet_num = 0;
  1231. for(;;) {
  1232. if (ts->stop_parse>0)
  1233. break;
  1234. packet_num++;
  1235. if (nb_packets != 0 && packet_num >= nb_packets)
  1236. break;
  1237. ret = read_packet(s, packet, ts->raw_packet_size);
  1238. if (ret != 0)
  1239. return ret;
  1240. ret = handle_packet(ts, packet);
  1241. if (ret != 0)
  1242. return ret;
  1243. }
  1244. return 0;
  1245. }
  1246. static int mpegts_probe(AVProbeData *p)
  1247. {
  1248. #if 1
  1249. const int size= p->buf_size;
  1250. int score, fec_score, dvhs_score;
  1251. int check_count= size / TS_FEC_PACKET_SIZE;
  1252. #define CHECK_COUNT 10
  1253. if (check_count < CHECK_COUNT)
  1254. return -1;
  1255. score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1256. dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
  1257. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1258. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1259. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1260. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1261. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1262. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1263. else return -1;
  1264. #else
  1265. /* only use the extension for safer guess */
  1266. if (av_match_ext(p->filename, "ts"))
  1267. return AVPROBE_SCORE_MAX;
  1268. else
  1269. return 0;
  1270. #endif
  1271. }
  1272. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1273. (-1) if not available */
  1274. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1275. const uint8_t *packet)
  1276. {
  1277. int afc, len, flags;
  1278. const uint8_t *p;
  1279. unsigned int v;
  1280. afc = (packet[3] >> 4) & 3;
  1281. if (afc <= 1)
  1282. return -1;
  1283. p = packet + 4;
  1284. len = p[0];
  1285. p++;
  1286. if (len == 0)
  1287. return -1;
  1288. flags = *p++;
  1289. len--;
  1290. if (!(flags & 0x10))
  1291. return -1;
  1292. if (len < 6)
  1293. return -1;
  1294. v = AV_RB32(p);
  1295. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1296. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1297. return 0;
  1298. }
  1299. static int mpegts_read_header(AVFormatContext *s,
  1300. AVFormatParameters *ap)
  1301. {
  1302. MpegTSContext *ts = s->priv_data;
  1303. AVIOContext *pb = s->pb;
  1304. uint8_t buf[5*1024];
  1305. int len;
  1306. int64_t pos;
  1307. if (ap) {
  1308. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1309. if(ap->mpeg2ts_raw){
  1310. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1311. return -1;
  1312. }
  1313. }
  1314. /* read the first 1024 bytes to get packet size */
  1315. pos = avio_tell(pb);
  1316. len = avio_read(pb, buf, sizeof(buf));
  1317. if (len != sizeof(buf))
  1318. goto fail;
  1319. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1320. if (ts->raw_packet_size <= 0)
  1321. goto fail;
  1322. ts->stream = s;
  1323. ts->auto_guess = 0;
  1324. if (s->iformat == &ff_mpegts_demuxer) {
  1325. /* normal demux */
  1326. /* first do a scaning to get all the services */
  1327. if (avio_seek(pb, pos, SEEK_SET) < 0)
  1328. av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
  1329. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1330. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1331. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1332. /* if could not find service, enable auto_guess */
  1333. ts->auto_guess = 1;
  1334. av_dlog(ts->stream, "tuning done\n");
  1335. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1336. } else {
  1337. AVStream *st;
  1338. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1339. int64_t pcrs[2], pcr_h;
  1340. int packet_count[2];
  1341. uint8_t packet[TS_PACKET_SIZE];
  1342. /* only read packets */
  1343. st = av_new_stream(s, 0);
  1344. if (!st)
  1345. goto fail;
  1346. av_set_pts_info(st, 60, 1, 27000000);
  1347. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1348. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1349. /* we iterate until we find two PCRs to estimate the bitrate */
  1350. pcr_pid = -1;
  1351. nb_pcrs = 0;
  1352. nb_packets = 0;
  1353. for(;;) {
  1354. ret = read_packet(s, packet, ts->raw_packet_size);
  1355. if (ret < 0)
  1356. return -1;
  1357. pid = AV_RB16(packet + 1) & 0x1fff;
  1358. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1359. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1360. pcr_pid = pid;
  1361. packet_count[nb_pcrs] = nb_packets;
  1362. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1363. nb_pcrs++;
  1364. if (nb_pcrs >= 2)
  1365. break;
  1366. }
  1367. nb_packets++;
  1368. }
  1369. /* NOTE1: the bitrate is computed without the FEC */
  1370. /* NOTE2: it is only the bitrate of the start of the stream */
  1371. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1372. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1373. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1374. st->codec->bit_rate = s->bit_rate;
  1375. st->start_time = ts->cur_pcr;
  1376. #if 0
  1377. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1378. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1379. #endif
  1380. }
  1381. avio_seek(pb, pos, SEEK_SET);
  1382. return 0;
  1383. fail:
  1384. return -1;
  1385. }
  1386. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1387. static int mpegts_raw_read_packet(AVFormatContext *s,
  1388. AVPacket *pkt)
  1389. {
  1390. MpegTSContext *ts = s->priv_data;
  1391. int ret, i;
  1392. int64_t pcr_h, next_pcr_h, pos;
  1393. int pcr_l, next_pcr_l;
  1394. uint8_t pcr_buf[12];
  1395. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1396. return AVERROR(ENOMEM);
  1397. pkt->pos= avio_tell(s->pb);
  1398. ret = read_packet(s, pkt->data, ts->raw_packet_size);
  1399. if (ret < 0) {
  1400. av_free_packet(pkt);
  1401. return ret;
  1402. }
  1403. if (ts->mpeg2ts_compute_pcr) {
  1404. /* compute exact PCR for each packet */
  1405. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1406. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1407. pos = avio_tell(s->pb);
  1408. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1409. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1410. avio_read(s->pb, pcr_buf, 12);
  1411. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1412. /* XXX: not precise enough */
  1413. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1414. (i + 1);
  1415. break;
  1416. }
  1417. }
  1418. avio_seek(s->pb, pos, SEEK_SET);
  1419. /* no next PCR found: we use previous increment */
  1420. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1421. }
  1422. pkt->pts = ts->cur_pcr;
  1423. pkt->duration = ts->pcr_incr;
  1424. ts->cur_pcr += ts->pcr_incr;
  1425. }
  1426. pkt->stream_index = 0;
  1427. return 0;
  1428. }
  1429. static int mpegts_read_packet(AVFormatContext *s,
  1430. AVPacket *pkt)
  1431. {
  1432. MpegTSContext *ts = s->priv_data;
  1433. int ret, i;
  1434. if (avio_tell(s->pb) != ts->last_pos) {
  1435. /* seek detected, flush pes buffer */
  1436. for (i = 0; i < NB_PID_MAX; i++) {
  1437. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1438. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1439. av_freep(&pes->buffer);
  1440. pes->data_index = 0;
  1441. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1442. }
  1443. }
  1444. }
  1445. ts->pkt = pkt;
  1446. ret = handle_packets(ts, 0);
  1447. if (ret < 0) {
  1448. /* flush pes data left */
  1449. for (i = 0; i < NB_PID_MAX; i++) {
  1450. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1451. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1452. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  1453. new_pes_packet(pes, pkt);
  1454. pes->state = MPEGTS_SKIP;
  1455. ret = 0;
  1456. break;
  1457. }
  1458. }
  1459. }
  1460. }
  1461. ts->last_pos = avio_tell(s->pb);
  1462. return ret;
  1463. }
  1464. static int mpegts_read_close(AVFormatContext *s)
  1465. {
  1466. MpegTSContext *ts = s->priv_data;
  1467. int i;
  1468. clear_programs(ts);
  1469. for(i=0;i<NB_PID_MAX;i++)
  1470. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1471. return 0;
  1472. }
  1473. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1474. int64_t *ppos, int64_t pos_limit)
  1475. {
  1476. MpegTSContext *ts = s->priv_data;
  1477. int64_t pos, timestamp;
  1478. uint8_t buf[TS_PACKET_SIZE];
  1479. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1480. const int find_next= 1;
  1481. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1482. if (find_next) {
  1483. for(;;) {
  1484. avio_seek(s->pb, pos, SEEK_SET);
  1485. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1486. return AV_NOPTS_VALUE;
  1487. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1488. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1489. break;
  1490. }
  1491. pos += ts->raw_packet_size;
  1492. }
  1493. } else {
  1494. for(;;) {
  1495. pos -= ts->raw_packet_size;
  1496. if (pos < 0)
  1497. return AV_NOPTS_VALUE;
  1498. avio_seek(s->pb, pos, SEEK_SET);
  1499. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1500. return AV_NOPTS_VALUE;
  1501. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1502. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1503. break;
  1504. }
  1505. }
  1506. }
  1507. *ppos = pos;
  1508. return timestamp;
  1509. }
  1510. #ifdef USE_SYNCPOINT_SEARCH
  1511. static int read_seek2(AVFormatContext *s,
  1512. int stream_index,
  1513. int64_t min_ts,
  1514. int64_t target_ts,
  1515. int64_t max_ts,
  1516. int flags)
  1517. {
  1518. int64_t pos;
  1519. int64_t ts_ret, ts_adj;
  1520. int stream_index_gen_search;
  1521. AVStream *st;
  1522. AVParserState *backup;
  1523. backup = ff_store_parser_state(s);
  1524. // detect direction of seeking for search purposes
  1525. flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
  1526. AVSEEK_FLAG_BACKWARD : 0;
  1527. if (flags & AVSEEK_FLAG_BYTE) {
  1528. // use position directly, we will search starting from it
  1529. pos = target_ts;
  1530. } else {
  1531. // search for some position with good timestamp match
  1532. if (stream_index < 0) {
  1533. stream_index_gen_search = av_find_default_stream_index(s);
  1534. if (stream_index_gen_search < 0) {
  1535. ff_restore_parser_state(s, backup);
  1536. return -1;
  1537. }
  1538. st = s->streams[stream_index_gen_search];
  1539. // timestamp for default must be expressed in AV_TIME_BASE units
  1540. ts_adj = av_rescale(target_ts,
  1541. st->time_base.den,
  1542. AV_TIME_BASE * (int64_t)st->time_base.num);
  1543. } else {
  1544. ts_adj = target_ts;
  1545. stream_index_gen_search = stream_index;
  1546. }
  1547. pos = av_gen_search(s, stream_index_gen_search, ts_adj,
  1548. 0, INT64_MAX, -1,
  1549. AV_NOPTS_VALUE,
  1550. AV_NOPTS_VALUE,
  1551. flags, &ts_ret, mpegts_get_pcr);
  1552. if (pos < 0) {
  1553. ff_restore_parser_state(s, backup);
  1554. return -1;
  1555. }
  1556. }
  1557. // search for actual matching keyframe/starting position for all streams
  1558. if (ff_gen_syncpoint_search(s, stream_index, pos,
  1559. min_ts, target_ts, max_ts,
  1560. flags) < 0) {
  1561. ff_restore_parser_state(s, backup);
  1562. return -1;
  1563. }
  1564. ff_free_parser_state(s, backup);
  1565. return 0;
  1566. }
  1567. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
  1568. {
  1569. int ret;
  1570. if (flags & AVSEEK_FLAG_BACKWARD) {
  1571. flags &= ~AVSEEK_FLAG_BACKWARD;
  1572. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
  1573. if (ret < 0)
  1574. // for compatibility reasons, seek to the best-fitting timestamp
  1575. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1576. } else {
  1577. ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
  1578. if (ret < 0)
  1579. // for compatibility reasons, seek to the best-fitting timestamp
  1580. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1581. }
  1582. return ret;
  1583. }
  1584. #else
  1585. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1586. MpegTSContext *ts = s->priv_data;
  1587. uint8_t buf[TS_PACKET_SIZE];
  1588. int64_t pos;
  1589. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1590. return -1;
  1591. pos= avio_tell(s->pb);
  1592. for(;;) {
  1593. avio_seek(s->pb, pos, SEEK_SET);
  1594. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1595. return -1;
  1596. // pid = AV_RB16(buf + 1) & 0x1fff;
  1597. if(buf[1] & 0x40) break;
  1598. pos += ts->raw_packet_size;
  1599. }
  1600. avio_seek(s->pb, pos, SEEK_SET);
  1601. return 0;
  1602. }
  1603. #endif
  1604. /**************************************************************/
  1605. /* parsing functions - called from other demuxers such as RTP */
  1606. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  1607. {
  1608. MpegTSContext *ts;
  1609. ts = av_mallocz(sizeof(MpegTSContext));
  1610. if (!ts)
  1611. return NULL;
  1612. /* no stream case, currently used by RTP */
  1613. ts->raw_packet_size = TS_PACKET_SIZE;
  1614. ts->stream = s;
  1615. ts->auto_guess = 1;
  1616. return ts;
  1617. }
  1618. /* return the consumed length if a packet was output, or -1 if no
  1619. packet is output */
  1620. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1621. const uint8_t *buf, int len)
  1622. {
  1623. int len1;
  1624. len1 = len;
  1625. ts->pkt = pkt;
  1626. ts->stop_parse = 0;
  1627. for(;;) {
  1628. if (ts->stop_parse>0)
  1629. break;
  1630. if (len < TS_PACKET_SIZE)
  1631. return -1;
  1632. if (buf[0] != 0x47) {
  1633. buf++;
  1634. len--;
  1635. } else {
  1636. handle_packet(ts, buf);
  1637. buf += TS_PACKET_SIZE;
  1638. len -= TS_PACKET_SIZE;
  1639. }
  1640. }
  1641. return len1 - len;
  1642. }
  1643. void ff_mpegts_parse_close(MpegTSContext *ts)
  1644. {
  1645. int i;
  1646. for(i=0;i<NB_PID_MAX;i++)
  1647. av_free(ts->pids[i]);
  1648. av_free(ts);
  1649. }
  1650. AVInputFormat ff_mpegts_demuxer = {
  1651. "mpegts",
  1652. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  1653. sizeof(MpegTSContext),
  1654. mpegts_probe,
  1655. mpegts_read_header,
  1656. mpegts_read_packet,
  1657. mpegts_read_close,
  1658. read_seek,
  1659. mpegts_get_pcr,
  1660. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1661. #ifdef USE_SYNCPOINT_SEARCH
  1662. .read_seek2 = read_seek2,
  1663. #endif
  1664. };
  1665. AVInputFormat ff_mpegtsraw_demuxer = {
  1666. "mpegtsraw",
  1667. NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
  1668. sizeof(MpegTSContext),
  1669. NULL,
  1670. mpegts_read_header,
  1671. mpegts_raw_read_packet,
  1672. mpegts_read_close,
  1673. read_seek,
  1674. mpegts_get_pcr,
  1675. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1676. #ifdef USE_SYNCPOINT_SEARCH
  1677. .read_seek2 = read_seek2,
  1678. #endif
  1679. };