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.

855 lines
25KB

  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] = FFMAX((state->range + 32) >> 6, 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. static void inline downscale_state(JLSState *state, int Q){
  162. if(state->N[Q] == state->reset){
  163. state->A[Q] >>=1;
  164. state->B[Q] >>=1;
  165. state->N[Q] >>=1;
  166. }
  167. state->N[Q]++;
  168. }
  169. static inline int update_state_regular(JLSState *state, int Q, int err){
  170. state->A[Q] += FFABS(err);
  171. err *= state->twonear;
  172. state->B[Q] += err;
  173. downscale_state(state, Q);
  174. if(state->B[Q] <= -state->N[Q]) {
  175. state->B[Q]= FFMAX(state->B[Q] + state->N[Q], 1-state->N[Q]);
  176. if(state->C[Q] > -128)
  177. state->C[Q]--;
  178. }else if(state->B[Q] > 0){
  179. state->B[Q]= FFMIN(state->B[Q] - state->N[Q], 0);
  180. if(state->C[Q] < 127)
  181. state->C[Q]++;
  182. }
  183. return err;
  184. }
  185. /**
  186. * Get context-dependent Golomb code, decode it and update context
  187. */
  188. static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
  189. int k, ret;
  190. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  191. #ifdef JLS_BROKEN
  192. if(!show_bits_long(gb, 32))return -1;
  193. #endif
  194. ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
  195. /* decode mapped error */
  196. if(ret & 1)
  197. ret = -((ret + 1) >> 1);
  198. else
  199. ret >>= 1;
  200. /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
  201. if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
  202. ret = -(ret + 1);
  203. ret= update_state_regular(state, Q, ret);
  204. return ret;
  205. }
  206. /**
  207. * Get Golomb code, decode it and update state for run termination
  208. */
  209. static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
  210. int k, ret, temp, map;
  211. int Q = 365 + RItype;
  212. temp= state->A[Q];
  213. if(RItype)
  214. temp += 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. downscale_state(state, Q);
  235. return ret;
  236. }
  237. #define R(a, i ) (bits == 8 ? ((uint8_t*)(a))[i] : ((uint16_t*)(a))[i] )
  238. #define W(a, i, v) (bits == 8 ? (((uint8_t*)(a))[i]=v) : (((uint16_t*)(a))[i]=v))
  239. /**
  240. * Decode one line of image
  241. */
  242. static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
  243. int i, x = 0;
  244. int Ra, Rb, Rc, Rd;
  245. int D0, D1, D2;
  246. while(x < w) {
  247. int err, pred;
  248. /* compute gradients */
  249. Ra = x ? R(dst, x - stride) : R(last, x);
  250. Rb = R(last, x);
  251. Rc = x ? R(last, x - stride) : last2;
  252. Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
  253. D0 = Rd - Rb;
  254. D1 = Rb - Rc;
  255. D2 = Rc - Ra;
  256. /* run mode */
  257. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  258. int r;
  259. int RItype;
  260. /* decode full runs while available */
  261. while(get_bits1(&s->gb)) {
  262. int r;
  263. r = 1 << log2_run[state->run_index[comp]];
  264. if(x + r * stride > w) {
  265. r = (w - x) / stride;
  266. }
  267. for(i = 0; i < r; i++) {
  268. W(dst, x, Ra);
  269. x += stride;
  270. }
  271. /* if EOL reached, we stop decoding */
  272. if(r != (1 << log2_run[state->run_index[comp]]))
  273. return;
  274. if(state->run_index[comp] < 31)
  275. state->run_index[comp]++;
  276. if(x + stride > w)
  277. return;
  278. }
  279. /* decode aborted run */
  280. r = log2_run[state->run_index[comp]];
  281. if(r)
  282. r = get_bits_long(&s->gb, r);
  283. for(i = 0; i < r; i++) {
  284. W(dst, x, Ra);
  285. x += stride;
  286. }
  287. /* decode run termination value */
  288. Rb = R(last, x);
  289. RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
  290. err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
  291. if(state->run_index[comp])
  292. state->run_index[comp]--;
  293. if(state->near && RItype){
  294. pred = Ra + err;
  295. } else {
  296. if(Rb < Ra)
  297. pred = Rb - err;
  298. else
  299. pred = Rb + err;
  300. }
  301. } else { /* regular mode */
  302. int context, sign;
  303. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  304. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  305. if(context < 0){
  306. context = -context;
  307. sign = 1;
  308. }else{
  309. sign = 0;
  310. }
  311. if(sign){
  312. pred = clip(pred - state->C[context], 0, state->maxval);
  313. err = -ls_get_code_regular(&s->gb, state, context);
  314. } else {
  315. pred = clip(pred + state->C[context], 0, state->maxval);
  316. err = ls_get_code_regular(&s->gb, state, context);
  317. }
  318. /* we have to do something more for near-lossless coding */
  319. pred += err;
  320. }
  321. if(state->near){
  322. if(pred < -state->near)
  323. pred += state->range * state->twonear;
  324. else if(pred > state->maxval + state->near)
  325. pred -= state->range * state->twonear;
  326. pred = clip(pred, 0, state->maxval);
  327. }
  328. W(dst, x, pred);
  329. x += stride;
  330. }
  331. }
  332. static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
  333. int i, t = 0;
  334. uint8_t *zero, *last, *cur;
  335. JLSState *state;
  336. int off = 0, stride = 1, width, shift;
  337. zero = av_mallocz(s->picture.linesize[0]);
  338. last = zero;
  339. cur = s->picture.data[0];
  340. state = av_mallocz(sizeof(JLSState));
  341. /* initialize JPEG-LS state from JPEG parameters */
  342. state->near = near;
  343. state->bpp = (s->bits < 2) ? 2 : s->bits;
  344. state->maxval = s->maxval;
  345. state->T1 = s->t1;
  346. state->T2 = s->t2;
  347. state->T3 = s->t3;
  348. state->reset = s->reset;
  349. reset_ls_coding_parameters(state, 0);
  350. ls_init_state(state);
  351. if(s->bits <= 8)
  352. shift = point_transform + (8 - s->bits);
  353. else
  354. shift = point_transform + (16 - s->bits);
  355. // 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);
  356. // 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);
  357. if(ilv == 0) { /* separate planes */
  358. off = s->cur_scan - 1;
  359. stride = (s->nb_components > 1) ? 3 : 1;
  360. width = s->width * stride;
  361. cur += off;
  362. for(i = 0; i < s->height; i++) {
  363. if(s->bits <= 8){
  364. ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
  365. t = last[0];
  366. }else{
  367. ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
  368. t = *((uint16_t*)last);
  369. }
  370. last = cur;
  371. cur += s->picture.linesize[0];
  372. if (s->restart_interval && !--s->restart_count) {
  373. align_get_bits(&s->gb);
  374. skip_bits(&s->gb, 16); /* skip RSTn */
  375. }
  376. }
  377. } else if(ilv == 1) { /* line interleaving */
  378. int j;
  379. int Rc[3] = {0, 0, 0};
  380. memset(cur, 0, s->picture.linesize[0]);
  381. width = s->width * 3;
  382. for(i = 0; i < s->height; i++) {
  383. for(j = 0; j < 3; j++) {
  384. ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
  385. Rc[j] = last[j];
  386. if (s->restart_interval && !--s->restart_count) {
  387. align_get_bits(&s->gb);
  388. skip_bits(&s->gb, 16); /* skip RSTn */
  389. }
  390. }
  391. last = cur;
  392. cur += s->picture.linesize[0];
  393. }
  394. } else if(ilv == 2) { /* sample interleaving */
  395. av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
  396. av_free(state);
  397. av_free(zero);
  398. return -1;
  399. }
  400. if(shift){ /* we need to do point transform or normalize samples */
  401. int x, w;
  402. w = s->width * s->nb_components;
  403. if(s->bits <= 8){
  404. uint8_t *src = s->picture.data[0];
  405. for(i = 0; i < s->height; i++){
  406. for(x = off; x < w; x+= stride){
  407. src[x] <<= shift;
  408. }
  409. src += s->picture.linesize[0];
  410. }
  411. }else{
  412. uint16_t *src = s->picture.data[0];
  413. for(i = 0; i < s->height; i++){
  414. for(x = 0; x < w; x++){
  415. src[x] <<= shift;
  416. }
  417. src += s->picture.linesize[0]/2;
  418. }
  419. }
  420. }
  421. av_free(state);
  422. av_free(zero);
  423. return 0;
  424. }
  425. #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
  426. /********** Encoder-specific functions **********/
  427. /**
  428. * Encode error from regular symbol
  429. */
  430. static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
  431. int k;
  432. int val;
  433. int map;
  434. for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
  435. map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
  436. if(err < 0)
  437. err += state->range;
  438. if(err >= ((state->range + 1) >> 1)) {
  439. err -= state->range;
  440. val = 2 * FFABS(err) - 1 - map;
  441. } else
  442. val = 2 * err + map;
  443. set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
  444. update_state_regular(state, Q, err);
  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. downscale_state(state, Q);
  470. }
  471. /**
  472. * Encode run value as specified by JPEG-LS standard
  473. */
  474. static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
  475. while(run >= (1 << log2_run[state->run_index[comp]])){
  476. put_bits(pb, 1, 1);
  477. run -= 1 << log2_run[state->run_index[comp]];
  478. if(state->run_index[comp] < 31)
  479. state->run_index[comp]++;
  480. }
  481. /* if hit EOL, encode another full run, else encode aborted run */
  482. if(!trail && run) {
  483. put_bits(pb, 1, 1);
  484. }else if(trail){
  485. put_bits(pb, 1, 0);
  486. if(log2_run[state->run_index[comp]])
  487. put_bits(pb, log2_run[state->run_index[comp]], run);
  488. }
  489. }
  490. /**
  491. * Encode one line of image
  492. */
  493. static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
  494. int x = 0;
  495. int Ra, Rb, Rc, Rd;
  496. int D0, D1, D2;
  497. while(x < w) {
  498. int err, pred, sign;
  499. /* compute gradients */
  500. Ra = x ? R(cur, x - stride) : R(last, x);
  501. Rb = R(last, x);
  502. Rc = x ? R(last, x - stride) : last2;
  503. Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
  504. D0 = Rd - Rb;
  505. D1 = Rb - Rc;
  506. D2 = Rc - Ra;
  507. /* run mode */
  508. if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
  509. int RUNval, RItype, run;
  510. run = 0;
  511. RUNval = Ra;
  512. while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
  513. run++;
  514. W(cur, x, Ra);
  515. x += stride;
  516. }
  517. ls_encode_run(state, pb, run, comp, x < w);
  518. if(x >= w)
  519. return;
  520. Rb = R(last, x);
  521. RItype = (FFABS(Ra - Rb) <= state->near);
  522. pred = RItype ? Ra : Rb;
  523. err = R(cur, x) - pred;
  524. if(!RItype && Ra > Rb)
  525. err = -err;
  526. if(state->near){
  527. if(err > 0)
  528. err = (state->near + err) / state->twonear;
  529. else
  530. err = -(state->near - err) / state->twonear;
  531. if(RItype || (Rb >= Ra))
  532. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  533. else
  534. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  535. W(cur, x, Ra);
  536. }
  537. if(err < 0)
  538. err += state->range;
  539. if(err >= ((state->range + 1) >> 1))
  540. err -= state->range;
  541. ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
  542. if(state->run_index[comp] > 0)
  543. state->run_index[comp]--;
  544. } else { /* regular mode */
  545. int context;
  546. context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
  547. pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
  548. if(context < 0){
  549. context = -context;
  550. sign = 1;
  551. pred = clip(pred - state->C[context], 0, state->maxval);
  552. err = pred - R(cur, x);
  553. }else{
  554. sign = 0;
  555. pred = clip(pred + state->C[context], 0, state->maxval);
  556. err = R(cur, x) - pred;
  557. }
  558. if(state->near){
  559. if(err > 0)
  560. err = (state->near + err) / state->twonear;
  561. else
  562. err = -(state->near - err) / state->twonear;
  563. if(!sign)
  564. Ra = clip(pred + err * state->twonear, 0, state->maxval);
  565. else
  566. Ra = clip(pred - err * state->twonear, 0, state->maxval);
  567. W(cur, x, Ra);
  568. }
  569. ls_encode_regular(state, pb, context, err);
  570. }
  571. x += stride;
  572. }
  573. }
  574. static void ls_store_lse(JLSState *state, PutBitContext *pb){
  575. /* Test if we have default params and don't need to store LSE */
  576. JLSState state2;
  577. memset(&state2, 0, sizeof(JLSState));
  578. state2.bpp = state->bpp;
  579. state2.near = state->near;
  580. reset_ls_coding_parameters(&state2, 1);
  581. if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
  582. return;
  583. /* store LSE type 1 */
  584. put_marker(pb, LSE);
  585. put_bits(pb, 16, 13);
  586. put_bits(pb, 8, 1);
  587. put_bits(pb, 16, state->maxval);
  588. put_bits(pb, 16, state->T1);
  589. put_bits(pb, 16, state->T2);
  590. put_bits(pb, 16, state->T3);
  591. put_bits(pb, 16, state->reset);
  592. }
  593. static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  594. JpeglsContext * const s = avctx->priv_data;
  595. AVFrame *pict = data;
  596. AVFrame * const p= (AVFrame*)&s->picture;
  597. const int near = avctx->prediction_method;
  598. PutBitContext pb, pb2;
  599. GetBitContext gb;
  600. uint8_t *buf2, *zero, *cur, *last;
  601. JLSState *state;
  602. int i, size;
  603. int comps;
  604. buf2 = av_malloc(buf_size);
  605. init_put_bits(&pb, buf, buf_size);
  606. init_put_bits(&pb2, buf2, buf_size);
  607. *p = *pict;
  608. p->pict_type= FF_I_TYPE;
  609. p->key_frame= 1;
  610. if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
  611. comps = 1;
  612. else
  613. comps = 3;
  614. /* write our own JPEG header, can't use mjpeg_picture_header */
  615. put_marker(&pb, SOI);
  616. put_marker(&pb, SOF48);
  617. put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
  618. put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
  619. put_bits(&pb, 16, avctx->height);
  620. put_bits(&pb, 16, avctx->width);
  621. put_bits(&pb, 8, comps); // components
  622. for(i = 1; i <= comps; i++) {
  623. put_bits(&pb, 8, i); // component ID
  624. put_bits(&pb, 8, 0x11); // subsampling: none
  625. put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
  626. }
  627. put_marker(&pb, SOS);
  628. put_bits(&pb, 16, 6 + comps * 2);
  629. put_bits(&pb, 8, comps);
  630. for(i = 1; i <= comps; i++) {
  631. put_bits(&pb, 8, i); // component ID
  632. put_bits(&pb, 8, 0); // mapping index: none
  633. }
  634. put_bits(&pb, 8, near);
  635. put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
  636. put_bits(&pb, 8, 0); // point transform: none
  637. state = av_mallocz(sizeof(JLSState));
  638. /* initialize JPEG-LS state from JPEG parameters */
  639. state->near = near;
  640. state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
  641. reset_ls_coding_parameters(state, 0);
  642. ls_init_state(state);
  643. ls_store_lse(state, &pb);
  644. zero = av_mallocz(p->linesize[0]);
  645. last = zero;
  646. cur = p->data[0];
  647. if(avctx->pix_fmt == PIX_FMT_GRAY8){
  648. int t = 0;
  649. for(i = 0; i < avctx->height; i++) {
  650. ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
  651. t = last[0];
  652. last = cur;
  653. cur += p->linesize[0];
  654. }
  655. }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
  656. int t = 0;
  657. for(i = 0; i < avctx->height; i++) {
  658. ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
  659. t = *((uint16_t*)last);
  660. last = cur;
  661. cur += p->linesize[0];
  662. }
  663. }else if(avctx->pix_fmt == PIX_FMT_RGB24){
  664. int j, width;
  665. int Rc[3] = {0, 0, 0};
  666. width = avctx->width * 3;
  667. for(i = 0; i < avctx->height; i++) {
  668. for(j = 0; j < 3; j++) {
  669. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
  670. Rc[j] = last[j];
  671. }
  672. last = cur;
  673. cur += s->picture.linesize[0];
  674. }
  675. }else if(avctx->pix_fmt == PIX_FMT_BGR24){
  676. int j, width;
  677. int Rc[3] = {0, 0, 0};
  678. width = avctx->width * 3;
  679. for(i = 0; i < avctx->height; i++) {
  680. for(j = 2; j >= 0; j--) {
  681. ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
  682. Rc[j] = last[j];
  683. }
  684. last = cur;
  685. cur += s->picture.linesize[0];
  686. }
  687. }
  688. av_free(zero);
  689. av_free(state);
  690. flush_put_bits(&pb2);
  691. /* do escape coding */
  692. size = put_bits_count(&pb2) >> 3;
  693. init_get_bits(&gb, buf2, size);
  694. while(get_bits_count(&gb) < size * 8){
  695. int v;
  696. v = get_bits(&gb, 8);
  697. put_bits(&pb, 8, v);
  698. if(v == 0xFF){
  699. v = get_bits(&gb, 7);
  700. put_bits(&pb, 8, v);
  701. }
  702. }
  703. align_put_bits(&pb);
  704. av_free(buf2);
  705. /* End of image */
  706. put_marker(&pb, EOI);
  707. flush_put_bits(&pb);
  708. emms_c();
  709. return put_bits_count(&pb) >> 3;
  710. }
  711. static int encode_init_ls(AVCodecContext *ctx) {
  712. JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
  713. c->avctx = ctx;
  714. ctx->coded_frame = &c->picture;
  715. 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){
  716. av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
  717. return -1;
  718. }
  719. return 0;
  720. }
  721. AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
  722. "jpegls",
  723. CODEC_TYPE_VIDEO,
  724. CODEC_ID_JPEGLS,
  725. sizeof(JpeglsContext),
  726. encode_init_ls,
  727. encode_picture_ls,
  728. NULL,
  729. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, -1},
  730. };
  731. #endif