36#define BITSTREAM_READER_LE
37#define CACHED_BITSTREAM_READER 1
51#define RW2_VLC_BITS 11
52#define RW2_JOINT_BITS 16
53#define RW2_SYM_DELTA 0x2000
54#define RW2_SYM_ESC 0x4000
69 int shift =
s->h.c.code_shift[k], extra = k -
shift;
74 d = (m <<
shift) & 0xFFFF;
76 d += !
shift - (1 << k);
79 d = !
shift - (1 << k);
89 while (seg < 5 && v >=
c->gamma_point[seg + 1])
94 uint32_t sh =
c->gamma_shift[seg];
95 uint32_t
r = v -
c->gamma_point[seg];
96 if ((sh & 0x1F) == 31)
97 r = seg == 5 ? 0xFFFF : 0;
100 else if ((sh & 0x1F) == 15)
103 r = (
r + (1 << ((sh & 0x1F) - 1))) >> (sh & 0x1F);
114 for (
int k = 0; k < 17; k++) {
115 int len =
c->code_len[k],
code =
c->code_val[k];
122 for (
int m = 0; m < (1 << k); m++) {
123 int d = k && !(m >> (k - 1)) ? m + 1 - (1 << k) : m;
130 &
s->codes[0].len,
sizeof(*
s->codes), 1,
131 &
s->codes[0].code,
sizeof(*
s->codes), 2,
132 &
s->codes[0].sym,
sizeof(*
s->codes), 2,
139 for (
int i = 0;
i <=
c->datamax;
i++) {
141 s->use_gamma |=
s->gamma[
i] !=
i;
148 int jobnr,
int threadnr)
154 const uint16_t *gamma =
s->use_gamma ?
s->gamma :
NULL;
155 const int datamax =
s->h.c.datamax;
156 int line_base[4], cur[4];
164 for (
int i = 0;
i < 4;
i++)
165 line_base[
i] =
s->h.c.initial[
i];
167 for (
int y = 0; y < st->
height; y += 2) {
169 uint16_t *dst1 = (uint16_t *)((uint8_t *)dst0 +
stride);
172 memcpy(cur, line_base,
sizeof(cur));
173 for (
int x = 0; x < st->
width; x += 2) {
175 static const uint8_t slot[4] = { 0, 2, 1, 3 };
176 for (
int i = 0;
i < 4;
i++) {
184 dst0[x] = gamma[cur[0]]; dst0[x + 1] = gamma[cur[1]];
185 dst1[x] = gamma[cur[2]]; dst1[x + 1] = gamma[cur[3]];
187 dst0[x] = cur[0]; dst0[x + 1] = cur[1];
188 dst1[x] = cur[2]; dst1[x + 1] = cur[3];
192 memcpy(line_base, cur,
sizeof(line_base));
203 unsigned expected,
int max)
208 n = bytestream2_get_le16(gb);
218 memset(
h, 0,
sizeof(*
h));
221 if (bytestream2_get_le32(&gb) !=
MKTAG(
'I',
'I',
'U',
'\0'))
223 int64_t ifd0 = bytestream2_get_le32(&gb);
224 if (ifd0 < 8 || ifd0 + 2 >
size)
228 int nb_entries = bytestream2_get_le16(&gb);
236 for (
int i = 0;
i < nb_entries;
i++) {
237 unsigned tag,
type, count, n, v;
246 const uint8_t *rec =
data + ifd0 + 2 + 12*
i;
247 if ((
tag == 0x2E ||
tag == 0x111 ||
tag == 0x127) &&
AV_RL32(rec + 8) >= ifd_end)
249 if (
tag >= 0x100 &&
tag != 0x111 &&
tag != 0x112 &&
tag != 0x117 &&
250 !(
tag >= 0x118 &&
tag <= 0x127)) {
251 memcpy(
h->exif +
h->exif_count*12, rec, 12);
257 case 0x03:
h->raw_height =
ff_tget(&gb,
type, 1);
break;
258 case 0x04:
case 0x05:
case 0x06:
case 0x07:
263 case 0x0E:
case 0x0F:
case 0x10:
266 case 0x1C:
case 0x1D:
case 0x1E:
269 case 0x24:
case 0x25:
case 0x26:
272 case 0x2D:
h->raw_format =
ff_tget(&gb,
type, 1);
break;
275 for (
int j = 0; j < n; j++)
276 c->gamma_shift[j] = bytestream2_get_le32(&gb);
280 for (
int j = 0; j < n; j++) {
282 c->gamma_point[j] = bytestream2_get_le16(&gb);
285 case 0x3B:
c->datamax =
ff_tget(&gb,
type, 1) & 0xFFFF;
break;
286 case 0x3C:
case 0x3D:
case 0x3E:
case 0x3F:
291 for (
int k = 0; k < n; k++) {
292 v = bytestream2_get_le16(&gb);
294 v = bytestream2_get_le16(&gb);
298 for (
int j = 0; j < k &&
len; j++)
299 if (
c->code_len[j] &&
c->code_len[j] <=
len &&
300 code >> (
len -
c->code_len[j]) ==
c->code_val[j])
302 c->code_len[k] =
len;
303 c->code_val[k] =
code;
308 for (
int j = 0; j < n; j++) {
309 v = bytestream2_get_le16(&gb);
310 c->code_shift[j] =
FFMIN(v, 64) & 0x1F;
317 case 0x44:
case 0x45:
case 0x46:
319 for (
int j = 0; j < n; j++) {
320 v = bytestream2_get_le32(&gb);
322 h->stripes[j].offset = v;
323 else if (
tag == 0x45)
324 h->stripes[j].left = v;
326 h->stripes[j].size_bits = v;
329 case 0x47:
case 0x48:
331 for (
int j = 0; j < n; j++) {
332 v = bytestream2_get_le16(&gb);
334 h->stripes[j].width = v;
336 h->stripes[j].height = v;
339 case 0x112:
h->orientation =
ff_tget(&gb,
type, 1);
break;
354 if (
h->nb_stripes < 1)
358 for (
int i = 0;
i <
h->nb_stripes;
i++) {
363 st->
offset > payload_size ||
371 if (
left !=
h->raw_width)
397 if (!
h->exif_count || off + 2 + (
int64_t)
h->exif_count*12 + 4 > keep)
408 memcpy(buf + off + 2,
h->exif,
h->exif_count * 12);
409 AV_WL32(buf + off + 2 +
h->exif_count*12, 0);
421 const uint8_t *hdr,
int hdr_size)
428 if (
h->border[0] >= 0 &&
h->border[1] >= 0 &&
429 h->border[0] <
h->border[2] &&
h->border[2] <=
h->raw_height &&
430 h->border[1] <
h->border[3] &&
h->border[3] <=
h->raw_width) {
431 frame->crop_top =
h->border[0] & ~1;
432 frame->crop_left =
h->border[1] & ~1;
433 frame->crop_bottom = (
h->raw_height -
h->border[2]) & ~1;
434 frame->crop_right = (
h->raw_width -
h->border[3]) & ~1;
437 if (
h->orientation > 1 &&
h->orientation <= 8) {
448 int white =
FFMAX3(
h->linearity[0],
h->linearity[1],
h->linearity[2]);
450 white = (1 <<
h->bpp) - 1;
483 if (
h->raw_format != 8) {
490 if (
h->cfa < 1 ||
h->cfa > 4 ||
h->bpp < 8 ||
h->bpp > 16 ||
491 (
h->raw_width & 1) || (
h->raw_height & 1))
498 s->payload = avpkt->
data;
504 if (cfa_fmts[
h->cfa - 1] !=
s->pix_fmt) {
505 s->pix_fmt = cfa_fmts[
h->cfa - 1];
535 for (
int i = 0;
i <
h->nb_stripes;
i++) {
543 ret =
hwaccel->end_frame(avctx);
556 for (
int i = 0;
i <
h->nb_stripes;
i++) {
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
const FFCodec ff_rw2_decoder
static av_cold void close(AVCodecParserContext *s)
Libavcodec external API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
refcounted data buffer API
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
#define i(width, name, range_min, range_max)
#define UPDATE_THREAD_CONTEXT(func)
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters if all frames are skipped due to the skip_frame setting...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx, AVFrame *frame, enum AVFrameSideDataType type, AVBufferRef **buf)
Similar to ff_frame_new_side_data, but using an existing buffer ref.
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
Allocate a hwaccel frame private data if the provided avctx uses a hwaccel method that needs it.
int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame, enum AVFrameSideDataType type, size_t size, AVFrameSideData **psd)
Wrapper around av_frame_new_side_data, which rejects side data overridden by the demuxer.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
static enum AVPixelFormat pix_fmt
int(* init)(AVBSFContext *ctx)
int av_exif_orientation_to_matrix(int32_t *matrix, int orientation)
Convert an orientation constant used by EXIF's orientation tag into a display matrix used by AV_FRAME...
EXIF metadata parser - internal functions.
static const char * hwaccel
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static int get_bits_left(GetBitContext *gb)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
@ AVDISCARD_ALL
discard all
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
@ AV_FRAME_DATA_EXIF
Exchangeable image file format metadata.
@ AV_FRAME_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static AVRational av_make_q(int num, int den)
Create an AVRational.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
@ AV_PICTURE_TYPE_I
Intra.
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
static av_cold int decode_init(AVCodecContext *avctx)
static av_cold int decode_end(AVCodecContext *avctx)
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
static int shift(int a, int b)
Multithreading API for decoders.
common internal API header
const uint8_t ff_reverse[256]
static enum AVPixelFormat pix_fmts[]
#define MKTAG(a, b, c, d)
Memory handling functions.
#define AV_PIX_FMT_BAYER_GRBG16
#define AV_PIX_FMT_BAYER_BGGR16
#define AV_PIX_FMT_BAYER_RGGB16
AVPixelFormat
Pixel format.
#define AV_PIX_FMT_BAYER_GBRG16
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
AVRawColorParams * av_raw_color_params_create_side_data(AVFrame *frame)
Allocate and add an AVRawColorParams structure to an existing AVFrame as AV_FRAME_DATA_RAW_COLOR_PARA...
@ AV_RAW_COLOR_PARAMS_RW2
The union is valid when interpreted as AVRW2RawColorParams (codec.rw2).
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
#define RW2_MAX_IFD0_ENTRIES
static int decode_stripe(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
static int read_array_count(GetByteContext *gb, unsigned type, unsigned count, unsigned expected, int max)
static av_always_inline int get_delta(const RW2DecContext *s, GetBitContext *bc, int *delta)
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
static int build_tables(RW2DecContext *s)
static int gamma_curve(const RW2Coding *c, uint32_t v)
static int check_stripes(AVCodecContext *avctx, const RW2Header *h, int payload_size)
static int export_metadata(AVCodecContext *avctx, AVFrame *frame, const uint8_t *hdr, int hdr_size)
static enum AVPixelFormat get_pixel_format(AVCodecContext *avctx, enum AVPixelFormat pix_fmt)
static av_cold int decode_init(AVCodecContext *avctx)
static av_cold int decode_end(AVCodecContext *avctx)
static int export_exif(AVCodecContext *avctx, AVFrame *frame, const uint8_t *data, int size)
static int parse_header(RW2Header *h, const uint8_t *data, int size)
A reference to a data buffer.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
enum AVDiscard skip_frame
Skip decoding for selected frames.
Structure to hold side data for an AVFrame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
AVRational wb_red
White balance multiplier for the red channel, applied pre-debayer.
AVRational wb_blue
White balance multiplier for the blue channel, applied pre-debayer.
Per-frame color information for a RAW camera codec.
AVRational black_level
Lowest valid raw sample code (sensor black point)
enum AVRawColorParamsType type
Selects which member of codec is valid.
union AVRawColorParams::@064000052037250306123267111255172234366153040136 codec
Additional codec-specific fields.
AVRational white_level
Highest valid raw sample code (sensor white point)
enum AVPixelFormat pix_fmt
#define avpriv_request_sample(...)
unsigned ff_tget(GetByteContext *gb, int type, int le)
Reads a byte from the bytestream using given endianness.
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type, unsigned *count, int *next)
Reads the first 3 fields of a TIFF tag, which are the tag id, the tag type and the count of values fo...
int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Build VLC decoding tables suitable for use with get_vlc2().
void ff_vlc_free(VLC *vlc)
#define VLC_INIT_OUTPUT_LE