tjbench.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * Copyright (C)2009-2016 D. R. Commander. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * - Neither the name of the libjpeg-turbo Project nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <math.h>
  33. #include <errno.h>
  34. #include <cdjpeg.h>
  35. #include "./bmp.h"
  36. #include "./tjutil.h"
  37. #include "./turbojpeg.h"
  38. #define _throw(op, err) { \
  39. printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
  40. retval=-1; goto bailout;}
  41. #define _throwunix(m) _throw(m, strerror(errno))
  42. #define _throwtj(m) _throw(m, tjGetErrorStr())
  43. #define _throwbmp(m) _throw(m, bmpgeterr())
  44. int flags=TJFLAG_NOREALLOC, componly=0, decomponly=0, doyuv=0, quiet=0,
  45. dotile=0, pf=TJPF_BGR, yuvpad=1, warmup=1, dowrite=1;
  46. char *ext="ppm";
  47. const char *pixFormatStr[TJ_NUMPF]=
  48. {
  49. "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
  50. };
  51. const char *subNameLong[TJ_NUMSAMP]=
  52. {
  53. "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
  54. };
  55. const char *csName[TJ_NUMCS]=
  56. {
  57. "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
  58. };
  59. const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"};
  60. tjscalingfactor *scalingfactors=NULL, sf={1, 1}; int nsf=0;
  61. int xformop=TJXOP_NONE, xformopt=0;
  62. int (*customFilter)(short *, tjregion, tjregion, int, int, tjtransform *);
  63. double benchtime=5.0;
  64. char *formatName(int subsamp, int cs, char *buf)
  65. {
  66. if(cs==TJCS_YCbCr) return (char *)subNameLong[subsamp];
  67. else if(cs==TJCS_YCCK)
  68. {
  69. snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
  70. return buf;
  71. }
  72. else return (char *)csName[cs];
  73. }
  74. char *sigfig(double val, int figs, char *buf, int len)
  75. {
  76. char format[80];
  77. int digitsafterdecimal=figs-(int)ceil(log10(fabs(val)));
  78. if(digitsafterdecimal<1) snprintf(format, 80, "%%.0f");
  79. else snprintf(format, 80, "%%.%df", digitsafterdecimal);
  80. snprintf(buf, len, format, val);
  81. return buf;
  82. }
  83. /* Custom DCT filter which produces a negative of the image */
  84. int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion,
  85. int componentIndex, int transformIndex, tjtransform *transform)
  86. {
  87. int i;
  88. for(i=0; i<arrayRegion.w*arrayRegion.h; i++) coeffs[i]=-coeffs[i];
  89. return 0;
  90. }
  91. /* Decompression test */
  92. int decomp(unsigned char *srcbuf, unsigned char **jpegbuf,
  93. unsigned long *jpegsize, unsigned char *dstbuf, int w, int h,
  94. int subsamp, int jpegqual, char *filename, int tilew, int tileh)
  95. {
  96. char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr;
  97. FILE *file=NULL; tjhandle handle=NULL;
  98. int row, col, iter=0, dstbufalloc=0, retval=0;
  99. double elapsed, elapsedDecode;
  100. int ps=tjPixelSize[pf];
  101. int scaledw=TJSCALED(w, sf);
  102. int scaledh=TJSCALED(h, sf);
  103. int pitch=scaledw*ps;
  104. int ntilesw=(w+tilew-1)/tilew, ntilesh=(h+tileh-1)/tileh;
  105. unsigned char *dstptr, *dstptr2, *yuvbuf=NULL;
  106. if(jpegqual>0)
  107. {
  108. snprintf(qualstr, 6, "_Q%d", jpegqual);
  109. qualstr[5]=0;
  110. }
  111. if((handle=tjInitDecompress())==NULL)
  112. _throwtj("executing tjInitDecompress()");
  113. if(dstbuf==NULL)
  114. {
  115. if((dstbuf=(unsigned char *)malloc(pitch*scaledh))==NULL)
  116. _throwunix("allocating destination buffer");
  117. dstbufalloc=1;
  118. }
  119. /* Set the destination buffer to gray so we know whether the decompressor
  120. attempted to write to it */
  121. memset(dstbuf, 127, pitch*scaledh);
  122. if(doyuv)
  123. {
  124. int width=dotile? tilew:scaledw;
  125. int height=dotile? tileh:scaledh;
  126. int yuvsize=tjBufSizeYUV2(width, yuvpad, height, subsamp);
  127. if((yuvbuf=(unsigned char *)malloc(yuvsize))==NULL)
  128. _throwunix("allocating YUV buffer");
  129. memset(yuvbuf, 127, yuvsize);
  130. }
  131. /* Benchmark */
  132. iter=-warmup;
  133. elapsed=elapsedDecode=0.;
  134. while(1)
  135. {
  136. int tile=0;
  137. double start=gettime();
  138. for(row=0, dstptr=dstbuf; row<ntilesh; row++, dstptr+=pitch*tileh)
  139. {
  140. for(col=0, dstptr2=dstptr; col<ntilesw; col++, tile++, dstptr2+=ps*tilew)
  141. {
  142. int width=dotile? min(tilew, w-col*tilew):scaledw;
  143. int height=dotile? min(tileh, h-row*tileh):scaledh;
  144. if(doyuv)
  145. {
  146. double startDecode;
  147. if(tjDecompressToYUV2(handle, jpegbuf[tile], jpegsize[tile], yuvbuf,
  148. width, yuvpad, height, flags)==-1)
  149. _throwtj("executing tjDecompressToYUV2()");
  150. startDecode=gettime();
  151. if(tjDecodeYUV(handle, yuvbuf, yuvpad, subsamp, dstptr2, width,
  152. pitch, height, pf, flags)==-1)
  153. _throwtj("executing tjDecodeYUV()");
  154. if(iter>=0) elapsedDecode+=gettime()-startDecode;
  155. }
  156. else
  157. if(tjDecompress2(handle, jpegbuf[tile], jpegsize[tile], dstptr2,
  158. width, pitch, height, pf, flags)==-1)
  159. _throwtj("executing tjDecompress2()");
  160. }
  161. }
  162. iter++;
  163. if(iter>=1)
  164. {
  165. elapsed+=gettime()-start;
  166. if(elapsed>=benchtime) break;
  167. }
  168. }
  169. if(doyuv) elapsed-=elapsedDecode;
  170. if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
  171. handle=NULL;
  172. if(quiet)
  173. {
  174. printf("%-6s%s",
  175. sigfig((double)(w*h)/1000000.*(double)iter/elapsed, 4, tempstr, 1024),
  176. quiet==2? "\n":" ");
  177. if(doyuv)
  178. printf("%s\n",
  179. sigfig((double)(w*h)/1000000.*(double)iter/elapsedDecode, 4, tempstr,
  180. 1024));
  181. else if(quiet!=2) printf("\n");
  182. }
  183. else
  184. {
  185. printf("%s --> Frame rate: %f fps\n",
  186. doyuv? "Decomp to YUV":"Decompress ", (double)iter/elapsed);
  187. printf(" Throughput: %f Megapixels/sec\n",
  188. (double)(w*h)/1000000.*(double)iter/elapsed);
  189. if(doyuv)
  190. {
  191. printf("YUV Decode --> Frame rate:  %f fps\n",
  192. (double)iter/elapsedDecode);
  193. printf(" Throughput: %f Megapixels/sec\n",
  194. (double)(w*h)/1000000.*(double)iter/elapsedDecode);
  195. }
  196. }
  197. if (!dowrite) goto bailout;
  198. if(sf.num!=1 || sf.denom!=1)
  199. snprintf(sizestr, 20, "%d_%d", sf.num, sf.denom);
  200. else if(tilew!=w || tileh!=h)
  201. snprintf(sizestr, 20, "%dx%d", tilew, tileh);
  202. else snprintf(sizestr, 20, "full");
  203. if(decomponly)
  204. snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr, ext);
  205. else
  206. snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename, subName[subsamp],
  207. qualstr, sizestr, ext);
  208. if(savebmp(tempstr, dstbuf, scaledw, scaledh, pf,
  209. (flags&TJFLAG_BOTTOMUP)!=0)==-1)
  210. _throwbmp("saving bitmap");
  211. ptr=strrchr(tempstr, '.');
  212. snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", ext);
  213. if(srcbuf && sf.num==1 && sf.denom==1)
  214. {
  215. if(!quiet) printf("Compression error written to %s.\n", tempstr);
  216. if(subsamp==TJ_GRAYSCALE)
  217. {
  218. int index, index2;
  219. for(row=0, index=0; row<h; row++, index+=pitch)
  220. {
  221. for(col=0, index2=index; col<w; col++, index2+=ps)
  222. {
  223. int rindex=index2+tjRedOffset[pf];
  224. int gindex=index2+tjGreenOffset[pf];
  225. int bindex=index2+tjBlueOffset[pf];
  226. int y=(int)((double)srcbuf[rindex]*0.299
  227. + (double)srcbuf[gindex]*0.587
  228. + (double)srcbuf[bindex]*0.114 + 0.5);
  229. if(y>255) y=255;
  230. if(y<0) y=0;
  231. dstbuf[rindex]=abs(dstbuf[rindex]-y);
  232. dstbuf[gindex]=abs(dstbuf[gindex]-y);
  233. dstbuf[bindex]=abs(dstbuf[bindex]-y);
  234. }
  235. }
  236. }
  237. else
  238. {
  239. for(row=0; row<h; row++)
  240. for(col=0; col<w*ps; col++)
  241. dstbuf[pitch*row+col]
  242. =abs(dstbuf[pitch*row+col]-srcbuf[pitch*row+col]);
  243. }
  244. if(savebmp(tempstr, dstbuf, w, h, pf,
  245. (flags&TJFLAG_BOTTOMUP)!=0)==-1)
  246. _throwbmp("saving bitmap");
  247. }
  248. bailout:
  249. if(file) fclose(file);
  250. if(handle) tjDestroy(handle);
  251. if(dstbuf && dstbufalloc) free(dstbuf);
  252. if(yuvbuf) free(yuvbuf);
  253. return retval;
  254. }
  255. int fullTest(unsigned char *srcbuf, int w, int h, int subsamp, int jpegqual,
  256. char *filename)
  257. {
  258. char tempstr[1024], tempstr2[80];
  259. FILE *file=NULL; tjhandle handle=NULL;
  260. unsigned char **jpegbuf=NULL, *yuvbuf=NULL, *tmpbuf=NULL, *srcptr, *srcptr2;
  261. double start, elapsed, elapsedEncode;
  262. int totaljpegsize=0, row, col, i, tilew=w, tileh=h, retval=0;
  263. int iter, yuvsize=0;
  264. unsigned long *jpegsize=NULL;
  265. int ps=tjPixelSize[pf];
  266. int ntilesw=1, ntilesh=1, pitch=w*ps;
  267. const char *pfStr=pixFormatStr[pf];
  268. if((tmpbuf=(unsigned char *)malloc(pitch*h)) == NULL)
  269. _throwunix("allocating temporary image buffer");
  270. if(!quiet)
  271. printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
  272. (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp],
  273. jpegqual);
  274. for(tilew=dotile? 8:w, tileh=dotile? 8:h; ; tilew*=2, tileh*=2)
  275. {
  276. if(tilew>w) tilew=w;
  277. if(tileh>h) tileh=h;
  278. ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
  279. if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
  280. *ntilesw*ntilesh))==NULL)
  281. _throwunix("allocating JPEG tile array");
  282. memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
  283. if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
  284. *ntilesw*ntilesh))==NULL)
  285. _throwunix("allocating JPEG size array");
  286. memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
  287. if((flags&TJFLAG_NOREALLOC)!=0)
  288. for(i=0; i<ntilesw*ntilesh; i++)
  289. {
  290. if((jpegbuf[i]=(unsigned char *)tjAlloc(tjBufSize(tilew, tileh,
  291. subsamp)))==NULL)
  292. _throwunix("allocating JPEG tiles");
  293. }
  294. /* Compression test */
  295. if(quiet==1)
  296. printf("%-4s (%s) %-5s %-3d ", pfStr,
  297. (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp], jpegqual);
  298. for(i=0; i<h; i++)
  299. memcpy(&tmpbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
  300. if((handle=tjInitCompress())==NULL)
  301. _throwtj("executing tjInitCompress()");
  302. if(doyuv)
  303. {
  304. yuvsize=tjBufSizeYUV2(tilew, yuvpad, tileh, subsamp);
  305. if((yuvbuf=(unsigned char *)malloc(yuvsize))==NULL)
  306. _throwunix("allocating YUV buffer");
  307. memset(yuvbuf, 127, yuvsize);
  308. }
  309. /* Benchmark */
  310. iter=-warmup;
  311. elapsed=elapsedEncode=0.;
  312. while(1)
  313. {
  314. int tile=0;
  315. totaljpegsize=0;
  316. start=gettime();
  317. for(row=0, srcptr=srcbuf; row<ntilesh; row++, srcptr+=pitch*tileh)
  318. {
  319. for(col=0, srcptr2=srcptr; col<ntilesw; col++, tile++,
  320. srcptr2+=ps*tilew)
  321. {
  322. int width=min(tilew, w-col*tilew);
  323. int height=min(tileh, h-row*tileh);
  324. if(doyuv)
  325. {
  326. double startEncode=gettime();
  327. if(tjEncodeYUV3(handle, srcptr2, width, pitch, height, pf, yuvbuf,
  328. yuvpad, subsamp, flags)==-1)
  329. _throwtj("executing tjEncodeYUV3()");
  330. if(iter>=0) elapsedEncode+=gettime()-startEncode;
  331. if(tjCompressFromYUV(handle, yuvbuf, width, yuvpad, height,
  332. subsamp, &jpegbuf[tile], &jpegsize[tile], jpegqual, flags)==-1)
  333. _throwtj("executing tjCompressFromYUV()");
  334. }
  335. else
  336. {
  337. if(tjCompress2(handle, srcptr2, width, pitch, height, pf,
  338. &jpegbuf[tile], &jpegsize[tile], subsamp, jpegqual, flags)==-1)
  339. _throwtj("executing tjCompress2()");
  340. }
  341. totaljpegsize+=jpegsize[tile];
  342. }
  343. }
  344. iter++;
  345. if(iter>=1)
  346. {
  347. elapsed+=gettime()-start;
  348. if(elapsed>=benchtime) break;
  349. }
  350. }
  351. if(doyuv) elapsed-=elapsedEncode;
  352. if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
  353. handle=NULL;
  354. if(quiet==1) printf("%-5d %-5d ", tilew, tileh);
  355. if(quiet)
  356. {
  357. if(doyuv)
  358. printf("%-6s%s",
  359. sigfig((double)(w*h)/1000000.*(double)iter/elapsedEncode, 4, tempstr,
  360. 1024), quiet==2? "\n":" ");
  361. printf("%-6s%s",
  362. sigfig((double)(w*h)/1000000.*(double)iter/elapsed, 4, tempstr, 1024),
  363. quiet==2? "\n":" ");
  364. printf("%-6s%s",
  365. sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
  366. quiet==2? "\n":" ");
  367. }
  368. else
  369. {
  370. printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilew,
  371. tileh);
  372. if(doyuv)
  373. {
  374. printf("Encode YUV --> Frame rate: %f fps\n",
  375. (double)iter/elapsedEncode);
  376. printf(" Output image size: %d bytes\n", yuvsize);
  377. printf(" Compression ratio: %f:1\n",
  378. (double)(w*h*ps)/(double)yuvsize);
  379. printf(" Throughput: %f Megapixels/sec\n",
  380. (double)(w*h)/1000000.*(double)iter/elapsedEncode);
  381. printf(" Output bit stream: %f Megabits/sec\n",
  382. (double)yuvsize*8./1000000.*(double)iter/elapsedEncode);
  383. }
  384. printf("%s --> Frame rate: %f fps\n",
  385. doyuv? "Comp from YUV":"Compress ", (double)iter/elapsed);
  386. printf(" Output image size: %d bytes\n",
  387. totaljpegsize);
  388. printf(" Compression ratio: %f:1\n",
  389. (double)(w*h*ps)/(double)totaljpegsize);
  390. printf(" Throughput: %f Megapixels/sec\n",
  391. (double)(w*h)/1000000.*(double)iter/elapsed);
  392. printf(" Output bit stream: %f Megabits/sec\n",
  393. (double)totaljpegsize*8./1000000.*(double)iter/elapsed);
  394. }
  395. if(tilew==w && tileh==h && dowrite)
  396. {
  397. snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, subName[subsamp],
  398. jpegqual);
  399. if((file=fopen(tempstr, "wb"))==NULL)
  400. _throwunix("opening reference image");
  401. if(fwrite(jpegbuf[0], jpegsize[0], 1, file)!=1)
  402. _throwunix("writing reference image");
  403. fclose(file); file=NULL;
  404. if(!quiet) printf("Reference image written to %s\n", tempstr);
  405. }
  406. /* Decompression test */
  407. if(!componly)
  408. {
  409. if(decomp(srcbuf, jpegbuf, jpegsize, tmpbuf, w, h, subsamp, jpegqual,
  410. filename, tilew, tileh)==-1)
  411. goto bailout;
  412. }
  413. for(i=0; i<ntilesw*ntilesh; i++)
  414. {
  415. if(jpegbuf[i]) tjFree(jpegbuf[i]);
  416. jpegbuf[i]=NULL;
  417. }
  418. free(jpegbuf); jpegbuf=NULL;
  419. free(jpegsize); jpegsize=NULL;
  420. if(doyuv)
  421. {
  422. free(yuvbuf); yuvbuf=NULL;
  423. }
  424. if(tilew==w && tileh==h) break;
  425. }
  426. bailout:
  427. if(file) {fclose(file); file=NULL;}
  428. if(jpegbuf)
  429. {
  430. for(i=0; i<ntilesw*ntilesh; i++)
  431. {
  432. if(jpegbuf[i]) tjFree(jpegbuf[i]);
  433. jpegbuf[i]=NULL;
  434. }
  435. free(jpegbuf); jpegbuf=NULL;
  436. }
  437. if(yuvbuf) {free(yuvbuf); yuvbuf=NULL;}
  438. if(jpegsize) {free(jpegsize); jpegsize=NULL;}
  439. if(tmpbuf) {free(tmpbuf); tmpbuf=NULL;}
  440. if(handle) {tjDestroy(handle); handle=NULL;}
  441. return retval;
  442. }
  443. int decompTest(char *filename)
  444. {
  445. FILE *file=NULL; tjhandle handle=NULL;
  446. unsigned char **jpegbuf=NULL, *srcbuf=NULL;
  447. unsigned long *jpegsize=NULL, srcsize, totaljpegsize;
  448. tjtransform *t=NULL;
  449. int w=0, h=0, subsamp=-1, cs=-1, _w, _h, _tilew, _tileh,
  450. _ntilesw, _ntilesh, _subsamp;
  451. char *temp=NULL, tempstr[80], tempstr2[80];
  452. int row, col, i, iter, tilew, tileh, ntilesw=1, ntilesh=1, retval=0;
  453. double start, elapsed;
  454. int ps=tjPixelSize[pf], tile;
  455. if((file=fopen(filename, "rb"))==NULL)
  456. _throwunix("opening file");
  457. if(fseek(file, 0, SEEK_END)<0 || (srcsize=ftell(file))==(unsigned long)-1)
  458. _throwunix("determining file size");
  459. if((srcbuf=(unsigned char *)malloc(srcsize))==NULL)
  460. _throwunix("allocating memory");
  461. if(fseek(file, 0, SEEK_SET)<0)
  462. _throwunix("setting file position");
  463. if(fread(srcbuf, srcsize, 1, file)<1)
  464. _throwunix("reading JPEG data");
  465. fclose(file); file=NULL;
  466. temp=strrchr(filename, '.');
  467. if(temp!=NULL) *temp='\0';
  468. if((handle=tjInitTransform())==NULL)
  469. _throwtj("executing tjInitTransform()");
  470. if(tjDecompressHeader3(handle, srcbuf, srcsize, &w, &h, &subsamp, &cs)==-1)
  471. _throwtj("executing tjDecompressHeader3()");
  472. if(cs==TJCS_YCCK || cs==TJCS_CMYK)
  473. {
  474. pf=TJPF_CMYK; ps=tjPixelSize[pf];
  475. }
  476. if(quiet==1)
  477. {
  478. printf("All performance values in Mpixels/sec\n\n");
  479. printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
  480. dotile? "Tile ":"Image", dotile? "Tile ":"Image");
  481. if(doyuv) printf("Decode");
  482. printf("\n");
  483. printf("Format CS Subsamp Width Height Perf Ratio Perf ");
  484. if(doyuv) printf("Perf");
  485. printf("\n\n");
  486. }
  487. else if(!quiet)
  488. printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
  489. formatName(subsamp, cs, tempstr), pixFormatStr[pf],
  490. (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down");
  491. for(tilew=dotile? 16:w, tileh=dotile? 16:h; ; tilew*=2, tileh*=2)
  492. {
  493. if(tilew>w) tilew=w;
  494. if(tileh>h) tileh=h;
  495. ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
  496. if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
  497. *ntilesw*ntilesh))==NULL)
  498. _throwunix("allocating JPEG tile array");
  499. memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
  500. if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
  501. *ntilesw*ntilesh))==NULL)
  502. _throwunix("allocating JPEG size array");
  503. memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
  504. if((flags&TJFLAG_NOREALLOC)!=0 || !dotile)
  505. for(i=0; i<ntilesw*ntilesh; i++)
  506. {
  507. if((jpegbuf[i]=(unsigned char *)tjAlloc(tjBufSize(tilew, tileh,
  508. subsamp)))==NULL)
  509. _throwunix("allocating JPEG tiles");
  510. }
  511. _w=w; _h=h; _tilew=tilew; _tileh=tileh;
  512. if(!quiet)
  513. {
  514. printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilew,
  515. _tileh);
  516. if(sf.num!=1 || sf.denom!=1)
  517. printf(" --> %d x %d", TJSCALED(_w, sf), TJSCALED(_h, sf));
  518. printf("\n");
  519. }
  520. else if(quiet==1)
  521. {
  522. printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
  523. (flags&TJFLAG_BOTTOMUP)? "BU":"TD", csName[cs], subNameLong[subsamp]);
  524. printf("%-5d %-5d ", tilew, tileh);
  525. }
  526. _subsamp=subsamp;
  527. if(dotile || xformop!=TJXOP_NONE || xformopt!=0 || customFilter)
  528. {
  529. if((t=(tjtransform *)malloc(sizeof(tjtransform)*ntilesw*ntilesh))
  530. ==NULL)
  531. _throwunix("allocating image transform array");
  532. if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
  533. || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
  534. {
  535. _w=h; _h=w; _tilew=tileh; _tileh=tilew;
  536. }
  537. if(xformopt&TJXOPT_GRAY) _subsamp=TJ_GRAYSCALE;
  538. if(xformop==TJXOP_HFLIP || xformop==TJXOP_ROT180)
  539. _w=_w-(_w%tjMCUWidth[_subsamp]);
  540. if(xformop==TJXOP_VFLIP || xformop==TJXOP_ROT180)
  541. _h=_h-(_h%tjMCUHeight[_subsamp]);
  542. if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT90)
  543. _w=_w-(_w%tjMCUHeight[_subsamp]);
  544. if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT270)
  545. _h=_h-(_h%tjMCUWidth[_subsamp]);
  546. _ntilesw=(_w+_tilew-1)/_tilew;
  547. _ntilesh=(_h+_tileh-1)/_tileh;
  548. if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
  549. || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
  550. {
  551. if(_subsamp==TJSAMP_422) _subsamp=TJSAMP_440;
  552. else if(_subsamp==TJSAMP_440) _subsamp=TJSAMP_422;
  553. }
  554. for(row=0, tile=0; row<_ntilesh; row++)
  555. {
  556. for(col=0; col<_ntilesw; col++, tile++)
  557. {
  558. t[tile].r.w=min(_tilew, _w-col*_tilew);
  559. t[tile].r.h=min(_tileh, _h-row*_tileh);
  560. t[tile].r.x=col*_tilew;
  561. t[tile].r.y=row*_tileh;
  562. t[tile].op=xformop;
  563. t[tile].options=xformopt|TJXOPT_TRIM;
  564. t[tile].customFilter=customFilter;
  565. if(t[tile].options&TJXOPT_NOOUTPUT && jpegbuf[tile])
  566. {
  567. tjFree(jpegbuf[tile]); jpegbuf[tile]=NULL;
  568. }
  569. }
  570. }
  571. iter=-warmup;
  572. elapsed=0.;
  573. while(1)
  574. {
  575. start=gettime();
  576. if(tjTransform(handle, srcbuf, srcsize, _ntilesw*_ntilesh, jpegbuf,
  577. jpegsize, t, flags)==-1)
  578. _throwtj("executing tjTransform()");
  579. iter++;
  580. if(iter>=1)
  581. {
  582. elapsed+=gettime()-start;
  583. if(elapsed>=benchtime) break;
  584. }
  585. }
  586. free(t); t=NULL;
  587. for(tile=0, totaljpegsize=0; tile<_ntilesw*_ntilesh; tile++)
  588. totaljpegsize+=jpegsize[tile];
  589. if(quiet)
  590. {
  591. printf("%-6s%s%-6s%s",
  592. sigfig((double)(w*h)/1000000./elapsed, 4, tempstr, 80),
  593. quiet==2? "\n":" ",
  594. sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
  595. quiet==2? "\n":" ");
  596. }
  597. else if(!quiet)
  598. {
  599. printf("Transform --> Frame rate: %f fps\n", 1.0/elapsed);
  600. printf(" Output image size: %lu bytes\n", totaljpegsize);
  601. printf(" Compression ratio: %f:1\n",
  602. (double)(w*h*ps)/(double)totaljpegsize);
  603. printf(" Throughput: %f Megapixels/sec\n",
  604. (double)(w*h)/1000000./elapsed);
  605. printf(" Output bit stream: %f Megabits/sec\n",
  606. (double)totaljpegsize*8./1000000./elapsed);
  607. }
  608. }
  609. else
  610. {
  611. if(quiet==1) printf("N/A N/A ");
  612. jpegsize[0]=srcsize;
  613. memcpy(jpegbuf[0], srcbuf, srcsize);
  614. }
  615. if(w==tilew) _tilew=_w;
  616. if(h==tileh) _tileh=_h;
  617. if(!(xformopt&TJXOPT_NOOUTPUT))
  618. {
  619. if(decomp(NULL, jpegbuf, jpegsize, NULL, _w, _h, _subsamp, 0,
  620. filename, _tilew, _tileh)==-1)
  621. goto bailout;
  622. }
  623. else if(quiet==1) printf("N/A\n");
  624. for(i=0; i<ntilesw*ntilesh; i++)
  625. {
  626. tjFree(jpegbuf[i]); jpegbuf[i]=NULL;
  627. }
  628. free(jpegbuf); jpegbuf=NULL;
  629. if(jpegsize) {free(jpegsize); jpegsize=NULL;}
  630. if(tilew==w && tileh==h) break;
  631. }
  632. bailout:
  633. if(file) {fclose(file); file=NULL;}
  634. if(jpegbuf)
  635. {
  636. for(i=0; i<ntilesw*ntilesh; i++)
  637. {
  638. if(jpegbuf[i]) tjFree(jpegbuf[i]);
  639. jpegbuf[i]=NULL;
  640. }
  641. free(jpegbuf); jpegbuf=NULL;
  642. }
  643. if(jpegsize) {free(jpegsize); jpegsize=NULL;}
  644. if(srcbuf) {free(srcbuf); srcbuf=NULL;}
  645. if(t) {free(t); t=NULL;}
  646. if(handle) {tjDestroy(handle); handle=NULL;}
  647. return retval;
  648. }
  649. void usage(char *progname)
  650. {
  651. int i;
  652. printf("USAGE: %s\n", progname);
  653. printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
  654. printf(" %s\n", progname);
  655. printf(" <Inputfile (JPG)> [options]\n\n");
  656. printf("Options:\n\n");
  657. printf("-alloc = Dynamically allocate JPEG image buffers\n");
  658. printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
  659. printf("-bottomup = Test bottom-up compression/decompression\n");
  660. printf("-tile = Test performance of the codec when the image is encoded as separate\n");
  661. printf(" tiles of varying sizes.\n");
  662. printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
  663. printf(" Test the specified color conversion path in the codec (default = BGR)\n");
  664. printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
  665. printf(" and destination bitmaps are still RGB. The conversion is done\n");
  666. printf(" internally prior to compression or after decompression.)\n");
  667. printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
  668. printf(" the underlying codec\n");
  669. printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
  670. printf(" codec\n");
  671. printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
  672. printf(" underlying codec\n");
  673. printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
  674. printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
  675. printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
  676. printf(" sequence.\n");
  677. printf("-quiet = Output results in tabular rather than verbose format\n");
  678. printf("-yuv = Test YUV encoding/decoding functions\n");
  679. printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
  680. printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
  681. printf(" padded (default = 1)\n");
  682. printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
  683. printf(" factor of M/N (M/N = ");
  684. for(i=0; i<nsf; i++)
  685. {
  686. printf("%d/%d", scalingfactors[i].num, scalingfactors[i].denom);
  687. if(nsf==2 && i!=nsf-1) printf(" or ");
  688. else if(nsf>2)
  689. {
  690. if(i!=nsf-1) printf(", ");
  691. if(i==nsf-2) printf("or ");
  692. }
  693. if(i%8==0 && i!=0) printf("\n ");
  694. }
  695. printf(")\n");
  696. printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
  697. printf(" Perform the corresponding lossless transform prior to\n");
  698. printf(" decompression (these options are mutually exclusive)\n");
  699. printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
  700. printf(" test (can be combined with the other transforms above)\n");
  701. printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
  702. printf("-warmup <w> = Execute each benchmark <w> times to prime the cache before\n");
  703. printf(" taking performance measurements (default = 1)\n");
  704. printf("-componly = Stop after running compression tests. Do not test decompression.\n");
  705. printf("-nowrite = Do not write reference or output images (improves consistency of\n");
  706. printf(" performance measurements.)\n\n");
  707. printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
  708. printf("test will be performed for all quality values in the range.\n\n");
  709. exit(1);
  710. }
  711. int main(int argc, char *argv[])
  712. {
  713. unsigned char *srcbuf=NULL; int w=0, h=0, i, j;
  714. int minqual=-1, maxqual=-1; char *temp;
  715. int minarg=2, retval=0, subsamp=-1;
  716. if((scalingfactors=tjGetScalingFactors(&nsf))==NULL || nsf==0)
  717. _throwtj("executing tjGetScalingFactors()");
  718. if(argc<minarg) usage(argv[0]);
  719. temp=strrchr(argv[1], '.');
  720. if(temp!=NULL)
  721. {
  722. if(!strcasecmp(temp, ".bmp")) ext="bmp";
  723. if(!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg")) decomponly=1;
  724. }
  725. printf("\n");
  726. if(!decomponly)
  727. {
  728. minarg=3;
  729. if(argc<minarg) usage(argv[0]);
  730. if((minqual=atoi(argv[2]))<1 || minqual>100)
  731. {
  732. puts("ERROR: Quality must be between 1 and 100.");
  733. exit(1);
  734. }
  735. if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
  736. && sscanf(&temp[1], "%d", &maxqual)==1 && maxqual>minqual && maxqual>=1
  737. && maxqual<=100) {}
  738. else maxqual=minqual;
  739. }
  740. if(argc>minarg)
  741. {
  742. for(i=minarg; i<argc; i++)
  743. {
  744. if(!strcasecmp(argv[i], "-tile"))
  745. {
  746. dotile=1; xformopt|=TJXOPT_CROP;
  747. }
  748. if(!strcasecmp(argv[i], "-fastupsample"))
  749. {
  750. printf("Using fast upsampling code\n\n");
  751. flags|=TJFLAG_FASTUPSAMPLE;
  752. }
  753. if(!strcasecmp(argv[i], "-fastdct"))
  754. {
  755. printf("Using fastest DCT/IDCT algorithm\n\n");
  756. flags|=TJFLAG_FASTDCT;
  757. }
  758. if(!strcasecmp(argv[i], "-accuratedct"))
  759. {
  760. printf("Using most accurate DCT/IDCT algorithm\n\n");
  761. flags|=TJFLAG_ACCURATEDCT;
  762. }
  763. if(!strcasecmp(argv[i], "-rgb")) pf=TJPF_RGB;
  764. if(!strcasecmp(argv[i], "-rgbx")) pf=TJPF_RGBX;
  765. if(!strcasecmp(argv[i], "-bgr")) pf=TJPF_BGR;
  766. if(!strcasecmp(argv[i], "-bgrx")) pf=TJPF_BGRX;
  767. if(!strcasecmp(argv[i], "-xbgr")) pf=TJPF_XBGR;
  768. if(!strcasecmp(argv[i], "-xrgb")) pf=TJPF_XRGB;
  769. if(!strcasecmp(argv[i], "-cmyk")) pf=TJPF_CMYK;
  770. if(!strcasecmp(argv[i], "-bottomup")) flags|=TJFLAG_BOTTOMUP;
  771. if(!strcasecmp(argv[i], "-quiet")) quiet=1;
  772. if(!strcasecmp(argv[i], "-qq")) quiet=2;
  773. if(!strcasecmp(argv[i], "-scale") && i<argc-1)
  774. {
  775. int temp1=0, temp2=0, match=0;
  776. if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2)
  777. {
  778. for(j=0; j<nsf; j++)
  779. {
  780. if((double)temp1/(double)temp2
  781. == (double)scalingfactors[j].num/(double)scalingfactors[j].denom)
  782. {
  783. sf=scalingfactors[j];
  784. match=1; break;
  785. }
  786. }
  787. if(!match) usage(argv[0]);
  788. }
  789. else usage(argv[0]);
  790. }
  791. if(!strcasecmp(argv[i], "-hflip")) xformop=TJXOP_HFLIP;
  792. if(!strcasecmp(argv[i], "-vflip")) xformop=TJXOP_VFLIP;
  793. if(!strcasecmp(argv[i], "-transpose")) xformop=TJXOP_TRANSPOSE;
  794. if(!strcasecmp(argv[i], "-transverse")) xformop=TJXOP_TRANSVERSE;
  795. if(!strcasecmp(argv[i], "-rot90")) xformop=TJXOP_ROT90;
  796. if(!strcasecmp(argv[i], "-rot180")) xformop=TJXOP_ROT180;
  797. if(!strcasecmp(argv[i], "-rot270")) xformop=TJXOP_ROT270;
  798. if(!strcasecmp(argv[i], "-grayscale")) xformopt|=TJXOPT_GRAY;
  799. if(!strcasecmp(argv[i], "-custom")) customFilter=dummyDCTFilter;
  800. if(!strcasecmp(argv[i], "-nooutput")) xformopt|=TJXOPT_NOOUTPUT;
  801. if(!strcasecmp(argv[i], "-benchtime") && i<argc-1)
  802. {
  803. double temp=atof(argv[++i]);
  804. if(temp>0.0) benchtime=temp;
  805. else usage(argv[0]);
  806. }
  807. if(!strcasecmp(argv[i], "-warmup") && i<argc-1)
  808. {
  809. int temp=atoi(argv[++i]);
  810. if(temp>=0)
  811. {
  812. warmup=temp;
  813. printf("Warmup runs = %d\n\n", warmup);
  814. }
  815. else usage(argv[0]);
  816. }
  817. if(!strcmp(argv[i], "-?")) usage(argv[0]);
  818. if(!strcasecmp(argv[i], "-alloc")) flags&=(~TJFLAG_NOREALLOC);
  819. if(!strcasecmp(argv[i], "-bmp")) ext="bmp";
  820. if(!strcasecmp(argv[i], "-yuv"))
  821. {
  822. printf("Testing YUV planar encoding/decoding\n\n");
  823. doyuv=1;
  824. }
  825. if(!strcasecmp(argv[i], "-yuvpad") && i<argc-1)
  826. {
  827. int temp=atoi(argv[++i]);
  828. if(temp>=1) yuvpad=temp;
  829. }
  830. if(!strcasecmp(argv[i], "-subsamp") && i<argc-1)
  831. {
  832. i++;
  833. if(toupper(argv[i][0])=='G') subsamp=TJSAMP_GRAY;
  834. else
  835. {
  836. int temp=atoi(argv[i]);
  837. switch(temp)
  838. {
  839. case 444: subsamp=TJSAMP_444; break;
  840. case 422: subsamp=TJSAMP_422; break;
  841. case 440: subsamp=TJSAMP_440; break;
  842. case 420: subsamp=TJSAMP_420; break;
  843. case 411: subsamp=TJSAMP_411; break;
  844. }
  845. }
  846. }
  847. if(!strcasecmp(argv[i], "-componly")) componly=1;
  848. if(!strcasecmp(argv[i], "-nowrite")) dowrite=0;
  849. }
  850. }
  851. if((sf.num!=1 || sf.denom!=1) && dotile)
  852. {
  853. printf("Disabling tiled compression/decompression tests, because those tests do not\n");
  854. printf("work when scaled decompression is enabled.\n");
  855. dotile=0;
  856. }
  857. if((flags&TJFLAG_NOREALLOC)==0 && dotile)
  858. {
  859. printf("Disabling tiled compression/decompression tests, because those tests do not\n");
  860. printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
  861. dotile=0;
  862. }
  863. if(!decomponly)
  864. {
  865. if(loadbmp(argv[1], &srcbuf, &w, &h, pf, (flags&TJFLAG_BOTTOMUP)!=0)==-1)
  866. _throwbmp("loading bitmap");
  867. temp=strrchr(argv[1], '.');
  868. if(temp!=NULL) *temp='\0';
  869. }
  870. if(quiet==1 && !decomponly)
  871. {
  872. printf("All performance values in Mpixels/sec\n\n");
  873. printf("Bitmap JPEG JPEG %s %s ",
  874. dotile? "Tile ":"Image", dotile? "Tile ":"Image");
  875. if(doyuv) printf("Encode ");
  876. printf("Comp Comp Decomp ");
  877. if(doyuv) printf("Decode");
  878. printf("\n");
  879. printf("Format Subsamp Qual Width Height ");
  880. if(doyuv) printf("Perf ");
  881. printf("Perf Ratio Perf ");
  882. if(doyuv) printf("Perf");
  883. printf("\n\n");
  884. }
  885. if(decomponly)
  886. {
  887. decompTest(argv[1]);
  888. printf("\n");
  889. goto bailout;
  890. }
  891. if(subsamp>=0 && subsamp<TJ_NUMSAMP)
  892. {
  893. for(i=maxqual; i>=minqual; i--)
  894. fullTest(srcbuf, w, h, subsamp, i, argv[1]);
  895. printf("\n");
  896. }
  897. else
  898. {
  899. if(pf!=TJPF_CMYK)
  900. {
  901. for(i=maxqual; i>=minqual; i--)
  902. fullTest(srcbuf, w, h, TJSAMP_GRAY, i, argv[1]);
  903. printf("\n");
  904. }
  905. for(i=maxqual; i>=minqual; i--)
  906. fullTest(srcbuf, w, h, TJSAMP_420, i, argv[1]);
  907. printf("\n");
  908. for(i=maxqual; i>=minqual; i--)
  909. fullTest(srcbuf, w, h, TJSAMP_422, i, argv[1]);
  910. printf("\n");
  911. for(i=maxqual; i>=minqual; i--)
  912. fullTest(srcbuf, w, h, TJSAMP_444, i, argv[1]);
  913. printf("\n");
  914. }
  915. bailout:
  916. if(srcbuf) free(srcbuf);
  917. return retval;
  918. }