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.

186 lines
6.3KB

  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. while (run_count > 1 && w-x > 1) {
  81. sample[1][x] = RENAME(predict)(sample[1] + x, sample[0] + x);
  82. x++;
  83. run_count--;
  84. }
  85. run_count--;
  86. if (run_count < 0) {
  87. run_mode = 0;
  88. run_count = 0;
  89. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
  90. bits);
  91. if (diff >= 0)
  92. diff++;
  93. } else
  94. diff = 0;
  95. } else
  96. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  97. ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  98. run_count, run_index, run_mode, x, get_bits_count(&s->gb));
  99. }
  100. if (sign)
  101. diff = -(unsigned)diff;
  102. sample[1][x] = av_mod_uintp2(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
  103. }
  104. s->run_index = run_index;
  105. return 0;
  106. }
  107. static int RENAME(decode_rgb_frame)(FFV1Context *s, uint8_t *src[4], int w, int h, int stride[4])
  108. {
  109. int x, y, p;
  110. TYPE *sample[4][2];
  111. int lbd = s->avctx->bits_per_raw_sample <= 8;
  112. int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
  113. int offset = 1 << bits;
  114. int transparency = s->transparency;
  115. for (x = 0; x < 4; x++) {
  116. sample[x][0] = RENAME(s->sample_buffer) + x * 2 * (w + 6) + 3;
  117. sample[x][1] = RENAME(s->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
  118. }
  119. s->run_index = 0;
  120. memset(RENAME(s->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(s->sample_buffer)));
  121. for (y = 0; y < h; y++) {
  122. for (p = 0; p < 3 + transparency; p++) {
  123. int ret;
  124. TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
  125. sample[p][0] = sample[p][1];
  126. sample[p][1] = temp;
  127. sample[p][1][-1]= sample[p][0][0 ];
  128. sample[p][0][ w]= sample[p][0][w-1];
  129. if (lbd && s->slice_coding_mode == 0)
  130. ret = RENAME(decode_line)(s, w, sample[p], (p + 1)/2, 9);
  131. else
  132. ret = RENAME(decode_line)(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
  133. if (ret < 0)
  134. return ret;
  135. }
  136. for (x = 0; x < w; x++) {
  137. int g = sample[0][1][x];
  138. int b = sample[1][1][x];
  139. int r = sample[2][1][x];
  140. int a = sample[3][1][x];
  141. if (s->slice_coding_mode != 1) {
  142. b -= offset;
  143. r -= offset;
  144. g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
  145. b += g;
  146. r += g;
  147. }
  148. if (lbd)
  149. *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
  150. else if (sizeof(TYPE) == 4 || transparency) {
  151. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
  152. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
  153. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  154. if (transparency)
  155. *((uint16_t*)(src[3] + x*2 + stride[3]*y)) = a;
  156. } else {
  157. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
  158. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
  159. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  160. }
  161. }
  162. }
  163. return 0;
  164. }