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.

1298 lines
42KB

  1. /*
  2. * MPEG1/2 muxer
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/fifo.h"
  22. #include "libavcodec/put_bits.h"
  23. #include "avformat.h"
  24. #include "mpeg.h"
  25. #define MAX_PAYLOAD_SIZE 4096
  26. //#define DEBUG_SEEK
  27. #undef NDEBUG
  28. #include <assert.h>
  29. typedef struct PacketDesc {
  30. int64_t pts;
  31. int64_t dts;
  32. int size;
  33. int unwritten_size;
  34. int flags;
  35. struct PacketDesc *next;
  36. } PacketDesc;
  37. typedef struct {
  38. AVFifoBuffer *fifo;
  39. uint8_t id;
  40. int max_buffer_size; /* in bytes */
  41. int buffer_index;
  42. PacketDesc *predecode_packet;
  43. PacketDesc *premux_packet;
  44. PacketDesc **next_packet;
  45. int packet_number;
  46. uint8_t lpcm_header[3];
  47. int lpcm_align;
  48. int bytes_to_iframe;
  49. int align_iframe;
  50. int64_t vobu_start_pts;
  51. } StreamInfo;
  52. typedef struct {
  53. int packet_size; /* required packet size */
  54. int packet_number;
  55. int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
  56. int system_header_freq;
  57. int system_header_size;
  58. int mux_rate; /* bitrate in units of 50 bytes/s */
  59. /* stream info */
  60. int audio_bound;
  61. int video_bound;
  62. int is_mpeg2;
  63. int is_vcd;
  64. int is_svcd;
  65. int is_dvd;
  66. int64_t last_scr; /* current system clock */
  67. double vcd_padding_bitrate; //FIXME floats
  68. int64_t vcd_padding_bytes_written;
  69. } MpegMuxContext;
  70. extern AVOutputFormat mpeg1vcd_muxer;
  71. extern AVOutputFormat mpeg2dvd_muxer;
  72. extern AVOutputFormat mpeg2svcd_muxer;
  73. extern AVOutputFormat mpeg2vob_muxer;
  74. static int put_pack_header(AVFormatContext *ctx,
  75. uint8_t *buf, int64_t timestamp)
  76. {
  77. MpegMuxContext *s = ctx->priv_data;
  78. PutBitContext pb;
  79. init_put_bits(&pb, buf, 128);
  80. put_bits(&pb, 32, PACK_START_CODE);
  81. if (s->is_mpeg2) {
  82. put_bits(&pb, 2, 0x1);
  83. } else {
  84. put_bits(&pb, 4, 0x2);
  85. }
  86. put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
  87. put_bits(&pb, 1, 1);
  88. put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
  89. put_bits(&pb, 1, 1);
  90. put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff));
  91. put_bits(&pb, 1, 1);
  92. if (s->is_mpeg2) {
  93. /* clock extension */
  94. put_bits(&pb, 9, 0);
  95. }
  96. put_bits(&pb, 1, 1);
  97. put_bits(&pb, 22, s->mux_rate);
  98. put_bits(&pb, 1, 1);
  99. if (s->is_mpeg2) {
  100. put_bits(&pb, 1, 1);
  101. put_bits(&pb, 5, 0x1f); /* reserved */
  102. put_bits(&pb, 3, 0); /* stuffing length */
  103. }
  104. flush_put_bits(&pb);
  105. return put_bits_ptr(&pb) - pb.buf;
  106. }
  107. static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
  108. {
  109. MpegMuxContext *s = ctx->priv_data;
  110. int size, i, private_stream_coded, id;
  111. PutBitContext pb;
  112. init_put_bits(&pb, buf, 128);
  113. put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
  114. put_bits(&pb, 16, 0);
  115. put_bits(&pb, 1, 1);
  116. put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
  117. put_bits(&pb, 1, 1); /* marker */
  118. if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
  119. /* This header applies only to the video stream (see VCD standard p. IV-7)*/
  120. put_bits(&pb, 6, 0);
  121. } else
  122. put_bits(&pb, 6, s->audio_bound);
  123. if (s->is_vcd) {
  124. /* see VCD standard, p. IV-7*/
  125. put_bits(&pb, 1, 0);
  126. put_bits(&pb, 1, 1);
  127. } else {
  128. put_bits(&pb, 1, 0); /* variable bitrate*/
  129. put_bits(&pb, 1, 0); /* non constrainted bit stream */
  130. }
  131. if (s->is_vcd || s->is_dvd) {
  132. /* see VCD standard p IV-7 */
  133. put_bits(&pb, 1, 1); /* audio locked */
  134. put_bits(&pb, 1, 1); /* video locked */
  135. } else {
  136. put_bits(&pb, 1, 0); /* audio locked */
  137. put_bits(&pb, 1, 0); /* video locked */
  138. }
  139. put_bits(&pb, 1, 1); /* marker */
  140. if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
  141. /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
  142. put_bits(&pb, 5, 0);
  143. } else
  144. put_bits(&pb, 5, s->video_bound);
  145. if (s->is_dvd) {
  146. put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
  147. put_bits(&pb, 7, 0x7f); /* reserved byte */
  148. } else
  149. put_bits(&pb, 8, 0xff); /* reserved byte */
  150. /* DVD-Video Stream_bound entries
  151. id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
  152. id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0)
  153. id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
  154. id (0xBF) private stream 2, NAV packs, set to 2x1024. */
  155. if (s->is_dvd) {
  156. int P_STD_max_video = 0;
  157. int P_STD_max_mpeg_audio = 0;
  158. int P_STD_max_mpeg_PS1 = 0;
  159. for(i=0;i<ctx->nb_streams;i++) {
  160. StreamInfo *stream = ctx->streams[i]->priv_data;
  161. id = stream->id;
  162. if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
  163. P_STD_max_mpeg_PS1 = stream->max_buffer_size;
  164. } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
  165. P_STD_max_mpeg_audio = stream->max_buffer_size;
  166. } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
  167. P_STD_max_video = stream->max_buffer_size;
  168. }
  169. }
  170. /* video */
  171. put_bits(&pb, 8, 0xb9); /* stream ID */
  172. put_bits(&pb, 2, 3);
  173. put_bits(&pb, 1, 1);
  174. put_bits(&pb, 13, P_STD_max_video / 1024);
  175. /* audio */
  176. if (P_STD_max_mpeg_audio == 0)
  177. P_STD_max_mpeg_audio = 4096;
  178. put_bits(&pb, 8, 0xb8); /* stream ID */
  179. put_bits(&pb, 2, 3);
  180. put_bits(&pb, 1, 0);
  181. put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
  182. /* private stream 1 */
  183. put_bits(&pb, 8, 0xbd); /* stream ID */
  184. put_bits(&pb, 2, 3);
  185. put_bits(&pb, 1, 0);
  186. put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
  187. /* private stream 2 */
  188. put_bits(&pb, 8, 0xbf); /* stream ID */
  189. put_bits(&pb, 2, 3);
  190. put_bits(&pb, 1, 1);
  191. put_bits(&pb, 13, 2);
  192. }
  193. else {
  194. /* audio stream info */
  195. private_stream_coded = 0;
  196. for(i=0;i<ctx->nb_streams;i++) {
  197. StreamInfo *stream = ctx->streams[i]->priv_data;
  198. /* For VCDs, only include the stream info for the stream
  199. that the pack which contains this system belongs to.
  200. (see VCD standard p. IV-7) */
  201. if ( !s->is_vcd || stream->id==only_for_stream_id
  202. || only_for_stream_id==0) {
  203. id = stream->id;
  204. if (id < 0xc0) {
  205. /* special case for private streams (AC-3 uses that) */
  206. if (private_stream_coded)
  207. continue;
  208. private_stream_coded = 1;
  209. id = 0xbd;
  210. }
  211. put_bits(&pb, 8, id); /* stream ID */
  212. put_bits(&pb, 2, 3);
  213. if (id < 0xe0) {
  214. /* audio */
  215. put_bits(&pb, 1, 0);
  216. put_bits(&pb, 13, stream->max_buffer_size / 128);
  217. } else {
  218. /* video */
  219. put_bits(&pb, 1, 1);
  220. put_bits(&pb, 13, stream->max_buffer_size / 1024);
  221. }
  222. }
  223. }
  224. }
  225. flush_put_bits(&pb);
  226. size = put_bits_ptr(&pb) - pb.buf;
  227. /* patch packet size */
  228. buf[4] = (size - 6) >> 8;
  229. buf[5] = (size - 6) & 0xff;
  230. return size;
  231. }
  232. static int get_system_header_size(AVFormatContext *ctx)
  233. {
  234. int buf_index, i, private_stream_coded;
  235. StreamInfo *stream;
  236. MpegMuxContext *s = ctx->priv_data;
  237. if (s->is_dvd)
  238. return 18; // DVD-Video system headers are 18 bytes fixed length.
  239. buf_index = 12;
  240. private_stream_coded = 0;
  241. for(i=0;i<ctx->nb_streams;i++) {
  242. stream = ctx->streams[i]->priv_data;
  243. if (stream->id < 0xc0) {
  244. if (private_stream_coded)
  245. continue;
  246. private_stream_coded = 1;
  247. }
  248. buf_index += 3;
  249. }
  250. return buf_index;
  251. }
  252. static int mpeg_mux_init(AVFormatContext *ctx)
  253. {
  254. MpegMuxContext *s = ctx->priv_data;
  255. int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
  256. AVStream *st;
  257. StreamInfo *stream;
  258. int audio_bitrate;
  259. int video_bitrate;
  260. s->packet_number = 0;
  261. s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &mpeg1vcd_muxer);
  262. s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer);
  263. s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &mpeg2vob_muxer) ||
  264. (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &mpeg2dvd_muxer) ||
  265. (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer));
  266. s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &mpeg2dvd_muxer);
  267. if(ctx->packet_size)
  268. s->packet_size = ctx->packet_size;
  269. else
  270. s->packet_size = 2048;
  271. s->vcd_padding_bytes_written = 0;
  272. s->vcd_padding_bitrate=0;
  273. s->audio_bound = 0;
  274. s->video_bound = 0;
  275. mpa_id = AUDIO_ID;
  276. ac3_id = AC3_ID;
  277. dts_id = DTS_ID;
  278. mpv_id = VIDEO_ID;
  279. mps_id = SUB_ID;
  280. lpcm_id = LPCM_ID;
  281. for(i=0;i<ctx->nb_streams;i++) {
  282. st = ctx->streams[i];
  283. stream = av_mallocz(sizeof(StreamInfo));
  284. if (!stream)
  285. goto fail;
  286. st->priv_data = stream;
  287. av_set_pts_info(st, 64, 1, 90000);
  288. switch(st->codec->codec_type) {
  289. case CODEC_TYPE_AUDIO:
  290. if (st->codec->codec_id == CODEC_ID_AC3) {
  291. stream->id = ac3_id++;
  292. } else if (st->codec->codec_id == CODEC_ID_DTS) {
  293. stream->id = dts_id++;
  294. } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
  295. stream->id = lpcm_id++;
  296. for(j = 0; j < 4; j++) {
  297. if (lpcm_freq_tab[j] == st->codec->sample_rate)
  298. break;
  299. }
  300. if (j == 4)
  301. goto fail;
  302. if (st->codec->channels > 8)
  303. return -1;
  304. stream->lpcm_header[0] = 0x0c;
  305. stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
  306. stream->lpcm_header[2] = 0x80;
  307. stream->lpcm_align = st->codec->channels * 2;
  308. } else {
  309. stream->id = mpa_id++;
  310. }
  311. /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
  312. Right now it is also used for everything else.*/
  313. stream->max_buffer_size = 4 * 1024;
  314. s->audio_bound++;
  315. break;
  316. case CODEC_TYPE_VIDEO:
  317. stream->id = mpv_id++;
  318. if (st->codec->rc_buffer_size)
  319. stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
  320. else
  321. stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
  322. #if 0
  323. /* see VCD standard, p. IV-7*/
  324. stream->max_buffer_size = 46 * 1024;
  325. else
  326. /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
  327. Right now it is also used for everything else.*/
  328. stream->max_buffer_size = 230 * 1024;
  329. #endif
  330. s->video_bound++;
  331. break;
  332. case CODEC_TYPE_SUBTITLE:
  333. stream->id = mps_id++;
  334. stream->max_buffer_size = 16 * 1024;
  335. break;
  336. default:
  337. return -1;
  338. }
  339. stream->fifo= av_fifo_alloc(16);
  340. if (!stream->fifo)
  341. goto fail;
  342. }
  343. bitrate = 0;
  344. audio_bitrate = 0;
  345. video_bitrate = 0;
  346. for(i=0;i<ctx->nb_streams;i++) {
  347. int codec_rate;
  348. st = ctx->streams[i];
  349. stream = (StreamInfo*) st->priv_data;
  350. if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
  351. codec_rate= st->codec->rc_max_rate;
  352. else
  353. codec_rate= st->codec->bit_rate;
  354. if(!codec_rate)
  355. codec_rate= (1<<21)*8*50/ctx->nb_streams;
  356. bitrate += codec_rate;
  357. if ((stream->id & 0xe0) == AUDIO_ID)
  358. audio_bitrate += codec_rate;
  359. else if (stream->id==VIDEO_ID)
  360. video_bitrate += codec_rate;
  361. }
  362. if(ctx->mux_rate){
  363. s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
  364. } else {
  365. /* we increase slightly the bitrate to take into account the
  366. headers. XXX: compute it exactly */
  367. bitrate += bitrate*5/100;
  368. bitrate += 10000;
  369. s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
  370. }
  371. if (s->is_vcd) {
  372. double overhead_rate;
  373. /* The VCD standard mandates that the mux_rate field is 3528
  374. (see standard p. IV-6).
  375. The value is actually "wrong", i.e. if you calculate
  376. it using the normal formula and the 75 sectors per second transfer
  377. rate you get a different value because the real pack size is 2324,
  378. not 2352. But the standard explicitly specifies that the mux_rate
  379. field in the header must have this value.*/
  380. // s->mux_rate=2352 * 75 / 50; /* = 3528*/
  381. /* The VCD standard states that the muxed stream must be
  382. exactly 75 packs / second (the data rate of a single speed cdrom).
  383. Since the video bitrate (probably 1150000 bits/sec) will be below
  384. the theoretical maximum we have to add some padding packets
  385. to make up for the lower data rate.
  386. (cf. VCD standard p. IV-6 )*/
  387. /* Add the header overhead to the data rate.
  388. 2279 data bytes per audio pack, 2294 data bytes per video pack*/
  389. overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
  390. overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
  391. overhead_rate *= 8;
  392. /* Add padding so that the full bitrate is 2324*75 bytes/sec */
  393. s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
  394. }
  395. if (s->is_vcd || s->is_mpeg2)
  396. /* every packet */
  397. s->pack_header_freq = 1;
  398. else
  399. /* every 2 seconds */
  400. s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
  401. /* the above seems to make pack_header_freq zero sometimes */
  402. if (s->pack_header_freq == 0)
  403. s->pack_header_freq = 1;
  404. if (s->is_mpeg2)
  405. /* every 200 packets. Need to look at the spec. */
  406. s->system_header_freq = s->pack_header_freq * 40;
  407. else if (s->is_vcd)
  408. /* the standard mandates that there are only two system headers
  409. in the whole file: one in the first packet of each stream.
  410. (see standard p. IV-7 and IV-8) */
  411. s->system_header_freq = 0x7fffffff;
  412. else
  413. s->system_header_freq = s->pack_header_freq * 5;
  414. for(i=0;i<ctx->nb_streams;i++) {
  415. stream = ctx->streams[i]->priv_data;
  416. stream->packet_number = 0;
  417. }
  418. s->system_header_size = get_system_header_size(ctx);
  419. s->last_scr = 0;
  420. return 0;
  421. fail:
  422. for(i=0;i<ctx->nb_streams;i++) {
  423. av_free(ctx->streams[i]->priv_data);
  424. }
  425. return AVERROR(ENOMEM);
  426. }
  427. static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
  428. {
  429. put_byte(pb,
  430. (id << 4) |
  431. (((timestamp >> 30) & 0x07) << 1) |
  432. 1);
  433. put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
  434. put_be16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
  435. }
  436. /* return the number of padding bytes that should be inserted into
  437. the multiplexed stream.*/
  438. static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
  439. {
  440. MpegMuxContext *s = ctx->priv_data;
  441. int pad_bytes = 0;
  442. if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
  443. {
  444. int64_t full_pad_bytes;
  445. full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
  446. pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
  447. if (pad_bytes<0)
  448. /* might happen if we have already padded to a later timestamp. This
  449. can occur if another stream has already advanced further.*/
  450. pad_bytes=0;
  451. }
  452. return pad_bytes;
  453. }
  454. #if 0 /* unused, remove? */
  455. /* return the exact available payload size for the next packet for
  456. stream 'stream_index'. 'pts' and 'dts' are only used to know if
  457. timestamps are needed in the packet header. */
  458. static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
  459. int64_t pts, int64_t dts)
  460. {
  461. MpegMuxContext *s = ctx->priv_data;
  462. int buf_index;
  463. StreamInfo *stream;
  464. stream = ctx->streams[stream_index]->priv_data;
  465. buf_index = 0;
  466. if (((s->packet_number % s->pack_header_freq) == 0)) {
  467. /* pack header size */
  468. if (s->is_mpeg2)
  469. buf_index += 14;
  470. else
  471. buf_index += 12;
  472. if (s->is_vcd) {
  473. /* there is exactly one system header for each stream in a VCD MPEG,
  474. One in the very first video packet and one in the very first
  475. audio packet (see VCD standard p. IV-7 and IV-8).*/
  476. if (stream->packet_number==0)
  477. /* The system headers refer only to the stream they occur in,
  478. so they have a constant size.*/
  479. buf_index += 15;
  480. } else {
  481. if ((s->packet_number % s->system_header_freq) == 0)
  482. buf_index += s->system_header_size;
  483. }
  484. }
  485. if ((s->is_vcd && stream->packet_number==0)
  486. || (s->is_svcd && s->packet_number==0))
  487. /* the first pack of each stream contains only the pack header,
  488. the system header and some padding (see VCD standard p. IV-6)
  489. Add the padding size, so that the actual payload becomes 0.*/
  490. buf_index += s->packet_size - buf_index;
  491. else {
  492. /* packet header size */
  493. buf_index += 6;
  494. if (s->is_mpeg2) {
  495. buf_index += 3;
  496. if (stream->packet_number==0)
  497. buf_index += 3; /* PES extension */
  498. buf_index += 1; /* obligatory stuffing byte */
  499. }
  500. if (pts != AV_NOPTS_VALUE) {
  501. if (dts != pts)
  502. buf_index += 5 + 5;
  503. else
  504. buf_index += 5;
  505. } else {
  506. if (!s->is_mpeg2)
  507. buf_index++;
  508. }
  509. if (stream->id < 0xc0) {
  510. /* AC-3/LPCM private data header */
  511. buf_index += 4;
  512. if (stream->id >= 0xa0) {
  513. int n;
  514. buf_index += 3;
  515. /* NOTE: we round the payload size to an integer number of
  516. LPCM samples */
  517. n = (s->packet_size - buf_index) % stream->lpcm_align;
  518. if (n)
  519. buf_index += (stream->lpcm_align - n);
  520. }
  521. }
  522. if (s->is_vcd && (stream->id & 0xe0) == AUDIO_ID)
  523. /* The VCD standard demands that 20 zero bytes follow
  524. each audio packet (see standard p. IV-8).*/
  525. buf_index+=20;
  526. }
  527. return s->packet_size - buf_index;
  528. }
  529. #endif
  530. /* Write an MPEG padding packet header. */
  531. static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
  532. {
  533. MpegMuxContext *s = ctx->priv_data;
  534. int i;
  535. put_be32(pb, PADDING_STREAM);
  536. put_be16(pb, packet_bytes - 6);
  537. if (!s->is_mpeg2) {
  538. put_byte(pb, 0x0f);
  539. packet_bytes -= 7;
  540. } else
  541. packet_bytes -= 6;
  542. for(i=0;i<packet_bytes;i++)
  543. put_byte(pb, 0xff);
  544. }
  545. static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
  546. int nb_frames=0;
  547. PacketDesc *pkt_desc= stream->premux_packet;
  548. while(len>0){
  549. if(pkt_desc->size == pkt_desc->unwritten_size)
  550. nb_frames++;
  551. len -= pkt_desc->unwritten_size;
  552. pkt_desc= pkt_desc->next;
  553. }
  554. return nb_frames;
  555. }
  556. /* flush the packet on stream stream_index */
  557. static int flush_packet(AVFormatContext *ctx, int stream_index,
  558. int64_t pts, int64_t dts, int64_t scr, int trailer_size)
  559. {
  560. MpegMuxContext *s = ctx->priv_data;
  561. StreamInfo *stream = ctx->streams[stream_index]->priv_data;
  562. uint8_t *buf_ptr;
  563. int size, payload_size, startcode, id, stuffing_size, i, header_len;
  564. int packet_size;
  565. uint8_t buffer[128];
  566. int zero_trail_bytes = 0;
  567. int pad_packet_bytes = 0;
  568. int pes_flags;
  569. int general_pack = 0; /*"general" pack without data specific to one stream?*/
  570. int nb_frames;
  571. id = stream->id;
  572. #if 0
  573. printf("packet ID=%2x PTS=%0.3f\n",
  574. id, pts / 90000.0);
  575. #endif
  576. buf_ptr = buffer;
  577. if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
  578. /* output pack and systems header if needed */
  579. size = put_pack_header(ctx, buf_ptr, scr);
  580. buf_ptr += size;
  581. s->last_scr= scr;
  582. if (s->is_vcd) {
  583. /* there is exactly one system header for each stream in a VCD MPEG,
  584. One in the very first video packet and one in the very first
  585. audio packet (see VCD standard p. IV-7 and IV-8).*/
  586. if (stream->packet_number==0) {
  587. size = put_system_header(ctx, buf_ptr, id);
  588. buf_ptr += size;
  589. }
  590. } else if (s->is_dvd) {
  591. if (stream->align_iframe || s->packet_number == 0){
  592. int PES_bytes_to_fill = s->packet_size - size - 10;
  593. if (pts != AV_NOPTS_VALUE) {
  594. if (dts != pts)
  595. PES_bytes_to_fill -= 5 + 5;
  596. else
  597. PES_bytes_to_fill -= 5;
  598. }
  599. if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
  600. size = put_system_header(ctx, buf_ptr, 0);
  601. buf_ptr += size;
  602. size = buf_ptr - buffer;
  603. put_buffer(ctx->pb, buffer, size);
  604. put_be32(ctx->pb, PRIVATE_STREAM_2);
  605. put_be16(ctx->pb, 0x03d4); // length
  606. put_byte(ctx->pb, 0x00); // substream ID, 00=PCI
  607. for (i = 0; i < 979; i++)
  608. put_byte(ctx->pb, 0x00);
  609. put_be32(ctx->pb, PRIVATE_STREAM_2);
  610. put_be16(ctx->pb, 0x03fa); // length
  611. put_byte(ctx->pb, 0x01); // substream ID, 01=DSI
  612. for (i = 0; i < 1017; i++)
  613. put_byte(ctx->pb, 0x00);
  614. memset(buffer, 0, 128);
  615. buf_ptr = buffer;
  616. s->packet_number++;
  617. stream->align_iframe = 0;
  618. scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
  619. size = put_pack_header(ctx, buf_ptr, scr);
  620. s->last_scr= scr;
  621. buf_ptr += size;
  622. /* GOP Start */
  623. } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
  624. pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
  625. }
  626. }
  627. } else {
  628. if ((s->packet_number % s->system_header_freq) == 0) {
  629. size = put_system_header(ctx, buf_ptr, 0);
  630. buf_ptr += size;
  631. }
  632. }
  633. }
  634. size = buf_ptr - buffer;
  635. put_buffer(ctx->pb, buffer, size);
  636. packet_size = s->packet_size - size;
  637. if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
  638. /* The VCD standard demands that 20 zero bytes follow
  639. each audio pack (see standard p. IV-8).*/
  640. zero_trail_bytes += 20;
  641. if ((s->is_vcd && stream->packet_number==0)
  642. || (s->is_svcd && s->packet_number==0)) {
  643. /* for VCD the first pack of each stream contains only the pack header,
  644. the system header and lots of padding (see VCD standard p. IV-6).
  645. In the case of an audio pack, 20 zero bytes are also added at
  646. the end.*/
  647. /* For SVCD we fill the very first pack to increase compatibility with
  648. some DVD players. Not mandated by the standard.*/
  649. if (s->is_svcd)
  650. general_pack = 1; /* the system header refers to both streams and no stream data*/
  651. pad_packet_bytes = packet_size - zero_trail_bytes;
  652. }
  653. packet_size -= pad_packet_bytes + zero_trail_bytes;
  654. if (packet_size > 0) {
  655. /* packet header size */
  656. packet_size -= 6;
  657. /* packet header */
  658. if (s->is_mpeg2) {
  659. header_len = 3;
  660. if (stream->packet_number==0)
  661. header_len += 3; /* PES extension */
  662. header_len += 1; /* obligatory stuffing byte */
  663. } else {
  664. header_len = 0;
  665. }
  666. if (pts != AV_NOPTS_VALUE) {
  667. if (dts != pts)
  668. header_len += 5 + 5;
  669. else
  670. header_len += 5;
  671. } else {
  672. if (!s->is_mpeg2)
  673. header_len++;
  674. }
  675. payload_size = packet_size - header_len;
  676. if (id < 0xc0) {
  677. startcode = PRIVATE_STREAM_1;
  678. payload_size -= 1;
  679. if (id >= 0x40) {
  680. payload_size -= 3;
  681. if (id >= 0xa0)
  682. payload_size -= 3;
  683. }
  684. } else {
  685. startcode = 0x100 + id;
  686. }
  687. stuffing_size = payload_size - av_fifo_size(stream->fifo);
  688. // first byte does not fit -> reset pts/dts + stuffing
  689. if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
  690. int timestamp_len=0;
  691. if(dts != pts)
  692. timestamp_len += 5;
  693. if(pts != AV_NOPTS_VALUE)
  694. timestamp_len += s->is_mpeg2 ? 5 : 4;
  695. pts=dts= AV_NOPTS_VALUE;
  696. header_len -= timestamp_len;
  697. if (s->is_dvd && stream->align_iframe) {
  698. pad_packet_bytes += timestamp_len;
  699. packet_size -= timestamp_len;
  700. } else {
  701. payload_size += timestamp_len;
  702. }
  703. stuffing_size += timestamp_len;
  704. if(payload_size > trailer_size)
  705. stuffing_size += payload_size - trailer_size;
  706. }
  707. if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
  708. packet_size += pad_packet_bytes;
  709. payload_size += pad_packet_bytes; // undo the previous adjustment
  710. if (stuffing_size < 0) {
  711. stuffing_size = pad_packet_bytes;
  712. } else {
  713. stuffing_size += pad_packet_bytes;
  714. }
  715. pad_packet_bytes = 0;
  716. }
  717. if (stuffing_size < 0)
  718. stuffing_size = 0;
  719. if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
  720. pad_packet_bytes += stuffing_size;
  721. packet_size -= stuffing_size;
  722. payload_size -= stuffing_size;
  723. stuffing_size = 0;
  724. }
  725. nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
  726. put_be32(ctx->pb, startcode);
  727. put_be16(ctx->pb, packet_size);
  728. if (!s->is_mpeg2)
  729. for(i=0;i<stuffing_size;i++)
  730. put_byte(ctx->pb, 0xff);
  731. if (s->is_mpeg2) {
  732. put_byte(ctx->pb, 0x80); /* mpeg2 id */
  733. pes_flags=0;
  734. if (pts != AV_NOPTS_VALUE) {
  735. pes_flags |= 0x80;
  736. if (dts != pts)
  737. pes_flags |= 0x40;
  738. }
  739. /* Both the MPEG-2 and the SVCD standards demand that the
  740. P-STD_buffer_size field be included in the first packet of
  741. every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
  742. and MPEG-2 standard 2.7.7) */
  743. if (stream->packet_number == 0)
  744. pes_flags |= 0x01;
  745. put_byte(ctx->pb, pes_flags); /* flags */
  746. put_byte(ctx->pb, header_len - 3 + stuffing_size);
  747. if (pes_flags & 0x80) /*write pts*/
  748. put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
  749. if (pes_flags & 0x40) /*write dts*/
  750. put_timestamp(ctx->pb, 0x01, dts);
  751. if (pes_flags & 0x01) { /*write pes extension*/
  752. put_byte(ctx->pb, 0x10); /* flags */
  753. /* P-STD buffer info */
  754. if ((id & 0xe0) == AUDIO_ID)
  755. put_be16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
  756. else
  757. put_be16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
  758. }
  759. } else {
  760. if (pts != AV_NOPTS_VALUE) {
  761. if (dts != pts) {
  762. put_timestamp(ctx->pb, 0x03, pts);
  763. put_timestamp(ctx->pb, 0x01, dts);
  764. } else {
  765. put_timestamp(ctx->pb, 0x02, pts);
  766. }
  767. } else {
  768. put_byte(ctx->pb, 0x0f);
  769. }
  770. }
  771. if (s->is_mpeg2) {
  772. /* special stuffing byte that is always written
  773. to prevent accidental generation of start codes. */
  774. put_byte(ctx->pb, 0xff);
  775. for(i=0;i<stuffing_size;i++)
  776. put_byte(ctx->pb, 0xff);
  777. }
  778. if (startcode == PRIVATE_STREAM_1) {
  779. put_byte(ctx->pb, id);
  780. if (id >= 0xa0) {
  781. /* LPCM (XXX: check nb_frames) */
  782. put_byte(ctx->pb, 7);
  783. put_be16(ctx->pb, 4); /* skip 3 header bytes */
  784. put_byte(ctx->pb, stream->lpcm_header[0]);
  785. put_byte(ctx->pb, stream->lpcm_header[1]);
  786. put_byte(ctx->pb, stream->lpcm_header[2]);
  787. } else if (id >= 0x40) {
  788. /* AC-3 */
  789. put_byte(ctx->pb, nb_frames);
  790. put_be16(ctx->pb, trailer_size+1);
  791. }
  792. }
  793. /* output data */
  794. assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
  795. av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, &put_buffer);
  796. stream->bytes_to_iframe -= payload_size - stuffing_size;
  797. }else{
  798. payload_size=
  799. stuffing_size= 0;
  800. }
  801. if (pad_packet_bytes > 0)
  802. put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
  803. for(i=0;i<zero_trail_bytes;i++)
  804. put_byte(ctx->pb, 0x00);
  805. put_flush_packet(ctx->pb);
  806. s->packet_number++;
  807. /* only increase the stream packet number if this pack actually contains
  808. something that is specific to this stream! I.e. a dedicated header
  809. or some data.*/
  810. if (!general_pack)
  811. stream->packet_number++;
  812. return payload_size - stuffing_size;
  813. }
  814. static void put_vcd_padding_sector(AVFormatContext *ctx)
  815. {
  816. /* There are two ways to do this padding: writing a sector/pack
  817. of 0 values, or writing an MPEG padding pack. Both seem to
  818. work with most decoders, BUT the VCD standard only allows a 0-sector
  819. (see standard p. IV-4, IV-5).
  820. So a 0-sector it is...*/
  821. MpegMuxContext *s = ctx->priv_data;
  822. int i;
  823. for(i=0;i<s->packet_size;i++)
  824. put_byte(ctx->pb, 0);
  825. s->vcd_padding_bytes_written += s->packet_size;
  826. put_flush_packet(ctx->pb);
  827. /* increasing the packet number is correct. The SCR of the following packs
  828. is calculated from the packet_number and it has to include the padding
  829. sector (it represents the sector index, not the MPEG pack index)
  830. (see VCD standard p. IV-6)*/
  831. s->packet_number++;
  832. }
  833. #if 0 /* unused, remove? */
  834. static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
  835. {
  836. MpegMuxContext *s = ctx->priv_data;
  837. int64_t scr;
  838. /* Since the data delivery rate is constant, SCR is computed
  839. using the formula C + i * 1200 where C is the start constant
  840. and i is the pack index.
  841. It is recommended that SCR 0 is at the beginning of the VCD front
  842. margin (a sequence of empty Form 2 sectors on the CD).
  843. It is recommended that the front margin is 30 sectors long, so
  844. we use C = 30*1200 = 36000
  845. (Note that even if the front margin is not 30 sectors the file
  846. will still be correct according to the standard. It just won't have
  847. the "recommended" value).*/
  848. scr = 36000 + s->packet_number * 1200;
  849. return scr;
  850. }
  851. #endif
  852. static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
  853. // MpegMuxContext *s = ctx->priv_data;
  854. int i;
  855. for(i=0; i<ctx->nb_streams; i++){
  856. AVStream *st = ctx->streams[i];
  857. StreamInfo *stream = st->priv_data;
  858. PacketDesc *pkt_desc;
  859. while((pkt_desc= stream->predecode_packet)
  860. && scr > pkt_desc->dts){ //FIXME > vs >=
  861. if(stream->buffer_index < pkt_desc->size ||
  862. stream->predecode_packet == stream->premux_packet){
  863. av_log(ctx, AV_LOG_ERROR,
  864. "buffer underflow i=%d bufi=%d size=%d\n",
  865. i, stream->buffer_index, pkt_desc->size);
  866. break;
  867. }
  868. stream->buffer_index -= pkt_desc->size;
  869. stream->predecode_packet= pkt_desc->next;
  870. av_freep(&pkt_desc);
  871. }
  872. }
  873. return 0;
  874. }
  875. static int output_packet(AVFormatContext *ctx, int flush){
  876. MpegMuxContext *s = ctx->priv_data;
  877. AVStream *st;
  878. StreamInfo *stream;
  879. int i, avail_space=0, es_size, trailer_size;
  880. int best_i= -1;
  881. int best_score= INT_MIN;
  882. int ignore_constraints=0;
  883. int64_t scr= s->last_scr;
  884. PacketDesc *timestamp_packet;
  885. const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
  886. retry:
  887. for(i=0; i<ctx->nb_streams; i++){
  888. AVStream *st = ctx->streams[i];
  889. StreamInfo *stream = st->priv_data;
  890. const int avail_data= av_fifo_size(stream->fifo);
  891. const int space= stream->max_buffer_size - stream->buffer_index;
  892. int rel_space= 1024*space / stream->max_buffer_size;
  893. PacketDesc *next_pkt= stream->premux_packet;
  894. /* for subtitle, a single PES packet must be generated,
  895. so we flush after every single subtitle packet */
  896. if(s->packet_size > avail_data && !flush
  897. && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
  898. return 0;
  899. if(avail_data==0)
  900. continue;
  901. assert(avail_data>0);
  902. if(space < s->packet_size && !ignore_constraints)
  903. continue;
  904. if(next_pkt && next_pkt->dts - scr > max_delay)
  905. continue;
  906. if(rel_space > best_score){
  907. best_score= rel_space;
  908. best_i = i;
  909. avail_space= space;
  910. }
  911. }
  912. if(best_i < 0){
  913. int64_t best_dts= INT64_MAX;
  914. for(i=0; i<ctx->nb_streams; i++){
  915. AVStream *st = ctx->streams[i];
  916. StreamInfo *stream = st->priv_data;
  917. PacketDesc *pkt_desc= stream->predecode_packet;
  918. if(pkt_desc && pkt_desc->dts < best_dts)
  919. best_dts= pkt_desc->dts;
  920. }
  921. #if 0
  922. av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
  923. scr/90000.0, best_dts/90000.0);
  924. #endif
  925. if(best_dts == INT64_MAX)
  926. return 0;
  927. if(scr >= best_dts+1 && !ignore_constraints){
  928. av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
  929. ignore_constraints= 1;
  930. }
  931. scr= FFMAX(best_dts+1, scr);
  932. if(remove_decoded_packets(ctx, scr) < 0)
  933. return -1;
  934. goto retry;
  935. }
  936. assert(best_i >= 0);
  937. st = ctx->streams[best_i];
  938. stream = st->priv_data;
  939. assert(av_fifo_size(stream->fifo) > 0);
  940. assert(avail_space >= s->packet_size || ignore_constraints);
  941. timestamp_packet= stream->premux_packet;
  942. if(timestamp_packet->unwritten_size == timestamp_packet->size){
  943. trailer_size= 0;
  944. }else{
  945. trailer_size= timestamp_packet->unwritten_size;
  946. timestamp_packet= timestamp_packet->next;
  947. }
  948. if(timestamp_packet){
  949. //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);
  950. es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
  951. }else{
  952. assert(av_fifo_size(stream->fifo) == trailer_size);
  953. es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
  954. }
  955. if (s->is_vcd) {
  956. /* Write one or more padding sectors, if necessary, to reach
  957. the constant overall bitrate.*/
  958. int vcd_pad_bytes;
  959. while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
  960. put_vcd_padding_sector(ctx);
  961. s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
  962. }
  963. }
  964. stream->buffer_index += es_size;
  965. s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
  966. while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
  967. es_size -= stream->premux_packet->unwritten_size;
  968. stream->premux_packet= stream->premux_packet->next;
  969. }
  970. if(es_size)
  971. stream->premux_packet->unwritten_size -= es_size;
  972. if(remove_decoded_packets(ctx, s->last_scr) < 0)
  973. return -1;
  974. return 1;
  975. }
  976. static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
  977. {
  978. MpegMuxContext *s = ctx->priv_data;
  979. int stream_index= pkt->stream_index;
  980. int size= pkt->size;
  981. uint8_t *buf= pkt->data;
  982. AVStream *st = ctx->streams[stream_index];
  983. StreamInfo *stream = st->priv_data;
  984. int64_t pts, dts;
  985. PacketDesc *pkt_desc;
  986. const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
  987. const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
  988. pts= pkt->pts;
  989. dts= pkt->dts;
  990. if(pts != AV_NOPTS_VALUE) pts += preload;
  991. if(dts != AV_NOPTS_VALUE) dts += preload;
  992. //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);
  993. if (!stream->premux_packet)
  994. stream->next_packet = &stream->premux_packet;
  995. *stream->next_packet=
  996. pkt_desc= av_mallocz(sizeof(PacketDesc));
  997. pkt_desc->pts= pts;
  998. pkt_desc->dts= dts;
  999. pkt_desc->unwritten_size=
  1000. pkt_desc->size= size;
  1001. if(!stream->predecode_packet)
  1002. stream->predecode_packet= pkt_desc;
  1003. stream->next_packet= &pkt_desc->next;
  1004. if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
  1005. return -1;
  1006. if (s->is_dvd){
  1007. if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
  1008. stream->bytes_to_iframe = av_fifo_size(stream->fifo);
  1009. stream->align_iframe = 1;
  1010. stream->vobu_start_pts = pts;
  1011. }
  1012. }
  1013. av_fifo_generic_write(stream->fifo, buf, size, NULL);
  1014. for(;;){
  1015. int ret= output_packet(ctx, 0);
  1016. if(ret<=0)
  1017. return ret;
  1018. }
  1019. }
  1020. static int mpeg_mux_end(AVFormatContext *ctx)
  1021. {
  1022. // MpegMuxContext *s = ctx->priv_data;
  1023. StreamInfo *stream;
  1024. int i;
  1025. for(;;){
  1026. int ret= output_packet(ctx, 1);
  1027. if(ret<0)
  1028. return ret;
  1029. else if(ret==0)
  1030. break;
  1031. }
  1032. /* End header according to MPEG1 systems standard. We do not write
  1033. it as it is usually not needed by decoders and because it
  1034. complicates MPEG stream concatenation. */
  1035. //put_be32(ctx->pb, ISO_11172_END_CODE);
  1036. //put_flush_packet(ctx->pb);
  1037. for(i=0;i<ctx->nb_streams;i++) {
  1038. stream = ctx->streams[i]->priv_data;
  1039. assert(av_fifo_size(stream->fifo) == 0);
  1040. av_fifo_free(stream->fifo);
  1041. }
  1042. return 0;
  1043. }
  1044. #if CONFIG_MPEG1SYSTEM_MUXER
  1045. AVOutputFormat mpeg1system_muxer = {
  1046. "mpeg",
  1047. NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
  1048. "video/mpeg",
  1049. "mpg,mpeg",
  1050. sizeof(MpegMuxContext),
  1051. CODEC_ID_MP2,
  1052. CODEC_ID_MPEG1VIDEO,
  1053. mpeg_mux_init,
  1054. mpeg_mux_write_packet,
  1055. mpeg_mux_end,
  1056. };
  1057. #endif
  1058. #if CONFIG_MPEG1VCD_MUXER
  1059. AVOutputFormat mpeg1vcd_muxer = {
  1060. "vcd",
  1061. NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
  1062. "video/mpeg",
  1063. NULL,
  1064. sizeof(MpegMuxContext),
  1065. CODEC_ID_MP2,
  1066. CODEC_ID_MPEG1VIDEO,
  1067. mpeg_mux_init,
  1068. mpeg_mux_write_packet,
  1069. mpeg_mux_end,
  1070. };
  1071. #endif
  1072. #if CONFIG_MPEG2VOB_MUXER
  1073. AVOutputFormat mpeg2vob_muxer = {
  1074. "vob",
  1075. NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
  1076. "video/mpeg",
  1077. "vob",
  1078. sizeof(MpegMuxContext),
  1079. CODEC_ID_MP2,
  1080. CODEC_ID_MPEG2VIDEO,
  1081. mpeg_mux_init,
  1082. mpeg_mux_write_packet,
  1083. mpeg_mux_end,
  1084. };
  1085. #endif
  1086. /* Same as mpeg2vob_mux except that the pack size is 2324 */
  1087. #if CONFIG_MPEG2SVCD_MUXER
  1088. AVOutputFormat mpeg2svcd_muxer = {
  1089. "svcd",
  1090. NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
  1091. "video/mpeg",
  1092. "vob",
  1093. sizeof(MpegMuxContext),
  1094. CODEC_ID_MP2,
  1095. CODEC_ID_MPEG2VIDEO,
  1096. mpeg_mux_init,
  1097. mpeg_mux_write_packet,
  1098. mpeg_mux_end,
  1099. };
  1100. #endif
  1101. /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
  1102. #if CONFIG_MPEG2DVD_MUXER
  1103. AVOutputFormat mpeg2dvd_muxer = {
  1104. "dvd",
  1105. NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),
  1106. "video/mpeg",
  1107. "dvd",
  1108. sizeof(MpegMuxContext),
  1109. CODEC_ID_MP2,
  1110. CODEC_ID_MPEG2VIDEO,
  1111. mpeg_mux_init,
  1112. mpeg_mux_write_packet,
  1113. mpeg_mux_end,
  1114. };
  1115. #endif