ffv1dec_template.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include "ffv1_template.c"
  23. static av_always_inline int
  24. RENAME(decode_line)(FFV1Context *f, FFV1SliceContext *sc,
  25. GetBitContext *gb,
  26. int w, TYPE *sample[2], int plane_index, int bits,
  27. int ac)
  28. {
  29. PlaneContext *const p = &sc->plane[plane_index];
  30. RangeCoder *const c = &sc->c;
  31. const int16_t (*quant_table)[256] = f->quant_tables[p->quant_table_index];
  32. int x;
  33. int run_count = 0;
  34. int run_mode = 0;
  35. int run_index = sc->run_index;
  36. if (is_input_end(c, gb, ac))
  37. return AVERROR_INVALIDDATA;
  38. if (sc->slice_coding_mode == 1) {
  39. int i;
  40. for (x = 0; x < w; x++) {
  41. int v = 0;
  42. for (i=0; i<bits; i++) {
  43. uint8_t state = 128;
  44. v += v + get_rac(c, &state);
  45. }
  46. sample[1][x] = v;
  47. }
  48. return 0;
  49. }
  50. for (x = 0; x < w; x++) {
  51. int diff, context, sign;
  52. if (!(x & 1023)) {
  53. if (is_input_end(c, gb, ac))
  54. return AVERROR_INVALIDDATA;
  55. }
  56. context = RENAME(get_context)(quant_table,
  57. sample[1] + x, sample[0] + x, sample[1] + x);
  58. if (context < 0) {
  59. context = -context;
  60. sign = 1;
  61. } else
  62. sign = 0;
  63. av_assert2(context < p->context_count);
  64. if (ac != AC_GOLOMB_RICE) {
  65. diff = get_symbol_inline(c, p->state[context], 1);
  66. } else {
  67. if (context == 0 && run_mode == 0)
  68. run_mode = 1;
  69. if (run_mode) {
  70. if (run_count == 0 && run_mode == 1) {
  71. if (get_bits1(gb)) {
  72. run_count = 1 << ff_log2_run[run_index];
  73. if (x + run_count <= w)
  74. run_index++;
  75. } else {
  76. if (ff_log2_run[run_index])
  77. run_count = get_bits(gb, ff_log2_run[run_index]);
  78. else
  79. run_count = 0;
  80. if (run_index)
  81. run_index--;
  82. run_mode = 2;
  83. }
  84. }
  85. if (sample[1][x - 1] == sample[0][x - 1]) {
  86. while (run_count > 1 && w-x > 1) {
  87. sample[1][x] = sample[0][x];
  88. x++;
  89. run_count--;
  90. }
  91. } else {
  92. while (run_count > 1 && w-x > 1) {
  93. sample[1][x] = RENAME(predict)(sample[1] + x, sample[0] + x);
  94. x++;
  95. run_count--;
  96. }
  97. }
  98. run_count--;
  99. if (run_count < 0) {
  100. run_mode = 0;
  101. run_count = 0;
  102. diff = get_vlc_symbol(gb, &p->vlc_state[context],
  103. bits);
  104. if (diff >= 0)
  105. diff++;
  106. } else
  107. diff = 0;
  108. } else
  109. diff = get_vlc_symbol(gb, &p->vlc_state[context], bits);
  110. ff_dlog(f->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  111. run_count, run_index, run_mode, x, get_bits_count(gb));
  112. }
  113. if (sign)
  114. diff = -(unsigned)diff;
  115. sample[1][x] = av_zero_extend(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
  116. }
  117. sc->run_index = run_index;
  118. return 0;
  119. }
  120. static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
  121. GetBitContext *gb,
  122. uint8_t *src[4], int w, int h, int stride[4])
  123. {
  124. int x, y, p;
  125. TYPE *sample[4][2];
  126. int lbd = f->avctx->bits_per_raw_sample <= 8;
  127. int bits = f->avctx->bits_per_raw_sample > 0 ? f->avctx->bits_per_raw_sample : 8;
  128. int offset = 1 << bits;
  129. int transparency = f->transparency;
  130. int ac = f->ac;
  131. if (sc->slice_coding_mode == 1)
  132. ac = 1;
  133. for (x = 0; x < 4; x++) {
  134. sample[x][0] = RENAME(sc->sample_buffer) + x * 2 * (w + 6) + 3;
  135. sample[x][1] = RENAME(sc->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
  136. }
  137. sc->run_index = 0;
  138. memset(RENAME(sc->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
  139. if (sc->remap) {
  140. for (int p= 0; p<3 + transparency; p++) {
  141. int j = 0;
  142. int lu = 0;
  143. uint8_t state[2][32];
  144. memset(state, 128, sizeof(state));
  145. for (int i= 0; i<65536; i++) {
  146. int run = get_symbol_inline(&sc->c, state[lu], 0);
  147. if (run > 65536U - i)
  148. return AVERROR_INVALIDDATA;
  149. if (lu) {
  150. lu ^= !run;
  151. while (run--) {
  152. sc->fltmap[p][j++] = i ^ ((i&0x8000) ? 0 : 0x7FFF);
  153. i++;
  154. }
  155. } else {
  156. i += run;
  157. if (i != 65536)
  158. sc->fltmap[p][j++] = i ^ ((i&0x8000) ? 0 : 0x7FFF);
  159. lu ^= !run;
  160. }
  161. }
  162. }
  163. }
  164. for (y = 0; y < h; y++) {
  165. for (p = 0; p < 3 + transparency; p++) {
  166. int ret;
  167. TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
  168. sample[p][0] = sample[p][1];
  169. sample[p][1] = temp;
  170. sample[p][1][-1]= sample[p][0][0 ];
  171. sample[p][0][ w]= sample[p][0][w-1];
  172. if (lbd && sc->slice_coding_mode == 0)
  173. ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, 9, ac);
  174. else
  175. ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, bits + (sc->slice_coding_mode != 1), ac);
  176. if (ret < 0)
  177. return ret;
  178. }
  179. for (x = 0; x < w; x++) {
  180. int g = sample[0][1][x];
  181. int b = sample[1][1][x];
  182. int r = sample[2][1][x];
  183. int a = sample[3][1][x];
  184. if (sc->slice_coding_mode != 1) {
  185. b -= offset;
  186. r -= offset;
  187. g -= (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
  188. b += g;
  189. r += g;
  190. }
  191. if (sc->remap) {
  192. r = sc->fltmap[0][r & 0xFFFF];
  193. g = sc->fltmap[1][g & 0xFFFF];
  194. b = sc->fltmap[2][b & 0xFFFF];
  195. if (transparency)
  196. a = sc->fltmap[3][a & 0xFFFF];
  197. }
  198. if (lbd)
  199. *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
  200. else if (sizeof(TYPE) == 4 || transparency) {
  201. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
  202. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
  203. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  204. if (transparency)
  205. *((uint16_t*)(src[3] + x*2 + stride[3]*y)) = a;
  206. } else {
  207. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
  208. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
  209. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  210. }
  211. }
  212. }
  213. return 0;
  214. }