FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sgirledec.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 38 44 86.4%
Functions: 4 4 100.0%
Branches: 21 34 61.8%

Line Branch Exec Source
1 /*
2 * Silicon Graphics RLE 8-bit video decoder
3 * Copyright (c) 2012 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 * Silicon Graphics RLE 8-bit video decoder
25 * @note Data is packed in rbg323 with rle, contained in mv or mov.
26 * The algorithm and pixfmt are subtly different from SGI images.
27 */
28
29 #include "libavutil/common.h"
30
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34
35 2 static av_cold int sgirle_decode_init(AVCodecContext *avctx)
36 {
37 2 avctx->pix_fmt = AV_PIX_FMT_BGR8;
38 2 return 0;
39 }
40
41 /**
42 * Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8
43 * SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb)
44 */
45 #define RBG323_TO_BGR8(x) ((((x) << 3) & 0xC0) | \
46 (((x) << 3) & 0x38) | \
47 (((x) >> 5) & 7))
48 static av_always_inline
49 89431 void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
50 {
51 int i;
52
2/2
✓ Branch 0 taken 1584786 times.
✓ Branch 1 taken 89431 times.
1674217 for (i = 0; i < size; i++)
53 1584786 dst[i] = RBG323_TO_BGR8(src[i]);
54 89431 }
55
56 /**
57 * @param[out] dst Destination buffer
58 * @param[in] src Source buffer
59 * @param src_size Source buffer size (bytes)
60 * @param width Width of destination buffer (pixels)
61 * @param height Height of destination buffer (pixels)
62 * @param linesize Line size of destination buffer (bytes)
63 *
64 * @return <0 on error
65 */
66 31 static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst,
67 const uint8_t *src, int src_size,
68 int width, int height, ptrdiff_t linesize)
69 {
70 31 const uint8_t *src_end = src + src_size;
71 31 int x = 0, y = 0;
72
73 #define INC_XY(n) \
74 x += n; \
75 if (x >= width) { \
76 y++; \
77 if (y >= height) \
78 return 0; \
79 x = 0; \
80 }
81
82
1/2
✓ Branch 0 taken 346427 times.
✗ Branch 1 not taken.
346427 while (src_end - src >= 2) {
83 346427 uint8_t v = *src++;
84
3/4
✓ Branch 0 taken 346427 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 256996 times.
✓ Branch 3 taken 89431 times.
346427 if (v > 0 && v < 0xC0) {
85 do {
86 268566 int length = FFMIN(v, width - x);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 268566 times.
268566 if (length <= 0)
88 break;
89 268566 memset(dst + y * linesize + x, RBG323_TO_BGR8(*src), length);
90
4/4
✓ Branch 0 taken 11625 times.
✓ Branch 1 taken 256941 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 11594 times.
268566 INC_XY(length);
91 268535 v -= length;
92
2/2
✓ Branch 0 taken 11570 times.
✓ Branch 1 taken 256965 times.
268535 } while (v > 0);
93 256965 src++;
94
1/2
✓ Branch 0 taken 89431 times.
✗ Branch 1 not taken.
89431 } else if (v >= 0xC1) {
95 89431 v -= 0xC0;
96 do {
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89431 times.
89431 int length = FFMIN3(v, width - x, src_end - src);
98
2/4
✓ Branch 0 taken 89431 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 89431 times.
✗ Branch 3 not taken.
89431 if (src_end - src < length || length <= 0)
99 break;
100 89431 rbg323_to_bgr8(dst + y * linesize + x, src, length);
101
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 89431 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
89431 INC_XY(length);
102 89431 src += length;
103 89431 v -= length;
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89431 times.
89431 } while (v > 0);
105 } else {
106 avpriv_request_sample(avctx, "opcode %d", v);
107 return AVERROR_PATCHWELCOME;
108 }
109 }
110 return 0;
111 }
112
113 31 static int sgirle_decode_frame(AVCodecContext *avctx, AVFrame *frame,
114 int *got_frame, AVPacket *avpkt)
115 {
116 int ret;
117
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
31 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
119 return ret;
120
121 31 ret = decode_sgirle8(avctx, frame->data[0], avpkt->data, avpkt->size,
122 31 avctx->width, avctx->height, frame->linesize[0]);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
124 return ret;
125
126 31 frame->pict_type = AV_PICTURE_TYPE_I;
127 31 frame->flags |= AV_FRAME_FLAG_KEY;
128
129 31 *got_frame = 1;
130
131 31 return avpkt->size;
132 }
133
134 const FFCodec ff_sgirle_decoder = {
135 .p.name = "sgirle",
136 CODEC_LONG_NAME("Silicon Graphics RLE 8-bit video"),
137 .p.type = AVMEDIA_TYPE_VIDEO,
138 .p.id = AV_CODEC_ID_SGIRLE,
139 .init = sgirle_decode_init,
140 FF_CODEC_DECODE_CB(sgirle_decode_frame),
141 .p.capabilities = AV_CODEC_CAP_DR1,
142 };
143