-
Notifications
You must be signed in to change notification settings - Fork 936
Expand file tree
/
Copy pathih264d_dpb_mgr.c
More file actions
2064 lines (1907 loc) · 81.3 KB
/
Copy pathih264d_dpb_mgr.c
File metadata and controls
2064 lines (1907 loc) · 81.3 KB
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
*/
#include "ih264_typedefs.h"
#include "ih264_macros.h"
#include "ih264_platform_macros.h"
#include "iv.h"
#include "ih264d_dpb_manager.h"
#include "ih264d_bitstrm.h"
#include "ih264d_parse_cavlc.h"
#include "ih264d_defs.h"
#include "ih264d_structs.h"
#include "ih264d_process_bslice.h"
#include "ih264d_debug.h"
#include "ih264d_tables.h"
#include "ih264d_error_handler.h"
#include "string.h"
#include "ih264d_defs.h"
#include "ih264_error.h"
#include "ih264_buf_mgr.h"
#include "assert.h"
/*!
***************************************************************************
* \file ih264d_dpb_mgr.c
*
* \brief
* Functions for managing the decoded picture buffer
*
* Detailed_description
*
* \date
* 19-12-2002
*
* \author Sriram Sethuraman
***************************************************************************
*/
/*!
**************************************************************************
* \if Function name : ih264d_init_ref_bufs \endif
*
* \brief
* Called at the start for initialization.
*
* \return
* none
**************************************************************************
*/
void ih264d_init_ref_bufs(dpb_manager_t *ps_dpb_mgr)
{
UWORD32 i;
struct dpb_info_t *ps_dpb_info = ps_dpb_mgr->as_dpb_info;
for(i = 0; i < MAX_REF_BUFS; i++)
{
ps_dpb_info[i].u1_used_as_ref = UNUSED_FOR_REF;
ps_dpb_info[i].u1_lt_idx = MAX_REF_BUFS + 1;
ps_dpb_info[i].ps_prev_short = NULL;
ps_dpb_info[i].ps_prev_long = NULL;
ps_dpb_info[i].ps_pic_buf = NULL;
ps_dpb_info[i].s_top_field.u1_reference_info = UNUSED_FOR_REF;
ps_dpb_info[i].s_bot_field.u1_reference_info = UNUSED_FOR_REF;
ps_dpb_info[i].s_top_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
ps_dpb_info[i].s_bot_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
}
ps_dpb_mgr->u1_num_st_ref_bufs = ps_dpb_mgr->u1_num_lt_ref_bufs = 0;
ps_dpb_mgr->ps_dpb_st_head = NULL;
ps_dpb_mgr->ps_dpb_ht_head = NULL;
ps_dpb_mgr->i1_gaps_deleted = 0;
ps_dpb_mgr->i1_poc_buf_id_entries = 0;
ps_dpb_mgr->u1_mmco_error_in_seq = 0;
ps_dpb_mgr->u1_num_gaps = 0;
for(i = 0; i < MAX_FRAMES; i++)
{
ps_dpb_mgr->ai4_gaps_start_frm_num[i] = INVALID_FRAME_NUM;
ps_dpb_mgr->ai4_gaps_end_frm_num[i] = 0;
ps_dpb_mgr->ai1_gaps_per_seq[i] = 0;
ps_dpb_mgr->ai4_poc_buf_id_map[i][0] = -1;
ps_dpb_mgr->ai4_poc_buf_id_map[i][1] = 0x7fffffff;
ps_dpb_mgr->ai4_poc_buf_id_map[i][2] = 0;
}
}
void ih264d_free_ref_pic_mv_bufs(void* pv_dec, UWORD8 pic_buf_id)
{
dec_struct_t *ps_dec = (dec_struct_t *)pv_dec;
if((pic_buf_id == ps_dec->u1_pic_buf_id) &&
ps_dec->ps_cur_slice->u1_field_pic_flag &&
(ps_dec->u1_top_bottom_decoded == 0))
{
return;
}
ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
pic_buf_id,
BUF_MGR_REF);
ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
ps_dec->au1_pic_buf_id_mv_buf_id_map[pic_buf_id],
BUF_MGR_REF);
}
/*!
**************************************************************************
* \if Function name : ih264d_delete_lt_node \endif
*
* \brief
* Delete a buffer with a long term index from the LT linked list
*
* \return
* none
**************************************************************************
*/
WORD32 ih264d_delete_lt_node(dpb_manager_t *ps_dpb_mgr,
UWORD32 u4_lt_idx,
UWORD8 u1_fld_pic_flag,
struct dpb_info_t *ps_lt_node_to_insert,
WORD32 *pi4_status)
{
*pi4_status = 0;
if(ps_dpb_mgr->u1_num_lt_ref_bufs > 0)
{
WORD32 i;
struct dpb_info_t *ps_next_dpb;
/* ps_unmark_node points to the node to be removed */
/* from long term list. */
struct dpb_info_t *ps_unmark_node;
//Find the node with matching LTIndex
ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
if(ps_next_dpb->u1_lt_idx == u4_lt_idx)
{
ps_unmark_node = ps_next_dpb;
}
else
{
for(i = 1; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
{
if(ps_next_dpb->ps_prev_long->u1_lt_idx == u4_lt_idx)
break;
ps_next_dpb = ps_next_dpb->ps_prev_long;
}
if(i == ps_dpb_mgr->u1_num_lt_ref_bufs)
*pi4_status = 1;
else
ps_unmark_node = ps_next_dpb->ps_prev_long;
}
if(*pi4_status == 0)
{
if(u1_fld_pic_flag)
{
if(ps_lt_node_to_insert != ps_unmark_node)
{
UWORD8 u1_deleted = 0;
/* for the ps_unmark_node mark the corresponding field */
/* field as unused for reference */
if(ps_unmark_node->s_top_field.u1_long_term_frame_idx
== u4_lt_idx)
{
ps_unmark_node->s_top_field.u1_reference_info =
UNUSED_FOR_REF;
ps_unmark_node->s_top_field.u1_long_term_frame_idx =
MAX_REF_BUFS + 1;
u1_deleted = 1;
}
if(ps_unmark_node->s_bot_field.u1_long_term_frame_idx
== u4_lt_idx)
{
ps_unmark_node->s_bot_field.u1_reference_info =
UNUSED_FOR_REF;
ps_unmark_node->s_bot_field.u1_long_term_frame_idx =
MAX_REF_BUFS + 1;
u1_deleted = 1;
}
if(!u1_deleted)
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
}
ps_unmark_node->u1_used_as_ref =
ps_unmark_node->s_top_field.u1_reference_info
| ps_unmark_node->s_bot_field.u1_reference_info;
}
else
ps_unmark_node->u1_used_as_ref = UNUSED_FOR_REF;
if(UNUSED_FOR_REF == ps_unmark_node->u1_used_as_ref)
{
if(ps_unmark_node == ps_dpb_mgr->ps_dpb_ht_head)
ps_dpb_mgr->ps_dpb_ht_head = ps_next_dpb->ps_prev_long;
ps_unmark_node->u1_lt_idx = MAX_REF_BUFS + 1;
ps_unmark_node->s_top_field.u1_reference_info =
UNUSED_FOR_REF;
ps_unmark_node->s_bot_field.u1_reference_info =
UNUSED_FOR_REF;
// Release the physical buffer
ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
ps_unmark_node->u1_buf_id);
ps_next_dpb->ps_prev_long = ps_unmark_node->ps_prev_long; //update link
ps_unmark_node->ps_prev_long = NULL;
ps_dpb_mgr->u1_num_lt_ref_bufs--; //decrement LT buf count
}
}
}
return OK;
}
/*!
**************************************************************************
* \if Function name : ih264d_insert_lt_node \endif
*
* \brief
* Insert a buffer into the LT linked list at a given LT index
*
* \return
* none
**************************************************************************
*/
WORD32 ih264d_insert_lt_node(dpb_manager_t *ps_dpb_mgr,
struct dpb_info_t *ps_mov_node,
UWORD32 u4_lt_idx,
UWORD8 u1_fld_pic_flag)
{
UWORD8 u1_mark_top_field_long_term = 0;
UWORD8 u1_mark_bot_field_long_term = 0;
{
if(u1_fld_pic_flag)
{
/* Assign corresponding field (top or bottom) long_term_frame_idx */
if((ps_mov_node->s_top_field.u1_reference_info == IS_LONG_TERM)
&& (ps_mov_node->s_bot_field.u1_reference_info
== IS_LONG_TERM))
{
if(ps_mov_node->u1_lt_idx == u4_lt_idx)
u1_mark_bot_field_long_term = 1;
else
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
}
else if(ps_mov_node->s_top_field.u1_reference_info == IS_LONG_TERM)
{
u1_mark_top_field_long_term = 1;
}
if(!(u1_mark_top_field_long_term || u1_mark_bot_field_long_term))
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
}
else
{
ps_mov_node->s_top_field.u1_reference_info = IS_LONG_TERM;
ps_mov_node->s_bot_field.u1_reference_info = IS_LONG_TERM;
ps_mov_node->s_top_field.u1_long_term_frame_idx = u4_lt_idx;
ps_mov_node->s_bot_field.u1_long_term_frame_idx = u4_lt_idx;
u1_mark_bot_field_long_term = 1;
u1_mark_top_field_long_term = 1;
}
ps_mov_node->u1_lt_idx = u4_lt_idx; //Assign the LT index to the node
ps_mov_node->ps_pic_buf->u1_long_term_frm_idx = u4_lt_idx;
ps_mov_node->u1_used_as_ref = IS_LONG_TERM;
/* Insert the new long term in the LT list with u4_lt_idx */
/* in ascending order. */
if(ps_dpb_mgr->u1_num_lt_ref_bufs > 0)
{
struct dpb_info_t *ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
if(u4_lt_idx < ps_next_dpb->u1_lt_idx)
{
//LTIndex to be inserted is the smallest LT index
//Update head and point prev to the next higher index
ps_mov_node->ps_prev_long = ps_next_dpb;
ps_dpb_mgr->ps_dpb_ht_head = ps_mov_node;
}
else
{
WORD32 i;
struct dpb_info_t *ps_nxtDPB = ps_next_dpb;
ps_next_dpb = ps_next_dpb->ps_prev_long;
for(i = 1; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
{
if(ps_next_dpb->u1_lt_idx > u4_lt_idx)
break;
ps_nxtDPB = ps_next_dpb;
ps_next_dpb = ps_next_dpb->ps_prev_long;
}
ps_nxtDPB->ps_prev_long = ps_mov_node;
ps_mov_node->ps_prev_long = ps_next_dpb;
}
}
else
{
ps_dpb_mgr->ps_dpb_ht_head = ps_mov_node;
ps_mov_node->ps_prev_long = NULL;
}
/* Identify the picture buffer as a long term picture buffer */
ps_mov_node->ps_pic_buf->u1_is_short = 0;
/* Increment LT buf count only if new LT node inserted */
/* If Increment during top_field is done, don't increment */
/* for bottom field, as both them are part of same pic. */
if(u1_mark_bot_field_long_term)
ps_dpb_mgr->u1_num_lt_ref_bufs++;
}
return OK;
}
/*!
**************************************************************************
* \if Function name : ih264d_insert_st_node \endif
*
* \brief
* Adds a short term reference picture into the ST linked list
*
* \return
* None
*
* \note
* Called only for a new coded picture with nal_ref_idc!=0
**************************************************************************
*/
WORD32 ih264d_insert_st_node(dpb_manager_t *ps_dpb_mgr,
struct pic_buffer_t *ps_pic_buf,
UWORD8 u1_buf_id,
UWORD32 u4_cur_pic_num)
{
WORD32 i;
struct dpb_info_t *ps_dpb_info = ps_dpb_mgr->as_dpb_info;
UWORD8 u1_picture_type = ps_pic_buf->u1_picturetype;
/* Find an unused dpb location */
for(i = 0; i < MAX_REF_BUFS; i++)
{
if((ps_dpb_info[i].ps_pic_buf == ps_pic_buf)
&& ps_dpb_info[i].u1_used_as_ref)
{
/*signal an error in the case of frame pic*/
if(ps_dpb_info[i].ps_pic_buf->u1_pic_type == FRM_PIC)
{
return ERROR_DBP_MANAGER_T;
}
else
{
/* Can occur only for field bottom pictures */
ps_dpb_info[i].s_bot_field.u1_reference_info = IS_SHORT_TERM;
return OK;
}
}
if((ps_dpb_info[i].u1_used_as_ref == UNUSED_FOR_REF)
&& (ps_dpb_info[i].s_top_field.u1_reference_info
== UNUSED_FOR_REF)
&& (ps_dpb_info[i].s_bot_field.u1_reference_info
== UNUSED_FOR_REF))
break;
}
if(i == MAX_REF_BUFS)
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
/* Create dpb info */
ps_dpb_info[i].ps_pic_buf = ps_pic_buf;
ps_dpb_info[i].ps_prev_short = ps_dpb_mgr->ps_dpb_st_head;
ps_dpb_info[i].u1_buf_id = u1_buf_id;
ps_dpb_info[i].u1_used_as_ref = TRUE;
ps_dpb_info[i].u1_lt_idx = MAX_REF_BUFS + 1;
ps_dpb_info[i].i4_frame_num = u4_cur_pic_num;
ps_dpb_info[i].ps_pic_buf->i4_frame_num = u4_cur_pic_num;
/* update the head node of linked list to point to the cur Pic */
ps_dpb_mgr->ps_dpb_st_head = ps_dpb_info + i;
// Increment Short term bufCount
ps_dpb_mgr->u1_num_st_ref_bufs++;
/* Identify the picture as a short term picture buffer */
ps_pic_buf->u1_is_short = IS_SHORT_TERM;
if((u1_picture_type & 0x03) == FRM_PIC)
{
ps_dpb_info[i].u1_used_as_ref = IS_SHORT_TERM;
ps_dpb_info[i].s_top_field.u1_reference_info = IS_SHORT_TERM;
ps_dpb_info[i].s_bot_field.u1_reference_info = IS_SHORT_TERM;
}
if((u1_picture_type & 0x03) == TOP_FLD)
ps_dpb_info[i].s_top_field.u1_reference_info = IS_SHORT_TERM;
if((u1_picture_type & 0x03) == BOT_FLD)
ps_dpb_info[i].s_bot_field.u1_reference_info = IS_SHORT_TERM;
return OK;
}
/*!
**************************************************************************
* \if Function name : ih264d_delete_st_node_or_make_lt \endif
*
* \brief
* Delete short term ref with a given picNum from the ST linked list or
* make it an LT node
*
* \return
* 0 - if successful; -1 - otherwise
*
* \note
* Common parts to MMCO==1 and MMCO==3 have been combined here
**************************************************************************
*/
WORD32 ih264d_delete_st_node_or_make_lt(dpb_manager_t *ps_dpb_mgr,
WORD32 i4_pic_num,
UWORD32 u4_lt_idx,
UWORD8 u1_fld_pic_flag)
{
WORD32 i;
struct dpb_info_t *ps_next_dpb;
WORD32 i4_frame_num = i4_pic_num;
struct dpb_info_t *ps_unmark_node = NULL;
UWORD8 u1_del_node = 0, u1_del_st = 0;
UWORD8 u1_reference_type = UNUSED_FOR_REF;
WORD32 ret;
if(u1_fld_pic_flag)
{
i4_frame_num = i4_frame_num >> 1;
if(u4_lt_idx == (MAX_REF_BUFS + 1))
u1_reference_type = UNUSED_FOR_REF;
else
u1_reference_type = IS_LONG_TERM;
}
//Find the node with matching picNum
ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
if((WORD32)ps_next_dpb->i4_frame_num == i4_frame_num)
{
ps_unmark_node = ps_next_dpb;
}
else
{
for(i = 1; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
{
if((WORD32)ps_next_dpb->ps_prev_short->i4_frame_num == i4_frame_num)
break;
ps_next_dpb = ps_next_dpb->ps_prev_short;
}
if(i == ps_dpb_mgr->u1_num_st_ref_bufs)
{
if(ps_dpb_mgr->u1_num_gaps)
{
ret = ih264d_delete_gap_frm_mmco(ps_dpb_mgr, i4_frame_num, &u1_del_st);
if(ret != OK)
return ret;
}
else
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
if(u1_del_st)
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
else
{
return 0;
}
}
else
ps_unmark_node = ps_next_dpb->ps_prev_short;
}
if(u1_fld_pic_flag)
{
/* Mark the corresponding field ( top or bot) as */
/* UNUSED_FOR_REF or IS_LONG_TERM depending on */
/* u1_reference_type. */
if(ps_unmark_node->s_top_field.i4_pic_num == i4_pic_num)
{
ps_unmark_node->s_top_field.u1_reference_info = u1_reference_type;
ps_unmark_node->s_top_field.u1_long_term_frame_idx = u4_lt_idx;
{
UWORD8 *pu1_src = ps_unmark_node->ps_pic_buf->pu1_col_zero_flag;
WORD32 i4_size = ((ps_dpb_mgr->u2_pic_wd
* ps_dpb_mgr->u2_pic_ht) >> 5);
/* memset the colocated zero u4_flag buffer */
memset(pu1_src, 0, i4_size);
}
}
else if(ps_unmark_node->s_bot_field.i4_pic_num == i4_pic_num)
{
ps_unmark_node->s_bot_field.u1_reference_info = u1_reference_type;
ps_unmark_node->s_bot_field.u1_long_term_frame_idx = u4_lt_idx;
{
UWORD8 *pu1_src =
ps_unmark_node->ps_pic_buf->pu1_col_zero_flag
+ ((ps_dpb_mgr->u2_pic_wd
* ps_dpb_mgr->u2_pic_ht)
>> 5);
WORD32 i4_size = ((ps_dpb_mgr->u2_pic_wd
* ps_dpb_mgr->u2_pic_ht) >> 5);
/* memset the colocated zero u4_flag buffer */
memset(pu1_src, 0, i4_size);
}
}
ps_unmark_node->u1_used_as_ref =
ps_unmark_node->s_top_field.u1_reference_info
| ps_unmark_node->s_bot_field.u1_reference_info;
}
else
{
ps_unmark_node->u1_used_as_ref = UNUSED_FOR_REF;
ps_unmark_node->s_top_field.u1_reference_info = UNUSED_FOR_REF;
ps_unmark_node->s_bot_field.u1_reference_info = UNUSED_FOR_REF;
{
UWORD8 *pu1_src = ps_unmark_node->ps_pic_buf->pu1_col_zero_flag;
WORD32 i4_size = ((ps_dpb_mgr->u2_pic_wd
* ps_dpb_mgr->u2_pic_ht) >> 4);
/* memset the colocated zero u4_flag buffer */
memset(pu1_src, 0, i4_size);
}
}
if(!(ps_unmark_node->u1_used_as_ref & IS_SHORT_TERM))
{
if(ps_unmark_node == ps_dpb_mgr->ps_dpb_st_head)
ps_dpb_mgr->ps_dpb_st_head = ps_next_dpb->ps_prev_short;
else
ps_next_dpb->ps_prev_short = ps_unmark_node->ps_prev_short; //update link
ps_dpb_mgr->u1_num_st_ref_bufs--; //decrement ST buf count
u1_del_node = 1;
}
if(u4_lt_idx == MAX_REF_BUFS + 1)
{
if(u1_del_node)
{
// Release the physical buffer
ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
ps_unmark_node->u1_buf_id);
ps_unmark_node->ps_prev_short = NULL;
}
}
else
{
WORD32 i4_status;
//If another node has the same LT index, delete that node
ret = ih264d_delete_lt_node(ps_dpb_mgr, u4_lt_idx,
u1_fld_pic_flag, ps_unmark_node, &i4_status);
if(ret != OK)
return ret;
// Now insert the short term node as a long term node
ret = ih264d_insert_lt_node(ps_dpb_mgr, ps_unmark_node, u4_lt_idx,
u1_fld_pic_flag);
if(ret != OK)
return ret;
}
return OK;
}
/*!
**************************************************************************
* \if Function name : ih264d_reset_ref_bufs \endif
*
* \brief
* Called if MMCO==5/7 or on the first slice of an IDR picture
*
* \return
* none
**************************************************************************
*/
void ih264d_reset_ref_bufs(dpb_manager_t *ps_dpb_mgr)
{
WORD32 i;
struct dpb_info_t *ps_dpb_info = ps_dpb_mgr->as_dpb_info;
for(i = 0; i < MAX_REF_BUFS; i++)
{
if(ps_dpb_info[i].u1_used_as_ref)
{
ps_dpb_info[i].u1_used_as_ref = UNUSED_FOR_REF;
ps_dpb_info[i].u1_lt_idx = MAX_REF_BUFS + 1;
ps_dpb_info[i].ps_prev_short = NULL;
ps_dpb_info[i].ps_prev_long = NULL;
ps_dpb_info[i].ps_pic_buf = NULL;
ps_dpb_info[i].s_top_field.u1_reference_info = UNUSED_FOR_REF;
ps_dpb_info[i].s_bot_field.u1_reference_info = UNUSED_FOR_REF;
ps_dpb_info[i].s_top_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
ps_dpb_info[i].s_bot_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
//Release physical buffer
ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
ps_dpb_info[i].u1_buf_id);
}
}
ps_dpb_mgr->u1_num_st_ref_bufs = ps_dpb_mgr->u1_num_lt_ref_bufs = 0;
ps_dpb_mgr->ps_dpb_st_head = NULL;
ps_dpb_mgr->ps_dpb_ht_head = NULL;
ps_dpb_mgr->u1_mmco_error_in_seq = 0;
/* release all gaps */
ps_dpb_mgr->u1_num_gaps = 0;
for(i = 0; i < MAX_FRAMES; i++)
{
ps_dpb_mgr->ai4_gaps_start_frm_num[i] = INVALID_FRAME_NUM;
ps_dpb_mgr->ai4_gaps_end_frm_num[i] = 0;
ps_dpb_mgr->ai1_gaps_per_seq[i] = 0;
}
}
/*!
**************************************************************************
* \if Function name : Name \endif
*
* \brief
* create the default index list after an MMCO
*
* \return
* 0 - if no_error; -1 - error
*
**************************************************************************
*/
WORD32 ih264d_update_default_index_list(dpb_manager_t *ps_dpb_mgr)
{
WORD32 i;
struct dpb_info_t *ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
{
ps_dpb_mgr->ps_def_dpb[i] = ps_next_dpb->ps_pic_buf;
ps_next_dpb = ps_next_dpb->ps_prev_short;
}
ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
for(;i< ps_dpb_mgr->u1_num_st_ref_bufs + ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
{
ps_dpb_mgr->ps_def_dpb[i] = ps_next_dpb->ps_pic_buf;
ps_next_dpb = ps_next_dpb->ps_prev_long;
}
return 0;
}
/*!
**************************************************************************
* \if Function name : ref_idx_reordering \endif
*
* \brief
* Parse the bitstream and reorder indices for the current slice
*
* \return
* 0 - if no_error; -1 - error
*
* \note
* Called only if ref_idx_reordering_flag_l0 is decoded as 1
* Remove error checking for unmatching picNum or LTIndex later (if not needed)
* \para
* This section implements 7.3.3.1 and 8.2.6.4
* Uses the default index list as the starting point and
* remaps the picNums sent to the next higher index in the
* modified list. The unmodified ones are copied from the
* default to modified list retaining their order in the default list.
*
**************************************************************************
*/
WORD32 ih264d_ref_idx_reordering(dec_struct_t *ps_dec, UWORD8 uc_lx)
{
dpb_manager_t *ps_dpb_mgr = ps_dec->ps_dpb_mgr;
UWORD16 u4_cur_pic_num = ps_dec->ps_cur_slice->u2_frame_num;
/*< Maximum Picture Number Minus 1 */
UWORD16 ui_max_frame_num =
ps_dec->ps_cur_sps->u2_u4_max_pic_num_minus1 + 1;
WORD32 i, count = 0;
UWORD32 ui_remapIdc, ui_nextUev;
WORD16 u2_pred_frame_num = u4_cur_pic_num;
WORD32 i_temp;
UWORD16 u2_def_mod_flag = 0; /* Flag to keep track of which indices have been remapped */
UWORD8 modCount = 0;
UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;
dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
UWORD8 u1_field_pic_flag = ps_cur_slice->u1_field_pic_flag;
if(u1_field_pic_flag)
{
u4_cur_pic_num = u4_cur_pic_num * 2 + 1;
ui_max_frame_num = ui_max_frame_num * 2;
}
u2_pred_frame_num = u4_cur_pic_num;
ui_remapIdc = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
while((ui_remapIdc != 3)
&& (count < ps_cur_slice->u1_num_ref_idx_lx_active[uc_lx]))
{
ui_nextUev = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
if(ui_remapIdc != 2)
{
if(ui_nextUev > ui_max_frame_num)
return ERROR_DBP_MANAGER_T;
ui_nextUev = ui_nextUev + 1;
if(ui_remapIdc == 0)
{
// diffPicNum is -ve
i_temp = (WORD32)u2_pred_frame_num - (WORD32)ui_nextUev;
if(i_temp < 0)
i_temp += ui_max_frame_num;
}
else
{
// diffPicNum is +ve
i_temp = (WORD32)u2_pred_frame_num + (WORD32)ui_nextUev;
if(i_temp >= ui_max_frame_num)
i_temp -= ui_max_frame_num;
}
/* Find the dpb with the matching picNum (picNum==frameNum for framePic) */
if(i_temp > u4_cur_pic_num)
i_temp = i_temp - ui_max_frame_num;
for(i = 0; i < (ps_cur_slice->u1_initial_list_size[uc_lx]); i++)
{
if(ps_dpb_mgr->ps_init_dpb[uc_lx][i]->i4_pic_num == i_temp)
break;
}
if(i == (ps_cur_slice->u1_initial_list_size[uc_lx]))
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
u2_def_mod_flag |= (1 << i);
ps_dpb_mgr->ps_mod_dpb[uc_lx][modCount++] =
ps_dpb_mgr->ps_init_dpb[uc_lx][i];
u2_pred_frame_num = i_temp; //update predictor to be the picNum just obtained
}
else //2
{
UWORD8 u1_lt_idx;
if(ui_nextUev > (MAX_REF_BUFS + 1))
return ERROR_DBP_MANAGER_T;
u1_lt_idx = (UWORD8)ui_nextUev;
for(i = 0; i < (ps_cur_slice->u1_initial_list_size[uc_lx]); i++)
{
if(!ps_dpb_mgr->ps_init_dpb[uc_lx][i]->u1_is_short)
{
if(ps_dpb_mgr->ps_init_dpb[uc_lx][i]->u1_long_term_pic_num
== u1_lt_idx)
break;
}
}
if(i == (ps_cur_slice->u1_initial_list_size[uc_lx]))
{
UWORD32 i4_error_code;
i4_error_code = ERROR_DBP_MANAGER_T;
return i4_error_code;
}
u2_def_mod_flag |= (1 << i);
ps_dpb_mgr->ps_mod_dpb[uc_lx][modCount++] =
ps_dpb_mgr->ps_init_dpb[uc_lx][i];
}
ui_remapIdc = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
/* Get the remapping_idc - 0/1/2/3 */
count++;
}
//Handle the ref indices that were not remapped
for(i = 0; i < (ps_cur_slice->u1_num_ref_idx_lx_active[uc_lx]); i++)
{
if(!(u2_def_mod_flag & (1 << i)))
ps_dpb_mgr->ps_mod_dpb[uc_lx][modCount++] =
ps_dpb_mgr->ps_init_dpb[uc_lx][i];
}
return OK;
}
/*!
**************************************************************************
* \if Function name : ih264d_read_mmco_commands \endif
*
* \brief
* Parses MMCO commands and stores them in a structure for later use.
*
* \return
* 0 - No error; -1 - Error
*
* \note
* This function stores MMCO commands in structure only for the first time.
* In case of MMCO commands being issued for same Picture Number, they are
* just parsed and not stored them in the structure.
*
**************************************************************************
*/
WORD32 ih264d_read_mmco_commands(struct _DecStruct * ps_dec)
{
dec_pic_params_t *ps_pps = ps_dec->ps_cur_pps;
dec_seq_params_t *ps_sps = ps_pps->ps_sps;
dec_bit_stream_t *ps_bitstrm = ps_dec->ps_bitstrm;
dpb_commands_t *ps_dpb_cmds = &(ps_dec->s_dpb_cmds_scratch);
dec_slice_params_t * ps_slice = ps_dec->ps_cur_slice;
WORD32 j;
UWORD8 u1_buf_mode;
struct MMCParams *ps_mmc_params;
UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
UWORD32 u4_bit_ofst = ps_dec->ps_bitstrm->u4_ofst;
ps_slice->u1_mmco_equalto5 = 0;
{
if(ps_dec->u1_nal_unit_type == IDR_SLICE_NAL)
{
ps_slice->u1_no_output_of_prior_pics_flag =
ih264d_get_bit_h264(ps_bitstrm);
COPYTHECONTEXT("SH: no_output_of_prior_pics_flag",
ps_slice->u1_no_output_of_prior_pics_flag);
ps_slice->u1_long_term_reference_flag = ih264d_get_bit_h264(
ps_bitstrm);
COPYTHECONTEXT("SH: long_term_reference_flag",
ps_slice->u1_long_term_reference_flag);
ps_dpb_cmds->u1_idr_pic = 1;
ps_dpb_cmds->u1_no_output_of_prior_pics_flag =
ps_slice->u1_no_output_of_prior_pics_flag;
ps_dpb_cmds->u1_long_term_reference_flag =
ps_slice->u1_long_term_reference_flag;
}
else
{
u1_buf_mode = ih264d_get_bit_h264(ps_bitstrm); //0 - sliding window; 1 - arbitrary
COPYTHECONTEXT("SH: adaptive_ref_pic_buffering_flag", u1_buf_mode);
ps_dpb_cmds->u1_buf_mode = u1_buf_mode;
j = 0;
if(u1_buf_mode == 1)
{
UWORD32 u4_mmco;
UWORD32 u4_diff_pic_num;
UWORD32 u4_lt_idx, u4_max_lt_idx_plus1;
u4_mmco = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
while(u4_mmco != END_OF_MMCO)
{
if (j >= MAX_REF_BUFS)
{
ps_dpb_cmds->u1_num_of_commands = 0;
return -1;
}
ps_mmc_params = &ps_dpb_cmds->as_mmc_params[j];
ps_mmc_params->u4_mmco = u4_mmco;
switch(u4_mmco)
{
case MARK_ST_PICNUM_AS_NONREF:
u4_diff_pic_num = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
//Get absDiffPicnumMinus1
ps_mmc_params->u4_diff_pic_num = u4_diff_pic_num;
break;
case MARK_LT_INDEX_AS_NONREF:
u4_lt_idx = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
ps_mmc_params->u4_lt_idx = u4_lt_idx;
break;
case MARK_ST_PICNUM_AS_LT_INDEX:
u4_diff_pic_num = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
ps_mmc_params->u4_diff_pic_num = u4_diff_pic_num;
u4_lt_idx = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
ps_mmc_params->u4_lt_idx = u4_lt_idx;
break;
case SET_MAX_LT_INDEX:
{
u4_max_lt_idx_plus1 = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
if (u4_max_lt_idx_plus1 > ps_sps->u1_num_ref_frames)
{
/* Invalid max LT ref index */
return -1;
}
ps_mmc_params->u4_max_lt_idx_plus1 = u4_max_lt_idx_plus1;
break;
}
case RESET_REF_PICTURES:
{
ps_slice->u1_mmco_equalto5 = 1;
break;
}
case SET_LT_INDEX:
u4_lt_idx = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
ps_mmc_params->u4_lt_idx = u4_lt_idx;
break;
default:
break;
}
u4_mmco = ih264d_uev(pu4_bitstrm_ofst,
pu4_bitstrm_buf);
j++;
}
ps_dpb_cmds->u1_num_of_commands = j;
}
}
ps_dpb_cmds->u1_dpb_commands_read = 1;
ps_dpb_cmds->u1_dpb_commands_read_slc = 1;
}
u4_bit_ofst = ps_dec->ps_bitstrm->u4_ofst - u4_bit_ofst;
return u4_bit_ofst;
}
/*!
**************************************************************************
* \if Function name : ih264d_do_mmco_buffer \endif
*
* \brief
* Perform decoded picture buffer memory management control operations
*
* \return
* 0 - No error; -1 - Error
*
* \note
* Bitstream is also parsed here to get the MMCOs
*
**************************************************************************
*/
WORD32 ih264d_do_mmco_buffer(dpb_commands_t *ps_dpb_cmds,
dpb_manager_t *ps_dpb_mgr,
UWORD8 u1_numRef_frames_for_seq, /*!< num_ref_frames from active SeqParSet*/
UWORD32 u4_cur_pic_num,
UWORD32 u2_u4_max_pic_num_minus1,
UWORD8 u1_nal_unit_type,
struct pic_buffer_t *ps_pic_buf,
UWORD8 u1_buf_id,
UWORD8 u1_fld_pic_flag,
UWORD8 u1_curr_pic_in_err)
{