typo fix
[mplayer/greg.git] / libmpdemux / muxer_mpeg.c
blob68f2d8f01fef6f664b65ff036f25c96bb17e1322
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <inttypes.h>
5 #include <string.h>
6 #include <sys/types.h>
8 #include "config.h"
9 #include "version.h"
10 #include "mp_msg.h"
11 #include "help_mp.h"
13 #include "aviheader.h"
14 #include "ms_hdr.h"
16 #include "stream/stream.h"
17 #include "muxer.h"
18 #include "demuxer.h"
19 #include "stheader.h"
20 #include "m_option.h"
21 #include "mpeg_hdr.h"
22 #include "mp3_hdr.h"
23 #include "liba52/a52.h"
25 #define PACK_HEADER_START_CODE 0x01ba
26 #define SYSTEM_HEADER_START_CODE 0x01bb
27 #define PSM_START_CODE 0x01bc
29 #define PES_PRIVATE1 0x01bd
30 #define PES_PRIVATE2 0x01bf
32 #define MUX_MPEG1 1
33 #define MUX_MPEG2 2
35 #define VIDEO_MPEG1 0x10000001
36 #define VIDEO_MPEG2 0x10000002
37 #define AUDIO_MP2 0x50
38 #define AUDIO_MP3 0x55
39 #define AUDIO_A52 0x2000
40 #define AUDIO_LPCM 0x10001 /* only a placeholder at the moment */
41 #define AUDIO_AAC1 0x706D
42 //#define AUDIO_AAC2 (short) mmioFOURCC('m','p','4','a')
43 #define AUDIO_AAC2 0x504D
45 #define ASPECT_1_1 1
46 #define ASPECT_4_3 2
47 #define ASPECT_16_9 3
48 #define ASPECT_2_21_1 4
50 #define FRAMERATE_23976 1
51 #define FRAMERATE_24 2
52 #define FRAMERATE_25 3
53 #define FRAMERATE_2997 4
54 #define FRAMERATE_30 5
55 #define FRAMERATE_50 6
56 #define FRAMERATE_5994 7
57 #define FRAMERATE_60 8
59 static char ftypes[] = {'?', 'I', 'P', 'B'};
60 #define FTYPE(x) (ftypes[(x)])
62 static const char *framerates[] = {
63 "unchanged", "23.976", "24", "25", "29.97", "30", "50", "59.94", "60"
66 static const char *aspect_ratios[] = {
67 "unchanged", "1/1", "4/3", "16/9", "2.21/1"
70 static char *conf_mux = "mpeg2";
71 static uint16_t conf_packet_size = 0; //dvd
72 static uint32_t conf_muxrate = 0; //kb/s
73 static float conf_vaspect = 0;
74 static float conf_vframerate = 0;
75 static uint32_t conf_vwidth = 0, conf_vheight = 0, conf_panscan_width = 0, conf_panscan_height = 0;
76 static uint32_t conf_vbitrate = 0;
77 static int conf_init_vpts = 200, conf_init_apts = 200;
78 static int conf_ts_allframes = 0;
79 static int conf_init_adelay = 0, conf_init_vdelay = 0;
80 static int conf_abuf_size = 0;
81 static int conf_vbuf_size = 0;
82 static int conf_drop = 0;
83 static int conf_telecine = 0;
84 static int conf_interleaving2 = 0;
85 static float conf_telecine_src = 0;
86 static float conf_telecine_dest = 0;
88 enum FRAME_TYPE {
89 I_FRAME = 1,
90 P_FRAME = 2,
91 B_FRAME = 3
94 typedef struct {
95 uint8_t *buffer;
96 size_t size;
97 size_t alloc_size;
98 uint8_t type;
99 uint64_t pts, dts, idur;
100 uint32_t pos; //start offset for the frame
101 } mpeg_frame_t;
103 typedef struct {
104 uint8_t cnt; // how many entries we use
105 struct {
106 uint8_t id, type;
107 uint32_t bufsize;
108 uint32_t format;
109 } streams[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
110 } sys_info_t;
112 typedef struct {
113 uint8_t cnt; // how many entries we use
114 struct {
115 uint8_t id;
116 uint8_t type;
117 uint32_t format;
118 } streams[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
119 } psm_info_t;
121 typedef struct {
122 int size;
123 uint64_t dts;
124 } buffer_track_t;
126 typedef struct {
127 uint64_t dts, pts;
128 uint64_t frame_dts, frame_pts;
129 int len, stflen;
130 } pack_stats_t;
132 typedef struct {
133 int mux;
134 sys_info_t sys_info;
135 psm_info_t psm_info;
136 uint16_t packet_size;
137 int is_dvd, is_xvcd, is_xsvcd, is_genmpeg1, is_genmpeg2, rawpes, ts_allframes, has_video, has_audio;
138 int update_system_header, use_psm;
139 off_t headers_size, data_size;
140 uint64_t scr;
141 uint64_t delta_scr;
142 uint64_t last_psm_scr;
143 uint32_t muxrate;
144 uint8_t *buff;
145 uint32_t headers_cnt;
146 double init_adelay;
147 int drop;
149 //video patching parameters
150 uint8_t vaspect, vframerate;
151 uint16_t vwidth, vheight, panscan_width, panscan_height;
152 uint32_t vbitrate;
153 int patch_seq, patch_sde;
154 int psm_streams_cnt;
156 //2 million frames are enough
157 #define MAX_PATTERN_LENGTH 2000000
158 uint8_t bff_mask[MAX_PATTERN_LENGTH];
159 } muxer_priv_t;
162 typedef struct {
163 int has_pts, has_dts, pes_is_aligned, type, min_pes_hlen;
164 int delay_rff;
165 uint64_t pts, last_pts, last_dts, dts, size, frame_duration, delta_pts, nom_delta_pts, last_saved_pts;
166 uint32_t buffer_size;
167 double delta_clock, timer;
168 int drop_delayed_frames;
169 mpeg_frame_t *framebuf;
170 uint16_t framebuf_cnt;
171 uint16_t framebuf_used;
172 int32_t last_tr;
173 int max_tr;
174 uint8_t id, is_mpeg12, telecine;
175 uint64_t vframes;
176 int64_t display_frame;
177 mp_mpeg_header_t picture;
178 int max_buffer_size;
179 buffer_track_t *buffer_track;
180 int track_pos, track_len, track_bufsize; //pos and len control the array, bufsize is the size of the buffer
181 unsigned char *pack;
182 int pack_offset, pes_offset, pes_set, payload_offset;
183 int frames;
184 int last_frame_rest; //the rest of the previous frame
185 int is_ready;
186 int mpa_layer;
187 } muxer_headers_t;
189 #define PULLDOWN32 1
190 #define TELECINE_FILM2PAL 2
191 #define TELECINE_DGPULLDOWN 3
193 m_option_t mpegopts_conf[] = {
194 {"format", &(conf_mux), CONF_TYPE_STRING, M_OPT_GLOBAL, 0 ,0, NULL},
195 {"size", &(conf_packet_size), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 65535, NULL},
196 {"muxrate", &(conf_muxrate), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 12000000, NULL}, //12 Mb/s
197 {"vaspect", &(conf_vaspect), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
198 {"vframerate", &(conf_vframerate), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
199 {"vwidth", &(conf_vwidth), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 4095, NULL},
200 {"vheight", &(conf_vheight), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 4095, NULL},
201 {"vpswidth", &(conf_panscan_width), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 16383, NULL},
202 {"vpsheight", &(conf_panscan_height), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 16383, NULL},
203 {"vbitrate", &(conf_vbitrate), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 104857599, NULL},
204 {"vdelay", &conf_init_vdelay, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 32760, NULL},
205 {"adelay", &conf_init_adelay, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 32760, NULL},
206 {"vbuf_size", &conf_vbuf_size, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 40, 1194, NULL},
207 {"abuf_size", &conf_abuf_size, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 4, 64, NULL},
208 {"drop", &conf_drop, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, 1, NULL},
209 {"tsaf", &conf_ts_allframes, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, 1, NULL},
210 {"telecine", &conf_telecine, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, PULLDOWN32, NULL},
211 {"interleaving2", &conf_interleaving2, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, 1, NULL},
212 {"film2pal", &conf_telecine, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, TELECINE_FILM2PAL, NULL},
213 {"tele_src", &(conf_telecine_src), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
214 {"tele_dest", &(conf_telecine_dest), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
215 {NULL, NULL, 0, 0, 0, 0, NULL}
218 static void fix_audio_sys_header(muxer_priv_t *priv, uint8_t id, uint8_t newid, uint32_t size)
220 uint8_t i;
222 for(i = 0; i < priv->sys_info.cnt; i++)
224 if(priv->sys_info.streams[i].id == id)
226 priv->sys_info.streams[i].id = newid;
227 priv->sys_info.streams[i].type = 1;
228 priv->sys_info.streams[i].bufsize = size;
233 static inline int is_mpeg1(uint32_t x)
235 return (
236 (x == 0x10000001) ||
237 (x == mmioFOURCC('m','p','g','1')) ||
238 (x == mmioFOURCC('M','P','G','1'))
242 static inline int is_mpeg2(uint32_t x)
244 return (
245 (x == 0x10000002) ||
246 (x == mmioFOURCC('m','p','g','2')) ||
247 (x == mmioFOURCC('M','P','G','2')) ||
248 (x == mmioFOURCC('m','p','e','g')) ||
249 (x == mmioFOURCC('M','P','E','G'))
253 static inline int is_mpeg4(uint32_t x)
255 return (
256 (x == 0x10000004) ||
257 (x == mmioFOURCC('d','i','v','x')) ||
258 (x == mmioFOURCC('D','I','V','X')) ||
259 (x == mmioFOURCC('x','v','i','d')) ||
260 (x == mmioFOURCC('X','V','I','D')) ||
261 (x == mmioFOURCC('X','v','i','D')) ||
262 (x == mmioFOURCC('x','v','i','x')) ||
263 (x == mmioFOURCC('X','V','I','X')) ||
264 (x == mmioFOURCC('m','p','4','v')) ||
265 (x == mmioFOURCC('M','P','4','V')) ||
266 (x == mmioFOURCC('F', 'M','P','4')) ||
267 (x == mmioFOURCC('f', 'm','p','4')) ||
268 (x == mmioFOURCC('D', 'X','5','0')) ||
269 (x == mmioFOURCC('d', 'x','5','0'))
273 //from unrarlib.c
274 static uint32_t CalcCRC32(uint8_t *buff, uint32_t size)
276 uint32_t i, j, CRCTab[256], crc;
278 for(i = 0;i < 256; i++)
280 for(crc = i, j = 0; j < 8; j++)
281 crc= (crc & 1) ? (crc >> 1)^0xEDB88320L : (crc >> 1);
282 CRCTab[i] = crc;
286 crc = 0xffffffff;
287 for(i = 0; i < size; i++)
288 crc = (crc << 8) ^ CRCTab[((crc >> 24) ^ buff[i]) & 0xff];
290 return crc;
294 static void add_to_psm(muxer_priv_t *priv, uint8_t id, uint32_t format)
296 uint8_t i;
298 i = priv->psm_info.cnt;
299 priv->psm_info.streams[i].id = id;
300 priv->psm_info.streams[i].format = format;
302 if(is_mpeg1(format))
303 priv->psm_info.streams[i].type = 0x01;
304 else if(is_mpeg2(format))
305 priv->psm_info.streams[i].type = 0x02;
306 else if(is_mpeg4(format))
307 priv->psm_info.streams[i].type = 0x10;
308 else if(format == AUDIO_MP2 || format == AUDIO_MP3)
309 priv->psm_info.streams[i].type = 0x03;
310 else if(format == AUDIO_AAC1 || format == AUDIO_AAC2)
311 priv->psm_info.streams[i].type = 0x0f;
312 else
313 priv->psm_info.streams[i].type = 0x81;
315 if(format == AUDIO_A52)
316 memcpy((char*) &(priv->psm_info.streams[i].format), "AC-3", 4);
318 priv->psm_info.cnt++;
322 static mpeg_frame_t *init_frames(uint16_t num, size_t size)
324 mpeg_frame_t *tmp;
325 uint16_t i;
327 tmp = (mpeg_frame_t *) calloc(num, sizeof(mpeg_frame_t));
328 if(tmp == NULL)
329 return NULL;
331 for(i=0; i < num; i++)
333 tmp[i].buffer = (uint8_t *) calloc(1, size);
334 if(tmp[i].buffer == NULL)
335 return NULL;
336 tmp[i].size = 0;
337 tmp[i].pos = 0;
338 tmp[i].alloc_size = size;
339 tmp[i].pts = 0;
342 return tmp;
345 static int add_frame(muxer_headers_t *spriv, uint64_t idur, uint8_t *ptr, int len, uint8_t pt, uint64_t dts, uint64_t pts);
347 static muxer_stream_t* mpegfile_new_stream(muxer_t *muxer,int type){
348 muxer_priv_t *priv = (muxer_priv_t*) muxer->priv;
349 muxer_stream_t *s;
350 muxer_headers_t *spriv;
352 if (!muxer) return NULL;
353 if(muxer->avih.dwStreams>=MUXER_MAX_STREAMS){
354 mp_msg(MSGT_MUXER, MSGL_ERR, "Too many streams! increase MUXER_MAX_STREAMS !\n");
355 return NULL;
357 switch (type) {
358 case MUXER_TYPE_VIDEO:
359 if (muxer->num_videos >= 16) {
360 mp_msg(MSGT_MUXER, MSGL_ERR, "MPEG files can't contain more than 16 video streams!\n");
361 return NULL;
363 break;
364 case MUXER_TYPE_AUDIO:
365 if (muxer->num_audios >= 16) {
366 mp_msg(MSGT_MUXER, MSGL_ERR, "MPEG files can't contain more than 16 audio streams!\n");
367 return NULL;
369 break;
370 default:
371 mp_msg(MSGT_MUXER, MSGL_ERR, "Unknown stream type!\n");
372 return NULL;
374 s = (muxer_stream_t*) calloc(1, sizeof(muxer_stream_t));
375 if(!s) return NULL; // no mem!?
376 if (!(s->b_buffer = malloc(priv->packet_size)))
377 goto init_fail;
378 s->b_buffer_size = priv->packet_size;
379 s->b_buffer_ptr = 0;
380 s->b_buffer_len = 0;
381 s->priv = (muxer_headers_t*) calloc(1, sizeof(muxer_headers_t));
382 if(s->priv == NULL)
383 goto init_fail;
384 spriv = (muxer_headers_t *) s->priv;
385 spriv->pack = malloc(priv->packet_size);
386 if(! spriv->pack)
387 goto init_fail;
388 spriv->buffer_track = calloc(1, 4096*sizeof(buffer_track_t));
389 if(!spriv->buffer_track)
390 goto init_fail;
391 spriv->track_pos = 0;
392 spriv->track_len = 4096;
393 muxer->streams[muxer->avih.dwStreams]=s;
394 s->type=type;
395 s->id=muxer->avih.dwStreams;
396 s->muxer=muxer;
398 if (type == MUXER_TYPE_VIDEO) {
399 spriv->type = 1;
400 spriv->last_pts = conf_init_vpts * 90 * 300;
401 if(conf_init_vdelay) {
402 spriv->last_dts += conf_init_vdelay * 90 * 300;
403 spriv->last_pts += conf_init_vdelay * 90 * 300;
405 spriv->id = 0xe0 + muxer->num_videos;
406 s->ckid = be2me_32 (0x100 + spriv->id);
407 if(priv->is_genmpeg1 || priv->is_genmpeg2) {
408 int v = (conf_vbuf_size ? conf_vbuf_size*1024 :
409 (s->h.dwSuggestedBufferSize ? s->h.dwSuggestedBufferSize : 46*1024));
410 int n = priv->sys_info.cnt;
412 priv->sys_info.streams[n].id = spriv->id;
413 priv->sys_info.streams[n].type = 1;
414 priv->sys_info.streams[n].bufsize = v;
415 priv->sys_info.cnt++;
417 muxer->num_videos++;
418 priv->has_video++;
419 s->h.fccType=streamtypeVIDEO;
420 if(!muxer->def_v) muxer->def_v=s;
421 spriv->framebuf_cnt = 30;
422 spriv->framebuf_used = 0;
423 spriv->framebuf = init_frames(spriv->framebuf_cnt, (size_t) 5000);
424 if(spriv->framebuf == NULL) {
425 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't allocate initial frames structure, abort!\n");
426 goto init_fail;
428 memset(&(spriv->picture), 0, sizeof(spriv->picture));
429 if(priv->is_xvcd)
430 spriv->min_pes_hlen = 18;
431 else if(priv->is_xsvcd)
432 spriv->min_pes_hlen = 22;
433 spriv->telecine = conf_telecine;
434 mp_msg (MSGT_MUXER, MSGL_DBG2, "Added video stream %d, ckid=%X\n", muxer->num_videos, s->ckid);
435 } else { // MUXER_TYPE_AUDIO
436 spriv->type = 0;
437 spriv->drop_delayed_frames = conf_drop;
438 spriv->last_pts = conf_init_apts * 90 * 300;
439 if(conf_init_adelay && ! spriv->drop_delayed_frames)
440 spriv->last_pts += conf_init_adelay * 90 * 300;
441 spriv->pts = spriv->last_pts;
442 spriv->id = 0xc0 + muxer->num_audios;
443 s->ckid = be2me_32 (0x100 + spriv->id);
444 if(priv->is_genmpeg1 || priv->is_genmpeg2) {
445 int a1 = (conf_abuf_size ? conf_abuf_size*1024 :
446 (s->h.dwSuggestedBufferSize ? s->h.dwSuggestedBufferSize : 4*1024));
447 int n = priv->sys_info.cnt;
449 priv->sys_info.streams[n].id = spriv->id;
450 priv->sys_info.streams[n].type = 0;
451 priv->sys_info.streams[n].bufsize = a1;
452 priv->sys_info.cnt++;
454 if(priv->is_xvcd)
455 spriv->min_pes_hlen = 13;
456 else if(priv->is_xsvcd)
457 spriv->min_pes_hlen = 17;
459 muxer->num_audios++;
460 priv->has_audio++;
461 s->h.fccType=streamtypeAUDIO;
463 spriv->framebuf_cnt = 30;
464 spriv->framebuf_used = 0;
465 spriv->framebuf = init_frames(spriv->framebuf_cnt, (size_t) 2048);
466 if(spriv->framebuf == NULL) {
467 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't allocate initial frames structure, abort!\n");
468 goto init_fail;
471 mp_msg (MSGT_MUXER, MSGL_DBG2, "Added audio stream %d, ckid=%X\n", s->id - muxer->num_videos + 1, s->ckid);
473 muxer->avih.dwStreams++;
474 return s;
476 init_fail:
477 if(s)
479 if(s->priv)
481 spriv = s->priv;
482 if(spriv->pack)
483 free(spriv->pack);
484 if(spriv->buffer_track)
485 free(spriv->buffer_track);
486 free(s->priv);
488 if(s->b_buffer)
489 free(s->b_buffer);
490 free(s);
492 return NULL;
495 static void write_mpeg_ts(unsigned char *b, uint64_t ts, uint8_t mod) {
496 ts /= 300;
497 b[0] = mod | ((ts >> 29) & 0xf) | 1;
498 b[1] = (ts >> 22) & 0xff;
499 b[2] = ((ts >> 14) & 0xff) | 1;
500 b[3] = (ts >> 7) & 0xff;
501 b[4] = ((ts << 1) & 0xff) | 1;
505 static void write_mpeg_rate(int type, unsigned char *b, unsigned int rate)
507 rate = ((rate*8)+399) / 400;
509 if(type == MUX_MPEG1)
511 b[0] = ((rate >> 15) & 0x7f) | 0x80;
512 b[1] = (rate >> 7) & 0xff;
513 b[2] = ((rate << 1) & 0xff) | 1;
515 else
517 b[0] = (rate >> 14);
518 b[1] = (rate >> 6) & 0xff;
519 b[2] = ((rate & 0x3f) << 2) | 0x03;
524 static void write_mpeg_std(unsigned char *b, unsigned int size, unsigned int type, uint8_t mod)
526 //type = 0:mpeg audio/128, 1:video and pes private streams (including ac3/dts/lpcm)/1024
527 if(type == 0) //audio
528 size = (size + 127) / 128;
529 else //video or other
530 size = ((size + 1023) / 1024);
532 if(! size)
533 size++;
535 b[0] = ((size >> 8) & 0x3f) | (type==1 ? 0x60 : 0x40) | mod;
536 b[1] = size & 0xff;
539 static void write_mpeg2_scr(unsigned char *b, uint64_t ts)
541 uint16_t t1, t2, t3, scr_ext;
542 scr_ext = ts % 300ULL;
543 ts /= 300ULL;
544 ts &= 0x1FFFFFFFFULL; //33 bits
545 t1 = (ts >> 30) & 0x7;;
546 t2 = (ts >> 15) & 0x7fff;
547 t3 = ts & 0x7fff;
549 b[0] = (t1 << 3 ) | 0x44 | ((t2 >> 13) & 0x3);
550 b[1] = (t2 >> 5);
551 b[2] = (t2 & 0x1f) << 3 | 0x4 | ((t3 >> 13) & 0x3);
552 b[3] = (t3 >> 5);
553 b[4] = (t3 & 0x1f) << 3 | ((scr_ext >> 7) & 0x03) | 0x4;
554 b[5] = ((scr_ext << 1) & 0xFF) | 1;
558 static int write_mpeg_pack_header(muxer_t *muxer, char *buff)
560 int len;
561 muxer_priv_t *priv;
563 priv = (muxer_priv_t *) muxer->priv;
564 *(uint32_t *)buff = be2me_32(PACK_HEADER_START_CODE);
565 if(priv->mux==MUX_MPEG1)
567 write_mpeg_ts(&buff[4], priv->scr, 0x20); // 0010 and SCR
568 write_mpeg_rate(priv->mux, &buff[9], muxer->sysrate);
569 len = 12;
571 else
573 write_mpeg2_scr(&buff[4], priv->scr); // 0010 and SCR
574 write_mpeg_rate(priv->mux, &buff[10], muxer->sysrate);
575 buff[13] = 0xf8; //5 bits reserved + 3 set to 0 to indicate 0 stuffing bytes
576 len = 14;
579 return len;
583 static int write_mpeg_system_header(muxer_t *muxer, char *buff)
585 int len;
586 uint8_t i;
587 muxer_priv_t *priv;
588 priv = (muxer_priv_t *) muxer->priv;
590 len = 0;
591 *(uint32_t *)(&buff[len]) = be2me_32(SYSTEM_HEADER_START_CODE);
592 len += 4;
593 *(uint16_t *)(&buff[len]) = 0; //fake length, we'll fix it later
594 len += 2;
595 write_mpeg_rate(MUX_MPEG1, &buff[len], muxer->sysrate);
596 len += 3;
598 buff[len++] = 0x4 | (priv->is_xvcd ? 1 : 0); //1 audio stream bound, no fixed, CSPS only for xvcd
599 //stolen from libavformat
600 if(priv->is_xvcd || priv->is_dvd)
601 buff[len++] = 0xe1; //system_audio_lock, system_video_lock, marker, 1 video stream bound
602 else
603 buff[len++] = 0x21; //marker, 1 video stream bound
605 buff[len++] = ((priv->mux == MUX_MPEG1) ? 0xff : 0x7f); //in mpeg2 there's the packet rate restriction
607 for(i = 0; i < priv->sys_info.cnt; i++)
609 buff[len++] = priv->sys_info.streams[i].id;
610 write_mpeg_std(&buff[len], priv->sys_info.streams[i].bufsize, priv->sys_info.streams[i].type,
611 (priv->sys_info.streams[i].type == 1 ? 0xe0: 0xc0));
612 len += 2;
615 *(uint16_t *)(&buff[4]) = be2me_16(len - 6); // length field fixed
617 return len;
620 static int write_mpeg_psm(muxer_t *muxer, char *buff)
622 int len;
623 uint8_t i;
624 uint16_t dlen;
625 muxer_priv_t *priv;
626 priv = (muxer_priv_t *) muxer->priv;
628 len = 0;
629 *(uint32_t *)(&buff[len]) = be2me_32(PSM_START_CODE);
630 len += 4;
631 *(uint16_t *)(&buff[len]) = 0; //fake length, we'll fix it later
632 len += 2;
633 buff[len++] = 0xe0; //1 current, 2 bits reserved, 5 version 0
634 buff[len++] = 0xff; //7 reserved, 1 marker
635 buff[len] = buff[len+1] = 0; //length of the program descriptors (unused)
636 len += 2;
637 *(uint16_t *)(&buff[len]) = 0; //length of the es descriptors
638 len += 2;
640 dlen = 0;
641 for(i = 0; i < priv->psm_info.cnt; i++)
644 (priv->psm_info.streams[i].id == 0xbd) ||
645 (priv->psm_info.streams[i].id >= 0xe0 && priv->psm_info.streams[i].id <= 0xef) ||
646 (priv->psm_info.streams[i].id >= 0xc0 && priv->psm_info.streams[i].id <= 0xcf)
649 buff[len++] = priv->psm_info.streams[i].type;
650 buff[len++] = priv->psm_info.streams[i].id;
651 buff[len++] = 0; //len of descriptor upper ...
652 buff[len++] = 0; //... lower
654 dlen += 4;
657 *(uint16_t *)(&buff[10]) = be2me_16(dlen); //length of the es descriptors
659 *(uint16_t *)(&buff[4]) = be2me_16(len - 6 + 4); // length field fixed, including size of CRC32
661 *(uint32_t *)(&buff[len]) = be2me_32(CalcCRC32(buff, len));
663 len += 4; //for crc
665 return len;
668 static int psm_is_late(muxer_priv_t *priv)
670 return (!priv->data_size || (priv->scr >= priv->last_psm_scr + 27000000ULL));
673 static int write_mpeg_pes_header(muxer_headers_t *h, uint8_t *pes_id, uint8_t *buff, uint16_t plen, int stuffing_len, int mux_type)
675 int len;
677 len = 0;
678 memcpy(&buff[len], pes_id, 4);
679 len += 4;
681 buff[len] = buff[len+1] = 0; //fake len
682 len += 2;
684 if(mux_type == MUX_MPEG1)
686 if(stuffing_len > 0)
688 memset(&buff[len], 0xff, stuffing_len);
689 len += stuffing_len;
692 if(h->buffer_size > 0)
694 write_mpeg_std(&buff[len], h->buffer_size, h->type, 0x40); // 01 is pes1 format
695 len += 2;
698 else //MPEG2
700 buff[len] = (h->pes_is_aligned ? 0x84 : 0x80); //0x10...
701 len++;
702 buff[len] = ((h->buffer_size > 0) ? 1 : 0) | (h->pts ? (h->dts ? 0xC0 : 0x80) : 0); //pes extension + pts/dts flags
703 len++;
704 buff[len] = (h->pts ? (h->dts ? 10 : 5) : 0) + ((h->buffer_size > 0) ? 3 : 0) + stuffing_len;//pts + std + stuffing
705 len++;
709 if(h->pts)
711 write_mpeg_ts(&buff[len], h->pts, (h->dts ? 0x30 : 0x20)); // 001x and both PTS/DTS
712 len += 5;
714 if(h->dts)
716 write_mpeg_ts(&buff[len], h->dts, 0x10); // 0001 before DTS
717 len += 5;
720 else
722 if(mux_type == MUX_MPEG1)
724 buff[len] = 0x0f;
725 len += 1;
730 if(mux_type == MUX_MPEG2)
732 if(h->buffer_size > 0)
734 buff[len] = 0x1e; //std flag
735 len++;
737 write_mpeg_std(&buff[len], h->buffer_size, h->type, 0x40);
738 len += 2;
741 if(stuffing_len > 0)
743 memset(&buff[len], 0xff, stuffing_len);
744 len += stuffing_len;
748 *((uint16_t*) &buff[4]) = be2me_16(len + plen - 6); //fix pes packet size
749 return len;
753 static void write_pes_padding(uint8_t *buff, uint16_t len)
755 //6 header bytes + len-6 0xff chars
756 buff[0] = buff[1] = 0;
757 buff[2] = 1;
758 buff[3] = 0xbe;
759 *((uint16_t*) &buff[4]) = be2me_16(len - 6);
760 memset(&buff[6], 0xff, len - 6);
764 static int write_nav_pack(uint8_t *buff)
766 // concatenation of pes_private2 + 03d4 x 0 and pes_private2 + 03fa x 0
767 int len;
769 mp_msg(MSGT_MUXER, MSGL_DBG3, "NAV\n");
770 len = 0;
771 *(uint32_t *)(&buff[len]) = be2me_32(PES_PRIVATE2);
772 len += 4;
773 buff[len++] = 0x3;
774 buff[len++] = 0xd4;
775 memset(&buff[len], 0, 0x03d4);
776 len += 0x03d4;
778 *(uint32_t *)(&buff[len]) = be2me_32(PES_PRIVATE2);
779 len += 4;
780 buff[len++] = 0x3;
781 buff[len++] = 0xfa;
782 memset(&buff[len], 0, 0x03fa);
783 len += 0x03fa;
785 return len;
788 static unsigned int calc_psm_len(muxer_priv_t *priv)
790 return 16 + 4*(priv->psm_info.cnt);
793 static uint32_t calc_pes_hlen(int format, muxer_headers_t *h, muxer_priv_t *priv)
795 uint32_t len;
797 if(format == MUX_MPEG1)
798 len = 6;
799 else
800 len = 9;
802 if(h->pts)
804 len += 5;
805 if(h->dts)
806 len += 5;
808 else if(format == MUX_MPEG1)
809 len += 1;
811 if(h->buffer_size > 0)
813 if(format == MUX_MPEG2)
814 len += 3;
815 else
816 len += 2;
819 //len = max(h->min_pes_hlen, len);
821 return len;
825 static int write_mpeg_pack(muxer_t *muxer, muxer_stream_t *s, stream_t *stream, int isoend)
827 size_t tot, offset;
828 muxer_priv_t *priv;
829 unsigned char *buff;
830 int stuffing_len;
832 priv = (muxer_priv_t *) muxer->priv;
833 buff = priv->buff;
835 if(isoend)
837 offset = priv->packet_size - 4;
838 write_pes_padding(buff, offset);
839 buff[offset + 0] = buff[offset + 1] = 0;
840 buff[offset + 2] = 1;
841 buff[offset + 3] = 0xb9;
843 stream_write_buffer(stream, buff, priv->packet_size);
844 return 1;
846 else //FAKE DVD NAV PACK
848 offset = write_mpeg_pack_header(muxer, buff);
849 offset += write_mpeg_system_header(muxer, &buff[offset]);
851 //priv->update_system_header = 0;
853 if(priv->is_dvd)
854 offset += write_nav_pack(&buff[offset]);
856 stuffing_len = priv->packet_size - offset;
857 if(stuffing_len > 0)
859 //insert a PES padding packet
860 write_pes_padding(&buff[offset], stuffing_len);
861 offset += stuffing_len;
864 stream_write_buffer(stream, buff, offset);
865 priv->headers_size += offset;
866 tot = offset;
867 muxer->movi_end += tot;
869 return tot;
873 static void update_demux_bufsize(muxer_headers_t *spriv, uint64_t dts, int framelen, int type)
875 int dim = (spriv->track_len+16)*sizeof(buffer_track_t);
877 if(spriv->track_pos+1 >= spriv->track_len)
879 buffer_track_t *tmp = realloc(spriv->buffer_track, dim);
880 if(!tmp)
882 mp_msg(MSGT_MUXER, MSGL_ERR, "\r\nERROR, couldn't realloc %d bytes for tracking buffer\r\n", dim);
883 return;
885 spriv->buffer_track = tmp;
886 memset(&(spriv->buffer_track[spriv->track_pos+1]), 0, 16*sizeof(buffer_track_t));
887 spriv->track_len += 16;
890 spriv->buffer_track[spriv->track_pos].size = framelen;
891 spriv->buffer_track[spriv->track_pos].dts = dts; //must be dts
893 spriv->track_pos++;
896 static void fix_a52_headers(muxer_stream_t *s)
898 muxer_headers_t *spriv = s->priv;
899 int x = spriv->payload_offset;
901 spriv->pack[x+0] = 0x80;
902 spriv->pack[x+1] = spriv->frames;
903 if(spriv->frames)
905 spriv->pack[x+2] = ((spriv->last_frame_rest+1) >> 8) & 0xff; //256 * 0 ...
906 spriv->pack[x+3] = (spriv->last_frame_rest+1) & 0xff; // + 1 byte(s) to skip
908 else
909 spriv->pack[x+2] = spriv->pack[x+3] = 0;
912 static inline void remove_frames(muxer_headers_t *spriv, int n)
914 mpeg_frame_t tmp;
915 int i;
917 for(i = n; i < spriv->framebuf_used; i++)
919 tmp = spriv->framebuf[i - n];
920 spriv->framebuf[i - n] = spriv->framebuf[i];
921 spriv->framebuf[i] = tmp;
923 spriv->framebuf_used -= n;
926 static int calc_packet_len(muxer_stream_t *s, int psize, int finalize)
928 muxer_headers_t *spriv = s->priv;
929 int n, len, frpos, m;
931 n = len = 0;
932 frpos = spriv->framebuf[0].pos;
933 while(len < psize && n < spriv->framebuf_used)
935 if(!frpos && len>0 && s->type == MUXER_TYPE_VIDEO && spriv->framebuf[n].type==I_FRAME)
936 return len;
937 m = FFMIN(spriv->framebuf[n].size - frpos, psize - len);
938 len += m;
939 frpos += m;
940 if(frpos == spriv->framebuf[n].size)
942 frpos = 0;
943 n++;
947 if(len < psize && !finalize)
948 return 0;
949 return len;
952 static int find_packet_timestamps(muxer_priv_t *priv, muxer_stream_t *s, unsigned int start, uint64_t *dts, uint64_t *pts)
954 muxer_headers_t *spriv = s->priv;
955 int i, m, pes_hlen, ret, threshold;
956 uint64_t spts, sdts, dpts;
958 if(!spriv->framebuf_used)
959 return 0;
961 spts = spriv->pts;
962 sdts = spriv->dts;
963 spriv->dts = spriv->pts = 0;
964 ret = 0;
965 if(spriv->framebuf[0].pos == 0) // start of frame
966 i = 0;
967 else
969 pes_hlen = calc_pes_hlen(priv->mux, spriv, priv);
971 if(pes_hlen < spriv->min_pes_hlen)
972 pes_hlen = spriv->min_pes_hlen;
974 m = spriv->framebuf[0].size - spriv->framebuf[0].pos;
976 if(start + pes_hlen + m >= priv->packet_size) //spriv->pack_offset
977 i = -1; //this pack won't have a pts: no space available
978 else
980 if(spriv->framebuf_used < 2)
981 goto fail;
983 if(spriv->framebuf[1].pts == spriv->framebuf[1].dts)
984 threshold = 5;
985 else
986 threshold = 10;
988 //headers+frame 0 < space available including timestamps
989 if(start + pes_hlen + m < priv->packet_size - threshold)
990 i = 1;
991 else
992 i = -1;
996 if(i > -1)
998 dpts = FFMAX(spriv->last_saved_pts, spriv->framebuf[i].pts) -
999 FFMIN(spriv->last_saved_pts, spriv->framebuf[i].pts) +
1000 spriv->framebuf[0].idur;
1002 if(s->type != MUXER_TYPE_VIDEO)
1003 ret = 1;
1004 else if((spriv->framebuf[i].type == I_FRAME || priv->ts_allframes || dpts >= 36000*300)) //0.4 seconds
1005 ret = 1;
1007 if(ret)
1009 *pts = spriv->framebuf[i].pts;
1010 *dts = spriv->framebuf[i].dts;
1011 if(*dts == *pts)
1012 *dts = 0;
1016 fail:
1017 spriv->pts = spts;
1018 spriv->dts = sdts;
1019 return ret;
1022 static int get_packet_stats(muxer_priv_t *priv, muxer_stream_t *s, pack_stats_t *p, int finalize)
1024 muxer_headers_t *spriv = s->priv;
1025 int len, len2, pack_hlen, pes_hlen, hlen, target, stflen, stuffing_len;
1026 uint64_t pts, dts;
1028 spriv->pts = spriv->dts = 0;
1029 p->dts = p->pts = p->frame_pts = p->frame_dts = 0;
1030 p->len = 0;
1032 if(priv->rawpes)
1033 pack_hlen = 0;
1034 else if(priv->mux == MUX_MPEG1)
1035 pack_hlen = 12;
1036 else
1037 pack_hlen = 14;
1038 if(priv->use_psm && psm_is_late(priv))
1039 pack_hlen += calc_psm_len(priv);
1041 if(find_packet_timestamps(priv, s, pack_hlen, &dts, &pts))
1043 p->pts = p->frame_pts = pts;
1044 p->dts = p->frame_dts = dts;
1046 spriv->pts = pts;
1047 spriv->dts = dts;
1049 pes_hlen = calc_pes_hlen(priv->mux, spriv, priv);
1051 p->stflen = stflen = (spriv->min_pes_hlen > pes_hlen ? spriv->min_pes_hlen - pes_hlen : 0);
1053 target = len = priv->packet_size - pack_hlen - pes_hlen - stflen; //max space available
1054 if(s->type == MUXER_TYPE_AUDIO && s->wf->wFormatTag == AUDIO_A52)
1055 hlen = 4;
1056 else
1057 hlen = 0;
1059 len -= hlen;
1060 target -= hlen;
1062 len2 = calc_packet_len(s, target, finalize);
1063 if(!len2 || (len2 < target && s->type == MUXER_TYPE_AUDIO && !finalize))
1065 //p->len = 0;
1066 //p->dts = p->pts = 0;
1067 spriv->pts = spriv->dts = 0;
1068 //fprintf(stderr, "\r\nLEN2: %d, target: %d, type: %d\r\n", len2, target, s->type);
1069 return 0;
1072 len = len2;
1073 stuffing_len = 0;
1074 if(len < target)
1076 if(s->type == MUXER_TYPE_VIDEO)
1078 if(spriv->pts)
1079 target += 5;
1080 if(spriv->dts)
1081 target += 5;
1082 spriv->pts = spriv->dts = 0;
1083 p->pts = p->dts = 0;
1086 stuffing_len = target - len;
1087 if(stuffing_len > 0 && stuffing_len < 7)
1089 if(stflen + stuffing_len > 16)
1091 int x = 7 - stuffing_len;
1092 stflen -= x;
1093 stuffing_len += x;
1095 else
1097 stflen += stuffing_len;
1098 stuffing_len = 0;
1103 len += hlen;
1105 p->len = len;
1106 p->stflen = stflen;
1108 return p->len;
1111 static int fill_packet(muxer_t *muxer, muxer_stream_t *s, int finalize)
1113 //try to fill a packet as much as possible
1114 //spriv->pack_offset is the start position inited to 0
1115 //data is taken from spriv->framebuf
1116 //if audio and a52 insert the headers
1117 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
1118 muxer_headers_t *spriv = (muxer_headers_t *) s->priv;
1119 int len, m, n, dvd_pack = 0;
1120 int write_psm = 0;
1121 mpeg_frame_t *frm;
1122 pack_stats_t p;
1124 spriv->dts = spriv->pts = 0;
1126 if(! spriv->framebuf_used)
1128 spriv->pack_offset = 0;
1129 return 0;
1132 if(!spriv->pack_offset)
1134 if(priv->rawpes)
1135 spriv->pack_offset = 0;
1136 else
1137 spriv->pack_offset = write_mpeg_pack_header(muxer, spriv->pack);
1138 if(priv->update_system_header && (priv->is_genmpeg1 || priv->is_genmpeg2))
1140 spriv->pack_offset += write_mpeg_system_header(muxer, &spriv->pack[spriv->pack_offset]);
1141 priv->update_system_header = 0;
1144 if(priv->use_psm && psm_is_late(priv))
1146 spriv->pack_offset += write_mpeg_psm(muxer, &spriv->pack[spriv->pack_offset]);
1147 write_psm = 1;
1150 spriv->pes_set = 0;
1151 spriv->pes_offset = spriv->pack_offset;
1152 spriv->payload_offset = 0;
1153 spriv->frames = 0;
1154 spriv->last_frame_rest = 0;
1157 if(!spriv->pes_set)
1159 int bufsize = 0;
1160 //search the pts. yes if either it's video && (I-frame or priv->ts_allframes) && framebuf[i].pos == 0
1161 //or it's audio && framebuf[i].pos == 0
1162 //NB pts and dts can only be relative to the first frame beginning in this pack
1163 if((priv->is_xsvcd || priv->is_xvcd || priv->rawpes) && spriv->size == 0)
1165 if(s->type == MUXER_TYPE_VIDEO)
1166 bufsize = (conf_vbuf_size ? conf_vbuf_size : (priv->is_xvcd ? 46 : 232));
1167 else
1168 bufsize = (conf_abuf_size ? conf_abuf_size : 4);
1169 spriv->buffer_size = bufsize*1024;
1172 if(priv->is_dvd && s->type == MUXER_TYPE_VIDEO
1173 && spriv->framebuf[0].type==I_FRAME && spriv->framebuf[0].pos==0)
1174 dvd_pack = 1;
1176 if(! get_packet_stats(priv, s, &p, finalize))
1178 spriv->pack_offset = 0;
1179 return 0;
1181 spriv->dts = p.dts;
1182 spriv->pts = p.pts;
1183 if(spriv->pts)
1184 spriv->last_saved_pts = p.pts;
1186 spriv->pack_offset += write_mpeg_pes_header(spriv, (uint8_t *) &s->ckid, &(spriv->pack[spriv->pack_offset]),
1187 p.len, p.stflen, priv->mux);
1189 if(s->type == MUXER_TYPE_AUDIO && s->wf->wFormatTag == AUDIO_A52)
1191 spriv->payload_offset = spriv->pack_offset;
1192 spriv->pack_offset += 4; //for the 4 bytes of header
1193 if(!spriv->framebuf[0].pos)
1194 spriv->last_frame_rest = 0;
1195 else
1196 spriv->last_frame_rest = spriv->framebuf[0].size - spriv->framebuf[0].pos;
1199 spriv->pes_set = 1;
1203 if(spriv->dts || spriv->pts)
1205 if((spriv->dts && priv->scr >= spriv->dts) || priv->scr >= spriv->pts)
1206 mp_msg(MSGT_MUXER, MSGL_ERR, "\r\nERROR: scr %.3lf, dts %.3lf, pts %.3lf\r\n", (double) priv->scr/27000000.0, (double) spriv->dts/27000000.0, (double) spriv->pts/27000000.0);
1207 else if(priv->scr + 63000*300 < spriv->dts)
1208 mp_msg(MSGT_MUXER, MSGL_INFO, "\r\nWARNING>: scr %.3lf, dts %.3lf, pts %.3lf, diff %.3lf, piff %.3lf\r\n", (double) priv->scr/27000000.0, (double) spriv->dts/27000000.0, (double) spriv->pts/27000000.0, (double)(spriv->dts - priv->scr)/27000000.0, (double)(spriv->pts - priv->scr)/27000000.0);
1211 n = 0;
1212 len = 0;
1214 frm = spriv->framebuf;
1215 while(spriv->pack_offset < priv->packet_size && n < spriv->framebuf_used)
1217 if(!frm->pos)
1219 //since iframes must always be aligned at block boundaries exit when we find the
1220 //beginning of one in the middle of the flush
1221 if(len > 0 && s->type == MUXER_TYPE_VIDEO && frm->type == I_FRAME)
1223 break;
1225 spriv->frames++;
1226 update_demux_bufsize(spriv, frm->dts, frm->size, s->type);
1229 m = FFMIN(frm->size - frm->pos, priv->packet_size - spriv->pack_offset);
1230 memcpy(&(spriv->pack[spriv->pack_offset]), &(frm->buffer[frm->pos]), m);
1232 len += m;
1233 spriv->pack_offset += m;
1234 frm->pos += m;
1236 if(frm->pos == frm->size) //end of frame
1238 frm->pos = frm->size = 0;
1239 frm->pts = frm->dts = 0;
1240 n++;
1241 frm++;
1245 if((priv->is_xsvcd || priv->is_xvcd || priv->rawpes) && spriv->size == 0)
1246 spriv->buffer_size = 0;
1248 spriv->size += len;
1250 if(dvd_pack && (spriv->pack_offset == priv->packet_size))
1251 write_mpeg_pack(muxer, NULL, muxer->stream, 0); //insert fake Nav Packet
1253 if(n > 0)
1254 remove_frames(spriv, n);
1256 spriv->track_bufsize += len;
1257 if(spriv->track_bufsize > spriv->max_buffer_size)
1258 mp_msg(MSGT_MUXER, MSGL_ERR, "\r\nBUFFER OVERFLOW: %d > %d, pts: %"PRIu64"\r\n", spriv->track_bufsize, spriv->max_buffer_size, spriv->pts);
1260 if(s->type == MUXER_TYPE_AUDIO && s->wf->wFormatTag == AUDIO_A52)
1261 fix_a52_headers(s);
1263 if(spriv->pack_offset < priv->packet_size && !priv->rawpes) //here finalize is set
1265 int diff = priv->packet_size - spriv->pack_offset;
1266 write_pes_padding(&(spriv->pack[spriv->pack_offset]), diff);
1267 spriv->pack_offset += diff;
1270 stream_write_buffer(muxer->stream, spriv->pack, spriv->pack_offset);
1272 priv->headers_size += spriv->pack_offset - len;
1273 priv->data_size += len;
1274 muxer->movi_end += spriv->pack_offset;
1276 spriv->pack_offset = 0;
1277 spriv->pes_set = 0;
1278 spriv->frames = 0;
1279 if(write_psm)
1280 priv->last_psm_scr = priv->scr;
1282 return len;
1285 static inline int find_best_stream(muxer_t *muxer)
1287 int i, ndts;
1288 uint64_t dts = -1;
1289 muxer_priv_t *priv = muxer->priv;
1290 muxer_headers_t *spriv;
1291 pack_stats_t p;
1292 unsigned int perc, sperc;
1294 ndts = -1;
1295 perc = -1;
1297 //THIS RULE MUST ALWAYS apply: dts <= SCR + 0.7 seconds
1298 for(i = 0; i < muxer->avih.dwStreams; i++)
1300 spriv = muxer->streams[i]->priv;
1302 p.len = 0;
1303 get_packet_stats(priv, muxer->streams[i], &p, 0);
1305 if(spriv->track_bufsize + p.len > spriv->max_buffer_size)
1306 continue;
1307 if(p.frame_pts && p.frame_dts > priv->scr + 63000*300)
1308 continue;
1310 if(spriv->framebuf[0].dts <= dts)
1312 dts = spriv->framebuf[0].dts;
1313 ndts = i;
1316 if(conf_interleaving2)
1318 sperc = (spriv->track_bufsize * 1024) / spriv->max_buffer_size;
1319 if(sperc < perc)
1321 ndts = i;
1322 perc = sperc;
1327 return ndts;
1330 static void patch_seq(muxer_priv_t *priv, unsigned char *buf)
1332 if(priv->vwidth > 0)
1334 buf[4] = (priv->vwidth >> 4) & 0xff;
1335 buf[5] &= 0x0f;
1336 buf[5] |= (priv->vwidth & 0x0f) << 4;
1339 if(priv->vheight > 0)
1341 buf[5] &= 0xf0;
1342 buf[5] |= (priv->vheight >> 8) & 0x0f;
1343 buf[6] = priv->vheight & 0xff;
1346 if(priv->vaspect > 0)
1347 buf[7] = (buf[7] & 0x0f) | (priv->vaspect << 4);
1349 if(priv->vframerate > 0)
1350 buf[7] = (buf[7] & 0xf0) | priv->vframerate;
1352 if(priv->vbitrate > 0)
1354 buf[8] = (priv->vbitrate >> 10);
1355 buf[9] = (priv->vbitrate >> 2);
1356 buf[10] = (buf[10] & 0x3f) | (unsigned char) ((priv->vbitrate & 0x4) << 2);
1360 static void patch_panscan(muxer_priv_t *priv, unsigned char *buf)
1361 { //patches sequence display extension (display_horizontal_size and display_vertical_size)
1362 //1:
1363 int offset = 1;
1365 if(buf[0] & 0x01)
1366 offset += 3;
1368 if(priv->panscan_width > 0)
1370 buf[offset] = (priv->panscan_width >> 6);
1371 buf[offset+1] = ((priv->panscan_width & 0x3F) << 2) | (buf[offset + 1] & 0x03);
1374 offset++;
1376 if(priv->panscan_height > 0)
1378 buf[offset] = (priv->panscan_height >> 13) << 7;
1379 buf[offset+1] = (priv->panscan_height >> 5) & 0xFF;
1380 buf[offset+2] = ((priv->panscan_height & 0x1F) << 3) | (buf[offset+2] & 0x07);
1385 static void update_scr(muxer_t *muxer)
1387 muxer_priv_t *priv = muxer->priv;
1388 muxer_stream_t *stream;
1389 muxer_headers_t *spriv;
1390 int i, j;
1391 uint64_t mindts = (uint64_t) -1;
1393 priv->scr += priv->delta_scr;
1395 for(i = 0; i < muxer->avih.dwStreams; i++)
1397 stream = muxer->streams[i];
1398 spriv = stream->priv;
1399 if(spriv->framebuf_used && spriv->framebuf[0].dts < mindts)
1400 mindts = spriv->framebuf[0].dts;
1403 mp_msg(MSGT_MUXER, MSGL_DBG2, "UPDATE SCR TO %"PRIu64" (%.3lf)\n", priv->scr, (double) (priv->scr/27000000.0));
1405 for(i = 0; i < muxer->avih.dwStreams; i++)
1407 stream = muxer->streams[i];
1408 spriv = stream->priv;
1410 j = 0;
1411 while(j < spriv->track_pos && priv->scr >= spriv->buffer_track[j].dts)
1413 spriv->track_bufsize -= spriv->buffer_track[j].size;
1414 j++;
1416 if(spriv->track_bufsize < 0)
1418 double d;
1419 muxer->sysrate = (muxer->sysrate * 11) / 10; //raise by 10%
1420 d = (double) priv->packet_size / (double)muxer->sysrate;
1421 priv->delta_scr = (uint64_t) (d * 27000000.0f);
1422 mp_msg(MSGT_MUXER, MSGL_INFO, "\r\nBUFFER UNDEFLOW at stream %d, raising muxrate to %d kb/s, delta_scr: %"PRIu64"\r\n", i, muxer->sysrate/125, priv->delta_scr);
1423 spriv->track_bufsize = 0;
1426 if(j > 0)
1428 memmove(spriv->buffer_track, &(spriv->buffer_track[j]), (spriv->track_len - j) * sizeof(buffer_track_t));
1429 spriv->track_pos -= j;
1430 for(j = spriv->track_pos; j < spriv->track_len; j++)
1431 spriv->buffer_track[j].size = 0;
1434 if(spriv->framebuf_used && spriv->framebuf[0].dts < mindts)
1435 mindts = spriv->framebuf[0].dts;
1440 static int calc_frames_to_flush(muxer_headers_t *vpriv)
1442 int n, found = 0;
1444 if(vpriv->framebuf_used > 0)
1446 n = 0;
1447 //let's count how many frames we'll store in the next pack sequence
1448 mp_msg(MSGT_MUXER, MSGL_DBG2, "\n");
1449 while(n < vpriv->framebuf_used)
1451 mp_msg(MSGT_MUXER, MSGL_DBG2, "CALC_FRAMES, n=%d, type=%c, pts=%.3lf\n", n, FTYPE(vpriv->framebuf[n].type), (double)vpriv->framebuf[n].pts/27000000.0f);
1452 if(n+1 < vpriv->framebuf_used)
1453 mp_msg(MSGT_MUXER, MSGL_DBG2, "n+1=%d, type=%c, pts=%.3lf\n", n+1, FTYPE(vpriv->framebuf[n+1].type), (double)vpriv->framebuf[n+1].pts/27000000.0f);
1455 if(vpriv->framebuf[n].type == I_FRAME)
1457 if(n > 0)
1459 found = 1;
1460 break;
1464 n++;
1468 if(found && (n < vpriv->framebuf_used+1))
1469 return n;
1470 else
1471 return 0;
1474 static int flush_buffers(muxer_t *muxer, int finalize)
1476 int i, n, found;
1477 int skip_cnt;
1478 uint64_t init_delay = 0;
1479 muxer_stream_t *s, *vs, *as;
1480 muxer_headers_t *vpriv = NULL, *apriv = NULL;
1481 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
1482 double duration;
1483 uint64_t iduration, iaduration;
1486 analyzes all streams and decides what to flush
1487 trying to respect an interleaving distribution
1488 equal to the v_bitrate/a_bitrate proportion
1490 n = 0;
1491 vs = as = NULL;
1492 found = 0;
1493 for(i = 0; i < muxer->avih.dwStreams; i++)
1495 s = muxer->streams[i];
1496 if(s->type == MUXER_TYPE_VIDEO)
1498 vs = muxer->streams[i];
1499 vpriv = (muxer_headers_t*) vs->priv;
1500 if(!vpriv->is_ready)
1501 return 0;
1502 n = found = calc_frames_to_flush(vpriv);
1504 else if(s->type == MUXER_TYPE_AUDIO)
1505 as = s;
1508 if((! found) && finalize)
1510 if(vpriv != NULL)
1511 found = n = vpriv->framebuf_used;
1514 if(found)
1516 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nVIDEO, FLUSH %d frames (of %d), 0 to %d\n", n, vpriv->framebuf_used, n-1);
1518 vpriv = (muxer_headers_t*) vs->priv;
1520 duration = 0;
1521 iduration = 0;
1522 for(i = 0; i < n; i++)
1523 iduration += vpriv->framebuf[i].idur;
1524 duration = (double) (iduration / 27000000.0);
1526 if(as != NULL)
1528 apriv = (muxer_headers_t*) as->priv;
1529 iaduration = 0;
1530 for(i = 0; i < apriv->framebuf_used; i++)
1532 iaduration += apriv->framebuf[i].idur;
1534 if(iaduration < iduration)
1536 mp_msg(MSGT_MUXER, MSGL_DBG2, "Not enough audio data exit\n");
1537 return 0;
1541 if(as != NULL && (apriv->size == 0))
1543 init_delay = vpriv->framebuf[0].pts - vpriv->framebuf[0].dts;
1545 for(i = 0; i < apriv->framebuf_cnt; i++)
1547 apriv->framebuf[i].pts += init_delay;
1548 apriv->framebuf[i].dts += init_delay;
1550 apriv->last_pts += init_delay;
1551 mp_msg(MSGT_MUXER, MSGL_DBG2, "\r\nINITIAL VIDEO DELAY: %.3lf, currAPTS: %.3lf\r\n", (double) init_delay/27000000.0f, (double) apriv->last_pts/27000000.0f);
1554 if((priv->is_xvcd || priv->is_xsvcd) && (vpriv->size == 0))
1555 vpriv->buffer_size = (conf_vbuf_size ? conf_vbuf_size : (priv->is_xvcd ? 46 : 230))*1024;
1557 i = 0;
1558 skip_cnt = 0;
1560 while(1)
1562 update_scr(muxer);
1563 i = find_best_stream(muxer);
1564 if(i < 0)
1565 continue;
1566 if(!fill_packet(muxer, muxer->streams[i], finalize))
1567 skip_cnt++;
1569 if(skip_cnt == muxer->avih.dwStreams)
1571 found = 0;
1572 break;
1577 muxer->file_end = priv->scr;
1578 return found;
1582 static inline uint64_t parse_fps(float fps)
1584 // 90000 * 300 * 1001 / d , there's no rounding error with any of the admitted framerates
1585 int d = (int)(fps*1001+0.5);
1587 return 27027000000ULL / d;
1591 static int soft_telecine(muxer_priv_t *priv, muxer_headers_t *vpriv, uint8_t *fps_ptr, uint8_t *se_ptr, uint8_t *pce_ptr, int n)
1593 if(! pce_ptr)
1594 return 0;
1595 if(fps_ptr != NULL)
1597 *fps_ptr = (*fps_ptr & 0xf0) | priv->vframerate;
1598 vpriv->nom_delta_pts = parse_fps(conf_vframerate);
1601 //in pce_ptr starting from bit 0 bit 24 is tff, bit 30 is rff,
1602 if(pce_ptr[3] & 0x2)
1604 mp_msg(MSGT_MUXER, MSGL_ERR, "\nERROR! RFF bit is already set, disabling telecining\n");
1605 vpriv->telecine = 0;
1606 return 0;
1609 vpriv->picture.progressive_sequence = 0;
1610 vpriv->picture.progressive_frame = 1;
1611 if(se_ptr)
1612 se_ptr[1] &= 0xf7;
1614 //disable tff and rff and overwrite them with the value in bff_mask
1615 pce_ptr[3] = (pce_ptr[3] & 0x7d) | priv->bff_mask[vpriv->display_frame % MAX_PATTERN_LENGTH];
1616 pce_ptr[4] |= 0x80; //sets progressive frame
1618 vpriv->display_frame += n;
1619 if(! vpriv->vframes)
1620 mp_msg(MSGT_MUXER, MSGL_INFO, "\nENABLED SOFT TELECINING, FPS=%.3f\n",conf_vframerate);
1622 return 1;
1625 static size_t parse_mpeg12_video(muxer_stream_t *s, muxer_priv_t *priv, muxer_headers_t *spriv, float fps, size_t len)
1627 uint8_t *fps_ptr = NULL; //pointer to the fps byte in the sequence header
1628 uint8_t *se_ptr = NULL; //pointer to sequence extension
1629 uint8_t *pce_ptr = NULL; //pointer to picture coding extension
1630 int frames_diff, d1, gop_reset = 0; //how any frames we advanced respect to the last one
1631 int ret;
1632 int i, err;
1633 uint32_t temp_ref;
1634 int pt;
1636 mp_msg(MSGT_MUXER, MSGL_DBG2,"parse_mpeg12_video, len=%u\n", (uint32_t) len);
1637 if(s->buffer[0] != 0 || s->buffer[1] != 0 || s->buffer[2] != 1 || len<6)
1639 mp_msg(MSGT_MUXER, MSGL_ERR,"Unknown video format, possibly non-MPEG1/2 stream, len=%d!\n", len);
1640 return 0;
1643 temp_ref = 0;
1644 pt = 0;
1645 err = 0;
1646 i = 0;
1647 while(i + 4 < len)
1648 { // Video (0) Sequence header (b3) or GOP (b8)
1649 if((s->buffer[i] == 0) && (s->buffer[i+1] == 0) && (s->buffer[i+2] == 1))
1651 switch(s->buffer[i+3])
1653 case 0xb3: //sequence
1655 if(i + 11 > len)
1657 err=1;
1658 break;
1660 fps_ptr = &(s->buffer[i+7]);
1661 mp_header_process_sequence_header(&(spriv->picture), &(s->buffer[i+4]));
1662 spriv->delta_pts = spriv->nom_delta_pts = parse_fps(spriv->picture.fps);
1664 spriv->delta_clock = (double) 1/fps;
1665 //the 2 lines below are needed to handle non-standard frame rates (such as 18)
1666 if(! spriv->delta_pts)
1667 spriv->delta_pts = spriv->nom_delta_pts = (uint64_t) ((double)27000000.0 * spriv->delta_clock );
1668 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nFPS: %.3f, FRAMETIME: %.3lf\n", fps, (double)1/fps);
1669 if(priv->patch_seq)
1670 patch_seq(priv, &(s->buffer[i]));
1672 break;
1674 case 0xb5:
1675 if(i + 9 > len)
1677 err = 1;
1678 break;
1680 mp_header_process_extension(&(spriv->picture), &(s->buffer[i+4]));
1681 if(((s->buffer[i+4] & 0xf0) == 0x10))
1682 se_ptr = &(s->buffer[i+4]);
1683 if(((s->buffer[i+4] & 0xf0) == 0x20))
1685 if(priv->patch_sde)
1686 patch_panscan(priv, &(s->buffer[i+4]));
1688 if((s->buffer[i+4] & 0xf0) == 0x80)
1690 pce_ptr = &(s->buffer[i+4]);
1692 break;
1694 case 0xb8:
1695 gop_reset = 1;
1696 break;
1698 case 0x00:
1699 if(i + 5 > len)
1701 err = 1;
1702 break;
1704 pt = (s->buffer[i+5] & 0x1c) >> 3;
1705 temp_ref = (s->buffer[i+4]<<2)+(s->buffer[i+5]>>6);
1706 break;
1708 if(err) break; //something went wrong
1709 if(s->buffer[i+3] >= 0x01 && s->buffer[i+3] <= 0xAF) break; //slice, we have already analized what we need
1711 i++;
1713 if(err)
1714 mp_msg(MSGT_MUXER, MSGL_ERR,"Warning: picture too short or broken!\n");
1716 //following 2 lines are workaround: lavf doesn't sync to sequence headers before passing demux_packets
1717 if(!spriv->nom_delta_pts)
1718 spriv->delta_pts = spriv->nom_delta_pts = parse_fps(fps);
1719 if(!spriv->vframes)
1720 spriv->last_tr = spriv->max_tr = temp_ref;
1721 d1 = temp_ref - spriv->last_tr;
1722 if(gop_reset)
1723 frames_diff = spriv->max_tr + 1 + temp_ref - spriv->last_tr;
1724 else
1726 if(d1 < -6) //there's a wraparound
1727 frames_diff = spriv->max_tr + 1 + temp_ref - spriv->last_tr;
1728 else if(d1 > 6) //there's a wraparound
1729 frames_diff = spriv->max_tr + 1 + spriv->last_tr - temp_ref;
1730 else if(!d1) //pre-emptive fix against broken sequences
1731 frames_diff = 1;
1732 else
1733 frames_diff = d1;
1735 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nLAST: %d, TR: %d, GOP: %d, DIFF: %d, MAX: %d, d1: %d\n",
1736 spriv->last_tr, temp_ref, gop_reset, frames_diff, spriv->max_tr, d1);
1738 if(temp_ref > spriv->max_tr || gop_reset)
1739 spriv->max_tr = temp_ref;
1741 spriv->last_tr = temp_ref;
1742 if(spriv->picture.mpeg1 == 0)
1744 if(spriv->telecine && pce_ptr)
1746 soft_telecine(priv, spriv, fps_ptr, se_ptr, pce_ptr, frames_diff);
1747 spriv->picture.display_time = 100;
1748 mp_header_process_extension(&(spriv->picture), pce_ptr);
1749 if(spriv->picture.display_time >= 50 && spriv->picture.display_time <= 300)
1750 spriv->delta_pts = (spriv->nom_delta_pts * spriv->picture.display_time) / 100;
1754 if(! spriv->vframes)
1755 frames_diff = 1;
1757 spriv->last_dts += spriv->delta_pts;
1758 spriv->last_pts += spriv->nom_delta_pts*(frames_diff-1) + spriv->delta_pts;
1760 ret = add_frame(spriv, spriv->delta_pts, s->buffer, len, pt, spriv->last_dts, spriv->last_pts);
1761 if(ret < 0)
1763 mp_msg(MSGT_MUXER, MSGL_FATAL, "\r\nPARSE_MPEG12: add_frames(%d) failed, exit\r\n", len);
1764 return 0;
1766 mp_msg(MSGT_MUXER, MSGL_DBG2, "\r\nVIDEO FRAME, PT: %C, tr: %d, diff: %d, dts: %.3lf, pts: %.3lf, pdt: %u, gop_reset: %d\r\n",
1767 ftypes[pt], temp_ref, frames_diff, ((double) spriv->last_dts/27000000.0f),
1768 ((double) spriv->last_pts/27000000.0f), spriv->picture.display_time, gop_reset);
1770 if(pt == B_FRAME)
1772 int j, n, adj = 0;
1773 int64_t diff = spriv->last_dts - spriv->last_pts;
1775 if(diff != 0)
1777 n = spriv->framebuf_used - 1;
1779 for(j = n; j >= 0; j--)
1781 if(spriv->framebuf[j].pts >= spriv->last_pts)
1783 spriv->framebuf[j].pts += diff;
1784 adj++;
1787 mp_msg(MSGT_MUXER, MSGL_V, "\r\nResynced B-frame by %d units, DIFF: %"PRId64" (%.3lf),[pd]ts=%.3lf\r\n",
1788 n, diff, (double) diff/27000000.0f, (double) spriv->last_pts/27000000.0f);
1789 spriv->last_pts = spriv->last_dts;
1792 spriv->vframes++;
1794 mp_msg(MSGT_MUXER, MSGL_DBG2,"parse_mpeg12_video, return %u\n", (uint32_t) len);
1795 return len;
1799 static uint64_t fix_mp4_frame_duration(muxer_headers_t *vpriv)
1801 uint64_t mn, md, mx, diff;
1802 uint32_t i;
1804 mn = mx = vpriv->framebuf[0].pts;
1805 for(i = 0; i < 3; i++)
1807 mp_msg(MSGT_DECVIDEO,MSGL_DBG2, "PTS: %"PRIu64"\n", vpriv->framebuf[i].pts);
1808 if(vpriv->framebuf[i].pts < mn)
1809 mn = vpriv->framebuf[i].pts;
1810 if(vpriv->framebuf[i].pts > mx)
1811 mx = vpriv->framebuf[i].pts;
1813 md = mn;
1814 for(i=0; i<3; i++)
1816 if((vpriv->framebuf[i].pts > mn) && (vpriv->framebuf[i].pts < mx))
1817 md = vpriv->framebuf[i].pts;
1820 if(mx - md > md - mn)
1821 diff = md - mn;
1822 else
1823 diff = mx - md;
1825 mp_msg(MSGT_DECVIDEO,MSGL_DBG2, "MIN: %"PRIu64", mid: %"PRIu64", max: %"PRIu64", diff: %"PRIu64"\n", mn, md, mx, diff);
1826 if(diff > 0)
1828 for(i=0; i<3; i++)
1830 vpriv->framebuf[i].pts += diff;
1831 vpriv->framebuf[i].dts += i * diff;
1832 mp_msg(MSGT_MUXER, MSGL_DBG2, "FIXED_PTS: %.3lf, FIXED_DTS: %.3lf\n",
1833 (double) (vpriv->framebuf[i].pts/27000000.0), (double) (vpriv->framebuf[i].dts/27000000.0));
1835 return diff;
1837 else
1838 return 0;
1842 static size_t parse_mpeg4_video(muxer_stream_t *s, muxer_priv_t *priv, muxer_headers_t *vpriv, float fps, size_t len)
1844 size_t ptr = 0;
1845 int64_t delta_pts=0;
1846 uint8_t pt;
1847 int ret;
1849 mp_msg(MSGT_MUXER, MSGL_DBG2,"parse_mpeg4_video, len=%u\n", (uint32_t) len);
1850 if(len<6)
1852 mp_msg(MSGT_MUXER, MSGL_ERR,"Frame too short: %d, exit!\n", len);
1853 return 0;
1856 pt = 0;
1857 while(ptr < len - 5)
1859 if(s->buffer[ptr] != 0 || s->buffer[ptr+1] != 0 || s->buffer[ptr+2] != 1)
1861 ptr++;
1862 continue;
1865 if(s->buffer[ptr+3] >= 0x20 && s->buffer[ptr+3] <= 0x2f) //VOL
1867 mp4_header_process_vol(&(vpriv->picture), &(s->buffer[ptr+4]));
1869 else if(s->buffer[ptr+3] == 0xb3) //gov
1871 //fprintf(stderr, "\nGOV\n");
1873 else if(s->buffer[ptr+3] == 0xb6) //vop
1875 int32_t delta;
1876 mp4_header_process_vop(&(vpriv->picture), &(s->buffer[ptr+4]));
1878 delta = vpriv->picture.timeinc_unit - vpriv->last_tr;
1879 if((delta > 0) && (delta > (vpriv->picture.timeinc_resolution/2)))
1880 delta -= vpriv->picture.timeinc_resolution;
1881 else if((delta < 0) && (delta < (-(vpriv->picture.timeinc_resolution/2))))
1882 delta += vpriv->picture.timeinc_resolution;
1884 delta_pts = (27000000 * (int64_t) delta) / vpriv->picture.timeinc_resolution;
1885 //warning, it seems that packed bops can lead to delta == 0
1887 pt = vpriv->picture.picture_type + 1;
1888 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nTYPE: %c, RESOLUTION: %d, TEMP: %d, delta: %d, delta_pts: %"PRId64" = %.3lf, delta2: %.3lf\n",
1889 FTYPE(pt), vpriv->picture.timeinc_resolution, vpriv->picture.timeinc_unit, delta, delta_pts, (double) (delta_pts/27000000.0),
1890 (double) delta / (double) vpriv->picture.timeinc_resolution);
1892 vpriv->last_tr = vpriv->picture.timeinc_unit;
1894 break;
1897 ptr++;
1900 if(vpriv->vframes)
1902 vpriv->last_dts += vpriv->frame_duration;
1903 vpriv->last_pts += delta_pts;
1906 ret = add_frame(vpriv, delta_pts, s->buffer, len, pt, vpriv->last_dts, vpriv->last_pts);
1907 if(ret < 0)
1909 mp_msg(MSGT_MUXER, MSGL_FATAL, "\r\nPARSE_MPEG4: add_frames(%d) failed, exit\r\n", len);
1910 return 0;
1913 if(!vpriv->frame_duration && vpriv->framebuf_used == 3)
1915 vpriv->frame_duration = fix_mp4_frame_duration(vpriv);
1916 if(vpriv->frame_duration)
1918 vpriv->last_pts += vpriv->frame_duration;
1919 vpriv->last_dts = vpriv->framebuf[vpriv->framebuf_used-1].dts;
1920 vpriv->delta_clock = ((double) vpriv->frame_duration)/27000000.0;
1921 mp_msg(MSGT_MUXER, MSGL_INFO, "FRAME DURATION: %"PRIu64" %.3lf\n",
1922 vpriv->frame_duration, (double) (vpriv->frame_duration/27000000.0));
1923 vpriv->is_ready = 1;
1927 mp_msg(MSGT_MUXER, MSGL_DBG2, "LAST_PTS: %.3lf, LAST_DTS: %.3lf\n",
1928 (double) (vpriv->last_pts/27000000.0), (double) (vpriv->last_dts/27000000.0));
1930 vpriv->vframes++;
1932 return len;
1936 static int fill_last_frame(muxer_headers_t *spriv, uint8_t *ptr, int len)
1938 int idx;
1940 if(!len)
1941 return 0;
1943 if(spriv->framebuf_used == 0)
1944 idx = spriv->framebuf_used;
1945 else
1946 idx = spriv->framebuf_used - 1;
1948 if(spriv->framebuf[idx].alloc_size < spriv->framebuf[idx].size + len)
1950 if(spriv->framebuf[idx].size > SIZE_MAX - (size_t)len)
1951 return 0;
1952 spriv->framebuf[idx].buffer = (uint8_t*) realloc(spriv->framebuf[idx].buffer, spriv->framebuf[idx].size + len);
1953 if(! spriv->framebuf[idx].buffer)
1954 return 0;
1955 spriv->framebuf[idx].alloc_size = spriv->framebuf[idx].size + len;
1958 memcpy(&(spriv->framebuf[idx].buffer[spriv->framebuf[idx].size]), ptr, len);
1959 spriv->framebuf[idx].size += len;
1961 return len;
1964 static int add_frame(muxer_headers_t *spriv, uint64_t idur, uint8_t *ptr, int len, uint8_t pt, uint64_t dts, uint64_t pts)
1966 int idx;
1968 idx = spriv->framebuf_used;
1969 if(idx >= spriv->framebuf_cnt)
1971 spriv->framebuf = (mpeg_frame_t*) realloc_struct(spriv->framebuf, (spriv->framebuf_cnt+1), sizeof(mpeg_frame_t));
1972 if(spriv->framebuf == NULL)
1974 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't realloc frame buffer(idx), abort\n");
1975 return -1;
1978 spriv->framebuf[spriv->framebuf_cnt].size = 0;
1979 spriv->framebuf[spriv->framebuf_cnt].alloc_size = 0;
1980 spriv->framebuf[spriv->framebuf_cnt].pos = 0;
1982 spriv->framebuf[spriv->framebuf_cnt].buffer = (uint8_t*) malloc(len);
1983 if(spriv->framebuf[spriv->framebuf_cnt].buffer == NULL)
1985 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't realloc frame buffer(frame), abort\n");
1986 return -1;
1988 spriv->framebuf[spriv->framebuf_cnt].alloc_size = len;
1989 spriv->framebuf_cnt++;
1992 if(spriv->framebuf[idx].alloc_size < spriv->framebuf[idx].size + len)
1994 if(spriv->framebuf[idx].size > SIZE_MAX - (size_t)len)
1996 mp_msg(MSGT_MUXER, MSGL_FATAL, "Size overflow, couldn't realloc frame buffer(frame), abort\n");
1997 return -1;
1999 spriv->framebuf[idx].buffer = realloc(spriv->framebuf[idx].buffer, spriv->framebuf[idx].size + len);
2000 if(spriv->framebuf[idx].buffer == NULL)
2002 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't realloc frame buffer(frame), abort\n");
2003 return -1;
2005 spriv->framebuf[idx].alloc_size = spriv->framebuf[idx].size + len;
2008 memcpy(&(spriv->framebuf[idx].buffer[spriv->framebuf[idx].size]), ptr, len);
2009 spriv->framebuf[idx].size += len;
2010 spriv->framebuf[idx].pos = 0;
2011 spriv->framebuf[idx].type = pt;
2013 spriv->framebuf[idx].idur = idur;
2014 spriv->framebuf[idx].dts = dts;
2015 spriv->framebuf[idx].pts = pts;
2016 spriv->framebuf_used++;
2017 mp_msg(MSGT_MUXER, MSGL_DBG2, "\r\nAdded frame, size: %u, idur: %"PRIu64", dts: %"PRIu64", pts: %"PRIu64", used: %u\r\n", len, idur, dts, pts, spriv->framebuf_used);
2019 return idx;
2022 static int analyze_mpa(muxer_stream_t *s)
2024 int i = 0, len, max, chans, srate, spf, layer;
2025 int score[4] = {0, 0, 0, 0};
2027 while(i < s->b_buffer_len + 3)
2029 if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xE0) == 0xE0))
2031 len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL);
2032 if(len > 0 && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len))
2034 score[layer]++;
2035 i += len;
2038 i++;
2041 max = 0;
2042 layer = 2;
2043 for(i = 1; i <= 3; i++)
2045 if(score[i] >= max)
2047 max = score[i];
2048 layer = i;
2052 return layer; //actual layer with the highest score
2055 extern int aac_parse_frame(uint8_t *buf, int *srate, int *num);
2057 static int parse_audio(muxer_stream_t *s, int finalize, unsigned int *nf, double *timer, double delay, int drop)
2059 int i, j, len, chans, srate, spf, layer, dummy, tot, num, frm_idx;
2060 int finished;
2061 unsigned int frames;
2062 uint64_t idur;
2063 double dur;
2064 muxer_headers_t *spriv = (muxer_headers_t *) s->priv;
2066 i = tot = frames = 0;
2067 finished = 0;
2068 while(1)
2070 len = 0;
2071 switch(s->wf->wFormatTag)
2073 case AUDIO_MP2:
2074 case AUDIO_MP3:
2076 if(i + 3 >= s->b_buffer_len)
2078 finished = 1;
2079 break;
2082 if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xE0) == 0xE0))
2084 len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL);
2085 if(len > 0 && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len)
2086 && layer == spriv->mpa_layer)
2088 dur = (double) spf / (double) srate;
2089 idur = (27000000ULL * spf) / srate;
2091 else
2092 len = 0;
2095 break;
2097 case AUDIO_A52:
2099 if(i + 6 >= s->b_buffer_len)
2101 finished = 1;
2102 break;
2105 if(s->b_buffer[i] == 0x0B && s->b_buffer[i+1] == 0x77)
2107 srate = 0;
2108 #ifdef USE_LIBA52
2109 len = a52_syncinfo(&(s->b_buffer[i]), &dummy, &srate, &dummy);
2110 #else
2111 len = mp_a52_framesize(&(s->b_buffer[i]), &srate);
2112 #endif
2113 if((len > 0) && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len))
2115 dur = (double) 1536 / (double) srate;
2116 idur = (27000000ULL * 1536) / srate;
2118 else
2119 len = 0;
2122 break;
2124 case AUDIO_AAC1:
2125 case AUDIO_AAC2:
2127 if(i + 7 >= s->b_buffer_len)
2129 finished = 1;
2130 break;
2133 if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xF6) == 0xF0))
2135 len = aac_parse_frame(&(s->b_buffer[i]), &srate, &num);
2136 if((len > 0) && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len))
2138 dur = (double) 1024 / (double) srate;
2139 idur = (27000000ULL * 1024 * num) / srate;
2141 else
2142 len = 0;
2147 if(finished)
2148 break;
2150 if(!len)
2152 i++;
2153 continue;
2156 spriv->timer += dur;
2157 if(spriv->drop_delayed_frames && delay < 0 && spriv->timer <= -delay)
2159 i += len;
2160 tot = i;
2161 continue;
2164 frames++;
2165 fill_last_frame(spriv, &(s->b_buffer[tot]), i - tot);
2166 frm_idx = add_frame(spriv, idur, &(s->b_buffer[i]), len, 0, spriv->last_pts, spriv->last_pts);
2167 if(frm_idx < 0)
2169 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't add audio frame buffer(frame), abort\n");
2170 goto audio_exit;
2172 for(j = frm_idx; j < spriv->framebuf_cnt; j++)
2173 spriv->framebuf[j].pts = spriv->last_pts;
2174 spriv->last_pts += idur;
2176 i += len;
2177 tot = i;
2180 audio_exit:
2181 if(tot)
2183 memmove(s->b_buffer, &(s->b_buffer[tot]), s->b_buffer_len - tot);
2184 s->b_buffer_len -= tot;
2185 s->b_buffer_ptr += tot;
2186 if(s->b_buffer_len > 0)
2187 memmove(s->b_buffer, &(s->b_buffer[s->b_buffer_ptr]), s->b_buffer_len);
2188 s->b_buffer_ptr = 0;
2191 if(finalize)
2193 frm_idx = add_frame(spriv, 0, s->b_buffer, s->b_buffer_len, 0, spriv->last_pts, spriv->last_pts);
2194 if(frm_idx >= 0)
2196 for(j = frm_idx; j < spriv->framebuf_cnt; j++)
2197 spriv->framebuf[j].pts = spriv->last_pts;
2201 *nf = frames;
2202 *timer = spriv->timer;
2204 return tot;
2207 static void fix_parameters(muxer_stream_t *stream)
2209 muxer_headers_t *spriv = stream->priv;
2210 muxer_t *muxer = stream->muxer;
2211 muxer_priv_t *priv = muxer->priv;
2212 uint32_t stream_format;
2213 int needs_psm = 0;
2215 if(stream->type == MUXER_TYPE_AUDIO)
2217 stream_format = stream->wf->wFormatTag;
2218 spriv->is_ready = 1;
2219 if(conf_abuf_size)
2220 spriv->max_buffer_size = conf_abuf_size*1024;
2221 else
2222 spriv->max_buffer_size = 4*1024;
2223 if(stream->wf->wFormatTag == AUDIO_A52)
2225 stream->ckid = be2me_32 (0x1bd);
2226 if(priv->is_genmpeg1 || priv->is_genmpeg2)
2227 fix_audio_sys_header(priv, spriv->id, 0xbd, FFMAX(conf_abuf_size, 58)*1024); //only one audio at the moment
2228 spriv->id = 0xbd;
2229 if(!conf_abuf_size)
2230 spriv->max_buffer_size = 16*1024;
2232 else if(stream->wf->wFormatTag == AUDIO_AAC1 || stream->wf->wFormatTag == AUDIO_AAC2)
2233 needs_psm = 1;
2234 else if(stream->wf->wFormatTag == AUDIO_MP2 || stream->wf->wFormatTag == AUDIO_MP3)
2235 spriv->is_ready = 0;
2237 else //video
2239 stream_format = stream->bih->biCompression;
2240 if(conf_vbuf_size)
2241 spriv->max_buffer_size = conf_vbuf_size*1024;
2242 else
2244 if(priv->is_dvd)
2245 spriv->max_buffer_size = 232*1024;
2246 else if(priv->is_xsvcd)
2247 spriv->max_buffer_size = 230*1024;
2248 else if(priv->is_xvcd)
2249 spriv->max_buffer_size = 46*1024;
2250 else
2251 spriv->max_buffer_size = 232*1024; //no profile => unconstrained :) FIXME!!!
2254 if(is_mpeg4(stream->bih->biCompression))
2255 spriv->is_ready = 0;
2256 else
2257 spriv->is_ready = 1;
2259 if(!is_mpeg1(stream_format) && !is_mpeg2(stream_format))
2260 needs_psm = 1;
2263 if(priv->is_genmpeg2 && needs_psm)
2265 priv->use_psm = 1;
2266 add_to_psm(priv, spriv->id, stream_format);
2267 priv->psm_streams_cnt++;
2272 static void mpegfile_write_chunk(muxer_stream_t *s,size_t len,unsigned int flags, double dts_arg, double pts_arg)
2274 size_t sz = 0;
2275 uint64_t tmp;
2276 muxer_t *muxer = s->muxer;
2277 muxer_priv_t *priv = (muxer_priv_t *)muxer->priv;
2278 muxer_headers_t *spriv = (muxer_headers_t*) s->priv;
2279 float fps;
2280 uint32_t stream_format, nf;
2282 if(s->buffer == NULL || len == -1)
2283 return;
2285 if (s->type == MUXER_TYPE_VIDEO)
2286 { // try to recognize frame type...
2287 fps = (float) s->h.dwRate/ (float) s->h.dwScale;
2288 spriv->type = 1;
2289 stream_format = s->bih->biCompression;
2290 if(! spriv->vframes)
2292 spriv->last_dts = spriv->last_pts - (uint64_t)(27000000.0f/fps);
2293 mp_msg(MSGT_MUXER, MSGL_INFO,"INITV: %.3lf, %.3lf, fps: %.3f\r\n", (double) spriv->last_pts/27000000.0f, (double) spriv->last_dts/27000000.0f, fps);
2296 if(is_mpeg1(stream_format) || is_mpeg2(stream_format))
2298 spriv->is_mpeg12 = 1;
2299 spriv->is_ready = 1;
2300 if(len)
2301 sz = parse_mpeg12_video(s, priv, spriv, fps, len);
2302 else
2304 tmp = (uint64_t) (27000000.0f / fps);
2305 spriv->last_pts += tmp;
2306 spriv->last_dts += tmp;
2309 else if(is_mpeg4(stream_format))
2311 spriv->is_mpeg12 = 0;
2312 spriv->telecine = 0;
2313 if(len)
2314 sz = parse_mpeg4_video(s, priv, spriv, fps, len);
2315 else
2317 tmp = (uint64_t) (27000000.0f / fps);
2318 spriv->last_pts += tmp;
2319 spriv->last_dts += tmp;
2323 mp_msg(MSGT_MUXER, MSGL_DBG2,"mpegfile_write_chunk, Video codec=%x, len=%u, mpeg12 returned %u\n", stream_format, (uint32_t) len, (uint32_t) sz);
2325 else
2326 { // MUXER_TYPE_AUDIO
2327 double fake_timer;
2328 spriv->type = 0;
2329 stream_format = s->wf->wFormatTag;
2331 if(s->b_buffer_size - s->b_buffer_len < len)
2333 void *tmp;
2335 if(s->b_buffer_len > SIZE_MAX - len)
2337 mp_msg(MSGT_MUXER, MSGL_FATAL, "\nFATAL! couldn't realloc, integer overflow\n");
2338 return;
2340 tmp = realloc(s->b_buffer, len + s->b_buffer_len);
2341 if(!tmp)
2343 mp_msg(MSGT_MUXER, MSGL_FATAL, "\nFATAL! couldn't realloc %d bytes\n", len + s->b_buffer_len);
2344 return;
2346 s->b_buffer = tmp;
2348 s->b_buffer_size = len + s->b_buffer_len;
2349 mp_msg(MSGT_MUXER, MSGL_DBG2, "REALLOC(%d) bytes to AUDIO backbuffer\n", s->b_buffer_size);
2351 memcpy(&(s->b_buffer[s->b_buffer_ptr + s->b_buffer_len]), s->buffer, len);
2352 s->b_buffer_len += len;
2354 if(!spriv->is_ready)
2356 if(s->b_buffer_len >= 32*1024)
2358 spriv->mpa_layer = analyze_mpa(s);
2359 spriv->is_ready = 1;
2362 else
2364 parse_audio(s, 0, &nf, &fake_timer, priv->init_adelay, priv->drop);
2365 spriv->vframes += nf;
2366 if(! spriv->vframes)
2367 mp_msg(MSGT_MUXER, MSGL_INFO, "AINIT: %.3lf\r\n", (double) spriv->last_pts/27000000.0f);
2371 flush_buffers(muxer, 0);
2375 static void mpegfile_write_index(muxer_t *muxer)
2377 int i, nf;
2378 double fake_timer;
2379 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
2381 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingTrailer);
2383 for(i = 0; i < muxer->avih.dwStreams; i++)
2385 if(muxer->streams[i]->type == MUXER_TYPE_AUDIO)
2386 parse_audio(muxer->streams[i], 1, &nf, &fake_timer, priv->init_adelay, priv->drop);
2388 while(flush_buffers(muxer, 0) > 0);
2389 flush_buffers(muxer, 1);
2390 if(priv->is_genmpeg1 || priv->is_genmpeg2)
2392 priv->scr = 0;
2393 write_mpeg_pack(muxer, NULL, muxer->stream, 1); //insert fake Nav Packet
2396 mp_msg(MSGT_MUXER, MSGL_INFO, "\nOverhead: %.3lf%% (%"PRIu64" / %"PRIu64")\n", 100.0 * (double)priv->headers_size / (double)priv->data_size, priv->headers_size, priv->data_size);
2399 static void mpegfile_write_header(muxer_t *muxer)
2401 muxer_priv_t *priv = (muxer_priv_t*) muxer->priv;
2403 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingHeader);
2405 priv->headers_cnt++;
2407 //write the first system header only for generic mpeg1/2 muxes, and only when we have collected all necessary infos
2408 if(priv->is_genmpeg1 || priv->is_genmpeg2 || ((priv->is_xvcd || priv->is_xsvcd) && (priv->headers_cnt == 1)))
2410 write_mpeg_pack(muxer, NULL, muxer->stream, 0);
2411 priv->update_system_header = 0;
2414 return;
2417 static void setup_sys_params(muxer_priv_t *priv)
2419 if(priv->is_dvd)
2421 int v = (conf_vbuf_size ? conf_vbuf_size : 232);
2422 int a1 = (conf_abuf_size ? conf_abuf_size : 4);
2423 int a2 = (conf_abuf_size>58 ? conf_abuf_size : 58);
2425 priv->sys_info.cnt = 4;
2427 priv->sys_info.streams[0].id = 0xb9;
2428 priv->sys_info.streams[0].type = 1;
2429 priv->sys_info.streams[0].bufsize = v*1024;
2431 priv->sys_info.streams[1].id = 0xb8;
2432 priv->sys_info.streams[1].type = 0;
2433 priv->sys_info.streams[1].bufsize = a1*1024;
2435 priv->sys_info.streams[2].id = 0xbd;
2436 priv->sys_info.streams[2].type = 1;
2437 priv->sys_info.streams[2].bufsize = a2*1024;
2439 priv->sys_info.streams[3].id = 0xbf;
2440 priv->sys_info.streams[3].type = 1;
2441 priv->sys_info.streams[3].bufsize = 2*1024;
2443 else if(priv->is_xvcd || priv->is_xsvcd)
2445 int v = (conf_vbuf_size ? conf_vbuf_size : (priv->is_xvcd ? 46: 230));
2446 int a1 = (conf_abuf_size ? conf_abuf_size : 4);
2448 priv->sys_info.cnt = 2;
2450 priv->sys_info.streams[0].id = 0xe0;
2451 priv->sys_info.streams[0].type = 1;
2452 priv->sys_info.streams[0].bufsize = v*1024;
2454 priv->sys_info.streams[1].id = 0xc0;
2455 priv->sys_info.streams[1].type = 0;
2456 priv->sys_info.streams[1].bufsize = a1*1024;
2458 else
2459 priv->sys_info.cnt = 0;
2462 /* excerpt from DGPulldown Copyright (C) 2005-2006, Donald Graft */
2463 static void generate_flags(uint8_t *bff_mask, int source, int target)
2465 unsigned int i, trfp;
2466 uint64_t dfl,tfl;
2467 unsigned char ormask[4] = {0x0, 0x2, 0x80, 0x82};
2469 dfl = (target - source) << 1;
2470 tfl = source >> 1;
2472 trfp = 0;
2473 for(i = 0; i < MAX_PATTERN_LENGTH; i++)
2475 tfl += dfl;
2476 if(tfl >= source)
2478 tfl -= source;
2479 bff_mask[i] = ormask[trfp + 1];
2480 trfp ^= 2;
2482 else
2483 bff_mask[i] = ormask[trfp];
2487 int muxer_init_muxer_mpeg(muxer_t *muxer)
2489 muxer_priv_t *priv;
2490 priv = (muxer_priv_t *) calloc(1, sizeof(muxer_priv_t));
2491 if(priv == NULL)
2492 return 0;
2493 priv->update_system_header = 1;
2495 //calloc() already zero-ed all flags, so we assign only the ones we need
2497 if(conf_mux != NULL)
2499 if(! strcasecmp(conf_mux, "mpeg1"))
2501 priv->mux = MUX_MPEG1;
2502 priv->packet_size = 2048;
2503 priv->is_genmpeg1 = 1;
2504 priv->muxrate = 1800 * 125; //Constrained parameters
2506 else if(! strcasecmp(conf_mux, "dvd"))
2508 priv->mux = MUX_MPEG2;
2509 priv->is_dvd = 1;
2510 priv->packet_size = 2048;
2511 priv->muxrate = 10080 * 125;
2513 else if(! strcasecmp(conf_mux, "xsvcd"))
2515 priv->mux = MUX_MPEG2;
2516 priv->is_xsvcd = 1;
2517 priv->packet_size = 2324;
2518 priv->muxrate = 150*2324;
2519 priv->ts_allframes = 1;
2521 else if(! strcasecmp(conf_mux, "xvcd"))
2523 priv->mux = MUX_MPEG1;
2524 priv->is_xvcd = 1;
2525 priv->packet_size = 2324;
2526 priv->muxrate = 75*2352;
2527 priv->ts_allframes = 1;
2529 else if(! strcasecmp(conf_mux, "pes1"))
2531 priv->mux = MUX_MPEG1;
2532 priv->rawpes = 1;
2533 priv->packet_size = 2048;
2534 priv->muxrate = 10080 * 125;
2535 priv->ts_allframes = 1;
2537 else if(! strcasecmp(conf_mux, "pes2"))
2539 priv->mux = MUX_MPEG2;
2540 priv->rawpes = 1;
2541 priv->packet_size = 2048;
2542 priv->muxrate = 10080 * 125;
2543 priv->ts_allframes = 1;
2545 else
2547 if(strcasecmp(conf_mux, "mpeg2"))
2548 mp_msg(MSGT_MUXER, MSGL_ERR, "Unknown format %s, default to mpeg2\n", conf_mux);
2549 priv->mux = MUX_MPEG2;
2550 priv->is_genmpeg2 = 1;
2551 priv->packet_size = 2048;
2552 priv->muxrate = 1800 * 125; //Constrained parameters
2556 if(conf_ts_allframes)
2557 priv->ts_allframes = 1;
2558 if(conf_muxrate > 0)
2559 priv->muxrate = conf_muxrate * 125; // * 1000 / 8
2560 if(conf_packet_size)
2561 priv->packet_size = conf_packet_size;
2562 priv->delta_scr = (uint64_t) (90000.0f*300.0f*(double)priv->packet_size/(double)priv->muxrate);
2563 mp_msg(MSGT_MUXER, MSGL_INFO, "PACKET SIZE: %u bytes, deltascr: %"PRIu64"\n", priv->packet_size, priv->delta_scr);
2564 setup_sys_params(priv);
2566 if(conf_vaspect > 0)
2568 int asp = (int) (conf_vaspect * 1000.0f);
2569 if(asp >= 1332 && asp <= 1334)
2570 priv->vaspect = ASPECT_4_3;
2571 else if(asp >= 1776 && asp <= 1778)
2572 priv->vaspect = ASPECT_16_9;
2573 else if(asp >= 2209 && asp <= 2211)
2574 priv->vaspect = ASPECT_2_21_1;
2575 else if(asp == 1000)
2576 priv->vaspect = ASPECT_1_1;
2577 else
2578 mp_msg(MSGT_MUXER, MSGL_ERR, "ERROR: unrecognized aspect %.3f\n", conf_vaspect);
2581 priv->vframerate = 0; // no change
2582 if(conf_telecine && conf_vframerate > 0)
2584 mp_msg(MSGT_MUXER, MSGL_ERR, "ERROR: options 'telecine' and 'vframerate' are mutually exclusive, vframerate disabled\n");
2585 conf_vframerate = 0;
2588 if(conf_telecine == TELECINE_FILM2PAL)
2590 if(conf_telecine_src==0.0f) conf_telecine_src = 24000.0/1001.0;
2591 conf_telecine_dest = 25;
2592 conf_telecine = TELECINE_DGPULLDOWN;
2594 else if(conf_telecine == PULLDOWN32)
2596 if(conf_telecine_src==0.0f) conf_telecine_src = 24000.0/1001.0;
2597 conf_telecine_dest = 30000.0/1001.0;
2598 conf_telecine = TELECINE_DGPULLDOWN;
2601 if(conf_telecine_src>0 && conf_telecine_dest>0 && conf_telecine_src < conf_telecine_dest)
2603 int sfps, tfps;
2605 sfps = (int) (conf_telecine_src * 1001 + 0.5);
2606 tfps = (int) (conf_telecine_dest * 1001 + 0.5);
2607 if(sfps % 2 || tfps % 2)
2609 sfps *= 2;
2610 tfps *= 2;
2613 if(((tfps - sfps)>>1) > sfps)
2615 mp_msg(MSGT_MUXER, MSGL_ERR, "ERROR! Framerate increment must be <= 1.5, telecining disabled\n");
2616 conf_telecine = 0;
2618 else
2620 generate_flags(priv->bff_mask, sfps, tfps);
2621 conf_telecine = TELECINE_DGPULLDOWN;
2622 conf_vframerate = conf_telecine_dest;
2626 if(conf_vframerate)
2628 int fps;
2630 fps = (int) (conf_vframerate * 1001 + 0.5);
2631 switch(fps)
2633 case 24000:
2634 priv->vframerate = FRAMERATE_23976;
2635 break;
2636 case 24024:
2637 priv->vframerate = FRAMERATE_24;
2638 break;
2639 case 25025:
2640 priv->vframerate = FRAMERATE_25;
2641 break;
2642 case 30000:
2643 priv->vframerate = FRAMERATE_2997;
2644 break;
2645 case 30030:
2646 priv->vframerate = FRAMERATE_30;
2647 break;
2648 case 50050:
2649 priv->vframerate = FRAMERATE_50;
2650 break;
2651 case 60000:
2652 priv->vframerate = FRAMERATE_5994;
2653 break;
2654 case 60060:
2655 priv->vframerate = FRAMERATE_60;
2656 break;
2657 default:
2659 mp_msg(MSGT_MUXER, MSGL_ERR, "WRONG FPS: %d/1000, ignoring\n", fps);
2660 if(conf_telecine)
2661 mp_msg(MSGT_MUXER, MSGL_ERR, "DISABLED TELECINING\n");
2662 conf_telecine = 0;
2667 priv->vwidth = (uint16_t) conf_vwidth;
2668 priv->vheight = (uint16_t) conf_vheight;
2669 priv->panscan_width = (uint16_t) conf_panscan_width;
2670 priv->panscan_height = (uint16_t) conf_panscan_height;
2671 priv->vbitrate = ((conf_vbitrate) * 10) >> 2; //*1000 / 400
2673 if(priv->vaspect || priv->vframerate || priv->vwidth || priv->vheight || priv->vbitrate || priv->panscan_width || priv->panscan_height)
2675 priv->patch_seq = priv->vaspect || priv->vframerate || priv->vwidth || priv->vheight || priv->vbitrate;
2676 priv->patch_sde = priv->panscan_width || priv->panscan_height;
2677 mp_msg(MSGT_MUXER, MSGL_INFO, "MPEG MUXER, patching");
2678 if(priv->vwidth || priv->vheight)
2679 mp_msg(MSGT_MUXER, MSGL_INFO, " resolution to %dx%d", priv->vwidth, priv->vheight);
2680 if(priv->panscan_width || priv->panscan_height)
2681 mp_msg(MSGT_MUXER, MSGL_INFO, " panscan to to %dx%d", priv->panscan_width, priv->panscan_height);
2682 if(priv->vframerate)
2683 mp_msg(MSGT_MUXER, MSGL_INFO, " framerate to %s fps", framerates[priv->vframerate]);
2684 if(priv->vaspect)
2685 mp_msg(MSGT_MUXER, MSGL_INFO, " aspect ratio to %s", aspect_ratios[priv->vaspect]);
2686 if(priv->vbitrate)
2687 mp_msg(MSGT_MUXER, MSGL_INFO, " bitrate to %u", conf_vbitrate);
2688 mp_msg(MSGT_MUXER, MSGL_INFO, "\n");
2691 priv->has_video = priv->has_audio = 0;
2693 muxer->sysrate = priv->muxrate; // initial muxrate = constrained stream parameter
2694 priv->scr = muxer->file_end = 0;
2696 if(conf_init_vdelay && conf_drop)
2698 mp_msg(MSGT_MUXER, MSGL_ERR, "\nmuxer_mpg, :drop and :vdelay used together are not supported, exiting\n");
2699 return 0;
2701 if(conf_init_adelay)
2702 priv->init_adelay = - (double) conf_init_adelay / (double) 1000.0;
2704 priv->drop = conf_drop;
2706 priv->buff = (uint8_t *) malloc(priv->packet_size);
2707 if((priv->buff == NULL))
2709 mp_msg(MSGT_MUXER, MSGL_ERR, "\nCouldn't allocate %d bytes, exit\n", priv->packet_size);
2710 return 0;
2713 muxer->priv = (void *) priv;
2714 muxer->cont_new_stream = &mpegfile_new_stream;
2715 muxer->cont_write_chunk = &mpegfile_write_chunk;
2716 muxer->cont_write_header = &mpegfile_write_header;
2717 muxer->cont_write_index = &mpegfile_write_index;
2718 muxer->fix_stream_parameters = &fix_parameters;
2719 return 1;