Signed-off-by: Martin Storsjö <martin@martin.st>
Maxim Poliakovski authored on 2011/09/21 20:09:32... | ... |
@@ -341,6 +341,7 @@ following image formats are supported: |
341 | 341 |
@tab Used in Chinese MP3 players. |
342 | 342 |
@item ANSI/ASCII art @tab @tab X |
343 | 343 |
@item Apple MJPEG-B @tab @tab X |
344 |
+@item Apple ProRes @tab @tab X |
|
344 | 345 |
@item Apple QuickDraw @tab @tab X |
345 | 346 |
@tab fourcc: qdrw |
346 | 347 |
@item Asus v1 @tab X @tab X |
... | ... |
@@ -295,6 +295,7 @@ OBJS-$(CONFIG_PNG_DECODER) += png.o pngdec.o |
295 | 295 |
OBJS-$(CONFIG_PNG_ENCODER) += png.o pngenc.o |
296 | 296 |
OBJS-$(CONFIG_PPM_DECODER) += pnmdec.o pnm.o |
297 | 297 |
OBJS-$(CONFIG_PPM_ENCODER) += pnmenc.o pnm.o |
298 |
+OBJS-$(CONFIG_PRORES_DECODER) += proresdec.o |
|
298 | 299 |
OBJS-$(CONFIG_PTX_DECODER) += ptx.o |
299 | 300 |
OBJS-$(CONFIG_QCELP_DECODER) += qcelpdec.o celp_math.o \ |
300 | 301 |
celp_filters.o acelp_vectors.o \ |
... | ... |
@@ -164,6 +164,7 @@ void avcodec_register_all(void) |
164 | 164 |
REGISTER_DECODER (PICTOR, pictor); |
165 | 165 |
REGISTER_ENCDEC (PNG, png); |
166 | 166 |
REGISTER_ENCDEC (PPM, ppm); |
167 |
+ REGISTER_DECODER (PRORES, prores); |
|
167 | 168 |
REGISTER_DECODER (PTX, ptx); |
168 | 169 |
REGISTER_DECODER (QDRAW, qdraw); |
169 | 170 |
REGISTER_DECODER (QPEG, qpeg); |
170 | 171 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,733 @@ |
0 |
+/* |
|
1 |
+ * Apple ProRes compatible decoder |
|
2 |
+ * |
|
3 |
+ * Copyright (c) 2010-2011 Maxim Poliakovski |
|
4 |
+ * |
|
5 |
+ * This file is part of Libav. |
|
6 |
+ * |
|
7 |
+ * Libav 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 |
+ * Libav 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 Libav; 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 |
+ * This is a decoder for Apple ProRes 422 SD/HQ/LT/Proxy and ProRes 4444. |
|
25 |
+ * It is used for storing and editing high definition video data in Apple's Final Cut Pro. |
|
26 |
+ * |
|
27 |
+ * @see http://wiki.multimedia.cx/index.php?title=Apple_ProRes |
|
28 |
+ */ |
|
29 |
+ |
|
30 |
+#define A32_BITSTREAM_READER // some ProRes vlc codes require up to 28 bits to be read at once |
|
31 |
+ |
|
32 |
+#include <stdint.h> |
|
33 |
+ |
|
34 |
+#include "libavutil/intmath.h" |
|
35 |
+#include "avcodec.h" |
|
36 |
+#include "dsputil.h" |
|
37 |
+#include "get_bits.h" |
|
38 |
+ |
|
39 |
+#define BITS_PER_SAMPLE 10 ///< output precision of that decoder |
|
40 |
+#define BIAS (1 << (BITS_PER_SAMPLE - 1)) ///< bias value for converting signed pixels into unsigned ones |
|
41 |
+#define CLIP_MIN (1 << (BITS_PER_SAMPLE - 8)) ///< minimum value for clipping resulting pixels |
|
42 |
+#define CLIP_MAX (1 << BITS_PER_SAMPLE) - CLIP_MIN - 1 ///< maximum value for clipping resulting pixels |
|
43 |
+ |
|
44 |
+ |
|
45 |
+typedef struct { |
|
46 |
+ DSPContext dsp; |
|
47 |
+ AVFrame picture; |
|
48 |
+ ScanTable scantable; |
|
49 |
+ int scantable_type; ///< -1 = uninitialized, 0 = progressive, 1/2 = interlaced |
|
50 |
+ |
|
51 |
+ int frame_type; ///< 0 = progressive, 1 = top-field first, 2 = bottom-field first |
|
52 |
+ int pic_format; ///< 2 = 422, 3 = 444 |
|
53 |
+ uint8_t qmat_luma[64]; ///< dequantization matrix for luma |
|
54 |
+ uint8_t qmat_chroma[64]; ///< dequantization matrix for chroma |
|
55 |
+ int qmat_changed; ///< 1 - global quantization matrices changed |
|
56 |
+ int prev_slice_sf; ///< scalefactor of the previous decoded slice |
|
57 |
+ DECLARE_ALIGNED(16, int16_t, qmat_luma_scaled[64]); |
|
58 |
+ DECLARE_ALIGNED(16, int16_t, qmat_chroma_scaled[64]); |
|
59 |
+ DECLARE_ALIGNED(16, DCTELEM, blocks[8 * 4 * 64]); |
|
60 |
+ int total_slices; ///< total number of slices in a picture |
|
61 |
+ const uint8_t **slice_data_index; ///< array of pointers to the data of each slice |
|
62 |
+ int chroma_factor; |
|
63 |
+ int mb_chroma_factor; |
|
64 |
+ int num_chroma_blocks; ///< number of chrominance blocks in a macroblock |
|
65 |
+ int num_x_slices; |
|
66 |
+ int num_y_slices; |
|
67 |
+ int slice_width_factor; |
|
68 |
+ int slice_height_factor; |
|
69 |
+ int num_x_mbs; |
|
70 |
+ int num_y_mbs; |
|
71 |
+} ProresContext; |
|
72 |
+ |
|
73 |
+ |
|
74 |
+static const uint8_t progressive_scan[64] = { |
|
75 |
+ 0, 1, 8, 9, 2, 3, 10, 11, |
|
76 |
+ 16, 17, 24, 25, 18, 19, 26, 27, |
|
77 |
+ 4, 5, 12, 20, 13, 6, 7, 14, |
|
78 |
+ 21, 28, 29, 22, 15, 23, 30, 31, |
|
79 |
+ 32, 33, 40, 48, 41, 34, 35, 42, |
|
80 |
+ 49, 56, 57, 50, 43, 36, 37, 44, |
|
81 |
+ 51, 58, 59, 52, 45, 38, 39, 46, |
|
82 |
+ 53, 60, 61, 54, 47, 55, 62, 63 |
|
83 |
+}; |
|
84 |
+ |
|
85 |
+static const uint8_t interlaced_scan[64] = { |
|
86 |
+ 0, 8, 1, 9, 16, 24, 17, 25, |
|
87 |
+ 2, 10, 3, 11, 18, 26, 19, 27, |
|
88 |
+ 32, 40, 33, 34, 41, 48, 56, 49, |
|
89 |
+ 42, 35, 43, 50, 57, 58, 51, 59, |
|
90 |
+ 4, 12, 5, 6, 13, 20, 28, 21, |
|
91 |
+ 14, 7, 15, 22, 29, 36, 44, 37, |
|
92 |
+ 30, 23, 31, 38, 45, 52, 60, 53, |
|
93 |
+ 46, 39, 47, 54, 61, 62, 55, 63 |
|
94 |
+}; |
|
95 |
+ |
|
96 |
+ |
|
97 |
+static av_cold int decode_init(AVCodecContext *avctx) |
|
98 |
+{ |
|
99 |
+ ProresContext *ctx = avctx->priv_data; |
|
100 |
+ |
|
101 |
+ ctx->total_slices = 0; |
|
102 |
+ ctx->slice_data_index = 0; |
|
103 |
+ |
|
104 |
+ avctx->pix_fmt = PIX_FMT_YUV422P10; // set default pixel format |
|
105 |
+ |
|
106 |
+ avctx->bits_per_raw_sample = BITS_PER_SAMPLE; |
|
107 |
+ dsputil_init(&ctx->dsp, avctx); |
|
108 |
+ |
|
109 |
+ avctx->coded_frame = &ctx->picture; |
|
110 |
+ avcodec_get_frame_defaults(&ctx->picture); |
|
111 |
+ ctx->picture.type = AV_PICTURE_TYPE_I; |
|
112 |
+ ctx->picture.key_frame = 1; |
|
113 |
+ |
|
114 |
+ ctx->scantable_type = -1; // set scantable type to uninitialized |
|
115 |
+ memset(ctx->qmat_luma, 4, 64); |
|
116 |
+ memset(ctx->qmat_chroma, 4, 64); |
|
117 |
+ ctx->prev_slice_sf = 0; |
|
118 |
+ |
|
119 |
+ return 0; |
|
120 |
+} |
|
121 |
+ |
|
122 |
+ |
|
123 |
+static int decode_frame_header(ProresContext *ctx, const uint8_t *buf, |
|
124 |
+ const int data_size, AVCodecContext *avctx) |
|
125 |
+{ |
|
126 |
+ int hdr_size, version, width, height, flags; |
|
127 |
+ const uint8_t *ptr; |
|
128 |
+ |
|
129 |
+ hdr_size = AV_RB16(buf); |
|
130 |
+ if (hdr_size > data_size) { |
|
131 |
+ av_log(avctx, AV_LOG_ERROR, "frame data too short!\n"); |
|
132 |
+ return -1; |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ version = AV_RB16(buf + 2); |
|
136 |
+ if (version >= 2) { |
|
137 |
+ av_log(avctx, AV_LOG_ERROR, |
|
138 |
+ "unsupported header version: %d\n", version); |
|
139 |
+ return -1; |
|
140 |
+ } |
|
141 |
+ |
|
142 |
+ width = AV_RB16(buf + 8); |
|
143 |
+ height = AV_RB16(buf + 10); |
|
144 |
+ if (width != avctx->width || height != avctx->height) { |
|
145 |
+ av_log(avctx, AV_LOG_ERROR, |
|
146 |
+ "picture dimension changed! Old: %d x %d, new: %d x %d\n", |
|
147 |
+ avctx->width, avctx->height, width, height); |
|
148 |
+ return -1; |
|
149 |
+ } |
|
150 |
+ |
|
151 |
+ ctx->frame_type = (buf[12] >> 2) & 3; |
|
152 |
+ if (ctx->frame_type > 2) { |
|
153 |
+ av_log(avctx, AV_LOG_ERROR, |
|
154 |
+ "unsupported frame type: %d!\n", ctx->frame_type); |
|
155 |
+ return -1; |
|
156 |
+ } |
|
157 |
+ |
|
158 |
+ ctx->chroma_factor = (buf[12] >> 6) & 3; |
|
159 |
+ ctx->mb_chroma_factor = ctx->chroma_factor + 2; |
|
160 |
+ ctx->num_chroma_blocks = (1 << ctx->chroma_factor) >> 1; |
|
161 |
+ switch (ctx->chroma_factor) { |
|
162 |
+ case 2: |
|
163 |
+ avctx->pix_fmt = PIX_FMT_YUV422P10; |
|
164 |
+ break; |
|
165 |
+ case 3: |
|
166 |
+ avctx->pix_fmt = PIX_FMT_YUV444P10; |
|
167 |
+ break; |
|
168 |
+ default: |
|
169 |
+ av_log(avctx, AV_LOG_ERROR, |
|
170 |
+ "unsupported picture format: %d!\n", ctx->pic_format); |
|
171 |
+ return -1; |
|
172 |
+ } |
|
173 |
+ |
|
174 |
+ if (ctx->scantable_type != ctx->frame_type) { |
|
175 |
+ if (!ctx->frame_type) |
|
176 |
+ ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, |
|
177 |
+ progressive_scan); |
|
178 |
+ else |
|
179 |
+ ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, |
|
180 |
+ interlaced_scan); |
|
181 |
+ ctx->scantable_type = ctx->frame_type; |
|
182 |
+ } |
|
183 |
+ |
|
184 |
+ if (ctx->frame_type) { /* if interlaced */ |
|
185 |
+ ctx->picture.interlaced_frame = 1; |
|
186 |
+ ctx->picture.top_field_first = ctx->frame_type & 1; |
|
187 |
+ } |
|
188 |
+ |
|
189 |
+ ctx->qmat_changed = 0; |
|
190 |
+ ptr = buf + 20; |
|
191 |
+ flags = buf[19]; |
|
192 |
+ if (flags & 2) { |
|
193 |
+ if (ptr - buf > hdr_size - 64) { |
|
194 |
+ av_log(avctx, AV_LOG_ERROR, "Too short header data\n"); |
|
195 |
+ return -1; |
|
196 |
+ } |
|
197 |
+ if (memcmp(ctx->qmat_luma, ptr, 64)) { |
|
198 |
+ memcpy(ctx->qmat_luma, ptr, 64); |
|
199 |
+ ctx->qmat_changed = 1; |
|
200 |
+ } |
|
201 |
+ ptr += 64; |
|
202 |
+ } else { |
|
203 |
+ memset(ctx->qmat_luma, 4, 64); |
|
204 |
+ ctx->qmat_changed = 1; |
|
205 |
+ } |
|
206 |
+ |
|
207 |
+ if (flags & 1) { |
|
208 |
+ if (ptr - buf > hdr_size - 64) { |
|
209 |
+ av_log(avctx, AV_LOG_ERROR, "Too short header data\n"); |
|
210 |
+ return -1; |
|
211 |
+ } |
|
212 |
+ if (memcmp(ctx->qmat_chroma, ptr, 64)) { |
|
213 |
+ memcpy(ctx->qmat_chroma, ptr, 64); |
|
214 |
+ ctx->qmat_changed = 1; |
|
215 |
+ } |
|
216 |
+ } else { |
|
217 |
+ memset(ctx->qmat_chroma, 4, 64); |
|
218 |
+ ctx->qmat_changed = 1; |
|
219 |
+ } |
|
220 |
+ |
|
221 |
+ return hdr_size; |
|
222 |
+} |
|
223 |
+ |
|
224 |
+ |
|
225 |
+static int decode_picture_header(ProresContext *ctx, const uint8_t *buf, |
|
226 |
+ const int data_size, AVCodecContext *avctx) |
|
227 |
+{ |
|
228 |
+ int i, hdr_size, pic_data_size, num_slices; |
|
229 |
+ int slice_width_factor, slice_height_factor; |
|
230 |
+ int remainder, num_x_slices; |
|
231 |
+ const uint8_t *data_ptr, *index_ptr; |
|
232 |
+ |
|
233 |
+ hdr_size = data_size > 0 ? buf[0] >> 3 : 0; |
|
234 |
+ if (hdr_size < 8 || hdr_size > data_size) { |
|
235 |
+ av_log(avctx, AV_LOG_ERROR, "picture header too short!\n"); |
|
236 |
+ return -1; |
|
237 |
+ } |
|
238 |
+ |
|
239 |
+ pic_data_size = AV_RB32(buf + 1); |
|
240 |
+ if (pic_data_size > data_size) { |
|
241 |
+ av_log(avctx, AV_LOG_ERROR, "picture data too short!\n"); |
|
242 |
+ return -1; |
|
243 |
+ } |
|
244 |
+ |
|
245 |
+ slice_width_factor = buf[7] >> 4; |
|
246 |
+ slice_height_factor = buf[7] & 0xF; |
|
247 |
+ if (slice_width_factor > 3 || slice_height_factor) { |
|
248 |
+ av_log(avctx, AV_LOG_ERROR, |
|
249 |
+ "unsupported slice dimension: %d x %d!\n", |
|
250 |
+ 1 << slice_width_factor, 1 << slice_height_factor); |
|
251 |
+ return -1; |
|
252 |
+ } |
|
253 |
+ |
|
254 |
+ ctx->slice_width_factor = slice_width_factor; |
|
255 |
+ ctx->slice_height_factor = slice_height_factor; |
|
256 |
+ |
|
257 |
+ ctx->num_x_mbs = (avctx->width + 15) >> 4; |
|
258 |
+ ctx->num_y_mbs = |
|
259 |
+ (avctx->height + (1 << (4 + ctx->picture.interlaced_frame)) - 1) >> |
|
260 |
+ (4 + ctx->picture.interlaced_frame); |
|
261 |
+ |
|
262 |
+ remainder = ctx->num_x_mbs & ((1 << slice_width_factor) - 1); |
|
263 |
+ num_x_slices = (ctx->num_x_mbs >> slice_width_factor) + (remainder & 1) + |
|
264 |
+ ((remainder >> 1) & 1) + ((remainder >> 2) & 1); |
|
265 |
+ |
|
266 |
+ num_slices = num_x_slices * ctx->num_y_mbs; |
|
267 |
+ if (num_slices != AV_RB16(buf + 5)) { |
|
268 |
+ av_log(avctx, AV_LOG_ERROR, "invalid number of slices!\n"); |
|
269 |
+ return -1; |
|
270 |
+ } |
|
271 |
+ |
|
272 |
+ if (ctx->total_slices != num_slices) { |
|
273 |
+ av_freep(&ctx->slice_data_index); |
|
274 |
+ ctx->slice_data_index = |
|
275 |
+ av_malloc((num_slices + 1) * sizeof(uint8_t*)); |
|
276 |
+ if (!ctx->slice_data_index) |
|
277 |
+ return AVERROR(ENOMEM); |
|
278 |
+ ctx->total_slices = num_slices; |
|
279 |
+ } |
|
280 |
+ |
|
281 |
+ if (hdr_size + num_slices * 2 > data_size) { |
|
282 |
+ av_log(avctx, AV_LOG_ERROR, "slice table too short!\n"); |
|
283 |
+ return -1; |
|
284 |
+ } |
|
285 |
+ |
|
286 |
+ /* parse slice table allowing quick access to the slice data */ |
|
287 |
+ index_ptr = buf + hdr_size; |
|
288 |
+ data_ptr = index_ptr + num_slices * 2; |
|
289 |
+ |
|
290 |
+ for (i = 0; i < num_slices; i++) { |
|
291 |
+ ctx->slice_data_index[i] = data_ptr; |
|
292 |
+ data_ptr += AV_RB16(index_ptr + i * 2); |
|
293 |
+ } |
|
294 |
+ ctx->slice_data_index[i] = data_ptr; |
|
295 |
+ |
|
296 |
+ if (data_ptr > buf + data_size) { |
|
297 |
+ av_log(avctx, AV_LOG_ERROR, "out of slice data!\n"); |
|
298 |
+ return -1; |
|
299 |
+ } |
|
300 |
+ |
|
301 |
+ return pic_data_size; |
|
302 |
+} |
|
303 |
+ |
|
304 |
+ |
|
305 |
+/** |
|
306 |
+ * Read an unsigned rice/exp golomb codeword. |
|
307 |
+ */ |
|
308 |
+static inline int decode_vlc_codeword(GetBitContext *gb, uint8_t codebook) |
|
309 |
+{ |
|
310 |
+ unsigned int rice_order, exp_order, switch_bits; |
|
311 |
+ unsigned int buf, code; |
|
312 |
+ int log, prefix_len, len; |
|
313 |
+ |
|
314 |
+ OPEN_READER(re, gb); |
|
315 |
+ UPDATE_CACHE(re, gb); |
|
316 |
+ buf = GET_CACHE(re, gb); |
|
317 |
+ |
|
318 |
+ /* number of prefix bits to switch between Rice and expGolomb */ |
|
319 |
+ switch_bits = (codebook & 3) + 1; |
|
320 |
+ rice_order = codebook >> 5; /* rice code order */ |
|
321 |
+ exp_order = (codebook >> 2) & 7; /* exp golomb code order */ |
|
322 |
+ |
|
323 |
+ log = 31 - av_log2(buf); /* count prefix bits (zeroes) */ |
|
324 |
+ |
|
325 |
+ if (log < switch_bits) { /* ok, we got a rice code */ |
|
326 |
+ if (!rice_order) { |
|
327 |
+ /* shortcut for faster decoding of rice codes without remainder */ |
|
328 |
+ code = log; |
|
329 |
+ LAST_SKIP_BITS(re, gb, log + 1); |
|
330 |
+ } else { |
|
331 |
+ prefix_len = log + 1; |
|
332 |
+ code = (log << rice_order) + NEG_USR32((buf << prefix_len), rice_order); |
|
333 |
+ LAST_SKIP_BITS(re, gb, prefix_len + rice_order); |
|
334 |
+ } |
|
335 |
+ } else { /* otherwise we got a exp golomb code */ |
|
336 |
+ len = (log << 1) - switch_bits + exp_order + 1; |
|
337 |
+ code = NEG_USR32(buf, len) - (1 << exp_order) + (switch_bits << rice_order); |
|
338 |
+ LAST_SKIP_BITS(re, gb, len); |
|
339 |
+ } |
|
340 |
+ |
|
341 |
+ CLOSE_READER(re, gb); |
|
342 |
+ |
|
343 |
+ return code; |
|
344 |
+} |
|
345 |
+ |
|
346 |
+#define LSB2SIGN(x) (-((x) & 1)) |
|
347 |
+#define TOSIGNED(x) (((x) >> 1) ^ LSB2SIGN(x)) |
|
348 |
+ |
|
349 |
+#define FIRST_DC_CB 0xB8 // rice_order = 5, exp_golomb_order = 6, switch_bits = 0 |
|
350 |
+ |
|
351 |
+static uint8_t dc_codebook[4] = { |
|
352 |
+ 0x04, // rice_order = 0, exp_golomb_order = 1, switch_bits = 0 |
|
353 |
+ 0x28, // rice_order = 1, exp_golomb_order = 2, switch_bits = 0 |
|
354 |
+ 0x4D, // rice_order = 2, exp_golomb_order = 3, switch_bits = 1 |
|
355 |
+ 0x70 // rice_order = 3, exp_golomb_order = 4, switch_bits = 0 |
|
356 |
+}; |
|
357 |
+ |
|
358 |
+ |
|
359 |
+/** |
|
360 |
+ * Decode DC coefficients for all blocks in a slice. |
|
361 |
+ */ |
|
362 |
+static inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out, |
|
363 |
+ int nblocks) |
|
364 |
+{ |
|
365 |
+ DCTELEM prev_dc; |
|
366 |
+ int i, sign; |
|
367 |
+ int16_t delta; |
|
368 |
+ unsigned int code; |
|
369 |
+ |
|
370 |
+ code = decode_vlc_codeword(gb, FIRST_DC_CB); |
|
371 |
+ out[0] = prev_dc = TOSIGNED(code); |
|
372 |
+ |
|
373 |
+ out += 64; /* move to the DC coeff of the next block */ |
|
374 |
+ delta = 3; |
|
375 |
+ |
|
376 |
+ for (i = 1; i < nblocks; i++, out += 64) { |
|
377 |
+ code = decode_vlc_codeword(gb, dc_codebook[FFMIN(FFABS(delta), 3)]); |
|
378 |
+ |
|
379 |
+ sign = -(((delta >> 15) & 1) ^ (code & 1)); |
|
380 |
+ delta = (((code + 1) >> 1) ^ sign) - sign; |
|
381 |
+ prev_dc += delta; |
|
382 |
+ out[0] = prev_dc; |
|
383 |
+ } |
|
384 |
+} |
|
385 |
+ |
|
386 |
+ |
|
387 |
+static uint8_t ac_codebook[7] = { |
|
388 |
+ 0x04, // rice_order = 0, exp_golomb_order = 1, switch_bits = 0 |
|
389 |
+ 0x28, // rice_order = 1, exp_golomb_order = 2, switch_bits = 0 |
|
390 |
+ 0x4C, // rice_order = 2, exp_golomb_order = 3, switch_bits = 0 |
|
391 |
+ 0x05, // rice_order = 0, exp_golomb_order = 1, switch_bits = 1 |
|
392 |
+ 0x29, // rice_order = 1, exp_golomb_order = 2, switch_bits = 1 |
|
393 |
+ 0x06, // rice_order = 0, exp_golomb_order = 1, switch_bits = 2 |
|
394 |
+ 0x0A, // rice_order = 0, exp_golomb_order = 2, switch_bits = 2 |
|
395 |
+}; |
|
396 |
+ |
|
397 |
+/** |
|
398 |
+ * Lookup tables for adaptive switching between codebooks |
|
399 |
+ * according with previous run/level value. |
|
400 |
+ */ |
|
401 |
+static uint8_t run_to_cb_index[16] = |
|
402 |
+ { 5, 5, 3, 3, 0, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 2 }; |
|
403 |
+ |
|
404 |
+static uint8_t lev_to_cb_index[10] = { 0, 6, 3, 5, 0, 1, 1, 1, 1, 2 }; |
|
405 |
+ |
|
406 |
+ |
|
407 |
+/** |
|
408 |
+ * Decode AC coefficients for all blocks in a slice. |
|
409 |
+ */ |
|
410 |
+static inline void decode_ac_coeffs(GetBitContext *gb, DCTELEM *out, |
|
411 |
+ int blocks_per_slice, |
|
412 |
+ int plane_size_factor, |
|
413 |
+ const uint8_t *scan) |
|
414 |
+{ |
|
415 |
+ int pos, block_mask, run, level, sign, run_cb_index, lev_cb_index; |
|
416 |
+ int max_coeffs, bits_left; |
|
417 |
+ |
|
418 |
+ /* set initial prediction values */ |
|
419 |
+ run = 4; |
|
420 |
+ level = 2; |
|
421 |
+ |
|
422 |
+ max_coeffs = blocks_per_slice << 6; |
|
423 |
+ block_mask = blocks_per_slice - 1; |
|
424 |
+ |
|
425 |
+ for (pos = blocks_per_slice - 1; pos < max_coeffs;) { |
|
426 |
+ run_cb_index = run_to_cb_index[FFMIN(run, 15)]; |
|
427 |
+ lev_cb_index = lev_to_cb_index[FFMIN(level, 9)]; |
|
428 |
+ |
|
429 |
+ bits_left = get_bits_left(gb); |
|
430 |
+ if (bits_left <= 8 && !show_bits(gb, bits_left)) |
|
431 |
+ return; |
|
432 |
+ |
|
433 |
+ run = decode_vlc_codeword(gb, ac_codebook[run_cb_index]); |
|
434 |
+ |
|
435 |
+ bits_left = get_bits_left(gb); |
|
436 |
+ if (bits_left <= 8 && !show_bits(gb, bits_left)) |
|
437 |
+ return; |
|
438 |
+ |
|
439 |
+ level = decode_vlc_codeword(gb, ac_codebook[lev_cb_index]) + 1; |
|
440 |
+ |
|
441 |
+ pos += run + 1; |
|
442 |
+ if (pos >= max_coeffs) |
|
443 |
+ break; |
|
444 |
+ |
|
445 |
+ sign = get_sbits(gb, 1); |
|
446 |
+ out[((pos & block_mask) << 6) + scan[pos >> plane_size_factor]] = |
|
447 |
+ (level ^ sign) - sign; |
|
448 |
+ } |
|
449 |
+} |
|
450 |
+ |
|
451 |
+ |
|
452 |
+#define CLIP_AND_BIAS(x) (av_clip((x) + BIAS, CLIP_MIN, CLIP_MAX)) |
|
453 |
+ |
|
454 |
+/** |
|
455 |
+ * Add bias value, clamp and output pixels of a slice |
|
456 |
+ */ |
|
457 |
+static void put_pixels(const DCTELEM *in, uint16_t *out, int stride, |
|
458 |
+ int mbs_per_slice, int blocks_per_mb) |
|
459 |
+{ |
|
460 |
+ int mb, x, y, src_offset, dst_offset; |
|
461 |
+ const DCTELEM *src1, *src2; |
|
462 |
+ uint16_t *dst1, *dst2; |
|
463 |
+ |
|
464 |
+ src1 = in; |
|
465 |
+ src2 = in + (blocks_per_mb << 5); |
|
466 |
+ dst1 = out; |
|
467 |
+ dst2 = out + (stride << 3); |
|
468 |
+ |
|
469 |
+ for (mb = 0; mb < mbs_per_slice; mb++) { |
|
470 |
+ for (y = 0, dst_offset = 0; y < 8; y++, dst_offset += stride) { |
|
471 |
+ for (x = 0; x < 8; x++) { |
|
472 |
+ src_offset = (y << 3) + x; |
|
473 |
+ |
|
474 |
+ dst1[dst_offset + x] = CLIP_AND_BIAS(src1[src_offset]); |
|
475 |
+ dst2[dst_offset + x] = CLIP_AND_BIAS(src2[src_offset]); |
|
476 |
+ |
|
477 |
+ if (blocks_per_mb > 2) { |
|
478 |
+ dst1[dst_offset + x + 8] = |
|
479 |
+ CLIP_AND_BIAS(src1[src_offset + 64]); |
|
480 |
+ dst2[dst_offset + x + 8] = |
|
481 |
+ CLIP_AND_BIAS(src2[src_offset + 64]); |
|
482 |
+ } |
|
483 |
+ } |
|
484 |
+ } |
|
485 |
+ |
|
486 |
+ src1 += blocks_per_mb << 6; |
|
487 |
+ src2 += blocks_per_mb << 6; |
|
488 |
+ dst1 += blocks_per_mb << 2; |
|
489 |
+ dst2 += blocks_per_mb << 2; |
|
490 |
+ } |
|
491 |
+} |
|
492 |
+ |
|
493 |
+ |
|
494 |
+/** |
|
495 |
+ * Decode a slice plane (luma or chroma). |
|
496 |
+ */ |
|
497 |
+static void decode_slice_plane(ProresContext *ctx, const uint8_t *buf, |
|
498 |
+ int data_size, uint16_t *out_ptr, |
|
499 |
+ int linesize, int mbs_per_slice, |
|
500 |
+ int blocks_per_mb, int plane_size_factor, |
|
501 |
+ const int16_t *qmat) |
|
502 |
+{ |
|
503 |
+ GetBitContext gb; |
|
504 |
+ DCTELEM *block_ptr; |
|
505 |
+ int i, blk_num, blocks_per_slice; |
|
506 |
+ |
|
507 |
+ blocks_per_slice = mbs_per_slice * blocks_per_mb; |
|
508 |
+ |
|
509 |
+ memset(ctx->blocks, 0, 8 * 4 * 64 * sizeof(*ctx->blocks)); |
|
510 |
+ |
|
511 |
+ init_get_bits(&gb, buf, data_size << 3); |
|
512 |
+ |
|
513 |
+ decode_dc_coeffs(&gb, ctx->blocks, blocks_per_slice); |
|
514 |
+ |
|
515 |
+ decode_ac_coeffs(&gb, ctx->blocks, blocks_per_slice, |
|
516 |
+ plane_size_factor, ctx->scantable.permutated); |
|
517 |
+ |
|
518 |
+ /* inverse quantization, inverse transform and output */ |
|
519 |
+ block_ptr = ctx->blocks; |
|
520 |
+ |
|
521 |
+ for (blk_num = 0; blk_num < blocks_per_slice; |
|
522 |
+ blk_num++, block_ptr += 64) { |
|
523 |
+ /* TODO: the correct solution shoud be (block_ptr[i] * qmat[i]) >> 1 |
|
524 |
+ * and the input of the inverse transform should be scaled by 2 |
|
525 |
+ * in order to avoid rounding errors. |
|
526 |
+ * Due to the fact the existing Libav transforms are incompatible with |
|
527 |
+ * that input I temporally introduced the coarse solution below... */ |
|
528 |
+ for (i = 0; i < 64; i++) |
|
529 |
+ block_ptr[i] = (block_ptr[i] * qmat[i]) >> 2; |
|
530 |
+ |
|
531 |
+ ctx->dsp.idct(block_ptr); |
|
532 |
+ } |
|
533 |
+ |
|
534 |
+ put_pixels(ctx->blocks, out_ptr, linesize >> 1, mbs_per_slice, |
|
535 |
+ blocks_per_mb); |
|
536 |
+} |
|
537 |
+ |
|
538 |
+ |
|
539 |
+static int decode_slice(ProresContext *ctx, int pic_num, int slice_num, |
|
540 |
+ int mb_x_pos, int mb_y_pos, int mbs_per_slice, |
|
541 |
+ AVCodecContext *avctx) |
|
542 |
+{ |
|
543 |
+ const uint8_t *buf; |
|
544 |
+ uint8_t *y_data, *u_data, *v_data; |
|
545 |
+ AVFrame *pic = avctx->coded_frame; |
|
546 |
+ int i, sf, slice_width_factor; |
|
547 |
+ int slice_data_size, hdr_size, y_data_size, u_data_size, v_data_size; |
|
548 |
+ int y_linesize, u_linesize, v_linesize; |
|
549 |
+ |
|
550 |
+ buf = ctx->slice_data_index[slice_num]; |
|
551 |
+ slice_data_size = ctx->slice_data_index[slice_num + 1] - buf; |
|
552 |
+ |
|
553 |
+ slice_width_factor = av_log2(mbs_per_slice); |
|
554 |
+ |
|
555 |
+ y_data = pic->data[0]; |
|
556 |
+ u_data = pic->data[1]; |
|
557 |
+ v_data = pic->data[2]; |
|
558 |
+ y_linesize = pic->linesize[0]; |
|
559 |
+ u_linesize = pic->linesize[1]; |
|
560 |
+ v_linesize = pic->linesize[2]; |
|
561 |
+ |
|
562 |
+ if (pic->interlaced_frame) { |
|
563 |
+ if (!(pic_num ^ pic->top_field_first)) { |
|
564 |
+ y_data += y_linesize; |
|
565 |
+ u_data += u_linesize; |
|
566 |
+ v_data += v_linesize; |
|
567 |
+ } |
|
568 |
+ y_linesize <<= 1; |
|
569 |
+ u_linesize <<= 1; |
|
570 |
+ v_linesize <<= 1; |
|
571 |
+ } |
|
572 |
+ |
|
573 |
+ if (slice_data_size < 6) { |
|
574 |
+ av_log(avctx, AV_LOG_ERROR, "slice data too short!\n"); |
|
575 |
+ return -1; |
|
576 |
+ } |
|
577 |
+ |
|
578 |
+ /* parse slice header */ |
|
579 |
+ hdr_size = buf[0] >> 3; |
|
580 |
+ y_data_size = AV_RB16(buf + 2); |
|
581 |
+ u_data_size = AV_RB16(buf + 4); |
|
582 |
+ v_data_size = slice_data_size - y_data_size - u_data_size - hdr_size; |
|
583 |
+ |
|
584 |
+ if (v_data_size < 0 || hdr_size < 6) { |
|
585 |
+ av_log(avctx, AV_LOG_ERROR, "invalid data sizes!\n"); |
|
586 |
+ return -1; |
|
587 |
+ } |
|
588 |
+ |
|
589 |
+ sf = av_clip(buf[1], 1, 224); |
|
590 |
+ sf = sf > 128 ? (sf - 96) << 2 : sf; |
|
591 |
+ |
|
592 |
+ /* scale quantization matrixes according with slice's scale factor */ |
|
593 |
+ /* TODO: this can be SIMD-optimized alot */ |
|
594 |
+ if (ctx->qmat_changed || sf != ctx->prev_slice_sf) { |
|
595 |
+ ctx->prev_slice_sf = sf; |
|
596 |
+ for (i = 0; i < 64; i++) { |
|
597 |
+ ctx->qmat_luma_scaled[i] = ctx->qmat_luma[i] * sf; |
|
598 |
+ ctx->qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * sf; |
|
599 |
+ } |
|
600 |
+ } |
|
601 |
+ |
|
602 |
+ /* decode luma plane */ |
|
603 |
+ decode_slice_plane(ctx, buf + hdr_size, y_data_size, |
|
604 |
+ (uint16_t*) (y_data + (mb_y_pos << 4) * y_linesize + |
|
605 |
+ (mb_x_pos << 5)), y_linesize, |
|
606 |
+ mbs_per_slice, 4, slice_width_factor + 2, |
|
607 |
+ ctx->qmat_luma_scaled); |
|
608 |
+ |
|
609 |
+ /* decode U chroma plane */ |
|
610 |
+ decode_slice_plane(ctx, buf + hdr_size + y_data_size, u_data_size, |
|
611 |
+ (uint16_t*) (u_data + (mb_y_pos << 4) * u_linesize + |
|
612 |
+ (mb_x_pos << ctx->mb_chroma_factor)), |
|
613 |
+ u_linesize, mbs_per_slice, ctx->num_chroma_blocks, |
|
614 |
+ slice_width_factor + ctx->chroma_factor - 1, |
|
615 |
+ ctx->qmat_chroma_scaled); |
|
616 |
+ |
|
617 |
+ /* decode V chroma plane */ |
|
618 |
+ decode_slice_plane(ctx, buf + hdr_size + y_data_size + u_data_size, |
|
619 |
+ v_data_size, |
|
620 |
+ (uint16_t*) (v_data + (mb_y_pos << 4) * v_linesize + |
|
621 |
+ (mb_x_pos << ctx->mb_chroma_factor)), |
|
622 |
+ v_linesize, mbs_per_slice, ctx->num_chroma_blocks, |
|
623 |
+ slice_width_factor + ctx->chroma_factor - 1, |
|
624 |
+ ctx->qmat_chroma_scaled); |
|
625 |
+ |
|
626 |
+ return 0; |
|
627 |
+} |
|
628 |
+ |
|
629 |
+ |
|
630 |
+static int decode_picture(ProresContext *ctx, int pic_num, |
|
631 |
+ AVCodecContext *avctx) |
|
632 |
+{ |
|
633 |
+ int slice_num, slice_width, x_pos, y_pos; |
|
634 |
+ |
|
635 |
+ slice_num = 0; |
|
636 |
+ |
|
637 |
+ for (y_pos = 0; y_pos < ctx->num_y_mbs; y_pos++) { |
|
638 |
+ slice_width = 1 << ctx->slice_width_factor; |
|
639 |
+ |
|
640 |
+ for (x_pos = 0; x_pos < ctx->num_x_mbs && slice_width; |
|
641 |
+ x_pos += slice_width) { |
|
642 |
+ while (ctx->num_x_mbs - x_pos < slice_width) |
|
643 |
+ slice_width >>= 1; |
|
644 |
+ |
|
645 |
+ if (decode_slice(ctx, pic_num, slice_num, x_pos, y_pos, |
|
646 |
+ slice_width, avctx) < 0) |
|
647 |
+ return -1; |
|
648 |
+ |
|
649 |
+ slice_num++; |
|
650 |
+ } |
|
651 |
+ } |
|
652 |
+ |
|
653 |
+ return 0; |
|
654 |
+} |
|
655 |
+ |
|
656 |
+ |
|
657 |
+#define FRAME_ID MKBETAG('i', 'c', 'p', 'f') |
|
658 |
+#define MOVE_DATA_PTR(nbytes) buf += (nbytes); buf_size -= (nbytes) |
|
659 |
+ |
|
660 |
+static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, |
|
661 |
+ AVPacket *avpkt) |
|
662 |
+{ |
|
663 |
+ ProresContext *ctx = avctx->priv_data; |
|
664 |
+ AVFrame *picture = avctx->coded_frame; |
|
665 |
+ const uint8_t *buf = avpkt->data; |
|
666 |
+ int buf_size = avpkt->size; |
|
667 |
+ int frame_hdr_size, pic_num, pic_data_size; |
|
668 |
+ |
|
669 |
+ /* check frame atom container */ |
|
670 |
+ if (buf_size < 28 || buf_size < AV_RB32(buf) || |
|
671 |
+ AV_RB32(buf + 4) != FRAME_ID) { |
|
672 |
+ av_log(avctx, AV_LOG_ERROR, "invalid frame\n"); |
|
673 |
+ return -1; |
|
674 |
+ } |
|
675 |
+ |
|
676 |
+ MOVE_DATA_PTR(8); |
|
677 |
+ |
|
678 |
+ frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx); |
|
679 |
+ if (frame_hdr_size < 0) |
|
680 |
+ return -1; |
|
681 |
+ |
|
682 |
+ MOVE_DATA_PTR(frame_hdr_size); |
|
683 |
+ |
|
684 |
+ if (picture->data[0]) |
|
685 |
+ avctx->release_buffer(avctx, picture); |
|
686 |
+ |
|
687 |
+ picture->reference = 0; |
|
688 |
+ if (avctx->get_buffer(avctx, picture) < 0) |
|
689 |
+ return -1; |
|
690 |
+ |
|
691 |
+ for (pic_num = 0; ctx->picture.interlaced_frame - pic_num + 1; pic_num++) { |
|
692 |
+ pic_data_size = decode_picture_header(ctx, buf, buf_size, avctx); |
|
693 |
+ if (pic_data_size < 0) |
|
694 |
+ return -1; |
|
695 |
+ |
|
696 |
+ if (decode_picture(ctx, pic_num, avctx)) |
|
697 |
+ return -1; |
|
698 |
+ |
|
699 |
+ MOVE_DATA_PTR(pic_data_size); |
|
700 |
+ } |
|
701 |
+ |
|
702 |
+ *data_size = sizeof(AVPicture); |
|
703 |
+ *(AVFrame*) data = *avctx->coded_frame; |
|
704 |
+ |
|
705 |
+ return avpkt->size; |
|
706 |
+} |
|
707 |
+ |
|
708 |
+ |
|
709 |
+static av_cold int decode_close(AVCodecContext *avctx) |
|
710 |
+{ |
|
711 |
+ ProresContext *ctx = avctx->priv_data; |
|
712 |
+ |
|
713 |
+ if (ctx->picture.data[0]) |
|
714 |
+ avctx->release_buffer(avctx, &ctx->picture); |
|
715 |
+ |
|
716 |
+ av_freep(&ctx->slice_data_index); |
|
717 |
+ |
|
718 |
+ return 0; |
|
719 |
+} |
|
720 |
+ |
|
721 |
+ |
|
722 |
+AVCodec ff_prores_decoder = { |
|
723 |
+ .name = "ProRes", |
|
724 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
725 |
+ .id = CODEC_ID_PRORES, |
|
726 |
+ .priv_data_size = sizeof(ProresContext), |
|
727 |
+ .init = decode_init, |
|
728 |
+ .close = decode_close, |
|
729 |
+ .decode = decode_frame, |
|
730 |
+ .capabilities = CODEC_CAP_DR1, |
|
731 |
+ .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)") |
|
732 |
+}; |
... | ... |
@@ -21,7 +21,7 @@ |
21 | 21 |
#define AVCODEC_VERSION_H |
22 | 22 |
|
23 | 23 |
#define LIBAVCODEC_VERSION_MAJOR 53 |
24 |
-#define LIBAVCODEC_VERSION_MINOR 10 |
|
24 |
+#define LIBAVCODEC_VERSION_MINOR 11 |
|
25 | 25 |
#define LIBAVCODEC_VERSION_MICRO 0 |
26 | 26 |
|
27 | 27 |
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ |