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.

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