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.

242 lines
7.4KB

  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 av_cold int xface_encode_init(AVCodecContext *avctx)
  116. {
  117. avctx->coded_frame = av_frame_alloc();
  118. if (!avctx->coded_frame)
  119. return AVERROR(ENOMEM);
  120. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  121. return 0;
  122. }
  123. static void push_integer(BigInt *b, const ProbRange *prange)
  124. {
  125. uint8_t r;
  126. ff_big_div(b, prange->range, &r);
  127. ff_big_mul(b, 0);
  128. ff_big_add(b, r + prange->offset);
  129. }
  130. static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  131. const AVFrame *frame, int *got_packet)
  132. {
  133. XFaceContext *xface = avctx->priv_data;
  134. ProbRangesQueue pq = {{{ 0 }}, 0};
  135. uint8_t bitmap_copy[XFACE_PIXELS];
  136. BigInt b = {0};
  137. int i, j, k, ret = 0;
  138. const uint8_t *buf;
  139. uint8_t *p;
  140. char intbuf[XFACE_MAX_DIGITS];
  141. if (avctx->width || avctx->height) {
  142. if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
  143. av_log(avctx, AV_LOG_ERROR,
  144. "Size value %dx%d not supported, only accepts a size of %dx%d\n",
  145. avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
  146. return AVERROR(EINVAL);
  147. }
  148. }
  149. avctx->width = XFACE_WIDTH;
  150. avctx->height = XFACE_HEIGHT;
  151. /* convert image from MONOWHITE to 1=black 0=white bitmap */
  152. buf = frame->data[0];
  153. i = j = 0;
  154. do {
  155. for (k = 0; k < 8; k++)
  156. xface->bitmap[i++] = (buf[j]>>(7-k))&1;
  157. if (++j == XFACE_WIDTH/8) {
  158. buf += frame->linesize[0];
  159. j = 0;
  160. }
  161. } while (i < XFACE_PIXELS);
  162. /* create a copy of bitmap */
  163. memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
  164. ff_xface_generate_face(xface->bitmap, bitmap_copy);
  165. encode_block(xface->bitmap, 16, 16, 0, &pq);
  166. encode_block(xface->bitmap + 16, 16, 16, 0, &pq);
  167. encode_block(xface->bitmap + 32, 16, 16, 0, &pq);
  168. encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq);
  169. encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq);
  170. encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq);
  171. encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq);
  172. encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq);
  173. encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq);
  174. while (pq.prob_ranges_idx > 0)
  175. push_integer(&b, &pq.prob_ranges[--pq.prob_ranges_idx]);
  176. /* write the inverted big integer in b to intbuf */
  177. i = 0;
  178. av_assert0(b.nb_words < XFACE_MAX_WORDS);
  179. while (b.nb_words) {
  180. uint8_t r;
  181. ff_big_div(&b, XFACE_PRINTS, &r);
  182. av_assert0(i < sizeof(intbuf));
  183. intbuf[i++] = r + XFACE_FIRST_PRINT;
  184. }
  185. if ((ret = ff_alloc_packet2(avctx, pkt, i+2)) < 0)
  186. return ret;
  187. /* revert the number, and close the buffer */
  188. p = pkt->data;
  189. while (--i >= 0)
  190. *(p++) = intbuf[i];
  191. *(p++) = '\n';
  192. *(p++) = 0;
  193. pkt->flags |= AV_PKT_FLAG_KEY;
  194. *got_packet = 1;
  195. return 0;
  196. }
  197. static av_cold int xface_encode_close(AVCodecContext *avctx)
  198. {
  199. av_frame_free(&avctx->coded_frame);
  200. return 0;
  201. }
  202. AVCodec ff_xface_encoder = {
  203. .name = "xface",
  204. .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
  205. .type = AVMEDIA_TYPE_VIDEO,
  206. .id = AV_CODEC_ID_XFACE,
  207. .priv_data_size = sizeof(XFaceContext),
  208. .init = xface_encode_init,
  209. .close = xface_encode_close,
  210. .encode2 = xface_encode_frame,
  211. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
  212. };