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.

844 lines
25KB

  1. /*
  2. * JPEG-LS encoder and decoder
  3. * Copyright (c) 2003 Michael Niedermayer
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "golomb.h"
  21. /**
  22. * @file jpeg_ls.c
  23. * JPEG-LS encoder and decoder.
  24. */
  25. typedef struct JpeglsContext{
  26. AVCodecContext *avctx;
  27. AVFrame picture;
  28. }JpeglsContext;
  29. typedef struct JLSState{
  30. int T1, T2, T3;
  31. int A[367], B[367], C[365], N[367];
  32. int limit, reset, bpp, qbpp, maxval, range;
  33. int near, twonear;
  34. int run_index[3];
  35. }JLSState;
  36. static const uint8_t log2_run[32]={
  37. 0, 0, 0, 0, 1, 1, 1, 1,
  38. 2, 2, 2, 2, 3, 3, 3, 3,
  39. 4, 4, 5, 5, 6, 6, 7, 7,
  40. 8, 9,10,11,12,13,14,15
  41. };
  42. /*
  43. * Uncomment this to significantly speed up decoding of broken JPEG-LS
  44. * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
  45. *
  46. * There is no Golomb code with length >= 32 bits possible, so check and
  47. * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
  48. * on this errors.
  49. */
  50. //#define JLS_BROKEN
  51. /********** Functions for both encoder and decoder **********/
  52. /**
  53. * Calculate initial JPEG-LS parameters
  54. */
  55. static void ls_init_state(JLSState *state){
  56. int i;
  57. state->twonear = state->near * 2 + 1;
  58. state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
  59. // QBPP = ceil(log2(RANGE))
  60. for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
  61. if(state->bpp < 8)
  62. state->limit = 16 + 2 * state->bpp - state->qbpp;
  63. else
  64. state->limit = (4 * state->bpp) - state->qbpp;
  65. for(i = 0; i < 367; i++) {
  66. state->A[i] = (state->range + 32) >> 6;
  67. if(state->A[i] < 2)
  68. state->A[i] = 2;
  69. state->N[i] = 1;
  70. }
  71. }
  72. /**
  73. * Calculate quantized gradient value, used for context determination
  74. */
  75. static inline int quantize(JLSState *s, int v){ //FIXME optimize
  76. if(v==0) return 0;
  77. if(v < 0){
  78. if(v <= -s->T3) return -4;
  79. if(v <= -s->T2) return -3;
  80. if(v <= -s->T1) return -2;
  81. if(v < -s->near) return -1;
  82. return 0;
  83. }else{
  84. if(v <= s->near) return 0;
  85. if(v < s->T1) return 1;
  86. if(v < s->T2) return 2;
  87. if(v < s->T3) return 3;
  88. return 4;
  89. }
  90. }
  91. /**
  92. * Custom value clipping function used in T1, T2, T3 calculation
  93. */
  94. static inline int iso_clip(int v, int vmin, int vmax){
  95. if(v > vmax || v < vmin) return vmin;
  96. else return v;
  97. }
  98. /**
  99. * Calculate JPEG-LS codec values
  100. */
  101. static void reset_ls_coding_parameters(JLSState *s, int reset_all){
  102. const int basic_t1= 3;
  103. const int basic_t2= 7;
  104. const int basic_t3= 21;
  105. int factor;
  106. if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
  107. if(s->maxval >=128){
  108. factor= (FFMIN(s->maxval, 4095) + 128)>>8;
  109. if(s->T1==0 || reset_all)
  110. s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
  111. if(s->T2==0 || reset_all)
  112. s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
  113. if(s->T3==0 || reset_all)
  114. s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
  115. }else{
  116. factor= 256 / (s->maxval + 1);
  117. if(s->T1==0 || reset_all)
  118. s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
  119. if(s->T2==0 || reset_all)
  120. s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
  121. if(s->T3==0 || reset_all)
  122. s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
  123. }
  124. if(s->reset==0 || reset_all) s->reset= 64;
  125. // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
  126. }
  127. /********** Decoder-specific functions **********/
  128. /**
  129. * Decode LSE block with initialization parameters
  130. */
  131. static int decode_lse(MJpegDecodeContext *s)
  132. {
  133. int len, id;
  134. /* XXX: verify len field validity */
  135. len = get_bits(&s->gb, 16);
  136. id = get_bits(&s->gb, 8);
  137. switch(id){
  138. case 1:
  139. s->maxval= get_bits(&s->gb, 16);
  140. s->t1= get_bits(&s->gb, 16);
  141. s->t2= get_bits(&s->gb, 16);
  142. s->t3= get_bits(&s->gb, 16);
  143. s->reset= get_bits(&s->gb, 16);
  144. // reset_ls_coding_parameters(s, 0);
  145. //FIXME quant table?
  146. break;
  147. case 2:
  148. case 3:
  149. av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
  150. return -1;
  151. case 4:
  152. av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
  153. return -1;
  154. default:
  155. av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
  156. return -1;
  157. }
  158. // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
  159. return 0;
  160. }
  161. /**
  162. * Get context-dependent Golomb code, decode it and update context
  163. */
  164. static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
  165. int k, ret;
  166. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  167. #ifdef JLS_BROKEN
  168. if(!show_bits_long(gb, 32))return -1;
  169. #endif
  170. ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
  171. /* decode mapped error */
  172. if(ret & 1)
  173. ret = -((ret + 1) >> 1);
  174. else
  175. ret >>= 1;
  176. /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
  177. if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
  178. ret = -(ret + 1);
  179. state->A[Q] += ABS(ret);
  180. ret *= state->twonear;
  181. state->B[Q] += ret;
  182. if(state->N[Q] == state->reset) {
  183. state->A[Q] >>= 1;
  184. state->B[Q] >>= 1;
  185. state->N[Q] >>= 1;
  186. }
  187. state->N[Q]++;
  188. if(state->B[Q] <= -state->N[Q]) {
  189. state->B[Q] += state->N[Q];
  190. if(state->C[Q] > -128)
  191. state->C[Q]--;
  192. if(state->B[Q] <= -state->N[Q])
  193. state->B[Q] = -state->N[Q] + 1;
  194. }else if(state->B[Q] > 0){
  195. state->B[Q] -= state->N[Q];
  196. if(state->C[Q] < 127)
  197. state->C[Q]++;
  198. if(state->B[Q] > 0)
  199. state->B[Q] = 0;
  200. }
  201. return ret;
  202. }
  203. /**
  204. * Get Golomb code, decode it and update state for run termination
  205. */
  206. static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
  207. int k, ret, temp, map;
  208. int Q = 365 + RItype;
  209. if(!RItype)
  210. temp = state->A[Q];
  211. else
  212. temp = state->A[Q] + (state->N[Q] >> 1);
  213. for(k = 0; (state->N[Q] << k) < temp; k++);
  214. #ifdef JLS_BROKEN
  215. if(!show_bits_long(gb, 32))return -1;
  216. #endif
  217. ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
  218. /* decode mapped error */
  219. map = 0;
  220. if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
  221. map = 1;
  222. ret += RItype + map;
  223. if(ret & 1){
  224. ret = map - ((ret + 1) >> 1);
  225. state->B[Q]++;
  226. } else {
  227. ret = ret >> 1;
  228. }
  229. /* update state */
  230. state->A[Q] += ABS(ret) - RItype;
  231. ret *= state->twonear;
  232. if(state->N[Q] == state->reset){
  233. state->A[Q] >>=1;
  234. state->B[Q] >>=1;
  235. state->N[Q] >>=1;
  236. }
  237. state->N[Q]++;
  238. return ret;
  239. }
  240. /**
  241. * Decode one line of image
  242. */
  243. static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, uint8_t *last, uint8_t *dst, int last2, int w, int stride, int comp){
  244. int i, x = 0;
  245. int Ra, Rb, Rc, Rd;
  246. int D0, D1, D2;
  247. while(x < w) {
  248. int err, pred;
  249. /* compute gradients */
  250. Ra = x ? dst[x - stride] : last[x];
  251. Rb = last[x];
  252. Rc = x ? last[x - stride] : last2;
  253. Rd = (x >= w - stride) ? last[x] : last[x + stride];
  254. D0 = Rd - Rb;
  255. D1 = Rb - Rc;
  256. D2 = Rc - Ra;
  257. /* run mode */
  258. if((ABS(D0) <= state->near) && (ABS(D1) <= state->near) && (ABS(D2) <= state->near)) {
  259. int r;
  260. int RItype;
  261. /* decode full runs while available */
  262. while(get_bits1(&s->gb)) {
  263. int r;
  264. r = 1 << log2_run[state->run_index[comp]];
  265. if(x + r * stride > w) {
  266. r = (w - x) / stride;
  267. }
  268. for(i = 0; i < r; i++) {
  269. dst[x] = Ra;
  270. x += stride;
  271. }
  272. /* if EOL reached, we stop decoding */
  273. if(r != (1 << log2_run[state->run_index[comp]]))
  274. return;
  275. if(state->run_index[comp] < 31)
  276. state->run_index[comp]++;
  277. if(x + stride > w)
  278. return;
  279. }
  280. /* decode aborted run */
  281. r = log2_run[state->run_index[comp]];
  282. if(r)
  283. r = get_bits_long(&s->gb, r);
  284. for(i = 0; i < r; i++) {
  285. dst[x] = Ra;
  286. x += stride;
  287. }
  288. /* decode run termination value */
  289. Rb = last[x];
  290. RItype = (ABS(Ra - Rb) <= state->near) ? 1 : 0;
  291. err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
  292. if(state->run_index[comp])
  293. state->run_index[comp]--;
  294. if(state->near && RItype){
  295. pred = Ra + err;
  296. } else {
  297. if(Rb < Ra)
  298. pred = Rb - err;
  299. else
  300. pred = Rb + err;
  301. }
  302. if(state->near){
  303. if(pred < -state->near)
  304. pred += state->range * state->twonear;
  305. else if(pred > state->maxval + state->near)
  306. pred -= state->range * state->twonear;
  307. pred = clip(pred, 0, state->maxval);
  308. }
  309. dst[x] = pred;
  310. x += stride;
  311. } else { /* regular mode */
  312. int context, sign;
  313. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  314. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  315. if(context < 0){
  316. context = -context;
  317. sign = 1;
  318. }else{
  319. sign = 0;
  320. }
  321. if(sign){
  322. pred = clip(pred - state->C[context], 0, state->maxval);
  323. err = -ls_get_code_regular(&s->gb, state, context);
  324. } else {
  325. pred = clip(pred + state->C[context], 0, state->maxval);
  326. err = ls_get_code_regular(&s->gb, state, context);
  327. }
  328. /* we have to do something more for near-lossless coding */
  329. pred += err;
  330. if(state->near) {
  331. if(pred < -state->near)
  332. pred += state->range * state->twonear;
  333. else if(pred > state->maxval + state->near)
  334. pred -= state->range * state->twonear;
  335. pred = clip(pred, 0, state->maxval);
  336. }
  337. dst[x] = pred;
  338. x += stride;
  339. }
  340. }
  341. }
  342. static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
  343. int i, t = 0;
  344. uint8_t *zero, *last, *cur;
  345. JLSState *state;
  346. int off, stride, width;
  347. zero = av_mallocz(s->picture.linesize[0]);
  348. last = zero;
  349. cur = s->picture.data[0];
  350. state = av_mallocz(sizeof(JLSState));
  351. /* initialize JPEG-LS state from JPEG parameters */
  352. state->near = near;
  353. state->bpp = (s->bits < 2) ? 2 : s->bits;
  354. state->maxval = s->maxval;
  355. state->T1 = s->t1;
  356. state->T2 = s->t2;
  357. state->T3 = s->t3;
  358. state->reset = s->reset;
  359. reset_ls_coding_parameters(state, 0);
  360. ls_init_state(state);
  361. // 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);
  362. // 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);
  363. if(ilv == 0) { /* separate planes */
  364. off = s->cur_scan - 1;
  365. stride = (s->nb_components > 1) ? 3 : 1;
  366. width = s->width * stride;
  367. cur += off;
  368. for(i = 0; i < s->height; i++) {
  369. ls_decode_line(state, s, last, cur, t, width, stride, off);
  370. t = last[0];
  371. last = cur;
  372. cur += s->picture.linesize[0];
  373. if (s->restart_interval && !--s->restart_count) {
  374. align_get_bits(&s->gb);
  375. skip_bits(&s->gb, 16); /* skip RSTn */
  376. }
  377. }
  378. } else if(ilv == 1) { /* line interleaving */
  379. int j;
  380. int Rc[3] = {0, 0, 0};
  381. memset(cur, 0, s->picture.linesize[0]);
  382. width = s->width * 3;
  383. for(i = 0; i < s->height; i++) {
  384. for(j = 0; j < 3; j++) {
  385. ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j);
  386. Rc[j] = last[j];
  387. if (s->restart_interval && !--s->restart_count) {
  388. align_get_bits(&s->gb);
  389. skip_bits(&s->gb, 16); /* skip RSTn */
  390. }
  391. }
  392. last = cur;
  393. cur += s->picture.linesize[0];
  394. }
  395. } else if(ilv == 2) { /* sample interleaving */
  396. av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
  397. return -1;
  398. }
  399. av_free(state);
  400. av_free(zero);
  401. return 0;
  402. }
  403. #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
  404. /********** Encoder-specific functions **********/
  405. /**
  406. * Encode error from regular symbol
  407. */
  408. static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
  409. int k;
  410. int val;
  411. int map;
  412. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  413. map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
  414. if(err < 0)
  415. err += state->range;
  416. if(err >= ((state->range + 1) >> 1)) {
  417. err -= state->range;
  418. val = 2 * ABS(err) - 1 - map;
  419. } else
  420. val = 2 * err + map;
  421. set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
  422. state->A[Q] += ABS(err);
  423. state->B[Q] += err * state->twonear;
  424. if(state->N[Q] == state->reset) {
  425. state->A[Q] >>= 1;
  426. state->B[Q] >>= 1;
  427. state->N[Q] >>= 1;
  428. }
  429. state->N[Q]++;
  430. if(state->B[Q] <= -state->N[Q]) {
  431. state->B[Q] += state->N[Q];
  432. if(state->C[Q] > -128)
  433. state->C[Q]--;
  434. if(state->B[Q] <= -state->N[Q])
  435. state->B[Q] = -state->N[Q] + 1;
  436. }else if(state->B[Q] > 0){
  437. state->B[Q] -= state->N[Q];
  438. if(state->C[Q] < 127)
  439. state->C[Q]++;
  440. if(state->B[Q] > 0)
  441. state->B[Q] = 0;
  442. }
  443. }
  444. /**
  445. * Encode error from run termination
  446. */
  447. static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
  448. int k;
  449. int val, map;
  450. int Q = 365 + RItype;
  451. int temp;
  452. temp = state->A[Q];
  453. if(RItype)
  454. temp += state->N[Q] >> 1;
  455. for(k = 0; (state->N[Q] << k) < temp; k++);
  456. map = 0;
  457. if(!k && err && (2 * state->B[Q] < state->N[Q]))
  458. map = 1;
  459. if(err < 0)
  460. val = - (2 * err) - 1 - RItype + map;
  461. else
  462. val = 2 * err - RItype - map;
  463. set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
  464. if(err < 0)
  465. state->B[Q]++;
  466. state->A[Q] += (val + 1 - RItype) >> 1;
  467. if(state->N[Q] == state->reset) {
  468. state->A[Q] >>= 1;
  469. state->B[Q] >>= 1;
  470. state->N[Q] >>= 1;
  471. }
  472. state->N[Q]++;
  473. }
  474. /**
  475. * Encode run value as specified by JPEG-LS standard
  476. */
  477. static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
  478. while(run >= (1 << log2_run[state->run_index[comp]])){
  479. put_bits(pb, 1, 1);
  480. run -= 1 << log2_run[state->run_index[comp]];
  481. if(state->run_index[comp] < 31)
  482. state->run_index[comp]++;
  483. }
  484. /* if hit EOL, encode another full run, else encode aborted run */
  485. if(!trail && run) {
  486. put_bits(pb, 1, 1);
  487. }else if(trail){
  488. put_bits(pb, 1, 0);
  489. if(log2_run[state->run_index[comp]])
  490. put_bits(pb, log2_run[state->run_index[comp]], run);
  491. }
  492. }
  493. /**
  494. * Encode one line of image
  495. */
  496. static inline void ls_encode_line(JLSState *state, PutBitContext *pb, uint8_t *last, uint8_t *cur, int last2, int w, int stride, int comp){
  497. int x = 0;
  498. int Ra, Rb, Rc, Rd;
  499. int D0, D1, D2;
  500. while(x < w) {
  501. int err, pred, sign;
  502. /* compute gradients */
  503. Ra = x ? cur[x - stride] : last[x];
  504. Rb = last[x];
  505. Rc = x ? last[x - stride] : last2;
  506. Rd = (x >= w - stride) ? last[x] : last[x + stride];
  507. D0 = Rd - Rb;
  508. D1 = Rb - Rc;
  509. D2 = Rc - Ra;
  510. /* run mode */
  511. if((ABS(D0) <= state->near) && (ABS(D1) <= state->near) && (ABS(D2) <= state->near)) {
  512. int RUNval, RItype, run;
  513. run = 0;
  514. RUNval = Ra;
  515. while(x < w && (ABS(cur[x] - RUNval) <= state->near)){
  516. run++;
  517. cur[x] = Ra;
  518. x += stride;
  519. }
  520. ls_encode_run(state, pb, run, comp, x < w);
  521. if(x >= w)
  522. return;
  523. Rb = last[x];
  524. RItype = (ABS(Ra - Rb) <= state->near);
  525. pred = RItype ? Ra : Rb;
  526. err = cur[x] - pred;
  527. if(!RItype && Ra > Rb)
  528. err = -err;
  529. if(state->near){
  530. if(err > 0)
  531. err = (state->near + err) / state->twonear;
  532. else
  533. err = -(state->near - err) / state->twonear;
  534. if(RItype || (Rb >= Ra))
  535. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  536. else
  537. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  538. cur[x] = Ra;
  539. }
  540. if(err < 0)
  541. err += state->range;
  542. if(err >= ((state->range + 1) >> 1))
  543. err -= state->range;
  544. ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
  545. if(state->run_index[comp] > 0)
  546. state->run_index[comp]--;
  547. x += stride;
  548. } else { /* regular mode */
  549. int context;
  550. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  551. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  552. if(context < 0){
  553. context = -context;
  554. sign = 1;
  555. pred = clip(pred - state->C[context], 0, state->maxval);
  556. err = pred - cur[x];
  557. }else{
  558. sign = 0;
  559. pred = clip(pred + state->C[context], 0, state->maxval);
  560. err = cur[x] - pred;
  561. }
  562. if(state->near){
  563. if(err > 0)
  564. err = (state->near + err) / state->twonear;
  565. else
  566. err = -(state->near - err) / state->twonear;
  567. if(!sign)
  568. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  569. else
  570. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  571. cur[x] = Ra;
  572. }
  573. ls_encode_regular(state, pb, context, err);
  574. x += stride;
  575. }
  576. }
  577. }
  578. static void ls_store_lse(JLSState *state, PutBitContext *pb){
  579. /* Test if we have default params and don't need to store LSE */
  580. JLSState state2;
  581. memset(&state2, 0, sizeof(JLSState));
  582. state2.bpp = 8;
  583. state2.near = state->near;
  584. reset_ls_coding_parameters(&state2, 1);
  585. if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
  586. return;
  587. /* store LSE type 1 */
  588. put_marker(pb, LSE);
  589. put_bits(pb, 16, 13);
  590. put_bits(pb, 8, 1);
  591. put_bits(pb, 16, state->maxval);
  592. put_bits(pb, 16, state->T1);
  593. put_bits(pb, 16, state->T2);
  594. put_bits(pb, 16, state->T3);
  595. put_bits(pb, 16, state->reset);
  596. }
  597. static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  598. JpeglsContext * const s = avctx->priv_data;
  599. AVFrame *pict = data;
  600. AVFrame * const p= (AVFrame*)&s->picture;
  601. const int near = avctx->prediction_method;
  602. PutBitContext pb, pb2;
  603. GetBitContext gb;
  604. uint8_t *buf2, *zero, *cur, *last;
  605. JLSState *state;
  606. int i, size;
  607. int comps;
  608. buf2 = av_malloc(buf_size);
  609. init_put_bits(&pb, buf, buf_size);
  610. init_put_bits(&pb2, buf2, buf_size);
  611. *p = *pict;
  612. p->pict_type= FF_I_TYPE;
  613. p->key_frame= 1;
  614. comps = (avctx->pix_fmt == PIX_FMT_GRAY8) ? 1 : 3;
  615. /* write our own JPEG header, can't use mjpeg_picture_header */
  616. put_marker(&pb, SOI);
  617. put_marker(&pb, SOF48);
  618. put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
  619. put_bits(&pb, 8, 8); // bpp
  620. put_bits(&pb, 16, avctx->height);
  621. put_bits(&pb, 16, avctx->width);
  622. put_bits(&pb, 8, comps); // components
  623. for(i = 1; i <= comps; i++) {
  624. put_bits(&pb, 8, i); // component ID
  625. put_bits(&pb, 8, 0x11); // subsampling: none
  626. put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
  627. }
  628. put_marker(&pb, SOS);
  629. put_bits(&pb, 16, 6 + comps * 2);
  630. put_bits(&pb, 8, comps);
  631. for(i = 1; i <= comps; i++) {
  632. put_bits(&pb, 8, i); // component ID
  633. put_bits(&pb, 8, 0); // mapping index: none
  634. }
  635. put_bits(&pb, 8, near);
  636. put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
  637. put_bits(&pb, 8, 0); // point transform: none
  638. state = av_mallocz(sizeof(JLSState));
  639. /* initialize JPEG-LS state from JPEG parameters */
  640. state->near = near;
  641. state->bpp = 8;
  642. reset_ls_coding_parameters(state, 0);
  643. ls_init_state(state);
  644. ls_store_lse(state, &pb);
  645. zero = av_mallocz(p->linesize[0]);
  646. last = zero;
  647. cur = p->data[0];
  648. if(avctx->pix_fmt == PIX_FMT_GRAY8){
  649. int t = 0;
  650. for(i = 0; i < avctx->height; i++) {
  651. ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0);
  652. t = last[0];
  653. last = cur;
  654. cur += p->linesize[0];
  655. }
  656. }else if(avctx->pix_fmt == PIX_FMT_RGB24){
  657. int j, width;
  658. int Rc[3] = {0, 0, 0};
  659. width = avctx->width * 3;
  660. for(i = 0; i < avctx->height; i++) {
  661. for(j = 0; j < 3; j++) {
  662. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
  663. Rc[j] = last[j];
  664. }
  665. last = cur;
  666. cur += s->picture.linesize[0];
  667. }
  668. }else if(avctx->pix_fmt == PIX_FMT_BGR24){
  669. int j, width;
  670. int Rc[3] = {0, 0, 0};
  671. width = avctx->width * 3;
  672. for(i = 0; i < avctx->height; i++) {
  673. for(j = 2; j >= 0; j--) {
  674. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
  675. Rc[j] = last[j];
  676. }
  677. last = cur;
  678. cur += s->picture.linesize[0];
  679. }
  680. }
  681. av_free(zero);
  682. av_free(state);
  683. flush_put_bits(&pb2);
  684. /* do escape coding */
  685. size = put_bits_count(&pb2) >> 3;
  686. init_get_bits(&gb, buf2, size);
  687. while(get_bits_count(&gb) < size * 8){
  688. int v;
  689. v = get_bits(&gb, 8);
  690. put_bits(&pb, 8, v);
  691. if(v == 0xFF){
  692. v = get_bits(&gb, 7);
  693. put_bits(&pb, 8, v);
  694. }
  695. }
  696. align_put_bits(&pb);
  697. av_free(buf2);
  698. /* End of image */
  699. put_marker(&pb, EOI);
  700. flush_put_bits(&pb);
  701. emms_c();
  702. return put_bits_count(&pb) >> 3;
  703. }
  704. static int encode_init_ls(AVCodecContext *ctx) {
  705. JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
  706. c->avctx = ctx;
  707. ctx->coded_frame = &c->picture;
  708. if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
  709. av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
  710. return -1;
  711. }
  712. return 0;
  713. }
  714. AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
  715. "jpegls",
  716. CODEC_TYPE_VIDEO,
  717. CODEC_ID_JPEGLS,
  718. sizeof(JpeglsContext),
  719. encode_init_ls,
  720. encode_picture_ls,
  721. NULL,
  722. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, -1},
  723. };
  724. #endif