mpegts.c 71 KB

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