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.

844 lines
24KB

  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 mpeg_mux_check_packet(AVFormatContext *s, int *size);
  55. static int put_pack_header(AVFormatContext *ctx,
  56. UINT8 *buf, INT64 timestamp)
  57. {
  58. MpegMuxContext *s = ctx->priv_data;
  59. PutBitContext pb;
  60. init_put_bits(&pb, buf, 128, NULL, NULL);
  61. put_bits(&pb, 32, PACK_START_CODE);
  62. put_bits(&pb, 4, 0x2);
  63. put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
  64. put_bits(&pb, 1, 1);
  65. put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
  66. put_bits(&pb, 1, 1);
  67. put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
  68. put_bits(&pb, 1, 1);
  69. put_bits(&pb, 1, 1);
  70. put_bits(&pb, 22, s->mux_rate);
  71. put_bits(&pb, 1, 1);
  72. flush_put_bits(&pb);
  73. return pbBufPtr(&pb) - pb.buf;
  74. }
  75. static int put_system_header(AVFormatContext *ctx, UINT8 *buf)
  76. {
  77. MpegMuxContext *s = ctx->priv_data;
  78. int size, rate_bound, i, private_stream_coded, id;
  79. PutBitContext pb;
  80. init_put_bits(&pb, buf, 128, NULL, NULL);
  81. put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
  82. put_bits(&pb, 16, 0);
  83. put_bits(&pb, 1, 1);
  84. rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
  85. put_bits(&pb, 22, rate_bound);
  86. put_bits(&pb, 1, 1); /* marker */
  87. put_bits(&pb, 6, s->audio_bound);
  88. put_bits(&pb, 1, 1); /* variable bitrate */
  89. put_bits(&pb, 1, 1); /* non constrainted bit stream */
  90. put_bits(&pb, 1, 0); /* audio locked */
  91. put_bits(&pb, 1, 0); /* video locked */
  92. put_bits(&pb, 1, 1); /* marker */
  93. put_bits(&pb, 5, s->video_bound);
  94. put_bits(&pb, 8, 0xff); /* reserved byte */
  95. /* audio stream info */
  96. private_stream_coded = 0;
  97. for(i=0;i<ctx->nb_streams;i++) {
  98. StreamInfo *stream = ctx->streams[i]->priv_data;
  99. id = stream->id;
  100. if (id < 0xc0) {
  101. /* special case for private streams (AC3 use that) */
  102. if (private_stream_coded)
  103. continue;
  104. private_stream_coded = 1;
  105. id = 0xbd;
  106. }
  107. put_bits(&pb, 8, id); /* stream ID */
  108. put_bits(&pb, 2, 3);
  109. if (id < 0xe0) {
  110. /* audio */
  111. put_bits(&pb, 1, 0);
  112. put_bits(&pb, 13, stream->max_buffer_size / 128);
  113. } else {
  114. /* video */
  115. put_bits(&pb, 1, 1);
  116. put_bits(&pb, 13, stream->max_buffer_size / 1024);
  117. }
  118. }
  119. flush_put_bits(&pb);
  120. size = pbBufPtr(&pb) - pb.buf;
  121. /* patch packet size */
  122. buf[4] = (size - 6) >> 8;
  123. buf[5] = (size - 6) & 0xff;
  124. return size;
  125. }
  126. static int mpeg_mux_init(AVFormatContext *ctx)
  127. {
  128. MpegMuxContext *s;
  129. int bitrate, i, mpa_id, mpv_id, ac3_id;
  130. AVStream *st;
  131. StreamInfo *stream;
  132. s = malloc(sizeof(MpegMuxContext));
  133. if (!s)
  134. return -1;
  135. memset(s, 0, sizeof(MpegMuxContext));
  136. ctx->priv_data = s;
  137. s->packet_number = 0;
  138. /* XXX: hardcoded */
  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. }
  168. }
  169. /* we increase slightly the bitrate to take into account the
  170. headers. XXX: compute it exactly */
  171. bitrate = 2000;
  172. for(i=0;i<ctx->nb_streams;i++) {
  173. st = ctx->streams[i];
  174. bitrate += st->codec.bit_rate;
  175. }
  176. s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
  177. /* every 2 seconds */
  178. s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
  179. /* every 10 seconds */
  180. s->system_header_freq = s->pack_header_freq * 5;
  181. for(i=0;i<ctx->nb_streams;i++) {
  182. stream = ctx->streams[i]->priv_data;
  183. stream->buffer_ptr = 0;
  184. stream->packet_number = 0;
  185. stream->pts = 0;
  186. stream->start_pts = -1;
  187. }
  188. return 0;
  189. fail:
  190. for(i=0;i<ctx->nb_streams;i++) {
  191. free(ctx->streams[i]->priv_data);
  192. }
  193. free(s);
  194. return -ENOMEM;
  195. }
  196. /* flush the packet on stream stream_index */
  197. static void flush_packet(AVFormatContext *ctx, int stream_index)
  198. {
  199. MpegMuxContext *s = ctx->priv_data;
  200. StreamInfo *stream = ctx->streams[stream_index]->priv_data;
  201. UINT8 *buf_ptr;
  202. int size, payload_size, startcode, id, len, stuffing_size, i;
  203. INT64 timestamp;
  204. UINT8 buffer[128];
  205. id = stream->id;
  206. timestamp = stream->start_pts;
  207. #if 0
  208. printf("packet ID=%2x PTS=%0.3f\n",
  209. id, timestamp / 90000.0);
  210. #endif
  211. buf_ptr = buffer;
  212. if ((s->packet_number % s->pack_header_freq) == 0) {
  213. /* output pack and systems header if needed */
  214. size = put_pack_header(ctx, buf_ptr, timestamp);
  215. buf_ptr += size;
  216. if ((s->packet_number % s->system_header_freq) == 0) {
  217. size = put_system_header(ctx, buf_ptr);
  218. buf_ptr += size;
  219. }
  220. }
  221. size = buf_ptr - buffer;
  222. put_buffer(&ctx->pb, buffer, size);
  223. /* packet header */
  224. payload_size = s->packet_size - (size + 6 + 5);
  225. if (id < 0xc0) {
  226. startcode = PRIVATE_STREAM_1;
  227. payload_size -= 4;
  228. } else {
  229. startcode = 0x100 + id;
  230. }
  231. stuffing_size = payload_size - stream->buffer_ptr;
  232. if (stuffing_size < 0)
  233. stuffing_size = 0;
  234. put_be32(&ctx->pb, startcode);
  235. put_be16(&ctx->pb, payload_size + 5);
  236. /* stuffing */
  237. for(i=0;i<stuffing_size;i++)
  238. put_byte(&ctx->pb, 0xff);
  239. /* presentation time stamp */
  240. put_byte(&ctx->pb,
  241. (0x02 << 4) |
  242. (((timestamp >> 30) & 0x07) << 1) |
  243. 1);
  244. put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1));
  245. put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1));
  246. if (startcode == PRIVATE_STREAM_1) {
  247. put_byte(&ctx->pb, id);
  248. if (id >= 0x80 && id <= 0xbf) {
  249. /* XXX: need to check AC3 spec */
  250. put_byte(&ctx->pb, 1);
  251. put_byte(&ctx->pb, 0);
  252. put_byte(&ctx->pb, 2);
  253. }
  254. }
  255. /* output data */
  256. put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
  257. put_flush_packet(&ctx->pb);
  258. /* preserve remaining data */
  259. len = stream->buffer_ptr - payload_size;
  260. if (len < 0)
  261. len = 0;
  262. memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
  263. stream->buffer_ptr = len;
  264. s->packet_number++;
  265. stream->packet_number++;
  266. stream->start_pts = -1;
  267. }
  268. static int mpeg_mux_write_packet(AVFormatContext *ctx,
  269. int stream_index, UINT8 *buf, int size)
  270. {
  271. MpegMuxContext *s = ctx->priv_data;
  272. AVStream *st = ctx->streams[stream_index];
  273. StreamInfo *stream = st->priv_data;
  274. int len;
  275. while (size > 0) {
  276. /* set pts */
  277. if (stream->start_pts == -1)
  278. stream->start_pts = stream->pts * 90000.0;
  279. len = s->packet_data_max_size - stream->buffer_ptr;
  280. if (len > size)
  281. len = size;
  282. memcpy(stream->buffer + stream->buffer_ptr, buf, len);
  283. stream->buffer_ptr += len;
  284. buf += len;
  285. size -= len;
  286. while (stream->buffer_ptr >= s->packet_data_max_size) {
  287. /* output the packet */
  288. if (stream->start_pts == -1)
  289. stream->start_pts = stream->pts * 90000.0;
  290. flush_packet(ctx, stream_index);
  291. }
  292. }
  293. if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
  294. stream->pts += (float)st->codec.frame_size / st->codec.sample_rate;
  295. } else {
  296. stream->pts += FRAME_RATE_BASE / (float)st->codec.frame_rate;
  297. }
  298. return 0;
  299. }
  300. static int mpeg_mux_end(AVFormatContext *ctx)
  301. {
  302. StreamInfo *stream;
  303. int i;
  304. /* flush each packet */
  305. for(i=0;i<ctx->nb_streams;i++) {
  306. stream = ctx->streams[i]->priv_data;
  307. if (stream->buffer_ptr > 0)
  308. flush_packet(ctx, i);
  309. }
  310. /* write the end header */
  311. put_be32(&ctx->pb, ISO_11172_END_CODE);
  312. put_flush_packet(&ctx->pb);
  313. return 0;
  314. }
  315. /*********************************************/
  316. /* demux code */
  317. #define MAX_SYNC_SIZE 100000
  318. typedef struct MpegDemuxContext {
  319. int header_state;
  320. int mux_rate; /* 50 byte/s unit */
  321. } MpegDemuxContext;
  322. static int find_start_code(ByteIOContext *pb, int *size_ptr,
  323. UINT32 *header_state)
  324. {
  325. unsigned int state, v;
  326. int val, n;
  327. state = *header_state;
  328. n = *size_ptr;
  329. while (n > 0) {
  330. if (url_feof(pb))
  331. break;
  332. v = get_byte(pb);
  333. n--;
  334. if (state == 0x000001) {
  335. state = ((state << 8) | v) & 0xffffff;
  336. val = state;
  337. goto found;
  338. }
  339. state = ((state << 8) | v) & 0xffffff;
  340. }
  341. val = -1;
  342. found:
  343. *header_state = state;
  344. *size_ptr = n;
  345. return val;
  346. }
  347. static int check_stream_id(AVFormatContext *s, int c_id)
  348. {
  349. AVStream *st;
  350. int i;
  351. for(i = 0;i < s->nb_streams;i++) {
  352. st = s->streams[i];
  353. if (st && st->id == c_id)
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. static int mpeg_mux_read_header(AVFormatContext *s,
  359. AVFormatParameters *ap)
  360. {
  361. MpegDemuxContext *m;
  362. int size, startcode, c, rate_bound, audio_bound, video_bound, mux_rate, val;
  363. int codec_id, n, i, type;
  364. AVStream *st;
  365. offset_t start_pos;
  366. m = av_mallocz(sizeof(MpegDemuxContext));
  367. if (!m)
  368. return -ENOMEM;
  369. s->priv_data = m;
  370. /* search first pack header */
  371. m->header_state = 0xff;
  372. size = MAX_SYNC_SIZE;
  373. start_pos = url_ftell(&s->pb); /* remember this pos */
  374. for(;;) {
  375. while (size > 0) {
  376. startcode = find_start_code(&s->pb, &size, &m->header_state);
  377. if (startcode == PACK_START_CODE)
  378. goto found;
  379. }
  380. /* System Header not found find streams searching through file */
  381. fprintf(stderr,"libav: MPEG-PS System Header not found!\n");
  382. url_fseek(&s->pb, start_pos, SEEK_SET);
  383. video_bound = 0;
  384. audio_bound = 0;
  385. c = 0;
  386. s->nb_streams = 0;
  387. size = 15*MAX_SYNC_SIZE;
  388. while (size > 0) {
  389. type = 0;
  390. codec_id = 0;
  391. n = 0;
  392. startcode = find_start_code(&s->pb, &size, &m->header_state);
  393. //fprintf(stderr,"\nstartcode: %x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
  394. if (startcode == 0x1bd) {
  395. url_fseek(&s->pb, -4, SEEK_CUR);
  396. size += 4;
  397. startcode = mpeg_mux_check_packet(s, &size);
  398. //fprintf(stderr,"\nstartcode: %x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
  399. if (startcode >= 0x80 && startcode <= 0x9f && !check_stream_id(s, startcode)) {
  400. //fprintf(stderr,"Found AC3 stream ID: 0x%x\n", startcode);
  401. type = CODEC_TYPE_AUDIO;
  402. codec_id = CODEC_ID_AC3;
  403. audio_bound++;
  404. n = 1;
  405. c = startcode;
  406. }
  407. } else if (startcode == 0x1e0 && !check_stream_id(s, startcode)) {
  408. //fprintf(stderr,"Found MPEGVIDEO stream ID: 0x%x\n", startcode);
  409. type = CODEC_TYPE_VIDEO;
  410. codec_id = CODEC_ID_MPEG1VIDEO;
  411. n = 1;
  412. c = startcode;
  413. video_bound++;
  414. } /*else if (startcode >= 0x1c0 && startcode <= 0x1df && !check_stream_id(s, startcode)) {
  415. fprintf(stderr,"Found MPEGAUDIO stream ID: 0x%x\n", startcode);
  416. type = CODEC_TYPE_AUDIO;
  417. codec_id = CODEC_ID_MP2;
  418. n = 1;
  419. c = startcode;
  420. audio_bound++;
  421. } */
  422. for(i=0;i<n;i++) {
  423. st = av_mallocz(sizeof(AVStream));
  424. if (!st)
  425. return -ENOMEM;
  426. s->streams[s->nb_streams++] = st;
  427. st->id = c;
  428. st->codec.codec_type = type;
  429. st->codec.codec_id = codec_id;
  430. }
  431. }
  432. if (video_bound || audio_bound) {
  433. url_fseek(&s->pb, start_pos, SEEK_SET);
  434. return 0;
  435. } else
  436. return -ENODATA;
  437. found:
  438. /* search system header just after pack header */
  439. /* parse pack header */
  440. get_byte(&s->pb); /* ts1 */
  441. get_be16(&s->pb); /* ts2 */
  442. get_be16(&s->pb); /* ts3 */
  443. mux_rate = get_byte(&s->pb) << 16;
  444. mux_rate |= get_byte(&s->pb) << 8;
  445. mux_rate |= get_byte(&s->pb);
  446. mux_rate &= (1 << 22) - 1;
  447. m->mux_rate = mux_rate;
  448. startcode = find_start_code(&s->pb, &size, &m->header_state);
  449. if (startcode == SYSTEM_HEADER_START_CODE)
  450. break;
  451. }
  452. size = get_be16(&s->pb);
  453. rate_bound = get_byte(&s->pb) << 16;
  454. rate_bound |= get_byte(&s->pb) << 8;
  455. rate_bound |= get_byte(&s->pb);
  456. rate_bound = (rate_bound >> 1) & ((1 << 22) - 1);
  457. audio_bound = get_byte(&s->pb) >> 2;
  458. video_bound = get_byte(&s->pb) & 0x1f;
  459. get_byte(&s->pb); /* reserved byte */
  460. #if 0
  461. printf("mux_rate=%d kbit/s\n", (m->mux_rate * 50 * 8) / 1000);
  462. printf("rate_bound=%d\n", rate_bound);
  463. printf("audio_bound=%d\n", audio_bound);
  464. printf("video_bound=%d\n", video_bound);
  465. #endif
  466. size -= 6;
  467. s->nb_streams = 0;
  468. while (size > 0) {
  469. c = get_byte(&s->pb);
  470. size--;
  471. if ((c & 0x80) == 0)
  472. break;
  473. val = get_be16(&s->pb);
  474. size -= 2;
  475. if (c >= 0xc0 && c <= 0xdf) {
  476. /* mpeg audio stream */
  477. type = CODEC_TYPE_AUDIO;
  478. codec_id = CODEC_ID_MP2;
  479. n = 1;
  480. c = c | 0x100;
  481. } else if (c >= 0xe0 && c <= 0xef) {
  482. type = CODEC_TYPE_VIDEO;
  483. codec_id = CODEC_ID_MPEG1VIDEO;
  484. n = 1;
  485. c = c | 0x100;
  486. } else if (c == 0xb8) {
  487. /* all audio streams */
  488. /* XXX: hack for DVD: we force AC3, although we do not
  489. know that this codec will be used */
  490. type = CODEC_TYPE_AUDIO;
  491. codec_id = CODEC_ID_AC3;
  492. /* XXX: Another hack for DVD: it seems, that AC3 streams
  493. aren't signaled on audio_bound on some DVDs (Matrix) */
  494. if (audio_bound == 0)
  495. audio_bound++;
  496. n = audio_bound;
  497. c = 0x80;
  498. //c = 0x1c0;
  499. } else if (c == 0xb9) {
  500. /* all video streams */
  501. type = CODEC_TYPE_VIDEO;
  502. codec_id = CODEC_ID_MPEG1VIDEO;
  503. n = video_bound;
  504. c = 0x1e0;
  505. } else {
  506. type = 0;
  507. codec_id = 0;
  508. n = 0;
  509. }
  510. for(i=0;i<n;i++) {
  511. st = av_mallocz(sizeof(AVStream));
  512. if (!st)
  513. return -ENOMEM;
  514. s->streams[s->nb_streams++] = st;
  515. st->id = c + i;
  516. st->codec.codec_type = type;
  517. st->codec.codec_id = codec_id;
  518. }
  519. }
  520. return 0;
  521. }
  522. static INT64 get_pts(ByteIOContext *pb, int c)
  523. {
  524. INT64 pts;
  525. int val;
  526. if (c < 0)
  527. c = get_byte(pb);
  528. pts = (INT64)((c >> 1) & 0x07) << 30;
  529. val = get_be16(pb);
  530. pts |= (INT64)(val >> 1) << 15;
  531. val = get_be16(pb);
  532. pts |= (INT64)(val >> 1);
  533. return pts;
  534. }
  535. static int mpeg_mux_read_packet(AVFormatContext *s,
  536. AVPacket *pkt)
  537. {
  538. MpegDemuxContext *m = s->priv_data;
  539. AVStream *st;
  540. int len, size, startcode, i, c, flags, header_len;
  541. INT64 pts, dts;
  542. /* next start code (should be immediately after */
  543. redo:
  544. m->header_state = 0xff;
  545. size = MAX_SYNC_SIZE;
  546. startcode = find_start_code(&s->pb, &size, &m->header_state);
  547. //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
  548. if (startcode < 0)
  549. return -EIO;
  550. if (startcode == PACK_START_CODE)
  551. goto redo;
  552. if (startcode == SYSTEM_HEADER_START_CODE)
  553. goto redo;
  554. if (startcode == PADDING_STREAM ||
  555. startcode == PRIVATE_STREAM_2) {
  556. /* skip them */
  557. len = get_be16(&s->pb);
  558. url_fskip(&s->pb, len);
  559. goto redo;
  560. }
  561. /* find matching stream */
  562. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  563. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  564. (startcode == 0x1bd)))
  565. goto redo;
  566. len = get_be16(&s->pb);
  567. pts = 0;
  568. dts = 0;
  569. /* stuffing */
  570. for(;;) {
  571. c = get_byte(&s->pb);
  572. len--;
  573. /* XXX: for mpeg1, should test only bit 7 */
  574. if (c != 0xff)
  575. break;
  576. }
  577. if ((c & 0xc0) == 0x40) {
  578. /* buffer scale & size */
  579. get_byte(&s->pb);
  580. c = get_byte(&s->pb);
  581. len -= 2;
  582. }
  583. if ((c & 0xf0) == 0x20) {
  584. pts = get_pts(&s->pb, c);
  585. len -= 4;
  586. dts = pts;
  587. } else if ((c & 0xf0) == 0x30) {
  588. pts = get_pts(&s->pb, c);
  589. dts = get_pts(&s->pb, -1);
  590. len -= 9;
  591. } else if ((c & 0xc0) == 0x80) {
  592. /* mpeg 2 PES */
  593. if ((c & 0x30) != 0) {
  594. fprintf(stderr, "Encrypted multiplex not handled\n");
  595. return -EIO;
  596. }
  597. flags = get_byte(&s->pb);
  598. header_len = get_byte(&s->pb);
  599. len -= 2;
  600. if (header_len > len)
  601. goto redo;
  602. if ((flags & 0xc0) == 0x40) {
  603. pts = get_pts(&s->pb, -1);
  604. dts = pts;
  605. header_len -= 5;
  606. len -= 5;
  607. } if ((flags & 0xc0) == 0xc0) {
  608. pts = get_pts(&s->pb, -1);
  609. dts = get_pts(&s->pb, -1);
  610. header_len -= 10;
  611. len -= 10;
  612. }
  613. len -= header_len;
  614. while (header_len > 0) {
  615. get_byte(&s->pb);
  616. header_len--;
  617. }
  618. }
  619. if (startcode == 0x1bd) {
  620. startcode = get_byte(&s->pb);
  621. len--;
  622. if (startcode >= 0x80 && startcode <= 0xbf) {
  623. /* audio: skip header */
  624. get_byte(&s->pb);
  625. get_byte(&s->pb);
  626. get_byte(&s->pb);
  627. len -= 3;
  628. }
  629. }
  630. /* now find stream */
  631. for(i=0;i<s->nb_streams;i++) {
  632. st = s->streams[i];
  633. if (st->id == startcode)
  634. goto found;
  635. }
  636. /* skip packet */
  637. url_fskip(&s->pb, len);
  638. goto redo;
  639. found:
  640. av_new_packet(pkt, len);
  641. get_buffer(&s->pb, pkt->data, pkt->size);
  642. pkt->pts = pts;
  643. pkt->stream_index = i;
  644. return 0;
  645. }
  646. static int mpeg_mux_check_packet(AVFormatContext *s, int *size)
  647. {
  648. MpegDemuxContext *m = s->priv_data;
  649. int len, startcode, c, n, flags, header_len;
  650. INT64 pts, dts;
  651. /* next start code (should be immediately after */
  652. redo:
  653. m->header_state = 0xff;
  654. startcode = find_start_code(&s->pb, size, &m->header_state);
  655. if (startcode < 0)
  656. return -EIO;
  657. if (startcode == PACK_START_CODE)
  658. goto redo;
  659. if (startcode == SYSTEM_HEADER_START_CODE)
  660. goto redo;
  661. if (startcode == PADDING_STREAM ||
  662. startcode == PRIVATE_STREAM_2) {
  663. /* skip them */
  664. len = get_be16(&s->pb);
  665. url_fskip(&s->pb, len);
  666. goto redo;
  667. }
  668. /* find matching stream */
  669. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  670. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  671. (startcode == 0x1bd)))
  672. goto redo;
  673. n = *size;
  674. len = get_be16(&s->pb);
  675. n -= 2;
  676. pts = 0;
  677. dts = 0;
  678. /* stuffing */
  679. for(;;) {
  680. c = get_byte(&s->pb);
  681. len--;
  682. n--;
  683. /* XXX: for mpeg1, should test only bit 7 */
  684. if (c != 0xff)
  685. break;
  686. }
  687. if ((c & 0xc0) == 0x40) {
  688. /* buffer scale & size */
  689. get_byte(&s->pb);
  690. c = get_byte(&s->pb);
  691. len -= 2;
  692. n -= 2;
  693. }
  694. if ((c & 0xf0) == 0x20) {
  695. pts = get_pts(&s->pb, c);
  696. len -= 4;
  697. n -= 4;
  698. dts = pts;
  699. } else if ((c & 0xf0) == 0x30) {
  700. pts = get_pts(&s->pb, c);
  701. dts = get_pts(&s->pb, -1);
  702. len -= 9;
  703. n -= 9;
  704. } else if ((c & 0xc0) == 0x80) {
  705. /* mpeg 2 PES */
  706. if ((c & 0x30) != 0) {
  707. fprintf(stderr, "Encrypted multiplex not handled\n");
  708. return -EIO;
  709. }
  710. flags = get_byte(&s->pb);
  711. header_len = get_byte(&s->pb);
  712. len -= 2;
  713. n -= 2;
  714. if (header_len > len)
  715. goto redo;
  716. if ((flags & 0xc0) == 0x40) {
  717. pts = get_pts(&s->pb, -1);
  718. dts = pts;
  719. header_len -= 5;
  720. len -= 5;
  721. n -= 5;
  722. } if ((flags & 0xc0) == 0xc0) {
  723. pts = get_pts(&s->pb, -1);
  724. dts = get_pts(&s->pb, -1);
  725. header_len -= 10;
  726. len -= 10;
  727. n -= 10;
  728. }
  729. len -= header_len;
  730. n -= header_len;
  731. while (header_len > 0) {
  732. get_byte(&s->pb);
  733. header_len--;
  734. }
  735. }
  736. if (startcode == 0x1bd) {
  737. startcode = get_byte(&s->pb);
  738. len--;
  739. n--;
  740. if (startcode >= 0x80 && startcode <= 0xbf) {
  741. /* audio: skip header */
  742. get_byte(&s->pb);
  743. get_byte(&s->pb);
  744. get_byte(&s->pb);
  745. len -= 3;
  746. n -= 3;
  747. }
  748. }
  749. *size = n;
  750. return startcode;
  751. }
  752. static int mpeg_mux_read_close(AVFormatContext *s)
  753. {
  754. MpegDemuxContext *m = s->priv_data;
  755. free(m);
  756. return 0;
  757. }
  758. AVFormat mpeg_mux_format = {
  759. "mpeg",
  760. "MPEG multiplex format",
  761. "video/x-mpeg",
  762. "mpg,mpeg,vob",
  763. CODEC_ID_MP2,
  764. CODEC_ID_MPEG1VIDEO,
  765. mpeg_mux_init,
  766. mpeg_mux_write_packet,
  767. mpeg_mux_end,
  768. mpeg_mux_read_header,
  769. mpeg_mux_read_packet,
  770. mpeg_mux_read_close,
  771. };