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.

1294 lines
44KB

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