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.

172 lines
5.9KB

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