1 /* global.h, global variables, function prototypes */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
30 #include "libzmpeg3.h"
36 /* choose between declaration (GLOBAL_ undefined)
37 * and definition (GLOBAL_ defined)
38 * GLOBAL_ is defined in exactly one file (mpeg2enc.c)
42 #define EXTERN_ extern
47 /* global variables */
51 EXTERN_ unsigned char mpeg2_zig_zag_scan[64]
55 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
56 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
57 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
58 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
64 EXTERN_ unsigned char alternate_scan_hv[64]
68 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
69 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
70 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
71 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
76 /* default intra quantization matrix */
77 EXTERN_ uint16_t default_intra_quantizer_matrix_hv[64]
81 8, 16, 19, 22, 26, 27, 29, 34,
82 16, 16, 22, 24, 27, 29, 34, 37,
83 19, 22, 26, 27, 29, 34, 34, 38,
84 22, 22, 26, 27, 29, 34, 37, 40,
85 22, 26, 27, 29, 32, 35, 40, 48,
86 26, 27, 29, 32, 35, 40, 48, 58,
87 26, 27, 29, 34, 38, 46, 56, 69,
88 27, 29, 35, 38, 46, 56, 69, 83
93 EXTERN_ uint16_t hires_intra_quantizer_matrix_hv[64]
97 8, 16, 18, 20, 24, 25, 26, 30,
98 16, 16, 20, 23, 25, 26, 30, 30,
99 18, 20, 22, 24, 26, 28, 29, 31,
100 20, 21, 23, 24, 26, 28, 31, 31,
101 21, 23, 24, 25, 28, 30, 30, 33,
102 23, 24, 25, 28, 30, 30, 33, 36,
103 24, 25, 26, 29, 29, 31, 34, 38,
104 25, 26, 28, 29, 31, 34, 38, 42
109 /* Our default non intra quantization matrix
110 This is *not* the MPEG default
112 EXTERN_ uint16_t default_nonintra_quantizer_matrix_hv[64]
117 16, 17, 18, 19, 20, 21, 22, 23,
118 17, 18, 19, 20, 21, 22, 23, 24,
119 18, 19, 20, 21, 22, 23, 24, 25,
120 19, 20, 21, 22, 23, 24, 26, 27,
121 20, 21, 22, 23, 25, 26, 27, 28,
122 21, 22, 23, 24, 26, 27, 28, 30,
123 22, 23, 24, 26, 27, 28, 30, 31,
124 23, 24, 25, 27, 28, 30, 31, 33
130 /* Hires non intra quantization matrix. THis *is*
131 the MPEG default... */
132 EXTERN_ uint16_t hires_nonintra_quantizer_matrix_hv[64]
136 16, 16, 16, 16, 16, 16, 16, 16,
137 16, 16, 16, 16, 16, 16, 16, 16,
138 16, 16, 16, 16, 16, 16, 16, 16,
139 16, 16, 16, 16, 16, 16, 16, 16,
140 16, 16, 16, 16, 16, 16, 16, 16,
141 16, 16, 16, 16, 16, 16, 16, 16,
142 16, 16, 16, 16, 16, 16, 16, 16,
143 16, 16, 16, 16, 16, 16, 16, 16
148 /* non-linear quantization coefficient table */
149 EXTERN_ unsigned char non_linear_mquant_table_hv[32]
153 0, 1, 2, 3, 4, 5, 6, 7,
154 8,10,12,14,16,18,20,22,
155 24,28,32,36,40,44,48,52,
156 56,64,72,80,88,96,104,112
161 /* non-linear mquant table for mapping from scale to code
162 * since reconstruction levels are not bijective with the index map,
163 * it is up to the designer to determine most of the quantization levels
166 EXTERN_ unsigned char map_non_linear_mquant_hv[113]
170 0,1,2,3,4,5,6,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
171 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
172 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
173 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
174 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
186 typedef struct mc_result mc_result_s;
188 typedef struct motion_comp
190 uint8_t **oldorg, **neworg;
191 uint8_t **oldref, **newref;
192 uint8_t **cur, **curref;
193 int sxf, syf, sxb, syb;
196 typedef struct mbinfo mbinfo_s;
198 typedef struct pict_data
201 /* picture structure (header) data */
203 int temp_ref; /* temporal reference */
204 int pict_type; /* picture coding type (I, P or B) */
205 int vbv_delay; /* video buffering verifier delay (1/90000 seconds) */
206 int forw_hor_f_code, forw_vert_f_code;
207 int back_hor_f_code, back_vert_f_code; /* motion vector ranges */
208 int dc_prec; /* DC coefficient prec for intra blocks */
209 int pict_struct; /* picture structure (frame, top / bottom) */
210 int topfirst; /* display top field first */
211 int frame_pred_dct; /* Use only frame prediction... */
212 int intravlc; /* Intra VLC format */
213 int q_scale_type; /* Quantiser scale... */
214 int altscan; /* Alternate scan */
215 int repeatfirst; /* repeat first field after second field */
216 int prog_frame; /* progressive frame */
218 /* 8*8 block data, raw (unquantised) and quantised, and (eventually but
219 not yet inverse quantised */
220 int16_t (*blocks)[64];
221 int16_t (*qblocks)[64];
223 unsigned char **curorg, **curref;
224 /* macroblock side information array */
226 /* motion estimation parameters */
231 int start_row, end_row;
232 pthread_mutex_t input_lock, output_lock;
233 pthread_t tid; /* ID of thread */
236 motion_comp_s *motion_comp;
237 pict_data_s *pict_data;
241 #define MAX_44_MATCHES (256*256/(4*4))
242 int sub22_num_mcomps;
243 int sub44_num_mcomps;
244 mc_result_s sub44_mcomps[MAX_44_MATCHES];
245 mc_result_s sub22_mcomps[MAX_44_MATCHES*4];
251 pthread_mutex_t ratectl_lock;
259 double next_ip_delay; /* due to frame reordering delay */
260 double decoding_time;
261 int Xi, Xp, Xb, r, d0i, d0p, d0b;
270 * next_ip_delay = 0.0;
271 * decoding_time = 0.0;
272 * P = 0; // P distance between complete intra slice refresh
273 * r = 0; // r (reaction parameter)
274 * avg_act = 0; // avg_act (initial average activity)
275 * Xi = 0; // Xi (initial I frame global complexity measure)
276 * Xp = 0; // Xp (initial P frame global complexity measure)
277 * Xb = 0; // Xb (initial B frame global complexity measure)
278 * d0i = 0; // d0i (initial I frame virtual buffer fullness)
279 * d0p = 0; // d0p (initial P frame virtual buffer fullness)
280 * d0b = 0; // d0b (initial B frame virtual buffer fullness)
282 pthread_mutex_t ratectl_lock;
293 /* bitcnt_EOP - Position in generated bit-stream for latest
294 end-of-picture Comparing these values with the
295 bit-stream position for when the picture is due to be
296 displayed allows us to see what the vbv buffer is up
299 gop_undershoot - If we *undershoot* our bit target the vbv buffer
300 calculations based on the actual length of the
301 bitstream will be wrong because in the final system
302 stream these bits will be padded away. I.e. frames
303 *won't* arrive as early as the length of the video
304 stream would suggest they would. To get it right we
305 have to keep track of the bits that would appear in
315 actsum - Total activity (sum block variances) in frame
316 actcovered - Activity macroblocks so far quantised (used to
317 fine tune quantisation to avoid starving highly
318 active blocks appearing late in frame...) UNUSED
319 avg_act - Current average activity...
331 /* Note: eventually we may wish to tweak these to suit image content */
332 double Ki; /* Down-scaling of I/B/P-frame complexity */
333 double Kb; /* relative to others in bit-allocation */
334 double Kp; /* calculations. We only need 2 but have all
340 /* TODO EXPERIMENT */
341 double avg_KI; /* TODO: These values empirically determined */
342 double avg_KB; /* for MPEG-1, may need tuning for MPEG-2 */
344 #define K_AVG_WINDOW_I 4.0 /* TODO: MPEG-1, hard-wired settings */
345 #define K_AVG_WINDOW_P 10.0
346 #define K_AVG_WINDOW_B 20.0
352 double current_quant;
359 int start_row, end_row;
360 pthread_mutex_t input_lock, output_lock;
361 pthread_t tid; /* ID of thread */
364 pict_data_s *picture;
365 unsigned char **pred;
368 unsigned char temp[128];
369 } transform_engine_t;
373 int start_row, end_row;
374 pthread_mutex_t input_lock, output_lock;
375 pthread_t tid; /* ID of thread */
380 /* prediction values for DCT coefficient (0,0) */
383 unsigned char *frame;
384 unsigned char *slice_buffer;
386 long slice_allocated;
387 pict_data_s *picture;
389 unsigned char outbfr;
393 EXTERN_ pthread_mutex_t test_lock;
394 EXTERN_ motion_engine_t *motion_engines;
395 EXTERN_ transform_engine_t *transform_engines;
396 EXTERN_ transform_engine_t *itransform_engines;
397 EXTERN_ slice_engine_t *slice_engines;
398 EXTERN_ ratectl_t **ratectl;
399 EXTERN_ int quiet; /* suppress warnings */
404 EXTERN_ pict_data_s cur_picture;
406 /* reconstructed frames */
407 EXTERN_ unsigned char *newrefframe[3], *oldrefframe[3], *auxframe[3];
408 /* original frames */
409 EXTERN_ unsigned char *neworgframe[3], *oldorgframe[3], *auxorgframe[3];
410 /* prediction of current frame */
411 EXTERN_ unsigned char *predframe[3];
412 /* motion estimation parameters */
413 EXTERN_ struct motion_data *motion_data;
415 /* SCale factor for fast integer arithmetic routines */
416 /* Changed this and you *must* change the quantisation routines as they depend on its absolute
418 #define IQUANT_SCALE_POW2 16
419 #define IQUANT_SCALE (1<<IQUANT_SCALE_POW2)
420 #define COEFFSUM_SCALE (1<<16)
422 /* Orginal intra / non_intra quantization matrices */
423 EXTERN_ uint16_t intra_q[64], inter_q[64];
424 EXTERN_ uint16_t i_intra_q[64], i_inter_q[64];
426 /* Table driven intra / non-intra quantization matrices */
427 EXTERN_ uint16_t intra_q_tbl[113][64], inter_q_tbl[113][64];
428 EXTERN_ uint16_t i_intra_q_tbl[113][64], i_inter_q_tbl[113][64];
429 EXTERN_ float intra_q_tblf[113][64], inter_q_tblf[113][64];
430 EXTERN_ float i_intra_q_tblf[113][64], i_inter_q_tblf[113][64];
432 EXTERN_ uint16_t chrom_intra_q[64],chrom_inter_q[64];
438 /* clipping (=saturation) table */
439 EXTERN_ unsigned char *clp;
442 EXTERN_ char id_string[256], tplorg[256], tplref[256], out_path[256];
443 EXTERN_ char iqname[256], niqname[256];
444 EXTERN_ char statname[256];
445 EXTERN_ char errortext[256];
447 EXTERN_ FILE *outfile; /* file descriptors */
448 EXTERN_ FILE *statfile; /* file descriptors */
449 EXTERN_ int inputtype; /* format of input frames */
452 How many frames to read ahead (eventually intended to support
453 scene change based GOP structuring. READ_LOOK_AHEAD/2 must be
454 greater than M (otherwise buffers will be overwritten that are
457 It should also be a multiple of 4 due to the way buffers are
461 #define READ_LOOK_AHEAD 4
462 EXTERN_ uint8_t ***frame_buffers;
465 /* These determine what input format to use */
466 EXTERN_ mpeg3_t *mpeg_file;
467 EXTERN_ int do_stdin;
468 EXTERN_ FILE *stdin_fd;
471 EXTERN_ int do_buffers;
472 EXTERN_ pthread_mutex_t input_lock;
473 EXTERN_ pthread_mutex_t output_lock;
474 EXTERN_ pthread_mutex_t copy_lock;
475 EXTERN_ char *input_buffer_y;
476 EXTERN_ char *input_buffer_u;
477 EXTERN_ char *input_buffer_v;
478 EXTERN_ int input_buffer_end;
482 EXTERN_ unsigned char *frame_buffer;
483 EXTERN_ unsigned char **row_pointers;
484 EXTERN_ int fixed_mquant;
485 EXTERN_ double quant_floor; /* quantisation floor [1..10] (0 for CBR) */
486 EXTERN_ double act_boost; /* Quantisation reduction for highly active blocks */
487 EXTERN_ int use_hires_quant;
488 EXTERN_ int use_denoise_quant;
489 /* Number of processors */
490 EXTERN_ int processors;
491 EXTERN_ long start_frame, end_frame; /* Range to encode in source framerate units */
492 EXTERN_ int seq_header_every_gop;
494 /* coding model parameters */
496 EXTERN_ int N; /* number of frames in Group of Pictures */
497 EXTERN_ int M; /* distance between I/P frames */
498 EXTERN_ int P; /* intra slice refresh interval */
499 EXTERN_ int nframes; /* total number of frames to encode */
500 EXTERN_ long frames_scaled; /* frame count normalized to output frame rate */
501 EXTERN_ int frame0, tc0; /* number and timecode of first frame */
502 EXTERN_ int mpeg1; /* ISO/IEC IS 11172-2 sequence */
503 EXTERN_ int fieldpic; /* use field pictures */
505 /* sequence specific data (sequence header) */
507 EXTERN_ int qsubsample_offset,
510 colsums_offset; /* Offset from picture buffer start of sub-sampled data... */
511 EXTERN_ int mb_per_pict; /* Number of macro-blocks in a picture */
512 EXTERN_ int fast_mc_frac; /* inverse proportion of fast motion estimates
513 consider in detail */
514 EXTERN_ int mc_44_red; /* Sub-mean population reduction passes for 4x4 and 2x2 */
515 EXTERN_ int mc_22_red; /* Motion compensation stages */
519 EXTERN_ int horizontal_size, vertical_size; /* frame size (pels) */
520 EXTERN_ int width, height; /* encoded frame size (pels) multiples of 16 or 32 */
521 EXTERN_ int chrom_width, chrom_height, block_count;
522 EXTERN_ int mb_width, mb_height; /* frame size (macroblocks) */
523 EXTERN_ int width2, height2, mb_height2, chrom_width2; /* picture size adjusted for interlacing */
524 EXTERN_ int aspectratio; /* aspect ratio information (pel or display) */
525 EXTERN_ int frame_rate_code; /* coded value of frame rate */
526 EXTERN_ int dctsatlim; /* Value to saturated DCT coeffs to */
527 EXTERN_ double frame_rate; /* frames per second */
528 EXTERN_ double input_frame_rate; /* input frame rate */
529 EXTERN_ double bit_rate; /* bits per second */
530 EXTERN_ int video_buffer_size;
531 EXTERN_ int vbv_buffer_size; /* size of VBV buffer (* 16 kbit) */
532 EXTERN_ int constrparms; /* constrained parameters flag (MPEG-1 only) */
533 EXTERN_ int load_iquant, load_niquant; /* use non-default quant. matrices */
534 EXTERN_ int load_ciquant,load_cniquant;
537 /* sequence specific data (sequence extension) */
539 EXTERN_ int profile, level; /* syntax / parameter constraints */
540 EXTERN_ int prog_seq; /* progressive sequence */
541 EXTERN_ int chroma_format;
542 EXTERN_ int low_delay; /* no B pictures, skipped pictures */
545 /* sequence specific data (sequence display extension) */
547 EXTERN_ int video_format; /* component, PAL, NTSC, SECAM or MAC */
548 EXTERN_ int color_primaries; /* source primary chromaticity coordinates */
549 EXTERN_ int transfer_characteristics; /* opto-electronic transfer char. (gamma) */
550 EXTERN_ int matrix_coefficients; /* Eg,Eb,Er / Y,Cb,Cr matrix coefficients */
551 EXTERN_ int display_horizontal_size, display_vertical_size; /* display size */
554 /* picture specific data (picture coding extension) */
555 EXTERN_ int opt_dc_prec;
556 EXTERN_ int opt_prog_frame;
557 EXTERN_ int opt_repeatfirst;
558 EXTERN_ int opt_topfirst;
560 /* use only frame prediction and frame DCT (I,P,B,current) */
561 EXTERN_ int frame_pred_dct_tab[3];
562 EXTERN_ int conceal_tab[3]; /* use concealment motion vectors (I,P,B) */
563 EXTERN_ int qscale_tab[3]; /* linear/non-linear quantizaton table */
564 EXTERN_ int intravlc_tab[3]; /* intra vlc format (I,P,B,current) */
565 EXTERN_ int altscan_tab[3]; /* alternate scan (I,P,B,current) */
567 /* prototypes of global functions */
570 void range_checks _ANSI_ARGS_((void));
571 void profile_and_level_checks _ANSI_ARGS_(());
574 void init_fdct _ANSI_ARGS_((void));
577 void init_idct _ANSI_ARGS_((void));
580 void motion_estimation _ANSI_ARGS_((pict_data_s *picture,
581 motion_comp_s *mc_data,
582 int secondfield, int ipflag));
585 void error _ANSI_ARGS_((char *text));
588 void predict _ANSI_ARGS_((pict_data_s *picture,
595 void slice_initbits(slice_engine_t *engine);
596 void slice_putbits(slice_engine_t *engine, long val, int n);
597 void slice_alignbits(slice_engine_t *engine);
598 void slice_finishslice(slice_engine_t *engine);
601 void mpeg2_initbits _ANSI_ARGS_((void));
602 void putbits _ANSI_ARGS_((int val, int n));
603 void alignbits _ANSI_ARGS_((void));
604 double bitcount _ANSI_ARGS_((void));
607 void putseqhdr _ANSI_ARGS_((void));
608 void putseqext _ANSI_ARGS_((void));
609 void putseqdispext _ANSI_ARGS_((void));
610 void putuserdata _ANSI_ARGS_((char *userdata));
611 void putgophdr _ANSI_ARGS_((int frame, int closed_gop));
612 void putpicthdr _ANSI_ARGS_((pict_data_s *picture));
613 void putpictcodext _ANSI_ARGS_((pict_data_s *picture));
614 void putseqend _ANSI_ARGS_((void));
617 void putintrablk _ANSI_ARGS_((slice_engine_t *engine, pict_data_s *picture, short *blk, int cc));
618 void putnonintrablk _ANSI_ARGS_((slice_engine_t *engine, pict_data_s *picture, short *blk));
619 void putmv _ANSI_ARGS_((slice_engine_t *engine, int dmv, int f_code));
622 void putpict _ANSI_ARGS_((pict_data_s *picture));
625 void putseq _ANSI_ARGS_((void));
628 void putDClum _ANSI_ARGS_((slice_engine_t *engine, int val));
629 void putDCchrom _ANSI_ARGS_((slice_engine_t *engine, int val));
630 void putACfirst _ANSI_ARGS_((slice_engine_t *engine, int run, int val));
631 void putAC _ANSI_ARGS_((slice_engine_t *engine, int run, int signed_level, int vlcformat));
632 void putaddrinc _ANSI_ARGS_((slice_engine_t *engine, int addrinc));
633 void putmbtype _ANSI_ARGS_((slice_engine_t *engine, int pict_type, int mb_type));
634 void putmotioncode _ANSI_ARGS_((slice_engine_t *engine, int motion_code));
635 void putdmv _ANSI_ARGS_((slice_engine_t *engine, int dmv));
636 void putcbp _ANSI_ARGS_((slice_engine_t *engine, int cbp));
638 extern int (*pquant_non_intra)(pict_data_s *picture, int16_t *src, int16_t *dst,
639 int mquant, int *nonsat_mquant);
641 extern int (*pquant_weight_coeff_sum)(int16_t *blk, uint16_t*i_quant_mat );
645 void iquantize( pict_data_s *picture );
646 void quant_intra_hv ( pict_data_s *picture,
647 int16_t *src, int16_t *dst,
648 int mquant, int *nonsat_mquant);
649 int quant_non_intra_hv( pict_data_s *picture,
650 int16_t *src, int16_t *dst,
651 int mquant, int *nonsat_mquant);
652 void iquant_intra ( int16_t *src, int16_t *dst, int dc_prec, int mquant);
653 void iquant_non_intra (int16_t *src, int16_t *dst, int mquant);
654 void init_quantizer_hv();
655 int next_larger_quant_hv( pict_data_s *picture, int quant );
657 extern int (*pquant_non_intra)(pict_data_s *picture, int16_t *src, int16_t *dst,
658 int mquant, int *nonsat_mquant);
660 extern int (*pquant_weight_coeff_sum)(int16_t *blk, uint16_t*i_quant_mat );
663 void ratectl_init_seq _ANSI_ARGS_((ratectl_t *ratectl));
664 void ratectl_init_GOP _ANSI_ARGS_((ratectl_t *ratectl, int np, int nb));
665 void ratectl_init_pict _ANSI_ARGS_((ratectl_t *ratectl, pict_data_s *picture));
666 void ratectl_update_pict _ANSI_ARGS_((ratectl_t *ratectl, pict_data_s *picture));
667 int ratectl_start_mb _ANSI_ARGS_((ratectl_t *ratectl, pict_data_s *picture));
668 int ratectl_calc_mquant _ANSI_ARGS_((ratectl_t *ratectl, pict_data_s *picture, int j));
669 void vbv_end_of_picture _ANSI_ARGS_((void));
670 void calc_vbv_delay _ANSI_ARGS_((void));
673 void readframe _ANSI_ARGS_((int frame_num, uint8_t *frame[]));
676 void calcSNR _ANSI_ARGS_((unsigned char *org[3], unsigned char *rec[3]));
677 void stats _ANSI_ARGS_((void));
680 void transform _ANSI_ARGS_((pict_data_s *picture,
683 void itransform _ANSI_ARGS_((pict_data_s *picture,
684 uint8_t *pred[], uint8_t *cur[]));
685 void dct_type_estimation _ANSI_ARGS_((pict_data_s *picture,
686 uint8_t *pred, uint8_t *cur));