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.

540 lines
17KB

  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. if (get_bits_left(&s->gb) <= 0)
  201. return;
  202. /* compute gradients */
  203. Ra = x ? R(dst, x - stride) : R(last, x);
  204. Rb = R(last, x);
  205. Rc = x ? R(last, x - stride) : last2;
  206. Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
  207. D0 = Rd - Rb;
  208. D1 = Rb - Rc;
  209. D2 = Rc - Ra;
  210. /* run mode */
  211. if ((FFABS(D0) <= state->near) &&
  212. (FFABS(D1) <= state->near) &&
  213. (FFABS(D2) <= state->near)) {
  214. int r;
  215. int RItype;
  216. /* decode full runs while available */
  217. while (get_bits1(&s->gb)) {
  218. int r;
  219. r = 1 << ff_log2_run[state->run_index[comp]];
  220. if (x + r * stride > w)
  221. r = (w - x) / stride;
  222. for (i = 0; i < r; i++) {
  223. W(dst, x, Ra);
  224. x += stride;
  225. }
  226. /* if EOL reached, we stop decoding */
  227. if (r != 1 << ff_log2_run[state->run_index[comp]])
  228. return;
  229. if (state->run_index[comp] < 31)
  230. state->run_index[comp]++;
  231. if (x + stride > w)
  232. return;
  233. }
  234. /* decode aborted run */
  235. r = ff_log2_run[state->run_index[comp]];
  236. if (r)
  237. r = get_bits_long(&s->gb, r);
  238. if (x + r * stride > w) {
  239. r = (w - x) / stride;
  240. }
  241. for (i = 0; i < r; i++) {
  242. W(dst, x, Ra);
  243. x += stride;
  244. }
  245. if (x >= w) {
  246. av_log(NULL, AV_LOG_ERROR, "run overflow\n");
  247. av_assert0(x <= w);
  248. return;
  249. }
  250. /* decode run termination value */
  251. Rb = R(last, x);
  252. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  253. err = ls_get_code_runterm(&s->gb, state, RItype,
  254. ff_log2_run[state->run_index[comp]]);
  255. if (state->run_index[comp])
  256. state->run_index[comp]--;
  257. if (state->near && RItype) {
  258. pred = Ra + err;
  259. } else {
  260. if (Rb < Ra)
  261. pred = Rb - err;
  262. else
  263. pred = Rb + err;
  264. }
  265. } else { /* regular mode */
  266. int context, sign;
  267. context = ff_jpegls_quantize(state, D0) * 81 +
  268. ff_jpegls_quantize(state, D1) * 9 +
  269. ff_jpegls_quantize(state, D2);
  270. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  271. if (context < 0) {
  272. context = -context;
  273. sign = 1;
  274. } else {
  275. sign = 0;
  276. }
  277. if (sign) {
  278. pred = av_clip(pred - state->C[context], 0, state->maxval);
  279. err = -ls_get_code_regular(&s->gb, state, context);
  280. } else {
  281. pred = av_clip(pred + state->C[context], 0, state->maxval);
  282. err = ls_get_code_regular(&s->gb, state, context);
  283. }
  284. /* we have to do something more for near-lossless coding */
  285. pred += err;
  286. }
  287. if (state->near) {
  288. if (pred < -state->near)
  289. pred += state->range * state->twonear;
  290. else if (pred > state->maxval + state->near)
  291. pred -= state->range * state->twonear;
  292. pred = av_clip(pred, 0, state->maxval);
  293. }
  294. pred &= state->maxval;
  295. W(dst, x, pred);
  296. x += stride;
  297. }
  298. }
  299. int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
  300. int point_transform, int ilv)
  301. {
  302. int i, t = 0;
  303. uint8_t *zero, *last, *cur;
  304. JLSState *state;
  305. int off = 0, stride = 1, width, shift, ret = 0;
  306. zero = av_mallocz(s->picture_ptr->linesize[0]);
  307. if (!zero)
  308. return AVERROR(ENOMEM);
  309. last = zero;
  310. cur = s->picture_ptr->data[0];
  311. state = av_mallocz(sizeof(JLSState));
  312. if (!state) {
  313. av_free(zero);
  314. return AVERROR(ENOMEM);
  315. }
  316. /* initialize JPEG-LS state from JPEG parameters */
  317. state->near = near;
  318. state->bpp = (s->bits < 2) ? 2 : s->bits;
  319. state->maxval = s->maxval;
  320. state->T1 = s->t1;
  321. state->T2 = s->t2;
  322. state->T3 = s->t3;
  323. state->reset = s->reset;
  324. ff_jpegls_reset_coding_parameters(state, 0);
  325. ff_jpegls_init_state(state);
  326. if (s->bits <= 8)
  327. shift = point_transform + (8 - s->bits);
  328. else
  329. shift = point_transform + (16 - s->bits);
  330. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  331. av_log(s->avctx, AV_LOG_DEBUG,
  332. "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
  333. "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
  334. s->width, s->height, state->near, state->maxval,
  335. state->T1, state->T2, state->T3,
  336. state->reset, state->limit, state->qbpp, state->range);
  337. av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
  338. ilv, point_transform, s->bits, s->cur_scan);
  339. }
  340. if (get_bits_left(&s->gb) < s->height) {
  341. ret = AVERROR_INVALIDDATA;
  342. goto end;
  343. }
  344. if (ilv == 0) { /* separate planes */
  345. if (s->cur_scan > s->nb_components) {
  346. ret = AVERROR_INVALIDDATA;
  347. goto end;
  348. }
  349. stride = (s->nb_components > 1) ? 3 : 1;
  350. off = av_clip(s->cur_scan - 1, 0, stride - 1);
  351. width = s->width * stride;
  352. cur += off;
  353. for (i = 0; i < s->height; i++) {
  354. if (s->bits <= 8) {
  355. ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
  356. t = last[0];
  357. } else {
  358. ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
  359. t = *((uint16_t *)last);
  360. }
  361. last = cur;
  362. cur += s->picture_ptr->linesize[0];
  363. if (s->restart_interval && !--s->restart_count) {
  364. align_get_bits(&s->gb);
  365. skip_bits(&s->gb, 16); /* skip RSTn */
  366. }
  367. }
  368. } else if (ilv == 1) { /* line interleaving */
  369. int j;
  370. int Rc[3] = { 0, 0, 0 };
  371. stride = (s->nb_components > 1) ? 3 : 1;
  372. memset(cur, 0, s->picture_ptr->linesize[0]);
  373. width = s->width * stride;
  374. for (i = 0; i < s->height; i++) {
  375. for (j = 0; j < stride; j++) {
  376. ls_decode_line(state, s, last + j, cur + j,
  377. Rc[j], width, stride, j, 8);
  378. Rc[j] = last[j];
  379. if (s->restart_interval && !--s->restart_count) {
  380. align_get_bits(&s->gb);
  381. skip_bits(&s->gb, 16); /* skip RSTn */
  382. }
  383. }
  384. last = cur;
  385. cur += s->picture_ptr->linesize[0];
  386. }
  387. } else if (ilv == 2) { /* sample interleaving */
  388. avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
  389. ret = AVERROR_PATCHWELCOME;
  390. goto end;
  391. } else { /* unknown interleaving */
  392. avpriv_report_missing_feature(s->avctx, "Unknown interleaved images");
  393. ret = AVERROR_PATCHWELCOME;
  394. goto end;
  395. }
  396. if (s->xfrm && s->nb_components == 3) {
  397. int x, w;
  398. w = s->width * s->nb_components;
  399. if (s->bits <= 8) {
  400. uint8_t *src = s->picture_ptr->data[0];
  401. for (i = 0; i < s->height; i++) {
  402. switch(s->xfrm) {
  403. case 1:
  404. for (x = off; x < w; x += 3) {
  405. src[x ] += src[x+1] + 128;
  406. src[x+2] += src[x+1] + 128;
  407. }
  408. break;
  409. case 2:
  410. for (x = off; x < w; x += 3) {
  411. src[x ] += src[x+1] + 128;
  412. src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
  413. }
  414. break;
  415. case 3:
  416. for (x = off; x < w; x += 3) {
  417. int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
  418. src[x+0] = src[x+2] + g + 128;
  419. src[x+2] = src[x+1] + g + 128;
  420. src[x+1] = g;
  421. }
  422. break;
  423. case 4:
  424. for (x = off; x < w; x += 3) {
  425. int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
  426. int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
  427. int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
  428. src[x+0] = av_clip_uint8(r);
  429. src[x+1] = av_clip_uint8(g);
  430. src[x+2] = av_clip_uint8(b);
  431. }
  432. break;
  433. }
  434. src += s->picture_ptr->linesize[0];
  435. }
  436. }else
  437. avpriv_report_missing_feature(s->avctx, "16bit xfrm");
  438. }
  439. if (shift) { /* we need to do point transform or normalize samples */
  440. int x, w;
  441. w = s->width * s->nb_components;
  442. if (s->bits <= 8) {
  443. uint8_t *src = s->picture_ptr->data[0];
  444. for (i = 0; i < s->height; i++) {
  445. for (x = off; x < w; x += stride)
  446. src[x] <<= shift;
  447. src += s->picture_ptr->linesize[0];
  448. }
  449. } else {
  450. uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
  451. for (i = 0; i < s->height; i++) {
  452. for (x = 0; x < w; x++)
  453. src[x] <<= shift;
  454. src += s->picture_ptr->linesize[0] / 2;
  455. }
  456. }
  457. }
  458. end:
  459. av_free(state);
  460. av_free(zero);
  461. return ret;
  462. }
  463. AVCodec ff_jpegls_decoder = {
  464. .name = "jpegls",
  465. .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
  466. .type = AVMEDIA_TYPE_VIDEO,
  467. .id = AV_CODEC_ID_JPEGLS,
  468. .priv_data_size = sizeof(MJpegDecodeContext),
  469. .init = ff_mjpeg_decode_init,
  470. .close = ff_mjpeg_decode_end,
  471. .decode = ff_mjpeg_decode_frame,
  472. .capabilities = AV_CODEC_CAP_DR1,
  473. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  474. };