FFmpeg
Loading...
Searching...
No Matches
xfacedec.c
Go to the documentation of this file.
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 "avcodec.h"
28#include "codec_internal.h"
29#include "decode.h"
30#include "xface.h"
31
32static 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
50static 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 {
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
68static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
69{
72 return;
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
88typedef struct XFaceContext {
89 uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
91
93{
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);
100 }
101 }
102
103 avctx->width = XFACE_WIDTH;
104 avctx->height = XFACE_HEIGHT;
106
107 return 0;
108}
109
111 int *got_frame, AVPacket *avpkt)
112{
113 XFaceContext *xface = avctx->priv_data;
114 int ret, i, j, k;
115 uint8_t byte;
116 BigInt b = {0};
117 char *buf;
118 int64_t c;
119
120 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
121 return ret;
122
123 for (i = 0, k = 0; i < avpkt->size && avpkt->data[i]; i++) {
124 c = avpkt->data[i];
125
126 /* ignore invalid digits */
128 continue;
129
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);
133 break;
134 }
137 }
138
139 /* decode image and put it in bitmap */
140 memset(xface->bitmap, 0, XFACE_PIXELS);
141 buf = xface->bitmap;
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);
151
152 ff_xface_generate_face(xface->bitmap, xface->bitmap);
153
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];
158 if (k == 7) {
159 buf[j++] = byte;
160 byte = k = 0;
161 } else {
162 k++;
163 byte <<= 1;
164 }
165 if (j == XFACE_WIDTH/8) {
166 j = 0;
167 buf += frame->linesize[0];
168 }
169 }
170
171 *got_frame = 1;
172
173 return avpkt->size;
174}
175
177 .p.name = "xface",
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),
185};
const FFCodec ff_xface_decoder
Definition xfacedec.c:176
Libavcodec external API header.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition bytestream.h:99
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_XFACE
Definition codec_id.h:257
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define r
Definition input.c:42
#define b
Definition input.c:43
unsigned offset
Definition libaomenc.c:763
#define av_cold
Definition attributes.h:117
uint8_t w
Definition llvidencdsp.c:39
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:82
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Definition xface.h:61
uint8_t range
Definition xface.h:91
uint8_t offset
Definition xface.h:92
uint8_t bitmap[XFACE_PIXELS]
image used internally for decoding
Definition xfacedec.c:89
uint8_t level
Definition svq3.c:208
#define av_log(a,...)
static double c[64]
void ff_xface_generate_face(uint8_t *dst, uint8_t *const src)
Definition xface.c:286
const ProbRange ff_xface_probranges_per_level[4][3]
Definition xface.c:129
void ff_big_div(BigInt *b, uint8_t a, uint8_t *r)
Divide b by a storing the result in b and the remainder in the word pointed to by r.
Definition xface.c:54
void ff_big_add(BigInt *b, uint8_t a)
Add a to b storing the result in b.
Definition xface.c:31
void ff_big_mul(BigInt *b, uint8_t a)
Multiply a by b storing the result in b.
Definition xface.c:93
const ProbRange ff_xface_probranges_2x2[16]
Definition xface.c:137
X-Face common definitions.
#define XFACE_WIDTH
Definition xface.h:33
#define XFACE_MAX_DIGITS
Definition xface.h:50
#define XFACE_FIRST_PRINT
Definition xface.h:40
#define XFACE_HEIGHT
Definition xface.h:34
@ XFACE_COLOR_BLACK
Definition xface.h:85
@ XFACE_COLOR_WHITE
Definition xface.h:85
#define XFACE_PRINTS
Definition xface.h:42
#define XFACE_PIXELS
Definition xface.h:35
#define XFACE_LAST_PRINT
Definition xface.h:41
static int xface_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition xfacedec.c:110
static void pop_greys(BigInt *b, char *bitmap, int w, int h)
Definition xfacedec.c:50
static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
Definition xfacedec.c:68
static int pop_integer(BigInt *b, const ProbRange *pranges)
Definition xfacedec.c:32
static av_cold int xface_decode_init(AVCodecContext *avctx)
Definition xfacedec.c:92