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.

628 lines
19KB

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