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.

1267 lines
42KB

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