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.

400 lines
12KB

  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 AVERROR(ENOSYS);
  65. case 4:
  66. av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
  67. return AVERROR(ENOSYS);
  68. default:
  69. av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
  70. return AVERROR_INVALIDDATA;
  71. }
  72. av_dlog(s->avctx, "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. {
  80. int k, ret;
  81. for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
  82. ;
  83. #ifdef JLS_BROKEN
  84. if (!show_bits_long(gb, 32))
  85. return -1;
  86. #endif
  87. ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
  88. /* decode mapped error */
  89. if (ret & 1)
  90. ret = -(ret + 1 >> 1);
  91. else
  92. ret >>= 1;
  93. /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
  94. if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
  95. ret = -(ret + 1);
  96. ret = ff_jpegls_update_state_regular(state, Q, ret);
  97. return ret;
  98. }
  99. /**
  100. * Get Golomb code, decode it and update state for run termination
  101. */
  102. static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
  103. int RItype, int limit_add)
  104. {
  105. int k, ret, temp, map;
  106. int Q = 365 + RItype;
  107. temp = state->A[Q];
  108. if (RItype)
  109. temp += state->N[Q] >> 1;
  110. for (k = 0; (state->N[Q] << k) < temp; k++)
  111. ;
  112. #ifdef JLS_BROKEN
  113. if (!show_bits_long(gb, 32))
  114. return -1;
  115. #endif
  116. ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
  117. state->qbpp);
  118. /* decode mapped error */
  119. map = 0;
  120. if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
  121. map = 1;
  122. ret += RItype + map;
  123. if (ret & 1) {
  124. ret = map - (ret + 1 >> 1);
  125. state->B[Q]++;
  126. } else {
  127. ret = ret >> 1;
  128. }
  129. /* update state */
  130. state->A[Q] += FFABS(ret) - RItype;
  131. ret *= state->twonear;
  132. ff_jpegls_downscale_state(state, Q);
  133. return ret;
  134. }
  135. /**
  136. * Decode one line of image
  137. */
  138. static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
  139. void *last, void *dst, int last2, int w,
  140. int stride, int comp, int bits)
  141. {
  142. int i, x = 0;
  143. int Ra, Rb, Rc, Rd;
  144. int D0, D1, D2;
  145. while (x < w) {
  146. int err, pred;
  147. /* compute gradients */
  148. Ra = x ? R(dst, x - stride) : R(last, x);
  149. Rb = R(last, x);
  150. Rc = x ? R(last, x - stride) : last2;
  151. Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
  152. D0 = Rd - Rb;
  153. D1 = Rb - Rc;
  154. D2 = Rc - Ra;
  155. /* run mode */
  156. if ((FFABS(D0) <= state->near) &&
  157. (FFABS(D1) <= state->near) &&
  158. (FFABS(D2) <= state->near)) {
  159. int r;
  160. int RItype;
  161. /* decode full runs while available */
  162. while (get_bits1(&s->gb)) {
  163. int r;
  164. r = 1 << ff_log2_run[state->run_index[comp]];
  165. if (x + r * stride > w)
  166. r = (w - x) / stride;
  167. for (i = 0; i < r; i++) {
  168. W(dst, x, Ra);
  169. x += stride;
  170. }
  171. /* if EOL reached, we stop decoding */
  172. if (r != 1 << ff_log2_run[state->run_index[comp]])
  173. return;
  174. if (state->run_index[comp] < 31)
  175. state->run_index[comp]++;
  176. if (x + stride > w)
  177. return;
  178. }
  179. /* decode aborted run */
  180. r = ff_log2_run[state->run_index[comp]];
  181. if (r)
  182. r = get_bits_long(&s->gb, r);
  183. for (i = 0; i < r; i++) {
  184. W(dst, x, Ra);
  185. x += stride;
  186. }
  187. /* decode run termination value */
  188. Rb = R(last, x);
  189. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  190. err = ls_get_code_runterm(&s->gb, state, RItype,
  191. ff_log2_run[state->run_index[comp]]);
  192. if (state->run_index[comp])
  193. state->run_index[comp]--;
  194. if (state->near && RItype) {
  195. pred = Ra + err;
  196. } else {
  197. if (Rb < Ra)
  198. pred = Rb - err;
  199. else
  200. pred = Rb + err;
  201. }
  202. } else { /* regular mode */
  203. int context, sign;
  204. context = ff_jpegls_quantize(state, D0) * 81 +
  205. ff_jpegls_quantize(state, D1) * 9 +
  206. ff_jpegls_quantize(state, D2);
  207. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  208. if (context < 0) {
  209. context = -context;
  210. sign = 1;
  211. } else {
  212. sign = 0;
  213. }
  214. if (sign) {
  215. pred = av_clip(pred - state->C[context], 0, state->maxval);
  216. err = -ls_get_code_regular(&s->gb, state, context);
  217. } else {
  218. pred = av_clip(pred + state->C[context], 0, state->maxval);
  219. err = ls_get_code_regular(&s->gb, state, context);
  220. }
  221. /* we have to do something more for near-lossless coding */
  222. pred += err;
  223. }
  224. if (state->near) {
  225. if (pred < -state->near)
  226. pred += state->range * state->twonear;
  227. else if (pred > state->maxval + state->near)
  228. pred -= state->range * state->twonear;
  229. pred = av_clip(pred, 0, state->maxval);
  230. }
  231. pred &= state->maxval;
  232. W(dst, x, pred);
  233. x += stride;
  234. }
  235. }
  236. int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
  237. int point_transform, int ilv)
  238. {
  239. int i, t = 0;
  240. uint8_t *zero, *last, *cur;
  241. JLSState *state;
  242. int off = 0, stride = 1, width, shift, ret = 0;
  243. zero = av_mallocz(s->picture_ptr->linesize[0]);
  244. last = zero;
  245. cur = s->picture_ptr->data[0];
  246. state = av_mallocz(sizeof(JLSState));
  247. /* initialize JPEG-LS state from JPEG parameters */
  248. state->near = near;
  249. state->bpp = (s->bits < 2) ? 2 : s->bits;
  250. state->maxval = s->maxval;
  251. state->T1 = s->t1;
  252. state->T2 = s->t2;
  253. state->T3 = s->t3;
  254. state->reset = s->reset;
  255. ff_jpegls_reset_coding_parameters(state, 0);
  256. ff_jpegls_init_state(state);
  257. if (s->bits <= 8)
  258. shift = point_transform + (8 - s->bits);
  259. else
  260. shift = point_transform + (16 - s->bits);
  261. av_dlog(s->avctx,
  262. "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
  263. "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
  264. s->width, s->height, state->near, state->maxval,
  265. state->T1, state->T2, state->T3,
  266. state->reset, state->limit, state->qbpp, state->range);
  267. av_dlog(s->avctx, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
  268. ilv, point_transform, s->bits, s->cur_scan);
  269. if (ilv == 0) { /* separate planes */
  270. if (s->cur_scan > s->nb_components) {
  271. ret = AVERROR_INVALIDDATA;
  272. goto end;
  273. }
  274. off = s->cur_scan - 1;
  275. stride = (s->nb_components > 1) ? 3 : 1;
  276. width = s->width * stride;
  277. cur += off;
  278. for (i = 0; i < s->height; i++) {
  279. if (s->bits <= 8) {
  280. ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
  281. t = last[0];
  282. } else {
  283. ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
  284. t = *((uint16_t *)last);
  285. }
  286. last = cur;
  287. cur += s->picture_ptr->linesize[0];
  288. if (s->restart_interval && !--s->restart_count) {
  289. align_get_bits(&s->gb);
  290. skip_bits(&s->gb, 16); /* skip RSTn */
  291. }
  292. }
  293. } else if (ilv == 1) { /* line interleaving */
  294. int j;
  295. int Rc[3] = { 0, 0, 0 };
  296. memset(cur, 0, s->picture_ptr->linesize[0]);
  297. width = s->width * 3;
  298. for (i = 0; i < s->height; i++) {
  299. for (j = 0; j < 3; j++) {
  300. ls_decode_line(state, s, last + j, cur + j,
  301. Rc[j], width, 3, j, 8);
  302. Rc[j] = last[j];
  303. if (s->restart_interval && !--s->restart_count) {
  304. align_get_bits(&s->gb);
  305. skip_bits(&s->gb, 16); /* skip RSTn */
  306. }
  307. }
  308. last = cur;
  309. cur += s->picture_ptr->linesize[0];
  310. }
  311. } else if (ilv == 2) { /* sample interleaving */
  312. avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
  313. ret = AVERROR_PATCHWELCOME;
  314. goto end;
  315. }
  316. if (shift) { /* we need to do point transform or normalize samples */
  317. int x, w;
  318. w = s->width * s->nb_components;
  319. if (s->bits <= 8) {
  320. uint8_t *src = s->picture_ptr->data[0];
  321. for (i = 0; i < s->height; i++) {
  322. for (x = off; x < w; x += stride)
  323. src[x] <<= shift;
  324. src += s->picture_ptr->linesize[0];
  325. }
  326. } else {
  327. uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
  328. for (i = 0; i < s->height; i++) {
  329. for (x = 0; x < w; x++)
  330. src[x] <<= shift;
  331. src += s->picture_ptr->linesize[0] / 2;
  332. }
  333. }
  334. }
  335. end:
  336. av_free(state);
  337. av_free(zero);
  338. return ret;
  339. }
  340. AVCodec ff_jpegls_decoder = {
  341. .name = "jpegls",
  342. .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .id = AV_CODEC_ID_JPEGLS,
  345. .priv_data_size = sizeof(MJpegDecodeContext),
  346. .init = ff_mjpeg_decode_init,
  347. .close = ff_mjpeg_decode_end,
  348. .decode = ff_mjpeg_decode_frame,
  349. .capabilities = CODEC_CAP_DR1,
  350. };