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.

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