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.

634 lines
20KB

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