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.

953 lines
26KB

  1. /*
  2. * MPEG1/2 mux/demux
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #define MAX_PAYLOAD_SIZE 4096
  21. #define NB_STREAMS 2
  22. //#define DEBUG_SEEK
  23. typedef struct {
  24. uint8_t buffer[MAX_PAYLOAD_SIZE];
  25. int buffer_ptr;
  26. uint8_t id;
  27. int max_buffer_size; /* in bytes */
  28. int packet_number;
  29. int64_t start_pts;
  30. } StreamInfo;
  31. typedef struct {
  32. int packet_size; /* required packet size */
  33. int packet_data_max_size; /* maximum data size inside a packet */
  34. int packet_number;
  35. int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
  36. int system_header_freq;
  37. int mux_rate; /* bitrate in units of 50 bytes/s */
  38. /* stream info */
  39. int audio_bound;
  40. int video_bound;
  41. int is_mpeg2;
  42. int is_vcd;
  43. } MpegMuxContext;
  44. #define PACK_START_CODE ((unsigned int)0x000001ba)
  45. #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
  46. #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
  47. #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
  48. #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
  49. #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
  50. /* mpeg2 */
  51. #define PROGRAM_STREAM_MAP 0x1bc
  52. #define PRIVATE_STREAM_1 0x1bd
  53. #define PADDING_STREAM 0x1be
  54. #define PRIVATE_STREAM_2 0x1bf
  55. #define AUDIO_ID 0xc0
  56. #define VIDEO_ID 0xe0
  57. #ifdef CONFIG_ENCODERS
  58. extern AVOutputFormat mpeg1system_mux;
  59. extern AVOutputFormat mpeg1vcd_mux;
  60. extern AVOutputFormat mpeg2vob_mux;
  61. static int put_pack_header(AVFormatContext *ctx,
  62. uint8_t *buf, int64_t timestamp)
  63. {
  64. MpegMuxContext *s = ctx->priv_data;
  65. PutBitContext pb;
  66. init_put_bits(&pb, buf, 128);
  67. put_bits(&pb, 32, PACK_START_CODE);
  68. if (s->is_mpeg2) {
  69. put_bits(&pb, 2, 0x1);
  70. } else {
  71. put_bits(&pb, 4, 0x2);
  72. }
  73. put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
  74. put_bits(&pb, 1, 1);
  75. put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
  76. put_bits(&pb, 1, 1);
  77. put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
  78. put_bits(&pb, 1, 1);
  79. if (s->is_mpeg2) {
  80. /* clock extension */
  81. put_bits(&pb, 9, 0);
  82. put_bits(&pb, 1, 1);
  83. }
  84. put_bits(&pb, 1, 1);
  85. put_bits(&pb, 22, s->mux_rate);
  86. put_bits(&pb, 1, 1);
  87. if (s->is_mpeg2) {
  88. put_bits(&pb, 5, 0x1f); /* reserved */
  89. put_bits(&pb, 3, 0); /* stuffing length */
  90. }
  91. flush_put_bits(&pb);
  92. return pbBufPtr(&pb) - pb.buf;
  93. }
  94. static int put_system_header(AVFormatContext *ctx, uint8_t *buf)
  95. {
  96. MpegMuxContext *s = ctx->priv_data;
  97. int size, rate_bound, i, private_stream_coded, id;
  98. PutBitContext pb;
  99. init_put_bits(&pb, buf, 128);
  100. put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
  101. put_bits(&pb, 16, 0);
  102. put_bits(&pb, 1, 1);
  103. rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
  104. put_bits(&pb, 22, rate_bound);
  105. put_bits(&pb, 1, 1); /* marker */
  106. put_bits(&pb, 6, s->audio_bound);
  107. put_bits(&pb, 1, 1); /* variable bitrate */
  108. put_bits(&pb, 1, 1); /* non constrainted bit stream */
  109. put_bits(&pb, 1, 0); /* audio locked */
  110. put_bits(&pb, 1, 0); /* video locked */
  111. put_bits(&pb, 1, 1); /* marker */
  112. put_bits(&pb, 5, s->video_bound);
  113. put_bits(&pb, 8, 0xff); /* reserved byte */
  114. /* audio stream info */
  115. private_stream_coded = 0;
  116. for(i=0;i<ctx->nb_streams;i++) {
  117. StreamInfo *stream = ctx->streams[i]->priv_data;
  118. id = stream->id;
  119. if (id < 0xc0) {
  120. /* special case for private streams (AC3 use that) */
  121. if (private_stream_coded)
  122. continue;
  123. private_stream_coded = 1;
  124. id = 0xbd;
  125. }
  126. put_bits(&pb, 8, id); /* stream ID */
  127. put_bits(&pb, 2, 3);
  128. if (id < 0xe0) {
  129. /* audio */
  130. put_bits(&pb, 1, 0);
  131. put_bits(&pb, 13, stream->max_buffer_size / 128);
  132. } else {
  133. /* video */
  134. put_bits(&pb, 1, 1);
  135. put_bits(&pb, 13, stream->max_buffer_size / 1024);
  136. }
  137. }
  138. flush_put_bits(&pb);
  139. size = pbBufPtr(&pb) - pb.buf;
  140. /* patch packet size */
  141. buf[4] = (size - 6) >> 8;
  142. buf[5] = (size - 6) & 0xff;
  143. return size;
  144. }
  145. static int mpeg_mux_init(AVFormatContext *ctx)
  146. {
  147. MpegMuxContext *s = ctx->priv_data;
  148. int bitrate, i, mpa_id, mpv_id, ac3_id;
  149. AVStream *st;
  150. StreamInfo *stream;
  151. s->packet_number = 0;
  152. s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
  153. s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
  154. if (s->is_vcd)
  155. s->packet_size = 2324; /* VCD packet size */
  156. else
  157. s->packet_size = 2048;
  158. /* startcode(4) + length(2) + flags(1) */
  159. s->packet_data_max_size = s->packet_size - 7;
  160. s->audio_bound = 0;
  161. s->video_bound = 0;
  162. mpa_id = AUDIO_ID;
  163. ac3_id = 0x80;
  164. mpv_id = VIDEO_ID;
  165. for(i=0;i<ctx->nb_streams;i++) {
  166. st = ctx->streams[i];
  167. stream = av_mallocz(sizeof(StreamInfo));
  168. if (!stream)
  169. goto fail;
  170. st->priv_data = stream;
  171. switch(st->codec.codec_type) {
  172. case CODEC_TYPE_AUDIO:
  173. if (st->codec.codec_id == CODEC_ID_AC3)
  174. stream->id = ac3_id++;
  175. else
  176. stream->id = mpa_id++;
  177. stream->max_buffer_size = 4 * 1024;
  178. s->audio_bound++;
  179. break;
  180. case CODEC_TYPE_VIDEO:
  181. stream->id = mpv_id++;
  182. stream->max_buffer_size = 46 * 1024;
  183. s->video_bound++;
  184. break;
  185. default:
  186. av_abort();
  187. }
  188. }
  189. /* we increase slightly the bitrate to take into account the
  190. headers. XXX: compute it exactly */
  191. bitrate = 2000;
  192. for(i=0;i<ctx->nb_streams;i++) {
  193. st = ctx->streams[i];
  194. bitrate += st->codec.bit_rate;
  195. }
  196. s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
  197. if (s->is_vcd || s->is_mpeg2)
  198. /* every packet */
  199. s->pack_header_freq = 1;
  200. else
  201. /* every 2 seconds */
  202. s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
  203. /* the above seems to make pack_header_freq zero sometimes */
  204. if (s->pack_header_freq == 0)
  205. s->pack_header_freq = 1;
  206. if (s->is_mpeg2)
  207. /* every 200 packets. Need to look at the spec. */
  208. s->system_header_freq = s->pack_header_freq * 40;
  209. else if (s->is_vcd)
  210. /* every 40 packets, this is my invention */
  211. s->system_header_freq = s->pack_header_freq * 40;
  212. else
  213. s->system_header_freq = s->pack_header_freq * 5;
  214. for(i=0;i<ctx->nb_streams;i++) {
  215. stream = ctx->streams[i]->priv_data;
  216. stream->buffer_ptr = 0;
  217. stream->packet_number = 0;
  218. stream->start_pts = -1;
  219. }
  220. return 0;
  221. fail:
  222. for(i=0;i<ctx->nb_streams;i++) {
  223. av_free(ctx->streams[i]->priv_data);
  224. }
  225. return -ENOMEM;
  226. }
  227. /* flush the packet on stream stream_index */
  228. static void flush_packet(AVFormatContext *ctx, int stream_index)
  229. {
  230. MpegMuxContext *s = ctx->priv_data;
  231. StreamInfo *stream = ctx->streams[stream_index]->priv_data;
  232. uint8_t *buf_ptr;
  233. int size, payload_size, startcode, id, len, stuffing_size, i, header_len;
  234. int64_t timestamp;
  235. uint8_t buffer[128];
  236. id = stream->id;
  237. timestamp = stream->start_pts;
  238. #if 0
  239. printf("packet ID=%2x PTS=%0.3f\n",
  240. id, timestamp / 90000.0);
  241. #endif
  242. buf_ptr = buffer;
  243. if (((s->packet_number % s->pack_header_freq) == 0)) {
  244. /* output pack and systems header if needed */
  245. size = put_pack_header(ctx, buf_ptr, timestamp);
  246. buf_ptr += size;
  247. if ((s->packet_number % s->system_header_freq) == 0) {
  248. size = put_system_header(ctx, buf_ptr);
  249. buf_ptr += size;
  250. }
  251. }
  252. size = buf_ptr - buffer;
  253. put_buffer(&ctx->pb, buffer, size);
  254. /* packet header */
  255. if (s->is_mpeg2) {
  256. header_len = 8;
  257. } else {
  258. header_len = 5;
  259. }
  260. payload_size = s->packet_size - (size + 6 + header_len);
  261. if (id < 0xc0) {
  262. startcode = PRIVATE_STREAM_1;
  263. payload_size -= 4;
  264. } else {
  265. startcode = 0x100 + id;
  266. }
  267. stuffing_size = payload_size - stream->buffer_ptr;
  268. if (stuffing_size < 0)
  269. stuffing_size = 0;
  270. put_be32(&ctx->pb, startcode);
  271. put_be16(&ctx->pb, payload_size + header_len);
  272. /* stuffing */
  273. for(i=0;i<stuffing_size;i++)
  274. put_byte(&ctx->pb, 0xff);
  275. if (s->is_mpeg2) {
  276. put_byte(&ctx->pb, 0x80); /* mpeg2 id */
  277. put_byte(&ctx->pb, 0x80); /* flags */
  278. put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */
  279. }
  280. put_byte(&ctx->pb,
  281. (0x02 << 4) |
  282. (((timestamp >> 30) & 0x07) << 1) |
  283. 1);
  284. put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
  285. put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
  286. if (startcode == PRIVATE_STREAM_1) {
  287. put_byte(&ctx->pb, id);
  288. if (id >= 0x80 && id <= 0xbf) {
  289. /* XXX: need to check AC3 spec */
  290. put_byte(&ctx->pb, 1);
  291. put_byte(&ctx->pb, 0);
  292. put_byte(&ctx->pb, 2);
  293. }
  294. }
  295. /* output data */
  296. put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
  297. put_flush_packet(&ctx->pb);
  298. /* preserve remaining data */
  299. len = stream->buffer_ptr - payload_size;
  300. if (len < 0)
  301. len = 0;
  302. memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
  303. stream->buffer_ptr = len;
  304. s->packet_number++;
  305. stream->packet_number++;
  306. stream->start_pts = -1;
  307. }
  308. static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
  309. const uint8_t *buf, int size, int64_t pts)
  310. {
  311. MpegMuxContext *s = ctx->priv_data;
  312. AVStream *st = ctx->streams[stream_index];
  313. StreamInfo *stream = st->priv_data;
  314. int len;
  315. while (size > 0) {
  316. /* set pts */
  317. if (stream->start_pts == -1) {
  318. stream->start_pts = pts;
  319. }
  320. len = s->packet_data_max_size - stream->buffer_ptr;
  321. if (len > size)
  322. len = size;
  323. memcpy(stream->buffer + stream->buffer_ptr, buf, len);
  324. stream->buffer_ptr += len;
  325. buf += len;
  326. size -= len;
  327. while (stream->buffer_ptr >= s->packet_data_max_size) {
  328. /* output the packet */
  329. if (stream->start_pts == -1)
  330. stream->start_pts = pts;
  331. flush_packet(ctx, stream_index);
  332. }
  333. }
  334. return 0;
  335. }
  336. static int mpeg_mux_end(AVFormatContext *ctx)
  337. {
  338. StreamInfo *stream;
  339. int i;
  340. /* flush each packet */
  341. for(i=0;i<ctx->nb_streams;i++) {
  342. stream = ctx->streams[i]->priv_data;
  343. if (stream->buffer_ptr > 0) {
  344. flush_packet(ctx, i);
  345. }
  346. }
  347. /* End header according to MPEG1 systems standard. We do not write
  348. it as it is usually not needed by decoders and because it
  349. complicates MPEG stream concatenation. */
  350. //put_be32(&ctx->pb, ISO_11172_END_CODE);
  351. //put_flush_packet(&ctx->pb);
  352. for(i=0;i<ctx->nb_streams;i++)
  353. av_freep(&ctx->streams[i]->priv_data);
  354. return 0;
  355. }
  356. #endif //CONFIG_ENCODERS
  357. /*********************************************/
  358. /* demux code */
  359. #define MAX_SYNC_SIZE 100000
  360. static int mpegps_probe(AVProbeData *p)
  361. {
  362. int code, c, i;
  363. code = 0xff;
  364. /* we search the first start code. If it is a packet start code,
  365. then we decide it is mpeg ps. We do not send highest value to
  366. give a chance to mpegts */
  367. /* NOTE: the search range was restricted to avoid too many false
  368. detections */
  369. if (p->buf_size < 6)
  370. return 0;
  371. for (i = 0; i < 20; i++) {
  372. c = p->buf[i];
  373. code = (code << 8) | c;
  374. if ((code & 0xffffff00) == 0x100) {
  375. if (code == PACK_START_CODE ||
  376. code == SYSTEM_HEADER_START_CODE ||
  377. (code >= 0x1e0 && code <= 0x1ef) ||
  378. (code >= 0x1c0 && code <= 0x1df) ||
  379. code == PRIVATE_STREAM_2 ||
  380. code == PROGRAM_STREAM_MAP ||
  381. code == PRIVATE_STREAM_1 ||
  382. code == PADDING_STREAM)
  383. return AVPROBE_SCORE_MAX - 2;
  384. else
  385. return 0;
  386. }
  387. }
  388. return 0;
  389. }
  390. typedef struct MpegDemuxContext {
  391. int header_state;
  392. } MpegDemuxContext;
  393. static int mpegps_read_header(AVFormatContext *s,
  394. AVFormatParameters *ap)
  395. {
  396. MpegDemuxContext *m = s->priv_data;
  397. m->header_state = 0xff;
  398. s->ctx_flags |= AVFMTCTX_NOHEADER;
  399. /* no need to do more */
  400. return 0;
  401. }
  402. static int64_t get_pts(ByteIOContext *pb, int c)
  403. {
  404. int64_t pts;
  405. int val;
  406. if (c < 0)
  407. c = get_byte(pb);
  408. pts = (int64_t)((c >> 1) & 0x07) << 30;
  409. val = get_be16(pb);
  410. pts |= (int64_t)(val >> 1) << 15;
  411. val = get_be16(pb);
  412. pts |= (int64_t)(val >> 1);
  413. return pts;
  414. }
  415. static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
  416. uint32_t *header_state)
  417. {
  418. unsigned int state, v;
  419. int val, n;
  420. state = *header_state;
  421. n = *size_ptr;
  422. while (n > 0) {
  423. if (url_feof(pb))
  424. break;
  425. v = get_byte(pb);
  426. n--;
  427. if (state == 0x000001) {
  428. state = ((state << 8) | v) & 0xffffff;
  429. val = state;
  430. goto found;
  431. }
  432. state = ((state << 8) | v) & 0xffffff;
  433. }
  434. val = -1;
  435. found:
  436. *header_state = state;
  437. *size_ptr = n;
  438. return val;
  439. }
  440. /* XXX: optimize */
  441. static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
  442. {
  443. int64_t pos, pos_start;
  444. int max_size, start_code;
  445. max_size = *size_ptr;
  446. pos_start = url_ftell(pb);
  447. /* in order to go faster, we fill the buffer */
  448. pos = pos_start - 16386;
  449. if (pos < 0)
  450. pos = 0;
  451. url_fseek(pb, pos, SEEK_SET);
  452. get_byte(pb);
  453. pos = pos_start;
  454. for(;;) {
  455. pos--;
  456. if (pos < 0 || (pos_start - pos) >= max_size) {
  457. start_code = -1;
  458. goto the_end;
  459. }
  460. url_fseek(pb, pos, SEEK_SET);
  461. start_code = get_be32(pb);
  462. if ((start_code & 0xffffff00) == 0x100)
  463. break;
  464. }
  465. the_end:
  466. *size_ptr = pos_start - pos;
  467. return start_code;
  468. }
  469. /* read the next (or previous) PES header. Return its position in ppos
  470. (if not NULL), and its start code, pts and dts.
  471. */
  472. static int mpegps_read_pes_header(AVFormatContext *s,
  473. int64_t *ppos, int *pstart_code,
  474. int64_t *ppts, int64_t *pdts, int find_next)
  475. {
  476. MpegDemuxContext *m = s->priv_data;
  477. int len, size, startcode, c, flags, header_len;
  478. int64_t pts, dts, last_pos;
  479. last_pos = -1;
  480. redo:
  481. if (find_next) {
  482. /* next start code (should be immediately after) */
  483. m->header_state = 0xff;
  484. size = MAX_SYNC_SIZE;
  485. startcode = find_next_start_code(&s->pb, &size, &m->header_state);
  486. } else {
  487. if (last_pos >= 0)
  488. url_fseek(&s->pb, last_pos, SEEK_SET);
  489. size = MAX_SYNC_SIZE;
  490. startcode = find_prev_start_code(&s->pb, &size);
  491. last_pos = url_ftell(&s->pb) - 4;
  492. }
  493. //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
  494. if (startcode < 0)
  495. return -EIO;
  496. if (startcode == PACK_START_CODE)
  497. goto redo;
  498. if (startcode == SYSTEM_HEADER_START_CODE)
  499. goto redo;
  500. if (startcode == PADDING_STREAM ||
  501. startcode == PRIVATE_STREAM_2) {
  502. /* skip them */
  503. len = get_be16(&s->pb);
  504. url_fskip(&s->pb, len);
  505. goto redo;
  506. }
  507. /* find matching stream */
  508. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  509. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  510. (startcode == 0x1bd)))
  511. goto redo;
  512. if (ppos) {
  513. *ppos = url_ftell(&s->pb) - 4;
  514. }
  515. len = get_be16(&s->pb);
  516. pts = AV_NOPTS_VALUE;
  517. dts = AV_NOPTS_VALUE;
  518. /* stuffing */
  519. for(;;) {
  520. if (len < 1)
  521. goto redo;
  522. c = get_byte(&s->pb);
  523. len--;
  524. /* XXX: for mpeg1, should test only bit 7 */
  525. if (c != 0xff)
  526. break;
  527. }
  528. if ((c & 0xc0) == 0x40) {
  529. /* buffer scale & size */
  530. if (len < 2)
  531. goto redo;
  532. get_byte(&s->pb);
  533. c = get_byte(&s->pb);
  534. len -= 2;
  535. }
  536. if ((c & 0xf0) == 0x20) {
  537. if (len < 4)
  538. goto redo;
  539. dts = pts = get_pts(&s->pb, c);
  540. len -= 4;
  541. } else if ((c & 0xf0) == 0x30) {
  542. if (len < 9)
  543. goto redo;
  544. pts = get_pts(&s->pb, c);
  545. dts = get_pts(&s->pb, -1);
  546. len -= 9;
  547. } else if ((c & 0xc0) == 0x80) {
  548. /* mpeg 2 PES */
  549. if ((c & 0x30) != 0) {
  550. /* Encrypted multiplex not handled */
  551. goto redo;
  552. }
  553. flags = get_byte(&s->pb);
  554. header_len = get_byte(&s->pb);
  555. len -= 2;
  556. if (header_len > len)
  557. goto redo;
  558. if ((flags & 0xc0) == 0x80) {
  559. dts = pts = get_pts(&s->pb, -1);
  560. if (header_len < 5)
  561. goto redo;
  562. header_len -= 5;
  563. len -= 5;
  564. } if ((flags & 0xc0) == 0xc0) {
  565. pts = get_pts(&s->pb, -1);
  566. dts = get_pts(&s->pb, -1);
  567. if (header_len < 10)
  568. goto redo;
  569. header_len -= 10;
  570. len -= 10;
  571. }
  572. len -= header_len;
  573. while (header_len > 0) {
  574. get_byte(&s->pb);
  575. header_len--;
  576. }
  577. }
  578. if (startcode == 0x1bd) {
  579. if (len < 1)
  580. goto redo;
  581. startcode = get_byte(&s->pb);
  582. len--;
  583. if (startcode >= 0x80 && startcode <= 0xbf) {
  584. /* audio: skip header */
  585. if (len < 3)
  586. goto redo;
  587. get_byte(&s->pb);
  588. get_byte(&s->pb);
  589. get_byte(&s->pb);
  590. len -= 3;
  591. }
  592. }
  593. *pstart_code = startcode;
  594. *ppts = pts;
  595. *pdts = dts;
  596. return len;
  597. }
  598. static int mpegps_read_packet(AVFormatContext *s,
  599. AVPacket *pkt)
  600. {
  601. AVStream *st;
  602. int len, startcode, i, type, codec_id;
  603. int64_t pts, dts;
  604. redo:
  605. len = mpegps_read_pes_header(s, NULL, &startcode, &pts, &dts, 1);
  606. if (len < 0)
  607. return len;
  608. /* now find stream */
  609. for(i=0;i<s->nb_streams;i++) {
  610. st = s->streams[i];
  611. if (st->id == startcode)
  612. goto found;
  613. }
  614. if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  615. type = CODEC_TYPE_VIDEO;
  616. codec_id = CODEC_ID_MPEG1VIDEO;
  617. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  618. type = CODEC_TYPE_AUDIO;
  619. codec_id = CODEC_ID_MP2;
  620. } else if (startcode >= 0x80 && startcode <= 0x9f) {
  621. type = CODEC_TYPE_AUDIO;
  622. codec_id = CODEC_ID_AC3;
  623. } else if (startcode >= 0xa0 && startcode <= 0xbf) {
  624. type = CODEC_TYPE_AUDIO;
  625. codec_id = CODEC_ID_PCM_S16BE;
  626. } else {
  627. skip:
  628. /* skip packet */
  629. url_fskip(&s->pb, len);
  630. goto redo;
  631. }
  632. /* no stream found: add a new stream */
  633. st = av_new_stream(s, startcode);
  634. if (!st)
  635. goto skip;
  636. st->codec.codec_type = type;
  637. st->codec.codec_id = codec_id;
  638. if (codec_id != CODEC_ID_PCM_S16BE)
  639. st->need_parsing = 1;
  640. found:
  641. if (startcode >= 0xa0 && startcode <= 0xbf) {
  642. int b1, freq;
  643. static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
  644. /* for LPCM, we just skip the header and consider it is raw
  645. audio data */
  646. if (len <= 3)
  647. goto skip;
  648. get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
  649. b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
  650. get_byte(&s->pb); /* dynamic range control (0x80 = off) */
  651. len -= 3;
  652. freq = (b1 >> 4) & 3;
  653. st->codec.sample_rate = lpcm_freq_tab[freq];
  654. st->codec.channels = 1 + (b1 & 7);
  655. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
  656. }
  657. av_new_packet(pkt, len);
  658. //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
  659. // (float)pts/90000, len);
  660. get_buffer(&s->pb, pkt->data, pkt->size);
  661. pkt->pts = pts;
  662. pkt->dts = dts;
  663. pkt->stream_index = st->index;
  664. return 0;
  665. }
  666. static int mpegps_read_close(AVFormatContext *s)
  667. {
  668. return 0;
  669. }
  670. static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
  671. int64_t *ppos, int find_next)
  672. {
  673. int len, startcode;
  674. int64_t pos, pts, dts;
  675. pos = *ppos;
  676. #ifdef DEBUG_SEEK
  677. printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
  678. #endif
  679. url_fseek(&s->pb, pos, SEEK_SET);
  680. for(;;) {
  681. len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts, find_next);
  682. if (len < 0) {
  683. #ifdef DEBUG_SEEK
  684. printf("none (ret=%d)\n", len);
  685. #endif
  686. return AV_NOPTS_VALUE;
  687. }
  688. if (startcode == s->streams[stream_index]->id &&
  689. dts != AV_NOPTS_VALUE) {
  690. break;
  691. }
  692. if (find_next) {
  693. url_fskip(&s->pb, len);
  694. } else {
  695. url_fseek(&s->pb, pos, SEEK_SET);
  696. }
  697. }
  698. #ifdef DEBUG_SEEK
  699. printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
  700. #endif
  701. *ppos = pos;
  702. return dts;
  703. }
  704. static int find_stream_index(AVFormatContext *s)
  705. {
  706. int i;
  707. AVStream *st;
  708. if (s->nb_streams <= 0)
  709. return -1;
  710. for(i = 0; i < s->nb_streams; i++) {
  711. st = s->streams[i];
  712. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  713. return i;
  714. }
  715. }
  716. return 0;
  717. }
  718. static int mpegps_read_seek(AVFormatContext *s,
  719. int stream_index, int64_t timestamp)
  720. {
  721. int64_t pos_min, pos_max, pos;
  722. int64_t dts_min, dts_max, dts;
  723. timestamp = (timestamp * 90000) / AV_TIME_BASE;
  724. #ifdef DEBUG_SEEK
  725. printf("read_seek: %d %0.3f\n", stream_index, timestamp / 90000.0);
  726. #endif
  727. /* XXX: find stream_index by looking at the first PES packet found */
  728. if (stream_index < 0) {
  729. stream_index = find_stream_index(s);
  730. if (stream_index < 0)
  731. return -1;
  732. }
  733. pos_min = 0;
  734. dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
  735. if (dts_min == AV_NOPTS_VALUE) {
  736. /* we can reach this case only if no PTS are present in
  737. the whole stream */
  738. return -1;
  739. }
  740. pos_max = url_filesize(url_fileno(&s->pb)) - 1;
  741. dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
  742. while (pos_min <= pos_max) {
  743. #ifdef DEBUG_SEEK
  744. printf("pos_min=0x%llx pos_max=0x%llx dts_min=%0.3f dts_max=%0.3f\n",
  745. pos_min, pos_max,
  746. dts_min / 90000.0, dts_max / 90000.0);
  747. #endif
  748. if (timestamp <= dts_min) {
  749. pos = pos_min;
  750. goto found;
  751. } else if (timestamp >= dts_max) {
  752. pos = pos_max;
  753. goto found;
  754. } else {
  755. /* interpolate position (better than dichotomy) */
  756. pos = (int64_t)((double)(pos_max - pos_min) *
  757. (double)(timestamp - dts_min) /
  758. (double)(dts_max - dts_min)) + pos_min;
  759. }
  760. #ifdef DEBUG_SEEK
  761. printf("pos=0x%llx\n", pos);
  762. #endif
  763. /* read the next timestamp */
  764. dts = mpegps_read_dts(s, stream_index, &pos, 1);
  765. /* check if we are lucky */
  766. if (dts == AV_NOPTS_VALUE) {
  767. /* should never happen */
  768. pos = pos_min;
  769. goto found;
  770. } else if (timestamp == dts) {
  771. goto found;
  772. } else if (timestamp < dts) {
  773. pos_max = pos;
  774. dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
  775. if (dts_max == AV_NOPTS_VALUE) {
  776. /* should never happen */
  777. break;
  778. } else if (timestamp >= dts_max) {
  779. pos = pos_max;
  780. goto found;
  781. }
  782. } else {
  783. pos_min = pos + 1;
  784. dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
  785. if (dts_min == AV_NOPTS_VALUE) {
  786. /* should never happen */
  787. goto found;
  788. } else if (timestamp <= dts_min) {
  789. goto found;
  790. }
  791. }
  792. }
  793. pos = pos_min;
  794. found:
  795. #ifdef DEBUG_SEEK
  796. pos_min = pos;
  797. dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
  798. pos_min++;
  799. dts_max = mpegps_read_dts(s, stream_index, &pos_min, 1);
  800. printf("pos=0x%llx %0.3f<=%0.3f<=%0.3f\n",
  801. pos, dts_min / 90000.0, timestamp / 90000.0, dts_max / 90000.0);
  802. #endif
  803. /* do the seek */
  804. url_fseek(&s->pb, pos, SEEK_SET);
  805. return 0;
  806. }
  807. #ifdef CONFIG_ENCODERS
  808. static AVOutputFormat mpeg1system_mux = {
  809. "mpeg",
  810. "MPEG1 System format",
  811. "video/mpeg",
  812. "mpg,mpeg",
  813. sizeof(MpegMuxContext),
  814. CODEC_ID_MP2,
  815. CODEC_ID_MPEG1VIDEO,
  816. mpeg_mux_init,
  817. mpeg_mux_write_packet,
  818. mpeg_mux_end,
  819. };
  820. static AVOutputFormat mpeg1vcd_mux = {
  821. "vcd",
  822. "MPEG1 System format (VCD)",
  823. "video/mpeg",
  824. NULL,
  825. sizeof(MpegMuxContext),
  826. CODEC_ID_MP2,
  827. CODEC_ID_MPEG1VIDEO,
  828. mpeg_mux_init,
  829. mpeg_mux_write_packet,
  830. mpeg_mux_end,
  831. };
  832. static AVOutputFormat mpeg2vob_mux = {
  833. "vob",
  834. "MPEG2 PS format (VOB)",
  835. "video/mpeg",
  836. "vob",
  837. sizeof(MpegMuxContext),
  838. CODEC_ID_MP2,
  839. CODEC_ID_MPEG1VIDEO,
  840. mpeg_mux_init,
  841. mpeg_mux_write_packet,
  842. mpeg_mux_end,
  843. };
  844. #endif //CONFIG_ENCODERS
  845. AVInputFormat mpegps_demux = {
  846. "mpeg",
  847. "MPEG PS format",
  848. sizeof(MpegDemuxContext),
  849. mpegps_probe,
  850. mpegps_read_header,
  851. mpegps_read_packet,
  852. mpegps_read_close,
  853. mpegps_read_seek,
  854. };
  855. int mpegps_init(void)
  856. {
  857. #ifdef CONFIG_ENCODERS
  858. av_register_output_format(&mpeg1system_mux);
  859. av_register_output_format(&mpeg1vcd_mux);
  860. av_register_output_format(&mpeg2vob_mux);
  861. #endif //CONFIG_ENCODERS
  862. av_register_input_format(&mpegps_demux);
  863. return 0;
  864. }