Libav
|
00001 /* 00002 * MPEG4 decoder / encoder common code. 00003 * Copyright (c) 2000,2001 Fabrice Bellard 00004 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at> 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "mpegvideo.h" 00024 #include "mpeg4video.h" 00025 #include "mpeg4data.h" 00026 00027 uint8_t ff_mpeg4_static_rl_table_store[3][2][2*MAX_RUN + MAX_LEVEL + 3]; 00028 00029 int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s){ 00030 switch(s->pict_type){ 00031 case FF_I_TYPE: 00032 return 16; 00033 case FF_P_TYPE: 00034 case FF_S_TYPE: 00035 return s->f_code+15; 00036 case FF_B_TYPE: 00037 return FFMAX3(s->f_code, s->b_code, 2) + 15; 00038 default: 00039 return -1; 00040 } 00041 } 00042 00043 void ff_mpeg4_clean_buffers(MpegEncContext *s) 00044 { 00045 int c_wrap, c_xy, l_wrap, l_xy; 00046 00047 l_wrap= s->b8_stride; 00048 l_xy= (2*s->mb_y-1)*l_wrap + s->mb_x*2 - 1; 00049 c_wrap= s->mb_stride; 00050 c_xy= (s->mb_y-1)*c_wrap + s->mb_x - 1; 00051 00052 #if 0 00053 /* clean DC */ 00054 memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1); 00055 memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1); 00056 memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1); 00057 #endif 00058 00059 /* clean AC */ 00060 memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(int16_t)); 00061 memset(s->ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t)); 00062 memset(s->ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t)); 00063 00064 /* clean MV */ 00065 // we can't clear the MVs as they might be needed by a b frame 00066 // memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(int16_t)); 00067 // memset(s->motion_val, 0, 2*sizeof(int16_t)*(2 + s->mb_width*2)*(2 + s->mb_height*2)); 00068 s->last_mv[0][0][0]= 00069 s->last_mv[0][0][1]= 00070 s->last_mv[1][0][0]= 00071 s->last_mv[1][0][1]= 0; 00072 } 00073 00074 #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0])) 00075 #define tab_bias (tab_size/2) 00076 00077 //used by mpeg4 and rv10 decoder 00078 void ff_mpeg4_init_direct_mv(MpegEncContext *s){ 00079 int i; 00080 for(i=0; i<tab_size; i++){ 00081 s->direct_scale_mv[0][i] = (i-tab_bias)*s->pb_time/s->pp_time; 00082 s->direct_scale_mv[1][i] = (i-tab_bias)*(s->pb_time-s->pp_time)/s->pp_time; 00083 } 00084 } 00085 00086 static inline void ff_mpeg4_set_one_direct_mv(MpegEncContext *s, int mx, int my, int i){ 00087 int xy= s->block_index[i]; 00088 uint16_t time_pp= s->pp_time; 00089 uint16_t time_pb= s->pb_time; 00090 int p_mx, p_my; 00091 00092 p_mx= s->next_picture.motion_val[0][xy][0]; 00093 if((unsigned)(p_mx + tab_bias) < tab_size){ 00094 s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias] + mx; 00095 s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx 00096 : s->direct_scale_mv[1][p_mx + tab_bias]; 00097 }else{ 00098 s->mv[0][i][0] = p_mx*time_pb/time_pp + mx; 00099 s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx 00100 : p_mx*(time_pb - time_pp)/time_pp; 00101 } 00102 p_my= s->next_picture.motion_val[0][xy][1]; 00103 if((unsigned)(p_my + tab_bias) < tab_size){ 00104 s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias] + my; 00105 s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my 00106 : s->direct_scale_mv[1][p_my + tab_bias]; 00107 }else{ 00108 s->mv[0][i][1] = p_my*time_pb/time_pp + my; 00109 s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my 00110 : p_my*(time_pb - time_pp)/time_pp; 00111 } 00112 } 00113 00114 #undef tab_size 00115 #undef tab_bias 00116 00121 int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){ 00122 const int mb_index= s->mb_x + s->mb_y*s->mb_stride; 00123 const int colocated_mb_type= s->next_picture.mb_type[mb_index]; 00124 uint16_t time_pp; 00125 uint16_t time_pb; 00126 int i; 00127 00128 //FIXME avoid divides 00129 // try special case with shifts for 1 and 3 B-frames? 00130 00131 if(IS_8X8(colocated_mb_type)){ 00132 s->mv_type = MV_TYPE_8X8; 00133 for(i=0; i<4; i++){ 00134 ff_mpeg4_set_one_direct_mv(s, mx, my, i); 00135 } 00136 return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1; 00137 } else if(IS_INTERLACED(colocated_mb_type)){ 00138 s->mv_type = MV_TYPE_FIELD; 00139 for(i=0; i<2; i++){ 00140 int field_select= s->next_picture.ref_index[0][4*mb_index + 2*i]; 00141 s->field_select[0][i]= field_select; 00142 s->field_select[1][i]= i; 00143 if(s->top_field_first){ 00144 time_pp= s->pp_field_time - field_select + i; 00145 time_pb= s->pb_field_time - field_select + i; 00146 }else{ 00147 time_pp= s->pp_field_time + field_select - i; 00148 time_pb= s->pb_field_time + field_select - i; 00149 } 00150 s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx; 00151 s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my; 00152 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0] 00153 : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp; 00154 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1] 00155 : s->p_field_mv_table[i][0][mb_index][1]*(time_pb - time_pp)/time_pp; 00156 } 00157 return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED; 00158 }else{ 00159 ff_mpeg4_set_one_direct_mv(s, mx, my, 0); 00160 s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->mv[0][0][0]; 00161 s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->mv[0][0][1]; 00162 s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = s->mv[1][0][0]; 00163 s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = s->mv[1][0][1]; 00164 if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample) 00165 s->mv_type= MV_TYPE_16X16; 00166 else 00167 s->mv_type= MV_TYPE_8X8; 00168 return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line 00169 } 00170 } 00171