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.

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