Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264.c @ 943f69a6

History | View | Annotate | Download (263 KB)

1 0da71265 Michael Niedermayer
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 0da71265 Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 0da71265 Michael Niedermayer
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 0da71265 Michael Niedermayer
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 0da71265 Michael Niedermayer
 */
21 115329f1 Diego Biurrun
22 0da71265 Michael Niedermayer
/**
23 bad5537e Diego Biurrun
 * @file libavcodec/h264.c
24 0da71265 Michael Niedermayer
 * H.264 / AVC / MPEG4 part10 codec.
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27
28 40e5d31b Gwenole Beauchesne
#include "internal.h"
29 0da71265 Michael Niedermayer
#include "dsputil.h"
30
#include "avcodec.h"
31
#include "mpegvideo.h"
32 26b4fe82 Aurelien Jacobs
#include "h264.h"
33 0da71265 Michael Niedermayer
#include "h264data.h"
34 26b4fe82 Aurelien Jacobs
#include "h264_parser.h"
35 0da71265 Michael Niedermayer
#include "golomb.h"
36 199436b9 Aurelien Jacobs
#include "mathops.h"
37 626464fb Kostya Shishkov
#include "rectangle.h"
38 369122dd NVIDIA Corporation
#include "vdpau_internal.h"
39 0da71265 Michael Niedermayer
40 e5017ab8 Laurent Aimar
#include "cabac.h"
41 b250f9c6 Aurelien Jacobs
#if ARCH_X86
42 a6493a8f Diego Biurrun
#include "x86/h264_i386.h"
43 52cb7981 Jeff Downs
#endif
44 e5017ab8 Laurent Aimar
45 2848ce84 Loren Merritt
//#undef NDEBUG
46 0da71265 Michael Niedermayer
#include <assert.h>
47
48 2ddcf84b Jeff Downs
/**
49
 * Value of Picture.reference when Picture is not a reference picture, but
50
 * is held for delayed output.
51
 */
52
#define DELAYED_PIC_REF 4
53
54 0da71265 Michael Niedermayer
static VLC coeff_token_vlc[4];
55 910e3668 Art Clarke
static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2];
56
static const int coeff_token_vlc_tables_size[4]={520,332,280,256};
57
58 0da71265 Michael Niedermayer
static VLC chroma_dc_coeff_token_vlc;
59 910e3668 Art Clarke
static VLC_TYPE chroma_dc_coeff_token_vlc_table[256][2];
60
static const int chroma_dc_coeff_token_vlc_table_size = 256;
61 0da71265 Michael Niedermayer
62
static VLC total_zeros_vlc[15];
63 910e3668 Art Clarke
static VLC_TYPE total_zeros_vlc_tables[15][512][2];
64
static const int total_zeros_vlc_tables_size = 512;
65
66 0da71265 Michael Niedermayer
static VLC chroma_dc_total_zeros_vlc[3];
67 910e3668 Art Clarke
static VLC_TYPE chroma_dc_total_zeros_vlc_tables[3][8][2];
68
static const int chroma_dc_total_zeros_vlc_tables_size = 8;
69 0da71265 Michael Niedermayer
70
static VLC run_vlc[6];
71 910e3668 Art Clarke
static VLC_TYPE run_vlc_tables[6][8][2];
72
static const int run_vlc_tables_size = 8;
73
74 0da71265 Michael Niedermayer
static VLC run7_vlc;
75 910e3668 Art Clarke
static VLC_TYPE run7_vlc_table[96][2];
76
static const int run7_vlc_table_size = 96;
77 0da71265 Michael Niedermayer
78 8b82a956 Michael Niedermayer
static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
79
static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
80 9c0e4624 Michael Niedermayer
static Picture * remove_long(H264Context *h, int i, int ref_mask);
81 8b82a956 Michael Niedermayer
82 d9ec210b Diego Pettenò
static const uint8_t rem6[52]={
83 acd8d10f Panagiotis Issaris
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,
84
};
85
86 d9ec210b Diego Pettenò
static const uint8_t div6[52]={
87 acd8d10f Panagiotis Issaris
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,
88
};
89
90 89818988 Alexander Strange
static const uint8_t left_block_options[4][8]={
91 143d7f14 Paul Kendall
    {0,1,2,3,7,10,8,11},
92
    {2,2,3,3,8,11,8,11},
93
    {0,0,1,1,7,10,7,10},
94
    {0,2,0,2,7,10,7,10}
95
};
96 acd8d10f Panagiotis Issaris
97 8140955d Michael Niedermayer
#define LEVEL_TAB_BITS 8
98
static int8_t cavlc_level_tab[7][1<<LEVEL_TAB_BITS][2];
99
100 70abb407 Loren Merritt
static void fill_caches(H264Context *h, int mb_type, int for_deblock){
101 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
102 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
103 0da71265 Michael Niedermayer
    int topleft_xy, top_xy, topright_xy, left_xy[2];
104
    int topleft_type, top_type, topright_type, left_type[2];
105 89818988 Alexander Strange
    const uint8_t * left_block;
106 02f7695b Loren Merritt
    int topleft_partition= -1;
107 0da71265 Michael Niedermayer
    int i;
108
109 36e097bc Jeff Downs
    top_xy     = mb_xy  - (s->mb_stride << FIELD_PICTURE);
110
111 717b1733 Loren Merritt
    //FIXME deblocking could skip the intra and nnz parts.
112 36e097bc Jeff Downs
    if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)
113 e2e5894a Loren Merritt
        return;
114
115 2cab6401 Diego Biurrun
    /* Wow, what a mess, why didn't they simplify the interlacing & intra
116
     * stuff, I can't imagine that these complex rules are worth it. */
117 115329f1 Diego Biurrun
118 6867a90b Loic Le Loarer
    topleft_xy = top_xy - 1;
119
    topright_xy= top_xy + 1;
120
    left_xy[1] = left_xy[0] = mb_xy-1;
121 143d7f14 Paul Kendall
    left_block = left_block_options[0];
122 5d18eaad Loren Merritt
    if(FRAME_MBAFF){
123 6867a90b Loic Le Loarer
        const int pair_xy          = s->mb_x     + (s->mb_y & ~1)*s->mb_stride;
124
        const int top_pair_xy      = pair_xy     - s->mb_stride;
125
        const int topleft_pair_xy  = top_pair_xy - 1;
126
        const int topright_pair_xy = top_pair_xy + 1;
127 6f3c50f2 Michael Niedermayer
        const int topleft_mb_field_flag  = IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
128
        const int top_mb_field_flag      = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
129
        const int topright_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
130
        const int left_mb_field_flag     = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
131
        const int curr_mb_field_flag     = IS_INTERLACED(mb_type);
132 6867a90b Loic Le Loarer
        const int bottom = (s->mb_y & 1);
133 6f3c50f2 Michael Niedermayer
        tprintf(s->avctx, "fill_caches: curr_mb_field_flag:%d, left_mb_field_flag:%d, topleft_mb_field_flag:%d, top_mb_field_flag:%d, topright_mb_field_flag:%d\n", curr_mb_field_flag, left_mb_field_flag, topleft_mb_field_flag, top_mb_field_flag, topright_mb_field_flag);
134 60c6ba7a Michael Niedermayer
135 6f3c50f2 Michael Niedermayer
        if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
136 6867a90b Loic Le Loarer
            top_xy -= s->mb_stride;
137
        }
138 6f3c50f2 Michael Niedermayer
        if (curr_mb_field_flag && (bottom || topleft_mb_field_flag)){
139 6867a90b Loic Le Loarer
            topleft_xy -= s->mb_stride;
140 6f3c50f2 Michael Niedermayer
        } else if(bottom && !curr_mb_field_flag && left_mb_field_flag) {
141 02f7695b Loren Merritt
            topleft_xy += s->mb_stride;
142 1412060e Diego Biurrun
            // take top left mv from the middle of the mb, as opposed to all other modes which use the bottom right partition
143 02f7695b Loren Merritt
            topleft_partition = 0;
144 6867a90b Loic Le Loarer
        }
145 6f3c50f2 Michael Niedermayer
        if (curr_mb_field_flag && (bottom || topright_mb_field_flag)){
146 6867a90b Loic Le Loarer
            topright_xy -= s->mb_stride;
147
        }
148 6f3c50f2 Michael Niedermayer
        if (left_mb_field_flag != curr_mb_field_flag) {
149 6867a90b Loic Le Loarer
            left_xy[1] = left_xy[0] = pair_xy - 1;
150 6f3c50f2 Michael Niedermayer
            if (curr_mb_field_flag) {
151
                left_xy[1] += s->mb_stride;
152
                left_block = left_block_options[3];
153
            } else {
154 03a035e0 Michael Niedermayer
                left_block= left_block_options[2 - bottom];
155 6867a90b Loic Le Loarer
            }
156
        }
157 0da71265 Michael Niedermayer
    }
158
159 826de46e Loïc Le Loarer
    h->top_mb_xy = top_xy;
160
    h->left_mb_xy[0] = left_xy[0];
161
    h->left_mb_xy[1] = left_xy[1];
162 6ba71fc4 Loïc Le Loarer
    if(for_deblock){
163 717b1733 Loren Merritt
        topleft_type = 0;
164
        topright_type = 0;
165 b735aeea Michael Niedermayer
        top_type     = h->slice_table[top_xy     ] < 0xFFFF ? s->current_picture.mb_type[top_xy]     : 0;
166
        left_type[0] = h->slice_table[left_xy[0] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[0]] : 0;
167
        left_type[1] = h->slice_table[left_xy[1] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[1]] : 0;
168 5d18eaad Loren Merritt
169 e248cb60 Michael Niedermayer
        if(MB_MBAFF && !IS_INTRA(mb_type)){
170 5d18eaad Loren Merritt
            int list;
171 3425501d Michael Niedermayer
            for(list=0; list<h->list_count; list++){
172 e248cb60 Michael Niedermayer
                //These values where changed for ease of performing MC, we need to change them back
173
                //FIXME maybe we can make MC and loop filter use the same values or prevent
174
                //the MC code from changing ref_cache and rather use a temporary array.
175 5d18eaad Loren Merritt
                if(USES_LIST(mb_type,list)){
176 191e8ca7 Måns Rullgård
                    int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
177 5d18eaad Loren Merritt
                    *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
178 beca9a28 Michael Niedermayer
                    *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
179 5d18eaad Loren Merritt
                    ref += h->b8_stride;
180
                    *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
181 beca9a28 Michael Niedermayer
                    *(uint32_t*)&h->ref_cache[list][scan8[10]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
182 5d18eaad Loren Merritt
                }
183
            }
184
        }
185 46f2f05f Michael Niedermayer
    }else{
186
        topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
187
        top_type     = h->slice_table[top_xy     ] == h->slice_num ? s->current_picture.mb_type[top_xy]     : 0;
188
        topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
189
        left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
190
        left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
191 0da71265 Michael Niedermayer
192
    if(IS_INTRA(mb_type)){
193 faa7e394 Michael Niedermayer
        int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
194 115329f1 Diego Biurrun
        h->topleft_samples_available=
195
        h->top_samples_available=
196 0da71265 Michael Niedermayer
        h->left_samples_available= 0xFFFF;
197
        h->topright_samples_available= 0xEEEA;
198
199 faa7e394 Michael Niedermayer
        if(!(top_type & type_mask)){
200 0da71265 Michael Niedermayer
            h->topleft_samples_available= 0xB3FF;
201
            h->top_samples_available= 0x33FF;
202
            h->topright_samples_available= 0x26EA;
203
        }
204 d1d10e91 Michael Niedermayer
        if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){
205
            if(IS_INTERLACED(mb_type)){
206 faa7e394 Michael Niedermayer
                if(!(left_type[0] & type_mask)){
207 d1d10e91 Michael Niedermayer
                    h->topleft_samples_available&= 0xDFFF;
208
                    h->left_samples_available&= 0x5FFF;
209
                }
210 faa7e394 Michael Niedermayer
                if(!(left_type[1] & type_mask)){
211 d1d10e91 Michael Niedermayer
                    h->topleft_samples_available&= 0xFF5F;
212
                    h->left_samples_available&= 0xFF5F;
213
                }
214
            }else{
215
                int left_typei = h->slice_table[left_xy[0] + s->mb_stride ] == h->slice_num
216
                                ? s->current_picture.mb_type[left_xy[0] + s->mb_stride] : 0;
217
                assert(left_xy[0] == left_xy[1]);
218 faa7e394 Michael Niedermayer
                if(!((left_typei & type_mask) && (left_type[0] & type_mask))){
219 d1d10e91 Michael Niedermayer
                    h->topleft_samples_available&= 0xDF5F;
220
                    h->left_samples_available&= 0x5F5F;
221
                }
222
            }
223
        }else{
224 faa7e394 Michael Niedermayer
            if(!(left_type[0] & type_mask)){
225 0da71265 Michael Niedermayer
                h->topleft_samples_available&= 0xDF5F;
226
                h->left_samples_available&= 0x5F5F;
227
            }
228
        }
229 115329f1 Diego Biurrun
230 faa7e394 Michael Niedermayer
        if(!(topleft_type & type_mask))
231 0da71265 Michael Niedermayer
            h->topleft_samples_available&= 0x7FFF;
232 115329f1 Diego Biurrun
233 faa7e394 Michael Niedermayer
        if(!(topright_type & type_mask))
234 0da71265 Michael Niedermayer
            h->topright_samples_available&= 0xFBFF;
235 115329f1 Diego Biurrun
236 0da71265 Michael Niedermayer
        if(IS_INTRA4x4(mb_type)){
237
            if(IS_INTRA4x4(top_type)){
238
                h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
239
                h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
240
                h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
241
                h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
242
            }else{
243
                int pred;
244 faa7e394 Michael Niedermayer
                if(!(top_type & type_mask))
245 0da71265 Michael Niedermayer
                    pred= -1;
246 6fbcaaa0 Loic Le Loarer
                else{
247
                    pred= 2;
248 0da71265 Michael Niedermayer
                }
249
                h->intra4x4_pred_mode_cache[4+8*0]=
250
                h->intra4x4_pred_mode_cache[5+8*0]=
251
                h->intra4x4_pred_mode_cache[6+8*0]=
252
                h->intra4x4_pred_mode_cache[7+8*0]= pred;
253
            }
254
            for(i=0; i<2; i++){
255
                if(IS_INTRA4x4(left_type[i])){
256
                    h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
257
                    h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
258
                }else{
259
                    int pred;
260 faa7e394 Michael Niedermayer
                    if(!(left_type[i] & type_mask))
261 0da71265 Michael Niedermayer
                        pred= -1;
262 6fbcaaa0 Loic Le Loarer
                    else{
263
                        pred= 2;
264 0da71265 Michael Niedermayer
                    }
265
                    h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
266
                    h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
267
                }
268
            }
269
        }
270
    }
271 29671011 Michael Niedermayer
    }
272 115329f1 Diego Biurrun
273
274 0da71265 Michael Niedermayer
/*
275 115329f1 Diego Biurrun
0 . T T. T T T T
276
1 L . .L . . . .
277
2 L . .L . . . .
278
3 . T TL . . . .
279
4 L . .L . . . .
280
5 L . .. . . . .
281 0da71265 Michael Niedermayer
*/
282 1412060e Diego Biurrun
//FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
283 0da71265 Michael Niedermayer
    if(top_type){
284 6867a90b Loic Le Loarer
        h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
285
        h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
286
        h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
287 53c05b1e Michael Niedermayer
        h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
288 115329f1 Diego Biurrun
289 6867a90b Loic Le Loarer
        h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
290 53c05b1e Michael Niedermayer
        h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
291 115329f1 Diego Biurrun
292 6867a90b Loic Le Loarer
        h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
293 53c05b1e Michael Niedermayer
        h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
294 115329f1 Diego Biurrun
295 0da71265 Michael Niedermayer
    }else{
296 115329f1 Diego Biurrun
        h->non_zero_count_cache[4+8*0]=
297 0da71265 Michael Niedermayer
        h->non_zero_count_cache[5+8*0]=
298
        h->non_zero_count_cache[6+8*0]=
299
        h->non_zero_count_cache[7+8*0]=
300 115329f1 Diego Biurrun
301 0da71265 Michael Niedermayer
        h->non_zero_count_cache[1+8*0]=
302
        h->non_zero_count_cache[2+8*0]=
303 115329f1 Diego Biurrun
304 0da71265 Michael Niedermayer
        h->non_zero_count_cache[1+8*3]=
305 3981c385 Michael Niedermayer
        h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
306 115329f1 Diego Biurrun
307 0da71265 Michael Niedermayer
    }
308 826de46e Loïc Le Loarer
309 6867a90b Loic Le Loarer
    for (i=0; i<2; i++) {
310
        if(left_type[i]){
311
            h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
312
            h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
313
            h->non_zero_count_cache[0+8*1 +   8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
314
            h->non_zero_count_cache[0+8*4 +   8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
315
        }else{
316 115329f1 Diego Biurrun
            h->non_zero_count_cache[3+8*1 + 2*8*i]=
317
            h->non_zero_count_cache[3+8*2 + 2*8*i]=
318
            h->non_zero_count_cache[0+8*1 +   8*i]=
319 6867a90b Loic Le Loarer
            h->non_zero_count_cache[0+8*4 +   8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
320 826de46e Loïc Le Loarer
        }
321
    }
322
323
    if( h->pps.cabac ) {
324
        // top_cbp
325
        if(top_type) {
326
            h->top_cbp = h->cbp_table[top_xy];
327
        } else if(IS_INTRA(mb_type)) {
328
            h->top_cbp = 0x1C0;
329
        } else {
330
            h->top_cbp = 0;
331
        }
332
        // left_cbp
333
        if (left_type[0]) {
334
            h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
335
        } else if(IS_INTRA(mb_type)) {
336
            h->left_cbp = 0x1C0;
337
        } else {
338
            h->left_cbp = 0;
339
        }
340
        if (left_type[0]) {
341
            h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
342
        }
343
        if (left_type[1]) {
344
            h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
345 6867a90b Loic Le Loarer
        }
346 0da71265 Michael Niedermayer
    }
347 6867a90b Loic Le Loarer
348 0da71265 Michael Niedermayer
#if 1
349 e2e5894a Loren Merritt
    if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
350 0da71265 Michael Niedermayer
        int list;
351 3425501d Michael Niedermayer
        for(list=0; list<h->list_count; list++){
352 e2e5894a Loren Merritt
            if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
353 0da71265 Michael Niedermayer
                /*if(!h->mv_cache_clean[list]){
354
                    memset(h->mv_cache [list],  0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
355
                    memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
356
                    h->mv_cache_clean[list]= 1;
357
                }*/
358 5ad984c9 Loren Merritt
                continue;
359 0da71265 Michael Niedermayer
            }
360
            h->mv_cache_clean[list]= 0;
361 115329f1 Diego Biurrun
362 53b19144 Loren Merritt
            if(USES_LIST(top_type, list)){
363 0da71265 Michael Niedermayer
                const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
364
                const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
365
                *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
366
                *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
367
                *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
368
                *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
369
                h->ref_cache[list][scan8[0] + 0 - 1*8]=
370
                h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
371
                h->ref_cache[list][scan8[0] + 2 - 1*8]=
372
                h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
373
            }else{
374 115329f1 Diego Biurrun
                *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
375
                *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
376
                *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
377 0da71265 Michael Niedermayer
                *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
378
                *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
379
            }
380
381 4672503d Loren Merritt
            for(i=0; i<2; i++){
382
                int cache_idx = scan8[0] - 1 + i*2*8;
383
                if(USES_LIST(left_type[i], list)){
384
                    const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
385
                    const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
386
                    *(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]];
387
                    *(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]];
388
                    h->ref_cache[list][cache_idx  ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
389
                    h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
390
                }else{
391
                    *(uint32_t*)h->mv_cache [list][cache_idx  ]=
392
                    *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
393
                    h->ref_cache[list][cache_idx  ]=
394
                    h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
395
                }
396 0da71265 Michael Niedermayer
            }
397
398 0281d325 Michael Niedermayer
            if(for_deblock || ((IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred) && !FRAME_MBAFF))
399 46f2f05f Michael Niedermayer
                continue;
400
401 53b19144 Loren Merritt
            if(USES_LIST(topleft_type, list)){
402 02f7695b Loren Merritt
                const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
403
                const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
404 e2e5894a Loren Merritt
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
405
                h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
406
            }else{
407
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
408
                h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
409
            }
410 115329f1 Diego Biurrun
411 53b19144 Loren Merritt
            if(USES_LIST(topright_type, list)){
412 e2e5894a Loren Merritt
                const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
413
                const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
414
                *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
415
                h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
416
            }else{
417
                *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
418
                h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
419
            }
420
421 ae08a563 Loren Merritt
            if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
422 717b1733 Loren Merritt
                continue;
423 115329f1 Diego Biurrun
424
            h->ref_cache[list][scan8[5 ]+1] =
425
            h->ref_cache[list][scan8[7 ]+1] =
426 3b66c4c5 Kevin Baragona
            h->ref_cache[list][scan8[13]+1] =  //FIXME remove past 3 (init somewhere else)
427 115329f1 Diego Biurrun
            h->ref_cache[list][scan8[4 ]] =
428 0da71265 Michael Niedermayer
            h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
429
            *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
430
            *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
431 3b66c4c5 Kevin Baragona
            *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
432 0da71265 Michael Niedermayer
            *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
433
            *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
434 9e528114 Laurent Aimar
435
            if( h->pps.cabac ) {
436
                /* XXX beurk, Load mvd */
437 53b19144 Loren Merritt
                if(USES_LIST(top_type, list)){
438 9e528114 Laurent Aimar
                    const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
439
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
440
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
441
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
442
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
443
                }else{
444 115329f1 Diego Biurrun
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
445
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
446
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
447 9e528114 Laurent Aimar
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
448
                }
449 53b19144 Loren Merritt
                if(USES_LIST(left_type[0], list)){
450 9e528114 Laurent Aimar
                    const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
451
                    *(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]];
452
                    *(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]];
453
                }else{
454
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
455
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
456
                }
457 53b19144 Loren Merritt
                if(USES_LIST(left_type[1], list)){
458 9e528114 Laurent Aimar
                    const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
459
                    *(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]];
460
                    *(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]];
461
                }else{
462
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
463
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
464
                }
465
                *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
466
                *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
467 3b66c4c5 Kevin Baragona
                *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
468 9e528114 Laurent Aimar
                *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
469
                *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
470 5ad984c9 Loren Merritt
471 9f5c1037 Michael Niedermayer
                if(h->slice_type_nos == FF_B_TYPE){
472 5ad984c9 Loren Merritt
                    fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
473
474
                    if(IS_DIRECT(top_type)){
475
                        *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
476
                    }else if(IS_8X8(top_type)){
477
                        int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
478
                        h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
479
                        h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
480
                    }else{
481
                        *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
482
                    }
483 115329f1 Diego Biurrun
484 5d18eaad Loren Merritt
                    if(IS_DIRECT(left_type[0]))
485
                        h->direct_cache[scan8[0] - 1 + 0*8]= 1;
486
                    else if(IS_8X8(left_type[0]))
487
                        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)];
488
                    else
489
                        h->direct_cache[scan8[0] - 1 + 0*8]= 0;
490
491
                    if(IS_DIRECT(left_type[1]))
492 5ad984c9 Loren Merritt
                        h->direct_cache[scan8[0] - 1 + 2*8]= 1;
493 5d18eaad Loren Merritt
                    else if(IS_8X8(left_type[1]))
494
                        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)];
495
                    else
496 5ad984c9 Loren Merritt
                        h->direct_cache[scan8[0] - 1 + 2*8]= 0;
497 5d18eaad Loren Merritt
                }
498
            }
499
500
            if(FRAME_MBAFF){
501
#define MAP_MVS\
502
                    MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
503
                    MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
504
                    MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
505
                    MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
506
                    MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
507
                    MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
508
                    MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
509
                    MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
510
                    MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
511
                    MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
512
                if(MB_FIELD){
513
#define MAP_F2F(idx, mb_type)\
514
                    if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
515
                        h->ref_cache[list][idx] <<= 1;\
516
                        h->mv_cache[list][idx][1] /= 2;\
517
                        h->mvd_cache[list][idx][1] /= 2;\
518
                    }
519
                    MAP_MVS
520
#undef MAP_F2F
521
                }else{
522
#define MAP_F2F(idx, mb_type)\
523
                    if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
524
                        h->ref_cache[list][idx] >>= 1;\
525
                        h->mv_cache[list][idx][1] <<= 1;\
526
                        h->mvd_cache[list][idx][1] <<= 1;\
527 5ad984c9 Loren Merritt
                    }
528 5d18eaad Loren Merritt
                    MAP_MVS
529
#undef MAP_F2F
530 5ad984c9 Loren Merritt
                }
531 9e528114 Laurent Aimar
            }
532 0da71265 Michael Niedermayer
        }
533
    }
534
#endif
535 43efd19a Loren Merritt
536
    h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
537 0da71265 Michael Niedermayer
}
538
539 903d58f6 Michael Niedermayer
void ff_h264_write_back_intra_pred_mode(H264Context *h){
540 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
541 0da71265 Michael Niedermayer
542
    h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
543
    h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
544
    h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
545
    h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
546
    h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
547
    h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
548
    h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
549
}
550
551
/**
552
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
553
 */
554 903d58f6 Michael Niedermayer
int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
555 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
556
    static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
557
    static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
558 115329f1 Diego Biurrun
559 43ff0714 Michael Niedermayer
    if(mode > 6U) {
560 5175b937 Loic Le Loarer
        av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
561 7440fe83 Michael Niedermayer
        return -1;
562 5175b937 Loic Le Loarer
    }
563 115329f1 Diego Biurrun
564 0da71265 Michael Niedermayer
    if(!(h->top_samples_available&0x8000)){
565
        mode= top[ mode ];
566
        if(mode<0){
567 9b879566 Michel Bardiaux
            av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
568 0da71265 Michael Niedermayer
            return -1;
569
        }
570
    }
571 115329f1 Diego Biurrun
572 d1d10e91 Michael Niedermayer
    if((h->left_samples_available&0x8080) != 0x8080){
573 0da71265 Michael Niedermayer
        mode= left[ mode ];
574 d1d10e91 Michael Niedermayer
        if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
575
            mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
576
        }
577 0da71265 Michael Niedermayer
        if(mode<0){
578 9b879566 Michel Bardiaux
            av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
579 0da71265 Michael Niedermayer
            return -1;
580 115329f1 Diego Biurrun
        }
581 0da71265 Michael Niedermayer
    }
582
583
    return mode;
584
}
585
586
/**
587
 * gets the predicted intra4x4 prediction mode.
588
 */
589
static inline int pred_intra_mode(H264Context *h, int n){
590
    const int index8= scan8[n];
591
    const int left= h->intra4x4_pred_mode_cache[index8 - 1];
592
    const int top = h->intra4x4_pred_mode_cache[index8 - 8];
593
    const int min= FFMIN(left, top);
594
595 a9c9a240 Michel Bardiaux
    tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
596 0da71265 Michael Niedermayer
597
    if(min<0) return DC_PRED;
598
    else      return min;
599
}
600
601
static inline void write_back_non_zero_count(H264Context *h){
602 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
603 0da71265 Michael Niedermayer
604 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
605
    h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
606
    h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
607 53c05b1e Michael Niedermayer
    h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
608 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
609
    h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
610
    h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
611 115329f1 Diego Biurrun
612 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
613 53c05b1e Michael Niedermayer
    h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
614 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
615 53c05b1e Michael Niedermayer
616 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
617 53c05b1e Michael Niedermayer
    h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
618 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
619 0da71265 Michael Niedermayer
}
620
621
/**
622 1412060e Diego Biurrun
 * gets the predicted number of non-zero coefficients.
623 0da71265 Michael Niedermayer
 * @param n block index
624
 */
625
static inline int pred_non_zero_count(H264Context *h, int n){
626
    const int index8= scan8[n];
627
    const int left= h->non_zero_count_cache[index8 - 1];
628
    const int top = h->non_zero_count_cache[index8 - 8];
629
    int i= left + top;
630 115329f1 Diego Biurrun
631 0da71265 Michael Niedermayer
    if(i<64) i= (i+1)>>1;
632
633 a9c9a240 Michel Bardiaux
    tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
634 0da71265 Michael Niedermayer
635
    return i&31;
636
}
637
638
/**
639
 * gets the directionally predicted 16x8 MV.
640
 * @param n the block index
641
 * @param mx the x component of the predicted motion vector
642
 * @param my the y component of the predicted motion vector
643
 */
644
static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
645
    if(n==0){
646
        const int top_ref=      h->ref_cache[list][ scan8[0] - 8 ];
647
        const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
648
649 a9c9a240 Michel Bardiaux
        tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
650 115329f1 Diego Biurrun
651 0da71265 Michael Niedermayer
        if(top_ref == ref){
652
            *mx= B[0];
653
            *my= B[1];
654
            return;
655
        }
656
    }else{
657
        const int left_ref=     h->ref_cache[list][ scan8[8] - 1 ];
658
        const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
659 115329f1 Diego Biurrun
660 a9c9a240 Michel Bardiaux
        tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
661 0da71265 Michael Niedermayer
662
        if(left_ref == ref){
663
            *mx= A[0];
664
            *my= A[1];
665
            return;
666
        }
667
    }
668
669
    //RARE
670
    pred_motion(h, n, 4, list, ref, mx, my);
671
}
672
673
/**
674
 * gets the directionally predicted 8x16 MV.
675
 * @param n the block index
676
 * @param mx the x component of the predicted motion vector
677
 * @param my the y component of the predicted motion vector
678
 */
679
static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
680
    if(n==0){
681
        const int left_ref=      h->ref_cache[list][ scan8[0] - 1 ];
682
        const int16_t * const A=  h->mv_cache[list][ scan8[0] - 1 ];
683 115329f1 Diego Biurrun
684 a9c9a240 Michel Bardiaux
        tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
685 0da71265 Michael Niedermayer
686
        if(left_ref == ref){
687
            *mx= A[0];
688
            *my= A[1];
689
            return;
690
        }
691
    }else{
692 1924f3ce Michael Niedermayer
        const int16_t * C;
693
        int diagonal_ref;
694
695
        diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
696 115329f1 Diego Biurrun
697 a9c9a240 Michel Bardiaux
        tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
698 0da71265 Michael Niedermayer
699 115329f1 Diego Biurrun
        if(diagonal_ref == ref){
700 0da71265 Michael Niedermayer
            *mx= C[0];
701
            *my= C[1];
702
            return;
703
        }
704
    }
705
706
    //RARE
707
    pred_motion(h, n, 2, list, ref, mx, my);
708
}
709
710
static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
711
    const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
712
    const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
713
714 a9c9a240 Michel Bardiaux
    tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
715 0da71265 Michael Niedermayer
716
    if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
717 62ea19c0 Michael Niedermayer
       || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
718
       || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
719 115329f1 Diego Biurrun
720 0da71265 Michael Niedermayer
        *mx = *my = 0;
721
        return;
722
    }
723 115329f1 Diego Biurrun
724 0da71265 Michael Niedermayer
    pred_motion(h, 0, 4, 0, 0, mx, my);
725
726
    return;
727
}
728
729
static inline void write_back_motion(H264Context *h, int mb_type){
730
    MpegEncContext * const s = &h->s;
731
    const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
732
    const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
733
    int list;
734
735 2ea39252 Loren Merritt
    if(!USES_LIST(mb_type, 0))
736
        fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
737
738 3425501d Michael Niedermayer
    for(list=0; list<h->list_count; list++){
739 0da71265 Michael Niedermayer
        int y;
740 53b19144 Loren Merritt
        if(!USES_LIST(mb_type, list))
741 5ad984c9 Loren Merritt
            continue;
742 115329f1 Diego Biurrun
743 0da71265 Michael Niedermayer
        for(y=0; y<4; y++){
744
            *(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];
745
            *(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];
746
        }
747 9e528114 Laurent Aimar
        if( h->pps.cabac ) {
748 e6e77eb6 Loren Merritt
            if(IS_SKIP(mb_type))
749
                fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
750
            else
751 9e528114 Laurent Aimar
            for(y=0; y<4; y++){
752
                *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
753
                *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
754
            }
755
        }
756 53b19144 Loren Merritt
757
        {
758 191e8ca7 Måns Rullgård
            int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
759 53b19144 Loren Merritt
            ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
760
            ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
761
            ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
762
            ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
763 0da71265 Michael Niedermayer
        }
764
    }
765 115329f1 Diego Biurrun
766 9f5c1037 Michael Niedermayer
    if(h->slice_type_nos == FF_B_TYPE && h->pps.cabac){
767 5ad984c9 Loren Merritt
        if(IS_8X8(mb_type)){
768 53b19144 Loren Merritt
            uint8_t *direct_table = &h->direct_table[b8_xy];
769
            direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
770
            direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
771
            direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
772 5ad984c9 Loren Merritt
        }
773
    }
774 0da71265 Michael Niedermayer
}
775
776 1790a5e9 Ivan Schreter
const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
777 0da71265 Michael Niedermayer
    int i, si, di;
778
    uint8_t *dst;
779 24456882 Andreas Öman
    int bufidx;
780 0da71265 Michael Niedermayer
781 bb270c08 Diego Biurrun
//    src[0]&0x80;                //forbidden bit
782 0da71265 Michael Niedermayer
    h->nal_ref_idc= src[0]>>5;
783
    h->nal_unit_type= src[0]&0x1F;
784
785
    src++; length--;
786 115329f1 Diego Biurrun
#if 0
787 0da71265 Michael Niedermayer
    for(i=0; i<length; i++)
788
        printf("%2X ", src[i]);
789
#endif
790 e08715d3 Michael Niedermayer
791 b250f9c6 Aurelien Jacobs
#if HAVE_FAST_UNALIGNED
792
# if HAVE_FAST_64BIT
793 e08715d3 Michael Niedermayer
#   define RS 7
794
    for(i=0; i+1<length; i+=9){
795 3878be31 Ivan Schreter
        if(!((~*(const uint64_t*)(src+i) & (*(const uint64_t*)(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
796 e08715d3 Michael Niedermayer
# else
797
#   define RS 3
798
    for(i=0; i+1<length; i+=5){
799 3878be31 Ivan Schreter
        if(!((~*(const uint32_t*)(src+i) & (*(const uint32_t*)(src+i) - 0x01000101U)) & 0x80008080U))
800 e08715d3 Michael Niedermayer
# endif
801
            continue;
802
        if(i>0 && !src[i]) i--;
803
        while(src[i]) i++;
804
#else
805
#   define RS 0
806 0da71265 Michael Niedermayer
    for(i=0; i+1<length; i+=2){
807
        if(src[i]) continue;
808
        if(i>0 && src[i-1]==0) i--;
809 e08715d3 Michael Niedermayer
#endif
810 0da71265 Michael Niedermayer
        if(i+2<length && src[i+1]==0 && src[i+2]<=3){
811
            if(src[i+2]!=3){
812
                /* startcode, so we must be past the end */
813
                length=i;
814
            }
815
            break;
816
        }
817 abb27cfb Michael Niedermayer
        i-= RS;
818 0da71265 Michael Niedermayer
    }
819
820
    if(i>=length-1){ //no escaped 0
821
        *dst_length= length;
822
        *consumed= length+1; //+1 for the header
823 115329f1 Diego Biurrun
        return src;
824 0da71265 Michael Niedermayer
    }
825
826 24456882 Andreas Öman
    bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
827 238ef6da Reimar Döffinger
    av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
828 24456882 Andreas Öman
    dst= h->rbsp_buffer[bufidx];
829 0da71265 Michael Niedermayer
830 ac658be5 Francois Oligny-Lemieux
    if (dst == NULL){
831
        return NULL;
832
    }
833
834 3b66c4c5 Kevin Baragona
//printf("decoding esc\n");
835 593af7cd Michael Niedermayer
    memcpy(dst, src, i);
836
    si=di=i;
837
    while(si+2<length){
838 0da71265 Michael Niedermayer
        //remove escapes (very rare 1:2^22)
839 593af7cd Michael Niedermayer
        if(src[si+2]>3){
840
            dst[di++]= src[si++];
841
            dst[di++]= src[si++];
842
        }else if(src[si]==0 && src[si+1]==0){
843 0da71265 Michael Niedermayer
            if(src[si+2]==3){ //escape
844
                dst[di++]= 0;
845
                dst[di++]= 0;
846
                si+=3;
847 c8470cc1 Michael Niedermayer
                continue;
848 0da71265 Michael Niedermayer
            }else //next start code
849 593af7cd Michael Niedermayer
                goto nsc;
850 0da71265 Michael Niedermayer
        }
851
852
        dst[di++]= src[si++];
853
    }
854 593af7cd Michael Niedermayer
    while(si<length)
855
        dst[di++]= src[si++];
856
nsc:
857 0da71265 Michael Niedermayer
858 d4369630 Alexander Strange
    memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
859
860 0da71265 Michael Niedermayer
    *dst_length= di;
861
    *consumed= si + 1;//+1 for the header
862 90b5b51e Diego Biurrun
//FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
863 0da71265 Michael Niedermayer
    return dst;
864
}
865
866 1790a5e9 Ivan Schreter
int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
867 0da71265 Michael Niedermayer
    int v= *src;
868
    int r;
869
870 a9c9a240 Michel Bardiaux
    tprintf(h->s.avctx, "rbsp trailing %X\n", v);
871 0da71265 Michael Niedermayer
872
    for(r=1; r<9; r++){
873
        if(v&1) return r;
874
        v>>=1;
875
    }
876
    return 0;
877
}
878
879
/**
880 1412060e Diego Biurrun
 * IDCT transforms the 16 dc values and dequantizes them.
881 0da71265 Michael Niedermayer
 * @param qp quantization parameter
882
 */
883 239ea04c Loren Merritt
static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
884 0da71265 Michael Niedermayer
#define stride 16
885
    int i;
886
    int temp[16]; //FIXME check if this is a good idea
887
    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
888
    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
889
890
//memset(block, 64, 2*256);
891
//return;
892
    for(i=0; i<4; i++){
893
        const int offset= y_offset[i];
894
        const int z0= block[offset+stride*0] + block[offset+stride*4];
895
        const int z1= block[offset+stride*0] - block[offset+stride*4];
896
        const int z2= block[offset+stride*1] - block[offset+stride*5];
897
        const int z3= block[offset+stride*1] + block[offset+stride*5];
898
899
        temp[4*i+0]= z0+z3;
900
        temp[4*i+1]= z1+z2;
901
        temp[4*i+2]= z1-z2;
902
        temp[4*i+3]= z0-z3;
903
    }
904
905
    for(i=0; i<4; i++){
906
        const int offset= x_offset[i];
907
        const int z0= temp[4*0+i] + temp[4*2+i];
908
        const int z1= temp[4*0+i] - temp[4*2+i];
909
        const int z2= temp[4*1+i] - temp[4*3+i];
910
        const int z3= temp[4*1+i] + temp[4*3+i];
911
912 1412060e Diego Biurrun
        block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
913 239ea04c Loren Merritt
        block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
914
        block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
915
        block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
916 0da71265 Michael Niedermayer
    }
917
}
918
919 e5017ab8 Laurent Aimar
#if 0
920 0da71265 Michael Niedermayer
/**
921 1412060e Diego Biurrun
 * DCT transforms the 16 dc values.
922 0da71265 Michael Niedermayer
 * @param qp quantization parameter ??? FIXME
923
 */
924
static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
925
//    const int qmul= dequant_coeff[qp][0];
926
    int i;
927
    int temp[16]; //FIXME check if this is a good idea
928
    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
929
    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
930

931
    for(i=0; i<4; i++){
932
        const int offset= y_offset[i];
933
        const int z0= block[offset+stride*0] + block[offset+stride*4];
934
        const int z1= block[offset+stride*0] - block[offset+stride*4];
935
        const int z2= block[offset+stride*1] - block[offset+stride*5];
936
        const int z3= block[offset+stride*1] + block[offset+stride*5];
937

938
        temp[4*i+0]= z0+z3;
939
        temp[4*i+1]= z1+z2;
940
        temp[4*i+2]= z1-z2;
941
        temp[4*i+3]= z0-z3;
942
    }
943

944
    for(i=0; i<4; i++){
945
        const int offset= x_offset[i];
946
        const int z0= temp[4*0+i] + temp[4*2+i];
947
        const int z1= temp[4*0+i] - temp[4*2+i];
948
        const int z2= temp[4*1+i] - temp[4*3+i];
949
        const int z3= temp[4*1+i] + temp[4*3+i];
950

951
        block[stride*0 +offset]= (z0 + z3)>>1;
952
        block[stride*2 +offset]= (z1 + z2)>>1;
953
        block[stride*8 +offset]= (z1 - z2)>>1;
954
        block[stride*10+offset]= (z0 - z3)>>1;
955
    }
956
}
957 e5017ab8 Laurent Aimar
#endif
958
959 0da71265 Michael Niedermayer
#undef xStride
960
#undef stride
961
962 239ea04c Loren Merritt
static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
963 0da71265 Michael Niedermayer
    const int stride= 16*2;
964
    const int xStride= 16;
965
    int a,b,c,d,e;
966
967
    a= block[stride*0 + xStride*0];
968
    b= block[stride*0 + xStride*1];
969
    c= block[stride*1 + xStride*0];
970
    d= block[stride*1 + xStride*1];
971
972
    e= a-b;
973
    a= a+b;
974
    b= c-d;
975
    c= c+d;
976
977 239ea04c Loren Merritt
    block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
978
    block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
979
    block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
980
    block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
981 0da71265 Michael Niedermayer
}
982
983 e5017ab8 Laurent Aimar
#if 0
984 0da71265 Michael Niedermayer
static void chroma_dc_dct_c(DCTELEM *block){
985
    const int stride= 16*2;
986
    const int xStride= 16;
987
    int a,b,c,d,e;
988

989
    a= block[stride*0 + xStride*0];
990
    b= block[stride*0 + xStride*1];
991
    c= block[stride*1 + xStride*0];
992
    d= block[stride*1 + xStride*1];
993

994
    e= a-b;
995
    a= a+b;
996
    b= c-d;
997
    c= c+d;
998

999
    block[stride*0 + xStride*0]= (a+c);
1000
    block[stride*0 + xStride*1]= (e+b);
1001
    block[stride*1 + xStride*0]= (a-c);
1002
    block[stride*1 + xStride*1]= (e-b);
1003
}
1004 e5017ab8 Laurent Aimar
#endif
1005 0da71265 Michael Niedermayer
1006
static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
1007
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1008
                           int src_x_offset, int src_y_offset,
1009
                           qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
1010
    MpegEncContext * const s = &h->s;
1011
    const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
1012 5d18eaad Loren Merritt
    int my=       h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
1013 0da71265 Michael Niedermayer
    const int luma_xy= (mx&3) + ((my&3)<<2);
1014 5d18eaad Loren Merritt
    uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
1015
    uint8_t * src_cb, * src_cr;
1016
    int extra_width= h->emu_edge_width;
1017
    int extra_height= h->emu_edge_height;
1018 0da71265 Michael Niedermayer
    int emu=0;
1019
    const int full_mx= mx>>2;
1020
    const int full_my= my>>2;
1021 fbd312fd Loren Merritt
    const int pic_width  = 16*s->mb_width;
1022 0d43dd8c Jeff Downs
    const int pic_height = 16*s->mb_height >> MB_FIELD;
1023 115329f1 Diego Biurrun
1024 0da71265 Michael Niedermayer
    if(mx&7) extra_width -= 3;
1025
    if(my&7) extra_height -= 3;
1026 115329f1 Diego Biurrun
1027
    if(   full_mx < 0-extra_width
1028
       || full_my < 0-extra_height
1029
       || full_mx + 16/*FIXME*/ > pic_width + extra_width
1030 fbd312fd Loren Merritt
       || full_my + 16/*FIXME*/ > pic_height + extra_height){
1031 5d18eaad Loren Merritt
        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);
1032
            src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
1033 0da71265 Michael Niedermayer
        emu=1;
1034
    }
1035 115329f1 Diego Biurrun
1036 5d18eaad Loren Merritt
    qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
1037 0da71265 Michael Niedermayer
    if(!square){
1038 5d18eaad Loren Merritt
        qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
1039 0da71265 Michael Niedermayer
    }
1040 115329f1 Diego Biurrun
1041 49fb20cb Aurelien Jacobs
    if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
1042 115329f1 Diego Biurrun
1043 0d43dd8c Jeff Downs
    if(MB_FIELD){
1044 5d18eaad Loren Merritt
        // chroma offset when predicting from a field of opposite parity
1045 2143b118 Jeff Downs
        my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
1046 5d18eaad Loren Merritt
        emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
1047
    }
1048
    src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
1049
    src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
1050
1051 0da71265 Michael Niedermayer
    if(emu){
1052 5d18eaad Loren Merritt
        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);
1053 0da71265 Michael Niedermayer
            src_cb= s->edge_emu_buffer;
1054
    }
1055 5d18eaad Loren Merritt
    chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
1056 0da71265 Michael Niedermayer
1057
    if(emu){
1058 5d18eaad Loren Merritt
        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);
1059 0da71265 Michael Niedermayer
            src_cr= s->edge_emu_buffer;
1060
    }
1061 5d18eaad Loren Merritt
    chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
1062 0da71265 Michael Niedermayer
}
1063
1064 9f2d1b4f Loren Merritt
static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
1065 0da71265 Michael Niedermayer
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1066
                           int x_offset, int y_offset,
1067
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
1068
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
1069
                           int list0, int list1){
1070
    MpegEncContext * const s = &h->s;
1071
    qpel_mc_func *qpix_op=  qpix_put;
1072
    h264_chroma_mc_func chroma_op= chroma_put;
1073 115329f1 Diego Biurrun
1074 5d18eaad Loren Merritt
    dest_y  += 2*x_offset + 2*y_offset*h->  mb_linesize;
1075
    dest_cb +=   x_offset +   y_offset*h->mb_uvlinesize;
1076
    dest_cr +=   x_offset +   y_offset*h->mb_uvlinesize;
1077 0da71265 Michael Niedermayer
    x_offset += 8*s->mb_x;
1078 0d43dd8c Jeff Downs
    y_offset += 8*(s->mb_y >> MB_FIELD);
1079 115329f1 Diego Biurrun
1080 0da71265 Michael Niedermayer
    if(list0){
1081 1924f3ce Michael Niedermayer
        Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
1082 0da71265 Michael Niedermayer
        mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
1083
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
1084
                           qpix_op, chroma_op);
1085
1086
        qpix_op=  qpix_avg;
1087
        chroma_op= chroma_avg;
1088
    }
1089
1090
    if(list1){
1091 1924f3ce Michael Niedermayer
        Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
1092 0da71265 Michael Niedermayer
        mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
1093
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
1094
                           qpix_op, chroma_op);
1095
    }
1096
}
1097
1098 9f2d1b4f Loren Merritt
static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
1099
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1100
                           int x_offset, int y_offset,
1101
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
1102
                           h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
1103
                           h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
1104
                           int list0, int list1){
1105
    MpegEncContext * const s = &h->s;
1106
1107 5d18eaad Loren Merritt
    dest_y  += 2*x_offset + 2*y_offset*h->  mb_linesize;
1108
    dest_cb +=   x_offset +   y_offset*h->mb_uvlinesize;
1109
    dest_cr +=   x_offset +   y_offset*h->mb_uvlinesize;
1110 9f2d1b4f Loren Merritt
    x_offset += 8*s->mb_x;
1111 0d43dd8c Jeff Downs
    y_offset += 8*(s->mb_y >> MB_FIELD);
1112 115329f1 Diego Biurrun
1113 9f2d1b4f Loren Merritt
    if(list0 && list1){
1114
        /* don't optimize for luma-only case, since B-frames usually
1115
         * use implicit weights => chroma too. */
1116
        uint8_t *tmp_cb = s->obmc_scratchpad;
1117 5d18eaad Loren Merritt
        uint8_t *tmp_cr = s->obmc_scratchpad + 8;
1118
        uint8_t *tmp_y  = s->obmc_scratchpad + 8*h->mb_uvlinesize;
1119 9f2d1b4f Loren Merritt
        int refn0 = h->ref_cache[0][ scan8[n] ];
1120
        int refn1 = h->ref_cache[1][ scan8[n] ];
1121
1122
        mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
1123
                    dest_y, dest_cb, dest_cr,
1124
                    x_offset, y_offset, qpix_put, chroma_put);
1125
        mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
1126
                    tmp_y, tmp_cb, tmp_cr,
1127
                    x_offset, y_offset, qpix_put, chroma_put);
1128
1129
        if(h->use_weight == 2){
1130
            int weight0 = h->implicit_weight[refn0][refn1];
1131
            int weight1 = 64 - weight0;
1132 5d18eaad Loren Merritt
            luma_weight_avg(  dest_y,  tmp_y,  h->  mb_linesize, 5, weight0, weight1, 0);
1133
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
1134
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
1135 9f2d1b4f Loren Merritt
        }else{
1136 5d18eaad Loren Merritt
            luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
1137 115329f1 Diego Biurrun
                            h->luma_weight[0][refn0], h->luma_weight[1][refn1],
1138 e8b56208 Loren Merritt
                            h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
1139 5d18eaad Loren Merritt
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
1140 115329f1 Diego Biurrun
                            h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
1141 e8b56208 Loren Merritt
                            h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
1142 5d18eaad Loren Merritt
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
1143 115329f1 Diego Biurrun
                            h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
1144 e8b56208 Loren Merritt
                            h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
1145 9f2d1b4f Loren Merritt
        }
1146
    }else{
1147
        int list = list1 ? 1 : 0;
1148
        int refn = h->ref_cache[list][ scan8[n] ];
1149
        Picture *ref= &h->ref_list[list][refn];
1150
        mc_dir_part(h, ref, n, square, chroma_height, delta, list,
1151
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
1152
                    qpix_put, chroma_put);
1153
1154 5d18eaad Loren Merritt
        luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
1155 9f2d1b4f Loren Merritt
                       h->luma_weight[list][refn], h->luma_offset[list][refn]);
1156
        if(h->use_weight_chroma){
1157 5d18eaad Loren Merritt
            chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
1158 9f2d1b4f Loren Merritt
                             h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
1159 5d18eaad Loren Merritt
            chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
1160 9f2d1b4f Loren Merritt
                             h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
1161
        }
1162
    }
1163
}
1164
1165
static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
1166
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1167
                           int x_offset, int y_offset,
1168
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
1169
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
1170 115329f1 Diego Biurrun
                           h264_weight_func *weight_op, h264_biweight_func *weight_avg,
1171 9f2d1b4f Loren Merritt
                           int list0, int list1){
1172
    if((h->use_weight==2 && list0 && list1
1173
        && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
1174
       || h->use_weight==1)
1175
        mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
1176
                         x_offset, y_offset, qpix_put, chroma_put,
1177
                         weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
1178
    else
1179
        mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
1180
                    x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
1181
}
1182
1183 513fbd8e Loren Merritt
static inline void prefetch_motion(H264Context *h, int list){
1184
    /* fetch pixels for estimated mv 4 macroblocks ahead
1185
     * optimized for 64byte cache lines */
1186
    MpegEncContext * const s = &h->s;
1187
    const int refn = h->ref_cache[list][scan8[0]];
1188
    if(refn >= 0){
1189
        const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
1190
        const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
1191
        uint8_t **src= h->ref_list[list][refn].data;
1192 5d18eaad Loren Merritt
        int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
1193 513fbd8e Loren Merritt
        s->dsp.prefetch(src[0]+off, s->linesize, 4);
1194
        off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
1195
        s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
1196
    }
1197
}
1198
1199 0da71265 Michael Niedermayer
static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1200
                      qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
1201 9f2d1b4f Loren Merritt
                      qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
1202
                      h264_weight_func *weight_op, h264_biweight_func *weight_avg){
1203 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
1204 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
1205 0da71265 Michael Niedermayer
    const int mb_type= s->current_picture.mb_type[mb_xy];
1206 115329f1 Diego Biurrun
1207 0da71265 Michael Niedermayer
    assert(IS_INTER(mb_type));
1208 115329f1 Diego Biurrun
1209 513fbd8e Loren Merritt
    prefetch_motion(h, 0);
1210
1211 0da71265 Michael Niedermayer
    if(IS_16X16(mb_type)){
1212
        mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
1213
                qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
1214 9f2d1b4f Loren Merritt
                &weight_op[0], &weight_avg[0],
1215 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
1216
    }else if(IS_16X8(mb_type)){
1217
        mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
1218
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
1219 9f2d1b4f Loren Merritt
                &weight_op[1], &weight_avg[1],
1220 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
1221
        mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
1222
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
1223 9f2d1b4f Loren Merritt
                &weight_op[1], &weight_avg[1],
1224 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
1225
    }else if(IS_8X16(mb_type)){
1226 5d18eaad Loren Merritt
        mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
1227 0da71265 Michael Niedermayer
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
1228 9f2d1b4f Loren Merritt
                &weight_op[2], &weight_avg[2],
1229 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
1230 5d18eaad Loren Merritt
        mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
1231 0da71265 Michael Niedermayer
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
1232 9f2d1b4f Loren Merritt
                &weight_op[2], &weight_avg[2],
1233 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
1234
    }else{
1235
        int i;
1236 115329f1 Diego Biurrun
1237 0da71265 Michael Niedermayer
        assert(IS_8X8(mb_type));
1238
1239
        for(i=0; i<4; i++){
1240
            const int sub_mb_type= h->sub_mb_type[i];
1241
            const int n= 4*i;
1242
            int x_offset= (i&1)<<2;
1243
            int y_offset= (i&2)<<1;
1244
1245
            if(IS_SUB_8X8(sub_mb_type)){
1246
                mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
1247
                    qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
1248 9f2d1b4f Loren Merritt
                    &weight_op[3], &weight_avg[3],
1249 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1250
            }else if(IS_SUB_8X4(sub_mb_type)){
1251
                mc_part(h, n  , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
1252
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
1253 9f2d1b4f Loren Merritt
                    &weight_op[4], &weight_avg[4],
1254 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1255
                mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
1256
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
1257 9f2d1b4f Loren Merritt
                    &weight_op[4], &weight_avg[4],
1258 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1259
            }else if(IS_SUB_4X8(sub_mb_type)){
1260 5d18eaad Loren Merritt
                mc_part(h, n  , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
1261 0da71265 Michael Niedermayer
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
1262 9f2d1b4f Loren Merritt
                    &weight_op[5], &weight_avg[5],
1263 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1264 5d18eaad Loren Merritt
                mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
1265 0da71265 Michael Niedermayer
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
1266 9f2d1b4f Loren Merritt
                    &weight_op[5], &weight_avg[5],
1267 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1268
            }else{
1269
                int j;
1270
                assert(IS_SUB_4X4(sub_mb_type));
1271
                for(j=0; j<4; j++){
1272
                    int sub_x_offset= x_offset + 2*(j&1);
1273
                    int sub_y_offset= y_offset +   (j&2);
1274
                    mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
1275
                        qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
1276 9f2d1b4f Loren Merritt
                        &weight_op[6], &weight_avg[6],
1277 0da71265 Michael Niedermayer
                        IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1278
                }
1279
            }
1280
        }
1281
    }
1282 513fbd8e Loren Merritt
1283
    prefetch_motion(h, 1);
1284 0da71265 Michael Niedermayer
}
1285
1286 8140955d Michael Niedermayer
static av_cold void init_cavlc_level_tab(void){
1287
    int suffix_length, mask;
1288
    unsigned int i;
1289
1290
    for(suffix_length=0; suffix_length<7; suffix_length++){
1291
        for(i=0; i<(1<<LEVEL_TAB_BITS); i++){
1292
            int prefix= LEVEL_TAB_BITS - av_log2(2*i);
1293
            int level_code= (prefix<<suffix_length) + (i>>(LEVEL_TAB_BITS-prefix-1-suffix_length)) - (1<<suffix_length);
1294
1295
            mask= -(level_code&1);
1296
            level_code= (((2+level_code)>>1) ^ mask) - mask;
1297
            if(prefix + 1 + suffix_length <= LEVEL_TAB_BITS){
1298
                cavlc_level_tab[suffix_length][i][0]= level_code;
1299
                cavlc_level_tab[suffix_length][i][1]= prefix + 1 + suffix_length;
1300
            }else if(prefix + 1 <= LEVEL_TAB_BITS){
1301
                cavlc_level_tab[suffix_length][i][0]= prefix+100;
1302
                cavlc_level_tab[suffix_length][i][1]= prefix + 1;
1303
            }else{
1304
                cavlc_level_tab[suffix_length][i][0]= LEVEL_TAB_BITS+100;
1305
                cavlc_level_tab[suffix_length][i][1]= LEVEL_TAB_BITS;
1306
            }
1307
        }
1308
    }
1309
}
1310
1311 98a6fff9 Zuxy Meng
static av_cold void decode_init_vlc(void){
1312 0da71265 Michael Niedermayer
    static int done = 0;
1313
1314
    if (!done) {
1315
        int i;
1316 910e3668 Art Clarke
        int offset;
1317 0da71265 Michael Niedermayer
        done = 1;
1318
1319 910e3668 Art Clarke
        chroma_dc_coeff_token_vlc.table = chroma_dc_coeff_token_vlc_table;
1320
        chroma_dc_coeff_token_vlc.table_allocated = chroma_dc_coeff_token_vlc_table_size;
1321 115329f1 Diego Biurrun
        init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
1322 0da71265 Michael Niedermayer
                 &chroma_dc_coeff_token_len [0], 1, 1,
1323 910e3668 Art Clarke
                 &chroma_dc_coeff_token_bits[0], 1, 1,
1324
                 INIT_VLC_USE_NEW_STATIC);
1325 0da71265 Michael Niedermayer
1326 910e3668 Art Clarke
        offset = 0;
1327 0da71265 Michael Niedermayer
        for(i=0; i<4; i++){
1328 910e3668 Art Clarke
            coeff_token_vlc[i].table = coeff_token_vlc_tables+offset;
1329
            coeff_token_vlc[i].table_allocated = coeff_token_vlc_tables_size[i];
1330 115329f1 Diego Biurrun
            init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
1331 0da71265 Michael Niedermayer
                     &coeff_token_len [i][0], 1, 1,
1332 910e3668 Art Clarke
                     &coeff_token_bits[i][0], 1, 1,
1333
                     INIT_VLC_USE_NEW_STATIC);
1334
            offset += coeff_token_vlc_tables_size[i];
1335 0da71265 Michael Niedermayer
        }
1336 910e3668 Art Clarke
        /*
1337
         * This is a one time safety check to make sure that
1338
         * the packed static coeff_token_vlc table sizes
1339
         * were initialized correctly.
1340
         */
1341 37d3e066 Aurelien Jacobs
        assert(offset == FF_ARRAY_ELEMS(coeff_token_vlc_tables));
1342 0da71265 Michael Niedermayer
1343
        for(i=0; i<3; i++){
1344 910e3668 Art Clarke
            chroma_dc_total_zeros_vlc[i].table = chroma_dc_total_zeros_vlc_tables[i];
1345
            chroma_dc_total_zeros_vlc[i].table_allocated = chroma_dc_total_zeros_vlc_tables_size;
1346
            init_vlc(&chroma_dc_total_zeros_vlc[i],
1347
                     CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
1348 0da71265 Michael Niedermayer
                     &chroma_dc_total_zeros_len [i][0], 1, 1,
1349 910e3668 Art Clarke
                     &chroma_dc_total_zeros_bits[i][0], 1, 1,
1350
                     INIT_VLC_USE_NEW_STATIC);
1351 0da71265 Michael Niedermayer
        }
1352
        for(i=0; i<15; i++){
1353 910e3668 Art Clarke
            total_zeros_vlc[i].table = total_zeros_vlc_tables[i];
1354
            total_zeros_vlc[i].table_allocated = total_zeros_vlc_tables_size;
1355
            init_vlc(&total_zeros_vlc[i],
1356
                     TOTAL_ZEROS_VLC_BITS, 16,
1357 0da71265 Michael Niedermayer
                     &total_zeros_len [i][0], 1, 1,
1358 910e3668 Art Clarke
                     &total_zeros_bits[i][0], 1, 1,
1359
                     INIT_VLC_USE_NEW_STATIC);
1360 0da71265 Michael Niedermayer
        }
1361
1362
        for(i=0; i<6; i++){
1363 910e3668 Art Clarke
            run_vlc[i].table = run_vlc_tables[i];
1364
            run_vlc[i].table_allocated = run_vlc_tables_size;
1365
            init_vlc(&run_vlc[i],
1366
                     RUN_VLC_BITS, 7,
1367 0da71265 Michael Niedermayer
                     &run_len [i][0], 1, 1,
1368 910e3668 Art Clarke
                     &run_bits[i][0], 1, 1,
1369
                     INIT_VLC_USE_NEW_STATIC);
1370 0da71265 Michael Niedermayer
        }
1371 910e3668 Art Clarke
        run7_vlc.table = run7_vlc_table,
1372
        run7_vlc.table_allocated = run7_vlc_table_size;
1373 115329f1 Diego Biurrun
        init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
1374 0da71265 Michael Niedermayer
                 &run_len [6][0], 1, 1,
1375 910e3668 Art Clarke
                 &run_bits[6][0], 1, 1,
1376
                 INIT_VLC_USE_NEW_STATIC);
1377 8140955d Michael Niedermayer
1378
        init_cavlc_level_tab();
1379 0da71265 Michael Niedermayer
    }
1380
}
1381
1382
static void free_tables(H264Context *h){
1383 7978debd Andreas Öman
    int i;
1384 afebe2f7 Andreas Öman
    H264Context *hx;
1385 0da71265 Michael Niedermayer
    av_freep(&h->intra4x4_pred_mode);
1386 e5017ab8 Laurent Aimar
    av_freep(&h->chroma_pred_mode_table);
1387
    av_freep(&h->cbp_table);
1388 9e528114 Laurent Aimar
    av_freep(&h->mvd_table[0]);
1389
    av_freep(&h->mvd_table[1]);
1390 5ad984c9 Loren Merritt
    av_freep(&h->direct_table);
1391 0da71265 Michael Niedermayer
    av_freep(&h->non_zero_count);
1392
    av_freep(&h->slice_table_base);
1393
    h->slice_table= NULL;
1394 e5017ab8 Laurent Aimar
1395 0da71265 Michael Niedermayer
    av_freep(&h->mb2b_xy);
1396
    av_freep(&h->mb2b8_xy);
1397 9f2d1b4f Loren Merritt
1398 6752dd5a Reimar Döffinger
    for(i = 0; i < MAX_THREADS; i++) {
1399 afebe2f7 Andreas Öman
        hx = h->thread_context[i];
1400
        if(!hx) continue;
1401
        av_freep(&hx->top_borders[1]);
1402
        av_freep(&hx->top_borders[0]);
1403
        av_freep(&hx->s.obmc_scratchpad);
1404 d2d5e067 Alexander Strange
        av_freep(&hx->rbsp_buffer[1]);
1405
        av_freep(&hx->rbsp_buffer[0]);
1406 eda4ea4e Mike Scheutzow
        hx->rbsp_buffer_size[0] = 0;
1407
        hx->rbsp_buffer_size[1] = 0;
1408 d2d5e067 Alexander Strange
        if (i) av_freep(&h->thread_context[i]);
1409 afebe2f7 Andreas Öman
    }
1410 0da71265 Michael Niedermayer
}
1411
1412 239ea04c Loren Merritt
static void init_dequant8_coeff_table(H264Context *h){
1413
    int i,q,x;
1414 548a1c8a Loren Merritt
    const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
1415 239ea04c Loren Merritt
    h->dequant8_coeff[0] = h->dequant8_buffer[0];
1416
    h->dequant8_coeff[1] = h->dequant8_buffer[1];
1417
1418
    for(i=0; i<2; i++ ){
1419
        if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
1420
            h->dequant8_coeff[1] = h->dequant8_buffer[0];
1421
            break;
1422
        }
1423
1424
        for(q=0; q<52; q++){
1425 d9ec210b Diego Pettenò
            int shift = div6[q];
1426
            int idx = rem6[q];
1427 239ea04c Loren Merritt
            for(x=0; x<64; x++)
1428 548a1c8a Loren Merritt
                h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
1429
                    ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
1430
                    h->pps.scaling_matrix8[i][x]) << shift;
1431 239ea04c Loren Merritt
        }
1432
    }
1433
}
1434
1435
static void init_dequant4_coeff_table(H264Context *h){
1436
    int i,j,q,x;
1437 ab2e3e2c Loren Merritt
    const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
1438 239ea04c Loren Merritt
    for(i=0; i<6; i++ ){
1439
        h->dequant4_coeff[i] = h->dequant4_buffer[i];
1440
        for(j=0; j<i; j++){
1441
            if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
1442
                h->dequant4_coeff[i] = h->dequant4_buffer[j];
1443
                break;
1444
            }
1445
        }
1446
        if(j<i)
1447
            continue;
1448
1449
        for(q=0; q<52; q++){
1450 d9ec210b Diego Pettenò
            int shift = div6[q] + 2;
1451
            int idx = rem6[q];
1452 239ea04c Loren Merritt
            for(x=0; x<16; x++)
1453 ab2e3e2c Loren Merritt
                h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
1454
                    ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
1455 239ea04c Loren Merritt
                    h->pps.scaling_matrix4[i][x]) << shift;
1456
        }
1457
    }
1458
}
1459
1460
static void init_dequant_tables(H264Context *h){
1461
    int i,x;
1462
    init_dequant4_coeff_table(h);
1463
    if(h->pps.transform_8x8_mode)
1464
        init_dequant8_coeff_table(h);
1465
    if(h->sps.transform_bypass){
1466
        for(i=0; i<6; i++)
1467
            for(x=0; x<16; x++)
1468
                h->dequant4_coeff[i][0][x] = 1<<6;
1469
        if(h->pps.transform_8x8_mode)
1470
            for(i=0; i<2; i++)
1471
                for(x=0; x<64; x++)
1472
                    h->dequant8_coeff[i][0][x] = 1<<6;
1473
    }
1474
}
1475
1476
1477 903d58f6 Michael Niedermayer
int ff_h264_alloc_tables(H264Context *h){
1478 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
1479 7bc9090a Michael Niedermayer
    const int big_mb_num= s->mb_stride * (s->mb_height+1);
1480 239ea04c Loren Merritt
    int x,y;
1481 0da71265 Michael Niedermayer
1482 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, big_mb_num * 8  * sizeof(uint8_t), fail)
1483 e5017ab8 Laurent Aimar
1484 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count    , big_mb_num * 16 * sizeof(uint8_t), fail)
1485
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base  , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
1486
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
1487 0da71265 Michael Niedermayer
1488 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
1489
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t), fail);
1490
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t), fail);
1491
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 32*big_mb_num * sizeof(uint8_t) , fail);
1492 e5017ab8 Laurent Aimar
1493 b735aeea Michael Niedermayer
    memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride)  * sizeof(*h->slice_table_base));
1494 5d18eaad Loren Merritt
    h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
1495 0da71265 Michael Niedermayer
1496 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy  , big_mb_num * sizeof(uint32_t), fail);
1497
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b8_xy , big_mb_num * sizeof(uint32_t), fail);
1498 0da71265 Michael Niedermayer
    for(y=0; y<s->mb_height; y++){
1499
        for(x=0; x<s->mb_width; x++){
1500 7bc9090a Michael Niedermayer
            const int mb_xy= x + y*s->mb_stride;
1501 0da71265 Michael Niedermayer
            const int b_xy = 4*x + 4*y*h->b_stride;
1502
            const int b8_xy= 2*x + 2*y*h->b8_stride;
1503 115329f1 Diego Biurrun
1504 0da71265 Michael Niedermayer
            h->mb2b_xy [mb_xy]= b_xy;
1505
            h->mb2b8_xy[mb_xy]= b8_xy;
1506
        }
1507
    }
1508 9f2d1b4f Loren Merritt
1509 9c6221ae Gert Vervoort
    s->obmc_scratchpad = NULL;
1510
1511 56edbd81 Loren Merritt
    if(!h->dequant4_coeff[0])
1512
        init_dequant_tables(h);
1513
1514 0da71265 Michael Niedermayer
    return 0;
1515
fail:
1516
    free_tables(h);
1517
    return -1;
1518
}
1519
1520 afebe2f7 Andreas Öman
/**
1521
 * Mimic alloc_tables(), but for every context thread.
1522
 */
1523
static void clone_tables(H264Context *dst, H264Context *src){
1524
    dst->intra4x4_pred_mode       = src->intra4x4_pred_mode;
1525
    dst->non_zero_count           = src->non_zero_count;
1526
    dst->slice_table              = src->slice_table;
1527
    dst->cbp_table                = src->cbp_table;
1528
    dst->mb2b_xy                  = src->mb2b_xy;
1529
    dst->mb2b8_xy                 = src->mb2b8_xy;
1530
    dst->chroma_pred_mode_table   = src->chroma_pred_mode_table;
1531
    dst->mvd_table[0]             = src->mvd_table[0];
1532
    dst->mvd_table[1]             = src->mvd_table[1];
1533
    dst->direct_table             = src->direct_table;
1534
1535
    dst->s.obmc_scratchpad = NULL;
1536
    ff_h264_pred_init(&dst->hpc, src->s.codec_id);
1537
}
1538
1539
/**
1540
 * Init context
1541
 * Allocate buffers which are not shared amongst multiple threads.
1542
 */
1543
static int context_init(H264Context *h){
1544 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
1545
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
1546 afebe2f7 Andreas Öman
1547
    return 0;
1548
fail:
1549
    return -1; // free_tables will clean up for us
1550
}
1551
1552 98a6fff9 Zuxy Meng
static av_cold void common_init(H264Context *h){
1553 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
1554
1555
    s->width = s->avctx->width;
1556
    s->height = s->avctx->height;
1557
    s->codec_id= s->avctx->codec->id;
1558 115329f1 Diego Biurrun
1559 c92a30bb Kostya Shishkov
    ff_h264_pred_init(&h->hpc, s->codec_id);
1560 0da71265 Michael Niedermayer
1561 239ea04c Loren Merritt
    h->dequant_coeff_pps= -1;
1562 9a41c2c7 Michael Niedermayer
    s->unrestricted_mv=1;
1563 0da71265 Michael Niedermayer
    s->decode=1; //FIXME
1564 56edbd81 Loren Merritt
1565 a5805aa9 Michael Niedermayer
    dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
1566
1567 56edbd81 Loren Merritt
    memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
1568
    memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
1569 0da71265 Michael Niedermayer
}
1570
1571 03831f46 Ivan Schreter
/**
1572
 * Reset SEI values at the beginning of the frame.
1573
 *
1574
 * @param h H.264 context.
1575
 */
1576
static void reset_sei(H264Context *h) {
1577
    h->sei_recovery_frame_cnt       = -1;
1578
    h->sei_dpb_output_delay         =  0;
1579
    h->sei_cpb_removal_delay        = -1;
1580
    h->sei_buffering_period_present =  0;
1581
}
1582
1583 903d58f6 Michael Niedermayer
av_cold int ff_h264_decode_init(AVCodecContext *avctx){
1584 0da71265 Michael Niedermayer
    H264Context *h= avctx->priv_data;
1585
    MpegEncContext * const s = &h->s;
1586
1587 3edcacde Michael Niedermayer
    MPV_decode_defaults(s);
1588 115329f1 Diego Biurrun
1589 0da71265 Michael Niedermayer
    s->avctx = avctx;
1590
    common_init(h);
1591
1592
    s->out_format = FMT_H264;
1593
    s->workaround_bugs= avctx->workaround_bugs;
1594
1595
    // set defaults
1596
//    s->decode_mb= ff_h263_decode_mb;
1597 9a5a05d0 Andreas Öman
    s->quarter_sample = 1;
1598 47cd974a Michael Niedermayer
    if(!avctx->has_b_frames)
1599 0da71265 Michael Niedermayer
    s->low_delay= 1;
1600 7a9dba3c Michael Niedermayer
1601 580a7465 David Conrad
    avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1602 0da71265 Michael Niedermayer
1603 c2212338 Panagiotis Issaris
    decode_init_vlc();
1604 115329f1 Diego Biurrun
1605 26165f99 Måns Rullgård
    if(avctx->extradata_size > 0 && avctx->extradata &&
1606
       *(char *)avctx->extradata == 1){
1607 4770b1b4 Roberto Togni
        h->is_avc = 1;
1608
        h->got_avcC = 0;
1609 26165f99 Måns Rullgård
    } else {
1610
        h->is_avc = 0;
1611 4770b1b4 Roberto Togni
    }
1612
1613 afebe2f7 Andreas Öman
    h->thread_context[0] = h;
1614 18c7be65 Jeff Downs
    h->outputed_poc = INT_MIN;
1615 e4b8f1fa Michael Niedermayer
    h->prev_poc_msb= 1<<16;
1616 03831f46 Ivan Schreter
    reset_sei(h);
1617 efd8c1f6 Michael Niedermayer
    if(avctx->codec_id == CODEC_ID_H264){
1618
        if(avctx->ticks_per_frame == 1){
1619
            s->avctx->time_base.den *=2;
1620
        }
1621 19df37a8 Michael Niedermayer
        avctx->ticks_per_frame = 2;
1622 efd8c1f6 Michael Niedermayer
    }
1623 0da71265 Michael Niedermayer
    return 0;
1624
}
1625
1626 903d58f6 Michael Niedermayer
int ff_h264_frame_start(H264Context *h){
1627 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
1628
    int i;
1629
1630 af8aa846 Michael Niedermayer
    if(MPV_frame_start(s, s->avctx) < 0)
1631
        return -1;
1632 0da71265 Michael Niedermayer
    ff_er_frame_start(s);
1633 3a22d7fa Jeff Downs
    /*
1634
     * MPV_frame_start uses pict_type to derive key_frame.
1635
     * This is incorrect for H.264; IDR markings must be used.
1636 1412060e Diego Biurrun
     * Zero here; IDR markings per slice in frame or fields are ORed in later.
1637 3a22d7fa Jeff Downs
     * See decode_nal_units().
1638
     */
1639
    s->current_picture_ptr->key_frame= 0;
1640 c173a088 Haruhiko Yamagata
    s->current_picture_ptr->mmco_reset= 0;
1641 0da71265 Michael Niedermayer
1642
    assert(s->linesize && s->uvlinesize);
1643
1644
    for(i=0; i<16; i++){
1645
        h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
1646 6867a90b Loic Le Loarer
        h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
1647 0da71265 Michael Niedermayer
    }
1648
    for(i=0; i<4; i++){
1649
        h->block_offset[16+i]=
1650
        h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
1651 6867a90b Loic Le Loarer
        h->block_offset[24+16+i]=
1652
        h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
1653 0da71265 Michael Niedermayer
    }
1654
1655 934b0821 Loren Merritt
    /* can't be in alloc_tables because linesize isn't known there.
1656
     * FIXME: redo bipred weight to not require extra buffer? */
1657 afebe2f7 Andreas Öman
    for(i = 0; i < s->avctx->thread_count; i++)
1658
        if(!h->thread_context[i]->s.obmc_scratchpad)
1659
            h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
1660 5d18eaad Loren Merritt
1661
    /* some macroblocks will be accessed before they're available */
1662 afebe2f7 Andreas Öman
    if(FRAME_MBAFF || s->avctx->thread_count > 1)
1663 b735aeea Michael Niedermayer
        memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
1664 934b0821 Loren Merritt
1665 0da71265 Michael Niedermayer
//    s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
1666 28bb9eb2 Michael Niedermayer
1667 1412060e Diego Biurrun
    // We mark the current picture as non-reference after allocating it, so
1668 28bb9eb2 Michael Niedermayer
    // that if we break out due to an error it can be released automatically
1669
    // in the next MPV_frame_start().
1670
    // SVQ3 as well as most other codecs have only last/next/current and thus
1671
    // get released even with set reference, besides SVQ3 and others do not
1672
    // mark frames as reference later "naturally".
1673
    if(s->codec_id != CODEC_ID_SVQ3)
1674
        s->current_picture_ptr->reference= 0;
1675 357282c6 Michael Niedermayer
1676
    s->current_picture_ptr->field_poc[0]=
1677
    s->current_picture_ptr->field_poc[1]= INT_MAX;
1678 5118c6c7 Michael Niedermayer
    assert(s->current_picture_ptr->long_ref==0);
1679 357282c6 Michael Niedermayer
1680 af8aa846 Michael Niedermayer
    return 0;
1681 0da71265 Michael Niedermayer
}
1682
1683 93cc10fa Andreas Öman
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){
1684 53c05b1e Michael Niedermayer
    MpegEncContext * const s = &h->s;
1685
    int i;
1686 5f7f9719 Michael Niedermayer
    int step    = 1;
1687
    int offset  = 1;
1688
    int uvoffset= 1;
1689
    int top_idx = 1;
1690
    int skiplast= 0;
1691 115329f1 Diego Biurrun
1692 53c05b1e Michael Niedermayer
    src_y  -=   linesize;
1693
    src_cb -= uvlinesize;
1694
    src_cr -= uvlinesize;
1695
1696 5f7f9719 Michael Niedermayer
    if(!simple && FRAME_MBAFF){
1697
        if(s->mb_y&1){
1698
            offset  = MB_MBAFF ? 1 : 17;
1699
            uvoffset= MB_MBAFF ? 1 : 9;
1700
            if(!MB_MBAFF){
1701
                *(uint64_t*)(h->top_borders[0][s->mb_x]+ 0)= *(uint64_t*)(src_y +  15*linesize);
1702
                *(uint64_t*)(h->top_borders[0][s->mb_x]+ 8)= *(uint64_t*)(src_y +8+15*linesize);
1703 49fb20cb Aurelien Jacobs
                if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1704 5f7f9719 Michael Niedermayer
                    *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+7*uvlinesize);
1705
                    *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+7*uvlinesize);
1706
                }
1707
            }
1708
        }else{
1709
            if(!MB_MBAFF){
1710
                h->left_border[0]= h->top_borders[0][s->mb_x][15];
1711 49fb20cb Aurelien Jacobs
                if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1712 5f7f9719 Michael Niedermayer
                    h->left_border[34   ]= h->top_borders[0][s->mb_x][16+7  ];
1713
                    h->left_border[34+18]= h->top_borders[0][s->mb_x][16+8+7];
1714
                }
1715
                skiplast= 1;
1716
            }
1717
            offset  =
1718
            uvoffset=
1719
            top_idx = MB_MBAFF ? 0 : 1;
1720
        }
1721
        step= MB_MBAFF ? 2 : 1;
1722
    }
1723
1724 3b66c4c5 Kevin Baragona
    // There are two lines saved, the line above the the top macroblock of a pair,
1725 6867a90b Loic Le Loarer
    // and the line above the bottom macroblock
1726 5f7f9719 Michael Niedermayer
    h->left_border[offset]= h->top_borders[top_idx][s->mb_x][15];
1727
    for(i=1; i<17 - skiplast; i++){
1728
        h->left_border[offset+i*step]= src_y[15+i*  linesize];
1729 53c05b1e Michael Niedermayer
    }
1730 115329f1 Diego Biurrun
1731 5f7f9719 Michael Niedermayer
    *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0)= *(uint64_t*)(src_y +  16*linesize);
1732
    *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
1733 53c05b1e Michael Niedermayer
1734 49fb20cb Aurelien Jacobs
    if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1735 5f7f9719 Michael Niedermayer
        h->left_border[uvoffset+34   ]= h->top_borders[top_idx][s->mb_x][16+7];
1736
        h->left_border[uvoffset+34+18]= h->top_borders[top_idx][s->mb_x][24+7];
1737
        for(i=1; i<9 - skiplast; i++){
1738
            h->left_border[uvoffset+34   +i*step]= src_cb[7+i*uvlinesize];
1739
            h->left_border[uvoffset+34+18+i*step]= src_cr[7+i*uvlinesize];
1740 53c05b1e Michael Niedermayer
        }
1741 5f7f9719 Michael Niedermayer
        *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
1742
        *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
1743 53c05b1e Michael Niedermayer
    }
1744
}
1745
1746 93cc10fa Andreas Öman
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){
1747 53c05b1e Michael Niedermayer
    MpegEncContext * const s = &h->s;
1748
    int temp8, i;
1749
    uint64_t temp64;
1750 b69378e2 Andreas Öman
    int deblock_left;
1751
    int deblock_top;
1752
    int mb_xy;
1753 5f7f9719 Michael Niedermayer
    int step    = 1;
1754
    int offset  = 1;
1755
    int uvoffset= 1;
1756
    int top_idx = 1;
1757
1758
    if(!simple && FRAME_MBAFF){
1759
        if(s->mb_y&1){
1760
            offset  = MB_MBAFF ? 1 : 17;
1761
            uvoffset= MB_MBAFF ? 1 : 9;
1762
        }else{
1763
            offset  =
1764
            uvoffset=
1765
            top_idx = MB_MBAFF ? 0 : 1;
1766
        }
1767
        step= MB_MBAFF ? 2 : 1;
1768
    }
1769 b69378e2 Andreas Öman
1770
    if(h->deblocking_filter == 2) {
1771 64514ee8 Alexander Strange
        mb_xy = h->mb_xy;
1772 b69378e2 Andreas Öman
        deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
1773
        deblock_top  = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
1774
    } else {
1775
        deblock_left = (s->mb_x > 0);
1776 6c805007 Michael Niedermayer
        deblock_top =  (s->mb_y > !!MB_FIELD);
1777 b69378e2 Andreas Öman
    }
1778 53c05b1e Michael Niedermayer
1779
    src_y  -=   linesize + 1;
1780
    src_cb -= uvlinesize + 1;
1781
    src_cr -= uvlinesize + 1;
1782
1783
#define XCHG(a,b,t,xchg)\
1784
t= a;\
1785
if(xchg)\
1786
    a= b;\
1787
b= t;
1788 d89dc06a Loren Merritt
1789
    if(deblock_left){
1790 5f7f9719 Michael Niedermayer
        for(i = !deblock_top; i<16; i++){
1791
            XCHG(h->left_border[offset+i*step], src_y [i*  linesize], temp8, xchg);
1792 d89dc06a Loren Merritt
        }
1793 5f7f9719 Michael Niedermayer
        XCHG(h->left_border[offset+i*step], src_y [i*  linesize], temp8, 1);
1794 d89dc06a Loren Merritt
    }
1795
1796
    if(deblock_top){
1797 5f7f9719 Michael Niedermayer
        XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
1798
        XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
1799 cad4368a Reimar Döffinger
        if(s->mb_x+1 < s->mb_width){
1800 5f7f9719 Michael Niedermayer
            XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
1801 43efd19a Loren Merritt
        }
1802 53c05b1e Michael Niedermayer
    }
1803
1804 49fb20cb Aurelien Jacobs
    if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1805 d89dc06a Loren Merritt
        if(deblock_left){
1806 5f7f9719 Michael Niedermayer
            for(i = !deblock_top; i<8; i++){
1807
                XCHG(h->left_border[uvoffset+34   +i*step], src_cb[i*uvlinesize], temp8, xchg);
1808
                XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, xchg);
1809 d89dc06a Loren Merritt
            }
1810 5f7f9719 Michael Niedermayer
            XCHG(h->left_border[uvoffset+34   +i*step], src_cb[i*uvlinesize], temp8, 1);
1811
            XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, 1);
1812 d89dc06a Loren Merritt
        }
1813
        if(deblock_top){
1814 5f7f9719 Michael Niedermayer
            XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
1815
            XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
1816 53c05b1e Michael Niedermayer
        }
1817
    }
1818
}
1819
1820 5a6a6cc7 Diego Biurrun
static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
1821 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
1822
    const int mb_x= s->mb_x;
1823
    const int mb_y= s->mb_y;
1824 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
1825 0da71265 Michael Niedermayer
    const int mb_type= s->current_picture.mb_type[mb_xy];
1826
    uint8_t  *dest_y, *dest_cb, *dest_cr;
1827
    int linesize, uvlinesize /*dct_offset*/;
1828
    int i;
1829 6867a90b Loic Le Loarer
    int *block_offset = &h->block_offset[0];
1830 41e4055b Michael Niedermayer
    const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
1831 8b6871ed Diego Biurrun
    /* is_h264 should always be true if SVQ3 is disabled. */
1832 49fb20cb Aurelien Jacobs
    const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
1833 36940eca Loren Merritt
    void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
1834 ef9d1d15 Loren Merritt
    void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
1835 0da71265 Michael Niedermayer
1836 6120a343 Michael Niedermayer
    dest_y  = s->current_picture.data[0] + (mb_x + mb_y * s->linesize  ) * 16;
1837
    dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
1838
    dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
1839 0da71265 Michael Niedermayer
1840 a957c27b Loren Merritt
    s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1841
    s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
1842
1843 bd91fee3 Alexander Strange
    if (!simple && MB_FIELD) {
1844 5d18eaad Loren Merritt
        linesize   = h->mb_linesize   = s->linesize * 2;
1845
        uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
1846 6867a90b Loic Le Loarer
        block_offset = &h->block_offset[24];
1847 1412060e Diego Biurrun
        if(mb_y&1){ //FIXME move out of this function?
1848 0da71265 Michael Niedermayer
            dest_y -= s->linesize*15;
1849 6867a90b Loic Le Loarer
            dest_cb-= s->uvlinesize*7;
1850
            dest_cr-= s->uvlinesize*7;
1851 0da71265 Michael Niedermayer
        }
1852 5d18eaad Loren Merritt
        if(FRAME_MBAFF) {
1853
            int list;
1854 3425501d Michael Niedermayer
            for(list=0; list<h->list_count; list++){
1855 5d18eaad Loren Merritt
                if(!USES_LIST(mb_type, list))
1856
                    continue;
1857
                if(IS_16X16(mb_type)){
1858
                    int8_t *ref = &h->ref_cache[list][scan8[0]];
1859 1710856c Andreas Öman
                    fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
1860 5d18eaad Loren Merritt
                }else{
1861
                    for(i=0; i<16; i+=4){
1862
                        int ref = h->ref_cache[list][scan8[i]];
1863
                        if(ref >= 0)
1864 1710856c Andreas Öman
                            fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
1865 5d18eaad Loren Merritt
                    }
1866
                }
1867
            }
1868
        }
1869 0da71265 Michael Niedermayer
    } else {
1870 5d18eaad Loren Merritt
        linesize   = h->mb_linesize   = s->linesize;
1871
        uvlinesize = h->mb_uvlinesize = s->uvlinesize;
1872 0da71265 Michael Niedermayer
//        dct_offset = s->linesize * 16;
1873
    }
1874 115329f1 Diego Biurrun
1875 bd91fee3 Alexander Strange
    if (!simple && IS_INTRA_PCM(mb_type)) {
1876 c1708e8d Michael Niedermayer
        for (i=0; i<16; i++) {
1877
            memcpy(dest_y + i*  linesize, h->mb       + i*8, 16);
1878 6fbcaaa0 Loic Le Loarer
        }
1879 c1708e8d Michael Niedermayer
        for (i=0; i<8; i++) {
1880
            memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4,  8);
1881
            memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4,  8);
1882 6fbcaaa0 Loic Le Loarer
        }
1883 e7e09b49 Loic Le Loarer
    } else {
1884
        if(IS_INTRA(mb_type)){
1885 5f7f9719 Michael Niedermayer
            if(h->deblocking_filter)
1886 93cc10fa Andreas Öman
                xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
1887 53c05b1e Michael Niedermayer
1888 49fb20cb Aurelien Jacobs
            if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1889 c92a30bb Kostya Shishkov
                h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
1890
                h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
1891 e7e09b49 Loic Le Loarer
            }
1892 0da71265 Michael Niedermayer
1893 e7e09b49 Loic Le Loarer
            if(IS_INTRA4x4(mb_type)){
1894 bd91fee3 Alexander Strange
                if(simple || !s->encoding){
1895 43efd19a Loren Merritt
                    if(IS_8x8DCT(mb_type)){
1896 1eb96035 Michael Niedermayer
                        if(transform_bypass){
1897
                            idct_dc_add =
1898
                            idct_add    = s->dsp.add_pixels8;
1899 dae006d7 Michael Niedermayer
                        }else{
1900 1eb96035 Michael Niedermayer
                            idct_dc_add = s->dsp.h264_idct8_dc_add;
1901
                            idct_add    = s->dsp.h264_idct8_add;
1902
                        }
1903 43efd19a Loren Merritt
                        for(i=0; i<16; i+=4){
1904
                            uint8_t * const ptr= dest_y + block_offset[i];
1905
                            const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1906 41e4055b Michael Niedermayer
                            if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1907
                                h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
1908
                            }else{
1909 ac0623b2 Michael Niedermayer
                                const int nnz = h->non_zero_count_cache[ scan8[i] ];
1910
                                h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
1911
                                                            (h->topright_samples_available<<i)&0x4000, linesize);
1912
                                if(nnz){
1913
                                    if(nnz == 1 && h->mb[i*16])
1914
                                        idct_dc_add(ptr, h->mb + i*16, linesize);
1915
                                    else
1916
                                        idct_add   (ptr, h->mb + i*16, linesize);
1917
                                }
1918 41e4055b Michael Niedermayer
                            }
1919 43efd19a Loren Merritt
                        }
1920 1eb96035 Michael Niedermayer
                    }else{
1921
                        if(transform_bypass){
1922
                            idct_dc_add =
1923
                            idct_add    = s->dsp.add_pixels4;
1924
                        }else{
1925
                            idct_dc_add = s->dsp.h264_idct_dc_add;
1926
                            idct_add    = s->dsp.h264_idct_add;
1927
                        }
1928 aebb5d6d Michael Niedermayer
                        for(i=0; i<16; i++){
1929
                            uint8_t * const ptr= dest_y + block_offset[i];
1930
                            const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1931 e7e09b49 Loic Le Loarer
1932 aebb5d6d Michael Niedermayer
                            if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1933
                                h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
1934
                            }else{
1935
                                uint8_t *topright;
1936
                                int nnz, tr;
1937
                                if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
1938
                                    const int topright_avail= (h->topright_samples_available<<i)&0x8000;
1939
                                    assert(mb_y || linesize <= block_offset[i]);
1940
                                    if(!topright_avail){
1941
                                        tr= ptr[3 - linesize]*0x01010101;
1942
                                        topright= (uint8_t*) &tr;
1943
                                    }else
1944
                                        topright= ptr + 4 - linesize;
1945 ac0623b2 Michael Niedermayer
                                }else
1946 aebb5d6d Michael Niedermayer
                                    topright= NULL;
1947
1948
                                h->hpc.pred4x4[ dir ](ptr, topright, linesize);
1949
                                nnz = h->non_zero_count_cache[ scan8[i] ];
1950
                                if(nnz){
1951
                                    if(is_h264){
1952
                                        if(nnz == 1 && h->mb[i*16])
1953
                                            idct_dc_add(ptr, h->mb + i*16, linesize);
1954
                                        else
1955
                                            idct_add   (ptr, h->mb + i*16, linesize);
1956
                                    }else
1957
                                        svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
1958
                                }
1959 ac0623b2 Michael Niedermayer
                            }
1960 41e4055b Michael Niedermayer
                        }
1961 8b82a956 Michael Niedermayer
                    }
1962 0da71265 Michael Niedermayer
                }
1963 e7e09b49 Loic Le Loarer
            }else{
1964 c92a30bb Kostya Shishkov
                h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
1965 bd91fee3 Alexander Strange
                if(is_h264){
1966 36940eca Loren Merritt
                    if(!transform_bypass)
1967 93f0c0a4 Panagiotis Issaris
                        h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
1968 36940eca Loren Merritt
                }else
1969 e7e09b49 Loic Le Loarer
                    svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
1970 0da71265 Michael Niedermayer
            }
1971 5f7f9719 Michael Niedermayer
            if(h->deblocking_filter)
1972 93cc10fa Andreas Öman
                xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
1973 bd91fee3 Alexander Strange
        }else if(is_h264){
1974 e7e09b49 Loic Le Loarer
            hl_motion(h, dest_y, dest_cb, dest_cr,
1975 2833fc46 Loren Merritt
                      s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
1976
                      s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
1977 e7e09b49 Loic Le Loarer
                      s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
1978 0da71265 Michael Niedermayer
        }
1979 e7e09b49 Loic Le Loarer
1980
1981
        if(!IS_INTRA4x4(mb_type)){
1982 bd91fee3 Alexander Strange
            if(is_h264){
1983 ef9d1d15 Loren Merritt
                if(IS_INTRA16x16(mb_type)){
1984 2fd1f0e0 Michael Niedermayer
                    if(transform_bypass){
1985
                        if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
1986 0a8ca22f Michael Niedermayer
                            h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
1987
                        }else{
1988
                            for(i=0; i<16; i++){
1989
                                if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
1990 1eb96035 Michael Niedermayer
                                    s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
1991 0a8ca22f Michael Niedermayer
                            }
1992 2fd1f0e0 Michael Niedermayer
                        }
1993
                    }else{
1994
                         s->dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
1995 41e4055b Michael Niedermayer
                    }
1996 49c084a7 Michael Niedermayer
                }else if(h->cbp&15){
1997 2fd1f0e0 Michael Niedermayer
                    if(transform_bypass){
1998 0a8ca22f Michael Niedermayer
                        const int di = IS_8x8DCT(mb_type) ? 4 : 1;
1999 1eb96035 Michael Niedermayer
                        idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
2000 0a8ca22f Michael Niedermayer
                        for(i=0; i<16; i+=di){
2001 62bc966f Michael Niedermayer
                            if(h->non_zero_count_cache[ scan8[i] ]){
2002 ef9d1d15 Loren Merritt
                                idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
2003 0a8ca22f Michael Niedermayer
                            }
2004 ef9d1d15 Loren Merritt
                        }
2005 2fd1f0e0 Michael Niedermayer
                    }else{
2006
                        if(IS_8x8DCT(mb_type)){
2007
                            s->dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
2008
                        }else{
2009
                            s->dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
2010
                        }
2011
                    }
2012 4704097a Michael Niedermayer
                }
2013 e7e09b49 Loic Le Loarer
            }else{
2014
                for(i=0; i<16; i++){
2015
                    if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
2016 6867a90b Loic Le Loarer
                        uint8_t * const ptr= dest_y + block_offset[i];
2017 e7e09b49 Loic Le Loarer
                        svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
2018
                    }
2019 4704097a Michael Niedermayer
                }
2020 0da71265 Michael Niedermayer
            }
2021
        }
2022
2023 49fb20cb Aurelien Jacobs
        if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
2024 ef9d1d15 Loren Merritt
            uint8_t *dest[2] = {dest_cb, dest_cr};
2025
            if(transform_bypass){
2026 96465b90 Michael Niedermayer
                if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
2027
                    h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
2028
                    h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
2029
                }else{
2030 c25ac15a Michael Niedermayer
                    idct_add = s->dsp.add_pixels4;
2031 96465b90 Michael Niedermayer
                    for(i=16; i<16+8; i++){
2032
                        if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
2033
                            idct_add   (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
2034
                    }
2035
                }
2036 ef9d1d15 Loren Merritt
            }else{
2037 4691a77d Andreas Öman
                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]);
2038
                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]);
2039 aebb5d6d Michael Niedermayer
                if(is_h264){
2040 c25ac15a Michael Niedermayer
                    idct_add = s->dsp.h264_idct_add;
2041
                    idct_dc_add = s->dsp.h264_idct_dc_add;
2042 ac0623b2 Michael Niedermayer
                    for(i=16; i<16+8; i++){
2043
                        if(h->non_zero_count_cache[ scan8[i] ])
2044
                            idct_add   (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
2045
                        else if(h->mb[i*16])
2046
                            idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
2047
                    }
2048 aebb5d6d Michael Niedermayer
                }else{
2049
                    for(i=16; i<16+8; i++){
2050
                        if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
2051
                            uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
2052
                            svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
2053
                        }
2054 e7e09b49 Loic Le Loarer
                    }
2055 4704097a Michael Niedermayer
                }
2056 0da71265 Michael Niedermayer
            }
2057
        }
2058
    }
2059 c212fb0c Michael Niedermayer
    if(h->cbp || IS_INTRA(mb_type))
2060
        s->dsp.clear_blocks(h->mb);
2061
2062 53c05b1e Michael Niedermayer
    if(h->deblocking_filter) {
2063 5f7f9719 Michael Niedermayer
        backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
2064
        fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
2065
        h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
2066
        h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
2067 bd91fee3 Alexander Strange
        if (!simple && FRAME_MBAFF) {
2068 082cf971 Michael Niedermayer
            ff_h264_filter_mb     (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
2069 6ba71fc4 Loïc Le Loarer
        } else {
2070 082cf971 Michael Niedermayer
            ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
2071 6ba71fc4 Loïc Le Loarer
        }
2072 53c05b1e Michael Niedermayer
    }
2073 0da71265 Michael Niedermayer
}
2074
2075
/**
2076 bd91fee3 Alexander Strange
 * Process a macroblock; this case avoids checks for expensive uncommon cases.
2077
 */
2078
static void hl_decode_mb_simple(H264Context *h){
2079
    hl_decode_mb_internal(h, 1);
2080
}
2081
2082
/**
2083
 * Process a macroblock; this handles edge cases, such as interlacing.
2084
 */
2085
static void av_noinline hl_decode_mb_complex(H264Context *h){
2086
    hl_decode_mb_internal(h, 0);
2087
}
2088
2089 903d58f6 Michael Niedermayer
void ff_h264_hl_decode_mb(H264Context *h){
2090 bd91fee3 Alexander Strange
    MpegEncContext * const s = &h->s;
2091 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
2092 bd91fee3 Alexander Strange
    const int mb_type= s->current_picture.mb_type[mb_xy];
2093 49fb20cb Aurelien Jacobs
    int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
2094 bd91fee3 Alexander Strange
2095
    if (is_complex)
2096
        hl_decode_mb_complex(h);
2097
    else hl_decode_mb_simple(h);
2098
}
2099
2100 2143b118 Jeff Downs
static void pic_as_field(Picture *pic, const int parity){
2101 11cc1d8c Jeff Downs
    int i;
2102
    for (i = 0; i < 4; ++i) {
2103 2143b118 Jeff Downs
        if (parity == PICT_BOTTOM_FIELD)
2104 11cc1d8c Jeff Downs
            pic->data[i] += pic->linesize[i];
2105 2143b118 Jeff Downs
        pic->reference = parity;
2106 11cc1d8c Jeff Downs
        pic->linesize[i] *= 2;
2107
    }
2108 2879c75f Michael Niedermayer
    pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
2109 11cc1d8c Jeff Downs
}
2110
2111
static int split_field_copy(Picture *dest, Picture *src,
2112
                            int parity, int id_add){
2113
    int match = !!(src->reference & parity);
2114
2115
    if (match) {
2116
        *dest = *src;
2117 d4f7d838 Michael Niedermayer
        if(parity != PICT_FRAME){
2118 b3e93fd4 Michael Niedermayer
            pic_as_field(dest, parity);
2119
            dest->pic_id *= 2;
2120
            dest->pic_id += id_add;
2121 d4f7d838 Michael Niedermayer
        }
2122 11cc1d8c Jeff Downs
    }
2123
2124
    return match;
2125
}
2126
2127 d4f7d838 Michael Niedermayer
static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
2128
    int i[2]={0};
2129
    int index=0;
2130 11cc1d8c Jeff Downs
2131 d4f7d838 Michael Niedermayer
    while(i[0]<len || i[1]<len){
2132
        while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
2133
            i[0]++;
2134
        while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
2135
            i[1]++;
2136
        if(i[0] < len){
2137
            in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
2138
            split_field_copy(&def[index++], in[ i[0]++ ], sel  , 1);
2139
        }
2140
        if(i[1] < len){
2141
            in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
2142
            split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
2143 11cc1d8c Jeff Downs
        }
2144
    }
2145
2146 d4f7d838 Michael Niedermayer
    return index;
2147 11cc1d8c Jeff Downs
}
2148
2149 d4f7d838 Michael Niedermayer
static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
2150
    int i, best_poc;
2151
    int out_i= 0;
2152 11cc1d8c Jeff Downs
2153 d4f7d838 Michael Niedermayer
    for(;;){
2154
        best_poc= dir ? INT_MIN : INT_MAX;
2155 11cc1d8c Jeff Downs
2156 d4f7d838 Michael Niedermayer
        for(i=0; i<len; i++){
2157
            const int poc= src[i]->poc;
2158
            if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
2159
                best_poc= poc;
2160
                sorted[out_i]= src[i];
2161
            }
2162
        }
2163
        if(best_poc == (dir ? INT_MIN : INT_MAX))
2164
            break;
2165
        limit= sorted[out_i++]->poc - dir;
2166
    }
2167
    return out_i;
2168 11cc1d8c Jeff Downs
}
2169
2170 bd91fee3 Alexander Strange
/**
2171 0da71265 Michael Niedermayer
 * fills the default_ref_list.
2172
 */
2173
static int fill_default_ref_list(H264Context *h){
2174
    MpegEncContext * const s = &h->s;
2175 d4f7d838 Michael Niedermayer
    int i, len;
2176 115329f1 Diego Biurrun
2177 9f5c1037 Michael Niedermayer
    if(h->slice_type_nos==FF_B_TYPE){
2178 d4f7d838 Michael Niedermayer
        Picture *sorted[32];
2179
        int cur_poc, list;
2180
        int lens[2];
2181 11cc1d8c Jeff Downs
2182 d4f7d838 Michael Niedermayer
        if(FIELD_PICTURE)
2183
            cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
2184
        else
2185
            cur_poc= s->current_picture_ptr->poc;
2186 086acdd5 Jeff Downs
2187 d4f7d838 Michael Niedermayer
        for(list= 0; list<2; list++){
2188
            len= add_sorted(sorted    , h->short_ref, h->short_ref_count, cur_poc, 1^list);
2189
            len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
2190
            assert(len<=32);
2191
            len= build_def_list(h->default_ref_list[list]    , sorted     , len, 0, s->picture_structure);
2192
            len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
2193
            assert(len<=32);
2194 086acdd5 Jeff Downs
2195 d4f7d838 Michael Niedermayer
            if(len < h->ref_count[list])
2196
                memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
2197
            lens[list]= len;
2198 086acdd5 Jeff Downs
        }
2199
2200 d4f7d838 Michael Niedermayer
        if(lens[0] == lens[1] && lens[1] > 1){
2201
            for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
2202
            if(i == lens[0])
2203
                FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
2204 086acdd5 Jeff Downs
        }
2205
    }else{
2206 d4f7d838 Michael Niedermayer
        len = build_def_list(h->default_ref_list[0]    , h->short_ref, h->short_ref_count, 0, s->picture_structure);
2207
        len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16                , 1, s->picture_structure);
2208
        assert(len <= 32);
2209
        if(len < h->ref_count[0])
2210
            memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
2211 0da71265 Michael Niedermayer
    }
2212 827c91bf Loic Le Loarer
#ifdef TRACE
2213
    for (i=0; i<h->ref_count[0]; i++) {
2214 a9c9a240 Michel Bardiaux
        tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
2215 827c91bf Loic Le Loarer
    }
2216 9f5c1037 Michael Niedermayer
    if(h->slice_type_nos==FF_B_TYPE){
2217 827c91bf Loic Le Loarer
        for (i=0; i<h->ref_count[1]; i++) {
2218 ffbc5e04 Loren Merritt
            tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].data[0]);
2219 827c91bf Loic Le Loarer
        }
2220
    }
2221
#endif
2222 0da71265 Michael Niedermayer
    return 0;
2223
}
2224
2225 827c91bf Loic Le Loarer
static void print_short_term(H264Context *h);
2226
static void print_long_term(H264Context *h);
2227
2228 949da388 Jeff Downs
/**
2229
 * Extract structure information about the picture described by pic_num in
2230
 * the current decoding context (frame or field). Note that pic_num is
2231
 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
2232
 * @param pic_num picture number for which to extract structure information
2233
 * @param structure one of PICT_XXX describing structure of picture
2234
 *                      with pic_num
2235
 * @return frame number (short term) or long term index of picture
2236
 *         described by pic_num
2237
 */
2238
static int pic_num_extract(H264Context *h, int pic_num, int *structure){
2239
    MpegEncContext * const s = &h->s;
2240
2241
    *structure = s->picture_structure;
2242
    if(FIELD_PICTURE){
2243
        if (!(pic_num & 1))
2244
            /* opposite field */
2245
            *structure ^= PICT_FRAME;
2246
        pic_num >>= 1;
2247
    }
2248
2249
    return pic_num;
2250
}
2251
2252 0da71265 Michael Niedermayer
static int decode_ref_pic_list_reordering(H264Context *h){
2253
    MpegEncContext * const s = &h->s;
2254 949da388 Jeff Downs
    int list, index, pic_structure;
2255 115329f1 Diego Biurrun
2256 827c91bf Loic Le Loarer
    print_short_term(h);
2257
    print_long_term(h);
2258 115329f1 Diego Biurrun
2259 3425501d Michael Niedermayer
    for(list=0; list<h->list_count; list++){
2260 0da71265 Michael Niedermayer
        memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
2261
2262
        if(get_bits1(&s->gb)){
2263
            int pred= h->curr_pic_num;
2264
2265
            for(index=0; ; index++){
2266 9963b332 Michael Niedermayer
                unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
2267 88e7a4d1 Michael Niedermayer
                unsigned int pic_id;
2268 0da71265 Michael Niedermayer
                int i;
2269 2f944356 Loren Merritt
                Picture *ref = NULL;
2270 115329f1 Diego Biurrun
2271
                if(reordering_of_pic_nums_idc==3)
2272 0bc42cad Loren Merritt
                    break;
2273 115329f1 Diego Biurrun
2274 0da71265 Michael Niedermayer
                if(index >= h->ref_count[list]){
2275 9b879566 Michel Bardiaux
                    av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
2276 0da71265 Michael Niedermayer
                    return -1;
2277
                }
2278 115329f1 Diego Biurrun
2279 0da71265 Michael Niedermayer
                if(reordering_of_pic_nums_idc<3){
2280
                    if(reordering_of_pic_nums_idc<2){
2281 88e7a4d1 Michael Niedermayer
                        const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
2282 949da388 Jeff Downs
                        int frame_num;
2283 0da71265 Michael Niedermayer
2284 03d3cab8 Jeff Downs
                        if(abs_diff_pic_num > h->max_pic_num){
2285 9b879566 Michel Bardiaux
                            av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
2286 0da71265 Michael Niedermayer
                            return -1;
2287
                        }
2288
2289
                        if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
2290
                        else                                pred+= abs_diff_pic_num;
2291
                        pred &= h->max_pic_num - 1;
2292 115329f1 Diego Biurrun
2293 949da388 Jeff Downs
                        frame_num = pic_num_extract(h, pred, &pic_structure);
2294
2295 0d175622 Michael Niedermayer
                        for(i= h->short_ref_count-1; i>=0; i--){
2296
                            ref = h->short_ref[i];
2297 949da388 Jeff Downs
                            assert(ref->reference);
2298 0d175622 Michael Niedermayer
                            assert(!ref->long_ref);
2299 6edac8e1 Michael Niedermayer
                            if(
2300 af8c5e08 Michael Niedermayer
                                   ref->frame_num == frame_num &&
2301
                                   (ref->reference & pic_structure)
2302 6edac8e1 Michael Niedermayer
                              )
2303 0da71265 Michael Niedermayer
                                break;
2304
                        }
2305 0d175622 Michael Niedermayer
                        if(i>=0)
2306 949da388 Jeff Downs
                            ref->pic_id= pred;
2307 0da71265 Michael Niedermayer
                    }else{
2308 949da388 Jeff Downs
                        int long_idx;
2309 0da71265 Michael Niedermayer
                        pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
2310 949da388 Jeff Downs
2311
                        long_idx= pic_num_extract(h, pic_id, &pic_structure);
2312
2313
                        if(long_idx>31){
2314 88e7a4d1 Michael Niedermayer
                            av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
2315
                            return -1;
2316
                        }
2317 949da388 Jeff Downs
                        ref = h->long_ref[long_idx];
2318
                        assert(!(ref && !ref->reference));
2319 af8c5e08 Michael Niedermayer
                        if(ref && (ref->reference & pic_structure)){
2320 ac658be5 Francois Oligny-Lemieux
                            ref->pic_id= pic_id;
2321
                            assert(ref->long_ref);
2322
                            i=0;
2323
                        }else{
2324
                            i=-1;
2325
                        }
2326 0da71265 Michael Niedermayer
                    }
2327
2328 0d315f28 Loïc Le Loarer
                    if (i < 0) {
2329 9b879566 Michel Bardiaux
                        av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
2330 0da71265 Michael Niedermayer
                        memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
2331 0d175622 Michael Niedermayer
                    } else {
2332
                        for(i=index; i+1<h->ref_count[list]; i++){
2333
                            if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
2334
                                break;
2335 21be92bf Michael Niedermayer
                        }
2336
                        for(; i > index; i--){
2337
                            h->ref_list[list][i]= h->ref_list[list][i-1];
2338
                        }
2339 0d175622 Michael Niedermayer
                        h->ref_list[list][index]= *ref;
2340 949da388 Jeff Downs
                        if (FIELD_PICTURE){
2341 2143b118 Jeff Downs
                            pic_as_field(&h->ref_list[list][index], pic_structure);
2342 949da388 Jeff Downs
                        }
2343 0da71265 Michael Niedermayer
                    }
2344 0bc42cad Loren Merritt
                }else{
2345 9b879566 Michel Bardiaux
                    av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
2346 0da71265 Michael Niedermayer
                    return -1;
2347
                }
2348
            }
2349
        }
2350
    }
2351 3425501d Michael Niedermayer
    for(list=0; list<h->list_count; list++){
2352 6ab87211 Michael Niedermayer
        for(index= 0; index < h->ref_count[list]; index++){
2353 79b5c776 Michael Niedermayer
            if(!h->ref_list[list][index].data[0]){
2354
                av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
2355 ead79335 Michael Niedermayer
                if(h->default_ref_list[list][0].data[0])
2356
                    h->ref_list[list][index]= h->default_ref_list[list][0];
2357
                else
2358
                    return -1;
2359 79b5c776 Michael Niedermayer
            }
2360 6ab87211 Michael Niedermayer
        }
2361
    }
2362 115329f1 Diego Biurrun
2363
    return 0;
2364 0da71265 Michael Niedermayer
}
2365
2366 91c58c94 Måns Rullgård
static void fill_mbaff_ref_list(H264Context *h){
2367 5d18eaad Loren Merritt
    int list, i, j;
2368 3425501d Michael Niedermayer
    for(list=0; list<2; list++){ //FIXME try list_count
2369 5d18eaad Loren Merritt
        for(i=0; i<h->ref_count[list]; i++){
2370
            Picture *frame = &h->ref_list[list][i];
2371
            Picture *field = &h->ref_list[list][16+2*i];
2372
            field[0] = *frame;
2373
            for(j=0; j<3; j++)
2374
                field[0].linesize[j] <<= 1;
2375 2143b118 Jeff Downs
            field[0].reference = PICT_TOP_FIELD;
2376 078f42dd Michael Niedermayer
            field[0].poc= field[0].field_poc[0];
2377 5d18eaad Loren Merritt
            field[1] = field[0];
2378
            for(j=0; j<3; j++)
2379
                field[1].data[j] += frame->linesize[j];
2380 2143b118 Jeff Downs
            field[1].reference = PICT_BOTTOM_FIELD;
2381 078f42dd Michael Niedermayer
            field[1].poc= field[1].field_poc[1];
2382 5d18eaad Loren Merritt
2383
            h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
2384
            h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
2385
            for(j=0; j<2; j++){
2386
                h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
2387
                h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
2388
            }
2389
        }
2390
    }
2391
    for(j=0; j<h->ref_count[1]; j++){
2392
        for(i=0; i<h->ref_count[0]; i++)
2393
            h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
2394
        memcpy(h->implicit_weight[16+2*j],   h->implicit_weight[j], sizeof(*h->implicit_weight));
2395
        memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
2396
    }
2397
}
2398
2399 0da71265 Michael Niedermayer
static int pred_weight_table(H264Context *h){
2400
    MpegEncContext * const s = &h->s;
2401
    int list, i;
2402 9f2d1b4f Loren Merritt
    int luma_def, chroma_def;
2403 115329f1 Diego Biurrun
2404 9f2d1b4f Loren Merritt
    h->use_weight= 0;
2405
    h->use_weight_chroma= 0;
2406 0da71265 Michael Niedermayer
    h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
2407
    h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
2408 9f2d1b4f Loren Merritt
    luma_def = 1<<h->luma_log2_weight_denom;
2409
    chroma_def = 1<<h->chroma_log2_weight_denom;
2410 0da71265 Michael Niedermayer
2411
    for(list=0; list<2; list++){
2412 cb99c652 Gwenole Beauchesne
        h->luma_weight_flag[list]   = 0;
2413
        h->chroma_weight_flag[list] = 0;
2414 0da71265 Michael Niedermayer
        for(i=0; i<h->ref_count[list]; i++){
2415
            int luma_weight_flag, chroma_weight_flag;
2416 115329f1 Diego Biurrun
2417 0da71265 Michael Niedermayer
            luma_weight_flag= get_bits1(&s->gb);
2418
            if(luma_weight_flag){
2419
                h->luma_weight[list][i]= get_se_golomb(&s->gb);
2420
                h->luma_offset[list][i]= get_se_golomb(&s->gb);
2421 9f2d1b4f Loren Merritt
                if(   h->luma_weight[list][i] != luma_def
2422 cb99c652 Gwenole Beauchesne
                   || h->luma_offset[list][i] != 0) {
2423 9f2d1b4f Loren Merritt
                    h->use_weight= 1;
2424 cb99c652 Gwenole Beauchesne
                    h->luma_weight_flag[list]= 1;
2425
                }
2426 9f2d1b4f Loren Merritt
            }else{
2427
                h->luma_weight[list][i]= luma_def;
2428
                h->luma_offset[list][i]= 0;
2429 0da71265 Michael Niedermayer
            }
2430
2431 0af6967e Michael Niedermayer
            if(CHROMA){
2432 fef744d4 Michael Niedermayer
                chroma_weight_flag= get_bits1(&s->gb);
2433
                if(chroma_weight_flag){
2434
                    int j;
2435
                    for(j=0; j<2; j++){
2436
                        h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
2437
                        h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
2438
                        if(   h->chroma_weight[list][i][j] != chroma_def
2439 cb99c652 Gwenole Beauchesne
                           || h->chroma_offset[list][i][j] != 0) {
2440 fef744d4 Michael Niedermayer
                            h->use_weight_chroma= 1;
2441 cb99c652 Gwenole Beauchesne
                            h->chroma_weight_flag[list]= 1;
2442
                        }
2443 fef744d4 Michael Niedermayer
                    }
2444
                }else{
2445
                    int j;
2446
                    for(j=0; j<2; j++){
2447
                        h->chroma_weight[list][i][j]= chroma_def;
2448
                        h->chroma_offset[list][i][j]= 0;
2449
                    }
2450 0da71265 Michael Niedermayer
                }
2451
            }
2452
        }
2453 9f5c1037 Michael Niedermayer
        if(h->slice_type_nos != FF_B_TYPE) break;
2454 0da71265 Michael Niedermayer
    }
2455 9f2d1b4f Loren Merritt
    h->use_weight= h->use_weight || h->use_weight_chroma;
2456 0da71265 Michael Niedermayer
    return 0;
2457
}
2458
2459 9f2d1b4f Loren Merritt
static void implicit_weight_table(H264Context *h){
2460
    MpegEncContext * const s = &h->s;
2461 cb99c652 Gwenole Beauchesne
    int ref0, ref1, i;
2462 9f2d1b4f Loren Merritt
    int cur_poc = s->current_picture_ptr->poc;
2463
2464 ce09f927 Gwenole Beauchesne
    for (i = 0; i < 2; i++) {
2465
        h->luma_weight_flag[i]   = 0;
2466
        h->chroma_weight_flag[i] = 0;
2467
    }
2468
2469 9f2d1b4f Loren Merritt
    if(   h->ref_count[0] == 1 && h->ref_count[1] == 1
2470
       && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
2471
        h->use_weight= 0;
2472
        h->use_weight_chroma= 0;
2473
        return;
2474
    }
2475
2476
    h->use_weight= 2;
2477
    h->use_weight_chroma= 2;
2478
    h->luma_log2_weight_denom= 5;
2479
    h->chroma_log2_weight_denom= 5;
2480
2481
    for(ref0=0; ref0 < h->ref_count[0]; ref0++){
2482
        int poc0 = h->ref_list[0][ref0].poc;
2483
        for(ref1=0; ref1 < h->ref_count[1]; ref1++){
2484 738386a5 Loren Merritt
            int poc1 = h->ref_list[1][ref1].poc;
2485 f66e4f5f Reimar Döffinger
            int td = av_clip(poc1 - poc0, -128, 127);
2486 9f2d1b4f Loren Merritt
            if(td){
2487 f66e4f5f Reimar Döffinger
                int tb = av_clip(cur_poc - poc0, -128, 127);
2488 c26abfa5 Diego Biurrun
                int tx = (16384 + (FFABS(td) >> 1)) / td;
2489 f66e4f5f Reimar Döffinger
                int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
2490 9f2d1b4f Loren Merritt
                if(dist_scale_factor < -64 || dist_scale_factor > 128)
2491
                    h->implicit_weight[ref0][ref1] = 32;
2492
                else
2493
                    h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
2494
            }else
2495
                h->implicit_weight[ref0][ref1] = 32;
2496
        }
2497
    }
2498
}
2499
2500 8fd57a66 Jeff Downs
/**
2501
 * Mark a picture as no longer needed for reference. The refmask
2502
 * argument allows unreferencing of individual fields or the whole frame.
2503
 * If the picture becomes entirely unreferenced, but is being held for
2504
 * display purposes, it is marked as such.
2505
 * @param refmask mask of fields to unreference; the mask is bitwise
2506
 *                anded with the reference marking of pic
2507
 * @return non-zero if pic becomes entirely unreferenced (except possibly
2508
 *         for display purposes) zero if one of the fields remains in
2509
 *         reference
2510
 */
2511
static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
2512 4e4d983e Loren Merritt
    int i;
2513 8fd57a66 Jeff Downs
    if (pic->reference &= refmask) {
2514
        return 0;
2515
    } else {
2516 79f4494a Michael Niedermayer
        for(i = 0; h->delayed_pic[i]; i++)
2517
            if(pic == h->delayed_pic[i]){
2518
                pic->reference=DELAYED_PIC_REF;
2519
                break;
2520
            }
2521 8fd57a66 Jeff Downs
        return 1;
2522
    }
2523 4e4d983e Loren Merritt
}
2524
2525 0da71265 Michael Niedermayer
/**
2526 5175b937 Loic Le Loarer
 * instantaneous decoder refresh.
2527 0da71265 Michael Niedermayer
 */
2528
static void idr(H264Context *h){
2529 4e4d983e Loren Merritt
    int i;
2530 0da71265 Michael Niedermayer
2531 dc032f33 Loïc Le Loarer
    for(i=0; i<16; i++){
2532 9c0e4624 Michael Niedermayer
        remove_long(h, i, 0);
2533 0da71265 Michael Niedermayer
    }
2534 849b9cef Michael Niedermayer
    assert(h->long_ref_count==0);
2535 0da71265 Michael Niedermayer
2536
    for(i=0; i<h->short_ref_count; i++){
2537 8fd57a66 Jeff Downs
        unreference_pic(h, h->short_ref[i], 0);
2538 0da71265 Michael Niedermayer
        h->short_ref[i]= NULL;
2539
    }
2540
    h->short_ref_count=0;
2541 a149c1a5 Michael Niedermayer
    h->prev_frame_num= 0;
2542 80f8e035 Michael Niedermayer
    h->prev_frame_num_offset= 0;
2543
    h->prev_poc_msb=
2544
    h->prev_poc_lsb= 0;
2545 0da71265 Michael Niedermayer
}
2546
2547 7c33ad19 Loren Merritt
/* forget old pics after a seek */
2548
static void flush_dpb(AVCodecContext *avctx){
2549
    H264Context *h= avctx->priv_data;
2550
    int i;
2551 64b9d48f Alexander Strange
    for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
2552 285b570f Loren Merritt
        if(h->delayed_pic[i])
2553
            h->delayed_pic[i]->reference= 0;
2554 7c33ad19 Loren Merritt
        h->delayed_pic[i]= NULL;
2555 285b570f Loren Merritt
    }
2556 df8a7dff Michael Niedermayer
    h->outputed_poc= INT_MIN;
2557 b19d493f Haruhiko Yamagata
    h->prev_interlaced_frame = 1;
2558 7c33ad19 Loren Merritt
    idr(h);
2559 ca159196 Måns Rullgård
    if(h->s.current_picture_ptr)
2560
        h->s.current_picture_ptr->reference= 0;
2561 12d96de3 Jeff Downs
    h->s.first_field= 0;
2562 03831f46 Ivan Schreter
    reset_sei(h);