FFmpeg
proresdsp.c
Go to the documentation of this file.
1 /*
2  * Apple ProRes compatible decoder
3  *
4  * Copyright (c) 2010-2011 Maxim Poliakovski
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "config.h"
24 #include "libavutil/attributes.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/common.h"
27 #include "libavutil/intreadwrite.h"
28 #include "idctdsp.h"
29 #include "proresdsp.h"
30 
31 #define IN_IDCT_DEPTH 16
32 #define PRORES_ONLY
33 
34 #define BIT_DEPTH 10
35 #define EXTRA_SHIFT
36 #include "simple_idct_template.c"
37 #undef BIT_DEPTH
38 #undef EXTRA_SHIFT
39 
40 #define BIT_DEPTH 12
41 #include "simple_idct_template.c"
42 #undef BIT_DEPTH
43 #undef IN_IDCT_DEPTH
44 
45 /* 32bit iDCT for the ProRes RAW */
46 #define IN_IDCT_DEPTH 32
47 #define BIT_DEPTH 12
48 #include "simple_idct_template.c"
49 #undef BIT_DEPTH
50 #undef IN_IDCT_DEPTH
51 
52 /**
53  * Special version of ff_simple_idct_int16_10bit() which does dequantization
54  * and scales by a factor of 2 more between the two IDCTs to account
55  * for larger scale of input coefficients.
56  */
57 static void prores_idct_10(int16_t *restrict block, const int16_t *restrict qmat)
58 {
59  for (int i = 0; i < 64; i++)
60  block[i] *= qmat[i];
61 
62  for (int i = 0; i < 8; i++)
63  idctRowCondDC_extrashift_10(block + i*8, 2);
64 
65  for (int i = 0; i < 8; i++) {
66  block[i] += 8192;
67  idctSparseCol_extrashift_10(block + i);
68  }
69 }
70 
71 static void prores_idct_12(int16_t *restrict block, const int16_t *restrict qmat)
72 {
73  for (int i = 0; i < 64; i++)
74  block[i] *= qmat[i];
75 
76  for (int i = 0; i < 8; i++)
77  idctRowCondDC_int16_12bit(block + i*8, 0);
78 
79  for (int i = 0; i < 8; i++) {
80  block[i] += 8192;
81  idctSparseCol_int16_12bit(block + i);
82  }
83 }
84 
85 /*
86  * 32-bit iDCT for the ProRes RAW
87  * qmat must be s->qmat[i] * scale
88  */
89 static void prores_idct_bayer_32(int32_t *restrict block, const int16_t *restrict qmat)
90 {
91  for (int i = 0; i < 64; i++)
92  block[i] = (block[i] * qmat[i]) >> 1;
93 
94  for (int i = 0; i < 8; i++)
95  idctRowCondDC_int32_12bit(block + i*8, 0);
96 
97  for (int i = 0; i < 8; i++) {
98  block[i] += 8192;
99  idctSparseCol_int32_12bit(block + i);
100  }
101 }
102 
103 #define CLIP_MIN (1 << 2) ///< minimum value for clipping resulting pixels
104 #define CLIP_MAX_10 (1 << 10) - CLIP_MIN - 1 ///< maximum value for clipping resulting pixels
105 #define CLIP_MAX_12 (1 << 12) - CLIP_MIN - 1 ///< maximum value for clipping resulting pixels
106 
107 #define CLIP_10(x) (av_clip((x), CLIP_MIN, CLIP_MAX_10))
108 #define CLIP_12(x) (av_clip((x), CLIP_MIN, CLIP_MAX_12))
109 
110 /**
111  * Add bias value, clamp and output pixels of a slice
112  */
113 
114 static inline void put_pixel(uint16_t *dst, ptrdiff_t linesize, const int16_t *in, int bits_per_raw_sample) {
115  for (int y = 0; y < 8; y++, dst += linesize) {
116  for (int x = 0; x < 8; x++) {
117  int src_offset = (y << 3) + x;
118 
119  if (bits_per_raw_sample == 10) {
120  dst[x] = CLIP_10(in[src_offset]);
121  } else {//12b
122  dst[x] = CLIP_12(in[src_offset]);
123  }
124  }
125  }
126 }
127 
128 /* Apply the 8-point combined linearization curve (inv. transfer fn + encoder shaping) */
129 static inline void put_pixel_bayer_lin_curve_12(uint16_t *dst, ptrdiff_t linesize,
130  const int32_t *in, const uint16_t *lin_curve)
131 {
132  for (int y = 0; y < 8; y++, dst += linesize) {
133  for (int x = 0; x < 8; x++) {
134  /* Convert the 32-bit input into 16-bits (lrintf(x*16 - 15.5f) = 16) */
135  int u = av_clip_uint16(in[(y << 3) + x]*16 - 16);
136  uint32_t seg = (uint32_t)u >> 13;
137  uint32_t frac = (uint32_t)u & 0x1FFF;
138  uint32_t cp0 = lin_curve[seg];
139  uint32_t cp1 = seg < 7 ? lin_curve[seg + 1] : 0;
140  uint32_t o = (cp0 * 8192 + ((cp1 - cp0) & 0xFFFF) * frac + 4096) >> 13;
141  dst[x*2] = FFMIN(o, 0xFFFF);
142  }
143  }
144 }
145 
146 static void put_pixels_10(uint16_t *dst, ptrdiff_t linesize, const int16_t *in)
147 {
148  put_pixel(dst, linesize, in, 10);
149 }
150 
151 static void put_pixels_12(uint16_t *dst, ptrdiff_t linesize, const int16_t *in)
152 {
153  put_pixel(dst, linesize, in, 12);
154 }
155 
156 static void prores_idct_put_10_c(uint16_t *out, ptrdiff_t linesize, int16_t *block, const int16_t *qmat)
157 {
158  prores_idct_10(block, qmat);
159  put_pixels_10(out, linesize >> 1, block);
160 }
161 
162 static void prores_idct_put_12_c(uint16_t *out, ptrdiff_t linesize, int16_t *block, const int16_t *qmat)
163 {
164  prores_idct_12(block, qmat);
165  put_pixels_12(out, linesize >> 1, block);
166 }
167 
168 static void prores_idct_put_bayer_12_c(uint16_t *out, ptrdiff_t linesize,
169  int32_t *block, const int16_t *qmat,
170  const uint16_t *lin_curve)
171 {
173  put_pixel_bayer_lin_curve_12(out, linesize << 1, block, lin_curve);
174 }
175 
176 av_cold void ff_proresdsp_init(ProresDSPContext *dsp, int bits_per_raw_sample)
177 {
178  if (bits_per_raw_sample == 10) {
181  } else {
182  av_assert1(bits_per_raw_sample == 12);
186  }
187 
188 #if ARCH_X86 && HAVE_X86ASM
189  ff_proresdsp_init_x86(dsp, bits_per_raw_sample);
190 #endif
191 
193  dsp->idct_permutation_type);
194 }
out
static FILE * out
Definition: movenc.c:55
u
#define u(width, name, range_min, range_max)
Definition: cbs_apv.c:68
ProresDSPContext::idct_permutation_type
int idct_permutation_type
Definition: proresdsp.h:30
ff_proresdsp_init_x86
void ff_proresdsp_init_x86(ProresDSPContext *dsp, int bits_per_raw_sample)
Definition: proresdsp_init.c:33
put_pixel
static void put_pixel(uint16_t *dst, ptrdiff_t linesize, const int16_t *in, int bits_per_raw_sample)
Add bias value, clamp and output pixels of a slice.
Definition: proresdsp.c:114
put_pixels_12
static void put_pixels_12(uint16_t *dst, ptrdiff_t linesize, const int16_t *in)
Definition: proresdsp.c:151
ProresDSPContext
Definition: proresdsp.h:29
prores_idct_10
static void prores_idct_10(int16_t *restrict block, const int16_t *restrict qmat)
Special version of ff_simple_idct_int16_10bit() which does dequantization and scales by a factor of 2...
Definition: proresdsp.c:57
avassert.h
av_cold
#define av_cold
Definition: attributes.h:119
intreadwrite.h
CLIP_12
#define CLIP_12(x)
Definition: proresdsp.c:108
ProresDSPContext::idct_permutation
uint8_t idct_permutation[64]
Definition: proresdsp.h:31
ff_proresdsp_init
av_cold void ff_proresdsp_init(ProresDSPContext *dsp, int bits_per_raw_sample)
Definition: proresdsp.c:176
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
FF_IDCT_PERM_NONE
@ FF_IDCT_PERM_NONE
Definition: idctdsp.h:28
CLIP_10
#define CLIP_10(x)
Definition: proresdsp.c:107
attributes.h
proresdsp.h
ProresDSPContext::idct_put_bayer
void(* idct_put_bayer)(uint16_t *out, ptrdiff_t linesize, int32_t *block, const int16_t *qmat, const uint16_t *lin_curve)
Definition: proresdsp.h:33
common.h
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:58
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
put_pixel_bayer_lin_curve_12
static void put_pixel_bayer_lin_curve_12(uint16_t *dst, ptrdiff_t linesize, const int32_t *in, const uint16_t *lin_curve)
Definition: proresdsp.c:129
idctdsp.h
prores_idct_bayer_32
static void prores_idct_bayer_32(int32_t *restrict block, const int16_t *restrict qmat)
Definition: proresdsp.c:89
prores_idct_put_bayer_12_c
static void prores_idct_put_bayer_12_c(uint16_t *out, ptrdiff_t linesize, int32_t *block, const int16_t *qmat, const uint16_t *lin_curve)
Definition: proresdsp.c:168
prores_idct_12
static void prores_idct_12(int16_t *restrict block, const int16_t *restrict qmat)
Definition: proresdsp.c:71
put_pixels_10
static void put_pixels_10(uint16_t *dst, ptrdiff_t linesize, const int16_t *in)
Definition: proresdsp.c:146
av_clip_uint16
#define av_clip_uint16
Definition: common.h:112
prores_idct_put_10_c
static void prores_idct_put_10_c(uint16_t *out, ptrdiff_t linesize, int16_t *block, const int16_t *qmat)
Definition: proresdsp.c:156
ff_init_scantable_permutation
av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation, enum idct_permutation_type perm_type)
Definition: idctdsp.c:39
int32_t
int32_t
Definition: audioconvert.c:56
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
ProresDSPContext::idct_put
void(* idct_put)(uint16_t *out, ptrdiff_t linesize, int16_t *block, const int16_t *qmat)
Definition: proresdsp.h:32
prores_idct_put_12_c
static void prores_idct_put_12_c(uint16_t *out, ptrdiff_t linesize, int16_t *block, const int16_t *qmat)
Definition: proresdsp.c:162
simple_idct_template.c