mpegts.c 57 KB

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