2012-06-05 11:01:47 +00:00
|
|
|
/**
|
|
|
|
* HEVC Encoder
|
|
|
|
* - Marko Viitanen ( fador at iki.fi ), Tampere University of Technology, Department of Computer Systems.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file encoder.c
|
|
|
|
\brief Encoding related functions
|
|
|
|
\author Marko Viitanen
|
|
|
|
\date 2012-06
|
|
|
|
|
|
|
|
Encoder main level
|
|
|
|
*/
|
|
|
|
/* Suppress some windows warnings */
|
|
|
|
#ifdef WIN32
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "global.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "encoder.h"
|
|
|
|
#include "cabac.h"
|
|
|
|
#include "picture.h"
|
2012-06-05 12:38:54 +00:00
|
|
|
#include "nal.h"
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2012-06-07 14:38:28 +00:00
|
|
|
void init_encoder_control(encoder_control* control,bitstream* output)
|
|
|
|
{
|
|
|
|
control->stream = output;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:01:47 +00:00
|
|
|
void init_encoder_input(encoder_input* input,FILE* inputfile, uint32_t width, uint32_t height)
|
|
|
|
{
|
2012-06-11 12:26:32 +00:00
|
|
|
int i;
|
2012-06-05 11:01:47 +00:00
|
|
|
input->file = inputfile;
|
|
|
|
input->width = width;
|
|
|
|
input->height = height;
|
|
|
|
|
2012-06-06 13:20:29 +00:00
|
|
|
input->height_in_LCU = height / LCU_WIDTH;
|
|
|
|
input->width_in_LCU = width / LCU_WIDTH;
|
|
|
|
if(input->height_in_LCU * LCU_WIDTH < height)
|
|
|
|
input->height_in_LCU++;
|
|
|
|
if(input->width_in_LCU * LCU_WIDTH < width)
|
|
|
|
input->width_in_LCU++;
|
|
|
|
|
2012-06-05 11:01:47 +00:00
|
|
|
input->cur_pic.width = width;
|
|
|
|
input->cur_pic.height = height;
|
|
|
|
input->cur_pic.referenced = 0;
|
|
|
|
/* Allocate buffers */
|
|
|
|
input->cur_pic.yData = (uint8_t *)malloc(width*height);
|
|
|
|
input->cur_pic.uData = (uint8_t *)malloc((width*height)>>2);
|
|
|
|
input->cur_pic.vData = (uint8_t *)malloc((width*height)>>2);
|
2012-06-11 12:26:32 +00:00
|
|
|
|
2012-06-11 12:39:18 +00:00
|
|
|
/* Allocate memory for CU info 2D array */
|
|
|
|
//ToDo: we don't need this much space on LCU...MAX_DEPTH-1
|
|
|
|
input->cur_pic.CU = (CU_info**)malloc((MAX_DEPTH+1)*sizeof(CU_info*));
|
2012-06-11 12:26:32 +00:00
|
|
|
for(i=0; i < MAX_DEPTH+1; i++)
|
|
|
|
{
|
|
|
|
input->cur_pic.CU[i] = (CU_info*)malloc((input->height_in_LCU<<2)*(input->width_in_LCU<<2)*sizeof(CU_info));
|
|
|
|
memset(input->cur_pic.CU[i], 0, (input->height_in_LCU<<2)*(input->width_in_LCU<<2)*sizeof(CU_info));
|
|
|
|
}
|
2012-06-07 14:38:28 +00:00
|
|
|
}
|
2012-06-05 11:01:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
void encode_one_frame(encoder_control* encoder)
|
|
|
|
{
|
2012-06-11 12:26:32 +00:00
|
|
|
/* output parameters before first frame */
|
2012-06-05 12:38:54 +00:00
|
|
|
if(encoder->frame == 0)
|
|
|
|
{
|
|
|
|
encode_seq_parameter_set(encoder);
|
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
2012-06-11 12:26:32 +00:00
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 1, NAL_SEQ_PARAMETER_SET, 1);
|
2012-06-06 10:42:02 +00:00
|
|
|
bitstream_clear_buffer(encoder->stream);
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2012-06-05 12:38:54 +00:00
|
|
|
encode_pic_parameter_set(encoder);
|
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 1, NAL_PIC_PARAMETER_SET, 0);
|
2012-06-06 11:09:28 +00:00
|
|
|
bitstream_clear_buffer(encoder->stream);
|
2012-06-05 14:45:17 +00:00
|
|
|
|
2012-06-11 12:26:32 +00:00
|
|
|
/* First slice is IDR */
|
2012-06-07 14:38:28 +00:00
|
|
|
cabac_start(&cabac);
|
2012-06-11 12:26:32 +00:00
|
|
|
encoder->in.cur_pic.type = NAL_IDR_SLICE;
|
2012-06-06 11:09:28 +00:00
|
|
|
encode_slice_header(encoder);
|
2012-06-11 12:26:32 +00:00
|
|
|
bitstream_align(encoder->stream);
|
2012-06-06 11:09:28 +00:00
|
|
|
encode_slice_data(encoder);
|
2012-06-06 13:20:29 +00:00
|
|
|
cabac_flush(&cabac);
|
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_IDR_SLICE, 0);
|
|
|
|
bitstream_clear_buffer(encoder->stream);
|
|
|
|
}
|
2012-06-11 12:26:32 +00:00
|
|
|
else if(encoder->frame < 3)
|
2012-06-06 13:20:29 +00:00
|
|
|
{
|
2012-06-11 12:26:32 +00:00
|
|
|
cabac_start(&cabac);
|
|
|
|
encoder->in.cur_pic.type = NAL_NONIDR_SLICE;
|
2012-06-06 13:20:29 +00:00
|
|
|
encode_slice_header(encoder);
|
2012-06-11 12:26:32 +00:00
|
|
|
bitstream_align(encoder->stream);
|
2012-06-06 13:20:29 +00:00
|
|
|
encode_slice_data(encoder);
|
|
|
|
cabac_flush(&cabac);
|
2012-06-06 11:09:28 +00:00
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
2012-06-11 12:26:32 +00:00
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_NONIDR_SLICE, 0);
|
2012-06-06 11:09:28 +00:00
|
|
|
bitstream_clear_buffer(encoder->stream);
|
2012-06-11 12:26:32 +00:00
|
|
|
}
|
2012-06-05 12:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void encode_pic_parameter_set(encoder_control* encoder)
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("=========== Picture Parameter Set ID: 0 ===========\n");
|
|
|
|
#endif
|
|
|
|
WRITE_UE(encoder->stream, 0, "pic_parameter_set_id");
|
|
|
|
WRITE_UE(encoder->stream, 0, "seq_parameter_set_id");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "sign_data_hiding_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "cabac_init_present_flag");
|
2012-06-06 10:42:02 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 3, "num_ref_idx_l0_default_active_minus1");
|
|
|
|
WRITE_U(encoder->stream, 0, 3, "num_ref_idx_l1_default_active_minus1");
|
|
|
|
/*
|
2012-06-06 11:09:28 +00:00
|
|
|
//Should be this
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "num_ref_idx_l0_default_active_minus1");
|
|
|
|
WRITE_UE(encoder->stream, 0, "num_ref_idx_l1_default_active_minus1");
|
2012-06-06 10:42:02 +00:00
|
|
|
*/
|
2012-06-07 14:38:28 +00:00
|
|
|
WRITE_SE(encoder->stream, encoder->QP-26, "pic_init_qp_minus26");
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "constrained_intra_pred_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "enable_temporal_mvp_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 2, "slice_granularity");
|
|
|
|
WRITE_UE(encoder->stream, 0, "max_cu_qp_delta_depth");
|
|
|
|
WRITE_SE(encoder->stream, 0, "cb_qp_offset");
|
|
|
|
WRITE_SE(encoder->stream, 0, "cr_qp_offset");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "weighted_pred_flag");
|
2012-06-06 11:09:28 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 2, "weighted_bipred_idc");
|
|
|
|
WRITE_U(encoder->stream, 1, 1, "output_flag_present_flag");
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "deblocking_filter_control_present_flag");
|
|
|
|
WRITE_UE(encoder->stream, 0, "log2_parallel_merge_level_minus2");
|
2012-06-06 11:09:28 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "pps_extension_flag");
|
2012-06-05 12:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void encode_seq_parameter_set(encoder_control* encoder)
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("=========== Sequence Parameter Set ID: 0 ===========\n");
|
|
|
|
#endif
|
|
|
|
WRITE_U(encoder->stream, 0, 8, "profile_idc");
|
|
|
|
WRITE_U(encoder->stream, 0, 8, "reserved_zero_8bits");
|
|
|
|
WRITE_U(encoder->stream, 0, 8, "level_idc");
|
|
|
|
WRITE_UE(encoder->stream, 0, "seq_parameter_set_id");
|
2012-06-08 12:26:07 +00:00
|
|
|
WRITE_UE(encoder->stream, 1, "chroma_format_idc"); /* 0 = 4:0:0, 1 = 4:2:0, 2 = 4:2:2, 3 = 4:4:4 */
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 3, "max_temporal_layers_minus1");
|
|
|
|
WRITE_UE(encoder->stream, encoder->in.width, "pic_width_in_luma_samples");
|
|
|
|
WRITE_UE(encoder->stream, encoder->in.height, "pic_height_in_luma_samples");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "pic_cropping_flag");
|
|
|
|
WRITE_UE(encoder->stream, 0, "bit_depth_luma_minus8");
|
|
|
|
WRITE_UE(encoder->stream, 0, "bit_depth_chroma_minus8");
|
2012-06-11 12:39:18 +00:00
|
|
|
WRITE_U(encoder->stream, ENABLE_PCM, 1, "pcm_enabled_flag");
|
|
|
|
#if ENABLE_PCM == 1
|
2012-06-06 13:20:29 +00:00
|
|
|
WRITE_U(encoder->stream, 7, 4, "pcm_bit_depth_luma_minus1");
|
|
|
|
WRITE_U(encoder->stream, 7, 4, "pcm_bit_depth_chroma_minus1");
|
2012-06-11 12:39:18 +00:00
|
|
|
#endif
|
2012-06-06 10:42:02 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "qpprime_y_zero_transquant_bypass_flag");
|
2012-06-07 14:38:28 +00:00
|
|
|
WRITE_UE(encoder->stream, 4, "log2_max_pic_order_cnt_lsb_minus4");
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "max_dec_pic_buffering");
|
|
|
|
WRITE_UE(encoder->stream, 0, "num_reorder_pics");
|
|
|
|
WRITE_UE(encoder->stream, 0, "max_latency_increase");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "restricted_ref_pic_lists_flag");
|
2012-06-07 14:38:28 +00:00
|
|
|
WRITE_UE(encoder->stream, 1, "log2_min_coding_block_size_minus3");
|
|
|
|
WRITE_UE(encoder->stream, 2, "log2_diff_max_min_coding_block_size");
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "log2_min_transform_block_size_minus2");
|
|
|
|
WRITE_UE(encoder->stream, 3, "log2_diff_max_min_transform_block_size");
|
|
|
|
|
2012-06-06 11:09:28 +00:00
|
|
|
//If log2MinCUSize == 3
|
2012-06-07 14:38:28 +00:00
|
|
|
//WRITE_U(encoder->stream, 0, 1, "DisInter4x4");
|
2012-06-06 11:09:28 +00:00
|
|
|
|
2012-06-11 12:39:18 +00:00
|
|
|
#if ENABLE_PCM == 1
|
2012-06-06 11:09:28 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "log2_min_pcm_coding_block_size_minus3");
|
2012-06-07 14:38:28 +00:00
|
|
|
WRITE_UE(encoder->stream, 2, "log2_diff_max_min_pcm_coding_block_size");
|
2012-06-11 12:39:18 +00:00
|
|
|
#endif
|
2012-06-06 13:20:29 +00:00
|
|
|
|
2012-06-06 10:42:02 +00:00
|
|
|
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_UE(encoder->stream, 2, "max_transform_hierarchy_depth_inter");
|
2012-06-06 13:20:29 +00:00
|
|
|
WRITE_UE(encoder->stream, 2, "max_transform_hierarchy_depth_intra");
|
|
|
|
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "scaling_list_enable_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "chroma_pred_from_luma_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "transform_skip_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "deblocking_filter_in_aps_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "seq_loop_filter_across_slices_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "asymmetric_motion_partitions_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "nsrqt_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "sample_adaptive_offset_enabled_flag");
|
2012-06-06 13:20:29 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "adaptive_loop_filter_enabled_flag");
|
2012-06-11 12:39:18 +00:00
|
|
|
#if ENABLE_PCM == 1
|
2012-06-07 14:38:28 +00:00
|
|
|
WRITE_U(encoder->stream, 1, 1, "pcm_loop_filter_disable_flag");
|
2012-06-11 12:39:18 +00:00
|
|
|
#endif
|
2012-06-11 12:26:32 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "temporal_id_nesting_flag");
|
|
|
|
WRITE_UE(encoder->stream, 0, "num_short_term_ref_pic_sets");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "long_term_ref_pics_present_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 2, "tiles_or_entropy_coding_sync_idc");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "sps_extension_flag");
|
|
|
|
//WRITE_U(encoder->stream, 0, 8, "stuffing");
|
2012-06-06 11:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void encode_slice_header(encoder_control* encoder)
|
|
|
|
{
|
2012-06-06 13:20:29 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("=========== Slice ===========\n");
|
|
|
|
#endif
|
2012-06-06 11:09:28 +00:00
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 1, 1, "first_slice_in_pic_flag");
|
|
|
|
WRITE_UE(encoder->stream, SLICE_I, "slice_type");
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "entropy_slice_flag");
|
|
|
|
// if !entropy_slice_flag
|
|
|
|
WRITE_UE(encoder->stream, 0, "pic_parameter_set_id");
|
|
|
|
//if output_flag_present_flag
|
|
|
|
WRITE_U(encoder->stream, 1, 1, "pic_output_flag");
|
|
|
|
//end if
|
|
|
|
//if( IdrPicFlag ) <- nal_unit_type == 5
|
2012-06-11 12:26:32 +00:00
|
|
|
if(encoder->in.cur_pic.type == NAL_IDR_SLICE)
|
|
|
|
{
|
|
|
|
WRITE_UE(encoder->stream, encoder->frame&3, "idr_pic_id");
|
2012-06-06 11:09:28 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "no_output_of_prior_pics_flag");
|
2012-06-11 12:26:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WRITE_U(encoder->stream, encoder->frame, 8, "pic_order_cnt_lsb");
|
|
|
|
WRITE_U(encoder->stream, 1, 1, "short_term_ref_pic_set_sps_flag");
|
|
|
|
WRITE_UE(encoder->stream, 0, "short_term_ref_pic_set_idx");
|
2012-06-06 11:09:28 +00:00
|
|
|
|
2012-06-11 12:26:32 +00:00
|
|
|
}
|
2012-06-06 11:09:28 +00:00
|
|
|
//end if
|
|
|
|
//end if
|
|
|
|
/*
|
|
|
|
Skip unpresent flags */
|
|
|
|
// if !entropy_slice_flag
|
2012-06-06 13:20:29 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "slice_qp_delta");
|
|
|
|
WRITE_UE(encoder->stream, 0, "5_minus_max_num_merge_cand");
|
2012-06-05 12:38:54 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:24 +00:00
|
|
|
cabac_ctx *SplitFlagSCModel;
|
|
|
|
cabac_ctx g_SplitFlagSCModel[3];
|
2012-06-07 14:38:28 +00:00
|
|
|
cabac_ctx PCMFlagSCModel;
|
|
|
|
cabac_ctx PartSizeSCModel;
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2012-06-06 11:09:28 +00:00
|
|
|
void encode_slice_data(encoder_control* encoder)
|
|
|
|
{
|
|
|
|
uint16_t xCtb,yCtb;
|
2012-06-11 12:26:32 +00:00
|
|
|
/* Initialize contexts */
|
|
|
|
cxt_init(&g_SplitFlagSCModel[0], encoder->QP, INIT_SPLIT_FLAG[SLICE_I][0]);
|
|
|
|
cxt_init(&g_SplitFlagSCModel[1], encoder->QP, INIT_SPLIT_FLAG[SLICE_I][1]);
|
|
|
|
cxt_init(&g_SplitFlagSCModel[2], encoder->QP, INIT_SPLIT_FLAG[SLICE_I][2]);
|
|
|
|
|
2012-06-08 12:26:07 +00:00
|
|
|
cxt_init(&PartSizeSCModel, encoder->QP, 154);
|
2012-06-11 12:26:32 +00:00
|
|
|
//g_SplitFlagSCModel[1].ucState = 47;
|
|
|
|
//g_SplitFlagSCModel[2].ucState = 36;
|
2012-06-08 13:55:24 +00:00
|
|
|
|
2012-06-11 12:26:32 +00:00
|
|
|
for(yCtb = 0; yCtb < encoder->in.height_in_LCU; yCtb++)
|
2012-06-06 13:20:29 +00:00
|
|
|
{
|
2012-06-11 12:26:32 +00:00
|
|
|
uint8_t lastCUy = (yCtb == (encoder->in.height_in_LCU-1))?1:0;
|
|
|
|
for(xCtb = 0; xCtb < encoder->in.width_in_LCU; xCtb++)
|
2012-06-06 13:20:29 +00:00
|
|
|
{
|
2012-06-11 12:26:32 +00:00
|
|
|
uint8_t lastCUx = (xCtb == (encoder->in.width_in_LCU-1))?1:0;
|
2012-06-06 13:20:29 +00:00
|
|
|
uint8_t depth = 0;
|
2012-06-08 13:55:24 +00:00
|
|
|
|
2012-06-11 12:26:32 +00:00
|
|
|
encode_coding_tree(encoder, xCtb<<2,yCtb<<2, depth);
|
2012-06-08 13:55:24 +00:00
|
|
|
//Terminating bit
|
|
|
|
if(!lastCUx || !lastCUy)
|
|
|
|
{
|
|
|
|
cabac_encodeBinTrm(&cabac, 0);
|
|
|
|
}
|
2012-06-11 12:26:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cabac_encodeBinTrm(&cabac, 1);
|
|
|
|
}
|
2012-06-06 13:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, uint8_t depth)
|
2012-06-07 14:38:28 +00:00
|
|
|
{
|
2012-06-08 12:36:56 +00:00
|
|
|
int i,x,y;
|
2012-06-07 14:38:28 +00:00
|
|
|
uint8_t split_flag = (depth!=1)?1:0;
|
2012-06-11 12:26:32 +00:00
|
|
|
uint8_t split_model = 0;
|
2012-06-11 12:39:18 +00:00
|
|
|
/* Get left and top block split_flags and if they are present and true, increase model number */
|
|
|
|
if(xCtb > 0 && GET_SPLITDATA(&(encoder->in.cur_pic.CU[depth][(xCtb>>(MAX_DEPTH-depth))-1+(yCtb>>(MAX_DEPTH-depth))*(encoder->in.width_in_LCU<<MAX_DEPTH)])) == 1)
|
2012-06-11 12:26:32 +00:00
|
|
|
{
|
|
|
|
split_model++;
|
|
|
|
}
|
2012-06-11 12:39:18 +00:00
|
|
|
if(yCtb > 0 && GET_SPLITDATA(&(encoder->in.cur_pic.CU[depth][(xCtb>>(MAX_DEPTH-depth))+((yCtb>>(MAX_DEPTH-depth))-1)*(encoder->in.width_in_LCU<<MAX_DEPTH)])) == 1)
|
2012-06-11 12:26:32 +00:00
|
|
|
{
|
|
|
|
split_model++;
|
|
|
|
}
|
|
|
|
cabac.ctx = &g_SplitFlagSCModel[split_model];
|
2012-06-08 13:55:24 +00:00
|
|
|
|
2012-06-11 12:39:18 +00:00
|
|
|
/* When not in MAX_DEPTH, insert split flag and split the blocks if needed */
|
2012-06-11 12:26:32 +00:00
|
|
|
if(depth != MAX_DEPTH)
|
2012-06-07 14:38:28 +00:00
|
|
|
{
|
2012-06-11 12:39:18 +00:00
|
|
|
SET_SPLITDATA(&(encoder->in.cur_pic.CU[depth][(xCtb>>(MAX_DEPTH-depth))+(yCtb>>(MAX_DEPTH-depth))*(encoder->in.width_in_LCU<<MAX_DEPTH)]),split_flag);
|
2012-06-08 12:26:07 +00:00
|
|
|
CABAC_BIN(&cabac, split_flag, "SplitFlag");
|
|
|
|
if(split_flag)
|
|
|
|
{
|
2012-06-11 12:26:32 +00:00
|
|
|
uint8_t change = 1<<(MAX_DEPTH-1-depth);
|
2012-06-08 12:26:07 +00:00
|
|
|
encode_coding_tree(encoder,xCtb,yCtb,depth+1);
|
2012-06-08 12:36:56 +00:00
|
|
|
encode_coding_tree(encoder,xCtb+change,yCtb,depth+1);
|
|
|
|
encode_coding_tree(encoder,xCtb,yCtb+change,depth+1);
|
|
|
|
encode_coding_tree(encoder,xCtb+change,yCtb+change,depth+1);
|
2012-06-08 12:26:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-06-07 14:38:28 +00:00
|
|
|
}
|
2012-06-06 13:20:29 +00:00
|
|
|
/* coding_unit( x0, y0, log2CbSize ) */
|
|
|
|
/* prediction_unit 2Nx2N*/
|
2012-06-07 14:38:28 +00:00
|
|
|
//if !intra PREDMODE
|
2012-06-08 12:26:07 +00:00
|
|
|
/* if depth = MAX_DEPTH */
|
|
|
|
//PartSize
|
|
|
|
if(depth == 2)
|
|
|
|
{
|
|
|
|
cabac.ctx = &PartSizeSCModel;
|
|
|
|
CABAC_BIN(&cabac, 1, "PartSize");
|
|
|
|
}
|
|
|
|
/*end partsize*/
|
2012-06-06 13:20:29 +00:00
|
|
|
//If MODE_INTRA
|
2012-06-08 12:26:07 +00:00
|
|
|
//cabac.ctx = &PCMFlagSCModel;
|
2012-06-07 14:38:28 +00:00
|
|
|
cabac_encodeBinTrm(&cabac, 1);
|
|
|
|
printf("\tIPCMFlag = 1\n");
|
|
|
|
cabac_finish(&cabac);
|
|
|
|
WRITE_U(cabac.stream, 1, 1, "stop_bit");
|
2012-06-11 12:26:32 +00:00
|
|
|
WRITE_U(cabac.stream, 0, 1, "numSubseqIPCM_flag");
|
2012-06-07 14:38:28 +00:00
|
|
|
bitstream_align(cabac.stream);
|
|
|
|
/* PCM sample */
|
2012-06-08 12:36:56 +00:00
|
|
|
{
|
|
|
|
uint8_t *base = &encoder->in.cur_pic.yData[xCtb*16 + (yCtb*16)*encoder->in.width];
|
2012-06-08 13:55:24 +00:00
|
|
|
uint8_t *baseCb = &encoder->in.cur_pic.uData[(xCtb*8 + (yCtb*8)*encoder->in.width/2)];
|
|
|
|
uint8_t *baseCr = &encoder->in.cur_pic.vData[(xCtb*8 + (yCtb*8)*encoder->in.width/2)];
|
2012-06-08 12:36:56 +00:00
|
|
|
for(y = 0; y < 32; y++)
|
2012-06-08 12:26:07 +00:00
|
|
|
{
|
2012-06-08 12:36:56 +00:00
|
|
|
for(x = 0; x < 32; x++)
|
|
|
|
{
|
|
|
|
bitstream_put(cabac.stream, base[x+y*encoder->in.width], 8);
|
|
|
|
}
|
2012-06-08 12:26:07 +00:00
|
|
|
}
|
|
|
|
//Cb
|
2012-06-08 13:55:24 +00:00
|
|
|
for(y = 0; y < 16; y++)
|
2012-06-07 14:38:28 +00:00
|
|
|
{
|
2012-06-08 13:55:24 +00:00
|
|
|
for(x = 0; x < 16; x++)
|
|
|
|
{
|
|
|
|
bitstream_put(cabac.stream, baseCb[x+y*(encoder->in.width>>1)], 8);
|
|
|
|
}
|
2012-06-07 14:38:28 +00:00
|
|
|
}
|
2012-06-08 12:26:07 +00:00
|
|
|
|
|
|
|
//Cr
|
2012-06-08 13:55:24 +00:00
|
|
|
for(y = 0; y < 16; y++)
|
2012-06-08 12:26:07 +00:00
|
|
|
{
|
2012-06-08 13:55:24 +00:00
|
|
|
for(x = 0; x < 16; x++)
|
|
|
|
{
|
|
|
|
bitstream_put(cabac.stream, baseCr[x+y*(encoder->in.width>>1)], 8);
|
|
|
|
}
|
2012-06-08 12:26:07 +00:00
|
|
|
}
|
2012-06-08 12:36:56 +00:00
|
|
|
}
|
2012-06-08 12:26:07 +00:00
|
|
|
/* end PCM sample */
|
|
|
|
cabac_start(&cabac);
|
2012-06-06 13:20:29 +00:00
|
|
|
//endif
|
|
|
|
|
|
|
|
/* end prediction unit */
|
2012-06-07 14:38:28 +00:00
|
|
|
//cabac_encodeBin(&cabac, 0); //prev_intra_luma_pred_flag
|
2012-06-06 13:20:29 +00:00
|
|
|
|
2012-06-07 14:38:28 +00:00
|
|
|
//cabac_encodeBin(&cabac, 1); //rem_intra_luma_pred_mode
|
2012-06-06 13:20:29 +00:00
|
|
|
|
|
|
|
/* end coding_unit */
|
|
|
|
|
2012-06-06 11:09:28 +00:00
|
|
|
}
|
|
|
|
|