71
79
for( i = 0; i < 3; i++ )
73
81
CHECKED_MALLOC( frame->buffer[4+i],
74
frame->i_stride[0] * ( frame->i_lines[0] + 64 ) );
82
frame->i_stride[0] * ( frame->i_lines[0] + 2*i_padv ) );
75
83
frame->filtered[i+1] = ((uint8_t*)frame->buffer[4+i]) +
76
frame->i_stride[0] * 32 + 32;
84
frame->i_stride[0] * i_padv + PADH;
79
87
if( h->frames.b_have_lowres )
81
frame->i_stride_lowres = frame->i_stride[0]/2 + 32;
89
frame->i_stride_lowres = frame->i_stride[0]/2 + PADH;
82
90
frame->i_lines_lowres = frame->i_lines[0]/2;
83
91
for( i = 0; i < 4; i++ )
85
CHECKED_MALLOC( frame->buffer[7+i],
86
frame->i_stride_lowres * ( frame->i_lines[0]/2 + 64 ) );
87
frame->lowres[i] = ((uint8_t*)frame->buffer[7+i]) +
88
frame->i_stride_lowres * 32 + 32;
93
CHECKED_MALLOC( frame->buffer_lowres[i],
94
frame->i_stride_lowres * ( frame->i_lines[0]/2 + 2*i_padv ) );
95
frame->lowres[i] = ((uint8_t*)frame->buffer_lowres[i]) +
96
frame->i_stride_lowres * i_padv + PADH;
92
100
if( h->param.analyse.i_me_method == X264_ME_ESA )
94
CHECKED_MALLOC( frame->buffer[11],
95
2 * frame->i_stride[0] * (frame->i_lines[0] + 64) * sizeof(uint16_t) );
96
frame->integral = (uint16_t*)frame->buffer[11] + frame->i_stride[0] * 32 + 32;
102
CHECKED_MALLOC( frame->buffer[7],
103
2 * frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv) * sizeof(uint16_t) );
104
frame->integral = (uint16_t*)frame->buffer[7] + frame->i_stride[0] * i_padv + PADH;
99
107
frame->i_poc = -1;
145
159
x264_free( frame->mv[1] );
146
160
x264_free( frame->ref[0] );
147
161
x264_free( frame->ref[1] );
162
pthread_mutex_destroy( &frame->mutex );
163
pthread_cond_destroy( &frame->cv );
148
164
x264_free( frame );
151
167
void x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
169
int i_csp = src->img.i_csp & X264_CSP_MASK;
153
170
dst->i_type = src->i_type;
154
171
dst->i_qpplus1 = src->i_qpplus1;
155
172
dst->i_pts = src->i_pts;
157
switch( src->img.i_csp & X264_CSP_MASK )
160
h->csp.i420( dst, &src->img, h->param.i_width, h->param.i_height );
163
h->csp.yv12( dst, &src->img, h->param.i_width, h->param.i_height );
166
h->csp.i422( dst, &src->img, h->param.i_width, h->param.i_height );
169
h->csp.i444( dst, &src->img, h->param.i_width, h->param.i_height );
172
h->csp.yuyv( dst, &src->img, h->param.i_width, h->param.i_height );
175
h->csp.rgb( dst, &src->img, h->param.i_width, h->param.i_height );
178
h->csp.bgr( dst, &src->img, h->param.i_width, h->param.i_height );
181
h->csp.bgra( dst, &src->img, h->param.i_width, h->param.i_height );
185
x264_log( h, X264_LOG_ERROR, "Arg invalid CSP\n" );
174
if( i_csp <= X264_CSP_NONE || i_csp >= X264_CSP_MAX )
175
x264_log( h, X264_LOG_ERROR, "Arg invalid CSP\n" );
177
h->csp.convert[i_csp]( &h->mc, dst, &src->img, h->param.i_width, h->param.i_height );
192
static void plane_expand_border( uint8_t *pix, int i_stride, int i_height, int i_pad )
182
static void plane_expand_border( uint8_t *pix, int i_stride, int i_width, int i_height, int i_padh, int i_padv, int b_pad_top, int b_pad_bottom )
194
184
#define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
195
const int i_width = i_stride - 2*i_pad;
198
186
for( y = 0; y < i_height; y++ )
201
memset( PPIXEL(-i_pad, y), PPIXEL(0, y)[0], i_pad );
189
memset( PPIXEL(-i_padh, y), PPIXEL(0, y)[0], i_padh );
203
memset( PPIXEL(i_width, y), PPIXEL(i_width-1, y)[0], i_pad );
191
memset( PPIXEL(i_width, y), PPIXEL(i_width-1, y)[0], i_padh );
206
for( y = 0; y < i_pad; y++ )
207
memcpy( PPIXEL(-i_pad, -y-1), PPIXEL(-i_pad, 0), i_stride );
195
for( y = 0; y < i_padv; y++ )
196
memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), i_width+2*i_padh );
209
for( y = 0; y < i_pad; y++ )
210
memcpy( PPIXEL(-i_pad, i_height+y), PPIXEL(-i_pad, i_height-1), i_stride );
199
for( y = 0; y < i_padv; y++ )
200
memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), i_width+2*i_padh );
214
void x264_frame_expand_border( x264_frame_t *frame )
204
void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
208
if( mb_y & h->sh.b_mbaff )
217
210
for( i = 0; i < frame->i_plane; i++ )
219
int i_pad = i ? 16 : 32;
220
plane_expand_border( frame->plane[i], frame->i_stride[i], frame->i_lines[i], i_pad );
212
int stride = frame->i_stride[i];
213
int width = 16*h->sps->i_mb_width >> !!i;
214
int height = (b_end ? 16*(h->sps->i_mb_height - mb_y) >> h->sh.b_mbaff : 16) >> !!i;
215
int padh = PADH >> !!i;
216
int padv = PADV >> !!i;
217
// buffer: 2 chroma, 3 luma (rounded to 4) because deblocking goes beyond the top of the mb
218
uint8_t *pix = frame->plane[i] + X264_MAX(0, (16*mb_y-4)*stride >> !!i);
219
if( b_end && !b_start )
220
height += 4 >> (!!i + h->sh.b_mbaff);
223
plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end );
224
plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end );
228
plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
224
void x264_frame_expand_border_filtered( x264_frame_t *frame )
233
void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
226
235
/* during filtering, 8 extra pixels were filtered on each edge.
227
236
we want to expand border from the last filtered pixel */
238
int stride = frame->i_stride[0];
239
int width = 16*h->sps->i_mb_width + 16;
240
int height = b_end ? (16*(h->sps->i_mb_height - mb_y) >> h->sh.b_mbaff) + 16 : 16;
229
244
for( i = 1; i < 4; i++ )
230
plane_expand_border( frame->filtered[i] - 8*frame->i_stride[0] - 8, frame->i_stride[0], frame->i_lines[0]+2*8, 24 );
246
// buffer: 8 luma, to match the hpel filter
247
uint8_t *pix = frame->filtered[i] + (16*mb_y - (8 << h->sh.b_mbaff)) * stride - 8;
250
plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end );
251
plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end );
255
plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
233
260
void x264_frame_expand_border_lowres( x264_frame_t *frame )
236
263
for( i = 0; i < 4; i++ )
237
plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_lines_lowres, 32 );
264
plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_stride_lowres - 2*PADH, frame->i_lines_lowres, PADH, PADV, 1, 1 );
240
267
void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame )
601
653
/* vertical edge */
602
deblock_edge( h, &h->fdec->plane[0][16*mb_y * h->fdec->i_stride[0] + 16*mb_x + 4*i_edge],
603
h->fdec->i_stride[0], bS, (i_qp+i_qpn+1) >> 1, 0,
654
deblock_edge( h, &h->fdec->plane[0][i_pix_y[0] + 4*i_edge],
655
i_stride2[0], bS, (i_qp+i_qpn+1) >> 1, 0,
604
656
h->loopf.deblock_h_luma, h->loopf.deblock_h_luma_intra );
605
657
if( !(i_edge & 1) )
608
660
int i_qpc = ( i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )] +
609
661
i_chroma_qp_table[x264_clip3( i_qpn + h->pps->i_chroma_qp_index_offset, 0, 51 )] + 1 ) >> 1;
610
deblock_edge( h, &h->fdec->plane[1][8*(mb_y*h->fdec->i_stride[1]+mb_x)+2*i_edge],
611
h->fdec->i_stride[1], bS, i_qpc, 1,
662
deblock_edge( h, &h->fdec->plane[1][i_pix_y[1] + 2*i_edge],
663
i_stride2[1], bS, i_qpc, 1,
612
664
h->loopf.deblock_h_chroma, h->loopf.deblock_h_chroma_intra );
613
deblock_edge( h, &h->fdec->plane[2][8*(mb_y*h->fdec->i_stride[2]+mb_x)+2*i_edge],
614
h->fdec->i_stride[2], bS, i_qpc, 1,
665
deblock_edge( h, &h->fdec->plane[2][i_pix_y[2] + 2*i_edge],
666
i_stride2[2], bS, i_qpc, 1,
615
667
h->loopf.deblock_h_chroma, h->loopf.deblock_h_chroma_intra );
620
672
/* horizontal edge */
621
deblock_edge( h, &h->fdec->plane[0][(16*mb_y + 4*i_edge) * h->fdec->i_stride[0] + 16*mb_x],
622
h->fdec->i_stride[0], bS, (i_qp+i_qpn+1) >> 1, 0,
673
deblock_edge( h, &h->fdec->plane[0][i_pix_y[0] + 4*i_edge*i_stride2[0]],
674
i_stride2[0], bS, (i_qp+i_qpn+1) >> 1, 0,
623
675
h->loopf.deblock_v_luma, h->loopf.deblock_v_luma_intra );
625
677
if( !(i_edge & 1) )
627
679
int i_qpc = ( i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )] +
628
680
i_chroma_qp_table[x264_clip3( i_qpn + h->pps->i_chroma_qp_index_offset, 0, 51 )] + 1 ) >> 1;
629
deblock_edge( h, &h->fdec->plane[1][8*(mb_y*h->fdec->i_stride[1]+mb_x)+2*i_edge*h->fdec->i_stride[1]],
630
h->fdec->i_stride[1], bS, i_qpc, 1,
681
deblock_edge( h, &h->fdec->plane[1][i_pix_y[1] + 2*i_edge*i_stride2[1]],
682
i_stride2[1], bS, i_qpc, 1,
631
683
h->loopf.deblock_v_chroma, h->loopf.deblock_v_chroma_intra );
632
deblock_edge( h, &h->fdec->plane[2][8*(mb_y*h->fdec->i_stride[2]+mb_x)+2*i_edge*h->fdec->i_stride[2]],
633
h->fdec->i_stride[2], bS, i_qpc, 1,
684
deblock_edge( h, &h->fdec->plane[2][i_pix_y[2] + 2*i_edge*i_stride2[2]],
685
i_stride2[2], bS, i_qpc, 1,
634
686
h->loopf.deblock_v_chroma, h->loopf.deblock_v_chroma_intra );
642
if( mb_x >= h->sps->i_mb_width )
693
if( !b_interlaced || (mb_y&1) )
695
mb_y ^= b_interlaced;
699
void x264_frame_deblock( x264_t *h )
702
for( mb_y = 0; mb_y < h->sps->i_mb_height; mb_y += 1 + h->sh.b_mbaff )
703
x264_frame_deblock_row( h, mb_y );
650
706
#ifdef HAVE_MMXEXT
651
707
void x264_deblock_v_chroma_mmxext( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
652
708
void x264_deblock_h_chroma_mmxext( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
764
void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
766
pthread_mutex_lock( &frame->mutex );
767
frame->i_lines_completed = i_lines_completed;
768
pthread_cond_broadcast( &frame->cv );
769
pthread_mutex_unlock( &frame->mutex );
772
void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
774
pthread_mutex_lock( &frame->mutex );
775
while( frame->i_lines_completed < i_lines_completed )
776
pthread_cond_wait( &frame->cv, &frame->mutex );
777
pthread_mutex_unlock( &frame->mutex );
781
void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
783
void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
790
void x264_frame_push( x264_frame_t **list, x264_frame_t *frame )
793
while( list[i] ) i++;
797
x264_frame_t *x264_frame_pop( x264_frame_t **list )
802
while( list[i+1] ) i++;
808
void x264_frame_unshift( x264_frame_t **list, x264_frame_t *frame )
811
while( list[i] ) i++;
817
x264_frame_t *x264_frame_shift( x264_frame_t **list )
819
x264_frame_t *frame = list[0];
821
for( i = 0; list[i]; i++ )
827
void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
829
assert( frame->i_reference_count > 0 );
830
frame->i_reference_count--;
831
if( frame->i_reference_count == 0 )
832
x264_frame_push( h->frames.unused, frame );
833
assert( h->frames.unused[ sizeof(h->frames.unused) / sizeof(*h->frames.unused) - 1 ] == NULL );
836
x264_frame_t *x264_frame_pop_unused( x264_t *h )
839
if( h->frames.unused[0] )
840
frame = x264_frame_pop( h->frames.unused );
842
frame = x264_frame_new( h );
843
assert( frame->i_reference_count == 0 );
844
frame->i_reference_count = 1;
848
void x264_frame_sort( x264_frame_t **list, int b_dts )
853
for( i = 0; list[i+1]; i++ )
855
int dtype = list[i]->i_type - list[i+1]->i_type;
856
int dtime = list[i]->i_frame - list[i+1]->i_frame;
857
int swap = b_dts ? dtype > 0 || ( dtype == 0 && dtime > 0 )
861
XCHG( x264_frame_t*, list[i], list[i+1] );