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.

370 lines
12KB

  1. /*
  2. * H263/MPEG4 backend for encoder and decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * H263+ support.
  5. * Copyright (c) 2001 Juan J. Sierralta P
  6. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * h263/mpeg4 codec.
  27. */
  28. #include <limits.h>
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h263.h"
  32. #include "h263data.h"
  33. #include "mathops.h"
  34. #include "mpegutils.h"
  35. #include "unary.h"
  36. #include "flv.h"
  37. #include "mpeg4video.h"
  38. uint8_t ff_h263_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  39. void ff_h263_update_motion_val(MpegEncContext * s){
  40. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  41. //FIXME a lot of that is only needed for !low_delay
  42. const int wrap = s->b8_stride;
  43. const int xy = s->block_index[0];
  44. s->current_picture.mbskip_table[mb_xy] = s->mb_skipped;
  45. if(s->mv_type != MV_TYPE_8X8){
  46. int motion_x, motion_y;
  47. if (s->mb_intra) {
  48. motion_x = 0;
  49. motion_y = 0;
  50. } else if (s->mv_type == MV_TYPE_16X16) {
  51. motion_x = s->mv[0][0][0];
  52. motion_y = s->mv[0][0][1];
  53. } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
  54. int i;
  55. motion_x = s->mv[0][0][0] + s->mv[0][1][0];
  56. motion_y = s->mv[0][0][1] + s->mv[0][1][1];
  57. motion_x = (motion_x>>1) | (motion_x&1);
  58. for(i=0; i<2; i++){
  59. s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
  60. s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
  61. }
  62. s->current_picture.ref_index[0][4*mb_xy ] =
  63. s->current_picture.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
  64. s->current_picture.ref_index[0][4*mb_xy + 2] =
  65. s->current_picture.ref_index[0][4*mb_xy + 3] = s->field_select[0][1];
  66. }
  67. /* no update if 8X8 because it has been done during parsing */
  68. s->current_picture.motion_val[0][xy][0] = motion_x;
  69. s->current_picture.motion_val[0][xy][1] = motion_y;
  70. s->current_picture.motion_val[0][xy + 1][0] = motion_x;
  71. s->current_picture.motion_val[0][xy + 1][1] = motion_y;
  72. s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
  73. s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
  74. s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
  75. s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
  76. }
  77. if(s->encoding){ //FIXME encoding MUST be cleaned up
  78. if (s->mv_type == MV_TYPE_8X8)
  79. s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_8x8;
  80. else if(s->mb_intra)
  81. s->current_picture.mb_type[mb_xy] = MB_TYPE_INTRA;
  82. else
  83. s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
  84. }
  85. }
  86. int ff_h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
  87. {
  88. int x, y, wrap, a, c, pred_dc;
  89. int16_t *dc_val;
  90. /* find prediction */
  91. if (n < 4) {
  92. x = 2 * s->mb_x + (n & 1);
  93. y = 2 * s->mb_y + ((n & 2) >> 1);
  94. wrap = s->b8_stride;
  95. dc_val = s->dc_val[0];
  96. } else {
  97. x = s->mb_x;
  98. y = s->mb_y;
  99. wrap = s->mb_stride;
  100. dc_val = s->dc_val[n - 4 + 1];
  101. }
  102. /* B C
  103. * A X
  104. */
  105. a = dc_val[(x - 1) + (y) * wrap];
  106. c = dc_val[(x) + (y - 1) * wrap];
  107. /* No prediction outside GOB boundary */
  108. if(s->first_slice_line && n!=3){
  109. if(n!=2) c= 1024;
  110. if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
  111. }
  112. /* just DC prediction */
  113. if (a != 1024 && c != 1024)
  114. pred_dc = (a + c) >> 1;
  115. else if (a != 1024)
  116. pred_dc = a;
  117. else
  118. pred_dc = c;
  119. /* we assume pred is positive */
  120. *dc_val_ptr = &dc_val[x + y * wrap];
  121. return pred_dc;
  122. }
  123. void ff_h263_loop_filter(MpegEncContext * s){
  124. int qp_c;
  125. const int linesize = s->linesize;
  126. const int uvlinesize= s->uvlinesize;
  127. const int xy = s->mb_y * s->mb_stride + s->mb_x;
  128. uint8_t *dest_y = s->dest[0];
  129. uint8_t *dest_cb= s->dest[1];
  130. uint8_t *dest_cr= s->dest[2];
  131. // if(s->pict_type==AV_PICTURE_TYPE_B && !s->readable) return;
  132. /*
  133. Diag Top
  134. Left Center
  135. */
  136. if (!IS_SKIP(s->current_picture.mb_type[xy])) {
  137. qp_c= s->qscale;
  138. s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize, linesize, qp_c);
  139. s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
  140. }else
  141. qp_c= 0;
  142. if(s->mb_y){
  143. int qp_dt, qp_tt, qp_tc;
  144. if (IS_SKIP(s->current_picture.mb_type[xy - s->mb_stride]))
  145. qp_tt=0;
  146. else
  147. qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
  148. if(qp_c)
  149. qp_tc= qp_c;
  150. else
  151. qp_tc= qp_tt;
  152. if(qp_tc){
  153. const int chroma_qp= s->chroma_qscale_table[qp_tc];
  154. s->h263dsp.h263_v_loop_filter(dest_y, linesize, qp_tc);
  155. s->h263dsp.h263_v_loop_filter(dest_y + 8, linesize, qp_tc);
  156. s->h263dsp.h263_v_loop_filter(dest_cb, uvlinesize, chroma_qp);
  157. s->h263dsp.h263_v_loop_filter(dest_cr, uvlinesize, chroma_qp);
  158. }
  159. if(qp_tt)
  160. s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize + 8, linesize, qp_tt);
  161. if(s->mb_x){
  162. if (qp_tt || IS_SKIP(s->current_picture.mb_type[xy - 1 - s->mb_stride]))
  163. qp_dt= qp_tt;
  164. else
  165. qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
  166. if(qp_dt){
  167. const int chroma_qp= s->chroma_qscale_table[qp_dt];
  168. s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize, linesize, qp_dt);
  169. s->h263dsp.h263_h_loop_filter(dest_cb - 8 * uvlinesize, uvlinesize, chroma_qp);
  170. s->h263dsp.h263_h_loop_filter(dest_cr - 8 * uvlinesize, uvlinesize, chroma_qp);
  171. }
  172. }
  173. }
  174. if(qp_c){
  175. s->h263dsp.h263_h_loop_filter(dest_y + 8, linesize, qp_c);
  176. if(s->mb_y + 1 == s->mb_height)
  177. s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
  178. }
  179. if(s->mb_x){
  180. int qp_lc;
  181. if (qp_c || IS_SKIP(s->current_picture.mb_type[xy - 1]))
  182. qp_lc= qp_c;
  183. else
  184. qp_lc = s->current_picture.qscale_table[xy - 1];
  185. if(qp_lc){
  186. s->h263dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
  187. if(s->mb_y + 1 == s->mb_height){
  188. const int chroma_qp= s->chroma_qscale_table[qp_lc];
  189. s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize, linesize, qp_lc);
  190. s->h263dsp.h263_h_loop_filter(dest_cb, uvlinesize, chroma_qp);
  191. s->h263dsp.h263_h_loop_filter(dest_cr, uvlinesize, chroma_qp);
  192. }
  193. }
  194. }
  195. }
  196. void ff_h263_pred_acdc(MpegEncContext * s, int16_t *block, int n)
  197. {
  198. int x, y, wrap, a, c, pred_dc, scale, i;
  199. int16_t *dc_val, *ac_val, *ac_val1;
  200. /* find prediction */
  201. if (n < 4) {
  202. x = 2 * s->mb_x + (n & 1);
  203. y = 2 * s->mb_y + (n>> 1);
  204. wrap = s->b8_stride;
  205. dc_val = s->dc_val[0];
  206. ac_val = s->ac_val[0][0];
  207. scale = s->y_dc_scale;
  208. } else {
  209. x = s->mb_x;
  210. y = s->mb_y;
  211. wrap = s->mb_stride;
  212. dc_val = s->dc_val[n - 4 + 1];
  213. ac_val = s->ac_val[n - 4 + 1][0];
  214. scale = s->c_dc_scale;
  215. }
  216. ac_val += ((y) * wrap + (x)) * 16;
  217. ac_val1 = ac_val;
  218. /* B C
  219. * A X
  220. */
  221. a = dc_val[(x - 1) + (y) * wrap];
  222. c = dc_val[(x) + (y - 1) * wrap];
  223. /* No prediction outside GOB boundary */
  224. if(s->first_slice_line && n!=3){
  225. if(n!=2) c= 1024;
  226. if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
  227. }
  228. if (s->ac_pred) {
  229. pred_dc = 1024;
  230. if (s->h263_aic_dir) {
  231. /* left prediction */
  232. if (a != 1024) {
  233. ac_val -= 16;
  234. for(i=1;i<8;i++) {
  235. block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
  236. }
  237. pred_dc = a;
  238. }
  239. } else {
  240. /* top prediction */
  241. if (c != 1024) {
  242. ac_val -= 16 * wrap;
  243. for(i=1;i<8;i++) {
  244. block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
  245. }
  246. pred_dc = c;
  247. }
  248. }
  249. } else {
  250. /* just DC prediction */
  251. if (a != 1024 && c != 1024)
  252. pred_dc = (a + c) >> 1;
  253. else if (a != 1024)
  254. pred_dc = a;
  255. else
  256. pred_dc = c;
  257. }
  258. /* we assume pred is positive */
  259. block[0]=block[0]*scale + pred_dc;
  260. if (block[0] < 0)
  261. block[0] = 0;
  262. else
  263. block[0] |= 1;
  264. /* Update AC/DC tables */
  265. dc_val[(x) + (y) * wrap] = block[0];
  266. /* left copy */
  267. for(i=1;i<8;i++)
  268. ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
  269. /* top copy */
  270. for(i=1;i<8;i++)
  271. ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
  272. }
  273. int16_t *ff_h263_pred_motion(MpegEncContext * s, int block, int dir,
  274. int *px, int *py)
  275. {
  276. int wrap;
  277. int16_t *A, *B, *C, (*mot_val)[2];
  278. static const int off[4]= {2, 1, 1, -1};
  279. wrap = s->b8_stride;
  280. mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
  281. A = mot_val[ - 1];
  282. /* special case for first (slice) line */
  283. if (s->first_slice_line && block<3) {
  284. // we can't just change some MVs to simulate that as we need them for the B frames (and ME)
  285. // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
  286. if(block==0){ //most common case
  287. if(s->mb_x == s->resync_mb_x){ //rare
  288. *px= *py = 0;
  289. }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
  290. C = mot_val[off[block] - wrap];
  291. if(s->mb_x==0){
  292. *px = C[0];
  293. *py = C[1];
  294. }else{
  295. *px = mid_pred(A[0], 0, C[0]);
  296. *py = mid_pred(A[1], 0, C[1]);
  297. }
  298. }else{
  299. *px = A[0];
  300. *py = A[1];
  301. }
  302. }else if(block==1){
  303. if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
  304. C = mot_val[off[block] - wrap];
  305. *px = mid_pred(A[0], 0, C[0]);
  306. *py = mid_pred(A[1], 0, C[1]);
  307. }else{
  308. *px = A[0];
  309. *py = A[1];
  310. }
  311. }else{ /* block==2*/
  312. B = mot_val[ - wrap];
  313. C = mot_val[off[block] - wrap];
  314. if(s->mb_x == s->resync_mb_x) //rare
  315. A[0]=A[1]=0;
  316. *px = mid_pred(A[0], B[0], C[0]);
  317. *py = mid_pred(A[1], B[1], C[1]);
  318. }
  319. } else {
  320. B = mot_val[ - wrap];
  321. C = mot_val[off[block] - wrap];
  322. *px = mid_pred(A[0], B[0], C[0]);
  323. *py = mid_pred(A[1], B[1], C[1]);
  324. }
  325. return *mot_val;
  326. }