Each field slice is assigned to one thread. Serial performance is unaffected.
Unified · Split
| @@ -58,6 +58,8 @@ typedef struct SHQContext { · 57 unmodified lines | ||
| 58 | 58 | enum { SHQ_SUBSAMPLING_420, SHQ_SUBSAMPLING_422, SHQ_SUBSAMPLING_444 } |
| 59 | 59 | subsampling; |
| 60 | 60 | enum { SHQ_NO_ALPHA, SHQ_RLE_ALPHA, SHQ_DCT_ALPHA } alpha_type; |
| 61 | + AVPacket *avpkt; | |
| 62 | + uint32_t second_field_offset; | |
| 61 | 63 | } SHQContext; |
| 62 | 64 | |
| 63 | 65 | /* NOTE: The first element is always 16, unscaled. */ |
| @@ -266,9 +268,10 @@ static int decode_speedhq_border(const SHQContext *s, GetBitContext *gb, AVFrame · 202 unmodified lines | ||
| 266 | 268 | return 0; |
| 267 | 269 | } |
| 268 | 270 | |
| 269 | −static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf_size, AVFrame *frame, int field_number, int start, int end, int line_stride) | |
| 271 | +static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf_size, AVFrame *frame, int field_number, int start, int end, int line_stride, int slice_number) | |
| 270 | 272 | { |
| 271 | − int ret, slice_number, slice_offsets[5]; | |
| 273 | + int ret, x, y, slice_offsets[5]; | |
| 274 | + uint32_t slice_begin, slice_end; | |
| 272 | 275 | int linesize_y = frame->linesize[0] * line_stride; |
| 273 | 276 | int linesize_cb = frame->linesize[1] * line_stride; |
| 274 | 277 | int linesize_cr = frame->linesize[2] * line_stride; |
| @@ -283,21 +286,17 @@ static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf · 8 unmodified lines | ||
| 283 | 286 | |
| 284 | 287 | slice_offsets[0] = start; |
| 285 | 288 | slice_offsets[4] = end; |
| 286 | − for (slice_number = 1; slice_number < 4; slice_number++) { | |
| 289 | + for (x = 1; x < 4; x++) { | |
| 287 | 290 | uint32_t last_offset, slice_len; |
| 288 | 291 | |
| 289 | − last_offset = slice_offsets[slice_number - 1]; | |
| 292 | + last_offset = slice_offsets[x - 1]; | |
| 290 | 293 | slice_len = AV_RL24(buf + last_offset); |
| 291 | − slice_offsets[slice_number] = last_offset + slice_len; | |
| 294 | + slice_offsets[x] = last_offset + slice_len; | |
| 292 | 295 | |
| 293 | − if (slice_len < 3 || slice_offsets[slice_number] > end - 3) | |
| 296 | + if (slice_len < 3 || slice_offsets[x] > end - 3) | |
| 294 | 297 | return AVERROR_INVALIDDATA; |
| 295 | 298 | } |
| 296 | 299 | |
| 297 | − for (slice_number = 0; slice_number < 4; slice_number++) { | |
| 298 | − uint32_t slice_begin, slice_end; | |
| 299 | − int x, y; | |
| 300 | − | |
| 301 | 300 | slice_begin = slice_offsets[slice_number]; |
| 302 | 301 | slice_end = slice_offsets[slice_number + 1]; |
| 303 | 302 | |
| @@ -390,14 +389,34 @@ static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf · 86 unmodified lines | ||
| 390 | 389 | } |
| 391 | 390 | } |
| 392 | 391 | } |
| 393 | − } | |
| 394 | 392 | |
| 395 | − if (s->subsampling != SHQ_SUBSAMPLING_444 && (frame->width & 15)) | |
| 393 | + if (s->subsampling != SHQ_SUBSAMPLING_444 && (frame->width & 15) && slice_number == 3) | |
| 396 | 394 | return decode_speedhq_border(s, &gb, frame, field_number, line_stride); |
| 397 | 395 | |
| 398 | 396 | return 0; |
| 399 | 397 | } |
| 400 | 398 | |
| 399 | +static int decode_slice_progressive(AVCodecContext *avctx, void *arg, int jobnr, int threadnr) | |
| 400 | +{ | |
| 401 | + SHQContext *s = avctx->priv_data; | |
| 402 | + (void)threadnr; | |
| 403 | + | |
| 404 | + return decode_speedhq_field(avctx->priv_data, s->avpkt->data, s->avpkt->size, arg, 0, 4, s->avpkt->size, 1, jobnr); | |
| 405 | +} | |
| 406 | + | |
| 407 | +static int decode_slice_interlaced(AVCodecContext *avctx, void *arg, int jobnr, int threadnr) | |
| 408 | +{ | |
| 409 | + SHQContext *s = avctx->priv_data; | |
| 410 | + int field_number = jobnr / 4; | |
| 411 | + int slice_number = jobnr % 4; | |
| 412 | + (void)threadnr; | |
| 413 | + | |
| 414 | + if (field_number == 0) | |
| 415 | + return decode_speedhq_field(avctx->priv_data, s->avpkt->data, s->avpkt->size, arg, 0, 4, s->second_field_offset, 2, slice_number); | |
| 416 | + else | |
| 417 | + return decode_speedhq_field(avctx->priv_data, s->avpkt->data, s->avpkt->size, arg, 1, s->second_field_offset, s->avpkt->size, 2, slice_number); | |
| 418 | +} | |
| 419 | + | |
| 401 | 420 | static void compute_quant_matrix(int *output, int qscale) |
| 402 | 421 | { |
| 403 | 422 | int i; |
| @@ -411,7 +430,6 @@ static int speedhq_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 7 unmodified lines | ||
| 411 | 430 | const uint8_t *buf = avpkt->data; |
| 412 | 431 | int buf_size = avpkt->size; |
| 413 | 432 | uint8_t quality; |
| 414 | − uint32_t second_field_offset; | |
| 415 | 433 | int ret; |
| 416 | 434 | |
| 417 | 435 | if (buf_size < 4 || avctx->width < 8 || avctx->width % 8 != 0) |
| @@ -429,8 +447,8 @@ static int speedhq_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 11 unmodified lines | ||
| 429 | 447 | |
| 430 | 448 | compute_quant_matrix(s->quant_matrix, 100 - quality); |
| 431 | 449 | |
| 432 | − second_field_offset = AV_RL24(buf + 1); | |
| 433 | − if (second_field_offset >= buf_size - 3) { | |
| 450 | + s->second_field_offset = AV_RL24(buf + 1); | |
| 451 | + if (s->second_field_offset >= buf_size - 3) { | |
| 434 | 452 | return AVERROR_INVALIDDATA; |
| 435 | 453 | } |
| 436 | 454 | |
| @@ -441,7 +459,9 @@ static int speedhq_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 4 unmodified lines | ||
| 441 | 459 | return ret; |
| 442 | 460 | } |
| 443 | 461 | |
| 444 | − if (second_field_offset == 4 || second_field_offset == (buf_size-4)) { | |
| 462 | + s->avpkt = avpkt; | |
| 463 | + | |
| 464 | + if (s->second_field_offset == 4 || s->second_field_offset == (buf_size-4)) { | |
| 445 | 465 | /* |
| 446 | 466 | * Overlapping first and second fields is used to signal |
| 447 | 467 | * encoding only a single field. In this case, "height" |
| @@ -451,12 +471,10 @@ static int speedhq_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 3 unmodified lines | ||
| 451 | 471 | * but this matches the convention used in NDI, which is |
| 452 | 472 | * the primary user of this trick. |
| 453 | 473 | */ |
| 454 | − if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, buf_size, 1)) < 0) | |
| 474 | + if ((ret = avctx->execute2(avctx, decode_slice_progressive, frame, NULL, 4)) < 0) | |
| 455 | 475 | return ret; |
| 456 | 476 | } else { |
| 457 | − if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 0, 4, second_field_offset, 2)) < 0) | |
| 458 | − return ret; | |
| 459 | − if ((ret = decode_speedhq_field(s, buf, buf_size, frame, 1, second_field_offset, buf_size, 2)) < 0) | |
| 477 | + if ((ret = avctx->execute2(avctx, decode_slice_interlaced, frame, NULL, 8)) < 0) | |
| 460 | 478 | return ret; |
| 461 | 479 | } |
| 462 | 480 | |
| @@ -652,5 +670,5 @@ const FFCodec ff_speedhq_decoder = { · 189 unmodified lines | ||
| 652 | 670 | .priv_data_size = sizeof(SHQContext), |
| 653 | 671 | .init = speedhq_decode_init, |
| 654 | 672 | FF_CODEC_DECODE_CB(speedhq_decode_frame), |
| 655 | − .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | |
| 673 | + .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS, | |
| 656 | 674 | }; |