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