motionpixels.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Motion Pixels Video Decoder
  3. * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
  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. #include "avcodec.h"
  22. #include "bitstream.h"
  23. #include "dsputil.h"
  24. #define MAX_HUFF_CODES 16
  25. typedef struct YuvPixel {
  26. int8_t y, v, u;
  27. } YuvPixel;
  28. typedef struct HuffCode {
  29. int code;
  30. uint8_t size;
  31. uint8_t delta;
  32. } HuffCode;
  33. typedef struct MotionPixelsContext {
  34. AVCodecContext *avctx;
  35. AVFrame frame;
  36. DSPContext dsp;
  37. uint8_t *changes_map;
  38. int offset_bits_len;
  39. int codes_count, current_codes_count;
  40. int max_codes_bits;
  41. HuffCode codes[MAX_HUFF_CODES];
  42. VLC vlc;
  43. YuvPixel *vpt, *hpt;
  44. uint8_t gradient_scale[3];
  45. uint8_t *bswapbuf;
  46. int bswapbuf_size;
  47. } MotionPixelsContext;
  48. static YuvPixel mp_rgb_yuv_table[1 << 15];
  49. static int mp_yuv_to_rgb(int y, int v, int u, int clip_rgb) {
  50. static const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  51. int r, g, b;
  52. r = (1000 * y + 701 * v) / 1000;
  53. g = (1000 * y - 357 * v - 172 * u) / 1000;
  54. b = (1000 * y + 886 * u) / 1000;
  55. if (clip_rgb)
  56. return ((cm[r * 8] & 0xF8) << 7) | ((cm[g * 8] & 0xF8) << 2) | (cm[b * 8] >> 3);
  57. if ((unsigned)r < 32 && (unsigned)g < 32 && (unsigned)b < 32)
  58. return (r << 10) | (g << 5) | b;
  59. return 1 << 15;
  60. }
  61. static void mp_set_zero_yuv(YuvPixel *p)
  62. {
  63. int i, j;
  64. for (i = 0; i < 31; ++i) {
  65. for (j = 31; j > i; --j)
  66. if (!(p[j].u | p[j].v | p[j].y))
  67. p[j] = p[j - 1];
  68. for (j = 0; j < 31 - i; ++j)
  69. if (!(p[j].u | p[j].v | p[j].y))
  70. p[j] = p[j + 1];
  71. }
  72. }
  73. static void mp_build_rgb_yuv_table(YuvPixel *p)
  74. {
  75. int y, v, u, i;
  76. for (y = 0; y <= 31; ++y)
  77. for (v = -31; v <= 31; ++v)
  78. for (u = -31; u <= 31; ++u) {
  79. i = mp_yuv_to_rgb(y, v, u, 0);
  80. if (i < (1 << 15) && !(p[i].u | p[i].v | p[i].y)) {
  81. p[i].y = y;
  82. p[i].v = v;
  83. p[i].u = u;
  84. }
  85. }
  86. for (i = 0; i < 1024; ++i)
  87. mp_set_zero_yuv(p + i * 32);
  88. }
  89. static av_cold int mp_decode_init(AVCodecContext *avctx)
  90. {
  91. MotionPixelsContext *mp = avctx->priv_data;
  92. if (!mp_rgb_yuv_table[0].u) {
  93. mp_build_rgb_yuv_table(mp_rgb_yuv_table);
  94. }
  95. mp->avctx = avctx;
  96. dsputil_init(&mp->dsp, avctx);
  97. mp->changes_map = av_mallocz(avctx->width * avctx->height);
  98. mp->offset_bits_len = av_log2(avctx->width * avctx->height) + 1;
  99. mp->vpt = av_mallocz(avctx->height * sizeof(YuvPixel));
  100. mp->hpt = av_mallocz(avctx->height * avctx->width / 16 * sizeof(YuvPixel));
  101. return 0;
  102. }
  103. static void mp_read_changes_map(MotionPixelsContext *mp, GetBitContext *gb, int count, int bits_len, int read_color)
  104. {
  105. uint16_t *pixels;
  106. int offset, w, h, color = 0, x, y, i;
  107. while (count--) {
  108. offset = get_bits_long(gb, mp->offset_bits_len);
  109. w = get_bits(gb, bits_len) + 1;
  110. h = get_bits(gb, bits_len) + 1;
  111. if (read_color)
  112. color = get_bits(gb, 15);
  113. x = offset % mp->avctx->width;
  114. y = offset / mp->avctx->width;
  115. if (y >= mp->avctx->height)
  116. continue;
  117. w = FFMIN(w, mp->avctx->width - x);
  118. h = FFMIN(h, mp->avctx->height - y);
  119. pixels = (uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2];
  120. while (h--) {
  121. mp->changes_map[offset] = w;
  122. if (read_color)
  123. for (i = 0; i < w; ++i)
  124. pixels[i] = color;
  125. offset += mp->avctx->width;
  126. pixels += mp->frame.linesize[0] / 2;
  127. }
  128. }
  129. }
  130. static void mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size, int code)
  131. {
  132. while (get_bits1(gb)) {
  133. ++size;
  134. if (size > mp->max_codes_bits) {
  135. av_log(mp->avctx, AV_LOG_ERROR, "invalid code size %d/%d\n", size, mp->max_codes_bits);
  136. return;
  137. }
  138. code <<= 1;
  139. mp_get_code(mp, gb, size, code + 1);
  140. }
  141. if (mp->current_codes_count >= MAX_HUFF_CODES) {
  142. av_log(mp->avctx, AV_LOG_ERROR, "too many codes\n");
  143. return;
  144. }
  145. mp->codes[mp->current_codes_count ].code = code;
  146. mp->codes[mp->current_codes_count++].size = size;
  147. }
  148. static void mp_read_codes_table(MotionPixelsContext *mp, GetBitContext *gb)
  149. {
  150. if (mp->codes_count == 1) {
  151. mp->codes[0].delta = get_bits(gb, 4);
  152. } else {
  153. int i;
  154. mp->max_codes_bits = get_bits(gb, 4);
  155. for (i = 0; i < mp->codes_count; ++i)
  156. mp->codes[i].delta = get_bits(gb, 4);
  157. mp->current_codes_count = 0;
  158. mp_get_code(mp, gb, 0, 0);
  159. }
  160. }
  161. static int mp_gradient(MotionPixelsContext *mp, int component, int v)
  162. {
  163. int delta;
  164. delta = (v - 7) * mp->gradient_scale[component];
  165. mp->gradient_scale[component] = (v == 0 || v == 14) ? 2 : 1;
  166. return delta;
  167. }
  168. static YuvPixel mp_get_yuv_from_rgb(MotionPixelsContext *mp, int x, int y)
  169. {
  170. int color;
  171. color = *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2];
  172. return mp_rgb_yuv_table[color];
  173. }
  174. static void mp_set_rgb_from_yuv(MotionPixelsContext *mp, int x, int y, const YuvPixel *p)
  175. {
  176. int color;
  177. color = mp_yuv_to_rgb(p->y, p->v, p->u, 1);
  178. *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2] = color;
  179. }
  180. static int mp_get_vlc(MotionPixelsContext *mp, GetBitContext *gb)
  181. {
  182. int i;
  183. i = (mp->codes_count == 1) ? 0 : get_vlc2(gb, mp->vlc.table, mp->max_codes_bits, 1);
  184. return mp->codes[i].delta;
  185. }
  186. static void mp_decode_line(MotionPixelsContext *mp, GetBitContext *gb, int y)
  187. {
  188. YuvPixel p;
  189. const int y0 = y * mp->avctx->width;
  190. int w, i, x = 0;
  191. p = mp->vpt[y];
  192. if (mp->changes_map[y0 + x] == 0) {
  193. memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
  194. ++x;
  195. }
  196. while (x < mp->avctx->width) {
  197. w = mp->changes_map[y0 + x];
  198. if (w != 0) {
  199. if ((y & 3) == 0) {
  200. if (mp->changes_map[y0 + x + mp->avctx->width] < w ||
  201. mp->changes_map[y0 + x + mp->avctx->width * 2] < w ||
  202. mp->changes_map[y0 + x + mp->avctx->width * 3] < w) {
  203. for (i = (x + 3) & ~3; i < x + w; i += 4) {
  204. mp->hpt[((y / 4) * mp->avctx->width + i) / 4] = mp_get_yuv_from_rgb(mp, i, y);
  205. }
  206. }
  207. }
  208. x += w;
  209. memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
  210. p = mp_get_yuv_from_rgb(mp, x - 1, y);
  211. } else {
  212. p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb));
  213. p.y = av_clip(p.y, 0, 31);
  214. if ((x & 3) == 0) {
  215. if ((y & 3) == 0) {
  216. p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
  217. p.v = av_clip(p.v, -32, 31);
  218. p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
  219. p.u = av_clip(p.u, -32, 31);
  220. mp->hpt[((y / 4) * mp->avctx->width + x) / 4] = p;
  221. } else {
  222. p.v = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].v;
  223. p.u = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].u;
  224. }
  225. }
  226. mp_set_rgb_from_yuv(mp, x, y, &p);
  227. ++x;
  228. }
  229. }
  230. }
  231. static void mp_decode_frame_helper(MotionPixelsContext *mp, GetBitContext *gb)
  232. {
  233. YuvPixel p;
  234. int y, y0;
  235. for (y = 0; y < mp->avctx->height; ++y) {
  236. if (mp->changes_map[y * mp->avctx->width] != 0) {
  237. memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
  238. p = mp_get_yuv_from_rgb(mp, 0, y);
  239. } else {
  240. p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb));
  241. p.y = av_clip(p.y, 0, 31);
  242. if ((y & 3) == 0) {
  243. p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
  244. p.v = av_clip(p.v, -32, 31);
  245. p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
  246. p.u = av_clip(p.u, -32, 31);
  247. }
  248. mp->vpt[y] = p;
  249. mp_set_rgb_from_yuv(mp, 0, y, &p);
  250. }
  251. }
  252. for (y0 = 0; y0 < 2; ++y0)
  253. for (y = y0; y < mp->avctx->height; y += 2)
  254. mp_decode_line(mp, gb, y);
  255. }
  256. static int mp_decode_frame(AVCodecContext *avctx,
  257. void *data, int *data_size,
  258. const uint8_t *buf, int buf_size)
  259. {
  260. MotionPixelsContext *mp = avctx->priv_data;
  261. GetBitContext gb;
  262. int i, count1, count2, sz;
  263. mp->frame.reference = 1;
  264. mp->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  265. if (avctx->reget_buffer(avctx, &mp->frame)) {
  266. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  267. return -1;
  268. }
  269. /* le32 bitstream msb first */
  270. mp->bswapbuf = av_fast_realloc(mp->bswapbuf, &mp->bswapbuf_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  271. mp->dsp.bswap_buf((uint32_t *)mp->bswapbuf, (const uint32_t *)buf, buf_size / 4);
  272. if (buf_size & 3)
  273. memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3);
  274. init_get_bits(&gb, mp->bswapbuf, buf_size * 8);
  275. memset(mp->changes_map, 0, avctx->width * avctx->height);
  276. for (i = !(avctx->extradata[1] & 2); i < 2; ++i) {
  277. count1 = get_bits(&gb, 12);
  278. count2 = get_bits(&gb, 12);
  279. mp_read_changes_map(mp, &gb, count1, 8, i);
  280. mp_read_changes_map(mp, &gb, count2, 4, i);
  281. }
  282. mp->codes_count = get_bits(&gb, 4);
  283. if (mp->codes_count == 0)
  284. goto end;
  285. if (mp->changes_map[0] == 0) {
  286. *(uint16_t *)mp->frame.data[0] = get_bits(&gb, 15);
  287. mp->changes_map[0] = 1;
  288. }
  289. mp_read_codes_table(mp, &gb);
  290. sz = get_bits(&gb, 18);
  291. if (avctx->extradata[0] != 5)
  292. sz += get_bits(&gb, 18);
  293. if (sz == 0)
  294. goto end;
  295. if (init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, &mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 4, 0))
  296. goto end;
  297. mp_decode_frame_helper(mp, &gb);
  298. free_vlc(&mp->vlc);
  299. end:
  300. *data_size = sizeof(AVFrame);
  301. *(AVFrame *)data = mp->frame;
  302. return buf_size;
  303. }
  304. static av_cold int mp_decode_end(AVCodecContext *avctx)
  305. {
  306. MotionPixelsContext *mp = avctx->priv_data;
  307. av_freep(&mp->changes_map);
  308. av_freep(&mp->vpt);
  309. av_freep(&mp->hpt);
  310. av_freep(&mp->bswapbuf);
  311. if (mp->frame.data[0])
  312. avctx->release_buffer(avctx, &mp->frame);
  313. return 0;
  314. }
  315. AVCodec motionpixels_decoder = {
  316. "motionpixels",
  317. CODEC_TYPE_VIDEO,
  318. CODEC_ID_MOTIONPIXELS,
  319. sizeof(MotionPixelsContext),
  320. mp_decode_init,
  321. NULL,
  322. mp_decode_end,
  323. mp_decode_frame,
  324. CODEC_CAP_DR1,
  325. .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels video"),
  326. };