r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / mpeg2enc / mpeg2enc.c
blobf634597a40dca49862a4ebc6ebb0c786a2d464de
1 /* mpeg2enc.c, main() and parameter file reading */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
5 /*
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
21 * patents.
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
26 * design.
30 #define MAX(a,b) ( (a)>(b) ? (a) : (b) )
31 #include <math.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #define GLOBAL_ /* used by global.h */
37 #include "config.h"
38 #include "global.h"
40 /* private prototypes */
41 static void init _ANSI_ARGS_((void));
42 static void readcmdline _ANSI_ARGS_((int argc, char *argv[]));
43 static void readquantmat _ANSI_ARGS_((void));
46 // Hack for libdv to remove glib dependancy
48 void
49 g_log (const char *log_domain,
50 int log_level,
51 const char *format,
52 ...)
56 void
57 g_logv (const char *log_domain,
58 int log_level,
59 const char *format,
60 ...)
65 void mpeg2enc_set_w(int width)
67 horizontal_size = width;
70 void mpeg2enc_set_h(int height)
72 vertical_size = height;
75 void mpeg2enc_set_rate(double rate)
77 input_frame_rate = rate;
80 void mpeg2enc_set_input_buffers(int eof, char *y, char *u, char *v)
82 pthread_mutex_lock(&output_lock);
83 input_buffer_end = eof;
84 input_buffer_y = y;
85 input_buffer_u = u;
86 input_buffer_v = v;
87 pthread_mutex_unlock(&input_lock);
88 // Wait for buffers to get copied before returning.
89 pthread_mutex_lock(&copy_lock);
92 void mpeg2enc_init_buffers()
94 pthread_mutex_init(&input_lock, 0);
95 pthread_mutex_init(&output_lock, 0);
96 pthread_mutex_init(&copy_lock, 0);
97 pthread_mutex_lock(&input_lock);
98 pthread_mutex_lock(&copy_lock);
99 input_buffer_end = 0;
102 int mpeg2enc(int argc, char *argv[])
104 stdin_fd = stdin;
106 verbose = 1;
108 printf("mpeg2enc 1\n");
110 /* Read command line */
111 readcmdline(argc, argv);
112 printf("mpeg2enc 2\n");
114 /* read quantization matrices */
115 readquantmat();
116 printf("mpeg2enc 3\n");
118 if(!strlen(out_path))
120 fprintf(stderr, "No output file given.\n");
122 printf("mpeg2enc 4\n");
124 /* open output file */
125 if(!(outfile = fopen(out_path, "wb")))
127 sprintf(errortext,"Couldn't create output file %s", out_path);
128 error(errortext);
130 printf("mpeg2enc 5\n");
132 init();
133 //printf("mpeg2enc 6\n");
135 if(nframes < 0x7fffffff)
136 printf("Frame Completion Current bitrate Predicted file size\n");
137 putseq();
139 stop_slice_engines();
140 stop_motion_engines();
141 stop_transform_engines();
142 stop_itransform_engines();
144 fclose(outfile);
145 fclose(statfile);
147 if(qt_file) quicktime_close(qt_file);
148 if(qt_output) quicktime_close(qt_output);
149 if(mpeg_file) mpeg3_close(mpeg_file);
151 if(do_stdin)
153 fclose(stdin_fd);
155 pthread_mutex_destroy(&input_lock);
156 pthread_mutex_destroy(&output_lock);
157 return 0;
160 int HorzMotionCode(int i)
162 if (i < 8)
163 return 1;
164 if (i < 16)
165 return 2;
166 if (i < 32)
167 return 3;
168 if ((i < 64) || (constrparms))
169 return 4;
170 if (i < 128)
171 return 5;
172 if (i < 256)
173 return 6;
174 if ((i < 512) || (level == 10))
175 return 7;
176 if ((i < 1024) || (level == 8))
177 return 8;
178 if (i < 2048)
179 return 9;
180 return 1;
183 int VertMotionCode(int i)
185 if (i < 8)
186 return 1;
187 if (i < 16)
188 return 2;
189 if (i < 32)
190 return 3;
191 if ((i < 64) || (level == 10) || (constrparms))
192 return 4;
193 return 5;
197 Wrapper for malloc that allocates pbuffers aligned to the
198 specified byte boundary and checks for failure.
199 N.b. don't try to free the resulting pointers, eh...
200 BUG: Of course this won't work if a char * won't fit in an int....
202 static uint8_t *bufalloc( size_t size )
204 char *buf = malloc( size + BUFFER_ALIGN );
205 int adjust;
207 if( buf == NULL )
209 error("malloc failed\n");
211 adjust = BUFFER_ALIGN-((int)buf)%BUFFER_ALIGN;
212 if( adjust == BUFFER_ALIGN )
213 adjust = 0;
214 return (uint8_t*)(buf+adjust);
217 static void init()
219 int i, n, size;
220 static int block_count_tab[3] = {6,8,12};
221 int lum_buffer_size, chrom_buffer_size;
222 pthread_mutexattr_t mutex_attr;
223 pthread_mutexattr_init(&mutex_attr);
224 pthread_mutex_init(&test_lock, &mutex_attr);
226 bzero(&cur_picture, sizeof(pict_data_s));
227 mpeg2_initbits();
228 init_fdct();
229 init_idct();
230 init_motion();
231 init_predict();
232 init_quantizer();
233 init_transform();
235 /* round picture dimensions to nZearest multiple of 16 or 32 */
236 mb_width = (horizontal_size+15)/16;
237 mb_height = prog_seq ?
238 (vertical_size + 15) / 16 :
239 2 * ((vertical_size + 31) / 32);
240 mb_height2 = fieldpic ?
241 mb_height >> 1 :
242 mb_height; /* for field pictures */
243 width = 16 * mb_width;
244 height = 16 * mb_height;
246 chrom_width = (chroma_format==CHROMA444) ? width : width>>1;
247 chrom_height = (chroma_format!=CHROMA420) ? height : height>>1;
249 height2 = fieldpic ? height>>1 : height;
250 width2 = fieldpic ? width<<1 : width;
251 chrom_width2 = fieldpic ? chrom_width<<1 : chrom_width;
253 block_count = block_count_tab[chroma_format-1];
254 lum_buffer_size = (width*height) +
255 sizeof(uint8_t) *(width/2)*(height/2) +
256 sizeof(uint8_t) *(width/4)*(height/4+1);
257 chrom_buffer_size = chrom_width*chrom_height;
259 fsubsample_offset = (width)*(height) * sizeof(uint8_t);
260 qsubsample_offset = fsubsample_offset + (width/2)*(height/2)*sizeof(uint8_t);
262 rowsums_offset = 0;
263 colsums_offset = 0;
265 mb_per_pict = mb_width*mb_height2;
267 /* clip table */
268 if (!(clp = (unsigned char *)malloc(1024)))
269 error("malloc failed\n");
270 clp+= 384;
271 for (i=-384; i<640; i++)
272 clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
276 /* Allocate the frame buffer */
279 frame_buffers = (uint8_t ***)
280 bufalloc(2*READ_LOOK_AHEAD*sizeof(uint8_t**));
282 for(n=0;n<2*READ_LOOK_AHEAD;n++)
284 frame_buffers[n] = (uint8_t **) bufalloc(3*sizeof(uint8_t*));
285 for (i=0; i<3; i++)
287 frame_buffers[n][i] =
288 bufalloc( (i==0) ? lum_buffer_size : chrom_buffer_size );
294 /* TODO: The ref and aux frame buffers are no redundant! */
295 for( i = 0 ; i<3; i++)
297 int size = (i==0) ? lum_buffer_size : chrom_buffer_size;
298 newrefframe[i] = bufalloc(size);
299 oldrefframe[i] = bufalloc(size);
300 auxframe[i] = bufalloc(size);
301 predframe[i] = bufalloc(size);
304 cur_picture.qblocks =
305 (int16_t (*)[64])bufalloc(mb_per_pict*block_count*sizeof(int16_t [64]));
307 /* Initialise current transformed picture data tables
308 These will soon become a buffer for transformed picture data to allow
309 look-ahead for bit allocation etc.
311 cur_picture.mbinfo = (
312 struct mbinfo *)bufalloc(mb_per_pict*sizeof(struct mbinfo));
314 cur_picture.blocks =
315 (int16_t (*)[64])bufalloc(mb_per_pict * block_count * sizeof(int16_t [64]));
318 /* open statistics output file */
319 if(statname[0]=='-') statfile = stdout;
320 else
321 if(!(statfile = fopen(statname,"w")))
323 sprintf(errortext,"Couldn't create statistics output file %s",statname);
324 error(errortext);
327 ratectl = malloc(processors * sizeof(ratectl_t*));
328 for(i = 0; i < processors; i++)
329 ratectl[i] = calloc(1, sizeof(ratectl_t));
333 /* Start parallel threads */
335 //printf("init 1\n");
336 start_motion_engines();
337 //printf("init 2\n");
338 start_transform_engines();
339 //printf("init 3\n");
340 start_itransform_engines();
341 //printf("init 4\n");
342 start_slice_engines();
343 //printf("init 5\n");
346 void error(text)
347 char *text;
349 fprintf(stderr,text);
350 putc('\n',stderr);
351 exit(1);
354 #define STRINGLEN 254
356 int calculate_smp()
358 /* Get processor count */
359 int result = 1;
360 FILE *proc;
361 if(proc = fopen("/proc/cpuinfo", "r"))
363 char string[1024];
364 while(!feof(proc))
366 fgets(string, 1024, proc);
367 if(!strncasecmp(string, "processor", 9))
369 char *ptr = strchr(string, ':');
370 if(ptr)
372 ptr++;
373 result = atol(ptr) + 1;
376 else
377 if(!strncasecmp(string, "cpus detected", 13))
379 char *ptr = strchr(string, ':');
380 if(ptr)
382 ptr++;
383 result = atol(ptr);
387 fclose(proc);
389 return result;
392 static void readcmdline(int argc, char *argv[])
394 int i, j;
395 int h,m,s,f;
396 FILE *fd;
397 char line[256];
398 // Master frame rate table must match decoder
399 static double ratetab[]=
400 {24000.0/1001.0, // Official rates
401 24.0,
402 25.0,
403 30000.0/1001.0,
404 30.0,
405 50.0,
406 60000.0/1001.0,
407 60.0,
409 1, // Unofficial economy rates
412 12,
416 // VBV buffer size limits
417 int vbvlim[4] = { 597, 448, 112, 29 };
418 long total_frame_rates = (sizeof(ratetab) / sizeof(double));
419 FILE *proc;
420 // Default 16
421 int param_searchrad = 16;
422 int isnum = 1;
424 //printf("readcmdline 1\n");
425 frame0 = 0; /* number of first frame */
426 start_frame = end_frame = -1;
427 use_hires_quant = 0;
428 use_denoise_quant = 0;
429 quiet = 1;
430 bit_rate = 5000000; /* default bit_rate (bits/s) */
431 prog_seq = 0; /* progressive_sequence is faster */
432 mpeg1 = 0; /* ISO/IEC 11172-2 stream */
433 fixed_mquant = 0; /* vary the quantization */
434 quant_floor = 0;
435 act_boost = 3.0;
436 N = 15; /* N (# of frames in GOP) */
437 M = 1; /* M (I/P frame distance) */
438 processors = calculate_smp();
439 frame_rate = -1;
440 chroma_format = 1; /* chroma_format: 1=4:2:0, 2=4:2:2, 3=4:4:4 LibMPEG3 only does 1 */
441 mpeg_file = 0;
442 qt_file = 0;
443 do_stdin = 0;
444 do_buffers = 1;
445 seq_header_every_gop = 0;
451 //printf("readcmdline 2\n");
454 sprintf(tplorg, "");
455 sprintf(out_path, "");
457 #define INTTOYES(x) ((x) ? "Yes" : "No")
458 if(argc < 2)
460 printf("mpeg2encode V1.3, 2000/01/10\n"
461 "(C) 1996, MPEG Software Simulation Group\n"
462 "(C) 2001 Heroine Virtual\n"
463 "Usage: %s [options] <input file> <output file>\n\n"
464 " -1 generate an MPEG-1 stream instead of MPEG-2 (%s)\n"
465 " -422 generate YUV 4:2:2 output\n"
466 " -b bitrate fix the bitrate, vary the quantization (%d)\n"
467 " -d Denoise (%s)\n"
468 " -f rate Convert framerate\n"
469 " -h High resolution quantization table (%s)\n"
470 " -m frames set number of frames between P frames (%d)\n"
471 " -n frames set number of frames between I frames (%d)\n"
472 " -p encode progressive frames (%s)\n"
473 " -q quantization fix the quantization, vary the bitrate\n"
474 " [number] Start encoding from frame number to end\n"
475 " [number1] [number2] Encode frame number 1 to frame number 2\n"
476 " -u Use only 1 processor\n\n"
477 "Default settings:\n"
478 " fixed 5000000 bits/sec\n"
479 " interlaced\n"
480 " MPEG-2\n"
481 " 15 frames between I frames 0 frames between P frames\n\n"
482 "For the recommended encoding parameters see docs/index.html.\n",
483 argv[0],
484 mpeg1 ? "MPEG-1" : "MPEG-2",
485 (int)bit_rate,
486 INTTOYES(use_denoise_quant),
487 INTTOYES(use_hires_quant),
488 M - 1,
490 INTTOYES(prog_seq));
491 exit(1);
493 //printf("readcmdline 3\n");
495 for(i = 1; i < argc; i++)
497 isnum = 1;
499 for(j = 0; j < strlen(argv[i]) && isnum; j++)
501 if(isalpha(argv[i][j])) isnum = 0;
505 //printf("readcmdline %s\n", argv[i]);
506 if(!strcmp(argv[i], "-1"))
508 mpeg1 = 1;
510 else
511 if(!strcmp(argv[i], "-b"))
513 i++;
514 if(i < argc)
516 bit_rate = atol(argv[i]);
518 else
520 fprintf(stderr, "-b requires a bitrate\n");
521 exit(1);
524 else
525 if(!strcmp(argv[i], "-d"))
527 use_denoise_quant = 1;
529 else
530 if(!strcmp(argv[i], "-f"))
532 i++;
533 if(i < argc)
535 frame_rate = atof(argv[i]);
537 else
539 fprintf(stderr, "-f requires a frame rate\n");
540 exit(1);
543 else
544 if(!strcmp(argv[i], "-h"))
546 use_hires_quant = 1;
548 else
549 if(!strcmp(argv[i], "-m"))
551 i++;
552 if(i < argc)
554 M = atol(argv[i]) + 1;
556 else
558 fprintf(stderr, "-m requires a frame count\n");
559 exit(1);
562 else
563 if(!strcmp(argv[i], "-n"))
565 i++;
566 if(i < argc)
568 N = atol(argv[i]);
570 else
572 fprintf(stderr, "-n requires a frame count\n");
573 exit(1);
576 else
577 if(!strcmp(argv[i], "-p"))
579 prog_seq = 1;
581 else
582 if(!strcmp(argv[i], "-q"))
584 i++;
585 if(i < argc)
587 fixed_mquant = atol(argv[i]);
589 else
591 fprintf(stderr, "-q requires a quantization value\n");
592 exit(1);
595 else
596 if(!strcmp(argv[i], "-u"))
598 processors = 1;
600 else
601 if(!strcmp(argv[i], "-422"))
603 chroma_format = 2;
605 else
606 if(!strcmp(argv[i], "-g"))
608 seq_header_every_gop = 1;
610 else
611 if(!strcmp(argv[i], "-"))
613 do_stdin = 1;
615 else
616 /* Start or end frame if number */
617 if(isnum)
619 if(start_frame < 0)
620 start_frame = atol(argv[i]);
621 else
622 if(end_frame < 0)
623 end_frame = atol(argv[i]);
625 else
626 if(!strlen(tplorg) && !do_stdin && !do_buffers)
628 /* Input path */
629 strncpy(tplorg, argv[i], STRINGLEN);
631 else
632 if(!strlen(out_path))
634 /* Output path */
635 strncpy(out_path, argv[i], STRINGLEN);
638 //printf("readcmdline 4\n");
640 if(!strlen(out_path))
642 // Default output path
643 strncpy(out_path, tplorg, STRINGLEN);
644 for(i = strlen(out_path) - 1; i >= 0 && out_path[i] != '.'; i--)
647 if(i < 0) i = strlen(out_path);
649 if(mpeg1)
650 sprintf(&out_path[i], ".m1v");
651 else
652 sprintf(&out_path[i], ".m2v");
654 //printf("readcmdline 5\n");
656 /* Get info from input file */
657 if(do_stdin)
659 inputtype = T_STDIN;
661 else
662 if(do_buffers)
664 inputtype = T_BUFFERS;
666 else
667 if(mpeg3_check_sig(tplorg))
669 mpeg_file = mpeg3_open(tplorg);
670 inputtype = T_MPEG;
672 else
673 if(quicktime_check_sig(tplorg))
675 qt_file = quicktime_open(tplorg, 1, 0);
676 inputtype = T_QUICKTIME;
678 //printf("readcmdline 6\n");
680 if(!qt_file && !mpeg_file && !do_stdin && !do_buffers)
682 fprintf(stderr, "File format not recognized.\n");
683 exit(1);
686 //printf("readcmdline 7\n");
687 if(qt_file)
689 if(!quicktime_video_tracks(qt_file))
691 fprintf(stderr, "No video tracks in file.\n");
692 exit(1);
695 if(!quicktime_supported_video(qt_file, 0))
697 fprintf(stderr, "Unsupported video codec.\n");
698 exit(1);
701 //printf("readcmdline 8\n");
703 /************************************************************************
704 * BEGIN PARAMETER FILE
705 ************************************************************************/
707 /* To eliminate the user hassle we replaced the parameter file with hard coded constants. */
708 strcpy(tplref, "-"); /* name of intra quant matrix file ("-": default matrix) */
709 strcpy(iqname, "-"); /* name of intra quant matrix file ("-": default matrix) */
710 strcpy(niqname, "-"); /* name of non intra quant matrix file ("-": default matrix) */
711 strcpy(statname, "/dev/null"); /* name of statistics file ("-": stdout ) */
713 if(qt_file)
715 nframes = quicktime_video_length(qt_file, 0); /* number of frames */
716 horizontal_size = quicktime_video_width(qt_file, 0);
717 vertical_size = quicktime_video_height(qt_file, 0);
719 else
720 if(mpeg_file)
722 nframes = 0x7fffffff; /* Use percentage instead */
723 horizontal_size = mpeg3_video_width(mpeg_file, 0);
724 vertical_size = mpeg3_video_height(mpeg_file, 0);
726 else
727 if(do_stdin)
729 unsigned char data[1024];
730 nframes = 0x7fffffff;
732 fgets(data, 1024, stdin_fd);
733 horizontal_size = atol(data);
734 fgets(data, 1024, stdin_fd);
735 vertical_size = atol(data);
737 else
738 if(do_buffers)
740 nframes = 0x7fffffff;
744 h = m = s = f = 0; /* timecode of first frame */
745 fieldpic = 0; /* 0: progressive, 1: bottom first, 2: top first, 3 = progressive seq, field MC and DCT in picture */
746 aspectratio = 1; /* aspect_ratio_information 1=square pel, 2=4:3, 3=16:9, 4=2.11:1 */
747 low_delay = 0; /* low_delay */
748 constrparms = 0; /* constrained_parameters_flag */
749 profile = 4; /* Profile ID: Simple = 5, Main = 4, SNR = 3, Spatial = 2, High = 1 */
750 level = 4; /* Level ID: Low = 10, Main = 8, High 1440 = 6, High = 4 */
751 video_format = 2; /* video_format: 0=comp., 1=PAL, 2=NTSC, 3=SECAM, 4=MAC, 5=unspec. */
752 color_primaries = 5; /* color_primaries */
753 dctsatlim = mpeg1 ? 255 : 2047;
754 dctsatlim = 255;
755 transfer_characteristics = 5; /* transfer_characteristics */
756 matrix_coefficients = 4; /* matrix_coefficients (not used) */
757 display_horizontal_size = horizontal_size;
758 display_vertical_size = vertical_size;
759 cur_picture.dc_prec = 0; /* intra_dc_precision (0: 8 bit, 1: 9 bit, 2: 10 bit, 3: 11 bit */
760 cur_picture.topfirst = 1; /* top_field_first */
762 frame_pred_dct_tab[0] = mpeg1 ? 1 : 0; /* frame_pred_frame_dct (I P B) */
763 frame_pred_dct_tab[1] = mpeg1 ? 1 : 0; /* frame_pred_frame_dct (I P B) */
764 frame_pred_dct_tab[2] = mpeg1 ? 1 : 0; /* frame_pred_frame_dct (I P B) */
766 conceal_tab[0] = 0; /* concealment_motion_vectors (I P B) */
767 conceal_tab[1] = 0; /* concealment_motion_vectors (I P B) */
768 conceal_tab[2] = 0; /* concealment_motion_vectors (I P B) */
769 qscale_tab[0] = mpeg1 ? 0 : 1; /* q_scale_type (I P B) */
770 qscale_tab[1] = mpeg1 ? 0 : 1; /* q_scale_type (I P B) */
771 qscale_tab[2] = mpeg1 ? 0 : 1; /* q_scale_type (I P B) */
773 intravlc_tab[0] = 0; /* intra_vlc_format (I P B)*/
774 intravlc_tab[1] = 0; /* intra_vlc_format (I P B)*/
775 intravlc_tab[2] = 0; /* intra_vlc_format (I P B)*/
776 altscan_tab[0] = 0; /* alternate_scan (I P B) */
777 altscan_tab[1] = 0; /* alternate_scan (I P B) */
778 altscan_tab[2] = 0; /* alternate_scan (I P B) */
779 opt_dc_prec = 0; /* 8 bits */
780 opt_topfirst = (fieldpic == 2);
781 opt_repeatfirst = 0;
782 opt_prog_frame = prog_seq;
783 cur_picture.repeatfirst = 0; /* repeat_first_field */
784 cur_picture.prog_frame = prog_seq; /* progressive_frame */
785 /* P: forw_hor_f_code forw_vert_f_code search_width/height */
786 motion_data = (struct motion_data *)malloc(3 * sizeof(struct motion_data));
787 video_buffer_size = 46 * 1024 * 8;
789 /************************************************************************
790 * END PARAMETER FILE
791 ************************************************************************/
792 //printf("readcmdline 10\n");
794 if(mpeg1)
796 opt_prog_frame = 1;
797 cur_picture.prog_frame = 1;
798 prog_seq = 1;
801 if(qt_file)
803 input_frame_rate = quicktime_frame_rate(qt_file, 0);
805 else
806 if(mpeg_file)
808 input_frame_rate = mpeg3_frame_rate(mpeg_file, 0);
810 else
811 if(do_stdin)
813 char data[1024];
815 fgets(data, 1024, stdin_fd);
817 input_frame_rate = atof(data);
821 if(frame_rate < 0)
823 frame_rate = input_frame_rate;
825 //printf("readcmdline 11\n");
827 //processors = 1;
828 //nframes = 16;
829 if(start_frame >= 0 && end_frame >= 0)
831 nframes = end_frame - start_frame;
832 frame0 = start_frame;
834 else
835 if(start_frame >= 0)
837 end_frame = nframes;
838 nframes -= start_frame;
839 frame0 = start_frame;
841 else
843 start_frame = 0;
844 end_frame = nframes;
846 //printf("readcmdline 12\n");
848 // Show status
849 if(verbose)
851 printf("Encoding: %s frames %ld\n", out_path, nframes);
853 if(fixed_mquant == 0)
854 printf(" bitrate %.0f\n", bit_rate);
855 else
856 printf(" quantization %d\n", fixed_mquant);
857 printf(" %d frames between I frames %d frames between P frames\n", N, M - 1);
858 printf(" %s\n", (prog_seq ? "progressive" : "interlaced"));
859 printf(" %s\n", (mpeg1 ? "MPEG-1" : "MPEG-2"));
860 printf(" %s\n", (chroma_format == 1) ? "YUV-420" : "YUV-422");
861 printf(" %d processors\n", processors);
862 printf(" %.02f frames per second\n", frame_rate);
863 printf(" Denoise %s\n", INTTOYES(use_denoise_quant));
864 printf(" Hires quantization %s\n", INTTOYES(use_hires_quant));
867 if(mpeg_file)
869 fprintf(stderr, "(MPEG to MPEG transcoding for official use only.)\n");
870 mpeg3_set_mmx(mpeg_file, 0);
877 int radius_x = ((param_searchrad + 4) / 8) * 8;
878 int radius_y = ((param_searchrad * vertical_size / horizontal_size + 4) / 8) * 8;
879 int c;
881 /* TODO: These f-codes should really be adjusted for each
882 picture type... */
883 c=5;
884 if( radius_x*M < 64) c = 4;
885 if( radius_x*M < 32) c = 3;
886 if( radius_x*M < 16) c = 2;
887 if( radius_x*M < 8) c = 1;
889 if (!motion_data)
890 error("malloc failed\n");
892 for (i=0; i<M; i++)
894 if(i==0)
896 motion_data[i].forw_hor_f_code = c;
897 motion_data[i].forw_vert_f_code = c;
898 motion_data[i].sxf = MAX(1,radius_x*M);
899 motion_data[i].syf = MAX(1,radius_y*M);
901 else
903 motion_data[i].forw_hor_f_code = c;
904 motion_data[i].forw_vert_f_code = c;
905 motion_data[i].sxf = MAX(1,radius_x*i);
906 motion_data[i].syf = MAX(1,radius_y*i);
907 motion_data[i].back_hor_f_code = c;
908 motion_data[i].back_vert_f_code = c;
909 motion_data[i].sxb = MAX(1,radius_x*(M-i));
910 motion_data[i].syb = MAX(1,radius_y*(M-i));
916 // vbv_buffer_size = floor(((double)bit_rate * 0.20343) / 16384.0);
917 if(mpeg1)
918 vbv_buffer_size = 20 * 16384;
919 else
920 vbv_buffer_size = 112 * 16384;
922 fast_mc_frac = 10;
923 mc_44_red = 2;
924 mc_22_red = 3;
926 if(vbv_buffer_size > vbvlim[(level - 4) >> 1])
927 vbv_buffer_size = vbvlim[(level - 4) >> 1];
929 /* Set up frame buffers */
930 frame_buffer = malloc(horizontal_size * vertical_size * 3 + 4);
931 row_pointers = malloc(sizeof(unsigned char*) * vertical_size);
932 for(i = 0; i < vertical_size; i++) row_pointers[i] = &frame_buffer[horizontal_size * 3 * i];
934 // Get frame rate code from input frame rate
935 for(i = 0; i < total_frame_rates; i++)
937 if(fabs(frame_rate - ratetab[i]) < 0.001) frame_rate_code = i + 1;
940 /* make flags boolean (x!=0 -> x=1) */
941 mpeg1 = !!mpeg1;
942 fieldpic = !!fieldpic;
943 low_delay = !!low_delay;
944 constrparms = !!constrparms;
945 prog_seq = !!prog_seq;
946 cur_picture.topfirst = !!cur_picture.topfirst;
948 for (i = 0; i < 3; i++)
950 frame_pred_dct_tab[i] = !!frame_pred_dct_tab[i];
951 conceal_tab[i] = !!conceal_tab[i];
952 qscale_tab[i] = !!qscale_tab[i];
953 intravlc_tab[i] = !!intravlc_tab[i];
954 altscan_tab[i] = !!altscan_tab[i];
956 cur_picture.repeatfirst = !!cur_picture.repeatfirst;
957 cur_picture.prog_frame = !!cur_picture.prog_frame;
959 /* make sure MPEG specific parameters are valid */
960 range_checks();
962 /* timecode -> frame number */
963 tc0 = h;
964 tc0 = 60*tc0 + m;
965 tc0 = 60*tc0 + s;
966 tc0 = (int)(frame_rate+0.5)*tc0 + f;
968 qt_output = 0;
979 if (!mpeg1)
981 profile_and_level_checks();
983 else
985 /* MPEG-1 */
986 if (constrparms)
988 if (horizontal_size>768
989 || vertical_size>576
990 || ((horizontal_size+15)/16)*((vertical_size+15) / 16) > 396
991 || ((horizontal_size+15)/16)*((vertical_size+15) / 16)*frame_rate>396*25.0
992 || frame_rate>30.0)
994 if (!quiet)
995 fprintf(stderr,"*** Warning: setting constrained_parameters_flag = 0\n");
996 constrparms = 0;
1001 /* relational checks */
1003 if (mpeg1)
1005 if (!prog_seq)
1007 prog_seq = 1;
1010 if (chroma_format!=CHROMA420)
1012 chroma_format = CHROMA420;
1015 if (cur_picture.dc_prec!=0)
1017 cur_picture.dc_prec = 0;
1020 for (i=0; i<3; i++)
1021 if (qscale_tab[i])
1023 qscale_tab[i] = 0;
1026 for (i=0; i<3; i++)
1027 if (intravlc_tab[i])
1029 intravlc_tab[i] = 0;
1032 for (i=0; i<3; i++)
1033 if (altscan_tab[i])
1035 altscan_tab[i] = 0;
1039 if (!mpeg1 && constrparms)
1041 constrparms = 0;
1044 if (prog_seq && !cur_picture.prog_frame)
1046 cur_picture.prog_frame = 1;
1049 if (cur_picture.prog_frame && fieldpic)
1051 fieldpic = 0;
1054 if (!cur_picture.prog_frame && cur_picture.repeatfirst)
1056 cur_picture.repeatfirst = 0;
1059 if (cur_picture.prog_frame)
1061 for (i=0; i<3; i++)
1062 if (!frame_pred_dct_tab[i])
1064 frame_pred_dct_tab[i] = 1;
1068 if (prog_seq && !cur_picture.repeatfirst && cur_picture.topfirst)
1070 if (!quiet)
1071 fprintf(stderr,"Warning: setting top_field_first = 0\n");
1072 cur_picture.topfirst = 0;
1075 /* search windows */
1076 for (i=0; i<M; i++)
1078 if (motion_data[i].sxf > (4<<motion_data[i].forw_hor_f_code)-1)
1080 if (!quiet)
1081 fprintf(stderr,
1082 "Warning: reducing forward horizontal search width to %d\n",
1083 (4<<motion_data[i].forw_hor_f_code)-1);
1084 motion_data[i].sxf = (4<<motion_data[i].forw_hor_f_code)-1;
1087 if (motion_data[i].syf > (4<<motion_data[i].forw_vert_f_code)-1)
1089 if (!quiet)
1090 fprintf(stderr,
1091 "Warning: reducing forward vertical search width to %d\n",
1092 (4<<motion_data[i].forw_vert_f_code)-1);
1093 motion_data[i].syf = (4<<motion_data[i].forw_vert_f_code)-1;
1096 if (i!=0)
1098 if (motion_data[i].sxb > (4<<motion_data[i].back_hor_f_code)-1)
1100 if (!quiet)
1101 fprintf(stderr,
1102 "Warning: reducing backward horizontal search width to %d\n",
1103 (4<<motion_data[i].back_hor_f_code)-1);
1104 motion_data[i].sxb = (4<<motion_data[i].back_hor_f_code)-1;
1107 if (motion_data[i].syb > (4<<motion_data[i].back_vert_f_code)-1)
1109 if (!quiet)
1110 fprintf(stderr,
1111 "Warning: reducing backward vertical search width to %d\n",
1112 (4<<motion_data[i].back_vert_f_code)-1);
1113 motion_data[i].syb = (4<<motion_data[i].back_vert_f_code)-1;
1121 If the user has selected suppression of hf noise via
1122 quantisation then we boost quantisation of hf components
1123 EXPERIMENTAL: currently a linear ramp from 0 at 4pel to
1124 50% increased quantisation...
1127 static int quant_hfnoise_filt(int orgquant, int qmat_pos )
1129 int x = qmat_pos % 8;
1130 int y = qmat_pos / 8;
1131 int qboost = 1024;
1133 if(!use_denoise_quant)
1135 return orgquant;
1138 /* Maximum 50% quantisation boost for HF components... */
1139 if( x > 4 )
1140 qboost += (256*(x-4)/3);
1141 if( y > 4 )
1142 qboost += (256*(y-4)/3);
1144 return (orgquant * qboost + 512)/ 1024;
1147 static void readquantmat()
1149 int i,v,q;
1150 load_iquant = 0;
1151 load_niquant = 0;
1153 if (iqname[0]=='-')
1155 if(use_hires_quant)
1157 load_iquant |= 1;
1158 for (i=0; i<64; i++)
1160 intra_q[i] = hires_intra_quantizer_matrix[i];
1163 else
1165 load_iquant = use_denoise_quant;
1166 for (i=0; i<64; i++)
1168 v = quant_hfnoise_filt(default_intra_quantizer_matrix[i], i);
1169 if (v<1 || v>255)
1170 error("value in intra quant matrix invalid (after noise filt adjust)");
1171 intra_q[i] = v;
1176 /* TODO: Inv Quant matrix initialisation should check if the fraction fits in 16 bits! */
1177 if (niqname[0]=='-')
1179 if(use_hires_quant)
1181 for (i=0; i<64; i++)
1183 inter_q[i] = hires_nonintra_quantizer_matrix[i];
1186 else
1188 /* default non-intra matrix is all 16's. For *our* default we use something
1189 more suitable for domestic analog sources... which is non-standard...*/
1190 load_niquant |= 1;
1191 for (i=0; i<64; i++)
1193 v = quant_hfnoise_filt(default_nonintra_quantizer_matrix[i],i);
1194 if (v<1 || v>255)
1195 error("value in non-intra quant matrix invalid (after noise filt adjust)");
1196 inter_q[i] = v;
1201 for (i=0; i<64; i++)
1203 i_intra_q[i] = (int)(((double)IQUANT_SCALE) / ((double)intra_q[i]));
1204 i_inter_q[i] = (int)(((double)IQUANT_SCALE) / ((double)inter_q[i]));
1207 for( q = 1; q <= 112; ++q )
1209 for (i=0; i<64; i++)
1211 intra_q_tbl[q][i] = intra_q[i] * q;
1212 inter_q_tbl[q][i] = inter_q[i] * q;
1213 intra_q_tblf[q][i] = (float)intra_q_tbl[q][i];
1214 inter_q_tblf[q][i] = (float)inter_q_tbl[q][i];
1215 i_intra_q_tblf[q][i] = 1.0f/ ( intra_q_tblf[q][i] * 0.98);
1216 i_intra_q_tbl[q][i] = (IQUANT_SCALE/intra_q_tbl[q][i]);
1217 i_inter_q_tblf[q][i] = 1.0f/ (inter_q_tblf[q][i] * 0.98);
1218 i_inter_q_tbl[q][i] = (IQUANT_SCALE/inter_q_tbl[q][i] );