mpeg.c 19 KB

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