40 #define BITSTREAM_READER_LE
49 #define VP8X_FLAG_ANIMATION 0x02
50 #define VP8X_FLAG_XMP_METADATA 0x04
51 #define VP8X_FLAG_EXIF_METADATA 0x08
52 #define VP8X_FLAG_ALPHA 0x10
53 #define VP8X_FLAG_ICC 0x20
55 #define MAX_PALETTE_SIZE 256
56 #define MAX_CACHE_BITS 11
57 #define NUM_CODE_LENGTH_CODES 19
58 #define HUFFMAN_CODES_PER_META_CODE 5
59 #define NUM_LITERAL_CODES 256
60 #define NUM_LENGTH_CODES 24
61 #define NUM_DISTANCE_CODES 40
62 #define NUM_SHORT_DISTANCES 120
63 #define MAX_HUFFMAN_CODE_LENGTH 15
72 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
76 { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
77 { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
78 { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
79 { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
80 { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
81 { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
82 { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
83 { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
84 { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
85 { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
86 { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
87 { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
88 { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
89 { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
90 { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
205 #define GET_PIXEL(frame, x, y) \
206 ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
208 #define GET_PIXEL_COMP(frame, x, y, c) \
209 (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
225 memset(img, 0,
sizeof(*img));
245 code = table[
index][0];
256 code = table[
index][0];
280 int len = 0, sym, code = 0, ret;
281 int max_code_length = 0;
285 for (sym = 0; sym < alphabet_size; sym++) {
286 if (code_lengths[sym] > 0) {
300 for (sym = 0; sym < alphabet_size; sym++)
301 max_code_length =
FFMAX(max_code_length, code_lengths[sym]);
306 codes =
av_malloc(alphabet_size *
sizeof(*codes));
312 for (len = 1; len <= max_code_length; len++) {
313 for (sym = 0; sym < alphabet_size; sym++) {
314 if (code_lengths[sym] != len)
327 code_lengths,
sizeof(*code_lengths),
sizeof(*code_lengths),
328 codes,
sizeof(*codes),
sizeof(*codes), 0);
357 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
358 int *code_lengths =
NULL;
360 int i, symbol, max_symbol, prev_code_len, ret;
366 for (i = 0; i < num_codes; i++)
383 if (max_symbol > alphabet_size) {
385 max_symbol, alphabet_size);
390 max_symbol = alphabet_size;
395 while (symbol < alphabet_size) {
403 code_lengths[symbol++] = code_len;
405 prev_code_len = code_len;
407 int repeat = 0, length = 0;
414 length = prev_code_len;
427 if (symbol + repeat > alphabet_size) {
429 "invalid symbol %d + repeat %d > alphabet size %d\n",
430 symbol, repeat, alphabet_size);
435 code_lengths[symbol++] = length;
450 #define PARSE_BLOCK_SIZE(w, h) do { \
451 block_bits = get_bits(&s->gb, 3) + 2; \
452 blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
453 blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
459 int ret, block_bits,
width, blocks_w, blocks_h, x, y, max;
490 int block_bits, blocks_w, blocks_h, ret;
506 int block_bits, blocks_w, blocks_h, ret;
523 int width_bits, index_size, ret, x;
530 else if (index_size <= 4)
532 else if (index_size <= 16)
549 for (x = 4; x < img->
frame->
width * 4; x++, ct++)
581 int i, j, ret, x, y,
width;
583 img = &s->
image[role];
654 while (y < img->frame->height) {
675 int prefix_code, length,
distance, ref_x, ref_y;
679 if (prefix_code < 4) {
680 length = prefix_code + 1;
683 int offset = 2 + (prefix_code & 1) << extra_bits;
684 length = offset +
get_bits(&s->
gb, extra_bits) + 1;
687 if (prefix_code < 4) {
688 distance = prefix_code + 1;
691 int offset = 2 + (prefix_code & 1) << extra_bits;
692 distance = offset +
get_bits(&s->
gb, extra_bits) + 1;
699 distance =
FFMAX(1, xi + yi * width);
712 while (distance >= width) {
720 ref_x =
FFMAX(0, ref_x);
721 ref_y =
FFMAX(0, ref_y);
726 for (i = 0; i < length; i++) {
739 if (ref_x == width) {
757 "color cache index out-of-bounds\n");
811 p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
812 p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
813 p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
814 p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
821 p[0] = p_l[0] + p_tl[0] >> 1;
822 p[1] = p_l[1] + p_tl[1] >> 1;
823 p[2] = p_l[2] + p_tl[2] >> 1;
824 p[3] = p_l[3] + p_tl[3] >> 1;
831 p[0] = p_l[0] + p_t[0] >> 1;
832 p[1] = p_l[1] + p_t[1] >> 1;
833 p[2] = p_l[2] + p_t[2] >> 1;
834 p[3] = p_l[3] + p_t[3] >> 1;
841 p[0] = p_tl[0] + p_t[0] >> 1;
842 p[1] = p_tl[1] + p_t[1] >> 1;
843 p[2] = p_tl[2] + p_t[2] >> 1;
844 p[3] = p_tl[3] + p_t[3] >> 1;
851 p[0] = p_t[0] + p_tr[0] >> 1;
852 p[1] = p_t[1] + p_tr[1] >> 1;
853 p[2] = p_t[2] + p_tr[2] >> 1;
854 p[3] = p_t[3] + p_tr[3] >> 1;
861 p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
862 p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
863 p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
864 p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
871 int diff = (
FFABS(p_l[0] - p_tl[0]) -
FFABS(p_t[0] - p_tl[0])) +
872 (
FFABS(p_l[1] - p_tl[1]) -
FFABS(p_t[1] - p_tl[1])) +
873 (
FFABS(p_l[2] - p_tl[2]) -
FFABS(p_t[2] - p_tl[2])) +
874 (
FFABS(p_l[3] - p_tl[3]) -
FFABS(p_t[3] - p_tl[3]));
885 p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
886 p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
887 p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
888 p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
894 return av_clip_uint8(d + (d - c) / 2);
920 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
927 if (x == frame->
width - 1)
932 inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
962 "invalid predictor mode: %d\n", m);
1043 p[2] =
get_bits(&gb_g, pixel_bits);
1071 int *got_frame,
uint8_t *data_start,
1072 unsigned int data_size,
int is_alpha_chunk)
1077 if (!is_alpha_chunk) {
1086 if (!is_alpha_chunk) {
1128 switch (transform) {
1140 goto free_and_return;
1149 goto free_and_return;
1168 goto free_and_return;
1191 dec = frame->
data[3] + 1;
1192 for (x = 1; x < frame->
width; x++, dec++)
1196 dec = frame->
data[3] + ls;
1197 for (y = 1; y < frame->
height; y++, dec += ls)
1198 *dec += *(dec - ls);
1203 for (y = 1; y < frame->
height; y++) {
1204 dec = frame->
data[3] + y * ls + 1;
1205 for (x = 1; x < frame->
width; x++, dec++)
1210 for (y = 1; y < frame->
height; y++) {
1211 dec = frame->
data[3] + y * ls + 1;
1212 for (x = 1; x < frame->
width; x++, dec++)
1213 *dec += *(dec - ls);
1217 for (y = 1; y < frame->
height; y++) {
1218 dec = frame->
data[3] + y * ls + 1;
1219 for (x = 1; x < frame->
width; x++, dec++)
1220 dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1228 unsigned int data_size)
1237 for (y = 0; y < s->
height; y++)
1242 int alpha_got_frame = 0;
1249 data_start, data_size, 1);
1254 if (!alpha_got_frame) {
1260 for (y = 0; y < s->
height; y++) {
1263 for (x = 0; x < s->
width; x++) {
1280 int *got_frame,
uint8_t *data_start,
1281 unsigned int data_size)
1295 if (data_size > INT_MAX) {
1301 pkt.
data = data_start;
1302 pkt.
size = data_size;
1321 uint32_t chunk_type, chunk_size;
1334 if (bytestream2_get_le32(&gb) !=
MKTAG(
'R',
'I',
'F',
'F')) {
1339 chunk_size = bytestream2_get_le32(&gb);
1343 if (bytestream2_get_le32(&gb) !=
MKTAG(
'W',
'E',
'B',
'P')) {
1349 char chunk_str[5] = { 0 };
1351 chunk_type = bytestream2_get_le32(&gb);
1352 chunk_size = bytestream2_get_le32(&gb);
1353 if (chunk_size == UINT32_MAX)
1355 chunk_size += chunk_size & 1;
1360 switch (chunk_type) {
1361 case MKTAG(
'V',
'P',
'8',
' '):
1371 case MKTAG(
'V',
'P',
'8',
'L'):
1381 case MKTAG(
'V',
'P',
'8',
'X'):
1382 vp8x_flags = bytestream2_get_byte(&gb);
1384 s->
width = bytestream2_get_le24(&gb) + 1;
1385 s->
height = bytestream2_get_le24(&gb) + 1;
1390 case MKTAG(
'A',
'L',
'P',
'H'): {
1391 int alpha_header, filter_m, compression;
1395 "ALPHA chunk present, but alpha bit not set in the "
1398 if (chunk_size == 0) {
1402 alpha_header = bytestream2_get_byte(&gb);
1407 filter_m = (alpha_header >> 2) & 0x03;
1408 compression = alpha_header & 0x03;
1412 "skipping unsupported ALPHA chunk\n");
1421 case MKTAG(
'I',
'C',
'C',
'P'):
1422 case MKTAG(
'A',
'N',
'I',
'M'):
1423 case MKTAG(
'A',
'N',
'M',
'F'):
1424 case MKTAG(
'E',
'X',
'I',
'F'):
1425 case MKTAG(
'X',
'M',
'P',
' '):
1426 AV_WL32(chunk_str, chunk_type);
1432 AV_WL32(chunk_str, chunk_type);
static int read_huffman_code_normal(WebPContext *s, HuffReader *hc, int alphabet_size)
HuffReader * huffman_groups
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
static int parse_transform_color_indexing(WebPContext *s)
static HuffReader * get_huffman_group(WebPContext *s, ImageContext *img, int x, int y)
static const uint8_t code_length_code_order[NUM_CODE_LENGTH_CODES]
static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static int8_t ff_u8_to_s8(uint8_t a)
static const inv_predict_func inverse_predict[14]
static int apply_color_indexing_transform(WebPContext *s)
static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c)
enum TransformType transforms[4]
uint16_t simple_symbols[2]
static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p, uint8_t *data_start, unsigned int data_size)
#define NUM_LITERAL_CODES
static av_always_inline void color_cache_put(ImageContext *img, uint32_t c)
enum AlphaFilter alpha_filter
static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define NUM_CODE_LENGTH_CODES
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Multithreading support functions.
int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define NUM_SHORT_DISTANCES
bitstream reader API header.
#define AV_LOG_VERBOSE
Detailed information.
static int decode_entropy_image(WebPContext *s)
static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static int apply_color_transform(WebPContext *s)
#define UPDATE_CACHE(name, gb)
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, uint8_t *data_start, unsigned int data_size)
#define CLOSE_READER(name, gb)
static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, uint8_t *data_start, unsigned int data_size, int is_alpha_chunk)
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
enum AlphaCompression alpha_compression
static void image_ctx_free(ImageContext *img)
#define SKIP_BITS(name, gb, num)
static float distance(float x, float y, int band)
static av_always_inline int webp_get_vlc(GetBitContext *gb, VLC_TYPE(*table)[2])
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
#define PARSE_BLOCK_SIZE(w, h)
enum AVPictureType pict_type
Picture type of the frame.
static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define LAST_SKIP_BITS(name, gb, num)
#define SHOW_UBITS(name, gb, num)
void(* inv_predict_func)(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
static av_always_inline int bytestream2_tell(GetByteContext *g)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
static const int8_t transform[32][32]
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static int huff_reader_build_canonical(HuffReader *r, int *code_lengths, int alphabet_size)
main external API structure.
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
static void close(AVCodecParserContext *s)
#define OPEN_READER(name, gb)
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
#define AVERROR_BUG
Bug detected, please report the issue.
static av_always_inline uint8_t color_transform_delta(uint8_t color_pred, uint8_t color)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define MAX_HUFFMAN_CODE_LENGTH
static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE]
#define HUFFMAN_CODES_PER_META_CODE
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
static void read_huffman_code_simple(WebPContext *s, HuffReader *hc)
av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
static int parse_transform_predictor(WebPContext *s)
#define GET_PIXEL(frame, x, y)
static av_cold int webp_decode_close(AVCodecContext *avctx)
common internal api header.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
static int apply_subtract_green_transform(WebPContext *s)
#define GET_PIXEL_COMP(frame, x, y, c)
static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static const uint8_t color[]
static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2]
ImageContext image[IMAGE_ROLE_NB]
static int apply_predictor_transform(WebPContext *s)
VLC_TYPE(* table)[2]
code, bits
static int parse_transform_color(WebPContext *s)
int key_frame
1 -> keyframe, 0-> not
static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, int w, int h)
static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static void * av_mallocz_array(size_t nmemb, size_t size)
static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m)
const uint8_t ff_reverse[256]
static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
#define MKTAG(a, b, c, d)
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define NUM_DISTANCE_CODES