loco.c 8.7 KB

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