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