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.1KB

  1. /*
  2. * LOCO codec
  3. * Copyright (c) 2005 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * LOCO codec.
  24. */
  25. #include "avcodec.h"
  26. #include "get_bits.h"
  27. #include "golomb.h"
  28. #include "internal.h"
  29. #include "mathops.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, const 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 *got_frame,
  141. AVPacket *avpkt)
  142. {
  143. const uint8_t *buf = avpkt->data;
  144. int buf_size = avpkt->size;
  145. LOCOContext * const l = avctx->priv_data;
  146. AVFrame * const p = &l->pic;
  147. int decoded;
  148. if(p->data[0])
  149. avctx->release_buffer(avctx, p);
  150. p->reference = 0;
  151. if(ff_get_buffer(avctx, p) < 0){
  152. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  153. return -1;
  154. }
  155. p->key_frame = 1;
  156. switch(l->mode) {
  157. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  158. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  159. p->linesize[0], buf, buf_size, 1);
  160. buf += decoded; buf_size -= decoded;
  161. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
  162. p->linesize[1], buf, buf_size, 1);
  163. buf += decoded; buf_size -= decoded;
  164. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
  165. p->linesize[2], buf, buf_size, 1);
  166. break;
  167. case LOCO_CYV12: case LOCO_YV12:
  168. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  169. p->linesize[0], buf, buf_size, 1);
  170. buf += decoded; buf_size -= decoded;
  171. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
  172. p->linesize[2], buf, buf_size, 1);
  173. buf += decoded; buf_size -= decoded;
  174. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
  175. p->linesize[1], buf, buf_size, 1);
  176. break;
  177. case LOCO_CRGB: case LOCO_RGB:
  178. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-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) + 1, avctx->width, avctx->height,
  182. -p->linesize[0], buf, buf_size, 3);
  183. buf += decoded; buf_size -= decoded;
  184. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
  185. -p->linesize[0], buf, buf_size, 3);
  186. break;
  187. case LOCO_RGBA:
  188. decoded = loco_decode_plane(l, p->data[0], 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] + 1, 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] + 2, avctx->width, avctx->height,
  195. p->linesize[0], buf, buf_size, 4);
  196. buf += decoded; buf_size -= decoded;
  197. decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
  198. p->linesize[0], buf, buf_size, 4);
  199. break;
  200. }
  201. *got_frame = 1;
  202. *(AVFrame*)data = l->pic;
  203. return buf_size;
  204. }
  205. static av_cold int decode_init(AVCodecContext *avctx){
  206. LOCOContext * const l = avctx->priv_data;
  207. int version;
  208. l->avctx = avctx;
  209. if (avctx->extradata_size < 12) {
  210. av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
  211. avctx->extradata_size);
  212. return -1;
  213. }
  214. version = AV_RL32(avctx->extradata);
  215. switch(version) {
  216. case 1:
  217. l->lossy = 0;
  218. break;
  219. case 2:
  220. l->lossy = AV_RL32(avctx->extradata + 8);
  221. break;
  222. default:
  223. l->lossy = AV_RL32(avctx->extradata + 8);
  224. av_log_ask_for_sample(avctx, "This is LOCO codec version %i.\n", version);
  225. }
  226. l->mode = AV_RL32(avctx->extradata + 4);
  227. switch(l->mode) {
  228. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  229. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  230. break;
  231. case LOCO_CRGB: case LOCO_RGB:
  232. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  233. break;
  234. case LOCO_CYV12: case LOCO_YV12:
  235. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  236. break;
  237. case LOCO_CRGBA: case LOCO_RGBA:
  238. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  239. break;
  240. default:
  241. av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
  242. return -1;
  243. }
  244. if(avctx->debug & FF_DEBUG_PICT_INFO)
  245. av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
  246. return 0;
  247. }
  248. static av_cold int decode_end(AVCodecContext *avctx){
  249. LOCOContext * const l = avctx->priv_data;
  250. AVFrame *pic = &l->pic;
  251. if (pic->data[0])
  252. avctx->release_buffer(avctx, pic);
  253. return 0;
  254. }
  255. AVCodec ff_loco_decoder = {
  256. .name = "loco",
  257. .type = AVMEDIA_TYPE_VIDEO,
  258. .id = AV_CODEC_ID_LOCO,
  259. .priv_data_size = sizeof(LOCOContext),
  260. .init = decode_init,
  261. .close = decode_end,
  262. .decode = decode_frame,
  263. .capabilities = CODEC_CAP_DR1,
  264. .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
  265. };