enctracestream.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Description : Encoder system model, stream tracing
  17. --
  18. ------------------------------------------------------------------------------*/
  19. #include <string.h>
  20. #include "enctracestream.h"
  21. /*------------------------------------------------------------------------------
  22. External compiler flags
  23. --------------------------------------------------------------------------------
  24. NO_2GB_LIMIT: Don't check 2GB limit when writing stream.trc
  25. --------------------------------------------------------------------------------
  26. Module defines
  27. ------------------------------------------------------------------------------*/
  28. /* Structure for storing stream trace information */
  29. traceStream_s traceStream;
  30. /*------------------------------------------------------------------------------
  31. Local function prototypes
  32. ------------------------------------------------------------------------------*/
  33. /*------------------------------------------------------------------------------
  34. EncOpenStreamTrace()
  35. ------------------------------------------------------------------------------*/
  36. i32 EncOpenStreamTrace(const char *filename)
  37. {
  38. FILE *file;
  39. u32 reopen = 0;
  40. if (traceStream.file) {
  41. reopen = 1;
  42. EncCloseStreamTrace();
  43. }
  44. traceStream.file = fopen(filename, "a");
  45. if (traceStream.file == NULL) {
  46. fprintf(stderr, "Unable to open <%s>.", filename);
  47. return -1;
  48. }
  49. file = traceStream.file;
  50. fprintf(file, " Vop\tByte\tID Data Len\tBitPattern\t\t\tDescription\n");
  51. fprintf(file, "-----------------------------------------------");
  52. fprintf(file, "-----------------------------------------\n");
  53. /* If reopening trace file don't reset the tracing */
  54. if (reopen)
  55. return 0;
  56. traceStream.frameNum = 0;
  57. traceStream.bitCnt = 0;
  58. traceStream.disableStreamTrace = 0;
  59. traceStream.id = 0;
  60. return 0;
  61. }
  62. /*------------------------------------------------------------------------------
  63. EncCloseStreamTrace()
  64. ------------------------------------------------------------------------------*/
  65. void EncCloseStreamTrace(void)
  66. {
  67. if (traceStream.file != NULL)
  68. fclose(traceStream.file);
  69. traceStream.file = NULL;
  70. }
  71. /*------------------------------------------------------------------------------
  72. EncTraceStream() Writes per symbol trace in ASCII format. Trace can be
  73. disable with traceStream.disableStreamTrace e.g. for unwanted headers.
  74. Id can be used to differentiate the source of the stream eg. SW=0 HW=1
  75. ------------------------------------------------------------------------------*/
  76. void EncTraceStream(i32 value, i32 numberOfBits)
  77. {
  78. static u32 lines = 0;
  79. FILE *file = traceStream.file;
  80. i32 i, j = 0, k = 0;
  81. #ifndef NO_2GB_LIMIT
  82. /* Check for file size limit 2GB, approx 50 bytes/line */
  83. lines++;
  84. if (file && (lines*50 >= (1U<<31))) {
  85. fclose(file); file = traceStream.file = NULL;
  86. }
  87. #endif
  88. if (file != NULL && !traceStream.disableStreamTrace) {
  89. fprintf(file,"%4i\t%4i\t%2i %6i %2i\t",
  90. traceStream.frameNum, traceStream.bitCnt/8,
  91. traceStream.id, value, numberOfBits);
  92. if (numberOfBits < 0)
  93. return;
  94. for (i = numberOfBits; i; i--) {
  95. if (value & (1 << (i - 1))) {
  96. fprintf(file, "1");
  97. } else {
  98. fprintf(file, "0");
  99. }
  100. j++;
  101. if (j == 4) {
  102. fprintf(file, " ");
  103. j = 0;
  104. k++;
  105. }
  106. }
  107. for (i = numberOfBits + k; i < 32; i += 8) {
  108. fprintf(file, "\t");
  109. }
  110. }
  111. traceStream.bitCnt += numberOfBits;
  112. }
  113. /*------------------------------------------------------------------------------
  114. EncCommentMbType()
  115. ------------------------------------------------------------------------------*/
  116. void EncCommentMbType(const char *comment, i32 mbNum)
  117. {
  118. FILE *file = traceStream.file;
  119. if (file == NULL || traceStream.disableStreamTrace) {
  120. return;
  121. }
  122. fprintf(file, "mb_type %-22s [%.3i]\n", comment, mbNum);
  123. }
  124. /*------------------------------------------------------------------------------
  125. EncComment()
  126. ------------------------------------------------------------------------------*/
  127. void EncComment(const char *comment)
  128. {
  129. FILE *file = traceStream.file;
  130. if (file == NULL || traceStream.disableStreamTrace) {
  131. return;
  132. }
  133. fprintf(file, "%s\n", comment);
  134. }