| @@ -419,7 +419,8 @@ static av_cold int init_matrices(MpegEncContext *s, AVCodecContext *avctx) · 418 unmodified lines |
| 419 | /* init video encoder */ | 419 | /* init video encoder */ |
| 420 | av_cold int ff_mpv_encode_init(AVCodecContext *avctx) | 420 | av_cold int ff_mpv_encode_init(AVCodecContext *avctx) |
| 421 | { | 421 | { |
| 422 | MpegEncContext *s = avctx->priv_data; | 422 | MPVMainEncContext *const m = avctx->priv_data; |
| | 423 | MpegEncContext *const s = &m->s; |
| 423 | AVCPBProperties *cpb_props; | 424 | AVCPBProperties *cpb_props; |
| 424 | int i, ret; | 425 | int i, ret; |
| 425 | int mb_array_size, mv_table_size; | 426 | int mb_array_size, mv_table_size; |
| @@ -456,10 +457,10 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 30 unmodified lines |
| 456 | } | 457 | } |
| 457 | s->gop_size = avctx->gop_size; | 458 | s->gop_size = avctx->gop_size; |
| 458 | s->avctx = avctx; | 459 | s->avctx = avctx; |
| 459 | if (avctx->max_b_frames > MAX_B_FRAMES) { | 460 | if (avctx->max_b_frames > MPVENC_MAX_B_FRAMES) { |
| 460 | av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum " | 461 | av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum " |
| 461 | "is %d.\n", MAX_B_FRAMES); | 462 | "is " AV_STRINGIFY(MPVENC_MAX_B_FRAMES) ".\n"); |
| 462 | avctx->max_b_frames = MAX_B_FRAMES; | 463 | avctx->max_b_frames = MPVENC_MAX_B_FRAMES; |
| 463 | } else if (avctx->max_b_frames < 0) { | 464 | } else if (avctx->max_b_frames < 0) { |
| 464 | av_log(avctx, AV_LOG_ERROR, | 465 | av_log(avctx, AV_LOG_ERROR, |
| 465 | "max b frames must be 0 or positive for mpegvideo based encoders\n"); | 466 | "max b frames must be 0 or positive for mpegvideo based encoders\n"); |
| @@ -700,10 +701,10 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 234 unmodified lines |
| 700 | return AVERROR(EINVAL); | 701 | return AVERROR(EINVAL); |
| 701 | } | 702 | } |
| 702 | | 703 | |
| 703 | if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) { | 704 | if (m->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) { |
| 704 | av_log(avctx, AV_LOG_INFO, | 705 | av_log(avctx, AV_LOG_INFO, |
| 705 | "notice: b_frame_strategy only affects the first pass\n"); | 706 | "notice: b_frame_strategy only affects the first pass\n"); |
| 706 | s->b_frame_strategy = 0; | 707 | m->b_frame_strategy = 0; |
| 707 | } | 708 | } |
| 708 | | 709 | |
| 709 | i = av_gcd(avctx->time_base.den, avctx->time_base.num); | 710 | i = av_gcd(avctx->time_base.den, avctx->time_base.num); |
| @@ -914,8 +915,8 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 204 unmodified lines |
| 914 | return ret; | 915 | return ret; |
| 915 | | 916 | |
| 916 | if (!(avctx->stats_out = av_mallocz(256)) || | 917 | if (!(avctx->stats_out = av_mallocz(256)) || |
| 917 | !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MAX_B_FRAMES + 1) || | 918 | !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MPVENC_MAX_B_FRAMES + 1) || |
| 918 | !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_B_FRAMES + 1) || | 919 | !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MPVENC_MAX_B_FRAMES + 1) || |
| 919 | !(s->new_pic = av_frame_alloc()) || | 920 | !(s->new_pic = av_frame_alloc()) || |
| 920 | !(s->picture_pool = ff_mpv_alloc_pic_pool(0))) | 921 | !(s->picture_pool = ff_mpv_alloc_pic_pool(0))) |
| 921 | return AVERROR(ENOMEM); | 922 | return AVERROR(ENOMEM); |
| @@ -1010,17 +1011,17 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 88 unmodified lines |
| 1010 | if ((ret = ff_rate_control_init(s)) < 0) | 1011 | if ((ret = ff_rate_control_init(s)) < 0) |
| 1011 | return ret; | 1012 | return ret; |
| 1012 | | 1013 | |
| 1013 | if (s->b_frame_strategy == 2) { | 1014 | if (m->b_frame_strategy == 2) { |
| 1014 | for (i = 0; i < s->max_b_frames + 2; i++) { | 1015 | for (i = 0; i < s->max_b_frames + 2; i++) { |
| 1015 | s->tmp_frames[i] = av_frame_alloc(); | 1016 | m->tmp_frames[i] = av_frame_alloc(); |
| 1016 | if (!s->tmp_frames[i]) | 1017 | if (!m->tmp_frames[i]) |
| 1017 | return AVERROR(ENOMEM); | 1018 | return AVERROR(ENOMEM); |
| 1018 | | 1019 | |
| 1019 | s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P; | 1020 | m->tmp_frames[i]->format = AV_PIX_FMT_YUV420P; |
| 1020 | s->tmp_frames[i]->width = s->width >> s->brd_scale; | 1021 | m->tmp_frames[i]->width = s->width >> m->brd_scale; |
| 1021 | s->tmp_frames[i]->height = s->height >> s->brd_scale; | 1022 | m->tmp_frames[i]->height = s->height >> m->brd_scale; |
| 1022 | | 1023 | |
| 1023 | ret = av_frame_get_buffer(s->tmp_frames[i], 0); | 1024 | ret = av_frame_get_buffer(m->tmp_frames[i], 0); |
| 1024 | if (ret < 0) | 1025 | if (ret < 0) |
| 1025 | return ret; | 1026 | return ret; |
| 1026 | } | 1027 | } |
| @@ -1039,8 +1040,8 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 12 unmodified lines |
| 1039 | | 1040 | |
| 1040 | av_cold int ff_mpv_encode_end(AVCodecContext *avctx) | 1041 | av_cold int ff_mpv_encode_end(AVCodecContext *avctx) |
| 1041 | { | 1042 | { |
| 1042 | MpegEncContext *s = avctx->priv_data; | 1043 | MPVMainEncContext *const m = avctx->priv_data; |
| 1043 | int i; | 1044 | MpegEncContext *const s = &m->s; |
| 1044 | | 1045 | |
| 1045 | ff_rate_control_uninit(&s->rc_context); | 1046 | ff_rate_control_uninit(&s->rc_context); |
| 1046 | | 1047 | |
| @@ -1048,13 +1049,13 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) · 1 unmodified line |
| 1048 | av_refstruct_pool_uninit(&s->picture_pool); | 1049 | av_refstruct_pool_uninit(&s->picture_pool); |
| 1049 | | 1050 | |
| 1050 | if (s->input_picture && s->reordered_input_picture) { | 1051 | if (s->input_picture && s->reordered_input_picture) { |
| 1051 | for (int i = 0; i < MAX_B_FRAMES + 1; i++) { | 1052 | for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) { |
| 1052 | av_refstruct_unref(&s->input_picture[i]); | 1053 | av_refstruct_unref(&s->input_picture[i]); |
| 1053 | av_refstruct_unref(&s->reordered_input_picture[i]); | 1054 | av_refstruct_unref(&s->reordered_input_picture[i]); |
| 1054 | } | 1055 | } |
| 1055 | } | 1056 | } |
| 1056 | for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++) | 1057 | for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++) |
| 1057 | av_frame_free(&s->tmp_frames[i]); | 1058 | av_frame_free(&m->tmp_frames[i]); |
| 1058 | | 1059 | |
| 1059 | av_frame_free(&s->new_pic); | 1060 | av_frame_free(&s->new_pic); |
| 1060 | | 1061 | |
| @@ -1249,8 +1250,9 @@ static int prepare_picture(MpegEncContext *s, AVFrame *f, const AVFrame *props_f · 188 unmodified lines |
| 1249 | return 0; | 1250 | return 0; |
| 1250 | } | 1251 | } |
| 1251 | | 1252 | |
| 1252 | static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg) | 1253 | static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg) |
| 1253 | { | 1254 | { |
| | 1255 | MpegEncContext *const s = &m->s; |
| 1254 | MPVPicture *pic = NULL; | 1256 | MPVPicture *pic = NULL; |
| 1255 | int64_t pts; | 1257 | int64_t pts; |
| 1256 | int display_picture_number = 0, ret; | 1258 | int display_picture_number = 0, ret; |
| @@ -1376,9 +1378,9 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg) · 119 unmodified lines |
| 1376 | } | 1378 | } |
| 1377 | | 1379 | |
| 1378 | /* shift buffer entries */ | 1380 | /* shift buffer entries */ |
| 1379 | for (int i = flush_offset; i <= MAX_B_FRAMES; i++) | 1381 | for (int i = flush_offset; i <= MPVENC_MAX_B_FRAMES; i++) |
| 1380 | s->input_picture[i - flush_offset] = s->input_picture[i]; | 1382 | s->input_picture[i - flush_offset] = s->input_picture[i]; |
| 1381 | for (int i = MAX_B_FRAMES + 1 - flush_offset; i <= MAX_B_FRAMES; i++) | 1383 | for (int i = MPVENC_MAX_B_FRAMES + 1 - flush_offset; i <= MPVENC_MAX_B_FRAMES; i++) |
| 1382 | s->input_picture[i] = NULL; | 1384 | s->input_picture[i] = NULL; |
| 1383 | | 1385 | |
| 1384 | s->input_picture[encoding_delay] = pic; | 1386 | s->input_picture[encoding_delay] = pic; |
| @@ -1451,10 +1453,11 @@ static int encode_frame(AVCodecContext *c, const AVFrame *frame, AVPacket *pkt) · 66 unmodified lines |
| 1451 | return size; | 1453 | return size; |
| 1452 | } | 1454 | } |
| 1453 | | 1455 | |
| 1454 | static int estimate_best_b_count(MpegEncContext *s) | 1456 | static int estimate_best_b_count(MPVMainEncContext *const m) |
| 1455 | { | 1457 | { |
| | 1458 | MpegEncContext *const s = &m->s; |
| 1456 | AVPacket *pkt; | 1459 | AVPacket *pkt; |
| 1457 | const int scale = s->brd_scale; | 1460 | const int scale = m->brd_scale; |
| 1458 | int width = s->width >> scale; | 1461 | int width = s->width >> scale; |
| 1459 | int height = s->height >> scale; | 1462 | int height = s->height >> scale; |
| 1460 | int i, j, out_size, p_lambda, b_lambda, lambda2; | 1463 | int i, j, out_size, p_lambda, b_lambda, lambda2; |
| @@ -1491,18 +1494,18 @@ static int estimate_best_b_count(MpegEncContext *s) · 30 unmodified lines |
| 1491 | data[2] += INPLACE_OFFSET; | 1494 | data[2] += INPLACE_OFFSET; |
| 1492 | } | 1495 | } |
| 1493 | | 1496 | |
| 1494 | s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0], | 1497 | s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[0], |
| 1495 | s->tmp_frames[i]->linesize[0], | 1498 | m->tmp_frames[i]->linesize[0], |
| 1496 | data[0], | 1499 | data[0], |
| 1497 | pre_input_ptr->f->linesize[0], | 1500 | pre_input_ptr->f->linesize[0], |
| 1498 | width, height); | 1501 | width, height); |
| 1499 | s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1], | 1502 | s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[1], |
| 1500 | s->tmp_frames[i]->linesize[1], | 1503 | m->tmp_frames[i]->linesize[1], |
| 1501 | data[1], | 1504 | data[1], |
| 1502 | pre_input_ptr->f->linesize[1], | 1505 | pre_input_ptr->f->linesize[1], |
| 1503 | width >> 1, height >> 1); | 1506 | width >> 1, height >> 1); |
| 1504 | s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2], | 1507 | s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[2], |
| 1505 | s->tmp_frames[i]->linesize[2], | 1508 | m->tmp_frames[i]->linesize[2], |
| 1506 | data[2], | 1509 | data[2], |
| 1507 | pre_input_ptr->f->linesize[2], | 1510 | pre_input_ptr->f->linesize[2], |
| 1508 | width >> 1, height >> 1); | 1511 | width >> 1, height >> 1); |
| @@ -1539,10 +1542,10 @@ static int estimate_best_b_count(MpegEncContext *s) · 30 unmodified lines |
| 1539 | goto fail; | 1542 | goto fail; |
| 1540 | | 1543 | |
| 1541 | | 1544 | |
| 1542 | s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I; | 1545 | m->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I; |
| 1543 | s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA; | 1546 | m->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA; |
| 1544 | | 1547 | |
| 1545 | out_size = encode_frame(c, s->tmp_frames[0], pkt); | 1548 | out_size = encode_frame(c, m->tmp_frames[0], pkt); |
| 1546 | if (out_size < 0) { | 1549 | if (out_size < 0) { |
| 1547 | ret = out_size; | 1550 | ret = out_size; |
| 1548 | goto fail; | 1551 | goto fail; |
| @@ -1553,11 +1556,11 @@ static int estimate_best_b_count(MpegEncContext *s) · 4 unmodified lines |
| 1553 | for (i = 0; i < s->max_b_frames + 1; i++) { | 1556 | for (i = 0; i < s->max_b_frames + 1; i++) { |
| 1554 | int is_p = i % (j + 1) == j || i == s->max_b_frames; | 1557 | int is_p = i % (j + 1) == j || i == s->max_b_frames; |
| 1555 | | 1558 | |
| 1556 | s->tmp_frames[i + 1]->pict_type = is_p ? | 1559 | m->tmp_frames[i + 1]->pict_type = is_p ? |
| 1557 | AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B; | 1560 | AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B; |
| 1558 | s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda; | 1561 | m->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda; |
| 1559 | | 1562 | |
| 1560 | out_size = encode_frame(c, s->tmp_frames[i + 1], pkt); | 1563 | out_size = encode_frame(c, m->tmp_frames[i + 1], pkt); |
| 1561 | if (out_size < 0) { | 1564 | if (out_size < 0) { |
| 1562 | ret = out_size; | 1565 | ret = out_size; |
| 1563 | goto fail; | 1566 | goto fail; |
| @@ -1603,8 +1606,10 @@ fail: · 39 unmodified lines |
| 1603 | * input_picture[0] is always NULL when exiting this function, even on error; | 1606 | * input_picture[0] is always NULL when exiting this function, even on error; |
| 1604 | * reordered_input_picture[0] is always NULL when exiting this function on error. | 1607 | * reordered_input_picture[0] is always NULL when exiting this function on error. |
| 1605 | */ | 1608 | */ |
| 1606 | static int set_bframe_chain_length(MpegEncContext *s) | 1609 | static int set_bframe_chain_length(MPVMainEncContext *const m) |
| 1607 | { | 1610 | { |
| | 1611 | MpegEncContext *const s = &m->s; |
| | 1612 | |
| 1608 | /* Either nothing to do or can't do anything */ | 1613 | /* Either nothing to do or can't do anything */ |
| 1609 | if (s->reordered_input_picture[0] || !s->input_picture[0]) | 1614 | if (s->reordered_input_picture[0] || !s->input_picture[0]) |
| 1610 | return 0; | 1615 | return 0; |
| @@ -1649,11 +1654,11 @@ static int set_bframe_chain_length(MpegEncContext *s) · 38 unmodified lines |
| 1649 | } | 1654 | } |
| 1650 | } | 1655 | } |
| 1651 | | 1656 | |
| 1652 | if (s->b_frame_strategy == 0) { | 1657 | if (m->b_frame_strategy == 0) { |
| 1653 | b_frames = s->max_b_frames; | 1658 | b_frames = s->max_b_frames; |
| 1654 | while (b_frames && !s->input_picture[b_frames]) | 1659 | while (b_frames && !s->input_picture[b_frames]) |
| 1655 | b_frames--; | 1660 | b_frames--; |
| 1656 | } else if (s->b_frame_strategy == 1) { | 1661 | } else if (m->b_frame_strategy == 1) { |
| 1657 | int i; | 1662 | int i; |
| 1658 | for (i = 1; i < s->max_b_frames + 1; i++) { | 1663 | for (i = 1; i < s->max_b_frames + 1; i++) { |
| 1659 | if (s->input_picture[i] && | 1664 | if (s->input_picture[i] && |
| @@ -1668,7 +1673,7 @@ static int set_bframe_chain_length(MpegEncContext *s) · 8 unmodified lines |
| 1668 | for (i = 0; i < s->max_b_frames + 1; i++) { | 1673 | for (i = 0; i < s->max_b_frames + 1; i++) { |
| 1669 | if (!s->input_picture[i] || | 1674 | if (!s->input_picture[i] || |
| 1670 | s->input_picture[i]->b_frame_score - 1 > | 1675 | s->input_picture[i]->b_frame_score - 1 > |
| 1671 | s->mb_num / s->b_sensitivity) | 1676 | s->mb_num / m->b_sensitivity) |
| 1672 | break; | 1677 | break; |
| 1673 | } | 1678 | } |
| 1674 | | 1679 | |
| @@ -1678,8 +1683,8 @@ static int set_bframe_chain_length(MpegEncContext *s) · 3 unmodified lines |
| 1678 | for (i = 0; i < b_frames + 1; i++) { | 1683 | for (i = 0; i < b_frames + 1; i++) { |
| 1679 | s->input_picture[i]->b_frame_score = 0; | 1684 | s->input_picture[i]->b_frame_score = 0; |
| 1680 | } | 1685 | } |
| 1681 | } else if (s->b_frame_strategy == 2) { | 1686 | } else if (m->b_frame_strategy == 2) { |
| 1682 | b_frames = estimate_best_b_count(s); | 1687 | b_frames = estimate_best_b_count(m); |
| 1683 | if (b_frames < 0) { | 1688 | if (b_frames < 0) { |
| 1684 | av_refstruct_unref(&s->input_picture[0]); | 1689 | av_refstruct_unref(&s->input_picture[0]); |
| 1685 | return b_frames; | 1690 | return b_frames; |
| @@ -1733,17 +1738,18 @@ static int set_bframe_chain_length(MpegEncContext *s) · 47 unmodified lines |
| 1733 | return 0; | 1738 | return 0; |
| 1734 | } | 1739 | } |
| 1735 | | 1740 | |
| 1736 | static int select_input_picture(MpegEncContext *s) | 1741 | static int select_input_picture(MPVMainEncContext *const m) |
| 1737 | { | 1742 | { |
| | 1743 | MpegEncContext *const s = &m->s; |
| 1738 | int ret; | 1744 | int ret; |
| 1739 | | 1745 | |
| 1740 | av_assert1(!s->reordered_input_picture[0]); | 1746 | av_assert1(!s->reordered_input_picture[0]); |
| 1741 | | 1747 | |
| 1742 | for (int i = 1; i <= MAX_B_FRAMES; i++) | 1748 | for (int i = 1; i <= MPVENC_MAX_B_FRAMES; i++) |
| 1743 | s->reordered_input_picture[i - 1] = s->reordered_input_picture[i]; | 1749 | s->reordered_input_picture[i - 1] = s->reordered_input_picture[i]; |
| 1744 | s->reordered_input_picture[MAX_B_FRAMES] = NULL; | 1750 | s->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL; |
| 1745 | | 1751 | |
| 1746 | ret = set_bframe_chain_length(s); | 1752 | ret = set_bframe_chain_length(m); |
| 1747 | av_assert1(!s->input_picture[0]); | 1753 | av_assert1(!s->input_picture[0]); |
| 1748 | if (ret < 0) | 1754 | if (ret < 0) |
| 1749 | return ret; | 1755 | return ret; |
| @@ -1867,7 +1873,8 @@ static void frame_start(MpegEncContext *s) · 117 unmodified lines |
| 1867 | int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, | 1873 | int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, |
| 1868 | const AVFrame *pic_arg, int *got_packet) | 1874 | const AVFrame *pic_arg, int *got_packet) |
| 1869 | { | 1875 | { |
| 1870 | MpegEncContext *s = avctx->priv_data; | 1876 | MPVMainEncContext *const m = avctx->priv_data; |
| | 1877 | MpegEncContext *const s = &m->s; |
| 1871 | int stuffing_count, ret; | 1878 | int stuffing_count, ret; |
| 1872 | int context_count = s->slice_context_count; | 1879 | int context_count = s->slice_context_count; |
| 1873 | | 1880 | |
| @@ -1877,12 +1884,13 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, · 3 unmodified lines |
| 1877 | | 1884 | |
| 1878 | s->picture_in_gop_number++; | 1885 | s->picture_in_gop_number++; |
| 1879 | | 1886 | |
| 1880 | if (load_input_picture(s, pic_arg) < 0) | 1887 | ret = load_input_picture(m, pic_arg); |
| 1881 | return -1; | 1888 | if (ret < 0) |
| | 1889 | return ret; |
| 1882 | | 1890 | |
| 1883 | if (select_input_picture(s) < 0) { | 1891 | ret = select_input_picture(m); |
| 1884 | return -1; | 1892 | if (ret < 0) |
| 1885 | } | 1893 | return ret; |
| 1886 | | 1894 | |
| 1887 | /* output? */ | 1895 | /* output? */ |
| 1888 | if (s->new_pic->data[0]) { | 1896 | if (s->new_pic->data[0]) { |