// gcc pessimizes multi-dimensional arrays here, even with constant indices
#define ZIG(i,y,x) level[i] = dct[0][x*8+y];
+#define ZIGZAG8_FRAME\
+ ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0)\
+ ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2)\
+ ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,4,0) ZIG(11,3,1)\
+ ZIG(12,2,2) ZIG(13,1,3) ZIG(14,0,4) ZIG(15,0,5)\
+ ZIG(16,1,4) ZIG(17,2,3) ZIG(18,3,2) ZIG(19,4,1)\
+ ZIG(20,5,0) ZIG(21,6,0) ZIG(22,5,1) ZIG(23,4,2)\
+ ZIG(24,3,3) ZIG(25,2,4) ZIG(26,1,5) ZIG(27,0,6)\
+ ZIG(28,0,7) ZIG(29,1,6) ZIG(30,2,5) ZIG(31,3,4)\
+ ZIG(32,4,3) ZIG(33,5,2) ZIG(34,6,1) ZIG(35,7,0)\
+ ZIG(36,7,1) ZIG(37,6,2) ZIG(38,5,3) ZIG(39,4,4)\
+ ZIG(40,3,5) ZIG(41,2,6) ZIG(42,1,7) ZIG(43,2,7)\
+ ZIG(44,3,6) ZIG(45,4,5) ZIG(46,5,4) ZIG(47,6,3)\
+ ZIG(48,7,2) ZIG(49,7,3) ZIG(50,6,4) ZIG(51,5,5)\
+ ZIG(52,4,6) ZIG(53,3,7) ZIG(54,4,7) ZIG(55,5,6)\
+ ZIG(56,6,5) ZIG(57,7,4) ZIG(58,7,5) ZIG(59,6,6)\
+ ZIG(60,5,7) ZIG(61,6,7) ZIG(62,7,6) ZIG(63,7,7)\
+
+#define ZIGZAG8_FIELD\
+ ZIG( 0,0,0) ZIG( 1,1,0) ZIG( 2,2,0) ZIG( 3,0,1)\
+ ZIG( 4,1,1) ZIG( 5,3,0) ZIG( 6,4,0) ZIG( 7,2,1)\
+ ZIG( 8,0,2) ZIG( 9,3,1) ZIG(10,5,0) ZIG(11,6,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ZIG(12,0,3) ZIG(13,1,3) ZIG(14,2,3) ZIG(15,3,3)
static void zigzag_scan_8x8_frame( int16_t level[64], int16_t dct[8][8] )
{
- ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0)
- ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2)
- ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,4,0) ZIG(11,3,1)
- ZIG(12,2,2) ZIG(13,1,3) ZIG(14,0,4) ZIG(15,0,5)
- ZIG(16,1,4) ZIG(17,2,3) ZIG(18,3,2) ZIG(19,4,1)
- ZIG(20,5,0) ZIG(21,6,0) ZIG(22,5,1) ZIG(23,4,2)
- ZIG(24,3,3) ZIG(25,2,4) ZIG(26,1,5) ZIG(27,0,6)
- ZIG(28,0,7) ZIG(29,1,6) ZIG(30,2,5) ZIG(31,3,4)
- ZIG(32,4,3) ZIG(33,5,2) ZIG(34,6,1) ZIG(35,7,0)
- ZIG(36,7,1) ZIG(37,6,2) ZIG(38,5,3) ZIG(39,4,4)
- ZIG(40,3,5) ZIG(41,2,6) ZIG(42,1,7) ZIG(43,2,7)
- ZIG(44,3,6) ZIG(45,4,5) ZIG(46,5,4) ZIG(47,6,3)
- ZIG(48,7,2) ZIG(49,7,3) ZIG(50,6,4) ZIG(51,5,5)
- ZIG(52,4,6) ZIG(53,3,7) ZIG(54,4,7) ZIG(55,5,6)
- ZIG(56,6,5) ZIG(57,7,4) ZIG(58,7,5) ZIG(59,6,6)
- ZIG(60,5,7) ZIG(61,6,7) ZIG(62,7,6) ZIG(63,7,7)
+ ZIGZAG8_FRAME
}
static void zigzag_scan_8x8_field( int16_t level[64], int16_t dct[8][8] )
{
- ZIG( 0,0,0) ZIG( 1,1,0) ZIG( 2,2,0) ZIG( 3,0,1)
- ZIG( 4,1,1) ZIG( 5,3,0) ZIG( 6,4,0) ZIG( 7,2,1)
- ZIG( 8,0,2) ZIG( 9,3,1) ZIG(10,5,0) ZIG(11,6,0)
- ZIG(12,7,0) ZIG(13,4,1) ZIG(14,1,2) ZIG(15,0,3)
- ZIG(16,2,2) ZIG(17,5,1) ZIG(18,6,1) ZIG(19,7,1)
- ZIG(20,3,2) ZIG(21,1,3) ZIG(22,0,4) ZIG(23,2,3)
- ZIG(24,4,2) ZIG(25,5,2) ZIG(26,6,2) ZIG(27,7,2)
- ZIG(28,3,3) ZIG(29,1,4) ZIG(30,0,5) ZIG(31,2,4)
- ZIG(32,4,3) ZIG(33,5,3) ZIG(34,6,3) ZIG(35,7,3)
- ZIG(36,3,4) ZIG(37,1,5) ZIG(38,0,6) ZIG(39,2,5)
- ZIG(40,4,4) ZIG(41,5,4) ZIG(42,6,4) ZIG(43,7,4)
- ZIG(44,3,5) ZIG(45,1,6) ZIG(46,2,6) ZIG(47,4,5)
- ZIG(48,5,5) ZIG(49,6,5) ZIG(50,7,5) ZIG(51,3,6)
- ZIG(52,0,7) ZIG(53,1,7) ZIG(54,4,6) ZIG(55,5,6)
- ZIG(56,6,6) ZIG(57,7,6) ZIG(58,2,7) ZIG(59,3,7)
- ZIG(60,4,7) ZIG(61,5,7) ZIG(62,6,7) ZIG(63,7,7)
+ ZIGZAG8_FIELD
}
#undef ZIG
static void zigzag_scan_4x4_frame( int16_t level[16], int16_t dct[4][4] )
{
- ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0)
- ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2)
- ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,3,1) ZIG(11,2,2)
- ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,2) ZIG(15,3,3)
+ ZIGZAG4_FRAME
}
static void zigzag_scan_4x4_field( int16_t level[16], int16_t dct[4][4] )
*(uint32_t*)(p_dst+0*FDEC_STRIDE) = *(uint32_t*)(p_src+0*FENC_STRIDE);\
*(uint32_t*)(p_dst+1*FDEC_STRIDE) = *(uint32_t*)(p_src+1*FENC_STRIDE);\
*(uint32_t*)(p_dst+2*FDEC_STRIDE) = *(uint32_t*)(p_src+2*FENC_STRIDE);\
- *(uint32_t*)(p_dst+3*FDEC_STRIDE) = *(uint32_t*)(p_src+3*FENC_STRIDE);\
+ *(uint32_t*)(p_dst+3*FDEC_STRIDE) = *(uint32_t*)(p_src+3*FENC_STRIDE);
+#define COPY8x8\
+ *(uint64_t*)(p_dst+0*FDEC_STRIDE) = *(uint64_t*)(p_src+0*FENC_STRIDE);\
+ *(uint64_t*)(p_dst+1*FDEC_STRIDE) = *(uint64_t*)(p_src+1*FENC_STRIDE);\
+ *(uint64_t*)(p_dst+2*FDEC_STRIDE) = *(uint64_t*)(p_src+2*FENC_STRIDE);\
+ *(uint64_t*)(p_dst+3*FDEC_STRIDE) = *(uint64_t*)(p_src+3*FENC_STRIDE);\
+ *(uint64_t*)(p_dst+4*FDEC_STRIDE) = *(uint64_t*)(p_src+4*FENC_STRIDE);\
+ *(uint64_t*)(p_dst+5*FDEC_STRIDE) = *(uint64_t*)(p_src+5*FENC_STRIDE);\
+ *(uint64_t*)(p_dst+6*FDEC_STRIDE) = *(uint64_t*)(p_src+6*FENC_STRIDE);\
+ *(uint64_t*)(p_dst+7*FDEC_STRIDE) = *(uint64_t*)(p_src+7*FENC_STRIDE);
static void zigzag_sub_4x4_frame( int16_t level[16], const uint8_t *p_src, uint8_t *p_dst )
{
- ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0)
- ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2)
- ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,3,1) ZIG(11,2,2)
- ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,2) ZIG(15,3,3)
+ ZIGZAG4_FRAME
COPY4x4
}
static void zigzag_sub_4x4_field( int16_t level[16], const uint8_t *p_src, uint8_t *p_dst )
{
- ZIG( 0,0,0) ZIG( 1,1,0) ZIG( 2,0,1) ZIG( 3,2,0)
- ZIG( 4,3,0) ZIG( 5,1,1) ZIG( 6,2,1) ZIG( 7,3,1)
- ZIG( 8,0,2) ZIG( 9,1,2) ZIG(10,2,2) ZIG(11,3,2)
- ZIG(12,0,3) ZIG(13,1,3) ZIG(14,2,3) ZIG(15,3,3)
+ ZIGZAG4_FIELD
COPY4x4
}
+static void zigzag_sub_8x8_frame( int16_t level[64], const uint8_t *p_src, uint8_t *p_dst )
+{
+ ZIGZAG8_FRAME
+ COPY8x8
+}
+static void zigzag_sub_8x8_field( int16_t level[64], const uint8_t *p_src, uint8_t *p_dst )
+{
+ ZIGZAG8_FIELD
+ COPY8x8
+}
+
#undef ZIG
#undef COPY4x4
{
pf->scan_8x8 = zigzag_scan_8x8_field;
pf->scan_4x4 = zigzag_scan_4x4_field;
+ pf->sub_8x8 = zigzag_sub_8x8_field;
pf->sub_4x4 = zigzag_sub_4x4_field;
#ifdef HAVE_MMX
if( cpu&X264_CPU_MMXEXT )
{
pf->scan_8x8 = zigzag_scan_8x8_frame;
pf->scan_4x4 = zigzag_scan_4x4_frame;
+ pf->sub_8x8 = zigzag_sub_8x8_frame;
pf->sub_4x4 = zigzag_sub_4x4_frame;
#ifdef HAVE_MMX
if( cpu&X264_CPU_MMX )
uint8_t *p_dst = &h->mb.pic.p_fdec[0][x+y*FDEC_STRIDE];
DECLARE_ALIGNED_16( int16_t dct8x8[8][8] );
+ if( h->mb.b_lossless )
+ {
+ h->zigzagf.sub_8x8( h->dct.luma8x8[idx], p_src, p_dst );
+ return;
+ }
+
h->dctf.sub8x8_dct8( dct8x8, p_src, p_dst );
x264_quant_8x8( h, dct8x8, i_qp, 1, idx );
x264_macroblock_encode_skip( h );
}
+/*****************************************************************************
+ * Intra prediction for predictive lossless mode.
+ *****************************************************************************/
+
+/* Note that these functions take a shortcut (mc.copy instead of actual pixel prediction) which assumes
+ * that the edge pixels of the reconstructed frame are the same as that of the source frame. This means
+ * they will only work correctly if the neighboring blocks are losslessly coded. In practice, this means
+ * lossless mode cannot be mixed with lossy mode within a frame. */
+/* This can be resolved by explicitly copying the edge pixels after doing the mc.copy, but this doesn't
+ * need to be done unless we decide to allow mixing lossless and lossy compression. */
+
+void x264_predict_lossless_8x8_chroma( x264_t *h, int i_mode )
+{
+ int stride = h->fenc->i_stride[1] << h->mb.b_interlaced;
+ if( i_mode == I_PRED_CHROMA_V )
+ {
+ h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc_plane[1]-stride, stride, 8 );
+ h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc_plane[2]-stride, stride, 8 );
+ }
+ else if( i_mode == I_PRED_CHROMA_H )
+ {
+ h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc_plane[1]-1, stride, 8 );
+ h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc_plane[2]-1, stride, 8 );
+ }
+ else
+ {
+ h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
+ h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
+ }
+}
+
+void x264_predict_lossless_4x4( x264_t *h, uint8_t *p_dst, int idx, int i_mode )
+{
+ int stride = h->fenc->i_stride[0] << h->mb.b_interlaced;
+ uint8_t *p_src = h->mb.pic.p_fenc_plane[0] + block_idx_x[idx]*4 + block_idx_y[idx]*4 * stride;
+
+ if( i_mode == I_PRED_4x4_V )
+ h->mc.copy[PIXEL_4x4]( p_dst, FDEC_STRIDE, p_src-stride, stride, 4 );
+ else if( i_mode == I_PRED_4x4_H )
+ h->mc.copy[PIXEL_4x4]( p_dst, FDEC_STRIDE, p_src-1, stride, 4 );
+ else
+ h->predict_4x4[i_mode]( p_dst );
+}
+
+void x264_predict_lossless_8x8( x264_t *h, uint8_t *p_dst, int idx, int i_mode, uint8_t edge[33] )
+{
+ int stride = h->fenc->i_stride[0] << h->mb.b_interlaced;
+ uint8_t *p_src = h->mb.pic.p_fenc_plane[0] + (idx&1)*8 + (idx>>1)*8*stride;
+
+ if( i_mode == I_PRED_8x8_V )
+ h->mc.copy[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src-stride, stride, 8 );
+ else if( i_mode == I_PRED_8x8_H )
+ h->mc.copy[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src-1, stride, 8 );
+ else
+ h->predict_8x8[i_mode]( p_dst, edge );
+}
+
+void x264_predict_lossless_16x16( x264_t *h, int i_mode )
+{
+ int stride = h->fenc->i_stride[0] << h->mb.b_interlaced;
+ if( i_mode == I_PRED_16x16_V )
+ h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc_plane[0]-stride, stride, 16 );
+ else if( i_mode == I_PRED_16x16_H )
+ h->mc.copy_16x16_unaligned( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc_plane[0]-1, stride, 16 );
+ else
+ h->predict_16x16[i_mode]( h->mb.pic.p_fdec[0] );
+}
+
/*****************************************************************************
* x264_macroblock_encode:
*****************************************************************************/
{
const int i_mode = h->mb.i_intra16x16_pred_mode;
h->mb.b_transform_8x8 = 0;
- /* do the right prediction */
- h->predict_16x16[i_mode]( h->mb.pic.p_fdec[0] );
+
+ if( h->mb.b_lossless )
+ x264_predict_lossless_16x16( h, i_mode );
+ else
+ h->predict_16x16[i_mode]( h->mb.pic.p_fdec[0] );
/* encode the 16x16 macroblock */
x264_mb_encode_i16x16( h, i_qp );
{
uint8_t *p_dst = &h->mb.pic.p_fdec[0][8 * (i&1) + 8 * (i>>1) * FDEC_STRIDE];
int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[4*i]];
-
x264_predict_8x8_filter( p_dst, edge, h->mb.i_neighbour8[i], x264_pred_i4x4_neighbors[i_mode] );
- h->predict_8x8[i_mode]( p_dst, edge );
+
+ if( h->mb.b_lossless )
+ x264_predict_lossless_8x8( h, p_dst, i, i_mode, edge );
+ else
+ h->predict_8x8[i_mode]( p_dst, edge );
+
x264_mb_encode_i8x8( h, i, i_qp );
}
for( i = 0; i < 4; i++ )
/* emulate missing topright samples */
*(uint32_t*) &p_dst[4-FDEC_STRIDE] = p_dst[3-FDEC_STRIDE] * 0x01010101U;
- h->predict_4x4[i_mode]( p_dst );
+ if( h->mb.b_lossless )
+ x264_predict_lossless_4x4( h, p_dst, i, i_mode );
+ else
+ h->predict_4x4[i_mode]( p_dst );
x264_mb_encode_i4x4( h, i, i_qp );
}
}
if( h->mb.b_lossless )
{
- for( i4x4 = 0; i4x4 < 16; i4x4++ )
- {
- h->zigzagf.sub_4x4( h->dct.luma4x4[i4x4],
- h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4x4],
- h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4x4] );
- }
+ if( h->mb.b_transform_8x8 )
+ for( i8x8 = 0; i8x8 < 4; i8x8++ )
+ {
+ int x = 8*(i8x8&1);
+ int y = 8*(i8x8>>1);
+ h->zigzagf.sub_8x8( h->dct.luma8x8[i8x8],
+ h->mb.pic.p_fenc[0]+x+y*FENC_STRIDE,
+ h->mb.pic.p_fdec[0]+x+y*FDEC_STRIDE );
+ nnz8x8[i8x8] = array_non_zero( h->dct.luma8x8[i8x8] );
+ }
+ else
+ for( i4x4 = 0; i4x4 < 16; i4x4++ )
+ {
+ h->zigzagf.sub_4x4( h->dct.luma4x4[i4x4],
+ h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4x4],
+ h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4x4] );
+ }
}
else if( h->mb.b_transform_8x8 )
{
if( IS_INTRA( h->mb.i_type ) )
{
const int i_mode = h->mb.i_chroma_pred_mode;
- h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
- h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
+ if( h->mb.b_lossless )
+ x264_predict_lossless_8x8_chroma( h, i_mode );
+ else
+ {
+ h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
+ h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
+ }
}
/* encode the 8x8 blocks */
if( h->mb.b_lossless )
{
int i4;
- for( i4 = i8*4; i4 < i8*4+4; i4++ )
+ if( h->mb.b_transform_8x8 )
+ {
+ h->zigzagf.sub_4x4( h->dct.luma4x4[i8], p_fenc, p_fdec );
+ nnz8x8 = array_non_zero( h->dct.luma8x8[i8] );
+ }
+ else
{
- h->zigzagf.sub_4x4( h->dct.luma4x4[i4],
- h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4],
- h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4] );
- nnz8x8 |= array_non_zero( h->dct.luma4x4[i4] );
+ for( i4 = i8*4; i4 < i8*4+4; i4++ )
+ {
+ h->zigzagf.sub_4x4( h->dct.luma4x4[i4],
+ h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4],
+ h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4] );
+ nnz8x8 |= array_non_zero( h->dct.luma4x4[i4] );
+ }
}
for( ch = 0; ch < 2; ch++ )
{