forked from ittiam-systems/libavc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathih264e_intra_modes_eval.c
2390 lines (2077 loc) · 87.3 KB
/
ih264e_intra_modes_eval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*****************************************************************************
* Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/**
*******************************************************************************
* @file
* ih264e_intra_modes_eval.c
*
* @brief
* This file contains definitions of routines that perform rate distortion
* analysis on a macroblock if they are to be coded as intra.
*
* @author
* ittiam
*
* @par List of Functions:
* - ih264e_derive_neighbor_availability_of_mbs
* - ih264e_derive_ngbr_avbl_of_mb_partitions
* - ih264e_evaluate_intra16x16_modes_for_least_cost_rdoptoff
* - ih264e_evaluate_intra8x8_modes_for_least_cost_rdoptoff
* - ih264e_evaluate_intra4x4_modes_for_least_cost_rdoptoff
* - ih264e_evaluate_intra4x4_modes_for_least_cost_rdopton
* - ih264e_evaluate_chroma_intra8x8_modes_for_least_cost_rdoptoff
* - ih264e_evaluate_intra16x16_modes
* - ih264e_evaluate_intra4x4_modes
* - ih264e_evaluate_intra_chroma_modes
*
* @remarks
* none
*
*******************************************************************************
*/
/*****************************************************************************/
/* File Includes */
/*****************************************************************************/
/* System Include Files */
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
/* User Include Files */
#include "ih264e_config.h"
#include "ih264_typedefs.h"
#include "iv2.h"
#include "ive2.h"
#include "ih264_debug.h"
#include "ih264_macros.h"
#include "ih264_defs.h"
#include "ih264_mem_fns.h"
#include "ih264_padding.h"
#include "ih264_structs.h"
#include "ih264_trans_quant_itrans_iquant.h"
#include "ih264_inter_pred_filters.h"
#include "ih264_intra_pred_filters.h"
#include "ih264_deblk_edge_filters.h"
#include "ih264_common_tables.h"
#include "ih264_cabac_tables.h"
#include "ime_defs.h"
#include "ime_distortion_metrics.h"
#include "ime_structs.h"
#include "ime_platform_macros.h"
#include "irc_cntrl_param.h"
#include "irc_frame_info_collector.h"
#include "ih264e_error.h"
#include "ih264e_defs.h"
#include "ih264e_globals.h"
#include "ih264e_rate_control.h"
#include "ih264e_bitstream.h"
#include "ih264e_cabac_structs.h"
#include "ih264e_structs.h"
#include "ih264e_intra_modes_eval.h"
/*****************************************************************************/
/* Function Definitions */
/*****************************************************************************/
/**
******************************************************************************
*
* @brief
* derivation process for macroblock availability
*
* @par Description
* Calculates the availability of the left, top, topright and topleft macroblocks.
*
* @param[in] ps_proc_ctxt
* pointer to proc context (handle)
*
* @remarks Based on section 6.4.5 in H264 spec
*
* @return none
*
******************************************************************************
*/
void ih264e_derive_nghbr_avbl_of_mbs(process_ctxt_t *ps_proc)
{
UWORD8 *pu1_slice_idx_curr = ps_proc->pu1_slice_idx;
UWORD8 *pu1_slice_idx_b;
UWORD8 *pu1_slice_idx_a;
UWORD8 *pu1_slice_idx_c;
UWORD8 *pu1_slice_idx_d;
block_neighbors_t *ps_ngbr_avbl;
WORD32 i4_mb_x, i4_mb_y;
WORD32 i4_wd_mbs;
i4_mb_x = ps_proc->i4_mb_x;
i4_mb_y = ps_proc->i4_mb_y;
i4_wd_mbs = ps_proc->i4_wd_mbs;
pu1_slice_idx_curr += (i4_mb_y * i4_wd_mbs) + i4_mb_x;
pu1_slice_idx_a = pu1_slice_idx_curr - 1;
pu1_slice_idx_b = pu1_slice_idx_curr - i4_wd_mbs;
pu1_slice_idx_c = pu1_slice_idx_b + 1;
pu1_slice_idx_d = pu1_slice_idx_b - 1;
ps_ngbr_avbl = ps_proc->ps_ngbr_avbl;
/**********************************************************************/
/* The macroblock is marked as available, unless one of the following */
/* conditions is true in which case the macroblock shall be marked as */
/* not available. */
/* 1. mbAddr < 0 */
/* 2 mbAddr > CurrMbAddr */
/* 3. the macroblock with address mbAddr belongs to a different slice */
/* than the macroblock with address CurrMbAddr */
/**********************************************************************/
/* left macroblock availability */
if (i4_mb_x == 0)
{ /* macroblocks along first column */
ps_ngbr_avbl->u1_mb_a = 0;
}
else
{ /* macroblocks belong to same slice? */
if (*pu1_slice_idx_a != *pu1_slice_idx_curr)
ps_ngbr_avbl->u1_mb_a = 0;
else
ps_ngbr_avbl->u1_mb_a = 1;
}
/* top macroblock availability */
if (i4_mb_y == 0)
{ /* macroblocks along first row */
ps_ngbr_avbl->u1_mb_b = 0;
}
else
{ /* macroblocks belong to same slice? */
if (*pu1_slice_idx_b != *pu1_slice_idx_curr)
ps_ngbr_avbl->u1_mb_b = 0;
else
ps_ngbr_avbl->u1_mb_b = 1;
}
/* top right macroblock availability */
if (i4_mb_x == i4_wd_mbs-1 || i4_mb_y == 0)
{ /* macroblocks along last column */
ps_ngbr_avbl->u1_mb_c = 0;
}
else
{ /* macroblocks belong to same slice? */
if (*pu1_slice_idx_c != *pu1_slice_idx_curr)
ps_ngbr_avbl->u1_mb_c = 0;
else
ps_ngbr_avbl->u1_mb_c = 1;
}
/* top left macroblock availability */
if (i4_mb_x == 0 || i4_mb_y == 0)
{ /* macroblocks along first column */
ps_ngbr_avbl->u1_mb_d = 0;
}
else
{ /* macroblocks belong to same slice? */
if (*pu1_slice_idx_d != *pu1_slice_idx_curr)
ps_ngbr_avbl->u1_mb_d = 0;
else
ps_ngbr_avbl->u1_mb_d = 1;
}
}
/**
******************************************************************************
*
* @brief
* derivation process for subblock/partition availability
*
* @par Description
* Calculates the availability of the left, top, topright and topleft subblock
* or partitions.
*
* @param[in] ps_proc_ctxt
* pointer to macroblock context (handle)
*
* @param[in] i1_pel_pos_x
* column position of the pel wrt the current block
*
* @param[in] i1_pel_pos_y
* row position of the pel in wrt current block
*
* @remarks Assumptions: before calling this function it is assumed that
* the neighbor availability of the current macroblock is already derived.
* Based on table 6-3 of H264 specification
*
* @return availability status (yes or no)
*
******************************************************************************
*/
UWORD8 ih264e_derive_ngbr_avbl_of_mb_partitions(block_neighbors_t *ps_ngbr_avbl,
WORD8 i1_pel_pos_x,
WORD8 i1_pel_pos_y)
{
UWORD8 u1_neighbor_avail=0;
/**********************************************************************/
/* values of i1_pel_pos_x in the range 0-15 inclusive correspond to */
/* various columns of a macroblock */
/* */
/* values of i1_pel_pos_y in the range 0-15 inclusive correspond to */
/* various rows of a macroblock */
/* */
/* other values of i1_pel_pos_x & i1_pel_pos_y represents elements */
/* outside the bound of an mb ie., represents its neighbors. */
/**********************************************************************/
if (i1_pel_pos_x < 0)
{ /* column(-1) */
if (i1_pel_pos_y < 0)
{ /* row(-1) */
u1_neighbor_avail = ps_ngbr_avbl->u1_mb_d; /* current mb topleft availability */
}
else if (i1_pel_pos_y >= 0 && i1_pel_pos_y < 16)
{ /* all rows of a macroblock */
u1_neighbor_avail = ps_ngbr_avbl->u1_mb_a; /* current mb left availability */
}
else /* if (i1_pel_pos_y >= 16) */
{ /* rows(+16) */
u1_neighbor_avail = 0; /* current mb bottom left availability */
}
}
else if (i1_pel_pos_x >= 0 && i1_pel_pos_x < 16)
{ /* all columns of a macroblock */
if (i1_pel_pos_y < 0)
{ /* row(-1) */
u1_neighbor_avail = ps_ngbr_avbl->u1_mb_b; /* current mb top availability */
}
else if (i1_pel_pos_y >= 0 && i1_pel_pos_y < 16)
{ /* all rows of a macroblock */
u1_neighbor_avail = 1; /* current mb availability */
/* availability of the partition is dependent on the position of the partition inside the mb */
/* although the availability is declared as 1 in all cases these needs to be corrected somewhere else and this is not done in here */
}
else /* if (i1_pel_pos_y >= 16) */
{ /* rows(+16) */
u1_neighbor_avail = 0; /* current mb bottom availability */
}
}
else if (i1_pel_pos_x >= 16)
{ /* column(+16) */
if (i1_pel_pos_y < 0)
{ /* row(-1) */
u1_neighbor_avail = ps_ngbr_avbl->u1_mb_c; /* current mb top right availability */
}
else /* if (i1_pel_pos_y >= 0) */
{ /* all other rows */
u1_neighbor_avail = 0; /* current mb right & bottom right availability */
}
}
return u1_neighbor_avail;
}
/**
******************************************************************************
*
* @brief
* evaluate best intra 16x16 mode (rate distortion opt off)
*
* @par Description
* This function evaluates all the possible intra 16x16 modes and finds the mode
* that best represents the macro-block (least distortion) and occupies fewer
* bits in the bit-stream.
*
* @param[in] ps_proc_ctxt
* pointer to process context (handle)
*
* @remarks
* Ideally the cost of encoding a macroblock is calculated as
* (distortion + lambda*rate). Where distortion is SAD/SATD,... between the
* input block and the reconstructed block and rate is the number of bits taken
* to place the macroblock in the bit-stream. In this routine the rate does not
* exactly point to the total number of bits it takes, rather it points to header
* bits necessary for encoding the macroblock. Assuming the deltaQP, cbp bits
* and residual bits fall in to texture bits the number of bits taken to encoding
* mbtype is considered as rate, we compute cost. Further we will approximate
* the distortion as the deviation b/w input and the predicted block as opposed
* to input and reconstructed block.
*
* NOTE: As per the Document JVT-O079, for intra 16x16 macroblock,
* the SAD and cost are one and the same.
*
* @return none
*
******************************************************************************
*/
void ih264e_evaluate_intra16x16_modes_for_least_cost_rdoptoff(process_ctxt_t *ps_proc)
{
/* Codec Context */
codec_t *ps_codec = ps_proc->ps_codec;
/* SAD(distortion metric) of an 8x8 block */
WORD32 i4_mb_distortion = INT_MAX, i4_mb_distortion_least = INT_MAX;
/* lambda */
UWORD32 u4_lambda = ps_proc->u4_lambda;
/* cost = distortion + lambda*rate */
WORD32 i4_mb_cost= INT_MAX, i4_mb_cost_least = INT_MAX;
/* intra mode */
UWORD32 u4_intra_mode, u4_best_intra_16x16_mode = DC_I16x16;
/* neighbor pels for intra prediction */
UWORD8 *pu1_ngbr_pels_i16 = ps_proc->au1_ngbr_pels;
/* neighbor availability */
WORD32 i4_ngbr_avbl;
/* pointer to src macro block */
UWORD8 *pu1_curr_mb = ps_proc->pu1_src_buf_luma;
UWORD8 *pu1_ref_mb = ps_proc->pu1_rec_buf_luma;
/* pointer to prediction macro block */
UWORD8 *pu1_pred_mb_intra_16x16 = ps_proc->pu1_pred_mb_intra_16x16;
UWORD8 *pu1_pred_mb_intra_16x16_plane = ps_proc->pu1_pred_mb_intra_16x16_plane;
/* strides */
WORD32 i4_src_strd = ps_proc->i4_src_strd;
WORD32 i4_pred_strd = ps_proc->i4_pred_strd;
WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
/* pointer to neighbors left, top, topleft */
UWORD8 *pu1_mb_a = pu1_ref_mb - 1;
UWORD8 *pu1_mb_b = pu1_ref_mb - i4_rec_strd;
UWORD8 *pu1_mb_d = pu1_mb_b - 1;
UWORD8 u1_mb_a, u1_mb_b, u1_mb_d;
/* valid intra modes map */
UWORD32 u4_valid_intra_modes;
/* lut for valid intra modes */
const UWORD8 u1_valid_intra_modes[8] = {4, 6, 4, 6, 5, 7, 5, 15};
/* temp var */
UWORD32 i, u4_enable_fast_sad = 0, offset = 0;
mb_info_t *ps_top_mb_syn_ele = ps_proc->ps_top_row_mb_syntax_ele + ps_proc->i4_mb_x;
UWORD32 u4_constrained_intra_pred = ps_proc->ps_codec->s_cfg.u4_constrained_intra_pred;
/* init temp var */
if (ps_proc->i4_slice_type != ISLICE)
{
/* Offset for MBtype */
offset = (ps_proc->i4_slice_type == PSLICE) ? 5 : 23;
u4_enable_fast_sad = ps_proc->s_me_ctxt.u4_enable_fast_sad;
}
/* locating neighbors that are available for prediction */
/* gather prediction pels from the neighbors, if particular set is not available
* it is set to zero*/
/* left pels */
u1_mb_a = ((ps_proc->ps_ngbr_avbl->u1_mb_a)
&& (u4_constrained_intra_pred ? ps_proc->s_left_mb_syntax_ele.u2_is_intra : 1));
if (u1_mb_a)
{
for(i = 0; i < 16; i++)
pu1_ngbr_pels_i16[16-1-i] = pu1_mb_a[i * i4_rec_strd];
}
else
{
ps_codec->pf_mem_set_mul8(pu1_ngbr_pels_i16,0,MB_SIZE);
}
/* top pels */
u1_mb_b = ((ps_proc->ps_ngbr_avbl->u1_mb_b)
&& (u4_constrained_intra_pred ? ps_top_mb_syn_ele->u2_is_intra : 1));
if (u1_mb_b)
{
ps_codec->pf_mem_cpy_mul8(pu1_ngbr_pels_i16+16+1,pu1_mb_b,16);
}
else
{
ps_codec->pf_mem_set_mul8(pu1_ngbr_pels_i16+16+1,0,MB_SIZE);
}
/* topleft pels */
u1_mb_d = ((ps_proc->ps_ngbr_avbl->u1_mb_d)
&& (u4_constrained_intra_pred ? ps_proc->s_top_left_mb_syntax_ele.u2_is_intra : 1));
if (u1_mb_d)
{
pu1_ngbr_pels_i16[16] = *pu1_mb_d;
}
else
{
pu1_ngbr_pels_i16[16] = 0;
}
i4_ngbr_avbl = (u1_mb_a) + (u1_mb_b << 2) + (u1_mb_d << 1);
ps_proc->i4_ngbr_avbl_16x16_mb = i4_ngbr_avbl;
/* set valid intra modes for evaluation */
u4_valid_intra_modes = u1_valid_intra_modes[i4_ngbr_avbl];
if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_FAST ||
ps_codec->s_cfg.u4_enc_speed_preset == IVE_FASTEST)
u4_valid_intra_modes &= ~(1 << PLANE_I16x16);
/* evaluate b/w HORZ_I16x16, VERT_I16x16 & DC_I16x16 */
ps_codec->pf_ih264e_evaluate_intra16x16_modes(pu1_curr_mb, pu1_ngbr_pels_i16, pu1_pred_mb_intra_16x16,
i4_src_strd, i4_pred_strd,
i4_ngbr_avbl, &u4_intra_mode, &i4_mb_distortion_least,
u4_valid_intra_modes);
/* cost = distortion + lambda*rate */
i4_mb_cost_least = i4_mb_distortion_least;
if (((u4_valid_intra_modes >> 3) & 1) != 0)
{
/* intra prediction for PLANE mode*/
(ps_codec->apf_intra_pred_16_l)[PLANE_I16x16](pu1_ngbr_pels_i16, pu1_pred_mb_intra_16x16_plane, 0, i4_pred_strd, i4_ngbr_avbl);
/* evaluate distortion between the actual blk and the estimated blk for the given mode */
ps_codec->apf_compute_sad_16x16[u4_enable_fast_sad](pu1_curr_mb, pu1_pred_mb_intra_16x16_plane, i4_src_strd, i4_pred_strd, i4_mb_cost_least, &i4_mb_distortion);
/* cost = distortion + lambda*rate */
i4_mb_cost = i4_mb_distortion;
/* update the least cost information if necessary */
if(i4_mb_cost < i4_mb_distortion_least)
{
u4_intra_mode = PLANE_I16x16;
i4_mb_cost_least = i4_mb_cost;
i4_mb_distortion_least = i4_mb_distortion;
}
}
u4_best_intra_16x16_mode = u4_intra_mode;
DEBUG("%d partition cost, %d intra mode\n", i4_mb_cost_least * 32, u4_best_intra_16x16_mode);
ps_proc->u1_l_i16_mode = u4_best_intra_16x16_mode;
/* cost = distortion + lambda*rate */
i4_mb_cost_least = i4_mb_distortion_least + u4_lambda*u1_uev_codelength[offset + u4_best_intra_16x16_mode];
/* update the type of the mb if necessary */
if (i4_mb_cost_least < ps_proc->i4_mb_cost)
{
ps_proc->i4_mb_cost = i4_mb_cost_least;
ps_proc->i4_mb_distortion = i4_mb_distortion_least;
ps_proc->u4_mb_type = I16x16;
}
if (i4_mb_cost_least < ps_proc->i4_mb_intra_cost)
{
ps_proc->i4_mb_intra_cost = i4_mb_cost_least;
}
return ;
}
/**
******************************************************************************
*
* @brief
* evaluate best intra 8x8 mode (rate distortion opt on)
*
* @par Description
* This function evaluates all the possible intra 8x8 modes and finds the mode
* that best represents the macro-block (least distortion) and occupies fewer
* bits in the bit-stream.
*
* @param[in] ps_proc_ctxt
* pointer to proc ctxt
*
* @remarks Ideally the cost of encoding a macroblock is calculated as
* (distortion + lambda*rate). Where distortion is SAD/SATD,... between the
* input block and the reconstructed block and rate is the number of bits taken
* to place the macroblock in the bit-stream. In this routine the rate does not
* exactly point to the total number of bits it takes, rather it points to header
* bits necessary for encoding the macroblock. Assuming the deltaQP, cbp bits
* and residual bits fall in to texture bits the number of bits taken to encoding
* mbtype is considered as rate, we compute cost. Further we will approximate
* the distortion as the deviation b/w input and the predicted block as opposed
* to input and reconstructed block.
*
* NOTE: TODO: This function needs to be tested
*
* @return none
*
******************************************************************************
*/
void ih264e_evaluate_intra8x8_modes_for_least_cost_rdoptoff(process_ctxt_t *ps_proc)
{
/* Codec Context */
codec_t *ps_codec = ps_proc->ps_codec;
/* SAD(distortion metric) of an 4x4 block */
WORD32 i4_partition_distortion, i4_partition_distortion_least = INT_MAX, i4_total_distortion = 0;
/* lambda */
UWORD32 u4_lambda = ps_proc->u4_lambda;
/* cost = distortion + lambda*rate */
WORD32 i4_partition_cost, i4_partition_cost_least, i4_total_cost = u4_lambda;
/* cost due to mbtype */
UWORD32 u4_cost_one_bit = u4_lambda, u4_cost_four_bits = 4 * u4_lambda;
/* intra mode */
UWORD32 u4_intra_mode, u4_best_intra_8x8_mode = DC_I8x8, u4_estimated_intra_8x8_mode;
/* neighbor pels for intra prediction */
UWORD8 *pu1_ngbr_pels_i8 = ps_proc->au1_ngbr_pels;
/* pointer to curr partition */
UWORD8 *pu1_mb_curr;
/* pointer to prediction macro block */
UWORD8 *pu1_pred_mb = ps_proc->pu1_pred_mb;
/* strides */
WORD32 i4_src_strd = ps_proc->i4_src_strd;
WORD32 i4_pred_strd = ps_proc->i4_pred_strd;
/* neighbors left, top, top right, top left */
UWORD8 *pu1_mb_a;
UWORD8 *pu1_mb_b;
UWORD8 *pu1_mb_d;
/* neighbor availability */
WORD32 i4_ngbr_avbl;
block_neighbors_t s_ngbr_avbl;
/* temp vars */
UWORD32 b8, u4_pix_x, u4_pix_y;
UWORD32 u4_constrained_intra_pred = ps_proc->ps_codec->s_cfg.u4_constrained_intra_pred;
block_neighbors_t s_ngbr_avbl_MB;
/* ngbr mb syntax information */
UWORD8 *pu1_top_mb_intra_modes = ps_proc->pu1_top_mb_intra_modes + (ps_proc->i4_mb_x << 4);
mb_info_t *ps_top_mb_syn_ele = ps_proc->ps_top_row_mb_syntax_ele + ps_proc->i4_mb_x;
mb_info_t *ps_top_right_mb_syn_ele = ps_proc->ps_top_row_mb_syntax_ele + ps_proc->i4_mb_x;
/* valid intra modes map */
UWORD32 u4_valid_intra_modes;
if (ps_proc->ps_ngbr_avbl->u1_mb_c)
{
ps_top_right_mb_syn_ele = ps_proc->ps_top_row_mb_syntax_ele + (ps_proc->i4_mb_x + 1);
}
/* left pels */
s_ngbr_avbl_MB.u1_mb_a = ((ps_proc->ps_ngbr_avbl->u1_mb_a)
&& (u4_constrained_intra_pred ? ps_proc->s_left_mb_syntax_ele.u2_is_intra : 1));
/* top pels */
s_ngbr_avbl_MB.u1_mb_b = ((ps_proc->ps_ngbr_avbl->u1_mb_b)
&& (u4_constrained_intra_pred ? ps_top_mb_syn_ele->u2_is_intra : 1));
/* topleft pels */
s_ngbr_avbl_MB.u1_mb_d = ((ps_proc->ps_ngbr_avbl->u1_mb_d)
&& (u4_constrained_intra_pred ? ps_proc->s_top_left_mb_syntax_ele.u2_is_intra : 1));
/* top right */
s_ngbr_avbl_MB.u1_mb_c = ((ps_proc->ps_ngbr_avbl->u1_mb_c)
&& (u4_constrained_intra_pred ? ps_top_right_mb_syn_ele->u2_is_intra : 1));
for (b8 = 0; b8 < 4; b8++)
{
u4_pix_x = (b8 & 0x01) << 3;
u4_pix_y = (b8 >> 1) << 3;
pu1_mb_curr = ps_proc->pu1_src_buf_luma + u4_pix_x + (u4_pix_y * i4_src_strd);
/* when rdopt is off, we use the input as reference for constructing prediction buffer */
/* as opposed to using the recon pels. (open loop intra prediction) */
pu1_mb_a = pu1_mb_curr - 1; /* pointer to left macro block */
pu1_mb_b = pu1_mb_curr - i4_src_strd; /* pointer to top macro block */
pu1_mb_d = pu1_mb_b - 1; /* pointer to top left macro block */
/* locating neighbors that are available for prediction */
/* TODO : update the neighbor availability information basing on constrained intra pred information */
/* TODO : i4_ngbr_avbl is only being used in DC mode. Can the DC mode be split in to distinct routines */
/* basing on neighbors available and hence evade the computation of neighbor availability totally. */
s_ngbr_avbl.u1_mb_a = ih264e_derive_ngbr_avbl_of_mb_partitions(&s_ngbr_avbl_MB, u4_pix_x - 1, u4_pix_y); /* xD = -1, yD = 0 */
s_ngbr_avbl.u1_mb_b = ih264e_derive_ngbr_avbl_of_mb_partitions(&s_ngbr_avbl_MB, u4_pix_x, u4_pix_y - 1); /* xD = 0, yD = -1 */
s_ngbr_avbl.u1_mb_c = ih264e_derive_ngbr_avbl_of_mb_partitions(&s_ngbr_avbl_MB, u4_pix_x + 8, u4_pix_y - 1); /* xD = BLK_8x8_SIZE, yD = -1 */
s_ngbr_avbl.u1_mb_d = ih264e_derive_ngbr_avbl_of_mb_partitions(&s_ngbr_avbl_MB, u4_pix_x - 1, u4_pix_y - 1); /* xD = -1, yD = -1 */
/* i4_ngbr_avbl = blk_a * LEFT_MB_AVAILABLE_MASK + blk_b * TOP_MB_AVAILABLE_MASK + blk_c * TOP_RIGHT_MB_AVAILABLE_MASK + blk_d * TOP_LEFT_MB_AVAILABLE_MASK */
i4_ngbr_avbl = (s_ngbr_avbl.u1_mb_a) + (s_ngbr_avbl.u1_mb_d << 1) + (s_ngbr_avbl.u1_mb_b << 2) + (s_ngbr_avbl.u1_mb_c << 3) +
(s_ngbr_avbl.u1_mb_a << 4);
/* if top partition is available and top right is not available for intra prediction, then */
/* padd top right samples using top sample and make top right also available */
/* i4_ngbr_avbl = (s_ngbr_avbl.u1_mb_a) + (s_ngbr_avbl.u1_mb_d << 1) + (s_ngbr_avbl.u1_mb_b << 2) + ((s_ngbr_avbl.u1_mb_b | s_ngbr_avbl.u1_mb_c) << 3); */
ps_proc->ai4_neighbor_avail_8x8_subblks[b8] = i4_ngbr_avbl;
ih264_intra_pred_luma_8x8_mode_ref_filtering(pu1_mb_a, pu1_mb_b, pu1_mb_d, pu1_ngbr_pels_i8,
i4_src_strd, i4_ngbr_avbl);
i4_partition_cost_least = INT_MAX;
/* set valid intra modes for evaluation */
u4_valid_intra_modes = 0x1ff;
if (!s_ngbr_avbl.u1_mb_b)
{
u4_valid_intra_modes &= ~(1 << VERT_I4x4);
u4_valid_intra_modes &= ~(1 << DIAG_DL_I4x4);
u4_valid_intra_modes &= ~(1 << VERT_L_I4x4);
}
if (!s_ngbr_avbl.u1_mb_a)
{
u4_valid_intra_modes &= ~(1 << HORZ_I4x4);
u4_valid_intra_modes &= ~(1 << HORZ_U_I4x4);
}
if (!s_ngbr_avbl.u1_mb_a || !s_ngbr_avbl.u1_mb_b || !s_ngbr_avbl.u1_mb_d)
{
u4_valid_intra_modes &= ~(1 << DIAG_DR_I4x4);
u4_valid_intra_modes &= ~(1 << VERT_R_I4x4);
u4_valid_intra_modes &= ~(1 << HORZ_D_I4x4);
}
/* estimate the intra 8x8 mode for the current partition (for evaluating cost) */
if (!s_ngbr_avbl.u1_mb_a || !s_ngbr_avbl.u1_mb_b)
{
u4_estimated_intra_8x8_mode = DC_I8x8;
}
else
{
UWORD32 u4_left_intra_8x8_mode = DC_I8x8;
UWORD32 u4_top_intra_8x8_mode = DC_I8x8;
if (u4_pix_x == 0)
{
if (ps_proc->s_left_mb_syntax_ele.u2_mb_type == I8x8)
{
u4_left_intra_8x8_mode = ps_proc->au1_left_mb_intra_modes[b8+1];
}
else if (ps_proc->s_left_mb_syntax_ele.u2_mb_type == I4x4)
{
u4_left_intra_8x8_mode = ps_proc->au1_left_mb_intra_modes[(b8+1)*4+2];
}
}
else
{
u4_left_intra_8x8_mode = ps_proc->au1_intra_luma_mb_8x8_modes[b8-1];
}
if (u4_pix_y == 0)
{
if (ps_top_mb_syn_ele->u2_mb_type == I8x8)
{
u4_top_intra_8x8_mode = pu1_top_mb_intra_modes[b8+2];
}
else if (ps_top_mb_syn_ele->u2_mb_type == I4x4)
{
u4_top_intra_8x8_mode = pu1_top_mb_intra_modes[(b8+2)*4+2];
}
}
else
{
u4_top_intra_8x8_mode = ps_proc->au1_intra_luma_mb_8x8_modes[b8-2];
}
u4_estimated_intra_8x8_mode = MIN(u4_left_intra_8x8_mode, u4_top_intra_8x8_mode);
}
/* perform intra mode 8x8 evaluation */
for (u4_intra_mode = VERT_I8x8; u4_valid_intra_modes != 0; u4_intra_mode++, u4_valid_intra_modes >>= 1)
{
if ( (u4_valid_intra_modes & 1) == 0)
continue;
/* intra prediction */
(ps_codec->apf_intra_pred_8_l)[u4_intra_mode](pu1_ngbr_pels_i8, pu1_pred_mb, 0, i4_pred_strd, i4_ngbr_avbl);
/* evaluate distortion between the actual blk and the estimated blk for the given mode */
ime_compute_sad_8x8(pu1_mb_curr, pu1_pred_mb, i4_src_strd, i4_pred_strd, i4_partition_cost_least, &i4_partition_distortion);
i4_partition_cost = i4_partition_distortion + ((u4_estimated_intra_8x8_mode == u4_intra_mode)?u4_cost_one_bit:u4_cost_four_bits);
/* update the least cost information if necessary */
if (i4_partition_cost < i4_partition_cost_least)
{
i4_partition_cost_least = i4_partition_cost;
i4_partition_distortion_least = i4_partition_distortion;
u4_best_intra_8x8_mode = u4_intra_mode;
}
}
/* macroblock distortion */
i4_total_cost += i4_partition_cost_least;
i4_total_distortion += i4_partition_distortion_least;
/* mb partition mode */
ps_proc->au1_intra_luma_mb_8x8_modes[b8] = u4_best_intra_8x8_mode;
}
/* update the type of the mb if necessary */
if (i4_total_cost < ps_proc->i4_mb_cost)
{
ps_proc->i4_mb_cost = i4_total_cost;
ps_proc->i4_mb_distortion = i4_total_distortion;
ps_proc->u4_mb_type = I8x8;
}
if (i4_total_cost < ps_proc->i4_mb_intra_cost)
{
ps_proc->i4_mb_intra_cost = i4_total_cost;
}
return ;
}
/**
******************************************************************************
*
* @brief
* evaluate best intra 4x4 mode (rate distortion opt off)
*
* @par Description
* This function evaluates all the possible intra 4x4 modes and finds the mode
* that best represents the macro-block (least distortion) and occupies fewer
* bits in the bit-stream.
*
* @param[in] ps_proc_ctxt
* pointer to proc ctxt
*
* @remarks
* Ideally the cost of encoding a macroblock is calculated as
* (distortion + lambda*rate). Where distortion is SAD/SATD,... between the
* input block and the reconstructed block and rate is the number of bits taken
* to place the macroblock in the bit-stream. In this routine the rate does not
* exactly point to the total number of bits it takes, rather it points to header
* bits necessary for encoding the macroblock. Assuming the deltaQP, cbp bits
* and residual bits fall in to texture bits the number of bits taken to encoding
* mbtype is considered as rate, we compute cost. Further we will approximate
* the distortion as the deviation b/w input and the predicted block as opposed
* to input and reconstructed block.
*
* NOTE: As per the Document JVT-O079, for the whole intra 4x4 macroblock,
* 24*lambda is added to the SAD before comparison with the best SAD for
* inter prediction. This is an empirical value to prevent using too many intra
* blocks.
*
* @return none
*
******************************************************************************
*/
void ih264e_evaluate_intra4x4_modes_for_least_cost_rdoptoff(process_ctxt_t *ps_proc)
{
/* Codec Context */
codec_t *ps_codec = ps_proc->ps_codec;
/* SAD(distortion metric) of an 4x4 block */
WORD32 i4_partition_distortion_least = INT_MAX, i4_total_distortion = 0;
/* lambda */
UWORD32 u4_lambda = ps_proc->u4_lambda;
/* cost = distortion + lambda*rate */
WORD32 i4_partition_cost_least, i4_total_cost = (24 + 1) * u4_lambda;
/* cost due to mbtype */
UWORD32 u4_cost_one_bit = u4_lambda, u4_cost_four_bits = 4 * u4_lambda;
/* intra mode */
UWORD32 u4_best_intra_4x4_mode = DC_I4x4, u4_estimated_intra_4x4_mode;
/* neighbor pels for intra prediction */
UWORD8 *pu1_ngbr_pels_i4 = ps_proc->au1_ngbr_pels;
/* pointer to curr partition */
UWORD8 *pu1_mb_curr;
/* pointer to prediction macro block */
UWORD8 *pu1_pred_mb = ps_proc->pu1_pred_mb;
/* strides */
WORD32 i4_src_strd = ps_proc->i4_src_strd;
WORD32 i4_pred_strd = ps_proc->i4_pred_strd;
/* neighbors left, top, top right, top left */
UWORD8 *pu1_mb_a;
UWORD8 *pu1_mb_b;
UWORD8 *pu1_mb_c;
UWORD8 *pu1_mb_d;
/* neighbor availability */
WORD32 i4_ngbr_avbl;
block_neighbors_t s_ngbr_avbl;
/* temp vars */
UWORD32 i, b8, b4, u4_blk_x, u4_blk_y, u4_pix_x, u4_pix_y;
/* scan order inside 4x4 block */
const UWORD8 u1_scan_order[16] = {0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15};
/* ngbr sub mb modes */
UWORD8 *pu1_top_mb_intra_modes = ps_proc->pu1_top_mb_intra_modes + (ps_proc->i4_mb_x << 4);
mb_info_t *ps_top_mb_syn_ele = ps_proc->ps_top_row_mb_syntax_ele + ps_proc->i4_mb_x;
mb_info_t *ps_top_right_mb_syn_ele = ps_proc->ps_top_row_mb_syntax_ele + ps_proc->i4_mb_x;
/* valid intra modes map */
UWORD32 u4_valid_intra_modes;
UWORD16 u2_valid_modes[8] = {4, 262, 4, 262, 141, 399, 141, 511};
UWORD32 u4_constrained_intra_pred = ps_proc->ps_codec->s_cfg.u4_constrained_intra_pred;
UWORD8 u1_mb_a, u1_mb_b, u1_mb_c, u1_mb_d;
if (ps_proc->ps_ngbr_avbl->u1_mb_c)
{
ps_top_right_mb_syn_ele = ps_proc->ps_top_row_mb_syntax_ele + ps_proc->i4_mb_x + 1;
}
/* left pels */
u1_mb_a = ((ps_proc->ps_ngbr_avbl->u1_mb_a)
&& (u4_constrained_intra_pred ? ps_proc->s_left_mb_syntax_ele.u2_is_intra : 1));
/* top pels */
u1_mb_b = ((ps_proc->ps_ngbr_avbl->u1_mb_b)
&& (u4_constrained_intra_pred ? ps_top_mb_syn_ele->u2_is_intra : 1));
/* topleft pels */
u1_mb_d = ((ps_proc->ps_ngbr_avbl->u1_mb_d)
&& (u4_constrained_intra_pred ? ps_proc->s_top_left_mb_syntax_ele.u2_is_intra : 1));
/* top right */
u1_mb_c = ((ps_proc->ps_ngbr_avbl->u1_mb_c)
&& (u4_constrained_intra_pred ? ps_top_right_mb_syn_ele->u2_is_intra : 1));
i4_ngbr_avbl = (u1_mb_a) + (u1_mb_d << 1) + (u1_mb_b << 2) + (u1_mb_c << 3);
memcpy(ps_proc->au1_ngbr_avbl_4x4_subblks, gau1_ih264_4x4_ngbr_avbl[i4_ngbr_avbl], 16);
for (b8 = 0; b8 < 4; b8++)
{
u4_blk_x = (b8 & 0x01) << 3;
u4_blk_y = (b8 >> 1) << 3;
for (b4 = 0; b4 < 4; b4++)
{
u4_pix_x = u4_blk_x + ((b4 & 0x01) << 2);
u4_pix_y = u4_blk_y + ((b4 >> 1) << 2);
pu1_mb_curr = ps_proc->pu1_src_buf_luma + u4_pix_x + (u4_pix_y * i4_src_strd);
/* when rdopt is off, we use the input as reference for constructing prediction buffer */
/* as opposed to using the recon pels. (open loop intra prediction) */
pu1_mb_a = pu1_mb_curr - 1; /* pointer to left macro block */
pu1_mb_b = pu1_mb_curr - i4_src_strd; /* pointer to top macro block */
pu1_mb_c = pu1_mb_b + 4; /* pointer to top macro block */
pu1_mb_d = pu1_mb_b - 1; /* pointer to top left macro block */
/* locating neighbors that are available for prediction */
/* TODO : update the neighbor availability information basing on constrained intra pred information */
/* TODO : i4_ngbr_avbl is only being used in DC mode. Can the DC mode be split in to distinct routines */
/* basing on neighbors available and hence evade the computation of neighbor availability totally. */
i4_ngbr_avbl = ps_proc->au1_ngbr_avbl_4x4_subblks[(b8 << 2) + b4];
s_ngbr_avbl.u1_mb_a = (i4_ngbr_avbl & 0x1);
s_ngbr_avbl.u1_mb_d = (i4_ngbr_avbl & 0x2) >> 1;
s_ngbr_avbl.u1_mb_b = (i4_ngbr_avbl & 0x4) >> 2;
s_ngbr_avbl.u1_mb_c = (i4_ngbr_avbl & 0x8) >> 3;
/* set valid intra modes for evaluation */
u4_valid_intra_modes = u2_valid_modes[i4_ngbr_avbl & 0x7];
/* if top partition is available and top right is not available for intra prediction, then */
/* padd top right samples using top sample and make top right also available */
/* i4_ngbr_avbl = (s_ngbr_avbl.u1_mb_a) + (s_ngbr_avbl.u1_mb_d << 1) + (s_ngbr_avbl.u1_mb_b << 2) + ((s_ngbr_avbl.u1_mb_b | s_ngbr_avbl.u1_mb_c) << 3); */
/* gather prediction pels from the neighbors */
if (s_ngbr_avbl.u1_mb_a)
{
for(i = 0; i < 4; i++)
pu1_ngbr_pels_i4[4 - 1 -i] = pu1_mb_a[i * i4_src_strd];
}
else
{
memset(pu1_ngbr_pels_i4, 0, 4);
}
if (s_ngbr_avbl.u1_mb_b)
{
memcpy(pu1_ngbr_pels_i4 + 4 + 1, pu1_mb_b, 4);
}
else
{
memset(pu1_ngbr_pels_i4 + 5, 0, 4);
}
if (s_ngbr_avbl.u1_mb_d)
pu1_ngbr_pels_i4[4] = *pu1_mb_d;
else
pu1_ngbr_pels_i4[4] = 0;
if (s_ngbr_avbl.u1_mb_c)
{
memcpy(pu1_ngbr_pels_i4 + 8 + 1, pu1_mb_c, 4);
}
else if (s_ngbr_avbl.u1_mb_b)
{
memset(pu1_ngbr_pels_i4 + 8 + 1, pu1_ngbr_pels_i4[8], 4);
s_ngbr_avbl.u1_mb_c = s_ngbr_avbl.u1_mb_b;
}
i4_partition_cost_least = INT_MAX;
/* predict the intra 4x4 mode for the current partition (for evaluating cost) */
if (!s_ngbr_avbl.u1_mb_a || !s_ngbr_avbl.u1_mb_b)
{
u4_estimated_intra_4x4_mode = DC_I4x4;
}
else
{
UWORD32 u4_left_intra_4x4_mode = DC_I4x4;
UWORD32 u4_top_intra_4x4_mode = DC_I4x4;
if (u4_pix_x == 0)
{
if (ps_proc->s_left_mb_syntax_ele.u2_mb_type == I4x4)
{
u4_left_intra_4x4_mode = ps_proc->au1_left_mb_intra_modes[u1_scan_order[3 + u4_pix_y]];
}
else if (ps_proc->s_left_mb_syntax_ele.u2_mb_type == I8x8)
{
u4_left_intra_4x4_mode = ps_proc->au1_left_mb_intra_modes[b8 + 1];
}
}
else
{
u4_left_intra_4x4_mode = ps_proc->au1_intra_luma_mb_4x4_modes[u1_scan_order[(u4_pix_x >> 2) + u4_pix_y - 1]];
}
if (u4_pix_y == 0)
{
if (ps_top_mb_syn_ele->u2_mb_type == I4x4)
{
u4_top_intra_4x4_mode = pu1_top_mb_intra_modes[u1_scan_order[12 + (u4_pix_x >> 2)]];
}
else if (ps_top_mb_syn_ele->u2_mb_type == I8x8)
{
u4_top_intra_4x4_mode = pu1_top_mb_intra_modes[b8 + 2];
}
}
else
{
u4_top_intra_4x4_mode = ps_proc->au1_intra_luma_mb_4x4_modes[u1_scan_order[(u4_pix_x >> 2) + u4_pix_y - 4]];
}
u4_estimated_intra_4x4_mode = MIN(u4_left_intra_4x4_mode, u4_top_intra_4x4_mode);
}
ps_proc->au1_predicted_intra_luma_mb_4x4_modes[(b8 << 2) + b4] = u4_estimated_intra_4x4_mode;
/* mode evaluation and prediction */
ps_codec->pf_ih264e_evaluate_intra_4x4_modes(pu1_mb_curr,
pu1_ngbr_pels_i4,
pu1_pred_mb, i4_src_strd,
i4_pred_strd, i4_ngbr_avbl,
&u4_best_intra_4x4_mode,
&i4_partition_cost_least,
u4_valid_intra_modes,
u4_lambda,
u4_estimated_intra_4x4_mode);
i4_partition_distortion_least = i4_partition_cost_least - ((u4_estimated_intra_4x4_mode == u4_best_intra_4x4_mode) ? u4_cost_one_bit : u4_cost_four_bits);
DEBUG("%d partition cost, %d intra mode\n", i4_partition_cost_least, u4_best_intra_4x4_mode);
/* macroblock distortion */