00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 #include "mpegvideo_common.h"
00034 #include "mjpegenc.h"
00035 #include "msmpeg4.h"
00036 #include "faandct.h"
00037 #include <limits.h>
00038
00039
00040
00041
00042 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
00043 DCTELEM *block, int n, int qscale);
00044 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
00045 DCTELEM *block, int n, int qscale);
00046 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
00047 DCTELEM *block, int n, int qscale);
00048 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
00049 DCTELEM *block, int n, int qscale);
00050 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
00051 DCTELEM *block, int n, int qscale);
00052 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
00053 DCTELEM *block, int n, int qscale);
00054 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
00055 DCTELEM *block, int n, int qscale);
00056 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
00057
00058 #ifdef HAVE_XVMC
00059 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
00060 extern void XVMC_field_end(MpegEncContext *s);
00061 extern void XVMC_decode_mb(MpegEncContext *s);
00062 #endif
00063
00064 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
00065
00066
00067
00068
00069
00070
00071
00072
00073 static const uint8_t ff_default_chroma_qscale_table[32]={
00074
00075 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
00076 };
00077
00078 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
00079 int i;
00080 int end;
00081
00082 st->scantable= src_scantable;
00083
00084 for(i=0; i<64; i++){
00085 int j;
00086 j = src_scantable[i];
00087 st->permutated[i] = permutation[j];
00088 #ifdef ARCH_POWERPC
00089 st->inverse[j] = i;
00090 #endif
00091 }
00092
00093 end=-1;
00094 for(i=0; i<64; i++){
00095 int j;
00096 j = st->permutated[i];
00097 if(j>end) end=j;
00098 st->raster_end[i]= end;
00099 }
00100 }
00101
00102 const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
00103 int i;
00104
00105
00106 if(p>=end)
00107 return end;
00108
00109 for(i=0; i<3; i++){
00110 uint32_t tmp= *state << 8;
00111 *state= tmp + *(p++);
00112 if(tmp == 0x100 || p==end)
00113 return p;
00114 }
00115
00116 while(p<end){
00117 if (p[-1] > 1 ) p+= 3;
00118 else if(p[-2] ) p+= 2;
00119 else if(p[-3]|(p[-1]-1)) p++;
00120 else{
00121 p++;
00122 break;
00123 }
00124 }
00125
00126 p= FFMIN(p, end)-4;
00127 *state= AV_RB32(p);
00128
00129 return p+4;
00130 }
00131
00132
00133 int ff_dct_common_init(MpegEncContext *s)
00134 {
00135 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
00136 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
00137 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
00138 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
00139 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
00140 if(s->flags & CODEC_FLAG_BITEXACT)
00141 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
00142 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
00143
00144 #if defined(HAVE_MMX)
00145 MPV_common_init_mmx(s);
00146 #elif defined(ARCH_ALPHA)
00147 MPV_common_init_axp(s);
00148 #elif defined(HAVE_MLIB)
00149 MPV_common_init_mlib(s);
00150 #elif defined(HAVE_MMI)
00151 MPV_common_init_mmi(s);
00152 #elif defined(ARCH_ARMV4L)
00153 MPV_common_init_armv4l(s);
00154 #elif defined(HAVE_ALTIVEC)
00155 MPV_common_init_altivec(s);
00156 #elif defined(ARCH_BFIN)
00157 MPV_common_init_bfin(s);
00158 #endif
00159
00160
00161
00162
00163 if(s->alternate_scan){
00164 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
00165 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
00166 }else{
00167 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
00168 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
00169 }
00170 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
00171 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
00172
00173 return 0;
00174 }
00175
00176 void copy_picture(Picture *dst, Picture *src){
00177 *dst = *src;
00178 dst->type= FF_BUFFER_TYPE_COPY;
00179 }
00180
00185 int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
00186 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1;
00187 const int mb_array_size= s->mb_stride*s->mb_height;
00188 const int b8_array_size= s->b8_stride*s->mb_height*2;
00189 const int b4_array_size= s->b4_stride*s->mb_height*4;
00190 int i;
00191 int r= -1;
00192
00193 if(shared){
00194 assert(pic->data[0]);
00195 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
00196 pic->type= FF_BUFFER_TYPE_SHARED;
00197 }else{
00198 assert(!pic->data[0]);
00199
00200 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
00201
00202 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
00203 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
00204 if(r>=0)
00205 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00206 return -1;
00207 }
00208
00209 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
00210 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
00211 if(r>=0)
00212 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00213 return -1;
00214 }
00215
00216 if(pic->linesize[1] != pic->linesize[2]){
00217 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
00218 if(r>=0)
00219 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00220 return -1;
00221 }
00222
00223 s->linesize = pic->linesize[0];
00224 s->uvlinesize= pic->linesize[1];
00225 }
00226
00227 if(pic->qscale_table==NULL){
00228 if (s->encoding) {
00229 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
00230 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
00231 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
00232 }
00233
00234 CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2)
00235 CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
00236 CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(uint32_t))
00237 pic->mb_type= pic->mb_type_base + s->mb_stride+1;
00238 if(s->out_format == FMT_H264){
00239 for(i=0; i<2; i++){
00240 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t))
00241 pic->motion_val[i]= pic->motion_val_base[i]+4;
00242 CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
00243 }
00244 pic->motion_subsample_log2= 2;
00245 }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
00246 for(i=0; i<2; i++){
00247 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t))
00248 pic->motion_val[i]= pic->motion_val_base[i]+4;
00249 CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
00250 }
00251 pic->motion_subsample_log2= 3;
00252 }
00253 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
00254 CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
00255 }
00256 pic->qstride= s->mb_stride;
00257 CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
00258 }
00259
00260
00261
00262 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
00263 s->prev_pict_types[0]= s->pict_type;
00264 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
00265 pic->age= INT_MAX;
00266
00267 return 0;
00268 fail:
00269 if(r>=0)
00270 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00271 return -1;
00272 }
00273
00277 static void free_picture(MpegEncContext *s, Picture *pic){
00278 int i;
00279
00280 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
00281 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00282 }
00283
00284 av_freep(&pic->mb_var);
00285 av_freep(&pic->mc_mb_var);
00286 av_freep(&pic->mb_mean);
00287 av_freep(&pic->mbskip_table);
00288 av_freep(&pic->qscale_table);
00289 av_freep(&pic->mb_type_base);
00290 av_freep(&pic->dct_coeff);
00291 av_freep(&pic->pan_scan);
00292 pic->mb_type= NULL;
00293 for(i=0; i<2; i++){
00294 av_freep(&pic->motion_val_base[i]);
00295 av_freep(&pic->ref_index[i]);
00296 }
00297
00298 if(pic->type == FF_BUFFER_TYPE_SHARED){
00299 for(i=0; i<4; i++){
00300 pic->base[i]=
00301 pic->data[i]= NULL;
00302 }
00303 pic->type= 0;
00304 }
00305 }
00306
00307 static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
00308 int i;
00309
00310
00311 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2);
00312 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
00313
00314
00315 CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
00316 s->rd_scratchpad= s->me.scratchpad;
00317 s->b_scratchpad= s->me.scratchpad;
00318 s->obmc_scratchpad= s->me.scratchpad + 16;
00319 if (s->encoding) {
00320 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
00321 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
00322 if(s->avctx->noise_reduction){
00323 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
00324 }
00325 }
00326 CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
00327 s->block= s->blocks[0];
00328
00329 for(i=0;i<12;i++){
00330 s->pblocks[i] = (short *)(&s->block[i]);
00331 }
00332 return 0;
00333 fail:
00334 return -1;
00335 }
00336
00337 static void free_duplicate_context(MpegEncContext *s){
00338 if(s==NULL) return;
00339
00340 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
00341 av_freep(&s->me.scratchpad);
00342 s->rd_scratchpad=
00343 s->b_scratchpad=
00344 s->obmc_scratchpad= NULL;
00345
00346 av_freep(&s->dct_error_sum);
00347 av_freep(&s->me.map);
00348 av_freep(&s->me.score_map);
00349 av_freep(&s->blocks);
00350 s->block= NULL;
00351 }
00352
00353 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
00354 #define COPY(a) bak->a= src->a
00355 COPY(allocated_edge_emu_buffer);
00356 COPY(edge_emu_buffer);
00357 COPY(me.scratchpad);
00358 COPY(rd_scratchpad);
00359 COPY(b_scratchpad);
00360 COPY(obmc_scratchpad);
00361 COPY(me.map);
00362 COPY(me.score_map);
00363 COPY(blocks);
00364 COPY(block);
00365 COPY(start_mb_y);
00366 COPY(end_mb_y);
00367 COPY(me.map_generation);
00368 COPY(pb);
00369 COPY(dct_error_sum);
00370 COPY(dct_count[0]);
00371 COPY(dct_count[1]);
00372 #undef COPY
00373 }
00374
00375 void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
00376 MpegEncContext bak;
00377 int i;
00378
00379
00380 backup_duplicate_context(&bak, dst);
00381 memcpy(dst, src, sizeof(MpegEncContext));
00382 backup_duplicate_context(dst, &bak);
00383 for(i=0;i<12;i++){
00384 dst->pblocks[i] = (short *)(&dst->block[i]);
00385 }
00386
00387 }
00388
00393 void MPV_common_defaults(MpegEncContext *s){
00394 s->y_dc_scale_table=
00395 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00396 s->chroma_qscale_table= ff_default_chroma_qscale_table;
00397 s->progressive_frame= 1;
00398 s->progressive_sequence= 1;
00399 s->picture_structure= PICT_FRAME;
00400
00401 s->coded_picture_number = 0;
00402 s->picture_number = 0;
00403 s->input_picture_number = 0;
00404
00405 s->picture_in_gop_number = 0;
00406
00407 s->f_code = 1;
00408 s->b_code = 1;
00409 }
00410
00415 void MPV_decode_defaults(MpegEncContext *s){
00416 MPV_common_defaults(s);
00417 }
00418
00423 int MPV_common_init(MpegEncContext *s)
00424 {
00425 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
00426
00427 s->mb_height = (s->height + 15) / 16;
00428
00429 if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
00430 av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
00431 return -1;
00432 }
00433
00434 if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
00435 return -1;
00436
00437 dsputil_init(&s->dsp, s->avctx);
00438 ff_dct_common_init(s);
00439
00440 s->flags= s->avctx->flags;
00441 s->flags2= s->avctx->flags2;
00442
00443 s->mb_width = (s->width + 15) / 16;
00444 s->mb_stride = s->mb_width + 1;
00445 s->b8_stride = s->mb_width*2 + 1;
00446 s->b4_stride = s->mb_width*4 + 1;
00447 mb_array_size= s->mb_height * s->mb_stride;
00448 mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
00449
00450
00451 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
00452 &(s->chroma_y_shift) );
00453
00454
00455 s->h_edge_pos= s->mb_width*16;
00456 s->v_edge_pos= s->mb_height*16;
00457
00458 s->mb_num = s->mb_width * s->mb_height;
00459
00460 s->block_wrap[0]=
00461 s->block_wrap[1]=
00462 s->block_wrap[2]=
00463 s->block_wrap[3]= s->b8_stride;
00464 s->block_wrap[4]=
00465 s->block_wrap[5]= s->mb_stride;
00466
00467 y_size = s->b8_stride * (2 * s->mb_height + 1);
00468 c_size = s->mb_stride * (s->mb_height + 1);
00469 yc_size = y_size + 2 * c_size;
00470
00471
00472 s->codec_tag= toupper( s->avctx->codec_tag &0xFF)
00473 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
00474 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
00475 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
00476
00477 s->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
00478 + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
00479 + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
00480 + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
00481
00482 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
00483
00484 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int))
00485 for(y=0; y<s->mb_height; y++){
00486 for(x=0; x<s->mb_width; x++){
00487 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
00488 }
00489 }
00490 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width;
00491
00492 if (s->encoding) {
00493
00494 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00495 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00496 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00497 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00498 CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00499 CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00500 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
00501 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
00502 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
00503 s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
00504 s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
00505 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
00506
00507 if(s->msmpeg4_version){
00508 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
00509 }
00510 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
00511
00512
00513 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t))
00514
00515 CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
00516
00517 CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
00518 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
00519 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
00520 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
00521 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
00522 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
00523
00524 if(s->avctx->noise_reduction){
00525 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
00526 }
00527 }
00528 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
00529
00530 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
00531
00532 if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
00533
00534 for(i=0; i<2; i++){
00535 int j, k;
00536 for(j=0; j<2; j++){
00537 for(k=0; k<2; k++){
00538 CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
00539 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
00540 }
00541 CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
00542 CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
00543 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
00544 }
00545 CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
00546 }
00547 }
00548 if (s->out_format == FMT_H263) {
00549
00550 CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
00551 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
00552 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
00553 s->ac_val[2] = s->ac_val[1] + c_size;
00554
00555
00556 CHECKED_ALLOCZ(s->coded_block_base, y_size);
00557 s->coded_block= s->coded_block_base + s->b8_stride + 1;
00558
00559
00560 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
00561 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
00562 }
00563
00564 if (s->h263_pred || s->h263_plus || !s->encoding) {
00565
00566
00567 CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
00568 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
00569 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
00570 s->dc_val[2] = s->dc_val[1] + c_size;
00571 for(i=0;i<yc_size;i++)
00572 s->dc_val_base[i] = 1024;
00573 }
00574
00575
00576 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
00577 memset(s->mbintra_table, 1, mb_array_size);
00578
00579
00580 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
00581
00582 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
00583
00584 s->parse_context.state= -1;
00585 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
00586 s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
00587 s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
00588 s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
00589 }
00590
00591 s->context_initialized = 1;
00592
00593 s->thread_context[0]= s;
00594 threads = s->avctx->thread_count;
00595
00596 for(i=1; i<threads; i++){
00597 s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
00598 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
00599 }
00600
00601 for(i=0; i<threads; i++){
00602 if(init_duplicate_context(s->thread_context[i], s) < 0)
00603 goto fail;
00604 s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
00605 s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
00606 }
00607
00608 return 0;
00609 fail:
00610 MPV_common_end(s);
00611 return -1;
00612 }
00613
00614
00615 void MPV_common_end(MpegEncContext *s)
00616 {
00617 int i, j, k;
00618
00619 for(i=0; i<s->avctx->thread_count; i++){
00620 free_duplicate_context(s->thread_context[i]);
00621 }
00622 for(i=1; i<s->avctx->thread_count; i++){
00623 av_freep(&s->thread_context[i]);
00624 }
00625
00626 av_freep(&s->parse_context.buffer);
00627 s->parse_context.buffer_size=0;
00628
00629 av_freep(&s->mb_type);
00630 av_freep(&s->p_mv_table_base);
00631 av_freep(&s->b_forw_mv_table_base);
00632 av_freep(&s->b_back_mv_table_base);
00633 av_freep(&s->b_bidir_forw_mv_table_base);
00634 av_freep(&s->b_bidir_back_mv_table_base);
00635 av_freep(&s->b_direct_mv_table_base);
00636 s->p_mv_table= NULL;
00637 s->b_forw_mv_table= NULL;
00638 s->b_back_mv_table= NULL;
00639 s->b_bidir_forw_mv_table= NULL;
00640 s->b_bidir_back_mv_table= NULL;
00641 s->b_direct_mv_table= NULL;
00642 for(i=0; i<2; i++){
00643 for(j=0; j<2; j++){
00644 for(k=0; k<2; k++){
00645 av_freep(&s->b_field_mv_table_base[i][j][k]);
00646 s->b_field_mv_table[i][j][k]=NULL;
00647 }
00648 av_freep(&s->b_field_select_table[i][j]);
00649 av_freep(&s->p_field_mv_table_base[i][j]);
00650 s->p_field_mv_table[i][j]=NULL;
00651 }
00652 av_freep(&s->p_field_select_table[i]);
00653 }
00654
00655 av_freep(&s->dc_val_base);
00656 av_freep(&s->ac_val_base);
00657 av_freep(&s->coded_block_base);
00658 av_freep(&s->mbintra_table);
00659 av_freep(&s->cbp_table);
00660 av_freep(&s->pred_dir_table);
00661
00662 av_freep(&s->mbskip_table);
00663 av_freep(&s->prev_pict_types);
00664 av_freep(&s->bitstream_buffer);
00665 s->allocated_bitstream_buffer_size=0;
00666
00667 av_freep(&s->avctx->stats_out);
00668 av_freep(&s->ac_stats);
00669 av_freep(&s->error_status_table);
00670 av_freep(&s->mb_index2xy);
00671 av_freep(&s->lambda_table);
00672 av_freep(&s->q_intra_matrix);
00673 av_freep(&s->q_inter_matrix);
00674 av_freep(&s->q_intra_matrix16);
00675 av_freep(&s->q_inter_matrix16);
00676 av_freep(&s->input_picture);
00677 av_freep(&s->reordered_input_picture);
00678 av_freep(&s->dct_offset);
00679
00680 if(s->picture){
00681 for(i=0; i<MAX_PICTURE_COUNT; i++){
00682 free_picture(s, &s->picture[i]);
00683 }
00684 }
00685 av_freep(&s->picture);
00686 s->context_initialized = 0;
00687 s->last_picture_ptr=
00688 s->next_picture_ptr=
00689 s->current_picture_ptr= NULL;
00690 s->linesize= s->uvlinesize= 0;
00691
00692 for(i=0; i<3; i++)
00693 av_freep(&s->visualization_buffer[i]);
00694
00695 avcodec_default_free_buffers(s->avctx);
00696 }
00697
00698 void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
00699 {
00700 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
00701 uint8_t index_run[MAX_RUN+1];
00702 int last, run, level, start, end, i;
00703
00704
00705 if(static_store && rl->max_level[0])
00706 return;
00707
00708
00709 for(last=0;last<2;last++) {
00710 if (last == 0) {
00711 start = 0;
00712 end = rl->last;
00713 } else {
00714 start = rl->last;
00715 end = rl->n;
00716 }
00717
00718 memset(max_level, 0, MAX_RUN + 1);
00719 memset(max_run, 0, MAX_LEVEL + 1);
00720 memset(index_run, rl->n, MAX_RUN + 1);
00721 for(i=start;i<end;i++) {
00722 run = rl->table_run[i];
00723 level = rl->table_level[i];
00724 if (index_run[run] == rl->n)
00725 index_run[run] = i;
00726 if (level > max_level[run])
00727 max_level[run] = level;
00728 if (run > max_run[level])
00729 max_run[level] = run;
00730 }
00731 if(static_store)
00732 rl->max_level[last] = static_store[last];
00733 else
00734 rl->max_level[last] = av_malloc(MAX_RUN + 1);
00735 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
00736 if(static_store)
00737 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
00738 else
00739 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
00740 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
00741 if(static_store)
00742 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
00743 else
00744 rl->index_run[last] = av_malloc(MAX_RUN + 1);
00745 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
00746 }
00747 }
00748
00749 void init_vlc_rl(RLTable *rl, int use_static)
00750 {
00751 int i, q;
00752
00753
00754 if(use_static && rl->rl_vlc[0])
00755 return;
00756
00757 init_vlc(&rl->vlc, 9, rl->n + 1,
00758 &rl->table_vlc[0][1], 4, 2,
00759 &rl->table_vlc[0][0], 4, 2, use_static);
00760
00761
00762 for(q=0; q<32; q++){
00763 int qmul= q*2;
00764 int qadd= (q-1)|1;
00765
00766 if(q==0){
00767 qmul=1;
00768 qadd=0;
00769 }
00770 if(use_static)
00771 rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
00772 else
00773 rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
00774 for(i=0; i<rl->vlc.table_size; i++){
00775 int code= rl->vlc.table[i][0];
00776 int len = rl->vlc.table[i][1];
00777 int level, run;
00778
00779 if(len==0){
00780 run= 66;
00781 level= MAX_LEVEL;
00782 }else if(len<0){
00783 run= 0;
00784 level= code;
00785 }else{
00786 if(code==rl->n){
00787 run= 66;
00788 level= 0;
00789 }else{
00790 run= rl->table_run [code] + 1;
00791 level= rl->table_level[code] * qmul + qadd;
00792 if(code >= rl->last) run+=192;
00793 }
00794 }
00795 rl->rl_vlc[q][i].len= len;
00796 rl->rl_vlc[q][i].level= level;
00797 rl->rl_vlc[q][i].run= run;
00798 }
00799 }
00800 }
00801
00802
00803
00804 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
00805 {
00806 uint8_t *ptr, *last_line;
00807 int i;
00808
00809 last_line = buf + (height - 1) * wrap;
00810 for(i=0;i<w;i++) {
00811
00812 memcpy(buf - (i + 1) * wrap, buf, width);
00813 memcpy(last_line + (i + 1) * wrap, last_line, width);
00814 }
00815
00816 ptr = buf;
00817 for(i=0;i<height;i++) {
00818 memset(ptr - w, ptr[0], w);
00819 memset(ptr + width, ptr[width-1], w);
00820 ptr += wrap;
00821 }
00822
00823 for(i=0;i<w;i++) {
00824 memset(buf - (i + 1) * wrap - w, buf[0], w);
00825 memset(buf - (i + 1) * wrap + width, buf[width-1], w);
00826 memset(last_line + (i + 1) * wrap - w, last_line[0], w);
00827 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w);
00828 }
00829 }
00830
00831 int ff_find_unused_picture(MpegEncContext *s, int shared){
00832 int i;
00833
00834 if(shared){
00835 for(i=0; i<MAX_PICTURE_COUNT; i++){
00836 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
00837 }
00838 }else{
00839 for(i=0; i<MAX_PICTURE_COUNT; i++){
00840 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i;
00841 }
00842 for(i=0; i<MAX_PICTURE_COUNT; i++){
00843 if(s->picture[i].data[0]==NULL) return i;
00844 }
00845 }
00846
00847 av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 abort();
00860 return -1;
00861 }
00862
00863 static void update_noise_reduction(MpegEncContext *s){
00864 int intra, i;
00865
00866 for(intra=0; intra<2; intra++){
00867 if(s->dct_count[intra] > (1<<16)){
00868 for(i=0; i<64; i++){
00869 s->dct_error_sum[intra][i] >>=1;
00870 }
00871 s->dct_count[intra] >>= 1;
00872 }
00873
00874 for(i=0; i<64; i++){
00875 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
00876 }
00877 }
00878 }
00879
00883 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
00884 {
00885 int i;
00886 AVFrame *pic;
00887 s->mb_skipped = 0;
00888
00889 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
00890
00891
00892 if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
00893 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
00894 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
00895
00896
00897
00898 if(!s->encoding){
00899 for(i=0; i<MAX_PICTURE_COUNT; i++){
00900 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
00901 av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
00902 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
00903 }
00904 }
00905 }
00906 }
00907 }
00908 alloc:
00909 if(!s->encoding){
00910
00911 for(i=0; i<MAX_PICTURE_COUNT; i++){
00912 if(s->picture[i].data[0] && !s->picture[i].reference ){
00913 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
00914 }
00915 }
00916
00917 if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
00918 pic= (AVFrame*)s->current_picture_ptr;
00919 else{
00920 i= ff_find_unused_picture(s, 0);
00921 pic= (AVFrame*)&s->picture[i];
00922 }
00923
00924 pic->reference= 0;
00925 if (!s->dropable){
00926 if (s->codec_id == CODEC_ID_H264)
00927 pic->reference = s->picture_structure;
00928 else if (s->pict_type != B_TYPE)
00929 pic->reference = 3;
00930 }
00931
00932
00933 memcpy(pic->atsc_cc_buf, s->tmp_atsc_cc_buf, s->tmp_atsc_cc_len);
00934 pic->atsc_cc_len = s->tmp_atsc_cc_len;
00935 s->tmp_atsc_cc_len = 0;
00936
00937
00938 memcpy(pic->dvb_cc_buf, s->tmp_dvb_cc_buf, s->tmp_dvb_cc_len);
00939 pic->dvb_cc_len = s->tmp_dvb_cc_len;
00940 s->tmp_dvb_cc_len = 0;
00941
00942
00943 pic->coded_picture_number= s->coded_picture_number++;
00944
00945 if( alloc_picture(s, (Picture*)pic, 0) < 0)
00946 return -1;
00947
00948 s->current_picture_ptr= (Picture*)pic;
00949 s->current_picture_ptr->top_field_first= s->top_field_first;
00950 s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
00951 }
00952
00953 s->current_picture_ptr->pict_type= s->pict_type;
00954
00955
00956 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
00957
00958 copy_picture(&s->current_picture, s->current_picture_ptr);
00959
00960 if (s->pict_type != B_TYPE) {
00961 s->last_picture_ptr= s->next_picture_ptr;
00962 if(!s->dropable)
00963 s->next_picture_ptr= s->current_picture_ptr;
00964 }
00965
00966
00967
00968
00969
00970
00971 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
00972 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
00973
00974 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) && !s->dropable){
00975 av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
00976 assert(s->pict_type != B_TYPE);
00977 goto alloc;
00978 }
00979
00980 assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
00981
00982 if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
00983 int i;
00984 for(i=0; i<4; i++){
00985 if(s->picture_structure == PICT_BOTTOM_FIELD){
00986 s->current_picture.data[i] += s->current_picture.linesize[i];
00987 }
00988 s->current_picture.linesize[i] *= 2;
00989 s->last_picture.linesize[i] *=2;
00990 s->next_picture.linesize[i] *=2;
00991 }
00992 }
00993
00994 s->hurry_up= s->avctx->hurry_up;
00995 s->error_resilience= avctx->error_resilience;
00996
00997
00998
00999 if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
01000 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
01001 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
01002 }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
01003 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
01004 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
01005 }else{
01006 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
01007 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
01008 }
01009
01010 if(s->dct_error_sum){
01011 assert(s->avctx->noise_reduction && s->encoding);
01012
01013 update_noise_reduction(s);
01014 }
01015
01016 #ifdef HAVE_XVMC
01017 if(s->avctx->xvmc_acceleration)
01018 return XVMC_field_start(s, avctx);
01019 #endif
01020 return 0;
01021 }
01022
01023
01024 void MPV_frame_end(MpegEncContext *s)
01025 {
01026 int i;
01027
01028 #ifdef HAVE_XVMC
01029
01030 if(s->avctx->xvmc_acceleration){
01031 XVMC_field_end(s);
01032 }else
01033 #endif
01034 if(s->unrestricted_mv && s->current_picture.reference && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
01035 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
01036 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
01037 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
01038 }
01039 emms_c();
01040
01041 s->last_pict_type = s->pict_type;
01042 s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
01043 if(s->pict_type!=B_TYPE){
01044 s->last_non_b_pict_type= s->pict_type;
01045 }
01046 #if 0
01047
01048 for(i=0; i<MAX_PICTURE_COUNT; i++){
01049 if(s->picture[i].data[0] == s->current_picture.data[0]){
01050 s->picture[i]= s->current_picture;
01051 break;
01052 }
01053 }
01054 assert(i<MAX_PICTURE_COUNT);
01055 #endif
01056
01057 if(s->encoding){
01058
01059 for(i=0; i<MAX_PICTURE_COUNT; i++){
01060 if(s->picture[i].data[0] && !s->picture[i].reference ){
01061 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
01062 }
01063 }
01064 }
01065
01066 #if 0
01067 memset(&s->last_picture, 0, sizeof(Picture));
01068 memset(&s->next_picture, 0, sizeof(Picture));
01069 memset(&s->current_picture, 0, sizeof(Picture));
01070 #endif
01071 s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
01072 }
01073
01081 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
01082 int x, y, fr, f;
01083
01084 sx= av_clip(sx, 0, w-1);
01085 sy= av_clip(sy, 0, h-1);
01086 ex= av_clip(ex, 0, w-1);
01087 ey= av_clip(ey, 0, h-1);
01088
01089 buf[sy*stride + sx]+= color;
01090
01091 if(FFABS(ex - sx) > FFABS(ey - sy)){
01092 if(sx > ex){
01093 FFSWAP(int, sx, ex);
01094 FFSWAP(int, sy, ey);
01095 }
01096 buf+= sx + sy*stride;
01097 ex-= sx;
01098 f= ((ey-sy)<<16)/ex;
01099 for(x= 0; x <= ex; x++){
01100 y = (x*f)>>16;
01101 fr= (x*f)&0xFFFF;
01102 buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
01103 buf[(y+1)*stride + x]+= (color* fr )>>16;
01104 }
01105 }else{
01106 if(sy > ey){
01107 FFSWAP(int, sx, ex);
01108 FFSWAP(int, sy, ey);
01109 }
01110 buf+= sx + sy*stride;
01111 ey-= sy;
01112 if(ey) f= ((ex-sx)<<16)/ey;
01113 else f= 0;
01114 for(y= 0; y <= ey; y++){
01115 x = (y*f)>>16;
01116 fr= (y*f)&0xFFFF;
01117 buf[y*stride + x ]+= (color*(0x10000-fr))>>16;;
01118 buf[y*stride + x+1]+= (color* fr )>>16;;
01119 }
01120 }
01121 }
01122
01130 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
01131 int dx,dy;
01132
01133 sx= av_clip(sx, -100, w+100);
01134 sy= av_clip(sy, -100, h+100);
01135 ex= av_clip(ex, -100, w+100);
01136 ey= av_clip(ey, -100, h+100);
01137
01138 dx= ex - sx;
01139 dy= ey - sy;
01140
01141 if(dx*dx + dy*dy > 3*3){
01142 int rx= dx + dy;
01143 int ry= -dx + dy;
01144 int length= ff_sqrt((rx*rx + ry*ry)<<8);
01145
01146
01147 rx= ROUNDED_DIV(rx*3<<4, length);
01148 ry= ROUNDED_DIV(ry*3<<4, length);
01149
01150 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
01151 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
01152 }
01153 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
01154 }
01155
01159 void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
01160
01161 if(!pict || !pict->mb_type) return;
01162
01163 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
01164 int x,y;
01165
01166 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
01167 switch (pict->pict_type) {
01168 case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
01169 case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
01170 case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
01171 case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
01172 case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
01173 case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
01174 }
01175 for(y=0; y<s->mb_height; y++){
01176 for(x=0; x<s->mb_width; x++){
01177 if(s->avctx->debug&FF_DEBUG_SKIP){
01178 int count= s->mbskip_table[x + y*s->mb_stride];
01179 if(count>9) count=9;
01180 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
01181 }
01182 if(s->avctx->debug&FF_DEBUG_QP){
01183 av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
01184 }
01185 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
01186 int mb_type= pict->mb_type[x + y*s->mb_stride];
01187
01188 if(IS_PCM(mb_type))
01189 av_log(s->avctx, AV_LOG_DEBUG, "P");
01190 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
01191 av_log(s->avctx, AV_LOG_DEBUG, "A");
01192 else if(IS_INTRA4x4(mb_type))
01193 av_log(s->avctx, AV_LOG_DEBUG, "i");
01194 else if(IS_INTRA16x16(mb_type))
01195 av_log(s->avctx, AV_LOG_DEBUG, "I");
01196 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
01197 av_log(s->avctx, AV_LOG_DEBUG, "d");
01198 else if(IS_DIRECT(mb_type))
01199 av_log(s->avctx, AV_LOG_DEBUG, "D");
01200 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
01201 av_log(s->avctx, AV_LOG_DEBUG, "g");
01202 else if(IS_GMC(mb_type))
01203 av_log(s->avctx, AV_LOG_DEBUG, "G");
01204 else if(IS_SKIP(mb_type))
01205 av_log(s->avctx, AV_LOG_DEBUG, "S");
01206 else if(!USES_LIST(mb_type, 1))
01207 av_log(s->avctx, AV_LOG_DEBUG, ">");
01208 else if(!USES_LIST(mb_type, 0))
01209 av_log(s->avctx, AV_LOG_DEBUG, "<");
01210 else{
01211 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
01212 av_log(s->avctx, AV_LOG_DEBUG, "X");
01213 }
01214
01215
01216 if(IS_8X8(mb_type))
01217 av_log(s->avctx, AV_LOG_DEBUG, "+");
01218 else if(IS_16X8(mb_type))
01219 av_log(s->avctx, AV_LOG_DEBUG, "-");
01220 else if(IS_8X16(mb_type))
01221 av_log(s->avctx, AV_LOG_DEBUG, "|");
01222 else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
01223 av_log(s->avctx, AV_LOG_DEBUG, " ");
01224 else
01225 av_log(s->avctx, AV_LOG_DEBUG, "?");
01226
01227
01228 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
01229 av_log(s->avctx, AV_LOG_DEBUG, "=");
01230 else
01231 av_log(s->avctx, AV_LOG_DEBUG, " ");
01232 }
01233
01234 }
01235 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01236 }
01237 }
01238
01239 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
01240 const int shift= 1 + s->quarter_sample;
01241 int mb_y;
01242 uint8_t *ptr;
01243 int i;
01244 int h_chroma_shift, v_chroma_shift;
01245 const int width = s->avctx->width;
01246 const int height= s->avctx->height;
01247 const int mv_sample_log2= 4 - pict->motion_subsample_log2;
01248 const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
01249 s->low_delay=0;
01250
01251 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
01252 for(i=0; i<3; i++){
01253 memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
01254 pict->data[i]= s->visualization_buffer[i];
01255 }
01256 pict->type= FF_BUFFER_TYPE_COPY;
01257 ptr= pict->data[0];
01258
01259 for(mb_y=0; mb_y<s->mb_height; mb_y++){
01260 int mb_x;
01261 for(mb_x=0; mb_x<s->mb_width; mb_x++){
01262 const int mb_index= mb_x + mb_y*s->mb_stride;
01263 if((s->avctx->debug_mv) && pict->motion_val){
01264 int type;
01265 for(type=0; type<3; type++){
01266 int direction = 0;
01267 switch (type) {
01268 case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
01269 continue;
01270 direction = 0;
01271 break;
01272 case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
01273 continue;
01274 direction = 0;
01275 break;
01276 case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
01277 continue;
01278 direction = 1;
01279 break;
01280 }
01281 if(!USES_LIST(pict->mb_type[mb_index], direction))
01282 continue;
01283
01284 if(IS_8X8(pict->mb_type[mb_index])){
01285 int i;
01286 for(i=0; i<4; i++){
01287 int sx= mb_x*16 + 4 + 8*(i&1);
01288 int sy= mb_y*16 + 4 + 8*(i>>1);
01289 int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
01290 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
01291 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
01292 draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
01293 }
01294 }else if(IS_16X8(pict->mb_type[mb_index])){
01295 int i;
01296 for(i=0; i<2; i++){
01297 int sx=mb_x*16 + 8;
01298 int sy=mb_y*16 + 4 + 8*i;
01299 int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
01300 int mx=(pict->motion_val[direction][xy][0]>>shift);
01301 int my=(pict->motion_val[direction][xy][1]>>shift);
01302
01303 if(IS_INTERLACED(pict->mb_type[mb_index]))
01304 my*=2;
01305
01306 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
01307 }
01308 }else if(IS_8X16(pict->mb_type[mb_index])){
01309 int i;
01310 for(i=0; i<2; i++){
01311 int sx=mb_x*16 + 4 + 8*i;
01312 int sy=mb_y*16 + 8;
01313 int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
01314 int mx=(pict->motion_val[direction][xy][0]>>shift);
01315 int my=(pict->motion_val[direction][xy][1]>>shift);
01316
01317 if(IS_INTERLACED(pict->mb_type[mb_index]))
01318 my*=2;
01319
01320 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
01321 }
01322 }else{
01323 int sx= mb_x*16 + 8;
01324 int sy= mb_y*16 + 8;
01325 int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
01326 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
01327 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
01328 draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
01329 }
01330 }
01331 }
01332 if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
01333 uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
01334 int y;
01335 for(y=0; y<8; y++){
01336 *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
01337 *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
01338 }
01339 }
01340 if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
01341 int mb_type= pict->mb_type[mb_index];
01342 uint64_t u,v;
01343 int y;
01344 #define COLOR(theta, r)\
01345 u= (int)(128 + r*cos(theta*3.141592/180));\
01346 v= (int)(128 + r*sin(theta*3.141592/180));
01347
01348
01349 u=v=128;
01350 if(IS_PCM(mb_type)){
01351 COLOR(120,48)
01352 }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
01353 COLOR(30,48)
01354 }else if(IS_INTRA4x4(mb_type)){
01355 COLOR(90,48)
01356 }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
01357
01358 }else if(IS_DIRECT(mb_type)){
01359 COLOR(150,48)
01360 }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
01361 COLOR(170,48)
01362 }else if(IS_GMC(mb_type)){
01363 COLOR(190,48)
01364 }else if(IS_SKIP(mb_type)){
01365
01366 }else if(!USES_LIST(mb_type, 1)){
01367 COLOR(240,48)
01368 }else if(!USES_LIST(mb_type, 0)){
01369 COLOR(0,48)
01370 }else{
01371 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
01372 COLOR(300,48)
01373 }
01374
01375 u*= 0x0101010101010101ULL;
01376 v*= 0x0101010101010101ULL;
01377 for(y=0; y<8; y++){
01378 *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
01379 *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
01380 }
01381
01382
01383 if(IS_8X8(mb_type) || IS_16X8(mb_type)){
01384 *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
01385 *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
01386 }
01387 if(IS_8X8(mb_type) || IS_8X16(mb_type)){
01388 for(y=0; y<16; y++)
01389 pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
01390 }
01391 if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
01392 int dm= 1 << (mv_sample_log2-2);
01393 for(i=0; i<4; i++){
01394 int sx= mb_x*16 + 8*(i&1);
01395 int sy= mb_y*16 + 8*(i>>1);
01396 int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
01397
01398 int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
01399 if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
01400 for(y=0; y<8; y++)
01401 pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
01402 if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
01403 *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
01404 }
01405 }
01406
01407 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
01408
01409 }
01410 }
01411 s->mbskip_table[mb_index]=0;
01412 }
01413 }
01414 }
01415 }
01416
01429 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
01430 int src_x, int src_y, int w, int h){
01431 int x, y;
01432 int start_y, start_x, end_y, end_x;
01433
01434 if(src_y>= h){
01435 src+= (h-1-src_y)*linesize;
01436 src_y=h-1;
01437 }else if(src_y<=-block_h){
01438 src+= (1-block_h-src_y)*linesize;
01439 src_y=1-block_h;
01440 }
01441 if(src_x>= w){
01442 src+= (w-1-src_x);
01443 src_x=w-1;
01444 }else if(src_x<=-block_w){
01445 src+= (1-block_w-src_x);
01446 src_x=1-block_w;
01447 }
01448
01449 start_y= FFMAX(0, -src_y);
01450 start_x= FFMAX(0, -src_x);
01451 end_y= FFMIN(block_h, h-src_y);
01452 end_x= FFMIN(block_w, w-src_x);
01453
01454
01455 for(y=start_y; y<end_y; y++){
01456 for(x=start_x; x<end_x; x++){
01457 buf[x + y*linesize]= src[x + y*linesize];
01458 }
01459 }
01460
01461
01462 for(y=0; y<start_y; y++){
01463 for(x=start_x; x<end_x; x++){
01464 buf[x + y*linesize]= buf[x + start_y*linesize];
01465 }
01466 }
01467
01468
01469 for(y=end_y; y<block_h; y++){
01470 for(x=start_x; x<end_x; x++){
01471 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
01472 }
01473 }
01474
01475 for(y=0; y<block_h; y++){
01476
01477 for(x=0; x<start_x; x++){
01478 buf[x + y*linesize]= buf[start_x + y*linesize];
01479 }
01480
01481
01482 for(x=end_x; x<block_w; x++){
01483 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
01484 }
01485 }
01486 }
01487
01488 static inline int hpel_motion_lowres(MpegEncContext *s,
01489 uint8_t *dest, uint8_t *src,
01490 int field_based, int field_select,
01491 int src_x, int src_y,
01492 int width, int height, int stride,
01493 int h_edge_pos, int v_edge_pos,
01494 int w, int h, h264_chroma_mc_func *pix_op,
01495 int motion_x, int motion_y)
01496 {
01497 const int lowres= s->avctx->lowres;
01498 const int s_mask= (2<<lowres)-1;
01499 int emu=0;
01500 int sx, sy;
01501
01502 if(s->quarter_sample){
01503 motion_x/=2;
01504 motion_y/=2;
01505 }
01506
01507 sx= motion_x & s_mask;
01508 sy= motion_y & s_mask;
01509 src_x += motion_x >> (lowres+1);
01510 src_y += motion_y >> (lowres+1);
01511
01512 src += src_y * stride + src_x;
01513
01514 if( (unsigned)src_x > h_edge_pos - (!!sx) - w
01515 || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
01516 ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
01517 src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
01518 src= s->edge_emu_buffer;
01519 emu=1;
01520 }
01521
01522 sx <<= 2 - lowres;
01523 sy <<= 2 - lowres;
01524 if(field_select)
01525 src += s->linesize;
01526 pix_op[lowres](dest, src, stride, h, sx, sy);
01527 return emu;
01528 }
01529
01530
01531 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
01532 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
01533 int field_based, int bottom_field, int field_select,
01534 uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
01535 int motion_x, int motion_y, int h)
01536 {
01537 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
01538 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
01539 const int lowres= s->avctx->lowres;
01540 const int block_s= 8>>lowres;
01541 const int s_mask= (2<<lowres)-1;
01542 const int h_edge_pos = s->h_edge_pos >> lowres;
01543 const int v_edge_pos = s->v_edge_pos >> lowres;
01544 linesize = s->current_picture.linesize[0] << field_based;
01545 uvlinesize = s->current_picture.linesize[1] << field_based;
01546
01547 if(s->quarter_sample){
01548 motion_x/=2;
01549 motion_y/=2;
01550 }
01551
01552 if(field_based){
01553 motion_y += (bottom_field - field_select)*((1<<lowres)-1);
01554 }
01555
01556 sx= motion_x & s_mask;
01557 sy= motion_y & s_mask;
01558 src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
01559 src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
01560
01561 if (s->out_format == FMT_H263) {
01562 uvsx = ((motion_x>>1) & s_mask) | (sx&1);
01563 uvsy = ((motion_y>>1) & s_mask) | (sy&1);
01564 uvsrc_x = src_x>>1;
01565 uvsrc_y = src_y>>1;
01566 }else if(s->out_format == FMT_H261){
01567 mx = motion_x / 4;
01568 my = motion_y / 4;
01569 uvsx = (2*mx) & s_mask;
01570 uvsy = (2*my) & s_mask;
01571 uvsrc_x = s->mb_x*block_s + (mx >> lowres);
01572 uvsrc_y = s->mb_y*block_s + (my >> lowres);
01573 } else {
01574 mx = motion_x / 2;
01575 my = motion_y / 2;
01576 uvsx = mx & s_mask;
01577 uvsy = my & s_mask;
01578 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
01579 uvsrc_y =(s->mb_y*block_s>>field_based) + (my >> (lowres+1));
01580 }
01581
01582 ptr_y = ref_picture[0] + src_y * linesize + src_x;
01583 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
01584 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
01585
01586 if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
01587 || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
01588 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
01589 src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
01590 ptr_y = s->edge_emu_buffer;
01591 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01592 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
01593 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
01594 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
01595 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
01596 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
01597 ptr_cb= uvbuf;
01598 ptr_cr= uvbuf+16;
01599 }
01600 }
01601
01602 if(bottom_field){
01603 dest_y += s->linesize;
01604 dest_cb+= s->uvlinesize;
01605 dest_cr+= s->uvlinesize;
01606 }
01607
01608 if(field_select){
01609 ptr_y += s->linesize;
01610 ptr_cb+= s->uvlinesize;
01611 ptr_cr+= s->uvlinesize;
01612 }
01613
01614 sx <<= 2 - lowres;
01615 sy <<= 2 - lowres;
01616 pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
01617
01618 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01619 uvsx <<= 2 - lowres;
01620 uvsy <<= 2 - lowres;
01621 pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
01622 pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
01623 }
01624
01625 }
01626
01627 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
01628 uint8_t *dest_cb, uint8_t *dest_cr,
01629 uint8_t **ref_picture,
01630 h264_chroma_mc_func *pix_op,
01631 int mx, int my){
01632 const int lowres= s->avctx->lowres;
01633 const int block_s= 8>>lowres;
01634 const int s_mask= (2<<lowres)-1;
01635 const int h_edge_pos = s->h_edge_pos >> (lowres+1);
01636 const int v_edge_pos = s->v_edge_pos >> (lowres+1);
01637 int emu=0, src_x, src_y, offset, sx, sy;
01638 uint8_t *ptr;
01639
01640 if(s->quarter_sample){
01641 mx/=2;
01642 my/=2;
01643 }
01644
01645
01646
01647 mx= ff_h263_round_chroma(mx);
01648 my= ff_h263_round_chroma(my);
01649
01650 sx= mx & s_mask;
01651 sy= my & s_mask;
01652 src_x = s->mb_x*block_s + (mx >> (lowres+1));
01653 src_y = s->mb_y*block_s + (my >> (lowres+1));
01654
01655 offset = src_y * s->uvlinesize + src_x;
01656 ptr = ref_picture[1] + offset;
01657 if(s->flags&CODEC_FLAG_EMU_EDGE){
01658 if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
01659 || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
01660 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
01661 ptr= s->edge_emu_buffer;
01662 emu=1;
01663 }
01664 }
01665 sx <<= 2 - lowres;
01666 sy <<= 2 - lowres;
01667 pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
01668
01669 ptr = ref_picture[2] + offset;
01670 if(emu){
01671 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
01672 ptr= s->edge_emu_buffer;
01673 }
01674 pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
01675 }
01676
01688 static inline void MPV_motion_lowres(MpegEncContext *s,
01689 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
01690 int dir, uint8_t **ref_picture,
01691 h264_chroma_mc_func *pix_op)
01692 {
01693 int mx, my;
01694 int mb_x, mb_y, i;
01695 const int lowres= s->avctx->lowres;
01696 const int block_s= 8>>lowres;
01697
01698 mb_x = s->mb_x;
01699 mb_y = s->mb_y;
01700
01701 switch(s->mv_type) {
01702 case MV_TYPE_16X16:
01703 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
01704 0, 0, 0,
01705 ref_picture, pix_op,
01706 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
01707 break;
01708 case MV_TYPE_8X8:
01709 mx = 0;
01710 my = 0;
01711 for(i=0;i<4;i++) {
01712 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
01713 ref_picture[0], 0, 0,
01714 (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
01715 s->width, s->height, s->linesize,
01716 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
01717 block_s, block_s, pix_op,
01718 s->mv[dir][i][0], s->mv[dir][i][1]);
01719
01720 mx += s->mv[dir][i][0];
01721 my += s->mv[dir][i][1];
01722 }
01723
01724 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY))
01725 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
01726 break;
01727 case MV_TYPE_FIELD:
01728 if (s->picture_structure == PICT_FRAME) {
01729
01730 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
01731 1, 0, s->field_select[dir][0],
01732 ref_picture, pix_op,
01733 s->mv[dir][0][0], s->mv[dir][0][1], block_s);
01734
01735 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
01736 1, 1, s->field_select[dir][1],
01737 ref_picture, pix_op,
01738 s->mv[dir][1][0], s->mv[dir][1][1], block_s);
01739 } else {
01740 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
01741 ref_picture= s->current_picture_ptr->data;
01742 }
01743
01744 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
01745 0, 0, s->field_select[dir][0],
01746 ref_picture, pix_op,
01747 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
01748 }
01749 break;
01750 case MV_TYPE_16X8:
01751 for(i=0; i<2; i++){
01752 uint8_t ** ref2picture;
01753
01754 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
01755 ref2picture= ref_picture;
01756 }else{
01757 ref2picture= s->current_picture_ptr->data;
01758 }
01759
01760 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
01761 0, 0, s->field_select[dir][i],
01762 ref2picture, pix_op,
01763 s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
01764
01765 dest_y += 2*block_s*s->linesize;
01766 dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
01767 dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
01768 }
01769 break;
01770 case MV_TYPE_DMV:
01771 if(s->picture_structure == PICT_FRAME){
01772 for(i=0; i<2; i++){
01773 int j;
01774 for(j=0; j<2; j++){
01775 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
01776 1, j, j^i,
01777 ref_picture, pix_op,
01778 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s);
01779 }
01780 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
01781 }
01782 }else{
01783 for(i=0; i<2; i++){
01784 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
01785 0, 0, s->picture_structure != i+1,
01786 ref_picture, pix_op,
01787 s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
01788
01789
01790 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
01791
01792
01793 if(!s->first_field){
01794 ref_picture = s->current_picture_ptr->data;
01795 }
01796 }
01797 }
01798 break;
01799 default: assert(0);
01800 }
01801 }
01802
01803
01804 static inline void put_dct(MpegEncContext *s,
01805 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
01806 {
01807 s->dct_unquantize_intra(s, block, i, qscale);
01808 s->dsp.idct_put (dest, line_size, block);
01809 }
01810
01811
01812 static inline void add_dct(MpegEncContext *s,
01813 DCTELEM *block, int i, uint8_t *dest, int line_size)
01814 {
01815 if (s->block_last_index[i] >= 0) {
01816 s->dsp.idct_add (dest, line_size, block);
01817 }
01818 }
01819
01820 static inline void add_dequant_dct(MpegEncContext *s,
01821 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
01822 {
01823 if (s->block_last_index[i] >= 0) {
01824 s->dct_unquantize_inter(s, block, i, qscale);
01825
01826 s->dsp.idct_add (dest, line_size, block);
01827 }
01828 }
01829
01833 void ff_clean_intra_table_entries(MpegEncContext *s)
01834 {
01835 int wrap = s->b8_stride;
01836 int xy = s->block_index[0];
01837
01838 s->dc_val[0][xy ] =
01839 s->dc_val[0][xy + 1 ] =
01840 s->dc_val[0][xy + wrap] =
01841 s->dc_val[0][xy + 1 + wrap] = 1024;
01842
01843 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
01844 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
01845 if (s->msmpeg4_version>=3) {
01846 s->coded_block[xy ] =
01847 s->coded_block[xy + 1 ] =
01848 s->coded_block[xy + wrap] =
01849 s->coded_block[xy + 1 + wrap] = 0;
01850 }
01851
01852 wrap = s->mb_stride;
01853 xy = s->mb_x + s->mb_y * wrap;
01854 s->dc_val[1][xy] =
01855 s->dc_val[2][xy] = 1024;
01856
01857 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
01858 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
01859
01860 s->mbintra_table[xy]= 0;
01861 }
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873 static av_always_inline void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64], int lowres_flag)
01874 {
01875 int mb_x, mb_y;
01876 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
01877 #ifdef HAVE_XVMC
01878 if(s->avctx->xvmc_acceleration){
01879 XVMC_decode_mb(s);
01880 return;
01881 }
01882 #endif
01883
01884 mb_x = s->mb_x;
01885 mb_y = s->mb_y;
01886
01887 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
01888
01889 int i,j;
01890 DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
01891 for(i=0; i<6; i++)
01892 for(j=0; j<64; j++)
01893 *dct++ = block[i][s->dsp.idct_permutation[j]];
01894 }
01895
01896 s->current_picture.qscale_table[mb_xy]= s->qscale;
01897
01898
01899 if (!s->mb_intra) {
01900 if (s->h263_pred || s->h263_aic) {
01901 if(s->mbintra_table[mb_xy])
01902 ff_clean_intra_table_entries(s);
01903 } else {
01904 s->last_dc[0] =
01905 s->last_dc[1] =
01906 s->last_dc[2] = 128 << s->intra_dc_precision;
01907 }
01908 }
01909 else if (s->h263_pred || s->h263_aic)
01910 s->mbintra_table[mb_xy]=1;
01911
01912 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE) && s->avctx->mb_decision != FF_MB_DECISION_RD)) {
01913 uint8_t *dest_y, *dest_cb, *dest_cr;
01914 int dct_linesize, dct_offset;
01915 op_pixels_func (*op_pix)[4];
01916 qpel_mc_func (*op_qpix)[16];
01917 const int linesize= s->current_picture.linesize[0];
01918 const int uvlinesize= s->current_picture.linesize[1];
01919 const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
01920 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
01921
01922
01923
01924 if(!s->encoding){
01925 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
01926 const int age= s->current_picture.age;
01927
01928 assert(age);
01929
01930 if (s->mb_skipped) {
01931 s->mb_skipped= 0;
01932 assert(s->pict_type!=I_TYPE);
01933
01934 (*mbskip_ptr) ++;
01935 if(*mbskip_ptr >99) *mbskip_ptr= 99;
01936
01937
01938 if (*mbskip_ptr >= age && s->current_picture.reference){
01939 return;
01940 }
01941 } else if(!s->current_picture.reference){
01942 (*mbskip_ptr) ++;
01943 if(*mbskip_ptr >99) *mbskip_ptr= 99;
01944 } else{
01945 *mbskip_ptr = 0;
01946 }
01947 }
01948
01949 dct_linesize = linesize << s->interlaced_dct;
01950 dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
01951
01952 if(readable){
01953 dest_y= s->dest[0];
01954 dest_cb= s->dest[1];
01955 dest_cr= s->dest[2];
01956 }else{
01957 dest_y = s->b_scratchpad;
01958 dest_cb= s->b_scratchpad+16*linesize;
01959 dest_cr= s->b_scratchpad+32*linesize;
01960 }
01961
01962 if (!s->mb_intra) {
01963
01964
01965 if(!s->encoding){
01966 if(lowres_flag){
01967 h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
01968
01969 if (s->mv_dir & MV_DIR_FORWARD) {
01970 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
01971 op_pix = s->dsp.avg_h264_chroma_pixels_tab;
01972 }
01973 if (s->mv_dir & MV_DIR_BACKWARD) {
01974 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
01975 }
01976 }else{
01977 op_qpix= s->me.qpel_put;
01978 if ((!s->no_rounding) || s->pict_type==B_TYPE){
01979 op_pix = s->dsp.put_pixels_tab;
01980 }else{
01981 op_pix = s->dsp.put_no_rnd_pixels_tab;
01982 }
01983 if (s->mv_dir & MV_DIR_FORWARD) {
01984 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
01985 op_pix = s->dsp.avg_pixels_tab;
01986 op_qpix= s->me.qpel_avg;
01987 }
01988 if (s->mv_dir & MV_DIR_BACKWARD) {
01989 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
01990 }
01991 }
01992 }
01993
01994
01995 if(s->hurry_up>1) goto skip_idct;
01996 if(s->avctx->skip_idct){
01997 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == B_TYPE)
01998 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != I_TYPE)
01999 || s->avctx->skip_idct >= AVDISCARD_ALL)
02000 goto skip_idct;
02001 }
02002
02003
02004 if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
02005 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
02006 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
02007 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
02008 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
02009 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
02010
02011 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02012 if (s->chroma_y_shift){
02013 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
02014 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
02015 }else{
02016 dct_linesize >>= 1;
02017 dct_offset >>=1;
02018 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
02019 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
02020 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
02021 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
02022 }
02023 }
02024 } else if(s->codec_id != CODEC_ID_WMV2){
02025 add_dct(s, block[0], 0, dest_y , dct_linesize);
02026 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
02027 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
02028 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
02029
02030 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02031 if(s->chroma_y_shift){
02032 add_dct(s, block[4], 4, dest_cb, uvlinesize);
02033 add_dct(s, block[5], 5, dest_cr, uvlinesize);
02034 }else{
02035
02036 dct_linesize = uvlinesize << s->interlaced_dct;
02037 dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
02038
02039 add_dct(s, block[4], 4, dest_cb, dct_linesize);
02040 add_dct(s, block[5], 5, dest_cr, dct_linesize);
02041 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
02042 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
02043 if(!s->chroma_x_shift){
02044 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
02045 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
02046 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
02047 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
02048 }
02049 }
02050 }
02051 }
02052 else if (ENABLE_WMV2) {
02053 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
02054 }
02055 } else {
02056
02057 if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
02058 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
02059 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
02060 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
02061 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
02062
02063 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02064 if(s->chroma_y_shift){
02065 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
02066 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
02067 }else{
02068 dct_offset >>=1;
02069 dct_linesize >>=1;
02070 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
02071 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
02072 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
02073 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
02074 }
02075 }
02076 }else{
02077 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
02078 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
02079 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
02080 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
02081
02082 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
02083 if(s->chroma_y_shift){
02084 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
02085 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
02086 }else{
02087
02088 dct_linesize = uvlinesize << s->interlaced_dct;
02089 dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
02090
02091 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
02092 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
02093 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
02094 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
02095 if(!s->chroma_x_shift){
02096 s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
02097 s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
02098 s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
02099 s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
02100 }
02101 }
02102 }
02103 }
02104 }
02105 skip_idct:
02106 if(!readable){
02107 s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
02108 s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
02109 s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
02110 }
02111 }
02112 }
02113
02114 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
02115 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1);
02116 else MPV_decode_mb_internal(s, block, 0);
02117 }
02118
02123 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
02124 if (s->avctx->draw_horiz_band) {
02125 AVFrame *src;
02126 int offset[4];
02127
02128 if(s->picture_structure != PICT_FRAME){
02129 h <<= 1;
02130 y <<= 1;
02131 if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
02132 }
02133
02134 h= FFMIN(h, s->avctx->height - y);
02135
02136 if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
02137 src= (AVFrame*)s->current_picture_ptr;
02138 else if(s->last_picture_ptr)
02139 src= (AVFrame*)s->last_picture_ptr;
02140 else
02141 return;
02142
02143 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
02144 offset[0]=
02145 offset[1]=
02146 offset[2]=
02147 offset[3]= 0;
02148 }else{
02149 offset[0]= y * s->linesize;;
02150 offset[1]=
02151 offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
02152 offset[3]= 0;
02153 }
02154
02155 emms_c();
02156
02157 s->avctx->draw_horiz_band(s->avctx, src, offset,
02158 y, s->picture_structure, h);
02159 }
02160 }
02161
02162 void ff_init_block_index(MpegEncContext *s){
02163 const int linesize= s->current_picture.linesize[0];
02164 const int uvlinesize= s->current_picture.linesize[1];
02165 const int mb_size= 4 - s->avctx->lowres;
02166
02167 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
02168 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
02169 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
02170 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
02171 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
02172 s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
02173
02174
02175 s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
02176 s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
02177 s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
02178
02179 if(!(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
02180 {
02181 s->dest[0] += s->mb_y * linesize << mb_size;
02182 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
02183 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
02184 }
02185 }
02186
02187 void ff_mpeg_flush(AVCodecContext *avctx){
02188 int i;
02189 MpegEncContext *s = avctx->priv_data;
02190
02191 if(s==NULL || s->picture==NULL)
02192 return;
02193
02194 for(i=0; i<MAX_PICTURE_COUNT; i++){
02195 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
02196 || s->picture[i].type == FF_BUFFER_TYPE_USER))
02197 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
02198 }
02199 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
02200
02201 s->mb_x= s->mb_y= 0;
02202
02203 s->parse_context.state= -1;
02204 s->parse_context.frame_start_found= 0;
02205 s->parse_context.overread= 0;
02206 s->parse_context.overread_index= 0;
02207 s->parse_context.index= 0;
02208 s->parse_context.last_index= 0;
02209 s->bitstream_buffer_size=0;
02210 s->pp_time=0;
02211 }
02212
02213 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
02214 DCTELEM *block, int n, int qscale)
02215 {
02216 int i, level, nCoeffs;
02217 const uint16_t *quant_matrix;
02218
02219 nCoeffs= s->block_last_index[n];
02220
02221 if (n < 4)
02222 block[0] = block[0] * s->y_dc_scale;
02223 else
02224 block[0] = block[0] * s->c_dc_scale;
02225
02226 quant_matrix = s->intra_matrix;
02227 for(i=1;i<=nCoeffs;i++) {
02228 int j= s->intra_scantable.permutated[i];
02229 level = block[j];
02230 if (level) {
02231 if (level < 0) {
02232 level = -level;
02233 level = (int)(level * qscale * quant_matrix[j]) >> 3;
02234 level = (level - 1) | 1;
02235 level = -level;
02236 } else {
02237 level = (int)(level * qscale * quant_matrix[j]) >> 3;
02238 level = (level - 1) | 1;
02239 }
02240 block[j] = level;
02241 }
02242 }
02243 }
02244
02245 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
02246 DCTELEM *block, int n, int qscale)
02247 {
02248 int i, level, nCoeffs;
02249 const uint16_t *quant_matrix;
02250
02251 nCoeffs= s->block_last_index[n];
02252
02253 quant_matrix = s->inter_matrix;
02254 for(i=0; i<=nCoeffs; i++) {
02255 int j= s->intra_scantable.permutated[i];
02256 level = block[j];
02257 if (level) {
02258 if (level < 0) {
02259 level = -level;
02260 level = (((level << 1) + 1) * qscale *
02261 ((int) (quant_matrix[j]))) >> 4;
02262 level = (level - 1) | 1;
02263 level = -level;
02264 } else {
02265 level = (((level << 1) + 1) * qscale *
02266 ((int) (quant_matrix[j]))) >> 4;
02267 level = (level - 1) | 1;
02268 }
02269 block[j] = level;
02270 }
02271 }
02272 }
02273
02274 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
02275 DCTELEM *block, int n, int qscale)
02276 {
02277 int i, level, nCoeffs;
02278 const uint16_t *quant_matrix;
02279
02280 if(s->alternate_scan) nCoeffs= 63;
02281 else nCoeffs= s->block_last_index[n];
02282
02283 if (n < 4)
02284 block[0] = block[0] * s->y_dc_scale;
02285 else
02286 block[0] = block[0] * s->c_dc_scale;
02287 quant_matrix = s->intra_matrix;
02288 for(i=1;i<=nCoeffs;i++) {
02289 int j= s->intra_scantable.permutated[i];
02290 level = block[j];
02291 if (level) {
02292 if (level < 0) {
02293 level = -level;
02294 level = (int)(level * qscale * quant_matrix[j]) >> 3;
02295 level = -level;
02296 } else {
02297 level = (int)(level * qscale * quant_matrix[j]) >> 3;
02298 }
02299 block[j] = level;
02300 }
02301 }
02302 }
02303
02304 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
02305 DCTELEM *block, int n, int qscale)
02306 {
02307 int i, level, nCoeffs;
02308 const uint16_t *quant_matrix;
02309 int sum=-1;
02310
02311 if(s->alternate_scan) nCoeffs= 63;
02312 else nCoeffs= s->block_last_index[n];
02313
02314 if (n < 4)
02315 block[0] = block[0] * s->y_dc_scale;
02316 else
02317 block[0] = block[0] * s->c_dc_scale;
02318 quant_matrix = s->intra_matrix;
02319 for(i=1;i<=nCoeffs;i++) {
02320 int j= s->intra_scantable.permutated[i];
02321 level = block[j];
02322 if (level) {
02323 if (level < 0) {
02324 level = -level;
02325 level = (int)(level * qscale * quant_matrix[j]) >> 3;
02326 level = -level;
02327 } else {
02328 level = (int)(level * qscale * quant_matrix[j]) >> 3;
02329 }
02330 block[j] = level;
02331 sum+=level;
02332 }
02333 }
02334 block[63]^=sum&1;
02335 }
02336
02337 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
02338 DCTELEM *block, int n, int qscale)
02339 {
02340 int i, level, nCoeffs;
02341 const uint16_t *quant_matrix;
02342 int sum=-1;
02343
02344 if(s->alternate_scan) nCoeffs= 63;
02345 else nCoeffs= s->block_last_index[n];
02346
02347 quant_matrix = s->inter_matrix;
02348 for(i=0; i<=nCoeffs; i++) {
02349 int j= s->intra_scantable.permutated[i];
02350 level = block[j];
02351 if (level) {
02352 if (level < 0) {
02353 level = -level;
02354 level = (((level << 1) + 1) * qscale *
02355 ((int) (quant_matrix[j]))) >> 4;
02356 level = -level;
02357 } else {
02358 level = (((level << 1) + 1) * qscale *
02359 ((int) (quant_matrix[j]))) >> 4;
02360 }
02361 block[j] = level;
02362 sum+=level;
02363 }
02364 }
02365 block[63]^=sum&1;
02366 }
02367
02368 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
02369 DCTELEM *block, int n, int qscale)
02370 {
02371 int i, level, qmul, qadd;
02372 int nCoeffs;
02373
02374 assert(s->block_last_index[n]>=0);
02375
02376 qmul = qscale << 1;
02377
02378 if (!s->h263_aic) {
02379 if (n < 4)
02380 block[0] = block[0] * s->y_dc_scale;
02381 else
02382 block[0] = block[0] * s->c_dc_scale;
02383 qadd = (qscale - 1) | 1;
02384 }else{
02385 qadd = 0;
02386 }
02387 if(s->ac_pred)
02388 nCoeffs=63;
02389 else
02390 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
02391
02392 for(i=1; i<=nCoeffs; i++) {
02393 level = block[i];
02394 if (level) {
02395 if (level < 0) {
02396 level = level * qmul - qadd;
02397 } else {
02398 level = level * qmul + qadd;
02399 }
02400 block[i] = level;
02401 }
02402 }
02403 }
02404
02405 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
02406 DCTELEM *block, int n, int qscale)
02407 {
02408 int i, level, qmul, qadd;
02409 int nCoeffs;
02410
02411 assert(s->block_last_index[n]>=0);
02412
02413 qadd = (qscale - 1) | 1;
02414 qmul = qscale << 1;
02415
02416 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
02417
02418 for(i=0; i<=nCoeffs; i++) {
02419 level = block[i];
02420 if (level) {
02421 if (level < 0) {
02422 level = level * qmul - qadd;
02423 } else {
02424 level = level * qmul + qadd;
02425 }
02426 block[i] = level;
02427 }
02428 }
02429 }
02430
02434 void ff_set_qscale(MpegEncContext * s, int qscale)
02435 {
02436 if (qscale < 1)
02437 qscale = 1;
02438 else if (qscale > 31)
02439 qscale = 31;
02440
02441 s->qscale = qscale;
02442 s->chroma_qscale= s->chroma_qscale_table[qscale];
02443
02444 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
02445 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
02446 }