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.

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