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.

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