mpegenc.c 42 KB

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