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.

288 lines
8.7KB

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