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.

8017 lines
309KB

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