h264.c
资源名称:X264CODEC.rar [点击查看]
上传用户:lctgjx
上传日期:2022-06-04
资源大小:8887k
文件大小:321k
源码类别:
流媒体/Mpeg4/MP4
开发平台:
Visual C++
- /*
- * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
- * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- /**
- * @file h264.c
- * H.264 / AVC / MPEG4 part10 codec.
- * @author Michael Niedermayer <michaelni@gmx.at>
- */
- #include "dsputil.h"
- #include "avcodec.h"
- #include "mpegvideo.h"
- #include "h264.h"
- #include "h264data.h"
- #include "h264_parser.h"
- #include "golomb.h"
- #include "rectangle.h"
- #include "define.h"
- #include "internal.h"
- #include "cabac.h"
- #ifdef ARCH_X86
- #include "i386/h264_i386.h"
- #endif
- //#undef NDEBUG
- #include <assert.h>
- /**
- * Value of Picture.reference when Picture is not a reference picture, but
- * is held for delayed output.
- */
- #define DELAYED_PIC_REF 4
- static VLC coeff_token_vlc[4];
- static VLC chroma_dc_coeff_token_vlc;
- static VLC total_zeros_vlc[15];
- static VLC chroma_dc_total_zeros_vlc[3];
- static VLC run_vlc[6];
- static VLC run7_vlc;
- static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
- static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
- static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
- static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
- static av_always_inline uint32_t pack16to32(int a, int b){
- #ifdef WORDS_BIGENDIAN
- return (b&0xFFFF) + (a<<16);
- #else
- return (a&0xFFFF) + (b<<16);
- #endif
- }
- const uint8_t ff_rem6[52]={
- 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
- };
- const uint8_t ff_log2_tab[256]={
- 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
- 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
- 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
- 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
- 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
- 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
- 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
- 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
- };
- const uint8_t ff_div6[52]={
- 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
- };
- static void fill_caches(H264Context *h, int mb_type, int for_deblock){
- MpegEncContext * const s = &h->s;
- const int mb_xy= h->mb_xy;
- int topleft_xy, top_xy, topright_xy, left_xy[2];
- int topleft_type, top_type, topright_type, left_type[2];
- int left_block[8];
- int topleft_partition= -1;
- int i;
- top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
- //FIXME deblocking could skip the intra and nnz parts.
- if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)
- return;
- /* Wow, what a mess, why didn't they simplify the interlacing & intra
- * stuff, I can't imagine that these complex rules are worth it. */
- topleft_xy = top_xy - 1;
- topright_xy= top_xy + 1;
- left_xy[1] = left_xy[0] = mb_xy-1;
- left_block[0]= 0;
- left_block[1]= 1;
- left_block[2]= 2;
- left_block[3]= 3;
- left_block[4]= 7;
- left_block[5]= 10;
- left_block[6]= 8;
- left_block[7]= 11;
- if(FRAME_MBAFF){
- const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
- const int top_pair_xy = pair_xy - s->mb_stride;
- const int topleft_pair_xy = top_pair_xy - 1;
- const int topright_pair_xy = top_pair_xy + 1;
- const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
- const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
- const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
- const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
- const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
- const int bottom = (s->mb_y & 1);
- tprintf(s->avctx, "fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%dn", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
- if (bottom
- ? !curr_mb_frame_flag // bottom macroblock
- : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
- ) {
- top_xy -= s->mb_stride;
- }
- if (bottom
- ? !curr_mb_frame_flag // bottom macroblock
- : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
- ) {
- topleft_xy -= s->mb_stride;
- } else if(bottom && curr_mb_frame_flag && !left_mb_frame_flag) {
- topleft_xy += s->mb_stride;
- // take topleft mv from the middle of the mb, as opposed to all other modes which use the bottom-right partition
- topleft_partition = 0;
- }
- if (bottom
- ? !curr_mb_frame_flag // bottom macroblock
- : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
- ) {
- topright_xy -= s->mb_stride;
- }
- if (left_mb_frame_flag != curr_mb_frame_flag) {
- left_xy[1] = left_xy[0] = pair_xy - 1;
- if (curr_mb_frame_flag) {
- if (bottom) {
- left_block[0]= 2;
- left_block[1]= 2;
- left_block[2]= 3;
- left_block[3]= 3;
- left_block[4]= 8;
- left_block[5]= 11;
- left_block[6]= 8;
- left_block[7]= 11;
- } else {
- left_block[0]= 0;
- left_block[1]= 0;
- left_block[2]= 1;
- left_block[3]= 1;
- left_block[4]= 7;
- left_block[5]= 10;
- left_block[6]= 7;
- left_block[7]= 10;
- }
- } else {
- left_xy[1] += s->mb_stride;
- //left_block[0]= 0;
- left_block[1]= 2;
- left_block[2]= 0;
- left_block[3]= 2;
- //left_block[4]= 7;
- left_block[5]= 10;
- left_block[6]= 7;
- left_block[7]= 10;
- }
- }
- }
- h->top_mb_xy = top_xy;
- h->left_mb_xy[0] = left_xy[0];
- h->left_mb_xy[1] = left_xy[1];
- if(for_deblock){
- topleft_type = 0;
- topright_type = 0;
- top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
- left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
- left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
- if(FRAME_MBAFF && !IS_INTRA(mb_type)){
- int list;
- int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
- for(i=0; i<16; i++)
- h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
- for(list=0; list<h->list_count; list++){
- if(USES_LIST(mb_type,list)){
- uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
- uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
- int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
- for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
- dst[0] = src[0];
- dst[1] = src[1];
- dst[2] = src[2];
- dst[3] = src[3];
- }
- *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
- *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
- ref += h->b8_stride;
- *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
- *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
- }else{
- fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
- fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
- }
- }
- }
- }else{
- topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
- top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
- topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
- left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
- left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
- }
- if(IS_INTRA(mb_type)){
- h->topleft_samples_available=
- h->top_samples_available=
- h->left_samples_available= 0xFFFF;
- h->topright_samples_available= 0xEEEA;
- if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
- h->topleft_samples_available= 0xB3FF;
- h->top_samples_available= 0x33FF;
- h->topright_samples_available= 0x26EA;
- }
- for(i=0; i<2; i++){
- if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
- h->topleft_samples_available&= 0xDF5F;
- h->left_samples_available&= 0x5F5F;
- }
- }
- if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
- h->topleft_samples_available&= 0x7FFF;
- if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
- h->topright_samples_available&= 0xFBFF;
- if(IS_INTRA4x4(mb_type)){
- if(IS_INTRA4x4(top_type)){
- h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
- h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
- h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
- h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
- }else{
- int pred;
- if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
- pred= -1;
- else{
- pred= 2;
- }
- h->intra4x4_pred_mode_cache[4+8*0]=
- h->intra4x4_pred_mode_cache[5+8*0]=
- h->intra4x4_pred_mode_cache[6+8*0]=
- h->intra4x4_pred_mode_cache[7+8*0]= pred;
- }
- for(i=0; i<2; i++){
- if(IS_INTRA4x4(left_type[i])){
- h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
- h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
- }else{
- int pred;
- if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
- pred= -1;
- else{
- pred= 2;
- }
- h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
- h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
- }
- }
- }
- }
- /*
- 0 . T T. T T T T
- 1 L . .L . . . .
- 2 L . .L . . . .
- 3 . T TL . . . .
- 4 L . .L . . . .
- 5 L . .. . . . .
- */
- //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
- if(top_type){
- h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
- h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
- h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
- h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
- h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
- h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
- h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
- h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
- }else{
- h->non_zero_count_cache[4+8*0]=
- h->non_zero_count_cache[5+8*0]=
- h->non_zero_count_cache[6+8*0]=
- h->non_zero_count_cache[7+8*0]=
- h->non_zero_count_cache[1+8*0]=
- h->non_zero_count_cache[2+8*0]=
- h->non_zero_count_cache[1+8*3]=
- h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
- }
- for (i=0; i<2; i++) {
- if(left_type[i]){
- h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
- h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
- h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
- h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
- }else{
- h->non_zero_count_cache[3+8*1 + 2*8*i]=
- h->non_zero_count_cache[3+8*2 + 2*8*i]=
- h->non_zero_count_cache[0+8*1 + 8*i]=
- h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
- }
- }
- if( h->pps.cabac ) {
- // top_cbp
- if(top_type) {
- h->top_cbp = h->cbp_table[top_xy];
- } else if(IS_INTRA(mb_type)) {
- h->top_cbp = 0x1C0;
- } else {
- h->top_cbp = 0;
- }
- // left_cbp
- if (left_type[0]) {
- h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
- } else if(IS_INTRA(mb_type)) {
- h->left_cbp = 0x1C0;
- } else {
- h->left_cbp = 0;
- }
- if (left_type[0]) {
- h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
- }
- if (left_type[1]) {
- h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
- }
- }
- #if 1
- if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
- int list;
- for(list=0; list<h->list_count; list++){
- if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
- /*if(!h->mv_cache_clean[list]){
- memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
- memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
- h->mv_cache_clean[list]= 1;
- }*/
- continue;
- }
- h->mv_cache_clean[list]= 0;
- if(USES_LIST(top_type, list)){
- const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
- const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
- *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
- *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
- *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
- *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
- h->ref_cache[list][scan8[0] + 0 - 1*8]=
- h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
- h->ref_cache[list][scan8[0] + 2 - 1*8]=
- h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
- }else{
- *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
- *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
- *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
- *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
- *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
- }
- for(i=0; i<2; i++){
- int cache_idx = scan8[0] - 1 + i*2*8;
- if(USES_LIST(left_type[i], list)){
- const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
- const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
- *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
- *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
- h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
- h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
- }else{
- *(uint32_t*)h->mv_cache [list][cache_idx ]=
- *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
- h->ref_cache[list][cache_idx ]=
- h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
- }
- }
- if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
- continue;
- if(USES_LIST(topleft_type, list)){
- const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
- const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
- *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
- h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
- }else{
- *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
- h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
- }
- if(USES_LIST(topright_type, list)){
- const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
- const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
- *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
- h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
- }else{
- *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
- h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
- }
- if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
- continue;
- h->ref_cache[list][scan8[5 ]+1] =
- h->ref_cache[list][scan8[7 ]+1] =
- h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
- h->ref_cache[list][scan8[4 ]] =
- h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
- *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
- *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
- *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
- *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
- *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
- if( h->pps.cabac ) {
- /* XXX beurk, Load mvd */
- if(USES_LIST(top_type, list)){
- const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
- *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
- *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
- *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
- *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
- }else{
- *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
- *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
- *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
- *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
- }
- if(USES_LIST(left_type[0], list)){
- const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
- *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
- *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
- }else{
- *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
- *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
- }
- if(USES_LIST(left_type[1], list)){
- const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
- *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
- *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
- }else{
- *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
- *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
- }
- *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
- *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
- *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
- *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
- *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
- if(h->slice_type == FF_B_TYPE){
- fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
- if(IS_DIRECT(top_type)){
- *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
- }else if(IS_8X8(top_type)){
- int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
- h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
- h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
- }else{
- *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
- }
- if(IS_DIRECT(left_type[0]))
- h->direct_cache[scan8[0] - 1 + 0*8]= 1;
- else if(IS_8X8(left_type[0]))
- h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
- else
- h->direct_cache[scan8[0] - 1 + 0*8]= 0;
- if(IS_DIRECT(left_type[1]))
- h->direct_cache[scan8[0] - 1 + 2*8]= 1;
- else if(IS_8X8(left_type[1]))
- h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
- else
- h->direct_cache[scan8[0] - 1 + 2*8]= 0;
- }
- }
- if(FRAME_MBAFF){
- #define MAP_MVS
- MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)
- MAP_F2F(scan8[0] + 0 - 1*8, top_type)
- MAP_F2F(scan8[0] + 1 - 1*8, top_type)
- MAP_F2F(scan8[0] + 2 - 1*8, top_type)
- MAP_F2F(scan8[0] + 3 - 1*8, top_type)
- MAP_F2F(scan8[0] + 4 - 1*8, topright_type)
- MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])
- MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])
- MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])
- MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
- if(MB_FIELD){
- #define MAP_F2F(idx, mb_type)
- if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){
- h->ref_cache[list][idx] <<= 1;
- h->mv_cache[list][idx][1] /= 2;
- h->mvd_cache[list][idx][1] /= 2;
- }
- MAP_MVS
- #undef MAP_F2F
- }else{
- #define MAP_F2F(idx, mb_type)
- if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){
- h->ref_cache[list][idx] >>= 1;
- h->mv_cache[list][idx][1] <<= 1;
- h->mvd_cache[list][idx][1] <<= 1;
- }
- MAP_MVS
- #undef MAP_F2F
- }
- }
- }
- }
- #endif
- h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
- }
- static inline void write_back_intra_pred_mode(H264Context *h){
- const int mb_xy= h->mb_xy;
- h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
- h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
- h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
- h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
- h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
- h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
- h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
- }
- /**
- * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
- */
- static inline int check_intra4x4_pred_mode(H264Context *h){
- MpegEncContext * const s = &h->s;
- static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
- static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
- int i;
- if(!(h->top_samples_available&0x8000)){
- for(i=0; i<4; i++){
- int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
- if(status<0){
- av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %dn", status, s->mb_x, s->mb_y);
- return -1;
- } else if(status){
- h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
- }
- }
- }
- if(!(h->left_samples_available&0x8000)){
- for(i=0; i<4; i++){
- int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
- if(status<0){
- av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %dn", status, s->mb_x, s->mb_y);
- return -1;
- } else if(status){
- h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
- }
- }
- }
- return 0;
- } //FIXME cleanup like next
- /**
- * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
- */
- static inline int check_intra_pred_mode(H264Context *h, int mode){
- MpegEncContext * const s = &h->s;
- static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
- static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
- if(mode > 6U) {
- av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %dn", s->mb_x, s->mb_y);
- return -1;
- }
- if(!(h->top_samples_available&0x8000)){
- mode= top[ mode ];
- if(mode<0){
- av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %dn", s->mb_x, s->mb_y);
- return -1;
- }
- }
- if(!(h->left_samples_available&0x8000)){
- mode= left[ mode ];
- if(mode<0){
- av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %dn", s->mb_x, s->mb_y);
- return -1;
- }
- }
- return mode;
- }
- /**
- * gets the predicted intra4x4 prediction mode.
- */
- static inline int pred_intra_mode(H264Context *h, int n){
- const int index8= scan8[n];
- const int left= h->intra4x4_pred_mode_cache[index8 - 1];
- const int top = h->intra4x4_pred_mode_cache[index8 - 8];
- const int min= FFMIN(left, top);
- tprintf(h->s.avctx, "mode:%d %d min:%dn", left ,top, min);
- if(min<0) return DC_PRED;
- else return min;
- }
- static inline void write_back_non_zero_count(H264Context *h){
- const int mb_xy= h->mb_xy;
- h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
- h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
- h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
- h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
- h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
- h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
- h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
- h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
- h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
- h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
- h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
- h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
- h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
- if(FRAME_MBAFF){
- // store all luma nnzs, for deblocking
- int v = 0, i;
- for(i=0; i<16; i++)
- v += (!!h->non_zero_count_cache[scan8[i]]) << i;
- *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
- }
- }
- /**
- * gets the predicted number of non zero coefficients.
- * @param n block index
- */
- static inline int pred_non_zero_count(H264Context *h, int n){
- const int index8= scan8[n];
- const int left= h->non_zero_count_cache[index8 - 1];
- const int top = h->non_zero_count_cache[index8 - 8];
- int i= left + top;
- if(i<64) i= (i+1)>>1;
- tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%Xn", left, top, n, scan8[n], i&31);
- return i&31;
- }
- static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
- const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
- MpegEncContext *s = &h->s;
- /* there is no consistent mapping of mvs to neighboring locations that will
- * make mbaff happy, so we can't move all this logic to fill_caches */
- if(FRAME_MBAFF){
- const uint32_t *mb_types = s->current_picture_ptr->mb_type;
- const int16_t *mv;
- *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
- *C = h->mv_cache[list][scan8[0]-2];
- if(!MB_FIELD
- && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
- int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
- if(IS_INTERLACED(mb_types[topright_xy])){
- #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)
- const int x4 = X4, y4 = Y4;
- const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];
- if(!USES_LIST(mb_type,list))
- return LIST_NOT_USED;
- mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];
- h->mv_cache[list][scan8[0]-2][0] = mv[0];
- h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;
- return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
- SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
- }
- }
- if(topright_ref == PART_NOT_AVAILABLE
- && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
- && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
- if(!MB_FIELD
- && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
- SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
- }
- if(MB_FIELD
- && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
- && i >= scan8[0]+8){
- // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
- SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
- }
- }
- #undef SET_DIAG_MV
- }
- if(topright_ref != PART_NOT_AVAILABLE){
- *C= h->mv_cache[list][ i - 8 + part_width ];
- return topright_ref;
- }else{
- tprintf(s->avctx, "topright MV not availablen");
- *C= h->mv_cache[list][ i - 8 - 1 ];
- return h->ref_cache[list][ i - 8 - 1 ];
- }
- }
- /**
- * gets the predicted MV.
- * @param n the block index
- * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
- * @param mx the x component of the predicted motion vector
- * @param my the y component of the predicted motion vector
- */
- static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
- const int index8= scan8[n];
- const int top_ref= h->ref_cache[list][ index8 - 8 ];
- const int left_ref= h->ref_cache[list][ index8 - 1 ];
- const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
- const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
- const int16_t * C;
- int diagonal_ref, match_count;
- assert(part_width==1 || part_width==2 || part_width==4);
- /* mv_cache
- B . . A T T T T
- U . . L . . , .
- U . . L . . . .
- U . . L . . , .
- . . . L . . . .
- */
- diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
- match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
- tprintf(h->s.avctx, "pred_motion match_count=%dn", match_count);
- if(match_count > 1){ //most common
- *mx= mid_pred(A[0], B[0], C[0]);
- *my= mid_pred(A[1], B[1], C[1]);
- }else if(match_count==1){
- if(left_ref==ref){
- *mx= A[0];
- *my= A[1];
- }else if(top_ref==ref){
- *mx= B[0];
- *my= B[1];
- }else{
- *mx= C[0];
- *my= C[1];
- }
- }else{
- if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
- *mx= A[0];
- *my= A[1];
- }else{
- *mx= mid_pred(A[0], B[0], C[0]);
- *my= mid_pred(A[1], B[1], C[1]);
- }
- }
- tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %dn", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
- }
- /**
- * gets the directionally predicted 16x8 MV.
- * @param n the block index
- * @param mx the x component of the predicted motion vector
- * @param my the y component of the predicted motion vector
- */
- static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
- if(n==0){
- const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
- const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
- tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %dn", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
- if(top_ref == ref){
- *mx= B[0];
- *my= B[1];
- return;
- }
- }else{
- const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
- const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
- tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %dn", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
- if(left_ref == ref){
- *mx= A[0];
- *my= A[1];
- return;
- }
- }
- //RARE
- pred_motion(h, n, 4, list, ref, mx, my);
- }
- /**
- * gets the directionally predicted 8x16 MV.
- * @param n the block index
- * @param mx the x component of the predicted motion vector
- * @param my the y component of the predicted motion vector
- */
- static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
- if(n==0){
- const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
- const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
- tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %dn", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
- if(left_ref == ref){
- *mx= A[0];
- *my= A[1];
- return;
- }
- }else{
- const int16_t * C;
- int diagonal_ref;
- diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
- tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %dn", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
- if(diagonal_ref == ref){
- *mx= C[0];
- *my= C[1];
- return;
- }
- }
- //RARE
- pred_motion(h, n, 2, list, ref, mx, my);
- }
- static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
- const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
- const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
- tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2dn", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
- if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
- || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
- || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
- *mx = *my = 0;
- return;
- }
- pred_motion(h, 0, 4, 0, 0, mx, my);
- return;
- }
- static inline void direct_dist_scale_factor(H264Context * const h){
- const int poc = h->s.current_picture_ptr->poc;
- const int poc1 = h->ref_list[1][0].poc;
- int i;
- for(i=0; i<h->ref_count[0]; i++){
- int poc0 = h->ref_list[0][i].poc;
- int td = av_clip(poc1 - poc0, -128, 127);
- if(td == 0 /* FIXME || pic0 is a long-term ref */){
- h->dist_scale_factor[i] = 256;
- }else{
- int tb = av_clip(poc - poc0, -128, 127);
- int tx = (16384 + (FFABS(td) >> 1)) / td;
- h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
- }
- }
- if(FRAME_MBAFF){
- for(i=0; i<h->ref_count[0]; i++){
- h->dist_scale_factor_field[2*i] =
- h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
- }
- }
- }
- static inline void direct_ref_list_init(H264Context * const h){
- MpegEncContext * const s = &h->s;
- Picture * const ref1 = &h->ref_list[1][0];
- Picture * const cur = s->current_picture_ptr;
- int list, i, j;
- if(cur->pict_type == FF_I_TYPE)
- cur->ref_count[0] = 0;
- if(cur->pict_type != FF_B_TYPE)
- cur->ref_count[1] = 0;
- for(list=0; list<2; list++){
- cur->ref_count[list] = h->ref_count[list];
- for(j=0; j<h->ref_count[list]; j++)
- cur->ref_poc[list][j] = h->ref_list[list][j].poc;
- }
- if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
- return;
- for(list=0; list<2; list++){
- for(i=0; i<ref1->ref_count[list]; i++){
- const int poc = ref1->ref_poc[list][i];
- h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
- for(j=0; j<h->ref_count[list]; j++)
- if(h->ref_list[list][j].poc == poc){
- h->map_col_to_list0[list][i] = j;
- break;
- }
- }
- }
- if(FRAME_MBAFF){
- for(list=0; list<2; list++){
- for(i=0; i<ref1->ref_count[list]; i++){
- j = h->map_col_to_list0[list][i];
- h->map_col_to_list0_field[list][2*i] = 2*j;
- h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
- }
- }
- }
- }
- static inline void pred_direct_motion(H264Context * const h, int *mb_type){
- MpegEncContext * const s = &h->s;
- const int mb_xy = h->mb_xy;
- const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
- const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
- const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
- const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
- const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
- const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
- const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
- const int is_b8x8 = IS_8X8(*mb_type);
- unsigned int sub_mb_type;
- int i8, i4;
- #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
- if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
- /* FIXME save sub mb types from previous frames (or derive from MVs)
- * so we know exactly what block size to use */
- sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
- *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
- }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
- sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
- *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
- }else{
- sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
- *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
- }
- if(!is_b8x8)
- *mb_type |= MB_TYPE_DIRECT2;
- if(MB_FIELD)
- *mb_type |= MB_TYPE_INTERLACED;
- tprintf(s->avctx, "mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08xn", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
- if(h->direct_spatial_mv_pred){
- int ref[2];
- int mv[2][2];
- int list;
- /* FIXME interlacing + spatial direct uses wrong colocated block positions */
- /* ref = min(neighbors) */
- for(list=0; list<2; list++){
- int refa = h->ref_cache[list][scan8[0] - 1];
- int refb = h->ref_cache[list][scan8[0] - 8];
- int refc = h->ref_cache[list][scan8[0] - 8 + 4];
- if(refc == -2)
- refc = h->ref_cache[list][scan8[0] - 8 - 1];
- ref[list] = refa;
- if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
- ref[list] = refb;
- if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
- ref[list] = refc;
- if(ref[list] < 0)
- ref[list] = -1;
- }
- if(ref[0] < 0 && ref[1] < 0){
- ref[0] = ref[1] = 0;
- mv[0][0] = mv[0][1] =
- mv[1][0] = mv[1][1] = 0;
- }else{
- for(list=0; list<2; list++){
- if(ref[list] >= 0)
- pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
- else
- mv[list][0] = mv[list][1] = 0;
- }
- }
- if(ref[1] < 0){
- if(!is_b8x8)
- *mb_type &= ~MB_TYPE_L1;
- sub_mb_type &= ~MB_TYPE_L1;
- }else if(ref[0] < 0){
- if(!is_b8x8)
- *mb_type &= ~MB_TYPE_L0;
- sub_mb_type &= ~MB_TYPE_L0;
- }
- if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
- int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
- int mb_types_col[2];
- int b8_stride = h->b8_stride;
- int b4_stride = h->b_stride;
- *mb_type = (*mb_type & ~MB_TYPE_16x16) | MB_TYPE_8x8;
- if(IS_INTERLACED(*mb_type)){
- mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
- mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
- if(s->mb_y&1){
- l1ref0 -= 2*b8_stride;
- l1ref1 -= 2*b8_stride;
- l1mv0 -= 4*b4_stride;
- l1mv1 -= 4*b4_stride;
- }
- b8_stride *= 3;
- b4_stride *= 6;
- }else{
- int cur_poc = s->current_picture_ptr->poc;
- int *col_poc = h->ref_list[1]->field_poc;
- int col_parity = FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc);
- int dy = 2*col_parity - (s->mb_y&1);
- mb_types_col[0] =
- mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy + col_parity*s->mb_stride];
- l1ref0 += dy*b8_stride;
- l1ref1 += dy*b8_stride;
- l1mv0 += 2*dy*b4_stride;
- l1mv1 += 2*dy*b4_stride;
- b8_stride = 0;
- }
- for(i8=0; i8<4; i8++){
- int x8 = i8&1;
- int y8 = i8>>1;
- int xy8 = x8+y8*b8_stride;
- int xy4 = 3*x8+y8*b4_stride;
- int a=0, b=0;
- if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
- continue;
- h->sub_mb_type[i8] = sub_mb_type;
- fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
- fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
- if(!IS_INTRA(mb_types_col[y8])
- && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
- || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
- if(ref[0] > 0)
- a= pack16to32(mv[0][0],mv[0][1]);
- if(ref[1] > 0)
- b= pack16to32(mv[1][0],mv[1][1]);
- }else{
- a= pack16to32(mv[0][0],mv[0][1]);
- b= pack16to32(mv[1][0],mv[1][1]);
- }
- fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
- fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
- }
- }else if(IS_16X16(*mb_type)){
- int a=0, b=0;
- fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
- fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
- if(!IS_INTRA(mb_type_col)
- && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
- || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
- && (h->x264_build>33 || !h->x264_build)))){
- if(ref[0] > 0)
- a= pack16to32(mv[0][0],mv[0][1]);
- if(ref[1] > 0)
- b= pack16to32(mv[1][0],mv[1][1]);
- }else{
- a= pack16to32(mv[0][0],mv[0][1]);
- b= pack16to32(mv[1][0],mv[1][1]);
- }
- fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
- fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
- }else{
- for(i8=0; i8<4; i8++){
- const int x8 = i8&1;
- const int y8 = i8>>1;
- if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
- continue;
- h->sub_mb_type[i8] = sub_mb_type;
- fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
- fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
- fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
- fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
- /* col_zero_flag */
- if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
- || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
- && (h->x264_build>33 || !h->x264_build)))){
- const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
- if(IS_SUB_8X8(sub_mb_type)){
- const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
- if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
- if(ref[0] == 0)
- fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
- if(ref[1] == 0)
- fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
- }
- }else
- for(i4=0; i4<4; i4++){
- const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
- if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
- if(ref[0] == 0)
- *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
- if(ref[1] == 0)
- *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
- }
- }
- }
- }
- }
- }else{ /* direct temporal mv pred */
- const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
- const int *dist_scale_factor = h->dist_scale_factor;
- if(FRAME_MBAFF){
- if(IS_INTERLACED(*mb_type)){
- map_col_to_list0[0] = h->map_col_to_list0_field[0];
- map_col_to_list0[1] = h->map_col_to_list0_field[1];
- dist_scale_factor = h->dist_scale_factor_field;
- }
- if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
- /* FIXME assumes direct_8x8_inference == 1 */
- const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
- int mb_types_col[2];
- int y_shift;
- *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
- | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
- | (*mb_type & MB_TYPE_INTERLACED);
- sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
- if(IS_INTERLACED(*mb_type)){
- /* frame to field scaling */
- mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
- mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
- if(s->mb_y&1){
- l1ref0 -= 2*h->b8_stride;
- l1ref1 -= 2*h->b8_stride;
- l1mv0 -= 4*h->b_stride;
- l1mv1 -= 4*h->b_stride;
- }
- y_shift = 0;
- if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
- && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
- && !is_b8x8)
- *mb_type |= MB_TYPE_16x8;
- else
- *mb_type |= MB_TYPE_8x8;
- }else{
- /* field to frame scaling */
- /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
- * but in MBAFF, top and bottom POC are equal */
- int dy = (s->mb_y&1) ? 1 : 2;
- mb_types_col[0] =
- mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
- l1ref0 += dy*h->b8_stride;
- l1ref1 += dy*h->b8_stride;
- l1mv0 += 2*dy*h->b_stride;
- l1mv1 += 2*dy*h->b_stride;
- y_shift = 2;
- if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
- && !is_b8x8)
- *mb_type |= MB_TYPE_16x16;
- else
- *mb_type |= MB_TYPE_8x8;
- }
- for(i8=0; i8<4; i8++){
- const int x8 = i8&1;
- const int y8 = i8>>1;
- int ref0, scale;
- const int16_t (*l1mv)[2]= l1mv0;
- if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
- continue;
- h->sub_mb_type[i8] = sub_mb_type;
- fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
- if(IS_INTRA(mb_types_col[y8])){
- fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
- fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
- fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
- continue;
- }
- ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
- if(ref0 >= 0)
- ref0 = map_col_to_list0[0][ref0*2>>y_shift];
- else{
- ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
- l1mv= l1mv1;
- }
- scale = dist_scale_factor[ref0];
- fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
- {
- const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
- int my_col = (mv_col[1]<<y_shift)/2;
- int mx = (scale * mv_col[0] + 128) >> 8;
- int my = (scale * my_col + 128) >> 8;
- fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
- fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
- }
- }
- return;
- }
- }
- /* one-to-one mv scaling */
- if(IS_16X16(*mb_type)){
- int ref, mv0, mv1;
- fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
- if(IS_INTRA(mb_type_col)){
- ref=mv0=mv1=0;
- }else{
- const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
- : map_col_to_list0[1][l1ref1[0]];
- const int scale = dist_scale_factor[ref0];
- const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
- int mv_l0[2];
- mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
- mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
- ref= ref0;
- mv0= pack16to32(mv_l0[0],mv_l0[1]);
- mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
- }
- fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
- fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
- fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
- }else{
- for(i8=0; i8<4; i8++){
- const int x8 = i8&1;
- const int y8 = i8>>1;
- int ref0, scale;
- const int16_t (*l1mv)[2]= l1mv0;
- if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
- continue;
- h->sub_mb_type[i8] = sub_mb_type;
- fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
- if(IS_INTRA(mb_type_col)){
- fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
- fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
- fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
- continue;
- }
- ref0 = l1ref0[x8 + y8*h->b8_stride];
- if(ref0 >= 0)
- ref0 = map_col_to_list0[0][ref0];
- else{
- ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
- l1mv= l1mv1;
- }
- scale = dist_scale_factor[ref0];
- fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
- if(IS_SUB_8X8(sub_mb_type)){
- const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
- int mx = (scale * mv_col[0] + 128) >> 8;
- int my = (scale * mv_col[1] + 128) >> 8;
- fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
- fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
- }else
- for(i4=0; i4<4; i4++){
- const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
- int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
- mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
- mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
- *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
- pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
- }
- }
- }
- }
- }
- static inline void write_back_motion(H264Context *h, int mb_type){
- MpegEncContext * const s = &h->s;
- const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
- const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
- int list;
- if(!USES_LIST(mb_type, 0))
- fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
- for(list=0; list<h->list_count; list++){
- int y;
- if(!USES_LIST(mb_type, list))
- continue;
- for(y=0; y<4; y++){
- *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
- *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
- }
- if( h->pps.cabac ) {
- if(IS_SKIP(mb_type))
- fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
- else
- for(y=0; y<4; y++){
- *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
- *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
- }
- }
- {
- int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
- ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
- ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
- ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
- ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
- }
- }
- if(h->slice_type == FF_B_TYPE && h->pps.cabac){
- if(IS_8X8(mb_type)){
- uint8_t *direct_table = &h->direct_table[b8_xy];
- direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
- direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
- direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
- }
- }
- }
- /**
- * Decodes a network abstraction layer unit.
- * @param consumed is the number of bytes used as input
- * @param length is the length of the array
- * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
- * @returns decoded bytes, might be src+1 if no escapes
- */
- static const uint8_t *decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
- int i, si, di;
- uint8_t *dst;
- int bufidx;
- // src[0]&0x80; //forbidden bit
- h->nal_ref_idc= src[0]>>5;
- h->nal_unit_type= src[0]&0x1F;
- src++; length--;
- #if 0
- for(i=0; i<length; i++)
- printf("%2X ", src[i]);
- #endif
- for(i=0; i+1<length; i+=2){
- if(src[i]) continue;
- if(i>0 && src[i-1]==0) i--;
- if(i+2<length && src[i+1]==0 && src[i+2]<=3){
- if(src[i+2]!=3){
- /* startcode, so we must be past the end */
- length=i;
- }
- break;
- }
- }
- if(i>=length-1){ //no escaped 0
- *dst_length= length;
- *consumed= length+1; //+1 for the header
- return src;
- }
- bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
- h->rbsp_buffer[bufidx]= av_fast_realloc(h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length);
- dst= h->rbsp_buffer[bufidx];
- if (dst == NULL){
- return NULL;
- }
- //printf("decoding escn");
- si=di=0;
- while(si<length){
- //remove escapes (very rare 1:2^22)
- if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
- if(src[si+2]==3){ //escape
- dst[di++]= 0;
- dst[di++]= 0;
- si+=3;
- continue;
- }else //next start code
- break;
- }
- dst[di++]= src[si++];
- }
- *dst_length= di;
- *consumed= si + 1;//+1 for the header
- //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
- return dst;
- }
- /**
- * identifies the exact end of the bitstream
- * @return the length of the trailing, or 0 if damaged
- */
- static int decode_rbsp_trailing(H264Context *h, const uint8_t *src){
- int v= *src;
- int r;
- tprintf(h->s.avctx, "rbsp trailing %dn", v);
- for(r=1; r<9; r++){
- if(v&1) return r;
- v>>=1;
- }
- return 0;
- }
- /**
- * idct tranforms the 16 dc values and dequantize them.
- * @param qp quantization parameter
- */
- static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
- #define stride 16
- int i;
- int temp[16]; //FIXME check if this is a good idea
- static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
- static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
- //memset(block, 64, 2*256);
- //return;
- for(i=0; i<4; i++){
- const int offset= y_offset[i];
- const int z0= block[offset+stride*0] + block[offset+stride*4];
- const int z1= block[offset+stride*0] - block[offset+stride*4];
- const int z2= block[offset+stride*1] - block[offset+stride*5];
- const int z3= block[offset+stride*1] + block[offset+stride*5];
- temp[4*i+0]= z0+z3;
- temp[4*i+1]= z1+z2;
- temp[4*i+2]= z1-z2;
- temp[4*i+3]= z0-z3;
- }
- for(i=0; i<4; i++){
- const int offset= x_offset[i];
- const int z0= temp[4*0+i] + temp[4*2+i];
- const int z1= temp[4*0+i] - temp[4*2+i];
- const int z2= temp[4*1+i] - temp[4*3+i];
- const int z3= temp[4*1+i] + temp[4*3+i];
- block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
- block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
- block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
- block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
- }
- }
- #if 0
- /**
- * dct tranforms the 16 dc values.
- * @param qp quantization parameter ??? FIXME
- */
- static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
- // const int qmul= dequant_coeff[qp][0];
- int i;
- int temp[16]; //FIXME check if this is a good idea
- static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
- static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
- for(i=0; i<4; i++){
- const int offset= y_offset[i];
- const int z0= block[offset+stride*0] + block[offset+stride*4];
- const int z1= block[offset+stride*0] - block[offset+stride*4];
- const int z2= block[offset+stride*1] - block[offset+stride*5];
- const int z3= block[offset+stride*1] + block[offset+stride*5];
- temp[4*i+0]= z0+z3;
- temp[4*i+1]= z1+z2;
- temp[4*i+2]= z1-z2;
- temp[4*i+3]= z0-z3;
- }
- for(i=0; i<4; i++){
- const int offset= x_offset[i];
- const int z0= temp[4*0+i] + temp[4*2+i];
- const int z1= temp[4*0+i] - temp[4*2+i];
- const int z2= temp[4*1+i] - temp[4*3+i];
- const int z3= temp[4*1+i] + temp[4*3+i];
- block[stride*0 +offset]= (z0 + z3)>>1;
- block[stride*2 +offset]= (z1 + z2)>>1;
- block[stride*8 +offset]= (z1 - z2)>>1;
- block[stride*10+offset]= (z0 - z3)>>1;
- }
- }
- #endif
- #undef xStride
- #undef stride
- static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
- const int stride= 16*2;
- const int xStride= 16;
- int a,b,c,d,e;
- a= block[stride*0 + xStride*0];
- b= block[stride*0 + xStride*1];
- c= block[stride*1 + xStride*0];
- d= block[stride*1 + xStride*1];
- e= a-b;
- a= a+b;
- b= c-d;
- c= c+d;
- block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
- block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
- block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
- block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
- }
- #if 0
- static void chroma_dc_dct_c(DCTELEM *block){
- const int stride= 16*2;
- const int xStride= 16;
- int a,b,c,d,e;
- a= block[stride*0 + xStride*0];
- b= block[stride*0 + xStride*1];
- c= block[stride*1 + xStride*0];
- d= block[stride*1 + xStride*1];
- e= a-b;
- a= a+b;
- b= c-d;
- c= c+d;
- block[stride*0 + xStride*0]= (a+c);
- block[stride*0 + xStride*1]= (e+b);
- block[stride*1 + xStride*0]= (a-c);
- block[stride*1 + xStride*1]= (e-b);
- }
- #endif
- /**
- * gets the chroma qp.
- */
- static inline int get_chroma_qp(H264Context *h, int t, int qscale){
- return h->pps.chroma_qp_table[t][qscale & 0xff];
- }
- //FIXME need to check that this does not overflow signed 32 bit for low qp, I am not sure, it's very close
- //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
- static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
- int i;
- const int * const quant_table= quant_coeff[qscale];
- const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
- const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
- const unsigned int threshold2= (threshold1<<1);
- int last_non_zero;
- if(separate_dc){
- if(qscale<=18){
- //avoid overflows
- const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
- const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
- const unsigned int dc_threshold2= (dc_threshold1<<1);
- int level= block[0]*quant_coeff[qscale+18][0];
- if(((unsigned)(level+dc_threshold1))>dc_threshold2){
- if(level>0){
- level= (dc_bias + level)>>(QUANT_SHIFT-2);
- block[0]= level;
- }else{
- level= (dc_bias - level)>>(QUANT_SHIFT-2);
- block[0]= -level;
- }
- // last_non_zero = i;
- }else{
- block[0]=0;
- }
- }else{
- const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
- const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
- const unsigned int dc_threshold2= (dc_threshold1<<1);
- int level= block[0]*quant_table[0];
- if(((unsigned)(level+dc_threshold1))>dc_threshold2){
- if(level>0){
- level= (dc_bias + level)>>(QUANT_SHIFT+1);
- block[0]= level;
- }else{
- level= (dc_bias - level)>>(QUANT_SHIFT+1);
- block[0]= -level;
- }
- // last_non_zero = i;
- }else{
- block[0]=0;
- }
- }
- last_non_zero= 0;
- i=1;
- }else{
- last_non_zero= -1;
- i=0;
- }
- for(; i<16; i++){
- const int j= scantable[i];
- int level= block[j]*quant_table[j];
- // if( bias+level >= (1<<(QMAT_SHIFT - 3))
- // || bias-level >= (1<<(QMAT_SHIFT - 3))){
- if(((unsigned)(level+threshold1))>threshold2){
- if(level>0){
- level= (bias + level)>>QUANT_SHIFT;
- block[j]= level;
- }else{
- level= (bias - level)>>QUANT_SHIFT;
- block[j]= -level;
- }
- last_non_zero = i;
- }else{
- block[j]=0;
- }
- }
- return last_non_zero;
- }
- static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
- uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
- int src_x_offset, int src_y_offset,
- qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
- MpegEncContext * const s = &h->s;
- const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
- int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
- const int luma_xy= (mx&3) + ((my&3)<<2);
- uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
- uint8_t * src_cb, * src_cr;
- int extra_width= h->emu_edge_width;
- int extra_height= h->emu_edge_height;
- int emu=0;
- const int full_mx= mx>>2;
- const int full_my= my>>2;
- const int pic_width = 16*s->mb_width;
- const int pic_height = 16*s->mb_height >> MB_FIELD;
- if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
- return;
- if(mx&7) extra_width -= 3;
- if(my&7) extra_height -= 3;
- if( full_mx < 0-extra_width
- || full_my < 0-extra_height
- || full_mx + 16/*FIXME*/ > pic_width + extra_width
- || full_my + 16/*FIXME*/ > pic_height + extra_height){
- ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
- src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
- emu=1;
- }
- qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
- if(!square){
- qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
- }
- if(ENABLE_GRAY && s->flags&CODEC_FLAG_GRAY) return;
- if(MB_FIELD){
- // chroma offset when predicting from a field of opposite parity
- my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
- emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
- }
- src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
- src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
- if(emu){
- ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
- src_cb= s->edge_emu_buffer;
- }
- chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
- if(emu){
- ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
- src_cr= s->edge_emu_buffer;
- }
- chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
- }
- static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
- uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
- int x_offset, int y_offset,
- qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
- qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
- int list0, int list1){
- MpegEncContext * const s = &h->s;
- qpel_mc_func *qpix_op= qpix_put;
- h264_chroma_mc_func chroma_op= chroma_put;
- dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
- dest_cb += x_offset + y_offset*h->mb_uvlinesize;
- dest_cr += x_offset + y_offset*h->mb_uvlinesize;
- x_offset += 8*s->mb_x;
- y_offset += 8*(s->mb_y >> MB_FIELD);
- if(list0){
- Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
- mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
- dest_y, dest_cb, dest_cr, x_offset, y_offset,
- qpix_op, chroma_op);
- qpix_op= qpix_avg;
- chroma_op= chroma_avg;
- }
- if(list1){
- Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
- mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
- dest_y, dest_cb, dest_cr, x_offset, y_offset,
- qpix_op, chroma_op);
- }
- }
- static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
- uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
- int x_offset, int y_offset,
- qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
- h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
- h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
- int list0, int list1){
- MpegEncContext * const s = &h->s;
- dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
- dest_cb += x_offset + y_offset*h->mb_uvlinesize;
- dest_cr += x_offset + y_offset*h->mb_uvlinesize;
- x_offset += 8*s->mb_x;
- y_offset += 8*(s->mb_y >> MB_FIELD);
- if(list0 && list1){
- /* don't optimize for luma-only case, since B-frames usually
- * use implicit weights => chroma too. */
- uint8_t *tmp_cb = s->obmc_scratchpad;
- uint8_t *tmp_cr = s->obmc_scratchpad + 8;
- uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
- int refn0 = h->ref_cache[0][ scan8[n] ];
- int refn1 = h->ref_cache[1][ scan8[n] ];
- mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
- dest_y, dest_cb, dest_cr,
- x_offset, y_offset, qpix_put, chroma_put);
- mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
- tmp_y, tmp_cb, tmp_cr,
- x_offset, y_offset, qpix_put, chroma_put);
- if(h->use_weight == 2){
- int weight0 = h->implicit_weight[refn0][refn1];
- int weight1 = 64 - weight0;
- luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
- chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
- chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
- }else{
- luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
- h->luma_weight[0][refn0], h->luma_weight[1][refn1],
- h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
- chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
- h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
- h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
- chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
- h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
- h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
- }
- }else{
- int list = list1 ? 1 : 0;
- int refn = h->ref_cache[list][ scan8[n] ];
- Picture *ref= &h->ref_list[list][refn];
- mc_dir_part(h, ref, n, square, chroma_height, delta, list,
- dest_y, dest_cb, dest_cr, x_offset, y_offset,
- qpix_put, chroma_put);
- luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
- h->luma_weight[list][refn], h->luma_offset[list][refn]);
- if(h->use_weight_chroma){
- chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
- h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
- chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
- h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
- }
- }
- }
- static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
- uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
- int x_offset, int y_offset,
- qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
- qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
- h264_weight_func *weight_op, h264_biweight_func *weight_avg,
- int list0, int list1){
- if((h->use_weight==2 && list0 && list1
- && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
- || h->use_weight==1)
- mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
- x_offset, y_offset, qpix_put, chroma_put,
- weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
- else
- mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
- x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
- }
- static inline void prefetch_motion(H264Context *h, int list){
- /* fetch pixels for estimated mv 4 macroblocks ahead
- * optimized for 64byte cache lines */
- MpegEncContext * const s = &h->s;
- const int refn = h->ref_cache[list][scan8[0]];
- if(refn >= 0){
- const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
- const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
- uint8_t **src= h->ref_list[list][refn].data;
- int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
- s->dsp.prefetch(src[0]+off, s->linesize, 4);
- off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
- s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
- }
- }
- static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
- qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
- qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
- h264_weight_func *weight_op, h264_biweight_func *weight_avg){
- MpegEncContext * const s = &h->s;
- const int mb_xy= h->mb_xy;
- const int mb_type= s->current_picture.mb_type[mb_xy];
- assert(IS_INTER(mb_type));
- prefetch_motion(h, 0);
- if(IS_16X16(mb_type)){
- mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
- qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
- &weight_op[0], &weight_avg[0],
- IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
- }else if(IS_16X8(mb_type)){
- mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
- qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
- &weight_op[1], &weight_avg[1],
- IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
- mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
- qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
- &weight_op[1], &weight_avg[1],
- IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
- }else if(IS_8X16(mb_type)){
- mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
- qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
- &weight_op[2], &weight_avg[2],
- IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
- mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
- qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
- &weight_op[2], &weight_avg[2],
- IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
- }else{
- int i;
- assert(IS_8X8(mb_type));
- for(i=0; i<4; i++){
- const int sub_mb_type= h->sub_mb_type[i];
- const int n= 4*i;
- int x_offset= (i&1)<<2;
- int y_offset= (i&2)<<1;
- if(IS_SUB_8X8(sub_mb_type)){
- mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
- qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
- &weight_op[3], &weight_avg[3],
- IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
- }else if(IS_SUB_8X4(sub_mb_type)){
- mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
- qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
- &weight_op[4], &weight_avg[4],
- IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
- mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
- qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
- &weight_op[4], &weight_avg[4],
- IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
- }else if(IS_SUB_4X8(sub_mb_type)){
- mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
- qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
- &weight_op[5], &weight_avg[5],
- IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
- mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
- qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
- &weight_op[5], &weight_avg[5],
- IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
- }else{
- int j;
- assert(IS_SUB_4X4(sub_mb_type));
- for(j=0; j<4; j++){
- int sub_x_offset= x_offset + 2*(j&1);
- int sub_y_offset= y_offset + (j&2);
- mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
- qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
- &weight_op[6], &weight_avg[6],
- IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
- }
- }
- }
- }
- prefetch_motion(h, 1);
- }
- static av_cold void decode_init_vlc(void){
- static int done = 0;
- if (!done) {
- int i;
- done = 1;
- init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
- &chroma_dc_coeff_token_len [0], 1, 1,
- &chroma_dc_coeff_token_bits[0], 1, 1, 1);
- for(i=0; i<4; i++){
- init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
- &coeff_token_len [i][0], 1, 1,
- &coeff_token_bits[i][0], 1, 1, 1);
- }
- for(i=0; i<3; i++){
- init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
- &chroma_dc_total_zeros_len [i][0], 1, 1,
- &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
- }
- for(i=0; i<15; i++){
- init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
- &total_zeros_len [i][0], 1, 1,
- &total_zeros_bits[i][0], 1, 1, 1);
- }
- for(i=0; i<6; i++){
- init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
- &run_len [i][0], 1, 1,
- &run_bits[i][0], 1, 1, 1);
- }
- init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
- &run_len [6][0], 1, 1,
- &run_bits[6][0], 1, 1, 1);
- }
- }
- static void free_tables(H264Context *h){
- int i;
- H264Context *hx;
- av_freep(&h->intra4x4_pred_mode);
- av_freep(&h->chroma_pred_mode_table);
- av_freep(&h->cbp_table);
- av_freep(&h->mvd_table[0]);
- av_freep(&h->mvd_table[1]);
- av_freep(&h->direct_table);
- av_freep(&h->non_zero_count);
- av_freep(&h->slice_table_base);
- h->slice_table= NULL;
- av_freep(&h->mb2b_xy);
- av_freep(&h->mb2b8_xy);
- for(i = 0; i < MAX_SPS_COUNT; i++)
- av_freep(h->sps_buffers + i);
- for(i = 0; i < MAX_PPS_COUNT; i++)
- av_freep(h->pps_buffers + i);
- for(i = 0; i < h->s.avctx->thread_count; i++) {
- hx = h->thread_context[i];
- if(!hx) continue;
- av_freep(&hx->top_borders[1]);
- av_freep(&hx->top_borders[0]);
- av_freep(&hx->s.obmc_scratchpad);
- }
- }
- static void init_dequant8_coeff_table(H264Context *h){
- int i,q,x;
- const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
- h->dequant8_coeff[0] = h->dequant8_buffer[0];
- h->dequant8_coeff[1] = h->dequant8_buffer[1];
- for(i=0; i<2; i++ ){
- if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
- h->dequant8_coeff[1] = h->dequant8_buffer[0];
- break;
- }
- for(q=0; q<52; q++){
- int shift = ff_div6[q];
- int idx = ff_rem6[q];
- for(x=0; x<64; x++)
- h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
- ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
- h->pps.scaling_matrix8[i][x]) << shift;
- }
- }
- }
- static void init_dequant4_coeff_table(H264Context *h){
- int i,j,q,x;
- const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
- for(i=0; i<6; i++ ){
- h->dequant4_coeff[i] = h->dequant4_buffer[i];
- for(j=0; j<i; j++){
- if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
- h->dequant4_coeff[i] = h->dequant4_buffer[j];
- break;
- }
- }
- if(j<i)
- continue;
- for(q=0; q<52; q++){
- int shift = ff_div6[q] + 2;
- int idx = ff_rem6[q];
- for(x=0; x<16; x++)
- h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
- ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
- h->pps.scaling_matrix4[i][x]) << shift;
- }
- }
- }
- static void init_dequant_tables(H264Context *h){
- int i,x;
- init_dequant4_coeff_table(h);
- if(h->pps.transform_8x8_mode)
- init_dequant8_coeff_table(h);
- if(h->sps.transform_bypass){
- for(i=0; i<6; i++)
- for(x=0; x<16; x++)
- h->dequant4_coeff[i][0][x] = 1<<6;
- if(h->pps.transform_8x8_mode)
- for(i=0; i<2; i++)
- for(x=0; x<64; x++)
- h->dequant8_coeff[i][0][x] = 1<<6;
- }
- }
- /**
- * allocates tables.
- * needs width/height
- */
- static int alloc_tables(H264Context *h){
- MpegEncContext * const s = &h->s;
- const int big_mb_num= s->mb_stride * (s->mb_height+1);
- int x,y;
- CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
- CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
- CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
- CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
- CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
- CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
- CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
- CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
- memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
- h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
- CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
- CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
- for(y=0; y<s->mb_height; y++){
- for(x=0; x<s->mb_width; x++){
- const int mb_xy= x + y*s->mb_stride;
- const int b_xy = 4*x + 4*y*h->b_stride;
- const int b8_xy= 2*x + 2*y*h->b8_stride;
- h->mb2b_xy [mb_xy]= b_xy;
- h->mb2b8_xy[mb_xy]= b8_xy;
- }
- }
- s->obmc_scratchpad = NULL;
- if(!h->dequant4_coeff[0])
- init_dequant_tables(h);
- return 0;
- fail:
- free_tables(h);
- return -1;
- }
- /**
- * Mimic alloc_tables(), but for every context thread.
- */
- static void clone_tables(H264Context *dst, H264Context *src){
- dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
- dst->non_zero_count = src->non_zero_count;
- dst->slice_table = src->slice_table;
- dst->cbp_table = src->cbp_table;
- dst->mb2b_xy = src->mb2b_xy;
- dst->mb2b8_xy = src->mb2b8_xy;
- dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
- dst->mvd_table[0] = src->mvd_table[0];
- dst->mvd_table[1] = src->mvd_table[1];
- dst->direct_table = src->direct_table;
- dst->s.obmc_scratchpad = NULL;
- ff_h264_pred_init(&dst->hpc, src->s.codec_id);
- }
- /**
- * Init context
- * Allocate buffers which are not shared amongst multiple threads.
- */
- static int context_init(H264Context *h){
- CHECKED_ALLOCZ(h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
- CHECKED_ALLOCZ(h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
- return 0;
- fail:
- return -1; // free_tables will clean up for us
- }
- static av_cold void common_init(H264Context *h){
- MpegEncContext * const s = &h->s;
- s->width = s->avctx->width;
- s->height = s->avctx->height;
- s->codec_id= s->avctx->codec->id;
- ff_h264_pred_init(&h->hpc, s->codec_id);
- h->dequant_coeff_pps= -1;
- s->unrestricted_mv=1;
- s->decode=1; //FIXME
- memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
- memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
- }
- static av_cold int decode_init(AVCodecContext *avctx){
- H264Context *h= avctx->priv_data;
- MpegEncContext * const s = &h->s;
- MPV_decode_defaults(s);
- s->avctx = avctx;
- common_init(h);
- s->out_format = FMT_H264;
- s->workaround_bugs= avctx->workaround_bugs;
- // set defaults
- // s->decode_mb= ff_h263_decode_mb;
- s->quarter_sample = 1;
- s->low_delay= 1;
- avctx->pix_fmt= PIX_FMT_YUV420P;
- decode_init_vlc();
- if(avctx->extradata_size > 0 && avctx->extradata &&
- *(char *)avctx->extradata == 1){
- h->is_avc = 1;
- h->got_avcC = 0;
- } else {
- h->is_avc = 0;
- }
- h->thread_context[0] = h;
- return 0;
- }
- static int frame_start(H264Context *h){
- MpegEncContext * const s = &h->s;
- int i;
- if(MPV_frame_start(s, s->avctx) < 0)
- return -1;
- ff_er_frame_start(s);
- /*
- * MPV_frame_start uses pict_type to derive key_frame.
- * This is incorrect for H.264; IDR markings must be used.
- * Zero here; IDR markings per slice in frame or fields are OR'd in later.
- * See decode_nal_units().
- */
- s->current_picture_ptr->key_frame= 0;
- assert(s->linesize && s->uvlinesize);
- for(i=0; i<16; i++){
- h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
- h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
- }
- for(i=0; i<4; i++){
- h->block_offset[16+i]=
- h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
- h->block_offset[24+16+i]=
- h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
- }
- /* can't be in alloc_tables because linesize isn't known there.
- * FIXME: redo bipred weight to not require extra buffer? */
- for(i = 0; i < s->avctx->thread_count; i++)
- if(!h->thread_context[i]->s.obmc_scratchpad)
- h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
- /* some macroblocks will be accessed before they're available */
- if(FRAME_MBAFF || s->avctx->thread_count > 1)
- memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
- // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
- return 0;
- }
- static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
- MpegEncContext * const s = &h->s;
- int i;
- src_y -= linesize;
- src_cb -= uvlinesize;
- src_cr -= uvlinesize;
- // There are two lines saved, the line above the the top macroblock of a pair,
- // and the line above the bottom macroblock
- h->left_border[0]= h->top_borders[0][s->mb_x][15];
- for(i=1; i<17; i++){
- h->left_border[i]= src_y[15+i* linesize];
- }
- *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
- *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
- if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
- h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
- h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
- for(i=1; i<9; i++){
- h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
- h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
- }
- *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
- *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
- }
- }
- static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
- MpegEncContext * const s = &h->s;
- int temp8, i;
- uint64_t temp64;
- int deblock_left;
- int deblock_top;
- int mb_xy;
- if(h->deblocking_filter == 2) {
- mb_xy = h->mb_xy;
- deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
- deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
- } else {
- deblock_left = (s->mb_x > 0);
- deblock_top = (s->mb_y > 0);
- }
- src_y -= linesize + 1;
- src_cb -= uvlinesize + 1;
- src_cr -= uvlinesize + 1;
- #define XCHG(a,b,t,xchg)
- t= a;
- if(xchg)
- a= b;
- b= t;
- if(deblock_left){
- for(i = !deblock_top; i<17; i++){
- XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
- }
- }
- if(deblock_top){
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
- if(s->mb_x+1 < s->mb_width){
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
- }
- }
- if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
- if(deblock_left){
- for(i = !deblock_top; i<9; i++){
- XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
- XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
- }
- }
- if(deblock_top){
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
- }
- }
- }
- static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
- MpegEncContext * const s = &h->s;
- int i;
- src_y -= 2 * linesize;
- src_cb -= 2 * uvlinesize;
- src_cr -= 2 * uvlinesize;
- // There are two lines saved, the line above the the top macroblock of a pair,
- // and the line above the bottom macroblock
- h->left_border[0]= h->top_borders[0][s->mb_x][15];
- h->left_border[1]= h->top_borders[1][s->mb_x][15];
- for(i=2; i<34; i++){
- h->left_border[i]= src_y[15+i* linesize];
- }
- *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
- *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
- *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
- *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
- if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
- h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
- h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
- h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
- h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
- for(i=2; i<18; i++){
- h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
- h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
- }
- *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
- *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
- *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
- *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
- }
- }
- static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
- MpegEncContext * const s = &h->s;
- int temp8, i;
- uint64_t temp64;
- int deblock_left = (s->mb_x > 0);
- int deblock_top = (s->mb_y > 1);
- tprintf(s->avctx, "xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%dn", src_y, src_cb, src_cr, linesize, uvlinesize);
- src_y -= 2 * linesize + 1;
- src_cb -= 2 * uvlinesize + 1;
- src_cr -= 2 * uvlinesize + 1;
- #define XCHG(a,b,t,xchg)
- t= a;
- if(xchg)
- a= b;
- b= t;
- if(deblock_left){
- for(i = (!deblock_top)<<1; i<34; i++){
- XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
- }
- }
- if(deblock_top){
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
- XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
- XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
- if(s->mb_x+1 < s->mb_width){
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
- XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
- }
- }
- if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
- if(deblock_left){
- for(i = (!deblock_top) << 1; i<18; i++){
- XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
- XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
- }
- }
- if(deblock_top){
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
- XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
- XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
- XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
- }
- }
- }
- static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
- MpegEncContext * const s = &h->s;
- const int mb_x= s->mb_x;
- const int mb_y= s->mb_y;
- const int mb_xy= h->mb_xy;
- const int mb_type= s->current_picture.mb_type[mb_xy];
- uint8_t *dest_y, *dest_cb, *dest_cr;
- int linesize, uvlinesize /*dct_offset*/;
- int i;
- int *block_offset = &h->block_offset[0];
- const unsigned int bottom = mb_y & 1;
- const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
- void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
- void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
- dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
- dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
- dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
- s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
- s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
- if (!simple && MB_FIELD) {
- linesize = h->mb_linesize = s->linesize * 2;
- uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
- block_offset = &h->block_offset[24];
- if(mb_y&1){ //FIXME move out of this func?
- dest_y -= s->linesize*15;
- dest_cb-= s->uvlinesize*7;
- dest_cr-= s->uvlinesize*7;
- }
- if(FRAME_MBAFF) {
- int list;
- for(list=0; list<h->list_count; list++){
- if(!USES_LIST(mb_type, list))
- continue;
- if(IS_16X16(mb_type)){
- int8_t *ref = &h->ref_cache[list][scan8[0]];
- fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
- }else{
- for(i=0; i<16; i+=4){
- //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
- int ref = h->ref_cache[list][scan8[i]];
- if(ref >= 0)
- fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
- }
- }
- }
- }
- } else {
- linesize = h->mb_linesize = s->linesize;
- uvlinesize = h->mb_uvlinesize = s->uvlinesize;
- // dct_offset = s->linesize * 16;
- }
- if(transform_bypass){
- idct_dc_add =
- idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
- }else if(IS_8x8DCT(mb_type)){
- idct_dc_add = s->dsp.h264_idct8_dc_add;
- idct_add = s->dsp.h264_idct8_add;
- }else{
- idct_dc_add = s->dsp.h264_idct_dc_add;
- idct_add = s->dsp.h264_idct_add;
- }
- if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
- && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
- int mbt_y = mb_y&~1;
- uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
- uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
- uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
- xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
- }
- if (!simple && IS_INTRA_PCM(mb_type)) {
- unsigned int x, y;
- // The pixels are stored in h->mb array in the same order as levels,
- // copy them in output in the correct order.
- for(i=0; i<16; i++) {
- for (y=0; y<4; y++) {
- for (x=0; x<4; x++) {
- *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
- }
- }
- }
- for(i=16; i<16+4; i++) {
- for (y=0; y<4; y++) {
- for (x=0; x<4; x++) {
- *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
- }
- }
- }
- for(i=20; i<20+4; i++) {
- for (y=0; y<4; y++) {
- for (x=0; x<4; x++) {
- *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
- }
- }
- }
- } else {
- if(IS_INTRA(mb_type)){
- if(h->deblocking_filter && (simple || !FRAME_MBAFF))
- xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
- if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
- h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
- h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
- }
- if(IS_INTRA4x4(mb_type)){
- if(simple || !s->encoding){
- if(IS_8x8DCT(mb_type)){
- for(i=0; i<16; i+=4){
- uint8_t * const ptr= dest_y + block_offset[i];
- const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
- const int nnz = h->non_zero_count_cache[ scan8[i] ];
- h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
- (h->topright_samples_available<<i)&0x4000, linesize);
- if(nnz){
- if(nnz == 1 && h->mb[i*16])
- idct_dc_add(ptr, h->mb + i*16, linesize);
- else
- idct_add(ptr, h->mb + i*16, linesize);
- }
- }
- }else
- for(i=0; i<16; i++){
- uint8_t * const ptr= dest_y + block_offset[i];
- uint8_t *topright;
- const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
- int nnz, tr;
- if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
- const int topright_avail= (h->topright_samples_available<<i)&0x8000;
- assert(mb_y || linesize <= block_offset[i]);
- if(!topright_avail){
- tr= ptr[3 - linesize]*0x01010101;
- topright= (uint8_t*) &tr;
- }else
- topright= ptr + 4 - linesize;
- }else
- topright= NULL;
- h->hpc.pred4x4[ dir ](ptr, topright, linesize);
- nnz = h->non_zero_count_cache[ scan8[i] ];
- if(nnz){
- if(is_h264){
- if(nnz == 1 && h->mb[i*16])
- idct_dc_add(ptr, h->mb + i*16, linesize);
- else
- idct_add(ptr, h->mb + i*16, linesize);
- }else
- svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
- }
- }
- }
- }else{
- h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
- if(is_h264){
- if(!transform_bypass)
- h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
- }else
- svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
- }
- if(h->deblocking_filter && (simple || !FRAME_MBAFF))
- xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
- }else if(is_h264){
- hl_motion(h, dest_y, dest_cb, dest_cr,
- s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
- s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
- s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
- }
- if(!IS_INTRA4x4(mb_type)){
- if(is_h264){
- if(IS_INTRA16x16(mb_type)){
- for(i=0; i<16; i++){
- if(h->non_zero_count_cache[ scan8[i] ])
- idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
- else if(h->mb[i*16])
- idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
- }
- }else{
- const int di = IS_8x8DCT(mb_type) ? 4 : 1;
- for(i=0; i<16; i+=di){
- int nnz = h->non_zero_count_cache[ scan8[i] ];
- if(nnz){
- if(nnz==1 && h->mb[i*16])
- idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
- else
- idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
- }
- }
- }
- }else{
- for(i=0; i<16; i++){
- if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
- uint8_t * const ptr= dest_y + block_offset[i];
- svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
- }
- }
- }
- }
- if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
- uint8_t *dest[2] = {dest_cb, dest_cr};
- if(transform_bypass){
- idct_add = idct_dc_add = s->dsp.add_pixels4;
- }else{
- idct_add = s->dsp.h264_idct_add;
- idct_dc_add = s->dsp.h264_idct_dc_add;
- chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
- chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
- }
- if(is_h264){
- for(i=16; i<16+8; i++){
- if(h->non_zero_count_cache[ scan8[i] ])
- idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
- else if(h->mb[i*16])
- idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
- }
- }else{
- for(i=16; i<16+8; i++){
- if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
- uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
- svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
- }
- }
- }
- }
- }
- if(h->deblocking_filter) {
- if (!simple && FRAME_MBAFF) {
- //FIXME try deblocking one mb at a time?
- // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
- const int mb_y = s->mb_y - 1;
- uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
- const int mb_xy= mb_x + mb_y*s->mb_stride;
- const int mb_type_top = s->current_picture.mb_type[mb_xy];
- const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
- if (!bottom) return;
- pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
- pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
- pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
- if(IS_INTRA(mb_type_top | mb_type_bottom))
- xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
- backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
- // deblock a pair
- // top
- s->mb_y--; h->mb_xy -= s->mb_stride;
- tprintf(h->s.avctx, "call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %pn", mb_x, mb_y, pair_dest_y, dest_y);
- fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
- h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
- h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
- filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
- // bottom
- s->mb_y++; h->mb_xy += s->mb_stride;
- tprintf(h->s.avctx, "call mbaff filter_mbn");
- fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
- h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy+s->mb_stride]);