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.

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