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 | 468 | const uint16_t *src, ptrdiff_t linesize, |
| 469 | 469 | int mbs_per_slice, int16_t *blocks, |
| 470 | 470 | int blocks_per_mb, int plane_size_factor, |
| 471 | 471 | const int16_t *qmat) |
| 472 | 472 | { |
| 473 | − int blocks_per_slice, saved_pos; | |
| 474 | − | |
| 475 | − saved_pos = put_bits_count(pb); | |
| 476 | − blocks_per_slice = mbs_per_slice * blocks_per_mb; | |
| 473 | + int blocks_per_slice = mbs_per_slice * blocks_per_mb; | |
| 477 | 474 | |
| 478 | 475 | encode_dcs(pb, blocks, blocks_per_slice, qmat[0]); |
| 479 | 476 | encode_acs(pb, blocks, blocks_per_slice, plane_size_factor, |
| 480 | 477 | ctx->scantable, qmat); |
| 481 | − flush_put_bits(pb); | |
| 482 | − | |
| 483 | − return (put_bits_count(pb) - saved_pos) >> 3; | |
| 484 | 478 | } |
| 485 | 479 | |
| 486 | 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 | 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 | 514 | int mbs_per_slice, uint16_t *blocks, |
| 521 | 515 | int quant) |
| 522 | 516 | { |
| 523 | 517 | const int abits = ctx->alpha_bits; |
| 524 | 518 | const int mask = (1 << abits) - 1; |
| 525 | 519 | const int num_coeffs = mbs_per_slice * 256; |
| 526 | − int saved_pos = put_bits_count(pb); | |
| 527 | 520 | int prev = mask, cur; |
| 528 | 521 | int idx = 0; |
| 529 | 522 | int run = 0; |
| @@ -544,8 +537,6 @@ static int encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, · 14 unmodified lines | ||
| 544 | 537 | } while (idx < num_coeffs); |
| 545 | 538 | if (run) |
| 546 | 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 | 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 | 602 | ctx->blocks[0], ctx->emu_buf, |
| 612 | 603 | mbs_per_slice, num_cblocks, is_chroma); |
| 613 | 604 | if (!is_chroma) {/* luma quant */ |
| 614 | − sizes[i] = encode_slice_plane(ctx, pb, src, linesize, | |
| 615 | − mbs_per_slice, ctx->blocks[0], | |
| 616 | − num_cblocks, plane_factor, | |
| 617 | − qmat); | |
| 605 | + encode_slice_plane(ctx, pb, src, linesize, | |
| 606 | + mbs_per_slice, ctx->blocks[0], | |
| 607 | + num_cblocks, plane_factor, qmat); | |
| 618 | 608 | } else { /* chroma plane */ |
| 619 | − sizes[i] = encode_slice_plane(ctx, pb, src, linesize, | |
| 620 | − mbs_per_slice, ctx->blocks[0], | |
| 621 | − num_cblocks, plane_factor, | |
| 622 | − qmat_chroma); | |
| 609 | + encode_slice_plane(ctx, pb, src, linesize, | |
| 610 | + mbs_per_slice, ctx->blocks[0], | |
| 611 | + num_cblocks, plane_factor, qmat_chroma); | |
| 623 | 612 | } |
| 624 | 613 | } else { |
| 625 | 614 | get_alpha_data(ctx, src, linesize, xp, yp, |
| 626 | 615 | pwidth, avctx->height / ctx->pictures_per_frame, |
| 627 | 616 | ctx->blocks[0], mbs_per_slice, ctx->alpha_bits); |
| 628 | − sizes[i] = encode_alpha_plane(ctx, pb, mbs_per_slice, | |
| 629 | − ctx->blocks[0], quant); | |
| 617 | + encode_alpha_plane(ctx, pb, mbs_per_slice, 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 | 622 | if (put_bits_left(pb) < 0) { |
| 633 | 623 | av_log(avctx, AV_LOG_ERROR, |
| 634 | 624 | "Underestimated required buffer size.\n"); |