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.

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