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.

366 lines
11KB

  1. /*
  2. * H.263/MPEG-4 backend for encoder and decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * H.263+ 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. * H.263/MPEG-4 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 "flv.h"
  36. #include "mpeg4video.h"
  37. void ff_h263_update_motion_val(MpegEncContext * s){
  38. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  39. //FIXME a lot of that is only needed for !low_delay
  40. const int wrap = s->b8_stride;
  41. const int xy = s->block_index[0];
  42. s->current_picture.mbskip_table[mb_xy] = s->mb_skipped;
  43. if(s->mv_type != MV_TYPE_8X8){
  44. int motion_x, motion_y;
  45. if (s->mb_intra) {
  46. motion_x = 0;
  47. motion_y = 0;
  48. } else if (s->mv_type == MV_TYPE_16X16) {
  49. motion_x = s->mv[0][0][0];
  50. motion_y = s->mv[0][0][1];
  51. } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
  52. int i;
  53. motion_x = s->mv[0][0][0] + s->mv[0][1][0];
  54. motion_y = s->mv[0][0][1] + s->mv[0][1][1];
  55. motion_x = (motion_x>>1) | (motion_x&1);
  56. for(i=0; i<2; i++){
  57. s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
  58. s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
  59. }
  60. s->current_picture.ref_index[0][4*mb_xy ] =
  61. s->current_picture.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
  62. s->current_picture.ref_index[0][4*mb_xy + 2] =
  63. s->current_picture.ref_index[0][4*mb_xy + 3] = s->field_select[0][1];
  64. }
  65. /* no update if 8X8 because it has been done during parsing */
  66. s->current_picture.motion_val[0][xy][0] = motion_x;
  67. s->current_picture.motion_val[0][xy][1] = motion_y;
  68. s->current_picture.motion_val[0][xy + 1][0] = motion_x;
  69. s->current_picture.motion_val[0][xy + 1][1] = motion_y;
  70. s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
  71. s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
  72. s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
  73. s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
  74. }
  75. if(s->encoding){ //FIXME encoding MUST be cleaned up
  76. if (s->mv_type == MV_TYPE_8X8)
  77. s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_8x8;
  78. else if(s->mb_intra)
  79. s->current_picture.mb_type[mb_xy] = MB_TYPE_INTRA;
  80. else
  81. s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
  82. }
  83. }
  84. int ff_h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
  85. {
  86. int x, y, wrap, a, c, pred_dc;
  87. int16_t *dc_val;
  88. /* find prediction */
  89. if (n < 4) {
  90. x = 2 * s->mb_x + (n & 1);
  91. y = 2 * s->mb_y + ((n & 2) >> 1);
  92. wrap = s->b8_stride;
  93. dc_val = s->dc_val[0];
  94. } else {
  95. x = s->mb_x;
  96. y = s->mb_y;
  97. wrap = s->mb_stride;
  98. dc_val = s->dc_val[n - 4 + 1];
  99. }
  100. /* B C
  101. * A X
  102. */
  103. a = dc_val[(x - 1) + (y) * wrap];
  104. c = dc_val[(x) + (y - 1) * wrap];
  105. /* No prediction outside GOB boundary */
  106. if(s->first_slice_line && n!=3){
  107. if(n!=2) c= 1024;
  108. if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
  109. }
  110. /* just DC prediction */
  111. if (a != 1024 && c != 1024)
  112. pred_dc = (a + c) >> 1;
  113. else if (a != 1024)
  114. pred_dc = a;
  115. else
  116. pred_dc = c;
  117. /* we assume pred is positive */
  118. *dc_val_ptr = &dc_val[x + y * wrap];
  119. return pred_dc;
  120. }
  121. void ff_h263_loop_filter(MpegEncContext * s){
  122. int qp_c;
  123. const int linesize = s->linesize;
  124. const int uvlinesize= s->uvlinesize;
  125. const int xy = s->mb_y * s->mb_stride + s->mb_x;
  126. uint8_t *dest_y = s->dest[0];
  127. uint8_t *dest_cb= s->dest[1];
  128. uint8_t *dest_cr= s->dest[2];
  129. // if(s->pict_type==AV_PICTURE_TYPE_B && !s->readable) return;
  130. /*
  131. Diag Top
  132. Left Center
  133. */
  134. if (!IS_SKIP(s->current_picture.mb_type[xy])) {
  135. qp_c= s->qscale;
  136. s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize, linesize, qp_c);
  137. s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
  138. }else
  139. qp_c= 0;
  140. if(s->mb_y){
  141. int qp_dt, qp_tt, qp_tc;
  142. if (IS_SKIP(s->current_picture.mb_type[xy - s->mb_stride]))
  143. qp_tt=0;
  144. else
  145. qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
  146. if(qp_c)
  147. qp_tc= qp_c;
  148. else
  149. qp_tc= qp_tt;
  150. if(qp_tc){
  151. const int chroma_qp= s->chroma_qscale_table[qp_tc];
  152. s->h263dsp.h263_v_loop_filter(dest_y, linesize, qp_tc);
  153. s->h263dsp.h263_v_loop_filter(dest_y + 8, linesize, qp_tc);
  154. s->h263dsp.h263_v_loop_filter(dest_cb, uvlinesize, chroma_qp);
  155. s->h263dsp.h263_v_loop_filter(dest_cr, uvlinesize, chroma_qp);
  156. }
  157. if(qp_tt)
  158. s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize + 8, linesize, qp_tt);
  159. if(s->mb_x){
  160. if (qp_tt || IS_SKIP(s->current_picture.mb_type[xy - 1 - s->mb_stride]))
  161. qp_dt= qp_tt;
  162. else
  163. qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
  164. if(qp_dt){
  165. const int chroma_qp= s->chroma_qscale_table[qp_dt];
  166. s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize, linesize, qp_dt);
  167. s->h263dsp.h263_h_loop_filter(dest_cb - 8 * uvlinesize, uvlinesize, chroma_qp);
  168. s->h263dsp.h263_h_loop_filter(dest_cr - 8 * uvlinesize, uvlinesize, chroma_qp);
  169. }
  170. }
  171. }
  172. if(qp_c){
  173. s->h263dsp.h263_h_loop_filter(dest_y + 8, linesize, qp_c);
  174. if(s->mb_y + 1 == s->mb_height)
  175. s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
  176. }
  177. if(s->mb_x){
  178. int qp_lc;
  179. if (qp_c || IS_SKIP(s->current_picture.mb_type[xy - 1]))
  180. qp_lc= qp_c;
  181. else
  182. qp_lc = s->current_picture.qscale_table[xy - 1];
  183. if(qp_lc){
  184. s->h263dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
  185. if(s->mb_y + 1 == s->mb_height){
  186. const int chroma_qp= s->chroma_qscale_table[qp_lc];
  187. s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize, linesize, qp_lc);
  188. s->h263dsp.h263_h_loop_filter(dest_cb, uvlinesize, chroma_qp);
  189. s->h263dsp.h263_h_loop_filter(dest_cr, uvlinesize, chroma_qp);
  190. }
  191. }
  192. }
  193. }
  194. void ff_h263_pred_acdc(MpegEncContext * s, int16_t *block, int n)
  195. {
  196. int x, y, wrap, a, c, pred_dc, scale, i;
  197. int16_t *dc_val, *ac_val, *ac_val1;
  198. /* find prediction */
  199. if (n < 4) {
  200. x = 2 * s->mb_x + (n & 1);
  201. y = 2 * s->mb_y + (n>> 1);
  202. wrap = s->b8_stride;
  203. dc_val = s->dc_val[0];
  204. ac_val = s->ac_val[0][0];
  205. scale = s->y_dc_scale;
  206. } else {
  207. x = s->mb_x;
  208. y = s->mb_y;
  209. wrap = s->mb_stride;
  210. dc_val = s->dc_val[n - 4 + 1];
  211. ac_val = s->ac_val[n - 4 + 1][0];
  212. scale = s->c_dc_scale;
  213. }
  214. ac_val += ((y) * wrap + (x)) * 16;
  215. ac_val1 = ac_val;
  216. /* B C
  217. * A X
  218. */
  219. a = dc_val[(x - 1) + (y) * wrap];
  220. c = dc_val[(x) + (y - 1) * wrap];
  221. /* No prediction outside GOB boundary */
  222. if(s->first_slice_line && n!=3){
  223. if(n!=2) c= 1024;
  224. if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
  225. }
  226. if (s->ac_pred) {
  227. pred_dc = 1024;
  228. if (s->h263_aic_dir) {
  229. /* left prediction */
  230. if (a != 1024) {
  231. ac_val -= 16;
  232. for(i=1;i<8;i++) {
  233. block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
  234. }
  235. pred_dc = a;
  236. }
  237. } else {
  238. /* top prediction */
  239. if (c != 1024) {
  240. ac_val -= 16 * wrap;
  241. for(i=1;i<8;i++) {
  242. block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
  243. }
  244. pred_dc = c;
  245. }
  246. }
  247. } else {
  248. /* just DC prediction */
  249. if (a != 1024 && c != 1024)
  250. pred_dc = (a + c) >> 1;
  251. else if (a != 1024)
  252. pred_dc = a;
  253. else
  254. pred_dc = c;
  255. }
  256. /* we assume pred is positive */
  257. block[0]=block[0]*scale + pred_dc;
  258. if (block[0] < 0)
  259. block[0] = 0;
  260. else
  261. block[0] |= 1;
  262. /* Update AC/DC tables */
  263. dc_val[(x) + (y) * wrap] = block[0];
  264. /* left copy */
  265. for(i=1;i<8;i++)
  266. ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
  267. /* top copy */
  268. for(i=1;i<8;i++)
  269. ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
  270. }
  271. int16_t *ff_h263_pred_motion(MpegEncContext * s, int block, int dir,
  272. int *px, int *py)
  273. {
  274. int wrap;
  275. int16_t *A, *B, *C, (*mot_val)[2];
  276. static const int off[4]= {2, 1, 1, -1};
  277. wrap = s->b8_stride;
  278. mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
  279. A = mot_val[ - 1];
  280. /* special case for first (slice) line */
  281. if (s->first_slice_line && block<3) {
  282. // we can't just change some MVs to simulate that as we need them for the B-frames (and ME)
  283. // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
  284. if(block==0){ //most common case
  285. if(s->mb_x == s->resync_mb_x){ //rare
  286. *px= *py = 0;
  287. }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
  288. C = mot_val[off[block] - wrap];
  289. if(s->mb_x==0){
  290. *px = C[0];
  291. *py = C[1];
  292. }else{
  293. *px = mid_pred(A[0], 0, C[0]);
  294. *py = mid_pred(A[1], 0, C[1]);
  295. }
  296. }else{
  297. *px = A[0];
  298. *py = A[1];
  299. }
  300. }else if(block==1){
  301. if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
  302. C = mot_val[off[block] - wrap];
  303. *px = mid_pred(A[0], 0, C[0]);
  304. *py = mid_pred(A[1], 0, C[1]);
  305. }else{
  306. *px = A[0];
  307. *py = A[1];
  308. }
  309. }else{ /* block==2*/
  310. B = mot_val[ - wrap];
  311. C = mot_val[off[block] - wrap];
  312. if(s->mb_x == s->resync_mb_x) //rare
  313. A[0]=A[1]=0;
  314. *px = mid_pred(A[0], B[0], C[0]);
  315. *py = mid_pred(A[1], B[1], C[1]);
  316. }
  317. } else {
  318. B = mot_val[ - wrap];
  319. C = mot_val[off[block] - wrap];
  320. *px = mid_pred(A[0], B[0], C[0]);
  321. *py = mid_pred(A[1], B[1], C[1]);
  322. }
  323. return *mot_val;
  324. }