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.

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