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.

1244 lines
41KB

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