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.

660 lines
18KB

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