mpegenc.c 40 KB

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