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.

171 lines
6.3KB

  1. /*
  2. * MPEG4 decoder / encoder common code.
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "mpegvideo.h"
  23. #include "mpeg4video.h"
  24. #include "mpeg4data.h"
  25. uint8_t ff_mpeg4_static_rl_table_store[3][2][2*MAX_RUN + MAX_LEVEL + 3];
  26. int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s){
  27. switch(s->pict_type){
  28. case AV_PICTURE_TYPE_I:
  29. return 16;
  30. case AV_PICTURE_TYPE_P:
  31. case AV_PICTURE_TYPE_S:
  32. return s->f_code+15;
  33. case AV_PICTURE_TYPE_B:
  34. return FFMAX3(s->f_code, s->b_code, 2) + 15;
  35. default:
  36. return -1;
  37. }
  38. }
  39. void ff_mpeg4_clean_buffers(MpegEncContext *s)
  40. {
  41. int c_wrap, c_xy, l_wrap, l_xy;
  42. l_wrap= s->b8_stride;
  43. l_xy= (2*s->mb_y-1)*l_wrap + s->mb_x*2 - 1;
  44. c_wrap= s->mb_stride;
  45. c_xy= (s->mb_y-1)*c_wrap + s->mb_x - 1;
  46. #if 0
  47. /* clean DC */
  48. memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1);
  49. memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1);
  50. memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1);
  51. #endif
  52. /* clean AC */
  53. memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(int16_t));
  54. memset(s->ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t));
  55. memset(s->ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t));
  56. /* clean MV */
  57. // we can't clear the MVs as they might be needed by a b frame
  58. // memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(int16_t));
  59. // memset(s->motion_val, 0, 2*sizeof(int16_t)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
  60. s->last_mv[0][0][0]=
  61. s->last_mv[0][0][1]=
  62. s->last_mv[1][0][0]=
  63. s->last_mv[1][0][1]= 0;
  64. }
  65. #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
  66. #define tab_bias (tab_size/2)
  67. //used by mpeg4 and rv10 decoder
  68. void ff_mpeg4_init_direct_mv(MpegEncContext *s){
  69. int i;
  70. for(i=0; i<tab_size; i++){
  71. s->direct_scale_mv[0][i] = (i-tab_bias)*s->pb_time/s->pp_time;
  72. s->direct_scale_mv[1][i] = (i-tab_bias)*(s->pb_time-s->pp_time)/s->pp_time;
  73. }
  74. }
  75. static inline void ff_mpeg4_set_one_direct_mv(MpegEncContext *s, int mx, int my, int i){
  76. int xy= s->block_index[i];
  77. uint16_t time_pp= s->pp_time;
  78. uint16_t time_pb= s->pb_time;
  79. int p_mx, p_my;
  80. p_mx = s->next_picture.f.motion_val[0][xy][0];
  81. if((unsigned)(p_mx + tab_bias) < tab_size){
  82. s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias] + mx;
  83. s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
  84. : s->direct_scale_mv[1][p_mx + tab_bias];
  85. }else{
  86. s->mv[0][i][0] = p_mx*time_pb/time_pp + mx;
  87. s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
  88. : p_mx*(time_pb - time_pp)/time_pp;
  89. }
  90. p_my = s->next_picture.f.motion_val[0][xy][1];
  91. if((unsigned)(p_my + tab_bias) < tab_size){
  92. s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias] + my;
  93. s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my
  94. : s->direct_scale_mv[1][p_my + tab_bias];
  95. }else{
  96. s->mv[0][i][1] = p_my*time_pb/time_pp + my;
  97. s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my
  98. : p_my*(time_pb - time_pp)/time_pp;
  99. }
  100. }
  101. #undef tab_size
  102. #undef tab_bias
  103. /**
  104. *
  105. * @return the mb_type
  106. */
  107. int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
  108. const int mb_index= s->mb_x + s->mb_y*s->mb_stride;
  109. const int colocated_mb_type = s->next_picture.f.mb_type[mb_index];
  110. uint16_t time_pp;
  111. uint16_t time_pb;
  112. int i;
  113. //FIXME avoid divides
  114. // try special case with shifts for 1 and 3 B-frames?
  115. if(IS_8X8(colocated_mb_type)){
  116. s->mv_type = MV_TYPE_8X8;
  117. for(i=0; i<4; i++){
  118. ff_mpeg4_set_one_direct_mv(s, mx, my, i);
  119. }
  120. return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
  121. } else if(IS_INTERLACED(colocated_mb_type)){
  122. s->mv_type = MV_TYPE_FIELD;
  123. for(i=0; i<2; i++){
  124. int field_select = s->next_picture.f.ref_index[0][4 * mb_index + 2 * i];
  125. s->field_select[0][i]= field_select;
  126. s->field_select[1][i]= i;
  127. if(s->top_field_first){
  128. time_pp= s->pp_field_time - field_select + i;
  129. time_pb= s->pb_field_time - field_select + i;
  130. }else{
  131. time_pp= s->pp_field_time + field_select - i;
  132. time_pb= s->pb_field_time + field_select - i;
  133. }
  134. s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx;
  135. s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my;
  136. s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0]
  137. : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp;
  138. s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1]
  139. : s->p_field_mv_table[i][0][mb_index][1]*(time_pb - time_pp)/time_pp;
  140. }
  141. return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
  142. }else{
  143. ff_mpeg4_set_one_direct_mv(s, mx, my, 0);
  144. s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->mv[0][0][0];
  145. s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->mv[0][0][1];
  146. s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = s->mv[1][0][0];
  147. s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = s->mv[1][0][1];
  148. if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
  149. s->mv_type= MV_TYPE_16X16;
  150. else
  151. s->mv_type= MV_TYPE_8X8;
  152. return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line
  153. }
  154. }