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.

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