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.

181 lines
6.1KB

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