00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #define MAX(a,b) ( (a)>(b) ? (a) : (b) )
00031 #include <math.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035
00036 #define GLOBAL_
00037 #include "config.h"
00038 #include "global.h"
00039
00040
00041 static void init _ANSI_ARGS_((void));
00042 static void readcmdline _ANSI_ARGS_((int argc, char *argv[]));
00043 static void readquantmat _ANSI_ARGS_((void));
00044
00045
00046
00047
00048 void
00049 g_log (const char *log_domain,
00050 int log_level,
00051 const char *format,
00052 ...)
00053 {
00054 }
00055
00056 void
00057 g_logv (const char *log_domain,
00058 int log_level,
00059 const char *format,
00060 ...)
00061 {
00062 }
00063
00064
00065 void mpeg2enc_set_w(int width)
00066 {
00067 horizontal_size = width;
00068 }
00069
00070 void mpeg2enc_set_h(int height)
00071 {
00072 vertical_size = height;
00073 }
00074
00075 void mpeg2enc_set_rate(double rate)
00076 {
00077 input_frame_rate = rate;
00078 }
00079
00080 void mpeg2enc_set_input_buffers(int eof, char *y, char *u, char *v)
00081 {
00082 pthread_mutex_lock(&output_lock);
00083 input_buffer_end = eof;
00084 input_buffer_y = y;
00085 input_buffer_u = u;
00086 input_buffer_v = v;
00087 pthread_mutex_unlock(&input_lock);
00088
00089 pthread_mutex_lock(©_lock);
00090 }
00091
00092 void mpeg2enc_init_buffers()
00093 {
00094 pthread_mutex_init(&input_lock, 0);
00095 pthread_mutex_init(&output_lock, 0);
00096 pthread_mutex_init(©_lock, 0);
00097 pthread_mutex_lock(&input_lock);
00098 pthread_mutex_lock(©_lock);
00099 input_buffer_end = 0;
00100 }
00101
00102 int mpeg2enc(int argc, char *argv[])
00103 {
00104 stdin_fd = stdin;
00105
00106 verbose = 1;
00107
00108
00109
00110 readcmdline(argc, argv);
00111
00112
00113 readquantmat();
00114
00115 if(!strlen(out_path))
00116 {
00117 fprintf(stderr, "No output file given.\n");
00118 }
00119
00120
00121 if(!(outfile = fopen(out_path, "wb")))
00122 {
00123 sprintf(errortext,"Couldn't create output file %s", out_path);
00124 error(errortext);
00125 }
00126
00127 init();
00128
00129 if(nframes < 0x7fffffff)
00130 printf("Frame Completion Current bitrate Predicted file size\n");
00131 putseq();
00132
00133 stop_slice_engines();
00134 stop_motion_engines();
00135 stop_transform_engines();
00136 stop_itransform_engines();
00137
00138 fclose(outfile);
00139 fclose(statfile);
00140
00141 if(qt_file) quicktime_close(qt_file);
00142 if(qt_output) quicktime_close(qt_output);
00143 if(mpeg_file) mpeg3_close(mpeg_file);
00144
00145 if(do_stdin)
00146 {
00147 fclose(stdin_fd);
00148 }
00149 pthread_mutex_destroy(&input_lock);
00150 pthread_mutex_destroy(&output_lock);
00151 return 0;
00152 }
00153
00154 int HorzMotionCode(int i)
00155 {
00156 if (i < 8)
00157 return 1;
00158 if (i < 16)
00159 return 2;
00160 if (i < 32)
00161 return 3;
00162 if ((i < 64) || (constrparms))
00163 return 4;
00164 if (i < 128)
00165 return 5;
00166 if (i < 256)
00167 return 6;
00168 if ((i < 512) || (level == 10))
00169 return 7;
00170 if ((i < 1024) || (level == 8))
00171 return 8;
00172 if (i < 2048)
00173 return 9;
00174 return 1;
00175 }
00176
00177 int VertMotionCode(int i)
00178 {
00179 if (i < 8)
00180 return 1;
00181 if (i < 16)
00182 return 2;
00183 if (i < 32)
00184 return 3;
00185 if ((i < 64) || (level == 10) || (constrparms))
00186 return 4;
00187 return 5;
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 static uint8_t *bufalloc( size_t size )
00197 {
00198 char *buf = malloc( size + BUFFER_ALIGN );
00199 int adjust;
00200
00201 if( buf == NULL )
00202 {
00203 error("malloc failed\n");
00204 }
00205 adjust = BUFFER_ALIGN-((int)buf)%BUFFER_ALIGN;
00206 if( adjust == BUFFER_ALIGN )
00207 adjust = 0;
00208 return (uint8_t*)(buf+adjust);
00209 }
00210
00211 static void init()
00212 {
00213 int i, n, size;
00214 static int block_count_tab[3] = {6,8,12};
00215 int lum_buffer_size, chrom_buffer_size;
00216 pthread_mutexattr_t mutex_attr;
00217 pthread_mutexattr_init(&mutex_attr);
00218 pthread_mutex_init(&test_lock, &mutex_attr);
00219
00220 bzero(&cur_picture, sizeof(pict_data_s));
00221 mpeg2_initbits();
00222 init_fdct();
00223 init_idct();
00224 init_motion();
00225 init_predict_hv();
00226 init_quantizer_hv();
00227 init_transform_hv();
00228
00229
00230 mb_width = (horizontal_size+15)/16;
00231 mb_height = prog_seq ?
00232 (vertical_size + 15) / 16 :
00233 2 * ((vertical_size + 31) / 32);
00234 mb_height2 = fieldpic ?
00235 mb_height >> 1 :
00236 mb_height;
00237 width = 16 * mb_width;
00238 height = 16 * mb_height;
00239
00240 chrom_width = (chroma_format==CHROMA444) ? width : width>>1;
00241 chrom_height = (chroma_format!=CHROMA420) ? height : height>>1;
00242
00243 height2 = fieldpic ? height>>1 : height;
00244 width2 = fieldpic ? width<<1 : width;
00245 chrom_width2 = fieldpic ? chrom_width<<1 : chrom_width;
00246
00247 block_count = block_count_tab[chroma_format-1];
00248 lum_buffer_size = (width*height) +
00249 sizeof(uint8_t) *(width/2)*(height/2) +
00250 sizeof(uint8_t) *(width/4)*(height/4+1);
00251 chrom_buffer_size = chrom_width*chrom_height;
00252
00253 fsubsample_offset = (width)*(height) * sizeof(uint8_t);
00254 qsubsample_offset = fsubsample_offset + (width/2)*(height/2)*sizeof(uint8_t);
00255
00256 rowsums_offset = 0;
00257 colsums_offset = 0;
00258
00259 mb_per_pict = mb_width*mb_height2;
00260
00261
00262 if (!(clp = (unsigned char *)malloc(1024)))
00263 error("malloc failed\n");
00264 clp+= 384;
00265 for (i=-384; i<640; i++)
00266 clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
00267
00268
00269
00270
00271
00272
00273 frame_buffers = (uint8_t ***)
00274 bufalloc(2*READ_LOOK_AHEAD*sizeof(uint8_t**));
00275
00276 for(n=0;n<2*READ_LOOK_AHEAD;n++)
00277 {
00278 frame_buffers[n] = (uint8_t **) bufalloc(3*sizeof(uint8_t*));
00279 for (i=0; i<3; i++)
00280 {
00281 frame_buffers[n][i] =
00282 bufalloc( (i==0) ? lum_buffer_size : chrom_buffer_size );
00283 }
00284 }
00285
00286
00287
00288
00289 for( i = 0 ; i<3; i++)
00290 {
00291 int size = (i==0) ? lum_buffer_size : chrom_buffer_size;
00292 newrefframe[i] = bufalloc(size);
00293 oldrefframe[i] = bufalloc(size);
00294 auxframe[i] = bufalloc(size);
00295 predframe[i] = bufalloc(size);
00296 }
00297
00298 cur_picture.qblocks =
00299 (int16_t (*)[64])bufalloc(mb_per_pict*block_count*sizeof(int16_t [64]));
00300
00301
00302
00303
00304
00305 cur_picture.mbinfo = (
00306 struct mbinfo *)bufalloc(mb_per_pict*sizeof(struct mbinfo));
00307
00308 cur_picture.blocks =
00309 (int16_t (*)[64])bufalloc(mb_per_pict * block_count * sizeof(int16_t [64]));
00310
00311
00312
00313 if(statname[0]=='-') statfile = stdout;
00314 else
00315 if(!(statfile = fopen(statname,"w")))
00316 {
00317 sprintf(errortext,"Couldn't create statistics output file %s",statname);
00318 error(errortext);
00319 }
00320
00321 ratectl = malloc(processors * sizeof(ratectl_t*));
00322 for(i = 0; i < processors; i++)
00323 ratectl[i] = calloc(1, sizeof(ratectl_t));
00324
00325
00326
00327
00328
00329
00330 start_motion_engines();
00331
00332 start_transform_engines();
00333
00334 start_itransform_engines();
00335
00336 start_slice_engines();
00337
00338 }
00339
00340 void error(text)
00341 char *text;
00342 {
00343 fprintf(stderr,text);
00344 putc('\n',stderr);
00345 exit(1);
00346 }
00347
00348 #define STRINGLEN 254
00349
00350 int calculate_smp()
00351 {
00352
00353 int result = 1;
00354 FILE *proc;
00355 if(proc = fopen("/proc/cpuinfo", "r"))
00356 {
00357 char string[1024];
00358 while(!feof(proc))
00359 {
00360 fgets(string, 1024, proc);
00361 if(!strncasecmp(string, "processor", 9))
00362 {
00363 char *ptr = strchr(string, ':');
00364 if(ptr)
00365 {
00366 ptr++;
00367 result = atol(ptr) + 1;
00368 }
00369 }
00370 else
00371 if(!strncasecmp(string, "cpus detected", 13))
00372 {
00373 char *ptr = strchr(string, ':');
00374 if(ptr)
00375 {
00376 ptr++;
00377 result = atol(ptr);
00378 }
00379 }
00380 }
00381 fclose(proc);
00382 }
00383 return result;
00384 }
00385
00386 static void readcmdline(int argc, char *argv[])
00387 {
00388 int i, j;
00389 int h,m,s,f;
00390 FILE *fd;
00391 char line[256];
00392
00393 static double ratetab[]=
00394 {24000.0/1001.0,
00395 24.0,
00396 25.0,
00397 30000.0/1001.0,
00398 30.0,
00399 50.0,
00400 60000.0/1001.0,
00401 60.0,
00402
00403 1,
00404 5,
00405 10,
00406 12,
00407 15,
00408 0,
00409 0};
00410
00411 int vbvlim[4] = { 597, 448, 112, 29 };
00412 long total_frame_rates = (sizeof(ratetab) / sizeof(double));
00413 FILE *proc;
00414
00415 int param_searchrad = 16;
00416 int isnum = 1;
00417
00418
00419 frame0 = 0;
00420 start_frame = end_frame = -1;
00421 use_hires_quant = 0;
00422 use_denoise_quant = 0;
00423 quiet = 1;
00424 bit_rate = 5000000;
00425 prog_seq = 0;
00426 mpeg1 = 0;
00427 fixed_mquant = 0;
00428 quant_floor = 0;
00429 act_boost = 3.0;
00430 N = 15;
00431 M = 1;
00432 processors = calculate_smp();
00433 frame_rate = -1;
00434 chroma_format = 1;
00435 mpeg_file = 0;
00436 qt_file = 0;
00437 do_stdin = 0;
00438 do_buffers = 1;
00439 seq_header_every_gop = 0;
00440
00441 aspectratio = 1;
00442
00443
00444
00445
00446
00447
00448
00449
00450 sprintf(tplorg, "");
00451 sprintf(out_path, "");
00452
00453 #define INTTOYES(x) ((x) ? "Yes" : "No")
00454
00455 if(argc < 2)
00456 {
00457 printf("mpeg2encode V1.3, 2000/01/10\n"
00458 "(C) 1996, MPEG Software Simulation Group\n"
00459 "(C) 2001 Heroine Virtual\n"
00460 "Usage: %s [options] <input file> <output file>\n\n"
00461 " -1 generate an MPEG-1 stream instead of MPEG-2 (%s)\n"
00462 " -422 generate YUV 4:2:2 output\n"
00463 " -b bitrate fix the bitrate, vary the quantization (%d)\n"
00464 " -d Denoise (%s)\n"
00465 " -f rate Convert framerate\n"
00466 " -h High resolution quantization table (%s)\n"
00467 " -m frames set number of frames between P frames (%d)\n"
00468 " -n frames set number of frames between I frames (%d)\n"
00469 " -p encode progressive frames (%s)\n"
00470 " -q quantization fix the quantization, vary the bitrate\n"
00471 " [number] Start encoding from frame number to end\n"
00472 " [number1] [number2] Encode frame number 1 to frame number 2\n"
00473 " -u Use only 1 processor\n\n"
00474 "Default settings:\n"
00475 " fixed 5000000 bits/sec\n"
00476 " interlaced\n"
00477 " MPEG-2\n"
00478 " 15 frames between I frames 0 frames between P frames\n\n"
00479 "For the recommended encoding parameters see docs/index.html.\n",
00480 argv[0],
00481 mpeg1 ? "MPEG-1" : "MPEG-2",
00482 (int)bit_rate,
00483 INTTOYES(use_denoise_quant),
00484 INTTOYES(use_hires_quant),
00485 M - 1,
00486 N,
00487 INTTOYES(prog_seq));
00488 exit(1);
00489 }
00490
00491
00492 for(i = 1; i < argc; i++)
00493 {
00494 isnum = 1;
00495
00496 for(j = 0; j < strlen(argv[i]) && isnum; j++)
00497 {
00498 if(isalpha(argv[i][j])) isnum = 0;
00499 }
00500
00501
00502
00503 if(!strcmp(argv[i], "-1"))
00504 {
00505 mpeg1 = 1;
00506 }
00507 else
00508 if(!strcmp(argv[i], "-a"))
00509 {
00510 i++;
00511 if(i < argc)
00512 {
00513 aspectratio = atoi(argv[i]);
00514 }
00515 else
00516 {
00517 fprintf(stderr, "-i needs an aspect ratio enumeration.\n");
00518 exit(1);
00519 }
00520 }
00521 else
00522 if(!strcmp(argv[i], "-b"))
00523 {
00524 i++;
00525 if(i < argc)
00526 {
00527 bit_rate = atol(argv[i]);
00528 }
00529 else
00530 {
00531 fprintf(stderr, "-b requires a bitrate\n");
00532 exit(1);
00533 }
00534 }
00535 else
00536 if(!strcmp(argv[i], "-d"))
00537 {
00538 use_denoise_quant = 1;
00539 }
00540 else
00541 if(!strcmp(argv[i], "-f"))
00542 {
00543 i++;
00544 if(i < argc)
00545 {
00546 frame_rate = atof(argv[i]);
00547 }
00548 else
00549 {
00550 fprintf(stderr, "-f requires a frame rate\n");
00551 exit(1);
00552 }
00553 }
00554 else
00555 if(!strcmp(argv[i], "-h"))
00556 {
00557 use_hires_quant = 1;
00558 }
00559 else
00560 if(!strcmp(argv[i], "-m"))
00561 {
00562 i++;
00563 if(i < argc)
00564 {
00565 M = atol(argv[i]) + 1;
00566 }
00567 else
00568 {
00569 fprintf(stderr, "-m requires a frame count\n");
00570 exit(1);
00571 }
00572 }
00573 else
00574 if(!strcmp(argv[i], "-n"))
00575 {
00576 i++;
00577 if(i < argc)
00578 {
00579 N = atol(argv[i]);
00580 }
00581 else
00582 {
00583 fprintf(stderr, "-n requires a frame count\n");
00584 exit(1);
00585 }
00586 }
00587 else
00588 if(!strcmp(argv[i], "-p"))
00589 {
00590 prog_seq = 1;
00591 }
00592 else
00593 if(!strcmp(argv[i], "-q"))
00594 {
00595 i++;
00596 if(i < argc)
00597 {
00598 fixed_mquant = atol(argv[i]);
00599 }
00600 else
00601 {
00602 fprintf(stderr, "-q requires a quantization value\n");
00603 exit(1);
00604 }
00605 }
00606 else
00607 if(!strcmp(argv[i], "-u"))
00608 {
00609 processors = 1;
00610 }
00611 else
00612 if(!strcmp(argv[i], "-422"))
00613 {
00614 chroma_format = 2;
00615 }
00616 else
00617 if(!strcmp(argv[i], "-g"))
00618 {
00619 seq_header_every_gop = 1;
00620 }
00621 else
00622 if(!strcmp(argv[i], "-"))
00623 {
00624 do_stdin = 1;
00625 }
00626 else
00627
00628 if(isnum)
00629 {
00630 if(start_frame < 0)
00631 start_frame = atol(argv[i]);
00632 else
00633 if(end_frame < 0)
00634 end_frame = atol(argv[i]);
00635 }
00636 else
00637 if(!strlen(tplorg) && !do_stdin && !do_buffers)
00638 {
00639
00640 strncpy(tplorg, argv[i], STRINGLEN);
00641 }
00642 else
00643 if(!strlen(out_path))
00644 {
00645
00646 strncpy(out_path, argv[i], STRINGLEN);
00647 }
00648 }
00649
00650
00651 if(!strlen(out_path))
00652 {
00653
00654 strncpy(out_path, tplorg, STRINGLEN);
00655 for(i = strlen(out_path) - 1; i >= 0 && out_path[i] != '.'; i--)
00656 ;
00657
00658 if(i < 0) i = strlen(out_path);
00659
00660 if(mpeg1)
00661 sprintf(&out_path[i], ".m1v");
00662 else
00663 sprintf(&out_path[i], ".m2v");
00664 }
00665
00666
00667
00668 if(do_stdin)
00669 {
00670 inputtype = T_STDIN;
00671 }
00672 else
00673 if(do_buffers)
00674 {
00675 inputtype = T_BUFFERS;
00676 }
00677 else
00678 if(mpeg3_check_sig(tplorg))
00679 {
00680 int error_return;
00681 mpeg_file = mpeg3_open(tplorg, &error_return);
00682 inputtype = T_MPEG;
00683 }
00684 else
00685 if(quicktime_check_sig(tplorg))
00686 {
00687 qt_file = quicktime_open(tplorg, 1, 0);
00688 inputtype = T_QUICKTIME;
00689 }
00690
00691
00692 if(!qt_file && !mpeg_file && !do_stdin && !do_buffers)
00693 {
00694 fprintf(stderr, "File format not recognized.\n");
00695 exit(1);
00696 }
00697
00698
00699 if(qt_file)
00700 {
00701 if(!quicktime_video_tracks(qt_file))
00702 {
00703 fprintf(stderr, "No video tracks in file.\n");
00704 exit(1);
00705 }
00706
00707 if(!quicktime_supported_video(qt_file, 0))
00708 {
00709 fprintf(stderr, "Unsupported video codec.\n");
00710 exit(1);
00711 }
00712 }
00713
00714
00715
00716
00717
00718
00719
00720 strcpy(tplref, "-");
00721 strcpy(iqname, "-");
00722 strcpy(niqname, "-");
00723 strcpy(statname, "/dev/null");
00724
00725 if(qt_file)
00726 {
00727 nframes = quicktime_video_length(qt_file, 0);
00728 horizontal_size = quicktime_video_width(qt_file, 0);
00729 vertical_size = quicktime_video_height(qt_file, 0);
00730 }
00731 else
00732 if(mpeg_file)
00733 {
00734 nframes = 0x7fffffff;
00735 horizontal_size = mpeg3_video_width(mpeg_file, 0);
00736 vertical_size = mpeg3_video_height(mpeg_file, 0);
00737 }
00738 else
00739 if(do_stdin)
00740 {
00741 unsigned char data[1024];
00742 nframes = 0x7fffffff;
00743
00744 fgets(data, 1024, stdin_fd);
00745 horizontal_size = atol(data);
00746 fgets(data, 1024, stdin_fd);
00747 vertical_size = atol(data);
00748 }
00749 else
00750 if(do_buffers)
00751 {
00752 nframes = 0x7fffffff;
00753 }
00754
00755
00756 h = m = s = f = 0;
00757 fieldpic = 0;
00758 low_delay = 0;
00759 constrparms = 0;
00760 profile = 4;
00761 level = 4;
00762 video_format = 2;
00763 color_primaries = 5;
00764 dctsatlim = mpeg1 ? 255 : 2047;
00765 dctsatlim = 255;
00766 transfer_characteristics = 5;
00767 matrix_coefficients = 4;
00768 display_horizontal_size = horizontal_size;
00769 display_vertical_size = vertical_size;
00770 cur_picture.dc_prec = 0;
00771 cur_picture.topfirst = 1;
00772
00773 frame_pred_dct_tab[0] = mpeg1 ? 1 : 0;
00774 frame_pred_dct_tab[1] = mpeg1 ? 1 : 0;
00775 frame_pred_dct_tab[2] = mpeg1 ? 1 : 0;
00776
00777 conceal_tab[0] = 0;
00778 conceal_tab[1] = 0;
00779 conceal_tab[2] = 0;
00780 qscale_tab[0] = mpeg1 ? 0 : 1;
00781 qscale_tab[1] = mpeg1 ? 0 : 1;
00782 qscale_tab[2] = mpeg1 ? 0 : 1;
00783
00784 intravlc_tab[0] = 0;
00785 intravlc_tab[1] = 0;
00786 intravlc_tab[2] = 0;
00787 altscan_tab[0] = 0;
00788 altscan_tab[1] = 0;
00789 altscan_tab[2] = 0;
00790 opt_dc_prec = 0;
00791 opt_topfirst = (fieldpic == 2);
00792 opt_repeatfirst = 0;
00793 opt_prog_frame = prog_seq;
00794 cur_picture.repeatfirst = 0;
00795 cur_picture.prog_frame = prog_seq;
00796
00797 motion_data = (struct motion_data *)malloc(3 * sizeof(struct motion_data));
00798 video_buffer_size = 46 * 1024 * 8;
00799
00800
00801
00802
00803
00804
00805 if(mpeg1)
00806 {
00807 opt_prog_frame = 1;
00808 cur_picture.prog_frame = 1;
00809 prog_seq = 1;
00810 }
00811
00812 if(qt_file)
00813 {
00814 input_frame_rate = quicktime_frame_rate(qt_file, 0);
00815 }
00816 else
00817 if(mpeg_file)
00818 {
00819 input_frame_rate = mpeg3_frame_rate(mpeg_file, 0);
00820 }
00821 else
00822 if(do_stdin)
00823 {
00824 char data[1024];
00825
00826 fgets(data, 1024, stdin_fd);
00827
00828 input_frame_rate = atof(data);
00829 }
00830
00831
00832 if(frame_rate < 0)
00833 {
00834 frame_rate = input_frame_rate;
00835 }
00836
00837
00838
00839
00840 if(start_frame >= 0 && end_frame >= 0)
00841 {
00842 nframes = end_frame - start_frame;
00843 frame0 = start_frame;
00844 }
00845 else
00846 if(start_frame >= 0)
00847 {
00848 end_frame = nframes;
00849 nframes -= start_frame;
00850 frame0 = start_frame;
00851 }
00852 else
00853 {
00854 start_frame = 0;
00855 end_frame = nframes;
00856 }
00857
00858
00859
00860 if(verbose)
00861 {
00862 printf("Encoding: %s frames %ld\n", out_path, nframes);
00863
00864 if(fixed_mquant == 0)
00865 printf(" bitrate %.0f\n", bit_rate);
00866 else
00867 printf(" quantization %d\n", fixed_mquant);
00868 printf(" %d frames between I frames %d frames between P frames\n", N, M - 1);
00869 printf(" %s\n", (prog_seq ? "progressive" : "interlaced"));
00870 printf(" %s\n", (mpeg1 ? "MPEG-1" : "MPEG-2"));
00871 printf(" %s\n", (chroma_format == 1) ? "YUV-420" : "YUV-422");
00872 printf(" %d processors\n", processors);
00873 printf(" %.02f frames per second\n", frame_rate);
00874 printf(" Denoise %s\n", INTTOYES(use_denoise_quant));
00875 printf(" Aspect ratio index %d\n", aspectratio);
00876 printf(" Hires quantization %s\n", INTTOYES(use_hires_quant));
00877
00878
00879 if(mpeg_file)
00880 {
00881 fprintf(stderr, "(MPEG to MPEG transcoding for official use only.)\n");
00882 }
00883 }
00884
00885
00886
00887 {
00888 int radius_x = ((param_searchrad + 4) / 8) * 8;
00889 int radius_y = ((param_searchrad * vertical_size / horizontal_size + 4) / 8) * 8;
00890 int c;
00891
00892
00893
00894 c=5;
00895 if( radius_x*M < 64) c = 4;
00896 if( radius_x*M < 32) c = 3;
00897 if( radius_x*M < 16) c = 2;
00898 if( radius_x*M < 8) c = 1;
00899
00900 if (!motion_data)
00901 error("malloc failed\n");
00902
00903 for (i=0; i<M; i++)
00904 {
00905 if(i==0)
00906 {
00907 motion_data[i].forw_hor_f_code = c;
00908 motion_data[i].forw_vert_f_code = c;
00909 motion_data[i].sxf = MAX(1,radius_x*M);
00910 motion_data[i].syf = MAX(1,radius_y*M);
00911 }
00912 else
00913 {
00914 motion_data[i].forw_hor_f_code = c;
00915 motion_data[i].forw_vert_f_code = c;
00916 motion_data[i].sxf = MAX(1,radius_x*i);
00917 motion_data[i].syf = MAX(1,radius_y*i);
00918 motion_data[i].back_hor_f_code = c;
00919 motion_data[i].back_vert_f_code = c;
00920 motion_data[i].sxb = MAX(1,radius_x*(M-i));
00921 motion_data[i].syb = MAX(1,radius_y*(M-i));
00922 }
00923 }
00924
00925 }
00926
00927
00928 if(mpeg1)
00929 vbv_buffer_size = 20 * 16384;
00930 else
00931 vbv_buffer_size = 112 * 16384;
00932
00933 fast_mc_frac = 10;
00934 mc_44_red = 2;
00935 mc_22_red = 3;
00936
00937 if(vbv_buffer_size > vbvlim[(level - 4) >> 1])
00938 vbv_buffer_size = vbvlim[(level - 4) >> 1];
00939
00940
00941 frame_buffer = malloc(horizontal_size * vertical_size * 3 + 4);
00942 row_pointers = malloc(sizeof(unsigned char*) * vertical_size);
00943 for(i = 0; i < vertical_size; i++) row_pointers[i] = &frame_buffer[horizontal_size * 3 * i];
00944
00945
00946 for(i = 0; i < total_frame_rates; i++)
00947 {
00948 if(fabs(frame_rate - ratetab[i]) < 0.001) frame_rate_code = i + 1;
00949 }
00950
00951
00952 mpeg1 = !!mpeg1;
00953 fieldpic = !!fieldpic;
00954 low_delay = !!low_delay;
00955 constrparms = !!constrparms;
00956 prog_seq = !!prog_seq;
00957 cur_picture.topfirst = !!cur_picture.topfirst;
00958
00959 for (i = 0; i < 3; i++)
00960 {
00961 frame_pred_dct_tab[i] = !!frame_pred_dct_tab[i];
00962 conceal_tab[i] = !!conceal_tab[i];
00963 qscale_tab[i] = !!qscale_tab[i];
00964 intravlc_tab[i] = !!intravlc_tab[i];
00965 altscan_tab[i] = !!altscan_tab[i];
00966 }
00967 cur_picture.repeatfirst = !!cur_picture.repeatfirst;
00968 cur_picture.prog_frame = !!cur_picture.prog_frame;
00969
00970
00971 range_checks();
00972
00973
00974 tc0 = h;
00975 tc0 = 60*tc0 + m;
00976 tc0 = 60*tc0 + s;
00977 tc0 = (int)(frame_rate+0.5)*tc0 + f;
00978
00979 qt_output = 0;
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990 if (!mpeg1)
00991 {
00992 profile_and_level_checks();
00993 }
00994 else
00995 {
00996
00997 if (constrparms)
00998 {
00999 if (horizontal_size>768
01000 || vertical_size>576
01001 || ((horizontal_size+15)/16)*((vertical_size+15) / 16) > 396
01002 || ((horizontal_size+15)/16)*((vertical_size+15) / 16)*frame_rate>396*25.0
01003 || frame_rate>30.0)
01004 {
01005 if (!quiet)
01006 fprintf(stderr,"*** Warning: setting constrained_parameters_flag = 0\n");
01007 constrparms = 0;
01008 }
01009 }
01010 }
01011
01012
01013
01014 if (mpeg1)
01015 {
01016 if (!prog_seq)
01017 {
01018 prog_seq = 1;
01019 }
01020
01021 if (chroma_format!=CHROMA420)
01022 {
01023 chroma_format = CHROMA420;
01024 }
01025
01026 if (cur_picture.dc_prec!=0)
01027 {
01028 cur_picture.dc_prec = 0;
01029 }
01030
01031 for (i=0; i<3; i++)
01032 if (qscale_tab[i])
01033 {
01034 qscale_tab[i] = 0;
01035 }
01036
01037 for (i=0; i<3; i++)
01038 if (intravlc_tab[i])
01039 {
01040 intravlc_tab[i] = 0;
01041 }
01042
01043 for (i=0; i<3; i++)
01044 if (altscan_tab[i])
01045 {
01046 altscan_tab[i] = 0;
01047 }
01048 }
01049
01050 if (!mpeg1 && constrparms)
01051 {
01052 constrparms = 0;
01053 }
01054
01055 if (prog_seq && !cur_picture.prog_frame)
01056 {
01057 cur_picture.prog_frame = 1;
01058 }
01059
01060 if (cur_picture.prog_frame && fieldpic)
01061 {
01062 fieldpic = 0;
01063 }
01064
01065 if (!cur_picture.prog_frame && cur_picture.repeatfirst)
01066 {
01067 cur_picture.repeatfirst = 0;
01068 }
01069
01070 if (cur_picture.prog_frame)
01071 {
01072 for (i=0; i<3; i++)
01073 if (!frame_pred_dct_tab[i])
01074 {
01075 frame_pred_dct_tab[i] = 1;
01076 }
01077 }
01078
01079 if (prog_seq && !cur_picture.repeatfirst && cur_picture.topfirst)
01080 {
01081 if (!quiet)
01082 fprintf(stderr,"Warning: setting top_field_first = 0\n");
01083 cur_picture.topfirst = 0;
01084 }
01085
01086
01087 for (i=0; i<M; i++)
01088 {
01089 if (motion_data[i].sxf > (4<<motion_data[i].forw_hor_f_code)-1)
01090 {
01091 if (!quiet)
01092 fprintf(stderr,
01093 "Warning: reducing forward horizontal search width to %d\n",
01094 (4<<motion_data[i].forw_hor_f_code)-1);
01095 motion_data[i].sxf = (4<<motion_data[i].forw_hor_f_code)-1;
01096 }
01097
01098 if (motion_data[i].syf > (4<<motion_data[i].forw_vert_f_code)-1)
01099 {
01100 if (!quiet)
01101 fprintf(stderr,
01102 "Warning: reducing forward vertical search width to %d\n",
01103 (4<<motion_data[i].forw_vert_f_code)-1);
01104 motion_data[i].syf = (4<<motion_data[i].forw_vert_f_code)-1;
01105 }
01106
01107 if (i!=0)
01108 {
01109 if (motion_data[i].sxb > (4<<motion_data[i].back_hor_f_code)-1)
01110 {
01111 if (!quiet)
01112 fprintf(stderr,
01113 "Warning: reducing backward horizontal search width to %d\n",
01114 (4<<motion_data[i].back_hor_f_code)-1);
01115 motion_data[i].sxb = (4<<motion_data[i].back_hor_f_code)-1;
01116 }
01117
01118 if (motion_data[i].syb > (4<<motion_data[i].back_vert_f_code)-1)
01119 {
01120 if (!quiet)
01121 fprintf(stderr,
01122 "Warning: reducing backward vertical search width to %d\n",
01123 (4<<motion_data[i].back_vert_f_code)-1);
01124 motion_data[i].syb = (4<<motion_data[i].back_vert_f_code)-1;
01125 }
01126 }
01127 }
01128
01129 }
01130
01131
01132
01133
01134
01135
01136
01137
01138 static int quant_hfnoise_filt(int orgquant, int qmat_pos )
01139 {
01140 int x = qmat_pos % 8;
01141 int y = qmat_pos / 8;
01142 int qboost = 1024;
01143
01144 if(!use_denoise_quant)
01145 {
01146 return orgquant;
01147 }
01148
01149
01150 if( x > 4 )
01151 qboost += (256*(x-4)/3);
01152 if( y > 4 )
01153 qboost += (256*(y-4)/3);
01154
01155 return (orgquant * qboost + 512)/ 1024;
01156 }
01157
01158 static void readquantmat()
01159 {
01160 int i,v,q;
01161 load_iquant = 0;
01162 load_niquant = 0;
01163
01164 if (iqname[0]=='-')
01165 {
01166 if(use_hires_quant)
01167 {
01168 load_iquant |= 1;
01169 for (i=0; i<64; i++)
01170 {
01171 intra_q[i] = hires_intra_quantizer_matrix_hv[i];
01172 }
01173 }
01174 else
01175 {
01176 load_iquant = use_denoise_quant;
01177 for (i=0; i<64; i++)
01178 {
01179 v = quant_hfnoise_filt(default_intra_quantizer_matrix_hv[i], i);
01180 if (v<1 || v>255)
01181 error("value in intra quant matrix invalid (after noise filt adjust)");
01182 intra_q[i] = v;
01183 }
01184 }
01185 }
01186
01187
01188 if (niqname[0]=='-')
01189 {
01190 if(use_hires_quant)
01191 {
01192 for (i=0; i<64; i++)
01193 {
01194 inter_q[i] = hires_nonintra_quantizer_matrix_hv[i];
01195 }
01196 }
01197 else
01198 {
01199
01200
01201 load_niquant |= 1;
01202 for (i=0; i<64; i++)
01203 {
01204 v = quant_hfnoise_filt(default_nonintra_quantizer_matrix_hv[i],i);
01205 if (v<1 || v>255)
01206 error("value in non-intra quant matrix invalid (after noise filt adjust)");
01207 inter_q[i] = v;
01208 }
01209 }
01210 }
01211
01212 for (i=0; i<64; i++)
01213 {
01214 i_intra_q[i] = (int)(((double)IQUANT_SCALE) / ((double)intra_q[i]));
01215 i_inter_q[i] = (int)(((double)IQUANT_SCALE) / ((double)inter_q[i]));
01216 }
01217
01218 for( q = 1; q <= 112; ++q )
01219 {
01220 for (i=0; i<64; i++)
01221 {
01222 intra_q_tbl[q][i] = intra_q[i] * q;
01223 inter_q_tbl[q][i] = inter_q[i] * q;
01224 intra_q_tblf[q][i] = (float)intra_q_tbl[q][i];
01225 inter_q_tblf[q][i] = (float)inter_q_tbl[q][i];
01226 i_intra_q_tblf[q][i] = 1.0f/ ( intra_q_tblf[q][i] * 0.98);
01227 i_intra_q_tbl[q][i] = (IQUANT_SCALE/intra_q_tbl[q][i]);
01228 i_inter_q_tblf[q][i] = 1.0f/ (inter_q_tblf[q][i] * 0.98);
01229 i_inter_q_tbl[q][i] = (IQUANT_SCALE/inter_q_tbl[q][i] );
01230 }
01231 }
01232 }