1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18#include "mp4def.h"
19#include "mp4lib_int.h"
20#include "rate_control.h"
21#include "mp4enc_lib.h"
22#include "bitstream_io.h"
23#include "m4venc_oscl.h"
24
25void targetBitCalculation(void *input);
26void calculateQuantizer_Multipass(void *video);
27void updateRateControl(rateControl *rc, VideoEncData *video);
28void updateRC_PostProc(rateControl *rc, VideoEncData *video);
29
30/***************************************************************************
31**************  RC APIs to core encoding modules  *******************
32
33PV_STATUS RC_Initialize(void *video);
34PV_STATUS RC_Cleanup(rateControl *rc[],Int numLayers);
35PV_STATUS RC_VopQPSetting(VideoEncData *video,rateControl *rc[]);
36PV_STATUS RC_VopUpdateStat(VideoEncData *video,rateControl *rc[]);
37PV_STATUS RC_UpdateBuffer(VideoEncData *video, Int currLayer, Int num_skip);
38Int       RC_GetSkipNextFrame(VideoEncData *video,Int currLayer);
39void      RC_ResetSkipNextFrame(void *video,Int currLayer);
40
41PV_STATUS RC_UpdateBXRCParams(void *input);  Parameters update for target bitrate or framerate change
42
43****************************************************************************/
44
45
46/************************************************************************/
47/************ API part **************************************************/
48/* must be called before each sequence*/
49
50PV_STATUS RC_Initialize(void *input)
51{
52    VideoEncData *video = (VideoEncData *) input;
53    VideoEncParams *encParams = video->encParams;
54    rateControl **rc = video->rc;
55    Int numLayers = encParams->nLayers;
56    Int *LayerBitRate = encParams->LayerBitRate;
57    float *LayerFrameRate = encParams->LayerFrameRate;
58    MultiPass **pMP = video->pMP;
59
60    Int n;
61
62    for (n = 0; n < numLayers; n++)
63    {
64        /* rate control */
65        rc[n]->fine_frame_skip = encParams->FineFrameSkip_Enabled;
66        rc[n]->no_frame_skip = encParams->NoFrameSkip_Enabled;
67        rc[n]->no_pre_skip = encParams->NoPreSkip_Enabled;
68        rc[n]->skip_next_frame = 0; /* must be initialized */
69
70        //rc[n]->TMN_TH = (Int)((float)LayerBitRate[n]/LayerFrameRate[n]);
71        rc[n]->Bs = video->encParams->BufferSize[n];
72        rc[n]->TMN_W = 0;
73        rc[n]->VBV_fullness = (Int)(rc[n]->Bs * 0.5); /* rc[n]->Bs */
74        rc[n]->encoded_frames = 0;
75        rc[n]->framerate = LayerFrameRate[n];
76        if (n == 0)
77        {
78            rc[n]->TMN_TH = (Int)((float)LayerBitRate[n] / LayerFrameRate[n]);
79            rc[n]->bitrate = LayerBitRate[n];
80            rc[n]->framerate = LayerFrameRate[n];
81
82            // For h263 or short header mode, the bit variation is within (-2*Rmax*1001/3000, 2*Rmax*1001/3000)
83            if (video->encParams->H263_Enabled)
84            {
85                rc[n]->max_BitVariance_num = (Int)((rc[n]->Bs - video->encParams->maxFrameSize) / 2 / (rc[n]->bitrate / rc[n]->framerate / 10.0)) - 5;
86                if (rc[n]->max_BitVariance_num < 0) rc[n]->max_BitVariance_num += 5;
87            }
88            else   // MPEG-4 normal modes
89            {
90                rc[n]->max_BitVariance_num = (Int)((float)(rc[n]->Bs - rc[n]->VBV_fullness) / ((float)LayerBitRate[n] / LayerFrameRate[n] / 10.0)) - 5;
91                if (rc[n]->max_BitVariance_num < 0) rc[n]->max_BitVariance_num += 5;
92            }
93        }
94        else
95        {
96            if (LayerFrameRate[n] - LayerFrameRate[n-1] > 0) /*  7/31/03 */
97            {
98                rc[n]->TMN_TH = (Int)((float)(LayerBitRate[n] - LayerBitRate[n-1]) / (LayerFrameRate[n] - LayerFrameRate[n-1]));
99                rc[n]->max_BitVariance_num = (Int)((float)(rc[n]->Bs - rc[n]->VBV_fullness) * 10 / ((float)rc[n]->TMN_TH)) - 5;
100                if (rc[n]->max_BitVariance_num < 0) rc[n]->max_BitVariance_num += 5;
101            }
102            else   /*  7/31/03 */
103            {
104                rc[n]->TMN_TH = 1 << 30;
105                rc[n]->max_BitVariance_num = 0;
106            }
107            rc[n]->bitrate = LayerBitRate[n] - LayerBitRate[n-1];
108            rc[n]->framerate = LayerFrameRate[n] - LayerFrameRate[n-1];
109        }
110
111        // Set the initial buffer fullness
112        if (1) //!video->encParams->H263_Enabled)  { // MPEG-4
113        {
114            /* According to the spec, the initial buffer fullness needs to be set to 1/3 */
115            rc[n]->VBV_fullness = (Int)(rc[n]->Bs / 3.0 - rc[n]->Bs / 2.0); /* the buffer range is [-Bs/2, Bs/2] */
116            pMP[n]->counter_BTsrc = (Int)((rc[n]->Bs / 2.0 - rc[n]->Bs / 3.0) / (rc[n]->bitrate / rc[n]->framerate / 10.0));
117            rc[n]->TMN_W = (Int)(rc[n]->VBV_fullness + pMP[n]->counter_BTsrc * (rc[n]->bitrate / rc[n]->framerate / 10.0));
118
119            rc[n]->low_bound = -rc[n]->Bs / 2;
120            rc[n]-> VBV_fullness_offset = 0;
121        }
122        else   /* this part doesn't work in some cases, the low_bound is too high, Jan 4,2006 */
123        {
124            rc[n]->VBV_fullness =  rc[n]->Bs - (Int)(video->encParams->VBV_delay * rc[n]->bitrate);
125            if (rc[n]->VBV_fullness < 0) rc[n]->VBV_fullness = 0;
126            //rc[n]->VBV_fullness = (rc[n]->Bs-video->encParams->maxFrameSize)/2 + video->encParams->maxFrameSize;
127
128            rc[n]->VBV_fullness -= rc[n]->Bs / 2; /* the buffer range is [-Bs/2, Bs/2] */
129            rc[n]->low_bound = -rc[n]->Bs / 2 + video->encParams->maxFrameSize;  /*  too high */
130            rc[n]->VBV_fullness_offset = video->encParams->maxFrameSize / 2; /*  don't understand the meaning of this */
131            pMP[n]->counter_BTdst = pMP[n]->counter_BTsrc = 0;
132
133        }
134
135        /* Setting the bitrate and framerate */
136        pMP[n]->bitrate = rc[n]->bitrate;
137        pMP[n]->framerate = rc[n]->framerate;
138        pMP[n]->target_bits_per_frame = pMP[n]->bitrate / pMP[n]->framerate;
139
140    }
141
142    return PV_SUCCESS;
143}
144
145
146/* ======================================================================== */
147/*  Function : RC_Cleanup                                                   */
148/*  Date     : 12/20/2000                                                   */
149/*  Purpose  : free Rate Control memory                                     */
150/*  In/out   :                                                              */
151/*  Return   :                                                              */
152/*  Modified :                                                              */
153/* ======================================================================== */
154
155
156PV_STATUS RC_Cleanup(rateControl *rc[], Int numLayers)
157{
158    OSCL_UNUSED_ARG(rc);
159    OSCL_UNUSED_ARG(numLayers);
160
161    return PV_SUCCESS;
162}
163
164
165
166/* ======================================================================== */
167/*  Function : RC_VopQPSetting                                              */
168/*  Date     : 4/11/2001                                                    */
169/*  Purpose  : Reset rate control before coding VOP, moved from vop.c       */
170/*              Compute QP for the whole VOP and initialize MB-based RC
171                reset QPMB[], currVop->quantizer, rc->Ec, video->header_bits */
172/* to          In order to  work RC_VopQPSetting has to do the followings
173                1. Set video->QPMB of all macroblocks.
174                2. Set currVop->quantizer
175                3. Reset video->header_bits to zero.
176                4. Initialize internal RC parameters for Vop cooding        */
177/*  In/out   :                                                              */
178/*  Return   : PV_STATUS                                                    */
179/*  Modified :                                                              */
180/* ======================================================================== */
181/* To be moved to rate_control.c and separate between BX_RC and ANNEX_L     */
182
183PV_STATUS RC_VopQPSetting(VideoEncData *video, rateControl *prc[])
184{
185    Int currLayer = video->currLayer;
186    Vol *currVol = video->vol[currLayer];
187    Vop *currVop = video->currVop;
188#ifdef TEST_MBBASED_QP
189    int i;
190#endif
191
192    rateControl *rc = video->rc[currLayer];
193    MultiPass *pMP = video->pMP[currLayer];
194
195    OSCL_UNUSED_ARG(prc);
196
197    if (video->encParams->RC_Type == CONSTANT_Q)
198    {
199        M4VENC_MEMSET(video->QPMB, currVop->quantizer, sizeof(UChar)*currVol->nTotalMB);
200        return PV_SUCCESS;
201    }
202    else
203    {
204
205        if (video->rc[currLayer]->encoded_frames == 0) /* rc[currLayer]->totalFrameNumber*/
206        {
207            M4VENC_MEMSET(video->QPMB, currVop->quantizer, sizeof(UChar)*currVol->nTotalMB);
208            video->rc[currLayer]->Qc = video->encParams->InitQuantIvop[currLayer];
209        }
210        else
211        {
212            calculateQuantizer_Multipass((void*) video);
213            currVop->quantizer = video->rc[currLayer]->Qc;
214#ifdef TEST_MBBASED_QP
215            i = currVol->nTotalMB;  /* testing changing QP at MB level */
216            while (i)
217            {
218                i--;
219                video->QPMB[i] = (i & 1) ? currVop->quantizer - 1 : currVop->quantizer + 1;
220            }
221#else
222            M4VENC_MEMSET(video->QPMB, currVop->quantizer, sizeof(UChar)*currVol->nTotalMB);
223#endif
224        }
225
226        video->header_bits = 0;
227    }
228
229    /* update pMP->framePos */
230    if (++pMP->framePos == pMP->frameRange) pMP->framePos = 0;
231
232    if (rc->T == 0)
233    {
234        pMP->counter_BTdst = (Int)(video->encParams->LayerFrameRate[video->currLayer] * 7.5 + 0.5); /* 0.75s time frame */
235        pMP->counter_BTdst = PV_MIN(pMP->counter_BTdst, (Int)(rc->max_BitVariance_num / 2 * 0.40)); /* 0.75s time frame may go beyond VBV buffer if we set the buffer size smaller than 0.75s */
236        pMP->counter_BTdst = PV_MAX(pMP->counter_BTdst, (Int)((rc->Bs / 2 - rc->VBV_fullness) * 0.30 / (rc->TMN_TH / 10.0) + 0.5)); /* At least 30% of VBV buffer size/2 */
237        pMP->counter_BTdst = PV_MIN(pMP->counter_BTdst, 20); /* Limit the target to be smaller than 3C */
238
239        pMP->target_bits = rc->T = rc->TMN_TH = (Int)(rc->TMN_TH * (1.0 + pMP->counter_BTdst * 0.1));
240        pMP->diff_counter = pMP->counter_BTdst;
241    }
242
243    /* collect the necessary data: target bits, actual bits, mad and QP */
244    pMP->target_bits = rc->T;
245    pMP->QP  = currVop->quantizer;
246
247    pMP->mad = video->sumMAD / (float)currVol->nTotalMB;
248    if (pMP->mad < MAD_MIN) pMP->mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
249
250    pMP->bitrate = rc->bitrate; /* calculated in RCVopQPSetting */
251    pMP->framerate = rc->framerate;
252
253    /* first pass encoding */
254    pMP->nRe_Quantized = 0;
255
256    return  PV_SUCCESS;
257}
258
259
260/* ======================================================================== */
261/*  Function : SaveRDSamples()                                              */
262/*  Date     : 08/29/2001                                                   */
263/*  History  :                                                              */
264/*  Purpose  : Save QP, actual_bits, mad and R_D of the current iteration   */
265/*  In/out   :                                                              */
266/*  Return   :                                                              */
267/*  Modified :                                                              */
268/*                                                                          */
269/* ======================================================================== */
270
271Void SaveRDSamples(MultiPass *pMP, Int counter_samples)
272{
273    /* for pMP->pRDSamples */
274    pMP->pRDSamples[pMP->framePos][counter_samples].QP    = pMP->QP;
275    pMP->pRDSamples[pMP->framePos][counter_samples].actual_bits = pMP->actual_bits;
276    pMP->pRDSamples[pMP->framePos][counter_samples].mad   = pMP->mad;
277    pMP->pRDSamples[pMP->framePos][counter_samples].R_D = (float)(pMP->actual_bits / (pMP->mad + 0.0001));
278
279    return ;
280}
281/* ======================================================================== */
282/*  Function : RC_VopUpdateStat                                             */
283/*  Date     : 12/20/2000                                                   */
284/*  Purpose  : Update statistics for rate control after encoding each VOP.  */
285/*             No need to change anything in VideoEncData structure.        */
286/*  In/out   :                                                              */
287/*  Return   :                                                              */
288/*  Modified :                                                              */
289/* ======================================================================== */
290
291PV_STATUS RC_VopUpdateStat(VideoEncData *video, rateControl *rc)
292{
293    Int currLayer = video->currLayer;
294    Vol *currVol = video->vol[currLayer];
295    MultiPass *pMP = video->pMP[currLayer];
296    Int diff_BTCounter;
297
298    switch (video->encParams->RC_Type)
299    {
300        case CONSTANT_Q:
301            break;
302
303        case CBR_1:
304        case CBR_2:
305        case VBR_1:
306        case VBR_2:
307        case CBR_LOWDELAY:
308
309            pMP->actual_bits = currVol->stream->byteCount << 3;
310
311            SaveRDSamples(pMP, 0);
312
313            pMP->encoded_frames++;
314
315            /* for pMP->samplesPerFrame */
316            pMP->samplesPerFrame[pMP->framePos] = 0;
317
318            pMP->sum_QP += pMP->QP;
319
320
321            /* update pMP->counter_BTsrc, pMP->counter_BTdst */
322            /* re-allocate the target bit again and then stop encoding */
323            diff_BTCounter = (Int)((float)(rc->TMN_TH - rc->TMN_W - pMP->actual_bits) /
324                                   (pMP->bitrate / (pMP->framerate + 0.0001) + 0.0001) / 0.1);
325            if (diff_BTCounter >= 0)
326                pMP->counter_BTsrc += diff_BTCounter; /* pMP->actual_bits is smaller */
327            else
328                pMP->counter_BTdst -= diff_BTCounter; /* pMP->actual_bits is bigger */
329
330            rc->TMN_TH -= (Int)((float)pMP->bitrate / (pMP->framerate + 0.0001) * (diff_BTCounter * 0.1));
331            rc->T = pMP->target_bits = rc->TMN_TH - rc->TMN_W;
332            pMP->diff_counter -= diff_BTCounter;
333
334            rc->Rc = currVol->stream->byteCount << 3;   /* Total Bits for current frame */
335            rc->Hc = video->header_bits;    /* Total Bits in Header and Motion Vector */
336
337            /* BX_RC */
338            updateRateControl(rc, video);
339
340            break;
341
342        default: /* for case CBR_1/2, VBR_1/2 */
343
344            return PV_FAIL;
345    }
346
347
348    return PV_SUCCESS;
349}
350
351/* ======================================================================== */
352/*  Function : RC_GetSkipNextFrame, RC_GetRemainingVops                     */
353/*  Date     : 2/20/2001                                                    */
354/*  Purpose  : To access RC parameters from other parts of the code.        */
355/*  In/out   :                                                              */
356/*  Return   :                                                              */
357/*  Modified :                                                              */
358/* ======================================================================== */
359
360Int RC_GetSkipNextFrame(VideoEncData *video, Int currLayer)
361{
362    return video->rc[currLayer]->skip_next_frame;
363}
364
365void RC_ResetSkipNextFrame(VideoEncData *video, Int currLayer)
366{
367
368    video->rc[currLayer]->skip_next_frame = 0;
369    return ;
370}
371
372/* ======================================================================== */
373/*  Function : RC_UpdateBuffer                                      */
374/*  Date     : 2/20/2001                                                    */
375/*  Purpose  : Update RC in case of there are frames skipped (camera freeze)*/
376/*              from the application level in addition to what RC requested */
377/*  In/out   : Nr, B, Rr                                                    */
378/*  Return   : Void                                                         */
379/*  Modified :                                                              */
380/* ======================================================================== */
381
382
383PV_STATUS RC_UpdateBuffer(VideoEncData *video, Int currLayer, Int num_skip)
384{
385    rateControl *rc  = video->rc[currLayer];
386    MultiPass   *pMP = video->pMP[currLayer];
387
388    if (video == NULL || rc == NULL || pMP == NULL)
389        return PV_FAIL;
390
391    rc->VBV_fullness   -= (Int)(rc->bitrate / rc->framerate * num_skip); //rc[currLayer]->Rp;
392    pMP->counter_BTsrc += 10 * num_skip;
393
394    /* Check buffer underflow */
395    if (rc->VBV_fullness < rc->low_bound)
396    {
397        rc->VBV_fullness = rc->low_bound; // -rc->Bs/2;
398        rc->TMN_W = rc->VBV_fullness - rc->low_bound;
399        pMP->counter_BTsrc = pMP->counter_BTdst + (Int)((float)(rc->Bs / 2 - rc->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
400    }
401
402    return PV_SUCCESS;
403}
404
405
406/* ======================================================================== */
407/*  Function : RC_UpdateBXRCParams                                          */
408/*  Date     : 4/08/2002                                                    */
409/*  Purpose  : Update RC parameters specifically for target bitrate or      */
410/*             framerate update during an encoding session                  */
411/*  In/out   :                                                              */
412/*  Return   : PV_TRUE if successed, PV_FALSE if failed.                    */
413/*  Modified :                                                              */
414/* ======================================================================== */
415
416PV_STATUS RC_UpdateBXRCParams(void *input)
417{
418    VideoEncData *video = (VideoEncData *) input;
419    VideoEncParams *encParams = video->encParams;
420    rateControl **rc = video->rc;
421    Int numLayers = encParams->nLayers;
422    Int *LayerBitRate = encParams->LayerBitRate;
423    float *LayerFrameRate = encParams->LayerFrameRate;
424    MultiPass **pMP = video->pMP;
425
426    Int n, VBV_fullness;
427    Int diff_counter;
428
429    extern Bool SetProfile_BufferSize(VideoEncData *video, float delay, Int bInitialized);
430
431
432    /* Reset video buffer size due to target bitrate change */
433    SetProfile_BufferSize(video, video->encParams->VBV_delay, 0); /* output: video->encParams->BufferSize[] */
434
435    for (n = 0; n < numLayers; n++)
436    {
437        /* Remaining stuff about frame dropping and underflow check in update RC */
438        updateRC_PostProc(rc[n], video);
439        rc[n]->skip_next_frame = 0; /* must be initialized */
440
441        /* New changes: bitrate and framerate, Bs, max_BitVariance_num, TMN_TH(optional), encoded_frames(optional) */
442        rc[n]->Bs = video->encParams->BufferSize[n];
443        VBV_fullness = (Int)(rc[n]->Bs * 0.5);
444
445        if (n == 0)
446        {
447            rc[n]->TMN_TH = (Int)((float)LayerBitRate[n] / LayerFrameRate[n]);
448            rc[n]->bitrate   = pMP[n]->bitrate   = LayerBitRate[n];
449            rc[n]->framerate = pMP[n]->framerate = LayerFrameRate[n];
450
451            // For h263 or short header mode, the bit variation is within (-2*Rmax*1001/3000, 2*Rmax*1001/3000)
452            if (video->encParams->H263_Enabled)
453            {
454                rc[n]->max_BitVariance_num = (Int)((rc[n]->Bs - video->encParams->maxFrameSize) / 2 / (rc[n]->bitrate / rc[n]->framerate / 10.0)) - 5;
455                //rc[n]->max_BitVariance_num = (Int)((float)(rc[n]->Bs - rc[n]->VBV_fullness)/((float)LayerBitRate[n]/LayerFrameRate[n]/10.0))-5;
456            }
457            else   // MPEG-4 normal modes
458            {
459                rc[n]->max_BitVariance_num = (Int)((float)(rc[n]->Bs - VBV_fullness) * 10 / ((float)LayerBitRate[n] / LayerFrameRate[n])) - 5;
460            }
461        }
462        else
463        {
464            if (LayerFrameRate[n] - LayerFrameRate[n-1] > 0) /*  7/31/03 */
465            {
466                rc[n]->TMN_TH = (Int)((float)(LayerBitRate[n] - LayerBitRate[n-1]) / (LayerFrameRate[n] - LayerFrameRate[n-1]));
467                rc[n]->max_BitVariance_num = (Int)((float)(rc[n]->Bs - VBV_fullness) * 10 / ((float)rc[n]->TMN_TH)) - 5;
468                if (rc[n]->max_BitVariance_num < 0) rc[n]->max_BitVariance_num += 5;
469            }
470            else   /*  7/31/03 */
471            {
472                rc[n]->TMN_TH = 1 << 30;
473                rc[n]->max_BitVariance_num = 0;
474            }
475            rc[n]->bitrate   = pMP[n]->bitrate   = LayerBitRate[n] - LayerBitRate[n-1];
476            rc[n]->framerate = pMP[n]->framerate = LayerFrameRate[n] - LayerFrameRate[n-1];
477        }
478
479        pMP[n]->target_bits_per_frame_prev = pMP[n]->target_bits_per_frame;
480        pMP[n]->target_bits_per_frame = pMP[n]->bitrate / (float)(pMP[n]->framerate + 0.0001);  /*  7/31/03 */
481
482        /* rc[n]->VBV_fullness and rc[n]->TMN_W should be kept same */
483        /* update pMP[n]->counter_BTdst and pMP[n]->counter_BTsrc   */
484        diff_counter = (Int)((float)(rc[n]->VBV_fullness - rc[n]->TMN_W) /
485                             (pMP[n]->target_bits_per_frame / 10 + 0.0001)); /*  7/31/03 */
486
487        pMP[n]->counter_BTdst = pMP[n]->counter_BTsrc = 0;
488        if (diff_counter > 0)
489            pMP[n]->counter_BTdst = diff_counter;
490
491        else if (diff_counter < 0)
492            pMP[n]->counter_BTsrc = -diff_counter;
493
494        rc[n]->TMN_W = (Int)(rc[n]->VBV_fullness -      /* re-calculate rc[n]->TMN_W in order for higher accuracy */
495                             (pMP[n]->target_bits_per_frame / 10) * (pMP[n]->counter_BTdst - pMP[n]->counter_BTsrc));
496
497        /* Keep the current average mad */
498        if (pMP[n]->aver_mad != 0)
499        {
500            pMP[n]->aver_mad_prev = pMP[n]->aver_mad;
501            pMP[n]->encoded_frames_prev = pMP[n]->encoded_frames;
502        }
503
504        pMP[n]->aver_mad = 0;
505        pMP[n]->overlapped_win_size = 4;
506
507        /* Misc */
508        pMP[n]->sum_mad = pMP[n]->sum_QP = 0;
509        //pMP[n]->encoded_frames_prev = pMP[n]->encoded_frames;
510        pMP[n]->encoded_frames = pMP[n]->re_encoded_frames = pMP[n]->re_encoded_times = 0;
511
512    } /* end of: for(n=0; n<numLayers; n++) */
513
514    return PV_SUCCESS;
515
516}
517
518
519/* ================================================================================ */
520/*  Function : targetBitCalculation                                                 */
521/*  Date     : 10/01/2001                                                           */
522/*  Purpose  : quadratic bit allocation model: T(n) = C*sqrt(mad(n)/aver_mad(n-1))  */
523/*                                                                                  */
524/*  In/out   : rc->T                                                                */
525/*  Return   : Void                                                                 */
526/*  Modified :                                                                      */
527/* ================================================================================ */
528
529void targetBitCalculation(void *input)
530{
531    VideoEncData *video = (VideoEncData *) input;
532    MultiPass *pMP = video->pMP[video->currLayer];
533    Vol *currVol = video->vol[video->currLayer];
534    rateControl *rc = video->rc[video->currLayer];
535
536    float curr_mad;//, average_mad;
537    Int diff_counter_BTsrc, diff_counter_BTdst, prev_counter_diff, curr_counter_diff, bound;
538    /* BT = Bit Transfer, for pMP->counter_BTsrc, pMP->counter_BTdst */
539
540    if (video == NULL || currVol == NULL || pMP == NULL || rc == NULL)
541        return;
542
543    /* some stuff about frame dropping remained here to be done because pMP cannot be inserted into updateRateControl()*/
544    updateRC_PostProc(rc, video);
545
546    /* update pMP->counter_BTsrc and pMP->counter_BTdst to avoid interger overflow */
547    if (pMP->counter_BTsrc > 1000 && pMP->counter_BTdst > 1000)
548    {
549        pMP->counter_BTsrc -= 1000;
550        pMP->counter_BTdst -= 1000;
551    }
552
553    /* ---------------------------------------------------------------------------------------------------*/
554    /* target calculation */
555    curr_mad = video->sumMAD / (float)currVol->nTotalMB;
556    if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
557    diff_counter_BTsrc = diff_counter_BTdst = 0;
558    pMP->diff_counter = 0;
559
560
561    /*1.calculate average mad */
562    pMP->sum_mad += curr_mad;
563    //average_mad = (pMP->encoded_frames < 1 ? curr_mad : pMP->sum_mad/(float)(pMP->encoded_frames+1)); /* this function is called from the scond encoded frame*/
564    //pMP->aver_mad = average_mad;
565    if (pMP->encoded_frames >= 0) /* pMP->encoded_frames is set to -1 initially, so forget about the very first I frame */
566        pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames + curr_mad) / (pMP->encoded_frames + 1);
567
568    if (pMP->overlapped_win_size > 0 && pMP->encoded_frames_prev >= 0)  /*  7/31/03 */
569        pMP->aver_mad_prev = (pMP->aver_mad_prev * pMP->encoded_frames_prev + curr_mad) / (pMP->encoded_frames_prev + 1);
570
571    /*2.average_mad, mad ==> diff_counter_BTsrc, diff_counter_BTdst */
572    if (pMP->overlapped_win_size == 0)
573    {
574        /* original verison */
575        if (curr_mad > pMP->aver_mad*1.1)
576        {
577            if (curr_mad / (pMP->aver_mad + 0.0001) > 2)
578                diff_counter_BTdst = (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.4) - 10;
579            //diff_counter_BTdst = (Int)((sqrt(curr_mad/pMP->aver_mad)*2+curr_mad/pMP->aver_mad)/(3*0.1) + 0.4) - 10;
580            else
581                diff_counter_BTdst = (Int)(curr_mad / (pMP->aver_mad + 0.0001) * 10 + 0.4) - 10;
582        }
583        else /* curr_mad <= average_mad*1.1 */
584            //diff_counter_BTsrc = 10 - (Int)((sqrt(curr_mad/pMP->aver_mad) + pow(curr_mad/pMP->aver_mad, 1.0/3.0))/(2.0*0.1) + 0.4);
585            diff_counter_BTsrc = 10 - (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.5);
586        //diff_counter_BTsrc = 10 - (Int)(curr_mad/pMP->aver_mad/0.1 + 0.5)
587
588        /* actively fill in the possible gap */
589        if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 &&
590                curr_mad <= pMP->aver_mad*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
591            diff_counter_BTsrc = 1;
592
593    }
594    else if (pMP->overlapped_win_size > 0)
595    {
596        /* transition time: use previous average mad "pMP->aver_mad_prev" instead of the current average mad "pMP->aver_mad" */
597        if (curr_mad > pMP->aver_mad_prev*1.1)
598        {
599            if (curr_mad / pMP->aver_mad_prev > 2)
600                diff_counter_BTdst = (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.4) - 10;
601            //diff_counter_BTdst = (Int)((M4VENC_SQRT(curr_mad/pMP->aver_mad_prev)*2+curr_mad/pMP->aver_mad_prev)/(3*0.1) + 0.4) - 10;
602            else
603                diff_counter_BTdst = (Int)(curr_mad / (pMP->aver_mad_prev + 0.0001) * 10 + 0.4) - 10;
604        }
605        else /* curr_mad <= average_mad*1.1 */
606            //diff_counter_BTsrc = 10 - (Int)((sqrt(curr_mad/pMP->aver_mad_prev) + pow(curr_mad/pMP->aver_mad_prev, 1.0/3.0))/(2.0*0.1) + 0.4);
607            diff_counter_BTsrc = 10 - (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.5);
608        //diff_counter_BTsrc = 10 - (Int)(curr_mad/pMP->aver_mad_prev/0.1 + 0.5)
609
610        /* actively fill in the possible gap */
611        if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 &&
612                curr_mad <= pMP->aver_mad_prev*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
613            diff_counter_BTsrc = 1;
614
615        if (--pMP->overlapped_win_size <= 0)    pMP->overlapped_win_size = 0;
616    }
617
618
619    /* if difference is too much, do clipping */
620    /* First, set the upper bound for current bit allocation variance: 80% of available buffer */
621    bound = (Int)((rc->Bs / 2 - rc->VBV_fullness) * 0.6 / (pMP->target_bits_per_frame / 10)); /* rc->Bs */
622    diff_counter_BTsrc =  PV_MIN(diff_counter_BTsrc, bound);
623    diff_counter_BTdst =  PV_MIN(diff_counter_BTdst, bound);
624
625    /* Second, set another upper bound for current bit allocation: 4-5*bitrate/framerate */
626    bound = 50;
627//  if(video->encParams->RC_Type == CBR_LOWDELAY)
628//  not necessary       bound = 10;     /*  1/17/02 -- For Low delay */
629
630    diff_counter_BTsrc =  PV_MIN(diff_counter_BTsrc, bound);
631    diff_counter_BTdst =  PV_MIN(diff_counter_BTdst, bound);
632
633
634    /* Third, check the buffer */
635    prev_counter_diff = pMP->counter_BTdst - pMP->counter_BTsrc;
636    curr_counter_diff = prev_counter_diff + (diff_counter_BTdst - diff_counter_BTsrc);
637
638    if (PV_ABS(prev_counter_diff) >= rc->max_BitVariance_num || PV_ABS(curr_counter_diff) >= rc->max_BitVariance_num) // PV_ABS(curr_counter_diff) >= PV_ABS(prev_counter_diff) )
639    {   //diff_counter_BTsrc = diff_counter_BTdst = 0;
640
641        if (curr_counter_diff > rc->max_BitVariance_num && diff_counter_BTdst)
642        {
643            diff_counter_BTdst = (rc->max_BitVariance_num - prev_counter_diff) + diff_counter_BTsrc;
644            if (diff_counter_BTdst < 0) diff_counter_BTdst = 0;
645        }
646
647        else if (curr_counter_diff < -rc->max_BitVariance_num && diff_counter_BTsrc)
648        {
649            diff_counter_BTsrc = diff_counter_BTdst - (-rc->max_BitVariance_num - prev_counter_diff);
650            if (diff_counter_BTsrc < 0) diff_counter_BTsrc = 0;
651        }
652    }
653
654
655    /*3.diff_counter_BTsrc, diff_counter_BTdst ==> TMN_TH */
656    //rc->TMN_TH = (Int)((float)pMP->bitrate/pMP->framerate);
657    rc->TMN_TH = (Int)(pMP->target_bits_per_frame);
658    pMP->diff_counter = 0;
659
660    if (diff_counter_BTsrc)
661    {
662        rc->TMN_TH -= (Int)(pMP->target_bits_per_frame * diff_counter_BTsrc * 0.1);
663        pMP->diff_counter = -diff_counter_BTsrc;
664    }
665    else if (diff_counter_BTdst)
666    {
667        rc->TMN_TH += (Int)(pMP->target_bits_per_frame * diff_counter_BTdst * 0.1);
668        pMP->diff_counter = diff_counter_BTdst;
669    }
670
671
672    /*4.update pMP->counter_BTsrc, pMP->counter_BTdst */
673    pMP->counter_BTsrc += diff_counter_BTsrc;
674    pMP->counter_BTdst += diff_counter_BTdst;
675
676
677    /*5.target bit calculation */
678    rc->T = rc->TMN_TH - rc->TMN_W;
679    //rc->T = rc->TMN_TH - (Int)((float)rc->TMN_W/rc->frameRate);
680
681    if (video->encParams->H263_Enabled && rc->T > video->encParams->maxFrameSize)
682    {
683        rc->T = video->encParams->maxFrameSize;  //  added this 11/07/05
684    }
685
686}
687
688/* ================================================================================ */
689/*  Function : calculateQuantizer_Multipass                                         */
690/*  Date     : 10/01/2001                                                           */
691/*  Purpose  : variable rate bit allocation + new QP determination scheme           */
692/*                                                                                  */
693/*  In/out   : rc->T and rc->Qc                                                     */
694/*  Return   : Void                                                                 */
695/*  Modified :                                                                      */
696/* ================================================================================ */
697
698/* Mad based variable bit allocation + QP calculation with a new quadratic method */
699void calculateQuantizer_Multipass(void *input)
700{
701    VideoEncData *video = (VideoEncData *) input;
702    MultiPass *pMP = video->pMP[video->currLayer];
703    Vol *currVol = video->vol[video->currLayer];
704    rateControl *rc = video->rc[video->currLayer];
705
706    Int prev_QP, prev_actual_bits, curr_target, i, j;
707
708    float curr_mad, prev_mad, curr_RD, prev_RD, average_mad, aver_QP;
709
710
711    if (video == NULL || currVol == NULL || pMP == NULL || rc == NULL)
712        return;
713
714    /* Mad based variable bit allocation */
715    targetBitCalculation((void*) video);
716
717    if (rc->T <= 0 || video->sumMAD == 0)
718    {
719        if (rc->T < 0)  rc->Qc = 31;
720        return;
721    }
722
723    /* ---------------------------------------------------------------------------------------------------*/
724    /* current frame QP estimation */
725    curr_target = rc->T;
726    curr_mad = video->sumMAD / (float)currVol->nTotalMB;
727    if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
728    curr_RD  = (float)curr_target / curr_mad;
729
730    /* Another version of search the optimal point */
731    prev_actual_bits = pMP->pRDSamples[0][0].actual_bits;
732    prev_mad = pMP->pRDSamples[0][0].mad;
733
734    for (i = 0, j = 0; i < pMP->frameRange; i++)
735    {
736        if (pMP->pRDSamples[i][0].mad != 0 && prev_mad != 0 &&
737                PV_ABS(prev_mad - curr_mad) > PV_ABS(pMP->pRDSamples[i][0].mad - curr_mad))
738        {
739            prev_mad = pMP->pRDSamples[i][0].mad;
740            prev_actual_bits = pMP->pRDSamples[i][0].actual_bits;
741            j = i;
742        }
743    }
744    prev_QP = pMP->pRDSamples[j][0].QP;
745    for (i = 1; i < pMP->samplesPerFrame[j]; i++)
746    {
747        if (PV_ABS(prev_actual_bits - curr_target) > PV_ABS(pMP->pRDSamples[j][i].actual_bits - curr_target))
748        {
749            prev_actual_bits = pMP->pRDSamples[j][i].actual_bits;
750            prev_QP = pMP->pRDSamples[j][i].QP;
751        }
752    }
753
754    // quadratic approximation
755    prev_RD = (float)prev_actual_bits / prev_mad;
756    //rc->Qc = (Int)(prev_QP * sqrt(prev_actual_bits/curr_target) + 0.4);
757    if (prev_QP == 1) // 11/14/05, added this to allow getting out of QP = 1 easily
758    {
759        rc->Qc = (Int)(prev_RD / curr_RD + 0.5);
760    }
761    else
762    {
763        rc->Qc = (Int)(prev_QP * M4VENC_SQRT(prev_RD / curr_RD) + 0.9);
764
765        if (prev_RD / curr_RD > 0.5 && prev_RD / curr_RD < 2.0)
766            rc->Qc = (Int)(prev_QP * (M4VENC_SQRT(prev_RD / curr_RD) + prev_RD / curr_RD) / 2.0 + 0.9); /* Quadratic and linear approximation */
767        else
768            rc->Qc = (Int)(prev_QP * (M4VENC_SQRT(prev_RD / curr_RD) + M4VENC_POW(prev_RD / curr_RD, 1.0 / 3.0)) / 2.0 + 0.9);
769    }
770    //rc->Qc =(Int)(prev_QP * sqrt(prev_RD/curr_RD) + 0.4);
771    // 11/08/05
772    // lower bound on Qc should be a function of curr_mad
773    // When mad is already low, lower bound on Qc doesn't have to be small.
774    // Note, this doesn't work well for low complexity clip encoded at high bit rate
775    // it doesn't hit the target bit rate due to this QP lower bound.
776/// if((curr_mad < 8) && (rc->Qc < 12)) rc->Qc = 12;
777//  else    if((curr_mad < 128) && (rc->Qc < 3)) rc->Qc = 3;
778
779    if (rc->Qc < 1) rc->Qc = 1;
780    if (rc->Qc > 31)    rc->Qc = 31;
781
782
783    /* active bit resource protection */
784    aver_QP = (pMP->encoded_frames == 0 ? 0 : pMP->sum_QP / (float)pMP->encoded_frames);
785    average_mad = (pMP->encoded_frames == 0 ? 0 : pMP->sum_mad / (float)pMP->encoded_frames); /* this function is called from the scond encoded frame*/
786    if (pMP->diff_counter == 0 &&
787            ((float)rc->Qc <= aver_QP*1.1 || curr_mad <= average_mad*1.1) &&
788            pMP->counter_BTsrc <= (pMP->counter_BTdst + (Int)(pMP->framerate*1.0 + 0.5)))
789    {
790        rc->TMN_TH -= (Int)(pMP->target_bits_per_frame / 10.0);
791        rc->T = rc->TMN_TH - rc->TMN_W;
792        pMP->counter_BTsrc++;
793        pMP->diff_counter--;
794    }
795
796}
797
798
799/* ======================================================================== */
800/*  Function : updateRateControl                                            */
801/*  Date     : 11/17/2000                                                   */
802/*  Purpose  :Update the RD Modal (After Encoding the Current Frame)        */
803/*  In/out   :                                                              */
804/*  Return   :                                                              */
805/*  Modified :                                                              */
806/* ======================================================================== */
807
808void updateRateControl(rateControl *rc, VideoEncData *video)
809{
810    Int  frame_bits;
811
812
813    /* rate contro\l */
814    frame_bits = (Int)(rc->bitrate / rc->framerate);
815    rc->TMN_W += (rc->Rc - rc->TMN_TH);
816    rc->VBV_fullness += (rc->Rc - frame_bits); //rc->Rp);
817    //if(rc->VBV_fullness < 0) rc->VBV_fullness = -1;
818
819    rc->encoded_frames++;
820
821    /* frame dropping */
822    rc->skip_next_frame = 0;
823
824    if ((video->encParams->H263_Enabled && rc->Rc > video->encParams->maxFrameSize) || /*  For H263/short header mode, drop the frame if the actual frame size exceeds the bound */
825            (rc->VBV_fullness > rc->Bs / 2 && !rc->no_pre_skip)) /* skip the current frame */ /* rc->Bs */
826    {
827        rc->TMN_W -= (rc->Rc - rc->TMN_TH);
828        rc->VBV_fullness -= rc->Rc;
829        rc->skip_next_frame = -1;
830    }
831    else if ((float)(rc->VBV_fullness - rc->VBV_fullness_offset) > (rc->Bs / 2 - rc->VBV_fullness_offset)*0.95 &&
832             !rc->no_frame_skip) /* skip next frame */
833    {
834        rc->VBV_fullness -= frame_bits; //rc->Rp;
835        rc->skip_next_frame = 1;
836        /*  skip more than 1 frames  */
837        //while(rc->VBV_fullness > rc->Bs*0.475)
838        while ((rc->VBV_fullness - rc->VBV_fullness_offset) > (rc->Bs / 2 - rc->VBV_fullness_offset)*0.95)
839        {
840            rc->VBV_fullness -= frame_bits; //rc->Rp;
841            rc->skip_next_frame++;
842        }
843        /* END  */
844    }
845
846}
847
848/* ======================================================================== */
849/*  Function : updateRC_PostProc                                            */
850/*  Date     : 04/08/2002                                                   */
851/*  Purpose  : Remaing RC update stuff for frame skip and buffer underflow  */
852/*             check                                                        */
853/*  In/out   :                                                              */
854/*  Return   :                                                              */
855/*  Modified :                                                              */
856/* ======================================================================== */
857void updateRC_PostProc(rateControl *rc, VideoEncData *video)
858{
859    MultiPass *pMP = video->pMP[video->currLayer];
860
861    if (rc->skip_next_frame == 1 && !rc->no_frame_skip) /* skip next frame */
862    {
863        pMP->counter_BTsrc += 10 * rc->skip_next_frame;
864
865    }
866    else if (rc->skip_next_frame == -1 && !rc->no_pre_skip) /* skip current frame */
867    {
868        pMP->counter_BTdst -= pMP->diff_counter;
869        pMP->counter_BTsrc += 10;
870
871        pMP->sum_mad -= pMP->mad;
872        pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames - pMP->mad) / (float)(pMP->encoded_frames - 1 + 0.0001);
873        pMP->sum_QP  -= pMP->QP;
874        pMP->encoded_frames --;
875    }
876    /* some stuff in update VBV_fullness remains here */
877    //if(rc->VBV_fullness < -rc->Bs/2) /* rc->Bs */
878    if (rc->VBV_fullness < rc->low_bound)
879    {
880        rc->VBV_fullness = rc->low_bound; // -rc->Bs/2;
881        rc->TMN_W = rc->VBV_fullness - rc->low_bound;
882        pMP->counter_BTsrc = pMP->counter_BTdst + (Int)((float)(rc->Bs / 2 - rc->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
883    }
884}
885
886