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.

7964 lines
308KB

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