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.

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