mpegenc.c 44 KB

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