license header consistency cosmetics.
[libav.git] / libavcodec / h264.c
CommitLineData
0da71265
MN
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
0da71265
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
0da71265 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
0da71265
MN
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0da71265 20 */
115329f1 21
0da71265
MN
22/**
23 * @file h264.c
24 * H.264 / AVC / MPEG4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
0da71265
MN
28#include "dsputil.h"
29#include "avcodec.h"
30#include "mpegvideo.h"
26b4fe82 31#include "h264.h"
0da71265 32#include "h264data.h"
26b4fe82 33#include "h264_parser.h"
0da71265
MN
34#include "golomb.h"
35
e5017ab8
LA
36#include "cabac.h"
37
2848ce84 38//#undef NDEBUG
0da71265
MN
39#include <assert.h>
40
2ddcf84b
JD
41/**
42 * Value of Picture.reference when Picture is not a reference picture, but
43 * is held for delayed output.
44 */
45#define DELAYED_PIC_REF 4
46
0da71265
MN
47static VLC coeff_token_vlc[4];
48static VLC chroma_dc_coeff_token_vlc;
49
50static VLC total_zeros_vlc[15];
51static VLC chroma_dc_total_zeros_vlc[3];
52
53static VLC run_vlc[6];
54static VLC run7_vlc;
55
8b82a956
MN
56static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
57static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
6ba71fc4 58static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
3e20143e 59static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
8b82a956 60
849f1035 61static av_always_inline uint32_t pack16to32(int a, int b){
377ec888
MN
62#ifdef WORDS_BIGENDIAN
63 return (b&0xFFFF) + (a<<16);
64#else
65 return (a&0xFFFF) + (b<<16);
66#endif
67}
68
acd8d10f
PI
69const uint8_t ff_rem6[52]={
700, 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,
71};
72
73const uint8_t ff_div6[52]={
740, 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,
75};
76
77
0da71265
MN
78/**
79 * fill a rectangle.
5175b937
LLL
80 * @param h height of the rectangle, should be a constant
81 * @param w width of the rectangle, should be a constant
0da71265
MN
82 * @param size the size of val (1 or 4), should be a constant
83 */
849f1035 84static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
af6e2fed 85 uint8_t *p= (uint8_t*)vp;
0da71265 86 assert(size==1 || size==4);
67a82086 87 assert(w<=4);
115329f1 88
0da71265
MN
89 w *= size;
90 stride *= size;
115329f1 91
4733abcb 92 assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
d9c780a8 93 assert((stride&(w-1))==0);
67a82086
LM
94 if(w==2){
95 const uint16_t v= size==4 ? val : val*0x0101;
96 *(uint16_t*)(p + 0*stride)= v;
97 if(h==1) return;
98 *(uint16_t*)(p + 1*stride)= v;
99 if(h==2) return;
6d324c81 100 *(uint16_t*)(p + 2*stride)= v;
67a82086
LM
101 *(uint16_t*)(p + 3*stride)= v;
102 }else if(w==4){
103 const uint32_t v= size==4 ? val : val*0x01010101;
104 *(uint32_t*)(p + 0*stride)= v;
105 if(h==1) return;
106 *(uint32_t*)(p + 1*stride)= v;
107 if(h==2) return;
6d324c81 108 *(uint32_t*)(p + 2*stride)= v;
67a82086
LM
109 *(uint32_t*)(p + 3*stride)= v;
110 }else if(w==8){
111 //gcc can't optimize 64bit math on x86_32
112#if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
113 const uint64_t v= val*0x0100000001ULL;
114 *(uint64_t*)(p + 0*stride)= v;
115 if(h==1) return;
116 *(uint64_t*)(p + 1*stride)= v;
117 if(h==2) return;
6d324c81 118 *(uint64_t*)(p + 2*stride)= v;
67a82086
LM
119 *(uint64_t*)(p + 3*stride)= v;
120 }else if(w==16){
121 const uint64_t v= val*0x0100000001ULL;
6d324c81
AS
122 *(uint64_t*)(p + 0+0*stride)= v;
123 *(uint64_t*)(p + 8+0*stride)= v;
124 *(uint64_t*)(p + 0+1*stride)= v;
67a82086
LM
125 *(uint64_t*)(p + 8+1*stride)= v;
126 if(h==2) return;
6d324c81
AS
127 *(uint64_t*)(p + 0+2*stride)= v;
128 *(uint64_t*)(p + 8+2*stride)= v;
129 *(uint64_t*)(p + 0+3*stride)= v;
67a82086
LM
130 *(uint64_t*)(p + 8+3*stride)= v;
131#else
6d324c81 132 *(uint32_t*)(p + 0+0*stride)= val;
67a82086
LM
133 *(uint32_t*)(p + 4+0*stride)= val;
134 if(h==1) return;
6d324c81 135 *(uint32_t*)(p + 0+1*stride)= val;
67a82086
LM
136 *(uint32_t*)(p + 4+1*stride)= val;
137 if(h==2) return;
6d324c81
AS
138 *(uint32_t*)(p + 0+2*stride)= val;
139 *(uint32_t*)(p + 4+2*stride)= val;
140 *(uint32_t*)(p + 0+3*stride)= val;
67a82086
LM
141 *(uint32_t*)(p + 4+3*stride)= val;
142 }else if(w==16){
6d324c81
AS
143 *(uint32_t*)(p + 0+0*stride)= val;
144 *(uint32_t*)(p + 4+0*stride)= val;
145 *(uint32_t*)(p + 8+0*stride)= val;
146 *(uint32_t*)(p +12+0*stride)= val;
147 *(uint32_t*)(p + 0+1*stride)= val;
148 *(uint32_t*)(p + 4+1*stride)= val;
149 *(uint32_t*)(p + 8+1*stride)= val;
67a82086
LM
150 *(uint32_t*)(p +12+1*stride)= val;
151 if(h==2) return;
6d324c81
AS
152 *(uint32_t*)(p + 0+2*stride)= val;
153 *(uint32_t*)(p + 4+2*stride)= val;
154 *(uint32_t*)(p + 8+2*stride)= val;
155 *(uint32_t*)(p +12+2*stride)= val;
156 *(uint32_t*)(p + 0+3*stride)= val;
157 *(uint32_t*)(p + 4+3*stride)= val;
158 *(uint32_t*)(p + 8+3*stride)= val;
67a82086
LM
159 *(uint32_t*)(p +12+3*stride)= val;
160#endif
0da71265
MN
161 }else
162 assert(0);
67a82086 163 assert(h==4);
0da71265
MN
164}
165
70abb407 166static void fill_caches(H264Context *h, int mb_type, int for_deblock){
0da71265 167 MpegEncContext * const s = &h->s;
7bc9090a 168 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265
MN
169 int topleft_xy, top_xy, topright_xy, left_xy[2];
170 int topleft_type, top_type, topright_type, left_type[2];
6867a90b 171 int left_block[8];
0da71265
MN
172 int i;
173
717b1733
LM
174 //FIXME deblocking could skip the intra and nnz parts.
175 if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
e2e5894a
LM
176 return;
177
115329f1
DB
178 //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
179
6867a90b
LLL
180 top_xy = mb_xy - s->mb_stride;
181 topleft_xy = top_xy - 1;
182 topright_xy= top_xy + 1;
183 left_xy[1] = left_xy[0] = mb_xy-1;
184 left_block[0]= 0;
185 left_block[1]= 1;
186 left_block[2]= 2;
187 left_block[3]= 3;
188 left_block[4]= 7;
189 left_block[5]= 10;
190 left_block[6]= 8;
191 left_block[7]= 11;
5d18eaad 192 if(FRAME_MBAFF){
6867a90b
LLL
193 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
194 const int top_pair_xy = pair_xy - s->mb_stride;
195 const int topleft_pair_xy = top_pair_xy - 1;
196 const int topright_pair_xy = top_pair_xy + 1;
197 const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
198 const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
199 const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
200 const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
201 const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
202 const int bottom = (s->mb_y & 1);
a9c9a240 203 tprintf(s->avctx, "fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
6867a90b
LLL
204 if (bottom
205 ? !curr_mb_frame_flag // bottom macroblock
206 : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
207 ) {
208 top_xy -= s->mb_stride;
209 }
210 if (bottom
211 ? !curr_mb_frame_flag // bottom macroblock
212 : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
213 ) {
214 topleft_xy -= s->mb_stride;
215 }
216 if (bottom
217 ? !curr_mb_frame_flag // bottom macroblock
218 : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
219 ) {
220 topright_xy -= s->mb_stride;
221 }
222 if (left_mb_frame_flag != curr_mb_frame_flag) {
223 left_xy[1] = left_xy[0] = pair_xy - 1;
224 if (curr_mb_frame_flag) {
225 if (bottom) {
226 left_block[0]= 2;
227 left_block[1]= 2;
228 left_block[2]= 3;
229 left_block[3]= 3;
230 left_block[4]= 8;
231 left_block[5]= 11;
232 left_block[6]= 8;
233 left_block[7]= 11;
234 } else {
235 left_block[0]= 0;
236 left_block[1]= 0;
237 left_block[2]= 1;
238 left_block[3]= 1;
239 left_block[4]= 7;
240 left_block[5]= 10;
241 left_block[6]= 7;
242 left_block[7]= 10;
243 }
244 } else {
245 left_xy[1] += s->mb_stride;
246 //left_block[0]= 0;
247 left_block[1]= 2;
248 left_block[2]= 0;
249 left_block[3]= 2;
250 //left_block[4]= 7;
251 left_block[5]= 10;
252 left_block[6]= 7;
253 left_block[7]= 10;
254 }
255 }
0da71265
MN
256 }
257
826de46e
LLL
258 h->top_mb_xy = top_xy;
259 h->left_mb_xy[0] = left_xy[0];
260 h->left_mb_xy[1] = left_xy[1];
6ba71fc4 261 if(for_deblock){
717b1733
LM
262 topleft_type = 0;
263 topright_type = 0;
46f2f05f 264 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
46f2f05f
MN
265 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
266 left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
5d18eaad
LM
267
268 if(FRAME_MBAFF && !IS_INTRA(mb_type)){
269 int list;
270 int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
271 for(i=0; i<16; i++)
272 h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
3425501d 273 for(list=0; list<h->list_count; list++){
5d18eaad
LM
274 if(USES_LIST(mb_type,list)){
275 uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
276 uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
191e8ca7 277 int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
5d18eaad
LM
278 for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
279 dst[0] = src[0];
280 dst[1] = src[1];
281 dst[2] = src[2];
282 dst[3] = src[3];
283 }
284 *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
285 *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
286 ref += h->b8_stride;
287 *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
288 *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
289 }else{
290 fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
291 fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
292 }
293 }
294 }
46f2f05f
MN
295 }else{
296 topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
297 top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
298 topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
299 left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
300 left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
301 }
0da71265
MN
302
303 if(IS_INTRA(mb_type)){
115329f1
DB
304 h->topleft_samples_available=
305 h->top_samples_available=
0da71265
MN
306 h->left_samples_available= 0xFFFF;
307 h->topright_samples_available= 0xEEEA;
308
309 if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
310 h->topleft_samples_available= 0xB3FF;
311 h->top_samples_available= 0x33FF;
312 h->topright_samples_available= 0x26EA;
313 }
314 for(i=0; i<2; i++){
315 if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
316 h->topleft_samples_available&= 0xDF5F;
317 h->left_samples_available&= 0x5F5F;
318 }
319 }
115329f1 320
0da71265
MN
321 if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
322 h->topleft_samples_available&= 0x7FFF;
115329f1 323
0da71265
MN
324 if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
325 h->topright_samples_available&= 0xFBFF;
115329f1 326
0da71265
MN
327 if(IS_INTRA4x4(mb_type)){
328 if(IS_INTRA4x4(top_type)){
329 h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
330 h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
331 h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
332 h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
333 }else{
334 int pred;
6fbcaaa0 335 if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
0da71265 336 pred= -1;
6fbcaaa0
LLL
337 else{
338 pred= 2;
0da71265
MN
339 }
340 h->intra4x4_pred_mode_cache[4+8*0]=
341 h->intra4x4_pred_mode_cache[5+8*0]=
342 h->intra4x4_pred_mode_cache[6+8*0]=
343 h->intra4x4_pred_mode_cache[7+8*0]= pred;
344 }
345 for(i=0; i<2; i++){
346 if(IS_INTRA4x4(left_type[i])){
347 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
348 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
349 }else{
350 int pred;
6fbcaaa0 351 if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
0da71265 352 pred= -1;
6fbcaaa0
LLL
353 else{
354 pred= 2;
0da71265
MN
355 }
356 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
357 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
358 }
359 }
360 }
361 }
115329f1
DB
362
363
0da71265 364/*
115329f1
DB
3650 . T T. T T T T
3661 L . .L . . . .
3672 L . .L . . . .
3683 . T TL . . . .
3694 L . .L . . . .
3705 L . .. . . . .
0da71265
MN
371*/
372//FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
373 if(top_type){
6867a90b
LLL
374 h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
375 h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
376 h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
53c05b1e 377 h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
115329f1 378
6867a90b 379 h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
53c05b1e 380 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
115329f1 381
6867a90b 382 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
53c05b1e 383 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
115329f1 384
0da71265 385 }else{
115329f1 386 h->non_zero_count_cache[4+8*0]=
0da71265
MN
387 h->non_zero_count_cache[5+8*0]=
388 h->non_zero_count_cache[6+8*0]=
389 h->non_zero_count_cache[7+8*0]=
115329f1 390
0da71265
MN
391 h->non_zero_count_cache[1+8*0]=
392 h->non_zero_count_cache[2+8*0]=
115329f1 393
0da71265 394 h->non_zero_count_cache[1+8*3]=
3981c385 395 h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
115329f1 396
0da71265 397 }
826de46e 398
6867a90b
LLL
399 for (i=0; i<2; i++) {
400 if(left_type[i]){
401 h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
402 h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
403 h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
404 h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
6867a90b 405 }else{
115329f1
DB
406 h->non_zero_count_cache[3+8*1 + 2*8*i]=
407 h->non_zero_count_cache[3+8*2 + 2*8*i]=
408 h->non_zero_count_cache[0+8*1 + 8*i]=
6867a90b 409 h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
826de46e
LLL
410 }
411 }
412
413 if( h->pps.cabac ) {
414 // top_cbp
415 if(top_type) {
416 h->top_cbp = h->cbp_table[top_xy];
417 } else if(IS_INTRA(mb_type)) {
418 h->top_cbp = 0x1C0;
419 } else {
420 h->top_cbp = 0;
421 }
422 // left_cbp
423 if (left_type[0]) {
424 h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
425 } else if(IS_INTRA(mb_type)) {
426 h->left_cbp = 0x1C0;
427 } else {
428 h->left_cbp = 0;
429 }
430 if (left_type[0]) {
431 h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
432 }
433 if (left_type[1]) {
434 h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
6867a90b 435 }
0da71265 436 }
6867a90b 437
0da71265 438#if 1
e2e5894a 439 if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
0da71265 440 int list;
3425501d 441 for(list=0; list<h->list_count; list++){
e2e5894a 442 if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
0da71265
MN
443 /*if(!h->mv_cache_clean[list]){
444 memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
445 memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
446 h->mv_cache_clean[list]= 1;
447 }*/
5ad984c9 448 continue;
0da71265
MN
449 }
450 h->mv_cache_clean[list]= 0;
115329f1 451
53b19144 452 if(USES_LIST(top_type, list)){
0da71265
MN
453 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
454 const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
455 *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
456 *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
457 *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
458 *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
459 h->ref_cache[list][scan8[0] + 0 - 1*8]=
460 h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
461 h->ref_cache[list][scan8[0] + 2 - 1*8]=
462 h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
463 }else{
115329f1
DB
464 *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
465 *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
466 *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
0da71265
MN
467 *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
468 *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
469 }
470
4672503d
LM
471 for(i=0; i<2; i++){
472 int cache_idx = scan8[0] - 1 + i*2*8;
473 if(USES_LIST(left_type[i], list)){
474 const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
475 const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
476 *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
477 *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
478 h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
479 h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
480 }else{
481 *(uint32_t*)h->mv_cache [list][cache_idx ]=
482 *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
483 h->ref_cache[list][cache_idx ]=
484 h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
485 }
0da71265
MN
486 }
487
ae08a563 488 if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
46f2f05f
MN
489 continue;
490
53b19144 491 if(USES_LIST(topleft_type, list)){
e2e5894a
LM
492 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
493 const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
494 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
495 h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
496 }else{
497 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
498 h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
499 }
115329f1 500
53b19144 501 if(USES_LIST(topright_type, list)){
e2e5894a
LM
502 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
503 const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
504 *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
505 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
506 }else{
507 *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
508 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
509 }
e2e5894a 510
ae08a563 511 if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
717b1733 512 continue;
115329f1
DB
513
514 h->ref_cache[list][scan8[5 ]+1] =
515 h->ref_cache[list][scan8[7 ]+1] =
3b66c4c5 516 h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
115329f1 517 h->ref_cache[list][scan8[4 ]] =
0da71265
MN
518 h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
519 *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
520 *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
3b66c4c5 521 *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
0da71265
MN
522 *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
523 *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
9e528114
LA
524
525 if( h->pps.cabac ) {
526 /* XXX beurk, Load mvd */
53b19144 527 if(USES_LIST(top_type, list)){
9e528114
LA
528 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
529 *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
530 *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
531 *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
532 *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
533 }else{
115329f1
DB
534 *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
535 *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
536 *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
9e528114
LA
537 *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
538 }
53b19144 539 if(USES_LIST(left_type[0], list)){
9e528114
LA
540 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
541 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
542 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
543 }else{
544 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
545 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
546 }
53b19144 547 if(USES_LIST(left_type[1], list)){
9e528114
LA
548 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
549 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
550 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
551 }else{
552 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
553 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
554 }
555 *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
556 *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
3b66c4c5 557 *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
9e528114
LA
558 *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
559 *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
5ad984c9
LM
560
561 if(h->slice_type == B_TYPE){
562 fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
563
564 if(IS_DIRECT(top_type)){
565 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
566 }else if(IS_8X8(top_type)){
567 int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
568 h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
569 h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
570 }else{
571 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
572 }
115329f1 573
5d18eaad
LM
574 if(IS_DIRECT(left_type[0]))
575 h->direct_cache[scan8[0] - 1 + 0*8]= 1;
576 else if(IS_8X8(left_type[0]))
577 h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
578 else
579 h->direct_cache[scan8[0] - 1 + 0*8]= 0;
580
581 if(IS_DIRECT(left_type[1]))
5ad984c9 582 h->direct_cache[scan8[0] - 1 + 2*8]= 1;
5d18eaad
LM
583 else if(IS_8X8(left_type[1]))
584 h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
585 else
5ad984c9 586 h->direct_cache[scan8[0] - 1 + 2*8]= 0;
5d18eaad
LM
587 }
588 }
589
590 if(FRAME_MBAFF){
591#define MAP_MVS\
592 MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
593 MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
594 MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
595 MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
596 MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
597 MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
598 MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
599 MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
600 MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
601 MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
602 if(MB_FIELD){
603#define MAP_F2F(idx, mb_type)\
604 if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
605 h->ref_cache[list][idx] <<= 1;\
606 h->mv_cache[list][idx][1] /= 2;\
607 h->mvd_cache[list][idx][1] /= 2;\
608 }
609 MAP_MVS
610#undef MAP_F2F
611 }else{
612#define MAP_F2F(idx, mb_type)\
613 if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
614 h->ref_cache[list][idx] >>= 1;\
615 h->mv_cache[list][idx][1] <<= 1;\
616 h->mvd_cache[list][idx][1] <<= 1;\
5ad984c9 617 }
5d18eaad
LM
618 MAP_MVS
619#undef MAP_F2F
5ad984c9 620 }
9e528114 621 }
0da71265 622 }
0da71265
MN
623 }
624#endif
43efd19a
LM
625
626 h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
0da71265
MN
627}
628
629static inline void write_back_intra_pred_mode(H264Context *h){
630 MpegEncContext * const s = &h->s;
7bc9090a 631 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265
MN
632
633 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
634 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
635 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
636 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
637 h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
638 h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
639 h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
640}
641
642/**
643 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
644 */
645static inline int check_intra4x4_pred_mode(H264Context *h){
646 MpegEncContext * const s = &h->s;
647 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
648 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
649 int i;
115329f1 650
0da71265
MN
651 if(!(h->top_samples_available&0x8000)){
652 for(i=0; i<4; i++){
653 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
654 if(status<0){
9b879566 655 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);
0da71265
MN
656 return -1;
657 } else if(status){
658 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
659 }
660 }
661 }
115329f1 662
0da71265
MN
663 if(!(h->left_samples_available&0x8000)){
664 for(i=0; i<4; i++){
665 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
666 if(status<0){
9b879566 667 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);
0da71265
MN
668 return -1;
669 } else if(status){
670 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
671 }
672 }
673 }
674
675 return 0;
676} //FIXME cleanup like next
677
678/**
679 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
680 */
681static inline int check_intra_pred_mode(H264Context *h, int mode){
682 MpegEncContext * const s = &h->s;
683 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
684 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
115329f1 685
43ff0714 686 if(mode > 6U) {
5175b937 687 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);
7440fe83 688 return -1;
5175b937 689 }
115329f1 690
0da71265
MN
691 if(!(h->top_samples_available&0x8000)){
692 mode= top[ mode ];
693 if(mode<0){
9b879566 694 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);
0da71265
MN
695 return -1;
696 }
697 }
115329f1 698
0da71265
MN
699 if(!(h->left_samples_available&0x8000)){
700 mode= left[ mode ];
701 if(mode<0){
9b879566 702 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);
0da71265 703 return -1;
115329f1 704 }
0da71265
MN
705 }
706
707 return mode;
708}
709
710/**
711 * gets the predicted intra4x4 prediction mode.
712 */
713static inline int pred_intra_mode(H264Context *h, int n){
714 const int index8= scan8[n];
715 const int left= h->intra4x4_pred_mode_cache[index8 - 1];
716 const int top = h->intra4x4_pred_mode_cache[index8 - 8];
717 const int min= FFMIN(left, top);
718
a9c9a240 719 tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
0da71265
MN
720
721 if(min<0) return DC_PRED;
722 else return min;
723}
724
725static inline void write_back_non_zero_count(H264Context *h){
726 MpegEncContext * const s = &h->s;
7bc9090a 727 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265 728
6867a90b
LLL
729 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
730 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
731 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
53c05b1e 732 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
6867a90b
LLL
733 h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
734 h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
735 h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
115329f1 736
6867a90b 737 h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
53c05b1e 738 h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
6867a90b 739 h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
53c05b1e 740
6867a90b 741 h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
53c05b1e 742 h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
6867a90b 743 h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
5d18eaad
LM
744
745 if(FRAME_MBAFF){
746 // store all luma nnzs, for deblocking
747 int v = 0, i;
748 for(i=0; i<16; i++)
749 v += (!!h->non_zero_count_cache[scan8[i]]) << i;
750 *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
751 }
0da71265
MN
752}
753
754/**
755 * gets the predicted number of non zero coefficients.
756 * @param n block index
757 */
758static inline int pred_non_zero_count(H264Context *h, int n){
759 const int index8= scan8[n];
760 const int left= h->non_zero_count_cache[index8 - 1];
761 const int top = h->non_zero_count_cache[index8 - 8];
762 int i= left + top;
115329f1 763
0da71265
MN
764 if(i<64) i= (i+1)>>1;
765
a9c9a240 766 tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
0da71265
MN
767
768 return i&31;
769}
770
1924f3ce
MN
771static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
772 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
a9c9a240 773 MpegEncContext *s = &h->s;
1924f3ce 774
5d18eaad
LM
775 /* there is no consistent mapping of mvs to neighboring locations that will
776 * make mbaff happy, so we can't move all this logic to fill_caches */
777 if(FRAME_MBAFF){
191e8ca7 778 const uint32_t *mb_types = s->current_picture_ptr->mb_type;
5d18eaad
LM
779 const int16_t *mv;
780 *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
781 *C = h->mv_cache[list][scan8[0]-2];
782
783 if(!MB_FIELD
784 && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
785 int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
786 if(IS_INTERLACED(mb_types[topright_xy])){
787#define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
788 const int x4 = X4, y4 = Y4;\
789 const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
790 if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
791 return LIST_NOT_USED;\
792 mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
793 h->mv_cache[list][scan8[0]-2][0] = mv[0];\
794 h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
795 return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
796
797 SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
798 }
799 }
800 if(topright_ref == PART_NOT_AVAILABLE
801 && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
802 && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
803 if(!MB_FIELD
804 && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
805 SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
806 }
807 if(MB_FIELD
808 && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
809 && i >= scan8[0]+8){
810 // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
811 SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
812 }
813 }
814#undef SET_DIAG_MV
815 }
816
1924f3ce
MN
817 if(topright_ref != PART_NOT_AVAILABLE){
818 *C= h->mv_cache[list][ i - 8 + part_width ];
819 return topright_ref;
820 }else{
a9c9a240 821 tprintf(s->avctx, "topright MV not available\n");
95c26348 822
1924f3ce
MN
823 *C= h->mv_cache[list][ i - 8 - 1 ];
824 return h->ref_cache[list][ i - 8 - 1 ];
825 }
826}
827
0da71265
MN
828/**
829 * gets the predicted MV.
830 * @param n the block index
831 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
832 * @param mx the x component of the predicted motion vector
833 * @param my the y component of the predicted motion vector
834 */
835static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
0da71265
MN
836 const int index8= scan8[n];
837 const int top_ref= h->ref_cache[list][ index8 - 8 ];
0da71265
MN
838 const int left_ref= h->ref_cache[list][ index8 - 1 ];
839 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
840 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
1924f3ce
MN
841 const int16_t * C;
842 int diagonal_ref, match_count;
843
0da71265 844 assert(part_width==1 || part_width==2 || part_width==4);
1924f3ce 845
0da71265 846/* mv_cache
115329f1 847 B . . A T T T T
0da71265
MN
848 U . . L . . , .
849 U . . L . . . .
850 U . . L . . , .
851 . . . L . . . .
852*/
1924f3ce
MN
853
854 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
855 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
a9c9a240 856 tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
1924f3ce
MN
857 if(match_count > 1){ //most common
858 *mx= mid_pred(A[0], B[0], C[0]);
859 *my= mid_pred(A[1], B[1], C[1]);
860 }else if(match_count==1){
861 if(left_ref==ref){
862 *mx= A[0];
115329f1 863 *my= A[1];
1924f3ce
MN
864 }else if(top_ref==ref){
865 *mx= B[0];
115329f1 866 *my= B[1];
0da71265 867 }else{
1924f3ce 868 *mx= C[0];
115329f1 869 *my= C[1];
0da71265
MN
870 }
871 }else{
1924f3ce 872 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
0da71265 873 *mx= A[0];
115329f1 874 *my= A[1];
0da71265 875 }else{
1924f3ce
MN
876 *mx= mid_pred(A[0], B[0], C[0]);
877 *my= mid_pred(A[1], B[1], C[1]);
0da71265 878 }
0da71265 879 }
115329f1 880
a9c9a240 881 tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
0da71265
MN
882}
883
884/**
885 * gets the directionally predicted 16x8 MV.
886 * @param n the block index
887 * @param mx the x component of the predicted motion vector
888 * @param my the y component of the predicted motion vector
889 */
890static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
0da71265
MN
891 if(n==0){
892 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
893 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
894
a9c9a240 895 tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
115329f1 896
0da71265
MN
897 if(top_ref == ref){
898 *mx= B[0];
899 *my= B[1];
900 return;
901 }
902 }else{
903 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
904 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
115329f1 905
a9c9a240 906 tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
0da71265
MN
907
908 if(left_ref == ref){
909 *mx= A[0];
910 *my= A[1];
911 return;
912 }
913 }
914
915 //RARE
916 pred_motion(h, n, 4, list, ref, mx, my);
917}
918
919/**
920 * gets the directionally predicted 8x16 MV.
921 * @param n the block index
922 * @param mx the x component of the predicted motion vector
923 * @param my the y component of the predicted motion vector
924 */
925static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
0da71265
MN
926 if(n==0){
927 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
928 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
115329f1 929
a9c9a240 930 tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
0da71265
MN
931
932 if(left_ref == ref){
933 *mx= A[0];
934 *my= A[1];
935 return;
936 }
937 }else{
1924f3ce
MN
938 const int16_t * C;
939 int diagonal_ref;
940
941 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
115329f1 942
a9c9a240 943 tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
0da71265 944
115329f1 945 if(diagonal_ref == ref){
0da71265
MN
946 *mx= C[0];
947 *my= C[1];
948 return;
949 }
0da71265
MN
950 }
951
952 //RARE
953 pred_motion(h, n, 2, list, ref, mx, my);
954}
955
956static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
0da71265
MN
957 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
958 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
959
a9c9a240 960 tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
0da71265
MN
961
962 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
963 || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
964 || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
115329f1 965
0da71265
MN
966 *mx = *my = 0;
967 return;
968 }
115329f1 969
0da71265
MN
970 pred_motion(h, 0, 4, 0, 0, mx, my);
971
972 return;
973}
974
5ad984c9
LM
975static inline void direct_dist_scale_factor(H264Context * const h){
976 const int poc = h->s.current_picture_ptr->poc;
977 const int poc1 = h->ref_list[1][0].poc;
978 int i;
979 for(i=0; i<h->ref_count[0]; i++){
980 int poc0 = h->ref_list[0][i].poc;
f66e4f5f 981 int td = av_clip(poc1 - poc0, -128, 127);
5ad984c9
LM
982 if(td == 0 /* FIXME || pic0 is a long-term ref */){
983 h->dist_scale_factor[i] = 256;
984 }else{
f66e4f5f 985 int tb = av_clip(poc - poc0, -128, 127);
c26abfa5 986 int tx = (16384 + (FFABS(td) >> 1)) / td;
f66e4f5f 987 h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
5ad984c9
LM
988 }
989 }
5d18eaad
LM
990 if(FRAME_MBAFF){
991 for(i=0; i<h->ref_count[0]; i++){
992 h->dist_scale_factor_field[2*i] =
993 h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
994 }
995 }
5ad984c9 996}
2f944356
LM
997static inline void direct_ref_list_init(H264Context * const h){
998 MpegEncContext * const s = &h->s;
999 Picture * const ref1 = &h->ref_list[1][0];
1000 Picture * const cur = s->current_picture_ptr;
1001 int list, i, j;
1002 if(cur->pict_type == I_TYPE)
1003 cur->ref_count[0] = 0;
1004 if(cur->pict_type != B_TYPE)
1005 cur->ref_count[1] = 0;
1006 for(list=0; list<2; list++){
1007 cur->ref_count[list] = h->ref_count[list];
1008 for(j=0; j<h->ref_count[list]; j++)
1009 cur->ref_poc[list][j] = h->ref_list[list][j].poc;
1010 }
1011 if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
1012 return;
1013 for(list=0; list<2; list++){
1014 for(i=0; i<ref1->ref_count[list]; i++){
1015 const int poc = ref1->ref_poc[list][i];
171c4076 1016 h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
2f944356
LM
1017 for(j=0; j<h->ref_count[list]; j++)
1018 if(h->ref_list[list][j].poc == poc){
1019 h->map_col_to_list0[list][i] = j;
1020 break;
1021 }
1022 }
1023 }
5d18eaad
LM
1024 if(FRAME_MBAFF){
1025 for(list=0; list<2; list++){
1026 for(i=0; i<ref1->ref_count[list]; i++){
1027 j = h->map_col_to_list0[list][i];
1028 h->map_col_to_list0_field[list][2*i] = 2*j;
1029 h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
1030 }
1031 }
1032 }
2f944356 1033}
5ad984c9
LM
1034
1035static inline void pred_direct_motion(H264Context * const h, int *mb_type){
1036 MpegEncContext * const s = &h->s;
1037 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
1038 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1039 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1040 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
1041 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
4866bd2b 1042 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
5ad984c9 1043 const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
2f944356 1044 const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
5ad984c9 1045 const int is_b8x8 = IS_8X8(*mb_type);
88e7a4d1 1046 unsigned int sub_mb_type;
5ad984c9
LM
1047 int i8, i4;
1048
5d18eaad 1049#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
5ad984c9
LM
1050 if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
1051 /* FIXME save sub mb types from previous frames (or derive from MVs)
1052 * so we know exactly what block size to use */
1053 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
3622988f 1054 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
5d18eaad 1055 }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
5ad984c9
LM
1056 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1057 *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
1058 }else{
1059 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
3622988f 1060 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
5ad984c9
LM
1061 }
1062 if(!is_b8x8)
1063 *mb_type |= MB_TYPE_DIRECT2;
5d18eaad
LM
1064 if(MB_FIELD)
1065 *mb_type |= MB_TYPE_INTERLACED;
5ad984c9 1066
a9c9a240 1067 tprintf(s->avctx, "mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
115329f1 1068
5ad984c9
LM
1069 if(h->direct_spatial_mv_pred){
1070 int ref[2];
1071 int mv[2][2];
1072 int list;
1073
5d18eaad
LM
1074 /* FIXME interlacing + spatial direct uses wrong colocated block positions */
1075
5ad984c9
LM
1076 /* ref = min(neighbors) */
1077 for(list=0; list<2; list++){
1078 int refa = h->ref_cache[list][scan8[0] - 1];
1079 int refb = h->ref_cache[list][scan8[0] - 8];
1080 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
1081 if(refc == -2)
1082 refc = h->ref_cache[list][scan8[0] - 8 - 1];
1083 ref[list] = refa;
1084 if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
1085 ref[list] = refb;
1086 if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
1087 ref[list] = refc;
1088 if(ref[list] < 0)
1089 ref[list] = -1;
1090 }
1091
1092 if(ref[0] < 0 && ref[1] < 0){
1093 ref[0] = ref[1] = 0;
1094 mv[0][0] = mv[0][1] =
1095 mv[1][0] = mv[1][1] = 0;
1096 }else{
1097 for(list=0; list<2; list++){
1098 if(ref[list] >= 0)
1099 pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
1100 else
1101 mv[list][0] = mv[list][1] = 0;
1102 }
1103 }
1104
1105 if(ref[1] < 0){
1106 *mb_type &= ~MB_TYPE_P0L1;
1107 sub_mb_type &= ~MB_TYPE_P0L1;
1108 }else if(ref[0] < 0){
1109 *mb_type &= ~MB_TYPE_P0L0;
1110 sub_mb_type &= ~MB_TYPE_P0L0;
1111 }
1112
1113 if(IS_16X16(*mb_type)){
d19f5acb
MN
1114 int a=0, b=0;
1115
cec93959
LM
1116 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
1117 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
115329f1 1118 if(!IS_INTRA(mb_type_col)
c26abfa5
DB
1119 && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
1120 || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
bf4e3bd2 1121 && (h->x264_build>33 || !h->x264_build)))){
5ad984c9 1122 if(ref[0] > 0)
d19f5acb 1123 a= pack16to32(mv[0][0],mv[0][1]);
5ad984c9 1124 if(ref[1] > 0)
d19f5acb 1125 b= pack16to32(mv[1][0],mv[1][1]);
5ad984c9 1126 }else{
d19f5acb
MN
1127 a= pack16to32(mv[0][0],mv[0][1]);
1128 b= pack16to32(mv[1][0],mv[1][1]);
5ad984c9 1129 }
d19f5acb
MN
1130 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
1131 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
5ad984c9
LM
1132 }else{
1133 for(i8=0; i8<4; i8++){
1134 const int x8 = i8&1;
1135 const int y8 = i8>>1;
115329f1 1136
5ad984c9
LM
1137 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1138 continue;
1139 h->sub_mb_type[i8] = sub_mb_type;
115329f1 1140
5ad984c9
LM
1141 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1142 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
cec93959
LM
1143 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
1144 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
115329f1 1145
5ad984c9 1146 /* col_zero_flag */
115329f1
DB
1147 if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
1148 || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
bf4e3bd2 1149 && (h->x264_build>33 || !h->x264_build)))){
4866bd2b 1150 const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
f1f17e54
LM
1151 if(IS_SUB_8X8(sub_mb_type)){
1152 const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
c26abfa5 1153 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
f1f17e54
LM
1154 if(ref[0] == 0)
1155 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1156 if(ref[1] == 0)
1157 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1158 }
1159 }else
5ad984c9 1160 for(i4=0; i4<4; i4++){
4866bd2b 1161 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
c26abfa5 1162 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
5ad984c9
LM
1163 if(ref[0] == 0)
1164 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
1165 if(ref[1] == 0)
1166 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
1167 }
1168 }
1169 }
1170 }
1171 }
1172 }else{ /* direct temporal mv pred */
5d18eaad
LM
1173 const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
1174 const int *dist_scale_factor = h->dist_scale_factor;
1175
1176 if(FRAME_MBAFF){
1177 if(IS_INTERLACED(*mb_type)){
1178 map_col_to_list0[0] = h->map_col_to_list0_field[0];
1179 map_col_to_list0[1] = h->map_col_to_list0_field[1];
1180 dist_scale_factor = h->dist_scale_factor_field;
1181 }
1182 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
1183 /* FIXME assumes direct_8x8_inference == 1 */
1184 const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
1185 int mb_types_col[2];
1186 int y_shift;
1187
1188 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
1189 | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
1190 | (*mb_type & MB_TYPE_INTERLACED);
1191 sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
1192
1193 if(IS_INTERLACED(*mb_type)){
1194 /* frame to field scaling */
1195 mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
1196 mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
1197 if(s->mb_y&1){
1198 l1ref0 -= 2*h->b8_stride;
1199 l1ref1 -= 2*h->b8_stride;
1200 l1mv0 -= 4*h->b_stride;
1201 l1mv1 -= 4*h->b_stride;
1202 }
1203 y_shift = 0;
1204
1205 if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
1206 && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
1207 && !is_b8x8)
1208 *mb_type |= MB_TYPE_16x8;
1209 else
1210 *mb_type |= MB_TYPE_8x8;
1211 }else{
1212 /* field to frame scaling */
1213 /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
1214 * but in MBAFF, top and bottom POC are equal */
1215 int dy = (s->mb_y&1) ? 1 : 2;
1216 mb_types_col[0] =
1217 mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
1218 l1ref0 += dy*h->b8_stride;
1219 l1ref1 += dy*h->b8_stride;
1220 l1mv0 += 2*dy*h->b_stride;
1221 l1mv1 += 2*dy*h->b_stride;
1222 y_shift = 2;
1223
1224 if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
1225 && !is_b8x8)
1226 *mb_type |= MB_TYPE_16x16;
1227 else
1228 *mb_type |= MB_TYPE_8x8;
1229 }
1230
1231 for(i8=0; i8<4; i8++){
1232 const int x8 = i8&1;
1233 const int y8 = i8>>1;
1234 int ref0, scale;
1235 const int16_t (*l1mv)[2]= l1mv0;
1236
1237 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1238 continue;
1239 h->sub_mb_type[i8] = sub_mb_type;
1240
1241 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1242 if(IS_INTRA(mb_types_col[y8])){
1243 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
1244 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1245 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1246 continue;
1247 }
1248
1249 ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
1250 if(ref0 >= 0)
1251 ref0 = map_col_to_list0[0][ref0*2>>y_shift];
1252 else{
1253 ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
1254 l1mv= l1mv1;
1255 }
1256 scale = dist_scale_factor[ref0];
1257 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
1258
1259 {
1260 const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
1261 int my_col = (mv_col[1]<<y_shift)/2;
1262 int mx = (scale * mv_col[0] + 128) >> 8;
1263 int my = (scale * my_col + 128) >> 8;
1264 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
1265 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
1266 }
1267 }
1268 return;
1269 }
1270 }
1271
1272 /* one-to-one mv scaling */
1273
5ad984c9 1274 if(IS_16X16(*mb_type)){
fda51641
MN
1275 int ref, mv0, mv1;
1276
5ad984c9
LM
1277 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
1278 if(IS_INTRA(mb_type_col)){
fda51641 1279 ref=mv0=mv1=0;
5ad984c9 1280 }else{
5d18eaad
LM
1281 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
1282 : map_col_to_list0[1][l1ref1[0]];
1283 const int scale = dist_scale_factor[ref0];
8583bef8 1284 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
5ad984c9 1285 int mv_l0[2];
5d18eaad
LM
1286 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
1287 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
fda51641
MN
1288 ref= ref0;
1289 mv0= pack16to32(mv_l0[0],mv_l0[1]);
1290 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
5ad984c9 1291 }
fda51641
MN
1292 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
1293 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
1294 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
5ad984c9
LM
1295 }else{
1296 for(i8=0; i8<4; i8++){
1297 const int x8 = i8&1;
1298 const int y8 = i8>>1;
5d18eaad 1299 int ref0, scale;
bf4e3bd2 1300 const int16_t (*l1mv)[2]= l1mv0;
8583bef8 1301
5ad984c9
LM
1302 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1303 continue;
1304 h->sub_mb_type[i8] = sub_mb_type;
5d18eaad 1305 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
5ad984c9
LM
1306 if(IS_INTRA(mb_type_col)){
1307 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
5ad984c9
LM
1308 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1309 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1310 continue;
1311 }
115329f1 1312
5ad984c9 1313 ref0 = l1ref0[x8 + y8*h->b8_stride];
2f944356 1314 if(ref0 >= 0)
5d18eaad 1315 ref0 = map_col_to_list0[0][ref0];
8583bef8 1316 else{
5d18eaad 1317 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
8583bef8
MN
1318 l1mv= l1mv1;
1319 }
5d18eaad 1320 scale = dist_scale_factor[ref0];
115329f1 1321
5ad984c9 1322 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
f1f17e54
LM
1323 if(IS_SUB_8X8(sub_mb_type)){
1324 const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
5d18eaad
LM
1325 int mx = (scale * mv_col[0] + 128) >> 8;
1326 int my = (scale * mv_col[1] + 128) >> 8;
f1f17e54
LM
1327 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
1328 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
1329 }else
5ad984c9 1330 for(i4=0; i4<4; i4++){
8583bef8 1331 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
5ad984c9 1332 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
5d18eaad
LM
1333 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
1334 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
5ad984c9
LM
1335 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
1336 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
1337 }
1338 }
1339 }
1340 }
1341}
1342
0da71265
MN
1343static inline void write_back_motion(H264Context *h, int mb_type){
1344 MpegEncContext * const s = &h->s;
0da71265
MN
1345 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1346 const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1347 int list;
1348
2ea39252
LM
1349 if(!USES_LIST(mb_type, 0))
1350 fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
1351
3425501d 1352 for(list=0; list<h->list_count; list++){
0da71265 1353 int y;
53b19144 1354 if(!USES_LIST(mb_type, list))
5ad984c9 1355 continue;
115329f1 1356
0da71265
MN
1357 for(y=0; y<4; y++){
1358 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
1359 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
1360 }
9e528114 1361 if( h->pps.cabac ) {
e6e77eb6
LM
1362 if(IS_SKIP(mb_type))
1363 fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
1364 else
9e528114
LA
1365 for(y=0; y<4; y++){
1366 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
1367 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
1368 }
1369 }
53b19144
LM
1370
1371 {
191e8ca7 1372 int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
53b19144
LM
1373 ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
1374 ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
1375 ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
1376 ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
0da71265
MN
1377 }
1378 }
115329f1 1379
5ad984c9
LM
1380 if(h->slice_type == B_TYPE && h->pps.cabac){
1381 if(IS_8X8(mb_type)){
53b19144
LM
1382 uint8_t *direct_table = &h->direct_table[b8_xy];
1383 direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
1384 direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
1385 direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
5ad984c9
LM
1386 }
1387 }
0da71265
MN
1388}
1389
1390/**
1391 * Decodes a network abstraction layer unit.
1392 * @param consumed is the number of bytes used as input
1393 * @param length is the length of the array
3b66c4c5 1394 * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
115329f1 1395 * @returns decoded bytes, might be src+1 if no escapes
0da71265
MN
1396 */
1397static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
1398 int i, si, di;
1399 uint8_t *dst;
24456882 1400 int bufidx;
0da71265 1401
bb270c08 1402// src[0]&0x80; //forbidden bit
0da71265
MN
1403 h->nal_ref_idc= src[0]>>5;
1404 h->nal_unit_type= src[0]&0x1F;
1405
1406 src++; length--;
115329f1 1407#if 0
0da71265
MN
1408 for(i=0; i<length; i++)
1409 printf("%2X ", src[i]);
1410#endif
1411 for(i=0; i+1<length; i+=2){
1412 if(src[i]) continue;
1413 if(i>0 && src[i-1]==0) i--;
1414 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1415 if(src[i+2]!=3){
1416 /* startcode, so we must be past the end */
1417 length=i;
1418 }
1419 break;
1420 }
1421 }
1422
1423 if(i>=length-1){ //no escaped 0
1424 *dst_length= length;
1425 *consumed= length+1; //+1 for the header
115329f1 1426 return src;
0da71265
MN
1427 }
1428
24456882
1429 bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
1430 h->rbsp_buffer[bufidx]= av_fast_realloc(h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length);
1431 dst= h->rbsp_buffer[bufidx];
0da71265 1432
ac658be5
FOL
1433 if (dst == NULL){
1434 return NULL;
1435 }
1436
3b66c4c5 1437//printf("decoding esc\n");
0da71265 1438 si=di=0;
115329f1 1439 while(si<length){
0da71265
MN
1440 //remove escapes (very rare 1:2^22)
1441 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1442 if(src[si+2]==3){ //escape
1443 dst[di++]= 0;
1444 dst[di++]= 0;
1445 si+=3;
c8470cc1 1446 continue;
0da71265
MN
1447 }else //next start code
1448 break;
1449 }
1450
1451 dst[di++]= src[si++];
1452 }
1453
1454 *dst_length= di;
1455 *consumed= si + 1;//+1 for the header
90b5b51e 1456//FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
0da71265
MN
1457 return dst;
1458}
1459
0da71265
MN
1460/**
1461 * identifies the exact end of the bitstream
1462 * @return the length of the trailing, or 0 if damaged
1463 */
a9c9a240 1464static int decode_rbsp_trailing(H264Context *h, uint8_t *src){
0da71265
MN
1465 int v= *src;
1466 int r;
1467
a9c9a240 1468 tprintf(h->s.avctx, "rbsp trailing %X\n", v);
0da71265
MN
1469
1470 for(r=1; r<9; r++){
1471 if(v&1) return r;
1472 v>>=1;
1473 }
1474 return 0;
1475}
1476
1477/**
1478 * idct tranforms the 16 dc values and dequantize them.
1479 * @param qp quantization parameter
1480 */
239ea04c 1481static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
0da71265
MN
1482#define stride 16
1483 int i;
1484 int temp[16]; //FIXME check if this is a good idea
1485 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1486 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1487
1488//memset(block, 64, 2*256);
1489//return;
1490 for(i=0; i<4; i++){
1491 const int offset= y_offset[i];
1492 const int z0= block[offset+stride*0] + block[offset+stride*4];
1493 const int z1= block[offset+stride*0] - block[offset+stride*4];
1494 const int z2= block[offset+stride*1] - block[offset+stride*5];
1495 const int z3= block[offset+stride*1] + block[offset+stride*5];
1496
1497 temp[4*i+0]= z0+z3;
1498 temp[4*i+1]= z1+z2;
1499 temp[4*i+2]= z1-z2;
1500 temp[4*i+3]= z0-z3;
1501 }
1502
1503 for(i=0; i<4; i++){
1504 const int offset= x_offset[i];
1505 const int z0= temp[4*0+i] + temp[4*2+i];
1506 const int z1= temp[4*0+i] - temp[4*2+i];
1507 const int z2= temp[4*1+i] - temp[4*3+i];
1508 const int z3= temp[4*1+i] + temp[4*3+i];
1509
239ea04c
LM
1510 block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
1511 block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
1512 block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
1513 block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
0da71265
MN
1514 }
1515}
1516
e5017ab8 1517#if 0
0da71265
MN
1518/**
1519 * dct tranforms the 16 dc values.
1520 * @param qp quantization parameter ??? FIXME
1521 */
1522static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
1523// const int qmul= dequant_coeff[qp][0];
1524 int i;
1525 int temp[16]; //FIXME check if this is a good idea
1526 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1527 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1528
1529 for(i=0; i<4; i++){
1530 const int offset= y_offset[i];
1531 const int z0= block[offset+stride*0] + block[offset+stride*4];
1532 const int z1= block[offset+stride*0] - block[offset+stride*4];
1533 const int z2= block[offset+stride*1] - block[offset+stride*5];
1534 const int z3= block[offset+stride*1] + block[offset+stride*5];
1535
1536 temp[4*i+0]= z0+z3;
1537 temp[4*i+1]= z1+z2;
1538 temp[4*i+2]= z1-z2;
1539 temp[4*i+3]= z0-z3;
1540 }
1541
1542 for(i=0; i<4; i++){
1543 const int offset= x_offset[i];
1544 const int z0= temp[4*0+i] + temp[4*2+i];
1545 const int z1= temp[4*0+i] - temp[4*2+i];
1546 const int z2= temp[4*1+i] - temp[4*3+i];
1547 const int z3= temp[4*1+i] + temp[4*3+i];
1548
1549 block[stride*0 +offset]= (z0 + z3)>>1;
1550 block[stride*2 +offset]= (z1 + z2)>>1;
1551 block[stride*8 +offset]= (z1 - z2)>>1;
1552 block[stride*10+offset]= (z0 - z3)>>1;
1553 }
1554}
e5017ab8
LA
1555#endif
1556
0da71265
MN
1557#undef xStride
1558#undef stride
1559
239ea04c 1560static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
0da71265
MN
1561 const int stride= 16*2;
1562 const int xStride= 16;
1563 int a,b,c,d,e;
1564
1565 a= block[stride*0 + xStride*0];
1566 b= block[stride*0 + xStride*1];
1567 c= block[stride*1 + xStride*0];
1568 d= block[stride*1 + xStride*1];
1569
1570 e= a-b;
1571 a= a+b;
1572 b= c-d;
1573 c= c+d;
1574
239ea04c
LM
1575 block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
1576 block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
1577 block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
1578 block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
0da71265
MN
1579}
1580
e5017ab8 1581#if 0
0da71265
MN
1582static void chroma_dc_dct_c(DCTELEM *block){
1583 const int stride= 16*2;
1584 const int xStride= 16;
1585 int a,b,c,d,e;
1586
1587 a= block[stride*0 + xStride*0];
1588 b= block[stride*0 + xStride*1];
1589 c= block[stride*1 + xStride*0];
1590 d= block[stride*1 + xStride*1];
1591
1592 e= a-b;
1593 a= a+b;
1594 b= c-d;
1595 c= c+d;
1596
1597 block[stride*0 + xStride*0]= (a+c);
1598 block[stride*0 + xStride*1]= (e+b);
1599 block[stride*1 + xStride*0]= (a-c);
1600 block[stride*1 + xStride*1]= (e-b);
1601}
e5017ab8 1602#endif
0da71265
MN
1603
1604/**
1605 * gets the chroma qp.
1606 */
4691a77d
1607static inline int get_chroma_qp(H264Context *h, int t, int qscale){
1608 return h->pps.chroma_qp_table[t][qscale & 0xff];
0da71265
MN
1609}
1610
755bfeab 1611//FIXME need to check that this does not overflow signed 32 bit for low qp, i am not sure, it's very close
0afd2a92
DB
1612//FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
1613static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
0da71265
MN
1614 int i;
1615 const int * const quant_table= quant_coeff[qscale];
1616 const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
1617 const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
1618 const unsigned int threshold2= (threshold1<<1);
1619 int last_non_zero;
1620
0afd2a92 1621 if(separate_dc){
0da71265
MN
1622 if(qscale<=18){
1623 //avoid overflows
1624 const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
1625 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
1626 const unsigned int dc_threshold2= (dc_threshold1<<1);
1627
1628 int level= block[0]*quant_coeff[qscale+18][0];
1629 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1630 if(level>0){
1631 level= (dc_bias + level)>>(QUANT_SHIFT-2);
1632 block[0]= level;
1633 }else{
1634 level= (dc_bias - level)>>(QUANT_SHIFT-2);
1635 block[0]= -level;
1636 }
1637// last_non_zero = i;
1638 }else{
1639 block[0]=0;
1640 }
1641 }else{
1642 const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
1643 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
1644 const unsigned int dc_threshold2= (dc_threshold1<<1);
1645
1646 int level= block[0]*quant_table[0];
1647 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1648 if(level>0){
1649 level= (dc_bias + level)>>(QUANT_SHIFT+1);
1650 block[0]= level;
1651 }else{
1652 level= (dc_bias - level)>>(QUANT_SHIFT+1);
1653 block[0]= -level;
1654 }
1655// last_non_zero = i;
1656 }else{
1657 block[0]=0;
1658 }
1659 }
1660 last_non_zero= 0;
1661 i=1;
1662 }else{
1663 last_non_zero= -1;
1664 i=0;
1665 }
1666
1667 for(; i<16; i++){
1668 const int j= scantable[i];
1669 int level= block[j]*quant_table[j];
1670
1671// if( bias+level >= (1<<(QMAT_SHIFT - 3))
1672// || bias-level >= (1<<(QMAT_SHIFT - 3))){
1673 if(((unsigned)(level+threshold1))>threshold2){
1674 if(level>0){
1675 level= (bias + level)>>QUANT_SHIFT;
1676 block[j]= level;
1677 }else{
1678 level= (bias - level)>>QUANT_SHIFT;
1679 block[j]= -level;
1680 }
1681 last_non_zero = i;
1682 }else{
1683 block[j]=0;
1684 }
1685 }
1686
1687 return last_non_zero;
1688}
1689
0da71265
MN
1690static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
1691 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1692 int src_x_offset, int src_y_offset,
1693 qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
1694 MpegEncContext * const s = &h->s;
1695 const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
5d18eaad 1696 int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
0da71265 1697 const int luma_xy= (mx&3) + ((my&3)<<2);
5d18eaad
LM
1698 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
1699 uint8_t * src_cb, * src_cr;
1700 int extra_width= h->emu_edge_width;
1701 int extra_height= h->emu_edge_height;
0da71265
MN
1702 int emu=0;
1703 const int full_mx= mx>>2;
1704 const int full_my= my>>2;
fbd312fd 1705 const int pic_width = 16*s->mb_width;
bbb3edb8 1706 const int pic_height = 16*s->mb_height >> (MB_MBAFF || FIELD_PICTURE);
115329f1 1707
2f29af39 1708 if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
171c4076 1709 return;
115329f1 1710
0da71265
MN
1711 if(mx&7) extra_width -= 3;
1712 if(my&7) extra_height -= 3;
115329f1
DB
1713
1714 if( full_mx < 0-extra_width
1715 || full_my < 0-extra_height
1716 || full_mx + 16/*FIXME*/ > pic_width + extra_width
fbd312fd 1717 || full_my + 16/*FIXME*/ > pic_height + extra_height){
5d18eaad
LM
1718 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);
1719 src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
0da71265
MN
1720 emu=1;
1721 }
115329f1 1722
5d18eaad 1723 qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
0da71265 1724 if(!square){
5d18eaad 1725 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
0da71265 1726 }
115329f1 1727
87352549 1728 if(ENABLE_GRAY && s->flags&CODEC_FLAG_GRAY) return;
115329f1 1729
bbb3edb8 1730 if(MB_MBAFF || FIELD_PICTURE){
5d18eaad
LM
1731 // chroma offset when predicting from a field of opposite parity
1732 my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
1733 emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
1734 }
1735 src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
1736 src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
1737
0da71265 1738 if(emu){
5d18eaad 1739 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);
0da71265
MN
1740 src_cb= s->edge_emu_buffer;
1741 }
5d18eaad 1742 chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
0da71265
MN
1743
1744 if(emu){
5d18eaad 1745 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);
0da71265
MN
1746 src_cr= s->edge_emu_buffer;
1747 }
5d18eaad 1748 chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
0da71265
MN
1749}
1750
9f2d1b4f 1751static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
0da71265
MN
1752 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1753 int x_offset, int y_offset,
1754 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
1755 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
1756 int list0, int list1){
1757 MpegEncContext * const s = &h->s;
1758 qpel_mc_func *qpix_op= qpix_put;
1759 h264_chroma_mc_func chroma_op= chroma_put;
115329f1 1760
5d18eaad
LM
1761 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
1762 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
1763 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
0da71265 1764 x_offset += 8*s->mb_x;
bbb3edb8 1765 y_offset += 8*(s->mb_y >> (MB_MBAFF || FIELD_PICTURE));
115329f1 1766
0da71265 1767 if(list0){
1924f3ce 1768 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
0da71265
MN
1769 mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
1770 dest_y, dest_cb, dest_cr, x_offset, y_offset,
1771 qpix_op, chroma_op);
1772
1773 qpix_op= qpix_avg;
1774 chroma_op= chroma_avg;
1775 }
1776
1777 if(list1){
1924f3ce 1778 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
0da71265
MN
1779 mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
1780 dest_y, dest_cb, dest_cr, x_offset, y_offset,
1781 qpix_op, chroma_op);
1782 }
1783}
1784
9f2d1b4f
LM
1785static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
1786 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1787 int x_offset, int y_offset,
1788 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
1789 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
1790 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
1791 int list0, int list1){
1792 MpegEncContext * const s = &h->s;
1793
5d18eaad
LM
1794 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
1795 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
1796 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
9f2d1b4f 1797 x_offset += 8*s->mb_x;
bbb3edb8 1798 y_offset += 8*(s->mb_y >> (MB_MBAFF || FIELD_PICTURE));
115329f1 1799
9f2d1b4f
LM
1800 if(list0 && list1){
1801 /* don't optimize for luma-only case, since B-frames usually
1802 * use implicit weights => chroma too. */
1803 uint8_t *tmp_cb = s->obmc_scratchpad;
5d18eaad
LM
1804 uint8_t *tmp_cr = s->obmc_scratchpad + 8;
1805 uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
9f2d1b4f
LM
1806 int refn0 = h->ref_cache[0][ scan8[n] ];
1807 int refn1 = h->ref_cache[1][ scan8[n] ];
1808
1809 mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
1810 dest_y, dest_cb, dest_cr,
1811 x_offset, y_offset, qpix_put, chroma_put);
1812 mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
1813 tmp_y, tmp_cb, tmp_cr,
1814 x_offset, y_offset, qpix_put, chroma_put);
1815
1816 if(h->use_weight == 2){
1817 int weight0 = h->implicit_weight[refn0][refn1];
1818 int weight1 = 64 - weight0;
5d18eaad
LM
1819 luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
1820 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
1821 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
9f2d1b4f 1822 }else{
5d18eaad 1823 luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
115329f1 1824 h->luma_weight[0][refn0], h->luma_weight[1][refn1],
e8b56208 1825 h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
5d18eaad 1826 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
115329f1 1827 h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
e8b56208 1828 h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
5d18eaad 1829 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
115329f1 1830 h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
e8b56208 1831 h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
9f2d1b4f
LM
1832 }
1833 }else{
1834 int list = list1 ? 1 : 0;
1835 int refn = h->ref_cache[list][ scan8[n] ];
1836 Picture *ref= &h->ref_list[list][refn];
1837 mc_dir_part(h, ref, n, square, chroma_height, delta, list,
1838 dest_y, dest_cb, dest_cr, x_offset, y_offset,
1839 qpix_put, chroma_put);
1840
5d18eaad 1841 luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
9f2d1b4f
LM
1842 h->luma_weight[list][refn], h->luma_offset[list][refn]);
1843 if(h->use_weight_chroma){
5d18eaad 1844 chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
9f2d1b4f 1845 h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
5d18eaad 1846 chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
9f2d1b4f
LM
1847 h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
1848 }
1849 }
1850}
1851
1852static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
1853 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1854 int x_offset, int y_offset,
1855 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
1856 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
115329f1 1857 h264_weight_func *weight_op, h264_biweight_func *weight_avg,
9f2d1b4f
LM
1858 int list0, int list1){
1859 if((h->use_weight==2 && list0 && list1
1860 && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
1861 || h->use_weight==1)
1862 mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
1863 x_offset, y_offset, qpix_put, chroma_put,
1864 weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
1865 else
1866 mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
1867 x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
1868}
1869
513fbd8e
LM
1870static inline void prefetch_motion(H264Context *h, int list){
1871 /* fetch pixels for estimated mv 4 macroblocks ahead
1872 * optimized for 64byte cache lines */
1873 MpegEncContext * const s = &h->s;
1874 const int refn = h->ref_cache[list][scan8[0]];
1875 if(refn >= 0){
1876 const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
1877 const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
1878 uint8_t **src= h->ref_list[list][refn].data;
5d18eaad 1879 int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
513fbd8e
LM
1880 s->dsp.prefetch(src[0]+off, s->linesize, 4);
1881 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
1882 s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
1883 }
1884}
1885
0da71265
MN
1886static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1887 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
9f2d1b4f
LM
1888 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
1889 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
0da71265 1890 MpegEncContext * const s = &h->s;
7bc9090a 1891 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265 1892 const int mb_type= s->current_picture.mb_type[mb_xy];
115329f1 1893
0da71265 1894 assert(IS_INTER(mb_type));
115329f1 1895
513fbd8e
LM
1896 prefetch_motion(h, 0);
1897
0da71265
MN
1898 if(IS_16X16(mb_type)){
1899 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
1900 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
9f2d1b4f 1901 &weight_op[0], &weight_avg[0],
0da71265
MN
1902 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
1903 }else if(IS_16X8(mb_type)){
1904 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
1905 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
9f2d1b4f 1906 &weight_op[1], &weight_avg[1],
0da71265
MN
1907 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
1908 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
1909 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
9f2d1b4f 1910 &weight_op[1], &weight_avg[1],
0da71265
MN
1911 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
1912 }else if(IS_8X16(mb_type)){
5d18eaad 1913 mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
0da71265 1914 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
9f2d1b4f 1915 &weight_op[2], &weight_avg[2],
0da71265 1916 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
5d18eaad 1917 mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
0da71265 1918 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
9f2d1b4f 1919 &weight_op[2], &weight_avg[2],
0da71265
MN
1920 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
1921 }else{
1922 int i;
115329f1 1923
0da71265
MN
1924 assert(IS_8X8(mb_type));
1925
1926 for(i=0; i<4; i++){
1927 const int sub_mb_type= h->sub_mb_type[i];
1928 const int n= 4*i;
1929 int x_offset= (i&1)<<2;
1930 int y_offset= (i&2)<<1;
1931
1932 if(IS_SUB_8X8(sub_mb_type)){
1933 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
1934 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
9f2d1b4f 1935 &weight_op[3], &weight_avg[3],
0da71265
MN
1936 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1937 }else if(IS_SUB_8X4(sub_mb_type)){
1938 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
1939 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
9f2d1b4f 1940 &weight_op[4], &weight_avg[4],
0da71265
MN
1941 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1942 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
1943 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
9f2d1b4f 1944 &weight_op[4], &weight_avg[4],
0da71265
MN
1945 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1946 }else if(IS_SUB_4X8(sub_mb_type)){
5d18eaad 1947 mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
0da71265 1948 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
9f2d1b4f 1949 &weight_op[5], &weight_avg[5],
0da71265 1950 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
5d18eaad 1951 mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
0da71265 1952 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
9f2d1b4f 1953 &weight_op[5], &weight_avg[5],
0da71265
MN
1954 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1955 }else{
1956 int j;
1957 assert(IS_SUB_4X4(sub_mb_type));
1958 for(j=0; j<4; j++){
1959 int sub_x_offset= x_offset + 2*(j&1);
1960 int sub_y_offset= y_offset + (j&2);
1961 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
1962 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
9f2d1b4f 1963 &weight_op[6], &weight_avg[6],
0da71265
MN
1964 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
1965 }
1966 }
1967 }
1968 }
513fbd8e
LM
1969
1970 prefetch_motion(h, 1);
0da71265
MN
1971}
1972
2b100ab2 1973static void decode_init_vlc(void){
0da71265
MN
1974 static int done = 0;
1975
1976 if (!done) {
1977 int i;
1978 done = 1;
1979
115329f1 1980 init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
0da71265 1981 &chroma_dc_coeff_token_len [0], 1, 1,
073c2593 1982 &chroma_dc_coeff_token_bits[0], 1, 1, 1);
0da71265
MN
1983
1984 for(i=0; i<4; i++){
115329f1 1985 init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
0da71265 1986 &coeff_token_len [i][0], 1, 1,
073c2593 1987 &coeff_token_bits[i][0], 1, 1, 1);
0da71265
MN
1988 }
1989
1990 for(i=0; i<3; i++){
1991 init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
1992 &chroma_dc_total_zeros_len [i][0], 1, 1,
073c2593 1993 &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
0da71265
MN
1994 }
1995 for(i=0; i<15; i++){
115329f1 1996 init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
0da71265 1997 &total_zeros_len [i][0], 1, 1,
073c2593 1998 &total_zeros_bits[i][0], 1, 1, 1);
0da71265
MN
1999 }
2000
2001 for(i=0; i<6; i++){
115329f1 2002 init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
0da71265 2003 &run_len [i][0], 1, 1,
073c2593 2004 &run_bits[i][0], 1, 1, 1);
0da71265 2005 }
115329f1 2006 init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
0da71265 2007 &run_len [6][0], 1, 1,
073c2593 2008 &run_bits[6][0], 1, 1, 1);
0da71265
MN
2009 }
2010}
2011
0da71265 2012static void free_tables(H264Context *h){
7978debd 2013 int i;
afebe2f7 2014 H264Context *hx;
0da71265 2015 av_freep(&h->intra4x4_pred_mode);
e5017ab8
LA
2016 av_freep(&h->chroma_pred_mode_table);
2017 av_freep(&h->cbp_table);
9e528114
LA
2018 av_freep(&h->mvd_table[0]);
2019 av_freep(&h->mvd_table[1]);
5ad984c9 2020 av_freep(&h->direct_table);
0da71265
MN
2021 av_freep(&h->non_zero_count);
2022 av_freep(&h->slice_table_base);
2023 h->slice_table= NULL;
e5017ab8 2024
0da71265
MN
2025 av_freep(&h->mb2b_xy);
2026 av_freep(&h->mb2b8_xy);
9f2d1b4f 2027
7978debd
2028 for(i = 0; i < MAX_SPS_COUNT; i++)
2029 av_freep(h->sps_buffers + i);
2030
2031 for(i = 0; i < MAX_PPS_COUNT; i++)
2032 av_freep(h->pps_buffers + i);
afebe2f7
2033
2034 for(i = 0; i < h->s.avctx->thread_count; i++) {
2035 hx = h->thread_context[i];
2036 if(!hx) continue;
2037 av_freep(&hx->top_borders[1]);
2038 av_freep(&hx->top_borders[0]);
2039 av_freep(&hx->s.obmc_scratchpad);
2040 av_freep(&hx->s.allocated_edge_emu_buffer);
2041 }
0da71265
MN
2042}
2043
239ea04c
LM
2044static void init_dequant8_coeff_table(H264Context *h){
2045 int i,q,x;
548a1c8a 2046 const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
239ea04c
LM
2047 h->dequant8_coeff[0] = h->dequant8_buffer[0];
2048 h->dequant8_coeff[1] = h->dequant8_buffer[1];
2049
2050 for(i=0; i<2; i++ ){
2051 if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
2052 h->dequant8_coeff[1] = h->dequant8_buffer[0];
2053 break;
2054 }
2055
2056 for(q=0; q<52; q++){
acd8d10f
PI
2057 int shift = ff_div6[q];
2058 int idx = ff_rem6[q];
239ea04c 2059 for(x=0; x<64; x++)
548a1c8a
LM
2060 h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
2061 ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
2062 h->pps.scaling_matrix8[i][x]) << shift;
239ea04c
LM
2063 }
2064 }
2065}
2066
2067static void init_dequant4_coeff_table(H264Context *h){
2068 int i,j,q,x;
ab2e3e2c 2069 const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
239ea04c
LM
2070 for(i=0; i<6; i++ ){
2071 h->dequant4_coeff[i] = h->dequant4_buffer[i];
2072 for(j=0; j<i; j++){
2073 if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
2074 h->dequant4_coeff[i] = h->dequant4_buffer[j];
2075 break;
2076 }
2077 }
2078 if(j<i)
2079 continue;
2080
2081 for(q=0; q<52; q++){
acd8d10f
PI
2082 int shift = ff_div6[q] + 2;
2083 int idx = ff_rem6[q];
239ea04c 2084 for(x=0; x<16; x++)
ab2e3e2c
LM
2085 h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
2086 ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
239ea04c
LM
2087 h->pps.scaling_matrix4[i][x]) << shift;
2088 }
2089 }
2090}
2091
2092static void init_dequant_tables(H264Context *h){
2093 int i,x;
2094 init_dequant4_coeff_table(h);
2095 if(h->pps.transform_8x8_mode)
2096 init_dequant8_coeff_table(h);
2097 if(h->sps.transform_bypass){
2098 for(i=0; i<6; i++)
2099 for(x=0; x<16; x++)
2100 h->dequant4_coeff[i][0][x] = 1<<6;
2101 if(h->pps.transform_8x8_mode)
2102 for(i=0; i<2; i++)
2103 for(x=0; x<64; x++)
2104 h->dequant8_coeff[i][0][x] = 1<<6;
2105 }
2106}
2107
2108
0da71265
MN
2109/**
2110 * allocates tables.
3b66c4c5 2111 * needs width/height
0da71265
MN
2112 */
2113static int alloc_tables(H264Context *h){
2114 MpegEncContext * const s = &h->s;
7bc9090a 2115 const int big_mb_num= s->mb_stride * (s->mb_height+1);
239ea04c 2116 int x,y;
0da71265
MN
2117
2118 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
e5017ab8 2119
53c05b1e 2120 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
5d18eaad 2121 CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
5d0e4cb8 2122 CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
0da71265 2123
e5017ab8
LA
2124 if( h->pps.cabac ) {
2125 CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
9e528114
LA
2126 CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
2127 CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
5ad984c9 2128 CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
e5017ab8
LA
2129 }
2130
5d18eaad
LM
2131 memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
2132 h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
0da71265 2133
a55f20bd
LM
2134 CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
2135 CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
0da71265
MN
2136 for(y=0; y<s->mb_height; y++){
2137 for(x=0; x<s->mb_width; x++){
7bc9090a 2138 const int mb_xy= x + y*s->mb_stride;
0da71265
MN
2139 const int b_xy = 4*x + 4*y*h->b_stride;
2140 const int b8_xy= 2*x + 2*y*h->b8_stride;
115329f1 2141
0da71265
MN
2142 h->mb2b_xy [mb_xy]= b_xy;
2143 h->mb2b8_xy[mb_xy]= b8_xy;
2144 }
2145 }
9f2d1b4f 2146
9c6221ae
GV
2147 s->obmc_scratchpad = NULL;
2148
56edbd81
LM
2149 if(!h->dequant4_coeff[0])
2150 init_dequant_tables(h);
2151
0da71265
MN
2152 return 0;
2153fail:
2154 free_tables(h);
2155 return -1;
2156}
2157
afebe2f7
2158/**
2159 * Mimic alloc_tables(), but for every context thread.
2160 */
2161static void clone_tables(H264Context *dst, H264Context *src){
2162 dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
2163 dst->non_zero_count = src->non_zero_count;
2164 dst->slice_table = src->slice_table;
2165 dst->cbp_table = src->cbp_table;
2166 dst->mb2b_xy = src->mb2b_xy;
2167 dst->mb2b8_xy = src->mb2b8_xy;
2168 dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
2169 dst->mvd_table[0] = src->mvd_table[0];
2170 dst->mvd_table[1] = src->mvd_table[1];
2171 dst->direct_table = src->direct_table;
2172
afebe2f7
2173 dst->s.obmc_scratchpad = NULL;
2174 ff_h264_pred_init(&dst->hpc, src->s.codec_id);
afebe2f7
2175}
2176
2177/**
2178 * Init context
2179 * Allocate buffers which are not shared amongst multiple threads.
2180 */
2181static int context_init(H264Context *h){
2182 MpegEncContext * const s = &h->s;
2183
2184 CHECKED_ALLOCZ(h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
2185 CHECKED_ALLOCZ(h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
2186
2187 // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
2188 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer,
2189 (s->width+64)*2*21*2); //(width + edge + align)*interlaced*MBsize*tolerance
2190 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
2191 return 0;
2192fail:
2193 return -1; // free_tables will clean up for us
2194}
2195
0da71265
MN
2196static void common_init(H264Context *h){
2197 MpegEncContext * const s = &h->s;
0da71265
MN
2198
2199 s->width = s->avctx->width;
2200 s->height = s->avctx->height;
2201 s->codec_id= s->avctx->codec->id;
115329f1 2202
c92a30bb 2203 ff_h264_pred_init(&h->hpc, s->codec_id);
0da71265 2204
239ea04c 2205 h->dequant_coeff_pps= -1;
9a41c2c7 2206 s->unrestricted_mv=1;
0da71265 2207 s->decode=1; //FIXME
56edbd81
LM
2208
2209 memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
2210 memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
0da71265
MN
2211}
2212
2213static int decode_init(AVCodecContext *avctx){
2214 H264Context *h= avctx->priv_data;
2215 MpegEncContext * const s = &h->s;
2216
3edcacde 2217 MPV_decode_defaults(s);
115329f1 2218
0da71265
MN
2219 s->avctx = avctx;
2220 common_init(h);
2221
2222 s->out_format = FMT_H264;
2223 s->workaround_bugs= avctx->workaround_bugs;
2224
2225 // set defaults
0da71265 2226// s->decode_mb= ff_h263_decode_mb;
9a5a05d0 2227 s->quarter_sample = 1;
0da71265
MN
2228 s->low_delay= 1;
2229 avctx->pix_fmt= PIX_FMT_YUV420P;
2230
c2212338 2231 decode_init_vlc();
115329f1 2232
26165f99
MR
2233 if(avctx->extradata_size > 0 && avctx->extradata &&
2234 *(char *)avctx->extradata == 1){
4770b1b4
RT
2235 h->is_avc = 1;
2236 h->got_avcC = 0;
26165f99
MR
2237 } else {
2238 h->is_avc = 0;
4770b1b4
RT
2239 }
2240
afebe2f7 2241 h->thread_context[0] = h;
0da71265
MN
2242 return 0;
2243}
2244
af8aa846 2245static int frame_start(H264Context *h){
0da71265
MN
2246 MpegEncContext * const s = &h->s;
2247 int i;
2248
af8aa846
MN
2249 if(MPV_frame_start(s, s->avctx) < 0)
2250 return -1;
0da71265 2251 ff_er_frame_start(s);
3a22d7fa
JD
2252 /*
2253 * MPV_frame_start uses pict_type to derive key_frame.
2254 * This is incorrect for H.264; IDR markings must be used.
2255 * Zero here; IDR markings per slice in frame or fields are OR'd in later.
2256 * See decode_nal_units().
2257 */
2258 s->current_picture_ptr->key_frame= 0;
0da71265
MN
2259
2260 assert(s->linesize && s->uvlinesize);
2261
2262 for(i=0; i<16; i++){
2263 h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
6867a90b 2264 h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
0da71265
MN
2265 }
2266 for(i=0; i<4; i++){
2267 h->block_offset[16+i]=
2268 h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
6867a90b
LLL
2269 h->block_offset[24+16+i]=
2270 h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
0da71265
MN
2271 }
2272
934b0821
LM
2273 /* can't be in alloc_tables because linesize isn't known there.
2274 * FIXME: redo bipred weight to not require extra buffer? */
afebe2f7
2275 for(i = 0; i < s->avctx->thread_count; i++)
2276 if(!h->thread_context[i]->s.obmc_scratchpad)
2277 h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
5d18eaad
LM
2278
2279 /* some macroblocks will be accessed before they're available */
afebe2f7 2280 if(FRAME_MBAFF || s->avctx->thread_count > 1)
5d18eaad 2281 memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
934b0821 2282
0da71265 2283// s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
af8aa846 2284 return 0;
0da71265
MN
2285}
2286
93cc10fa 2287static 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){
53c05b1e
MN
2288 MpegEncContext * const s = &h->s;
2289 int i;
115329f1 2290
53c05b1e
MN
2291 src_y -= linesize;
2292 src_cb -= uvlinesize;
2293 src_cr -= uvlinesize;
2294
3b66c4c5 2295 // There are two lines saved, the line above the the top macroblock of a pair,
6867a90b 2296 // and the line above the bottom macroblock
6ba71fc4 2297 h->left_border[0]= h->top_borders[0][s->mb_x][15];
53c05b1e
MN
2298 for(i=1; i<17; i++){
2299 h->left_border[i]= src_y[15+i* linesize];
2300 }
115329f1 2301
6ba71fc4
LLL
2302 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
2303 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
53c05b1e 2304
87352549 2305 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
6ba71fc4
LLL
2306 h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
2307 h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
53c05b1e
MN
2308 for(i=1; i<9; i++){
2309 h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
2310 h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
2311 }
6ba71fc4
LLL
2312 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
2313 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
53c05b1e
MN
2314 }
2315}
2316
93cc10fa 2317static 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){
53c05b1e
MN
2318 MpegEncContext * const s = &h->s;
2319 int temp8, i;
2320 uint64_t temp64;
b69378e2
2321 int deblock_left;
2322 int deblock_top;
2323 int mb_xy;
2324
2325 if(h->deblocking_filter == 2) {
2326 mb_xy = s->mb_x + s->mb_y*s->mb_stride;
2327 deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
2328 deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
2329 } else {
2330 deblock_left = (s->mb_x > 0);
2331 deblock_top = (s->mb_y > 0);
2332 }
53c05b1e
MN
2333
2334 src_y -= linesize + 1;
2335 src_cb -= uvlinesize + 1;
2336 src_cr -= uvlinesize + 1;
2337
2338#define XCHG(a,b,t,xchg)\
2339t= a;\
2340if(xchg)\
2341 a= b;\
2342b= t;
d89dc06a
LM
2343
2344 if(deblock_left){
2345 for(i = !deblock_top; i<17; i++){
2346 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
2347 }
2348 }
2349
2350 if(deblock_top){
6ba71fc4
LLL
2351 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
2352 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
cad4368a 2353 if(s->mb_x+1 < s->mb_width){
43efd19a
LM
2354 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
2355 }
53c05b1e 2356 }
53c05b1e 2357
87352549 2358 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
d89dc06a
LM
2359 if(deblock_left){
2360 for(i = !deblock_top; i<9; i++){
2361 XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
2362 XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
2363 }
2364 }
2365 if(deblock_top){
6ba71fc4
LLL
2366 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
2367 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
2368 }
2369 }
2370}
2371
2372static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
2373 MpegEncContext * const s = &h->s;
2374 int i;
115329f1 2375
6ba71fc4
LLL
2376 src_y -= 2 * linesize;
2377 src_cb -= 2 * uvlinesize;
2378 src_cr -= 2 * uvlinesize;
2379
3b66c4c5 2380 // There are two lines saved, the line above the the top macroblock of a pair,
6ba71fc4
LLL
2381 // and the line above the bottom macroblock
2382 h->left_border[0]= h->top_borders[0][s->mb_x][15];
2383 h->left_border[1]= h->top_borders[1][s->mb_x][15];
2384 for(i=2; i<34; i++){
2385 h->left_border[i]= src_y[15+i* linesize];
2386 }
115329f1 2387
6ba71fc4
LLL
2388 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
2389 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
2390 *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
2391 *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
2392
87352549 2393 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
6ba71fc4
LLL
2394 h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
2395 h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
2396 h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
2397 h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
2398 for(i=2; i<18; i++){
2399 h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
2400 h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
2401 }
2402 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
2403 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
2404 *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
2405 *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
2406 }
2407}
2408
2409static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
2410 MpegEncContext * const s = &h->s;
2411 int temp8, i;
2412 uint64_t temp64;
2413 int deblock_left = (s->mb_x > 0);
5d18eaad 2414 int deblock_top = (s->mb_y > 1);
6ba71fc4 2415
a9c9a240 2416 tprintf(s->avctx, "xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
6ba71fc4
LLL
2417
2418 src_y -= 2 * linesize + 1;
2419 src_cb -= 2 * uvlinesize + 1;
2420 src_cr -= 2 * uvlinesize + 1;
2421
2422#define XCHG(a,b,t,xchg)\
2423t= a;\
2424if(xchg)\
2425 a= b;\
2426b= t;
2427
2428 if(deblock_left){
2429 for(i = (!deblock_top)<<1; i<34; i++){
2430 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
2431 }
2432 }
2433
2434 if(deblock_top){
2435 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
2436 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
2437 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
2438 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
5d18eaad
LM
2439 if(s->mb_x+1 < s->mb_width){
2440 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
2441 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
2442 }
6ba71fc4
LLL
2443 }
2444
87352549 2445 if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
6ba71fc4
LLL
2446 if(deblock_left){
2447 for(i = (!deblock_top) << 1; i<18; i++){
2448 XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
2449 XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
2450 }
2451 }
2452 if(deblock_top){
2453 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
2454 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
2455 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
2456 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
53c05b1e 2457 }
53c05b1e
MN
2458 }
2459}
2460
5a6a6cc7 2461static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
0da71265
MN
2462 MpegEncContext * const s = &h->s;
2463 const int mb_x= s->mb_x;
2464 const int mb_y= s->mb_y;
7bc9090a 2465 const int mb_xy= mb_x + mb_y*s->mb_stride;
0da71265
MN
2466 const int mb_type= s->current_picture.mb_type[mb_xy];
2467 uint8_t *dest_y, *dest_cb, *dest_cr;
2468 int linesize, uvlinesize /*dct_offset*/;
2469 int i;
6867a90b 2470 int *block_offset = &h->block_offset[0];
6ba71fc4 2471 const unsigned int bottom = mb_y & 1;
bd91fee3 2472 const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
36940eca 2473 void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
ef9d1d15 2474 void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
0da71265 2475
0da71265
MN
2476 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
2477 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2478 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2479
a957c27b
LM
2480 s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
2481 s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
2482
bd91fee3 2483 if (!simple && MB_FIELD) {
5d18eaad
LM
2484 linesize = h->mb_linesize = s->linesize * 2;
2485 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
6867a90b 2486 block_offset = &h->block_offset[24];
0da71265
MN
2487 if(mb_y&1){ //FIXME move out of this func?
2488 dest_y -= s->linesize*15;
6867a90b
LLL
2489 dest_cb-= s->uvlinesize*7;
2490 dest_cr-= s->uvlinesize*7;
0da71265 2491 }
5d18eaad
LM
2492 if(FRAME_MBAFF) {
2493 int list;
3425501d 2494 for(list=0; list<h->list_count; list++){
5d18eaad
LM
2495 if(!USES_LIST(mb_type, list))
2496 continue;
2497 if(IS_16X16(mb_type)){
2498 int8_t *ref = &h->ref_cache[list][scan8[0]];
2499 fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
2500 }else{
2501 for(i=0; i<16; i+=4){
2502 //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
2503 int ref = h->ref_cache[list][scan8[i]];
2504 if(ref >= 0)
2505 fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
2506 }
2507 }
2508 }
2509 }
0da71265 2510 } else {
5d18eaad
LM
2511 linesize = h->mb_linesize = s->linesize;
2512 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
0da71265
MN
2513// dct_offset = s->linesize * 16;
2514 }
115329f1 2515
ef9d1d15
LM
2516 if(transform_bypass){
2517 idct_dc_add =
2518 idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
2519 }else if(IS_8x8DCT(mb_type)){
2520 idct_dc_add = s->dsp.h264_idct8_dc_add;
2521 idct_add = s->dsp.h264_idct8_add;
2522 }else{
2523 idct_dc_add = s->dsp.h264_idct_dc_add;
2524 idct_add = s->dsp.h264_idct_add;
2525 }
0da71265 2526
bd91fee3 2527 if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
5d18eaad
LM
2528 && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
2529 int mbt_y = mb_y&~1;
2530 uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
2531 uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
2532 uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
2533 xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
2534 }
2535
bd91fee3 2536 if (!simple && IS_INTRA_PCM(mb_type)) {
6fbcaaa0
LLL
2537 unsigned int x, y;
2538
2539 // The pixels are stored in h->mb array in the same order as levels,
2540 // copy them in output in the correct order.
2541 for(i=0; i<16; i++) {
2542 for (y=0; y<4; y++) {
2543 for (x=0; x<4; x++) {
6867a90b 2544 *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
6fbcaaa0
LLL
2545 }
2546 }
2547 }
2548 for(i=16; i<16+4; i++) {
2549 for (y=0; y<4; y++) {
2550 for (x=0; x<4; x++) {
6867a90b 2551 *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
6fbcaaa0
LLL
2552 }
2553 }
2554 }
2555 for(i=20; i<20+4; i++) {
2556 for (y=0; y<4; y++) {
2557 for (x=0; x<4; x++) {
6867a90b 2558 *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
6fbcaaa0
LLL
2559 }
2560 }
2561 }
e7e09b49
LLL
2562 } else {
2563 if(IS_INTRA(mb_type)){
bd91fee3 2564 if(h->deblocking_filter && (simple || !FRAME_MBAFF))
93cc10fa 2565 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
53c05b1e 2566
87352549 2567 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
c92a30bb
KS
2568 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
2569 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
e7e09b49 2570 }
0da71265 2571
e7e09b49 2572 if(IS_INTRA4x4(mb_type)){
bd91fee3 2573 if(simple || !s->encoding){
43efd19a
LM
2574 if(IS_8x8DCT(mb_type)){
2575 for(i=0; i<16; i+=4){
2576 uint8_t * const ptr= dest_y + block_offset[i];
2577 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
ef9d1d15 2578 const int nnz = h->non_zero_count_cache[ scan8[i] ];
c92a30bb 2579 h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
4672503d 2580 (h->topright_samples_available<<i)&0x4000, linesize);
ef9d1d15
LM
2581 if(nnz){
2582 if(nnz == 1 && h->mb[i*16])
2583 idct_dc_add(ptr, h->mb + i*16, linesize);
2584 else
2585 idct_add(ptr, h->mb + i*16, linesize);
2586 }
43efd19a
LM
2587 }
2588 }else
e7e09b49 2589 for(i=0; i<16; i++){
6867a90b 2590 uint8_t * const ptr= dest_y + block_offset[i];
e7e09b49
LLL
2591 uint8_t *topright;
2592 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
ef9d1d15 2593 int nnz, tr;
e7e09b49
LLL
2594
2595 if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
2596 const int topright_avail= (h->topright_samples_available<<i)&0x8000;
6867a90b 2597 assert(mb_y || linesize <= block_offset[i]);
e7e09b49
LLL
2598 if(!topright_avail){
2599 tr= ptr[3 - linesize]*0x01010101;
2600 topright= (uint8_t*) &tr;
115329f1 2601 }else
e7e09b49 2602 topright= ptr + 4 - linesize;
a9799653 2603 }else
e7e09b49
LLL
2604 topright= NULL;
2605
c92a30bb 2606 h->hpc.pred4x4[ dir ](ptr, topright, linesize);
ef9d1d15
LM
2607 nnz = h->non_zero_count_cache[ scan8[i] ];
2608 if(nnz){
bd91fee3 2609 if(is_h264){
ef9d1d15
LM
2610 if(nnz == 1 && h->mb[i*16])
2611 idct_dc_add(ptr, h->mb + i*16, linesize);
2612 else
2613 idct_add(ptr, h->mb + i*16, linesize);
2614 }else
e7e09b49
LLL
2615 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
2616 }
8b82a956 2617 }
0da71265 2618 }
e7e09b49 2619 }else{
c92a30bb 2620 h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
bd91fee3 2621 if(is_h264){
36940eca 2622 if(!transform_bypass)
93f0c0a4 2623 h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
36940eca 2624 }else
e7e09b49 2625 svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
0da71265 2626 }
bd91fee3 2627 if(h->deblocking_filter && (simple || !FRAME_MBAFF))
93cc10fa 2628 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
bd91fee3 2629 }else if(is_h264){
e7e09b49 2630 hl_motion(h, dest_y, dest_cb, dest_cr,
2833fc46
LM
2631 s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
2632 s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
e7e09b49 2633 s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
0da71265 2634 }
e7e09b49
LLL
2635
2636
2637 if(!IS_INTRA4x4(mb_type)){
bd91fee3 2638 if(is_h264){
ef9d1d15
LM
2639 if(IS_INTRA16x16(mb_type)){
2640 for(i=0; i<16; i++){
2641 if(h->non_zero_count_cache[ scan8[i] ])
2642 idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
2643 else if(h->mb[i*16])
2644 idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
2645 }
2646 }else{
2647 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
2648 for(i=0; i<16; i+=di){
2649 int nnz = h->non_zero_count_cache[ scan8[i] ];
2650 if(nnz){
2651 if(nnz==1 && h->mb[i*16])
2652 idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
2653 else
2654 idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
2655 }
e7e09b49 2656 }
4704097a 2657 }
e7e09b49
LLL
2658 }else{
2659 for(i=0; i<16; i++){
2660 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
6867a90b 2661 uint8_t * const ptr= dest_y + block_offset[i];
e7e09b49
LLL
2662 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
2663 }
4704097a 2664 }
0da71265
MN
2665 }
2666 }
0da71265 2667
87352549 2668 if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
ef9d1d15
LM
2669 uint8_t *dest[2] = {dest_cb, dest_cr};
2670 if(transform_bypass){
2671 idct_add = idct_dc_add = s->dsp.add_pixels4;
2672 }else{
2673 idct_add = s->dsp.h264_idct_add;
2674 idct_dc_add = s->dsp.h264_idct_dc_add;
4691a77d
2675 chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
2676 chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
36940eca 2677 }
bd91fee3 2678 if(is_h264){
ef9d1d15
LM
2679 for(i=16; i<16+8; i++){
2680 if(h->non_zero_count_cache[ scan8[i] ])
2681 idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
2682 else if(h->mb[i*16])
2683 idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
4704097a 2684 }
e7e09b49 2685 }else{
ef9d1d15 2686 for(i=16; i<16+8; i++){
e7e09b49 2687 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
ef9d1d15 2688 uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
e7e09b49
LLL
2689 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
2690 }
4704097a 2691 }
0da71265
MN
2692 }
2693 }
2694 }
53c05b1e 2695 if(h->deblocking_filter) {
bd91fee3 2696 if (!simple && FRAME_MBAFF) {
5d18eaad
LM
2697 //FIXME try deblocking one mb at a time?
2698 // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
6ba71fc4
LLL
2699 const int mb_y = s->mb_y - 1;
2700 uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
2701 const int mb_xy= mb_x + mb_y*s->mb_stride;
2702 const int mb_type_top = s->current_picture.mb_type[mb_xy];
2703 const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
6ba71fc4
LLL
2704 if (!bottom) return;
2705 pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
2706 pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2707 pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2708
5d18eaad
LM
2709 if(IS_INTRA(mb_type_top | mb_type_bottom))
2710 xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
2711
6ba71fc4 2712 backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
5d18eaad 2713 // deblock a pair
115329f1 2714 // top
6ba71fc4 2715 s->mb_y--;
a9c9a240 2716 tprintf(h->s.avctx, "call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
3b66c4c5 2717 fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
4691a77d
2718 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
2719 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
6ba71fc4 2720 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
6ba71fc4
LLL
2721 // bottom
2722 s->mb_y++;
a9c9a240 2723 tprintf(h->s.avctx, "call mbaff filter_mb\n");
3b66c4c5 2724 fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
4691a77d
2725 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
2726 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
6ba71fc4 2727 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
6ba71fc4 2728 } else {
a9c9a240 2729 tprintf(h->s.avctx, "call filter_mb\n");
93cc10fa 2730 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
3b66c4c5 2731 fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
3e20143e 2732 filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
6ba71fc4 2733 }
53c05b1e 2734 }
0da71265
MN
2735}
2736
0da71265 2737/**
bd91fee3
AS
2738 * Process a macroblock; this case avoids checks for expensive uncommon cases.
2739 */
2740static void hl_decode_mb_simple(H264Context *h){
2741 hl_decode_mb_internal(h, 1);
2742}
2743
2744/**
2745 * Process a macroblock; this handles edge cases, such as interlacing.
2746 */
2747static void av_noinline hl_decode_mb_complex(H264Context *h){
2748 hl_decode_mb_internal(h, 0);
2749}
2750
2751static void hl_decode_mb(H264Context *h){
2752 MpegEncContext * const s = &h->s;
2753 const int mb_x= s->mb_x;
2754 const int mb_y= s->mb_y;
2755 const int mb_xy= mb_x + mb_y*s->mb_stride;
2756 const int mb_type= s->current_picture.mb_type[mb_xy];
87352549 2757 int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (ENABLE_GRAY && (s->flags&CODEC_FLAG_GRAY)) || s->encoding;
bd91fee3
AS
2758
2759 if(!s->decode)
2760 return;
2761
2762 if (is_complex)
2763 hl_decode_mb_complex(h);
2764 else hl_decode_mb_simple(h);
2765}
2766
2767/**
0da71265
MN
2768 * fills the default_ref_list.
2769 */
2770static int fill_default_ref_list(H264Context *h){
2771 MpegEncContext * const s = &h->s;
2772 int i;
827c91bf 2773 int smallest_poc_greater_than_current = -1;
17107065 2774 Picture sorted_short_ref[32];
115329f1 2775
0da71265
MN
2776 if(h->slice_type==B_TYPE){
2777 int out_i;
29860cc8 2778 int limit= INT_MIN;
0da71265 2779
827c91bf 2780 /* sort frame according to poc in B slice */
0da71265 2781 for(out_i=0; out_i<h->short_ref_count; out_i++){
29860cc8 2782 int best_i=INT_MIN;
792bb815 2783 int best_poc=INT_MAX;
0da71265
MN
2784
2785 for(i=0; i<h->short_ref_count; i++){
2786 const int poc= h->short_ref[i]->poc;
2787 if(poc > limit && poc < best_poc){
2788 best_poc= poc;
2789 best_i= i;
2790 }
2791 }
115329f1 2792
29860cc8 2793 assert(best_i != INT_MIN);
115329f1 2794
0da71265
MN
2795 limit= best_poc;
2796 sorted_short_ref[out_i]= *h->short_ref[best_i];
a9c9a240 2797 tprintf(h->s.avctx, "sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
827c91bf
LLL
2798 if (-1 == smallest_poc_greater_than_current) {
2799 if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
2800 smallest_poc_greater_than_current = out_i;
2801 }
2802 }
0da71265
MN
2803 }
2804 }
2805
2806 if(s->picture_structure == PICT_FRAME){
2807 if(h->slice_type==B_TYPE){
0da71265 2808 int list;
a9c9a240 2809 tprintf(h->s.avctx, "current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
0da71265 2810
827c91bf 2811 // find the largest poc
0da71265 2812 for(list=0; list<2; list++){
827c91bf 2813 int index = 0;
3b33943e
MN
2814 int j= -99;
2815 int step= list ? -1 : 1;
827c91bf 2816
3b33943e
MN
2817 for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
2818 while(j<0 || j>= h->short_ref_count){
6ab87211
MN
2819 if(j != -99 && step == (list ? -1 : 1))
2820 return -1;
3b33943e
MN
2821 step = -step;
2822 j= smallest_poc_greater_than_current + (step>>1);
0da71265 2823 }
3b33943e
MN
2824 if(sorted_short_ref[j].reference != 3) continue;
2825 h->default_ref_list[list][index ]= sorted_short_ref[j];
2826 h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
0da71265
MN
2827 }
2828
827c91bf
LLL
2829 for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
2830 if(h->long_ref[i] == NULL) continue;
0da71265
MN
2831 if(h->long_ref[i]->reference != 3) continue;
2832
2833 h->default_ref_list[ list ][index ]= *h->long_ref[i];
2834 h->default_ref_list[ list ][index++].pic_id= i;;
2835 }
115329f1 2836
3b33943e 2837 if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
827c91bf
LLL
2838 // swap the two first elements of L1 when
2839 // L0 and L1 are identical
0da71265
MN
2840 Picture temp= h->default_ref_list[1][0];
2841 h->default_ref_list[1][0] = h->default_ref_list[1][1];
5cb46bc7 2842 h->default_ref_list[1][1] = temp;
0da71265
MN
2843 }
2844
2845 if(index < h->ref_count[ list ])
2846 memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
2847 }
2848 }else{
2849 int index=0;
2f944356 2850 for(i=0; i<h->short_ref_count; i++){
0da71265
MN
2851 if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
2852 h->default_ref_list[0][index ]= *h->short_ref[i];
2853 h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
2854 }
2f944356 2855 for(i = 0; i < 16; i++){
827c91bf 2856 if(h->long_ref[i] == NULL) continue;
0da71265
MN
2857 if(h->long_ref[i]->reference != 3) continue;
2858 h->default_ref_list[0][index ]= *h->long_ref[i];
2859 h->default_ref_list[0][index++].pic_id= i;;
2860 }
2861 if(index < h->ref_count[0])
2862 memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
2863 }
2864 }else{ //FIELD
2865 if(h->slice_type==B_TYPE){
2866 }else{
2867 //FIXME second field balh
2868 }
2869 }
827c91bf
LLL
2870#ifdef TRACE
2871 for (i=0; i<h->ref_count[0]; i++) {
a9c9a240 2872 tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
827c91bf
LLL
2873 }
2874 if(h->slice_type==B_TYPE){
2875 for (i=0; i<h->ref_count[1]; i++) {
a9c9a240 2876 tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
827c91bf
LLL
2877 }
2878 }
2879#endif
0da71265
MN
2880 return 0;
2881}
2882
827c91bf
LLL
2883static void print_short_term(H264Context *h);
2884static void print_long_term(H264Context *h);
2885
0da71265
MN
2886static int decode_ref_pic_list_reordering(H264Context *h){
2887 MpegEncContext * const s = &h->s;
6ab87211 2888 int list, index;
115329f1 2889
827c91bf
LLL
2890 print_short_term(h);
2891 print_long_term(h);
3b66c4c5 2892 if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
115329f1 2893
3425501d 2894 for(list=0; list<h->list_count; list++){
0da71265
MN
2895 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
2896
2897 if(get_bits1(&s->gb)){
2898 int pred= h->curr_pic_num;
0da71265
MN
2899
2900 for(index=0; ; index++){
88e7a4d1
MN
2901 unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
2902 unsigned int pic_id;
0da71265 2903 int i;
2f944356 2904 Picture *ref = NULL;
115329f1
DB
2905
2906 if(reordering_of_pic_nums_idc==3)
0bc42cad 2907 break;
115329f1 2908
0da71265 2909 if(index >= h->ref_count[list]){
9b879566 2910 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
0da71265
MN
2911 return -1;
2912 }
115329f1 2913
0da71265
MN
2914 if(reordering_of_pic_nums_idc<3){
2915 if(reordering_of_pic_nums_idc<2){
88e7a4d1 2916 const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
0da71265
MN
2917
2918 if(abs_diff_pic_num >= h->max_pic_num){
9b879566 2919 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
0da71265
MN
2920 return -1;
2921 }
2922
2923 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
2924 else pred+= abs_diff_pic_num;
2925 pred &= h->max_pic_num - 1;
115329f1 2926
0d175622
MN
2927 for(i= h->short_ref_count-1; i>=0; i--){
2928 ref = h->short_ref[i];
2929 assert(ref->reference == 3);
2930 assert(!ref->long_ref);
2931 if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
0da71265
MN
2932 break;
2933 }
0d175622
MN
2934 if(i>=0)
2935 ref->pic_id= ref->frame_num;
0da71265
MN
2936 }else{
2937 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
88e7a4d1
MN
2938 if(pic_id>31){
2939 av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
2940 return -1;
2941 }
0d175622 2942 ref = h->long_ref[pic_id];
ac658be5
FOL
2943 if(ref){
2944 ref->pic_id= pic_id;
2945 assert(ref->reference == 3);
2946 assert(ref->long_ref);
2947 i=0;
2948 }else{
2949 i=-1;
2950 }
0da71265
MN
2951 }
2952
0d315f28 2953 if (i < 0) {
9b879566 2954 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
0da71265 2955 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
0d175622
MN
2956 } else {
2957 for(i=index; i+1<h->ref_count[list]; i++){
2958 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
2959 break;
21be92bf
MN
2960 }
2961 for(; i > index; i--){
2962 h->ref_list[list][i]= h->ref_list[list][i-1];
2963 }
0d175622 2964 h->ref_list[list][index]= *ref;
0da71265 2965 }
0bc42cad 2966 }else{
9b879566 2967 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
0da71265
MN
2968 return -1;
2969 }
2970 }
2971 }
0da71265 2972 }
3425501d 2973 for(list=0; list<h->list_count; list++){
6ab87211
MN
2974 for(index= 0; index < h->ref_count[list]; index++){
2975 if(!h->ref_list[list][index].data[0])
2976 h->ref_list[list][index]= s->current_picture;
2977 }
6ab87211 2978 }
115329f1 2979
5ad984c9
LM
2980 if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
2981 direct_dist_scale_factor(h);
2f944356 2982 direct_ref_list_init(h);
115329f1 2983 return 0;
0da71265
MN
2984}
2985
91c58c94 2986static void fill_mbaff_ref_list(H264Context *h){
5d18eaad 2987 int list, i, j;
3425501d 2988 for(list=0; list<2; list++){ //FIXME try list_count
5d18eaad
LM
2989 for(i=0; i<h->ref_count[list]; i++){
2990 Picture *frame = &h->ref_list[list][i];
2991 Picture *field = &h->ref_list[list][16+2*i];
2992 field[0] = *frame;
2993 for(j=0; j<3; j++)
2994 field[0].linesize[j] <<= 1;
2995 field[1] = field[0];
2996 for(j=0; j<3; j++)
2997 field[1].data[j] += frame->linesize[j];
2998
2999 h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
3000 h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
3001 for(j=0; j<2; j++){
3002 h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
3003 h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
3004 }
3005 }
3006 }
3007 for(j=0; j<h->ref_count[1]; j++){
3008 for(i=0; i<h->ref_count[0]; i++)
3009 h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
3010 memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
3011 memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
3012 }
3013}
3014
0da71265
MN
3015static int pred_weight_table(H264Context *h){
3016 MpegEncContext * const s = &h->s;
3017 int list, i;
9f2d1b4f 3018 int luma_def, chroma_def;
115329f1 3019
9f2d1b4f
LM
3020 h->use_weight= 0;
3021 h->use_weight_chroma= 0;
0da71265
MN
3022 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
3023 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
9f2d1b4f
LM
3024 luma_def = 1<<h->luma_log2_weight_denom;
3025 chroma_def = 1<<h->chroma_log2_weight_denom;
0da71265
MN
3026
3027 for(list=0; list<2; list++){
3028 for(i=0; i<h->ref_count[list]; i++){
3029 int luma_weight_flag, chroma_weight_flag;
115329f1 3030
0da71265
MN
3031 luma_weight_flag= get_bits1(&s->gb);
3032 if(luma_weight_flag){
3033 h->luma_weight[list][i]= get_se_golomb(&s->gb);
3034 h->luma_offset[list][i]= get_se_golomb(&s->gb);
9f2d1b4f
LM
3035 if( h->luma_weight[list][i] != luma_def
3036 || h->luma_offset[list][i] != 0)
3037 h->use_weight= 1;
3038 }else{
3039 h->luma_weight[list][i]= luma_def;
3040 h->luma_offset[list][i]= 0;
0da71265
MN
3041 }
3042
3043 chroma_weight_flag= get_bits1(&s->gb);
3044 if(chroma_weight_flag){
3045 int j;
3046 for(j=0; j<2; j++){
3047 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
3048 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
9f2d1b4f
LM
3049 if( h->chroma_weight[list][i][j] != chroma_def
3050 || h->chroma_offset[list][i][j] != 0)
3051 h->use_weight_chroma= 1;
3052 }
3053 }else{
3054 int j;
3055 for(j=0; j<2; j++){
3056 h->chroma_weight[list][i][j]= chroma_def;
3057 h->chroma_offset[list][i][j]= 0;
0da71265
MN
3058 }
3059 }
3060 }
3061 if(h->slice_type != B_TYPE) break;
3062 }
9f2d1b4f 3063 h->use_weight= h->use_weight || h->use_weight_chroma;
0da71265
MN
3064 return 0;
3065}
3066
9f2d1b4f
LM
3067static void implicit_weight_table(H264Context *h){
3068 MpegEncContext * const s = &h->s;
9f2d1b4f
LM
3069 int ref0, ref1;
3070 int cur_poc = s->current_picture_ptr->poc;
3071
3072 if( h->ref_count[0] == 1 && h->ref_count[1] == 1
3073 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
3074 h->use_weight= 0;
3075 h->use_weight_chroma= 0;
3076 return;
3077 }
3078
3079 h->use_weight= 2;
3080 h->use_weight_chroma= 2;
3081 h->luma_log2_weight_denom= 5;
3082 h->chroma_log2_weight_denom= 5;
3083
9f2d1b4f
LM
3084 for(ref0=0; ref0 < h->ref_count[0]; ref0++){
3085 int poc0 = h->ref_list[0][ref0].poc;
3086 for(ref1=0; ref1 < h->ref_count[1]; ref1++){
738386a5 3087 int poc1 = h->ref_list[1][ref1].poc;
f66e4f5f 3088 int td = av_clip(poc1 - poc0, -128, 127);
9f2d1b4f 3089 if(td){
f66e4f5f 3090 int tb = av_clip(cur_poc - poc0, -128, 127);
c26abfa5 3091 int tx = (16384 + (FFABS(td) >> 1)) / td;
f66e4f5f 3092 int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
9f2d1b4f
LM
3093 if(dist_scale_factor < -64 || dist_scale_factor > 128)
3094 h->implicit_weight[ref0][ref1] = 32;
3095 else
3096 h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
3097 }else
3098 h->implicit_weight[ref0][ref1] = 32;
3099 }
3100 }
3101}
3102
8fd57a66
JD
3103/**
3104 * Mark a picture as no longer needed for reference. The refmask
3105 * argument allows unreferencing of individual fields or the whole frame.
3106 * If the picture becomes entirely unreferenced, but is being held for
3107 * display purposes, it is marked as such.
3108 * @param refmask mask of fields to unreference; the mask is bitwise
3109 * anded with the reference marking of pic
3110 * @return non-zero if pic becomes entirely unreferenced (except possibly
3111 * for display purposes) zero if one of the fields remains in
3112 * reference
3113 */
3114static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
4e4d983e 3115 int i;
8fd57a66
JD
3116 if (pic->reference &= refmask) {
3117 return 0;
3118 } else {
5a7b254c
JD
3119 if(pic == h->delayed_output_pic)
3120 pic->reference=DELAYED_PIC_REF;
3121 else{
3122 for(i = 0; h->delayed_pic[i]; i++)
3123 if(pic == h->delayed_pic[i]){
3124 pic->reference=DELAYED_PIC_REF;
3125 break;
3126 }
3127 }
8fd57a66
JD
3128 return 1;
3129 }
4e4d983e
LM
3130}
3131
0da71265 3132/**
5175b937 3133 * instantaneous decoder refresh.
0da71265
MN
3134 */
3135static void idr(H264Context *h){
4e4d983e 3136 int i;
0da71265 3137
dc032f33
LLL
3138 for(i=0; i<16; i++){
3139 if (h->long_ref[i] != NULL) {
8fd57a66 3140 unreference_pic(h, h->long_ref[i], 0);
dc032f33
LLL
3141 h->long_ref[i]= NULL;
3142 }
0da71265
MN
3143 }
3144 h->long_ref_count=0;
3145
3146 for(i=0; i<h->short_ref_count; i++){
8fd57a66 3147 unreference_pic(h, h->short_ref[i], 0);
0da71265
MN
3148 h->short_ref[i]= NULL;
3149 }
3150 h->short_ref_count=0;
3151}
3152
7c33ad19
LM
3153/* forget old pics after a seek */
3154static void flush_dpb(AVCodecContext *avctx){
3155 H264Context *h= avctx->priv_data;
3156 int i;
285b570f
LM
3157 for(i=0; i<16; i++) {
3158 if(h->delayed_pic[i])
3159 h->delayed_pic[i]->reference= 0;
7c33ad19 3160 h->delayed_pic[i]= NULL;
285b570f
LM
3161 }
3162 if(h->delayed_output_pic)
3163 h->delayed_output_pic->reference= 0;
7c33ad19
LM
3164 h->delayed_output_pic= NULL;
3165 idr(h);
ca159196
MR
3166 if(h->s.current_picture_ptr)
3167 h->s.current_picture_ptr->reference= 0;
7c33ad19
LM
3168}
3169
0da71265 3170/**
47e112f8
JD
3171 * Find a Picture in the short term reference list by frame number.
3172 * @param frame_num frame number to search for
3173 * @param idx the index into h->short_ref where returned picture is found
3174 * undefined if no picture found.
3175 * @return pointer to the found picture, or NULL if no pic with the provided
3176 * frame number is found
0da71265 3177 */
47e112f8 3178static Picture * find_short(H264Context *h, int frame_num, int *idx){
1924f3ce 3179 MpegEncContext * const s = &h->s;
0da71265 3180 int i;
115329f1 3181
0da71265
MN
3182 for(i=0; i<h->short_ref_count; i++){
3183 Picture *pic= h->short_ref[i];
1924f3ce 3184 if(s->avctx->debug&FF_DEBUG_MMCO)
9b879566 3185 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
47e112f8
JD
3186 if(pic->frame_num == frame_num) {
3187 *idx = i;
0da71265
MN
3188 return pic;
3189 }
3190 }
3191 return NULL;
3192}
3193
3194/**
47e112f8
JD
3195 * Remove a picture from the short term reference list by its index in
3196 * that list. This does no checking on the provided index; it is assumed
3197 * to be valid. Other list entries are shifted down.
3198 * @param i index into h->short_ref of picture to remove.
3199 */
3200static void remove_short_at_index(H264Context *h, int i){
3201 assert(i > 0 && i < h->short_ref_count);
3202 h->short_ref[i]= NULL;
3203 if (--h->short_ref_count)
3204 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
3205}
3206
3207/**
3208 *
3209 * @return the removed picture or NULL if an error occurs
3210 */
3211static Picture * remove_short(H264Context *h, int frame_num){
3212 MpegEncContext * const s = &h->s;
3213 Picture *pic;
3214 int i;
3215
3216 if(s->avctx->debug&FF_DEBUG_MMCO)
3217 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
3218
3219 pic = find_short(h, frame_num, &i);
3220 if (pic)
3221 remove_short_at_index(h, i);
3222
3223 return pic;
3224}
3225
3226/**
24231e4c
JD
3227 * Remove a picture from the long term reference list by its index in
3228 * that list. This does no checking on the provided index; it is assumed
3229 * to be valid. The removed entry is set to NULL. Other entries are unaffected.
3230 * @param i index into h->long_ref of picture to remove.
3231 */
3232static void remove_long_at_index(H264Context *h, int i){
3233 h->long_ref[i]= NULL;
3234 h->long_ref_count--;
3235}
3236
3237/**
0da71265 3238 *
3b66c4c5 3239 * @return the removed picture or NULL if an error occurs
0da71265
MN
3240 */
3241static Picture * remove_long(H264Context *h, int i){
3242 Picture *pic;
3243
0da71265 3244 pic= h->long_ref[i];
24231e4c
JD
3245 if (pic)
3246 remove_long_at_index(h, i);
0da71265
MN
3247
3248 return pic;
3249}
3250
3251/**
827c91bf
LLL
3252 * print short term list
3253 */
3254static void print_short_term(H264Context *h) {
3255 uint32_t i;
3256 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
3257 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
3258 for(i=0; i<h->short_ref_count; i++){
3259 Picture *pic= h->short_ref[i];
3260 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
3261 }
3262 }
3263}
3264
3265/**
3266 * print long term list
3267 */
3268static void print_long_term(H264Context *h) {
3269 uint32_t i;
3270 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
3271 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
3272 for(i = 0; i < 16; i++){
3273 Picture *pic= h->long_ref[i];
3274 if (pic) {
3275 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
3276 }
3277 }
3278 }
3279}
3280
3281/**
0da71265
MN
3282 * Executes the reference picture marking (memory management control operations).
3283 */
3284static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
3285 MpegEncContext * const s = &h->s;
827c91bf 3286 int i, j;
0da71265
MN
3287 int current_is_long=0;
3288 Picture *pic;
115329f1 3289
0da71265 3290 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
9b879566 3291 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
115329f1 3292
0da71265
MN
3293 for(i=0; i<mmco_count; i++){
3294 if(s->avctx->debug&FF_DEBUG_MMCO)
0d0447ea 3295 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
0da71265
MN
3296
3297 switch(mmco[i].opcode){
3298 case MMCO_SHORT2UNUSED:
0d0447ea 3299 pic= remove_short(h, mmco[i].short_pic_num);
806bb93f 3300 if(pic)
8fd57a66 3301 unreference_pic(h, pic, 0);
806bb93f
MN
3302 else if(s->avctx->debug&FF_DEBUG_MMCO)