tng_slotorder.c revision 8e9a21e730449c10cac6e6f69d255611c93f63c2
18e9a21e730449c10cac6e6f69d255611c93f63c2hding/*
28e9a21e730449c10cac6e6f69d255611c93f63c2hding * INTEL CONFIDENTIAL
38e9a21e730449c10cac6e6f69d255611c93f63c2hding * Copyright 2007 Intel Corporation. All Rights Reserved.
48e9a21e730449c10cac6e6f69d255611c93f63c2hding *
58e9a21e730449c10cac6e6f69d255611c93f63c2hding * The source code contained or described herein and all documents related to
68e9a21e730449c10cac6e6f69d255611c93f63c2hding * the source code ("Material") are owned by Intel Corporation or its suppliers
78e9a21e730449c10cac6e6f69d255611c93f63c2hding * or licensors. Title to the Material remains with Intel Corporation or its
88e9a21e730449c10cac6e6f69d255611c93f63c2hding * suppliers and licensors. The Material may contain trade secrets and
98e9a21e730449c10cac6e6f69d255611c93f63c2hding * proprietary and confidential information of Intel Corporation and its
108e9a21e730449c10cac6e6f69d255611c93f63c2hding * suppliers and licensors, and is protected by worldwide copyright and trade
118e9a21e730449c10cac6e6f69d255611c93f63c2hding * secret laws and treaty provisions. No part of the Material may be used,
128e9a21e730449c10cac6e6f69d255611c93f63c2hding * copied, reproduced, modified, published, uploaded, posted, transmitted,
138e9a21e730449c10cac6e6f69d255611c93f63c2hding * distributed, or disclosed in any way without Intel's prior express written
148e9a21e730449c10cac6e6f69d255611c93f63c2hding * permission.
158e9a21e730449c10cac6e6f69d255611c93f63c2hding *
168e9a21e730449c10cac6e6f69d255611c93f63c2hding * No license under any patent, copyright, trade secret or other intellectual
178e9a21e730449c10cac6e6f69d255611c93f63c2hding * property right is granted to or conferred upon you by disclosure or delivery
188e9a21e730449c10cac6e6f69d255611c93f63c2hding * of the Materials, either expressly, by implication, inducement, estoppel or
198e9a21e730449c10cac6e6f69d255611c93f63c2hding * otherwise. Any license under such intellectual property rights must be
208e9a21e730449c10cac6e6f69d255611c93f63c2hding * express and approved by Intel in writing.
218e9a21e730449c10cac6e6f69d255611c93f63c2hding */
228e9a21e730449c10cac6e6f69d255611c93f63c2hding
238e9a21e730449c10cac6e6f69d255611c93f63c2hding/*
248e9a21e730449c10cac6e6f69d255611c93f63c2hding * Authors:
258e9a21e730449c10cac6e6f69d255611c93f63c2hding *    Edward Lin <edward.lin@intel.com>
268e9a21e730449c10cac6e6f69d255611c93f63c2hding *
278e9a21e730449c10cac6e6f69d255611c93f63c2hding */
288e9a21e730449c10cac6e6f69d255611c93f63c2hding
298e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <unistd.h>
308e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <stdio.h>
318e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <memory.h>
328e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_drv_video.h"
338e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "ptg_hostheader.h"
348e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "ptg_slotorder.h"
358e9a21e730449c10cac6e6f69d255611c93f63c2hding
368e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic unsigned long long displayingOrder2EncodingOrder(
378e9a21e730449c10cac6e6f69d255611c93f63c2hding    unsigned long long displaying_order,
388e9a21e730449c10cac6e6f69d255611c93f63c2hding    int bframes,
398e9a21e730449c10cac6e6f69d255611c93f63c2hding    int intracnt,
408e9a21e730449c10cac6e6f69d255611c93f63c2hding    int idrcnt)
418e9a21e730449c10cac6e6f69d255611c93f63c2hding{
428e9a21e730449c10cac6e6f69d255611c93f63c2hding    int poc;
438e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (idrcnt != 0)
448e9a21e730449c10cac6e6f69d255611c93f63c2hding        poc = displaying_order % (intracnt * idrcnt + 1);
458e9a21e730449c10cac6e6f69d255611c93f63c2hding    else
468e9a21e730449c10cac6e6f69d255611c93f63c2hding        poc = displaying_order;
478e9a21e730449c10cac6e6f69d255611c93f63c2hding
488e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (poc == 0) //IDR
498e9a21e730449c10cac6e6f69d255611c93f63c2hding        return displaying_order;
508e9a21e730449c10cac6e6f69d255611c93f63c2hding    else if ((poc % (bframes + 1)) == 0) //I or P
518e9a21e730449c10cac6e6f69d255611c93f63c2hding        return (displaying_order - bframes);
528e9a21e730449c10cac6e6f69d255611c93f63c2hding    else
538e9a21e730449c10cac6e6f69d255611c93f63c2hding        return (displaying_order + 1); //B
548e9a21e730449c10cac6e6f69d255611c93f63c2hding}
558e9a21e730449c10cac6e6f69d255611c93f63c2hding
568e9a21e730449c10cac6e6f69d255611c93f63c2hding
578e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic int getSlotIndex(
588e9a21e730449c10cac6e6f69d255611c93f63c2hding    int bframes, int intracnt, int idrcnt,
598e9a21e730449c10cac6e6f69d255611c93f63c2hding    int displaying_order, int encoding_count,
608e9a21e730449c10cac6e6f69d255611c93f63c2hding    FRAME_ORDER_INFO *last_info)
618e9a21e730449c10cac6e6f69d255611c93f63c2hding{
628e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i, slot_idx = 0;
638e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (displaying_order == 0) {
648e9a21e730449c10cac6e6f69d255611c93f63c2hding	for (i = 0; i < (bframes + 2); i++)  {
658e9a21e730449c10cac6e6f69d255611c93f63c2hding	    //encoding order
668e9a21e730449c10cac6e6f69d255611c93f63c2hding	    if (i == 0)
678e9a21e730449c10cac6e6f69d255611c93f63c2hding			last_info->slot_consume_enc_order[0] = 0;
688e9a21e730449c10cac6e6f69d255611c93f63c2hding	    else if (i == 1)
698e9a21e730449c10cac6e6f69d255611c93f63c2hding			last_info->slot_consume_enc_order[bframes + 2 - 1] = 1;
708e9a21e730449c10cac6e6f69d255611c93f63c2hding	    else
718e9a21e730449c10cac6e6f69d255611c93f63c2hding			last_info->slot_consume_enc_order[i - 1] = i;
728e9a21e730449c10cac6e6f69d255611c93f63c2hding	    last_info->slot_consume_dpy_order[i] = i; //displaying order
738e9a21e730449c10cac6e6f69d255611c93f63c2hding	}
748e9a21e730449c10cac6e6f69d255611c93f63c2hding	last_info->slot_consume_dpy_order[0] = bframes + 2;
758e9a21e730449c10cac6e6f69d255611c93f63c2hding        last_info->slot_consume_enc_order[0] =
768e9a21e730449c10cac6e6f69d255611c93f63c2hding	    displayingOrder2EncodingOrder(bframes + 2,
778e9a21e730449c10cac6e6f69d255611c93f63c2hding		    bframes, intracnt, idrcnt);
788e9a21e730449c10cac6e6f69d255611c93f63c2hding	last_info->max_dpy_num = bframes + 2;
798e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else {
808e9a21e730449c10cac6e6f69d255611c93f63c2hding	for (i = 0; i < (bframes + 2); i++) {
818e9a21e730449c10cac6e6f69d255611c93f63c2hding	    if (last_info->slot_consume_enc_order[i] == encoding_count) {
828e9a21e730449c10cac6e6f69d255611c93f63c2hding		slot_idx = i;
838e9a21e730449c10cac6e6f69d255611c93f63c2hding		break;
848e9a21e730449c10cac6e6f69d255611c93f63c2hding	    }
858e9a21e730449c10cac6e6f69d255611c93f63c2hding	}
868e9a21e730449c10cac6e6f69d255611c93f63c2hding	last_info->max_dpy_num++;
878e9a21e730449c10cac6e6f69d255611c93f63c2hding	last_info->slot_consume_dpy_order[slot_idx] = last_info->max_dpy_num;
888e9a21e730449c10cac6e6f69d255611c93f63c2hding	last_info->slot_consume_enc_order[slot_idx] =
898e9a21e730449c10cac6e6f69d255611c93f63c2hding	    displayingOrder2EncodingOrder(last_info->max_dpy_num,
908e9a21e730449c10cac6e6f69d255611c93f63c2hding		    bframes, intracnt, idrcnt);
918e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
928e9a21e730449c10cac6e6f69d255611c93f63c2hding
938e9a21e730449c10cac6e6f69d255611c93f63c2hding    return slot_idx;
948e9a21e730449c10cac6e6f69d255611c93f63c2hding}
958e9a21e730449c10cac6e6f69d255611c93f63c2hding
968e9a21e730449c10cac6e6f69d255611c93f63c2hdingint getFrameDpyOrder(
978e9a21e730449c10cac6e6f69d255611c93f63c2hding    unsigned long long encoding_count, /*Input, the encoding order, start from 0*/
988e9a21e730449c10cac6e6f69d255611c93f63c2hding    int bframes, /*Input, The number of B frames between P and I */
998e9a21e730449c10cac6e6f69d255611c93f63c2hding    int intracnt, /*Input, Intra period*/
1008e9a21e730449c10cac6e6f69d255611c93f63c2hding    int idrcnt, /*INput, IDR period. 0: only one IDR; */
1018e9a21e730449c10cac6e6f69d255611c93f63c2hding    FRAME_ORDER_INFO *p_last_info, /*Input & Output. Reset to 0 on first call*/
1028e9a21e730449c10cac6e6f69d255611c93f63c2hding    unsigned long long *displaying_order, /* Output. The displaying order */
1038e9a21e730449c10cac6e6f69d255611c93f63c2hding    int *frame_type, /*Output. Frame type. 0: I frame. 1: P frame. 2: B frame*/
1048e9a21e730449c10cac6e6f69d255611c93f63c2hding    int *slot) /*Output. The corresponding slot index */
1058e9a21e730449c10cac6e6f69d255611c93f63c2hding{
1068e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
1078e9a21e730449c10cac6e6f69d255611c93f63c2hding    unsigned long long disp_index;
1088e9a21e730449c10cac6e6f69d255611c93f63c2hding    unsigned long long val;
1098e9a21e730449c10cac6e6f69d255611c93f63c2hding
1108e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((intracnt % (bframes + 1)) != 0 || bframes == 0)
1118e9a21e730449c10cac6e6f69d255611c93f63c2hding	return -1;
1128e9a21e730449c10cac6e6f69d255611c93f63c2hding
1138e9a21e730449c10cac6e6f69d255611c93f63c2hding    val = ((idrcnt == 0) ? encoding_count :
1148e9a21e730449c10cac6e6f69d255611c93f63c2hding	    encoding_count % (intracnt * idrcnt + 1));
1158e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((idrcnt == 0 && encoding_count == 0) ||
1168e9a21e730449c10cac6e6f69d255611c93f63c2hding	    (idrcnt != 0 && (encoding_count % (intracnt * idrcnt + 1) == 0))) {
1178e9a21e730449c10cac6e6f69d255611c93f63c2hding	   *frame_type = FRAME_IDR;
1188e9a21e730449c10cac6e6f69d255611c93f63c2hding	   disp_index = encoding_count;
1198e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else if (((val - 1) % (bframes + 1)) != 0) {
1208e9a21e730449c10cac6e6f69d255611c93f63c2hding	*frame_type = FRAME_B;
1218e9a21e730449c10cac6e6f69d255611c93f63c2hding	 disp_index = encoding_count - 1;
1228e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else if (p_last_info->last_frame_type == FRAME_IDR ||
1238e9a21e730449c10cac6e6f69d255611c93f63c2hding	    ((val - 1) / (bframes + 1) % (intracnt / (bframes + 1))) != 0) {
1248e9a21e730449c10cac6e6f69d255611c93f63c2hding	*frame_type = FRAME_P;
1258e9a21e730449c10cac6e6f69d255611c93f63c2hding	disp_index = encoding_count + bframes;
1268e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else {
1278e9a21e730449c10cac6e6f69d255611c93f63c2hding	*frame_type = FRAME_I;
1288e9a21e730449c10cac6e6f69d255611c93f63c2hding	disp_index = encoding_count + bframes;
1298e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1308e9a21e730449c10cac6e6f69d255611c93f63c2hding
1318e9a21e730449c10cac6e6f69d255611c93f63c2hding    *displaying_order = disp_index;
1328e9a21e730449c10cac6e6f69d255611c93f63c2hding    *slot = getSlotIndex(bframes, intracnt, idrcnt,
1338e9a21e730449c10cac6e6f69d255611c93f63c2hding	    disp_index, encoding_count,
1348e9a21e730449c10cac6e6f69d255611c93f63c2hding	    p_last_info);
1358e9a21e730449c10cac6e6f69d255611c93f63c2hding    p_last_info->last_frame_type = *frame_type;
1368e9a21e730449c10cac6e6f69d255611c93f63c2hding    p_last_info->last_slot = *slot;
1378e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
1388e9a21e730449c10cac6e6f69d255611c93f63c2hding}
1398e9a21e730449c10cac6e6f69d255611c93f63c2hding
1408e9a21e730449c10cac6e6f69d255611c93f63c2hding#if 0
1418e9a21e730449c10cac6e6f69d255611c93f63c2hdingint main(int argc, char **argv) {
1428e9a21e730449c10cac6e6f69d255611c93f63c2hding    int bframes, intracnt, frame_num;
1438e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
1448e9a21e730449c10cac6e6f69d255611c93f63c2hding    int displaying_order, frame_type, slot;
1458e9a21e730449c10cac6e6f69d255611c93f63c2hding    int j;
1468e9a21e730449c10cac6e6f69d255611c93f63c2hding     char ac_frame_type[] = {'I', 'P', 'B'};
1478e9a21e730449c10cac6e6f69d255611c93f63c2hding    FRAME_ORDER_INFO last_info;
1488e9a21e730449c10cac6e6f69d255611c93f63c2hding
1498e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (argc != 4)
1508e9a21e730449c10cac6e6f69d255611c93f63c2hding    {
1518e9a21e730449c10cac6e6f69d255611c93f63c2hding	    printf("%s [bframe_number] [intra period] [frame_number]\n", argv[0]);
1528e9a21e730449c10cac6e6f69d255611c93f63c2hding	    return 0;
1538e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1548e9a21e730449c10cac6e6f69d255611c93f63c2hding    else {
1558e9a21e730449c10cac6e6f69d255611c93f63c2hding		bframes = atoi(argv[1]);
1568e9a21e730449c10cac6e6f69d255611c93f63c2hding		intracnt = atoi(argv[2]);
1578e9a21e730449c10cac6e6f69d255611c93f63c2hding		frame_num = atoi(argv[3]);
1588e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1598e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (intracnt % (bframes + 1) != 0) {
1608e9a21e730449c10cac6e6f69d255611c93f63c2hding		printf(" intra count must be a muliple of (bframe_number + 1)\n");
1618e9a21e730449c10cac6e6f69d255611c93f63c2hding		return 0;
1628e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1638e9a21e730449c10cac6e6f69d255611c93f63c2hding
1648e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset(&last_info, 0, sizeof(FRAME_ORDER_INFO));
1658e9a21e730449c10cac6e6f69d255611c93f63c2hding    last_info.slot_consume_dpy_order = (int *)malloc((bframes + 2)  * sizeof(int));
1668e9a21e730449c10cac6e6f69d255611c93f63c2hding    last_info.slot_consume_enc_order = (int *)malloc((bframes + 2)  * sizeof(int));
1678e9a21e730449c10cac6e6f69d255611c93f63c2hding
1688e9a21e730449c10cac6e6f69d255611c93f63c2hding    printf("encodingorder displaying order	frame_type	slot index\n");
1698e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < frame_num; i++) {
1708e9a21e730449c10cac6e6f69d255611c93f63c2hding		getFrameDpyOrder(i, bframes, intracnt, &last_info, &displaying_order, &frame_type, &slot);
1718e9a21e730449c10cac6e6f69d255611c93f63c2hding		printf("%5d\t%5d\t%c\t%d\n", i, displaying_order, ac_frame_type[frame_type], slot);
1728e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1738e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(last_info.slot_consume_dpy_order);
1748e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(last_info.slot_consume_enc_order);
1758e9a21e730449c10cac6e6f69d255611c93f63c2hding
1768e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
1778e9a21e730449c10cac6e6f69d255611c93f63c2hding}
1788e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif //test routine
179