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.

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