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.

120 lines
2.7KB

  1. /*
  2. * MQ-coder encoder
  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 encoder
  23. * @file
  24. * @author Kamil Nowosad
  25. */
  26. #include "mqc.h"
  27. static void byteout(MqcState *mqc)
  28. {
  29. retry:
  30. if (*mqc->bp == 0xff){
  31. mqc->bp++;
  32. *mqc->bp = mqc->c >> 20;
  33. mqc->c &= 0xfffff;
  34. mqc->ct = 7;
  35. } else if ((mqc->c & 0x8000000)){
  36. (*mqc->bp)++;
  37. mqc->c &= 0x7ffffff;
  38. goto retry;
  39. } else{
  40. mqc->bp++;
  41. *mqc->bp = mqc->c >> 19;
  42. mqc->c &= 0x7ffff;
  43. mqc->ct = 8;
  44. }
  45. }
  46. static void renorme(MqcState *mqc)
  47. {
  48. do{
  49. mqc->a += mqc->a;
  50. mqc->c += mqc->c;
  51. if (!--mqc->ct)
  52. byteout(mqc);
  53. } while (!(mqc->a & 0x8000));
  54. }
  55. static void setbits(MqcState *mqc)
  56. {
  57. int tmp = mqc->c + mqc->a;
  58. mqc->c |= 0xffff;
  59. if (mqc->c >= tmp)
  60. mqc->c -= 0x8000;
  61. }
  62. void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
  63. {
  64. ff_mqc_init_contexts(mqc);
  65. mqc->a = 0x8000;
  66. mqc->c = 0;
  67. mqc->bp = bp-1;
  68. mqc->bpstart = bp;
  69. mqc->ct = 12 + (*mqc->bp == 0xff);
  70. }
  71. void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
  72. {
  73. int qe;
  74. qe = ff_mqc_qe[*cxstate];
  75. mqc->a -= qe;
  76. if ((*cxstate & 1) == d){
  77. if (!(mqc->a & 0x8000)){
  78. if (mqc->a < qe)
  79. mqc->a = qe;
  80. else
  81. mqc->c += qe;
  82. *cxstate = ff_mqc_nmps[*cxstate];
  83. renorme(mqc);
  84. } else
  85. mqc->c += qe;
  86. } else{
  87. if (mqc->a < qe)
  88. mqc->c += qe;
  89. else
  90. mqc->a = qe;
  91. *cxstate = ff_mqc_nlps[*cxstate];
  92. renorme(mqc);
  93. }
  94. }
  95. int ff_mqc_length(MqcState *mqc)
  96. {
  97. return mqc->bp - mqc->bpstart;
  98. }
  99. int ff_mqc_flush(MqcState *mqc)
  100. {
  101. setbits(mqc);
  102. mqc->c = mqc->c << mqc->ct;
  103. byteout(mqc);
  104. mqc->c = mqc->c << mqc->ct;
  105. byteout(mqc);
  106. if (*mqc->bp != 0xff)
  107. mqc->bp++;
  108. return mqc->bp - mqc->bpstart;
  109. }