[cvs] / xvidcore / src / utils / mbtransquant.c Repository:
ViewVC logotype

Diff of /xvidcore/src/utils/mbtransquant.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7, Wed Jun 12 20:38:41 2002 UTC revision 1.19, Sun Nov 17 00:51:10 2002 UTC
# Line 1  Line 1 
1   /******************************************************************************  /*****************************************************************************
2    *                                                                            *   *
3    *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *   *  XVID MPEG-4 VIDEO CODEC
4    *                                                                            *   *  - MacroBlock transfer and quantization -
5    *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *   *
6    *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *   *  Copyright(C) 2002-2001 Christoph Lampert <gruel@web.de>
7    *  software module in hardware or software products are advised that its     *   *               2002-2001 Michael Militzer <isibaar@xvid.org>
8    *  use may infringe existing patents or copyrights, and any such use         *   *               2002-2001 Peter Ross <pross@xvid.org>
9    *  would be at such party's own risk.  The original developer of this        *   *               2002      Daniel Smith <danielsmith@astroboymail.com>
10    *  software module and his/her company, and subsequent editors and their     *   *
11    *  companies, will have no liability for use of this software or             *   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
12    *  modifications or derivatives thereof.                                     *   *
13    *                                                                            *   *  XviD is free software; you can redistribute it and/or modify it
14    *  XviD is free software; you can redistribute it and/or modify it           *   *  under the terms of the GNU General Public License as published by
15    *  under the terms of the GNU General Public License as published by         *   *  the Free Software Foundation; either version 2 of the License, or
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  (at your option) any later version.
17    *  (at your option) any later version.                                       *   *
18    *                                                                            *   *  This program is distributed in the hope that it will be useful,
19    *  XviD is distributed in the hope that it will be useful, but               *   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *  GNU General Public License for more details.
22    *  GNU General Public License for more details.                              *   *
23    *                                                                            *   *  You should have received a copy of the GNU General Public License
24    *  You should have received a copy of the GNU General Public License         *   *  along with this program; if not, write to the Free Software
25    *  along with this program; if not, write to the Free Software               *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *   *
27    *                                                                            *   *  Under section 8 of the GNU General Public License, the copyright
28    ******************************************************************************/   *  holders of XVID explicitly forbid distribution in the following
29     *  countries:
30   /******************************************************************************   *
31    *                                                                            *   *    - Japan
32    *  mbtransquant.c                                                            *   *    - United States of America
33    *                                                                            *   *
34    *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *   *  Linking XviD statically or dynamically with other modules is making a
35    *  Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org>                  *   *  combined work based on XviD.  Thus, the terms and conditions of the
36    *                                                                            *   *  GNU General Public License cover the whole combination.
37    *  For more information visit the XviD homepage: http://www.xvid.org         *   *
38    *                                                                            *   *  As a special exception, the copyright holders of XviD give you
39    ******************************************************************************/   *  permission to link XviD with independent modules that communicate with
40     *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
41   /******************************************************************************   *  license terms of these independent modules, and to copy and distribute
42    *                                                                            *   *  the resulting combined work under terms of your choice, provided that
43    *  Revision history:                                                         *   *  every copy of the combined work is accompanied by a complete copy of
44    *                                                                            *   *  the source code of XviD (the version of XviD used to produce the
45    *  29.03.2002 interlacing speedup - used transfer strides instead of   *  combined work), being distributed under the terms of the GNU General
46    *             manual field-to-frame conversion   *  Public License plus this exception.  An independent module is a module
47    *  26.03.2002 interlacing support - moved transfers outside loops   *  which is not derived from or based on XviD.
48    *  22.12.2001 get_dc_scaler() moved to common.h   *
49    *  19.11.2001 introduced coefficient thresholding (Isibaar)                  *   *  Note that people who make modified versions of XviD are not obligated
50    *  17.11.2001 initial version                                                *   *  to grant this special exception for their modified versions; it is
51    *                                                                            *   *  their choice whether to do so.  The GNU General Public License gives
52    ******************************************************************************/   *  permission to release a modified version without this exception; this
53     *  exception also makes it possible to release a modified version which
54     *  carries forward this exception.
55     *
56     * $Id$
57     *
58     ****************************************************************************/
59    
60  #include <string.h>  #include <string.h>
61    
# Line 68  Line 74 
74  #define MIN(X, Y) ((X)<(Y)?(X):(Y))  #define MIN(X, Y) ((X)<(Y)?(X):(Y))
75  #define MAX(X, Y) ((X)>(Y)?(X):(Y))  #define MAX(X, Y) ((X)>(Y)?(X):(Y))
76    
77  #define TOOSMALL_LIMIT 1                /* skip blocks having a coefficient sum below this value */  #define TOOSMALL_LIMIT 3                /* skip blocks having a coefficient sum below this value */
78    
79  /* this isnt pretty, but its better than 20 ifdefs */  /* this isnt pretty, but its better than 20 ifdefs */
80    
# Line 105  Line 111 
111    
112          start_timer();          start_timer();
113          pMB->field_dct = 0;          pMB->field_dct = 0;
114          if ((frame->global_flags & XVID_INTERLACING)) {          if ((frame->global_flags & XVID_INTERLACING) &&
115                    (x_pos>0) && (x_pos<pParam->mb_width-1) &&
116                    (y_pos>0) && (y_pos<pParam->mb_height-1)) {
117                  pMB->field_dct = MBDecideFieldDCT(data);                  pMB->field_dct = MBDecideFieldDCT(data);
118          }          }
119          stop_interlacing_timer();          stop_interlacing_timer();
# Line 183  Line 191 
191    
192          start_timer();          start_timer();
193          pMB->field_dct = 0;          pMB->field_dct = 0;
194          if ((frame->global_flags & XVID_INTERLACING)) {          if ((frame->global_flags & XVID_INTERLACING) &&
195                    (x_pos>0) && (x_pos<pParam->mb_width-1) &&
196                    (y_pos>0) && (y_pos<pParam->mb_height-1)) {
197                  pMB->field_dct = MBDecideFieldDCT(data);                  pMB->field_dct = MBDecideFieldDCT(data);
198          }          }
199          stop_interlacing_timer();          stop_interlacing_timer();
# Line 207  Line 217 
217                          stop_quant_timer();                          stop_quant_timer();
218                  }                  }
219    
220                  if (sum >= TOOSMALL_LIMIT) {    // skip block ?                  if ((sum >= TOOSMALL_LIMIT) || (qcoeff[i*64] != 0) ||
221                            (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) {
222    
223                          if (pParam->m_quant_type == H263_QUANT) {                          if (pParam->m_quant_type == H263_QUANT) {
224                                  start_timer();                                  start_timer();
# Line 251  Line 262 
262    
263  }  }
264    
265    void
266    MBTransQuantIntra2(const MBParam * pParam,
267                                      FRAMEINFO * frame,
268                                      MACROBLOCK * pMB,
269                                      const uint32_t x_pos,
270                                      const uint32_t y_pos,
271                                      int16_t data[6 * 64],
272                                      int16_t qcoeff[6 * 64])
273    {
274            MBTrans(pParam,frame,pMB,x_pos,y_pos,data);
275            MBfDCT(pParam,frame,pMB,data);
276            MBQuantIntra(pParam,frame,pMB,data,qcoeff);
277            MBDeQuantIntra(pParam,frame->quant,data,qcoeff);
278            MBiDCT(data,0x3F);
279            MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F);
280    }
281    
282    
283    uint8_t
284    MBTransQuantInter2(const MBParam * pParam,
285                                      FRAMEINFO * frame,
286                                      MACROBLOCK * pMB,
287                                      const uint32_t x_pos,
288                                      const uint32_t y_pos,
289                                      int16_t data[6 * 64],
290                                      int16_t qcoeff[6 * 64])
291    {
292            uint8_t cbp;
293    
294    /* there is no MBTrans for Inter block, that's done in motion compensation already */
295    
296            MBfDCT(pParam,frame,pMB,data);
297            cbp = MBQuantInter(pParam,frame->quant,data,qcoeff);
298            MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp);
299            MBiDCT(data,cbp);
300            MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp);
301    
302            return cbp;
303    }
304    
305    uint8_t
306    MBTransQuantInterBVOP(const MBParam * pParam,
307                                      FRAMEINFO * frame,
308                                      MACROBLOCK * pMB,
309                                      int16_t data[6 * 64],
310                                      int16_t qcoeff[6 * 64])
311    {
312            uint8_t cbp;
313    
314    /* there is no MBTrans for Inter block, that's done in motion compensation already */
315    
316            MBfDCT(pParam,frame,pMB,data);
317            cbp = MBQuantInter(pParam,frame->quant,data,qcoeff);
318    
319    /* we don't have to DeQuant, iDCT and Transfer back data for B-frames */
320    
321            return cbp;
322    }
323    
324    
325    void
326    MBfDCT(const MBParam * pParam,
327                                      FRAMEINFO * frame,
328                                      MACROBLOCK * pMB,
329                                      int16_t data[6 * 64])
330    {
331            int i;
332    
333            start_timer();
334            pMB->field_dct = 0;
335            if ((frame->global_flags & XVID_INTERLACING)) {
336                    pMB->field_dct = MBDecideFieldDCT(data);
337            }
338            stop_interlacing_timer();
339    
340            for (i = 0; i < 6; i++) {
341                    start_timer();
342                    fdct(&data[i * 64]);
343                    stop_dct_timer();
344            }
345    }
346    
347    void
348    MBQuantDeQuantIntra(const MBParam * pParam,
349                                            FRAMEINFO * frame,
350                                            MACROBLOCK * pMB,
351                                            int16_t qcoeff[6 * 64],
352                                            int16_t data[6*64])
353    {
354            int i;
355            int iQuant = frame->quant;
356    
357            start_timer();
358            pMB->field_dct = 0;
359            if ((frame->global_flags & XVID_INTERLACING)) {
360                    pMB->field_dct = MBDecideFieldDCT(data);
361            }
362            stop_interlacing_timer();
363    
364            for (i = 0; i < 6; i++) {
365                    uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
366    
367                    if (pParam->m_quant_type == H263_QUANT) {
368                            start_timer();
369                            quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
370                            stop_quant_timer();
371    
372                            start_timer();
373                            dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);
374                            stop_iquant_timer();
375                    } else {
376                            start_timer();
377                            quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
378                            stop_quant_timer();
379    
380                            start_timer();
381                            dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);
382                            stop_iquant_timer();
383                    }
384            }
385    }
386    
387    void
388    MBQuantIntra(const MBParam * pParam,
389                             FRAMEINFO * frame,
390                             MACROBLOCK *pMB,
391                             int16_t data[6 * 64],
392                         int16_t qcoeff[6 * 64])
393    {
394            int i;
395            int iQuant = frame->quant;
396    
397            start_timer();
398            pMB->field_dct = 0;
399            if ((frame->global_flags & XVID_INTERLACING)) {
400                    pMB->field_dct = MBDecideFieldDCT(data);
401            }
402            stop_interlacing_timer();
403    
404            for (i = 0; i < 6; i++) {
405                    uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
406    
407                    if (pParam->m_quant_type == H263_QUANT) {
408                            start_timer();
409                            quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
410                            stop_quant_timer();
411                    } else {
412                            start_timer();
413                            quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
414                            stop_quant_timer();
415                    }
416            }
417    }
418    
419    void
420    MBDeQuantIntra(const MBParam * pParam,
421                               const int iQuant,
422                                      int16_t qcoeff[6 * 64],
423                                      int16_t data[6*64])
424    {
425            int i;
426    
427            for (i = 0; i < 6; i++) {
428                    uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
429    
430                    if (pParam->m_quant_type == H263_QUANT) {
431                            start_timer();
432                            dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);
433                            stop_iquant_timer();
434                    } else {
435                            start_timer();
436                            dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler);
437                            stop_iquant_timer();
438                    }
439            }
440    }
441    
442    uint8_t
443    MBQuantInter(const MBParam * pParam,
444                             const int iQuant,
445                                      int16_t data[6 * 64],
446                                      int16_t qcoeff[6 * 64])
447    {
448    
449            int i;
450            uint8_t cbp = 0;
451            int sum;
452    
453            for (i = 0; i < 6; i++) {
454    
455                    if (pParam->m_quant_type == 0) {
456                            start_timer();
457                            sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant);
458                            stop_quant_timer();
459                    } else {
460                            start_timer();
461                            sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant);
462                            stop_quant_timer();
463                    }
464    
465                    if (sum >= TOOSMALL_LIMIT) {    // skip block ?
466                            cbp |= 1 << (5 - i);
467                    }
468            }
469            return cbp;
470    }
471    
472    void
473    MBDeQuantInter( const MBParam * pParam,
474                                    const int iQuant,
475                                      int16_t data[6 * 64],
476                                      int16_t qcoeff[6 * 64],
477                                      const uint8_t cbp)
478    {
479            int i;
480    
481            for (i = 0; i < 6; i++) {
482                    if (cbp & (1 << (5 - i)))
483                    {
484                            if (pParam->m_quant_type == H263_QUANT) {
485                                    start_timer();
486                                    dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
487                                    stop_iquant_timer();
488                            } else {
489                                    start_timer();
490                                    dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
491                                    stop_iquant_timer();
492                            }
493                    }
494            }
495    }
496    
497    void
498    MBiDCT( int16_t data[6 * 64],
499                    const uint8_t cbp)
500    {
501            int i;
502    
503            for (i = 0; i < 6; i++) {
504                    if (cbp & (1 << (5 - i)))
505                    {
506                            start_timer();
507                            idct(&data[i * 64]);
508                            stop_idct_timer();
509    
510                    }
511            }
512    }
513    
514    
515    void
516    MBTrans(const MBParam * pParam,
517                                      FRAMEINFO * frame,
518                                      MACROBLOCK * pMB,
519                                      const uint32_t x_pos,
520                                      const uint32_t y_pos,
521                                      int16_t data[6 * 64])
522    {
523            uint32_t stride = pParam->edged_width;
524            uint32_t stride2 = stride / 2;
525            uint32_t next_block = stride * 8;
526            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
527            IMAGE *pCurrent = &frame->image;
528    
529            pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);
530            pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
531            pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
532    
533            start_timer();
534            transfer_8to16copy(&data[0 * 64], pY_Cur, stride);
535            transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride);
536            transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride);
537            transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride);
538            transfer_8to16copy(&data[4 * 64], pU_Cur, stride2);
539            transfer_8to16copy(&data[5 * 64], pV_Cur, stride2);
540            stop_transfer_timer();
541    }
542    
543    void
544    MBTransAdd(const MBParam * pParam,
545                                      FRAMEINFO * frame,
546                                      MACROBLOCK * pMB,
547                                      const uint32_t x_pos,
548                                      const uint32_t y_pos,
549                                      int16_t data[6 * 64],
550                                      const uint8_t cbp)
551    {
552            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
553            uint32_t stride = pParam->edged_width;
554            uint32_t stride2 = stride / 2;
555            uint32_t next_block = stride * 8;
556            IMAGE *pCurrent = &frame->image;
557    
558            pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);
559            pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
560            pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
561    
562            if (pMB->field_dct) {
563                    next_block = stride;
564                    stride *= 2;
565            }
566    
567            start_timer();
568            if (cbp & 32)
569                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
570            if (cbp & 16)
571                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
572            if (cbp & 8)
573                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
574            if (cbp & 4)
575                    transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride);
576            if (cbp & 2)
577                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
578            if (cbp & 1)
579                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
580            stop_transfer_timer();
581    }
582    
583    
584    
585  /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */  /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */
586    
# Line 292  Line 622 
622                  }                  }
623          }          }
624    
625          if (frame > field) {          if (frame > (field + 350)) {
626                  MBFrameToField(data);                  MBFrameToField(data);
627          }          }
628    
629          return (frame > field);          return (frame > (field + 350));
630  }  }
631    
632    

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.19

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4