You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.3KB

  1. /*
  2. * H263 internal header
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVCODEC_H263_H
  21. #define AVCODEC_H263_H
  22. // The defines below define the number of bits that are read at once for
  23. // reading vlc values. Changing these may improve speed and data cache needs
  24. // be aware though that decreasing them may need the number of stages that is
  25. // passed to get_vlc* to be increased.
  26. #define INTRA_MCBPC_VLC_BITS 6
  27. #define INTER_MCBPC_VLC_BITS 7
  28. #define CBPY_VLC_BITS 6
  29. #define TEX_VLC_BITS 9
  30. extern const AVRational ff_h263_pixel_aspect[16];
  31. extern const uint8_t cbpy_tab[16][2];
  32. extern const uint8_t mvtab[33][2];
  33. extern const uint8_t intra_MCBPC_code[9];
  34. extern const uint8_t intra_MCBPC_bits[9];
  35. extern const uint8_t inter_MCBPC_code[28];
  36. extern const uint8_t inter_MCBPC_bits[28];
  37. extern VLC intra_MCBPC_vlc;
  38. extern VLC inter_MCBPC_vlc;
  39. extern VLC cbpy_vlc;
  40. extern RLTable rl_inter;
  41. int h263_decode_motion(MpegEncContext * s, int pred, int f_code);
  42. av_const int ff_h263_aspect_to_info(AVRational aspect);
  43. static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){
  44. int l, bit_size, code;
  45. if (val == 0) {
  46. return mvtab[0][1];
  47. } else {
  48. bit_size = f_code - 1;
  49. /* modulo encoding */
  50. l= INT_BIT - 6 - bit_size;
  51. val = (val<<l)>>l;
  52. val--;
  53. code = (val >> bit_size) + 1;
  54. return mvtab[code][1] + 1 + bit_size;
  55. }
  56. }
  57. static inline void ff_h263_encode_motion_vector(MpegEncContext * s, int x, int y, int f_code){
  58. if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
  59. skip_put_bits(&s->pb,
  60. h263_get_motion_length(s, x, f_code)
  61. +h263_get_motion_length(s, y, f_code));
  62. }else{
  63. ff_h263_encode_motion(s, x, f_code);
  64. ff_h263_encode_motion(s, y, f_code);
  65. }
  66. }
  67. static inline int get_p_cbp(MpegEncContext * s,
  68. DCTELEM block[6][64],
  69. int motion_x, int motion_y){
  70. int cbp, i;
  71. if(s->flags & CODEC_FLAG_CBP_RD){
  72. int best_cbpy_score= INT_MAX;
  73. int best_cbpc_score= INT_MAX;
  74. int cbpc = (-1), cbpy= (-1);
  75. const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);
  76. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  77. for(i=0; i<4; i++){
  78. int score= inter_MCBPC_bits[i + offset] * lambda;
  79. if(i&1) score += s->coded_score[5];
  80. if(i&2) score += s->coded_score[4];
  81. if(score < best_cbpc_score){
  82. best_cbpc_score= score;
  83. cbpc= i;
  84. }
  85. }
  86. for(i=0; i<16; i++){
  87. int score= cbpy_tab[i ^ 0xF][1] * lambda;
  88. if(i&1) score += s->coded_score[3];
  89. if(i&2) score += s->coded_score[2];
  90. if(i&4) score += s->coded_score[1];
  91. if(i&8) score += s->coded_score[0];
  92. if(score < best_cbpy_score){
  93. best_cbpy_score= score;
  94. cbpy= i;
  95. }
  96. }
  97. cbp= cbpc + 4*cbpy;
  98. if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){
  99. if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0)
  100. cbp= 0;
  101. }
  102. for (i = 0; i < 6; i++) {
  103. if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
  104. s->block_last_index[i]= -1;
  105. s->dsp.clear_block(s->block[i]);
  106. }
  107. }
  108. }else{
  109. cbp= 0;
  110. for (i = 0; i < 6; i++) {
  111. if (s->block_last_index[i] >= 0)
  112. cbp |= 1 << (5 - i);
  113. }
  114. }
  115. return cbp;
  116. }
  117. static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
  118. int motion_x, int motion_y, int mb_type){
  119. int cbp=0, i;
  120. if(s->flags & CODEC_FLAG_CBP_RD){
  121. int score=0;
  122. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  123. for(i=0; i<6; i++){
  124. if(s->coded_score[i] < 0){
  125. score += s->coded_score[i];
  126. cbp |= 1 << (5 - i);
  127. }
  128. }
  129. if(cbp){
  130. int zero_score= -6;
  131. if ((motion_x | motion_y | s->dquant | mb_type) == 0){
  132. zero_score-= 4; //2*MV + mb_type + cbp bit
  133. }
  134. zero_score*= lambda;
  135. if(zero_score <= score){
  136. cbp=0;
  137. }
  138. }
  139. for (i = 0; i < 6; i++) {
  140. if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
  141. s->block_last_index[i]= -1;
  142. s->dsp.clear_block(s->block[i]);
  143. }
  144. }
  145. }else{
  146. for (i = 0; i < 6; i++) {
  147. if (s->block_last_index[i] >= 0)
  148. cbp |= 1 << (5 - i);
  149. }
  150. }
  151. return cbp;
  152. }
  153. #endif