FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rw2_parser.c
Date: 2026-10-09 00:41:53
Exec Total Coverage
Lines: 0 50 0.0%
Functions: 0 2 0.0%
Branches: 0 43 0.0%

Line Branch Exec Source
1 /*
2 * Panasonic RW2 parser
3 * Copyright (c) 2026 Lynne <dev@lynne.ee>
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 * Delimits frames on the "IIU\0" magic and reads IFD0 to set the dimensions
24 * and pixel format without decoding.
25 */
26
27 #include "libavutil/common.h"
28 #include "libavutil/intreadwrite.h"
29
30 #include "parser.h"
31 #include "parser_internal.h"
32
33 typedef struct RW2ParseContext {
34 ParseContext pc;
35 } RW2ParseContext;
36
37 ✗ static void rw2_set_params(AVCodecParserContext *s, AVCodecContext *avctx,
38 const uint8_t *buf, int buf_size)
39 {
40 static const enum AVPixelFormat cfa_fmts[] = {
41 AV_PIX_FMT_BAYER_RGGB16, AV_PIX_FMT_BAYER_GRBG16,
42 AV_PIX_FMT_BAYER_GBRG16, AV_PIX_FMT_BAYER_BGGR16,
43 };
44
45 ✗ if (buf_size < 8 || AV_RL32(buf) != MKTAG('I', 'I', 'U', 0))
46 ✗ return;
47
48 ✗ int64_t ifd = AV_RL32(buf + 4);
49 ✗ if (ifd < 8 || ifd + 2 > buf_size)
50 ✗ return;
51
52 ✗ int width = 0, height = 0, cfa = 0, bpp = 0;
53 ✗ int n = FFMIN(AV_RL16(buf + ifd), (buf_size - ifd - 2) / 12);
54 ✗ for (int i = 0; i < n; i++) {
55 ✗ const uint8_t *e = buf + ifd + 2 + 12*i;
56 ✗ unsigned tag = AV_RL16(e), type = AV_RL16(e + 2), val = AV_RL32(e + 8);
57 ✗ unsigned v = type == 3 ? val & 0xFFFF : val;
58
59 ✗ switch (tag) {
60 ✗ case 0x02: width = v; break;
61 ✗ case 0x03: height = v; break;
62 ✗ case 0x09: cfa = v; break;
63 ✗ case 0x0A: bpp = v; break;
64 }
65 }
66
67 ✗ if (width > 0 && height > 0 && cfa >= 1 && cfa <= 4) {
68 ✗ s->width = width;
69 ✗ s->height = height;
70 ✗ s->format = cfa_fmts[cfa - 1];
71 ✗ avctx->color_trc = AVCOL_TRC_LINEAR;
72 ✗ if (bpp >= 8 && bpp <= 16)
73 ✗ avctx->bits_per_raw_sample = bpp;
74 }
75 }
76
77 ✗ static int rw2_parse(AVCodecParserContext *s, AVCodecContext *avctx,
78 const uint8_t **poutbuf, int *poutbuf_size,
79 const uint8_t *buf, int buf_size)
80 {
81 ✗ RW2ParseContext *ipc = s->priv_data;
82 ✗ uint32_t state = ipc->pc.state;
83 ✗ int next = END_NOT_FOUND;
84
85 ✗ s->key_frame = 1;
86 ✗ s->pict_type = AV_PICTURE_TYPE_I;
87
88 /* The header lands in the first chunk, so the parameters are known without
89 * waiting for, or decoding, the whole frame */
90 ✗ if (buf_size >= 8 && AV_RL32(buf) == MKTAG('I', 'I', 'U', 0))
91 ✗ rw2_set_params(s, avctx, buf, buf_size);
92
93 ✗ if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
94 ✗ next = buf_size;
95 } else {
96 ✗ for (int i = 0; i < buf_size; i++) {
97 ✗ state = (state << 8) | buf[i];
98 ✗ if (state == MKBETAG('I', 'I', 'U', 0)) {
99 /* The next frame's magic ends this one */
100 ✗ if (ipc->pc.frame_start_found) {
101 ✗ next = i - 3;
102 ✗ ipc->pc.frame_start_found = 0;
103 ✗ break;
104 }
105 ✗ ipc->pc.frame_start_found = 1;
106 }
107 }
108 ✗ ipc->pc.state = state;
109 ✗ if (ff_combine_frame(&ipc->pc, next, &buf, &buf_size) < 0) {
110 ✗ *poutbuf = NULL;
111 ✗ *poutbuf_size = 0;
112 ✗ return buf_size;
113 }
114 }
115
116 ✗ *poutbuf = buf;
117 ✗ *poutbuf_size = buf_size;
118 ✗ return next;
119 }
120
121 const FFCodecParser ff_rw2_parser = {
122 PARSER_CODEC_LIST(AV_CODEC_ID_RW2),
123 .priv_data_size = sizeof(RW2ParseContext),
124 .parse = rw2_parse,
125 .close = ff_parse_close,
126 };
127