h264: ff_h264_decode_extradata: check buffer args
[ffmpeg.git] / libavcodec / h264.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * 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  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include "libavutil/imgutils.h"
29 #include "internal.h"
30 #include "dsputil.h"
31 #include "avcodec.h"
32 #include "mpegvideo.h"
33 #include "h264.h"
34 #include "h264data.h"
35 #include "h264_mvpred.h"
36 #include "golomb.h"
37 #include "mathops.h"
38 #include "rectangle.h"
39 #include "thread.h"
40 #include "vdpau_internal.h"
41 #include "libavutil/avassert.h"
42
43 #include "cabac.h"
44
45 //#undef NDEBUG
46 #include <assert.h>
47
48 static const uint8_t rem6[QP_MAX_NUM+1]={
49 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, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
50 };
51
52 static const uint8_t div6[QP_MAX_NUM+1]={
53 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, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,
54 };
55
56 static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
57     PIX_FMT_DXVA2_VLD,
58     PIX_FMT_VAAPI_VLD,
59     PIX_FMT_YUVJ420P,
60     PIX_FMT_NONE
61 };
62
63 void ff_h264_write_back_intra_pred_mode(H264Context *h){
64     int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
65
66     AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
67     mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
68     mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
69     mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
70 }
71
72 /**
73  * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
74  */
75 int ff_h264_check_intra4x4_pred_mode(H264Context *h){
76     MpegEncContext * const s = &h->s;
77     static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
78     static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
79     int i;
80
81     if(!(h->top_samples_available&0x8000)){
82         for(i=0; i<4; i++){
83             int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
84             if(status<0){
85                 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
86                 return -1;
87             } else if(status){
88                 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
89             }
90         }
91     }
92
93     if((h->left_samples_available&0x8888)!=0x8888){
94         static const int mask[4]={0x8000,0x2000,0x80,0x20};
95         for(i=0; i<4; i++){
96             if(!(h->left_samples_available&mask[i])){
97                 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
98                 if(status<0){
99                     av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
100                     return -1;
101                 } else if(status){
102                     h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
103                 }
104             }
105         }
106     }
107
108     return 0;
109 } //FIXME cleanup like ff_h264_check_intra_pred_mode
110
111 /**
112  * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
113  */
114 int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
115     MpegEncContext * const s = &h->s;
116     static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
117     static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
118
119     if(mode > 6U) {
120         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);
121         return -1;
122     }
123
124     if(!(h->top_samples_available&0x8000)){
125         mode= top[ mode ];
126         if(mode<0){
127             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);
128             return -1;
129         }
130     }
131
132     if((h->left_samples_available&0x8080) != 0x8080){
133         mode= left[ mode ];
134         if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
135             mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
136         }
137         if(mode<0){
138             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);
139             return -1;
140         }
141     }
142
143     return mode;
144 }
145
146 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
147     int i, si, di;
148     uint8_t *dst;
149     int bufidx;
150
151 //    src[0]&0x80;                //forbidden bit
152     h->nal_ref_idc= src[0]>>5;
153     h->nal_unit_type= src[0]&0x1F;
154
155     src++; length--;
156
157 #if HAVE_FAST_UNALIGNED
158 # if HAVE_FAST_64BIT
159 #   define RS 7
160     for(i=0; i+1<length; i+=9){
161         if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
162 # else
163 #   define RS 3
164     for(i=0; i+1<length; i+=5){
165         if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
166 # endif
167             continue;
168         if(i>0 && !src[i]) i--;
169         while(src[i]) i++;
170 #else
171 #   define RS 0
172     for(i=0; i+1<length; i+=2){
173         if(src[i]) continue;
174         if(i>0 && src[i-1]==0) i--;
175 #endif
176         if(i+2<length && src[i+1]==0 && src[i+2]<=3){
177             if(src[i+2]!=3){
178                 /* startcode, so we must be past the end */
179                 length=i;
180             }
181             break;
182         }
183         i-= RS;
184     }
185
186     bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
187     si=h->rbsp_buffer_size[bufidx];
188     av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE+MAX_MBPAIR_SIZE);
189     dst= h->rbsp_buffer[bufidx];
190     if(si != h->rbsp_buffer_size[bufidx])
191         memset(dst + length, 0, FF_INPUT_BUFFER_PADDING_SIZE+MAX_MBPAIR_SIZE);
192
193     if (dst == NULL){
194         return NULL;
195     }
196
197     if(i>=length-1){ //no escaped 0
198         *dst_length= length;
199         *consumed= length+1; //+1 for the header
200         if(h->s.avctx->flags2 & CODEC_FLAG2_FAST){
201             return src;
202         }else{
203             memcpy(dst, src, length);
204             return dst;
205         }
206     }
207
208 //printf("decoding esc\n");
209     memcpy(dst, src, i);
210     si=di=i;
211     while(si+2<length){
212         //remove escapes (very rare 1:2^22)
213         if(src[si+2]>3){
214             dst[di++]= src[si++];
215             dst[di++]= src[si++];
216         }else if(src[si]==0 && src[si+1]==0){
217             if(src[si+2]==3){ //escape
218                 dst[di++]= 0;
219                 dst[di++]= 0;
220                 si+=3;
221                 continue;
222             }else //next start code
223                 goto nsc;
224         }
225
226         dst[di++]= src[si++];
227     }
228     while(si<length)
229         dst[di++]= src[si++];
230 nsc:
231
232     memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
233
234     *dst_length= di;
235     *consumed= si + 1;//+1 for the header
236 //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
237     return dst;
238 }
239
240 /**
241  * Identify the exact end of the bitstream
242  * @return the length of the trailing, or 0 if damaged
243  */
244 static int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
245     int v= *src;
246     int r;
247
248     tprintf(h->s.avctx, "rbsp trailing %X\n", v);
249
250     for(r=1; r<9; r++){
251         if(v&1) return r;
252         v>>=1;
253     }
254     return 0;
255 }
256
257 static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n, int height,
258                                  int y_offset, int list){
259     int raw_my= h->mv_cache[list][ scan8[n] ][1];
260     int filter_height= (raw_my&3) ? 2 : 0;
261     int full_my= (raw_my>>2) + y_offset;
262     int top = full_my - filter_height, bottom = full_my + height + filter_height;
263
264     return FFMAX(abs(top), bottom);
265 }
266
267 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n, int height,
268                                int y_offset, int list0, int list1, int *nrefs){
269     MpegEncContext * const s = &h->s;
270     int my;
271
272     y_offset += 16*(s->mb_y >> MB_FIELD);
273
274     if(list0){
275         int ref_n = h->ref_cache[0][ scan8[n] ];
276         Picture *ref= &h->ref_list[0][ref_n];
277
278         // Error resilience puts the current picture in the ref list.
279         // Don't try to wait on these as it will cause a deadlock.
280         // Fields can wait on each other, though.
281         if(ref->thread_opaque != s->current_picture.thread_opaque ||
282            (ref->reference&3) != s->picture_structure) {
283             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
284             if (refs[0][ref_n] < 0) nrefs[0] += 1;
285             refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
286         }
287     }
288
289     if(list1){
290         int ref_n = h->ref_cache[1][ scan8[n] ];
291         Picture *ref= &h->ref_list[1][ref_n];
292
293         if(ref->thread_opaque != s->current_picture.thread_opaque ||
294            (ref->reference&3) != s->picture_structure) {
295             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
296             if (refs[1][ref_n] < 0) nrefs[1] += 1;
297             refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
298         }
299     }
300 }
301
302 /**
303  * Wait until all reference frames are available for MC operations.
304  *
305  * @param h the H264 context
306  */
307 static void await_references(H264Context *h){
308     MpegEncContext * const s = &h->s;
309     const int mb_xy= h->mb_xy;
310     const int mb_type= s->current_picture.mb_type[mb_xy];
311     int refs[2][48];
312     int nrefs[2] = {0};
313     int ref, list;
314
315     memset(refs, -1, sizeof(refs));
316
317     if(IS_16X16(mb_type)){
318         get_lowest_part_y(h, refs, 0, 16, 0,
319                   IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
320     }else if(IS_16X8(mb_type)){
321         get_lowest_part_y(h, refs, 0, 8, 0,
322                   IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
323         get_lowest_part_y(h, refs, 8, 8, 8,
324                   IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
325     }else if(IS_8X16(mb_type)){
326         get_lowest_part_y(h, refs, 0, 16, 0,
327                   IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
328         get_lowest_part_y(h, refs, 4, 16, 0,
329                   IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
330     }else{
331         int i;
332
333         assert(IS_8X8(mb_type));
334
335         for(i=0; i<4; i++){
336             const int sub_mb_type= h->sub_mb_type[i];
337             const int n= 4*i;
338             int y_offset= (i&2)<<2;
339
340             if(IS_SUB_8X8(sub_mb_type)){
341                 get_lowest_part_y(h, refs, n  , 8, y_offset,
342                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
343             }else if(IS_SUB_8X4(sub_mb_type)){
344                 get_lowest_part_y(h, refs, n  , 4, y_offset,
345                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
346                 get_lowest_part_y(h, refs, n+2, 4, y_offset+4,
347                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
348             }else if(IS_SUB_4X8(sub_mb_type)){
349                 get_lowest_part_y(h, refs, n  , 8, y_offset,
350                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
351                 get_lowest_part_y(h, refs, n+1, 8, y_offset,
352                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
353             }else{
354                 int j;
355                 assert(IS_SUB_4X4(sub_mb_type));
356                 for(j=0; j<4; j++){
357                     int sub_y_offset= y_offset + 2*(j&2);
358                     get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,
359                               IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
360                 }
361             }
362         }
363     }
364
365     for(list=h->list_count-1; list>=0; list--){
366         for(ref=0; ref<48 && nrefs[list]; ref++){
367             int row = refs[list][ref];
368             if(row >= 0){
369                 Picture *ref_pic = &h->ref_list[list][ref];
370                 int ref_field = ref_pic->reference - 1;
371                 int ref_field_picture = ref_pic->field_picture;
372                 int pic_height = 16*s->mb_height >> ref_field_picture;
373
374                 row <<= MB_MBAFF;
375                 nrefs[list]--;
376
377                 if(!FIELD_PICTURE && ref_field_picture){ // frame referencing two fields
378                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);
379                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1)           , pic_height-1), 0);
380                 }else if(FIELD_PICTURE && !ref_field_picture){ // field referencing one field of a frame
381                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field    , pic_height-1), 0);
382                 }else if(FIELD_PICTURE){
383                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);
384                 }else{
385                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);
386                 }
387             }
388         }
389     }
390 }
391
392 #if 0
393 /**
394  * DCT transforms the 16 dc values.
395  * @param qp quantization parameter ??? FIXME
396  */
397 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
398 //    const int qmul= dequant_coeff[qp][0];
399     int i;
400     int temp[16]; //FIXME check if this is a good idea
401     static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
402     static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
403
404     for(i=0; i<4; i++){
405         const int offset= y_offset[i];
406         const int z0= block[offset+stride*0] + block[offset+stride*4];
407         const int z1= block[offset+stride*0] - block[offset+stride*4];
408         const int z2= block[offset+stride*1] - block[offset+stride*5];
409         const int z3= block[offset+stride*1] + block[offset+stride*5];
410
411         temp[4*i+0]= z0+z3;
412         temp[4*i+1]= z1+z2;
413         temp[4*i+2]= z1-z2;
414         temp[4*i+3]= z0-z3;
415     }
416
417     for(i=0; i<4; i++){
418         const int offset= x_offset[i];
419         const int z0= temp[4*0+i] + temp[4*2+i];
420         const int z1= temp[4*0+i] - temp[4*2+i];
421         const int z2= temp[4*1+i] - temp[4*3+i];
422         const int z3= temp[4*1+i] + temp[4*3+i];
423
424         block[stride*0 +offset]= (z0 + z3)>>1;
425         block[stride*2 +offset]= (z1 + z2)>>1;
426         block[stride*8 +offset]= (z1 - z2)>>1;
427         block[stride*10+offset]= (z0 - z3)>>1;
428     }
429 }
430 #endif
431
432 #undef xStride
433 #undef stride
434
435 #if 0
436 static void chroma_dc_dct_c(DCTELEM *block){
437     const int stride= 16*2;
438     const int xStride= 16;
439     int a,b,c,d,e;
440
441     a= block[stride*0 + xStride*0];
442     b= block[stride*0 + xStride*1];
443     c= block[stride*1 + xStride*0];
444     d= block[stride*1 + xStride*1];
445
446     e= a-b;
447     a= a+b;
448     b= c-d;
449     c= c+d;
450
451     block[stride*0 + xStride*0]= (a+c);
452     block[stride*0 + xStride*1]= (e+b);
453     block[stride*1 + xStride*0]= (a-c);
454     block[stride*1 + xStride*1]= (e-b);
455 }
456 #endif
457
458 static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
459                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
460                            int src_x_offset, int src_y_offset,
461                            qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op,
462                            int pixel_shift, int chroma444){
463     MpegEncContext * const s = &h->s;
464     const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
465     int my=       h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
466     const int luma_xy= (mx&3) + ((my&3)<<2);
467     int offset = ((mx>>2) << pixel_shift) + (my>>2)*h->mb_linesize;
468     uint8_t * src_y = pic->data[0] + offset;
469     uint8_t * src_cb, * src_cr;
470     int extra_width= h->emu_edge_width;
471     int extra_height= h->emu_edge_height;
472     int emu=0;
473     const int full_mx= mx>>2;
474     const int full_my= my>>2;
475     const int pic_width  = 16*s->mb_width;
476     const int pic_height = 16*s->mb_height >> MB_FIELD;
477
478     if(mx&7) extra_width -= 3;
479     if(my&7) extra_height -= 3;
480
481     if(   full_mx < 0-extra_width
482        || full_my < 0-extra_height
483        || full_mx + 16/*FIXME*/ > pic_width + extra_width
484        || full_my + 16/*FIXME*/ > pic_height + extra_height){
485         s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_y - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
486             src_y= s->edge_emu_buffer + (2 << pixel_shift) + 2*h->mb_linesize;
487         emu=1;
488     }
489
490     qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
491     if(!square){
492         qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
493     }
494
495     if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
496
497     if(chroma444){
498         src_cb = pic->data[1] + offset;
499         if(emu){
500             s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
501                                     16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
502             src_cb= s->edge_emu_buffer + (2 << pixel_shift) + 2*h->mb_linesize;
503         }
504         qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); //FIXME try variable height perhaps?
505         if(!square){
506             qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
507         }
508
509         src_cr = pic->data[2] + offset;
510         if(emu){
511             s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
512                                     16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
513             src_cr= s->edge_emu_buffer + (2 << pixel_shift) + 2*h->mb_linesize;
514         }
515         qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); //FIXME try variable height perhaps?
516         if(!square){
517             qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
518         }
519         return;
520     }
521
522     if(MB_FIELD){
523         // chroma offset when predicting from a field of opposite parity
524         my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
525         emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
526     }
527     src_cb= pic->data[1] + ((mx>>3) << pixel_shift) + (my>>3)*h->mb_uvlinesize;
528     src_cr= pic->data[2] + ((mx>>3) << pixel_shift) + (my>>3)*h->mb_uvlinesize;
529
530     if(emu){
531         s->dsp.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);
532             src_cb= s->edge_emu_buffer;
533     }
534     chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
535
536     if(emu){
537         s->dsp.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);
538             src_cr= s->edge_emu_buffer;
539     }
540     chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
541 }
542
543 static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
544                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
545                            int x_offset, int y_offset,
546                            qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
547                            qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
548                            int list0, int list1, int pixel_shift, int chroma444){
549     MpegEncContext * const s = &h->s;
550     qpel_mc_func *qpix_op=  qpix_put;
551     h264_chroma_mc_func chroma_op= chroma_put;
552
553     dest_y  += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
554     if(chroma444){
555         dest_cb += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
556         dest_cr += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
557     }else{
558         dest_cb += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
559         dest_cr += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
560     }
561     x_offset += 8*s->mb_x;
562     y_offset += 8*(s->mb_y >> MB_FIELD);
563
564     if(list0){
565         Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
566         mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
567                            dest_y, dest_cb, dest_cr, x_offset, y_offset,
568                            qpix_op, chroma_op, pixel_shift, chroma444);
569
570         qpix_op=  qpix_avg;
571         chroma_op= chroma_avg;
572     }
573
574     if(list1){
575         Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
576         mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
577                            dest_y, dest_cb, dest_cr, x_offset, y_offset,
578                            qpix_op, chroma_op, pixel_shift, chroma444);
579     }
580 }
581
582 static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
583                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
584                            int x_offset, int y_offset,
585                            qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
586                            h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
587                            h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
588                            int list0, int list1, int pixel_shift, int chroma444){
589     MpegEncContext * const s = &h->s;
590
591     dest_y += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
592     if(chroma444){
593         chroma_weight_avg = luma_weight_avg;
594         chroma_weight_op = luma_weight_op;
595         dest_cb += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
596         dest_cr += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
597     }else{
598         dest_cb += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
599         dest_cr += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
600     }
601     x_offset += 8*s->mb_x;
602     y_offset += 8*(s->mb_y >> MB_FIELD);
603
604     if(list0 && list1){
605         /* don't optimize for luma-only case, since B-frames usually
606          * use implicit weights => chroma too. */
607         uint8_t *tmp_cb = s->obmc_scratchpad;
608         uint8_t *tmp_cr = s->obmc_scratchpad + (16 << pixel_shift);
609         uint8_t *tmp_y  = s->obmc_scratchpad + 16*h->mb_uvlinesize;
610         int refn0 = h->ref_cache[0][ scan8[n] ];
611         int refn1 = h->ref_cache[1][ scan8[n] ];
612
613         mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
614                     dest_y, dest_cb, dest_cr,
615                     x_offset, y_offset, qpix_put, chroma_put, pixel_shift, chroma444);
616         mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
617                     tmp_y, tmp_cb, tmp_cr,
618                     x_offset, y_offset, qpix_put, chroma_put, pixel_shift, chroma444);
619
620         if(h->use_weight == 2){
621             int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
622             int weight1 = 64 - weight0;
623             luma_weight_avg(  dest_y,  tmp_y,  h->  mb_linesize, 5, weight0, weight1, 0);
624             chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
625             chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
626         }else{
627             luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
628                             h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
629                             h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
630             chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
631                             h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
632                             h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
633             chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
634                             h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
635                             h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
636         }
637     }else{
638         int list = list1 ? 1 : 0;
639         int refn = h->ref_cache[list][ scan8[n] ];
640         Picture *ref= &h->ref_list[list][refn];
641         mc_dir_part(h, ref, n, square, chroma_height, delta, list,
642                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
643                     qpix_put, chroma_put, pixel_shift, chroma444);
644
645         luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
646                        h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
647         if(h->use_weight_chroma){
648             chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
649                              h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
650             chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
651                              h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
652         }
653     }
654 }
655
656 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
657                            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
658                            int x_offset, int y_offset,
659                            qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
660                            qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
661                            h264_weight_func *weight_op, h264_biweight_func *weight_avg,
662                            int list0, int list1, int pixel_shift, int chroma444){
663     if((h->use_weight==2 && list0 && list1
664         && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
665        || h->use_weight==1)
666         mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
667                          x_offset, y_offset, qpix_put, chroma_put,
668                          weight_op[0], weight_op[3], weight_avg[0],
669                          weight_avg[3], list0, list1, pixel_shift, chroma444);
670     else
671         mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
672                     x_offset, y_offset, qpix_put, chroma_put, qpix_avg,
673                     chroma_avg, list0, list1, pixel_shift, chroma444);
674 }
675
676 static inline void prefetch_motion(H264Context *h, int list, int pixel_shift, int chroma444){
677     /* fetch pixels for estimated mv 4 macroblocks ahead
678      * optimized for 64byte cache lines */
679     MpegEncContext * const s = &h->s;
680     const int refn = h->ref_cache[list][scan8[0]];
681     if(refn >= 0){
682         const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
683         const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
684         uint8_t **src= h->ref_list[list][refn].data;
685         int off= ((mx+64)<<h->pixel_shift) + (my + (s->mb_x&3)*4)*h->mb_linesize;
686         s->dsp.prefetch(src[0]+off, s->linesize, 4);
687         if(chroma444){
688             s->dsp.prefetch(src[1]+off, s->linesize, 4);
689             s->dsp.prefetch(src[2]+off, s->linesize, 4);
690         }else{
691             off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (s->mb_x&7))*s->uvlinesize;
692             s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
693         }
694     }
695 }
696
697 static av_always_inline void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
698                       qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
699                       qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
700                       h264_weight_func *weight_op, h264_biweight_func *weight_avg,
701                       int pixel_shift, int chroma444){
702     MpegEncContext * const s = &h->s;
703     const int mb_xy= h->mb_xy;
704     const int mb_type= s->current_picture.mb_type[mb_xy];
705
706     assert(IS_INTER(mb_type));
707
708     if(HAVE_PTHREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
709         await_references(h);
710     prefetch_motion(h, 0, pixel_shift, chroma444);
711
712     if(IS_16X16(mb_type)){
713         mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
714                 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
715                 weight_op, weight_avg,
716                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
717                 pixel_shift, chroma444);
718     }else if(IS_16X8(mb_type)){
719         mc_part(h, 0, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,
720                 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
721                 &weight_op[1], &weight_avg[1],
722                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
723                 pixel_shift, chroma444);
724         mc_part(h, 8, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,
725                 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
726                 &weight_op[1], &weight_avg[1],
727                 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
728                 pixel_shift, chroma444);
729     }else if(IS_8X16(mb_type)){
730         mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
731                 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
732                 &weight_op[2], &weight_avg[2],
733                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
734                 pixel_shift, chroma444);
735         mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
736                 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
737                 &weight_op[2], &weight_avg[2],
738                 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
739                 pixel_shift, chroma444);
740     }else{
741         int i;
742
743         assert(IS_8X8(mb_type));
744
745         for(i=0; i<4; i++){
746             const int sub_mb_type= h->sub_mb_type[i];
747             const int n= 4*i;
748             int x_offset= (i&1)<<2;
749             int y_offset= (i&2)<<1;
750
751             if(IS_SUB_8X8(sub_mb_type)){
752                 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
753                     qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
754                     &weight_op[3], &weight_avg[3],
755                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
756                     pixel_shift, chroma444);
757             }else if(IS_SUB_8X4(sub_mb_type)){
758                 mc_part(h, n  , 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset,
759                     qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
760                     &weight_op[4], &weight_avg[4],
761                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
762                     pixel_shift, chroma444);
763                 mc_part(h, n+2, 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
764                     qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
765                     &weight_op[4], &weight_avg[4],
766                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
767                     pixel_shift, chroma444);
768             }else if(IS_SUB_4X8(sub_mb_type)){
769                 mc_part(h, n  , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
770                     qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
771                     &weight_op[5], &weight_avg[5],
772                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
773                     pixel_shift, chroma444);
774                 mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
775                     qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
776                     &weight_op[5], &weight_avg[5],
777                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
778                     pixel_shift, chroma444);
779             }else{
780                 int j;
781                 assert(IS_SUB_4X4(sub_mb_type));
782                 for(j=0; j<4; j++){
783                     int sub_x_offset= x_offset + 2*(j&1);
784                     int sub_y_offset= y_offset +   (j&2);
785                     mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
786                         qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
787                         &weight_op[6], &weight_avg[6],
788                         IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
789                         pixel_shift, chroma444);
790                 }
791             }
792         }
793     }
794
795     prefetch_motion(h, 1, pixel_shift, chroma444);
796 }
797
798 #define hl_motion_fn(sh, bits) \
799 static av_always_inline void hl_motion_ ## bits(H264Context *h, \
800                                        uint8_t *dest_y, \
801                                        uint8_t *dest_cb, uint8_t *dest_cr, \
802                                        qpel_mc_func (*qpix_put)[16], \
803                                        h264_chroma_mc_func (*chroma_put), \
804                                        qpel_mc_func (*qpix_avg)[16], \
805                                        h264_chroma_mc_func (*chroma_avg), \
806                                        h264_weight_func *weight_op, \
807                                        h264_biweight_func *weight_avg, \
808                                        int chroma444) \
809 { \
810     hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put, \
811               qpix_avg, chroma_avg, weight_op, weight_avg, sh, chroma444); \
812 }
813 hl_motion_fn(0, 8);
814 hl_motion_fn(1, 16);
815
816 static void free_tables(H264Context *h, int free_rbsp){
817     int i;
818     H264Context *hx;
819
820     av_freep(&h->intra4x4_pred_mode);
821     av_freep(&h->chroma_pred_mode_table);
822     av_freep(&h->cbp_table);
823     av_freep(&h->mvd_table[0]);
824     av_freep(&h->mvd_table[1]);
825     av_freep(&h->direct_table);
826     av_freep(&h->non_zero_count);
827     av_freep(&h->slice_table_base);
828     h->slice_table= NULL;
829     av_freep(&h->list_counts);
830
831     av_freep(&h->mb2b_xy);
832     av_freep(&h->mb2br_xy);
833
834     for(i = 0; i < MAX_THREADS; i++) {
835         hx = h->thread_context[i];
836         if(!hx) continue;
837         av_freep(&hx->top_borders[1]);
838         av_freep(&hx->top_borders[0]);
839         av_freep(&hx->s.obmc_scratchpad);
840         if (free_rbsp){
841             av_freep(&hx->rbsp_buffer[1]);
842             av_freep(&hx->rbsp_buffer[0]);
843             hx->rbsp_buffer_size[0] = 0;
844             hx->rbsp_buffer_size[1] = 0;
845         }
846         if (i) av_freep(&h->thread_context[i]);
847     }
848 }
849
850 static void init_dequant8_coeff_table(H264Context *h){
851     int i,j,q,x;
852     const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
853
854     for(i=0; i<6; i++ ){
855         h->dequant8_coeff[i] = h->dequant8_buffer[i];
856         for(j=0; j<i; j++){
857             if(!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i], 64*sizeof(uint8_t))){
858                 h->dequant8_coeff[i] = h->dequant8_buffer[j];
859                 break;
860             }
861         }
862         if(j<i)
863             continue;
864
865         for(q=0; q<max_qp+1; q++){
866             int shift = div6[q];
867             int idx = rem6[q];
868             for(x=0; x<64; x++)
869                 h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
870                     ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
871                     h->pps.scaling_matrix8[i][x]) << shift;
872         }
873     }
874 }
875
876 static void init_dequant4_coeff_table(H264Context *h){
877     int i,j,q,x;
878     const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
879     for(i=0; i<6; i++ ){
880         h->dequant4_coeff[i] = h->dequant4_buffer[i];
881         for(j=0; j<i; j++){
882             if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
883                 h->dequant4_coeff[i] = h->dequant4_buffer[j];
884                 break;
885             }
886         }
887         if(j<i)
888             continue;
889
890         for(q=0; q<max_qp+1; q++){
891             int shift = div6[q] + 2;
892             int idx = rem6[q];
893             for(x=0; x<16; x++)
894                 h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
895                     ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
896                     h->pps.scaling_matrix4[i][x]) << shift;
897         }
898     }
899 }
900
901 static void init_dequant_tables(H264Context *h){
902     int i,x;
903     init_dequant4_coeff_table(h);
904     if(h->pps.transform_8x8_mode)
905         init_dequant8_coeff_table(h);
906     if(h->sps.transform_bypass){
907         for(i=0; i<6; i++)
908             for(x=0; x<16; x++)
909                 h->dequant4_coeff[i][0][x] = 1<<6;
910         if(h->pps.transform_8x8_mode)
911             for(i=0; i<6; i++)
912                 for(x=0; x<64; x++)
913                     h->dequant8_coeff[i][0][x] = 1<<6;
914     }
915 }
916
917
918 int ff_h264_alloc_tables(H264Context *h){
919     MpegEncContext * const s = &h->s;
920     const int big_mb_num= s->mb_stride * (s->mb_height+1);
921     const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
922     int x,y;
923
924     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8  * sizeof(uint8_t), fail)
925
926     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count    , big_mb_num * 48 * sizeof(uint8_t), fail)
927     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base  , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
928     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
929
930     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
931     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
932     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
933     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
934     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
935
936     memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride)  * sizeof(*h->slice_table_base));
937     h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
938
939     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy  , big_mb_num * sizeof(uint32_t), fail);
940     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
941     for(y=0; y<s->mb_height; y++){
942         for(x=0; x<s->mb_width; x++){
943             const int mb_xy= x + y*s->mb_stride;
944             const int b_xy = 4*x + 4*y*h->b_stride;
945
946             h->mb2b_xy [mb_xy]= b_xy;
947             h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
948         }
949     }
950
951     s->obmc_scratchpad = NULL;
952
953     if(!h->dequant4_coeff[0])
954         init_dequant_tables(h);
955
956     return 0;
957 fail:
958     free_tables(h, 1);
959     return -1;
960 }
961
962 /**
963  * Mimic alloc_tables(), but for every context thread.
964  */
965 static void clone_tables(H264Context *dst, H264Context *src, int i){
966     MpegEncContext * const s = &src->s;
967     dst->intra4x4_pred_mode       = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
968     dst->non_zero_count           = src->non_zero_count;
969     dst->slice_table              = src->slice_table;
970     dst->cbp_table                = src->cbp_table;
971     dst->mb2b_xy                  = src->mb2b_xy;
972     dst->mb2br_xy                 = src->mb2br_xy;
973     dst->chroma_pred_mode_table   = src->chroma_pred_mode_table;
974     dst->mvd_table[0]             = src->mvd_table[0] + i*8*2*s->mb_stride;
975     dst->mvd_table[1]             = src->mvd_table[1] + i*8*2*s->mb_stride;
976     dst->direct_table             = src->direct_table;
977     dst->list_counts              = src->list_counts;
978
979     dst->s.obmc_scratchpad = NULL;
980     ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma);
981 }
982
983 /**
984  * Init context
985  * Allocate buffers which are not shared amongst multiple threads.
986  */
987 static int context_init(H264Context *h){
988     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * 16*3 * sizeof(uint8_t)*2, fail)
989     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * 16*3 * sizeof(uint8_t)*2, fail)
990
991     h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
992     h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
993
994     return 0;
995 fail:
996     return -1; // free_tables will clean up for us
997 }
998
999 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
1000
1001 static av_cold void common_init(H264Context *h){
1002     MpegEncContext * const s = &h->s;
1003
1004     s->width = s->avctx->width;
1005     s->height = s->avctx->height;
1006     s->codec_id= s->avctx->codec->id;
1007
1008     ff_h264dsp_init(&h->h264dsp, 8);
1009     ff_h264_pred_init(&h->hpc, s->codec_id, 8);
1010
1011     h->dequant_coeff_pps= -1;
1012     s->unrestricted_mv=1;
1013     s->decode=1; //FIXME
1014
1015     dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
1016
1017     memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
1018     memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
1019 }
1020
1021 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
1022 {
1023     AVCodecContext *avctx = h->s.avctx;
1024
1025     if(!buf || size <= 0)
1026         return -1;
1027
1028     if(buf[0] == 1){
1029         int i, cnt, nalsize;
1030         const unsigned char *p = buf;
1031
1032         h->is_avc = 1;
1033
1034         if(size < 7) {
1035             av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
1036             return -1;
1037         }
1038         /* sps and pps in the avcC always have length coded with 2 bytes,
1039            so put a fake nal_length_size = 2 while parsing them */
1040         h->nal_length_size = 2;
1041         // Decode sps from avcC
1042         cnt = *(p+5) & 0x1f; // Number of sps
1043         p += 6;
1044         for (i = 0; i < cnt; i++) {
1045             nalsize = AV_RB16(p) + 2;
1046             if(nalsize > size - (p-buf))
1047                 return -1;
1048             if(decode_nal_units(h, p, nalsize) < 0) {
1049                 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
1050                 return -1;
1051             }
1052             p += nalsize;
1053         }
1054         // Decode pps from avcC
1055         cnt = *(p++); // Number of pps
1056         for (i = 0; i < cnt; i++) {
1057             nalsize = AV_RB16(p) + 2;
1058             if(nalsize > size - (p-buf))
1059                 return -1;
1060             if (decode_nal_units(h, p, nalsize) < 0) {
1061                 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
1062                 return -1;
1063             }
1064             p += nalsize;
1065         }
1066         // Now store right nal length size, that will be use to parse all other nals
1067         h->nal_length_size = (buf[4] & 0x03) + 1;
1068     } else {
1069         h->is_avc = 0;
1070         if(decode_nal_units(h, buf, size) < 0)
1071             return -1;
1072     }
1073     return 0;
1074 }
1075
1076 av_cold int ff_h264_decode_init(AVCodecContext *avctx){
1077     H264Context *h= avctx->priv_data;
1078     MpegEncContext * const s = &h->s;
1079
1080     MPV_decode_defaults(s);
1081
1082     s->avctx = avctx;
1083     common_init(h);
1084
1085     s->out_format = FMT_H264;
1086     s->workaround_bugs= avctx->workaround_bugs;
1087
1088     // set defaults
1089 //    s->decode_mb= ff_h263_decode_mb;
1090     s->quarter_sample = 1;
1091     if(!avctx->has_b_frames)
1092     s->low_delay= 1;
1093
1094     avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1095
1096     ff_h264_decode_init_vlc();
1097
1098     h->pixel_shift = 0;
1099     h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
1100
1101     h->thread_context[0] = h;
1102     h->outputed_poc = h->next_outputed_poc = INT_MIN;
1103     h->prev_poc_msb= 1<<16;
1104     h->x264_build = -1;
1105     ff_h264_reset_sei(h);
1106     if(avctx->codec_id == CODEC_ID_H264){
1107         if(avctx->ticks_per_frame == 1){
1108             s->avctx->time_base.den *=2;
1109         }
1110         avctx->ticks_per_frame = 2;
1111     }
1112
1113     if(avctx->extradata_size > 0 && avctx->extradata &&
1114         ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size))
1115         return -1;
1116
1117     if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
1118         s->avctx->has_b_frames = h->sps.num_reorder_frames;
1119         s->low_delay = 0;
1120     }
1121
1122     return 0;
1123 }
1124
1125 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b)+(size))))
1126 static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
1127 {
1128     int i;
1129
1130     for (i=0; i<count; i++){
1131         assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
1132                 IN_RANGE(from[i], old_base->picture, sizeof(Picture) * old_base->picture_count) ||
1133                 !from[i]));
1134         to[i] = REBASE_PICTURE(from[i], new_base, old_base);
1135     }
1136 }
1137
1138 static void copy_parameter_set(void **to, void **from, int count, int size)
1139 {
1140     int i;
1141
1142     for (i=0; i<count; i++){
1143         if (to[i] && !from[i]) av_freep(&to[i]);
1144         else if (from[i] && !to[i]) to[i] = av_malloc(size);
1145
1146         if (from[i]) memcpy(to[i], from[i], size);
1147     }
1148 }
1149
1150 static int decode_init_thread_copy(AVCodecContext *avctx){
1151     H264Context *h= avctx->priv_data;
1152
1153     if (!avctx->is_copy) return 0;
1154     memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1155     memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1156
1157     return 0;
1158 }
1159
1160 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1161 static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
1162     H264Context *h= dst->priv_data, *h1= src->priv_data;
1163     MpegEncContext * const s = &h->s, * const s1 = &h1->s;
1164     int inited = s->context_initialized, err;
1165     int i;
1166
1167     if(dst == src || !s1->context_initialized) return 0;
1168
1169     err = ff_mpeg_update_thread_context(dst, src);
1170     if(err) return err;
1171
1172     //FIXME handle width/height changing
1173     if(!inited){
1174         for(i = 0; i < MAX_SPS_COUNT; i++)
1175             av_freep(h->sps_buffers + i);
1176
1177         for(i = 0; i < MAX_PPS_COUNT; i++)
1178             av_freep(h->pps_buffers + i);
1179
1180         memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
1181         memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1182         memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1183         if (ff_h264_alloc_tables(h) < 0) {
1184             av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
1185             return AVERROR(ENOMEM);
1186         }
1187         context_init(h);
1188
1189         for(i=0; i<2; i++){
1190             h->rbsp_buffer[i] = NULL;
1191             h->rbsp_buffer_size[i] = 0;
1192         }
1193
1194         h->thread_context[0] = h;
1195
1196         // frame_start may not be called for the next thread (if it's decoding a bottom field)
1197         // so this has to be allocated here
1198         h->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
1199
1200         s->dsp.clear_blocks(h->mb);
1201         s->dsp.clear_blocks(h->mb+(24*16<<h->pixel_shift));
1202     }
1203
1204     //extradata/NAL handling
1205     h->is_avc          = h1->is_avc;
1206
1207     //SPS/PPS
1208     copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
1209     h->sps             = h1->sps;
1210     copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
1211     h->pps             = h1->pps;
1212
1213     //Dequantization matrices
1214     //FIXME these are big - can they be only copied when PPS changes?
1215     copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1216
1217     for(i=0; i<6; i++)
1218         h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1219
1220     for(i=0; i<6; i++)
1221         h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1222
1223     h->dequant_coeff_pps = h1->dequant_coeff_pps;
1224
1225     //POC timing
1226     copy_fields(h, h1, poc_lsb, redundant_pic_count);
1227
1228     //reference lists
1229     copy_fields(h, h1, ref_count, list_count);
1230     copy_fields(h, h1, ref_list,  intra_gb);
1231     copy_fields(h, h1, short_ref, cabac_init_idc);
1232
1233     copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1);
1234     copy_picture_range(h->long_ref,    h1->long_ref,    32, s, s1);
1235     copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
1236
1237     h->last_slice_type = h1->last_slice_type;
1238
1239     if(!s->current_picture_ptr) return 0;
1240
1241     if(!s->dropable) {
1242         ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1243         h->prev_poc_msb     = h->poc_msb;
1244         h->prev_poc_lsb     = h->poc_lsb;
1245     }
1246     h->prev_frame_num_offset= h->frame_num_offset;
1247     h->prev_frame_num       = h->frame_num;
1248     h->outputed_poc         = h->next_outputed_poc;
1249
1250     return 0;
1251 }
1252
1253 int ff_h264_frame_start(H264Context *h){
1254     MpegEncContext * const s = &h->s;
1255     int i;
1256     const int pixel_shift = h->pixel_shift;
1257     int thread_count = (s->avctx->active_thread_type & FF_THREAD_SLICE) ? s->avctx->thread_count : 1;
1258
1259     if(MPV_frame_start(s, s->avctx) < 0)
1260         return -1;
1261     ff_er_frame_start(s);
1262     /*
1263      * MPV_frame_start uses pict_type to derive key_frame.
1264      * This is incorrect for H.264; IDR markings must be used.
1265      * Zero here; IDR markings per slice in frame or fields are ORed in later.
1266      * See decode_nal_units().
1267      */
1268     s->current_picture_ptr->key_frame= 0;
1269     s->current_picture_ptr->mmco_reset= 0;
1270
1271     assert(s->linesize && s->uvlinesize);
1272
1273     for(i=0; i<16; i++){
1274         h->block_offset[i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
1275         h->block_offset[48+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
1276     }
1277     for(i=0; i<16; i++){
1278         h->block_offset[16+i]=
1279         h->block_offset[32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
1280         h->block_offset[48+16+i]=
1281         h->block_offset[48+32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
1282     }
1283
1284     /* can't be in alloc_tables because linesize isn't known there.
1285      * FIXME: redo bipred weight to not require extra buffer? */
1286     for(i = 0; i < thread_count; i++)
1287         if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
1288             h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
1289
1290     /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
1291     memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
1292
1293 //    s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
1294
1295     // We mark the current picture as non-reference after allocating it, so
1296     // that if we break out due to an error it can be released automatically
1297     // in the next MPV_frame_start().
1298     // SVQ3 as well as most other codecs have only last/next/current and thus
1299     // get released even with set reference, besides SVQ3 and others do not
1300     // mark frames as reference later "naturally".
1301     if(s->codec_id != CODEC_ID_SVQ3)
1302         s->current_picture_ptr->reference= 0;
1303
1304     s->current_picture_ptr->field_poc[0]=
1305     s->current_picture_ptr->field_poc[1]= INT_MAX;
1306
1307     h->next_output_pic = NULL;
1308
1309     assert(s->current_picture_ptr->long_ref==0);
1310
1311     return 0;
1312 }
1313
1314 /**
1315   * Run setup operations that must be run after slice header decoding.
1316   * This includes finding the next displayed frame.
1317   *
1318   * @param h h264 master context
1319   * @param setup_finished enough NALs have been read that we can call
1320   * ff_thread_finish_setup()
1321   */
1322 static void decode_postinit(H264Context *h, int setup_finished){
1323     MpegEncContext * const s = &h->s;
1324     Picture *out = s->current_picture_ptr;
1325     Picture *cur = s->current_picture_ptr;
1326     int i, pics, out_of_order, out_idx;
1327
1328     s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
1329     s->current_picture_ptr->pict_type= s->pict_type;
1330
1331     if (h->next_output_pic) return;
1332
1333     if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
1334         //FIXME: if we have two PAFF fields in one packet, we can't start the next thread here.
1335         //If we have one field per packet, we can. The check in decode_nal_units() is not good enough
1336         //to find this yet, so we assume the worst for now.
1337         //if (setup_finished)
1338         //    ff_thread_finish_setup(s->avctx);
1339         return;
1340     }
1341
1342     cur->interlaced_frame = 0;
1343     cur->repeat_pict = 0;
1344
1345     /* Signal interlacing information externally. */
1346     /* Prioritize picture timing SEI information over used decoding process if it exists. */
1347
1348     if(h->sps.pic_struct_present_flag){
1349         switch (h->sei_pic_struct)
1350         {
1351         case SEI_PIC_STRUCT_FRAME:
1352             break;
1353         case SEI_PIC_STRUCT_TOP_FIELD:
1354         case SEI_PIC_STRUCT_BOTTOM_FIELD:
1355             cur->interlaced_frame = 1;
1356             break;
1357         case SEI_PIC_STRUCT_TOP_BOTTOM:
1358         case SEI_PIC_STRUCT_BOTTOM_TOP:
1359             if (FIELD_OR_MBAFF_PICTURE)
1360                 cur->interlaced_frame = 1;
1361             else
1362                 // try to flag soft telecine progressive
1363                 cur->interlaced_frame = h->prev_interlaced_frame;
1364             break;
1365         case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1366         case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1367             // Signal the possibility of telecined film externally (pic_struct 5,6)
1368             // From these hints, let the applications decide if they apply deinterlacing.
1369             cur->repeat_pict = 1;
1370             break;
1371         case SEI_PIC_STRUCT_FRAME_DOUBLING:
1372             // Force progressive here, as doubling interlaced frame is a bad idea.
1373             cur->repeat_pict = 2;
1374             break;
1375         case SEI_PIC_STRUCT_FRAME_TRIPLING:
1376             cur->repeat_pict = 4;
1377             break;
1378         }
1379
1380         if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
1381             cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
1382     }else{
1383         /* Derive interlacing flag from used decoding process. */
1384         cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
1385     }
1386     h->prev_interlaced_frame = cur->interlaced_frame;
1387
1388     if (cur->field_poc[0] != cur->field_poc[1]){
1389         /* Derive top_field_first from field pocs. */
1390         cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
1391     }else{
1392         if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
1393             /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
1394             if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
1395               || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1396                 cur->top_field_first = 1;
1397             else
1398                 cur->top_field_first = 0;
1399         }else{
1400             /* Most likely progressive */
1401             cur->top_field_first = 0;
1402         }
1403     }
1404
1405     //FIXME do something with unavailable reference frames
1406
1407     /* Sort B-frames into display order */
1408
1409     if(h->sps.bitstream_restriction_flag
1410        && s->avctx->has_b_frames < h->sps.num_reorder_frames){
1411         s->avctx->has_b_frames = h->sps.num_reorder_frames;
1412         s->low_delay = 0;
1413     }
1414
1415     if(   s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
1416        && !h->sps.bitstream_restriction_flag){
1417         s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
1418         s->low_delay= 0;
1419     }
1420
1421     pics = 0;
1422     while(h->delayed_pic[pics]) pics++;
1423
1424     av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
1425
1426     h->delayed_pic[pics++] = cur;
1427     if(cur->reference == 0)
1428         cur->reference = DELAYED_PIC_REF;
1429
1430     out = h->delayed_pic[0];
1431     out_idx = 0;
1432     for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
1433         if(h->delayed_pic[i]->poc < out->poc){
1434             out = h->delayed_pic[i];
1435             out_idx = i;
1436         }
1437     if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
1438         h->next_outputed_poc= INT_MIN;
1439     out_of_order = out->poc < h->next_outputed_poc;
1440
1441     if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
1442         { }
1443     else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
1444        || (s->low_delay &&
1445         ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
1446          || cur->pict_type == AV_PICTURE_TYPE_B)))
1447     {
1448         s->low_delay = 0;
1449         s->avctx->has_b_frames++;
1450     }
1451
1452     if(out_of_order || pics > s->avctx->has_b_frames){
1453         out->reference &= ~DELAYED_PIC_REF;
1454         out->owner2 = s; // for frame threading, the owner must be the second field's thread
1455                          // or else the first thread can release the picture and reuse it unsafely
1456         for(i=out_idx; h->delayed_pic[i]; i++)
1457             h->delayed_pic[i] = h->delayed_pic[i+1];
1458     }
1459     if(!out_of_order && pics > s->avctx->has_b_frames){
1460         h->next_output_pic = out;
1461         if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
1462             h->next_outputed_poc = INT_MIN;
1463         } else
1464             h->next_outputed_poc = out->poc;
1465     }else{
1466         av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
1467     }
1468
1469     if (setup_finished)
1470         ff_thread_finish_setup(s->avctx);
1471 }
1472
1473 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 chroma444, int simple){
1474     MpegEncContext * const s = &h->s;
1475     uint8_t *top_border;
1476     int top_idx = 1;
1477     const int pixel_shift = h->pixel_shift;
1478
1479     src_y  -=   linesize;
1480     src_cb -= uvlinesize;
1481     src_cr -= uvlinesize;
1482
1483     if(!simple && FRAME_MBAFF){
1484         if(s->mb_y&1){
1485             if(!MB_MBAFF){
1486                 top_border = h->top_borders[0][s->mb_x];
1487                 AV_COPY128(top_border, src_y + 15*linesize);
1488                 if (pixel_shift)
1489                     AV_COPY128(top_border+16, src_y+15*linesize+16);
1490                 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1491                     if(chroma444){
1492                         if (pixel_shift){
1493                             AV_COPY128(top_border+32, src_cb + 15*uvlinesize);
1494                             AV_COPY128(top_border+48, src_cb + 15*uvlinesize+16);
1495                             AV_COPY128(top_border+64, src_cr + 15*uvlinesize);
1496                             AV_COPY128(top_border+80, src_cr + 15*uvlinesize+16);
1497                         } else {
1498                             AV_COPY128(top_border+16, src_cb + 15*uvlinesize);
1499                             AV_COPY128(top_border+32, src_cr + 15*uvlinesize);
1500                         }
1501                     } else {
1502                         if (pixel_shift) {
1503                             AV_COPY128(top_border+32, src_cb+7*uvlinesize);
1504                             AV_COPY128(top_border+48, src_cr+7*uvlinesize);
1505                         } else {
1506                             AV_COPY64(top_border+16, src_cb+7*uvlinesize);
1507                             AV_COPY64(top_border+24, src_cr+7*uvlinesize);
1508                         }
1509                     }
1510                 }
1511             }
1512         }else if(MB_MBAFF){
1513             top_idx = 0;
1514         }else
1515             return;
1516     }
1517
1518     top_border = h->top_borders[top_idx][s->mb_x];
1519     // There are two lines saved, the line above the the top macroblock of a pair,
1520     // and the line above the bottom macroblock
1521     AV_COPY128(top_border, src_y + 16*linesize);
1522     if (pixel_shift)
1523         AV_COPY128(top_border+16, src_y+16*linesize+16);
1524
1525     if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1526         if(chroma444){
1527             if (pixel_shift){
1528                 AV_COPY128(top_border+32, src_cb + 16*linesize);
1529                 AV_COPY128(top_border+48, src_cb + 16*linesize+16);
1530                 AV_COPY128(top_border+64, src_cr + 16*linesize);
1531                 AV_COPY128(top_border+80, src_cr + 16*linesize+16);
1532             } else {
1533                 AV_COPY128(top_border+16, src_cb + 16*linesize);
1534                 AV_COPY128(top_border+32, src_cr + 16*linesize);
1535             }
1536         } else {
1537             if (pixel_shift) {
1538                 AV_COPY128(top_border+32, src_cb+8*uvlinesize);
1539                 AV_COPY128(top_border+48, src_cr+8*uvlinesize);
1540             } else {
1541                 AV_COPY64(top_border+16, src_cb+8*uvlinesize);
1542                 AV_COPY64(top_border+24, src_cr+8*uvlinesize);
1543             }
1544         }
1545     }
1546 }
1547
1548 static inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
1549                                   uint8_t *src_cb, uint8_t *src_cr,
1550                                   int linesize, int uvlinesize,
1551                                   int xchg, int chroma444,
1552                                   int simple, int pixel_shift){
1553     MpegEncContext * const s = &h->s;
1554     int deblock_topleft;
1555     int deblock_top;
1556     int top_idx = 1;
1557     uint8_t *top_border_m1;
1558     uint8_t *top_border;
1559
1560     if(!simple && FRAME_MBAFF){
1561         if(s->mb_y&1){
1562             if(!MB_MBAFF)
1563                 return;
1564         }else{
1565             top_idx = MB_MBAFF ? 0 : 1;
1566         }
1567     }
1568
1569     if(h->deblocking_filter == 2) {
1570         deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
1571         deblock_top     = h->top_type;
1572     } else {
1573         deblock_topleft = (s->mb_x > 0);
1574         deblock_top     = (s->mb_y > !!MB_FIELD);
1575     }
1576
1577     src_y  -=   linesize + 1 + pixel_shift;
1578     src_cb -= uvlinesize + 1 + pixel_shift;
1579     src_cr -= uvlinesize + 1 + pixel_shift;
1580
1581     top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
1582     top_border    = h->top_borders[top_idx][s->mb_x];
1583
1584 #define XCHG(a,b,xchg)\
1585     if (pixel_shift) {\
1586         if (xchg) {\
1587             AV_SWAP64(b+0,a+0);\
1588             AV_SWAP64(b+8,a+8);\
1589         } else {\
1590             AV_COPY128(b,a); \
1591         }\
1592     } else \
1593 if (xchg) AV_SWAP64(b,a);\
1594 else      AV_COPY64(b,a);
1595
1596     if(deblock_top){
1597         if(deblock_topleft){
1598             XCHG(top_border_m1 + (8 << pixel_shift), src_y - (7 << pixel_shift), 1);
1599         }
1600         XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
1601         XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
1602         if(s->mb_x+1 < s->mb_width){
1603             XCHG(h->top_borders[top_idx][s->mb_x+1], src_y + (17 << pixel_shift), 1);
1604         }
1605     }
1606     if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1607         if(chroma444){
1608             if(deblock_topleft){
1609                 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
1610                 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
1611             }
1612             XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
1613             XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
1614             XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
1615             XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
1616             if(s->mb_x+1 < s->mb_width){
1617                 XCHG(h->top_borders[top_idx][s->mb_x+1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
1618                 XCHG(h->top_borders[top_idx][s->mb_x+1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
1619             }
1620         } else {
1621             if(deblock_top){
1622                 if(deblock_topleft){
1623                     XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
1624                     XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
1625                 }
1626                 XCHG(top_border + (16 << pixel_shift), src_cb+1+pixel_shift, 1);
1627                 XCHG(top_border + (24 << pixel_shift), src_cr+1+pixel_shift, 1);
1628             }
1629         }
1630     }
1631 }
1632
1633 static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth, int index) {
1634     if (high_bit_depth) {
1635         return AV_RN32A(((int32_t*)mb) + index);
1636     } else
1637         return AV_RN16A(mb + index);
1638 }
1639
1640 static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth, int index, int value) {
1641     if (high_bit_depth) {
1642         AV_WN32A(((int32_t*)mb) + index, value);
1643     } else
1644         AV_WN16A(mb + index, value);
1645 }
1646
1647 static av_always_inline void hl_decode_mb_predict_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
1648                                                        int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
1649 {
1650     MpegEncContext * const s = &h->s;
1651     void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
1652     void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
1653     int i;
1654     int qscale = p == 0 ? s->qscale : h->chroma_qp[p-1];
1655     block_offset += 16*p;
1656     if(IS_INTRA4x4(mb_type)){
1657         if(simple || !s->encoding){
1658             if(IS_8x8DCT(mb_type)){
1659                 if(transform_bypass){
1660                     idct_dc_add =
1661                     idct_add    = s->dsp.add_pixels8;
1662                 }else{
1663                     idct_dc_add = h->h264dsp.h264_idct8_dc_add;
1664                     idct_add    = h->h264dsp.h264_idct8_add;
1665                 }
1666                 for(i=0; i<16; i+=4){
1667                     uint8_t * const ptr= dest_y + block_offset[i];
1668                     const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1669                     if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1670                         h->hpc.pred8x8l_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1671                     }else{
1672                         const int nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
1673                         h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
1674                                                     (h->topright_samples_available<<i)&0x4000, linesize);
1675                         if(nnz){
1676                             if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
1677                                 idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1678                             else
1679                                 idct_add   (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1680                         }
1681                     }
1682                 }
1683             }else{
1684                 if(transform_bypass){
1685                     idct_dc_add =
1686                     idct_add    = s->dsp.add_pixels4;
1687                 }else{
1688                     idct_dc_add = h->h264dsp.h264_idct_dc_add;
1689                     idct_add    = h->h264dsp.h264_idct_add;
1690                 }
1691                 for(i=0; i<16; i++){
1692                     uint8_t * const ptr= dest_y + block_offset[i];
1693                     const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1694
1695                     if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1696                         h->hpc.pred4x4_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1697                     }else{
1698                         uint8_t *topright;
1699                         int nnz, tr;
1700                         uint64_t tr_high;
1701                         if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
1702                             const int topright_avail= (h->topright_samples_available<<i)&0x8000;
1703                             assert(s->mb_y || linesize <= block_offset[i]);
1704                             if(!topright_avail){
1705                                 if (pixel_shift) {
1706                                     tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
1707                                     topright= (uint8_t*) &tr_high;
1708                                 } else {
1709                                     tr= ptr[3 - linesize]*0x01010101;
1710                                     topright= (uint8_t*) &tr;
1711                                 }
1712                             }else
1713                                 topright= ptr + (4 << pixel_shift) - linesize;
1714                         }else
1715                             topright= NULL;
1716
1717                         h->hpc.pred4x4[ dir ](ptr, topright, linesize);
1718                         nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
1719                         if(nnz){
1720                             if(is_h264){
1721                                 if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
1722                                     idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1723                                 else
1724                                     idct_add   (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1725                             }else
1726                                 ff_svq3_add_idct_c(ptr, h->mb + i*16+p*256, linesize, qscale, 0);
1727                         }
1728                     }
1729                 }
1730             }
1731         }
1732     }else{
1733         h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
1734         if(is_h264){
1735             if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX+p] ]){
1736                 if(!transform_bypass)
1737                     h->h264dsp.h264_luma_dc_dequant_idct(h->mb+(p*256 << pixel_shift), h->mb_luma_dc[p], h->dequant4_coeff[p][qscale][0]);
1738                 else{
1739                     static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
1740                                                             8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
1741                     for(i = 0; i < 16; i++)
1742                         dctcoef_set(h->mb+p*256, pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
1743                 }
1744             }
1745         }else
1746             ff_svq3_luma_dc_dequant_idct_c(h->mb+p*256, h->mb_luma_dc[p], qscale);
1747     }
1748 }
1749
1750 static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
1751                                                     int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
1752 {
1753     MpegEncContext * const s = &h->s;
1754     void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
1755     int i;
1756     block_offset += 16*p;
1757     if(!IS_INTRA4x4(mb_type)){
1758         if(is_h264){
1759             if(IS_INTRA16x16(mb_type)){
1760                 if(transform_bypass){
1761                     if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
1762                         h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize);
1763                     }else{
1764                         for(i=0; i<16; i++){
1765                             if(h->non_zero_count_cache[ scan8[i+p*16] ] || dctcoef_get(h->mb, pixel_shift, i*16+p*256))
1766                                 s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
1767                         }
1768                     }
1769                 }else{
1770                     h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
1771                 }
1772             }else if(h->cbp&15){
1773                 if(transform_bypass){
1774                     const int di = IS_8x8DCT(mb_type) ? 4 : 1;
1775                     idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
1776                     for(i=0; i<16; i+=di){
1777                         if(h->non_zero_count_cache[ scan8[i+p*16] ]){
1778                             idct_add(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
1779                         }
1780                     }
1781                 }else{
1782                     if(IS_8x8DCT(mb_type)){
1783                         h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
1784                     }else{
1785                         h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
1786                     }
1787                 }
1788             }
1789         }else{
1790             for(i=0; i<16; i++){
1791                 if(h->non_zero_count_cache[ scan8[i+p*16] ] || h->mb[i*16+p*256]){ //FIXME benchmark weird rule, & below
1792                     uint8_t * const ptr= dest_y + block_offset[i];
1793                     ff_svq3_add_idct_c(ptr, h->mb + i*16 + p*256, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
1794                 }
1795             }
1796         }
1797     }
1798 }
1799
1800 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift){
1801     MpegEncContext * const s = &h->s;
1802     const int mb_x= s->mb_x;
1803     const int mb_y= s->mb_y;
1804     const int mb_xy= h->mb_xy;
1805     const int mb_type= s->current_picture.mb_type[mb_xy];
1806     uint8_t  *dest_y, *dest_cb, *dest_cr;
1807     int linesize, uvlinesize /*dct_offset*/;
1808     int i, j;
1809     int *block_offset = &h->block_offset[0];
1810     const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
1811     /* is_h264 should always be true if SVQ3 is disabled. */
1812     const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
1813     void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
1814
1815     dest_y  = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
1816     dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
1817     dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
1818
1819     s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
1820     s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
1821
1822     h->list_counts[mb_xy]= h->list_count;
1823
1824     if (!simple && MB_FIELD) {
1825         linesize   = h->mb_linesize   = s->linesize * 2;
1826         uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
1827         block_offset = &h->block_offset[48];
1828         if(mb_y&1){ //FIXME move out of this function?
1829             dest_y -= s->linesize*15;
1830             dest_cb-= s->uvlinesize*7;
1831             dest_cr-= s->uvlinesize*7;
1832         }
1833         if(FRAME_MBAFF) {
1834             int list;
1835             for(list=0; list<h->list_count; list++){
1836                 if(!USES_LIST(mb_type, list))
1837                     continue;
1838                 if(IS_16X16(mb_type)){
1839                     int8_t *ref = &h->ref_cache[list][scan8[0]];
1840                     fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
1841                 }else{
1842                     for(i=0; i<16; i+=4){
1843                         int ref = h->ref_cache[list][scan8[i]];
1844                         if(ref >= 0)
1845                             fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
1846                     }
1847                 }
1848             }
1849         }
1850     } else {
1851         linesize   = h->mb_linesize   = s->linesize;
1852         uvlinesize = h->mb_uvlinesize = s->uvlinesize;
1853 //        dct_offset = s->linesize * 16;
1854     }
1855
1856     if (!simple && IS_INTRA_PCM(mb_type)) {
1857         if (pixel_shift) {
1858             const int bit_depth = h->sps.bit_depth_luma;
1859             int j;
1860             GetBitContext gb;
1861             init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
1862
1863             for (i = 0; i < 16; i++) {
1864                 uint16_t *tmp_y  = (uint16_t*)(dest_y  + i*linesize);
1865                 for (j = 0; j < 16; j++)
1866                     tmp_y[j] = get_bits(&gb, bit_depth);
1867             }
1868             if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1869                 if (!h->sps.chroma_format_idc) {
1870                     for (i = 0; i < 8; i++) {
1871                         uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
1872                         for (j = 0; j < 8; j++) {
1873                             tmp_cb[j] = 1 << (bit_depth - 1);
1874                         }
1875                     }
1876                     for (i = 0; i < 8; i++) {
1877                         uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
1878                         for (j = 0; j < 8; j++) {
1879                             tmp_cr[j] = 1 << (bit_depth - 1);
1880                         }
1881                     }
1882                 } else {
1883                     for (i = 0; i < 8; i++) {
1884                         uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
1885                         for (j = 0; j < 8; j++)
1886                             tmp_cb[j] = get_bits(&gb, bit_depth);
1887                     }
1888                     for (i = 0; i < 8; i++) {
1889                         uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
1890                         for (j = 0; j < 8; j++)
1891                             tmp_cr[j] = get_bits(&gb, bit_depth);
1892                     }
1893                 }
1894             }
1895         } else {
1896             for (i=0; i<16; i++) {
1897                 memcpy(dest_y + i*  linesize, h->mb       + i*8, 16);
1898             }
1899             if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1900                 if (!h->sps.chroma_format_idc) {
1901                     for (i = 0; i < 8; i++) {
1902                         memset(dest_cb + i*uvlinesize, 128, 8);
1903                         memset(dest_cr + i*uvlinesize, 128, 8);
1904                     }
1905                 } else {
1906                     for (i = 0; i < 8; i++) {
1907                         memcpy(dest_cb + i*uvlinesize, h->mb + 128 + i*4,  8);
1908                         memcpy(dest_cr + i*uvlinesize, h->mb + 160 + i*4,  8);
1909                     }
1910                 }
1911             }
1912         }
1913     } else {
1914         if(IS_INTRA(mb_type)){
1915             if(h->deblocking_filter)
1916                 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, 0, simple, pixel_shift);
1917
1918             if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1919                 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
1920                 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
1921             }
1922
1923             hl_decode_mb_predict_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
1924
1925             if(h->deblocking_filter)
1926                 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, 0, simple, pixel_shift);
1927         }else if(is_h264){
1928             if (pixel_shift) {
1929                 hl_motion_16(h, dest_y, dest_cb, dest_cr,
1930                              s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
1931                              s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
1932                              h->h264dsp.weight_h264_pixels_tab,
1933                              h->h264dsp.biweight_h264_pixels_tab, 0);
1934             } else
1935                 hl_motion_8(h, dest_y, dest_cb, dest_cr,
1936                             s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
1937                             s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
1938                             h->h264dsp.weight_h264_pixels_tab,
1939                             h->h264dsp.biweight_h264_pixels_tab, 0);
1940         }
1941
1942         hl_decode_mb_idct_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
1943
1944         if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
1945             uint8_t *dest[2] = {dest_cb, dest_cr};
1946             if(transform_bypass){
1947                 if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
1948                     h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + (16*16*1 << pixel_shift), uvlinesize);
1949                     h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 32, h->mb + (16*16*2 << pixel_shift), uvlinesize);
1950                 }else{
1951                     idct_add = s->dsp.add_pixels4;
1952                     for(j=1; j<3; j++){
1953                         for(i=j*16; i<j*16+4; i++){
1954                             if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
1955                                 idct_add   (dest[j-1] + block_offset[i], h->mb + (i*16 << pixel_shift), uvlinesize);
1956                         }
1957                     }
1958                 }
1959             }else{
1960                 if(is_h264){
1961                     if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
1962                         h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*1 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
1963                     if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
1964                         h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*2 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
1965                     h->h264dsp.h264_idct_add8(dest, block_offset,
1966                                               h->mb, uvlinesize,
1967                                               h->non_zero_count_cache);
1968                 }
1969 #if CONFIG_SVQ3_DECODER
1970                 else{
1971                     h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16*1, h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
1972                     h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16*2, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
1973                     for(j=1; j<3; j++){
1974                         for(i=j*16; i<j*16+4; i++){
1975                             if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
1976                                 uint8_t * const ptr= dest[j-1] + block_offset[i];
1977                                 ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
1978                             }
1979                         }
1980                     }
1981                 }
1982 #endif
1983             }
1984         }
1985     }
1986     if(h->cbp || IS_INTRA(mb_type))
1987     {
1988         s->dsp.clear_blocks(h->mb);
1989         s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
1990     }
1991 }
1992
1993 static av_always_inline void hl_decode_mb_444_internal(H264Context *h, int simple, int pixel_shift){
1994     MpegEncContext * const s = &h->s;
1995     const int mb_x= s->mb_x;
1996     const int mb_y= s->mb_y;
1997     const int mb_xy= h->mb_xy;
1998     const int mb_type= s->current_picture.mb_type[mb_xy];
1999     uint8_t  *dest[3];
2000     int linesize;
2001     int i, j, p;
2002     int *block_offset = &h->block_offset[0];
2003     const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
2004     const int plane_count = (simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) ? 3 : 1;
2005
2006     for (p = 0; p < plane_count; p++)
2007     {
2008         dest[p] = s->current_picture.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
2009         s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
2010     }
2011
2012     h->list_counts[mb_xy]= h->list_count;
2013
2014     if (!simple && MB_FIELD) {
2015         linesize   = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
2016         block_offset = &h->block_offset[48];
2017         if(mb_y&1) //FIXME move out of this function?
2018             for (p = 0; p < 3; p++)
2019                 dest[p] -= s->linesize*15;
2020         if(FRAME_MBAFF) {
2021             int list;
2022             for(list=0; list<h->list_count; list++){
2023                 if(!USES_LIST(mb_type, list))
2024                     continue;
2025                 if(IS_16X16(mb_type)){
2026                     int8_t *ref = &h->ref_cache[list][scan8[0]];
2027                     fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
2028                 }else{
2029                     for(i=0; i<16; i+=4){
2030                         int ref = h->ref_cache[list][scan8[i]];
2031                         if(ref >= 0)
2032                             fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
2033                     }
2034                 }
2035             }
2036         }
2037     } else {
2038         linesize   = h->mb_linesize = h->mb_uvlinesize = s->linesize;
2039     }
2040
2041     if (!simple && IS_INTRA_PCM(mb_type)) {
2042         if (pixel_shift) {
2043             const int bit_depth = h->sps.bit_depth_luma;
2044             GetBitContext gb;
2045             init_get_bits(&gb, (uint8_t*)h->mb, 768*bit_depth);
2046
2047             for (p = 0; p < plane_count; p++) {
2048                 for (i = 0; i < 16; i++) {
2049                     uint16_t *tmp = (uint16_t*)(dest[p] + i*linesize);
2050                     for (j = 0; j < 16; j++)
2051                         tmp[j] = get_bits(&gb, bit_depth);
2052                 }
2053             }
2054         } else {
2055             for (p = 0; p < plane_count; p++) {
2056                 for (i = 0; i < 16; i++) {
2057                     memcpy(dest[p] + i*linesize, h->mb + p*128 + i*8, 16);
2058                 }
2059             }
2060         }
2061     } else {
2062         if(IS_INTRA(mb_type)){
2063             if(h->deblocking_filter)
2064                 xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 1, 1, simple, pixel_shift);
2065
2066             for (p = 0; p < plane_count; p++)
2067                 hl_decode_mb_predict_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
2068
2069             if(h->deblocking_filter)
2070                 xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);
2071         }else{
2072             if (pixel_shift) {
2073                 hl_motion_16(h, dest[0], dest[1], dest[2],
2074                              s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
2075                              s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
2076                              h->h264dsp.weight_h264_pixels_tab,
2077                              h->h264dsp.biweight_h264_pixels_tab, 1);
2078             } else
2079                 hl_motion_8(h, dest[0], dest[1], dest[2],
2080                             s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
2081                             s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
2082                             h->h264dsp.weight_h264_pixels_tab,
2083                             h->h264dsp.biweight_h264_pixels_tab, 1);
2084         }
2085
2086         for (p = 0; p < plane_count; p++)
2087             hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
2088     }
2089     if(h->cbp || IS_INTRA(mb_type))
2090     {
2091         s->dsp.clear_blocks(h->mb);
2092         s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
2093     }
2094 }
2095
2096 /**
2097  * Process a macroblock; this case avoids checks for expensive uncommon cases.
2098  */
2099 #define hl_decode_mb_simple(sh, bits) \
2100 static void hl_decode_mb_simple_ ## bits(H264Context *h){ \
2101     hl_decode_mb_internal(h, 1, sh); \
2102 }
2103 hl_decode_mb_simple(0, 8);
2104 hl_decode_mb_simple(1, 16);
2105
2106 /**
2107  * Process a macroblock; this handles edge cases, such as interlacing.
2108  */
2109 static void av_noinline hl_decode_mb_complex(H264Context *h){
2110     hl_decode_mb_internal(h, 0, h->pixel_shift);
2111 }
2112
2113 static void av_noinline hl_decode_mb_444_complex(H264Context *h){
2114     hl_decode_mb_444_internal(h, 0, h->pixel_shift);
2115 }
2116
2117 static void av_noinline hl_decode_mb_444_simple(H264Context *h){
2118     hl_decode_mb_444_internal(h, 1, 0);
2119 }
2120
2121 void ff_h264_hl_decode_mb(H264Context *h){
2122     MpegEncContext * const s = &h->s;
2123     const int mb_xy= h->mb_xy;
2124     const int mb_type= s->current_picture.mb_type[mb_xy];
2125     int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
2126
2127     if (CHROMA444) {
2128         if(is_complex || h->pixel_shift)
2129             hl_decode_mb_444_complex(h);
2130         else
2131             hl_decode_mb_444_simple(h);
2132     } else if (is_complex) {
2133         hl_decode_mb_complex(h);
2134     } else if (h->pixel_shift) {
2135         hl_decode_mb_simple_16(h);
2136     } else
2137         hl_decode_mb_simple_8(h);
2138 }
2139
2140 static int pred_weight_table(H264Context *h){
2141     MpegEncContext * const s = &h->s;
2142     int list, i;
2143     int luma_def, chroma_def;
2144
2145     h->use_weight= 0;
2146     h->use_weight_chroma= 0;
2147     h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
2148     if(h->sps.chroma_format_idc)
2149         h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
2150     luma_def = 1<<h->luma_log2_weight_denom;
2151     chroma_def = 1<<h->chroma_log2_weight_denom;
2152
2153     for(list=0; list<2; list++){
2154         h->luma_weight_flag[list]   = 0;
2155         h->chroma_weight_flag[list] = 0;
2156         for(i=0; i<h->ref_count[list]; i++){
2157             int luma_weight_flag, chroma_weight_flag;
2158
2159             luma_weight_flag= get_bits1(&s->gb);
2160             if(luma_weight_flag){
2161                 h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
2162                 h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
2163                 if(   h->luma_weight[i][list][0] != luma_def
2164                    || h->luma_weight[i][list][1] != 0) {
2165                     h->use_weight= 1;
2166                     h->luma_weight_flag[list]= 1;
2167                 }
2168             }else{
2169                 h->luma_weight[i][list][0]= luma_def;
2170                 h->luma_weight[i][list][1]= 0;
2171             }
2172
2173             if(h->sps.chroma_format_idc){
2174                 chroma_weight_flag= get_bits1(&s->gb);
2175                 if(chroma_weight_flag){
2176                     int j;
2177                     for(j=0; j<2; j++){
2178                         h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
2179                         h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
2180                         if(   h->chroma_weight[i][list][j][0] != chroma_def
2181                            || h->chroma_weight[i][list][j][1] != 0) {
2182                             h->use_weight_chroma= 1;
2183                             h->chroma_weight_flag[list]= 1;
2184                         }
2185                     }
2186                 }else{
2187                     int j;
2188                     for(j=0; j<2; j++){
2189                         h->chroma_weight[i][list][j][0]= chroma_def;
2190                         h->chroma_weight[i][list][j][1]= 0;
2191                     }
2192                 }
2193             }
2194         }
2195         if(h->slice_type_nos != AV_PICTURE_TYPE_B) break;
2196     }
2197     h->use_weight= h->use_weight || h->use_weight_chroma;
2198     return 0;
2199 }
2200
2201 /**
2202  * Initialize implicit_weight table.
2203  * @param field  0/1 initialize the weight for interlaced MBAFF
2204  *                -1 initializes the rest
2205  */
2206 static void implicit_weight_table(H264Context *h, int field){
2207     MpegEncContext * const s = &h->s;
2208     int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
2209
2210     for (i = 0; i < 2; i++) {
2211         h->luma_weight_flag[i]   = 0;
2212         h->chroma_weight_flag[i] = 0;
2213     }
2214
2215     if(field < 0){
2216         cur_poc = s->current_picture_ptr->poc;
2217     if(   h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
2218        && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
2219         h->use_weight= 0;
2220         h->use_weight_chroma= 0;
2221         return;
2222     }
2223         ref_start= 0;
2224         ref_count0= h->ref_count[0];
2225         ref_count1= h->ref_count[1];
2226     }else{
2227         cur_poc = s->current_picture_ptr->field_poc[field];
2228         ref_start= 16;
2229         ref_count0= 16+2*h->ref_count[0];
2230         ref_count1= 16+2*h->ref_count[1];
2231     }
2232
2233     h->use_weight= 2;
2234     h->use_weight_chroma= 2;
2235     h->luma_log2_weight_denom= 5;
2236     h->chroma_log2_weight_denom= 5;
2237
2238     for(ref0=ref_start; ref0 < ref_count0; ref0++){
2239         int poc0 = h->ref_list[0][ref0].poc;
2240         for(ref1=ref_start; ref1 < ref_count1; ref1++){
2241             int w = 32;
2242             if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
2243                 int poc1 = h->ref_list[1][ref1].poc;
2244                 int td = av_clip(poc1 - poc0, -128, 127);
2245                 if(td){
2246                     int tb = av_clip(cur_poc - poc0, -128, 127);
2247                     int tx = (16384 + (FFABS(td) >> 1)) / td;
2248                     int dist_scale_factor = (tb*tx + 32) >> 8;
2249                     if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
2250                         w = 64 - dist_scale_factor;
2251                 }
2252             }
2253             if(field<0){
2254                 h->implicit_weight[ref0][ref1][0]=
2255                 h->implicit_weight[ref0][ref1][1]= w;
2256             }else{
2257                 h->implicit_weight[ref0][ref1][field]=w;
2258             }
2259         }
2260     }
2261 }
2262
2263 /**
2264  * instantaneous decoder refresh.
2265  */
2266 static void idr(H264Context *h){
2267     ff_h264_remove_all_refs(h);
2268     h->prev_frame_num= 0;
2269     h->prev_frame_num_offset= 0;
2270     h->prev_poc_msb=
2271     h->prev_poc_lsb= 0;
2272 }
2273
2274 /* forget old pics after a seek */
2275 static void flush_dpb(AVCodecContext *avctx){
2276     H264Context *h= avctx->priv_data;
2277     int i;
2278     for(i=0; i<=MAX_DELAYED_PIC_COUNT; i++) {
2279         if(h->delayed_pic[i])
2280             h->delayed_pic[i]->reference= 0;
2281         h->delayed_pic[i]= NULL;
2282     }
2283     h->outputed_poc=h->next_outputed_poc= INT_MIN;
2284     h->prev_interlaced_frame = 1;
2285     idr(h);
2286     if(h->s.current_picture_ptr)
2287         h->s.current_picture_ptr->reference= 0;
2288     h->s.first_field= 0;
2289     ff_h264_reset_sei(h);
2290     ff_mpeg_flush(avctx);
2291 }
2292
2293 static int init_poc(H264Context *h){
2294     MpegEncContext * const s = &h->s;
2295     const int max_frame_num= 1<<h->sps.log2_max_frame_num;
2296     int field_poc[2];
2297     Picture *cur = s->current_picture_ptr;
2298
2299     h->frame_num_offset= h->prev_frame_num_offset;
2300     if(h->frame_num < h->prev_frame_num)
2301         h->frame_num_offset += max_frame_num;
2302
2303     if(h->sps.poc_type==0){
2304         const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
2305
2306         if     (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
2307             h->poc_msb = h->prev_poc_msb + max_poc_lsb;
2308         else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
2309             h->poc_msb = h->prev_poc_msb - max_poc_lsb;
2310         else
2311             h->poc_msb = h->prev_poc_msb;
2312 //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
2313         field_poc[0] =
2314         field_poc[1] = h->poc_msb + h->poc_lsb;
2315         if(s->picture_structure == PICT_FRAME)
2316             field_poc[1] += h->delta_poc_bottom;
2317     }else if(h->sps.poc_type==1){
2318         int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
2319         int i;
2320
2321         if(h->sps.poc_cycle_length != 0)
2322             abs_frame_num = h->frame_num_offset + h->frame_num;
2323         else
2324             abs_frame_num = 0;
2325
2326         if(h->nal_ref_idc==0 && abs_frame_num > 0)
2327             abs_frame_num--;
2328
2329         expected_delta_per_poc_cycle = 0;
2330         for(i=0; i < h->sps.poc_cycle_length; i++)
2331             expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
2332
2333         if(abs_frame_num > 0){
2334             int poc_cycle_cnt          = (abs_frame_num - 1) / h->sps.poc_cycle_length;
2335             int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
2336
2337             expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2338             for(i = 0; i <= frame_num_in_poc_cycle; i++)
2339                 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
2340         } else
2341             expectedpoc = 0;
2342
2343         if(h->nal_ref_idc == 0)
2344             expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
2345
2346         field_poc[0] = expectedpoc + h->delta_poc[0];
2347         field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
2348
2349         if(s->picture_structure == PICT_FRAME)
2350             field_poc[1] += h->delta_poc[1];
2351     }else{
2352         int poc= 2*(h->frame_num_offset + h->frame_num);
2353
2354         if(!h->nal_ref_idc)
2355             poc--;
2356
2357         field_poc[0]= poc;
2358         field_poc[1]= poc;
2359     }
2360
2361     if(s->picture_structure != PICT_BOTTOM_FIELD)
2362         s->current_picture_ptr->field_poc[0]= field_poc[0];
2363     if(s->picture_structure != PICT_TOP_FIELD)
2364         s->current_picture_ptr->field_poc[1]= field_poc[1];
2365     cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
2366
2367     return 0;
2368 }
2369
2370
2371 /**
2372  * initialize scan tables
2373  */
2374 static void init_scan_tables(H264Context *h){
2375     int i;
2376     for(i=0; i<16; i++){
2377 #define T(x) (x>>2) | ((x<<2) & 0xF)
2378         h->zigzag_scan[i] = T(zigzag_scan[i]);
2379         h-> field_scan[i] = T( field_scan[i]);
2380 #undef T
2381     }
2382     for(i=0; i<64; i++){
2383 #define T(x) (x>>3) | ((x&7)<<3)
2384         h->zigzag_scan8x8[i]       = T(ff_zigzag_direct[i]);
2385         h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
2386         h->field_scan8x8[i]        = T(field_scan8x8[i]);
2387         h->field_scan8x8_cavlc[i]  = T(field_scan8x8_cavlc[i]);
2388 #undef T
2389     }
2390     if(h->sps.transform_bypass){ //FIXME same ugly
2391         h->zigzag_scan_q0          = zigzag_scan;
2392         h->zigzag_scan8x8_q0       = ff_zigzag_direct;
2393         h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
2394         h->field_scan_q0           = field_scan;
2395         h->field_scan8x8_q0        = field_scan8x8;
2396         h->field_scan8x8_cavlc_q0  = field_scan8x8_cavlc;
2397     }else{
2398         h->zigzag_scan_q0          = h->zigzag_scan;
2399         h->zigzag_scan8x8_q0       = h->zigzag_scan8x8;
2400         h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
2401         h->field_scan_q0           = h->field_scan;
2402         h->field_scan8x8_q0        = h->field_scan8x8;
2403         h->field_scan8x8_cavlc_q0  = h->field_scan8x8_cavlc;
2404     }
2405 }
2406
2407 static void field_end(H264Context *h, int in_setup){
2408     MpegEncContext * const s = &h->s;
2409     AVCodecContext * const avctx= s->avctx;
2410     s->mb_y= 0;
2411
2412     if (!in_setup && !s->dropable)
2413         ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
2414                                  s->picture_structure==PICT_BOTTOM_FIELD);
2415
2416     if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2417         ff_vdpau_h264_set_reference_frames(s);
2418
2419     if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
2420         if(!s->dropable) {
2421             ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
2422             h->prev_poc_msb= h->poc_msb;
2423             h->prev_poc_lsb= h->poc_lsb;
2424         }
2425         h->prev_frame_num_offset= h->frame_num_offset;
2426         h->prev_frame_num= h->frame_num;
2427         h->outputed_poc = h->next_outputed_poc;
2428     }
2429
2430     if (avctx->hwaccel) {
2431         if (avctx->hwaccel->end_frame(avctx) < 0)
2432             av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
2433     }
2434
2435     if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2436         ff_vdpau_h264_picture_complete(s);
2437
2438     /*
2439      * FIXME: Error handling code does not seem to support interlaced
2440      * when slices span multiple rows
2441      * The ff_er_add_slice calls don't work right for bottom
2442      * fields; they cause massive erroneous error concealing
2443      * Error marking covers both fields (top and bottom).
2444      * This causes a mismatched s->error_count
2445      * and a bad error table. Further, the error count goes to
2446      * INT_MAX when called for bottom field, because mb_y is
2447      * past end by one (callers fault) and resync_mb_y != 0
2448      * causes problems for the first MB line, too.
2449      */
2450     if (!FIELD_PICTURE)
2451         ff_er_frame_end(s);
2452
2453     MPV_frame_end(s);
2454
2455     h->current_slice=0;
2456 }
2457
2458 /**
2459  * Replicate H264 "master" context to thread contexts.
2460  */
2461 static void clone_slice(H264Context *dst, H264Context *src)
2462 {
2463     memcpy(dst->block_offset,     src->block_offset, sizeof(dst->block_offset));
2464     dst->s.current_picture_ptr  = src->s.current_picture_ptr;
2465     dst->s.current_picture      = src->s.current_picture;
2466     dst->s.linesize             = src->s.linesize;
2467     dst->s.uvlinesize           = src->s.uvlinesize;
2468     dst->s.first_field          = src->s.first_field;
2469
2470     dst->prev_poc_msb           = src->prev_poc_msb;
2471     dst->prev_poc_lsb           = src->prev_poc_lsb;
2472     dst->prev_frame_num_offset  = src->prev_frame_num_offset;
2473     dst->prev_frame_num         = src->prev_frame_num;
2474     dst->short_ref_count        = src->short_ref_count;
2475
2476     memcpy(dst->short_ref,        src->short_ref,        sizeof(dst->short_ref));
2477     memcpy(dst->long_ref,         src->long_ref,         sizeof(dst->long_ref));
2478     memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
2479     memcpy(dst->ref_list,         src->ref_list,         sizeof(dst->ref_list));
2480
2481     memcpy(dst->dequant4_coeff,   src->dequant4_coeff,   sizeof(src->dequant4_coeff));
2482     memcpy(dst->dequant8_coeff,   src->dequant8_coeff,   sizeof(src->dequant8_coeff));
2483 }
2484
2485 /**
2486  * computes profile from profile_idc and constraint_set?_flags
2487  *
2488  * @param sps SPS
2489  *
2490  * @return profile as defined by FF_PROFILE_H264_*
2491  */
2492 int ff_h264_get_profile(SPS *sps)
2493 {
2494     int profile = sps->profile_idc;
2495
2496     switch(sps->profile_idc) {
2497     case FF_PROFILE_H264_BASELINE:
2498         // constraint_set1_flag set to 1
2499         profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
2500         break;
2501     case FF_PROFILE_H264_HIGH_10:
2502     case FF_PROFILE_H264_HIGH_422:
2503     case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
2504         // constraint_set3_flag set to 1
2505         profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
2506         break;
2507     }
2508
2509     return profile;
2510 }
2511
2512 /**
2513  * decodes a slice header.
2514  * This will also call MPV_common_init() and frame_start() as needed.
2515  *
2516  * @param h h264context
2517  * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
2518  *
2519  * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
2520  */
2521 static int decode_slice_header(H264Context *h, H264Context *h0){
2522     MpegEncContext * const s = &h->s;
2523     MpegEncContext * const s0 = &h0->s;
2524     unsigned int first_mb_in_slice;
2525     unsigned int pps_id;
2526     int num_ref_idx_active_override_flag;
2527     unsigned int slice_type, tmp, i, j;
2528     int default_ref_list_done = 0;
2529     int last_pic_structure;
2530
2531     s->dropable= h->nal_ref_idc == 0;
2532
2533     /* FIXME: 2tap qpel isn't implemented for high bit depth. */
2534     if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc && !h->pixel_shift){
2535         s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
2536         s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
2537     }else{
2538         s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
2539         s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
2540     }
2541
2542     first_mb_in_slice= get_ue_golomb(&s->gb);
2543
2544     if(first_mb_in_slice == 0){ //FIXME better field boundary detection
2545         if(h0->current_slice && FIELD_PICTURE){
2546             field_end(h, 1);
2547         }
2548
2549         h0->current_slice = 0;
2550         if (!s0->first_field)
2551             s->current_picture_ptr= NULL;
2552     }
2553
2554     slice_type= get_ue_golomb_31(&s->gb);
2555     if(slice_type > 9){
2556         av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
2557         return -1;
2558     }
2559     if(slice_type > 4){
2560         slice_type -= 5;
2561         h->slice_type_fixed=1;
2562     }else
2563         h->slice_type_fixed=0;
2564
2565     slice_type= golomb_to_pict_type[ slice_type ];
2566     if (slice_type == AV_PICTURE_TYPE_I
2567         || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
2568         default_ref_list_done = 1;
2569     }
2570     h->slice_type= slice_type;
2571     h->slice_type_nos= slice_type & 3;
2572
2573     s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
2574
2575     pps_id= get_ue_golomb(&s->gb);
2576     if(pps_id>=MAX_PPS_COUNT){
2577         av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
2578         return -1;
2579     }
2580     if(!h0->pps_buffers[pps_id]) {
2581         av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
2582         return -1;
2583     }
2584     h->pps= *h0->pps_buffers[pps_id];
2585
2586     if(!h0->sps_buffers[h->pps.sps_id]) {
2587         av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
2588         return -1;
2589     }
2590     h->sps = *h0->sps_buffers[h->pps.sps_id];
2591
2592     s->avctx->profile = ff_h264_get_profile(&h->sps);
2593     s->avctx->level   = h->sps.level_idc;
2594     s->avctx->refs    = h->sps.ref_frame_count;
2595
2596     if(h == h0 && h->dequant_coeff_pps != pps_id){
2597         h->dequant_coeff_pps = pps_id;
2598         init_dequant_tables(h);
2599     }
2600
2601     s->mb_width= h->sps.mb_width;
2602     s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
2603
2604     h->b_stride=  s->mb_width*4;
2605
2606     s->width = 16*s->mb_width - (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
2607     if(h->sps.frame_mbs_only_flag)
2608         s->height= 16*s->mb_height - (2>>CHROMA444)*FFMIN(h->sps.crop_bottom, (8<<CHROMA444)-1);
2609     else
2610         s->height= 16*s->mb_height - (4>>CHROMA444)*FFMIN(h->sps.crop_bottom, (8<<CHROMA444)-1);
2611
2612     if (s->context_initialized
2613         && (   s->width != s->avctx->width || s->height != s->avctx->height
2614             || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
2615         if(h != h0) {
2616             av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
2617             return -1;   // width / height changed during parallelized decoding
2618         }
2619         free_tables(h, 0);
2620         flush_dpb(s->avctx);
2621         MPV_common_end(s);
2622     }
2623     if (!s->context_initialized) {
2624         if (h != h0) {
2625             av_log(h->s.avctx, AV_LOG_ERROR, "Cannot (re-)initialize context during parallel decoding.\n");
2626             return -1;
2627         }
2628
2629         avcodec_set_dimensions(s->avctx, s->width, s->height);
2630         s->avctx->sample_aspect_ratio= h->sps.sar;
2631         av_assert0(s->avctx->sample_aspect_ratio.den);
2632
2633         h->s.avctx->coded_width = 16*s->mb_width;
2634         h->s.avctx->coded_height = 16*s->mb_height;
2635
2636         if(h->sps.video_signal_type_present_flag){
2637             s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2638             if(h->sps.colour_description_present_flag){
2639                 s->avctx->color_primaries = h->sps.color_primaries;
2640                 s->avctx->color_trc       = h->sps.color_trc;
2641                 s->avctx->colorspace      = h->sps.colorspace;
2642             }
2643         }
2644
2645         if(h->sps.timing_info_present_flag){
2646             int64_t den= h->sps.time_scale;
2647             if(h->x264_build < 44U)
2648                 den *= 2;
2649             av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
2650                       h->sps.num_units_in_tick, den, 1<<30);
2651         }
2652
2653         switch (h->sps.bit_depth_luma) {
2654             case 9 :
2655                 s->avctx->pix_fmt = CHROMA444 ? PIX_FMT_YUV444P9 : PIX_FMT_YUV420P9;
2656                 break;
2657             case 10 :
2658                 s->avctx->pix_fmt = CHROMA444 ? PIX_FMT_YUV444P10 : PIX_FMT_YUV420P10;
2659                 break;
2660             default:
2661                 if (CHROMA444){
2662                     s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P : PIX_FMT_YUV444P;
2663                 }else{
2664                     s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
2665                                                              s->avctx->codec->pix_fmts ?
2666                                                              s->avctx->codec->pix_fmts :
2667                                                              s->avctx->color_range == AVCOL_RANGE_JPEG ?
2668                                                              hwaccel_pixfmt_list_h264_jpeg_420 :
2669                                                              ff_hwaccel_pixfmt_list_420);
2670                 }
2671         }
2672
2673         s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
2674
2675         if (MPV_common_init(s) < 0) {
2676             av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed.\n");
2677             return -1;
2678         }
2679         s->first_field = 0;
2680         h->prev_interlaced_frame = 1;
2681
2682         init_scan_tables(h);
2683         if (ff_h264_alloc_tables(h) < 0) {
2684             av_log(h->s.avctx, AV_LOG_ERROR, "Could not allocate memory for h264\n");
2685             return AVERROR(ENOMEM);
2686         }
2687
2688         if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
2689             if (context_init(h) < 0) {
2690                 av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
2691                 return -1;
2692             }
2693         } else {
2694             for(i = 1; i < s->avctx->thread_count; i++) {
2695                 H264Context *c;
2696                 c = h->thread_context[i] = av_malloc(sizeof(H264Context));
2697                 memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
2698                 memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
2699                 c->h264dsp = h->h264dsp;
2700                 c->sps = h->sps;
2701                 c->pps = h->pps;
2702                 c->pixel_shift = h->pixel_shift;
2703                 init_scan_tables(c);
2704                 clone_tables(c, h, i);
2705             }
2706
2707             for(i = 0; i < s->avctx->thread_count; i++)
2708                 if (context_init(h->thread_context[i]) < 0) {
2709                     av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
2710                     return -1;
2711                 }
2712         }
2713     }
2714
2715     h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
2716
2717     h->mb_mbaff = 0;
2718     h->mb_aff_frame = 0;
2719     last_pic_structure = s0->picture_structure;
2720     if(h->sps.frame_mbs_only_flag){
2721         s->picture_structure= PICT_FRAME;
2722     }else{
2723         if(get_bits1(&s->gb)) { //field_pic_flag
2724             s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
2725         } else {
2726             s->picture_structure= PICT_FRAME;
2727             h->mb_aff_frame = h->sps.mb_aff;
2728         }
2729     }
2730     h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
2731
2732     if(h0->current_slice == 0){
2733         // Shorten frame num gaps so we don't have to allocate reference frames just to throw them away
2734         if(h->frame_num != h->prev_frame_num) {
2735             int unwrap_prev_frame_num = h->prev_frame_num, max_frame_num = 1<<h->sps.log2_max_frame_num;
2736
2737             if (unwrap_prev_frame_num > h->frame_num) unwrap_prev_frame_num -= max_frame_num;
2738
2739             if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
2740                 unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
2741                 if (unwrap_prev_frame_num < 0)
2742                     unwrap_prev_frame_num += max_frame_num;
2743
2744                 h->prev_frame_num = unwrap_prev_frame_num;
2745             }
2746         }
2747
2748         while(h->frame_num !=  h->prev_frame_num &&
2749               h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
2750             Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
2751             av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
2752             if (ff_h264_frame_start(h) < 0)
2753                 return -1;
2754             h->prev_frame_num++;
2755             h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
2756             s->current_picture_ptr->frame_num= h->prev_frame_num;
2757             ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
2758             ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
2759             ff_generate_sliding_window_mmcos(h);
2760             ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
2761             /* Error concealment: if a ref is missing, copy the previous ref in its place.
2762              * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
2763              * about there being no actual duplicates.
2764              * FIXME: this doesn't copy padding for out-of-frame motion vectors.  Given we're
2765              * concealing a lost frame, this probably isn't noticable by comparison, but it should
2766              * be fixed. */
2767             if (h->short_ref_count) {
2768                 if (prev) {
2769                     av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
2770                                   (const uint8_t**)prev->data, prev->linesize,
2771                                   s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
2772                     h->short_ref[0]->poc = prev->poc+2;
2773                 }
2774                 h->short_ref[0]->frame_num = h->prev_frame_num;
2775             }
2776         }
2777
2778         /* See if we have a decoded first field looking for a pair... */
2779         if (s0->first_field) {
2780             assert(s0->current_picture_ptr);
2781             assert(s0->current_picture_ptr->data[0]);
2782             assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
2783
2784             /* figure out if we have a complementary field pair */
2785             if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
2786                 /*
2787                  * Previous field is unmatched. Don't display it, but let it
2788                  * remain for reference if marked as such.
2789                  */
2790                 s0->current_picture_ptr = NULL;
2791                 s0->first_field = FIELD_PICTURE;
2792
2793             } else {
2794                 if (h->nal_ref_idc &&
2795                         s0->current_picture_ptr->reference &&
2796                         s0->current_picture_ptr->frame_num != h->frame_num) {
2797                     /*
2798                      * This and previous field were reference, but had
2799                      * different frame_nums. Consider this field first in
2800                      * pair. Throw away previous field except for reference
2801                      * purposes.
2802                      */
2803                     s0->first_field = 1;
2804                     s0->current_picture_ptr = NULL;
2805
2806                 } else {
2807                     /* Second field in complementary pair */
2808                     s0->first_field = 0;
2809                 }
2810             }
2811
2812         } else {
2813             /* Frame or first field in a potentially complementary pair */
2814             assert(!s0->current_picture_ptr);
2815             s0->first_field = FIELD_PICTURE;
2816         }
2817
2818         if(!FIELD_PICTURE || s0->first_field) {
2819             if (ff_h264_frame_start(h) < 0) {
2820                 s0->first_field = 0;
2821                 return -1;
2822             }
2823         } else {
2824             ff_release_unused_pictures(s, 0);
2825         }
2826     }
2827     if(h != h0)
2828         clone_slice(h, h0);
2829
2830     s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
2831
2832     assert(s->mb_num == s->mb_width * s->mb_height);
2833     if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
2834        first_mb_in_slice                    >= s->mb_num){
2835         av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
2836         return -1;
2837     }
2838     s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
2839     s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
2840     if (s->picture_structure == PICT_BOTTOM_FIELD)
2841         s->resync_mb_y = s->mb_y = s->mb_y + 1;
2842     assert(s->mb_y < s->mb_height);
2843
2844     if(s->picture_structure==PICT_FRAME){
2845         h->curr_pic_num=   h->frame_num;
2846         h->max_pic_num= 1<< h->sps.log2_max_frame_num;
2847     }else{
2848         h->curr_pic_num= 2*h->frame_num + 1;
2849         h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
2850     }
2851
2852     if(h->nal_unit_type == NAL_IDR_SLICE){
2853         get_ue_golomb(&s->gb); /* idr_pic_id */
2854     }
2855
2856     if(h->sps.poc_type==0){
2857         h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
2858
2859         if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
2860             h->delta_poc_bottom= get_se_golomb(&s->gb);
2861         }
2862     }
2863
2864     if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
2865         h->delta_poc[0]= get_se_golomb(&s->gb);
2866
2867         if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
2868             h->delta_poc[1]= get_se_golomb(&s->gb);
2869     }
2870
2871     init_poc(h);
2872
2873     if(h->pps.redundant_pic_cnt_present){
2874         h->redundant_pic_count= get_ue_golomb(&s->gb);
2875     }
2876
2877     //set defaults, might be overridden a few lines later
2878     h->ref_count[0]= h->pps.ref_count[0];
2879     h->ref_count[1]= h->pps.ref_count[1];
2880
2881     if(h->slice_type_nos != AV_PICTURE_TYPE_I){
2882         unsigned max= (16<<(s->picture_structure != PICT_FRAME))-1;
2883         if(h->slice_type_nos == AV_PICTURE_TYPE_B){
2884             h->direct_spatial_mv_pred= get_bits1(&s->gb);
2885         }
2886         num_ref_idx_active_override_flag= get_bits1(&s->gb);
2887
2888         if(num_ref_idx_active_override_flag){
2889             h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
2890             if(h->slice_type_nos==AV_PICTURE_TYPE_B)
2891                 h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
2892
2893         }
2894         if(h->ref_count[0]-1 > max || h->ref_count[1]-1 > max){
2895             av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
2896             h->ref_count[0]= h->ref_count[1]= 1;
2897             return -1;
2898         }
2899         if(h->slice_type_nos == AV_PICTURE_TYPE_B)
2900             h->list_count= 2;
2901         else
2902             h->list_count= 1;
2903     }else
2904         h->list_count= 0;
2905
2906     if(!default_ref_list_done){
2907         ff_h264_fill_default_ref_list(h);
2908     }
2909
2910     if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0)
2911         return -1;
2912
2913     if(h->slice_type_nos!=AV_PICTURE_TYPE_I){
2914         s->last_picture_ptr= &h->ref_list[0][0];
2915         ff_copy_picture(&s->last_picture, s->last_picture_ptr);
2916     }
2917     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
2918         s->next_picture_ptr= &h->ref_list[1][0];
2919         ff_copy_picture(&s->next_picture, s->next_picture_ptr);
2920     }
2921
2922     if(   (h->pps.weighted_pred          && h->slice_type_nos == AV_PICTURE_TYPE_P )
2923        ||  (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== AV_PICTURE_TYPE_B ) )
2924         pred_weight_table(h);
2925     else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
2926         implicit_weight_table(h, -1);
2927     }else {
2928         h->use_weight = 0;
2929         for (i = 0; i < 2; i++) {
2930             h->luma_weight_flag[i]   = 0;
2931             h->chroma_weight_flag[i] = 0;
2932         }
2933     }
2934
2935     if(h->nal_ref_idc)
2936         ff_h264_decode_ref_pic_marking(h0, &s->gb);
2937
2938     if(FRAME_MBAFF){
2939         ff_h264_fill_mbaff_ref_list(h);
2940
2941         if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
2942             implicit_weight_table(h, 0);
2943             implicit_weight_table(h, 1);
2944         }
2945     }
2946
2947     if(h->slice_type_nos==AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
2948         ff_h264_direct_dist_scale_factor(h);
2949     ff_h264_direct_ref_list_init(h);
2950
2951     if( h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac ){
2952         tmp = get_ue_golomb_31(&s->gb);
2953         if(tmp > 2){
2954             av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
2955             return -1;
2956         }
2957         h->cabac_init_idc= tmp;
2958     }
2959
2960     h->last_qscale_diff = 0;
2961     tmp = h->pps.init_qp + get_se_golomb(&s->gb);
2962     if(tmp>51+6*(h->sps.bit_depth_luma-8)){
2963         av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
2964         return -1;
2965     }
2966     s->qscale= tmp;
2967     h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
2968     h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
2969     //FIXME qscale / qp ... stuff
2970     if(h->slice_type == AV_PICTURE_TYPE_SP){
2971         get_bits1(&s->gb); /* sp_for_switch_flag */
2972     }
2973     if(h->slice_type==AV_PICTURE_TYPE_SP || h->slice_type == AV_PICTURE_TYPE_SI){
2974         get_se_golomb(&s->gb); /* slice_qs_delta */
2975     }
2976
2977     h->deblocking_filter = 1;
2978     h->slice_alpha_c0_offset = 52;
2979     h->slice_beta_offset = 52;
2980     if( h->pps.deblocking_filter_parameters_present ) {
2981         tmp= get_ue_golomb_31(&s->gb);
2982         if(tmp > 2){
2983             av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
2984             return -1;
2985         }
2986         h->deblocking_filter= tmp;
2987         if(h->deblocking_filter < 2)
2988             h->deblocking_filter^= 1; // 1<->0
2989
2990         if( h->deblocking_filter ) {
2991             h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
2992             h->slice_beta_offset     += get_se_golomb(&s->gb) << 1;
2993             if(   h->slice_alpha_c0_offset > 104U
2994                || h->slice_beta_offset     > 104U){
2995                 av_log(s->avctx, AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", h->slice_alpha_c0_offset, h->slice_beta_offset);
2996                 return -1;
2997             }
2998         }
2999     }
3000
3001     if(   s->avctx->skip_loop_filter >= AVDISCARD_ALL
3002        ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != AV_PICTURE_TYPE_I)
3003        ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR  && h->slice_type_nos == AV_PICTURE_TYPE_B)
3004        ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
3005         h->deblocking_filter= 0;
3006
3007     if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
3008         if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
3009             /* Cheat slightly for speed:
3010                Do not bother to deblock across slices. */
3011             h->deblocking_filter = 2;
3012         } else {
3013             h0->max_contexts = 1;
3014             if(!h0->single_decode_warning) {
3015                 av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
3016                 h0->single_decode_warning = 1;
3017             }
3018             if (h != h0) {
3019                 av_log(h->s.avctx, AV_LOG_ERROR, "Deblocking switched inside frame.\n");
3020                 return 1;
3021             }
3022         }
3023     }
3024     h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
3025
3026 #if 0 //FMO
3027     if( h->pps.num_slice_groups > 1  && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
3028         slice_group_change_cycle= get_bits(&s->gb, ?);
3029 #endif
3030
3031     h0->last_slice_type = slice_type;
3032     h->slice_num = ++h0->current_slice;
3033     if(h->slice_num >= MAX_SLICES){
3034         av_log(s->avctx, AV_LOG_ERROR, "Too many slices (%d >= %d), increase MAX_SLICES and recompile\n", h->slice_num, MAX_SLICES);
3035     }
3036
3037     for(j=0; j<2; j++){
3038         int id_list[16];
3039         int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
3040         for(i=0; i<16; i++){
3041             id_list[i]= 60;
3042             if(h->ref_list[j][i].data[0]){
3043                 int k;
3044                 uint8_t *base= h->ref_list[j][i].base[0];
3045                 for(k=0; k<h->short_ref_count; k++)
3046                     if(h->short_ref[k]->base[0] == base){
3047                         id_list[i]= k;
3048                         break;
3049                     }
3050                 for(k=0; k<h->long_ref_count; k++)
3051                     if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
3052                         id_list[i]= h->short_ref_count + k;
3053                         break;
3054                     }
3055             }
3056         }
3057
3058         ref2frm[0]=
3059         ref2frm[1]= -1;
3060         for(i=0; i<16; i++)
3061             ref2frm[i+2]= 4*id_list[i]
3062                           +(h->ref_list[j][i].reference&3);
3063         ref2frm[18+0]=
3064         ref2frm[18+1]= -1;
3065         for(i=16; i<48; i++)
3066             ref2frm[i+4]= 4*id_list[(i-16)>>1]
3067                           +(h->ref_list[j][i].reference&3);
3068     }
3069
3070     //FIXME: fix draw_edges+PAFF+frame threads
3071     h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type)) ? 0 : 16;
3072     h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
3073
3074     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
3075         av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
3076                h->slice_num,
3077                (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
3078                first_mb_in_slice,
3079                av_get_picture_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
3080                pps_id, h->frame_num,
3081                s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
3082                h->ref_count[0], h->ref_count[1],
3083                s->qscale,
3084                h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
3085                h->use_weight,
3086                h->use_weight==1 && h->use_weight_chroma ? "c" : "",
3087                h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
3088                );
3089     }
3090
3091     return 0;
3092 }
3093
3094 int ff_h264_get_slice_type(const H264Context *h)
3095 {
3096     switch (h->slice_type) {
3097     case AV_PICTURE_TYPE_P:  return 0;
3098     case AV_PICTURE_TYPE_B:  return 1;
3099     case AV_PICTURE_TYPE_I:  return 2;
3100     case AV_PICTURE_TYPE_SP: return 3;
3101     case AV_PICTURE_TYPE_SI: return 4;
3102     default:         return -1;
3103     }
3104 }
3105
3106 /**
3107  *
3108  * @return non zero if the loop filter can be skiped
3109  */
3110 static int fill_filter_caches(H264Context *h, int mb_type){
3111     MpegEncContext * const s = &h->s;
3112     const int mb_xy= h->mb_xy;
3113     int top_xy, left_xy[2];
3114     int top_type, left_type[2];
3115
3116     top_xy     = mb_xy  - (s->mb_stride << MB_FIELD);
3117
3118     //FIXME deblocking could skip the intra and nnz parts.
3119
3120     /* Wow, what a mess, why didn't they simplify the interlacing & intra
3121      * stuff, I can't imagine that these complex rules are worth it. */
3122
3123     left_xy[1] = left_xy[0] = mb_xy-1;
3124     if(FRAME_MBAFF){
3125         const int left_mb_field_flag     = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
3126         const int curr_mb_field_flag     = IS_INTERLACED(mb_type);
3127         if(s->mb_y&1){
3128             if (left_mb_field_flag != curr_mb_field_flag) {
3129                 left_xy[0] -= s->mb_stride;
3130             }
3131         }else{
3132             if(curr_mb_field_flag){
3133                 top_xy      += s->mb_stride & (((s->current_picture.mb_type[top_xy    ]>>7)&1)-1);
3134             }
3135             if (left_mb_field_flag != curr_mb_field_flag) {
3136                 left_xy[1] += s->mb_stride;
3137             }
3138         }
3139     }
3140
3141     h->top_mb_xy = top_xy;
3142     h->left_mb_xy[0] = left_xy[0];
3143     h->left_mb_xy[1] = left_xy[1];
3144     {
3145         //for sufficiently low qp, filtering wouldn't do anything
3146         //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
3147         int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
3148         int qp = s->current_picture.qscale_table[mb_xy];
3149         if(qp <= qp_thresh
3150            && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
3151            && (top_xy   < 0 || ((qp + s->current_picture.qscale_table[top_xy    ] + 1)>>1) <= qp_thresh)){
3152             if(!FRAME_MBAFF)
3153                 return 1;
3154             if(   (left_xy[0]< 0            || ((qp + s->current_picture.qscale_table[left_xy[1]             ] + 1)>>1) <= qp_thresh)
3155                && (top_xy    < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy    -s->mb_stride] + 1)>>1) <= qp_thresh))
3156                 return 1;
3157         }
3158     }
3159
3160     top_type     = s->current_picture.mb_type[top_xy]    ;
3161     left_type[0] = s->current_picture.mb_type[left_xy[0]];
3162     left_type[1] = s->current_picture.mb_type[left_xy[1]];
3163     if(h->deblocking_filter == 2){
3164         if(h->slice_table[top_xy     ] != h->slice_num) top_type= 0;
3165         if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
3166     }else{
3167         if(h->slice_table[top_xy     ] == 0xFFFF) top_type= 0;
3168         if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
3169     }
3170     h->top_type    = top_type    ;
3171     h->left_type[0]= left_type[0];
3172     h->left_type[1]= left_type[1];
3173
3174     if(IS_INTRA(mb_type))
3175         return 0;
3176
3177     AV_COPY32(&h->non_zero_count_cache[4+8* 1], &h->non_zero_count[mb_xy][ 0]);
3178     AV_COPY32(&h->non_zero_count_cache[4+8* 2], &h->non_zero_count[mb_xy][ 4]);
3179     AV_COPY32(&h->non_zero_count_cache[4+8* 3], &h->non_zero_count[mb_xy][ 8]);
3180     AV_COPY32(&h->non_zero_count_cache[4+8* 4], &h->non_zero_count[mb_xy][12]);
3181
3182     h->cbp= h->cbp_table[mb_xy];
3183
3184     {
3185         int list;
3186         for(list=0; list<h->list_count; list++){
3187             int8_t *ref;
3188             int y, b_stride;
3189             int16_t (*mv_dst)[2];
3190             int16_t (*mv_src)[2];
3191
3192             if(!USES_LIST(mb_type, list)){
3193                 fill_rectangle(  h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
3194                 AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3195                 AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3196                 AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3197                 AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3198                 continue;
3199             }
3200
3201             ref = &s->current_picture.ref_index[list][4*mb_xy];
3202             {
3203                 int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
3204                 AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3205                 AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3206                 ref += 2;
3207                 AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3208                 AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3209             }
3210
3211             b_stride = h->b_stride;
3212             mv_dst   = &h->mv_cache[list][scan8[0]];
3213             mv_src   = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
3214             for(y=0; y<4; y++){
3215                 AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
3216             }
3217
3218         }
3219     }
3220
3221
3222 /*
3223 0 . T T. T T T T
3224 1 L . .L . . . .
3225 2 L . .L . . . .
3226 3 . T TL . . . .
3227 4 L . .L . . . .
3228 5 L . .. . . . .
3229 */
3230 //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
3231     if(top_type){
3232         AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][3*4]);
3233     }
3234
3235     if(left_type[0]){
3236         h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][3+0*4];
3237         h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][3+1*4];
3238         h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][3+2*4];
3239         h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][3+3*4];
3240     }
3241
3242     // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
3243     if(!CABAC && h->pps.transform_8x8_mode){
3244         if(IS_8x8DCT(top_type)){
3245             h->non_zero_count_cache[4+8*0]=
3246             h->non_zero_count_cache[5+8*0]= (h->cbp_table[top_xy] & 0x4000) >> 12;
3247             h->non_zero_count_cache[6+8*0]=
3248             h->non_zero_count_cache[7+8*0]= (h->cbp_table[top_xy] & 0x8000) >> 12;
3249         }
3250         if(IS_8x8DCT(left_type[0])){
3251             h->non_zero_count_cache[3+8*1]=
3252             h->non_zero_count_cache[3+8*2]= (h->cbp_table[left_xy[0]]&0x2000) >> 12; //FIXME check MBAFF
3253         }
3254         if(IS_8x8DCT(left_type[1])){
3255             h->non_zero_count_cache[3+8*3]=
3256             h->non_zero_count_cache[3+8*4]= (h->cbp_table[left_xy[1]]&0x8000) >> 12; //FIXME check MBAFF
3257         }
3258
3259         if(IS_8x8DCT(mb_type)){
3260             h->non_zero_count_cache[scan8[0   ]]= h->non_zero_count_cache[scan8[1   ]]=
3261             h->non_zero_count_cache[scan8[2   ]]= h->non_zero_count_cache[scan8[3   ]]= (h->cbp & 0x1000) >> 12;
3262
3263             h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
3264             h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= (h->cbp & 0x2000) >> 12;
3265
3266             h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
3267             h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= (h->cbp & 0x4000) >> 12;
3268
3269             h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
3270             h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= (h->cbp & 0x8000) >> 12;
3271         }
3272     }
3273
3274     if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
3275         int list;
3276         for(list=0; list<h->list_count; list++){
3277             if(USES_LIST(top_type, list)){
3278                 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
3279                 const int b8_xy= 4*top_xy + 2;
3280                 int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
3281                 AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
3282                 h->ref_cache[list][scan8[0] + 0 - 1*8]=
3283                 h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
3284                 h->ref_cache[list][scan8[0] + 2 - 1*8]=
3285                 h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
3286             }else{
3287                 AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
3288                 AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3289             }
3290
3291             if(!IS_INTERLACED(mb_type^left_type[0])){
3292                 if(USES_LIST(left_type[0], list)){
3293                     const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
3294                     const int b8_xy= 4*left_xy[0] + 1;
3295                     int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
3296                     AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
3297                     AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
3298                     AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
3299                     AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
3300                     h->ref_cache[list][scan8[0] - 1 + 0 ]=
3301                     h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
3302                     h->ref_cache[list][scan8[0] - 1 +16 ]=
3303                     h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
3304                 }else{
3305                     AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
3306                     AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
3307                     AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
3308                     AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
3309                     h->ref_cache[list][scan8[0] - 1 + 0  ]=
3310                     h->ref_cache[list][scan8[0] - 1 + 8  ]=
3311                     h->ref_cache[list][scan8[0] - 1 + 16 ]=
3312                     h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
3313                 }
3314             }
3315         }
3316     }
3317
3318     return 0;
3319 }
3320
3321 static void loop_filter(H264Context *h, int start_x, int end_x){
3322     MpegEncContext * const s = &h->s;
3323     uint8_t  *dest_y, *dest_cb, *dest_cr;
3324     int linesize, uvlinesize, mb_x, mb_y;
3325     const int end_mb_y= s->mb_y + FRAME_MBAFF;
3326     const int old_slice_type= h->slice_type;
3327     const int pixel_shift = h->pixel_shift;
3328
3329     if(h->deblocking_filter) {
3330         for(mb_x= start_x; mb_x<end_x; mb_x++){
3331             for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
3332                 int mb_xy, mb_type;
3333                 mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
3334                 h->slice_num= h->slice_table[mb_xy];
3335                 mb_type= s->current_picture.mb_type[mb_xy];
3336                 h->list_count= h->list_counts[mb_xy];
3337
3338                 if(FRAME_MBAFF)
3339                     h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
3340
3341                 s->mb_x= mb_x;
3342                 s->mb_y= mb_y;
3343                 dest_y  = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
3344                 dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * (8 << CHROMA444);
3345                 dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * (8 << CHROMA444);
3346                     //FIXME simplify above
3347
3348                 if (MB_FIELD) {
3349                     linesize   = h->mb_linesize   = s->linesize * 2;
3350                     uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
3351                     if(mb_y&1){ //FIXME move out of this function?
3352                         dest_y -= s->linesize*15;
3353                         dest_cb-= s->uvlinesize*((8 << CHROMA444)-1);
3354                         dest_cr-= s->uvlinesize*((8 << CHROMA444)-1);
3355                     }
3356                 } else {
3357                     linesize   = h->mb_linesize   = s->linesize;
3358                     uvlinesize = h->mb_uvlinesize = s->uvlinesize;
3359                 }
3360                 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, CHROMA444, 0);
3361                 if(fill_filter_caches(h, mb_type))
3362                     continue;
3363                 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
3364                 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
3365
3366                 if (FRAME_MBAFF) {
3367                     ff_h264_filter_mb     (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3368                 } else {
3369                     ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3370                 }
3371             }
3372         }
3373     }
3374     h->slice_type= old_slice_type;
3375     s->mb_x= end_x;
3376     s->mb_y= end_mb_y - FRAME_MBAFF;
3377     h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
3378     h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
3379 }
3380
3381 static void predict_field_decoding_flag(H264Context *h){
3382     MpegEncContext * const s = &h->s;
3383     const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
3384     int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
3385                 ? s->current_picture.mb_type[mb_xy-1]
3386                 : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
3387                 ? s->current_picture.mb_type[mb_xy-s->mb_stride]
3388                 : 0;
3389     h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
3390 }
3391
3392 /**
3393  * Draw edges and report progress for the last MB row.
3394  */
3395 static void decode_finish_row(H264Context *h){
3396     MpegEncContext * const s = &h->s;
3397     int top = 16*(s->mb_y >> FIELD_PICTURE);
3398     int height = 16 << FRAME_MBAFF;
3399     int deblock_border = (16 + 4) << FRAME_MBAFF;
3400     int pic_height = 16*s->mb_height >> FIELD_PICTURE;
3401
3402     if (h->deblocking_filter) {
3403         if((top + height) >= pic_height)
3404             height += deblock_border;
3405
3406         top -= deblock_border;
3407     }
3408
3409     if (top >= pic_height || (top + height) < h->emu_edge_height)
3410         return;
3411
3412     height = FFMIN(height, pic_height - top);
3413     if (top < h->emu_edge_height) {
3414         height = top+height;
3415         top = 0;
3416     }
3417
3418     ff_draw_horiz_band(s, top, height);
3419
3420     if (s->dropable) return;
3421
3422     ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
3423                              s->picture_structure==PICT_BOTTOM_FIELD);
3424 }
3425
3426 static int decode_slice(struct AVCodecContext *avctx, void *arg){
3427     H264Context *h = *(void**)arg;
3428     MpegEncContext * const s = &h->s;
3429     const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
3430     int lf_x_start = s->mb_x;
3431
3432     s->mb_skip_run= -1;
3433
3434     h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
3435                     (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
3436
3437     if( h->pps.cabac ) {
3438         /* realign */
3439         align_get_bits( &s->gb );
3440
3441         /* init cabac */
3442         ff_init_cabac_states( &h->cabac);
3443         ff_init_cabac_decoder( &h->cabac,
3444                                s->gb.buffer + get_bits_count(&s->gb)/8,
3445                                (get_bits_left(&s->gb) + 7)/8);
3446
3447         ff_h264_init_cabac_states(h);
3448
3449         for(;;){
3450 //START_TIMER
3451             int ret = ff_h264_decode_mb_cabac(h);
3452             int eos;
3453 //STOP_TIMER("decode_mb_cabac")
3454
3455             if(ret>=0) ff_h264_hl_decode_mb(h);
3456
3457             if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
3458                 s->mb_y++;