int main(void) { unsigned long i,j,msize,*ija; float **a,*sa,*ax,*b; static float ainit[NP][NP]={ 3.0,0.0,1.0,0.0,0.0, 0.0,4.0,0.0,0.0,0.0, 0.0,7.0,5.0,9.0,0.0, 0.0,0.0,0.0,0.0,2.0, 0.0,0.0,0.0,6.0,5.0}; static float x[NP+1]={0.0,1.0,2.0,3.0,4.0,5.0}; ija=lvector(1,NMAX); ax=vector(1,NP); b=vector(1,NP); sa=vector(1,NMAX); a=convert_matrix(&ainit[0][0],1,NP,1,NP); sprsin(a,NP,0.5,NMAX,sa,ija); msize=ija[1]-2; sprstx(sa,ija,x,b,msize); for (i=1;i<=msize;i++) for (ax[i]=0.0,j=1;j<=msize;j++) ax[i] += a[j][i]*x[j]; printf("\tReference\tsprstx result\n"); for (i=1;i<=msize;i++) printf("\t%5.2f\t\t%5.2f\n",ax[i],b[i]); free_convert_matrix(a,1,NP,1,NP); free_vector(sa,1,NMAX); free_vector(b,1,NP); free_vector(ax,1,NP); free_lvector(ija,1,NMAX); return 0; }
HYPRE_Int hypre_ParaSailsSetupValues(hypre_ParaSails obj, HYPRE_DistributedMatrix *distmat, HYPRE_Real filter, HYPRE_Real loadbal, HYPRE_Int logging) { Matrix *mat; hypre_ParaSails_struct *internal = (hypre_ParaSails_struct *) obj; HYPRE_Int err; mat = convert_matrix(internal->comm, distmat); internal->ps->loadbal_beta = loadbal; internal->ps->setup_pattern_time = 0.0; err = ParaSailsSetupValues(internal->ps, mat, filter); if (logging) ParaSailsStatsValues(internal->ps, mat); MatrixDestroy(mat); if (err) { hypre_error(HYPRE_ERROR_GENERIC); } return hypre_error_flag; }
static inline int writemm( char const* const filename, matrix_t* AA, char const * const comment, enum sparse_matrix_file_format_t ext ) { // make sure we're in the right format (COO) and base 0 int ret; if (( ret = convert_matrix( AA, SM_COO, FIRST_INDEX_ZERO ) ) != 0 ) return ret; assert( AA->format == SM_COO ); assert( AA->base == FIRST_INDEX_ZERO ); assert( AA->sym == SM_UNSYMMETRIC ); assert( AA->data_type == REAL_DOUBLE ); // copy data into the BeBOP format struct sparse_matrix_t A; struct coo_matrix_t Acoo; A.format = COO; A.repr = &Acoo; Acoo.m = AA->m; Acoo.n = AA->n; Acoo.nnz = AA->nz; Acoo.II = ( signed int* ) AA->ii; Acoo.JJ = ( signed int* ) AA->jj; Acoo.val = AA->dd; Acoo.index_base = ZERO; Acoo.symmetry_type = UNSYMMETRIC; Acoo.value_type = REAL; Acoo.ownership = USER_DEALLOCATES; // don't let BeBOP blow away our data save_sparse_matrix( filename, &A, ext ); return 0; // success }
/* * Invert the matrix if x = -1 * use gaussj */ FMatrix FMatrix::operator^( int x ) const { assert( x == -1 ); /* may want to extend to 2... in the future */ // check dimension assert( XDim() == YDim() ); // convience variables int n = XDim(); int size = n*n; // make a copy of the matrix for nrc routine to work on float *copy = new float[ size ]; memcpy( copy, elem, size*sizeof(float) ); float **a = convert_matrix( copy, 1, n, 1, n ); float **b = matrix( 1, n, 1, 1 ); b[1][1] = 1.0F; for( int i = 2; i <= n; i++ ) b[i][1] = 0.0F; gaussj( a, n, b, 1 ); FMatrix ret( n, n ); for( int iy = 0; iy < n; iy++ ) for( int ix = 0; ix < n; ix++ ) ret.Elem( iy, ix ) = copy[ iy*n + ix ]; delete [] copy; free_matrix( b, 1, n, 1, 1 ); free_convert_matrix( a, 1, n, 1, n ); return( ret ); }
int main(void) { int i,j; static float c[NP][NP]= {1.0,2.0,0.0,0.0,0.0, -2.0,3.0,0.0,0.0,0.0, 3.0,4.0,50.0,0.0,0.0, -4.0,5.0,-60.0,7.0,0.0, -5.0,6.0,-70.0,8.0,-9.0}; float *wr,*wi,**a; wr=vector(1,NP); wi=vector(1,NP); a=convert_matrix(&c[0][0],1,NP,1,NP); printf("matrix:\n"); for (i=1;i<=NP;i++) { for (j=1;j<=NP;j++) printf("%12.2f",a[i][j]); printf("\n"); } balanc(a,NP); elmhes(a,NP); hqr(a,NP,wr,wi); printf("eigenvalues:\n"); printf("%11s %16s \n","real","imag."); for (i=1;i<=NP;i++) printf("%15f %14f\n",wr[i],wi[i]); free_convert_matrix(a,1,NP,1,NP); free_vector(wi,1,NP); free_vector(wr,1,NP); return 0; }
// save a .mat matlab matrix // will store a single matrix (sparse or dense) in a .mat file // returns 0 on success static int write_mat(char const *const filename, matrix_t *const A) { #ifndef HAVE_MATIO return 1; #else const char created_by[] = "created by " PACKAGE_STRING; // "created by Meagre-Crowd x.y.z" mat_t* matfp; matfp = Mat_Create(filename, created_by); if(matfp == NULL) return 1; // failed to open file // create a matrix convert into matvar_t* t = NULL; int ret = 0; if(A->format == DCOL || A->format == DROW) { // dense ret = convert_matrix(A, DCOL, FIRST_INDEX_ZERO); // DROW -> DCOL if DROW if(ret != 0) ret = 2; // conversion failure if(ret == 0) { // TODO check for integer overflow in cast from unsigned int -> int size_t dims[] = { A->m, A->n }; t = Mat_VarCreate( "x", MAT_C_DOUBLE, MAT_T_DOUBLE, 2, // always at least rank 2 dims, A->dd, 0 // MAT_F_COMPLEX if complex, could avoid copying data: MAT_F_DONT_COPY_DATA if not sparse ); if(t == NULL) { ret = 3; // failed to malloc data for storage } else { ret = Mat_VarWrite(matfp, t, 1); // compress if(ret != 0) ret = 4; // failed data write } } } else { // sparse assert(0); // do we ever get sparse results? } Mat_Close(matfp); Mat_VarFree(t); return ret; // success? #endif }
int main(void) { int i,j,nrot; static float c[NP][NP]= {5.0,4.3,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,-4.0, 4.3,5.1,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0, 3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0, 2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0, 1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0, 0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0, -1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0, -2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0, -3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0, -4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0}; float *d,**v,**e; d=vector(1,NP); v=matrix(1,NP,1,NP); e=convert_matrix(&c[0][0],1,NP,1,NP); printf("****** Finding Eigenvectors ******\n"); jacobi(e,NP,d,v,&nrot); printf("unsorted eigenvectors:\n"); for (i=1;i<=NP;i++) { printf("eigenvalue %3d = %12.6f\n",i,d[i]); printf("eigenvector:\n"); for (j=1;j<=NP;j++) { printf("%12.6f",v[j][i]); if ((j % 5) == 0) printf("\n"); } printf("\n"); } printf("\n****** Sorting Eigenvectors ******\n\n"); eigsrt(d,v,NP); printf("sorted eigenvectors:\n"); for (i=1;i<=NP;i++) { printf("eigenvalue %3d = %12.6f\n",i,d[i]); printf("eigenvector:\n"); for (j=1;j<=NP;j++) { printf("%12.6f",v[j][i]); if ((j % 5) == 0) printf("\n"); } printf("\n"); } free_convert_matrix(e,1,NP,1,NP); free_matrix(v,1,NP,1,NP); free_vector(d,1,NP); return 0; }
int main(void) { int i,icase,j,*izrov,*iposv; static float c[MP][NP]= {0.0,1.0,1.0,3.0,-0.5, 740.0,-1.0,0.0,-2.0,0.0, 0.0,0.0,-2.0,0.0,7.0, 0.5,0.0,-1.0,1.0,-2.0, 9.0,-1.0,-1.0,-1.0,-1.0, 0.0,0.0,0.0,0.0,0.0}; float **a; static char *txt[NM1M2+1]= {" ","x1","x2","x3","x4","y1","y2","y3"}; izrov=ivector(1,N); iposv=ivector(1,M); a=convert_matrix(&c[0][0],1,MP,1,NP); simplx(a,M,N,M1,M2,M3,&icase,izrov,iposv); if (icase == 1) printf("\nunbounded objective function\n"); else if (icase == -1) printf("\nno solutions satisfy constraints given\n"); else { printf("\n%11s"," "); for (i=1;i<=N;i++) if (izrov[i] <= NM1M2) printf("%10s",txt[izrov[i]]); printf("\n"); for (i=1;i<=M+1;i++) { if (i == 1 || iposv[i-1] <= NM1M2) { if (i > 1) printf("%s",txt[iposv[i-1]]); else printf(" "); printf("%10.2f",a[i][1]); for (j=2;j<=N+1;j++) if (izrov[j-1] <= NM1M2) printf("%10.2f",a[i][j]); printf("\n"); } } } free_convert_matrix(a,1,MP,1,NP); free_ivector(iposv,1,M); free_ivector(izrov,1,N); return 0; }
int main(void) { int i,j,*indx; long idum=(-13); float d,*x,**a,**aa; static float ainit[NP][NP]= {1.0,2.0,3.0,4.0,5.0, 2.0,3.0,4.0,5.0,1.0, 1.0,1.0,1.0,1.0,1.0, 4.0,5.0,1.0,2.0,3.0, 5.0,1.0,2.0,3.0,4.0}; static float b[N+1]={0.0,1.0,1.0,1.0,1.0,1.0}; indx=ivector(1,N); x=vector(1,N); a=convert_matrix(&ainit[0][0],1,N,1,N); aa=matrix(1,N,1,N); for (i=1;i<=N;i++) { x[i]=b[i]; for (j=1;j<=N;j++) aa[i][j]=a[i][j]; } ludcmp(aa,N,indx,&d); lubksb(aa,N,indx,x); printf("\nSolution vector for the equations:\n"); for (i=1;i<=N;i++) printf("%12.6f",x[i]); printf("\n"); /* now phoney up x and let mprove fix it */ for (i=1;i<=N;i++) x[i] *= (1.0+0.2*ran3(&idum)); printf("\nSolution vector with noise added:\n"); for (i=1;i<=N;i++) printf("%12.6f",x[i]); printf("\n"); mprove(a,aa,N,indx,b,x); printf("\nSolution vector recovered by mprove:\n"); for (i=1;i<=N;i++) printf("%12.6f",x[i]); printf("\n"); free_matrix(aa,1,N,1,N); free_convert_matrix(a,1,N,1,N); free_vector(x,1,N); free_ivector(indx,1,N); return 0; }
/*this function takes as a parameter the Laplacian matrix and returns the array of its eigenvalues*/ float eig2Value(float **matrix1){ int i,j; float *d, *r, **v, **e; float matrix2[sizeOfLaplacianMatrix][sizeOfLaplacianMatrix]; for (i=0; i<sizeOfLaplacianMatrix; i++) for (j=0; j<sizeOfLaplacianMatrix; j++) matrix2[i][j] = matrix1[i][j]; d = vector(1,sizeOfLaplacianMatrix); r = vector(1,sizeOfLaplacianMatrix); v = matrix(1,sizeOfLaplacianMatrix,1,sizeOfLaplacianMatrix); e = convert_matrix(&matrix2[0][0],1,sizeOfLaplacianMatrix, 1,sizeOfLaplacianMatrix); jacobi(e,sizeOfLaplacianMatrix,d,v,&nrot); eigsrt(d,v,sizeOfLaplacianMatrix); return d[sizeOfLaplacianMatrix-1]; }
HYPRE_Int hypre_ParaSailsSetup(hypre_ParaSails obj, HYPRE_DistributedMatrix *distmat, HYPRE_Int sym, HYPRE_Real thresh, HYPRE_Int nlevels, HYPRE_Real filter, HYPRE_Real loadbal, HYPRE_Int logging) { /* HYPRE_Real cost; */ Matrix *mat; hypre_ParaSails_struct *internal = (hypre_ParaSails_struct *) obj; HYPRE_Int err; mat = convert_matrix(internal->comm, distmat); ParaSailsDestroy(internal->ps); internal->ps = ParaSailsCreate(internal->comm, mat->beg_row, mat->end_row, sym); ParaSailsSetupPattern(internal->ps, mat, thresh, nlevels); if (logging) /* cost = */ ParaSailsStatsPattern(internal->ps, mat); internal->ps->loadbal_beta = loadbal; err = ParaSailsSetupValues(internal->ps, mat, filter); if (logging) ParaSailsStatsValues(internal->ps, mat); MatrixDestroy(mat); if (err) { hypre_error(HYPRE_ERROR_GENERIC); } return hypre_error_flag; }
HYPRE_Int hypre_ParaSailsSetupPattern(hypre_ParaSails obj, HYPRE_DistributedMatrix *distmat, HYPRE_Int sym, HYPRE_Real thresh, HYPRE_Int nlevels, HYPRE_Int logging) { /* HYPRE_Real cost; */ Matrix *mat; hypre_ParaSails_struct *internal = (hypre_ParaSails_struct *) obj; mat = convert_matrix(internal->comm, distmat); ParaSailsDestroy(internal->ps); internal->ps = ParaSailsCreate(internal->comm, mat->beg_row, mat->end_row, sym); ParaSailsSetupPattern(internal->ps, mat, thresh, nlevels); if (logging) /* cost = */ ParaSailsStatsPattern(internal->ps, mat); MatrixDestroy(mat); return hypre_error_flag; }
int main(void) { int i,j,k,kk,l,ll,nrot; static float a[3][3]= {1.0,2.0,3.0, 2.0,2.0,3.0, 3.0,3.0,3.0}; static float b[5][5]= {-2.0,-1.0,0.0,1.0,2.0, -1.0,-1.0,0.0,1.0,2.0, 0.0,0.0,0.0,1.0,2.0, 1.0,1.0,1.0,1.0,2.0, 2.0,2.0,2.0,2.0,2.0}; static float c[NP][NP]= {5.0,4.3,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,-4.0, 4.3,5.1,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0, 3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0, 2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0, 1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0, 0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0, -1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0, -2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0, -3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0, -4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0}; float *d,*r,**v,**e; static int num[4]={0,3,5,10}; d=vector(1,NP); r=vector(1,NP); v=matrix(1,NP,1,NP); for (i=1;i<=NMAT;i++) { if (i == 1) e=convert_matrix(&a[0][0],1,num[i],1,num[i]); else if (i == 2) e=convert_matrix(&b[0][0],1,num[i],1,num[i]); else if (i == 3) e=convert_matrix(&c[0][0],1,num[i],1,num[i]); jacobi(e,num[i],d,v,&nrot); printf("matrix number %2d\n",i); printf("number of JACOBI rotations: %3d\n",nrot); printf("eigenvalues: \n"); for (j=1;j<=num[i];j++) { printf("%12.6f",d[j]); if ((j % 5) == 0) printf("\n"); } printf("\neigenvectors:\n"); for (j=1;j<=num[i];j++) { printf("%9s %3d \n","number",j); for (k=1;k<=num[i];k++) { printf("%12.6f",v[k][j]); if ((k % 5) == 0) printf("\n"); } printf("\n"); } /* eigenvector test */ printf("eigenvector test\n"); for (j=1;j<=num[i];j++) { for (l=1;l<=num[i];l++) { r[l]=0.0; for (k=1;k<=num[i];k++) { if (k > l) { kk=l; ll=k; } else { kk=k; ll=l; } r[l] += (e[ll][kk]*v[k][j]); } } printf("vector number %3d\n",j); printf("%11s %14s %10s\n", "vector","mtrx*vec.","ratio"); for (l=1;l<=num[i];l++) printf("%12.6f %12.6f %12.6f\n", v[l][j],r[l],r[l]/v[l][j]); } printf("press RETURN to continue...\n"); (void) getchar(); free_convert_matrix(e,1,num[i],1,num[i]); } free_matrix(v,1,NP,1,NP); free_vector(r,1,NP); free_vector(d,1,NP); return 0; }
static int PySip_init( PySip* self, PyObject* args, /*@unused@*/ PyObject* kwds) { PyObject* py_a = NULL; PyObject* py_b = NULL; PyObject* py_ap = NULL; PyObject* py_bp = NULL; PyObject* py_crpix = NULL; PyArrayObject* a = NULL; PyArrayObject* b = NULL; PyArrayObject* ap = NULL; PyArrayObject* bp = NULL; PyArrayObject* crpix = NULL; double* a_data = NULL; double* b_data = NULL; double* ap_data = NULL; double* bp_data = NULL; unsigned int a_order = 0; unsigned int b_order = 0; unsigned int ap_order = 0; unsigned int bp_order = 0; int status = -1; if (!PyArg_ParseTuple(args, "OOOOO:Sip.__init__", &py_a, &py_b, &py_ap, &py_bp, &py_crpix)) { return -1; } if (convert_matrix(py_a, &a, &a_data, &a_order) || convert_matrix(py_b, &b, &b_data, &b_order) || convert_matrix(py_ap, &ap, &ap_data, &ap_order) || convert_matrix(py_bp, &bp, &bp_data, &bp_order)) { goto exit; } crpix = (PyArrayObject*)PyArray_ContiguousFromAny(py_crpix, PyArray_DOUBLE, 1, 1); if (crpix == NULL) { goto exit; } if (PyArray_DIM(crpix, 0) != 2) { PyErr_SetString(PyExc_ValueError, "CRPIX wrong length"); goto exit; } status = sip_init(&self->x, a_order, a_data, b_order, b_data, ap_order, ap_data, bp_order, bp_data, PyArray_DATA(crpix)); exit: Py_XDECREF(a); Py_XDECREF(b); Py_XDECREF(ap); Py_XDECREF(bp); Py_XDECREF(crpix); if (status == 0) { return 0; } else if (status == -1) { /* Exception already set */ return -1; } else { wcserr_to_python_exc(self->x.err); return -1; } }
int main(void) { unsigned long i,j,k,*ija,*ijb,*ijbt,*ijc; float *sa,*sb,*sbt,*sc,**a,**b,**c,**ab; static float ainit[NP][NP]={ 1.0,0.5,0.0,0.0,0.0, 0.5,2.0,0.5,0.0,0.0, 0.0,0.5,3.0,0.5,0.0, 0.0,0.0,0.5,4.0,0.5, 0.0,0.0,0.0,0.5,5.0}; static float binit[NP][NP]={ 1.0,1.0,0.0,0.0,0.0, 1.0,2.0,1.0,0.0,0.0, 0.0,1.0,3.0,1.0,0.0, 0.0,0.0,1.0,4.0,1.0, 0.0,0.0,0.0,1.0,5.0}; ija=lvector(1,NMAX); ijb=lvector(1,NMAX); ijbt=lvector(1,NMAX); ijc=lvector(1,NMAX); sa=vector(1,NMAX); sb=vector(1,NMAX); sbt=vector(1,NMAX); sc=vector(1,NMAX); c=matrix(1,NP,1,NP); ab=matrix(1,NP,1,NP); a=convert_matrix(&ainit[0][0],1,NP,1,NP); b=convert_matrix(&binit[0][0],1,NP,1,NP); sprsin(a,NP,0.5,NMAX,sa,ija); sprsin(b,NP,0.5,NMAX,sb,ijb); sprstp(sb,ijb,sbt,ijbt); /* specify tridiagonal output, using fact that a is tridiagonal */ for (i=1;i<=ija[ija[1]-1]-1;i++) ijc[i]=ija[i]; sprspm(sa,ija,sbt,ijbt,sc,ijc); for (i=1;i<=NP;i++) { for (j=1;j<=NP;j++) { ab[i][j]=0.0; for (k=1;k<=NP;k++) { ab[i][j]=ab[i][j]+a[i][k]*b[k][j]; } } } printf("Reference matrix:\n"); for (i=1;i<=NP;i++) { for (j=1;j<=NP;j++) printf("%5.2f\t",ab[i][j]); printf("\n"); } printf("sprspm matrix (should show only tridiagonals):\n"); for (i=1;i<=NP;i++) for (j=1;j<=NP;j++) c[i][j]=0.0; for (i=1;i<=NP;i++) { c[i][i]=sc[i]; for (j=ijc[i];j<=ijc[i+1]-1;j++) c[i][ijc[j]]=sc[j]; } for (i=1;i<=NP;i++) { for (j=1;j<=NP;j++) printf("%5.2f\t",c[i][j]); printf("\n"); } free_convert_matrix(b,1,NP,1,NP); free_convert_matrix(a,1,NP,1,NP); free_matrix(ab,1,NP,1,NP); free_matrix(c,1,NP,1,NP); free_vector(sc,1,NMAX); free_vector(sbt,1,NMAX); free_vector(sb,1,NMAX); free_vector(sa,1,NMAX); free_lvector(ijc,1,NMAX); free_lvector(ijbt,1,NMAX); free_lvector(ijb,1,NMAX); free_lvector(ija,1,NMAX); return 0; }
/* The encoder doesn't know anything about interlacing, the halve height * needs to be passed and the double rowstride. Which field gets encoded * is decided by what buffers are passed to mjpeg_encode_frame */ jpeg_enc_t *jpeg_enc_init(int w, int h, int y_psize, int y_rsize, int u_psize, int u_rsize, int v_psize, int v_rsize, int cu, int q, int b) { jpeg_enc_t *j; int i = 0; mp_msg(MSGT_VO, MSGL_V, "JPEnc init: %dx%d %d %d %d %d %d %d\n", w, h, y_psize, y_rsize, u_psize, u_rsize, v_psize, v_rsize); j = av_malloc(sizeof(jpeg_enc_t)); if (j == NULL) return NULL; j->s = av_malloc(sizeof(MpegEncContext)); memset(j->s,0x00,sizeof(MpegEncContext)); if (j->s == NULL) { av_free(j); return NULL; } /* info on how to access the pixels */ j->y_ps = y_psize; j->u_ps = u_psize; j->v_ps = v_psize; j->y_rs = y_rsize; j->u_rs = u_rsize; j->v_rs = v_rsize; j->s->width = w; j->s->height = h; j->s->qscale = q; j->s->out_format = FMT_MJPEG; j->s->intra_only = 1; j->s->encoding = 1; j->s->pict_type = I_TYPE; j->s->y_dc_scale = 8; j->s->c_dc_scale = 8; //FIXME j->s->mjpeg_write_tables = 1; j->s->mjpeg_vsample[0] = 1; j->s->mjpeg_vsample[1] = 1; j->s->mjpeg_vsample[2] = 1; j->s->mjpeg_hsample[0] = 2; j->s->mjpeg_hsample[1] = 1; j->s->mjpeg_hsample[2] = 1; j->cheap_upsample = cu; j->bw = b; /* if libavcodec is used by the decoder then we must not * initialize again, but if it is not initialized then we must * initialize it here. */ if (!avcodec_inited) { /* we need to initialize libavcodec */ avcodec_init(); avcodec_register_all(); avcodec_inited=1; } if (ff_mjpeg_encode_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* alloc bogus avctx to keep MPV_common_init from segfaulting */ j->s->avctx = calloc(sizeof(*j->s->avctx), 1); /* Set up to encode mjpeg */ j->s->avctx->codec_id = CODEC_ID_MJPEG; /* make MPV_common_init allocate important buffers, like s->block */ j->s->avctx->thread_count = 1; if (MPV_common_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* correct the value for sc->mb_height */ j->s->mb_height = j->s->height/8; j->s->mb_intra = 1; j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; for (i = 1; i < 64; i++) j->s->intra_matrix[i] = av_clip_uint8( (ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3); convert_matrix(j->s, j->s->q_intra_matrix, j->s->q_intra_matrix16, j->s->intra_matrix, j->s->intra_quant_bias, 8, 8); return j; }
/** * \brief initialize mjpeg encoder * * This routine is to set up the parameters and initialize the mjpeg encoder. * It does all the initializations needed of lower level routines. * The formats accepted by this encoder is YUV422P and YUV420 * * \param w width in pixels of the image to encode, must be a multiple of 16 * \param h height in pixels of the image to encode, must be a multiple of 8 * \param y_rsize size of each plane row Y component * \param y_rsize size of each plane row U component * \param v_rsize size of each plane row V component * \param cu "cheap upsample". Set to 0 for YUV422 format, 1 for YUV420 format * when set to 1, the encoder will assume that there is only half th * number of rows of chroma information, and every chroma row is * duplicated. * \param q quality parameter for the mjpeg encode. Between 1 and 20 where 1 * is best quality and 20 is the worst quality. * \param b monochrome flag. When set to 1, the mjpeg output is monochrome. * In that case, the colour information is omitted, and actually the * colour planes are not touched. * * \returns an appropriately set up jpeg_enc_t structure * * The actual plane buffer addreses are passed by jpeg_enc_frame(). * * The encoder doesn't know anything about interlacing, the halve height * needs to be passed and the double rowstride. Which field gets encoded * is decided by what buffers are passed to mjpeg_encode_frame() */ static jpeg_enc_t *jpeg_enc_init(int w, int h, int y_rsize, int u_rsize, int v_rsize, int cu, int q, int b) { jpeg_enc_t *j; int i = 0; VERBOSE("JPEG encoder init: %dx%d %d %d %d cu=%d q=%d bw=%d\n", w, h, y_rsize, u_rsize, v_rsize, cu, q, b); j = av_mallocz(sizeof(jpeg_enc_t)); if (j == NULL) return NULL; j->s = av_mallocz(sizeof(MpegEncContext)); if (j->s == NULL) { av_free(j); return NULL; } /* info on how to access the pixels */ j->y_rs = y_rsize; j->u_rs = u_rsize; j->v_rs = v_rsize; j->s->width = w; // image width and height j->s->height = h; j->s->qscale = q; // Encoding quality j->s->out_format = FMT_MJPEG; j->s->intra_only = 1; // Generate only intra pictures for jpeg j->s->encoding = 1; // Set mode to encode j->s->pict_type = AV_PICTURE_TYPE_I; j->s->y_dc_scale = 8; j->s->c_dc_scale = 8; /* * This sets up the MCU (Minimal Code Unit) number * of appearances of the various component * for the SOF0 table in the generated MJPEG. * The values are not used for anything else. * The current setup is simply YUV422, with two horizontal Y components * for every UV component. */ //FIXME j->s->mjpeg_write_tables = 1; // setup to write tables j->s->mjpeg_vsample[0] = 1; // 1 appearance of Y vertically j->s->mjpeg_vsample[1] = 1; // 1 appearance of U vertically j->s->mjpeg_vsample[2] = 1; // 1 appearance of V vertically j->s->mjpeg_hsample[0] = 2; // 2 appearances of Y horizontally j->s->mjpeg_hsample[1] = 1; // 1 appearance of U horizontally j->s->mjpeg_hsample[2] = 1; // 1 appearance of V horizontally j->cheap_upsample = cu; j->bw = b; init_avcodec(); // Build mjpeg huffman code tables, setting up j->s->mjpeg_ctx if (ff_mjpeg_encode_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* alloc bogus avctx to keep MPV_common_init from segfaulting */ j->s->avctx = avcodec_alloc_context(); if (j->s->avctx == NULL) { av_free(j->s); av_free(j); return NULL; } // Set some a minimum amount of default values that are needed // Indicates that we should generated normal MJPEG j->s->avctx->codec_id = AV_CODEC_ID_MJPEG; // Which DCT method to use. AUTO will select the fastest one j->s->avctx->dct_algo = FF_DCT_AUTO; j->s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x // indicate we 'decode' to jpeg 4:2:2 j->s->avctx->pix_fmt = PIX_FMT_YUVJ422P; j->s->avctx->thread_count = 1; /* make MPV_common_init allocate important buffers, like s->block * Also initializes dsputil */ if (ff_MPV_common_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* correct the value for sc->mb_height. MPV_common_init put other * values there */ j->s->mb_height = j->s->height/8; j->s->mb_intra = 1; // Init q matrix j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; for (i = 1; i < 64; i++) j->s->intra_matrix[i] = av_clip_uint8( (ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3); // precompute matrix convert_matrix(j->s, j->s->q_intra_matrix, j->s->q_intra_matrix16, j->s->intra_matrix, j->s->intra_quant_bias, 8, 8); /* Pick up the selection of the optimal get_pixels() routine * to use, which was done in MPV_common_init() */ get_pixels = j->s->dsp.get_pixels; return j; }