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