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.

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