FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/anm.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 72 87 82.8%
Branches: 43 58 74.1%

Line Branch Exec Source
1 /*
2 * Deluxe Paint Animation decoder
3 * Copyright (c) 2009 Peter Ross
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 * Deluxe Paint Animation decoder
25 */
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30
31 typedef struct AnmContext {
32 AVFrame *frame;
33 int palette[AVPALETTE_COUNT];
34 } AnmContext;
35
36 2 static av_cold int decode_init(AVCodecContext *avctx)
37 {
38 2 AnmContext *s = avctx->priv_data;
39 GetByteContext gb;
40 int i;
41
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->extradata_size < 16 * 8 + 4 * 256)
43 return AVERROR_INVALIDDATA;
44
45 2 avctx->pix_fmt = AV_PIX_FMT_PAL8;
46
47 2 s->frame = av_frame_alloc();
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->frame)
49 return AVERROR(ENOMEM);
50
51 2 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
52 2 bytestream2_skipu(&gb, 16 * 8);
53
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (i = 0; i < 256; i++)
54 512 s->palette[i] = (0xFFU << 24) | bytestream2_get_le32u(&gb);
55
56 2 return 0;
57 }
58
59 /**
60 * Perform decode operation
61 * @param dst pointer to destination image buffer
62 * @param dst_end pointer to end of destination image buffer
63 * @param gb GetByteContext (optional, see below)
64 * @param pixel Fill color (optional, see below)
65 * @param count Pixel count
66 * @param x Pointer to x-axis counter
67 * @param width Image width
68 * @param linesize Destination image buffer linesize
69 * @return non-zero if destination buffer is exhausted
70 *
71 * a copy operation is achieved when 'gb' is set
72 * a fill operation is achieved when 'gb' is null and pixel is >= 0
73 * a skip operation is achieved when 'gb' is null and pixel is < 0
74 */
75 107093 static inline int op(uint8_t **dst, const uint8_t *dst_end,
76 GetByteContext *gb,
77 int pixel, int count,
78 int *x, int width, int linesize)
79 {
80 107093 int remaining = width - *x;
81
2/2
✓ Branch 0 taken 124635 times.
✓ Branch 1 taken 107086 times.
231721 while(count > 0) {
82 124635 int striplen = FFMIN(count, remaining);
83
2/2
✓ Branch 0 taken 43244 times.
✓ Branch 1 taken 81391 times.
124635 if (gb) {
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43244 times.
43244 if (bytestream2_get_bytes_left(gb) < striplen)
85 goto exhausted;
86 43244 bytestream2_get_bufferu(gb, *dst, striplen);
87
2/2
✓ Branch 0 taken 29042 times.
✓ Branch 1 taken 52349 times.
81391 } else if (pixel >= 0)
88 29042 memset(*dst, pixel, striplen);
89 124635 *dst += striplen;
90 124635 remaining -= striplen;
91 124635 count -= striplen;
92
2/2
✓ Branch 0 taken 18753 times.
✓ Branch 1 taken 105882 times.
124635 if (remaining <= 0) {
93 18753 *dst += linesize - width;
94 18753 remaining = width;
95 }
96
1/2
✓ Branch 0 taken 124635 times.
✗ Branch 1 not taken.
124635 if (linesize > 0) {
97
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 124628 times.
124635 if (*dst >= dst_end) goto exhausted;
98 } else {
99 if (*dst <= dst_end) goto exhausted;
100 }
101 }
102 107086 *x = width - remaining;
103 107086 return 0;
104
105 7 exhausted:
106 7 *x = width - remaining;
107 7 return 1;
108 }
109
110 123 static int decode_frame(AVCodecContext *avctx,
111 void *data, int *got_frame,
112 AVPacket *avpkt)
113 {
114 123 AnmContext *s = avctx->priv_data;
115 123 const int buf_size = avpkt->size;
116 uint8_t *dst, *dst_end;
117 GetByteContext gb;
118 123 int count, ret, x = 0;
119
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
123 if (buf_size < 7)
121 return AVERROR_INVALIDDATA;
122
123
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
123 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
124 return ret;
125 123 dst = s->frame->data[0];
126 123 dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
127
128 123 bytestream2_init(&gb, avpkt->data, buf_size);
129
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
123 if (bytestream2_get_byte(&gb) != 0x42) {
131 avpriv_request_sample(avctx, "Unknown record type");
132 return AVERROR_INVALIDDATA;
133 }
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
123 if (bytestream2_get_byte(&gb)) {
135 avpriv_request_sample(avctx, "Padding bytes");
136 return AVERROR_PATCHWELCOME;
137 }
138 123 bytestream2_skip(&gb, 2);
139
140 do {
141 /* if statements are ordered by probability */
142 #define OP(gb, pixel, count) \
143 op(&dst, dst_end, (gb), (pixel), (count), &x, avctx->width, s->frame->linesize[0])
144
145 107209 int type = bytestream2_get_byte(&gb);
146 107209 count = type & 0x7F;
147 107209 type >>= 7;
148
2/2
✓ Branch 0 taken 74301 times.
✓ Branch 1 taken 32908 times.
107209 if (count) {
149
4/4
✓ Branch 0 taken 42887 times.
✓ Branch 1 taken 31414 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 74298 times.
74301 if (OP(type ? NULL : &gb, -1, count)) break;
150
2/2
✓ Branch 0 taken 28842 times.
✓ Branch 1 taken 4066 times.
32908 } else if (!type) {
151 int pixel;
152 28842 count = bytestream2_get_byte(&gb); /* count==0 gives nop */
153 28842 pixel = bytestream2_get_byte(&gb);
154
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 28839 times.
28842 if (OP(NULL, pixel, count)) break;
155 } else {
156 int pixel;
157 4066 type = bytestream2_get_le16(&gb);
158 4066 count = type & 0x3FFF;
159 4066 type >>= 14;
160
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 3950 times.
4066 if (!count) {
161
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 if (type == 0)
162 116 break; // stop
163 if (type == 2) {
164 avpriv_request_sample(avctx, "Unknown opcode");
165 return AVERROR_PATCHWELCOME;
166 }
167 continue;
168 }
169
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 3865 times.
3950 pixel = type == 3 ? bytestream2_get_byte(&gb) : -1;
170
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3934 times.
3950 if (type == 1) count += 0x4000;
171
4/4
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 3770 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3949 times.
3950 if (OP(type == 2 ? &gb : NULL, pixel, count)) break;
172 }
173
1/2
✓ Branch 1 taken 107086 times.
✗ Branch 2 not taken.
107086 } while (bytestream2_get_bytes_left(&gb) > 0);
174
175 123 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
176
177 123 *got_frame = 1;
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
123 if ((ret = av_frame_ref(data, s->frame)) < 0)
179 return ret;
180
181 123 return buf_size;
182 }
183
184 2 static av_cold int decode_end(AVCodecContext *avctx)
185 {
186 2 AnmContext *s = avctx->priv_data;
187
188 2 av_frame_free(&s->frame);
189 2 return 0;
190 }
191
192 const AVCodec ff_anm_decoder = {
193 .name = "anm",
194 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
195 .type = AVMEDIA_TYPE_VIDEO,
196 .id = AV_CODEC_ID_ANM,
197 .priv_data_size = sizeof(AnmContext),
198 .init = decode_init,
199 .close = decode_end,
200 .decode = decode_frame,
201 .capabilities = AV_CODEC_CAP_DR1,
202 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
203 };
204