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