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.

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