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.

224 lines
7.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 encoder, based on libcompface, by James Ashton.
  24. */
  25. #include "xface.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. #include "libavutil/avassert.h"
  29. typedef struct XFaceContext {
  30. AVClass *class;
  31. uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
  32. int max_line_len; ///< max line length for compressed data
  33. int set_header; ///< set X-Face header in the output
  34. } XFaceContext;
  35. static int all_same(char *bitmap, int w, int h)
  36. {
  37. char val, *row;
  38. int x;
  39. val = *bitmap;
  40. while (h--) {
  41. row = bitmap;
  42. x = w;
  43. while (x--)
  44. if (*(row++) != val)
  45. return 0;
  46. bitmap += XFACE_WIDTH;
  47. }
  48. return 1;
  49. }
  50. static int all_black(char *bitmap, int w, int h)
  51. {
  52. if (w > 3) {
  53. w /= 2;
  54. h /= 2;
  55. return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) &&
  56. all_black(bitmap + XFACE_WIDTH * h, w, h) &&
  57. all_black(bitmap + XFACE_WIDTH * h + w, w, h));
  58. } else {
  59. /* at least one pixel in the 2x2 grid is non-zero */
  60. return *bitmap || *(bitmap + 1) ||
  61. *(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1);
  62. }
  63. }
  64. static int all_white(char *bitmap, int w, int h)
  65. {
  66. return *bitmap == 0 && all_same(bitmap, w, h);
  67. }
  68. typedef struct {
  69. ProbRange prob_ranges[XFACE_PIXELS*2];
  70. int prob_ranges_idx;
  71. } ProbRangesQueue;
  72. static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
  73. {
  74. if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
  75. return -1;
  76. pq->prob_ranges[pq->prob_ranges_idx++] = *p;
  77. return 0;
  78. }
  79. static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
  80. {
  81. if (w > 3) {
  82. w /= 2;
  83. h /= 2;
  84. push_greys(pq, bitmap, w, h);
  85. push_greys(pq, bitmap + w, w, h);
  86. push_greys(pq, bitmap + XFACE_WIDTH * h, w, h);
  87. push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h);
  88. } else {
  89. const ProbRange *p = ff_xface_probranges_2x2 +
  90. *bitmap +
  91. 2 * *(bitmap + 1) +
  92. 4 * *(bitmap + XFACE_WIDTH) +
  93. 8 * *(bitmap + XFACE_WIDTH + 1);
  94. pq_push(pq, p);
  95. }
  96. }
  97. static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
  98. {
  99. if (all_white(bitmap, w, h)) {
  100. pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_WHITE]);
  101. } else if (all_black(bitmap, w, h)) {
  102. pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_BLACK]);
  103. push_greys(pq, bitmap, w, h);
  104. } else {
  105. pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_GREY]);
  106. w /= 2;
  107. h /= 2;
  108. level++;
  109. encode_block(bitmap, w, h, level, pq);
  110. encode_block(bitmap + w, w, h, level, pq);
  111. encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq);
  112. encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq);
  113. }
  114. }
  115. static void push_integer(BigInt *b, const ProbRange *prange)
  116. {
  117. uint8_t r;
  118. ff_big_div(b, prange->range, &r);
  119. ff_big_mul(b, 0);
  120. ff_big_add(b, r + prange->offset);
  121. }
  122. static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  123. const AVFrame *frame, int *got_packet)
  124. {
  125. XFaceContext *xface = avctx->priv_data;
  126. ProbRangesQueue pq = {{{ 0 }}, 0};
  127. uint8_t bitmap_copy[XFACE_PIXELS];
  128. BigInt b = {0};
  129. int i, j, k, ret = 0;
  130. const uint8_t *buf;
  131. uint8_t *p;
  132. char intbuf[XFACE_MAX_DIGITS];
  133. if (avctx->width || avctx->height) {
  134. if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
  135. av_log(avctx, AV_LOG_ERROR,
  136. "Size value %dx%d not supported, only accepts a size of %dx%d\n",
  137. avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
  138. return AVERROR(EINVAL);
  139. }
  140. }
  141. avctx->width = XFACE_WIDTH;
  142. avctx->height = XFACE_HEIGHT;
  143. /* convert image from MONOWHITE to 1=black 0=white bitmap */
  144. buf = frame->data[0];
  145. i = j = 0;
  146. do {
  147. for (k = 0; k < 8; k++)
  148. xface->bitmap[i++] = (buf[j]>>(7-k))&1;
  149. if (++j == XFACE_WIDTH/8) {
  150. buf += frame->linesize[0];
  151. j = 0;
  152. }
  153. } while (i < XFACE_PIXELS);
  154. /* create a copy of bitmap */
  155. memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
  156. ff_xface_generate_face(xface->bitmap, bitmap_copy);
  157. encode_block(xface->bitmap, 16, 16, 0, &pq);
  158. encode_block(xface->bitmap + 16, 16, 16, 0, &pq);
  159. encode_block(xface->bitmap + 32, 16, 16, 0, &pq);
  160. encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq);
  161. encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq);
  162. encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq);
  163. encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq);
  164. encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq);
  165. encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq);
  166. while (pq.prob_ranges_idx > 0)
  167. push_integer(&b, &pq.prob_ranges[--pq.prob_ranges_idx]);
  168. /* write the inverted big integer in b to intbuf */
  169. i = 0;
  170. av_assert0(b.nb_words < XFACE_MAX_WORDS);
  171. while (b.nb_words) {
  172. uint8_t r;
  173. ff_big_div(&b, XFACE_PRINTS, &r);
  174. av_assert0(i < sizeof(intbuf));
  175. intbuf[i++] = r + XFACE_FIRST_PRINT;
  176. }
  177. if ((ret = ff_alloc_packet2(avctx, pkt, i+2, 0)) < 0)
  178. return ret;
  179. /* revert the number, and close the buffer */
  180. p = pkt->data;
  181. while (--i >= 0)
  182. *(p++) = intbuf[i];
  183. *(p++) = '\n';
  184. *(p++) = 0;
  185. pkt->flags |= AV_PKT_FLAG_KEY;
  186. *got_packet = 1;
  187. return 0;
  188. }
  189. AVCodec ff_xface_encoder = {
  190. .name = "xface",
  191. .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .id = AV_CODEC_ID_XFACE,
  194. .priv_data_size = sizeof(XFaceContext),
  195. .encode2 = xface_encode_frame,
  196. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
  197. .capabilities = AV_CODEC_CAP_INTRA_ONLY,
  198. };