Replace a mention of MPlayer by MEncoder in the MEncoder section.
[mplayer/glamo.git] / libmpdemux / muxer_mpeg.c
blob7e88686483884a3be2934e029609ea262bf6a9c9
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 "mp_msg.h"
10 #include "help_mp.h"
12 #include "aviheader.h"
13 #include "ms_hdr.h"
15 #include "stream/stream.h"
16 #include "muxer.h"
17 #include "demuxer.h"
18 #include "stheader.h"
19 #include "m_option.h"
20 #include "mpeg_hdr.h"
21 #include "mp3_hdr.h"
22 #include "liba52/a52.h"
24 #define PACK_HEADER_START_CODE 0x01ba
25 #define SYSTEM_HEADER_START_CODE 0x01bb
26 #define PSM_START_CODE 0x01bc
28 #define PES_PRIVATE1 0x01bd
29 #define PES_PRIVATE2 0x01bf
31 #define MUX_MPEG1 1
32 #define MUX_MPEG2 2
34 #define VIDEO_MPEG1 0x10000001
35 #define VIDEO_MPEG2 0x10000002
36 #define AUDIO_MP2 0x50
37 #define AUDIO_MP3 0x55
38 #define AUDIO_A52 0x2000
39 #define AUDIO_LPCM 0x10001 /* only a placeholder at the moment */
40 #define AUDIO_AAC1 0x706D
41 //#define AUDIO_AAC2 (short) mmioFOURCC('m','p','4','a')
42 #define AUDIO_AAC2 0x504D
44 #define ASPECT_1_1 1
45 #define ASPECT_4_3 2
46 #define ASPECT_16_9 3
47 #define ASPECT_2_21_1 4
49 #define FRAMERATE_23976 1
50 #define FRAMERATE_24 2
51 #define FRAMERATE_25 3
52 #define FRAMERATE_2997 4
53 #define FRAMERATE_30 5
54 #define FRAMERATE_50 6
55 #define FRAMERATE_5994 7
56 #define FRAMERATE_60 8
58 static char ftypes[] = {'?', 'I', 'P', 'B'};
59 #define FTYPE(x) (ftypes[(x)])
61 static const char *framerates[] = {
62 "unchanged", "23.976", "24", "25", "29.97", "30", "50", "59.94", "60"
65 static const char *aspect_ratios[] = {
66 "unchanged", "1/1", "4/3", "16/9", "2.21/1"
69 static char *conf_mux = "mpeg2";
70 static uint16_t conf_packet_size = 0; //dvd
71 static uint32_t conf_muxrate = 0; //kb/s
72 static float conf_vaspect = 0;
73 static float conf_vframerate = 0;
74 static uint32_t conf_vwidth = 0, conf_vheight = 0, conf_panscan_width = 0, conf_panscan_height = 0;
75 static uint32_t conf_vbitrate = 0;
76 static int conf_init_vpts = 200, conf_init_apts = 200;
77 static int conf_ts_allframes = 0;
78 static int conf_init_adelay = 0, conf_init_vdelay = 0;
79 static int conf_abuf_size = 0;
80 static int conf_vbuf_size = 0;
81 static int conf_drop = 0;
82 static int conf_telecine = 0;
83 static int conf_interleaving2 = 0;
84 static float conf_telecine_src = 0;
85 static float conf_telecine_dest = 0;
87 enum FRAME_TYPE {
88 I_FRAME = 1,
89 P_FRAME = 2,
90 B_FRAME = 3
93 typedef struct {
94 uint8_t *buffer;
95 size_t size;
96 size_t alloc_size;
97 uint8_t type;
98 uint64_t pts, dts, idur;
99 uint32_t pos; //start offset for the frame
100 } mpeg_frame_t;
102 typedef struct {
103 uint8_t cnt; // how many entries we use
104 struct {
105 uint8_t id, type;
106 uint32_t bufsize;
107 uint32_t format;
108 } streams[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
109 } sys_info_t;
111 typedef struct {
112 uint8_t cnt; // how many entries we use
113 struct {
114 uint8_t id;
115 uint8_t type;
116 uint32_t format;
117 } streams[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
118 } psm_info_t;
120 typedef struct {
121 int size;
122 uint64_t dts;
123 } buffer_track_t;
125 typedef struct {
126 uint64_t dts, pts;
127 uint64_t frame_dts, frame_pts;
128 int len, stflen;
129 } pack_stats_t;
131 typedef struct {
132 int mux;
133 sys_info_t sys_info;
134 psm_info_t psm_info;
135 uint16_t packet_size;
136 int is_dvd, is_xvcd, is_xsvcd, is_genmpeg1, is_genmpeg2, rawpes, ts_allframes, has_video, has_audio;
137 int update_system_header, use_psm;
138 off_t headers_size, data_size;
139 uint64_t scr;
140 uint64_t delta_scr;
141 uint64_t last_psm_scr;
142 uint32_t muxrate;
143 uint8_t *buff;
144 uint32_t headers_cnt;
145 double init_adelay;
146 int drop;
148 //video patching parameters
149 uint8_t vaspect, vframerate;
150 uint16_t vwidth, vheight, panscan_width, panscan_height;
151 uint32_t vbitrate;
152 int patch_seq, patch_sde;
153 int psm_streams_cnt;
155 //2 million frames are enough
156 #define MAX_PATTERN_LENGTH 2000000
157 uint8_t bff_mask[MAX_PATTERN_LENGTH];
158 } muxer_priv_t;
161 typedef struct {
162 int has_pts, has_dts, pes_is_aligned, type, min_pes_hlen;
163 int delay_rff;
164 uint64_t pts, last_pts, last_dts, dts, size, frame_duration, delta_pts, nom_delta_pts, last_saved_pts;
165 uint32_t buffer_size;
166 double delta_clock, timer;
167 int drop_delayed_frames;
168 mpeg_frame_t *framebuf;
169 uint16_t framebuf_cnt;
170 uint16_t framebuf_used;
171 int32_t last_tr;
172 int max_tr;
173 uint8_t id, is_mpeg12, telecine;
174 uint64_t vframes;
175 int64_t display_frame;
176 mp_mpeg_header_t picture;
177 int max_buffer_size;
178 buffer_track_t *buffer_track;
179 int track_pos, track_len, track_bufsize; //pos and len control the array, bufsize is the size of the buffer
180 unsigned char *pack;
181 int pack_offset, pes_offset, pes_set, payload_offset;
182 int frames;
183 int last_frame_rest; //the rest of the previous frame
184 int is_ready;
185 int mpa_layer;
186 } muxer_headers_t;
188 #define PULLDOWN32 1
189 #define TELECINE_FILM2PAL 2
190 #define TELECINE_DGPULLDOWN 3
192 m_option_t mpegopts_conf[] = {
193 {"format", &(conf_mux), CONF_TYPE_STRING, M_OPT_GLOBAL, 0 ,0, NULL},
194 {"size", &(conf_packet_size), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 65535, NULL},
195 {"muxrate", &(conf_muxrate), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 12000000, NULL}, //12 Mb/s
196 {"vaspect", &(conf_vaspect), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
197 {"vframerate", &(conf_vframerate), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
198 {"vwidth", &(conf_vwidth), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 4095, NULL},
199 {"vheight", &(conf_vheight), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 4095, NULL},
200 {"vpswidth", &(conf_panscan_width), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 16383, NULL},
201 {"vpsheight", &(conf_panscan_height), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 16383, NULL},
202 {"vbitrate", &(conf_vbitrate), CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 1, 104857599, NULL},
203 {"vdelay", &conf_init_vdelay, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 32760, NULL},
204 {"adelay", &conf_init_adelay, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 0, 32760, NULL},
205 {"vbuf_size", &conf_vbuf_size, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 40, 1194, NULL},
206 {"abuf_size", &conf_abuf_size, CONF_TYPE_INT, M_OPT_GLOBAL|M_OPT_RANGE, 4, 64, NULL},
207 {"drop", &conf_drop, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, 1, NULL},
208 {"tsaf", &conf_ts_allframes, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, 1, NULL},
209 {"telecine", &conf_telecine, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, PULLDOWN32, NULL},
210 {"interleaving2", &conf_interleaving2, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, 1, NULL},
211 {"film2pal", &conf_telecine, CONF_TYPE_FLAG, M_OPT_GLOBAL, 0, TELECINE_FILM2PAL, NULL},
212 {"tele_src", &(conf_telecine_src), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
213 {"tele_dest", &(conf_telecine_dest), CONF_TYPE_FLOAT, M_OPT_GLOBAL, 0, 0, NULL},
214 {NULL, NULL, 0, 0, 0, 0, NULL}
217 static void fix_audio_sys_header(muxer_priv_t *priv, uint8_t id, uint8_t newid, uint32_t size)
219 uint8_t i;
221 for(i = 0; i < priv->sys_info.cnt; i++)
223 if(priv->sys_info.streams[i].id == id)
225 priv->sys_info.streams[i].id = newid;
226 priv->sys_info.streams[i].type = 1;
227 priv->sys_info.streams[i].bufsize = size;
232 static inline int is_mpeg1(uint32_t x)
234 return
235 (x == 0x10000001) ||
236 (x == mmioFOURCC('m','p','g','1')) ||
237 (x == mmioFOURCC('M','P','G','1'));
240 static inline int is_mpeg2(uint32_t x)
242 return
243 (x == 0x10000002) ||
244 (x == mmioFOURCC('m','p','g','2')) ||
245 (x == mmioFOURCC('M','P','G','2')) ||
246 (x == mmioFOURCC('m','p','e','g')) ||
247 (x == mmioFOURCC('M','P','E','G'));
250 static inline int is_mpeg4(uint32_t x)
252 return
253 (x == 0x10000004) ||
254 (x == mmioFOURCC('d','i','v','x')) ||
255 (x == mmioFOURCC('D','I','V','X')) ||
256 (x == mmioFOURCC('x','v','i','d')) ||
257 (x == mmioFOURCC('X','V','I','D')) ||
258 (x == mmioFOURCC('X','v','i','D')) ||
259 (x == mmioFOURCC('x','v','i','x')) ||
260 (x == mmioFOURCC('X','V','I','X')) ||
261 (x == mmioFOURCC('m','p','4','v')) ||
262 (x == mmioFOURCC('M','P','4','V')) ||
263 (x == mmioFOURCC('F', 'M','P','4')) ||
264 (x == mmioFOURCC('f', 'm','p','4')) ||
265 (x == mmioFOURCC('D', 'X','5','0')) ||
266 (x == mmioFOURCC('d', 'x','5','0'));
269 //from unrarlib.c
270 static uint32_t CalcCRC32(uint8_t *buff, uint32_t size)
272 uint32_t i, j, CRCTab[256], crc;
274 for(i = 0;i < 256; i++)
276 for(crc = i, j = 0; j < 8; j++)
277 crc= (crc & 1) ? (crc >> 1)^0xEDB88320L : (crc >> 1);
278 CRCTab[i] = crc;
282 crc = 0xffffffff;
283 for(i = 0; i < size; i++)
284 crc = (crc << 8) ^ CRCTab[((crc >> 24) ^ buff[i]) & 0xff];
286 return crc;
290 static void add_to_psm(muxer_priv_t *priv, uint8_t id, uint32_t format)
292 uint8_t i;
294 i = priv->psm_info.cnt;
295 priv->psm_info.streams[i].id = id;
296 priv->psm_info.streams[i].format = format;
298 if(is_mpeg1(format))
299 priv->psm_info.streams[i].type = 0x01;
300 else if(is_mpeg2(format))
301 priv->psm_info.streams[i].type = 0x02;
302 else if(is_mpeg4(format))
303 priv->psm_info.streams[i].type = 0x10;
304 else if(format == AUDIO_MP2 || format == AUDIO_MP3)
305 priv->psm_info.streams[i].type = 0x03;
306 else if(format == AUDIO_AAC1 || format == AUDIO_AAC2)
307 priv->psm_info.streams[i].type = 0x0f;
308 else
309 priv->psm_info.streams[i].type = 0x81;
311 if(format == AUDIO_A52)
312 memcpy((char*) &(priv->psm_info.streams[i].format), "AC-3", 4);
314 priv->psm_info.cnt++;
318 static mpeg_frame_t *init_frames(uint16_t num, size_t size)
320 mpeg_frame_t *tmp;
321 uint16_t i;
323 tmp = (mpeg_frame_t *) calloc(num, sizeof(mpeg_frame_t));
324 if(tmp == NULL)
325 return NULL;
327 for(i=0; i < num; i++)
329 tmp[i].buffer = (uint8_t *) calloc(1, size);
330 if(tmp[i].buffer == NULL)
331 return NULL;
332 tmp[i].size = 0;
333 tmp[i].pos = 0;
334 tmp[i].alloc_size = size;
335 tmp[i].pts = 0;
338 return tmp;
341 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);
343 static muxer_stream_t* mpegfile_new_stream(muxer_t *muxer,int type){
344 muxer_priv_t *priv = (muxer_priv_t*) muxer->priv;
345 muxer_stream_t *s;
346 muxer_headers_t *spriv;
348 if (!muxer) return NULL;
349 if(muxer->avih.dwStreams>=MUXER_MAX_STREAMS){
350 mp_msg(MSGT_MUXER, MSGL_ERR, "Too many streams! increase MUXER_MAX_STREAMS !\n");
351 return NULL;
353 switch (type) {
354 case MUXER_TYPE_VIDEO:
355 if (muxer->num_videos >= 16) {
356 mp_msg(MSGT_MUXER, MSGL_ERR, "MPEG files can't contain more than 16 video streams!\n");
357 return NULL;
359 break;
360 case MUXER_TYPE_AUDIO:
361 if (muxer->num_audios >= 16) {
362 mp_msg(MSGT_MUXER, MSGL_ERR, "MPEG files can't contain more than 16 audio streams!\n");
363 return NULL;
365 break;
366 default:
367 mp_msg(MSGT_MUXER, MSGL_ERR, "Unknown stream type!\n");
368 return NULL;
370 s = (muxer_stream_t*) calloc(1, sizeof(muxer_stream_t));
371 if(!s) return NULL; // no mem!?
372 if (!(s->b_buffer = malloc(priv->packet_size)))
373 goto init_fail;
374 s->b_buffer_size = priv->packet_size;
375 s->b_buffer_ptr = 0;
376 s->b_buffer_len = 0;
377 s->priv = (muxer_headers_t*) calloc(1, sizeof(muxer_headers_t));
378 if(s->priv == NULL)
379 goto init_fail;
380 spriv = (muxer_headers_t *) s->priv;
381 spriv->pack = malloc(priv->packet_size);
382 if(! spriv->pack)
383 goto init_fail;
384 spriv->buffer_track = calloc(1, 4096*sizeof(buffer_track_t));
385 if(!spriv->buffer_track)
386 goto init_fail;
387 spriv->track_pos = 0;
388 spriv->track_len = 4096;
389 muxer->streams[muxer->avih.dwStreams]=s;
390 s->type=type;
391 s->id=muxer->avih.dwStreams;
392 s->muxer=muxer;
394 if (type == MUXER_TYPE_VIDEO) {
395 spriv->type = 1;
396 spriv->last_pts = conf_init_vpts * 90 * 300;
397 if(conf_init_vdelay) {
398 spriv->last_dts += conf_init_vdelay * 90 * 300;
399 spriv->last_pts += conf_init_vdelay * 90 * 300;
401 spriv->id = 0xe0 + muxer->num_videos;
402 s->ckid = be2me_32 (0x100 + spriv->id);
403 if(priv->is_genmpeg1 || priv->is_genmpeg2) {
404 int v = (conf_vbuf_size ? conf_vbuf_size*1024 :
405 (s->h.dwSuggestedBufferSize ? s->h.dwSuggestedBufferSize : 46*1024));
406 int n = priv->sys_info.cnt;
408 priv->sys_info.streams[n].id = spriv->id;
409 priv->sys_info.streams[n].type = 1;
410 priv->sys_info.streams[n].bufsize = v;
411 priv->sys_info.cnt++;
413 muxer->num_videos++;
414 priv->has_video++;
415 s->h.fccType=streamtypeVIDEO;
416 if(!muxer->def_v) muxer->def_v=s;
417 spriv->framebuf_cnt = 30;
418 spriv->framebuf_used = 0;
419 spriv->framebuf = init_frames(spriv->framebuf_cnt, (size_t) 5000);
420 if(spriv->framebuf == NULL) {
421 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't allocate initial frames structure, abort!\n");
422 goto init_fail;
424 memset(&(spriv->picture), 0, sizeof(spriv->picture));
425 if(priv->is_xvcd)
426 spriv->min_pes_hlen = 18;
427 else if(priv->is_xsvcd)
428 spriv->min_pes_hlen = 22;
429 spriv->telecine = conf_telecine;
430 mp_msg (MSGT_MUXER, MSGL_DBG2, "Added video stream %d, ckid=%X\n", muxer->num_videos, s->ckid);
431 } else { // MUXER_TYPE_AUDIO
432 spriv->type = 0;
433 spriv->drop_delayed_frames = conf_drop;
434 spriv->last_pts = conf_init_apts * 90 * 300;
435 if(conf_init_adelay && ! spriv->drop_delayed_frames)
436 spriv->last_pts += conf_init_adelay * 90 * 300;
437 spriv->pts = spriv->last_pts;
438 spriv->id = 0xc0 + muxer->num_audios;
439 s->ckid = be2me_32 (0x100 + spriv->id);
440 if(priv->is_genmpeg1 || priv->is_genmpeg2) {
441 int a1 = (conf_abuf_size ? conf_abuf_size*1024 :
442 (s->h.dwSuggestedBufferSize ? s->h.dwSuggestedBufferSize : 4*1024));
443 int n = priv->sys_info.cnt;
445 priv->sys_info.streams[n].id = spriv->id;
446 priv->sys_info.streams[n].type = 0;
447 priv->sys_info.streams[n].bufsize = a1;
448 priv->sys_info.cnt++;
450 if(priv->is_xvcd)
451 spriv->min_pes_hlen = 13;
452 else if(priv->is_xsvcd)
453 spriv->min_pes_hlen = 17;
455 muxer->num_audios++;
456 priv->has_audio++;
457 s->h.fccType=streamtypeAUDIO;
459 spriv->framebuf_cnt = 30;
460 spriv->framebuf_used = 0;
461 spriv->framebuf = init_frames(spriv->framebuf_cnt, (size_t) 2048);
462 if(spriv->framebuf == NULL) {
463 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't allocate initial frames structure, abort!\n");
464 goto init_fail;
467 mp_msg (MSGT_MUXER, MSGL_DBG2, "Added audio stream %d, ckid=%X\n", s->id - muxer->num_videos + 1, s->ckid);
469 muxer->avih.dwStreams++;
470 return s;
472 init_fail:
473 if(s)
475 if(s->priv)
477 spriv = s->priv;
478 if(spriv->pack)
479 free(spriv->pack);
480 if(spriv->buffer_track)
481 free(spriv->buffer_track);
482 free(s->priv);
484 if(s->b_buffer)
485 free(s->b_buffer);
486 free(s);
488 return NULL;
491 static void write_mpeg_ts(unsigned char *b, uint64_t ts, uint8_t mod) {
492 ts /= 300;
493 b[0] = mod | ((ts >> 29) & 0xf) | 1;
494 b[1] = (ts >> 22) & 0xff;
495 b[2] = ((ts >> 14) & 0xff) | 1;
496 b[3] = (ts >> 7) & 0xff;
497 b[4] = ((ts << 1) & 0xff) | 1;
501 static void write_mpeg_rate(int type, unsigned char *b, unsigned int rate)
503 rate = ((rate*8)+399) / 400;
505 if(type == MUX_MPEG1)
507 b[0] = ((rate >> 15) & 0x7f) | 0x80;
508 b[1] = (rate >> 7) & 0xff;
509 b[2] = ((rate << 1) & 0xff) | 1;
511 else
513 b[0] = (rate >> 14);
514 b[1] = (rate >> 6) & 0xff;
515 b[2] = ((rate & 0x3f) << 2) | 0x03;
520 static void write_mpeg_std(unsigned char *b, unsigned int size, unsigned int type, uint8_t mod)
522 //type = 0:mpeg audio/128, 1:video and pes private streams (including ac3/dts/lpcm)/1024
523 if(type == 0) //audio
524 size = (size + 127) / 128;
525 else //video or other
526 size = ((size + 1023) / 1024);
528 if(! size)
529 size++;
531 b[0] = ((size >> 8) & 0x3f) | (type==1 ? 0x60 : 0x40) | mod;
532 b[1] = size & 0xff;
535 static void write_mpeg2_scr(unsigned char *b, uint64_t ts)
537 uint16_t t1, t2, t3, scr_ext;
538 scr_ext = ts % 300ULL;
539 ts /= 300ULL;
540 ts &= 0x1FFFFFFFFULL; //33 bits
541 t1 = (ts >> 30) & 0x7;;
542 t2 = (ts >> 15) & 0x7fff;
543 t3 = ts & 0x7fff;
545 b[0] = (t1 << 3 ) | 0x44 | ((t2 >> 13) & 0x3);
546 b[1] = (t2 >> 5);
547 b[2] = (t2 & 0x1f) << 3 | 0x4 | ((t3 >> 13) & 0x3);
548 b[3] = (t3 >> 5);
549 b[4] = (t3 & 0x1f) << 3 | ((scr_ext >> 7) & 0x03) | 0x4;
550 b[5] = ((scr_ext << 1) & 0xFF) | 1;
554 static int write_mpeg_pack_header(muxer_t *muxer, char *buff)
556 int len;
557 muxer_priv_t *priv;
559 priv = (muxer_priv_t *) muxer->priv;
560 *(uint32_t *)buff = be2me_32(PACK_HEADER_START_CODE);
561 if(priv->mux==MUX_MPEG1)
563 write_mpeg_ts(&buff[4], priv->scr, 0x20); // 0010 and SCR
564 write_mpeg_rate(priv->mux, &buff[9], muxer->sysrate);
565 len = 12;
567 else
569 write_mpeg2_scr(&buff[4], priv->scr); // 0010 and SCR
570 write_mpeg_rate(priv->mux, &buff[10], muxer->sysrate);
571 buff[13] = 0xf8; //5 bits reserved + 3 set to 0 to indicate 0 stuffing bytes
572 len = 14;
575 return len;
579 static int write_mpeg_system_header(muxer_t *muxer, char *buff)
581 int len;
582 uint8_t i;
583 muxer_priv_t *priv;
584 priv = (muxer_priv_t *) muxer->priv;
586 len = 0;
587 *(uint32_t *)(&buff[len]) = be2me_32(SYSTEM_HEADER_START_CODE);
588 len += 4;
589 *(uint16_t *)(&buff[len]) = 0; //fake length, we'll fix it later
590 len += 2;
591 write_mpeg_rate(MUX_MPEG1, &buff[len], muxer->sysrate);
592 len += 3;
594 buff[len++] = 0x4 | (priv->is_xvcd ? 1 : 0); //1 audio stream bound, no fixed, CSPS only for xvcd
595 //stolen from libavformat
596 if(priv->is_xvcd || priv->is_dvd)
597 buff[len++] = 0xe1; //system_audio_lock, system_video_lock, marker, 1 video stream bound
598 else
599 buff[len++] = 0x21; //marker, 1 video stream bound
601 buff[len++] = ((priv->mux == MUX_MPEG1) ? 0xff : 0x7f); //in mpeg2 there's the packet rate restriction
603 for(i = 0; i < priv->sys_info.cnt; i++)
605 buff[len++] = priv->sys_info.streams[i].id;
606 write_mpeg_std(&buff[len], priv->sys_info.streams[i].bufsize, priv->sys_info.streams[i].type,
607 (priv->sys_info.streams[i].type == 1 ? 0xe0: 0xc0));
608 len += 2;
611 *(uint16_t *)(&buff[4]) = be2me_16(len - 6); // length field fixed
613 return len;
616 static int write_mpeg_psm(muxer_t *muxer, char *buff)
618 int len;
619 uint8_t i;
620 uint16_t dlen;
621 muxer_priv_t *priv;
622 priv = (muxer_priv_t *) muxer->priv;
624 len = 0;
625 *(uint32_t *)(&buff[len]) = be2me_32(PSM_START_CODE);
626 len += 4;
627 *(uint16_t *)(&buff[len]) = 0; //fake length, we'll fix it later
628 len += 2;
629 buff[len++] = 0xe0; //1 current, 2 bits reserved, 5 version 0
630 buff[len++] = 0xff; //7 reserved, 1 marker
631 buff[len] = buff[len+1] = 0; //length of the program descriptors (unused)
632 len += 2;
633 *(uint16_t *)(&buff[len]) = 0; //length of the es descriptors
634 len += 2;
636 dlen = 0;
637 for(i = 0; i < priv->psm_info.cnt; i++)
640 (priv->psm_info.streams[i].id == 0xbd) ||
641 (priv->psm_info.streams[i].id >= 0xe0 && priv->psm_info.streams[i].id <= 0xef) ||
642 (priv->psm_info.streams[i].id >= 0xc0 && priv->psm_info.streams[i].id <= 0xcf)
645 buff[len++] = priv->psm_info.streams[i].type;
646 buff[len++] = priv->psm_info.streams[i].id;
647 buff[len++] = 0; //len of descriptor upper ...
648 buff[len++] = 0; //... lower
650 dlen += 4;
653 *(uint16_t *)(&buff[10]) = be2me_16(dlen); //length of the es descriptors
655 *(uint16_t *)(&buff[4]) = be2me_16(len - 6 + 4); // length field fixed, including size of CRC32
657 *(uint32_t *)(&buff[len]) = be2me_32(CalcCRC32(buff, len));
659 len += 4; //for crc
661 return len;
664 static int psm_is_late(muxer_priv_t *priv)
666 return !priv->data_size || (priv->scr >= priv->last_psm_scr + 27000000ULL);
669 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)
671 int len;
673 len = 0;
674 memcpy(&buff[len], pes_id, 4);
675 len += 4;
677 buff[len] = buff[len+1] = 0; //fake len
678 len += 2;
680 if(mux_type == MUX_MPEG1)
682 if(stuffing_len > 0)
684 memset(&buff[len], 0xff, stuffing_len);
685 len += stuffing_len;
688 if(h->buffer_size > 0)
690 write_mpeg_std(&buff[len], h->buffer_size, h->type, 0x40); // 01 is pes1 format
691 len += 2;
694 else //MPEG2
696 buff[len] = (h->pes_is_aligned ? 0x84 : 0x80); //0x10...
697 len++;
698 buff[len] = ((h->buffer_size > 0) ? 1 : 0) | (h->pts ? (h->dts ? 0xC0 : 0x80) : 0); //pes extension + pts/dts flags
699 len++;
700 buff[len] = (h->pts ? (h->dts ? 10 : 5) : 0) + ((h->buffer_size > 0) ? 3 : 0) + stuffing_len;//pts + std + stuffing
701 len++;
705 if(h->pts)
707 write_mpeg_ts(&buff[len], h->pts, (h->dts ? 0x30 : 0x20)); // 001x and both PTS/DTS
708 len += 5;
710 if(h->dts)
712 write_mpeg_ts(&buff[len], h->dts, 0x10); // 0001 before DTS
713 len += 5;
716 else
718 if(mux_type == MUX_MPEG1)
720 buff[len] = 0x0f;
721 len += 1;
726 if(mux_type == MUX_MPEG2)
728 if(h->buffer_size > 0)
730 buff[len] = 0x1e; //std flag
731 len++;
733 write_mpeg_std(&buff[len], h->buffer_size, h->type, 0x40);
734 len += 2;
737 if(stuffing_len > 0)
739 memset(&buff[len], 0xff, stuffing_len);
740 len += stuffing_len;
744 *((uint16_t*) &buff[4]) = be2me_16(len + plen - 6); //fix pes packet size
745 return len;
749 static void write_pes_padding(uint8_t *buff, uint16_t len)
751 //6 header bytes + len-6 0xff chars
752 buff[0] = buff[1] = 0;
753 buff[2] = 1;
754 buff[3] = 0xbe;
755 *((uint16_t*) &buff[4]) = be2me_16(len - 6);
756 memset(&buff[6], 0xff, len - 6);
760 static int write_nav_pack(uint8_t *buff)
762 // concatenation of pes_private2 + 03d4 x 0 and pes_private2 + 03fa x 0
763 int len;
765 mp_msg(MSGT_MUXER, MSGL_DBG3, "NAV\n");
766 len = 0;
767 *(uint32_t *)(&buff[len]) = be2me_32(PES_PRIVATE2);
768 len += 4;
769 buff[len++] = 0x3;
770 buff[len++] = 0xd4;
771 memset(&buff[len], 0, 0x03d4);
772 len += 0x03d4;
774 *(uint32_t *)(&buff[len]) = be2me_32(PES_PRIVATE2);
775 len += 4;
776 buff[len++] = 0x3;
777 buff[len++] = 0xfa;
778 memset(&buff[len], 0, 0x03fa);
779 len += 0x03fa;
781 return len;
784 static unsigned int calc_psm_len(muxer_priv_t *priv)
786 return 16 + 4*(priv->psm_info.cnt);
789 static uint32_t calc_pes_hlen(int format, muxer_headers_t *h, muxer_priv_t *priv)
791 uint32_t len;
793 if(format == MUX_MPEG1)
794 len = 6;
795 else
796 len = 9;
798 if(h->pts)
800 len += 5;
801 if(h->dts)
802 len += 5;
804 else if(format == MUX_MPEG1)
805 len += 1;
807 if(h->buffer_size > 0)
809 if(format == MUX_MPEG2)
810 len += 3;
811 else
812 len += 2;
815 //len = max(h->min_pes_hlen, len);
817 return len;
821 static int write_mpeg_pack(muxer_t *muxer, muxer_stream_t *s, stream_t *stream, int isoend)
823 size_t tot, offset;
824 muxer_priv_t *priv;
825 unsigned char *buff;
826 int stuffing_len;
828 priv = (muxer_priv_t *) muxer->priv;
829 buff = priv->buff;
831 if(isoend)
833 offset = priv->packet_size - 4;
834 write_pes_padding(buff, offset);
835 buff[offset + 0] = buff[offset + 1] = 0;
836 buff[offset + 2] = 1;
837 buff[offset + 3] = 0xb9;
839 stream_write_buffer(stream, buff, priv->packet_size);
840 return 1;
842 else //FAKE DVD NAV PACK
844 offset = write_mpeg_pack_header(muxer, buff);
845 offset += write_mpeg_system_header(muxer, &buff[offset]);
847 //priv->update_system_header = 0;
849 if(priv->is_dvd)
850 offset += write_nav_pack(&buff[offset]);
852 stuffing_len = priv->packet_size - offset;
853 if(stuffing_len > 0)
855 //insert a PES padding packet
856 write_pes_padding(&buff[offset], stuffing_len);
857 offset += stuffing_len;
860 stream_write_buffer(stream, buff, offset);
861 priv->headers_size += offset;
862 tot = offset;
863 muxer->movi_end += tot;
865 return tot;
869 static void update_demux_bufsize(muxer_headers_t *spriv, uint64_t dts, int framelen, int type)
871 int dim = (spriv->track_len+16)*sizeof(buffer_track_t);
873 if(spriv->track_pos+1 >= spriv->track_len)
875 buffer_track_t *tmp = realloc(spriv->buffer_track, dim);
876 if(!tmp)
878 mp_msg(MSGT_MUXER, MSGL_ERR, "\r\nERROR, couldn't realloc %d bytes for tracking buffer\r\n", dim);
879 return;
881 spriv->buffer_track = tmp;
882 memset(&(spriv->buffer_track[spriv->track_pos+1]), 0, 16*sizeof(buffer_track_t));
883 spriv->track_len += 16;
886 spriv->buffer_track[spriv->track_pos].size = framelen;
887 spriv->buffer_track[spriv->track_pos].dts = dts; //must be dts
889 spriv->track_pos++;
892 static void fix_a52_headers(muxer_stream_t *s)
894 muxer_headers_t *spriv = s->priv;
895 int x = spriv->payload_offset;
897 spriv->pack[x+0] = 0x80;
898 spriv->pack[x+1] = spriv->frames;
899 if(spriv->frames)
901 spriv->pack[x+2] = ((spriv->last_frame_rest+1) >> 8) & 0xff; //256 * 0 ...
902 spriv->pack[x+3] = (spriv->last_frame_rest+1) & 0xff; // + 1 byte(s) to skip
904 else
905 spriv->pack[x+2] = spriv->pack[x+3] = 0;
908 static inline void remove_frames(muxer_headers_t *spriv, int n)
910 mpeg_frame_t tmp;
911 int i;
913 for(i = n; i < spriv->framebuf_used; i++)
915 tmp = spriv->framebuf[i - n];
916 spriv->framebuf[i - n] = spriv->framebuf[i];
917 spriv->framebuf[i] = tmp;
919 spriv->framebuf_used -= n;
922 static int calc_packet_len(muxer_stream_t *s, int psize, int finalize)
924 muxer_headers_t *spriv = s->priv;
925 int n, len, frpos, m;
927 n = len = 0;
928 frpos = spriv->framebuf[0].pos;
929 while(len < psize && n < spriv->framebuf_used)
931 if(!frpos && len>0 && s->type == MUXER_TYPE_VIDEO && spriv->framebuf[n].type==I_FRAME)
932 return len;
933 m = FFMIN(spriv->framebuf[n].size - frpos, psize - len);
934 len += m;
935 frpos += m;
936 if(frpos == spriv->framebuf[n].size)
938 frpos = 0;
939 n++;
943 if(len < psize && !finalize)
944 return 0;
945 return len;
948 static int find_packet_timestamps(muxer_priv_t *priv, muxer_stream_t *s, unsigned int start, uint64_t *dts, uint64_t *pts)
950 muxer_headers_t *spriv = s->priv;
951 int i, m, pes_hlen, ret, threshold;
952 uint64_t spts, sdts, dpts;
954 if(!spriv->framebuf_used)
955 return 0;
957 spts = spriv->pts;
958 sdts = spriv->dts;
959 spriv->dts = spriv->pts = 0;
960 ret = 0;
961 if(spriv->framebuf[0].pos == 0) // start of frame
962 i = 0;
963 else
965 pes_hlen = calc_pes_hlen(priv->mux, spriv, priv);
967 if(pes_hlen < spriv->min_pes_hlen)
968 pes_hlen = spriv->min_pes_hlen;
970 m = spriv->framebuf[0].size - spriv->framebuf[0].pos;
972 if(start + pes_hlen + m >= priv->packet_size) //spriv->pack_offset
973 i = -1; //this pack won't have a pts: no space available
974 else
976 if(spriv->framebuf_used < 2)
977 goto fail;
979 if(spriv->framebuf[1].pts == spriv->framebuf[1].dts)
980 threshold = 5;
981 else
982 threshold = 10;
984 //headers+frame 0 < space available including timestamps
985 if(start + pes_hlen + m < priv->packet_size - threshold)
986 i = 1;
987 else
988 i = -1;
992 if(i > -1)
994 dpts = FFMAX(spriv->last_saved_pts, spriv->framebuf[i].pts) -
995 FFMIN(spriv->last_saved_pts, spriv->framebuf[i].pts) +
996 spriv->framebuf[0].idur;
998 if(s->type != MUXER_TYPE_VIDEO)
999 ret = 1;
1000 else if((spriv->framebuf[i].type == I_FRAME || priv->ts_allframes || dpts >= 36000*300)) //0.4 seconds
1001 ret = 1;
1003 if(ret)
1005 *pts = spriv->framebuf[i].pts;
1006 *dts = spriv->framebuf[i].dts;
1007 if(*dts == *pts)
1008 *dts = 0;
1012 fail:
1013 spriv->pts = spts;
1014 spriv->dts = sdts;
1015 return ret;
1018 static int get_packet_stats(muxer_priv_t *priv, muxer_stream_t *s, pack_stats_t *p, int finalize)
1020 muxer_headers_t *spriv = s->priv;
1021 int len, len2, pack_hlen, pes_hlen, hlen, target, stflen, stuffing_len;
1022 uint64_t pts, dts;
1024 spriv->pts = spriv->dts = 0;
1025 p->dts = p->pts = p->frame_pts = p->frame_dts = 0;
1026 p->len = 0;
1028 if(priv->rawpes)
1029 pack_hlen = 0;
1030 else if(priv->mux == MUX_MPEG1)
1031 pack_hlen = 12;
1032 else
1033 pack_hlen = 14;
1034 if(priv->use_psm && psm_is_late(priv))
1035 pack_hlen += calc_psm_len(priv);
1037 if(find_packet_timestamps(priv, s, pack_hlen, &dts, &pts))
1039 p->pts = p->frame_pts = pts;
1040 p->dts = p->frame_dts = dts;
1042 spriv->pts = pts;
1043 spriv->dts = dts;
1045 pes_hlen = calc_pes_hlen(priv->mux, spriv, priv);
1047 p->stflen = stflen = (spriv->min_pes_hlen > pes_hlen ? spriv->min_pes_hlen - pes_hlen : 0);
1049 target = len = priv->packet_size - pack_hlen - pes_hlen - stflen; //max space available
1050 if(s->type == MUXER_TYPE_AUDIO && s->wf->wFormatTag == AUDIO_A52)
1051 hlen = 4;
1052 else
1053 hlen = 0;
1055 len -= hlen;
1056 target -= hlen;
1058 len2 = calc_packet_len(s, target, finalize);
1059 if(!len2 || (len2 < target && s->type == MUXER_TYPE_AUDIO && !finalize))
1061 //p->len = 0;
1062 //p->dts = p->pts = 0;
1063 spriv->pts = spriv->dts = 0;
1064 //fprintf(stderr, "\r\nLEN2: %d, target: %d, type: %d\r\n", len2, target, s->type);
1065 return 0;
1068 len = len2;
1069 stuffing_len = 0;
1070 if(len < target)
1072 if(s->type == MUXER_TYPE_VIDEO)
1074 if(spriv->pts)
1075 target += 5;
1076 if(spriv->dts)
1077 target += 5;
1078 spriv->pts = spriv->dts = 0;
1079 p->pts = p->dts = 0;
1082 stuffing_len = target - len;
1083 if(stuffing_len > 0 && stuffing_len < 7)
1085 if(stflen + stuffing_len > 16)
1087 int x = 7 - stuffing_len;
1088 stflen -= x;
1089 stuffing_len += x;
1091 else
1093 stflen += stuffing_len;
1094 stuffing_len = 0;
1099 len += hlen;
1101 p->len = len;
1102 p->stflen = stflen;
1104 return p->len;
1107 static int fill_packet(muxer_t *muxer, muxer_stream_t *s, int finalize)
1109 //try to fill a packet as much as possible
1110 //spriv->pack_offset is the start position initialized to 0
1111 //data is taken from spriv->framebuf
1112 //if audio and a52 insert the headers
1113 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
1114 muxer_headers_t *spriv = (muxer_headers_t *) s->priv;
1115 int len, m, n, dvd_pack = 0;
1116 int write_psm = 0;
1117 mpeg_frame_t *frm;
1118 pack_stats_t p;
1120 spriv->dts = spriv->pts = 0;
1122 if(! spriv->framebuf_used)
1124 spriv->pack_offset = 0;
1125 return 0;
1128 if(!spriv->pack_offset)
1130 if(priv->rawpes)
1131 spriv->pack_offset = 0;
1132 else
1133 spriv->pack_offset = write_mpeg_pack_header(muxer, spriv->pack);
1134 if(priv->update_system_header && (priv->is_genmpeg1 || priv->is_genmpeg2))
1136 spriv->pack_offset += write_mpeg_system_header(muxer, &spriv->pack[spriv->pack_offset]);
1137 priv->update_system_header = 0;
1140 if(priv->use_psm && psm_is_late(priv))
1142 spriv->pack_offset += write_mpeg_psm(muxer, &spriv->pack[spriv->pack_offset]);
1143 write_psm = 1;
1146 spriv->pes_set = 0;
1147 spriv->pes_offset = spriv->pack_offset;
1148 spriv->payload_offset = 0;
1149 spriv->frames = 0;
1150 spriv->last_frame_rest = 0;
1153 if(!spriv->pes_set)
1155 int bufsize = 0;
1156 //search the pts. yes if either it's video && (I-frame or priv->ts_allframes) && framebuf[i].pos == 0
1157 //or it's audio && framebuf[i].pos == 0
1158 //NB pts and dts can only be relative to the first frame beginning in this pack
1159 if((priv->is_xsvcd || priv->is_xvcd || priv->rawpes) && spriv->size == 0)
1161 if(s->type == MUXER_TYPE_VIDEO)
1162 bufsize = (conf_vbuf_size ? conf_vbuf_size : (priv->is_xvcd ? 46 : 232));
1163 else
1164 bufsize = (conf_abuf_size ? conf_abuf_size : 4);
1165 spriv->buffer_size = bufsize*1024;
1168 if(priv->is_dvd && s->type == MUXER_TYPE_VIDEO
1169 && spriv->framebuf[0].type==I_FRAME && spriv->framebuf[0].pos==0)
1170 dvd_pack = 1;
1172 if(! get_packet_stats(priv, s, &p, finalize))
1174 spriv->pack_offset = 0;
1175 return 0;
1177 spriv->dts = p.dts;
1178 spriv->pts = p.pts;
1179 if(spriv->pts)
1180 spriv->last_saved_pts = p.pts;
1182 spriv->pack_offset += write_mpeg_pes_header(spriv, (uint8_t *) &s->ckid, &(spriv->pack[spriv->pack_offset]),
1183 p.len, p.stflen, priv->mux);
1185 if(s->type == MUXER_TYPE_AUDIO && s->wf->wFormatTag == AUDIO_A52)
1187 spriv->payload_offset = spriv->pack_offset;
1188 spriv->pack_offset += 4; //for the 4 bytes of header
1189 if(!spriv->framebuf[0].pos)
1190 spriv->last_frame_rest = 0;
1191 else
1192 spriv->last_frame_rest = spriv->framebuf[0].size - spriv->framebuf[0].pos;
1195 spriv->pes_set = 1;
1199 if(spriv->dts || spriv->pts)
1201 if((spriv->dts && priv->scr >= spriv->dts) || priv->scr >= spriv->pts)
1202 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);
1203 else if(priv->scr + 63000*300 < spriv->dts)
1204 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);
1207 n = 0;
1208 len = 0;
1210 frm = spriv->framebuf;
1211 while(spriv->pack_offset < priv->packet_size && n < spriv->framebuf_used)
1213 if(!frm->pos)
1215 //since iframes must always be aligned at block boundaries exit when we find the
1216 //beginning of one in the middle of the flush
1217 if(len > 0 && s->type == MUXER_TYPE_VIDEO && frm->type == I_FRAME)
1219 break;
1221 spriv->frames++;
1222 update_demux_bufsize(spriv, frm->dts, frm->size, s->type);
1225 m = FFMIN(frm->size - frm->pos, priv->packet_size - spriv->pack_offset);
1226 memcpy(&(spriv->pack[spriv->pack_offset]), &(frm->buffer[frm->pos]), m);
1228 len += m;
1229 spriv->pack_offset += m;
1230 frm->pos += m;
1232 if(frm->pos == frm->size) //end of frame
1234 frm->pos = frm->size = 0;
1235 frm->pts = frm->dts = 0;
1236 n++;
1237 frm++;
1241 if((priv->is_xsvcd || priv->is_xvcd || priv->rawpes) && spriv->size == 0)
1242 spriv->buffer_size = 0;
1244 spriv->size += len;
1246 if(dvd_pack && (spriv->pack_offset == priv->packet_size))
1247 write_mpeg_pack(muxer, NULL, muxer->stream, 0); //insert fake Nav Packet
1249 if(n > 0)
1250 remove_frames(spriv, n);
1252 spriv->track_bufsize += len;
1253 if(spriv->track_bufsize > spriv->max_buffer_size)
1254 mp_msg(MSGT_MUXER, MSGL_ERR, "\r\nBUFFER OVERFLOW: %d > %d, pts: %"PRIu64"\r\n", spriv->track_bufsize, spriv->max_buffer_size, spriv->pts);
1256 if(s->type == MUXER_TYPE_AUDIO && s->wf->wFormatTag == AUDIO_A52)
1257 fix_a52_headers(s);
1259 if(spriv->pack_offset < priv->packet_size && !priv->rawpes) //here finalize is set
1261 int diff = priv->packet_size - spriv->pack_offset;
1262 write_pes_padding(&(spriv->pack[spriv->pack_offset]), diff);
1263 spriv->pack_offset += diff;
1266 stream_write_buffer(muxer->stream, spriv->pack, spriv->pack_offset);
1268 priv->headers_size += spriv->pack_offset - len;
1269 priv->data_size += len;
1270 muxer->movi_end += spriv->pack_offset;
1272 spriv->pack_offset = 0;
1273 spriv->pes_set = 0;
1274 spriv->frames = 0;
1275 if(write_psm)
1276 priv->last_psm_scr = priv->scr;
1278 return len;
1281 static inline int find_best_stream(muxer_t *muxer)
1283 int i, ndts;
1284 uint64_t dts = -1;
1285 muxer_priv_t *priv = muxer->priv;
1286 muxer_headers_t *spriv;
1287 pack_stats_t p;
1288 unsigned int perc, sperc;
1290 ndts = -1;
1291 perc = -1;
1293 //THIS RULE MUST ALWAYS apply: dts <= SCR + 0.7 seconds
1294 for(i = 0; i < muxer->avih.dwStreams; i++)
1296 spriv = muxer->streams[i]->priv;
1298 p.len = 0;
1299 get_packet_stats(priv, muxer->streams[i], &p, 0);
1301 if(spriv->track_bufsize + p.len > spriv->max_buffer_size)
1302 continue;
1303 if(p.frame_pts && p.frame_dts > priv->scr + 63000*300)
1304 continue;
1306 if(spriv->framebuf[0].dts <= dts)
1308 dts = spriv->framebuf[0].dts;
1309 ndts = i;
1312 if(conf_interleaving2)
1314 sperc = (spriv->track_bufsize * 1024) / spriv->max_buffer_size;
1315 if(sperc < perc)
1317 ndts = i;
1318 perc = sperc;
1323 return ndts;
1326 static void patch_seq(muxer_priv_t *priv, unsigned char *buf)
1328 if(priv->vwidth > 0)
1330 buf[4] = (priv->vwidth >> 4) & 0xff;
1331 buf[5] &= 0x0f;
1332 buf[5] |= (priv->vwidth & 0x0f) << 4;
1335 if(priv->vheight > 0)
1337 buf[5] &= 0xf0;
1338 buf[5] |= (priv->vheight >> 8) & 0x0f;
1339 buf[6] = priv->vheight & 0xff;
1342 if(priv->vaspect > 0)
1343 buf[7] = (buf[7] & 0x0f) | (priv->vaspect << 4);
1345 if(priv->vframerate > 0)
1346 buf[7] = (buf[7] & 0xf0) | priv->vframerate;
1348 if(priv->vbitrate > 0)
1350 buf[8] = (priv->vbitrate >> 10);
1351 buf[9] = (priv->vbitrate >> 2);
1352 buf[10] = (buf[10] & 0x3f) | (unsigned char) ((priv->vbitrate & 0x4) << 2);
1356 static void patch_panscan(muxer_priv_t *priv, unsigned char *buf)
1357 { //patches sequence display extension (display_horizontal_size and display_vertical_size)
1358 //1:
1359 int offset = 1;
1361 if(buf[0] & 0x01)
1362 offset += 3;
1364 if(priv->panscan_width > 0)
1366 buf[offset] = (priv->panscan_width >> 6);
1367 buf[offset+1] = ((priv->panscan_width & 0x3F) << 2) | (buf[offset + 1] & 0x03);
1370 offset++;
1372 if(priv->panscan_height > 0)
1374 buf[offset] = (priv->panscan_height >> 13) << 7;
1375 buf[offset+1] = (priv->panscan_height >> 5) & 0xFF;
1376 buf[offset+2] = ((priv->panscan_height & 0x1F) << 3) | (buf[offset+2] & 0x07);
1381 static void update_scr(muxer_t *muxer)
1383 muxer_priv_t *priv = muxer->priv;
1384 muxer_stream_t *stream;
1385 muxer_headers_t *spriv;
1386 int i, j;
1387 uint64_t mindts = (uint64_t) -1;
1389 priv->scr += priv->delta_scr;
1391 for(i = 0; i < muxer->avih.dwStreams; i++)
1393 stream = muxer->streams[i];
1394 spriv = stream->priv;
1395 if(spriv->framebuf_used && spriv->framebuf[0].dts < mindts)
1396 mindts = spriv->framebuf[0].dts;
1399 mp_msg(MSGT_MUXER, MSGL_DBG2, "UPDATE SCR TO %"PRIu64" (%.3lf)\n", priv->scr, (double) (priv->scr/27000000.0));
1401 for(i = 0; i < muxer->avih.dwStreams; i++)
1403 stream = muxer->streams[i];
1404 spriv = stream->priv;
1406 j = 0;
1407 while(j < spriv->track_pos && priv->scr >= spriv->buffer_track[j].dts)
1409 spriv->track_bufsize -= spriv->buffer_track[j].size;
1410 j++;
1412 if(spriv->track_bufsize < 0)
1414 double d;
1415 muxer->sysrate = (muxer->sysrate * 11) / 10; //raise by 10%
1416 d = (double) priv->packet_size / (double)muxer->sysrate;
1417 priv->delta_scr = (uint64_t) (d * 27000000.0f);
1418 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);
1419 spriv->track_bufsize = 0;
1422 if(j > 0)
1424 memmove(spriv->buffer_track, &(spriv->buffer_track[j]), (spriv->track_len - j) * sizeof(buffer_track_t));
1425 spriv->track_pos -= j;
1426 for(j = spriv->track_pos; j < spriv->track_len; j++)
1427 spriv->buffer_track[j].size = 0;
1430 if(spriv->framebuf_used && spriv->framebuf[0].dts < mindts)
1431 mindts = spriv->framebuf[0].dts;
1436 static int calc_frames_to_flush(muxer_headers_t *vpriv)
1438 int n, found = 0;
1440 if(vpriv->framebuf_used > 0)
1442 n = 0;
1443 //let's count how many frames we'll store in the next pack sequence
1444 mp_msg(MSGT_MUXER, MSGL_DBG2, "\n");
1445 while(n < vpriv->framebuf_used)
1447 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);
1448 if(n+1 < vpriv->framebuf_used)
1449 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);
1451 if(vpriv->framebuf[n].type == I_FRAME)
1453 if(n > 0)
1455 found = 1;
1456 break;
1460 n++;
1464 if(found && (n < vpriv->framebuf_used+1))
1465 return n;
1466 else
1467 return 0;
1470 static int flush_buffers(muxer_t *muxer, int finalize)
1472 int i, n, found;
1473 int skip_cnt;
1474 uint64_t init_delay = 0;
1475 muxer_stream_t *s, *vs, *as;
1476 muxer_headers_t *vpriv = NULL, *apriv = NULL;
1477 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
1478 double duration;
1479 uint64_t iduration, iaduration;
1482 analyzes all streams and decides what to flush
1483 trying to respect an interleaving distribution
1484 equal to the v_bitrate/a_bitrate proportion
1486 n = 0;
1487 vs = as = NULL;
1488 found = 0;
1489 for(i = 0; i < muxer->avih.dwStreams; i++)
1491 s = muxer->streams[i];
1492 if(s->type == MUXER_TYPE_VIDEO)
1494 vs = muxer->streams[i];
1495 vpriv = (muxer_headers_t*) vs->priv;
1496 if(!vpriv->is_ready)
1497 return 0;
1498 n = found = calc_frames_to_flush(vpriv);
1500 else if(s->type == MUXER_TYPE_AUDIO)
1501 as = s;
1504 if((! found) && finalize)
1506 if(vpriv != NULL)
1507 found = n = vpriv->framebuf_used;
1510 if(found)
1512 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nVIDEO, FLUSH %d frames (of %d), 0 to %d\n", n, vpriv->framebuf_used, n-1);
1514 vpriv = (muxer_headers_t*) vs->priv;
1516 duration = 0;
1517 iduration = 0;
1518 for(i = 0; i < n; i++)
1519 iduration += vpriv->framebuf[i].idur;
1520 duration = (double) (iduration / 27000000.0);
1522 if(as != NULL)
1524 apriv = (muxer_headers_t*) as->priv;
1525 iaduration = 0;
1526 for(i = 0; i < apriv->framebuf_used; i++)
1528 iaduration += apriv->framebuf[i].idur;
1530 if(iaduration < iduration)
1532 mp_msg(MSGT_MUXER, MSGL_DBG2, "Not enough audio data exit\n");
1533 return 0;
1537 if(as != NULL && (apriv->size == 0))
1539 init_delay = vpriv->framebuf[0].pts - vpriv->framebuf[0].dts;
1541 for(i = 0; i < apriv->framebuf_cnt; i++)
1543 apriv->framebuf[i].pts += init_delay;
1544 apriv->framebuf[i].dts += init_delay;
1546 apriv->last_pts += init_delay;
1547 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);
1550 if((priv->is_xvcd || priv->is_xsvcd) && (vpriv->size == 0))
1551 vpriv->buffer_size = (conf_vbuf_size ? conf_vbuf_size : (priv->is_xvcd ? 46 : 230))*1024;
1553 i = 0;
1554 skip_cnt = 0;
1556 while(1)
1558 update_scr(muxer);
1559 i = find_best_stream(muxer);
1560 if(i < 0)
1561 continue;
1562 if(!fill_packet(muxer, muxer->streams[i], finalize))
1563 skip_cnt++;
1565 if(skip_cnt == muxer->avih.dwStreams)
1567 found = 0;
1568 break;
1573 muxer->file_end = priv->scr;
1574 return found;
1578 static inline uint64_t parse_fps(float fps)
1580 // 90000 * 300 * 1001 / d , there's no rounding error with any of the admitted framerates
1581 int d = (int)(fps*1001+0.5);
1583 return 27027000000ULL / d;
1587 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)
1589 if(! pce_ptr)
1590 return 0;
1591 if(fps_ptr != NULL)
1593 *fps_ptr = (*fps_ptr & 0xf0) | priv->vframerate;
1594 vpriv->nom_delta_pts = parse_fps(conf_vframerate);
1597 //in pce_ptr starting from bit 0 bit 24 is tff, bit 30 is rff,
1598 if(pce_ptr[3] & 0x2)
1600 mp_msg(MSGT_MUXER, MSGL_ERR, "\nERROR! RFF bit is already set, disabling telecining\n");
1601 vpriv->telecine = 0;
1602 return 0;
1605 vpriv->picture.progressive_sequence = 0;
1606 vpriv->picture.progressive_frame = 1;
1607 if(se_ptr)
1608 se_ptr[1] &= 0xf7;
1610 //disable tff and rff and overwrite them with the value in bff_mask
1611 pce_ptr[3] = (pce_ptr[3] & 0x7d) | priv->bff_mask[vpriv->display_frame % MAX_PATTERN_LENGTH];
1612 pce_ptr[4] |= 0x80; //sets progressive frame
1614 vpriv->display_frame += n;
1615 if(! vpriv->vframes)
1616 mp_msg(MSGT_MUXER, MSGL_INFO, "\nENABLED SOFT TELECINING, FPS=%.3f\n",conf_vframerate);
1618 return 1;
1621 static size_t parse_mpeg12_video(muxer_stream_t *s, muxer_priv_t *priv, muxer_headers_t *spriv, float fps, size_t len)
1623 uint8_t *fps_ptr = NULL; //pointer to the fps byte in the sequence header
1624 uint8_t *se_ptr = NULL; //pointer to sequence extension
1625 uint8_t *pce_ptr = NULL; //pointer to picture coding extension
1626 int frames_diff, d1, gop_reset = 0; //how any frames we advanced respect to the last one
1627 int ret;
1628 int i, err;
1629 uint32_t temp_ref;
1630 int pt;
1632 mp_msg(MSGT_MUXER, MSGL_DBG2,"parse_mpeg12_video, len=%u\n", (uint32_t) len);
1633 if(s->buffer[0] != 0 || s->buffer[1] != 0 || s->buffer[2] != 1 || len<6)
1635 mp_msg(MSGT_MUXER, MSGL_ERR,"Unknown video format, possibly non-MPEG1/2 stream, len=%d!\n", len);
1636 return 0;
1639 temp_ref = 0;
1640 pt = 0;
1641 err = 0;
1642 i = 0;
1643 while(i + 4 < len)
1644 { // Video (0) Sequence header (b3) or GOP (b8)
1645 if((s->buffer[i] == 0) && (s->buffer[i+1] == 0) && (s->buffer[i+2] == 1))
1647 switch(s->buffer[i+3])
1649 case 0xb3: //sequence
1651 if(i + 11 > len)
1653 err=1;
1654 break;
1656 fps_ptr = &(s->buffer[i+7]);
1657 mp_header_process_sequence_header(&(spriv->picture), &(s->buffer[i+4]));
1658 spriv->delta_pts = spriv->nom_delta_pts = parse_fps(spriv->picture.fps);
1660 spriv->delta_clock = (double) 1/fps;
1661 //the 2 lines below are needed to handle non-standard frame rates (such as 18)
1662 if(! spriv->delta_pts)
1663 spriv->delta_pts = spriv->nom_delta_pts = (uint64_t) ((double)27000000.0 * spriv->delta_clock );
1664 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nFPS: %.3f, FRAMETIME: %.3lf\n", fps, (double)1/fps);
1665 if(priv->patch_seq)
1666 patch_seq(priv, &(s->buffer[i]));
1668 break;
1670 case 0xb5:
1671 if(i + 9 > len)
1673 err = 1;
1674 break;
1676 mp_header_process_extension(&(spriv->picture), &(s->buffer[i+4]));
1677 if(((s->buffer[i+4] & 0xf0) == 0x10))
1678 se_ptr = &(s->buffer[i+4]);
1679 if(((s->buffer[i+4] & 0xf0) == 0x20))
1681 if(priv->patch_sde)
1682 patch_panscan(priv, &(s->buffer[i+4]));
1684 if((s->buffer[i+4] & 0xf0) == 0x80)
1686 pce_ptr = &(s->buffer[i+4]);
1688 break;
1690 case 0xb8:
1691 gop_reset = 1;
1692 break;
1694 case 0x00:
1695 if(i + 5 > len)
1697 err = 1;
1698 break;
1700 pt = (s->buffer[i+5] & 0x1c) >> 3;
1701 temp_ref = (s->buffer[i+4]<<2)+(s->buffer[i+5]>>6);
1702 break;
1704 if(err) break; //something went wrong
1705 if(s->buffer[i+3] >= 0x01 && s->buffer[i+3] <= 0xAF) break; //slice, we have already analized what we need
1707 i++;
1709 if(err)
1710 mp_msg(MSGT_MUXER, MSGL_ERR,"Warning: picture too short or broken!\n");
1712 //following 2 lines are workaround: lavf doesn't sync to sequence headers before passing demux_packets
1713 if(!spriv->nom_delta_pts)
1714 spriv->delta_pts = spriv->nom_delta_pts = parse_fps(fps);
1715 if(!spriv->vframes)
1716 spriv->last_tr = spriv->max_tr = temp_ref;
1717 d1 = temp_ref - spriv->last_tr;
1718 if(gop_reset)
1719 frames_diff = spriv->max_tr + 1 + temp_ref - spriv->last_tr;
1720 else
1722 if(d1 < -6) //there's a wraparound
1723 frames_diff = spriv->max_tr + 1 + temp_ref - spriv->last_tr;
1724 else if(d1 > 6) //there's a wraparound
1725 frames_diff = spriv->max_tr + 1 + spriv->last_tr - temp_ref;
1726 else if(!d1) //pre-emptive fix against broken sequences
1727 frames_diff = 1;
1728 else
1729 frames_diff = d1;
1731 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nLAST: %d, TR: %d, GOP: %d, DIFF: %d, MAX: %d, d1: %d\n",
1732 spriv->last_tr, temp_ref, gop_reset, frames_diff, spriv->max_tr, d1);
1734 if(temp_ref > spriv->max_tr || gop_reset)
1735 spriv->max_tr = temp_ref;
1737 spriv->last_tr = temp_ref;
1738 if(spriv->picture.mpeg1 == 0)
1740 if(spriv->telecine && pce_ptr)
1742 soft_telecine(priv, spriv, fps_ptr, se_ptr, pce_ptr, frames_diff);
1743 spriv->picture.display_time = 100;
1744 mp_header_process_extension(&(spriv->picture), pce_ptr);
1745 if(spriv->picture.display_time >= 50 && spriv->picture.display_time <= 300)
1746 spriv->delta_pts = (spriv->nom_delta_pts * spriv->picture.display_time) / 100;
1750 if(! spriv->vframes)
1751 frames_diff = 1;
1753 spriv->last_dts += spriv->delta_pts;
1754 spriv->last_pts += spriv->nom_delta_pts*(frames_diff-1) + spriv->delta_pts;
1756 ret = add_frame(spriv, spriv->delta_pts, s->buffer, len, pt, spriv->last_dts, spriv->last_pts);
1757 if(ret < 0)
1759 mp_msg(MSGT_MUXER, MSGL_FATAL, "\r\nPARSE_MPEG12: add_frames(%d) failed, exit\r\n", len);
1760 return 0;
1762 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",
1763 ftypes[pt], temp_ref, frames_diff, ((double) spriv->last_dts/27000000.0f),
1764 ((double) spriv->last_pts/27000000.0f), spriv->picture.display_time, gop_reset);
1766 if(pt == B_FRAME)
1768 int j, n, adj = 0;
1769 int64_t diff = spriv->last_dts - spriv->last_pts;
1771 if(diff != 0)
1773 n = spriv->framebuf_used - 1;
1775 for(j = n; j >= 0; j--)
1777 if(spriv->framebuf[j].pts >= spriv->last_pts)
1779 spriv->framebuf[j].pts += diff;
1780 adj++;
1783 mp_msg(MSGT_MUXER, MSGL_V, "\r\nResynced B-frame by %d units, DIFF: %"PRId64" (%.3lf),[pd]ts=%.3lf\r\n",
1784 n, diff, (double) diff/27000000.0f, (double) spriv->last_pts/27000000.0f);
1785 spriv->last_pts = spriv->last_dts;
1788 spriv->vframes++;
1790 mp_msg(MSGT_MUXER, MSGL_DBG2,"parse_mpeg12_video, return %u\n", (uint32_t) len);
1791 return len;
1795 static uint64_t fix_mp4_frame_duration(muxer_headers_t *vpriv)
1797 uint64_t mn, md, mx, diff;
1798 uint32_t i;
1800 mn = mx = vpriv->framebuf[0].pts;
1801 for(i = 0; i < 3; i++)
1803 mp_msg(MSGT_DECVIDEO,MSGL_DBG2, "PTS: %"PRIu64"\n", vpriv->framebuf[i].pts);
1804 if(vpriv->framebuf[i].pts < mn)
1805 mn = vpriv->framebuf[i].pts;
1806 if(vpriv->framebuf[i].pts > mx)
1807 mx = vpriv->framebuf[i].pts;
1809 md = mn;
1810 for(i=0; i<3; i++)
1812 if((vpriv->framebuf[i].pts > mn) && (vpriv->framebuf[i].pts < mx))
1813 md = vpriv->framebuf[i].pts;
1816 if(mx - md > md - mn)
1817 diff = md - mn;
1818 else
1819 diff = mx - md;
1821 mp_msg(MSGT_DECVIDEO,MSGL_DBG2, "MIN: %"PRIu64", mid: %"PRIu64", max: %"PRIu64", diff: %"PRIu64"\n", mn, md, mx, diff);
1822 if(diff > 0)
1824 for(i=0; i<3; i++)
1826 vpriv->framebuf[i].pts += diff;
1827 vpriv->framebuf[i].dts += i * diff;
1828 mp_msg(MSGT_MUXER, MSGL_DBG2, "FIXED_PTS: %.3lf, FIXED_DTS: %.3lf\n",
1829 (double) (vpriv->framebuf[i].pts/27000000.0), (double) (vpriv->framebuf[i].dts/27000000.0));
1831 return diff;
1833 else
1834 return 0;
1838 static size_t parse_mpeg4_video(muxer_stream_t *s, muxer_priv_t *priv, muxer_headers_t *vpriv, float fps, size_t len)
1840 size_t ptr = 0;
1841 int64_t delta_pts=0;
1842 uint8_t pt;
1843 int ret;
1845 mp_msg(MSGT_MUXER, MSGL_DBG2,"parse_mpeg4_video, len=%u\n", (uint32_t) len);
1846 if(len<6)
1848 mp_msg(MSGT_MUXER, MSGL_ERR,"Frame too short: %d, exit!\n", len);
1849 return 0;
1852 pt = 0;
1853 while(ptr < len - 5)
1855 if(s->buffer[ptr] != 0 || s->buffer[ptr+1] != 0 || s->buffer[ptr+2] != 1)
1857 ptr++;
1858 continue;
1861 if(s->buffer[ptr+3] >= 0x20 && s->buffer[ptr+3] <= 0x2f) //VOL
1863 mp4_header_process_vol(&(vpriv->picture), &(s->buffer[ptr+4]));
1865 else if(s->buffer[ptr+3] == 0xb3) //gov
1867 //fprintf(stderr, "\nGOV\n");
1869 else if(s->buffer[ptr+3] == 0xb6) //vop
1871 int32_t delta;
1872 mp4_header_process_vop(&(vpriv->picture), &(s->buffer[ptr+4]));
1874 delta = vpriv->picture.timeinc_unit - vpriv->last_tr;
1875 if((delta > 0) && (delta > (vpriv->picture.timeinc_resolution/2)))
1876 delta -= vpriv->picture.timeinc_resolution;
1877 else if((delta < 0) && (delta < (-(vpriv->picture.timeinc_resolution/2))))
1878 delta += vpriv->picture.timeinc_resolution;
1880 delta_pts = (27000000 * (int64_t) delta) / vpriv->picture.timeinc_resolution;
1881 //warning, it seems that packed bops can lead to delta == 0
1883 pt = vpriv->picture.picture_type + 1;
1884 mp_msg(MSGT_MUXER, MSGL_DBG2, "\nTYPE: %c, RESOLUTION: %d, TEMP: %d, delta: %d, delta_pts: %"PRId64" = %.3lf, delta2: %.3lf\n",
1885 FTYPE(pt), vpriv->picture.timeinc_resolution, vpriv->picture.timeinc_unit, delta, delta_pts, (double) (delta_pts/27000000.0),
1886 (double) delta / (double) vpriv->picture.timeinc_resolution);
1888 vpriv->last_tr = vpriv->picture.timeinc_unit;
1890 break;
1893 ptr++;
1896 if(vpriv->vframes)
1898 vpriv->last_dts += vpriv->frame_duration;
1899 vpriv->last_pts += delta_pts;
1902 ret = add_frame(vpriv, delta_pts, s->buffer, len, pt, vpriv->last_dts, vpriv->last_pts);
1903 if(ret < 0)
1905 mp_msg(MSGT_MUXER, MSGL_FATAL, "\r\nPARSE_MPEG4: add_frames(%d) failed, exit\r\n", len);
1906 return 0;
1909 if(!vpriv->frame_duration && vpriv->framebuf_used == 3)
1911 vpriv->frame_duration = fix_mp4_frame_duration(vpriv);
1912 if(vpriv->frame_duration)
1914 vpriv->last_pts += vpriv->frame_duration;
1915 vpriv->last_dts = vpriv->framebuf[vpriv->framebuf_used-1].dts;
1916 vpriv->delta_clock = ((double) vpriv->frame_duration)/27000000.0;
1917 mp_msg(MSGT_MUXER, MSGL_INFO, "FRAME DURATION: %"PRIu64" %.3lf\n",
1918 vpriv->frame_duration, (double) (vpriv->frame_duration/27000000.0));
1919 vpriv->is_ready = 1;
1923 mp_msg(MSGT_MUXER, MSGL_DBG2, "LAST_PTS: %.3lf, LAST_DTS: %.3lf\n",
1924 (double) (vpriv->last_pts/27000000.0), (double) (vpriv->last_dts/27000000.0));
1926 vpriv->vframes++;
1928 return len;
1932 static int fill_last_frame(muxer_headers_t *spriv, uint8_t *ptr, int len)
1934 int idx;
1936 if(!len)
1937 return 0;
1939 if(spriv->framebuf_used == 0)
1940 idx = spriv->framebuf_used;
1941 else
1942 idx = spriv->framebuf_used - 1;
1944 if(spriv->framebuf[idx].alloc_size < spriv->framebuf[idx].size + len)
1946 if(spriv->framebuf[idx].size > SIZE_MAX - (size_t)len)
1947 return 0;
1948 spriv->framebuf[idx].buffer = (uint8_t*) realloc(spriv->framebuf[idx].buffer, spriv->framebuf[idx].size + len);
1949 if(! spriv->framebuf[idx].buffer)
1950 return 0;
1951 spriv->framebuf[idx].alloc_size = spriv->framebuf[idx].size + len;
1954 memcpy(&(spriv->framebuf[idx].buffer[spriv->framebuf[idx].size]), ptr, len);
1955 spriv->framebuf[idx].size += len;
1957 return len;
1960 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)
1962 int idx;
1964 idx = spriv->framebuf_used;
1965 if(idx >= spriv->framebuf_cnt)
1967 spriv->framebuf = (mpeg_frame_t*) realloc_struct(spriv->framebuf, (spriv->framebuf_cnt+1), sizeof(mpeg_frame_t));
1968 if(spriv->framebuf == NULL)
1970 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't realloc frame buffer(idx), abort\n");
1971 return -1;
1974 spriv->framebuf[spriv->framebuf_cnt].size = 0;
1975 spriv->framebuf[spriv->framebuf_cnt].alloc_size = 0;
1976 spriv->framebuf[spriv->framebuf_cnt].pos = 0;
1978 spriv->framebuf[spriv->framebuf_cnt].buffer = (uint8_t*) malloc(len);
1979 if(spriv->framebuf[spriv->framebuf_cnt].buffer == NULL)
1981 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't realloc frame buffer(frame), abort\n");
1982 return -1;
1984 spriv->framebuf[spriv->framebuf_cnt].alloc_size = len;
1985 spriv->framebuf_cnt++;
1988 if(spriv->framebuf[idx].alloc_size < spriv->framebuf[idx].size + len)
1990 if(spriv->framebuf[idx].size > SIZE_MAX - (size_t)len)
1992 mp_msg(MSGT_MUXER, MSGL_FATAL, "Size overflow, couldn't realloc frame buffer(frame), abort\n");
1993 return -1;
1995 spriv->framebuf[idx].buffer = realloc(spriv->framebuf[idx].buffer, spriv->framebuf[idx].size + len);
1996 if(spriv->framebuf[idx].buffer == NULL)
1998 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't realloc frame buffer(frame), abort\n");
1999 return -1;
2001 spriv->framebuf[idx].alloc_size = spriv->framebuf[idx].size + len;
2004 memcpy(&(spriv->framebuf[idx].buffer[spriv->framebuf[idx].size]), ptr, len);
2005 spriv->framebuf[idx].size += len;
2006 spriv->framebuf[idx].pos = 0;
2007 spriv->framebuf[idx].type = pt;
2009 spriv->framebuf[idx].idur = idur;
2010 spriv->framebuf[idx].dts = dts;
2011 spriv->framebuf[idx].pts = pts;
2012 spriv->framebuf_used++;
2013 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);
2015 return idx;
2018 static int analyze_mpa(muxer_stream_t *s)
2020 int i = 0, len, max, chans, srate, spf, layer;
2021 int score[4] = {0, 0, 0, 0};
2023 while(i < s->b_buffer_len + 3)
2025 if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xE0) == 0xE0))
2027 len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL);
2028 if(len > 0 && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len))
2030 score[layer]++;
2031 i += len;
2034 i++;
2037 max = 0;
2038 layer = 2;
2039 for(i = 1; i <= 3; i++)
2041 if(score[i] >= max)
2043 max = score[i];
2044 layer = i;
2048 return layer; //actual layer with the highest score
2051 int aac_parse_frame(uint8_t *buf, int *srate, int *num);
2053 static int parse_audio(muxer_stream_t *s, int finalize, unsigned int *nf, double *timer, double delay, int drop)
2055 int i, j, len, chans, srate, spf, layer, dummy, tot, num, frm_idx;
2056 int finished;
2057 unsigned int frames;
2058 uint64_t idur;
2059 double dur;
2060 muxer_headers_t *spriv = (muxer_headers_t *) s->priv;
2062 i = tot = frames = 0;
2063 finished = 0;
2064 while(1)
2066 len = 0;
2067 switch(s->wf->wFormatTag)
2069 case AUDIO_MP2:
2070 case AUDIO_MP3:
2072 if(i + 3 >= s->b_buffer_len)
2074 finished = 1;
2075 break;
2078 if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xE0) == 0xE0))
2080 len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL);
2081 if(len > 0 && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len)
2082 && layer == spriv->mpa_layer)
2084 dur = (double) spf / (double) srate;
2085 idur = (27000000ULL * spf) / srate;
2087 else
2088 len = 0;
2091 break;
2093 case AUDIO_A52:
2095 if(i + 6 >= s->b_buffer_len)
2097 finished = 1;
2098 break;
2101 if(s->b_buffer[i] == 0x0B && s->b_buffer[i+1] == 0x77)
2103 srate = 0;
2104 #ifdef CONFIG_LIBA52
2105 len = a52_syncinfo(&(s->b_buffer[i]), &dummy, &srate, &dummy);
2106 #else
2107 len = mp_a52_framesize(&(s->b_buffer[i]), &srate);
2108 #endif
2109 if((len > 0) && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len))
2111 dur = (double) 1536 / (double) srate;
2112 idur = (27000000ULL * 1536) / srate;
2114 else
2115 len = 0;
2118 break;
2120 case AUDIO_AAC1:
2121 case AUDIO_AAC2:
2123 if(i + 7 >= s->b_buffer_len)
2125 finished = 1;
2126 break;
2129 if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xF6) == 0xF0))
2131 len = aac_parse_frame(&(s->b_buffer[i]), &srate, &num);
2132 if((len > 0) && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len))
2134 dur = (double) 1024 / (double) srate;
2135 idur = (27000000ULL * 1024 * num) / srate;
2137 else
2138 len = 0;
2143 if(finished)
2144 break;
2146 if(!len)
2148 i++;
2149 continue;
2152 spriv->timer += dur;
2153 if(spriv->drop_delayed_frames && delay < 0 && spriv->timer <= -delay)
2155 i += len;
2156 tot = i;
2157 continue;
2160 frames++;
2161 fill_last_frame(spriv, &(s->b_buffer[tot]), i - tot);
2162 frm_idx = add_frame(spriv, idur, &(s->b_buffer[i]), len, 0, spriv->last_pts, spriv->last_pts);
2163 if(frm_idx < 0)
2165 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't add audio frame buffer(frame), abort\n");
2166 goto audio_exit;
2168 for(j = frm_idx; j < spriv->framebuf_cnt; j++)
2169 spriv->framebuf[j].pts = spriv->last_pts;
2170 spriv->last_pts += idur;
2172 i += len;
2173 tot = i;
2176 audio_exit:
2177 if(tot)
2179 memmove(s->b_buffer, &(s->b_buffer[tot]), s->b_buffer_len - tot);
2180 s->b_buffer_len -= tot;
2181 s->b_buffer_ptr += tot;
2182 if(s->b_buffer_len > 0)
2183 memmove(s->b_buffer, &(s->b_buffer[s->b_buffer_ptr]), s->b_buffer_len);
2184 s->b_buffer_ptr = 0;
2187 if(finalize)
2189 frm_idx = add_frame(spriv, 0, s->b_buffer, s->b_buffer_len, 0, spriv->last_pts, spriv->last_pts);
2190 if(frm_idx >= 0)
2192 for(j = frm_idx; j < spriv->framebuf_cnt; j++)
2193 spriv->framebuf[j].pts = spriv->last_pts;
2197 *nf = frames;
2198 *timer = spriv->timer;
2200 return tot;
2203 static void fix_parameters(muxer_stream_t *stream)
2205 muxer_headers_t *spriv = stream->priv;
2206 muxer_t *muxer = stream->muxer;
2207 muxer_priv_t *priv = muxer->priv;
2208 uint32_t stream_format;
2209 int needs_psm = 0;
2211 if(stream->type == MUXER_TYPE_AUDIO)
2213 stream_format = stream->wf->wFormatTag;
2214 spriv->is_ready = 1;
2215 if(conf_abuf_size)
2216 spriv->max_buffer_size = conf_abuf_size*1024;
2217 else
2218 spriv->max_buffer_size = 4*1024;
2219 if(stream->wf->wFormatTag == AUDIO_A52)
2221 stream->ckid = be2me_32 (0x1bd);
2222 if(priv->is_genmpeg1 || priv->is_genmpeg2)
2223 fix_audio_sys_header(priv, spriv->id, 0xbd, FFMAX(conf_abuf_size, 58)*1024); //only one audio at the moment
2224 spriv->id = 0xbd;
2225 if(!conf_abuf_size)
2226 spriv->max_buffer_size = 16*1024;
2228 else if(stream->wf->wFormatTag == AUDIO_AAC1 || stream->wf->wFormatTag == AUDIO_AAC2)
2229 needs_psm = 1;
2230 else if(stream->wf->wFormatTag == AUDIO_MP2 || stream->wf->wFormatTag == AUDIO_MP3)
2231 spriv->is_ready = 0;
2233 else //video
2235 stream_format = stream->bih->biCompression;
2236 if(conf_vbuf_size)
2237 spriv->max_buffer_size = conf_vbuf_size*1024;
2238 else
2240 if(priv->is_dvd)
2241 spriv->max_buffer_size = 232*1024;
2242 else if(priv->is_xsvcd)
2243 spriv->max_buffer_size = 230*1024;
2244 else if(priv->is_xvcd)
2245 spriv->max_buffer_size = 46*1024;
2246 else
2247 spriv->max_buffer_size = 232*1024; //no profile => unconstrained :) FIXME!!!
2250 if(is_mpeg4(stream->bih->biCompression))
2251 spriv->is_ready = 0;
2252 else
2253 spriv->is_ready = 1;
2255 if(!is_mpeg1(stream_format) && !is_mpeg2(stream_format))
2256 needs_psm = 1;
2259 if(priv->is_genmpeg2 && needs_psm)
2261 priv->use_psm = 1;
2262 add_to_psm(priv, spriv->id, stream_format);
2263 priv->psm_streams_cnt++;
2268 static void mpegfile_write_chunk(muxer_stream_t *s,size_t len,unsigned int flags, double dts_arg, double pts_arg)
2270 size_t sz = 0;
2271 uint64_t tmp;
2272 muxer_t *muxer = s->muxer;
2273 muxer_priv_t *priv = (muxer_priv_t *)muxer->priv;
2274 muxer_headers_t *spriv = (muxer_headers_t*) s->priv;
2275 float fps;
2276 uint32_t stream_format, nf;
2278 if(s->buffer == NULL || len == -1)
2279 return;
2281 if (s->type == MUXER_TYPE_VIDEO)
2282 { // try to recognize frame type...
2283 fps = (float) s->h.dwRate/ (float) s->h.dwScale;
2284 spriv->type = 1;
2285 stream_format = s->bih->biCompression;
2286 if(! spriv->vframes)
2288 spriv->last_dts = spriv->last_pts - (uint64_t)(27000000.0f/fps);
2289 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);
2292 if(is_mpeg1(stream_format) || is_mpeg2(stream_format))
2294 spriv->is_mpeg12 = 1;
2295 spriv->is_ready = 1;
2296 if(len)
2297 sz = parse_mpeg12_video(s, priv, spriv, fps, len);
2298 else
2300 tmp = (uint64_t) (27000000.0f / fps);
2301 spriv->last_pts += tmp;
2302 spriv->last_dts += tmp;
2305 else if(is_mpeg4(stream_format))
2307 spriv->is_mpeg12 = 0;
2308 spriv->telecine = 0;
2309 if(len)
2310 sz = parse_mpeg4_video(s, priv, spriv, fps, len);
2311 else
2313 tmp = (uint64_t) (27000000.0f / fps);
2314 spriv->last_pts += tmp;
2315 spriv->last_dts += tmp;
2319 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);
2321 else
2322 { // MUXER_TYPE_AUDIO
2323 double fake_timer;
2324 spriv->type = 0;
2325 stream_format = s->wf->wFormatTag;
2327 if(s->b_buffer_size - s->b_buffer_len < len)
2329 void *tmp;
2331 if(s->b_buffer_len > SIZE_MAX - len)
2333 mp_msg(MSGT_MUXER, MSGL_FATAL, "\nFATAL! couldn't realloc, integer overflow\n");
2334 return;
2336 tmp = realloc(s->b_buffer, len + s->b_buffer_len);
2337 if(!tmp)
2339 mp_msg(MSGT_MUXER, MSGL_FATAL, "\nFATAL! couldn't realloc %d bytes\n", len + s->b_buffer_len);
2340 return;
2342 s->b_buffer = tmp;
2344 s->b_buffer_size = len + s->b_buffer_len;
2345 mp_msg(MSGT_MUXER, MSGL_DBG2, "REALLOC(%d) bytes to AUDIO backbuffer\n", s->b_buffer_size);
2347 memcpy(&(s->b_buffer[s->b_buffer_ptr + s->b_buffer_len]), s->buffer, len);
2348 s->b_buffer_len += len;
2350 if(!spriv->is_ready)
2352 if(s->b_buffer_len >= 32*1024)
2354 spriv->mpa_layer = analyze_mpa(s);
2355 spriv->is_ready = 1;
2358 else
2360 parse_audio(s, 0, &nf, &fake_timer, priv->init_adelay, priv->drop);
2361 spriv->vframes += nf;
2362 if(! spriv->vframes)
2363 mp_msg(MSGT_MUXER, MSGL_INFO, "AINIT: %.3lf\r\n", (double) spriv->last_pts/27000000.0f);
2367 flush_buffers(muxer, 0);
2371 static void mpegfile_write_index(muxer_t *muxer)
2373 int i, nf;
2374 double fake_timer;
2375 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
2377 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingTrailer);
2379 for(i = 0; i < muxer->avih.dwStreams; i++)
2381 if(muxer->streams[i]->type == MUXER_TYPE_AUDIO)
2382 parse_audio(muxer->streams[i], 1, &nf, &fake_timer, priv->init_adelay, priv->drop);
2384 while(flush_buffers(muxer, 0) > 0);
2385 flush_buffers(muxer, 1);
2386 if(priv->is_genmpeg1 || priv->is_genmpeg2)
2388 priv->scr = 0;
2389 write_mpeg_pack(muxer, NULL, muxer->stream, 1); //insert fake Nav Packet
2392 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);
2395 static void mpegfile_write_header(muxer_t *muxer)
2397 muxer_priv_t *priv = (muxer_priv_t*) muxer->priv;
2399 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingHeader);
2401 priv->headers_cnt++;
2403 //write the first system header only for generic mpeg1/2 muxes, and only when we have collected all necessary infos
2404 if(priv->is_genmpeg1 || priv->is_genmpeg2 || ((priv->is_xvcd || priv->is_xsvcd) && (priv->headers_cnt == 1)))
2406 write_mpeg_pack(muxer, NULL, muxer->stream, 0);
2407 priv->update_system_header = 0;
2410 return;
2413 static void setup_sys_params(muxer_priv_t *priv)
2415 if(priv->is_dvd)
2417 int v = (conf_vbuf_size ? conf_vbuf_size : 232);
2418 int a1 = (conf_abuf_size ? conf_abuf_size : 4);
2419 int a2 = (conf_abuf_size>58 ? conf_abuf_size : 58);
2421 priv->sys_info.cnt = 4;
2423 priv->sys_info.streams[0].id = 0xb9;
2424 priv->sys_info.streams[0].type = 1;
2425 priv->sys_info.streams[0].bufsize = v*1024;
2427 priv->sys_info.streams[1].id = 0xb8;
2428 priv->sys_info.streams[1].type = 0;
2429 priv->sys_info.streams[1].bufsize = a1*1024;
2431 priv->sys_info.streams[2].id = 0xbd;
2432 priv->sys_info.streams[2].type = 1;
2433 priv->sys_info.streams[2].bufsize = a2*1024;
2435 priv->sys_info.streams[3].id = 0xbf;
2436 priv->sys_info.streams[3].type = 1;
2437 priv->sys_info.streams[3].bufsize = 2*1024;
2439 else if(priv->is_xvcd || priv->is_xsvcd)
2441 int v = (conf_vbuf_size ? conf_vbuf_size : (priv->is_xvcd ? 46: 230));
2442 int a1 = (conf_abuf_size ? conf_abuf_size : 4);
2444 priv->sys_info.cnt = 2;
2446 priv->sys_info.streams[0].id = 0xe0;
2447 priv->sys_info.streams[0].type = 1;
2448 priv->sys_info.streams[0].bufsize = v*1024;
2450 priv->sys_info.streams[1].id = 0xc0;
2451 priv->sys_info.streams[1].type = 0;
2452 priv->sys_info.streams[1].bufsize = a1*1024;
2454 else
2455 priv->sys_info.cnt = 0;
2458 /* excerpt from DGPulldown Copyright (C) 2005-2006, Donald Graft */
2459 static void generate_flags(uint8_t *bff_mask, int source, int target)
2461 unsigned int i, trfp;
2462 uint64_t dfl,tfl;
2463 unsigned char ormask[4] = {0x0, 0x2, 0x80, 0x82};
2465 dfl = (target - source) << 1;
2466 tfl = source >> 1;
2468 trfp = 0;
2469 for(i = 0; i < MAX_PATTERN_LENGTH; i++)
2471 tfl += dfl;
2472 if(tfl >= source)
2474 tfl -= source;
2475 bff_mask[i] = ormask[trfp + 1];
2476 trfp ^= 2;
2478 else
2479 bff_mask[i] = ormask[trfp];
2483 int muxer_init_muxer_mpeg(muxer_t *muxer)
2485 muxer_priv_t *priv;
2486 priv = (muxer_priv_t *) calloc(1, sizeof(muxer_priv_t));
2487 if(priv == NULL)
2488 return 0;
2489 priv->update_system_header = 1;
2491 //calloc() already zero-ed all flags, so we assign only the ones we need
2493 if(conf_mux != NULL)
2495 if(! strcasecmp(conf_mux, "mpeg1"))
2497 priv->mux = MUX_MPEG1;
2498 priv->packet_size = 2048;
2499 priv->is_genmpeg1 = 1;
2500 priv->muxrate = 1800 * 125; //Constrained parameters
2502 else if(! strcasecmp(conf_mux, "dvd"))
2504 priv->mux = MUX_MPEG2;
2505 priv->is_dvd = 1;
2506 priv->packet_size = 2048;
2507 priv->muxrate = 10080 * 125;
2509 else if(! strcasecmp(conf_mux, "xsvcd"))
2511 priv->mux = MUX_MPEG2;
2512 priv->is_xsvcd = 1;
2513 priv->packet_size = 2324;
2514 priv->muxrate = 150*2324;
2515 priv->ts_allframes = 1;
2517 else if(! strcasecmp(conf_mux, "xvcd"))
2519 priv->mux = MUX_MPEG1;
2520 priv->is_xvcd = 1;
2521 priv->packet_size = 2324;
2522 priv->muxrate = 75*2352;
2523 priv->ts_allframes = 1;
2525 else if(! strcasecmp(conf_mux, "pes1"))
2527 priv->mux = MUX_MPEG1;
2528 priv->rawpes = 1;
2529 priv->packet_size = 2048;
2530 priv->muxrate = 10080 * 125;
2531 priv->ts_allframes = 1;
2533 else if(! strcasecmp(conf_mux, "pes2"))
2535 priv->mux = MUX_MPEG2;
2536 priv->rawpes = 1;
2537 priv->packet_size = 2048;
2538 priv->muxrate = 10080 * 125;
2539 priv->ts_allframes = 1;
2541 else
2543 if(strcasecmp(conf_mux, "mpeg2"))
2544 mp_msg(MSGT_MUXER, MSGL_ERR, "Unknown format %s, default to mpeg2\n", conf_mux);
2545 priv->mux = MUX_MPEG2;
2546 priv->is_genmpeg2 = 1;
2547 priv->packet_size = 2048;
2548 priv->muxrate = 1800 * 125; //Constrained parameters
2552 if(conf_ts_allframes)
2553 priv->ts_allframes = 1;
2554 if(conf_muxrate > 0)
2555 priv->muxrate = conf_muxrate * 125; // * 1000 / 8
2556 if(conf_packet_size)
2557 priv->packet_size = conf_packet_size;
2558 priv->delta_scr = (uint64_t) (90000.0f*300.0f*(double)priv->packet_size/(double)priv->muxrate);
2559 mp_msg(MSGT_MUXER, MSGL_INFO, "PACKET SIZE: %u bytes, deltascr: %"PRIu64"\n", priv->packet_size, priv->delta_scr);
2560 setup_sys_params(priv);
2562 if(conf_vaspect > 0)
2564 int asp = (int) (conf_vaspect * 1000.0f);
2565 if(asp >= 1332 && asp <= 1334)
2566 priv->vaspect = ASPECT_4_3;
2567 else if(asp >= 1776 && asp <= 1778)
2568 priv->vaspect = ASPECT_16_9;
2569 else if(asp >= 2209 && asp <= 2211)
2570 priv->vaspect = ASPECT_2_21_1;
2571 else if(asp == 1000)
2572 priv->vaspect = ASPECT_1_1;
2573 else
2574 mp_msg(MSGT_MUXER, MSGL_ERR, "ERROR: unrecognized aspect %.3f\n", conf_vaspect);
2577 priv->vframerate = 0; // no change
2578 if(conf_telecine && conf_vframerate > 0)
2580 mp_msg(MSGT_MUXER, MSGL_ERR, "ERROR: options 'telecine' and 'vframerate' are mutually exclusive, vframerate disabled\n");
2581 conf_vframerate = 0;
2584 if(conf_telecine == TELECINE_FILM2PAL)
2586 if(conf_telecine_src==0.0f) conf_telecine_src = 24000.0/1001.0;
2587 conf_telecine_dest = 25;
2588 conf_telecine = TELECINE_DGPULLDOWN;
2590 else if(conf_telecine == PULLDOWN32)
2592 if(conf_telecine_src==0.0f) conf_telecine_src = 24000.0/1001.0;
2593 conf_telecine_dest = 30000.0/1001.0;
2594 conf_telecine = TELECINE_DGPULLDOWN;
2597 if(conf_telecine_src>0 && conf_telecine_dest>0 && conf_telecine_src < conf_telecine_dest)
2599 int sfps, tfps;
2601 sfps = (int) (conf_telecine_src * 1001 + 0.5);
2602 tfps = (int) (conf_telecine_dest * 1001 + 0.5);
2603 if(sfps % 2 || tfps % 2)
2605 sfps *= 2;
2606 tfps *= 2;
2609 if(((tfps - sfps)>>1) > sfps)
2611 mp_msg(MSGT_MUXER, MSGL_ERR, "ERROR! Framerate increment must be <= 1.5, telecining disabled\n");
2612 conf_telecine = 0;
2614 else
2616 generate_flags(priv->bff_mask, sfps, tfps);
2617 conf_telecine = TELECINE_DGPULLDOWN;
2618 conf_vframerate = conf_telecine_dest;
2622 if(conf_vframerate)
2624 int fps;
2626 fps = (int) (conf_vframerate * 1001 + 0.5);
2627 switch(fps)
2629 case 24000:
2630 priv->vframerate = FRAMERATE_23976;
2631 break;
2632 case 24024:
2633 priv->vframerate = FRAMERATE_24;
2634 break;
2635 case 25025:
2636 priv->vframerate = FRAMERATE_25;
2637 break;
2638 case 30000:
2639 priv->vframerate = FRAMERATE_2997;
2640 break;
2641 case 30030:
2642 priv->vframerate = FRAMERATE_30;
2643 break;
2644 case 50050:
2645 priv->vframerate = FRAMERATE_50;
2646 break;
2647 case 60000:
2648 priv->vframerate = FRAMERATE_5994;
2649 break;
2650 case 60060:
2651 priv->vframerate = FRAMERATE_60;
2652 break;
2653 default:
2655 mp_msg(MSGT_MUXER, MSGL_ERR, "WRONG FPS: %d/1000, ignoring\n", fps);
2656 if(conf_telecine)
2657 mp_msg(MSGT_MUXER, MSGL_ERR, "DISABLED TELECINING\n");
2658 conf_telecine = 0;
2663 priv->vwidth = (uint16_t) conf_vwidth;
2664 priv->vheight = (uint16_t) conf_vheight;
2665 priv->panscan_width = (uint16_t) conf_panscan_width;
2666 priv->panscan_height = (uint16_t) conf_panscan_height;
2667 priv->vbitrate = ((conf_vbitrate) * 10) >> 2; //*1000 / 400
2669 if(priv->vaspect || priv->vframerate || priv->vwidth || priv->vheight || priv->vbitrate || priv->panscan_width || priv->panscan_height)
2671 priv->patch_seq = priv->vaspect || priv->vframerate || priv->vwidth || priv->vheight || priv->vbitrate;
2672 priv->patch_sde = priv->panscan_width || priv->panscan_height;
2673 mp_msg(MSGT_MUXER, MSGL_INFO, "MPEG MUXER, patching");
2674 if(priv->vwidth || priv->vheight)
2675 mp_msg(MSGT_MUXER, MSGL_INFO, " resolution to %dx%d", priv->vwidth, priv->vheight);
2676 if(priv->panscan_width || priv->panscan_height)
2677 mp_msg(MSGT_MUXER, MSGL_INFO, " panscan to to %dx%d", priv->panscan_width, priv->panscan_height);
2678 if(priv->vframerate)
2679 mp_msg(MSGT_MUXER, MSGL_INFO, " framerate to %s fps", framerates[priv->vframerate]);
2680 if(priv->vaspect)
2681 mp_msg(MSGT_MUXER, MSGL_INFO, " aspect ratio to %s", aspect_ratios[priv->vaspect]);
2682 if(priv->vbitrate)
2683 mp_msg(MSGT_MUXER, MSGL_INFO, " bitrate to %u", conf_vbitrate);
2684 mp_msg(MSGT_MUXER, MSGL_INFO, "\n");
2687 priv->has_video = priv->has_audio = 0;
2689 muxer->sysrate = priv->muxrate; // initial muxrate = constrained stream parameter
2690 priv->scr = muxer->file_end = 0;
2692 if(conf_init_vdelay && conf_drop)
2694 mp_msg(MSGT_MUXER, MSGL_ERR, "\nmuxer_mpg, :drop and :vdelay used together are not supported, exiting\n");
2695 return 0;
2697 if(conf_init_adelay)
2698 priv->init_adelay = - (double) conf_init_adelay / (double) 1000.0;
2700 priv->drop = conf_drop;
2702 priv->buff = (uint8_t *) malloc(priv->packet_size);
2703 if((priv->buff == NULL))
2705 mp_msg(MSGT_MUXER, MSGL_ERR, "\nCouldn't allocate %d bytes, exit\n", priv->packet_size);
2706 return 0;
2709 muxer->priv = (void *) priv;
2710 muxer->cont_new_stream = &mpegfile_new_stream;
2711 muxer->cont_write_chunk = &mpegfile_write_chunk;
2712 muxer->cont_write_header = &mpegfile_write_header;
2713 muxer->cont_write_index = &mpegfile_write_index;
2714 muxer->fix_stream_parameters = &fix_parameters;
2715 return 1;