2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <sys/types.h>
29 #include "aviheader.h"
32 #include "stream/stream.h"
39 #include "liba52/a52.h"
41 #define PACK_HEADER_START_CODE 0x01ba
42 #define SYSTEM_HEADER_START_CODE 0x01bb
43 #define PSM_START_CODE 0x01bc
45 #define PES_PRIVATE1 0x01bd
46 #define PES_PRIVATE2 0x01bf
51 #define VIDEO_MPEG1 0x10000001
52 #define VIDEO_MPEG2 0x10000002
53 #define AUDIO_MP2 0x50
54 #define AUDIO_MP3 0x55
55 #define AUDIO_A52 0x2000
56 #define AUDIO_LPCM 0x10001 /* only a placeholder at the moment */
57 #define AUDIO_AAC1 0x706D
58 //#define AUDIO_AAC2 (short) mmioFOURCC('m','p','4','a')
59 #define AUDIO_AAC2 0x504D
64 #define ASPECT_2_21_1 4
66 #define FRAMERATE_23976 1
67 #define FRAMERATE_24 2
68 #define FRAMERATE_25 3
69 #define FRAMERATE_2997 4
70 #define FRAMERATE_30 5
71 #define FRAMERATE_50 6
72 #define FRAMERATE_5994 7
73 #define FRAMERATE_60 8
75 static char ftypes
[] = {'?', 'I', 'P', 'B'};
76 #define FTYPE(x) (ftypes[(x)])
78 static const char *framerates
[] = {
79 "unchanged", "23.976", "24", "25", "29.97", "30", "50", "59.94", "60"
82 static const char *aspect_ratios
[] = {
83 "unchanged", "1/1", "4/3", "16/9", "2.21/1"
86 static char *conf_mux
= "mpeg2";
87 static uint16_t conf_packet_size
= 0; //dvd
88 static uint32_t conf_muxrate
= 0; //kb/s
89 static float conf_vaspect
= 0;
90 static float conf_vframerate
= 0;
91 static uint32_t conf_vwidth
= 0, conf_vheight
= 0, conf_panscan_width
= 0, conf_panscan_height
= 0;
92 static uint32_t conf_vbitrate
= 0;
93 static int conf_init_vpts
= 200, conf_init_apts
= 200;
94 static int conf_ts_allframes
= 0;
95 static int conf_init_adelay
= 0, conf_init_vdelay
= 0;
96 static int conf_abuf_size
= 0;
97 static int conf_vbuf_size
= 0;
98 static int conf_drop
= 0;
99 static int conf_telecine
= 0;
100 static int conf_interleaving2
= 0;
101 static float conf_telecine_src
= 0;
102 static float conf_telecine_dest
= 0;
115 uint64_t pts
, dts
, idur
;
116 uint32_t pos
; //start offset for the frame
120 uint8_t cnt
; // how many entries we use
125 } streams
[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
129 uint8_t cnt
; // how many entries we use
134 } streams
[50]; //16 video + 16 audio mpa + 16 audio private + bd/bf for dvd
144 uint64_t frame_dts
, frame_pts
;
152 uint16_t packet_size
;
153 int is_dvd
, is_xvcd
, is_xsvcd
, is_genmpeg1
, is_genmpeg2
, rawpes
, ts_allframes
, has_video
, has_audio
;
154 int update_system_header
, use_psm
;
155 off_t headers_size
, data_size
;
158 uint64_t last_psm_scr
;
161 uint32_t headers_cnt
;
165 //video patching parameters
166 uint8_t vaspect
, vframerate
;
167 uint16_t vwidth
, vheight
, panscan_width
, panscan_height
;
169 int patch_seq
, patch_sde
;
172 //2 million frames are enough
173 #define MAX_PATTERN_LENGTH 2000000
174 uint8_t bff_mask
[MAX_PATTERN_LENGTH
];
179 int has_pts
, has_dts
, pes_is_aligned
, type
, min_pes_hlen
;
181 uint64_t pts
, last_pts
, last_dts
, dts
, size
, frame_duration
, delta_pts
, nom_delta_pts
, last_saved_pts
;
182 uint32_t buffer_size
;
183 double delta_clock
, timer
;
184 int drop_delayed_frames
;
185 mpeg_frame_t
*framebuf
;
186 uint16_t framebuf_cnt
;
187 uint16_t framebuf_used
;
190 uint8_t id
, is_mpeg12
, telecine
;
192 int64_t display_frame
;
193 mp_mpeg_header_t picture
;
195 buffer_track_t
*buffer_track
;
196 int track_pos
, track_len
, track_bufsize
; //pos and len control the array, bufsize is the size of the buffer
198 int pack_offset
, pes_offset
, pes_set
, payload_offset
;
200 int last_frame_rest
; //the rest of the previous frame
206 #define TELECINE_FILM2PAL 2
207 #define TELECINE_DGPULLDOWN 3
209 m_option_t mpegopts_conf
[] = {
210 {"format", &(conf_mux
), CONF_TYPE_STRING
, M_OPT_GLOBAL
, 0 ,0, NULL
},
211 {"size", &(conf_packet_size
), CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 0, 65535, NULL
},
212 {"muxrate", &(conf_muxrate
), CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 0, 12000000, NULL
}, //12 Mb/s
213 {"vaspect", &(conf_vaspect
), CONF_TYPE_FLOAT
, M_OPT_GLOBAL
, 0, 0, NULL
},
214 {"vframerate", &(conf_vframerate
), CONF_TYPE_FLOAT
, M_OPT_GLOBAL
, 0, 0, NULL
},
215 {"vwidth", &(conf_vwidth
), CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 1, 4095, NULL
},
216 {"vheight", &(conf_vheight
), CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 1, 4095, NULL
},
217 {"vpswidth", &(conf_panscan_width
), CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 1, 16383, NULL
},
218 {"vpsheight", &(conf_panscan_height
), CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 1, 16383, NULL
},
219 {"vbitrate", &(conf_vbitrate
), CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 1, 104857599, NULL
},
220 {"vdelay", &conf_init_vdelay
, CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 0, 32760, NULL
},
221 {"adelay", &conf_init_adelay
, CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 0, 32760, NULL
},
222 {"vbuf_size", &conf_vbuf_size
, CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 40, 1194, NULL
},
223 {"abuf_size", &conf_abuf_size
, CONF_TYPE_INT
, M_OPT_GLOBAL
|M_OPT_RANGE
, 4, 64, NULL
},
224 {"drop", &conf_drop
, CONF_TYPE_FLAG
, M_OPT_GLOBAL
, 0, 1, NULL
},
225 {"tsaf", &conf_ts_allframes
, CONF_TYPE_FLAG
, M_OPT_GLOBAL
, 0, 1, NULL
},
226 {"telecine", &conf_telecine
, CONF_TYPE_FLAG
, M_OPT_GLOBAL
, 0, PULLDOWN32
, NULL
},
227 {"interleaving2", &conf_interleaving2
, CONF_TYPE_FLAG
, M_OPT_GLOBAL
, 0, 1, NULL
},
228 {"film2pal", &conf_telecine
, CONF_TYPE_FLAG
, M_OPT_GLOBAL
, 0, TELECINE_FILM2PAL
, NULL
},
229 {"tele_src", &(conf_telecine_src
), CONF_TYPE_FLOAT
, M_OPT_GLOBAL
, 0, 0, NULL
},
230 {"tele_dest", &(conf_telecine_dest
), CONF_TYPE_FLOAT
, M_OPT_GLOBAL
, 0, 0, NULL
},
231 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
234 int mp_a52_framesize(uint8_t *buf
, int *srate
);
236 static void fix_audio_sys_header(muxer_priv_t
*priv
, uint8_t id
, uint8_t newid
, uint32_t size
)
240 for(i
= 0; i
< priv
->sys_info
.cnt
; i
++)
242 if(priv
->sys_info
.streams
[i
].id
== id
)
244 priv
->sys_info
.streams
[i
].id
= newid
;
245 priv
->sys_info
.streams
[i
].type
= 1;
246 priv
->sys_info
.streams
[i
].bufsize
= size
;
251 static inline int is_mpeg1(uint32_t x
)
255 (x
== mmioFOURCC('m','p','g','1')) ||
256 (x
== mmioFOURCC('M','P','G','1'));
259 static inline int is_mpeg2(uint32_t x
)
263 (x
== mmioFOURCC('m','p','g','2')) ||
264 (x
== mmioFOURCC('M','P','G','2')) ||
265 (x
== mmioFOURCC('m','p','e','g')) ||
266 (x
== mmioFOURCC('M','P','E','G'));
269 static inline int is_mpeg4(uint32_t x
)
273 (x
== mmioFOURCC('d','i','v','x')) ||
274 (x
== mmioFOURCC('D','I','V','X')) ||
275 (x
== mmioFOURCC('x','v','i','d')) ||
276 (x
== mmioFOURCC('X','V','I','D')) ||
277 (x
== mmioFOURCC('X','v','i','D')) ||
278 (x
== mmioFOURCC('x','v','i','x')) ||
279 (x
== mmioFOURCC('X','V','I','X')) ||
280 (x
== mmioFOURCC('m','p','4','v')) ||
281 (x
== mmioFOURCC('M','P','4','V')) ||
282 (x
== mmioFOURCC('F', 'M','P','4')) ||
283 (x
== mmioFOURCC('f', 'm','p','4')) ||
284 (x
== mmioFOURCC('D', 'X','5','0')) ||
285 (x
== mmioFOURCC('d', 'x','5','0'));
289 static uint32_t CalcCRC32(uint8_t *buff
, uint32_t size
)
291 uint32_t i
, j
, CRCTab
[256], crc
;
293 for(i
= 0;i
< 256; i
++)
295 for(crc
= i
, j
= 0; j
< 8; j
++)
296 crc
= (crc
& 1) ? (crc
>> 1)^0xEDB88320L
: (crc
>> 1);
302 for(i
= 0; i
< size
; i
++)
303 crc
= (crc
<< 8) ^ CRCTab
[((crc
>> 24) ^ buff
[i
]) & 0xff];
309 static void add_to_psm(muxer_priv_t
*priv
, uint8_t id
, uint32_t format
)
313 i
= priv
->psm_info
.cnt
;
314 priv
->psm_info
.streams
[i
].id
= id
;
315 priv
->psm_info
.streams
[i
].format
= format
;
318 priv
->psm_info
.streams
[i
].type
= 0x01;
319 else if(is_mpeg2(format
))
320 priv
->psm_info
.streams
[i
].type
= 0x02;
321 else if(is_mpeg4(format
))
322 priv
->psm_info
.streams
[i
].type
= 0x10;
323 else if(format
== AUDIO_MP2
|| format
== AUDIO_MP3
)
324 priv
->psm_info
.streams
[i
].type
= 0x03;
325 else if(format
== AUDIO_AAC1
|| format
== AUDIO_AAC2
)
326 priv
->psm_info
.streams
[i
].type
= 0x0f;
328 priv
->psm_info
.streams
[i
].type
= 0x81;
330 if(format
== AUDIO_A52
)
331 memcpy((char*) &(priv
->psm_info
.streams
[i
].format
), "AC-3", 4);
333 priv
->psm_info
.cnt
++;
337 static mpeg_frame_t
*init_frames(uint16_t num
, size_t size
)
342 tmp
= (mpeg_frame_t
*) calloc(num
, sizeof(mpeg_frame_t
));
346 for(i
=0; i
< num
; i
++)
348 tmp
[i
].buffer
= (uint8_t *) calloc(1, size
);
349 if(tmp
[i
].buffer
== NULL
)
353 tmp
[i
].alloc_size
= size
;
360 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
);
362 static muxer_stream_t
* mpegfile_new_stream(muxer_t
*muxer
,int type
){
363 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
365 muxer_headers_t
*spriv
;
367 if (!muxer
) return NULL
;
368 if(muxer
->avih
.dwStreams
>=MUXER_MAX_STREAMS
){
369 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Too many streams! increase MUXER_MAX_STREAMS !\n");
373 case MUXER_TYPE_VIDEO
:
374 if (muxer
->num_videos
>= 16) {
375 mp_msg(MSGT_MUXER
, MSGL_ERR
, "MPEG files can't contain more than 16 video streams!\n");
379 case MUXER_TYPE_AUDIO
:
380 if (muxer
->num_audios
>= 16) {
381 mp_msg(MSGT_MUXER
, MSGL_ERR
, "MPEG files can't contain more than 16 audio streams!\n");
386 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Unknown stream type!\n");
389 s
= (muxer_stream_t
*) calloc(1, sizeof(muxer_stream_t
));
390 if(!s
) return NULL
; // no mem!?
391 if (!(s
->b_buffer
= malloc(priv
->packet_size
)))
393 s
->b_buffer_size
= priv
->packet_size
;
396 s
->priv
= (muxer_headers_t
*) calloc(1, sizeof(muxer_headers_t
));
399 spriv
= (muxer_headers_t
*) s
->priv
;
400 spriv
->pack
= malloc(priv
->packet_size
);
403 spriv
->buffer_track
= calloc(1, 4096*sizeof(buffer_track_t
));
404 if(!spriv
->buffer_track
)
406 spriv
->track_pos
= 0;
407 spriv
->track_len
= 4096;
408 muxer
->streams
[muxer
->avih
.dwStreams
]=s
;
410 s
->id
=muxer
->avih
.dwStreams
;
413 if (type
== MUXER_TYPE_VIDEO
) {
415 spriv
->last_pts
= conf_init_vpts
* 90 * 300;
416 if(conf_init_vdelay
) {
417 spriv
->last_dts
+= conf_init_vdelay
* 90 * 300;
418 spriv
->last_pts
+= conf_init_vdelay
* 90 * 300;
420 spriv
->id
= 0xe0 + muxer
->num_videos
;
421 s
->ckid
= be2me_32 (0x100 + spriv
->id
);
422 if(priv
->is_genmpeg1
|| priv
->is_genmpeg2
) {
423 int v
= (conf_vbuf_size
? conf_vbuf_size
*1024 :
424 (s
->h
.dwSuggestedBufferSize
? s
->h
.dwSuggestedBufferSize
: 46*1024));
425 int n
= priv
->sys_info
.cnt
;
427 priv
->sys_info
.streams
[n
].id
= spriv
->id
;
428 priv
->sys_info
.streams
[n
].type
= 1;
429 priv
->sys_info
.streams
[n
].bufsize
= v
;
430 priv
->sys_info
.cnt
++;
434 s
->h
.fccType
=streamtypeVIDEO
;
435 if(!muxer
->def_v
) muxer
->def_v
=s
;
436 spriv
->framebuf_cnt
= 30;
437 spriv
->framebuf_used
= 0;
438 spriv
->framebuf
= init_frames(spriv
->framebuf_cnt
, (size_t) 5000);
439 if(spriv
->framebuf
== NULL
) {
440 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't allocate initial frames structure, abort!\n");
443 memset(&(spriv
->picture
), 0, sizeof(spriv
->picture
));
445 spriv
->min_pes_hlen
= 18;
446 else if(priv
->is_xsvcd
)
447 spriv
->min_pes_hlen
= 22;
448 spriv
->telecine
= conf_telecine
;
449 mp_msg (MSGT_MUXER
, MSGL_DBG2
, "Added video stream %d, ckid=%X\n", muxer
->num_videos
, s
->ckid
);
450 } else { // MUXER_TYPE_AUDIO
452 spriv
->drop_delayed_frames
= conf_drop
;
453 spriv
->last_pts
= conf_init_apts
* 90 * 300;
454 if(conf_init_adelay
&& ! spriv
->drop_delayed_frames
)
455 spriv
->last_pts
+= conf_init_adelay
* 90 * 300;
456 spriv
->pts
= spriv
->last_pts
;
457 spriv
->id
= 0xc0 + muxer
->num_audios
;
458 s
->ckid
= be2me_32 (0x100 + spriv
->id
);
459 if(priv
->is_genmpeg1
|| priv
->is_genmpeg2
) {
460 int a1
= (conf_abuf_size
? conf_abuf_size
*1024 :
461 (s
->h
.dwSuggestedBufferSize
? s
->h
.dwSuggestedBufferSize
: 4*1024));
462 int n
= priv
->sys_info
.cnt
;
464 priv
->sys_info
.streams
[n
].id
= spriv
->id
;
465 priv
->sys_info
.streams
[n
].type
= 0;
466 priv
->sys_info
.streams
[n
].bufsize
= a1
;
467 priv
->sys_info
.cnt
++;
470 spriv
->min_pes_hlen
= 13;
471 else if(priv
->is_xsvcd
)
472 spriv
->min_pes_hlen
= 17;
476 s
->h
.fccType
=streamtypeAUDIO
;
478 spriv
->framebuf_cnt
= 30;
479 spriv
->framebuf_used
= 0;
480 spriv
->framebuf
= init_frames(spriv
->framebuf_cnt
, (size_t) 2048);
481 if(spriv
->framebuf
== NULL
) {
482 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't allocate initial frames structure, abort!\n");
486 mp_msg (MSGT_MUXER
, MSGL_DBG2
, "Added audio stream %d, ckid=%X\n", s
->id
- muxer
->num_videos
+ 1, s
->ckid
);
488 muxer
->avih
.dwStreams
++;
499 if(spriv
->buffer_track
)
500 free(spriv
->buffer_track
);
510 static void write_mpeg_ts(unsigned char *b
, uint64_t ts
, uint8_t mod
) {
512 b
[0] = mod
| ((ts
>> 29) & 0xf) | 1;
513 b
[1] = (ts
>> 22) & 0xff;
514 b
[2] = ((ts
>> 14) & 0xff) | 1;
515 b
[3] = (ts
>> 7) & 0xff;
516 b
[4] = ((ts
<< 1) & 0xff) | 1;
520 static void write_mpeg_rate(int type
, unsigned char *b
, unsigned int rate
)
522 rate
= ((rate
*8)+399) / 400;
524 if(type
== MUX_MPEG1
)
526 b
[0] = ((rate
>> 15) & 0x7f) | 0x80;
527 b
[1] = (rate
>> 7) & 0xff;
528 b
[2] = ((rate
<< 1) & 0xff) | 1;
533 b
[1] = (rate
>> 6) & 0xff;
534 b
[2] = ((rate
& 0x3f) << 2) | 0x03;
539 static void write_mpeg_std(unsigned char *b
, unsigned int size
, unsigned int type
, uint8_t mod
)
541 //type = 0:mpeg audio/128, 1:video and pes private streams (including ac3/dts/lpcm)/1024
542 if(type
== 0) //audio
543 size
= (size
+ 127) / 128;
544 else //video or other
545 size
= ((size
+ 1023) / 1024);
550 b
[0] = ((size
>> 8) & 0x3f) | (type
==1 ? 0x60 : 0x40) | mod
;
554 static void write_mpeg2_scr(unsigned char *b
, uint64_t ts
)
556 uint16_t t1
, t2
, t3
, scr_ext
;
557 scr_ext
= ts
% 300ULL;
559 ts
&= 0x1FFFFFFFFULL
; //33 bits
560 t1
= (ts
>> 30) & 0x7;
561 t2
= (ts
>> 15) & 0x7fff;
564 b
[0] = (t1
<< 3 ) | 0x44 | ((t2
>> 13) & 0x3);
566 b
[2] = (t2
& 0x1f) << 3 | 0x4 | ((t3
>> 13) & 0x3);
568 b
[4] = (t3
& 0x1f) << 3 | ((scr_ext
>> 7) & 0x03) | 0x4;
569 b
[5] = ((scr_ext
<< 1) & 0xFF) | 1;
573 static int write_mpeg_pack_header(muxer_t
*muxer
, char *buff
)
578 priv
= (muxer_priv_t
*) muxer
->priv
;
579 *(uint32_t *)buff
= be2me_32(PACK_HEADER_START_CODE
);
580 if(priv
->mux
==MUX_MPEG1
)
582 write_mpeg_ts(&buff
[4], priv
->scr
, 0x20); // 0010 and SCR
583 write_mpeg_rate(priv
->mux
, &buff
[9], muxer
->sysrate
);
588 write_mpeg2_scr(&buff
[4], priv
->scr
); // 0010 and SCR
589 write_mpeg_rate(priv
->mux
, &buff
[10], muxer
->sysrate
);
590 buff
[13] = 0xf8; //5 bits reserved + 3 set to 0 to indicate 0 stuffing bytes
598 static int write_mpeg_system_header(muxer_t
*muxer
, char *buff
)
603 priv
= (muxer_priv_t
*) muxer
->priv
;
606 *(uint32_t *)(&buff
[len
]) = be2me_32(SYSTEM_HEADER_START_CODE
);
608 *(uint16_t *)(&buff
[len
]) = 0; //fake length, we'll fix it later
610 write_mpeg_rate(MUX_MPEG1
, &buff
[len
], muxer
->sysrate
);
613 buff
[len
++] = 0x4 | (priv
->is_xvcd
? 1 : 0); //1 audio stream bound, no fixed, CSPS only for xvcd
614 //stolen from libavformat
615 if(priv
->is_xvcd
|| priv
->is_dvd
)
616 buff
[len
++] = 0xe1; //system_audio_lock, system_video_lock, marker, 1 video stream bound
618 buff
[len
++] = 0x21; //marker, 1 video stream bound
620 buff
[len
++] = ((priv
->mux
== MUX_MPEG1
) ? 0xff : 0x7f); //in mpeg2 there's the packet rate restriction
622 for(i
= 0; i
< priv
->sys_info
.cnt
; i
++)
624 buff
[len
++] = priv
->sys_info
.streams
[i
].id
;
625 write_mpeg_std(&buff
[len
], priv
->sys_info
.streams
[i
].bufsize
, priv
->sys_info
.streams
[i
].type
,
626 (priv
->sys_info
.streams
[i
].type
== 1 ? 0xe0: 0xc0));
630 *(uint16_t *)(&buff
[4]) = be2me_16(len
- 6); // length field fixed
635 static int write_mpeg_psm(muxer_t
*muxer
, char *buff
)
641 priv
= (muxer_priv_t
*) muxer
->priv
;
644 *(uint32_t *)(&buff
[len
]) = be2me_32(PSM_START_CODE
);
646 *(uint16_t *)(&buff
[len
]) = 0; //fake length, we'll fix it later
648 buff
[len
++] = 0xe0; //1 current, 2 bits reserved, 5 version 0
649 buff
[len
++] = 0xff; //7 reserved, 1 marker
650 buff
[len
] = buff
[len
+1] = 0; //length of the program descriptors (unused)
652 *(uint16_t *)(&buff
[len
]) = 0; //length of the es descriptors
656 for(i
= 0; i
< priv
->psm_info
.cnt
; i
++)
659 (priv
->psm_info
.streams
[i
].id
== 0xbd) ||
660 (priv
->psm_info
.streams
[i
].id
>= 0xe0 && priv
->psm_info
.streams
[i
].id
<= 0xef) ||
661 (priv
->psm_info
.streams
[i
].id
>= 0xc0 && priv
->psm_info
.streams
[i
].id
<= 0xcf)
664 buff
[len
++] = priv
->psm_info
.streams
[i
].type
;
665 buff
[len
++] = priv
->psm_info
.streams
[i
].id
;
666 buff
[len
++] = 0; //len of descriptor upper ...
667 buff
[len
++] = 0; //... lower
672 *(uint16_t *)(&buff
[10]) = be2me_16(dlen
); //length of the es descriptors
674 *(uint16_t *)(&buff
[4]) = be2me_16(len
- 6 + 4); // length field fixed, including size of CRC32
676 *(uint32_t *)(&buff
[len
]) = be2me_32(CalcCRC32(buff
, len
));
683 static int psm_is_late(muxer_priv_t
*priv
)
685 return !priv
->data_size
|| (priv
->scr
>= priv
->last_psm_scr
+ 27000000ULL);
688 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
)
693 memcpy(&buff
[len
], pes_id
, 4);
696 buff
[len
] = buff
[len
+1] = 0; //fake len
699 if(mux_type
== MUX_MPEG1
)
703 memset(&buff
[len
], 0xff, stuffing_len
);
707 if(h
->buffer_size
> 0)
709 write_mpeg_std(&buff
[len
], h
->buffer_size
, h
->type
, 0x40); // 01 is pes1 format
715 buff
[len
] = (h
->pes_is_aligned
? 0x84 : 0x80); //0x10...
717 buff
[len
] = ((h
->buffer_size
> 0) ? 1 : 0) | (h
->pts
? (h
->dts
? 0xC0 : 0x80) : 0); //pes extension + pts/dts flags
719 buff
[len
] = (h
->pts
? (h
->dts
? 10 : 5) : 0) + ((h
->buffer_size
> 0) ? 3 : 0) + stuffing_len
;//pts + std + stuffing
726 write_mpeg_ts(&buff
[len
], h
->pts
, (h
->dts
? 0x30 : 0x20)); // 001x and both PTS/DTS
731 write_mpeg_ts(&buff
[len
], h
->dts
, 0x10); // 0001 before DTS
737 if(mux_type
== MUX_MPEG1
)
745 if(mux_type
== MUX_MPEG2
)
747 if(h
->buffer_size
> 0)
749 buff
[len
] = 0x1e; //std flag
752 write_mpeg_std(&buff
[len
], h
->buffer_size
, h
->type
, 0x40);
758 memset(&buff
[len
], 0xff, stuffing_len
);
763 *((uint16_t*) &buff
[4]) = be2me_16(len
+ plen
- 6); //fix pes packet size
768 static void write_pes_padding(uint8_t *buff
, uint16_t len
)
770 //6 header bytes + len-6 0xff chars
771 buff
[0] = buff
[1] = 0;
774 *((uint16_t*) &buff
[4]) = be2me_16(len
- 6);
775 memset(&buff
[6], 0xff, len
- 6);
779 static int write_nav_pack(uint8_t *buff
)
781 // concatenation of pes_private2 + 03d4 x 0 and pes_private2 + 03fa x 0
784 mp_msg(MSGT_MUXER
, MSGL_DBG3
, "NAV\n");
786 *(uint32_t *)(&buff
[len
]) = be2me_32(PES_PRIVATE2
);
790 memset(&buff
[len
], 0, 0x03d4);
793 *(uint32_t *)(&buff
[len
]) = be2me_32(PES_PRIVATE2
);
797 memset(&buff
[len
], 0, 0x03fa);
803 static unsigned int calc_psm_len(muxer_priv_t
*priv
)
805 return 16 + 4*(priv
->psm_info
.cnt
);
808 static uint32_t calc_pes_hlen(int format
, muxer_headers_t
*h
, muxer_priv_t
*priv
)
812 if(format
== MUX_MPEG1
)
823 else if(format
== MUX_MPEG1
)
826 if(h
->buffer_size
> 0)
828 if(format
== MUX_MPEG2
)
834 //len = max(h->min_pes_hlen, len);
840 static int write_mpeg_pack(muxer_t
*muxer
, muxer_stream_t
*s
, stream_t
*stream
, int isoend
)
847 priv
= (muxer_priv_t
*) muxer
->priv
;
852 offset
= priv
->packet_size
- 4;
853 write_pes_padding(buff
, offset
);
854 buff
[offset
+ 0] = buff
[offset
+ 1] = 0;
855 buff
[offset
+ 2] = 1;
856 buff
[offset
+ 3] = 0xb9;
858 stream_write_buffer(stream
, buff
, priv
->packet_size
);
861 else //FAKE DVD NAV PACK
863 offset
= write_mpeg_pack_header(muxer
, buff
);
864 offset
+= write_mpeg_system_header(muxer
, &buff
[offset
]);
866 //priv->update_system_header = 0;
869 offset
+= write_nav_pack(&buff
[offset
]);
871 stuffing_len
= priv
->packet_size
- offset
;
874 //insert a PES padding packet
875 write_pes_padding(&buff
[offset
], stuffing_len
);
876 offset
+= stuffing_len
;
879 stream_write_buffer(stream
, buff
, offset
);
880 priv
->headers_size
+= offset
;
882 muxer
->movi_end
+= tot
;
888 static void update_demux_bufsize(muxer_headers_t
*spriv
, uint64_t dts
, int framelen
, int type
)
890 int dim
= (spriv
->track_len
+16)*sizeof(buffer_track_t
);
892 if(spriv
->track_pos
+1 >= spriv
->track_len
)
894 buffer_track_t
*tmp
= realloc(spriv
->buffer_track
, dim
);
897 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\r\nERROR, couldn't realloc %d bytes for tracking buffer\r\n", dim
);
900 spriv
->buffer_track
= tmp
;
901 memset(&(spriv
->buffer_track
[spriv
->track_pos
+1]), 0, 16*sizeof(buffer_track_t
));
902 spriv
->track_len
+= 16;
905 spriv
->buffer_track
[spriv
->track_pos
].size
= framelen
;
906 spriv
->buffer_track
[spriv
->track_pos
].dts
= dts
; //must be dts
911 static void fix_a52_headers(muxer_stream_t
*s
)
913 muxer_headers_t
*spriv
= s
->priv
;
914 int x
= spriv
->payload_offset
;
916 spriv
->pack
[x
+0] = 0x80;
917 spriv
->pack
[x
+1] = spriv
->frames
;
920 spriv
->pack
[x
+2] = ((spriv
->last_frame_rest
+1) >> 8) & 0xff; //256 * 0 ...
921 spriv
->pack
[x
+3] = (spriv
->last_frame_rest
+1) & 0xff; // + 1 byte(s) to skip
924 spriv
->pack
[x
+2] = spriv
->pack
[x
+3] = 0;
927 static inline void remove_frames(muxer_headers_t
*spriv
, int n
)
932 for(i
= n
; i
< spriv
->framebuf_used
; i
++)
934 tmp
= spriv
->framebuf
[i
- n
];
935 spriv
->framebuf
[i
- n
] = spriv
->framebuf
[i
];
936 spriv
->framebuf
[i
] = tmp
;
938 spriv
->framebuf_used
-= n
;
941 static int calc_packet_len(muxer_stream_t
*s
, int psize
, int finalize
)
943 muxer_headers_t
*spriv
= s
->priv
;
944 int n
, len
, frpos
, m
;
947 frpos
= spriv
->framebuf
[0].pos
;
948 while(len
< psize
&& n
< spriv
->framebuf_used
)
950 if(!frpos
&& len
>0 && s
->type
== MUXER_TYPE_VIDEO
&& spriv
->framebuf
[n
].type
==I_FRAME
)
952 m
= FFMIN(spriv
->framebuf
[n
].size
- frpos
, psize
- len
);
955 if(frpos
== spriv
->framebuf
[n
].size
)
962 if(len
< psize
&& !finalize
)
967 static int find_packet_timestamps(muxer_priv_t
*priv
, muxer_stream_t
*s
, unsigned int start
, uint64_t *dts
, uint64_t *pts
)
969 muxer_headers_t
*spriv
= s
->priv
;
970 int i
, m
, pes_hlen
, ret
, threshold
;
971 uint64_t spts
, sdts
, dpts
;
973 if(!spriv
->framebuf_used
)
978 spriv
->dts
= spriv
->pts
= 0;
980 if(spriv
->framebuf
[0].pos
== 0) // start of frame
984 pes_hlen
= calc_pes_hlen(priv
->mux
, spriv
, priv
);
986 if(pes_hlen
< spriv
->min_pes_hlen
)
987 pes_hlen
= spriv
->min_pes_hlen
;
989 m
= spriv
->framebuf
[0].size
- spriv
->framebuf
[0].pos
;
991 if(start
+ pes_hlen
+ m
>= priv
->packet_size
) //spriv->pack_offset
992 i
= -1; //this pack won't have a pts: no space available
995 if(spriv
->framebuf_used
< 2)
998 if(spriv
->framebuf
[1].pts
== spriv
->framebuf
[1].dts
)
1003 //headers+frame 0 < space available including timestamps
1004 if(start
+ pes_hlen
+ m
< priv
->packet_size
- threshold
)
1013 dpts
= FFMAX(spriv
->last_saved_pts
, spriv
->framebuf
[i
].pts
) -
1014 FFMIN(spriv
->last_saved_pts
, spriv
->framebuf
[i
].pts
) +
1015 spriv
->framebuf
[0].idur
;
1017 if(s
->type
!= MUXER_TYPE_VIDEO
)
1019 else if((spriv
->framebuf
[i
].type
== I_FRAME
|| priv
->ts_allframes
|| dpts
>= 36000*300)) //0.4 seconds
1024 *pts
= spriv
->framebuf
[i
].pts
;
1025 *dts
= spriv
->framebuf
[i
].dts
;
1037 static int get_packet_stats(muxer_priv_t
*priv
, muxer_stream_t
*s
, pack_stats_t
*p
, int finalize
)
1039 muxer_headers_t
*spriv
= s
->priv
;
1040 int len
, len2
, pack_hlen
, pes_hlen
, hlen
, target
, stflen
, stuffing_len
;
1043 spriv
->pts
= spriv
->dts
= 0;
1044 p
->dts
= p
->pts
= p
->frame_pts
= p
->frame_dts
= 0;
1049 else if(priv
->mux
== MUX_MPEG1
)
1053 if(priv
->use_psm
&& psm_is_late(priv
))
1054 pack_hlen
+= calc_psm_len(priv
);
1056 if(find_packet_timestamps(priv
, s
, pack_hlen
, &dts
, &pts
))
1058 p
->pts
= p
->frame_pts
= pts
;
1059 p
->dts
= p
->frame_dts
= dts
;
1064 pes_hlen
= calc_pes_hlen(priv
->mux
, spriv
, priv
);
1066 p
->stflen
= stflen
= (spriv
->min_pes_hlen
> pes_hlen
? spriv
->min_pes_hlen
- pes_hlen
: 0);
1068 target
= len
= priv
->packet_size
- pack_hlen
- pes_hlen
- stflen
; //max space available
1069 if(s
->type
== MUXER_TYPE_AUDIO
&& s
->wf
->wFormatTag
== AUDIO_A52
)
1077 len2
= calc_packet_len(s
, target
, finalize
);
1078 if(!len2
|| (len2
< target
&& s
->type
== MUXER_TYPE_AUDIO
&& !finalize
))
1081 //p->dts = p->pts = 0;
1082 spriv
->pts
= spriv
->dts
= 0;
1083 //fprintf(stderr, "\r\nLEN2: %d, target: %d, type: %d\r\n", len2, target, s->type);
1091 if(s
->type
== MUXER_TYPE_VIDEO
)
1097 spriv
->pts
= spriv
->dts
= 0;
1098 p
->pts
= p
->dts
= 0;
1101 stuffing_len
= target
- len
;
1102 if(stuffing_len
> 0 && stuffing_len
< 7)
1104 if(stflen
+ stuffing_len
> 16)
1106 int x
= 7 - stuffing_len
;
1112 stflen
+= stuffing_len
;
1126 static int fill_packet(muxer_t
*muxer
, muxer_stream_t
*s
, int finalize
)
1128 //try to fill a packet as much as possible
1129 //spriv->pack_offset is the start position initialized to 0
1130 //data is taken from spriv->framebuf
1131 //if audio and a52 insert the headers
1132 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
1133 muxer_headers_t
*spriv
= (muxer_headers_t
*) s
->priv
;
1134 int len
, m
, n
, dvd_pack
= 0;
1139 spriv
->dts
= spriv
->pts
= 0;
1141 if(! spriv
->framebuf_used
)
1143 spriv
->pack_offset
= 0;
1147 if(!spriv
->pack_offset
)
1150 spriv
->pack_offset
= 0;
1152 spriv
->pack_offset
= write_mpeg_pack_header(muxer
, spriv
->pack
);
1153 if(priv
->update_system_header
&& (priv
->is_genmpeg1
|| priv
->is_genmpeg2
))
1155 spriv
->pack_offset
+= write_mpeg_system_header(muxer
, &spriv
->pack
[spriv
->pack_offset
]);
1156 priv
->update_system_header
= 0;
1159 if(priv
->use_psm
&& psm_is_late(priv
))
1161 spriv
->pack_offset
+= write_mpeg_psm(muxer
, &spriv
->pack
[spriv
->pack_offset
]);
1166 spriv
->pes_offset
= spriv
->pack_offset
;
1167 spriv
->payload_offset
= 0;
1169 spriv
->last_frame_rest
= 0;
1175 //search the pts. yes if either it's video && (I-frame or priv->ts_allframes) && framebuf[i].pos == 0
1176 //or it's audio && framebuf[i].pos == 0
1177 //NB pts and dts can only be relative to the first frame beginning in this pack
1178 if((priv
->is_xsvcd
|| priv
->is_xvcd
|| priv
->rawpes
) && spriv
->size
== 0)
1180 if(s
->type
== MUXER_TYPE_VIDEO
)
1181 bufsize
= (conf_vbuf_size
? conf_vbuf_size
: (priv
->is_xvcd
? 46 : 232));
1183 bufsize
= (conf_abuf_size
? conf_abuf_size
: 4);
1184 spriv
->buffer_size
= bufsize
*1024;
1187 if(priv
->is_dvd
&& s
->type
== MUXER_TYPE_VIDEO
1188 && spriv
->framebuf
[0].type
==I_FRAME
&& spriv
->framebuf
[0].pos
==0)
1191 if(! get_packet_stats(priv
, s
, &p
, finalize
))
1193 spriv
->pack_offset
= 0;
1199 spriv
->last_saved_pts
= p
.pts
;
1201 spriv
->pack_offset
+= write_mpeg_pes_header(spriv
, (uint8_t *) &s
->ckid
, &(spriv
->pack
[spriv
->pack_offset
]),
1202 p
.len
, p
.stflen
, priv
->mux
);
1204 if(s
->type
== MUXER_TYPE_AUDIO
&& s
->wf
->wFormatTag
== AUDIO_A52
)
1206 spriv
->payload_offset
= spriv
->pack_offset
;
1207 spriv
->pack_offset
+= 4; //for the 4 bytes of header
1208 if(!spriv
->framebuf
[0].pos
)
1209 spriv
->last_frame_rest
= 0;
1211 spriv
->last_frame_rest
= spriv
->framebuf
[0].size
- spriv
->framebuf
[0].pos
;
1218 if(spriv
->dts
|| spriv
->pts
)
1220 if((spriv
->dts
&& priv
->scr
>= spriv
->dts
) || priv
->scr
>= spriv
->pts
)
1221 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\r\nERROR: scr %.3f, dts %.3f, pts %.3f\r\n", (double) priv
->scr
/27000000.0, (double) spriv
->dts
/27000000.0, (double) spriv
->pts
/27000000.0);
1222 else if(priv
->scr
+ 63000*300 < spriv
->dts
)
1223 mp_msg(MSGT_MUXER
, MSGL_INFO
, "\r\nWARNING>: scr %.3f, dts %.3f, pts %.3f, diff %.3f, piff %.3f\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);
1229 frm
= spriv
->framebuf
;
1230 while(spriv
->pack_offset
< priv
->packet_size
&& n
< spriv
->framebuf_used
)
1234 //since iframes must always be aligned at block boundaries exit when we find the
1235 //beginning of one in the middle of the flush
1236 if(len
> 0 && s
->type
== MUXER_TYPE_VIDEO
&& frm
->type
== I_FRAME
)
1241 update_demux_bufsize(spriv
, frm
->dts
, frm
->size
, s
->type
);
1244 m
= FFMIN(frm
->size
- frm
->pos
, priv
->packet_size
- spriv
->pack_offset
);
1245 memcpy(&(spriv
->pack
[spriv
->pack_offset
]), &(frm
->buffer
[frm
->pos
]), m
);
1248 spriv
->pack_offset
+= m
;
1251 if(frm
->pos
== frm
->size
) //end of frame
1253 frm
->pos
= frm
->size
= 0;
1254 frm
->pts
= frm
->dts
= 0;
1260 if((priv
->is_xsvcd
|| priv
->is_xvcd
|| priv
->rawpes
) && spriv
->size
== 0)
1261 spriv
->buffer_size
= 0;
1265 if(dvd_pack
&& (spriv
->pack_offset
== priv
->packet_size
))
1266 write_mpeg_pack(muxer
, NULL
, muxer
->stream
, 0); //insert fake Nav Packet
1269 remove_frames(spriv
, n
);
1271 spriv
->track_bufsize
+= len
;
1272 if(spriv
->track_bufsize
> spriv
->max_buffer_size
)
1273 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\r\nBUFFER OVERFLOW: %d > %d, pts: %"PRIu64
"\r\n", spriv
->track_bufsize
, spriv
->max_buffer_size
, spriv
->pts
);
1275 if(s
->type
== MUXER_TYPE_AUDIO
&& s
->wf
->wFormatTag
== AUDIO_A52
)
1278 if(spriv
->pack_offset
< priv
->packet_size
&& !priv
->rawpes
) //here finalize is set
1280 int diff
= priv
->packet_size
- spriv
->pack_offset
;
1281 write_pes_padding(&(spriv
->pack
[spriv
->pack_offset
]), diff
);
1282 spriv
->pack_offset
+= diff
;
1285 stream_write_buffer(muxer
->stream
, spriv
->pack
, spriv
->pack_offset
);
1287 priv
->headers_size
+= spriv
->pack_offset
- len
;
1288 priv
->data_size
+= len
;
1289 muxer
->movi_end
+= spriv
->pack_offset
;
1291 spriv
->pack_offset
= 0;
1295 priv
->last_psm_scr
= priv
->scr
;
1300 static inline int find_best_stream(muxer_t
*muxer
)
1304 muxer_priv_t
*priv
= muxer
->priv
;
1305 muxer_headers_t
*spriv
;
1307 unsigned int perc
, sperc
;
1312 //THIS RULE MUST ALWAYS apply: dts <= SCR + 0.7 seconds
1313 for(i
= 0; i
< muxer
->avih
.dwStreams
; i
++)
1315 spriv
= muxer
->streams
[i
]->priv
;
1318 get_packet_stats(priv
, muxer
->streams
[i
], &p
, 0);
1320 if(spriv
->track_bufsize
+ p
.len
> spriv
->max_buffer_size
)
1322 if(p
.frame_pts
&& p
.frame_dts
> priv
->scr
+ 63000*300)
1325 if(spriv
->framebuf
[0].dts
<= dts
)
1327 dts
= spriv
->framebuf
[0].dts
;
1331 if(conf_interleaving2
)
1333 sperc
= (spriv
->track_bufsize
* 1024) / spriv
->max_buffer_size
;
1345 static void patch_seq(muxer_priv_t
*priv
, unsigned char *buf
)
1347 if(priv
->vwidth
> 0)
1349 buf
[4] = (priv
->vwidth
>> 4) & 0xff;
1351 buf
[5] |= (priv
->vwidth
& 0x0f) << 4;
1354 if(priv
->vheight
> 0)
1357 buf
[5] |= (priv
->vheight
>> 8) & 0x0f;
1358 buf
[6] = priv
->vheight
& 0xff;
1361 if(priv
->vaspect
> 0)
1362 buf
[7] = (buf
[7] & 0x0f) | (priv
->vaspect
<< 4);
1364 if(priv
->vframerate
> 0)
1365 buf
[7] = (buf
[7] & 0xf0) | priv
->vframerate
;
1367 if(priv
->vbitrate
> 0)
1369 buf
[8] = (priv
->vbitrate
>> 10);
1370 buf
[9] = (priv
->vbitrate
>> 2);
1371 buf
[10] = (buf
[10] & 0x3f) | (unsigned char) ((priv
->vbitrate
& 0x4) << 2);
1375 static void patch_panscan(muxer_priv_t
*priv
, unsigned char *buf
)
1376 { //patches sequence display extension (display_horizontal_size and display_vertical_size)
1383 if(priv
->panscan_width
> 0)
1385 buf
[offset
] = (priv
->panscan_width
>> 6);
1386 buf
[offset
+1] = ((priv
->panscan_width
& 0x3F) << 2) | (buf
[offset
+ 1] & 0x03);
1391 if(priv
->panscan_height
> 0)
1393 buf
[offset
] = (priv
->panscan_height
>> 13) << 7;
1394 buf
[offset
+1] = (priv
->panscan_height
>> 5) & 0xFF;
1395 buf
[offset
+2] = ((priv
->panscan_height
& 0x1F) << 3) | (buf
[offset
+2] & 0x07);
1400 static void update_scr(muxer_t
*muxer
)
1402 muxer_priv_t
*priv
= muxer
->priv
;
1403 muxer_stream_t
*stream
;
1404 muxer_headers_t
*spriv
;
1406 uint64_t mindts
= (uint64_t) -1;
1408 priv
->scr
+= priv
->delta_scr
;
1410 for(i
= 0; i
< muxer
->avih
.dwStreams
; i
++)
1412 stream
= muxer
->streams
[i
];
1413 spriv
= stream
->priv
;
1414 if(spriv
->framebuf_used
&& spriv
->framebuf
[0].dts
< mindts
)
1415 mindts
= spriv
->framebuf
[0].dts
;
1418 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "UPDATE SCR TO %"PRIu64
" (%.3f)\n", priv
->scr
, (double) (priv
->scr
/27000000.0));
1420 for(i
= 0; i
< muxer
->avih
.dwStreams
; i
++)
1422 stream
= muxer
->streams
[i
];
1423 spriv
= stream
->priv
;
1426 while(j
< spriv
->track_pos
&& priv
->scr
>= spriv
->buffer_track
[j
].dts
)
1428 spriv
->track_bufsize
-= spriv
->buffer_track
[j
].size
;
1431 if(spriv
->track_bufsize
< 0)
1434 muxer
->sysrate
= (muxer
->sysrate
* 11) / 10; //raise by 10%
1435 d
= (double) priv
->packet_size
/ (double)muxer
->sysrate
;
1436 priv
->delta_scr
= (uint64_t) (d
* 27000000.0f
);
1437 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
);
1438 spriv
->track_bufsize
= 0;
1443 memmove(spriv
->buffer_track
, &(spriv
->buffer_track
[j
]), (spriv
->track_len
- j
) * sizeof(buffer_track_t
));
1444 spriv
->track_pos
-= j
;
1445 for(j
= spriv
->track_pos
; j
< spriv
->track_len
; j
++)
1446 spriv
->buffer_track
[j
].size
= 0;
1449 if(spriv
->framebuf_used
&& spriv
->framebuf
[0].dts
< mindts
)
1450 mindts
= spriv
->framebuf
[0].dts
;
1455 static int calc_frames_to_flush(muxer_headers_t
*vpriv
)
1459 if(vpriv
->framebuf_used
> 0)
1462 //let's count how many frames we'll store in the next pack sequence
1463 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "\n");
1464 while(n
< vpriv
->framebuf_used
)
1466 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "CALC_FRAMES, n=%d, type=%c, pts=%.3f\n", n
, FTYPE(vpriv
->framebuf
[n
].type
), (double)vpriv
->framebuf
[n
].pts
/27000000.0f
);
1467 if(n
+1 < vpriv
->framebuf_used
)
1468 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "n+1=%d, type=%c, pts=%.3f\n", n
+1, FTYPE(vpriv
->framebuf
[n
+1].type
), (double)vpriv
->framebuf
[n
+1].pts
/27000000.0f
);
1470 if(vpriv
->framebuf
[n
].type
== I_FRAME
)
1483 if(found
&& (n
< vpriv
->framebuf_used
+1))
1489 static int flush_buffers(muxer_t
*muxer
, int finalize
)
1493 uint64_t init_delay
= 0;
1494 muxer_stream_t
*s
, *vs
, *as
;
1495 muxer_headers_t
*vpriv
= NULL
, *apriv
= NULL
;
1496 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
1498 uint64_t iduration
, iaduration
;
1501 analyzes all streams and decides what to flush
1502 trying to respect an interleaving distribution
1503 equal to the v_bitrate/a_bitrate proportion
1508 for(i
= 0; i
< muxer
->avih
.dwStreams
; i
++)
1510 s
= muxer
->streams
[i
];
1511 if(s
->type
== MUXER_TYPE_VIDEO
)
1513 vs
= muxer
->streams
[i
];
1514 vpriv
= (muxer_headers_t
*) vs
->priv
;
1515 if(!vpriv
->is_ready
)
1517 n
= found
= calc_frames_to_flush(vpriv
);
1519 else if(s
->type
== MUXER_TYPE_AUDIO
)
1523 if((! found
) && finalize
)
1526 found
= n
= vpriv
->framebuf_used
;
1531 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "\nVIDEO, FLUSH %d frames (of %d), 0 to %d\n", n
, vpriv
->framebuf_used
, n
-1);
1533 vpriv
= (muxer_headers_t
*) vs
->priv
;
1537 for(i
= 0; i
< n
; i
++)
1538 iduration
+= vpriv
->framebuf
[i
].idur
;
1539 duration
= (double) (iduration
/ 27000000.0);
1543 apriv
= (muxer_headers_t
*) as
->priv
;
1545 for(i
= 0; i
< apriv
->framebuf_used
; i
++)
1547 iaduration
+= apriv
->framebuf
[i
].idur
;
1549 if(iaduration
< iduration
)
1551 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "Not enough audio data exit\n");
1556 if(as
!= NULL
&& (apriv
->size
== 0))
1558 init_delay
= vpriv
->framebuf
[0].pts
- vpriv
->framebuf
[0].dts
;
1560 for(i
= 0; i
< apriv
->framebuf_cnt
; i
++)
1562 apriv
->framebuf
[i
].pts
+= init_delay
;
1563 apriv
->framebuf
[i
].dts
+= init_delay
;
1565 apriv
->last_pts
+= init_delay
;
1566 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "\r\nINITIAL VIDEO DELAY: %.3f, currAPTS: %.3f\r\n", (double) init_delay
/27000000.0f
, (double) apriv
->last_pts
/27000000.0f
);
1569 if((priv
->is_xvcd
|| priv
->is_xsvcd
) && (vpriv
->size
== 0))
1570 vpriv
->buffer_size
= (conf_vbuf_size
? conf_vbuf_size
: (priv
->is_xvcd
? 46 : 230))*1024;
1578 i
= find_best_stream(muxer
);
1581 if(!fill_packet(muxer
, muxer
->streams
[i
], finalize
))
1584 if(skip_cnt
== muxer
->avih
.dwStreams
)
1592 muxer
->file_end
= priv
->scr
;
1597 static inline uint64_t parse_fps(float fps
)
1599 // 90000 * 300 * 1001 / d , there's no rounding error with any of the admitted framerates
1600 int d
= (int)(fps
*1001+0.5);
1602 return 27027000000ULL / d
;
1606 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
)
1612 *fps_ptr
= (*fps_ptr
& 0xf0) | priv
->vframerate
;
1613 vpriv
->nom_delta_pts
= parse_fps(conf_vframerate
);
1616 //in pce_ptr starting from bit 0 bit 24 is tff, bit 30 is rff,
1617 if(pce_ptr
[3] & 0x2)
1619 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\nERROR! RFF bit is already set, disabling telecining\n");
1620 vpriv
->telecine
= 0;
1624 vpriv
->picture
.progressive_sequence
= 0;
1625 vpriv
->picture
.progressive_frame
= 1;
1629 //disable tff and rff and overwrite them with the value in bff_mask
1630 pce_ptr
[3] = (pce_ptr
[3] & 0x7d) | priv
->bff_mask
[vpriv
->display_frame
% MAX_PATTERN_LENGTH
];
1631 pce_ptr
[4] |= 0x80; //sets progressive frame
1633 vpriv
->display_frame
+= n
;
1634 if(! vpriv
->vframes
)
1635 mp_msg(MSGT_MUXER
, MSGL_INFO
, "\nENABLED SOFT TELECINING, FPS=%.3f\n",conf_vframerate
);
1640 static size_t parse_mpeg12_video(muxer_stream_t
*s
, muxer_priv_t
*priv
, muxer_headers_t
*spriv
, float fps
, size_t len
)
1642 uint8_t *fps_ptr
= NULL
; //pointer to the fps byte in the sequence header
1643 uint8_t *se_ptr
= NULL
; //pointer to sequence extension
1644 uint8_t *pce_ptr
= NULL
; //pointer to picture coding extension
1645 int frames_diff
, d1
, gop_reset
= 0; //how any frames we advanced respect to the last one
1651 mp_msg(MSGT_MUXER
, MSGL_DBG2
,"parse_mpeg12_video, len=%u\n", (uint32_t) len
);
1652 if(s
->buffer
[0] != 0 || s
->buffer
[1] != 0 || s
->buffer
[2] != 1 || len
<6)
1654 mp_msg(MSGT_MUXER
, MSGL_ERR
,"Unknown video format, possibly non-MPEG1/2 stream, len=%d!\n", len
);
1663 { // Video (0) Sequence header (b3) or GOP (b8)
1664 if((s
->buffer
[i
] == 0) && (s
->buffer
[i
+1] == 0) && (s
->buffer
[i
+2] == 1))
1666 switch(s
->buffer
[i
+3])
1668 case 0xb3: //sequence
1675 fps_ptr
= &(s
->buffer
[i
+7]);
1676 mp_header_process_sequence_header(&(spriv
->picture
), &(s
->buffer
[i
+4]));
1677 spriv
->delta_pts
= spriv
->nom_delta_pts
= parse_fps(spriv
->picture
.fps
);
1679 spriv
->delta_clock
= (double) 1/fps
;
1680 //the 2 lines below are needed to handle non-standard frame rates (such as 18)
1681 if(! spriv
->delta_pts
)
1682 spriv
->delta_pts
= spriv
->nom_delta_pts
= (uint64_t) ((double)27000000.0 * spriv
->delta_clock
);
1683 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "\nFPS: %.3f, FRAMETIME: %.3f\n", fps
, (double)1/fps
);
1685 patch_seq(priv
, &(s
->buffer
[i
]));
1695 mp_header_process_extension(&(spriv
->picture
), &(s
->buffer
[i
+4]));
1696 if(((s
->buffer
[i
+4] & 0xf0) == 0x10))
1697 se_ptr
= &(s
->buffer
[i
+4]);
1698 if(((s
->buffer
[i
+4] & 0xf0) == 0x20))
1701 patch_panscan(priv
, &(s
->buffer
[i
+4]));
1703 if((s
->buffer
[i
+4] & 0xf0) == 0x80)
1705 pce_ptr
= &(s
->buffer
[i
+4]);
1719 pt
= (s
->buffer
[i
+5] & 0x1c) >> 3;
1720 temp_ref
= (s
->buffer
[i
+4]<<2)+(s
->buffer
[i
+5]>>6);
1723 if(err
) break; //something went wrong
1724 if(s
->buffer
[i
+3] >= 0x01 && s
->buffer
[i
+3] <= 0xAF) break; //slice, we have already analized what we need
1729 mp_msg(MSGT_MUXER
, MSGL_ERR
,"Warning: picture too short or broken!\n");
1731 //following 2 lines are workaround: lavf doesn't sync to sequence headers before passing demux_packets
1732 if(!spriv
->nom_delta_pts
)
1733 spriv
->delta_pts
= spriv
->nom_delta_pts
= parse_fps(fps
);
1735 spriv
->last_tr
= spriv
->max_tr
= temp_ref
;
1736 d1
= temp_ref
- spriv
->last_tr
;
1738 frames_diff
= spriv
->max_tr
+ 1 + temp_ref
- spriv
->last_tr
;
1741 if(d1
< -6) //there's a wraparound
1742 frames_diff
= spriv
->max_tr
+ 1 + temp_ref
- spriv
->last_tr
;
1743 else if(d1
> 6) //there's a wraparound
1744 frames_diff
= spriv
->max_tr
+ 1 + spriv
->last_tr
- temp_ref
;
1745 else if(!d1
) //pre-emptive fix against broken sequences
1750 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "\nLAST: %d, TR: %d, GOP: %d, DIFF: %d, MAX: %d, d1: %d\n",
1751 spriv
->last_tr
, temp_ref
, gop_reset
, frames_diff
, spriv
->max_tr
, d1
);
1753 if(temp_ref
> spriv
->max_tr
|| gop_reset
)
1754 spriv
->max_tr
= temp_ref
;
1756 spriv
->last_tr
= temp_ref
;
1757 if(spriv
->picture
.mpeg1
== 0)
1759 if(spriv
->telecine
&& pce_ptr
)
1761 soft_telecine(priv
, spriv
, fps_ptr
, se_ptr
, pce_ptr
, frames_diff
);
1762 spriv
->picture
.display_time
= 100;
1763 mp_header_process_extension(&(spriv
->picture
), pce_ptr
);
1764 if(spriv
->picture
.display_time
>= 50 && spriv
->picture
.display_time
<= 300)
1765 spriv
->delta_pts
= (spriv
->nom_delta_pts
* spriv
->picture
.display_time
) / 100;
1769 if(! spriv
->vframes
)
1772 spriv
->last_dts
+= spriv
->delta_pts
;
1773 spriv
->last_pts
+= spriv
->nom_delta_pts
*(frames_diff
-1) + spriv
->delta_pts
;
1775 ret
= add_frame(spriv
, spriv
->delta_pts
, s
->buffer
, len
, pt
, spriv
->last_dts
, spriv
->last_pts
);
1778 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\r\nPARSE_MPEG12: add_frames(%d) failed, exit\r\n", len
);
1781 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "\r\nVIDEO FRAME, PT: %C, tr: %d, diff: %d, dts: %.3f, pts: %.3f, pdt: %u, gop_reset: %d\r\n",
1782 ftypes
[pt
], temp_ref
, frames_diff
, ((double) spriv
->last_dts
/27000000.0f
),
1783 ((double) spriv
->last_pts
/27000000.0f
), spriv
->picture
.display_time
, gop_reset
);
1788 int64_t diff
= spriv
->last_dts
- spriv
->last_pts
;
1792 n
= spriv
->framebuf_used
- 1;
1794 for(j
= n
; j
>= 0; j
--)
1796 if(spriv
->framebuf
[j
].pts
>= spriv
->last_pts
)
1798 spriv
->framebuf
[j
].pts
+= diff
;
1802 mp_msg(MSGT_MUXER
, MSGL_V
, "\r\nResynced B-frame by %d units, DIFF: %"PRId64
" (%.3f),[pd]ts=%.3f\r\n",
1803 n
, diff
, (double) diff
/27000000.0f
, (double) spriv
->last_pts
/27000000.0f
);
1804 spriv
->last_pts
= spriv
->last_dts
;
1809 mp_msg(MSGT_MUXER
, MSGL_DBG2
,"parse_mpeg12_video, return %u\n", (uint32_t) len
);
1814 static uint64_t fix_mp4_frame_duration(muxer_headers_t
*vpriv
)
1816 uint64_t mn
, md
, mx
, diff
;
1819 mn
= mx
= vpriv
->framebuf
[0].pts
;
1820 for(i
= 0; i
< 3; i
++)
1822 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, "PTS: %"PRIu64
"\n", vpriv
->framebuf
[i
].pts
);
1823 if(vpriv
->framebuf
[i
].pts
< mn
)
1824 mn
= vpriv
->framebuf
[i
].pts
;
1825 if(vpriv
->framebuf
[i
].pts
> mx
)
1826 mx
= vpriv
->framebuf
[i
].pts
;
1831 if((vpriv
->framebuf
[i
].pts
> mn
) && (vpriv
->framebuf
[i
].pts
< mx
))
1832 md
= vpriv
->framebuf
[i
].pts
;
1835 if(mx
- md
> md
- mn
)
1840 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, "MIN: %"PRIu64
", mid: %"PRIu64
", max: %"PRIu64
", diff: %"PRIu64
"\n", mn
, md
, mx
, diff
);
1845 vpriv
->framebuf
[i
].pts
+= diff
;
1846 vpriv
->framebuf
[i
].dts
+= i
* diff
;
1847 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "FIXED_PTS: %.3f, FIXED_DTS: %.3f\n",
1848 (double) (vpriv
->framebuf
[i
].pts
/27000000.0), (double) (vpriv
->framebuf
[i
].dts
/27000000.0));
1857 static size_t parse_mpeg4_video(muxer_stream_t
*s
, muxer_priv_t
*priv
, muxer_headers_t
*vpriv
, float fps
, size_t len
)
1860 int64_t delta_pts
=0;
1864 mp_msg(MSGT_MUXER
, MSGL_DBG2
,"parse_mpeg4_video, len=%u\n", (uint32_t) len
);
1867 mp_msg(MSGT_MUXER
, MSGL_ERR
,"Frame too short: %d, exit!\n", len
);
1872 while(ptr
< len
- 5)
1874 if(s
->buffer
[ptr
] != 0 || s
->buffer
[ptr
+1] != 0 || s
->buffer
[ptr
+2] != 1)
1880 if(s
->buffer
[ptr
+3] >= 0x20 && s
->buffer
[ptr
+3] <= 0x2f) //VOL
1882 mp4_header_process_vol(&(vpriv
->picture
), &(s
->buffer
[ptr
+4]));
1884 else if(s
->buffer
[ptr
+3] == 0xb3) //gov
1886 //fprintf(stderr, "\nGOV\n");
1888 else if(s
->buffer
[ptr
+3] == 0xb6) //vop
1891 mp4_header_process_vop(&(vpriv
->picture
), &(s
->buffer
[ptr
+4]));
1893 delta
= vpriv
->picture
.timeinc_unit
- vpriv
->last_tr
;
1894 if((delta
> 0) && (delta
> (vpriv
->picture
.timeinc_resolution
/2)))
1895 delta
-= vpriv
->picture
.timeinc_resolution
;
1896 else if((delta
< 0) && (delta
< (-(vpriv
->picture
.timeinc_resolution
/2))))
1897 delta
+= vpriv
->picture
.timeinc_resolution
;
1899 delta_pts
= (27000000 * (int64_t) delta
) / vpriv
->picture
.timeinc_resolution
;
1900 //warning, it seems that packed bops can lead to delta == 0
1902 pt
= vpriv
->picture
.picture_type
+ 1;
1903 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "\nTYPE: %c, RESOLUTION: %d, TEMP: %d, delta: %d, delta_pts: %"PRId64
" = %.3f, delta2: %.3f\n",
1904 FTYPE(pt
), vpriv
->picture
.timeinc_resolution
, vpriv
->picture
.timeinc_unit
, delta
, delta_pts
, (double) (delta_pts
/27000000.0),
1905 (double) delta
/ (double) vpriv
->picture
.timeinc_resolution
);
1907 vpriv
->last_tr
= vpriv
->picture
.timeinc_unit
;
1917 vpriv
->last_dts
+= vpriv
->frame_duration
;
1918 vpriv
->last_pts
+= delta_pts
;
1921 ret
= add_frame(vpriv
, delta_pts
, s
->buffer
, len
, pt
, vpriv
->last_dts
, vpriv
->last_pts
);
1924 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\r\nPARSE_MPEG4: add_frames(%d) failed, exit\r\n", len
);
1928 if(!vpriv
->frame_duration
&& vpriv
->framebuf_used
== 3)
1930 vpriv
->frame_duration
= fix_mp4_frame_duration(vpriv
);
1931 if(vpriv
->frame_duration
)
1933 vpriv
->last_pts
+= vpriv
->frame_duration
;
1934 vpriv
->last_dts
= vpriv
->framebuf
[vpriv
->framebuf_used
-1].dts
;
1935 vpriv
->delta_clock
= ((double) vpriv
->frame_duration
)/27000000.0;
1936 mp_msg(MSGT_MUXER
, MSGL_INFO
, "FRAME DURATION: %"PRIu64
" %.3f\n",
1937 vpriv
->frame_duration
, (double) (vpriv
->frame_duration
/27000000.0));
1938 vpriv
->is_ready
= 1;
1942 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "LAST_PTS: %.3f, LAST_DTS: %.3f\n",
1943 (double) (vpriv
->last_pts
/27000000.0), (double) (vpriv
->last_dts
/27000000.0));
1951 static int fill_last_frame(muxer_headers_t
*spriv
, uint8_t *ptr
, int len
)
1958 if(spriv
->framebuf_used
== 0)
1959 idx
= spriv
->framebuf_used
;
1961 idx
= spriv
->framebuf_used
- 1;
1963 if(spriv
->framebuf
[idx
].alloc_size
< spriv
->framebuf
[idx
].size
+ len
)
1965 if(spriv
->framebuf
[idx
].size
> SIZE_MAX
- (size_t)len
)
1967 spriv
->framebuf
[idx
].buffer
= (uint8_t*) realloc(spriv
->framebuf
[idx
].buffer
, spriv
->framebuf
[idx
].size
+ len
);
1968 if(! spriv
->framebuf
[idx
].buffer
)
1970 spriv
->framebuf
[idx
].alloc_size
= spriv
->framebuf
[idx
].size
+ len
;
1973 memcpy(&(spriv
->framebuf
[idx
].buffer
[spriv
->framebuf
[idx
].size
]), ptr
, len
);
1974 spriv
->framebuf
[idx
].size
+= len
;
1979 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
)
1983 idx
= spriv
->framebuf_used
;
1984 if(idx
>= spriv
->framebuf_cnt
)
1986 spriv
->framebuf
= (mpeg_frame_t
*) realloc_struct(spriv
->framebuf
, (spriv
->framebuf_cnt
+1), sizeof(mpeg_frame_t
));
1987 if(spriv
->framebuf
== NULL
)
1989 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't realloc frame buffer(idx), abort\n");
1993 spriv
->framebuf
[spriv
->framebuf_cnt
].size
= 0;
1994 spriv
->framebuf
[spriv
->framebuf_cnt
].alloc_size
= 0;
1995 spriv
->framebuf
[spriv
->framebuf_cnt
].pos
= 0;
1997 spriv
->framebuf
[spriv
->framebuf_cnt
].buffer
= (uint8_t*) malloc(len
);
1998 if(spriv
->framebuf
[spriv
->framebuf_cnt
].buffer
== NULL
)
2000 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't realloc frame buffer(frame), abort\n");
2003 spriv
->framebuf
[spriv
->framebuf_cnt
].alloc_size
= len
;
2004 spriv
->framebuf_cnt
++;
2007 if(spriv
->framebuf
[idx
].alloc_size
< spriv
->framebuf
[idx
].size
+ len
)
2009 if(spriv
->framebuf
[idx
].size
> SIZE_MAX
- (size_t)len
)
2011 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Size overflow, couldn't realloc frame buffer(frame), abort\n");
2014 spriv
->framebuf
[idx
].buffer
= realloc(spriv
->framebuf
[idx
].buffer
, spriv
->framebuf
[idx
].size
+ len
);
2015 if(spriv
->framebuf
[idx
].buffer
== NULL
)
2017 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't realloc frame buffer(frame), abort\n");
2020 spriv
->framebuf
[idx
].alloc_size
= spriv
->framebuf
[idx
].size
+ len
;
2023 memcpy(&(spriv
->framebuf
[idx
].buffer
[spriv
->framebuf
[idx
].size
]), ptr
, len
);
2024 spriv
->framebuf
[idx
].size
+= len
;
2025 spriv
->framebuf
[idx
].pos
= 0;
2026 spriv
->framebuf
[idx
].type
= pt
;
2028 spriv
->framebuf
[idx
].idur
= idur
;
2029 spriv
->framebuf
[idx
].dts
= dts
;
2030 spriv
->framebuf
[idx
].pts
= pts
;
2031 spriv
->framebuf_used
++;
2032 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
);
2037 static int analyze_mpa(muxer_stream_t
*s
)
2039 int i
= 0, len
, max
, chans
, srate
, spf
, layer
;
2040 int score
[4] = {0, 0, 0, 0};
2042 while(i
< s
->b_buffer_len
+ 3)
2044 if(s
->b_buffer
[i
] == 0xFF && ((s
->b_buffer
[i
+1] & 0xE0) == 0xE0))
2046 len
= mp_get_mp3_header(&(s
->b_buffer
[i
]), &chans
, &srate
, &spf
, &layer
, NULL
);
2047 if(len
> 0 && (srate
== s
->wf
->nSamplesPerSec
) && (i
+ len
<= s
->b_buffer_len
))
2058 for(i
= 1; i
<= 3; i
++)
2067 return layer
; //actual layer with the highest score
2070 int aac_parse_frame(uint8_t *buf
, int *srate
, int *num
);
2072 static int parse_audio(muxer_stream_t
*s
, int finalize
, unsigned int *nf
, double *timer
, double delay
, int drop
)
2074 int i
, j
, len
, chans
, srate
, spf
, layer
, dummy
, tot
, num
, frm_idx
;
2076 unsigned int frames
;
2079 muxer_headers_t
*spriv
= (muxer_headers_t
*) s
->priv
;
2081 i
= tot
= frames
= 0;
2086 switch(s
->wf
->wFormatTag
)
2091 if(i
+ 3 >= s
->b_buffer_len
)
2097 if(s
->b_buffer
[i
] == 0xFF && ((s
->b_buffer
[i
+1] & 0xE0) == 0xE0))
2099 len
= mp_get_mp3_header(&(s
->b_buffer
[i
]), &chans
, &srate
, &spf
, &layer
, NULL
);
2100 if(len
> 0 && (srate
== s
->wf
->nSamplesPerSec
) && (i
+ len
<= s
->b_buffer_len
)
2101 && layer
== spriv
->mpa_layer
)
2103 dur
= (double) spf
/ (double) srate
;
2104 idur
= (27000000ULL * spf
) / srate
;
2114 if(i
+ 6 >= s
->b_buffer_len
)
2120 if(s
->b_buffer
[i
] == 0x0B && s
->b_buffer
[i
+1] == 0x77)
2123 #ifdef CONFIG_LIBA52
2124 len
= a52_syncinfo(&(s
->b_buffer
[i
]), &dummy
, &srate
, &dummy
);
2126 len
= mp_a52_framesize(&(s
->b_buffer
[i
]), &srate
);
2128 if((len
> 0) && (srate
== s
->wf
->nSamplesPerSec
) && (i
+ len
<= s
->b_buffer_len
))
2130 dur
= (double) 1536 / (double) srate
;
2131 idur
= (27000000ULL * 1536) / srate
;
2142 if(i
+ 7 >= s
->b_buffer_len
)
2148 if(s
->b_buffer
[i
] == 0xFF && ((s
->b_buffer
[i
+1] & 0xF6) == 0xF0))
2150 len
= aac_parse_frame(&(s
->b_buffer
[i
]), &srate
, &num
);
2151 if((len
> 0) && (srate
== s
->wf
->nSamplesPerSec
) && (i
+ len
<= s
->b_buffer_len
))
2153 dur
= (double) 1024 / (double) srate
;
2154 idur
= (27000000ULL * 1024 * num
) / srate
;
2171 spriv
->timer
+= dur
;
2172 if(spriv
->drop_delayed_frames
&& delay
< 0 && spriv
->timer
<= -delay
)
2180 fill_last_frame(spriv
, &(s
->b_buffer
[tot
]), i
- tot
);
2181 frm_idx
= add_frame(spriv
, idur
, &(s
->b_buffer
[i
]), len
, 0, spriv
->last_pts
, spriv
->last_pts
);
2184 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't add audio frame buffer(frame), abort\n");
2187 for(j
= frm_idx
; j
< spriv
->framebuf_cnt
; j
++)
2188 spriv
->framebuf
[j
].pts
= spriv
->last_pts
;
2189 spriv
->last_pts
+= idur
;
2198 memmove(s
->b_buffer
, &(s
->b_buffer
[tot
]), s
->b_buffer_len
- tot
);
2199 s
->b_buffer_len
-= tot
;
2200 s
->b_buffer_ptr
+= tot
;
2201 if(s
->b_buffer_len
> 0)
2202 memmove(s
->b_buffer
, &(s
->b_buffer
[s
->b_buffer_ptr
]), s
->b_buffer_len
);
2203 s
->b_buffer_ptr
= 0;
2208 frm_idx
= add_frame(spriv
, 0, s
->b_buffer
, s
->b_buffer_len
, 0, spriv
->last_pts
, spriv
->last_pts
);
2211 for(j
= frm_idx
; j
< spriv
->framebuf_cnt
; j
++)
2212 spriv
->framebuf
[j
].pts
= spriv
->last_pts
;
2217 *timer
= spriv
->timer
;
2222 static void fix_parameters(muxer_stream_t
*stream
)
2224 muxer_headers_t
*spriv
= stream
->priv
;
2225 muxer_t
*muxer
= stream
->muxer
;
2226 muxer_priv_t
*priv
= muxer
->priv
;
2227 uint32_t stream_format
;
2230 if(stream
->type
== MUXER_TYPE_AUDIO
)
2232 stream_format
= stream
->wf
->wFormatTag
;
2233 spriv
->is_ready
= 1;
2235 spriv
->max_buffer_size
= conf_abuf_size
*1024;
2237 spriv
->max_buffer_size
= 4*1024;
2238 if(stream
->wf
->wFormatTag
== AUDIO_A52
)
2240 stream
->ckid
= be2me_32 (0x1bd);
2241 if(priv
->is_genmpeg1
|| priv
->is_genmpeg2
)
2242 fix_audio_sys_header(priv
, spriv
->id
, 0xbd, FFMAX(conf_abuf_size
, 58)*1024); //only one audio at the moment
2245 spriv
->max_buffer_size
= 16*1024;
2247 else if(stream
->wf
->wFormatTag
== AUDIO_AAC1
|| stream
->wf
->wFormatTag
== AUDIO_AAC2
)
2249 else if(stream
->wf
->wFormatTag
== AUDIO_MP2
|| stream
->wf
->wFormatTag
== AUDIO_MP3
)
2250 spriv
->is_ready
= 0;
2254 stream_format
= stream
->bih
->biCompression
;
2256 spriv
->max_buffer_size
= conf_vbuf_size
*1024;
2260 spriv
->max_buffer_size
= 232*1024;
2261 else if(priv
->is_xsvcd
)
2262 spriv
->max_buffer_size
= 230*1024;
2263 else if(priv
->is_xvcd
)
2264 spriv
->max_buffer_size
= 46*1024;
2266 spriv
->max_buffer_size
= 232*1024; //no profile => unconstrained :) FIXME!!!
2269 if(is_mpeg4(stream
->bih
->biCompression
))
2270 spriv
->is_ready
= 0;
2272 spriv
->is_ready
= 1;
2274 if(!is_mpeg1(stream_format
) && !is_mpeg2(stream_format
))
2278 if(priv
->is_genmpeg2
&& needs_psm
)
2281 add_to_psm(priv
, spriv
->id
, stream_format
);
2282 priv
->psm_streams_cnt
++;
2287 static void mpegfile_write_chunk(muxer_stream_t
*s
,size_t len
,unsigned int flags
, double dts_arg
, double pts_arg
)
2291 muxer_t
*muxer
= s
->muxer
;
2292 muxer_priv_t
*priv
= (muxer_priv_t
*)muxer
->priv
;
2293 muxer_headers_t
*spriv
= (muxer_headers_t
*) s
->priv
;
2295 uint32_t stream_format
, nf
;
2297 if(s
->buffer
== NULL
|| len
== -1)
2300 if (s
->type
== MUXER_TYPE_VIDEO
)
2301 { // try to recognize frame type...
2302 fps
= (float) s
->h
.dwRate
/ (float) s
->h
.dwScale
;
2304 stream_format
= s
->bih
->biCompression
;
2305 if(! spriv
->vframes
)
2307 spriv
->last_dts
= spriv
->last_pts
- (uint64_t)(27000000.0f
/fps
);
2308 mp_msg(MSGT_MUXER
, MSGL_INFO
,"INITV: %.3f, %.3f, fps: %.3f\r\n", (double) spriv
->last_pts
/27000000.0f
, (double) spriv
->last_dts
/27000000.0f
, fps
);
2311 if(is_mpeg1(stream_format
) || is_mpeg2(stream_format
))
2313 spriv
->is_mpeg12
= 1;
2314 spriv
->is_ready
= 1;
2316 sz
= parse_mpeg12_video(s
, priv
, spriv
, fps
, len
);
2319 tmp
= (uint64_t) (27000000.0f
/ fps
);
2320 spriv
->last_pts
+= tmp
;
2321 spriv
->last_dts
+= tmp
;
2324 else if(is_mpeg4(stream_format
))
2326 spriv
->is_mpeg12
= 0;
2327 spriv
->telecine
= 0;
2329 sz
= parse_mpeg4_video(s
, priv
, spriv
, fps
, len
);
2332 tmp
= (uint64_t) (27000000.0f
/ fps
);
2333 spriv
->last_pts
+= tmp
;
2334 spriv
->last_dts
+= tmp
;
2338 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
);
2341 { // MUXER_TYPE_AUDIO
2344 stream_format
= s
->wf
->wFormatTag
;
2346 if(s
->b_buffer_size
- s
->b_buffer_len
< len
)
2350 if(s
->b_buffer_len
> SIZE_MAX
- len
)
2352 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\nFATAL! couldn't realloc, integer overflow\n");
2355 tmp
= realloc(s
->b_buffer
, len
+ s
->b_buffer_len
);
2358 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "\nFATAL! couldn't realloc %d bytes\n", len
+ s
->b_buffer_len
);
2363 s
->b_buffer_size
= len
+ s
->b_buffer_len
;
2364 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "REALLOC(%d) bytes to AUDIO backbuffer\n", s
->b_buffer_size
);
2366 memcpy(&(s
->b_buffer
[s
->b_buffer_ptr
+ s
->b_buffer_len
]), s
->buffer
, len
);
2367 s
->b_buffer_len
+= len
;
2369 if(!spriv
->is_ready
)
2371 if(s
->b_buffer_len
>= 32*1024)
2373 spriv
->mpa_layer
= analyze_mpa(s
);
2374 spriv
->is_ready
= 1;
2379 parse_audio(s
, 0, &nf
, &fake_timer
, priv
->init_adelay
, priv
->drop
);
2380 spriv
->vframes
+= nf
;
2381 if(! spriv
->vframes
)
2382 mp_msg(MSGT_MUXER
, MSGL_INFO
, "AINIT: %.3f\r\n", (double) spriv
->last_pts
/27000000.0f
);
2386 flush_buffers(muxer
, 0);
2390 static void mpegfile_write_index(muxer_t
*muxer
)
2394 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
2396 mp_tmsg(MSGT_MUXER
, MSGL_INFO
, "Writing index...\n");
2398 for(i
= 0; i
< muxer
->avih
.dwStreams
; i
++)
2400 if(muxer
->streams
[i
]->type
== MUXER_TYPE_AUDIO
)
2401 parse_audio(muxer
->streams
[i
], 1, &nf
, &fake_timer
, priv
->init_adelay
, priv
->drop
);
2403 while(flush_buffers(muxer
, 0) > 0);
2404 flush_buffers(muxer
, 1);
2405 if(priv
->is_genmpeg1
|| priv
->is_genmpeg2
)
2408 write_mpeg_pack(muxer
, NULL
, muxer
->stream
, 1); //insert fake Nav Packet
2411 mp_msg(MSGT_MUXER
, MSGL_INFO
, "\nOverhead: %.3f%% (%"PRIu64
" / %"PRIu64
")\n", 100.0 * (double)priv
->headers_size
/ (double)priv
->data_size
, priv
->headers_size
, priv
->data_size
);
2414 static void mpegfile_write_header(muxer_t
*muxer
)
2416 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
2418 mp_tmsg(MSGT_MUXER
, MSGL_INFO
, "Writing header...\n");
2420 priv
->headers_cnt
++;
2422 //write the first system header only for generic mpeg1/2 muxes, and only when we have collected all necessary infos
2423 if(priv
->is_genmpeg1
|| priv
->is_genmpeg2
|| ((priv
->is_xvcd
|| priv
->is_xsvcd
) && (priv
->headers_cnt
== 1)))
2425 write_mpeg_pack(muxer
, NULL
, muxer
->stream
, 0);
2426 priv
->update_system_header
= 0;
2432 static void setup_sys_params(muxer_priv_t
*priv
)
2436 int v
= (conf_vbuf_size
? conf_vbuf_size
: 232);
2437 int a1
= (conf_abuf_size
? conf_abuf_size
: 4);
2438 int a2
= (conf_abuf_size
>58 ? conf_abuf_size
: 58);
2440 priv
->sys_info
.cnt
= 4;
2442 priv
->sys_info
.streams
[0].id
= 0xb9;
2443 priv
->sys_info
.streams
[0].type
= 1;
2444 priv
->sys_info
.streams
[0].bufsize
= v
*1024;
2446 priv
->sys_info
.streams
[1].id
= 0xb8;
2447 priv
->sys_info
.streams
[1].type
= 0;
2448 priv
->sys_info
.streams
[1].bufsize
= a1
*1024;
2450 priv
->sys_info
.streams
[2].id
= 0xbd;
2451 priv
->sys_info
.streams
[2].type
= 1;
2452 priv
->sys_info
.streams
[2].bufsize
= a2
*1024;
2454 priv
->sys_info
.streams
[3].id
= 0xbf;
2455 priv
->sys_info
.streams
[3].type
= 1;
2456 priv
->sys_info
.streams
[3].bufsize
= 2*1024;
2458 else if(priv
->is_xvcd
|| priv
->is_xsvcd
)
2460 int v
= (conf_vbuf_size
? conf_vbuf_size
: (priv
->is_xvcd
? 46: 230));
2461 int a1
= (conf_abuf_size
? conf_abuf_size
: 4);
2463 priv
->sys_info
.cnt
= 2;
2465 priv
->sys_info
.streams
[0].id
= 0xe0;
2466 priv
->sys_info
.streams
[0].type
= 1;
2467 priv
->sys_info
.streams
[0].bufsize
= v
*1024;
2469 priv
->sys_info
.streams
[1].id
= 0xc0;
2470 priv
->sys_info
.streams
[1].type
= 0;
2471 priv
->sys_info
.streams
[1].bufsize
= a1
*1024;
2474 priv
->sys_info
.cnt
= 0;
2477 /* excerpt from DGPulldown Copyright (C) 2005-2006, Donald Graft */
2478 static void generate_flags(uint8_t *bff_mask
, int source
, int target
)
2480 unsigned int i
, trfp
;
2482 unsigned char ormask
[4] = {0x0, 0x2, 0x80, 0x82};
2484 dfl
= (target
- source
) << 1;
2488 for(i
= 0; i
< MAX_PATTERN_LENGTH
; i
++)
2494 bff_mask
[i
] = ormask
[trfp
+ 1];
2498 bff_mask
[i
] = ormask
[trfp
];
2502 int muxer_init_muxer_mpeg(muxer_t
*muxer
)
2505 priv
= (muxer_priv_t
*) calloc(1, sizeof(muxer_priv_t
));
2508 priv
->update_system_header
= 1;
2510 //calloc() already zero-ed all flags, so we assign only the ones we need
2512 if(conf_mux
!= NULL
)
2514 if(! strcasecmp(conf_mux
, "mpeg1"))
2516 priv
->mux
= MUX_MPEG1
;
2517 priv
->packet_size
= 2048;
2518 priv
->is_genmpeg1
= 1;
2519 priv
->muxrate
= 1800 * 125; //Constrained parameters
2521 else if(! strcasecmp(conf_mux
, "dvd"))
2523 priv
->mux
= MUX_MPEG2
;
2525 priv
->packet_size
= 2048;
2526 priv
->muxrate
= 10080 * 125;
2528 else if(! strcasecmp(conf_mux
, "xsvcd"))
2530 priv
->mux
= MUX_MPEG2
;
2532 priv
->packet_size
= 2324;
2533 priv
->muxrate
= 150*2324;
2534 priv
->ts_allframes
= 1;
2536 else if(! strcasecmp(conf_mux
, "xvcd"))
2538 priv
->mux
= MUX_MPEG1
;
2540 priv
->packet_size
= 2324;
2541 priv
->muxrate
= 75*2352;
2542 priv
->ts_allframes
= 1;
2544 else if(! strcasecmp(conf_mux
, "pes1"))
2546 priv
->mux
= MUX_MPEG1
;
2548 priv
->packet_size
= 2048;
2549 priv
->muxrate
= 10080 * 125;
2550 priv
->ts_allframes
= 1;
2552 else if(! strcasecmp(conf_mux
, "pes2"))
2554 priv
->mux
= MUX_MPEG2
;
2556 priv
->packet_size
= 2048;
2557 priv
->muxrate
= 10080 * 125;
2558 priv
->ts_allframes
= 1;
2562 if(strcasecmp(conf_mux
, "mpeg2"))
2563 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Unknown format %s, default to mpeg2\n", conf_mux
);
2564 priv
->mux
= MUX_MPEG2
;
2565 priv
->is_genmpeg2
= 1;
2566 priv
->packet_size
= 2048;
2567 priv
->muxrate
= 1800 * 125; //Constrained parameters
2571 if(conf_ts_allframes
)
2572 priv
->ts_allframes
= 1;
2573 if(conf_muxrate
> 0)
2574 priv
->muxrate
= conf_muxrate
* 125; // * 1000 / 8
2575 if(conf_packet_size
)
2576 priv
->packet_size
= conf_packet_size
;
2577 priv
->delta_scr
= (uint64_t) (90000.0f
*300.0f
*(double)priv
->packet_size
/(double)priv
->muxrate
);
2578 mp_msg(MSGT_MUXER
, MSGL_INFO
, "PACKET SIZE: %u bytes, deltascr: %"PRIu64
"\n", priv
->packet_size
, priv
->delta_scr
);
2579 setup_sys_params(priv
);
2581 if(conf_vaspect
> 0)
2583 int asp
= (int) (conf_vaspect
* 1000.0f
);
2584 if(asp
>= 1332 && asp
<= 1334)
2585 priv
->vaspect
= ASPECT_4_3
;
2586 else if(asp
>= 1776 && asp
<= 1778)
2587 priv
->vaspect
= ASPECT_16_9
;
2588 else if(asp
>= 2209 && asp
<= 2211)
2589 priv
->vaspect
= ASPECT_2_21_1
;
2590 else if(asp
== 1000)
2591 priv
->vaspect
= ASPECT_1_1
;
2593 mp_msg(MSGT_MUXER
, MSGL_ERR
, "ERROR: unrecognized aspect %.3f\n", conf_vaspect
);
2596 priv
->vframerate
= 0; // no change
2597 if(conf_telecine
&& conf_vframerate
> 0)
2599 mp_msg(MSGT_MUXER
, MSGL_ERR
, "ERROR: options 'telecine' and 'vframerate' are mutually exclusive, vframerate disabled\n");
2600 conf_vframerate
= 0;
2603 if(conf_telecine
== TELECINE_FILM2PAL
)
2605 if(conf_telecine_src
==0.0f
) conf_telecine_src
= 24000.0/1001.0;
2606 conf_telecine_dest
= 25;
2607 conf_telecine
= TELECINE_DGPULLDOWN
;
2609 else if(conf_telecine
== PULLDOWN32
)
2611 if(conf_telecine_src
==0.0f
) conf_telecine_src
= 24000.0/1001.0;
2612 conf_telecine_dest
= 30000.0/1001.0;
2613 conf_telecine
= TELECINE_DGPULLDOWN
;
2616 if(conf_telecine_src
>0 && conf_telecine_dest
>0 && conf_telecine_src
< conf_telecine_dest
)
2620 sfps
= (int) (conf_telecine_src
* 1001 + 0.5);
2621 tfps
= (int) (conf_telecine_dest
* 1001 + 0.5);
2622 if(sfps
% 2 || tfps
% 2)
2628 if(((tfps
- sfps
)>>1) > sfps
)
2630 mp_msg(MSGT_MUXER
, MSGL_ERR
, "ERROR! Framerate increment must be <= 1.5, telecining disabled\n");
2635 generate_flags(priv
->bff_mask
, sfps
, tfps
);
2636 conf_telecine
= TELECINE_DGPULLDOWN
;
2637 conf_vframerate
= conf_telecine_dest
;
2645 fps
= (int) (conf_vframerate
* 1001 + 0.5);
2649 priv
->vframerate
= FRAMERATE_23976
;
2652 priv
->vframerate
= FRAMERATE_24
;
2655 priv
->vframerate
= FRAMERATE_25
;
2658 priv
->vframerate
= FRAMERATE_2997
;
2661 priv
->vframerate
= FRAMERATE_30
;
2664 priv
->vframerate
= FRAMERATE_50
;
2667 priv
->vframerate
= FRAMERATE_5994
;
2670 priv
->vframerate
= FRAMERATE_60
;
2674 mp_msg(MSGT_MUXER
, MSGL_ERR
, "WRONG FPS: %d/1000, ignoring\n", fps
);
2676 mp_msg(MSGT_MUXER
, MSGL_ERR
, "DISABLED TELECINING\n");
2682 priv
->vwidth
= (uint16_t) conf_vwidth
;
2683 priv
->vheight
= (uint16_t) conf_vheight
;
2684 priv
->panscan_width
= (uint16_t) conf_panscan_width
;
2685 priv
->panscan_height
= (uint16_t) conf_panscan_height
;
2686 priv
->vbitrate
= ((conf_vbitrate
) * 10) >> 2; //*1000 / 400
2688 if(priv
->vaspect
|| priv
->vframerate
|| priv
->vwidth
|| priv
->vheight
|| priv
->vbitrate
|| priv
->panscan_width
|| priv
->panscan_height
)
2690 priv
->patch_seq
= priv
->vaspect
|| priv
->vframerate
|| priv
->vwidth
|| priv
->vheight
|| priv
->vbitrate
;
2691 priv
->patch_sde
= priv
->panscan_width
|| priv
->panscan_height
;
2692 mp_msg(MSGT_MUXER
, MSGL_INFO
, "MPEG MUXER, patching");
2693 if(priv
->vwidth
|| priv
->vheight
)
2694 mp_msg(MSGT_MUXER
, MSGL_INFO
, " resolution to %dx%d", priv
->vwidth
, priv
->vheight
);
2695 if(priv
->panscan_width
|| priv
->panscan_height
)
2696 mp_msg(MSGT_MUXER
, MSGL_INFO
, " panscan to to %dx%d", priv
->panscan_width
, priv
->panscan_height
);
2697 if(priv
->vframerate
)
2698 mp_msg(MSGT_MUXER
, MSGL_INFO
, " framerate to %s fps", framerates
[priv
->vframerate
]);
2700 mp_msg(MSGT_MUXER
, MSGL_INFO
, " aspect ratio to %s", aspect_ratios
[priv
->vaspect
]);
2702 mp_msg(MSGT_MUXER
, MSGL_INFO
, " bitrate to %u", conf_vbitrate
);
2703 mp_msg(MSGT_MUXER
, MSGL_INFO
, "\n");
2706 priv
->has_video
= priv
->has_audio
= 0;
2708 muxer
->sysrate
= priv
->muxrate
; // initial muxrate = constrained stream parameter
2709 priv
->scr
= muxer
->file_end
= 0;
2711 if(conf_init_vdelay
&& conf_drop
)
2713 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\nmuxer_mpg, :drop and :vdelay used together are not supported, exiting\n");
2716 if(conf_init_adelay
)
2717 priv
->init_adelay
= - (double) conf_init_adelay
/ (double) 1000.0;
2719 priv
->drop
= conf_drop
;
2721 priv
->buff
= (uint8_t *) malloc(priv
->packet_size
);
2722 if((priv
->buff
== NULL
))
2724 mp_msg(MSGT_MUXER
, MSGL_ERR
, "\nCouldn't allocate %d bytes, exit\n", priv
->packet_size
);
2728 muxer
->priv
= (void *) priv
;
2729 muxer
->cont_new_stream
= &mpegfile_new_stream
;
2730 muxer
->cont_write_chunk
= &mpegfile_write_chunk
;
2731 muxer
->cont_write_header
= &mpegfile_write_header
;
2732 muxer
->cont_write_index
= &mpegfile_write_index
;
2733 muxer
->fix_stream_parameters
= &fix_parameters
;