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.

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