13 #include "aviheader.h"
16 #include "stream/stream.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
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
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;
99 uint64_t pts
, dts
, idur
;
100 uint32_t pos
; //start offset for the frame
104 uint8_t cnt
; // how many entries we use
109 } streams
[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
113 uint8_t cnt
; // how many entries we use
118 } streams
[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
128 uint64_t frame_dts
, frame_pts
;
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
;
142 uint64_t last_psm_scr
;
145 uint32_t headers_cnt
;
149 //video patching parameters
150 uint8_t vaspect
, vframerate
;
151 uint16_t vwidth
, vheight
, panscan_width
, panscan_height
;
153 int patch_seq
, patch_sde
;
156 //2 million frames are enough
157 #define MAX_PATTERN_LENGTH 2000000
158 uint8_t bff_mask
[MAX_PATTERN_LENGTH
];
163 int has_pts
, has_dts
, pes_is_aligned
, type
, min_pes_hlen
;
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
;
174 uint8_t id
, is_mpeg12
, telecine
;
176 int64_t display_frame
;
177 mp_mpeg_header_t picture
;
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
182 int pack_offset
, pes_offset
, pes_set
, payload_offset
;
184 int last_frame_rest
; //the rest of the previous frame
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
)
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
)
237 (x
== mmioFOURCC('m','p','g','1')) ||
238 (x
== mmioFOURCC('M','P','G','1'))
242 static inline int is_mpeg2(uint32_t x
)
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
)
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'))
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);
287 for(i
= 0; i
< size
; i
++)
288 crc
= (crc
<< 8) ^ CRCTab
[((crc
>> 24) ^ buff
[i
]) & 0xff];
294 static void add_to_psm(muxer_priv_t
*priv
, uint8_t id
, uint32_t format
)
298 i
= priv
->psm_info
.cnt
;
299 priv
->psm_info
.streams
[i
].id
= id
;
300 priv
->psm_info
.streams
[i
].format
= 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;
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
)
327 tmp
= (mpeg_frame_t
*) calloc(num
, sizeof(mpeg_frame_t
));
331 for(i
=0; i
< num
; i
++)
333 tmp
[i
].buffer
= (uint8_t *) calloc(1, size
);
334 if(tmp
[i
].buffer
== NULL
)
338 tmp
[i
].alloc_size
= size
;
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
;
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");
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");
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");
371 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Unknown stream type!\n");
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
)))
378 s
->b_buffer_size
= priv
->packet_size
;
381 s
->priv
= (muxer_headers_t
*) calloc(1, sizeof(muxer_headers_t
));
384 spriv
= (muxer_headers_t
*) s
->priv
;
385 spriv
->pack
= malloc(priv
->packet_size
);
388 spriv
->buffer_track
= calloc(1, 4096*sizeof(buffer_track_t
));
389 if(!spriv
->buffer_track
)
391 spriv
->track_pos
= 0;
392 spriv
->track_len
= 4096;
393 muxer
->streams
[muxer
->avih
.dwStreams
]=s
;
395 s
->id
=muxer
->avih
.dwStreams
;
398 if (type
== MUXER_TYPE_VIDEO
) {
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
++;
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");
428 memset(&(spriv
->picture
), 0, sizeof(spriv
->picture
));
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
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
++;
455 spriv
->min_pes_hlen
= 13;
456 else if(priv
->is_xsvcd
)
457 spriv
->min_pes_hlen
= 17;
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");
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
++;
484 if(spriv
->buffer_track
)
485 free(spriv
->buffer_track
);
495 static void write_mpeg_ts(unsigned char *b
, uint64_t ts
, uint8_t mod
) {
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;
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);
535 b
[0] = ((size
>> 8) & 0x3f) | (type
==1 ? 0x60 : 0x40) | mod
;
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;
544 ts
&= 0x1FFFFFFFFULL
; //33 bits
545 t1
= (ts
>> 30) & 0x7;;
546 t2
= (ts
>> 15) & 0x7fff;
549 b
[0] = (t1
<< 3 ) | 0x44 | ((t2
>> 13) & 0x3);
551 b
[2] = (t2
& 0x1f) << 3 | 0x4 | ((t3
>> 13) & 0x3);
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
)
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
);
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
583 static int write_mpeg_system_header(muxer_t
*muxer
, char *buff
)
588 priv
= (muxer_priv_t
*) muxer
->priv
;
591 *(uint32_t *)(&buff
[len
]) = be2me_32(SYSTEM_HEADER_START_CODE
);
593 *(uint16_t *)(&buff
[len
]) = 0; //fake length, we'll fix it later
595 write_mpeg_rate(MUX_MPEG1
, &buff
[len
], muxer
->sysrate
);
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
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));
615 *(uint16_t *)(&buff
[4]) = be2me_16(len
- 6); // length field fixed
620 static int write_mpeg_psm(muxer_t
*muxer
, char *buff
)
626 priv
= (muxer_priv_t
*) muxer
->priv
;
629 *(uint32_t *)(&buff
[len
]) = be2me_32(PSM_START_CODE
);
631 *(uint16_t *)(&buff
[len
]) = 0; //fake length, we'll fix it later
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)
637 *(uint16_t *)(&buff
[len
]) = 0; //length of the es descriptors
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
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
));
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
)
678 memcpy(&buff
[len
], pes_id
, 4);
681 buff
[len
] = buff
[len
+1] = 0; //fake len
684 if(mux_type
== MUX_MPEG1
)
688 memset(&buff
[len
], 0xff, stuffing_len
);
692 if(h
->buffer_size
> 0)
694 write_mpeg_std(&buff
[len
], h
->buffer_size
, h
->type
, 0x40); // 01 is pes1 format
700 buff
[len
] = (h
->pes_is_aligned
? 0x84 : 0x80); //0x10...
702 buff
[len
] = ((h
->buffer_size
> 0) ? 1 : 0) | (h
->pts
? (h
->dts
? 0xC0 : 0x80) : 0); //pes extension + pts/dts flags
704 buff
[len
] = (h
->pts
? (h
->dts
? 10 : 5) : 0) + ((h
->buffer_size
> 0) ? 3 : 0) + stuffing_len
;//pts + std + stuffing
711 write_mpeg_ts(&buff
[len
], h
->pts
, (h
->dts
? 0x30 : 0x20)); // 001x and both PTS/DTS
716 write_mpeg_ts(&buff
[len
], h
->dts
, 0x10); // 0001 before DTS
722 if(mux_type
== MUX_MPEG1
)
730 if(mux_type
== MUX_MPEG2
)
732 if(h
->buffer_size
> 0)
734 buff
[len
] = 0x1e; //std flag
737 write_mpeg_std(&buff
[len
], h
->buffer_size
, h
->type
, 0x40);
743 memset(&buff
[len
], 0xff, stuffing_len
);
748 *((uint16_t*) &buff
[4]) = be2me_16(len
+ plen
- 6); //fix pes packet size
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;
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
769 mp_msg(MSGT_MUXER
, MSGL_DBG3
, "NAV\n");
771 *(uint32_t *)(&buff
[len
]) = be2me_32(PES_PRIVATE2
);
775 memset(&buff
[len
], 0, 0x03d4);
778 *(uint32_t *)(&buff
[len
]) = be2me_32(PES_PRIVATE2
);
782 memset(&buff
[len
], 0, 0x03fa);
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
)
797 if(format
== MUX_MPEG1
)
808 else if(format
== MUX_MPEG1
)
811 if(h
->buffer_size
> 0)
813 if(format
== MUX_MPEG2
)
819 //len = max(h->min_pes_hlen, len);
825 static int write_mpeg_pack(muxer_t
*muxer
, muxer_stream_t
*s
, stream_t
*stream
, int isoend
)
832 priv
= (muxer_priv_t
*) muxer
->priv
;
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
);
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;
854 offset
+= write_nav_pack(&buff
[offset
]);
856 stuffing_len
= priv
->packet_size
- offset
;
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
;
867 muxer
->movi_end
+= 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
);
882 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\r\nERROR, couldn't realloc %d bytes for tracking buffer\r\n", dim
);
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
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
;
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
909 spriv
->pack
[x
+2] = spriv
->pack
[x
+3] = 0;
912 static inline void remove_frames(muxer_headers_t
*spriv
, int n
)
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
;
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
)
937 m
= FFMIN(spriv
->framebuf
[n
].size
- frpos
, psize
- len
);
940 if(frpos
== spriv
->framebuf
[n
].size
)
947 if(len
< psize
&& !finalize
)
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
)
963 spriv
->dts
= spriv
->pts
= 0;
965 if(spriv
->framebuf
[0].pos
== 0) // start of frame
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
980 if(spriv
->framebuf_used
< 2)
983 if(spriv
->framebuf
[1].pts
== spriv
->framebuf
[1].dts
)
988 //headers+frame 0 < space available including timestamps
989 if(start
+ pes_hlen
+ m
< priv
->packet_size
- threshold
)
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
)
1004 else if((spriv
->framebuf
[i
].type
== I_FRAME
|| priv
->ts_allframes
|| dpts
>= 36000*300)) //0.4 seconds
1009 *pts
= spriv
->framebuf
[i
].pts
;
1010 *dts
= spriv
->framebuf
[i
].dts
;
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
;
1028 spriv
->pts
= spriv
->dts
= 0;
1029 p
->dts
= p
->pts
= p
->frame_pts
= p
->frame_dts
= 0;
1034 else if(priv
->mux
== MUX_MPEG1
)
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
;
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
)
1062 len2
= calc_packet_len(s
, target
, finalize
);
1063 if(!len2
|| (len2
< target
&& s
->type
== MUXER_TYPE_AUDIO
&& !finalize
))
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);
1076 if(s
->type
== MUXER_TYPE_VIDEO
)
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
;
1097 stflen
+= stuffing_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;
1124 spriv
->dts
= spriv
->pts
= 0;
1126 if(! spriv
->framebuf_used
)
1128 spriv
->pack_offset
= 0;
1132 if(!spriv
->pack_offset
)
1135 spriv
->pack_offset
= 0;
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
]);
1151 spriv
->pes_offset
= spriv
->pack_offset
;
1152 spriv
->payload_offset
= 0;
1154 spriv
->last_frame_rest
= 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));
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)
1176 if(! get_packet_stats(priv
, s
, &p
, finalize
))
1178 spriv
->pack_offset
= 0;
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;
1196 spriv
->last_frame_rest
= spriv
->framebuf
[0].size
- spriv
->framebuf
[0].pos
;
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);
1214 frm
= spriv
->framebuf
;
1215 while(spriv
->pack_offset
< priv
->packet_size
&& n
< spriv
->framebuf_used
)
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
)
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
);
1233 spriv
->pack_offset
+= m
;
1236 if(frm
->pos
== frm
->size
) //end of frame
1238 frm
->pos
= frm
->size
= 0;
1239 frm
->pts
= frm
->dts
= 0;
1245 if((priv
->is_xsvcd
|| priv
->is_xvcd
|| priv
->rawpes
) && spriv
->size
== 0)
1246 spriv
->buffer_size
= 0;
1250 if(dvd_pack
&& (spriv
->pack_offset
== priv
->packet_size
))
1251 write_mpeg_pack(muxer
, NULL
, muxer
->stream
, 0); //insert fake Nav Packet
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
)
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;
1280 priv
->last_psm_scr
= priv
->scr
;
1285 static inline int find_best_stream(muxer_t
*muxer
)
1289 muxer_priv_t
*priv
= muxer
->priv
;
1290 muxer_headers_t
*spriv
;
1292 unsigned int perc
, sperc
;
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
;
1303 get_packet_stats(priv
, muxer
->streams
[i
], &p
, 0);
1305 if(spriv
->track_bufsize
+ p
.len
> spriv
->max_buffer_size
)
1307 if(p
.frame_pts
&& p
.frame_dts
> priv
->scr
+ 63000*300)
1310 if(spriv
->framebuf
[0].dts
<= dts
)
1312 dts
= spriv
->framebuf
[0].dts
;
1316 if(conf_interleaving2
)
1318 sperc
= (spriv
->track_bufsize
* 1024) / spriv
->max_buffer_size
;
1330 static void patch_seq(muxer_priv_t
*priv
, unsigned char *buf
)
1332 if(priv
->vwidth
> 0)
1334 buf
[4] = (priv
->vwidth
>> 4) & 0xff;
1336 buf
[5] |= (priv
->vwidth
& 0x0f) << 4;
1339 if(priv
->vheight
> 0)
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)
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);
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
;
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
;
1411 while(j
< spriv
->track_pos
&& priv
->scr
>= spriv
->buffer_track
[j
].dts
)
1413 spriv
->track_bufsize
-= spriv
->buffer_track
[j
].size
;
1416 if(spriv
->track_bufsize
< 0)
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;
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
)
1444 if(vpriv
->framebuf_used
> 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
)
1468 if(found
&& (n
< vpriv
->framebuf_used
+1))
1474 static int flush_buffers(muxer_t
*muxer
, int finalize
)
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
;
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
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
)
1502 n
= found
= calc_frames_to_flush(vpriv
);
1504 else if(s
->type
== MUXER_TYPE_AUDIO
)
1508 if((! found
) && finalize
)
1511 found
= n
= vpriv
->framebuf_used
;
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
;
1522 for(i
= 0; i
< n
; i
++)
1523 iduration
+= vpriv
->framebuf
[i
].idur
;
1524 duration
= (double) (iduration
/ 27000000.0);
1528 apriv
= (muxer_headers_t
*) as
->priv
;
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");
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;
1563 i
= find_best_stream(muxer
);
1566 if(!fill_packet(muxer
, muxer
->streams
[i
], finalize
))
1569 if(skip_cnt
== muxer
->avih
.dwStreams
)
1577 muxer
->file_end
= priv
->scr
;
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
)
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;
1609 vpriv
->picture
.progressive_sequence
= 0;
1610 vpriv
->picture
.progressive_frame
= 1;
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
);
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
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
);
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
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
);
1670 patch_seq(priv
, &(s
->buffer
[i
]));
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))
1686 patch_panscan(priv
, &(s
->buffer
[i
+4]));
1688 if((s
->buffer
[i
+4] & 0xf0) == 0x80)
1690 pce_ptr
= &(s
->buffer
[i
+4]);
1704 pt
= (s
->buffer
[i
+5] & 0x1c) >> 3;
1705 temp_ref
= (s
->buffer
[i
+4]<<2)+(s
->buffer
[i
+5]>>6);
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
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
);
1720 spriv
->last_tr
= spriv
->max_tr
= temp_ref
;
1721 d1
= temp_ref
- spriv
->last_tr
;
1723 frames_diff
= spriv
->max_tr
+ 1 + temp_ref
- spriv
->last_tr
;
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
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
)
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
);
1763 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\r\nPARSE_MPEG12: add_frames(%d) failed, exit\r\n", len
);
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
);
1773 int64_t diff
= spriv
->last_dts
- spriv
->last_pts
;
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
;
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
;
1794 mp_msg(MSGT_MUXER
, MSGL_DBG2
,"parse_mpeg12_video, return %u\n", (uint32_t) len
);
1799 static uint64_t fix_mp4_frame_duration(muxer_headers_t
*vpriv
)
1801 uint64_t mn
, md
, mx
, diff
;
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
;
1816 if((vpriv
->framebuf
[i
].pts
> mn
) && (vpriv
->framebuf
[i
].pts
< mx
))
1817 md
= vpriv
->framebuf
[i
].pts
;
1820 if(mx
- md
> md
- mn
)
1825 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, "MIN: %"PRIu64
", mid: %"PRIu64
", max: %"PRIu64
", diff: %"PRIu64
"\n", mn
, md
, mx
, diff
);
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));
1842 static size_t parse_mpeg4_video(muxer_stream_t
*s
, muxer_priv_t
*priv
, muxer_headers_t
*vpriv
, float fps
, size_t len
)
1845 int64_t delta_pts
=0;
1849 mp_msg(MSGT_MUXER
, MSGL_DBG2
,"parse_mpeg4_video, len=%u\n", (uint32_t) len
);
1852 mp_msg(MSGT_MUXER
, MSGL_ERR
,"Frame too short: %d, exit!\n", len
);
1857 while(ptr
< len
- 5)
1859 if(s
->buffer
[ptr
] != 0 || s
->buffer
[ptr
+1] != 0 || s
->buffer
[ptr
+2] != 1)
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
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
;
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
);
1909 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\r\nPARSE_MPEG4: add_frames(%d) failed, exit\r\n", len
);
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));
1936 static int fill_last_frame(muxer_headers_t
*spriv
, uint8_t *ptr
, int len
)
1943 if(spriv
->framebuf_used
== 0)
1944 idx
= spriv
->framebuf_used
;
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
)
1952 spriv
->framebuf
[idx
].buffer
= (uint8_t*) realloc(spriv
->framebuf
[idx
].buffer
, spriv
->framebuf
[idx
].size
+ len
);
1953 if(! spriv
->framebuf
[idx
].buffer
)
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
;
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
)
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");
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");
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");
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");
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
);
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
))
2043 for(i
= 1; i
<= 3; 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
;
2061 unsigned int frames
;
2064 muxer_headers_t
*spriv
= (muxer_headers_t
*) s
->priv
;
2066 i
= tot
= frames
= 0;
2071 switch(s
->wf
->wFormatTag
)
2076 if(i
+ 3 >= s
->b_buffer_len
)
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
;
2099 if(i
+ 6 >= s
->b_buffer_len
)
2105 if(s
->b_buffer
[i
] == 0x0B && s
->b_buffer
[i
+1] == 0x77)
2109 len
= a52_syncinfo(&(s
->b_buffer
[i
]), &dummy
, &srate
, &dummy
);
2111 len
= mp_a52_framesize(&(s
->b_buffer
[i
]), &srate
);
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
;
2127 if(i
+ 7 >= s
->b_buffer_len
)
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
;
2156 spriv
->timer
+= dur
;
2157 if(spriv
->drop_delayed_frames
&& delay
< 0 && spriv
->timer
<= -delay
)
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
);
2169 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't add audio frame buffer(frame), abort\n");
2172 for(j
= frm_idx
; j
< spriv
->framebuf_cnt
; j
++)
2173 spriv
->framebuf
[j
].pts
= spriv
->last_pts
;
2174 spriv
->last_pts
+= idur
;
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;
2193 frm_idx
= add_frame(spriv
, 0, s
->b_buffer
, s
->b_buffer_len
, 0, spriv
->last_pts
, spriv
->last_pts
);
2196 for(j
= frm_idx
; j
< spriv
->framebuf_cnt
; j
++)
2197 spriv
->framebuf
[j
].pts
= spriv
->last_pts
;
2202 *timer
= spriv
->timer
;
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
;
2215 if(stream
->type
== MUXER_TYPE_AUDIO
)
2217 stream_format
= stream
->wf
->wFormatTag
;
2218 spriv
->is_ready
= 1;
2220 spriv
->max_buffer_size
= conf_abuf_size
*1024;
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
2230 spriv
->max_buffer_size
= 16*1024;
2232 else if(stream
->wf
->wFormatTag
== AUDIO_AAC1
|| stream
->wf
->wFormatTag
== AUDIO_AAC2
)
2234 else if(stream
->wf
->wFormatTag
== AUDIO_MP2
|| stream
->wf
->wFormatTag
== AUDIO_MP3
)
2235 spriv
->is_ready
= 0;
2239 stream_format
= stream
->bih
->biCompression
;
2241 spriv
->max_buffer_size
= conf_vbuf_size
*1024;
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;
2251 spriv
->max_buffer_size
= 232*1024; //no profile => unconstrained :) FIXME!!!
2254 if(is_mpeg4(stream
->bih
->biCompression
))
2255 spriv
->is_ready
= 0;
2257 spriv
->is_ready
= 1;
2259 if(!is_mpeg1(stream_format
) && !is_mpeg2(stream_format
))
2263 if(priv
->is_genmpeg2
&& needs_psm
)
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
)
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
;
2280 uint32_t stream_format
, nf
;
2282 if(s
->buffer
== NULL
|| len
== -1)
2285 if (s
->type
== MUXER_TYPE_VIDEO
)
2286 { // try to recognize frame type...
2287 fps
= (float) s
->h
.dwRate
/ (float) s
->h
.dwScale
;
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;
2301 sz
= parse_mpeg12_video(s
, priv
, spriv
, fps
, len
);
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;
2314 sz
= parse_mpeg4_video(s
, priv
, spriv
, fps
, len
);
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
);
2326 { // MUXER_TYPE_AUDIO
2329 stream_format
= s
->wf
->wFormatTag
;
2331 if(s
->b_buffer_size
- s
->b_buffer_len
< len
)
2335 if(s
->b_buffer_len
> SIZE_MAX
- len
)
2337 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\nFATAL! couldn't realloc, integer overflow\n");
2340 tmp
= realloc(s
->b_buffer
, len
+ s
->b_buffer_len
);
2343 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\nFATAL! couldn't realloc %d bytes\n", len
+ s
->b_buffer_len
);
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;
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
)
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
)
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;
2417 static void setup_sys_params(muxer_priv_t
*priv
)
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;
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
;
2467 unsigned char ormask
[4] = {0x0, 0x2, 0x80, 0x82};
2469 dfl
= (target
- source
) << 1;
2473 for(i
= 0; i
< MAX_PATTERN_LENGTH
; i
++)
2479 bff_mask
[i
] = ormask
[trfp
+ 1];
2483 bff_mask
[i
] = ormask
[trfp
];
2487 int muxer_init_muxer_mpeg(muxer_t
*muxer
)
2490 priv
= (muxer_priv_t
*) calloc(1, sizeof(muxer_priv_t
));
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
;
2510 priv
->packet_size
= 2048;
2511 priv
->muxrate
= 10080 * 125;
2513 else if(! strcasecmp(conf_mux
, "xsvcd"))
2515 priv
->mux
= MUX_MPEG2
;
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
;
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
;
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
;
2541 priv
->packet_size
= 2048;
2542 priv
->muxrate
= 10080 * 125;
2543 priv
->ts_allframes
= 1;
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
;
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
)
2605 sfps
= (int) (conf_telecine_src
* 1001 + 0.5);
2606 tfps
= (int) (conf_telecine_dest
* 1001 + 0.5);
2607 if(sfps
% 2 || 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");
2620 generate_flags(priv
->bff_mask
, sfps
, tfps
);
2621 conf_telecine
= TELECINE_DGPULLDOWN
;
2622 conf_vframerate
= conf_telecine_dest
;
2630 fps
= (int) (conf_vframerate
* 1001 + 0.5);
2634 priv
->vframerate
= FRAMERATE_23976
;
2637 priv
->vframerate
= FRAMERATE_24
;
2640 priv
->vframerate
= FRAMERATE_25
;
2643 priv
->vframerate
= FRAMERATE_2997
;
2646 priv
->vframerate
= FRAMERATE_30
;
2649 priv
->vframerate
= FRAMERATE_50
;
2652 priv
->vframerate
= FRAMERATE_5994
;
2655 priv
->vframerate
= FRAMERATE_60
;
2659 mp_msg(MSGT_MUXER
, MSGL_ERR
, "WRONG FPS: %d/1000, ignoring\n", fps
);
2661 mp_msg(MSGT_MUXER
, MSGL_ERR
, "DISABLED TELECINING\n");
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
]);
2685 mp_msg(MSGT_MUXER
, MSGL_INFO
, " aspect ratio to %s", aspect_ratios
[priv
->vaspect
]);
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");
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
);
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
;