blob: 8c6bb1929c2e0debd830d0bc190434da7d64acb9 [file] [log] [blame]
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301/******************************************************************************
2 *
3 * Copyright (C) 2015 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19*/
Naveen Kumar Ponnusamy943323f2015-12-04 16:51:43 +053020#include "log/log.h"
21#include <cutils/log.h>
22
Hamsalekha S8d3d3032015-03-13 21:24:58 +053023#include "ih264_typedefs.h"
24#include "ih264_macros.h"
25#include "ih264_platform_macros.h"
26#include "iv.h"
27#include "ih264d_dpb_manager.h"
28#include "ih264d_bitstrm.h"
29#include "ih264d_parse_cavlc.h"
30#include "ih264d_defs.h"
31#include "ih264d_structs.h"
32#include "ih264d_process_bslice.h"
33#include "ih264d_debug.h"
34#include "ih264d_tables.h"
35#include "ih264d_error_handler.h"
36#include "string.h"
37#include "ih264d_defs.h"
38#include "ih264_error.h"
39#include "ih264_buf_mgr.h"
40#include "assert.h"
41
42/*!
43 ***************************************************************************
44 * \file ih264d_dpb_mgr.c
45 *
46 * \brief
47 * Functions for managing the decoded picture buffer
48 *
49 * Detailed_description
50 *
51 * \date
52 * 19-12-2002
53 *
54 * \author Sriram Sethuraman
55 ***************************************************************************
56 */
57
58/*!
59 **************************************************************************
60 * \if Function name : ih264d_init_ref_bufs \endif
61 *
62 * \brief
63 * Called at the start for initialization.
64 *
65 * \return
66 * none
67 **************************************************************************
68 */
69void ih264d_init_ref_bufs(dpb_manager_t *ps_dpb_mgr)
70{
71 UWORD32 i;
72 struct dpb_info_t *ps_dpb_info = ps_dpb_mgr->as_dpb_info;
73 for(i = 0; i < MAX_REF_BUFS; i++)
74 {
75 ps_dpb_info[i].u1_used_as_ref = UNUSED_FOR_REF;
76 ps_dpb_info[i].u1_lt_idx = MAX_REF_BUFS + 1;
77 ps_dpb_info[i].ps_prev_short = NULL;
78 ps_dpb_info[i].ps_prev_long = NULL;
79 ps_dpb_info[i].ps_pic_buf = NULL;
80 ps_dpb_info[i].s_top_field.u1_reference_info = UNUSED_FOR_REF;
81 ps_dpb_info[i].s_bot_field.u1_reference_info = UNUSED_FOR_REF;
82 ps_dpb_info[i].s_top_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
83 ps_dpb_info[i].s_bot_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
84
85 }
86 ps_dpb_mgr->u1_num_st_ref_bufs = ps_dpb_mgr->u1_num_lt_ref_bufs = 0;
87 ps_dpb_mgr->ps_dpb_st_head = NULL;
88 ps_dpb_mgr->ps_dpb_ht_head = NULL;
89 ps_dpb_mgr->i1_gaps_deleted = 0;
90 ps_dpb_mgr->i1_poc_buf_id_entries = 0;
91
92 ps_dpb_mgr->u1_num_gaps = 0;
93 for(i = 0; i < MAX_FRAMES; i++)
94 {
95 ps_dpb_mgr->ai4_gaps_start_frm_num[i] = INVALID_FRAME_NUM;
96 ps_dpb_mgr->ai4_gaps_end_frm_num[i] = 0;
97 ps_dpb_mgr->ai1_gaps_per_seq[i] = 0;
98 ps_dpb_mgr->ai4_poc_buf_id_map[i][0] = -1;
99 ps_dpb_mgr->ai4_poc_buf_id_map[i][1] = 0x7fffffff;
100 ps_dpb_mgr->ai4_poc_buf_id_map[i][2] = 0;
101 }
102
103}
104
105void ih264d_free_ref_pic_mv_bufs(void* pv_dec, UWORD8 pic_buf_id)
106{
107 dec_struct_t *ps_dec = (dec_struct_t *)pv_dec;
108
109 if((pic_buf_id == ps_dec->u1_pic_buf_id) &&
110 ps_dec->ps_cur_slice->u1_field_pic_flag &&
111 (ps_dec->u1_top_bottom_decoded == 0))
112 {
113 return;
114 }
115
116 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
117 pic_buf_id,
118 BUF_MGR_REF);
119 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
120 ps_dec->au1_pic_buf_id_mv_buf_id_map[pic_buf_id],
121 BUF_MGR_REF);
122}
123/*!
124 **************************************************************************
125 * \if Function name : ih264d_delete_lt_node \endif
126 *
127 * \brief
128 * Delete a buffer with a long term index from the LT linked list
129 *
130 * \return
131 * none
132 **************************************************************************
133 */
134WORD32 ih264d_delete_lt_node(dpb_manager_t *ps_dpb_mgr,
135 UWORD32 u4_lt_idx,
136 UWORD8 u1_fld_pic_flag,
137 struct dpb_info_t *ps_lt_node_to_insert,
138 WORD32 *pi4_status)
139{
140 *pi4_status = 0;
141 if(ps_dpb_mgr->u1_num_lt_ref_bufs > 0)
142 {
143 WORD32 i;
144 struct dpb_info_t *ps_next_dpb;
145 /* ps_unmark_node points to the node to be removed */
146 /* from long term list. */
147 struct dpb_info_t *ps_unmark_node;
148 //Find the node with matching LTIndex
149 ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
150 if(ps_next_dpb->u1_lt_idx == u4_lt_idx)
151 {
152 ps_unmark_node = ps_next_dpb;
153 }
154 else
155 {
156 for(i = 1; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
157 {
158 if(ps_next_dpb->ps_prev_long->u1_lt_idx == u4_lt_idx)
159 break;
160 ps_next_dpb = ps_next_dpb->ps_prev_long;
161 }
162 if(i == ps_dpb_mgr->u1_num_lt_ref_bufs)
163 *pi4_status = 1;
164 else
165 ps_unmark_node = ps_next_dpb->ps_prev_long;
166 }
167
168 if(*pi4_status == 0)
169 {
170 if(u1_fld_pic_flag)
171 {
172 if(ps_lt_node_to_insert != ps_unmark_node)
173 {
174 UWORD8 u1_deleted = 0;
175 /* for the ps_unmark_node mark the corresponding field */
176 /* field as unused for reference */
177
178 if(ps_unmark_node->s_top_field.u1_long_term_frame_idx
179 == u4_lt_idx)
180 {
181 ps_unmark_node->s_top_field.u1_reference_info =
182 UNUSED_FOR_REF;
183 ps_unmark_node->s_top_field.u1_long_term_frame_idx =
184 MAX_REF_BUFS + 1;
185 u1_deleted = 1;
186 }
187 if(ps_unmark_node->s_bot_field.u1_long_term_frame_idx
188 == u4_lt_idx)
189 {
190 ps_unmark_node->s_bot_field.u1_reference_info =
191 UNUSED_FOR_REF;
192 ps_unmark_node->s_bot_field.u1_long_term_frame_idx =
193 MAX_REF_BUFS + 1;
194 u1_deleted = 1;
195 }
196
197 if(!u1_deleted)
198 {
199
200 UWORD32 i4_error_code;
201 i4_error_code = ERROR_DBP_MANAGER_T;
202
203 return i4_error_code;
204 }
205 }
206
207 ps_unmark_node->u1_used_as_ref =
208 ps_unmark_node->s_top_field.u1_reference_info
209 | ps_unmark_node->s_bot_field.u1_reference_info;
210 }
211 else
212 ps_unmark_node->u1_used_as_ref = UNUSED_FOR_REF;
213
214 if(UNUSED_FOR_REF == ps_unmark_node->u1_used_as_ref)
215 {
216 if(ps_unmark_node == ps_dpb_mgr->ps_dpb_ht_head)
217 ps_dpb_mgr->ps_dpb_ht_head = ps_next_dpb->ps_prev_long;
218
219 ps_unmark_node->u1_lt_idx = MAX_REF_BUFS + 1;
220 ps_unmark_node->s_top_field.u1_reference_info =
221 UNUSED_FOR_REF;
222 ps_unmark_node->s_bot_field.u1_reference_info =
223 UNUSED_FOR_REF;
224 // Release the physical buffer
225 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
226 ps_unmark_node->u1_buf_id);
227 ps_next_dpb->ps_prev_long = ps_unmark_node->ps_prev_long; //update link
228 ps_unmark_node->ps_prev_long = NULL;
229 ps_dpb_mgr->u1_num_lt_ref_bufs--; //decrement LT buf count
230 }
231 }
232 }
233 return OK;
234}
235
236/*!
237 **************************************************************************
238 * \if Function name : ih264d_insert_lt_node \endif
239 *
240 * \brief
241 * Insert a buffer into the LT linked list at a given LT index
242 *
243 * \return
244 * none
245 **************************************************************************
246 */
247WORD32 ih264d_insert_lt_node(dpb_manager_t *ps_dpb_mgr,
248 struct dpb_info_t *ps_mov_node,
249 UWORD32 u4_lt_idx,
250 UWORD8 u1_fld_pic_flag)
251{
252 UWORD8 u1_mark_top_field_long_term = 0;
253 UWORD8 u1_mark_bot_field_long_term = 0;
254
255 {
256 if(u1_fld_pic_flag)
257 {
258 /* Assign corresponding field (top or bottom) long_term_frame_idx */
259
260 if((ps_mov_node->s_top_field.u1_reference_info == IS_LONG_TERM)
261 && (ps_mov_node->s_bot_field.u1_reference_info
262 == IS_LONG_TERM))
263 {
264 if(ps_mov_node->u1_lt_idx == u4_lt_idx)
265 u1_mark_bot_field_long_term = 1;
266 else
267 {
268
269 UWORD32 i4_error_code;
270 i4_error_code = ERROR_DBP_MANAGER_T;
271
272 return i4_error_code;
273
274 }
275 }
276 else if(ps_mov_node->s_top_field.u1_reference_info == IS_LONG_TERM)
277 {
278 u1_mark_top_field_long_term = 1;
279 }
280
281 if(!(u1_mark_top_field_long_term || u1_mark_bot_field_long_term))
282 {
283 UWORD32 i4_error_code;
284 i4_error_code = ERROR_DBP_MANAGER_T;
285 return i4_error_code;
286 }
287 }
288 else
289 {
290 ps_mov_node->s_top_field.u1_reference_info = IS_LONG_TERM;
291 ps_mov_node->s_bot_field.u1_reference_info = IS_LONG_TERM;
292 ps_mov_node->s_top_field.u1_long_term_frame_idx = u4_lt_idx;
293 ps_mov_node->s_bot_field.u1_long_term_frame_idx = u4_lt_idx;
Hamsalekha S46e96d42017-02-21 15:47:02 +0530294 u1_mark_bot_field_long_term = 1;
295 u1_mark_top_field_long_term = 1;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530296 }
297
298 ps_mov_node->u1_lt_idx = u4_lt_idx; //Assign the LT index to the node
299 ps_mov_node->ps_pic_buf->u1_long_term_frm_idx = u4_lt_idx;
300 ps_mov_node->u1_used_as_ref = IS_LONG_TERM;
301
302 /* Insert the new long term in the LT list with u4_lt_idx */
303 /* in ascending order. */
304 if(ps_dpb_mgr->u1_num_lt_ref_bufs > 0)
305 {
306 struct dpb_info_t *ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
307 if(u4_lt_idx < ps_next_dpb->u1_lt_idx)
308 {
309 //LTIndex to be inserted is the smallest LT index
310 //Update head and point prev to the next higher index
311 ps_mov_node->ps_prev_long = ps_next_dpb;
312 ps_dpb_mgr->ps_dpb_ht_head = ps_mov_node;
313 }
314 else
315 {
316 WORD32 i;
317 struct dpb_info_t *ps_nxtDPB = ps_next_dpb;
318 ps_next_dpb = ps_next_dpb->ps_prev_long;
319 for(i = 1; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
320 {
321 if(ps_next_dpb->u1_lt_idx > u4_lt_idx)
322 break;
323 ps_nxtDPB = ps_next_dpb;
324 ps_next_dpb = ps_next_dpb->ps_prev_long;
325 }
326
327 ps_nxtDPB->ps_prev_long = ps_mov_node;
328 ps_mov_node->ps_prev_long = ps_next_dpb;
329 }
330 }
331 else
332 {
333 ps_dpb_mgr->ps_dpb_ht_head = ps_mov_node;
334 ps_mov_node->ps_prev_long = NULL;
335 }
336 /* Identify the picture buffer as a long term picture buffer */
337 ps_mov_node->ps_pic_buf->u1_is_short = 0;
338
339 /* Increment LT buf count only if new LT node inserted */
340 /* If Increment during top_field is done, don't increment */
341 /* for bottom field, as both them are part of same pic. */
Hamsalekha S46e96d42017-02-21 15:47:02 +0530342 if(u1_mark_bot_field_long_term)
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530343 ps_dpb_mgr->u1_num_lt_ref_bufs++;
344
345 }
346 return OK;
347}
348
349/*!
350 **************************************************************************
351 * \if Function name : ih264d_insert_st_node \endif
352 *
353 * \brief
354 * Adds a short term reference picture into the ST linked list
355 *
356 * \return
357 * None
358 *
359 * \note
360 * Called only for a new coded picture with nal_ref_idc!=0
361 **************************************************************************
362 */
363WORD32 ih264d_insert_st_node(dpb_manager_t *ps_dpb_mgr,
364 struct pic_buffer_t *ps_pic_buf,
365 UWORD8 u1_buf_id,
366 UWORD32 u4_cur_pic_num)
367{
368 WORD32 i;
369 struct dpb_info_t *ps_dpb_info = ps_dpb_mgr->as_dpb_info;
370 UWORD8 u1_picture_type = ps_pic_buf->u1_picturetype;
371 /* Find an unused dpb location */
372 for(i = 0; i < MAX_REF_BUFS; i++)
373 {
374 if((ps_dpb_info[i].ps_pic_buf == ps_pic_buf)
375 && ps_dpb_info[i].u1_used_as_ref)
376 {
377 /* Can occur only for field bottom pictures */
378 ps_dpb_info[i].s_bot_field.u1_reference_info = IS_SHORT_TERM;
Harish Mahendrakar026745e2016-04-26 17:23:03 +0530379
380 /*signal an error in the case of frame pic*/
381 if(ps_dpb_info[i].ps_pic_buf->u1_pic_type == FRM_PIC)
382 {
383 return ERROR_DBP_MANAGER_T;
384 }
385 else
386 {
387 return OK;
388 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530389 }
390
391 if((ps_dpb_info[i].u1_used_as_ref == UNUSED_FOR_REF)
392 && (ps_dpb_info[i].s_top_field.u1_reference_info
393 == UNUSED_FOR_REF)
394 && (ps_dpb_info[i].s_bot_field.u1_reference_info
395 == UNUSED_FOR_REF))
396 break;
397 }
398 if(i == MAX_REF_BUFS)
399 {
400 UWORD32 i4_error_code;
401 i4_error_code = ERROR_DBP_MANAGER_T;
402 return i4_error_code;
403 }
404
405 /* Create dpb info */
406 ps_dpb_info[i].ps_pic_buf = ps_pic_buf;
407 ps_dpb_info[i].ps_prev_short = ps_dpb_mgr->ps_dpb_st_head;
408 ps_dpb_info[i].u1_buf_id = u1_buf_id;
409 ps_dpb_info[i].u1_used_as_ref = TRUE;
410 ps_dpb_info[i].u1_lt_idx = MAX_REF_BUFS + 1;
411 ps_dpb_info[i].i4_frame_num = u4_cur_pic_num;
412 ps_dpb_info[i].ps_pic_buf->i4_frame_num = u4_cur_pic_num;
413
414 /* update the head node of linked list to point to the cur Pic */
415 ps_dpb_mgr->ps_dpb_st_head = ps_dpb_info + i;
416
417 // Increment Short term bufCount
418 ps_dpb_mgr->u1_num_st_ref_bufs++;
419 /* Identify the picture as a short term picture buffer */
420 ps_pic_buf->u1_is_short = IS_SHORT_TERM;
421
422 if((u1_picture_type & 0x03) == FRM_PIC)
423 {
424 ps_dpb_info[i].u1_used_as_ref = IS_SHORT_TERM;
425 ps_dpb_info[i].s_top_field.u1_reference_info = IS_SHORT_TERM;
426 ps_dpb_info[i].s_bot_field.u1_reference_info = IS_SHORT_TERM;
427 }
428
429 if((u1_picture_type & 0x03) == TOP_FLD)
430 ps_dpb_info[i].s_top_field.u1_reference_info = IS_SHORT_TERM;
431
432 if((u1_picture_type & 0x03) == BOT_FLD)
433 ps_dpb_info[i].s_bot_field.u1_reference_info = IS_SHORT_TERM;
434
435 return OK;
436}
437
438/*!
439 **************************************************************************
440 * \if Function name : ih264d_delete_st_node_or_make_lt \endif
441 *
442 * \brief
443 * Delete short term ref with a given picNum from the ST linked list or
444 * make it an LT node
445 *
446 * \return
447 * 0 - if successful; -1 - otherwise
448 *
449 * \note
450 * Common parts to MMCO==1 and MMCO==3 have been combined here
451 **************************************************************************
452 */
453WORD32 ih264d_delete_st_node_or_make_lt(dpb_manager_t *ps_dpb_mgr,
454 WORD32 i4_pic_num,
455 UWORD32 u4_lt_idx,
456 UWORD8 u1_fld_pic_flag)
457{
458 WORD32 i;
459 struct dpb_info_t *ps_next_dpb;
460 WORD32 i4_frame_num = i4_pic_num;
461 struct dpb_info_t *ps_unmark_node = NULL;
462 UWORD8 u1_del_node = 0, u1_del_st = 0;
463 UWORD8 u1_reference_type = UNUSED_FOR_REF;
464 WORD32 ret;
465
466 if(u1_fld_pic_flag)
467 {
468 i4_frame_num = i4_frame_num >> 1;
469
470 if(u4_lt_idx == (MAX_REF_BUFS + 1))
471 u1_reference_type = UNUSED_FOR_REF;
472 else
473 u1_reference_type = IS_LONG_TERM;
474 }
475
476 //Find the node with matching picNum
477 ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
478 if((WORD32)ps_next_dpb->i4_frame_num == i4_frame_num)
479 {
480 ps_unmark_node = ps_next_dpb;
481 }
482 else
483 {
484 for(i = 1; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
485 {
486 if((WORD32)ps_next_dpb->ps_prev_short->i4_frame_num == i4_frame_num)
487 break;
488 ps_next_dpb = ps_next_dpb->ps_prev_short;
489 }
490
491 if(i == ps_dpb_mgr->u1_num_st_ref_bufs)
492 {
493 if(ps_dpb_mgr->u1_num_gaps)
494 {
495 ret = ih264d_delete_gap_frm_mmco(ps_dpb_mgr, i4_frame_num, &u1_del_st);
496 if(ret != OK)
497 return ret;
498 }
499 else
500 {
501 UWORD32 i4_error_code;
502 i4_error_code = ERROR_DBP_MANAGER_T;
503
504 return i4_error_code;
505 }
506
507 if(u1_del_st)
508 {
509 UWORD32 i4_error_code;
510 i4_error_code = ERROR_DBP_MANAGER_T;
511 return i4_error_code;
512 }
513 else
514 {
515 return 0;
516 }
517 }
518 else
519 ps_unmark_node = ps_next_dpb->ps_prev_short;
520 }
521
522 if(u1_fld_pic_flag)
523 {
524 /* Mark the corresponding field ( top or bot) as */
525 /* UNUSED_FOR_REF or IS_LONG_TERM depending on */
526 /* u1_reference_type. */
527 if(ps_unmark_node->s_top_field.i4_pic_num == i4_pic_num)
528 {
529 ps_unmark_node->s_top_field.u1_reference_info = u1_reference_type;
530 ps_unmark_node->s_top_field.u1_long_term_frame_idx = u4_lt_idx;
531 {
532 UWORD8 *pu1_src = ps_unmark_node->ps_pic_buf->pu1_col_zero_flag;
533 WORD32 i4_size = ((ps_dpb_mgr->u2_pic_wd
534 * ps_dpb_mgr->u2_pic_ht) >> 5);
535 /* memset the colocated zero u4_flag buffer */
536 memset(pu1_src, 0, i4_size);
537 }
538 }
539
540 else if(ps_unmark_node->s_bot_field.i4_pic_num == i4_pic_num)
541 {
542
543 ps_unmark_node->s_bot_field.u1_reference_info = u1_reference_type;
544 ps_unmark_node->s_bot_field.u1_long_term_frame_idx = u4_lt_idx;
545 {
546 UWORD8 *pu1_src =
547 ps_unmark_node->ps_pic_buf->pu1_col_zero_flag
548 + ((ps_dpb_mgr->u2_pic_wd
549 * ps_dpb_mgr->u2_pic_ht)
550 >> 5);
551 WORD32 i4_size = ((ps_dpb_mgr->u2_pic_wd
552 * ps_dpb_mgr->u2_pic_ht) >> 5);
553 /* memset the colocated zero u4_flag buffer */
554 memset(pu1_src, 0, i4_size);
555 }
556 }
557 ps_unmark_node->u1_used_as_ref =
558 ps_unmark_node->s_top_field.u1_reference_info
559 | ps_unmark_node->s_bot_field.u1_reference_info;
560 }
561 else
562 {
563 ps_unmark_node->u1_used_as_ref = UNUSED_FOR_REF;
564 ps_unmark_node->s_top_field.u1_reference_info = UNUSED_FOR_REF;
565 ps_unmark_node->s_bot_field.u1_reference_info = UNUSED_FOR_REF;
566
567 {
568 UWORD8 *pu1_src = ps_unmark_node->ps_pic_buf->pu1_col_zero_flag;
569
570 WORD32 i4_size = ((ps_dpb_mgr->u2_pic_wd
571 * ps_dpb_mgr->u2_pic_ht) >> 4);
572 /* memset the colocated zero u4_flag buffer */
573 memset(pu1_src, 0, i4_size);
574 }
575 }
576
577 if(!(ps_unmark_node->u1_used_as_ref & IS_SHORT_TERM))
578 {
579 if(ps_unmark_node == ps_dpb_mgr->ps_dpb_st_head)
580 ps_dpb_mgr->ps_dpb_st_head = ps_next_dpb->ps_prev_short;
581 else
582 ps_next_dpb->ps_prev_short = ps_unmark_node->ps_prev_short; //update link
583 ps_dpb_mgr->u1_num_st_ref_bufs--; //decrement ST buf count
584 u1_del_node = 1;
585 }
586
587 if(u4_lt_idx == MAX_REF_BUFS + 1)
588 {
589 if(u1_del_node)
590 {
591 // Release the physical buffer
592 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
593 ps_unmark_node->u1_buf_id);
594 ps_unmark_node->ps_prev_short = NULL;
595 }
596 }
597 else
598 {
599 WORD32 i4_status;
600 //If another node has the same LT index, delete that node
601 ret = ih264d_delete_lt_node(ps_dpb_mgr, u4_lt_idx,
602 u1_fld_pic_flag, ps_unmark_node, &i4_status);
603 if(ret != OK)
604 return ret;
605 // Now insert the short term node as a long term node
606 ret = ih264d_insert_lt_node(ps_dpb_mgr, ps_unmark_node, u4_lt_idx,
607 u1_fld_pic_flag);
608 if(ret != OK)
609 return ret;
610 }
611 return OK;
612}
613/*!
614 **************************************************************************
615 * \if Function name : ih264d_reset_ref_bufs \endif
616 *
617 * \brief
618 * Called if MMCO==5/7 or on the first slice of an IDR picture
619 *
620 * \return
621 * none
622 **************************************************************************
623 */
624void ih264d_reset_ref_bufs(dpb_manager_t *ps_dpb_mgr)
625{
626 WORD32 i;
627 struct dpb_info_t *ps_dpb_info = ps_dpb_mgr->as_dpb_info;
628
629 for(i = 0; i < MAX_REF_BUFS; i++)
630 {
631 if(ps_dpb_info[i].u1_used_as_ref)
632 {
633 ps_dpb_info[i].u1_used_as_ref = UNUSED_FOR_REF;
634 ps_dpb_info[i].u1_lt_idx = MAX_REF_BUFS + 1;
635 ps_dpb_info[i].ps_prev_short = NULL;
636 ps_dpb_info[i].ps_prev_long = NULL;
637 ps_dpb_info[i].ps_pic_buf = NULL;
638 ps_dpb_info[i].s_top_field.u1_reference_info = UNUSED_FOR_REF;
639 ps_dpb_info[i].s_bot_field.u1_reference_info = UNUSED_FOR_REF;
640 ps_dpb_info[i].s_top_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
641 ps_dpb_info[i].s_bot_field.u1_long_term_frame_idx = MAX_REF_BUFS + 1;
642
643 //Release physical buffer
644 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
645 ps_dpb_info[i].u1_buf_id);
646 }
647 }
648 ps_dpb_mgr->u1_num_st_ref_bufs = ps_dpb_mgr->u1_num_lt_ref_bufs = 0;
649 ps_dpb_mgr->ps_dpb_st_head = NULL;
650 ps_dpb_mgr->ps_dpb_ht_head = NULL;
651
652 /* release all gaps */
653 ps_dpb_mgr->u1_num_gaps = 0;
654 for(i = 0; i < MAX_FRAMES; i++)
655 {
656 ps_dpb_mgr->ai4_gaps_start_frm_num[i] = INVALID_FRAME_NUM;
657 ps_dpb_mgr->ai4_gaps_end_frm_num[i] = 0;
658 ps_dpb_mgr->ai1_gaps_per_seq[i] = 0;
659 }
660}
661
662/*!
663 **************************************************************************
664 * \if Function name : Name \endif
665 *
666 * \brief
667 * create the default index list after an MMCO
668 *
669 * \return
670 * 0 - if no_error; -1 - error
671 *
672 **************************************************************************
673 */
674WORD32 ih264d_update_default_index_list(dpb_manager_t *ps_dpb_mgr)
675{
676 WORD32 i;
677 struct dpb_info_t *ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
678
679 for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
680 {
681 ps_dpb_mgr->ps_def_dpb[i] = ps_next_dpb->ps_pic_buf;
682 ps_next_dpb = ps_next_dpb->ps_prev_short;
683 }
684
685 ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
686 for(;i< ps_dpb_mgr->u1_num_st_ref_bufs + ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
687 {
688 ps_dpb_mgr->ps_def_dpb[i] = ps_next_dpb->ps_pic_buf;
689 ps_next_dpb = ps_next_dpb->ps_prev_long;
690 }
691 return 0;
692}
693
694/*!
695 **************************************************************************
696 * \if Function name : ref_idx_reordering \endif
697 *
698 * \brief
699 * Parse the bitstream and reorder indices for the current slice
700 *
701 * \return
702 * 0 - if no_error; -1 - error
703 *
704 * \note
705 * Called only if ref_idx_reordering_flag_l0 is decoded as 1
706 * Remove error checking for unmatching picNum or LTIndex later (if not needed)
707 * \para
708 * This section implements 7.3.3.1 and 8.2.6.4
709 * Uses the default index list as the starting point and
710 * remaps the picNums sent to the next higher index in the
711 * modified list. The unmodified ones are copied from the
712 * default to modified list retaining their order in the default list.
713 *
714 **************************************************************************
715 */
716WORD32 ih264d_ref_idx_reordering(dec_struct_t *ps_dec, UWORD8 uc_lx)
717{
718 dpb_manager_t *ps_dpb_mgr = ps_dec->ps_dpb_mgr;
719 UWORD16 u4_cur_pic_num = ps_dec->ps_cur_slice->u2_frame_num;
720 /*< Maximum Picture Number Minus 1 */
721 UWORD16 ui_max_frame_num =
722 ps_dec->ps_cur_sps->u2_u4_max_pic_num_minus1 + 1;
723
Ritu Baldwa7ea47d52017-11-28 18:38:18 +0530724 WORD32 i, count = 0;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530725 UWORD32 ui_remapIdc, ui_nextUev;
726 WORD16 u2_pred_frame_num = u4_cur_pic_num;
727 WORD32 i_temp;
728 UWORD16 u2_def_mod_flag = 0; /* Flag to keep track of which indices have been remapped */
729 UWORD8 modCount = 0;
730 UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
731 UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;
732 dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
733 UWORD8 u1_field_pic_flag = ps_cur_slice->u1_field_pic_flag;
734
735 if(u1_field_pic_flag)
736 {
737 u4_cur_pic_num = u4_cur_pic_num * 2 + 1;
738 ui_max_frame_num = ui_max_frame_num * 2;
739 }
740
741 u2_pred_frame_num = u4_cur_pic_num;
742
743 ui_remapIdc = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
744
Ritu Baldwa7ea47d52017-11-28 18:38:18 +0530745 while((ui_remapIdc != 3)
746 && (count < ps_cur_slice->u1_num_ref_idx_lx_active[uc_lx]))
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530747 {
748 ui_nextUev = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
749 if(ui_remapIdc != 2)
750 {
751 ui_nextUev = ui_nextUev + 1;
752 if(ui_remapIdc == 0)
753 {
754 // diffPicNum is -ve
755 i_temp = u2_pred_frame_num - ui_nextUev;
756 if(i_temp < 0)
757 i_temp += ui_max_frame_num;
758 }
759 else
760 {
761 // diffPicNum is +ve
762 i_temp = u2_pred_frame_num + ui_nextUev;
763 if(i_temp >= ui_max_frame_num)
764 i_temp -= ui_max_frame_num;
765 }
766 /* Find the dpb with the matching picNum (picNum==frameNum for framePic) */
767
768 if(i_temp > u4_cur_pic_num)
769 i_temp = i_temp - ui_max_frame_num;
770
771 for(i = 0; i < (ps_cur_slice->u1_initial_list_size[uc_lx]); i++)
772 {
773 if(ps_dpb_mgr->ps_init_dpb[uc_lx][i]->i4_pic_num == i_temp)
774 break;
775 }
776 if(i == (ps_cur_slice->u1_initial_list_size[uc_lx]))
777 {
778 UWORD32 i4_error_code;
779 i4_error_code = ERROR_DBP_MANAGER_T;
780 return i4_error_code;
781 }
782
783 u2_def_mod_flag |= (1 << i);
784 ps_dpb_mgr->ps_mod_dpb[uc_lx][modCount++] =
785 ps_dpb_mgr->ps_init_dpb[uc_lx][i];
786 u2_pred_frame_num = i_temp; //update predictor to be the picNum just obtained
787 }
788 else //2
789 {
790 UWORD8 u1_lt_idx = (UWORD8)ui_nextUev;
791
792 for(i = 0; i < (ps_cur_slice->u1_initial_list_size[uc_lx]); i++)
793 {
794 if(!ps_dpb_mgr->ps_init_dpb[uc_lx][i]->u1_is_short)
795 {
796 if(ps_dpb_mgr->ps_init_dpb[uc_lx][i]->u1_long_term_pic_num
797 == u1_lt_idx)
798 break;
799 }
800 }
801 if(i == (ps_cur_slice->u1_initial_list_size[uc_lx]))
802 {
803 UWORD32 i4_error_code;
804 i4_error_code = ERROR_DBP_MANAGER_T;
805 return i4_error_code;
806 }
807
808 u2_def_mod_flag |= (1 << i);
809 ps_dpb_mgr->ps_mod_dpb[uc_lx][modCount++] =
810 ps_dpb_mgr->ps_init_dpb[uc_lx][i];
811 }
812
813 ui_remapIdc = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
814 /* Get the remapping_idc - 0/1/2/3 */
Ritu Baldwa7ea47d52017-11-28 18:38:18 +0530815 count++;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530816 }
817
818 //Handle the ref indices that were not remapped
819 for(i = 0; i < (ps_cur_slice->u1_num_ref_idx_lx_active[uc_lx]); i++)
820 {
821 if(!(u2_def_mod_flag & (1 << i)))
822 ps_dpb_mgr->ps_mod_dpb[uc_lx][modCount++] =
823 ps_dpb_mgr->ps_init_dpb[uc_lx][i];
824 }
825 return OK;
826}
827/*!
828 **************************************************************************
829 * \if Function name : ih264d_read_mmco_commands \endif
830 *
831 * \brief
832 * Parses MMCO commands and stores them in a structure for later use.
833 *
834 * \return
835 * 0 - No error; -1 - Error
836 *
837 * \note
838 * This function stores MMCO commands in structure only for the first time.
839 * In case of MMCO commands being issued for same Picture Number, they are
840 * just parsed and not stored them in the structure.
841 *
842 **************************************************************************
843 */
844WORD32 ih264d_read_mmco_commands(struct _DecStruct * ps_dec)
845{
846 dec_bit_stream_t *ps_bitstrm = ps_dec->ps_bitstrm;
Ritu Baldwa3c70b9a2017-10-09 13:52:45 +0530847 dpb_commands_t *ps_dpb_cmds = &(ps_dec->s_dpb_cmds_scratch);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530848 dec_slice_params_t * ps_slice = ps_dec->ps_cur_slice;
849 WORD32 j;
850 UWORD8 u1_buf_mode;
851 struct MMCParams *ps_mmc_params;
852 UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
853 UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
854 UWORD32 u4_bit_ofst = ps_dec->ps_bitstrm->u4_ofst;
855
856 ps_slice->u1_mmco_equalto5 = 0;
857 {
858 if(ps_dec->u1_nal_unit_type == IDR_SLICE_NAL)
859 {
860 ps_slice->u1_no_output_of_prior_pics_flag =
861 ih264d_get_bit_h264(ps_bitstrm);
862 COPYTHECONTEXT("SH: no_output_of_prior_pics_flag",
863 ps_slice->u1_no_output_of_prior_pics_flag);
864 ps_slice->u1_long_term_reference_flag = ih264d_get_bit_h264(
865 ps_bitstrm);
866 COPYTHECONTEXT("SH: long_term_reference_flag",
867 ps_slice->u1_long_term_reference_flag);
868 ps_dpb_cmds->u1_idr_pic = 1;
869 ps_dpb_cmds->u1_no_output_of_prior_pics_flag =
870 ps_slice->u1_no_output_of_prior_pics_flag;
871 ps_dpb_cmds->u1_long_term_reference_flag =
872 ps_slice->u1_long_term_reference_flag;
873 }
874 else
875 {
876 u1_buf_mode = ih264d_get_bit_h264(ps_bitstrm); //0 - sliding window; 1 - arbitrary
877 COPYTHECONTEXT("SH: adaptive_ref_pic_buffering_flag", u1_buf_mode);
878 ps_dpb_cmds->u1_buf_mode = u1_buf_mode;
879 j = 0;
880
881 if(u1_buf_mode == 1)
882 {
883 UWORD32 u4_mmco;
884 UWORD32 u4_diff_pic_num;
885 UWORD32 u4_lt_idx, u4_max_lt_idx;
886
887 u4_mmco = ih264d_uev(pu4_bitstrm_ofst,
888 pu4_bitstrm_buf);
889 while(u4_mmco != END_OF_MMCO)
890 {
Naveen Kumar Ponnusamy943323f2015-12-04 16:51:43 +0530891 if (j >= MAX_REF_BUFS)
892 {
893 ALOGE("b/25818142");
894 android_errorWriteLog(0x534e4554, "25818142");
895 ps_dpb_cmds->u1_num_of_commands = 0;
896 return -1;
897 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530898 ps_mmc_params = &ps_dpb_cmds->as_mmc_params[j];
899 ps_mmc_params->u4_mmco = u4_mmco;
900 switch(u4_mmco)
901 {
902 case MARK_ST_PICNUM_AS_NONREF:
903 u4_diff_pic_num = ih264d_uev(pu4_bitstrm_ofst,
904 pu4_bitstrm_buf);
905 //Get absDiffPicnumMinus1
906 ps_mmc_params->u4_diff_pic_num = u4_diff_pic_num;
907 break;
908
909 case MARK_LT_INDEX_AS_NONREF:
910 u4_lt_idx = ih264d_uev(pu4_bitstrm_ofst,
911 pu4_bitstrm_buf);
912 ps_mmc_params->u4_lt_idx = u4_lt_idx;
913 break;
914
915 case MARK_ST_PICNUM_AS_LT_INDEX:
916 u4_diff_pic_num = ih264d_uev(pu4_bitstrm_ofst,
917 pu4_bitstrm_buf);
918 ps_mmc_params->u4_diff_pic_num = u4_diff_pic_num;
919 u4_lt_idx = ih264d_uev(pu4_bitstrm_ofst,
920 pu4_bitstrm_buf);
921 ps_mmc_params->u4_lt_idx = u4_lt_idx;
922 break;
923
924 case SET_MAX_LT_INDEX:
925 {
926 u4_max_lt_idx = ih264d_uev(pu4_bitstrm_ofst,
927 pu4_bitstrm_buf);
928 ps_mmc_params->u4_max_lt_idx_plus1 = u4_max_lt_idx;
929 break;
930 }
931 case RESET_REF_PICTURES:
932 {
933 ps_slice->u1_mmco_equalto5 = 1;
934 break;
935 }
936
937 case SET_LT_INDEX:
938 u4_lt_idx = ih264d_uev(pu4_bitstrm_ofst,
939 pu4_bitstrm_buf);
940 ps_mmc_params->u4_lt_idx = u4_lt_idx;
941 break;
942
943 default:
944 break;
945 }
946 u4_mmco = ih264d_uev(pu4_bitstrm_ofst,
947 pu4_bitstrm_buf);
948
949 j++;
950 }
951 ps_dpb_cmds->u1_num_of_commands = j;
952
953 }
954 }
955 ps_dpb_cmds->u1_dpb_commands_read = 1;
956 ps_dpb_cmds->u1_dpb_commands_read_slc = 1;
957
958 }
959 u4_bit_ofst = ps_dec->ps_bitstrm->u4_ofst - u4_bit_ofst;
960 return u4_bit_ofst;
961}
962
963/*!
964 **************************************************************************
965 * \if Function name : ih264d_do_mmco_buffer \endif
966 *
967 * \brief
968 * Perform decoded picture buffer memory management control operations
969 *
970 * \return
971 * 0 - No error; -1 - Error
972 *
973 * \note
974 * Bitstream is also parsed here to get the MMCOs
975 *
976 **************************************************************************
977 */
978WORD32 ih264d_do_mmco_buffer(dpb_commands_t *ps_dpb_cmds,
979 dpb_manager_t *ps_dpb_mgr,
980 UWORD8 u1_numRef_frames_for_seq, /*!< num_ref_frames from active SeqParSet*/
981 UWORD32 u4_cur_pic_num,
982 UWORD32 u2_u4_max_pic_num_minus1,
983 UWORD8 u1_nal_unit_type,
984 struct pic_buffer_t *ps_pic_buf,
985 UWORD8 u1_buf_id,
986 UWORD8 u1_fld_pic_flag,
987 UWORD8 u1_curr_pic_in_err)
988{
989 WORD32 i;
990 UWORD8 u1_buf_mode, u1_marked_lt;
991 struct dpb_info_t *ps_next_dpb;
992 UWORD8 u1_num_gaps;
993 UWORD8 u1_del_node = 1;
994 UWORD8 u1_insert_st_pic = 1;
995 WORD32 ret;
996 UNUSED(u1_nal_unit_type);
997 UNUSED(u2_u4_max_pic_num_minus1);
998 u1_buf_mode = ps_dpb_cmds->u1_buf_mode; //0 - sliding window; 1 - Adaptive
999 u1_marked_lt = 0;
1000 u1_num_gaps = ps_dpb_mgr->u1_num_gaps;
1001
1002 if(!u1_buf_mode)
1003 {
1004 //Sliding window - implements 8.2.5.3
1005 if((ps_dpb_mgr->u1_num_st_ref_bufs
1006 + ps_dpb_mgr->u1_num_lt_ref_bufs + u1_num_gaps)
1007 == u1_numRef_frames_for_seq)
1008 {
1009 UWORD8 u1_new_node_flag = 1;
1010 if((0 == ps_dpb_mgr->u1_num_st_ref_bufs) && (0 == u1_num_gaps))
1011 {
1012 UWORD32 i4_error_code;
1013 i4_error_code = ERROR_DBP_MANAGER_T;
1014 return i4_error_code;
1015 }
1016
1017 // Chase the links to reach the last but one picNum, if available
1018 ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1019
1020 if(ps_dpb_mgr->u1_num_st_ref_bufs > 1)
1021 {
1022 if(ps_next_dpb->i4_frame_num == (WORD32)u4_cur_pic_num)
1023 {
1024 /* Incase of filed pictures top_field has been allocated */
1025 /* picture buffer and complementary bottom field pair comes */
1026 /* then the sliding window mechanism should not allocate a */
1027 /* new node */
1028 u1_new_node_flag = 0;
1029 }
1030
1031 for(i = 1; i < (ps_dpb_mgr->u1_num_st_ref_bufs - 1); i++)
1032 {
1033 if(ps_next_dpb == NULL)
1034 {
1035 UWORD32 i4_error_code;
1036 i4_error_code = ERROR_DBP_MANAGER_T;
1037 return i4_error_code;
1038 }
1039 if(ps_next_dpb->i4_frame_num == (WORD32)u4_cur_pic_num)
1040 {
1041 /* Incase of field pictures top_field has been allocated */
1042 /* picture buffer and complementary bottom field pair comes */
1043 /* then the sliding window mechanism should not allocate a */
1044 /* new node */
1045 u1_new_node_flag = 0;
1046 }
1047 ps_next_dpb = ps_next_dpb->ps_prev_short;
1048 }
1049
1050 if(ps_next_dpb->ps_prev_short->ps_prev_short != NULL)
1051 {
1052 UWORD32 i4_error_code;
1053 i4_error_code = ERROR_DBP_MANAGER_T;
1054 return i4_error_code;
1055 }
1056
1057 if(u1_new_node_flag)
1058 {
1059 if(u1_num_gaps)
1060 {
1061 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1062 ps_next_dpb->ps_prev_short->i4_frame_num,
1063 &u1_del_node);
1064 if(ret != OK)
1065 return ret;
1066 }
1067
1068 if(u1_del_node)
1069 {
1070 ps_dpb_mgr->u1_num_st_ref_bufs--;
1071 ps_next_dpb->ps_prev_short->u1_used_as_ref =
1072 UNUSED_FOR_REF;
1073 ps_next_dpb->ps_prev_short->s_top_field.u1_reference_info =
1074 UNUSED_FOR_REF;
1075 ps_next_dpb->ps_prev_short->s_bot_field.u1_reference_info =
1076 UNUSED_FOR_REF;
1077 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1078 ps_next_dpb->ps_prev_short->u1_buf_id);
1079 ps_next_dpb->ps_prev_short->ps_pic_buf = NULL;
1080 ps_next_dpb->ps_prev_short = NULL;
1081 }
1082 }
1083 }
1084 else
1085 {
1086 if(ps_dpb_mgr->u1_num_st_ref_bufs)
1087 {
1088 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1089 ps_next_dpb->i4_frame_num,
1090 &u1_del_node);
1091 if(ret != OK)
1092 return ret;
1093 if((ps_next_dpb->i4_frame_num != (WORD32)u4_cur_pic_num)
1094 && u1_del_node)
1095 {
1096 ps_dpb_mgr->u1_num_st_ref_bufs--;
1097 ps_next_dpb->u1_used_as_ref = FALSE;
1098 ps_next_dpb->s_top_field.u1_reference_info =
1099 UNUSED_FOR_REF;
1100 ps_next_dpb->s_bot_field.u1_reference_info =
1101 UNUSED_FOR_REF;
1102 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1103 ps_next_dpb->u1_buf_id);
1104 ps_next_dpb->ps_pic_buf = NULL;
1105 ps_next_dpb->ps_prev_short = NULL;
1106 ps_dpb_mgr->ps_dpb_st_head = NULL;
1107 ps_next_dpb = NULL;
1108 }
1109 else if(ps_next_dpb->i4_frame_num == (WORD32)u4_cur_pic_num)
1110 {
1111 if(u1_curr_pic_in_err)
1112 {
1113 u1_insert_st_pic = 0;
1114 }
1115 else if(ps_dpb_mgr->u1_num_st_ref_bufs > 0)
1116 {
1117 ps_dpb_mgr->u1_num_st_ref_bufs--;
1118 ps_next_dpb->u1_used_as_ref = FALSE;
1119 ps_next_dpb->s_top_field.u1_reference_info =
1120 UNUSED_FOR_REF;
1121 ps_next_dpb->s_bot_field.u1_reference_info =
1122 UNUSED_FOR_REF;
1123 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1124 ps_next_dpb->u1_buf_id);
1125 ps_next_dpb->ps_pic_buf = NULL;
1126 ps_next_dpb = NULL;
1127 }
1128 }
1129 }
1130 else
1131 {
1132 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1133 INVALID_FRAME_NUM,
1134 &u1_del_node);
1135 if(ret != OK)
1136 return ret;
1137 if(u1_del_node)
1138 {
1139 UWORD32 i4_error_code;
1140 i4_error_code = ERROR_DBP_MANAGER_T;
1141 return i4_error_code;
1142 }
1143 }
1144 }
1145 }
1146 }
1147 else
1148 {
1149 //Adaptive memory control - implements 8.2.5.4
1150 UWORD32 u4_mmco;
1151 UWORD32 u4_diff_pic_num;
1152 WORD32 i4_pic_num;
1153 UWORD32 u4_lt_idx;
1154 WORD32 j;
1155 struct MMCParams *ps_mmc_params;
1156
1157 for(j = 0; j < ps_dpb_cmds->u1_num_of_commands; j++)
1158 {
1159 ps_mmc_params = &ps_dpb_cmds->as_mmc_params[j];
1160 u4_mmco = ps_mmc_params->u4_mmco; //Get MMCO
1161
1162 switch(u4_mmco)
1163 {
1164 case MARK_ST_PICNUM_AS_NONREF:
1165 {
1166
1167 {
1168 UWORD32 i4_cur_pic_num = u4_cur_pic_num;
1169 u4_diff_pic_num = ps_mmc_params->u4_diff_pic_num; //Get absDiffPicnumMinus1
1170 if(u1_fld_pic_flag)
1171 i4_cur_pic_num = i4_cur_pic_num * 2 + 1;
1172 i4_pic_num = i4_cur_pic_num - (u4_diff_pic_num + 1);
1173 }
1174
1175 if(ps_dpb_mgr->u1_num_st_ref_bufs > 0)
1176 {
1177 ret = ih264d_delete_st_node_or_make_lt(ps_dpb_mgr,
1178 i4_pic_num,
1179 MAX_REF_BUFS + 1,
1180 u1_fld_pic_flag);
1181 if(ret != OK)
1182 return ret;
1183 }
1184 else
1185 {
1186 UWORD8 u1_dummy;
1187 ret = ih264d_delete_gap_frm_mmco(ps_dpb_mgr, i4_pic_num, &u1_dummy);
1188 if(ret != OK)
1189 return ret;
1190 }
1191 break;
1192 }
1193 case MARK_LT_INDEX_AS_NONREF:
1194 {
1195 WORD32 i4_status;
1196 u4_lt_idx = ps_mmc_params->u4_lt_idx; //Get long term index
1197 ret = ih264d_delete_lt_node(ps_dpb_mgr,
1198 u4_lt_idx,
1199 u1_fld_pic_flag,
1200 0, &i4_status);
1201 if(ret != OK)
1202 return ret;
1203 if(i4_status)
1204 {
1205 UWORD32 i4_error_code;
1206 i4_error_code = ERROR_DBP_MANAGER_T;
1207 return i4_error_code;
1208 }
1209 break;
1210 }
1211
1212 case MARK_ST_PICNUM_AS_LT_INDEX:
1213 {
1214 {
1215 UWORD32 i4_cur_pic_num = u4_cur_pic_num;
1216 u4_diff_pic_num = ps_mmc_params->u4_diff_pic_num; //Get absDiffPicnumMinus1
1217 if(u1_fld_pic_flag)
1218 i4_cur_pic_num = i4_cur_pic_num * 2 + 1;
1219
1220 i4_pic_num = i4_cur_pic_num - (u4_diff_pic_num + 1);
1221 }
1222
1223 u4_lt_idx = ps_mmc_params->u4_lt_idx; //Get long term index
1224 if(ps_dpb_mgr->u1_num_st_ref_bufs > 0)
1225 {
1226 ret = ih264d_delete_st_node_or_make_lt(ps_dpb_mgr,
1227 i4_pic_num, u4_lt_idx,
1228 u1_fld_pic_flag);
1229 if(ret != OK)
1230 return ret;
1231 }
1232 break;
1233 }
1234 case SET_MAX_LT_INDEX:
1235 {
1236 UWORD8 uc_numLT = ps_dpb_mgr->u1_num_lt_ref_bufs;
1237 u4_lt_idx = ps_mmc_params->u4_max_lt_idx_plus1; //Get Max_long_term_index_plus1
1238 if(u4_lt_idx < ps_dpb_mgr->u1_max_lt_pic_idx_plus1
1239 && uc_numLT > 0)
1240 {
1241 struct dpb_info_t *ps_nxtDPB;
1242 //Set all LT buffers with index >= u4_lt_idx to nonreference
1243 ps_nxtDPB = ps_dpb_mgr->ps_dpb_ht_head;
1244 ps_next_dpb = ps_nxtDPB->ps_prev_long;
1245 if(ps_nxtDPB->u1_lt_idx >= u4_lt_idx)
1246 {
1247 i = 0;
1248 ps_dpb_mgr->ps_dpb_ht_head = NULL;
1249 }
1250 else
1251 {
1252 for(i = 1; i < uc_numLT; i++)
1253 {
1254 if(ps_next_dpb->u1_lt_idx >= u4_lt_idx)
1255 break;
1256 ps_nxtDPB = ps_next_dpb;
1257 ps_next_dpb = ps_next_dpb->ps_prev_long;
1258 }
1259 ps_nxtDPB->ps_prev_long = NULL; //Terminate the link of the closest LTIndex that is <=Max
1260 }
1261 ps_dpb_mgr->u1_num_lt_ref_bufs = i;
1262 if(i == 0)
1263 ps_next_dpb = ps_nxtDPB;
1264
1265 for(; i < uc_numLT; i++)
1266 {
1267 ps_nxtDPB = ps_next_dpb;
1268 ps_nxtDPB->u1_lt_idx = MAX_REF_BUFS + 1;
1269 ps_nxtDPB->u1_used_as_ref = UNUSED_FOR_REF;
1270 ps_nxtDPB->s_top_field.u1_reference_info =
1271 UNUSED_FOR_REF;
1272 ps_nxtDPB->s_bot_field.u1_reference_info =
1273 UNUSED_FOR_REF;
1274
1275 ps_nxtDPB->ps_pic_buf = NULL;
1276 //Release buffer
1277 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1278 ps_nxtDPB->u1_buf_id);
1279 ps_next_dpb = ps_nxtDPB->ps_prev_long;
1280 ps_nxtDPB->ps_prev_long = NULL;
1281 }
1282 }
1283 ps_dpb_mgr->u1_max_lt_pic_idx_plus1 = u4_lt_idx;
1284
1285 break;
1286 }
1287 case SET_LT_INDEX:
1288 {
1289 u4_lt_idx = ps_mmc_params->u4_lt_idx; //Get long term index
1290 ret = ih264d_insert_st_node(ps_dpb_mgr, ps_pic_buf, u1_buf_id,
1291 u4_cur_pic_num);
1292 if(ret != OK)
1293 return ret;
Hamsalekha S41489f92017-02-10 14:47:11 +05301294
1295 if(ps_dpb_mgr->u1_num_st_ref_bufs > 0)
1296
1297 {
1298 ret = ih264d_delete_st_node_or_make_lt(ps_dpb_mgr,
1299 u4_cur_pic_num,
1300 u4_lt_idx,
1301 u1_fld_pic_flag);
1302 if(ret != OK)
1303 return ret;
1304 }
1305 else
1306 {
1307 return ERROR_DBP_MANAGER_T;
1308 }
1309
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301310 u1_marked_lt = 1;
1311 break;
1312 }
1313
1314 default:
1315 break;
1316 }
1317 if(u4_mmco == RESET_REF_PICTURES || u4_mmco == RESET_ALL_PICTURES)
1318 {
1319 ih264d_reset_ref_bufs(ps_dpb_mgr);
1320 u4_cur_pic_num = 0;
1321 }
1322 }
1323 }
1324 if(!u1_marked_lt && u1_insert_st_pic)
1325 {
1326 ret = ih264d_insert_st_node(ps_dpb_mgr, ps_pic_buf, u1_buf_id,
1327 u4_cur_pic_num);
1328 if(ret != OK)
1329 return ret;
1330 }
1331 return OK;
1332}
1333
1334/*****************************************************************************/
1335/* */
1336/* Function Name : ih264d_release_pics_in_dpb */
1337/* */
1338/* Description : This function deletes all pictures from DPB */
1339/* */
1340/* Inputs : h_pic_buf_api: pointer to picture buffer API */
1341/* u1_disp_bufs: number pictures ready for display */
1342/* */
1343/* Globals : None */
1344/* Outputs : None */
1345/* Returns : None */
1346/* */
1347/* Issues : None */
1348/* */
1349/* Revision History: */
1350/* */
1351/* DD MM YYYY Author(s) Changes (Describe the changes made) */
1352/* 22 06 2005 NS Draft */
1353/* */
1354/*****************************************************************************/
1355void ih264d_release_pics_in_dpb(void *pv_dec,
1356 UWORD8 u1_disp_bufs)
1357{
1358 WORD8 i;
1359 dec_struct_t *ps_dec = (dec_struct_t *)pv_dec;
1360
1361 for(i = 0; i < u1_disp_bufs; i++)
1362 {
1363 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
1364 i,
1365 BUF_MGR_REF);
1366 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
1367 ps_dec->au1_pic_buf_id_mv_buf_id_map[i],
1368 BUF_MGR_REF);
1369 }
1370}
1371
1372/*****************************************************************************/
1373/* */
1374/* Function Name : ih264d_delete_gap_frm_sliding */
1375/* */
1376/* Description : This function deletes a picture from the list of gaps, */
1377/* if the frame number of gap frame is lesser than the one */
1378/* to be deleted by sliding window */
1379/* Inputs : ps_dpb_mgr: pointer to dpb manager */
1380/* i4_frame_num: frame number of picture that's going to */
1381/* be deleted by sliding window */
1382/* pu1_del_node: holds 0 if a gap is deleted else 1 */
1383/* Globals : None */
1384/* Processing : Function searches for frame number lesser than */
1385/* i4_frame_num in the gaps list */
1386/* Outputs : None */
1387/* Returns : None */
1388/* */
1389/* Issues : None */
1390/* */
1391/* Revision History: */
1392/* */
1393/* DD MM YYYY Author(s) Changes (Describe the changes made) */
1394/* 22 06 2005 NS Draft */
1395/* */
1396/*****************************************************************************/
1397WORD32 ih264d_delete_gap_frm_sliding(dpb_manager_t *ps_dpb_mgr,
1398 WORD32 i4_frame_num,
1399 UWORD8 *pu1_del_node)
1400{
1401 WORD8 i1_gap_idx, i, j, j_min;
1402 WORD32 *pi4_gaps_start_frm_num, *pi4_gaps_end_frm_num, i4_gap_frame_num;
1403 WORD32 i4_start_frm_num, i4_end_frm_num;
1404 WORD32 i4_max_frm_num;
1405 WORD32 i4_frm_num, i4_gap_frm_num_min;
1406
1407 /* find the least frame num from gaps and current DPB node */
1408 /* Delete the least one */
1409 *pu1_del_node = 1;
1410 if(0 == ps_dpb_mgr->u1_num_gaps)
1411 return OK;
1412 pi4_gaps_start_frm_num = ps_dpb_mgr->ai4_gaps_start_frm_num;
1413 pi4_gaps_end_frm_num = ps_dpb_mgr->ai4_gaps_end_frm_num;
1414 i4_gap_frame_num = INVALID_FRAME_NUM;
1415 i4_max_frm_num = ps_dpb_mgr->i4_max_frm_num;
1416
1417 i1_gap_idx = -1;
1418 if(INVALID_FRAME_NUM != i4_frame_num)
1419 {
1420 i4_gap_frame_num = i4_frame_num;
1421 for(i = 0; i < MAX_FRAMES; i++)
1422 {
1423 i4_start_frm_num = pi4_gaps_start_frm_num[i];
1424 if(INVALID_FRAME_NUM != i4_start_frm_num)
1425 {
1426 i4_end_frm_num = pi4_gaps_end_frm_num[i];
1427 if(i4_end_frm_num < i4_max_frm_num)
1428 {
1429 if(i4_start_frm_num <= i4_gap_frame_num)
1430 {
1431 i4_gap_frame_num = i4_start_frm_num;
1432 i1_gap_idx = i;
1433 }
1434 }
1435 else
1436 {
1437 if(((i4_start_frm_num <= i4_gap_frame_num)
1438 && (i4_gap_frame_num <= i4_max_frm_num))
1439 || ((i4_start_frm_num >= i4_gap_frame_num)
1440 && ((i4_gap_frame_num
1441 + i4_max_frm_num)
1442 >= i4_end_frm_num)))
1443 {
1444 i4_gap_frame_num = i4_start_frm_num;
1445 i1_gap_idx = i;
1446 }
1447 }
1448 }
1449 }
1450 }
1451 else
1452 {
1453 /* no valid short term buffers, delete one gap from the least start */
1454 /* of gap sequence */
1455 i4_gap_frame_num = pi4_gaps_start_frm_num[0];
1456 i1_gap_idx = 0;
1457 for(i = 1; i < MAX_FRAMES; i++)
1458 {
1459 if(INVALID_FRAME_NUM != pi4_gaps_start_frm_num[i])
1460 {
1461 if(pi4_gaps_start_frm_num[i] < i4_gap_frame_num)
1462 {
1463 i4_gap_frame_num = pi4_gaps_start_frm_num[i];
1464 i1_gap_idx = i;
1465 }
1466 }
1467 }
1468 if(INVALID_FRAME_NUM == i4_gap_frame_num)
1469 {
1470 UWORD32 i4_error_code;
1471 i4_error_code = ERROR_DBP_MANAGER_T;
1472 return i4_error_code;
1473 }
1474 }
1475
1476 if(-1 != i1_gap_idx)
1477 {
1478 /* find least frame_num in the poc_map, which is in this range */
1479 i4_start_frm_num = pi4_gaps_start_frm_num[i1_gap_idx];
1480 if(i4_start_frm_num < 0)
1481 i4_start_frm_num += i4_max_frm_num;
1482 i4_end_frm_num = pi4_gaps_end_frm_num[i1_gap_idx];
1483 if(i4_end_frm_num < 0)
1484 i4_end_frm_num += i4_max_frm_num;
1485
1486 i4_gap_frm_num_min = 0xfffffff;
1487 j_min = MAX_FRAMES;
1488 for(j = 0; j < MAX_FRAMES; j++)
1489 {
1490 i4_frm_num = ps_dpb_mgr->ai4_poc_buf_id_map[j][2];
1491 if((i4_start_frm_num <= i4_frm_num)
1492 && (i4_end_frm_num >= i4_frm_num))
1493 {
1494 if(i4_frm_num < i4_gap_frm_num_min)
1495 {
1496 j_min = j;
1497 i4_gap_frm_num_min = i4_frm_num;
1498 }
1499 }
1500 }
1501
1502 if(j_min != MAX_FRAMES)
1503 {
1504
1505 ps_dpb_mgr->ai4_poc_buf_id_map[j_min][0] = -1;
1506 ps_dpb_mgr->ai4_poc_buf_id_map[j_min][1] = 0x7fffffff;
1507 ps_dpb_mgr->ai4_poc_buf_id_map[j_min][2] = GAP_FRAME_NUM;
1508 ps_dpb_mgr->i1_gaps_deleted++;
1509
1510 ps_dpb_mgr->ai1_gaps_per_seq[i1_gap_idx]--;
1511 ps_dpb_mgr->u1_num_gaps--;
1512 *pu1_del_node = 0;
1513 if(0 == ps_dpb_mgr->ai1_gaps_per_seq[i1_gap_idx])
1514 {
1515 ps_dpb_mgr->ai4_gaps_start_frm_num[i1_gap_idx] =
1516 INVALID_FRAME_NUM;
1517 ps_dpb_mgr->ai4_gaps_end_frm_num[i1_gap_idx] = 0;
1518 }
1519 }
1520 }
1521
1522 return OK;
1523}
1524
1525/*****************************************************************************/
1526/* */
1527/* Function Name : ih264d_delete_gap_frm_mmco */
1528/* */
1529/* Description : This function deletes a picture from the list of gaps, */
1530/* if the frame number (specified by mmco commands) to be */
1531/* deleted is in the range by gap sequence. */
1532/* */
1533/* Inputs : ps_dpb_mgr: pointer to dpb manager */
1534/* i4_frame_num: frame number of picture that's going to */
1535/* be deleted by mmco */
1536/* pu1_del_node: holds 0 if a gap is deleted else 1 */
1537/* Globals : None */
1538/* Processing : Function searches for frame number lesser in the range */
1539/* specified by gap sequence */
1540/* Outputs : None */
1541/* Returns : None */
1542/* */
1543/* Issues : None */
1544/* */
1545/* Revision History: */
1546/* */
1547/* DD MM YYYY Author(s) Changes (Describe the changes made) */
1548/* 22 06 2005 NS Draft */
1549/* */
1550/*****************************************************************************/
1551WORD32 ih264d_delete_gap_frm_mmco(dpb_manager_t *ps_dpb_mgr,
1552 WORD32 i4_frame_num,
1553 UWORD8 *pu1_del_node)
1554{
1555 WORD8 i, j;
1556 WORD32 *pi4_start, *pi4_end;
1557 WORD32 i4_start_frm_num, i4_end_frm_num, i4_max_frm_num;
1558
1559 /* find the least frame num from gaps and current DPB node */
1560 /* Delete the gaps */
1561 *pu1_del_node = 1;
1562 pi4_start = ps_dpb_mgr->ai4_gaps_start_frm_num;
1563 pi4_end = ps_dpb_mgr->ai4_gaps_end_frm_num;
1564 i4_max_frm_num = ps_dpb_mgr->i4_max_frm_num;
1565
1566 if(0 == ps_dpb_mgr->u1_num_gaps)
1567 return OK;
1568
1569 if(i4_frame_num < 0)
1570 i4_frame_num += i4_max_frm_num;
1571 for(i = 0; i < MAX_FRAMES; i++)
1572 {
1573 i4_start_frm_num = pi4_start[i];
1574 if(i4_start_frm_num < 0)
1575 i4_start_frm_num += i4_max_frm_num;
1576 if(INVALID_FRAME_NUM != i4_start_frm_num)
1577 {
1578 i4_end_frm_num = pi4_end[i];
1579 if(i4_end_frm_num < 0)
1580 i4_end_frm_num += i4_max_frm_num;
1581
1582 if((i4_frame_num >= i4_start_frm_num)
1583 && (i4_frame_num <= i4_end_frm_num))
1584 {
1585 break;
1586 }
1587 else
1588 {
1589 if(((i4_frame_num + i4_max_frm_num) >= i4_start_frm_num)
1590 && ((i4_frame_num + i4_max_frm_num)
1591 <= i4_end_frm_num))
1592 {
1593 UWORD32 i4_error_code;
1594 i4_error_code = ERROR_DBP_MANAGER_T;
1595 return i4_error_code;
1596 }
1597 }
1598 }
1599 }
1600
1601 /* find frame_num index, in the poc_map which needs to be deleted */
1602 for(j = 0; j < MAX_FRAMES; j++)
1603 {
1604 if(i4_frame_num == ps_dpb_mgr->ai4_poc_buf_id_map[j][2])
1605 break;
1606 }
1607
1608 if(MAX_FRAMES != i)
1609 {
1610 if(j == MAX_FRAMES)
1611 {
1612 UWORD32 i4_error_code;
1613 i4_error_code = ERROR_DBP_MANAGER_T;
1614 return i4_error_code;
1615 }
1616
1617 ps_dpb_mgr->ai4_poc_buf_id_map[j][0] = -1;
1618 ps_dpb_mgr->ai4_poc_buf_id_map[j][1] = 0x7fffffff;
1619 ps_dpb_mgr->ai4_poc_buf_id_map[j][2] = GAP_FRAME_NUM;
1620 ps_dpb_mgr->i1_gaps_deleted++;
1621
1622 ps_dpb_mgr->ai1_gaps_per_seq[i]--;
1623 ps_dpb_mgr->u1_num_gaps--;
1624 *pu1_del_node = 0;
1625 if(0 == ps_dpb_mgr->ai1_gaps_per_seq[i])
1626 {
1627 ps_dpb_mgr->ai4_gaps_start_frm_num[i] = INVALID_FRAME_NUM;
1628 ps_dpb_mgr->ai4_gaps_end_frm_num[i] = 0;
1629 }
1630 }
1631 else
1632 {
1633 UWORD32 i4_error_code;
1634 i4_error_code = ERROR_DBP_MANAGER_T;
1635 return i4_error_code;
1636 }
1637
1638 return OK;
1639}
1640
1641/*!
1642 **************************************************************************
1643 * \if Function name : ih264d_do_mmco_for_gaps \endif
1644 *
1645 * \brief
1646 * Perform decoded picture buffer memory management control operations
1647 *
1648 * \return
1649 * 0 - No error; -1 - Error
1650 *
1651 * \note
1652 * Bitstream is also parsed here to get the MMCOs
1653 *
1654 **************************************************************************
1655 */
1656WORD32 ih264d_do_mmco_for_gaps(dpb_manager_t *ps_dpb_mgr,
1657 UWORD8 u1_num_ref_frames /*!< num_ref_frames from active SeqParSet*/
1658 )
1659{
1660 struct dpb_info_t *ps_next_dpb;
1661 UWORD8 u1_num_gaps;
1662 UWORD8 u1_st_ref_bufs, u1_lt_ref_bufs, u1_del_node;
1663 WORD8 i;
1664 WORD32 i4_frame_gaps = 1;
1665 WORD32 ret;
1666
1667 //Sliding window - implements 8.2.5.3, flush out buffers
1668 u1_st_ref_bufs = ps_dpb_mgr->u1_num_st_ref_bufs;
1669 u1_lt_ref_bufs = ps_dpb_mgr->u1_num_lt_ref_bufs;
1670
1671 while(1)
1672 {
1673 u1_num_gaps = ps_dpb_mgr->u1_num_gaps;
1674 if((u1_st_ref_bufs + u1_lt_ref_bufs + u1_num_gaps + i4_frame_gaps)
1675 > u1_num_ref_frames)
1676 {
1677 if(0 == (u1_st_ref_bufs + u1_num_gaps))
1678 {
1679 i4_frame_gaps = 0;
1680 ps_dpb_mgr->u1_num_gaps = (u1_num_ref_frames
1681 - u1_lt_ref_bufs);
1682 }
1683 else
1684 {
1685 u1_del_node = 1;
1686 ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1687
1688 if(u1_st_ref_bufs > 1)
1689 {
1690 for(i = 1; i < (u1_st_ref_bufs - 1); i++)
1691 {
1692 if(ps_next_dpb == NULL)
1693 {
1694 UWORD32 i4_error_code;
1695 i4_error_code = ERROR_DBP_MANAGER_T;
1696 return i4_error_code;
1697 }
1698 ps_next_dpb = ps_next_dpb->ps_prev_short;
1699 }
1700
1701 if(ps_next_dpb->ps_prev_short->ps_prev_short != NULL)
1702 {
1703 return ERROR_DBP_MANAGER_T;
1704 }
1705
1706 if(u1_num_gaps)
1707 {
1708 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1709 ps_next_dpb->ps_prev_short->i4_frame_num,
1710 &u1_del_node);
1711 if(ret != OK)
1712 return ret;
1713 }
1714
1715 if(u1_del_node)
1716 {
1717 u1_st_ref_bufs--;
1718 ps_next_dpb->ps_prev_short->u1_used_as_ref =
1719 UNUSED_FOR_REF;
1720 ps_next_dpb->ps_prev_short->s_top_field.u1_reference_info =
1721 UNUSED_FOR_REF;
1722 ps_next_dpb->ps_prev_short->s_bot_field.u1_reference_info =
1723 UNUSED_FOR_REF;
1724 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1725 ps_next_dpb->ps_prev_short->u1_buf_id);
1726 ps_next_dpb->ps_prev_short->ps_pic_buf = NULL;
1727 ps_next_dpb->ps_prev_short = NULL;
1728 }
1729 }
1730 else
1731 {
1732 if(u1_st_ref_bufs)
1733 {
1734 if(u1_num_gaps)
1735 {
1736 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1737 ps_next_dpb->i4_frame_num,
1738 &u1_del_node);
1739 if(ret != OK)
1740 return ret;
1741 }
1742
1743 if(u1_del_node)
1744 {
1745 u1_st_ref_bufs--;
1746 ps_next_dpb->u1_used_as_ref = FALSE;
1747 ps_next_dpb->s_top_field.u1_reference_info =
1748 UNUSED_FOR_REF;
1749 ps_next_dpb->s_bot_field.u1_reference_info =
1750 UNUSED_FOR_REF;
1751 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1752 ps_next_dpb->u1_buf_id);
1753 ps_next_dpb->ps_pic_buf = NULL;
1754 ps_next_dpb = NULL;
1755 ps_dpb_mgr->ps_dpb_st_head = NULL;
1756 ps_dpb_mgr->u1_num_st_ref_bufs = u1_st_ref_bufs;
1757 }
1758 }
1759 else
1760 {
1761 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1762 INVALID_FRAME_NUM,
1763 &u1_del_node);
1764 if(ret != OK)
1765 return ret;
1766 if(u1_del_node)
1767 {
1768 return ERROR_DBP_MANAGER_T;
1769 }
1770 }
1771 }
1772 }
1773 }
1774 else
1775 {
1776 ps_dpb_mgr->u1_num_gaps += i4_frame_gaps;
1777 break;
1778 }
1779 }
1780
1781 ps_dpb_mgr->u1_num_st_ref_bufs = u1_st_ref_bufs;
1782
1783 return OK;
1784}
1785/****************************************************************************/
1786/* */
1787/* Function Name : ih264d_free_node_from_dpb */
1788/* */
1789/* Description : */
1790/* */
1791/* Inputs : */
1792/* */
1793/* Globals : */
1794/* */
1795/* Processing : */
1796/* */
1797/* Outputs : */
1798/* */
1799/* Returns : */
1800/* */
1801/* Known Issues : */
1802/* */
1803/* Revision History */
1804/* */
1805/* DD MM YY Author Changes */
1806/* Sarat */
1807/****************************************************************************/
1808/**** Function Added for Error Resilience *****/
1809WORD32 ih264d_free_node_from_dpb(dpb_manager_t *ps_dpb_mgr,
1810 UWORD32 u4_cur_pic_num,
1811 UWORD8 u1_numRef_frames_for_seq)
1812{
1813 WORD32 i;
1814 UWORD8 u1_num_gaps = ps_dpb_mgr->u1_num_gaps;
1815 struct dpb_info_t *ps_next_dpb;
1816 UWORD8 u1_del_node = 1;
1817 WORD32 ret;
1818
1819 //Sliding window - implements 8.2.5.3
1820 if((ps_dpb_mgr->u1_num_st_ref_bufs + ps_dpb_mgr->u1_num_lt_ref_bufs
1821 + u1_num_gaps) == u1_numRef_frames_for_seq)
1822 {
1823 UWORD8 u1_new_node_flag = 1;
1824 if((0 == ps_dpb_mgr->u1_num_st_ref_bufs) && (0 == u1_num_gaps))
1825 {
1826 return ERROR_DBP_MANAGER_T;
1827 }
1828
1829 // Chase the links to reach the last but one picNum, if available
1830 ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1831
1832 if(ps_dpb_mgr->u1_num_st_ref_bufs > 1)
1833 {
1834 if(ps_next_dpb->i4_frame_num == (WORD32)u4_cur_pic_num)
1835 {
1836 /* Incase of filed pictures top_field has been allocated */
1837 /* picture buffer and complementary bottom field pair comes */
1838 /* then the sliding window mechanism should not allocate a */
1839 /* new node */
1840 u1_new_node_flag = 0;
1841 }
1842
1843 for(i = 1; i < (ps_dpb_mgr->u1_num_st_ref_bufs - 1); i++)
1844 {
1845 if(ps_next_dpb == NULL)
1846 return ERROR_DBP_MANAGER_T;
1847
1848 if(ps_next_dpb->i4_frame_num == (WORD32)u4_cur_pic_num)
1849 {
1850 /* Incase of field pictures top_field has been allocated */
1851 /* picture buffer and complementary bottom field pair comes */
1852 /* then the sliding window mechanism should not allocate a */
1853 /* new node */
1854 u1_new_node_flag = 0;
1855 }
1856 ps_next_dpb = ps_next_dpb->ps_prev_short;
1857 }
1858
1859 if(ps_next_dpb->ps_prev_short->ps_prev_short != NULL)
1860 return ERROR_DBP_MANAGER_T;
1861
1862 if(u1_new_node_flag)
1863 {
1864 if(u1_num_gaps)
1865 {
1866 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1867 ps_next_dpb->ps_prev_short->i4_frame_num,
1868 &u1_del_node);
1869 if(ret != OK)
1870 return ret;
1871 }
1872
1873 if(u1_del_node)
1874 {
1875 ps_dpb_mgr->u1_num_st_ref_bufs--;
1876 ps_next_dpb->ps_prev_short->u1_used_as_ref = UNUSED_FOR_REF;
1877 ps_next_dpb->ps_prev_short->s_top_field.u1_reference_info =
1878 UNUSED_FOR_REF;
1879 ps_next_dpb->ps_prev_short->s_bot_field.u1_reference_info =
1880 UNUSED_FOR_REF;
1881 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1882 ps_next_dpb->ps_prev_short->u1_buf_id);
1883 ps_next_dpb->ps_prev_short->ps_pic_buf = NULL;
1884 ps_next_dpb->ps_prev_short = NULL;
1885 }
1886 }
1887 }
1888 else
1889 {
1890 if(ps_dpb_mgr->u1_num_st_ref_bufs)
1891 {
1892 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr,
1893 ps_next_dpb->i4_frame_num,
1894 &u1_del_node);
1895 if(ret != OK)
1896 return ret;
1897 if((ps_next_dpb->i4_frame_num != (WORD32)u4_cur_pic_num)
1898 && u1_del_node)
1899 {
1900 ps_dpb_mgr->u1_num_st_ref_bufs--;
1901 ps_next_dpb->u1_used_as_ref = FALSE;
1902 ps_next_dpb->s_top_field.u1_reference_info = UNUSED_FOR_REF;
1903 ps_next_dpb->s_bot_field.u1_reference_info = UNUSED_FOR_REF;
1904 ih264d_free_ref_pic_mv_bufs(ps_dpb_mgr->pv_codec_handle,
1905 ps_next_dpb->u1_buf_id);
1906 ps_next_dpb->ps_pic_buf = NULL;
1907 ps_next_dpb = NULL;
1908 }
1909 }
1910 else
1911 {
1912 ret = ih264d_delete_gap_frm_sliding(ps_dpb_mgr, INVALID_FRAME_NUM, &u1_del_node);
1913 if(ret != OK)
1914 return ret;
1915 if(u1_del_node)
1916 return ERROR_DBP_MANAGER_T;
1917 }
1918 }
1919 }
1920 return OK;
1921}
1922/*****************************************************************************/
1923/* */
1924/* Function Name : ih264d_delete_nonref_nondisplay_pics */
1925/* */
1926/* Description : */
1927/* */
1928/* */
1929/* Inputs : */
1930/* Globals : */
1931/* Processing : */
1932/* */
1933/* Outputs : */
1934/* Returns : */
1935/* */
1936/* Issues : */
1937/* */
1938/* Revision History: */
1939/* */
1940/* DD MM YYYY Author(s) Changes (Describe the changes made) */
1941/* 05 06 2007 Varun Draft */
1942/* */
1943/*****************************************************************************/
1944
1945void ih264d_delete_nonref_nondisplay_pics(dpb_manager_t *ps_dpb_mgr)
1946{
1947 WORD8 i;
1948 WORD32 (*i4_poc_buf_id_map)[3] = ps_dpb_mgr->ai4_poc_buf_id_map;
1949
1950 /* remove all gaps marked as unused for ref */
1951 for(i = 0; (i < MAX_FRAMES) && ps_dpb_mgr->i1_gaps_deleted; i++)
1952 {
1953 if(GAP_FRAME_NUM == i4_poc_buf_id_map[i][2])
1954 {
1955 ps_dpb_mgr->i1_gaps_deleted--;
1956 ps_dpb_mgr->i1_poc_buf_id_entries--;
1957 i4_poc_buf_id_map[i][0] = -1;
1958 i4_poc_buf_id_map[i][1] = 0x7fffffff;
1959 i4_poc_buf_id_map[i][2] = 0;
1960 }
1961 }
1962}
1963/*****************************************************************************/
1964/* */
1965/* Function Name : ih264d_insert_pic_in_display_list */
1966/* */
1967/* Description : */
1968/* */
1969/* */
1970/* Inputs : */
1971/* Globals : */
1972/* Processing : */
1973/* */
1974/* Outputs : */
1975/* Returns : */
1976/* */
1977/* Issues : */
1978/* */
1979/* Revision History: */
1980/* */
1981/* DD MM YYYY Author(s) Changes (Describe the changes made) */
1982/* 05 06 2007 Varun Draft */
1983/* */
1984/*****************************************************************************/
1985
1986WORD32 ih264d_insert_pic_in_display_list(dpb_manager_t *ps_dpb_mgr,
1987 UWORD8 u1_buf_id,
1988 WORD32 i4_display_poc,
1989 UWORD32 u4_frame_num)
1990{
1991 WORD8 i;
1992 WORD32 (*i4_poc_buf_id_map)[3] = ps_dpb_mgr->ai4_poc_buf_id_map;
1993
1994 for(i = 0; i < MAX_FRAMES; i++)
1995 {
1996 /* Find an empty slot */
1997 if(i4_poc_buf_id_map[i][0] == -1)
1998 {
1999 if(GAP_FRAME_NUM == i4_poc_buf_id_map[i][2])
2000 ps_dpb_mgr->i1_gaps_deleted--;
2001 else
2002 ps_dpb_mgr->i1_poc_buf_id_entries++;
2003
2004 i4_poc_buf_id_map[i][0] = u1_buf_id;
2005 i4_poc_buf_id_map[i][1] = i4_display_poc;
2006 i4_poc_buf_id_map[i][2] = u4_frame_num;
2007
2008 break;
2009 }
2010 }
2011
2012 if(MAX_FRAMES == i)
2013 {
2014
2015 UWORD32 i4_error_code;
2016 i4_error_code = ERROR_GAPS_IN_FRM_NUM;
2017 return i4_error_code;
2018 }
2019 return OK;
2020}
2021