1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: decoder.c,v 1.1 2004/06/03 19:27:07 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
29 #include "common/common.h"
30 #include "common/cpu.h"
31 #include "common/vlc.h"
33 #include "macroblock.h"
38 static void x264_slice_idr( x264_t *h )
44 h->i_frame_offset = 0;
49 for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
51 h->freference[i]->i_poc = -1;
54 h->fdec = h->freference[0];
60 /* The slice reading is split in two part:
61 * - before ref_pic_list_reordering( )
62 * - after dec_ref_pic_marking( )
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 )
67 sh->i_first_mb = bs_read_ue( s );
68 sh->i_type = bs_read_ue( s );
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 )
76 fprintf( stderr, "invalid pps_id in slice header\n" );
80 sh->pps = &pps_array[sh->i_pps_id];
81 sh->sps = &sps_array[sh->pps->i_sps_id]; /* valid if pps valid */
83 sh->i_frame_num = bs_read( s, sh->sps->i_log2_max_frame_num );
84 if( !sh->sps->b_frame_mbs_only )
86 sh->b_field_pic = bs_read1( s );
89 sh->b_bottom_field = bs_read1( s );
95 sh->i_idr_pic_id = bs_read_ue( s );
102 if( sh->sps->i_poc_type == 0 )
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 )
107 sh->i_delta_poc_bottom = bs_read_se( s );
110 else if( sh->sps->i_poc_type == 1 && !sh->sps->b_delta_pic_order_always_zero )
112 sh->i_delta_poc[0] = bs_read_se( s );
113 if( sh->pps->b_pic_order && !sh->b_field_pic )
115 sh->i_delta_poc[1] = bs_read_se( s );
119 if( sh->pps->b_redundant_pic_cnt )
121 sh->i_redundant_pic_cnt = bs_read_ue( s );
124 if( sh->i_type == SLICE_TYPE_B )
126 sh->b_direct_spatial_mv_pred = bs_read1( s );
129 if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_B )
131 sh->b_num_ref_idx_override = bs_read1( s );
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 */
136 if( sh->b_num_ref_idx_override )
138 sh->i_num_ref_idx_l0_active = bs_read_ue( s ) + 1;
139 if( sh->i_type == SLICE_TYPE_B )
141 sh->i_num_ref_idx_l1_active = bs_read_ue( s ) + 1;
146 return bs_eof( s ) ? -1 : 0;
149 static int x264_slice_header_part2_read( bs_t *s, x264_slice_header_t *sh )
151 if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I && sh->i_type != SLICE_TYPE_SI )
153 sh->i_cabac_init_idc = bs_read_ue( s );
155 sh->i_qp_delta = bs_read_se( s );
157 if( sh->i_type == SLICE_TYPE_SI || sh->i_type == SLICE_TYPE_SP )
159 if( sh->i_type == SLICE_TYPE_SP )
161 sh->b_sp_for_swidth = bs_read1( s );
163 sh->i_qs_delta = bs_read_se( s );
166 if( sh->pps->b_deblocking_filter_control )
168 sh->i_disable_deblocking_filter_idc = bs_read_ue( s );
169 if( sh->i_disable_deblocking_filter_idc != 1 )
171 sh->i_alpha_c0_offset = bs_read_se( s );
172 sh->i_beta_offset = bs_read_se( s );
177 sh->i_alpha_c0_offset = 0;
178 sh->i_beta_offset = 0;
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 )
189 static int x264_slice_header_ref_pic_reordering( x264_t *h, bs_t *s )
194 /* use the no more use frame */
195 h->fdec = h->freference[0];
196 h->fdec->i_poc = h->i_poc;
198 /* build ref list 0/1 */
201 for( i = 1; i < h->sps->i_num_ref_frames + 1; i++ )
203 if( h->freference[i]->i_poc >= 0 )
205 if( h->freference[i]->i_poc < h->fdec->i_poc )
207 h->fref0[h->i_ref0++] = h->freference[i];
209 else if( h->freference[i]->i_poc > h->fdec->i_poc )
211 h->fref1[h->i_ref1++] = h->freference[i];
216 /* Order ref0 from higher to lower poc */
220 for( i = 0; i < h->i_ref0 - 1; i++ )
222 if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
224 x264_frame_t *tmp = h->fref0[i+1];
226 h->fref0[i+1] = h->fref0[i];
233 /* Order ref1 from lower to higher poc (bubble sort) for B-frame */
237 for( i = 0; i < h->i_ref1 - 1; i++ )
239 if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
241 x264_frame_t *tmp = h->fref1[i+1];
243 h->fref1[i+1] = h->fref1[i];
251 if( h->i_ref0 > h->pps->i_num_ref_idx_l0_active )
253 h->i_ref0 = h->pps->i_num_ref_idx_l0_active;
255 if( h->i_ref1 > h->pps->i_num_ref_idx_l1_active )
257 h->i_ref1 = h->pps->i_num_ref_idx_l1_active;
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 );
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 )
266 int b_reorder = bs_read1( s );
274 if( h->sh.i_type == SLICE_TYPE_B )
276 int b_reorder = bs_read1( s );
286 static int x264_slice_header_pred_weight_table( x264_t *h, bs_t *s )
291 static int x264_slice_header_dec_ref_pic_marking( x264_t *h, bs_t *s, int i_nal_type )
293 if( i_nal_type == NAL_SLICE_IDR )
295 int b_no_output_of_prior_pics = bs_read1( s );
296 int b_long_term_reference_flag = bs_read1( s );
299 if( b_no_output_of_prior_pics )
304 if( b_long_term_reference_flag )
311 int b_adaptive_ref_pic_marking_mode = bs_read1( s );
312 if( b_adaptive_ref_pic_marking_mode )
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 )
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 )
330 fprintf( stderr, "x264_slice_header_part1_read failed\n" );
334 /* now reset h if needed for this frame */
335 if( h->sps != h->sh.sps || h->pps != h->sh.pps )
342 if( h->picture->i_width != 0 && h->picture->i_height != 0 )
344 for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
346 x264_frame_delete( h->freference[i]);
351 h->picture->i_width = 0;
352 h->picture->i_height = 0;
355 /* and init if needed */
356 if( h->sps == NULL || h->pps == NULL )
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;
366 fprintf( stderr, "x264: %dx%d\n", h->picture->i_width, h->picture->i_height );
368 h->mb = x264_macroblocks_new( h->sps->i_mb_width, h->sps->i_mb_height );
370 for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
372 h->freference[i] = x264_frame_new( h );
373 h->freference[i]->i_poc = -1;
375 h->fdec = h->freference[0];
381 h->i_frame_offset = 0;
385 /* calculate poc for current frame */
386 if( h->sps->i_poc_type == 0 )
388 int i_max_poc_lsb = 1 << h->sps->i_log2_max_poc_lsb;
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 )
392 h->i_poc_msb += i_max_poc_lsb;
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 )
396 h->i_poc_msb -= i_max_poc_lsb;
398 h->i_poc_lsb = h->sh.i_poc_lsb;
400 h->i_poc = h->i_poc_msb + h->sh.i_poc_lsb;
402 else if( h->sps->i_poc_type == 1 )
409 if( nal->i_type == NAL_SLICE_IDR )
411 h->i_frame_offset = 0;
416 if( h->sh.i_frame_num < h->i_frame_num )
418 h->i_frame_offset += 1 << h->sps->i_log2_max_frame_num;
420 if( nal->i_ref_idc > 0 )
422 h->i_poc = 2 * ( h->i_frame_offset + h->sh.i_frame_num );
426 h->i_poc = 2 * ( h->i_frame_offset + h->sh.i_frame_num ) - 1;
429 h->i_frame_num = h->sh.i_frame_num;
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?" ),
436 if( h->sh.i_type != SLICE_TYPE_I && h->sh.i_type != SLICE_TYPE_P )
438 fprintf( stderr, "only SLICE I/P supported\n" );
442 /* read and do the ref pic reordering */
443 if( x264_slice_header_ref_pic_reordering( h, s ) < 0 )
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 ) )
451 if( x264_slice_header_pred_weight_table( h, s ) < 0 )
457 if( nal->i_ref_idc != 0 )
459 x264_slice_header_dec_ref_pic_marking( h, s, nal->i_type );
462 if( x264_slice_header_part2_read( s, &h->sh ) < 0 )
470 static int x264_slice_data_decode( x264_t *h, bs_t *s )
472 int mb_xy = h->sh.i_first_mb;
475 if( h->pps->b_cabac )
477 /* TODO: alignement and cabac init */
480 /* FIXME field decoding */
483 x264_mb_context_t context;
484 x264_macroblock_t *mb;
486 if( mb_xy >= h->sps->i_mb_width * h->sps->i_mb_height )
494 x264_macroblock_context_load( h, mb, &context );
497 if( h->pps->b_cabac )
499 if( h->sh.i_type != SLICE_TYPE_I && h->sh.i_type != SLICE_TYPE_SI )
503 i_ret = x264_macroblock_read_cabac( h, s, mb );
507 if( h->sh.i_type != SLICE_TYPE_I && h->sh.i_type != SLICE_TYPE_SI )
509 int i_skip = bs_read_ue( s );
513 x264_macroblock_decode_skip( h, mb );
515 /* next macroblock */
517 if( mb_xy >= h->sps->i_mb_width * h->sps->i_mb_height )
524 x264_macroblock_context_load( h, mb, &context );
528 if( mb_xy >= h->sps->i_mb_width * h->sps->i_mb_height )
533 i_ret = x264_macroblock_read_cavlc( h, s, mb );
538 fprintf( stderr, "x264_macroblock_read failed [%d,%d]\n", mb->i_mb_x, mb->i_mb_y );
542 if( x264_macroblock_decode( h, mb ) < 0 )
544 fprintf( stderr, "x264_macroblock_decode failed\n" );
545 /* try to do some error correction ;) */
555 /* expand border for frame reference TODO avoid it when using b-frame */
556 x264_frame_expand_border( h->fdec );
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 )
561 x264_frame_deblocking_filter( h, h->sh.i_type );
565 /* expand border for frame reference TODO avoid it when using b-frame */
566 x264_frame_expand_border( h->fdec );
569 h->picture->i_plane = h->fdec->i_plane;
570 for( i = 0; i < h->picture->i_plane; i++ )
572 h->picture->i_stride[i] = h->fdec->i_stride[i];
573 h->picture->plane[i] = h->fdec->plane[i];
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-- )
580 h->freference[i] = h->freference[i-1];
582 h->freference[0] = h->fdec;
588 /****************************************************************************
590 ******************************* x264 libs **********************************
592 ****************************************************************************/
594 /****************************************************************************
596 ****************************************************************************/
597 x264_t *x264_decoder_open ( x264_param_t *param )
599 x264_t *h = x264_malloc( sizeof( x264_t ) );
602 memcpy( &h->param, param, sizeof( x264_param_t ) );
606 /* no SPS and PPS active yet */
610 for( i = 0; i < 32; i++ )
612 h->sps_array[i].i_id = -1; /* invalidate it */
614 for( i = 0; i < 256; i++ )
616 h->pps_array[i].i_id = -1; /* invalidate it */
619 h->picture = x264_malloc( sizeof( x264_picture_t ) );
620 h->picture->i_width = 0;
621 h->picture->i_height= 0;
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 );
628 x264_pixel_init( h->cpu, &h->pixf );
629 x264_dct_init( h->cpu, &h->dctf );
631 x264_mc_init( h->cpu, h->mc );
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++ )
638 h->x264_coeff_token_lookup[i] = x264_vlc_table_lookup_new( x264_coeff_token[i], 17*4, 4 );
641 h->x264_level_prefix_lookup = x264_vlc_table_lookup_new( x264_level_prefix, 16, 8 );
643 for( i = 0; i < 15; i++ )
646 h->x264_total_zeros_lookup[i] = x264_vlc_table_lookup_new( x264_total_zeros[i], 16, 9 );
648 for( i = 0;i < 3; i++ )
651 h->x264_total_zeros_dc_lookup[i] = x264_vlc_table_lookup_new( x264_total_zeros_dc[i], 4, 3 );
653 for( i = 0;i < 7; i++ )
656 h->x264_run_before_lookup[i] = x264_vlc_table_lookup_new( x264_run_before[i], 15, 6 );
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 )
674 /* init bitstream reader */
675 bs_init( &bs, nal->p_payload, nal->i_payload );
677 switch( nal->i_type )
680 if( ( i_ret = x264_sps_read( &bs, h->sps_array ) ) < 0 )
682 fprintf( stderr, "x264: x264_sps_read failed\n" );
687 if( ( i_ret = x264_pps_read( &bs, h->pps_array ) ) < 0 )
689 fprintf( stderr, "x264: x264_pps_read failed\n" );
694 fprintf( stderr, "x264: NAL_SLICE_IDR\n" );
698 if( ( i_ret = x264_slice_header_decode( h, &bs, nal ) ) < 0 )
700 fprintf( stderr, "x264: x264_slice_header_decode failed\n" );
702 if( h->sh.i_redundant_pic_cnt == 0 && i_ret == 0 )
704 if( ( i_ret = x264_slice_data_decode( h, &bs ) ) < 0 )
706 fprintf( stderr, "x264: x264_slice_data_decode failed\n" );
710 *pp_pic = h->picture;
718 fprintf( stderr, "partitioned stream unsupported\n" );
727 /* restore CPU state (before using float again) */
728 x264_cpu_restore( h->cpu );
733 /****************************************************************************
734 * x264_decoder_close:
735 ****************************************************************************/
736 void x264_decoder_close ( x264_t *h )
740 if( h->picture->i_width != 0 && h->picture->i_height != 0 )
742 for( i = 0; i < h->sps->i_num_ref_frames + 1; i++ )
744 x264_frame_delete( h->freference[i]);
750 for( i = 0; i < 5; i++ )
752 x264_vlc_table_lookup_delete( h->x264_coeff_token_lookup[i] );
754 x264_vlc_table_lookup_delete( h->x264_level_prefix_lookup );
756 for( i = 0; i < 15; i++ )
758 x264_vlc_table_lookup_delete( h->x264_total_zeros_lookup[i] );
760 for( i = 0;i < 3; i++ )
762 x264_vlc_table_lookup_delete( h->x264_total_zeros_dc_lookup[i] );
764 for( i = 0;i < 7; i++ )
766 x264_vlc_table_lookup_delete( h->x264_run_before_lookup[i] );
769 x264_free( h->picture );