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.

108 lines
2.7KB

  1. /*
  2. * MQ-coder decoder
  3. * Copyright (c) 2007 Kamil Nowosad
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * MQ-coder decoder
  23. * @file
  24. * @author Kamil Nowosad
  25. */
  26. #include "mqc.h"
  27. static void bytein(MqcState *mqc)
  28. {
  29. if (*mqc->bp == 0xff) {
  30. if (*(mqc->bp + 1) > 0x8f)
  31. mqc->c++;
  32. else {
  33. mqc->bp++;
  34. mqc->c += 2 + 0xfe00 - (*mqc->bp << 9);
  35. }
  36. } else {
  37. mqc->bp++;
  38. mqc->c += 1 + 0xff00 - (*mqc->bp << 8);
  39. }
  40. }
  41. static int exchange(MqcState *mqc, uint8_t *cxstate, int lps)
  42. {
  43. int d;
  44. if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)) {
  45. if (lps)
  46. mqc->a = ff_mqc_qe[*cxstate];
  47. d = *cxstate & 1;
  48. *cxstate = ff_mqc_nmps[*cxstate];
  49. } else {
  50. if (lps)
  51. mqc->a = ff_mqc_qe[*cxstate];
  52. d = 1 - (*cxstate & 1);
  53. *cxstate = ff_mqc_nlps[*cxstate];
  54. }
  55. // do RENORMD: see ISO/IEC 15444-1:2002 §C.3.3
  56. do {
  57. if (!(mqc->c & 0xff)) {
  58. mqc->c -= 0x100;
  59. bytein(mqc);
  60. }
  61. mqc->a += mqc->a;
  62. mqc->c += mqc->c;
  63. } while (!(mqc->a & 0x8000));
  64. return d;
  65. }
  66. void ff_mqc_initdec(MqcState *mqc, uint8_t *bp, int raw, int reset)
  67. {
  68. mqc->raw = raw;
  69. if (reset)
  70. ff_mqc_init_contexts(mqc);
  71. mqc->bp = bp;
  72. mqc->c = (*mqc->bp ^ 0xff) << 16;
  73. bytein(mqc);
  74. mqc->c = mqc->c << 7;
  75. mqc->a = 0x8000;
  76. }
  77. static int mqc_decode_bypass(MqcState *mqc) {
  78. int bit = !(mqc->c & 0x40000000);
  79. if (!(mqc->c & 0xff)) {
  80. mqc->c -= 0x100;
  81. bytein(mqc);
  82. }
  83. mqc->c += mqc->c;
  84. return bit;
  85. }
  86. int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
  87. {
  88. if (mqc->raw)
  89. return mqc_decode_bypass(mqc);
  90. mqc->a -= ff_mqc_qe[*cxstate];
  91. if ((mqc->c >> 16) < mqc->a) {
  92. if (mqc->a & 0x8000)
  93. return *cxstate & 1;
  94. else
  95. return exchange(mqc, cxstate, 0);
  96. } else {
  97. mqc->c -= mqc->a << 16;
  98. return exchange(mqc, cxstate, 1);
  99. }
  100. }