mpegts.c 74 KB

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