]> git.sesse.net Git - x264/blob - decoder/decoder.c
remove relative include paths, to avoid conflicts with libtool
[x264] / decoder / decoder.c
1 /*****************************************************************************
2  * x264: h264 decoder
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: decoder.c,v 1.1 2004/06/03 19:27:07 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 "common/common.h"
30 #include "common/cpu.h"
31 #include "common/vlc.h"
32
33 #include "macroblock.h"
34 #include "set.h"
35 #include "vlc.h"
36
37
38 static void x264_slice_idr( x264_t *h )
39 {
40     int i;
41
42     h->i_poc_msb = 0;
43     h->i_poc_lsb = 0;
44     h->i_frame_offset = 0;
45     h->i_frame_num = 0;
46
47     if( h->sps )
48     {
49         for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
50         {
51             h->freference[i]->i_poc = -1;
52         }
53
54         h->fdec = h->freference[0];
55         h->i_ref0 = 0;
56         h->i_ref1 = 0;
57     }
58 }
59
60 /* The slice reading is split in two part:
61  *      - before ref_pic_list_reordering( )
62  *      - after  dec_ref_pic_marking( )
63  */
64 static int x264_slice_header_part1_read( bs_t *s,
65                                          x264_slice_header_t *sh, x264_sps_t sps_array[32], x264_pps_t pps_array[256], int b_idr )
66 {
67     sh->i_first_mb = bs_read_ue( s );
68     sh->i_type = bs_read_ue( s );
69     if( sh->i_type >= 5 )
70     {
71         sh->i_type -= 5;
72     }
73     sh->i_pps_id = bs_read_ue( s );
74     if( bs_eof( s ) || sh->i_pps_id >= 256 || pps_array[sh->i_pps_id].i_id == -1 )
75     {
76         fprintf( stderr, "invalid pps_id in slice header\n" );
77         return -1;
78     }
79
80     sh->pps = &pps_array[sh->i_pps_id];
81     sh->sps = &sps_array[sh->pps->i_sps_id];    /* valid if pps valid */
82
83     sh->i_frame_num = bs_read( s, sh->sps->i_log2_max_frame_num );
84     if( !sh->sps->b_frame_mbs_only )
85     {
86         sh->b_field_pic = bs_read1( s );
87         if( sh->b_field_pic )
88         {
89             sh->b_bottom_field = bs_read1( s );
90         }
91     }
92
93     if( b_idr )
94     {
95         sh->i_idr_pic_id = bs_read_ue( s );
96     }
97     else
98     {
99         sh->i_idr_pic_id = 0;
100     }
101
102     if( sh->sps->i_poc_type == 0 )
103     {
104         sh->i_poc_lsb = bs_read( s, sh->sps->i_log2_max_poc_lsb );
105         if( sh->pps->b_pic_order && !sh->b_field_pic )
106         {
107             sh->i_delta_poc_bottom = bs_read_se( s );
108         }
109     }
110     else if( sh->sps->i_poc_type == 1 && !sh->sps->b_delta_pic_order_always_zero )
111     {
112         sh->i_delta_poc[0] = bs_read_se( s );
113         if( sh->pps->b_pic_order && !sh->b_field_pic )
114         {
115             sh->i_delta_poc[1] = bs_read_se( s );
116         }
117     }
118
119     if( sh->pps->b_redundant_pic_cnt )
120     {
121         sh->i_redundant_pic_cnt = bs_read_ue( s );
122     }
123
124     if( sh->i_type == SLICE_TYPE_B )
125     {
126         sh->b_direct_spatial_mv_pred = bs_read1( s );
127     }
128
129     if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_B )
130     {
131         sh->b_num_ref_idx_override = bs_read1( s );
132
133         sh->i_num_ref_idx_l0_active = sh->pps->i_num_ref_idx_l0_active; /* default */
134         sh->i_num_ref_idx_l1_active = sh->pps->i_num_ref_idx_l1_active; /* default */
135
136         if( sh->b_num_ref_idx_override )
137         {
138             sh->i_num_ref_idx_l0_active = bs_read_ue( s ) + 1;
139             if( sh->i_type == SLICE_TYPE_B )
140             {
141                 sh->i_num_ref_idx_l1_active = bs_read_ue( s ) + 1;
142             }
143         }
144     }
145
146     return bs_eof( s ) ? -1 : 0;
147 }
148
149 static int x264_slice_header_part2_read( bs_t *s, x264_slice_header_t *sh )
150 {
151     if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I && sh->i_type != SLICE_TYPE_SI )
152     {
153         sh->i_cabac_init_idc = bs_read_ue( s );
154     }
155     sh->i_qp_delta = bs_read_se( s );
156
157     if( sh->i_type == SLICE_TYPE_SI || sh->i_type == SLICE_TYPE_SP )
158     {
159         if( sh->i_type == SLICE_TYPE_SP )
160         {
161             sh->b_sp_for_swidth = bs_read1( s );
162         }
163         sh->i_qs_delta = bs_read_se( s );
164     }
165
166     if( sh->pps->b_deblocking_filter_control )
167     {
168         sh->i_disable_deblocking_filter_idc = bs_read_ue( s );
169         if( sh->i_disable_deblocking_filter_idc != 1 )
170         {
171             sh->i_alpha_c0_offset = bs_read_se( s );
172             sh->i_beta_offset = bs_read_se( s );
173         }
174     }
175     else
176     {
177         sh->i_alpha_c0_offset = 0;
178         sh->i_beta_offset = 0;
179     }
180
181     if( sh->pps->i_num_slice_groups > 1 && sh->pps->i_slice_group_map_type >= 3 && sh->pps->i_slice_group_map_type <= 5 )
182     {
183         /* FIXME */
184         return -1;
185     }
186     return 0;
187 }
188
189 static int x264_slice_header_ref_pic_reordering( x264_t *h, bs_t *s )
190 {
191     int b_ok;
192     int i;
193
194     /* use the no more use frame */
195     h->fdec = h->freference[0];
196     h->fdec->i_poc = h->i_poc;
197
198     /* build ref list 0/1 */
199     h->i_ref0 = 0;
200     h->i_ref1 = 0;
201     for( i = 1; i < h->sps->i_num_ref_frames + 1; i++ )
202     {
203         if( h->freference[i]->i_poc >= 0 )
204         {
205             if( h->freference[i]->i_poc < h->fdec->i_poc )
206             {
207                 h->fref0[h->i_ref0++] = h->freference[i];
208             }
209             else if( h->freference[i]->i_poc > h->fdec->i_poc )
210             {
211                 h->fref1[h->i_ref1++] = h->freference[i];
212             }
213         }
214     }
215
216     /* Order ref0 from higher to lower poc */
217     do
218     {
219         b_ok = 1;
220         for( i = 0; i < h->i_ref0 - 1; i++ )
221         {
222             if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
223             {
224                 x264_frame_t *tmp = h->fref0[i+1];
225
226                 h->fref0[i+1] = h->fref0[i];
227                 h->fref0[i] = tmp;
228                 b_ok = 0;
229                 break;
230             }
231         }
232     } while( !b_ok );
233     /* Order ref1 from lower to higher poc (bubble sort) for B-frame */
234     do
235     {
236         b_ok = 1;
237         for( i = 0; i < h->i_ref1 - 1; i++ )
238         {
239             if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
240             {
241                 x264_frame_t *tmp = h->fref1[i+1];
242
243                 h->fref1[i+1] = h->fref1[i];
244                 h->fref1[i] = tmp;
245                 b_ok = 0;
246                 break;
247             }
248         }
249     } while( !b_ok );
250
251     if( h->i_ref0 > h->pps->i_num_ref_idx_l0_active )
252     {
253         h->i_ref0 = h->pps->i_num_ref_idx_l0_active;
254     }
255     if( h->i_ref1 > h->pps->i_num_ref_idx_l1_active )
256     {
257         h->i_ref1 = h->pps->i_num_ref_idx_l1_active;
258     }
259
260     //fprintf( stderr,"POC:%d ref0=%d POC0=%d\n", h->fdec->i_poc, h->i_ref0, h->i_ref0 > 0 ? h->fref0[0]->i_poc : -1 );
261
262
263     /* Now parse the stream and change the default order */
264     if( h->sh.i_type != SLICE_TYPE_I && h->sh.i_type != SLICE_TYPE_SI )
265     {
266         int b_reorder = bs_read1( s );
267
268         if( b_reorder )
269         {
270             /* FIXME */
271             return -1;
272         }
273     }
274     if( h->sh.i_type == SLICE_TYPE_B )
275     {
276         int b_reorder = bs_read1( s );
277         if( b_reorder )
278         {
279             /* FIXME */
280             return -1;
281         }
282     }
283     return 0;
284 }
285
286 static int x264_slice_header_pred_weight_table( x264_t *h, bs_t *s )
287 {
288     return -1;
289 }
290
291 static int  x264_slice_header_dec_ref_pic_marking( x264_t *h, bs_t *s, int i_nal_type  )
292 {
293     if( i_nal_type == NAL_SLICE_IDR )
294     {
295         int b_no_output_of_prior_pics = bs_read1( s );
296         int b_long_term_reference_flag = bs_read1( s );
297
298         /* TODO */
299         if( b_no_output_of_prior_pics )
300         {
301
302         }
303
304         if( b_long_term_reference_flag )
305         {
306
307         }
308     }
309     else
310     {
311         int b_adaptive_ref_pic_marking_mode = bs_read1( s );
312         if( b_adaptive_ref_pic_marking_mode )
313         {
314             return -1;
315         }
316     }
317     return 0;
318 }
319
320 /****************************************************************************
321  * Decode a slice header and setup h for mb decoding.
322  ****************************************************************************/
323 static int x264_slice_header_decode( x264_t *h, bs_t *s, x264_nal_t *nal )
324 {
325     /* read the first part of the slice */
326     if( x264_slice_header_part1_read( s, &h->sh,
327                                       h->sps_array, h->pps_array,
328                                       nal->i_type == NAL_SLICE_IDR ? 1 : 0 ) < 0 )
329     {
330         fprintf( stderr, "x264_slice_header_part1_read failed\n" );
331         return -1;
332     }
333
334     /* now reset h if needed for this frame */
335     if( h->sps != h->sh.sps || h->pps != h->sh.pps )
336     {
337         int i;
338         /* TODO */
339
340         h->sps = NULL;
341         h->pps = NULL;
342         if( h->picture->i_width != 0 && h->picture->i_height != 0 )
343         {
344             for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
345             {
346                 x264_frame_delete( h->freference[i]);
347             }
348             free( h->mb );
349         }
350
351         h->picture->i_width = 0;
352         h->picture->i_height = 0;
353     }
354
355     /* and init if needed */
356     if( h->sps == NULL || h->pps == NULL )
357     {
358         int i;
359
360         h->sps = h->sh.sps;
361         h->pps = h->sh.pps;
362
363         h->param.i_width = h->picture->i_width = 16 * h->sps->i_mb_width;
364         h->param.i_height= h->picture->i_height= 16 * h->sps->i_mb_height;
365
366         fprintf( stderr, "x264: %dx%d\n", h->picture->i_width, h->picture->i_height );
367
368         h->mb = x264_macroblocks_new( h->sps->i_mb_width, h->sps->i_mb_height );
369
370         for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
371         {
372             h->freference[i] = x264_frame_new( h );
373             h->freference[i]->i_poc = -1;
374         }
375         h->fdec = h->freference[0];
376         h->i_ref0 = 0;
377         h->i_ref1 = 0;
378
379         h->i_poc_msb = 0;
380         h->i_poc_lsb = 0;
381         h->i_frame_offset = 0;
382         h->i_frame_num = 0;
383     }
384
385     /* calculate poc for current frame */
386     if( h->sps->i_poc_type == 0 )
387     {
388         int i_max_poc_lsb = 1 << h->sps->i_log2_max_poc_lsb;
389
390         if( h->sh.i_poc_lsb < h->i_poc_lsb && h->i_poc_lsb - h->sh.i_poc_lsb >= i_max_poc_lsb/2 )
391         {
392             h->i_poc_msb += i_max_poc_lsb;
393         }
394         else if( h->sh.i_poc_lsb > h->i_poc_lsb  && h->sh.i_poc_lsb - h->i_poc_lsb > i_max_poc_lsb/2 )
395         {
396             h->i_poc_msb -= i_max_poc_lsb;
397         }
398         h->i_poc_lsb = h->sh.i_poc_lsb;
399
400         h->i_poc = h->i_poc_msb + h->sh.i_poc_lsb;
401     }
402     else if( h->sps->i_poc_type == 1 )
403     {
404         /* FIXME */
405         return -1;
406     }
407     else
408     {
409         if( nal->i_type == NAL_SLICE_IDR )
410         {
411             h->i_frame_offset = 0;
412             h->i_poc = 0;
413         }
414         else
415         {
416             if( h->sh.i_frame_num < h->i_frame_num )
417             {
418                 h->i_frame_offset += 1 << h->sps->i_log2_max_frame_num;
419             }
420             if( nal->i_ref_idc > 0 )
421             {
422                 h->i_poc = 2 * ( h->i_frame_offset + h->sh.i_frame_num );
423             }
424             else
425             {
426                 h->i_poc = 2 * ( h->i_frame_offset + h->sh.i_frame_num ) - 1;
427             }
428         }
429         h->i_frame_num = h->sh.i_frame_num;
430     }
431
432     fprintf( stderr, "x264: pic type=%s poc:%d\n",
433              h->sh.i_type == SLICE_TYPE_I ? "I" : (h->sh.i_type == SLICE_TYPE_P ? "P" : "B?" ),
434              h->i_poc );
435
436     if( h->sh.i_type != SLICE_TYPE_I && h->sh.i_type != SLICE_TYPE_P )
437     {
438         fprintf( stderr, "only SLICE I/P supported\n" );
439         return -1;
440     }
441
442     /* read and do the ref pic reordering */
443     if( x264_slice_header_ref_pic_reordering( h, s ) < 0 )
444     {
445         return -1;
446     }
447
448     if( ( (h->sh.i_type == SLICE_TYPE_P || h->sh.i_type == SLICE_TYPE_SP) && h->sh.pps->b_weighted_pred  ) ||
449         ( h->sh.i_type == SLICE_TYPE_B && h->sh.pps->b_weighted_bipred ) )
450     {
451         if( x264_slice_header_pred_weight_table( h, s ) < 0 )
452         {
453             return -1;
454         }
455     }
456
457     if( nal->i_ref_idc != 0 )
458     {
459         x264_slice_header_dec_ref_pic_marking( h, s, nal->i_type );
460     }
461
462     if( x264_slice_header_part2_read( s, &h->sh ) < 0 )
463     {
464         return -1;
465     }
466
467     return 0;
468 }
469
470 static int x264_slice_data_decode( x264_t *h, bs_t *s )
471 {
472     int mb_xy = h->sh.i_first_mb;
473     int i_ret = 0;
474
475     if( h->pps->b_cabac )
476     {
477         /* TODO: alignement and cabac init */
478     }
479
480     /* FIXME field decoding */
481     for( ;; )
482     {
483         x264_mb_context_t context;
484         x264_macroblock_t *mb;
485
486         if( mb_xy >= h->sps->i_mb_width * h->sps->i_mb_height )
487         {
488             break;
489         }
490
491         mb = &h->mb[mb_xy];
492
493         /* load neighbour */
494         x264_macroblock_context_load( h, mb, &context );
495
496
497         if( h->pps->b_cabac )
498         {
499             if( h->sh.i_type != SLICE_TYPE_I && h->sh.i_type != SLICE_TYPE_SI )
500             {
501                 /* TODO */
502             }
503             i_ret = x264_macroblock_read_cabac( h, s, mb );
504         }
505         else
506         {
507             if( h->sh.i_type != SLICE_TYPE_I && h->sh.i_type != SLICE_TYPE_SI )
508             {
509                 int i_skip = bs_read_ue( s );
510
511                 while( i_skip > 0 )
512                 {
513                     x264_macroblock_decode_skip( h, mb );
514
515                     /* next macroblock */
516                     mb_xy++;
517                     if( mb_xy >= h->sps->i_mb_width * h->sps->i_mb_height )
518                     {
519                         break;
520                     }
521                     mb++;
522
523                     /* load neighbour */
524                     x264_macroblock_context_load( h, mb, &context );
525
526                     i_skip--;
527                 }
528                 if( mb_xy >= h->sps->i_mb_width * h->sps->i_mb_height )
529                 {
530                     break;
531                 }
532             }
533             i_ret = x264_macroblock_read_cavlc( h, s, mb );
534         }
535
536         if( i_ret < 0 )
537         {
538             fprintf( stderr, "x264_macroblock_read failed [%d,%d]\n", mb->i_mb_x, mb->i_mb_y );
539             break;
540         }
541
542         if( x264_macroblock_decode( h, mb ) < 0 )
543         {
544             fprintf( stderr, "x264_macroblock_decode failed\n" );
545             /* try to do some error correction ;) */
546         }
547
548         mb_xy++;
549     }
550
551     if( i_ret >= 0 )
552     {
553         int i;
554
555         /* expand border for frame reference TODO avoid it when using b-frame */
556         x264_frame_expand_border( h->fdec );
557
558         /* apply deblocking filter to the current decoded picture */
559         if( !h->pps->b_deblocking_filter_control || h->sh.i_disable_deblocking_filter_idc != 1 )
560         {
561             x264_frame_deblocking_filter( h, h->sh.i_type );
562         }
563
564 #if 0
565         /* expand border for frame reference TODO avoid it when using b-frame */
566         x264_frame_expand_border( h->fdec );
567 #endif
568
569         h->picture->i_plane = h->fdec->i_plane;
570         for( i = 0; i < h->picture->i_plane; i++ )
571         {
572             h->picture->i_stride[i] = h->fdec->i_stride[i];
573             h->picture->plane[i]    = h->fdec->plane[i];
574         }
575
576         /* move frame in the buffer FIXME won't work for B-frame */
577         h->fdec = h->freference[h->sps->i_num_ref_frames];
578         for( i = h->sps->i_num_ref_frames; i > 0; i-- )
579         {
580             h->freference[i] = h->freference[i-1];
581         }
582         h->freference[0] = h->fdec;
583     }
584
585     return i_ret;
586 }
587
588 /****************************************************************************
589  *
590  ******************************* x264 libs **********************************
591  *
592  ****************************************************************************/
593
594 /****************************************************************************
595  * x264_decoder_open:
596  ****************************************************************************/
597 x264_t *x264_decoder_open   ( x264_param_t *param )
598 {
599     x264_t *h = x264_malloc( sizeof( x264_t ) );
600     int i;
601
602     memcpy( &h->param, param, sizeof( x264_param_t ) );
603
604     h->cpu = param->cpu;
605
606     /* no SPS and PPS active yet */
607     h->sps = NULL;
608     h->pps = NULL;
609
610     for( i = 0; i < 32; i++ )
611     {
612         h->sps_array[i].i_id = -1;  /* invalidate it */
613     }
614     for( i = 0; i < 256; i++ )
615     {
616         h->pps_array[i].i_id = -1;  /* invalidate it */
617     }
618
619     h->picture = x264_malloc( sizeof( x264_picture_t ) );
620     h->picture->i_width = 0;
621     h->picture->i_height= 0;
622
623     /* init predict_XxX */
624     x264_predict_16x16_init( h->cpu, h->predict_16x16 );
625     x264_predict_8x8_init( h->cpu, h->predict_8x8 );
626     x264_predict_4x4_init( h->cpu, h->predict_4x4 );
627
628     x264_pixel_init( h->cpu, &h->pixf );
629     x264_dct_init( h->cpu, &h->dctf );
630
631     x264_mc_init( h->cpu, h->mc );
632
633     /* create the vlc table (we could remove it from x264_t but it will need
634      * to introduce a x264_init() for global librarie) */
635     for( i = 0; i < 5; i++ )
636     {
637         /* max 2 step */
638         h->x264_coeff_token_lookup[i] = x264_vlc_table_lookup_new( x264_coeff_token[i], 17*4, 4 );
639     }
640     /* max 2 step */
641     h->x264_level_prefix_lookup = x264_vlc_table_lookup_new( x264_level_prefix, 16, 8 );
642
643     for( i = 0; i < 15; i++ )
644     {
645         /* max 1 step */
646         h->x264_total_zeros_lookup[i] = x264_vlc_table_lookup_new( x264_total_zeros[i], 16, 9 );
647     }
648     for( i = 0;i < 3; i++ )
649     {
650         /* max 1 step */
651         h->x264_total_zeros_dc_lookup[i] = x264_vlc_table_lookup_new( x264_total_zeros_dc[i], 4, 3 );
652     }
653     for( i = 0;i < 7; i++ )
654     {
655         /* max 2 step */
656         h->x264_run_before_lookup[i] = x264_vlc_table_lookup_new( x264_run_before[i], 15, 6 );
657     }
658
659     return h;
660 }
661
662 /****************************************************************************
663  * x264_decoder_decode: decode one nal unit
664  ****************************************************************************/
665 int     x264_decoder_decode( x264_t *h,
666                              x264_picture_t **pp_pic, x264_nal_t *nal )
667 {
668     int i_ret = 0;
669     bs_t bs;
670
671     /* no picture */
672     *pp_pic = NULL;
673
674     /* init bitstream reader */
675     bs_init( &bs, nal->p_payload, nal->i_payload );
676
677     switch( nal->i_type )
678     {
679         case NAL_SPS:
680             if( ( i_ret = x264_sps_read( &bs, h->sps_array ) ) < 0 )
681             {
682                 fprintf( stderr, "x264: x264_sps_read failed\n" );
683             }
684             break;
685
686         case NAL_PPS:
687             if( ( i_ret = x264_pps_read( &bs, h->pps_array ) ) < 0 )
688             {
689                 fprintf( stderr, "x264: x264_pps_read failed\n" );
690             }
691             break;
692
693         case NAL_SLICE_IDR:
694             fprintf( stderr, "x264: NAL_SLICE_IDR\n" );
695             x264_slice_idr( h );
696
697         case NAL_SLICE:
698             if( ( i_ret = x264_slice_header_decode( h, &bs, nal ) ) < 0 )
699             {
700                 fprintf( stderr, "x264: x264_slice_header_decode failed\n" );
701             }
702             if( h->sh.i_redundant_pic_cnt == 0 && i_ret == 0 )
703             {
704                 if( ( i_ret = x264_slice_data_decode( h, &bs ) ) < 0 )
705                 {
706                     fprintf( stderr, "x264: x264_slice_data_decode failed\n" );
707                 }
708                 else
709                 {
710                     *pp_pic = h->picture;
711                 }
712             }
713             break;
714
715         case NAL_SLICE_DPA:
716         case NAL_SLICE_DPB:
717         case NAL_SLICE_DPC:
718             fprintf( stderr, "partitioned stream unsupported\n" );
719             i_ret = -1;
720             break;
721
722         case NAL_SEI:
723         default:
724             break;
725     }
726
727     /* restore CPU state (before using float again) */
728     x264_cpu_restore( h->cpu );
729
730     return i_ret;
731 }
732
733 /****************************************************************************
734  * x264_decoder_close:
735  ****************************************************************************/
736 void    x264_decoder_close  ( x264_t *h )
737 {
738     int i;
739
740     if( h->picture->i_width != 0 && h->picture->i_height != 0 )
741     {
742         for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
743         {
744             x264_frame_delete( h->freference[i]);
745         }
746         x264_free( h->mb );
747     }
748
749     /* free vlc table */
750     for( i = 0; i < 5; i++ )
751     {
752         x264_vlc_table_lookup_delete( h->x264_coeff_token_lookup[i] );
753     }
754     x264_vlc_table_lookup_delete( h->x264_level_prefix_lookup );
755
756     for( i = 0; i < 15; i++ )
757     {
758         x264_vlc_table_lookup_delete( h->x264_total_zeros_lookup[i] );
759     }
760     for( i = 0;i < 3; i++ )
761     {
762         x264_vlc_table_lookup_delete( h->x264_total_zeros_dc_lookup[i] );
763     }
764     for( i = 0;i < 7; i++ )
765     {
766         x264_vlc_table_lookup_delete( h->x264_run_before_lookup[i] );
767     }
768
769     x264_free( h->picture );
770     x264_free( h );
771 }
772