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.

1234 lines
40KB

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