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.

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