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.

94 lines
2.4KB

  1. /*
  2. * MQ-coder decoder
  3. * Copyright (c) 2007 Kamil Nowosad
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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)
  67. {
  68. ff_mqc_init_contexts(mqc);
  69. mqc->bp = bp;
  70. mqc->c = (*mqc->bp ^ 0xff) << 16;
  71. bytein(mqc);
  72. mqc->c = mqc->c << 7;
  73. mqc->a = 0x8000;
  74. }
  75. int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
  76. {
  77. mqc->a -= ff_mqc_qe[*cxstate];
  78. if ((mqc->c >> 16) < mqc->a) {
  79. if (mqc->a & 0x8000)
  80. return *cxstate & 1;
  81. else
  82. return exchange(mqc, cxstate, 0);
  83. } else {
  84. mqc->c -= mqc->a << 16;
  85. return exchange(mqc, cxstate, 1);
  86. }
  87. }