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.

918 lines
30KB

  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. #if CONFIG_VOBSUB_DEMUXER
  25. # include "subtitles.h"
  26. # include "libavutil/bprint.h"
  27. #endif
  28. #undef NDEBUG
  29. #include <assert.h>
  30. #include "libavutil/avassert.h"
  31. /*********************************************/
  32. /* demux code */
  33. #define MAX_SYNC_SIZE 100000
  34. static int check_pes(const uint8_t *p, const uint8_t *end){
  35. int pes1;
  36. int pes2= (p[3] & 0xC0) == 0x80
  37. && (p[4] & 0xC0) != 0x40
  38. &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
  39. for(p+=3; p<end && *p == 0xFF; p++);
  40. if((*p&0xC0) == 0x40) p+=2;
  41. if((*p&0xF0) == 0x20){
  42. pes1= p[0]&p[2]&p[4]&1;
  43. }else if((*p&0xF0) == 0x30){
  44. pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
  45. }else
  46. pes1 = *p == 0x0F;
  47. return pes1||pes2;
  48. }
  49. static int check_pack_header(const uint8_t *buf) {
  50. return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
  51. }
  52. static int mpegps_probe(AVProbeData *p)
  53. {
  54. uint32_t code= -1;
  55. int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
  56. int i;
  57. int score=0;
  58. for(i=0; i<p->buf_size; i++){
  59. code = (code<<8) + p->buf[i];
  60. if ((code & 0xffffff00) == 0x100) {
  61. int len= p->buf[i+1] << 8 | p->buf[i+2];
  62. int pes= check_pes(p->buf+i, p->buf+p->buf_size);
  63. int pack = check_pack_header(p->buf+i);
  64. if(code == SYSTEM_HEADER_START_CODE) sys++;
  65. else if(code == PACK_START_CODE && pack) pspack++;
  66. else if((code & 0xf0) == VIDEO_ID && pes) vid++;
  67. // skip pes payload to avoid start code emulation for private
  68. // and audio streams
  69. else if((code & 0xe0) == AUDIO_ID && pes) {audio++; i+=len;}
  70. else if(code == PRIVATE_STREAM_1 && pes) {priv1++; i+=len;}
  71. else if(code == 0x1fd && pes) vid++; //VC1
  72. else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
  73. else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
  74. else if(code == PRIVATE_STREAM_1 && !pes) invalid++;
  75. }
  76. }
  77. if(vid+audio > invalid+1) /* invalid VDR files nd short PES streams */
  78. score = AVPROBE_SCORE_EXTENSION / 2;
  79. if(sys>invalid && sys*9 <= pspack*10)
  80. return (audio > 12 || vid > 3 || pspack > 2) ? AVPROBE_SCORE_EXTENSION + 2 : AVPROBE_SCORE_EXTENSION / 2; // 1 more than .mpg
  81. if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
  82. return pspack > 2 ? AVPROBE_SCORE_EXTENSION + 2 : AVPROBE_SCORE_EXTENSION / 2; // 1 more than .mpg
  83. if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
  84. return (audio > 12 || vid > 3 + 2*invalid) ? AVPROBE_SCORE_EXTENSION + 2 : AVPROBE_SCORE_EXTENSION / 2;
  85. //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
  86. //mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
  87. //Have\ Yourself\ a\ Merry\ Little\ Christmas.mp3 0 0 0 5 0 1 len:21618
  88. return score;
  89. }
  90. typedef struct MpegDemuxContext {
  91. int32_t header_state;
  92. unsigned char psm_es_type[256];
  93. int sofdec;
  94. int dvd;
  95. int imkh_cctv;
  96. #if CONFIG_VOBSUB_DEMUXER
  97. AVFormatContext *sub_ctx;
  98. FFDemuxSubtitlesQueue q;
  99. #endif
  100. } MpegDemuxContext;
  101. static int mpegps_read_header(AVFormatContext *s)
  102. {
  103. MpegDemuxContext *m = s->priv_data;
  104. char buffer[7];
  105. int64_t last_pos = avio_tell(s->pb);
  106. m->header_state = 0xff;
  107. s->ctx_flags |= AVFMTCTX_NOHEADER;
  108. avio_get_str(s->pb, 6, buffer, sizeof(buffer));
  109. if (!memcmp("IMKH", buffer, 4)) {
  110. m->imkh_cctv = 1;
  111. } else if (!memcmp("Sofdec", buffer, 6)) {
  112. m->sofdec = 1;
  113. } else
  114. avio_seek(s->pb, last_pos, SEEK_SET);
  115. /* no need to do more */
  116. return 0;
  117. }
  118. static int64_t get_pts(AVIOContext *pb, int c)
  119. {
  120. uint8_t buf[5];
  121. buf[0] = c<0 ? avio_r8(pb) : c;
  122. avio_read(pb, buf+1, 4);
  123. return ff_parse_pes_pts(buf);
  124. }
  125. static int find_next_start_code(AVIOContext *pb, int *size_ptr,
  126. int32_t *header_state)
  127. {
  128. unsigned int state, v;
  129. int val, n;
  130. state = *header_state;
  131. n = *size_ptr;
  132. while (n > 0) {
  133. if (url_feof(pb))
  134. break;
  135. v = avio_r8(pb);
  136. n--;
  137. if (state == 0x000001) {
  138. state = ((state << 8) | v) & 0xffffff;
  139. val = state;
  140. goto found;
  141. }
  142. state = ((state << 8) | v) & 0xffffff;
  143. }
  144. val = -1;
  145. found:
  146. *header_state = state;
  147. *size_ptr = n;
  148. return val;
  149. }
  150. /**
  151. * Extract stream types from a program stream map
  152. * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
  153. *
  154. * @return number of bytes occupied by PSM in the bitstream
  155. */
  156. static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
  157. {
  158. int psm_length, ps_info_length, es_map_length;
  159. psm_length = avio_rb16(pb);
  160. avio_r8(pb);
  161. avio_r8(pb);
  162. ps_info_length = avio_rb16(pb);
  163. /* skip program_stream_info */
  164. avio_skip(pb, ps_info_length);
  165. es_map_length = avio_rb16(pb);
  166. /* at least one es available? */
  167. while (es_map_length >= 4){
  168. unsigned char type = avio_r8(pb);
  169. unsigned char es_id = avio_r8(pb);
  170. uint16_t es_info_length = avio_rb16(pb);
  171. /* remember mapping from stream id to stream type */
  172. m->psm_es_type[es_id] = type;
  173. /* skip program_stream_info */
  174. avio_skip(pb, es_info_length);
  175. es_map_length -= 4 + es_info_length;
  176. }
  177. avio_rb32(pb); /* crc32 */
  178. return 2 + psm_length;
  179. }
  180. /* read the next PES header. Return its position in ppos
  181. (if not NULL), and its start code, pts and dts.
  182. */
  183. static int mpegps_read_pes_header(AVFormatContext *s,
  184. int64_t *ppos, int *pstart_code,
  185. int64_t *ppts, int64_t *pdts)
  186. {
  187. MpegDemuxContext *m = s->priv_data;
  188. int len, size, startcode, c, flags, header_len;
  189. int pes_ext, ext2_len, id_ext, skip;
  190. int64_t pts, dts;
  191. int64_t last_sync= avio_tell(s->pb);
  192. error_redo:
  193. avio_seek(s->pb, last_sync, SEEK_SET);
  194. redo:
  195. /* next start code (should be immediately after) */
  196. m->header_state = 0xff;
  197. size = MAX_SYNC_SIZE;
  198. startcode = find_next_start_code(s->pb, &size, &m->header_state);
  199. last_sync = avio_tell(s->pb);
  200. if (startcode < 0){
  201. if(url_feof(s->pb))
  202. return AVERROR_EOF;
  203. //FIXME we should remember header_state
  204. return AVERROR(EAGAIN);
  205. }
  206. if (startcode == PACK_START_CODE)
  207. goto redo;
  208. if (startcode == SYSTEM_HEADER_START_CODE)
  209. goto redo;
  210. if (startcode == PADDING_STREAM) {
  211. avio_skip(s->pb, avio_rb16(s->pb));
  212. goto redo;
  213. }
  214. if (startcode == PRIVATE_STREAM_2) {
  215. if (!m->sofdec) {
  216. /* Need to detect whether this from a DVD or a 'Sofdec' stream */
  217. int len = avio_rb16(s->pb);
  218. int bytesread = 0;
  219. uint8_t *ps2buf = av_malloc(len);
  220. if (ps2buf) {
  221. bytesread = avio_read(s->pb, ps2buf, len);
  222. if (bytesread != len) {
  223. avio_skip(s->pb, len - bytesread);
  224. } else {
  225. uint8_t *p = 0;
  226. if (len >= 6)
  227. p = memchr(ps2buf, 'S', len - 5);
  228. if (p)
  229. m->sofdec = !memcmp(p+1, "ofdec", 5);
  230. m->sofdec -= !m->sofdec;
  231. if (m->sofdec < 0) {
  232. if (len == 980 && ps2buf[0] == 0) {
  233. /* PCI structure? */
  234. uint32_t startpts = AV_RB32(ps2buf + 0x0d);
  235. uint32_t endpts = AV_RB32(ps2buf + 0x11);
  236. uint8_t hours = ((ps2buf[0x19] >> 4) * 10) + (ps2buf[0x19] & 0x0f);
  237. uint8_t mins = ((ps2buf[0x1a] >> 4) * 10) + (ps2buf[0x1a] & 0x0f);
  238. uint8_t secs = ((ps2buf[0x1b] >> 4) * 10) + (ps2buf[0x1b] & 0x0f);
  239. m->dvd = (hours <= 23 &&
  240. mins <= 59 &&
  241. secs <= 59 &&
  242. (ps2buf[0x19] & 0x0f) < 10 &&
  243. (ps2buf[0x1a] & 0x0f) < 10 &&
  244. (ps2buf[0x1b] & 0x0f) < 10 &&
  245. endpts >= startpts);
  246. } else if (len == 1018 && ps2buf[0] == 1) {
  247. /* DSI structure? */
  248. uint8_t hours = ((ps2buf[0x1d] >> 4) * 10) + (ps2buf[0x1d] & 0x0f);
  249. uint8_t mins = ((ps2buf[0x1e] >> 4) * 10) + (ps2buf[0x1e] & 0x0f);
  250. uint8_t secs = ((ps2buf[0x1f] >> 4) * 10) + (ps2buf[0x1f] & 0x0f);
  251. m->dvd = (hours <= 23 &&
  252. mins <= 59 &&
  253. secs <= 59 &&
  254. (ps2buf[0x1d] & 0x0f) < 10 &&
  255. (ps2buf[0x1e] & 0x0f) < 10 &&
  256. (ps2buf[0x1f] & 0x0f) < 10);
  257. }
  258. }
  259. }
  260. av_free(ps2buf);
  261. /* If this isn't a DVD packet or no memory
  262. * could be allocated, just ignore it.
  263. * If we did, move back to the start of the
  264. * packet (plus 'length' field) */
  265. if (!m->dvd || avio_skip(s->pb, -(len + 2)) < 0) {
  266. /* Skip back failed.
  267. * This packet will be lost but that can't be helped
  268. * if we can't skip back
  269. */
  270. goto redo;
  271. }
  272. } else {
  273. /* No memory */
  274. avio_skip(s->pb, len);
  275. goto redo;
  276. }
  277. } else if (!m->dvd) {
  278. int len = avio_rb16(s->pb);
  279. avio_skip(s->pb, len);
  280. goto redo;
  281. }
  282. }
  283. if (startcode == PROGRAM_STREAM_MAP) {
  284. mpegps_psm_parse(m, s->pb);
  285. goto redo;
  286. }
  287. /* find matching stream */
  288. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  289. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  290. (startcode == 0x1bd) ||
  291. (startcode == PRIVATE_STREAM_2) ||
  292. (startcode == 0x1fd)))
  293. goto redo;
  294. if (ppos) {
  295. *ppos = avio_tell(s->pb) - 4;
  296. }
  297. len = avio_rb16(s->pb);
  298. pts =
  299. dts = AV_NOPTS_VALUE;
  300. if (startcode != PRIVATE_STREAM_2)
  301. {
  302. /* stuffing */
  303. for(;;) {
  304. if (len < 1)
  305. goto error_redo;
  306. c = avio_r8(s->pb);
  307. len--;
  308. /* XXX: for mpeg1, should test only bit 7 */
  309. if (c != 0xff)
  310. break;
  311. }
  312. if ((c & 0xc0) == 0x40) {
  313. /* buffer scale & size */
  314. avio_r8(s->pb);
  315. c = avio_r8(s->pb);
  316. len -= 2;
  317. }
  318. if ((c & 0xe0) == 0x20) {
  319. dts = pts = get_pts(s->pb, c);
  320. len -= 4;
  321. if (c & 0x10){
  322. dts = get_pts(s->pb, -1);
  323. len -= 5;
  324. }
  325. } else if ((c & 0xc0) == 0x80) {
  326. /* mpeg 2 PES */
  327. flags = avio_r8(s->pb);
  328. header_len = avio_r8(s->pb);
  329. len -= 2;
  330. if (header_len > len)
  331. goto error_redo;
  332. len -= header_len;
  333. if (flags & 0x80) {
  334. dts = pts = get_pts(s->pb, -1);
  335. header_len -= 5;
  336. if (flags & 0x40) {
  337. dts = get_pts(s->pb, -1);
  338. header_len -= 5;
  339. }
  340. }
  341. if (flags & 0x3f && header_len == 0){
  342. flags &= 0xC0;
  343. av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
  344. }
  345. if (flags & 0x01) { /* PES extension */
  346. pes_ext = avio_r8(s->pb);
  347. header_len--;
  348. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  349. skip = (pes_ext >> 4) & 0xb;
  350. skip += skip & 0x9;
  351. if (pes_ext & 0x40 || skip > header_len){
  352. av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
  353. pes_ext=skip=0;
  354. }
  355. avio_skip(s->pb, skip);
  356. header_len -= skip;
  357. if (pes_ext & 0x01) { /* PES extension 2 */
  358. ext2_len = avio_r8(s->pb);
  359. header_len--;
  360. if ((ext2_len & 0x7f) > 0) {
  361. id_ext = avio_r8(s->pb);
  362. if ((id_ext & 0x80) == 0)
  363. startcode = ((startcode & 0xff) << 8) | id_ext;
  364. header_len--;
  365. }
  366. }
  367. }
  368. if(header_len < 0)
  369. goto error_redo;
  370. avio_skip(s->pb, header_len);
  371. }
  372. else if( c!= 0xf )
  373. goto redo;
  374. }
  375. if (startcode == PRIVATE_STREAM_1) {
  376. startcode = avio_r8(s->pb);
  377. len--;
  378. }
  379. if(len<0)
  380. goto error_redo;
  381. if(dts != AV_NOPTS_VALUE && ppos){
  382. int i;
  383. for(i=0; i<s->nb_streams; i++){
  384. if(startcode == s->streams[i]->id &&
  385. s->pb->seekable /* index useless on streams anyway */) {
  386. ff_reduce_index(s, i);
  387. av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
  388. }
  389. }
  390. }
  391. *pstart_code = startcode;
  392. *ppts = pts;
  393. *pdts = dts;
  394. return len;
  395. }
  396. static int mpegps_read_packet(AVFormatContext *s,
  397. AVPacket *pkt)
  398. {
  399. MpegDemuxContext *m = s->priv_data;
  400. AVStream *st;
  401. int len, startcode, i, es_type, ret;
  402. int lpcm_header_len = -1; //Init to supress warning
  403. int request_probe= 0;
  404. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  405. enum AVMediaType type;
  406. int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
  407. redo:
  408. len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
  409. if (len < 0)
  410. return len;
  411. if (startcode >= 0x80 && startcode <= 0xcf) {
  412. if(len < 4)
  413. goto skip;
  414. /* audio: skip header */
  415. avio_r8(s->pb);
  416. lpcm_header_len = avio_rb16(s->pb);
  417. len -= 3;
  418. if (startcode >= 0xb0 && startcode <= 0xbf) {
  419. /* MLP/TrueHD audio has a 4-byte header */
  420. avio_r8(s->pb);
  421. len--;
  422. }
  423. }
  424. /* now find stream */
  425. for(i=0;i<s->nb_streams;i++) {
  426. st = s->streams[i];
  427. if (st->id == startcode)
  428. goto found;
  429. }
  430. es_type = m->psm_es_type[startcode & 0xff];
  431. if(es_type == STREAM_TYPE_VIDEO_MPEG1){
  432. codec_id = AV_CODEC_ID_MPEG2VIDEO;
  433. type = AVMEDIA_TYPE_VIDEO;
  434. } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
  435. codec_id = AV_CODEC_ID_MPEG2VIDEO;
  436. type = AVMEDIA_TYPE_VIDEO;
  437. } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
  438. es_type == STREAM_TYPE_AUDIO_MPEG2){
  439. codec_id = AV_CODEC_ID_MP3;
  440. type = AVMEDIA_TYPE_AUDIO;
  441. } else if(es_type == STREAM_TYPE_AUDIO_AAC){
  442. codec_id = AV_CODEC_ID_AAC;
  443. type = AVMEDIA_TYPE_AUDIO;
  444. } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
  445. codec_id = AV_CODEC_ID_MPEG4;
  446. type = AVMEDIA_TYPE_VIDEO;
  447. } else if(es_type == STREAM_TYPE_VIDEO_H264){
  448. codec_id = AV_CODEC_ID_H264;
  449. type = AVMEDIA_TYPE_VIDEO;
  450. } else if(es_type == STREAM_TYPE_AUDIO_AC3){
  451. codec_id = AV_CODEC_ID_AC3;
  452. type = AVMEDIA_TYPE_AUDIO;
  453. } else if(m->imkh_cctv && es_type == 0x91){
  454. codec_id = AV_CODEC_ID_PCM_MULAW;
  455. type = AVMEDIA_TYPE_AUDIO;
  456. } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  457. static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
  458. unsigned char buf[8];
  459. avio_read(s->pb, buf, 8);
  460. avio_seek(s->pb, -8, SEEK_CUR);
  461. if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
  462. codec_id = AV_CODEC_ID_CAVS;
  463. else
  464. request_probe= 1;
  465. type = AVMEDIA_TYPE_VIDEO;
  466. } else if (startcode == PRIVATE_STREAM_2) {
  467. type = AVMEDIA_TYPE_DATA;
  468. codec_id = AV_CODEC_ID_DVD_NAV;
  469. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  470. type = AVMEDIA_TYPE_AUDIO;
  471. codec_id = m->sofdec > 0 ? AV_CODEC_ID_ADPCM_ADX : AV_CODEC_ID_MP2;
  472. } else if (startcode >= 0x80 && startcode <= 0x87) {
  473. type = AVMEDIA_TYPE_AUDIO;
  474. codec_id = AV_CODEC_ID_AC3;
  475. } else if ( ( startcode >= 0x88 && startcode <= 0x8f)
  476. ||( startcode >= 0x98 && startcode <= 0x9f)) {
  477. /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
  478. type = AVMEDIA_TYPE_AUDIO;
  479. codec_id = AV_CODEC_ID_DTS;
  480. } else if (startcode >= 0xa0 && startcode <= 0xaf) {
  481. type = AVMEDIA_TYPE_AUDIO;
  482. if(lpcm_header_len == 6) {
  483. codec_id = AV_CODEC_ID_MLP;
  484. } else {
  485. /* 16 bit form will be handled as AV_CODEC_ID_PCM_S16BE */
  486. codec_id = AV_CODEC_ID_PCM_DVD;
  487. }
  488. } else if (startcode >= 0xb0 && startcode <= 0xbf) {
  489. type = AVMEDIA_TYPE_AUDIO;
  490. codec_id = AV_CODEC_ID_TRUEHD;
  491. } else if (startcode >= 0xc0 && startcode <= 0xcf) {
  492. /* Used for both AC-3 and E-AC-3 in EVOB files */
  493. type = AVMEDIA_TYPE_AUDIO;
  494. codec_id = AV_CODEC_ID_AC3;
  495. } else if (startcode >= 0x20 && startcode <= 0x3f) {
  496. type = AVMEDIA_TYPE_SUBTITLE;
  497. codec_id = AV_CODEC_ID_DVD_SUBTITLE;
  498. } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
  499. type = AVMEDIA_TYPE_VIDEO;
  500. codec_id = AV_CODEC_ID_VC1;
  501. } else {
  502. skip:
  503. /* skip packet */
  504. avio_skip(s->pb, len);
  505. goto redo;
  506. }
  507. /* no stream found: add a new stream */
  508. st = avformat_new_stream(s, NULL);
  509. if (!st)
  510. goto skip;
  511. st->id = startcode;
  512. st->codec->codec_type = type;
  513. st->codec->codec_id = codec_id;
  514. if (st->codec->codec_id == AV_CODEC_ID_PCM_MULAW) {
  515. st->codec->channels = 1;
  516. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  517. st->codec->sample_rate = 8000;
  518. }
  519. st->request_probe = request_probe;
  520. if (codec_id != AV_CODEC_ID_PCM_S16BE)
  521. st->need_parsing = AVSTREAM_PARSE_FULL;
  522. found:
  523. if(st->discard >= AVDISCARD_ALL)
  524. goto skip;
  525. if (startcode >= 0xa0 && startcode <= 0xaf) {
  526. if (lpcm_header_len == 6 && st->codec->codec_id == AV_CODEC_ID_MLP) {
  527. if (len < 6)
  528. goto skip;
  529. avio_skip(s->pb, 6);
  530. len -=6;
  531. } else {
  532. int b1, freq;
  533. /* for LPCM, we just skip the header and consider it is raw
  534. audio data */
  535. if (len <= 3)
  536. goto skip;
  537. avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
  538. b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
  539. avio_r8(s->pb); /* dynamic range control (0x80 = off) */
  540. len -= 3;
  541. freq = (b1 >> 4) & 3;
  542. st->codec->sample_rate = lpcm_freq_tab[freq];
  543. st->codec->channels = 1 + (b1 & 7);
  544. st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
  545. st->codec->bit_rate = st->codec->channels *
  546. st->codec->sample_rate *
  547. st->codec->bits_per_coded_sample;
  548. if (st->codec->bits_per_coded_sample == 16)
  549. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  550. else if (st->codec->bits_per_coded_sample == 28)
  551. return AVERROR(EINVAL);
  552. }
  553. }
  554. ret = av_get_packet(s->pb, pkt, len);
  555. pkt->pts = pts;
  556. pkt->dts = dts;
  557. pkt->pos = dummy_pos;
  558. pkt->stream_index = st->index;
  559. av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
  560. pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
  561. pkt->size);
  562. return (ret < 0) ? ret : 0;
  563. }
  564. static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
  565. int64_t *ppos, int64_t pos_limit)
  566. {
  567. int len, startcode;
  568. int64_t pos, pts, dts;
  569. pos = *ppos;
  570. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  571. return AV_NOPTS_VALUE;
  572. for(;;) {
  573. len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
  574. if (len < 0) {
  575. av_dlog(s, "none (ret=%d)\n", len);
  576. return AV_NOPTS_VALUE;
  577. }
  578. if (startcode == s->streams[stream_index]->id &&
  579. dts != AV_NOPTS_VALUE) {
  580. break;
  581. }
  582. avio_skip(s->pb, len);
  583. }
  584. av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
  585. pos, dts, dts / 90000.0);
  586. *ppos = pos;
  587. return dts;
  588. }
  589. AVInputFormat ff_mpegps_demuxer = {
  590. .name = "mpeg",
  591. .long_name = NULL_IF_CONFIG_SMALL("MPEG-PS (MPEG-2 Program Stream)"),
  592. .priv_data_size = sizeof(MpegDemuxContext),
  593. .read_probe = mpegps_probe,
  594. .read_header = mpegps_read_header,
  595. .read_packet = mpegps_read_packet,
  596. .read_timestamp = mpegps_read_dts,
  597. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  598. };
  599. #if CONFIG_VOBSUB_DEMUXER
  600. #define REF_STRING "# VobSub index file,"
  601. static int vobsub_probe(AVProbeData *p)
  602. {
  603. if (!strncmp(p->buf, REF_STRING, sizeof(REF_STRING) - 1))
  604. return AVPROBE_SCORE_MAX;
  605. return 0;
  606. }
  607. static int vobsub_read_header(AVFormatContext *s)
  608. {
  609. int i, ret = 0, header_parsed = 0, langidx = 0;
  610. MpegDemuxContext *vobsub = s->priv_data;
  611. char *sub_name = NULL;
  612. size_t fname_len;
  613. char *ext, *header_str;
  614. AVBPrint header;
  615. int64_t delay = 0;
  616. AVStream *st = NULL;
  617. sub_name = av_strdup(s->filename);
  618. fname_len = strlen(sub_name);
  619. ext = sub_name - 3 + fname_len;
  620. if (fname_len < 4 || *(ext - 1) != '.') {
  621. av_log(s, AV_LOG_ERROR, "The input index filename is too short "
  622. "to guess the associated .SUB file\n");
  623. ret = AVERROR_INVALIDDATA;
  624. goto end;
  625. }
  626. memcpy(ext, !strncmp(ext, "IDX", 3) ? "SUB" : "sub", 3);
  627. av_log(s, AV_LOG_VERBOSE, "IDX/SUB: %s -> %s\n", s->filename, sub_name);
  628. ret = avformat_open_input(&vobsub->sub_ctx, sub_name, &ff_mpegps_demuxer, NULL);
  629. if (ret < 0) {
  630. av_log(s, AV_LOG_ERROR, "Unable to open %s as MPEG subtitles\n", sub_name);
  631. goto end;
  632. }
  633. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  634. while (!url_feof(s->pb)) {
  635. char line[2048];
  636. int len = ff_get_line(s->pb, line, sizeof(line));
  637. if (!len)
  638. break;
  639. line[strcspn(line, "\r\n")] = 0;
  640. if (!strncmp(line, "id:", 3)) {
  641. int n, stream_id = 0;
  642. char id[64] = {0};
  643. n = sscanf(line, "id: %63[^,], index: %u", id, &stream_id);
  644. if (n != 2) {
  645. av_log(s, AV_LOG_WARNING, "Unable to parse index line '%s', "
  646. "assuming 'id: und, index: 0'\n", line);
  647. strcpy(id, "und");
  648. stream_id = 0;
  649. }
  650. st = avformat_new_stream(s, NULL);
  651. if (!st) {
  652. ret = AVERROR(ENOMEM);
  653. goto end;
  654. }
  655. st->id = stream_id;
  656. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  657. st->codec->codec_id = AV_CODEC_ID_DVD_SUBTITLE;
  658. av_dict_set(&st->metadata, "language", id, 0);
  659. av_log(s, AV_LOG_DEBUG, "IDX stream[%d] id=%s\n", stream_id, id);
  660. header_parsed = 1;
  661. } else if (st && !strncmp(line, "timestamp:", 10)) {
  662. AVPacket *sub;
  663. int hh, mm, ss, ms;
  664. int64_t pos, timestamp;
  665. const char *p = line + 10;
  666. if (sscanf(p, "%02d:%02d:%02d:%03d, filepos: %"SCNx64,
  667. &hh, &mm, &ss, &ms, &pos) != 5) {
  668. av_log(s, AV_LOG_ERROR, "Unable to parse timestamp line '%s', "
  669. "abort parsing\n", line);
  670. break;
  671. }
  672. timestamp = (hh*3600LL + mm*60LL + ss) * 1000LL + ms + delay;
  673. timestamp = av_rescale_q(timestamp, (AVRational){1,1000}, st->time_base);
  674. sub = ff_subtitles_queue_insert(&vobsub->q, "", 0, 0);
  675. if (!sub) {
  676. ret = AVERROR(ENOMEM);
  677. goto end;
  678. }
  679. sub->pos = pos;
  680. sub->pts = timestamp;
  681. sub->stream_index = s->nb_streams - 1;
  682. } else if (st && !strncmp(line, "alt:", 4)) {
  683. const char *p = line + 4;
  684. while (*p == ' ')
  685. p++;
  686. av_dict_set(&st->metadata, "title", p, 0);
  687. av_log(s, AV_LOG_DEBUG, "IDX stream[%d] name=%s\n", st->id, p);
  688. header_parsed = 1;
  689. } else if (!strncmp(line, "delay:", 6)) {
  690. int sign = 1, hh = 0, mm = 0, ss = 0, ms = 0;
  691. const char *p = line + 6;
  692. while (*p == ' ')
  693. p++;
  694. if (*p == '-' || *p == '+') {
  695. sign = *p == '-' ? -1 : 1;
  696. p++;
  697. }
  698. sscanf(p, "%d:%d:%d:%d", &hh, &mm, &ss, &ms);
  699. delay = ((hh*3600LL + mm*60LL + ss) * 1000LL + ms) * sign;
  700. } else if (!strncmp(line, "langidx:", 8)) {
  701. const char *p = line + 8;
  702. if (sscanf(p, "%d", &langidx) != 1)
  703. av_log(s, AV_LOG_ERROR, "Invalid langidx specified\n");
  704. } else if (!header_parsed) {
  705. if (line[0] && line[0] != '#')
  706. av_bprintf(&header, "%s\n", line);
  707. }
  708. }
  709. if (langidx < s->nb_streams)
  710. s->streams[langidx]->disposition |= AV_DISPOSITION_DEFAULT;
  711. ff_subtitles_queue_finalize(&vobsub->q);
  712. if (!av_bprint_is_complete(&header)) {
  713. av_bprint_finalize(&header, NULL);
  714. ret = AVERROR(ENOMEM);
  715. goto end;
  716. }
  717. av_bprint_finalize(&header, &header_str);
  718. for (i = 0; i < s->nb_streams; i++) {
  719. AVStream *sub_st = s->streams[i];
  720. sub_st->codec->extradata = av_strdup(header_str);
  721. sub_st->codec->extradata_size = header.len;
  722. }
  723. av_free(header_str);
  724. end:
  725. av_free(sub_name);
  726. return ret;
  727. }
  728. #define FAIL(r) do { ret = r; goto fail; } while (0)
  729. static int vobsub_read_packet(AVFormatContext *s, AVPacket *pkt)
  730. {
  731. MpegDemuxContext *vobsub = s->priv_data;
  732. FFDemuxSubtitlesQueue *q = &vobsub->q;
  733. AVIOContext *pb = vobsub->sub_ctx->pb;
  734. int ret, psize, len16 = -1;
  735. AVPacket idx_pkt;
  736. ret = ff_subtitles_queue_read_packet(q, &idx_pkt);
  737. if (ret < 0)
  738. return ret;
  739. /* compute maximum packet size using the next packet position. This is
  740. * useful when the len in the header is non-sense */
  741. if (q->current_sub_idx < q->nb_subs) {
  742. psize = q->subs[q->current_sub_idx].pos - idx_pkt.pos;
  743. } else {
  744. int64_t fsize = avio_size(pb);
  745. psize = fsize < 0 ? 0xffff : fsize - idx_pkt.pos;
  746. }
  747. avio_seek(pb, idx_pkt.pos, SEEK_SET);
  748. av_init_packet(pkt);
  749. pkt->size = 0;
  750. pkt->data = NULL;
  751. do {
  752. int n, to_read, startcode;
  753. int64_t pts, dts;
  754. ret = mpegps_read_pes_header(vobsub->sub_ctx, NULL, &startcode, &pts, &dts);
  755. if (ret < 0)
  756. FAIL(ret);
  757. to_read = ret & 0xffff;
  758. /* this prevents reads above the current packet */
  759. if (pkt->size + to_read > psize)
  760. break;
  761. /* if the len is computed, we check for overread */
  762. if (len16 != -1 && pkt->size + to_read > len16)
  763. break;
  764. /* the current chunk doesn't match the stream index (unlikely) */
  765. if ((startcode & 0x1f) != idx_pkt.stream_index)
  766. break;
  767. ret = av_grow_packet(pkt, to_read);
  768. if (ret < 0)
  769. FAIL(ret);
  770. n = avio_read(pb, pkt->data + (pkt->size - to_read), to_read);
  771. if (n < to_read)
  772. pkt->size -= to_read - n;
  773. /* first chunk contains the total len of the packet to raise */
  774. if (len16 == -1 && n > 2)
  775. len16 = AV_RB16(pkt->data);
  776. } while (len16 != -1 && pkt->size != len16);
  777. pkt->pts = pkt->dts = idx_pkt.pts;
  778. pkt->pos = idx_pkt.pos;
  779. pkt->stream_index = idx_pkt.stream_index;
  780. av_free_packet(&idx_pkt);
  781. return 0;
  782. fail:
  783. av_free_packet(&idx_pkt);
  784. return ret;
  785. }
  786. static int vobsub_read_seek(AVFormatContext *s, int stream_index,
  787. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  788. {
  789. MpegDemuxContext *vobsub = s->priv_data;
  790. return ff_subtitles_queue_seek(&vobsub->q, s, stream_index,
  791. min_ts, ts, max_ts, flags);
  792. }
  793. static int vobsub_read_close(AVFormatContext *s)
  794. {
  795. MpegDemuxContext *vobsub = s->priv_data;
  796. ff_subtitles_queue_clean(&vobsub->q);
  797. if (vobsub->sub_ctx)
  798. avformat_close_input(&vobsub->sub_ctx);
  799. return 0;
  800. }
  801. AVInputFormat ff_vobsub_demuxer = {
  802. .name = "vobsub",
  803. .long_name = NULL_IF_CONFIG_SMALL("VobSub subtitle format"),
  804. .priv_data_size = sizeof(MpegDemuxContext),
  805. .read_probe = vobsub_probe,
  806. .read_header = vobsub_read_header,
  807. .read_packet = vobsub_read_packet,
  808. .read_seek2 = vobsub_read_seek,
  809. .read_close = vobsub_read_close,
  810. .flags = AVFMT_SHOW_IDS,
  811. .extensions = "idx",
  812. };
  813. #endif