mpegts.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  1. /*
  2. * MPEG2 transport stream (aka DVB) demuxer
  3. * Copyright (c) 2002-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/crc.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/dict.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavcodec/bytestream.h"
  29. #include "libavcodec/get_bits.h"
  30. #include "avformat.h"
  31. #include "mpegts.h"
  32. #include "internal.h"
  33. #include "avio_internal.h"
  34. #include "seek.h"
  35. #include "mpeg.h"
  36. #include "isom.h"
  37. /* maximum size in which we look for synchronisation if
  38. synchronisation is lost */
  39. #define MAX_RESYNC_SIZE 65536
  40. #define MAX_PES_PAYLOAD 200*1024
  41. #define MAX_MP4_DESCR_COUNT 16
  42. enum MpegTSFilterType {
  43. MPEGTS_PES,
  44. MPEGTS_SECTION,
  45. };
  46. typedef struct MpegTSFilter MpegTSFilter;
  47. typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
  48. typedef struct MpegTSPESFilter {
  49. PESCallback *pes_cb;
  50. void *opaque;
  51. } MpegTSPESFilter;
  52. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  53. typedef void SetServiceCallback(void *opaque, int ret);
  54. typedef struct MpegTSSectionFilter {
  55. int section_index;
  56. int section_h_size;
  57. uint8_t *section_buf;
  58. unsigned int check_crc:1;
  59. unsigned int end_of_section_reached:1;
  60. SectionCallback *section_cb;
  61. void *opaque;
  62. } MpegTSSectionFilter;
  63. struct MpegTSFilter {
  64. int pid;
  65. int es_id;
  66. int last_cc; /* last cc code (-1 if first packet) */
  67. enum MpegTSFilterType type;
  68. union {
  69. MpegTSPESFilter pes_filter;
  70. MpegTSSectionFilter section_filter;
  71. } u;
  72. };
  73. #define MAX_PIDS_PER_PROGRAM 64
  74. struct Program {
  75. unsigned int id; //program id/service id
  76. unsigned int nb_pids;
  77. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  78. };
  79. struct MpegTSContext {
  80. const AVClass *class;
  81. /* user data */
  82. AVFormatContext *stream;
  83. /** raw packet size, including FEC if present */
  84. int raw_packet_size;
  85. int pos47;
  86. /** if true, all pids are analyzed to find streams */
  87. int auto_guess;
  88. /** compute exact PCR for each transport stream packet */
  89. int mpeg2ts_compute_pcr;
  90. int64_t cur_pcr; /**< used to estimate the exact PCR */
  91. int pcr_incr; /**< used to estimate the exact PCR */
  92. /* data needed to handle file based ts */
  93. /** stop parsing loop */
  94. int stop_parse;
  95. /** packet containing Audio/Video data */
  96. AVPacket *pkt;
  97. /** to detect seek */
  98. int64_t last_pos;
  99. /******************************************/
  100. /* private mpegts data */
  101. /* scan context */
  102. /** structure to keep track of Program->pids mapping */
  103. unsigned int nb_prg;
  104. struct Program *prg;
  105. /** filters for various streams specified by PMT + for the PAT and PMT */
  106. MpegTSFilter *pids[NB_PID_MAX];
  107. };
  108. static const AVOption options[] = {
  109. {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
  110. {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  111. { NULL },
  112. };
  113. static const AVClass mpegtsraw_class = {
  114. .class_name = "mpegtsraw demuxer",
  115. .item_name = av_default_item_name,
  116. .option = options,
  117. .version = LIBAVUTIL_VERSION_INT,
  118. };
  119. /* TS stream handling */
  120. enum MpegTSState {
  121. MPEGTS_HEADER = 0,
  122. MPEGTS_PESHEADER,
  123. MPEGTS_PESHEADER_FILL,
  124. MPEGTS_PAYLOAD,
  125. MPEGTS_SKIP,
  126. };
  127. /* enough for PES header + length */
  128. #define PES_START_SIZE 6
  129. #define PES_HEADER_SIZE 9
  130. #define MAX_PES_HEADER_SIZE (9 + 255)
  131. typedef struct PESContext {
  132. int pid;
  133. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  134. int stream_type;
  135. MpegTSContext *ts;
  136. AVFormatContext *stream;
  137. AVStream *st;
  138. AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
  139. enum MpegTSState state;
  140. /* used to get the format */
  141. int data_index;
  142. int flags; /**< copied to the AVPacket flags */
  143. int total_size;
  144. int pes_header_size;
  145. int extended_stream_id;
  146. int64_t pts, dts;
  147. int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
  148. uint8_t header[MAX_PES_HEADER_SIZE];
  149. uint8_t *buffer;
  150. SLConfigDescr sl;
  151. } PESContext;
  152. extern AVInputFormat ff_mpegts_demuxer;
  153. static void clear_program(MpegTSContext *ts, unsigned int programid)
  154. {
  155. int i;
  156. for(i=0; i<ts->nb_prg; i++)
  157. if(ts->prg[i].id == programid)
  158. ts->prg[i].nb_pids = 0;
  159. }
  160. static void clear_programs(MpegTSContext *ts)
  161. {
  162. av_freep(&ts->prg);
  163. ts->nb_prg=0;
  164. }
  165. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  166. {
  167. struct Program *p;
  168. void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
  169. if(!tmp)
  170. return;
  171. ts->prg = tmp;
  172. p = &ts->prg[ts->nb_prg];
  173. p->id = programid;
  174. p->nb_pids = 0;
  175. ts->nb_prg++;
  176. }
  177. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  178. {
  179. int i;
  180. struct Program *p = NULL;
  181. for(i=0; i<ts->nb_prg; i++) {
  182. if(ts->prg[i].id == programid) {
  183. p = &ts->prg[i];
  184. break;
  185. }
  186. }
  187. if(!p)
  188. return;
  189. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  190. return;
  191. p->pids[p->nb_pids++] = pid;
  192. }
  193. static void set_pcr_pid(AVFormatContext *s, unsigned int programid, unsigned int pid)
  194. {
  195. int i;
  196. for(i=0; i<s->nb_programs; i++) {
  197. if(s->programs[i]->id == programid) {
  198. s->programs[i]->pcr_pid = pid;
  199. break;
  200. }
  201. }
  202. }
  203. /**
  204. * @brief discard_pid() decides if the pid is to be discarded according
  205. * to caller's programs selection
  206. * @param ts : - TS context
  207. * @param pid : - pid
  208. * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  209. * 0 otherwise
  210. */
  211. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  212. {
  213. int i, j, k;
  214. int used = 0, discarded = 0;
  215. struct Program *p;
  216. for(i=0; i<ts->nb_prg; i++) {
  217. p = &ts->prg[i];
  218. for(j=0; j<p->nb_pids; j++) {
  219. if(p->pids[j] != pid)
  220. continue;
  221. //is program with id p->id set to be discarded?
  222. for(k=0; k<ts->stream->nb_programs; k++) {
  223. if(ts->stream->programs[k]->id == p->id) {
  224. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  225. discarded++;
  226. else
  227. used++;
  228. }
  229. }
  230. }
  231. }
  232. return !used && discarded;
  233. }
  234. /**
  235. * Assemble PES packets out of TS packets, and then call the "section_cb"
  236. * function when they are complete.
  237. */
  238. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  239. const uint8_t *buf, int buf_size, int is_start)
  240. {
  241. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  242. int len;
  243. if (is_start) {
  244. memcpy(tss->section_buf, buf, buf_size);
  245. tss->section_index = buf_size;
  246. tss->section_h_size = -1;
  247. tss->end_of_section_reached = 0;
  248. } else {
  249. if (tss->end_of_section_reached)
  250. return;
  251. len = 4096 - tss->section_index;
  252. if (buf_size < len)
  253. len = buf_size;
  254. memcpy(tss->section_buf + tss->section_index, buf, len);
  255. tss->section_index += len;
  256. }
  257. /* compute section length if possible */
  258. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  259. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  260. if (len > 4096)
  261. return;
  262. tss->section_h_size = len;
  263. }
  264. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  265. tss->end_of_section_reached = 1;
  266. if (!tss->check_crc ||
  267. av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
  268. tss->section_buf, tss->section_h_size) == 0)
  269. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  270. }
  271. }
  272. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  273. SectionCallback *section_cb, void *opaque,
  274. int check_crc)
  275. {
  276. MpegTSFilter *filter;
  277. MpegTSSectionFilter *sec;
  278. av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
  279. if (pid >= NB_PID_MAX || ts->pids[pid])
  280. return NULL;
  281. filter = av_mallocz(sizeof(MpegTSFilter));
  282. if (!filter)
  283. return NULL;
  284. ts->pids[pid] = filter;
  285. filter->type = MPEGTS_SECTION;
  286. filter->pid = pid;
  287. filter->es_id = -1;
  288. filter->last_cc = -1;
  289. sec = &filter->u.section_filter;
  290. sec->section_cb = section_cb;
  291. sec->opaque = opaque;
  292. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  293. sec->check_crc = check_crc;
  294. if (!sec->section_buf) {
  295. av_free(filter);
  296. return NULL;
  297. }
  298. return filter;
  299. }
  300. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  301. PESCallback *pes_cb,
  302. void *opaque)
  303. {
  304. MpegTSFilter *filter;
  305. MpegTSPESFilter *pes;
  306. if (pid >= NB_PID_MAX || ts->pids[pid])
  307. return NULL;
  308. filter = av_mallocz(sizeof(MpegTSFilter));
  309. if (!filter)
  310. return NULL;
  311. ts->pids[pid] = filter;
  312. filter->type = MPEGTS_PES;
  313. filter->pid = pid;
  314. filter->es_id = -1;
  315. filter->last_cc = -1;
  316. pes = &filter->u.pes_filter;
  317. pes->pes_cb = pes_cb;
  318. pes->opaque = opaque;
  319. return filter;
  320. }
  321. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  322. {
  323. int pid;
  324. pid = filter->pid;
  325. if (filter->type == MPEGTS_SECTION)
  326. av_freep(&filter->u.section_filter.section_buf);
  327. else if (filter->type == MPEGTS_PES) {
  328. PESContext *pes = filter->u.pes_filter.opaque;
  329. av_freep(&pes->buffer);
  330. /* referenced private data will be freed later in
  331. * avformat_close_input */
  332. if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
  333. av_freep(&filter->u.pes_filter.opaque);
  334. }
  335. }
  336. av_free(filter);
  337. ts->pids[pid] = NULL;
  338. }
  339. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  340. int stat[TS_MAX_PACKET_SIZE];
  341. int i;
  342. int x=0;
  343. int best_score=0;
  344. memset(stat, 0, packet_size*sizeof(int));
  345. for(x=i=0; i<size-3; i++){
  346. if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && buf[i+3] != 0x47){
  347. stat[x]++;
  348. if(stat[x] > best_score){
  349. best_score= stat[x];
  350. if(index) *index= x;
  351. }
  352. }
  353. x++;
  354. if(x == packet_size) x= 0;
  355. }
  356. return best_score;
  357. }
  358. /* autodetect fec presence. Must have at least 1024 bytes */
  359. static int get_packet_size(const uint8_t *buf, int size)
  360. {
  361. int score, fec_score, dvhs_score;
  362. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  363. return -1;
  364. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  365. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  366. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  367. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  368. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  369. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  370. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  371. else return -1;
  372. }
  373. typedef struct SectionHeader {
  374. uint8_t tid;
  375. uint16_t id;
  376. uint8_t version;
  377. uint8_t sec_num;
  378. uint8_t last_sec_num;
  379. } SectionHeader;
  380. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  381. {
  382. const uint8_t *p;
  383. int c;
  384. p = *pp;
  385. if (p >= p_end)
  386. return -1;
  387. c = *p++;
  388. *pp = p;
  389. return c;
  390. }
  391. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  392. {
  393. const uint8_t *p;
  394. int c;
  395. p = *pp;
  396. if ((p + 1) >= p_end)
  397. return -1;
  398. c = AV_RB16(p);
  399. p += 2;
  400. *pp = p;
  401. return c;
  402. }
  403. /* read and allocate a DVB string preceded by its length */
  404. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  405. {
  406. int len;
  407. const uint8_t *p;
  408. char *str;
  409. p = *pp;
  410. len = get8(&p, p_end);
  411. if (len < 0)
  412. return NULL;
  413. if ((p + len) > p_end)
  414. return NULL;
  415. str = av_malloc(len + 1);
  416. if (!str)
  417. return NULL;
  418. memcpy(str, p, len);
  419. str[len] = '\0';
  420. p += len;
  421. *pp = p;
  422. return str;
  423. }
  424. static int parse_section_header(SectionHeader *h,
  425. const uint8_t **pp, const uint8_t *p_end)
  426. {
  427. int val;
  428. val = get8(pp, p_end);
  429. if (val < 0)
  430. return -1;
  431. h->tid = val;
  432. *pp += 2;
  433. val = get16(pp, p_end);
  434. if (val < 0)
  435. return -1;
  436. h->id = val;
  437. val = get8(pp, p_end);
  438. if (val < 0)
  439. return -1;
  440. h->version = (val >> 1) & 0x1f;
  441. val = get8(pp, p_end);
  442. if (val < 0)
  443. return -1;
  444. h->sec_num = val;
  445. val = get8(pp, p_end);
  446. if (val < 0)
  447. return -1;
  448. h->last_sec_num = val;
  449. return 0;
  450. }
  451. typedef struct {
  452. uint32_t stream_type;
  453. enum AVMediaType codec_type;
  454. enum AVCodecID codec_id;
  455. } StreamType;
  456. static const StreamType ISO_types[] = {
  457. { 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
  458. { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
  459. { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
  460. { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
  461. { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
  462. { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 },
  463. /* Makito encoder sets stream type 0x11 for AAC,
  464. * so auto-detect LOAS/LATM instead of hardcoding it. */
  465. // { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
  466. { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
  467. { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
  468. { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
  469. { 0 },
  470. };
  471. static const StreamType HDMV_types[] = {
  472. { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
  473. { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  474. { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  475. { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
  476. { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
  477. { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
  478. { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
  479. { 0xa1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC3 Secondary Audio */
  480. { 0xa2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS Express Secondary Audio */
  481. { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
  482. { 0 },
  483. };
  484. /* ATSC ? */
  485. static const StreamType MISC_types[] = {
  486. { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  487. { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  488. { 0 },
  489. };
  490. static const StreamType REGD_types[] = {
  491. { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
  492. { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  493. { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
  494. { MKTAG('D','T','S','1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  495. { MKTAG('D','T','S','2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  496. { MKTAG('D','T','S','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  497. { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
  498. { 0 },
  499. };
  500. /* descriptor present */
  501. static const StreamType DESC_types[] = {
  502. { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
  503. { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  504. { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  505. { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
  506. { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  507. { 0 },
  508. };
  509. static void mpegts_find_stream_type(AVStream *st,
  510. uint32_t stream_type, const StreamType *types)
  511. {
  512. for (; types->stream_type; types++) {
  513. if (stream_type == types->stream_type) {
  514. st->codec->codec_type = types->codec_type;
  515. st->codec->codec_id = types->codec_id;
  516. st->request_probe = 0;
  517. return;
  518. }
  519. }
  520. }
  521. static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
  522. uint32_t stream_type, uint32_t prog_reg_desc)
  523. {
  524. int old_codec_type= st->codec->codec_type;
  525. int old_codec_id = st->codec->codec_id;
  526. avpriv_set_pts_info(st, 33, 1, 90000);
  527. st->priv_data = pes;
  528. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  529. st->codec->codec_id = AV_CODEC_ID_NONE;
  530. st->need_parsing = AVSTREAM_PARSE_FULL;
  531. pes->st = st;
  532. pes->stream_type = stream_type;
  533. av_log(pes->stream, AV_LOG_DEBUG,
  534. "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
  535. st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
  536. st->codec->codec_tag = pes->stream_type;
  537. mpegts_find_stream_type(st, pes->stream_type, ISO_types);
  538. if ((prog_reg_desc == AV_RL32("HDMV") ||
  539. prog_reg_desc == AV_RL32("HDPR")) &&
  540. st->codec->codec_id == AV_CODEC_ID_NONE) {
  541. mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
  542. if (pes->stream_type == 0x83) {
  543. // HDMV TrueHD streams also contain an AC3 coded version of the
  544. // audio track - add a second stream for this
  545. AVStream *sub_st;
  546. // priv_data cannot be shared between streams
  547. PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
  548. if (!sub_pes)
  549. return AVERROR(ENOMEM);
  550. memcpy(sub_pes, pes, sizeof(*sub_pes));
  551. sub_st = avformat_new_stream(pes->stream, NULL);
  552. if (!sub_st) {
  553. av_free(sub_pes);
  554. return AVERROR(ENOMEM);
  555. }
  556. sub_st->id = pes->pid;
  557. avpriv_set_pts_info(sub_st, 33, 1, 90000);
  558. sub_st->priv_data = sub_pes;
  559. sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  560. sub_st->codec->codec_id = AV_CODEC_ID_AC3;
  561. sub_st->need_parsing = AVSTREAM_PARSE_FULL;
  562. sub_pes->sub_st = pes->sub_st = sub_st;
  563. }
  564. }
  565. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  566. mpegts_find_stream_type(st, pes->stream_type, MISC_types);
  567. if (st->codec->codec_id == AV_CODEC_ID_NONE){
  568. st->codec->codec_id = old_codec_id;
  569. st->codec->codec_type= old_codec_type;
  570. }
  571. return 0;
  572. }
  573. static void new_pes_packet(PESContext *pes, AVPacket *pkt)
  574. {
  575. av_init_packet(pkt);
  576. pkt->destruct = av_destruct_packet;
  577. pkt->data = pes->buffer;
  578. pkt->size = pes->data_index;
  579. if(pes->total_size != MAX_PES_PAYLOAD &&
  580. pes->pes_header_size + pes->data_index != pes->total_size + PES_START_SIZE) {
  581. av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
  582. pes->flags |= AV_PKT_FLAG_CORRUPT;
  583. }
  584. memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  585. // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
  586. if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
  587. pkt->stream_index = pes->sub_st->index;
  588. else
  589. pkt->stream_index = pes->st->index;
  590. pkt->pts = pes->pts;
  591. pkt->dts = pes->dts;
  592. /* store position of first TS packet of this PES packet */
  593. pkt->pos = pes->ts_packet_pos;
  594. pkt->flags = pes->flags;
  595. /* reset pts values */
  596. pes->pts = AV_NOPTS_VALUE;
  597. pes->dts = AV_NOPTS_VALUE;
  598. pes->buffer = NULL;
  599. pes->data_index = 0;
  600. pes->flags = 0;
  601. }
  602. static uint64_t get_bits64(GetBitContext *gb, int bits)
  603. {
  604. uint64_t ret = 0;
  605. if (get_bits_left(gb) < bits)
  606. return AV_NOPTS_VALUE;
  607. while (bits > 17) {
  608. ret <<= 17;
  609. ret |= get_bits(gb, 17);
  610. bits -= 17;
  611. }
  612. ret <<= bits;
  613. ret |= get_bits(gb, bits);
  614. return ret;
  615. }
  616. static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
  617. {
  618. GetBitContext gb;
  619. int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
  620. int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
  621. int dts_flag = -1, cts_flag = -1;
  622. int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
  623. init_get_bits(&gb, buf, buf_size*8);
  624. if (sl->use_au_start)
  625. au_start_flag = get_bits1(&gb);
  626. if (sl->use_au_end)
  627. au_end_flag = get_bits1(&gb);
  628. if (!sl->use_au_start && !sl->use_au_end)
  629. au_start_flag = au_end_flag = 1;
  630. if (sl->ocr_len > 0)
  631. ocr_flag = get_bits1(&gb);
  632. if (sl->use_idle)
  633. idle_flag = get_bits1(&gb);
  634. if (sl->use_padding)
  635. padding_flag = get_bits1(&gb);
  636. if (padding_flag)
  637. padding_bits = get_bits(&gb, 3);
  638. if (!idle_flag && (!padding_flag || padding_bits != 0)) {
  639. if (sl->packet_seq_num_len)
  640. skip_bits_long(&gb, sl->packet_seq_num_len);
  641. if (sl->degr_prior_len)
  642. if (get_bits1(&gb))
  643. skip_bits(&gb, sl->degr_prior_len);
  644. if (ocr_flag)
  645. skip_bits_long(&gb, sl->ocr_len);
  646. if (au_start_flag) {
  647. if (sl->use_rand_acc_pt)
  648. get_bits1(&gb);
  649. if (sl->au_seq_num_len > 0)
  650. skip_bits_long(&gb, sl->au_seq_num_len);
  651. if (sl->use_timestamps) {
  652. dts_flag = get_bits1(&gb);
  653. cts_flag = get_bits1(&gb);
  654. }
  655. }
  656. if (sl->inst_bitrate_len)
  657. inst_bitrate_flag = get_bits1(&gb);
  658. if (dts_flag == 1)
  659. dts = get_bits64(&gb, sl->timestamp_len);
  660. if (cts_flag == 1)
  661. cts = get_bits64(&gb, sl->timestamp_len);
  662. if (sl->au_len > 0)
  663. skip_bits_long(&gb, sl->au_len);
  664. if (inst_bitrate_flag)
  665. skip_bits_long(&gb, sl->inst_bitrate_len);
  666. }
  667. if (dts != AV_NOPTS_VALUE)
  668. pes->dts = dts;
  669. if (cts != AV_NOPTS_VALUE)
  670. pes->pts = cts;
  671. if (sl->timestamp_len && sl->timestamp_res)
  672. avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
  673. return (get_bits_count(&gb) + 7) >> 3;
  674. }
  675. /* return non zero if a packet could be constructed */
  676. static int mpegts_push_data(MpegTSFilter *filter,
  677. const uint8_t *buf, int buf_size, int is_start,
  678. int64_t pos)
  679. {
  680. PESContext *pes = filter->u.pes_filter.opaque;
  681. MpegTSContext *ts = pes->ts;
  682. const uint8_t *p;
  683. int len, code;
  684. if(!ts->pkt)
  685. return 0;
  686. if (is_start) {
  687. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  688. new_pes_packet(pes, ts->pkt);
  689. ts->stop_parse = 1;
  690. }
  691. pes->state = MPEGTS_HEADER;
  692. pes->data_index = 0;
  693. pes->ts_packet_pos = pos;
  694. }
  695. p = buf;
  696. while (buf_size > 0) {
  697. switch(pes->state) {
  698. case MPEGTS_HEADER:
  699. len = PES_START_SIZE - pes->data_index;
  700. if (len > buf_size)
  701. len = buf_size;
  702. memcpy(pes->header + pes->data_index, p, len);
  703. pes->data_index += len;
  704. p += len;
  705. buf_size -= len;
  706. if (pes->data_index == PES_START_SIZE) {
  707. /* we got all the PES or section header. We can now
  708. decide */
  709. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  710. pes->header[2] == 0x01) {
  711. /* it must be an mpeg2 PES stream */
  712. code = pes->header[3] | 0x100;
  713. av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
  714. if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
  715. (!pes->sub_st || pes->sub_st->discard == AVDISCARD_ALL)) ||
  716. code == 0x1be) /* padding_stream */
  717. goto skip;
  718. /* stream not present in PMT */
  719. if (!pes->st) {
  720. pes->st = avformat_new_stream(ts->stream, NULL);
  721. if (!pes->st)
  722. return AVERROR(ENOMEM);
  723. pes->st->id = pes->pid;
  724. mpegts_set_stream_info(pes->st, pes, 0, 0);
  725. }
  726. pes->total_size = AV_RB16(pes->header + 4);
  727. /* NOTE: a zero total size means the PES size is
  728. unbounded */
  729. if (!pes->total_size)
  730. pes->total_size = MAX_PES_PAYLOAD;
  731. /* allocate pes buffer */
  732. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  733. if (!pes->buffer)
  734. return AVERROR(ENOMEM);
  735. if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
  736. code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
  737. code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
  738. code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
  739. pes->state = MPEGTS_PESHEADER;
  740. if (pes->st->codec->codec_id == AV_CODEC_ID_NONE && !pes->st->request_probe) {
  741. av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
  742. pes->pid, pes->stream_type);
  743. pes->st->request_probe= 1;
  744. }
  745. } else {
  746. pes->state = MPEGTS_PAYLOAD;
  747. pes->data_index = 0;
  748. }
  749. } else {
  750. /* otherwise, it should be a table */
  751. /* skip packet */
  752. skip:
  753. pes->state = MPEGTS_SKIP;
  754. continue;
  755. }
  756. }
  757. break;
  758. /**********************************************/
  759. /* PES packing parsing */
  760. case MPEGTS_PESHEADER:
  761. len = PES_HEADER_SIZE - pes->data_index;
  762. if (len < 0)
  763. return -1;
  764. if (len > buf_size)
  765. len = buf_size;
  766. memcpy(pes->header + pes->data_index, p, len);
  767. pes->data_index += len;
  768. p += len;
  769. buf_size -= len;
  770. if (pes->data_index == PES_HEADER_SIZE) {
  771. pes->pes_header_size = pes->header[8] + 9;
  772. pes->state = MPEGTS_PESHEADER_FILL;
  773. }
  774. break;
  775. case MPEGTS_PESHEADER_FILL:
  776. len = pes->pes_header_size - pes->data_index;
  777. if (len < 0)
  778. return -1;
  779. if (len > buf_size)
  780. len = buf_size;
  781. memcpy(pes->header + pes->data_index, p, len);
  782. pes->data_index += len;
  783. p += len;
  784. buf_size -= len;
  785. if (pes->data_index == pes->pes_header_size) {
  786. const uint8_t *r;
  787. unsigned int flags, pes_ext, skip;
  788. flags = pes->header[7];
  789. r = pes->header + 9;
  790. pes->pts = AV_NOPTS_VALUE;
  791. pes->dts = AV_NOPTS_VALUE;
  792. if ((flags & 0xc0) == 0x80) {
  793. pes->dts = pes->pts = ff_parse_pes_pts(r);
  794. r += 5;
  795. } else if ((flags & 0xc0) == 0xc0) {
  796. pes->pts = ff_parse_pes_pts(r);
  797. r += 5;
  798. pes->dts = ff_parse_pes_pts(r);
  799. r += 5;
  800. }
  801. pes->extended_stream_id = -1;
  802. if (flags & 0x01) { /* PES extension */
  803. pes_ext = *r++;
  804. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  805. skip = (pes_ext >> 4) & 0xb;
  806. skip += skip & 0x9;
  807. r += skip;
  808. if ((pes_ext & 0x41) == 0x01 &&
  809. (r + 2) <= (pes->header + pes->pes_header_size)) {
  810. /* PES extension 2 */
  811. if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
  812. pes->extended_stream_id = r[1];
  813. }
  814. }
  815. /* we got the full header. We parse it and get the payload */
  816. pes->state = MPEGTS_PAYLOAD;
  817. pes->data_index = 0;
  818. if (pes->stream_type == 0x12 && buf_size > 0) {
  819. int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size);
  820. pes->pes_header_size += sl_header_bytes;
  821. p += sl_header_bytes;
  822. buf_size -= sl_header_bytes;
  823. }
  824. }
  825. break;
  826. case MPEGTS_PAYLOAD:
  827. if (buf_size > 0 && pes->buffer) {
  828. if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
  829. new_pes_packet(pes, ts->pkt);
  830. pes->total_size = MAX_PES_PAYLOAD;
  831. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  832. if (!pes->buffer)
  833. return AVERROR(ENOMEM);
  834. ts->stop_parse = 1;
  835. } else if (pes->data_index == 0 && buf_size > pes->total_size) {
  836. // pes packet size is < ts size packet and pes data is padded with 0xff
  837. // not sure if this is legal in ts but see issue #2392
  838. buf_size = pes->total_size;
  839. }
  840. memcpy(pes->buffer+pes->data_index, p, buf_size);
  841. pes->data_index += buf_size;
  842. }
  843. buf_size = 0;
  844. /* emit complete packets with known packet size
  845. * decreases demuxer delay for infrequent packets like subtitles from
  846. * a couple of seconds to milliseconds for properly muxed files.
  847. * total_size is the number of bytes following pes_packet_length
  848. * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
  849. if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
  850. pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
  851. ts->stop_parse = 1;
  852. new_pes_packet(pes, ts->pkt);
  853. }
  854. break;
  855. case MPEGTS_SKIP:
  856. buf_size = 0;
  857. break;
  858. }
  859. }
  860. return 0;
  861. }
  862. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
  863. {
  864. MpegTSFilter *tss;
  865. PESContext *pes;
  866. /* if no pid found, then add a pid context */
  867. pes = av_mallocz(sizeof(PESContext));
  868. if (!pes)
  869. return 0;
  870. pes->ts = ts;
  871. pes->stream = ts->stream;
  872. pes->pid = pid;
  873. pes->pcr_pid = pcr_pid;
  874. pes->state = MPEGTS_SKIP;
  875. pes->pts = AV_NOPTS_VALUE;
  876. pes->dts = AV_NOPTS_VALUE;
  877. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  878. if (!tss) {
  879. av_free(pes);
  880. return 0;
  881. }
  882. return pes;
  883. }
  884. #define MAX_LEVEL 4
  885. typedef struct {
  886. AVFormatContext *s;
  887. AVIOContext pb;
  888. Mp4Descr *descr;
  889. Mp4Descr *active_descr;
  890. int descr_count;
  891. int max_descr_count;
  892. int level;
  893. } MP4DescrParseContext;
  894. static int init_MP4DescrParseContext(
  895. MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf,
  896. unsigned size, Mp4Descr *descr, int max_descr_count)
  897. {
  898. int ret;
  899. if (size > (1<<30))
  900. return AVERROR_INVALIDDATA;
  901. if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0,
  902. NULL, NULL, NULL, NULL)) < 0)
  903. return ret;
  904. d->s = s;
  905. d->level = 0;
  906. d->descr_count = 0;
  907. d->descr = descr;
  908. d->active_descr = NULL;
  909. d->max_descr_count = max_descr_count;
  910. return 0;
  911. }
  912. static void update_offsets(AVIOContext *pb, int64_t *off, int *len) {
  913. int64_t new_off = avio_tell(pb);
  914. (*len) -= new_off - *off;
  915. *off = new_off;
  916. }
  917. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  918. int target_tag);
  919. static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
  920. {
  921. while (len > 0) {
  922. if (parse_mp4_descr(d, off, len, 0) < 0)
  923. return -1;
  924. update_offsets(&d->pb, &off, &len);
  925. }
  926. return 0;
  927. }
  928. static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  929. {
  930. avio_rb16(&d->pb); // ID
  931. avio_r8(&d->pb);
  932. avio_r8(&d->pb);
  933. avio_r8(&d->pb);
  934. avio_r8(&d->pb);
  935. avio_r8(&d->pb);
  936. update_offsets(&d->pb, &off, &len);
  937. return parse_mp4_descr_arr(d, off, len);
  938. }
  939. static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  940. {
  941. int id_flags;
  942. if (len < 2)
  943. return 0;
  944. id_flags = avio_rb16(&d->pb);
  945. if (!(id_flags & 0x0020)) { //URL_Flag
  946. update_offsets(&d->pb, &off, &len);
  947. return parse_mp4_descr_arr(d, off, len); //ES_Descriptor[]
  948. } else {
  949. return 0;
  950. }
  951. }
  952. static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  953. {
  954. int es_id = 0;
  955. if (d->descr_count >= d->max_descr_count)
  956. return -1;
  957. ff_mp4_parse_es_descr(&d->pb, &es_id);
  958. d->active_descr = d->descr + (d->descr_count++);
  959. d->active_descr->es_id = es_id;
  960. update_offsets(&d->pb, &off, &len);
  961. parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
  962. update_offsets(&d->pb, &off, &len);
  963. if (len > 0)
  964. parse_mp4_descr(d, off, len, MP4SLDescrTag);
  965. d->active_descr = NULL;
  966. return 0;
  967. }
  968. static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  969. {
  970. Mp4Descr *descr = d->active_descr;
  971. if (!descr)
  972. return -1;
  973. d->active_descr->dec_config_descr = av_malloc(len);
  974. if (!descr->dec_config_descr)
  975. return AVERROR(ENOMEM);
  976. descr->dec_config_descr_len = len;
  977. avio_read(&d->pb, descr->dec_config_descr, len);
  978. return 0;
  979. }
  980. static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  981. {
  982. Mp4Descr *descr = d->active_descr;
  983. int predefined;
  984. if (!descr)
  985. return -1;
  986. predefined = avio_r8(&d->pb);
  987. if (!predefined) {
  988. int lengths;
  989. int flags = avio_r8(&d->pb);
  990. descr->sl.use_au_start = !!(flags & 0x80);
  991. descr->sl.use_au_end = !!(flags & 0x40);
  992. descr->sl.use_rand_acc_pt = !!(flags & 0x20);
  993. descr->sl.use_padding = !!(flags & 0x08);
  994. descr->sl.use_timestamps = !!(flags & 0x04);
  995. descr->sl.use_idle = !!(flags & 0x02);
  996. descr->sl.timestamp_res = avio_rb32(&d->pb);
  997. avio_rb32(&d->pb);
  998. descr->sl.timestamp_len = avio_r8(&d->pb);
  999. descr->sl.ocr_len = avio_r8(&d->pb);
  1000. descr->sl.au_len = avio_r8(&d->pb);
  1001. descr->sl.inst_bitrate_len = avio_r8(&d->pb);
  1002. lengths = avio_rb16(&d->pb);
  1003. descr->sl.degr_prior_len = lengths >> 12;
  1004. descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
  1005. descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
  1006. } else {
  1007. av_log_missing_feature(d->s, "Predefined SLConfigDescriptor", 0);
  1008. }
  1009. return 0;
  1010. }
  1011. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  1012. int target_tag) {
  1013. int tag;
  1014. int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
  1015. update_offsets(&d->pb, &off, &len);
  1016. if (len < 0 || len1 > len || len1 <= 0) {
  1017. av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
  1018. return -1;
  1019. }
  1020. if (d->level++ >= MAX_LEVEL) {
  1021. av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
  1022. goto done;
  1023. }
  1024. if (target_tag && tag != target_tag) {
  1025. av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag);
  1026. goto done;
  1027. }
  1028. switch (tag) {
  1029. case MP4IODescrTag:
  1030. parse_MP4IODescrTag(d, off, len1);
  1031. break;
  1032. case MP4ODescrTag:
  1033. parse_MP4ODescrTag(d, off, len1);
  1034. break;
  1035. case MP4ESDescrTag:
  1036. parse_MP4ESDescrTag(d, off, len1);
  1037. break;
  1038. case MP4DecConfigDescrTag:
  1039. parse_MP4DecConfigDescrTag(d, off, len1);
  1040. break;
  1041. case MP4SLDescrTag:
  1042. parse_MP4SLDescrTag(d, off, len1);
  1043. break;
  1044. }
  1045. done:
  1046. d->level--;
  1047. avio_seek(&d->pb, off + len1, SEEK_SET);
  1048. return 0;
  1049. }
  1050. static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1051. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1052. {
  1053. MP4DescrParseContext d;
  1054. if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
  1055. return -1;
  1056. parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
  1057. *descr_count = d.descr_count;
  1058. return 0;
  1059. }
  1060. static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1061. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1062. {
  1063. MP4DescrParseContext d;
  1064. if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
  1065. return -1;
  1066. parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
  1067. *descr_count = d.descr_count;
  1068. return 0;
  1069. }
  1070. static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1071. {
  1072. MpegTSContext *ts = filter->u.section_filter.opaque;
  1073. SectionHeader h;
  1074. const uint8_t *p, *p_end;
  1075. AVIOContext pb;
  1076. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
  1077. int mp4_descr_count = 0;
  1078. int i, pid;
  1079. AVFormatContext *s = ts->stream;
  1080. p_end = section + section_len - 4;
  1081. p = section;
  1082. if (parse_section_header(&h, &p, p_end) < 0)
  1083. return;
  1084. if (h.tid != M4OD_TID)
  1085. return;
  1086. mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT);
  1087. for (pid = 0; pid < NB_PID_MAX; pid++) {
  1088. if (!ts->pids[pid])
  1089. continue;
  1090. for (i = 0; i < mp4_descr_count; i++) {
  1091. PESContext *pes;
  1092. AVStream *st;
  1093. if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
  1094. continue;
  1095. if (ts->pids[pid]->type != MPEGTS_PES) {
  1096. av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
  1097. continue;
  1098. }
  1099. pes = ts->pids[pid]->u.pes_filter.opaque;
  1100. st = pes->st;
  1101. if (!st) {
  1102. continue;
  1103. }
  1104. pes->sl = mp4_descr[i].sl;
  1105. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1106. mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1107. ff_mp4_read_dec_config_descr(s, st, &pb);
  1108. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1109. st->codec->extradata_size > 0)
  1110. st->need_parsing = 0;
  1111. if (st->codec->codec_id == AV_CODEC_ID_H264 &&
  1112. st->codec->extradata_size > 0)
  1113. st->need_parsing = 0;
  1114. if (st->codec->codec_id <= AV_CODEC_ID_NONE) {
  1115. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO) {
  1116. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1117. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE) {
  1118. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1119. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN) {
  1120. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1121. }
  1122. }
  1123. }
  1124. for (i = 0; i < mp4_descr_count; i++)
  1125. av_free(mp4_descr[i].dec_config_descr);
  1126. }
  1127. int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
  1128. const uint8_t **pp, const uint8_t *desc_list_end,
  1129. Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
  1130. MpegTSContext *ts)
  1131. {
  1132. const uint8_t *desc_end;
  1133. int desc_len, desc_tag, desc_es_id;
  1134. char language[252];
  1135. int i;
  1136. desc_tag = get8(pp, desc_list_end);
  1137. if (desc_tag < 0)
  1138. return -1;
  1139. desc_len = get8(pp, desc_list_end);
  1140. if (desc_len < 0)
  1141. return -1;
  1142. desc_end = *pp + desc_len;
  1143. if (desc_end > desc_list_end)
  1144. return -1;
  1145. av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
  1146. if (st->codec->codec_id == AV_CODEC_ID_NONE &&
  1147. stream_type == STREAM_TYPE_PRIVATE_DATA)
  1148. mpegts_find_stream_type(st, desc_tag, DESC_types);
  1149. switch(desc_tag) {
  1150. case 0x1E: /* SL descriptor */
  1151. desc_es_id = get16(pp, desc_end);
  1152. if (ts && ts->pids[pid])
  1153. ts->pids[pid]->es_id = desc_es_id;
  1154. for (i = 0; i < mp4_descr_count; i++)
  1155. if (mp4_descr[i].dec_config_descr_len &&
  1156. mp4_descr[i].es_id == desc_es_id) {
  1157. AVIOContext pb;
  1158. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1159. mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1160. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1161. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1162. st->codec->extradata_size > 0)
  1163. st->need_parsing = 0;
  1164. if (st->codec->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
  1165. mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
  1166. }
  1167. break;
  1168. case 0x1F: /* FMC descriptor */
  1169. get16(pp, desc_end);
  1170. if (mp4_descr_count > 0 && (st->codec->codec_id == AV_CODEC_ID_AAC_LATM || st->request_probe>0) &&
  1171. mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
  1172. AVIOContext pb;
  1173. ffio_init_context(&pb, mp4_descr->dec_config_descr,
  1174. mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1175. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1176. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1177. st->codec->extradata_size > 0){
  1178. st->request_probe= st->need_parsing = 0;
  1179. st->codec->codec_type= AVMEDIA_TYPE_AUDIO;
  1180. }
  1181. }
  1182. break;
  1183. case 0x56: /* DVB teletext descriptor */
  1184. language[0] = get8(pp, desc_end);
  1185. language[1] = get8(pp, desc_end);
  1186. language[2] = get8(pp, desc_end);
  1187. language[3] = 0;
  1188. av_dict_set(&st->metadata, "language", language, 0);
  1189. break;
  1190. case 0x59: /* subtitling descriptor */
  1191. language[0] = get8(pp, desc_end);
  1192. language[1] = get8(pp, desc_end);
  1193. language[2] = get8(pp, desc_end);
  1194. language[3] = 0;
  1195. /* hearing impaired subtitles detection */
  1196. switch(get8(pp, desc_end)) {
  1197. case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
  1198. case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
  1199. case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
  1200. case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
  1201. case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
  1202. case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
  1203. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  1204. break;
  1205. }
  1206. if (st->codec->extradata) {
  1207. if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
  1208. av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
  1209. } else {
  1210. st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  1211. if (st->codec->extradata) {
  1212. st->codec->extradata_size = 4;
  1213. memcpy(st->codec->extradata, *pp, 4);
  1214. }
  1215. }
  1216. *pp += 4;
  1217. av_dict_set(&st->metadata, "language", language, 0);
  1218. break;
  1219. case 0x0a: /* ISO 639 language descriptor */
  1220. for (i = 0; i + 4 <= desc_len; i += 4) {
  1221. language[i + 0] = get8(pp, desc_end);
  1222. language[i + 1] = get8(pp, desc_end);
  1223. language[i + 2] = get8(pp, desc_end);
  1224. language[i + 3] = ',';
  1225. switch (get8(pp, desc_end)) {
  1226. case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
  1227. case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
  1228. case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
  1229. }
  1230. }
  1231. if (i) {
  1232. language[i - 1] = 0;
  1233. av_dict_set(&st->metadata, "language", language, 0);
  1234. }
  1235. break;
  1236. case 0x05: /* registration descriptor */
  1237. st->codec->codec_tag = bytestream_get_le32(pp);
  1238. av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
  1239. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  1240. mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
  1241. break;
  1242. case 0x52: /* stream identifier descriptor */
  1243. st->stream_identifier = 1 + get8(pp, desc_end);
  1244. break;
  1245. default:
  1246. break;
  1247. }
  1248. *pp = desc_end;
  1249. return 0;
  1250. }
  1251. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1252. {
  1253. MpegTSContext *ts = filter->u.section_filter.opaque;
  1254. SectionHeader h1, *h = &h1;
  1255. PESContext *pes;
  1256. AVStream *st;
  1257. const uint8_t *p, *p_end, *desc_list_end;
  1258. int program_info_length, pcr_pid, pid, stream_type;
  1259. int desc_list_len;
  1260. uint32_t prog_reg_desc = 0; /* registration descriptor */
  1261. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
  1262. int mp4_descr_count = 0;
  1263. int i;
  1264. av_dlog(ts->stream, "PMT: len %i\n", section_len);
  1265. hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
  1266. p_end = section + section_len - 4;
  1267. p = section;
  1268. if (parse_section_header(h, &p, p_end) < 0)
  1269. return;
  1270. av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
  1271. h->id, h->sec_num, h->last_sec_num);
  1272. if (h->tid != PMT_TID)
  1273. return;
  1274. clear_program(ts, h->id);
  1275. pcr_pid = get16(&p, p_end);
  1276. if (pcr_pid < 0)
  1277. return;
  1278. pcr_pid &= 0x1fff;
  1279. add_pid_to_pmt(ts, h->id, pcr_pid);
  1280. set_pcr_pid(ts->stream, h->id, pcr_pid);
  1281. av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
  1282. program_info_length = get16(&p, p_end);
  1283. if (program_info_length < 0)
  1284. return;
  1285. program_info_length &= 0xfff;
  1286. while(program_info_length >= 2) {
  1287. uint8_t tag, len;
  1288. tag = get8(&p, p_end);
  1289. len = get8(&p, p_end);
  1290. av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
  1291. if(len > program_info_length - 2)
  1292. //something else is broken, exit the program_descriptors_loop
  1293. break;
  1294. program_info_length -= len + 2;
  1295. if (tag == 0x1d) { // IOD descriptor
  1296. get8(&p, p_end); // scope
  1297. get8(&p, p_end); // label
  1298. len -= 2;
  1299. mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
  1300. &mp4_descr_count, MAX_MP4_DESCR_COUNT);
  1301. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  1302. prog_reg_desc = bytestream_get_le32(&p);
  1303. len -= 4;
  1304. }
  1305. p += len;
  1306. }
  1307. p += program_info_length;
  1308. if (p >= p_end)
  1309. goto out;
  1310. // stop parsing after pmt, we found header
  1311. if (!ts->stream->nb_streams)
  1312. ts->stop_parse = 2;
  1313. for(;;) {
  1314. st = 0;
  1315. pes = NULL;
  1316. stream_type = get8(&p, p_end);
  1317. if (stream_type < 0)
  1318. break;
  1319. pid = get16(&p, p_end);
  1320. if (pid < 0)
  1321. break;
  1322. pid &= 0x1fff;
  1323. /* now create stream */
  1324. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  1325. pes = ts->pids[pid]->u.pes_filter.opaque;
  1326. if (!pes->st) {
  1327. pes->st = avformat_new_stream(pes->stream, NULL);
  1328. pes->st->id = pes->pid;
  1329. }
  1330. st = pes->st;
  1331. } else if (stream_type != 0x13) {
  1332. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  1333. pes = add_pes_stream(ts, pid, pcr_pid);
  1334. if (pes) {
  1335. st = avformat_new_stream(pes->stream, NULL);
  1336. st->id = pes->pid;
  1337. }
  1338. } else {
  1339. int idx = ff_find_stream_index(ts->stream, pid);
  1340. if (idx >= 0) {
  1341. st = ts->stream->streams[idx];
  1342. } else {
  1343. st = avformat_new_stream(ts->stream, NULL);
  1344. st->id = pid;
  1345. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1346. }
  1347. }
  1348. if (!st)
  1349. goto out;
  1350. if (pes && !pes->stream_type)
  1351. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  1352. add_pid_to_pmt(ts, h->id, pid);
  1353. ff_program_add_stream_index(ts->stream, h->id, st->index);
  1354. desc_list_len = get16(&p, p_end);
  1355. if (desc_list_len < 0)
  1356. break;
  1357. desc_list_len &= 0xfff;
  1358. desc_list_end = p + desc_list_len;
  1359. if (desc_list_end > p_end)
  1360. break;
  1361. for(;;) {
  1362. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
  1363. mp4_descr, mp4_descr_count, pid, ts) < 0)
  1364. break;
  1365. if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
  1366. ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
  1367. pes->sub_st->codec->codec_tag = st->codec->codec_tag;
  1368. }
  1369. }
  1370. p = desc_list_end;
  1371. }
  1372. out:
  1373. for (i = 0; i < mp4_descr_count; i++)
  1374. av_free(mp4_descr[i].dec_config_descr);
  1375. }
  1376. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1377. {
  1378. MpegTSContext *ts = filter->u.section_filter.opaque;
  1379. SectionHeader h1, *h = &h1;
  1380. const uint8_t *p, *p_end;
  1381. int sid, pmt_pid;
  1382. AVProgram *program;
  1383. av_dlog(ts->stream, "PAT:\n");
  1384. hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
  1385. p_end = section + section_len - 4;
  1386. p = section;
  1387. if (parse_section_header(h, &p, p_end) < 0)
  1388. return;
  1389. if (h->tid != PAT_TID)
  1390. return;
  1391. ts->stream->ts_id = h->id;
  1392. clear_programs(ts);
  1393. for(;;) {
  1394. sid = get16(&p, p_end);
  1395. if (sid < 0)
  1396. break;
  1397. pmt_pid = get16(&p, p_end);
  1398. if (pmt_pid < 0)
  1399. break;
  1400. pmt_pid &= 0x1fff;
  1401. av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1402. if (sid == 0x0000) {
  1403. /* NIT info */
  1404. } else {
  1405. MpegTSFilter *fil = ts->pids[pmt_pid];
  1406. program = av_new_program(ts->stream, sid);
  1407. program->program_num = sid;
  1408. program->pmt_pid = pmt_pid;
  1409. if (fil)
  1410. if ( fil->type != MPEGTS_SECTION
  1411. || fil->pid != pmt_pid
  1412. || fil->u.section_filter.section_cb != pmt_cb)
  1413. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1414. if (!ts->pids[pmt_pid])
  1415. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1416. add_pat_entry(ts, sid);
  1417. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  1418. add_pid_to_pmt(ts, sid, pmt_pid);
  1419. }
  1420. }
  1421. }
  1422. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1423. {
  1424. MpegTSContext *ts = filter->u.section_filter.opaque;
  1425. SectionHeader h1, *h = &h1;
  1426. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1427. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1428. char *name, *provider_name;
  1429. av_dlog(ts->stream, "SDT:\n");
  1430. hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
  1431. p_end = section + section_len - 4;
  1432. p = section;
  1433. if (parse_section_header(h, &p, p_end) < 0)
  1434. return;
  1435. if (h->tid != SDT_TID)
  1436. return;
  1437. onid = get16(&p, p_end);
  1438. if (onid < 0)
  1439. return;
  1440. val = get8(&p, p_end);
  1441. if (val < 0)
  1442. return;
  1443. for(;;) {
  1444. sid = get16(&p, p_end);
  1445. if (sid < 0)
  1446. break;
  1447. val = get8(&p, p_end);
  1448. if (val < 0)
  1449. break;
  1450. desc_list_len = get16(&p, p_end);
  1451. if (desc_list_len < 0)
  1452. break;
  1453. desc_list_len &= 0xfff;
  1454. desc_list_end = p + desc_list_len;
  1455. if (desc_list_end > p_end)
  1456. break;
  1457. for(;;) {
  1458. desc_tag = get8(&p, desc_list_end);
  1459. if (desc_tag < 0)
  1460. break;
  1461. desc_len = get8(&p, desc_list_end);
  1462. desc_end = p + desc_len;
  1463. if (desc_end > desc_list_end)
  1464. break;
  1465. av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
  1466. desc_tag, desc_len);
  1467. switch(desc_tag) {
  1468. case 0x48:
  1469. service_type = get8(&p, p_end);
  1470. if (service_type < 0)
  1471. break;
  1472. provider_name = getstr8(&p, p_end);
  1473. if (!provider_name)
  1474. break;
  1475. name = getstr8(&p, p_end);
  1476. if (name) {
  1477. AVProgram *program = av_new_program(ts->stream, sid);
  1478. if(program) {
  1479. av_dict_set(&program->metadata, "service_name", name, 0);
  1480. av_dict_set(&program->metadata, "service_provider", provider_name, 0);
  1481. }
  1482. }
  1483. av_free(name);
  1484. av_free(provider_name);
  1485. break;
  1486. default:
  1487. break;
  1488. }
  1489. p = desc_end;
  1490. }
  1491. p = desc_list_end;
  1492. }
  1493. }
  1494. /* handle one TS packet */
  1495. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1496. {
  1497. AVFormatContext *s = ts->stream;
  1498. MpegTSFilter *tss;
  1499. int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
  1500. has_adaptation, has_payload;
  1501. const uint8_t *p, *p_end;
  1502. int64_t pos;
  1503. pid = AV_RB16(packet + 1) & 0x1fff;
  1504. if(pid && discard_pid(ts, pid))
  1505. return 0;
  1506. is_start = packet[1] & 0x40;
  1507. tss = ts->pids[pid];
  1508. if (ts->auto_guess && tss == NULL && is_start) {
  1509. add_pes_stream(ts, pid, -1);
  1510. tss = ts->pids[pid];
  1511. }
  1512. if (!tss)
  1513. return 0;
  1514. afc = (packet[3] >> 4) & 3;
  1515. if (afc == 0) /* reserved value */
  1516. return 0;
  1517. has_adaptation = afc & 2;
  1518. has_payload = afc & 1;
  1519. is_discontinuity = has_adaptation
  1520. && packet[4] != 0 /* with length > 0 */
  1521. && (packet[5] & 0x80); /* and discontinuity indicated */
  1522. /* continuity check (currently not used) */
  1523. cc = (packet[3] & 0xf);
  1524. expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
  1525. cc_ok = pid == 0x1FFF // null packet PID
  1526. || is_discontinuity
  1527. || tss->last_cc < 0
  1528. || expected_cc == cc;
  1529. tss->last_cc = cc;
  1530. if (!cc_ok) {
  1531. av_log(ts->stream, AV_LOG_DEBUG,
  1532. "Continuity check failed for pid %d expected %d got %d\n",
  1533. pid, expected_cc, cc);
  1534. if(tss->type == MPEGTS_PES) {
  1535. PESContext *pc = tss->u.pes_filter.opaque;
  1536. pc->flags |= AV_PKT_FLAG_CORRUPT;
  1537. }
  1538. }
  1539. if (!has_payload)
  1540. return 0;
  1541. p = packet + 4;
  1542. if (has_adaptation) {
  1543. /* skip adaptation field */
  1544. p += p[0] + 1;
  1545. }
  1546. /* if past the end of packet, ignore */
  1547. p_end = packet + TS_PACKET_SIZE;
  1548. if (p >= p_end)
  1549. return 0;
  1550. pos = avio_tell(ts->stream->pb);
  1551. ts->pos47= pos % ts->raw_packet_size;
  1552. if (tss->type == MPEGTS_SECTION) {
  1553. if (is_start) {
  1554. /* pointer field present */
  1555. len = *p++;
  1556. if (p + len > p_end)
  1557. return 0;
  1558. if (len && cc_ok) {
  1559. /* write remaining section bytes */
  1560. write_section_data(s, tss,
  1561. p, len, 0);
  1562. /* check whether filter has been closed */
  1563. if (!ts->pids[pid])
  1564. return 0;
  1565. }
  1566. p += len;
  1567. if (p < p_end) {
  1568. write_section_data(s, tss,
  1569. p, p_end - p, 1);
  1570. }
  1571. } else {
  1572. if (cc_ok) {
  1573. write_section_data(s, tss,
  1574. p, p_end - p, 0);
  1575. }
  1576. }
  1577. } else {
  1578. int ret;
  1579. // Note: The position here points actually behind the current packet.
  1580. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1581. pos - ts->raw_packet_size)) < 0)
  1582. return ret;
  1583. }
  1584. return 0;
  1585. }
  1586. /* XXX: try to find a better synchro over several packets (use
  1587. get_packet_size() ?) */
  1588. static int mpegts_resync(AVFormatContext *s)
  1589. {
  1590. AVIOContext *pb = s->pb;
  1591. int c, i;
  1592. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1593. c = avio_r8(pb);
  1594. if (url_feof(pb))
  1595. return -1;
  1596. if (c == 0x47) {
  1597. avio_seek(pb, -1, SEEK_CUR);
  1598. return 0;
  1599. }
  1600. }
  1601. av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
  1602. /* no sync found */
  1603. return -1;
  1604. }
  1605. /* return -1 if error or EOF. Return 0 if OK. */
  1606. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
  1607. {
  1608. AVIOContext *pb = s->pb;
  1609. int skip, len;
  1610. for(;;) {
  1611. len = avio_read(pb, buf, TS_PACKET_SIZE);
  1612. if (len != TS_PACKET_SIZE)
  1613. return len < 0 ? len : AVERROR_EOF;
  1614. /* check packet sync byte */
  1615. if (buf[0] != 0x47) {
  1616. /* find a new packet start */
  1617. avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1618. if (mpegts_resync(s) < 0)
  1619. return AVERROR(EAGAIN);
  1620. else
  1621. continue;
  1622. } else {
  1623. skip = raw_packet_size - TS_PACKET_SIZE;
  1624. if (skip > 0)
  1625. avio_skip(pb, skip);
  1626. break;
  1627. }
  1628. }
  1629. return 0;
  1630. }
  1631. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1632. {
  1633. AVFormatContext *s = ts->stream;
  1634. uint8_t packet[TS_PACKET_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  1635. int packet_num, ret = 0;
  1636. if (avio_tell(s->pb) != ts->last_pos) {
  1637. int i;
  1638. av_dlog(ts->stream, "Skipping after seek\n");
  1639. /* seek detected, flush pes buffer */
  1640. for (i = 0; i < NB_PID_MAX; i++) {
  1641. if (ts->pids[i]) {
  1642. if (ts->pids[i]->type == MPEGTS_PES) {
  1643. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1644. av_freep(&pes->buffer);
  1645. pes->data_index = 0;
  1646. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1647. }
  1648. ts->pids[i]->last_cc = -1;
  1649. }
  1650. }
  1651. }
  1652. ts->stop_parse = 0;
  1653. packet_num = 0;
  1654. memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  1655. for(;;) {
  1656. packet_num++;
  1657. if (nb_packets != 0 && packet_num >= nb_packets ||
  1658. ts->stop_parse > 1) {
  1659. ret = AVERROR(EAGAIN);
  1660. break;
  1661. }
  1662. if (ts->stop_parse > 0)
  1663. break;
  1664. ret = read_packet(s, packet, ts->raw_packet_size);
  1665. if (ret != 0)
  1666. break;
  1667. ret = handle_packet(ts, packet);
  1668. if (ret != 0)
  1669. break;
  1670. }
  1671. ts->last_pos = avio_tell(s->pb);
  1672. return ret;
  1673. }
  1674. static int mpegts_probe(AVProbeData *p)
  1675. {
  1676. const int size= p->buf_size;
  1677. int score, fec_score, dvhs_score;
  1678. int check_count= size / TS_FEC_PACKET_SIZE;
  1679. #define CHECK_COUNT 10
  1680. if (check_count < CHECK_COUNT)
  1681. return -1;
  1682. score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1683. dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
  1684. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1685. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1686. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1687. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1688. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1689. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1690. else return -1;
  1691. }
  1692. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1693. (-1) if not available */
  1694. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1695. const uint8_t *packet)
  1696. {
  1697. int afc, len, flags;
  1698. const uint8_t *p;
  1699. unsigned int v;
  1700. afc = (packet[3] >> 4) & 3;
  1701. if (afc <= 1)
  1702. return -1;
  1703. p = packet + 4;
  1704. len = p[0];
  1705. p++;
  1706. if (len == 0)
  1707. return -1;
  1708. flags = *p++;
  1709. len--;
  1710. if (!(flags & 0x10))
  1711. return -1;
  1712. if (len < 6)
  1713. return -1;
  1714. v = AV_RB32(p);
  1715. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1716. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1717. return 0;
  1718. }
  1719. static int mpegts_read_header(AVFormatContext *s)
  1720. {
  1721. MpegTSContext *ts = s->priv_data;
  1722. AVIOContext *pb = s->pb;
  1723. uint8_t buf[8*1024]={0};
  1724. int len;
  1725. int64_t pos;
  1726. /* read the first 8192 bytes to get packet size */
  1727. pos = avio_tell(pb);
  1728. len = avio_read(pb, buf, sizeof(buf));
  1729. ts->raw_packet_size = get_packet_size(buf, len);
  1730. if (ts->raw_packet_size <= 0) {
  1731. av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
  1732. ts->raw_packet_size = TS_PACKET_SIZE;
  1733. }
  1734. ts->stream = s;
  1735. ts->auto_guess = 0;
  1736. if (s->iformat == &ff_mpegts_demuxer) {
  1737. /* normal demux */
  1738. /* first do a scan to get all the services */
  1739. /* NOTE: We attempt to seek on non-seekable files as well, as the
  1740. * probe buffer usually is big enough. Only warn if the seek failed
  1741. * on files where the seek should work. */
  1742. if (avio_seek(pb, pos, SEEK_SET) < 0)
  1743. av_log(s, pb->seekable ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
  1744. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1745. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1746. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1747. /* if could not find service, enable auto_guess */
  1748. ts->auto_guess = 1;
  1749. av_dlog(ts->stream, "tuning done\n");
  1750. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1751. } else {
  1752. AVStream *st;
  1753. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1754. int64_t pcrs[2], pcr_h;
  1755. int packet_count[2];
  1756. uint8_t packet[TS_PACKET_SIZE];
  1757. /* only read packets */
  1758. st = avformat_new_stream(s, NULL);
  1759. if (!st)
  1760. goto fail;
  1761. avpriv_set_pts_info(st, 60, 1, 27000000);
  1762. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1763. st->codec->codec_id = AV_CODEC_ID_MPEG2TS;
  1764. /* we iterate until we find two PCRs to estimate the bitrate */
  1765. pcr_pid = -1;
  1766. nb_pcrs = 0;
  1767. nb_packets = 0;
  1768. for(;;) {
  1769. ret = read_packet(s, packet, ts->raw_packet_size);
  1770. if (ret < 0)
  1771. return -1;
  1772. pid = AV_RB16(packet + 1) & 0x1fff;
  1773. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1774. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1775. pcr_pid = pid;
  1776. packet_count[nb_pcrs] = nb_packets;
  1777. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1778. nb_pcrs++;
  1779. if (nb_pcrs >= 2)
  1780. break;
  1781. }
  1782. nb_packets++;
  1783. }
  1784. /* NOTE1: the bitrate is computed without the FEC */
  1785. /* NOTE2: it is only the bitrate of the start of the stream */
  1786. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1787. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1788. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1789. st->codec->bit_rate = s->bit_rate;
  1790. st->start_time = ts->cur_pcr;
  1791. av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
  1792. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1793. }
  1794. avio_seek(pb, pos, SEEK_SET);
  1795. return 0;
  1796. fail:
  1797. return -1;
  1798. }
  1799. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1800. static int mpegts_raw_read_packet(AVFormatContext *s,
  1801. AVPacket *pkt)
  1802. {
  1803. MpegTSContext *ts = s->priv_data;
  1804. int ret, i;
  1805. int64_t pcr_h, next_pcr_h, pos;
  1806. int pcr_l, next_pcr_l;
  1807. uint8_t pcr_buf[12];
  1808. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1809. return AVERROR(ENOMEM);
  1810. pkt->pos= avio_tell(s->pb);
  1811. ret = read_packet(s, pkt->data, ts->raw_packet_size);
  1812. if (ret < 0) {
  1813. av_free_packet(pkt);
  1814. return ret;
  1815. }
  1816. if (ts->mpeg2ts_compute_pcr) {
  1817. /* compute exact PCR for each packet */
  1818. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1819. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1820. pos = avio_tell(s->pb);
  1821. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1822. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1823. avio_read(s->pb, pcr_buf, 12);
  1824. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1825. /* XXX: not precise enough */
  1826. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1827. (i + 1);
  1828. break;
  1829. }
  1830. }
  1831. avio_seek(s->pb, pos, SEEK_SET);
  1832. /* no next PCR found: we use previous increment */
  1833. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1834. }
  1835. pkt->pts = ts->cur_pcr;
  1836. pkt->duration = ts->pcr_incr;
  1837. ts->cur_pcr += ts->pcr_incr;
  1838. }
  1839. pkt->stream_index = 0;
  1840. return 0;
  1841. }
  1842. static int mpegts_read_packet(AVFormatContext *s,
  1843. AVPacket *pkt)
  1844. {
  1845. MpegTSContext *ts = s->priv_data;
  1846. int ret, i;
  1847. pkt->size = -1;
  1848. ts->pkt = pkt;
  1849. ret = handle_packets(ts, 0);
  1850. if (ret < 0) {
  1851. av_free_packet(ts->pkt);
  1852. /* flush pes data left */
  1853. for (i = 0; i < NB_PID_MAX; i++) {
  1854. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1855. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1856. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  1857. new_pes_packet(pes, pkt);
  1858. pes->state = MPEGTS_SKIP;
  1859. ret = 0;
  1860. break;
  1861. }
  1862. }
  1863. }
  1864. }
  1865. if (!ret && pkt->size < 0)
  1866. ret = AVERROR(EINTR);
  1867. return ret;
  1868. }
  1869. static int mpegts_read_close(AVFormatContext *s)
  1870. {
  1871. MpegTSContext *ts = s->priv_data;
  1872. int i;
  1873. clear_programs(ts);
  1874. for(i=0;i<NB_PID_MAX;i++)
  1875. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1876. return 0;
  1877. }
  1878. static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1879. int64_t *ppos, int64_t pos_limit)
  1880. {
  1881. MpegTSContext *ts = s->priv_data;
  1882. int64_t pos, timestamp;
  1883. uint8_t buf[TS_PACKET_SIZE];
  1884. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1885. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1886. while(pos < pos_limit) {
  1887. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  1888. return AV_NOPTS_VALUE;
  1889. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1890. return AV_NOPTS_VALUE;
  1891. if (buf[0] != 0x47) {
  1892. if (mpegts_resync(s) < 0)
  1893. return AV_NOPTS_VALUE;
  1894. pos = avio_tell(s->pb);
  1895. continue;
  1896. }
  1897. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1898. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1899. *ppos = pos;
  1900. return timestamp;
  1901. }
  1902. pos += ts->raw_packet_size;
  1903. }
  1904. return AV_NOPTS_VALUE;
  1905. }
  1906. static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
  1907. int64_t *ppos, int64_t pos_limit)
  1908. {
  1909. MpegTSContext *ts = s->priv_data;
  1910. int64_t pos;
  1911. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1912. ff_read_frame_flush(s);
  1913. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  1914. return AV_NOPTS_VALUE;
  1915. while(pos < pos_limit) {
  1916. int ret;
  1917. AVPacket pkt;
  1918. av_init_packet(&pkt);
  1919. ret= av_read_frame(s, &pkt);
  1920. if(ret < 0)
  1921. return AV_NOPTS_VALUE;
  1922. av_free_packet(&pkt);
  1923. if(pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0){
  1924. ff_reduce_index(s, pkt.stream_index);
  1925. av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
  1926. if(pkt.stream_index == stream_index){
  1927. *ppos= pkt.pos;
  1928. return pkt.dts;
  1929. }
  1930. }
  1931. pos = pkt.pos;
  1932. }
  1933. return AV_NOPTS_VALUE;
  1934. }
  1935. /**************************************************************/
  1936. /* parsing functions - called from other demuxers such as RTP */
  1937. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  1938. {
  1939. MpegTSContext *ts;
  1940. ts = av_mallocz(sizeof(MpegTSContext));
  1941. if (!ts)
  1942. return NULL;
  1943. /* no stream case, currently used by RTP */
  1944. ts->raw_packet_size = TS_PACKET_SIZE;
  1945. ts->stream = s;
  1946. ts->auto_guess = 1;
  1947. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1948. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1949. return ts;
  1950. }
  1951. /* return the consumed length if a packet was output, or -1 if no
  1952. packet is output */
  1953. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1954. const uint8_t *buf, int len)
  1955. {
  1956. int len1;
  1957. len1 = len;
  1958. ts->pkt = pkt;
  1959. for(;;) {
  1960. ts->stop_parse = 0;
  1961. if (len < TS_PACKET_SIZE)
  1962. return -1;
  1963. if (buf[0] != 0x47) {
  1964. buf++;
  1965. len--;
  1966. } else {
  1967. handle_packet(ts, buf);
  1968. buf += TS_PACKET_SIZE;
  1969. len -= TS_PACKET_SIZE;
  1970. if (ts->stop_parse == 1)
  1971. break;
  1972. }
  1973. }
  1974. return len1 - len;
  1975. }
  1976. void ff_mpegts_parse_close(MpegTSContext *ts)
  1977. {
  1978. int i;
  1979. for(i=0;i<NB_PID_MAX;i++)
  1980. av_free(ts->pids[i]);
  1981. av_free(ts);
  1982. }
  1983. AVInputFormat ff_mpegts_demuxer = {
  1984. .name = "mpegts",
  1985. .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
  1986. .priv_data_size = sizeof(MpegTSContext),
  1987. .read_probe = mpegts_probe,
  1988. .read_header = mpegts_read_header,
  1989. .read_packet = mpegts_read_packet,
  1990. .read_close = mpegts_read_close,
  1991. .read_timestamp = mpegts_get_dts,
  1992. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  1993. };
  1994. AVInputFormat ff_mpegtsraw_demuxer = {
  1995. .name = "mpegtsraw",
  1996. .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
  1997. .priv_data_size = sizeof(MpegTSContext),
  1998. .read_header = mpegts_read_header,
  1999. .read_packet = mpegts_raw_read_packet,
  2000. .read_close = mpegts_read_close,
  2001. .read_timestamp = mpegts_get_dts,
  2002. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  2003. .priv_class = &mpegtsraw_class,
  2004. };