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.

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