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.

1025 lines
29KB

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