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.

846 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. av_free(state);
  398. av_free(zero);
  399. return -1;
  400. }
  401. av_free(state);
  402. av_free(zero);
  403. return 0;
  404. }
  405. #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
  406. /********** Encoder-specific functions **********/
  407. /**
  408. * Encode error from regular symbol
  409. */
  410. static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
  411. int k;
  412. int val;
  413. int map;
  414. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  415. map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
  416. if(err < 0)
  417. err += state->range;
  418. if(err >= ((state->range + 1) >> 1)) {
  419. err -= state->range;
  420. val = 2 * ABS(err) - 1 - map;
  421. } else
  422. val = 2 * err + map;
  423. set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
  424. state->A[Q] += ABS(err);
  425. state->B[Q] += err * state->twonear;
  426. if(state->N[Q] == state->reset) {
  427. state->A[Q] >>= 1;
  428. state->B[Q] >>= 1;
  429. state->N[Q] >>= 1;
  430. }
  431. state->N[Q]++;
  432. if(state->B[Q] <= -state->N[Q]) {
  433. state->B[Q] += state->N[Q];
  434. if(state->C[Q] > -128)
  435. state->C[Q]--;
  436. if(state->B[Q] <= -state->N[Q])
  437. state->B[Q] = -state->N[Q] + 1;
  438. }else if(state->B[Q] > 0){
  439. state->B[Q] -= state->N[Q];
  440. if(state->C[Q] < 127)
  441. state->C[Q]++;
  442. if(state->B[Q] > 0)
  443. state->B[Q] = 0;
  444. }
  445. }
  446. /**
  447. * Encode error from run termination
  448. */
  449. static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
  450. int k;
  451. int val, map;
  452. int Q = 365 + RItype;
  453. int temp;
  454. temp = state->A[Q];
  455. if(RItype)
  456. temp += state->N[Q] >> 1;
  457. for(k = 0; (state->N[Q] << k) < temp; k++);
  458. map = 0;
  459. if(!k && err && (2 * state->B[Q] < state->N[Q]))
  460. map = 1;
  461. if(err < 0)
  462. val = - (2 * err) - 1 - RItype + map;
  463. else
  464. val = 2 * err - RItype - map;
  465. set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
  466. if(err < 0)
  467. state->B[Q]++;
  468. state->A[Q] += (val + 1 - RItype) >> 1;
  469. if(state->N[Q] == state->reset) {
  470. state->A[Q] >>= 1;
  471. state->B[Q] >>= 1;
  472. state->N[Q] >>= 1;
  473. }
  474. state->N[Q]++;
  475. }
  476. /**
  477. * Encode run value as specified by JPEG-LS standard
  478. */
  479. static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
  480. while(run >= (1 << log2_run[state->run_index[comp]])){
  481. put_bits(pb, 1, 1);
  482. run -= 1 << log2_run[state->run_index[comp]];
  483. if(state->run_index[comp] < 31)
  484. state->run_index[comp]++;
  485. }
  486. /* if hit EOL, encode another full run, else encode aborted run */
  487. if(!trail && run) {
  488. put_bits(pb, 1, 1);
  489. }else if(trail){
  490. put_bits(pb, 1, 0);
  491. if(log2_run[state->run_index[comp]])
  492. put_bits(pb, log2_run[state->run_index[comp]], run);
  493. }
  494. }
  495. /**
  496. * Encode one line of image
  497. */
  498. static inline void ls_encode_line(JLSState *state, PutBitContext *pb, uint8_t *last, uint8_t *cur, int last2, int w, int stride, int comp){
  499. int x = 0;
  500. int Ra, Rb, Rc, Rd;
  501. int D0, D1, D2;
  502. while(x < w) {
  503. int err, pred, sign;
  504. /* compute gradients */
  505. Ra = x ? cur[x - stride] : last[x];
  506. Rb = last[x];
  507. Rc = x ? last[x - stride] : last2;
  508. Rd = (x >= w - stride) ? last[x] : last[x + stride];
  509. D0 = Rd - Rb;
  510. D1 = Rb - Rc;
  511. D2 = Rc - Ra;
  512. /* run mode */
  513. if((ABS(D0) <= state->near) && (ABS(D1) <= state->near) && (ABS(D2) <= state->near)) {
  514. int RUNval, RItype, run;
  515. run = 0;
  516. RUNval = Ra;
  517. while(x < w && (ABS(cur[x] - RUNval) <= state->near)){
  518. run++;
  519. cur[x] = Ra;
  520. x += stride;
  521. }
  522. ls_encode_run(state, pb, run, comp, x < w);
  523. if(x >= w)
  524. return;
  525. Rb = last[x];
  526. RItype = (ABS(Ra - Rb) <= state->near);
  527. pred = RItype ? Ra : Rb;
  528. err = cur[x] - pred;
  529. if(!RItype && Ra > Rb)
  530. err = -err;
  531. if(state->near){
  532. if(err > 0)
  533. err = (state->near + err) / state->twonear;
  534. else
  535. err = -(state->near - err) / state->twonear;
  536. if(RItype || (Rb >= Ra))
  537. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  538. else
  539. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  540. cur[x] = Ra;
  541. }
  542. if(err < 0)
  543. err += state->range;
  544. if(err >= ((state->range + 1) >> 1))
  545. err -= state->range;
  546. ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
  547. if(state->run_index[comp] > 0)
  548. state->run_index[comp]--;
  549. x += stride;
  550. } else { /* regular mode */
  551. int context;
  552. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  553. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  554. if(context < 0){
  555. context = -context;
  556. sign = 1;
  557. pred = clip(pred - state->C[context], 0, state->maxval);
  558. err = pred - cur[x];
  559. }else{
  560. sign = 0;
  561. pred = clip(pred + state->C[context], 0, state->maxval);
  562. err = cur[x] - pred;
  563. }
  564. if(state->near){
  565. if(err > 0)
  566. err = (state->near + err) / state->twonear;
  567. else
  568. err = -(state->near - err) / state->twonear;
  569. if(!sign)
  570. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  571. else
  572. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  573. cur[x] = Ra;
  574. }
  575. ls_encode_regular(state, pb, context, err);
  576. x += stride;
  577. }
  578. }
  579. }
  580. static void ls_store_lse(JLSState *state, PutBitContext *pb){
  581. /* Test if we have default params and don't need to store LSE */
  582. JLSState state2;
  583. memset(&state2, 0, sizeof(JLSState));
  584. state2.bpp = 8;
  585. state2.near = state->near;
  586. reset_ls_coding_parameters(&state2, 1);
  587. if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
  588. return;
  589. /* store LSE type 1 */
  590. put_marker(pb, LSE);
  591. put_bits(pb, 16, 13);
  592. put_bits(pb, 8, 1);
  593. put_bits(pb, 16, state->maxval);
  594. put_bits(pb, 16, state->T1);
  595. put_bits(pb, 16, state->T2);
  596. put_bits(pb, 16, state->T3);
  597. put_bits(pb, 16, state->reset);
  598. }
  599. static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  600. JpeglsContext * const s = avctx->priv_data;
  601. AVFrame *pict = data;
  602. AVFrame * const p= (AVFrame*)&s->picture;
  603. const int near = avctx->prediction_method;
  604. PutBitContext pb, pb2;
  605. GetBitContext gb;
  606. uint8_t *buf2, *zero, *cur, *last;
  607. JLSState *state;
  608. int i, size;
  609. int comps;
  610. buf2 = av_malloc(buf_size);
  611. init_put_bits(&pb, buf, buf_size);
  612. init_put_bits(&pb2, buf2, buf_size);
  613. *p = *pict;
  614. p->pict_type= FF_I_TYPE;
  615. p->key_frame= 1;
  616. comps = (avctx->pix_fmt == PIX_FMT_GRAY8) ? 1 : 3;
  617. /* write our own JPEG header, can't use mjpeg_picture_header */
  618. put_marker(&pb, SOI);
  619. put_marker(&pb, SOF48);
  620. put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
  621. put_bits(&pb, 8, 8); // bpp
  622. put_bits(&pb, 16, avctx->height);
  623. put_bits(&pb, 16, avctx->width);
  624. put_bits(&pb, 8, comps); // components
  625. for(i = 1; i <= comps; i++) {
  626. put_bits(&pb, 8, i); // component ID
  627. put_bits(&pb, 8, 0x11); // subsampling: none
  628. put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
  629. }
  630. put_marker(&pb, SOS);
  631. put_bits(&pb, 16, 6 + comps * 2);
  632. put_bits(&pb, 8, comps);
  633. for(i = 1; i <= comps; i++) {
  634. put_bits(&pb, 8, i); // component ID
  635. put_bits(&pb, 8, 0); // mapping index: none
  636. }
  637. put_bits(&pb, 8, near);
  638. put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
  639. put_bits(&pb, 8, 0); // point transform: none
  640. state = av_mallocz(sizeof(JLSState));
  641. /* initialize JPEG-LS state from JPEG parameters */
  642. state->near = near;
  643. state->bpp = 8;
  644. reset_ls_coding_parameters(state, 0);
  645. ls_init_state(state);
  646. ls_store_lse(state, &pb);
  647. zero = av_mallocz(p->linesize[0]);
  648. last = zero;
  649. cur = p->data[0];
  650. if(avctx->pix_fmt == PIX_FMT_GRAY8){
  651. int t = 0;
  652. for(i = 0; i < avctx->height; i++) {
  653. ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0);
  654. t = last[0];
  655. last = cur;
  656. cur += p->linesize[0];
  657. }
  658. }else if(avctx->pix_fmt == PIX_FMT_RGB24){
  659. int j, width;
  660. int Rc[3] = {0, 0, 0};
  661. width = avctx->width * 3;
  662. for(i = 0; i < avctx->height; i++) {
  663. for(j = 0; j < 3; j++) {
  664. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
  665. Rc[j] = last[j];
  666. }
  667. last = cur;
  668. cur += s->picture.linesize[0];
  669. }
  670. }else if(avctx->pix_fmt == PIX_FMT_BGR24){
  671. int j, width;
  672. int Rc[3] = {0, 0, 0};
  673. width = avctx->width * 3;
  674. for(i = 0; i < avctx->height; i++) {
  675. for(j = 2; j >= 0; j--) {
  676. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
  677. Rc[j] = last[j];
  678. }
  679. last = cur;
  680. cur += s->picture.linesize[0];
  681. }
  682. }
  683. av_free(zero);
  684. av_free(state);
  685. flush_put_bits(&pb2);
  686. /* do escape coding */
  687. size = put_bits_count(&pb2) >> 3;
  688. init_get_bits(&gb, buf2, size);
  689. while(get_bits_count(&gb) < size * 8){
  690. int v;
  691. v = get_bits(&gb, 8);
  692. put_bits(&pb, 8, v);
  693. if(v == 0xFF){
  694. v = get_bits(&gb, 7);
  695. put_bits(&pb, 8, v);
  696. }
  697. }
  698. align_put_bits(&pb);
  699. av_free(buf2);
  700. /* End of image */
  701. put_marker(&pb, EOI);
  702. flush_put_bits(&pb);
  703. emms_c();
  704. return put_bits_count(&pb) >> 3;
  705. }
  706. static int encode_init_ls(AVCodecContext *ctx) {
  707. JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
  708. c->avctx = ctx;
  709. ctx->coded_frame = &c->picture;
  710. if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
  711. av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
  712. return -1;
  713. }
  714. return 0;
  715. }
  716. AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
  717. "jpegls",
  718. CODEC_TYPE_VIDEO,
  719. CODEC_ID_JPEGLS,
  720. sizeof(JpeglsContext),
  721. encode_init_ls,
  722. encode_picture_ls,
  723. NULL,
  724. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, -1},
  725. };
  726. #endif