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.

507 lines
16KB

  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
  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, FFmpeg 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. int tid, wt, maxtab, i, j;
  50. int len = get_bits(&s->gb, 16);
  51. id = get_bits(&s->gb, 8);
  52. switch (id) {
  53. case 1:
  54. if (len < 13)
  55. return AVERROR_INVALIDDATA;
  56. s->maxval = get_bits(&s->gb, 16);
  57. s->t1 = get_bits(&s->gb, 16);
  58. s->t2 = get_bits(&s->gb, 16);
  59. s->t3 = get_bits(&s->gb, 16);
  60. s->reset = get_bits(&s->gb, 16);
  61. if(s->avctx->debug & FF_DEBUG_PICT_INFO) {
  62. av_log(s->avctx, AV_LOG_DEBUG, "Coding parameters maxval:%d T1:%d T2:%d T3:%d reset:%d\n",
  63. s->maxval, s->t1, s->t2, s->t3, s->reset);
  64. }
  65. // ff_jpegls_reset_coding_parameters(s, 0);
  66. //FIXME quant table?
  67. break;
  68. case 2:
  69. s->palette_index = 0;
  70. case 3:
  71. tid= get_bits(&s->gb, 8);
  72. wt = get_bits(&s->gb, 8);
  73. if (len < 5)
  74. return AVERROR_INVALIDDATA;
  75. if (wt < 1 || wt > MAX_COMPONENTS) {
  76. avpriv_request_sample(s->avctx, "wt %d", wt);
  77. return AVERROR_PATCHWELCOME;
  78. }
  79. if (!s->maxval)
  80. maxtab = 255;
  81. else if ((5 + wt*(s->maxval+1)) < 65535)
  82. maxtab = s->maxval;
  83. else
  84. maxtab = 65530/wt - 1;
  85. if(s->avctx->debug & FF_DEBUG_PICT_INFO) {
  86. av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab);
  87. }
  88. if (maxtab >= 256) {
  89. avpriv_request_sample(s->avctx, ">8bit palette");
  90. return AVERROR_PATCHWELCOME;
  91. }
  92. maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index);
  93. if (s->palette_index > maxtab)
  94. return AVERROR_INVALIDDATA;
  95. if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) &&
  96. (s->picture_ptr->format == AV_PIX_FMT_GRAY8 || s->picture_ptr->format == AV_PIX_FMT_PAL8)) {
  97. uint32_t *pal = s->picture_ptr->data[1];
  98. s->picture_ptr->format =
  99. s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
  100. for (i=s->palette_index; i<=maxtab; i++) {
  101. pal[i] = 0;
  102. for (j=0; j<wt; j++) {
  103. pal[i] |= get_bits(&s->gb, 8) << (8*(wt-j-1));
  104. }
  105. }
  106. s->palette_index = i;
  107. }
  108. break;
  109. case 4:
  110. avpriv_request_sample(s->avctx, "oversize image");
  111. return AVERROR(ENOSYS);
  112. default:
  113. av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
  114. return AVERROR_INVALIDDATA;
  115. }
  116. av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
  117. return 0;
  118. }
  119. /**
  120. * Get context-dependent Golomb code, decode it and update context
  121. */
  122. static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
  123. {
  124. int k, ret;
  125. for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
  126. ;
  127. #ifdef JLS_BROKEN
  128. if (!show_bits_long(gb, 32))
  129. return -1;
  130. #endif
  131. ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
  132. /* decode mapped error */
  133. if (ret & 1)
  134. ret = -(ret + 1 >> 1);
  135. else
  136. ret >>= 1;
  137. /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
  138. if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
  139. ret = -(ret + 1);
  140. ret = ff_jpegls_update_state_regular(state, Q, ret);
  141. return ret;
  142. }
  143. /**
  144. * Get Golomb code, decode it and update state for run termination
  145. */
  146. static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
  147. int RItype, int limit_add)
  148. {
  149. int k, ret, temp, map;
  150. int Q = 365 + RItype;
  151. temp = state->A[Q];
  152. if (RItype)
  153. temp += state->N[Q] >> 1;
  154. for (k = 0; (state->N[Q] << k) < temp; k++)
  155. ;
  156. #ifdef JLS_BROKEN
  157. if (!show_bits_long(gb, 32))
  158. return -1;
  159. #endif
  160. ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
  161. state->qbpp);
  162. /* decode mapped error */
  163. map = 0;
  164. if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
  165. map = 1;
  166. ret += RItype + map;
  167. if (ret & 1) {
  168. ret = map - (ret + 1 >> 1);
  169. state->B[Q]++;
  170. } else {
  171. ret = ret >> 1;
  172. }
  173. if(FFABS(ret) > 0xFFFF)
  174. return -0x10000;
  175. /* update state */
  176. state->A[Q] += FFABS(ret) - RItype;
  177. ret *= state->twonear;
  178. ff_jpegls_downscale_state(state, Q);
  179. return ret;
  180. }
  181. /**
  182. * Decode one line of image
  183. */
  184. static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
  185. void *last, void *dst, int last2, int w,
  186. int stride, int comp, int bits)
  187. {
  188. int i, x = 0;
  189. int Ra, Rb, Rc, Rd;
  190. int D0, D1, D2;
  191. while (x < w) {
  192. int err, pred;
  193. /* compute gradients */
  194. Ra = x ? R(dst, x - stride) : R(last, x);
  195. Rb = R(last, x);
  196. Rc = x ? R(last, x - stride) : last2;
  197. Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
  198. D0 = Rd - Rb;
  199. D1 = Rb - Rc;
  200. D2 = Rc - Ra;
  201. /* run mode */
  202. if ((FFABS(D0) <= state->near) &&
  203. (FFABS(D1) <= state->near) &&
  204. (FFABS(D2) <= state->near)) {
  205. int r;
  206. int RItype;
  207. /* decode full runs while available */
  208. while (get_bits1(&s->gb)) {
  209. int r;
  210. r = 1 << ff_log2_run[state->run_index[comp]];
  211. if (x + r * stride > w)
  212. r = (w - x) / stride;
  213. for (i = 0; i < r; i++) {
  214. W(dst, x, Ra);
  215. x += stride;
  216. }
  217. /* if EOL reached, we stop decoding */
  218. if (r != 1 << ff_log2_run[state->run_index[comp]])
  219. return;
  220. if (state->run_index[comp] < 31)
  221. state->run_index[comp]++;
  222. if (x + stride > w)
  223. return;
  224. }
  225. /* decode aborted run */
  226. r = ff_log2_run[state->run_index[comp]];
  227. if (r)
  228. r = get_bits_long(&s->gb, r);
  229. if (x + r * stride > w) {
  230. r = (w - x) / stride;
  231. }
  232. for (i = 0; i < r; i++) {
  233. W(dst, x, Ra);
  234. x += stride;
  235. }
  236. /* decode run termination value */
  237. Rb = R(last, x);
  238. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  239. err = ls_get_code_runterm(&s->gb, state, RItype,
  240. ff_log2_run[state->run_index[comp]]);
  241. if (state->run_index[comp])
  242. state->run_index[comp]--;
  243. if (state->near && RItype) {
  244. pred = Ra + err;
  245. } else {
  246. if (Rb < Ra)
  247. pred = Rb - err;
  248. else
  249. pred = Rb + err;
  250. }
  251. } else { /* regular mode */
  252. int context, sign;
  253. context = ff_jpegls_quantize(state, D0) * 81 +
  254. ff_jpegls_quantize(state, D1) * 9 +
  255. ff_jpegls_quantize(state, D2);
  256. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  257. if (context < 0) {
  258. context = -context;
  259. sign = 1;
  260. } else {
  261. sign = 0;
  262. }
  263. if (sign) {
  264. pred = av_clip(pred - state->C[context], 0, state->maxval);
  265. err = -ls_get_code_regular(&s->gb, state, context);
  266. } else {
  267. pred = av_clip(pred + state->C[context], 0, state->maxval);
  268. err = ls_get_code_regular(&s->gb, state, context);
  269. }
  270. /* we have to do something more for near-lossless coding */
  271. pred += err;
  272. }
  273. if (state->near) {
  274. if (pred < -state->near)
  275. pred += state->range * state->twonear;
  276. else if (pred > state->maxval + state->near)
  277. pred -= state->range * state->twonear;
  278. pred = av_clip(pred, 0, state->maxval);
  279. }
  280. pred &= state->maxval;
  281. W(dst, x, pred);
  282. x += stride;
  283. }
  284. }
  285. int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
  286. int point_transform, int ilv)
  287. {
  288. int i, t = 0;
  289. uint8_t *zero, *last, *cur;
  290. JLSState *state;
  291. int off = 0, stride = 1, width, shift, ret = 0;
  292. zero = av_mallocz(s->picture_ptr->linesize[0]);
  293. last = zero;
  294. cur = s->picture_ptr->data[0];
  295. state = av_mallocz(sizeof(JLSState));
  296. /* initialize JPEG-LS state from JPEG parameters */
  297. state->near = near;
  298. state->bpp = (s->bits < 2) ? 2 : s->bits;
  299. state->maxval = s->maxval;
  300. state->T1 = s->t1;
  301. state->T2 = s->t2;
  302. state->T3 = s->t3;
  303. state->reset = s->reset;
  304. ff_jpegls_reset_coding_parameters(state, 0);
  305. ff_jpegls_init_state(state);
  306. if (s->bits <= 8)
  307. shift = point_transform + (8 - s->bits);
  308. else
  309. shift = point_transform + (16 - s->bits);
  310. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  311. av_log(s->avctx, AV_LOG_DEBUG,
  312. "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
  313. "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
  314. s->width, s->height, state->near, state->maxval,
  315. state->T1, state->T2, state->T3,
  316. state->reset, state->limit, state->qbpp, state->range);
  317. av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
  318. ilv, point_transform, s->bits, s->cur_scan);
  319. }
  320. if (ilv == 0) { /* separate planes */
  321. if (s->cur_scan > s->nb_components) {
  322. ret = AVERROR_INVALIDDATA;
  323. goto end;
  324. }
  325. stride = (s->nb_components > 1) ? 3 : 1;
  326. off = av_clip(s->cur_scan - 1, 0, stride - 1);
  327. width = s->width * stride;
  328. cur += off;
  329. for (i = 0; i < s->height; i++) {
  330. if (s->bits <= 8) {
  331. ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
  332. t = last[0];
  333. } else {
  334. ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
  335. t = *((uint16_t *)last);
  336. }
  337. last = cur;
  338. cur += s->picture_ptr->linesize[0];
  339. if (s->restart_interval && !--s->restart_count) {
  340. align_get_bits(&s->gb);
  341. skip_bits(&s->gb, 16); /* skip RSTn */
  342. }
  343. }
  344. } else if (ilv == 1) { /* line interleaving */
  345. int j;
  346. int Rc[3] = { 0, 0, 0 };
  347. stride = (s->nb_components > 1) ? 3 : 1;
  348. memset(cur, 0, s->picture_ptr->linesize[0]);
  349. width = s->width * stride;
  350. for (i = 0; i < s->height; i++) {
  351. for (j = 0; j < stride; j++) {
  352. ls_decode_line(state, s, last + j, cur + j,
  353. Rc[j], width, stride, j, 8);
  354. Rc[j] = last[j];
  355. if (s->restart_interval && !--s->restart_count) {
  356. align_get_bits(&s->gb);
  357. skip_bits(&s->gb, 16); /* skip RSTn */
  358. }
  359. }
  360. last = cur;
  361. cur += s->picture_ptr->linesize[0];
  362. }
  363. } else if (ilv == 2) { /* sample interleaving */
  364. avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
  365. ret = AVERROR_PATCHWELCOME;
  366. goto end;
  367. }
  368. if (s->xfrm && s->nb_components == 3) {
  369. int x, w;
  370. w = s->width * s->nb_components;
  371. if (s->bits <= 8) {
  372. uint8_t *src = s->picture_ptr->data[0];
  373. for (i = 0; i < s->height; i++) {
  374. switch(s->xfrm) {
  375. case 1:
  376. for (x = off; x < w; x += 3) {
  377. src[x ] += src[x+1] + 128;
  378. src[x+2] += src[x+1] + 128;
  379. }
  380. break;
  381. case 2:
  382. for (x = off; x < w; x += 3) {
  383. src[x ] += src[x+1] + 128;
  384. src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
  385. }
  386. break;
  387. case 3:
  388. for (x = off; x < w; x += 3) {
  389. int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
  390. src[x+0] = src[x+2] + g + 128;
  391. src[x+2] = src[x+1] + g + 128;
  392. src[x+1] = g;
  393. }
  394. break;
  395. case 4:
  396. for (x = off; x < w; x += 3) {
  397. int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
  398. int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
  399. int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
  400. src[x+0] = av_clip_uint8(r);
  401. src[x+1] = av_clip_uint8(g);
  402. src[x+2] = av_clip_uint8(b);
  403. }
  404. break;
  405. }
  406. src += s->picture_ptr->linesize[0];
  407. }
  408. }else
  409. avpriv_report_missing_feature(s->avctx, "16bit xfrm");
  410. }
  411. if (shift) { /* we need to do point transform or normalize samples */
  412. int x, w;
  413. w = s->width * s->nb_components;
  414. if (s->bits <= 8) {
  415. uint8_t *src = s->picture_ptr->data[0];
  416. for (i = 0; i < s->height; i++) {
  417. for (x = off; x < w; x += stride)
  418. src[x] <<= shift;
  419. src += s->picture_ptr->linesize[0];
  420. }
  421. } else {
  422. uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
  423. for (i = 0; i < s->height; i++) {
  424. for (x = 0; x < w; x++)
  425. src[x] <<= shift;
  426. src += s->picture_ptr->linesize[0] / 2;
  427. }
  428. }
  429. }
  430. end:
  431. av_free(state);
  432. av_free(zero);
  433. return ret;
  434. }
  435. AVCodec ff_jpegls_decoder = {
  436. .name = "jpegls",
  437. .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
  438. .type = AVMEDIA_TYPE_VIDEO,
  439. .id = AV_CODEC_ID_JPEGLS,
  440. .priv_data_size = sizeof(MJpegDecodeContext),
  441. .init = ff_mjpeg_decode_init,
  442. .close = ff_mjpeg_decode_end,
  443. .decode = ff_mjpeg_decode_frame,
  444. .capabilities = CODEC_CAP_DR1,
  445. };