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.

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