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.

350 lines
10KB

  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
  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 {
  31. LOCO_UNKN = 0,
  32. LOCO_CYUY2 = -1,
  33. LOCO_CRGB = -2,
  34. LOCO_CRGBA = -3,
  35. LOCO_CYV12 = -4,
  36. LOCO_YUY2 = 1,
  37. LOCO_UYVY = 2,
  38. LOCO_RGB = 3,
  39. LOCO_RGBA = 4,
  40. LOCO_YV12 = 5,
  41. };
  42. typedef struct LOCOContext {
  43. AVCodecContext *avctx;
  44. int lossy;
  45. enum LOCO_MODE mode;
  46. } LOCOContext;
  47. typedef struct RICEContext {
  48. GetBitContext gb;
  49. int save, run, run2; /* internal rice decoder state */
  50. int sum, count; /* sum and count for getting rice parameter */
  51. int lossy;
  52. } RICEContext;
  53. static int loco_get_rice_param(RICEContext *r)
  54. {
  55. int cnt = 0;
  56. int val = r->count;
  57. while (r->sum > val && cnt < 9) {
  58. val <<= 1;
  59. cnt++;
  60. }
  61. return cnt;
  62. }
  63. static inline void loco_update_rice_param(RICEContext *r, int val)
  64. {
  65. r->sum += val;
  66. r->count++;
  67. if (r->count == 16) {
  68. r->sum >>= 1;
  69. r->count >>= 1;
  70. }
  71. }
  72. static inline int loco_get_rice(RICEContext *r)
  73. {
  74. unsigned v;
  75. if (r->run > 0) { /* we have zero run */
  76. r->run--;
  77. loco_update_rice_param(r, 0);
  78. return 0;
  79. }
  80. if (get_bits_left(&r->gb) < 1)
  81. return INT_MIN;
  82. v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0);
  83. loco_update_rice_param(r, (v + 1) >> 1);
  84. if (!v) {
  85. if (r->save >= 0) {
  86. r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0);
  87. if (r->run > 1)
  88. r->save += r->run + 1;
  89. else
  90. r->save -= 3;
  91. } else
  92. r->run2++;
  93. } else {
  94. v = ((v >> 1) + r->lossy) ^ -(v & 1);
  95. if (r->run2 > 0) {
  96. if (r->run2 > 2)
  97. r->save += r->run2;
  98. else
  99. r->save -= 3;
  100. r->run2 = 0;
  101. }
  102. }
  103. return v;
  104. }
  105. /* LOCO main predictor - LOCO-I/JPEG-LS predictor */
  106. static inline int loco_predict(uint8_t* data, int stride)
  107. {
  108. int a, b, c;
  109. a = data[-stride];
  110. b = data[-1];
  111. c = data[-stride - 1];
  112. return mid_pred(a, a + b - c, b);
  113. }
  114. static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height,
  115. int stride, const uint8_t *buf, int buf_size)
  116. {
  117. RICEContext rc;
  118. int val;
  119. int ret;
  120. int i, j;
  121. if(buf_size<=0)
  122. return -1;
  123. if ((ret = init_get_bits8(&rc.gb, buf, buf_size)) < 0)
  124. return ret;
  125. rc.save = 0;
  126. rc.run = 0;
  127. rc.run2 = 0;
  128. rc.lossy = l->lossy;
  129. rc.sum = 8;
  130. rc.count = 1;
  131. /* restore top left pixel */
  132. val = loco_get_rice(&rc);
  133. data[0] = 128 + val;
  134. /* restore top line */
  135. for (i = 1; i < width; i++) {
  136. val = loco_get_rice(&rc);
  137. if (val == INT_MIN)
  138. return AVERROR_INVALIDDATA;
  139. data[i] = data[i - 1] + val;
  140. }
  141. data += stride;
  142. for (j = 1; j < height; j++) {
  143. /* restore left column */
  144. val = loco_get_rice(&rc);
  145. if (val == INT_MIN)
  146. return AVERROR_INVALIDDATA;
  147. data[0] = data[-stride] + val;
  148. /* restore all other pixels */
  149. for (i = 1; i < width; i++) {
  150. val = loco_get_rice(&rc);
  151. if (val == INT_MIN)
  152. return -1;
  153. data[i] = loco_predict(&data[i], stride) + val;
  154. }
  155. data += stride;
  156. }
  157. return (get_bits_count(&rc.gb) + 7) >> 3;
  158. }
  159. static void rotate_faulty_loco(uint8_t *data, int width, int height, int stride)
  160. {
  161. int y;
  162. for (y=1; y<height; y++) {
  163. if (width>=y) {
  164. memmove(data + y*stride,
  165. data + y*(stride + 1),
  166. (width-y));
  167. if (y+1 < height)
  168. memmove(data + y*stride + (width-y),
  169. data + (y+1)*stride, y);
  170. }
  171. }
  172. }
  173. static int decode_frame(AVCodecContext *avctx,
  174. void *data, int *got_frame,
  175. AVPacket *avpkt)
  176. {
  177. LOCOContext * const l = avctx->priv_data;
  178. const uint8_t *buf = avpkt->data;
  179. int buf_size = avpkt->size;
  180. AVFrame * const p = data;
  181. int decoded, ret;
  182. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  183. return ret;
  184. p->key_frame = 1;
  185. #define ADVANCE_BY_DECODED do { \
  186. if (decoded < 0 || decoded >= buf_size) goto buf_too_small; \
  187. buf += decoded; buf_size -= decoded; \
  188. } while(0)
  189. switch(l->mode) {
  190. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  191. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  192. p->linesize[0], buf, buf_size);
  193. ADVANCE_BY_DECODED;
  194. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
  195. p->linesize[1], buf, buf_size);
  196. ADVANCE_BY_DECODED;
  197. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
  198. p->linesize[2], buf, buf_size);
  199. break;
  200. case LOCO_CYV12: case LOCO_YV12:
  201. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  202. p->linesize[0], buf, buf_size);
  203. ADVANCE_BY_DECODED;
  204. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
  205. p->linesize[2], buf, buf_size);
  206. ADVANCE_BY_DECODED;
  207. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
  208. p->linesize[1], buf, buf_size);
  209. break;
  210. case LOCO_CRGB: case LOCO_RGB:
  211. decoded = loco_decode_plane(l, p->data[1] + p->linesize[1]*(avctx->height-1), avctx->width, avctx->height,
  212. -p->linesize[1], buf, buf_size);
  213. ADVANCE_BY_DECODED;
  214. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
  215. -p->linesize[0], buf, buf_size);
  216. ADVANCE_BY_DECODED;
  217. decoded = loco_decode_plane(l, p->data[2] + p->linesize[2]*(avctx->height-1), avctx->width, avctx->height,
  218. -p->linesize[2], buf, buf_size);
  219. if (avctx->width & 1) {
  220. rotate_faulty_loco(p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height, -p->linesize[0]);
  221. rotate_faulty_loco(p->data[1] + p->linesize[1]*(avctx->height-1), avctx->width, avctx->height, -p->linesize[1]);
  222. rotate_faulty_loco(p->data[2] + p->linesize[2]*(avctx->height-1), avctx->width, avctx->height, -p->linesize[2]);
  223. }
  224. break;
  225. case LOCO_CRGBA:
  226. case LOCO_RGBA:
  227. decoded = loco_decode_plane(l, p->data[1] + p->linesize[1]*(avctx->height-1), avctx->width, avctx->height,
  228. -p->linesize[1], buf, buf_size);
  229. ADVANCE_BY_DECODED;
  230. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
  231. -p->linesize[0], buf, buf_size);
  232. ADVANCE_BY_DECODED;
  233. decoded = loco_decode_plane(l, p->data[2] + p->linesize[2]*(avctx->height-1), avctx->width, avctx->height,
  234. -p->linesize[2], buf, buf_size);
  235. ADVANCE_BY_DECODED;
  236. decoded = loco_decode_plane(l, p->data[3] + p->linesize[3]*(avctx->height-1), avctx->width, avctx->height,
  237. -p->linesize[3], buf, buf_size);
  238. break;
  239. default:
  240. av_assert0(0);
  241. }
  242. if (decoded < 0 || decoded > buf_size)
  243. goto buf_too_small;
  244. buf_size -= decoded;
  245. *got_frame = 1;
  246. return avpkt->size - buf_size;
  247. buf_too_small:
  248. av_log(avctx, AV_LOG_ERROR, "Input data too small.\n");
  249. return AVERROR(EINVAL);
  250. }
  251. static av_cold int decode_init(AVCodecContext *avctx)
  252. {
  253. LOCOContext * const l = avctx->priv_data;
  254. int version;
  255. l->avctx = avctx;
  256. if (avctx->extradata_size < 12) {
  257. av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
  258. avctx->extradata_size);
  259. return AVERROR_INVALIDDATA;
  260. }
  261. version = AV_RL32(avctx->extradata);
  262. switch (version) {
  263. case 1:
  264. l->lossy = 0;
  265. break;
  266. case 2:
  267. l->lossy = AV_RL32(avctx->extradata + 8);
  268. break;
  269. default:
  270. l->lossy = AV_RL32(avctx->extradata + 8);
  271. avpriv_request_sample(avctx, "LOCO codec version %i", version);
  272. }
  273. if (l->lossy > 65536U) {
  274. av_log(avctx, AV_LOG_ERROR, "lossy %i is too large\n", l->lossy);
  275. return AVERROR_INVALIDDATA;
  276. }
  277. l->mode = AV_RL32(avctx->extradata + 4);
  278. switch (l->mode) {
  279. case LOCO_CYUY2:
  280. case LOCO_YUY2:
  281. case LOCO_UYVY:
  282. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  283. break;
  284. case LOCO_CRGB:
  285. case LOCO_RGB:
  286. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  287. break;
  288. case LOCO_CYV12:
  289. case LOCO_YV12:
  290. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  291. break;
  292. case LOCO_CRGBA:
  293. case LOCO_RGBA:
  294. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  295. break;
  296. default:
  297. av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
  298. return AVERROR_INVALIDDATA;
  299. }
  300. if (avctx->debug & FF_DEBUG_PICT_INFO)
  301. av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
  302. return 0;
  303. }
  304. AVCodec ff_loco_decoder = {
  305. .name = "loco",
  306. .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
  307. .type = AVMEDIA_TYPE_VIDEO,
  308. .id = AV_CODEC_ID_LOCO,
  309. .priv_data_size = sizeof(LOCOContext),
  310. .init = decode_init,
  311. .decode = decode_frame,
  312. .capabilities = AV_CODEC_CAP_DR1,
  313. };