~ubuntu-branches/ubuntu/precise/mpeg4ip/precise

« back to all changes in this revision

Viewing changes to common/video/iso-mpeg4/src/sys_encoder_newpenc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-01-12 15:59:56 UTC
  • Revision ID: james.westby@ubuntu.com-20080112155956-1vznw5njidvrh649
Tags: upstream-1.6dfsg
ImportĀ upstreamĀ versionĀ 1.6dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
 
 
3
This software module was originally developed by 
 
4
 
 
5
        Hideaki Kimata (NTT)
 
6
 
 
7
 
 
8
in the course of development of the MPEG-4 Video (ISO/IEC 14496-2) standard.
 
9
This software module is an implementation of a part of one or more MPEG-4 
 
10
Video (ISO/IEC 14496-2) tools as specified by the MPEG-4 Video (ISO/IEC 
 
11
14496-2) standard. 
 
12
 
 
13
ISO/IEC gives users of the MPEG-4 Video (ISO/IEC 14496-2) standard free 
 
14
license to this software module or modifications thereof for use in hardware
 
15
or software products claiming conformance to the MPEG-4 Video (ISO/IEC 
 
16
14496-2) standard. 
 
17
 
 
18
Those intending to use this software module in hardware or software products
 
19
are advised that its use may infringe existing patents. The original 
 
20
developer of this software module and his/her company, the subsequent 
 
21
editors and their companies, and ISO/IEC have no liability for use of this 
 
22
software module or modifications thereof in an implementation. Copyright is 
 
23
not released for non MPEG-4 Video (ISO/IEC 14496-2) standard conforming 
 
24
products. 
 
25
 
 
26
NTT retains full right to use the code for his/her own 
 
27
purpose, assign or donate the code to a third party and to inhibit third 
 
28
parties from using the code for non MPEG-4 Video (ISO/IEC 14496-2) standard
 
29
conforming products. This copyright notice must be included in all copies or
 
30
derivative works. 
 
31
 
 
32
Copyright (c) 1999.
 
33
 
 
34
Module Name:
 
35
 
 
36
        newpenc.cpp
 
37
 
 
38
Abstract:
 
39
 
 
40
        Implementation of the CNewPredEncoder class.
 
41
 
 
42
Revision History:
 
43
 
 
44
**************************************************************************/
 
45
 
 
46
#ifdef __MFC_
 
47
#ifdef _DEBUG
 
48
#undef THIS_FILE
 
49
static char BASED_CODE THIS_FILE[] = __FILE__;
 
50
#endif
 
51
 
 
52
#define new DEBUG_NEW                              
 
53
#endif // __MFC_
 
54
 
 
55
#include "basic.hpp"
 
56
#include "typeapi.h"
 
57
#include "codehead.h"
 
58
#include "mode.hpp"
 
59
#include "global.hpp"
 
60
#include "bitstrm.hpp"
 
61
#include "newpred.hpp"
 
62
#include <string.h>
 
63
 
 
64
CNewPredEncoder::CNewPredEncoder() : CNewPred()
 
65
{
 
66
        m_enumWho = NP_ENCODER;
 
67
}
 
68
 
 
69
CNewPredEncoder::~CNewPredEncoder()
 
70
{
 
71
        endNEWPREDcnt( m_pNewPredControl );
 
72
}
 
73
 
 
74
void CNewPredEncoder::SetObject (
 
75
                                        int                             iNumBitsTimeIncr,
 
76
                                        int                             iWidth,
 
77
                                        int                             iHeight,
 
78
                                        UInt                    uiVOId,
 
79
                                        char*                   pchRefname,
 
80
                                        char*                   pchSlicePointParam,
 
81
                                        Bool                    bNewpredSegmentType,
 
82
                                        int                             iAUsage,
 
83
                                        int                             bShapeOnly,
 
84
                                        CVOPU8YUVBA*    pNPvopcRefQ0,
 
85
                                        CVOPU8YUVBA*    pNPvopcRefQ1,
 
86
                                        CRct                    rctNPFrameY,
 
87
                                        CRct                    rctNPFrameUV
 
88
                                        )
 
89
{
 
90
 
 
91
        m_iNumBitsVopID = iNumBitsTimeIncr + NUMBITS_VOP_ID_PLUS;
 
92
        if( m_iNumBitsVopID > NP_MAX_NUMBITS_VOP_ID )
 
93
                m_iNumBitsVopID = NP_MAX_NUMBITS_VOP_ID;
 
94
        m_iWidth = iWidth;
 
95
        m_iHeight = iHeight;
 
96
        m_iNPNumMBX = m_iWidth/MB_SIZE;
 
97
        m_iNPNumMBY = m_iHeight/MB_SIZE;
 
98
 
 
99
        m_iNumBuffEnc = ENC_BUFCNT;
 
100
        m_bNewPredSegmentType = bNewpredSegmentType;
 
101
 
 
102
        m_iAUsage = iAUsage;
 
103
        m_bShapeOnly = bShapeOnly;
 
104
        
 
105
        m_pNPvopcRefQ0 = pNPvopcRefQ0;          // set reference picture memory
 
106
        m_pNPvopcRefQ1 = pNPvopcRefQ1;          // set original picture memory
 
107
 
 
108
        m_rctNPFrameY = rctNPFrameY;
 
109
        m_rctNPFrameUV = rctNPFrameUV;
 
110
 
 
111
        Int iHeightUV = iHeight/(MB_SIZE/BLOCK_SIZE);
 
112
        Int     iWidthUV  = iWidth/(MB_SIZE/BLOCK_SIZE);
 
113
 
 
114
        if (m_bShapeOnly == FALSE) {
 
115
                m_pchNewPredRefY = new PixelC[(2*EXPANDY_REF_FRAME+iWidth)*(2*EXPANDY_REF_FRAME+iHeight)];
 
116
                m_pchNewPredRefU = new PixelC[(2*EXPANDUV_REF_FRAME+iWidthUV)*(2*EXPANDUV_REF_FRAME+iHeightUV)];
 
117
                m_pchNewPredRefV = new PixelC[(2*EXPANDUV_REF_FRAME+iWidthUV)*(2*EXPANDUV_REF_FRAME+iHeightUV)];
 
118
        }
 
119
        
 
120
        GetSlicePoint(pchSlicePointParam);
 
121
 
 
122
        m_pNewPredControl = initNEWPREDcnt( uiVOId );
 
123
        sprintf( refname, "%s", pchRefname);
 
124
        load_ref_ind(); 
 
125
}
 
126
 
 
127
Int CNewPredEncoder::SetVPData (
 
128
                NP_SYNTAX_TYPE  mode,
 
129
                int             *md_iVopID,     
 
130
                int             *md_iNumBitsVopID,
 
131
                int             *md_iVopID4Prediction_Indication,
 
132
                int             *md_iVopID4Prediction
 
133
                )
 
134
{
 
135
        Int             iPut = 0;
 
136
        static int CurrentSlice = 0;
 
137
 
 
138
        switch( mode )
 
139
        {
 
140
        case NP_VOP_HEADER:
 
141
                CurrentSlice = 0;
 
142
                m_iVopID4Prediction = m_pNewPredControl->ref[CurrentSlice];
 
143
                IncrementVopID(); 
 
144
                iPut += m_iNumBitsVopID;
 
145
                if((m_iVopID - 1) == m_iVopID4Prediction || m_iVopID4Prediction == 0)
 
146
                        m_iVopID4Prediction_Indication = 0;
 
147
                else
 
148
                        m_iVopID4Prediction_Indication = 1;
 
149
                
 
150
                iPut += NUMBITS_VOP_ID_FOR_PREDICTION_INDICATION;
 
151
                if( m_iVopID4Prediction_Indication )
 
152
                {
 
153
                        iPut += m_iNumBitsVopID;
 
154
                }
 
155
                iPut += MARKER_BIT;
 
156
                break;
 
157
        case NP_VP_HEADER:
 
158
                if (m_bNewPredSegmentType == 0)
 
159
                        CurrentSlice ++;
 
160
                m_iVopID4Prediction = m_pNewPredControl->ref[CurrentSlice];
 
161
                iPut += m_iNumBitsVopID;
 
162
 
 
163
                if((m_iVopID - 1) == m_iVopID4Prediction || m_iVopID4Prediction == 0)
 
164
                        m_iVopID4Prediction_Indication = 0;
 
165
                else
 
166
                        m_iVopID4Prediction_Indication = 1;
 
167
 
 
168
                iPut += NUMBITS_VOP_ID_FOR_PREDICTION_INDICATION;
 
169
                if( m_iVopID4Prediction_Indication )
 
170
                {
 
171
                        iPut += m_iNumBitsVopID;
 
172
                }
 
173
                iPut += MARKER_BIT;
 
174
                break;
 
175
        }
 
176
        *md_iVopID = m_iVopID;  
 
177
        *md_iNumBitsVopID = m_iNumBitsVopID;
 
178
        *md_iVopID4Prediction_Indication = m_iVopID4Prediction_Indication;
 
179
        *md_iVopID4Prediction = m_iVopID4Prediction;
 
180
        return iPut;
 
181
}
 
182
 
 
183
/* move from newpred.cpp */
 
184
void  CNewPredEncoder::load_ref_ind()
 
185
{
 
186
        int             i, j;
 
187
        FILE    *read_file;
 
188
        char    buf[256];
 
189
        char    *dtp;
 
190
        int             count=0;
 
191
        int             line=0;
 
192
        int             err_flag = 0;
 
193
        int             int_dt;
 
194
        int             frameNo;
 
195
        int             first = 1;
 
196
 
 
197
        memset(m_pNewPredControl->ref_tbl, 0x00, 0x800*MAX_NumGOB*sizeof(int));
 
198
 
 
199
        if( (read_file = fopen(refname,"r")) == NULL ) {
 
200
                fprintf( stdout,
 
201
                        "Unable to open Ref_Indication_file: %s\n",
 
202
                        refname);
 
203
                exit( -1 );
 
204
        }
 
205
 
 
206
        while( !feof(read_file) ) {
 
207
                line++;
 
208
 
 
209
                if( first ) {
 
210
                        if( fgets(buf,256,read_file) == 0 )
 
211
                        {
 
212
                                fprintf( stderr, "Read error!! : %s\n", refname );
 
213
                                exit( -1 );
 
214
                        }
 
215
                        first = 0;
 
216
                }
 
217
                check_comment( buf );
 
218
 
 
219
                if( check_space(buf)!=0 ) {
 
220
                        fgets( buf,256,read_file );
 
221
                        continue;
 
222
                }
 
223
 
 
224
                i = 0, j = 0; 
 
225
                dtp = strtok( buf, " \t\n" );
 
226
                frameNo = atoi( dtp );
 
227
 
 
228
                while( (dtp = strtok(NULL, " \t\n")) != NULL ) {
 
229
                        int_dt = atoi( dtp );
 
230
 
 
231
                        if( int_dt < 0 || int_dt > count + 1 ) {
 
232
                                err_flag = 1;
 
233
                        }
 
234
 
 
235
                        if(int_dt > (int)~(0xffffffff<<m_iNumBitsVopID)) {
 
236
                                err_flag = 1;
 
237
                        }
 
238
 
 
239
                        m_pNewPredControl->ref_tbl[count][i++] = int_dt;
 
240
                        if( i >= MAX_NumGOB )
 
241
                                break;
 
242
                }
 
243
 
 
244
                if( err_flag != 0 || i > (m_iHeight/MB_SIZE) )
 
245
                {
 
246
                        fprintf( stdout,
 
247
                                "Error reference frame table: %s(%d Line)\n", refname, line );
 
248
                        exit( -1 );
 
249
                }
 
250
 
 
251
                count++;
 
252
                if( count >= 0x800 )
 
253
                        break;
 
254
 
 
255
                fgets( buf, 256, read_file );
 
256
        }
 
257
 
 
258
        fclose( read_file );
 
259
        first = 1;
 
260
}
 
261
 
 
262
 
 
263
NEWPREDcnt*  CNewPredEncoder::initNEWPREDcnt( UInt uiVO_id)
 
264
{
 
265
        int                     i, j;
 
266
        NEWPREDcnt*     newpredCnt;
 
267
 
 
268
        newpredCnt = (NEWPREDcnt *) malloc(sizeof (NEWPREDcnt));
 
269
        memset(newpredCnt, 0, sizeof(NEWPREDcnt));
 
270
 
 
271
        m_iNumSlice = 1;
 
272
        for( i = 0; i < NP_MAX_NUMSLICE; i++ ) {
 
273
                if( m_piSlicePoint[i] < 0 )
 
274
                        break;
 
275
                m_iNumSlice++;
 
276
        }
 
277
        --m_iNumSlice;
 
278
 
 
279
        newpredCnt->NPRefBuf = (NEWPRED_buf***)aalloc(m_iNumSlice,m_iNumBuffEnc,sizeof(NEWPRED_buf*));
 
280
        if (newpredCnt->NPRefBuf == NULL) {
 
281
          fprintf(stderr, "initNEWPREDcnt: ERROR Memory allocate error(NEWPRED_buf)\n");
 
282
                return newpredCnt;
 
283
        }
 
284
        newpredCnt->ref = new int[m_iNumSlice];
 
285
        if (newpredCnt->ref == NULL) {
 
286
            fprintf(stderr, "initNEWPREDcnt: ERROR Memory allocate error(ref)\n");
 
287
                return newpredCnt;
 
288
        }
 
289
        memset(newpredCnt->ref, 0, sizeof(int)*m_iNumSlice);
 
290
                
 
291
        Int *iY = new int[m_iNumSlice];
 
292
        m_iHMBNum = new int[m_iNumSlice];
 
293
 
 
294
        for (i= 0; (i < m_iNumSlice); i++) {
 
295
                if (i+1 < m_iNumSlice) {
 
296
                        iY[i] = *(m_piSlicePoint + i+1) - *(m_piSlicePoint + i);
 
297
                } else {
 
298
                        iY[i] = (m_iNPNumMBX*m_iNPNumMBY) - *(m_piSlicePoint + i);
 
299
                }
 
300
                m_iHMBNum[i] = iY[i] / m_iNPNumMBX;
 
301
 
 
302
                if (m_iHMBNum[i] == 0)
 
303
                                m_iHMBNum[i] = 1;
 
304
        }
 
305
        delete []iY;
 
306
 
 
307
        for (i= 0; (i < m_iNumSlice) && (*(m_piSlicePoint + i) >= 0); i++) {
 
308
                Int     iWidthUV  = m_iWidth/(MB_SIZE/BLOCK_SIZE);
 
309
                Int iHeightUV = (MB_SIZE * m_iHMBNum[i])/(MB_SIZE/BLOCK_SIZE);
 
310
                
 
311
                for (j= 0; j<m_iNumBuffEnc; j++) {
 
312
                        newpredCnt->NPRefBuf[i][j] = new NEWPRED_buf;
 
313
                        if (newpredCnt->NPRefBuf[i][j] == NULL) {
 
314
                        fprintf(stderr, "initNEWPREDcnt: ERROR Memory allocate error(NEWPRED_buf)\n");
 
315
                                return newpredCnt;
 
316
                        }
 
317
 
 
318
                newpredCnt->NPRefBuf[i][j]->vop_id = 0;
 
319
 
 
320
                        newpredCnt->NPRefBuf[i][j]->iSizeY = (2*EXPANDY_REF_FRAME+m_iWidth)*(MB_SIZE* m_iHMBNum[i]);
 
321
                        newpredCnt->NPRefBuf[i][j]->iSizeUV = (EXPANDY_REF_FRAME+iWidthUV)* iHeightUV;
 
322
 
 
323
                        newpredCnt->NPRefBuf[i][j]->iSlice = i;
 
324
 
 
325
                        newpredCnt->NPRefBuf[i][j]->pdata.pchY = new PixelC[newpredCnt->NPRefBuf[i][j]->iSizeY];
 
326
                        if (newpredCnt->NPRefBuf[i][j]->pdata.pchY == NULL) {
 
327
                        fprintf(stderr, "initNEWPREDcnt: ERROR Memory allocate error(pchY)\n");
 
328
                                return newpredCnt;
 
329
                        }
 
330
                        newpredCnt->NPRefBuf[i][j]->pdata.pchU = new PixelC[newpredCnt->NPRefBuf[i][j]->iSizeUV];
 
331
                        if (newpredCnt->NPRefBuf[i][j]->pdata.pchU == NULL) {
 
332
                        fprintf(stderr, "initNEWPREDcnt: ERROR Memory allocate error(pchU)\n");
 
333
                                return newpredCnt;
 
334
                        }
 
335
                        newpredCnt->NPRefBuf[i][j]->pdata.pchV = new PixelC[newpredCnt->NPRefBuf[i][j]->iSizeUV];
 
336
                        if (newpredCnt->NPRefBuf[i][j]->pdata.pchV == NULL) {
 
337
                        fprintf(stderr, "initNEWPREDcnt: ERROR Memory allocate error(pchV)\n");
 
338
                                return newpredCnt;
 
339
                        }
 
340
 
 
341
                        memset(newpredCnt->NPRefBuf[i][j]->pdata.pchY, 0, newpredCnt->NPRefBuf[i][j]->iSizeY);
 
342
                        memset(newpredCnt->NPRefBuf[i][j]->pdata.pchU, 0, newpredCnt->NPRefBuf[i][j]->iSizeUV);
 
343
                        memset(newpredCnt->NPRefBuf[i][j]->pdata.pchV, 0, newpredCnt->NPRefBuf[i][j]->iSizeUV);
 
344
 
 
345
                }
 
346
        }
 
347
    return(newpredCnt);
 
348
}
 
349
 
 
350
void  CNewPredEncoder::endNEWPREDcnt( NEWPREDcnt* newpredCnt )
 
351
{
 
352
        {
 
353
                int             i, j;
 
354
 
 
355
                for (i= 0; (i < m_iNumSlice) && (*(m_piSlicePoint + i) >= 0); i++) {
 
356
                
 
357
                        for (j= 0; j<m_iNumBuffEnc; j++) {
 
358
                                delete newpredCnt->NPRefBuf[i][j]->pdata.pchY;
 
359
                                delete newpredCnt->NPRefBuf[i][j]->pdata.pchU;
 
360
                                delete newpredCnt->NPRefBuf[i][j]->pdata.pchV;
 
361
                                delete newpredCnt->NPRefBuf[i][j];
 
362
                        }
 
363
                }
 
364
 
 
365
                delete []newpredCnt->ref;
 
366
 
 
367
                if (newpredCnt->NPRefBuf != NULL) {
 
368
                        afree((int**)(newpredCnt->NPRefBuf));
 
369
                }
 
370
        }
 
371
        delete []m_iHMBNum;
 
372
 
 
373
        free(newpredCnt);
 
374
}
 
375
 
 
376
void  CNewPredEncoder::makeNextRef(
 
377
                                NEWPREDcnt*     newpredCnt,
 
378
                                int                     slice_no)
 
379
{
 
380
        static int count=m_iNumSlice-1;
 
381
        if (m_iVopID == 0 && slice_no == 0)
 
382
                  count = m_iNumSlice-1;
 
383
 
 
384
        count++;
 
385
        newpredCnt->ref[slice_no] = newpredCnt->ref_tbl[((count/m_iNumSlice)+1)-2][slice_no];
 
386
        return;
 
387
}
 
388
 
 
389
void  CNewPredEncoder::makeNextBuf(
 
390
        NEWPREDcnt*     newpredCnt,
 
391
        int                     vop_id,
 
392
        int                     slice_no)
 
393
{
 
394
        m_pShiftBufTmp = newpredCnt->NPRefBuf[slice_no];
 
395
        shiftBuffer(vop_id, m_iNumBuffEnc);
 
396
}
 
397