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.

300 lines
9.0KB

  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 "get_bits.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. AVPacket *avpkt)
  141. {
  142. const uint8_t *buf = avpkt->data;
  143. int buf_size = avpkt->size;
  144. LOCOContext * const l = avctx->priv_data;
  145. AVFrame * const p= (AVFrame*)&l->pic;
  146. int decoded;
  147. if(p->data[0])
  148. avctx->release_buffer(avctx, p);
  149. p->reference = 0;
  150. if(avctx->get_buffer(avctx, p) < 0){
  151. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  152. return -1;
  153. }
  154. p->key_frame = 1;
  155. switch(l->mode) {
  156. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  157. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  158. p->linesize[0], buf, buf_size, 1);
  159. buf += decoded; buf_size -= decoded;
  160. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
  161. p->linesize[1], buf, buf_size, 1);
  162. buf += decoded; buf_size -= decoded;
  163. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
  164. p->linesize[2], buf, buf_size, 1);
  165. break;
  166. case LOCO_CYV12: case LOCO_YV12:
  167. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  168. p->linesize[0], buf, buf_size, 1);
  169. buf += decoded; buf_size -= decoded;
  170. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
  171. p->linesize[2], buf, buf_size, 1);
  172. buf += decoded; buf_size -= decoded;
  173. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
  174. p->linesize[1], buf, buf_size, 1);
  175. break;
  176. case LOCO_CRGB: case LOCO_RGB:
  177. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-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) + 1, avctx->width, avctx->height,
  181. -p->linesize[0], buf, buf_size, 3);
  182. buf += decoded; buf_size -= decoded;
  183. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
  184. -p->linesize[0], buf, buf_size, 3);
  185. break;
  186. case LOCO_RGBA:
  187. decoded = loco_decode_plane(l, p->data[0], 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] + 1, 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] + 2, avctx->width, avctx->height,
  194. p->linesize[0], buf, buf_size, 4);
  195. buf += decoded; buf_size -= decoded;
  196. decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
  197. p->linesize[0], buf, buf_size, 4);
  198. break;
  199. }
  200. *data_size = sizeof(AVFrame);
  201. *(AVFrame*)data = l->pic;
  202. return buf_size;
  203. }
  204. static av_cold int decode_init(AVCodecContext *avctx){
  205. LOCOContext * const l = avctx->priv_data;
  206. int version;
  207. l->avctx = avctx;
  208. if (avctx->extradata_size < 12) {
  209. av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
  210. avctx->extradata_size);
  211. return -1;
  212. }
  213. version = AV_RL32(avctx->extradata);
  214. switch(version) {
  215. case 1:
  216. l->lossy = 0;
  217. break;
  218. case 2:
  219. l->lossy = AV_RL32(avctx->extradata + 8);
  220. break;
  221. default:
  222. l->lossy = AV_RL32(avctx->extradata + 8);
  223. av_log(avctx, AV_LOG_INFO, "This is LOCO codec version %i, please upload file for study\n", version);
  224. }
  225. l->mode = AV_RL32(avctx->extradata + 4);
  226. switch(l->mode) {
  227. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  228. avctx->pix_fmt = PIX_FMT_YUV422P;
  229. break;
  230. case LOCO_CRGB: case LOCO_RGB:
  231. avctx->pix_fmt = PIX_FMT_BGR24;
  232. break;
  233. case LOCO_CYV12: case LOCO_YV12:
  234. avctx->pix_fmt = PIX_FMT_YUV420P;
  235. break;
  236. case LOCO_CRGBA: case LOCO_RGBA:
  237. avctx->pix_fmt = PIX_FMT_RGB32;
  238. break;
  239. default:
  240. av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
  241. return -1;
  242. }
  243. if(avctx->debug & FF_DEBUG_PICT_INFO)
  244. av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
  245. return 0;
  246. }
  247. static av_cold int decode_end(AVCodecContext *avctx){
  248. LOCOContext * const l = avctx->priv_data;
  249. AVFrame *pic = &l->pic;
  250. if (pic->data[0])
  251. avctx->release_buffer(avctx, pic);
  252. return 0;
  253. }
  254. AVCodec loco_decoder = {
  255. "loco",
  256. CODEC_TYPE_VIDEO,
  257. CODEC_ID_LOCO,
  258. sizeof(LOCOContext),
  259. decode_init,
  260. NULL,
  261. decode_end,
  262. decode_frame,
  263. CODEC_CAP_DR1,
  264. .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
  265. };