The function to write an ordinary (luma or chroma) plane as well as the function for writing an alpha plane have some similarities: They record the initial bitposition (despite said position always being byte-aligned), flush the PutBitContext themselves and return the amount of bytes they wrote. This commit factors this out; it also replaces bitpositions by bytepositions and it avoids recording the initial byteposition because said information is already available from the position at the end of the last plane.
Unified · Split
| @@ -464,23 +464,17 @@ static void encode_acs(PutBitContext *pb, int16_t *blocks, · 463 unmodified lines | |||
| 464 | } | 464 | } |
| 465 | } | 465 | } |
| 466 | 466 | ||
| 467 | static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb, | 467 | static void encode_slice_plane(ProresContext *ctx, PutBitContext *pb, |
| 468 | const uint16_t *src, ptrdiff_t linesize, | 468 | const uint16_t *src, ptrdiff_t linesize, |
| 469 | int mbs_per_slice, int16_t *blocks, | 469 | int mbs_per_slice, int16_t *blocks, |
| 470 | int blocks_per_mb, int plane_size_factor, | 470 | int blocks_per_mb, int plane_size_factor, |
| 471 | const int16_t *qmat) | 471 | const int16_t *qmat) |
| 472 | { | 472 | { |
| 473 | int blocks_per_slice, saved_pos; | 473 | int blocks_per_slice = mbs_per_slice * blocks_per_mb; |
| 474 | |||
| 475 | saved_pos = put_bits_count(pb); | ||
| 476 | blocks_per_slice = mbs_per_slice * blocks_per_mb; | ||
| 477 | 474 | ||
| 478 | encode_dcs(pb, blocks, blocks_per_slice, qmat[0]); | 475 | encode_dcs(pb, blocks, blocks_per_slice, qmat[0]); |
| 479 | encode_acs(pb, blocks, blocks_per_slice, plane_size_factor, | 476 | encode_acs(pb, blocks, blocks_per_slice, plane_size_factor, |
| 480 | ctx->scantable, qmat); | 477 | ctx->scantable, qmat); |
| 481 | flush_put_bits(pb); | ||
| 482 | |||
| 483 | return (put_bits_count(pb) - saved_pos) >> 3; | ||
| 484 | } | 478 | } |
| 485 | 479 | ||
| 486 | static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits) | 480 | static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits) |
| @@ -516,14 +510,13 @@ static void put_alpha_run(PutBitContext *pb, int run) · 29 unmodified lines | |||
| 516 | } | 510 | } |
| 517 | 511 | ||
| 518 | // todo alpha quantisation for high quants | 512 | // todo alpha quantisation for high quants |
| 519 | static int encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, | 513 | static void encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, |
| 520 | int mbs_per_slice, uint16_t *blocks, | 514 | int mbs_per_slice, uint16_t *blocks, |
| 521 | int quant) | 515 | int quant) |
| 522 | { | 516 | { |
| 523 | const int abits = ctx->alpha_bits; | 517 | const int abits = ctx->alpha_bits; |
| 524 | const int mask = (1 << abits) - 1; | 518 | const int mask = (1 << abits) - 1; |
| 525 | const int num_coeffs = mbs_per_slice * 256; | 519 | const int num_coeffs = mbs_per_slice * 256; |
| 526 | int saved_pos = put_bits_count(pb); | ||
| 527 | int prev = mask, cur; | 520 | int prev = mask, cur; |
| 528 | int idx = 0; | 521 | int idx = 0; |
| 529 | int run = 0; | 522 | int run = 0; |
| @@ -544,8 +537,6 @@ static int encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, · 14 unmodified lines | |||
| 544 | } while (idx < num_coeffs); | 537 | } while (idx < num_coeffs); |
| 545 | if (run) | 538 | if (run) |
| 546 | put_alpha_run(pb, run); | 539 | put_alpha_run(pb, run); |
| 547 | flush_put_bits(pb); | ||
| 548 | return (put_bits_count(pb) - saved_pos) >> 3; | ||
| 549 | } | 540 | } |
| 550 | 541 | ||
| 551 | static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, | 542 | static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, |
| @@ -611,24 +602,23 @@ static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, · 59 unmodified lines | |||
| 611 | ctx->blocks[0], ctx->emu_buf, | 602 | ctx->blocks[0], ctx->emu_buf, |
| 612 | mbs_per_slice, num_cblocks, is_chroma); | 603 | mbs_per_slice, num_cblocks, is_chroma); |
| 613 | if (!is_chroma) {/* luma quant */ | 604 | if (!is_chroma) {/* luma quant */ |
| 614 | sizes[i] = encode_slice_plane(ctx, pb, src, linesize, | 605 | encode_slice_plane(ctx, pb, src, linesize, |
| 615 | mbs_per_slice, ctx->blocks[0], | 606 | mbs_per_slice, ctx->blocks[0], |
| 616 | num_cblocks, plane_factor, | 607 | num_cblocks, plane_factor, qmat); |
| 617 | qmat); | ||
| 618 | } else { /* chroma plane */ | 608 | } else { /* chroma plane */ |
| 619 | sizes[i] = encode_slice_plane(ctx, pb, src, linesize, | 609 | encode_slice_plane(ctx, pb, src, linesize, |
| 620 | mbs_per_slice, ctx->blocks[0], | 610 | mbs_per_slice, ctx->blocks[0], |
| 621 | num_cblocks, plane_factor, | 611 | num_cblocks, plane_factor, qmat_chroma); |
| 622 | qmat_chroma); | ||
| 623 | } | 612 | } |
| 624 | } else { | 613 | } else { |
| 625 | get_alpha_data(ctx, src, linesize, xp, yp, | 614 | get_alpha_data(ctx, src, linesize, xp, yp, |
| 626 | pwidth, avctx->height / ctx->pictures_per_frame, | 615 | pwidth, avctx->height / ctx->pictures_per_frame, |
| 627 | ctx->blocks[0], mbs_per_slice, ctx->alpha_bits); | 616 | ctx->blocks[0], mbs_per_slice, ctx->alpha_bits); |
| 628 | sizes[i] = encode_alpha_plane(ctx, pb, mbs_per_slice, | 617 | encode_alpha_plane(ctx, pb, mbs_per_slice, ctx->blocks[0], quant); |
| 629 | ctx->blocks[0], quant); | ||
| 630 | } | 618 | } |
| 631 | total_size += sizes[i]; | 619 | flush_put_bits(pb); |
| 620 | sizes[i] = put_bytes_output(pb) - total_size; | ||
| 621 | total_size = put_bytes_output(pb); | ||
| 632 | if (put_bits_left(pb) < 0) { | 622 | if (put_bits_left(pb) < 0) { |
| 633 | av_log(avctx, AV_LOG_ERROR, | 623 | av_log(avctx, AV_LOG_ERROR, |
| 634 | "Underestimated required buffer size.\n"); | 624 | "Underestimated required buffer size.\n"); |