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.

362 lines
11KB

  1. /*
  2. * JPEG-LS decoder
  3. * Copyright (c) 2003 Michael Niedermayer
  4. * Copyright (c) 2006 Konstantin Shishkov
  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. /**
  23. * @file jpeglsdec.c
  24. * JPEG-LS decoder.
  25. */
  26. #include "avcodec.h"
  27. #include "bitstream.h"
  28. #include "golomb.h"
  29. #include "mjpeg.h"
  30. #include "jpegls.h"
  31. #include "jpeglsdec.h"
  32. /*
  33. * Uncomment this to significantly speed up decoding of broken JPEG-LS
  34. * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
  35. *
  36. * There is no Golomb code with length >= 32 bits possible, so check and
  37. * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
  38. * on this errors.
  39. */
  40. //#define JLS_BROKEN
  41. /**
  42. * Decode LSE block with initialization parameters
  43. */
  44. int ff_jpegls_decode_lse(MJpegDecodeContext *s)
  45. {
  46. int len, id;
  47. /* XXX: verify len field validity */
  48. len = get_bits(&s->gb, 16);
  49. id = get_bits(&s->gb, 8);
  50. switch(id){
  51. case 1:
  52. s->maxval= get_bits(&s->gb, 16);
  53. s->t1= get_bits(&s->gb, 16);
  54. s->t2= get_bits(&s->gb, 16);
  55. s->t3= get_bits(&s->gb, 16);
  56. s->reset= get_bits(&s->gb, 16);
  57. // ff_jpegls_reset_coding_parameters(s, 0);
  58. //FIXME quant table?
  59. break;
  60. case 2:
  61. case 3:
  62. av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
  63. return -1;
  64. case 4:
  65. av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
  66. return -1;
  67. default:
  68. av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
  69. return -1;
  70. }
  71. // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
  72. return 0;
  73. }
  74. /**
  75. * Get context-dependent Golomb code, decode it and update context
  76. */
  77. static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
  78. int k, ret;
  79. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  80. #ifdef JLS_BROKEN
  81. if(!show_bits_long(gb, 32))return -1;
  82. #endif
  83. ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
  84. /* decode mapped error */
  85. if(ret & 1)
  86. ret = -((ret + 1) >> 1);
  87. else
  88. ret >>= 1;
  89. /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
  90. if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
  91. ret = -(ret + 1);
  92. ret= ff_jpegls_update_state_regular(state, Q, ret);
  93. return ret;
  94. }
  95. /**
  96. * Get Golomb code, decode it and update state for run termination
  97. */
  98. static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
  99. int k, ret, temp, map;
  100. int Q = 365 + RItype;
  101. temp= state->A[Q];
  102. if(RItype)
  103. temp += state->N[Q] >> 1;
  104. for(k = 0; (state->N[Q] << k) < temp; k++);
  105. #ifdef JLS_BROKEN
  106. if(!show_bits_long(gb, 32))return -1;
  107. #endif
  108. ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
  109. /* decode mapped error */
  110. map = 0;
  111. if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
  112. map = 1;
  113. ret += RItype + map;
  114. if(ret & 1){
  115. ret = map - ((ret + 1) >> 1);
  116. state->B[Q]++;
  117. } else {
  118. ret = ret >> 1;
  119. }
  120. /* update state */
  121. state->A[Q] += FFABS(ret) - RItype;
  122. ret *= state->twonear;
  123. ff_jpegls_downscale_state(state, Q);
  124. return ret;
  125. }
  126. /**
  127. * Decode one line of image
  128. */
  129. static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
  130. int i, x = 0;
  131. int Ra, Rb, Rc, Rd;
  132. int D0, D1, D2;
  133. while(x < w) {
  134. int err, pred;
  135. /* compute gradients */
  136. Ra = x ? R(dst, x - stride) : R(last, x);
  137. Rb = R(last, x);
  138. Rc = x ? R(last, x - stride) : last2;
  139. Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
  140. D0 = Rd - Rb;
  141. D1 = Rb - Rc;
  142. D2 = Rc - Ra;
  143. /* run mode */
  144. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  145. int r;
  146. int RItype;
  147. /* decode full runs while available */
  148. while(get_bits1(&s->gb)) {
  149. int r;
  150. r = 1 << ff_log2_run[state->run_index[comp]];
  151. if(x + r * stride > w) {
  152. r = (w - x) / stride;
  153. }
  154. for(i = 0; i < r; i++) {
  155. W(dst, x, Ra);
  156. x += stride;
  157. }
  158. /* if EOL reached, we stop decoding */
  159. if(r != (1 << ff_log2_run[state->run_index[comp]]))
  160. return;
  161. if(state->run_index[comp] < 31)
  162. state->run_index[comp]++;
  163. if(x + stride > w)
  164. return;
  165. }
  166. /* decode aborted run */
  167. r = ff_log2_run[state->run_index[comp]];
  168. if(r)
  169. r = get_bits_long(&s->gb, r);
  170. for(i = 0; i < r; i++) {
  171. W(dst, x, Ra);
  172. x += stride;
  173. }
  174. /* decode run termination value */
  175. Rb = R(last, x);
  176. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  177. err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
  178. if(state->run_index[comp])
  179. state->run_index[comp]--;
  180. if(state->near && RItype){
  181. pred = Ra + err;
  182. } else {
  183. if(Rb < Ra)
  184. pred = Rb - err;
  185. else
  186. pred = Rb + err;
  187. }
  188. } else { /* regular mode */
  189. int context, sign;
  190. context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
  191. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  192. if(context < 0){
  193. context = -context;
  194. sign = 1;
  195. }else{
  196. sign = 0;
  197. }
  198. if(sign){
  199. pred = av_clip(pred - state->C[context], 0, state->maxval);
  200. err = -ls_get_code_regular(&s->gb, state, context);
  201. } else {
  202. pred = av_clip(pred + state->C[context], 0, state->maxval);
  203. err = ls_get_code_regular(&s->gb, state, context);
  204. }
  205. /* we have to do something more for near-lossless coding */
  206. pred += err;
  207. }
  208. if(state->near){
  209. if(pred < -state->near)
  210. pred += state->range * state->twonear;
  211. else if(pred > state->maxval + state->near)
  212. pred -= state->range * state->twonear;
  213. pred = av_clip(pred, 0, state->maxval);
  214. }
  215. pred &= state->maxval;
  216. W(dst, x, pred);
  217. x += stride;
  218. }
  219. }
  220. int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
  221. int i, t = 0;
  222. uint8_t *zero, *last, *cur;
  223. JLSState *state;
  224. int off = 0, stride = 1, width, shift;
  225. zero = av_mallocz(s->picture.linesize[0]);
  226. last = zero;
  227. cur = s->picture.data[0];
  228. state = av_mallocz(sizeof(JLSState));
  229. /* initialize JPEG-LS state from JPEG parameters */
  230. state->near = near;
  231. state->bpp = (s->bits < 2) ? 2 : s->bits;
  232. state->maxval = s->maxval;
  233. state->T1 = s->t1;
  234. state->T2 = s->t2;
  235. state->T3 = s->t3;
  236. state->reset = s->reset;
  237. ff_jpegls_reset_coding_parameters(state, 0);
  238. ff_jpegls_init_state(state);
  239. if(s->bits <= 8)
  240. shift = point_transform + (8 - s->bits);
  241. else
  242. shift = point_transform + (16 - s->bits);
  243. // av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
  244. // av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
  245. if(ilv == 0) { /* separate planes */
  246. off = s->cur_scan - 1;
  247. stride = (s->nb_components > 1) ? 3 : 1;
  248. width = s->width * stride;
  249. cur += off;
  250. for(i = 0; i < s->height; i++) {
  251. if(s->bits <= 8){
  252. ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
  253. t = last[0];
  254. }else{
  255. ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
  256. t = *((uint16_t*)last);
  257. }
  258. last = cur;
  259. cur += s->picture.linesize[0];
  260. if (s->restart_interval && !--s->restart_count) {
  261. align_get_bits(&s->gb);
  262. skip_bits(&s->gb, 16); /* skip RSTn */
  263. }
  264. }
  265. } else if(ilv == 1) { /* line interleaving */
  266. int j;
  267. int Rc[3] = {0, 0, 0};
  268. memset(cur, 0, s->picture.linesize[0]);
  269. width = s->width * 3;
  270. for(i = 0; i < s->height; i++) {
  271. for(j = 0; j < 3; j++) {
  272. ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
  273. Rc[j] = last[j];
  274. if (s->restart_interval && !--s->restart_count) {
  275. align_get_bits(&s->gb);
  276. skip_bits(&s->gb, 16); /* skip RSTn */
  277. }
  278. }
  279. last = cur;
  280. cur += s->picture.linesize[0];
  281. }
  282. } else if(ilv == 2) { /* sample interleaving */
  283. av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
  284. av_free(state);
  285. av_free(zero);
  286. return -1;
  287. }
  288. if(shift){ /* we need to do point transform or normalize samples */
  289. int x, w;
  290. w = s->width * s->nb_components;
  291. if(s->bits <= 8){
  292. uint8_t *src = s->picture.data[0];
  293. for(i = 0; i < s->height; i++){
  294. for(x = off; x < w; x+= stride){
  295. src[x] <<= shift;
  296. }
  297. src += s->picture.linesize[0];
  298. }
  299. }else{
  300. uint16_t *src = (uint16_t*) s->picture.data[0];
  301. for(i = 0; i < s->height; i++){
  302. for(x = 0; x < w; x++){
  303. src[x] <<= shift;
  304. }
  305. src += s->picture.linesize[0]/2;
  306. }
  307. }
  308. }
  309. av_free(state);
  310. av_free(zero);
  311. return 0;
  312. }