]> git.sesse.net Git - x264/blob - common/frame.c
Fix crash in fake-interlaced at some resolutions
[x264] / common / frame.c
1 /*****************************************************************************
2  * frame.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Fiona Glaser <fiona@x264.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include "common.h"
26
27 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
28
29 x264_frame_t *x264_frame_new( x264_t *h, int b_fdec )
30 {
31     x264_frame_t *frame;
32
33     int i_mb_count = h->mb.i_mb_count;
34     int i_stride, i_width, i_lines;
35     int i_padv = PADV << h->param.b_interlaced;
36     int luma_plane_size;
37     int chroma_plane_size;
38     int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
39
40     CHECKED_MALLOCZERO( frame, sizeof(x264_frame_t) );
41
42     /* allocate frame data (+64 for extra data for me) */
43     i_width  = h->sps->i_mb_width*16;
44     i_stride = ALIGN( i_width + 2*PADH, align );
45     i_lines  = h->sps->i_mb_height*16;
46
47     frame->i_plane = 3;
48     for( int i = 0; i < 3; i++ )
49     {
50         frame->i_stride[i] = ALIGN( i_stride >> !!i, align );
51         frame->i_width[i] = i_width >> !!i;
52         frame->i_lines[i] = i_lines >> !!i;
53     }
54
55     luma_plane_size = (frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv));
56     chroma_plane_size = (frame->i_stride[1] * (frame->i_lines[1] + 2*i_padv));
57     for( int i = 1; i < 3; i++ )
58     {
59         CHECKED_MALLOC( frame->buffer[i], chroma_plane_size * sizeof(pixel) );
60         frame->plane[i] = frame->buffer[i] + (frame->i_stride[i] * i_padv + PADH)/2;
61     }
62
63     for( int i = 0; i < h->param.i_bframe + 2; i++ )
64         for( int j = 0; j < h->param.i_bframe + 2; j++ )
65             CHECKED_MALLOC( frame->i_row_satds[i][j], i_lines/16 * sizeof(int) );
66
67     frame->i_poc = -1;
68     frame->i_type = X264_TYPE_AUTO;
69     frame->i_qpplus1 = 0;
70     frame->i_pts = -1;
71     frame->i_frame = -1;
72     frame->i_frame_num = -1;
73     frame->i_lines_completed = -1;
74     frame->b_fdec = b_fdec;
75     frame->i_pic_struct = PIC_STRUCT_AUTO;
76     frame->i_field_cnt = -1;
77     frame->i_duration =
78     frame->i_cpb_duration =
79     frame->i_dpb_output_delay =
80     frame->i_cpb_delay = 0;
81     frame->i_coded_fields_lookahead =
82     frame->i_cpb_delay_lookahead = -1;
83
84     frame->orig = frame;
85
86     /* all 4 luma planes allocated together, since the cacheline split code
87      * requires them to be in-phase wrt cacheline alignment. */
88     if( h->param.analyse.i_subpel_refine && b_fdec )
89     {
90         CHECKED_MALLOC( frame->buffer[0], 4*luma_plane_size * sizeof(pixel) );
91         for( int i = 0; i < 4; i++ )
92             frame->filtered[i] = frame->buffer[0] + i*luma_plane_size + frame->i_stride[0] * i_padv + PADH;
93         frame->plane[0] = frame->filtered[0];
94     }
95     else
96     {
97         CHECKED_MALLOC( frame->buffer[0], luma_plane_size * sizeof(pixel) );
98         frame->filtered[0] = frame->plane[0] = frame->buffer[0] + frame->i_stride[0] * i_padv + PADH;
99     }
100
101     frame->b_duplicate = 0;
102
103     if( b_fdec ) /* fdec frame */
104     {
105         CHECKED_MALLOC( frame->mb_type, i_mb_count * sizeof(int8_t));
106         CHECKED_MALLOC( frame->mb_partition, i_mb_count * sizeof(uint8_t));
107         CHECKED_MALLOC( frame->mv[0], 2*16 * i_mb_count * sizeof(int16_t) );
108         CHECKED_MALLOC( frame->mv16x16, 2*i_mb_count * sizeof(int16_t) );
109         CHECKED_MALLOC( frame->ref[0], 4 * i_mb_count * sizeof(int8_t) );
110         if( h->param.i_bframe )
111         {
112             CHECKED_MALLOC( frame->mv[1], 2*16 * i_mb_count * sizeof(int16_t) );
113             CHECKED_MALLOC( frame->ref[1], 4 * i_mb_count * sizeof(int8_t) );
114         }
115         else
116         {
117             frame->mv[1]  = NULL;
118             frame->ref[1] = NULL;
119         }
120         CHECKED_MALLOC( frame->i_row_bits, i_lines/16 * sizeof(int) );
121         CHECKED_MALLOC( frame->f_row_qp, i_lines/16 * sizeof(float) );
122         if( h->param.analyse.i_me_method >= X264_ME_ESA )
123         {
124             CHECKED_MALLOC( frame->buffer[3],
125                             frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv) * sizeof(uint16_t) << h->frames.b_have_sub8x8_esa );
126             frame->integral = (uint16_t*)frame->buffer[3] + frame->i_stride[0] * i_padv + PADH;
127         }
128     }
129     else /* fenc frame */
130     {
131         if( h->frames.b_have_lowres )
132         {
133             frame->i_width_lowres = frame->i_width[0]/2;
134             frame->i_stride_lowres = ALIGN( frame->i_width_lowres + 2*PADH, align );
135             frame->i_lines_lowres = frame->i_lines[0]/2;
136
137             luma_plane_size = frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV);
138
139             CHECKED_MALLOC( frame->buffer_lowres[0], 4 * luma_plane_size * sizeof(pixel) );
140             for( int i = 0; i < 4; i++ )
141                 frame->lowres[i] = frame->buffer_lowres[0] + (frame->i_stride_lowres * PADV + PADH) + i * luma_plane_size;
142
143             for( int j = 0; j <= !!h->param.i_bframe; j++ )
144                 for( int i = 0; i <= h->param.i_bframe; i++ )
145                 {
146                     CHECKED_MALLOCZERO( frame->lowres_mvs[j][i], 2*h->mb.i_mb_count*sizeof(int16_t) );
147                     CHECKED_MALLOC( frame->lowres_mv_costs[j][i], h->mb.i_mb_count*sizeof(int) );
148                 }
149             CHECKED_MALLOC( frame->i_propagate_cost, (i_mb_count+3) * sizeof(uint16_t) );
150             for( int j = 0; j <= h->param.i_bframe+1; j++ )
151                 for( int i = 0; i <= h->param.i_bframe+1; i++ )
152                     CHECKED_MALLOC( frame->lowres_costs[j][i], (i_mb_count+3) * sizeof(uint16_t) );
153             frame->i_intra_cost = frame->lowres_costs[0][0];
154             memset( frame->i_intra_cost, -1, (i_mb_count+3) * sizeof(uint16_t) );
155         }
156         if( h->param.rc.i_aq_mode )
157         {
158             CHECKED_MALLOC( frame->f_qp_offset, h->mb.i_mb_count * sizeof(float) );
159             CHECKED_MALLOC( frame->f_qp_offset_aq, h->mb.i_mb_count * sizeof(float) );
160             if( h->frames.b_have_lowres )
161                 /* shouldn't really be initialized, just silences a valgrind false-positive in x264_mbtree_propagate_cost_sse2 */
162                 CHECKED_MALLOCZERO( frame->i_inv_qscale_factor, (h->mb.i_mb_count+3) * sizeof(uint16_t) );
163         }
164     }
165
166     if( x264_pthread_mutex_init( &frame->mutex, NULL ) )
167         goto fail;
168     if( x264_pthread_cond_init( &frame->cv, NULL ) )
169         goto fail;
170
171     return frame;
172
173 fail:
174     x264_free( frame );
175     return NULL;
176 }
177
178 void x264_frame_delete( x264_frame_t *frame )
179 {
180     /* Duplicate frames are blank copies of real frames (including pointers),
181      * so freeing those pointers would cause a double free later. */
182     if( !frame->b_duplicate )
183     {
184         for( int i = 0; i < 4; i++ )
185             x264_free( frame->buffer[i] );
186         for( int i = 0; i < 4; i++ )
187             x264_free( frame->buffer_lowres[i] );
188         for( int i = 0; i < X264_BFRAME_MAX+2; i++ )
189             for( int j = 0; j < X264_BFRAME_MAX+2; j++ )
190                 x264_free( frame->i_row_satds[i][j] );
191         for( int j = 0; j < 2; j++ )
192             for( int i = 0; i <= X264_BFRAME_MAX; i++ )
193             {
194                 x264_free( frame->lowres_mvs[j][i] );
195                 x264_free( frame->lowres_mv_costs[j][i] );
196             }
197         x264_free( frame->i_propagate_cost );
198         for( int j = 0; j <= X264_BFRAME_MAX+1; j++ )
199             for( int i = 0; i <= X264_BFRAME_MAX+1; i++ )
200                 x264_free( frame->lowres_costs[j][i] );
201         x264_free( frame->f_qp_offset );
202         x264_free( frame->f_qp_offset_aq );
203         x264_free( frame->i_inv_qscale_factor );
204         x264_free( frame->i_row_bits );
205         x264_free( frame->f_row_qp );
206         x264_free( frame->mb_type );
207         x264_free( frame->mb_partition );
208         x264_free( frame->mv[0] );
209         x264_free( frame->mv[1] );
210         x264_free( frame->mv16x16 );
211         x264_free( frame->ref[0] );
212         x264_free( frame->ref[1] );
213         x264_pthread_mutex_destroy( &frame->mutex );
214         x264_pthread_cond_destroy( &frame->cv );
215     }
216     x264_free( frame );
217 }
218
219 int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
220 {
221     int i_csp = src->img.i_csp & X264_CSP_MASK;
222     if( i_csp != X264_CSP_I420 && i_csp != X264_CSP_YV12 )
223     {
224         x264_log( h, X264_LOG_ERROR, "Invalid input colorspace\n" );
225         return -1;
226     }
227
228     dst->i_type     = src->i_type;
229     dst->i_qpplus1  = src->i_qpplus1;
230     dst->i_pts      = dst->i_reordered_pts = src->i_pts;
231     dst->param      = src->param;
232     dst->i_pic_struct = src->i_pic_struct;
233
234     for( int i = 0; i < 3; i++ )
235     {
236         int s = (i_csp == X264_CSP_YV12 && i) ? i^3 : i;
237         uint8_t *plane = src->img.plane[s];
238         int stride = src->img.i_stride[s];
239         int width = h->param.i_width >> !!i;
240         int height = h->param.i_height >> !!i;
241         if( src->img.i_csp & X264_CSP_VFLIP )
242         {
243             plane += (height-1)*stride;
244             stride = -stride;
245         }
246         if( width > abs(stride) )
247         {
248             x264_log( h, X264_LOG_ERROR, "Input picture width is greater than stride\n" );
249             return -1;
250         }
251         h->mc.plane_copy( dst->plane[i], dst->i_stride[i], plane, stride, width, height );
252     }
253     return 0;
254 }
255
256 static void ALWAYS_INLINE pixel_memset( pixel *dst, int value, int size )
257 {
258     for( int i = 0; i < size; i++ )
259         dst[i] = value;
260 }
261
262 static void plane_expand_border( pixel *pix, int i_stride, int i_width, int i_height, int i_padh, int i_padv, int b_pad_top, int b_pad_bottom )
263 {
264 #define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
265     for( int y = 0; y < i_height; y++ )
266     {
267         /* left band */
268         pixel_memset( PPIXEL(-i_padh, y), PPIXEL(0, y)[0], i_padh );
269         /* right band */
270         pixel_memset( PPIXEL(i_width, y), PPIXEL(i_width-1, y)[0], i_padh );
271     }
272     /* upper band */
273     if( b_pad_top )
274         for( int y = 0; y < i_padv; y++ )
275             memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), (i_width+2*i_padh) * sizeof(pixel) );
276     /* lower band */
277     if( b_pad_bottom )
278         for( int y = 0; y < i_padv; y++ )
279             memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), (i_width+2*i_padh) * sizeof(pixel) );
280 #undef PPIXEL
281 }
282
283 void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
284 {
285     int b_start = !mb_y;
286     if( mb_y & h->sh.b_mbaff )
287         return;
288     for( int i = 0; i < frame->i_plane; i++ )
289     {
290         int stride = frame->i_stride[i];
291         int width = 16*h->sps->i_mb_width >> !!i;
292         int height = (b_end ? 16*(h->sps->i_mb_height - mb_y) >> h->sh.b_mbaff : 16) >> !!i;
293         int padh = PADH >> !!i;
294         int padv = PADV >> !!i;
295         // buffer: 2 chroma, 3 luma (rounded to 4) because deblocking goes beyond the top of the mb
296         pixel *pix = frame->plane[i] + X264_MAX(0, (16*mb_y-4)*stride >> !!i);
297         if( b_end && !b_start )
298             height += 4 >> (!!i + h->sh.b_mbaff);
299         if( h->sh.b_mbaff )
300         {
301             plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end );
302             plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end );
303         }
304         else
305         {
306             plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
307         }
308     }
309 }
310
311 void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
312 {
313     /* during filtering, 8 extra pixels were filtered on each edge,
314      * but up to 3 of the horizontal ones may be wrong.
315        we want to expand border from the last filtered pixel */
316     int b_start = !mb_y;
317     int stride = frame->i_stride[0];
318     int width = 16*h->sps->i_mb_width + 8;
319     int height = b_end ? (16*(h->sps->i_mb_height - mb_y) >> h->sh.b_mbaff) + 16 : 16;
320     int padh = PADH - 4;
321     int padv = PADV - 8;
322     for( int i = 1; i < 4; i++ )
323     {
324         // buffer: 8 luma, to match the hpel filter
325         pixel *pix = frame->filtered[i] + (16*mb_y - (8 << h->sh.b_mbaff)) * stride - 4;
326         if( h->sh.b_mbaff )
327         {
328             plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end );
329             plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end );
330         }
331         else
332             plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
333     }
334 }
335
336 void x264_frame_expand_border_lowres( x264_frame_t *frame )
337 {
338     for( int i = 0; i < 4; i++ )
339         plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1 );
340 }
341
342 void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame )
343 {
344     for( int i = 0; i < frame->i_plane; i++ )
345     {
346         int i_subsample = i ? 1 : 0;
347         int i_width = h->param.i_width >> i_subsample;
348         int i_height = h->param.i_height >> i_subsample;
349         int i_padx = (h->sps->i_mb_width * 16 - h->param.i_width) >> i_subsample;
350         int i_pady = (h->sps->i_mb_height * 16 - h->param.i_height) >> i_subsample;
351
352         if( i_padx )
353         {
354             for( int y = 0; y < i_height; y++ )
355             {
356                 pixel value = frame->plane[i][y*frame->i_stride[i] + i_width - 1];
357                 pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width], value, i_padx );
358             }
359         }
360         if( i_pady )
361         {
362             for( int y = i_height; y < i_height + i_pady; y++ )
363                 memcpy( &frame->plane[i][y*frame->i_stride[i]],
364                         &frame->plane[i][(i_height-(~y&h->param.b_interlaced)-1)*frame->i_stride[i]],
365                         (i_width + i_padx) * sizeof(pixel) );
366         }
367     }
368 }
369
370 /* threading */
371 void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
372 {
373     x264_pthread_mutex_lock( &frame->mutex );
374     frame->i_lines_completed = i_lines_completed;
375     x264_pthread_cond_broadcast( &frame->cv );
376     x264_pthread_mutex_unlock( &frame->mutex );
377 }
378
379 void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
380 {
381     x264_pthread_mutex_lock( &frame->mutex );
382     while( frame->i_lines_completed < i_lines_completed )
383         x264_pthread_cond_wait( &frame->cv, &frame->mutex );
384     x264_pthread_mutex_unlock( &frame->mutex );
385 }
386
387 /* list operators */
388
389 void x264_frame_push( x264_frame_t **list, x264_frame_t *frame )
390 {
391     int i = 0;
392     while( list[i] ) i++;
393     list[i] = frame;
394 }
395
396 x264_frame_t *x264_frame_pop( x264_frame_t **list )
397 {
398     x264_frame_t *frame;
399     int i = 0;
400     assert( list[0] );
401     while( list[i+1] ) i++;
402     frame = list[i];
403     list[i] = NULL;
404     return frame;
405 }
406
407 void x264_frame_unshift( x264_frame_t **list, x264_frame_t *frame )
408 {
409     int i = 0;
410     while( list[i] ) i++;
411     while( i-- )
412         list[i+1] = list[i];
413     list[0] = frame;
414 }
415
416 x264_frame_t *x264_frame_shift( x264_frame_t **list )
417 {
418     x264_frame_t *frame = list[0];
419     int i;
420     for( i = 0; list[i]; i++ )
421         list[i] = list[i+1];
422     assert(frame);
423     return frame;
424 }
425
426 void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
427 {
428     assert( frame->i_reference_count > 0 );
429     frame->i_reference_count--;
430     if( frame->i_reference_count == 0 )
431         x264_frame_push( h->frames.unused[frame->b_fdec], frame );
432 }
433
434 x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
435 {
436     x264_frame_t *frame;
437     if( h->frames.unused[b_fdec][0] )
438         frame = x264_frame_pop( h->frames.unused[b_fdec] );
439     else
440         frame = x264_frame_new( h, b_fdec );
441     if( !frame )
442         return NULL;
443     frame->b_last_minigop_bframe = 0;
444     frame->i_reference_count = 1;
445     frame->b_intra_calculated = 0;
446     frame->b_scenecut = 1;
447     frame->b_keyframe = 0;
448
449     memset( frame->weight, 0, sizeof(frame->weight) );
450     memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
451
452     return frame;
453 }
454
455 void x264_frame_push_blank_unused( x264_t *h, x264_frame_t *frame )
456 {
457     assert( frame->i_reference_count > 0 );
458     frame->i_reference_count--;
459     if( frame->i_reference_count == 0 )
460         x264_frame_push( h->frames.blank_unused, frame );
461 }
462
463 x264_frame_t *x264_frame_pop_blank_unused( x264_t *h )
464 {
465     x264_frame_t *frame;
466     if( h->frames.blank_unused[0] )
467         frame = x264_frame_pop( h->frames.blank_unused );
468     else
469         frame = x264_malloc( sizeof(x264_frame_t) );
470     if( !frame )
471         return NULL;
472     frame->b_duplicate = 1;
473     frame->i_reference_count = 1;
474     return frame;
475 }
476
477 void x264_frame_sort( x264_frame_t **list, int b_dts )
478 {
479     int b_ok;
480     do {
481         b_ok = 1;
482         for( int i = 0; list[i+1]; i++ )
483         {
484             int dtype = list[i]->i_type - list[i+1]->i_type;
485             int dtime = list[i]->i_frame - list[i+1]->i_frame;
486             int swap = b_dts ? dtype > 0 || ( dtype == 0 && dtime > 0 )
487                              : dtime > 0;
488             if( swap )
489             {
490                 XCHG( x264_frame_t*, list[i], list[i+1] );
491                 b_ok = 0;
492             }
493         }
494     } while( !b_ok );
495 }
496
497 void x264_weight_scale_plane( x264_t *h, pixel *dst, int i_dst_stride, pixel *src, int i_src_stride,
498                          int i_width, int i_height, x264_weight_t *w )
499 {
500     /* Weight horizontal strips of height 16. This was found to be the optimal height
501      * in terms of the cache loads. */
502     while( i_height > 0 )
503     {
504         for( int x = 0; x < i_width; x += 16 )
505             w->weightfn[16>>2]( dst+x, i_dst_stride, src+x, i_src_stride, w, X264_MIN( i_height, 16 ) );
506         i_height -= 16;
507         dst += 16 * i_dst_stride;
508         src += 16 * i_src_stride;
509     }
510 }
511
512 void x264_frame_delete_list( x264_frame_t **list )
513 {
514     int i = 0;
515     if( !list )
516         return;
517     while( list[i] )
518         x264_frame_delete( list[i++] );
519     x264_free( list );
520 }
521
522 int x264_synch_frame_list_init( x264_synch_frame_list_t *slist, int max_size )
523 {
524     if( max_size < 0 )
525         return -1;
526     slist->i_max_size = max_size;
527     slist->i_size = 0;
528     CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
529     if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
530         x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
531         x264_pthread_cond_init( &slist->cv_empty, NULL ) )
532         return -1;
533     return 0;
534 fail:
535     return -1;
536 }
537
538 void x264_synch_frame_list_delete( x264_synch_frame_list_t *slist )
539 {
540     x264_pthread_mutex_destroy( &slist->mutex );
541     x264_pthread_cond_destroy( &slist->cv_fill );
542     x264_pthread_cond_destroy( &slist->cv_empty );
543     x264_frame_delete_list( slist->list );
544 }
545
546 void x264_synch_frame_list_push( x264_synch_frame_list_t *slist, x264_frame_t *frame )
547 {
548     x264_pthread_mutex_lock( &slist->mutex );
549     while( slist->i_size == slist->i_max_size )
550         x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
551     slist->list[ slist->i_size++ ] = frame;
552     x264_pthread_mutex_unlock( &slist->mutex );
553     x264_pthread_cond_broadcast( &slist->cv_fill );
554 }