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.

392 lines
12KB

  1. /*
  2. * JPEG-LS encoder and decoder
  3. * Copyright (c) 2003 Michael Niedermayer
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file jpeg_ls.c
  21. * JPEG-LS encoder and decoder.
  22. */
  23. #undef printf
  24. #undef fprintf
  25. static inline int quantize(MJpegDecodeContext *s, int v){ //FIXME optimize
  26. if(v==0) return 0;
  27. if(v < 0){
  28. if (v >-s->t1) return -1;
  29. else if(v >-s->t2) return -2;
  30. else if(v >-s->t3) return -3;
  31. else return -4;
  32. }else{
  33. if (v < s->t1) return 1;
  34. else if(v < s->t2) return 2;
  35. else if(v < s->t3) return 3;
  36. else return 4;
  37. }
  38. }
  39. static inline int predict8(uint8_t *src, uint8_t *last){ //FIXME perhaps its better to suppress these 2
  40. const int LT= last[-1];
  41. const int T= last[ 0];
  42. const int L = src[-1];
  43. return mid_pred(L, L + T - LT, T);
  44. }
  45. static inline int predict16(uint16_t *src, uint16_t *last){
  46. const int LT= last[-1];
  47. const int T= last[ 0];
  48. const int L = src[-1];
  49. return mid_pred(L, L + T - LT, T);
  50. }
  51. static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  52. return 0;
  53. }
  54. static int iso_clip(int v, int vmin, int vmax){
  55. if(v > vmax || v < vmin) return vmin;
  56. else return v;
  57. }
  58. static void reset_ls_coding_parameters(MJpegDecodeContext *s, int reset_all){
  59. const int basic_t1= 3;
  60. const int basic_t2= 7;
  61. const int basic_t3= 21;
  62. int factor;
  63. if(s->maxval==0 || reset_all) s->maxval= (1<<s->bits) - 1;
  64. if(s->maxval >=128){
  65. factor= (FFMIN(s->maxval, 4096) + 128)>>8;
  66. if(s->t1==0 || reset_all)
  67. s->t1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
  68. if(s->t2==0 || reset_all)
  69. s->t2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->t1, s->maxval);
  70. if(s->t3==0 || reset_all)
  71. s->t3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->t2, s->maxval);
  72. }else{
  73. factor= 256 / (s->maxval + 1);
  74. if(s->t1==0 || reset_all)
  75. s->t1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
  76. if(s->t2==0 || reset_all)
  77. s->t2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->t1, s->maxval);
  78. if(s->t3==0 || reset_all)
  79. s->t3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->t2, s->maxval);
  80. }
  81. if(s->reset==0 || reset_all) s->reset= 64;
  82. }
  83. static int decode_lse(MJpegDecodeContext *s)
  84. {
  85. int len, id;
  86. /* XXX: verify len field validity */
  87. len = get_bits(&s->gb, 16);
  88. id = get_bits(&s->gb, 8);
  89. switch(id){
  90. case 1:
  91. s->maxval= get_bits(&s->gb, 16);
  92. s->t1= get_bits(&s->gb, 16);
  93. s->t2= get_bits(&s->gb, 16);
  94. s->t3= get_bits(&s->gb, 16);
  95. s->reset= get_bits(&s->gb, 16);
  96. reset_ls_coding_parameters(s, 0);
  97. //FIXME quant table?
  98. break;
  99. case 2:
  100. case 3:
  101. printf("palette not supported\n");
  102. return -1;
  103. case 4:
  104. printf("oversize image not supported\n");
  105. return -1;
  106. default:
  107. printf("invalid id %d\n", id);
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. #if 0
  113. static inline void update_vlc_state(VlcState * const state, const int v, int half_count){
  114. int drift= state->drift;
  115. int count= state->count;
  116. state->error_sum += ABS(v);
  117. drift += v;
  118. if(count == half_count){
  119. count >>= 1;
  120. drift >>= 1;
  121. state->error_sum >>= 1;
  122. }
  123. count++;
  124. if(drift <= -count){
  125. if(state->bias > -128) state->bias--;
  126. drift += count;
  127. if(drift <= -count)
  128. drift= -count + 1;
  129. }else if(drift > 0){
  130. if(state->bias < 127) state->bias++;
  131. drift -= count;
  132. if(drift > 0)
  133. drift= 0;
  134. }
  135. state->drift= drift;
  136. state->count= count;
  137. }
  138. #define R(p, i) (is_uint8 ? (((uint8_t*)p)[i] : ((uint16_t*)p)[i])
  139. static inline int ls_decode_line(MJpegDecodeContext *s, void *lastv, void *dstv, int last2,
  140. int w, int point_transform, int is_uint8){
  141. int i, x, y;
  142. for(x=0; x < w; x++){
  143. int l, t, lt, rt;
  144. t= R(last, 0);
  145. if(x){
  146. l = t;
  147. lt= last2;
  148. }else{
  149. l = R(dst, x-1);
  150. lt= R(last, x-1);
  151. }
  152. if(x<w-1) rt= R(last, x+1);
  153. else rt= t;
  154. hr_gradient= rt - t;
  155. hl_gradient= t - lt;
  156. v_gradient= lt - l;
  157. context= quantize(s, v_gradient) + 9*(quantize(s, hl_gradient) + 9*quantize(s, hr_gradient));
  158. if(context){
  159. int pred= mid_pred(l, l + t - lt, t);
  160. if(context < 0){
  161. context= -context;
  162. sign= 1;
  163. pred= clip(0, pred - state->bias, maxval);
  164. }else{
  165. sign= 0;
  166. pred= clip(0, pred + state->bias, maxval);
  167. }
  168. i= state->count;
  169. k=0;
  170. while(i < state->error_sum){ //FIXME optimize
  171. k++;
  172. i += i;
  173. }
  174. v= get_ur_golomb_jpegls(gb, k, LIMIT-qbpp, qbpp);
  175. #if 1
  176. v++;
  177. if(v&1) v= (v>>1);
  178. else v= -(v>>1);
  179. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  180. #else
  181. v ^= (k==0 && 2*state->drift <= - state->count);
  182. v++;
  183. if(v&1) v= (v>>1);
  184. else v= -(v>>1);
  185. #endif
  186. update_vlc_state(state, v, half_count);
  187. if(sign) v= -v;
  188. if(is_uint8) ((uint8_t *)dst)[x]= (pred + v) & maxval;
  189. else ((uint16_t*)dst)[x]= (pred + v) & maxval;
  190. }else{
  191. int run_count;
  192. while(get_bits1(&s->gb)){
  193. run_count = 1<<log2_run[run_index];
  194. if(x + run_count > w) run_count= w - x;
  195. else run_index++;
  196. for(; run_count; run_count--){
  197. if(is_uint8) ((uint8_t *)dst)[x++]= l;
  198. else ((uint16_t*)dst)[x++]= l;
  199. }
  200. if(x >= w) return 0;
  201. }
  202. run_count= get_bits(&s->gb, log2_run[run_index]);
  203. for(; run_count; run_count--){
  204. if(is_uint8) ((uint8_t *)dst)[x++]= l;
  205. else ((uint16_t*)dst)[x++]= l;
  206. }
  207. if(run_index) run_index--;
  208. if(x >= w) return 0;
  209. t= R(last, 0);
  210. RItype= (l==t);
  211. if(l==t){
  212. state= 366;
  213. temp= state->error_sum + (state->count>>1);
  214. }else{
  215. state= 365;
  216. temp= state->error_sum;
  217. }
  218. pred= t;
  219. sign= l > t;
  220. i= state->count;
  221. k=0;
  222. while(i < temp){ //FIXME optimize
  223. k++;
  224. i += i;
  225. }
  226. assert(Errval != 0);
  227. map = (k==0 && 2*Nn < state->count) == (Errval>0);
  228. if(run_count==0 && run_mode==1){
  229. if(get_bits1(&s->gb)){
  230. run_count = 1<<log2_run[run_index];
  231. if(x + run_count <= w) run_index++;
  232. }else{
  233. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  234. else run_count=0;
  235. if(run_index) run_index--;
  236. run_mode=2;
  237. }
  238. }
  239. run_count--;
  240. if(run_count < 0){
  241. run_mode=0;
  242. run_count=0;
  243. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
  244. if(diff>=0) diff++;
  245. }else
  246. diff=0;
  247. }
  248. }
  249. /* if (s->restart_interval && !s->restart_count)
  250. s->restart_count = s->restart_interval;*/
  251. if(mb_x==0 || mb_y==0 || s->interlaced){
  252. for(i=0;i<nb_components;i++) {
  253. uint8_t *ptr;
  254. int n, h, v, x, y, c, j, linesize;
  255. n = s->nb_blocks[i];
  256. c = s->comp_index[i];
  257. h = s->h_scount[i];
  258. v = s->v_scount[i];
  259. x = 0;
  260. y = 0;
  261. linesize= s->linesize[c];
  262. for(j=0; j<n; j++) {
  263. int pred;
  264. ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  265. if(y==0 && mb_y==0){
  266. if(x==0 && mb_x==0){
  267. pred= 128 << point_transform;
  268. }else{
  269. pred= ptr[-1];
  270. }
  271. }else{
  272. if(x==0 && mb_x==0){
  273. pred= ptr[-linesize];
  274. }else{
  275. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  276. }
  277. }
  278. if (s->interlaced && s->bottom_field)
  279. ptr += linesize >> 1;
  280. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  281. if (++x == h) {
  282. x = 0;
  283. y++;
  284. }
  285. }
  286. }
  287. }else{
  288. for(i=0;i<nb_components;i++) {
  289. uint8_t *ptr;
  290. int n, h, v, x, y, c, j, linesize;
  291. n = s->nb_blocks[i];
  292. c = s->comp_index[i];
  293. h = s->h_scount[i];
  294. v = s->v_scount[i];
  295. x = 0;
  296. y = 0;
  297. linesize= s->linesize[c];
  298. for(j=0; j<n; j++) {
  299. int pred;
  300. ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  301. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  302. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  303. if (++x == h) {
  304. x = 0;
  305. y++;
  306. }
  307. }
  308. }
  309. }
  310. if (s->restart_interval && !--s->restart_count) {
  311. align_get_bits(&s->gb);
  312. skip_bits(&s->gb, 16); /* skip RSTn */
  313. }
  314. return 0;
  315. }
  316. #endif
  317. #ifdef CONFIG_ENCODERS
  318. AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
  319. "jpegls",
  320. CODEC_TYPE_VIDEO,
  321. CODEC_ID_JPEGLS,
  322. sizeof(MpegEncContext),
  323. MPV_encode_init,
  324. encode_picture_ls,
  325. MPV_encode_end,
  326. };
  327. #endif