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.

891 lines
25KB

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