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.

1253 lines
42KB

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