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