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.

659 lines
18KB

  1. /*
  2. * MPEG1 mux/demux
  3. * Copyright (c) 2000, 2001, 2002 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avformat.h"
  20. #include "tick.h"
  21. #define MAX_PAYLOAD_SIZE 4096
  22. #define NB_STREAMS 2
  23. typedef struct {
  24. UINT8 buffer[MAX_PAYLOAD_SIZE];
  25. int buffer_ptr;
  26. UINT8 id;
  27. int max_buffer_size; /* in bytes */
  28. int packet_number;
  29. INT64 pts;
  30. Ticker pts_ticker;
  31. INT64 start_pts;
  32. } StreamInfo;
  33. typedef struct {
  34. int packet_size; /* required packet size */
  35. int packet_data_max_size; /* maximum data size inside a packet */
  36. int packet_number;
  37. int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
  38. int system_header_freq;
  39. int mux_rate; /* bitrate in units of 50 bytes/s */
  40. /* stream info */
  41. int audio_bound;
  42. int video_bound;
  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. static int put_pack_header(AVFormatContext *ctx,
  58. UINT8 *buf, INT64 timestamp)
  59. {
  60. MpegMuxContext *s = ctx->priv_data;
  61. PutBitContext pb;
  62. init_put_bits(&pb, buf, 128, NULL, NULL);
  63. put_bits(&pb, 32, PACK_START_CODE);
  64. put_bits(&pb, 4, 0x2);
  65. put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
  66. put_bits(&pb, 1, 1);
  67. put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
  68. put_bits(&pb, 1, 1);
  69. put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
  70. put_bits(&pb, 1, 1);
  71. put_bits(&pb, 1, 1);
  72. put_bits(&pb, 22, s->mux_rate);
  73. put_bits(&pb, 1, 1);
  74. flush_put_bits(&pb);
  75. return pbBufPtr(&pb) - pb.buf;
  76. }
  77. static int put_system_header(AVFormatContext *ctx, UINT8 *buf)
  78. {
  79. MpegMuxContext *s = ctx->priv_data;
  80. int size, rate_bound, i, private_stream_coded, id;
  81. PutBitContext pb;
  82. init_put_bits(&pb, buf, 128, NULL, NULL);
  83. put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
  84. put_bits(&pb, 16, 0);
  85. put_bits(&pb, 1, 1);
  86. rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
  87. put_bits(&pb, 22, rate_bound);
  88. put_bits(&pb, 1, 1); /* marker */
  89. put_bits(&pb, 6, s->audio_bound);
  90. put_bits(&pb, 1, 1); /* variable bitrate */
  91. put_bits(&pb, 1, 1); /* non constrainted bit stream */
  92. put_bits(&pb, 1, 0); /* audio locked */
  93. put_bits(&pb, 1, 0); /* video locked */
  94. put_bits(&pb, 1, 1); /* marker */
  95. put_bits(&pb, 5, s->video_bound);
  96. put_bits(&pb, 8, 0xff); /* reserved byte */
  97. /* audio stream info */
  98. private_stream_coded = 0;
  99. for(i=0;i<ctx->nb_streams;i++) {
  100. StreamInfo *stream = ctx->streams[i]->priv_data;
  101. id = stream->id;
  102. if (id < 0xc0) {
  103. /* special case for private streams (AC3 use that) */
  104. if (private_stream_coded)
  105. continue;
  106. private_stream_coded = 1;
  107. id = 0xbd;
  108. }
  109. put_bits(&pb, 8, id); /* stream ID */
  110. put_bits(&pb, 2, 3);
  111. if (id < 0xe0) {
  112. /* audio */
  113. put_bits(&pb, 1, 0);
  114. put_bits(&pb, 13, stream->max_buffer_size / 128);
  115. } else {
  116. /* video */
  117. put_bits(&pb, 1, 1);
  118. put_bits(&pb, 13, stream->max_buffer_size / 1024);
  119. }
  120. }
  121. flush_put_bits(&pb);
  122. size = pbBufPtr(&pb) - pb.buf;
  123. /* patch packet size */
  124. buf[4] = (size - 6) >> 8;
  125. buf[5] = (size - 6) & 0xff;
  126. return size;
  127. }
  128. static int mpeg_mux_init(AVFormatContext *ctx)
  129. {
  130. MpegMuxContext *s = ctx->priv_data;
  131. int bitrate, i, mpa_id, mpv_id, ac3_id;
  132. AVStream *st;
  133. StreamInfo *stream;
  134. s->packet_number = 0;
  135. /* XXX: hardcoded */
  136. if (ctx->flags & AVF_FLAG_VCD)
  137. s->packet_size = 2324; /* VCD packet size */
  138. else
  139. s->packet_size = 2048;
  140. /* startcode(4) + length(2) + flags(1) */
  141. s->packet_data_max_size = s->packet_size - 7;
  142. s->audio_bound = 0;
  143. s->video_bound = 0;
  144. mpa_id = AUDIO_ID;
  145. ac3_id = 0x80;
  146. mpv_id = VIDEO_ID;
  147. for(i=0;i<ctx->nb_streams;i++) {
  148. st = ctx->streams[i];
  149. stream = av_mallocz(sizeof(StreamInfo));
  150. if (!stream)
  151. goto fail;
  152. st->priv_data = stream;
  153. switch(st->codec.codec_type) {
  154. case CODEC_TYPE_AUDIO:
  155. if (st->codec.codec_id == CODEC_ID_AC3)
  156. stream->id = ac3_id++;
  157. else
  158. stream->id = mpa_id++;
  159. stream->max_buffer_size = 4 * 1024;
  160. s->audio_bound++;
  161. break;
  162. case CODEC_TYPE_VIDEO:
  163. stream->id = mpv_id++;
  164. stream->max_buffer_size = 46 * 1024;
  165. s->video_bound++;
  166. break;
  167. default:
  168. abort();
  169. }
  170. }
  171. /* we increase slightly the bitrate to take into account the
  172. headers. XXX: compute it exactly */
  173. bitrate = 2000;
  174. for(i=0;i<ctx->nb_streams;i++) {
  175. st = ctx->streams[i];
  176. bitrate += st->codec.bit_rate;
  177. }
  178. s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
  179. if (ctx->flags & AVF_FLAG_VCD)
  180. /* every packet */
  181. s->pack_header_freq = 1;
  182. else
  183. /* every 2 seconds */
  184. s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
  185. if (ctx->flags & AVF_FLAG_VCD)
  186. /* every 40 packets, this is my invention */
  187. s->system_header_freq = s->pack_header_freq * 40;
  188. else
  189. /* every 10 seconds */
  190. s->system_header_freq = s->pack_header_freq * 5;
  191. for(i=0;i<ctx->nb_streams;i++) {
  192. stream = ctx->streams[i]->priv_data;
  193. stream->buffer_ptr = 0;
  194. stream->packet_number = 0;
  195. stream->pts = 0;
  196. stream->start_pts = -1;
  197. st = ctx->streams[i];
  198. switch (st->codec.codec_type) {
  199. case CODEC_TYPE_AUDIO:
  200. ticker_init(&stream->pts_ticker,
  201. st->codec.sample_rate,
  202. 90000 * st->codec.frame_size);
  203. break;
  204. case CODEC_TYPE_VIDEO:
  205. ticker_init(&stream->pts_ticker,
  206. st->codec.frame_rate,
  207. 90000 * FRAME_RATE_BASE);
  208. break;
  209. default:
  210. abort();
  211. }
  212. }
  213. return 0;
  214. fail:
  215. for(i=0;i<ctx->nb_streams;i++) {
  216. av_free(ctx->streams[i]->priv_data);
  217. }
  218. return -ENOMEM;
  219. }
  220. /* flush the packet on stream stream_index */
  221. static void flush_packet(AVFormatContext *ctx, int stream_index, int last_pkt)
  222. {
  223. MpegMuxContext *s = ctx->priv_data;
  224. StreamInfo *stream = ctx->streams[stream_index]->priv_data;
  225. UINT8 *buf_ptr;
  226. int size, payload_size, startcode, id, len, stuffing_size, i;
  227. INT64 timestamp;
  228. UINT8 buffer[128];
  229. int last = last_pkt ? 4 : 0;
  230. id = stream->id;
  231. timestamp = stream->start_pts;
  232. #if 0
  233. printf("packet ID=%2x PTS=%0.3f\n",
  234. id, timestamp / 90000.0);
  235. #endif
  236. buf_ptr = buffer;
  237. if (((s->packet_number % s->pack_header_freq) == 0)) {
  238. /* output pack and systems header if needed */
  239. size = put_pack_header(ctx, buf_ptr, timestamp);
  240. buf_ptr += size;
  241. if ((s->packet_number % s->system_header_freq) == 0) {
  242. size = put_system_header(ctx, buf_ptr);
  243. buf_ptr += size;
  244. }
  245. }
  246. size = buf_ptr - buffer;
  247. put_buffer(&ctx->pb, buffer, size);
  248. /* packet header */
  249. payload_size = s->packet_size - (size + 6 + 5 + last);
  250. if (id < 0xc0) {
  251. startcode = PRIVATE_STREAM_1;
  252. payload_size -= 4;
  253. } else {
  254. startcode = 0x100 + id;
  255. }
  256. stuffing_size = payload_size - stream->buffer_ptr;
  257. if (stuffing_size < 0)
  258. stuffing_size = 0;
  259. put_be32(&ctx->pb, startcode);
  260. put_be16(&ctx->pb, payload_size + 5);
  261. /* stuffing */
  262. for(i=0;i<stuffing_size;i++)
  263. put_byte(&ctx->pb, 0xff);
  264. /* presentation time stamp */
  265. put_byte(&ctx->pb,
  266. (0x02 << 4) |
  267. (((timestamp >> 30) & 0x07) << 1) |
  268. 1);
  269. put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1));
  270. put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1));
  271. if (startcode == PRIVATE_STREAM_1) {
  272. put_byte(&ctx->pb, id);
  273. if (id >= 0x80 && id <= 0xbf) {
  274. /* XXX: need to check AC3 spec */
  275. put_byte(&ctx->pb, 1);
  276. put_byte(&ctx->pb, 0);
  277. put_byte(&ctx->pb, 2);
  278. }
  279. }
  280. if (last_pkt) {
  281. put_be32(&ctx->pb, ISO_11172_END_CODE);
  282. }
  283. /* output data */
  284. put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
  285. put_flush_packet(&ctx->pb);
  286. /* preserve remaining data */
  287. len = stream->buffer_ptr - payload_size;
  288. if (len < 0)
  289. len = 0;
  290. memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
  291. stream->buffer_ptr = len;
  292. s->packet_number++;
  293. stream->packet_number++;
  294. stream->start_pts = -1;
  295. }
  296. static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
  297. UINT8 *buf, int size, int force_pts)
  298. {
  299. MpegMuxContext *s = ctx->priv_data;
  300. AVStream *st = ctx->streams[stream_index];
  301. StreamInfo *stream = st->priv_data;
  302. int len;
  303. while (size > 0) {
  304. /* set pts */
  305. if (stream->start_pts == -1) {
  306. if (force_pts)
  307. stream->pts = force_pts;
  308. stream->start_pts = stream->pts;
  309. }
  310. len = s->packet_data_max_size - stream->buffer_ptr;
  311. if (len > size)
  312. len = size;
  313. memcpy(stream->buffer + stream->buffer_ptr, buf, len);
  314. stream->buffer_ptr += len;
  315. buf += len;
  316. size -= len;
  317. while (stream->buffer_ptr >= s->packet_data_max_size) {
  318. /* output the packet */
  319. if (stream->start_pts == -1)
  320. stream->start_pts = stream->pts;
  321. flush_packet(ctx, stream_index, 0);
  322. }
  323. }
  324. stream->pts += ticker_tick(&stream->pts_ticker, 1);
  325. //if (st->codec.codec_type == CODEC_TYPE_VIDEO)
  326. // fprintf(stderr,"\nVideo PTS: %6lld", stream->pts);
  327. //else
  328. // fprintf(stderr,"\nAudio PTS: %6lld", stream->pts);
  329. return 0;
  330. }
  331. static int mpeg_mux_end(AVFormatContext *ctx)
  332. {
  333. StreamInfo *stream;
  334. int i;
  335. /* flush each packet */
  336. for(i=0;i<ctx->nb_streams;i++) {
  337. stream = ctx->streams[i]->priv_data;
  338. if (stream->buffer_ptr > 0) {
  339. if (i == (ctx->nb_streams - 1))
  340. flush_packet(ctx, i, 1);
  341. else
  342. flush_packet(ctx, i, 0);
  343. }
  344. }
  345. /* write the end header */
  346. //put_be32(&ctx->pb, ISO_11172_END_CODE);
  347. //put_flush_packet(&ctx->pb);
  348. return 0;
  349. }
  350. /*********************************************/
  351. /* demux code */
  352. #define MAX_SYNC_SIZE 100000
  353. static int mpegps_probe(AVProbeData *p)
  354. {
  355. int code, c, i;
  356. code = 0xff;
  357. /* we search the first start code. If it is a packet start code,
  358. then we decide it is mpeg ps. We do not send highest value to
  359. give a chance to mpegts */
  360. for(i=0;i<p->buf_size;i++) {
  361. c = p->buf[i];
  362. code = (code << 8) | c;
  363. if ((code & 0xffffff00) == 0x100) {
  364. if (code == PACK_START_CODE ||
  365. code == SYSTEM_HEADER_START_CODE ||
  366. (code >= 0x1e0 && code <= 0x1ef) ||
  367. (code >= 0x1c0 && code <= 0x1df) ||
  368. code == PRIVATE_STREAM_2 ||
  369. code == PROGRAM_STREAM_MAP ||
  370. code == PRIVATE_STREAM_1 ||
  371. code == PADDING_STREAM)
  372. return AVPROBE_SCORE_MAX - 1;
  373. else
  374. return 0;
  375. }
  376. }
  377. return 0;
  378. }
  379. typedef struct MpegDemuxContext {
  380. int header_state;
  381. } MpegDemuxContext;
  382. static int find_start_code(ByteIOContext *pb, int *size_ptr,
  383. UINT32 *header_state)
  384. {
  385. unsigned int state, v;
  386. int val, n;
  387. state = *header_state;
  388. n = *size_ptr;
  389. while (n > 0) {
  390. if (url_feof(pb))
  391. break;
  392. v = get_byte(pb);
  393. n--;
  394. if (state == 0x000001) {
  395. state = ((state << 8) | v) & 0xffffff;
  396. val = state;
  397. goto found;
  398. }
  399. state = ((state << 8) | v) & 0xffffff;
  400. }
  401. val = -1;
  402. found:
  403. *header_state = state;
  404. *size_ptr = n;
  405. return val;
  406. }
  407. static int mpegps_read_header(AVFormatContext *s,
  408. AVFormatParameters *ap)
  409. {
  410. MpegDemuxContext *m = s->priv_data;
  411. m->header_state = 0xff;
  412. /* no need to do more */
  413. return 0;
  414. }
  415. static INT64 get_pts(ByteIOContext *pb, int c)
  416. {
  417. INT64 pts;
  418. int val;
  419. if (c < 0)
  420. c = get_byte(pb);
  421. pts = (INT64)((c >> 1) & 0x07) << 30;
  422. val = get_be16(pb);
  423. pts |= (INT64)(val >> 1) << 15;
  424. val = get_be16(pb);
  425. pts |= (INT64)(val >> 1);
  426. return pts;
  427. }
  428. static int mpegps_read_packet(AVFormatContext *s,
  429. AVPacket *pkt)
  430. {
  431. MpegDemuxContext *m = s->priv_data;
  432. AVStream *st;
  433. int len, size, startcode, i, c, flags, header_len, type, codec_id;
  434. INT64 pts, dts;
  435. /* next start code (should be immediately after */
  436. redo:
  437. m->header_state = 0xff;
  438. size = MAX_SYNC_SIZE;
  439. startcode = find_start_code(&s->pb, &size, &m->header_state);
  440. //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
  441. if (startcode < 0)
  442. return -EIO;
  443. if (startcode == PACK_START_CODE)
  444. goto redo;
  445. if (startcode == SYSTEM_HEADER_START_CODE)
  446. goto redo;
  447. if (startcode == PADDING_STREAM ||
  448. startcode == PRIVATE_STREAM_2) {
  449. /* skip them */
  450. len = get_be16(&s->pb);
  451. url_fskip(&s->pb, len);
  452. goto redo;
  453. }
  454. /* find matching stream */
  455. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  456. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  457. (startcode == 0x1bd)))
  458. goto redo;
  459. len = get_be16(&s->pb);
  460. pts = 0;
  461. dts = 0;
  462. /* stuffing */
  463. for(;;) {
  464. c = get_byte(&s->pb);
  465. len--;
  466. /* XXX: for mpeg1, should test only bit 7 */
  467. if (c != 0xff)
  468. break;
  469. }
  470. if ((c & 0xc0) == 0x40) {
  471. /* buffer scale & size */
  472. get_byte(&s->pb);
  473. c = get_byte(&s->pb);
  474. len -= 2;
  475. }
  476. if ((c & 0xf0) == 0x20) {
  477. pts = get_pts(&s->pb, c);
  478. len -= 4;
  479. dts = pts;
  480. } else if ((c & 0xf0) == 0x30) {
  481. pts = get_pts(&s->pb, c);
  482. dts = get_pts(&s->pb, -1);
  483. len -= 9;
  484. } else if ((c & 0xc0) == 0x80) {
  485. /* mpeg 2 PES */
  486. if ((c & 0x30) != 0) {
  487. fprintf(stderr, "Encrypted multiplex not handled\n");
  488. return -EIO;
  489. }
  490. flags = get_byte(&s->pb);
  491. header_len = get_byte(&s->pb);
  492. len -= 2;
  493. if (header_len > len)
  494. goto redo;
  495. if ((flags & 0xc0) == 0x40) {
  496. pts = get_pts(&s->pb, -1);
  497. dts = pts;
  498. header_len -= 5;
  499. len -= 5;
  500. } if ((flags & 0xc0) == 0xc0) {
  501. pts = get_pts(&s->pb, -1);
  502. dts = get_pts(&s->pb, -1);
  503. header_len -= 10;
  504. len -= 10;
  505. }
  506. len -= header_len;
  507. while (header_len > 0) {
  508. get_byte(&s->pb);
  509. header_len--;
  510. }
  511. }
  512. if (startcode == 0x1bd) {
  513. startcode = get_byte(&s->pb);
  514. len--;
  515. if (startcode >= 0x80 && startcode <= 0xbf) {
  516. /* audio: skip header */
  517. get_byte(&s->pb);
  518. get_byte(&s->pb);
  519. get_byte(&s->pb);
  520. len -= 3;
  521. }
  522. }
  523. /* now find stream */
  524. for(i=0;i<s->nb_streams;i++) {
  525. st = s->streams[i];
  526. if (st->id == startcode)
  527. goto found;
  528. }
  529. /* no stream found: add a new stream */
  530. st = av_new_stream(s, startcode);
  531. if (!st)
  532. goto skip;
  533. if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  534. type = CODEC_TYPE_VIDEO;
  535. codec_id = CODEC_ID_MPEG1VIDEO;
  536. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  537. type = CODEC_TYPE_AUDIO;
  538. codec_id = CODEC_ID_MP2;
  539. } else if (startcode >= 0x80 && startcode <= 0x9f) {
  540. type = CODEC_TYPE_AUDIO;
  541. codec_id = CODEC_ID_AC3;
  542. } else {
  543. skip:
  544. /* skip packet */
  545. url_fskip(&s->pb, len);
  546. goto redo;
  547. }
  548. st->codec.codec_type = type;
  549. st->codec.codec_id = codec_id;
  550. found:
  551. av_new_packet(pkt, len);
  552. //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
  553. // (float)pts/90000, len);
  554. get_buffer(&s->pb, pkt->data, pkt->size);
  555. pkt->pts = pts;
  556. pkt->stream_index = st->index;
  557. return 0;
  558. }
  559. static int mpegps_read_close(AVFormatContext *s)
  560. {
  561. return 0;
  562. }
  563. static AVOutputFormat mpegps_mux = {
  564. "mpeg",
  565. "MPEG PS format",
  566. "video/x-mpeg",
  567. "mpg,mpeg,vob",
  568. sizeof(MpegMuxContext),
  569. CODEC_ID_MP2,
  570. CODEC_ID_MPEG1VIDEO,
  571. mpeg_mux_init,
  572. mpeg_mux_write_packet,
  573. mpeg_mux_end,
  574. };
  575. static AVInputFormat mpegps_demux = {
  576. "mpeg",
  577. "MPEG PS format",
  578. sizeof(MpegDemuxContext),
  579. mpegps_probe,
  580. mpegps_read_header,
  581. mpegps_read_packet,
  582. mpegps_read_close,
  583. flags: AVFMT_NOHEADER,
  584. };
  585. int mpegps_init(void)
  586. {
  587. av_register_output_format(&mpegps_mux);
  588. av_register_input_format(&mpegps_demux);
  589. return 0;
  590. }