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.

189 lines
5.8KB

  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. uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
  83. } XFaceContext;
  84. static av_cold int xface_decode_init(AVCodecContext *avctx)
  85. {
  86. if (avctx->width || avctx->height) {
  87. if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
  88. av_log(avctx, AV_LOG_ERROR,
  89. "Size value %dx%d not supported, only accepts a size of %dx%d\n",
  90. avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
  91. return AVERROR(EINVAL);
  92. }
  93. }
  94. avctx->width = XFACE_WIDTH;
  95. avctx->height = XFACE_HEIGHT;
  96. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  97. return 0;
  98. }
  99. static int xface_decode_frame(AVCodecContext *avctx,
  100. void *data, int *got_frame,
  101. AVPacket *avpkt)
  102. {
  103. XFaceContext *xface = avctx->priv_data;
  104. int ret, i, j, k;
  105. uint8_t byte;
  106. BigInt b = {0};
  107. char *buf;
  108. int64_t c;
  109. AVFrame *frame = data;
  110. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  111. return ret;
  112. for (i = 0, k = 0; i < avpkt->size && avpkt->data[i]; i++) {
  113. c = avpkt->data[i];
  114. /* ignore invalid digits */
  115. if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT)
  116. continue;
  117. if (++k > XFACE_MAX_DIGITS) {
  118. av_log(avctx, AV_LOG_WARNING,
  119. "Buffer is longer than expected, truncating at byte %d\n", i);
  120. break;
  121. }
  122. ff_big_mul(&b, XFACE_PRINTS);
  123. ff_big_add(&b, c - XFACE_FIRST_PRINT);
  124. }
  125. /* decode image and put it in bitmap */
  126. memset(xface->bitmap, 0, XFACE_PIXELS);
  127. buf = xface->bitmap;
  128. decode_block(&b, buf, 16, 16, 0);
  129. decode_block(&b, buf + 16, 16, 16, 0);
  130. decode_block(&b, buf + 32, 16, 16, 0);
  131. decode_block(&b, buf + XFACE_WIDTH * 16, 16, 16, 0);
  132. decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0);
  133. decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0);
  134. decode_block(&b, buf + XFACE_WIDTH * 32 , 16, 16, 0);
  135. decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0);
  136. decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0);
  137. ff_xface_generate_face(xface->bitmap, xface->bitmap);
  138. /* convert image from 1=black 0=white bitmap to MONOWHITE */
  139. buf = frame->data[0];
  140. for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
  141. byte += xface->bitmap[i];
  142. if (k == 7) {
  143. buf[j++] = byte;
  144. byte = k = 0;
  145. } else {
  146. k++;
  147. byte <<= 1;
  148. }
  149. if (j == XFACE_WIDTH/8) {
  150. j = 0;
  151. buf += frame->linesize[0];
  152. }
  153. }
  154. *got_frame = 1;
  155. return avpkt->size;
  156. }
  157. AVCodec ff_xface_decoder = {
  158. .name = "xface",
  159. .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .id = AV_CODEC_ID_XFACE,
  162. .priv_data_size = sizeof(XFaceContext),
  163. .init = xface_decode_init,
  164. .decode = xface_decode_frame,
  165. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
  166. };