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.

165 lines
5.7KB

  1. /*
  2. * FFV1 decoder template
  3. *
  4. * Copyright (c) 2003-2016 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. static av_always_inline void RENAME(decode_line)(FFV1Context *s, int w,
  23. TYPE *sample[2],
  24. int plane_index, int bits)
  25. {
  26. PlaneContext *const p = &s->plane[plane_index];
  27. RangeCoder *const c = &s->c;
  28. int x;
  29. int run_count = 0;
  30. int run_mode = 0;
  31. int run_index = s->run_index;
  32. if (s->slice_coding_mode == 1) {
  33. int i;
  34. for (x = 0; x < w; x++) {
  35. int v = 0;
  36. for (i=0; i<bits; i++) {
  37. uint8_t state = 128;
  38. v += v + get_rac(c, &state);
  39. }
  40. sample[1][x] = v;
  41. }
  42. return;
  43. }
  44. for (x = 0; x < w; x++) {
  45. int diff, context, sign;
  46. context = RENAME(get_context)(p, sample[1] + x, sample[0] + x, sample[1] + x);
  47. if (context < 0) {
  48. context = -context;
  49. sign = 1;
  50. } else
  51. sign = 0;
  52. av_assert2(context < p->context_count);
  53. if (s->ac != AC_GOLOMB_RICE) {
  54. diff = get_symbol_inline(c, p->state[context], 1);
  55. } else {
  56. if (context == 0 && run_mode == 0)
  57. run_mode = 1;
  58. if (run_mode) {
  59. if (run_count == 0 && run_mode == 1) {
  60. if (get_bits1(&s->gb)) {
  61. run_count = 1 << ff_log2_run[run_index];
  62. if (x + run_count <= w)
  63. run_index++;
  64. } else {
  65. if (ff_log2_run[run_index])
  66. run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  67. else
  68. run_count = 0;
  69. if (run_index)
  70. run_index--;
  71. run_mode = 2;
  72. }
  73. }
  74. run_count--;
  75. if (run_count < 0) {
  76. run_mode = 0;
  77. run_count = 0;
  78. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
  79. bits);
  80. if (diff >= 0)
  81. diff++;
  82. } else
  83. diff = 0;
  84. } else
  85. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  86. ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  87. run_count, run_index, run_mode, x, get_bits_count(&s->gb));
  88. }
  89. if (sign)
  90. diff = -(unsigned)diff;
  91. sample[1][x] = av_mod_uintp2(RENAME(predict)(sample[1] + x, sample[0] + x) + diff, bits);
  92. }
  93. s->run_index = run_index;
  94. }
  95. static void RENAME(decode_rgb_frame)(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
  96. {
  97. int x, y, p;
  98. TYPE *sample[4][2];
  99. int lbd = s->avctx->bits_per_raw_sample <= 8;
  100. int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
  101. int offset = 1 << bits;
  102. for (x = 0; x < 4; x++) {
  103. sample[x][0] = RENAME(s->sample_buffer) + x * 2 * (w + 6) + 3;
  104. sample[x][1] = RENAME(s->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
  105. }
  106. s->run_index = 0;
  107. memset(RENAME(s->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(s->sample_buffer)));
  108. for (y = 0; y < h; y++) {
  109. for (p = 0; p < 3 + s->transparency; p++) {
  110. TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
  111. sample[p][0] = sample[p][1];
  112. sample[p][1] = temp;
  113. sample[p][1][-1]= sample[p][0][0 ];
  114. sample[p][0][ w]= sample[p][0][w-1];
  115. if (lbd && s->slice_coding_mode == 0)
  116. RENAME(decode_line)(s, w, sample[p], (p + 1)/2, 9);
  117. else
  118. RENAME(decode_line)(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
  119. }
  120. for (x = 0; x < w; x++) {
  121. int g = sample[0][1][x];
  122. int b = sample[1][1][x];
  123. int r = sample[2][1][x];
  124. int a = sample[3][1][x];
  125. if (s->slice_coding_mode != 1) {
  126. b -= offset;
  127. r -= offset;
  128. g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
  129. b += g;
  130. r += g;
  131. }
  132. if (lbd)
  133. *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
  134. else if (sizeof(TYPE) == 4) {
  135. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
  136. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
  137. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  138. } else {
  139. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
  140. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
  141. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  142. }
  143. }
  144. }
  145. }