xref: /third_party/ffmpeg/libavcodec/xfacedec.c (revision cabdff1a)
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/**
23 * @file
24 * X-Face decoder, based on libcompface, by James Ashton.
25 */
26
27#include "libavutil/pixdesc.h"
28#include "avcodec.h"
29#include "bytestream.h"
30#include "codec_internal.h"
31#include "internal.h"
32#include "xface.h"
33
34static int pop_integer(BigInt *b, const ProbRange *pranges)
35{
36    uint8_t r;
37    int i;
38
39    /* extract the last byte into r, and shift right b by 8 bits */
40    ff_big_div(b, 0, &r);
41
42    i = 0;
43    while (r < pranges->offset || r >= pranges->range + pranges->offset) {
44        pranges++;
45        i++;
46    }
47    ff_big_mul(b, pranges->range);
48    ff_big_add(b, r - pranges->offset);
49    return i;
50}
51
52static void pop_greys(BigInt *b, char *bitmap, int w, int h)
53{
54    if (w > 3) {
55        w /= 2;
56        h /= 2;
57        pop_greys(b, bitmap,                       w, h);
58        pop_greys(b, bitmap + w,                   w, h);
59        pop_greys(b, bitmap + XFACE_WIDTH * h,     w, h);
60        pop_greys(b, bitmap + XFACE_WIDTH * h + w, w, h);
61    } else {
62        w = pop_integer(b, ff_xface_probranges_2x2);
63        if (w & 1) bitmap[0]               = 1;
64        if (w & 2) bitmap[1]               = 1;
65        if (w & 4) bitmap[XFACE_WIDTH]     = 1;
66        if (w & 8) bitmap[XFACE_WIDTH + 1] = 1;
67    }
68}
69
70static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
71{
72    switch (pop_integer(b, &ff_xface_probranges_per_level[level][0])) {
73    case XFACE_COLOR_WHITE:
74        return;
75    case XFACE_COLOR_BLACK:
76        pop_greys(b, bitmap, w, h);
77        return;
78    default:
79        w /= 2;
80        h /= 2;
81        level++;
82        decode_block(b, bitmap,                       w, h, level);
83        decode_block(b, bitmap + w,                   w, h, level);
84        decode_block(b, bitmap + h * XFACE_WIDTH,     w, h, level);
85        decode_block(b, bitmap + w + h * XFACE_WIDTH, w, h, level);
86        return;
87    }
88}
89
90typedef struct XFaceContext {
91    uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
92} XFaceContext;
93
94static av_cold int xface_decode_init(AVCodecContext *avctx)
95{
96    if (avctx->width || avctx->height) {
97        if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
98            av_log(avctx, AV_LOG_ERROR,
99                   "Size value %dx%d not supported, only accepts a size of %dx%d\n",
100                   avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
101            return AVERROR(EINVAL);
102        }
103    }
104
105    avctx->width   = XFACE_WIDTH;
106    avctx->height  = XFACE_HEIGHT;
107    avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
108
109    return 0;
110}
111
112static int xface_decode_frame(AVCodecContext *avctx, AVFrame *frame,
113                              int *got_frame, AVPacket *avpkt)
114{
115    XFaceContext *xface = avctx->priv_data;
116    int ret, i, j, k;
117    uint8_t byte;
118    BigInt b = {0};
119    char *buf;
120    int64_t c;
121
122    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
123        return ret;
124
125    for (i = 0, k = 0; i < avpkt->size && avpkt->data[i]; i++) {
126        c = avpkt->data[i];
127
128        /* ignore invalid digits */
129        if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT)
130            continue;
131
132        if (++k > XFACE_MAX_DIGITS) {
133            av_log(avctx, AV_LOG_WARNING,
134                   "Buffer is longer than expected, truncating at byte %d\n", i);
135            break;
136        }
137        ff_big_mul(&b, XFACE_PRINTS);
138        ff_big_add(&b, c - XFACE_FIRST_PRINT);
139    }
140
141    /* decode image and put it in bitmap */
142    memset(xface->bitmap, 0, XFACE_PIXELS);
143    buf = xface->bitmap;
144    decode_block(&b, buf,                         16, 16, 0);
145    decode_block(&b, buf + 16,                    16, 16, 0);
146    decode_block(&b, buf + 32,                    16, 16, 0);
147    decode_block(&b, buf + XFACE_WIDTH * 16,      16, 16, 0);
148    decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0);
149    decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0);
150    decode_block(&b, buf + XFACE_WIDTH * 32     , 16, 16, 0);
151    decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0);
152    decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0);
153
154    ff_xface_generate_face(xface->bitmap, xface->bitmap);
155
156    /* convert image from 1=black 0=white bitmap to MONOWHITE */
157    buf = frame->data[0];
158    for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
159        byte += xface->bitmap[i];
160        if (k == 7) {
161            buf[j++] = byte;
162            byte = k = 0;
163        } else {
164            k++;
165            byte <<= 1;
166        }
167        if (j == XFACE_WIDTH/8) {
168            j = 0;
169            buf += frame->linesize[0];
170        }
171    }
172
173    *got_frame = 1;
174
175    return avpkt->size;
176}
177
178const FFCodec ff_xface_decoder = {
179    .p.name         = "xface",
180    .p.long_name    = NULL_IF_CONFIG_SMALL("X-face image"),
181    .p.type         = AVMEDIA_TYPE_VIDEO,
182    .p.id           = AV_CODEC_ID_XFACE,
183    .p.capabilities = AV_CODEC_CAP_DR1,
184    .priv_data_size = sizeof(XFaceContext),
185    .init           = xface_decode_init,
186    FF_CODEC_DECODE_CB(xface_decode_frame),
187    .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
188    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
189};
190