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.

340 lines
11KB

  1. /*
  2. * Motion Pixels Video Decoder
  3. * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
  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. #include "avcodec.h"
  22. #include "bitstream.h"
  23. #include "bswapdsp.h"
  24. #include "internal.h"
  25. #include "vlc.h"
  26. #define MAX_HUFF_CODES 16
  27. #include "motionpixels_tablegen.h"
  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. BswapDSPContext bdsp;
  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 av_cold int mp_decode_end(AVCodecContext *avctx)
  49. {
  50. MotionPixelsContext *mp = avctx->priv_data;
  51. av_freep(&mp->changes_map);
  52. av_freep(&mp->vpt);
  53. av_freep(&mp->hpt);
  54. av_freep(&mp->bswapbuf);
  55. av_frame_free(&mp->frame);
  56. return 0;
  57. }
  58. static av_cold int mp_decode_init(AVCodecContext *avctx)
  59. {
  60. MotionPixelsContext *mp = avctx->priv_data;
  61. int w4 = (avctx->width + 3) & ~3;
  62. int h4 = (avctx->height + 3) & ~3;
  63. motionpixels_tableinit();
  64. mp->avctx = avctx;
  65. ff_bswapdsp_init(&mp->bdsp);
  66. mp->changes_map = av_mallocz(avctx->width * h4);
  67. mp->offset_bits_len = av_log2(avctx->width * avctx->height) + 1;
  68. mp->vpt = av_mallocz(avctx->height * sizeof(YuvPixel));
  69. mp->hpt = av_mallocz(h4 * w4 / 16 * sizeof(YuvPixel));
  70. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  71. mp->frame = av_frame_alloc();
  72. if (!mp->frame) {
  73. mp_decode_end(avctx);
  74. return AVERROR(ENOMEM);
  75. }
  76. return 0;
  77. }
  78. static void mp_read_changes_map(MotionPixelsContext *mp, BitstreamContext *bc,
  79. int count, int bits_len, int read_color)
  80. {
  81. uint16_t *pixels;
  82. int offset, w, h, color = 0, x, y, i;
  83. while (count--) {
  84. offset = bitstream_read(bc, mp->offset_bits_len);
  85. w = bitstream_read(bc, bits_len) + 1;
  86. h = bitstream_read(bc, bits_len) + 1;
  87. if (read_color)
  88. color = bitstream_read(bc, 15);
  89. x = offset % mp->avctx->width;
  90. y = offset / mp->avctx->width;
  91. if (y >= mp->avctx->height)
  92. continue;
  93. w = FFMIN(w, mp->avctx->width - x);
  94. h = FFMIN(h, mp->avctx->height - y);
  95. pixels = (uint16_t *)&mp->frame->data[0][y * mp->frame->linesize[0] + x * 2];
  96. while (h--) {
  97. mp->changes_map[offset] = w;
  98. if (read_color)
  99. for (i = 0; i < w; ++i)
  100. pixels[i] = color;
  101. offset += mp->avctx->width;
  102. pixels += mp->frame->linesize[0] / 2;
  103. }
  104. }
  105. }
  106. static void mp_get_code(MotionPixelsContext *mp, BitstreamContext *bc,
  107. int size, int code)
  108. {
  109. while (bitstream_read_bit(bc)) {
  110. ++size;
  111. if (size > mp->max_codes_bits) {
  112. av_log(mp->avctx, AV_LOG_ERROR, "invalid code size %d/%d\n", size, mp->max_codes_bits);
  113. return;
  114. }
  115. code <<= 1;
  116. mp_get_code(mp, bc, size, code + 1);
  117. }
  118. if (mp->current_codes_count >= MAX_HUFF_CODES) {
  119. av_log(mp->avctx, AV_LOG_ERROR, "too many codes\n");
  120. return;
  121. }
  122. mp->codes[mp->current_codes_count ].code = code;
  123. mp->codes[mp->current_codes_count++].size = size;
  124. }
  125. static void mp_read_codes_table(MotionPixelsContext *mp, BitstreamContext *bc)
  126. {
  127. if (mp->codes_count == 1) {
  128. mp->codes[0].delta = bitstream_read(bc, 4);
  129. } else {
  130. int i;
  131. mp->max_codes_bits = bitstream_read(bc, 4);
  132. for (i = 0; i < mp->codes_count; ++i)
  133. mp->codes[i].delta = bitstream_read(bc, 4);
  134. mp->current_codes_count = 0;
  135. mp_get_code(mp, bc, 0, 0);
  136. }
  137. }
  138. static int mp_gradient(MotionPixelsContext *mp, int component, int v)
  139. {
  140. int delta;
  141. delta = (v - 7) * mp->gradient_scale[component];
  142. mp->gradient_scale[component] = (v == 0 || v == 14) ? 2 : 1;
  143. return delta;
  144. }
  145. static YuvPixel mp_get_yuv_from_rgb(MotionPixelsContext *mp, int x, int y)
  146. {
  147. int color;
  148. color = *(uint16_t *)&mp->frame->data[0][y * mp->frame->linesize[0] + x * 2];
  149. return mp_rgb_yuv_table[color];
  150. }
  151. static void mp_set_rgb_from_yuv(MotionPixelsContext *mp, int x, int y, const YuvPixel *p)
  152. {
  153. int color;
  154. color = mp_yuv_to_rgb(p->y, p->v, p->u, 1);
  155. *(uint16_t *)&mp->frame->data[0][y * mp->frame->linesize[0] + x * 2] = color;
  156. }
  157. static int mp_get_vlc(MotionPixelsContext *mp, BitstreamContext *bc)
  158. {
  159. int i;
  160. i = (mp->codes_count == 1) ? 0 : bitstream_read_vlc(bc, mp->vlc.table, mp->max_codes_bits, 1);
  161. i = FFMIN(i, FF_ARRAY_ELEMS(mp->codes) - 1);
  162. return mp->codes[i].delta;
  163. }
  164. static void mp_decode_line(MotionPixelsContext *mp, BitstreamContext *bc, int y)
  165. {
  166. YuvPixel p;
  167. const int y0 = y * mp->avctx->width;
  168. int w, i, x = 0;
  169. p = mp->vpt[y];
  170. if (mp->changes_map[y0 + x] == 0) {
  171. memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
  172. ++x;
  173. }
  174. while (x < mp->avctx->width) {
  175. w = mp->changes_map[y0 + x];
  176. if (w != 0) {
  177. if ((y & 3) == 0) {
  178. if (mp->changes_map[y0 + x + mp->avctx->width] < w ||
  179. mp->changes_map[y0 + x + mp->avctx->width * 2] < w ||
  180. mp->changes_map[y0 + x + mp->avctx->width * 3] < w) {
  181. for (i = (x + 3) & ~3; i < x + w; i += 4) {
  182. mp->hpt[((y / 4) * mp->avctx->width + i) / 4] = mp_get_yuv_from_rgb(mp, i, y);
  183. }
  184. }
  185. }
  186. x += w;
  187. memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
  188. p = mp_get_yuv_from_rgb(mp, x - 1, y);
  189. } else {
  190. p.y += mp_gradient(mp, 0, mp_get_vlc(mp, bc));
  191. p.y = av_clip_uintp2(p.y, 5);
  192. if ((x & 3) == 0) {
  193. if ((y & 3) == 0) {
  194. p.v += mp_gradient(mp, 1, mp_get_vlc(mp, bc));
  195. p.v = av_clip_intp2(p.v, 5);
  196. p.u += mp_gradient(mp, 2, mp_get_vlc(mp, bc));
  197. p.u = av_clip_intp2(p.u, 5);
  198. mp->hpt[((y / 4) * mp->avctx->width + x) / 4] = p;
  199. } else {
  200. p.v = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].v;
  201. p.u = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].u;
  202. }
  203. }
  204. mp_set_rgb_from_yuv(mp, x, y, &p);
  205. ++x;
  206. }
  207. }
  208. }
  209. static void mp_decode_frame_helper(MotionPixelsContext *mp,
  210. BitstreamContext *bc)
  211. {
  212. YuvPixel p;
  213. int y, y0;
  214. for (y = 0; y < mp->avctx->height; ++y) {
  215. if (mp->changes_map[y * mp->avctx->width] != 0) {
  216. memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
  217. p = mp_get_yuv_from_rgb(mp, 0, y);
  218. } else {
  219. p.y += mp_gradient(mp, 0, mp_get_vlc(mp, bc));
  220. p.y = av_clip_uintp2(p.y, 5);
  221. if ((y & 3) == 0) {
  222. p.v += mp_gradient(mp, 1, mp_get_vlc(mp, bc));
  223. p.v = av_clip_intp2(p.v, 5);
  224. p.u += mp_gradient(mp, 2, mp_get_vlc(mp, bc));
  225. p.u = av_clip_intp2(p.u, 5);
  226. }
  227. mp->vpt[y] = p;
  228. mp_set_rgb_from_yuv(mp, 0, y, &p);
  229. }
  230. }
  231. for (y0 = 0; y0 < 2; ++y0)
  232. for (y = y0; y < mp->avctx->height; y += 2)
  233. mp_decode_line(mp, bc, y);
  234. }
  235. static int mp_decode_frame(AVCodecContext *avctx,
  236. void *data, int *got_frame,
  237. AVPacket *avpkt)
  238. {
  239. const uint8_t *buf = avpkt->data;
  240. int buf_size = avpkt->size;
  241. MotionPixelsContext *mp = avctx->priv_data;
  242. BitstreamContext bc;
  243. int i, count1, count2, sz, ret;
  244. if ((ret = ff_reget_buffer(avctx, mp->frame)) < 0) {
  245. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  246. return ret;
  247. }
  248. /* le32 bitstream msb first */
  249. av_fast_malloc(&mp->bswapbuf, &mp->bswapbuf_size, buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  250. if (!mp->bswapbuf)
  251. return AVERROR(ENOMEM);
  252. mp->bdsp.bswap_buf((uint32_t *) mp->bswapbuf, (const uint32_t *) buf,
  253. buf_size / 4);
  254. if (buf_size & 3)
  255. memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3);
  256. memset(mp->bswapbuf + buf_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  257. bitstream_init8(&bc, mp->bswapbuf, buf_size);
  258. memset(mp->changes_map, 0, avctx->width * avctx->height);
  259. for (i = !(avctx->extradata[1] & 2); i < 2; ++i) {
  260. count1 = bitstream_read(&bc, 12);
  261. count2 = bitstream_read(&bc, 12);
  262. mp_read_changes_map(mp, &bc, count1, 8, i);
  263. mp_read_changes_map(mp, &bc, count2, 4, i);
  264. }
  265. mp->codes_count = bitstream_read(&bc, 4);
  266. if (mp->codes_count == 0)
  267. goto end;
  268. if (mp->changes_map[0] == 0) {
  269. *(uint16_t *)mp->frame->data[0] = bitstream_read(&bc, 15);
  270. mp->changes_map[0] = 1;
  271. }
  272. mp_read_codes_table(mp, &bc);
  273. sz = bitstream_read(&bc, 18);
  274. if (avctx->extradata[0] != 5)
  275. sz += bitstream_read(&bc, 18);
  276. if (sz == 0)
  277. goto end;
  278. if (mp->max_codes_bits <= 0)
  279. goto end;
  280. 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))
  281. goto end;
  282. mp_decode_frame_helper(mp, &bc);
  283. ff_free_vlc(&mp->vlc);
  284. end:
  285. if ((ret = av_frame_ref(data, mp->frame)) < 0)
  286. return ret;
  287. *got_frame = 1;
  288. return buf_size;
  289. }
  290. AVCodec ff_motionpixels_decoder = {
  291. .name = "motionpixels",
  292. .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels video"),
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .id = AV_CODEC_ID_MOTIONPIXELS,
  295. .priv_data_size = sizeof(MotionPixelsContext),
  296. .init = mp_decode_init,
  297. .close = mp_decode_end,
  298. .decode = mp_decode_frame,
  299. .capabilities = AV_CODEC_CAP_DR1,
  300. };