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.

286 lines
8.7KB

  1. /*
  2. * LOCO codec
  3. * Copyright (c) 2005 Konstantin Shishkov
  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. /**
  21. * @file loco.c
  22. * LOCO codec.
  23. */
  24. #include "avcodec.h"
  25. #include "common.h"
  26. #include "bitstream.h"
  27. #include "golomb.h"
  28. enum LOCO_MODE {LOCO_UNKN=0, LOCO_CYUY2=-1, LOCO_CRGB=-2, LOCO_CRGBA=-3, LOCO_CYV12=-4,
  29. LOCO_YUY2=1, LOCO_UYVY=2, LOCO_RGB=3, LOCO_RGBA=4, LOCO_YV12=5};
  30. typedef struct LOCOContext{
  31. AVCodecContext *avctx;
  32. AVFrame pic;
  33. int lossy;
  34. int mode;
  35. } LOCOContext;
  36. typedef struct RICEContext{
  37. GetBitContext gb;
  38. int save, run, run2; /* internal rice decoder state */
  39. int sum, count; /* sum and count for getting rice parameter */
  40. int lossy;
  41. }RICEContext;
  42. static int loco_get_rice_param(RICEContext *r)
  43. {
  44. int cnt = 0;
  45. int val = r->count;
  46. while(r->sum > val && cnt < 9) {
  47. val <<= 1;
  48. cnt++;
  49. }
  50. return cnt;
  51. }
  52. static inline void loco_update_rice_param(RICEContext *r, int val)
  53. {
  54. r->sum += val;
  55. r->count++;
  56. if(r->count == 16) {
  57. r->sum >>= 1;
  58. r->count >>= 1;
  59. }
  60. }
  61. static inline int loco_get_rice(RICEContext *r)
  62. {
  63. int v;
  64. if (r->run > 0) { /* we have zero run */
  65. r->run--;
  66. loco_update_rice_param(r, 0);
  67. return 0;
  68. }
  69. v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0);
  70. loco_update_rice_param(r, (v+1)>>1);
  71. if (!v) {
  72. if (r->save >= 0) {
  73. r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0);
  74. if(r->run > 1)
  75. r->save += r->run + 1;
  76. else
  77. r->save -= 3;
  78. }
  79. else
  80. r->run2++;
  81. } else {
  82. v = ((v>>1) + r->lossy) ^ -(v&1);
  83. if (r->run2 > 0) {
  84. if (r->run2 > 2)
  85. r->save += r->run2;
  86. else
  87. r->save -= 3;
  88. r->run2 = 0;
  89. }
  90. }
  91. return v;
  92. }
  93. /* LOCO main predictor - LOCO-I/JPEG-LS predictor */
  94. static inline int loco_predict(uint8_t* data, int stride, int step)
  95. {
  96. int a, b, c;
  97. a = data[-stride];
  98. b = data[-step];
  99. c = data[-stride - step];
  100. return mid_pred(a, a + b - c, b);
  101. }
  102. static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height,
  103. int stride, uint8_t *buf, int buf_size, int step)
  104. {
  105. RICEContext rc;
  106. int val;
  107. int i, j;
  108. init_get_bits(&rc.gb, buf, buf_size*8);
  109. rc.save = 0;
  110. rc.run = 0;
  111. rc.run2 = 0;
  112. rc.lossy = l->lossy;
  113. rc.sum = 8;
  114. rc.count = 1;
  115. /* restore top left pixel */
  116. val = loco_get_rice(&rc);
  117. data[0] = 128 + val;
  118. /* restore top line */
  119. for (i = 1; i < width; i++) {
  120. val = loco_get_rice(&rc);
  121. data[i * step] = data[i * step - step] + val;
  122. }
  123. data += stride;
  124. for (j = 1; j < height; j++) {
  125. /* restore left column */
  126. val = loco_get_rice(&rc);
  127. data[0] = data[-stride] + val;
  128. /* restore all other pixels */
  129. for (i = 1; i < width; i++) {
  130. val = loco_get_rice(&rc);
  131. data[i * step] = loco_predict(&data[i * step], stride, step) + val;
  132. }
  133. data += stride;
  134. }
  135. return ((get_bits_count(&rc.gb) + 7) >> 3);
  136. }
  137. static int decode_frame(AVCodecContext *avctx,
  138. void *data, int *data_size,
  139. uint8_t *buf, int buf_size)
  140. {
  141. LOCOContext * const l = avctx->priv_data;
  142. AVFrame * const p= (AVFrame*)&l->pic;
  143. int decoded;
  144. if(p->data[0])
  145. avctx->release_buffer(avctx, p);
  146. p->reference = 0;
  147. if(avctx->get_buffer(avctx, p) < 0){
  148. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  149. return -1;
  150. }
  151. p->key_frame = 1;
  152. switch(l->mode) {
  153. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  154. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  155. p->linesize[0], buf, buf_size, 1);
  156. buf += decoded; buf_size -= decoded;
  157. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
  158. p->linesize[1], buf, buf_size, 1);
  159. buf += decoded; buf_size -= decoded;
  160. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
  161. p->linesize[2], buf, buf_size, 1);
  162. break;
  163. case LOCO_CYV12: case LOCO_YV12:
  164. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  165. p->linesize[0], buf, buf_size, 1);
  166. buf += decoded; buf_size -= decoded;
  167. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
  168. p->linesize[2], buf, buf_size, 1);
  169. buf += decoded; buf_size -= decoded;
  170. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
  171. p->linesize[1], buf, buf_size, 1);
  172. break;
  173. case LOCO_CRGB: case LOCO_RGB:
  174. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
  175. -p->linesize[0], buf, buf_size, 3);
  176. buf += decoded; buf_size -= decoded;
  177. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 1, avctx->width, avctx->height,
  178. -p->linesize[0], buf, buf_size, 3);
  179. buf += decoded; buf_size -= decoded;
  180. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
  181. -p->linesize[0], buf, buf_size, 3);
  182. break;
  183. case LOCO_RGBA:
  184. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  185. p->linesize[0], buf, buf_size, 4);
  186. buf += decoded; buf_size -= decoded;
  187. decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height,
  188. p->linesize[0], buf, buf_size, 4);
  189. buf += decoded; buf_size -= decoded;
  190. decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
  191. p->linesize[0], buf, buf_size, 4);
  192. buf += decoded; buf_size -= decoded;
  193. decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
  194. p->linesize[0], buf, buf_size, 4);
  195. break;
  196. }
  197. *data_size = sizeof(AVFrame);
  198. *(AVFrame*)data = l->pic;
  199. return buf_size;
  200. }
  201. static int decode_init(AVCodecContext *avctx){
  202. LOCOContext * const l = avctx->priv_data;
  203. int version;
  204. l->avctx = avctx;
  205. if (avctx->extradata_size < 12) {
  206. av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
  207. avctx->extradata_size);
  208. return -1;
  209. }
  210. version = LE_32(avctx->extradata);
  211. switch(version) {
  212. case 1:
  213. l->lossy = 0;
  214. break;
  215. case 2:
  216. l->lossy = LE_32(avctx->extradata + 8);
  217. break;
  218. default:
  219. l->lossy = LE_32(avctx->extradata + 8);
  220. av_log(avctx, AV_LOG_INFO, "This is LOCO codec version %i, please upload file for study\n", version);
  221. }
  222. l->mode = LE_32(avctx->extradata + 4);
  223. switch(l->mode) {
  224. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  225. avctx->pix_fmt = PIX_FMT_YUV422P;
  226. break;
  227. case LOCO_CRGB: case LOCO_RGB:
  228. avctx->pix_fmt = PIX_FMT_BGR24;
  229. break;
  230. case LOCO_CYV12: case LOCO_YV12:
  231. avctx->pix_fmt = PIX_FMT_YUV420P;
  232. break;
  233. case LOCO_CRGBA: case LOCO_RGBA:
  234. avctx->pix_fmt = PIX_FMT_RGBA32;
  235. break;
  236. default:
  237. av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
  238. return -1;
  239. }
  240. if(avctx->debug & FF_DEBUG_PICT_INFO)
  241. av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
  242. return 0;
  243. }
  244. AVCodec loco_decoder = {
  245. "loco",
  246. CODEC_TYPE_VIDEO,
  247. CODEC_ID_LOCO,
  248. sizeof(LOCOContext),
  249. decode_init,
  250. NULL,
  251. NULL,
  252. decode_frame,
  253. CODEC_CAP_DR1,
  254. };