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
25 void targetBitCalculation(void *input);
26 void calculateQuantizer_Multipass(void *video);
27 void updateRateControl(rateControl *rc, VideoEncData *video);
28 void updateRC_PostProc(rateControl *rc, VideoEncData *video);
29
30 /***************************************************************************
31 ************** RC APIs to core encoding modules *******************
32
33 PV_STATUS RC_Initialize(void *video);
34 PV_STATUS RC_Cleanup(rateControl *rc[],Int numLayers);
35 PV_STATUS RC_VopQPSetting(VideoEncData *video,rateControl *rc[]);
36 PV_STATUS RC_VopUpdateStat(VideoEncData *video,rateControl *rc[]);
37 PV_STATUS RC_UpdateBuffer(VideoEncData *video, Int currLayer, Int num_skip);
38 Int RC_GetSkipNextFrame(VideoEncData *video,Int currLayer);
39 void RC_ResetSkipNextFrame(void *video,Int currLayer);
40
41 PV_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
RC_Initialize(void * input)50 PV_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
RC_Cleanup(rateControl * rc[],Int numLayers)156 PV_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
RC_VopQPSetting(VideoEncData * video,rateControl * prc[])183 PV_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
SaveRDSamples(MultiPass * pMP,Int counter_samples)271 Void 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
RC_VopUpdateStat(VideoEncData * video,rateControl * rc)291 PV_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
RC_GetSkipNextFrame(VideoEncData * video,Int currLayer)360 Int RC_GetSkipNextFrame(VideoEncData *video, Int currLayer)
361 {
362 return video->rc[currLayer]->skip_next_frame;
363 }
364
RC_ResetSkipNextFrame(VideoEncData * video,Int currLayer)365 void 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 /* Input argument "video" is guaranteed non-null by caller */
381 /* ======================================================================== */
382
RC_UpdateBuffer(VideoEncData * video,Int currLayer,Int num_skip)383 PV_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 (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
RC_UpdateBXRCParams(void * input)416 PV_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 /* Input argument "input" is guaranteed non-null by caller */
528 /* ================================================================================ */
529
targetBitCalculation(void * input)530 void targetBitCalculation(void *input)
531 {
532 VideoEncData *video = (VideoEncData *) input;
533 MultiPass *pMP = video->pMP[video->currLayer];
534 Vol *currVol = video->vol[video->currLayer];
535 rateControl *rc = video->rc[video->currLayer];
536
537 float curr_mad;//, average_mad;
538 Int diff_counter_BTsrc, diff_counter_BTdst, prev_counter_diff, curr_counter_diff, bound;
539 /* BT = Bit Transfer, for pMP->counter_BTsrc, pMP->counter_BTdst */
540
541 if (currVol == NULL || pMP == NULL || rc == NULL)
542 return;
543
544 /* some stuff about frame dropping remained here to be done because pMP cannot be inserted into updateRateControl()*/
545 updateRC_PostProc(rc, video);
546
547 /* update pMP->counter_BTsrc and pMP->counter_BTdst to avoid interger overflow */
548 if (pMP->counter_BTsrc > 1000 && pMP->counter_BTdst > 1000)
549 {
550 pMP->counter_BTsrc -= 1000;
551 pMP->counter_BTdst -= 1000;
552 }
553
554 /* ---------------------------------------------------------------------------------------------------*/
555 /* target calculation */
556 curr_mad = video->sumMAD / (float)currVol->nTotalMB;
557 if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
558 diff_counter_BTsrc = diff_counter_BTdst = 0;
559 pMP->diff_counter = 0;
560
561
562 /*1.calculate average mad */
563 pMP->sum_mad += curr_mad;
564 //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*/
565 //pMP->aver_mad = average_mad;
566 if (pMP->encoded_frames >= 0) /* pMP->encoded_frames is set to -1 initially, so forget about the very first I frame */
567 pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames + curr_mad) / (pMP->encoded_frames + 1);
568
569 if (pMP->overlapped_win_size > 0 && pMP->encoded_frames_prev >= 0) /* 7/31/03 */
570 pMP->aver_mad_prev = (pMP->aver_mad_prev * pMP->encoded_frames_prev + curr_mad) / (pMP->encoded_frames_prev + 1);
571
572 /*2.average_mad, mad ==> diff_counter_BTsrc, diff_counter_BTdst */
573 if (pMP->overlapped_win_size == 0)
574 {
575 /* original verison */
576 if (curr_mad > pMP->aver_mad*1.1)
577 {
578 if (curr_mad / (pMP->aver_mad + 0.0001) > 2)
579 diff_counter_BTdst = (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.4) - 10;
580 //diff_counter_BTdst = (Int)((sqrt(curr_mad/pMP->aver_mad)*2+curr_mad/pMP->aver_mad)/(3*0.1) + 0.4) - 10;
581 else
582 diff_counter_BTdst = (Int)(curr_mad / (pMP->aver_mad + 0.0001) * 10 + 0.4) - 10;
583 }
584 else /* curr_mad <= average_mad*1.1 */
585 //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);
586 diff_counter_BTsrc = 10 - (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.5);
587 //diff_counter_BTsrc = 10 - (Int)(curr_mad/pMP->aver_mad/0.1 + 0.5)
588
589 /* actively fill in the possible gap */
590 if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 &&
591 curr_mad <= pMP->aver_mad*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
592 diff_counter_BTsrc = 1;
593
594 }
595 else if (pMP->overlapped_win_size > 0)
596 {
597 /* transition time: use previous average mad "pMP->aver_mad_prev" instead of the current average mad "pMP->aver_mad" */
598 if (curr_mad > pMP->aver_mad_prev*1.1)
599 {
600 if (curr_mad / pMP->aver_mad_prev > 2)
601 diff_counter_BTdst = (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.4) - 10;
602 //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;
603 else
604 diff_counter_BTdst = (Int)(curr_mad / (pMP->aver_mad_prev + 0.0001) * 10 + 0.4) - 10;
605 }
606 else /* curr_mad <= average_mad*1.1 */
607 //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);
608 diff_counter_BTsrc = 10 - (Int)(M4VENC_SQRT(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.5);
609 //diff_counter_BTsrc = 10 - (Int)(curr_mad/pMP->aver_mad_prev/0.1 + 0.5)
610
611 /* actively fill in the possible gap */
612 if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 &&
613 curr_mad <= pMP->aver_mad_prev*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
614 diff_counter_BTsrc = 1;
615
616 if (--pMP->overlapped_win_size <= 0) pMP->overlapped_win_size = 0;
617 }
618
619
620 /* if difference is too much, do clipping */
621 /* First, set the upper bound for current bit allocation variance: 80% of available buffer */
622 bound = (Int)((rc->Bs / 2 - rc->VBV_fullness) * 0.6 / (pMP->target_bits_per_frame / 10)); /* rc->Bs */
623 diff_counter_BTsrc = PV_MIN(diff_counter_BTsrc, bound);
624 diff_counter_BTdst = PV_MIN(diff_counter_BTdst, bound);
625
626 /* Second, set another upper bound for current bit allocation: 4-5*bitrate/framerate */
627 bound = 50;
628 // if(video->encParams->RC_Type == CBR_LOWDELAY)
629 // not necessary bound = 10; /* 1/17/02 -- For Low delay */
630
631 diff_counter_BTsrc = PV_MIN(diff_counter_BTsrc, bound);
632 diff_counter_BTdst = PV_MIN(diff_counter_BTdst, bound);
633
634
635 /* Third, check the buffer */
636 prev_counter_diff = pMP->counter_BTdst - pMP->counter_BTsrc;
637 curr_counter_diff = prev_counter_diff + (diff_counter_BTdst - diff_counter_BTsrc);
638
639 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) )
640 { //diff_counter_BTsrc = diff_counter_BTdst = 0;
641
642 if (curr_counter_diff > rc->max_BitVariance_num && diff_counter_BTdst)
643 {
644 diff_counter_BTdst = (rc->max_BitVariance_num - prev_counter_diff) + diff_counter_BTsrc;
645 if (diff_counter_BTdst < 0) diff_counter_BTdst = 0;
646 }
647
648 else if (curr_counter_diff < -rc->max_BitVariance_num && diff_counter_BTsrc)
649 {
650 diff_counter_BTsrc = diff_counter_BTdst - (-rc->max_BitVariance_num - prev_counter_diff);
651 if (diff_counter_BTsrc < 0) diff_counter_BTsrc = 0;
652 }
653 }
654
655
656 /*3.diff_counter_BTsrc, diff_counter_BTdst ==> TMN_TH */
657 //rc->TMN_TH = (Int)((float)pMP->bitrate/pMP->framerate);
658 rc->TMN_TH = (Int)(pMP->target_bits_per_frame);
659 pMP->diff_counter = 0;
660
661 if (diff_counter_BTsrc)
662 {
663 rc->TMN_TH -= (Int)(pMP->target_bits_per_frame * diff_counter_BTsrc * 0.1);
664 pMP->diff_counter = -diff_counter_BTsrc;
665 }
666 else if (diff_counter_BTdst)
667 {
668 rc->TMN_TH += (Int)(pMP->target_bits_per_frame * diff_counter_BTdst * 0.1);
669 pMP->diff_counter = diff_counter_BTdst;
670 }
671
672
673 /*4.update pMP->counter_BTsrc, pMP->counter_BTdst */
674 pMP->counter_BTsrc += diff_counter_BTsrc;
675 pMP->counter_BTdst += diff_counter_BTdst;
676
677
678 /*5.target bit calculation */
679 rc->T = rc->TMN_TH - rc->TMN_W;
680 //rc->T = rc->TMN_TH - (Int)((float)rc->TMN_W/rc->frameRate);
681
682 if (video->encParams->H263_Enabled && rc->T > video->encParams->maxFrameSize)
683 {
684 rc->T = video->encParams->maxFrameSize; // added this 11/07/05
685 }
686
687 }
688
689 /* ================================================================================ */
690 /* Function : calculateQuantizer_Multipass */
691 /* Date : 10/01/2001 */
692 /* Purpose : variable rate bit allocation + new QP determination scheme */
693 /* */
694 /* In/out : rc->T and rc->Qc */
695 /* Return : Void */
696 /* Modified : */
697 /* Input argument "input" is guaranteed non-null by caller */
698 /* ================================================================================ */
699
700 /* Mad based variable bit allocation + QP calculation with a new quadratic method */
calculateQuantizer_Multipass(void * input)701 void calculateQuantizer_Multipass(void *input)
702 {
703 VideoEncData *video = (VideoEncData *) input;
704 MultiPass *pMP = video->pMP[video->currLayer];
705 Vol *currVol = video->vol[video->currLayer];
706 rateControl *rc = video->rc[video->currLayer];
707
708 Int prev_QP, prev_actual_bits, curr_target, i, j;
709
710 float curr_mad, prev_mad, curr_RD, prev_RD, average_mad, aver_QP;
711
712
713 if (currVol == NULL || pMP == NULL || rc == NULL)
714 return;
715
716 /* Mad based variable bit allocation */
717 targetBitCalculation((void*) video);
718
719 if (rc->T <= 0 || video->sumMAD == 0)
720 {
721 if (rc->T < 0) rc->Qc = 31;
722 return;
723 }
724
725 /* ---------------------------------------------------------------------------------------------------*/
726 /* current frame QP estimation */
727 curr_target = rc->T;
728 curr_mad = video->sumMAD / (float)currVol->nTotalMB;
729 if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
730 curr_RD = (float)curr_target / curr_mad;
731
732 /* Another version of search the optimal point */
733 prev_actual_bits = pMP->pRDSamples[0][0].actual_bits;
734 prev_mad = pMP->pRDSamples[0][0].mad;
735
736 for (i = 0, j = 0; i < pMP->frameRange; i++)
737 {
738 if (pMP->pRDSamples[i][0].mad != 0 && prev_mad != 0 &&
739 PV_ABS(prev_mad - curr_mad) > PV_ABS(pMP->pRDSamples[i][0].mad - curr_mad))
740 {
741 prev_mad = pMP->pRDSamples[i][0].mad;
742 prev_actual_bits = pMP->pRDSamples[i][0].actual_bits;
743 j = i;
744 }
745 }
746 prev_QP = pMP->pRDSamples[j][0].QP;
747 for (i = 1; i < pMP->samplesPerFrame[j]; i++)
748 {
749 if (PV_ABS(prev_actual_bits - curr_target) > PV_ABS(pMP->pRDSamples[j][i].actual_bits - curr_target))
750 {
751 prev_actual_bits = pMP->pRDSamples[j][i].actual_bits;
752 prev_QP = pMP->pRDSamples[j][i].QP;
753 }
754 }
755
756 // quadratic approximation
757 prev_RD = (float)prev_actual_bits / prev_mad;
758 //rc->Qc = (Int)(prev_QP * sqrt(prev_actual_bits/curr_target) + 0.4);
759 if (prev_QP == 1) // 11/14/05, added this to allow getting out of QP = 1 easily
760 {
761 rc->Qc = (Int)(prev_RD / curr_RD + 0.5);
762 }
763 else
764 {
765 rc->Qc = (Int)(prev_QP * M4VENC_SQRT(prev_RD / curr_RD) + 0.9);
766
767 if (prev_RD / curr_RD > 0.5 && prev_RD / curr_RD < 2.0)
768 rc->Qc = (Int)(prev_QP * (M4VENC_SQRT(prev_RD / curr_RD) + prev_RD / curr_RD) / 2.0 + 0.9); /* Quadratic and linear approximation */
769 else
770 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);
771 }
772 //rc->Qc =(Int)(prev_QP * sqrt(prev_RD/curr_RD) + 0.4);
773 // 11/08/05
774 // lower bound on Qc should be a function of curr_mad
775 // When mad is already low, lower bound on Qc doesn't have to be small.
776 // Note, this doesn't work well for low complexity clip encoded at high bit rate
777 // it doesn't hit the target bit rate due to this QP lower bound.
778 /// if((curr_mad < 8) && (rc->Qc < 12)) rc->Qc = 12;
779 // else if((curr_mad < 128) && (rc->Qc < 3)) rc->Qc = 3;
780
781 if (rc->Qc < 1) rc->Qc = 1;
782 if (rc->Qc > 31) rc->Qc = 31;
783
784
785 /* active bit resource protection */
786 aver_QP = (pMP->encoded_frames == 0 ? 0 : pMP->sum_QP / (float)pMP->encoded_frames);
787 average_mad = (pMP->encoded_frames == 0 ? 0 : pMP->sum_mad / (float)pMP->encoded_frames); /* this function is called from the scond encoded frame*/
788 if (pMP->diff_counter == 0 &&
789 ((float)rc->Qc <= aver_QP*1.1 || curr_mad <= average_mad*1.1) &&
790 pMP->counter_BTsrc <= (pMP->counter_BTdst + (Int)(pMP->framerate*1.0 + 0.5)))
791 {
792 rc->TMN_TH -= (Int)(pMP->target_bits_per_frame / 10.0);
793 rc->T = rc->TMN_TH - rc->TMN_W;
794 pMP->counter_BTsrc++;
795 pMP->diff_counter--;
796 }
797
798 }
799
800
801 /* ======================================================================== */
802 /* Function : updateRateControl */
803 /* Date : 11/17/2000 */
804 /* Purpose :Update the RD Modal (After Encoding the Current Frame) */
805 /* In/out : */
806 /* Return : */
807 /* Modified : */
808 /* ======================================================================== */
809
updateRateControl(rateControl * rc,VideoEncData * video)810 void updateRateControl(rateControl *rc, VideoEncData *video)
811 {
812 Int frame_bits;
813
814
815 /* rate contro\l */
816 frame_bits = (Int)(rc->bitrate / rc->framerate);
817 rc->TMN_W += (rc->Rc - rc->TMN_TH);
818 rc->VBV_fullness += (rc->Rc - frame_bits); //rc->Rp);
819 //if(rc->VBV_fullness < 0) rc->VBV_fullness = -1;
820
821 rc->encoded_frames++;
822
823 /* frame dropping */
824 rc->skip_next_frame = 0;
825
826 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 */
827 (rc->VBV_fullness > rc->Bs / 2 && !rc->no_pre_skip)) /* skip the current frame */ /* rc->Bs */
828 {
829 rc->TMN_W -= (rc->Rc - rc->TMN_TH);
830 rc->VBV_fullness -= rc->Rc;
831 rc->skip_next_frame = -1;
832 }
833 else if ((float)(rc->VBV_fullness - rc->VBV_fullness_offset) > (rc->Bs / 2 - rc->VBV_fullness_offset)*0.95 &&
834 !rc->no_frame_skip) /* skip next frame */
835 {
836 rc->VBV_fullness -= frame_bits; //rc->Rp;
837 rc->skip_next_frame = 1;
838 /* skip more than 1 frames */
839 //while(rc->VBV_fullness > rc->Bs*0.475)
840 while ((rc->VBV_fullness - rc->VBV_fullness_offset) > (rc->Bs / 2 - rc->VBV_fullness_offset)*0.95)
841 {
842 rc->VBV_fullness -= frame_bits; //rc->Rp;
843 rc->skip_next_frame++;
844 }
845 /* END */
846 }
847
848 }
849
850 /* ======================================================================== */
851 /* Function : updateRC_PostProc */
852 /* Date : 04/08/2002 */
853 /* Purpose : Remaing RC update stuff for frame skip and buffer underflow */
854 /* check */
855 /* In/out : */
856 /* Return : */
857 /* Modified : */
858 /* ======================================================================== */
updateRC_PostProc(rateControl * rc,VideoEncData * video)859 void updateRC_PostProc(rateControl *rc, VideoEncData *video)
860 {
861 MultiPass *pMP = video->pMP[video->currLayer];
862
863 if (rc->skip_next_frame == 1 && !rc->no_frame_skip) /* skip next frame */
864 {
865 pMP->counter_BTsrc += 10 * rc->skip_next_frame;
866
867 }
868 else if (rc->skip_next_frame == -1 && !rc->no_pre_skip) /* skip current frame */
869 {
870 pMP->counter_BTdst -= pMP->diff_counter;
871 pMP->counter_BTsrc += 10;
872
873 pMP->sum_mad -= pMP->mad;
874 pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames - pMP->mad) / (float)(pMP->encoded_frames - 1 + 0.0001);
875 pMP->sum_QP -= pMP->QP;
876 pMP->encoded_frames --;
877 }
878 /* some stuff in update VBV_fullness remains here */
879 //if(rc->VBV_fullness < -rc->Bs/2) /* rc->Bs */
880 if (rc->VBV_fullness < rc->low_bound)
881 {
882 rc->VBV_fullness = rc->low_bound; // -rc->Bs/2;
883 rc->TMN_W = rc->VBV_fullness - rc->low_bound;
884 pMP->counter_BTsrc = pMP->counter_BTdst + (Int)((float)(rc->Bs / 2 - rc->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
885 }
886 }
887
888