/*
* Copyright (C) STMicroelectronics SA 2015
* Authors: Yannick Fertre <yannick.fertre@st.com>
* Hugues Fruchet <hugues.fruchet@st.com>
* License terms: GNU General Public License (GPL), version 2
*/
#include "hva.h"
#include "hva-hw.h"
#define MAX_SPS_PPS_SIZE 128
#define BITSTREAM_OFFSET_MASK 0x7F
/* video max size*/
#define H264_MAX_SIZE_W 1920
#define H264_MAX_SIZE_H 1920
/* macroBlocs number (width & height) */
#define MB_W(w) ((w + 0xF) / 0x10)
#define MB_H(h) ((h + 0xF) / 0x10)
/* formula to get temporal or spatial data size */
#define DATA_SIZE(w, h) (MB_W(w) * MB_H(h) * 16)
#define SEARCH_WINDOW_BUFFER_MAX_SIZE(w) ((4 * MB_W(w) + 42) * 256 * 3 / 2)
#define CABAC_CONTEXT_BUFFER_MAX_SIZE(w) (MB_W(w) * 16)
#define CTX_MB_BUFFER_MAX_SIZE(w) (MB_W(w) * 16 * 8)
#define SLICE_HEADER_SIZE (4 * 16)
#define BRC_DATA_SIZE (5 * 16)
/* source buffer copy in YUV 420 MB-tiled format with size=16*256*3/2 */
#define CURRENT_WINDOW_BUFFER_MAX_SIZE (16 * 256 * 3 / 2)
/*
* 4 lines of pixels (in Luma, Chroma blue and Chroma red) of top MB
* for deblocking with size=4*16*MBx*2
*/
#define LOCAL_RECONSTRUCTED_BUFFER_MAX_SIZE(w) (4 * 16 * MB_W(w) * 2)
/* factor for bitrate and cpb buffer size max values if profile >= high */
#define H264_FACTOR_HIGH 1200
/* factor for bitrate and cpb buffer size max values if profile < high */
#define H264_FACTOR_BASELINE 1000
/* number of bytes for NALU_TYPE_FILLER_DATA header and footer */
#define H264_FILLER_DATA_SIZE 6
struct h264_profile {
enum v4l2_mpeg_video_h264_level level;
u32 max_mb_per_seconds;
u32 max_frame_size;
u32 max_bitrate;
u32 max_cpb_size;
u32 min_comp_ratio;
};
static const struct h264_profile h264_infos_list[] = {
{V4L2_MPEG_VIDEO_H264_LEVEL_1_0, 1485, 99, 64, 175, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_1B, 1485, 99, 128, 350, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_1_1, 3000, 396, 192, 500, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_1_2, 6000, 396, 384, 1000, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_1_3, 11880, 396, 768, 2000, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_2_0, 11880, 396, 2000, 2000, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_2_1, 19800, 792, 4000, 4000, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_2_2, 20250, 1620, 4000, 4000, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_3_0, 40500, 1620, 10000, 10000, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_3_1, 108000, 3600, 14000, 14000, 4},
{V4L2_MPEG_VIDEO_H264_LEVEL_3_2, 216000, 5120, 20000, 20000, 4},
{V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 245760, 8192, 20000, 25000, 4},
{V4L2_MPEG_VIDEO_H264_LEVEL_4_1, 245760, 8192, 50000, 62500, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_4_2, 522240, 8704, 50000, 62500, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_5_0, 589824, 22080, 135000, 135000, 2},
{V4L2_MPEG_VIDEO_H264_LEVEL_5_1, 983040, 36864, 240000, 240000, 2}
};
enum hva_brc_type {
BRC_TYPE_NONE = 0,
BRC_TYPE_CBR = 1,
BRC_TYPE_VBR = 2,
BRC_TYPE_VBR_LOW_DELAY = 3
};
enum hva_entropy_coding_mode {
CAVLC = 0,
CABAC = 1
};
enum hva_picture_coding_type {
PICTURE_CODING_TYPE_I = 0,
PICTURE_CODING_TYPE_P = 1,
PICTURE_CODING_TYPE_B = 2
};
enum hva_h264_sampling_mode {
SAMPLING_MODE_NV12 = 0,
SAMPLING_MODE_UYVY = 1,
SAMPLING_MODE_RGB3 = 3,
SAMPLING_MODE_XRGB4 = 4,
SAMPLING_MODE_NV21 = 8,
SAMPLING_MODE_VYUY = 9,
SAMPLING_MODE_BGR3 = 11,
SAMPLING_MODE_XBGR4 = 12,
SAMPLING_MODE_RGBX4 = 20,
SAMPLING_MODE_BGRX4 = 28
};
enum hva_h264_nalu_type {
NALU_TYPE_UNKNOWN = 0,
NALU_TYPE_SLICE = 1,
NALU_TYPE_SLICE_DPA = 2,
NALU_TYPE_SLICE_DPB = 3,
NALU_TYPE_SLICE_DPC = 4,
NALU_TYPE_SLICE_IDR = 5,
NALU_TYPE_SEI = 6,
NALU_TYPE_SPS = 7,
NALU_TYPE_PPS = 8,
NALU_TYPE_AU_DELIMITER = 9,
NALU_TYPE_SEQ_END = 10,
NALU_TYPE_STREAM_END = 11,
NALU_TYPE_FILLER_DATA = 12,
NALU_TYPE_SPS_EXT = 13,
NALU_TYPE_PREFIX_UNIT = 14,
NALU_TYPE_SUBSET_SPS = 15,
NALU_TYPE_SLICE_AUX = 19,
NALU_TYPE_SLICE_EXT = 20
};
enum hva_h264_sei_payload_type {
SEI_BUFFERING_PERIOD = 0,
SEI_PICTURE_TIMING = 1,
SEI_STEREO_VIDEO_INFO = 21,
SEI_FRAME_PACKING_ARRANGEMENT = 45
};