You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

621 lines
19KB

  1. /*
  2. * MPEG1/2 demuxer
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "internal.h"
  23. #include "mpeg.h"
  24. #undef NDEBUG
  25. #include <assert.h>
  26. /*********************************************/
  27. /* demux code */
  28. #define MAX_SYNC_SIZE 100000
  29. static int check_pes(uint8_t *p, uint8_t *end){
  30. int pes1;
  31. int pes2= (p[3] & 0xC0) == 0x80
  32. && (p[4] & 0xC0) != 0x40
  33. &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
  34. for(p+=3; p<end && *p == 0xFF; p++);
  35. if((*p&0xC0) == 0x40) p+=2;
  36. if((*p&0xF0) == 0x20){
  37. pes1= p[0]&p[2]&p[4]&1;
  38. }else if((*p&0xF0) == 0x30){
  39. pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
  40. }else
  41. pes1 = *p == 0x0F;
  42. return pes1||pes2;
  43. }
  44. static int check_pack_header(const uint8_t *buf) {
  45. return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
  46. }
  47. static int mpegps_probe(AVProbeData *p)
  48. {
  49. uint32_t code= -1;
  50. int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
  51. int i;
  52. int score=0;
  53. for(i=0; i<p->buf_size; i++){
  54. code = (code<<8) + p->buf[i];
  55. if ((code & 0xffffff00) == 0x100) {
  56. int len= p->buf[i+1] << 8 | p->buf[i+2];
  57. int pes= check_pes(p->buf+i, p->buf+p->buf_size);
  58. int pack = check_pack_header(p->buf+i);
  59. if(code == SYSTEM_HEADER_START_CODE) sys++;
  60. else if(code == PACK_START_CODE && pack) pspack++;
  61. else if((code & 0xf0) == VIDEO_ID && pes) vid++;
  62. // skip pes payload to avoid start code emulation for private
  63. // and audio streams
  64. else if((code & 0xe0) == AUDIO_ID && pes) {audio++; i+=len;}
  65. else if(code == PRIVATE_STREAM_1 && pes) {priv1++; i+=len;}
  66. else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
  67. else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
  68. else if(code == PRIVATE_STREAM_1 && !pes) invalid++;
  69. }
  70. }
  71. if(vid+audio > invalid) /* invalid VDR files nd short PES streams */
  72. score= AVPROBE_SCORE_MAX/4;
  73. //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, invalid, p->buf_size);
  74. if(sys>invalid && sys*9 <= pspack*10)
  75. return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
  76. if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
  77. return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
  78. if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
  79. return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
  80. //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
  81. //mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
  82. return score;
  83. }
  84. typedef struct MpegDemuxContext {
  85. int32_t header_state;
  86. unsigned char psm_es_type[256];
  87. int sofdec;
  88. } MpegDemuxContext;
  89. static int mpegps_read_header(AVFormatContext *s,
  90. AVFormatParameters *ap)
  91. {
  92. MpegDemuxContext *m = s->priv_data;
  93. const char *sofdec = "Sofdec";
  94. int v, i = 0;
  95. m->header_state = 0xff;
  96. s->ctx_flags |= AVFMTCTX_NOHEADER;
  97. m->sofdec = -1;
  98. do {
  99. v = avio_r8(s->pb);
  100. m->header_state = m->header_state << 8 | v;
  101. m->sofdec++;
  102. } while (v == sofdec[i] && i++ < 6);
  103. m->sofdec = (m->sofdec == 6) ? 1 : 0;
  104. /* no need to do more */
  105. return 0;
  106. }
  107. static int64_t get_pts(AVIOContext *pb, int c)
  108. {
  109. uint8_t buf[5];
  110. buf[0] = c<0 ? avio_r8(pb) : c;
  111. avio_read(pb, buf+1, 4);
  112. return ff_parse_pes_pts(buf);
  113. }
  114. static int find_next_start_code(AVIOContext *pb, int *size_ptr,
  115. int32_t *header_state)
  116. {
  117. unsigned int state, v;
  118. int val, n;
  119. state = *header_state;
  120. n = *size_ptr;
  121. while (n > 0) {
  122. if (pb->eof_reached)
  123. break;
  124. v = avio_r8(pb);
  125. n--;
  126. if (state == 0x000001) {
  127. state = ((state << 8) | v) & 0xffffff;
  128. val = state;
  129. goto found;
  130. }
  131. state = ((state << 8) | v) & 0xffffff;
  132. }
  133. val = -1;
  134. found:
  135. *header_state = state;
  136. *size_ptr = n;
  137. return val;
  138. }
  139. #if 0 /* unused, remove? */
  140. /* XXX: optimize */
  141. static int find_prev_start_code(AVIOContext *pb, int *size_ptr)
  142. {
  143. int64_t pos, pos_start;
  144. int max_size, start_code;
  145. max_size = *size_ptr;
  146. pos_start = avio_tell(pb);
  147. /* in order to go faster, we fill the buffer */
  148. pos = pos_start - 16386;
  149. if (pos < 0)
  150. pos = 0;
  151. avio_seek(pb, pos, SEEK_SET);
  152. avio_r8(pb);
  153. pos = pos_start;
  154. for(;;) {
  155. pos--;
  156. if (pos < 0 || (pos_start - pos) >= max_size) {
  157. start_code = -1;
  158. goto the_end;
  159. }
  160. avio_seek(pb, pos, SEEK_SET);
  161. start_code = avio_rb32(pb);
  162. if ((start_code & 0xffffff00) == 0x100)
  163. break;
  164. }
  165. the_end:
  166. *size_ptr = pos_start - pos;
  167. return start_code;
  168. }
  169. #endif
  170. /**
  171. * Extract stream types from a program stream map
  172. * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
  173. *
  174. * @return number of bytes occupied by PSM in the bitstream
  175. */
  176. static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
  177. {
  178. int psm_length, ps_info_length, es_map_length;
  179. psm_length = avio_rb16(pb);
  180. avio_r8(pb);
  181. avio_r8(pb);
  182. ps_info_length = avio_rb16(pb);
  183. /* skip program_stream_info */
  184. avio_skip(pb, ps_info_length);
  185. es_map_length = avio_rb16(pb);
  186. /* at least one es available? */
  187. while (es_map_length >= 4){
  188. unsigned char type = avio_r8(pb);
  189. unsigned char es_id = avio_r8(pb);
  190. uint16_t es_info_length = avio_rb16(pb);
  191. /* remember mapping from stream id to stream type */
  192. m->psm_es_type[es_id] = type;
  193. /* skip program_stream_info */
  194. avio_skip(pb, es_info_length);
  195. es_map_length -= 4 + es_info_length;
  196. }
  197. avio_rb32(pb); /* crc32 */
  198. return 2 + psm_length;
  199. }
  200. /* read the next PES header. Return its position in ppos
  201. (if not NULL), and its start code, pts and dts.
  202. */
  203. static int mpegps_read_pes_header(AVFormatContext *s,
  204. int64_t *ppos, int *pstart_code,
  205. int64_t *ppts, int64_t *pdts)
  206. {
  207. MpegDemuxContext *m = s->priv_data;
  208. int len, size, startcode, c, flags, header_len;
  209. int pes_ext, ext2_len, id_ext, skip;
  210. int64_t pts, dts;
  211. int64_t last_sync= avio_tell(s->pb);
  212. error_redo:
  213. avio_seek(s->pb, last_sync, SEEK_SET);
  214. redo:
  215. /* next start code (should be immediately after) */
  216. m->header_state = 0xff;
  217. size = MAX_SYNC_SIZE;
  218. startcode = find_next_start_code(s->pb, &size, &m->header_state);
  219. last_sync = avio_tell(s->pb);
  220. //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, avio_tell(s->pb));
  221. if (startcode < 0){
  222. if(s->pb->eof_reached)
  223. return AVERROR_EOF;
  224. //FIXME we should remember header_state
  225. return AVERROR(EAGAIN);
  226. }
  227. if (startcode == PACK_START_CODE)
  228. goto redo;
  229. if (startcode == SYSTEM_HEADER_START_CODE)
  230. goto redo;
  231. if (startcode == PADDING_STREAM) {
  232. avio_skip(s->pb, avio_rb16(s->pb));
  233. goto redo;
  234. }
  235. if (startcode == PRIVATE_STREAM_2) {
  236. len = avio_rb16(s->pb);
  237. if (!m->sofdec) {
  238. while (len-- >= 6) {
  239. if (avio_r8(s->pb) == 'S') {
  240. uint8_t buf[5];
  241. avio_read(s->pb, buf, sizeof(buf));
  242. m->sofdec = !memcmp(buf, "ofdec", 5);
  243. len -= sizeof(buf);
  244. break;
  245. }
  246. }
  247. m->sofdec -= !m->sofdec;
  248. }
  249. avio_skip(s->pb, len);
  250. goto redo;
  251. }
  252. if (startcode == PROGRAM_STREAM_MAP) {
  253. mpegps_psm_parse(m, s->pb);
  254. goto redo;
  255. }
  256. /* find matching stream */
  257. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  258. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  259. (startcode == 0x1bd) || (startcode == 0x1fd)))
  260. goto redo;
  261. if (ppos) {
  262. *ppos = avio_tell(s->pb) - 4;
  263. }
  264. len = avio_rb16(s->pb);
  265. pts =
  266. dts = AV_NOPTS_VALUE;
  267. /* stuffing */
  268. for(;;) {
  269. if (len < 1)
  270. goto error_redo;
  271. c = avio_r8(s->pb);
  272. len--;
  273. /* XXX: for mpeg1, should test only bit 7 */
  274. if (c != 0xff)
  275. break;
  276. }
  277. if ((c & 0xc0) == 0x40) {
  278. /* buffer scale & size */
  279. avio_r8(s->pb);
  280. c = avio_r8(s->pb);
  281. len -= 2;
  282. }
  283. if ((c & 0xe0) == 0x20) {
  284. dts = pts = get_pts(s->pb, c);
  285. len -= 4;
  286. if (c & 0x10){
  287. dts = get_pts(s->pb, -1);
  288. len -= 5;
  289. }
  290. } else if ((c & 0xc0) == 0x80) {
  291. /* mpeg 2 PES */
  292. #if 0 /* some streams have this field set for no apparent reason */
  293. if ((c & 0x30) != 0) {
  294. /* Encrypted multiplex not handled */
  295. goto redo;
  296. }
  297. #endif
  298. flags = avio_r8(s->pb);
  299. header_len = avio_r8(s->pb);
  300. len -= 2;
  301. if (header_len > len)
  302. goto error_redo;
  303. len -= header_len;
  304. if (flags & 0x80) {
  305. dts = pts = get_pts(s->pb, -1);
  306. header_len -= 5;
  307. if (flags & 0x40) {
  308. dts = get_pts(s->pb, -1);
  309. header_len -= 5;
  310. }
  311. }
  312. if (flags & 0x3f && header_len == 0){
  313. flags &= 0xC0;
  314. av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
  315. }
  316. if (flags & 0x01) { /* PES extension */
  317. pes_ext = avio_r8(s->pb);
  318. header_len--;
  319. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  320. skip = (pes_ext >> 4) & 0xb;
  321. skip += skip & 0x9;
  322. if (pes_ext & 0x40 || skip > header_len){
  323. av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
  324. pes_ext=skip=0;
  325. }
  326. avio_skip(s->pb, skip);
  327. header_len -= skip;
  328. if (pes_ext & 0x01) { /* PES extension 2 */
  329. ext2_len = avio_r8(s->pb);
  330. header_len--;
  331. if ((ext2_len & 0x7f) > 0) {
  332. id_ext = avio_r8(s->pb);
  333. if ((id_ext & 0x80) == 0)
  334. startcode = ((startcode & 0xff) << 8) | id_ext;
  335. header_len--;
  336. }
  337. }
  338. }
  339. if(header_len < 0)
  340. goto error_redo;
  341. avio_skip(s->pb, header_len);
  342. }
  343. else if( c!= 0xf )
  344. goto redo;
  345. if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
  346. startcode = avio_r8(s->pb);
  347. len--;
  348. if (startcode >= 0x80 && startcode <= 0xcf) {
  349. /* audio: skip header */
  350. avio_r8(s->pb);
  351. avio_r8(s->pb);
  352. avio_r8(s->pb);
  353. len -= 3;
  354. if (startcode >= 0xb0 && startcode <= 0xbf) {
  355. /* MLP/TrueHD audio has a 4-byte header */
  356. avio_r8(s->pb);
  357. len--;
  358. }
  359. }
  360. }
  361. if(len<0)
  362. goto error_redo;
  363. if(dts != AV_NOPTS_VALUE && ppos){
  364. int i;
  365. for(i=0; i<s->nb_streams; i++){
  366. if(startcode == s->streams[i]->id &&
  367. s->pb->seekable /* index useless on streams anyway */) {
  368. ff_reduce_index(s, i);
  369. av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
  370. }
  371. }
  372. }
  373. *pstart_code = startcode;
  374. *ppts = pts;
  375. *pdts = dts;
  376. return len;
  377. }
  378. static int mpegps_read_packet(AVFormatContext *s,
  379. AVPacket *pkt)
  380. {
  381. MpegDemuxContext *m = s->priv_data;
  382. AVStream *st;
  383. int len, startcode, i, es_type, ret;
  384. enum CodecID codec_id = CODEC_ID_NONE;
  385. enum AVMediaType type;
  386. int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
  387. uint8_t av_uninit(dvdaudio_substream_type);
  388. redo:
  389. len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
  390. if (len < 0)
  391. return len;
  392. if(startcode == 0x1bd) {
  393. dvdaudio_substream_type = avio_r8(s->pb);
  394. avio_skip(s->pb, 3);
  395. len -= 4;
  396. }
  397. /* now find stream */
  398. for(i=0;i<s->nb_streams;i++) {
  399. st = s->streams[i];
  400. if (st->id == startcode)
  401. goto found;
  402. }
  403. es_type = m->psm_es_type[startcode & 0xff];
  404. if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
  405. if(es_type == STREAM_TYPE_VIDEO_MPEG1){
  406. codec_id = CODEC_ID_MPEG2VIDEO;
  407. type = AVMEDIA_TYPE_VIDEO;
  408. } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
  409. codec_id = CODEC_ID_MPEG2VIDEO;
  410. type = AVMEDIA_TYPE_VIDEO;
  411. } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
  412. es_type == STREAM_TYPE_AUDIO_MPEG2){
  413. codec_id = CODEC_ID_MP3;
  414. type = AVMEDIA_TYPE_AUDIO;
  415. } else if(es_type == STREAM_TYPE_AUDIO_AAC){
  416. codec_id = CODEC_ID_AAC;
  417. type = AVMEDIA_TYPE_AUDIO;
  418. } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
  419. codec_id = CODEC_ID_MPEG4;
  420. type = AVMEDIA_TYPE_VIDEO;
  421. } else if(es_type == STREAM_TYPE_VIDEO_H264){
  422. codec_id = CODEC_ID_H264;
  423. type = AVMEDIA_TYPE_VIDEO;
  424. } else if(es_type == STREAM_TYPE_AUDIO_AC3){
  425. codec_id = CODEC_ID_AC3;
  426. type = AVMEDIA_TYPE_AUDIO;
  427. } else {
  428. goto skip;
  429. }
  430. } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  431. static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
  432. unsigned char buf[8];
  433. avio_read(s->pb, buf, 8);
  434. avio_seek(s->pb, -8, SEEK_CUR);
  435. if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
  436. codec_id = CODEC_ID_CAVS;
  437. else
  438. codec_id = CODEC_ID_PROBE;
  439. type = AVMEDIA_TYPE_VIDEO;
  440. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  441. type = AVMEDIA_TYPE_AUDIO;
  442. codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
  443. } else if (startcode >= 0x80 && startcode <= 0x87) {
  444. type = AVMEDIA_TYPE_AUDIO;
  445. codec_id = CODEC_ID_AC3;
  446. } else if ( ( startcode >= 0x88 && startcode <= 0x8f)
  447. ||( startcode >= 0x98 && startcode <= 0x9f)) {
  448. /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
  449. type = AVMEDIA_TYPE_AUDIO;
  450. codec_id = CODEC_ID_DTS;
  451. } else if (startcode >= 0xa0 && startcode <= 0xaf) {
  452. type = AVMEDIA_TYPE_AUDIO;
  453. /* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
  454. codec_id = CODEC_ID_PCM_DVD;
  455. } else if (startcode >= 0xb0 && startcode <= 0xbf) {
  456. type = AVMEDIA_TYPE_AUDIO;
  457. codec_id = CODEC_ID_TRUEHD;
  458. } else if (startcode >= 0xc0 && startcode <= 0xcf) {
  459. /* Used for both AC-3 and E-AC-3 in EVOB files */
  460. type = AVMEDIA_TYPE_AUDIO;
  461. codec_id = CODEC_ID_AC3;
  462. } else if (startcode >= 0x20 && startcode <= 0x3f) {
  463. type = AVMEDIA_TYPE_SUBTITLE;
  464. codec_id = CODEC_ID_DVD_SUBTITLE;
  465. } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
  466. type = AVMEDIA_TYPE_VIDEO;
  467. codec_id = CODEC_ID_VC1;
  468. } else if (startcode == 0x1bd) {
  469. // check dvd audio substream type
  470. type = AVMEDIA_TYPE_AUDIO;
  471. switch(dvdaudio_substream_type & 0xe0) {
  472. case 0xa0: codec_id = CODEC_ID_PCM_DVD;
  473. break;
  474. case 0x80: if((dvdaudio_substream_type & 0xf8) == 0x88)
  475. codec_id = CODEC_ID_DTS;
  476. else codec_id = CODEC_ID_AC3;
  477. break;
  478. default: av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
  479. goto skip;
  480. }
  481. } else {
  482. skip:
  483. /* skip packet */
  484. avio_skip(s->pb, len);
  485. goto redo;
  486. }
  487. /* no stream found: add a new stream */
  488. st = avformat_new_stream(s, NULL);
  489. if (!st)
  490. goto skip;
  491. st->id = startcode;
  492. st->codec->codec_type = type;
  493. st->codec->codec_id = codec_id;
  494. if (codec_id != CODEC_ID_PCM_S16BE)
  495. st->need_parsing = AVSTREAM_PARSE_FULL;
  496. found:
  497. if(st->discard >= AVDISCARD_ALL)
  498. goto skip;
  499. if ((startcode >= 0xa0 && startcode <= 0xaf) ||
  500. (startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) {
  501. int b1, freq;
  502. /* for LPCM, we just skip the header and consider it is raw
  503. audio data */
  504. if (len <= 3)
  505. goto skip;
  506. avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
  507. b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
  508. avio_r8(s->pb); /* dynamic range control (0x80 = off) */
  509. len -= 3;
  510. freq = (b1 >> 4) & 3;
  511. st->codec->sample_rate = lpcm_freq_tab[freq];
  512. st->codec->channels = 1 + (b1 & 7);
  513. st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
  514. st->codec->bit_rate = st->codec->channels *
  515. st->codec->sample_rate *
  516. st->codec->bits_per_coded_sample;
  517. if (st->codec->bits_per_coded_sample == 16)
  518. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  519. else if (st->codec->bits_per_coded_sample == 28)
  520. return AVERROR(EINVAL);
  521. }
  522. ret = av_get_packet(s->pb, pkt, len);
  523. pkt->pts = pts;
  524. pkt->dts = dts;
  525. pkt->pos = dummy_pos;
  526. pkt->stream_index = st->index;
  527. av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
  528. pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
  529. pkt->size);
  530. return (ret < 0) ? ret : 0;
  531. }
  532. static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
  533. int64_t *ppos, int64_t pos_limit)
  534. {
  535. int len, startcode;
  536. int64_t pos, pts, dts;
  537. pos = *ppos;
  538. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  539. return AV_NOPTS_VALUE;
  540. for(;;) {
  541. len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
  542. if (len < 0) {
  543. av_dlog(s, "none (ret=%d)\n", len);
  544. return AV_NOPTS_VALUE;
  545. }
  546. if (startcode == s->streams[stream_index]->id &&
  547. dts != AV_NOPTS_VALUE) {
  548. break;
  549. }
  550. avio_skip(s->pb, len);
  551. }
  552. av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
  553. pos, dts, dts / 90000.0);
  554. *ppos = pos;
  555. return dts;
  556. }
  557. AVInputFormat ff_mpegps_demuxer = {
  558. .name = "mpeg",
  559. .long_name = NULL_IF_CONFIG_SMALL("MPEG-PS format"),
  560. .priv_data_size = sizeof(MpegDemuxContext),
  561. .read_probe = mpegps_probe,
  562. .read_header = mpegps_read_header,
  563. .read_packet = mpegps_read_packet,
  564. .read_seek = NULL, //mpegps_read_seek,
  565. .read_timestamp = mpegps_read_dts,
  566. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  567. };