| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 32 | 194021 | 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 | 194021 | ff_big_div(b, 0, &r); | |
| 39 | |||
| 40 | 194021 | i = 0; | |
| 41 |
4/4✓ Branch 0 taken 82000 times.
✓ Branch 1 taken 694193 times.
✓ Branch 2 taken 500172 times.
✓ Branch 3 taken 194021 times.
|
776193 | while (r < pranges->offset || r >= pranges->range + pranges->offset) { |
| 42 | 582172 | pranges++; | |
| 43 | 582172 | i++; | |
| 44 | } | ||
| 45 | 194021 | ff_big_mul(b, pranges->range); | |
| 46 | 194021 | ff_big_add(b, r - pranges->offset); | |
| 47 | 194021 | return i; | |
| 48 | } | ||
| 49 | |||
| 50 | 97901 | static void pop_greys(BigInt *b, char *bitmap, int w, int h) | |
| 51 | { | ||
| 52 |
2/2✓ Branch 0 taken 11033 times.
✓ Branch 1 taken 86868 times.
|
97901 | if (w > 3) { |
| 53 | 11033 | w /= 2; | |
| 54 | 11033 | h /= 2; | |
| 55 | 11033 | pop_greys(b, bitmap, w, h); | |
| 56 | 11033 | pop_greys(b, bitmap + w, w, h); | |
| 57 | 11033 | pop_greys(b, bitmap + XFACE_WIDTH * h, w, h); | |
| 58 | 11033 | pop_greys(b, bitmap + XFACE_WIDTH * h + w, w, h); | |
| 59 | } else { | ||
| 60 | 86868 | w = pop_integer(b, ff_xface_probranges_2x2); | |
| 61 |
2/2✓ Branch 0 taken 38854 times.
✓ Branch 1 taken 48014 times.
|
86868 | if (w & 1) bitmap[0] = 1; |
| 62 |
2/2✓ Branch 0 taken 43663 times.
✓ Branch 1 taken 43205 times.
|
86868 | if (w & 2) bitmap[1] = 1; |
| 63 |
2/2✓ Branch 0 taken 36042 times.
✓ Branch 1 taken 50826 times.
|
86868 | if (w & 4) bitmap[XFACE_WIDTH] = 1; |
| 64 |
2/2✓ Branch 0 taken 28924 times.
✓ Branch 1 taken 57944 times.
|
86868 | if (w & 8) bitmap[XFACE_WIDTH + 1] = 1; |
| 65 | } | ||
| 66 | 97901 | } | |
| 67 | |||
| 68 | 107153 | static void decode_block(BigInt *b, char *bitmap, int w, int h, int level) | |
| 69 | { | ||
| 70 |
3/3✓ Branch 1 taken 27048 times.
✓ Branch 2 taken 53769 times.
✓ Branch 3 taken 26336 times.
|
107153 | switch (pop_integer(b, &ff_xface_probranges_per_level[level][0])) { |
| 71 | 27048 | case XFACE_COLOR_WHITE: | |
| 72 | 27048 | return; | |
| 73 | 53769 | case XFACE_COLOR_BLACK: | |
| 74 | 53769 | pop_greys(b, bitmap, w, h); | |
| 75 | 53769 | return; | |
| 76 | 26336 | default: | |
| 77 | 26336 | w /= 2; | |
| 78 | 26336 | h /= 2; | |
| 79 | 26336 | level++; | |
| 80 | 26336 | decode_block(b, bitmap, w, h, level); | |
| 81 | 26336 | decode_block(b, bitmap + w, w, h, level); | |
| 82 | 26336 | decode_block(b, bitmap + h * XFACE_WIDTH, w, h, level); | |
| 83 | 26336 | decode_block(b, bitmap + w + h * XFACE_WIDTH, w, h, level); | |
| 84 | 26336 | return; | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | typedef struct XFaceContext { | ||
| 89 | uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding | ||
| 90 | } XFaceContext; | ||
| 91 | |||
| 92 | 10 | static av_cold int xface_decode_init(AVCodecContext *avctx) | |
| 93 | { | ||
| 94 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
10 | if (avctx->width || avctx->height) { |
| 95 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 | 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 | 10 | avctx->width = XFACE_WIDTH; | |
| 104 | 10 | avctx->height = XFACE_HEIGHT; | |
| 105 | 10 | avctx->pix_fmt = AV_PIX_FMT_MONOWHITE; | |
| 106 | |||
| 107 | 10 | return 0; | |
| 108 | } | ||
| 109 | |||
| 110 | 201 | static int xface_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 111 | int *got_frame, AVPacket *avpkt) | ||
| 112 | { | ||
| 113 | 201 | XFaceContext *xface = avctx->priv_data; | |
| 114 | int ret, i, j, k; | ||
| 115 | uint8_t byte; | ||
| 116 | 201 | BigInt b = {0}; | |
| 117 | char *buf; | ||
| 118 | int64_t c; | ||
| 119 | |||
| 120 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 201 times.
|
201 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 121 | ✗ | return ret; | |
| 122 | |||
| 123 |
3/4✓ Branch 0 taken 68153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67952 times.
✓ Branch 3 taken 201 times.
|
68153 | for (i = 0, k = 0; i < avpkt->size && avpkt->data[i]; i++) { |
| 124 | 67952 | c = avpkt->data[i]; | |
| 125 | |||
| 126 | /* ignore invalid digits */ | ||
| 127 |
3/4✓ Branch 0 taken 67751 times.
✓ Branch 1 taken 201 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 67751 times.
|
67952 | if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT) |
| 128 | 201 | continue; | |
| 129 | |||
| 130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 67751 times.
|
67751 | 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 | } | ||
| 135 | 67751 | ff_big_mul(&b, XFACE_PRINTS); | |
| 136 | 67751 | ff_big_add(&b, c - XFACE_FIRST_PRINT); | |
| 137 | } | ||
| 138 | |||
| 139 | /* decode image and put it in bitmap */ | ||
| 140 | 201 | memset(xface->bitmap, 0, XFACE_PIXELS); | |
| 141 | 201 | buf = xface->bitmap; | |
| 142 | 201 | decode_block(&b, buf, 16, 16, 0); | |
| 143 | 201 | decode_block(&b, buf + 16, 16, 16, 0); | |
| 144 | 201 | decode_block(&b, buf + 32, 16, 16, 0); | |
| 145 | 201 | decode_block(&b, buf + XFACE_WIDTH * 16, 16, 16, 0); | |
| 146 | 201 | decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0); | |
| 147 | 201 | decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0); | |
| 148 | 201 | decode_block(&b, buf + XFACE_WIDTH * 32 , 16, 16, 0); | |
| 149 | 201 | decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0); | |
| 150 | 201 | decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0); | |
| 151 | |||
| 152 | 201 | ff_xface_generate_face(xface->bitmap, xface->bitmap); | |
| 153 | |||
| 154 | /* convert image from 1=black 0=white bitmap to MONOWHITE */ | ||
| 155 | 201 | buf = frame->data[0]; | |
| 156 |
2/2✓ Branch 0 taken 463104 times.
✓ Branch 1 taken 201 times.
|
463305 | for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) { |
| 157 | 463104 | byte += xface->bitmap[i]; | |
| 158 |
2/2✓ Branch 0 taken 57888 times.
✓ Branch 1 taken 405216 times.
|
463104 | if (k == 7) { |
| 159 | 57888 | buf[j++] = byte; | |
| 160 | 57888 | byte = k = 0; | |
| 161 | } else { | ||
| 162 | 405216 | k++; | |
| 163 | 405216 | byte <<= 1; | |
| 164 | } | ||
| 165 |
2/2✓ Branch 0 taken 9648 times.
✓ Branch 1 taken 453456 times.
|
463104 | if (j == XFACE_WIDTH/8) { |
| 166 | 9648 | j = 0; | |
| 167 | 9648 | buf += frame->linesize[0]; | |
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | 201 | *got_frame = 1; | |
| 172 | |||
| 173 | 201 | return avpkt->size; | |
| 174 | } | ||
| 175 | |||
| 176 | const FFCodec ff_xface_decoder = { | ||
| 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), | ||
| 183 | .init = xface_decode_init, | ||
| 184 | FF_CODEC_DECODE_CB(xface_decode_frame), | ||
| 185 | }; | ||
| 186 |