]> git.sesse.net Git - x264/blob - encoder/encoder.c
* encoder.c, analyse.c, macroblock: fixed when using a qp per MB.
[x264] / encoder / encoder.c
1 /*****************************************************************************
2  * x264: h264 encoder
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: encoder.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28
29 #include <math.h>
30
31 #include "../core/common.h"
32 #include "../core/cpu.h"
33
34 #include "set.h"
35 #include "analyse.h"
36 #include "ratecontrol.h"
37 #include "macroblock.h"
38
39 //#define DEBUG_MB_TYPE
40 #define DEBUG_DUMP_FRAME 1
41
42 static int64_t i_mtime_encode_frame = 0;
43
44 static int64_t i_mtime_analyse = 0;
45 static int64_t i_mtime_encode = 0;
46 static int64_t i_mtime_write = 0;
47 static int64_t i_mtime_filter = 0;
48 #define TIMER_START( d ) \
49     { \
50         int64_t d##start = x264_mdate();
51
52 #define TIMER_STOP( d ) \
53         d += x264_mdate() - d##start;\
54     }
55
56
57 /****************************************************************************
58  *
59  ******************************* x264 libs **********************************
60  *
61  ****************************************************************************/
62
63
64 static float x264_psnr( uint8_t *pix1, int i_pix_stride, uint8_t *pix2, int i_pix2_stride, int i_width, int i_height )
65 {
66     int64_t i_sqe = 0;
67
68     int x, y;
69
70     for( y = 0; y < i_height; y++ )
71     {
72         for( x = 0; x < i_width; x++ )
73         {
74             int tmp;
75
76             tmp = pix1[y*i_pix_stride+x] - pix2[y*i_pix2_stride+x];
77
78             i_sqe += tmp * tmp;
79         }
80     }
81
82     if( i_sqe == 0 )
83     {
84         return -1.0;
85     }
86     return (float)(10.0 * log( (double)65025.0 * (double)i_height * (double)i_width / (double)i_sqe ) / log( 10.0 ));
87 }
88
89 #if DEBUG_DUMP_FRAME
90 static void x264_frame_dump( x264_t *h, x264_frame_t *fr, char *name )
91 {
92     FILE * f = fopen( name, "a" );
93     int i, y;
94
95     fseek( f, 0, SEEK_END );
96
97     for( i = 0; i < fr->i_plane; i++ )
98     {
99         for( y = 0; y < h->param.i_height / ( i == 0 ? 1 : 2 ); y++ )
100         {
101             fwrite( &fr->plane[i][y*fr->i_stride[i]], 1, h->param.i_width / ( i == 0 ? 1 : 2 ), f );
102         }
103     }
104     fclose( f );
105 }
106 #endif
107
108
109 /* Fill "default" values */
110 static void x264_slice_header_init( x264_slice_header_t *sh, x264_param_t *param,
111                                     x264_sps_t *sps, x264_pps_t *pps,
112                                     int i_type, int i_idr_pic_id, int i_frame )
113 {
114     /* First we fill all field */
115     sh->sps = sps;
116     sh->pps = pps;
117
118     sh->i_type      = i_type;
119     sh->i_first_mb  = 0;
120     sh->i_pps_id    = pps->i_id;
121
122     sh->i_frame_num = i_frame;
123
124     sh->b_field_pic = 0;    /* Not field support for now */
125     sh->b_bottom_field = 1; /* not yet used */
126
127     sh->i_idr_pic_id = i_idr_pic_id;
128
129     /* poc stuff, fixed later */
130     sh->i_poc_lsb = 0;
131     sh->i_delta_poc_bottom = 0;
132     sh->i_delta_poc[0] = 0;
133     sh->i_delta_poc[1] = 0;
134
135     sh->i_redundant_pic_cnt = 0;
136
137     sh->b_direct_spatial_mv_pred = 1;
138
139     sh->b_num_ref_idx_override = 0;
140     sh->i_num_ref_idx_l0_active = 1;
141     sh->i_num_ref_idx_l1_active = 1;
142
143     sh->i_cabac_init_idc = param->i_cabac_init_idc;
144
145     sh->i_qp_delta = 0;
146     sh->b_sp_for_swidth = 0;
147     sh->i_qs_delta = 0;
148
149     if( param->b_deblocking_filter )
150     {
151         sh->i_disable_deblocking_filter_idc = 0;
152     }
153     else
154     {
155         sh->i_disable_deblocking_filter_idc = 1;
156     }
157     sh->i_alpha_c0_offset = param->i_deblocking_filter_alphac0 << 1;
158     sh->i_beta_offset = param->i_deblocking_filter_beta << 1;
159 }
160
161 static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal_ref_idc )
162 {
163     bs_write_ue( s, sh->i_first_mb );
164     bs_write_ue( s, sh->i_type + 5 );   /* same type things */
165     bs_write_ue( s, sh->i_pps_id );
166     bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num );
167
168     if( sh->i_idr_pic_id >= 0 ) /* NAL IDR */
169     {
170         bs_write_ue( s, sh->i_idr_pic_id );
171     }
172
173     if( sh->sps->i_poc_type == 0 )
174     {
175         bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc_lsb );
176         if( sh->pps->b_pic_order && !sh->b_field_pic )
177         {
178             bs_write_se( s, sh->i_delta_poc_bottom );
179         }
180     }
181     else if( sh->sps->i_poc_type == 1 && !sh->sps->b_delta_pic_order_always_zero )
182     {
183         bs_write_se( s, sh->i_delta_poc[0] );
184         if( sh->pps->b_pic_order && !sh->b_field_pic )
185         {
186             bs_write_se( s, sh->i_delta_poc[1] );
187         }
188     }
189
190     if( sh->pps->b_redundant_pic_cnt )
191     {
192         bs_write_ue( s, sh->i_redundant_pic_cnt );
193     }
194
195     if( sh->i_type == SLICE_TYPE_B )
196     {
197         bs_write1( s, sh->b_direct_spatial_mv_pred );
198     }
199     if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_B )
200     {
201         bs_write1( s, sh->b_num_ref_idx_override );
202         if( sh->b_num_ref_idx_override )
203         {
204             bs_write_ue( s, sh->i_num_ref_idx_l0_active - 1 );
205             if( sh->i_type == SLICE_TYPE_B )
206             {
207                 bs_write_ue( s, sh->i_num_ref_idx_l1_active - 1 );
208             }
209         }
210     }
211
212     /* ref pic list reordering */
213     if( sh->i_type != SLICE_TYPE_I )
214     {
215         int b_ref_pic_list_reordering_l0 = 0;
216         bs_write1( s, b_ref_pic_list_reordering_l0 );
217         if( b_ref_pic_list_reordering_l0 )
218         {
219             /* FIXME */
220         }
221     }
222     if( sh->i_type == SLICE_TYPE_B )
223     {
224         int b_ref_pic_list_reordering_l1 = 0;
225         bs_write1( s, b_ref_pic_list_reordering_l1 );
226         if( b_ref_pic_list_reordering_l1 )
227         {
228             /* FIXME */
229         }
230     }
231
232     if( ( sh->pps->b_weighted_pred && ( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP ) ) ||
233         ( sh->pps->b_weighted_bipred == 1 && sh->i_type == SLICE_TYPE_B ) )
234     {
235         /* FIXME */
236     }
237
238     if( i_nal_ref_idc != 0 )
239     {
240         if( sh->i_idr_pic_id >= 0 )
241         {
242             bs_write1( s, 0 );  /* no output of prior pics flag */
243             bs_write1( s, 0 );  /* long term reference flag */
244         }
245         else
246         {
247             bs_write1( s, 0 );  /* adaptive_ref_pic_marking_mode_flag */
248             /* FIXME */
249         }
250     }
251
252     if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I )
253     {
254         bs_write_ue( s, sh->i_cabac_init_idc );
255     }
256     bs_write_se( s, sh->i_qp_delta );      /* slice qp delta */
257 #if 0
258     if( sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_SI )
259     {
260         if( sh->i_type == SLICE_TYPE_SP )
261         {
262             bs_write1( s, sh->b_sp_for_swidth );
263         }
264         bs_write_se( s, sh->i_qs_delta );
265     }
266 #endif
267
268     if( sh->pps->b_deblocking_filter_control )
269     {
270         bs_write_ue( s, sh->i_disable_deblocking_filter_idc );
271         if( sh->i_disable_deblocking_filter_idc != 1 )
272         {
273             bs_write_se( s, sh->i_alpha_c0_offset >> 1 );
274             bs_write_se( s, sh->i_beta_offset >> 1 );
275         }
276     }
277 }
278
279 /****************************************************************************
280  *
281  ****************************************************************************
282  ****************************** External API*********************************
283  ****************************************************************************
284  *
285  ****************************************************************************/
286
287 /****************************************************************************
288  * x264_encoder_open:
289  ****************************************************************************/
290 x264_t *x264_encoder_open   ( x264_param_t *param )
291 {
292     x264_t *h = x264_malloc( sizeof( x264_t ) );
293     int i;
294
295     /* Check parameters validity */
296     if( param->i_width <= 0  || param->i_height <= 0 )
297     {
298         fprintf( stderr, "invalid width x height (%dx%d)\n",
299                  param->i_width, param->i_height );
300         free( h );
301         return NULL;
302     }
303
304     if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
305     {
306         fprintf( stderr, "width %% 16 != 0 pr height %% 16 != 0 (%dx%d)\n",
307                  param->i_width, param->i_height );
308         free( h );
309         return NULL;
310     }
311     if( param->i_csp != X264_CSP_I420 )
312     {
313         fprintf( stderr, "invalid CSP (only I420 supported)\n" );
314         free( h );
315         return NULL;
316     }
317
318     /* Fix parameters values */
319     memcpy( &h->param, param, sizeof( x264_param_t ) );
320     h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 15 );
321     if( h->param.i_idrframe <= 0 )
322     {
323         h->param.i_idrframe = 1;
324     }
325     if( h->param.i_iframe <= 0 )
326     {
327         h->param.i_iframe = 1;
328     }
329     h->param.i_bframe  = x264_clip3( h->param.i_bframe , 0, X264_BFRAME_MAX );
330 #if 0
331     if( h->param.i_bframe > 0 && h->param.b_cabac )
332     {
333         fprintf( stderr, "cabac not supported with B frame (cabac disabled)\n" );
334         h->param.b_cabac = 0;
335     }
336 #endif
337
338     h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
339     h->param.i_deblocking_filter_beta    = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
340
341     h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, -1, 2 );
342
343     /* Init x264_t */
344     h->out.i_nal = 0;
345     h->out.i_bitstream = 1000000; /* FIXME estimate max size (idth/height) */
346     h->out.p_bitstream = x264_malloc( h->out.i_bitstream );
347
348     h->i_frame = 0;
349     h->i_frame_num = 0;
350     h->i_poc   = 0;
351     h->i_idr_pic_id = 0;
352
353     h->sps = &h->sps_array[0];
354     x264_sps_init( h->sps, 0, &h->param );
355
356     h->pps = &h->pps_array[0];
357     x264_pps_init( h->pps, 0, &h->param, h->sps);
358
359     /* Init frames. */
360     for( i = 0; i < X264_BFRAME_MAX + 1; i++ )
361     {
362         h->frames.current[i] = NULL;
363         h->frames.next[i]    = NULL;
364         h->frames.unused[i]  = NULL;
365     }
366     for( i = 0; i < 1 + h->param.i_bframe; i++ )
367     {
368         h->frames.unused[i] =  x264_frame_new( h );
369     }
370     for( i = 0; i < 2 + h->param.i_frame_reference; i++ )
371     {
372         /* 2 = 1 backward ref  + 1 fdec */
373         h->frames.reference[i] = x264_frame_new( h );
374     }
375     h->frames.i_last_idr = h->param.i_idrframe;
376     h->frames.i_last_i   = h->param.i_iframe;
377
378     h->i_ref0 = 0;
379     h->i_ref1 = 0;
380
381     h->fdec = h->frames.reference[0];
382
383     /* init mb cache */
384     x264_macroblock_cache_init( h );
385
386     /* init cabac adaptive model */
387     x264_cabac_model_init( &h->cabac );
388
389     /* init CPU functions */
390     x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
391     x264_predict_8x8_init( h->param.cpu, h->predict_8x8 );
392     x264_predict_4x4_init( h->param.cpu, h->predict_4x4 );
393
394     x264_pixel_init( h->param.cpu, &h->pixf );
395     x264_dct_init( h->param.cpu, &h->dctf );
396     x264_mc_init( h->param.cpu, h->mc );
397     x264_csp_init( h->param.cpu, h->param.i_csp, &h->csp );
398
399     /* rate control */
400     h->rc = x264_ratecontrol_new( &h->param );
401
402     /* stat */
403     h->stat.i_slice_count[SLICE_TYPE_I] = 0;
404     h->stat.i_slice_count[SLICE_TYPE_P] = 0;
405     h->stat.i_slice_count[SLICE_TYPE_B] = 0;
406     h->stat.i_slice_size[SLICE_TYPE_I] = 0;
407     h->stat.i_slice_size[SLICE_TYPE_P] = 0;
408     h->stat.i_slice_size[SLICE_TYPE_B] = 0;
409
410     h->stat.f_psnr_y[SLICE_TYPE_I] = 0.0; h->stat.f_psnr_u[SLICE_TYPE_I] = 0.0; h->stat.f_psnr_v[SLICE_TYPE_I] = 0.0;
411     h->stat.f_psnr_y[SLICE_TYPE_P] = 0.0; h->stat.f_psnr_u[SLICE_TYPE_P] = 0.0; h->stat.f_psnr_v[SLICE_TYPE_P] = 0.0;
412     h->stat.f_psnr_y[SLICE_TYPE_B] = 0.0; h->stat.f_psnr_u[SLICE_TYPE_B] = 0.0; h->stat.f_psnr_v[SLICE_TYPE_B] = 0.0;
413
414     for( i = 0; i < 17; i++ )
415     {
416         h->stat.i_mb_count[SLICE_TYPE_I][i] = 0;
417         h->stat.i_mb_count[SLICE_TYPE_P][i] = 0;
418         h->stat.i_mb_count[SLICE_TYPE_B][i] = 0;
419     }
420     return h;
421 }
422
423 /* internal usage */
424 static void x264_nal_start( x264_t *h, int i_type, int i_ref_idc )
425 {
426     x264_nal_t *nal = &h->out.nal[h->out.i_nal];
427
428     nal->i_ref_idc = i_ref_idc;
429     nal->i_type    = i_type;
430
431     bs_align_0( &h->out.bs );   /* not needed */
432
433     nal->i_payload= 0;
434     nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs) / 8];
435 }
436 static void x264_nal_end( x264_t *h )
437 {
438     x264_nal_t *nal = &h->out.nal[h->out.i_nal];
439
440     bs_align_0( &h->out.bs );   /* not needed */
441
442     nal->i_payload = &h->out.p_bitstream[bs_pos( &h->out.bs)/8] - nal->p_payload;
443
444     h->out.i_nal++;
445 }
446
447 /****************************************************************************
448  * x264_encoder_headers:
449  ****************************************************************************/
450 int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
451 {
452     /* init bitstream context */
453     h->out.i_nal = 0;
454     bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
455
456     /* Put SPS and PPS */
457     if( h->i_frame == 0 )
458     {
459         /* generate sequence parameters */
460         x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
461         x264_sps_write( &h->out.bs, h->sps );
462         x264_nal_end( h );
463
464         /* generate picture parameters */
465         x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
466         x264_pps_write( &h->out.bs, h->pps );
467         x264_nal_end( h );
468     }
469     /* now set output*/
470     *pi_nal = h->out.i_nal;
471     *pp_nal = &h->out.nal[0];
472
473     return 0;
474 }
475
476
477 static void x264_frame_put( x264_frame_t *list[X264_BFRAME_MAX], x264_frame_t *frame )
478 {
479     int i = 0;
480
481     while( list[i] != NULL ) i++;
482
483     list[i] = frame;
484 }
485
486 static x264_frame_t *x264_frame_get( x264_frame_t *list[X264_BFRAME_MAX+1] )
487 {
488     x264_frame_t *frame = list[0];
489     int i;
490
491     for( i = 0; i < X264_BFRAME_MAX; i++ )
492     {
493         list[i] = list[i+1];
494     }
495     list[X264_BFRAME_MAX] = NULL;
496
497     return frame;
498 }
499
500 static inline void x264_reference_build_list( x264_t *h, int i_poc )
501 {
502     int i;
503     int b_ok;
504
505     /* build ref list 0/1 */
506     h->i_ref0 = 0;
507     h->i_ref1 = 0;
508     for( i = 1; i < h->param.i_frame_reference+2; i++ )
509     {
510         if( h->frames.reference[i]->i_poc >= 0 )
511         {
512             if( h->frames.reference[i]->i_poc < i_poc )
513             {
514                 h->fref0[h->i_ref0++] = h->frames.reference[i];
515             }
516             else if( h->frames.reference[i]->i_poc > i_poc )
517             {
518                 h->fref1[h->i_ref1++] = h->frames.reference[i];
519             }
520         }
521     }
522     /* Order ref0 from higher to lower poc */
523     do
524     {
525         b_ok = 1;
526         for( i = 0; i < h->i_ref0 - 1; i++ )
527         {
528             if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
529             {
530                 x264_frame_t *tmp = h->fref0[i+1];
531
532                 h->fref0[i+1] = h->fref0[i];
533                 h->fref0[i] = tmp;
534                 b_ok = 0;
535                 break;
536             }
537         }
538     } while( !b_ok );
539     /* Order ref1 from lower to higher poc (bubble sort) for B-frame */
540     do
541     {
542         b_ok = 1;
543         for( i = 0; i < h->i_ref1 - 1; i++ )
544         {
545             if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
546             {
547                 x264_frame_t *tmp = h->fref1[i+1];
548
549                 h->fref1[i+1] = h->fref1[i];
550                 h->fref1[i] = tmp;
551                 b_ok = 0;
552                 break;
553             }
554         }
555     } while( !b_ok );
556
557     if( h->i_ref0 > h->param.i_frame_reference )
558     {
559         h->i_ref0 = h->param.i_frame_reference;
560     }
561     if( h->i_ref1 > 1 )
562     {
563         h->i_ref1 = 1;
564     }
565 }
566
567 static inline void x264_reference_update( x264_t *h )
568 {
569     int i;
570
571     /* apply deblocking filter to the current decoded picture */
572     if( h->param.b_deblocking_filter )
573     {
574         TIMER_START( i_mtime_filter );
575         x264_frame_deblocking_filter( h, h->sh.i_type );
576         TIMER_STOP( i_mtime_filter );
577     }
578     /* expand border */
579     x264_frame_expand_border( h->fdec );
580
581     /* move frame in the buffer */
582     h->fdec = h->frames.reference[h->param.i_frame_reference+1];
583     for( i = h->param.i_frame_reference+1; i > 0; i-- )
584     {
585         h->frames.reference[i] = h->frames.reference[i-1];
586     }
587     h->frames.reference[0] = h->fdec;
588 }
589
590 static inline void x264_reference_reset( x264_t *h )
591 {
592     int i;
593
594     /* reset ref pictures */
595     for( i = 1; i < h->param.i_frame_reference+2; i++ )
596     {
597         h->frames.reference[i]->i_poc = -1;
598     }
599     h->frames.reference[0]->i_poc = 0;
600 }
601
602 static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_slice_type, int i_global_qp )
603 {
604     /* ------------------------ Create slice header  ----------------------- */
605     if( i_nal_type == NAL_SLICE_IDR )
606     {
607         x264_slice_header_init( &h->sh, &h->param, h->sps, h->pps, i_slice_type, h->i_idr_pic_id, h->i_frame_num - 1 );
608
609         /* increment id */
610         h->i_idr_pic_id = ( h->i_idr_pic_id + 1 ) % 65535;
611     }
612     else
613     {
614         x264_slice_header_init( &h->sh, &h->param, h->sps, h->pps, i_slice_type, -1, h->i_frame_num - 1 );
615
616         /* always set the real higher num of ref frame used */
617         h->sh.b_num_ref_idx_override = 1;
618         h->sh.i_num_ref_idx_l0_active = h->i_ref0 <= 0 ? 1 : h->i_ref0;
619         h->sh.i_num_ref_idx_l1_active = h->i_ref1 <= 0 ? 1 : h->i_ref1;
620     }
621
622     if( h->sps->i_poc_type == 0 )
623     {
624         h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );
625         h->sh.i_delta_poc_bottom = 0;   /* XXX won't work for field */
626     }
627     else if( h->sps->i_poc_type == 1 )
628     {
629         /* FIXME TODO FIXME */
630     }
631     else
632     {
633         /* Nothing to do ? */
634     }
635
636     /* global qp */
637     h->sh.i_qp_delta = i_global_qp - h->pps->i_pic_init_qp;
638
639     /* get adapative cabac model if needed */
640     if( h->param.b_cabac )
641     {
642         if( h->param.i_cabac_init_idc == -1 )
643         {
644             h->sh.i_cabac_init_idc = x264_cabac_model_get( &h->cabac, i_slice_type );
645         }
646     }
647 }
648
649 static inline void x264_slice_write( x264_t *h, int i_nal_type, int i_nal_ref_idc, int i_mb_count[18] )
650 {
651     int i_skip;
652     int mb_xy;
653     int i;
654
655     /* Init stats */
656     for( i = 0; i < 17; i++ ) i_mb_count[i] = 0;
657
658     /* Slice */
659     x264_nal_start( h, i_nal_type, i_nal_ref_idc );
660
661     /* Slice header */
662     x264_slice_header_write( &h->out.bs, &h->sh, i_nal_ref_idc );
663     if( h->param.b_cabac )
664     {
665         /* alignement needed */
666         bs_align_1( &h->out.bs );
667
668         /* init cabac */
669         x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.pps->i_pic_init_qp + h->sh.i_qp_delta, h->sh.i_cabac_init_idc );
670         x264_cabac_encode_init ( &h->cabac, &h->out.bs );
671     }
672     h->mb.i_last_qp = h->pps->i_pic_init_qp + h->sh.i_qp_delta;
673     h->mb.i_last_dqp = 0;
674
675     for( mb_xy = 0, i_skip = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
676     {
677         const int i_mb_y = mb_xy / h->sps->i_mb_width;
678         const int i_mb_x = mb_xy % h->sps->i_mb_width;
679
680         /* load cache */
681         x264_macroblock_cache_load( h, i_mb_x, i_mb_y );
682
683         /* analyse parameters
684          * Slice I: choose I_4x4 or I_16x16 mode
685          * Slice P: choose between using P mode or intra (4x4 or 16x16)
686          * */
687         TIMER_START( i_mtime_analyse );
688         x264_macroblock_analyse( h );
689         TIMER_STOP( i_mtime_analyse );
690
691         /* encode this macrobock -> be carefull it can change the mb type to P_SKIP if needed */
692         TIMER_START( i_mtime_encode );
693         x264_macroblock_encode( h );
694         TIMER_STOP( i_mtime_encode );
695
696         TIMER_START( i_mtime_write );
697         if( IS_SKIP( h->mb.i_type ) )
698         {
699             if( h->param.b_cabac )
700             {
701                 if( mb_xy > 0 )
702                 {
703                     /* not end_of_slice_flag */
704                     x264_cabac_encode_terminal( &h->cabac, 0 );
705                 }
706
707                 x264_cabac_mb_skip( h, 1 );
708             }
709             else
710             {
711                 i_skip++;
712             }
713         }
714         else
715         {
716             if( h->param.b_cabac )
717             {
718                 if( mb_xy > 0 )
719                 {
720                     /* not end_of_slice_flag */
721                     x264_cabac_encode_terminal( &h->cabac, 0 );
722                 }
723                 if( h->sh.i_type != SLICE_TYPE_I )
724                 {
725                     x264_cabac_mb_skip( h, 0 );
726                 }
727                 x264_macroblock_write_cabac( h, &h->out.bs );
728             }
729             else
730             {
731                 if( h->sh.i_type != SLICE_TYPE_I )
732                 {
733                     bs_write_ue( &h->out.bs, i_skip );  /* skip run */
734                     i_skip = 0;
735                 }
736                 x264_macroblock_write_cavlc( h, &h->out.bs );
737             }
738         }
739         TIMER_STOP( i_mtime_write );
740
741         /* save cache */
742         x264_macroblock_cache_save( h );
743
744         i_mb_count[h->mb.i_type]++;
745     }
746
747     if( h->param.b_cabac )
748     {
749         /* end of slice */
750         x264_cabac_encode_terminal( &h->cabac, 1 );
751     }
752     else if( i_skip > 0 )
753     {
754         bs_write_ue( &h->out.bs, i_skip );  /* last skip run */
755     }
756
757     if( h->param.b_cabac )
758     {
759         int i_cabac_word;
760         x264_cabac_encode_flush( &h->cabac );
761         /* TODO cabac stuffing things (p209) */
762         i_cabac_word = (((3 * h->cabac.i_sym_cnt - 3 * 96 * h->sps->i_mb_width * h->sps->i_mb_height)/32) - bs_pos( &h->out.bs)/8)/3;
763
764         while( i_cabac_word > 0 )
765         {
766             bs_write( &h->out.bs, 16, 0x0000 );
767             i_cabac_word--;
768         }
769     }
770     else
771     {
772         /* rbsp_slice_trailing_bits */
773         bs_rbsp_trailing( &h->out.bs );
774     }
775
776     x264_nal_end( h );
777 }
778
779 /****************************************************************************
780  * x264_encoder_encode:
781  *  XXX: i_poc   : is the poc of the current given picture
782  *       i_frame : is the number of the frame being coded
783  *  ex:  type frame poc
784  *       I      0   2*0
785  *       P      1   2*3
786  *       B      2   2*1
787  *       B      3   2*2
788  *       P      4   2*6
789  *       B      5   2*4
790  *       B      6   2*5
791  ****************************************************************************/
792 int     x264_encoder_encode( x264_t *h,
793                              x264_nal_t **pp_nal, int *pi_nal,
794                              x264_picture_t *pic )
795 {
796     x264_frame_t   *frame_psnr = h->fdec; /* just to kept the current decoded frame for psnr calculation */
797     int     i_nal_type;
798     int     i_nal_ref_idc;
799     int     i_slice_type;
800
801     int i;
802
803     float psnr_y, psnr_u, psnr_v;
804
805     int   i_global_qp;
806
807     int i_mb_count[18];
808
809     /* no data out */
810     *pi_nal = 0;
811     *pp_nal = NULL;
812
813
814     /* ------------------- Setup new frame from picture -------------------- */
815     TIMER_START( i_mtime_encode_frame );
816     if( pic != NULL )
817     {
818         /* Copy the picture to a frame, init the frame and move it to a buffer */
819         /* 1: get a frame */
820         x264_frame_t *fenc = x264_frame_get( h->frames.unused );
821
822         x264_frame_copy_picture( h, fenc, pic );
823
824         /* 2: get its type */
825         if( ( h->frames.i_last_i + 1 >= h->param.i_iframe && h->frames.i_last_idr + 1 >= h->param.i_idrframe ) ||
826             pic->i_type == X264_TYPE_IDR )
827         {
828             /* IDR */
829             fenc->i_type = X264_TYPE_IDR;
830
831             h->i_poc       = 0;
832             h->i_frame_num = 0;
833
834             /* Last schedule B frames need to be encoded as P */
835             if( h->frames.next[0] != NULL )
836             {
837                 x264_frame_t *tmp;
838                 int i = 0;
839
840                 while( h->frames.next[i+1] != NULL ) i++;
841                 h->frames.next[i]->i_type = X264_TYPE_P;
842
843                 /* remove this P from next */
844                 tmp = h->frames.next[i];
845                 h->frames.next[i] = NULL;
846
847                 /* move this P + Bs to current */
848                 x264_frame_put( h->frames.current, tmp );
849                 while( ( tmp = x264_frame_get( h->frames.next ) ) )
850                 {
851                     x264_frame_put( h->frames.current, tmp );
852                 }
853             }
854         }
855         else if( h->param.i_bframe > 0 )
856         {
857             if( h->frames.i_last_i  + 1 >= h->param.i_iframe )
858                 fenc->i_type = X264_TYPE_I;
859             else if( h->frames.next[h->param.i_bframe-1] != NULL )
860                 fenc->i_type = X264_TYPE_P;
861             else if( pic->i_type == X264_TYPE_AUTO )
862                 fenc->i_type = X264_TYPE_B;
863             else
864                 fenc->i_type = pic->i_type;
865         }
866         else
867         {
868             if( pic->i_type == X264_TYPE_AUTO )
869             {
870                 if( h->frames.i_last_i + 1 >= h->param.i_iframe )
871                     fenc->i_type = X264_TYPE_I;
872                 else
873                     fenc->i_type = X264_TYPE_P;
874             }
875             else
876             {
877                 fenc->i_type = pic->i_type;
878             }
879         }
880
881         fenc->i_poc = h->i_poc;
882         if( fenc->i_type == X264_TYPE_IDR )
883         {
884             h->frames.i_last_idr = 0;
885             h->frames.i_last_i = 0;
886         }
887         else if( fenc->i_type == X264_TYPE_I )
888         {
889             h->frames.i_last_idr++;
890             h->frames.i_last_i = 0;
891         }
892         else
893         {
894             h->frames.i_last_i++;
895         }
896
897         /* 3: Update current/next */
898         if( fenc->i_type == X264_TYPE_B )
899         {
900             x264_frame_put( h->frames.next, fenc );
901         }
902         else
903         {
904             x264_frame_put( h->frames.current, fenc );
905             while( ( fenc = x264_frame_get( h->frames.next ) ) )
906             {
907                 x264_frame_put( h->frames.current, fenc );
908             }
909         }
910         h->i_poc += 2;
911     }
912     else    /* No more picture, begin encoding of last frames */
913     {
914         /* Move all next frames to current and mark the last one as a P */
915         x264_frame_t *tmp;
916         int i = -1;
917         while( h->frames.next[i+1] != NULL ) i++;
918         if( i >= 0 )
919         {
920             h->frames.next[i]->i_type = X264_TYPE_P;
921             tmp = h->frames.next[i];
922             h->frames.next[i] = NULL;
923
924             x264_frame_put( h->frames.current, tmp );
925             while( ( tmp = x264_frame_get( h->frames.next ) ) )
926             {
927                 x264_frame_put( h->frames.current, tmp );
928             }
929         }
930     }
931     TIMER_STOP( i_mtime_encode_frame );
932
933     /* ------------------- Get frame to be encoded ------------------------- */
934     /* 4: get picture to encode */
935     h->fenc = x264_frame_get( h->frames.current );
936     if( h->fenc == NULL )
937     {
938         /* Nothing yet to encode (ex: waiting for I/P with B frames) */
939         /* waiting for filling bframe buffer */
940         pic->i_type = X264_TYPE_AUTO;
941         return 0;
942     }
943     x264_frame_put( h->frames.unused, h->fenc );  /* Safe to do it now, we don't use frames.unused for the rest */
944
945     /* ------------------- Setup frame context ----------------------------- */
946     /* 5: Init data dependant of frame type */
947     TIMER_START( i_mtime_encode_frame );
948     if( h->fenc->i_type == X264_TYPE_IDR )
949     {
950         /* reset ref pictures */
951         x264_reference_reset( h );
952
953         i_nal_type    = NAL_SLICE_IDR;
954         i_nal_ref_idc = NAL_PRIORITY_HIGHEST;
955         i_slice_type = SLICE_TYPE_I;
956     }
957     else if( h->fenc->i_type == X264_TYPE_I )
958     {
959         i_nal_type    = NAL_SLICE;
960         i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
961         i_slice_type = SLICE_TYPE_I;
962     }
963     else if( h->fenc->i_type == X264_TYPE_P )
964     {
965         i_nal_type    = NAL_SLICE;
966         i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
967         i_slice_type = SLICE_TYPE_P;
968     }
969     else    /* B frame */
970     {
971         i_nal_type    = NAL_SLICE;
972         i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE;
973         i_slice_type = SLICE_TYPE_B;
974     }
975
976     pic->i_type     =
977     h->fdec->i_type = h->fenc->i_type;
978     h->fdec->i_poc  = h->fenc->i_poc;
979
980
981
982     /* ------------------- Init                ----------------------------- */
983     /* Init the rate control */
984     x264_ratecontrol_start( h->rc, i_slice_type );
985     i_global_qp = x264_ratecontrol_qp( h->rc );
986     if( h->fenc->i_qpplus1 > 0 )
987     {
988         i_global_qp = x264_clip3( h->fenc->i_qpplus1 - 1, 0, 51 );
989     }
990
991     /* build ref list 0/1 */
992     x264_reference_build_list( h, h->fdec->i_poc );
993
994     /* increase frame num but only once for B frame */
995     if( i_slice_type != SLICE_TYPE_B || h->sh.i_type != SLICE_TYPE_B )
996     {
997         h->i_frame_num++;
998     }
999
1000     /* ------------------------ Create slice header  ----------------------- */
1001     x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );
1002
1003     /* ---------------------- Write the bitstream -------------------------- */
1004     /* Init bitstream context */
1005     h->out.i_nal = 0;
1006     bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
1007
1008     /* Write SPS and PPS */
1009     if( i_nal_type == NAL_SLICE_IDR )
1010     {
1011         /* generate sequence parameters */
1012         x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
1013         x264_sps_write( &h->out.bs, h->sps );
1014         x264_nal_end( h );
1015
1016         /* generate picture parameters */
1017         x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
1018         x264_pps_write( &h->out.bs, h->pps );
1019         x264_nal_end( h );
1020     }
1021
1022     /* Write the slice */
1023     x264_slice_write( h, i_nal_type, i_nal_ref_idc, i_mb_count );
1024
1025     /* End bitstream, set output  */
1026     *pi_nal = h->out.i_nal;
1027     *pp_nal = &h->out.nal[0];
1028
1029     /* Set output picture properties */
1030     if( i_slice_type == SLICE_TYPE_I )
1031         pic->i_type = i_nal_type == NAL_SLICE_IDR ? X264_TYPE_IDR : X264_TYPE_I;
1032     else if( i_slice_type == SLICE_TYPE_P )
1033         pic->i_type = X264_TYPE_P;
1034     else
1035         pic->i_type = X264_TYPE_B;
1036     pic->i_pts = h->fenc->i_pts;
1037
1038     /* ---------------------- Update encoder state ------------------------- */
1039     /* update cabac */
1040     if( h->param.b_cabac )
1041     {
1042         x264_cabac_model_update( &h->cabac, i_slice_type, h->sh.pps->i_pic_init_qp + h->sh.i_qp_delta );
1043     }
1044
1045     /* handle references */
1046     if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )
1047     {
1048         x264_reference_update( h );
1049     }
1050
1051     /* increase frame count */
1052     h->i_frame++;
1053
1054     /* update rc */
1055     x264_ratecontrol_end( h->rc, h->out.nal[h->out.i_nal-1].i_payload * 8 );
1056
1057     /* restore CPU state (before using float again) */
1058     x264_cpu_restore( h->param.cpu );
1059
1060     TIMER_STOP( i_mtime_encode_frame );
1061
1062     /* ---------------------- Compute/Print statistics --------------------- */
1063     /* PSNR */
1064     psnr_y = x264_psnr( frame_psnr->plane[0], frame_psnr->i_stride[0], h->fenc->plane[0], h->fenc->i_stride[0], h->param.i_width, h->param.i_height );
1065     psnr_u = x264_psnr( frame_psnr->plane[1], frame_psnr->i_stride[1], h->fenc->plane[1], h->fenc->i_stride[1], h->param.i_width/2, h->param.i_height/2);
1066     psnr_v = x264_psnr( frame_psnr->plane[2], frame_psnr->i_stride[2], h->fenc->plane[2], h->fenc->i_stride[2], h->param.i_width/2, h->param.i_height/2);
1067
1068     /* Slice stat */
1069     h->stat.i_slice_count[i_slice_type]++;
1070     h->stat.i_slice_size[i_slice_type] += bs_pos( &h->out.bs) / 8;
1071     h->stat.f_psnr_y[i_slice_type] += psnr_y;
1072     h->stat.f_psnr_u[i_slice_type] += psnr_u;
1073     h->stat.f_psnr_v[i_slice_type] += psnr_v;
1074
1075     for( i = 0; i < 17; i++ )
1076     {
1077         h->stat.i_mb_count[h->sh.i_type][i] += i_mb_count[i];
1078     }
1079
1080     /* print stat */
1081     fprintf( stderr, "frame=%4d NAL=%d Slice:%c Poc:%-3d I4x4:%-5d I16x16:%-5d P:%-5d SKIP:%-3d size=%d bytes PSNR Y:%2.2f U:%2.2f V:%2.2f\n",
1082              h->i_frame - 1,
1083              i_nal_ref_idc,
1084              i_slice_type == SLICE_TYPE_I ? 'I' : (i_slice_type == SLICE_TYPE_P ? 'P' : 'B' ),
1085              frame_psnr->i_poc,
1086              i_mb_count[I_4x4],
1087              i_mb_count[I_16x16],
1088              i_mb_count[P_L0] + i_mb_count[P_8x8],
1089              i_mb_count[P_SKIP],
1090              h->out.nal[h->out.i_nal-1].i_payload,
1091              psnr_y, psnr_u, psnr_v );
1092 #ifdef DEBUG_MB_TYPE
1093     for( mb_xy = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
1094     {
1095         const int i_mb_y = mb_xy / h->sps->i_mb_width;
1096         const int i_mb_x = mb_xy % h->sps->i_mb_width;
1097
1098         if( i_mb_y > 0 && i_mb_x == 0 )
1099             fprintf( stderr, "\n" );
1100
1101         if( h->mb.type[mb_xy] == I_4x4 )
1102             fprintf( stderr, "i" );
1103         else if( h->mb.type[mb_xy] == I_16x16 )
1104             fprintf( stderr, "I" );
1105         else if( h->mb.type[mb_xy] == P_SKIP )
1106             fprintf( stderr, "S" );
1107         else if( h->mb.type[mb_xy] == P_8x8 )
1108             fprintf( stderr, "8" );
1109         else if( h->mb.type[mb_xy] == P_L0 )
1110             fprintf( stderr, "P" );
1111         else
1112             fprintf( stderr, "?" );
1113
1114         fprintf( stderr, " " );
1115     }
1116 #endif
1117
1118 #if DEBUG_DUMP_FRAME
1119     /* Dump reconstructed frame */
1120     x264_frame_dump( h, frame_psnr, "fdec.yuv" );
1121 #endif
1122 #if 0
1123     if( h->i_ref0 > 0 )
1124     {
1125         x264_frame_dump( h, h->fref0[0], "ref0.yuv" );
1126     }
1127     if( h->i_ref1 > 0 )
1128     {
1129         x264_frame_dump( h, h->fref1[0], "ref1.yuv" );
1130     }
1131 #endif
1132     return 0;
1133 }
1134
1135 /****************************************************************************
1136  * x264_encoder_close:
1137  ****************************************************************************/
1138 void    x264_encoder_close  ( x264_t *h )
1139 {
1140     int64_t i_mtime_total = i_mtime_analyse + i_mtime_encode + i_mtime_write + i_mtime_filter + 1;
1141     int i;
1142
1143     fprintf( stderr, "x264: analyse=%d(%lldms) encode=%d(%lldms) write=%d(%lldms) filter=%d(%lldms)\n",
1144              (int)(100*i_mtime_analyse/i_mtime_total), i_mtime_analyse/1000,
1145              (int)(100*i_mtime_encode/i_mtime_total), i_mtime_encode/1000,
1146              (int)(100*i_mtime_write/i_mtime_total), i_mtime_write/1000,
1147              (int)(100*i_mtime_filter/i_mtime_total), i_mtime_filter/1000 );
1148
1149     /* Slices used and PNSR */
1150     if( h->stat.i_slice_count[SLICE_TYPE_I] > 0 )
1151     {
1152         const int i_count = h->stat.i_slice_count[SLICE_TYPE_I];
1153         fprintf( stderr, "x264: slice I:%-4d Avg size:%-5d PSNR Y:%2.2f U:%2.2f V:%2.2f PSNR-Y/Size:%2.2f\n",
1154                  i_count,
1155                  h->stat.i_slice_size[SLICE_TYPE_I] / i_count,
1156                  h->stat.f_psnr_y[SLICE_TYPE_I] / i_count,
1157                  h->stat.f_psnr_u[SLICE_TYPE_I] / i_count,
1158                  h->stat.f_psnr_v[SLICE_TYPE_I] / i_count,
1159                  1000*h->stat.f_psnr_y[SLICE_TYPE_I] / h->stat.i_slice_size[SLICE_TYPE_I] );
1160     }
1161     if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
1162     {
1163         const int i_count = h->stat.i_slice_count[SLICE_TYPE_P];
1164         fprintf( stderr, "x264: slice P:%-4d Avg size:%-5d PSNR Y:%2.2f U:%2.2f V:%2.2f PSNR-Y/Size:%2.2f\n",
1165                 i_count,
1166                 h->stat.i_slice_size[SLICE_TYPE_P] / i_count,
1167                 h->stat.f_psnr_y[SLICE_TYPE_P] / i_count,
1168                 h->stat.f_psnr_u[SLICE_TYPE_P] / i_count,
1169                 h->stat.f_psnr_v[SLICE_TYPE_P] / i_count,
1170                 1000.0*h->stat.f_psnr_y[SLICE_TYPE_P] / h->stat.i_slice_size[SLICE_TYPE_P] );
1171     }
1172     if( h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
1173     {
1174         fprintf( stderr, "x264: slice B:%-4d Avg size:%-5d PSNR Y:%2.2f U:%2.2f V:%2.2f PSNR-Y/Size:%2.2f\n",
1175                 h->stat.i_slice_count[SLICE_TYPE_B],
1176                 h->stat.i_slice_size[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1177                 h->stat.f_psnr_y[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1178                 h->stat.f_psnr_u[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1179                 h->stat.f_psnr_v[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1180                 1000*h->stat.f_psnr_y[SLICE_TYPE_B] / h->stat.i_slice_size[SLICE_TYPE_B] );
1181     }
1182
1183     /* MB types used */
1184     if( h->stat.i_slice_count[SLICE_TYPE_I] > 0 )
1185     {
1186         const int i_count =  h->stat.i_slice_count[SLICE_TYPE_I];
1187         fprintf( stderr, "x264: slice I      Avg I4x4:%-5d I16x16:%-5d\n",
1188                  h->stat.i_mb_count[SLICE_TYPE_I][I_4x4]  / i_count,
1189                  h->stat.i_mb_count[SLICE_TYPE_I][I_16x16]/ i_count );
1190     }
1191     if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
1192     {
1193         const int i_count = h->stat.i_slice_count[SLICE_TYPE_P];
1194         fprintf( stderr, "x264: slice P      Avg I4x4:%-5d I16x16:%-5d P:%-5d P8x8:%-5d PSKIP:%-5d\n",
1195                  h->stat.i_mb_count[SLICE_TYPE_P][I_4x4]  / i_count,
1196                  h->stat.i_mb_count[SLICE_TYPE_P][I_16x16]/ i_count,
1197                  h->stat.i_mb_count[SLICE_TYPE_P][P_L0] / i_count,
1198                  h->stat.i_mb_count[SLICE_TYPE_P][P_8x8] / i_count,
1199                  h->stat.i_mb_count[SLICE_TYPE_P][P_SKIP] /i_count );
1200     }
1201
1202     {
1203         const int i_count = h->stat.i_slice_count[SLICE_TYPE_I] +
1204                             h->stat.i_slice_count[SLICE_TYPE_P] +
1205                             h->stat.i_slice_count[SLICE_TYPE_B];
1206
1207         fprintf( stderr, "x264: overall PSNR Y:%2.2f U:%2.2f V:%2.2f kb/s:%.1f fps:%.3f\n",
1208                  (h->stat.f_psnr_y[SLICE_TYPE_I]+h->stat.f_psnr_y[SLICE_TYPE_P]+h->stat.f_psnr_y[SLICE_TYPE_B]) / i_count,
1209                  (h->stat.f_psnr_u[SLICE_TYPE_I]+h->stat.f_psnr_u[SLICE_TYPE_P]+h->stat.f_psnr_u[SLICE_TYPE_B]) / i_count,
1210                  (h->stat.f_psnr_v[SLICE_TYPE_I]+h->stat.f_psnr_v[SLICE_TYPE_P]+h->stat.f_psnr_v[SLICE_TYPE_B]) / i_count,
1211                  h->param.f_fps * 8*(h->stat.i_slice_size[SLICE_TYPE_I]+h->stat.i_slice_size[SLICE_TYPE_P]+h->stat.i_slice_size[SLICE_TYPE_B]) / i_count / 1024,
1212                  (double)1000000.0 * (double)i_count / (double)i_mtime_encode_frame );
1213     }
1214
1215     /* frames */
1216     for( i = 0; i < X264_BFRAME_MAX + 1; i++ )
1217     {
1218         if( h->frames.current[i] ) x264_frame_delete( h->frames.current[i] );
1219         if( h->frames.next[i] )    x264_frame_delete( h->frames.next[i] );
1220         if( h->frames.unused[i] )  x264_frame_delete( h->frames.unused[i] );
1221     }
1222     /* ref frames */
1223     for( i = 0; i < h->param.i_frame_reference+2; i++ )
1224     {
1225         x264_frame_delete( h->frames.reference[i] );
1226     }
1227
1228     /* rc */
1229     x264_ratecontrol_delete( h->rc );
1230
1231     x264_macroblock_cache_end( h );
1232     x264_free( h->out.p_bitstream );
1233     x264_free( h );
1234 }
1235