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