PageRenderTime 87ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/H264Dec/source/Broadway.c

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