FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rpza.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 106 123 86.2%
Functions: 4 4 100.0%
Branches: 53 68 77.9%

Line Branch Exec Source
1 /*
2 * Quicktime Video (RPZA) Video Decoder
3 * Copyright (C) 2003 The FFmpeg project
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 * QT RPZA Video Decoder by Roberto Togni
25 * For more information about the RPZA format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
27 *
28 * The RPZA decoder outputs RGB555 colorspace data.
29 *
30 * Note that this decoder reads big endian RGB555 pixel values from the
31 * bytestream, arranges them in the host's endian order, and outputs
32 * them to the final rendered map in the same host endian order. This is
33 * intended behavior as the libavcodec documentation states that RGB555
34 * pixels shall be stored in native CPU endianness.
35 */
36
37 #include <stdint.h>
38
39 #include "libavutil/attributes.h"
40 #include "libavutil/internal.h"
41 #include "avcodec.h"
42 #include "bytestream.h"
43 #include "codec_internal.h"
44 #include "decode.h"
45
46 typedef struct RpzaContext {
47
48 AVCodecContext *avctx;
49 AVFrame *frame;
50
51 GetByteContext gb;
52 } RpzaContext;
53
54 #define CHECK_BLOCK() \
55 if (total_blocks < 1) { \
56 av_log(s->avctx, AV_LOG_ERROR, \
57 "Block counter just went negative (this should not happen)\n"); \
58 return AVERROR_INVALIDDATA; \
59 } \
60
61 #define ADVANCE_BLOCK() \
62 { \
63 pixel_ptr += 4; \
64 if (pixel_ptr >= width) \
65 { \
66 pixel_ptr = 0; \
67 row_ptr += stride * 4; \
68 } \
69 total_blocks--; \
70 }
71
72 233 static int rpza_decode_stream(RpzaContext *s)
73 {
74 233 int width = s->avctx->width;
75 int stride, row_inc, ret;
76 int chunk_size;
77 233 uint16_t colorA = 0, colorB;
78 uint16_t color4[4];
79 uint16_t ta, tb;
80 uint16_t *pixels;
81
82 233 int row_ptr = 0;
83 233 int pixel_ptr = 0;
84 int block_ptr;
85 int pixel_x, pixel_y;
86 int total_blocks;
87
88 /* First byte is always 0xe1. Warn if it's different */
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 if (bytestream2_peek_byte(&s->gb) != 0xe1)
90 av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0xe1\n",
91 bytestream2_peek_byte(&s->gb));
92
93 /* Get chunk size, ignoring first byte */
94 233 chunk_size = bytestream2_get_be32(&s->gb) & 0x00FFFFFF;
95
96 /* If length mismatch use size from MOV file and try to decode anyway */
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 if (chunk_size != bytestream2_get_bytes_left(&s->gb) + 4)
98 av_log(s->avctx, AV_LOG_WARNING,
99 "MOV chunk size %d != encoded chunk size %d\n",
100 chunk_size,
101 bytestream2_get_bytes_left(&s->gb) + 4
102 );
103
104 /* Number of 4x4 blocks in frame. */
105 233 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
106
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 if (total_blocks / 32 > bytestream2_get_bytes_left(&s->gb))
108 return AVERROR_INVALIDDATA;
109
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 if ((ret = ff_reget_buffer(s->avctx, s->frame, 0)) < 0)
111 return ret;
112 233 pixels = (uint16_t *)s->frame->data[0];
113 233 stride = s->frame->linesize[0] / 2;
114 233 row_inc = stride - 4;
115
116 /* Process chunk data */
117
2/2
✓ Branch 1 taken 1016462 times.
✓ Branch 2 taken 233 times.
1016695 while (bytestream2_get_bytes_left(&s->gb)) {
118 1016462 uint8_t opcode = bytestream2_get_byte(&s->gb); /* Get opcode */
119
120 1016462 int n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
121
122 /* If opcode MSbit is 0, we need more data to decide what to do */
123
2/2
✓ Branch 0 taken 902537 times.
✓ Branch 1 taken 113925 times.
1016462 if ((opcode & 0x80) == 0) {
124 902537 colorA = (opcode << 8) | bytestream2_get_byte(&s->gb);
125 902537 opcode = 0;
126
2/2
✓ Branch 1 taken 51436 times.
✓ Branch 2 taken 851101 times.
902537 if ((bytestream2_peek_byte(&s->gb) & 0x80) != 0) {
127 /* Must behave as opcode 110xxxxx, using colorA computed
128 * above. Use fake opcode 0x20 to enter switch block at
129 * the right place */
130 51436 opcode = 0x20;
131 51436 n_blocks = 1;
132 }
133 }
134
135 1016462 n_blocks = FFMIN(n_blocks, total_blocks);
136
137
4/6
✓ Branch 0 taken 13412 times.
✓ Branch 1 taken 100513 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 51436 times.
✓ Branch 4 taken 851101 times.
✗ Branch 5 not taken.
1016462 switch (opcode & 0xe0) {
138
139 /* Skip blocks */
140 13412 case 0x80:
141
2/2
✓ Branch 0 taken 108261 times.
✓ Branch 1 taken 13412 times.
121673 while (n_blocks--) {
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108261 times.
108261 CHECK_BLOCK();
143
2/2
✓ Branch 0 taken 1737 times.
✓ Branch 1 taken 106524 times.
108261 ADVANCE_BLOCK();
144 }
145 13412 break;
146
147 /* Fill blocks with one color */
148 100513 case 0xa0:
149 100513 colorA = bytestream2_get_be16(&s->gb);
150
2/2
✓ Branch 0 taken 102052 times.
✓ Branch 1 taken 100513 times.
202565 while (n_blocks--) {
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102052 times.
102052 CHECK_BLOCK();
152 102052 block_ptr = row_ptr + pixel_ptr;
153
2/2
✓ Branch 0 taken 408208 times.
✓ Branch 1 taken 102052 times.
510260 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
154
2/2
✓ Branch 0 taken 1632832 times.
✓ Branch 1 taken 408208 times.
2041040 for (pixel_x = 0; pixel_x < 4; pixel_x++){
155 1632832 pixels[block_ptr] = colorA;
156 1632832 block_ptr++;
157 }
158 408208 block_ptr += row_inc;
159 }
160
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 101028 times.
102052 ADVANCE_BLOCK();
161 }
162 100513 break;
163
164 /* Fill blocks with 4 colors */
165 case 0xc0:
166 colorA = bytestream2_get_be16(&s->gb);
167 av_fallthrough;
168 51436 case 0x20:
169 51436 colorB = bytestream2_get_be16(&s->gb);
170
171 /* sort out the colors */
172 51436 color4[0] = colorB;
173 51436 color4[1] = 0;
174 51436 color4[2] = 0;
175 51436 color4[3] = colorA;
176
177 /* red components */
178 51436 ta = (colorA >> 10) & 0x1F;
179 51436 tb = (colorB >> 10) & 0x1F;
180 51436 color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
181 51436 color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
182
183 /* green components */
184 51436 ta = (colorA >> 5) & 0x1F;
185 51436 tb = (colorB >> 5) & 0x1F;
186 51436 color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
187 51436 color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
188
189 /* blue components */
190 51436 ta = colorA & 0x1F;
191 51436 tb = colorB & 0x1F;
192 51436 color4[1] |= ((11 * ta + 21 * tb) >> 5);
193 51436 color4[2] |= ((21 * ta + 11 * tb) >> 5);
194
195
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51436 times.
51436 if (bytestream2_get_bytes_left(&s->gb) < n_blocks * 4)
196 return AVERROR_INVALIDDATA;
197
2/2
✓ Branch 0 taken 51436 times.
✓ Branch 1 taken 51436 times.
102872 while (n_blocks--) {
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51436 times.
51436 CHECK_BLOCK();
199 51436 block_ptr = row_ptr + pixel_ptr;
200
2/2
✓ Branch 0 taken 205744 times.
✓ Branch 1 taken 51436 times.
257180 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
201 205744 uint8_t index = bytestream2_get_byteu(&s->gb);
202
2/2
✓ Branch 0 taken 822976 times.
✓ Branch 1 taken 205744 times.
1028720 for (pixel_x = 0; pixel_x < 4; pixel_x++){
203 822976 uint8_t idx = (index >> (2 * (3 - pixel_x))) & 0x03;
204 822976 pixels[block_ptr] = color4[idx];
205 822976 block_ptr++;
206 }
207 205744 block_ptr += row_inc;
208 }
209
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 51275 times.
51436 ADVANCE_BLOCK();
210 }
211 51436 break;
212
213 /* Fill block with 16 colors */
214 851101 case 0x00:
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 851101 times.
851101 if (bytestream2_get_bytes_left(&s->gb) < 30)
216 return AVERROR_INVALIDDATA;
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 851101 times.
851101 CHECK_BLOCK();
218 851101 block_ptr = row_ptr + pixel_ptr;
219
2/2
✓ Branch 0 taken 3404404 times.
✓ Branch 1 taken 851101 times.
4255505 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
220
2/2
✓ Branch 0 taken 13617616 times.
✓ Branch 1 taken 3404404 times.
17022020 for (pixel_x = 0; pixel_x < 4; pixel_x++){
221 /* We already have color of upper left pixel */
222
4/4
✓ Branch 0 taken 3404404 times.
✓ Branch 1 taken 10213212 times.
✓ Branch 2 taken 2553303 times.
✓ Branch 3 taken 851101 times.
13617616 if ((pixel_y != 0) || (pixel_x != 0))
223 12766515 colorA = bytestream2_get_be16u(&s->gb);
224 13617616 pixels[block_ptr] = colorA;
225 13617616 block_ptr++;
226 }
227 3404404 block_ptr += row_inc;
228 }
229
2/2
✓ Branch 0 taken 10308 times.
✓ Branch 1 taken 840793 times.
851101 ADVANCE_BLOCK();
230 851101 break;
231
232 /* Unknown opcode */
233 default:
234 av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
235 " Skip remaining %d bytes of chunk data.\n", opcode,
236 bytestream2_get_bytes_left(&s->gb));
237 return AVERROR_INVALIDDATA;
238 } /* Opcode switch */
239 }
240
241 233 return 0;
242 }
243
244 10 static av_cold int rpza_decode_init(AVCodecContext *avctx)
245 {
246 10 RpzaContext *s = avctx->priv_data;
247
248 10 s->avctx = avctx;
249 10 avctx->pix_fmt = AV_PIX_FMT_RGB555;
250
251 10 s->frame = av_frame_alloc();
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!s->frame)
253 return AVERROR(ENOMEM);
254
255 10 return 0;
256 }
257
258 233 static int rpza_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
259 int *got_frame, AVPacket *avpkt)
260 {
261 233 RpzaContext *s = avctx->priv_data;
262 int ret;
263
264 233 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
265
266 233 ret = rpza_decode_stream(s);
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 233 times.
233 if (ret < 0)
268 return ret;
269
270
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
271 return ret;
272
273 233 *got_frame = 1;
274
275 /* always report that the buffer was completely consumed */
276 233 return avpkt->size;
277 }
278
279 10 static av_cold int rpza_decode_end(AVCodecContext *avctx)
280 {
281 10 RpzaContext *s = avctx->priv_data;
282
283 10 av_frame_free(&s->frame);
284
285 10 return 0;
286 }
287
288 const FFCodec ff_rpza_decoder = {
289 .p.name = "rpza",
290 CODEC_LONG_NAME("QuickTime video (RPZA)"),
291 .p.type = AVMEDIA_TYPE_VIDEO,
292 .p.id = AV_CODEC_ID_RPZA,
293 .priv_data_size = sizeof(RpzaContext),
294 .init = rpza_decode_init,
295 .close = rpza_decode_end,
296 FF_CODEC_DECODE_CB(rpza_decode_frame),
297 .p.capabilities = AV_CODEC_CAP_DR1,
298 };
299