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.

199 lines
6.0KB

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