aboutsummaryrefslogtreecommitdiff
path: root/encoder/hme_interface.h
blob: 33c98faf0a227836eebd701e72cadf1a7a571d8d (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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/******************************************************************************
 *
 * 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 hme_interface.h
*
* \brief
*    Interfaces exported by ME to the world outside of ME
*
* \date
*    18/09/2012
*
* \author
*    Ittiam
*
******************************************************************************
*/

#ifndef _HME_INTERFACE_H_
#define _HME_INTERFACE_H_

/*****************************************************************************/
/* Constant Macros                                                           */
/*****************************************************************************/

/**
******************************************************************************
 *  @brief      Maximum number of layers allowed
******************************************************************************
 */
#define MAX_NUM_LAYERS 4

/**
******************************************************************************
 *  @brief      layer max dimensions
******************************************************************************
 */
#define HME_MAX_WIDTH 1920
#define HME_MAX_HEIGHT 1088

/**
******************************************************************************
 *  @brief      layer min dimensions
******************************************************************************
 */
#define MIN_WD_COARSE 16
#define MIN_HT_COARSE 16

/**
******************************************************************************
 *  @brief     HME COARSE LAYER STEP SIZE
******************************************************************************
 */

#define HME_COARSE_STEP_SIZE_HIGH_SPEED 4
#define HME_COARSE_STEP_SIZE_HIGH_QUALITY 2

/**
******************************************************************************
 *  @brief      Memtabs required by layer ctxt: each layer ctxt requires 1
 *               memtab for itslf, 1 for mv bank, 1 for ref idx bank, one
 *               for input bufffer and 1 for storing segmentation info in
 *               worst case
******************************************************************************
 */
#define HME_MEMTABS_COARSE_LAYER_CTXT (5 * (MAX_NUM_LAYERS - 1) * (MAX_NUM_REF + 1))

/**
******************************************************************************
 *  @brief      Total number of memtabs reuqired by HME. Atleast 22 memtabs
 *              for different search results structure, 2*MAX_NUM_REF memtabs
 *              for search nodes maintaining coarse layer results in prev
 *              row, and for histograms. Memtabs reqd for layer,me ctxt
 *              ctb node mgr and buf mgr plus some 8 for safety
 *              if multi threaded then some memtabs will be more
******************************************************************************
 */
#define HME_COARSE_TOT_MEMTABS                                                                     \
    (22 + HME_MEMTABS_COARSE_LAYER_CTXT + (3 * MAX_NUM_REF) + 8 * MAX_NUM_FRM_PROC_THRDS_PRE_ENC + \
     1)

/**
******************************************************************************
 *  @brief      Memtabs required by layer ctxt (enc): each layer ctxt requires 1
 *               memtab for itslf, 1 for mv bank, 1 for ref idx bank, one
 *               for input bufffer and 1 for storing segmentation info in
 *               worst case
******************************************************************************
 */
#define MIN_HME_MEMTABS_ENC_LAYER_CTXT (5 * 1 * (MAX_NUM_REF + 1))

#define MAX_HME_MEMTABS_ENC_LAYER_CTXT (5 * 1 * (MAX_NUM_REF + 1 + MAX_NUM_ME_PARALLEL))

/**
******************************************************************************
 *  @brief      Total number of memtabs reuqired by HME. Atleast 22 memtabs
 *              for different search results structure, 2*MAX_NUM_REF memtabs
 *              for search nodes maintaining coarse layer results in prev
 *              row, and for histograms. Memtabs reqd for layer,me ctxt
 *              ctb node mgr and buf mgr plus some 8 for safety
 *              if multi threaded then some memtabs will be more
******************************************************************************
 */

#define MIN_HME_ENC_TOT_MEMTABS                                                                    \
    (22 + MIN_HME_MEMTABS_ENC_LAYER_CTXT + (3 * MAX_NUM_REF) + 28 * MAX_NUM_FRM_PROC_THRDS_ENC +   \
     2 /* Clustering */ + 1 /*traqo*/ + 1 /* ME Optimised Function List */)

#define MAX_HME_ENC_TOT_MEMTABS                                                                    \
    ((22 * MAX_NUM_ME_PARALLEL) + MAX_HME_MEMTABS_ENC_LAYER_CTXT +                                 \
     (3 * MAX_NUM_REF * MAX_NUM_ME_PARALLEL) +                                                     \
     28 * MAX_NUM_FRM_PROC_THRDS_ENC * MAX_NUM_ME_PARALLEL + 2 /* Clustering */ + 1 /*traqo*/ +    \
     1 /* ME Optimised Function List */)

/*****************************************************************************/
/* Enumerations                                                              */
/*****************************************************************************/
/**
******************************************************************************
 *  @enum     HME_MEM_ATTRS_T
 *  @brief      Contains type of memory: scratch, scratch ovly, persistent
******************************************************************************
 */
typedef enum
{
    HME_SCRATCH_MEM,
    HME_SCRATCH_OVLY_MEM,
    HME_PERSISTENT_MEM
} HME_MEM_ATTRS_T;

/**
******************************************************************************
 *  @enum     ME_QUALITY_PRESETS_T
 *  @brief    Describes the source for values in me_quality_params_t struct
******************************************************************************
 */
typedef enum
{
    ME_PRISTINE_QUALITY = 0,
    ME_HIGH_QUALITY = 2,
    ME_MEDIUM_SPEED,
    ME_HIGH_SPEED,
    ME_XTREME_SPEED,
    ME_XTREME_SPEED_25,
    ME_USER_DEFINED
} ME_QUALITY_PRESETS_T;

/*****************************************************************************/
/* Structures                                                                */
/*****************************************************************************/

/**
******************************************************************************
 *  @struct     hme_ref_buf_info_t
 *  @brief      Contains all required information of a ref picture
 *              Valid for a given layer.
******************************************************************************
 */
typedef struct
{
    /** Amt of padding in X direction both sides. */
    U08 u1_pad_x;

    /** Amt of padding in Y direction both sides */
    U08 u1_pad_y;

    /** Recon stride, in pixels */
    S32 luma_stride;

    /** Offset w.r.t. actual start of the buffer */
    S32 luma_offset;

    /** Src ptrs of the reference pictures*/
    U08 *pu1_ref_src;

    /** Reference ptrs for fpel plane, needed for this layer closed loop ME */
    U08 *pu1_rec_fxfy;

    /** Reference ptrs for hxfy plane (x = k+0.5, y = m) */
    U08 *pu1_rec_hxfy;

    /** Reference ptrs for fxhy plane (x = k, y = m + 0.5 */
    U08 *pu1_rec_fxhy;

    /** Reference ptrs for hxhy plane (x = k + 0.5, y = m + 0.5 */
    U08 *pu1_rec_hxhy;

    /** Reference ptr for u plane */
    U08 *pu1_rec_u;

    /** Reference ptr for v plane */
    U08 *pu1_rec_v;

    /** chroma plane stride in pixels */
    S32 chroma_stride;

    S32 chroma_offset;

    /** Pointer to dependency manager of recon buffer */
    void *pv_dep_mngr;

} hme_ref_buf_info_t;

/**
******************************************************************************
 *  @struct     interp_prms_t
 *  @brief      All parameters for the interpolation function
******************************************************************************
 */
typedef struct
{
    /** Array of ptr of 4 planes in order fxfy, hxfy, fxhy, hxhy */
    U08 **ppu1_ref;

    /**
     *  Array of pointers for ping-pong buffers, used to store interp out
     *  Output during a call goes to any one of these buffers
     */
    U08 *apu1_interp_out[5];

    /**
     *  Working memory to store 16 bit intermediate output. This has to be
     *  of size i4_blk_wd * (i4_blk_ht + 7) * 2
     */
    U08 *pu1_wkg_mem;

    /** Stride of all 4 planes of ref buffers */
    S32 i4_ref_stride;

    /** Width of interpolated output blk desired */
    S32 i4_blk_wd;

    /** Ht of interpolated output blk desired */
    S32 i4_blk_ht;

    /**
     *  Stride of interpolated output bufers,
     *  applicable for both ping and pong
     */
    S32 i4_out_stride;

    /** Final output pointer, which may be one of ping-pong or hpel planes */
    U08 *pu1_final_out;

    /** STride of the output bfufer */
    S32 i4_final_out_stride;

} interp_prms_t;

/*****************************************************************************/
/* Typedefs                                                                  */
/*****************************************************************************/
typedef void (*PF_EXT_UPDATE_FXN_T)(void *, void *, S32, S32);

//typedef void (*PF_GET_INTRA_CU_AND_COST)(void *, S32, S32, S32 *, S32*, double *, S32);

typedef void (*PF_INTERP_FXN_T)(interp_prms_t *ps_prms, S32 i4_mv_x, S32 i4_mv_y, S32 interp_buf_id);

typedef void (*PF_SCALE_FXN_T)(
    U08 *pu1_src, S32 src_stride, U08 *pu1_dst, S32 dst_stride, S32 wd, S32 ht, U08 *pu1_wkg_mem);

/**
******************************************************************************
 *  @struct     hme_ref_desc_t
 *  @brief      Contains all reqd information for ref pics across all layers
 *              but for a given POC/ref id
******************************************************************************
 */
typedef struct
{
    /**
     *  Reference id in LC list. This is a unified list containing both fwd
     *  and backward direction references. Having a unified list just does
     *  a unique mapping of frames to ref id and eases out addressing in the
     *  ME search.
     */
    S08 i1_ref_id_lc;

    /**
     *  Reference id in L0 list. Priority is given to temporally fwd dirn
     *  unless of a scene change like case
     */
    S08 i1_ref_id_l0;

    /**
     *  Reference id in L1 list. Priority to backward dirn unless scene change
     *  like case
    */
    S08 i1_ref_id_l1;

    /** Whether this ref is temporally forward w.r.t. current pic */
    U08 u1_is_fwd;

    /** POC of this ref pic. */
    S32 i4_poc;

    /** display_num of this ref pic. */
    S32 i4_display_num;
    /**
     *  Lambda to be used for S + lambda*bits style cost computations when
     *  using this ref pic. This is a function of ref dist and hence diff
     *  ref has diff lambda
     */
    S32 lambda;

    /** Ref buffer info for all layers */
    hme_ref_buf_info_t as_ref_info[MAX_NUM_LAYERS];

    /** Weights and offset of reference picture
     * used for weighted pred analysis
     */
    S16 i2_weight;

    S16 i2_offset;

    /*
    * IDR GOP number
    */

    WORD32 i4_GOP_num;

} hme_ref_desc_t;

/**
******************************************************************************
 *  @struct     hme_ref_map_t
 *  @brief      Complete ref information across all layers and POCs
 *              Information valid for a given inp frame with a given POC.
******************************************************************************
 */
typedef struct
{
    /** Number of active ref picturs in LC list */
    S32 i4_num_ref;

    /** Recon Pic buffer pointers for L0 list */
    recon_pic_buf_t **pps_rec_list_l0;

    /** Recon Pic buffer pointers for L0 list */
    recon_pic_buf_t **pps_rec_list_l1;

    /** Reference descriptors for all ref pics */
    hme_ref_desc_t as_ref_desc[MAX_NUM_REF];

} hme_ref_map_t;

/**
 ******************************************************************************
 *  @struct me_coding_params_t
 *  @param e_me_quality_presets : Quality preset value
 *  @brief  ME Parameters that affect quality depending on their state
 ******************************************************************************
*/
typedef struct
{
    ME_QUALITY_PRESETS_T e_me_quality_presets;

    S32 i4_num_steps_hpel_refine;

    S32 i4_num_steps_qpel_refine;

    U08 u1_l0_me_controlled_via_cmd_line;

    U08 u1_num_results_per_part_in_l0me;

    U08 u1_num_results_per_part_in_l1me;

    U08 u1_num_results_per_part_in_l2me;

    U08 u1_max_num_coloc_cands;

    U08 u1_max_2nx2n_tu_recur_cands;

    U08 u1_max_num_fpel_refine_centers;

    U08 u1_max_num_subpel_refine_centers;
} me_coding_params_t;

/**
 ******************************************************************************
 *  @struct hme_init_prms_t
 *  @brief  Initialization parameters used during HME instance creation
 ******************************************************************************
*/
typedef struct
{
    /** Pointer to widths of various simulcast layers,
     * starting with biggest resolution
     */
    S32 a_wd[MAX_NUM_LAYERS];

    /** Pointer to heights of various simulcast layers,
     *  starting with biggest resolution
     */
    S32 a_ht[MAX_NUM_LAYERS];

    /** Maximum number of reference frames that a frame ever has to search */
    S32 max_num_ref;

    /** Number of results to be stored in the coarsest layer */
    S32 max_num_results_coarse;

    /**
     *  Number of layers for which explicit ME is to be done
     *  0 or MAX_NUM_LAYERS: encoder will do explicit ME for all layers
     *  anything in between, explicit ME done for that many layers
     */
    S32 num_layers_explicit_search;

    /** Number of simulcast layers to be encoded */
    S32 num_simulcast_layers;

    /** Maximum number of results per reference per partition */
    S32 max_num_results;

    /**
     *  If enabled, all layers store segmentation info at 16x16 lvl
     *  If not enabled, then only finest layer stores this info
     */
    S32 segment_higher_layers;

    /**
     *  If enabled, the non enocde layers use 8x8 blks with 4x4 partial
     *  sads also being evaluated, which is more powerful but computationally
     *  less efficient
     */
    S32 use_4x4;

    /**
     *  Number of B frames allowed between P frames
     */
    S32 num_b_frms;

    /** CTB Size as passed by encoder */
    S32 log_ctb_size;

    /** number of threads created run time */
    S32 i4_num_proc_thrds;

    /* This struct contains fields corresponding to quality knobs for ME */
    me_coding_params_t s_me_coding_tools;

    S32 max_vert_search_range;

    S32 max_horz_search_range;

    S32 is_interlaced;

    U08 u1_max_tr_depth;

    U08 u1_is_stasino_enabled;

    IV_ARCH_T e_arch_type;
} hme_init_prms_t;

/**
 ******************************************************************************
 *  @struct hme_frm_prms_t
 *  @brief  Frame level prms for HME execution
 ******************************************************************************
*/
typedef struct
{
    /** Range of the Motion vector in fpel units at finest layer x dirn */
    S16 i2_mv_range_x;

    /** range of motion vector in fpel units at finest layer y dirn */
    S16 i2_mv_range_y;

    /** Context for computing the cost function */
    void *pv_mv_cost_ctxt;

    /** Interpolation function pointers */
    PF_INTERP_FXN_T pf_interp_fxn;

    U08 is_i_pic;

    S32 bidir_enabled;

    S32 i4_temporal_layer_id;

    /**
      * Lambda values in Q format. 4 values exist: Closed loop SATD/SAD
      * and open loop SATD/SAD
      */
    S32 i4_cl_sad_lambda_qf;
    S32 i4_cl_satd_lambda_qf;
    S32 i4_ol_sad_lambda_qf;
    S32 i4_ol_satd_lambda_qf;

    /** Shift for lambda QFormat */
    S32 lambda_q_shift;

    S32 qstep;
    S32 qstep_ls8;
    S32 i4_frame_qp;
    S32 is_pic_second_field;

    /**
     * Number of active references in l0
     */
    U08 u1_num_active_ref_l0;

    /**
     * Number of active references in l1
     */
    U08 u1_num_active_ref_l1;

    /* Flag that specifies whether CU level QP */
    /* modulation is enabled */
    U08 u1_is_cu_qp_delta_enabled;

} hme_frm_prms_t;

/**
 ******************************************************************************
 *  @struct hme_memtab_t
 *  @brief  Structure to return memory requirements for one buffer.
 ******************************************************************************
*/
typedef struct
{
    /** Base of the memtab. Filled by application */
    U08 *pu1_mem;

    /** Required size of the memtab. Filed by module */
    S32 size;

    /** Alignment required */
    S32 align;

    /** type of memory */
    HME_MEM_ATTRS_T e_mem_attr;

} hme_memtab_t;

/**
 ******************************************************************************
 *  @struct hme_inp_buf_attr_t
 *  @brief  Attributes of input buffer and planes
 ******************************************************************************
*/
typedef struct
{
    /** Luma ptr 0, 0 position */
    U08 *pu1_y;

    /** Cb component or U component, 0, 0 position */
    U08 *pu1_u;

    /** Cr component or V component, 0, 0 position */
    U08 *pu1_v;

    /** Stride of luma component in pixels */
    S32 luma_stride;

    /** Stride of chroma component in pixels */
    S32 chroma_stride;
} hme_inp_buf_attr_t;

/**
 ******************************************************************************
 *  @struct hme_inp_desc_t
 *  @brief  Descriptor of a complete input frames (all simulcast layers incl)
 ******************************************************************************
*/
typedef struct
{
    /** input attributes for all simulcast layers */
    hme_inp_buf_attr_t s_layer_desc[MAX_NUM_LAYERS];

    /** POC of the current input frame */
    S32 i4_poc;

    /** idr GOP number*/
    S32 i4_idr_gop_num;

    /** is refence picture */
    S32 i4_is_reference;

} hme_inp_desc_t;

/*****************************************************************************/
/* Extern Function Declarations                                              */
/*****************************************************************************/

/**
********************************************************************************
*  @fn     hme_enc_num_alloc()
*
*  @brief  returns number of memtabs that is required by hme module
*
*  @return   Number of memtabs required
********************************************************************************
*/
S32 hme_enc_num_alloc(WORD32 i4_num_me_frm_pllel);

/**
********************************************************************************
*  @fn     hme_coarse_num_alloc()
*
*  @brief  returns number of memtabs that is required by hme module
*
*  @return   Number of memtabs required
********************************************************************************
*/
S32 hme_coarse_num_alloc();

/**
********************************************************************************
*  @fn     hme_coarse_dep_mngr_num_alloc()
*
*  @brief  returns number of memtabs that is required by Dep Mngr for hme module
*
*  @return   Number of memtabs required
********************************************************************************
*/
WORD32 hme_coarse_dep_mngr_num_alloc();

/**
********************************************************************************
*  @fn     S32 hme_coarse_alloc(hme_memtab_t *ps_memtabs, hme_init_prms_t *ps_prms)
*
*  @brief  Fills up memtabs with memory information details required by HME
*
*  @param[out] ps_memtabs : Pointre to an array of memtabs where module fills
*              up its requirements of memory
*
*  @param[in] ps_prms : Input parameters to module crucial in calculating reqd
*                       amt of memory
*
*  @return   Number of memtabs required
*******************************************************************************
*/
S32 hme_coarse_alloc(hme_memtab_t *ps_memtabs, hme_init_prms_t *ps_prms);

/**
*******************************************************************************
*  @fn hme_coarse_dep_mngr_alloc
*
*  @brief  Fills up memtabs with memory information details required by Coarse HME
*
* \param[in,out]  ps_mem_tab : pointer to memory descriptors table
* \param[in] ps_init_prms : Create time static parameters
* \param[in] i4_mem_space : memspace in whihc memory request should be done
*
*  @return   Number of memtabs required
*******************************************************************************
*/
WORD32 hme_coarse_dep_mngr_alloc(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    WORD32 i4_mem_space,
    WORD32 i4_num_proc_thrds,
    WORD32 i4_resolution_id);

/**
********************************************************************************
*  @fn     S32 hme_enc_alloc(hme_memtab_t *ps_memtabs, hme_init_prms_t *ps_prms)
*
*  @brief  Fills up memtabs with memory information details required by HME
*
*  @param[out] ps_memtabs : Pointer to an array of memtabs where module fills
*              up its requirements of memory
*
*  @param[in] ps_prms : Input parameters to module crucial in calculating reqd
*                       amt of memory
*
*  @return   Number of memtabs required
*******************************************************************************
*/
S32 hme_enc_alloc(hme_memtab_t *ps_memtabs, hme_init_prms_t *ps_prms, WORD32 i4_num_me_frm_pllel);

/**
********************************************************************************
*  @fn     S32 hme_enc_init(void *pv_ctxt,
*                       hme_memtab_t *ps_memtabs,
*                       hme_init_prms_t *ps_prms);
*
*  @brief  Initialization (one time) of HME
*
*  @param[in,out] pv_ctxt : Pointer to context of HME
*
*  @param[in] ps_memtabs : updated memtabs by application (allocated memory)
*
*  @param[in] ps_prms : Initialization parametres
*
*  @return   0 : success, -1 : failure
*******************************************************************************
*/
S32 hme_enc_init(
    void *pv_ctxt,
    hme_memtab_t *ps_memtabs,
    hme_init_prms_t *ps_prms,
    rc_quant_t *ps_rc_quant_ctxt,
    WORD32 i4_num_me_frm_pllel);

/**
********************************************************************************
*  @fn     S32 hme_coarse_init(void *pv_ctxt,
*                       hme_memtab_t *ps_memtabs,
*                       hme_init_prms_t *ps_prms);
*
*  @brief  Initialization (one time) of HME
*
*  @param[in,out] pv_ctxt : Pointer to context of HME
*
*  @param[in] ps_memtabs : updated memtabs by application (allocated memory)
*
*  @param[in] ps_prms : Initialization parametres
*
*  @return   0 : success, -1 : failure
*******************************************************************************
*/
S32 hme_coarse_init(void *pv_ctxt, hme_memtab_t *ps_memtabs, hme_init_prms_t *ps_prms);

/*!
******************************************************************************
* \if Function name : ihevce_coarse_me_get_lyr_prms_dep_mngr \endif
*
* \brief Returns to the caller key attributes relevant for dependency manager,
*        ie, the number of vertical units in each layer
*
* \par Description:
*    This function requires the precondition that the width and ht of encode
*    layer is known.
*    The number of layers, number of vertical units in each layer, and for
*    each vertial unit in each layer, its dependency on previous layer's units
*    From ME's perspective, a vertical unit is one which is smallest min size
*    vertically (and spans the entire row horizontally). This is CTB for encode
*    layer, and 8x8 / 4x4 for non encode layers.
*
* \param[in] num_layers : Number of ME Layers
* \param[in] pai4_ht    : Array storing ht at each layer
* \param[in] pai4_wd    : Array storing wd at each layer
* \param[out] pi4_num_vert_units_in_lyr : Array of size N (num layers), each
*                     entry has num vertical units in that particular layer
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_coarse_me_get_lyr_prms_dep_mngr(
    WORD32 num_layers, WORD32 *pai4_ht, WORD32 *pai4_wd, WORD32 *pai4_num_vert_units_in_lyr);

/**
********************************************************************************
*  @fn     hme_coarse_dep_mngr_alloc_mem()
*
*  @brief  Requests/ assign memory for HME Dep Mngr
*
* \param[in,out]  ps_mem_tab : pointer to memory descriptors table
* \param[in] ps_init_prms : Create time static parameters
* \param[in] i4_mem_space : memspace in whihc memory request should be done
*
*  @return  number of memtabs
********************************************************************************
*/
WORD32 hme_coarse_dep_mngr_alloc_mem(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    WORD32 i4_mem_space,
    WORD32 i4_num_proc_thrds,
    WORD32 i4_resolution_id);

/**
********************************************************************************
*  @fn     hme_coarse_dep_mngr_init()
*
*  @brief  Assign memory for HME Dep Mngr
*
* \param[in,out]  ps_mem_tab : pointer to memory descriptors table
* \param[in] ps_init_prms : Create time static parameters
*  @param[in] pv_ctxt : ME ctxt
* \param[in] pv_osal_handle : Osal handle
*
*  @return  number of memtabs
********************************************************************************
*/
WORD32 hme_coarse_dep_mngr_init(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    void *pv_ctxt,
    void *pv_osal_handle,
    WORD32 i4_num_proc_thrds,
    WORD32 i4_resolution_id);

/**
********************************************************************************
*  @fn     hme_coarse_dep_mngr_reg_sem()
*
*  @brief  Assign semaphores for HME Dep Mngr
*
* \param[in] pv_me_ctxt : pointer to Coarse ME ctxt
* \param[in] ppv_sem_hdls : Arry of semaphore handles
* \param[in] i4_num_proc_thrds : Number of processing threads
*
*  @return  number of memtabs
********************************************************************************
*/
void hme_coarse_dep_mngr_reg_sem(void *pv_ctxt, void **ppv_sem_hdls, WORD32 i4_num_proc_thrds);

/**
********************************************************************************
*  @fn     hme_coarse_dep_mngr_delete()
*
*    Destroy Coarse ME Dep Mngr module
*   Note : Only Destroys the resources allocated in the module like
*   semaphore,etc. Memory free is done Separately using memtabs
*
* \param[in] pv_me_ctxt : pointer to Coarse ME ctxt
* \param[in] ps_init_prms : Create time static parameters
*
*  @return  none
********************************************************************************
*/
void hme_coarse_dep_mngr_delete(
    void *pv_me_ctxt, ihevce_static_cfg_params_t *ps_init_prms, WORD32 i4_resolution_id);

void hme_coarse_get_layer1_mv_bank_ref_idx_size(
    S32 n_tot_layers,
    S32 *a_wd,
    S32 *a_ht,
    S32 max_num_ref,
    S32 *pi4_mv_bank_size,
    S32 *pi4_ref_idx_size);

/**
********************************************************************************
*  @fn     S32 hme_add_inp(void *pv_ctxt,
*                       hme_inp_desc_t *ps_inp_desc);
*
*  @brief  Updates the HME context with details of the input buffers and POC.
*          Layers that are not encoded are processed further in terms of
*          pyramid generation.
*
*  @param[in,out] pv_ctxt : Pointer to context of HME
*
*  @param[in] ps_inp_desc : Input descriptor containing information of all
*             simulcast layers of input.
*
*  @return   void
*******************************************************************************
*/
void hme_add_inp(void *pv_ctxt, hme_inp_desc_t *ps_inp_desc, S32 me_frm_id, WORD32 thrd_id);

void hme_coarse_add_inp(void *pv_me_ctxt, hme_inp_desc_t *ps_inp_desc, WORD32 i4_curr_idx);

/**
********************************************************************************
*  @fn     hme_process_frm_init
*
*  @brief  HME frame level initialsation processing function
*
*  @param[in] pv_me_ctxt : ME ctxt pointer
*
*  @param[in] ps_ref_map : Reference map prms pointer
*
*  @param[in] ps_frm_prms :Pointer to frame params
*
*  @return Scale factor in Q8 format
********************************************************************************
*/

void hme_process_frm_init(
    void *pv_me_ctxt,
    hme_ref_map_t *ps_ref_map,
    hme_frm_prms_t *ps_frm_prms,
    WORD32 me_frm_id,
    WORD32 i4_num_me_frm_pllel);

void hme_coarse_process_frm_init(
    void *pv_me_ctxt, hme_ref_map_t *ps_ref_map, hme_frm_prms_t *ps_frm_prms);

/**
********************************************************************************
*  @fn     void hme_process_frm(void *pv_ctxt,
*                    hme_ref_map_t *ps_ref_map,
*                    U16 **ppu2_intra_cost,
*                    hme_frm_prms_t *ps_frm_prms);
*
*  @brief  Processes all the layers of the input, and updates the MV Banks.
*          Note that this function is not to be called if processing of a single
*          layer is desired.
*
*  @param[in,out] pv_ctxt : Pointer to context of HME
*
*  @param[in] ps_ref_map : Map structure that has for current input, lists of
*             ref pics (POC) mapping to LC, L0 and L1, and buffer ptrs as well
*             Informatino for all simulcast layers present.
*
*  @param[in] ppu2_intra_cost : array of Pointer to intra cost evaluated at an
*              8x8 level, stored in raster order. At each layer, the
*              corresponding ptr points to raster ordered array of wdxht/64,
*              wd and ht are layer width and ht respectively. Also, note that
*              ppu2_intra_cost[0] points to biggest resolution layer,
*              and from there on in decreasing order of size.
*
* @param[in]  ps_frm_prms : input frame parameters (excluding ref info) that
*             control the search complexity. Refer to hme_frm_prms_t for more
*              info regards the same.
*
*  @return   void
*******************************************************************************
*/

void hme_process_frm(
    void *pv_me_ctxt,
    pre_enc_L0_ipe_encloop_ctxt_t *ps_l0_ipe_input,
    hme_ref_map_t *ps_ref_map,
    double **ppd_intra_costs,
    hme_frm_prms_t *ps_frm_prms,
    PF_EXT_UPDATE_FXN_T pf_ext_update_fxn,
    //PF_GET_INTRA_CU_AND_COST pf_get_intra_cu_and_cost,
    void *pv_coarse_layer,
    void *pv_multi_thrd_ctxt,
    WORD32 i4_frame_parallelism_level,
    S32 thrd_id,
    S32 i4_me_frm_id);

void hme_coarse_process_frm(
    void *pv_me_ctxt,
    hme_ref_map_t *ps_ref_map,
    hme_frm_prms_t *ps_frm_prms,
    void *pv_multi_thrd_ctxt,
    WORD32 i4_ping_pong,
    void **ppv_dep_mngr_hme_sync);

void hme_discard_frm(
    void *pv_ctxt, S32 *p_pocs_to_remove, S32 i4_idr_gop_num, S32 i4_num_me_frm_pllel);

void hme_coarse_discard_frm(void *pv_me_ctxt, S32 *p_pocs_to_remove);

/**
*******************************************************************************
*  @fn     S32 hme_set_resolution(void *pv_me_ctxt,
*                                   S32 n_enc_layers,
*                                   S32 *p_wd,
*                                   S32 *p_ht
*
*  @brief  Sets up the layers based on resolution information.
*
*  @param[in, out] pv_me_ctxt : ME handle, updated with the resolution info
*
*  @param[in] n_enc_layers : Number of layers encoded
*
*  @param[in] p_wd : Pointer to an array having widths for each encode layer
*
*  @param[in] p_ht : Pointer to an array having heights for each encode layer
*
*  @return   void
*******************************************************************************
*/

void hme_set_resolution(void *pv_me_ctxt, S32 n_enc_layers, S32 *p_wd, S32 *p_ht, S32 me_frm_id);

void hme_coarse_set_resolution(void *pv_me_ctxt, S32 n_enc_layers, S32 *p_wd, S32 *p_ht);

/**
*******************************************************************************
*  @fn     WORD32 hme_get_active_pocs_list(void *pv_me_ctxt)
*
*  @brief  Returns the list of active POCs in ME ctxt
*
*  @param[in] pv_me_ctxt : handle to ME context
*
*  @param[out] p_pocs_buffered_in_me : pointer to an array which this fxn
*                                      populates with pocs active
*
*  @return   void
*******************************************************************************
*/
WORD32 hme_get_active_pocs_list(void *pv_me_ctxt, S32 i4_num_me_frm_pllel);

void hme_coarse_get_active_pocs_list(void *pv_me_ctxt, S32 *p_pocs_buffered_in_me);

S32 hme_get_blk_size(S32 use_4x4, S32 layer_id, S32 n_layers, S32 encode);

/**
********************************************************************************
*  @fn     hme_get_mv_blk_size()
*
*  @brief  returns whether blk uses 4x4 size or something else.
*
*  @param[in] enable_4x4 : input param from application to enable 4x4
*
*  @param[in] layer_id : id of current layer (0 finest)
*
*  @param[in] num_layeers : total num layers
*
*  @param[in] is_enc : Whether encoding enabled for layer
*
*  @return   1 for 4x4 blks, 0 for 8x8
********************************************************************************
*/
S32 hme_get_mv_blk_size(S32 enable_4x4, S32 layer_id, S32 num_layers, S32 is_enc);

void hme_set_refine_prms(
    void *pv_refine_prms,
    U08 u1_encode,
    S32 num_ref,
    S32 layer_id,
    S32 num_layers,
    S32 num_layers_explicit_search,
    S32 use_4x4,
    hme_frm_prms_t *ps_frm_prms,
    double **ppd_intra_costs,
    me_coding_params_t *ps_me_coding_tools);

S32 hme_coarse_find_free_descr_idx(void *pv_ctxt);

S32 hme_derive_num_layers(S32 n_enc_layers, S32 *p_wd, S32 *p_ht, S32 *p_disp_wd, S32 *p_disp_ht);

#endif /* #ifndef _HME_INTERFACE_H_ */