You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.9KB

  1. /*
  2. * Copyright (c) 2016 Umair Khan <omerjerk@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "mlz.h"
  21. av_cold void ff_mlz_init_dict(void* context, MLZ *mlz) {
  22. mlz->dict = av_mallocz_array(TABLE_SIZE, sizeof(*mlz->dict));
  23. mlz->flush_code = FLUSH_CODE;
  24. mlz->current_dic_index_max = DIC_INDEX_INIT;
  25. mlz->dic_code_bit = CODE_BIT_INIT;
  26. mlz->bump_code = (DIC_INDEX_INIT - 1);
  27. mlz->next_code = FIRST_CODE;
  28. mlz->freeze_flag = 0;
  29. mlz->context = context;
  30. }
  31. av_cold void ff_mlz_flush_dict(MLZ *mlz) {
  32. MLZDict *dict = mlz->dict;
  33. int i;
  34. for ( i = 0; i < TABLE_SIZE; i++ ) {
  35. dict[i].string_code = CODE_UNSET;
  36. dict[i].parent_code = CODE_UNSET;
  37. dict[i].match_len = 0;
  38. }
  39. mlz->current_dic_index_max = DIC_INDEX_INIT;
  40. mlz->dic_code_bit = CODE_BIT_INIT; // DicCodeBitInit;
  41. mlz->bump_code = mlz->current_dic_index_max - 1;
  42. mlz->next_code = FIRST_CODE;
  43. mlz->freeze_flag = 0;
  44. }
  45. static void set_new_entry_dict(MLZDict* dict, int string_code, int parent_code, int char_code) {
  46. dict[string_code].parent_code = parent_code;
  47. dict[string_code].string_code = string_code;
  48. dict[string_code].char_code = char_code;
  49. if (parent_code < FIRST_CODE) {
  50. dict[string_code].match_len = 2;
  51. } else {
  52. dict[string_code].match_len = (dict[parent_code].match_len) + 1;
  53. }
  54. }
  55. static int decode_string(MLZ* mlz, unsigned char *buff, int string_code, int *first_char_code, unsigned long bufsize) {
  56. MLZDict* dict = mlz->dict;
  57. unsigned long count, offset;
  58. int current_code, parent_code, tmp_code;
  59. count = 0;
  60. current_code = string_code;
  61. *first_char_code = CODE_UNSET;
  62. while (count < bufsize) {
  63. switch (current_code) {
  64. case CODE_UNSET:
  65. return count;
  66. break;
  67. default:
  68. if (current_code < FIRST_CODE) {
  69. *first_char_code = current_code;
  70. buff[0] = current_code;
  71. count++;
  72. return count;
  73. } else {
  74. offset = dict[current_code].match_len - 1;
  75. tmp_code = dict[current_code].char_code;
  76. if (offset >= bufsize) {
  77. av_log(mlz->context, AV_LOG_ERROR, "MLZ offset error.\n");
  78. return count;
  79. }
  80. buff[offset] = tmp_code;
  81. count++;
  82. }
  83. current_code = dict[current_code].parent_code;
  84. if ((current_code < 0) || (current_code > (DIC_INDEX_MAX - 1))) {
  85. av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
  86. return count;
  87. }
  88. if (current_code > FIRST_CODE) {
  89. parent_code = dict[current_code].parent_code;
  90. offset = (dict[current_code].match_len) - 1;
  91. if (parent_code < 0 || parent_code > DIC_INDEX_MAX-1) {
  92. av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
  93. return count;
  94. }
  95. if (( offset > (DIC_INDEX_MAX - 1))) {
  96. av_log(mlz->context, AV_LOG_ERROR, "MLZ dic offset error.\n");
  97. return count;
  98. }
  99. }
  100. break;
  101. }
  102. }
  103. return count;
  104. }
  105. static int input_code(GetBitContext* gb, int len) {
  106. int tmp_code = 0;
  107. int i;
  108. for (i = 0; i < len; ++i) {
  109. tmp_code |= get_bits1(gb) << i;
  110. }
  111. return tmp_code;
  112. }
  113. int ff_mlz_decompression(MLZ* mlz, GetBitContext* gb, int size, unsigned char *buff) {
  114. MLZDict *dict = mlz->dict;
  115. unsigned long output_chars;
  116. int string_code, last_string_code, char_code;
  117. string_code = 0;
  118. char_code = -1;
  119. last_string_code = -1;
  120. output_chars = 0;
  121. while (output_chars < size) {
  122. string_code = input_code(gb, mlz->dic_code_bit);
  123. switch (string_code) {
  124. case FLUSH_CODE:
  125. case MAX_CODE:
  126. ff_mlz_flush_dict(mlz);
  127. char_code = -1;
  128. last_string_code = -1;
  129. break;
  130. case FREEZE_CODE:
  131. mlz->freeze_flag = 1;
  132. break;
  133. default:
  134. if (string_code > mlz->current_dic_index_max) {
  135. av_log(mlz->context, AV_LOG_ERROR, "String code %d exceeds maximum value of %d.\n", string_code, mlz->current_dic_index_max);
  136. return output_chars;
  137. }
  138. if (string_code == (int) mlz->bump_code) {
  139. ++mlz->dic_code_bit;
  140. mlz->current_dic_index_max *= 2;
  141. mlz->bump_code = mlz->current_dic_index_max - 1;
  142. } else {
  143. if (string_code >= mlz->next_code) {
  144. int ret = decode_string(mlz, &buff[output_chars], last_string_code, &char_code, size - output_chars);
  145. if (ret < 0 || ret > size - output_chars) {
  146. av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
  147. return output_chars;
  148. }
  149. output_chars += ret;
  150. ret = decode_string(mlz, &buff[output_chars], char_code, &char_code, size - output_chars);
  151. if (ret < 0 || ret > size - output_chars) {
  152. av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
  153. return output_chars;
  154. }
  155. output_chars += ret;
  156. set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
  157. if (mlz->next_code >= TABLE_SIZE - 1) {
  158. av_log(mlz->context, AV_LOG_ERROR, "Too many MLZ codes\n");
  159. return output_chars;
  160. }
  161. mlz->next_code++;
  162. } else {
  163. int ret = decode_string(mlz, &buff[output_chars], string_code, &char_code, size - output_chars);
  164. if (ret < 0 || ret > size - output_chars) {
  165. av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
  166. return output_chars;
  167. }
  168. output_chars += ret;
  169. if (output_chars <= size && !mlz->freeze_flag) {
  170. if (last_string_code != -1) {
  171. set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
  172. if (mlz->next_code >= TABLE_SIZE - 1) {
  173. av_log(mlz->context, AV_LOG_ERROR, "Too many MLZ codes\n");
  174. return output_chars;
  175. }
  176. mlz->next_code++;
  177. }
  178. } else {
  179. break;
  180. }
  181. }
  182. last_string_code = string_code;
  183. }
  184. break;
  185. }
  186. }
  187. return output_chars;
  188. }