12 #include "aviheader.h"
15 #include "stream/stream.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
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
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;
98 uint64_t pts
, dts
, idur
;
99 uint32_t pos
; //start offset for the frame
103 uint8_t cnt
; // how many entries we use
108 } streams
[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
112 uint8_t cnt
; // how many entries we use
117 } streams
[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
127 uint64_t frame_dts
, frame_pts
;
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
;
141 uint64_t last_psm_scr
;
144 uint32_t headers_cnt
;
148 //video patching parameters
149 uint8_t vaspect
, vframerate
;
150 uint16_t vwidth
, vheight
, panscan_width
, panscan_height
;
152 int patch_seq
, patch_sde
;
155 //2 million frames are enough
156 #define MAX_PATTERN_LENGTH 2000000
157 uint8_t bff_mask
[MAX_PATTERN_LENGTH
];
162 int has_pts
, has_dts
, pes_is_aligned
, type
, min_pes_hlen
;
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
;
173 uint8_t id
, is_mpeg12
, telecine
;
175 int64_t display_frame
;
176 mp_mpeg_header_t picture
;
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
181 int pack_offset
, pes_offset
, pes_set
, payload_offset
;
183 int last_frame_rest
; //the rest of the previous frame
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
)
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
)
236 (x
== mmioFOURCC('m','p','g','1')) ||
237 (x
== mmioFOURCC('M','P','G','1'));
240 static inline int is_mpeg2(uint32_t x
)
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
)
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'));
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);
283 for(i
= 0; i
< size
; i
++)
284 crc
= (crc
<< 8) ^ CRCTab
[((crc
>> 24) ^ buff
[i
]) & 0xff];
290 static void add_to_psm(muxer_priv_t
*priv
, uint8_t id
, uint32_t format
)
294 i
= priv
->psm_info
.cnt
;
295 priv
->psm_info
.streams
[i
].id
= id
;
296 priv
->psm_info
.streams
[i
].format
= 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;
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
)
323 tmp
= (mpeg_frame_t
*) calloc(num
, sizeof(mpeg_frame_t
));
327 for(i
=0; i
< num
; i
++)
329 tmp
[i
].buffer
= (uint8_t *) calloc(1, size
);
330 if(tmp
[i
].buffer
== NULL
)
334 tmp
[i
].alloc_size
= size
;
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
;
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");
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");
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");
367 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Unknown stream type!\n");
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
)))
374 s
->b_buffer_size
= priv
->packet_size
;
377 s
->priv
= (muxer_headers_t
*) calloc(1, sizeof(muxer_headers_t
));
380 spriv
= (muxer_headers_t
*) s
->priv
;
381 spriv
->pack
= malloc(priv
->packet_size
);
384 spriv
->buffer_track
= calloc(1, 4096*sizeof(buffer_track_t
));
385 if(!spriv
->buffer_track
)
387 spriv
->track_pos
= 0;
388 spriv
->track_len
= 4096;
389 muxer
->streams
[muxer
->avih
.dwStreams
]=s
;
391 s
->id
=muxer
->avih
.dwStreams
;
394 if (type
== MUXER_TYPE_VIDEO
) {
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
++;
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");
424 memset(&(spriv
->picture
), 0, sizeof(spriv
->picture
));
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
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
++;
451 spriv
->min_pes_hlen
= 13;
452 else if(priv
->is_xsvcd
)
453 spriv
->min_pes_hlen
= 17;
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");
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
++;
480 if(spriv
->buffer_track
)
481 free(spriv
->buffer_track
);
491 static void write_mpeg_ts(unsigned char *b
, uint64_t ts
, uint8_t mod
) {
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;
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);
531 b
[0] = ((size
>> 8) & 0x3f) | (type
==1 ? 0x60 : 0x40) | mod
;
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;
540 ts
&= 0x1FFFFFFFFULL
; //33 bits
541 t1
= (ts
>> 30) & 0x7;
542 t2
= (ts
>> 15) & 0x7fff;
545 b
[0] = (t1
<< 3 ) | 0x44 | ((t2
>> 13) & 0x3);
547 b
[2] = (t2
& 0x1f) << 3 | 0x4 | ((t3
>> 13) & 0x3);
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
)
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
);
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
579 static int write_mpeg_system_header(muxer_t
*muxer
, char *buff
)
584 priv
= (muxer_priv_t
*) muxer
->priv
;
587 *(uint32_t *)(&buff
[len
]) = be2me_32(SYSTEM_HEADER_START_CODE
);
589 *(uint16_t *)(&buff
[len
]) = 0; //fake length, we'll fix it later
591 write_mpeg_rate(MUX_MPEG1
, &buff
[len
], muxer
->sysrate
);
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
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));
611 *(uint16_t *)(&buff
[4]) = be2me_16(len
- 6); // length field fixed
616 static int write_mpeg_psm(muxer_t
*muxer
, char *buff
)
622 priv
= (muxer_priv_t
*) muxer
->priv
;
625 *(uint32_t *)(&buff
[len
]) = be2me_32(PSM_START_CODE
);
627 *(uint16_t *)(&buff
[len
]) = 0; //fake length, we'll fix it later
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)
633 *(uint16_t *)(&buff
[len
]) = 0; //length of the es descriptors
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
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
));
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
)
674 memcpy(&buff
[len
], pes_id
, 4);
677 buff
[len
] = buff
[len
+1] = 0; //fake len
680 if(mux_type
== MUX_MPEG1
)
684 memset(&buff
[len
], 0xff, stuffing_len
);
688 if(h
->buffer_size
> 0)
690 write_mpeg_std(&buff
[len
], h
->buffer_size
, h
->type
, 0x40); // 01 is pes1 format
696 buff
[len
] = (h
->pes_is_aligned
? 0x84 : 0x80); //0x10...
698 buff
[len
] = ((h
->buffer_size
> 0) ? 1 : 0) | (h
->pts
? (h
->dts
? 0xC0 : 0x80) : 0); //pes extension + pts/dts flags
700 buff
[len
] = (h
->pts
? (h
->dts
? 10 : 5) : 0) + ((h
->buffer_size
> 0) ? 3 : 0) + stuffing_len
;//pts + std + stuffing
707 write_mpeg_ts(&buff
[len
], h
->pts
, (h
->dts
? 0x30 : 0x20)); // 001x and both PTS/DTS
712 write_mpeg_ts(&buff
[len
], h
->dts
, 0x10); // 0001 before DTS
718 if(mux_type
== MUX_MPEG1
)
726 if(mux_type
== MUX_MPEG2
)
728 if(h
->buffer_size
> 0)
730 buff
[len
] = 0x1e; //std flag
733 write_mpeg_std(&buff
[len
], h
->buffer_size
, h
->type
, 0x40);
739 memset(&buff
[len
], 0xff, stuffing_len
);
744 *((uint16_t*) &buff
[4]) = be2me_16(len
+ plen
- 6); //fix pes packet size
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;
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
765 mp_msg(MSGT_MUXER
, MSGL_DBG3
, "NAV\n");
767 *(uint32_t *)(&buff
[len
]) = be2me_32(PES_PRIVATE2
);
771 memset(&buff
[len
], 0, 0x03d4);
774 *(uint32_t *)(&buff
[len
]) = be2me_32(PES_PRIVATE2
);
778 memset(&buff
[len
], 0, 0x03fa);
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
)
793 if(format
== MUX_MPEG1
)
804 else if(format
== MUX_MPEG1
)
807 if(h
->buffer_size
> 0)
809 if(format
== MUX_MPEG2
)
815 //len = max(h->min_pes_hlen, len);
821 static int write_mpeg_pack(muxer_t
*muxer
, muxer_stream_t
*s
, stream_t
*stream
, int isoend
)
828 priv
= (muxer_priv_t
*) muxer
->priv
;
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
);
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;
850 offset
+= write_nav_pack(&buff
[offset
]);
852 stuffing_len
= priv
->packet_size
- offset
;
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
;
863 muxer
->movi_end
+= 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
);
878 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\r\nERROR, couldn't realloc %d bytes for tracking buffer\r\n", dim
);
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
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
;
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
905 spriv
->pack
[x
+2] = spriv
->pack
[x
+3] = 0;
908 static inline void remove_frames(muxer_headers_t
*spriv
, int n
)
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
;
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
)
933 m
= FFMIN(spriv
->framebuf
[n
].size
- frpos
, psize
- len
);
936 if(frpos
== spriv
->framebuf
[n
].size
)
943 if(len
< psize
&& !finalize
)
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
)
959 spriv
->dts
= spriv
->pts
= 0;
961 if(spriv
->framebuf
[0].pos
== 0) // start of frame
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
976 if(spriv
->framebuf_used
< 2)
979 if(spriv
->framebuf
[1].pts
== spriv
->framebuf
[1].dts
)
984 //headers+frame 0 < space available including timestamps
985 if(start
+ pes_hlen
+ m
< priv
->packet_size
- threshold
)
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
)
1000 else if((spriv
->framebuf
[i
].type
== I_FRAME
|| priv
->ts_allframes
|| dpts
>= 36000*300)) //0.4 seconds
1005 *pts
= spriv
->framebuf
[i
].pts
;
1006 *dts
= spriv
->framebuf
[i
].dts
;
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
;
1024 spriv
->pts
= spriv
->dts
= 0;
1025 p
->dts
= p
->pts
= p
->frame_pts
= p
->frame_dts
= 0;
1030 else if(priv
->mux
== MUX_MPEG1
)
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
;
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
)
1058 len2
= calc_packet_len(s
, target
, finalize
);
1059 if(!len2
|| (len2
< target
&& s
->type
== MUXER_TYPE_AUDIO
&& !finalize
))
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);
1072 if(s
->type
== MUXER_TYPE_VIDEO
)
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
;
1093 stflen
+= stuffing_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;
1120 spriv
->dts
= spriv
->pts
= 0;
1122 if(! spriv
->framebuf_used
)
1124 spriv
->pack_offset
= 0;
1128 if(!spriv
->pack_offset
)
1131 spriv
->pack_offset
= 0;
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
]);
1147 spriv
->pes_offset
= spriv
->pack_offset
;
1148 spriv
->payload_offset
= 0;
1150 spriv
->last_frame_rest
= 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));
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)
1172 if(! get_packet_stats(priv
, s
, &p
, finalize
))
1174 spriv
->pack_offset
= 0;
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;
1192 spriv
->last_frame_rest
= spriv
->framebuf
[0].size
- spriv
->framebuf
[0].pos
;
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);
1210 frm
= spriv
->framebuf
;
1211 while(spriv
->pack_offset
< priv
->packet_size
&& n
< spriv
->framebuf_used
)
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
)
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
);
1229 spriv
->pack_offset
+= m
;
1232 if(frm
->pos
== frm
->size
) //end of frame
1234 frm
->pos
= frm
->size
= 0;
1235 frm
->pts
= frm
->dts
= 0;
1241 if((priv
->is_xsvcd
|| priv
->is_xvcd
|| priv
->rawpes
) && spriv
->size
== 0)
1242 spriv
->buffer_size
= 0;
1246 if(dvd_pack
&& (spriv
->pack_offset
== priv
->packet_size
))
1247 write_mpeg_pack(muxer
, NULL
, muxer
->stream
, 0); //insert fake Nav Packet
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
)
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;
1276 priv
->last_psm_scr
= priv
->scr
;
1281 static inline int find_best_stream(muxer_t
*muxer
)
1285 muxer_priv_t
*priv
= muxer
->priv
;
1286 muxer_headers_t
*spriv
;
1288 unsigned int perc
, sperc
;
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
;
1299 get_packet_stats(priv
, muxer
->streams
[i
], &p
, 0);
1301 if(spriv
->track_bufsize
+ p
.len
> spriv
->max_buffer_size
)
1303 if(p
.frame_pts
&& p
.frame_dts
> priv
->scr
+ 63000*300)
1306 if(spriv
->framebuf
[0].dts
<= dts
)
1308 dts
= spriv
->framebuf
[0].dts
;
1312 if(conf_interleaving2
)
1314 sperc
= (spriv
->track_bufsize
* 1024) / spriv
->max_buffer_size
;
1326 static void patch_seq(muxer_priv_t
*priv
, unsigned char *buf
)
1328 if(priv
->vwidth
> 0)
1330 buf
[4] = (priv
->vwidth
>> 4) & 0xff;
1332 buf
[5] |= (priv
->vwidth
& 0x0f) << 4;
1335 if(priv
->vheight
> 0)
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)
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);
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
;
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
;
1407 while(j
< spriv
->track_pos
&& priv
->scr
>= spriv
->buffer_track
[j
].dts
)
1409 spriv
->track_bufsize
-= spriv
->buffer_track
[j
].size
;
1412 if(spriv
->track_bufsize
< 0)
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;
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
)
1440 if(vpriv
->framebuf_used
> 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
)
1464 if(found
&& (n
< vpriv
->framebuf_used
+1))
1470 static int flush_buffers(muxer_t
*muxer
, int finalize
)
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
;
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
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
)
1498 n
= found
= calc_frames_to_flush(vpriv
);
1500 else if(s
->type
== MUXER_TYPE_AUDIO
)
1504 if((! found
) && finalize
)
1507 found
= n
= vpriv
->framebuf_used
;
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
;
1518 for(i
= 0; i
< n
; i
++)
1519 iduration
+= vpriv
->framebuf
[i
].idur
;
1520 duration
= (double) (iduration
/ 27000000.0);
1524 apriv
= (muxer_headers_t
*) as
->priv
;
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");
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;
1559 i
= find_best_stream(muxer
);
1562 if(!fill_packet(muxer
, muxer
->streams
[i
], finalize
))
1565 if(skip_cnt
== muxer
->avih
.dwStreams
)
1573 muxer
->file_end
= priv
->scr
;
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
)
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;
1605 vpriv
->picture
.progressive_sequence
= 0;
1606 vpriv
->picture
.progressive_frame
= 1;
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
);
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
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
);
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
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
);
1666 patch_seq(priv
, &(s
->buffer
[i
]));
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))
1682 patch_panscan(priv
, &(s
->buffer
[i
+4]));
1684 if((s
->buffer
[i
+4] & 0xf0) == 0x80)
1686 pce_ptr
= &(s
->buffer
[i
+4]);
1700 pt
= (s
->buffer
[i
+5] & 0x1c) >> 3;
1701 temp_ref
= (s
->buffer
[i
+4]<<2)+(s
->buffer
[i
+5]>>6);
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
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
);
1716 spriv
->last_tr
= spriv
->max_tr
= temp_ref
;
1717 d1
= temp_ref
- spriv
->last_tr
;
1719 frames_diff
= spriv
->max_tr
+ 1 + temp_ref
- spriv
->last_tr
;
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
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
)
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
);
1759 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\r\nPARSE_MPEG12: add_frames(%d) failed, exit\r\n", len
);
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
);
1769 int64_t diff
= spriv
->last_dts
- spriv
->last_pts
;
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
;
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
;
1790 mp_msg(MSGT_MUXER
, MSGL_DBG2
,"parse_mpeg12_video, return %u\n", (uint32_t) len
);
1795 static uint64_t fix_mp4_frame_duration(muxer_headers_t
*vpriv
)
1797 uint64_t mn
, md
, mx
, diff
;
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
;
1812 if((vpriv
->framebuf
[i
].pts
> mn
) && (vpriv
->framebuf
[i
].pts
< mx
))
1813 md
= vpriv
->framebuf
[i
].pts
;
1816 if(mx
- md
> md
- mn
)
1821 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, "MIN: %"PRIu64
", mid: %"PRIu64
", max: %"PRIu64
", diff: %"PRIu64
"\n", mn
, md
, mx
, diff
);
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));
1838 static size_t parse_mpeg4_video(muxer_stream_t
*s
, muxer_priv_t
*priv
, muxer_headers_t
*vpriv
, float fps
, size_t len
)
1841 int64_t delta_pts
=0;
1845 mp_msg(MSGT_MUXER
, MSGL_DBG2
,"parse_mpeg4_video, len=%u\n", (uint32_t) len
);
1848 mp_msg(MSGT_MUXER
, MSGL_ERR
,"Frame too short: %d, exit!\n", len
);
1853 while(ptr
< len
- 5)
1855 if(s
->buffer
[ptr
] != 0 || s
->buffer
[ptr
+1] != 0 || s
->buffer
[ptr
+2] != 1)
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
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
;
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
);
1905 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\r\nPARSE_MPEG4: add_frames(%d) failed, exit\r\n", len
);
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));
1932 static int fill_last_frame(muxer_headers_t
*spriv
, uint8_t *ptr
, int len
)
1939 if(spriv
->framebuf_used
== 0)
1940 idx
= spriv
->framebuf_used
;
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
)
1948 spriv
->framebuf
[idx
].buffer
= (uint8_t*) realloc(spriv
->framebuf
[idx
].buffer
, spriv
->framebuf
[idx
].size
+ len
);
1949 if(! spriv
->framebuf
[idx
].buffer
)
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
;
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
)
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");
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");
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");
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");
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
);
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
))
2039 for(i
= 1; i
<= 3; 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
;
2057 unsigned int frames
;
2060 muxer_headers_t
*spriv
= (muxer_headers_t
*) s
->priv
;
2062 i
= tot
= frames
= 0;
2067 switch(s
->wf
->wFormatTag
)
2072 if(i
+ 3 >= s
->b_buffer_len
)
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
;
2095 if(i
+ 6 >= s
->b_buffer_len
)
2101 if(s
->b_buffer
[i
] == 0x0B && s
->b_buffer
[i
+1] == 0x77)
2104 #ifdef CONFIG_LIBA52
2105 len
= a52_syncinfo(&(s
->b_buffer
[i
]), &dummy
, &srate
, &dummy
);
2107 len
= mp_a52_framesize(&(s
->b_buffer
[i
]), &srate
);
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
;
2123 if(i
+ 7 >= s
->b_buffer_len
)
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
;
2152 spriv
->timer
+= dur
;
2153 if(spriv
->drop_delayed_frames
&& delay
< 0 && spriv
->timer
<= -delay
)
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
);
2165 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't add audio frame buffer(frame), abort\n");
2168 for(j
= frm_idx
; j
< spriv
->framebuf_cnt
; j
++)
2169 spriv
->framebuf
[j
].pts
= spriv
->last_pts
;
2170 spriv
->last_pts
+= idur
;
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;
2189 frm_idx
= add_frame(spriv
, 0, s
->b_buffer
, s
->b_buffer_len
, 0, spriv
->last_pts
, spriv
->last_pts
);
2192 for(j
= frm_idx
; j
< spriv
->framebuf_cnt
; j
++)
2193 spriv
->framebuf
[j
].pts
= spriv
->last_pts
;
2198 *timer
= spriv
->timer
;
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
;
2211 if(stream
->type
== MUXER_TYPE_AUDIO
)
2213 stream_format
= stream
->wf
->wFormatTag
;
2214 spriv
->is_ready
= 1;
2216 spriv
->max_buffer_size
= conf_abuf_size
*1024;
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
2226 spriv
->max_buffer_size
= 16*1024;
2228 else if(stream
->wf
->wFormatTag
== AUDIO_AAC1
|| stream
->wf
->wFormatTag
== AUDIO_AAC2
)
2230 else if(stream
->wf
->wFormatTag
== AUDIO_MP2
|| stream
->wf
->wFormatTag
== AUDIO_MP3
)
2231 spriv
->is_ready
= 0;
2235 stream_format
= stream
->bih
->biCompression
;
2237 spriv
->max_buffer_size
= conf_vbuf_size
*1024;
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;
2247 spriv
->max_buffer_size
= 232*1024; //no profile => unconstrained :) FIXME!!!
2250 if(is_mpeg4(stream
->bih
->biCompression
))
2251 spriv
->is_ready
= 0;
2253 spriv
->is_ready
= 1;
2255 if(!is_mpeg1(stream_format
) && !is_mpeg2(stream_format
))
2259 if(priv
->is_genmpeg2
&& needs_psm
)
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
)
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
;
2276 uint32_t stream_format
, nf
;
2278 if(s
->buffer
== NULL
|| len
== -1)
2281 if (s
->type
== MUXER_TYPE_VIDEO
)
2282 { // try to recognize frame type...
2283 fps
= (float) s
->h
.dwRate
/ (float) s
->h
.dwScale
;
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;
2297 sz
= parse_mpeg12_video(s
, priv
, spriv
, fps
, len
);
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;
2310 sz
= parse_mpeg4_video(s
, priv
, spriv
, fps
, len
);
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
);
2322 { // MUXER_TYPE_AUDIO
2325 stream_format
= s
->wf
->wFormatTag
;
2327 if(s
->b_buffer_size
- s
->b_buffer_len
< len
)
2331 if(s
->b_buffer_len
> SIZE_MAX
- len
)
2333 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\nFATAL! couldn't realloc, integer overflow\n");
2336 tmp
= realloc(s
->b_buffer
, len
+ s
->b_buffer_len
);
2339 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\nFATAL! couldn't realloc %d bytes\n", len
+ s
->b_buffer_len
);
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;
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
)
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
)
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;
2413 static void setup_sys_params(muxer_priv_t
*priv
)
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;
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
;
2463 unsigned char ormask
[4] = {0x0, 0x2, 0x80, 0x82};
2465 dfl
= (target
- source
) << 1;
2469 for(i
= 0; i
< MAX_PATTERN_LENGTH
; i
++)
2475 bff_mask
[i
] = ormask
[trfp
+ 1];
2479 bff_mask
[i
] = ormask
[trfp
];
2483 int muxer_init_muxer_mpeg(muxer_t
*muxer
)
2486 priv
= (muxer_priv_t
*) calloc(1, sizeof(muxer_priv_t
));
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
;
2506 priv
->packet_size
= 2048;
2507 priv
->muxrate
= 10080 * 125;
2509 else if(! strcasecmp(conf_mux
, "xsvcd"))
2511 priv
->mux
= MUX_MPEG2
;
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
;
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
;
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
;
2537 priv
->packet_size
= 2048;
2538 priv
->muxrate
= 10080 * 125;
2539 priv
->ts_allframes
= 1;
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
;
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
)
2601 sfps
= (int) (conf_telecine_src
* 1001 + 0.5);
2602 tfps
= (int) (conf_telecine_dest
* 1001 + 0.5);
2603 if(sfps
% 2 || 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");
2616 generate_flags(priv
->bff_mask
, sfps
, tfps
);
2617 conf_telecine
= TELECINE_DGPULLDOWN
;
2618 conf_vframerate
= conf_telecine_dest
;
2626 fps
= (int) (conf_vframerate
* 1001 + 0.5);
2630 priv
->vframerate
= FRAMERATE_23976
;
2633 priv
->vframerate
= FRAMERATE_24
;
2636 priv
->vframerate
= FRAMERATE_25
;
2639 priv
->vframerate
= FRAMERATE_2997
;
2642 priv
->vframerate
= FRAMERATE_30
;
2645 priv
->vframerate
= FRAMERATE_50
;
2648 priv
->vframerate
= FRAMERATE_5994
;
2651 priv
->vframerate
= FRAMERATE_60
;
2655 mp_msg(MSGT_MUXER
, MSGL_ERR
, "WRONG FPS: %d/1000, ignoring\n", fps
);
2657 mp_msg(MSGT_MUXER
, MSGL_ERR
, "DISABLED TELECINING\n");
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
]);
2681 mp_msg(MSGT_MUXER
, MSGL_INFO
, " aspect ratio to %s", aspect_ratios
[priv
->vaspect
]);
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");
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
);
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
;