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.

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