2 * Copyright (c) 1990 James Ashton - Sydney University
3 * Copyright (c) 2012 Stefano Sabatini
5 * This file is part of FFmpeg.
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.
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.
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
24 * X-Face decoder, based on libcompface, by James Ashton.
28 #include "codec_internal.h"
32 static int pop_integer(BigInt
*b
, const ProbRange
*pranges
)
37 /* extract the last byte into r, and shift right b by 8 bits */
41 while (r
< pranges
->offset
|| r
>= pranges
->range
+ pranges
->offset
) {
45 ff_big_mul(b
, pranges
->range
);
46 ff_big_add(b
, r
- pranges
->offset
);
50 static void pop_greys(BigInt
*b
, char *bitmap
, int w
, int h
)
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
);
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;
68 static void decode_block(BigInt
*b
, char *bitmap
, int w
, int h
, int level
)
70 switch (pop_integer(b
, &ff_xface_probranges_per_level
[level
][0])) {
71 case XFACE_COLOR_WHITE
:
73 case XFACE_COLOR_BLACK
:
74 pop_greys(b
, bitmap
, w
, h
);
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
);
88 typedef struct XFaceContext
{
89 uint8_t bitmap
[XFACE_PIXELS
]; ///< image used internally for decoding
92 static av_cold
int xface_decode_init(AVCodecContext
*avctx
)
94 if (avctx
->width
|| avctx
->height
) {
95 if (avctx
->width
!= XFACE_WIDTH
|| avctx
->height
!= XFACE_HEIGHT
) {
96 av_log(avctx
, AV_LOG_ERROR
,
97 "Size value %dx%d not supported, only accepts a size of %dx%d\n",
98 avctx
->width
, avctx
->height
, XFACE_WIDTH
, XFACE_HEIGHT
);
99 return AVERROR(EINVAL
);
103 avctx
->width
= XFACE_WIDTH
;
104 avctx
->height
= XFACE_HEIGHT
;
105 avctx
->pix_fmt
= AV_PIX_FMT_MONOWHITE
;
110 static int xface_decode_frame(AVCodecContext
*avctx
, AVFrame
*frame
,
111 int *got_frame
, AVPacket
*avpkt
)
113 XFaceContext
*xface
= avctx
->priv_data
;
120 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
123 for (i
= 0, k
= 0; i
< avpkt
->size
&& avpkt
->data
[i
]; i
++) {
126 /* ignore invalid digits */
127 if (c
< XFACE_FIRST_PRINT
|| c
> XFACE_LAST_PRINT
)
130 if (++k
> XFACE_MAX_DIGITS
) {
131 av_log(avctx
, AV_LOG_WARNING
,
132 "Buffer is longer than expected, truncating at byte %d\n", i
);
135 ff_big_mul(&b
, XFACE_PRINTS
);
136 ff_big_add(&b
, c
- XFACE_FIRST_PRINT
);
139 /* decode image and put it in bitmap */
140 memset(xface
->bitmap
, 0, XFACE_PIXELS
);
142 decode_block(&b
, buf
, 16, 16, 0);
143 decode_block(&b
, buf
+ 16, 16, 16, 0);
144 decode_block(&b
, buf
+ 32, 16, 16, 0);
145 decode_block(&b
, buf
+ XFACE_WIDTH
* 16, 16, 16, 0);
146 decode_block(&b
, buf
+ XFACE_WIDTH
* 16 + 16, 16, 16, 0);
147 decode_block(&b
, buf
+ XFACE_WIDTH
* 16 + 32, 16, 16, 0);
148 decode_block(&b
, buf
+ XFACE_WIDTH
* 32 , 16, 16, 0);
149 decode_block(&b
, buf
+ XFACE_WIDTH
* 32 + 16, 16, 16, 0);
150 decode_block(&b
, buf
+ XFACE_WIDTH
* 32 + 32, 16, 16, 0);
152 ff_xface_generate_face(xface
->bitmap
, xface
->bitmap
);
154 /* convert image from 1=black 0=white bitmap to MONOWHITE */
155 buf
= frame
->data
[0];
156 for (i
= 0, j
= 0, k
= 0, byte
= 0; i
< XFACE_PIXELS
; i
++) {
157 byte
+= xface
->bitmap
[i
];
165 if (j
== XFACE_WIDTH
/8) {
167 buf
+= frame
->linesize
[0];
176 const FFCodec ff_xface_decoder
= {
178 CODEC_LONG_NAME("X-face image"),
179 .p
.type
= AVMEDIA_TYPE_VIDEO
,
180 .p
.id
= AV_CODEC_ID_XFACE
,
181 .p
.capabilities
= AV_CODEC_CAP_DR1
,
182 .priv_data_size
= sizeof(XFaceContext
),
183 .init
= xface_decode_init
,
184 FF_CODEC_DECODE_CB(xface_decode_frame
),