2012-06-05 11:01:47 +00:00
|
|
|
/**
|
2013-09-18 14:29:30 +00:00
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* \author Marko Viitanen ( fador@iki.fi ),
|
|
|
|
* Tampere University of Technology,
|
|
|
|
* Department of Pervasive Computing.
|
|
|
|
* \author Ari Koivula ( ari@koivu.la ),
|
|
|
|
* Tampere University of Technology,
|
|
|
|
* Department of Pervasive Computing.
|
2012-06-05 11:01:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
|
#endif
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "encoder.h"
|
|
|
|
|
2013-04-16 12:10:43 +00:00
|
|
|
#include <math.h>
|
2012-06-05 11:01:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-09-09 15:32:16 +00:00
|
|
|
#include <assert.h>
|
2013-09-18 09:16:03 +00:00
|
|
|
|
2012-06-05 11:01:47 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "cabac.h"
|
|
|
|
#include "picture.h"
|
2012-06-05 12:38:54 +00:00
|
|
|
#include "nal.h"
|
2012-08-15 10:22:31 +00:00
|
|
|
#include "context.h"
|
2013-02-05 13:48:06 +00:00
|
|
|
#include "transform.h"
|
2013-03-07 15:42:00 +00:00
|
|
|
#include "intra.h"
|
2013-09-12 15:50:11 +00:00
|
|
|
#include "inter.h"
|
2013-04-04 12:08:28 +00:00
|
|
|
#include "filter.h"
|
2013-04-16 12:10:43 +00:00
|
|
|
#include "search.h"
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-04-17 14:08:52 +00:00
|
|
|
int16_t g_lambda_cost[55];
|
2013-09-18 11:49:01 +00:00
|
|
|
uint32_t* g_sig_last_scan[3][7];
|
2013-04-17 14:08:52 +00:00
|
|
|
|
2013-09-05 12:59:51 +00:00
|
|
|
/* Local functions. */
|
|
|
|
static void add_checksum(encoder_control* encoder);
|
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
void initSigLastScan(uint32_t* pBuffD, uint32_t* pBuffH, uint32_t* pBuffV, int32_t iWidth, int32_t iHeight)
|
2012-08-15 14:18:58 +00:00
|
|
|
{
|
|
|
|
uint32_t uiNumScanPos = iWidth * iWidth;
|
|
|
|
uint32_t uiNextScanPos = 0;
|
|
|
|
int32_t iX,iY,x,y;
|
|
|
|
uint32_t uiScanLine;
|
|
|
|
uint32_t blkY,blkX;
|
|
|
|
uint32_t uiBlk;
|
|
|
|
uint32_t uiCnt = 0;
|
|
|
|
|
|
|
|
if( iWidth < 16 )
|
|
|
|
{
|
|
|
|
uint32_t* pBuffTemp = pBuffD;
|
|
|
|
if( iWidth == 8 )
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
pBuffTemp = (uint32_t *)g_sig_last_scan_32x32;
|
2012-08-15 14:18:58 +00:00
|
|
|
}
|
|
|
|
for( uiScanLine = 0; uiNextScanPos < uiNumScanPos; uiScanLine++ )
|
|
|
|
{
|
|
|
|
int iPrimDim = uiScanLine;
|
|
|
|
int iScndDim = 0;
|
|
|
|
while( iPrimDim >= iWidth )
|
|
|
|
{
|
|
|
|
iScndDim++;
|
|
|
|
iPrimDim--;
|
|
|
|
}
|
|
|
|
while( iPrimDim >= 0 && iScndDim < iWidth )
|
|
|
|
{
|
|
|
|
pBuffTemp[ uiNextScanPos ] = iPrimDim * iWidth + iScndDim ;
|
|
|
|
uiNextScanPos++;
|
|
|
|
iScndDim++;
|
|
|
|
iPrimDim--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( iWidth > 4 )
|
|
|
|
{
|
|
|
|
uint32_t uiNumBlkSide = iWidth >> 2;
|
|
|
|
uint32_t uiNumBlks = uiNumBlkSide * uiNumBlkSide;
|
2013-09-18 11:49:01 +00:00
|
|
|
uint32_t log2Blk = g_convert_to_bit[ uiNumBlkSide ] + 1;
|
2012-08-15 14:18:58 +00:00
|
|
|
|
|
|
|
for(uiBlk = 0; uiBlk < uiNumBlks; uiBlk++ )
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
uint32_t initBlkPos = g_sig_last_scan[ SCAN_DIAG ][ log2Blk ][ uiBlk ];
|
2012-08-15 14:18:58 +00:00
|
|
|
uiNextScanPos = 0;
|
|
|
|
if( iWidth == 32 )
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
initBlkPos = g_sig_last_scan_32x32[ uiBlk ];
|
2012-08-15 14:18:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
uint32_t offsetY = initBlkPos / uiNumBlkSide;
|
|
|
|
uint32_t offsetX = initBlkPos - offsetY * uiNumBlkSide;
|
|
|
|
uint32_t offsetD = 4 * ( offsetX + offsetY * iWidth );
|
|
|
|
uint32_t offsetScan = 16 * uiBlk;
|
|
|
|
for( uiScanLine = 0; uiNextScanPos < 16; uiScanLine++ )
|
|
|
|
{
|
|
|
|
int iPrimDim = uiScanLine;
|
|
|
|
int iScndDim = 0;
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: optimize
|
2012-08-15 14:18:58 +00:00
|
|
|
while( iPrimDim >= 4 )
|
|
|
|
{
|
|
|
|
iScndDim++;
|
|
|
|
iPrimDim--;
|
|
|
|
}
|
|
|
|
while( iPrimDim >= 0 && iScndDim < 4 )
|
|
|
|
{
|
|
|
|
pBuffD[ uiNextScanPos + offsetScan ] = iPrimDim * iWidth + iScndDim + offsetD;
|
|
|
|
uiNextScanPos++;
|
|
|
|
iScndDim++;
|
|
|
|
iPrimDim--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( iWidth > 2 )
|
|
|
|
{
|
|
|
|
uint32_t numBlkSide = iWidth >> 2;
|
|
|
|
for(blkY=0; blkY < numBlkSide; blkY++)
|
|
|
|
{
|
|
|
|
for(blkX=0; blkX < numBlkSide; blkX++)
|
|
|
|
{
|
|
|
|
uint32_t offset = blkY * 4 * iWidth + blkX * 4;
|
|
|
|
for(y=0; y < 4; y++)
|
|
|
|
{
|
|
|
|
for(x=0; x < 4; x++)
|
|
|
|
{
|
|
|
|
pBuffH[uiCnt] = y*iWidth + x + offset;
|
|
|
|
uiCnt ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uiCnt = 0;
|
|
|
|
for(blkX=0; blkX < numBlkSide; blkX++)
|
|
|
|
{
|
|
|
|
for(blkY=0; blkY < numBlkSide; blkY++)
|
|
|
|
{
|
2013-04-10 13:55:31 +00:00
|
|
|
uint32_t offset = blkY * 4 * iWidth + blkX * 4;
|
2012-08-15 14:18:58 +00:00
|
|
|
for(x=0; x < 4; x++)
|
|
|
|
{
|
|
|
|
for(y=0; y < 4; y++)
|
|
|
|
{
|
|
|
|
pBuffV[uiCnt] = y*iWidth + x + offset;
|
|
|
|
uiCnt ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(iY=0; iY < iHeight; iY++)
|
|
|
|
{
|
|
|
|
for(iX=0; iX < iWidth; iX++)
|
|
|
|
{
|
|
|
|
pBuffH[uiCnt] = iY*iWidth + iX;
|
|
|
|
uiCnt ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uiCnt = 0;
|
|
|
|
for(iX=0; iX < iWidth; iX++)
|
|
|
|
{
|
|
|
|
for(iY=0; iY < iHeight; iY++)
|
|
|
|
{
|
|
|
|
pBuffV[uiCnt] = iY*iWidth + iX;
|
|
|
|
uiCnt ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:10:43 +00:00
|
|
|
|
2012-08-15 14:18:58 +00:00
|
|
|
void init_tables(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int c = 0;
|
2013-09-18 11:49:01 +00:00
|
|
|
memset( g_convert_to_bit,-1, sizeof( g_convert_to_bit ) );
|
2013-02-05 13:48:06 +00:00
|
|
|
for ( i=4; i<(1<<7); i*=2 )
|
2012-08-15 14:18:58 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
g_convert_to_bit[i] = c;
|
2012-08-15 14:18:58 +00:00
|
|
|
c++;
|
|
|
|
}
|
2013-09-18 11:49:01 +00:00
|
|
|
g_convert_to_bit[i] = c;
|
2012-08-15 14:18:58 +00:00
|
|
|
|
|
|
|
c = 2;
|
2013-02-05 13:48:06 +00:00
|
|
|
for ( i=0; i<7; i++ )
|
2012-08-15 14:18:58 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
g_sig_last_scan[0][i] = (uint32_t*)malloc(c*c*sizeof(uint32_t));
|
|
|
|
g_sig_last_scan[1][i] = (uint32_t*)malloc(c*c*sizeof(uint32_t));
|
|
|
|
g_sig_last_scan[2][i] = (uint32_t*)malloc(c*c*sizeof(uint32_t));
|
2012-08-15 14:18:58 +00:00
|
|
|
|
2013-09-18 11:49:01 +00:00
|
|
|
initSigLastScan( g_sig_last_scan[0][i], g_sig_last_scan[1][i], g_sig_last_scan[2][i], c, c);
|
2012-08-15 14:18:58 +00:00
|
|
|
c <<= 1;
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:10:43 +00:00
|
|
|
/* Lambda cost */
|
2013-09-09 11:22:53 +00:00
|
|
|
/* TODO: cleanup */
|
2013-04-17 14:08:52 +00:00
|
|
|
//g_lambda_cost = (int16_t*)malloc(sizeof(int16_t)*55);
|
2013-04-16 12:10:43 +00:00
|
|
|
for(i = 0; i < 55; i++)
|
|
|
|
{
|
2013-04-17 14:08:52 +00:00
|
|
|
if(i < 12) g_lambda_cost[i]= 0;
|
2013-04-18 11:04:15 +00:00
|
|
|
else g_lambda_cost[i] = (int16_t)sqrt(0.57*pow(2.0,(i-12)/3));
|
2013-04-17 14:08:52 +00:00
|
|
|
//g_lambda_cost[i] = g_lambda_cost[i]*g_lambda_cost[i];
|
2013-04-16 12:10:43 +00:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:18:58 +00:00
|
|
|
}
|
2012-06-07 14:38:28 +00:00
|
|
|
void init_encoder_control(encoder_control* control,bitstream* output)
|
|
|
|
{
|
2013-02-06 14:31:01 +00:00
|
|
|
control->stream = output;
|
2012-06-07 14:38:28 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 13:27:18 +00:00
|
|
|
void init_encoder_input(encoder_input* input,FILE* inputfile, int32_t width, int32_t height)
|
2012-06-05 11:01:47 +00:00
|
|
|
{
|
|
|
|
input->file = inputfile;
|
|
|
|
input->width = width;
|
|
|
|
input->height = height;
|
2013-09-11 16:15:05 +00:00
|
|
|
input->real_width = width;
|
|
|
|
input->real_height = height;
|
2013-09-09 15:32:16 +00:00
|
|
|
|
|
|
|
// If input dimensions are not divisible by the smallest block size, add pixels to the dimensions, so that they are.
|
|
|
|
// These extra pixels will be compressed along with the real ones but they will be cropped out before rendering.
|
|
|
|
if (width % CU_MIN_SIZE_PIXELS) {
|
|
|
|
input->width += CU_MIN_SIZE_PIXELS - (width % CU_MIN_SIZE_PIXELS);
|
|
|
|
}
|
2013-09-11 16:15:05 +00:00
|
|
|
if (height % CU_MIN_SIZE_PIXELS) {
|
2013-09-09 15:32:16 +00:00
|
|
|
input->height += CU_MIN_SIZE_PIXELS - (height % CU_MIN_SIZE_PIXELS);
|
|
|
|
}
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-09-18 11:49:01 +00:00
|
|
|
input->height_in_lcu = input->height / LCU_WIDTH;
|
|
|
|
input->width_in_lcu = input->width / LCU_WIDTH;
|
2013-03-21 15:12:55 +00:00
|
|
|
|
|
|
|
/* Add one extra LCU when image not divisible by LCU_WIDTH */
|
2013-09-18 11:49:01 +00:00
|
|
|
if(input->height_in_lcu * LCU_WIDTH < height)
|
2013-03-21 15:12:55 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
input->height_in_lcu++;
|
2013-03-21 15:12:55 +00:00
|
|
|
}
|
2013-09-18 11:49:01 +00:00
|
|
|
if(input->width_in_lcu * LCU_WIDTH < width)
|
2013-03-21 15:12:55 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
input->width_in_lcu++;
|
2013-03-21 15:12:55 +00:00
|
|
|
}
|
2012-06-06 13:20:29 +00:00
|
|
|
|
2013-09-12 13:28:40 +00:00
|
|
|
/* Allocate the picture and CU array */
|
2013-09-18 11:49:01 +00:00
|
|
|
input->cur_pic = picture_init(input->width, input->height, input->width_in_lcu,input->height_in_lcu);
|
2013-09-12 13:28:40 +00:00
|
|
|
|
|
|
|
if(!input->cur_pic)
|
|
|
|
{
|
|
|
|
printf("Error allocating picture!\r\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-09-09 15:32:16 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
if (width != input->width || height != input->height) {
|
|
|
|
printf("Picture buffer has been extended to be a multiple of the smallest block size:\r\n");
|
2013-09-11 16:15:05 +00:00
|
|
|
printf(" Width = %d (%d), Height = %d (%d)\r\n", width, input->width, height, input->height);
|
2013-09-09 15:32:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-12 13:28:40 +00:00
|
|
|
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2012-06-11 12:26:32 +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)
|
|
|
|
{
|
2013-05-07 07:36:04 +00:00
|
|
|
|
|
|
|
/* Video Parameter Set (VPS) */
|
|
|
|
encode_vid_parameter_set(encoder);
|
2012-06-05 12:38:54 +00:00
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
2013-09-09 08:04:09 +00:00
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_VPS_NUT, 0);
|
2012-06-06 10:42:02 +00:00
|
|
|
bitstream_clear_buffer(encoder->stream);
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-05-07 07:36:04 +00:00
|
|
|
/* Sequence Parameter Set (SPS) */
|
|
|
|
encode_seq_parameter_set(encoder);
|
2012-08-10 08:59:05 +00:00
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
2013-09-09 08:04:09 +00:00
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_SPS_NUT, 0);
|
2012-08-10 08:59:05 +00:00
|
|
|
bitstream_clear_buffer(encoder->stream);
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2012-06-11 15:43:29 +00:00
|
|
|
/* Picture Parameter Set (PPS) */
|
2012-06-05 12:38:54 +00:00
|
|
|
encode_pic_parameter_set(encoder);
|
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
2013-09-09 08:04:09 +00:00
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_PPS_NUT, 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);
|
2013-09-12 13:28:40 +00:00
|
|
|
encoder->in.cur_pic->slicetype = SLICE_I;
|
|
|
|
encoder->in.cur_pic->type = NAL_IDR_W_RADL;
|
2013-04-16 12:10:43 +00:00
|
|
|
search_slice_data(encoder);
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2012-06-06 11:09:28 +00:00
|
|
|
encode_slice_header(encoder);
|
2012-08-14 08:02:08 +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);
|
2013-09-03 14:55:36 +00:00
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_IDR_W_RADL, 0);
|
2012-06-06 13:20:29 +00:00
|
|
|
bitstream_clear_buffer(encoder->stream);
|
2013-09-02 06:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//else if(encoder->frame == 1)
|
|
|
|
//{
|
|
|
|
// /*
|
|
|
|
// cabac_start(&cabac);
|
2013-09-12 13:28:40 +00:00
|
|
|
// encoder->in.cur_pic->slicetype = SLICE_P;
|
|
|
|
// encoder->in.cur_pic->type = 1;
|
2013-09-02 06:53:36 +00:00
|
|
|
|
|
|
|
// encode_slice_header(encoder);
|
|
|
|
// bitstream_align(encoder->stream);
|
|
|
|
// cabac_flush(&cabac);
|
|
|
|
// bitstream_align(encoder->stream);
|
|
|
|
// bitstream_flush(encoder->stream);
|
|
|
|
// nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0,1,0);
|
|
|
|
// bitstream_clear_buffer(encoder->stream);*/
|
|
|
|
|
|
|
|
|
|
|
|
// cabac_start(&cabac);
|
2013-09-12 13:28:40 +00:00
|
|
|
// encoder->in.cur_pic->slicetype = SLICE_P;
|
|
|
|
// encoder->in.cur_pic->type = 1;
|
2013-09-02 06:53:36 +00:00
|
|
|
// search_slice_data(encoder);
|
|
|
|
|
|
|
|
// encode_slice_header(encoder);
|
|
|
|
// bitstream_align(encoder->stream);
|
|
|
|
// encode_slice_data(encoder);
|
|
|
|
// cabac_flush(&cabac);
|
|
|
|
// bitstream_align(encoder->stream);
|
|
|
|
// bitstream_flush(encoder->stream);
|
|
|
|
// nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0,1,1);
|
|
|
|
// bitstream_clear_buffer(encoder->stream);
|
|
|
|
//}
|
2013-03-22 14:36:35 +00:00
|
|
|
else
|
2013-09-02 06:53:36 +00:00
|
|
|
{
|
2013-06-03 11:22:50 +00:00
|
|
|
cabac_start(&cabac);
|
2013-09-12 13:28:40 +00:00
|
|
|
encoder->in.cur_pic->slicetype = (encoder->frame==1)?SLICE_P:SLICE_I;
|
|
|
|
encoder->in.cur_pic->type = NAL_TRAIL_R;
|
2013-06-03 11:22:50 +00:00
|
|
|
search_slice_data(encoder);
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-06-03 11:22:50 +00:00
|
|
|
encode_slice_header(encoder);
|
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
encode_slice_data(encoder);
|
|
|
|
cabac_flush(&cabac);
|
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
2013-09-12 12:26:03 +00:00
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0,NAL_TRAIL_R,0);
|
2013-06-03 11:22:50 +00:00
|
|
|
bitstream_clear_buffer(encoder->stream);
|
2012-06-11 12:26:32 +00:00
|
|
|
}
|
2013-03-19 13:45:50 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
int x,y;
|
|
|
|
for(y = 0; y < encoder->in.height_in_LCU*2;y++)
|
|
|
|
{
|
|
|
|
for(x = 0;x < encoder->in.width_in_LCU*2;x++)
|
|
|
|
{
|
|
|
|
i = (x<<2)+(y<<2)*(encoder->in.width_in_LCU<<MAX_DEPTH);
|
2013-09-12 13:28:40 +00:00
|
|
|
printf("(%d,%d) Intramode: %d\n", x<<2, y<<2,encoder->in.cur_pic->CU[0][i].intra.mode);
|
2013-03-19 13:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
#endif
|
2013-04-04 12:08:28 +00:00
|
|
|
|
|
|
|
/* Filtering */
|
2013-09-12 15:50:11 +00:00
|
|
|
/* TODO: Check for correct deblock condition on inter blocks */
|
|
|
|
if(encoder->deblock_enable && encoder->in.cur_pic->slicetype == SLICE_I)
|
2013-06-03 11:22:50 +00:00
|
|
|
{
|
|
|
|
filter_deblock(encoder);
|
|
|
|
}
|
2013-04-04 12:08:28 +00:00
|
|
|
|
2013-09-12 15:50:11 +00:00
|
|
|
|
2013-09-05 12:59:51 +00:00
|
|
|
/* Calculate checksum */
|
|
|
|
add_checksum(encoder);
|
2013-03-19 13:45:50 +00:00
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
}
|
2013-03-19 13:45:50 +00:00
|
|
|
|
2013-09-09 16:18:47 +00:00
|
|
|
void fill_after_frame(FILE* file, unsigned height, unsigned array_width, unsigned array_height, unsigned char* data)
|
|
|
|
{
|
2013-09-11 16:15:05 +00:00
|
|
|
unsigned char* p = data + height * array_width;
|
2013-09-09 16:18:47 +00:00
|
|
|
unsigned char* end = data + array_width * array_height;
|
2013-09-11 16:15:05 +00:00
|
|
|
|
|
|
|
while (p < end) {
|
|
|
|
// Fill the line by copying the line above.
|
|
|
|
memcpy(p, p - array_width, array_width);
|
|
|
|
p += array_width;
|
|
|
|
}
|
2013-09-09 16:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_and_fill_frame_data(FILE* file, unsigned width, unsigned height, unsigned array_width, unsigned char* data)
|
|
|
|
{
|
2013-09-11 16:15:05 +00:00
|
|
|
unsigned char* p = data;
|
|
|
|
unsigned char* end = data + array_width * height;
|
|
|
|
unsigned char fill_char;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
while (p < end) {
|
|
|
|
// Read the beginning of the line from input.
|
2013-09-09 16:18:47 +00:00
|
|
|
fread(p, sizeof(unsigned char), width, file);
|
2013-09-11 16:15:05 +00:00
|
|
|
|
|
|
|
// Fill the rest with the last pixel value.
|
|
|
|
fill_char = p[width - 1];
|
|
|
|
for (i = width; i < array_width; ++i) {
|
|
|
|
p[i] = fill_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += array_width;
|
2013-09-09 16:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 15:45:41 +00:00
|
|
|
void read_one_frame(FILE* file, encoder_control* encoder)
|
|
|
|
{
|
|
|
|
encoder_input* in = &encoder->in;
|
2013-09-11 16:15:05 +00:00
|
|
|
unsigned width = in->real_width;
|
|
|
|
unsigned height = in->real_height;
|
2013-09-12 16:15:05 +00:00
|
|
|
unsigned array_width = in->cur_pic->width;
|
|
|
|
unsigned array_height = in->cur_pic->height;
|
2013-09-09 16:18:47 +00:00
|
|
|
|
2013-09-09 15:45:41 +00:00
|
|
|
|
2013-09-09 16:18:47 +00:00
|
|
|
if (width != array_width) {
|
2013-09-11 16:15:05 +00:00
|
|
|
// In the case of frames not being aligned on 8 bit borders, bits need to be copied to fill them in.
|
2013-09-18 11:58:46 +00:00
|
|
|
read_and_fill_frame_data(file, width, height, array_width, in->cur_pic->y_data);
|
|
|
|
read_and_fill_frame_data(file, width >> 1, height >> 1, array_width >> 1, in->cur_pic->u_data);
|
|
|
|
read_and_fill_frame_data(file, width >> 1, height >> 1, array_width >> 1, in->cur_pic->v_data);
|
2013-09-09 16:18:47 +00:00
|
|
|
} else {
|
2013-09-11 16:15:05 +00:00
|
|
|
// Otherwise the data can be read directly to the array.
|
2013-09-18 11:58:46 +00:00
|
|
|
fread(in->cur_pic->y_data, sizeof(unsigned char), width * height, file);
|
|
|
|
fread(in->cur_pic->u_data, sizeof(unsigned char), (width >> 1) * (height >> 1), file);
|
|
|
|
fread(in->cur_pic->v_data, sizeof(unsigned char), (width >> 1) * (height >> 1), file);
|
2013-09-09 16:18:47 +00:00
|
|
|
}
|
2013-09-09 15:45:41 +00:00
|
|
|
|
2013-09-09 16:18:47 +00:00
|
|
|
if (height != array_height) {
|
2013-09-18 11:58:46 +00:00
|
|
|
fill_after_frame(file, height, array_width, array_height, in->cur_pic->y_data);
|
|
|
|
fill_after_frame(file, height >> 1, array_width >> 1, array_height >> 1, in->cur_pic->u_data);
|
|
|
|
fill_after_frame(file, height >> 1, array_width >> 1, array_height >> 1, in->cur_pic->v_data);
|
2013-09-09 16:18:47 +00:00
|
|
|
}
|
2013-09-09 15:45:41 +00:00
|
|
|
}
|
2013-09-05 12:59:51 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Add a checksum SEI message to the bitstream.
|
|
|
|
\param encoder The encoder.
|
|
|
|
\returns Void
|
|
|
|
*/
|
|
|
|
static void add_checksum(encoder_control* encoder)
|
|
|
|
{
|
2013-09-19 13:03:02 +00:00
|
|
|
unsigned char checksum[3][SEI_HASH_MAX_LENGTH];
|
2013-09-05 12:59:51 +00:00
|
|
|
uint32_t checksum_val;
|
|
|
|
unsigned int i;
|
|
|
|
|
2013-09-12 13:28:40 +00:00
|
|
|
picture_checksum(encoder->in.cur_pic, checksum);
|
2013-09-05 12:59:51 +00:00
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 132, 8, "sei_type");
|
|
|
|
WRITE_U(encoder->stream, 13, 8, "size");
|
|
|
|
WRITE_U(encoder->stream, 2, 8, "hash_type"); /* 2 = checksum*/
|
|
|
|
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
/* Pack bits into a single 32 bit uint instead of pushing them one byte at a time. */
|
|
|
|
checksum_val = (checksum[i][0] << 24) + (checksum[i][1] << 16) + (checksum[i][2] << 8) + (checksum[i][3]);
|
2013-09-10 17:44:22 +00:00
|
|
|
WRITE_U(encoder->stream, checksum_val, 32, "picture_checksum");
|
2013-09-05 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bitstream_align(encoder->stream);
|
|
|
|
bitstream_flush(encoder->stream);
|
|
|
|
nal_write(encoder->output, encoder->stream->buffer, encoder->stream->buffer_pos, 0, NAL_SUFFIT_SEI_NUT, 0);
|
|
|
|
bitstream_clear_buffer(encoder->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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");
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "dependent_slice_segments_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "output_flag_present_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 3, "num_extra_slice_header_bits");
|
2013-03-20 15:27:47 +00:00
|
|
|
WRITE_U(encoder->stream, ENABLE_SIGN_HIDING, 1, "sign_data_hiding_flag");
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "cabac_init_present_flag");
|
2013-02-06 14:31:01 +00:00
|
|
|
|
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");
|
2013-03-07 15:42:00 +00:00
|
|
|
WRITE_SE(encoder->stream, ((int8_t)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");
|
2012-08-10 08:59:05 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "transform_skip_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "cu_qp_delta_enabled_flag");
|
|
|
|
//if cu_qp_delta_enabled_flag
|
|
|
|
//WRITE_UE(encoder->stream, 0, "diff_cu_qp_delta_depth");
|
|
|
|
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: add QP offsets
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_SE(encoder->stream, 0, "pps_cb_qp_offset");
|
|
|
|
WRITE_SE(encoder->stream, 0, "pps_cr_qp_offset");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "pps_slice_chroma_qp_offsets_present_flag");
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "weighted_pred_flag");
|
2012-08-14 08:02:08 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "weighted_bipred_idc");
|
2013-02-06 14:31:01 +00:00
|
|
|
|
|
|
|
//WRITE_U(encoder->stream, 0, 1, "dependent_slices_enabled_flag");
|
2012-08-10 08:59:05 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "transquant_bypass_enable_flag");
|
2013-02-13 12:46:26 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "tiles_enabled_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "entropy_coding_sync_enabled_flag");
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: enable tiles for concurrency
|
2013-02-06 14:31:01 +00:00
|
|
|
//IF tiles
|
|
|
|
//ENDIF
|
2012-08-10 08:59:05 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "loop_filter_across_slice_flag");
|
2013-03-11 10:06:08 +00:00
|
|
|
WRITE_U(encoder->stream, 1, 1, "deblocking_filter_control_present_flag");
|
2013-02-06 14:31:01 +00:00
|
|
|
//IF deblocking_filter
|
2013-03-11 10:06:08 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "deblocking_filter_override_enabled_flag");
|
2013-05-22 14:27:15 +00:00
|
|
|
WRITE_U(encoder->stream, encoder->deblock_enable?0:1, 1, "pps_disable_deblocking_filter_flag");
|
2013-04-04 12:08:28 +00:00
|
|
|
//IF !disabled
|
2013-05-22 14:27:15 +00:00
|
|
|
if(encoder->deblock_enable)
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
WRITE_SE(encoder->stream, encoder->beta_offset_div2, "beta_offset_div2");
|
|
|
|
WRITE_SE(encoder->stream, encoder->tc_offset_div2, "tc_offset_div2");
|
2013-05-22 14:27:15 +00:00
|
|
|
}
|
2013-04-04 12:08:28 +00:00
|
|
|
//ENDIF
|
2013-02-06 14:31:01 +00:00
|
|
|
//ENDIF
|
2012-08-10 08:59:05 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "pps_scaling_list_data_present_flag");
|
2013-02-06 14:31:01 +00:00
|
|
|
//IF scaling_list
|
|
|
|
//ENDIF
|
2013-02-13 12:46:26 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "lists_modification_present_flag");
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "log2_parallel_merge_level_minus2");
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "slice_segment_header_extension_present_flag");
|
2012-08-10 08:59:05 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "pps_extension_flag");
|
2012-06-05 12:38:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 12:46:26 +00:00
|
|
|
void encode_PTL(encoder_control *encoder)
|
|
|
|
{
|
2013-02-21 14:45:22 +00:00
|
|
|
int i;
|
2013-02-13 12:46:26 +00:00
|
|
|
/*PTL*/
|
|
|
|
/*Profile Tier*/
|
|
|
|
WRITE_U(encoder->stream, 0, 2, "XXX_profile_space[]");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "XXX_tier_flag[]");
|
|
|
|
WRITE_U(encoder->stream, 0, 5, "XXX_profile_idc[]");
|
|
|
|
WRITE_U(encoder->stream, 0, 32, "XXX_profile_compatibility_flag[][j]");
|
|
|
|
|
2013-05-07 07:36:04 +00:00
|
|
|
WRITE_U(encoder->stream, 1, 1, "general_progressive_source_flag");
|
2013-02-13 12:46:26 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "general_interlaced_source_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "general_non_packed_constraint_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "general_frame_only_constraint_flag");
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 32, "XXX_reserved_zero_44bits[0..31]");
|
|
|
|
WRITE_U(encoder->stream, 0, 12, "XXX_reserved_zero_44bits[32..43]");
|
|
|
|
|
|
|
|
/*end Profile Tier */
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2013-02-13 12:46:26 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 8, "general_level_idc");
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "sub_layer_profile_present_flag");
|
2013-02-21 14:45:22 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "sub_layer_level_present_flag");
|
|
|
|
for(i = 1; i < 8; i++)
|
|
|
|
{
|
|
|
|
WRITE_U(encoder->stream, 0, 2, "reserved_zero_2bits");
|
2013-05-16 12:27:54 +00:00
|
|
|
}
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2013-02-13 12:46:26 +00:00
|
|
|
/*end PTL*/
|
|
|
|
}
|
|
|
|
|
2012-06-05 12:38:54 +00:00
|
|
|
void encode_seq_parameter_set(encoder_control* encoder)
|
|
|
|
{
|
2013-09-09 15:32:16 +00:00
|
|
|
encoder_input* const in = &encoder->in;
|
|
|
|
|
2012-06-05 12:38:54 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("=========== Sequence Parameter Set ID: 0 ===========\n");
|
|
|
|
#endif
|
2013-09-09 11:22:53 +00:00
|
|
|
/* TODO: profile IDC and level IDC should be defined later on */
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 4, "sps_video_parameter_set_id");
|
2013-02-21 14:45:22 +00:00
|
|
|
WRITE_U(encoder->stream, 1, 3, "sps_max_sub_layers_minus1");
|
2013-02-06 14:31:01 +00:00
|
|
|
|
2013-02-21 14:45:22 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "sps_temporal_id_nesting_flag");
|
2013-02-13 12:46:26 +00:00
|
|
|
|
|
|
|
encode_PTL(encoder);
|
2013-02-06 14:31:01 +00:00
|
|
|
|
|
|
|
WRITE_UE(encoder->stream, 0, "sps_seq_parameter_set_id");
|
2012-06-12 14:35:45 +00:00
|
|
|
WRITE_UE(encoder->stream, encoder->in.video_format, "chroma_format_idc"); /* 0 = 4:0:0, 1 = 4:2:0, 2 = 4:2:2, 3 = 4:4:4 */
|
2013-02-06 14:31:01 +00:00
|
|
|
if(encoder->in.video_format == 3)
|
|
|
|
{
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "separate_colour_plane_flag");
|
|
|
|
}
|
2012-06-05 12:38:54 +00:00
|
|
|
WRITE_UE(encoder->stream, encoder->in.width, "pic_width_in_luma_samples");
|
|
|
|
WRITE_UE(encoder->stream, encoder->in.height, "pic_height_in_luma_samples");
|
2013-09-02 06:53:36 +00:00
|
|
|
|
2013-09-11 16:15:05 +00:00
|
|
|
if (in->width != in->real_width || in->height != in->real_height) {
|
2013-09-09 15:32:16 +00:00
|
|
|
// The standard does not seem to allow setting conf_win values such that the number of
|
|
|
|
// luma samples is not a multiple of 2. Options are to either hide one line or show an
|
|
|
|
// extra line of non-video. Neither seems like a very good option, so let's not even try.
|
|
|
|
assert(!(in->width % 2));
|
|
|
|
WRITE_U(encoder->stream, 1, 1, "conformance_window_flag");
|
|
|
|
WRITE_UE(encoder->stream, 0, "conf_win_left_offset");
|
2013-09-11 16:15:05 +00:00
|
|
|
WRITE_UE(encoder->stream, (in->width - in->real_width) >> 1, "conf_win_right_offset");
|
2013-09-09 15:32:16 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "conf_win_top_offset");
|
2013-09-11 16:15:05 +00:00
|
|
|
WRITE_UE(encoder->stream, (in->height - in->real_height) >> 1, "conf_win_bottom_offset");
|
2013-09-09 15:32:16 +00:00
|
|
|
} else {
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "conformance_window_flag");
|
|
|
|
}
|
|
|
|
|
2013-02-06 14:31:01 +00:00
|
|
|
//IF window flag
|
|
|
|
//END IF
|
|
|
|
|
|
|
|
WRITE_UE(encoder->stream, encoder->bitdepth-8, "bit_depth_luma_minus8");
|
|
|
|
WRITE_UE(encoder->stream, encoder->bitdepth-8, "bit_depth_chroma_minus8");
|
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "log2_max_pic_order_cnt_lsb_minus4");
|
2013-02-06 14:31:01 +00:00
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "sps_sub_layer_ordering_info_present_flag");
|
|
|
|
//for each layer
|
|
|
|
WRITE_UE(encoder->stream, 0, "sps_max_dec_pic_buffering");
|
|
|
|
WRITE_UE(encoder->stream, 0, "sps_num_reorder_pics");
|
|
|
|
WRITE_UE(encoder->stream, 0, "sps_max_latency_increase");
|
|
|
|
//end for
|
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
WRITE_UE(encoder->stream, MIN_SIZE-3, "log2_min_coding_block_size_minus3");
|
2012-06-11 15:43:29 +00:00
|
|
|
WRITE_UE(encoder->stream, MAX_DEPTH, "log2_diff_max_min_coding_block_size");
|
2013-03-26 14:53:07 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "log2_min_transform_block_size_minus2"); /* 4x4 */
|
|
|
|
WRITE_UE(encoder->stream, 3, "log2_diff_max_min_transform_block_size"); /* 4x4...32x32 */
|
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");
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
/* Use default scaling list */
|
2013-08-02 13:35:30 +00:00
|
|
|
WRITE_U(encoder->stream, ENABLE_SCALING_LIST, 1, "scaling_list_enable_flag");
|
|
|
|
#if ENABLE_SCALING_LIST == 1
|
2013-03-11 15:15:11 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "sps_scaling_list_data_present_flag");
|
2013-08-02 13:35:30 +00:00
|
|
|
#endif
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "amp_enabled_flag");
|
2013-06-03 11:22:50 +00:00
|
|
|
WRITE_U(encoder->stream, encoder->sao_enable?1:0, 1, "sample_adaptive_offset_enabled_flag");
|
2013-02-06 14:31:01 +00:00
|
|
|
|
|
|
|
WRITE_U(encoder->stream, ENABLE_PCM, 1, "pcm_enabled_flag");
|
2012-06-11 12:39:18 +00:00
|
|
|
#if ENABLE_PCM == 1
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 7, 4, "pcm_sample_bit_depth_luma_minus1");
|
|
|
|
WRITE_U(encoder->stream, 7, 4, "pcm_sample_bit_depth_chroma_minus1");
|
|
|
|
WRITE_UE(encoder->stream, 0, "log2_min_pcm_coding_block_size_minus3");
|
|
|
|
WRITE_UE(encoder->stream, 2, "log2_diff_max_min_pcm_coding_block_size");
|
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
|
2013-02-06 14:31:01 +00:00
|
|
|
|
|
|
|
WRITE_UE(encoder->stream, 0, "num_short_term_ref_pic_sets");
|
|
|
|
//IF num short term ref pic sets
|
|
|
|
//ENDIF
|
2012-06-11 12:26:32 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "long_term_ref_pics_present_flag");
|
2013-02-06 14:31:01 +00:00
|
|
|
//IF long_term_ref_pics_present
|
|
|
|
//ENDIF
|
2013-09-18 07:15:05 +00:00
|
|
|
WRITE_U(encoder->stream, ENABLE_TEMPORAL_MVP, 1, "sps_temporal_mvp_enable_flag");
|
2013-02-06 14:31:01 +00:00
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "sps_strong_intra_smoothing_enable_flag");
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "vui_parameters_present_flag");
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: VUI?
|
2013-02-13 12:46:26 +00:00
|
|
|
//encode_VUI(encoder);
|
|
|
|
|
2012-06-12 14:35:45 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "sps_extension_flag");
|
2012-08-10 08:59:05 +00:00
|
|
|
}
|
2012-06-12 14:35:45 +00:00
|
|
|
|
2012-08-10 08:59:05 +00:00
|
|
|
void encode_vid_parameter_set(encoder_control* encoder)
|
|
|
|
{
|
2013-02-21 14:45:22 +00:00
|
|
|
int i;
|
2012-08-10 08:59:05 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("=========== Video Parameter Set ID: 0 ===========\n");
|
|
|
|
#endif
|
|
|
|
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 4, "vps_video_parameter_set_id");
|
|
|
|
WRITE_U(encoder->stream, 3, 2, "vps_reserved_three_2bits" );
|
|
|
|
WRITE_U(encoder->stream, 0, 6, "vps_reserved_zero_6bits" );
|
2013-02-21 14:45:22 +00:00
|
|
|
WRITE_U(encoder->stream, 1, 3, "vps_max_sub_layers_minus1");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "vps_temporal_id_nesting_flag");
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 0xffff, 16, "vps_reserved_ffff_16bits");
|
2013-02-13 12:46:26 +00:00
|
|
|
|
|
|
|
encode_PTL(encoder);
|
|
|
|
|
2013-02-06 14:31:01 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "vps_sub_layer_ordering_info_present_flag");
|
|
|
|
//for each layer
|
2013-02-21 14:45:22 +00:00
|
|
|
for(i = 0; i < 1; i++)
|
|
|
|
{
|
2013-03-20 15:27:47 +00:00
|
|
|
WRITE_UE(encoder->stream, 1, "vps_max_dec_pic_buffering");
|
2012-08-14 08:02:08 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "vps_num_reorder_pics");
|
|
|
|
WRITE_UE(encoder->stream, 0, "vps_max_latency_increase");
|
2013-02-21 14:45:22 +00:00
|
|
|
}
|
2013-02-06 14:31:01 +00:00
|
|
|
//end for
|
|
|
|
WRITE_U(encoder->stream, 0, 6, "vps_max_nuh_reserved_zero_layer_id");
|
|
|
|
WRITE_UE(encoder->stream, 0, "vps_max_op_sets_minus1");
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "vps_timing_info_present_flag");
|
|
|
|
//IF timing info
|
|
|
|
//END IF
|
2012-08-10 08:59:05 +00:00
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "vps_extension_flag");
|
2012-06-06 11:09:28 +00:00
|
|
|
}
|
|
|
|
|
2013-02-06 14:31:01 +00:00
|
|
|
void encode_VUI(encoder_control* encoder)
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("=========== VUI Set ID: 0 ===========\n");
|
|
|
|
#endif
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "aspect_ratio_info_present_flag");
|
|
|
|
//IF aspect ratio info
|
|
|
|
//ENDIF
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "overscan_info_present_flag");
|
|
|
|
//IF overscan info
|
|
|
|
//ENDIF
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "video_signal_type_present_flag");
|
|
|
|
//IF video type
|
|
|
|
//ENDIF
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "chroma_loc_info_present_flag");
|
|
|
|
//IF chroma loc info
|
|
|
|
//ENDIF
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "neutral_chroma_indication_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "field_seq_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "frame_field_info_present_flag");
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "default_display_window_flag");
|
|
|
|
//IF default display window
|
|
|
|
//ENDIF
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "vui_timing_info_present_flag");
|
|
|
|
//IF timing info
|
|
|
|
//ENDIF
|
|
|
|
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "bitstream_restriction_flag");
|
|
|
|
//IF bitstream restriction
|
|
|
|
//ENDIF
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-02-13 12:46:26 +00:00
|
|
|
WRITE_U(encoder->stream, 1, 1, "first_slice_segment_in_pic_flag");
|
2013-09-12 13:28:40 +00:00
|
|
|
if(encoder->in.cur_pic->type >= NAL_BLA_W_LP && encoder->in.cur_pic->type <= NAL_RSV_IRAP_VCL23)
|
2012-08-10 08:59:05 +00:00
|
|
|
{
|
2013-05-07 07:36:04 +00:00
|
|
|
WRITE_U(encoder->stream, 1, 1, "no_output_of_prior_pics_flag");
|
2012-08-10 08:59:05 +00:00
|
|
|
}
|
2013-02-13 12:46:26 +00:00
|
|
|
WRITE_UE(encoder->stream, 0, "slice_pic_parameter_set_id");
|
|
|
|
|
|
|
|
//WRITE_U(encoder->stream, 0, 1, "dependent_slice_segment_flag");
|
2012-08-10 08:59:05 +00:00
|
|
|
|
2013-09-12 13:28:40 +00:00
|
|
|
WRITE_UE(encoder->stream, encoder->in.cur_pic->slicetype, "slice_type");
|
2012-08-10 08:59:05 +00:00
|
|
|
|
2012-06-06 11:09:28 +00:00
|
|
|
// if !entropy_slice_flag
|
2012-08-10 08:59:05 +00:00
|
|
|
|
2012-06-06 11:09:28 +00:00
|
|
|
//if output_flag_present_flag
|
2013-02-13 12:46:26 +00:00
|
|
|
//WRITE_U(encoder->stream, 1, 1, "pic_output_flag");
|
2012-06-06 11:09:28 +00:00
|
|
|
//end if
|
|
|
|
//if( IdrPicFlag ) <- nal_unit_type == 5
|
2013-09-12 13:28:40 +00:00
|
|
|
if(encoder->in.cur_pic->type != NAL_IDR_W_RADL && encoder->in.cur_pic->type != NAL_IDR_N_LP)
|
2012-06-11 12:26:32 +00:00
|
|
|
{
|
2013-09-04 14:47:54 +00:00
|
|
|
int j;
|
|
|
|
int ref_negative = 1;
|
|
|
|
int ref_positive = 0;
|
2013-05-20 14:26:57 +00:00
|
|
|
WRITE_U(encoder->stream, encoder->frame&0xf, 4, "pic_order_cnt_lsb");
|
2013-05-07 07:36:04 +00:00
|
|
|
WRITE_U(encoder->stream, 0, 1, "short_term_ref_pic_set_sps_flag");
|
2013-09-04 14:47:54 +00:00
|
|
|
WRITE_UE(encoder->stream, ref_negative, "num_negative_pics");
|
|
|
|
WRITE_UE(encoder->stream, ref_positive, "num_positive_pics");
|
|
|
|
for(j=0; j <ref_negative; j++)
|
|
|
|
{
|
|
|
|
WRITE_UE(encoder->stream, 0, "delta_poc_s0_minus1");
|
|
|
|
WRITE_U(encoder->stream,1,1, "used_by_curr_pic_s0_flag");
|
|
|
|
}
|
|
|
|
|
2013-02-21 14:45:22 +00:00
|
|
|
//WRITE_UE(encoder->stream, 0, "short_term_ref_pic_set_idx");
|
2012-06-11 12:26:32 +00:00
|
|
|
}
|
2012-06-06 11:09:28 +00:00
|
|
|
//end if
|
|
|
|
//end if
|
2013-06-03 11:22:50 +00:00
|
|
|
if(encoder->sao_enable)
|
|
|
|
{
|
2013-09-02 06:53:36 +00:00
|
|
|
WRITE_U(encoder->stream, 1,1, "slice_sao_luma_flag");
|
|
|
|
WRITE_U(encoder->stream, 0,1, "slice_sao_chroma_flag");
|
2013-06-03 11:22:50 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 13:28:40 +00:00
|
|
|
if(encoder->in.cur_pic->slicetype != SLICE_I)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "num_ref_idx_active_override_flag");
|
|
|
|
WRITE_UE(encoder->stream, 0, "five_minus_max_num_merge_cand");
|
|
|
|
}
|
2013-09-02 06:53:36 +00:00
|
|
|
|
2013-09-12 13:28:40 +00:00
|
|
|
if(encoder->in.cur_pic->slicetype == SLICE_B)
|
2013-09-02 06:53:36 +00:00
|
|
|
{
|
|
|
|
WRITE_U(encoder->stream, 0, 1, "mvd_l1_zero_flag");
|
|
|
|
}
|
2012-06-11 15:43:29 +00:00
|
|
|
/* Skip flags that are not present */
|
2012-06-06 11:09:28 +00:00
|
|
|
// if !entropy_slice_flag
|
2012-08-14 08:02:08 +00:00
|
|
|
WRITE_SE(encoder->stream, 0, "slice_qp_delta");
|
2013-02-21 14:45:22 +00:00
|
|
|
//WRITE_U(encoder->stream, 1, 1, "alignment");
|
2012-06-05 12:38:54 +00:00
|
|
|
}
|
2012-06-13 15:08:15 +00:00
|
|
|
|
2012-06-06 11:09:28 +00:00
|
|
|
void encode_slice_data(encoder_control* encoder)
|
|
|
|
{
|
2012-08-15 10:22:31 +00:00
|
|
|
uint16_t xCtb,yCtb;
|
2012-06-15 11:54:57 +00:00
|
|
|
|
2013-03-07 15:42:00 +00:00
|
|
|
scalinglist_process();
|
2013-09-12 13:28:40 +00:00
|
|
|
init_contexts(encoder,encoder->in.cur_pic->slicetype);
|
2012-06-11 15:43:29 +00:00
|
|
|
|
|
|
|
/* Loop through every LCU in the slice */
|
2013-09-18 11:49:01 +00:00
|
|
|
for(yCtb = 0; yCtb < encoder->in.height_in_lcu; yCtb++)
|
2012-06-06 13:20:29 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +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
|
|
|
{
|
2013-09-18 11:49:01 +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-11 15:43:29 +00:00
|
|
|
|
|
|
|
/* Recursive function for looping through all the sub-blocks */
|
|
|
|
encode_coding_tree(encoder, xCtb<<MAX_DEPTH,yCtb<<MAX_DEPTH, depth);
|
|
|
|
|
|
|
|
/* signal Terminating bit */
|
2012-06-08 13:55:24 +00:00
|
|
|
if(!lastCUx || !lastCUy)
|
|
|
|
{
|
2013-09-18 11:06:45 +00:00
|
|
|
cabac_encode_bin_trm(&cabac, 0);
|
2012-06-08 13:55:24 +00:00
|
|
|
}
|
2012-06-06 13:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void encode_coding_tree(encoder_control* encoder,uint16_t xCtb,uint16_t yCtb, uint8_t depth)
|
2013-04-17 14:08:52 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
CU_info *cur_CU = &encoder->in.cur_pic->CU[depth][xCtb+yCtb*(encoder->in.width_in_lcu<<MAX_DEPTH)];
|
2013-09-18 07:15:05 +00:00
|
|
|
uint8_t split_flag = cur_CU->split;
|
2012-06-11 12:26:32 +00:00
|
|
|
uint8_t split_model = 0;
|
2012-08-15 12:23:44 +00:00
|
|
|
|
|
|
|
/* Check for slice border */
|
2013-04-09 12:59:12 +00:00
|
|
|
uint8_t border_x = ((encoder->in.width)<( xCtb*(LCU_WIDTH>>MAX_DEPTH) + (LCU_WIDTH>>depth) ))?1:0;
|
|
|
|
uint8_t border_y = ((encoder->in.height)<( yCtb*(LCU_WIDTH>>MAX_DEPTH) + (LCU_WIDTH>>depth) ))?1:0;
|
2013-06-03 11:22:50 +00:00
|
|
|
uint8_t border_split_x = ((encoder->in.width) < ( (xCtb+1)*(LCU_WIDTH>>MAX_DEPTH) + (LCU_WIDTH>>(depth+1)) ))?0:1;
|
|
|
|
uint8_t border_split_y = ((encoder->in.height) < ( (yCtb+1)*(LCU_WIDTH>>MAX_DEPTH) + (LCU_WIDTH>>(depth+1)) ))?0:1;
|
2013-03-22 14:36:35 +00:00
|
|
|
uint8_t border = border_x | border_y; /*!< are we in any border CU */
|
2013-04-17 14:08:52 +00:00
|
|
|
|
2012-08-15 12:23:44 +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-08-15 12:23:44 +00:00
|
|
|
if(depth != MAX_DEPTH)
|
2012-06-07 14:38:28 +00:00
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
/* Implisit split flag when on border */
|
2012-08-15 10:22:31 +00:00
|
|
|
if(!border)
|
|
|
|
{
|
2012-08-15 12:23:44 +00:00
|
|
|
/* Get left and top block split_flags and if they are present and true, increase model number */
|
2013-09-18 11:49:01 +00:00
|
|
|
if(xCtb > 0 && GET_SPLITDATA(&(encoder->in.cur_pic->CU[depth][xCtb-1+yCtb*(encoder->in.width_in_lcu<<MAX_DEPTH)]),depth) == 1)
|
2012-08-15 12:23:44 +00:00
|
|
|
{
|
|
|
|
split_model++;
|
|
|
|
}
|
2013-09-18 11:49:01 +00:00
|
|
|
if(yCtb > 0 && GET_SPLITDATA(&(encoder->in.cur_pic->CU[depth][xCtb+(yCtb-1)*(encoder->in.width_in_lcu<<MAX_DEPTH)]),depth) == 1)
|
2012-08-15 12:23:44 +00:00
|
|
|
{
|
|
|
|
split_model++;
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_split_flag_model[split_model];
|
2012-08-15 10:22:31 +00:00
|
|
|
CABAC_BIN(&cabac, split_flag, "SplitFlag");
|
|
|
|
}
|
2012-08-15 12:23:44 +00:00
|
|
|
if(split_flag || border)
|
2012-06-08 12:26:07 +00:00
|
|
|
{
|
2012-06-11 15:43:29 +00:00
|
|
|
/* Split blocks and remember to change x and y block positions */
|
2012-06-11 12:26:32 +00:00
|
|
|
uint8_t change = 1<<(MAX_DEPTH-1-depth);
|
2013-05-20 14:26:57 +00:00
|
|
|
encode_coding_tree(encoder,xCtb,yCtb,depth+1); /* x,y */
|
2013-09-09 11:22:53 +00:00
|
|
|
if(!border_x || border_split_x) /* TODO: fix when other half of the block would not be completely over the border */
|
2012-08-15 10:22:31 +00:00
|
|
|
{
|
2013-05-20 14:26:57 +00:00
|
|
|
encode_coding_tree(encoder,xCtb+change,yCtb,depth+1); /* x+1,y */
|
2012-08-15 10:22:31 +00:00
|
|
|
}
|
2013-09-09 11:22:53 +00:00
|
|
|
if(!border_y || border_split_y) /* TODO: fix when other half of the block would not be completely over the border */
|
2012-08-15 10:22:31 +00:00
|
|
|
{
|
2013-05-20 14:26:57 +00:00
|
|
|
encode_coding_tree(encoder,xCtb,yCtb+change,depth+1); /* x,y+1 */
|
2012-08-15 10:22:31 +00:00
|
|
|
}
|
2013-09-09 11:22:53 +00:00
|
|
|
if(!border || (border_split_x && border_split_y) ) /* TODO: fix when other half of the block would not be completely over the border */
|
2012-08-15 10:22:31 +00:00
|
|
|
{
|
2013-05-20 14:26:57 +00:00
|
|
|
encode_coding_tree(encoder,xCtb+change,yCtb+change,depth+1); /* x+1,y+1 */
|
2012-08-15 10:22:31 +00:00
|
|
|
}
|
2012-06-11 15:43:29 +00:00
|
|
|
/* We don't need to do anything else here */
|
2012-06-08 12:26:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-06-07 14:38:28 +00:00
|
|
|
}
|
2013-02-24 14:03:40 +00:00
|
|
|
|
2013-04-19 07:56:40 +00:00
|
|
|
/* Encode skip flag */
|
2013-09-12 13:28:40 +00:00
|
|
|
if(encoder->in.cur_pic->slicetype != SLICE_I)
|
2012-08-15 14:18:58 +00:00
|
|
|
{
|
2013-09-09 14:37:49 +00:00
|
|
|
int8_t uiCtxSkip = 0;
|
2013-04-19 07:56:40 +00:00
|
|
|
/* uiCtxSkip = aboveskipped + leftskipped; */
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_skip_flag_model[uiCtxSkip];
|
2013-04-24 13:49:47 +00:00
|
|
|
CABAC_BIN(&cabac, (cur_CU->type == CU_SKIP)?1:0, "SkipFlag");
|
2013-04-19 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* IF SKIP */
|
2013-04-24 13:49:47 +00:00
|
|
|
if(cur_CU->type == CU_SKIP)
|
2013-04-19 07:56:40 +00:00
|
|
|
{
|
2013-04-24 13:49:47 +00:00
|
|
|
/* Encode merge index */
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: calculate/fetch merge candidates
|
2013-04-24 13:49:47 +00:00
|
|
|
int16_t unaryIdx = 0;//pcCU->getMergeIndex( uiAbsPartIdx );
|
|
|
|
int16_t numCand = 0;//pcCU->getSlice()->getMaxNumMergeCand();
|
|
|
|
int32_t ui;
|
|
|
|
if ( numCand > 1 )
|
|
|
|
{
|
|
|
|
for(ui = 0; ui < numCand - 1; ui++ )
|
|
|
|
{
|
|
|
|
int32_t symbol = (ui == unaryIdx) ? 0 : 1;
|
|
|
|
if ( ui==0 )
|
|
|
|
{
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_merge_idx_ext_model;
|
2013-04-24 13:49:47 +00:00
|
|
|
CABAC_BIN(&cabac, symbol, "MergeIndex");
|
|
|
|
}
|
|
|
|
else
|
2013-05-16 12:27:54 +00:00
|
|
|
{
|
2013-04-24 13:49:47 +00:00
|
|
|
CABAC_BIN_EP(&cabac,symbol,"MergeIndex");
|
|
|
|
}
|
|
|
|
if( symbol == 0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2013-04-19 07:56:40 +00:00
|
|
|
}
|
|
|
|
/* ENDIF SKIP */
|
|
|
|
|
|
|
|
/* Prediction mode */
|
2013-09-12 13:28:40 +00:00
|
|
|
if(encoder->in.cur_pic->slicetype != SLICE_I)
|
2013-04-19 07:56:40 +00:00
|
|
|
{
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_pred_mode_model;
|
2013-04-19 07:56:40 +00:00
|
|
|
CABAC_BIN(&cabac, (cur_CU->type == CU_INTRA)?1:0, "PredMode");
|
2012-08-15 14:18:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 12:27:54 +00:00
|
|
|
/* Signal PartSize on max depth */
|
2013-04-24 13:49:47 +00:00
|
|
|
if(depth == MAX_DEPTH || cur_CU->type != CU_INTRA)
|
2013-04-24 07:35:27 +00:00
|
|
|
{
|
2013-09-09 11:22:53 +00:00
|
|
|
/* TODO: Handle inter sizes other than 2Nx2N */
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_part_size_model[0];
|
2013-04-24 07:35:27 +00:00
|
|
|
CABAC_BIN(&cabac, 1, "PartSize");
|
2013-09-09 11:22:53 +00:00
|
|
|
/* TODO: add AMP modes */
|
2013-04-24 07:35:27 +00:00
|
|
|
}
|
2013-04-04 12:08:28 +00:00
|
|
|
|
2013-03-11 14:26:09 +00:00
|
|
|
/*end partsize*/
|
2013-04-19 07:56:40 +00:00
|
|
|
if(cur_CU->type == CU_INTER)
|
|
|
|
{
|
2013-04-24 13:49:47 +00:00
|
|
|
/* FOR each part */
|
|
|
|
/* Mergeflag */
|
2013-08-02 13:35:30 +00:00
|
|
|
uint8_t mergeFlag = 0;
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_merge_flag_ext_model;
|
2013-08-02 13:35:30 +00:00
|
|
|
CABAC_BIN(&cabac, mergeFlag, "MergeFlag");
|
|
|
|
if(mergeFlag) //merge
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
|
|
|
/* MergeIndex */
|
|
|
|
int16_t unaryIdx = 0;//pcCU->getMergeIndex( uiAbsPartIdx );
|
2013-08-02 13:35:30 +00:00
|
|
|
int16_t numCand = 0;//pcCU->getSlice()->getMaxNumMergeCand();
|
2013-04-24 13:49:47 +00:00
|
|
|
int32_t ui;
|
2013-08-02 13:35:30 +00:00
|
|
|
if (numCand > 1)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
|
|
|
for(ui = 0; ui < numCand - 1; ui++ )
|
|
|
|
{
|
|
|
|
int32_t symbol = (ui == unaryIdx) ? 0 : 1;
|
2013-08-02 13:35:30 +00:00
|
|
|
if (ui == 0)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_merge_idx_ext_model;
|
2013-04-24 13:49:47 +00:00
|
|
|
CABAC_BIN(&cabac, symbol, "MergeIndex");
|
|
|
|
}
|
|
|
|
else
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-04-24 13:49:47 +00:00
|
|
|
CABAC_BIN_EP(&cabac,symbol,"MergeIndex");
|
|
|
|
}
|
2013-08-02 13:35:30 +00:00
|
|
|
if(symbol == 0)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-02 13:35:30 +00:00
|
|
|
uint32_t uiRefListIdx;
|
2013-09-16 13:37:24 +00:00
|
|
|
|
|
|
|
int16_t mv_cand[2][2];
|
|
|
|
|
|
|
|
|
2013-04-24 13:49:47 +00:00
|
|
|
/*
|
2013-08-02 13:35:30 +00:00
|
|
|
// Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
|
2013-09-12 13:28:40 +00:00
|
|
|
if(encoder->in.cur_pic->slicetype == SLICE_B)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
|
|
|
// Code Inter Dir
|
|
|
|
const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
|
|
|
|
const UInt uiCtx = pcCU->getCtxInterDir( uiAbsPartIdx );
|
|
|
|
ContextModel *pCtx = m_cCUInterDirSCModel.get( 0 );
|
|
|
|
if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
|
|
|
|
{
|
|
|
|
m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
|
|
|
|
}
|
|
|
|
if (uiInterDir < 2)
|
|
|
|
{
|
|
|
|
m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 13:35:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2013-09-16 13:37:24 +00:00
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
for(uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
2013-09-02 06:53:36 +00:00
|
|
|
//if(encoder->ref_idx_num[uiRefListIdx] > 0)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
2013-09-02 06:53:36 +00:00
|
|
|
if(cur_CU->inter.mv_dir & (1 << uiRefListIdx))
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
2013-09-02 06:53:36 +00:00
|
|
|
if(0)//encoder->ref_idx_num[uiRefListIdx] != 1)//NumRefIdx != 1)
|
2013-04-24 13:49:47 +00:00
|
|
|
{
|
2013-09-05 12:04:16 +00:00
|
|
|
/* parseRefFrmIdx */
|
2013-09-02 06:53:36 +00:00
|
|
|
int32_t iRefFrame = cur_CU->inter.mv_ref;
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_ref_pic_model[0];
|
2013-09-05 12:04:16 +00:00
|
|
|
CABAC_BIN(&cabac, (iRefFrame==0)?0:1, "ref_frame_flag");
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
if(iRefFrame > 0)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
|
|
|
uint32_t ui;
|
2013-09-02 06:53:36 +00:00
|
|
|
uint32_t uiRefNum = encoder->ref_idx_num[uiRefListIdx]-2;
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_ref_pic_model[1];
|
2013-08-02 13:35:30 +00:00
|
|
|
iRefFrame--;
|
2013-09-02 06:53:36 +00:00
|
|
|
for(ui = 0; ui < uiRefNum; ++ui)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
|
|
|
const uint32_t uiSymbol = (ui==iRefFrame)?0:1;
|
2013-09-02 06:53:36 +00:00
|
|
|
if(ui == 0)
|
|
|
|
{
|
2013-08-02 13:35:30 +00:00
|
|
|
CABAC_BIN(&cabac, uiSymbol, "ref_frame_flag2");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CABAC_BIN_EP(&cabac,uiSymbol,"ref_frame_flag2");
|
|
|
|
}
|
2013-09-02 06:53:36 +00:00
|
|
|
if(uiSymbol == 0)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:37:24 +00:00
|
|
|
/* Get MV candidates */
|
|
|
|
inter_get_mv_cand(encoder, xCtb, yCtb, depth, mv_cand);
|
2013-09-16 13:51:13 +00:00
|
|
|
|
|
|
|
/* Select better candidate */
|
2013-09-16 14:15:54 +00:00
|
|
|
cur_CU->inter.mv_ref = 0; /* Default to candidate 0 */
|
2013-09-16 13:51:13 +00:00
|
|
|
/* Only check when candidates are different */
|
|
|
|
if (mv_cand[0][0] != mv_cand[1][0] || mv_cand[0][1] != mv_cand[1][1]) {
|
|
|
|
uint16_t cand_1_diff = abs(cur_CU->inter.mv[0]-mv_cand[0][0]) + abs(cur_CU->inter.mv[1]-mv_cand[0][1]);
|
|
|
|
uint16_t cand_2_diff = abs(cur_CU->inter.mv[0]-mv_cand[1][0]) + abs(cur_CU->inter.mv[1]-mv_cand[1][1]);
|
|
|
|
/* Select candidate 1 if it's closer */
|
|
|
|
if (cand_2_diff < cand_1_diff) {
|
|
|
|
cur_CU->inter.mv_ref = 1;
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
if (!(/*pcCU->getSlice()->getMvdL1ZeroFlag() &&*/ encoder->ref_list == REF_PIC_LIST_1 && cur_CU->inter.mv_dir==3))
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-16 13:37:24 +00:00
|
|
|
const int32_t mvd_hor = cur_CU->inter.mv[0]-mv_cand[cur_CU->inter.mv_ref][0];
|
|
|
|
const int32_t mvd_ver = cur_CU->inter.mv[1]-mv_cand[cur_CU->inter.mv_ref][1];
|
2013-08-02 13:35:30 +00:00
|
|
|
const int8_t bHorAbsGr0 = mvd_hor != 0;
|
|
|
|
const int8_t bVerAbsGr0 = mvd_ver != 0;
|
|
|
|
const uint32_t mvd_hor_abs = abs(mvd_hor);
|
|
|
|
const uint32_t mvd_ver_abs = abs(mvd_ver);
|
2013-09-18 11:21:03 +00:00
|
|
|
|
|
|
|
cabac.ctx = &g_cu_mvd_model[0];
|
2013-09-09 14:37:49 +00:00
|
|
|
CABAC_BIN(&cabac, (mvd_hor!=0)?1:0, "abs_mvd_greater0_flag_hor");
|
|
|
|
CABAC_BIN(&cabac, (mvd_ver!=0)?1:0, "abs_mvd_greater0_flag_ver");
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_mvd_model[1];
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
if(bHorAbsGr0)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-09 14:37:49 +00:00
|
|
|
CABAC_BIN(&cabac, (mvd_hor_abs>1)?1:0, "abs_mvd_greater1_flag_hor");
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
if(bVerAbsGr0)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-09 14:37:49 +00:00
|
|
|
CABAC_BIN(&cabac, (mvd_ver_abs>1)?1:0, "abs_mvd_greater1_flag_ver");
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
if(bHorAbsGr0)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-02 06:53:36 +00:00
|
|
|
if(mvd_hor_abs > 1)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-18 11:06:45 +00:00
|
|
|
cabac_write_ep_ex_golomb(&cabac,mvd_hor_abs-2, 1);
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
2013-09-09 14:37:49 +00:00
|
|
|
CABAC_BIN_EP(&cabac, (mvd_hor>0)?0:1, "mvd_sign_flag_hor");
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 06:53:36 +00:00
|
|
|
if(bVerAbsGr0)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-02 06:53:36 +00:00
|
|
|
if(mvd_ver_abs > 1)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-18 11:06:45 +00:00
|
|
|
cabac_write_ep_ex_golomb(&cabac,mvd_ver_abs-2, 1);
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
2013-09-09 14:37:49 +00:00
|
|
|
CABAC_BIN_EP(&cabac, (mvd_ver>0)?0:1, "mvd_sign_flag_ver");
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
2013-09-12 15:50:11 +00:00
|
|
|
|
|
|
|
/* Inter reconstruction */
|
|
|
|
inter_recon(encoder->ref->pics[0],xCtb*CU_MIN_SIZE_PIXELS,yCtb*CU_MIN_SIZE_PIXELS,LCU_WIDTH>>depth,cur_CU->inter.mv,encoder->in.cur_pic);
|
2013-09-16 13:37:24 +00:00
|
|
|
|
|
|
|
/* Mark this block as "coded" (can be used for predictions..) */
|
2013-09-20 08:07:31 +00:00
|
|
|
picture_set_block_coded(encoder->in.cur_pic,xCtb, yCtb, depth, 1);
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
2013-09-16 13:51:13 +00:00
|
|
|
/* Signal which candidate MV to use */
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac_write_unary_max_symbol(&cabac,g_mvp_idx_model, cur_CU->inter.mv_ref,1,AMVP_MAX_NUM_CANDS-1);
|
2013-04-24 13:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-18 11:21:03 +00:00
|
|
|
}
|
2013-09-02 06:53:36 +00:00
|
|
|
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_cu_qt_root_cbf_model;
|
2013-09-09 14:37:49 +00:00
|
|
|
CABAC_BIN(&cabac, 0, "rqt_root_cbf");
|
|
|
|
if(0)
|
2013-09-02 06:53:36 +00:00
|
|
|
{
|
|
|
|
transform_info ti;
|
|
|
|
memset(&ti, 0, sizeof(transform_info));
|
|
|
|
|
2013-09-18 11:49:01 +00:00
|
|
|
ti.x_ctb = xCtb; ti.y_ctb = yCtb;
|
2013-09-02 06:53:36 +00:00
|
|
|
|
|
|
|
/* Coded block pattern */
|
|
|
|
ti.cb_top[0] = 0;
|
|
|
|
ti.cb_top[1] = 0;
|
|
|
|
ti.cb_top[2] = 0;
|
|
|
|
ti.split[0] = 0;
|
|
|
|
|
|
|
|
/* Code (possible) coeffs to bitstream */
|
|
|
|
ti.idx = 0;
|
|
|
|
encode_transform_coeff(encoder, &ti,depth, 0);
|
|
|
|
}
|
2013-04-24 13:49:47 +00:00
|
|
|
}
|
2013-03-11 14:26:09 +00:00
|
|
|
|
2013-04-24 13:49:47 +00:00
|
|
|
/* END for each part */
|
2013-04-19 07:56:40 +00:00
|
|
|
}
|
|
|
|
else if(cur_CU->type == CU_INTRA)
|
2012-06-12 14:35:45 +00:00
|
|
|
{
|
2013-04-17 14:08:52 +00:00
|
|
|
uint8_t intraPredMode = cur_CU->intra.mode;
|
|
|
|
uint8_t intraPredModeChroma = 36; /* 36 = Chroma derived from luma */
|
2013-03-07 15:42:00 +00:00
|
|
|
int8_t intraPreds[3] = {-1, -1, -1};
|
|
|
|
int8_t mpmPred = -1;
|
2013-03-25 15:17:24 +00:00
|
|
|
int i;
|
2013-05-16 12:27:54 +00:00
|
|
|
uint32_t flag;
|
2013-09-18 11:58:46 +00:00
|
|
|
uint8_t *base = &encoder->in.cur_pic->y_data[xCtb*(LCU_WIDTH>>(MAX_DEPTH)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH))) *encoder->in.width];
|
|
|
|
uint8_t *baseU = &encoder->in.cur_pic->u_data[xCtb*(LCU_WIDTH>>(MAX_DEPTH+1)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH+1)))*(encoder->in.width>>1)];
|
|
|
|
uint8_t *baseV = &encoder->in.cur_pic->v_data[xCtb*(LCU_WIDTH>>(MAX_DEPTH+1)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH+1)))*(encoder->in.width>>1)];
|
2013-03-13 13:56:43 +00:00
|
|
|
uint32_t width = LCU_WIDTH>>depth;
|
|
|
|
|
2013-05-21 12:57:22 +00:00
|
|
|
/* INTRAPREDICTION VARIABLES */
|
2013-06-12 12:41:57 +00:00
|
|
|
int16_t pred[LCU_WIDTH*LCU_WIDTH+1];
|
2013-03-21 09:56:19 +00:00
|
|
|
int16_t predU[LCU_WIDTH*LCU_WIDTH>>2];
|
|
|
|
int16_t predV[LCU_WIDTH*LCU_WIDTH>>2];
|
2013-03-14 15:55:06 +00:00
|
|
|
|
2013-09-18 11:58:46 +00:00
|
|
|
uint8_t *recbase = &encoder->in.cur_pic->y_recdata[xCtb*(LCU_WIDTH>>(MAX_DEPTH)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH))) *encoder->in.width];
|
|
|
|
uint8_t *recbaseU = &encoder->in.cur_pic->u_recdata[xCtb*(LCU_WIDTH>>(MAX_DEPTH+1)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH+1)))*(encoder->in.width>>1)];
|
|
|
|
uint8_t *recbaseV = &encoder->in.cur_pic->v_recdata[xCtb*(LCU_WIDTH>>(MAX_DEPTH+1)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH+1)))*(encoder->in.width>>1)];
|
2013-03-19 14:23:33 +00:00
|
|
|
|
2013-06-12 12:41:57 +00:00
|
|
|
|
|
|
|
/* SEARCH BEST INTRA MODE (AGAIN) */
|
|
|
|
|
|
|
|
int16_t rec[(LCU_WIDTH*2+8)*(LCU_WIDTH*2+8)];
|
|
|
|
int16_t *recShift = &rec[(LCU_WIDTH>>(depth))*2+8+1];
|
2013-09-18 12:07:43 +00:00
|
|
|
intra_build_reference_border(encoder->in.cur_pic, xCtb, yCtb,(LCU_WIDTH>>(depth))*2+8, rec, (LCU_WIDTH>>(depth))*2+8, 0);
|
2013-09-18 11:58:46 +00:00
|
|
|
cur_CU->intra.mode = (int8_t)intra_prediction(encoder->in.cur_pic->y_data,encoder->in.width,recShift,(LCU_WIDTH>>(depth))*2+8,xCtb*(LCU_WIDTH>>(MAX_DEPTH)),yCtb*(LCU_WIDTH>>(MAX_DEPTH)),width,pred,width,&cur_CU->intra.cost);
|
2013-06-12 12:41:57 +00:00
|
|
|
intraPredMode = cur_CU->intra.mode;
|
2013-09-18 12:07:43 +00:00
|
|
|
intra_set_block_mode(encoder->in.cur_pic,xCtb, yCtb, depth, intraPredMode);
|
2013-06-12 12:41:57 +00:00
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
#if ENABLE_PCM == 1
|
2013-03-19 14:23:33 +00:00
|
|
|
/* Code must start after variable initialization */
|
2013-09-18 11:06:45 +00:00
|
|
|
cabac_encode_bin_trm(&cabac, 0); /* IPCMFlag == 0 */
|
2013-03-20 15:27:47 +00:00
|
|
|
#endif
|
2013-06-12 12:41:57 +00:00
|
|
|
|
|
|
|
|
2012-06-12 14:35:45 +00:00
|
|
|
/*
|
2013-02-28 15:31:14 +00:00
|
|
|
PREDINFO CODING
|
|
|
|
If intra prediction mode is found from the predictors,
|
|
|
|
it can be signaled with two EP's. Otherwise we can send
|
|
|
|
5 EP bins with the full predmode
|
2013-09-09 11:22:53 +00:00
|
|
|
TODO: split to a function
|
2012-06-12 14:35:45 +00:00
|
|
|
*/
|
2013-09-18 12:07:43 +00:00
|
|
|
intra_get_dir_luma_predictor(encoder->in.cur_pic, xCtb, yCtb, depth, intraPreds);
|
2013-04-02 11:50:09 +00:00
|
|
|
|
2013-03-07 15:42:00 +00:00
|
|
|
for(i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
if(intraPreds[i] == intraPredMode)
|
|
|
|
{
|
|
|
|
mpmPred = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-11 14:26:09 +00:00
|
|
|
/* For each part { */
|
2013-03-13 13:56:43 +00:00
|
|
|
flag = (mpmPred==-1)?0:1;
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_intra_mode_model;
|
2013-03-13 13:56:43 +00:00
|
|
|
CABAC_BIN(&cabac,flag,"IntraPred");
|
2013-03-11 14:26:09 +00:00
|
|
|
/*} End for each part */
|
2013-03-08 09:42:22 +00:00
|
|
|
|
2013-03-08 23:49:42 +00:00
|
|
|
/* Intrapredmode signaling
|
|
|
|
If found from predictors, we can simplify signaling
|
|
|
|
*/
|
2013-03-13 13:56:43 +00:00
|
|
|
if(flag)
|
2013-03-08 23:49:42 +00:00
|
|
|
{
|
2013-03-13 13:56:43 +00:00
|
|
|
flag = (mpmPred==0)?0:1;
|
|
|
|
CABAC_BIN_EP(&cabac, flag, "intraPredMode");
|
2013-03-08 09:42:22 +00:00
|
|
|
if(mpmPred!=0)
|
2013-03-13 13:56:43 +00:00
|
|
|
{
|
|
|
|
flag = (mpmPred==1)?0:1;
|
|
|
|
CABAC_BIN_EP(&cabac, flag, "intraPredMode");
|
|
|
|
}
|
2013-03-08 09:42:22 +00:00
|
|
|
}
|
2013-03-11 14:26:09 +00:00
|
|
|
else /* Else we signal the "full" predmode */
|
2013-03-08 23:49:42 +00:00
|
|
|
{
|
2013-05-16 12:27:54 +00:00
|
|
|
int32_t intraPredModeTemp = intraPredMode;
|
2013-03-08 09:42:22 +00:00
|
|
|
if (intraPreds[0] > intraPreds[1])
|
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
SWAP(intraPreds[0], intraPreds[1], int8_t);
|
2013-03-08 09:42:22 +00:00
|
|
|
}
|
|
|
|
if (intraPreds[0] > intraPreds[2])
|
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
SWAP(intraPreds[0], intraPreds[2], int8_t);
|
2013-03-08 09:42:22 +00:00
|
|
|
}
|
|
|
|
if (intraPreds[1] > intraPreds[2])
|
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
SWAP(intraPreds[1], intraPreds[2], int8_t);
|
2013-03-07 15:42:00 +00:00
|
|
|
}
|
2013-03-08 09:42:22 +00:00
|
|
|
for(i = 2; i >= 0; i--)
|
|
|
|
{
|
|
|
|
intraPredModeTemp = intraPredModeTemp > intraPreds[i] ? intraPredModeTemp - 1 : intraPredModeTemp;
|
|
|
|
}
|
|
|
|
CABAC_BINS_EP(&cabac, intraPredModeTemp, 5, "intraPredMode");
|
|
|
|
}
|
2013-02-21 14:45:22 +00:00
|
|
|
|
2013-03-08 23:49:42 +00:00
|
|
|
/* If we have chroma, signal it */
|
2013-03-08 09:42:22 +00:00
|
|
|
if(encoder->in.video_format != FORMAT_400)
|
|
|
|
{
|
2013-03-11 14:26:09 +00:00
|
|
|
/* Chroma intra prediction */
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_chroma_pred_model[0];
|
2013-03-08 09:42:22 +00:00
|
|
|
CABAC_BIN(&cabac,((intraPredModeChroma!=36)?1:0),"IntraPredChroma");
|
|
|
|
|
2013-03-11 14:26:09 +00:00
|
|
|
/* If not copied from luma, signal it */
|
2013-03-08 09:42:22 +00:00
|
|
|
if(intraPredModeChroma!=36)
|
2013-02-28 15:31:14 +00:00
|
|
|
{
|
2013-03-08 09:42:22 +00:00
|
|
|
int8_t intraPredModeChromaTemp = intraPredModeChroma;
|
2013-03-08 23:49:42 +00:00
|
|
|
/* Default chroma predictors */
|
2013-03-08 09:42:22 +00:00
|
|
|
uint32_t allowedChromaDir[ 5 ] = { 0, 26, 10, 1, 36 };
|
|
|
|
|
2013-03-11 14:26:09 +00:00
|
|
|
/* If intra is the same as one of the default predictors, replace it */
|
2013-03-08 09:42:22 +00:00
|
|
|
for(i = 0; i < 4; i++ )
|
2013-02-28 15:31:14 +00:00
|
|
|
{
|
2013-03-08 09:42:22 +00:00
|
|
|
if( intraPredMode == allowedChromaDir[i] )
|
|
|
|
{
|
2013-03-11 14:26:09 +00:00
|
|
|
allowedChromaDir[i] = 34; /* VER+8 mode */
|
2013-03-08 09:42:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-02-28 15:31:14 +00:00
|
|
|
}
|
2013-03-08 09:42:22 +00:00
|
|
|
|
|
|
|
for(i = 0; i < 4; i++ )
|
|
|
|
{
|
|
|
|
if( intraPredModeChromaTemp == allowedChromaDir[i] )
|
|
|
|
{
|
|
|
|
intraPredModeChromaTemp = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CABAC_BINS_EP(&cabac, intraPredModeChromaTemp, 2, "intraPredModeChroma");
|
2013-02-28 15:31:14 +00:00
|
|
|
}
|
2013-03-08 09:42:22 +00:00
|
|
|
}
|
2013-02-28 15:31:14 +00:00
|
|
|
/*
|
|
|
|
END OF PREDINFO CODING
|
|
|
|
*/
|
2012-06-12 14:35:45 +00:00
|
|
|
|
|
|
|
/* Coeff */
|
|
|
|
/* Transform tree */
|
2013-03-25 15:17:24 +00:00
|
|
|
{
|
2013-09-09 11:22:53 +00:00
|
|
|
/* TODO: dynamic memory allocation */
|
2013-03-26 14:53:07 +00:00
|
|
|
int16_t coeff[LCU_WIDTH*LCU_WIDTH*2];
|
|
|
|
int16_t coeffU[LCU_WIDTH*LCU_WIDTH>>1];
|
|
|
|
int16_t coeffV[LCU_WIDTH*LCU_WIDTH>>1];
|
2013-04-02 11:50:09 +00:00
|
|
|
|
|
|
|
/* Initialize helper structure for transform */
|
|
|
|
transform_info ti;
|
|
|
|
memset(&ti, 0, sizeof(transform_info));
|
|
|
|
|
2013-09-18 11:49:01 +00:00
|
|
|
ti.x_ctb = xCtb; ti.y_ctb = yCtb;
|
2013-05-21 12:57:22 +00:00
|
|
|
|
2013-04-02 11:50:09 +00:00
|
|
|
/* Base pointers */
|
2013-09-18 11:49:01 +00:00
|
|
|
ti.base = base; ti.base_u = baseU; ti.base_v = baseV;
|
2013-04-02 11:50:09 +00:00
|
|
|
ti.base_stride = encoder->in.width;
|
|
|
|
|
|
|
|
/* Prediction pointers */
|
2013-09-18 11:49:01 +00:00
|
|
|
ti.pred = pred; ti.pred_u = predU; ti.pred_v = predV;
|
2013-04-02 11:50:09 +00:00
|
|
|
ti.pred_stride = (LCU_WIDTH>>depth);
|
|
|
|
|
|
|
|
/* Reconstruction pointers */
|
2013-09-18 11:49:01 +00:00
|
|
|
ti.recbase = recbase; ti.recbase_u = recbaseU; ti.recbase_v = recbaseV;
|
2013-04-02 11:50:09 +00:00
|
|
|
ti.recbase_stride = encoder->in.width;
|
|
|
|
|
|
|
|
/* Coeff pointers */
|
|
|
|
ti.coeff[0] = coeff; ti.coeff[1] = coeffU; ti.coeff[2] = coeffV;
|
|
|
|
|
|
|
|
/* Prediction info */
|
2013-09-18 11:49:01 +00:00
|
|
|
ti.intra_pred_mode = intraPredMode; ti.intra_pred_mode_chroma = intraPredModeChroma;
|
2013-04-02 11:50:09 +00:00
|
|
|
|
2013-03-26 14:53:07 +00:00
|
|
|
/* Handle transforms, quant and reconstruction */
|
2013-04-02 11:50:09 +00:00
|
|
|
ti.idx = 0;
|
|
|
|
encode_transform_tree(encoder,&ti, depth);
|
|
|
|
|
|
|
|
/* Coded block pattern */
|
|
|
|
ti.cb_top[0] = (ti.cb[0] & 0x1 || ti.cb[1] & 0x1 || ti.cb[2] & 0x1 || ti.cb[3] & 0x1)?1:0;
|
|
|
|
ti.cb_top[1] = (ti.cb[0] & 0x2 || ti.cb[1] & 0x2 || ti.cb[2] & 0x2 || ti.cb[3] & 0x2)?1:0;
|
|
|
|
ti.cb_top[2] = (ti.cb[0] & 0x4 || ti.cb[1] & 0x4 || ti.cb[2] & 0x4 || ti.cb[3] & 0x4)?1:0;
|
|
|
|
|
|
|
|
/* Code (possible) coeffs to bitstream */
|
|
|
|
ti.idx = 0;
|
|
|
|
encode_transform_coeff(encoder, &ti,depth, 0);
|
2013-03-25 15:17:24 +00:00
|
|
|
}
|
2013-03-11 10:06:08 +00:00
|
|
|
/* end Transform tree */
|
|
|
|
/* end Coeff */
|
2013-03-08 23:49:42 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
}
|
2013-03-20 15:27:47 +00:00
|
|
|
#if ENABLE_PCM == 1
|
2013-03-11 10:06:08 +00:00
|
|
|
/* Code IPCM block */
|
2013-03-26 14:53:07 +00:00
|
|
|
else if(cur_CU->type == CU_PCM)
|
2013-03-11 10:06:08 +00:00
|
|
|
{
|
2013-09-18 11:06:45 +00:00
|
|
|
cabac_encode_bin_trm(&cabac, 1); /* IPCMFlag == 1 */
|
2013-03-11 10:06:08 +00:00
|
|
|
cabac_finish(&cabac);
|
|
|
|
bitstream_align(cabac.stream);
|
|
|
|
/* PCM sample */
|
|
|
|
{
|
2013-09-18 11:58:46 +00:00
|
|
|
uint8_t *base = &encoder->in.cur_pic->y_data[xCtb*(LCU_WIDTH>>(MAX_DEPTH)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH)))*encoder->in.width];
|
|
|
|
uint8_t *baseCb = &encoder->in.cur_pic->u_data[(xCtb*(LCU_WIDTH>>(MAX_DEPTH+1)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH+1)))*encoder->in.width/2)];
|
|
|
|
uint8_t *baseCr = &encoder->in.cur_pic->v_data[(xCtb*(LCU_WIDTH>>(MAX_DEPTH+1)) + (yCtb*(LCU_WIDTH>>(MAX_DEPTH+1)))*encoder->in.width/2)];
|
2013-03-11 10:06:08 +00:00
|
|
|
for(y = 0; y < LCU_WIDTH>>depth; y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>depth; x++)
|
|
|
|
{
|
|
|
|
bitstream_put(cabac.stream, base[x+y*encoder->in.width], 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(encoder->in.video_format != FORMAT_400)
|
|
|
|
{
|
2013-03-11 14:26:09 +00:00
|
|
|
/* Cb */
|
2013-03-11 10:06:08 +00:00
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
|
|
|
bitstream_put(cabac.stream, baseCb[x+y*(encoder->in.width>>1)], 8);
|
|
|
|
}
|
2013-04-02 11:50:09 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
}
|
2013-03-07 15:42:00 +00:00
|
|
|
|
2013-03-11 14:26:09 +00:00
|
|
|
/* Cr */
|
2013-03-11 10:06:08 +00:00
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
|
|
|
bitstream_put(cabac.stream, baseCr[x+y*(encoder->in.width>>1)], 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* end PCM sample */
|
|
|
|
cabac_start(&cabac);
|
2013-02-05 13:48:06 +00:00
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
} /* end Code IPCM block */
|
|
|
|
#endif /* END ENABLE_PCM */
|
2013-03-11 10:06:08 +00:00
|
|
|
else /* Should not happend */
|
|
|
|
{
|
|
|
|
printf("UNHANDLED TYPE!\r\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* end prediction unit */
|
|
|
|
/* end coding_unit */
|
|
|
|
|
|
|
|
}
|
2013-03-08 23:49:42 +00:00
|
|
|
|
2013-04-02 11:50:09 +00:00
|
|
|
void encode_transform_tree(encoder_control* encoder,transform_info* ti,uint8_t depth)
|
2013-03-25 10:48:19 +00:00
|
|
|
{
|
2013-03-25 15:17:24 +00:00
|
|
|
/* we have 64>>depth transform size */
|
2013-03-25 10:48:19 +00:00
|
|
|
int x,y,i;
|
|
|
|
int32_t width = LCU_WIDTH>>depth;
|
2013-03-25 15:17:24 +00:00
|
|
|
|
2013-05-21 12:57:22 +00:00
|
|
|
if(depth == 0) /* Split 64x64 */
|
2013-03-25 15:17:24 +00:00
|
|
|
{
|
2013-03-26 14:53:07 +00:00
|
|
|
/* Prepare for multi-level splitting */
|
2013-04-02 11:50:09 +00:00
|
|
|
ti->split[ti->idx] = 1<<depth;
|
2013-03-25 15:17:24 +00:00
|
|
|
}
|
|
|
|
|
2013-05-22 14:27:15 +00:00
|
|
|
/* Split transform and increase depth */
|
2013-04-02 11:50:09 +00:00
|
|
|
if(ti->split[ti->idx] & (1<<depth))
|
2013-03-25 10:48:19 +00:00
|
|
|
{
|
2013-05-21 12:57:22 +00:00
|
|
|
uint8_t change = 1<<(MAX_DEPTH-1-depth);
|
|
|
|
ti->idx = 0; encode_transform_tree(encoder,ti,depth+1);
|
2013-09-18 11:49:01 +00:00
|
|
|
ti->x_ctb += change;
|
2013-04-02 11:50:09 +00:00
|
|
|
ti->idx = 1; encode_transform_tree(encoder,ti,depth+1);
|
2013-09-18 11:49:01 +00:00
|
|
|
ti->x_ctb -= change; ti->y_ctb += change;
|
2013-04-02 11:50:09 +00:00
|
|
|
ti->idx = 2; encode_transform_tree(encoder,ti,depth+1);
|
2013-09-18 11:49:01 +00:00
|
|
|
ti->x_ctb += change;
|
2013-04-02 11:50:09 +00:00
|
|
|
ti->idx = 3; encode_transform_tree(encoder,ti,depth+1);
|
|
|
|
return;
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
2013-03-25 15:17:24 +00:00
|
|
|
|
2013-03-25 10:48:19 +00:00
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
uint8_t CbY = 0,CbU = 0,CbV = 0;
|
2013-05-16 12:27:54 +00:00
|
|
|
int32_t coeff_fourth = ((LCU_WIDTH>>(depth))*(LCU_WIDTH>>(depth)))+1;
|
2013-04-02 11:50:09 +00:00
|
|
|
|
|
|
|
int32_t base_stride = ti->base_stride;
|
|
|
|
int32_t recbase_stride = ti->recbase_stride;
|
|
|
|
int32_t pred_stride = ti->pred_stride;
|
|
|
|
|
|
|
|
int32_t recbase_offset[4] = {0, width , ti->recbase_stride*(width) , ti->recbase_stride*(width) +width };
|
|
|
|
int32_t base_offset[4] = {0, width , ti->base_stride*(width) , ti->base_stride*(width) +width };
|
|
|
|
int32_t pred_offset[4] = {0, width , ti->pred_stride*(width) , ti->pred_stride*(width) +width };
|
|
|
|
int32_t recbase_offset_c[4] = {0, width>>1, (ti->recbase_stride>>1)*(width>>1), (ti->recbase_stride>>1)*(width>>1)+(width>>1)};
|
|
|
|
int32_t base_offset_c[4] = {0, width>>1, (ti->base_stride>>1)*(width>>1) , (ti->base_stride>>1)*(width>>1) +(width>>1)};
|
|
|
|
int32_t pred_offset_c[4] = {0, width>>1, (ti->pred_stride>>1)*(width>>1) , (ti->pred_stride>>1)*(width>>1) +(width>>1)};
|
2013-03-25 15:17:24 +00:00
|
|
|
|
2013-04-02 11:50:09 +00:00
|
|
|
uint8_t* base = &ti->base[base_offset[ti->idx]];
|
2013-09-18 11:49:01 +00:00
|
|
|
uint8_t* baseU = &ti->base_u[base_offset_c[ti->idx]];
|
|
|
|
uint8_t* baseV = &ti->base_v[base_offset_c[ti->idx]];
|
2013-04-02 11:50:09 +00:00
|
|
|
|
|
|
|
uint8_t* recbase = &ti->recbase[recbase_offset[ti->idx]];
|
2013-09-18 11:49:01 +00:00
|
|
|
uint8_t* recbaseU = &ti->recbase_u[recbase_offset_c[ti->idx]];
|
|
|
|
uint8_t* recbaseV = &ti->recbase_v[recbase_offset_c[ti->idx]];
|
2013-04-02 11:50:09 +00:00
|
|
|
|
|
|
|
int16_t* pred = &ti->pred[pred_offset[ti->idx]];
|
2013-09-18 11:49:01 +00:00
|
|
|
int16_t* predU = &ti->pred_u[pred_offset_c[ti->idx]];
|
|
|
|
int16_t* predV = &ti->pred_v[pred_offset_c[ti->idx]];
|
2013-04-02 11:50:09 +00:00
|
|
|
|
|
|
|
int16_t* coeff = &ti->coeff[0][ti->idx*coeff_fourth];
|
|
|
|
int16_t* coeffU = &ti->coeff[1][ti->idx*coeff_fourth>>1];
|
|
|
|
int16_t* coeffV = &ti->coeff[2][ti->idx*coeff_fourth>>1];
|
2013-05-21 12:57:22 +00:00
|
|
|
|
2013-03-25 10:48:19 +00:00
|
|
|
/*
|
|
|
|
Quant and transform here...
|
|
|
|
*/
|
2013-03-25 15:17:24 +00:00
|
|
|
int16_t block[LCU_WIDTH*LCU_WIDTH>>2];
|
|
|
|
int16_t pre_quant_coeff[LCU_WIDTH*LCU_WIDTH>>2];
|
2013-03-25 10:48:19 +00:00
|
|
|
|
2013-05-21 12:57:22 +00:00
|
|
|
/* INTRA PREDICTION */
|
2013-09-09 11:22:53 +00:00
|
|
|
/* TODO: split to a function! */
|
2013-05-21 12:57:22 +00:00
|
|
|
int16_t rec[(LCU_WIDTH*2+8)*(LCU_WIDTH*2+8)];
|
|
|
|
int16_t *recShift = &rec[(LCU_WIDTH>>(depth))*2+8+1];
|
|
|
|
int16_t *recShiftU = &rec[(LCU_WIDTH>>(depth+1))*2+8+1];
|
2013-06-12 12:41:57 +00:00
|
|
|
|
|
|
|
uint32_t ac_sum = 0;
|
2013-05-21 12:57:22 +00:00
|
|
|
|
|
|
|
/* Build reconstructed block to use in prediction with extrapolated borders */
|
2013-09-18 12:07:43 +00:00
|
|
|
intra_build_reference_border(encoder->in.cur_pic, ti->x_ctb, ti->y_ctb,(LCU_WIDTH>>(depth))*2+8, rec, (LCU_WIDTH>>(depth))*2+8, 0);
|
2013-09-18 11:49:01 +00:00
|
|
|
intra_recon(recShift,(LCU_WIDTH>>(depth))*2+8,ti->x_ctb*(LCU_WIDTH>>(MAX_DEPTH)),ti->y_ctb*(LCU_WIDTH>>(MAX_DEPTH)),width,pred,pred_stride,ti->intra_pred_mode,0);
|
2013-05-21 12:57:22 +00:00
|
|
|
|
|
|
|
/* Filter DC-prediction */
|
2013-09-18 11:49:01 +00:00
|
|
|
if(ti->intra_pred_mode == 1 && width < 32)
|
2013-05-21 12:57:22 +00:00
|
|
|
{
|
2013-09-18 12:07:43 +00:00
|
|
|
intra_dc_pred_filtering(recShift,(LCU_WIDTH>>(depth))*2+8,pred,width,LCU_WIDTH>>depth,LCU_WIDTH>>depth);
|
2013-05-21 12:57:22 +00:00
|
|
|
}
|
2013-09-18 11:49:01 +00:00
|
|
|
if(ti->intra_pred_mode_chroma != 36 && ti->intra_pred_mode_chroma == ti->intra_pred_mode)
|
2013-05-21 12:57:22 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
ti->intra_pred_mode_chroma = 36;
|
2013-05-21 12:57:22 +00:00
|
|
|
}
|
2013-09-18 12:07:43 +00:00
|
|
|
intra_build_reference_border(encoder->in.cur_pic, ti->x_ctb, ti->y_ctb,(LCU_WIDTH>>(depth+1))*2+8, rec, (LCU_WIDTH>>(depth+1))*2+8, 1);
|
2013-09-18 11:49:01 +00:00
|
|
|
intra_recon(recShiftU,(LCU_WIDTH>>(depth+1))*2+8,ti->x_ctb*(LCU_WIDTH>>(MAX_DEPTH+1)),ti->y_ctb*(LCU_WIDTH>>(MAX_DEPTH+1)),width>>1,predU,pred_stride>>1,ti->intra_pred_mode_chroma!=36?ti->intra_pred_mode_chroma:ti->intra_pred_mode,1);
|
2013-09-18 12:07:43 +00:00
|
|
|
intra_build_reference_border(encoder->in.cur_pic, ti->x_ctb, ti->y_ctb,(LCU_WIDTH>>(depth+1))*2+8, rec, (LCU_WIDTH>>(depth+1))*2+8, 2);
|
2013-09-18 11:49:01 +00:00
|
|
|
intra_recon(recShiftU,(LCU_WIDTH>>(depth+1))*2+8,ti->x_ctb*(LCU_WIDTH>>(MAX_DEPTH+1)),ti->y_ctb*(LCU_WIDTH>>(MAX_DEPTH+1)),width>>1,predV,pred_stride>>1,ti->intra_pred_mode_chroma!=36?ti->intra_pred_mode_chroma:ti->intra_pred_mode,1);
|
2013-05-21 12:57:22 +00:00
|
|
|
|
|
|
|
/* This affects reconstruction, do after that */
|
2013-09-20 08:07:31 +00:00
|
|
|
picture_set_block_coded(encoder->in.cur_pic, ti->x_ctb, ti->y_ctb, depth, 1);
|
2013-05-21 12:57:22 +00:00
|
|
|
|
|
|
|
/* INTRA PREDICTION ENDS HERE */
|
|
|
|
|
2013-03-25 10:48:19 +00:00
|
|
|
/* Get residual by subtracting prediction */
|
2013-05-21 12:57:22 +00:00
|
|
|
i = 0;
|
2013-06-12 12:41:57 +00:00
|
|
|
ac_sum = 0;
|
2013-03-25 10:48:19 +00:00
|
|
|
for(y = 0; y < LCU_WIDTH>>depth; y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>depth; x++)
|
|
|
|
{
|
2013-06-12 12:41:57 +00:00
|
|
|
block[i]=((int16_t)base[x+y*base_stride])-pred[x+y*pred_stride];
|
|
|
|
i++;
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 12:57:22 +00:00
|
|
|
/* Transform and quant residual to coeffs */
|
2013-03-25 10:48:19 +00:00
|
|
|
transform2d(block,pre_quant_coeff,width,0);
|
2013-06-12 12:41:57 +00:00
|
|
|
quant(encoder,pre_quant_coeff,coeff,width, width,&ac_sum, 0, SCAN_DIAG);
|
2013-03-25 10:48:19 +00:00
|
|
|
|
|
|
|
/* Check for non-zero coeffs */
|
2013-03-25 15:17:24 +00:00
|
|
|
for(i = 0; i < width*width; i++)
|
2013-03-25 10:48:19 +00:00
|
|
|
{
|
|
|
|
if(coeff[i] != 0)
|
|
|
|
{
|
|
|
|
/* Found one, we can break here */
|
|
|
|
CbY = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if non-zero coeffs */
|
|
|
|
if(CbY)
|
|
|
|
{
|
|
|
|
/* RECONSTRUCT for predictions */
|
|
|
|
dequant(encoder,coeff,pre_quant_coeff,width, width,0);
|
|
|
|
itransform2d(block,pre_quant_coeff,width,0);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(y = 0; y < LCU_WIDTH>>depth; y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>depth; x++)
|
|
|
|
{
|
2013-03-25 15:17:24 +00:00
|
|
|
int16_t val = block[i++]+pred[x+y*pred_stride];
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: support 10+bits
|
2013-04-18 11:37:56 +00:00
|
|
|
recbase[x+y*recbase_stride] = (uint8_t)/*(val&0xff);//*/CLIP(0,255,val);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END RECONTRUCTION */
|
|
|
|
}
|
|
|
|
/* without coeffs, we only use the prediction */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(y = 0; y < LCU_WIDTH>>depth; y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>depth; x++)
|
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
recbase[x+y*recbase_stride] = (uint8_t)CLIP(0,255,pred[x+y*pred_stride]);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(encoder->in.video_format != FORMAT_400)
|
|
|
|
{
|
|
|
|
/* U */
|
|
|
|
i = 0;
|
2013-06-12 12:41:57 +00:00
|
|
|
ac_sum = 0;
|
2013-03-25 10:48:19 +00:00
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
2013-06-12 12:41:57 +00:00
|
|
|
block[i]=((int16_t)baseU[x+y*(base_stride>>1)])-predU[x+y*(pred_stride>>1)];
|
|
|
|
i++;
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-09 12:59:12 +00:00
|
|
|
transform2d(block,pre_quant_coeff,LCU_WIDTH>>(depth+1),65535);
|
2013-06-12 12:41:57 +00:00
|
|
|
quant(encoder,pre_quant_coeff,coeffU, width>>1, width>>1, &ac_sum,2,SCAN_DIAG);
|
2013-03-25 15:17:24 +00:00
|
|
|
for(i = 0; i < width*width>>2; i++)
|
2013-03-25 10:48:19 +00:00
|
|
|
{
|
|
|
|
if(coeffU[i] != 0)
|
|
|
|
{
|
|
|
|
/* Found one, we can break here */
|
|
|
|
CbU = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 12:57:22 +00:00
|
|
|
/* V */
|
2013-03-25 10:48:19 +00:00
|
|
|
i = 0;
|
2013-06-12 12:41:57 +00:00
|
|
|
ac_sum = 0;
|
2013-03-25 10:48:19 +00:00
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
2013-06-12 12:41:57 +00:00
|
|
|
block[i]=((int16_t)baseV[x+y*(base_stride>>1)])-predV[x+y*(pred_stride>>1)];
|
|
|
|
i++;
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-09 12:59:12 +00:00
|
|
|
transform2d(block,pre_quant_coeff,LCU_WIDTH>>(depth+1),65535);
|
2013-06-12 12:41:57 +00:00
|
|
|
quant(encoder,pre_quant_coeff,coeffV, width>>1, width>>1, &ac_sum,3,SCAN_DIAG);
|
2013-03-25 15:17:24 +00:00
|
|
|
for(i = 0; i < width*width>>2; i++)
|
2013-03-25 10:48:19 +00:00
|
|
|
{
|
|
|
|
if(coeffV[i] != 0)
|
|
|
|
{
|
|
|
|
/* Found one, we can break here */
|
|
|
|
CbV = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(CbU)
|
|
|
|
{
|
|
|
|
/* RECONSTRUCT for predictions */
|
|
|
|
dequant(encoder,coeffU,pre_quant_coeff,width>>1, width>>1,2);
|
2013-04-09 12:59:12 +00:00
|
|
|
itransform2d(block,pre_quant_coeff,LCU_WIDTH>>(depth+1),65535);
|
2013-03-25 10:48:19 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
2013-03-25 15:17:24 +00:00
|
|
|
int16_t val = block[i++]+predU[x+y*(pred_stride>>1)];
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: support 10+bits
|
2013-04-02 11:50:09 +00:00
|
|
|
recbaseU[x+y*(recbase_stride>>1)] = (uint8_t)CLIP(0,255,val);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END RECONTRUCTION */
|
|
|
|
}
|
|
|
|
/* without coeffs, we only use the prediction */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
recbaseU[x+y*(recbase_stride>>1)] = (uint8_t)CLIP(0,255,predU[x+y*(pred_stride>>1)]);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-21 12:57:22 +00:00
|
|
|
|
2013-03-25 10:48:19 +00:00
|
|
|
if(CbV)
|
|
|
|
{
|
|
|
|
/* RECONSTRUCT for predictions */
|
|
|
|
dequant(encoder,coeffV,pre_quant_coeff,width>>1, width>>1,3);
|
2013-04-09 12:59:12 +00:00
|
|
|
itransform2d(block,pre_quant_coeff,LCU_WIDTH>>(depth+1),65535);
|
2013-03-25 10:48:19 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
2013-03-25 15:17:24 +00:00
|
|
|
int16_t val = block[i++]+predV[x+y*(pred_stride>>1)];
|
2013-09-09 11:22:53 +00:00
|
|
|
//TODO: support 10+bits
|
2013-04-02 11:50:09 +00:00
|
|
|
recbaseV[x+y*(recbase_stride>>1)] = (uint8_t)CLIP(0,255,val);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END RECONTRUCTION */
|
|
|
|
}
|
|
|
|
/* without coeffs, we only use the prediction */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(y = 0; y < LCU_WIDTH>>(depth+1); y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < LCU_WIDTH>>(depth+1); x++)
|
|
|
|
{
|
2013-04-02 11:50:09 +00:00
|
|
|
recbaseV[x+y*(recbase_stride>>1)] = (uint8_t)CLIP(0,255,predV[x+y*(pred_stride>>1)]);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-02 06:53:36 +00:00
|
|
|
|
|
|
|
/* Store coded block pattern */
|
2013-04-02 11:50:09 +00:00
|
|
|
ti->cb[ti->idx] = CbY | (CbU<<1) | (CbV<<2);
|
2013-03-25 10:48:19 +00:00
|
|
|
/* END INTRAPREDICTION */
|
2013-04-02 11:50:09 +00:00
|
|
|
return;
|
2013-03-25 15:17:24 +00:00
|
|
|
}
|
2013-03-25 10:48:19 +00:00
|
|
|
|
2013-03-25 15:17:24 +00:00
|
|
|
/* end Residual Coding */
|
2013-04-02 11:50:09 +00:00
|
|
|
|
2013-03-25 15:17:24 +00:00
|
|
|
}
|
2013-03-25 10:48:19 +00:00
|
|
|
|
|
|
|
|
2013-04-02 11:50:09 +00:00
|
|
|
void encode_transform_coeff(encoder_control* encoder,transform_info* ti,int8_t depth, int8_t trDepth)
|
2013-03-25 15:17:24 +00:00
|
|
|
{
|
2013-03-26 14:53:07 +00:00
|
|
|
int8_t width = LCU_WIDTH>>depth;
|
2013-04-02 11:50:09 +00:00
|
|
|
int8_t split = (ti->split[ti->idx]&(1<<depth))?1:0;
|
|
|
|
int8_t CbY,CbU,CbV;
|
2013-05-16 12:27:54 +00:00
|
|
|
int32_t coeff_fourth = ((LCU_WIDTH>>(depth))*(LCU_WIDTH>>(depth)))+1;
|
2013-04-02 11:50:09 +00:00
|
|
|
|
2013-04-04 12:08:28 +00:00
|
|
|
if(depth != 0 && depth != MAX_DEPTH+1)
|
2013-03-26 14:53:07 +00:00
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
cabac.ctx = &g_trans_subdiv_model[5-((g_convert_to_bit[LCU_WIDTH]+2)-depth)];
|
2013-03-26 14:53:07 +00:00
|
|
|
CABAC_BIN(&cabac,split,"TransformSubdivFlag");
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:17:24 +00:00
|
|
|
/* Signal if chroma data is present */
|
2013-05-21 12:57:22 +00:00
|
|
|
/* Chroma data is also signaled BEFORE transform split */
|
|
|
|
/* Chroma data is not signaled if it was set to 0 before split */
|
2013-03-26 14:53:07 +00:00
|
|
|
if(encoder->in.video_format != FORMAT_400)
|
2013-03-25 15:17:24 +00:00
|
|
|
{
|
2013-05-21 12:57:22 +00:00
|
|
|
/* Non-zero chroma U Tcoeffs */
|
|
|
|
int8_t Cb_flag = (trDepth==0)?ti->cb_top[1]:((ti->cb[ti->idx]&0x2)?1:0);
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_qt_cbf_model_chroma[trDepth];
|
2013-04-02 11:50:09 +00:00
|
|
|
if(trDepth == 0 || ti->cb_top[1])
|
|
|
|
{
|
|
|
|
CABAC_BIN(&cabac,Cb_flag,"cbf_chroma_u");
|
|
|
|
}
|
2013-03-26 14:53:07 +00:00
|
|
|
/* Non-zero chroma V Tcoeffs */
|
2013-05-21 12:57:22 +00:00
|
|
|
/* NOTE: Using the same ctx as before */
|
|
|
|
Cb_flag = (trDepth==0)?ti->cb_top[2]:((ti->cb[ti->idx]&0x4)?1:0);
|
2013-04-02 11:50:09 +00:00
|
|
|
if(trDepth == 0 || ti->cb_top[2])
|
|
|
|
{
|
|
|
|
CABAC_BIN(&cabac,Cb_flag,"cbf_chroma_v");
|
|
|
|
}
|
2013-03-25 15:17:24 +00:00
|
|
|
}
|
2013-03-26 14:53:07 +00:00
|
|
|
|
|
|
|
if(split)
|
2013-04-02 11:50:09 +00:00
|
|
|
{
|
|
|
|
ti->idx = 0; encode_transform_coeff(encoder,ti,depth+1,trDepth+1);
|
|
|
|
ti->idx = 1; encode_transform_coeff(encoder,ti,depth+1,trDepth+1);
|
|
|
|
ti->idx = 2; encode_transform_coeff(encoder,ti,depth+1,trDepth+1);
|
|
|
|
ti->idx = 3; encode_transform_coeff(encoder,ti,depth+1,trDepth+1);
|
2013-03-26 14:53:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-02 11:50:09 +00:00
|
|
|
CbY = ti->cb[ti->idx]&0x1;
|
|
|
|
CbU = (ti->cb[ti->idx]&0x2)?1:0;
|
|
|
|
CbV = (ti->cb[ti->idx]&0x4)?1:0;
|
2013-03-25 15:17:24 +00:00
|
|
|
|
|
|
|
/* Non-zero luma Tcoeffs */
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac.ctx = &g_qt_cbf_model_luma[trDepth?0:1];
|
2013-03-25 15:17:24 +00:00
|
|
|
CABAC_BIN(&cabac,CbY,"cbf_luma");
|
|
|
|
|
|
|
|
{
|
2013-03-25 10:48:19 +00:00
|
|
|
uint32_t uiCTXIdx;
|
|
|
|
uint32_t uiScanIdx = SCAN_DIAG;
|
|
|
|
uint32_t uiDirMode;
|
|
|
|
switch(width)
|
|
|
|
{
|
|
|
|
case 2: uiCTXIdx = 6; break;
|
|
|
|
case 4: uiCTXIdx = 5; break;
|
|
|
|
case 8: uiCTXIdx = 4; break;
|
|
|
|
case 16: uiCTXIdx = 3; break;
|
|
|
|
case 32: uiCTXIdx = 2; break;
|
|
|
|
case 64: uiCTXIdx = 1; break;
|
|
|
|
default: uiCTXIdx = 0; break;
|
|
|
|
}
|
2013-05-16 12:27:54 +00:00
|
|
|
uiCTXIdx -= trDepth;
|
2013-03-25 10:48:19 +00:00
|
|
|
/* CoeffNxN */
|
|
|
|
/* Residual Coding */
|
|
|
|
if(CbY)
|
|
|
|
{
|
|
|
|
/* Luma (Intra) scanmode */
|
2013-09-18 11:49:01 +00:00
|
|
|
uiDirMode = ti->intra_pred_mode;
|
2013-03-25 10:48:19 +00:00
|
|
|
if (uiCTXIdx >3 && uiCTXIdx < 6) //if multiple scans supported for transform size
|
|
|
|
{
|
|
|
|
uiScanIdx = abs((int32_t) uiDirMode - 26) < 5 ? 1 : (abs((int32_t)uiDirMode - 10) < 5 ? 2 : 0);
|
|
|
|
}
|
2013-04-02 11:50:09 +00:00
|
|
|
encode_CoeffNxN(encoder,&ti->coeff[0][ti->idx*coeff_fourth], width, 0, uiScanIdx);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
if(CbU||CbV)
|
|
|
|
{
|
2013-04-05 11:55:58 +00:00
|
|
|
int8_t chromaWidth = width>>1;
|
2013-03-25 10:48:19 +00:00
|
|
|
/* Chroma scanmode */
|
|
|
|
uiCTXIdx++;
|
2013-09-18 11:49:01 +00:00
|
|
|
uiDirMode = ti->intra_pred_mode_chroma;
|
2013-03-25 10:48:19 +00:00
|
|
|
if(uiDirMode==36)
|
|
|
|
{
|
2013-09-09 11:22:53 +00:00
|
|
|
/* TODO: support NxN */
|
2013-09-18 11:49:01 +00:00
|
|
|
uiDirMode = ti->intra_pred_mode;
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
uiScanIdx = SCAN_DIAG;
|
|
|
|
if (uiCTXIdx >4 && uiCTXIdx < 7) //if multiple scans supported for transform size
|
|
|
|
{
|
|
|
|
uiScanIdx = abs((int32_t) uiDirMode - 26) < 5 ? 1 : (abs((int32_t)uiDirMode - 10) < 5 ? 2 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(CbU)
|
|
|
|
{
|
2013-04-05 11:55:58 +00:00
|
|
|
encode_CoeffNxN(encoder,&ti->coeff[1][ti->idx*coeff_fourth>>1], chromaWidth, 2, uiScanIdx);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
if(CbV)
|
|
|
|
{
|
2013-04-05 11:55:58 +00:00
|
|
|
encode_CoeffNxN(encoder,&ti->coeff[2][ti->idx*coeff_fourth>>1], chromaWidth, 2, uiScanIdx);
|
2013-03-25 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
void encode_CoeffNxN(encoder_control* encoder,int16_t* coeff, uint8_t width, uint8_t type, int8_t scanMode)
|
2013-03-11 10:06:08 +00:00
|
|
|
{
|
2013-03-26 14:53:07 +00:00
|
|
|
int c1 = 1;
|
2013-03-11 10:06:08 +00:00
|
|
|
uint8_t last_coeff_x = 0;
|
|
|
|
uint8_t last_coeff_y = 0;
|
|
|
|
int32_t i;
|
|
|
|
uint32_t sig_coeffgroup_flag[64];
|
2013-03-26 14:53:07 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
uint32_t num_nonzero = 0;
|
2013-03-26 14:53:07 +00:00
|
|
|
int32_t scanPosLast = -1;
|
2013-03-11 10:06:08 +00:00
|
|
|
int32_t posLast = 0;
|
2013-03-26 14:53:07 +00:00
|
|
|
int32_t shift = 4>>1;
|
|
|
|
int8_t beValid = ENABLE_SIGN_HIDING;
|
2013-03-11 10:06:08 +00:00
|
|
|
int32_t iScanPosSig;
|
|
|
|
int32_t iLastScanSet;
|
|
|
|
uint32_t uiGoRiceParam = 0;
|
2013-03-26 14:53:07 +00:00
|
|
|
uint32_t uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
|
2013-03-07 15:42:00 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
/* CONSTANTS */
|
2013-03-22 14:36:35 +00:00
|
|
|
const uint32_t uiNumBlkSide = width >> shift;
|
2013-09-18 11:49:01 +00:00
|
|
|
const uint32_t uiLog2BlockSize = g_convert_to_bit[ width ] + 2;
|
|
|
|
const uint32_t* scan = g_sig_last_scan[ scanMode ][ uiLog2BlockSize - 1 ];
|
2013-03-22 14:36:35 +00:00
|
|
|
const uint32_t* scanCG = NULL;
|
2013-03-07 15:42:00 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
/* Init base contexts according to block type */
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac_ctx* baseCoeffGroupCtx = &g_cu_sig_coeff_group_model[type];
|
|
|
|
cabac_ctx* baseCtx = (type==0) ? &g_cu_sig_model_luma[0] :&g_cu_sig_model_chroma[0];
|
2013-03-11 10:06:08 +00:00
|
|
|
memset(sig_coeffgroup_flag,0,sizeof(uint32_t)*64);
|
2013-03-20 15:27:47 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
/* Count non-zero coeffs */
|
2013-03-11 14:26:09 +00:00
|
|
|
for(i = 0; i < width*width; i++)
|
2013-03-11 10:06:08 +00:00
|
|
|
{
|
|
|
|
if(coeff[i] != 0)
|
|
|
|
{
|
|
|
|
num_nonzero++;
|
|
|
|
}
|
2013-05-07 07:36:04 +00:00
|
|
|
}
|
2012-06-15 11:54:57 +00:00
|
|
|
|
2013-09-18 11:49:01 +00:00
|
|
|
scanCG = g_sig_last_scan[ scanMode ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-3 : 0 ];
|
2013-03-11 10:06:08 +00:00
|
|
|
if( uiLog2BlockSize == 3 )
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
scanCG = g_sig_last_scan_8x8[ scanMode ];
|
2013-03-11 10:06:08 +00:00
|
|
|
}
|
|
|
|
else if( uiLog2BlockSize == 5 )
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
scanCG = g_sig_last_scan_32x32;
|
2013-03-11 10:06:08 +00:00
|
|
|
}
|
2013-02-05 13:48:06 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
scanPosLast = -1;
|
|
|
|
/* Significance mapping */
|
|
|
|
while(num_nonzero > 0)
|
|
|
|
{
|
|
|
|
posLast = scan[ ++scanPosLast ];
|
|
|
|
#define POSY (posLast >> uiLog2BlockSize)
|
|
|
|
#define POSX (posLast - ( POSY << uiLog2BlockSize ))
|
|
|
|
if( coeff[ posLast ] != 0 )
|
|
|
|
{
|
|
|
|
sig_coeffgroup_flag[(uiNumBlkSide * (POSY >> shift) + (POSX >> shift))] = 1;
|
|
|
|
}
|
|
|
|
num_nonzero -= ( coeff[ posLast ] != 0 )?1:0;
|
|
|
|
#undef POSY
|
|
|
|
#undef POSX
|
|
|
|
}
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
last_coeff_x = posLast & (width-1);
|
|
|
|
last_coeff_y = posLast>> uiLog2BlockSize;
|
|
|
|
|
|
|
|
/* Code last_coeff_x and last_coeff_y */
|
2013-04-25 13:29:44 +00:00
|
|
|
encode_lastSignificantXY(encoder,last_coeff_x, last_coeff_y, width, width, type, scanMode);
|
2013-05-07 07:36:04 +00:00
|
|
|
|
2013-03-22 14:36:35 +00:00
|
|
|
iScanPosSig = scanPosLast;
|
2013-03-11 10:06:08 +00:00
|
|
|
iLastScanSet = (scanPosLast >> 4);
|
|
|
|
/* significant_coeff_flag */
|
|
|
|
for(i = iLastScanSet; i >= 0; i-- )
|
|
|
|
{
|
2013-03-22 14:36:35 +00:00
|
|
|
int32_t iSubPos = i << 4 /*LOG2_SCAN_SET_SIZE*/;
|
2013-03-11 10:06:08 +00:00
|
|
|
int32_t abs_coeff[16];
|
2013-03-22 14:36:35 +00:00
|
|
|
int32_t iCGBlkPos = scanCG[ i ];
|
|
|
|
int32_t iCGPosY = iCGBlkPos / uiNumBlkSide;
|
|
|
|
int32_t iCGPosX = iCGBlkPos - (iCGPosY * uiNumBlkSide);
|
|
|
|
uint32_t coeffSigns = 0;
|
2013-03-11 10:06:08 +00:00
|
|
|
int32_t lastNZPosInCG = -1, firstNZPosInCG = 16;
|
2013-03-22 14:36:35 +00:00
|
|
|
int32_t numNonZero = 0;
|
|
|
|
uiGoRiceParam = 0;
|
2013-03-11 10:06:08 +00:00
|
|
|
|
|
|
|
if( iScanPosSig == scanPosLast )
|
|
|
|
{
|
|
|
|
abs_coeff[ 0 ] = abs( coeff[ posLast ] );
|
2013-05-07 07:36:04 +00:00
|
|
|
coeffSigns = ( coeff[ posLast ] < 0 );
|
2013-03-22 14:36:35 +00:00
|
|
|
numNonZero = 1;
|
2013-03-11 10:06:08 +00:00
|
|
|
lastNZPosInCG = iScanPosSig;
|
|
|
|
firstNZPosInCG = iScanPosSig;
|
|
|
|
iScanPosSig--;
|
|
|
|
}
|
|
|
|
if( i == iLastScanSet || i == 0)
|
|
|
|
{
|
|
|
|
sig_coeffgroup_flag[ iCGBlkPos ] = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint32_t uiSigCoeffGroup = (sig_coeffgroup_flag[ iCGBlkPos ] != 0);
|
2013-09-18 11:21:03 +00:00
|
|
|
uint32_t uiCtxSig = context_get_sig_coeff_group(sig_coeffgroup_flag, iCGPosX, iCGPosY,width);
|
2013-03-11 10:06:08 +00:00
|
|
|
cabac.ctx = &baseCoeffGroupCtx[ uiCtxSig ];
|
|
|
|
CABAC_BIN(&cabac,uiSigCoeffGroup,"significant_coeff_group");
|
|
|
|
}
|
2013-02-05 13:48:06 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
if( sig_coeffgroup_flag[ iCGBlkPos ] )
|
|
|
|
{
|
2013-09-18 11:21:03 +00:00
|
|
|
int32_t patternSigCtx = context_calc_pattern_sig_ctx( sig_coeffgroup_flag, iCGPosX, iCGPosY, width);
|
2013-03-11 10:06:08 +00:00
|
|
|
for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
|
|
|
|
{
|
|
|
|
uiBlkPos = scan[ iScanPosSig ];
|
|
|
|
uiPosY = uiBlkPos >> uiLog2BlockSize;
|
|
|
|
uiPosX = uiBlkPos - ( uiPosY << uiLog2BlockSize );
|
|
|
|
uiSig = (coeff[ uiBlkPos ] != 0)?1:0;
|
|
|
|
if( iScanPosSig > iSubPos || i == 0 || numNonZero )
|
|
|
|
{
|
2013-09-18 11:21:03 +00:00
|
|
|
uiCtxSig = context_get_sig_ctx_inc( patternSigCtx, scanMode, uiPosX, uiPosY, uiLog2BlockSize, width, type );
|
2013-03-11 10:06:08 +00:00
|
|
|
cabac.ctx = &baseCtx[ uiCtxSig ];
|
|
|
|
CABAC_BIN(&cabac,uiSig,"significant_coeff_flag");
|
|
|
|
}
|
|
|
|
if( uiSig )
|
|
|
|
{
|
|
|
|
abs_coeff[ numNonZero ] = abs( coeff[ uiBlkPos ] );
|
2013-03-22 14:36:35 +00:00
|
|
|
coeffSigns = 2 * coeffSigns + ( coeff[ uiBlkPos ] < 0 );
|
2013-03-11 10:06:08 +00:00
|
|
|
numNonZero++;
|
|
|
|
if( lastNZPosInCG == -1 )
|
|
|
|
{
|
|
|
|
lastNZPosInCG = iScanPosSig;
|
|
|
|
}
|
2013-03-22 14:36:35 +00:00
|
|
|
firstNZPosInCG = iScanPosSig;
|
2013-03-11 10:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iScanPosSig = iSubPos - 1;
|
2013-03-22 14:36:35 +00:00
|
|
|
}
|
2012-06-11 15:43:29 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
if( numNonZero > 0 )
|
|
|
|
{
|
2013-08-02 13:35:30 +00:00
|
|
|
int8_t signHidden = ( lastNZPosInCG - firstNZPosInCG >= 4 /*SBH_THRESHOLD*/ )?1:0;
|
2013-03-22 14:36:35 +00:00
|
|
|
uint32_t uiCtxSet = (i > 0 && type==0) ? 2 : 0;
|
2013-03-11 10:06:08 +00:00
|
|
|
cabac_ctx* baseCtxMod;
|
|
|
|
int32_t numC1Flag,firstC2FlagIdx,idx,iFirstCoeff2;
|
|
|
|
if( c1 == 0 )
|
|
|
|
{
|
|
|
|
uiCtxSet++;
|
|
|
|
}
|
|
|
|
c1 = 1;
|
|
|
|
|
2013-09-18 11:21:03 +00:00
|
|
|
baseCtxMod = ( type==0 ) ? &g_cu_one_model_luma[4 * uiCtxSet] : &g_cu_one_model_chroma[4 * uiCtxSet];
|
2013-03-22 14:36:35 +00:00
|
|
|
numC1Flag = MIN(numNonZero, C1FLAG_NUMBER);
|
2013-03-11 10:06:08 +00:00
|
|
|
firstC2FlagIdx = -1;
|
|
|
|
for(idx = 0; idx < numC1Flag; idx++ )
|
|
|
|
{
|
|
|
|
uint32_t uiSymbol = (abs_coeff[ idx ] > 1)?1:0;
|
|
|
|
cabac.ctx = &baseCtxMod[c1];
|
|
|
|
CABAC_BIN(&cabac,uiSymbol,"significant_coeff2_flag");
|
|
|
|
if( uiSymbol )
|
|
|
|
{
|
|
|
|
c1 = 0;
|
|
|
|
if (firstC2FlagIdx == -1)
|
|
|
|
{
|
|
|
|
firstC2FlagIdx = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( (c1 < 3) && (c1 > 0) )
|
|
|
|
{
|
|
|
|
c1++;
|
|
|
|
}
|
|
|
|
}
|
2013-02-05 13:48:06 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
if (c1 == 0)
|
|
|
|
{
|
2013-09-18 11:21:03 +00:00
|
|
|
baseCtxMod = ( type==0 ) ? &g_cu_abs_model_luma[uiCtxSet] : &g_cu_abs_model_chroma[uiCtxSet];
|
2013-05-07 07:36:04 +00:00
|
|
|
if (firstC2FlagIdx != -1)
|
2013-03-11 10:06:08 +00:00
|
|
|
{
|
|
|
|
uint8_t symbol = (abs_coeff[ firstC2FlagIdx ] > 2)?1:0;
|
2013-03-22 14:36:35 +00:00
|
|
|
cabac.ctx = &baseCtxMod[0];
|
2013-03-11 10:06:08 +00:00
|
|
|
CABAC_BIN(&cabac,symbol,"first_c2_flag");
|
|
|
|
}
|
2013-03-21 09:56:19 +00:00
|
|
|
}
|
2013-02-05 13:48:06 +00:00
|
|
|
|
2013-05-07 07:36:04 +00:00
|
|
|
if(beValid && signHidden)
|
2013-03-11 10:06:08 +00:00
|
|
|
{
|
|
|
|
CABAC_BINS_EP(&cabac,(coeffSigns >> 1),(numNonZero-1),"");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CABAC_BINS_EP(&cabac,coeffSigns,numNonZero,"");
|
|
|
|
}
|
2013-03-05 15:09:01 +00:00
|
|
|
|
2013-03-11 10:06:08 +00:00
|
|
|
if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
|
|
|
|
{
|
|
|
|
iFirstCoeff2 = 1;
|
|
|
|
for (idx = 0; idx < numNonZero; idx++ )
|
|
|
|
{
|
|
|
|
int32_t baseLevel = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
|
|
|
|
|
|
|
|
if( abs_coeff[ idx ] >= baseLevel)
|
|
|
|
{
|
2013-09-18 11:06:45 +00:00
|
|
|
cabac_write_coeff_remain(&cabac, abs_coeff[ idx ] - baseLevel, uiGoRiceParam );
|
2013-03-11 10:06:08 +00:00
|
|
|
if(abs_coeff[idx] > 3*(1<<uiGoRiceParam))
|
|
|
|
{
|
2013-03-20 15:27:47 +00:00
|
|
|
uiGoRiceParam = MIN(uiGoRiceParam+1, 4);
|
2013-02-05 13:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 10:06:08 +00:00
|
|
|
if(abs_coeff[ idx ] >= 2)
|
|
|
|
{
|
|
|
|
iFirstCoeff2 = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 11:54:57 +00:00
|
|
|
}
|
2012-08-15 14:18:58 +00:00
|
|
|
}
|
2013-03-11 10:06:08 +00:00
|
|
|
}
|
2013-02-05 13:48:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
/*!
|
|
|
|
\brief Encode (X,Y) position of the last significant coefficient
|
|
|
|
\param lastpos_x X component of last coefficient
|
|
|
|
\param lastpos_y Y component of last coefficient
|
|
|
|
\param width Block width
|
|
|
|
\param height Block height
|
|
|
|
\param type plane type / luminance or chrominance
|
|
|
|
\param scan scan type (diag, hor, ver)
|
|
|
|
|
|
|
|
This method encodes the X and Y component within a block of the last significant coefficient.
|
|
|
|
*/
|
2012-06-14 10:09:34 +00:00
|
|
|
void encode_lastSignificantXY(encoder_control* encoder,uint8_t lastpos_x, uint8_t lastpos_y, uint8_t width, uint8_t height, uint8_t type, uint8_t scan)
|
|
|
|
{
|
|
|
|
uint8_t offset_x = type?0:((TOBITS(width)*3) + ((TOBITS(width)+1)>>2)),offset_y = offset_x;
|
|
|
|
uint8_t shift_x = type?(TOBITS(width)):((TOBITS(width)+3)>>2), shift_y = shift_x;
|
2013-03-21 09:56:19 +00:00
|
|
|
int uiGroupIdxX;
|
|
|
|
int uiGroupIdxY;
|
2012-06-14 10:09:34 +00:00
|
|
|
int last_x,last_y,i;
|
2013-09-18 11:21:03 +00:00
|
|
|
cabac_ctx* basectxX = (type?g_cu_ctx_last_x_chroma:g_cu_ctx_last_x_luma);
|
|
|
|
cabac_ctx* basectxY = (type?g_cu_ctx_last_y_chroma:g_cu_ctx_last_y_luma);
|
2012-06-14 10:09:34 +00:00
|
|
|
|
2013-03-20 15:27:47 +00:00
|
|
|
if( scan == SCAN_VER )
|
|
|
|
{
|
2013-03-21 09:56:19 +00:00
|
|
|
SWAP( lastpos_x, lastpos_y,uint8_t );
|
2013-03-20 15:27:47 +00:00
|
|
|
}
|
2013-09-18 11:49:01 +00:00
|
|
|
uiGroupIdxX = g_group_idx[lastpos_x];
|
|
|
|
uiGroupIdxY = g_group_idx[lastpos_y];
|
2013-03-20 15:27:47 +00:00
|
|
|
|
2012-06-14 10:09:34 +00:00
|
|
|
/* Last X binarization */
|
|
|
|
for(last_x = 0; last_x < uiGroupIdxX ; last_x++)
|
|
|
|
{
|
2013-03-11 14:26:09 +00:00
|
|
|
cabac.ctx = &basectxX[offset_x+(last_x>>shift_x)];
|
2012-06-14 10:09:34 +00:00
|
|
|
CABAC_BIN(&cabac,1,"LastSignificantX");
|
|
|
|
}
|
2013-09-18 11:49:01 +00:00
|
|
|
if(uiGroupIdxX < g_group_idx[width-1])
|
2012-06-14 10:09:34 +00:00
|
|
|
{
|
2013-03-11 14:26:09 +00:00
|
|
|
cabac.ctx = &basectxX[offset_x+(last_x>>shift_x)];
|
2012-06-14 10:09:34 +00:00
|
|
|
CABAC_BIN(&cabac,0,"LastSignificantX");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last Y binarization */
|
|
|
|
for(last_y = 0; last_y < uiGroupIdxY ; last_y++)
|
|
|
|
{
|
2013-03-11 14:26:09 +00:00
|
|
|
cabac.ctx = &basectxY[offset_y+(last_y>>shift_y)];
|
2012-06-14 10:09:34 +00:00
|
|
|
CABAC_BIN(&cabac,1,"LastSignificantY");
|
|
|
|
}
|
2013-09-18 11:49:01 +00:00
|
|
|
if(uiGroupIdxY < g_group_idx[height-1])
|
2012-06-14 10:09:34 +00:00
|
|
|
{
|
2013-03-11 14:26:09 +00:00
|
|
|
cabac.ctx = &basectxY[offset_y+(last_y>>shift_y)];
|
2012-06-14 10:09:34 +00:00
|
|
|
CABAC_BIN(&cabac,0,"LastSignificantY");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last X */
|
|
|
|
if(uiGroupIdxX > 3)
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
lastpos_x -= g_min_in_group[uiGroupIdxX];
|
2012-06-14 10:09:34 +00:00
|
|
|
for(i = ((uiGroupIdxX-2)>>1)-1; i>=0; i--)
|
|
|
|
{
|
|
|
|
CABAC_BIN_EP(&cabac,(lastpos_x>>i) & 1,"LastSignificantX");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Last Y */
|
|
|
|
if(uiGroupIdxY > 3)
|
|
|
|
{
|
2013-09-18 11:49:01 +00:00
|
|
|
lastpos_y -= g_min_in_group[uiGroupIdxY];
|
2012-06-14 10:09:34 +00:00
|
|
|
for(i = ((uiGroupIdxY-2)>>1)-1; i>=0; i--)
|
|
|
|
{
|
|
|
|
CABAC_BIN_EP(&cabac,(lastpos_y>>i) & 1,"LastSignificantY");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* end LastSignificantXY */
|
|
|
|
}
|