PageRenderTime 591ms CodeModel.GetById 570ms RepoModel.GetById 0ms app.codeStats 1ms

/H264Dec/source/DecTestBench.c

http://github.com/mbebenita/Broadway
C | 820 lines | 484 code | 118 blank | 218 comment | 111 complexity | f4340705957a17a27c73620fdcc428c5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "H264SwDecApi.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "SDL/SDL.h"
  21. /*------------------------------------------------------------------------------
  22. Module defines
  23. ------------------------------------------------------------------------------*/
  24. /* CHECK_MEMORY_USAGE prints and sums the memory allocated in calls to
  25. * H264SwDecMalloc() */
  26. /* #define CHECK_MEMORY_USAGE */
  27. /* _NO_OUT disables output file writing */
  28. /* #define _NO_OUT */
  29. /* Debug prints */
  30. #define DEBUG(argv) printf argv
  31. /* CVS tag name for identification */
  32. const char tagName[256] = "$Name: FIRST_ANDROID_COPYRIGHT $";
  33. void WriteOutput(char *filename, u8 *data, u32 picSize);
  34. u32 NextPacket(u8 **pStrm);
  35. u32 CropPicture(u8 *pOutImage, u8 *pInImage,
  36. u32 picWidth, u32 picHeight, CropParams *pCropParams);
  37. /* Global variables for stream handling */
  38. u8 *streamStop = NULL;
  39. u32 packetize = 0;
  40. u32 nalUnitStream = 0;
  41. FILE *foutput = NULL;
  42. #define RENDER 1
  43. void DrawOutput(u8 *data, u32 picWidth, u32 picHeight);
  44. SDL_Surface* screen = NULL;
  45. #ifdef SOC_DESIGNER
  46. // Initialisation function defined in InitCache.s
  47. extern void cache_init(void);
  48. /*------------------------------------------------------------------------------
  49. Function name: $Sub$$main
  50. Purpose:
  51. This function is called at the end of the C library initialisation and
  52. before main. Its purpose is to do any further initialisation before the
  53. application start.
  54. ------------------------------------------------------------------------------*/
  55. int $Sub$$main(char argc, char * argv[])
  56. {
  57. cache_init(); // does some extra setup work setting up caches
  58. return $Super$$main(argc, argv); // calls the original function
  59. }
  60. #endif
  61. /*------------------------------------------------------------------------------
  62. Function name: main
  63. Purpose:
  64. main function of decoder testbench. Provides command line interface
  65. with file I/O for H.264 decoder. Prints out the usage information
  66. when executed without arguments.
  67. ------------------------------------------------------------------------------*/
  68. #ifdef LINUX
  69. int main(int argc, char **argv) {
  70. #else
  71. int SDL_main(int argc, char **argv) {
  72. #endif
  73. u32 i, tmp;
  74. u32 maxNumPics = 0;
  75. u8 *byteStrmStart;
  76. u8 *imageData;
  77. u8 *tmpImage = NULL;
  78. u32 strmLen;
  79. u32 picSize;
  80. H264SwDecInst decInst;
  81. H264SwDecRet ret;
  82. H264SwDecInput decInput;
  83. H264SwDecOutput decOutput;
  84. H264SwDecPicture decPicture;
  85. H264SwDecInfo decInfo;
  86. H264SwDecApiVersion decVer;
  87. u32 picDecodeNumber;
  88. u32 picDisplayNumber;
  89. u32 numErrors = 0;
  90. u32 cropDisplay = 0;
  91. u32 disableOutputReordering = 0;
  92. FILE *finput;
  93. char outFileName[256] = "";
  94. /* Print API version number */
  95. decVer = H264SwDecGetAPIVersion();
  96. DEBUG(("H.264 Decoder API v%d.%d\n", decVer.major, decVer.minor));
  97. /* Print tag name if '-T' argument present */
  98. if ( argc > 1 && strcmp(argv[1], "-T") == 0 )
  99. {
  100. DEBUG(("%s\n", tagName));
  101. return 0;
  102. }
  103. /* Check that enough command line arguments given, if not -> print usage
  104. * information out */
  105. if (argc < 2)
  106. {
  107. DEBUG((
  108. "Usage: %s [-Nn] [-Ooutfile] [-P] [-U] [-C] [-R] [-T] file.h264\n",
  109. argv[0]));
  110. DEBUG(("\t-Nn forces decoding to stop after n pictures\n"));
  111. #if defined(_NO_OUT)
  112. DEBUG(("\t-Ooutfile output writing disabled at compile time\n"));
  113. #else
  114. DEBUG(("\t-Ooutfile write output to \"outfile\" (default out_wxxxhyyy.yuv)\n"));
  115. DEBUG(("\t-Onone does not write output\n"));
  116. #endif
  117. DEBUG(("\t-P packet-by-packet mode\n"));
  118. DEBUG(("\t-U NAL unit stream mode\n"));
  119. DEBUG(("\t-C display cropped image (default decoded image)\n"));
  120. DEBUG(("\t-R disable DPB output reordering\n"));
  121. DEBUG(("\t-T to print tag name and exit\n"));
  122. return 0;
  123. }
  124. /* read command line arguments */
  125. for (i = 1; i < (u32)(argc-1); i++)
  126. {
  127. if ( strncmp(argv[i], "-N", 2) == 0 )
  128. {
  129. maxNumPics = (u32)atoi(argv[i]+2);
  130. }
  131. else if ( strncmp(argv[i], "-O", 2) == 0 )
  132. {
  133. strcpy(outFileName, argv[i]+2);
  134. }
  135. else if ( strcmp(argv[i], "-P") == 0 )
  136. {
  137. packetize = 1;
  138. }
  139. else if ( strcmp(argv[i], "-U") == 0 )
  140. {
  141. nalUnitStream = 1;
  142. }
  143. else if ( strcmp(argv[i], "-C") == 0 )
  144. {
  145. cropDisplay = 1;
  146. }
  147. else if ( strcmp(argv[i], "-R") == 0 )
  148. {
  149. disableOutputReordering = 1;
  150. }
  151. }
  152. /* open input file for reading, file name given by user. If file open
  153. * fails -> exit */
  154. finput = fopen(argv[argc-1],"rb");
  155. if (finput == NULL)
  156. {
  157. DEBUG(("UNABLE TO OPEN INPUT FILE\n"));
  158. return -1;
  159. }
  160. /* check size of the input file -> length of the stream in bytes */
  161. fseek(finput,0L,SEEK_END);
  162. strmLen = (u32)ftell(finput);
  163. rewind(finput);
  164. /* allocate memory for stream buffer. if unsuccessful -> exit */
  165. byteStrmStart = (u8 *)malloc(sizeof(u8)*strmLen);
  166. if (byteStrmStart == NULL)
  167. {
  168. DEBUG(("UNABLE TO ALLOCATE MEMORY\n"));
  169. return -1;
  170. }
  171. /* read input stream from file to buffer and close input file */
  172. fread(byteStrmStart, sizeof(u8), strmLen, finput);
  173. fclose(finput);
  174. /* initialize decoder. If unsuccessful -> exit */
  175. ret = H264SwDecInit(&decInst, disableOutputReordering);
  176. if (ret != H264SWDEC_OK)
  177. {
  178. DEBUG(("DECODER INITIALIZATION FAILED\n"));
  179. free(byteStrmStart);
  180. return -1;
  181. }
  182. #if RENDER
  183. SDL_Init(SDL_INIT_VIDEO);
  184. #endif
  185. /* initialize H264SwDecDecode() input structure */
  186. streamStop = byteStrmStart + strmLen;
  187. decInput.pStream = byteStrmStart;
  188. decInput.dataLen = strmLen;
  189. decInput.intraConcealmentMethod = 0;
  190. /* get pointer to next packet and the size of packet
  191. * (for packetize or nalUnitStream modes) */
  192. if ( (tmp = NextPacket(&decInput.pStream)) != 0 )
  193. decInput.dataLen = tmp;
  194. picDecodeNumber = picDisplayNumber = 1;
  195. /* main decoding loop */
  196. do
  197. {
  198. /* Picture ID is the picture number in decoding order */
  199. decInput.picId = picDecodeNumber;
  200. /* call API function to perform decoding */
  201. ret = H264SwDecDecode(decInst, &decInput, &decOutput);
  202. switch(ret)
  203. {
  204. case H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY:
  205. /* Stream headers were successfully decoded
  206. * -> stream information is available for query now */
  207. ret = H264SwDecGetInfo(decInst, &decInfo);
  208. if (ret != H264SWDEC_OK)
  209. return -1;
  210. DEBUG(("Profile %d\n", decInfo.profile));
  211. DEBUG(("Width %d Height %d\n",
  212. decInfo.picWidth, decInfo.picHeight));
  213. #if RENDER
  214. if (!screen) {
  215. screen = SDL_SetVideoMode(decInfo.picWidth, decInfo.picHeight, 32, SDL_HWSURFACE | SDL_RESIZABLE);
  216. }
  217. SDL_LockSurface(screen);
  218. #endif
  219. if (cropDisplay && decInfo.croppingFlag)
  220. {
  221. DEBUG(("Cropping params: (%d, %d) %dx%d\n",
  222. decInfo.cropParams.cropLeftOffset,
  223. decInfo.cropParams.cropTopOffset,
  224. decInfo.cropParams.cropOutWidth,
  225. decInfo.cropParams.cropOutHeight));
  226. /* Cropped frame size in planar YUV 4:2:0 */
  227. picSize = decInfo.cropParams.cropOutWidth *
  228. decInfo.cropParams.cropOutHeight;
  229. picSize = (3 * picSize)/2;
  230. tmpImage = malloc(picSize);
  231. if (tmpImage == NULL)
  232. return -1;
  233. }
  234. else
  235. {
  236. /* Decoder output frame size in planar YUV 4:2:0 */
  237. picSize = decInfo.picWidth * decInfo.picHeight;
  238. picSize = (3 * picSize)/2;
  239. }
  240. DEBUG(("videoRange %d, matrixCoefficients %d\n",
  241. decInfo.videoRange, decInfo.matrixCoefficients));
  242. /* update H264SwDecDecode() input structure, number of bytes
  243. * "consumed" is computed as difference between the new stream
  244. * pointer and old stream pointer */
  245. decInput.dataLen -=
  246. (u32)(decOutput.pStrmCurrPos - decInput.pStream);
  247. decInput.pStream = decOutput.pStrmCurrPos;
  248. /* If -O option not used, generate default file name */
  249. if (outFileName[0] == 0)
  250. sprintf(outFileName, "out_w%dh%d.yuv",
  251. decInfo.picWidth, decInfo.picHeight);
  252. break;
  253. case H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY:
  254. /* Picture is ready and more data remains in input buffer
  255. * -> update H264SwDecDecode() input structure, number of bytes
  256. * "consumed" is computed as difference between the new stream
  257. * pointer and old stream pointer */
  258. decInput.dataLen -=
  259. (u32)(decOutput.pStrmCurrPos - decInput.pStream);
  260. decInput.pStream = decOutput.pStrmCurrPos;
  261. /* fall through */
  262. case H264SWDEC_PIC_RDY:
  263. /*lint -esym(644,tmpImage,picSize) variable initialized at
  264. * H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY case */
  265. if (ret == H264SWDEC_PIC_RDY)
  266. decInput.dataLen = NextPacket(&decInput.pStream);
  267. /* If enough pictures decoded -> force decoding to end
  268. * by setting that no more stream is available */
  269. if (maxNumPics && picDecodeNumber == maxNumPics)
  270. decInput.dataLen = 0;
  271. /* Increment decoding number for every decoded picture */
  272. picDecodeNumber++;
  273. /* use function H264SwDecNextPicture() to obtain next picture
  274. * in display order. Function is called until no more images
  275. * are ready for display */
  276. while ( H264SwDecNextPicture(decInst, &decPicture, 0) ==
  277. H264SWDEC_PIC_RDY )
  278. {
  279. DEBUG(("PIC %d, type %s", picDisplayNumber,
  280. decPicture.isIdrPicture ? "IDR" : "NON-IDR"));
  281. if (picDisplayNumber != decPicture.picId)
  282. DEBUG((", decoded pic %d", decPicture.picId));
  283. if (decPicture.nbrOfErrMBs)
  284. {
  285. DEBUG((", concealed %d\n", decPicture.nbrOfErrMBs));
  286. }
  287. else
  288. DEBUG(("\n"));
  289. fflush(stdout);
  290. numErrors += decPicture.nbrOfErrMBs;
  291. /* Increment display number for every displayed picture */
  292. picDisplayNumber++;
  293. /*lint -esym(644,decInfo) always initialized if pictures
  294. * available for display */
  295. /* Write output picture to file */
  296. imageData = (u8*)decPicture.pOutputPicture;
  297. if (cropDisplay && decInfo.croppingFlag)
  298. {
  299. tmp = CropPicture(tmpImage, imageData,
  300. decInfo.picWidth, decInfo.picHeight,
  301. &decInfo.cropParams);
  302. if (tmp)
  303. return -1;
  304. WriteOutput(outFileName, tmpImage, picSize);
  305. }
  306. else
  307. {
  308. WriteOutput(outFileName, imageData, picSize);
  309. DrawOutput(imageData, decInfo.picWidth, decInfo.picHeight);
  310. }
  311. }
  312. break;
  313. case H264SWDEC_STRM_PROCESSED:
  314. case H264SWDEC_STRM_ERR:
  315. /* Input stream was decoded but no picture is ready
  316. * -> Get more data */
  317. decInput.dataLen = NextPacket(&decInput.pStream);
  318. break;
  319. default:
  320. DEBUG(("FATAL ERROR\n"));
  321. return -1;
  322. }
  323. /* keep decoding until all data from input stream buffer consumed */
  324. } while (decInput.dataLen > 0);
  325. /* if output in display order is preferred, the decoder shall be forced
  326. * to output pictures remaining in decoded picture buffer. Use function
  327. * H264SwDecNextPicture() to obtain next picture in display order. Function
  328. * is called until no more images are ready for display. Second parameter
  329. * for the function is set to '1' to indicate that this is end of the
  330. * stream and all pictures shall be output */
  331. while (H264SwDecNextPicture(decInst, &decPicture, 1) == H264SWDEC_PIC_RDY)
  332. {
  333. DEBUG(("PIC %d, type %s", picDisplayNumber,
  334. decPicture.isIdrPicture ? "IDR" : "NON-IDR"));
  335. if (picDisplayNumber != decPicture.picId)
  336. DEBUG((", decoded pic %d", decPicture.picId));
  337. if (decPicture.nbrOfErrMBs)
  338. {
  339. DEBUG((", concealed %d\n", decPicture.nbrOfErrMBs));
  340. }
  341. else
  342. DEBUG(("\n"));
  343. fflush(stdout);
  344. numErrors += decPicture.nbrOfErrMBs;
  345. /* Increment display number for every displayed picture */
  346. picDisplayNumber++;
  347. /* Write output picture to file */
  348. imageData = (u8*)decPicture.pOutputPicture;
  349. if (cropDisplay && decInfo.croppingFlag)
  350. {
  351. tmp = CropPicture(tmpImage, imageData,
  352. decInfo.picWidth, decInfo.picHeight,
  353. &decInfo.cropParams);
  354. if (tmp)
  355. return -1;
  356. WriteOutput(outFileName, tmpImage, picSize);
  357. }
  358. else
  359. {
  360. WriteOutput(outFileName, imageData, picSize);
  361. }
  362. }
  363. #if RENDER
  364. SDL_Quit();
  365. #endif
  366. /* release decoder instance */
  367. H264SwDecRelease(decInst);
  368. if (foutput)
  369. fclose(foutput);
  370. /* free allocated buffers */
  371. free(byteStrmStart);
  372. free(tmpImage);
  373. DEBUG(("Output file: %s\n", outFileName));
  374. DEBUG(("DECODING DONE\n"));
  375. if (numErrors || picDecodeNumber == 1)
  376. {
  377. DEBUG(("ERRORS FOUND\n"));
  378. return 1;
  379. }
  380. return 0;
  381. }
  382. void paint(u8 *luma, u8 *cb, u8 *cr, int width, int height) {
  383. int chromaWidth = width >> 1;
  384. u32 *dst = (u32 *)screen->pixels;
  385. int x, y = 0;
  386. for (y = 0; y < height; y++) {
  387. int lineOffLuma = y * width;
  388. int lineOffChroma = (y >> 1) * chromaWidth;
  389. for (x = 0; x < width; x++) {
  390. int c = luma[lineOffLuma + x] - 16;
  391. int d = cb[lineOffChroma + (x >> 1)] - 128;
  392. int e = cr[lineOffChroma + (x >> 1)] - 128;
  393. int red = (298 * c + 409 * e + 128) >> 8;
  394. red = red < 0 ? 0 : (red > 255 ? 255 : red);
  395. int green = (298 * c - 100 * d - 208 * e + 128) >> 8;
  396. green = green < 0 ? 0 : (green > 255 ? 255 : green);
  397. int blue = (298 * c + 516 * d + 128) >> 8;
  398. blue = blue < 0 ? 0 : (blue > 255 ? 255 : blue);
  399. int alpha = 255;
  400. dst[lineOffLuma + x] = SDL_MapRGB(screen->format, red & 0xff, green & 0xff, blue & 0xff);
  401. }
  402. }
  403. }
  404. void DrawOutput(u8 *data, u32 picWidth, u32 picHeight) {
  405. u32 size = picWidth * picHeight;
  406. // paint(data, data + size, data + size + (size >> 2), picWidth, picHeight);
  407. }
  408. /*------------------------------------------------------------------------------
  409. Function name: WriteOutput
  410. Purpose:
  411. Write picture pointed by data to file. Size of the
  412. picture in pixels is indicated by picSize.
  413. ------------------------------------------------------------------------------*/
  414. void WriteOutput(char *filename, u8 *data, u32 picSize)
  415. {
  416. /* foutput is global file pointer */
  417. if (foutput == NULL)
  418. {
  419. /* open output file for writing, can be disabled with define.
  420. * If file open fails -> exit */
  421. if (strcmp(filename, "none") != 0)
  422. {
  423. #if !defined(_NO_OUT)
  424. foutput = fopen(filename, "wb");
  425. if (foutput == NULL)
  426. {
  427. DEBUG(("UNABLE TO OPEN OUTPUT FILE\n"));
  428. exit(100);
  429. }
  430. #endif
  431. }
  432. }
  433. if (foutput && data)
  434. fwrite(data, 1, picSize, foutput);
  435. #if RENDER
  436. SDL_UnlockSurface(screen);
  437. SDL_Flip(screen);
  438. #endif
  439. }
  440. /*------------------------------------------------------------------------------
  441. Function name: NextPacket
  442. Purpose:
  443. Get the pointer to start of next packet in input stream. Uses
  444. global variables 'packetize' and 'nalUnitStream' to determine the
  445. decoder input stream mode and 'streamStop' to determine the end
  446. of stream. There are three possible stream modes:
  447. default - the whole stream at once
  448. packetize - a single NAL-unit with start code prefix
  449. nalUnitStream - a single NAL-unit without start code prefix
  450. pStrm stores pointer to the start of previous decoder input and is
  451. replaced with pointer to the start of the next decoder input.
  452. Returns the packet size in bytes
  453. ------------------------------------------------------------------------------*/
  454. u32 NextPacket(u8 **pStrm)
  455. {
  456. u32 index;
  457. u32 maxIndex;
  458. u32 zeroCount;
  459. u8 *stream;
  460. u8 byte;
  461. static u32 prevIndex=0;
  462. /* For default stream mode all the stream is in first packet */
  463. if (!packetize && !nalUnitStream)
  464. return 0;
  465. index = 0;
  466. stream = *pStrm + prevIndex;
  467. maxIndex = (u32)(streamStop - stream);
  468. if (maxIndex == 0)
  469. return(0);
  470. /* leading zeros of first NAL unit */
  471. do
  472. {
  473. byte = stream[index++];
  474. } while (byte != 1 && index < maxIndex);
  475. /* invalid start code prefix */
  476. if (index == maxIndex || index < 3)
  477. {
  478. DEBUG(("INVALID BYTE STREAM\n"));
  479. exit(100);
  480. }
  481. /* nalUnitStream is without start code prefix */
  482. if (nalUnitStream)
  483. {
  484. stream += index;
  485. maxIndex -= index;
  486. index = 0;
  487. }
  488. zeroCount = 0;
  489. /* Search stream for next start code prefix */
  490. /*lint -e(716) while(1) used consciously */
  491. while (1)
  492. {
  493. byte = stream[index++];
  494. if (!byte)
  495. zeroCount++;
  496. if ( (byte == 0x01) && (zeroCount >= 2) )
  497. {
  498. /* Start code prefix has two zeros
  499. * Third zero is assumed to be leading zero of next packet
  500. * Fourth and more zeros are assumed to be trailing zeros of this
  501. * packet */
  502. if (zeroCount > 3)
  503. {
  504. index -= 4;
  505. zeroCount -= 3;
  506. }
  507. else
  508. {
  509. index -= zeroCount+1;
  510. zeroCount = 0;
  511. }
  512. break;
  513. }
  514. else if (byte)
  515. zeroCount = 0;
  516. if (index == maxIndex)
  517. {
  518. break;
  519. }
  520. }
  521. /* Store pointer to the beginning of the packet */
  522. *pStrm = stream;
  523. prevIndex = index;
  524. /* nalUnitStream is without trailing zeros */
  525. if (nalUnitStream)
  526. index -= zeroCount;
  527. return(index);
  528. }
  529. /*------------------------------------------------------------------------------
  530. Function name: CropPicture
  531. Purpose:
  532. Perform cropping for picture. Input picture pInImage with dimensions
  533. picWidth x picHeight is cropped with pCropParams and the resulting
  534. picture is stored in pOutImage.
  535. ------------------------------------------------------------------------------*/
  536. u32 CropPicture(u8 *pOutImage, u8 *pInImage,
  537. u32 picWidth, u32 picHeight, CropParams *pCropParams)
  538. {
  539. u32 i, j;
  540. u32 outWidth, outHeight;
  541. u8 *pOut, *pIn;
  542. if (pOutImage == NULL || pInImage == NULL || pCropParams == NULL ||
  543. !picWidth || !picHeight)
  544. {
  545. /* just to prevent lint warning, returning non-zero will result in
  546. * return without freeing the memory */
  547. free(pOutImage);
  548. return(1);
  549. }
  550. if ( ((pCropParams->cropLeftOffset + pCropParams->cropOutWidth) >
  551. picWidth ) ||
  552. ((pCropParams->cropTopOffset + pCropParams->cropOutHeight) >
  553. picHeight ) )
  554. {
  555. /* just to prevent lint warning, returning non-zero will result in
  556. * return without freeing the memory */
  557. free(pOutImage);
  558. return(1);
  559. }
  560. outWidth = pCropParams->cropOutWidth;
  561. outHeight = pCropParams->cropOutHeight;
  562. /* Calculate starting pointer for luma */
  563. pIn = pInImage + pCropParams->cropTopOffset*picWidth +
  564. pCropParams->cropLeftOffset;
  565. pOut = pOutImage;
  566. /* Copy luma pixel values */
  567. for (i = outHeight; i; i--)
  568. {
  569. for (j = outWidth; j; j--)
  570. {
  571. *pOut++ = *pIn++;
  572. }
  573. pIn += picWidth - outWidth;
  574. }
  575. outWidth >>= 1;
  576. outHeight >>= 1;
  577. /* Calculate starting pointer for cb */
  578. pIn = pInImage + picWidth*picHeight +
  579. pCropParams->cropTopOffset*picWidth/4 + pCropParams->cropLeftOffset/2;
  580. /* Copy cb pixel values */
  581. for (i = outHeight; i; i--)
  582. {
  583. for (j = outWidth; j; j--)
  584. {
  585. *pOut++ = *pIn++;
  586. }
  587. pIn += picWidth/2 - outWidth;
  588. }
  589. /* Calculate starting pointer for cr */
  590. pIn = pInImage + 5*picWidth*picHeight/4 +
  591. pCropParams->cropTopOffset*picWidth/4 + pCropParams->cropLeftOffset/2;
  592. /* Copy cr pixel values */
  593. for (i = outHeight; i; i--)
  594. {
  595. for (j = outWidth; j; j--)
  596. {
  597. *pOut++ = *pIn++;
  598. }
  599. pIn += picWidth/2 - outWidth;
  600. }
  601. return (0);
  602. }
  603. /*------------------------------------------------------------------------------
  604. Function name: H264SwDecTrace
  605. Purpose:
  606. Example implementation of H264SwDecTrace function. Prototype of this
  607. function is given in H264SwDecApi.h. This implementation appends
  608. trace messages to file named 'dec_api.trc'.
  609. ------------------------------------------------------------------------------*/
  610. void H264SwDecTrace(char *string)
  611. {
  612. FILE *fp;
  613. fp = fopen("dec_api.trc", "at");
  614. if (!fp)
  615. return;
  616. fwrite(string, 1, strlen(string), fp);
  617. fwrite("\n", 1,1, fp);
  618. fclose(fp);
  619. }
  620. /*------------------------------------------------------------------------------
  621. Function name: H264SwDecMalloc
  622. Purpose:
  623. Example implementation of H264SwDecMalloc function. Prototype of this
  624. function is given in H264SwDecApi.h. This implementation uses
  625. library function malloc for allocation of memory.
  626. ------------------------------------------------------------------------------*/
  627. void* H264SwDecMalloc(u32 size)
  628. {
  629. #if defined(CHECK_MEMORY_USAGE)
  630. /* Note that if the decoder has to free and reallocate some of the buffers
  631. * the total value will be invalid */
  632. static u32 numBytes = 0;
  633. numBytes += size;
  634. DEBUG(("Allocated %d bytes, total %d\n", size, numBytes));
  635. #endif
  636. return malloc(size);
  637. }
  638. /*------------------------------------------------------------------------------
  639. Function name: H264SwDecFree
  640. Purpose:
  641. Example implementation of H264SwDecFree function. Prototype of this
  642. function is given in H264SwDecApi.h. This implementation uses
  643. library function free for freeing of memory.
  644. ------------------------------------------------------------------------------*/
  645. void H264SwDecFree(void *ptr)
  646. {
  647. free(ptr);
  648. }
  649. /*------------------------------------------------------------------------------
  650. Function name: H264SwDecMemcpy
  651. Purpose:
  652. Example implementation of H264SwDecMemcpy function. Prototype of this
  653. function is given in H264SwDecApi.h. This implementation uses
  654. library function memcpy to copy src to dest.
  655. ------------------------------------------------------------------------------*/
  656. void H264SwDecMemcpy(void *dest, void *src, u32 count)
  657. {
  658. memcpy(dest, src, count);
  659. }
  660. /*------------------------------------------------------------------------------
  661. Function name: H264SwDecMemset
  662. Purpose:
  663. Example implementation of H264SwDecMemset function. Prototype of this
  664. function is given in H264SwDecApi.h. This implementation uses
  665. library function memset to set content of memory area pointed by ptr.
  666. ------------------------------------------------------------------------------*/
  667. void H264SwDecMemset(void *ptr, i32 value, u32 count)
  668. {
  669. memset(ptr, value, count);
  670. }