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