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.

1109 lines
33KB

  1. /*
  2. * JPEG-LS encoder and 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. #include "golomb.h"
  23. /**
  24. * @file jpeg_ls.c
  25. * JPEG-LS encoder and decoder.
  26. */
  27. typedef struct JpeglsContext{
  28. AVCodecContext *avctx;
  29. AVFrame picture;
  30. }JpeglsContext;
  31. typedef struct JLSState{
  32. int T1, T2, T3;
  33. int A[367], B[367], C[365], N[367];
  34. int limit, reset, bpp, qbpp, maxval, range;
  35. int near, twonear;
  36. int run_index[3];
  37. }JLSState;
  38. static const uint8_t log2_run[32]={
  39. 0, 0, 0, 0, 1, 1, 1, 1,
  40. 2, 2, 2, 2, 3, 3, 3, 3,
  41. 4, 4, 5, 5, 6, 6, 7, 7,
  42. 8, 9,10,11,12,13,14,15
  43. };
  44. /*
  45. * Uncomment this to significantly speed up decoding of broken JPEG-LS
  46. * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
  47. *
  48. * There is no Golomb code with length >= 32 bits possible, so check and
  49. * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
  50. * on this errors.
  51. */
  52. //#define JLS_BROKEN
  53. /********** Functions for both encoder and decoder **********/
  54. /**
  55. * Calculate initial JPEG-LS parameters
  56. */
  57. static void ls_init_state(JLSState *state){
  58. int i;
  59. state->twonear = state->near * 2 + 1;
  60. state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
  61. // QBPP = ceil(log2(RANGE))
  62. for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
  63. if(state->bpp < 8)
  64. state->limit = 16 + 2 * state->bpp - state->qbpp;
  65. else
  66. state->limit = (4 * state->bpp) - state->qbpp;
  67. for(i = 0; i < 367; i++) {
  68. state->A[i] = (state->range + 32) >> 6;
  69. if(state->A[i] < 2)
  70. state->A[i] = 2;
  71. state->N[i] = 1;
  72. }
  73. }
  74. /**
  75. * Calculate quantized gradient value, used for context determination
  76. */
  77. static inline int quantize(JLSState *s, int v){ //FIXME optimize
  78. if(v==0) return 0;
  79. if(v < 0){
  80. if(v <= -s->T3) return -4;
  81. if(v <= -s->T2) return -3;
  82. if(v <= -s->T1) return -2;
  83. if(v < -s->near) return -1;
  84. return 0;
  85. }else{
  86. if(v <= s->near) return 0;
  87. if(v < s->T1) return 1;
  88. if(v < s->T2) return 2;
  89. if(v < s->T3) return 3;
  90. return 4;
  91. }
  92. }
  93. /**
  94. * Custom value clipping function used in T1, T2, T3 calculation
  95. */
  96. static inline int iso_clip(int v, int vmin, int vmax){
  97. if(v > vmax || v < vmin) return vmin;
  98. else return v;
  99. }
  100. /**
  101. * Calculate JPEG-LS codec values
  102. */
  103. static void reset_ls_coding_parameters(JLSState *s, int reset_all){
  104. const int basic_t1= 3;
  105. const int basic_t2= 7;
  106. const int basic_t3= 21;
  107. int factor;
  108. if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
  109. if(s->maxval >=128){
  110. factor= (FFMIN(s->maxval, 4095) + 128)>>8;
  111. if(s->T1==0 || reset_all)
  112. s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
  113. if(s->T2==0 || reset_all)
  114. s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
  115. if(s->T3==0 || reset_all)
  116. s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
  117. }else{
  118. factor= 256 / (s->maxval + 1);
  119. if(s->T1==0 || reset_all)
  120. s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
  121. if(s->T2==0 || reset_all)
  122. s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
  123. if(s->T3==0 || reset_all)
  124. s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
  125. }
  126. if(s->reset==0 || reset_all) s->reset= 64;
  127. // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
  128. }
  129. /********** Decoder-specific functions **********/
  130. /**
  131. * Decode LSE block with initialization parameters
  132. */
  133. static int decode_lse(MJpegDecodeContext *s)
  134. {
  135. int len, id;
  136. /* XXX: verify len field validity */
  137. len = get_bits(&s->gb, 16);
  138. id = get_bits(&s->gb, 8);
  139. switch(id){
  140. case 1:
  141. s->maxval= get_bits(&s->gb, 16);
  142. s->t1= get_bits(&s->gb, 16);
  143. s->t2= get_bits(&s->gb, 16);
  144. s->t3= get_bits(&s->gb, 16);
  145. s->reset= get_bits(&s->gb, 16);
  146. // reset_ls_coding_parameters(s, 0);
  147. //FIXME quant table?
  148. break;
  149. case 2:
  150. case 3:
  151. av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
  152. return -1;
  153. case 4:
  154. av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
  155. return -1;
  156. default:
  157. av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
  158. return -1;
  159. }
  160. // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
  161. return 0;
  162. }
  163. /**
  164. * Get context-dependent Golomb code, decode it and update context
  165. */
  166. static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
  167. int k, ret;
  168. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  169. #ifdef JLS_BROKEN
  170. if(!show_bits_long(gb, 32))return -1;
  171. #endif
  172. ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
  173. /* decode mapped error */
  174. if(ret & 1)
  175. ret = -((ret + 1) >> 1);
  176. else
  177. ret >>= 1;
  178. /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
  179. if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
  180. ret = -(ret + 1);
  181. state->A[Q] += FFABS(ret);
  182. ret *= state->twonear;
  183. state->B[Q] += ret;
  184. if(state->N[Q] == state->reset) {
  185. state->A[Q] >>= 1;
  186. state->B[Q] >>= 1;
  187. state->N[Q] >>= 1;
  188. }
  189. state->N[Q]++;
  190. if(state->B[Q] <= -state->N[Q]) {
  191. state->B[Q] += state->N[Q];
  192. if(state->C[Q] > -128)
  193. state->C[Q]--;
  194. if(state->B[Q] <= -state->N[Q])
  195. state->B[Q] = -state->N[Q] + 1;
  196. }else if(state->B[Q] > 0){
  197. state->B[Q] -= state->N[Q];
  198. if(state->C[Q] < 127)
  199. state->C[Q]++;
  200. if(state->B[Q] > 0)
  201. state->B[Q] = 0;
  202. }
  203. return ret;
  204. }
  205. /**
  206. * Get Golomb code, decode it and update state for run termination
  207. */
  208. static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
  209. int k, ret, temp, map;
  210. int Q = 365 + RItype;
  211. if(!RItype)
  212. temp = state->A[Q];
  213. else
  214. temp = state->A[Q] + (state->N[Q] >> 1);
  215. for(k = 0; (state->N[Q] << k) < temp; k++);
  216. #ifdef JLS_BROKEN
  217. if(!show_bits_long(gb, 32))return -1;
  218. #endif
  219. ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
  220. /* decode mapped error */
  221. map = 0;
  222. if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
  223. map = 1;
  224. ret += RItype + map;
  225. if(ret & 1){
  226. ret = map - ((ret + 1) >> 1);
  227. state->B[Q]++;
  228. } else {
  229. ret = ret >> 1;
  230. }
  231. /* update state */
  232. state->A[Q] += FFABS(ret) - RItype;
  233. ret *= state->twonear;
  234. if(state->N[Q] == state->reset){
  235. state->A[Q] >>=1;
  236. state->B[Q] >>=1;
  237. state->N[Q] >>=1;
  238. }
  239. state->N[Q]++;
  240. return ret;
  241. }
  242. /**
  243. * Decode one line of image
  244. */
  245. static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, uint8_t *last, uint8_t *dst, int last2, int w, int stride, int comp){
  246. int i, x = 0;
  247. int Ra, Rb, Rc, Rd;
  248. int D0, D1, D2;
  249. while(x < w) {
  250. int err, pred;
  251. /* compute gradients */
  252. Ra = x ? dst[x - stride] : last[x];
  253. Rb = last[x];
  254. Rc = x ? last[x - stride] : last2;
  255. Rd = (x >= w - stride) ? last[x] : last[x + stride];
  256. D0 = Rd - Rb;
  257. D1 = Rb - Rc;
  258. D2 = Rc - Ra;
  259. /* run mode */
  260. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  261. int r;
  262. int RItype;
  263. /* decode full runs while available */
  264. while(get_bits1(&s->gb)) {
  265. int r;
  266. r = 1 << log2_run[state->run_index[comp]];
  267. if(x + r * stride > w) {
  268. r = (w - x) / stride;
  269. }
  270. for(i = 0; i < r; i++) {
  271. dst[x] = Ra;
  272. x += stride;
  273. }
  274. /* if EOL reached, we stop decoding */
  275. if(r != (1 << log2_run[state->run_index[comp]]))
  276. return;
  277. if(state->run_index[comp] < 31)
  278. state->run_index[comp]++;
  279. if(x + stride > w)
  280. return;
  281. }
  282. /* decode aborted run */
  283. r = log2_run[state->run_index[comp]];
  284. if(r)
  285. r = get_bits_long(&s->gb, r);
  286. for(i = 0; i < r; i++) {
  287. dst[x] = Ra;
  288. x += stride;
  289. }
  290. /* decode run termination value */
  291. Rb = last[x];
  292. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  293. err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
  294. if(state->run_index[comp])
  295. state->run_index[comp]--;
  296. if(state->near && RItype){
  297. pred = Ra + err;
  298. } else {
  299. if(Rb < Ra)
  300. pred = Rb - err;
  301. else
  302. pred = Rb + err;
  303. }
  304. if(state->near){
  305. if(pred < -state->near)
  306. pred += state->range * state->twonear;
  307. else if(pred > state->maxval + state->near)
  308. pred -= state->range * state->twonear;
  309. pred = clip(pred, 0, state->maxval);
  310. }
  311. dst[x] = pred;
  312. x += stride;
  313. } else { /* regular mode */
  314. int context, sign;
  315. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  316. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  317. if(context < 0){
  318. context = -context;
  319. sign = 1;
  320. }else{
  321. sign = 0;
  322. }
  323. if(sign){
  324. pred = clip(pred - state->C[context], 0, state->maxval);
  325. err = -ls_get_code_regular(&s->gb, state, context);
  326. } else {
  327. pred = clip(pred + state->C[context], 0, state->maxval);
  328. err = ls_get_code_regular(&s->gb, state, context);
  329. }
  330. /* we have to do something more for near-lossless coding */
  331. pred += err;
  332. if(state->near) {
  333. if(pred < -state->near)
  334. pred += state->range * state->twonear;
  335. else if(pred > state->maxval + state->near)
  336. pred -= state->range * state->twonear;
  337. pred = clip(pred, 0, state->maxval);
  338. }
  339. dst[x] = pred;
  340. x += stride;
  341. }
  342. }
  343. }
  344. /**
  345. * Decode one line of image - 16bpp version
  346. */
  347. static inline void ls_decode_line_16bpp(JLSState *state, MJpegDecodeContext *s, uint16_t *last, uint16_t *dst, int last2, int w, int stride, int comp){
  348. int i, x = 0;
  349. int Ra, Rb, Rc, Rd;
  350. int D0, D1, D2;
  351. while(x < w) {
  352. int err, pred;
  353. /* compute gradients */
  354. Ra = x ? dst[x - stride] : last[x];
  355. Rb = last[x];
  356. Rc = x ? last[x - stride] : last2;
  357. Rd = (x >= w - stride) ? last[x] : last[x + stride];
  358. D0 = Rd - Rb;
  359. D1 = Rb - Rc;
  360. D2 = Rc - Ra;
  361. /* run mode */
  362. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  363. int r;
  364. int RItype;
  365. /* decode full runs while available */
  366. while(get_bits1(&s->gb)) {
  367. int r;
  368. r = 1 << log2_run[state->run_index[comp]];
  369. if(x + r * stride > w) {
  370. r = (w - x) / stride;
  371. }
  372. for(i = 0; i < r; i++) {
  373. dst[x] = Ra;
  374. x += stride;
  375. }
  376. /* if EOL reached, we stop decoding */
  377. if(r != (1 << log2_run[state->run_index[comp]]))
  378. return;
  379. if(state->run_index[comp] < 31)
  380. state->run_index[comp]++;
  381. if(x + stride > w)
  382. return;
  383. }
  384. /* decode aborted run */
  385. r = log2_run[state->run_index[comp]];
  386. if(r)
  387. r = get_bits_long(&s->gb, r);
  388. for(i = 0; i < r; i++) {
  389. dst[x] = Ra;
  390. x += stride;
  391. }
  392. /* decode run termination value */
  393. Rb = last[x];
  394. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  395. err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
  396. if(state->run_index[comp])
  397. state->run_index[comp]--;
  398. if(state->near && RItype){
  399. pred = Ra + err;
  400. } else {
  401. if(Rb < Ra)
  402. pred = Rb - err;
  403. else
  404. pred = Rb + err;
  405. }
  406. if(state->near){
  407. if(pred < -state->near)
  408. pred += state->range * state->twonear;
  409. else if(pred > state->maxval + state->near)
  410. pred -= state->range * state->twonear;
  411. pred = clip(pred, 0, state->maxval);
  412. }
  413. dst[x] = pred;
  414. x += stride;
  415. } else { /* regular mode */
  416. int context, sign;
  417. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  418. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  419. if(context < 0){
  420. context = -context;
  421. sign = 1;
  422. }else{
  423. sign = 0;
  424. }
  425. if(sign){
  426. pred = clip(pred - state->C[context], 0, state->maxval);
  427. err = -ls_get_code_regular(&s->gb, state, context);
  428. } else {
  429. pred = clip(pred + state->C[context], 0, state->maxval);
  430. err = ls_get_code_regular(&s->gb, state, context);
  431. }
  432. /* we have to do something more for near-lossless coding */
  433. pred += err;
  434. if(state->near) {
  435. if(pred < -state->near)
  436. pred += state->range * state->twonear;
  437. else if(pred > state->maxval + state->near)
  438. pred -= state->range * state->twonear;
  439. pred = clip(pred, 0, state->maxval);
  440. }
  441. dst[x] = pred;
  442. x += stride;
  443. }
  444. }
  445. }
  446. static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
  447. int i, t = 0;
  448. uint8_t *zero, *last, *cur;
  449. JLSState *state;
  450. int off = 0, stride = 1, width, shift;
  451. zero = av_mallocz(s->picture.linesize[0]);
  452. last = zero;
  453. cur = s->picture.data[0];
  454. state = av_mallocz(sizeof(JLSState));
  455. /* initialize JPEG-LS state from JPEG parameters */
  456. state->near = near;
  457. state->bpp = (s->bits < 2) ? 2 : s->bits;
  458. state->maxval = s->maxval;
  459. state->T1 = s->t1;
  460. state->T2 = s->t2;
  461. state->T3 = s->t3;
  462. state->reset = s->reset;
  463. reset_ls_coding_parameters(state, 0);
  464. ls_init_state(state);
  465. if(s->bits <= 8)
  466. shift = point_transform + (8 - s->bits);
  467. else
  468. shift = point_transform + (16 - s->bits);
  469. // 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);
  470. // 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);
  471. if(ilv == 0) { /* separate planes */
  472. off = s->cur_scan - 1;
  473. stride = (s->nb_components > 1) ? 3 : 1;
  474. width = s->width * stride;
  475. cur += off;
  476. for(i = 0; i < s->height; i++) {
  477. if(s->bits <= 8){
  478. ls_decode_line(state, s, last, cur, t, width, stride, off);
  479. t = last[0];
  480. }else{
  481. ls_decode_line_16bpp(state, s, last, cur, t, width, stride, off);
  482. t = *((uint16_t*)last);
  483. }
  484. last = cur;
  485. cur += s->picture.linesize[0];
  486. if (s->restart_interval && !--s->restart_count) {
  487. align_get_bits(&s->gb);
  488. skip_bits(&s->gb, 16); /* skip RSTn */
  489. }
  490. }
  491. } else if(ilv == 1) { /* line interleaving */
  492. int j;
  493. int Rc[3] = {0, 0, 0};
  494. memset(cur, 0, s->picture.linesize[0]);
  495. width = s->width * 3;
  496. for(i = 0; i < s->height; i++) {
  497. for(j = 0; j < 3; j++) {
  498. ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j);
  499. Rc[j] = last[j];
  500. if (s->restart_interval && !--s->restart_count) {
  501. align_get_bits(&s->gb);
  502. skip_bits(&s->gb, 16); /* skip RSTn */
  503. }
  504. }
  505. last = cur;
  506. cur += s->picture.linesize[0];
  507. }
  508. } else if(ilv == 2) { /* sample interleaving */
  509. av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
  510. av_free(state);
  511. av_free(zero);
  512. return -1;
  513. }
  514. if(shift){ /* we need to do point transform or normalize samples */
  515. int x, w;
  516. w = s->width * s->nb_components;
  517. if(s->bits <= 8){
  518. uint8_t *src = s->picture.data[0];
  519. for(i = 0; i < s->height; i++){
  520. for(x = off; x < w; x+= stride){
  521. src[x] <<= shift;
  522. }
  523. src += s->picture.linesize[0];
  524. }
  525. }else{
  526. uint16_t *src = s->picture.data[0];
  527. for(i = 0; i < s->height; i++){
  528. for(x = 0; x < w; x++){
  529. src[x] <<= shift;
  530. }
  531. src += s->picture.linesize[0]/2;
  532. }
  533. }
  534. }
  535. av_free(state);
  536. av_free(zero);
  537. return 0;
  538. }
  539. #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
  540. /********** Encoder-specific functions **********/
  541. /**
  542. * Encode error from regular symbol
  543. */
  544. static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
  545. int k;
  546. int val;
  547. int map;
  548. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  549. map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
  550. if(err < 0)
  551. err += state->range;
  552. if(err >= ((state->range + 1) >> 1)) {
  553. err -= state->range;
  554. val = 2 * FFABS(err) - 1 - map;
  555. } else
  556. val = 2 * err + map;
  557. set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
  558. state->A[Q] += FFABS(err);
  559. state->B[Q] += err * state->twonear;
  560. if(state->N[Q] == state->reset) {
  561. state->A[Q] >>= 1;
  562. state->B[Q] >>= 1;
  563. state->N[Q] >>= 1;
  564. }
  565. state->N[Q]++;
  566. if(state->B[Q] <= -state->N[Q]) {
  567. state->B[Q] += state->N[Q];
  568. if(state->C[Q] > -128)
  569. state->C[Q]--;
  570. if(state->B[Q] <= -state->N[Q])
  571. state->B[Q] = -state->N[Q] + 1;
  572. }else if(state->B[Q] > 0){
  573. state->B[Q] -= state->N[Q];
  574. if(state->C[Q] < 127)
  575. state->C[Q]++;
  576. if(state->B[Q] > 0)
  577. state->B[Q] = 0;
  578. }
  579. }
  580. /**
  581. * Encode error from run termination
  582. */
  583. static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
  584. int k;
  585. int val, map;
  586. int Q = 365 + RItype;
  587. int temp;
  588. temp = state->A[Q];
  589. if(RItype)
  590. temp += state->N[Q] >> 1;
  591. for(k = 0; (state->N[Q] << k) < temp; k++);
  592. map = 0;
  593. if(!k && err && (2 * state->B[Q] < state->N[Q]))
  594. map = 1;
  595. if(err < 0)
  596. val = - (2 * err) - 1 - RItype + map;
  597. else
  598. val = 2 * err - RItype - map;
  599. set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
  600. if(err < 0)
  601. state->B[Q]++;
  602. state->A[Q] += (val + 1 - RItype) >> 1;
  603. if(state->N[Q] == state->reset) {
  604. state->A[Q] >>= 1;
  605. state->B[Q] >>= 1;
  606. state->N[Q] >>= 1;
  607. }
  608. state->N[Q]++;
  609. }
  610. /**
  611. * Encode run value as specified by JPEG-LS standard
  612. */
  613. static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
  614. while(run >= (1 << log2_run[state->run_index[comp]])){
  615. put_bits(pb, 1, 1);
  616. run -= 1 << log2_run[state->run_index[comp]];
  617. if(state->run_index[comp] < 31)
  618. state->run_index[comp]++;
  619. }
  620. /* if hit EOL, encode another full run, else encode aborted run */
  621. if(!trail && run) {
  622. put_bits(pb, 1, 1);
  623. }else if(trail){
  624. put_bits(pb, 1, 0);
  625. if(log2_run[state->run_index[comp]])
  626. put_bits(pb, log2_run[state->run_index[comp]], run);
  627. }
  628. }
  629. /**
  630. * Encode one line of image
  631. */
  632. static inline void ls_encode_line(JLSState *state, PutBitContext *pb, uint8_t *last, uint8_t *cur, int last2, int w, int stride, int comp){
  633. int x = 0;
  634. int Ra, Rb, Rc, Rd;
  635. int D0, D1, D2;
  636. while(x < w) {
  637. int err, pred, sign;
  638. /* compute gradients */
  639. Ra = x ? cur[x - stride] : last[x];
  640. Rb = last[x];
  641. Rc = x ? last[x - stride] : last2;
  642. Rd = (x >= w - stride) ? last[x] : last[x + stride];
  643. D0 = Rd - Rb;
  644. D1 = Rb - Rc;
  645. D2 = Rc - Ra;
  646. /* run mode */
  647. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  648. int RUNval, RItype, run;
  649. run = 0;
  650. RUNval = Ra;
  651. while(x < w && (FFABS(cur[x] - RUNval) <= state->near)){
  652. run++;
  653. cur[x] = Ra;
  654. x += stride;
  655. }
  656. ls_encode_run(state, pb, run, comp, x < w);
  657. if(x >= w)
  658. return;
  659. Rb = last[x];
  660. RItype = (FFABS(Ra - Rb) <= state->near);
  661. pred = RItype ? Ra : Rb;
  662. err = cur[x] - pred;
  663. if(!RItype && Ra > Rb)
  664. err = -err;
  665. if(state->near){
  666. if(err > 0)
  667. err = (state->near + err) / state->twonear;
  668. else
  669. err = -(state->near - err) / state->twonear;
  670. if(RItype || (Rb >= Ra))
  671. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  672. else
  673. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  674. cur[x] = Ra;
  675. }
  676. if(err < 0)
  677. err += state->range;
  678. if(err >= ((state->range + 1) >> 1))
  679. err -= state->range;
  680. ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
  681. if(state->run_index[comp] > 0)
  682. state->run_index[comp]--;
  683. x += stride;
  684. } else { /* regular mode */
  685. int context;
  686. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  687. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  688. if(context < 0){
  689. context = -context;
  690. sign = 1;
  691. pred = clip(pred - state->C[context], 0, state->maxval);
  692. err = pred - cur[x];
  693. }else{
  694. sign = 0;
  695. pred = clip(pred + state->C[context], 0, state->maxval);
  696. err = cur[x] - pred;
  697. }
  698. if(state->near){
  699. if(err > 0)
  700. err = (state->near + err) / state->twonear;
  701. else
  702. err = -(state->near - err) / state->twonear;
  703. if(!sign)
  704. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  705. else
  706. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  707. cur[x] = Ra;
  708. }
  709. ls_encode_regular(state, pb, context, err);
  710. x += stride;
  711. }
  712. }
  713. }
  714. /**
  715. * Encode one line of image with 16 bpp sample size
  716. */
  717. static inline void ls_encode_line_16bpp(JLSState *state, PutBitContext *pb, uint16_t *last, uint16_t *cur, int last2, int w, int stride, int comp){
  718. int x = 0;
  719. int Ra, Rb, Rc, Rd;
  720. int D0, D1, D2;
  721. while(x < w) {
  722. int err, pred, sign;
  723. /* compute gradients */
  724. Ra = x ? cur[x - stride] : last[x];
  725. Rb = last[x];
  726. Rc = x ? last[x - stride] : last2;
  727. Rd = (x >= w - stride) ? last[x] : last[x + stride];
  728. D0 = Rd - Rb;
  729. D1 = Rb - Rc;
  730. D2 = Rc - Ra;
  731. /* run mode */
  732. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  733. int RUNval, RItype, run;
  734. run = 0;
  735. RUNval = Ra;
  736. while(x < w && (FFABS(cur[x] - RUNval) <= state->near)){
  737. run++;
  738. cur[x] = Ra;
  739. x += stride;
  740. }
  741. ls_encode_run(state, pb, run, comp, x < w);
  742. if(x >= w)
  743. return;
  744. Rb = last[x];
  745. RItype = (FFABS(Ra - Rb) <= state->near);
  746. pred = RItype ? Ra : Rb;
  747. err = cur[x] - pred;
  748. if(!RItype && Ra > Rb)
  749. err = -err;
  750. if(state->near){
  751. if(err > 0)
  752. err = (state->near + err) / state->twonear;
  753. else
  754. err = -(state->near - err) / state->twonear;
  755. if(RItype || (Rb >= Ra))
  756. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  757. else
  758. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  759. cur[x] = Ra;
  760. }
  761. if(err < 0)
  762. err += state->range;
  763. if(err >= ((state->range + 1) >> 1))
  764. err -= state->range;
  765. ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
  766. if(state->run_index[comp] > 0)
  767. state->run_index[comp]--;
  768. x += stride;
  769. } else { /* regular mode */
  770. int context;
  771. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  772. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  773. if(context < 0){
  774. context = -context;
  775. sign = 1;
  776. pred = clip(pred - state->C[context], 0, state->maxval);
  777. err = pred - cur[x];
  778. }else{
  779. sign = 0;
  780. pred = clip(pred + state->C[context], 0, state->maxval);
  781. err = cur[x] - pred;
  782. }
  783. if(state->near){
  784. if(err > 0)
  785. err = (state->near + err) / state->twonear;
  786. else
  787. err = -(state->near - err) / state->twonear;
  788. if(!sign)
  789. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  790. else
  791. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  792. cur[x] = Ra;
  793. }
  794. ls_encode_regular(state, pb, context, err);
  795. x += stride;
  796. }
  797. }
  798. }
  799. static void ls_store_lse(JLSState *state, PutBitContext *pb){
  800. /* Test if we have default params and don't need to store LSE */
  801. JLSState state2;
  802. memset(&state2, 0, sizeof(JLSState));
  803. state2.bpp = state->bpp;
  804. state2.near = state->near;
  805. reset_ls_coding_parameters(&state2, 1);
  806. if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
  807. return;
  808. /* store LSE type 1 */
  809. put_marker(pb, LSE);
  810. put_bits(pb, 16, 13);
  811. put_bits(pb, 8, 1);
  812. put_bits(pb, 16, state->maxval);
  813. put_bits(pb, 16, state->T1);
  814. put_bits(pb, 16, state->T2);
  815. put_bits(pb, 16, state->T3);
  816. put_bits(pb, 16, state->reset);
  817. }
  818. static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  819. JpeglsContext * const s = avctx->priv_data;
  820. AVFrame *pict = data;
  821. AVFrame * const p= (AVFrame*)&s->picture;
  822. const int near = avctx->prediction_method;
  823. PutBitContext pb, pb2;
  824. GetBitContext gb;
  825. uint8_t *buf2, *zero, *cur, *last;
  826. JLSState *state;
  827. int i, size;
  828. int comps;
  829. buf2 = av_malloc(buf_size);
  830. init_put_bits(&pb, buf, buf_size);
  831. init_put_bits(&pb2, buf2, buf_size);
  832. *p = *pict;
  833. p->pict_type= FF_I_TYPE;
  834. p->key_frame= 1;
  835. if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
  836. comps = 1;
  837. else
  838. comps = 3;
  839. /* write our own JPEG header, can't use mjpeg_picture_header */
  840. put_marker(&pb, SOI);
  841. put_marker(&pb, SOF48);
  842. put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
  843. put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
  844. put_bits(&pb, 16, avctx->height);
  845. put_bits(&pb, 16, avctx->width);
  846. put_bits(&pb, 8, comps); // components
  847. for(i = 1; i <= comps; i++) {
  848. put_bits(&pb, 8, i); // component ID
  849. put_bits(&pb, 8, 0x11); // subsampling: none
  850. put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
  851. }
  852. put_marker(&pb, SOS);
  853. put_bits(&pb, 16, 6 + comps * 2);
  854. put_bits(&pb, 8, comps);
  855. for(i = 1; i <= comps; i++) {
  856. put_bits(&pb, 8, i); // component ID
  857. put_bits(&pb, 8, 0); // mapping index: none
  858. }
  859. put_bits(&pb, 8, near);
  860. put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
  861. put_bits(&pb, 8, 0); // point transform: none
  862. state = av_mallocz(sizeof(JLSState));
  863. /* initialize JPEG-LS state from JPEG parameters */
  864. state->near = near;
  865. state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
  866. reset_ls_coding_parameters(state, 0);
  867. ls_init_state(state);
  868. ls_store_lse(state, &pb);
  869. zero = av_mallocz(p->linesize[0]);
  870. last = zero;
  871. cur = p->data[0];
  872. if(avctx->pix_fmt == PIX_FMT_GRAY8){
  873. int t = 0;
  874. for(i = 0; i < avctx->height; i++) {
  875. ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0);
  876. t = last[0];
  877. last = cur;
  878. cur += p->linesize[0];
  879. }
  880. }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
  881. int t = 0;
  882. for(i = 0; i < avctx->height; i++) {
  883. ls_encode_line_16bpp(state, &pb2, last, cur, t, avctx->width, 1, 0);
  884. t = *((uint16_t*)last);
  885. last = cur;
  886. cur += p->linesize[0];
  887. }
  888. }else if(avctx->pix_fmt == PIX_FMT_RGB24){
  889. int j, width;
  890. int Rc[3] = {0, 0, 0};
  891. width = avctx->width * 3;
  892. for(i = 0; i < avctx->height; i++) {
  893. for(j = 0; j < 3; j++) {
  894. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
  895. Rc[j] = last[j];
  896. }
  897. last = cur;
  898. cur += s->picture.linesize[0];
  899. }
  900. }else if(avctx->pix_fmt == PIX_FMT_BGR24){
  901. int j, width;
  902. int Rc[3] = {0, 0, 0};
  903. width = avctx->width * 3;
  904. for(i = 0; i < avctx->height; i++) {
  905. for(j = 2; j >= 0; j--) {
  906. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
  907. Rc[j] = last[j];
  908. }
  909. last = cur;
  910. cur += s->picture.linesize[0];
  911. }
  912. }
  913. av_free(zero);
  914. av_free(state);
  915. flush_put_bits(&pb2);
  916. /* do escape coding */
  917. size = put_bits_count(&pb2) >> 3;
  918. init_get_bits(&gb, buf2, size);
  919. while(get_bits_count(&gb) < size * 8){
  920. int v;
  921. v = get_bits(&gb, 8);
  922. put_bits(&pb, 8, v);
  923. if(v == 0xFF){
  924. v = get_bits(&gb, 7);
  925. put_bits(&pb, 8, v);
  926. }
  927. }
  928. align_put_bits(&pb);
  929. av_free(buf2);
  930. /* End of image */
  931. put_marker(&pb, EOI);
  932. flush_put_bits(&pb);
  933. emms_c();
  934. return put_bits_count(&pb) >> 3;
  935. }
  936. static int encode_init_ls(AVCodecContext *ctx) {
  937. JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
  938. c->avctx = ctx;
  939. ctx->coded_frame = &c->picture;
  940. if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_GRAY16 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
  941. av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
  942. return -1;
  943. }
  944. return 0;
  945. }
  946. AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
  947. "jpegls",
  948. CODEC_TYPE_VIDEO,
  949. CODEC_ID_JPEGLS,
  950. sizeof(JpeglsContext),
  951. encode_init_ls,
  952. encode_picture_ls,
  953. NULL,
  954. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, -1},
  955. };
  956. #endif