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.

194 lines
6.6KB

  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 int 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 (is_input_end(s))
  33. return AVERROR_INVALIDDATA;
  34. if (s->slice_coding_mode == 1) {
  35. int i;
  36. for (x = 0; x < w; x++) {
  37. int v = 0;
  38. for (i=0; i<bits; i++) {
  39. uint8_t state = 128;
  40. v += v + get_rac(c, &state);
  41. }
  42. sample[1][x] = v;
  43. }
  44. return 0;
  45. }
  46. for (x = 0; x < w; x++) {
  47. int diff, context, sign;
  48. if (!(x & 1023)) {
  49. if (is_input_end(s))
  50. return AVERROR_INVALIDDATA;
  51. }
  52. context = RENAME(get_context)(p, sample[1] + x, sample[0] + x, sample[1] + x);
  53. if (context < 0) {
  54. context = -context;
  55. sign = 1;
  56. } else
  57. sign = 0;
  58. av_assert2(context < p->context_count);
  59. if (s->ac != AC_GOLOMB_RICE) {
  60. diff = get_symbol_inline(c, p->state[context], 1);
  61. } else {
  62. if (context == 0 && run_mode == 0)
  63. run_mode = 1;
  64. if (run_mode) {
  65. if (run_count == 0 && run_mode == 1) {
  66. if (get_bits1(&s->gb)) {
  67. run_count = 1 << ff_log2_run[run_index];
  68. if (x + run_count <= w)
  69. run_index++;
  70. } else {
  71. if (ff_log2_run[run_index])
  72. run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  73. else
  74. run_count = 0;
  75. if (run_index)
  76. run_index--;
  77. run_mode = 2;
  78. }
  79. }
  80. if (sample[1][x - 1] == sample[0][x - 1]) {
  81. while (run_count > 1 && w-x > 1) {
  82. sample[1][x] = sample[0][x];
  83. x++;
  84. run_count--;
  85. }
  86. } else {
  87. while (run_count > 1 && w-x > 1) {
  88. sample[1][x] = RENAME(predict)(sample[1] + x, sample[0] + x);
  89. x++;
  90. run_count--;
  91. }
  92. }
  93. run_count--;
  94. if (run_count < 0) {
  95. run_mode = 0;
  96. run_count = 0;
  97. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
  98. bits);
  99. if (diff >= 0)
  100. diff++;
  101. } else
  102. diff = 0;
  103. } else
  104. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  105. ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  106. run_count, run_index, run_mode, x, get_bits_count(&s->gb));
  107. }
  108. if (sign)
  109. diff = -(unsigned)diff;
  110. sample[1][x] = av_mod_uintp2(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
  111. }
  112. s->run_index = run_index;
  113. return 0;
  114. }
  115. static int RENAME(decode_rgb_frame)(FFV1Context *s, uint8_t *src[4], int w, int h, int stride[4])
  116. {
  117. int x, y, p;
  118. TYPE *sample[4][2];
  119. int lbd = s->avctx->bits_per_raw_sample <= 8;
  120. int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
  121. int offset = 1 << bits;
  122. int transparency = s->transparency;
  123. for (x = 0; x < 4; x++) {
  124. sample[x][0] = RENAME(s->sample_buffer) + x * 2 * (w + 6) + 3;
  125. sample[x][1] = RENAME(s->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
  126. }
  127. s->run_index = 0;
  128. memset(RENAME(s->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(s->sample_buffer)));
  129. for (y = 0; y < h; y++) {
  130. for (p = 0; p < 3 + transparency; p++) {
  131. int ret;
  132. TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
  133. sample[p][0] = sample[p][1];
  134. sample[p][1] = temp;
  135. sample[p][1][-1]= sample[p][0][0 ];
  136. sample[p][0][ w]= sample[p][0][w-1];
  137. if (lbd && s->slice_coding_mode == 0)
  138. ret = RENAME(decode_line)(s, w, sample[p], (p + 1)/2, 9);
  139. else
  140. ret = RENAME(decode_line)(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
  141. if (ret < 0)
  142. return ret;
  143. }
  144. for (x = 0; x < w; x++) {
  145. int g = sample[0][1][x];
  146. int b = sample[1][1][x];
  147. int r = sample[2][1][x];
  148. int a = sample[3][1][x];
  149. if (s->slice_coding_mode != 1) {
  150. b -= offset;
  151. r -= offset;
  152. g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
  153. b += g;
  154. r += g;
  155. }
  156. if (lbd)
  157. *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
  158. else if (sizeof(TYPE) == 4 || transparency) {
  159. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
  160. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
  161. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  162. if (transparency)
  163. *((uint16_t*)(src[3] + x*2 + stride[3]*y)) = a;
  164. } else {
  165. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
  166. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
  167. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  168. }
  169. }
  170. }
  171. return 0;
  172. }