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.

378 lines
11KB

  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 len, id;
  49. /* XXX: verify len field validity */
  50. len = get_bits(&s->gb, 16);
  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 -1;
  66. case 4:
  67. av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
  68. return -1;
  69. default:
  70. av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
  71. return -1;
  72. }
  73. // av_log(s->avctx, AV_LOG_DEBUG, "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. int k, ret;
  81. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  82. #ifdef JLS_BROKEN
  83. if(!show_bits_long(gb, 32))return -1;
  84. #endif
  85. ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
  86. /* decode mapped error */
  87. if(ret & 1)
  88. ret = -((ret + 1) >> 1);
  89. else
  90. ret >>= 1;
  91. /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
  92. if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
  93. ret = -(ret + 1);
  94. ret= ff_jpegls_update_state_regular(state, Q, ret);
  95. return ret;
  96. }
  97. /**
  98. * Get Golomb code, decode it and update state for run termination
  99. */
  100. static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
  101. int k, ret, temp, map;
  102. int Q = 365 + RItype;
  103. temp= state->A[Q];
  104. if(RItype)
  105. temp += state->N[Q] >> 1;
  106. for(k = 0; (state->N[Q] << k) < temp; k++);
  107. #ifdef JLS_BROKEN
  108. if(!show_bits_long(gb, 32))return -1;
  109. #endif
  110. ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
  111. /* decode mapped error */
  112. map = 0;
  113. if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
  114. map = 1;
  115. ret += RItype + map;
  116. if(ret & 1){
  117. ret = map - ((ret + 1) >> 1);
  118. state->B[Q]++;
  119. } else {
  120. ret = ret >> 1;
  121. }
  122. /* update state */
  123. state->A[Q] += FFABS(ret) - RItype;
  124. ret *= state->twonear;
  125. ff_jpegls_downscale_state(state, Q);
  126. return ret;
  127. }
  128. /**
  129. * Decode one line of image
  130. */
  131. static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
  132. int i, x = 0;
  133. int Ra, Rb, Rc, Rd;
  134. int D0, D1, D2;
  135. while(x < w) {
  136. int err, pred;
  137. /* compute gradients */
  138. Ra = x ? R(dst, x - stride) : R(last, x);
  139. Rb = R(last, x);
  140. Rc = x ? R(last, x - stride) : last2;
  141. Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
  142. D0 = Rd - Rb;
  143. D1 = Rb - Rc;
  144. D2 = Rc - Ra;
  145. /* run mode */
  146. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  147. int r;
  148. int RItype;
  149. /* decode full runs while available */
  150. while(get_bits1(&s->gb)) {
  151. int r;
  152. r = 1 << ff_log2_run[state->run_index[comp]];
  153. if(x + r * stride > w) {
  154. r = (w - x) / stride;
  155. }
  156. for(i = 0; i < r; i++) {
  157. W(dst, x, Ra);
  158. x += stride;
  159. }
  160. /* if EOL reached, we stop decoding */
  161. if(r != (1 << ff_log2_run[state->run_index[comp]]))
  162. return;
  163. if(state->run_index[comp] < 31)
  164. state->run_index[comp]++;
  165. if(x + stride > w)
  166. return;
  167. }
  168. /* decode aborted run */
  169. r = ff_log2_run[state->run_index[comp]];
  170. if(r)
  171. r = get_bits_long(&s->gb, r);
  172. for(i = 0; i < r; i++) {
  173. W(dst, x, Ra);
  174. x += stride;
  175. }
  176. /* decode run termination value */
  177. Rb = R(last, x);
  178. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  179. err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
  180. if(state->run_index[comp])
  181. state->run_index[comp]--;
  182. if(state->near && RItype){
  183. pred = Ra + err;
  184. } else {
  185. if(Rb < Ra)
  186. pred = Rb - err;
  187. else
  188. pred = Rb + err;
  189. }
  190. } else { /* regular mode */
  191. int context, sign;
  192. context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
  193. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  194. if(context < 0){
  195. context = -context;
  196. sign = 1;
  197. }else{
  198. sign = 0;
  199. }
  200. if(sign){
  201. pred = av_clip(pred - state->C[context], 0, state->maxval);
  202. err = -ls_get_code_regular(&s->gb, state, context);
  203. } else {
  204. pred = av_clip(pred + state->C[context], 0, state->maxval);
  205. err = ls_get_code_regular(&s->gb, state, context);
  206. }
  207. /* we have to do something more for near-lossless coding */
  208. pred += err;
  209. }
  210. if(state->near){
  211. if(pred < -state->near)
  212. pred += state->range * state->twonear;
  213. else if(pred > state->maxval + state->near)
  214. pred -= state->range * state->twonear;
  215. pred = av_clip(pred, 0, state->maxval);
  216. }
  217. pred &= state->maxval;
  218. W(dst, x, pred);
  219. x += stride;
  220. }
  221. }
  222. int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
  223. int i, t = 0;
  224. uint8_t *zero, *last, *cur;
  225. JLSState *state;
  226. int off = 0, stride = 1, width, shift;
  227. zero = av_mallocz(s->picture.linesize[0]);
  228. last = zero;
  229. cur = s->picture.data[0];
  230. state = av_mallocz(sizeof(JLSState));
  231. /* initialize JPEG-LS state from JPEG parameters */
  232. state->near = near;
  233. state->bpp = (s->bits < 2) ? 2 : s->bits;
  234. state->maxval = s->maxval;
  235. state->T1 = s->t1;
  236. state->T2 = s->t2;
  237. state->T3 = s->t3;
  238. state->reset = s->reset;
  239. ff_jpegls_reset_coding_parameters(state, 0);
  240. ff_jpegls_init_state(state);
  241. if(s->bits <= 8)
  242. shift = point_transform + (8 - s->bits);
  243. else
  244. shift = point_transform + (16 - s->bits);
  245. // av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
  246. // av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
  247. if(ilv == 0) { /* separate planes */
  248. off = s->cur_scan - 1;
  249. stride = (s->nb_components > 1) ? 3 : 1;
  250. width = s->width * stride;
  251. cur += off;
  252. for(i = 0; i < s->height; i++) {
  253. if(s->bits <= 8){
  254. ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
  255. t = last[0];
  256. }else{
  257. ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
  258. t = *((uint16_t*)last);
  259. }
  260. last = cur;
  261. cur += s->picture.linesize[0];
  262. if (s->restart_interval && !--s->restart_count) {
  263. align_get_bits(&s->gb);
  264. skip_bits(&s->gb, 16); /* skip RSTn */
  265. }
  266. }
  267. } else if(ilv == 1) { /* line interleaving */
  268. int j;
  269. int Rc[3] = {0, 0, 0};
  270. memset(cur, 0, s->picture.linesize[0]);
  271. width = s->width * 3;
  272. for(i = 0; i < s->height; i++) {
  273. for(j = 0; j < 3; j++) {
  274. ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
  275. Rc[j] = last[j];
  276. if (s->restart_interval && !--s->restart_count) {
  277. align_get_bits(&s->gb);
  278. skip_bits(&s->gb, 16); /* skip RSTn */
  279. }
  280. }
  281. last = cur;
  282. cur += s->picture.linesize[0];
  283. }
  284. } else if(ilv == 2) { /* sample interleaving */
  285. av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
  286. av_free(state);
  287. av_free(zero);
  288. return -1;
  289. }
  290. if(shift){ /* we need to do point transform or normalize samples */
  291. int x, w;
  292. w = s->width * s->nb_components;
  293. if(s->bits <= 8){
  294. uint8_t *src = s->picture.data[0];
  295. for(i = 0; i < s->height; i++){
  296. for(x = off; x < w; x+= stride){
  297. src[x] <<= shift;
  298. }
  299. src += s->picture.linesize[0];
  300. }
  301. }else{
  302. uint16_t *src = (uint16_t*) s->picture.data[0];
  303. for(i = 0; i < s->height; i++){
  304. for(x = 0; x < w; x++){
  305. src[x] <<= shift;
  306. }
  307. src += s->picture.linesize[0]/2;
  308. }
  309. }
  310. }
  311. av_free(state);
  312. av_free(zero);
  313. return 0;
  314. }
  315. AVCodec jpegls_decoder = {
  316. "jpegls",
  317. AVMEDIA_TYPE_VIDEO,
  318. CODEC_ID_JPEGLS,
  319. sizeof(MJpegDecodeContext),
  320. ff_mjpeg_decode_init,
  321. NULL,
  322. ff_mjpeg_decode_end,
  323. ff_mjpeg_decode_frame,
  324. CODEC_CAP_DR1,
  325. .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
  326. };