Mercurial > libavcodec.hg
annotate wmv2.c @ 1064:b32afefe7d33 libavcodec
* UINTX -> uintx_t INTX -> intx_t
| author | kabi |
|---|---|
| date | Tue, 11 Feb 2003 16:35:48 +0000 |
| parents | bb5de8a59da8 |
| children | 6da5ae9ee199 |
| rev | line source |
|---|---|
| 936 | 1 /* |
| 2 * Copyright (c) 2002 The FFmpeg Project. | |
| 3 * | |
| 4 * This library is free software; you can redistribute it and/or | |
| 5 * modify it under the terms of the GNU Lesser General Public | |
| 6 * License as published by the Free Software Foundation; either | |
| 7 * version 2 of the License, or (at your option) any later version. | |
| 8 * | |
| 9 * This library is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 12 * Lesser General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU Lesser General Public | |
| 15 * License along with this library; if not, write to the Free Software | |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17 * | |
| 18 */ | |
| 19 | |
| 20 #include "simple_idct.h" | |
| 21 | |
| 22 #define SKIP_TYPE_NONE 0 | |
| 23 #define SKIP_TYPE_MPEG 1 | |
| 24 #define SKIP_TYPE_ROW 2 | |
| 25 #define SKIP_TYPE_COL 3 | |
| 26 | |
| 27 | |
| 28 typedef struct Wmv2Context{ | |
| 29 MpegEncContext s; | |
| 30 int j_type_bit; | |
| 31 int j_type; | |
| 32 int flag3; | |
| 33 int flag63; | |
| 34 int abt_flag; | |
| 35 int abt_type; | |
| 36 int abt_type_table[6]; | |
| 37 int per_mb_abt; | |
| 38 int per_block_abt; | |
| 39 int mspel_bit; | |
| 40 int cbp_table_index; | |
| 41 int top_left_mv_flag; | |
| 42 int per_mb_rl_bit; | |
| 43 int skip_type; | |
| 44 int hshift; | |
| 45 | |
| 46 ScanTable abt_scantable[2]; | |
| 47 DCTELEM abt_block2[6][64] __align8; | |
| 48 }Wmv2Context; | |
| 49 | |
| 50 static void wmv2_common_init(Wmv2Context * w){ | |
| 51 MpegEncContext * const s= &w->s; | |
| 52 | |
| 53 ff_init_scantable(s, &w->abt_scantable[0], wmv2_scantableA); | |
| 54 ff_init_scantable(s, &w->abt_scantable[1], wmv2_scantableB); | |
| 55 } | |
| 56 | |
| 57 static int encode_ext_header(Wmv2Context *w){ | |
| 58 MpegEncContext * const s= &w->s; | |
| 59 PutBitContext pb; | |
| 60 int code; | |
| 61 | |
| 62 init_put_bits(&pb, s->avctx->extradata, s->avctx->extradata_size, NULL, NULL); | |
| 63 | |
| 64 put_bits(&pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29 | |
| 65 put_bits(&pb, 11, FFMIN(s->bit_rate/1024, 2047)); | |
| 66 | |
| 67 put_bits(&pb, 1, w->mspel_bit=1); | |
| 68 put_bits(&pb, 1, w->flag3=1); | |
| 69 put_bits(&pb, 1, w->abt_flag=1); | |
| 70 put_bits(&pb, 1, w->j_type_bit=1); | |
| 71 put_bits(&pb, 1, w->top_left_mv_flag=0); | |
| 72 put_bits(&pb, 1, w->per_mb_rl_bit=1); | |
| 73 put_bits(&pb, 3, code=1); | |
| 74 | |
| 75 flush_put_bits(&pb); | |
| 76 | |
| 77 s->slice_height = s->mb_height / code; | |
| 78 | |
| 79 return 0; | |
| 80 } | |
| 81 | |
| 82 static int wmv2_encode_init(AVCodecContext *avctx){ | |
| 83 Wmv2Context * const w= avctx->priv_data; | |
| 84 | |
| 85 if(MPV_encode_init(avctx) < 0) | |
| 86 return -1; | |
| 87 | |
| 88 wmv2_common_init(w); | |
| 89 | |
| 90 avctx->extradata_size= 4; | |
| 91 avctx->extradata= av_mallocz(avctx->extradata_size + 10); | |
| 92 encode_ext_header(w); | |
| 93 | |
| 94 return 0; | |
| 95 } | |
| 96 | |
| 97 static int wmv2_encode_end(AVCodecContext *avctx){ | |
| 98 Wmv2Context * const w= avctx->priv_data; | |
| 99 | |
| 100 if(MPV_encode_end(avctx) < 0) | |
| 101 return -1; | |
| 102 | |
| 103 avctx->extradata_size= 0; | |
| 104 av_freep(&avctx->extradata); | |
| 105 | |
| 106 return 0; | |
| 107 } | |
| 108 | |
| 109 int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number) | |
| 110 { | |
| 111 Wmv2Context * const w= (Wmv2Context*)s; | |
| 112 | |
| 113 put_bits(&s->pb, 1, s->pict_type - 1); | |
| 114 if(s->pict_type == I_TYPE){ | |
| 115 put_bits(&s->pb, 7, 0); | |
| 116 } | |
| 117 put_bits(&s->pb, 5, s->qscale); | |
| 118 | |
| 119 s->dc_table_index = 1; | |
| 120 s->mv_table_index = 1; /* only if P frame */ | |
| 121 // s->use_skip_mb_code = 1; /* only if P frame */ | |
| 122 s->per_mb_rl_table = 0; | |
| 123 s->mspel= 0; | |
| 124 w->per_mb_abt=0; | |
| 125 w->abt_type=0; | |
| 126 w->j_type=0; | |
| 127 | |
| 128 if (s->pict_type == I_TYPE) { | |
| 129 if(w->j_type_bit) put_bits(&s->pb, 1, w->j_type); | |
| 130 | |
| 131 if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table); | |
| 132 | |
| 133 if(!s->per_mb_rl_table){ | |
| 134 code012(&s->pb, s->rl_chroma_table_index); | |
| 135 code012(&s->pb, s->rl_table_index); | |
| 136 } | |
| 137 | |
| 138 put_bits(&s->pb, 1, s->dc_table_index); | |
| 139 | |
| 140 s->inter_intra_pred= 0; | |
| 141 s->no_rounding = 1; | |
| 142 }else{ | |
| 143 int cbp_index; | |
| 144 | |
| 145 put_bits(&s->pb, 2, SKIP_TYPE_NONE); | |
| 146 | |
| 147 code012(&s->pb, cbp_index=0); | |
| 148 if(s->qscale <= 10){ | |
| 149 int map[3]= {0,2,1}; | |
| 150 w->cbp_table_index= map[cbp_index]; | |
| 151 }else if(s->qscale <= 20){ | |
| 152 int map[3]= {1,0,2}; | |
| 153 w->cbp_table_index= map[cbp_index]; | |
| 154 }else{ | |
| 155 int map[3]= {2,1,0}; | |
| 156 w->cbp_table_index= map[cbp_index]; | |
| 157 } | |
| 158 | |
| 159 if(w->mspel_bit) put_bits(&s->pb, 1, s->mspel); | |
| 160 | |
| 161 if(w->abt_flag){ | |
| 162 put_bits(&s->pb, 1, w->per_mb_abt^1); | |
| 163 if(!w->per_mb_abt){ | |
| 164 code012(&s->pb, w->abt_type); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table); | |
| 169 | |
| 170 if(!s->per_mb_rl_table){ | |
| 171 code012(&s->pb, s->rl_table_index); | |
| 172 s->rl_chroma_table_index = s->rl_table_index; | |
| 173 } | |
| 174 put_bits(&s->pb, 1, s->dc_table_index); | |
| 175 put_bits(&s->pb, 1, s->mv_table_index); | |
| 176 | |
| 177 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); | |
| 178 s->no_rounding ^= 1; | |
| 179 } | |
| 180 s->esc3_level_length= 0; | |
| 181 s->esc3_run_length= 0; | |
| 182 | |
| 183 return 0; | |
| 184 } | |
| 185 | |
| 186 // nearly idential to wmv1 but thats just because we dont use the useless M$ crap features | |
| 187 // its duplicated here in case someone wants to add support for these carp features | |
| 188 void ff_wmv2_encode_mb(MpegEncContext * s, | |
| 189 DCTELEM block[6][64], | |
| 190 int motion_x, int motion_y) | |
| 191 { | |
| 192 Wmv2Context * const w= (Wmv2Context*)s; | |
| 193 int cbp, coded_cbp, i; | |
| 194 int pred_x, pred_y; | |
| 1064 | 195 uint8_t *coded_block; |
| 936 | 196 |
| 197 handle_slices(s); | |
| 198 | |
| 199 if (!s->mb_intra) { | |
| 200 /* compute cbp */ | |
| 201 set_stat(ST_INTER_MB); | |
| 202 cbp = 0; | |
| 203 for (i = 0; i < 6; i++) { | |
| 204 if (s->block_last_index[i] >= 0) | |
| 205 cbp |= 1 << (5 - i); | |
| 206 } | |
| 207 | |
| 208 put_bits(&s->pb, | |
| 209 wmv2_inter_table[w->cbp_table_index][cbp + 64][1], | |
| 210 wmv2_inter_table[w->cbp_table_index][cbp + 64][0]); | |
| 211 | |
| 212 /* motion vector */ | |
| 213 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
| 214 msmpeg4_encode_motion(s, motion_x - pred_x, | |
| 215 motion_y - pred_y); | |
| 216 } else { | |
| 217 /* compute cbp */ | |
| 218 cbp = 0; | |
| 219 coded_cbp = 0; | |
| 220 for (i = 0; i < 6; i++) { | |
| 221 int val, pred; | |
| 222 val = (s->block_last_index[i] >= 1); | |
| 223 cbp |= val << (5 - i); | |
| 224 if (i < 4) { | |
| 225 /* predict value for close blocks only for luma */ | |
| 226 pred = coded_block_pred(s, i, &coded_block); | |
| 227 *coded_block = val; | |
| 228 val = val ^ pred; | |
| 229 } | |
| 230 coded_cbp |= val << (5 - i); | |
| 231 } | |
| 232 #if 0 | |
| 233 if (coded_cbp) | |
| 234 printf("cbp=%x %x\n", cbp, coded_cbp); | |
| 235 #endif | |
| 236 | |
| 237 if (s->pict_type == I_TYPE) { | |
| 238 set_stat(ST_INTRA_MB); | |
| 239 put_bits(&s->pb, | |
| 240 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]); | |
| 241 } else { | |
| 242 put_bits(&s->pb, | |
| 243 wmv2_inter_table[w->cbp_table_index][cbp][1], | |
| 244 wmv2_inter_table[w->cbp_table_index][cbp][0]); | |
| 245 } | |
| 246 set_stat(ST_INTRA_MB); | |
| 247 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
| 248 if(s->inter_intra_pred){ | |
| 249 s->h263_aic_dir=0; | |
| 250 put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]); | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 for (i = 0; i < 6; i++) { | |
| 255 msmpeg4_encode_block(s, block[i], i); | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 static void parse_mb_skip(Wmv2Context * w){ | |
| 260 int mb_x, mb_y; | |
| 261 MpegEncContext * const s= &w->s; | |
| 262 | |
| 263 w->skip_type= get_bits(&s->gb, 2); | |
| 264 switch(w->skip_type){ | |
| 265 case SKIP_TYPE_NONE: | |
| 266 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
| 267 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
| 268 s->mb_type[mb_y*s->mb_width + mb_x]= 0; | |
| 269 } | |
| 270 } | |
| 271 break; | |
| 272 case SKIP_TYPE_MPEG: | |
| 273 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
| 274 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
| 275 s->mb_type[mb_y*s->mb_width + mb_x]= get_bits1(&s->gb) ? MB_TYPE_SKIPED : 0; | |
| 276 } | |
| 277 } | |
| 278 break; | |
| 279 case SKIP_TYPE_ROW: | |
| 280 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
| 281 if(get_bits1(&s->gb)){ | |
| 282 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
| 283 s->mb_type[mb_y*s->mb_width + mb_x]= MB_TYPE_SKIPED; | |
| 284 } | |
| 285 }else{ | |
| 286 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
| 287 s->mb_type[mb_y*s->mb_width + mb_x]= get_bits1(&s->gb) ? MB_TYPE_SKIPED : 0; | |
| 288 } | |
| 289 } | |
| 290 } | |
| 291 break; | |
| 292 case SKIP_TYPE_COL: | |
| 293 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
| 294 if(get_bits1(&s->gb)){ | |
| 295 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
| 296 s->mb_type[mb_y*s->mb_width + mb_x]= MB_TYPE_SKIPED; | |
| 297 } | |
| 298 }else{ | |
| 299 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
| 300 s->mb_type[mb_y*s->mb_width + mb_x]= get_bits1(&s->gb) ? MB_TYPE_SKIPED : 0; | |
| 301 } | |
| 302 } | |
| 303 } | |
| 304 break; | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 static int decode_ext_header(Wmv2Context *w){ | |
| 309 MpegEncContext * const s= &w->s; | |
| 310 GetBitContext gb; | |
| 311 int fps; | |
| 312 int code; | |
| 313 | |
| 314 if(s->avctx->extradata_size<4) return -1; | |
| 315 | |
|
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
983
diff
changeset
|
316 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8); |
| 936 | 317 |
| 318 fps = get_bits(&gb, 5); | |
| 319 s->bit_rate = get_bits(&gb, 11)*1024; | |
| 320 w->mspel_bit = get_bits1(&gb); | |
| 321 w->flag3 = get_bits1(&gb); | |
| 322 w->abt_flag = get_bits1(&gb); | |
| 323 w->j_type_bit = get_bits1(&gb); | |
| 324 w->top_left_mv_flag= get_bits1(&gb); | |
| 325 w->per_mb_rl_bit = get_bits1(&gb); | |
| 326 code = get_bits(&gb, 3); | |
| 327 | |
| 328 if(code==0) return -1; | |
| 329 | |
| 330 s->slice_height = s->mb_height / code; | |
| 331 | |
| 332 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
| 983 | 333 printf("fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, tl_mv_flag:%d, mbrl_bit:%d, code:%d, flag3:%d, slices:%d\n", |
| 334 fps, s->bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit, w->top_left_mv_flag, w->per_mb_rl_bit, code, w->flag3, | |
| 335 code); | |
| 936 | 336 } |
| 337 return 0; | |
| 338 } | |
| 339 | |
| 340 int ff_wmv2_decode_picture_header(MpegEncContext * s) | |
| 341 { | |
| 342 Wmv2Context * const w= (Wmv2Context*)s; | |
| 343 int code, i; | |
| 344 | |
| 345 #if 0 | |
| 346 { | |
| 347 int i; | |
| 348 for(i=0; i<s->gb.size*8; i++) | |
| 349 printf("%d", get_bits1(&s->gb)); | |
| 350 // get_bits1(&s->gb); | |
| 351 printf("END\n"); | |
| 352 return -1; | |
| 353 } | |
| 354 #endif | |
| 355 if(s->picture_number==0) | |
| 356 decode_ext_header(w); | |
| 357 | |
| 358 s->pict_type = get_bits(&s->gb, 1) + 1; | |
| 359 if(s->pict_type == I_TYPE){ | |
| 360 code = get_bits(&s->gb, 7); | |
| 361 printf("I7:%X/\n", code); | |
| 362 } | |
| 363 s->qscale = get_bits(&s->gb, 5); | |
| 364 | |
| 365 if (s->pict_type == I_TYPE) { | |
| 366 if(w->j_type_bit) w->j_type= get_bits1(&s->gb); | |
| 367 else w->j_type= 0; //FIXME check | |
| 368 | |
| 369 if(!w->j_type){ | |
| 370 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb); | |
| 371 else s->per_mb_rl_table= 0; | |
| 372 | |
| 373 if(!s->per_mb_rl_table){ | |
| 374 s->rl_chroma_table_index = decode012(&s->gb); | |
| 375 s->rl_table_index = decode012(&s->gb); | |
| 376 } | |
| 377 | |
| 378 s->dc_table_index = get_bits1(&s->gb); | |
| 379 } | |
| 380 s->inter_intra_pred= 0; | |
| 381 s->no_rounding = 1; | |
| 382 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
| 383 printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n", | |
| 384 s->qscale, | |
| 385 s->rl_chroma_table_index, | |
| 386 s->rl_table_index, | |
| 387 s->dc_table_index, | |
| 388 s->per_mb_rl_table, | |
| 389 w->j_type); | |
| 390 } | |
| 391 }else{ | |
| 392 int cbp_index; | |
| 393 w->j_type=0; | |
| 394 | |
| 395 parse_mb_skip(w); | |
| 396 cbp_index= decode012(&s->gb); | |
| 397 if(s->qscale <= 10){ | |
| 398 int map[3]= {0,2,1}; | |
| 399 w->cbp_table_index= map[cbp_index]; | |
| 400 }else if(s->qscale <= 20){ | |
| 401 int map[3]= {1,0,2}; | |
| 402 w->cbp_table_index= map[cbp_index]; | |
| 403 }else{ | |
| 404 int map[3]= {2,1,0}; | |
| 405 w->cbp_table_index= map[cbp_index]; | |
| 406 } | |
| 407 | |
| 408 if(w->mspel_bit) s->mspel= get_bits1(&s->gb); | |
| 409 else s->mspel= 0; //FIXME check | |
| 410 | |
| 411 if(w->abt_flag){ | |
| 412 w->per_mb_abt= get_bits1(&s->gb)^1; | |
| 413 if(!w->per_mb_abt){ | |
| 414 w->abt_type= decode012(&s->gb); | |
| 415 } | |
| 416 } | |
| 417 | |
| 418 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb); | |
| 419 else s->per_mb_rl_table= 0; | |
| 420 | |
| 421 if(!s->per_mb_rl_table){ | |
| 422 s->rl_table_index = decode012(&s->gb); | |
| 423 s->rl_chroma_table_index = s->rl_table_index; | |
| 424 } | |
| 425 | |
| 426 s->dc_table_index = get_bits1(&s->gb); | |
| 427 s->mv_table_index = get_bits1(&s->gb); | |
| 428 | |
| 429 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); | |
| 430 s->no_rounding ^= 1; | |
| 431 | |
| 432 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
| 433 printf("rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n", | |
| 434 s->rl_table_index, | |
| 435 s->rl_chroma_table_index, | |
| 436 s->dc_table_index, | |
| 437 s->mv_table_index, | |
| 438 s->per_mb_rl_table, | |
| 439 s->qscale, | |
| 440 s->mspel, | |
| 441 w->per_mb_abt, | |
| 442 w->abt_type, | |
| 443 w->cbp_table_index, | |
| 444 s->inter_intra_pred); | |
| 445 } | |
| 446 } | |
| 447 s->esc3_level_length= 0; | |
| 448 s->esc3_run_length= 0; | |
| 449 | |
| 450 if(s->avctx->debug&FF_DEBUG_SKIP){ | |
| 451 for(i=0; i<s->mb_num; i++){ | |
| 452 if(i%s->mb_width==0) printf("\n"); | |
| 453 printf("%d", s->mb_type[i]); | |
| 454 } | |
| 455 } | |
| 456 s->picture_number++; //FIXME ? | |
| 457 | |
| 458 | |
| 459 // if(w->j_type) | |
| 460 // return wmv2_decode_j_picture(w); //FIXME | |
| 461 | |
| 462 if(w->j_type){ | |
| 463 printf("J-type picture isnt supported\n"); | |
| 464 return -1; | |
| 465 } | |
| 466 | |
| 467 return 0; | |
| 468 } | |
| 469 | |
| 1057 | 470 static void ff_wmv2_decode_init(MpegEncContext *s){ |
| 936 | 471 } |
| 472 | |
| 473 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){ | |
| 474 MpegEncContext * const s= &w->s; | |
| 475 int ret; | |
| 476 | |
| 477 ret= msmpeg4_decode_motion(s, mx_ptr, my_ptr); | |
| 478 | |
| 479 if(ret<0) return -1; | |
| 480 | |
| 481 if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel) | |
| 482 w->hshift= get_bits1(&s->gb); | |
| 483 else | |
| 484 w->hshift= 0; | |
| 485 | |
| 486 //printf("%d %d ", *mx_ptr, *my_ptr); | |
| 487 | |
| 488 return 0; | |
| 489 } | |
| 490 | |
| 491 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){ | |
| 492 MpegEncContext * const s= &w->s; | |
| 493 int xy, wrap, diff, type; | |
| 1064 | 494 int16_t *A, *B, *C, *mot_val; |
| 936 | 495 |
| 496 wrap = s->block_wrap[0]; | |
| 497 xy = s->block_index[0]; | |
| 498 | |
| 499 mot_val = s->motion_val[xy]; | |
| 500 | |
| 501 A = s->motion_val[xy - 1]; | |
| 502 B = s->motion_val[xy - wrap]; | |
| 503 C = s->motion_val[xy + 2 - wrap]; | |
| 504 | |
| 505 diff= FFMAX(ABS(A[0] - B[0]), ABS(A[1] - B[1])); | |
| 506 | |
| 1043 | 507 if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag && diff >= 8) |
| 936 | 508 type= get_bits1(&s->gb); |
| 509 else | |
| 510 type= 2; | |
| 511 | |
| 512 if(type == 0){ | |
| 513 *px= A[0]; | |
| 514 *py= A[1]; | |
| 515 }else if(type == 1){ | |
| 516 *px= B[0]; | |
| 517 *py= B[1]; | |
| 518 }else{ | |
| 519 /* special case for first (slice) line */ | |
| 520 if (s->first_slice_line) { | |
| 521 *px = A[0]; | |
| 522 *py = A[1]; | |
| 523 } else { | |
| 524 *px = mid_pred(A[0], B[0], C[0]); | |
| 525 *py = mid_pred(A[1], B[1], C[1]); | |
| 526 } | |
| 527 } | |
| 528 | |
| 529 return mot_val; | |
| 530 } | |
| 531 | |
| 532 static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){ | |
| 533 MpegEncContext * const s= &w->s; | |
| 534 static const int sub_cbp_table[3]= {2,3,1}; | |
| 535 int sub_cbp; | |
| 536 | |
| 537 if(!cbp){ | |
| 538 s->block_last_index[n] = -1; | |
| 539 | |
| 540 return 0; | |
| 541 } | |
| 542 | |
| 543 if(w->per_block_abt) | |
| 544 w->abt_type= decode012(&s->gb); | |
| 545 #if 0 | |
| 546 if(w->per_block_abt) | |
| 547 printf("B%d", w->abt_type); | |
| 548 #endif | |
| 549 w->abt_type_table[n]= w->abt_type; | |
| 550 | |
| 551 if(w->abt_type){ | |
| 552 // const uint8_t *scantable= w->abt_scantable[w->abt_type-1].permutated; | |
| 553 const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable; | |
| 554 // const uint8_t *scantable= w->abt_type-1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable; | |
| 555 | |
| 556 sub_cbp= sub_cbp_table[ decode012(&s->gb) ]; | |
| 557 // printf("S%d", sub_cbp); | |
| 558 | |
| 559 if(sub_cbp&1){ | |
| 560 if (msmpeg4_decode_block(s, block, n, 1, scantable) < 0) | |
| 561 return -1; | |
| 562 } | |
| 563 | |
| 564 if(sub_cbp&2){ | |
| 565 if (msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0) | |
| 566 return -1; | |
| 567 } | |
| 568 s->block_last_index[n] = 63; | |
| 569 | |
| 570 return 0; | |
| 571 }else{ | |
| 572 return msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated); | |
| 573 } | |
| 574 } | |
| 575 | |
| 576 static void wmv2_add_block(Wmv2Context *w, DCTELEM *block1, uint8_t *dst, int stride, int n){ | |
| 577 MpegEncContext * const s= &w->s; | |
| 578 uint8_t temp[2][64]; | |
| 1064 | 579 |
| 936 | 580 switch(w->abt_type_table[n]){ |
| 581 case 0: | |
| 582 if (s->block_last_index[n] >= 0) { | |
| 583 s->idct_add (dst, stride, block1); | |
| 584 } | |
| 585 break; | |
| 586 case 1: | |
| 587 simple_idct84_add(dst , stride, block1); | |
| 588 simple_idct84_add(dst + 4*stride, stride, w->abt_block2[n]); | |
| 589 memset(w->abt_block2[n], 0, 64*sizeof(DCTELEM)); | |
| 590 break; | |
| 591 case 2: | |
| 592 simple_idct48_add(dst , stride, block1); | |
| 593 simple_idct48_add(dst + 4 , stride, w->abt_block2[n]); | |
| 594 memset(w->abt_block2[n], 0, 64*sizeof(DCTELEM)); | |
| 595 break; | |
| 596 default: | |
| 597 fprintf(stderr, "internal error in WMV2 abt\n"); | |
| 598 } | |
| 599 } | |
| 600 | |
| 601 void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block1[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr){ | |
| 602 Wmv2Context * const w= (Wmv2Context*)s; | |
| 603 | |
| 604 wmv2_add_block(w, block1[0], dest_y , s->linesize, 0); | |
| 605 wmv2_add_block(w, block1[1], dest_y + 8 , s->linesize, 1); | |
| 606 wmv2_add_block(w, block1[2], dest_y + 8*s->linesize, s->linesize, 2); | |
| 607 wmv2_add_block(w, block1[3], dest_y + 8 + 8*s->linesize, s->linesize, 3); | |
| 608 | |
| 609 if(s->flags&CODEC_FLAG_GRAY) return; | |
| 610 | |
| 611 wmv2_add_block(w, block1[4], dest_cb , s->uvlinesize, 4); | |
| 612 wmv2_add_block(w, block1[5], dest_cr , s->uvlinesize, 5); | |
| 613 } | |
| 614 | |
| 615 void ff_mspel_motion(MpegEncContext *s, | |
| 1064 | 616 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, |
| 617 uint8_t **ref_picture, op_pixels_func (*pix_op)[4], | |
| 936 | 618 int motion_x, int motion_y, int h) |
| 619 { | |
| 620 Wmv2Context * const w= (Wmv2Context*)s; | |
| 1064 | 621 uint8_t *ptr; |
| 936 | 622 int dxy, offset, mx, my, src_x, src_y, v_edge_pos, linesize, uvlinesize; |
| 623 int emu=0; | |
| 624 | |
| 625 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
| 626 dxy = 2*dxy + w->hshift; | |
| 627 src_x = s->mb_x * 16 + (motion_x >> 1); | |
| 628 src_y = s->mb_y * 16 + (motion_y >> 1); | |
| 629 | |
| 630 /* WARNING: do no forget half pels */ | |
| 631 v_edge_pos = s->v_edge_pos; | |
| 632 src_x = clip(src_x, -16, s->width); | |
| 633 src_y = clip(src_y, -16, s->height); | |
| 634 linesize = s->linesize; | |
| 635 uvlinesize = s->uvlinesize; | |
| 636 ptr = ref_picture[0] + (src_y * linesize) + src_x; | |
| 637 | |
| 638 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
| 639 if(src_x<1 || src_y<1 || src_x + 17 >= s->h_edge_pos | |
| 640 || src_y + h+1 >= v_edge_pos){ | |
| 641 ff_emulated_edge_mc(s, ptr - 1 - s->linesize, s->linesize, 19, 19, | |
| 642 src_x-1, src_y-1, s->h_edge_pos, s->v_edge_pos); | |
| 643 ptr= s->edge_emu_buffer + 1 + s->linesize; | |
| 644 emu=1; | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 s->dsp.put_mspel_pixels_tab[dxy](dest_y , ptr , linesize); | |
| 649 s->dsp.put_mspel_pixels_tab[dxy](dest_y+8 , ptr+8 , linesize); | |
| 650 s->dsp.put_mspel_pixels_tab[dxy](dest_y +8*linesize, ptr +8*linesize, linesize); | |
| 651 s->dsp.put_mspel_pixels_tab[dxy](dest_y+8+8*linesize, ptr+8+8*linesize, linesize); | |
| 652 | |
| 653 if(s->flags&CODEC_FLAG_GRAY) return; | |
| 654 | |
| 655 if (s->out_format == FMT_H263) { | |
| 656 dxy = 0; | |
| 657 if ((motion_x & 3) != 0) | |
| 658 dxy |= 1; | |
| 659 if ((motion_y & 3) != 0) | |
| 660 dxy |= 2; | |
| 661 mx = motion_x >> 2; | |
| 662 my = motion_y >> 2; | |
| 663 } else { | |
| 664 mx = motion_x / 2; | |
| 665 my = motion_y / 2; | |
| 666 dxy = ((my & 1) << 1) | (mx & 1); | |
| 667 mx >>= 1; | |
| 668 my >>= 1; | |
| 669 } | |
| 670 | |
| 671 src_x = s->mb_x * 8 + mx; | |
| 672 src_y = s->mb_y * 8 + my; | |
| 673 src_x = clip(src_x, -8, s->width >> 1); | |
| 674 if (src_x == (s->width >> 1)) | |
| 675 dxy &= ~1; | |
| 676 src_y = clip(src_y, -8, s->height >> 1); | |
| 677 if (src_y == (s->height >> 1)) | |
| 678 dxy &= ~2; | |
| 679 offset = (src_y * uvlinesize) + src_x; | |
| 680 ptr = ref_picture[1] + offset; | |
| 681 if(emu){ | |
| 682 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, | |
| 683 src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
| 684 ptr= s->edge_emu_buffer; | |
| 685 } | |
| 686 pix_op[1][dxy](dest_cb, ptr, uvlinesize, h >> 1); | |
| 687 | |
| 688 ptr = ref_picture[2] + offset; | |
| 689 if(emu){ | |
| 690 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, | |
| 691 src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
| 692 ptr= s->edge_emu_buffer; | |
| 693 } | |
| 694 pix_op[1][dxy](dest_cr, ptr, uvlinesize, h >> 1); | |
| 695 } | |
| 696 | |
| 697 | |
| 698 static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
| 699 { | |
| 700 Wmv2Context * const w= (Wmv2Context*)s; | |
| 701 int cbp, code, i; | |
| 1064 | 702 uint8_t *coded_val; |
| 936 | 703 |
| 704 if(w->j_type) return 0; | |
| 705 | |
| 706 s->error_status_table[s->mb_x + s->mb_y*s->mb_width]= 0; | |
| 707 | |
| 708 if (s->pict_type == P_TYPE) { | |
| 709 if(s->mb_type[s->mb_y * s->mb_width + s->mb_x]&MB_TYPE_SKIPED){ | |
| 710 /* skip mb */ | |
| 711 s->mb_intra = 0; | |
| 712 for(i=0;i<6;i++) | |
| 713 s->block_last_index[i] = -1; | |
| 714 s->mv_dir = MV_DIR_FORWARD; | |
| 715 s->mv_type = MV_TYPE_16X16; | |
| 716 s->mv[0][0][0] = 0; | |
| 717 s->mv[0][0][1] = 0; | |
| 718 s->mb_skiped = 1; | |
| 719 return 0; | |
| 720 } | |
| 721 | |
| 722 code = get_vlc2(&s->gb, mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3); | |
| 723 if (code < 0) | |
| 724 return -1; | |
| 725 s->mb_intra = (~code & 0x40) >> 6; | |
| 726 | |
| 727 cbp = code & 0x3f; | |
| 728 } else { | |
| 729 s->mb_intra = 1; | |
| 730 code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2); | |
| 731 if (code < 0){ | |
| 732 fprintf(stderr, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y); | |
| 733 return -1; | |
| 734 } | |
| 735 /* predict coded block pattern */ | |
| 736 cbp = 0; | |
| 737 for(i=0;i<6;i++) { | |
| 738 int val = ((code >> (5 - i)) & 1); | |
| 739 if (i < 4) { | |
| 740 int pred = coded_block_pred(s, i, &coded_val); | |
| 741 val = val ^ pred; | |
| 742 *coded_val = val; | |
| 743 } | |
| 744 cbp |= val << (5 - i); | |
| 745 } | |
| 746 } | |
| 747 | |
| 748 if (!s->mb_intra) { | |
| 749 int mx, my; | |
| 750 //printf("P at %d %d\n", s->mb_x, s->mb_y); | |
| 751 wmv2_pred_motion(w, &mx, &my); | |
| 752 | |
| 753 if(cbp){ | |
| 754 if(s->per_mb_rl_table){ | |
| 755 s->rl_table_index = decode012(&s->gb); | |
| 756 s->rl_chroma_table_index = s->rl_table_index; | |
| 757 } | |
| 758 | |
| 759 if(w->abt_flag && w->per_mb_abt){ | |
| 760 w->per_block_abt= get_bits1(&s->gb); | |
| 761 if(!w->per_block_abt) | |
| 762 w->abt_type= decode012(&s->gb); | |
| 763 }else | |
| 764 w->per_block_abt=0; | |
| 765 } | |
| 766 | |
| 767 if (wmv2_decode_motion(w, &mx, &my) < 0) | |
| 768 return -1; | |
| 769 | |
| 770 s->mv_dir = MV_DIR_FORWARD; | |
| 771 s->mv_type = MV_TYPE_16X16; | |
| 772 s->mv[0][0][0] = mx; | |
| 773 s->mv[0][0][1] = my; | |
| 774 | |
| 775 for (i = 0; i < 6; i++) { | |
| 776 if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 777 { | |
| 778 fprintf(stderr,"\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
| 779 return -1; | |
| 780 } | |
| 781 } | |
| 782 } else { | |
| 783 //if(s->pict_type==P_TYPE) | |
| 784 // printf("%d%d ", s->inter_intra_pred, cbp); | |
| 785 //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24)); | |
| 786 s->ac_pred = get_bits1(&s->gb); | |
| 787 if(s->inter_intra_pred){ | |
| 788 s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1); | |
| 789 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y); | |
| 790 } | |
| 791 if(s->per_mb_rl_table && cbp){ | |
| 792 s->rl_table_index = decode012(&s->gb); | |
| 793 s->rl_chroma_table_index = s->rl_table_index; | |
| 794 } | |
| 795 | |
| 796 for (i = 0; i < 6; i++) { | |
| 797 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) | |
| 798 { | |
| 799 fprintf(stderr,"\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
| 800 return -1; | |
| 801 } | |
| 802 } | |
| 803 } | |
| 804 | |
| 805 return 0; | |
| 806 } | |
| 807 | |
| 808 static int wmv2_decode_init(AVCodecContext *avctx){ | |
| 809 Wmv2Context * const w= avctx->priv_data; | |
| 810 | |
| 811 if(ff_h263_decode_init(avctx) < 0) | |
| 812 return -1; | |
| 813 | |
| 814 wmv2_common_init(w); | |
| 815 | |
| 816 return 0; | |
| 817 } | |
| 818 | |
| 819 AVCodec wmv2_decoder = { | |
| 820 "wmv2", | |
| 821 CODEC_TYPE_VIDEO, | |
| 822 CODEC_ID_WMV2, | |
| 823 sizeof(Wmv2Context), | |
| 824 wmv2_decode_init, | |
| 825 NULL, | |
| 826 ff_h263_decode_end, | |
| 827 ff_h263_decode_frame, | |
| 828 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, | |
| 829 }; | |
| 830 | |
| 831 AVCodec wmv2_encoder = { | |
| 832 "wmv2", | |
| 833 CODEC_TYPE_VIDEO, | |
| 834 CODEC_ID_WMV2, | |
| 835 sizeof(Wmv2Context), | |
| 836 wmv2_encode_init, | |
| 837 MPV_encode_picture, | |
| 838 MPV_encode_end, | |
| 839 }; | |
| 840 |
