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.

8312 lines
315KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file h264.c
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h264.h"
  30. #include "h264data.h"
  31. #include "h264_parser.h"
  32. #include "golomb.h"
  33. #include "cabac.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. static VLC coeff_token_vlc[4];
  37. static VLC chroma_dc_coeff_token_vlc;
  38. static VLC total_zeros_vlc[15];
  39. static VLC chroma_dc_total_zeros_vlc[3];
  40. static VLC run_vlc[6];
  41. static VLC run7_vlc;
  42. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  43. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  44. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  45. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  46. static av_always_inline uint32_t pack16to32(int a, int b){
  47. #ifdef WORDS_BIGENDIAN
  48. return (b&0xFFFF) + (a<<16);
  49. #else
  50. return (a&0xFFFF) + (b<<16);
  51. #endif
  52. }
  53. const uint8_t ff_rem6[52]={
  54. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  55. };
  56. const uint8_t ff_div6[52]={
  57. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
  58. };
  59. /**
  60. * fill a rectangle.
  61. * @param h height of the rectangle, should be a constant
  62. * @param w width of the rectangle, should be a constant
  63. * @param size the size of val (1 or 4), should be a constant
  64. */
  65. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  66. uint8_t *p= (uint8_t*)vp;
  67. assert(size==1 || size==4);
  68. assert(w<=4);
  69. w *= size;
  70. stride *= size;
  71. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  72. assert((stride&(w-1))==0);
  73. if(w==2){
  74. const uint16_t v= size==4 ? val : val*0x0101;
  75. *(uint16_t*)(p + 0*stride)= v;
  76. if(h==1) return;
  77. *(uint16_t*)(p + 1*stride)= v;
  78. if(h==2) return;
  79. *(uint16_t*)(p + 2*stride)=
  80. *(uint16_t*)(p + 3*stride)= v;
  81. }else if(w==4){
  82. const uint32_t v= size==4 ? val : val*0x01010101;
  83. *(uint32_t*)(p + 0*stride)= v;
  84. if(h==1) return;
  85. *(uint32_t*)(p + 1*stride)= v;
  86. if(h==2) return;
  87. *(uint32_t*)(p + 2*stride)=
  88. *(uint32_t*)(p + 3*stride)= v;
  89. }else if(w==8){
  90. //gcc can't optimize 64bit math on x86_32
  91. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  92. const uint64_t v= val*0x0100000001ULL;
  93. *(uint64_t*)(p + 0*stride)= v;
  94. if(h==1) return;
  95. *(uint64_t*)(p + 1*stride)= v;
  96. if(h==2) return;
  97. *(uint64_t*)(p + 2*stride)=
  98. *(uint64_t*)(p + 3*stride)= v;
  99. }else if(w==16){
  100. const uint64_t v= val*0x0100000001ULL;
  101. *(uint64_t*)(p + 0+0*stride)=
  102. *(uint64_t*)(p + 8+0*stride)=
  103. *(uint64_t*)(p + 0+1*stride)=
  104. *(uint64_t*)(p + 8+1*stride)= v;
  105. if(h==2) return;
  106. *(uint64_t*)(p + 0+2*stride)=
  107. *(uint64_t*)(p + 8+2*stride)=
  108. *(uint64_t*)(p + 0+3*stride)=
  109. *(uint64_t*)(p + 8+3*stride)= v;
  110. #else
  111. *(uint32_t*)(p + 0+0*stride)=
  112. *(uint32_t*)(p + 4+0*stride)= val;
  113. if(h==1) return;
  114. *(uint32_t*)(p + 0+1*stride)=
  115. *(uint32_t*)(p + 4+1*stride)= val;
  116. if(h==2) return;
  117. *(uint32_t*)(p + 0+2*stride)=
  118. *(uint32_t*)(p + 4+2*stride)=
  119. *(uint32_t*)(p + 0+3*stride)=
  120. *(uint32_t*)(p + 4+3*stride)= val;
  121. }else if(w==16){
  122. *(uint32_t*)(p + 0+0*stride)=
  123. *(uint32_t*)(p + 4+0*stride)=
  124. *(uint32_t*)(p + 8+0*stride)=
  125. *(uint32_t*)(p +12+0*stride)=
  126. *(uint32_t*)(p + 0+1*stride)=
  127. *(uint32_t*)(p + 4+1*stride)=
  128. *(uint32_t*)(p + 8+1*stride)=
  129. *(uint32_t*)(p +12+1*stride)= val;
  130. if(h==2) return;
  131. *(uint32_t*)(p + 0+2*stride)=
  132. *(uint32_t*)(p + 4+2*stride)=
  133. *(uint32_t*)(p + 8+2*stride)=
  134. *(uint32_t*)(p +12+2*stride)=
  135. *(uint32_t*)(p + 0+3*stride)=
  136. *(uint32_t*)(p + 4+3*stride)=
  137. *(uint32_t*)(p + 8+3*stride)=
  138. *(uint32_t*)(p +12+3*stride)= val;
  139. #endif
  140. }else
  141. assert(0);
  142. assert(h==4);
  143. }
  144. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  145. MpegEncContext * const s = &h->s;
  146. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  147. int topleft_xy, top_xy, topright_xy, left_xy[2];
  148. int topleft_type, top_type, topright_type, left_type[2];
  149. int left_block[8];
  150. int i;
  151. //FIXME deblocking could skip the intra and nnz parts.
  152. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  153. return;
  154. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  155. top_xy = mb_xy - s->mb_stride;
  156. topleft_xy = top_xy - 1;
  157. topright_xy= top_xy + 1;
  158. left_xy[1] = left_xy[0] = mb_xy-1;
  159. left_block[0]= 0;
  160. left_block[1]= 1;
  161. left_block[2]= 2;
  162. left_block[3]= 3;
  163. left_block[4]= 7;
  164. left_block[5]= 10;
  165. left_block[6]= 8;
  166. left_block[7]= 11;
  167. if(FRAME_MBAFF){
  168. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  169. const int top_pair_xy = pair_xy - s->mb_stride;
  170. const int topleft_pair_xy = top_pair_xy - 1;
  171. const int topright_pair_xy = top_pair_xy + 1;
  172. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  173. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  174. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  175. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  176. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  177. const int bottom = (s->mb_y & 1);
  178. tprintf(s->avctx, "fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
  179. if (bottom
  180. ? !curr_mb_frame_flag // bottom macroblock
  181. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  182. ) {
  183. top_xy -= s->mb_stride;
  184. }
  185. if (bottom
  186. ? !curr_mb_frame_flag // bottom macroblock
  187. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  188. ) {
  189. topleft_xy -= s->mb_stride;
  190. }
  191. if (bottom
  192. ? !curr_mb_frame_flag // bottom macroblock
  193. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  194. ) {
  195. topright_xy -= s->mb_stride;
  196. }
  197. if (left_mb_frame_flag != curr_mb_frame_flag) {
  198. left_xy[1] = left_xy[0] = pair_xy - 1;
  199. if (curr_mb_frame_flag) {
  200. if (bottom) {
  201. left_block[0]= 2;
  202. left_block[1]= 2;
  203. left_block[2]= 3;
  204. left_block[3]= 3;
  205. left_block[4]= 8;
  206. left_block[5]= 11;
  207. left_block[6]= 8;
  208. left_block[7]= 11;
  209. } else {
  210. left_block[0]= 0;
  211. left_block[1]= 0;
  212. left_block[2]= 1;
  213. left_block[3]= 1;
  214. left_block[4]= 7;
  215. left_block[5]= 10;
  216. left_block[6]= 7;
  217. left_block[7]= 10;
  218. }
  219. } else {
  220. left_xy[1] += s->mb_stride;
  221. //left_block[0]= 0;
  222. left_block[1]= 2;
  223. left_block[2]= 0;
  224. left_block[3]= 2;
  225. //left_block[4]= 7;
  226. left_block[5]= 10;
  227. left_block[6]= 7;
  228. left_block[7]= 10;
  229. }
  230. }
  231. }
  232. h->top_mb_xy = top_xy;
  233. h->left_mb_xy[0] = left_xy[0];
  234. h->left_mb_xy[1] = left_xy[1];
  235. if(for_deblock){
  236. topleft_type = 0;
  237. topright_type = 0;
  238. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  239. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  240. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  241. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  242. int list;
  243. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  244. for(i=0; i<16; i++)
  245. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  246. for(list=0; list<h->list_count; list++){
  247. if(USES_LIST(mb_type,list)){
  248. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  249. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  250. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  251. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  252. dst[0] = src[0];
  253. dst[1] = src[1];
  254. dst[2] = src[2];
  255. dst[3] = src[3];
  256. }
  257. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  258. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  259. ref += h->b8_stride;
  260. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  261. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  262. }else{
  263. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  264. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  265. }
  266. }
  267. }
  268. }else{
  269. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  270. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  271. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  272. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  273. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  274. }
  275. if(IS_INTRA(mb_type)){
  276. h->topleft_samples_available=
  277. h->top_samples_available=
  278. h->left_samples_available= 0xFFFF;
  279. h->topright_samples_available= 0xEEEA;
  280. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  281. h->topleft_samples_available= 0xB3FF;
  282. h->top_samples_available= 0x33FF;
  283. h->topright_samples_available= 0x26EA;
  284. }
  285. for(i=0; i<2; i++){
  286. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  287. h->topleft_samples_available&= 0xDF5F;
  288. h->left_samples_available&= 0x5F5F;
  289. }
  290. }
  291. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  292. h->topleft_samples_available&= 0x7FFF;
  293. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  294. h->topright_samples_available&= 0xFBFF;
  295. if(IS_INTRA4x4(mb_type)){
  296. if(IS_INTRA4x4(top_type)){
  297. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  298. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  299. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  300. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  301. }else{
  302. int pred;
  303. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  304. pred= -1;
  305. else{
  306. pred= 2;
  307. }
  308. h->intra4x4_pred_mode_cache[4+8*0]=
  309. h->intra4x4_pred_mode_cache[5+8*0]=
  310. h->intra4x4_pred_mode_cache[6+8*0]=
  311. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  312. }
  313. for(i=0; i<2; i++){
  314. if(IS_INTRA4x4(left_type[i])){
  315. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  316. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  317. }else{
  318. int pred;
  319. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  320. pred= -1;
  321. else{
  322. pred= 2;
  323. }
  324. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  325. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  326. }
  327. }
  328. }
  329. }
  330. /*
  331. 0 . T T. T T T T
  332. 1 L . .L . . . .
  333. 2 L . .L . . . .
  334. 3 . T TL . . . .
  335. 4 L . .L . . . .
  336. 5 L . .. . . . .
  337. */
  338. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  339. if(top_type){
  340. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  341. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  342. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  343. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  344. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  345. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  346. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  347. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  348. }else{
  349. h->non_zero_count_cache[4+8*0]=
  350. h->non_zero_count_cache[5+8*0]=
  351. h->non_zero_count_cache[6+8*0]=
  352. h->non_zero_count_cache[7+8*0]=
  353. h->non_zero_count_cache[1+8*0]=
  354. h->non_zero_count_cache[2+8*0]=
  355. h->non_zero_count_cache[1+8*3]=
  356. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  357. }
  358. for (i=0; i<2; i++) {
  359. if(left_type[i]){
  360. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  361. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  362. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  363. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  364. }else{
  365. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  366. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  367. h->non_zero_count_cache[0+8*1 + 8*i]=
  368. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  369. }
  370. }
  371. if( h->pps.cabac ) {
  372. // top_cbp
  373. if(top_type) {
  374. h->top_cbp = h->cbp_table[top_xy];
  375. } else if(IS_INTRA(mb_type)) {
  376. h->top_cbp = 0x1C0;
  377. } else {
  378. h->top_cbp = 0;
  379. }
  380. // left_cbp
  381. if (left_type[0]) {
  382. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  383. } else if(IS_INTRA(mb_type)) {
  384. h->left_cbp = 0x1C0;
  385. } else {
  386. h->left_cbp = 0;
  387. }
  388. if (left_type[0]) {
  389. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  390. }
  391. if (left_type[1]) {
  392. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  393. }
  394. }
  395. #if 1
  396. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  397. int list;
  398. for(list=0; list<h->list_count; list++){
  399. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  400. /*if(!h->mv_cache_clean[list]){
  401. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  402. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  403. h->mv_cache_clean[list]= 1;
  404. }*/
  405. continue;
  406. }
  407. h->mv_cache_clean[list]= 0;
  408. if(USES_LIST(top_type, list)){
  409. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  410. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  411. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  412. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  413. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  414. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  415. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  416. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  417. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  418. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  419. }else{
  420. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  421. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  422. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  423. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  424. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  425. }
  426. for(i=0; i<2; i++){
  427. int cache_idx = scan8[0] - 1 + i*2*8;
  428. if(USES_LIST(left_type[i], list)){
  429. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  430. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  431. *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
  432. *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
  433. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  434. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  435. }else{
  436. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  437. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  438. h->ref_cache[list][cache_idx ]=
  439. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  440. }
  441. }
  442. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  443. continue;
  444. if(USES_LIST(topleft_type, list)){
  445. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  446. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  447. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  448. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  449. }else{
  450. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  451. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  452. }
  453. if(USES_LIST(topright_type, list)){
  454. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  455. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  456. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  457. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  458. }else{
  459. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  460. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  461. }
  462. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  463. continue;
  464. h->ref_cache[list][scan8[5 ]+1] =
  465. h->ref_cache[list][scan8[7 ]+1] =
  466. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  467. h->ref_cache[list][scan8[4 ]] =
  468. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  469. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  470. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  471. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  472. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  473. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  474. if( h->pps.cabac ) {
  475. /* XXX beurk, Load mvd */
  476. if(USES_LIST(top_type, list)){
  477. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  478. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  479. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  480. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  481. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  482. }else{
  483. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  484. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  485. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  486. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  487. }
  488. if(USES_LIST(left_type[0], list)){
  489. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  490. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
  491. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
  492. }else{
  493. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  494. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  495. }
  496. if(USES_LIST(left_type[1], list)){
  497. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  498. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
  499. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
  500. }else{
  501. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  502. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  503. }
  504. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  505. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  506. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  507. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  508. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  509. if(h->slice_type == B_TYPE){
  510. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  511. if(IS_DIRECT(top_type)){
  512. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  513. }else if(IS_8X8(top_type)){
  514. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  515. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  516. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  517. }else{
  518. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  519. }
  520. if(IS_DIRECT(left_type[0]))
  521. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  522. else if(IS_8X8(left_type[0]))
  523. h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
  524. else
  525. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  526. if(IS_DIRECT(left_type[1]))
  527. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  528. else if(IS_8X8(left_type[1]))
  529. h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
  530. else
  531. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  532. }
  533. }
  534. if(FRAME_MBAFF){
  535. #define MAP_MVS\
  536. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  537. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  538. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  539. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  540. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  541. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  542. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  543. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  544. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  545. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  546. if(MB_FIELD){
  547. #define MAP_F2F(idx, mb_type)\
  548. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  549. h->ref_cache[list][idx] <<= 1;\
  550. h->mv_cache[list][idx][1] /= 2;\
  551. h->mvd_cache[list][idx][1] /= 2;\
  552. }
  553. MAP_MVS
  554. #undef MAP_F2F
  555. }else{
  556. #define MAP_F2F(idx, mb_type)\
  557. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  558. h->ref_cache[list][idx] >>= 1;\
  559. h->mv_cache[list][idx][1] <<= 1;\
  560. h->mvd_cache[list][idx][1] <<= 1;\
  561. }
  562. MAP_MVS
  563. #undef MAP_F2F
  564. }
  565. }
  566. }
  567. }
  568. #endif
  569. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  570. }
  571. static inline void write_back_intra_pred_mode(H264Context *h){
  572. MpegEncContext * const s = &h->s;
  573. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  574. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  575. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  576. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  577. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  578. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  579. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  580. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  581. }
  582. /**
  583. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  584. */
  585. static inline int check_intra4x4_pred_mode(H264Context *h){
  586. MpegEncContext * const s = &h->s;
  587. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  588. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  589. int i;
  590. if(!(h->top_samples_available&0x8000)){
  591. for(i=0; i<4; i++){
  592. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  593. if(status<0){
  594. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  595. return -1;
  596. } else if(status){
  597. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  598. }
  599. }
  600. }
  601. if(!(h->left_samples_available&0x8000)){
  602. for(i=0; i<4; i++){
  603. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  604. if(status<0){
  605. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  606. return -1;
  607. } else if(status){
  608. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  609. }
  610. }
  611. }
  612. return 0;
  613. } //FIXME cleanup like next
  614. /**
  615. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  616. */
  617. static inline int check_intra_pred_mode(H264Context *h, int mode){
  618. MpegEncContext * const s = &h->s;
  619. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  620. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  621. if(mode > 6U) {
  622. av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
  623. return -1;
  624. }
  625. if(!(h->top_samples_available&0x8000)){
  626. mode= top[ mode ];
  627. if(mode<0){
  628. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  629. return -1;
  630. }
  631. }
  632. if(!(h->left_samples_available&0x8000)){
  633. mode= left[ mode ];
  634. if(mode<0){
  635. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  636. return -1;
  637. }
  638. }
  639. return mode;
  640. }
  641. /**
  642. * gets the predicted intra4x4 prediction mode.
  643. */
  644. static inline int pred_intra_mode(H264Context *h, int n){
  645. const int index8= scan8[n];
  646. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  647. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  648. const int min= FFMIN(left, top);
  649. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  650. if(min<0) return DC_PRED;
  651. else return min;
  652. }
  653. static inline void write_back_non_zero_count(H264Context *h){
  654. MpegEncContext * const s = &h->s;
  655. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  656. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  657. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  658. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  659. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  660. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  661. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  662. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  663. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  664. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  665. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  666. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  667. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  668. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  669. if(FRAME_MBAFF){
  670. // store all luma nnzs, for deblocking
  671. int v = 0, i;
  672. for(i=0; i<16; i++)
  673. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  674. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  675. }
  676. }
  677. /**
  678. * gets the predicted number of non zero coefficients.
  679. * @param n block index
  680. */
  681. static inline int pred_non_zero_count(H264Context *h, int n){
  682. const int index8= scan8[n];
  683. const int left= h->non_zero_count_cache[index8 - 1];
  684. const int top = h->non_zero_count_cache[index8 - 8];
  685. int i= left + top;
  686. if(i<64) i= (i+1)>>1;
  687. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  688. return i&31;
  689. }
  690. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  691. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  692. MpegEncContext *s = &h->s;
  693. /* there is no consistent mapping of mvs to neighboring locations that will
  694. * make mbaff happy, so we can't move all this logic to fill_caches */
  695. if(FRAME_MBAFF){
  696. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  697. const int16_t *mv;
  698. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  699. *C = h->mv_cache[list][scan8[0]-2];
  700. if(!MB_FIELD
  701. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  702. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  703. if(IS_INTERLACED(mb_types[topright_xy])){
  704. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  705. const int x4 = X4, y4 = Y4;\
  706. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  707. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  708. return LIST_NOT_USED;\
  709. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  710. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  711. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  712. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  713. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  714. }
  715. }
  716. if(topright_ref == PART_NOT_AVAILABLE
  717. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  718. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  719. if(!MB_FIELD
  720. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  721. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  722. }
  723. if(MB_FIELD
  724. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  725. && i >= scan8[0]+8){
  726. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  727. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  728. }
  729. }
  730. #undef SET_DIAG_MV
  731. }
  732. if(topright_ref != PART_NOT_AVAILABLE){
  733. *C= h->mv_cache[list][ i - 8 + part_width ];
  734. return topright_ref;
  735. }else{
  736. tprintf(s->avctx, "topright MV not available\n");
  737. *C= h->mv_cache[list][ i - 8 - 1 ];
  738. return h->ref_cache[list][ i - 8 - 1 ];
  739. }
  740. }
  741. /**
  742. * gets the predicted MV.
  743. * @param n the block index
  744. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  745. * @param mx the x component of the predicted motion vector
  746. * @param my the y component of the predicted motion vector
  747. */
  748. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  749. const int index8= scan8[n];
  750. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  751. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  752. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  753. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  754. const int16_t * C;
  755. int diagonal_ref, match_count;
  756. assert(part_width==1 || part_width==2 || part_width==4);
  757. /* mv_cache
  758. B . . A T T T T
  759. U . . L . . , .
  760. U . . L . . . .
  761. U . . L . . , .
  762. . . . L . . . .
  763. */
  764. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  765. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  766. tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
  767. if(match_count > 1){ //most common
  768. *mx= mid_pred(A[0], B[0], C[0]);
  769. *my= mid_pred(A[1], B[1], C[1]);
  770. }else if(match_count==1){
  771. if(left_ref==ref){
  772. *mx= A[0];
  773. *my= A[1];
  774. }else if(top_ref==ref){
  775. *mx= B[0];
  776. *my= B[1];
  777. }else{
  778. *mx= C[0];
  779. *my= C[1];
  780. }
  781. }else{
  782. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  783. *mx= A[0];
  784. *my= A[1];
  785. }else{
  786. *mx= mid_pred(A[0], B[0], C[0]);
  787. *my= mid_pred(A[1], B[1], C[1]);
  788. }
  789. }
  790. tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
  791. }
  792. /**
  793. * gets the directionally predicted 16x8 MV.
  794. * @param n the block index
  795. * @param mx the x component of the predicted motion vector
  796. * @param my the y component of the predicted motion vector
  797. */
  798. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  799. if(n==0){
  800. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  801. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  802. tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
  803. if(top_ref == ref){
  804. *mx= B[0];
  805. *my= B[1];
  806. return;
  807. }
  808. }else{
  809. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  810. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  811. tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  812. if(left_ref == ref){
  813. *mx= A[0];
  814. *my= A[1];
  815. return;
  816. }
  817. }
  818. //RARE
  819. pred_motion(h, n, 4, list, ref, mx, my);
  820. }
  821. /**
  822. * gets the directionally predicted 8x16 MV.
  823. * @param n the block index
  824. * @param mx the x component of the predicted motion vector
  825. * @param my the y component of the predicted motion vector
  826. */
  827. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  828. if(n==0){
  829. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  830. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  831. tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  832. if(left_ref == ref){
  833. *mx= A[0];
  834. *my= A[1];
  835. return;
  836. }
  837. }else{
  838. const int16_t * C;
  839. int diagonal_ref;
  840. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  841. tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
  842. if(diagonal_ref == ref){
  843. *mx= C[0];
  844. *my= C[1];
  845. return;
  846. }
  847. }
  848. //RARE
  849. pred_motion(h, n, 2, list, ref, mx, my);
  850. }
  851. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  852. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  853. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  854. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  855. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  856. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  857. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  858. *mx = *my = 0;
  859. return;
  860. }
  861. pred_motion(h, 0, 4, 0, 0, mx, my);
  862. return;
  863. }
  864. static inline void direct_dist_scale_factor(H264Context * const h){
  865. const int poc = h->s.current_picture_ptr->poc;
  866. const int poc1 = h->ref_list[1][0].poc;
  867. int i;
  868. for(i=0; i<h->ref_count[0]; i++){
  869. int poc0 = h->ref_list[0][i].poc;
  870. int td = av_clip(poc1 - poc0, -128, 127);
  871. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  872. h->dist_scale_factor[i] = 256;
  873. }else{
  874. int tb = av_clip(poc - poc0, -128, 127);
  875. int tx = (16384 + (FFABS(td) >> 1)) / td;
  876. h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
  877. }
  878. }
  879. if(FRAME_MBAFF){
  880. for(i=0; i<h->ref_count[0]; i++){
  881. h->dist_scale_factor_field[2*i] =
  882. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  883. }
  884. }
  885. }
  886. static inline void direct_ref_list_init(H264Context * const h){
  887. MpegEncContext * const s = &h->s;
  888. Picture * const ref1 = &h->ref_list[1][0];
  889. Picture * const cur = s->current_picture_ptr;
  890. int list, i, j;
  891. if(cur->pict_type == I_TYPE)
  892. cur->ref_count[0] = 0;
  893. if(cur->pict_type != B_TYPE)
  894. cur->ref_count[1] = 0;
  895. for(list=0; list<2; list++){
  896. cur->ref_count[list] = h->ref_count[list];
  897. for(j=0; j<h->ref_count[list]; j++)
  898. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  899. }
  900. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  901. return;
  902. for(list=0; list<2; list++){
  903. for(i=0; i<ref1->ref_count[list]; i++){
  904. const int poc = ref1->ref_poc[list][i];
  905. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  906. for(j=0; j<h->ref_count[list]; j++)
  907. if(h->ref_list[list][j].poc == poc){
  908. h->map_col_to_list0[list][i] = j;
  909. break;
  910. }
  911. }
  912. }
  913. if(FRAME_MBAFF){
  914. for(list=0; list<2; list++){
  915. for(i=0; i<ref1->ref_count[list]; i++){
  916. j = h->map_col_to_list0[list][i];
  917. h->map_col_to_list0_field[list][2*i] = 2*j;
  918. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  919. }
  920. }
  921. }
  922. }
  923. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  924. MpegEncContext * const s = &h->s;
  925. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  926. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  927. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  928. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  929. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  930. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  931. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  932. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  933. const int is_b8x8 = IS_8X8(*mb_type);
  934. unsigned int sub_mb_type;
  935. int i8, i4;
  936. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  937. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  938. /* FIXME save sub mb types from previous frames (or derive from MVs)
  939. * so we know exactly what block size to use */
  940. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  941. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  942. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  943. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  944. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  945. }else{
  946. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  947. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  948. }
  949. if(!is_b8x8)
  950. *mb_type |= MB_TYPE_DIRECT2;
  951. if(MB_FIELD)
  952. *mb_type |= MB_TYPE_INTERLACED;
  953. tprintf(s->avctx, "mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
  954. if(h->direct_spatial_mv_pred){
  955. int ref[2];
  956. int mv[2][2];
  957. int list;
  958. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  959. /* ref = min(neighbors) */
  960. for(list=0; list<2; list++){
  961. int refa = h->ref_cache[list][scan8[0] - 1];
  962. int refb = h->ref_cache[list][scan8[0] - 8];
  963. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  964. if(refc == -2)
  965. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  966. ref[list] = refa;
  967. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  968. ref[list] = refb;
  969. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  970. ref[list] = refc;
  971. if(ref[list] < 0)
  972. ref[list] = -1;
  973. }
  974. if(ref[0] < 0 && ref[1] < 0){
  975. ref[0] = ref[1] = 0;
  976. mv[0][0] = mv[0][1] =
  977. mv[1][0] = mv[1][1] = 0;
  978. }else{
  979. for(list=0; list<2; list++){
  980. if(ref[list] >= 0)
  981. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  982. else
  983. mv[list][0] = mv[list][1] = 0;
  984. }
  985. }
  986. if(ref[1] < 0){
  987. *mb_type &= ~MB_TYPE_P0L1;
  988. sub_mb_type &= ~MB_TYPE_P0L1;
  989. }else if(ref[0] < 0){
  990. *mb_type &= ~MB_TYPE_P0L0;
  991. sub_mb_type &= ~MB_TYPE_P0L0;
  992. }
  993. if(IS_16X16(*mb_type)){
  994. int a=0, b=0;
  995. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  996. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  997. if(!IS_INTRA(mb_type_col)
  998. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  999. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1000. && (h->x264_build>33 || !h->x264_build)))){
  1001. if(ref[0] > 0)
  1002. a= pack16to32(mv[0][0],mv[0][1]);
  1003. if(ref[1] > 0)
  1004. b= pack16to32(mv[1][0],mv[1][1]);
  1005. }else{
  1006. a= pack16to32(mv[0][0],mv[0][1]);
  1007. b= pack16to32(mv[1][0],mv[1][1]);
  1008. }
  1009. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  1010. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  1011. }else{
  1012. for(i8=0; i8<4; i8++){
  1013. const int x8 = i8&1;
  1014. const int y8 = i8>>1;
  1015. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1016. continue;
  1017. h->sub_mb_type[i8] = sub_mb_type;
  1018. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1019. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1020. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1021. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1022. /* col_zero_flag */
  1023. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1024. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1025. && (h->x264_build>33 || !h->x264_build)))){
  1026. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1027. if(IS_SUB_8X8(sub_mb_type)){
  1028. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1029. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1030. if(ref[0] == 0)
  1031. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1032. if(ref[1] == 0)
  1033. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1034. }
  1035. }else
  1036. for(i4=0; i4<4; i4++){
  1037. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1038. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1039. if(ref[0] == 0)
  1040. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1041. if(ref[1] == 0)
  1042. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }else{ /* direct temporal mv pred */
  1049. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1050. const int *dist_scale_factor = h->dist_scale_factor;
  1051. if(FRAME_MBAFF){
  1052. if(IS_INTERLACED(*mb_type)){
  1053. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1054. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1055. dist_scale_factor = h->dist_scale_factor_field;
  1056. }
  1057. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1058. /* FIXME assumes direct_8x8_inference == 1 */
  1059. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1060. int mb_types_col[2];
  1061. int y_shift;
  1062. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1063. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1064. | (*mb_type & MB_TYPE_INTERLACED);
  1065. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1066. if(IS_INTERLACED(*mb_type)){
  1067. /* frame to field scaling */
  1068. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1069. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1070. if(s->mb_y&1){
  1071. l1ref0 -= 2*h->b8_stride;
  1072. l1ref1 -= 2*h->b8_stride;
  1073. l1mv0 -= 4*h->b_stride;
  1074. l1mv1 -= 4*h->b_stride;
  1075. }
  1076. y_shift = 0;
  1077. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1078. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1079. && !is_b8x8)
  1080. *mb_type |= MB_TYPE_16x8;
  1081. else
  1082. *mb_type |= MB_TYPE_8x8;
  1083. }else{
  1084. /* field to frame scaling */
  1085. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1086. * but in MBAFF, top and bottom POC are equal */
  1087. int dy = (s->mb_y&1) ? 1 : 2;
  1088. mb_types_col[0] =
  1089. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1090. l1ref0 += dy*h->b8_stride;
  1091. l1ref1 += dy*h->b8_stride;
  1092. l1mv0 += 2*dy*h->b_stride;
  1093. l1mv1 += 2*dy*h->b_stride;
  1094. y_shift = 2;
  1095. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1096. && !is_b8x8)
  1097. *mb_type |= MB_TYPE_16x16;
  1098. else
  1099. *mb_type |= MB_TYPE_8x8;
  1100. }
  1101. for(i8=0; i8<4; i8++){
  1102. const int x8 = i8&1;
  1103. const int y8 = i8>>1;
  1104. int ref0, scale;
  1105. const int16_t (*l1mv)[2]= l1mv0;
  1106. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1107. continue;
  1108. h->sub_mb_type[i8] = sub_mb_type;
  1109. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1110. if(IS_INTRA(mb_types_col[y8])){
  1111. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1112. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1113. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1114. continue;
  1115. }
  1116. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1117. if(ref0 >= 0)
  1118. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1119. else{
  1120. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1121. l1mv= l1mv1;
  1122. }
  1123. scale = dist_scale_factor[ref0];
  1124. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1125. {
  1126. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1127. int my_col = (mv_col[1]<<y_shift)/2;
  1128. int mx = (scale * mv_col[0] + 128) >> 8;
  1129. int my = (scale * my_col + 128) >> 8;
  1130. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1131. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1132. }
  1133. }
  1134. return;
  1135. }
  1136. }
  1137. /* one-to-one mv scaling */
  1138. if(IS_16X16(*mb_type)){
  1139. int ref, mv0, mv1;
  1140. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1141. if(IS_INTRA(mb_type_col)){
  1142. ref=mv0=mv1=0;
  1143. }else{
  1144. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1145. : map_col_to_list0[1][l1ref1[0]];
  1146. const int scale = dist_scale_factor[ref0];
  1147. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1148. int mv_l0[2];
  1149. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1150. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1151. ref= ref0;
  1152. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  1153. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1154. }
  1155. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  1156. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  1157. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  1158. }else{
  1159. for(i8=0; i8<4; i8++){
  1160. const int x8 = i8&1;
  1161. const int y8 = i8>>1;
  1162. int ref0, scale;
  1163. const int16_t (*l1mv)[2]= l1mv0;
  1164. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1165. continue;
  1166. h->sub_mb_type[i8] = sub_mb_type;
  1167. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1168. if(IS_INTRA(mb_type_col)){
  1169. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1170. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1171. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1172. continue;
  1173. }
  1174. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1175. if(ref0 >= 0)
  1176. ref0 = map_col_to_list0[0][ref0];
  1177. else{
  1178. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1179. l1mv= l1mv1;
  1180. }
  1181. scale = dist_scale_factor[ref0];
  1182. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1183. if(IS_SUB_8X8(sub_mb_type)){
  1184. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1185. int mx = (scale * mv_col[0] + 128) >> 8;
  1186. int my = (scale * mv_col[1] + 128) >> 8;
  1187. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1188. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1189. }else
  1190. for(i4=0; i4<4; i4++){
  1191. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1192. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1193. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1194. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1195. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1196. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1197. }
  1198. }
  1199. }
  1200. }
  1201. }
  1202. static inline void write_back_motion(H264Context *h, int mb_type){
  1203. MpegEncContext * const s = &h->s;
  1204. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1205. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1206. int list;
  1207. if(!USES_LIST(mb_type, 0))
  1208. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1209. for(list=0; list<h->list_count; list++){
  1210. int y;
  1211. if(!USES_LIST(mb_type, list))
  1212. continue;
  1213. for(y=0; y<4; y++){
  1214. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
  1215. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
  1216. }
  1217. if( h->pps.cabac ) {
  1218. if(IS_SKIP(mb_type))
  1219. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1220. else
  1221. for(y=0; y<4; y++){
  1222. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1223. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1224. }
  1225. }
  1226. {
  1227. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1228. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1229. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1230. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1231. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1232. }
  1233. }
  1234. if(h->slice_type == B_TYPE && h->pps.cabac){
  1235. if(IS_8X8(mb_type)){
  1236. uint8_t *direct_table = &h->direct_table[b8_xy];
  1237. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1238. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1239. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1240. }
  1241. }
  1242. }
  1243. /**
  1244. * Decodes a network abstraction layer unit.
  1245. * @param consumed is the number of bytes used as input
  1246. * @param length is the length of the array
  1247. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1248. * @returns decoded bytes, might be src+1 if no escapes
  1249. */
  1250. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1251. int i, si, di;
  1252. uint8_t *dst;
  1253. int bufidx;
  1254. // src[0]&0x80; //forbidden bit
  1255. h->nal_ref_idc= src[0]>>5;
  1256. h->nal_unit_type= src[0]&0x1F;
  1257. src++; length--;
  1258. #if 0
  1259. for(i=0; i<length; i++)
  1260. printf("%2X ", src[i]);
  1261. #endif
  1262. for(i=0; i+1<length; i+=2){
  1263. if(src[i]) continue;
  1264. if(i>0 && src[i-1]==0) i--;
  1265. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1266. if(src[i+2]!=3){
  1267. /* startcode, so we must be past the end */
  1268. length=i;
  1269. }
  1270. break;
  1271. }
  1272. }
  1273. if(i>=length-1){ //no escaped 0
  1274. *dst_length= length;
  1275. *consumed= length+1; //+1 for the header
  1276. return src;
  1277. }
  1278. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  1279. h->rbsp_buffer[bufidx]= av_fast_realloc(h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length);
  1280. dst= h->rbsp_buffer[bufidx];
  1281. if (dst == NULL){
  1282. return NULL;
  1283. }
  1284. //printf("decoding esc\n");
  1285. si=di=0;
  1286. while(si<length){
  1287. //remove escapes (very rare 1:2^22)
  1288. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1289. if(src[si+2]==3){ //escape
  1290. dst[di++]= 0;
  1291. dst[di++]= 0;
  1292. si+=3;
  1293. continue;
  1294. }else //next start code
  1295. break;
  1296. }
  1297. dst[di++]= src[si++];
  1298. }
  1299. *dst_length= di;
  1300. *consumed= si + 1;//+1 for the header
  1301. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  1302. return dst;
  1303. }
  1304. /**
  1305. * identifies the exact end of the bitstream
  1306. * @return the length of the trailing, or 0 if damaged
  1307. */
  1308. static int decode_rbsp_trailing(H264Context *h, uint8_t *src){
  1309. int v= *src;
  1310. int r;
  1311. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  1312. for(r=1; r<9; r++){
  1313. if(v&1) return r;
  1314. v>>=1;
  1315. }
  1316. return 0;
  1317. }
  1318. /**
  1319. * idct tranforms the 16 dc values and dequantize them.
  1320. * @param qp quantization parameter
  1321. */
  1322. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1323. #define stride 16
  1324. int i;
  1325. int temp[16]; //FIXME check if this is a good idea
  1326. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1327. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1328. //memset(block, 64, 2*256);
  1329. //return;
  1330. for(i=0; i<4; i++){
  1331. const int offset= y_offset[i];
  1332. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1333. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1334. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1335. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1336. temp[4*i+0]= z0+z3;
  1337. temp[4*i+1]= z1+z2;
  1338. temp[4*i+2]= z1-z2;
  1339. temp[4*i+3]= z0-z3;
  1340. }
  1341. for(i=0; i<4; i++){
  1342. const int offset= x_offset[i];
  1343. const int z0= temp[4*0+i] + temp[4*2+i];
  1344. const int z1= temp[4*0+i] - temp[4*2+i];
  1345. const int z2= temp[4*1+i] - temp[4*3+i];
  1346. const int z3= temp[4*1+i] + temp[4*3+i];
  1347. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1348. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1349. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1350. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1351. }
  1352. }
  1353. #if 0
  1354. /**
  1355. * dct tranforms the 16 dc values.
  1356. * @param qp quantization parameter ??? FIXME
  1357. */
  1358. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1359. // const int qmul= dequant_coeff[qp][0];
  1360. int i;
  1361. int temp[16]; //FIXME check if this is a good idea
  1362. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1363. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1364. for(i=0; i<4; i++){
  1365. const int offset= y_offset[i];
  1366. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1367. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1368. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1369. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1370. temp[4*i+0]= z0+z3;
  1371. temp[4*i+1]= z1+z2;
  1372. temp[4*i+2]= z1-z2;
  1373. temp[4*i+3]= z0-z3;
  1374. }
  1375. for(i=0; i<4; i++){
  1376. const int offset= x_offset[i];
  1377. const int z0= temp[4*0+i] + temp[4*2+i];
  1378. const int z1= temp[4*0+i] - temp[4*2+i];
  1379. const int z2= temp[4*1+i] - temp[4*3+i];
  1380. const int z3= temp[4*1+i] + temp[4*3+i];
  1381. block[stride*0 +offset]= (z0 + z3)>>1;
  1382. block[stride*2 +offset]= (z1 + z2)>>1;
  1383. block[stride*8 +offset]= (z1 - z2)>>1;
  1384. block[stride*10+offset]= (z0 - z3)>>1;
  1385. }
  1386. }
  1387. #endif
  1388. #undef xStride
  1389. #undef stride
  1390. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1391. const int stride= 16*2;
  1392. const int xStride= 16;
  1393. int a,b,c,d,e;
  1394. a= block[stride*0 + xStride*0];
  1395. b= block[stride*0 + xStride*1];
  1396. c= block[stride*1 + xStride*0];
  1397. d= block[stride*1 + xStride*1];
  1398. e= a-b;
  1399. a= a+b;
  1400. b= c-d;
  1401. c= c+d;
  1402. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1403. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1404. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1405. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1406. }
  1407. #if 0
  1408. static void chroma_dc_dct_c(DCTELEM *block){
  1409. const int stride= 16*2;
  1410. const int xStride= 16;
  1411. int a,b,c,d,e;
  1412. a= block[stride*0 + xStride*0];
  1413. b= block[stride*0 + xStride*1];
  1414. c= block[stride*1 + xStride*0];
  1415. d= block[stride*1 + xStride*1];
  1416. e= a-b;
  1417. a= a+b;
  1418. b= c-d;
  1419. c= c+d;
  1420. block[stride*0 + xStride*0]= (a+c);
  1421. block[stride*0 + xStride*1]= (e+b);
  1422. block[stride*1 + xStride*0]= (a-c);
  1423. block[stride*1 + xStride*1]= (e-b);
  1424. }
  1425. #endif
  1426. /**
  1427. * gets the chroma qp.
  1428. */
  1429. static inline int get_chroma_qp(H264Context *h, int t, int qscale){
  1430. return h->pps.chroma_qp_table[t][qscale & 0xff];
  1431. }
  1432. //FIXME need to check that this does not overflow signed 32 bit for low qp, i am not sure, it's very close
  1433. //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
  1434. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
  1435. int i;
  1436. const int * const quant_table= quant_coeff[qscale];
  1437. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1438. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1439. const unsigned int threshold2= (threshold1<<1);
  1440. int last_non_zero;
  1441. if(separate_dc){
  1442. if(qscale<=18){
  1443. //avoid overflows
  1444. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1445. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1446. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1447. int level= block[0]*quant_coeff[qscale+18][0];
  1448. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1449. if(level>0){
  1450. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1451. block[0]= level;
  1452. }else{
  1453. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1454. block[0]= -level;
  1455. }
  1456. // last_non_zero = i;
  1457. }else{
  1458. block[0]=0;
  1459. }
  1460. }else{
  1461. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1462. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1463. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1464. int level= block[0]*quant_table[0];
  1465. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1466. if(level>0){
  1467. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1468. block[0]= level;
  1469. }else{
  1470. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1471. block[0]= -level;
  1472. }
  1473. // last_non_zero = i;
  1474. }else{
  1475. block[0]=0;
  1476. }
  1477. }
  1478. last_non_zero= 0;
  1479. i=1;
  1480. }else{
  1481. last_non_zero= -1;
  1482. i=0;
  1483. }
  1484. for(; i<16; i++){
  1485. const int j= scantable[i];
  1486. int level= block[j]*quant_table[j];
  1487. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1488. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1489. if(((unsigned)(level+threshold1))>threshold2){
  1490. if(level>0){
  1491. level= (bias + level)>>QUANT_SHIFT;
  1492. block[j]= level;
  1493. }else{
  1494. level= (bias - level)>>QUANT_SHIFT;
  1495. block[j]= -level;
  1496. }
  1497. last_non_zero = i;
  1498. }else{
  1499. block[j]=0;
  1500. }
  1501. }
  1502. return last_non_zero;
  1503. }
  1504. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1505. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1506. ((uint32_t*)(src+0*stride))[0]= a;
  1507. ((uint32_t*)(src+1*stride))[0]= a;
  1508. ((uint32_t*)(src+2*stride))[0]= a;
  1509. ((uint32_t*)(src+3*stride))[0]= a;
  1510. }
  1511. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1512. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1513. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1514. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1515. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1516. }
  1517. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1518. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1519. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1520. ((uint32_t*)(src+0*stride))[0]=
  1521. ((uint32_t*)(src+1*stride))[0]=
  1522. ((uint32_t*)(src+2*stride))[0]=
  1523. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1524. }
  1525. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1526. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1527. ((uint32_t*)(src+0*stride))[0]=
  1528. ((uint32_t*)(src+1*stride))[0]=
  1529. ((uint32_t*)(src+2*stride))[0]=
  1530. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1531. }
  1532. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1533. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1534. ((uint32_t*)(src+0*stride))[0]=
  1535. ((uint32_t*)(src+1*stride))[0]=
  1536. ((uint32_t*)(src+2*stride))[0]=
  1537. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1538. }
  1539. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1540. ((uint32_t*)(src+0*stride))[0]=
  1541. ((uint32_t*)(src+1*stride))[0]=
  1542. ((uint32_t*)(src+2*stride))[0]=
  1543. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1544. }
  1545. #define LOAD_TOP_RIGHT_EDGE\
  1546. const int av_unused t4= topright[0];\
  1547. const int av_unused t5= topright[1];\
  1548. const int av_unused t6= topright[2];\
  1549. const int av_unused t7= topright[3];\
  1550. #define LOAD_LEFT_EDGE\
  1551. const int av_unused l0= src[-1+0*stride];\
  1552. const int av_unused l1= src[-1+1*stride];\
  1553. const int av_unused l2= src[-1+2*stride];\
  1554. const int av_unused l3= src[-1+3*stride];\
  1555. #define LOAD_TOP_EDGE\
  1556. const int av_unused t0= src[ 0-1*stride];\
  1557. const int av_unused t1= src[ 1-1*stride];\
  1558. const int av_unused t2= src[ 2-1*stride];\
  1559. const int av_unused t3= src[ 3-1*stride];\
  1560. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1561. const int lt= src[-1-1*stride];
  1562. LOAD_TOP_EDGE
  1563. LOAD_LEFT_EDGE
  1564. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1565. src[0+2*stride]=
  1566. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1567. src[0+1*stride]=
  1568. src[1+2*stride]=
  1569. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1570. src[0+0*stride]=
  1571. src[1+1*stride]=
  1572. src[2+2*stride]=
  1573. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1574. src[1+0*stride]=
  1575. src[2+1*stride]=
  1576. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1577. src[2+0*stride]=
  1578. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1579. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1580. }
  1581. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1582. LOAD_TOP_EDGE
  1583. LOAD_TOP_RIGHT_EDGE
  1584. // LOAD_LEFT_EDGE
  1585. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1586. src[1+0*stride]=
  1587. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1588. src[2+0*stride]=
  1589. src[1+1*stride]=
  1590. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1591. src[3+0*stride]=
  1592. src[2+1*stride]=
  1593. src[1+2*stride]=
  1594. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1595. src[3+1*stride]=
  1596. src[2+2*stride]=
  1597. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1598. src[3+2*stride]=
  1599. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1600. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1601. }
  1602. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1603. const int lt= src[-1-1*stride];
  1604. LOAD_TOP_EDGE
  1605. LOAD_LEFT_EDGE
  1606. src[0+0*stride]=
  1607. src[1+2*stride]=(lt + t0 + 1)>>1;
  1608. src[1+0*stride]=
  1609. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1610. src[2+0*stride]=
  1611. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1612. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1613. src[0+1*stride]=
  1614. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1615. src[1+1*stride]=
  1616. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1617. src[2+1*stride]=
  1618. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1619. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1620. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1621. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1622. }
  1623. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1624. LOAD_TOP_EDGE
  1625. LOAD_TOP_RIGHT_EDGE
  1626. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1627. src[1+0*stride]=
  1628. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1629. src[2+0*stride]=
  1630. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1631. src[3+0*stride]=
  1632. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1633. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1634. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1635. src[1+1*stride]=
  1636. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1637. src[2+1*stride]=
  1638. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1639. src[3+1*stride]=
  1640. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1641. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1642. }
  1643. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1644. LOAD_LEFT_EDGE
  1645. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1646. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1647. src[2+0*stride]=
  1648. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1649. src[3+0*stride]=
  1650. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1651. src[2+1*stride]=
  1652. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1653. src[3+1*stride]=
  1654. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1655. src[3+2*stride]=
  1656. src[1+3*stride]=
  1657. src[0+3*stride]=
  1658. src[2+2*stride]=
  1659. src[2+3*stride]=
  1660. src[3+3*stride]=l3;
  1661. }
  1662. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1663. const int lt= src[-1-1*stride];
  1664. LOAD_TOP_EDGE
  1665. LOAD_LEFT_EDGE
  1666. src[0+0*stride]=
  1667. src[2+1*stride]=(lt + l0 + 1)>>1;
  1668. src[1+0*stride]=
  1669. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1670. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1671. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1672. src[0+1*stride]=
  1673. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1674. src[1+1*stride]=
  1675. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1676. src[0+2*stride]=
  1677. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1678. src[1+2*stride]=
  1679. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1680. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1681. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1682. }
  1683. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1684. int i;
  1685. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1686. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1687. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1688. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1689. for(i=0; i<16; i++){
  1690. ((uint32_t*)(src+i*stride))[0]= a;
  1691. ((uint32_t*)(src+i*stride))[1]= b;
  1692. ((uint32_t*)(src+i*stride))[2]= c;
  1693. ((uint32_t*)(src+i*stride))[3]= d;
  1694. }
  1695. }
  1696. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  1697. int i;
  1698. for(i=0; i<16; i++){
  1699. ((uint32_t*)(src+i*stride))[0]=
  1700. ((uint32_t*)(src+i*stride))[1]=
  1701. ((uint32_t*)(src+i*stride))[2]=
  1702. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  1703. }
  1704. }
  1705. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  1706. int i, dc=0;
  1707. for(i=0;i<16; i++){
  1708. dc+= src[-1+i*stride];
  1709. }
  1710. for(i=0;i<16; i++){
  1711. dc+= src[i-stride];
  1712. }
  1713. dc= 0x01010101*((dc + 16)>>5);
  1714. for(i=0; i<16; i++){
  1715. ((uint32_t*)(src+i*stride))[0]=
  1716. ((uint32_t*)(src+i*stride))[1]=
  1717. ((uint32_t*)(src+i*stride))[2]=
  1718. ((uint32_t*)(src+i*stride))[3]= dc;
  1719. }
  1720. }
  1721. void ff_pred16x16_left_dc_c(uint8_t *src, int stride){
  1722. int i, dc=0;
  1723. for(i=0;i<16; i++){
  1724. dc+= src[-1+i*stride];
  1725. }
  1726. dc= 0x01010101*((dc + 8)>>4);
  1727. for(i=0; i<16; i++){
  1728. ((uint32_t*)(src+i*stride))[0]=
  1729. ((uint32_t*)(src+i*stride))[1]=
  1730. ((uint32_t*)(src+i*stride))[2]=
  1731. ((uint32_t*)(src+i*stride))[3]= dc;
  1732. }
  1733. }
  1734. void ff_pred16x16_top_dc_c(uint8_t *src, int stride){
  1735. int i, dc=0;
  1736. for(i=0;i<16; i++){
  1737. dc+= src[i-stride];
  1738. }
  1739. dc= 0x01010101*((dc + 8)>>4);
  1740. for(i=0; i<16; i++){
  1741. ((uint32_t*)(src+i*stride))[0]=
  1742. ((uint32_t*)(src+i*stride))[1]=
  1743. ((uint32_t*)(src+i*stride))[2]=
  1744. ((uint32_t*)(src+i*stride))[3]= dc;
  1745. }
  1746. }
  1747. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  1748. int i;
  1749. for(i=0; i<16; i++){
  1750. ((uint32_t*)(src+i*stride))[0]=
  1751. ((uint32_t*)(src+i*stride))[1]=
  1752. ((uint32_t*)(src+i*stride))[2]=
  1753. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  1754. }
  1755. }
  1756. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1757. int i, j, k;
  1758. int a;
  1759. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1760. const uint8_t * const src0 = src+7-stride;
  1761. const uint8_t *src1 = src+8*stride-1;
  1762. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1763. int H = src0[1] - src0[-1];
  1764. int V = src1[0] - src2[ 0];
  1765. for(k=2; k<=8; ++k) {
  1766. src1 += stride; src2 -= stride;
  1767. H += k*(src0[k] - src0[-k]);
  1768. V += k*(src1[0] - src2[ 0]);
  1769. }
  1770. if(svq3){
  1771. H = ( 5*(H/4) ) / 16;
  1772. V = ( 5*(V/4) ) / 16;
  1773. /* required for 100% accuracy */
  1774. i = H; H = V; V = i;
  1775. }else{
  1776. H = ( 5*H+32 ) >> 6;
  1777. V = ( 5*V+32 ) >> 6;
  1778. }
  1779. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1780. for(j=16; j>0; --j) {
  1781. int b = a;
  1782. a += V;
  1783. for(i=-16; i<0; i+=4) {
  1784. src[16+i] = cm[ (b ) >> 5 ];
  1785. src[17+i] = cm[ (b+ H) >> 5 ];
  1786. src[18+i] = cm[ (b+2*H) >> 5 ];
  1787. src[19+i] = cm[ (b+3*H) >> 5 ];
  1788. b += 4*H;
  1789. }
  1790. src += stride;
  1791. }
  1792. }
  1793. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  1794. pred16x16_plane_compat_c(src, stride, 0);
  1795. }
  1796. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  1797. int i;
  1798. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1799. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1800. for(i=0; i<8; i++){
  1801. ((uint32_t*)(src+i*stride))[0]= a;
  1802. ((uint32_t*)(src+i*stride))[1]= b;
  1803. }
  1804. }
  1805. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  1806. int i;
  1807. for(i=0; i<8; i++){
  1808. ((uint32_t*)(src+i*stride))[0]=
  1809. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1810. }
  1811. }
  1812. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  1813. int i;
  1814. for(i=0; i<8; i++){
  1815. ((uint32_t*)(src+i*stride))[0]=
  1816. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1817. }
  1818. }
  1819. void ff_pred8x8_left_dc_c(uint8_t *src, int stride){
  1820. int i;
  1821. int dc0, dc2;
  1822. dc0=dc2=0;
  1823. for(i=0;i<4; i++){
  1824. dc0+= src[-1+i*stride];
  1825. dc2+= src[-1+(i+4)*stride];
  1826. }
  1827. dc0= 0x01010101*((dc0 + 2)>>2);
  1828. dc2= 0x01010101*((dc2 + 2)>>2);
  1829. for(i=0; i<4; i++){
  1830. ((uint32_t*)(src+i*stride))[0]=
  1831. ((uint32_t*)(src+i*stride))[1]= dc0;
  1832. }
  1833. for(i=4; i<8; i++){
  1834. ((uint32_t*)(src+i*stride))[0]=
  1835. ((uint32_t*)(src+i*stride))[1]= dc2;
  1836. }
  1837. }
  1838. void ff_pred8x8_top_dc_c(uint8_t *src, int stride){
  1839. int i;
  1840. int dc0, dc1;
  1841. dc0=dc1=0;
  1842. for(i=0;i<4; i++){
  1843. dc0+= src[i-stride];
  1844. dc1+= src[4+i-stride];
  1845. }
  1846. dc0= 0x01010101*((dc0 + 2)>>2);
  1847. dc1= 0x01010101*((dc1 + 2)>>2);
  1848. for(i=0; i<4; i++){
  1849. ((uint32_t*)(src+i*stride))[0]= dc0;
  1850. ((uint32_t*)(src+i*stride))[1]= dc1;
  1851. }
  1852. for(i=4; i<8; i++){
  1853. ((uint32_t*)(src+i*stride))[0]= dc0;
  1854. ((uint32_t*)(src+i*stride))[1]= dc1;
  1855. }
  1856. }
  1857. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  1858. int i;
  1859. int dc0, dc1, dc2, dc3;
  1860. dc0=dc1=dc2=0;
  1861. for(i=0;i<4; i++){
  1862. dc0+= src[-1+i*stride] + src[i-stride];
  1863. dc1+= src[4+i-stride];
  1864. dc2+= src[-1+(i+4)*stride];
  1865. }
  1866. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  1867. dc0= 0x01010101*((dc0 + 4)>>3);
  1868. dc1= 0x01010101*((dc1 + 2)>>2);
  1869. dc2= 0x01010101*((dc2 + 2)>>2);
  1870. for(i=0; i<4; i++){
  1871. ((uint32_t*)(src+i*stride))[0]= dc0;
  1872. ((uint32_t*)(src+i*stride))[1]= dc1;
  1873. }
  1874. for(i=4; i<8; i++){
  1875. ((uint32_t*)(src+i*stride))[0]= dc2;
  1876. ((uint32_t*)(src+i*stride))[1]= dc3;
  1877. }
  1878. }
  1879. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  1880. int j, k;
  1881. int a;
  1882. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1883. const uint8_t * const src0 = src+3-stride;
  1884. const uint8_t *src1 = src+4*stride-1;
  1885. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  1886. int H = src0[1] - src0[-1];
  1887. int V = src1[0] - src2[ 0];
  1888. for(k=2; k<=4; ++k) {
  1889. src1 += stride; src2 -= stride;
  1890. H += k*(src0[k] - src0[-k]);
  1891. V += k*(src1[0] - src2[ 0]);
  1892. }
  1893. H = ( 17*H+16 ) >> 5;
  1894. V = ( 17*V+16 ) >> 5;
  1895. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  1896. for(j=8; j>0; --j) {
  1897. int b = a;
  1898. a += V;
  1899. src[0] = cm[ (b ) >> 5 ];
  1900. src[1] = cm[ (b+ H) >> 5 ];
  1901. src[2] = cm[ (b+2*H) >> 5 ];
  1902. src[3] = cm[ (b+3*H) >> 5 ];
  1903. src[4] = cm[ (b+4*H) >> 5 ];
  1904. src[5] = cm[ (b+5*H) >> 5 ];
  1905. src[6] = cm[ (b+6*H) >> 5 ];
  1906. src[7] = cm[ (b+7*H) >> 5 ];
  1907. src += stride;
  1908. }
  1909. }
  1910. #define SRC(x,y) src[(x)+(y)*stride]
  1911. #define PL(y) \
  1912. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  1913. #define PREDICT_8x8_LOAD_LEFT \
  1914. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  1915. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  1916. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  1917. const int l7 av_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  1918. #define PT(x) \
  1919. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  1920. #define PREDICT_8x8_LOAD_TOP \
  1921. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  1922. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  1923. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  1924. const int t7 av_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  1925. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  1926. #define PTR(x) \
  1927. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  1928. #define PREDICT_8x8_LOAD_TOPRIGHT \
  1929. int t8, t9, t10, t11, t12, t13, t14, t15; \
  1930. if(has_topright) { \
  1931. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  1932. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  1933. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  1934. #define PREDICT_8x8_LOAD_TOPLEFT \
  1935. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  1936. #define PREDICT_8x8_DC(v) \
  1937. int y; \
  1938. for( y = 0; y < 8; y++ ) { \
  1939. ((uint32_t*)src)[0] = \
  1940. ((uint32_t*)src)[1] = v; \
  1941. src += stride; \
  1942. }
  1943. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1944. {
  1945. PREDICT_8x8_DC(0x80808080);
  1946. }
  1947. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1948. {
  1949. PREDICT_8x8_LOAD_LEFT;
  1950. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  1951. PREDICT_8x8_DC(dc);
  1952. }
  1953. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1954. {
  1955. PREDICT_8x8_LOAD_TOP;
  1956. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  1957. PREDICT_8x8_DC(dc);
  1958. }
  1959. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1960. {
  1961. PREDICT_8x8_LOAD_LEFT;
  1962. PREDICT_8x8_LOAD_TOP;
  1963. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  1964. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  1965. PREDICT_8x8_DC(dc);
  1966. }
  1967. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1968. {
  1969. PREDICT_8x8_LOAD_LEFT;
  1970. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  1971. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  1972. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  1973. #undef ROW
  1974. }
  1975. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1976. {
  1977. int y;
  1978. PREDICT_8x8_LOAD_TOP;
  1979. src[0] = t0;
  1980. src[1] = t1;
  1981. src[2] = t2;
  1982. src[3] = t3;
  1983. src[4] = t4;
  1984. src[5] = t5;
  1985. src[6] = t6;
  1986. src[7] = t7;
  1987. for( y = 1; y < 8; y++ )
  1988. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  1989. }
  1990. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1991. {
  1992. PREDICT_8x8_LOAD_TOP;
  1993. PREDICT_8x8_LOAD_TOPRIGHT;
  1994. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  1995. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  1996. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  1997. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  1998. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  1999. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2000. SRC(0,6)=SRC(1,5)=SRC(2,4)=SRC(3,3)=SRC(4,2)=SRC(5,1)=SRC(6,0)= (t6 + 2*t7 + t8 + 2) >> 2;
  2001. SRC(0,7)=SRC(1,6)=SRC(2,5)=SRC(3,4)=SRC(4,3)=SRC(5,2)=SRC(6,1)=SRC(7,0)= (t7 + 2*t8 + t9 + 2) >> 2;
  2002. SRC(1,7)=SRC(2,6)=SRC(3,5)=SRC(4,4)=SRC(5,3)=SRC(6,2)=SRC(7,1)= (t8 + 2*t9 + t10 + 2) >> 2;
  2003. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2004. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2005. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2006. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2007. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2008. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2009. }
  2010. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2011. {
  2012. PREDICT_8x8_LOAD_TOP;
  2013. PREDICT_8x8_LOAD_LEFT;
  2014. PREDICT_8x8_LOAD_TOPLEFT;
  2015. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2016. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2017. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2018. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2019. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2020. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2021. SRC(0,1)=SRC(1,2)=SRC(2,3)=SRC(3,4)=SRC(4,5)=SRC(5,6)=SRC(6,7)= (l1 + 2*l0 + lt + 2) >> 2;
  2022. SRC(0,0)=SRC(1,1)=SRC(2,2)=SRC(3,3)=SRC(4,4)=SRC(5,5)=SRC(6,6)=SRC(7,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2023. SRC(1,0)=SRC(2,1)=SRC(3,2)=SRC(4,3)=SRC(5,4)=SRC(6,5)=SRC(7,6)= (lt + 2*t0 + t1 + 2) >> 2;
  2024. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2025. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2026. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2027. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2028. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2029. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2030. }
  2031. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2032. {
  2033. PREDICT_8x8_LOAD_TOP;
  2034. PREDICT_8x8_LOAD_LEFT;
  2035. PREDICT_8x8_LOAD_TOPLEFT;
  2036. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2037. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2038. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2039. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2040. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2041. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2042. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2043. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2044. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2045. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2046. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2047. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2048. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2049. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2050. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2051. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2052. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2053. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2054. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2055. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2056. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2057. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2058. }
  2059. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2060. {
  2061. PREDICT_8x8_LOAD_TOP;
  2062. PREDICT_8x8_LOAD_LEFT;
  2063. PREDICT_8x8_LOAD_TOPLEFT;
  2064. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2065. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2066. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2067. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2068. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2069. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2070. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2071. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2072. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2073. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2074. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2075. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2076. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2077. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2078. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2079. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2080. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2081. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2082. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2083. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2084. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2085. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2086. }
  2087. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2088. {
  2089. PREDICT_8x8_LOAD_TOP;
  2090. PREDICT_8x8_LOAD_TOPRIGHT;
  2091. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2092. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2093. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2094. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2095. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2096. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2097. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2098. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2099. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2100. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2101. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2102. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2103. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2104. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2105. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2106. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2107. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2108. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2109. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2110. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2111. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2112. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2113. }
  2114. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2115. {
  2116. PREDICT_8x8_LOAD_LEFT;
  2117. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2118. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2119. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2120. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2121. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2122. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2123. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2124. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2125. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2126. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2127. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2128. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2129. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2130. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2131. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2132. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2133. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2134. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2135. }
  2136. #undef PREDICT_8x8_LOAD_LEFT
  2137. #undef PREDICT_8x8_LOAD_TOP
  2138. #undef PREDICT_8x8_LOAD_TOPLEFT
  2139. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2140. #undef PREDICT_8x8_DC
  2141. #undef PTR
  2142. #undef PT
  2143. #undef PL
  2144. #undef SRC
  2145. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2146. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2147. int src_x_offset, int src_y_offset,
  2148. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2149. MpegEncContext * const s = &h->s;
  2150. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2151. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2152. const int luma_xy= (mx&3) + ((my&3)<<2);
  2153. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2154. uint8_t * src_cb, * src_cr;
  2155. int extra_width= h->emu_edge_width;
  2156. int extra_height= h->emu_edge_height;
  2157. int emu=0;
  2158. const int full_mx= mx>>2;
  2159. const int full_my= my>>2;
  2160. const int pic_width = 16*s->mb_width;
  2161. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2162. if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
  2163. return;
  2164. if(mx&7) extra_width -= 3;
  2165. if(my&7) extra_height -= 3;
  2166. if( full_mx < 0-extra_width
  2167. || full_my < 0-extra_height
  2168. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2169. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2170. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  2171. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2172. emu=1;
  2173. }
  2174. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2175. if(!square){
  2176. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2177. }
  2178. if(ENABLE_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  2179. if(MB_MBAFF){
  2180. // chroma offset when predicting from a field of opposite parity
  2181. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2182. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2183. }
  2184. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2185. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2186. if(emu){
  2187. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2188. src_cb= s->edge_emu_buffer;
  2189. }
  2190. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2191. if(emu){
  2192. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2193. src_cr= s->edge_emu_buffer;
  2194. }
  2195. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2196. }
  2197. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2198. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2199. int x_offset, int y_offset,
  2200. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2201. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2202. int list0, int list1){
  2203. MpegEncContext * const s = &h->s;
  2204. qpel_mc_func *qpix_op= qpix_put;
  2205. h264_chroma_mc_func chroma_op= chroma_put;
  2206. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2207. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2208. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2209. x_offset += 8*s->mb_x;
  2210. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2211. if(list0){
  2212. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2213. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2214. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2215. qpix_op, chroma_op);
  2216. qpix_op= qpix_avg;
  2217. chroma_op= chroma_avg;
  2218. }
  2219. if(list1){
  2220. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2221. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2222. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2223. qpix_op, chroma_op);
  2224. }
  2225. }
  2226. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2227. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2228. int x_offset, int y_offset,
  2229. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2230. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2231. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2232. int list0, int list1){
  2233. MpegEncContext * const s = &h->s;
  2234. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2235. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2236. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2237. x_offset += 8*s->mb_x;
  2238. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2239. if(list0 && list1){
  2240. /* don't optimize for luma-only case, since B-frames usually
  2241. * use implicit weights => chroma too. */
  2242. uint8_t *tmp_cb = s->obmc_scratchpad;
  2243. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2244. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2245. int refn0 = h->ref_cache[0][ scan8[n] ];
  2246. int refn1 = h->ref_cache[1][ scan8[n] ];
  2247. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2248. dest_y, dest_cb, dest_cr,
  2249. x_offset, y_offset, qpix_put, chroma_put);
  2250. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2251. tmp_y, tmp_cb, tmp_cr,
  2252. x_offset, y_offset, qpix_put, chroma_put);
  2253. if(h->use_weight == 2){
  2254. int weight0 = h->implicit_weight[refn0][refn1];
  2255. int weight1 = 64 - weight0;
  2256. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2257. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2258. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2259. }else{
  2260. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2261. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2262. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2263. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2264. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2265. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2266. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2267. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2268. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2269. }
  2270. }else{
  2271. int list = list1 ? 1 : 0;
  2272. int refn = h->ref_cache[list][ scan8[n] ];
  2273. Picture *ref= &h->ref_list[list][refn];
  2274. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2275. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2276. qpix_put, chroma_put);
  2277. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2278. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2279. if(h->use_weight_chroma){
  2280. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2281. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2282. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2283. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2284. }
  2285. }
  2286. }
  2287. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2288. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2289. int x_offset, int y_offset,
  2290. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2291. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2292. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2293. int list0, int list1){
  2294. if((h->use_weight==2 && list0 && list1
  2295. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2296. || h->use_weight==1)
  2297. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2298. x_offset, y_offset, qpix_put, chroma_put,
  2299. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2300. else
  2301. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2302. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2303. }
  2304. static inline void prefetch_motion(H264Context *h, int list){
  2305. /* fetch pixels for estimated mv 4 macroblocks ahead
  2306. * optimized for 64byte cache lines */
  2307. MpegEncContext * const s = &h->s;
  2308. const int refn = h->ref_cache[list][scan8[0]];
  2309. if(refn >= 0){
  2310. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2311. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2312. uint8_t **src= h->ref_list[list][refn].data;
  2313. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2314. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2315. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2316. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2317. }
  2318. }
  2319. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2320. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2321. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2322. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2323. MpegEncContext * const s = &h->s;
  2324. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2325. const int mb_type= s->current_picture.mb_type[mb_xy];
  2326. assert(IS_INTER(mb_type));
  2327. prefetch_motion(h, 0);
  2328. if(IS_16X16(mb_type)){
  2329. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2330. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2331. &weight_op[0], &weight_avg[0],
  2332. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2333. }else if(IS_16X8(mb_type)){
  2334. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2335. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2336. &weight_op[1], &weight_avg[1],
  2337. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2338. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2339. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2340. &weight_op[1], &weight_avg[1],
  2341. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2342. }else if(IS_8X16(mb_type)){
  2343. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2344. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2345. &weight_op[2], &weight_avg[2],
  2346. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2347. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2348. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2349. &weight_op[2], &weight_avg[2],
  2350. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2351. }else{
  2352. int i;
  2353. assert(IS_8X8(mb_type));
  2354. for(i=0; i<4; i++){
  2355. const int sub_mb_type= h->sub_mb_type[i];
  2356. const int n= 4*i;
  2357. int x_offset= (i&1)<<2;
  2358. int y_offset= (i&2)<<1;
  2359. if(IS_SUB_8X8(sub_mb_type)){
  2360. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2361. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2362. &weight_op[3], &weight_avg[3],
  2363. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2364. }else if(IS_SUB_8X4(sub_mb_type)){
  2365. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2366. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2367. &weight_op[4], &weight_avg[4],
  2368. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2369. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2370. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2371. &weight_op[4], &weight_avg[4],
  2372. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2373. }else if(IS_SUB_4X8(sub_mb_type)){
  2374. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2375. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2376. &weight_op[5], &weight_avg[5],
  2377. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2378. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2379. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2380. &weight_op[5], &weight_avg[5],
  2381. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2382. }else{
  2383. int j;
  2384. assert(IS_SUB_4X4(sub_mb_type));
  2385. for(j=0; j<4; j++){
  2386. int sub_x_offset= x_offset + 2*(j&1);
  2387. int sub_y_offset= y_offset + (j&2);
  2388. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2389. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2390. &weight_op[6], &weight_avg[6],
  2391. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2392. }
  2393. }
  2394. }
  2395. }
  2396. prefetch_motion(h, 1);
  2397. }
  2398. static void decode_init_vlc(void){
  2399. static int done = 0;
  2400. if (!done) {
  2401. int i;
  2402. done = 1;
  2403. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2404. &chroma_dc_coeff_token_len [0], 1, 1,
  2405. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2406. for(i=0; i<4; i++){
  2407. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2408. &coeff_token_len [i][0], 1, 1,
  2409. &coeff_token_bits[i][0], 1, 1, 1);
  2410. }
  2411. for(i=0; i<3; i++){
  2412. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2413. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2414. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2415. }
  2416. for(i=0; i<15; i++){
  2417. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2418. &total_zeros_len [i][0], 1, 1,
  2419. &total_zeros_bits[i][0], 1, 1, 1);
  2420. }
  2421. for(i=0; i<6; i++){
  2422. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2423. &run_len [i][0], 1, 1,
  2424. &run_bits[i][0], 1, 1, 1);
  2425. }
  2426. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2427. &run_len [6][0], 1, 1,
  2428. &run_bits[6][0], 1, 1, 1);
  2429. }
  2430. }
  2431. /**
  2432. * Sets the intra prediction function pointers.
  2433. */
  2434. static void init_pred_ptrs(H264Context *h){
  2435. // MpegEncContext * const s = &h->s;
  2436. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2437. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2438. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2439. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2440. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2441. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2442. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2443. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2444. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2445. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2446. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2447. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2448. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2449. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2450. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2451. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2452. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2453. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2454. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2455. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2456. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2457. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2458. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2459. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2460. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2461. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2462. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2463. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2464. h->pred8x8[LEFT_DC_PRED8x8]= ff_pred8x8_left_dc_c;
  2465. h->pred8x8[TOP_DC_PRED8x8 ]= ff_pred8x8_top_dc_c;
  2466. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2467. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2468. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2469. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2470. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2471. h->pred16x16[LEFT_DC_PRED8x8]= ff_pred16x16_left_dc_c;
  2472. h->pred16x16[TOP_DC_PRED8x8 ]= ff_pred16x16_top_dc_c;
  2473. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2474. }
  2475. static void free_tables(H264Context *h){
  2476. int i;
  2477. av_freep(&h->intra4x4_pred_mode);
  2478. av_freep(&h->chroma_pred_mode_table);
  2479. av_freep(&h->cbp_table);
  2480. av_freep(&h->mvd_table[0]);
  2481. av_freep(&h->mvd_table[1]);
  2482. av_freep(&h->direct_table);
  2483. av_freep(&h->non_zero_count);
  2484. av_freep(&h->slice_table_base);
  2485. av_freep(&h->top_borders[1]);
  2486. av_freep(&h->top_borders[0]);
  2487. h->slice_table= NULL;
  2488. av_freep(&h->mb2b_xy);
  2489. av_freep(&h->mb2b8_xy);
  2490. av_freep(&h->s.obmc_scratchpad);
  2491. for(i = 0; i < MAX_SPS_COUNT; i++)
  2492. av_freep(h->sps_buffers + i);
  2493. for(i = 0; i < MAX_PPS_COUNT; i++)
  2494. av_freep(h->pps_buffers + i);
  2495. }
  2496. static void init_dequant8_coeff_table(H264Context *h){
  2497. int i,q,x;
  2498. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2499. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2500. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2501. for(i=0; i<2; i++ ){
  2502. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2503. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2504. break;
  2505. }
  2506. for(q=0; q<52; q++){
  2507. int shift = ff_div6[q];
  2508. int idx = ff_rem6[q];
  2509. for(x=0; x<64; x++)
  2510. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2511. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2512. h->pps.scaling_matrix8[i][x]) << shift;
  2513. }
  2514. }
  2515. }
  2516. static void init_dequant4_coeff_table(H264Context *h){
  2517. int i,j,q,x;
  2518. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2519. for(i=0; i<6; i++ ){
  2520. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2521. for(j=0; j<i; j++){
  2522. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2523. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2524. break;
  2525. }
  2526. }
  2527. if(j<i)
  2528. continue;
  2529. for(q=0; q<52; q++){
  2530. int shift = ff_div6[q] + 2;
  2531. int idx = ff_rem6[q];
  2532. for(x=0; x<16; x++)
  2533. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2534. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2535. h->pps.scaling_matrix4[i][x]) << shift;
  2536. }
  2537. }
  2538. }
  2539. static void init_dequant_tables(H264Context *h){
  2540. int i,x;
  2541. init_dequant4_coeff_table(h);
  2542. if(h->pps.transform_8x8_mode)
  2543. init_dequant8_coeff_table(h);
  2544. if(h->sps.transform_bypass){
  2545. for(i=0; i<6; i++)
  2546. for(x=0; x<16; x++)
  2547. h->dequant4_coeff[i][0][x] = 1<<6;
  2548. if(h->pps.transform_8x8_mode)
  2549. for(i=0; i<2; i++)
  2550. for(x=0; x<64; x++)
  2551. h->dequant8_coeff[i][0][x] = 1<<6;
  2552. }
  2553. }
  2554. /**
  2555. * allocates tables.
  2556. * needs width/height
  2557. */
  2558. static int alloc_tables(H264Context *h){
  2559. MpegEncContext * const s = &h->s;
  2560. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2561. int x,y;
  2562. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2563. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2564. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2565. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2566. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2567. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2568. if( h->pps.cabac ) {
  2569. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2570. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2571. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2572. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2573. }
  2574. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2575. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2576. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2577. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2578. for(y=0; y<s->mb_height; y++){
  2579. for(x=0; x<s->mb_width; x++){
  2580. const int mb_xy= x + y*s->mb_stride;
  2581. const int b_xy = 4*x + 4*y*h->b_stride;
  2582. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2583. h->mb2b_xy [mb_xy]= b_xy;
  2584. h->mb2b8_xy[mb_xy]= b8_xy;
  2585. }
  2586. }
  2587. s->obmc_scratchpad = NULL;
  2588. if(!h->dequant4_coeff[0])
  2589. init_dequant_tables(h);
  2590. return 0;
  2591. fail:
  2592. free_tables(h);
  2593. return -1;
  2594. }
  2595. static void common_init(H264Context *h){
  2596. MpegEncContext * const s = &h->s;
  2597. s->width = s->avctx->width;
  2598. s->height = s->avctx->height;
  2599. s->codec_id= s->avctx->codec->id;
  2600. init_pred_ptrs(h);
  2601. h->dequant_coeff_pps= -1;
  2602. s->unrestricted_mv=1;
  2603. s->decode=1; //FIXME
  2604. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2605. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2606. }
  2607. static int decode_init(AVCodecContext *avctx){
  2608. H264Context *h= avctx->priv_data;
  2609. MpegEncContext * const s = &h->s;
  2610. MPV_decode_defaults(s);
  2611. s->avctx = avctx;
  2612. common_init(h);
  2613. s->out_format = FMT_H264;
  2614. s->workaround_bugs= avctx->workaround_bugs;
  2615. // set defaults
  2616. // s->decode_mb= ff_h263_decode_mb;
  2617. s->quarter_sample = 1;
  2618. s->low_delay= 1;
  2619. avctx->pix_fmt= PIX_FMT_YUV420P;
  2620. decode_init_vlc();
  2621. if(avctx->extradata_size > 0 && avctx->extradata &&
  2622. *(char *)avctx->extradata == 1){
  2623. h->is_avc = 1;
  2624. h->got_avcC = 0;
  2625. } else {
  2626. h->is_avc = 0;
  2627. }
  2628. return 0;
  2629. }
  2630. static int frame_start(H264Context *h){
  2631. MpegEncContext * const s = &h->s;
  2632. int i;
  2633. if(MPV_frame_start(s, s->avctx) < 0)
  2634. return -1;
  2635. ff_er_frame_start(s);
  2636. assert(s->linesize && s->uvlinesize);
  2637. for(i=0; i<16; i++){
  2638. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2639. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2640. }
  2641. for(i=0; i<4; i++){
  2642. h->block_offset[16+i]=
  2643. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2644. h->block_offset[24+16+i]=
  2645. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2646. }
  2647. /* can't be in alloc_tables because linesize isn't known there.
  2648. * FIXME: redo bipred weight to not require extra buffer? */
  2649. if(!s->obmc_scratchpad)
  2650. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2651. /* some macroblocks will be accessed before they're available */
  2652. if(FRAME_MBAFF)
  2653. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2654. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2655. return 0;
  2656. }
  2657. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
  2658. MpegEncContext * const s = &h->s;
  2659. int i;
  2660. src_y -= linesize;
  2661. src_cb -= uvlinesize;
  2662. src_cr -= uvlinesize;
  2663. // There are two lines saved, the line above the the top macroblock of a pair,
  2664. // and the line above the bottom macroblock
  2665. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2666. for(i=1; i<17; i++){
  2667. h->left_border[i]= src_y[15+i* linesize];
  2668. }
  2669. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2670. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2671. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2672. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2673. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2674. for(i=1; i<9; i++){
  2675. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2676. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2677. }
  2678. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2679. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2680. }
  2681. }
  2682. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
  2683. MpegEncContext * const s = &h->s;
  2684. int temp8, i;
  2685. uint64_t temp64;
  2686. int deblock_left;
  2687. int deblock_top;
  2688. int mb_xy;
  2689. if(h->deblocking_filter == 2) {
  2690. mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  2691. deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
  2692. deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
  2693. } else {
  2694. deblock_left = (s->mb_x > 0);
  2695. deblock_top = (s->mb_y > 0);
  2696. }
  2697. src_y -= linesize + 1;
  2698. src_cb -= uvlinesize + 1;
  2699. src_cr -= uvlinesize + 1;
  2700. #define XCHG(a,b,t,xchg)\
  2701. t= a;\
  2702. if(xchg)\
  2703. a= b;\
  2704. b= t;
  2705. if(deblock_left){
  2706. for(i = !deblock_top; i<17; i++){
  2707. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2708. }
  2709. }
  2710. if(deblock_top){
  2711. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2712. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2713. if(s->mb_x+1 < s->mb_width){
  2714. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2715. }
  2716. }
  2717. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2718. if(deblock_left){
  2719. for(i = !deblock_top; i<9; i++){
  2720. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  2721. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  2722. }
  2723. }
  2724. if(deblock_top){
  2725. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2726. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2727. }
  2728. }
  2729. }
  2730. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2731. MpegEncContext * const s = &h->s;
  2732. int i;
  2733. src_y -= 2 * linesize;
  2734. src_cb -= 2 * uvlinesize;
  2735. src_cr -= 2 * uvlinesize;
  2736. // There are two lines saved, the line above the the top macroblock of a pair,
  2737. // and the line above the bottom macroblock
  2738. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2739. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  2740. for(i=2; i<34; i++){
  2741. h->left_border[i]= src_y[15+i* linesize];
  2742. }
  2743. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  2744. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  2745. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  2746. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  2747. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2748. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  2749. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  2750. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  2751. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  2752. for(i=2; i<18; i++){
  2753. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  2754. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  2755. }
  2756. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  2757. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  2758. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  2759. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  2760. }
  2761. }
  2762. static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
  2763. MpegEncContext * const s = &h->s;
  2764. int temp8, i;
  2765. uint64_t temp64;
  2766. int deblock_left = (s->mb_x > 0);
  2767. int deblock_top = (s->mb_y > 1);
  2768. tprintf(s->avctx, "xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
  2769. src_y -= 2 * linesize + 1;
  2770. src_cb -= 2 * uvlinesize + 1;
  2771. src_cr -= 2 * uvlinesize + 1;
  2772. #define XCHG(a,b,t,xchg)\
  2773. t= a;\
  2774. if(xchg)\
  2775. a= b;\
  2776. b= t;
  2777. if(deblock_left){
  2778. for(i = (!deblock_top)<<1; i<34; i++){
  2779. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2780. }
  2781. }
  2782. if(deblock_top){
  2783. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2784. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2785. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  2786. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  2787. if(s->mb_x+1 < s->mb_width){
  2788. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2789. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  2790. }
  2791. }
  2792. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2793. if(deblock_left){
  2794. for(i = (!deblock_top) << 1; i<18; i++){
  2795. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  2796. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  2797. }
  2798. }
  2799. if(deblock_top){
  2800. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2801. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2802. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  2803. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  2804. }
  2805. }
  2806. }
  2807. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  2808. MpegEncContext * const s = &h->s;
  2809. const int mb_x= s->mb_x;
  2810. const int mb_y= s->mb_y;
  2811. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2812. const int mb_type= s->current_picture.mb_type[mb_xy];
  2813. uint8_t *dest_y, *dest_cb, *dest_cr;
  2814. int linesize, uvlinesize /*dct_offset*/;
  2815. int i;
  2816. int *block_offset = &h->block_offset[0];
  2817. const unsigned int bottom = mb_y & 1;
  2818. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
  2819. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  2820. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  2821. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2822. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2823. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2824. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  2825. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  2826. if (!simple && MB_FIELD) {
  2827. linesize = h->mb_linesize = s->linesize * 2;
  2828. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2829. block_offset = &h->block_offset[24];
  2830. if(mb_y&1){ //FIXME move out of this func?
  2831. dest_y -= s->linesize*15;
  2832. dest_cb-= s->uvlinesize*7;
  2833. dest_cr-= s->uvlinesize*7;
  2834. }
  2835. if(FRAME_MBAFF) {
  2836. int list;
  2837. for(list=0; list<h->list_count; list++){
  2838. if(!USES_LIST(mb_type, list))
  2839. continue;
  2840. if(IS_16X16(mb_type)){
  2841. int8_t *ref = &h->ref_cache[list][scan8[0]];
  2842. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  2843. }else{
  2844. for(i=0; i<16; i+=4){
  2845. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  2846. int ref = h->ref_cache[list][scan8[i]];
  2847. if(ref >= 0)
  2848. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  2849. }
  2850. }
  2851. }
  2852. }
  2853. } else {
  2854. linesize = h->mb_linesize = s->linesize;
  2855. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2856. // dct_offset = s->linesize * 16;
  2857. }
  2858. if(transform_bypass){
  2859. idct_dc_add =
  2860. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  2861. }else if(IS_8x8DCT(mb_type)){
  2862. idct_dc_add = s->dsp.h264_idct8_dc_add;
  2863. idct_add = s->dsp.h264_idct8_add;
  2864. }else{
  2865. idct_dc_add = s->dsp.h264_idct_dc_add;
  2866. idct_add = s->dsp.h264_idct_add;
  2867. }
  2868. if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  2869. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  2870. int mbt_y = mb_y&~1;
  2871. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  2872. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2873. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2874. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  2875. }
  2876. if (!simple && IS_INTRA_PCM(mb_type)) {
  2877. unsigned int x, y;
  2878. // The pixels are stored in h->mb array in the same order as levels,
  2879. // copy them in output in the correct order.
  2880. for(i=0; i<16; i++) {
  2881. for (y=0; y<4; y++) {
  2882. for (x=0; x<4; x++) {
  2883. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  2884. }
  2885. }
  2886. }
  2887. for(i=16; i<16+4; i++) {
  2888. for (y=0; y<4; y++) {
  2889. for (x=0; x<4; x++) {
  2890. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2891. }
  2892. }
  2893. }
  2894. for(i=20; i<20+4; i++) {
  2895. for (y=0; y<4; y++) {
  2896. for (x=0; x<4; x++) {
  2897. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2898. }
  2899. }
  2900. }
  2901. } else {
  2902. if(IS_INTRA(mb_type)){
  2903. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2904. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
  2905. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2906. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2907. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2908. }
  2909. if(IS_INTRA4x4(mb_type)){
  2910. if(simple || !s->encoding){
  2911. if(IS_8x8DCT(mb_type)){
  2912. for(i=0; i<16; i+=4){
  2913. uint8_t * const ptr= dest_y + block_offset[i];
  2914. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2915. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  2916. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  2917. (h->topright_samples_available<<i)&0x4000, linesize);
  2918. if(nnz){
  2919. if(nnz == 1 && h->mb[i*16])
  2920. idct_dc_add(ptr, h->mb + i*16, linesize);
  2921. else
  2922. idct_add(ptr, h->mb + i*16, linesize);
  2923. }
  2924. }
  2925. }else
  2926. for(i=0; i<16; i++){
  2927. uint8_t * const ptr= dest_y + block_offset[i];
  2928. uint8_t *topright;
  2929. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2930. int nnz, tr;
  2931. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  2932. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2933. assert(mb_y || linesize <= block_offset[i]);
  2934. if(!topright_avail){
  2935. tr= ptr[3 - linesize]*0x01010101;
  2936. topright= (uint8_t*) &tr;
  2937. }else
  2938. topright= ptr + 4 - linesize;
  2939. }else
  2940. topright= NULL;
  2941. h->pred4x4[ dir ](ptr, topright, linesize);
  2942. nnz = h->non_zero_count_cache[ scan8[i] ];
  2943. if(nnz){
  2944. if(is_h264){
  2945. if(nnz == 1 && h->mb[i*16])
  2946. idct_dc_add(ptr, h->mb + i*16, linesize);
  2947. else
  2948. idct_add(ptr, h->mb + i*16, linesize);
  2949. }else
  2950. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2951. }
  2952. }
  2953. }
  2954. }else{
  2955. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2956. if(is_h264){
  2957. if(!transform_bypass)
  2958. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  2959. }else
  2960. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2961. }
  2962. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2963. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
  2964. }else if(is_h264){
  2965. hl_motion(h, dest_y, dest_cb, dest_cr,
  2966. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  2967. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  2968. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  2969. }
  2970. if(!IS_INTRA4x4(mb_type)){
  2971. if(is_h264){
  2972. if(IS_INTRA16x16(mb_type)){
  2973. for(i=0; i<16; i++){
  2974. if(h->non_zero_count_cache[ scan8[i] ])
  2975. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2976. else if(h->mb[i*16])
  2977. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2978. }
  2979. }else{
  2980. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  2981. for(i=0; i<16; i+=di){
  2982. int nnz = h->non_zero_count_cache[ scan8[i] ];
  2983. if(nnz){
  2984. if(nnz==1 && h->mb[i*16])
  2985. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2986. else
  2987. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2988. }
  2989. }
  2990. }
  2991. }else{
  2992. for(i=0; i<16; i++){
  2993. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2994. uint8_t * const ptr= dest_y + block_offset[i];
  2995. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2996. }
  2997. }
  2998. }
  2999. }
  3000. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  3001. uint8_t *dest[2] = {dest_cb, dest_cr};
  3002. if(transform_bypass){
  3003. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3004. }else{
  3005. idct_add = s->dsp.h264_idct_add;
  3006. idct_dc_add = s->dsp.h264_idct_dc_add;
  3007. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  3008. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  3009. }
  3010. if(is_h264){
  3011. for(i=16; i<16+8; i++){
  3012. if(h->non_zero_count_cache[ scan8[i] ])
  3013. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3014. else if(h->mb[i*16])
  3015. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3016. }
  3017. }else{
  3018. for(i=16; i<16+8; i++){
  3019. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3020. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3021. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3022. }
  3023. }
  3024. }
  3025. }
  3026. }
  3027. if(h->deblocking_filter) {
  3028. if (!simple && FRAME_MBAFF) {
  3029. //FIXME try deblocking one mb at a time?
  3030. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3031. const int mb_y = s->mb_y - 1;
  3032. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3033. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3034. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3035. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3036. if (!bottom) return;
  3037. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3038. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3039. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3040. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3041. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3042. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3043. // deblock a pair
  3044. // top
  3045. s->mb_y--;
  3046. tprintf(h->s.avctx, "call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
  3047. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3048. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  3049. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  3050. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3051. // bottom
  3052. s->mb_y++;
  3053. tprintf(h->s.avctx, "call mbaff filter_mb\n");
  3054. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3055. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3056. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3057. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3058. } else {
  3059. tprintf(h->s.avctx, "call filter_mb\n");
  3060. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
  3061. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3062. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3063. }
  3064. }
  3065. }
  3066. /**
  3067. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  3068. */
  3069. static void hl_decode_mb_simple(H264Context *h){
  3070. hl_decode_mb_internal(h, 1);
  3071. }
  3072. /**
  3073. * Process a macroblock; this handles edge cases, such as interlacing.
  3074. */
  3075. static void av_noinline hl_decode_mb_complex(H264Context *h){
  3076. hl_decode_mb_internal(h, 0);
  3077. }
  3078. static void hl_decode_mb(H264Context *h){
  3079. MpegEncContext * const s = &h->s;
  3080. const int mb_x= s->mb_x;
  3081. const int mb_y= s->mb_y;
  3082. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3083. const int mb_type= s->current_picture.mb_type[mb_xy];
  3084. int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (ENABLE_GRAY && (s->flags&CODEC_FLAG_GRAY)) || s->encoding;
  3085. if(!s->decode)
  3086. return;
  3087. if (is_complex)
  3088. hl_decode_mb_complex(h);
  3089. else hl_decode_mb_simple(h);
  3090. }
  3091. /**
  3092. * fills the default_ref_list.
  3093. */
  3094. static int fill_default_ref_list(H264Context *h){
  3095. MpegEncContext * const s = &h->s;
  3096. int i;
  3097. int smallest_poc_greater_than_current = -1;
  3098. Picture sorted_short_ref[32];
  3099. if(h->slice_type==B_TYPE){
  3100. int out_i;
  3101. int limit= INT_MIN;
  3102. /* sort frame according to poc in B slice */
  3103. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3104. int best_i=INT_MIN;
  3105. int best_poc=INT_MAX;
  3106. for(i=0; i<h->short_ref_count; i++){
  3107. const int poc= h->short_ref[i]->poc;
  3108. if(poc > limit && poc < best_poc){
  3109. best_poc= poc;
  3110. best_i= i;
  3111. }
  3112. }
  3113. assert(best_i != INT_MIN);
  3114. limit= best_poc;
  3115. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3116. tprintf(h->s.avctx, "sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
  3117. if (-1 == smallest_poc_greater_than_current) {
  3118. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3119. smallest_poc_greater_than_current = out_i;
  3120. }
  3121. }
  3122. }
  3123. }
  3124. if(s->picture_structure == PICT_FRAME){
  3125. if(h->slice_type==B_TYPE){
  3126. int list;
  3127. tprintf(h->s.avctx, "current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3128. // find the largest poc
  3129. for(list=0; list<2; list++){
  3130. int index = 0;
  3131. int j= -99;
  3132. int step= list ? -1 : 1;
  3133. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3134. while(j<0 || j>= h->short_ref_count){
  3135. if(j != -99 && step == (list ? -1 : 1))
  3136. return -1;
  3137. step = -step;
  3138. j= smallest_poc_greater_than_current + (step>>1);
  3139. }
  3140. if(sorted_short_ref[j].reference != 3) continue;
  3141. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3142. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3143. }
  3144. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3145. if(h->long_ref[i] == NULL) continue;
  3146. if(h->long_ref[i]->reference != 3) continue;
  3147. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3148. h->default_ref_list[ list ][index++].pic_id= i;;
  3149. }
  3150. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3151. // swap the two first elements of L1 when
  3152. // L0 and L1 are identical
  3153. Picture temp= h->default_ref_list[1][0];
  3154. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3155. h->default_ref_list[1][1] = temp;
  3156. }
  3157. if(index < h->ref_count[ list ])
  3158. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3159. }
  3160. }else{
  3161. int index=0;
  3162. for(i=0; i<h->short_ref_count; i++){
  3163. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3164. h->default_ref_list[0][index ]= *h->short_ref[i];
  3165. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3166. }
  3167. for(i = 0; i < 16; i++){
  3168. if(h->long_ref[i] == NULL) continue;
  3169. if(h->long_ref[i]->reference != 3) continue;
  3170. h->default_ref_list[0][index ]= *h->long_ref[i];
  3171. h->default_ref_list[0][index++].pic_id= i;;
  3172. }
  3173. if(index < h->ref_count[0])
  3174. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3175. }
  3176. }else{ //FIELD
  3177. if(h->slice_type==B_TYPE){
  3178. }else{
  3179. //FIXME second field balh
  3180. }
  3181. }
  3182. #ifdef TRACE
  3183. for (i=0; i<h->ref_count[0]; i++) {
  3184. tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
  3185. }
  3186. if(h->slice_type==B_TYPE){
  3187. for (i=0; i<h->ref_count[1]; i++) {
  3188. tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
  3189. }
  3190. }
  3191. #endif
  3192. return 0;
  3193. }
  3194. static void print_short_term(H264Context *h);
  3195. static void print_long_term(H264Context *h);
  3196. static int decode_ref_pic_list_reordering(H264Context *h){
  3197. MpegEncContext * const s = &h->s;
  3198. int list, index;
  3199. print_short_term(h);
  3200. print_long_term(h);
  3201. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3202. for(list=0; list<h->list_count; list++){
  3203. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3204. if(get_bits1(&s->gb)){
  3205. int pred= h->curr_pic_num;
  3206. for(index=0; ; index++){
  3207. unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3208. unsigned int pic_id;
  3209. int i;
  3210. Picture *ref = NULL;
  3211. if(reordering_of_pic_nums_idc==3)
  3212. break;
  3213. if(index >= h->ref_count[list]){
  3214. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3215. return -1;
  3216. }
  3217. if(reordering_of_pic_nums_idc<3){
  3218. if(reordering_of_pic_nums_idc<2){
  3219. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3220. if(abs_diff_pic_num >= h->max_pic_num){
  3221. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3222. return -1;
  3223. }
  3224. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3225. else pred+= abs_diff_pic_num;
  3226. pred &= h->max_pic_num - 1;
  3227. for(i= h->short_ref_count-1; i>=0; i--){
  3228. ref = h->short_ref[i];
  3229. assert(ref->reference == 3);
  3230. assert(!ref->long_ref);
  3231. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3232. break;
  3233. }
  3234. if(i>=0)
  3235. ref->pic_id= ref->frame_num;
  3236. }else{
  3237. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3238. if(pic_id>31){
  3239. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  3240. return -1;
  3241. }
  3242. ref = h->long_ref[pic_id];
  3243. if(ref){
  3244. ref->pic_id= pic_id;
  3245. assert(ref->reference == 3);
  3246. assert(ref->long_ref);
  3247. i=0;
  3248. }else{
  3249. i=-1;
  3250. }
  3251. }
  3252. if (i < 0) {
  3253. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3254. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3255. } else {
  3256. for(i=index; i+1<h->ref_count[list]; i++){
  3257. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3258. break;
  3259. }
  3260. for(; i > index; i--){
  3261. h->ref_list[list][i]= h->ref_list[list][i-1];
  3262. }
  3263. h->ref_list[list][index]= *ref;
  3264. }
  3265. }else{
  3266. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3267. return -1;
  3268. }
  3269. }
  3270. }
  3271. }
  3272. for(list=0; list<h->list_count; list++){
  3273. for(index= 0; index < h->ref_count[list]; index++){
  3274. if(!h->ref_list[list][index].data[0])
  3275. h->ref_list[list][index]= s->current_picture;
  3276. }
  3277. }
  3278. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3279. direct_dist_scale_factor(h);
  3280. direct_ref_list_init(h);
  3281. return 0;
  3282. }
  3283. static void fill_mbaff_ref_list(H264Context *h){
  3284. int list, i, j;
  3285. for(list=0; list<2; list++){ //FIXME try list_count
  3286. for(i=0; i<h->ref_count[list]; i++){
  3287. Picture *frame = &h->ref_list[list][i];
  3288. Picture *field = &h->ref_list[list][16+2*i];
  3289. field[0] = *frame;
  3290. for(j=0; j<3; j++)
  3291. field[0].linesize[j] <<= 1;
  3292. field[1] = field[0];
  3293. for(j=0; j<3; j++)
  3294. field[1].data[j] += frame->linesize[j];
  3295. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3296. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3297. for(j=0; j<2; j++){
  3298. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3299. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3300. }
  3301. }
  3302. }
  3303. for(j=0; j<h->ref_count[1]; j++){
  3304. for(i=0; i<h->ref_count[0]; i++)
  3305. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3306. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3307. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3308. }
  3309. }
  3310. static int pred_weight_table(H264Context *h){
  3311. MpegEncContext * const s = &h->s;
  3312. int list, i;
  3313. int luma_def, chroma_def;
  3314. h->use_weight= 0;
  3315. h->use_weight_chroma= 0;
  3316. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3317. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3318. luma_def = 1<<h->luma_log2_weight_denom;
  3319. chroma_def = 1<<h->chroma_log2_weight_denom;
  3320. for(list=0; list<2; list++){
  3321. for(i=0; i<h->ref_count[list]; i++){
  3322. int luma_weight_flag, chroma_weight_flag;
  3323. luma_weight_flag= get_bits1(&s->gb);
  3324. if(luma_weight_flag){
  3325. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3326. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3327. if( h->luma_weight[list][i] != luma_def
  3328. || h->luma_offset[list][i] != 0)
  3329. h->use_weight= 1;
  3330. }else{
  3331. h->luma_weight[list][i]= luma_def;
  3332. h->luma_offset[list][i]= 0;
  3333. }
  3334. chroma_weight_flag= get_bits1(&s->gb);
  3335. if(chroma_weight_flag){
  3336. int j;
  3337. for(j=0; j<2; j++){
  3338. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3339. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3340. if( h->chroma_weight[list][i][j] != chroma_def
  3341. || h->chroma_offset[list][i][j] != 0)
  3342. h->use_weight_chroma= 1;
  3343. }
  3344. }else{
  3345. int j;
  3346. for(j=0; j<2; j++){
  3347. h->chroma_weight[list][i][j]= chroma_def;
  3348. h->chroma_offset[list][i][j]= 0;
  3349. }
  3350. }
  3351. }
  3352. if(h->slice_type != B_TYPE) break;
  3353. }
  3354. h->use_weight= h->use_weight || h->use_weight_chroma;
  3355. return 0;
  3356. }
  3357. static void implicit_weight_table(H264Context *h){
  3358. MpegEncContext * const s = &h->s;
  3359. int ref0, ref1;
  3360. int cur_poc = s->current_picture_ptr->poc;
  3361. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3362. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3363. h->use_weight= 0;
  3364. h->use_weight_chroma= 0;
  3365. return;
  3366. }
  3367. h->use_weight= 2;
  3368. h->use_weight_chroma= 2;
  3369. h->luma_log2_weight_denom= 5;
  3370. h->chroma_log2_weight_denom= 5;
  3371. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3372. int poc0 = h->ref_list[0][ref0].poc;
  3373. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3374. int poc1 = h->ref_list[1][ref1].poc;
  3375. int td = av_clip(poc1 - poc0, -128, 127);
  3376. if(td){
  3377. int tb = av_clip(cur_poc - poc0, -128, 127);
  3378. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3379. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3380. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3381. h->implicit_weight[ref0][ref1] = 32;
  3382. else
  3383. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3384. }else
  3385. h->implicit_weight[ref0][ref1] = 32;
  3386. }
  3387. }
  3388. }
  3389. static inline void unreference_pic(H264Context *h, Picture *pic){
  3390. int i;
  3391. pic->reference=0;
  3392. if(pic == h->delayed_output_pic)
  3393. pic->reference=1;
  3394. else{
  3395. for(i = 0; h->delayed_pic[i]; i++)
  3396. if(pic == h->delayed_pic[i]){
  3397. pic->reference=1;
  3398. break;
  3399. }
  3400. }
  3401. }
  3402. /**
  3403. * instantaneous decoder refresh.
  3404. */
  3405. static void idr(H264Context *h){
  3406. int i;
  3407. for(i=0; i<16; i++){
  3408. if (h->long_ref[i] != NULL) {
  3409. unreference_pic(h, h->long_ref[i]);
  3410. h->long_ref[i]= NULL;
  3411. }
  3412. }
  3413. h->long_ref_count=0;
  3414. for(i=0; i<h->short_ref_count; i++){
  3415. unreference_pic(h, h->short_ref[i]);
  3416. h->short_ref[i]= NULL;
  3417. }
  3418. h->short_ref_count=0;
  3419. }
  3420. /* forget old pics after a seek */
  3421. static void flush_dpb(AVCodecContext *avctx){
  3422. H264Context *h= avctx->priv_data;
  3423. int i;
  3424. for(i=0; i<16; i++) {
  3425. if(h->delayed_pic[i])
  3426. h->delayed_pic[i]->reference= 0;
  3427. h->delayed_pic[i]= NULL;
  3428. }
  3429. if(h->delayed_output_pic)
  3430. h->delayed_output_pic->reference= 0;
  3431. h->delayed_output_pic= NULL;
  3432. idr(h);
  3433. if(h->s.current_picture_ptr)
  3434. h->s.current_picture_ptr->reference= 0;
  3435. }
  3436. /**
  3437. *
  3438. * @return the removed picture or NULL if an error occurs
  3439. */
  3440. static Picture * remove_short(H264Context *h, int frame_num){
  3441. MpegEncContext * const s = &h->s;
  3442. int i;
  3443. if(s->avctx->debug&FF_DEBUG_MMCO)
  3444. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3445. for(i=0; i<h->short_ref_count; i++){
  3446. Picture *pic= h->short_ref[i];
  3447. if(s->avctx->debug&FF_DEBUG_MMCO)
  3448. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3449. if(pic->frame_num == frame_num){
  3450. h->short_ref[i]= NULL;
  3451. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3452. h->short_ref_count--;
  3453. return pic;
  3454. }
  3455. }
  3456. return NULL;
  3457. }
  3458. /**
  3459. *
  3460. * @return the removed picture or NULL if an error occurs
  3461. */
  3462. static Picture * remove_long(H264Context *h, int i){
  3463. Picture *pic;
  3464. pic= h->long_ref[i];
  3465. h->long_ref[i]= NULL;
  3466. if(pic) h->long_ref_count--;
  3467. return pic;
  3468. }
  3469. /**
  3470. * print short term list
  3471. */
  3472. static void print_short_term(H264Context *h) {
  3473. uint32_t i;
  3474. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3475. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3476. for(i=0; i<h->short_ref_count; i++){
  3477. Picture *pic= h->short_ref[i];
  3478. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3479. }
  3480. }
  3481. }
  3482. /**
  3483. * print long term list
  3484. */
  3485. static void print_long_term(H264Context *h) {
  3486. uint32_t i;
  3487. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3488. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3489. for(i = 0; i < 16; i++){
  3490. Picture *pic= h->long_ref[i];
  3491. if (pic) {
  3492. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3493. }
  3494. }
  3495. }
  3496. }
  3497. /**
  3498. * Executes the reference picture marking (memory management control operations).
  3499. */
  3500. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3501. MpegEncContext * const s = &h->s;
  3502. int i, j;
  3503. int current_is_long=0;
  3504. Picture *pic;
  3505. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3506. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3507. for(i=0; i<mmco_count; i++){
  3508. if(s->avctx->debug&FF_DEBUG_MMCO)
  3509. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
  3510. switch(mmco[i].opcode){
  3511. case MMCO_SHORT2UNUSED:
  3512. pic= remove_short(h, mmco[i].short_frame_num);
  3513. if(pic)
  3514. unreference_pic(h, pic);
  3515. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3516. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3517. break;
  3518. case MMCO_SHORT2LONG:
  3519. pic= remove_long(h, mmco[i].long_index);
  3520. if(pic) unreference_pic(h, pic);
  3521. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3522. if (h->long_ref[ mmco[i].long_index ]){
  3523. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3524. h->long_ref_count++;
  3525. }
  3526. break;
  3527. case MMCO_LONG2UNUSED:
  3528. pic= remove_long(h, mmco[i].long_index);
  3529. if(pic)
  3530. unreference_pic(h, pic);
  3531. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3532. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3533. break;
  3534. case MMCO_LONG:
  3535. pic= remove_long(h, mmco[i].long_index);
  3536. if(pic) unreference_pic(h, pic);
  3537. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3538. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3539. h->long_ref_count++;
  3540. current_is_long=1;
  3541. break;
  3542. case MMCO_SET_MAX_LONG:
  3543. assert(mmco[i].long_index <= 16);
  3544. // just remove the long term which index is greater than new max
  3545. for(j = mmco[i].long_index; j<16; j++){
  3546. pic = remove_long(h, j);
  3547. if (pic) unreference_pic(h, pic);
  3548. }
  3549. break;
  3550. case MMCO_RESET:
  3551. while(h->short_ref_count){
  3552. pic= remove_short(h, h->short_ref[0]->frame_num);
  3553. if(pic) unreference_pic(h, pic);
  3554. }
  3555. for(j = 0; j < 16; j++) {
  3556. pic= remove_long(h, j);
  3557. if(pic) unreference_pic(h, pic);
  3558. }
  3559. break;
  3560. default: assert(0);
  3561. }
  3562. }
  3563. if(!current_is_long){
  3564. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3565. if(pic){
  3566. unreference_pic(h, pic);
  3567. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3568. }
  3569. if(h->short_ref_count)
  3570. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3571. h->short_ref[0]= s->current_picture_ptr;
  3572. h->short_ref[0]->long_ref=0;
  3573. h->short_ref_count++;
  3574. }
  3575. print_short_term(h);
  3576. print_long_term(h);
  3577. return 0;
  3578. }
  3579. static int decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
  3580. MpegEncContext * const s = &h->s;
  3581. int i;
  3582. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3583. s->broken_link= get_bits1(gb) -1;
  3584. h->mmco[0].long_index= get_bits1(gb) - 1; // current_long_term_idx
  3585. if(h->mmco[0].long_index == -1)
  3586. h->mmco_index= 0;
  3587. else{
  3588. h->mmco[0].opcode= MMCO_LONG;
  3589. h->mmco_index= 1;
  3590. }
  3591. }else{
  3592. if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
  3593. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3594. MMCOOpcode opcode= get_ue_golomb(gb);
  3595. h->mmco[i].opcode= opcode;
  3596. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3597. h->mmco[i].short_frame_num= (h->frame_num - get_ue_golomb(gb) - 1) & ((1<<h->sps.log2_max_frame_num)-1); //FIXME fields
  3598. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3599. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3600. return -1;
  3601. }*/
  3602. }
  3603. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3604. unsigned int long_index= get_ue_golomb(gb);
  3605. if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ long_index >= 16){
  3606. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3607. return -1;
  3608. }
  3609. h->mmco[i].long_index= long_index;
  3610. }
  3611. if(opcode > (unsigned)MMCO_LONG){
  3612. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3613. return -1;
  3614. }
  3615. if(opcode == MMCO_END)
  3616. break;
  3617. }
  3618. h->mmco_index= i;
  3619. }else{
  3620. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3621. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3622. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3623. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3624. h->mmco_index= 1;
  3625. }else
  3626. h->mmco_index= 0;
  3627. }
  3628. }
  3629. return 0;
  3630. }
  3631. static int init_poc(H264Context *h){
  3632. MpegEncContext * const s = &h->s;
  3633. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3634. int field_poc[2];
  3635. if(h->nal_unit_type == NAL_IDR_SLICE){
  3636. h->frame_num_offset= 0;
  3637. }else{
  3638. if(h->frame_num < h->prev_frame_num)
  3639. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3640. else
  3641. h->frame_num_offset= h->prev_frame_num_offset;
  3642. }
  3643. if(h->sps.poc_type==0){
  3644. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3645. if(h->nal_unit_type == NAL_IDR_SLICE){
  3646. h->prev_poc_msb=
  3647. h->prev_poc_lsb= 0;
  3648. }
  3649. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3650. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3651. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3652. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3653. else
  3654. h->poc_msb = h->prev_poc_msb;
  3655. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3656. field_poc[0] =
  3657. field_poc[1] = h->poc_msb + h->poc_lsb;
  3658. if(s->picture_structure == PICT_FRAME)
  3659. field_poc[1] += h->delta_poc_bottom;
  3660. }else if(h->sps.poc_type==1){
  3661. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3662. int i;
  3663. if(h->sps.poc_cycle_length != 0)
  3664. abs_frame_num = h->frame_num_offset + h->frame_num;
  3665. else
  3666. abs_frame_num = 0;
  3667. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3668. abs_frame_num--;
  3669. expected_delta_per_poc_cycle = 0;
  3670. for(i=0; i < h->sps.poc_cycle_length; i++)
  3671. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3672. if(abs_frame_num > 0){
  3673. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3674. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3675. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3676. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3677. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3678. } else
  3679. expectedpoc = 0;
  3680. if(h->nal_ref_idc == 0)
  3681. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3682. field_poc[0] = expectedpoc + h->delta_poc[0];
  3683. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3684. if(s->picture_structure == PICT_FRAME)
  3685. field_poc[1] += h->delta_poc[1];
  3686. }else{
  3687. int poc;
  3688. if(h->nal_unit_type == NAL_IDR_SLICE){
  3689. poc= 0;
  3690. }else{
  3691. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3692. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3693. }
  3694. field_poc[0]= poc;
  3695. field_poc[1]= poc;
  3696. }
  3697. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3698. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3699. if(s->picture_structure != PICT_TOP_FIELD)
  3700. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3701. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3702. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3703. return 0;
  3704. }
  3705. /**
  3706. * initialize scan tables
  3707. */
  3708. static void init_scan_tables(H264Context *h){
  3709. MpegEncContext * const s = &h->s;
  3710. int i;
  3711. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  3712. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  3713. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  3714. }else{
  3715. for(i=0; i<16; i++){
  3716. #define T(x) (x>>2) | ((x<<2) & 0xF)
  3717. h->zigzag_scan[i] = T(zigzag_scan[i]);
  3718. h-> field_scan[i] = T( field_scan[i]);
  3719. #undef T
  3720. }
  3721. }
  3722. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  3723. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  3724. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  3725. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  3726. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  3727. }else{
  3728. for(i=0; i<64; i++){
  3729. #define T(x) (x>>3) | ((x&7)<<3)
  3730. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  3731. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  3732. h->field_scan8x8[i] = T(field_scan8x8[i]);
  3733. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  3734. #undef T
  3735. }
  3736. }
  3737. if(h->sps.transform_bypass){ //FIXME same ugly
  3738. h->zigzag_scan_q0 = zigzag_scan;
  3739. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  3740. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  3741. h->field_scan_q0 = field_scan;
  3742. h->field_scan8x8_q0 = field_scan8x8;
  3743. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  3744. }else{
  3745. h->zigzag_scan_q0 = h->zigzag_scan;
  3746. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  3747. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  3748. h->field_scan_q0 = h->field_scan;
  3749. h->field_scan8x8_q0 = h->field_scan8x8;
  3750. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  3751. }
  3752. }
  3753. /**
  3754. * decodes a slice header.
  3755. * this will allso call MPV_common_init() and frame_start() as needed
  3756. */
  3757. static int decode_slice_header(H264Context *h){
  3758. MpegEncContext * const s = &h->s;
  3759. unsigned int first_mb_in_slice;
  3760. unsigned int pps_id;
  3761. int num_ref_idx_active_override_flag;
  3762. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3763. unsigned int slice_type, tmp;
  3764. int default_ref_list_done = 0;
  3765. s->current_picture.reference= h->nal_ref_idc != 0;
  3766. s->dropable= h->nal_ref_idc == 0;
  3767. first_mb_in_slice= get_ue_golomb(&s->gb);
  3768. if((s->flags2 & CODEC_FLAG2_CHUNKS) && first_mb_in_slice == 0){
  3769. h->slice_num = 0;
  3770. s->current_picture_ptr= NULL;
  3771. }
  3772. slice_type= get_ue_golomb(&s->gb);
  3773. if(slice_type > 9){
  3774. av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
  3775. return -1;
  3776. }
  3777. if(slice_type > 4){
  3778. slice_type -= 5;
  3779. h->slice_type_fixed=1;
  3780. }else
  3781. h->slice_type_fixed=0;
  3782. slice_type= slice_type_map[ slice_type ];
  3783. if (slice_type == I_TYPE
  3784. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3785. default_ref_list_done = 1;
  3786. }
  3787. h->slice_type= slice_type;
  3788. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  3789. pps_id= get_ue_golomb(&s->gb);
  3790. if(pps_id>=MAX_PPS_COUNT){
  3791. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3792. return -1;
  3793. }
  3794. if(!h->pps_buffers[pps_id]) {
  3795. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  3796. return -1;
  3797. }
  3798. h->pps= *h->pps_buffers[pps_id];
  3799. if(!h->sps_buffers[h->pps.sps_id]) {
  3800. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  3801. return -1;
  3802. }
  3803. h->sps = *h->sps_buffers[h->pps.sps_id];
  3804. if(h->dequant_coeff_pps != pps_id){
  3805. h->dequant_coeff_pps = pps_id;
  3806. init_dequant_tables(h);
  3807. }
  3808. s->mb_width= h->sps.mb_width;
  3809. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3810. h->b_stride= s->mb_width*4;
  3811. h->b8_stride= s->mb_width*2;
  3812. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  3813. if(h->sps.frame_mbs_only_flag)
  3814. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  3815. else
  3816. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  3817. if (s->context_initialized
  3818. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3819. free_tables(h);
  3820. MPV_common_end(s);
  3821. }
  3822. if (!s->context_initialized) {
  3823. if (MPV_common_init(s) < 0)
  3824. return -1;
  3825. init_scan_tables(h);
  3826. alloc_tables(h);
  3827. s->avctx->width = s->width;
  3828. s->avctx->height = s->height;
  3829. s->avctx->sample_aspect_ratio= h->sps.sar;
  3830. if(!s->avctx->sample_aspect_ratio.den)
  3831. s->avctx->sample_aspect_ratio.den = 1;
  3832. if(h->sps.timing_info_present_flag){
  3833. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  3834. if(h->x264_build > 0 && h->x264_build < 44)
  3835. s->avctx->time_base.den *= 2;
  3836. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  3837. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  3838. }
  3839. }
  3840. if(h->slice_num == 0){
  3841. if(frame_start(h) < 0)
  3842. return -1;
  3843. }
  3844. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  3845. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3846. h->mb_mbaff = 0;
  3847. h->mb_aff_frame = 0;
  3848. if(h->sps.frame_mbs_only_flag){
  3849. s->picture_structure= PICT_FRAME;
  3850. }else{
  3851. if(get_bits1(&s->gb)) { //field_pic_flag
  3852. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3853. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  3854. } else {
  3855. s->picture_structure= PICT_FRAME;
  3856. h->mb_aff_frame = h->sps.mb_aff;
  3857. }
  3858. }
  3859. assert(s->mb_num == s->mb_width * s->mb_height);
  3860. if(first_mb_in_slice << h->mb_aff_frame >= s->mb_num ||
  3861. first_mb_in_slice >= s->mb_num){
  3862. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  3863. return -1;
  3864. }
  3865. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3866. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  3867. assert(s->mb_y < s->mb_height);
  3868. if(s->picture_structure==PICT_FRAME){
  3869. h->curr_pic_num= h->frame_num;
  3870. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3871. }else{
  3872. h->curr_pic_num= 2*h->frame_num;
  3873. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3874. }
  3875. if(h->nal_unit_type == NAL_IDR_SLICE){
  3876. get_ue_golomb(&s->gb); /* idr_pic_id */
  3877. }
  3878. if(h->sps.poc_type==0){
  3879. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3880. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3881. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3882. }
  3883. }
  3884. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3885. h->delta_poc[0]= get_se_golomb(&s->gb);
  3886. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3887. h->delta_poc[1]= get_se_golomb(&s->gb);
  3888. }
  3889. init_poc(h);
  3890. if(h->pps.redundant_pic_cnt_present){
  3891. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3892. }
  3893. //set defaults, might be overriden a few line later
  3894. h->ref_count[0]= h->pps.ref_count[0];
  3895. h->ref_count[1]= h->pps.ref_count[1];
  3896. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  3897. if(h->slice_type == B_TYPE){
  3898. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3899. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  3900. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  3901. }
  3902. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3903. if(num_ref_idx_active_override_flag){
  3904. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3905. if(h->slice_type==B_TYPE)
  3906. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3907. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  3908. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3909. h->ref_count[0]= h->ref_count[1]= 1;
  3910. return -1;
  3911. }
  3912. }
  3913. if(h->slice_type == B_TYPE)
  3914. h->list_count= 2;
  3915. else
  3916. h->list_count= 1;
  3917. }else
  3918. h->list_count= 0;
  3919. if(!default_ref_list_done){
  3920. fill_default_ref_list(h);
  3921. }
  3922. if(decode_ref_pic_list_reordering(h) < 0)
  3923. return -1;
  3924. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  3925. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  3926. pred_weight_table(h);
  3927. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  3928. implicit_weight_table(h);
  3929. else
  3930. h->use_weight = 0;
  3931. if(s->current_picture.reference)
  3932. decode_ref_pic_marking(h, &s->gb);
  3933. if(FRAME_MBAFF)
  3934. fill_mbaff_ref_list(h);
  3935. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ){
  3936. tmp = get_ue_golomb(&s->gb);
  3937. if(tmp > 2){
  3938. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3939. return -1;
  3940. }
  3941. h->cabac_init_idc= tmp;
  3942. }
  3943. h->last_qscale_diff = 0;
  3944. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  3945. if(tmp>51){
  3946. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3947. return -1;
  3948. }
  3949. s->qscale= tmp;
  3950. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  3951. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  3952. //FIXME qscale / qp ... stuff
  3953. if(h->slice_type == SP_TYPE){
  3954. get_bits1(&s->gb); /* sp_for_switch_flag */
  3955. }
  3956. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  3957. get_se_golomb(&s->gb); /* slice_qs_delta */
  3958. }
  3959. h->deblocking_filter = 1;
  3960. h->slice_alpha_c0_offset = 0;
  3961. h->slice_beta_offset = 0;
  3962. if( h->pps.deblocking_filter_parameters_present ) {
  3963. tmp= get_ue_golomb(&s->gb);
  3964. if(tmp > 2){
  3965. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  3966. return -1;
  3967. }
  3968. h->deblocking_filter= tmp;
  3969. if(h->deblocking_filter < 2)
  3970. h->deblocking_filter^= 1; // 1<->0
  3971. if( h->deblocking_filter ) {
  3972. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3973. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3974. }
  3975. }
  3976. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  3977. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  3978. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  3979. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  3980. h->deblocking_filter= 0;
  3981. #if 0 //FMO
  3982. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3983. slice_group_change_cycle= get_bits(&s->gb, ?);
  3984. #endif
  3985. h->slice_num++;
  3986. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  3987. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  3988. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3989. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
  3990. h->slice_num,
  3991. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3992. first_mb_in_slice,
  3993. av_get_pict_type_char(h->slice_type),
  3994. pps_id, h->frame_num,
  3995. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3996. h->ref_count[0], h->ref_count[1],
  3997. s->qscale,
  3998. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3999. h->use_weight,
  4000. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4001. );
  4002. }
  4003. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4004. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4005. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4006. }else{
  4007. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4008. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4009. }
  4010. return 0;
  4011. }
  4012. /**
  4013. *
  4014. */
  4015. static inline int get_level_prefix(GetBitContext *gb){
  4016. unsigned int buf;
  4017. int log;
  4018. OPEN_READER(re, gb);
  4019. UPDATE_CACHE(re, gb);
  4020. buf=GET_CACHE(re, gb);
  4021. log= 32 - av_log2(buf);
  4022. #ifdef TRACE
  4023. print_bin(buf>>(32-log), log);
  4024. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  4025. #endif
  4026. LAST_SKIP_BITS(re, gb, log);
  4027. CLOSE_READER(re, gb);
  4028. return log-1;
  4029. }
  4030. static inline int get_dct8x8_allowed(H264Context *h){
  4031. int i;
  4032. for(i=0; i<4; i++){
  4033. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4034. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4035. return 0;
  4036. }
  4037. return 1;
  4038. }
  4039. /**
  4040. * decodes a residual block.
  4041. * @param n block index
  4042. * @param scantable scantable
  4043. * @param max_coeff number of coefficients in the block
  4044. * @return <0 if an error occured
  4045. */
  4046. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4047. MpegEncContext * const s = &h->s;
  4048. static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  4049. int level[16];
  4050. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4051. //FIXME put trailing_onex into the context
  4052. if(n == CHROMA_DC_BLOCK_INDEX){
  4053. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4054. total_coeff= coeff_token>>2;
  4055. }else{
  4056. if(n == LUMA_DC_BLOCK_INDEX){
  4057. total_coeff= pred_non_zero_count(h, 0);
  4058. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4059. total_coeff= coeff_token>>2;
  4060. }else{
  4061. total_coeff= pred_non_zero_count(h, n);
  4062. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4063. total_coeff= coeff_token>>2;
  4064. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4065. }
  4066. }
  4067. //FIXME set last_non_zero?
  4068. if(total_coeff==0)
  4069. return 0;
  4070. if(total_coeff > (unsigned)max_coeff) {
  4071. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  4072. return -1;
  4073. }
  4074. trailing_ones= coeff_token&3;
  4075. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4076. assert(total_coeff<=16);
  4077. for(i=0; i<trailing_ones; i++){
  4078. level[i]= 1 - 2*get_bits1(gb);
  4079. }
  4080. if(i<total_coeff) {
  4081. int level_code, mask;
  4082. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4083. int prefix= get_level_prefix(gb);
  4084. //first coefficient has suffix_length equal to 0 or 1
  4085. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4086. if(suffix_length)
  4087. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4088. else
  4089. level_code= (prefix<<suffix_length); //part
  4090. }else if(prefix==14){
  4091. if(suffix_length)
  4092. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4093. else
  4094. level_code= prefix + get_bits(gb, 4); //part
  4095. }else if(prefix==15){
  4096. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4097. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4098. }else{
  4099. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4100. return -1;
  4101. }
  4102. if(trailing_ones < 3) level_code += 2;
  4103. suffix_length = 1;
  4104. if(level_code > 5)
  4105. suffix_length++;
  4106. mask= -(level_code&1);
  4107. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4108. i++;
  4109. //remaining coefficients have suffix_length > 0
  4110. for(;i<total_coeff;i++) {
  4111. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4112. prefix = get_level_prefix(gb);
  4113. if(prefix<15){
  4114. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4115. }else if(prefix==15){
  4116. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4117. }else{
  4118. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4119. return -1;
  4120. }
  4121. mask= -(level_code&1);
  4122. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4123. if(level_code > suffix_limit[suffix_length])
  4124. suffix_length++;
  4125. }
  4126. }
  4127. if(total_coeff == max_coeff)
  4128. zeros_left=0;
  4129. else{
  4130. if(n == CHROMA_DC_BLOCK_INDEX)
  4131. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4132. else
  4133. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4134. }
  4135. coeff_num = zeros_left + total_coeff - 1;
  4136. j = scantable[coeff_num];
  4137. if(n > 24){
  4138. block[j] = level[0];
  4139. for(i=1;i<total_coeff;i++) {
  4140. if(zeros_left <= 0)
  4141. run_before = 0;
  4142. else if(zeros_left < 7){
  4143. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4144. }else{
  4145. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4146. }
  4147. zeros_left -= run_before;
  4148. coeff_num -= 1 + run_before;
  4149. j= scantable[ coeff_num ];
  4150. block[j]= level[i];
  4151. }
  4152. }else{
  4153. block[j] = (level[0] * qmul[j] + 32)>>6;
  4154. for(i=1;i<total_coeff;i++) {
  4155. if(zeros_left <= 0)
  4156. run_before = 0;
  4157. else if(zeros_left < 7){
  4158. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4159. }else{
  4160. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4161. }
  4162. zeros_left -= run_before;
  4163. coeff_num -= 1 + run_before;
  4164. j= scantable[ coeff_num ];
  4165. block[j]= (level[i] * qmul[j] + 32)>>6;
  4166. }
  4167. }
  4168. if(zeros_left<0){
  4169. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4170. return -1;
  4171. }
  4172. return 0;
  4173. }
  4174. static void predict_field_decoding_flag(H264Context *h){
  4175. MpegEncContext * const s = &h->s;
  4176. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4177. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4178. ? s->current_picture.mb_type[mb_xy-1]
  4179. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4180. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4181. : 0;
  4182. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4183. }
  4184. /**
  4185. * decodes a P_SKIP or B_SKIP macroblock
  4186. */
  4187. static void decode_mb_skip(H264Context *h){
  4188. MpegEncContext * const s = &h->s;
  4189. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4190. int mb_type=0;
  4191. memset(h->non_zero_count[mb_xy], 0, 16);
  4192. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4193. if(MB_FIELD)
  4194. mb_type|= MB_TYPE_INTERLACED;
  4195. if( h->slice_type == B_TYPE )
  4196. {
  4197. // just for fill_caches. pred_direct_motion will set the real mb_type
  4198. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4199. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4200. pred_direct_motion(h, &mb_type);
  4201. mb_type|= MB_TYPE_SKIP;
  4202. }
  4203. else
  4204. {
  4205. int mx, my;
  4206. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4207. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4208. pred_pskip_motion(h, &mx, &my);
  4209. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4210. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4211. }
  4212. write_back_motion(h, mb_type);
  4213. s->current_picture.mb_type[mb_xy]= mb_type;
  4214. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4215. h->slice_table[ mb_xy ]= h->slice_num;
  4216. h->prev_mb_skipped= 1;
  4217. }
  4218. /**
  4219. * decodes a macroblock
  4220. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4221. */
  4222. static int decode_mb_cavlc(H264Context *h){
  4223. MpegEncContext * const s = &h->s;
  4224. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4225. int partition_count;
  4226. unsigned int mb_type, cbp;
  4227. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4228. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4229. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4230. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4231. down the code */
  4232. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4233. if(s->mb_skip_run==-1)
  4234. s->mb_skip_run= get_ue_golomb(&s->gb);
  4235. if (s->mb_skip_run--) {
  4236. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4237. if(s->mb_skip_run==0)
  4238. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4239. else
  4240. predict_field_decoding_flag(h);
  4241. }
  4242. decode_mb_skip(h);
  4243. return 0;
  4244. }
  4245. }
  4246. if(FRAME_MBAFF){
  4247. if( (s->mb_y&1) == 0 )
  4248. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4249. }else
  4250. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4251. h->prev_mb_skipped= 0;
  4252. mb_type= get_ue_golomb(&s->gb);
  4253. if(h->slice_type == B_TYPE){
  4254. if(mb_type < 23){
  4255. partition_count= b_mb_type_info[mb_type].partition_count;
  4256. mb_type= b_mb_type_info[mb_type].type;
  4257. }else{
  4258. mb_type -= 23;
  4259. goto decode_intra_mb;
  4260. }
  4261. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4262. if(mb_type < 5){
  4263. partition_count= p_mb_type_info[mb_type].partition_count;
  4264. mb_type= p_mb_type_info[mb_type].type;
  4265. }else{
  4266. mb_type -= 5;
  4267. goto decode_intra_mb;
  4268. }
  4269. }else{
  4270. assert(h->slice_type == I_TYPE);
  4271. decode_intra_mb:
  4272. if(mb_type > 25){
  4273. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  4274. return -1;
  4275. }
  4276. partition_count=0;
  4277. cbp= i_mb_type_info[mb_type].cbp;
  4278. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4279. mb_type= i_mb_type_info[mb_type].type;
  4280. }
  4281. if(MB_FIELD)
  4282. mb_type |= MB_TYPE_INTERLACED;
  4283. h->slice_table[ mb_xy ]= h->slice_num;
  4284. if(IS_INTRA_PCM(mb_type)){
  4285. unsigned int x, y;
  4286. // We assume these blocks are very rare so we do not optimize it.
  4287. align_get_bits(&s->gb);
  4288. // The pixels are stored in the same order as levels in h->mb array.
  4289. for(y=0; y<16; y++){
  4290. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4291. for(x=0; x<16; x++){
  4292. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4293. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4294. }
  4295. }
  4296. for(y=0; y<8; y++){
  4297. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4298. for(x=0; x<8; x++){
  4299. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4300. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4301. }
  4302. }
  4303. for(y=0; y<8; y++){
  4304. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4305. for(x=0; x<8; x++){
  4306. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4307. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4308. }
  4309. }
  4310. // In deblocking, the quantizer is 0
  4311. s->current_picture.qscale_table[mb_xy]= 0;
  4312. h->chroma_qp[0] = get_chroma_qp(h, 0, 0);
  4313. h->chroma_qp[1] = get_chroma_qp(h, 1, 0);
  4314. // All coeffs are present
  4315. memset(h->non_zero_count[mb_xy], 16, 16);
  4316. s->current_picture.mb_type[mb_xy]= mb_type;
  4317. return 0;
  4318. }
  4319. if(MB_MBAFF){
  4320. h->ref_count[0] <<= 1;
  4321. h->ref_count[1] <<= 1;
  4322. }
  4323. fill_caches(h, mb_type, 0);
  4324. //mb_pred
  4325. if(IS_INTRA(mb_type)){
  4326. int pred_mode;
  4327. // init_top_left_availability(h);
  4328. if(IS_INTRA4x4(mb_type)){
  4329. int i;
  4330. int di = 1;
  4331. if(dct8x8_allowed && get_bits1(&s->gb)){
  4332. mb_type |= MB_TYPE_8x8DCT;
  4333. di = 4;
  4334. }
  4335. // fill_intra4x4_pred_table(h);
  4336. for(i=0; i<16; i+=di){
  4337. int mode= pred_intra_mode(h, i);
  4338. if(!get_bits1(&s->gb)){
  4339. const int rem_mode= get_bits(&s->gb, 3);
  4340. mode = rem_mode + (rem_mode >= mode);
  4341. }
  4342. if(di==4)
  4343. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4344. else
  4345. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4346. }
  4347. write_back_intra_pred_mode(h);
  4348. if( check_intra4x4_pred_mode(h) < 0)
  4349. return -1;
  4350. }else{
  4351. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4352. if(h->intra16x16_pred_mode < 0)
  4353. return -1;
  4354. }
  4355. pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
  4356. if(pred_mode < 0)
  4357. return -1;
  4358. h->chroma_pred_mode= pred_mode;
  4359. }else if(partition_count==4){
  4360. int i, j, sub_partition_count[4], list, ref[2][4];
  4361. if(h->slice_type == B_TYPE){
  4362. for(i=0; i<4; i++){
  4363. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4364. if(h->sub_mb_type[i] >=13){
  4365. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4366. return -1;
  4367. }
  4368. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4369. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4370. }
  4371. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4372. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4373. pred_direct_motion(h, &mb_type);
  4374. h->ref_cache[0][scan8[4]] =
  4375. h->ref_cache[1][scan8[4]] =
  4376. h->ref_cache[0][scan8[12]] =
  4377. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4378. }
  4379. }else{
  4380. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4381. for(i=0; i<4; i++){
  4382. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4383. if(h->sub_mb_type[i] >=4){
  4384. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4385. return -1;
  4386. }
  4387. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4388. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4389. }
  4390. }
  4391. for(list=0; list<h->list_count; list++){
  4392. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4393. for(i=0; i<4; i++){
  4394. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4395. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4396. unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4397. if(tmp>=ref_count){
  4398. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  4399. return -1;
  4400. }
  4401. ref[list][i]= tmp;
  4402. }else{
  4403. //FIXME
  4404. ref[list][i] = -1;
  4405. }
  4406. }
  4407. }
  4408. if(dct8x8_allowed)
  4409. dct8x8_allowed = get_dct8x8_allowed(h);
  4410. for(list=0; list<h->list_count; list++){
  4411. for(i=0; i<4; i++){
  4412. if(IS_DIRECT(h->sub_mb_type[i])) {
  4413. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4414. continue;
  4415. }
  4416. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4417. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4418. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4419. const int sub_mb_type= h->sub_mb_type[i];
  4420. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4421. for(j=0; j<sub_partition_count[i]; j++){
  4422. int mx, my;
  4423. const int index= 4*i + block_width*j;
  4424. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4425. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4426. mx += get_se_golomb(&s->gb);
  4427. my += get_se_golomb(&s->gb);
  4428. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4429. if(IS_SUB_8X8(sub_mb_type)){
  4430. mv_cache[ 1 ][0]=
  4431. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4432. mv_cache[ 1 ][1]=
  4433. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4434. }else if(IS_SUB_8X4(sub_mb_type)){
  4435. mv_cache[ 1 ][0]= mx;
  4436. mv_cache[ 1 ][1]= my;
  4437. }else if(IS_SUB_4X8(sub_mb_type)){
  4438. mv_cache[ 8 ][0]= mx;
  4439. mv_cache[ 8 ][1]= my;
  4440. }
  4441. mv_cache[ 0 ][0]= mx;
  4442. mv_cache[ 0 ][1]= my;
  4443. }
  4444. }else{
  4445. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4446. p[0] = p[1]=
  4447. p[8] = p[9]= 0;
  4448. }
  4449. }
  4450. }
  4451. }else if(IS_DIRECT(mb_type)){
  4452. pred_direct_motion(h, &mb_type);
  4453. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4454. }else{
  4455. int list, mx, my, i;
  4456. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4457. if(IS_16X16(mb_type)){
  4458. for(list=0; list<h->list_count; list++){
  4459. unsigned int val;
  4460. if(IS_DIR(mb_type, 0, list)){
  4461. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4462. if(val >= h->ref_count[list]){
  4463. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4464. return -1;
  4465. }
  4466. }else
  4467. val= LIST_NOT_USED&0xFF;
  4468. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4469. }
  4470. for(list=0; list<h->list_count; list++){
  4471. unsigned int val;
  4472. if(IS_DIR(mb_type, 0, list)){
  4473. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4474. mx += get_se_golomb(&s->gb);
  4475. my += get_se_golomb(&s->gb);
  4476. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4477. val= pack16to32(mx,my);
  4478. }else
  4479. val=0;
  4480. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  4481. }
  4482. }
  4483. else if(IS_16X8(mb_type)){
  4484. for(list=0; list<h->list_count; list++){
  4485. for(i=0; i<2; i++){
  4486. unsigned int val;
  4487. if(IS_DIR(mb_type, i, list)){
  4488. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4489. if(val >= h->ref_count[list]){
  4490. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4491. return -1;
  4492. }
  4493. }else
  4494. val= LIST_NOT_USED&0xFF;
  4495. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4496. }
  4497. }
  4498. for(list=0; list<h->list_count; list++){
  4499. for(i=0; i<2; i++){
  4500. unsigned int val;
  4501. if(IS_DIR(mb_type, i, list)){
  4502. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4503. mx += get_se_golomb(&s->gb);
  4504. my += get_se_golomb(&s->gb);
  4505. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4506. val= pack16to32(mx,my);
  4507. }else
  4508. val=0;
  4509. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  4510. }
  4511. }
  4512. }else{
  4513. assert(IS_8X16(mb_type));
  4514. for(list=0; list<h->list_count; list++){
  4515. for(i=0; i<2; i++){
  4516. unsigned int val;
  4517. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4518. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4519. if(val >= h->ref_count[list]){
  4520. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4521. return -1;
  4522. }
  4523. }else
  4524. val= LIST_NOT_USED&0xFF;
  4525. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4526. }
  4527. }
  4528. for(list=0; list<h->list_count; list++){
  4529. for(i=0; i<2; i++){
  4530. unsigned int val;
  4531. if(IS_DIR(mb_type, i, list)){
  4532. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4533. mx += get_se_golomb(&s->gb);
  4534. my += get_se_golomb(&s->gb);
  4535. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4536. val= pack16to32(mx,my);
  4537. }else
  4538. val=0;
  4539. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  4540. }
  4541. }
  4542. }
  4543. }
  4544. if(IS_INTER(mb_type))
  4545. write_back_motion(h, mb_type);
  4546. if(!IS_INTRA16x16(mb_type)){
  4547. cbp= get_ue_golomb(&s->gb);
  4548. if(cbp > 47){
  4549. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4550. return -1;
  4551. }
  4552. if(IS_INTRA4x4(mb_type))
  4553. cbp= golomb_to_intra4x4_cbp[cbp];
  4554. else
  4555. cbp= golomb_to_inter_cbp[cbp];
  4556. }
  4557. h->cbp = cbp;
  4558. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4559. if(get_bits1(&s->gb))
  4560. mb_type |= MB_TYPE_8x8DCT;
  4561. }
  4562. s->current_picture.mb_type[mb_xy]= mb_type;
  4563. if(cbp || IS_INTRA16x16(mb_type)){
  4564. int i8x8, i4x4, chroma_idx;
  4565. int dquant;
  4566. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4567. const uint8_t *scan, *scan8x8, *dc_scan;
  4568. // fill_non_zero_count_cache(h);
  4569. if(IS_INTERLACED(mb_type)){
  4570. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4571. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4572. dc_scan= luma_dc_field_scan;
  4573. }else{
  4574. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4575. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4576. dc_scan= luma_dc_zigzag_scan;
  4577. }
  4578. dquant= get_se_golomb(&s->gb);
  4579. if( dquant > 25 || dquant < -26 ){
  4580. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4581. return -1;
  4582. }
  4583. s->qscale += dquant;
  4584. if(((unsigned)s->qscale) > 51){
  4585. if(s->qscale<0) s->qscale+= 52;
  4586. else s->qscale-= 52;
  4587. }
  4588. h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
  4589. h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
  4590. if(IS_INTRA16x16(mb_type)){
  4591. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4592. return -1; //FIXME continue if partitioned and other return -1 too
  4593. }
  4594. assert((cbp&15) == 0 || (cbp&15) == 15);
  4595. if(cbp&15){
  4596. for(i8x8=0; i8x8<4; i8x8++){
  4597. for(i4x4=0; i4x4<4; i4x4++){
  4598. const int index= i4x4 + 4*i8x8;
  4599. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4600. return -1;
  4601. }
  4602. }
  4603. }
  4604. }else{
  4605. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4606. }
  4607. }else{
  4608. for(i8x8=0; i8x8<4; i8x8++){
  4609. if(cbp & (1<<i8x8)){
  4610. if(IS_8x8DCT(mb_type)){
  4611. DCTELEM *buf = &h->mb[64*i8x8];
  4612. uint8_t *nnz;
  4613. for(i4x4=0; i4x4<4; i4x4++){
  4614. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4615. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4616. return -1;
  4617. }
  4618. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4619. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4620. }else{
  4621. for(i4x4=0; i4x4<4; i4x4++){
  4622. const int index= i4x4 + 4*i8x8;
  4623. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4624. return -1;
  4625. }
  4626. }
  4627. }
  4628. }else{
  4629. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4630. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4631. }
  4632. }
  4633. }
  4634. if(cbp&0x30){
  4635. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4636. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4637. return -1;
  4638. }
  4639. }
  4640. if(cbp&0x20){
  4641. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4642. const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
  4643. for(i4x4=0; i4x4<4; i4x4++){
  4644. const int index= 16 + 4*chroma_idx + i4x4;
  4645. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
  4646. return -1;
  4647. }
  4648. }
  4649. }
  4650. }else{
  4651. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4652. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4653. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4654. }
  4655. }else{
  4656. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4657. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4658. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4659. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4660. }
  4661. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4662. write_back_non_zero_count(h);
  4663. if(MB_MBAFF){
  4664. h->ref_count[0] >>= 1;
  4665. h->ref_count[1] >>= 1;
  4666. }
  4667. return 0;
  4668. }
  4669. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4670. MpegEncContext * const s = &h->s;
  4671. const int mb_x = s->mb_x;
  4672. const int mb_y = s->mb_y & ~1;
  4673. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4674. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4675. unsigned int ctx = 0;
  4676. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4677. ctx += 1;
  4678. }
  4679. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4680. ctx += 1;
  4681. }
  4682. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4683. }
  4684. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4685. uint8_t *state= &h->cabac_state[ctx_base];
  4686. int mb_type;
  4687. if(intra_slice){
  4688. MpegEncContext * const s = &h->s;
  4689. const int mba_xy = h->left_mb_xy[0];
  4690. const int mbb_xy = h->top_mb_xy;
  4691. int ctx=0;
  4692. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4693. ctx++;
  4694. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4695. ctx++;
  4696. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4697. return 0; /* I4x4 */
  4698. state += 2;
  4699. }else{
  4700. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4701. return 0; /* I4x4 */
  4702. }
  4703. if( get_cabac_terminate( &h->cabac ) )
  4704. return 25; /* PCM */
  4705. mb_type = 1; /* I16x16 */
  4706. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4707. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4708. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4709. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4710. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4711. return mb_type;
  4712. }
  4713. static int decode_cabac_mb_type( H264Context *h ) {
  4714. MpegEncContext * const s = &h->s;
  4715. if( h->slice_type == I_TYPE ) {
  4716. return decode_cabac_intra_mb_type(h, 3, 1);
  4717. } else if( h->slice_type == P_TYPE ) {
  4718. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4719. /* P-type */
  4720. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4721. /* P_L0_D16x16, P_8x8 */
  4722. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4723. } else {
  4724. /* P_L0_D8x16, P_L0_D16x8 */
  4725. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4726. }
  4727. } else {
  4728. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4729. }
  4730. } else if( h->slice_type == B_TYPE ) {
  4731. const int mba_xy = h->left_mb_xy[0];
  4732. const int mbb_xy = h->top_mb_xy;
  4733. int ctx = 0;
  4734. int bits;
  4735. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4736. ctx++;
  4737. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4738. ctx++;
  4739. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4740. return 0; /* B_Direct_16x16 */
  4741. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4742. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4743. }
  4744. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4745. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4746. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4747. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4748. if( bits < 8 )
  4749. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4750. else if( bits == 13 ) {
  4751. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4752. } else if( bits == 14 )
  4753. return 11; /* B_L1_L0_8x16 */
  4754. else if( bits == 15 )
  4755. return 22; /* B_8x8 */
  4756. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4757. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4758. } else {
  4759. /* TODO SI/SP frames? */
  4760. return -1;
  4761. }
  4762. }
  4763. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4764. MpegEncContext * const s = &h->s;
  4765. int mba_xy, mbb_xy;
  4766. int ctx = 0;
  4767. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4768. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4769. mba_xy = mb_xy - 1;
  4770. if( (mb_y&1)
  4771. && h->slice_table[mba_xy] == h->slice_num
  4772. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  4773. mba_xy += s->mb_stride;
  4774. if( MB_FIELD ){
  4775. mbb_xy = mb_xy - s->mb_stride;
  4776. if( !(mb_y&1)
  4777. && h->slice_table[mbb_xy] == h->slice_num
  4778. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  4779. mbb_xy -= s->mb_stride;
  4780. }else
  4781. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  4782. }else{
  4783. int mb_xy = mb_x + mb_y*s->mb_stride;
  4784. mba_xy = mb_xy - 1;
  4785. mbb_xy = mb_xy - s->mb_stride;
  4786. }
  4787. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4788. ctx++;
  4789. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4790. ctx++;
  4791. if( h->slice_type == B_TYPE )
  4792. ctx += 13;
  4793. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  4794. }
  4795. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4796. int mode = 0;
  4797. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4798. return pred_mode;
  4799. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4800. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4801. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4802. if( mode >= pred_mode )
  4803. return mode + 1;
  4804. else
  4805. return mode;
  4806. }
  4807. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4808. const int mba_xy = h->left_mb_xy[0];
  4809. const int mbb_xy = h->top_mb_xy;
  4810. int ctx = 0;
  4811. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4812. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4813. ctx++;
  4814. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4815. ctx++;
  4816. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4817. return 0;
  4818. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4819. return 1;
  4820. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4821. return 2;
  4822. else
  4823. return 3;
  4824. }
  4825. static const uint8_t block_idx_x[16] = {
  4826. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  4827. };
  4828. static const uint8_t block_idx_y[16] = {
  4829. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  4830. };
  4831. static const uint8_t block_idx_xy[4][4] = {
  4832. { 0, 2, 8, 10},
  4833. { 1, 3, 9, 11},
  4834. { 4, 6, 12, 14},
  4835. { 5, 7, 13, 15}
  4836. };
  4837. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4838. int cbp = 0;
  4839. int cbp_b = -1;
  4840. int i8x8;
  4841. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  4842. cbp_b = h->top_cbp;
  4843. tprintf(h->s.avctx, "cbp_b = top_cbp = %x\n", cbp_b);
  4844. }
  4845. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4846. int cbp_a = -1;
  4847. int x, y;
  4848. int ctx = 0;
  4849. x = block_idx_x[4*i8x8];
  4850. y = block_idx_y[4*i8x8];
  4851. if( x > 0 )
  4852. cbp_a = cbp;
  4853. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  4854. cbp_a = h->left_cbp;
  4855. tprintf(h->s.avctx, "cbp_a = left_cbp = %x\n", cbp_a);
  4856. }
  4857. if( y > 0 )
  4858. cbp_b = cbp;
  4859. /* No need to test for skip as we put 0 for skip block */
  4860. /* No need to test for IPCM as we put 1 for IPCM block */
  4861. if( cbp_a >= 0 ) {
  4862. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  4863. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  4864. ctx++;
  4865. }
  4866. if( cbp_b >= 0 ) {
  4867. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  4868. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  4869. ctx += 2;
  4870. }
  4871. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  4872. cbp |= 1 << i8x8;
  4873. }
  4874. }
  4875. return cbp;
  4876. }
  4877. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4878. int ctx;
  4879. int cbp_a, cbp_b;
  4880. cbp_a = (h->left_cbp>>4)&0x03;
  4881. cbp_b = (h-> top_cbp>>4)&0x03;
  4882. ctx = 0;
  4883. if( cbp_a > 0 ) ctx++;
  4884. if( cbp_b > 0 ) ctx += 2;
  4885. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4886. return 0;
  4887. ctx = 4;
  4888. if( cbp_a == 2 ) ctx++;
  4889. if( cbp_b == 2 ) ctx += 2;
  4890. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  4891. }
  4892. static int decode_cabac_mb_dqp( H264Context *h) {
  4893. MpegEncContext * const s = &h->s;
  4894. int mbn_xy;
  4895. int ctx = 0;
  4896. int val = 0;
  4897. if( s->mb_x > 0 )
  4898. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  4899. else
  4900. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  4901. if( h->last_qscale_diff != 0 )
  4902. ctx++;
  4903. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4904. if( ctx < 2 )
  4905. ctx = 2;
  4906. else
  4907. ctx = 3;
  4908. val++;
  4909. if(val > 102) //prevent infinite loop
  4910. return INT_MIN;
  4911. }
  4912. if( val&0x01 )
  4913. return (val + 1)/2;
  4914. else
  4915. return -(val + 1)/2;
  4916. }
  4917. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4918. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4919. return 0; /* 8x8 */
  4920. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4921. return 1; /* 8x4 */
  4922. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4923. return 2; /* 4x8 */
  4924. return 3; /* 4x4 */
  4925. }
  4926. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4927. int type;
  4928. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4929. return 0; /* B_Direct_8x8 */
  4930. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4931. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4932. type = 3;
  4933. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4934. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4935. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4936. type += 4;
  4937. }
  4938. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4939. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4940. return type;
  4941. }
  4942. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4943. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4944. }
  4945. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4946. int refa = h->ref_cache[list][scan8[n] - 1];
  4947. int refb = h->ref_cache[list][scan8[n] - 8];
  4948. int ref = 0;
  4949. int ctx = 0;
  4950. if( h->slice_type == B_TYPE) {
  4951. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4952. ctx++;
  4953. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4954. ctx += 2;
  4955. } else {
  4956. if( refa > 0 )
  4957. ctx++;
  4958. if( refb > 0 )
  4959. ctx += 2;
  4960. }
  4961. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4962. ref++;
  4963. if( ctx < 4 )
  4964. ctx = 4;
  4965. else
  4966. ctx = 5;
  4967. if(ref >= 32 /*h->ref_list[list]*/){
  4968. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
  4969. return 0; //FIXME we should return -1 and check the return everywhere
  4970. }
  4971. }
  4972. return ref;
  4973. }
  4974. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4975. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4976. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4977. int ctxbase = (l == 0) ? 40 : 47;
  4978. int ctx, mvd;
  4979. if( amvd < 3 )
  4980. ctx = 0;
  4981. else if( amvd > 32 )
  4982. ctx = 2;
  4983. else
  4984. ctx = 1;
  4985. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4986. return 0;
  4987. mvd= 1;
  4988. ctx= 3;
  4989. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4990. mvd++;
  4991. if( ctx < 6 )
  4992. ctx++;
  4993. }
  4994. if( mvd >= 9 ) {
  4995. int k = 3;
  4996. while( get_cabac_bypass( &h->cabac ) ) {
  4997. mvd += 1 << k;
  4998. k++;
  4999. if(k>24){
  5000. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  5001. return INT_MIN;
  5002. }
  5003. }
  5004. while( k-- ) {
  5005. if( get_cabac_bypass( &h->cabac ) )
  5006. mvd += 1 << k;
  5007. }
  5008. }
  5009. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5010. }
  5011. static inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5012. int nza, nzb;
  5013. int ctx = 0;
  5014. if( cat == 0 ) {
  5015. nza = h->left_cbp&0x100;
  5016. nzb = h-> top_cbp&0x100;
  5017. } else if( cat == 1 || cat == 2 ) {
  5018. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5019. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5020. } else if( cat == 3 ) {
  5021. nza = (h->left_cbp>>(6+idx))&0x01;
  5022. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5023. } else {
  5024. assert(cat == 4);
  5025. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5026. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5027. }
  5028. if( nza > 0 )
  5029. ctx++;
  5030. if( nzb > 0 )
  5031. ctx += 2;
  5032. return ctx + 4 * cat;
  5033. }
  5034. static const attribute_used uint8_t last_coeff_flag_offset_8x8[63] = {
  5035. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5036. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5037. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5038. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5039. };
  5040. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5041. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5042. static const int significant_coeff_flag_offset[2][6] = {
  5043. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5044. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5045. };
  5046. static const int last_coeff_flag_offset[2][6] = {
  5047. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5048. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5049. };
  5050. static const int coeff_abs_level_m1_offset[6] = {
  5051. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5052. };
  5053. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5054. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5055. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5056. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5057. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5058. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5059. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5060. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5061. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5062. };
  5063. int index[64];
  5064. int av_unused last;
  5065. int coeff_count = 0;
  5066. int abslevel1 = 1;
  5067. int abslevelgt1 = 0;
  5068. uint8_t *significant_coeff_ctx_base;
  5069. uint8_t *last_coeff_ctx_base;
  5070. uint8_t *abs_level_m1_ctx_base;
  5071. #ifndef ARCH_X86
  5072. #define CABAC_ON_STACK
  5073. #endif
  5074. #ifdef CABAC_ON_STACK
  5075. #define CC &cc
  5076. CABACContext cc;
  5077. cc.range = h->cabac.range;
  5078. cc.low = h->cabac.low;
  5079. cc.bytestream= h->cabac.bytestream;
  5080. #else
  5081. #define CC &h->cabac
  5082. #endif
  5083. /* cat: 0-> DC 16x16 n = 0
  5084. * 1-> AC 16x16 n = luma4x4idx
  5085. * 2-> Luma4x4 n = luma4x4idx
  5086. * 3-> DC Chroma n = iCbCr
  5087. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5088. * 5-> Luma8x8 n = 4 * luma8x8idx
  5089. */
  5090. /* read coded block flag */
  5091. if( cat != 5 ) {
  5092. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5093. if( cat == 1 || cat == 2 )
  5094. h->non_zero_count_cache[scan8[n]] = 0;
  5095. else if( cat == 4 )
  5096. h->non_zero_count_cache[scan8[16+n]] = 0;
  5097. #ifdef CABAC_ON_STACK
  5098. h->cabac.range = cc.range ;
  5099. h->cabac.low = cc.low ;
  5100. h->cabac.bytestream= cc.bytestream;
  5101. #endif
  5102. return 0;
  5103. }
  5104. }
  5105. significant_coeff_ctx_base = h->cabac_state
  5106. + significant_coeff_flag_offset[MB_FIELD][cat];
  5107. last_coeff_ctx_base = h->cabac_state
  5108. + last_coeff_flag_offset[MB_FIELD][cat];
  5109. abs_level_m1_ctx_base = h->cabac_state
  5110. + coeff_abs_level_m1_offset[cat];
  5111. if( cat == 5 ) {
  5112. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5113. for(last= 0; last < coefs; last++) { \
  5114. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5115. if( get_cabac( CC, sig_ctx )) { \
  5116. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5117. index[coeff_count++] = last; \
  5118. if( get_cabac( CC, last_ctx ) ) { \
  5119. last= max_coeff; \
  5120. break; \
  5121. } \
  5122. } \
  5123. }\
  5124. if( last == max_coeff -1 ) {\
  5125. index[coeff_count++] = last;\
  5126. }
  5127. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5128. #if defined(ARCH_X86) && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
  5129. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5130. } else {
  5131. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5132. #else
  5133. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5134. } else {
  5135. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5136. #endif
  5137. }
  5138. assert(coeff_count > 0);
  5139. if( cat == 0 )
  5140. h->cbp_table[mb_xy] |= 0x100;
  5141. else if( cat == 1 || cat == 2 )
  5142. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5143. else if( cat == 3 )
  5144. h->cbp_table[mb_xy] |= 0x40 << n;
  5145. else if( cat == 4 )
  5146. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5147. else {
  5148. assert( cat == 5 );
  5149. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5150. }
  5151. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5152. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5153. int j= scantable[index[coeff_count]];
  5154. if( get_cabac( CC, ctx ) == 0 ) {
  5155. if( !qmul ) {
  5156. block[j] = get_cabac_bypass_sign( CC, -1);
  5157. }else{
  5158. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5159. }
  5160. abslevel1++;
  5161. } else {
  5162. int coeff_abs = 2;
  5163. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5164. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5165. coeff_abs++;
  5166. }
  5167. if( coeff_abs >= 15 ) {
  5168. int j = 0;
  5169. while( get_cabac_bypass( CC ) ) {
  5170. j++;
  5171. }
  5172. coeff_abs=1;
  5173. while( j-- ) {
  5174. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5175. }
  5176. coeff_abs+= 14;
  5177. }
  5178. if( !qmul ) {
  5179. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5180. else block[j] = coeff_abs;
  5181. }else{
  5182. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5183. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5184. }
  5185. abslevelgt1++;
  5186. }
  5187. }
  5188. #ifdef CABAC_ON_STACK
  5189. h->cabac.range = cc.range ;
  5190. h->cabac.low = cc.low ;
  5191. h->cabac.bytestream= cc.bytestream;
  5192. #endif
  5193. return 0;
  5194. }
  5195. static inline void compute_mb_neighbors(H264Context *h)
  5196. {
  5197. MpegEncContext * const s = &h->s;
  5198. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5199. h->top_mb_xy = mb_xy - s->mb_stride;
  5200. h->left_mb_xy[0] = mb_xy - 1;
  5201. if(FRAME_MBAFF){
  5202. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5203. const int top_pair_xy = pair_xy - s->mb_stride;
  5204. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5205. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5206. const int curr_mb_frame_flag = !MB_FIELD;
  5207. const int bottom = (s->mb_y & 1);
  5208. if (bottom
  5209. ? !curr_mb_frame_flag // bottom macroblock
  5210. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5211. ) {
  5212. h->top_mb_xy -= s->mb_stride;
  5213. }
  5214. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5215. h->left_mb_xy[0] = pair_xy - 1;
  5216. }
  5217. }
  5218. return;
  5219. }
  5220. /**
  5221. * decodes a macroblock
  5222. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5223. */
  5224. static int decode_mb_cabac(H264Context *h) {
  5225. MpegEncContext * const s = &h->s;
  5226. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5227. int mb_type, partition_count, cbp = 0;
  5228. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5229. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5230. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5231. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5232. int skip;
  5233. /* a skipped mb needs the aff flag from the following mb */
  5234. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5235. predict_field_decoding_flag(h);
  5236. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5237. skip = h->next_mb_skipped;
  5238. else
  5239. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5240. /* read skip flags */
  5241. if( skip ) {
  5242. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5243. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5244. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5245. if(h->next_mb_skipped)
  5246. predict_field_decoding_flag(h);
  5247. else
  5248. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5249. }
  5250. decode_mb_skip(h);
  5251. h->cbp_table[mb_xy] = 0;
  5252. h->chroma_pred_mode_table[mb_xy] = 0;
  5253. h->last_qscale_diff = 0;
  5254. return 0;
  5255. }
  5256. }
  5257. if(FRAME_MBAFF){
  5258. if( (s->mb_y&1) == 0 )
  5259. h->mb_mbaff =
  5260. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5261. }else
  5262. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5263. h->prev_mb_skipped = 0;
  5264. compute_mb_neighbors(h);
  5265. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5266. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5267. return -1;
  5268. }
  5269. if( h->slice_type == B_TYPE ) {
  5270. if( mb_type < 23 ){
  5271. partition_count= b_mb_type_info[mb_type].partition_count;
  5272. mb_type= b_mb_type_info[mb_type].type;
  5273. }else{
  5274. mb_type -= 23;
  5275. goto decode_intra_mb;
  5276. }
  5277. } else if( h->slice_type == P_TYPE ) {
  5278. if( mb_type < 5) {
  5279. partition_count= p_mb_type_info[mb_type].partition_count;
  5280. mb_type= p_mb_type_info[mb_type].type;
  5281. } else {
  5282. mb_type -= 5;
  5283. goto decode_intra_mb;
  5284. }
  5285. } else {
  5286. assert(h->slice_type == I_TYPE);
  5287. decode_intra_mb:
  5288. partition_count = 0;
  5289. cbp= i_mb_type_info[mb_type].cbp;
  5290. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5291. mb_type= i_mb_type_info[mb_type].type;
  5292. }
  5293. if(MB_FIELD)
  5294. mb_type |= MB_TYPE_INTERLACED;
  5295. h->slice_table[ mb_xy ]= h->slice_num;
  5296. if(IS_INTRA_PCM(mb_type)) {
  5297. const uint8_t *ptr;
  5298. unsigned int x, y;
  5299. // We assume these blocks are very rare so we do not optimize it.
  5300. // FIXME The two following lines get the bitstream position in the cabac
  5301. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5302. ptr= h->cabac.bytestream;
  5303. if(h->cabac.low&0x1) ptr--;
  5304. if(CABAC_BITS==16){
  5305. if(h->cabac.low&0x1FF) ptr--;
  5306. }
  5307. // The pixels are stored in the same order as levels in h->mb array.
  5308. for(y=0; y<16; y++){
  5309. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5310. for(x=0; x<16; x++){
  5311. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", *ptr);
  5312. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5313. }
  5314. }
  5315. for(y=0; y<8; y++){
  5316. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5317. for(x=0; x<8; x++){
  5318. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5319. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5320. }
  5321. }
  5322. for(y=0; y<8; y++){
  5323. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5324. for(x=0; x<8; x++){
  5325. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5326. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5327. }
  5328. }
  5329. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5330. // All blocks are present
  5331. h->cbp_table[mb_xy] = 0x1ef;
  5332. h->chroma_pred_mode_table[mb_xy] = 0;
  5333. // In deblocking, the quantizer is 0
  5334. s->current_picture.qscale_table[mb_xy]= 0;
  5335. h->chroma_qp[0] = get_chroma_qp(h, 0, 0);
  5336. h->chroma_qp[1] = get_chroma_qp(h, 1, 0);
  5337. // All coeffs are present
  5338. memset(h->non_zero_count[mb_xy], 16, 16);
  5339. s->current_picture.mb_type[mb_xy]= mb_type;
  5340. return 0;
  5341. }
  5342. if(MB_MBAFF){
  5343. h->ref_count[0] <<= 1;
  5344. h->ref_count[1] <<= 1;
  5345. }
  5346. fill_caches(h, mb_type, 0);
  5347. if( IS_INTRA( mb_type ) ) {
  5348. int i, pred_mode;
  5349. if( IS_INTRA4x4( mb_type ) ) {
  5350. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5351. mb_type |= MB_TYPE_8x8DCT;
  5352. for( i = 0; i < 16; i+=4 ) {
  5353. int pred = pred_intra_mode( h, i );
  5354. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5355. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5356. }
  5357. } else {
  5358. for( i = 0; i < 16; i++ ) {
  5359. int pred = pred_intra_mode( h, i );
  5360. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5361. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5362. }
  5363. }
  5364. write_back_intra_pred_mode(h);
  5365. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5366. } else {
  5367. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5368. if( h->intra16x16_pred_mode < 0 ) return -1;
  5369. }
  5370. h->chroma_pred_mode_table[mb_xy] =
  5371. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5372. pred_mode= check_intra_pred_mode( h, pred_mode );
  5373. if( pred_mode < 0 ) return -1;
  5374. h->chroma_pred_mode= pred_mode;
  5375. } else if( partition_count == 4 ) {
  5376. int i, j, sub_partition_count[4], list, ref[2][4];
  5377. if( h->slice_type == B_TYPE ) {
  5378. for( i = 0; i < 4; i++ ) {
  5379. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5380. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5381. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5382. }
  5383. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5384. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5385. pred_direct_motion(h, &mb_type);
  5386. h->ref_cache[0][scan8[4]] =
  5387. h->ref_cache[1][scan8[4]] =
  5388. h->ref_cache[0][scan8[12]] =
  5389. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  5390. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5391. for( i = 0; i < 4; i++ )
  5392. if( IS_DIRECT(h->sub_mb_type[i]) )
  5393. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5394. }
  5395. }
  5396. } else {
  5397. for( i = 0; i < 4; i++ ) {
  5398. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5399. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5400. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5401. }
  5402. }
  5403. for( list = 0; list < h->list_count; list++ ) {
  5404. for( i = 0; i < 4; i++ ) {
  5405. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5406. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5407. if( h->ref_count[list] > 1 )
  5408. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5409. else
  5410. ref[list][i] = 0;
  5411. } else {
  5412. ref[list][i] = -1;
  5413. }
  5414. h->ref_cache[list][ scan8[4*i]+1 ]=
  5415. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5416. }
  5417. }
  5418. if(dct8x8_allowed)
  5419. dct8x8_allowed = get_dct8x8_allowed(h);
  5420. for(list=0; list<h->list_count; list++){
  5421. for(i=0; i<4; i++){
  5422. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5423. if(IS_DIRECT(h->sub_mb_type[i])){
  5424. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5425. continue;
  5426. }
  5427. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5428. const int sub_mb_type= h->sub_mb_type[i];
  5429. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5430. for(j=0; j<sub_partition_count[i]; j++){
  5431. int mpx, mpy;
  5432. int mx, my;
  5433. const int index= 4*i + block_width*j;
  5434. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5435. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5436. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5437. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5438. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5439. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5440. if(IS_SUB_8X8(sub_mb_type)){
  5441. mv_cache[ 1 ][0]=
  5442. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5443. mv_cache[ 1 ][1]=
  5444. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5445. mvd_cache[ 1 ][0]=
  5446. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5447. mvd_cache[ 1 ][1]=
  5448. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5449. }else if(IS_SUB_8X4(sub_mb_type)){
  5450. mv_cache[ 1 ][0]= mx;
  5451. mv_cache[ 1 ][1]= my;
  5452. mvd_cache[ 1 ][0]= mx - mpx;
  5453. mvd_cache[ 1 ][1]= my - mpy;
  5454. }else if(IS_SUB_4X8(sub_mb_type)){
  5455. mv_cache[ 8 ][0]= mx;
  5456. mv_cache[ 8 ][1]= my;
  5457. mvd_cache[ 8 ][0]= mx - mpx;
  5458. mvd_cache[ 8 ][1]= my - mpy;
  5459. }
  5460. mv_cache[ 0 ][0]= mx;
  5461. mv_cache[ 0 ][1]= my;
  5462. mvd_cache[ 0 ][0]= mx - mpx;
  5463. mvd_cache[ 0 ][1]= my - mpy;
  5464. }
  5465. }else{
  5466. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5467. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5468. p[0] = p[1] = p[8] = p[9] = 0;
  5469. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5470. }
  5471. }
  5472. }
  5473. } else if( IS_DIRECT(mb_type) ) {
  5474. pred_direct_motion(h, &mb_type);
  5475. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5476. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5477. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5478. } else {
  5479. int list, mx, my, i, mpx, mpy;
  5480. if(IS_16X16(mb_type)){
  5481. for(list=0; list<h->list_count; list++){
  5482. if(IS_DIR(mb_type, 0, list)){
  5483. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5484. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5485. }else
  5486. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1); //FIXME factorize and the other fill_rect below too
  5487. }
  5488. for(list=0; list<h->list_count; list++){
  5489. if(IS_DIR(mb_type, 0, list)){
  5490. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5491. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5492. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5493. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5494. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5495. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5496. }else
  5497. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5498. }
  5499. }
  5500. else if(IS_16X8(mb_type)){
  5501. for(list=0; list<h->list_count; list++){
  5502. for(i=0; i<2; i++){
  5503. if(IS_DIR(mb_type, i, list)){
  5504. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5505. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5506. }else
  5507. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5508. }
  5509. }
  5510. for(list=0; list<h->list_count; list++){
  5511. for(i=0; i<2; i++){
  5512. if(IS_DIR(mb_type, i, list)){
  5513. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5514. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5515. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5516. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5517. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5518. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5519. }else{
  5520. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5521. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5522. }
  5523. }
  5524. }
  5525. }else{
  5526. assert(IS_8X16(mb_type));
  5527. for(list=0; list<h->list_count; list++){
  5528. for(i=0; i<2; i++){
  5529. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5530. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5531. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5532. }else
  5533. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5534. }
  5535. }
  5536. for(list=0; list<h->list_count; list++){
  5537. for(i=0; i<2; i++){
  5538. if(IS_DIR(mb_type, i, list)){
  5539. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5540. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5541. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5542. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5543. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5544. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5545. }else{
  5546. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5547. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5548. }
  5549. }
  5550. }
  5551. }
  5552. }
  5553. if( IS_INTER( mb_type ) ) {
  5554. h->chroma_pred_mode_table[mb_xy] = 0;
  5555. write_back_motion( h, mb_type );
  5556. }
  5557. if( !IS_INTRA16x16( mb_type ) ) {
  5558. cbp = decode_cabac_mb_cbp_luma( h );
  5559. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5560. }
  5561. h->cbp_table[mb_xy] = h->cbp = cbp;
  5562. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5563. if( decode_cabac_mb_transform_size( h ) )
  5564. mb_type |= MB_TYPE_8x8DCT;
  5565. }
  5566. s->current_picture.mb_type[mb_xy]= mb_type;
  5567. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5568. const uint8_t *scan, *scan8x8, *dc_scan;
  5569. int dqp;
  5570. if(IS_INTERLACED(mb_type)){
  5571. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5572. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5573. dc_scan= luma_dc_field_scan;
  5574. }else{
  5575. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5576. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5577. dc_scan= luma_dc_zigzag_scan;
  5578. }
  5579. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5580. if( dqp == INT_MIN ){
  5581. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5582. return -1;
  5583. }
  5584. s->qscale += dqp;
  5585. if(((unsigned)s->qscale) > 51){
  5586. if(s->qscale<0) s->qscale+= 52;
  5587. else s->qscale-= 52;
  5588. }
  5589. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  5590. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  5591. if( IS_INTRA16x16( mb_type ) ) {
  5592. int i;
  5593. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5594. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5595. return -1;
  5596. if( cbp&15 ) {
  5597. for( i = 0; i < 16; i++ ) {
  5598. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5599. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5600. return -1;
  5601. }
  5602. } else {
  5603. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5604. }
  5605. } else {
  5606. int i8x8, i4x4;
  5607. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5608. if( cbp & (1<<i8x8) ) {
  5609. if( IS_8x8DCT(mb_type) ) {
  5610. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5611. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5612. return -1;
  5613. } else
  5614. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5615. const int index = 4*i8x8 + i4x4;
  5616. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5617. //START_TIMER
  5618. if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) < 0 )
  5619. return -1;
  5620. //STOP_TIMER("decode_residual")
  5621. }
  5622. } else {
  5623. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5624. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5625. }
  5626. }
  5627. }
  5628. if( cbp&0x30 ){
  5629. int c;
  5630. for( c = 0; c < 2; c++ ) {
  5631. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5632. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5633. return -1;
  5634. }
  5635. }
  5636. if( cbp&0x20 ) {
  5637. int c, i;
  5638. for( c = 0; c < 2; c++ ) {
  5639. const uint32_t *qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];
  5640. for( i = 0; i < 4; i++ ) {
  5641. const int index = 16 + 4 * c + i;
  5642. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5643. if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, qmul, 15) < 0)
  5644. return -1;
  5645. }
  5646. }
  5647. } else {
  5648. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5649. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5650. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5651. }
  5652. } else {
  5653. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5654. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5655. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5656. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5657. h->last_qscale_diff = 0;
  5658. }
  5659. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5660. write_back_non_zero_count(h);
  5661. if(MB_MBAFF){
  5662. h->ref_count[0] >>= 1;
  5663. h->ref_count[1] >>= 1;
  5664. }
  5665. return 0;
  5666. }
  5667. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5668. int i, d;
  5669. const int index_a = qp + h->slice_alpha_c0_offset;
  5670. const int alpha = (alpha_table+52)[index_a];
  5671. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5672. if( bS[0] < 4 ) {
  5673. int8_t tc[4];
  5674. for(i=0; i<4; i++)
  5675. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5676. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5677. } else {
  5678. /* 16px edge length, because bS=4 is triggered by being at
  5679. * the edge of an intra MB, so all 4 bS are the same */
  5680. for( d = 0; d < 16; d++ ) {
  5681. const int p0 = pix[-1];
  5682. const int p1 = pix[-2];
  5683. const int p2 = pix[-3];
  5684. const int q0 = pix[0];
  5685. const int q1 = pix[1];
  5686. const int q2 = pix[2];
  5687. if( FFABS( p0 - q0 ) < alpha &&
  5688. FFABS( p1 - p0 ) < beta &&
  5689. FFABS( q1 - q0 ) < beta ) {
  5690. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5691. if( FFABS( p2 - p0 ) < beta)
  5692. {
  5693. const int p3 = pix[-4];
  5694. /* p0', p1', p2' */
  5695. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5696. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5697. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5698. } else {
  5699. /* p0' */
  5700. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5701. }
  5702. if( FFABS( q2 - q0 ) < beta)
  5703. {
  5704. const int q3 = pix[3];
  5705. /* q0', q1', q2' */
  5706. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5707. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5708. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5709. } else {
  5710. /* q0' */
  5711. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5712. }
  5713. }else{
  5714. /* p0', q0' */
  5715. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5716. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5717. }
  5718. tprintf(h->s.avctx, "filter_mb_edgev i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, p2, p1, p0, q0, q1, q2, pix[-2], pix[-1], pix[0], pix[1]);
  5719. }
  5720. pix += stride;
  5721. }
  5722. }
  5723. }
  5724. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5725. int i;
  5726. const int index_a = qp + h->slice_alpha_c0_offset;
  5727. const int alpha = (alpha_table+52)[index_a];
  5728. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5729. if( bS[0] < 4 ) {
  5730. int8_t tc[4];
  5731. for(i=0; i<4; i++)
  5732. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5733. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5734. } else {
  5735. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5736. }
  5737. }
  5738. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5739. int i;
  5740. for( i = 0; i < 16; i++, pix += stride) {
  5741. int index_a;
  5742. int alpha;
  5743. int beta;
  5744. int qp_index;
  5745. int bS_index = (i >> 1);
  5746. if (!MB_FIELD) {
  5747. bS_index &= ~1;
  5748. bS_index |= (i & 1);
  5749. }
  5750. if( bS[bS_index] == 0 ) {
  5751. continue;
  5752. }
  5753. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5754. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5755. alpha = (alpha_table+52)[index_a];
  5756. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5757. if( bS[bS_index] < 4 ) {
  5758. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  5759. const int p0 = pix[-1];
  5760. const int p1 = pix[-2];
  5761. const int p2 = pix[-3];
  5762. const int q0 = pix[0];
  5763. const int q1 = pix[1];
  5764. const int q2 = pix[2];
  5765. if( FFABS( p0 - q0 ) < alpha &&
  5766. FFABS( p1 - p0 ) < beta &&
  5767. FFABS( q1 - q0 ) < beta ) {
  5768. int tc = tc0;
  5769. int i_delta;
  5770. if( FFABS( p2 - p0 ) < beta ) {
  5771. pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5772. tc++;
  5773. }
  5774. if( FFABS( q2 - q0 ) < beta ) {
  5775. pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5776. tc++;
  5777. }
  5778. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5779. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5780. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5781. tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5782. }
  5783. }else{
  5784. const int p0 = pix[-1];
  5785. const int p1 = pix[-2];
  5786. const int p2 = pix[-3];
  5787. const int q0 = pix[0];
  5788. const int q1 = pix[1];
  5789. const int q2 = pix[2];
  5790. if( FFABS( p0 - q0 ) < alpha &&
  5791. FFABS( p1 - p0 ) < beta &&
  5792. FFABS( q1 - q0 ) < beta ) {
  5793. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5794. if( FFABS( p2 - p0 ) < beta)
  5795. {
  5796. const int p3 = pix[-4];
  5797. /* p0', p1', p2' */
  5798. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5799. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5800. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5801. } else {
  5802. /* p0' */
  5803. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5804. }
  5805. if( FFABS( q2 - q0 ) < beta)
  5806. {
  5807. const int q3 = pix[3];
  5808. /* q0', q1', q2' */
  5809. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5810. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5811. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5812. } else {
  5813. /* q0' */
  5814. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5815. }
  5816. }else{
  5817. /* p0', q0' */
  5818. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5819. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5820. }
  5821. tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  5822. }
  5823. }
  5824. }
  5825. }
  5826. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5827. int i;
  5828. for( i = 0; i < 8; i++, pix += stride) {
  5829. int index_a;
  5830. int alpha;
  5831. int beta;
  5832. int qp_index;
  5833. int bS_index = i;
  5834. if( bS[bS_index] == 0 ) {
  5835. continue;
  5836. }
  5837. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  5838. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5839. alpha = (alpha_table+52)[index_a];
  5840. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5841. if( bS[bS_index] < 4 ) {
  5842. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  5843. const int p0 = pix[-1];
  5844. const int p1 = pix[-2];
  5845. const int q0 = pix[0];
  5846. const int q1 = pix[1];
  5847. if( FFABS( p0 - q0 ) < alpha &&
  5848. FFABS( p1 - p0 ) < beta &&
  5849. FFABS( q1 - q0 ) < beta ) {
  5850. const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5851. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5852. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5853. tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5854. }
  5855. }else{
  5856. const int p0 = pix[-1];
  5857. const int p1 = pix[-2];
  5858. const int q0 = pix[0];
  5859. const int q1 = pix[1];
  5860. if( FFABS( p0 - q0 ) < alpha &&
  5861. FFABS( p1 - p0 ) < beta &&
  5862. FFABS( q1 - q0 ) < beta ) {
  5863. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5864. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5865. tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  5866. }
  5867. }
  5868. }
  5869. }
  5870. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5871. int i, d;
  5872. const int index_a = qp + h->slice_alpha_c0_offset;
  5873. const int alpha = (alpha_table+52)[index_a];
  5874. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5875. const int pix_next = stride;
  5876. if( bS[0] < 4 ) {
  5877. int8_t tc[4];
  5878. for(i=0; i<4; i++)
  5879. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5880. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5881. } else {
  5882. /* 16px edge length, see filter_mb_edgev */
  5883. for( d = 0; d < 16; d++ ) {
  5884. const int p0 = pix[-1*pix_next];
  5885. const int p1 = pix[-2*pix_next];
  5886. const int p2 = pix[-3*pix_next];
  5887. const int q0 = pix[0];
  5888. const int q1 = pix[1*pix_next];
  5889. const int q2 = pix[2*pix_next];
  5890. if( FFABS( p0 - q0 ) < alpha &&
  5891. FFABS( p1 - p0 ) < beta &&
  5892. FFABS( q1 - q0 ) < beta ) {
  5893. const int p3 = pix[-4*pix_next];
  5894. const int q3 = pix[ 3*pix_next];
  5895. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5896. if( FFABS( p2 - p0 ) < beta) {
  5897. /* p0', p1', p2' */
  5898. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5899. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5900. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5901. } else {
  5902. /* p0' */
  5903. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5904. }
  5905. if( FFABS( q2 - q0 ) < beta) {
  5906. /* q0', q1', q2' */
  5907. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5908. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5909. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5910. } else {
  5911. /* q0' */
  5912. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5913. }
  5914. }else{
  5915. /* p0', q0' */
  5916. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5917. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5918. }
  5919. tprintf(h->s.avctx, "filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
  5920. }
  5921. pix++;
  5922. }
  5923. }
  5924. }
  5925. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5926. int i;
  5927. const int index_a = qp + h->slice_alpha_c0_offset;
  5928. const int alpha = (alpha_table+52)[index_a];
  5929. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5930. if( bS[0] < 4 ) {
  5931. int8_t tc[4];
  5932. for(i=0; i<4; i++)
  5933. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5934. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5935. } else {
  5936. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5937. }
  5938. }
  5939. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  5940. MpegEncContext * const s = &h->s;
  5941. int mb_xy, mb_type;
  5942. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  5943. mb_xy = mb_x + mb_y*s->mb_stride;
  5944. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
  5945. (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] ||
  5946. h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
  5947. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  5948. return;
  5949. }
  5950. assert(!FRAME_MBAFF);
  5951. mb_type = s->current_picture.mb_type[mb_xy];
  5952. qp = s->current_picture.qscale_table[mb_xy];
  5953. qp0 = s->current_picture.qscale_table[mb_xy-1];
  5954. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  5955. qpc = get_chroma_qp( h, 0, qp );
  5956. qpc0 = get_chroma_qp( h, 0, qp0 );
  5957. qpc1 = get_chroma_qp( h, 0, qp1 );
  5958. qp0 = (qp + qp0 + 1) >> 1;
  5959. qp1 = (qp + qp1 + 1) >> 1;
  5960. qpc0 = (qpc + qpc0 + 1) >> 1;
  5961. qpc1 = (qpc + qpc1 + 1) >> 1;
  5962. qp_thresh = 15 - h->slice_alpha_c0_offset;
  5963. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  5964. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  5965. return;
  5966. if( IS_INTRA(mb_type) ) {
  5967. int16_t bS4[4] = {4,4,4,4};
  5968. int16_t bS3[4] = {3,3,3,3};
  5969. if( IS_8x8DCT(mb_type) ) {
  5970. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5971. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5972. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  5973. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5974. } else {
  5975. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5976. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  5977. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5978. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  5979. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  5980. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  5981. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5982. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  5983. }
  5984. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  5985. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  5986. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  5987. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  5988. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  5989. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5990. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  5991. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5992. return;
  5993. } else {
  5994. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  5995. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  5996. int edges;
  5997. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  5998. edges = 4;
  5999. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6000. } else {
  6001. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6002. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6003. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6004. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6005. ? 3 : 0;
  6006. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6007. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6008. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6009. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6010. }
  6011. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6012. bSv[0][0] = 0x0004000400040004ULL;
  6013. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6014. bSv[1][0] = 0x0004000400040004ULL;
  6015. #define FILTER(hv,dir,edge)\
  6016. if(bSv[dir][edge]) {\
  6017. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6018. if(!(edge&1)) {\
  6019. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6020. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6021. }\
  6022. }
  6023. if( edges == 1 ) {
  6024. FILTER(v,0,0);
  6025. FILTER(h,1,0);
  6026. } else if( IS_8x8DCT(mb_type) ) {
  6027. FILTER(v,0,0);
  6028. FILTER(v,0,2);
  6029. FILTER(h,1,0);
  6030. FILTER(h,1,2);
  6031. } else {
  6032. FILTER(v,0,0);
  6033. FILTER(v,0,1);
  6034. FILTER(v,0,2);
  6035. FILTER(v,0,3);
  6036. FILTER(h,1,0);
  6037. FILTER(h,1,1);
  6038. FILTER(h,1,2);
  6039. FILTER(h,1,3);
  6040. }
  6041. #undef FILTER
  6042. }
  6043. }
  6044. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  6045. MpegEncContext * const s = &h->s;
  6046. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6047. const int mb_type = s->current_picture.mb_type[mb_xy];
  6048. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6049. int first_vertical_edge_done = 0;
  6050. int dir;
  6051. /* FIXME: A given frame may occupy more than one position in
  6052. * the reference list. So ref2frm should be populated with
  6053. * frame numbers, not indices. */
  6054. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6055. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6056. //for sufficiently low qp, filtering wouldn't do anything
  6057. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6058. if(!FRAME_MBAFF){
  6059. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, FFMAX(h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]));
  6060. int qp = s->current_picture.qscale_table[mb_xy];
  6061. if(qp <= qp_thresh
  6062. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6063. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6064. return;
  6065. }
  6066. }
  6067. if (FRAME_MBAFF
  6068. // left mb is in picture
  6069. && h->slice_table[mb_xy-1] != 255
  6070. // and current and left pair do not have the same interlaced type
  6071. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6072. // and left mb is in the same slice if deblocking_filter == 2
  6073. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6074. /* First vertical edge is different in MBAFF frames
  6075. * There are 8 different bS to compute and 2 different Qp
  6076. */
  6077. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6078. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6079. int16_t bS[8];
  6080. int qp[2];
  6081. int bqp[2];
  6082. int rqp[2];
  6083. int mb_qp, mbn0_qp, mbn1_qp;
  6084. int i;
  6085. first_vertical_edge_done = 1;
  6086. if( IS_INTRA(mb_type) )
  6087. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6088. else {
  6089. for( i = 0; i < 8; i++ ) {
  6090. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6091. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6092. bS[i] = 4;
  6093. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6094. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6095. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6096. bS[i] = 2;
  6097. else
  6098. bS[i] = 1;
  6099. }
  6100. }
  6101. mb_qp = s->current_picture.qscale_table[mb_xy];
  6102. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6103. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6104. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6105. bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
  6106. get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
  6107. rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
  6108. get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
  6109. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6110. bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
  6111. get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
  6112. rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
  6113. get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
  6114. /* Filter edge */
  6115. tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);
  6116. { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6117. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6118. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, bqp );
  6119. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, rqp );
  6120. }
  6121. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6122. for( dir = 0; dir < 2; dir++ )
  6123. {
  6124. int edge;
  6125. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6126. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6127. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6128. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6129. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6130. // how often to recheck mv-based bS when iterating between edges
  6131. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6132. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6133. // how often to recheck mv-based bS when iterating along each edge
  6134. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6135. if (first_vertical_edge_done) {
  6136. start = 1;
  6137. first_vertical_edge_done = 0;
  6138. }
  6139. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6140. start = 1;
  6141. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6142. && !IS_INTERLACED(mb_type)
  6143. && IS_INTERLACED(mbm_type)
  6144. ) {
  6145. // This is a special case in the norm where the filtering must
  6146. // be done twice (one each of the field) even if we are in a
  6147. // frame macroblock.
  6148. //
  6149. static const int nnz_idx[4] = {4,5,6,3};
  6150. unsigned int tmp_linesize = 2 * linesize;
  6151. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6152. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6153. int qp;
  6154. int i, j;
  6155. int16_t bS[4];
  6156. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6157. if( IS_INTRA(mb_type) ||
  6158. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6159. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6160. } else {
  6161. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6162. for( i = 0; i < 4; i++ ) {
  6163. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6164. mbn_nnz[nnz_idx[i]] != 0 )
  6165. bS[i] = 2;
  6166. else
  6167. bS[i] = 1;
  6168. }
  6169. }
  6170. // Do not use s->qscale as luma quantizer because it has not the same
  6171. // value in IPCM macroblocks.
  6172. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6173. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
  6174. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6175. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6176. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
  6177. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  6178. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
  6179. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  6180. }
  6181. start = 1;
  6182. }
  6183. /* Calculate bS */
  6184. for( edge = start; edge < edges; edge++ ) {
  6185. /* mbn_xy: neighbor macroblock */
  6186. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6187. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6188. int16_t bS[4];
  6189. int qp;
  6190. if( (edge&1) && IS_8x8DCT(mb_type) )
  6191. continue;
  6192. if( IS_INTRA(mb_type) ||
  6193. IS_INTRA(mbn_type) ) {
  6194. int value;
  6195. if (edge == 0) {
  6196. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6197. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6198. ) {
  6199. value = 4;
  6200. } else {
  6201. value = 3;
  6202. }
  6203. } else {
  6204. value = 3;
  6205. }
  6206. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6207. } else {
  6208. int i, l;
  6209. int mv_done;
  6210. if( edge & mask_edge ) {
  6211. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6212. mv_done = 1;
  6213. }
  6214. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6215. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6216. mv_done = 1;
  6217. }
  6218. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6219. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6220. int bn_idx= b_idx - (dir ? 8:1);
  6221. int v = 0;
  6222. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6223. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6224. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6225. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6226. }
  6227. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6228. mv_done = 1;
  6229. }
  6230. else
  6231. mv_done = 0;
  6232. for( i = 0; i < 4; i++ ) {
  6233. int x = dir == 0 ? edge : i;
  6234. int y = dir == 0 ? i : edge;
  6235. int b_idx= 8 + 4 + x + 8*y;
  6236. int bn_idx= b_idx - (dir ? 8:1);
  6237. if( h->non_zero_count_cache[b_idx] != 0 ||
  6238. h->non_zero_count_cache[bn_idx] != 0 ) {
  6239. bS[i] = 2;
  6240. }
  6241. else if(!mv_done)
  6242. {
  6243. bS[i] = 0;
  6244. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6245. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6246. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6247. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6248. bS[i] = 1;
  6249. break;
  6250. }
  6251. }
  6252. }
  6253. }
  6254. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6255. continue;
  6256. }
  6257. /* Filter edge */
  6258. // Do not use s->qscale as luma quantizer because it has not the same
  6259. // value in IPCM macroblocks.
  6260. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6261. //tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);
  6262. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6263. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6264. if( dir == 0 ) {
  6265. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6266. if( (edge&1) == 0 ) {
  6267. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS,
  6268. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  6269. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS,
  6270. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  6271. }
  6272. } else {
  6273. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6274. if( (edge&1) == 0 ) {
  6275. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS,
  6276. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  6277. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS,
  6278. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  6279. }
  6280. }
  6281. }
  6282. }
  6283. }
  6284. static int decode_slice(H264Context *h){
  6285. MpegEncContext * const s = &h->s;
  6286. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6287. s->mb_skip_run= -1;
  6288. if( h->pps.cabac ) {
  6289. int i;
  6290. /* realign */
  6291. align_get_bits( &s->gb );
  6292. /* init cabac */
  6293. ff_init_cabac_states( &h->cabac);
  6294. ff_init_cabac_decoder( &h->cabac,
  6295. s->gb.buffer + get_bits_count(&s->gb)/8,
  6296. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6297. /* calculate pre-state */
  6298. for( i= 0; i < 460; i++ ) {
  6299. int pre;
  6300. if( h->slice_type == I_TYPE )
  6301. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6302. else
  6303. pre = av_clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
  6304. if( pre <= 63 )
  6305. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6306. else
  6307. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6308. }
  6309. for(;;){
  6310. //START_TIMER
  6311. int ret = decode_mb_cabac(h);
  6312. int eos;
  6313. //STOP_TIMER("decode_mb_cabac")
  6314. if(ret>=0) hl_decode_mb(h);
  6315. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6316. s->mb_y++;
  6317. if(ret>=0) ret = decode_mb_cabac(h);
  6318. if(ret>=0) hl_decode_mb(h);
  6319. s->mb_y--;
  6320. }
  6321. eos = get_cabac_terminate( &h->cabac );
  6322. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6323. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
  6324. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6325. return -1;
  6326. }
  6327. if( ++s->mb_x >= s->mb_width ) {
  6328. s->mb_x = 0;
  6329. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6330. ++s->mb_y;
  6331. if(FRAME_MBAFF) {
  6332. ++s->mb_y;
  6333. }
  6334. }
  6335. if( eos || s->mb_y >= s->mb_height ) {
  6336. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6337. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6338. return 0;
  6339. }
  6340. }
  6341. } else {
  6342. for(;;){
  6343. int ret = decode_mb_cavlc(h);
  6344. if(ret>=0) hl_decode_mb(h);
  6345. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6346. s->mb_y++;
  6347. ret = decode_mb_cavlc(h);
  6348. if(ret>=0) hl_decode_mb(h);
  6349. s->mb_y--;
  6350. }
  6351. if(ret<0){
  6352. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6353. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6354. return -1;
  6355. }
  6356. if(++s->mb_x >= s->mb_width){
  6357. s->mb_x=0;
  6358. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6359. ++s->mb_y;
  6360. if(FRAME_MBAFF) {
  6361. ++s->mb_y;
  6362. }
  6363. if(s->mb_y >= s->mb_height){
  6364. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6365. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6366. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6367. return 0;
  6368. }else{
  6369. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6370. return -1;
  6371. }
  6372. }
  6373. }
  6374. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6375. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6376. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6377. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6378. return 0;
  6379. }else{
  6380. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6381. return -1;
  6382. }
  6383. }
  6384. }
  6385. }
  6386. #if 0
  6387. for(;s->mb_y < s->mb_height; s->mb_y++){
  6388. for(;s->mb_x < s->mb_width; s->mb_x++){
  6389. int ret= decode_mb(h);
  6390. hl_decode_mb(h);
  6391. if(ret<0){
  6392. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6393. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6394. return -1;
  6395. }
  6396. if(++s->mb_x >= s->mb_width){
  6397. s->mb_x=0;
  6398. if(++s->mb_y >= s->mb_height){
  6399. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6400. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6401. return 0;
  6402. }else{
  6403. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6404. return -1;
  6405. }
  6406. }
  6407. }
  6408. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6409. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6410. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6411. return 0;
  6412. }else{
  6413. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6414. return -1;
  6415. }
  6416. }
  6417. }
  6418. s->mb_x=0;
  6419. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6420. }
  6421. #endif
  6422. return -1; //not reached
  6423. }
  6424. static int decode_unregistered_user_data(H264Context *h, int size){
  6425. MpegEncContext * const s = &h->s;
  6426. uint8_t user_data[16+256];
  6427. int e, build, i;
  6428. if(size<16)
  6429. return -1;
  6430. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6431. user_data[i]= get_bits(&s->gb, 8);
  6432. }
  6433. user_data[i]= 0;
  6434. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6435. if(e==1 && build>=0)
  6436. h->x264_build= build;
  6437. if(s->avctx->debug & FF_DEBUG_BUGS)
  6438. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6439. for(; i<size; i++)
  6440. skip_bits(&s->gb, 8);
  6441. return 0;
  6442. }
  6443. static int decode_sei(H264Context *h){
  6444. MpegEncContext * const s = &h->s;
  6445. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6446. int size, type;
  6447. type=0;
  6448. do{
  6449. type+= show_bits(&s->gb, 8);
  6450. }while(get_bits(&s->gb, 8) == 255);
  6451. size=0;
  6452. do{
  6453. size+= show_bits(&s->gb, 8);
  6454. }while(get_bits(&s->gb, 8) == 255);
  6455. switch(type){
  6456. case 5:
  6457. if(decode_unregistered_user_data(h, size) < 0)
  6458. return -1;
  6459. break;
  6460. default:
  6461. skip_bits(&s->gb, 8*size);
  6462. }
  6463. //FIXME check bits here
  6464. align_get_bits(&s->gb);
  6465. }
  6466. return 0;
  6467. }
  6468. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6469. MpegEncContext * const s = &h->s;
  6470. int cpb_count, i;
  6471. cpb_count = get_ue_golomb(&s->gb) + 1;
  6472. get_bits(&s->gb, 4); /* bit_rate_scale */
  6473. get_bits(&s->gb, 4); /* cpb_size_scale */
  6474. for(i=0; i<cpb_count; i++){
  6475. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6476. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6477. get_bits1(&s->gb); /* cbr_flag */
  6478. }
  6479. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6480. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6481. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6482. get_bits(&s->gb, 5); /* time_offset_length */
  6483. }
  6484. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6485. MpegEncContext * const s = &h->s;
  6486. int aspect_ratio_info_present_flag;
  6487. unsigned int aspect_ratio_idc;
  6488. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6489. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6490. if( aspect_ratio_info_present_flag ) {
  6491. aspect_ratio_idc= get_bits(&s->gb, 8);
  6492. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6493. sps->sar.num= get_bits(&s->gb, 16);
  6494. sps->sar.den= get_bits(&s->gb, 16);
  6495. }else if(aspect_ratio_idc < 14){
  6496. sps->sar= pixel_aspect[aspect_ratio_idc];
  6497. }else{
  6498. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6499. return -1;
  6500. }
  6501. }else{
  6502. sps->sar.num=
  6503. sps->sar.den= 0;
  6504. }
  6505. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6506. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6507. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6508. }
  6509. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6510. get_bits(&s->gb, 3); /* video_format */
  6511. get_bits1(&s->gb); /* video_full_range_flag */
  6512. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6513. get_bits(&s->gb, 8); /* colour_primaries */
  6514. get_bits(&s->gb, 8); /* transfer_characteristics */
  6515. get_bits(&s->gb, 8); /* matrix_coefficients */
  6516. }
  6517. }
  6518. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6519. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6520. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6521. }
  6522. sps->timing_info_present_flag = get_bits1(&s->gb);
  6523. if(sps->timing_info_present_flag){
  6524. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6525. sps->time_scale = get_bits_long(&s->gb, 32);
  6526. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6527. }
  6528. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6529. if(nal_hrd_parameters_present_flag)
  6530. decode_hrd_parameters(h, sps);
  6531. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6532. if(vcl_hrd_parameters_present_flag)
  6533. decode_hrd_parameters(h, sps);
  6534. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6535. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6536. get_bits1(&s->gb); /* pic_struct_present_flag */
  6537. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6538. if(sps->bitstream_restriction_flag){
  6539. unsigned int num_reorder_frames;
  6540. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6541. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6542. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6543. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6544. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6545. num_reorder_frames= get_ue_golomb(&s->gb);
  6546. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6547. if(num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6548. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
  6549. return -1;
  6550. }
  6551. sps->num_reorder_frames= num_reorder_frames;
  6552. }
  6553. return 0;
  6554. }
  6555. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6556. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6557. MpegEncContext * const s = &h->s;
  6558. int i, last = 8, next = 8;
  6559. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6560. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6561. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6562. else
  6563. for(i=0;i<size;i++){
  6564. if(next)
  6565. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6566. if(!i && !next){ /* matrix not written, we use the preset one */
  6567. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6568. break;
  6569. }
  6570. last = factors[scan[i]] = next ? next : last;
  6571. }
  6572. }
  6573. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6574. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6575. MpegEncContext * const s = &h->s;
  6576. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6577. const uint8_t *fallback[4] = {
  6578. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6579. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6580. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6581. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6582. };
  6583. if(get_bits1(&s->gb)){
  6584. sps->scaling_matrix_present |= is_sps;
  6585. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6586. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6587. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6588. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6589. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6590. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6591. if(is_sps || pps->transform_8x8_mode){
  6592. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6593. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6594. }
  6595. } else if(fallback_sps) {
  6596. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6597. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6598. }
  6599. }
  6600. /**
  6601. * Returns and optionally allocates SPS / PPS structures in the supplied array 'vec'
  6602. */
  6603. static void *
  6604. alloc_parameter_set(H264Context *h, void **vec, const unsigned int id, const unsigned int max,
  6605. const size_t size, const char *name)
  6606. {
  6607. if(id>=max) {
  6608. av_log(h->s.avctx, AV_LOG_ERROR, "%s_id (%d) out of range\n", name, id);
  6609. return NULL;
  6610. }
  6611. if(!vec[id]) {
  6612. vec[id] = av_mallocz(size);
  6613. if(vec[id] == NULL)
  6614. av_log(h->s.avctx, AV_LOG_ERROR, "cannot allocate memory for %s\n", name);
  6615. }
  6616. return vec[id];
  6617. }
  6618. static inline int decode_seq_parameter_set(H264Context *h){
  6619. MpegEncContext * const s = &h->s;
  6620. int profile_idc, level_idc;
  6621. unsigned int sps_id, tmp, mb_width, mb_height;
  6622. int i;
  6623. SPS *sps;
  6624. profile_idc= get_bits(&s->gb, 8);
  6625. get_bits1(&s->gb); //constraint_set0_flag
  6626. get_bits1(&s->gb); //constraint_set1_flag
  6627. get_bits1(&s->gb); //constraint_set2_flag
  6628. get_bits1(&s->gb); //constraint_set3_flag
  6629. get_bits(&s->gb, 4); // reserved
  6630. level_idc= get_bits(&s->gb, 8);
  6631. sps_id= get_ue_golomb(&s->gb);
  6632. sps = alloc_parameter_set(h, (void **)h->sps_buffers, sps_id, MAX_SPS_COUNT, sizeof(SPS), "sps");
  6633. if(sps == NULL)
  6634. return -1;
  6635. sps->profile_idc= profile_idc;
  6636. sps->level_idc= level_idc;
  6637. if(sps->profile_idc >= 100){ //high profile
  6638. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6639. get_bits1(&s->gb); //residual_color_transform_flag
  6640. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6641. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6642. sps->transform_bypass = get_bits1(&s->gb);
  6643. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6644. }else
  6645. sps->scaling_matrix_present = 0;
  6646. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6647. sps->poc_type= get_ue_golomb(&s->gb);
  6648. if(sps->poc_type == 0){ //FIXME #define
  6649. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6650. } else if(sps->poc_type == 1){//FIXME #define
  6651. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6652. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6653. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6654. tmp= get_ue_golomb(&s->gb);
  6655. if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
  6656. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
  6657. return -1;
  6658. }
  6659. sps->poc_cycle_length= tmp;
  6660. for(i=0; i<sps->poc_cycle_length; i++)
  6661. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6662. }else if(sps->poc_type != 2){
  6663. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6664. return -1;
  6665. }
  6666. tmp= get_ue_golomb(&s->gb);
  6667. if(tmp > MAX_PICTURE_COUNT-2){
  6668. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6669. }
  6670. sps->ref_frame_count= tmp;
  6671. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6672. mb_width= get_ue_golomb(&s->gb) + 1;
  6673. mb_height= get_ue_golomb(&s->gb) + 1;
  6674. if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
  6675. avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
  6676. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6677. return -1;
  6678. }
  6679. sps->mb_width = mb_width;
  6680. sps->mb_height= mb_height;
  6681. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6682. if(!sps->frame_mbs_only_flag)
  6683. sps->mb_aff= get_bits1(&s->gb);
  6684. else
  6685. sps->mb_aff= 0;
  6686. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6687. #ifndef ALLOW_INTERLACE
  6688. if(sps->mb_aff)
  6689. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6690. #endif
  6691. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6692. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6693. sps->crop= get_bits1(&s->gb);
  6694. if(sps->crop){
  6695. sps->crop_left = get_ue_golomb(&s->gb);
  6696. sps->crop_right = get_ue_golomb(&s->gb);
  6697. sps->crop_top = get_ue_golomb(&s->gb);
  6698. sps->crop_bottom= get_ue_golomb(&s->gb);
  6699. if(sps->crop_left || sps->crop_top){
  6700. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6701. }
  6702. }else{
  6703. sps->crop_left =
  6704. sps->crop_right =
  6705. sps->crop_top =
  6706. sps->crop_bottom= 0;
  6707. }
  6708. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6709. if( sps->vui_parameters_present_flag )
  6710. decode_vui_parameters(h, sps);
  6711. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6712. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  6713. sps_id, sps->profile_idc, sps->level_idc,
  6714. sps->poc_type,
  6715. sps->ref_frame_count,
  6716. sps->mb_width, sps->mb_height,
  6717. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6718. sps->direct_8x8_inference_flag ? "8B8" : "",
  6719. sps->crop_left, sps->crop_right,
  6720. sps->crop_top, sps->crop_bottom,
  6721. sps->vui_parameters_present_flag ? "VUI" : ""
  6722. );
  6723. }
  6724. return 0;
  6725. }
  6726. static void
  6727. build_qp_table(PPS *pps, int t, int index)
  6728. {
  6729. int i;
  6730. for(i = 0; i < 255; i++)
  6731. pps->chroma_qp_table[t][i & 0xff] = chroma_qp[av_clip(i + index, 0, 51)];
  6732. }
  6733. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6734. MpegEncContext * const s = &h->s;
  6735. unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
  6736. PPS *pps;
  6737. pps = alloc_parameter_set(h, (void **)h->pps_buffers, pps_id, MAX_PPS_COUNT, sizeof(PPS), "pps");
  6738. if(pps == NULL)
  6739. return -1;
  6740. tmp= get_ue_golomb(&s->gb);
  6741. if(tmp>=MAX_SPS_COUNT || h->sps_buffers[tmp] == NULL){
  6742. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6743. return -1;
  6744. }
  6745. pps->sps_id= tmp;
  6746. pps->cabac= get_bits1(&s->gb);
  6747. pps->pic_order_present= get_bits1(&s->gb);
  6748. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6749. if(pps->slice_group_count > 1 ){
  6750. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6751. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6752. switch(pps->mb_slice_group_map_type){
  6753. case 0:
  6754. #if 0
  6755. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6756. | run_length[ i ] |1 |ue(v) |
  6757. #endif
  6758. break;
  6759. case 2:
  6760. #if 0
  6761. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6762. |{ | | |
  6763. | top_left_mb[ i ] |1 |ue(v) |
  6764. | bottom_right_mb[ i ] |1 |ue(v) |
  6765. | } | | |
  6766. #endif
  6767. break;
  6768. case 3:
  6769. case 4:
  6770. case 5:
  6771. #if 0
  6772. | slice_group_change_direction_flag |1 |u(1) |
  6773. | slice_group_change_rate_minus1 |1 |ue(v) |
  6774. #endif
  6775. break;
  6776. case 6:
  6777. #if 0
  6778. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6779. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6780. |) | | |
  6781. | slice_group_id[ i ] |1 |u(v) |
  6782. #endif
  6783. break;
  6784. }
  6785. }
  6786. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6787. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6788. if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
  6789. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6790. pps->ref_count[0]= pps->ref_count[1]= 1;
  6791. return -1;
  6792. }
  6793. pps->weighted_pred= get_bits1(&s->gb);
  6794. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6795. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6796. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6797. pps->chroma_qp_index_offset[0]= get_se_golomb(&s->gb);
  6798. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6799. pps->constrained_intra_pred= get_bits1(&s->gb);
  6800. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6801. pps->transform_8x8_mode= 0;
  6802. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6803. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6804. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6805. if(get_bits_count(&s->gb) < bit_length){
  6806. pps->transform_8x8_mode= get_bits1(&s->gb);
  6807. decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6808. pps->chroma_qp_index_offset[1]= get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6809. } else {
  6810. pps->chroma_qp_index_offset[1]= pps->chroma_qp_index_offset[0];
  6811. }
  6812. build_qp_table(pps, 0, pps->chroma_qp_index_offset[0]);
  6813. if(pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1]) {
  6814. build_qp_table(pps, 1, pps->chroma_qp_index_offset[1]);
  6815. h->pps.chroma_qp_diff= 1;
  6816. } else
  6817. memcpy(pps->chroma_qp_table[1], pps->chroma_qp_table[0], 256);
  6818. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6819. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%u sps:%u %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d/%d %s %s %s %s\n",
  6820. pps_id, pps->sps_id,
  6821. pps->cabac ? "CABAC" : "CAVLC",
  6822. pps->slice_group_count,
  6823. pps->ref_count[0], pps->ref_count[1],
  6824. pps->weighted_pred ? "weighted" : "",
  6825. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
  6826. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6827. pps->constrained_intra_pred ? "CONSTR" : "",
  6828. pps->redundant_pic_cnt_present ? "REDU" : "",
  6829. pps->transform_8x8_mode ? "8x8DCT" : ""
  6830. );
  6831. }
  6832. return 0;
  6833. }
  6834. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  6835. MpegEncContext * const s = &h->s;
  6836. AVCodecContext * const avctx= s->avctx;
  6837. int buf_index=0;
  6838. #if 0
  6839. int i;
  6840. for(i=0; i<50; i++){
  6841. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  6842. }
  6843. #endif
  6844. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  6845. h->slice_num = 0;
  6846. s->current_picture_ptr= NULL;
  6847. }
  6848. for(;;){
  6849. int consumed;
  6850. int dst_length;
  6851. int bit_length;
  6852. uint8_t *ptr;
  6853. int i, nalsize = 0;
  6854. if(h->is_avc) {
  6855. if(buf_index >= buf_size) break;
  6856. nalsize = 0;
  6857. for(i = 0; i < h->nal_length_size; i++)
  6858. nalsize = (nalsize << 8) | buf[buf_index++];
  6859. if(nalsize <= 1 || (nalsize+buf_index > buf_size)){
  6860. if(nalsize == 1){
  6861. buf_index++;
  6862. continue;
  6863. }else{
  6864. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  6865. break;
  6866. }
  6867. }
  6868. } else {
  6869. // start code prefix search
  6870. for(; buf_index + 3 < buf_size; buf_index++){
  6871. // This should always succeed in the first iteration.
  6872. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  6873. break;
  6874. }
  6875. if(buf_index+3 >= buf_size) break;
  6876. buf_index+=3;
  6877. }
  6878. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  6879. if (ptr==NULL || dst_length < 0){
  6880. return -1;
  6881. }
  6882. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  6883. dst_length--;
  6884. bit_length= !dst_length ? 0 : (8*dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1));
  6885. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  6886. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", h->nal_unit_type, buf_index, buf_size, dst_length);
  6887. }
  6888. if (h->is_avc && (nalsize != consumed))
  6889. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  6890. buf_index += consumed;
  6891. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
  6892. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  6893. continue;
  6894. switch(h->nal_unit_type){
  6895. case NAL_IDR_SLICE:
  6896. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  6897. case NAL_SLICE:
  6898. init_get_bits(&s->gb, ptr, bit_length);
  6899. h->intra_gb_ptr=
  6900. h->inter_gb_ptr= &s->gb;
  6901. s->data_partitioning = 0;
  6902. if(decode_slice_header(h) < 0){
  6903. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6904. break;
  6905. }
  6906. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  6907. if(h->redundant_pic_count==0 && s->hurry_up < 5
  6908. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6909. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6910. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6911. && avctx->skip_frame < AVDISCARD_ALL)
  6912. decode_slice(h);
  6913. break;
  6914. case NAL_DPA:
  6915. init_get_bits(&s->gb, ptr, bit_length);
  6916. h->intra_gb_ptr=
  6917. h->inter_gb_ptr= NULL;
  6918. s->data_partitioning = 1;
  6919. if(decode_slice_header(h) < 0){
  6920. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6921. }
  6922. break;
  6923. case NAL_DPB:
  6924. init_get_bits(&h->intra_gb, ptr, bit_length);
  6925. h->intra_gb_ptr= &h->intra_gb;
  6926. break;
  6927. case NAL_DPC:
  6928. init_get_bits(&h->inter_gb, ptr, bit_length);
  6929. h->inter_gb_ptr= &h->inter_gb;
  6930. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  6931. && s->context_initialized
  6932. && s->hurry_up < 5
  6933. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6934. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6935. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6936. && avctx->skip_frame < AVDISCARD_ALL)
  6937. decode_slice(h);
  6938. break;
  6939. case NAL_SEI:
  6940. init_get_bits(&s->gb, ptr, bit_length);
  6941. decode_sei(h);
  6942. break;
  6943. case NAL_SPS:
  6944. init_get_bits(&s->gb, ptr, bit_length);
  6945. decode_seq_parameter_set(h);
  6946. if(s->flags& CODEC_FLAG_LOW_DELAY)
  6947. s->low_delay=1;
  6948. if(avctx->has_b_frames < 2)
  6949. avctx->has_b_frames= !s->low_delay;
  6950. break;
  6951. case NAL_PPS:
  6952. init_get_bits(&s->gb, ptr, bit_length);
  6953. decode_picture_parameter_set(h, bit_length);
  6954. break;
  6955. case NAL_AUD:
  6956. case NAL_END_SEQUENCE:
  6957. case NAL_END_STREAM:
  6958. case NAL_FILLER_DATA:
  6959. case NAL_SPS_EXT:
  6960. case NAL_AUXILIARY_SLICE:
  6961. break;
  6962. default:
  6963. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d (%d bits)\n", h->nal_unit_type, bit_length);
  6964. }
  6965. }
  6966. return buf_index;
  6967. }
  6968. /**
  6969. * returns the number of bytes consumed for building the current frame
  6970. */
  6971. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  6972. if(s->flags&CODEC_FLAG_TRUNCATED){
  6973. pos -= s->parse_context.last_index;
  6974. if(pos<0) pos=0; // FIXME remove (unneeded?)
  6975. return pos;
  6976. }else{
  6977. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  6978. if(pos+10>buf_size) pos=buf_size; // oops ;)
  6979. return pos;
  6980. }
  6981. }
  6982. static int decode_frame(AVCodecContext *avctx,
  6983. void *data, int *data_size,
  6984. uint8_t *buf, int buf_size)
  6985. {
  6986. H264Context *h = avctx->priv_data;
  6987. MpegEncContext *s = &h->s;
  6988. AVFrame *pict = data;
  6989. int buf_index;
  6990. s->flags= avctx->flags;
  6991. s->flags2= avctx->flags2;
  6992. /* no supplementary picture */
  6993. if (buf_size == 0) {
  6994. Picture *out;
  6995. int i, out_idx;
  6996. //FIXME factorize this with the output code below
  6997. out = h->delayed_pic[0];
  6998. out_idx = 0;
  6999. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7000. if(h->delayed_pic[i]->poc < out->poc){
  7001. out = h->delayed_pic[i];
  7002. out_idx = i;
  7003. }
  7004. for(i=out_idx; h->delayed_pic[i]; i++)
  7005. h->delayed_pic[i] = h->delayed_pic[i+1];
  7006. if(out){
  7007. *data_size = sizeof(AVFrame);
  7008. *pict= *(AVFrame*)out;
  7009. }
  7010. return 0;
  7011. }
  7012. if(s->flags&CODEC_FLAG_TRUNCATED){
  7013. int next= ff_h264_find_frame_end(h, buf, buf_size);
  7014. if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  7015. return buf_size;
  7016. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7017. }
  7018. if(h->is_avc && !h->got_avcC) {
  7019. int i, cnt, nalsize;
  7020. unsigned char *p = avctx->extradata;
  7021. if(avctx->extradata_size < 7) {
  7022. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7023. return -1;
  7024. }
  7025. if(*p != 1) {
  7026. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7027. return -1;
  7028. }
  7029. /* sps and pps in the avcC always have length coded with 2 bytes,
  7030. so put a fake nal_length_size = 2 while parsing them */
  7031. h->nal_length_size = 2;
  7032. // Decode sps from avcC
  7033. cnt = *(p+5) & 0x1f; // Number of sps
  7034. p += 6;
  7035. for (i = 0; i < cnt; i++) {
  7036. nalsize = AV_RB16(p) + 2;
  7037. if(decode_nal_units(h, p, nalsize) < 0) {
  7038. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7039. return -1;
  7040. }
  7041. p += nalsize;
  7042. }
  7043. // Decode pps from avcC
  7044. cnt = *(p++); // Number of pps
  7045. for (i = 0; i < cnt; i++) {
  7046. nalsize = AV_RB16(p) + 2;
  7047. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7048. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7049. return -1;
  7050. }
  7051. p += nalsize;
  7052. }
  7053. // Now store right nal length size, that will be use to parse all other nals
  7054. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7055. // Do not reparse avcC
  7056. h->got_avcC = 1;
  7057. }
  7058. if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
  7059. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7060. return -1;
  7061. }
  7062. buf_index=decode_nal_units(h, buf, buf_size);
  7063. if(buf_index < 0)
  7064. return -1;
  7065. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  7066. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  7067. return -1;
  7068. }
  7069. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  7070. Picture *out = s->current_picture_ptr;
  7071. Picture *cur = s->current_picture_ptr;
  7072. Picture *prev = h->delayed_output_pic;
  7073. int i, pics, cross_idr, out_of_order, out_idx;
  7074. s->mb_y= 0;
  7075. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7076. s->current_picture_ptr->pict_type= s->pict_type;
  7077. h->prev_frame_num_offset= h->frame_num_offset;
  7078. h->prev_frame_num= h->frame_num;
  7079. if(s->current_picture_ptr->reference){
  7080. h->prev_poc_msb= h->poc_msb;
  7081. h->prev_poc_lsb= h->poc_lsb;
  7082. }
  7083. if(s->current_picture_ptr->reference)
  7084. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7085. ff_er_frame_end(s);
  7086. MPV_frame_end(s);
  7087. //FIXME do something with unavailable reference frames
  7088. #if 0 //decode order
  7089. *data_size = sizeof(AVFrame);
  7090. #else
  7091. /* Sort B-frames into display order */
  7092. if(h->sps.bitstream_restriction_flag
  7093. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7094. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7095. s->low_delay = 0;
  7096. }
  7097. pics = 0;
  7098. while(h->delayed_pic[pics]) pics++;
  7099. assert(pics+1 < sizeof(h->delayed_pic) / sizeof(h->delayed_pic[0]));
  7100. h->delayed_pic[pics++] = cur;
  7101. if(cur->reference == 0)
  7102. cur->reference = 1;
  7103. cross_idr = 0;
  7104. for(i=0; h->delayed_pic[i]; i++)
  7105. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7106. cross_idr = 1;
  7107. out = h->delayed_pic[0];
  7108. out_idx = 0;
  7109. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7110. if(h->delayed_pic[i]->poc < out->poc){
  7111. out = h->delayed_pic[i];
  7112. out_idx = i;
  7113. }
  7114. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7115. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7116. { }
  7117. else if(prev && pics <= s->avctx->has_b_frames)
  7118. out = prev;
  7119. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7120. || (s->low_delay &&
  7121. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7122. || cur->pict_type == B_TYPE)))
  7123. {
  7124. s->low_delay = 0;
  7125. s->avctx->has_b_frames++;
  7126. out = prev;
  7127. }
  7128. else if(out_of_order)
  7129. out = prev;
  7130. if(out_of_order || pics > s->avctx->has_b_frames){
  7131. for(i=out_idx; h->delayed_pic[i]; i++)
  7132. h->delayed_pic[i] = h->delayed_pic[i+1];
  7133. }
  7134. if(prev == out)
  7135. *data_size = 0;
  7136. else
  7137. *data_size = sizeof(AVFrame);
  7138. if(prev && prev != out && prev->reference == 1)
  7139. prev->reference = 0;
  7140. h->delayed_output_pic = out;
  7141. #endif
  7142. if(out)
  7143. *pict= *(AVFrame*)out;
  7144. else
  7145. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7146. }
  7147. assert(pict->data[0] || !*data_size);
  7148. ff_print_debug_info(s, pict);
  7149. //printf("out %d\n", (int)pict->data[0]);
  7150. #if 0 //?
  7151. /* Return the Picture timestamp as the frame number */
  7152. /* we substract 1 because it is added on utils.c */
  7153. avctx->frame_number = s->picture_number - 1;
  7154. #endif
  7155. return get_consumed_bytes(s, buf_index, buf_size);
  7156. }
  7157. #if 0
  7158. static inline void fill_mb_avail(H264Context *h){
  7159. MpegEncContext * const s = &h->s;
  7160. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7161. if(s->mb_y){
  7162. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7163. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7164. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7165. }else{
  7166. h->mb_avail[0]=
  7167. h->mb_avail[1]=
  7168. h->mb_avail[2]= 0;
  7169. }
  7170. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7171. h->mb_avail[4]= 1; //FIXME move out
  7172. h->mb_avail[5]= 0; //FIXME move out
  7173. }
  7174. #endif
  7175. #if 0 //selftest
  7176. #undef random
  7177. #define COUNT 8000
  7178. #define SIZE (COUNT*40)
  7179. int main(){
  7180. int i;
  7181. uint8_t temp[SIZE];
  7182. PutBitContext pb;
  7183. GetBitContext gb;
  7184. // int int_temp[10000];
  7185. DSPContext dsp;
  7186. AVCodecContext avctx;
  7187. dsputil_init(&dsp, &avctx);
  7188. init_put_bits(&pb, temp, SIZE);
  7189. printf("testing unsigned exp golomb\n");
  7190. for(i=0; i<COUNT; i++){
  7191. START_TIMER
  7192. set_ue_golomb(&pb, i);
  7193. STOP_TIMER("set_ue_golomb");
  7194. }
  7195. flush_put_bits(&pb);
  7196. init_get_bits(&gb, temp, 8*SIZE);
  7197. for(i=0; i<COUNT; i++){
  7198. int j, s;
  7199. s= show_bits(&gb, 24);
  7200. START_TIMER
  7201. j= get_ue_golomb(&gb);
  7202. if(j != i){
  7203. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7204. // return -1;
  7205. }
  7206. STOP_TIMER("get_ue_golomb");
  7207. }
  7208. init_put_bits(&pb, temp, SIZE);
  7209. printf("testing signed exp golomb\n");
  7210. for(i=0; i<COUNT; i++){
  7211. START_TIMER
  7212. set_se_golomb(&pb, i - COUNT/2);
  7213. STOP_TIMER("set_se_golomb");
  7214. }
  7215. flush_put_bits(&pb);
  7216. init_get_bits(&gb, temp, 8*SIZE);
  7217. for(i=0; i<COUNT; i++){
  7218. int j, s;
  7219. s= show_bits(&gb, 24);
  7220. START_TIMER
  7221. j= get_se_golomb(&gb);
  7222. if(j != i - COUNT/2){
  7223. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7224. // return -1;
  7225. }
  7226. STOP_TIMER("get_se_golomb");
  7227. }
  7228. printf("testing 4x4 (I)DCT\n");
  7229. DCTELEM block[16];
  7230. uint8_t src[16], ref[16];
  7231. uint64_t error= 0, max_error=0;
  7232. for(i=0; i<COUNT; i++){
  7233. int j;
  7234. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7235. for(j=0; j<16; j++){
  7236. ref[j]= random()%255;
  7237. src[j]= random()%255;
  7238. }
  7239. h264_diff_dct_c(block, src, ref, 4);
  7240. //normalize
  7241. for(j=0; j<16; j++){
  7242. // printf("%d ", block[j]);
  7243. block[j]= block[j]*4;
  7244. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7245. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7246. }
  7247. // printf("\n");
  7248. s->dsp.h264_idct_add(ref, block, 4);
  7249. /* for(j=0; j<16; j++){
  7250. printf("%d ", ref[j]);
  7251. }
  7252. printf("\n");*/
  7253. for(j=0; j<16; j++){
  7254. int diff= FFABS(src[j] - ref[j]);
  7255. error+= diff*diff;
  7256. max_error= FFMAX(max_error, diff);
  7257. }
  7258. }
  7259. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7260. #if 0
  7261. printf("testing quantizer\n");
  7262. for(qp=0; qp<52; qp++){
  7263. for(i=0; i<16; i++)
  7264. src1_block[i]= src2_block[i]= random()%255;
  7265. }
  7266. #endif
  7267. printf("Testing NAL layer\n");
  7268. uint8_t bitstream[COUNT];
  7269. uint8_t nal[COUNT*2];
  7270. H264Context h;
  7271. memset(&h, 0, sizeof(H264Context));
  7272. for(i=0; i<COUNT; i++){
  7273. int zeros= i;
  7274. int nal_length;
  7275. int consumed;
  7276. int out_length;
  7277. uint8_t *out;
  7278. int j;
  7279. for(j=0; j<COUNT; j++){
  7280. bitstream[j]= (random() % 255) + 1;
  7281. }
  7282. for(j=0; j<zeros; j++){
  7283. int pos= random() % COUNT;
  7284. while(bitstream[pos] == 0){
  7285. pos++;
  7286. pos %= COUNT;
  7287. }
  7288. bitstream[pos]=0;
  7289. }
  7290. START_TIMER
  7291. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7292. if(nal_length<0){
  7293. printf("encoding failed\n");
  7294. return -1;
  7295. }
  7296. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7297. STOP_TIMER("NAL")
  7298. if(out_length != COUNT){
  7299. printf("incorrect length %d %d\n", out_length, COUNT);
  7300. return -1;
  7301. }
  7302. if(consumed != nal_length){
  7303. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7304. return -1;
  7305. }
  7306. if(memcmp(bitstream, out, COUNT)){
  7307. printf("mismatch\n");
  7308. return -1;
  7309. }
  7310. }
  7311. printf("Testing RBSP\n");
  7312. return 0;
  7313. }
  7314. #endif
  7315. static int decode_end(AVCodecContext *avctx)
  7316. {
  7317. H264Context *h = avctx->priv_data;
  7318. MpegEncContext *s = &h->s;
  7319. av_freep(&h->rbsp_buffer[0]);
  7320. av_freep(&h->rbsp_buffer[1]);
  7321. free_tables(h); //FIXME cleanup init stuff perhaps
  7322. MPV_common_end(s);
  7323. // memset(h, 0, sizeof(H264Context));
  7324. return 0;
  7325. }
  7326. AVCodec h264_decoder = {
  7327. "h264",
  7328. CODEC_TYPE_VIDEO,
  7329. CODEC_ID_H264,
  7330. sizeof(H264Context),
  7331. decode_init,
  7332. NULL,
  7333. decode_end,
  7334. decode_frame,
  7335. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7336. .flush= flush_dpb,
  7337. };
  7338. #include "svq3.c"