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.

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