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.

8018 lines
309KB

  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 "rectangle.h"
  34. #include "cabac.h"
  35. #ifdef ARCH_X86
  36. #include "i386/h264_i386.h"
  37. #endif
  38. //#undef NDEBUG
  39. #include <assert.h>
  40. /**
  41. * Value of Picture.reference when Picture is not a reference picture, but
  42. * is held for delayed output.
  43. */
  44. #define DELAYED_PIC_REF 4
  45. static VLC coeff_token_vlc[4];
  46. static VLC chroma_dc_coeff_token_vlc;
  47. static VLC total_zeros_vlc[15];
  48. static VLC chroma_dc_total_zeros_vlc[3];
  49. static VLC run_vlc[6];
  50. static VLC run7_vlc;
  51. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  52. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  53. 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);
  54. 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);
  55. static Picture * remove_long(H264Context *h, int i, int ref_mask);
  56. static av_always_inline uint32_t pack16to32(int a, int b){
  57. #ifdef WORDS_BIGENDIAN
  58. return (b&0xFFFF) + (a<<16);
  59. #else
  60. return (a&0xFFFF) + (b<<16);
  61. #endif
  62. }
  63. const uint8_t ff_rem6[52]={
  64. 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,
  65. };
  66. const uint8_t ff_div6[52]={
  67. 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,
  68. };
  69. static const int left_block_options[4][8]={
  70. {0,1,2,3,7,10,8,11},
  71. {2,2,3,3,8,11,8,11},
  72. {0,0,1,1,7,10,7,10},
  73. {0,2,0,2,7,10,7,10}
  74. };
  75. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  76. MpegEncContext * const s = &h->s;
  77. const int mb_xy= h->mb_xy;
  78. int topleft_xy, top_xy, topright_xy, left_xy[2];
  79. int topleft_type, top_type, topright_type, left_type[2];
  80. int * left_block;
  81. int topleft_partition= -1;
  82. int i;
  83. top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  84. //FIXME deblocking could skip the intra and nnz parts.
  85. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)
  86. return;
  87. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  88. * stuff, I can't imagine that these complex rules are worth it. */
  89. topleft_xy = top_xy - 1;
  90. topright_xy= top_xy + 1;
  91. left_xy[1] = left_xy[0] = mb_xy-1;
  92. left_block = left_block_options[0];
  93. if(FRAME_MBAFF){
  94. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  95. const int top_pair_xy = pair_xy - s->mb_stride;
  96. const int topleft_pair_xy = top_pair_xy - 1;
  97. const int topright_pair_xy = top_pair_xy + 1;
  98. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  99. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  100. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  101. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  102. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  103. const int bottom = (s->mb_y & 1);
  104. 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);
  105. if (bottom
  106. ? !curr_mb_frame_flag // bottom macroblock
  107. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  108. ) {
  109. top_xy -= s->mb_stride;
  110. }
  111. if (bottom
  112. ? !curr_mb_frame_flag // bottom macroblock
  113. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  114. ) {
  115. topleft_xy -= s->mb_stride;
  116. } else if(bottom && curr_mb_frame_flag && !left_mb_frame_flag) {
  117. topleft_xy += s->mb_stride;
  118. // take top left mv from the middle of the mb, as opposed to all other modes which use the bottom right partition
  119. topleft_partition = 0;
  120. }
  121. if (bottom
  122. ? !curr_mb_frame_flag // bottom macroblock
  123. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  124. ) {
  125. topright_xy -= s->mb_stride;
  126. }
  127. if (left_mb_frame_flag != curr_mb_frame_flag) {
  128. left_xy[1] = left_xy[0] = pair_xy - 1;
  129. if (curr_mb_frame_flag) {
  130. if (bottom) {
  131. left_block = left_block_options[1];
  132. } else {
  133. left_block= left_block_options[2];
  134. }
  135. } else {
  136. left_xy[1] += s->mb_stride;
  137. left_block = left_block_options[3];
  138. }
  139. }
  140. }
  141. h->top_mb_xy = top_xy;
  142. h->left_mb_xy[0] = left_xy[0];
  143. h->left_mb_xy[1] = left_xy[1];
  144. if(for_deblock){
  145. topleft_type = 0;
  146. topright_type = 0;
  147. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  148. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  149. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  150. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  151. int list;
  152. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  153. for(i=0; i<16; i++)
  154. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  155. for(list=0; list<h->list_count; list++){
  156. if(USES_LIST(mb_type,list)){
  157. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  158. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  159. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  160. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  161. dst[0] = src[0];
  162. dst[1] = src[1];
  163. dst[2] = src[2];
  164. dst[3] = src[3];
  165. }
  166. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  167. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  168. ref += h->b8_stride;
  169. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  170. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  171. }else{
  172. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  173. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  174. }
  175. }
  176. }
  177. }else{
  178. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  179. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  180. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  181. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  182. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  183. }
  184. if(IS_INTRA(mb_type)){
  185. h->topleft_samples_available=
  186. h->top_samples_available=
  187. h->left_samples_available= 0xFFFF;
  188. h->topright_samples_available= 0xEEEA;
  189. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  190. h->topleft_samples_available= 0xB3FF;
  191. h->top_samples_available= 0x33FF;
  192. h->topright_samples_available= 0x26EA;
  193. }
  194. for(i=0; i<2; i++){
  195. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  196. h->topleft_samples_available&= 0xDF5F;
  197. h->left_samples_available&= 0x5F5F;
  198. }
  199. }
  200. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  201. h->topleft_samples_available&= 0x7FFF;
  202. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  203. h->topright_samples_available&= 0xFBFF;
  204. if(IS_INTRA4x4(mb_type)){
  205. if(IS_INTRA4x4(top_type)){
  206. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  207. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  208. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  209. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  210. }else{
  211. int pred;
  212. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  213. pred= -1;
  214. else{
  215. pred= 2;
  216. }
  217. h->intra4x4_pred_mode_cache[4+8*0]=
  218. h->intra4x4_pred_mode_cache[5+8*0]=
  219. h->intra4x4_pred_mode_cache[6+8*0]=
  220. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  221. }
  222. for(i=0; i<2; i++){
  223. if(IS_INTRA4x4(left_type[i])){
  224. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  225. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  226. }else{
  227. int pred;
  228. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  229. pred= -1;
  230. else{
  231. pred= 2;
  232. }
  233. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  234. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  235. }
  236. }
  237. }
  238. }
  239. /*
  240. 0 . T T. T T T T
  241. 1 L . .L . . . .
  242. 2 L . .L . . . .
  243. 3 . T TL . . . .
  244. 4 L . .L . . . .
  245. 5 L . .. . . . .
  246. */
  247. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  248. if(top_type){
  249. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  250. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  251. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  252. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  253. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  254. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  255. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  256. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  257. }else{
  258. h->non_zero_count_cache[4+8*0]=
  259. h->non_zero_count_cache[5+8*0]=
  260. h->non_zero_count_cache[6+8*0]=
  261. h->non_zero_count_cache[7+8*0]=
  262. h->non_zero_count_cache[1+8*0]=
  263. h->non_zero_count_cache[2+8*0]=
  264. h->non_zero_count_cache[1+8*3]=
  265. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  266. }
  267. for (i=0; i<2; i++) {
  268. if(left_type[i]){
  269. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  270. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  271. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  272. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  273. }else{
  274. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  275. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  276. h->non_zero_count_cache[0+8*1 + 8*i]=
  277. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  278. }
  279. }
  280. if( h->pps.cabac ) {
  281. // top_cbp
  282. if(top_type) {
  283. h->top_cbp = h->cbp_table[top_xy];
  284. } else if(IS_INTRA(mb_type)) {
  285. h->top_cbp = 0x1C0;
  286. } else {
  287. h->top_cbp = 0;
  288. }
  289. // left_cbp
  290. if (left_type[0]) {
  291. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  292. } else if(IS_INTRA(mb_type)) {
  293. h->left_cbp = 0x1C0;
  294. } else {
  295. h->left_cbp = 0;
  296. }
  297. if (left_type[0]) {
  298. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  299. }
  300. if (left_type[1]) {
  301. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  302. }
  303. }
  304. #if 1
  305. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  306. int list;
  307. for(list=0; list<h->list_count; list++){
  308. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  309. /*if(!h->mv_cache_clean[list]){
  310. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  311. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  312. h->mv_cache_clean[list]= 1;
  313. }*/
  314. continue;
  315. }
  316. h->mv_cache_clean[list]= 0;
  317. if(USES_LIST(top_type, list)){
  318. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  319. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  320. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  321. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  322. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  323. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  324. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  325. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  326. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  327. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  328. }else{
  329. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  330. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  331. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  332. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  333. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  334. }
  335. for(i=0; i<2; i++){
  336. int cache_idx = scan8[0] - 1 + i*2*8;
  337. if(USES_LIST(left_type[i], list)){
  338. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  339. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  340. *(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]];
  341. *(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]];
  342. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  343. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  344. }else{
  345. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  346. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  347. h->ref_cache[list][cache_idx ]=
  348. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  349. }
  350. }
  351. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  352. continue;
  353. if(USES_LIST(topleft_type, list)){
  354. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
  355. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
  356. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  357. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  358. }else{
  359. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  360. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  361. }
  362. if(USES_LIST(topright_type, list)){
  363. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  364. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  365. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  366. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  367. }else{
  368. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  369. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  370. }
  371. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  372. continue;
  373. h->ref_cache[list][scan8[5 ]+1] =
  374. h->ref_cache[list][scan8[7 ]+1] =
  375. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  376. h->ref_cache[list][scan8[4 ]] =
  377. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  378. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  379. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  380. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  381. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  382. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  383. if( h->pps.cabac ) {
  384. /* XXX beurk, Load mvd */
  385. if(USES_LIST(top_type, list)){
  386. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  387. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  388. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  389. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  390. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  391. }else{
  392. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  393. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  394. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  395. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  396. }
  397. if(USES_LIST(left_type[0], list)){
  398. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  399. *(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]];
  400. *(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]];
  401. }else{
  402. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  403. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  404. }
  405. if(USES_LIST(left_type[1], list)){
  406. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  407. *(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]];
  408. *(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]];
  409. }else{
  410. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  411. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  412. }
  413. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  414. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  415. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  416. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  417. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  418. if(h->slice_type_nos == FF_B_TYPE){
  419. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  420. if(IS_DIRECT(top_type)){
  421. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  422. }else if(IS_8X8(top_type)){
  423. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  424. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  425. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  426. }else{
  427. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  428. }
  429. if(IS_DIRECT(left_type[0]))
  430. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  431. else if(IS_8X8(left_type[0]))
  432. 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)];
  433. else
  434. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  435. if(IS_DIRECT(left_type[1]))
  436. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  437. else if(IS_8X8(left_type[1]))
  438. 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)];
  439. else
  440. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  441. }
  442. }
  443. if(FRAME_MBAFF){
  444. #define MAP_MVS\
  445. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  446. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  447. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  448. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  449. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  450. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  451. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  452. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  453. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  454. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  455. if(MB_FIELD){
  456. #define MAP_F2F(idx, mb_type)\
  457. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  458. h->ref_cache[list][idx] <<= 1;\
  459. h->mv_cache[list][idx][1] /= 2;\
  460. h->mvd_cache[list][idx][1] /= 2;\
  461. }
  462. MAP_MVS
  463. #undef MAP_F2F
  464. }else{
  465. #define MAP_F2F(idx, mb_type)\
  466. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  467. h->ref_cache[list][idx] >>= 1;\
  468. h->mv_cache[list][idx][1] <<= 1;\
  469. h->mvd_cache[list][idx][1] <<= 1;\
  470. }
  471. MAP_MVS
  472. #undef MAP_F2F
  473. }
  474. }
  475. }
  476. }
  477. #endif
  478. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  479. }
  480. static inline void write_back_intra_pred_mode(H264Context *h){
  481. const int mb_xy= h->mb_xy;
  482. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  483. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  484. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  485. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  486. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  487. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  488. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  489. }
  490. /**
  491. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  492. */
  493. static inline int check_intra4x4_pred_mode(H264Context *h){
  494. MpegEncContext * const s = &h->s;
  495. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  496. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  497. int i;
  498. if(!(h->top_samples_available&0x8000)){
  499. for(i=0; i<4; i++){
  500. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  501. if(status<0){
  502. 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);
  503. return -1;
  504. } else if(status){
  505. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  506. }
  507. }
  508. }
  509. if(!(h->left_samples_available&0x8000)){
  510. for(i=0; i<4; i++){
  511. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  512. if(status<0){
  513. 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);
  514. return -1;
  515. } else if(status){
  516. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  517. }
  518. }
  519. }
  520. return 0;
  521. } //FIXME cleanup like next
  522. /**
  523. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  524. */
  525. static inline int check_intra_pred_mode(H264Context *h, int mode){
  526. MpegEncContext * const s = &h->s;
  527. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  528. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  529. if(mode > 6U) {
  530. 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);
  531. return -1;
  532. }
  533. if(!(h->top_samples_available&0x8000)){
  534. mode= top[ mode ];
  535. if(mode<0){
  536. 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);
  537. return -1;
  538. }
  539. }
  540. if(!(h->left_samples_available&0x8000)){
  541. mode= left[ mode ];
  542. if(mode<0){
  543. 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);
  544. return -1;
  545. }
  546. }
  547. return mode;
  548. }
  549. /**
  550. * gets the predicted intra4x4 prediction mode.
  551. */
  552. static inline int pred_intra_mode(H264Context *h, int n){
  553. const int index8= scan8[n];
  554. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  555. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  556. const int min= FFMIN(left, top);
  557. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  558. if(min<0) return DC_PRED;
  559. else return min;
  560. }
  561. static inline void write_back_non_zero_count(H264Context *h){
  562. const int mb_xy= h->mb_xy;
  563. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  564. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  565. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  566. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  567. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  568. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  569. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  570. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  571. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  572. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  573. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  574. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  575. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  576. if(FRAME_MBAFF){
  577. // store all luma nnzs, for deblocking
  578. int v = 0, i;
  579. for(i=0; i<16; i++)
  580. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  581. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  582. }
  583. }
  584. /**
  585. * gets the predicted number of non-zero coefficients.
  586. * @param n block index
  587. */
  588. static inline int pred_non_zero_count(H264Context *h, int n){
  589. const int index8= scan8[n];
  590. const int left= h->non_zero_count_cache[index8 - 1];
  591. const int top = h->non_zero_count_cache[index8 - 8];
  592. int i= left + top;
  593. if(i<64) i= (i+1)>>1;
  594. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  595. return i&31;
  596. }
  597. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  598. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  599. MpegEncContext *s = &h->s;
  600. /* there is no consistent mapping of mvs to neighboring locations that will
  601. * make mbaff happy, so we can't move all this logic to fill_caches */
  602. if(FRAME_MBAFF){
  603. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  604. const int16_t *mv;
  605. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  606. *C = h->mv_cache[list][scan8[0]-2];
  607. if(!MB_FIELD
  608. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  609. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  610. if(IS_INTERLACED(mb_types[topright_xy])){
  611. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  612. const int x4 = X4, y4 = Y4;\
  613. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  614. if(!USES_LIST(mb_type,list))\
  615. return LIST_NOT_USED;\
  616. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  617. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  618. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  619. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  620. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  621. }
  622. }
  623. if(topright_ref == PART_NOT_AVAILABLE
  624. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  625. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  626. if(!MB_FIELD
  627. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  628. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  629. }
  630. if(MB_FIELD
  631. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  632. && i >= scan8[0]+8){
  633. // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
  634. SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  635. }
  636. }
  637. #undef SET_DIAG_MV
  638. }
  639. if(topright_ref != PART_NOT_AVAILABLE){
  640. *C= h->mv_cache[list][ i - 8 + part_width ];
  641. return topright_ref;
  642. }else{
  643. tprintf(s->avctx, "topright MV not available\n");
  644. *C= h->mv_cache[list][ i - 8 - 1 ];
  645. return h->ref_cache[list][ i - 8 - 1 ];
  646. }
  647. }
  648. /**
  649. * gets the predicted MV.
  650. * @param n the block index
  651. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  652. * @param mx the x component of the predicted motion vector
  653. * @param my the y component of the predicted motion vector
  654. */
  655. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  656. const int index8= scan8[n];
  657. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  658. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  659. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  660. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  661. const int16_t * C;
  662. int diagonal_ref, match_count;
  663. assert(part_width==1 || part_width==2 || part_width==4);
  664. /* mv_cache
  665. B . . A T T T T
  666. U . . L . . , .
  667. U . . L . . . .
  668. U . . L . . , .
  669. . . . L . . . .
  670. */
  671. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  672. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  673. tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
  674. if(match_count > 1){ //most common
  675. *mx= mid_pred(A[0], B[0], C[0]);
  676. *my= mid_pred(A[1], B[1], C[1]);
  677. }else if(match_count==1){
  678. if(left_ref==ref){
  679. *mx= A[0];
  680. *my= A[1];
  681. }else if(top_ref==ref){
  682. *mx= B[0];
  683. *my= B[1];
  684. }else{
  685. *mx= C[0];
  686. *my= C[1];
  687. }
  688. }else{
  689. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  690. *mx= A[0];
  691. *my= A[1];
  692. }else{
  693. *mx= mid_pred(A[0], B[0], C[0]);
  694. *my= mid_pred(A[1], B[1], C[1]);
  695. }
  696. }
  697. 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);
  698. }
  699. /**
  700. * gets the directionally predicted 16x8 MV.
  701. * @param n the block index
  702. * @param mx the x component of the predicted motion vector
  703. * @param my the y component of the predicted motion vector
  704. */
  705. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  706. if(n==0){
  707. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  708. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  709. 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);
  710. if(top_ref == ref){
  711. *mx= B[0];
  712. *my= B[1];
  713. return;
  714. }
  715. }else{
  716. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  717. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  718. 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);
  719. if(left_ref == ref){
  720. *mx= A[0];
  721. *my= A[1];
  722. return;
  723. }
  724. }
  725. //RARE
  726. pred_motion(h, n, 4, list, ref, mx, my);
  727. }
  728. /**
  729. * gets the directionally predicted 8x16 MV.
  730. * @param n the block index
  731. * @param mx the x component of the predicted motion vector
  732. * @param my the y component of the predicted motion vector
  733. */
  734. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  735. if(n==0){
  736. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  737. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  738. 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);
  739. if(left_ref == ref){
  740. *mx= A[0];
  741. *my= A[1];
  742. return;
  743. }
  744. }else{
  745. const int16_t * C;
  746. int diagonal_ref;
  747. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  748. 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);
  749. if(diagonal_ref == ref){
  750. *mx= C[0];
  751. *my= C[1];
  752. return;
  753. }
  754. }
  755. //RARE
  756. pred_motion(h, n, 2, list, ref, mx, my);
  757. }
  758. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  759. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  760. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  761. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  762. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  763. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  764. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  765. *mx = *my = 0;
  766. return;
  767. }
  768. pred_motion(h, 0, 4, 0, 0, mx, my);
  769. return;
  770. }
  771. static inline void direct_dist_scale_factor(H264Context * const h){
  772. const int poc = h->s.current_picture_ptr->poc;
  773. const int poc1 = h->ref_list[1][0].poc;
  774. int i;
  775. for(i=0; i<h->ref_count[0]; i++){
  776. int poc0 = h->ref_list[0][i].poc;
  777. int td = av_clip(poc1 - poc0, -128, 127);
  778. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  779. h->dist_scale_factor[i] = 256;
  780. }else{
  781. int tb = av_clip(poc - poc0, -128, 127);
  782. int tx = (16384 + (FFABS(td) >> 1)) / td;
  783. h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
  784. }
  785. }
  786. if(FRAME_MBAFF){
  787. for(i=0; i<h->ref_count[0]; i++){
  788. h->dist_scale_factor_field[2*i] =
  789. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  790. }
  791. }
  792. }
  793. static inline void direct_ref_list_init(H264Context * const h){
  794. MpegEncContext * const s = &h->s;
  795. Picture * const ref1 = &h->ref_list[1][0];
  796. Picture * const cur = s->current_picture_ptr;
  797. int list, i, j;
  798. if(cur->pict_type == FF_I_TYPE)
  799. cur->ref_count[0] = 0;
  800. if(cur->pict_type != FF_B_TYPE)
  801. cur->ref_count[1] = 0;
  802. for(list=0; list<2; list++){
  803. cur->ref_count[list] = h->ref_count[list];
  804. for(j=0; j<h->ref_count[list]; j++)
  805. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  806. }
  807. if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
  808. return;
  809. for(list=0; list<2; list++){
  810. for(i=0; i<ref1->ref_count[list]; i++){
  811. const int poc = ref1->ref_poc[list][i];
  812. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  813. for(j=0; j<h->ref_count[list]; j++)
  814. if(h->ref_list[list][j].poc == poc){
  815. h->map_col_to_list0[list][i] = j;
  816. break;
  817. }
  818. }
  819. }
  820. if(FRAME_MBAFF){
  821. for(list=0; list<2; list++){
  822. for(i=0; i<ref1->ref_count[list]; i++){
  823. j = h->map_col_to_list0[list][i];
  824. h->map_col_to_list0_field[list][2*i] = 2*j;
  825. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  826. }
  827. }
  828. }
  829. }
  830. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  831. MpegEncContext * const s = &h->s;
  832. const int mb_xy = h->mb_xy;
  833. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  834. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  835. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  836. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  837. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  838. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  839. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  840. const int is_b8x8 = IS_8X8(*mb_type);
  841. unsigned int sub_mb_type;
  842. int i8, i4;
  843. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  844. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  845. /* FIXME save sub mb types from previous frames (or derive from MVs)
  846. * so we know exactly what block size to use */
  847. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  848. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  849. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  850. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  851. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  852. }else{
  853. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  854. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  855. }
  856. if(!is_b8x8)
  857. *mb_type |= MB_TYPE_DIRECT2;
  858. if(MB_FIELD)
  859. *mb_type |= MB_TYPE_INTERLACED;
  860. 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);
  861. if(h->direct_spatial_mv_pred){
  862. int ref[2];
  863. int mv[2][2];
  864. int list;
  865. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  866. /* ref = min(neighbors) */
  867. for(list=0; list<2; list++){
  868. int refa = h->ref_cache[list][scan8[0] - 1];
  869. int refb = h->ref_cache[list][scan8[0] - 8];
  870. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  871. if(refc == PART_NOT_AVAILABLE)
  872. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  873. ref[list] = FFMIN3((unsigned)refa, (unsigned)refb, (unsigned)refc);
  874. if(ref[list] < 0)
  875. ref[list] = -1;
  876. }
  877. if(ref[0] < 0 && ref[1] < 0){
  878. ref[0] = ref[1] = 0;
  879. mv[0][0] = mv[0][1] =
  880. mv[1][0] = mv[1][1] = 0;
  881. }else{
  882. for(list=0; list<2; list++){
  883. if(ref[list] >= 0)
  884. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  885. else
  886. mv[list][0] = mv[list][1] = 0;
  887. }
  888. }
  889. if(ref[1] < 0){
  890. if(!is_b8x8)
  891. *mb_type &= ~MB_TYPE_L1;
  892. sub_mb_type &= ~MB_TYPE_L1;
  893. }else if(ref[0] < 0){
  894. if(!is_b8x8)
  895. *mb_type &= ~MB_TYPE_L0;
  896. sub_mb_type &= ~MB_TYPE_L0;
  897. }
  898. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  899. int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  900. int mb_types_col[2];
  901. int b8_stride = h->b8_stride;
  902. int b4_stride = h->b_stride;
  903. *mb_type = (*mb_type & ~MB_TYPE_16x16) | MB_TYPE_8x8;
  904. if(IS_INTERLACED(*mb_type)){
  905. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  906. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  907. if(s->mb_y&1){
  908. l1ref0 -= 2*b8_stride;
  909. l1ref1 -= 2*b8_stride;
  910. l1mv0 -= 4*b4_stride;
  911. l1mv1 -= 4*b4_stride;
  912. }
  913. b8_stride *= 3;
  914. b4_stride *= 6;
  915. }else{
  916. int cur_poc = s->current_picture_ptr->poc;
  917. int *col_poc = h->ref_list[1]->field_poc;
  918. int col_parity = FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc);
  919. int dy = 2*col_parity - (s->mb_y&1);
  920. mb_types_col[0] =
  921. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy + col_parity*s->mb_stride];
  922. l1ref0 += dy*b8_stride;
  923. l1ref1 += dy*b8_stride;
  924. l1mv0 += 2*dy*b4_stride;
  925. l1mv1 += 2*dy*b4_stride;
  926. b8_stride = 0;
  927. }
  928. for(i8=0; i8<4; i8++){
  929. int x8 = i8&1;
  930. int y8 = i8>>1;
  931. int xy8 = x8+y8*b8_stride;
  932. int xy4 = 3*x8+y8*b4_stride;
  933. int a=0, b=0;
  934. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  935. continue;
  936. h->sub_mb_type[i8] = sub_mb_type;
  937. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  938. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  939. if(!IS_INTRA(mb_types_col[y8])
  940. && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
  941. || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
  942. if(ref[0] > 0)
  943. a= pack16to32(mv[0][0],mv[0][1]);
  944. if(ref[1] > 0)
  945. b= pack16to32(mv[1][0],mv[1][1]);
  946. }else{
  947. a= pack16to32(mv[0][0],mv[0][1]);
  948. b= pack16to32(mv[1][0],mv[1][1]);
  949. }
  950. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
  951. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
  952. }
  953. }else if(IS_16X16(*mb_type)){
  954. int a=0, b=0;
  955. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  956. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  957. if(!IS_INTRA(mb_type_col)
  958. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  959. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  960. && (h->x264_build>33 || !h->x264_build)))){
  961. if(ref[0] > 0)
  962. a= pack16to32(mv[0][0],mv[0][1]);
  963. if(ref[1] > 0)
  964. b= pack16to32(mv[1][0],mv[1][1]);
  965. }else{
  966. a= pack16to32(mv[0][0],mv[0][1]);
  967. b= pack16to32(mv[1][0],mv[1][1]);
  968. }
  969. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  970. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  971. }else{
  972. for(i8=0; i8<4; i8++){
  973. const int x8 = i8&1;
  974. const int y8 = i8>>1;
  975. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  976. continue;
  977. h->sub_mb_type[i8] = sub_mb_type;
  978. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  979. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  980. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  981. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  982. /* col_zero_flag */
  983. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  984. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  985. && (h->x264_build>33 || !h->x264_build)))){
  986. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  987. if(IS_SUB_8X8(sub_mb_type)){
  988. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  989. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  990. if(ref[0] == 0)
  991. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  992. if(ref[1] == 0)
  993. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  994. }
  995. }else
  996. for(i4=0; i4<4; i4++){
  997. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  998. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  999. if(ref[0] == 0)
  1000. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1001. if(ref[1] == 0)
  1002. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. }
  1008. }else{ /* direct temporal mv pred */
  1009. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1010. const int *dist_scale_factor = h->dist_scale_factor;
  1011. if(FRAME_MBAFF){
  1012. if(IS_INTERLACED(*mb_type)){
  1013. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1014. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1015. dist_scale_factor = h->dist_scale_factor_field;
  1016. }
  1017. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1018. /* FIXME assumes direct_8x8_inference == 1 */
  1019. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1020. int mb_types_col[2];
  1021. int y_shift;
  1022. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1023. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1024. | (*mb_type & MB_TYPE_INTERLACED);
  1025. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1026. if(IS_INTERLACED(*mb_type)){
  1027. /* frame to field scaling */
  1028. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1029. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1030. if(s->mb_y&1){
  1031. l1ref0 -= 2*h->b8_stride;
  1032. l1ref1 -= 2*h->b8_stride;
  1033. l1mv0 -= 4*h->b_stride;
  1034. l1mv1 -= 4*h->b_stride;
  1035. }
  1036. y_shift = 0;
  1037. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1038. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1039. && !is_b8x8)
  1040. *mb_type |= MB_TYPE_16x8;
  1041. else
  1042. *mb_type |= MB_TYPE_8x8;
  1043. }else{
  1044. /* field to frame scaling */
  1045. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1046. * but in MBAFF, top and bottom POC are equal */
  1047. int dy = (s->mb_y&1) ? 1 : 2;
  1048. mb_types_col[0] =
  1049. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1050. l1ref0 += dy*h->b8_stride;
  1051. l1ref1 += dy*h->b8_stride;
  1052. l1mv0 += 2*dy*h->b_stride;
  1053. l1mv1 += 2*dy*h->b_stride;
  1054. y_shift = 2;
  1055. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1056. && !is_b8x8)
  1057. *mb_type |= MB_TYPE_16x16;
  1058. else
  1059. *mb_type |= MB_TYPE_8x8;
  1060. }
  1061. for(i8=0; i8<4; i8++){
  1062. const int x8 = i8&1;
  1063. const int y8 = i8>>1;
  1064. int ref0, scale;
  1065. const int16_t (*l1mv)[2]= l1mv0;
  1066. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1067. continue;
  1068. h->sub_mb_type[i8] = sub_mb_type;
  1069. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1070. if(IS_INTRA(mb_types_col[y8])){
  1071. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1072. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1073. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1074. continue;
  1075. }
  1076. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1077. if(ref0 >= 0)
  1078. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1079. else{
  1080. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1081. l1mv= l1mv1;
  1082. }
  1083. scale = dist_scale_factor[ref0];
  1084. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1085. {
  1086. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1087. int my_col = (mv_col[1]<<y_shift)/2;
  1088. int mx = (scale * mv_col[0] + 128) >> 8;
  1089. int my = (scale * my_col + 128) >> 8;
  1090. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1091. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1092. }
  1093. }
  1094. return;
  1095. }
  1096. }
  1097. /* one-to-one mv scaling */
  1098. if(IS_16X16(*mb_type)){
  1099. int ref, mv0, mv1;
  1100. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1101. if(IS_INTRA(mb_type_col)){
  1102. ref=mv0=mv1=0;
  1103. }else{
  1104. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1105. : map_col_to_list0[1][l1ref1[0]];
  1106. const int scale = dist_scale_factor[ref0];
  1107. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1108. int mv_l0[2];
  1109. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1110. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1111. ref= ref0;
  1112. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  1113. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1114. }
  1115. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  1116. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  1117. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  1118. }else{
  1119. for(i8=0; i8<4; i8++){
  1120. const int x8 = i8&1;
  1121. const int y8 = i8>>1;
  1122. int ref0, scale;
  1123. const int16_t (*l1mv)[2]= l1mv0;
  1124. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1125. continue;
  1126. h->sub_mb_type[i8] = sub_mb_type;
  1127. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1128. if(IS_INTRA(mb_type_col)){
  1129. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1130. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1131. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1132. continue;
  1133. }
  1134. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1135. if(ref0 >= 0)
  1136. ref0 = map_col_to_list0[0][ref0];
  1137. else{
  1138. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1139. l1mv= l1mv1;
  1140. }
  1141. scale = dist_scale_factor[ref0];
  1142. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1143. if(IS_SUB_8X8(sub_mb_type)){
  1144. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1145. int mx = (scale * mv_col[0] + 128) >> 8;
  1146. int my = (scale * mv_col[1] + 128) >> 8;
  1147. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1148. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1149. }else
  1150. for(i4=0; i4<4; i4++){
  1151. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1152. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1153. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1154. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1155. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1156. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1157. }
  1158. }
  1159. }
  1160. }
  1161. }
  1162. static inline void write_back_motion(H264Context *h, int mb_type){
  1163. MpegEncContext * const s = &h->s;
  1164. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1165. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1166. int list;
  1167. if(!USES_LIST(mb_type, 0))
  1168. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1169. for(list=0; list<h->list_count; list++){
  1170. int y;
  1171. if(!USES_LIST(mb_type, list))
  1172. continue;
  1173. for(y=0; y<4; y++){
  1174. *(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];
  1175. *(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];
  1176. }
  1177. if( h->pps.cabac ) {
  1178. if(IS_SKIP(mb_type))
  1179. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1180. else
  1181. for(y=0; y<4; y++){
  1182. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1183. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1184. }
  1185. }
  1186. {
  1187. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1188. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1189. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1190. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1191. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1192. }
  1193. }
  1194. if(h->slice_type_nos == FF_B_TYPE && h->pps.cabac){
  1195. if(IS_8X8(mb_type)){
  1196. uint8_t *direct_table = &h->direct_table[b8_xy];
  1197. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1198. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1199. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1200. }
  1201. }
  1202. }
  1203. /**
  1204. * Decodes a network abstraction layer unit.
  1205. * @param consumed is the number of bytes used as input
  1206. * @param length is the length of the array
  1207. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1208. * @returns decoded bytes, might be src+1 if no escapes
  1209. */
  1210. static const uint8_t *decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  1211. int i, si, di;
  1212. uint8_t *dst;
  1213. int bufidx;
  1214. // src[0]&0x80; //forbidden bit
  1215. h->nal_ref_idc= src[0]>>5;
  1216. h->nal_unit_type= src[0]&0x1F;
  1217. src++; length--;
  1218. #if 0
  1219. for(i=0; i<length; i++)
  1220. printf("%2X ", src[i]);
  1221. #endif
  1222. for(i=0; i+1<length; i+=2){
  1223. if(src[i]) continue;
  1224. if(i>0 && src[i-1]==0) i--;
  1225. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1226. if(src[i+2]!=3){
  1227. /* startcode, so we must be past the end */
  1228. length=i;
  1229. }
  1230. break;
  1231. }
  1232. }
  1233. if(i>=length-1){ //no escaped 0
  1234. *dst_length= length;
  1235. *consumed= length+1; //+1 for the header
  1236. return src;
  1237. }
  1238. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  1239. h->rbsp_buffer[bufidx]= av_fast_realloc(h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length);
  1240. dst= h->rbsp_buffer[bufidx];
  1241. if (dst == NULL){
  1242. return NULL;
  1243. }
  1244. //printf("decoding esc\n");
  1245. si=di=0;
  1246. while(si<length){
  1247. //remove escapes (very rare 1:2^22)
  1248. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1249. if(src[si+2]==3){ //escape
  1250. dst[di++]= 0;
  1251. dst[di++]= 0;
  1252. si+=3;
  1253. continue;
  1254. }else //next start code
  1255. break;
  1256. }
  1257. dst[di++]= src[si++];
  1258. }
  1259. *dst_length= di;
  1260. *consumed= si + 1;//+1 for the header
  1261. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  1262. return dst;
  1263. }
  1264. /**
  1265. * identifies the exact end of the bitstream
  1266. * @return the length of the trailing, or 0 if damaged
  1267. */
  1268. static int decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  1269. int v= *src;
  1270. int r;
  1271. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  1272. for(r=1; r<9; r++){
  1273. if(v&1) return r;
  1274. v>>=1;
  1275. }
  1276. return 0;
  1277. }
  1278. /**
  1279. * IDCT transforms the 16 dc values and dequantizes them.
  1280. * @param qp quantization parameter
  1281. */
  1282. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1283. #define stride 16
  1284. int i;
  1285. int temp[16]; //FIXME check if this is a good idea
  1286. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1287. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1288. //memset(block, 64, 2*256);
  1289. //return;
  1290. for(i=0; i<4; i++){
  1291. const int offset= y_offset[i];
  1292. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1293. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1294. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1295. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1296. temp[4*i+0]= z0+z3;
  1297. temp[4*i+1]= z1+z2;
  1298. temp[4*i+2]= z1-z2;
  1299. temp[4*i+3]= z0-z3;
  1300. }
  1301. for(i=0; i<4; i++){
  1302. const int offset= x_offset[i];
  1303. const int z0= temp[4*0+i] + temp[4*2+i];
  1304. const int z1= temp[4*0+i] - temp[4*2+i];
  1305. const int z2= temp[4*1+i] - temp[4*3+i];
  1306. const int z3= temp[4*1+i] + temp[4*3+i];
  1307. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
  1308. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1309. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1310. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1311. }
  1312. }
  1313. #if 0
  1314. /**
  1315. * DCT transforms the 16 dc values.
  1316. * @param qp quantization parameter ??? FIXME
  1317. */
  1318. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1319. // const int qmul= dequant_coeff[qp][0];
  1320. int i;
  1321. int temp[16]; //FIXME check if this is a good idea
  1322. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1323. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1324. for(i=0; i<4; i++){
  1325. const int offset= y_offset[i];
  1326. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1327. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1328. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1329. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1330. temp[4*i+0]= z0+z3;
  1331. temp[4*i+1]= z1+z2;
  1332. temp[4*i+2]= z1-z2;
  1333. temp[4*i+3]= z0-z3;
  1334. }
  1335. for(i=0; i<4; i++){
  1336. const int offset= x_offset[i];
  1337. const int z0= temp[4*0+i] + temp[4*2+i];
  1338. const int z1= temp[4*0+i] - temp[4*2+i];
  1339. const int z2= temp[4*1+i] - temp[4*3+i];
  1340. const int z3= temp[4*1+i] + temp[4*3+i];
  1341. block[stride*0 +offset]= (z0 + z3)>>1;
  1342. block[stride*2 +offset]= (z1 + z2)>>1;
  1343. block[stride*8 +offset]= (z1 - z2)>>1;
  1344. block[stride*10+offset]= (z0 - z3)>>1;
  1345. }
  1346. }
  1347. #endif
  1348. #undef xStride
  1349. #undef stride
  1350. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1351. const int stride= 16*2;
  1352. const int xStride= 16;
  1353. int a,b,c,d,e;
  1354. a= block[stride*0 + xStride*0];
  1355. b= block[stride*0 + xStride*1];
  1356. c= block[stride*1 + xStride*0];
  1357. d= block[stride*1 + xStride*1];
  1358. e= a-b;
  1359. a= a+b;
  1360. b= c-d;
  1361. c= c+d;
  1362. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1363. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1364. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1365. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1366. }
  1367. #if 0
  1368. static void chroma_dc_dct_c(DCTELEM *block){
  1369. const int stride= 16*2;
  1370. const int xStride= 16;
  1371. int a,b,c,d,e;
  1372. a= block[stride*0 + xStride*0];
  1373. b= block[stride*0 + xStride*1];
  1374. c= block[stride*1 + xStride*0];
  1375. d= block[stride*1 + xStride*1];
  1376. e= a-b;
  1377. a= a+b;
  1378. b= c-d;
  1379. c= c+d;
  1380. block[stride*0 + xStride*0]= (a+c);
  1381. block[stride*0 + xStride*1]= (e+b);
  1382. block[stride*1 + xStride*0]= (a-c);
  1383. block[stride*1 + xStride*1]= (e-b);
  1384. }
  1385. #endif
  1386. /**
  1387. * gets the chroma qp.
  1388. */
  1389. static inline int get_chroma_qp(H264Context *h, int t, int qscale){
  1390. return h->pps.chroma_qp_table[t][qscale];
  1391. }
  1392. //FIXME need to check that this does not overflow signed 32 bit for low qp, I am not sure, it's very close
  1393. //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
  1394. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
  1395. int i;
  1396. const int * const quant_table= quant_coeff[qscale];
  1397. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1398. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1399. const unsigned int threshold2= (threshold1<<1);
  1400. int last_non_zero;
  1401. if(separate_dc){
  1402. if(qscale<=18){
  1403. //avoid overflows
  1404. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1405. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1406. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1407. int level= block[0]*quant_coeff[qscale+18][0];
  1408. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1409. if(level>0){
  1410. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1411. block[0]= level;
  1412. }else{
  1413. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1414. block[0]= -level;
  1415. }
  1416. // last_non_zero = i;
  1417. }else{
  1418. block[0]=0;
  1419. }
  1420. }else{
  1421. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1422. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1423. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1424. int level= block[0]*quant_table[0];
  1425. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1426. if(level>0){
  1427. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1428. block[0]= level;
  1429. }else{
  1430. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1431. block[0]= -level;
  1432. }
  1433. // last_non_zero = i;
  1434. }else{
  1435. block[0]=0;
  1436. }
  1437. }
  1438. last_non_zero= 0;
  1439. i=1;
  1440. }else{
  1441. last_non_zero= -1;
  1442. i=0;
  1443. }
  1444. for(; i<16; i++){
  1445. const int j= scantable[i];
  1446. int level= block[j]*quant_table[j];
  1447. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1448. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1449. if(((unsigned)(level+threshold1))>threshold2){
  1450. if(level>0){
  1451. level= (bias + level)>>QUANT_SHIFT;
  1452. block[j]= level;
  1453. }else{
  1454. level= (bias - level)>>QUANT_SHIFT;
  1455. block[j]= -level;
  1456. }
  1457. last_non_zero = i;
  1458. }else{
  1459. block[j]=0;
  1460. }
  1461. }
  1462. return last_non_zero;
  1463. }
  1464. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  1465. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1466. int src_x_offset, int src_y_offset,
  1467. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  1468. MpegEncContext * const s = &h->s;
  1469. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  1470. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  1471. const int luma_xy= (mx&3) + ((my&3)<<2);
  1472. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  1473. uint8_t * src_cb, * src_cr;
  1474. int extra_width= h->emu_edge_width;
  1475. int extra_height= h->emu_edge_height;
  1476. int emu=0;
  1477. const int full_mx= mx>>2;
  1478. const int full_my= my>>2;
  1479. const int pic_width = 16*s->mb_width;
  1480. const int pic_height = 16*s->mb_height >> MB_FIELD;
  1481. if(!pic->data[0]) //FIXME this is unacceptable, some sensible error concealment must be done for missing reference frames
  1482. return;
  1483. if(mx&7) extra_width -= 3;
  1484. if(my&7) extra_height -= 3;
  1485. if( full_mx < 0-extra_width
  1486. || full_my < 0-extra_height
  1487. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  1488. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  1489. 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);
  1490. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  1491. emu=1;
  1492. }
  1493. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  1494. if(!square){
  1495. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  1496. }
  1497. if(ENABLE_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  1498. if(MB_FIELD){
  1499. // chroma offset when predicting from a field of opposite parity
  1500. my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
  1501. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  1502. }
  1503. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  1504. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  1505. if(emu){
  1506. 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);
  1507. src_cb= s->edge_emu_buffer;
  1508. }
  1509. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  1510. if(emu){
  1511. 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);
  1512. src_cr= s->edge_emu_buffer;
  1513. }
  1514. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  1515. }
  1516. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  1517. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1518. int x_offset, int y_offset,
  1519. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1520. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1521. int list0, int list1){
  1522. MpegEncContext * const s = &h->s;
  1523. qpel_mc_func *qpix_op= qpix_put;
  1524. h264_chroma_mc_func chroma_op= chroma_put;
  1525. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  1526. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  1527. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  1528. x_offset += 8*s->mb_x;
  1529. y_offset += 8*(s->mb_y >> MB_FIELD);
  1530. if(list0){
  1531. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  1532. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  1533. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1534. qpix_op, chroma_op);
  1535. qpix_op= qpix_avg;
  1536. chroma_op= chroma_avg;
  1537. }
  1538. if(list1){
  1539. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  1540. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  1541. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1542. qpix_op, chroma_op);
  1543. }
  1544. }
  1545. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  1546. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1547. int x_offset, int y_offset,
  1548. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1549. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  1550. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  1551. int list0, int list1){
  1552. MpegEncContext * const s = &h->s;
  1553. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  1554. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  1555. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  1556. x_offset += 8*s->mb_x;
  1557. y_offset += 8*(s->mb_y >> MB_FIELD);
  1558. if(list0 && list1){
  1559. /* don't optimize for luma-only case, since B-frames usually
  1560. * use implicit weights => chroma too. */
  1561. uint8_t *tmp_cb = s->obmc_scratchpad;
  1562. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  1563. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  1564. int refn0 = h->ref_cache[0][ scan8[n] ];
  1565. int refn1 = h->ref_cache[1][ scan8[n] ];
  1566. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  1567. dest_y, dest_cb, dest_cr,
  1568. x_offset, y_offset, qpix_put, chroma_put);
  1569. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  1570. tmp_y, tmp_cb, tmp_cr,
  1571. x_offset, y_offset, qpix_put, chroma_put);
  1572. if(h->use_weight == 2){
  1573. int weight0 = h->implicit_weight[refn0][refn1];
  1574. int weight1 = 64 - weight0;
  1575. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  1576. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  1577. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  1578. }else{
  1579. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  1580. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  1581. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  1582. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1583. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  1584. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  1585. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1586. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  1587. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  1588. }
  1589. }else{
  1590. int list = list1 ? 1 : 0;
  1591. int refn = h->ref_cache[list][ scan8[n] ];
  1592. Picture *ref= &h->ref_list[list][refn];
  1593. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  1594. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1595. qpix_put, chroma_put);
  1596. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  1597. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  1598. if(h->use_weight_chroma){
  1599. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1600. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  1601. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1602. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  1603. }
  1604. }
  1605. }
  1606. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  1607. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1608. int x_offset, int y_offset,
  1609. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1610. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1611. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  1612. int list0, int list1){
  1613. if((h->use_weight==2 && list0 && list1
  1614. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  1615. || h->use_weight==1)
  1616. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1617. x_offset, y_offset, qpix_put, chroma_put,
  1618. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  1619. else
  1620. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1621. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  1622. }
  1623. static inline void prefetch_motion(H264Context *h, int list){
  1624. /* fetch pixels for estimated mv 4 macroblocks ahead
  1625. * optimized for 64byte cache lines */
  1626. MpegEncContext * const s = &h->s;
  1627. const int refn = h->ref_cache[list][scan8[0]];
  1628. if(refn >= 0){
  1629. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  1630. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  1631. uint8_t **src= h->ref_list[list][refn].data;
  1632. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  1633. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1634. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  1635. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1636. }
  1637. }
  1638. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1639. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  1640. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  1641. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  1642. MpegEncContext * const s = &h->s;
  1643. const int mb_xy= h->mb_xy;
  1644. const int mb_type= s->current_picture.mb_type[mb_xy];
  1645. assert(IS_INTER(mb_type));
  1646. prefetch_motion(h, 0);
  1647. if(IS_16X16(mb_type)){
  1648. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  1649. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  1650. &weight_op[0], &weight_avg[0],
  1651. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1652. }else if(IS_16X8(mb_type)){
  1653. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  1654. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1655. &weight_op[1], &weight_avg[1],
  1656. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1657. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  1658. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1659. &weight_op[1], &weight_avg[1],
  1660. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1661. }else if(IS_8X16(mb_type)){
  1662. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  1663. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1664. &weight_op[2], &weight_avg[2],
  1665. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1666. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  1667. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1668. &weight_op[2], &weight_avg[2],
  1669. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1670. }else{
  1671. int i;
  1672. assert(IS_8X8(mb_type));
  1673. for(i=0; i<4; i++){
  1674. const int sub_mb_type= h->sub_mb_type[i];
  1675. const int n= 4*i;
  1676. int x_offset= (i&1)<<2;
  1677. int y_offset= (i&2)<<1;
  1678. if(IS_SUB_8X8(sub_mb_type)){
  1679. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1680. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1681. &weight_op[3], &weight_avg[3],
  1682. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1683. }else if(IS_SUB_8X4(sub_mb_type)){
  1684. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1685. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1686. &weight_op[4], &weight_avg[4],
  1687. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1688. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  1689. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1690. &weight_op[4], &weight_avg[4],
  1691. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1692. }else if(IS_SUB_4X8(sub_mb_type)){
  1693. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1694. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1695. &weight_op[5], &weight_avg[5],
  1696. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1697. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  1698. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1699. &weight_op[5], &weight_avg[5],
  1700. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1701. }else{
  1702. int j;
  1703. assert(IS_SUB_4X4(sub_mb_type));
  1704. for(j=0; j<4; j++){
  1705. int sub_x_offset= x_offset + 2*(j&1);
  1706. int sub_y_offset= y_offset + (j&2);
  1707. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  1708. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1709. &weight_op[6], &weight_avg[6],
  1710. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1711. }
  1712. }
  1713. }
  1714. }
  1715. prefetch_motion(h, 1);
  1716. }
  1717. static av_cold void decode_init_vlc(void){
  1718. static int done = 0;
  1719. if (!done) {
  1720. int i;
  1721. done = 1;
  1722. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  1723. &chroma_dc_coeff_token_len [0], 1, 1,
  1724. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  1725. for(i=0; i<4; i++){
  1726. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  1727. &coeff_token_len [i][0], 1, 1,
  1728. &coeff_token_bits[i][0], 1, 1, 1);
  1729. }
  1730. for(i=0; i<3; i++){
  1731. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  1732. &chroma_dc_total_zeros_len [i][0], 1, 1,
  1733. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  1734. }
  1735. for(i=0; i<15; i++){
  1736. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  1737. &total_zeros_len [i][0], 1, 1,
  1738. &total_zeros_bits[i][0], 1, 1, 1);
  1739. }
  1740. for(i=0; i<6; i++){
  1741. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  1742. &run_len [i][0], 1, 1,
  1743. &run_bits[i][0], 1, 1, 1);
  1744. }
  1745. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  1746. &run_len [6][0], 1, 1,
  1747. &run_bits[6][0], 1, 1, 1);
  1748. }
  1749. }
  1750. static void free_tables(H264Context *h){
  1751. int i;
  1752. H264Context *hx;
  1753. av_freep(&h->intra4x4_pred_mode);
  1754. av_freep(&h->chroma_pred_mode_table);
  1755. av_freep(&h->cbp_table);
  1756. av_freep(&h->mvd_table[0]);
  1757. av_freep(&h->mvd_table[1]);
  1758. av_freep(&h->direct_table);
  1759. av_freep(&h->non_zero_count);
  1760. av_freep(&h->slice_table_base);
  1761. h->slice_table= NULL;
  1762. av_freep(&h->mb2b_xy);
  1763. av_freep(&h->mb2b8_xy);
  1764. for(i = 0; i < MAX_SPS_COUNT; i++)
  1765. av_freep(h->sps_buffers + i);
  1766. for(i = 0; i < MAX_PPS_COUNT; i++)
  1767. av_freep(h->pps_buffers + i);
  1768. for(i = 0; i < h->s.avctx->thread_count; i++) {
  1769. hx = h->thread_context[i];
  1770. if(!hx) continue;
  1771. av_freep(&hx->top_borders[1]);
  1772. av_freep(&hx->top_borders[0]);
  1773. av_freep(&hx->s.obmc_scratchpad);
  1774. }
  1775. }
  1776. static void init_dequant8_coeff_table(H264Context *h){
  1777. int i,q,x;
  1778. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  1779. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  1780. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  1781. for(i=0; i<2; i++ ){
  1782. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  1783. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  1784. break;
  1785. }
  1786. for(q=0; q<52; q++){
  1787. int shift = ff_div6[q];
  1788. int idx = ff_rem6[q];
  1789. for(x=0; x<64; x++)
  1790. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  1791. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  1792. h->pps.scaling_matrix8[i][x]) << shift;
  1793. }
  1794. }
  1795. }
  1796. static void init_dequant4_coeff_table(H264Context *h){
  1797. int i,j,q,x;
  1798. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  1799. for(i=0; i<6; i++ ){
  1800. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  1801. for(j=0; j<i; j++){
  1802. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  1803. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  1804. break;
  1805. }
  1806. }
  1807. if(j<i)
  1808. continue;
  1809. for(q=0; q<52; q++){
  1810. int shift = ff_div6[q] + 2;
  1811. int idx = ff_rem6[q];
  1812. for(x=0; x<16; x++)
  1813. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  1814. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  1815. h->pps.scaling_matrix4[i][x]) << shift;
  1816. }
  1817. }
  1818. }
  1819. static void init_dequant_tables(H264Context *h){
  1820. int i,x;
  1821. init_dequant4_coeff_table(h);
  1822. if(h->pps.transform_8x8_mode)
  1823. init_dequant8_coeff_table(h);
  1824. if(h->sps.transform_bypass){
  1825. for(i=0; i<6; i++)
  1826. for(x=0; x<16; x++)
  1827. h->dequant4_coeff[i][0][x] = 1<<6;
  1828. if(h->pps.transform_8x8_mode)
  1829. for(i=0; i<2; i++)
  1830. for(x=0; x<64; x++)
  1831. h->dequant8_coeff[i][0][x] = 1<<6;
  1832. }
  1833. }
  1834. /**
  1835. * allocates tables.
  1836. * needs width/height
  1837. */
  1838. static int alloc_tables(H264Context *h){
  1839. MpegEncContext * const s = &h->s;
  1840. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1841. int x,y;
  1842. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  1843. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  1844. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  1845. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  1846. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  1847. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  1848. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  1849. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  1850. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  1851. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  1852. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  1853. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  1854. for(y=0; y<s->mb_height; y++){
  1855. for(x=0; x<s->mb_width; x++){
  1856. const int mb_xy= x + y*s->mb_stride;
  1857. const int b_xy = 4*x + 4*y*h->b_stride;
  1858. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1859. h->mb2b_xy [mb_xy]= b_xy;
  1860. h->mb2b8_xy[mb_xy]= b8_xy;
  1861. }
  1862. }
  1863. s->obmc_scratchpad = NULL;
  1864. if(!h->dequant4_coeff[0])
  1865. init_dequant_tables(h);
  1866. return 0;
  1867. fail:
  1868. free_tables(h);
  1869. return -1;
  1870. }
  1871. /**
  1872. * Mimic alloc_tables(), but for every context thread.
  1873. */
  1874. static void clone_tables(H264Context *dst, H264Context *src){
  1875. dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
  1876. dst->non_zero_count = src->non_zero_count;
  1877. dst->slice_table = src->slice_table;
  1878. dst->cbp_table = src->cbp_table;
  1879. dst->mb2b_xy = src->mb2b_xy;
  1880. dst->mb2b8_xy = src->mb2b8_xy;
  1881. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  1882. dst->mvd_table[0] = src->mvd_table[0];
  1883. dst->mvd_table[1] = src->mvd_table[1];
  1884. dst->direct_table = src->direct_table;
  1885. dst->s.obmc_scratchpad = NULL;
  1886. ff_h264_pred_init(&dst->hpc, src->s.codec_id);
  1887. }
  1888. /**
  1889. * Init context
  1890. * Allocate buffers which are not shared amongst multiple threads.
  1891. */
  1892. static int context_init(H264Context *h){
  1893. CHECKED_ALLOCZ(h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
  1894. CHECKED_ALLOCZ(h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
  1895. return 0;
  1896. fail:
  1897. return -1; // free_tables will clean up for us
  1898. }
  1899. static av_cold void common_init(H264Context *h){
  1900. MpegEncContext * const s = &h->s;
  1901. s->width = s->avctx->width;
  1902. s->height = s->avctx->height;
  1903. s->codec_id= s->avctx->codec->id;
  1904. ff_h264_pred_init(&h->hpc, s->codec_id);
  1905. h->dequant_coeff_pps= -1;
  1906. s->unrestricted_mv=1;
  1907. s->decode=1; //FIXME
  1908. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  1909. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  1910. }
  1911. static av_cold int decode_init(AVCodecContext *avctx){
  1912. H264Context *h= avctx->priv_data;
  1913. MpegEncContext * const s = &h->s;
  1914. MPV_decode_defaults(s);
  1915. s->avctx = avctx;
  1916. common_init(h);
  1917. s->out_format = FMT_H264;
  1918. s->workaround_bugs= avctx->workaround_bugs;
  1919. // set defaults
  1920. // s->decode_mb= ff_h263_decode_mb;
  1921. s->quarter_sample = 1;
  1922. s->low_delay= 1;
  1923. if(avctx->codec_id == CODEC_ID_SVQ3)
  1924. avctx->pix_fmt= PIX_FMT_YUVJ420P;
  1925. else
  1926. avctx->pix_fmt= PIX_FMT_YUV420P;
  1927. decode_init_vlc();
  1928. if(avctx->extradata_size > 0 && avctx->extradata &&
  1929. *(char *)avctx->extradata == 1){
  1930. h->is_avc = 1;
  1931. h->got_avcC = 0;
  1932. } else {
  1933. h->is_avc = 0;
  1934. }
  1935. h->thread_context[0] = h;
  1936. h->outputed_poc = INT_MIN;
  1937. return 0;
  1938. }
  1939. static int frame_start(H264Context *h){
  1940. MpegEncContext * const s = &h->s;
  1941. int i;
  1942. if(MPV_frame_start(s, s->avctx) < 0)
  1943. return -1;
  1944. ff_er_frame_start(s);
  1945. /*
  1946. * MPV_frame_start uses pict_type to derive key_frame.
  1947. * This is incorrect for H.264; IDR markings must be used.
  1948. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1949. * See decode_nal_units().
  1950. */
  1951. s->current_picture_ptr->key_frame= 0;
  1952. assert(s->linesize && s->uvlinesize);
  1953. for(i=0; i<16; i++){
  1954. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1955. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  1956. }
  1957. for(i=0; i<4; i++){
  1958. h->block_offset[16+i]=
  1959. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1960. h->block_offset[24+16+i]=
  1961. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1962. }
  1963. /* can't be in alloc_tables because linesize isn't known there.
  1964. * FIXME: redo bipred weight to not require extra buffer? */
  1965. for(i = 0; i < s->avctx->thread_count; i++)
  1966. if(!h->thread_context[i]->s.obmc_scratchpad)
  1967. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  1968. /* some macroblocks will be accessed before they're available */
  1969. if(FRAME_MBAFF || s->avctx->thread_count > 1)
  1970. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  1971. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1972. // We mark the current picture as non-reference after allocating it, so
  1973. // that if we break out due to an error it can be released automatically
  1974. // in the next MPV_frame_start().
  1975. // SVQ3 as well as most other codecs have only last/next/current and thus
  1976. // get released even with set reference, besides SVQ3 and others do not
  1977. // mark frames as reference later "naturally".
  1978. if(s->codec_id != CODEC_ID_SVQ3)
  1979. s->current_picture_ptr->reference= 0;
  1980. s->current_picture_ptr->field_poc[0]=
  1981. s->current_picture_ptr->field_poc[1]= INT_MAX;
  1982. assert(s->current_picture_ptr->long_ref==0);
  1983. return 0;
  1984. }
  1985. 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){
  1986. MpegEncContext * const s = &h->s;
  1987. int i;
  1988. src_y -= linesize;
  1989. src_cb -= uvlinesize;
  1990. src_cr -= uvlinesize;
  1991. // There are two lines saved, the line above the the top macroblock of a pair,
  1992. // and the line above the bottom macroblock
  1993. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  1994. for(i=1; i<17; i++){
  1995. h->left_border[i]= src_y[15+i* linesize];
  1996. }
  1997. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  1998. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  1999. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2000. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2001. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2002. for(i=1; i<9; i++){
  2003. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2004. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2005. }
  2006. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2007. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2008. }
  2009. }
  2010. 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){
  2011. MpegEncContext * const s = &h->s;
  2012. int temp8, i;
  2013. uint64_t temp64;
  2014. int deblock_left;
  2015. int deblock_top;
  2016. int mb_xy;
  2017. if(h->deblocking_filter == 2) {
  2018. mb_xy = h->mb_xy;
  2019. deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
  2020. deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
  2021. } else {
  2022. deblock_left = (s->mb_x > 0);
  2023. deblock_top = (s->mb_y > 0);
  2024. }
  2025. src_y -= linesize + 1;
  2026. src_cb -= uvlinesize + 1;
  2027. src_cr -= uvlinesize + 1;
  2028. #define XCHG(a,b,t,xchg)\
  2029. t= a;\
  2030. if(xchg)\
  2031. a= b;\
  2032. b= t;
  2033. if(deblock_left){
  2034. for(i = !deblock_top; i<17; i++){
  2035. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2036. }
  2037. }
  2038. if(deblock_top){
  2039. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2040. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2041. if(s->mb_x+1 < s->mb_width){
  2042. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2043. }
  2044. }
  2045. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2046. if(deblock_left){
  2047. for(i = !deblock_top; i<9; i++){
  2048. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  2049. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  2050. }
  2051. }
  2052. if(deblock_top){
  2053. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2054. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2055. }
  2056. }
  2057. }
  2058. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2059. MpegEncContext * const s = &h->s;
  2060. int i;
  2061. src_y -= 2 * linesize;
  2062. src_cb -= 2 * uvlinesize;
  2063. src_cr -= 2 * uvlinesize;
  2064. // There are two lines saved, the line above the the top macroblock of a pair,
  2065. // and the line above the bottom macroblock
  2066. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2067. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  2068. for(i=2; i<34; i++){
  2069. h->left_border[i]= src_y[15+i* linesize];
  2070. }
  2071. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  2072. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  2073. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  2074. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  2075. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2076. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  2077. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  2078. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  2079. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  2080. for(i=2; i<18; i++){
  2081. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  2082. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  2083. }
  2084. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  2085. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  2086. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  2087. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  2088. }
  2089. }
  2090. 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){
  2091. MpegEncContext * const s = &h->s;
  2092. int temp8, i;
  2093. uint64_t temp64;
  2094. int deblock_left = (s->mb_x > 0);
  2095. int deblock_top = (s->mb_y > 1);
  2096. 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);
  2097. src_y -= 2 * linesize + 1;
  2098. src_cb -= 2 * uvlinesize + 1;
  2099. src_cr -= 2 * uvlinesize + 1;
  2100. #define XCHG(a,b,t,xchg)\
  2101. t= a;\
  2102. if(xchg)\
  2103. a= b;\
  2104. b= t;
  2105. if(deblock_left){
  2106. for(i = (!deblock_top)<<1; i<34; i++){
  2107. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2108. }
  2109. }
  2110. if(deblock_top){
  2111. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2112. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2113. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  2114. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  2115. if(s->mb_x+1 < s->mb_width){
  2116. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2117. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  2118. }
  2119. }
  2120. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2121. if(deblock_left){
  2122. for(i = (!deblock_top) << 1; i<18; i++){
  2123. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  2124. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  2125. }
  2126. }
  2127. if(deblock_top){
  2128. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2129. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2130. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  2131. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  2132. }
  2133. }
  2134. }
  2135. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  2136. MpegEncContext * const s = &h->s;
  2137. const int mb_x= s->mb_x;
  2138. const int mb_y= s->mb_y;
  2139. const int mb_xy= h->mb_xy;
  2140. const int mb_type= s->current_picture.mb_type[mb_xy];
  2141. uint8_t *dest_y, *dest_cb, *dest_cr;
  2142. int linesize, uvlinesize /*dct_offset*/;
  2143. int i;
  2144. int *block_offset = &h->block_offset[0];
  2145. const unsigned int bottom = mb_y & 1;
  2146. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
  2147. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  2148. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  2149. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2150. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2151. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2152. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  2153. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  2154. if (!simple && MB_FIELD) {
  2155. linesize = h->mb_linesize = s->linesize * 2;
  2156. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2157. block_offset = &h->block_offset[24];
  2158. if(mb_y&1){ //FIXME move out of this function?
  2159. dest_y -= s->linesize*15;
  2160. dest_cb-= s->uvlinesize*7;
  2161. dest_cr-= s->uvlinesize*7;
  2162. }
  2163. if(FRAME_MBAFF) {
  2164. int list;
  2165. for(list=0; list<h->list_count; list++){
  2166. if(!USES_LIST(mb_type, list))
  2167. continue;
  2168. if(IS_16X16(mb_type)){
  2169. int8_t *ref = &h->ref_cache[list][scan8[0]];
  2170. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  2171. }else{
  2172. for(i=0; i<16; i+=4){
  2173. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  2174. int ref = h->ref_cache[list][scan8[i]];
  2175. if(ref >= 0)
  2176. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  2177. }
  2178. }
  2179. }
  2180. }
  2181. } else {
  2182. linesize = h->mb_linesize = s->linesize;
  2183. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2184. // dct_offset = s->linesize * 16;
  2185. }
  2186. if(transform_bypass){
  2187. idct_dc_add =
  2188. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  2189. }else if(IS_8x8DCT(mb_type)){
  2190. idct_dc_add = s->dsp.h264_idct8_dc_add;
  2191. idct_add = s->dsp.h264_idct8_add;
  2192. }else{
  2193. idct_dc_add = s->dsp.h264_idct_dc_add;
  2194. idct_add = s->dsp.h264_idct_add;
  2195. }
  2196. if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  2197. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  2198. int mbt_y = mb_y&~1;
  2199. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  2200. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2201. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2202. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  2203. }
  2204. if (!simple && IS_INTRA_PCM(mb_type)) {
  2205. unsigned int x, y;
  2206. // The pixels are stored in h->mb array in the same order as levels,
  2207. // copy them in output in the correct order.
  2208. for(i=0; i<16; i++) {
  2209. for (y=0; y<4; y++) {
  2210. for (x=0; x<4; x++) {
  2211. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  2212. }
  2213. }
  2214. }
  2215. for(i=16; i<16+4; i++) {
  2216. for (y=0; y<4; y++) {
  2217. for (x=0; x<4; x++) {
  2218. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2219. }
  2220. }
  2221. }
  2222. for(i=20; i<20+4; i++) {
  2223. for (y=0; y<4; y++) {
  2224. for (x=0; x<4; x++) {
  2225. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2226. }
  2227. }
  2228. }
  2229. } else {
  2230. if(IS_INTRA(mb_type)){
  2231. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2232. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
  2233. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2234. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2235. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2236. }
  2237. if(IS_INTRA4x4(mb_type)){
  2238. if(simple || !s->encoding){
  2239. if(IS_8x8DCT(mb_type)){
  2240. for(i=0; i<16; i+=4){
  2241. uint8_t * const ptr= dest_y + block_offset[i];
  2242. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2243. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  2244. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  2245. (h->topright_samples_available<<i)&0x4000, linesize);
  2246. if(nnz){
  2247. if(nnz == 1 && h->mb[i*16])
  2248. idct_dc_add(ptr, h->mb + i*16, linesize);
  2249. else
  2250. idct_add(ptr, h->mb + i*16, linesize);
  2251. }
  2252. }
  2253. }else
  2254. for(i=0; i<16; i++){
  2255. uint8_t * const ptr= dest_y + block_offset[i];
  2256. uint8_t *topright;
  2257. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2258. int nnz, tr;
  2259. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  2260. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2261. assert(mb_y || linesize <= block_offset[i]);
  2262. if(!topright_avail){
  2263. tr= ptr[3 - linesize]*0x01010101;
  2264. topright= (uint8_t*) &tr;
  2265. }else
  2266. topright= ptr + 4 - linesize;
  2267. }else
  2268. topright= NULL;
  2269. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  2270. nnz = h->non_zero_count_cache[ scan8[i] ];
  2271. if(nnz){
  2272. if(is_h264){
  2273. if(nnz == 1 && h->mb[i*16])
  2274. idct_dc_add(ptr, h->mb + i*16, linesize);
  2275. else
  2276. idct_add(ptr, h->mb + i*16, linesize);
  2277. }else
  2278. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2279. }
  2280. }
  2281. }
  2282. }else{
  2283. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2284. if(is_h264){
  2285. if(!transform_bypass)
  2286. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
  2287. }else
  2288. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2289. }
  2290. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2291. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
  2292. }else if(is_h264){
  2293. hl_motion(h, dest_y, dest_cb, dest_cr,
  2294. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  2295. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  2296. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  2297. }
  2298. if(!IS_INTRA4x4(mb_type)){
  2299. if(is_h264){
  2300. if(IS_INTRA16x16(mb_type)){
  2301. for(i=0; i<16; i++){
  2302. if(h->non_zero_count_cache[ scan8[i] ])
  2303. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2304. else if(h->mb[i*16])
  2305. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2306. }
  2307. }else{
  2308. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  2309. for(i=0; i<16; i+=di){
  2310. int nnz = h->non_zero_count_cache[ scan8[i] ];
  2311. if(nnz){
  2312. if(nnz==1 && h->mb[i*16])
  2313. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2314. else
  2315. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2316. }
  2317. }
  2318. }
  2319. }else{
  2320. for(i=0; i<16; i++){
  2321. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2322. uint8_t * const ptr= dest_y + block_offset[i];
  2323. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2324. }
  2325. }
  2326. }
  2327. }
  2328. if(simple || !ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2329. uint8_t *dest[2] = {dest_cb, dest_cr};
  2330. if(transform_bypass){
  2331. idct_add = idct_dc_add = s->dsp.add_pixels4;
  2332. }else{
  2333. idct_add = s->dsp.h264_idct_add;
  2334. idct_dc_add = s->dsp.h264_idct_dc_add;
  2335. 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]);
  2336. 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]);
  2337. }
  2338. if(is_h264){
  2339. for(i=16; i<16+8; i++){
  2340. if(h->non_zero_count_cache[ scan8[i] ])
  2341. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  2342. else if(h->mb[i*16])
  2343. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  2344. }
  2345. }else{
  2346. for(i=16; i<16+8; i++){
  2347. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2348. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  2349. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2350. }
  2351. }
  2352. }
  2353. }
  2354. }
  2355. if(h->deblocking_filter) {
  2356. if (!simple && FRAME_MBAFF) {
  2357. //FIXME try deblocking one mb at a time?
  2358. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  2359. const int mb_y = s->mb_y - 1;
  2360. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  2361. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2362. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  2363. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  2364. if (!bottom) return;
  2365. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2366. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2367. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2368. if(IS_INTRA(mb_type_top | mb_type_bottom))
  2369. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  2370. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  2371. // deblock a pair
  2372. // top
  2373. s->mb_y--; h->mb_xy -= s->mb_stride;
  2374. 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);
  2375. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  2376. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2377. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2378. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  2379. // bottom
  2380. s->mb_y++; h->mb_xy += s->mb_stride;
  2381. tprintf(h->s.avctx, "call mbaff filter_mb\n");
  2382. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  2383. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  2384. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  2385. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2386. } else {
  2387. tprintf(h->s.avctx, "call filter_mb\n");
  2388. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
  2389. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  2390. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2391. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2392. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2393. }
  2394. }
  2395. }
  2396. /**
  2397. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  2398. */
  2399. static void hl_decode_mb_simple(H264Context *h){
  2400. hl_decode_mb_internal(h, 1);
  2401. }
  2402. /**
  2403. * Process a macroblock; this handles edge cases, such as interlacing.
  2404. */
  2405. static void av_noinline hl_decode_mb_complex(H264Context *h){
  2406. hl_decode_mb_internal(h, 0);
  2407. }
  2408. static void hl_decode_mb(H264Context *h){
  2409. MpegEncContext * const s = &h->s;
  2410. const int mb_xy= h->mb_xy;
  2411. const int mb_type= s->current_picture.mb_type[mb_xy];
  2412. int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 ||
  2413. (ENABLE_GRAY && (s->flags&CODEC_FLAG_GRAY)) || (ENABLE_H264_ENCODER && s->encoding) || ENABLE_SMALL;
  2414. if(ENABLE_H264_ENCODER && !s->decode)
  2415. return;
  2416. if (is_complex)
  2417. hl_decode_mb_complex(h);
  2418. else hl_decode_mb_simple(h);
  2419. }
  2420. static void pic_as_field(Picture *pic, const int parity){
  2421. int i;
  2422. for (i = 0; i < 4; ++i) {
  2423. if (parity == PICT_BOTTOM_FIELD)
  2424. pic->data[i] += pic->linesize[i];
  2425. pic->reference = parity;
  2426. pic->linesize[i] *= 2;
  2427. }
  2428. }
  2429. static int split_field_copy(Picture *dest, Picture *src,
  2430. int parity, int id_add){
  2431. int match = !!(src->reference & parity);
  2432. if (match) {
  2433. *dest = *src;
  2434. if(parity != PICT_FRAME){
  2435. pic_as_field(dest, parity);
  2436. dest->pic_id *= 2;
  2437. dest->pic_id += id_add;
  2438. }
  2439. }
  2440. return match;
  2441. }
  2442. static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
  2443. int i[2]={0};
  2444. int index=0;
  2445. while(i[0]<len || i[1]<len){
  2446. while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
  2447. i[0]++;
  2448. while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
  2449. i[1]++;
  2450. if(i[0] < len){
  2451. in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
  2452. split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
  2453. }
  2454. if(i[1] < len){
  2455. in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
  2456. split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
  2457. }
  2458. }
  2459. return index;
  2460. }
  2461. static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
  2462. int i, best_poc;
  2463. int out_i= 0;
  2464. for(;;){
  2465. best_poc= dir ? INT_MIN : INT_MAX;
  2466. for(i=0; i<len; i++){
  2467. const int poc= src[i]->poc;
  2468. if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
  2469. best_poc= poc;
  2470. sorted[out_i]= src[i];
  2471. }
  2472. }
  2473. if(best_poc == (dir ? INT_MIN : INT_MAX))
  2474. break;
  2475. limit= sorted[out_i++]->poc - dir;
  2476. }
  2477. return out_i;
  2478. }
  2479. /**
  2480. * fills the default_ref_list.
  2481. */
  2482. static int fill_default_ref_list(H264Context *h){
  2483. MpegEncContext * const s = &h->s;
  2484. int i, len;
  2485. if(h->slice_type_nos==FF_B_TYPE){
  2486. Picture *sorted[32];
  2487. int cur_poc, list;
  2488. int lens[2];
  2489. if(FIELD_PICTURE)
  2490. cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
  2491. else
  2492. cur_poc= s->current_picture_ptr->poc;
  2493. for(list= 0; list<2; list++){
  2494. len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
  2495. len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
  2496. assert(len<=32);
  2497. len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
  2498. len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
  2499. assert(len<=32);
  2500. if(len < h->ref_count[list])
  2501. memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
  2502. lens[list]= len;
  2503. }
  2504. if(lens[0] == lens[1] && lens[1] > 1){
  2505. for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
  2506. if(i == lens[0])
  2507. FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
  2508. }
  2509. }else{
  2510. len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
  2511. len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
  2512. assert(len <= 32);
  2513. if(len < h->ref_count[0])
  2514. memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
  2515. }
  2516. #ifdef TRACE
  2517. for (i=0; i<h->ref_count[0]; i++) {
  2518. 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]);
  2519. }
  2520. if(h->slice_type_nos==FF_B_TYPE){
  2521. for (i=0; i<h->ref_count[1]; i++) {
  2522. 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[1][i].data[0]);
  2523. }
  2524. }
  2525. #endif
  2526. return 0;
  2527. }
  2528. static void print_short_term(H264Context *h);
  2529. static void print_long_term(H264Context *h);
  2530. /**
  2531. * Extract structure information about the picture described by pic_num in
  2532. * the current decoding context (frame or field). Note that pic_num is
  2533. * picture number without wrapping (so, 0<=pic_num<max_pic_num).
  2534. * @param pic_num picture number for which to extract structure information
  2535. * @param structure one of PICT_XXX describing structure of picture
  2536. * with pic_num
  2537. * @return frame number (short term) or long term index of picture
  2538. * described by pic_num
  2539. */
  2540. static int pic_num_extract(H264Context *h, int pic_num, int *structure){
  2541. MpegEncContext * const s = &h->s;
  2542. *structure = s->picture_structure;
  2543. if(FIELD_PICTURE){
  2544. if (!(pic_num & 1))
  2545. /* opposite field */
  2546. *structure ^= PICT_FRAME;
  2547. pic_num >>= 1;
  2548. }
  2549. return pic_num;
  2550. }
  2551. static int decode_ref_pic_list_reordering(H264Context *h){
  2552. MpegEncContext * const s = &h->s;
  2553. int list, index, pic_structure;
  2554. print_short_term(h);
  2555. print_long_term(h);
  2556. for(list=0; list<h->list_count; list++){
  2557. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2558. if(get_bits1(&s->gb)){
  2559. int pred= h->curr_pic_num;
  2560. for(index=0; ; index++){
  2561. unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  2562. unsigned int pic_id;
  2563. int i;
  2564. Picture *ref = NULL;
  2565. if(reordering_of_pic_nums_idc==3)
  2566. break;
  2567. if(index >= h->ref_count[list]){
  2568. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  2569. return -1;
  2570. }
  2571. if(reordering_of_pic_nums_idc<3){
  2572. if(reordering_of_pic_nums_idc<2){
  2573. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2574. int frame_num;
  2575. if(abs_diff_pic_num > h->max_pic_num){
  2576. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  2577. return -1;
  2578. }
  2579. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2580. else pred+= abs_diff_pic_num;
  2581. pred &= h->max_pic_num - 1;
  2582. frame_num = pic_num_extract(h, pred, &pic_structure);
  2583. for(i= h->short_ref_count-1; i>=0; i--){
  2584. ref = h->short_ref[i];
  2585. assert(ref->reference);
  2586. assert(!ref->long_ref);
  2587. if(
  2588. ref->frame_num == frame_num &&
  2589. (ref->reference & pic_structure)
  2590. )
  2591. break;
  2592. }
  2593. if(i>=0)
  2594. ref->pic_id= pred;
  2595. }else{
  2596. int long_idx;
  2597. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2598. long_idx= pic_num_extract(h, pic_id, &pic_structure);
  2599. if(long_idx>31){
  2600. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  2601. return -1;
  2602. }
  2603. ref = h->long_ref[long_idx];
  2604. assert(!(ref && !ref->reference));
  2605. if(ref && (ref->reference & pic_structure)){
  2606. ref->pic_id= pic_id;
  2607. assert(ref->long_ref);
  2608. i=0;
  2609. }else{
  2610. i=-1;
  2611. }
  2612. }
  2613. if (i < 0) {
  2614. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  2615. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2616. } else {
  2617. for(i=index; i+1<h->ref_count[list]; i++){
  2618. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  2619. break;
  2620. }
  2621. for(; i > index; i--){
  2622. h->ref_list[list][i]= h->ref_list[list][i-1];
  2623. }
  2624. h->ref_list[list][index]= *ref;
  2625. if (FIELD_PICTURE){
  2626. pic_as_field(&h->ref_list[list][index], pic_structure);
  2627. }
  2628. }
  2629. }else{
  2630. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  2631. return -1;
  2632. }
  2633. }
  2634. }
  2635. }
  2636. for(list=0; list<h->list_count; list++){
  2637. for(index= 0; index < h->ref_count[list]; index++){
  2638. if(!h->ref_list[list][index].data[0]){
  2639. av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
  2640. h->ref_list[list][index]= s->current_picture; //FIXME this is not a sensible solution
  2641. }
  2642. }
  2643. }
  2644. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  2645. direct_dist_scale_factor(h);
  2646. direct_ref_list_init(h);
  2647. return 0;
  2648. }
  2649. static void fill_mbaff_ref_list(H264Context *h){
  2650. int list, i, j;
  2651. for(list=0; list<2; list++){ //FIXME try list_count
  2652. for(i=0; i<h->ref_count[list]; i++){
  2653. Picture *frame = &h->ref_list[list][i];
  2654. Picture *field = &h->ref_list[list][16+2*i];
  2655. field[0] = *frame;
  2656. for(j=0; j<3; j++)
  2657. field[0].linesize[j] <<= 1;
  2658. field[0].reference = PICT_TOP_FIELD;
  2659. field[1] = field[0];
  2660. for(j=0; j<3; j++)
  2661. field[1].data[j] += frame->linesize[j];
  2662. field[1].reference = PICT_BOTTOM_FIELD;
  2663. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  2664. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  2665. for(j=0; j<2; j++){
  2666. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  2667. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  2668. }
  2669. }
  2670. }
  2671. for(j=0; j<h->ref_count[1]; j++){
  2672. for(i=0; i<h->ref_count[0]; i++)
  2673. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  2674. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  2675. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  2676. }
  2677. }
  2678. static int pred_weight_table(H264Context *h){
  2679. MpegEncContext * const s = &h->s;
  2680. int list, i;
  2681. int luma_def, chroma_def;
  2682. h->use_weight= 0;
  2683. h->use_weight_chroma= 0;
  2684. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2685. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2686. luma_def = 1<<h->luma_log2_weight_denom;
  2687. chroma_def = 1<<h->chroma_log2_weight_denom;
  2688. for(list=0; list<2; list++){
  2689. for(i=0; i<h->ref_count[list]; i++){
  2690. int luma_weight_flag, chroma_weight_flag;
  2691. luma_weight_flag= get_bits1(&s->gb);
  2692. if(luma_weight_flag){
  2693. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2694. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2695. if( h->luma_weight[list][i] != luma_def
  2696. || h->luma_offset[list][i] != 0)
  2697. h->use_weight= 1;
  2698. }else{
  2699. h->luma_weight[list][i]= luma_def;
  2700. h->luma_offset[list][i]= 0;
  2701. }
  2702. if(CHROMA){
  2703. chroma_weight_flag= get_bits1(&s->gb);
  2704. if(chroma_weight_flag){
  2705. int j;
  2706. for(j=0; j<2; j++){
  2707. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2708. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2709. if( h->chroma_weight[list][i][j] != chroma_def
  2710. || h->chroma_offset[list][i][j] != 0)
  2711. h->use_weight_chroma= 1;
  2712. }
  2713. }else{
  2714. int j;
  2715. for(j=0; j<2; j++){
  2716. h->chroma_weight[list][i][j]= chroma_def;
  2717. h->chroma_offset[list][i][j]= 0;
  2718. }
  2719. }
  2720. }
  2721. }
  2722. if(h->slice_type_nos != FF_B_TYPE) break;
  2723. }
  2724. h->use_weight= h->use_weight || h->use_weight_chroma;
  2725. return 0;
  2726. }
  2727. static void implicit_weight_table(H264Context *h){
  2728. MpegEncContext * const s = &h->s;
  2729. int ref0, ref1;
  2730. int cur_poc = s->current_picture_ptr->poc;
  2731. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  2732. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  2733. h->use_weight= 0;
  2734. h->use_weight_chroma= 0;
  2735. return;
  2736. }
  2737. h->use_weight= 2;
  2738. h->use_weight_chroma= 2;
  2739. h->luma_log2_weight_denom= 5;
  2740. h->chroma_log2_weight_denom= 5;
  2741. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  2742. int poc0 = h->ref_list[0][ref0].poc;
  2743. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  2744. int poc1 = h->ref_list[1][ref1].poc;
  2745. int td = av_clip(poc1 - poc0, -128, 127);
  2746. if(td){
  2747. int tb = av_clip(cur_poc - poc0, -128, 127);
  2748. int tx = (16384 + (FFABS(td) >> 1)) / td;
  2749. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  2750. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  2751. h->implicit_weight[ref0][ref1] = 32;
  2752. else
  2753. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  2754. }else
  2755. h->implicit_weight[ref0][ref1] = 32;
  2756. }
  2757. }
  2758. }
  2759. /**
  2760. * Mark a picture as no longer needed for reference. The refmask
  2761. * argument allows unreferencing of individual fields or the whole frame.
  2762. * If the picture becomes entirely unreferenced, but is being held for
  2763. * display purposes, it is marked as such.
  2764. * @param refmask mask of fields to unreference; the mask is bitwise
  2765. * anded with the reference marking of pic
  2766. * @return non-zero if pic becomes entirely unreferenced (except possibly
  2767. * for display purposes) zero if one of the fields remains in
  2768. * reference
  2769. */
  2770. static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
  2771. int i;
  2772. if (pic->reference &= refmask) {
  2773. return 0;
  2774. } else {
  2775. for(i = 0; h->delayed_pic[i]; i++)
  2776. if(pic == h->delayed_pic[i]){
  2777. pic->reference=DELAYED_PIC_REF;
  2778. break;
  2779. }
  2780. return 1;
  2781. }
  2782. }
  2783. /**
  2784. * instantaneous decoder refresh.
  2785. */
  2786. static void idr(H264Context *h){
  2787. int i;
  2788. for(i=0; i<16; i++){
  2789. remove_long(h, i, 0);
  2790. }
  2791. assert(h->long_ref_count==0);
  2792. for(i=0; i<h->short_ref_count; i++){
  2793. unreference_pic(h, h->short_ref[i], 0);
  2794. h->short_ref[i]= NULL;
  2795. }
  2796. h->short_ref_count=0;
  2797. h->prev_frame_num= 0;
  2798. h->prev_frame_num_offset= 0;
  2799. h->prev_poc_msb=
  2800. h->prev_poc_lsb= 0;
  2801. }
  2802. /* forget old pics after a seek */
  2803. static void flush_dpb(AVCodecContext *avctx){
  2804. H264Context *h= avctx->priv_data;
  2805. int i;
  2806. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  2807. if(h->delayed_pic[i])
  2808. h->delayed_pic[i]->reference= 0;
  2809. h->delayed_pic[i]= NULL;
  2810. }
  2811. h->outputed_poc= INT_MIN;
  2812. idr(h);
  2813. if(h->s.current_picture_ptr)
  2814. h->s.current_picture_ptr->reference= 0;
  2815. h->s.first_field= 0;
  2816. ff_mpeg_flush(avctx);
  2817. }
  2818. /**
  2819. * Find a Picture in the short term reference list by frame number.
  2820. * @param frame_num frame number to search for
  2821. * @param idx the index into h->short_ref where returned picture is found
  2822. * undefined if no picture found.
  2823. * @return pointer to the found picture, or NULL if no pic with the provided
  2824. * frame number is found
  2825. */
  2826. static Picture * find_short(H264Context *h, int frame_num, int *idx){
  2827. MpegEncContext * const s = &h->s;
  2828. int i;
  2829. for(i=0; i<h->short_ref_count; i++){
  2830. Picture *pic= h->short_ref[i];
  2831. if(s->avctx->debug&FF_DEBUG_MMCO)
  2832. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  2833. if(pic->frame_num == frame_num) {
  2834. *idx = i;
  2835. return pic;
  2836. }
  2837. }
  2838. return NULL;
  2839. }
  2840. /**
  2841. * Remove a picture from the short term reference list by its index in
  2842. * that list. This does no checking on the provided index; it is assumed
  2843. * to be valid. Other list entries are shifted down.
  2844. * @param i index into h->short_ref of picture to remove.
  2845. */
  2846. static void remove_short_at_index(H264Context *h, int i){
  2847. assert(i >= 0 && i < h->short_ref_count);
  2848. h->short_ref[i]= NULL;
  2849. if (--h->short_ref_count)
  2850. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
  2851. }
  2852. /**
  2853. *
  2854. * @return the removed picture or NULL if an error occurs
  2855. */
  2856. static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
  2857. MpegEncContext * const s = &h->s;
  2858. Picture *pic;
  2859. int i;
  2860. if(s->avctx->debug&FF_DEBUG_MMCO)
  2861. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  2862. pic = find_short(h, frame_num, &i);
  2863. if (pic){
  2864. if(unreference_pic(h, pic, ref_mask))
  2865. remove_short_at_index(h, i);
  2866. }
  2867. return pic;
  2868. }
  2869. /**
  2870. * Remove a picture from the long term reference list by its index in
  2871. * that list.
  2872. * @return the removed picture or NULL if an error occurs
  2873. */
  2874. static Picture * remove_long(H264Context *h, int i, int ref_mask){
  2875. Picture *pic;
  2876. pic= h->long_ref[i];
  2877. if (pic){
  2878. if(unreference_pic(h, pic, ref_mask)){
  2879. assert(h->long_ref[i]->long_ref == 1);
  2880. h->long_ref[i]->long_ref= 0;
  2881. h->long_ref[i]= NULL;
  2882. h->long_ref_count--;
  2883. }
  2884. }
  2885. return pic;
  2886. }
  2887. /**
  2888. * print short term list
  2889. */
  2890. static void print_short_term(H264Context *h) {
  2891. uint32_t i;
  2892. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2893. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  2894. for(i=0; i<h->short_ref_count; i++){
  2895. Picture *pic= h->short_ref[i];
  2896. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2897. }
  2898. }
  2899. }
  2900. /**
  2901. * print long term list
  2902. */
  2903. static void print_long_term(H264Context *h) {
  2904. uint32_t i;
  2905. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2906. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  2907. for(i = 0; i < 16; i++){
  2908. Picture *pic= h->long_ref[i];
  2909. if (pic) {
  2910. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2911. }
  2912. }
  2913. }
  2914. }
  2915. /**
  2916. * Executes the reference picture marking (memory management control operations).
  2917. */
  2918. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2919. MpegEncContext * const s = &h->s;
  2920. int i, j;
  2921. int current_ref_assigned=0;
  2922. Picture *pic;
  2923. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2924. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  2925. for(i=0; i<mmco_count; i++){
  2926. int structure, frame_num;
  2927. if(s->avctx->debug&FF_DEBUG_MMCO)
  2928. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
  2929. if( mmco[i].opcode == MMCO_SHORT2UNUSED
  2930. || mmco[i].opcode == MMCO_SHORT2LONG){
  2931. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  2932. pic = find_short(h, frame_num, &j);
  2933. if(!pic){
  2934. if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
  2935. || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
  2936. av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
  2937. continue;
  2938. }
  2939. }
  2940. switch(mmco[i].opcode){
  2941. case MMCO_SHORT2UNUSED:
  2942. if(s->avctx->debug&FF_DEBUG_MMCO)
  2943. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
  2944. remove_short(h, frame_num, structure ^ PICT_FRAME);
  2945. break;
  2946. case MMCO_SHORT2LONG:
  2947. if (h->long_ref[mmco[i].long_arg] != pic)
  2948. remove_long(h, mmco[i].long_arg, 0);
  2949. remove_short_at_index(h, j);
  2950. h->long_ref[ mmco[i].long_arg ]= pic;
  2951. if (h->long_ref[ mmco[i].long_arg ]){
  2952. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  2953. h->long_ref_count++;
  2954. }
  2955. break;
  2956. case MMCO_LONG2UNUSED:
  2957. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  2958. pic = h->long_ref[j];
  2959. if (pic) {
  2960. remove_long(h, j, structure ^ PICT_FRAME);
  2961. } else if(s->avctx->debug&FF_DEBUG_MMCO)
  2962. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  2963. break;
  2964. case MMCO_LONG:
  2965. // Comment below left from previous code as it is an interresting note.
  2966. /* First field in pair is in short term list or
  2967. * at a different long term index.
  2968. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  2969. * Report the problem and keep the pair where it is,
  2970. * and mark this field valid.
  2971. */
  2972. if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
  2973. remove_long(h, mmco[i].long_arg, 0);
  2974. h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
  2975. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  2976. h->long_ref_count++;
  2977. }
  2978. s->current_picture_ptr->reference |= s->picture_structure;
  2979. current_ref_assigned=1;
  2980. break;
  2981. case MMCO_SET_MAX_LONG:
  2982. assert(mmco[i].long_arg <= 16);
  2983. // just remove the long term which index is greater than new max
  2984. for(j = mmco[i].long_arg; j<16; j++){
  2985. remove_long(h, j, 0);
  2986. }
  2987. break;
  2988. case MMCO_RESET:
  2989. while(h->short_ref_count){
  2990. remove_short(h, h->short_ref[0]->frame_num, 0);
  2991. }
  2992. for(j = 0; j < 16; j++) {
  2993. remove_long(h, j, 0);
  2994. }
  2995. s->current_picture_ptr->poc=
  2996. s->current_picture_ptr->field_poc[0]=
  2997. s->current_picture_ptr->field_poc[1]=
  2998. h->poc_lsb=
  2999. h->poc_msb=
  3000. h->frame_num=
  3001. s->current_picture_ptr->frame_num= 0;
  3002. break;
  3003. default: assert(0);
  3004. }
  3005. }
  3006. if (!current_ref_assigned) {
  3007. /* Second field of complementary field pair; the first field of
  3008. * which is already referenced. If short referenced, it
  3009. * should be first entry in short_ref. If not, it must exist
  3010. * in long_ref; trying to put it on the short list here is an
  3011. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  3012. */
  3013. if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
  3014. /* Just mark the second field valid */
  3015. s->current_picture_ptr->reference = PICT_FRAME;
  3016. } else if (s->current_picture_ptr->long_ref) {
  3017. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
  3018. "assignment for second field "
  3019. "in complementary field pair "
  3020. "(first field is long term)\n");
  3021. } else {
  3022. pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
  3023. if(pic){
  3024. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3025. }
  3026. if(h->short_ref_count)
  3027. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3028. h->short_ref[0]= s->current_picture_ptr;
  3029. h->short_ref_count++;
  3030. s->current_picture_ptr->reference |= s->picture_structure;
  3031. }
  3032. }
  3033. if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
  3034. /* We have too many reference frames, probably due to corrupted
  3035. * stream. Need to discard one frame. Prevents overrun of the
  3036. * short_ref and long_ref buffers.
  3037. */
  3038. av_log(h->s.avctx, AV_LOG_ERROR,
  3039. "number of reference frames exceeds max (probably "
  3040. "corrupt input), discarding one\n");
  3041. if (h->long_ref_count && !h->short_ref_count) {
  3042. for (i = 0; i < 16; ++i)
  3043. if (h->long_ref[i])
  3044. break;
  3045. assert(i < 16);
  3046. remove_long(h, i, 0);
  3047. } else {
  3048. pic = h->short_ref[h->short_ref_count - 1];
  3049. remove_short(h, pic->frame_num, 0);
  3050. }
  3051. }
  3052. print_short_term(h);
  3053. print_long_term(h);
  3054. return 0;
  3055. }
  3056. static int decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
  3057. MpegEncContext * const s = &h->s;
  3058. int i;
  3059. h->mmco_index= 0;
  3060. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3061. s->broken_link= get_bits1(gb) -1;
  3062. if(get_bits1(gb)){
  3063. h->mmco[0].opcode= MMCO_LONG;
  3064. h->mmco[0].long_arg= 0;
  3065. h->mmco_index= 1;
  3066. }
  3067. }else{
  3068. if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
  3069. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3070. MMCOOpcode opcode= get_ue_golomb(gb);
  3071. h->mmco[i].opcode= opcode;
  3072. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3073. h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
  3074. /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
  3075. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3076. return -1;
  3077. }*/
  3078. }
  3079. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3080. unsigned int long_arg= get_ue_golomb(gb);
  3081. if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
  3082. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3083. return -1;
  3084. }
  3085. h->mmco[i].long_arg= long_arg;
  3086. }
  3087. if(opcode > (unsigned)MMCO_LONG){
  3088. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3089. return -1;
  3090. }
  3091. if(opcode == MMCO_END)
  3092. break;
  3093. }
  3094. h->mmco_index= i;
  3095. }else{
  3096. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3097. if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
  3098. !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
  3099. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3100. h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3101. h->mmco_index= 1;
  3102. if (FIELD_PICTURE) {
  3103. h->mmco[0].short_pic_num *= 2;
  3104. h->mmco[1].opcode= MMCO_SHORT2UNUSED;
  3105. h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
  3106. h->mmco_index= 2;
  3107. }
  3108. }
  3109. }
  3110. }
  3111. return 0;
  3112. }
  3113. static int init_poc(H264Context *h){
  3114. MpegEncContext * const s = &h->s;
  3115. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3116. int field_poc[2];
  3117. Picture *cur = s->current_picture_ptr;
  3118. h->frame_num_offset= h->prev_frame_num_offset;
  3119. if(h->frame_num < h->prev_frame_num)
  3120. h->frame_num_offset += max_frame_num;
  3121. if(h->sps.poc_type==0){
  3122. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3123. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3124. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3125. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3126. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3127. else
  3128. h->poc_msb = h->prev_poc_msb;
  3129. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3130. field_poc[0] =
  3131. field_poc[1] = h->poc_msb + h->poc_lsb;
  3132. if(s->picture_structure == PICT_FRAME)
  3133. field_poc[1] += h->delta_poc_bottom;
  3134. }else if(h->sps.poc_type==1){
  3135. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3136. int i;
  3137. if(h->sps.poc_cycle_length != 0)
  3138. abs_frame_num = h->frame_num_offset + h->frame_num;
  3139. else
  3140. abs_frame_num = 0;
  3141. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3142. abs_frame_num--;
  3143. expected_delta_per_poc_cycle = 0;
  3144. for(i=0; i < h->sps.poc_cycle_length; i++)
  3145. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3146. if(abs_frame_num > 0){
  3147. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3148. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3149. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3150. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3151. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3152. } else
  3153. expectedpoc = 0;
  3154. if(h->nal_ref_idc == 0)
  3155. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3156. field_poc[0] = expectedpoc + h->delta_poc[0];
  3157. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3158. if(s->picture_structure == PICT_FRAME)
  3159. field_poc[1] += h->delta_poc[1];
  3160. }else{
  3161. int poc= 2*(h->frame_num_offset + h->frame_num);
  3162. if(!h->nal_ref_idc)
  3163. poc--;
  3164. field_poc[0]= poc;
  3165. field_poc[1]= poc;
  3166. }
  3167. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3168. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3169. if(s->picture_structure != PICT_TOP_FIELD)
  3170. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3171. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  3172. return 0;
  3173. }
  3174. /**
  3175. * initialize scan tables
  3176. */
  3177. static void init_scan_tables(H264Context *h){
  3178. MpegEncContext * const s = &h->s;
  3179. int i;
  3180. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  3181. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  3182. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  3183. }else{
  3184. for(i=0; i<16; i++){
  3185. #define T(x) (x>>2) | ((x<<2) & 0xF)
  3186. h->zigzag_scan[i] = T(zigzag_scan[i]);
  3187. h-> field_scan[i] = T( field_scan[i]);
  3188. #undef T
  3189. }
  3190. }
  3191. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  3192. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  3193. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  3194. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  3195. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  3196. }else{
  3197. for(i=0; i<64; i++){
  3198. #define T(x) (x>>3) | ((x&7)<<3)
  3199. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  3200. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  3201. h->field_scan8x8[i] = T(field_scan8x8[i]);
  3202. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  3203. #undef T
  3204. }
  3205. }
  3206. if(h->sps.transform_bypass){ //FIXME same ugly
  3207. h->zigzag_scan_q0 = zigzag_scan;
  3208. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  3209. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  3210. h->field_scan_q0 = field_scan;
  3211. h->field_scan8x8_q0 = field_scan8x8;
  3212. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  3213. }else{
  3214. h->zigzag_scan_q0 = h->zigzag_scan;
  3215. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  3216. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  3217. h->field_scan_q0 = h->field_scan;
  3218. h->field_scan8x8_q0 = h->field_scan8x8;
  3219. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  3220. }
  3221. }
  3222. /**
  3223. * Replicates H264 "master" context to thread contexts.
  3224. */
  3225. static void clone_slice(H264Context *dst, H264Context *src)
  3226. {
  3227. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  3228. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  3229. dst->s.current_picture = src->s.current_picture;
  3230. dst->s.linesize = src->s.linesize;
  3231. dst->s.uvlinesize = src->s.uvlinesize;
  3232. dst->s.first_field = src->s.first_field;
  3233. dst->prev_poc_msb = src->prev_poc_msb;
  3234. dst->prev_poc_lsb = src->prev_poc_lsb;
  3235. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  3236. dst->prev_frame_num = src->prev_frame_num;
  3237. dst->short_ref_count = src->short_ref_count;
  3238. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  3239. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  3240. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  3241. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  3242. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  3243. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  3244. }
  3245. /**
  3246. * decodes a slice header.
  3247. * This will also call MPV_common_init() and frame_start() as needed.
  3248. *
  3249. * @param h h264context
  3250. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  3251. *
  3252. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  3253. */
  3254. static int decode_slice_header(H264Context *h, H264Context *h0){
  3255. MpegEncContext * const s = &h->s;
  3256. MpegEncContext * const s0 = &h0->s;
  3257. unsigned int first_mb_in_slice;
  3258. unsigned int pps_id;
  3259. int num_ref_idx_active_override_flag;
  3260. static const uint8_t slice_type_map[5]= {FF_P_TYPE, FF_B_TYPE, FF_I_TYPE, FF_SP_TYPE, FF_SI_TYPE};
  3261. unsigned int slice_type, tmp, i, j;
  3262. int default_ref_list_done = 0;
  3263. int last_pic_structure;
  3264. s->dropable= h->nal_ref_idc == 0;
  3265. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  3266. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  3267. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  3268. }else{
  3269. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  3270. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  3271. }
  3272. first_mb_in_slice= get_ue_golomb(&s->gb);
  3273. if((s->flags2 & CODEC_FLAG2_CHUNKS) && first_mb_in_slice == 0){
  3274. h0->current_slice = 0;
  3275. if (!s0->first_field)
  3276. s->current_picture_ptr= NULL;
  3277. }
  3278. slice_type= get_ue_golomb(&s->gb);
  3279. if(slice_type > 9){
  3280. 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);
  3281. return -1;
  3282. }
  3283. if(slice_type > 4){
  3284. slice_type -= 5;
  3285. h->slice_type_fixed=1;
  3286. }else
  3287. h->slice_type_fixed=0;
  3288. slice_type= slice_type_map[ slice_type ];
  3289. if (slice_type == FF_I_TYPE
  3290. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  3291. default_ref_list_done = 1;
  3292. }
  3293. h->slice_type= slice_type;
  3294. h->slice_type_nos= slice_type & 3;
  3295. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  3296. if (s->pict_type == FF_B_TYPE && s0->last_picture_ptr == NULL) {
  3297. av_log(h->s.avctx, AV_LOG_ERROR,
  3298. "B picture before any references, skipping\n");
  3299. return -1;
  3300. }
  3301. pps_id= get_ue_golomb(&s->gb);
  3302. if(pps_id>=MAX_PPS_COUNT){
  3303. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3304. return -1;
  3305. }
  3306. if(!h0->pps_buffers[pps_id]) {
  3307. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
  3308. return -1;
  3309. }
  3310. h->pps= *h0->pps_buffers[pps_id];
  3311. if(!h0->sps_buffers[h->pps.sps_id]) {
  3312. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
  3313. return -1;
  3314. }
  3315. h->sps = *h0->sps_buffers[h->pps.sps_id];
  3316. if(h == h0 && h->dequant_coeff_pps != pps_id){
  3317. h->dequant_coeff_pps = pps_id;
  3318. init_dequant_tables(h);
  3319. }
  3320. s->mb_width= h->sps.mb_width;
  3321. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3322. h->b_stride= s->mb_width*4;
  3323. h->b8_stride= s->mb_width*2;
  3324. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  3325. if(h->sps.frame_mbs_only_flag)
  3326. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  3327. else
  3328. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3);
  3329. if (s->context_initialized
  3330. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3331. if(h != h0)
  3332. return -1; // width / height changed during parallelized decoding
  3333. free_tables(h);
  3334. MPV_common_end(s);
  3335. }
  3336. if (!s->context_initialized) {
  3337. if(h != h0)
  3338. return -1; // we cant (re-)initialize context during parallel decoding
  3339. if (MPV_common_init(s) < 0)
  3340. return -1;
  3341. s->first_field = 0;
  3342. init_scan_tables(h);
  3343. alloc_tables(h);
  3344. for(i = 1; i < s->avctx->thread_count; i++) {
  3345. H264Context *c;
  3346. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  3347. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  3348. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  3349. c->sps = h->sps;
  3350. c->pps = h->pps;
  3351. init_scan_tables(c);
  3352. clone_tables(c, h);
  3353. }
  3354. for(i = 0; i < s->avctx->thread_count; i++)
  3355. if(context_init(h->thread_context[i]) < 0)
  3356. return -1;
  3357. s->avctx->width = s->width;
  3358. s->avctx->height = s->height;
  3359. s->avctx->sample_aspect_ratio= h->sps.sar;
  3360. if(!s->avctx->sample_aspect_ratio.den)
  3361. s->avctx->sample_aspect_ratio.den = 1;
  3362. if(h->sps.timing_info_present_flag){
  3363. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  3364. if(h->x264_build > 0 && h->x264_build < 44)
  3365. s->avctx->time_base.den *= 2;
  3366. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  3367. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  3368. }
  3369. }
  3370. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3371. h->mb_mbaff = 0;
  3372. h->mb_aff_frame = 0;
  3373. last_pic_structure = s0->picture_structure;
  3374. if(h->sps.frame_mbs_only_flag){
  3375. s->picture_structure= PICT_FRAME;
  3376. }else{
  3377. if(get_bits1(&s->gb)) { //field_pic_flag
  3378. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3379. } else {
  3380. s->picture_structure= PICT_FRAME;
  3381. h->mb_aff_frame = h->sps.mb_aff;
  3382. }
  3383. }
  3384. if(h0->current_slice == 0){
  3385. while(h->frame_num != h->prev_frame_num &&
  3386. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  3387. av_log(NULL, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  3388. frame_start(h);
  3389. h->prev_frame_num++;
  3390. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  3391. s->current_picture_ptr->frame_num= h->prev_frame_num;
  3392. execute_ref_pic_marking(h, NULL, 0);
  3393. }
  3394. /* See if we have a decoded first field looking for a pair... */
  3395. if (s0->first_field) {
  3396. assert(s0->current_picture_ptr);
  3397. assert(s0->current_picture_ptr->data[0]);
  3398. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  3399. /* figure out if we have a complementary field pair */
  3400. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  3401. /*
  3402. * Previous field is unmatched. Don't display it, but let it
  3403. * remain for reference if marked as such.
  3404. */
  3405. s0->current_picture_ptr = NULL;
  3406. s0->first_field = FIELD_PICTURE;
  3407. } else {
  3408. if (h->nal_ref_idc &&
  3409. s0->current_picture_ptr->reference &&
  3410. s0->current_picture_ptr->frame_num != h->frame_num) {
  3411. /*
  3412. * This and previous field were reference, but had
  3413. * different frame_nums. Consider this field first in
  3414. * pair. Throw away previous field except for reference
  3415. * purposes.
  3416. */
  3417. s0->first_field = 1;
  3418. s0->current_picture_ptr = NULL;
  3419. } else {
  3420. /* Second field in complementary pair */
  3421. s0->first_field = 0;
  3422. }
  3423. }
  3424. } else {
  3425. /* Frame or first field in a potentially complementary pair */
  3426. assert(!s0->current_picture_ptr);
  3427. s0->first_field = FIELD_PICTURE;
  3428. }
  3429. if((!FIELD_PICTURE || s0->first_field) && frame_start(h) < 0) {
  3430. s0->first_field = 0;
  3431. return -1;
  3432. }
  3433. }
  3434. if(h != h0)
  3435. clone_slice(h, h0);
  3436. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  3437. assert(s->mb_num == s->mb_width * s->mb_height);
  3438. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  3439. first_mb_in_slice >= s->mb_num){
  3440. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  3441. return -1;
  3442. }
  3443. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3444. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  3445. if (s->picture_structure == PICT_BOTTOM_FIELD)
  3446. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  3447. assert(s->mb_y < s->mb_height);
  3448. if(s->picture_structure==PICT_FRAME){
  3449. h->curr_pic_num= h->frame_num;
  3450. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3451. }else{
  3452. h->curr_pic_num= 2*h->frame_num + 1;
  3453. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3454. }
  3455. if(h->nal_unit_type == NAL_IDR_SLICE){
  3456. get_ue_golomb(&s->gb); /* idr_pic_id */
  3457. }
  3458. if(h->sps.poc_type==0){
  3459. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3460. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3461. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3462. }
  3463. }
  3464. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3465. h->delta_poc[0]= get_se_golomb(&s->gb);
  3466. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3467. h->delta_poc[1]= get_se_golomb(&s->gb);
  3468. }
  3469. init_poc(h);
  3470. if(h->pps.redundant_pic_cnt_present){
  3471. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3472. }
  3473. //set defaults, might be overridden a few lines later
  3474. h->ref_count[0]= h->pps.ref_count[0];
  3475. h->ref_count[1]= h->pps.ref_count[1];
  3476. if(h->slice_type_nos != FF_I_TYPE){
  3477. if(h->slice_type_nos == FF_B_TYPE){
  3478. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3479. }
  3480. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3481. if(num_ref_idx_active_override_flag){
  3482. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3483. if(h->slice_type_nos==FF_B_TYPE)
  3484. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3485. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  3486. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3487. h->ref_count[0]= h->ref_count[1]= 1;
  3488. return -1;
  3489. }
  3490. }
  3491. if(h->slice_type_nos == FF_B_TYPE)
  3492. h->list_count= 2;
  3493. else
  3494. h->list_count= 1;
  3495. }else
  3496. h->list_count= 0;
  3497. if(!default_ref_list_done){
  3498. fill_default_ref_list(h);
  3499. }
  3500. if(h->slice_type_nos!=FF_I_TYPE && decode_ref_pic_list_reordering(h) < 0)
  3501. return -1;
  3502. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  3503. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  3504. pred_weight_table(h);
  3505. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE)
  3506. implicit_weight_table(h);
  3507. else
  3508. h->use_weight = 0;
  3509. if(h->nal_ref_idc)
  3510. decode_ref_pic_marking(h0, &s->gb);
  3511. if(FRAME_MBAFF)
  3512. fill_mbaff_ref_list(h);
  3513. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  3514. tmp = get_ue_golomb(&s->gb);
  3515. if(tmp > 2){
  3516. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3517. return -1;
  3518. }
  3519. h->cabac_init_idc= tmp;
  3520. }
  3521. h->last_qscale_diff = 0;
  3522. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  3523. if(tmp>51){
  3524. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3525. return -1;
  3526. }
  3527. s->qscale= tmp;
  3528. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  3529. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  3530. //FIXME qscale / qp ... stuff
  3531. if(h->slice_type == FF_SP_TYPE){
  3532. get_bits1(&s->gb); /* sp_for_switch_flag */
  3533. }
  3534. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  3535. get_se_golomb(&s->gb); /* slice_qs_delta */
  3536. }
  3537. h->deblocking_filter = 1;
  3538. h->slice_alpha_c0_offset = 0;
  3539. h->slice_beta_offset = 0;
  3540. if( h->pps.deblocking_filter_parameters_present ) {
  3541. tmp= get_ue_golomb(&s->gb);
  3542. if(tmp > 2){
  3543. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  3544. return -1;
  3545. }
  3546. h->deblocking_filter= tmp;
  3547. if(h->deblocking_filter < 2)
  3548. h->deblocking_filter^= 1; // 1<->0
  3549. if( h->deblocking_filter ) {
  3550. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3551. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3552. }
  3553. }
  3554. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  3555. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  3556. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  3557. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  3558. h->deblocking_filter= 0;
  3559. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  3560. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  3561. /* Cheat slightly for speed:
  3562. Do not bother to deblock across slices. */
  3563. h->deblocking_filter = 2;
  3564. } else {
  3565. h0->max_contexts = 1;
  3566. if(!h0->single_decode_warning) {
  3567. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  3568. h0->single_decode_warning = 1;
  3569. }
  3570. if(h != h0)
  3571. return 1; // deblocking switched inside frame
  3572. }
  3573. }
  3574. #if 0 //FMO
  3575. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3576. slice_group_change_cycle= get_bits(&s->gb, ?);
  3577. #endif
  3578. h0->last_slice_type = slice_type;
  3579. h->slice_num = ++h0->current_slice;
  3580. for(j=0; j<2; j++){
  3581. int *ref2frm= h->ref2frm[h->slice_num&15][j];
  3582. ref2frm[0]=
  3583. ref2frm[1]= -1;
  3584. for(i=0; i<48; i++)
  3585. ref2frm[i+2]= 4*h->ref_list[j][i].frame_num
  3586. +(h->ref_list[j][i].reference&3);
  3587. }
  3588. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  3589. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  3590. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3591. 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 %s\n",
  3592. h->slice_num,
  3593. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3594. first_mb_in_slice,
  3595. av_get_pict_type_char(h->slice_type),
  3596. pps_id, h->frame_num,
  3597. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3598. h->ref_count[0], h->ref_count[1],
  3599. s->qscale,
  3600. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3601. h->use_weight,
  3602. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  3603. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  3604. );
  3605. }
  3606. return 0;
  3607. }
  3608. /**
  3609. *
  3610. */
  3611. static inline int get_level_prefix(GetBitContext *gb){
  3612. unsigned int buf;
  3613. int log;
  3614. OPEN_READER(re, gb);
  3615. UPDATE_CACHE(re, gb);
  3616. buf=GET_CACHE(re, gb);
  3617. log= 32 - av_log2(buf);
  3618. #ifdef TRACE
  3619. print_bin(buf>>(32-log), log);
  3620. 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__);
  3621. #endif
  3622. LAST_SKIP_BITS(re, gb, log);
  3623. CLOSE_READER(re, gb);
  3624. return log-1;
  3625. }
  3626. static inline int get_dct8x8_allowed(H264Context *h){
  3627. int i;
  3628. for(i=0; i<4; i++){
  3629. if(!IS_SUB_8X8(h->sub_mb_type[i])
  3630. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  3631. return 0;
  3632. }
  3633. return 1;
  3634. }
  3635. /**
  3636. * decodes a residual block.
  3637. * @param n block index
  3638. * @param scantable scantable
  3639. * @param max_coeff number of coefficients in the block
  3640. * @return <0 if an error occurred
  3641. */
  3642. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  3643. MpegEncContext * const s = &h->s;
  3644. 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};
  3645. int level[16];
  3646. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  3647. //FIXME put trailing_onex into the context
  3648. if(n == CHROMA_DC_BLOCK_INDEX){
  3649. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  3650. total_coeff= coeff_token>>2;
  3651. }else{
  3652. if(n == LUMA_DC_BLOCK_INDEX){
  3653. total_coeff= pred_non_zero_count(h, 0);
  3654. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3655. total_coeff= coeff_token>>2;
  3656. }else{
  3657. total_coeff= pred_non_zero_count(h, n);
  3658. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3659. total_coeff= coeff_token>>2;
  3660. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  3661. }
  3662. }
  3663. //FIXME set last_non_zero?
  3664. if(total_coeff==0)
  3665. return 0;
  3666. if(total_coeff > (unsigned)max_coeff) {
  3667. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  3668. return -1;
  3669. }
  3670. trailing_ones= coeff_token&3;
  3671. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  3672. assert(total_coeff<=16);
  3673. for(i=0; i<trailing_ones; i++){
  3674. level[i]= 1 - 2*get_bits1(gb);
  3675. }
  3676. if(i<total_coeff) {
  3677. int level_code, mask;
  3678. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  3679. int prefix= get_level_prefix(gb);
  3680. //first coefficient has suffix_length equal to 0 or 1
  3681. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  3682. if(suffix_length)
  3683. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3684. else
  3685. level_code= (prefix<<suffix_length); //part
  3686. }else if(prefix==14){
  3687. if(suffix_length)
  3688. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3689. else
  3690. level_code= prefix + get_bits(gb, 4); //part
  3691. }else{
  3692. level_code= (15<<suffix_length) + get_bits(gb, prefix-3); //part
  3693. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  3694. if(prefix>=16)
  3695. level_code += (1<<(prefix-3))-4096;
  3696. }
  3697. if(trailing_ones < 3) level_code += 2;
  3698. suffix_length = 1;
  3699. if(level_code > 5)
  3700. suffix_length++;
  3701. mask= -(level_code&1);
  3702. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  3703. i++;
  3704. //remaining coefficients have suffix_length > 0
  3705. for(;i<total_coeff;i++) {
  3706. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  3707. prefix = get_level_prefix(gb);
  3708. if(prefix<15){
  3709. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  3710. }else{
  3711. level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
  3712. if(prefix>=16)
  3713. level_code += (1<<(prefix-3))-4096;
  3714. }
  3715. mask= -(level_code&1);
  3716. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  3717. if(level_code > suffix_limit[suffix_length])
  3718. suffix_length++;
  3719. }
  3720. }
  3721. if(total_coeff == max_coeff)
  3722. zeros_left=0;
  3723. else{
  3724. if(n == CHROMA_DC_BLOCK_INDEX)
  3725. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  3726. else
  3727. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  3728. }
  3729. coeff_num = zeros_left + total_coeff - 1;
  3730. j = scantable[coeff_num];
  3731. if(n > 24){
  3732. block[j] = level[0];
  3733. for(i=1;i<total_coeff;i++) {
  3734. if(zeros_left <= 0)
  3735. run_before = 0;
  3736. else if(zeros_left < 7){
  3737. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3738. }else{
  3739. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3740. }
  3741. zeros_left -= run_before;
  3742. coeff_num -= 1 + run_before;
  3743. j= scantable[ coeff_num ];
  3744. block[j]= level[i];
  3745. }
  3746. }else{
  3747. block[j] = (level[0] * qmul[j] + 32)>>6;
  3748. for(i=1;i<total_coeff;i++) {
  3749. if(zeros_left <= 0)
  3750. run_before = 0;
  3751. else if(zeros_left < 7){
  3752. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3753. }else{
  3754. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3755. }
  3756. zeros_left -= run_before;
  3757. coeff_num -= 1 + run_before;
  3758. j= scantable[ coeff_num ];
  3759. block[j]= (level[i] * qmul[j] + 32)>>6;
  3760. }
  3761. }
  3762. if(zeros_left<0){
  3763. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  3764. return -1;
  3765. }
  3766. return 0;
  3767. }
  3768. static void predict_field_decoding_flag(H264Context *h){
  3769. MpegEncContext * const s = &h->s;
  3770. const int mb_xy= h->mb_xy;
  3771. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  3772. ? s->current_picture.mb_type[mb_xy-1]
  3773. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  3774. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  3775. : 0;
  3776. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  3777. }
  3778. /**
  3779. * decodes a P_SKIP or B_SKIP macroblock
  3780. */
  3781. static void decode_mb_skip(H264Context *h){
  3782. MpegEncContext * const s = &h->s;
  3783. const int mb_xy= h->mb_xy;
  3784. int mb_type=0;
  3785. memset(h->non_zero_count[mb_xy], 0, 16);
  3786. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  3787. if(MB_FIELD)
  3788. mb_type|= MB_TYPE_INTERLACED;
  3789. if( h->slice_type_nos == FF_B_TYPE )
  3790. {
  3791. // just for fill_caches. pred_direct_motion will set the real mb_type
  3792. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  3793. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3794. pred_direct_motion(h, &mb_type);
  3795. mb_type|= MB_TYPE_SKIP;
  3796. }
  3797. else
  3798. {
  3799. int mx, my;
  3800. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  3801. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3802. pred_pskip_motion(h, &mx, &my);
  3803. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  3804. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  3805. }
  3806. write_back_motion(h, mb_type);
  3807. s->current_picture.mb_type[mb_xy]= mb_type;
  3808. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3809. h->slice_table[ mb_xy ]= h->slice_num;
  3810. h->prev_mb_skipped= 1;
  3811. }
  3812. /**
  3813. * decodes a macroblock
  3814. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  3815. */
  3816. static int decode_mb_cavlc(H264Context *h){
  3817. MpegEncContext * const s = &h->s;
  3818. int mb_xy;
  3819. int partition_count;
  3820. unsigned int mb_type, cbp;
  3821. int dct8x8_allowed= h->pps.transform_8x8_mode;
  3822. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3823. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  3824. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  3825. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  3826. down the code */
  3827. if(h->slice_type_nos != FF_I_TYPE){
  3828. if(s->mb_skip_run==-1)
  3829. s->mb_skip_run= get_ue_golomb(&s->gb);
  3830. if (s->mb_skip_run--) {
  3831. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  3832. if(s->mb_skip_run==0)
  3833. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  3834. else
  3835. predict_field_decoding_flag(h);
  3836. }
  3837. decode_mb_skip(h);
  3838. return 0;
  3839. }
  3840. }
  3841. if(FRAME_MBAFF){
  3842. if( (s->mb_y&1) == 0 )
  3843. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  3844. }else
  3845. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  3846. h->prev_mb_skipped= 0;
  3847. mb_type= get_ue_golomb(&s->gb);
  3848. if(h->slice_type_nos == FF_B_TYPE){
  3849. if(mb_type < 23){
  3850. partition_count= b_mb_type_info[mb_type].partition_count;
  3851. mb_type= b_mb_type_info[mb_type].type;
  3852. }else{
  3853. mb_type -= 23;
  3854. goto decode_intra_mb;
  3855. }
  3856. }else if(h->slice_type_nos == FF_P_TYPE){
  3857. if(mb_type < 5){
  3858. partition_count= p_mb_type_info[mb_type].partition_count;
  3859. mb_type= p_mb_type_info[mb_type].type;
  3860. }else{
  3861. mb_type -= 5;
  3862. goto decode_intra_mb;
  3863. }
  3864. }else{
  3865. assert(h->slice_type_nos == FF_I_TYPE);
  3866. if(h->slice_type == FF_SI_TYPE && mb_type)
  3867. mb_type--;
  3868. decode_intra_mb:
  3869. if(mb_type > 25){
  3870. 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);
  3871. return -1;
  3872. }
  3873. partition_count=0;
  3874. cbp= i_mb_type_info[mb_type].cbp;
  3875. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  3876. mb_type= i_mb_type_info[mb_type].type;
  3877. }
  3878. if(MB_FIELD)
  3879. mb_type |= MB_TYPE_INTERLACED;
  3880. h->slice_table[ mb_xy ]= h->slice_num;
  3881. if(IS_INTRA_PCM(mb_type)){
  3882. unsigned int x, y;
  3883. // We assume these blocks are very rare so we do not optimize it.
  3884. align_get_bits(&s->gb);
  3885. // The pixels are stored in the same order as levels in h->mb array.
  3886. for(y=0; y<16; y++){
  3887. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  3888. for(x=0; x<16; x++){
  3889. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3890. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  3891. }
  3892. }
  3893. if(CHROMA){
  3894. for(y=0; y<8; y++){
  3895. const int index= 256 + 4*(y&3) + 32*(y>>2);
  3896. for(x=0; x<8; x++){
  3897. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3898. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  3899. }
  3900. }
  3901. for(y=0; y<8; y++){
  3902. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  3903. for(x=0; x<8; x++){
  3904. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3905. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  3906. }
  3907. }
  3908. }
  3909. // In deblocking, the quantizer is 0
  3910. s->current_picture.qscale_table[mb_xy]= 0;
  3911. // All coeffs are present
  3912. memset(h->non_zero_count[mb_xy], 16, 16);
  3913. s->current_picture.mb_type[mb_xy]= mb_type;
  3914. return 0;
  3915. }
  3916. if(MB_MBAFF){
  3917. h->ref_count[0] <<= 1;
  3918. h->ref_count[1] <<= 1;
  3919. }
  3920. fill_caches(h, mb_type, 0);
  3921. //mb_pred
  3922. if(IS_INTRA(mb_type)){
  3923. int pred_mode;
  3924. // init_top_left_availability(h);
  3925. if(IS_INTRA4x4(mb_type)){
  3926. int i;
  3927. int di = 1;
  3928. if(dct8x8_allowed && get_bits1(&s->gb)){
  3929. mb_type |= MB_TYPE_8x8DCT;
  3930. di = 4;
  3931. }
  3932. // fill_intra4x4_pred_table(h);
  3933. for(i=0; i<16; i+=di){
  3934. int mode= pred_intra_mode(h, i);
  3935. if(!get_bits1(&s->gb)){
  3936. const int rem_mode= get_bits(&s->gb, 3);
  3937. mode = rem_mode + (rem_mode >= mode);
  3938. }
  3939. if(di==4)
  3940. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  3941. else
  3942. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  3943. }
  3944. write_back_intra_pred_mode(h);
  3945. if( check_intra4x4_pred_mode(h) < 0)
  3946. return -1;
  3947. }else{
  3948. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  3949. if(h->intra16x16_pred_mode < 0)
  3950. return -1;
  3951. }
  3952. if(CHROMA){
  3953. pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
  3954. if(pred_mode < 0)
  3955. return -1;
  3956. h->chroma_pred_mode= pred_mode;
  3957. }
  3958. }else if(partition_count==4){
  3959. int i, j, sub_partition_count[4], list, ref[2][4];
  3960. if(h->slice_type_nos == FF_B_TYPE){
  3961. for(i=0; i<4; i++){
  3962. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  3963. if(h->sub_mb_type[i] >=13){
  3964. 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);
  3965. return -1;
  3966. }
  3967. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3968. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3969. }
  3970. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  3971. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  3972. pred_direct_motion(h, &mb_type);
  3973. h->ref_cache[0][scan8[4]] =
  3974. h->ref_cache[1][scan8[4]] =
  3975. h->ref_cache[0][scan8[12]] =
  3976. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  3977. }
  3978. }else{
  3979. assert(h->slice_type_nos == FF_P_TYPE); //FIXME SP correct ?
  3980. for(i=0; i<4; i++){
  3981. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  3982. if(h->sub_mb_type[i] >=4){
  3983. 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);
  3984. return -1;
  3985. }
  3986. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3987. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3988. }
  3989. }
  3990. for(list=0; list<h->list_count; list++){
  3991. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3992. for(i=0; i<4; i++){
  3993. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3994. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3995. unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  3996. if(tmp>=ref_count){
  3997. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  3998. return -1;
  3999. }
  4000. ref[list][i]= tmp;
  4001. }else{
  4002. //FIXME
  4003. ref[list][i] = -1;
  4004. }
  4005. }
  4006. }
  4007. if(dct8x8_allowed)
  4008. dct8x8_allowed = get_dct8x8_allowed(h);
  4009. for(list=0; list<h->list_count; list++){
  4010. for(i=0; i<4; i++){
  4011. if(IS_DIRECT(h->sub_mb_type[i])) {
  4012. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4013. continue;
  4014. }
  4015. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4016. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4017. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4018. const int sub_mb_type= h->sub_mb_type[i];
  4019. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4020. for(j=0; j<sub_partition_count[i]; j++){
  4021. int mx, my;
  4022. const int index= 4*i + block_width*j;
  4023. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4024. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4025. mx += get_se_golomb(&s->gb);
  4026. my += get_se_golomb(&s->gb);
  4027. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4028. if(IS_SUB_8X8(sub_mb_type)){
  4029. mv_cache[ 1 ][0]=
  4030. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4031. mv_cache[ 1 ][1]=
  4032. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4033. }else if(IS_SUB_8X4(sub_mb_type)){
  4034. mv_cache[ 1 ][0]= mx;
  4035. mv_cache[ 1 ][1]= my;
  4036. }else if(IS_SUB_4X8(sub_mb_type)){
  4037. mv_cache[ 8 ][0]= mx;
  4038. mv_cache[ 8 ][1]= my;
  4039. }
  4040. mv_cache[ 0 ][0]= mx;
  4041. mv_cache[ 0 ][1]= my;
  4042. }
  4043. }else{
  4044. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4045. p[0] = p[1]=
  4046. p[8] = p[9]= 0;
  4047. }
  4048. }
  4049. }
  4050. }else if(IS_DIRECT(mb_type)){
  4051. pred_direct_motion(h, &mb_type);
  4052. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4053. }else{
  4054. int list, mx, my, i;
  4055. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4056. if(IS_16X16(mb_type)){
  4057. for(list=0; list<h->list_count; list++){
  4058. unsigned int val;
  4059. if(IS_DIR(mb_type, 0, list)){
  4060. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4061. if(val >= h->ref_count[list]){
  4062. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4063. return -1;
  4064. }
  4065. }else
  4066. val= LIST_NOT_USED&0xFF;
  4067. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4068. }
  4069. for(list=0; list<h->list_count; list++){
  4070. unsigned int val;
  4071. if(IS_DIR(mb_type, 0, list)){
  4072. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4073. mx += get_se_golomb(&s->gb);
  4074. my += get_se_golomb(&s->gb);
  4075. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4076. val= pack16to32(mx,my);
  4077. }else
  4078. val=0;
  4079. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  4080. }
  4081. }
  4082. else if(IS_16X8(mb_type)){
  4083. for(list=0; list<h->list_count; list++){
  4084. for(i=0; i<2; i++){
  4085. unsigned int val;
  4086. if(IS_DIR(mb_type, i, list)){
  4087. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4088. if(val >= h->ref_count[list]){
  4089. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4090. return -1;
  4091. }
  4092. }else
  4093. val= LIST_NOT_USED&0xFF;
  4094. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4095. }
  4096. }
  4097. for(list=0; list<h->list_count; list++){
  4098. for(i=0; i<2; i++){
  4099. unsigned int val;
  4100. if(IS_DIR(mb_type, i, list)){
  4101. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4102. mx += get_se_golomb(&s->gb);
  4103. my += get_se_golomb(&s->gb);
  4104. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4105. val= pack16to32(mx,my);
  4106. }else
  4107. val=0;
  4108. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  4109. }
  4110. }
  4111. }else{
  4112. assert(IS_8X16(mb_type));
  4113. for(list=0; list<h->list_count; list++){
  4114. for(i=0; i<2; i++){
  4115. unsigned int val;
  4116. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4117. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4118. if(val >= h->ref_count[list]){
  4119. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4120. return -1;
  4121. }
  4122. }else
  4123. val= LIST_NOT_USED&0xFF;
  4124. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4125. }
  4126. }
  4127. for(list=0; list<h->list_count; list++){
  4128. for(i=0; i<2; i++){
  4129. unsigned int val;
  4130. if(IS_DIR(mb_type, i, list)){
  4131. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4132. mx += get_se_golomb(&s->gb);
  4133. my += get_se_golomb(&s->gb);
  4134. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4135. val= pack16to32(mx,my);
  4136. }else
  4137. val=0;
  4138. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  4139. }
  4140. }
  4141. }
  4142. }
  4143. if(IS_INTER(mb_type))
  4144. write_back_motion(h, mb_type);
  4145. if(!IS_INTRA16x16(mb_type)){
  4146. cbp= get_ue_golomb(&s->gb);
  4147. if(cbp > 47){
  4148. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4149. return -1;
  4150. }
  4151. if(CHROMA){
  4152. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
  4153. else cbp= golomb_to_inter_cbp [cbp];
  4154. }else{
  4155. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
  4156. else cbp= golomb_to_inter_cbp_gray[cbp];
  4157. }
  4158. }
  4159. h->cbp = cbp;
  4160. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4161. if(get_bits1(&s->gb)){
  4162. mb_type |= MB_TYPE_8x8DCT;
  4163. h->cbp_table[mb_xy]= cbp;
  4164. }
  4165. }
  4166. s->current_picture.mb_type[mb_xy]= mb_type;
  4167. if(cbp || IS_INTRA16x16(mb_type)){
  4168. int i8x8, i4x4, chroma_idx;
  4169. int dquant;
  4170. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4171. const uint8_t *scan, *scan8x8, *dc_scan;
  4172. // fill_non_zero_count_cache(h);
  4173. if(IS_INTERLACED(mb_type)){
  4174. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4175. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4176. dc_scan= luma_dc_field_scan;
  4177. }else{
  4178. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4179. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4180. dc_scan= luma_dc_zigzag_scan;
  4181. }
  4182. dquant= get_se_golomb(&s->gb);
  4183. if( dquant > 25 || dquant < -26 ){
  4184. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4185. return -1;
  4186. }
  4187. s->qscale += dquant;
  4188. if(((unsigned)s->qscale) > 51){
  4189. if(s->qscale<0) s->qscale+= 52;
  4190. else s->qscale-= 52;
  4191. }
  4192. h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
  4193. h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
  4194. if(IS_INTRA16x16(mb_type)){
  4195. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4196. return -1; //FIXME continue if partitioned and other return -1 too
  4197. }
  4198. assert((cbp&15) == 0 || (cbp&15) == 15);
  4199. if(cbp&15){
  4200. for(i8x8=0; i8x8<4; i8x8++){
  4201. for(i4x4=0; i4x4<4; i4x4++){
  4202. const int index= i4x4 + 4*i8x8;
  4203. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4204. return -1;
  4205. }
  4206. }
  4207. }
  4208. }else{
  4209. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4210. }
  4211. }else{
  4212. for(i8x8=0; i8x8<4; i8x8++){
  4213. if(cbp & (1<<i8x8)){
  4214. if(IS_8x8DCT(mb_type)){
  4215. DCTELEM *buf = &h->mb[64*i8x8];
  4216. uint8_t *nnz;
  4217. for(i4x4=0; i4x4<4; i4x4++){
  4218. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4219. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4220. return -1;
  4221. }
  4222. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4223. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4224. }else{
  4225. for(i4x4=0; i4x4<4; i4x4++){
  4226. const int index= i4x4 + 4*i8x8;
  4227. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4228. return -1;
  4229. }
  4230. }
  4231. }
  4232. }else{
  4233. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4234. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4235. }
  4236. }
  4237. }
  4238. if(cbp&0x30){
  4239. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4240. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4241. return -1;
  4242. }
  4243. }
  4244. if(cbp&0x20){
  4245. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4246. const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
  4247. for(i4x4=0; i4x4<4; i4x4++){
  4248. const int index= 16 + 4*chroma_idx + i4x4;
  4249. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
  4250. return -1;
  4251. }
  4252. }
  4253. }
  4254. }else{
  4255. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4256. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4257. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4258. }
  4259. }else{
  4260. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4261. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4262. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4263. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4264. }
  4265. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4266. write_back_non_zero_count(h);
  4267. if(MB_MBAFF){
  4268. h->ref_count[0] >>= 1;
  4269. h->ref_count[1] >>= 1;
  4270. }
  4271. return 0;
  4272. }
  4273. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4274. MpegEncContext * const s = &h->s;
  4275. const int mb_x = s->mb_x;
  4276. const int mb_y = s->mb_y & ~1;
  4277. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4278. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4279. unsigned int ctx = 0;
  4280. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4281. ctx += 1;
  4282. }
  4283. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4284. ctx += 1;
  4285. }
  4286. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4287. }
  4288. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4289. uint8_t *state= &h->cabac_state[ctx_base];
  4290. int mb_type;
  4291. if(intra_slice){
  4292. MpegEncContext * const s = &h->s;
  4293. const int mba_xy = h->left_mb_xy[0];
  4294. const int mbb_xy = h->top_mb_xy;
  4295. int ctx=0;
  4296. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4297. ctx++;
  4298. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4299. ctx++;
  4300. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4301. return 0; /* I4x4 */
  4302. state += 2;
  4303. }else{
  4304. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4305. return 0; /* I4x4 */
  4306. }
  4307. if( get_cabac_terminate( &h->cabac ) )
  4308. return 25; /* PCM */
  4309. mb_type = 1; /* I16x16 */
  4310. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4311. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4312. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4313. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4314. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4315. return mb_type;
  4316. }
  4317. static int decode_cabac_mb_type( H264Context *h ) {
  4318. MpegEncContext * const s = &h->s;
  4319. if( h->slice_type_nos == FF_I_TYPE ) {
  4320. return decode_cabac_intra_mb_type(h, 3, 1);
  4321. } else if( h->slice_type_nos == FF_P_TYPE ) {
  4322. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4323. /* P-type */
  4324. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4325. /* P_L0_D16x16, P_8x8 */
  4326. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4327. } else {
  4328. /* P_L0_D8x16, P_L0_D16x8 */
  4329. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4330. }
  4331. } else {
  4332. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4333. }
  4334. } else if( h->slice_type_nos == FF_B_TYPE ) {
  4335. const int mba_xy = h->left_mb_xy[0];
  4336. const int mbb_xy = h->top_mb_xy;
  4337. int ctx = 0;
  4338. int bits;
  4339. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4340. ctx++;
  4341. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4342. ctx++;
  4343. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4344. return 0; /* B_Direct_16x16 */
  4345. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4346. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4347. }
  4348. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4349. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4350. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4351. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4352. if( bits < 8 )
  4353. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4354. else if( bits == 13 ) {
  4355. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4356. } else if( bits == 14 )
  4357. return 11; /* B_L1_L0_8x16 */
  4358. else if( bits == 15 )
  4359. return 22; /* B_8x8 */
  4360. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4361. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4362. } else {
  4363. /* TODO SI/SP frames? */
  4364. return -1;
  4365. }
  4366. }
  4367. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4368. MpegEncContext * const s = &h->s;
  4369. int mba_xy, mbb_xy;
  4370. int ctx = 0;
  4371. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4372. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4373. mba_xy = mb_xy - 1;
  4374. if( (mb_y&1)
  4375. && h->slice_table[mba_xy] == h->slice_num
  4376. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  4377. mba_xy += s->mb_stride;
  4378. if( MB_FIELD ){
  4379. mbb_xy = mb_xy - s->mb_stride;
  4380. if( !(mb_y&1)
  4381. && h->slice_table[mbb_xy] == h->slice_num
  4382. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  4383. mbb_xy -= s->mb_stride;
  4384. }else
  4385. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  4386. }else{
  4387. int mb_xy = h->mb_xy;
  4388. mba_xy = mb_xy - 1;
  4389. mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  4390. }
  4391. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4392. ctx++;
  4393. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4394. ctx++;
  4395. if( h->slice_type_nos == FF_B_TYPE )
  4396. ctx += 13;
  4397. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  4398. }
  4399. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4400. int mode = 0;
  4401. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4402. return pred_mode;
  4403. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4404. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4405. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4406. if( mode >= pred_mode )
  4407. return mode + 1;
  4408. else
  4409. return mode;
  4410. }
  4411. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4412. const int mba_xy = h->left_mb_xy[0];
  4413. const int mbb_xy = h->top_mb_xy;
  4414. int ctx = 0;
  4415. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4416. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4417. ctx++;
  4418. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4419. ctx++;
  4420. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4421. return 0;
  4422. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4423. return 1;
  4424. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4425. return 2;
  4426. else
  4427. return 3;
  4428. }
  4429. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4430. int cbp_b, cbp_a, ctx, cbp = 0;
  4431. cbp_a = h->slice_table[h->left_mb_xy[0]] == h->slice_num ? h->left_cbp : -1;
  4432. cbp_b = h->slice_table[h->top_mb_xy] == h->slice_num ? h->top_cbp : -1;
  4433. ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04);
  4434. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]);
  4435. ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08);
  4436. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1;
  4437. ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01);
  4438. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2;
  4439. ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02);
  4440. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3;
  4441. return cbp;
  4442. }
  4443. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4444. int ctx;
  4445. int cbp_a, cbp_b;
  4446. cbp_a = (h->left_cbp>>4)&0x03;
  4447. cbp_b = (h-> top_cbp>>4)&0x03;
  4448. ctx = 0;
  4449. if( cbp_a > 0 ) ctx++;
  4450. if( cbp_b > 0 ) ctx += 2;
  4451. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4452. return 0;
  4453. ctx = 4;
  4454. if( cbp_a == 2 ) ctx++;
  4455. if( cbp_b == 2 ) ctx += 2;
  4456. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  4457. }
  4458. static int decode_cabac_mb_dqp( H264Context *h) {
  4459. int ctx = 0;
  4460. int val = 0;
  4461. if( h->last_qscale_diff != 0 )
  4462. ctx++;
  4463. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4464. if( ctx < 2 )
  4465. ctx = 2;
  4466. else
  4467. ctx = 3;
  4468. val++;
  4469. if(val > 102) //prevent infinite loop
  4470. return INT_MIN;
  4471. }
  4472. if( val&0x01 )
  4473. return (val + 1)/2;
  4474. else
  4475. return -(val + 1)/2;
  4476. }
  4477. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4478. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4479. return 0; /* 8x8 */
  4480. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4481. return 1; /* 8x4 */
  4482. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4483. return 2; /* 4x8 */
  4484. return 3; /* 4x4 */
  4485. }
  4486. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4487. int type;
  4488. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4489. return 0; /* B_Direct_8x8 */
  4490. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4491. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4492. type = 3;
  4493. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4494. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4495. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4496. type += 4;
  4497. }
  4498. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4499. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4500. return type;
  4501. }
  4502. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4503. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4504. }
  4505. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4506. int refa = h->ref_cache[list][scan8[n] - 1];
  4507. int refb = h->ref_cache[list][scan8[n] - 8];
  4508. int ref = 0;
  4509. int ctx = 0;
  4510. if( h->slice_type_nos == FF_B_TYPE) {
  4511. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4512. ctx++;
  4513. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4514. ctx += 2;
  4515. } else {
  4516. if( refa > 0 )
  4517. ctx++;
  4518. if( refb > 0 )
  4519. ctx += 2;
  4520. }
  4521. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4522. ref++;
  4523. if( ctx < 4 )
  4524. ctx = 4;
  4525. else
  4526. ctx = 5;
  4527. if(ref >= 32 /*h->ref_list[list]*/){
  4528. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
  4529. return 0; //FIXME we should return -1 and check the return everywhere
  4530. }
  4531. }
  4532. return ref;
  4533. }
  4534. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4535. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4536. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4537. int ctxbase = (l == 0) ? 40 : 47;
  4538. int ctx, mvd;
  4539. if( amvd < 3 )
  4540. ctx = 0;
  4541. else if( amvd > 32 )
  4542. ctx = 2;
  4543. else
  4544. ctx = 1;
  4545. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4546. return 0;
  4547. mvd= 1;
  4548. ctx= 3;
  4549. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4550. mvd++;
  4551. if( ctx < 6 )
  4552. ctx++;
  4553. }
  4554. if( mvd >= 9 ) {
  4555. int k = 3;
  4556. while( get_cabac_bypass( &h->cabac ) ) {
  4557. mvd += 1 << k;
  4558. k++;
  4559. if(k>24){
  4560. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  4561. return INT_MIN;
  4562. }
  4563. }
  4564. while( k-- ) {
  4565. if( get_cabac_bypass( &h->cabac ) )
  4566. mvd += 1 << k;
  4567. }
  4568. }
  4569. return get_cabac_bypass_sign( &h->cabac, -mvd );
  4570. }
  4571. static av_always_inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx, int is_dc ) {
  4572. int nza, nzb;
  4573. int ctx = 0;
  4574. if( is_dc ) {
  4575. if( cat == 0 ) {
  4576. nza = h->left_cbp&0x100;
  4577. nzb = h-> top_cbp&0x100;
  4578. } else {
  4579. nza = (h->left_cbp>>(6+idx))&0x01;
  4580. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4581. }
  4582. } else {
  4583. if( cat == 4 ) {
  4584. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  4585. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  4586. } else {
  4587. assert(cat == 1 || cat == 2);
  4588. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4589. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4590. }
  4591. }
  4592. if( nza > 0 )
  4593. ctx++;
  4594. if( nzb > 0 )
  4595. ctx += 2;
  4596. return ctx + 4 * cat;
  4597. }
  4598. DECLARE_ASM_CONST(1, uint8_t, last_coeff_flag_offset_8x8[63]) = {
  4599. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  4600. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  4601. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  4602. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  4603. };
  4604. static av_always_inline void decode_cabac_residual_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff, int is_dc ) {
  4605. static const int significant_coeff_flag_offset[2][6] = {
  4606. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  4607. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  4608. };
  4609. static const int last_coeff_flag_offset[2][6] = {
  4610. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  4611. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  4612. };
  4613. static const int coeff_abs_level_m1_offset[6] = {
  4614. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  4615. };
  4616. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  4617. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  4618. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  4619. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  4620. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  4621. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  4622. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  4623. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  4624. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  4625. };
  4626. /* node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
  4627. * 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
  4628. * map node ctx => cabac ctx for level=1 */
  4629. static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
  4630. /* map node ctx => cabac ctx for level>1 */
  4631. static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
  4632. static const uint8_t coeff_abs_level_transition[2][8] = {
  4633. /* update node ctx after decoding a level=1 */
  4634. { 1, 2, 3, 3, 4, 5, 6, 7 },
  4635. /* update node ctx after decoding a level>1 */
  4636. { 4, 4, 4, 4, 5, 6, 7, 7 }
  4637. };
  4638. int index[64];
  4639. int av_unused last;
  4640. int coeff_count = 0;
  4641. int node_ctx = 0;
  4642. uint8_t *significant_coeff_ctx_base;
  4643. uint8_t *last_coeff_ctx_base;
  4644. uint8_t *abs_level_m1_ctx_base;
  4645. #ifndef ARCH_X86
  4646. #define CABAC_ON_STACK
  4647. #endif
  4648. #ifdef CABAC_ON_STACK
  4649. #define CC &cc
  4650. CABACContext cc;
  4651. cc.range = h->cabac.range;
  4652. cc.low = h->cabac.low;
  4653. cc.bytestream= h->cabac.bytestream;
  4654. #else
  4655. #define CC &h->cabac
  4656. #endif
  4657. /* cat: 0-> DC 16x16 n = 0
  4658. * 1-> AC 16x16 n = luma4x4idx
  4659. * 2-> Luma4x4 n = luma4x4idx
  4660. * 3-> DC Chroma n = iCbCr
  4661. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  4662. * 5-> Luma8x8 n = 4 * luma8x8idx
  4663. */
  4664. /* read coded block flag */
  4665. if( is_dc || cat != 5 ) {
  4666. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n, is_dc ) ] ) == 0 ) {
  4667. if( !is_dc ) {
  4668. if( cat == 4 )
  4669. h->non_zero_count_cache[scan8[16+n]] = 0;
  4670. else
  4671. h->non_zero_count_cache[scan8[n]] = 0;
  4672. }
  4673. #ifdef CABAC_ON_STACK
  4674. h->cabac.range = cc.range ;
  4675. h->cabac.low = cc.low ;
  4676. h->cabac.bytestream= cc.bytestream;
  4677. #endif
  4678. return;
  4679. }
  4680. }
  4681. significant_coeff_ctx_base = h->cabac_state
  4682. + significant_coeff_flag_offset[MB_FIELD][cat];
  4683. last_coeff_ctx_base = h->cabac_state
  4684. + last_coeff_flag_offset[MB_FIELD][cat];
  4685. abs_level_m1_ctx_base = h->cabac_state
  4686. + coeff_abs_level_m1_offset[cat];
  4687. if( !is_dc && cat == 5 ) {
  4688. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  4689. for(last= 0; last < coefs; last++) { \
  4690. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  4691. if( get_cabac( CC, sig_ctx )) { \
  4692. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  4693. index[coeff_count++] = last; \
  4694. if( get_cabac( CC, last_ctx ) ) { \
  4695. last= max_coeff; \
  4696. break; \
  4697. } \
  4698. } \
  4699. }\
  4700. if( last == max_coeff -1 ) {\
  4701. index[coeff_count++] = last;\
  4702. }
  4703. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  4704. #if defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
  4705. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  4706. } else {
  4707. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  4708. #else
  4709. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  4710. } else {
  4711. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  4712. #endif
  4713. }
  4714. assert(coeff_count > 0);
  4715. if( is_dc ) {
  4716. if( cat == 0 )
  4717. h->cbp_table[h->mb_xy] |= 0x100;
  4718. else
  4719. h->cbp_table[h->mb_xy] |= 0x40 << n;
  4720. } else {
  4721. if( cat == 5 )
  4722. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  4723. else if( cat == 4 )
  4724. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  4725. else {
  4726. assert( cat == 1 || cat == 2 );
  4727. h->non_zero_count_cache[scan8[n]] = coeff_count;
  4728. }
  4729. }
  4730. do {
  4731. uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base;
  4732. int j= scantable[index[--coeff_count]];
  4733. if( get_cabac( CC, ctx ) == 0 ) {
  4734. node_ctx = coeff_abs_level_transition[0][node_ctx];
  4735. if( is_dc ) {
  4736. block[j] = get_cabac_bypass_sign( CC, -1);
  4737. }else{
  4738. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;
  4739. }
  4740. } else {
  4741. int coeff_abs = 2;
  4742. ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base;
  4743. node_ctx = coeff_abs_level_transition[1][node_ctx];
  4744. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  4745. coeff_abs++;
  4746. }
  4747. if( coeff_abs >= 15 ) {
  4748. int j = 0;
  4749. while( get_cabac_bypass( CC ) ) {
  4750. j++;
  4751. }
  4752. coeff_abs=1;
  4753. while( j-- ) {
  4754. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  4755. }
  4756. coeff_abs+= 14;
  4757. }
  4758. if( is_dc ) {
  4759. block[j] = get_cabac_bypass_sign( CC, -coeff_abs );
  4760. }else{
  4761. block[j] = (get_cabac_bypass_sign( CC, -coeff_abs ) * qmul[j] + 32) >> 6;
  4762. }
  4763. }
  4764. } while( coeff_count );
  4765. #ifdef CABAC_ON_STACK
  4766. h->cabac.range = cc.range ;
  4767. h->cabac.low = cc.low ;
  4768. h->cabac.bytestream= cc.bytestream;
  4769. #endif
  4770. }
  4771. #ifndef CONFIG_SMALL
  4772. static void decode_cabac_residual_dc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  4773. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 1);
  4774. }
  4775. static void decode_cabac_residual_nondc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  4776. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0);
  4777. }
  4778. #endif
  4779. static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  4780. #ifdef CONFIG_SMALL
  4781. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, cat == 0 || cat == 3);
  4782. #else
  4783. if( cat == 0 || cat == 3 ) decode_cabac_residual_dc(h, block, cat, n, scantable, qmul, max_coeff);
  4784. else decode_cabac_residual_nondc(h, block, cat, n, scantable, qmul, max_coeff);
  4785. #endif
  4786. }
  4787. static inline void compute_mb_neighbors(H264Context *h)
  4788. {
  4789. MpegEncContext * const s = &h->s;
  4790. const int mb_xy = h->mb_xy;
  4791. h->top_mb_xy = mb_xy - s->mb_stride;
  4792. h->left_mb_xy[0] = mb_xy - 1;
  4793. if(FRAME_MBAFF){
  4794. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  4795. const int top_pair_xy = pair_xy - s->mb_stride;
  4796. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  4797. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  4798. const int curr_mb_frame_flag = !MB_FIELD;
  4799. const int bottom = (s->mb_y & 1);
  4800. if (bottom
  4801. ? !curr_mb_frame_flag // bottom macroblock
  4802. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  4803. ) {
  4804. h->top_mb_xy -= s->mb_stride;
  4805. }
  4806. if (left_mb_frame_flag != curr_mb_frame_flag) {
  4807. h->left_mb_xy[0] = pair_xy - 1;
  4808. }
  4809. } else if (FIELD_PICTURE) {
  4810. h->top_mb_xy -= s->mb_stride;
  4811. }
  4812. return;
  4813. }
  4814. /**
  4815. * decodes a macroblock
  4816. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4817. */
  4818. static int decode_mb_cabac(H264Context *h) {
  4819. MpegEncContext * const s = &h->s;
  4820. int mb_xy;
  4821. int mb_type, partition_count, cbp = 0;
  4822. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4823. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  4824. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  4825. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4826. if( h->slice_type_nos != FF_I_TYPE ) {
  4827. int skip;
  4828. /* a skipped mb needs the aff flag from the following mb */
  4829. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  4830. predict_field_decoding_flag(h);
  4831. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  4832. skip = h->next_mb_skipped;
  4833. else
  4834. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  4835. /* read skip flags */
  4836. if( skip ) {
  4837. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  4838. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  4839. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  4840. if(h->next_mb_skipped)
  4841. predict_field_decoding_flag(h);
  4842. else
  4843. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  4844. }
  4845. decode_mb_skip(h);
  4846. h->cbp_table[mb_xy] = 0;
  4847. h->chroma_pred_mode_table[mb_xy] = 0;
  4848. h->last_qscale_diff = 0;
  4849. return 0;
  4850. }
  4851. }
  4852. if(FRAME_MBAFF){
  4853. if( (s->mb_y&1) == 0 )
  4854. h->mb_mbaff =
  4855. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  4856. }else
  4857. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4858. h->prev_mb_skipped = 0;
  4859. compute_mb_neighbors(h);
  4860. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  4861. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  4862. return -1;
  4863. }
  4864. if( h->slice_type_nos == FF_B_TYPE ) {
  4865. if( mb_type < 23 ){
  4866. partition_count= b_mb_type_info[mb_type].partition_count;
  4867. mb_type= b_mb_type_info[mb_type].type;
  4868. }else{
  4869. mb_type -= 23;
  4870. goto decode_intra_mb;
  4871. }
  4872. } else if( h->slice_type_nos == FF_P_TYPE ) {
  4873. if( mb_type < 5) {
  4874. partition_count= p_mb_type_info[mb_type].partition_count;
  4875. mb_type= p_mb_type_info[mb_type].type;
  4876. } else {
  4877. mb_type -= 5;
  4878. goto decode_intra_mb;
  4879. }
  4880. } else {
  4881. if(h->slice_type == FF_SI_TYPE && mb_type)
  4882. mb_type--;
  4883. assert(h->slice_type_nos == FF_I_TYPE);
  4884. decode_intra_mb:
  4885. partition_count = 0;
  4886. cbp= i_mb_type_info[mb_type].cbp;
  4887. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4888. mb_type= i_mb_type_info[mb_type].type;
  4889. }
  4890. if(MB_FIELD)
  4891. mb_type |= MB_TYPE_INTERLACED;
  4892. h->slice_table[ mb_xy ]= h->slice_num;
  4893. if(IS_INTRA_PCM(mb_type)) {
  4894. const uint8_t *ptr;
  4895. unsigned int x, y;
  4896. // We assume these blocks are very rare so we do not optimize it.
  4897. // FIXME The two following lines get the bitstream position in the cabac
  4898. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  4899. ptr= h->cabac.bytestream;
  4900. if(h->cabac.low&0x1) ptr--;
  4901. if(CABAC_BITS==16){
  4902. if(h->cabac.low&0x1FF) ptr--;
  4903. }
  4904. // The pixels are stored in the same order as levels in h->mb array.
  4905. for(y=0; y<16; y++){
  4906. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4907. for(x=0; x<16; x++){
  4908. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", *ptr);
  4909. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  4910. }
  4911. }
  4912. if(CHROMA){
  4913. for(y=0; y<8; y++){
  4914. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4915. for(x=0; x<8; x++){
  4916. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  4917. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  4918. }
  4919. }
  4920. for(y=0; y<8; y++){
  4921. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4922. for(x=0; x<8; x++){
  4923. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  4924. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  4925. }
  4926. }
  4927. }
  4928. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  4929. // All blocks are present
  4930. h->cbp_table[mb_xy] = 0x1ef;
  4931. h->chroma_pred_mode_table[mb_xy] = 0;
  4932. // In deblocking, the quantizer is 0
  4933. s->current_picture.qscale_table[mb_xy]= 0;
  4934. // All coeffs are present
  4935. memset(h->non_zero_count[mb_xy], 16, 16);
  4936. s->current_picture.mb_type[mb_xy]= mb_type;
  4937. h->last_qscale_diff = 0;
  4938. return 0;
  4939. }
  4940. if(MB_MBAFF){
  4941. h->ref_count[0] <<= 1;
  4942. h->ref_count[1] <<= 1;
  4943. }
  4944. fill_caches(h, mb_type, 0);
  4945. if( IS_INTRA( mb_type ) ) {
  4946. int i, pred_mode;
  4947. if( IS_INTRA4x4( mb_type ) ) {
  4948. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  4949. mb_type |= MB_TYPE_8x8DCT;
  4950. for( i = 0; i < 16; i+=4 ) {
  4951. int pred = pred_intra_mode( h, i );
  4952. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4953. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4954. }
  4955. } else {
  4956. for( i = 0; i < 16; i++ ) {
  4957. int pred = pred_intra_mode( h, i );
  4958. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4959. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  4960. }
  4961. }
  4962. write_back_intra_pred_mode(h);
  4963. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  4964. } else {
  4965. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  4966. if( h->intra16x16_pred_mode < 0 ) return -1;
  4967. }
  4968. if(CHROMA){
  4969. h->chroma_pred_mode_table[mb_xy] =
  4970. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  4971. pred_mode= check_intra_pred_mode( h, pred_mode );
  4972. if( pred_mode < 0 ) return -1;
  4973. h->chroma_pred_mode= pred_mode;
  4974. }
  4975. } else if( partition_count == 4 ) {
  4976. int i, j, sub_partition_count[4], list, ref[2][4];
  4977. if( h->slice_type_nos == FF_B_TYPE ) {
  4978. for( i = 0; i < 4; i++ ) {
  4979. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  4980. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4981. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4982. }
  4983. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  4984. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  4985. pred_direct_motion(h, &mb_type);
  4986. h->ref_cache[0][scan8[4]] =
  4987. h->ref_cache[1][scan8[4]] =
  4988. h->ref_cache[0][scan8[12]] =
  4989. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4990. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  4991. for( i = 0; i < 4; i++ )
  4992. if( IS_DIRECT(h->sub_mb_type[i]) )
  4993. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  4994. }
  4995. }
  4996. } else {
  4997. for( i = 0; i < 4; i++ ) {
  4998. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  4999. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5000. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5001. }
  5002. }
  5003. for( list = 0; list < h->list_count; list++ ) {
  5004. for( i = 0; i < 4; i++ ) {
  5005. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5006. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5007. if( h->ref_count[list] > 1 )
  5008. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5009. else
  5010. ref[list][i] = 0;
  5011. } else {
  5012. ref[list][i] = -1;
  5013. }
  5014. h->ref_cache[list][ scan8[4*i]+1 ]=
  5015. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5016. }
  5017. }
  5018. if(dct8x8_allowed)
  5019. dct8x8_allowed = get_dct8x8_allowed(h);
  5020. for(list=0; list<h->list_count; list++){
  5021. for(i=0; i<4; i++){
  5022. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5023. if(IS_DIRECT(h->sub_mb_type[i])){
  5024. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5025. continue;
  5026. }
  5027. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5028. const int sub_mb_type= h->sub_mb_type[i];
  5029. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5030. for(j=0; j<sub_partition_count[i]; j++){
  5031. int mpx, mpy;
  5032. int mx, my;
  5033. const int index= 4*i + block_width*j;
  5034. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5035. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5036. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5037. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5038. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5039. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5040. if(IS_SUB_8X8(sub_mb_type)){
  5041. mv_cache[ 1 ][0]=
  5042. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5043. mv_cache[ 1 ][1]=
  5044. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5045. mvd_cache[ 1 ][0]=
  5046. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5047. mvd_cache[ 1 ][1]=
  5048. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5049. }else if(IS_SUB_8X4(sub_mb_type)){
  5050. mv_cache[ 1 ][0]= mx;
  5051. mv_cache[ 1 ][1]= my;
  5052. mvd_cache[ 1 ][0]= mx - mpx;
  5053. mvd_cache[ 1 ][1]= my - mpy;
  5054. }else if(IS_SUB_4X8(sub_mb_type)){
  5055. mv_cache[ 8 ][0]= mx;
  5056. mv_cache[ 8 ][1]= my;
  5057. mvd_cache[ 8 ][0]= mx - mpx;
  5058. mvd_cache[ 8 ][1]= my - mpy;
  5059. }
  5060. mv_cache[ 0 ][0]= mx;
  5061. mv_cache[ 0 ][1]= my;
  5062. mvd_cache[ 0 ][0]= mx - mpx;
  5063. mvd_cache[ 0 ][1]= my - mpy;
  5064. }
  5065. }else{
  5066. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5067. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5068. p[0] = p[1] = p[8] = p[9] = 0;
  5069. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5070. }
  5071. }
  5072. }
  5073. } else if( IS_DIRECT(mb_type) ) {
  5074. pred_direct_motion(h, &mb_type);
  5075. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5076. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5077. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5078. } else {
  5079. int list, mx, my, i, mpx, mpy;
  5080. if(IS_16X16(mb_type)){
  5081. for(list=0; list<h->list_count; list++){
  5082. if(IS_DIR(mb_type, 0, list)){
  5083. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5084. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5085. }else
  5086. 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
  5087. }
  5088. for(list=0; list<h->list_count; list++){
  5089. if(IS_DIR(mb_type, 0, list)){
  5090. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5091. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5092. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5093. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5094. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5095. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5096. }else
  5097. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5098. }
  5099. }
  5100. else if(IS_16X8(mb_type)){
  5101. for(list=0; list<h->list_count; list++){
  5102. for(i=0; i<2; i++){
  5103. if(IS_DIR(mb_type, i, list)){
  5104. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5105. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5106. }else
  5107. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5108. }
  5109. }
  5110. for(list=0; list<h->list_count; list++){
  5111. for(i=0; i<2; i++){
  5112. if(IS_DIR(mb_type, i, list)){
  5113. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5114. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5115. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5116. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5117. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5118. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5119. }else{
  5120. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5121. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5122. }
  5123. }
  5124. }
  5125. }else{
  5126. assert(IS_8X16(mb_type));
  5127. for(list=0; list<h->list_count; list++){
  5128. for(i=0; i<2; i++){
  5129. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5130. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5131. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5132. }else
  5133. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5134. }
  5135. }
  5136. for(list=0; list<h->list_count; list++){
  5137. for(i=0; i<2; i++){
  5138. if(IS_DIR(mb_type, i, list)){
  5139. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5140. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5141. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5142. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5143. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5144. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5145. }else{
  5146. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5147. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5148. }
  5149. }
  5150. }
  5151. }
  5152. }
  5153. if( IS_INTER( mb_type ) ) {
  5154. h->chroma_pred_mode_table[mb_xy] = 0;
  5155. write_back_motion( h, mb_type );
  5156. }
  5157. if( !IS_INTRA16x16( mb_type ) ) {
  5158. cbp = decode_cabac_mb_cbp_luma( h );
  5159. if(CHROMA)
  5160. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5161. }
  5162. h->cbp_table[mb_xy] = h->cbp = cbp;
  5163. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5164. if( decode_cabac_mb_transform_size( h ) )
  5165. mb_type |= MB_TYPE_8x8DCT;
  5166. }
  5167. s->current_picture.mb_type[mb_xy]= mb_type;
  5168. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5169. const uint8_t *scan, *scan8x8, *dc_scan;
  5170. const uint32_t *qmul;
  5171. int dqp;
  5172. if(IS_INTERLACED(mb_type)){
  5173. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5174. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5175. dc_scan= luma_dc_field_scan;
  5176. }else{
  5177. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5178. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5179. dc_scan= luma_dc_zigzag_scan;
  5180. }
  5181. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5182. if( dqp == INT_MIN ){
  5183. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5184. return -1;
  5185. }
  5186. s->qscale += dqp;
  5187. if(((unsigned)s->qscale) > 51){
  5188. if(s->qscale<0) s->qscale+= 52;
  5189. else s->qscale-= 52;
  5190. }
  5191. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  5192. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  5193. if( IS_INTRA16x16( mb_type ) ) {
  5194. int i;
  5195. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5196. decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16);
  5197. if( cbp&15 ) {
  5198. qmul = h->dequant4_coeff[0][s->qscale];
  5199. for( i = 0; i < 16; i++ ) {
  5200. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5201. decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15);
  5202. }
  5203. } else {
  5204. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5205. }
  5206. } else {
  5207. int i8x8, i4x4;
  5208. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5209. if( cbp & (1<<i8x8) ) {
  5210. if( IS_8x8DCT(mb_type) ) {
  5211. decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5212. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64);
  5213. } else {
  5214. qmul = h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale];
  5215. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5216. const int index = 4*i8x8 + i4x4;
  5217. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5218. //START_TIMER
  5219. decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, qmul, 16);
  5220. //STOP_TIMER("decode_residual")
  5221. }
  5222. }
  5223. } else {
  5224. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5225. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5226. }
  5227. }
  5228. }
  5229. if( cbp&0x30 ){
  5230. int c;
  5231. for( c = 0; c < 2; c++ ) {
  5232. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5233. decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4);
  5234. }
  5235. }
  5236. if( cbp&0x20 ) {
  5237. int c, i;
  5238. for( c = 0; c < 2; c++ ) {
  5239. qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];
  5240. for( i = 0; i < 4; i++ ) {
  5241. const int index = 16 + 4 * c + i;
  5242. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5243. decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, qmul, 15);
  5244. }
  5245. }
  5246. } else {
  5247. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5248. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5249. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5250. }
  5251. } else {
  5252. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5253. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5254. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5255. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5256. h->last_qscale_diff = 0;
  5257. }
  5258. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5259. write_back_non_zero_count(h);
  5260. if(MB_MBAFF){
  5261. h->ref_count[0] >>= 1;
  5262. h->ref_count[1] >>= 1;
  5263. }
  5264. return 0;
  5265. }
  5266. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5267. int i, d;
  5268. const int index_a = qp + h->slice_alpha_c0_offset;
  5269. const int alpha = (alpha_table+52)[index_a];
  5270. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5271. if( bS[0] < 4 ) {
  5272. int8_t tc[4];
  5273. for(i=0; i<4; i++)
  5274. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5275. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5276. } else {
  5277. /* 16px edge length, because bS=4 is triggered by being at
  5278. * the edge of an intra MB, so all 4 bS are the same */
  5279. for( d = 0; d < 16; d++ ) {
  5280. const int p0 = pix[-1];
  5281. const int p1 = pix[-2];
  5282. const int p2 = pix[-3];
  5283. const int q0 = pix[0];
  5284. const int q1 = pix[1];
  5285. const int q2 = pix[2];
  5286. if( FFABS( p0 - q0 ) < alpha &&
  5287. FFABS( p1 - p0 ) < beta &&
  5288. FFABS( q1 - q0 ) < beta ) {
  5289. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5290. if( FFABS( p2 - p0 ) < beta)
  5291. {
  5292. const int p3 = pix[-4];
  5293. /* p0', p1', p2' */
  5294. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5295. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5296. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5297. } else {
  5298. /* p0' */
  5299. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5300. }
  5301. if( FFABS( q2 - q0 ) < beta)
  5302. {
  5303. const int q3 = pix[3];
  5304. /* q0', q1', q2' */
  5305. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5306. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5307. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5308. } else {
  5309. /* q0' */
  5310. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5311. }
  5312. }else{
  5313. /* p0', q0' */
  5314. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5315. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5316. }
  5317. 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]);
  5318. }
  5319. pix += stride;
  5320. }
  5321. }
  5322. }
  5323. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5324. int i;
  5325. const int index_a = qp + h->slice_alpha_c0_offset;
  5326. const int alpha = (alpha_table+52)[index_a];
  5327. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5328. if( bS[0] < 4 ) {
  5329. int8_t tc[4];
  5330. for(i=0; i<4; i++)
  5331. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5332. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5333. } else {
  5334. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5335. }
  5336. }
  5337. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5338. int i;
  5339. for( i = 0; i < 16; i++, pix += stride) {
  5340. int index_a;
  5341. int alpha;
  5342. int beta;
  5343. int qp_index;
  5344. int bS_index = (i >> 1);
  5345. if (!MB_FIELD) {
  5346. bS_index &= ~1;
  5347. bS_index |= (i & 1);
  5348. }
  5349. if( bS[bS_index] == 0 ) {
  5350. continue;
  5351. }
  5352. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5353. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5354. alpha = (alpha_table+52)[index_a];
  5355. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5356. if( bS[bS_index] < 4 ) {
  5357. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  5358. const int p0 = pix[-1];
  5359. const int p1 = pix[-2];
  5360. const int p2 = pix[-3];
  5361. const int q0 = pix[0];
  5362. const int q1 = pix[1];
  5363. const int q2 = pix[2];
  5364. if( FFABS( p0 - q0 ) < alpha &&
  5365. FFABS( p1 - p0 ) < beta &&
  5366. FFABS( q1 - q0 ) < beta ) {
  5367. int tc = tc0;
  5368. int i_delta;
  5369. if( FFABS( p2 - p0 ) < beta ) {
  5370. pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5371. tc++;
  5372. }
  5373. if( FFABS( q2 - q0 ) < beta ) {
  5374. pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5375. tc++;
  5376. }
  5377. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5378. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5379. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5380. 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);
  5381. }
  5382. }else{
  5383. const int p0 = pix[-1];
  5384. const int p1 = pix[-2];
  5385. const int p2 = pix[-3];
  5386. const int q0 = pix[0];
  5387. const int q1 = pix[1];
  5388. const int q2 = pix[2];
  5389. if( FFABS( p0 - q0 ) < alpha &&
  5390. FFABS( p1 - p0 ) < beta &&
  5391. FFABS( q1 - q0 ) < beta ) {
  5392. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5393. if( FFABS( p2 - p0 ) < beta)
  5394. {
  5395. const int p3 = pix[-4];
  5396. /* p0', p1', p2' */
  5397. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5398. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5399. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5400. } else {
  5401. /* p0' */
  5402. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5403. }
  5404. if( FFABS( q2 - q0 ) < beta)
  5405. {
  5406. const int q3 = pix[3];
  5407. /* q0', q1', q2' */
  5408. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5409. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5410. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5411. } else {
  5412. /* q0' */
  5413. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5414. }
  5415. }else{
  5416. /* p0', q0' */
  5417. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5418. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5419. }
  5420. 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]);
  5421. }
  5422. }
  5423. }
  5424. }
  5425. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5426. int i;
  5427. for( i = 0; i < 8; i++, pix += stride) {
  5428. int index_a;
  5429. int alpha;
  5430. int beta;
  5431. int qp_index;
  5432. int bS_index = i;
  5433. if( bS[bS_index] == 0 ) {
  5434. continue;
  5435. }
  5436. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  5437. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5438. alpha = (alpha_table+52)[index_a];
  5439. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5440. if( bS[bS_index] < 4 ) {
  5441. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  5442. const int p0 = pix[-1];
  5443. const int p1 = pix[-2];
  5444. const int q0 = pix[0];
  5445. const int q1 = pix[1];
  5446. if( FFABS( p0 - q0 ) < alpha &&
  5447. FFABS( p1 - p0 ) < beta &&
  5448. FFABS( q1 - q0 ) < beta ) {
  5449. const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5450. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5451. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5452. 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);
  5453. }
  5454. }else{
  5455. const int p0 = pix[-1];
  5456. const int p1 = pix[-2];
  5457. const int q0 = pix[0];
  5458. const int q1 = pix[1];
  5459. if( FFABS( p0 - q0 ) < alpha &&
  5460. FFABS( p1 - p0 ) < beta &&
  5461. FFABS( q1 - q0 ) < beta ) {
  5462. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5463. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5464. 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]);
  5465. }
  5466. }
  5467. }
  5468. }
  5469. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5470. int i, d;
  5471. const int index_a = qp + h->slice_alpha_c0_offset;
  5472. const int alpha = (alpha_table+52)[index_a];
  5473. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5474. const int pix_next = stride;
  5475. if( bS[0] < 4 ) {
  5476. int8_t tc[4];
  5477. for(i=0; i<4; i++)
  5478. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5479. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5480. } else {
  5481. /* 16px edge length, see filter_mb_edgev */
  5482. for( d = 0; d < 16; d++ ) {
  5483. const int p0 = pix[-1*pix_next];
  5484. const int p1 = pix[-2*pix_next];
  5485. const int p2 = pix[-3*pix_next];
  5486. const int q0 = pix[0];
  5487. const int q1 = pix[1*pix_next];
  5488. const int q2 = pix[2*pix_next];
  5489. if( FFABS( p0 - q0 ) < alpha &&
  5490. FFABS( p1 - p0 ) < beta &&
  5491. FFABS( q1 - q0 ) < beta ) {
  5492. const int p3 = pix[-4*pix_next];
  5493. const int q3 = pix[ 3*pix_next];
  5494. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5495. if( FFABS( p2 - p0 ) < beta) {
  5496. /* p0', p1', p2' */
  5497. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5498. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5499. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5500. } else {
  5501. /* p0' */
  5502. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5503. }
  5504. if( FFABS( q2 - q0 ) < beta) {
  5505. /* q0', q1', q2' */
  5506. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5507. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5508. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5509. } else {
  5510. /* q0' */
  5511. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5512. }
  5513. }else{
  5514. /* p0', q0' */
  5515. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5516. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5517. }
  5518. 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]);
  5519. }
  5520. pix++;
  5521. }
  5522. }
  5523. }
  5524. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5525. int i;
  5526. const int index_a = qp + h->slice_alpha_c0_offset;
  5527. const int alpha = (alpha_table+52)[index_a];
  5528. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5529. if( bS[0] < 4 ) {
  5530. int8_t tc[4];
  5531. for(i=0; i<4; i++)
  5532. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5533. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5534. } else {
  5535. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5536. }
  5537. }
  5538. 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) {
  5539. MpegEncContext * const s = &h->s;
  5540. int mb_y_firstrow = s->picture_structure == PICT_BOTTOM_FIELD;
  5541. int mb_xy, mb_type;
  5542. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  5543. mb_xy = h->mb_xy;
  5544. if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
  5545. 1 ||
  5546. (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] ||
  5547. h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
  5548. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  5549. return;
  5550. }
  5551. assert(!FRAME_MBAFF);
  5552. mb_type = s->current_picture.mb_type[mb_xy];
  5553. qp = s->current_picture.qscale_table[mb_xy];
  5554. qp0 = s->current_picture.qscale_table[mb_xy-1];
  5555. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  5556. qpc = get_chroma_qp( h, 0, qp );
  5557. qpc0 = get_chroma_qp( h, 0, qp0 );
  5558. qpc1 = get_chroma_qp( h, 0, qp1 );
  5559. qp0 = (qp + qp0 + 1) >> 1;
  5560. qp1 = (qp + qp1 + 1) >> 1;
  5561. qpc0 = (qpc + qpc0 + 1) >> 1;
  5562. qpc1 = (qpc + qpc1 + 1) >> 1;
  5563. qp_thresh = 15 - h->slice_alpha_c0_offset;
  5564. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  5565. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  5566. return;
  5567. if( IS_INTRA(mb_type) ) {
  5568. int16_t bS4[4] = {4,4,4,4};
  5569. int16_t bS3[4] = {3,3,3,3};
  5570. int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
  5571. if( IS_8x8DCT(mb_type) ) {
  5572. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5573. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5574. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
  5575. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5576. } else {
  5577. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5578. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  5579. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5580. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  5581. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
  5582. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  5583. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5584. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  5585. }
  5586. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  5587. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  5588. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  5589. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  5590. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
  5591. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5592. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
  5593. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5594. return;
  5595. } else {
  5596. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  5597. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  5598. int edges;
  5599. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  5600. edges = 4;
  5601. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  5602. } else {
  5603. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  5604. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  5605. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5606. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5607. ? 3 : 0;
  5608. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  5609. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  5610. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  5611. (h->slice_type_nos == FF_B_TYPE), edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);
  5612. }
  5613. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  5614. bSv[0][0] = 0x0004000400040004ULL;
  5615. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  5616. bSv[1][0] = FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL;
  5617. #define FILTER(hv,dir,edge)\
  5618. if(bSv[dir][edge]) {\
  5619. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  5620. if(!(edge&1)) {\
  5621. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5622. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5623. }\
  5624. }
  5625. if( edges == 1 ) {
  5626. FILTER(v,0,0);
  5627. FILTER(h,1,0);
  5628. } else if( IS_8x8DCT(mb_type) ) {
  5629. FILTER(v,0,0);
  5630. FILTER(v,0,2);
  5631. FILTER(h,1,0);
  5632. FILTER(h,1,2);
  5633. } else {
  5634. FILTER(v,0,0);
  5635. FILTER(v,0,1);
  5636. FILTER(v,0,2);
  5637. FILTER(v,0,3);
  5638. FILTER(h,1,0);
  5639. FILTER(h,1,1);
  5640. FILTER(h,1,2);
  5641. FILTER(h,1,3);
  5642. }
  5643. #undef FILTER
  5644. }
  5645. }
  5646. 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) {
  5647. MpegEncContext * const s = &h->s;
  5648. const int mb_xy= mb_x + mb_y*s->mb_stride;
  5649. const int mb_type = s->current_picture.mb_type[mb_xy];
  5650. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  5651. int first_vertical_edge_done = 0;
  5652. int dir;
  5653. //for sufficiently low qp, filtering wouldn't do anything
  5654. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  5655. if(!FRAME_MBAFF){
  5656. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
  5657. int qp = s->current_picture.qscale_table[mb_xy];
  5658. if(qp <= qp_thresh
  5659. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  5660. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  5661. return;
  5662. }
  5663. }
  5664. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  5665. if(!h->pps.cabac && h->pps.transform_8x8_mode){
  5666. int top_type, left_type[2];
  5667. top_type = s->current_picture.mb_type[h->top_mb_xy] ;
  5668. left_type[0] = s->current_picture.mb_type[h->left_mb_xy[0]];
  5669. left_type[1] = s->current_picture.mb_type[h->left_mb_xy[1]];
  5670. if(IS_8x8DCT(top_type)){
  5671. h->non_zero_count_cache[4+8*0]=
  5672. h->non_zero_count_cache[5+8*0]= h->cbp_table[h->top_mb_xy] & 4;
  5673. h->non_zero_count_cache[6+8*0]=
  5674. h->non_zero_count_cache[7+8*0]= h->cbp_table[h->top_mb_xy] & 8;
  5675. }
  5676. if(IS_8x8DCT(left_type[0])){
  5677. h->non_zero_count_cache[3+8*1]=
  5678. h->non_zero_count_cache[3+8*2]= h->cbp_table[h->left_mb_xy[0]]&2; //FIXME check MBAFF
  5679. }
  5680. if(IS_8x8DCT(left_type[1])){
  5681. h->non_zero_count_cache[3+8*3]=
  5682. h->non_zero_count_cache[3+8*4]= h->cbp_table[h->left_mb_xy[1]]&8; //FIXME check MBAFF
  5683. }
  5684. if(IS_8x8DCT(mb_type)){
  5685. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  5686. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp_table[mb_xy] & 1;
  5687. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  5688. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp_table[mb_xy] & 2;
  5689. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  5690. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp_table[mb_xy] & 4;
  5691. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  5692. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp_table[mb_xy] & 8;
  5693. }
  5694. }
  5695. if (FRAME_MBAFF
  5696. // left mb is in picture
  5697. && h->slice_table[mb_xy-1] != 255
  5698. // and current and left pair do not have the same interlaced type
  5699. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  5700. // and left mb is in the same slice if deblocking_filter == 2
  5701. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  5702. /* First vertical edge is different in MBAFF frames
  5703. * There are 8 different bS to compute and 2 different Qp
  5704. */
  5705. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5706. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  5707. int16_t bS[8];
  5708. int qp[2];
  5709. int bqp[2];
  5710. int rqp[2];
  5711. int mb_qp, mbn0_qp, mbn1_qp;
  5712. int i;
  5713. first_vertical_edge_done = 1;
  5714. if( IS_INTRA(mb_type) )
  5715. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  5716. else {
  5717. for( i = 0; i < 8; i++ ) {
  5718. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  5719. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  5720. bS[i] = 4;
  5721. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  5722. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  5723. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  5724. bS[i] = 2;
  5725. else
  5726. bS[i] = 1;
  5727. }
  5728. }
  5729. mb_qp = s->current_picture.qscale_table[mb_xy];
  5730. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  5731. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  5732. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  5733. bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
  5734. get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
  5735. rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
  5736. get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
  5737. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  5738. bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
  5739. get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
  5740. rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
  5741. get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
  5742. /* Filter edge */
  5743. 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);
  5744. { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  5745. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  5746. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, bqp );
  5747. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, rqp );
  5748. }
  5749. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  5750. for( dir = 0; dir < 2; dir++ )
  5751. {
  5752. int edge;
  5753. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  5754. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  5755. int (*ref2frm) [48+2] = h->ref2frm[ h->slice_num &15 ];
  5756. int (*ref2frmm)[48+2] = h->ref2frm[ h->slice_table[mbm_xy]&15 ];
  5757. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  5758. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  5759. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  5760. // how often to recheck mv-based bS when iterating between edges
  5761. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  5762. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  5763. // how often to recheck mv-based bS when iterating along each edge
  5764. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  5765. if (first_vertical_edge_done) {
  5766. start = 1;
  5767. first_vertical_edge_done = 0;
  5768. }
  5769. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  5770. start = 1;
  5771. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  5772. && !IS_INTERLACED(mb_type)
  5773. && IS_INTERLACED(mbm_type)
  5774. ) {
  5775. // This is a special case in the norm where the filtering must
  5776. // be done twice (one each of the field) even if we are in a
  5777. // frame macroblock.
  5778. //
  5779. static const int nnz_idx[4] = {4,5,6,3};
  5780. unsigned int tmp_linesize = 2 * linesize;
  5781. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  5782. int mbn_xy = mb_xy - 2 * s->mb_stride;
  5783. int qp;
  5784. int i, j;
  5785. int16_t bS[4];
  5786. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  5787. if( IS_INTRA(mb_type) ||
  5788. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  5789. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5790. } else {
  5791. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  5792. for( i = 0; i < 4; i++ ) {
  5793. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  5794. mbn_nnz[nnz_idx[i]] != 0 )
  5795. bS[i] = 2;
  5796. else
  5797. bS[i] = 1;
  5798. }
  5799. }
  5800. // Do not use s->qscale as luma quantizer because it has not the same
  5801. // value in IPCM macroblocks.
  5802. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5803. 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);
  5804. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  5805. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  5806. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
  5807. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5808. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
  5809. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5810. }
  5811. start = 1;
  5812. }
  5813. /* Calculate bS */
  5814. for( edge = start; edge < edges; edge++ ) {
  5815. /* mbn_xy: neighbor macroblock */
  5816. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  5817. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  5818. int (*ref2frmn)[48+2] = edge > 0 ? ref2frm : ref2frmm;
  5819. int16_t bS[4];
  5820. int qp;
  5821. if( (edge&1) && IS_8x8DCT(mb_type) )
  5822. continue;
  5823. if( IS_INTRA(mb_type) ||
  5824. IS_INTRA(mbn_type) ) {
  5825. int value;
  5826. if (edge == 0) {
  5827. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  5828. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  5829. ) {
  5830. value = 4;
  5831. } else {
  5832. value = 3;
  5833. }
  5834. } else {
  5835. value = 3;
  5836. }
  5837. bS[0] = bS[1] = bS[2] = bS[3] = value;
  5838. } else {
  5839. int i, l;
  5840. int mv_done;
  5841. if( edge & mask_edge ) {
  5842. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  5843. mv_done = 1;
  5844. }
  5845. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  5846. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  5847. mv_done = 1;
  5848. }
  5849. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  5850. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  5851. int bn_idx= b_idx - (dir ? 8:1);
  5852. int v = 0;
  5853. for( l = 0; !v && l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
  5854. v |= ref2frm[l][h->ref_cache[l][b_idx]+2] != ref2frmn[l][h->ref_cache[l][bn_idx]+2] ||
  5855. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5856. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  5857. }
  5858. if(h->slice_type_nos == FF_B_TYPE && v){
  5859. v=0;
  5860. for( l = 0; !v && l < 2; l++ ) {
  5861. int ln= 1-l;
  5862. v |= ref2frm[l][h->ref_cache[l][b_idx]+2] != ref2frmn[ln][h->ref_cache[ln][bn_idx]+2] ||
  5863. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] ) >= 4 ||
  5864. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit;
  5865. }
  5866. }
  5867. bS[0] = bS[1] = bS[2] = bS[3] = v;
  5868. mv_done = 1;
  5869. }
  5870. else
  5871. mv_done = 0;
  5872. for( i = 0; i < 4; i++ ) {
  5873. int x = dir == 0 ? edge : i;
  5874. int y = dir == 0 ? i : edge;
  5875. int b_idx= 8 + 4 + x + 8*y;
  5876. int bn_idx= b_idx - (dir ? 8:1);
  5877. if( h->non_zero_count_cache[b_idx] != 0 ||
  5878. h->non_zero_count_cache[bn_idx] != 0 ) {
  5879. bS[i] = 2;
  5880. }
  5881. else if(!mv_done)
  5882. {
  5883. bS[i] = 0;
  5884. for( l = 0; l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
  5885. if( ref2frm[l][h->ref_cache[l][b_idx]+2] != ref2frmn[l][h->ref_cache[l][bn_idx]+2] ||
  5886. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5887. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  5888. bS[i] = 1;
  5889. break;
  5890. }
  5891. }
  5892. if(h->slice_type_nos == FF_B_TYPE && bS[i]){
  5893. bS[i] = 0;
  5894. for( l = 0; l < 2; l++ ) {
  5895. int ln= 1-l;
  5896. if( ref2frm[l][h->ref_cache[l][b_idx]+2] != ref2frmn[ln][h->ref_cache[ln][bn_idx]+2] ||
  5897. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] ) >= 4 ||
  5898. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit ) {
  5899. bS[i] = 1;
  5900. break;
  5901. }
  5902. }
  5903. }
  5904. }
  5905. }
  5906. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  5907. continue;
  5908. }
  5909. /* Filter edge */
  5910. // Do not use s->qscale as luma quantizer because it has not the same
  5911. // value in IPCM macroblocks.
  5912. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5913. //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]);
  5914. 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);
  5915. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  5916. if( dir == 0 ) {
  5917. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  5918. if( (edge&1) == 0 ) {
  5919. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS,
  5920. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5921. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS,
  5922. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5923. }
  5924. } else {
  5925. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  5926. if( (edge&1) == 0 ) {
  5927. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS,
  5928. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5929. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS,
  5930. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5931. }
  5932. }
  5933. }
  5934. }
  5935. }
  5936. static int decode_slice(struct AVCodecContext *avctx, H264Context *h){
  5937. MpegEncContext * const s = &h->s;
  5938. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  5939. s->mb_skip_run= -1;
  5940. if( h->pps.cabac ) {
  5941. int i;
  5942. /* realign */
  5943. align_get_bits( &s->gb );
  5944. /* init cabac */
  5945. ff_init_cabac_states( &h->cabac);
  5946. ff_init_cabac_decoder( &h->cabac,
  5947. s->gb.buffer + get_bits_count(&s->gb)/8,
  5948. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  5949. /* calculate pre-state */
  5950. for( i= 0; i < 460; i++ ) {
  5951. int pre;
  5952. if( h->slice_type_nos == FF_I_TYPE )
  5953. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  5954. else
  5955. 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 );
  5956. if( pre <= 63 )
  5957. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  5958. else
  5959. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  5960. }
  5961. for(;;){
  5962. //START_TIMER
  5963. int ret = decode_mb_cabac(h);
  5964. int eos;
  5965. //STOP_TIMER("decode_mb_cabac")
  5966. if(ret>=0) hl_decode_mb(h);
  5967. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  5968. s->mb_y++;
  5969. if(ret>=0) ret = decode_mb_cabac(h);
  5970. if(ret>=0) hl_decode_mb(h);
  5971. s->mb_y--;
  5972. }
  5973. eos = get_cabac_terminate( &h->cabac );
  5974. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  5975. 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);
  5976. 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);
  5977. return -1;
  5978. }
  5979. if( ++s->mb_x >= s->mb_width ) {
  5980. s->mb_x = 0;
  5981. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5982. ++s->mb_y;
  5983. if(FIELD_OR_MBAFF_PICTURE) {
  5984. ++s->mb_y;
  5985. }
  5986. }
  5987. if( eos || s->mb_y >= s->mb_height ) {
  5988. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5989. 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);
  5990. return 0;
  5991. }
  5992. }
  5993. } else {
  5994. for(;;){
  5995. int ret = decode_mb_cavlc(h);
  5996. if(ret>=0) hl_decode_mb(h);
  5997. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  5998. s->mb_y++;
  5999. ret = decode_mb_cavlc(h);
  6000. if(ret>=0) hl_decode_mb(h);
  6001. s->mb_y--;
  6002. }
  6003. if(ret<0){
  6004. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6005. 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);
  6006. return -1;
  6007. }
  6008. if(++s->mb_x >= s->mb_width){
  6009. s->mb_x=0;
  6010. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6011. ++s->mb_y;
  6012. if(FIELD_OR_MBAFF_PICTURE) {
  6013. ++s->mb_y;
  6014. }
  6015. if(s->mb_y >= s->mb_height){
  6016. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6017. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6018. 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);
  6019. return 0;
  6020. }else{
  6021. 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);
  6022. return -1;
  6023. }
  6024. }
  6025. }
  6026. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6027. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6028. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6029. 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);
  6030. return 0;
  6031. }else{
  6032. 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);
  6033. return -1;
  6034. }
  6035. }
  6036. }
  6037. }
  6038. #if 0
  6039. for(;s->mb_y < s->mb_height; s->mb_y++){
  6040. for(;s->mb_x < s->mb_width; s->mb_x++){
  6041. int ret= decode_mb(h);
  6042. hl_decode_mb(h);
  6043. if(ret<0){
  6044. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6045. 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);
  6046. return -1;
  6047. }
  6048. if(++s->mb_x >= s->mb_width){
  6049. s->mb_x=0;
  6050. if(++s->mb_y >= s->mb_height){
  6051. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6052. 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);
  6053. return 0;
  6054. }else{
  6055. 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);
  6056. return -1;
  6057. }
  6058. }
  6059. }
  6060. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6061. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6062. 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);
  6063. return 0;
  6064. }else{
  6065. 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);
  6066. return -1;
  6067. }
  6068. }
  6069. }
  6070. s->mb_x=0;
  6071. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6072. }
  6073. #endif
  6074. return -1; //not reached
  6075. }
  6076. static int decode_unregistered_user_data(H264Context *h, int size){
  6077. MpegEncContext * const s = &h->s;
  6078. uint8_t user_data[16+256];
  6079. int e, build, i;
  6080. if(size<16)
  6081. return -1;
  6082. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6083. user_data[i]= get_bits(&s->gb, 8);
  6084. }
  6085. user_data[i]= 0;
  6086. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6087. if(e==1 && build>=0)
  6088. h->x264_build= build;
  6089. if(s->avctx->debug & FF_DEBUG_BUGS)
  6090. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6091. for(; i<size; i++)
  6092. skip_bits(&s->gb, 8);
  6093. return 0;
  6094. }
  6095. static int decode_sei(H264Context *h){
  6096. MpegEncContext * const s = &h->s;
  6097. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6098. int size, type;
  6099. type=0;
  6100. do{
  6101. type+= show_bits(&s->gb, 8);
  6102. }while(get_bits(&s->gb, 8) == 255);
  6103. size=0;
  6104. do{
  6105. size+= show_bits(&s->gb, 8);
  6106. }while(get_bits(&s->gb, 8) == 255);
  6107. switch(type){
  6108. case 5:
  6109. if(decode_unregistered_user_data(h, size) < 0)
  6110. return -1;
  6111. break;
  6112. default:
  6113. skip_bits(&s->gb, 8*size);
  6114. }
  6115. //FIXME check bits here
  6116. align_get_bits(&s->gb);
  6117. }
  6118. return 0;
  6119. }
  6120. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6121. MpegEncContext * const s = &h->s;
  6122. int cpb_count, i;
  6123. cpb_count = get_ue_golomb(&s->gb) + 1;
  6124. get_bits(&s->gb, 4); /* bit_rate_scale */
  6125. get_bits(&s->gb, 4); /* cpb_size_scale */
  6126. for(i=0; i<cpb_count; i++){
  6127. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6128. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6129. get_bits1(&s->gb); /* cbr_flag */
  6130. }
  6131. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6132. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6133. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6134. get_bits(&s->gb, 5); /* time_offset_length */
  6135. }
  6136. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6137. MpegEncContext * const s = &h->s;
  6138. int aspect_ratio_info_present_flag;
  6139. unsigned int aspect_ratio_idc;
  6140. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6141. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6142. if( aspect_ratio_info_present_flag ) {
  6143. aspect_ratio_idc= get_bits(&s->gb, 8);
  6144. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6145. sps->sar.num= get_bits(&s->gb, 16);
  6146. sps->sar.den= get_bits(&s->gb, 16);
  6147. }else if(aspect_ratio_idc < sizeof(pixel_aspect)/sizeof(*pixel_aspect)){
  6148. sps->sar= pixel_aspect[aspect_ratio_idc];
  6149. }else{
  6150. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6151. return -1;
  6152. }
  6153. }else{
  6154. sps->sar.num=
  6155. sps->sar.den= 0;
  6156. }
  6157. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6158. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6159. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6160. }
  6161. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6162. get_bits(&s->gb, 3); /* video_format */
  6163. get_bits1(&s->gb); /* video_full_range_flag */
  6164. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6165. get_bits(&s->gb, 8); /* colour_primaries */
  6166. get_bits(&s->gb, 8); /* transfer_characteristics */
  6167. get_bits(&s->gb, 8); /* matrix_coefficients */
  6168. }
  6169. }
  6170. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6171. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6172. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6173. }
  6174. sps->timing_info_present_flag = get_bits1(&s->gb);
  6175. if(sps->timing_info_present_flag){
  6176. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6177. sps->time_scale = get_bits_long(&s->gb, 32);
  6178. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6179. }
  6180. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6181. if(nal_hrd_parameters_present_flag)
  6182. decode_hrd_parameters(h, sps);
  6183. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6184. if(vcl_hrd_parameters_present_flag)
  6185. decode_hrd_parameters(h, sps);
  6186. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6187. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6188. get_bits1(&s->gb); /* pic_struct_present_flag */
  6189. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6190. if(sps->bitstream_restriction_flag){
  6191. unsigned int num_reorder_frames;
  6192. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6193. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6194. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6195. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6196. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6197. num_reorder_frames= get_ue_golomb(&s->gb);
  6198. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6199. if(num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6200. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
  6201. return -1;
  6202. }
  6203. sps->num_reorder_frames= num_reorder_frames;
  6204. }
  6205. return 0;
  6206. }
  6207. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6208. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6209. MpegEncContext * const s = &h->s;
  6210. int i, last = 8, next = 8;
  6211. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6212. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6213. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6214. else
  6215. for(i=0;i<size;i++){
  6216. if(next)
  6217. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6218. if(!i && !next){ /* matrix not written, we use the preset one */
  6219. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6220. break;
  6221. }
  6222. last = factors[scan[i]] = next ? next : last;
  6223. }
  6224. }
  6225. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6226. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6227. MpegEncContext * const s = &h->s;
  6228. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6229. const uint8_t *fallback[4] = {
  6230. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6231. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6232. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6233. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6234. };
  6235. if(get_bits1(&s->gb)){
  6236. sps->scaling_matrix_present |= is_sps;
  6237. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6238. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6239. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6240. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6241. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6242. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6243. if(is_sps || pps->transform_8x8_mode){
  6244. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6245. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6246. }
  6247. } else if(fallback_sps) {
  6248. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6249. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6250. }
  6251. }
  6252. /**
  6253. * Returns and optionally allocates SPS / PPS structures in the supplied array 'vec'
  6254. */
  6255. static void *
  6256. alloc_parameter_set(H264Context *h, void **vec, const unsigned int id, const unsigned int max,
  6257. const size_t size, const char *name)
  6258. {
  6259. if(id>=max) {
  6260. av_log(h->s.avctx, AV_LOG_ERROR, "%s_id (%d) out of range\n", name, id);
  6261. return NULL;
  6262. }
  6263. if(!vec[id]) {
  6264. vec[id] = av_mallocz(size);
  6265. if(vec[id] == NULL)
  6266. av_log(h->s.avctx, AV_LOG_ERROR, "cannot allocate memory for %s\n", name);
  6267. }
  6268. return vec[id];
  6269. }
  6270. static inline int decode_seq_parameter_set(H264Context *h){
  6271. MpegEncContext * const s = &h->s;
  6272. int profile_idc, level_idc;
  6273. unsigned int sps_id, tmp, mb_width, mb_height;
  6274. int i;
  6275. SPS *sps;
  6276. profile_idc= get_bits(&s->gb, 8);
  6277. get_bits1(&s->gb); //constraint_set0_flag
  6278. get_bits1(&s->gb); //constraint_set1_flag
  6279. get_bits1(&s->gb); //constraint_set2_flag
  6280. get_bits1(&s->gb); //constraint_set3_flag
  6281. get_bits(&s->gb, 4); // reserved
  6282. level_idc= get_bits(&s->gb, 8);
  6283. sps_id= get_ue_golomb(&s->gb);
  6284. sps = alloc_parameter_set(h, (void **)h->sps_buffers, sps_id, MAX_SPS_COUNT, sizeof(SPS), "sps");
  6285. if(sps == NULL)
  6286. return -1;
  6287. sps->profile_idc= profile_idc;
  6288. sps->level_idc= level_idc;
  6289. if(sps->profile_idc >= 100){ //high profile
  6290. sps->chroma_format_idc= get_ue_golomb(&s->gb);
  6291. if(sps->chroma_format_idc == 3)
  6292. get_bits1(&s->gb); //residual_color_transform_flag
  6293. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6294. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6295. sps->transform_bypass = get_bits1(&s->gb);
  6296. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6297. }else{
  6298. sps->scaling_matrix_present = 0;
  6299. sps->chroma_format_idc= 1;
  6300. }
  6301. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6302. sps->poc_type= get_ue_golomb(&s->gb);
  6303. if(sps->poc_type == 0){ //FIXME #define
  6304. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6305. } else if(sps->poc_type == 1){//FIXME #define
  6306. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6307. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6308. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6309. tmp= get_ue_golomb(&s->gb);
  6310. if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
  6311. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
  6312. return -1;
  6313. }
  6314. sps->poc_cycle_length= tmp;
  6315. for(i=0; i<sps->poc_cycle_length; i++)
  6316. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6317. }else if(sps->poc_type != 2){
  6318. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6319. return -1;
  6320. }
  6321. tmp= get_ue_golomb(&s->gb);
  6322. if(tmp > MAX_PICTURE_COUNT-2 || tmp >= 32){
  6323. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6324. return -1;
  6325. }
  6326. sps->ref_frame_count= tmp;
  6327. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6328. mb_width= get_ue_golomb(&s->gb) + 1;
  6329. mb_height= get_ue_golomb(&s->gb) + 1;
  6330. if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
  6331. avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
  6332. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6333. return -1;
  6334. }
  6335. sps->mb_width = mb_width;
  6336. sps->mb_height= mb_height;
  6337. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6338. if(!sps->frame_mbs_only_flag)
  6339. sps->mb_aff= get_bits1(&s->gb);
  6340. else
  6341. sps->mb_aff= 0;
  6342. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6343. #ifndef ALLOW_INTERLACE
  6344. if(sps->mb_aff)
  6345. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6346. #endif
  6347. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6348. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6349. sps->crop= get_bits1(&s->gb);
  6350. if(sps->crop){
  6351. sps->crop_left = get_ue_golomb(&s->gb);
  6352. sps->crop_right = get_ue_golomb(&s->gb);
  6353. sps->crop_top = get_ue_golomb(&s->gb);
  6354. sps->crop_bottom= get_ue_golomb(&s->gb);
  6355. if(sps->crop_left || sps->crop_top){
  6356. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6357. }
  6358. if(sps->crop_right >= 8 || sps->crop_bottom >= (8>> !h->sps.frame_mbs_only_flag)){
  6359. av_log(h->s.avctx, AV_LOG_ERROR, "brainfart cropping not supported, this could look slightly wrong ...\n");
  6360. }
  6361. }else{
  6362. sps->crop_left =
  6363. sps->crop_right =
  6364. sps->crop_top =
  6365. sps->crop_bottom= 0;
  6366. }
  6367. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6368. if( sps->vui_parameters_present_flag )
  6369. decode_vui_parameters(h, sps);
  6370. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6371. 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 %s\n",
  6372. sps_id, sps->profile_idc, sps->level_idc,
  6373. sps->poc_type,
  6374. sps->ref_frame_count,
  6375. sps->mb_width, sps->mb_height,
  6376. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6377. sps->direct_8x8_inference_flag ? "8B8" : "",
  6378. sps->crop_left, sps->crop_right,
  6379. sps->crop_top, sps->crop_bottom,
  6380. sps->vui_parameters_present_flag ? "VUI" : "",
  6381. ((const char*[]){"Gray","420","422","444"})[sps->chroma_format_idc]
  6382. );
  6383. }
  6384. return 0;
  6385. }
  6386. static void
  6387. build_qp_table(PPS *pps, int t, int index)
  6388. {
  6389. int i;
  6390. for(i = 0; i < 52; i++)
  6391. pps->chroma_qp_table[t][i] = chroma_qp[av_clip(i + index, 0, 51)];
  6392. }
  6393. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6394. MpegEncContext * const s = &h->s;
  6395. unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
  6396. PPS *pps;
  6397. pps = alloc_parameter_set(h, (void **)h->pps_buffers, pps_id, MAX_PPS_COUNT, sizeof(PPS), "pps");
  6398. if(pps == NULL)
  6399. return -1;
  6400. tmp= get_ue_golomb(&s->gb);
  6401. if(tmp>=MAX_SPS_COUNT || h->sps_buffers[tmp] == NULL){
  6402. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6403. return -1;
  6404. }
  6405. pps->sps_id= tmp;
  6406. pps->cabac= get_bits1(&s->gb);
  6407. pps->pic_order_present= get_bits1(&s->gb);
  6408. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6409. if(pps->slice_group_count > 1 ){
  6410. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6411. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6412. switch(pps->mb_slice_group_map_type){
  6413. case 0:
  6414. #if 0
  6415. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6416. | run_length[ i ] |1 |ue(v) |
  6417. #endif
  6418. break;
  6419. case 2:
  6420. #if 0
  6421. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6422. |{ | | |
  6423. | top_left_mb[ i ] |1 |ue(v) |
  6424. | bottom_right_mb[ i ] |1 |ue(v) |
  6425. | } | | |
  6426. #endif
  6427. break;
  6428. case 3:
  6429. case 4:
  6430. case 5:
  6431. #if 0
  6432. | slice_group_change_direction_flag |1 |u(1) |
  6433. | slice_group_change_rate_minus1 |1 |ue(v) |
  6434. #endif
  6435. break;
  6436. case 6:
  6437. #if 0
  6438. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6439. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6440. |) | | |
  6441. | slice_group_id[ i ] |1 |u(v) |
  6442. #endif
  6443. break;
  6444. }
  6445. }
  6446. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6447. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6448. if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
  6449. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6450. pps->ref_count[0]= pps->ref_count[1]= 1;
  6451. return -1;
  6452. }
  6453. pps->weighted_pred= get_bits1(&s->gb);
  6454. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6455. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6456. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6457. pps->chroma_qp_index_offset[0]= get_se_golomb(&s->gb);
  6458. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6459. pps->constrained_intra_pred= get_bits1(&s->gb);
  6460. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6461. pps->transform_8x8_mode= 0;
  6462. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6463. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6464. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6465. if(get_bits_count(&s->gb) < bit_length){
  6466. pps->transform_8x8_mode= get_bits1(&s->gb);
  6467. decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6468. pps->chroma_qp_index_offset[1]= get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6469. } else {
  6470. pps->chroma_qp_index_offset[1]= pps->chroma_qp_index_offset[0];
  6471. }
  6472. build_qp_table(pps, 0, pps->chroma_qp_index_offset[0]);
  6473. build_qp_table(pps, 1, pps->chroma_qp_index_offset[1]);
  6474. if(pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
  6475. h->pps.chroma_qp_diff= 1;
  6476. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6477. 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",
  6478. pps_id, pps->sps_id,
  6479. pps->cabac ? "CABAC" : "CAVLC",
  6480. pps->slice_group_count,
  6481. pps->ref_count[0], pps->ref_count[1],
  6482. pps->weighted_pred ? "weighted" : "",
  6483. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
  6484. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6485. pps->constrained_intra_pred ? "CONSTR" : "",
  6486. pps->redundant_pic_cnt_present ? "REDU" : "",
  6487. pps->transform_8x8_mode ? "8x8DCT" : ""
  6488. );
  6489. }
  6490. return 0;
  6491. }
  6492. /**
  6493. * Call decode_slice() for each context.
  6494. *
  6495. * @param h h264 master context
  6496. * @param context_count number of contexts to execute
  6497. */
  6498. static void execute_decode_slices(H264Context *h, int context_count){
  6499. MpegEncContext * const s = &h->s;
  6500. AVCodecContext * const avctx= s->avctx;
  6501. H264Context *hx;
  6502. int i;
  6503. if(context_count == 1) {
  6504. decode_slice(avctx, h);
  6505. } else {
  6506. for(i = 1; i < context_count; i++) {
  6507. hx = h->thread_context[i];
  6508. hx->s.error_resilience = avctx->error_resilience;
  6509. hx->s.error_count = 0;
  6510. }
  6511. avctx->execute(avctx, (void *)decode_slice,
  6512. (void **)h->thread_context, NULL, context_count);
  6513. /* pull back stuff from slices to master context */
  6514. hx = h->thread_context[context_count - 1];
  6515. s->mb_x = hx->s.mb_x;
  6516. s->mb_y = hx->s.mb_y;
  6517. s->dropable = hx->s.dropable;
  6518. s->picture_structure = hx->s.picture_structure;
  6519. for(i = 1; i < context_count; i++)
  6520. h->s.error_count += h->thread_context[i]->s.error_count;
  6521. }
  6522. }
  6523. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  6524. MpegEncContext * const s = &h->s;
  6525. AVCodecContext * const avctx= s->avctx;
  6526. int buf_index=0;
  6527. H264Context *hx; ///< thread context
  6528. int context_count = 0;
  6529. h->max_contexts = avctx->thread_count;
  6530. #if 0
  6531. int i;
  6532. for(i=0; i<50; i++){
  6533. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  6534. }
  6535. #endif
  6536. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  6537. h->current_slice = 0;
  6538. if (!s->first_field)
  6539. s->current_picture_ptr= NULL;
  6540. }
  6541. for(;;){
  6542. int consumed;
  6543. int dst_length;
  6544. int bit_length;
  6545. const uint8_t *ptr;
  6546. int i, nalsize = 0;
  6547. int err;
  6548. if(h->is_avc) {
  6549. if(buf_index >= buf_size) break;
  6550. nalsize = 0;
  6551. for(i = 0; i < h->nal_length_size; i++)
  6552. nalsize = (nalsize << 8) | buf[buf_index++];
  6553. if(nalsize <= 1 || (nalsize+buf_index > buf_size)){
  6554. if(nalsize == 1){
  6555. buf_index++;
  6556. continue;
  6557. }else{
  6558. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  6559. break;
  6560. }
  6561. }
  6562. } else {
  6563. // start code prefix search
  6564. for(; buf_index + 3 < buf_size; buf_index++){
  6565. // This should always succeed in the first iteration.
  6566. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  6567. break;
  6568. }
  6569. if(buf_index+3 >= buf_size) break;
  6570. buf_index+=3;
  6571. }
  6572. hx = h->thread_context[context_count];
  6573. ptr= decode_nal(hx, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  6574. if (ptr==NULL || dst_length < 0){
  6575. return -1;
  6576. }
  6577. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  6578. dst_length--;
  6579. bit_length= !dst_length ? 0 : (8*dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1));
  6580. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  6581. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
  6582. }
  6583. if (h->is_avc && (nalsize != consumed)){
  6584. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  6585. consumed= nalsize;
  6586. }
  6587. buf_index += consumed;
  6588. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
  6589. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  6590. continue;
  6591. again:
  6592. err = 0;
  6593. switch(hx->nal_unit_type){
  6594. case NAL_IDR_SLICE:
  6595. if (h->nal_unit_type != NAL_IDR_SLICE) {
  6596. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  6597. return -1;
  6598. }
  6599. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  6600. case NAL_SLICE:
  6601. init_get_bits(&hx->s.gb, ptr, bit_length);
  6602. hx->intra_gb_ptr=
  6603. hx->inter_gb_ptr= &hx->s.gb;
  6604. hx->s.data_partitioning = 0;
  6605. if((err = decode_slice_header(hx, h)))
  6606. break;
  6607. s->current_picture_ptr->key_frame|= (hx->nal_unit_type == NAL_IDR_SLICE);
  6608. if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
  6609. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  6610. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  6611. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  6612. && avctx->skip_frame < AVDISCARD_ALL)
  6613. context_count++;
  6614. break;
  6615. case NAL_DPA:
  6616. init_get_bits(&hx->s.gb, ptr, bit_length);
  6617. hx->intra_gb_ptr=
  6618. hx->inter_gb_ptr= NULL;
  6619. hx->s.data_partitioning = 1;
  6620. err = decode_slice_header(hx, h);
  6621. break;
  6622. case NAL_DPB:
  6623. init_get_bits(&hx->intra_gb, ptr, bit_length);
  6624. hx->intra_gb_ptr= &hx->intra_gb;
  6625. break;
  6626. case NAL_DPC:
  6627. init_get_bits(&hx->inter_gb, ptr, bit_length);
  6628. hx->inter_gb_ptr= &hx->inter_gb;
  6629. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  6630. && s->context_initialized
  6631. && s->hurry_up < 5
  6632. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  6633. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  6634. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  6635. && avctx->skip_frame < AVDISCARD_ALL)
  6636. context_count++;
  6637. break;
  6638. case NAL_SEI:
  6639. init_get_bits(&s->gb, ptr, bit_length);
  6640. decode_sei(h);
  6641. break;
  6642. case NAL_SPS:
  6643. init_get_bits(&s->gb, ptr, bit_length);
  6644. decode_seq_parameter_set(h);
  6645. if(s->flags& CODEC_FLAG_LOW_DELAY)
  6646. s->low_delay=1;
  6647. if(avctx->has_b_frames < 2)
  6648. avctx->has_b_frames= !s->low_delay;
  6649. break;
  6650. case NAL_PPS:
  6651. init_get_bits(&s->gb, ptr, bit_length);
  6652. decode_picture_parameter_set(h, bit_length);
  6653. break;
  6654. case NAL_AUD:
  6655. case NAL_END_SEQUENCE:
  6656. case NAL_END_STREAM:
  6657. case NAL_FILLER_DATA:
  6658. case NAL_SPS_EXT:
  6659. case NAL_AUXILIARY_SLICE:
  6660. break;
  6661. default:
  6662. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", h->nal_unit_type, bit_length);
  6663. }
  6664. if(context_count == h->max_contexts) {
  6665. execute_decode_slices(h, context_count);
  6666. context_count = 0;
  6667. }
  6668. if (err < 0)
  6669. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6670. else if(err == 1) {
  6671. /* Slice could not be decoded in parallel mode, copy down
  6672. * NAL unit stuff to context 0 and restart. Note that
  6673. * rbsp_buffer is not transferred, but since we no longer
  6674. * run in parallel mode this should not be an issue. */
  6675. h->nal_unit_type = hx->nal_unit_type;
  6676. h->nal_ref_idc = hx->nal_ref_idc;
  6677. hx = h;
  6678. goto again;
  6679. }
  6680. }
  6681. if(context_count)
  6682. execute_decode_slices(h, context_count);
  6683. return buf_index;
  6684. }
  6685. /**
  6686. * returns the number of bytes consumed for building the current frame
  6687. */
  6688. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  6689. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  6690. if(pos+10>buf_size) pos=buf_size; // oops ;)
  6691. return pos;
  6692. }
  6693. static int decode_frame(AVCodecContext *avctx,
  6694. void *data, int *data_size,
  6695. const uint8_t *buf, int buf_size)
  6696. {
  6697. H264Context *h = avctx->priv_data;
  6698. MpegEncContext *s = &h->s;
  6699. AVFrame *pict = data;
  6700. int buf_index;
  6701. s->flags= avctx->flags;
  6702. s->flags2= avctx->flags2;
  6703. /* end of stream, output what is still in the buffers */
  6704. if (buf_size == 0) {
  6705. Picture *out;
  6706. int i, out_idx;
  6707. //FIXME factorize this with the output code below
  6708. out = h->delayed_pic[0];
  6709. out_idx = 0;
  6710. for(i=1; h->delayed_pic[i] && h->delayed_pic[i]->poc; i++)
  6711. if(h->delayed_pic[i]->poc < out->poc){
  6712. out = h->delayed_pic[i];
  6713. out_idx = i;
  6714. }
  6715. for(i=out_idx; h->delayed_pic[i]; i++)
  6716. h->delayed_pic[i] = h->delayed_pic[i+1];
  6717. if(out){
  6718. *data_size = sizeof(AVFrame);
  6719. *pict= *(AVFrame*)out;
  6720. }
  6721. return 0;
  6722. }
  6723. if(h->is_avc && !h->got_avcC) {
  6724. int i, cnt, nalsize;
  6725. unsigned char *p = avctx->extradata;
  6726. if(avctx->extradata_size < 7) {
  6727. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  6728. return -1;
  6729. }
  6730. if(*p != 1) {
  6731. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  6732. return -1;
  6733. }
  6734. /* sps and pps in the avcC always have length coded with 2 bytes,
  6735. so put a fake nal_length_size = 2 while parsing them */
  6736. h->nal_length_size = 2;
  6737. // Decode sps from avcC
  6738. cnt = *(p+5) & 0x1f; // Number of sps
  6739. p += 6;
  6740. for (i = 0; i < cnt; i++) {
  6741. nalsize = AV_RB16(p) + 2;
  6742. if(decode_nal_units(h, p, nalsize) < 0) {
  6743. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  6744. return -1;
  6745. }
  6746. p += nalsize;
  6747. }
  6748. // Decode pps from avcC
  6749. cnt = *(p++); // Number of pps
  6750. for (i = 0; i < cnt; i++) {
  6751. nalsize = AV_RB16(p) + 2;
  6752. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6753. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  6754. return -1;
  6755. }
  6756. p += nalsize;
  6757. }
  6758. // Now store right nal length size, that will be use to parse all other nals
  6759. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  6760. // Do not reparse avcC
  6761. h->got_avcC = 1;
  6762. }
  6763. if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
  6764. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  6765. return -1;
  6766. }
  6767. buf_index=decode_nal_units(h, buf, buf_size);
  6768. if(buf_index < 0)
  6769. return -1;
  6770. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  6771. if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
  6772. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  6773. return -1;
  6774. }
  6775. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  6776. Picture *out = s->current_picture_ptr;
  6777. Picture *cur = s->current_picture_ptr;
  6778. int i, pics, cross_idr, out_of_order, out_idx;
  6779. s->mb_y= 0;
  6780. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  6781. s->current_picture_ptr->pict_type= s->pict_type;
  6782. if(!s->dropable) {
  6783. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  6784. h->prev_poc_msb= h->poc_msb;
  6785. h->prev_poc_lsb= h->poc_lsb;
  6786. }
  6787. h->prev_frame_num_offset= h->frame_num_offset;
  6788. h->prev_frame_num= h->frame_num;
  6789. /*
  6790. * FIXME: Error handling code does not seem to support interlaced
  6791. * when slices span multiple rows
  6792. * The ff_er_add_slice calls don't work right for bottom
  6793. * fields; they cause massive erroneous error concealing
  6794. * Error marking covers both fields (top and bottom).
  6795. * This causes a mismatched s->error_count
  6796. * and a bad error table. Further, the error count goes to
  6797. * INT_MAX when called for bottom field, because mb_y is
  6798. * past end by one (callers fault) and resync_mb_y != 0
  6799. * causes problems for the first MB line, too.
  6800. */
  6801. if (!FIELD_PICTURE)
  6802. ff_er_frame_end(s);
  6803. MPV_frame_end(s);
  6804. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  6805. /* Wait for second field. */
  6806. *data_size = 0;
  6807. } else {
  6808. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  6809. /* Derive top_field_first from field pocs. */
  6810. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  6811. //FIXME do something with unavailable reference frames
  6812. /* Sort B-frames into display order */
  6813. if(h->sps.bitstream_restriction_flag
  6814. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  6815. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  6816. s->low_delay = 0;
  6817. }
  6818. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  6819. && !h->sps.bitstream_restriction_flag){
  6820. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  6821. s->low_delay= 0;
  6822. }
  6823. pics = 0;
  6824. while(h->delayed_pic[pics]) pics++;
  6825. assert(pics <= MAX_DELAYED_PIC_COUNT);
  6826. h->delayed_pic[pics++] = cur;
  6827. if(cur->reference == 0)
  6828. cur->reference = DELAYED_PIC_REF;
  6829. out = h->delayed_pic[0];
  6830. out_idx = 0;
  6831. for(i=1; h->delayed_pic[i] && h->delayed_pic[i]->poc; i++)
  6832. if(h->delayed_pic[i]->poc < out->poc){
  6833. out = h->delayed_pic[i];
  6834. out_idx = i;
  6835. }
  6836. cross_idr = !h->delayed_pic[0]->poc || !!h->delayed_pic[i];
  6837. out_of_order = !cross_idr && out->poc < h->outputed_poc;
  6838. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  6839. { }
  6840. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  6841. || (s->low_delay &&
  6842. ((!cross_idr && out->poc > h->outputed_poc + 2)
  6843. || cur->pict_type == FF_B_TYPE)))
  6844. {
  6845. s->low_delay = 0;
  6846. s->avctx->has_b_frames++;
  6847. }
  6848. if(out_of_order || pics > s->avctx->has_b_frames){
  6849. out->reference &= ~DELAYED_PIC_REF;
  6850. for(i=out_idx; h->delayed_pic[i]; i++)
  6851. h->delayed_pic[i] = h->delayed_pic[i+1];
  6852. }
  6853. if(!out_of_order && pics > s->avctx->has_b_frames){
  6854. *data_size = sizeof(AVFrame);
  6855. h->outputed_poc = out->poc;
  6856. *pict= *(AVFrame*)out;
  6857. }else{
  6858. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  6859. }
  6860. }
  6861. }
  6862. assert(pict->data[0] || !*data_size);
  6863. ff_print_debug_info(s, pict);
  6864. //printf("out %d\n", (int)pict->data[0]);
  6865. #if 0 //?
  6866. /* Return the Picture timestamp as the frame number */
  6867. /* we subtract 1 because it is added on utils.c */
  6868. avctx->frame_number = s->picture_number - 1;
  6869. #endif
  6870. return get_consumed_bytes(s, buf_index, buf_size);
  6871. }
  6872. #if 0
  6873. static inline void fill_mb_avail(H264Context *h){
  6874. MpegEncContext * const s = &h->s;
  6875. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  6876. if(s->mb_y){
  6877. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  6878. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  6879. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  6880. }else{
  6881. h->mb_avail[0]=
  6882. h->mb_avail[1]=
  6883. h->mb_avail[2]= 0;
  6884. }
  6885. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  6886. h->mb_avail[4]= 1; //FIXME move out
  6887. h->mb_avail[5]= 0; //FIXME move out
  6888. }
  6889. #endif
  6890. #ifdef TEST
  6891. #undef printf
  6892. #undef random
  6893. #define COUNT 8000
  6894. #define SIZE (COUNT*40)
  6895. int main(void){
  6896. int i;
  6897. uint8_t temp[SIZE];
  6898. PutBitContext pb;
  6899. GetBitContext gb;
  6900. // int int_temp[10000];
  6901. DSPContext dsp;
  6902. AVCodecContext avctx;
  6903. dsputil_init(&dsp, &avctx);
  6904. init_put_bits(&pb, temp, SIZE);
  6905. printf("testing unsigned exp golomb\n");
  6906. for(i=0; i<COUNT; i++){
  6907. START_TIMER
  6908. set_ue_golomb(&pb, i);
  6909. STOP_TIMER("set_ue_golomb");
  6910. }
  6911. flush_put_bits(&pb);
  6912. init_get_bits(&gb, temp, 8*SIZE);
  6913. for(i=0; i<COUNT; i++){
  6914. int j, s;
  6915. s= show_bits(&gb, 24);
  6916. START_TIMER
  6917. j= get_ue_golomb(&gb);
  6918. if(j != i){
  6919. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  6920. // return -1;
  6921. }
  6922. STOP_TIMER("get_ue_golomb");
  6923. }
  6924. init_put_bits(&pb, temp, SIZE);
  6925. printf("testing signed exp golomb\n");
  6926. for(i=0; i<COUNT; i++){
  6927. START_TIMER
  6928. set_se_golomb(&pb, i - COUNT/2);
  6929. STOP_TIMER("set_se_golomb");
  6930. }
  6931. flush_put_bits(&pb);
  6932. init_get_bits(&gb, temp, 8*SIZE);
  6933. for(i=0; i<COUNT; i++){
  6934. int j, s;
  6935. s= show_bits(&gb, 24);
  6936. START_TIMER
  6937. j= get_se_golomb(&gb);
  6938. if(j != i - COUNT/2){
  6939. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  6940. // return -1;
  6941. }
  6942. STOP_TIMER("get_se_golomb");
  6943. }
  6944. #if 0
  6945. printf("testing 4x4 (I)DCT\n");
  6946. DCTELEM block[16];
  6947. uint8_t src[16], ref[16];
  6948. uint64_t error= 0, max_error=0;
  6949. for(i=0; i<COUNT; i++){
  6950. int j;
  6951. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  6952. for(j=0; j<16; j++){
  6953. ref[j]= random()%255;
  6954. src[j]= random()%255;
  6955. }
  6956. h264_diff_dct_c(block, src, ref, 4);
  6957. //normalize
  6958. for(j=0; j<16; j++){
  6959. // printf("%d ", block[j]);
  6960. block[j]= block[j]*4;
  6961. if(j&1) block[j]= (block[j]*4 + 2)/5;
  6962. if(j&4) block[j]= (block[j]*4 + 2)/5;
  6963. }
  6964. // printf("\n");
  6965. s->dsp.h264_idct_add(ref, block, 4);
  6966. /* for(j=0; j<16; j++){
  6967. printf("%d ", ref[j]);
  6968. }
  6969. printf("\n");*/
  6970. for(j=0; j<16; j++){
  6971. int diff= FFABS(src[j] - ref[j]);
  6972. error+= diff*diff;
  6973. max_error= FFMAX(max_error, diff);
  6974. }
  6975. }
  6976. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  6977. printf("testing quantizer\n");
  6978. for(qp=0; qp<52; qp++){
  6979. for(i=0; i<16; i++)
  6980. src1_block[i]= src2_block[i]= random()%255;
  6981. }
  6982. printf("Testing NAL layer\n");
  6983. uint8_t bitstream[COUNT];
  6984. uint8_t nal[COUNT*2];
  6985. H264Context h;
  6986. memset(&h, 0, sizeof(H264Context));
  6987. for(i=0; i<COUNT; i++){
  6988. int zeros= i;
  6989. int nal_length;
  6990. int consumed;
  6991. int out_length;
  6992. uint8_t *out;
  6993. int j;
  6994. for(j=0; j<COUNT; j++){
  6995. bitstream[j]= (random() % 255) + 1;
  6996. }
  6997. for(j=0; j<zeros; j++){
  6998. int pos= random() % COUNT;
  6999. while(bitstream[pos] == 0){
  7000. pos++;
  7001. pos %= COUNT;
  7002. }
  7003. bitstream[pos]=0;
  7004. }
  7005. START_TIMER
  7006. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7007. if(nal_length<0){
  7008. printf("encoding failed\n");
  7009. return -1;
  7010. }
  7011. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7012. STOP_TIMER("NAL")
  7013. if(out_length != COUNT){
  7014. printf("incorrect length %d %d\n", out_length, COUNT);
  7015. return -1;
  7016. }
  7017. if(consumed != nal_length){
  7018. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7019. return -1;
  7020. }
  7021. if(memcmp(bitstream, out, COUNT)){
  7022. printf("mismatch\n");
  7023. return -1;
  7024. }
  7025. }
  7026. #endif
  7027. printf("Testing RBSP\n");
  7028. return 0;
  7029. }
  7030. #endif /* TEST */
  7031. static av_cold int decode_end(AVCodecContext *avctx)
  7032. {
  7033. H264Context *h = avctx->priv_data;
  7034. MpegEncContext *s = &h->s;
  7035. av_freep(&h->rbsp_buffer[0]);
  7036. av_freep(&h->rbsp_buffer[1]);
  7037. free_tables(h); //FIXME cleanup init stuff perhaps
  7038. MPV_common_end(s);
  7039. // memset(h, 0, sizeof(H264Context));
  7040. return 0;
  7041. }
  7042. AVCodec h264_decoder = {
  7043. "h264",
  7044. CODEC_TYPE_VIDEO,
  7045. CODEC_ID_H264,
  7046. sizeof(H264Context),
  7047. decode_init,
  7048. NULL,
  7049. decode_end,
  7050. decode_frame,
  7051. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  7052. .flush= flush_dpb,
  7053. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  7054. };
  7055. #include "svq3.c"