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.

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