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.

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