00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "xface.h"
00028 #include "avcodec.h"
00029 #include "internal.h"
00030
00031 typedef struct XFaceContext {
00032 AVClass *class;
00033 uint8_t bitmap[XFACE_PIXELS];
00034 int max_line_len;
00035 int set_header;
00036 } XFaceContext;
00037
00038 static int all_same(char *bitmap, int w, int h)
00039 {
00040 char val, *row;
00041 int x;
00042
00043 val = *bitmap;
00044 while (h--) {
00045 row = bitmap;
00046 x = w;
00047 while (x--)
00048 if (*(row++) != val)
00049 return 0;
00050 bitmap += XFACE_WIDTH;
00051 }
00052 return 1;
00053 }
00054
00055 static int all_black(char *bitmap, int w, int h)
00056 {
00057 if (w > 3) {
00058 w /= 2;
00059 h /= 2;
00060 return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) &&
00061 all_black(bitmap + XFACE_WIDTH * h, w, h) &&
00062 all_black(bitmap + XFACE_WIDTH * h + w, w, h));
00063 } else {
00064
00065 return *bitmap || *(bitmap + 1) ||
00066 *(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1);
00067 }
00068 }
00069
00070 static int all_white(char *bitmap, int w, int h)
00071 {
00072 return *bitmap == 0 && all_same(bitmap, w, h);
00073 }
00074
00075 typedef struct {
00076 const ProbRange *prob_ranges[XFACE_PIXELS*2];
00077 int prob_ranges_idx;
00078 } ProbRangesQueue;
00079
00080 static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
00081 {
00082 if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
00083 return -1;
00084 pq->prob_ranges[pq->prob_ranges_idx++] = p;
00085 return 0;
00086 }
00087
00088 static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
00089 {
00090 if (w > 3) {
00091 w /= 2;
00092 h /= 2;
00093 push_greys(pq, bitmap, w, h);
00094 push_greys(pq, bitmap + w, w, h);
00095 push_greys(pq, bitmap + XFACE_WIDTH * h, w, h);
00096 push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h);
00097 } else {
00098 const ProbRange *p = ff_xface_probranges_2x2 +
00099 *bitmap +
00100 2 * *(bitmap + 1) +
00101 4 * *(bitmap + XFACE_WIDTH) +
00102 8 * *(bitmap + XFACE_WIDTH + 1);
00103 pq_push(pq, p);
00104 }
00105 }
00106
00107 static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
00108 {
00109 if (all_white(bitmap, w, h)) {
00110 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_WHITE]);
00111 } else if (all_black(bitmap, w, h)) {
00112 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_BLACK]);
00113 push_greys(pq, bitmap, w, h);
00114 } else {
00115 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_GREY]);
00116 w /= 2;
00117 h /= 2;
00118 level++;
00119 encode_block(bitmap, w, h, level, pq);
00120 encode_block(bitmap + w, w, h, level, pq);
00121 encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq);
00122 encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq);
00123 }
00124 }
00125
00126 static av_cold int xface_encode_init(AVCodecContext *avctx)
00127 {
00128 avctx->coded_frame = avcodec_alloc_frame();
00129 if (!avctx->coded_frame)
00130 return AVERROR(ENOMEM);
00131 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00132
00133 return 0;
00134 }
00135
00136 static void push_integer(BigInt *b, const ProbRange *prange)
00137 {
00138 uint8_t r;
00139
00140 ff_big_div(b, prange->range, &r);
00141 ff_big_mul(b, 0);
00142 ff_big_add(b, r + prange->offset);
00143 }
00144
00145 static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00146 const AVFrame *frame, int *got_packet)
00147 {
00148 XFaceContext *xface = avctx->priv_data;
00149 ProbRangesQueue pq = {{ 0 }, 0};
00150 uint8_t bitmap_copy[XFACE_PIXELS];
00151 BigInt b = {0};
00152 int i, j, k, ret = 0;
00153 const uint8_t *buf;
00154 uint8_t *p;
00155 char intbuf[XFACE_MAX_DIGITS];
00156
00157 if (avctx->width || avctx->height) {
00158 if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
00159 av_log(avctx, AV_LOG_ERROR,
00160 "Size value %dx%d not supported, only accepts a size of %dx%d\n",
00161 avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
00162 return AVERROR(EINVAL);
00163 }
00164 }
00165 avctx->width = XFACE_WIDTH;
00166 avctx->height = XFACE_HEIGHT;
00167
00168
00169 buf = frame->data[0];
00170 i = j = 0;
00171 do {
00172 for (k = 0; k < 8; k++)
00173 xface->bitmap[i++] = (buf[j]>>(7-k))&1;
00174 if (++j == XFACE_WIDTH/8) {
00175 buf += frame->linesize[0];
00176 j = 0;
00177 }
00178 } while (i < XFACE_PIXELS);
00179
00180
00181 memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
00182 ff_xface_generate_face(xface->bitmap, bitmap_copy);
00183
00184 encode_block(xface->bitmap, 16, 16, 0, &pq);
00185 encode_block(xface->bitmap + 16, 16, 16, 0, &pq);
00186 encode_block(xface->bitmap + 32, 16, 16, 0, &pq);
00187 encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq);
00188 encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq);
00189 encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq);
00190 encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq);
00191 encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq);
00192 encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq);
00193
00194 while (pq.prob_ranges_idx > 0)
00195 push_integer(&b, pq.prob_ranges[--pq.prob_ranges_idx]);
00196
00197
00198 i = 0;
00199 while (b.nb_words) {
00200 uint8_t r;
00201 ff_big_div(&b, XFACE_PRINTS, &r);
00202 intbuf[i++] = r + XFACE_FIRST_PRINT;
00203 }
00204
00205 if ((ret = ff_alloc_packet2(avctx, pkt, i+2)) < 0)
00206 return ret;
00207
00208
00209 p = pkt->data;
00210 while (--i >= 0)
00211 *(p++) = intbuf[i];
00212 *(p++) = '\n';
00213 *(p++) = 0;
00214
00215 pkt->flags |= AV_PKT_FLAG_KEY;
00216 *got_packet = 1;
00217
00218 return 0;
00219 }
00220
00221 static av_cold int xface_encode_close(AVCodecContext *avctx)
00222 {
00223 av_freep(&avctx->coded_frame);
00224
00225 return 0;
00226 }
00227
00228 AVCodec ff_xface_encoder = {
00229 .name = "xface",
00230 .type = AVMEDIA_TYPE_VIDEO,
00231 .id = AV_CODEC_ID_XFACE,
00232 .priv_data_size = sizeof(XFaceContext),
00233 .init = xface_encode_init,
00234 .close = xface_encode_close,
00235 .encode2 = xface_encode_frame,
00236 .pix_fmts = (const enum PixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
00237 .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
00238 };