aboutsummaryrefslogtreecommitdiff
path: root/encoder/rc_rd_model.c
blob: a9165fb8eccc9d226858a51e052972e7f57b90cf (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/******************************************************************************
 *
 * Copyright (C) 2018 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 Name         : rc_rd_model.c                                        */
/*                                                                          */
/* Description       : Implall the Functions to Model the                   */
/*                     Rate Distortion Behaviour of the Codec over the Last */
/*                     Few Frames.                                          */
/*                                                                          */
/* List of Functions : update_frame_rd_model                                */
/*                     estimate_mpeg2_qp_for_resbits                        */
/*                                                                          */
/* Issues / Problems : None                                                 */
/*                                                                          */
/* Revision History  :                                                      */
/*        DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*        21 06 2006   ittiam           Initial Version                      */
/****************************************************************************/

/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* System include files */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/* System include files */
#include "ittiam_datatypes.h"
#include "rc_common.h"
#include "var_q_operator.h"
#include "mem_req_and_acq.h"
#include "rc_rd_model.h"
#include "rc_rd_model_struct.h"

#if !(RC_FIXED_POINT)

#if NON_STEADSTATE_CODE
WORD32 rc_rd_model_num_fill_use_free_memtab(
    rc_rd_model_t **pps_rc_rd_model, itt_memtab_t *ps_memtab, ITT_FUNC_TYPE_E e_func_type)
{
    WORD32 i4_mem_tab_idx = 0;
    static rc_rd_model_t s_rc_rd_model_temp;

    /* Hack for al alloc, during which we dont have any state memory.
      Dereferencing can cause issues */
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
        (*pps_rc_rd_model) = &s_rc_rd_model_temp;

    /*for src rate control state structure*/
    if(e_func_type != GET_NUM_MEMTAB)
    {
        fill_memtab(
            &ps_memtab[i4_mem_tab_idx], sizeof(rc_rd_model_t), MEM_TAB_ALIGNMENT, PERSISTENT, DDR);
        use_or_fill_base(&ps_memtab[0], (void **)pps_rc_rd_model, e_func_type);
    }
    i4_mem_tab_idx++;

    return (i4_mem_tab_idx);
}

void init_frm_rc_rd_model(rc_rd_model_t *ps_rd_model, UWORD8 u1_max_frames_modelled)
{
    /*ps_rd_model = ps_rd_model + u1_pic_type;*/

    ps_rd_model->u1_num_frms_in_model = 0;
    ps_rd_model->u1_curr_frm_counter = 0;
    ps_rd_model->u1_max_frms_to_model = u1_max_frames_modelled;
    /*
    ps_rd_model->u1_min_frames_for_quad_model = u1_min_frames_for_quad_model;
    ps_rd_model->u1_min_frames_for_lin_model  = u1_min_frames_for_lin_model;
    */

    ps_rd_model->model_coeff_a_quad = 0;
    ps_rd_model->model_coeff_b_quad = 0;
    ps_rd_model->model_coeff_c_quad = 0;

    ps_rd_model->model_coeff_a_lin = 0;
    ps_rd_model->model_coeff_b_lin = 0;
    ps_rd_model->model_coeff_c_lin = 0;

    ps_rd_model->model_coeff_a_lin_wo_int = 0;
    ps_rd_model->model_coeff_b_lin_wo_int = 0;
    ps_rd_model->model_coeff_c_lin_wo_int = 0;
}

void reset_frm_rc_rd_model(rc_rd_model_t *ps_rd_model)
{
    /*ps_rd_model = ps_rd_model + u1_pic_type;*/

    ps_rd_model->u1_num_frms_in_model = 0;
    ps_rd_model->u1_curr_frm_counter = 0;
    ps_rd_model->model_coeff_a_quad = 0;
    ps_rd_model->model_coeff_b_quad = 0;
    ps_rd_model->model_coeff_c_quad = 0;

    ps_rd_model->model_coeff_a_lin = 0;
    ps_rd_model->model_coeff_b_lin = 0;
    ps_rd_model->model_coeff_c_lin = 0;

    ps_rd_model->model_coeff_a_lin_wo_int = 0;
    ps_rd_model->model_coeff_b_lin_wo_int = 0;
    ps_rd_model->model_coeff_c_lin_wo_int = 0;
}
#endif /* #if NON_STEADSTATE_CODE */

#if ENABLE_QUAD_MODEL
static UWORD8 find_model_coeffs(
    UWORD32 *pi4_res_bits,
    UWORD32 *pi4_sad_h264,
    UWORD8 *pu1_num_skips,
    UWORD8 *pui_avg_mpeg2_qp,
    UWORD8 u1_num_frms,
    UWORD8 u1_model_used,
    WORD8 *pi1_frame_index,
    model_coeff *pmc_model_coeff,
    model_coeff *pmc_model_coeff_lin,
    model_coeff *pmc_model_coeff_lin_wo_int,
    rc_rd_model_t *ps_rd_model)
{
    UWORD32 i;
    UWORD8 u1_num_frms_used = 0;
    UWORD8 u1_frm_indx;

    float sum_y = 0;
    float sum_x_y = 0;
    float sum_x2_y = 0;
    float sum_x = 0;
    float sum_x2 = 0;
    float sum_x3 = 0;
    float sum_x4 = 0;
    float var_x2_y = 0;
    float var_x_y = 0;
    float var_x2_x = 0;
    float var_x2_x2 = 0;
    float var_x_x = 0;
    float x0, y0;
    float model_coeff_a, model_coeff_b, model_coeff_c, model_coeff_den;

    for(i = 0; i < u1_num_frms; i++)
    {
        if(-1 == pi1_frame_index[i])
            continue;

        u1_frm_indx = (UWORD8)pi1_frame_index[i];

        y0 = (float)(pi4_res_bits[u1_frm_indx]);
        x0 = (float)(pi4_sad_h264[u1_frm_indx] / (float)pui_avg_mpeg2_qp[u1_frm_indx]);

        sum_y += y0;
        sum_x_y += x0 * y0;
        sum_x2_y += x0 * x0 * y0;
        sum_x += x0;
        sum_x2 += x0 * x0;
        sum_x3 += x0 * x0 * x0;
        sum_x4 += x0 * x0 * x0 * x0;
        u1_num_frms_used++;
    }

    sum_y /= u1_num_frms_used;
    sum_x_y /= u1_num_frms_used;
    sum_x2_y /= u1_num_frms_used;
    sum_x /= u1_num_frms_used;
    sum_x2 /= u1_num_frms_used;
    sum_x3 /= u1_num_frms_used;
    sum_x4 /= u1_num_frms_used;

#if !QUAD
    u1_model_used = LIN_MODEL;
#endif

    if((QUAD_MODEL == u1_model_used) && (u1_num_frms_used <= MIN_FRAMES_FOR_QUAD_MODEL))
    {
        u1_model_used = LIN_MODEL;
    }

    if(QUAD_MODEL == u1_model_used)
    {
        var_x2_y = sum_x2_y - sum_x2 * sum_y;
        var_x_y = sum_x_y - sum_x * sum_y;
        var_x2_x = sum_x3 - sum_x2 * sum_x;
        var_x2_x2 = sum_x4 - sum_x2 * sum_x2;
        var_x_x = sum_x2 - sum_x * sum_x;

        model_coeff_den = (var_x2_x * var_x2_x - var_x2_x2 * var_x_x);

        if(0 != model_coeff_den)
        {
            model_coeff_b = (var_x_y * var_x2_x - var_x2_y * var_x_x);
            model_coeff_b /= model_coeff_den;

            model_coeff_a = (var_x2_y * var_x2_x - var_x_y * var_x2_x2);
            model_coeff_a /= model_coeff_den;

            model_coeff_c = sum_y - (model_coeff_a * sum_x) - (model_coeff_b * sum_x2);
        }

        pmc_model_coeff[0] = model_coeff_b;
        pmc_model_coeff[1] = model_coeff_a;
        pmc_model_coeff[2] = model_coeff_c;
    }

    if(NULL != pmc_model_coeff_lin)
    {
        var_x_y = sum_x_y - sum_x * sum_y;
        var_x_x = sum_x2 - sum_x * sum_x;

        if(0 != var_x_x)
        {
            model_coeff_a = (var_x_y / var_x_x);
            model_coeff_c = sum_y - (model_coeff_a * sum_x);
            /*model_coeff_b = 0;*/
            model_coeff_b = model_coeff_a;

            pmc_model_coeff_lin[0] = model_coeff_b;
            pmc_model_coeff_lin[1] = model_coeff_a;
            pmc_model_coeff_lin[2] = model_coeff_c;
        }
    }

    if(NULL != pmc_model_coeff_lin_wo_int)
    {
        UWORD8 u1_curr_frame_index;
        UWORD8 u1_avgqp_prvfrm;
        UWORD32 u4_prevfrm_bits, u4_prevfrm_sad;

        u1_curr_frame_index = ps_rd_model->u1_curr_frm_counter;
        if(0 == u1_curr_frame_index)
            u1_curr_frame_index = (MAX_FRAMES_MODELLED - 1);
        else
            u1_curr_frame_index--;

        u1_avgqp_prvfrm = ps_rd_model->pu1_avg_qp[u1_curr_frame_index];
        u4_prevfrm_bits = ps_rd_model->pi4_res_bits[u1_curr_frame_index];
        u4_prevfrm_sad = ps_rd_model->pi4_sad[u1_curr_frame_index];

        if(0 != u4_prevfrm_sad)
            model_coeff_a = (float)(u4_prevfrm_bits * u1_avgqp_prvfrm) / u4_prevfrm_sad;
        else
            model_coeff_a = 0;

        model_coeff_b = 0;
        model_coeff_c = 0;

        pmc_model_coeff_lin_wo_int[0] = model_coeff_b;
        pmc_model_coeff_lin_wo_int[1] = model_coeff_a;
        pmc_model_coeff_lin_wo_int[2] = model_coeff_c;
    }

    return u1_model_used;
}

static WORD8 refine_set_of_points(
    UWORD32 *pi4_res_bits,
    UWORD32 *pi4_sad_h264,
    UWORD8 *pu1_num_skips,
    UWORD8 *pui_avg_mpeg2_qp,
    UWORD8 u1_num_frms,
    WORD8 *pi1_frame_index,
    model_coeff *pmc_model_coeff,
    float *pfl_avg_deviation)
{
    float fl_avg_deviation, fl_estimated_bits, fl_deviation, x_val;
    UWORD8 u1_return_value = 1;
    UWORD32 i;
    UWORD8 u1_num_frms_used, u1_frm_indx;

    u1_num_frms_used = 0;
    fl_avg_deviation = 0;
    for(i = 0; i < u1_num_frms; i++)
    {
        if(-1 == pi1_frame_index[i])
            continue;

        u1_frm_indx = (UWORD8)pi1_frame_index[i];
        x_val = pi4_sad_h264[u1_frm_indx] / (float)pui_avg_mpeg2_qp[u1_frm_indx];

        fl_estimated_bits = (pmc_model_coeff[0] * x_val * x_val) + (pmc_model_coeff[1] * x_val) +
                            (pmc_model_coeff[2]);

        fl_deviation =
            fabs(pi4_res_bits[u1_frm_indx] - fl_estimated_bits) / (float)pi4_res_bits[u1_frm_indx];
        fl_deviation = fl_deviation * fl_deviation;
        fl_avg_deviation += fl_deviation;
        u1_num_frms_used++;
    }

    fl_avg_deviation /= u1_num_frms_used;
    /*fl_avg_deviation = sqrt(fl_avg_deviation);*/
    fl_avg_deviation = (fl_avg_deviation);

    for(i = 0; i < u1_num_frms; i++)
    {
        if((-1 == pi1_frame_index[i]) && (i != 0))
            continue;

        u1_frm_indx = (UWORD8)pi1_frame_index[i];

        x_val = pi4_sad_h264[u1_frm_indx] / (float)pui_avg_mpeg2_qp[u1_frm_indx];

        fl_estimated_bits = (pmc_model_coeff[0] * x_val * x_val) + (pmc_model_coeff[1] * x_val) +
                            (pmc_model_coeff[2]);

        fl_deviation =
            fabs(pi4_res_bits[u1_frm_indx] - fl_estimated_bits) / (float)pi4_res_bits[u1_frm_indx];

        fl_deviation = fl_deviation * fl_deviation;

        if(fl_deviation > (fl_avg_deviation))
        {
            pi1_frame_index[i] = -1;
        }
    }

    if(fl_avg_deviation > 0.0625)
        u1_return_value = 0;
    if(fl_avg_deviation < 0.0225)
        u1_return_value = 2;

    *pfl_avg_deviation = fl_avg_deviation;

    return (u1_return_value);
}
static void calc_avg_sqr_dev_for_model(
    UWORD32 *pi4_res_bits,
    UWORD32 *pi4_sad_h264,
    UWORD8 *pu1_num_skips,
    UWORD8 *pui_avg_mpeg2_qp,
    UWORD8 u1_num_frms,
    WORD8 *pi1_frame_index,
    model_coeff *pmc_model_coeff,
    float *pfl_avg_deviation)
{
    float fl_avg_deviation, fl_estimated_bits, fl_deviation, x_val;
    UWORD8 u1_return_value = 1;
    UWORD32 i;
    UWORD8 u1_num_frms_used, u1_frm_indx;

    u1_num_frms_used = 0;
    fl_avg_deviation = 0;
    for(i = 0; i < u1_num_frms; i++)
    {
        if(-1 == pi1_frame_index[i])
            continue;

        u1_frm_indx = (UWORD8)pi1_frame_index[i];

        u1_frm_indx = (UWORD8)i;
        x_val = pi4_sad_h264[u1_frm_indx] / (float)pui_avg_mpeg2_qp[u1_frm_indx];

        fl_estimated_bits = (pmc_model_coeff[1] * x_val) + (pmc_model_coeff[2]);

        fl_deviation =
            fabs(pi4_res_bits[u1_frm_indx] - fl_estimated_bits) / (float)pi4_res_bits[u1_frm_indx];
        fl_deviation = fl_deviation * fl_deviation;
        fl_avg_deviation += fl_deviation;
        u1_num_frms_used++;
    }

    fl_avg_deviation /= u1_num_frms_used;
    /*fl_avg_deviation = sqrt(fl_avg_deviation);*/
    fl_avg_deviation = (fl_avg_deviation);

    *pfl_avg_deviation = fl_avg_deviation;
    /*return (u1_return_value);*/
}
static void update_frame_rd_model(rc_rd_model_t *ps_rd_model)
{
    WORD8 pi1_frame_index[MAX_FRAMES_MODELLED], pi1_frame_index_initial[MAX_FRAMES_MODELLED];

    UWORD8 u1_num_skips_temp;
    UWORD8 u1_avg_mpeg2_qp_temp, u1_min_mpeg2_qp, u1_max_mpeg2_qp;
    UWORD8 u1_num_frms_input, u1_num_active_frames, u1_reject_frame;
    UWORD32 u4_num_skips;

    UWORD8 u1_min2_mpeg2_qp, u1_max2_mpeg2_qp;
    UWORD8 u1_min_qp_frame_indx, u1_max_qp_frame_indx;
    UWORD8 pu1_num_frames[MPEG2_QP_ELEM];
    model_coeff model_coeff_array[3], model_coeff_array_lin[3], model_coeff_array_lin_wo_int[3];
    UWORD32 i;
    UWORD8 u1_curr_frame_index;
    UWORD8 u1_quad_model_valid, u1_lin_model_valid;

    float fl_quad_avg_dev, fl_lin_avg_dev;

    UWORD8 u1_check_model;

    /*ps_rd_model += u1_pic_type;*/

    u1_curr_frame_index = ps_rd_model->u1_curr_frm_counter;

    ps_rd_model->u1_model_used = QUAD_MODEL;

    if(0 == u1_curr_frame_index)
        u1_curr_frame_index = (MAX_FRAMES_MODELLED - 1);
    else
        u1_curr_frame_index--;

    /************************************************************************/
    /* Rearrange data to be fed into a Linear Regression Module             */
    /* Module finds a,b,c such that                                         */
    /*      y = ax + bx^2 + c                                               */
    /************************************************************************/
    u4_num_skips = 0;
    u1_num_frms_input = 0;
    memset(pu1_num_frames, 0, MPEG2_QP_ELEM);
    memset(pi1_frame_index, -1, MAX_FRAMES_MODELLED);
    u1_min_mpeg2_qp = MAX_MPEG2_QP;
    u1_max_mpeg2_qp = 0;

    u1_num_active_frames = ps_rd_model->u1_num_frms_in_model;
    if(u1_num_active_frames > MAX_ACTIVE_FRAMES)
        u1_num_active_frames = MAX_ACTIVE_FRAMES;

    /************************************************************************/
    /* Choose the set of Points to be used for MSE fit of Quadratic model   */
    /* Points chosen are spread across the Qp range. Max of 2 points are    */
    /* chosen for a Qp.                                                     */
    /************************************************************************/
    for(i = 0; i < u1_num_active_frames; i++)
    {
        u1_reject_frame = 0;
        u1_num_skips_temp = ps_rd_model->pu1_num_skips[u1_curr_frame_index];
        u1_avg_mpeg2_qp_temp = ps_rd_model->pu1_avg_qp[u1_curr_frame_index];

        if((0 == u4_num_skips) && (0 != u1_num_skips_temp))
            u1_reject_frame = 1;
        if((1 == u4_num_skips) && (u1_num_skips_temp > 1))
            u1_reject_frame = 1;
        if(pu1_num_frames[u1_avg_mpeg2_qp_temp] >= 2)
            u1_reject_frame = 1;

        if(0 == i)
            u1_reject_frame = 0;

        if(0 == u1_reject_frame)
        {
            pi1_frame_index[u1_num_frms_input] = (WORD8)u1_curr_frame_index;
            pu1_num_frames[u1_avg_mpeg2_qp_temp] += 1;

            if(u1_min_mpeg2_qp > u1_avg_mpeg2_qp_temp)
                u1_min_mpeg2_qp = u1_avg_mpeg2_qp_temp;
            if(u1_max_mpeg2_qp < u1_avg_mpeg2_qp_temp)
                u1_max_mpeg2_qp = u1_avg_mpeg2_qp_temp;

            u1_num_frms_input++;
        }

        if(0 == u1_curr_frame_index)
            u1_curr_frame_index = (MAX_FRAMES_MODELLED - 1);
        else
            u1_curr_frame_index--;
    }

    /************************************************************************/
    /* Add Pivot Points to the Data set to be used for finding Quadratic    */
    /* Model Coeffs. These will help in constraining the shape of  Quadratic*/
    /* to adapt too much to the Local deviations.                           */
    /************************************************************************/
    u1_min2_mpeg2_qp = u1_min_mpeg2_qp;
    u1_max2_mpeg2_qp = u1_max_mpeg2_qp;
    u1_min_qp_frame_indx = INVALID_FRAME_INDEX;
    u1_max_qp_frame_indx = INVALID_FRAME_INDEX;

    /* Loop runnning over the Stored Frame Level Data
       to find frames of MinQp and MaxQp */
    for(; i < ps_rd_model->u1_num_frms_in_model; i++)
    {
        u1_num_skips_temp = ps_rd_model->pu1_num_skips[u1_curr_frame_index];
        u1_avg_mpeg2_qp_temp = ps_rd_model->pu1_avg_qp[u1_curr_frame_index];

        if(((0 == u4_num_skips) && (0 != u1_num_skips_temp)) ||
           ((1 == u4_num_skips) && (u1_num_skips_temp > 1)))
            continue;

        if(u1_min2_mpeg2_qp > u1_avg_mpeg2_qp_temp)
        {
            u1_min2_mpeg2_qp = u1_avg_mpeg2_qp_temp;
            u1_min_qp_frame_indx = u1_curr_frame_index;
        }
        if(u1_max2_mpeg2_qp < u1_avg_mpeg2_qp_temp)
        {
            u1_max2_mpeg2_qp = u1_avg_mpeg2_qp_temp;
            u1_max_qp_frame_indx = u1_curr_frame_index;
        }
        if(0 == u1_curr_frame_index)
            u1_curr_frame_index = (MAX_FRAMES_MODELLED - 1);
        else
            u1_curr_frame_index--;
    }

    /* Add the Chosen Points to the regression data set */
    if(INVALID_FRAME_INDEX != u1_min_qp_frame_indx)
    {
        pi1_frame_index[u1_num_frms_input] = (WORD8)u1_min_qp_frame_indx;
        u1_num_frms_input++;
    }
    if(INVALID_FRAME_INDEX != u1_max_qp_frame_indx)
    {
        pi1_frame_index[u1_num_frms_input] = (WORD8)u1_max_qp_frame_indx;
        u1_num_frms_input++;
    }
    memcpy(pi1_frame_index_initial, pi1_frame_index, MAX_FRAMES_MODELLED);

    if(QUAD_MODEL == ps_rd_model->u1_model_used)
    {
        if(u1_num_frms_input < (MIN_FRAMES_FOR_QUAD_MODEL))
            ps_rd_model->u1_model_used = LIN_MODEL;
        if((WORD32)u1_max_mpeg2_qp < ((WORD32)(21 * u1_min_mpeg2_qp) >> 4))
            ps_rd_model->u1_model_used = LIN_MODEL;
    }

    if(LIN_MODEL == ps_rd_model->u1_model_used)
    {
        if(u1_num_frms_input < MIN_FRAMES_FOR_LIN_MODEL)
            ps_rd_model->u1_model_used = PREV_FRAME_MODEL;
        if((WORD32)u1_max_mpeg2_qp < ((WORD32)(19 * u1_min_mpeg2_qp) >> 4))
            ps_rd_model->u1_model_used = PREV_FRAME_MODEL;
    }

    /***** Call the Module to Return the Coeffs for the Fed Data *****/
    ps_rd_model->u1_model_used = find_model_coeffs(
        ps_rd_model->pi4_res_bits,
        ps_rd_model->pi4_sad,
        ps_rd_model->pu1_num_skips,
        ps_rd_model->pu1_avg_qp,
        u1_num_frms_input,
        ps_rd_model->u1_model_used,
        pi1_frame_index,
        model_coeff_array,
        model_coeff_array_lin,
        model_coeff_array_lin_wo_int,
        ps_rd_model);

    if((model_coeff_array_lin[2] > 0) || (model_coeff_array_lin[0] < 0))
        u1_lin_model_valid = 0;
    else
    {
        u1_lin_model_valid = 1;
        /* lin deviation calculation */
        calc_avg_sqr_dev_for_model(
            ps_rd_model->pi4_res_bits,
            ps_rd_model->pi4_sad,
            ps_rd_model->pu1_num_skips,
            ps_rd_model->pu1_avg_qp,
            u1_num_frms_input,
            pi1_frame_index_initial,
            model_coeff_array_lin,
            &fl_lin_avg_dev);
    }

    if(QUAD_MODEL == ps_rd_model->u1_model_used)
    {
        u1_check_model = refine_set_of_points(
            ps_rd_model->pi4_res_bits,
            ps_rd_model->pi4_sad,
            ps_rd_model->pu1_num_skips,
            ps_rd_model->pu1_avg_qp,
            u1_num_frms_input,
            pi1_frame_index,
            model_coeff_array,
            &fl_quad_avg_dev);

        if(2 == u1_check_model)
        {
            ps_rd_model->u1_model_used = QUAD_MODEL;
        }
        else
        {
            /*******************************************************************/
            /* Make sure that some of the Pivot Points are used in the Refined */
            /* data set. 1. Previous Frame                                     */
            /*******************************************************************/
            /*pi1_frame_index[0] = ps_rd_model->u1_curr_frm_counter;*/

            ps_rd_model->u1_model_used = find_model_coeffs(
                ps_rd_model->pi4_res_bits,
                ps_rd_model->pi4_sad,
                ps_rd_model->pu1_num_skips,
                ps_rd_model->pu1_avg_qp,
                u1_num_frms_input,
                ps_rd_model->u1_model_used,
                pi1_frame_index,
                model_coeff_array,
                NULL,
                NULL,
                ps_rd_model);

            u1_check_model = refine_set_of_points(
                ps_rd_model->pi4_res_bits,
                ps_rd_model->pi4_sad,
                ps_rd_model->pu1_num_skips,
                ps_rd_model->pu1_avg_qp,
                u1_num_frms_input,
                pi1_frame_index,
                model_coeff_array,
                &fl_quad_avg_dev);

            if((0 == u1_check_model))
            {
#if RC_MODEL_USED_BUG_FIX
                if((fl_lin_avg_dev < fl_quad_avg_dev) && (1 == u1_lin_model_valid))
#endif
                    ps_rd_model->u1_model_used = LIN_MODEL;
            }
        }
    }

    if(QUAD_MODEL == ps_rd_model->u1_model_used)
    {
        /*min_res_bits = model_coeff_c -
                       ((model_coeff_a * model_coeff_a) / (4 * model_coeff_b));*/

        if(model_coeff_array[0] < 0)
            ps_rd_model->u1_model_used = LIN_MODEL;

        /*if ((model_coeff_a * model_coeff_b) > 0)
             u1_model_used = LIN_MODEL;*/
    }
    if(LIN_MODEL == ps_rd_model->u1_model_used)
    {
        if((model_coeff_array_lin[2] > 0) || (model_coeff_array_lin[0] < 0))
            ps_rd_model->u1_model_used = PREV_FRAME_MODEL;
    }

#if RC_MODEL_USED_BUG_FIX
    /* Another threshold of .25 on deviation i.e. deviation greater than 25%  */
    if((QUAD_MODEL == ps_rd_model->u1_model_used) && (fl_quad_avg_dev > .25))
        ps_rd_model->u1_model_used = PREV_FRAME_MODEL;

    if((LIN_MODEL == ps_rd_model->u1_model_used) && (fl_lin_avg_dev > .25))
        ps_rd_model->u1_model_used = PREV_FRAME_MODEL;
#endif /* #if RC_MODEL_USED_BUG_FIX */

    ps_rd_model->model_coeff_b_quad = model_coeff_array[0];
    ps_rd_model->model_coeff_a_quad = model_coeff_array[1];
    ps_rd_model->model_coeff_c_quad = model_coeff_array[2];

    ps_rd_model->model_coeff_b_lin = model_coeff_array_lin[0];
    ps_rd_model->model_coeff_a_lin = model_coeff_array_lin[1];
    ps_rd_model->model_coeff_c_lin = model_coeff_array_lin[2];

    ps_rd_model->model_coeff_b_lin_wo_int = model_coeff_array_lin_wo_int[0];
    ps_rd_model->model_coeff_a_lin_wo_int = model_coeff_array_lin_wo_int[1];
    ps_rd_model->model_coeff_c_lin_wo_int = model_coeff_array_lin_wo_int[2];

    /*ps_rd_model->u1_model_used = PREV_FRAME_MODEL;*/
}
#endif /* ENABLE_QUAD_MODEL */

UWORD32 estimate_bits_for_qp(rc_rd_model_t *ps_rd_model, UWORD32 u4_estimated_sad, UWORD8 u1_avg_qp)
{
    float fl_num_bits;
    /*ps_rd_model += u1_curr_pic_type;*/

    {
        fl_num_bits =
            ps_rd_model->model_coeff_a_lin_wo_int * ((float)(u4_estimated_sad / u1_avg_qp));
    }

    return ((UWORD32)fl_num_bits);
}

UWORD8 find_qp_for_target_bits(
    rc_rd_model_t *ps_rd_model,
    UWORD32 u4_target_res_bits,
    UWORD32 u4_estimated_sad,
    UWORD8 u1_min_qp,
    UWORD8 u1_max_qp)
{
    UWORD8 u1_qp;
    float x_value, f_qp;
    /*ps_rd_model += u1_curr_pic_type;*/
#if ENABLE_QUAD_MODEL
    if(QUAD_MODEL == ps_rd_model->u1_model_used)
    {
        float det;
        det = (ps_rd_model->model_coeff_a_quad * ps_rd_model->model_coeff_a_quad) -
              (4 * (ps_rd_model->model_coeff_b_quad) *
               (ps_rd_model->model_coeff_c_quad - u4_target_res_bits));

        if(det > 0)
        {
            x_value = sqrt(det);

            x_value =
                (x_value - ps_rd_model->model_coeff_a_quad) / (2 * ps_rd_model->model_coeff_b_quad);
        }
        else
            ps_rd_model->u1_model_used = PREV_FRAME_MODEL;
    }

    if(LIN_MODEL == ps_rd_model->u1_model_used)
    {
        x_value = ((float)u4_target_res_bits - ps_rd_model->model_coeff_c_lin) /
                  (ps_rd_model->model_coeff_b_lin);
    }
#else
    ps_rd_model->u1_model_used = PREV_FRAME_MODEL;
#endif

    if(PREV_FRAME_MODEL == ps_rd_model->u1_model_used)
    {
        x_value = (float)u4_target_res_bits / ps_rd_model->model_coeff_a_lin_wo_int;
    }

    if(0 != x_value)
        f_qp = u4_estimated_sad / x_value;
    else
        f_qp = 255;

    if(f_qp > 255)
        f_qp = 255;

    /* Truncating the QP to the Max and Min Qp values possible */
    if(f_qp < u1_min_qp)
        f_qp = u1_min_qp;
    if(f_qp > u1_max_qp)
        f_qp = u1_max_qp;

    u1_qp = (UWORD8)(f_qp + 0.5);

    return u1_qp;
}

void add_frame_to_rd_model(
    rc_rd_model_t *ps_rd_model,
    UWORD32 i4_res_bits,
    UWORD8 u1_avg_mp2qp,
    UWORD32 i4_sad_h264,
    UWORD8 u1_num_skips)
{
    UWORD8 u1_curr_frame_index;
    /*ps_rd_model += u1_curr_pic_type;*/
    u1_curr_frame_index = ps_rd_model->u1_curr_frm_counter;
    /*** Insert the Present Frame Data into the RD Model State Memory ***/
    ps_rd_model->pi4_res_bits[u1_curr_frame_index] = i4_res_bits;
    ps_rd_model->pi4_sad[u1_curr_frame_index] = i4_sad_h264;
    ps_rd_model->pu1_num_skips[u1_curr_frame_index] = u1_num_skips;
    ps_rd_model->pu1_avg_qp[u1_curr_frame_index] = u1_avg_mp2qp;

    ps_rd_model->u1_curr_frm_counter++;
    if(MAX_FRAMES_MODELLED == ps_rd_model->u1_curr_frm_counter)
        ps_rd_model->u1_curr_frm_counter = 0;

    if(ps_rd_model->u1_num_frms_in_model < ps_rd_model->u1_max_frms_to_model)
    {
        ps_rd_model->u1_num_frms_in_model++;
    }
    update_frame_rd_model(ps_rd_model);
}

WORD32 calc_per_frm_bits(
    rc_rd_model_t *ps_rd_model, /* array of model structs */
    UWORD16 *pu2_num_pics_of_a_pic_type, /* N1, N2,...Nk */
    UWORD8 *
        pu1_update_pic_type_model, /* flag which tells whether or not to update model coefficients of a particular pic-type */
    UWORD8 u1_num_pic_types, /* value of k */
    UWORD32 *
        pu4_num_skip_of_a_pic_type, /* the number of skips of that pic-type. It "may" be used to update the model coefficients at a later point. Right now it is not being used at all. */
    UWORD8 u1_base_pic_type, /* base pic type index wrt which alpha & beta are calculated */
    float *pfl_gamma, /* gamma_i = beta_i / alpha_i */
    float *pfl_eta,
    UWORD8
        u1_curr_pic_type, /* the current pic-type for which the targetted bits need to be computed */
    UWORD32
        u4_bits_for_sub_gop, /* the number of bits to be consumed for the remaining part of sub-gop */
    UWORD32 u4_curr_estimated_sad,
    UWORD8 *pu1_curr_pic_type_qp) /* output of this function */
{
    WORD32 i4_per_frm_bits_Ti;
    UWORD8 u1_i;
    rc_rd_model_t *ps_rd_model_of_pic_type;

    /* first part of this function updates all the model coefficients */
    /*for all the pic-types */
    {
        for(u1_i = 0; u1_i < u1_num_pic_types; u1_i++)
        {
            if((0 != pu2_num_pics_of_a_pic_type[u1_i]) && (1 == pu1_update_pic_type_model[u1_i]))
            {
                /* ps_rd_model_of_pic_type = ps_rd_model + u1_i; */

                update_frame_rd_model(&ps_rd_model[u1_i]);
            }
        }
    }

    /* The second part of this function deals with solving the
    equation using all the pic-types models */

    {
        UWORD8 u1_combined_model_used;

        /* first choose the model to be used */
        u1_combined_model_used = QUAD_MODEL;

        for(u1_i = 0; u1_i < u1_num_pic_types; u1_i++)
        {
            ps_rd_model_of_pic_type = ps_rd_model + u1_i;

            if((0 != pu2_num_pics_of_a_pic_type[u1_i]) &&
               (QUAD_MODEL != ps_rd_model_of_pic_type->u1_model_used))
            {
                u1_combined_model_used = LIN_MODEL;
                break;
            }
        }

        if(u1_combined_model_used == LIN_MODEL)
        {
            for(u1_i = 0; u1_i < u1_num_pic_types; u1_i++)
            {
                ps_rd_model_of_pic_type = ps_rd_model + u1_i;

                if((0 != pu2_num_pics_of_a_pic_type[u1_i]) &&
                   (QUAD_MODEL != ps_rd_model_of_pic_type->u1_model_used) &&
                   (LIN_MODEL != ps_rd_model_of_pic_type->u1_model_used))
                {
                    u1_combined_model_used = PREV_FRAME_MODEL;
                    break;
                }
            }
        }

        /* solve the equation for the */
        {
            model_coeff eff_A;
            model_coeff eff_B;
            model_coeff eff_C;
            float fl_determinant;
            float fl_sad_by_qp_base;
            float fl_sad_by_qp_curr_frm;
            float fl_qp_curr_frm;
            float fl_bits_for_curr_frm;

            /* If the combined chosen model is quad model */
            if(QUAD_MODEL == u1_combined_model_used)
            {
                eff_A = 0.0;
                eff_B = 0.0;
                eff_C = 0.0;
                for(u1_i = 0; u1_i < u1_num_pic_types; u1_i++)
                {
                    ps_rd_model_of_pic_type = ps_rd_model + u1_i;

                    eff_A +=
                        ((pfl_eta[u1_i] + pu2_num_pics_of_a_pic_type[u1_i] - 1) *
                         ps_rd_model_of_pic_type->model_coeff_a_quad * pfl_gamma[u1_i]);
                    eff_B +=
                        ((pfl_eta[u1_i] * pfl_eta[u1_i] + pu2_num_pics_of_a_pic_type[u1_i] - 1) *
                         ps_rd_model_of_pic_type->model_coeff_b_quad * pfl_gamma[u1_i] *
                         pfl_gamma[u1_i]);
                    eff_C +=
                        (pu2_num_pics_of_a_pic_type[u1_i] *
                         ps_rd_model_of_pic_type->model_coeff_c_quad);
                }
                eff_C -= u4_bits_for_sub_gop;

                fl_determinant = eff_A * eff_A - 4 * eff_B * eff_C;

                if(fl_determinant < 0)
                {
                    u1_combined_model_used =
                        PREV_FRAME_MODEL; /* TO BE replaced by LIN_MODEL later */
                }
                else
                {
                    fl_determinant = sqrt(fl_determinant);

                    fl_sad_by_qp_base = fl_determinant - eff_A;
                    fl_sad_by_qp_base = fl_sad_by_qp_base / (2 * eff_B);

                    fl_sad_by_qp_curr_frm =
                        fl_sad_by_qp_base * pfl_gamma[u1_curr_pic_type] * pfl_eta[u1_curr_pic_type];

                    ps_rd_model_of_pic_type = ps_rd_model + u1_curr_pic_type;

                    fl_bits_for_curr_frm =
                        ps_rd_model_of_pic_type->model_coeff_a_quad * fl_sad_by_qp_curr_frm +
                        ps_rd_model_of_pic_type->model_coeff_b_quad * fl_sad_by_qp_curr_frm *
                            fl_sad_by_qp_curr_frm +
                        ps_rd_model_of_pic_type->model_coeff_c_quad;
                }
            }

            /* If the combined chosen model is linear model with an intercept */
            if(LIN_MODEL == u1_combined_model_used)
            {
                eff_A = 0.0;
                eff_B = 0.0;
                eff_C = 0.0;
                for(u1_i = 0; u1_i < u1_num_pic_types; u1_i++)
                {
                    ps_rd_model_of_pic_type = ps_rd_model + u1_i;

                    eff_A +=
                        ((pfl_eta[u1_i] + pu2_num_pics_of_a_pic_type[u1_i] - 1) *
                         ps_rd_model_of_pic_type->model_coeff_a_lin * pfl_gamma[u1_i]);

                    eff_C +=
                        (pu2_num_pics_of_a_pic_type[u1_i] *
                         ps_rd_model_of_pic_type->model_coeff_c_lin);
                }
                eff_C -= u4_bits_for_sub_gop;

                fl_determinant = (-(eff_C / eff_A));

                if((fl_determinant) <= 0)
                {
                    u1_combined_model_used = PREV_FRAME_MODEL;
                }
                else
                {
                    fl_sad_by_qp_base = fl_determinant;

                    fl_sad_by_qp_curr_frm =
                        fl_sad_by_qp_base * pfl_gamma[u1_curr_pic_type] * pfl_eta[u1_curr_pic_type];

                    ps_rd_model_of_pic_type = ps_rd_model + u1_curr_pic_type;

                    fl_bits_for_curr_frm =
                        ps_rd_model_of_pic_type->model_coeff_a_lin * fl_sad_by_qp_curr_frm +
                        ps_rd_model_of_pic_type->model_coeff_c_lin;
                }
            }

            /* If the combined chosen model is linear model without an intercept */
            if(PREV_FRAME_MODEL == u1_combined_model_used)
            {
                eff_A = 0.0;
                eff_B = 0.0;
                eff_C = 0.0;
                for(u1_i = 0; u1_i < u1_num_pic_types; u1_i++)
                {
                    ps_rd_model_of_pic_type = ps_rd_model + u1_i;

                    eff_A +=
                        ((pfl_eta[u1_i] + pu2_num_pics_of_a_pic_type[u1_i] - 1) *
                         ps_rd_model_of_pic_type->model_coeff_a_lin_wo_int * pfl_gamma[u1_i]);
                }

                fl_sad_by_qp_base = u4_bits_for_sub_gop / eff_A;

                fl_sad_by_qp_curr_frm =
                    fl_sad_by_qp_base * pfl_gamma[u1_curr_pic_type] * pfl_eta[u1_curr_pic_type];

                ps_rd_model_of_pic_type = ps_rd_model + u1_curr_pic_type;

                fl_bits_for_curr_frm =
                    ps_rd_model_of_pic_type->model_coeff_a_lin_wo_int * fl_sad_by_qp_curr_frm;
            }

            /* store the model that was finally used to calculate Qp.
            This is so that the same model is used in further calculations for this picture. */
            ps_rd_model_of_pic_type = ps_rd_model + u1_curr_pic_type;
            ps_rd_model_of_pic_type->u1_model_used = u1_combined_model_used;

            i4_per_frm_bits_Ti = (WORD32)(fl_bits_for_curr_frm + 0.5);

            if(fl_sad_by_qp_curr_frm > 0)
                fl_qp_curr_frm = (float)u4_curr_estimated_sad / fl_sad_by_qp_curr_frm;
            else
                fl_qp_curr_frm = 255;

            if(fl_qp_curr_frm > 255)
                fl_qp_curr_frm = 255;

            *pu1_curr_pic_type_qp = (fl_qp_curr_frm + 0.5);
        }
    }
    return (i4_per_frm_bits_Ti);
}

model_coeff get_linear_coefficient(rc_rd_model_t *ps_rd_model)
{
    /*UWORD32 linear_coeff:
    linear_coeff = ps_rd_model->model_coeff_a_lin_wo_int;*/
    return (ps_rd_model->model_coeff_a_lin_wo_int);
}
#endif /* !(RC_FIXED_POINT) */
WORD32 rc_rd_model_dummy_for_avoiding_warnings(
    rc_rd_model_t **pps_rc_rd_model, itt_memtab_t *ps_memtab, ITT_FUNC_TYPE_E e_func_type)
{
    WORD32 i4_mem_tab_idx = 0;
    static rc_rd_model_t s_rc_rd_model_temp;

    /* Hack for al alloc, during which we dont have any state memory.
      Dereferencing can cause issues */
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
        (*pps_rc_rd_model) = &s_rc_rd_model_temp;

    /*for src rate control state structure*/
    if(e_func_type != GET_NUM_MEMTAB)
    {
        fill_memtab(
            &ps_memtab[i4_mem_tab_idx], sizeof(rc_rd_model_t), MEM_TAB_ALIGNMENT, PERSISTENT, DDR);
        use_or_fill_base(&ps_memtab[0], (void **)pps_rc_rd_model, e_func_type);
    }
    i4_mem_tab_idx++;

    return (i4_mem_tab_idx);
}