Showing the changes to libavcodec (1 of 1 file). Show all
Unified · Split
| @@ -56,6 +56,7 @@ typedef struct PTable { · 55 unmodified lines | ||
| 56 | 56 | typedef struct Slice { |
| 57 | 57 | int width; |
| 58 | 58 | int height; |
| 59 | + int encode_raw; | |
| 59 | 60 | unsigned pos; |
| 60 | 61 | unsigned size; |
| 61 | 62 | uint8_t *slice; |
| @@ -425,7 +426,7 @@ static int encode_table(AVCodecContext *avctx, · 363 unmodified lines | ||
| 425 | 426 | return 0; |
| 426 | 427 | } |
| 427 | 428 | |
| 428 | −static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst_size, | |
| 429 | +static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, | |
| 429 | 430 | int width, int height, int prediction) |
| 430 | 431 | { |
| 431 | 432 | unsigned count = width * height; |
| @@ -442,10 +443,9 @@ static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst · 10 unmodified lines | ||
| 442 | 443 | return count; |
| 443 | 444 | } |
| 444 | 445 | |
| 445 | −static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size, | |
| 446 | − int width, int height, HuffEntry *he, int prediction) | |
| 446 | +static void encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size, | |
| 447 | + int width, int height, HuffEntry *he, int prediction) | |
| 447 | 448 | { |
| 448 | − const uint8_t *osrc = src; | |
| 449 | 449 | PutBitContext pb; |
| 450 | 450 | int count; |
| 451 | 451 | |
| @@ -458,8 +458,6 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz · 6 unmodified lines | ||
| 458 | 458 | for (int i = 0; i < width; i++) { |
| 459 | 459 | const int idx = src[i]; |
| 460 | 460 | const int len = he[idx].len; |
| 461 | − if (put_bits_left(&pb) < len + 32) | |
| 462 | − return encode_plane_slice_raw(osrc, dst, dst_size, width, height, prediction); | |
| 463 | 461 | put_bits(&pb, len, he[idx].code); |
| 464 | 462 | } |
| 465 | 463 | |
| @@ -472,8 +470,7 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz · 6 unmodified lines | ||
| 472 | 470 | put_bits(&pb, 32 - count, 0); |
| 473 | 471 | |
| 474 | 472 | flush_put_bits(&pb); |
| 475 | − | |
| 476 | − return put_bytes_output(&pb); | |
| 473 | + av_assert1(put_bytes_left(&pb, 0) == 0); | |
| 477 | 474 | } |
| 478 | 475 | |
| 479 | 476 | static int encode_slice(AVCodecContext *avctx, void *tdata, |
| @@ -484,10 +481,13 @@ static int encode_slice(AVCodecContext *avctx, void *tdata, · 4 unmodified lines | ||
| 484 | 481 | for (int i = 0; i < s->planes; i++) { |
| 485 | 482 | Slice *sl = &s->slices[n * s->planes + i]; |
| 486 | 483 | |
| 487 | − sl->size = | |
| 484 | + if (sl->encode_raw) | |
| 485 | + encode_plane_slice_raw(sl->slice, sl->bitslice, | |
| 486 | + sl->width, sl->height, s->frame_pred); | |
| 487 | + else | |
| 488 | 488 | encode_plane_slice(sl->slice, |
| 489 | 489 | sl->bitslice, |
| 490 | − s->bitslice_size, | |
| 490 | + sl->size, | |
| 491 | 491 | sl->width, sl->height, |
| 492 | 492 | s->he[i], s->frame_pred); |
| 493 | 493 | } |
| @@ -612,6 +612,20 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt, · 118 unmodified lines | ||
| 612 | 612 | tables_size = put_bytes_count(&pbit, 1); |
| 613 | 613 | bytestream2_skip_p(&pb, tables_size); |
| 614 | 614 | |
| 615 | + for (int i = 0; i < s->planes; ++i) { | |
| 616 | + for (int j = 0; j < s->nb_slices; ++j) { | |
| 617 | + Slice *const sl = &s->slices[j * s->planes + i]; | |
| 618 | + int64_t size = 0; | |
| 619 | + | |
| 620 | + for (size_t k = 0; k < FF_ARRAY_ELEMS(sl->counts); ++k) | |
| 621 | + size += sl->counts[k] * s->he[i][k].len; | |
| 622 | + size = AV_CEIL_RSHIFT(size, 3); | |
| 623 | + sl->encode_raw = size >= sl->width * sl->height; | |
| 624 | + if (sl->encode_raw) | |
| 625 | + size = sl->width * sl->height; | |
| 626 | + sl->size = FFALIGN(size + 2, 4); | |
| 627 | + } | |
| 628 | + } | |
| 615 | 629 | avctx->execute2(avctx, encode_slice, NULL, NULL, s->nb_slices); |
| 616 | 630 | |
| 617 | 631 | for (int n = 0; n < s->nb_slices; n++) { |