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.

208 lines
6.3KB

  1. /*
  2. * Copyright (c) 1990 James Ashton - Sydney University
  3. * Copyright (c) 2012 Stefano Sabatini
  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. * X-Face decoder, based on libcompface, by James Ashton.
  24. */
  25. #include "libavutil/pixdesc.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "xface.h"
  29. static int pop_integer(BigInt *b, const ProbRange *pranges)
  30. {
  31. uint8_t r;
  32. int i;
  33. /* extract the last byte into r, and shift right b by 8 bits */
  34. ff_big_div(b, 0, &r);
  35. i = 0;
  36. while (r < pranges->offset || r >= pranges->range + pranges->offset) {
  37. pranges++;
  38. i++;
  39. }
  40. ff_big_mul(b, pranges->range);
  41. ff_big_add(b, r - pranges->offset);
  42. return i;
  43. }
  44. static void pop_greys(BigInt *b, char *bitmap, int w, int h)
  45. {
  46. if (w > 3) {
  47. w /= 2;
  48. h /= 2;
  49. pop_greys(b, bitmap, w, h);
  50. pop_greys(b, bitmap + w, w, h);
  51. pop_greys(b, bitmap + XFACE_WIDTH * h, w, h);
  52. pop_greys(b, bitmap + XFACE_WIDTH * h + w, w, h);
  53. } else {
  54. w = pop_integer(b, ff_xface_probranges_2x2);
  55. if (w & 1) bitmap[0] = 1;
  56. if (w & 2) bitmap[1] = 1;
  57. if (w & 4) bitmap[XFACE_WIDTH] = 1;
  58. if (w & 8) bitmap[XFACE_WIDTH + 1] = 1;
  59. }
  60. }
  61. static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
  62. {
  63. switch (pop_integer(b, &ff_xface_probranges_per_level[level][0])) {
  64. case XFACE_COLOR_WHITE:
  65. return;
  66. case XFACE_COLOR_BLACK:
  67. pop_greys(b, bitmap, w, h);
  68. return;
  69. default:
  70. w /= 2;
  71. h /= 2;
  72. level++;
  73. decode_block(b, bitmap, w, h, level);
  74. decode_block(b, bitmap + w, w, h, level);
  75. decode_block(b, bitmap + h * XFACE_WIDTH, w, h, level);
  76. decode_block(b, bitmap + w + h * XFACE_WIDTH, w, h, level);
  77. return;
  78. }
  79. }
  80. typedef struct XFaceContext {
  81. AVFrame frame;
  82. uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
  83. } XFaceContext;
  84. static av_cold int xface_decode_init(AVCodecContext *avctx)
  85. {
  86. XFaceContext *xface = avctx->priv_data;
  87. avcodec_get_frame_defaults(&xface->frame);
  88. if (avctx->width || avctx->height) {
  89. if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
  90. av_log(avctx, AV_LOG_ERROR,
  91. "Size value %dx%d not supported, only accepts a size of %dx%d\n",
  92. avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
  93. return AVERROR(EINVAL);
  94. }
  95. }
  96. avctx->width = XFACE_WIDTH;
  97. avctx->height = XFACE_HEIGHT;
  98. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  99. return 0;
  100. }
  101. static av_cold int xface_decode_close(AVCodecContext *avctx)
  102. {
  103. XFaceContext *xface = avctx->priv_data;
  104. if (xface->frame.data[0])
  105. avctx->release_buffer(avctx, &xface->frame);
  106. return 0;
  107. }
  108. static int xface_decode_frame(AVCodecContext *avctx,
  109. void *data, int *data_size,
  110. AVPacket *avpkt)
  111. {
  112. XFaceContext *xface = avctx->priv_data;
  113. int ret, i, j, k;
  114. uint8_t byte;
  115. BigInt b = {0};
  116. char *buf;
  117. int64_t c;
  118. if (xface->frame.data[0])
  119. avctx->release_buffer(avctx, &xface->frame);
  120. xface->frame.data[0] = NULL;
  121. if ((ret = avctx->get_buffer(avctx, &xface->frame)) < 0)
  122. return ret;
  123. xface->frame.reference = 0;
  124. for (i = 0, k = 0; avpkt->data[i] && i < avpkt->size; i++) {
  125. c = avpkt->data[i];
  126. /* ignore invalid digits */
  127. if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT)
  128. continue;
  129. if (++k > XFACE_MAX_DIGITS) {
  130. av_log(avctx, AV_LOG_WARNING,
  131. "Buffer is longer than expected, truncating at byte %d\n", i);
  132. break;
  133. }
  134. ff_big_mul(&b, XFACE_PRINTS);
  135. ff_big_add(&b, c - XFACE_FIRST_PRINT);
  136. }
  137. /* decode image and put it in bitmap */
  138. memset(xface->bitmap, 0, XFACE_PIXELS);
  139. buf = xface->bitmap;
  140. decode_block(&b, buf, 16, 16, 0);
  141. decode_block(&b, buf + 16, 16, 16, 0);
  142. decode_block(&b, buf + 32, 16, 16, 0);
  143. decode_block(&b, buf + XFACE_WIDTH * 16, 16, 16, 0);
  144. decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0);
  145. decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0);
  146. decode_block(&b, buf + XFACE_WIDTH * 32 , 16, 16, 0);
  147. decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0);
  148. decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0);
  149. ff_xface_generate_face(xface->bitmap, xface->bitmap);
  150. /* convert image from 1=black 0=white bitmap to MONOWHITE */
  151. buf = xface->frame.data[0];
  152. for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
  153. byte += xface->bitmap[i];
  154. if (k == 7) {
  155. buf[j++] = byte;
  156. byte = k = 0;
  157. } else {
  158. k++;
  159. byte <<= 1;
  160. }
  161. if (j == XFACE_WIDTH/8) {
  162. j = 0;
  163. buf += xface->frame.linesize[0];
  164. }
  165. }
  166. *data_size = sizeof(AVFrame);
  167. *(AVFrame*)data = xface->frame;
  168. return avpkt->size;
  169. }
  170. AVCodec ff_xface_decoder = {
  171. .name = "xface",
  172. .type = AVMEDIA_TYPE_VIDEO,
  173. .id = AV_CODEC_ID_XFACE,
  174. .priv_data_size = sizeof(XFaceContext),
  175. .init = xface_decode_init,
  176. .close = xface_decode_close,
  177. .decode = xface_decode_frame,
  178. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
  179. .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
  180. };