Lines Matching +full:max +full:- +full:bitrate

1 // SPDX-License-Identifier: GPL-2.0
9 #include "hva-hw.h"
15 /* video max size*/
32 /* source buffer copy in YUV 420 MB-tiled format with size=16*256*3/2 */
41 /* factor for bitrate and cpb buffer size max values if profile >= high */
44 /* factor for bitrate and cpb buffer size max values if profile < high */
200 * @brc_type: selects the bit-rate control algorithm
205 * @non_VCL_NALU_Size: size of non-VCL NALUs (SPS, PPS, filler),
208 * @bit_rate: target bitrate, for BRC
210 * @qp_max: max QP threshold
213 * @delay: End-to-End Initial Delay
251 * Bit 0-6 used for qp offset (value -64 to 63).
277 * @brc_no_skip: Disable skipping in the Bitrate Controller
437 u32 frame_order = frame_num % ctrls->gop_size; in hva_h264_fill_slice_header()
439 if (!(frame_num % ctrls->gop_size)) in hva_h264_fill_slice_header()
456 if ((frame_num / ctrls->gop_size) % 2) { in hva_h264_fill_slice_header()
469 if (ctrls->entropy_mode == cabac) { in hva_h264_fill_slice_header()
484 pctx->name, __func__, frame_order, idr_pic_id, *header_size); in hva_h264_fill_slice_header()
495 dev_dbg(dev, "%s %s stuffing bytes %d\n", pctx->name, __func__, in hva_h264_fill_data_nal()
500 pctx->name, __func__, stuffing_bytes); in hva_h264_fill_data_nal()
556 msg = info.field_views_flag << offset--; in hva_h264_fill_sei_nal()
560 offset--; in hva_h264_fill_sei_nal()
563 offset--; in hva_h264_fill_sei_nal()
565 offset--; in hva_h264_fill_sei_nal()
567 msg |= info.left_view_self_contained_flag << offset--; in hva_h264_fill_sei_nal()
568 msg |= info.right_view_self_contained_flag << offset--; in hva_h264_fill_sei_nal()
582 pctx->name, type); in hva_h264_fill_sei_nal()
583 return -EINVAL; in hva_h264_fill_sei_nal()
594 struct hva_h264_ctx *ctx = pctx->priv; in hva_h264_prepare_task()
595 struct hva_buffer *seq_info = ctx->seq_info; in hva_h264_prepare_task()
596 struct hva_buffer *fwd_ref_frame = ctx->ref_frame; in hva_h264_prepare_task()
597 struct hva_buffer *loc_rec_frame = ctx->rec_frame; in hva_h264_prepare_task()
598 struct hva_h264_td *td = &task->td; in hva_h264_prepare_task()
599 struct hva_controls *ctrls = &pctx->ctrls; in hva_h264_prepare_task()
600 struct v4l2_fract *time_per_frame = &pctx->ctrls.time_per_frame; in hva_h264_prepare_task()
602 u32 frame_num = pctx->stream_num; in hva_h264_prepare_task()
603 u32 addr_esram = hva->esram_addr; in hva_h264_prepare_task()
607 u32 frame_width = frame->info.aligned_width; in hva_h264_prepare_task()
608 u32 frame_height = frame->info.aligned_height; in hva_h264_prepare_task()
610 unsigned int payload = stream->bytesused; in hva_h264_prepare_task()
614 if ((frame_width > max(H264_MAX_SIZE_W, H264_MAX_SIZE_H)) || in hva_h264_prepare_task()
615 (frame_height > max(H264_MAX_SIZE_W, H264_MAX_SIZE_H))) { in hva_h264_prepare_task()
618 pctx->name, frame_width, frame_height, in hva_h264_prepare_task()
620 pctx->frame_errors++; in hva_h264_prepare_task()
621 return -EINVAL; in hva_h264_prepare_task()
624 level = ctrls->level; in hva_h264_prepare_task()
628 td->frame_width = frame_width; in hva_h264_prepare_task()
629 td->frame_height = frame_height; in hva_h264_prepare_task()
632 td->window_width = frame_width; in hva_h264_prepare_task()
633 td->window_height = frame_height; in hva_h264_prepare_task()
634 td->window_horizontal_offset = 0; in hva_h264_prepare_task()
635 td->window_vertical_offset = 0; in hva_h264_prepare_task()
637 td->first_picture_in_sequence = (!frame_num) ? 1 : 0; in hva_h264_prepare_task()
640 td->pic_order_cnt_type = 2; in hva_h264_prepare_task()
643 td->use_constrained_intra_flag = false; in hva_h264_prepare_task()
644 td->brc_type = (ctrls->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) in hva_h264_prepare_task()
647 td->entropy_coding_mode = (ctrls->entropy_mode == cavlc) ? CAVLC : in hva_h264_prepare_task()
650 td->bit_rate = ctrls->bitrate; in hva_h264_prepare_task()
653 if (time_per_frame->numerator >= 536) { in hva_h264_prepare_task()
658 td->framerate_den = 1; in hva_h264_prepare_task()
659 td->framerate_num = (time_per_frame->denominator + in hva_h264_prepare_task()
660 (time_per_frame->numerator >> 1) - 1) / in hva_h264_prepare_task()
661 time_per_frame->numerator; in hva_h264_prepare_task()
664 * update bitrate to introduce a correction due to in hva_h264_prepare_task()
666 * new bitrate = (old bitrate * new framerate) / old framerate in hva_h264_prepare_task()
668 td->bit_rate /= time_per_frame->numerator; in hva_h264_prepare_task()
669 td->bit_rate *= time_per_frame->denominator; in hva_h264_prepare_task()
670 td->bit_rate /= td->framerate_num; in hva_h264_prepare_task()
672 td->framerate_den = time_per_frame->numerator; in hva_h264_prepare_task()
673 td->framerate_num = time_per_frame->denominator; in hva_h264_prepare_task()
676 /* compute maximum bitrate depending on profile */ in hva_h264_prepare_task()
677 if (ctrls->profile >= V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) in hva_h264_prepare_task()
684 /* check if bitrate doesn't exceed max size */ in hva_h264_prepare_task()
685 if (td->bit_rate > max_bitrate) { in hva_h264_prepare_task()
687 "%s bitrate (%d) larger than level and profile allow, clip to %d\n", in hva_h264_prepare_task()
688 pctx->name, td->bit_rate, max_bitrate); in hva_h264_prepare_task()
689 td->bit_rate = max_bitrate; in hva_h264_prepare_task()
693 td->cpb_buffer_size = ctrls->cpb_size * 8000; in hva_h264_prepare_task()
696 if (ctrls->profile >= V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) in hva_h264_prepare_task()
703 /* check if cpb buffer size doesn't exceed max size */ in hva_h264_prepare_task()
704 if (td->cpb_buffer_size > max_cpb_buffer_size) { in hva_h264_prepare_task()
707 pctx->name, td->cpb_buffer_size, max_cpb_buffer_size); in hva_h264_prepare_task()
708 td->cpb_buffer_size = max_cpb_buffer_size; in hva_h264_prepare_task()
711 /* enable skipping in the Bitrate Controller */ in hva_h264_prepare_task()
712 td->brc_no_skip = 0; in hva_h264_prepare_task()
715 if ((ctrls->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) && in hva_h264_prepare_task()
716 td->bit_rate) in hva_h264_prepare_task()
717 td->delay = 1000 * (td->cpb_buffer_size / td->bit_rate); in hva_h264_prepare_task()
719 td->delay = 0; in hva_h264_prepare_task()
721 switch (frame->info.pixelformat) { in hva_h264_prepare_task()
723 td->sampling_mode = SAMPLING_MODE_NV12; in hva_h264_prepare_task()
726 td->sampling_mode = SAMPLING_MODE_NV21; in hva_h264_prepare_task()
730 pctx->name); in hva_h264_prepare_task()
731 pctx->frame_errors++; in hva_h264_prepare_task()
732 return -EINVAL; in hva_h264_prepare_task()
738 * Cb = -0,1687 R -0,3313 G + 0,5 B + 128 in hva_h264_prepare_task()
739 * Cr = 0,5 R - 0,4187 G - 0,0813 B + 128 in hva_h264_prepare_task()
741 td->rgb2_yuv_y_coeff = 0x12031008; in hva_h264_prepare_task()
742 td->rgb2_yuv_u_coeff = 0x800EF7FB; in hva_h264_prepare_task()
743 td->rgb2_yuv_v_coeff = 0x80FEF40E; in hva_h264_prepare_task()
746 td->transform_mode = ctrls->dct8x8; in hva_h264_prepare_task()
749 td->encoder_complexity = 2; in hva_h264_prepare_task()
752 td->quant = 28; in hva_h264_prepare_task()
754 if (td->framerate_den == 0) { in hva_h264_prepare_task()
755 dev_err(dev, "%s invalid framerate\n", pctx->name); in hva_h264_prepare_task()
756 pctx->frame_errors++; in hva_h264_prepare_task()
757 return -EINVAL; in hva_h264_prepare_task()
760 /* if automatic framerate, deactivate bitrate controller */ in hva_h264_prepare_task()
761 if (td->framerate_num == 0) in hva_h264_prepare_task()
762 td->brc_type = 0; in hva_h264_prepare_task()
765 td->strict_hrd_compliancy = 1; in hva_h264_prepare_task()
768 td->qp_min = clamp_val(ctrls->qpmin, 0, 51); in hva_h264_prepare_task()
769 td->qp_max = clamp_val(ctrls->qpmax, 0, 51); in hva_h264_prepare_task()
771 td->addr_source_buffer = frame->paddr; in hva_h264_prepare_task()
772 td->addr_fwd_ref_buffer = fwd_ref_frame->paddr; in hva_h264_prepare_task()
773 td->addr_rec_buffer = loc_rec_frame->paddr; in hva_h264_prepare_task()
775 td->addr_output_bitstream_end = (u32)stream->paddr + stream->size; in hva_h264_prepare_task()
777 td->addr_output_bitstream_start = (u32)stream->paddr; in hva_h264_prepare_task()
778 td->bitstream_offset = (((u32)stream->paddr & 0xF) << 3) & in hva_h264_prepare_task()
781 td->addr_param_out = (u32)ctx->task->paddr + in hva_h264_prepare_task()
786 paddr = seq_info->paddr; in hva_h264_prepare_task()
787 td->addr_spatial_context = ALIGN(paddr, 0x100); in hva_h264_prepare_task()
788 paddr = seq_info->paddr + DATA_SIZE(frame_width, in hva_h264_prepare_task()
790 td->addr_temporal_context = ALIGN(paddr, 0x100); in hva_h264_prepare_task()
792 paddr = seq_info->paddr; in hva_h264_prepare_task()
793 td->addr_temporal_context = ALIGN(paddr, 0x100); in hva_h264_prepare_task()
794 paddr = seq_info->paddr + DATA_SIZE(frame_width, in hva_h264_prepare_task()
796 td->addr_spatial_context = ALIGN(paddr, 0x100); in hva_h264_prepare_task()
799 paddr = seq_info->paddr + 2 * DATA_SIZE(frame_width, frame_height); in hva_h264_prepare_task()
801 td->addr_brc_in_out_parameter = ALIGN(paddr, 0x100); in hva_h264_prepare_task()
803 paddr = td->addr_brc_in_out_parameter + BRC_DATA_SIZE; in hva_h264_prepare_task()
804 td->addr_slice_header = ALIGN(paddr, 0x100); in hva_h264_prepare_task()
805 td->addr_external_sw = ALIGN(addr_esram, 0x100); in hva_h264_prepare_task()
808 td->addr_local_rec_buffer = ALIGN(addr_esram, 0x100); in hva_h264_prepare_task()
811 td->addr_lctx = ALIGN(addr_esram, 0x100); in hva_h264_prepare_task()
813 addr_esram += CTX_MB_BUFFER_MAX_SIZE(max(frame_width, frame_height)); in hva_h264_prepare_task()
814 td->addr_cabac_context_buffer = ALIGN(addr_esram, 0x100); in hva_h264_prepare_task()
816 if (!(frame_num % ctrls->gop_size)) { in hva_h264_prepare_task()
817 td->picture_coding_type = PICTURE_CODING_TYPE_I; in hva_h264_prepare_task()
818 stream->vbuf.flags |= V4L2_BUF_FLAG_KEYFRAME; in hva_h264_prepare_task()
820 td->picture_coding_type = PICTURE_CODING_TYPE_P; in hva_h264_prepare_task()
821 stream->vbuf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; in hva_h264_prepare_task()
825 slice_header_vaddr = seq_info->vaddr + (td->addr_slice_header - in hva_h264_prepare_task()
826 seq_info->paddr); in hva_h264_prepare_task()
829 &td->slice_header_size_in_bits, in hva_h264_prepare_task()
830 &td->slice_header_offset0, in hva_h264_prepare_task()
831 &td->slice_header_offset1, in hva_h264_prepare_task()
832 &td->slice_header_offset2); in hva_h264_prepare_task()
834 td->chroma_qp_index_offset = 2; in hva_h264_prepare_task()
835 td->slice_synchro_enable = 0; in hva_h264_prepare_task()
836 td->max_slice_number = 1; in hva_h264_prepare_task()
843 if ((stream->vbuf.flags == V4L2_BUF_FLAG_KEYFRAME) && in hva_h264_prepare_task()
845 dev_err(dev, "%s invalid sps/pps size %d\n", pctx->name, in hva_h264_prepare_task()
847 pctx->frame_errors++; in hva_h264_prepare_task()
848 return -EINVAL; in hva_h264_prepare_task()
851 if (stream->vbuf.flags != V4L2_BUF_FLAG_KEYFRAME) in hva_h264_prepare_task()
855 if (ctrls->sei_fp && hva_h264_fill_sei_nal(pctx, SEI_STEREO_VIDEO_INFO, in hva_h264_prepare_task()
856 (u8 *)stream->vaddr, in hva_h264_prepare_task()
858 dev_err(dev, "%s fail to get SEI nal\n", pctx->name); in hva_h264_prepare_task()
859 pctx->frame_errors++; in hva_h264_prepare_task()
860 return -EINVAL; in hva_h264_prepare_task()
863 /* fill size of non-VCL NAL units (SPS, PPS, filler and SEI) */ in hva_h264_prepare_task()
864 td->non_vcl_nalu_size = payload * 8; in hva_h264_prepare_task()
867 td->addr_output_bitstream_start += ((payload >> 4) << 4); in hva_h264_prepare_task()
868 td->bitstream_offset += (payload - ((payload >> 4) << 4)) * 8; in hva_h264_prepare_task()
870 stream->bytesused = payload; in hva_h264_prepare_task()
877 struct hva_h264_po *po = &task->po; in hva_h264_get_stream_size()
879 return po->bitstream_size; in hva_h264_get_stream_size()
884 struct hva_h264_po *po = &task->po; in hva_h264_get_stuffing_bytes()
886 return po->stuffing_bits >> 3; in hva_h264_get_stuffing_bytes()
894 u32 frame_width = pctx->frameinfo.aligned_width; in hva_h264_open()
895 u32 frame_height = pctx->frameinfo.aligned_height; in hva_h264_open()
902 CTX_MB_BUFFER_MAX_SIZE(max(frame_width, frame_height)) + in hva_h264_open()
905 if (hva->esram_size < size) { in hva_h264_open()
906 dev_err(dev, "%s not enough esram (max:%d request:%d)\n", in hva_h264_open()
907 pctx->name, hva->esram_size, size); in hva_h264_open()
908 ret = -EINVAL; in hva_h264_open()
915 ret = -ENOMEM; in hva_h264_open()
925 &ctx->seq_info); in hva_h264_open()
929 pctx->name); in hva_h264_open()
937 &ctx->ref_frame); in hva_h264_open()
940 pctx->name); in hva_h264_open()
948 &ctx->rec_frame); in hva_h264_open()
952 pctx->name); in hva_h264_open()
960 &ctx->task); in hva_h264_open()
964 pctx->name); in hva_h264_open()
968 pctx->priv = (void *)ctx; in hva_h264_open()
973 hva_mem_free(pctx, ctx->rec_frame); in hva_h264_open()
975 hva_mem_free(pctx, ctx->ref_frame); in hva_h264_open()
977 hva_mem_free(pctx, ctx->seq_info); in hva_h264_open()
981 pctx->sys_errors++; in hva_h264_open()
987 struct hva_h264_ctx *ctx = pctx->priv; in hva_h264_close()
990 if (ctx->seq_info) in hva_h264_close()
991 hva_mem_free(pctx, ctx->seq_info); in hva_h264_close()
993 if (ctx->ref_frame) in hva_h264_close()
994 hva_mem_free(pctx, ctx->ref_frame); in hva_h264_close()
996 if (ctx->rec_frame) in hva_h264_close()
997 hva_mem_free(pctx, ctx->rec_frame); in hva_h264_close()
999 if (ctx->task) in hva_h264_close()
1000 hva_mem_free(pctx, ctx->task); in hva_h264_close()
1010 struct hva_h264_ctx *ctx = pctx->priv; in hva_h264_encode()
1011 struct hva_h264_task *task = ctx->task->vaddr; in hva_h264_encode()
1019 ret = hva_hw_execute_task(pctx, H264_ENC, ctx->task); in hva_h264_encode()
1023 pctx->stream_num++; in hva_h264_encode()
1024 stream->bytesused += hva_h264_get_stream_size(task); in hva_h264_encode()
1030 (u8 *)stream->vaddr, in hva_h264_encode()
1031 stream->size, in hva_h264_encode()
1032 &stream->bytesused); in hva_h264_encode()
1035 swap(ctx->ref_frame, ctx->rec_frame); in hva_h264_encode()
1039 stream->bytesused = 0; in hva_h264_encode()