FFmpeg
Loading...
Searching...
No Matches
rw2_parser.c
Go to the documentation of this file.
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"
29
30#include "parser.h"
31#include "parser_internal.h"
32
36
38 const uint8_t *buf, int buf_size)
39{
40 static const enum AVPixelFormat cfa_fmts[] = {
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];
72 if (bpp >= 8 && bpp <= 16)
73 avctx->bits_per_raw_sample = bpp;
74 }
75}
76
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
123 .priv_data_size = sizeof(RW2ParseContext),
124 .parse = rw2_parse,
126};
static double val(void *priv, double ch)
Definition aeval.c:77
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2667
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static struct @346255127015250356166251341105367306144006377143 state
@ AV_CODEC_ID_RW2
Definition codec_id.h:328
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
#define AV_RL32(p)
#define AV_RL16(p)
uint32_t type
Definition jpegmpfenc.c:80
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MKBETAG(a, b, c, d)
Definition macros.h:56
uint32_t tag
Definition movenc.c:2128
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
const FFCodecParser ff_rw2_parser
Definition rw2_parser.c:121
#define AV_PIX_FMT_BAYER_GRBG16
Definition pixfmt.h:583
#define AV_PIX_FMT_BAYER_BGGR16
Definition pixfmt.h:580
#define AV_PIX_FMT_BAYER_RGGB16
Definition pixfmt.h:581
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
#define AV_PIX_FMT_BAYER_GBRG16
Definition pixfmt.h:582
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition pixfmt.h:686
static int rw2_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition rw2_parser.c:77
static void rw2_set_params(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition rw2_parser.c:37
main external API structure.
Definition avcodec.h:443
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1576
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
int frame_start_found
Definition parser.h:34
ParseContext pc
Definition rw2_parser.c:34
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99