2 * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file libavcodec/h264_direct.c
24 * H.264 / AVC / MPEG4 part10 direct mb/block decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
31 #include "mpegvideo.h"
33 #include "h264_mvpred.h"
34 #include "rectangle.h"
40 static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
41 int poc0 = h->ref_list[0][i].poc;
42 int td = av_clip(poc1 - poc0, -128, 127);
43 if(td == 0 || h->ref_list[0][i].long_ref){
46 int tb = av_clip(poc - poc0, -128, 127);
47 int tx = (16384 + (FFABS(td) >> 1)) / td;
48 return av_clip((tb*tx + 32) >> 6, -1024, 1023);
52 void ff_h264_direct_dist_scale_factor(H264Context * const h){
53 MpegEncContext * const s = &h->s;
54 const int poc = h->s.current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
55 const int poc1 = h->ref_list[1][0].poc;
57 for(field=0; field<2; field++){
58 const int poc = h->s.current_picture_ptr->field_poc[field];
59 const int poc1 = h->ref_list[1][0].field_poc[field];
60 for(i=0; i < 2*h->ref_count[0]; i++)
61 h->dist_scale_factor_field[field][i^field] = get_scale_factor(h, poc, poc1, i+16);
64 for(i=0; i<h->ref_count[0]; i++){
65 h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
69 static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
70 MpegEncContext * const s = &h->s;
71 Picture * const ref1 = &h->ref_list[1][0];
72 int j, old_ref, rfield;
73 int start= mbafi ? 16 : 0;
74 int end = mbafi ? 16+2*h->ref_count[0] : h->ref_count[0];
75 int interl= mbafi || s->picture_structure != PICT_FRAME;
77 /* bogus; fills in for missing frames */
78 memset(map[list], 0, sizeof(map[list]));
80 for(rfield=0; rfield<2; rfield++){
81 for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
82 int poc = ref1->ref_poc[colfield][list][old_ref];
86 else if( interl && (poc&3) == 3) //FIXME store all MBAFF references so this isnt needed
87 poc= (poc&~3) + rfield + 1;
89 for(j=start; j<end; j++){
90 if(4*h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference&3) == poc){
91 int cur_ref= mbafi ? (j-16)^field : j;
92 map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
93 if(rfield == field || !interl)
94 map[list][old_ref] = cur_ref;
102 void ff_h264_direct_ref_list_init(H264Context * const h){
103 MpegEncContext * const s = &h->s;
104 Picture * const ref1 = &h->ref_list[1][0];
105 Picture * const cur = s->current_picture_ptr;
107 int sidx= (s->picture_structure&1)^1;
108 int ref1sidx= (ref1->reference&1)^1;
110 for(list=0; list<2; list++){
111 cur->ref_count[sidx][list] = h->ref_count[list];
112 for(j=0; j<h->ref_count[list]; j++)
113 cur->ref_poc[sidx][list][j] = 4*h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference&3);
116 if(s->picture_structure == PICT_FRAME){
117 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
118 memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
121 cur->mbaff= FRAME_MBAFF;
124 if(s->picture_structure == PICT_FRAME){
125 int cur_poc = s->current_picture_ptr->poc;
126 int *col_poc = h->ref_list[1]->field_poc;
127 h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
128 ref1sidx=sidx= h->col_parity;
129 }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity
130 h->col_fieldoff= s->mb_stride*(2*(h->ref_list[1][0].reference) - 3);
133 if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
136 for(list=0; list<2; list++){
137 fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
139 for(field=0; field<2; field++)
140 fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
144 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
145 MpegEncContext * const s = &h->s;
146 int b8_stride = h->b8_stride;
147 int b4_stride = h->b_stride;
148 int mb_xy = h->mb_xy;
150 const int16_t (*l1mv0)[2], (*l1mv1)[2];
151 const int8_t *l1ref0, *l1ref1;
152 const int is_b8x8 = IS_8X8(*mb_type);
153 unsigned int sub_mb_type;
156 assert(h->ref_list[1][0].reference&3);
158 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
160 if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
161 if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
162 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
165 mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
168 }else{ // AFL/AFR/FR/FL -> AFR/FR
169 if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
170 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
171 mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
172 mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
176 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
177 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
178 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
180 *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
182 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
184 }else{ // AFR/FR -> AFR/FR
187 mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
188 if(IS_8X8(mb_type_col[0]) && !h->sps.direct_8x8_inference_flag){
189 /* FIXME save sub mb types from previous frames (or derive from MVs)
190 * so we know exactly what block size to use */
191 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
192 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
193 }else if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
194 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
195 *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
196 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
197 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
198 *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
200 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
201 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
206 l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
207 l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
208 l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
209 l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
212 l1ref0 += h->b8_stride;
213 l1ref1 += h->b8_stride;
214 l1mv0 += 2*b4_stride;
215 l1mv1 += 2*b4_stride;
219 if(h->direct_spatial_mv_pred){
224 /* FIXME interlacing + spatial direct uses wrong colocated block positions */
226 /* ref = min(neighbors) */
227 for(list=0; list<2; list++){
228 int refa = h->ref_cache[list][scan8[0] - 1];
229 int refb = h->ref_cache[list][scan8[0] - 8];
230 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
231 if(refc == PART_NOT_AVAILABLE)
232 refc = h->ref_cache[list][scan8[0] - 8 - 1];
233 ref[list] = FFMIN3((unsigned)refa, (unsigned)refb, (unsigned)refc);
235 pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
237 int mask= ~(MB_TYPE_L0 << (2*list));
238 mv[list][0] = mv[list][1] = 0;
245 if(ref[0] < 0 && ref[1] < 0){
248 *mb_type |= MB_TYPE_L0L1;
249 sub_mb_type |= MB_TYPE_L0L1;
252 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
253 for(i8=0; i8<4; i8++){
256 int xy8 = x8+y8*b8_stride;
257 int xy4 = 3*x8+y8*b4_stride;
260 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
262 h->sub_mb_type[i8] = sub_mb_type;
264 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
265 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
266 if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
267 && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
268 || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
270 a= pack16to32(mv[0][0],mv[0][1]);
272 b= pack16to32(mv[1][0],mv[1][1]);
274 a= pack16to32(mv[0][0],mv[0][1]);
275 b= pack16to32(mv[1][0],mv[1][1]);
277 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
278 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
280 }else if(IS_16X16(*mb_type)){
283 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
284 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
285 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
286 && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
287 || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
288 && (h->x264_build>33 || !h->x264_build)))){
290 a= pack16to32(mv[0][0],mv[0][1]);
292 b= pack16to32(mv[1][0],mv[1][1]);
294 a= pack16to32(mv[0][0],mv[0][1]);
295 b= pack16to32(mv[1][0],mv[1][1]);
297 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
298 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
300 for(i8=0; i8<4; i8++){
302 const int y8 = i8>>1;
304 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
306 h->sub_mb_type[i8] = sub_mb_type;
308 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
309 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
310 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
311 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
314 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[x8 + y8*b8_stride] == 0
315 || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
316 && (h->x264_build>33 || !h->x264_build)))){
317 const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
318 if(IS_SUB_8X8(sub_mb_type)){
319 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
320 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
322 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
324 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
327 for(i4=0; i4<4; i4++){
328 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
329 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
331 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
333 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
339 }else{ /* direct temporal mv pred */
340 const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
341 const int *dist_scale_factor = h->dist_scale_factor;
344 if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
345 map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
346 map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
347 dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
349 if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0]))
352 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
353 int y_shift = 2*!IS_INTERLACED(*mb_type);
354 assert(h->sps.direct_8x8_inference_flag);
356 for(i8=0; i8<4; i8++){
358 const int y8 = i8>>1;
360 const int16_t (*l1mv)[2]= l1mv0;
362 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
364 h->sub_mb_type[i8] = sub_mb_type;
366 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
367 if(IS_INTRA(mb_type_col[y8])){
368 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
369 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
370 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
374 ref0 = l1ref0[x8 + y8*b8_stride];
376 ref0 = map_col_to_list0[0][ref0 + ref_offset];
378 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
381 scale = dist_scale_factor[ref0];
382 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
385 const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
386 int my_col = (mv_col[1]<<y_shift)/2;
387 int mx = (scale * mv_col[0] + 128) >> 8;
388 int my = (scale * my_col + 128) >> 8;
389 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
390 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
396 /* one-to-one mv scaling */
398 if(IS_16X16(*mb_type)){
401 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
402 if(IS_INTRA(mb_type_col[0])){
405 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
406 : map_col_to_list0[1][l1ref1[0] + ref_offset];
407 const int scale = dist_scale_factor[ref0];
408 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
410 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
411 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
413 mv0= pack16to32(mv_l0[0],mv_l0[1]);
414 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
416 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
417 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
418 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
420 for(i8=0; i8<4; i8++){
422 const int y8 = i8>>1;
424 const int16_t (*l1mv)[2]= l1mv0;
426 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
428 h->sub_mb_type[i8] = sub_mb_type;
429 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
430 if(IS_INTRA(mb_type_col[0])){
431 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
432 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
433 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
437 ref0 = l1ref0[x8 + y8*b8_stride];
439 ref0 = map_col_to_list0[0][ref0 + ref_offset];
441 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
444 scale = dist_scale_factor[ref0];
446 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
447 if(IS_SUB_8X8(sub_mb_type)){
448 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
449 int mx = (scale * mv_col[0] + 128) >> 8;
450 int my = (scale * mv_col[1] + 128) >> 8;
451 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
452 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
454 for(i4=0; i4<4; i4++){
455 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
456 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
457 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
458 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
459 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
460 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);