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.

1517 lines
47KB

  1. /*
  2. * MPEG1/2 mux/demux
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #define MAX_PAYLOAD_SIZE 4096
  21. //#define DEBUG_SEEK
  22. #undef NDEBUG
  23. #include <assert.h>
  24. typedef struct {
  25. uint8_t buffer[MAX_PAYLOAD_SIZE];
  26. int buffer_ptr;
  27. int nb_frames; /* number of starting frame encountered (AC3) */
  28. int frame_start_offset; /* starting offset of the frame + 1 (0 if none) */
  29. uint8_t id;
  30. int max_buffer_size; /* in bytes */
  31. int packet_number;
  32. int64_t start_pts;
  33. int64_t start_dts;
  34. uint8_t lpcm_header[3];
  35. int lpcm_align;
  36. } StreamInfo;
  37. typedef struct {
  38. int packet_size; /* required packet size */
  39. int packet_number;
  40. int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
  41. int system_header_freq;
  42. int system_header_size;
  43. int mux_rate; /* bitrate in units of 50 bytes/s */
  44. /* stream info */
  45. int audio_bound;
  46. int video_bound;
  47. int is_mpeg2;
  48. int is_vcd;
  49. int is_svcd;
  50. int scr_stream_index; /* stream from which the system clock is
  51. computed (VBR case) */
  52. int64_t last_scr; /* current system clock */
  53. double vcd_padding_bitrate;
  54. int64_t vcd_padding_bytes_written;
  55. } MpegMuxContext;
  56. #define PACK_START_CODE ((unsigned int)0x000001ba)
  57. #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
  58. #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
  59. #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
  60. #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
  61. #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
  62. /* mpeg2 */
  63. #define PROGRAM_STREAM_MAP 0x1bc
  64. #define PRIVATE_STREAM_1 0x1bd
  65. #define PADDING_STREAM 0x1be
  66. #define PRIVATE_STREAM_2 0x1bf
  67. #define AUDIO_ID 0xc0
  68. #define VIDEO_ID 0xe0
  69. #define AC3_ID 0x80
  70. #define LPCM_ID 0xa0
  71. static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
  72. #ifdef CONFIG_ENCODERS
  73. static AVOutputFormat mpeg1system_mux;
  74. static AVOutputFormat mpeg1vcd_mux;
  75. static AVOutputFormat mpeg2vob_mux;
  76. static AVOutputFormat mpeg2svcd_mux;
  77. static int put_pack_header(AVFormatContext *ctx,
  78. uint8_t *buf, int64_t timestamp)
  79. {
  80. MpegMuxContext *s = ctx->priv_data;
  81. PutBitContext pb;
  82. init_put_bits(&pb, buf, 128);
  83. put_bits(&pb, 32, PACK_START_CODE);
  84. if (s->is_mpeg2) {
  85. put_bits(&pb, 2, 0x1);
  86. } else {
  87. put_bits(&pb, 4, 0x2);
  88. }
  89. put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
  90. put_bits(&pb, 1, 1);
  91. put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
  92. put_bits(&pb, 1, 1);
  93. put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
  94. put_bits(&pb, 1, 1);
  95. if (s->is_mpeg2) {
  96. /* clock extension */
  97. put_bits(&pb, 9, 0);
  98. }
  99. put_bits(&pb, 1, 1);
  100. put_bits(&pb, 22, s->mux_rate);
  101. put_bits(&pb, 1, 1);
  102. if (s->is_mpeg2) {
  103. put_bits(&pb, 1, 1);
  104. put_bits(&pb, 5, 0x1f); /* reserved */
  105. put_bits(&pb, 3, 0); /* stuffing length */
  106. }
  107. flush_put_bits(&pb);
  108. return pbBufPtr(&pb) - pb.buf;
  109. }
  110. static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
  111. {
  112. MpegMuxContext *s = ctx->priv_data;
  113. int size, rate_bound, i, private_stream_coded, id;
  114. PutBitContext pb;
  115. init_put_bits(&pb, buf, 128);
  116. put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
  117. put_bits(&pb, 16, 0);
  118. put_bits(&pb, 1, 1);
  119. rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
  120. put_bits(&pb, 22, rate_bound);
  121. put_bits(&pb, 1, 1); /* marker */
  122. if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
  123. /* This header applies only to the video stream (see VCD standard p. IV-7)*/
  124. put_bits(&pb, 6, 0);
  125. } else
  126. put_bits(&pb, 6, s->audio_bound);
  127. if (s->is_vcd) {
  128. /* see VCD standard, p. IV-7*/
  129. put_bits(&pb, 1, 0);
  130. put_bits(&pb, 1, 1);
  131. } else {
  132. put_bits(&pb, 1, 0); /* variable bitrate*/
  133. put_bits(&pb, 1, 0); /* non constrainted bit stream */
  134. }
  135. if (s->is_vcd) {
  136. /* see VCD standard p IV-7 */
  137. put_bits(&pb, 1, 1); /* audio locked */
  138. put_bits(&pb, 1, 1); /* video locked */
  139. } else {
  140. put_bits(&pb, 1, 0); /* audio locked */
  141. put_bits(&pb, 1, 0); /* video locked */
  142. }
  143. put_bits(&pb, 1, 1); /* marker */
  144. if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
  145. /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
  146. put_bits(&pb, 5, 0);
  147. } else
  148. put_bits(&pb, 5, s->video_bound);
  149. put_bits(&pb, 8, 0xff); /* reserved byte */
  150. /* audio stream info */
  151. private_stream_coded = 0;
  152. for(i=0;i<ctx->nb_streams;i++) {
  153. StreamInfo *stream = ctx->streams[i]->priv_data;
  154. /* For VCDs, only include the stream info for the stream
  155. that the pack which contains this system belongs to.
  156. (see VCD standard p. IV-7) */
  157. if ( !s->is_vcd || stream->id==only_for_stream_id
  158. || only_for_stream_id==0) {
  159. id = stream->id;
  160. if (id < 0xc0) {
  161. /* special case for private streams (AC3 use that) */
  162. if (private_stream_coded)
  163. continue;
  164. private_stream_coded = 1;
  165. id = 0xbd;
  166. }
  167. put_bits(&pb, 8, id); /* stream ID */
  168. put_bits(&pb, 2, 3);
  169. if (id < 0xe0) {
  170. /* audio */
  171. put_bits(&pb, 1, 0);
  172. put_bits(&pb, 13, stream->max_buffer_size / 128);
  173. } else {
  174. /* video */
  175. put_bits(&pb, 1, 1);
  176. put_bits(&pb, 13, stream->max_buffer_size / 1024);
  177. }
  178. }
  179. }
  180. flush_put_bits(&pb);
  181. size = pbBufPtr(&pb) - pb.buf;
  182. /* patch packet size */
  183. buf[4] = (size - 6) >> 8;
  184. buf[5] = (size - 6) & 0xff;
  185. return size;
  186. }
  187. static int get_system_header_size(AVFormatContext *ctx)
  188. {
  189. int buf_index, i, private_stream_coded;
  190. StreamInfo *stream;
  191. buf_index = 12;
  192. private_stream_coded = 0;
  193. for(i=0;i<ctx->nb_streams;i++) {
  194. stream = ctx->streams[i]->priv_data;
  195. if (stream->id < 0xc0) {
  196. if (private_stream_coded)
  197. continue;
  198. private_stream_coded = 1;
  199. }
  200. buf_index += 3;
  201. }
  202. return buf_index;
  203. }
  204. static int mpeg_mux_init(AVFormatContext *ctx)
  205. {
  206. MpegMuxContext *s = ctx->priv_data;
  207. int bitrate, i, mpa_id, mpv_id, ac3_id, lpcm_id, j;
  208. AVStream *st;
  209. StreamInfo *stream;
  210. int audio_bitrate;
  211. int video_bitrate;
  212. s->packet_number = 0;
  213. s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
  214. s->is_svcd = (ctx->oformat == &mpeg2svcd_mux);
  215. s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux || ctx->oformat == &mpeg2svcd_mux);
  216. if (s->is_vcd || s->is_svcd)
  217. s->packet_size = 2324; /* VCD/SVCD packet size */
  218. else
  219. s->packet_size = 2048;
  220. s->vcd_padding_bytes_written = 0;
  221. s->vcd_padding_bitrate=0;
  222. s->audio_bound = 0;
  223. s->video_bound = 0;
  224. mpa_id = AUDIO_ID;
  225. ac3_id = AC3_ID;
  226. mpv_id = VIDEO_ID;
  227. lpcm_id = LPCM_ID;
  228. s->scr_stream_index = -1;
  229. for(i=0;i<ctx->nb_streams;i++) {
  230. st = ctx->streams[i];
  231. stream = av_mallocz(sizeof(StreamInfo));
  232. if (!stream)
  233. goto fail;
  234. st->priv_data = stream;
  235. switch(st->codec.codec_type) {
  236. case CODEC_TYPE_AUDIO:
  237. if (st->codec.codec_id == CODEC_ID_AC3) {
  238. stream->id = ac3_id++;
  239. } else if (st->codec.codec_id == CODEC_ID_PCM_S16BE) {
  240. stream->id = lpcm_id++;
  241. for(j = 0; j < 4; j++) {
  242. if (lpcm_freq_tab[j] == st->codec.sample_rate)
  243. break;
  244. }
  245. if (j == 4)
  246. goto fail;
  247. if (st->codec.channels > 8)
  248. return -1;
  249. stream->lpcm_header[0] = 0x0c;
  250. stream->lpcm_header[1] = (st->codec.channels - 1) | (j << 4);
  251. stream->lpcm_header[2] = 0x80;
  252. stream->lpcm_align = st->codec.channels * 2;
  253. } else {
  254. stream->id = mpa_id++;
  255. }
  256. /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
  257. Right now it is also used for everything else.*/
  258. stream->max_buffer_size = 4 * 1024;
  259. s->audio_bound++;
  260. break;
  261. case CODEC_TYPE_VIDEO:
  262. /* by default, video is used for the SCR computation */
  263. if (s->scr_stream_index == -1)
  264. s->scr_stream_index = i;
  265. stream->id = mpv_id++;
  266. if (s->is_vcd)
  267. /* see VCD standard, p. IV-7*/
  268. stream->max_buffer_size = 46 * 1024;
  269. else
  270. /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
  271. Right now it is also used for everything else.*/
  272. stream->max_buffer_size = 230 * 1024;
  273. s->video_bound++;
  274. break;
  275. default:
  276. av_abort();
  277. }
  278. }
  279. /* if no SCR, use first stream (audio) */
  280. if (s->scr_stream_index == -1)
  281. s->scr_stream_index = 0;
  282. bitrate = 0;
  283. audio_bitrate = 0;
  284. video_bitrate = 0;
  285. for(i=0;i<ctx->nb_streams;i++) {
  286. st = ctx->streams[i];
  287. stream = (StreamInfo*) st->priv_data;
  288. bitrate += st->codec.bit_rate;
  289. if (stream->id==AUDIO_ID)
  290. audio_bitrate += st->codec.bit_rate;
  291. else if (stream->id==VIDEO_ID)
  292. video_bitrate += st->codec.bit_rate;
  293. }
  294. if (s->is_vcd) {
  295. double overhead_rate;
  296. /* The VCD standard mandates that the mux_rate field is 3528
  297. (see standard p. IV-6).
  298. The value is actually "wrong", i.e. if you calculate
  299. it using the normal formula and the 75 sectors per second transfer
  300. rate you get a different value because the real pack size is 2324,
  301. not 2352. But the standard explicitly specifies that the mux_rate
  302. field in the header must have this value.*/
  303. s->mux_rate=2352 * 75 / 50; /* = 3528*/
  304. /* The VCD standard states that the muxed stream must be
  305. exactly 75 packs / second (the data rate of a single speed cdrom).
  306. Since the video bitrate (probably 1150000 bits/sec) will be below
  307. the theoretical maximum we have to add some padding packets
  308. to make up for the lower data rate.
  309. (cf. VCD standard p. IV-6 )*/
  310. /* Add the header overhead to the data rate.
  311. 2279 data bytes per audio pack, 2294 data bytes per video pack*/
  312. overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
  313. overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
  314. overhead_rate *= 8;
  315. /* Add padding so that the full bitrate is 2324*75 bytes/sec */
  316. s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
  317. } else {
  318. /* we increase slightly the bitrate to take into account the
  319. headers. XXX: compute it exactly */
  320. bitrate += 2000;
  321. s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
  322. }
  323. if (s->is_vcd || s->is_mpeg2)
  324. /* every packet */
  325. s->pack_header_freq = 1;
  326. else
  327. /* every 2 seconds */
  328. s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
  329. /* the above seems to make pack_header_freq zero sometimes */
  330. if (s->pack_header_freq == 0)
  331. s->pack_header_freq = 1;
  332. if (s->is_mpeg2)
  333. /* every 200 packets. Need to look at the spec. */
  334. s->system_header_freq = s->pack_header_freq * 40;
  335. else if (s->is_vcd)
  336. /* the standard mandates that there are only two system headers
  337. in the whole file: one in the first packet of each stream.
  338. (see standard p. IV-7 and IV-8) */
  339. s->system_header_freq = 0x7fffffff;
  340. else
  341. s->system_header_freq = s->pack_header_freq * 5;
  342. for(i=0;i<ctx->nb_streams;i++) {
  343. stream = ctx->streams[i]->priv_data;
  344. stream->buffer_ptr = 0;
  345. stream->packet_number = 0;
  346. stream->start_pts = AV_NOPTS_VALUE;
  347. stream->start_dts = AV_NOPTS_VALUE;
  348. }
  349. s->system_header_size = get_system_header_size(ctx);
  350. s->last_scr = 0;
  351. return 0;
  352. fail:
  353. for(i=0;i<ctx->nb_streams;i++) {
  354. av_free(ctx->streams[i]->priv_data);
  355. }
  356. return -ENOMEM;
  357. }
  358. static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
  359. {
  360. put_byte(pb,
  361. (id << 4) |
  362. (((timestamp >> 30) & 0x07) << 1) |
  363. 1);
  364. put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
  365. put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
  366. }
  367. /* return the number of padding bytes that should be inserted into
  368. the multiplexed stream.*/
  369. static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
  370. {
  371. MpegMuxContext *s = ctx->priv_data;
  372. int pad_bytes = 0;
  373. if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
  374. {
  375. int64_t full_pad_bytes;
  376. full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0);
  377. pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
  378. if (pad_bytes<0)
  379. /* might happen if we have already padded to a later timestamp. This
  380. can occur if another stream has already advanced further.*/
  381. pad_bytes=0;
  382. }
  383. return pad_bytes;
  384. }
  385. /* return the exact available payload size for the next packet for
  386. stream 'stream_index'. 'pts' and 'dts' are only used to know if
  387. timestamps are needed in the packet header. */
  388. static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
  389. int64_t pts, int64_t dts)
  390. {
  391. MpegMuxContext *s = ctx->priv_data;
  392. int buf_index;
  393. StreamInfo *stream;
  394. stream = ctx->streams[stream_index]->priv_data;
  395. buf_index = 0;
  396. if (((s->packet_number % s->pack_header_freq) == 0)) {
  397. /* pack header size */
  398. if (s->is_mpeg2)
  399. buf_index += 14;
  400. else
  401. buf_index += 12;
  402. if (s->is_vcd) {
  403. /* there is exactly one system header for each stream in a VCD MPEG,
  404. One in the very first video packet and one in the very first
  405. audio packet (see VCD standard p. IV-7 and IV-8).*/
  406. if (stream->packet_number==0)
  407. /* The system headers refer only to the stream they occur in,
  408. so they have a constant size.*/
  409. buf_index += 15;
  410. } else {
  411. if ((s->packet_number % s->system_header_freq) == 0)
  412. buf_index += s->system_header_size;
  413. }
  414. }
  415. if ((s->is_vcd && stream->packet_number==0)
  416. || (s->is_svcd && s->packet_number==0))
  417. /* the first pack of each stream contains only the pack header,
  418. the system header and some padding (see VCD standard p. IV-6)
  419. Add the padding size, so that the actual payload becomes 0.*/
  420. buf_index += s->packet_size - buf_index;
  421. else {
  422. /* packet header size */
  423. buf_index += 6;
  424. if (s->is_mpeg2) {
  425. buf_index += 3;
  426. if (stream->packet_number==0)
  427. buf_index += 3; /* PES extension */
  428. buf_index += 1; /* obligatory stuffing byte */
  429. }
  430. if (pts != AV_NOPTS_VALUE) {
  431. if (dts != pts)
  432. buf_index += 5 + 5;
  433. else
  434. buf_index += 5;
  435. } else {
  436. if (!s->is_mpeg2)
  437. buf_index++;
  438. }
  439. if (stream->id < 0xc0) {
  440. /* AC3/LPCM private data header */
  441. buf_index += 4;
  442. if (stream->id >= 0xa0) {
  443. int n;
  444. buf_index += 3;
  445. /* NOTE: we round the payload size to an integer number of
  446. LPCM samples */
  447. n = (s->packet_size - buf_index) % stream->lpcm_align;
  448. if (n)
  449. buf_index += (stream->lpcm_align - n);
  450. }
  451. }
  452. if (s->is_vcd && stream->id == AUDIO_ID)
  453. /* The VCD standard demands that 20 zero bytes follow
  454. each audio packet (see standard p. IV-8).*/
  455. buf_index+=20;
  456. }
  457. return s->packet_size - buf_index;
  458. }
  459. /* Write an MPEG padding packet header. */
  460. static int put_padding_header(AVFormatContext *ctx,uint8_t* buf, int full_padding_size)
  461. {
  462. MpegMuxContext *s = ctx->priv_data;
  463. int size = full_padding_size - 6; /* subtract header length */
  464. buf[0] = (uint8_t)(PADDING_STREAM >> 24);
  465. buf[1] = (uint8_t)(PADDING_STREAM >> 16);
  466. buf[2] = (uint8_t)(PADDING_STREAM >> 8);
  467. buf[3] = (uint8_t)(PADDING_STREAM);
  468. buf[4] = (uint8_t)(size >> 8);
  469. buf[5] = (uint8_t)(size & 0xff);
  470. if (!s->is_mpeg2) {
  471. buf[6] = 0x0f;
  472. return 7;
  473. } else
  474. return 6;
  475. }
  476. static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
  477. {
  478. uint8_t buffer[7];
  479. int size, i;
  480. size = put_padding_header(ctx,buffer, packet_bytes);
  481. put_buffer(pb, buffer, size);
  482. packet_bytes -= size;
  483. for(i=0;i<packet_bytes;i++)
  484. put_byte(pb, 0xff);
  485. }
  486. /* flush the packet on stream stream_index */
  487. static void flush_packet(AVFormatContext *ctx, int stream_index,
  488. int64_t pts, int64_t dts, int64_t scr)
  489. {
  490. MpegMuxContext *s = ctx->priv_data;
  491. StreamInfo *stream = ctx->streams[stream_index]->priv_data;
  492. uint8_t *buf_ptr;
  493. int size, payload_size, startcode, id, stuffing_size, i, header_len;
  494. int packet_size;
  495. uint8_t buffer[128];
  496. int zero_trail_bytes = 0;
  497. int pad_packet_bytes = 0;
  498. int pes_flags;
  499. int general_pack = 0; /*"general" pack without data specific to one stream?*/
  500. id = stream->id;
  501. #if 0
  502. printf("packet ID=%2x PTS=%0.3f\n",
  503. id, pts / 90000.0);
  504. #endif
  505. buf_ptr = buffer;
  506. if (((s->packet_number % s->pack_header_freq) == 0)) {
  507. /* output pack and systems header if needed */
  508. size = put_pack_header(ctx, buf_ptr, scr);
  509. buf_ptr += size;
  510. if (s->is_vcd) {
  511. /* there is exactly one system header for each stream in a VCD MPEG,
  512. One in the very first video packet and one in the very first
  513. audio packet (see VCD standard p. IV-7 and IV-8).*/
  514. if (stream->packet_number==0) {
  515. size = put_system_header(ctx, buf_ptr, id);
  516. buf_ptr += size;
  517. }
  518. } else {
  519. if ((s->packet_number % s->system_header_freq) == 0) {
  520. size = put_system_header(ctx, buf_ptr, 0);
  521. buf_ptr += size;
  522. }
  523. }
  524. }
  525. size = buf_ptr - buffer;
  526. put_buffer(&ctx->pb, buffer, size);
  527. packet_size = s->packet_size - size;
  528. if (s->is_vcd && id == AUDIO_ID)
  529. /* The VCD standard demands that 20 zero bytes follow
  530. each audio pack (see standard p. IV-8).*/
  531. zero_trail_bytes += 20;
  532. if ((s->is_vcd && stream->packet_number==0)
  533. || (s->is_svcd && s->packet_number==0)) {
  534. /* for VCD the first pack of each stream contains only the pack header,
  535. the system header and lots of padding (see VCD standard p. IV-6).
  536. In the case of an audio pack, 20 zero bytes are also added at
  537. the end.*/
  538. /* For SVCD we fill the very first pack to increase compatibility with
  539. some DVD players. Not mandated by the standard.*/
  540. if (s->is_svcd)
  541. general_pack = 1; /* the system header refers to both streams and no stream data*/
  542. pad_packet_bytes = packet_size - zero_trail_bytes;
  543. }
  544. packet_size -= pad_packet_bytes + zero_trail_bytes;
  545. if (packet_size > 0) {
  546. /* packet header size */
  547. packet_size -= 6;
  548. /* packet header */
  549. if (s->is_mpeg2) {
  550. header_len = 3;
  551. if (stream->packet_number==0)
  552. header_len += 3; /* PES extension */
  553. header_len += 1; /* obligatory stuffing byte */
  554. } else {
  555. header_len = 0;
  556. }
  557. if (pts != AV_NOPTS_VALUE) {
  558. if (dts != pts)
  559. header_len += 5 + 5;
  560. else
  561. header_len += 5;
  562. } else {
  563. if (!s->is_mpeg2)
  564. header_len++;
  565. }
  566. payload_size = packet_size - header_len;
  567. if (id < 0xc0) {
  568. startcode = PRIVATE_STREAM_1;
  569. payload_size -= 4;
  570. if (id >= 0xa0)
  571. payload_size -= 3;
  572. } else {
  573. startcode = 0x100 + id;
  574. }
  575. stuffing_size = payload_size - stream->buffer_ptr;
  576. if (stuffing_size < 0)
  577. stuffing_size = 0;
  578. if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
  579. pad_packet_bytes += stuffing_size;
  580. packet_size -= stuffing_size;
  581. payload_size -= stuffing_size;
  582. stuffing_size = 0;
  583. }
  584. put_be32(&ctx->pb, startcode);
  585. put_be16(&ctx->pb, packet_size);
  586. if (!s->is_mpeg2)
  587. for(i=0;i<stuffing_size;i++)
  588. put_byte(&ctx->pb, 0xff);
  589. if (s->is_mpeg2) {
  590. put_byte(&ctx->pb, 0x80); /* mpeg2 id */
  591. pes_flags=0;
  592. if (pts != AV_NOPTS_VALUE) {
  593. pes_flags |= 0x80;
  594. if (dts != pts)
  595. pes_flags |= 0x40;
  596. }
  597. /* Both the MPEG-2 and the SVCD standards demand that the
  598. P-STD_buffer_size field be included in the first packet of
  599. every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
  600. and MPEG-2 standard 2.7.7) */
  601. if (stream->packet_number == 0)
  602. pes_flags |= 0x01;
  603. put_byte(&ctx->pb, pes_flags); /* flags */
  604. put_byte(&ctx->pb, header_len - 3 + stuffing_size);
  605. if (pes_flags & 0x80) /*write pts*/
  606. put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
  607. if (pes_flags & 0x40) /*write dts*/
  608. put_timestamp(&ctx->pb, 0x01, dts);
  609. if (pes_flags & 0x01) { /*write pes extension*/
  610. put_byte(&ctx->pb, 0x10); /* flags */
  611. /* P-STD buffer info */
  612. if (id == AUDIO_ID)
  613. put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
  614. else
  615. put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
  616. }
  617. } else {
  618. if (pts != AV_NOPTS_VALUE) {
  619. if (dts != pts) {
  620. put_timestamp(&ctx->pb, 0x03, pts);
  621. put_timestamp(&ctx->pb, 0x01, dts);
  622. } else {
  623. put_timestamp(&ctx->pb, 0x02, pts);
  624. }
  625. } else {
  626. put_byte(&ctx->pb, 0x0f);
  627. }
  628. }
  629. if (startcode == PRIVATE_STREAM_1) {
  630. put_byte(&ctx->pb, id);
  631. if (id >= 0xa0) {
  632. /* LPCM (XXX: check nb_frames) */
  633. put_byte(&ctx->pb, 7);
  634. put_be16(&ctx->pb, 4); /* skip 3 header bytes */
  635. put_byte(&ctx->pb, stream->lpcm_header[0]);
  636. put_byte(&ctx->pb, stream->lpcm_header[1]);
  637. put_byte(&ctx->pb, stream->lpcm_header[2]);
  638. } else {
  639. /* AC3 */
  640. put_byte(&ctx->pb, stream->nb_frames);
  641. put_be16(&ctx->pb, stream->frame_start_offset);
  642. }
  643. }
  644. if (s->is_mpeg2) {
  645. /* special stuffing byte that is always written
  646. to prevent accidental generation of start codes. */
  647. put_byte(&ctx->pb, 0xff);
  648. for(i=0;i<stuffing_size;i++)
  649. put_byte(&ctx->pb, 0xff);
  650. }
  651. /* output data */
  652. put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
  653. }
  654. if (pad_packet_bytes > 0)
  655. put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
  656. for(i=0;i<zero_trail_bytes;i++)
  657. put_byte(&ctx->pb, 0x00);
  658. put_flush_packet(&ctx->pb);
  659. s->packet_number++;
  660. /* only increase the stream packet number if this pack actually contains
  661. something that is specific to this stream! I.e. a dedicated header
  662. or some data.*/
  663. if (!general_pack)
  664. stream->packet_number++;
  665. stream->nb_frames = 0;
  666. stream->frame_start_offset = 0;
  667. }
  668. static void put_vcd_padding_sector(AVFormatContext *ctx)
  669. {
  670. /* There are two ways to do this padding: writing a sector/pack
  671. of 0 values, or writing an MPEG padding pack. Both seem to
  672. work with most decoders, BUT the VCD standard only allows a 0-sector
  673. (see standard p. IV-4, IV-5).
  674. So a 0-sector it is...*/
  675. MpegMuxContext *s = ctx->priv_data;
  676. int i;
  677. for(i=0;i<s->packet_size;i++)
  678. put_byte(&ctx->pb, 0);
  679. s->vcd_padding_bytes_written += s->packet_size;
  680. put_flush_packet(&ctx->pb);
  681. /* increasing the packet number is correct. The SCR of the following packs
  682. is calculated from the packet_number and it has to include the padding
  683. sector (it represents the sector index, not the MPEG pack index)
  684. (see VCD standard p. IV-6)*/
  685. s->packet_number++;
  686. }
  687. /* XXX: move that to upper layer */
  688. /* XXX: we assume that there are always 'max_b_frames' between
  689. reference frames. A better solution would be to use the AVFrame pts
  690. field */
  691. static void compute_pts_dts(AVStream *st, int64_t *ppts, int64_t *pdts,
  692. int64_t timestamp)
  693. {
  694. int frame_delay;
  695. int64_t pts, dts;
  696. if (st->codec.codec_type == CODEC_TYPE_VIDEO &&
  697. st->codec.max_b_frames != 0) {
  698. frame_delay = (st->codec.frame_rate_base * 90000LL) /
  699. st->codec.frame_rate;
  700. if (timestamp == 0) {
  701. /* specific case for first frame : DTS just before */
  702. pts = timestamp;
  703. dts = timestamp - frame_delay;
  704. } else {
  705. timestamp -= frame_delay;
  706. if (st->codec.coded_frame->pict_type == FF_B_TYPE) {
  707. /* B frames has identical pts/dts */
  708. pts = timestamp;
  709. dts = timestamp;
  710. } else {
  711. /* a reference frame has a pts equal to the dts of the
  712. _next_ one */
  713. dts = timestamp;
  714. pts = timestamp + (st->codec.max_b_frames + 1) * frame_delay;
  715. }
  716. }
  717. #if 1
  718. av_log(&st->codec, AV_LOG_DEBUG, "pts=%0.3f dts=%0.3f pict_type=%c\n",
  719. pts / 90000.0, dts / 90000.0,
  720. av_get_pict_type_char(st->codec.coded_frame->pict_type));
  721. #endif
  722. } else {
  723. pts = timestamp;
  724. dts = timestamp;
  725. }
  726. *ppts = pts & ((1LL << 33) - 1);
  727. *pdts = dts & ((1LL << 33) - 1);
  728. }
  729. static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
  730. {
  731. MpegMuxContext *s = ctx->priv_data;
  732. int64_t scr;
  733. StreamInfo *stream;
  734. int i;
  735. if (s->is_vcd) {
  736. /* Since the data delivery rate is constant, SCR is computed
  737. using the formula C + i * 1200 where C is the start constant
  738. and i is the pack index.
  739. It is recommended that SCR 0 is at the beginning of the VCD front
  740. margin (a sequence of empty Form 2 sectors on the CD).
  741. It is recommended that the front margin is 30 sectors long, so
  742. we use C = 30*1200 = 36000
  743. (Note that even if the front margin is not 30 sectors the file
  744. will still be correct according to the standard. It just won't have
  745. the "recommended" value).*/
  746. scr = 36000 + s->packet_number * 1200;
  747. #if 0
  748. for(i=0;i<ctx->nb_streams;i++) {
  749. stream = ctx->streams[i]->priv_data;
  750. if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
  751. av_log(ctx, AV_LOG_DEBUG, "mpeg vcd: SCR above PTS (scr=%0.3f, stream index=%d, stream_pts=%0.3f).\n", scr/90000.0, i, stream->start_pts / 90000.0);
  752. }
  753. }
  754. #endif
  755. }
  756. else {
  757. /* XXX I believe this calculation of SCR is wrong. SCR
  758. specifies at which time the data should enter the decoder.
  759. Two packs cannot enter the decoder at the same time. */
  760. /* XXX: system clock should be computed precisely, especially for
  761. CBR case. The current mode gives at least something coherent */
  762. if (stream_index == s->scr_stream_index
  763. && pts != AV_NOPTS_VALUE)
  764. scr = pts;
  765. else
  766. scr = s->last_scr;
  767. /* "Sanity hack": make sure that the SCR does not overtake the pts of
  768. buffered data that is still waiting to be written.*/
  769. for(i=0;i<ctx->nb_streams;i++) {
  770. stream = ctx->streams[i]->priv_data;
  771. if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
  772. /* av_log(ctx, AV_LOG_DEBUG, "mpeg: restricting scr to stream pts (scr=%0.3f, stream index=%d, stream_pts=%0.3f).\n", scr/90000.0, i, stream->start_pts / 90000.0); */
  773. scr = stream->start_pts;
  774. }
  775. }
  776. }
  777. s->last_scr=scr;
  778. return scr;
  779. }
  780. static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
  781. const uint8_t *buf, int size,
  782. int64_t timestamp)
  783. {
  784. MpegMuxContext *s = ctx->priv_data;
  785. AVStream *st = ctx->streams[stream_index];
  786. StreamInfo *stream = st->priv_data;
  787. int64_t pts, dts, new_start_pts, new_start_dts;
  788. int len, avail_size;
  789. compute_pts_dts(st, &pts, &dts, timestamp);
  790. if(s->is_svcd) {
  791. /* offset pts and dts slightly into the future to be able
  792. to do the compatibility fix below.*/
  793. pts = (pts + 2) & ((1LL << 33) - 1);
  794. dts = (dts + 2) & ((1LL << 33) - 1);
  795. if (stream->packet_number == 0 && dts == pts)
  796. /* For the very first packet we want to force the DTS to be included.
  797. This increases compatibility with lots of DVD players.
  798. Since the MPEG-2 standard mandates that DTS is only written when
  799. it is different from PTS we have to move it slightly into the past.*/
  800. dts = (dts - 2) & ((1LL << 33) - 1);
  801. }
  802. if(s->is_vcd) {
  803. /* We have to offset the PTS, so that it is consistent with the SCR.
  804. SCR starts at 36000, but the first two packs contain only padding
  805. and the first pack from the other stream, respectively, may also have
  806. been written before.
  807. So the real data starts at SCR 36000+3*1200. */
  808. pts = (pts + 36000 + 3600) & ((1LL << 33) - 1);
  809. dts = (dts + 36000 + 3600) & ((1LL << 33) - 1);
  810. }
  811. #if 0
  812. update_scr(ctx,stream_index,pts);
  813. printf("%d: pts=%0.3f dts=%0.3f scr=%0.3f\n",
  814. stream_index,
  815. pts / 90000.0,
  816. dts / 90000.0,
  817. s->last_scr / 90000.0);
  818. #endif
  819. /* we assume here that pts != AV_NOPTS_VALUE */
  820. new_start_pts = stream->start_pts;
  821. new_start_dts = stream->start_dts;
  822. if (stream->start_pts == AV_NOPTS_VALUE) {
  823. new_start_pts = pts;
  824. new_start_dts = dts;
  825. }
  826. avail_size = get_packet_payload_size(ctx, stream_index,
  827. new_start_pts,
  828. new_start_dts);
  829. if (stream->buffer_ptr >= avail_size) {
  830. update_scr(ctx,stream_index,stream->start_pts);
  831. /* unlikely case: outputing the pts or dts increase the packet
  832. size so that we cannot write the start of the next
  833. packet. In this case, we must flush the current packet with
  834. padding.
  835. Note: this always happens for the first audio and video packet
  836. in a VCD file, since they do not carry any data.*/
  837. flush_packet(ctx, stream_index,
  838. stream->start_pts, stream->start_dts, s->last_scr);
  839. stream->buffer_ptr = 0;
  840. }
  841. stream->start_pts = new_start_pts;
  842. stream->start_dts = new_start_dts;
  843. stream->nb_frames++;
  844. if (stream->frame_start_offset == 0)
  845. stream->frame_start_offset = stream->buffer_ptr;
  846. while (size > 0) {
  847. avail_size = get_packet_payload_size(ctx, stream_index,
  848. stream->start_pts,
  849. stream->start_dts);
  850. len = avail_size - stream->buffer_ptr;
  851. if (len > size)
  852. len = size;
  853. memcpy(stream->buffer + stream->buffer_ptr, buf, len);
  854. stream->buffer_ptr += len;
  855. buf += len;
  856. size -= len;
  857. if (stream->buffer_ptr >= avail_size) {
  858. update_scr(ctx,stream_index,stream->start_pts);
  859. /* if packet full, we send it now */
  860. flush_packet(ctx, stream_index,
  861. stream->start_pts, stream->start_dts, s->last_scr);
  862. stream->buffer_ptr = 0;
  863. if (s->is_vcd) {
  864. /* Write one or more padding sectors, if necessary, to reach
  865. the constant overall bitrate.*/
  866. int vcd_pad_bytes;
  867. while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size)
  868. put_vcd_padding_sector(ctx);
  869. }
  870. /* Make sure only the FIRST pes packet for this frame has
  871. a timestamp */
  872. stream->start_pts = AV_NOPTS_VALUE;
  873. stream->start_dts = AV_NOPTS_VALUE;
  874. }
  875. }
  876. return 0;
  877. }
  878. static int mpeg_mux_end(AVFormatContext *ctx)
  879. {
  880. MpegMuxContext *s = ctx->priv_data;
  881. StreamInfo *stream;
  882. int i;
  883. /* flush each packet */
  884. for(i=0;i<ctx->nb_streams;i++) {
  885. stream = ctx->streams[i]->priv_data;
  886. if (stream->buffer_ptr > 0) {
  887. update_scr(ctx,i,stream->start_pts);
  888. /* NOTE: we can always write the remaining data as it was
  889. tested before in mpeg_mux_write_packet() */
  890. flush_packet(ctx, i, stream->start_pts, stream->start_dts,
  891. s->last_scr);
  892. }
  893. }
  894. /* End header according to MPEG1 systems standard. We do not write
  895. it as it is usually not needed by decoders and because it
  896. complicates MPEG stream concatenation. */
  897. //put_be32(&ctx->pb, ISO_11172_END_CODE);
  898. //put_flush_packet(&ctx->pb);
  899. for(i=0;i<ctx->nb_streams;i++)
  900. av_freep(&ctx->streams[i]->priv_data);
  901. return 0;
  902. }
  903. #endif //CONFIG_ENCODERS
  904. /*********************************************/
  905. /* demux code */
  906. #define MAX_SYNC_SIZE 100000
  907. static int mpegps_probe(AVProbeData *p)
  908. {
  909. int code, c, i;
  910. code = 0xff;
  911. /* we search the first start code. If it is a packet start code,
  912. then we decide it is mpeg ps. We do not send highest value to
  913. give a chance to mpegts */
  914. /* NOTE: the search range was restricted to avoid too many false
  915. detections */
  916. if (p->buf_size < 6)
  917. return 0;
  918. for (i = 0; i < 20; i++) {
  919. c = p->buf[i];
  920. code = (code << 8) | c;
  921. if ((code & 0xffffff00) == 0x100) {
  922. if (code == PACK_START_CODE ||
  923. code == SYSTEM_HEADER_START_CODE ||
  924. (code >= 0x1e0 && code <= 0x1ef) ||
  925. (code >= 0x1c0 && code <= 0x1df) ||
  926. code == PRIVATE_STREAM_2 ||
  927. code == PROGRAM_STREAM_MAP ||
  928. code == PRIVATE_STREAM_1 ||
  929. code == PADDING_STREAM)
  930. return AVPROBE_SCORE_MAX - 2;
  931. else
  932. return 0;
  933. }
  934. }
  935. return 0;
  936. }
  937. typedef struct MpegDemuxContext {
  938. int header_state;
  939. } MpegDemuxContext;
  940. static int mpegps_read_header(AVFormatContext *s,
  941. AVFormatParameters *ap)
  942. {
  943. MpegDemuxContext *m = s->priv_data;
  944. m->header_state = 0xff;
  945. s->ctx_flags |= AVFMTCTX_NOHEADER;
  946. /* no need to do more */
  947. return 0;
  948. }
  949. static int64_t get_pts(ByteIOContext *pb, int c)
  950. {
  951. int64_t pts;
  952. int val;
  953. if (c < 0)
  954. c = get_byte(pb);
  955. pts = (int64_t)((c >> 1) & 0x07) << 30;
  956. val = get_be16(pb);
  957. pts |= (int64_t)(val >> 1) << 15;
  958. val = get_be16(pb);
  959. pts |= (int64_t)(val >> 1);
  960. return pts;
  961. }
  962. static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
  963. uint32_t *header_state)
  964. {
  965. unsigned int state, v;
  966. int val, n;
  967. state = *header_state;
  968. n = *size_ptr;
  969. while (n > 0) {
  970. if (url_feof(pb))
  971. break;
  972. v = get_byte(pb);
  973. n--;
  974. if (state == 0x000001) {
  975. state = ((state << 8) | v) & 0xffffff;
  976. val = state;
  977. goto found;
  978. }
  979. state = ((state << 8) | v) & 0xffffff;
  980. }
  981. val = -1;
  982. found:
  983. *header_state = state;
  984. *size_ptr = n;
  985. return val;
  986. }
  987. /* XXX: optimize */
  988. static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
  989. {
  990. int64_t pos, pos_start;
  991. int max_size, start_code;
  992. max_size = *size_ptr;
  993. pos_start = url_ftell(pb);
  994. /* in order to go faster, we fill the buffer */
  995. pos = pos_start - 16386;
  996. if (pos < 0)
  997. pos = 0;
  998. url_fseek(pb, pos, SEEK_SET);
  999. get_byte(pb);
  1000. pos = pos_start;
  1001. for(;;) {
  1002. pos--;
  1003. if (pos < 0 || (pos_start - pos) >= max_size) {
  1004. start_code = -1;
  1005. goto the_end;
  1006. }
  1007. url_fseek(pb, pos, SEEK_SET);
  1008. start_code = get_be32(pb);
  1009. if ((start_code & 0xffffff00) == 0x100)
  1010. break;
  1011. }
  1012. the_end:
  1013. *size_ptr = pos_start - pos;
  1014. return start_code;
  1015. }
  1016. /* read the next PES header. Return its position in ppos
  1017. (if not NULL), and its start code, pts and dts.
  1018. */
  1019. static int mpegps_read_pes_header(AVFormatContext *s,
  1020. int64_t *ppos, int *pstart_code,
  1021. int64_t *ppts, int64_t *pdts)
  1022. {
  1023. MpegDemuxContext *m = s->priv_data;
  1024. int len, size, startcode, c, flags, header_len;
  1025. int64_t pts, dts, last_pos;
  1026. last_pos = -1;
  1027. redo:
  1028. /* next start code (should be immediately after) */
  1029. m->header_state = 0xff;
  1030. size = MAX_SYNC_SIZE;
  1031. startcode = find_next_start_code(&s->pb, &size, &m->header_state);
  1032. //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
  1033. if (startcode < 0)
  1034. return -EIO;
  1035. if (startcode == PACK_START_CODE)
  1036. goto redo;
  1037. if (startcode == SYSTEM_HEADER_START_CODE)
  1038. goto redo;
  1039. if (startcode == PADDING_STREAM ||
  1040. startcode == PRIVATE_STREAM_2) {
  1041. /* skip them */
  1042. len = get_be16(&s->pb);
  1043. url_fskip(&s->pb, len);
  1044. goto redo;
  1045. }
  1046. /* find matching stream */
  1047. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  1048. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  1049. (startcode == 0x1bd)))
  1050. goto redo;
  1051. if (ppos) {
  1052. *ppos = url_ftell(&s->pb) - 4;
  1053. }
  1054. len = get_be16(&s->pb);
  1055. pts = AV_NOPTS_VALUE;
  1056. dts = AV_NOPTS_VALUE;
  1057. /* stuffing */
  1058. for(;;) {
  1059. if (len < 1)
  1060. goto redo;
  1061. c = get_byte(&s->pb);
  1062. len--;
  1063. /* XXX: for mpeg1, should test only bit 7 */
  1064. if (c != 0xff)
  1065. break;
  1066. }
  1067. if ((c & 0xc0) == 0x40) {
  1068. /* buffer scale & size */
  1069. if (len < 2)
  1070. goto redo;
  1071. get_byte(&s->pb);
  1072. c = get_byte(&s->pb);
  1073. len -= 2;
  1074. }
  1075. if ((c & 0xf0) == 0x20) {
  1076. if (len < 4)
  1077. goto redo;
  1078. dts = pts = get_pts(&s->pb, c);
  1079. len -= 4;
  1080. } else if ((c & 0xf0) == 0x30) {
  1081. if (len < 9)
  1082. goto redo;
  1083. pts = get_pts(&s->pb, c);
  1084. dts = get_pts(&s->pb, -1);
  1085. len -= 9;
  1086. } else if ((c & 0xc0) == 0x80) {
  1087. /* mpeg 2 PES */
  1088. if ((c & 0x30) != 0) {
  1089. /* Encrypted multiplex not handled */
  1090. goto redo;
  1091. }
  1092. flags = get_byte(&s->pb);
  1093. header_len = get_byte(&s->pb);
  1094. len -= 2;
  1095. if (header_len > len)
  1096. goto redo;
  1097. if ((flags & 0xc0) == 0x80) {
  1098. dts = pts = get_pts(&s->pb, -1);
  1099. if (header_len < 5)
  1100. goto redo;
  1101. header_len -= 5;
  1102. len -= 5;
  1103. } if ((flags & 0xc0) == 0xc0) {
  1104. pts = get_pts(&s->pb, -1);
  1105. dts = get_pts(&s->pb, -1);
  1106. if (header_len < 10)
  1107. goto redo;
  1108. header_len -= 10;
  1109. len -= 10;
  1110. }
  1111. len -= header_len;
  1112. while (header_len > 0) {
  1113. get_byte(&s->pb);
  1114. header_len--;
  1115. }
  1116. }
  1117. else if( c!= 0xf )
  1118. goto redo;
  1119. if (startcode == 0x1bd) {
  1120. if (len < 1)
  1121. goto redo;
  1122. startcode = get_byte(&s->pb);
  1123. len--;
  1124. if (startcode >= 0x80 && startcode <= 0xbf) {
  1125. /* audio: skip header */
  1126. if (len < 3)
  1127. goto redo;
  1128. get_byte(&s->pb);
  1129. get_byte(&s->pb);
  1130. get_byte(&s->pb);
  1131. len -= 3;
  1132. }
  1133. }
  1134. if(dts != AV_NOPTS_VALUE && ppos){
  1135. int i;
  1136. for(i=0; i<s->nb_streams; i++){
  1137. if(startcode == s->streams[i]->id) {
  1138. av_add_index_entry(s->streams[i], *ppos, dts*AV_TIME_BASE/90000, 0, 0 /* FIXME keyframe? */);
  1139. }
  1140. }
  1141. }
  1142. *pstart_code = startcode;
  1143. *ppts = pts;
  1144. *pdts = dts;
  1145. return len;
  1146. }
  1147. static int mpegps_read_packet(AVFormatContext *s,
  1148. AVPacket *pkt)
  1149. {
  1150. AVStream *st;
  1151. int len, startcode, i, type, codec_id;
  1152. int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
  1153. redo:
  1154. len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
  1155. if (len < 0)
  1156. return len;
  1157. /* now find stream */
  1158. for(i=0;i<s->nb_streams;i++) {
  1159. st = s->streams[i];
  1160. if (st->id == startcode)
  1161. goto found;
  1162. }
  1163. if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  1164. type = CODEC_TYPE_VIDEO;
  1165. codec_id = CODEC_ID_MPEG2VIDEO;
  1166. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  1167. type = CODEC_TYPE_AUDIO;
  1168. codec_id = CODEC_ID_MP2;
  1169. } else if (startcode >= 0x80 && startcode <= 0x9f) {
  1170. type = CODEC_TYPE_AUDIO;
  1171. codec_id = CODEC_ID_AC3;
  1172. } else if (startcode >= 0xa0 && startcode <= 0xbf) {
  1173. type = CODEC_TYPE_AUDIO;
  1174. codec_id = CODEC_ID_PCM_S16BE;
  1175. } else {
  1176. skip:
  1177. /* skip packet */
  1178. url_fskip(&s->pb, len);
  1179. goto redo;
  1180. }
  1181. /* no stream found: add a new stream */
  1182. st = av_new_stream(s, startcode);
  1183. if (!st)
  1184. goto skip;
  1185. st->codec.codec_type = type;
  1186. st->codec.codec_id = codec_id;
  1187. if (codec_id != CODEC_ID_PCM_S16BE)
  1188. st->need_parsing = 1;
  1189. found:
  1190. if (startcode >= 0xa0 && startcode <= 0xbf) {
  1191. int b1, freq;
  1192. /* for LPCM, we just skip the header and consider it is raw
  1193. audio data */
  1194. if (len <= 3)
  1195. goto skip;
  1196. get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
  1197. b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
  1198. get_byte(&s->pb); /* dynamic range control (0x80 = off) */
  1199. len -= 3;
  1200. freq = (b1 >> 4) & 3;
  1201. st->codec.sample_rate = lpcm_freq_tab[freq];
  1202. st->codec.channels = 1 + (b1 & 7);
  1203. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
  1204. }
  1205. av_new_packet(pkt, len);
  1206. get_buffer(&s->pb, pkt->data, pkt->size);
  1207. pkt->pts = pts;
  1208. pkt->dts = dts;
  1209. pkt->stream_index = st->index;
  1210. #if 0
  1211. printf("%d: pts=%0.3f dts=%0.3f\n",
  1212. pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0);
  1213. #endif
  1214. return 0;
  1215. }
  1216. static int mpegps_read_close(AVFormatContext *s)
  1217. {
  1218. return 0;
  1219. }
  1220. static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
  1221. int64_t *ppos, int64_t pos_limit)
  1222. {
  1223. int len, startcode;
  1224. int64_t pos, pts, dts;
  1225. pos = *ppos;
  1226. #ifdef DEBUG_SEEK
  1227. printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
  1228. #endif
  1229. url_fseek(&s->pb, pos, SEEK_SET);
  1230. for(;;) {
  1231. len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
  1232. if (len < 0) {
  1233. #ifdef DEBUG_SEEK
  1234. printf("none (ret=%d)\n", len);
  1235. #endif
  1236. return AV_NOPTS_VALUE;
  1237. }
  1238. if (startcode == s->streams[stream_index]->id &&
  1239. dts != AV_NOPTS_VALUE) {
  1240. break;
  1241. }
  1242. url_fskip(&s->pb, len);
  1243. }
  1244. #ifdef DEBUG_SEEK
  1245. printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
  1246. #endif
  1247. *ppos = pos;
  1248. return dts*AV_TIME_BASE/90000;
  1249. }
  1250. #ifdef CONFIG_ENCODERS
  1251. static AVOutputFormat mpeg1system_mux = {
  1252. "mpeg",
  1253. "MPEG1 System format",
  1254. "video/mpeg",
  1255. "mpg,mpeg",
  1256. sizeof(MpegMuxContext),
  1257. CODEC_ID_MP2,
  1258. CODEC_ID_MPEG1VIDEO,
  1259. mpeg_mux_init,
  1260. mpeg_mux_write_packet,
  1261. mpeg_mux_end,
  1262. };
  1263. static AVOutputFormat mpeg1vcd_mux = {
  1264. "vcd",
  1265. "MPEG1 System format (VCD)",
  1266. "video/mpeg",
  1267. NULL,
  1268. sizeof(MpegMuxContext),
  1269. CODEC_ID_MP2,
  1270. CODEC_ID_MPEG1VIDEO,
  1271. mpeg_mux_init,
  1272. mpeg_mux_write_packet,
  1273. mpeg_mux_end,
  1274. };
  1275. static AVOutputFormat mpeg2vob_mux = {
  1276. "vob",
  1277. "MPEG2 PS format (VOB)",
  1278. "video/mpeg",
  1279. "vob",
  1280. sizeof(MpegMuxContext),
  1281. CODEC_ID_MP2,
  1282. CODEC_ID_MPEG2VIDEO,
  1283. mpeg_mux_init,
  1284. mpeg_mux_write_packet,
  1285. mpeg_mux_end,
  1286. };
  1287. /* Same as mpeg2vob_mux except that the pack size is 2324 */
  1288. static AVOutputFormat mpeg2svcd_mux = {
  1289. "svcd",
  1290. "MPEG2 PS format (VOB)",
  1291. "video/mpeg",
  1292. "vob",
  1293. sizeof(MpegMuxContext),
  1294. CODEC_ID_MP2,
  1295. CODEC_ID_MPEG2VIDEO,
  1296. mpeg_mux_init,
  1297. mpeg_mux_write_packet,
  1298. mpeg_mux_end,
  1299. };
  1300. #endif //CONFIG_ENCODERS
  1301. AVInputFormat mpegps_demux = {
  1302. "mpeg",
  1303. "MPEG PS format",
  1304. sizeof(MpegDemuxContext),
  1305. mpegps_probe,
  1306. mpegps_read_header,
  1307. mpegps_read_packet,
  1308. mpegps_read_close,
  1309. NULL, //mpegps_read_seek,
  1310. mpegps_read_dts,
  1311. };
  1312. int mpegps_init(void)
  1313. {
  1314. #ifdef CONFIG_ENCODERS
  1315. av_register_output_format(&mpeg1system_mux);
  1316. av_register_output_format(&mpeg1vcd_mux);
  1317. av_register_output_format(&mpeg2vob_mux);
  1318. av_register_output_format(&mpeg2svcd_mux);
  1319. #endif //CONFIG_ENCODERS
  1320. av_register_input_format(&mpegps_demux);
  1321. return 0;
  1322. }