| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2010-2011 Maxim Poliakovski | ||
| 3 | * Copyright (c) 2010-2011 Elvis Presley | ||
| 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 | * @file | ||
| 24 | * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'apco' (Proxy), 'ap4h' (4444), 'ap4x' (4444 XQ) | ||
| 25 | */ | ||
| 26 | |||
| 27 | //#define DEBUG | ||
| 28 | |||
| 29 | #include "config_components.h" | ||
| 30 | |||
| 31 | #include "libavutil/internal.h" | ||
| 32 | #include "libavutil/mem.h" | ||
| 33 | #include "libavutil/mem_internal.h" | ||
| 34 | |||
| 35 | #include "avcodec.h" | ||
| 36 | #include "codec_internal.h" | ||
| 37 | #include "decode.h" | ||
| 38 | #include "get_bits.h" | ||
| 39 | #include "hwaccel_internal.h" | ||
| 40 | #include "hwconfig.h" | ||
| 41 | #include "idctdsp.h" | ||
| 42 | #include "profiles.h" | ||
| 43 | #include "proresdec.h" | ||
| 44 | #include "proresdata.h" | ||
| 45 | #include "thread.h" | ||
| 46 | |||
| 47 | #define ALPHA_SHIFT_16_TO_10(alpha_val) (alpha_val >> 6) | ||
| 48 | #define ALPHA_SHIFT_8_TO_10(alpha_val) ((alpha_val << 2) | (alpha_val >> 6)) | ||
| 49 | #define ALPHA_SHIFT_16_TO_12(alpha_val) (alpha_val >> 4) | ||
| 50 | #define ALPHA_SHIFT_8_TO_12(alpha_val) ((alpha_val << 4) | (alpha_val >> 4)) | ||
| 51 | |||
| 52 | 3060 | static void inline unpack_alpha(GetBitContext *gb, uint16_t *dst, int num_coeffs, | |
| 53 | const int num_bits, const int decode_precision) { | ||
| 54 | 3060 | const int mask = (1 << num_bits) - 1; | |
| 55 | int i, idx, val, alpha_val; | ||
| 56 | |||
| 57 | 3060 | idx = 0; | |
| 58 | 3060 | alpha_val = mask; | |
| 59 | do { | ||
| 60 | do { | ||
| 61 |
2/2✓ Branch 1 taken 22212 times.
✓ Branch 2 taken 8804 times.
|
31016 | if (get_bits1(gb)) { |
| 62 | 22212 | val = get_bits(gb, num_bits); | |
| 63 | } else { | ||
| 64 | int sign; | ||
| 65 |
1/2✓ Branch 0 taken 8804 times.
✗ Branch 1 not taken.
|
8804 | val = get_bits(gb, num_bits == 16 ? 7 : 4); |
| 66 | 8804 | sign = val & 1; | |
| 67 | 8804 | val = (val + 2) >> 1; | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8804 times.
|
8804 | if (sign) |
| 69 | ✗ | val = -val; | |
| 70 | } | ||
| 71 | 31016 | alpha_val = (alpha_val + val) & mask; | |
| 72 |
1/2✓ Branch 0 taken 31016 times.
✗ Branch 1 not taken.
|
31016 | if (num_bits == 16) { |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31016 times.
|
31016 | if (decode_precision == 10) { |
| 74 | ✗ | dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val); | |
| 75 | } else { /* 12b */ | ||
| 76 | 31016 | dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val); | |
| 77 | } | ||
| 78 | } else { | ||
| 79 | ✗ | if (decode_precision == 10) { | |
| 80 | ✗ | dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val); | |
| 81 | } else { /* 12b */ | ||
| 82 | ✗ | dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val); | |
| 83 | } | ||
| 84 | } | ||
| 85 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 30997 times.
|
31016 | if (idx >= num_coeffs) |
| 86 | 19 | break; | |
| 87 |
4/4✓ Branch 1 taken 23158 times.
✓ Branch 2 taken 7839 times.
✓ Branch 4 taken 10424 times.
✓ Branch 5 taken 12734 times.
|
30997 | } while (get_bits_left(gb)>0 && get_bits1(gb)); |
| 88 | 20592 | val = get_bits(gb, 4); | |
| 89 |
2/2✓ Branch 0 taken 19691 times.
✓ Branch 1 taken 901 times.
|
20592 | if (!val) |
| 90 | 19691 | val = get_bits(gb, 11); | |
| 91 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 20508 times.
|
20592 | if (idx + val > num_coeffs) |
| 92 | 84 | val = num_coeffs - idx; | |
| 93 |
1/2✓ Branch 0 taken 20592 times.
✗ Branch 1 not taken.
|
20592 | if (num_bits == 16) { |
| 94 |
2/2✓ Branch 0 taken 6235864 times.
✓ Branch 1 taken 20592 times.
|
6256456 | for (i = 0; i < val; i++) { |
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6235864 times.
|
6235864 | if (decode_precision == 10) { |
| 96 | ✗ | dst[idx++] = ALPHA_SHIFT_16_TO_10(alpha_val); | |
| 97 | } else { /* 12b */ | ||
| 98 | 6235864 | dst[idx++] = ALPHA_SHIFT_16_TO_12(alpha_val); | |
| 99 | } | ||
| 100 | } | ||
| 101 | } else { | ||
| 102 | ✗ | for (i = 0; i < val; i++) { | |
| 103 | ✗ | if (decode_precision == 10) { | |
| 104 | ✗ | dst[idx++] = ALPHA_SHIFT_8_TO_10(alpha_val); | |
| 105 | } else { /* 12b */ | ||
| 106 | ✗ | dst[idx++] = ALPHA_SHIFT_8_TO_12(alpha_val); | |
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 |
2/2✓ Branch 0 taken 17532 times.
✓ Branch 1 taken 3060 times.
|
20592 | } while (idx < num_coeffs); |
| 111 | 3060 | } | |
| 112 | |||
| 113 | ✗ | static void unpack_alpha_10(GetBitContext *gb, uint16_t *dst, int num_coeffs, | |
| 114 | const int num_bits) | ||
| 115 | { | ||
| 116 | ✗ | if (num_bits == 16) { | |
| 117 | ✗ | unpack_alpha(gb, dst, num_coeffs, 16, 10); | |
| 118 | } else { /* 8 bits alpha */ | ||
| 119 | ✗ | unpack_alpha(gb, dst, num_coeffs, 8, 10); | |
| 120 | } | ||
| 121 | ✗ | } | |
| 122 | |||
| 123 | 3060 | static void unpack_alpha_12(GetBitContext *gb, uint16_t *dst, int num_coeffs, | |
| 124 | const int num_bits) | ||
| 125 | { | ||
| 126 |
1/2✓ Branch 0 taken 3060 times.
✗ Branch 1 not taken.
|
3060 | if (num_bits == 16) { |
| 127 | 3060 | unpack_alpha(gb, dst, num_coeffs, 16, 12); | |
| 128 | } else { /* 8 bits alpha */ | ||
| 129 | ✗ | unpack_alpha(gb, dst, num_coeffs, 8, 12); | |
| 130 | } | ||
| 131 | 3060 | } | |
| 132 | |||
| 133 | 76 | static av_cold int decode_init(AVCodecContext *avctx) | |
| 134 | { | ||
| 135 | 76 | ProresContext *ctx = avctx->priv_data; | |
| 136 | |||
| 137 | 76 | avctx->bits_per_raw_sample = 10; | |
| 138 | |||
| 139 |
5/7✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
76 | switch (avctx->codec_tag) { |
| 140 | 14 | case MKTAG('a','p','c','o'): | |
| 141 | 14 | avctx->profile = AV_PROFILE_PRORES_PROXY; | |
| 142 | 14 | break; | |
| 143 | 2 | case MKTAG('a','p','c','s'): | |
| 144 | 2 | avctx->profile = AV_PROFILE_PRORES_LT; | |
| 145 | 2 | break; | |
| 146 | 21 | case MKTAG('a','p','c','n'): | |
| 147 | 21 | avctx->profile = AV_PROFILE_PRORES_STANDARD; | |
| 148 | 21 | break; | |
| 149 | 15 | case MKTAG('a','p','c','h'): | |
| 150 | 15 | avctx->profile = AV_PROFILE_PRORES_HQ; | |
| 151 | 15 | break; | |
| 152 | 24 | case MKTAG('a','p','4','h'): | |
| 153 | 24 | avctx->profile = AV_PROFILE_PRORES_4444; | |
| 154 | 24 | avctx->bits_per_raw_sample = 12; | |
| 155 | 24 | break; | |
| 156 | ✗ | case MKTAG('a','p','4','x'): | |
| 157 | ✗ | avctx->profile = AV_PROFILE_PRORES_XQ; | |
| 158 | ✗ | avctx->bits_per_raw_sample = 12; | |
| 159 | ✗ | break; | |
| 160 | ✗ | default: | |
| 161 | ✗ | avctx->profile = AV_PROFILE_UNKNOWN; | |
| 162 | ✗ | av_log(avctx, AV_LOG_WARNING, "Unknown prores profile %d\n", avctx->codec_tag); | |
| 163 | } | ||
| 164 | |||
| 165 | 152 | ctx->unpack_alpha = avctx->bits_per_raw_sample == 10 ? | |
| 166 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 24 times.
|
76 | unpack_alpha_10 : unpack_alpha_12; |
| 167 | |||
| 168 | 76 | av_log(avctx, AV_LOG_DEBUG, | |
| 169 | "Auto bitdepth precision. Use %db decoding based on codec tag.\n", | ||
| 170 | avctx->bits_per_raw_sample); | ||
| 171 | |||
| 172 | 76 | ff_blockdsp_init(&ctx->bdsp); | |
| 173 | 76 | ff_proresdsp_init(&ctx->prodsp, avctx->bits_per_raw_sample); | |
| 174 | |||
| 175 | 76 | ff_permute_scantable(ctx->progressive_scan, ff_prores_progressive_scan, | |
| 176 | 76 | ctx->prodsp.idct_permutation); | |
| 177 | 76 | ff_permute_scantable(ctx->interlaced_scan, ff_prores_interlaced_scan, | |
| 178 | 76 | ctx->prodsp.idct_permutation); | |
| 179 | |||
| 180 | 76 | ctx->pix_fmt = AV_PIX_FMT_NONE; | |
| 181 | |||
| 182 | 76 | return 0; | |
| 183 | } | ||
| 184 | |||
| 185 | 1065 | static int decode_frame_header(ProresContext *ctx, const uint8_t *buf, | |
| 186 | const int data_size, AVCodecContext *avctx) | ||
| 187 | { | ||
| 188 | 1065 | int hdr_size, width, height, flags, dimensions_changed = 0; | |
| 189 | int version; | ||
| 190 | const uint8_t *ptr; | ||
| 191 | enum AVPixelFormat pix_fmt; | ||
| 192 | 1065 | int old_frame_type = ctx->frame_type; | |
| 193 | |||
| 194 | 1065 | hdr_size = AV_RB16(buf); | |
| 195 | ff_dlog(avctx, "header size %d\n", hdr_size); | ||
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
|
1065 | if (hdr_size > data_size) { |
| 197 | ✗ | av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n"); | |
| 198 | ✗ | return AVERROR_INVALIDDATA; | |
| 199 | } | ||
| 200 | |||
| 201 | 1065 | version = AV_RB16(buf + 2); | |
| 202 | ff_dlog(avctx, "%.4s version %d\n", buf+4, version); | ||
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
|
1065 | if (version > 1) { |
| 204 | ✗ | av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version); | |
| 205 | ✗ | return AVERROR_PATCHWELCOME; | |
| 206 | } | ||
| 207 | |||
| 208 | 1065 | width = AV_RB16(buf + 8); | |
| 209 | 1065 | height = AV_RB16(buf + 10); | |
| 210 | |||
| 211 |
2/4✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1065 times.
|
1065 | if (width != avctx->width || height != avctx->height) { |
| 212 | int ret; | ||
| 213 | |||
| 214 | ✗ | av_log(avctx, AV_LOG_WARNING, "picture resolution change: %dx%d -> %dx%d\n", | |
| 215 | avctx->width, avctx->height, width, height); | ||
| 216 | ✗ | if ((ret = ff_set_dimensions(avctx, width, height)) < 0) | |
| 217 | ✗ | return ret; | |
| 218 | ✗ | dimensions_changed = 1; | |
| 219 | } | ||
| 220 | |||
| 221 | 1065 | ctx->frame_type = (buf[12] >> 2) & 3; | |
| 222 | 1065 | ctx->alpha_info = buf[17] & 0xf; | |
| 223 | |||
| 224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
|
1065 | if (ctx->alpha_info > 2) { |
| 225 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info); | |
| 226 | ✗ | return AVERROR_INVALIDDATA; | |
| 227 | } | ||
| 228 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1060 times.
|
1065 | if (avctx->skip_alpha) ctx->alpha_info = 0; |
| 229 | |||
| 230 | ff_dlog(avctx, "frame type %d\n", ctx->frame_type); | ||
| 231 | |||
| 232 |
2/2✓ Branch 0 taken 633 times.
✓ Branch 1 taken 432 times.
|
1065 | if (ctx->frame_type == 0) { |
| 233 | 633 | ctx->scan = ctx->progressive_scan; // permuted | |
| 234 | } else { | ||
| 235 | 432 | ctx->scan = ctx->interlaced_scan; // permuted | |
| 236 | 432 | ctx->frame->flags |= AV_FRAME_FLAG_INTERLACED; | |
| 237 |
2/2✓ Branch 0 taken 408 times.
✓ Branch 1 taken 24 times.
|
432 | if (ctx->frame_type == 1) |
| 238 | 408 | ctx->frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
| 239 | } | ||
| 240 | |||
| 241 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1060 times.
|
1065 | if (ctx->alpha_info) { |
| 242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (avctx->bits_per_raw_sample == 10) { |
| 243 | ✗ | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10; | |
| 244 | } else { /* 12b */ | ||
| 245 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P12 : AV_PIX_FMT_YUVA422P12; |
| 246 | } | ||
| 247 | } else { | ||
| 248 |
2/2✓ Branch 0 taken 647 times.
✓ Branch 1 taken 413 times.
|
1060 | if (avctx->bits_per_raw_sample == 10) { |
| 249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 647 times.
|
647 | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10; |
| 250 | } else { /* 12b */ | ||
| 251 |
1/2✓ Branch 0 taken 413 times.
✗ Branch 1 not taken.
|
413 | pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P12 : AV_PIX_FMT_YUV422P12; |
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 |
3/4✓ Branch 0 taken 992 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 992 times.
✗ Branch 3 not taken.
|
1065 | if (pix_fmt != ctx->pix_fmt || dimensions_changed || |
| 256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 992 times.
|
992 | ctx->frame_type != old_frame_type) { |
| 257 | #define HWACCEL_MAX (CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL + CONFIG_PRORES_VULKAN_HWACCEL) | ||
| 258 | #if HWACCEL_MAX | ||
| 259 | enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts; | ||
| 260 | int ret; | ||
| 261 | |||
| 262 | ctx->pix_fmt = pix_fmt; | ||
| 263 | |||
| 264 | #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL | ||
| 265 | *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX; | ||
| 266 | #endif | ||
| 267 | #if CONFIG_PRORES_VULKAN_HWACCEL | ||
| 268 | *fmtp++ = AV_PIX_FMT_VULKAN; | ||
| 269 | #endif | ||
| 270 | *fmtp++ = ctx->pix_fmt; | ||
| 271 | *fmtp = AV_PIX_FMT_NONE; | ||
| 272 | |||
| 273 | if ((ret = ff_get_format(avctx, pix_fmts)) < 0) | ||
| 274 | return ret; | ||
| 275 | |||
| 276 | avctx->pix_fmt = ret; | ||
| 277 | #else | ||
| 278 | 73 | avctx->pix_fmt = ctx->pix_fmt = pix_fmt; | |
| 279 | #endif | ||
| 280 | } | ||
| 281 | |||
| 282 | 1065 | avctx->color_primaries = buf[14]; | |
| 283 | 1065 | avctx->color_trc = buf[15]; | |
| 284 | 1065 | avctx->colorspace = buf[16]; | |
| 285 | 1065 | avctx->color_range = AVCOL_RANGE_MPEG; | |
| 286 | |||
| 287 | 1065 | ptr = buf + 20; | |
| 288 | 1065 | flags = buf[19]; | |
| 289 | ff_dlog(avctx, "flags %x\n", flags); | ||
| 290 | |||
| 291 |
1/2✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
|
1065 | if (flags & 2) { |
| 292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
|
1065 | if(buf + data_size - ptr < 64) { |
| 293 | ✗ | av_log(avctx, AV_LOG_ERROR, "Header truncated\n"); | |
| 294 | ✗ | return AVERROR_INVALIDDATA; | |
| 295 | } | ||
| 296 | 1065 | ff_permute_scantable(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr); | |
| 297 | 1065 | ptr += 64; | |
| 298 | } else { | ||
| 299 | ✗ | memset(ctx->qmat_luma, 4, 64); | |
| 300 | } | ||
| 301 | |||
| 302 |
1/2✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
|
1065 | if (flags & 1) { |
| 303 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
|
1065 | if(buf + data_size - ptr < 64) { |
| 304 | ✗ | av_log(avctx, AV_LOG_ERROR, "Header truncated\n"); | |
| 305 | ✗ | return AVERROR_INVALIDDATA; | |
| 306 | } | ||
| 307 | 1065 | ff_permute_scantable(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr); | |
| 308 | } else { | ||
| 309 | ✗ | memcpy(ctx->qmat_chroma, ctx->qmat_luma, 64); | |
| 310 | } | ||
| 311 | |||
| 312 | 1065 | return hdr_size; | |
| 313 | } | ||
| 314 | |||
| 315 | 1437 | static int decode_picture_header(AVCodecContext *avctx, const uint8_t *buf, const int buf_size) | |
| 316 | { | ||
| 317 | 1437 | ProresContext *ctx = avctx->priv_data; | |
| 318 | int i, hdr_size, slice_count; | ||
| 319 | unsigned pic_data_size; | ||
| 320 | int log2_slice_mb_width, log2_slice_mb_height; | ||
| 321 | int slice_mb_count, mb_x, mb_y; | ||
| 322 | const uint8_t *data_ptr, *index_ptr; | ||
| 323 | |||
| 324 | 1437 | hdr_size = buf[0] >> 3; | |
| 325 |
2/4✓ Branch 0 taken 1437 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1437 times.
|
1437 | if (hdr_size < 8 || hdr_size > buf_size) { |
| 326 | ✗ | av_log(avctx, AV_LOG_ERROR, "error, wrong picture header size\n"); | |
| 327 | ✗ | return AVERROR_INVALIDDATA; | |
| 328 | } | ||
| 329 | |||
| 330 | 1437 | pic_data_size = AV_RB32(buf + 1); | |
| 331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
|
1437 | if (pic_data_size > buf_size) { |
| 332 | ✗ | av_log(avctx, AV_LOG_ERROR, "error, wrong picture data size\n"); | |
| 333 | ✗ | return AVERROR_INVALIDDATA; | |
| 334 | } | ||
| 335 | |||
| 336 | 1437 | log2_slice_mb_width = buf[7] >> 4; | |
| 337 | 1437 | log2_slice_mb_height = buf[7] & 0xF; | |
| 338 |
2/4✓ Branch 0 taken 1437 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1437 times.
|
1437 | if (log2_slice_mb_width > 3 || log2_slice_mb_height) { |
| 339 | ✗ | av_log(avctx, AV_LOG_ERROR, "unsupported slice resolution: %dx%d\n", | |
| 340 | 1 << log2_slice_mb_width, 1 << log2_slice_mb_height); | ||
| 341 | ✗ | return AVERROR_INVALIDDATA; | |
| 342 | } | ||
| 343 | |||
| 344 | 1437 | ctx->slice_mb_width = 1 << log2_slice_mb_width; | |
| 345 | 1437 | ctx->slice_mb_height = 1 << log2_slice_mb_height; | |
| 346 | |||
| 347 | 1437 | ctx->mb_width = (avctx->width + 15) >> 4; | |
| 348 |
2/2✓ Branch 0 taken 828 times.
✓ Branch 1 taken 609 times.
|
1437 | if (ctx->frame_type) |
| 349 | 828 | ctx->mb_height = (avctx->height + 31) >> 5; | |
| 350 | else | ||
| 351 | 609 | ctx->mb_height = (avctx->height + 15) >> 4; | |
| 352 | |||
| 353 | // QT ignores the written value | ||
| 354 | // slice_count = AV_RB16(buf + 5); | ||
| 355 | 1437 | slice_count = ctx->mb_height * ((ctx->mb_width >> log2_slice_mb_width) + | |
| 356 | 1437 | av_popcount(ctx->mb_width & ctx->slice_mb_width - 1)); | |
| 357 | |||
| 358 |
3/4✓ Branch 0 taken 1406 times.
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1406 times.
|
1437 | if (ctx->slice_count != slice_count || !ctx->slices) { |
| 359 | 31 | av_freep(&ctx->slices); | |
| 360 | 31 | ctx->slice_count = 0; | |
| 361 | 31 | ctx->slices = av_calloc(slice_count, sizeof(*ctx->slices)); | |
| 362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (!ctx->slices) |
| 363 | ✗ | return AVERROR(ENOMEM); | |
| 364 | 31 | ctx->slice_count = slice_count; | |
| 365 | } | ||
| 366 | |||
| 367 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
|
1437 | if (!slice_count) |
| 368 | ✗ | return AVERROR(EINVAL); | |
| 369 | |||
| 370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
|
1437 | if (hdr_size + slice_count*2 > buf_size) { |
| 371 | ✗ | av_log(avctx, AV_LOG_ERROR, "error, wrong slice count\n"); | |
| 372 | ✗ | return AVERROR_INVALIDDATA; | |
| 373 | } | ||
| 374 | |||
| 375 | // parse slice information | ||
| 376 | 1437 | index_ptr = buf + hdr_size; | |
| 377 | 1437 | data_ptr = index_ptr + slice_count*2; | |
| 378 | |||
| 379 | 1437 | slice_mb_count = ctx->slice_mb_width; | |
| 380 | 1437 | mb_x = 0; | |
| 381 | 1437 | mb_y = 0; | |
| 382 | |||
| 383 |
2/2✓ Branch 0 taken 74774 times.
✓ Branch 1 taken 1437 times.
|
76211 | for (i = 0; i < slice_count; i++) { |
| 384 | 74774 | SliceContext *slice = &ctx->slices[i]; | |
| 385 | |||
| 386 | 74774 | slice->data = data_ptr; | |
| 387 | 74774 | data_ptr += AV_RB16(index_ptr + i*2); | |
| 388 | |||
| 389 |
2/2✓ Branch 0 taken 29766 times.
✓ Branch 1 taken 74774 times.
|
104540 | while (ctx->mb_width - mb_x < slice_mb_count) |
| 390 | 29766 | slice_mb_count >>= 1; | |
| 391 | |||
| 392 | 74774 | slice->mb_x = mb_x; | |
| 393 | 74774 | slice->mb_y = mb_y; | |
| 394 | 74774 | slice->mb_count = slice_mb_count; | |
| 395 | 74774 | slice->data_size = data_ptr - slice->data; | |
| 396 | |||
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74774 times.
|
74774 | if (slice->data_size < 6) { |
| 398 | ✗ | av_log(avctx, AV_LOG_ERROR, "error, wrong slice data size\n"); | |
| 399 | ✗ | return AVERROR_INVALIDDATA; | |
| 400 | } | ||
| 401 | |||
| 402 | 74774 | mb_x += slice_mb_count; | |
| 403 |
2/2✓ Branch 0 taken 15735 times.
✓ Branch 1 taken 59039 times.
|
74774 | if (mb_x == ctx->mb_width) { |
| 404 | 15735 | slice_mb_count = ctx->slice_mb_width; | |
| 405 | 15735 | mb_x = 0; | |
| 406 | 15735 | mb_y++; | |
| 407 | } | ||
| 408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74774 times.
|
74774 | if (data_ptr > buf + buf_size) { |
| 409 | ✗ | av_log(avctx, AV_LOG_ERROR, "error, slice out of bounds\n"); | |
| 410 | ✗ | return AVERROR_INVALIDDATA; | |
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 |
2/4✓ Branch 0 taken 1437 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1437 times.
|
1437 | if (mb_x || mb_y != ctx->mb_height) { |
| 415 | ✗ | av_log(avctx, AV_LOG_ERROR, "error wrong mb count y %d h %d\n", | |
| 416 | mb_y, ctx->mb_height); | ||
| 417 | ✗ | return AVERROR_INVALIDDATA; | |
| 418 | } | ||
| 419 | |||
| 420 | 1437 | return pic_data_size; | |
| 421 | } | ||
| 422 | |||
| 423 | #define DECODE_CODEWORD(val, codebook, SKIP) \ | ||
| 424 | do { \ | ||
| 425 | unsigned int rice_order, exp_order, switch_bits; \ | ||
| 426 | unsigned int q, buf, bits; \ | ||
| 427 | \ | ||
| 428 | UPDATE_CACHE_32(re, gb); /* We really need 32 bits */ \ | ||
| 429 | buf = GET_CACHE(re, gb); \ | ||
| 430 | \ | ||
| 431 | /* number of bits to switch between rice and exp golomb */ \ | ||
| 432 | switch_bits = codebook & 3; \ | ||
| 433 | rice_order = codebook >> 5; \ | ||
| 434 | exp_order = (codebook >> 2) & 7; \ | ||
| 435 | \ | ||
| 436 | q = 31 - av_log2(buf); \ | ||
| 437 | \ | ||
| 438 | if (q > switch_bits) { /* exp golomb */ \ | ||
| 439 | bits = exp_order - switch_bits + (q<<1); \ | ||
| 440 | if (bits > 31) \ | ||
| 441 | return AVERROR_INVALIDDATA; \ | ||
| 442 | val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \ | ||
| 443 | ((switch_bits + 1) << rice_order); \ | ||
| 444 | SKIP(re, gb, bits); \ | ||
| 445 | } else if (rice_order) { \ | ||
| 446 | SKIP_BITS(re, gb, q+1); \ | ||
| 447 | val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \ | ||
| 448 | SKIP(re, gb, rice_order); \ | ||
| 449 | } else { \ | ||
| 450 | val = q; \ | ||
| 451 | SKIP(re, gb, q+1); \ | ||
| 452 | } \ | ||
| 453 | } while (0) | ||
| 454 | |||
| 455 | #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1))) | ||
| 456 | |||
| 457 | #define FIRST_DC_CB 0xB8 | ||
| 458 | |||
| 459 | static const uint8_t dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70}; | ||
| 460 | |||
| 461 | 223314 | static av_always_inline int decode_dc_coeffs(GetBitContext *gb, int16_t *out, | |
| 462 | int blocks_per_slice) | ||
| 463 | { | ||
| 464 | int16_t prev_dc; | ||
| 465 | int code, i, sign; | ||
| 466 | |||
| 467 | 223314 | OPEN_READER(re, gb); | |
| 468 | |||
| 469 |
4/6✓ Branch 0 taken 194784 times.
✓ Branch 1 taken 28530 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 194784 times.
✓ Branch 5 taken 28530 times.
✗ Branch 6 not taken.
|
223314 | DECODE_CODEWORD(code, FIRST_DC_CB, LAST_SKIP_BITS); |
| 470 | 223314 | prev_dc = TOSIGNED(code); | |
| 471 | 223314 | out[0] = prev_dc; | |
| 472 | |||
| 473 | 223314 | out += 64; // dc coeff for the next block | |
| 474 | |||
| 475 | 223314 | code = 5; | |
| 476 | 223314 | sign = 0; | |
| 477 |
2/2✓ Branch 0 taken 4049766 times.
✓ Branch 1 taken 223314 times.
|
4273080 | for (i = 1; i < blocks_per_slice; i++, out += 64) { |
| 478 |
5/6✓ Branch 0 taken 2694902 times.
✓ Branch 1 taken 1354864 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2694902 times.
✓ Branch 5 taken 301338 times.
✓ Branch 6 taken 1053526 times.
|
4049766 | DECODE_CODEWORD(code, dc_codebook[FFMIN(code, 6U)], LAST_SKIP_BITS); |
| 479 |
2/2✓ Branch 0 taken 2870012 times.
✓ Branch 1 taken 1179754 times.
|
4049766 | if(code) sign ^= -(code & 1); |
| 480 | 1179754 | else sign = 0; | |
| 481 | 4049766 | prev_dc += (((code + 1) >> 1) ^ sign) - sign; | |
| 482 | 4049766 | out[0] = prev_dc; | |
| 483 | } | ||
| 484 | 223314 | CLOSE_READER(re, gb); | |
| 485 | 223314 | return 0; | |
| 486 | } | ||
| 487 | |||
| 488 | // adaptive codebook switching lut according to previous run/level values | ||
| 489 | static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C }; | ||
| 490 | static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C }; | ||
| 491 | |||
| 492 | 223314 | static av_always_inline int decode_ac_coeffs(AVCodecContext *avctx, GetBitContext *gb, | |
| 493 | int16_t *out, int blocks_per_slice) | ||
| 494 | { | ||
| 495 | 223314 | const ProresContext *ctx = avctx->priv_data; | |
| 496 | int block_mask, sign; | ||
| 497 | unsigned pos, run, level; | ||
| 498 | int max_coeffs, i, bits_left; | ||
| 499 | 223314 | int log2_block_count = av_log2(blocks_per_slice); | |
| 500 | |||
| 501 | 223314 | OPEN_READER(re, gb); | |
| 502 | 223314 | UPDATE_CACHE_32(re, gb); | |
| 503 | 223314 | run = 4; | |
| 504 | 223314 | level = 2; | |
| 505 | |||
| 506 | 223314 | max_coeffs = 64 << log2_block_count; | |
| 507 | 223314 | block_mask = blocks_per_slice - 1; | |
| 508 | |||
| 509 | 223314 | for (pos = block_mask;;) { | |
| 510 | 82640304 | bits_left = gb->size_in_bits - re_index; | |
| 511 |
6/6✓ Branch 0 taken 82601981 times.
✓ Branch 1 taken 38323 times.
✓ Branch 2 taken 771624 times.
✓ Branch 3 taken 81830357 times.
✓ Branch 5 taken 586633 times.
✓ Branch 6 taken 184991 times.
|
82640304 | if (bits_left <= 0 || (bits_left < 32 && !SHOW_UBITS(re, gb, bits_left))) |
| 512 | break; | ||
| 513 | |||
| 514 |
5/6✓ Branch 0 taken 7719704 times.
✓ Branch 1 taken 74697286 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7719704 times.
✓ Branch 5 taken 2313063 times.
✓ Branch 6 taken 72384223 times.
|
82416990 | DECODE_CODEWORD(run, run_to_cb[FFMIN(run, 15)], LAST_SKIP_BITS); |
| 515 | 82416990 | pos += run + 1; | |
| 516 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 82416990 times.
|
82416990 | if (pos >= max_coeffs) { |
| 517 | ✗ | av_log(avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", pos, max_coeffs); | |
| 518 | ✗ | return AVERROR_INVALIDDATA; | |
| 519 | } | ||
| 520 | |||
| 521 |
5/6✓ Branch 0 taken 44505518 times.
✓ Branch 1 taken 37911472 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44505518 times.
✓ Branch 5 taken 9270643 times.
✓ Branch 6 taken 28640829 times.
|
82416990 | DECODE_CODEWORD(level, lev_to_cb[FFMIN(level, 9)], SKIP_BITS); |
| 522 | 82416990 | level += 1; | |
| 523 | |||
| 524 | 82416990 | i = pos >> log2_block_count; | |
| 525 | |||
| 526 | 82416990 | sign = SHOW_SBITS(re, gb, 1); | |
| 527 | 82416990 | SKIP_BITS(re, gb, 1); | |
| 528 | 82416990 | out[((pos & block_mask) << 6) + ctx->scan[i]] = ((level ^ sign) - sign); | |
| 529 | } | ||
| 530 | |||
| 531 | 223314 | CLOSE_READER(re, gb); | |
| 532 | 223314 | return 0; | |
| 533 | } | ||
| 534 | |||
| 535 | 74774 | static int decode_slice_luma(AVCodecContext *avctx, SliceContext *slice, | |
| 536 | uint16_t *dst, int dst_stride, | ||
| 537 | const uint8_t *buf, unsigned buf_size, | ||
| 538 | const int16_t *qmat) | ||
| 539 | { | ||
| 540 | 74774 | const ProresContext *ctx = avctx->priv_data; | |
| 541 | 74774 | LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]); | |
| 542 | int16_t *block; | ||
| 543 | GetBitContext gb; | ||
| 544 | 74774 | int i, blocks_per_slice = slice->mb_count<<2; | |
| 545 | int ret; | ||
| 546 | |||
| 547 |
2/2✓ Branch 0 taken 1805400 times.
✓ Branch 1 taken 74774 times.
|
1880174 | for (i = 0; i < blocks_per_slice; i++) |
| 548 | 1805400 | ctx->bdsp.clear_block(blocks+(i<<6)); | |
| 549 | |||
| 550 | 74774 | init_get_bits(&gb, buf, buf_size << 3); | |
| 551 | |||
| 552 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 74774 times.
|
74774 | if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0) |
| 553 | ✗ | return ret; | |
| 554 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 74774 times.
|
74774 | if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0) |
| 555 | ✗ | return ret; | |
| 556 | |||
| 557 | 74774 | block = blocks; | |
| 558 |
2/2✓ Branch 0 taken 451350 times.
✓ Branch 1 taken 74774 times.
|
526124 | for (i = 0; i < slice->mb_count; i++) { |
| 559 | 451350 | ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat); | |
| 560 | 451350 | ctx->prodsp.idct_put(dst +8, dst_stride, block+(1<<6), qmat); | |
| 561 | 451350 | ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride, block+(2<<6), qmat); | |
| 562 | 451350 | ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride, block+(3<<6), qmat); | |
| 563 | 451350 | block += 4*64; | |
| 564 | 451350 | dst += 16; | |
| 565 | } | ||
| 566 | 74774 | return 0; | |
| 567 | } | ||
| 568 | |||
| 569 | 149548 | static int decode_slice_chroma(AVCodecContext *avctx, SliceContext *slice, | |
| 570 | uint16_t *dst, int dst_stride, | ||
| 571 | const uint8_t *buf, unsigned buf_size, | ||
| 572 | const int16_t *qmat, int log2_blocks_per_mb) | ||
| 573 | { | ||
| 574 | 149548 | ProresContext *ctx = avctx->priv_data; | |
| 575 | 149548 | LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]); | |
| 576 | int16_t *block; | ||
| 577 | GetBitContext gb; | ||
| 578 | 149548 | int i, j, blocks_per_slice = slice->mb_count << log2_blocks_per_mb; | |
| 579 | int ret; | ||
| 580 | |||
| 581 |
2/2✓ Branch 0 taken 2480640 times.
✓ Branch 1 taken 149548 times.
|
2630188 | for (i = 0; i < blocks_per_slice; i++) |
| 582 | 2480640 | ctx->bdsp.clear_block(blocks+(i<<6)); | |
| 583 | |||
| 584 | /* Some encodes have empty chroma scans to simulate grayscale */ | ||
| 585 |
2/2✓ Branch 0 taken 148540 times.
✓ Branch 1 taken 1008 times.
|
149548 | if (buf_size) { |
| 586 | 148540 | init_get_bits(&gb, buf, buf_size << 3); | |
| 587 | |||
| 588 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 148540 times.
|
148540 | if ((ret = decode_dc_coeffs(&gb, blocks, blocks_per_slice)) < 0) |
| 589 | ✗ | return ret; | |
| 590 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 148540 times.
|
148540 | if ((ret = decode_ac_coeffs(avctx, &gb, blocks, blocks_per_slice)) < 0) |
| 591 | ✗ | return ret; | |
| 592 | } | ||
| 593 | |||
| 594 | 149548 | block = blocks; | |
| 595 |
2/2✓ Branch 0 taken 902700 times.
✓ Branch 1 taken 149548 times.
|
1052248 | for (i = 0; i < slice->mb_count; i++) { |
| 596 |
2/2✓ Branch 0 taken 1240320 times.
✓ Branch 1 taken 902700 times.
|
2143020 | for (j = 0; j < log2_blocks_per_mb; j++) { |
| 597 | 1240320 | ctx->prodsp.idct_put(dst, dst_stride, block+(0<<6), qmat); | |
| 598 | 1240320 | ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride, block+(1<<6), qmat); | |
| 599 | 1240320 | block += 2*64; | |
| 600 | 1240320 | dst += 8; | |
| 601 | } | ||
| 602 | } | ||
| 603 | 149548 | return 0; | |
| 604 | } | ||
| 605 | |||
| 606 | /** | ||
| 607 | * Decode alpha slice plane. | ||
| 608 | */ | ||
| 609 | 3060 | static void decode_slice_alpha(const ProresContext *ctx, | |
| 610 | uint16_t *dst, int dst_stride, | ||
| 611 | const uint8_t *buf, int buf_size, | ||
| 612 | int blocks_per_slice) | ||
| 613 | { | ||
| 614 | GetBitContext gb; | ||
| 615 | int i; | ||
| 616 | 3060 | LOCAL_ALIGNED_32(int16_t, blocks, [8*4*64]); | |
| 617 | int16_t *block; | ||
| 618 | |||
| 619 |
2/2✓ Branch 0 taken 97920 times.
✓ Branch 1 taken 3060 times.
|
100980 | for (i = 0; i < blocks_per_slice<<2; i++) |
| 620 | 97920 | ctx->bdsp.clear_block(blocks+(i<<6)); | |
| 621 | |||
| 622 | 3060 | init_get_bits(&gb, buf, buf_size << 3); | |
| 623 | |||
| 624 |
1/2✓ Branch 0 taken 3060 times.
✗ Branch 1 not taken.
|
3060 | if (ctx->alpha_info == 2) { |
| 625 | 3060 | ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16); | |
| 626 | } else { | ||
| 627 | ✗ | ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8); | |
| 628 | } | ||
| 629 | |||
| 630 | 3060 | block = blocks; | |
| 631 | |||
| 632 |
2/2✓ Branch 0 taken 48960 times.
✓ Branch 1 taken 3060 times.
|
52020 | for (i = 0; i < 16; i++) { |
| 633 | 48960 | memcpy(dst, block, 16 * blocks_per_slice * sizeof(*dst)); | |
| 634 | 48960 | dst += dst_stride >> 1; | |
| 635 | 48960 | block += 16 * blocks_per_slice; | |
| 636 | } | ||
| 637 | 3060 | } | |
| 638 | |||
| 639 | 74774 | static int decode_slice_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr) | |
| 640 | { | ||
| 641 | 74774 | const ProresContext *ctx = avctx->priv_data; | |
| 642 | 74774 | SliceContext *slice = &ctx->slices[jobnr]; | |
| 643 | 74774 | const uint8_t *buf = slice->data; | |
| 644 | 74774 | AVFrame *pic = ctx->frame; | |
| 645 | int i, hdr_size, qscale, log2_chroma_blocks_per_mb; | ||
| 646 | int luma_stride, chroma_stride; | ||
| 647 | int y_data_size, u_data_size, v_data_size, a_data_size, offset; | ||
| 648 | uint8_t *dest_y, *dest_u, *dest_v; | ||
| 649 | 74774 | LOCAL_ALIGNED_16(int16_t, qmat_luma_scaled, [64]); | |
| 650 | 74774 | LOCAL_ALIGNED_16(int16_t, qmat_chroma_scaled,[64]); | |
| 651 | int mb_x_shift; | ||
| 652 | int ret; | ||
| 653 | |||
| 654 | 74774 | slice->ret = -1; | |
| 655 | //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n", | ||
| 656 | // jobnr, slice->mb_count, slice->mb_x, slice->mb_y); | ||
| 657 | |||
| 658 | // slice header | ||
| 659 | 74774 | hdr_size = buf[0] >> 3; | |
| 660 | 74774 | qscale = av_clip(buf[1], 1, 224); | |
| 661 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74774 times.
|
74774 | qscale = qscale > 128 ? qscale - 96 << 2: qscale; |
| 662 | 74774 | y_data_size = AV_RB16(buf + 2); | |
| 663 | 74774 | u_data_size = AV_RB16(buf + 4); | |
| 664 | 74774 | v_data_size = slice->data_size - y_data_size - u_data_size - hdr_size; | |
| 665 |
2/2✓ Branch 0 taken 6120 times.
✓ Branch 1 taken 68654 times.
|
74774 | if (hdr_size > 7) v_data_size = AV_RB16(buf + 6); |
| 666 | 74774 | a_data_size = slice->data_size - y_data_size - u_data_size - | |
| 667 | 74774 | v_data_size - hdr_size; | |
| 668 | |||
| 669 |
3/6✓ Branch 0 taken 74774 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74774 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74774 times.
✗ Branch 5 not taken.
|
74774 | if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0 |
| 670 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74774 times.
|
74774 | || hdr_size+y_data_size+u_data_size+v_data_size > slice->data_size){ |
| 671 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid plane data size\n"); | |
| 672 | ✗ | return AVERROR_INVALIDDATA; | |
| 673 | } | ||
| 674 | |||
| 675 | 74774 | buf += hdr_size; | |
| 676 | |||
| 677 |
2/2✓ Branch 0 taken 4785536 times.
✓ Branch 1 taken 74774 times.
|
4860310 | for (i = 0; i < 64; i++) { |
| 678 | 4785536 | qmat_luma_scaled [i] = ctx->qmat_luma [i] * qscale; | |
| 679 | 4785536 | qmat_chroma_scaled[i] = ctx->qmat_chroma[i] * qscale; | |
| 680 | } | ||
| 681 | |||
| 682 |
2/2✓ Branch 0 taken 38094 times.
✓ Branch 1 taken 36680 times.
|
74774 | if (ctx->frame_type == 0) { |
| 683 | 38094 | luma_stride = pic->linesize[0]; | |
| 684 | 38094 | chroma_stride = pic->linesize[1]; | |
| 685 | } else { | ||
| 686 | 36680 | luma_stride = pic->linesize[0] << 1; | |
| 687 | 36680 | chroma_stride = pic->linesize[1] << 1; | |
| 688 | } | ||
| 689 | |||
| 690 |
2/4✓ Branch 0 taken 74774 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74774 times.
✗ Branch 3 not taken.
|
74774 | if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P10 || |
| 691 |
4/4✓ Branch 0 taken 49414 times.
✓ Branch 1 taken 25360 times.
✓ Branch 2 taken 3060 times.
✓ Branch 3 taken 46354 times.
|
74774 | avctx->pix_fmt == AV_PIX_FMT_YUV444P12 || avctx->pix_fmt == AV_PIX_FMT_YUVA444P12) { |
| 692 | 28420 | mb_x_shift = 5; | |
| 693 | 28420 | log2_chroma_blocks_per_mb = 2; | |
| 694 | } else { | ||
| 695 | 46354 | mb_x_shift = 4; | |
| 696 | 46354 | log2_chroma_blocks_per_mb = 1; | |
| 697 | } | ||
| 698 | |||
| 699 | 74774 | offset = (slice->mb_y << 4) * luma_stride + (slice->mb_x << 5); | |
| 700 | 74774 | dest_y = pic->data[0] + offset; | |
| 701 | 74774 | dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift); | |
| 702 | 74774 | dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift); | |
| 703 | |||
| 704 |
4/4✓ Branch 0 taken 36680 times.
✓ Branch 1 taken 38094 times.
✓ Branch 2 taken 18340 times.
✓ Branch 3 taken 18340 times.
|
74774 | if (ctx->frame_type && ctx->first_field ^ !!(ctx->frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)) { |
| 705 | 18340 | dest_y += pic->linesize[0]; | |
| 706 | 18340 | dest_u += pic->linesize[1]; | |
| 707 | 18340 | dest_v += pic->linesize[2]; | |
| 708 | 18340 | offset += pic->linesize[3]; | |
| 709 | } | ||
| 710 | |||
| 711 | 74774 | ret = decode_slice_luma(avctx, slice, (uint16_t*)dest_y, luma_stride, | |
| 712 | buf, y_data_size, qmat_luma_scaled); | ||
| 713 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74774 times.
|
74774 | if (ret < 0) |
| 714 | ✗ | return ret; | |
| 715 | |||
| 716 |
1/2✓ Branch 0 taken 74774 times.
✗ Branch 1 not taken.
|
74774 | if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) { |
| 717 | 74774 | ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_u, chroma_stride, | |
| 718 | buf + y_data_size, u_data_size, | ||
| 719 | qmat_chroma_scaled, log2_chroma_blocks_per_mb); | ||
| 720 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74774 times.
|
74774 | if (ret < 0) |
| 721 | ✗ | return ret; | |
| 722 | |||
| 723 | 74774 | ret = decode_slice_chroma(avctx, slice, (uint16_t*)dest_v, chroma_stride, | |
| 724 | 74774 | buf + y_data_size + u_data_size, v_data_size, | |
| 725 | qmat_chroma_scaled, log2_chroma_blocks_per_mb); | ||
| 726 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74774 times.
|
74774 | if (ret < 0) |
| 727 | ✗ | return ret; | |
| 728 | } | ||
| 729 | |||
| 730 | /* decode alpha plane if available */ | ||
| 731 |
4/6✓ Branch 0 taken 3060 times.
✓ Branch 1 taken 71714 times.
✓ Branch 2 taken 3060 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3060 times.
✗ Branch 5 not taken.
|
74774 | if (ctx->alpha_info && pic->data[3] && a_data_size) { |
| 732 | 3060 | uint8_t *dest_a = pic->data[3] + offset; | |
| 733 | 3060 | decode_slice_alpha(ctx, (uint16_t*)dest_a, luma_stride, | |
| 734 | 3060 | buf + y_data_size + u_data_size + v_data_size, | |
| 735 | 3060 | a_data_size, slice->mb_count); | |
| 736 | } | ||
| 737 | |||
| 738 | 74774 | slice->ret = 0; | |
| 739 | 74774 | return 0; | |
| 740 | } | ||
| 741 | |||
| 742 | 1437 | static int decode_picture(AVCodecContext *avctx) | |
| 743 | { | ||
| 744 | 1437 | ProresContext *ctx = avctx->priv_data; | |
| 745 | int i; | ||
| 746 | 1437 | int error = 0; | |
| 747 | |||
| 748 | 1437 | avctx->execute2(avctx, decode_slice_thread, NULL, NULL, ctx->slice_count); | |
| 749 | |||
| 750 |
2/2✓ Branch 0 taken 74774 times.
✓ Branch 1 taken 1437 times.
|
76211 | for (i = 0; i < ctx->slice_count; i++) |
| 751 | 74774 | error += ctx->slices[i].ret < 0; | |
| 752 | |||
| 753 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
|
1437 | if (error) |
| 754 | ✗ | ctx->frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM; | |
| 755 |
1/2✓ Branch 0 taken 1437 times.
✗ Branch 1 not taken.
|
1437 | if (error < ctx->slice_count) |
| 756 | 1437 | return 0; | |
| 757 | |||
| 758 | ✗ | return ctx->slices[0].ret; | |
| 759 | } | ||
| 760 | |||
| 761 | 1065 | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 762 | int *got_frame, AVPacket *avpkt) | ||
| 763 | { | ||
| 764 | 1065 | ProresContext *ctx = avctx->priv_data; | |
| 765 | 1065 | const uint8_t *buf = avpkt->data; | |
| 766 | 1065 | int buf_size = avpkt->size; | |
| 767 | int frame_hdr_size, pic_size, ret; | ||
| 768 | int i; | ||
| 769 | |||
| 770 |
2/4✓ Branch 0 taken 1065 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1065 times.
|
1065 | if (buf_size < 28 || AV_RL32(buf + 4) != AV_RL32("icpf")) { |
| 771 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid frame header\n"); | |
| 772 | ✗ | return AVERROR_INVALIDDATA; | |
| 773 | } | ||
| 774 | |||
| 775 | 1065 | ctx->frame = frame; | |
| 776 | 1065 | ctx->first_field = 1; | |
| 777 | |||
| 778 | 1065 | buf += 8; | |
| 779 | 1065 | buf_size -= 8; | |
| 780 | |||
| 781 | 1065 | frame_hdr_size = decode_frame_header(ctx, buf, buf_size, avctx); | |
| 782 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
|
1065 | if (frame_hdr_size < 0) |
| 783 | ✗ | return frame_hdr_size; | |
| 784 | |||
| 785 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 1023 times.
|
1065 | if (avctx->skip_frame == AVDISCARD_ALL) |
| 786 | 42 | return 0; | |
| 787 | |||
| 788 | 1023 | buf += frame_hdr_size; | |
| 789 | 1023 | buf_size -= frame_hdr_size; | |
| 790 | |||
| 791 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1023 times.
|
1023 | if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) |
| 792 | ✗ | return ret; | |
| 793 | |||
| 794 | 1023 | av_refstruct_unref(&ctx->hwaccel_picture_private); | |
| 795 | |||
| 796 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1023 times.
|
1023 | if ((ret = ff_hwaccel_frame_priv_alloc(avctx, &ctx->hwaccel_picture_private)) < 0) |
| 797 | ✗ | return ret; | |
| 798 | |||
| 799 | 1023 | ff_thread_finish_setup(avctx); | |
| 800 | |||
| 801 | 1437 | decode_picture: | |
| 802 | 1437 | pic_size = decode_picture_header(avctx, buf, buf_size); | |
| 803 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
|
1437 | if (pic_size < 0) { |
| 804 | ✗ | av_log(avctx, AV_LOG_ERROR, "error decoding picture header\n"); | |
| 805 | ✗ | return pic_size; | |
| 806 | } | ||
| 807 | |||
| 808 | if (HWACCEL_MAX && avctx->hwaccel) { | ||
| 809 | const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel); | ||
| 810 | |||
| 811 | ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size); | ||
| 812 | if (ret < 0) | ||
| 813 | return ret; | ||
| 814 | |||
| 815 | for (i = 0; i < ctx->slice_count; ++i) { | ||
| 816 | ret = hwaccel->decode_slice(avctx, ctx->slices[i].data, ctx->slices[i].data_size); | ||
| 817 | if (ret < 0) | ||
| 818 | return ret; | ||
| 819 | } | ||
| 820 | |||
| 821 | ret = hwaccel->end_frame(avctx); | ||
| 822 | if (ret < 0) | ||
| 823 | return ret; | ||
| 824 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1437 times.
|
1437 | } else if ((ret = decode_picture(avctx)) < 0) { |
| 825 | ✗ | av_log(avctx, AV_LOG_ERROR, "error decoding picture\n"); | |
| 826 | ✗ | return ret; | |
| 827 | } | ||
| 828 | |||
| 829 | 1437 | buf += pic_size; | |
| 830 | 1437 | buf_size -= pic_size; | |
| 831 | |||
| 832 |
6/6✓ Branch 0 taken 828 times.
✓ Branch 1 taken 609 times.
✓ Branch 2 taken 428 times.
✓ Branch 3 taken 400 times.
✓ Branch 4 taken 414 times.
✓ Branch 5 taken 14 times.
|
1437 | if (ctx->frame_type && buf_size > 0 && ctx->first_field) { |
| 833 | 414 | ctx->first_field = 0; | |
| 834 | 414 | goto decode_picture; | |
| 835 | } | ||
| 836 | |||
| 837 | 1023 | av_refstruct_unref(&ctx->hwaccel_picture_private); | |
| 838 | |||
| 839 | 1023 | *got_frame = 1; | |
| 840 | |||
| 841 | 1023 | return avpkt->size; | |
| 842 | } | ||
| 843 | |||
| 844 | 76 | static av_cold int decode_close(AVCodecContext *avctx) | |
| 845 | { | ||
| 846 | 76 | ProresContext *ctx = avctx->priv_data; | |
| 847 | |||
| 848 | 76 | av_freep(&ctx->slices); | |
| 849 | 76 | av_refstruct_unref(&ctx->hwaccel_picture_private); | |
| 850 | |||
| 851 | 76 | return 0; | |
| 852 | } | ||
| 853 | |||
| 854 | #if HAVE_THREADS | ||
| 855 | ✗ | static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) | |
| 856 | { | ||
| 857 | ✗ | ProresContext *csrc = src->priv_data; | |
| 858 | ✗ | ProresContext *cdst = dst->priv_data; | |
| 859 | |||
| 860 | ✗ | cdst->pix_fmt = csrc->pix_fmt; | |
| 861 | ✗ | cdst->frame_type = csrc->frame_type; | |
| 862 | |||
| 863 | ✗ | return 0; | |
| 864 | } | ||
| 865 | #endif | ||
| 866 | |||
| 867 | const FFCodec ff_prores_decoder = { | ||
| 868 | .p.name = "prores", | ||
| 869 | CODEC_LONG_NAME("Apple ProRes (iCodec Pro)"), | ||
| 870 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 871 | .p.id = AV_CODEC_ID_PRORES, | ||
| 872 | .priv_data_size = sizeof(ProresContext), | ||
| 873 | .init = decode_init, | ||
| 874 | .close = decode_close, | ||
| 875 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 876 | UPDATE_THREAD_CONTEXT(update_thread_context), | ||
| 877 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS, | ||
| 878 | .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
| 879 | .p.profiles = NULL_IF_CONFIG_SMALL(ff_prores_profiles), | ||
| 880 | #if HWACCEL_MAX | ||
| 881 | .hw_configs = (const AVCodecHWConfigInternal *const []) { | ||
| 882 | #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL | ||
| 883 | HWACCEL_VIDEOTOOLBOX(prores), | ||
| 884 | #endif | ||
| 885 | #if CONFIG_PRORES_VULKAN_HWACCEL | ||
| 886 | HWACCEL_VULKAN(prores), | ||
| 887 | #endif | ||
| 888 | NULL | ||
| 889 | }, | ||
| 890 | #endif | ||
| 891 | }; | ||
| 892 |