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.

1262 lines
42KB

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