Commit cd559bb4 authored by Stefano Sabatini's avatar Stefano Sabatini

lavc: add xface image decoder and encoder

Based on libcompface code by James Ashton <James.Ashton@anu.edu.au>, and
relicensed to LGPL with the author's consent.
parent 35782bfb
......@@ -11,6 +11,7 @@ version next:
- TAK demuxer, decoder and parser
- DTS-HD demuxer
- remove -same_quant, it hasn't worked for years
- X-Face image encoder and decoder
version 1.0:
......
......@@ -441,6 +441,8 @@ following image formats are supported:
@tab Targa (.TGA) image format
@item XBM @tab X @tab X
@tab X BitMap image format
@item XFace @tab X @tab X
@tab X-Face image format
@item XWD @tab X @tab X
@tab X Window Dump image format
@end multitable
......
......@@ -482,6 +482,8 @@ OBJS-$(CONFIG_XAN_WC4_DECODER) += xxan.o
OBJS-$(CONFIG_XBIN_DECODER) += bintext.o cga_data.o
OBJS-$(CONFIG_XBM_DECODER) += xbmdec.o
OBJS-$(CONFIG_XBM_ENCODER) += xbmenc.o
OBJS-$(CONFIG_XFACE_DECODER) += xfacedec.o xface.o
OBJS-$(CONFIG_XFACE_ENCODER) += xfaceenc.o xface.o
OBJS-$(CONFIG_XL_DECODER) += xl.o
OBJS-$(CONFIG_XSUB_DECODER) += xsubdec.o
OBJS-$(CONFIG_XSUB_ENCODER) += xsubenc.o
......
......@@ -265,6 +265,7 @@ void avcodec_register_all(void)
REGISTER_DECODER (XAN_WC3, xan_wc3);
REGISTER_DECODER (XAN_WC4, xan_wc4);
REGISTER_ENCDEC (XBM, xbm);
REGISTER_ENCDEC (XFACE, xface);
REGISTER_DECODER (XL, xl);
REGISTER_ENCDEC (XWD, xwd);
REGISTER_ENCDEC (Y41P, y41p);
......
......@@ -283,6 +283,7 @@ enum AVCodecID {
AV_CODEC_ID_PAF_VIDEO = MKBETAG('P','A','F','V'),
AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'),
AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'),
AV_CODEC_ID_XFACE = MKBETAG('X','F','A','C'),
/* various PCM "codecs" */
AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
......
......@@ -1315,6 +1315,12 @@ static const AVCodecDescriptor codec_descriptors[] = {
.name = "cpia",
.long_name = NULL_IF_CONFIG_SMALL("CPiA video format"),
},
{
.id = AV_CODEC_ID_XFACE,
.type = AVMEDIA_TYPE_VIDEO,
.name = "xface",
.long_name = NULL_IF_CONFIG_SMALL("X-face image"),
},
/* various PCM "codecs" */
{
......
......@@ -29,7 +29,7 @@
#include "libavutil/avutil.h"
#define LIBAVCODEC_VERSION_MAJOR 54
#define LIBAVCODEC_VERSION_MINOR 65
#define LIBAVCODEC_VERSION_MINOR 66
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
......
This diff is collapsed.
/*
* Copyright (c) 1990 James Ashton - Sydney University
* Copyright (c) 2012 Stefano Sabatini
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* X-Face common definitions.
*/
#include <stdint.h>
/* define the face size - 48x48x1 */
#define XFACE_WIDTH 48
#define XFACE_HEIGHT 48
#define XFACE_PIXELS (XFACE_WIDTH * XFACE_HEIGHT)
/* compressed output uses the full range of printable characters.
* In ASCII these are in a contiguous block so we just need to know
* the first and last. The total number of printables is needed too. */
#define XFACE_FIRST_PRINT '!'
#define XFACE_LAST_PRINT '~'
#define XFACE_PRINTS (XFACE_LAST_PRINT - XFACE_FIRST_PRINT + 1)
/*
* Image is encoded as a big integer, using characters from '~' to
* '!', for a total of 92 symbols. In order to express 48x48=2304
* bits, we need a total of 354 digits, as given by:
* ceil(lg_92(2^2304)) = 354
*/
#define XFACE_MAX_DIGITS 354
#define XFACE_BITSPERWORD 8
#define XFACE_WORDCARRY (1 << XFACE_BITSPERWORD)
#define XFACE_WORDMASK (XFACE_WORDCARRY - 1)
#define XFACE_MAX_WORDS ((XFACE_PIXELS * 2 + XFACE_BITSPERWORD - 1) / XFACE_BITSPERWORD)
/* Portable, very large unsigned integer arithmetic is needed.
* Implementation uses arrays of WORDs. */
typedef struct {
int nb_words;
uint8_t words[XFACE_MAX_WORDS];
} BigInt;
/**
* Add a to b storing the result in b.
*/
void ff_big_add(BigInt *b, uint8_t a);
/**
* Divide b by a storing the result in b and the remainder in the word
* pointed to by r.
*/
void ff_big_div(BigInt *b, uint8_t a, uint8_t *r);
/**
* Multiply a by b storing the result in b.
*/
void ff_big_mul(BigInt *b, uint8_t a);
/* Each face is encoded using 9 octrees of 16x16 each. Each level of the
* trees has varying probabilities of being white, grey or black.
* The table below is based on sampling many faces */
enum XFaceColor { XFACE_COLOR_BLACK = 0, XFACE_COLOR_GREY, XFACE_COLOR_WHITE };
/* Data of varying probabilities are encoded by a value in the range 0 - 255.
* The probability of the data determines the range of possible encodings.
* Offset gives the first possible encoding of the range. */
typedef struct {
int range;
int offset;
} ProbRange;
extern const ProbRange ff_xface_probranges_per_level[4][3];
extern const ProbRange ff_xface_probranges_2x2[16];
void ff_xface_generate_face(uint8_t *dst, uint8_t * const src);
/*
* Copyright (c) 1990 James Ashton - Sydney University
* Copyright (c) 2012 Stefano Sabatini
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* X-Face decoder, based on libcompface, by James Ashton.
*/
#include "libavutil/pixdesc.h"
#include "avcodec.h"
#include "bytestream.h"
#include "xface.h"
static int pop_integer(BigInt *b, const ProbRange *pranges)
{
uint8_t r;
int i;
/* extract the last byte into r, and shift right b by 8 bits */
ff_big_div(b, 0, &r);
i = 0;
while (r < pranges->offset || r >= pranges->range + pranges->offset) {
pranges++;
i++;
}
ff_big_mul(b, pranges->range);
ff_big_add(b, r - pranges->offset);
return i;
}
static void pop_greys(BigInt *b, char *bitmap, int w, int h)
{
if (w > 3) {
w /= 2;
h /= 2;
pop_greys(b, bitmap, w, h);
pop_greys(b, bitmap + w, w, h);
pop_greys(b, bitmap + XFACE_WIDTH * h, w, h);
pop_greys(b, bitmap + XFACE_WIDTH * h + w, w, h);
} else {
w = pop_integer(b, ff_xface_probranges_2x2);
if (w & 1) bitmap[0] = 1;
if (w & 2) bitmap[1] = 1;
if (w & 4) bitmap[XFACE_WIDTH] = 1;
if (w & 8) bitmap[XFACE_WIDTH + 1] = 1;
}
}
static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
{
switch (pop_integer(b, &ff_xface_probranges_per_level[level][0])) {
case XFACE_COLOR_WHITE:
return;
case XFACE_COLOR_BLACK:
pop_greys(b, bitmap, w, h);
return;
default:
w /= 2;
h /= 2;
level++;
decode_block(b, bitmap, w, h, level);
decode_block(b, bitmap + w, w, h, level);
decode_block(b, bitmap + h * XFACE_WIDTH, w, h, level);
decode_block(b, bitmap + w + h * XFACE_WIDTH, w, h, level);
return;
}
}
typedef struct XFaceContext {
AVFrame frame;
uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
} XFaceContext;
static av_cold int xface_decode_init(AVCodecContext *avctx)
{
XFaceContext *xface = avctx->priv_data;
avcodec_get_frame_defaults(&xface->frame);
if (avctx->width || avctx->height) {
if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
av_log(avctx, AV_LOG_ERROR,
"Size value %dx%d not supported, only accepts a size of %dx%d\n",
avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
return AVERROR(EINVAL);
}
}
avctx->width = XFACE_WIDTH;
avctx->height = XFACE_HEIGHT;
avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
return 0;
}
static av_cold int xface_decode_close(AVCodecContext *avctx)
{
XFaceContext *xface = avctx->priv_data;
if (xface->frame.data[0])
avctx->release_buffer(avctx, &xface->frame);
return 0;
}
static int xface_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
XFaceContext *xface = avctx->priv_data;
int ret, i, j, k;
uint8_t byte;
BigInt b = {0};
char *buf;
int64_t c;
if (xface->frame.data[0])
avctx->release_buffer(avctx, &xface->frame);
xface->frame.data[0] = NULL;
if ((ret = avctx->get_buffer(avctx, &xface->frame)) < 0)
return ret;
xface->frame.reference = 0;
for (i = 0, k = 0; avpkt->data[i] && i < avpkt->size; i++) {
c = avpkt->data[i];
/* ignore invalid digits */
if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT)
continue;
if (++k > XFACE_MAX_DIGITS) {
av_log(avctx, AV_LOG_WARNING,
"Buffer is longer than expected, truncating at byte %d\n", i);
break;
}
ff_big_mul(&b, XFACE_PRINTS);
ff_big_add(&b, c - XFACE_FIRST_PRINT);
}
/* decode image and put it in bitmap */
memset(xface->bitmap, 0, XFACE_PIXELS);
buf = xface->bitmap;
decode_block(&b, buf, 16, 16, 0);
decode_block(&b, buf + 16, 16, 16, 0);
decode_block(&b, buf + 32, 16, 16, 0);
decode_block(&b, buf + XFACE_WIDTH * 16, 16, 16, 0);
decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0);
decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0);
decode_block(&b, buf + XFACE_WIDTH * 32 , 16, 16, 0);
decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0);
decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0);
ff_xface_generate_face(xface->bitmap, xface->bitmap);
/* convert image from 1=black 0=white bitmap to MONOWHITE */
buf = xface->frame.data[0];
for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
byte += xface->bitmap[i];
if (k == 7) {
buf[j++] = byte;
byte = k = 0;
} else {
k++;
byte <<= 1;
}
if (j == XFACE_WIDTH/8) {
j = 0;
buf += xface->frame.linesize[0];
}
}
*data_size = sizeof(AVFrame);
*(AVFrame*)data = xface->frame;
return avpkt->size;
}
AVCodec ff_xface_decoder = {
.name = "xface",
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_XFACE,
.priv_data_size = sizeof(XFaceContext),
.init = xface_decode_init,
.close = xface_decode_close,
.decode = xface_decode_frame,
.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
.long_name = NULL_IF_CONFIG_SMALL("X-face image"),
};
/*
* Copyright (c) 1990 James Ashton - Sydney University
* Copyright (c) 2012 Stefano Sabatini
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* X-Face encoder, based on libcompface, by James Ashton.
*/
#include "xface.h"
#include "avcodec.h"
#include "internal.h"
typedef struct XFaceContext {
AVClass *class;
uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
int max_line_len; ///< max line length for compressed data
int set_header; ///< set X-Face header in the output
} XFaceContext;
static int all_same(char *bitmap, int w, int h)
{
char val, *row;
int x;
val = *bitmap;
while (h--) {
row = bitmap;
x = w;
while (x--)
if (*(row++) != val)
return 0;
bitmap += XFACE_WIDTH;
}
return 1;
}
static int all_black(char *bitmap, int w, int h)
{
if (w > 3) {
w /= 2;
h /= 2;
return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) &&
all_black(bitmap + XFACE_WIDTH * h, w, h) &&
all_black(bitmap + XFACE_WIDTH * h + w, w, h));
} else {
/* at least one pixel in the 2x2 grid is non-zero */
return *bitmap || *(bitmap + 1) ||
*(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1);
}
}
static int all_white(char *bitmap, int w, int h)
{
return *bitmap == 0 && all_same(bitmap, w, h);
}
typedef struct {
const ProbRange *prob_ranges[XFACE_PIXELS*2];
int prob_ranges_idx;
} ProbRangesQueue;
static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
{
if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
return -1;
pq->prob_ranges[pq->prob_ranges_idx++] = p;
return 0;
}
static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
{
if (w > 3) {
w /= 2;
h /= 2;
push_greys(pq, bitmap, w, h);
push_greys(pq, bitmap + w, w, h);
push_greys(pq, bitmap + XFACE_WIDTH * h, w, h);
push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h);
} else {
const ProbRange *p = ff_xface_probranges_2x2 +
*bitmap +
2 * *(bitmap + 1) +
4 * *(bitmap + XFACE_WIDTH) +
8 * *(bitmap + XFACE_WIDTH + 1);
pq_push(pq, p);
}
}
static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
{
if (all_white(bitmap, w, h)) {
pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_WHITE]);
} else if (all_black(bitmap, w, h)) {
pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_BLACK]);
push_greys(pq, bitmap, w, h);
} else {
pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_GREY]);
w /= 2;
h /= 2;
level++;
encode_block(bitmap, w, h, level, pq);
encode_block(bitmap + w, w, h, level, pq);
encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq);
encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq);
}
}
static av_cold int xface_encode_init(AVCodecContext *avctx)
{
avctx->coded_frame = avcodec_alloc_frame();
if (!avctx->coded_frame)
return AVERROR(ENOMEM);
avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
return 0;
}
static void push_integer(BigInt *b, const ProbRange *prange)
{
uint8_t r;
ff_big_div(b, prange->range, &r);
ff_big_mul(b, 0);
ff_big_add(b, r + prange->offset);
}
static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *frame, int *got_packet)
{
XFaceContext *xface = avctx->priv_data;
ProbRangesQueue pq = {{ 0 }, 0};
uint8_t bitmap_copy[XFACE_PIXELS];
BigInt b = {0};
int i, j, k, ret = 0;
const uint8_t *buf;
uint8_t *p;
char intbuf[XFACE_MAX_DIGITS];
if (avctx->width || avctx->height) {
if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
av_log(avctx, AV_LOG_ERROR,
"Size value %dx%d not supported, only accepts a size of %dx%d\n",
avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
return AVERROR(EINVAL);
}
}
avctx->width = XFACE_WIDTH;
avctx->height = XFACE_HEIGHT;
/* convert image from MONOWHITE to 1=black 0=white bitmap */
buf = frame->data[0];
for (i = 0, j = 0; i < XFACE_PIXELS; ) {
for (k = 0; k < 8; k++)
xface->bitmap[i++] = (buf[j]>>(7-k))&1;
if (++j == XFACE_WIDTH/8) {
buf += frame->linesize[0];
j = 0;
}
}
/* create a copy of bitmap */
memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
ff_xface_generate_face(xface->bitmap, bitmap_copy);
encode_block(xface->bitmap, 16, 16, 0, &pq);
encode_block(xface->bitmap + 16, 16, 16, 0, &pq);
encode_block(xface->bitmap + 32, 16, 16, 0, &pq);
encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq);
encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq);
encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq);
encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq);
encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq);
encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq);
while (pq.prob_ranges_idx > 0)
push_integer(&b, pq.prob_ranges[--pq.prob_ranges_idx]);
/* write the inverted big integer in b to intbuf */
i = 0;
while (b.nb_words) {
uint8_t r;
ff_big_div(&b, XFACE_PRINTS, &r);
intbuf[i++] = r + XFACE_FIRST_PRINT;
}
if ((ret = ff_alloc_packet2(avctx, pkt, i+2)) < 0)
return ret;
/* revert the number, and close the buffer */
p = pkt->data;
while (--i >= 0)
*(p++) = intbuf[i];
*(p++) = '\n';
*(p++) = 0;
pkt->flags |= AV_PKT_FLAG_KEY;
*got_packet = 1;
return 0;
}
static av_cold int xface_encode_close(AVCodecContext *avctx)
{
av_freep(&avctx->coded_frame);
return 0;
}
AVCodec ff_xface_encoder = {
.name = "xface",
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_XFACE,
.priv_data_size = sizeof(XFaceContext),
.init = xface_encode_init,
.close = xface_encode_close,
.encode2 = xface_encode_frame,
.pix_fmts = (const enum PixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
.long_name = NULL_IF_CONFIG_SMALL("X-face image"),
};
......@@ -74,6 +74,7 @@ static const IdStrMap img_tags[] = {
{ AV_CODEC_ID_PICTOR , "pic"},
{ AV_CODEC_ID_V210X , "yuv10"},
{ AV_CODEC_ID_XBM , "xbm"},
{ AV_CODEC_ID_XFACE , "xface"},
{ AV_CODEC_ID_XWD , "xwd"},
{ AV_CODEC_ID_NONE , NULL}
};
......
......@@ -152,7 +152,7 @@ AVOutputFormat ff_image2_muxer = {
.long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
.extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
"ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
"sunras,xbm",
"sunras,xbm,xface",
.priv_data_size = sizeof(VideoMuxData),
.video_codec = AV_CODEC_ID_MJPEG,
.write_header = write_header,
......
......@@ -31,7 +31,7 @@
#define LIBAVFORMAT_VERSION_MAJOR 54
#define LIBAVFORMAT_VERSION_MINOR 32
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_MICRO 101
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
LIBAVFORMAT_VERSION_MINOR, \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment