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.

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