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.

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