mpeg.c 19 KB

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