| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Intel Indeo 2 codec | ||
| 3 | * Copyright (c) 2005 Konstantin Shishkov | ||
| 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 | * Intel Indeo 2 decoder. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/attributes.h" | ||
| 28 | #include "libavutil/thread.h" | ||
| 29 | |||
| 30 | #define BITSTREAM_READER_LE | ||
| 31 | #include "avcodec.h" | ||
| 32 | #include "codec_internal.h" | ||
| 33 | #include "decode.h" | ||
| 34 | #include "get_bits.h" | ||
| 35 | #include "indeo2data.h" | ||
| 36 | |||
| 37 | typedef struct Ir2Context{ | ||
| 38 | AVCodecContext *avctx; | ||
| 39 | AVFrame *picture; | ||
| 40 | GetBitContext gb; | ||
| 41 | int decode_delta; | ||
| 42 | } Ir2Context; | ||
| 43 | |||
| 44 | #define CODE_VLC_BITS 14 | ||
| 45 | static VLCElem ir2_vlc[1 << CODE_VLC_BITS]; | ||
| 46 | |||
| 47 | /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */ | ||
| 48 | 2449607 | static inline int ir2_get_code(GetBitContext *gb) | |
| 49 | { | ||
| 50 | 2449607 | return get_vlc2(gb, ir2_vlc, CODE_VLC_BITS, 1); | |
| 51 | } | ||
| 52 | |||
| 53 | 633 | static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst, | |
| 54 | int pitch, const uint8_t *table) | ||
| 55 | { | ||
| 56 | int i; | ||
| 57 | int j; | ||
| 58 | 633 | int out = 0; | |
| 59 | |||
| 60 |
2/4✓ Branch 0 taken 633 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 633 times.
|
633 | if ((width & 1) || width * height / (2*(IR2_CODES - 0x7F)) > get_bits_left(&ctx->gb)) |
| 61 | ✗ | return AVERROR_INVALIDDATA; | |
| 62 | |||
| 63 | /* first line contain absolute values, other lines contain deltas */ | ||
| 64 |
2/2✓ Branch 0 taken 25231 times.
✓ Branch 1 taken 633 times.
|
25864 | while (out < width) { |
| 65 | 25231 | int c = ir2_get_code(&ctx->gb); | |
| 66 |
2/2✓ Branch 0 taken 338 times.
✓ Branch 1 taken 24893 times.
|
25231 | if (c >= 0x80) { /* we have a run */ |
| 67 | 338 | c -= 0x7F; | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
|
338 | if (out + c*2 > width) |
| 69 | ✗ | return AVERROR_INVALIDDATA; | |
| 70 |
2/2✓ Branch 0 taken 854 times.
✓ Branch 1 taken 338 times.
|
1192 | for (i = 0; i < c * 2; i++) |
| 71 | 854 | dst[out++] = 0x80; | |
| 72 | } else { /* copy two values from table */ | ||
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24893 times.
|
24893 | if (c <= 0) |
| 74 | ✗ | return AVERROR_INVALIDDATA; | |
| 75 | 24893 | dst[out++] = table[c * 2]; | |
| 76 | 24893 | dst[out++] = table[(c * 2) + 1]; | |
| 77 | } | ||
| 78 | } | ||
| 79 | 633 | dst += pitch; | |
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 37347 times.
✓ Branch 1 taken 633 times.
|
37980 | for (j = 1; j < height; j++) { |
| 82 | 37347 | out = 0; | |
| 83 |
2/2✓ Branch 0 taken 2115081 times.
✓ Branch 1 taken 37347 times.
|
2152428 | while (out < width) { |
| 84 | int c; | ||
| 85 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2115081 times.
|
2115081 | if (get_bits_left(&ctx->gb) <= 0) |
| 86 | ✗ | return AVERROR_INVALIDDATA; | |
| 87 | 2115081 | c = ir2_get_code(&ctx->gb); | |
| 88 |
2/2✓ Branch 0 taken 228280 times.
✓ Branch 1 taken 1886801 times.
|
2115081 | if (c >= 0x80) { /* we have a skip */ |
| 89 | 228280 | c -= 0x7F; | |
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 228280 times.
|
228280 | if (out + c*2 > width) |
| 91 | ✗ | return AVERROR_INVALIDDATA; | |
| 92 |
2/2✓ Branch 0 taken 733358 times.
✓ Branch 1 taken 228280 times.
|
961638 | for (i = 0; i < c * 2; i++) { |
| 93 | 733358 | dst[out] = dst[out - pitch]; | |
| 94 | 733358 | out++; | |
| 95 | } | ||
| 96 | } else { /* add two deltas from table */ | ||
| 97 | int t; | ||
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1886801 times.
|
1886801 | if (c <= 0) |
| 99 | ✗ | return AVERROR_INVALIDDATA; | |
| 100 | 1886801 | t = dst[out - pitch] + (table[c * 2] - 128); | |
| 101 | 1886801 | t = av_clip_uint8(t); | |
| 102 | 1886801 | dst[out] = t; | |
| 103 | 1886801 | out++; | |
| 104 | 1886801 | t = dst[out - pitch] + (table[(c * 2) + 1] - 128); | |
| 105 | 1886801 | t = av_clip_uint8(t); | |
| 106 | 1886801 | dst[out] = t; | |
| 107 | 1886801 | out++; | |
| 108 | } | ||
| 109 | } | ||
| 110 | 37347 | dst += pitch; | |
| 111 | } | ||
| 112 | 633 | return 0; | |
| 113 | } | ||
| 114 | |||
| 115 | 99 | static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst, | |
| 116 | int pitch, const uint8_t *table) | ||
| 117 | { | ||
| 118 | int j; | ||
| 119 | 99 | int out = 0; | |
| 120 | int c; | ||
| 121 | int t; | ||
| 122 | |||
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
|
99 | if (width & 1) |
| 124 | ✗ | return AVERROR_INVALIDDATA; | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 99 times.
|
6039 | for (j = 0; j < height; j++) { |
| 127 | 5940 | out = 0; | |
| 128 |
2/2✓ Branch 0 taken 309295 times.
✓ Branch 1 taken 5940 times.
|
315235 | while (out < width) { |
| 129 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 309295 times.
|
309295 | if (get_bits_left(&ctx->gb) <= 0) |
| 130 | ✗ | return AVERROR_INVALIDDATA; | |
| 131 | 309295 | c = ir2_get_code(&ctx->gb); | |
| 132 |
2/2✓ Branch 0 taken 56657 times.
✓ Branch 1 taken 252638 times.
|
309295 | if (c >= 0x80) { /* we have a skip */ |
| 133 | 56657 | c -= 0x7F; | |
| 134 | 56657 | out += c * 2; | |
| 135 | } else { /* add two deltas from table */ | ||
| 136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 252638 times.
|
252638 | if (c <= 0) |
| 137 | ✗ | return AVERROR_INVALIDDATA; | |
| 138 | 252638 | t = dst[out] + (((table[c * 2] - 128)*3) >> 2); | |
| 139 | 252638 | t = av_clip_uint8(t); | |
| 140 | 252638 | dst[out] = t; | |
| 141 | 252638 | out++; | |
| 142 | 252638 | t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2); | |
| 143 | 252638 | t = av_clip_uint8(t); | |
| 144 | 252638 | dst[out] = t; | |
| 145 | 252638 | out++; | |
| 146 | } | ||
| 147 | } | ||
| 148 | 5940 | dst += pitch; | |
| 149 | } | ||
| 150 | 99 | return 0; | |
| 151 | } | ||
| 152 | |||
| 153 | 244 | static int ir2_decode_frame(AVCodecContext *avctx, AVFrame *picture, | |
| 154 | int *got_frame, AVPacket *avpkt) | ||
| 155 | { | ||
| 156 | 244 | Ir2Context * const s = avctx->priv_data; | |
| 157 | 244 | const uint8_t *buf = avpkt->data; | |
| 158 | 244 | int buf_size = avpkt->size; | |
| 159 | 244 | AVFrame * const p = s->picture; | |
| 160 | int start, ret; | ||
| 161 | int ltab, ctab; | ||
| 162 | |||
| 163 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
|
244 | if ((ret = ff_reget_buffer(avctx, p, 0)) < 0) |
| 164 | ✗ | return ret; | |
| 165 | |||
| 166 | 244 | start = 48; /* hardcoded for now */ | |
| 167 | |||
| 168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 244 times.
|
244 | if (start >= buf_size) { |
| 169 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size); | |
| 170 | ✗ | return AVERROR_INVALIDDATA; | |
| 171 | } | ||
| 172 | |||
| 173 | 244 | s->decode_delta = buf[18]; | |
| 174 | |||
| 175 | /* decide whether frame uses deltas or not */ | ||
| 176 | |||
| 177 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
|
244 | if ((ret = init_get_bits8(&s->gb, buf + start, buf_size - start)) < 0) |
| 178 | ✗ | return ret; | |
| 179 | |||
| 180 | 244 | ltab = buf[0x22] & 3; | |
| 181 | 244 | ctab = buf[0x22] >> 2; | |
| 182 | |||
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 244 times.
|
244 | if (ctab > 3) { |
| 184 | ✗ | av_log(avctx, AV_LOG_ERROR, "ctab %d is invalid\n", ctab); | |
| 185 | ✗ | return AVERROR_INVALIDDATA; | |
| 186 | } | ||
| 187 | |||
| 188 |
2/2✓ Branch 0 taken 211 times.
✓ Branch 1 taken 33 times.
|
244 | if (s->decode_delta) { /* intraframe */ |
| 189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
|
211 | if ((ret = ir2_decode_plane(s, avctx->width, avctx->height, |
| 190 | p->data[0], p->linesize[0], | ||
| 191 | 211 | ir2_delta_table[ltab])) < 0) | |
| 192 | ✗ | return ret; | |
| 193 | |||
| 194 | /* swapped U and V */ | ||
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
|
211 | if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2, |
| 196 | p->data[2], p->linesize[2], | ||
| 197 | 211 | ir2_delta_table[ctab])) < 0) | |
| 198 | ✗ | return ret; | |
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
|
211 | if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2, |
| 200 | p->data[1], p->linesize[1], | ||
| 201 | 211 | ir2_delta_table[ctab])) < 0) | |
| 202 | ✗ | return ret; | |
| 203 | } else { /* interframe */ | ||
| 204 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height, |
| 205 | p->data[0], p->linesize[0], | ||
| 206 | 33 | ir2_delta_table[ltab])) < 0) | |
| 207 | ✗ | return ret; | |
| 208 | /* swapped U and V */ | ||
| 209 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2, |
| 210 | p->data[2], p->linesize[2], | ||
| 211 | 33 | ir2_delta_table[ctab])) < 0) | |
| 212 | ✗ | return ret; | |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2, |
| 214 | p->data[1], p->linesize[1], | ||
| 215 | 33 | ir2_delta_table[ctab])) < 0) | |
| 216 | ✗ | return ret; | |
| 217 | } | ||
| 218 | |||
| 219 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
|
244 | if ((ret = av_frame_ref(picture, p)) < 0) |
| 220 | ✗ | return ret; | |
| 221 | |||
| 222 | 244 | *got_frame = 1; | |
| 223 | |||
| 224 | 244 | return buf_size; | |
| 225 | } | ||
| 226 | |||
| 227 | 2 | static av_cold void ir2_init_static(void) | |
| 228 | { | ||
| 229 | 2 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(ir2_vlc, CODE_VLC_BITS, IR2_CODES, | |
| 230 | &ir2_tab[0][1], 2, &ir2_tab[0][0], 2, 1, | ||
| 231 | 0, VLC_INIT_OUTPUT_LE); | ||
| 232 | 2 | } | |
| 233 | |||
| 234 | 4 | static av_cold int ir2_decode_init(AVCodecContext *avctx) | |
| 235 | { | ||
| 236 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
| 237 | 4 | Ir2Context * const ic = avctx->priv_data; | |
| 238 | |||
| 239 | 4 | ic->avctx = avctx; | |
| 240 | |||
| 241 | 4 | avctx->pix_fmt= AV_PIX_FMT_YUV410P; | |
| 242 | |||
| 243 | 4 | ic->picture = av_frame_alloc(); | |
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!ic->picture) |
| 245 | ✗ | return AVERROR(ENOMEM); | |
| 246 | |||
| 247 | 4 | ff_thread_once(&init_static_once, ir2_init_static); | |
| 248 | |||
| 249 | 4 | return 0; | |
| 250 | } | ||
| 251 | |||
| 252 | 4 | static av_cold int ir2_decode_end(AVCodecContext *avctx) | |
| 253 | { | ||
| 254 | 4 | Ir2Context * const ic = avctx->priv_data; | |
| 255 | |||
| 256 | 4 | av_frame_free(&ic->picture); | |
| 257 | |||
| 258 | 4 | return 0; | |
| 259 | } | ||
| 260 | |||
| 261 | const FFCodec ff_indeo2_decoder = { | ||
| 262 | .p.name = "indeo2", | ||
| 263 | CODEC_LONG_NAME("Intel Indeo 2"), | ||
| 264 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 265 | .p.id = AV_CODEC_ID_INDEO2, | ||
| 266 | .priv_data_size = sizeof(Ir2Context), | ||
| 267 | .init = ir2_decode_init, | ||
| 268 | .close = ir2_decode_end, | ||
| 269 | FF_CODEC_DECODE_CB(ir2_decode_frame), | ||
| 270 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 271 | }; | ||
| 272 |