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.

1226 lines
40KB

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