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.

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