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.

367 lines
12KB

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