/* mpeg2enc.c, main() and parameter file reading */ /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */ /* * Disclaimer of Warranty * * These software programs are available to the user without any license fee or * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims * any and all warranties, whether express, implied, or statuary, including any * implied warranties or merchantability or of fitness for a particular * purpose. In no event shall the copyright-holder be liable for any * incidental, punitive, or consequential damages of any kind whatsoever * arising from the use of these programs. * * This disclaimer of warranty extends to the user of these programs and user's * customers, employees, agents, transferees, successors, and assigns. * * The MPEG Software Simulation Group does not represent or warrant that the * programs furnished hereunder are free of infringement of any third-party * patents. * * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware, * are subject to royalty fees to patent holders. Many of these patents are * general enough such that they are unavoidable regardless of implementation * design. * */ #include #include #include #include #include #include #include #define GLOBAL /* used by global.h */ #include "config.h" #include "global.h" #include "common.h" /* private prototypes */ static void init _ANSI_ARGS_((void)); static void readparmfile _ANSI_ARGS_((char *fname)); static void readquantmat _ANSI_ARGS_((void)); int eysk, eusk, evsk, epsk; int outsk; int main(argc, argv) int argc; char *argv[]; { int n; int s, icli; struct sockaddr_in sin, cli; struct sockaddr_un py = { AF_UNIX, "new0.Y" }; struct sockaddr_un pu = { AF_UNIX, "new0.U" }; struct sockaddr_un pv = { AF_UNIX, "new0.V" }; struct sockaddr_un pp = { AF_UNIX, "new0.P" }; /******************THIS FILE WAS CREATED BY KENT MEDIANET**********************/ FILE *net_par; char srv_id[10], srv_ip[50], srv_port[10]; char xcoder_id[10], xcoder_ip[10], xcoder_port[10]; enc_dynamic_bypass_on = ON; /* This part is commented to disable communication with player*/ /*************************net.par************************/ if ((net_par = fopen("net.par", "r")) == NULL) printf("Usage:[net.par] could not opened\n"); else { fscanf(net_par, "%s\t%s\t%s\n", srv_id, srv_ip, srv_port); fscanf(net_par, "%s\t%s\t%s\n", xcoder_id, xcoder_ip, xcoder_port); } fclose(net_par); printf("XCODER: assined port for communication with player %d\n", atoi(xcoder_port)); /*************************************************/ s = socket(AF_INET, SOCK_STREAM, 0); bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(atoi(xcoder_port)); bind(s, (struct sockaddr *) &sin, sizeof(sin)); listen(s, 4096); printf("XCODER: waiting for connection with player ...\n"); outsk = accept(s, (struct sockaddr *) &cli, &icli); flag = 0; n = 0; if (argc < 3) { printf("\n%s, %s\n", version, author); printf("Usage: mpeg2encode in.par out.m2v\n"); exit(0); } sscanf(argv[3], "%d", &flag); sleep(2); eysk = socket(AF_UNIX, SOCK_STREAM, 0); if (connect(eysk, &py, sizeof(py)) == -1) { perror("Couldn't open eysk.\n"); close(eysk); unlink(py.sun_path); exit(1); } sleep(2); eusk = socket(AF_UNIX, SOCK_STREAM, 0); if (connect(eusk, &pu, sizeof(pu)) == -1) { perror("Couldn't open eusk.\n"); close(eusk); unlink(pu.sun_path); exit(1); } sleep(2); evsk = socket(AF_UNIX, SOCK_STREAM, 0); if (connect(evsk, &pv, sizeof(pv)) == -1) { perror("Couldn't open evsk.\n"); close(evsk); unlink(pv.sun_path); exit(1); } sleep(2); epsk = socket(AF_UNIX, SOCK_STREAM, 0); if (connect(epsk, &pp, sizeof(pp)) == -1) { perror("Couldn't open epsk.\n"); close(epsk); unlink(pp.sun_path); exit(1); } printf("XCODER: connection with player is established\n"); /* read parameter file */ readparmfile(argv[1]); /* read quantization matrices */ /*readquantmat();*/ /* open output file */ if (!(outfile = fopen(argv[2], "wb"))) { sprintf(errortext, "Couldn't create output file %s", argv[2]); error(errortext); } init(); putseq(); fclose(outfile); fclose(statfile); close(eysk); close(eusk); close(evsk); close(epsk); return 0; } static void init() { int i, size; static int block_count_tab[3] = { 6, 8, 12 }; initbits(); init_fdct(); init_idct(); /* round picture dimensions to nearest multiple of 16 or 32 */ mb_width = (horizontal_size + 15) / 16; mb_height = prog_seq ? (vertical_size + 15) / 16 : 2 * ((vertical_size + 31) / 32); mb_height2 = fieldpic ? mb_height >> 1 : mb_height; /* for field pictures */ width = 16 * mb_width; height = 16 * mb_height; chrom_width = (chroma_format == CHROMA444) ? width : width >> 1; chrom_height = (chroma_format != CHROMA420) ? height : height >> 1; height2 = fieldpic ? height >> 1 : height; width2 = fieldpic ? width << 1 : width; chrom_width2 = fieldpic ? chrom_width << 1 : chrom_width; block_count = block_count_tab[chroma_format - 1]; /* clip table */ if (!(clp = (unsigned char *) malloc(1024))) error("malloc failed\n"); clp += 384; for (i = -384; i < 640; i++) clp[i] = (i < 0) ? 0 : ((i > 255) ? 255 : i); for (i = 0; i < 3; i++) { size = (i == 0) ? width * height : chrom_width * chrom_height; if (!(newrefframe[i] = (unsigned char *) malloc(size))) error("malloc failed\n"); if (!(oldrefframe[i] = (unsigned char *) malloc(size))) error("malloc failed\n"); if (!(auxframe[i] = (unsigned char *) malloc(size))) error("malloc failed\n"); if (!(neworgframe[i] = (unsigned char *) malloc(size))) error("malloc failed\n"); if (!(oldorgframe[i] = (unsigned char *) malloc(size))) error("malloc failed\n"); if (!(auxorgframe[i] = (unsigned char *) malloc(size))) error("malloc failed\n"); if (!(predframe[i] = (unsigned char *) malloc(size))) error("malloc failed\n"); } mbinfo = (struct mbinfo *) malloc(mb_width * mb_height2 * sizeof(struct mbinfo)); if (!mbinfo) error("malloc failed\n"); blocks = (short (*)[64]) malloc(mb_width * mb_height2 * block_count * sizeof(short[64])); if (!blocks) error("malloc failed\n"); /* open statistics output file */ if (statname[0] == '-') statfile = stdout; else if (!(statfile = fopen(statname, "w"))) { sprintf(errortext, "Couldn't create statistics output file %s", statname); error(errortext); } } void error(text) char *text; { fprintf(stderr, text); putc('\n', stderr); exit(1); } static void readparmfile(fname) char *fname; { int i; int h, m, s, f; FILE *fd; char str[256]; static double ratetab[8] = { 24000.0 / 1001.0, 24.0, 25.0, 30000.0 / 1001.0, 30.0, 50.0, 60000.0 / 1001.0, 60.0 }; extern int r, Xi, Xb, Xp, d0i, d0p, d0b; /* rate control */ extern double avg_act; /* rate control */ ratepar = "rate.par"; if (!(fdp = fopen(ratepar, "r"))) { sprintf(errortext, "Couldn't open parameter file rate.par"); error(errortext); } fgets(line, 254, fdp); sscanf(line, "%lf", &ratep); /***********************KENT MEDIANET CHANGES *********************/ /*Reading net par file and coparing old bit rate with a new one, if the new one is different the program will notify user about that*/ if (old_ratep != ratep) { printf("XCODER: changing rate parameter from %.1f to %.1f\n", old_ratep, ratep); } old_ratep = ratep; /***********************KENT MEDIANET CHANGES END*********************/ fclose(fdp); if (!(fd = fopen(fname, "r"))) { sprintf(errortext, "Couldn't open parameter file %s", fname); error(errortext); } fgets(id_string, 254, fd); fgets(line, 254, fd); sscanf(line, "%d", &N); fgets(line, 254, fd); sscanf(line, "%d", &M); if (N < 1) error("N must be positive"); if (M < 1) error("M must be positive"); if (N % M != 0) error("N must be an integer multiple of M"); motion_data = (struct motion_data *) malloc(M * sizeof(struct motion_data)); if (!motion_data) error("malloc failed\n"); for (i = 0; i < M; i++) { /* if (i == 0) { motion_data[i].forw_hor_f_code = 3; motion_data[i].forw_vert_f_code = 3; motion_data[i].sxf = 31; motion_data[i].syf = 31; } else { motion_data[i].forw_hor_f_code = 2; motion_data[i].forw_vert_f_code = 2; motion_data[i].sxf = 15; motion_data[i].syf = 15; motion_data[i].back_hor_f_code = 2; motion_data[i].back_vert_f_code = 2; motion_data[i].sxb = 15; motion_data[i].syb = 15; } */ if (i == 0) { motion_data[i].forw_hor_f_code = 2; motion_data[i].forw_vert_f_code = 2; motion_data[i].sxf = 15; motion_data[i].syf = 15; } else { motion_data[i].forw_hor_f_code = 1; motion_data[i].forw_vert_f_code = 1; motion_data[i].sxf = 7; motion_data[i].syf = 7; motion_data[i].back_hor_f_code = 1; motion_data[i].back_vert_f_code = 1; motion_data[i].sxb = 7; motion_data[i].syb = 7; } } fclose(fd); strcpy(id_string, ""); /* design some parameter for mpeg2 AVT */ strcpy(tplorg, "mid%d"); strcpy(tplref, "-"); strcpy(statname, "stat.out"); inputtype = 0; mpeg1 = 0; nframes = 100; fieldpic = 0; P = 0; r = 0; avg_act = 0; Xi = 0; Xp = 0; Xb = 0; d0i = 0; d0p = 0; d0b = 0; frame_pred_dct_tab[0] = 0; frame_pred_dct_tab[1] = 0; frame_pred_dct_tab[2] = 0; conceal_tab[0] = 0; conceal_tab[1] = 0; conceal_tab[2] = 0; qscale_tab[0] = 1; qscale_tab[1] = 1; qscale_tab[2] = 1; intravlc_tab[0] = 1; intravlc_tab[1] = 0; intravlc_tab[2] = 0; altscan_tab[0] = 0; altscan_tab[1] = 0; altscan_tab[2] = 0; /* get parameters from the dec */ read(epsk, line, 256); sscanf(line, "%s%d", str, &horizontal_size); /* printf("**** %s%d",line,horizontal_size); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &vertical_size); /* printf("**** %s%d",line,vertical_size); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &aspectratio); /* printf("**** %s%d",line,aspectratio); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &frame_rate_code); /* printf("**** %s%d",line,frame_rate_code); */ read(epsk, line, 256); sscanf(line, "%s%lf", str, &bit_rate); bit_rate = 400.0 * bit_rate; /* printf("**** %s%lf",line,bit_rate); */ /*printf("bit_rate = %lf %lf\n",bit_rate,ratep); if(ratep!=0.0) bit_rate = ratep; */ /* printf("bit_rate = %lf\n",bit_rate); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &vbv_buffer_size); /* printf("**** %s%d",line,vbv_buffer_size); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &constrparms); /* printf("**** %s%d",line,constrparms); */ read(epsk, line, 256); sscanf(line, "%s%s", str, iqname); /* printf("**** %s %s",line,iqname); */ read(epsk, line, 256); sscanf(line, "%s%s", str, niqname); /* printf("**** %s%s",line,niqname); */ readquantmat(); read(epsk, line, 256); sscanf(line, "%s%d", str, &profile); /* printf("**** %s %d",line,profile); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &level); /* printf("**** %s %d",line,level); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &prog_seq); /* printf("**** %s %d",line,prog_seq); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &chroma_format); /* printf("**** %s%d",line,chroma_format); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &low_delay); /* printf("**** %s %d",line,low_delay); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &video_format); /* printf("**** %s %d",line,video_format); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &color_primaries); /* printf("**** %s %d",line,color_primaries); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &transfer_characteristics); /* printf("**** %s %d",line,transfer_characteristics); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &matrix_coefficients); /* printf("**** %s %d",line,matrix_coefficients); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &display_horizontal_size); /* printf("**** %s %d",line,display_horizontal_size); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &display_vertical_size); /* printf("**** %s %d",line,display_vertical_size); */ read(epsk, line, 256); sscanf(line, "%s %d:%02d:%02d:%02d", str, &h, &m, &s, &f); /* printf("**** %s %d:%02d:%02d:%02d\n",line,h,m,s,f); */ frame0 = f; read(epsk, line, 256); sscanf(line, "%s%d", str, &dc_prec); /* printf("**** %s %d",line,dc_prec); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &topfirst); /* printf("**** %s %d",line,topfirst); */ read(epsk, line, 256); sscanf(line, "%s%d", str, &repeatfirst); /* printf("**** %s %d",line,repeatfirst); */ do { i = read(epsk, line, 256); } while (!i); sscanf(line, "%s%d", str, &prog_frame); /* printf("**** %s %d ",line,prog_frame); */ /* make flags boolean (x!=0 -> x=1) */ mpeg1 = !!mpeg1; fieldpic = !!fieldpic; low_delay = !!low_delay; constrparms = !!constrparms; prog_seq = !!prog_seq; topfirst = !!topfirst; for (i = 0; i < 3; i++) { frame_pred_dct_tab[i] = !!frame_pred_dct_tab[i]; conceal_tab[i] = !!conceal_tab[i]; qscale_tab[i] = !!qscale_tab[i]; intravlc_tab[i] = !!intravlc_tab[i]; altscan_tab[i] = !!altscan_tab[i]; } repeatfirst = !!repeatfirst; prog_frame = !!prog_frame; /* make sure MPEG specific parameters are valid */ range_checks(); frame_rate = ratetab[frame_rate_code - 1]; /* timecode -> frame number */ tc0 = h; tc0 = 60 * tc0 + m; tc0 = 60 * tc0 + s; tc0 = (int) (frame_rate + 0.5) * tc0 + f; if (!mpeg1) { profile_and_level_checks(); } else { /* MPEG-1 */ if (constrparms) { if (horizontal_size > 768 || vertical_size > 576 || ((horizontal_size + 15) / 16) * ((vertical_size + 15) / 16) > 396 || ((horizontal_size + 15) / 16) * ((vertical_size + 15) / 16) * frame_rate > 396 * 25.0 || frame_rate > 30.0) { if (!quiet) fprintf(stderr, "Warning: setting constrained_parameters_flag = 0\n"); constrparms = 0; } } if (constrparms) { for (i = 0; i < M; i++) { if (motion_data[i].forw_hor_f_code > 4) { if (!quiet) fprintf(stderr, "Warning: setting constrained_parameters_flag = 0\n"); constrparms = 0; break; } if (motion_data[i].forw_vert_f_code > 4) { if (!quiet) fprintf(stderr, "Warning: setting constrained_parameters_flag = 0\n"); constrparms = 0; break; } if (i != 0) { if (motion_data[i].back_hor_f_code > 4) { if (!quiet) fprintf(stderr, "Warning: setting constrained_parameters_flag = 0\n"); constrparms = 0; break; } if (motion_data[i].back_vert_f_code > 4) { if (!quiet) fprintf(stderr, "Warning: setting constrained_parameters_flag = 0\n"); constrparms = 0; break; } } } } } /* relational checks */ if (mpeg1) { if (!prog_seq) { if (!quiet) fprintf(stderr, "Warning: setting progressive_sequence = 1\n"); prog_seq = 1; } if (chroma_format != CHROMA420) { if (!quiet) fprintf(stderr, "Warning: setting chroma_format = 1 (4:2:0)\n"); chroma_format = CHROMA420; } if (dc_prec != 0) { if (!quiet) fprintf(stderr, "Warning: setting intra_dc_precision = 0\n"); dc_prec = 0; } for (i = 0; i < 3; i++) if (qscale_tab[i]) { if (!quiet) fprintf(stderr, "Warning: setting qscale_tab[%d] = 0\n", i); qscale_tab[i] = 0; } for (i = 0; i < 3; i++) if (intravlc_tab[i]) { if (!quiet) fprintf(stderr, "Warning: setting intravlc_tab[%d] = 0\n", i); intravlc_tab[i] = 0; } for (i = 0; i < 3; i++) if (altscan_tab[i]) { if (!quiet) fprintf(stderr, "Warning: setting altscan_tab[%d] = 0\n", i); altscan_tab[i] = 0; } } if (!mpeg1 && constrparms) { if (!quiet) fprintf(stderr, "Warning: setting constrained_parameters_flag = 0\n"); constrparms = 0; } if (prog_seq && !prog_frame) { if (!quiet) fprintf(stderr, "Warning: setting progressive_frame = 1\n"); prog_frame = 1; } if (prog_frame && fieldpic) { if (!quiet) fprintf(stderr, "Warning: setting field_pictures = 0\n"); fieldpic = 0; } if (!prog_frame && repeatfirst) { if (!quiet) fprintf(stderr, "Warning: setting repeat_first_field = 0\n"); repeatfirst = 0; } if (prog_frame) { for (i = 0; i < 3; i++) if (!frame_pred_dct_tab[i]) { if (!quiet) fprintf(stderr, "Warning: setting frame_pred_frame_dct[%d] = 1\n", i); frame_pred_dct_tab[i] = 1; } } if (prog_seq && !repeatfirst && topfirst) { if (!quiet) fprintf(stderr, "Warning: setting top_field_first = 0\n"); topfirst = 0; } /* search windows */ for (i = 0; i < M; i++) { if (motion_data[i].sxf > (4 << motion_data[i].forw_hor_f_code) - 1) { if (!quiet) fprintf(stderr, "Warning: reducing forward horizontal search width to %d\n", (4 << motion_data[i].forw_hor_f_code) - 1); motion_data[i].sxf = (4 << motion_data[i].forw_hor_f_code) - 1; } if (motion_data[i].syf > (4 << motion_data[i].forw_vert_f_code) - 1) { if (!quiet) fprintf(stderr, "Warning: reducing forward vertical search width to %d\n", (4 << motion_data[i].forw_vert_f_code) - 1); motion_data[i].syf = (4 << motion_data[i].forw_vert_f_code) - 1; } if (i != 0) { if (motion_data[i].sxb > (4 << motion_data[i].back_hor_f_code) - 1) { if (!quiet) fprintf(stderr, "Warning: reducing backward horizontal search width to %d\n", (4 << motion_data[i].back_hor_f_code) - 1); motion_data[i].sxb = (4 << motion_data[i].back_hor_f_code) - 1; } if (motion_data[i].syb > (4 << motion_data[i].back_vert_f_code) - 1) { if (!quiet) fprintf(stderr, "Warning: reducing backward vertical search width to %d\n", (4 << motion_data[i].back_vert_f_code) - 1); motion_data[i].syb = (4 << motion_data[i].back_vert_f_code) - 1; } } } } static void readquantmat() { int i, v; /* FILE *fd; */ if (iqname[0] == '0') { /* use default intra matrix */ load_iquant = 0; for (i = 0; i < 64; i++) intra_q[i] = default_intra_quantizer_matrix[i]; } else { /* read customized intra matrix */ load_iquant = 1; /*if (!(fd = fopen(iqname,"r"))) { sprintf(errortext,"Couldn't open quant matrix file %s",iqname); error(errortext); } */ for (i = 0; i < 64; i++) { read(epsk, line, 256); sscanf(line, "%d", &v); if (v < 1 || v > 255) error("invalid value in quant matrix"); intra_q[i] = v; } /* fclose(fd); */ } if (niqname[0] == '0') { /* use default non-intra matrix */ load_niquant = 0; for (i = 0; i < 64; i++) inter_q[i] = 16; } else { /* read customized non-intra matrix */ load_niquant = 1; /*if (!(fd = fopen(niqname,"r"))) { sprintf(errortext,"Couldn't open quant matrix file %s",niqname); error(errortext); } */ for (i = 0; i < 64; i++) { read(epsk, line, 256); sscanf(line, "%d", &v); if (v < 1 || v > 255) error("invalid value in quant matrix"); inter_q[i] = v; } /* fclose(fd); */ } } /******************THIS FILE WAS CREATED BY KENT MEDIANET**********************/ void Read_Decodedframe_Bypass() { FILE *ebp; char temp_name[256]; chdir("./test"); memset(frameinfo, '\0', 100); sprintf(frameinfo, "encframebypass%d.txt", encframe); if((ebp = fopen(frameinfo, "r")) == NULL) { enc_dynamic_bypass_on = ON; if(ECHO) printf("FILE encframebypass%d.txt NOT FOUND.SET ENC_DYNAMIC_BYPASS ON\n",encframe); } else { fscanf(ebp, "%d %d %d\n",&enc_dynamic_bypass_on, &enc_frame_bypass_on, &enc_framecount); if(ECHO)printf("ENC: read bypass.par %d %d %d\n, file %sd\n",enc_dynamic_bypass_on, enc_frame_bypass_on,enc_framecount,frameinfo); } fclose(ebp); memset(temp_name, '\0', 256); sprintf(temp_name, "rm %s", frameinfo); system(temp_name); }