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.

6353 lines
239KB

  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 libavcodec/h264.c
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h264.h"
  31. #include "h264data.h"
  32. #include "h264_parser.h"
  33. #include "golomb.h"
  34. #include "mathops.h"
  35. #include "rectangle.h"
  36. #include "vdpau_internal.h"
  37. #include "cabac.h"
  38. #if ARCH_X86
  39. #include "x86/h264_i386.h"
  40. #endif
  41. //#undef NDEBUG
  42. #include <assert.h>
  43. /**
  44. * Value of Picture.reference when Picture is not a reference picture, but
  45. * is held for delayed output.
  46. */
  47. #define DELAYED_PIC_REF 4
  48. static VLC coeff_token_vlc[4];
  49. static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2];
  50. static const int coeff_token_vlc_tables_size[4]={520,332,280,256};
  51. static VLC chroma_dc_coeff_token_vlc;
  52. static VLC_TYPE chroma_dc_coeff_token_vlc_table[256][2];
  53. static const int chroma_dc_coeff_token_vlc_table_size = 256;
  54. static VLC total_zeros_vlc[15];
  55. static VLC_TYPE total_zeros_vlc_tables[15][512][2];
  56. static const int total_zeros_vlc_tables_size = 512;
  57. static VLC chroma_dc_total_zeros_vlc[3];
  58. static VLC_TYPE chroma_dc_total_zeros_vlc_tables[3][8][2];
  59. static const int chroma_dc_total_zeros_vlc_tables_size = 8;
  60. static VLC run_vlc[6];
  61. static VLC_TYPE run_vlc_tables[6][8][2];
  62. static const int run_vlc_tables_size = 8;
  63. static VLC run7_vlc;
  64. static VLC_TYPE run7_vlc_table[96][2];
  65. static const int run7_vlc_table_size = 96;
  66. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  67. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  68. static Picture * remove_long(H264Context *h, int i, int ref_mask);
  69. static const uint8_t rem6[52]={
  70. 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,
  71. };
  72. static const uint8_t div6[52]={
  73. 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,
  74. };
  75. static const uint8_t left_block_options[4][8]={
  76. {0,1,2,3,7,10,8,11},
  77. {2,2,3,3,8,11,8,11},
  78. {0,0,1,1,7,10,7,10},
  79. {0,2,0,2,7,10,7,10}
  80. };
  81. #define LEVEL_TAB_BITS 8
  82. static int8_t cavlc_level_tab[7][1<<LEVEL_TAB_BITS][2];
  83. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  84. MpegEncContext * const s = &h->s;
  85. const int mb_xy= h->mb_xy;
  86. int topleft_xy, top_xy, topright_xy, left_xy[2];
  87. int topleft_type, top_type, topright_type, left_type[2];
  88. const uint8_t * left_block;
  89. int topleft_partition= -1;
  90. int i;
  91. top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  92. //FIXME deblocking could skip the intra and nnz parts.
  93. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)
  94. return;
  95. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  96. * stuff, I can't imagine that these complex rules are worth it. */
  97. topleft_xy = top_xy - 1;
  98. topright_xy= top_xy + 1;
  99. left_xy[1] = left_xy[0] = mb_xy-1;
  100. left_block = left_block_options[0];
  101. if(FRAME_MBAFF){
  102. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  103. const int top_pair_xy = pair_xy - s->mb_stride;
  104. const int topleft_pair_xy = top_pair_xy - 1;
  105. const int topright_pair_xy = top_pair_xy + 1;
  106. const int topleft_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  107. const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  108. const int topright_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  109. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  110. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  111. const int bottom = (s->mb_y & 1);
  112. tprintf(s->avctx, "fill_caches: curr_mb_field_flag:%d, left_mb_field_flag:%d, topleft_mb_field_flag:%d, top_mb_field_flag:%d, topright_mb_field_flag:%d\n", curr_mb_field_flag, left_mb_field_flag, topleft_mb_field_flag, top_mb_field_flag, topright_mb_field_flag);
  113. if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
  114. top_xy -= s->mb_stride;
  115. }
  116. if (curr_mb_field_flag && (bottom || topleft_mb_field_flag)){
  117. topleft_xy -= s->mb_stride;
  118. } else if(bottom && !curr_mb_field_flag && left_mb_field_flag) {
  119. topleft_xy += s->mb_stride;
  120. // take top left mv from the middle of the mb, as opposed to all other modes which use the bottom right partition
  121. topleft_partition = 0;
  122. }
  123. if (curr_mb_field_flag && (bottom || topright_mb_field_flag)){
  124. topright_xy -= s->mb_stride;
  125. }
  126. if (left_mb_field_flag != curr_mb_field_flag) {
  127. left_xy[1] = left_xy[0] = pair_xy - 1;
  128. if (curr_mb_field_flag) {
  129. left_xy[1] += s->mb_stride;
  130. left_block = left_block_options[3];
  131. } else {
  132. left_block= left_block_options[2 - bottom];
  133. }
  134. }
  135. }
  136. h->top_mb_xy = top_xy;
  137. h->left_mb_xy[0] = left_xy[0];
  138. h->left_mb_xy[1] = left_xy[1];
  139. if(for_deblock){
  140. topleft_type = 0;
  141. topright_type = 0;
  142. top_type = h->slice_table[top_xy ] < 0xFFFF ? s->current_picture.mb_type[top_xy] : 0;
  143. left_type[0] = h->slice_table[left_xy[0] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[0]] : 0;
  144. left_type[1] = h->slice_table[left_xy[1] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[1]] : 0;
  145. if(MB_MBAFF && !IS_INTRA(mb_type)){
  146. int list;
  147. for(list=0; list<h->list_count; list++){
  148. //These values where changed for ease of performing MC, we need to change them back
  149. //FIXME maybe we can make MC and loop filter use the same values or prevent
  150. //the MC code from changing ref_cache and rather use a temporary array.
  151. if(USES_LIST(mb_type,list)){
  152. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  153. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  154. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
  155. ref += h->b8_stride;
  156. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  157. *(uint32_t*)&h->ref_cache[list][scan8[10]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
  158. }
  159. }
  160. }
  161. }else{
  162. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  163. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  164. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  165. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  166. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  167. if(IS_INTRA(mb_type)){
  168. int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
  169. h->topleft_samples_available=
  170. h->top_samples_available=
  171. h->left_samples_available= 0xFFFF;
  172. h->topright_samples_available= 0xEEEA;
  173. if(!(top_type & type_mask)){
  174. h->topleft_samples_available= 0xB3FF;
  175. h->top_samples_available= 0x33FF;
  176. h->topright_samples_available= 0x26EA;
  177. }
  178. if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){
  179. if(IS_INTERLACED(mb_type)){
  180. if(!(left_type[0] & type_mask)){
  181. h->topleft_samples_available&= 0xDFFF;
  182. h->left_samples_available&= 0x5FFF;
  183. }
  184. if(!(left_type[1] & type_mask)){
  185. h->topleft_samples_available&= 0xFF5F;
  186. h->left_samples_available&= 0xFF5F;
  187. }
  188. }else{
  189. int left_typei = h->slice_table[left_xy[0] + s->mb_stride ] == h->slice_num
  190. ? s->current_picture.mb_type[left_xy[0] + s->mb_stride] : 0;
  191. assert(left_xy[0] == left_xy[1]);
  192. if(!((left_typei & type_mask) && (left_type[0] & type_mask))){
  193. h->topleft_samples_available&= 0xDF5F;
  194. h->left_samples_available&= 0x5F5F;
  195. }
  196. }
  197. }else{
  198. if(!(left_type[0] & type_mask)){
  199. h->topleft_samples_available&= 0xDF5F;
  200. h->left_samples_available&= 0x5F5F;
  201. }
  202. }
  203. if(!(topleft_type & type_mask))
  204. h->topleft_samples_available&= 0x7FFF;
  205. if(!(topright_type & type_mask))
  206. h->topright_samples_available&= 0xFBFF;
  207. if(IS_INTRA4x4(mb_type)){
  208. if(IS_INTRA4x4(top_type)){
  209. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  210. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  211. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  212. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  213. }else{
  214. int pred;
  215. if(!(top_type & type_mask))
  216. pred= -1;
  217. else{
  218. pred= 2;
  219. }
  220. h->intra4x4_pred_mode_cache[4+8*0]=
  221. h->intra4x4_pred_mode_cache[5+8*0]=
  222. h->intra4x4_pred_mode_cache[6+8*0]=
  223. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  224. }
  225. for(i=0; i<2; i++){
  226. if(IS_INTRA4x4(left_type[i])){
  227. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  228. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  229. }else{
  230. int pred;
  231. if(!(left_type[i] & type_mask))
  232. pred= -1;
  233. else{
  234. pred= 2;
  235. }
  236. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  237. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. /*
  244. 0 . T T. T T T T
  245. 1 L . .L . . . .
  246. 2 L . .L . . . .
  247. 3 . T TL . . . .
  248. 4 L . .L . . . .
  249. 5 L . .. . . . .
  250. */
  251. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  252. if(top_type){
  253. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  254. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  255. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  256. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  257. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  258. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  259. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  260. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  261. }else{
  262. h->non_zero_count_cache[4+8*0]=
  263. h->non_zero_count_cache[5+8*0]=
  264. h->non_zero_count_cache[6+8*0]=
  265. h->non_zero_count_cache[7+8*0]=
  266. h->non_zero_count_cache[1+8*0]=
  267. h->non_zero_count_cache[2+8*0]=
  268. h->non_zero_count_cache[1+8*3]=
  269. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  270. }
  271. for (i=0; i<2; i++) {
  272. if(left_type[i]){
  273. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  274. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  275. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  276. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  277. }else{
  278. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  279. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  280. h->non_zero_count_cache[0+8*1 + 8*i]=
  281. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  282. }
  283. }
  284. if( h->pps.cabac ) {
  285. // top_cbp
  286. if(top_type) {
  287. h->top_cbp = h->cbp_table[top_xy];
  288. } else if(IS_INTRA(mb_type)) {
  289. h->top_cbp = 0x1C0;
  290. } else {
  291. h->top_cbp = 0;
  292. }
  293. // left_cbp
  294. if (left_type[0]) {
  295. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  296. } else if(IS_INTRA(mb_type)) {
  297. h->left_cbp = 0x1C0;
  298. } else {
  299. h->left_cbp = 0;
  300. }
  301. if (left_type[0]) {
  302. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  303. }
  304. if (left_type[1]) {
  305. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  306. }
  307. }
  308. #if 1
  309. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  310. int list;
  311. for(list=0; list<h->list_count; list++){
  312. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  313. /*if(!h->mv_cache_clean[list]){
  314. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  315. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  316. h->mv_cache_clean[list]= 1;
  317. }*/
  318. continue;
  319. }
  320. h->mv_cache_clean[list]= 0;
  321. if(USES_LIST(top_type, list)){
  322. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  323. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  324. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  325. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  326. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  327. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  328. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  329. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  330. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  331. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  332. }else{
  333. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  334. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  335. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  336. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  337. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  338. }
  339. for(i=0; i<2; i++){
  340. int cache_idx = scan8[0] - 1 + i*2*8;
  341. if(USES_LIST(left_type[i], list)){
  342. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  343. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  344. *(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]];
  345. *(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]];
  346. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  347. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  348. }else{
  349. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  350. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  351. h->ref_cache[list][cache_idx ]=
  352. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  353. }
  354. }
  355. if(for_deblock || ((IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred) && !FRAME_MBAFF))
  356. continue;
  357. if(USES_LIST(topleft_type, list)){
  358. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
  359. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
  360. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  361. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  362. }else{
  363. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  364. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  365. }
  366. if(USES_LIST(topright_type, list)){
  367. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  368. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  369. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  370. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  371. }else{
  372. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  373. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  374. }
  375. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  376. continue;
  377. h->ref_cache[list][scan8[5 ]+1] =
  378. h->ref_cache[list][scan8[7 ]+1] =
  379. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  380. h->ref_cache[list][scan8[4 ]] =
  381. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  382. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  383. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  384. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  385. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  386. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  387. if( h->pps.cabac ) {
  388. /* XXX beurk, Load mvd */
  389. if(USES_LIST(top_type, list)){
  390. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  391. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  392. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  393. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  394. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  395. }else{
  396. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  397. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  398. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  399. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  400. }
  401. if(USES_LIST(left_type[0], list)){
  402. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  403. *(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]];
  404. *(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]];
  405. }else{
  406. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  407. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  408. }
  409. if(USES_LIST(left_type[1], list)){
  410. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  411. *(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]];
  412. *(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]];
  413. }else{
  414. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  415. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  416. }
  417. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  418. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  419. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  420. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  421. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  422. if(h->slice_type_nos == FF_B_TYPE){
  423. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  424. if(IS_DIRECT(top_type)){
  425. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  426. }else if(IS_8X8(top_type)){
  427. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  428. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  429. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  430. }else{
  431. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  432. }
  433. if(IS_DIRECT(left_type[0]))
  434. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  435. else if(IS_8X8(left_type[0]))
  436. 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)];
  437. else
  438. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  439. if(IS_DIRECT(left_type[1]))
  440. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  441. else if(IS_8X8(left_type[1]))
  442. 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)];
  443. else
  444. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  445. }
  446. }
  447. if(FRAME_MBAFF){
  448. #define MAP_MVS\
  449. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  450. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  451. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  452. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  453. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  454. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  455. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  456. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  457. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  458. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  459. if(MB_FIELD){
  460. #define MAP_F2F(idx, mb_type)\
  461. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  462. h->ref_cache[list][idx] <<= 1;\
  463. h->mv_cache[list][idx][1] /= 2;\
  464. h->mvd_cache[list][idx][1] /= 2;\
  465. }
  466. MAP_MVS
  467. #undef MAP_F2F
  468. }else{
  469. #define MAP_F2F(idx, mb_type)\
  470. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  471. h->ref_cache[list][idx] >>= 1;\
  472. h->mv_cache[list][idx][1] <<= 1;\
  473. h->mvd_cache[list][idx][1] <<= 1;\
  474. }
  475. MAP_MVS
  476. #undef MAP_F2F
  477. }
  478. }
  479. }
  480. }
  481. #endif
  482. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  483. }
  484. void ff_h264_write_back_intra_pred_mode(H264Context *h){
  485. const int mb_xy= h->mb_xy;
  486. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  487. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  488. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  489. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  490. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  491. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  492. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  493. }
  494. /**
  495. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  496. */
  497. int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
  498. MpegEncContext * const s = &h->s;
  499. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  500. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  501. if(mode > 6U) {
  502. 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);
  503. return -1;
  504. }
  505. if(!(h->top_samples_available&0x8000)){
  506. mode= top[ mode ];
  507. if(mode<0){
  508. 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);
  509. return -1;
  510. }
  511. }
  512. if((h->left_samples_available&0x8080) != 0x8080){
  513. mode= left[ mode ];
  514. if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
  515. mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
  516. }
  517. if(mode<0){
  518. 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);
  519. return -1;
  520. }
  521. }
  522. return mode;
  523. }
  524. /**
  525. * gets the predicted intra4x4 prediction mode.
  526. */
  527. static inline int pred_intra_mode(H264Context *h, int n){
  528. const int index8= scan8[n];
  529. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  530. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  531. const int min= FFMIN(left, top);
  532. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  533. if(min<0) return DC_PRED;
  534. else return min;
  535. }
  536. static inline void write_back_non_zero_count(H264Context *h){
  537. const int mb_xy= h->mb_xy;
  538. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  539. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  540. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  541. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  542. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  543. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  544. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  545. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  546. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  547. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  548. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  549. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  550. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  551. }
  552. /**
  553. * gets the predicted number of non-zero coefficients.
  554. * @param n block index
  555. */
  556. static inline int pred_non_zero_count(H264Context *h, int n){
  557. const int index8= scan8[n];
  558. const int left= h->non_zero_count_cache[index8 - 1];
  559. const int top = h->non_zero_count_cache[index8 - 8];
  560. int i= left + top;
  561. if(i<64) i= (i+1)>>1;
  562. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  563. return i&31;
  564. }
  565. /**
  566. * gets the directionally predicted 16x8 MV.
  567. * @param n the block index
  568. * @param mx the x component of the predicted motion vector
  569. * @param my the y component of the predicted motion vector
  570. */
  571. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  572. if(n==0){
  573. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  574. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  575. 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);
  576. if(top_ref == ref){
  577. *mx= B[0];
  578. *my= B[1];
  579. return;
  580. }
  581. }else{
  582. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  583. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  584. 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);
  585. if(left_ref == ref){
  586. *mx= A[0];
  587. *my= A[1];
  588. return;
  589. }
  590. }
  591. //RARE
  592. pred_motion(h, n, 4, list, ref, mx, my);
  593. }
  594. /**
  595. * gets the directionally predicted 8x16 MV.
  596. * @param n the block index
  597. * @param mx the x component of the predicted motion vector
  598. * @param my the y component of the predicted motion vector
  599. */
  600. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  601. if(n==0){
  602. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  603. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  604. 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);
  605. if(left_ref == ref){
  606. *mx= A[0];
  607. *my= A[1];
  608. return;
  609. }
  610. }else{
  611. const int16_t * C;
  612. int diagonal_ref;
  613. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  614. 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);
  615. if(diagonal_ref == ref){
  616. *mx= C[0];
  617. *my= C[1];
  618. return;
  619. }
  620. }
  621. //RARE
  622. pred_motion(h, n, 2, list, ref, mx, my);
  623. }
  624. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  625. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  626. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  627. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  628. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  629. || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
  630. || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
  631. *mx = *my = 0;
  632. return;
  633. }
  634. pred_motion(h, 0, 4, 0, 0, mx, my);
  635. return;
  636. }
  637. static inline void write_back_motion(H264Context *h, int mb_type){
  638. MpegEncContext * const s = &h->s;
  639. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  640. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  641. int list;
  642. if(!USES_LIST(mb_type, 0))
  643. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  644. for(list=0; list<h->list_count; list++){
  645. int y;
  646. if(!USES_LIST(mb_type, list))
  647. continue;
  648. for(y=0; y<4; y++){
  649. *(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];
  650. *(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];
  651. }
  652. if( h->pps.cabac ) {
  653. if(IS_SKIP(mb_type))
  654. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  655. else
  656. for(y=0; y<4; y++){
  657. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  658. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  659. }
  660. }
  661. {
  662. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  663. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  664. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  665. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  666. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  667. }
  668. }
  669. if(h->slice_type_nos == FF_B_TYPE && h->pps.cabac){
  670. if(IS_8X8(mb_type)){
  671. uint8_t *direct_table = &h->direct_table[b8_xy];
  672. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  673. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  674. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  675. }
  676. }
  677. }
  678. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  679. int i, si, di;
  680. uint8_t *dst;
  681. int bufidx;
  682. // src[0]&0x80; //forbidden bit
  683. h->nal_ref_idc= src[0]>>5;
  684. h->nal_unit_type= src[0]&0x1F;
  685. src++; length--;
  686. #if 0
  687. for(i=0; i<length; i++)
  688. printf("%2X ", src[i]);
  689. #endif
  690. #if HAVE_FAST_UNALIGNED
  691. # if HAVE_FAST_64BIT
  692. # define RS 7
  693. for(i=0; i+1<length; i+=9){
  694. if(!((~*(const uint64_t*)(src+i) & (*(const uint64_t*)(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  695. # else
  696. # define RS 3
  697. for(i=0; i+1<length; i+=5){
  698. if(!((~*(const uint32_t*)(src+i) & (*(const uint32_t*)(src+i) - 0x01000101U)) & 0x80008080U))
  699. # endif
  700. continue;
  701. if(i>0 && !src[i]) i--;
  702. while(src[i]) i++;
  703. #else
  704. # define RS 0
  705. for(i=0; i+1<length; i+=2){
  706. if(src[i]) continue;
  707. if(i>0 && src[i-1]==0) i--;
  708. #endif
  709. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  710. if(src[i+2]!=3){
  711. /* startcode, so we must be past the end */
  712. length=i;
  713. }
  714. break;
  715. }
  716. i-= RS;
  717. }
  718. if(i>=length-1){ //no escaped 0
  719. *dst_length= length;
  720. *consumed= length+1; //+1 for the header
  721. return src;
  722. }
  723. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  724. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  725. dst= h->rbsp_buffer[bufidx];
  726. if (dst == NULL){
  727. return NULL;
  728. }
  729. //printf("decoding esc\n");
  730. memcpy(dst, src, i);
  731. si=di=i;
  732. while(si+2<length){
  733. //remove escapes (very rare 1:2^22)
  734. if(src[si+2]>3){
  735. dst[di++]= src[si++];
  736. dst[di++]= src[si++];
  737. }else if(src[si]==0 && src[si+1]==0){
  738. if(src[si+2]==3){ //escape
  739. dst[di++]= 0;
  740. dst[di++]= 0;
  741. si+=3;
  742. continue;
  743. }else //next start code
  744. goto nsc;
  745. }
  746. dst[di++]= src[si++];
  747. }
  748. while(si<length)
  749. dst[di++]= src[si++];
  750. nsc:
  751. memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  752. *dst_length= di;
  753. *consumed= si + 1;//+1 for the header
  754. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  755. return dst;
  756. }
  757. int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  758. int v= *src;
  759. int r;
  760. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  761. for(r=1; r<9; r++){
  762. if(v&1) return r;
  763. v>>=1;
  764. }
  765. return 0;
  766. }
  767. /**
  768. * IDCT transforms the 16 dc values and dequantizes them.
  769. * @param qp quantization parameter
  770. */
  771. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  772. #define stride 16
  773. int i;
  774. int temp[16]; //FIXME check if this is a good idea
  775. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  776. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  777. //memset(block, 64, 2*256);
  778. //return;
  779. for(i=0; i<4; i++){
  780. const int offset= y_offset[i];
  781. const int z0= block[offset+stride*0] + block[offset+stride*4];
  782. const int z1= block[offset+stride*0] - block[offset+stride*4];
  783. const int z2= block[offset+stride*1] - block[offset+stride*5];
  784. const int z3= block[offset+stride*1] + block[offset+stride*5];
  785. temp[4*i+0]= z0+z3;
  786. temp[4*i+1]= z1+z2;
  787. temp[4*i+2]= z1-z2;
  788. temp[4*i+3]= z0-z3;
  789. }
  790. for(i=0; i<4; i++){
  791. const int offset= x_offset[i];
  792. const int z0= temp[4*0+i] + temp[4*2+i];
  793. const int z1= temp[4*0+i] - temp[4*2+i];
  794. const int z2= temp[4*1+i] - temp[4*3+i];
  795. const int z3= temp[4*1+i] + temp[4*3+i];
  796. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
  797. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  798. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  799. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  800. }
  801. }
  802. #if 0
  803. /**
  804. * DCT transforms the 16 dc values.
  805. * @param qp quantization parameter ??? FIXME
  806. */
  807. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  808. // const int qmul= dequant_coeff[qp][0];
  809. int i;
  810. int temp[16]; //FIXME check if this is a good idea
  811. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  812. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  813. for(i=0; i<4; i++){
  814. const int offset= y_offset[i];
  815. const int z0= block[offset+stride*0] + block[offset+stride*4];
  816. const int z1= block[offset+stride*0] - block[offset+stride*4];
  817. const int z2= block[offset+stride*1] - block[offset+stride*5];
  818. const int z3= block[offset+stride*1] + block[offset+stride*5];
  819. temp[4*i+0]= z0+z3;
  820. temp[4*i+1]= z1+z2;
  821. temp[4*i+2]= z1-z2;
  822. temp[4*i+3]= z0-z3;
  823. }
  824. for(i=0; i<4; i++){
  825. const int offset= x_offset[i];
  826. const int z0= temp[4*0+i] + temp[4*2+i];
  827. const int z1= temp[4*0+i] - temp[4*2+i];
  828. const int z2= temp[4*1+i] - temp[4*3+i];
  829. const int z3= temp[4*1+i] + temp[4*3+i];
  830. block[stride*0 +offset]= (z0 + z3)>>1;
  831. block[stride*2 +offset]= (z1 + z2)>>1;
  832. block[stride*8 +offset]= (z1 - z2)>>1;
  833. block[stride*10+offset]= (z0 - z3)>>1;
  834. }
  835. }
  836. #endif
  837. #undef xStride
  838. #undef stride
  839. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  840. const int stride= 16*2;
  841. const int xStride= 16;
  842. int a,b,c,d,e;
  843. a= block[stride*0 + xStride*0];
  844. b= block[stride*0 + xStride*1];
  845. c= block[stride*1 + xStride*0];
  846. d= block[stride*1 + xStride*1];
  847. e= a-b;
  848. a= a+b;
  849. b= c-d;
  850. c= c+d;
  851. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  852. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  853. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  854. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  855. }
  856. #if 0
  857. static void chroma_dc_dct_c(DCTELEM *block){
  858. const int stride= 16*2;
  859. const int xStride= 16;
  860. int a,b,c,d,e;
  861. a= block[stride*0 + xStride*0];
  862. b= block[stride*0 + xStride*1];
  863. c= block[stride*1 + xStride*0];
  864. d= block[stride*1 + xStride*1];
  865. e= a-b;
  866. a= a+b;
  867. b= c-d;
  868. c= c+d;
  869. block[stride*0 + xStride*0]= (a+c);
  870. block[stride*0 + xStride*1]= (e+b);
  871. block[stride*1 + xStride*0]= (a-c);
  872. block[stride*1 + xStride*1]= (e-b);
  873. }
  874. #endif
  875. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  876. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  877. int src_x_offset, int src_y_offset,
  878. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  879. MpegEncContext * const s = &h->s;
  880. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  881. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  882. const int luma_xy= (mx&3) + ((my&3)<<2);
  883. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  884. uint8_t * src_cb, * src_cr;
  885. int extra_width= h->emu_edge_width;
  886. int extra_height= h->emu_edge_height;
  887. int emu=0;
  888. const int full_mx= mx>>2;
  889. const int full_my= my>>2;
  890. const int pic_width = 16*s->mb_width;
  891. const int pic_height = 16*s->mb_height >> MB_FIELD;
  892. if(mx&7) extra_width -= 3;
  893. if(my&7) extra_height -= 3;
  894. if( full_mx < 0-extra_width
  895. || full_my < 0-extra_height
  896. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  897. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  898. 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);
  899. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  900. emu=1;
  901. }
  902. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  903. if(!square){
  904. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  905. }
  906. if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  907. if(MB_FIELD){
  908. // chroma offset when predicting from a field of opposite parity
  909. my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
  910. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  911. }
  912. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  913. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  914. if(emu){
  915. 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);
  916. src_cb= s->edge_emu_buffer;
  917. }
  918. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  919. if(emu){
  920. 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);
  921. src_cr= s->edge_emu_buffer;
  922. }
  923. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  924. }
  925. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  926. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  927. int x_offset, int y_offset,
  928. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  929. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  930. int list0, int list1){
  931. MpegEncContext * const s = &h->s;
  932. qpel_mc_func *qpix_op= qpix_put;
  933. h264_chroma_mc_func chroma_op= chroma_put;
  934. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  935. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  936. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  937. x_offset += 8*s->mb_x;
  938. y_offset += 8*(s->mb_y >> MB_FIELD);
  939. if(list0){
  940. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  941. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  942. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  943. qpix_op, chroma_op);
  944. qpix_op= qpix_avg;
  945. chroma_op= chroma_avg;
  946. }
  947. if(list1){
  948. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  949. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  950. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  951. qpix_op, chroma_op);
  952. }
  953. }
  954. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  955. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  956. int x_offset, int y_offset,
  957. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  958. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  959. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  960. int list0, int list1){
  961. MpegEncContext * const s = &h->s;
  962. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  963. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  964. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  965. x_offset += 8*s->mb_x;
  966. y_offset += 8*(s->mb_y >> MB_FIELD);
  967. if(list0 && list1){
  968. /* don't optimize for luma-only case, since B-frames usually
  969. * use implicit weights => chroma too. */
  970. uint8_t *tmp_cb = s->obmc_scratchpad;
  971. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  972. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  973. int refn0 = h->ref_cache[0][ scan8[n] ];
  974. int refn1 = h->ref_cache[1][ scan8[n] ];
  975. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  976. dest_y, dest_cb, dest_cr,
  977. x_offset, y_offset, qpix_put, chroma_put);
  978. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  979. tmp_y, tmp_cb, tmp_cr,
  980. x_offset, y_offset, qpix_put, chroma_put);
  981. if(h->use_weight == 2){
  982. int weight0 = h->implicit_weight[refn0][refn1];
  983. int weight1 = 64 - weight0;
  984. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  985. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  986. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  987. }else{
  988. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  989. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  990. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  991. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  992. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  993. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  994. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  995. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  996. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  997. }
  998. }else{
  999. int list = list1 ? 1 : 0;
  1000. int refn = h->ref_cache[list][ scan8[n] ];
  1001. Picture *ref= &h->ref_list[list][refn];
  1002. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  1003. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1004. qpix_put, chroma_put);
  1005. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  1006. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  1007. if(h->use_weight_chroma){
  1008. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1009. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  1010. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1011. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  1012. }
  1013. }
  1014. }
  1015. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  1016. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1017. int x_offset, int y_offset,
  1018. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1019. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1020. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  1021. int list0, int list1){
  1022. if((h->use_weight==2 && list0 && list1
  1023. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  1024. || h->use_weight==1)
  1025. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1026. x_offset, y_offset, qpix_put, chroma_put,
  1027. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  1028. else
  1029. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1030. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  1031. }
  1032. static inline void prefetch_motion(H264Context *h, int list){
  1033. /* fetch pixels for estimated mv 4 macroblocks ahead
  1034. * optimized for 64byte cache lines */
  1035. MpegEncContext * const s = &h->s;
  1036. const int refn = h->ref_cache[list][scan8[0]];
  1037. if(refn >= 0){
  1038. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  1039. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  1040. uint8_t **src= h->ref_list[list][refn].data;
  1041. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  1042. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1043. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  1044. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1045. }
  1046. }
  1047. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1048. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  1049. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  1050. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  1051. MpegEncContext * const s = &h->s;
  1052. const int mb_xy= h->mb_xy;
  1053. const int mb_type= s->current_picture.mb_type[mb_xy];
  1054. assert(IS_INTER(mb_type));
  1055. prefetch_motion(h, 0);
  1056. if(IS_16X16(mb_type)){
  1057. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  1058. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  1059. &weight_op[0], &weight_avg[0],
  1060. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1061. }else if(IS_16X8(mb_type)){
  1062. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  1063. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1064. &weight_op[1], &weight_avg[1],
  1065. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1066. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  1067. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1068. &weight_op[1], &weight_avg[1],
  1069. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1070. }else if(IS_8X16(mb_type)){
  1071. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  1072. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1073. &weight_op[2], &weight_avg[2],
  1074. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1075. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  1076. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1077. &weight_op[2], &weight_avg[2],
  1078. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1079. }else{
  1080. int i;
  1081. assert(IS_8X8(mb_type));
  1082. for(i=0; i<4; i++){
  1083. const int sub_mb_type= h->sub_mb_type[i];
  1084. const int n= 4*i;
  1085. int x_offset= (i&1)<<2;
  1086. int y_offset= (i&2)<<1;
  1087. if(IS_SUB_8X8(sub_mb_type)){
  1088. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1089. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1090. &weight_op[3], &weight_avg[3],
  1091. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1092. }else if(IS_SUB_8X4(sub_mb_type)){
  1093. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1094. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1095. &weight_op[4], &weight_avg[4],
  1096. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1097. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  1098. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1099. &weight_op[4], &weight_avg[4],
  1100. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1101. }else if(IS_SUB_4X8(sub_mb_type)){
  1102. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1103. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1104. &weight_op[5], &weight_avg[5],
  1105. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1106. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  1107. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1108. &weight_op[5], &weight_avg[5],
  1109. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1110. }else{
  1111. int j;
  1112. assert(IS_SUB_4X4(sub_mb_type));
  1113. for(j=0; j<4; j++){
  1114. int sub_x_offset= x_offset + 2*(j&1);
  1115. int sub_y_offset= y_offset + (j&2);
  1116. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  1117. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1118. &weight_op[6], &weight_avg[6],
  1119. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1120. }
  1121. }
  1122. }
  1123. }
  1124. prefetch_motion(h, 1);
  1125. }
  1126. static av_cold void init_cavlc_level_tab(void){
  1127. int suffix_length, mask;
  1128. unsigned int i;
  1129. for(suffix_length=0; suffix_length<7; suffix_length++){
  1130. for(i=0; i<(1<<LEVEL_TAB_BITS); i++){
  1131. int prefix= LEVEL_TAB_BITS - av_log2(2*i);
  1132. int level_code= (prefix<<suffix_length) + (i>>(LEVEL_TAB_BITS-prefix-1-suffix_length)) - (1<<suffix_length);
  1133. mask= -(level_code&1);
  1134. level_code= (((2+level_code)>>1) ^ mask) - mask;
  1135. if(prefix + 1 + suffix_length <= LEVEL_TAB_BITS){
  1136. cavlc_level_tab[suffix_length][i][0]= level_code;
  1137. cavlc_level_tab[suffix_length][i][1]= prefix + 1 + suffix_length;
  1138. }else if(prefix + 1 <= LEVEL_TAB_BITS){
  1139. cavlc_level_tab[suffix_length][i][0]= prefix+100;
  1140. cavlc_level_tab[suffix_length][i][1]= prefix + 1;
  1141. }else{
  1142. cavlc_level_tab[suffix_length][i][0]= LEVEL_TAB_BITS+100;
  1143. cavlc_level_tab[suffix_length][i][1]= LEVEL_TAB_BITS;
  1144. }
  1145. }
  1146. }
  1147. }
  1148. static av_cold void decode_init_vlc(void){
  1149. static int done = 0;
  1150. if (!done) {
  1151. int i;
  1152. int offset;
  1153. done = 1;
  1154. chroma_dc_coeff_token_vlc.table = chroma_dc_coeff_token_vlc_table;
  1155. chroma_dc_coeff_token_vlc.table_allocated = chroma_dc_coeff_token_vlc_table_size;
  1156. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  1157. &chroma_dc_coeff_token_len [0], 1, 1,
  1158. &chroma_dc_coeff_token_bits[0], 1, 1,
  1159. INIT_VLC_USE_NEW_STATIC);
  1160. offset = 0;
  1161. for(i=0; i<4; i++){
  1162. coeff_token_vlc[i].table = coeff_token_vlc_tables+offset;
  1163. coeff_token_vlc[i].table_allocated = coeff_token_vlc_tables_size[i];
  1164. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  1165. &coeff_token_len [i][0], 1, 1,
  1166. &coeff_token_bits[i][0], 1, 1,
  1167. INIT_VLC_USE_NEW_STATIC);
  1168. offset += coeff_token_vlc_tables_size[i];
  1169. }
  1170. /*
  1171. * This is a one time safety check to make sure that
  1172. * the packed static coeff_token_vlc table sizes
  1173. * were initialized correctly.
  1174. */
  1175. assert(offset == FF_ARRAY_ELEMS(coeff_token_vlc_tables));
  1176. for(i=0; i<3; i++){
  1177. chroma_dc_total_zeros_vlc[i].table = chroma_dc_total_zeros_vlc_tables[i];
  1178. chroma_dc_total_zeros_vlc[i].table_allocated = chroma_dc_total_zeros_vlc_tables_size;
  1179. init_vlc(&chroma_dc_total_zeros_vlc[i],
  1180. CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  1181. &chroma_dc_total_zeros_len [i][0], 1, 1,
  1182. &chroma_dc_total_zeros_bits[i][0], 1, 1,
  1183. INIT_VLC_USE_NEW_STATIC);
  1184. }
  1185. for(i=0; i<15; i++){
  1186. total_zeros_vlc[i].table = total_zeros_vlc_tables[i];
  1187. total_zeros_vlc[i].table_allocated = total_zeros_vlc_tables_size;
  1188. init_vlc(&total_zeros_vlc[i],
  1189. TOTAL_ZEROS_VLC_BITS, 16,
  1190. &total_zeros_len [i][0], 1, 1,
  1191. &total_zeros_bits[i][0], 1, 1,
  1192. INIT_VLC_USE_NEW_STATIC);
  1193. }
  1194. for(i=0; i<6; i++){
  1195. run_vlc[i].table = run_vlc_tables[i];
  1196. run_vlc[i].table_allocated = run_vlc_tables_size;
  1197. init_vlc(&run_vlc[i],
  1198. RUN_VLC_BITS, 7,
  1199. &run_len [i][0], 1, 1,
  1200. &run_bits[i][0], 1, 1,
  1201. INIT_VLC_USE_NEW_STATIC);
  1202. }
  1203. run7_vlc.table = run7_vlc_table,
  1204. run7_vlc.table_allocated = run7_vlc_table_size;
  1205. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  1206. &run_len [6][0], 1, 1,
  1207. &run_bits[6][0], 1, 1,
  1208. INIT_VLC_USE_NEW_STATIC);
  1209. init_cavlc_level_tab();
  1210. }
  1211. }
  1212. static void free_tables(H264Context *h){
  1213. int i;
  1214. H264Context *hx;
  1215. av_freep(&h->intra4x4_pred_mode);
  1216. av_freep(&h->chroma_pred_mode_table);
  1217. av_freep(&h->cbp_table);
  1218. av_freep(&h->mvd_table[0]);
  1219. av_freep(&h->mvd_table[1]);
  1220. av_freep(&h->direct_table);
  1221. av_freep(&h->non_zero_count);
  1222. av_freep(&h->slice_table_base);
  1223. h->slice_table= NULL;
  1224. av_freep(&h->mb2b_xy);
  1225. av_freep(&h->mb2b8_xy);
  1226. for(i = 0; i < MAX_THREADS; i++) {
  1227. hx = h->thread_context[i];
  1228. if(!hx) continue;
  1229. av_freep(&hx->top_borders[1]);
  1230. av_freep(&hx->top_borders[0]);
  1231. av_freep(&hx->s.obmc_scratchpad);
  1232. av_freep(&hx->rbsp_buffer[1]);
  1233. av_freep(&hx->rbsp_buffer[0]);
  1234. hx->rbsp_buffer_size[0] = 0;
  1235. hx->rbsp_buffer_size[1] = 0;
  1236. if (i) av_freep(&h->thread_context[i]);
  1237. }
  1238. }
  1239. static void init_dequant8_coeff_table(H264Context *h){
  1240. int i,q,x;
  1241. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  1242. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  1243. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  1244. for(i=0; i<2; i++ ){
  1245. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  1246. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  1247. break;
  1248. }
  1249. for(q=0; q<52; q++){
  1250. int shift = div6[q];
  1251. int idx = rem6[q];
  1252. for(x=0; x<64; x++)
  1253. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  1254. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  1255. h->pps.scaling_matrix8[i][x]) << shift;
  1256. }
  1257. }
  1258. }
  1259. static void init_dequant4_coeff_table(H264Context *h){
  1260. int i,j,q,x;
  1261. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  1262. for(i=0; i<6; i++ ){
  1263. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  1264. for(j=0; j<i; j++){
  1265. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  1266. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  1267. break;
  1268. }
  1269. }
  1270. if(j<i)
  1271. continue;
  1272. for(q=0; q<52; q++){
  1273. int shift = div6[q] + 2;
  1274. int idx = rem6[q];
  1275. for(x=0; x<16; x++)
  1276. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  1277. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  1278. h->pps.scaling_matrix4[i][x]) << shift;
  1279. }
  1280. }
  1281. }
  1282. static void init_dequant_tables(H264Context *h){
  1283. int i,x;
  1284. init_dequant4_coeff_table(h);
  1285. if(h->pps.transform_8x8_mode)
  1286. init_dequant8_coeff_table(h);
  1287. if(h->sps.transform_bypass){
  1288. for(i=0; i<6; i++)
  1289. for(x=0; x<16; x++)
  1290. h->dequant4_coeff[i][0][x] = 1<<6;
  1291. if(h->pps.transform_8x8_mode)
  1292. for(i=0; i<2; i++)
  1293. for(x=0; x<64; x++)
  1294. h->dequant8_coeff[i][0][x] = 1<<6;
  1295. }
  1296. }
  1297. int ff_h264_alloc_tables(H264Context *h){
  1298. MpegEncContext * const s = &h->s;
  1299. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1300. int x,y;
  1301. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t), fail)
  1302. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t), fail)
  1303. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
  1304. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
  1305. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
  1306. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t), fail);
  1307. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t), fail);
  1308. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 32*big_mb_num * sizeof(uint8_t) , fail);
  1309. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  1310. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  1311. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
  1312. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b8_xy , big_mb_num * sizeof(uint32_t), fail);
  1313. for(y=0; y<s->mb_height; y++){
  1314. for(x=0; x<s->mb_width; x++){
  1315. const int mb_xy= x + y*s->mb_stride;
  1316. const int b_xy = 4*x + 4*y*h->b_stride;
  1317. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1318. h->mb2b_xy [mb_xy]= b_xy;
  1319. h->mb2b8_xy[mb_xy]= b8_xy;
  1320. }
  1321. }
  1322. s->obmc_scratchpad = NULL;
  1323. if(!h->dequant4_coeff[0])
  1324. init_dequant_tables(h);
  1325. return 0;
  1326. fail:
  1327. free_tables(h);
  1328. return -1;
  1329. }
  1330. /**
  1331. * Mimic alloc_tables(), but for every context thread.
  1332. */
  1333. static void clone_tables(H264Context *dst, H264Context *src){
  1334. dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
  1335. dst->non_zero_count = src->non_zero_count;
  1336. dst->slice_table = src->slice_table;
  1337. dst->cbp_table = src->cbp_table;
  1338. dst->mb2b_xy = src->mb2b_xy;
  1339. dst->mb2b8_xy = src->mb2b8_xy;
  1340. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  1341. dst->mvd_table[0] = src->mvd_table[0];
  1342. dst->mvd_table[1] = src->mvd_table[1];
  1343. dst->direct_table = src->direct_table;
  1344. dst->s.obmc_scratchpad = NULL;
  1345. ff_h264_pred_init(&dst->hpc, src->s.codec_id);
  1346. }
  1347. /**
  1348. * Init context
  1349. * Allocate buffers which are not shared amongst multiple threads.
  1350. */
  1351. static int context_init(H264Context *h){
  1352. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  1353. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  1354. return 0;
  1355. fail:
  1356. return -1; // free_tables will clean up for us
  1357. }
  1358. static av_cold void common_init(H264Context *h){
  1359. MpegEncContext * const s = &h->s;
  1360. s->width = s->avctx->width;
  1361. s->height = s->avctx->height;
  1362. s->codec_id= s->avctx->codec->id;
  1363. ff_h264_pred_init(&h->hpc, s->codec_id);
  1364. h->dequant_coeff_pps= -1;
  1365. s->unrestricted_mv=1;
  1366. s->decode=1; //FIXME
  1367. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  1368. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  1369. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  1370. }
  1371. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  1372. H264Context *h= avctx->priv_data;
  1373. MpegEncContext * const s = &h->s;
  1374. MPV_decode_defaults(s);
  1375. s->avctx = avctx;
  1376. common_init(h);
  1377. s->out_format = FMT_H264;
  1378. s->workaround_bugs= avctx->workaround_bugs;
  1379. // set defaults
  1380. // s->decode_mb= ff_h263_decode_mb;
  1381. s->quarter_sample = 1;
  1382. if(!avctx->has_b_frames)
  1383. s->low_delay= 1;
  1384. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1385. decode_init_vlc();
  1386. if(avctx->extradata_size > 0 && avctx->extradata &&
  1387. *(char *)avctx->extradata == 1){
  1388. h->is_avc = 1;
  1389. h->got_avcC = 0;
  1390. } else {
  1391. h->is_avc = 0;
  1392. }
  1393. h->thread_context[0] = h;
  1394. h->outputed_poc = INT_MIN;
  1395. h->prev_poc_msb= 1<<16;
  1396. ff_h264_reset_sei(h);
  1397. if(avctx->codec_id == CODEC_ID_H264){
  1398. if(avctx->ticks_per_frame == 1){
  1399. s->avctx->time_base.den *=2;
  1400. }
  1401. avctx->ticks_per_frame = 2;
  1402. }
  1403. return 0;
  1404. }
  1405. int ff_h264_frame_start(H264Context *h){
  1406. MpegEncContext * const s = &h->s;
  1407. int i;
  1408. if(MPV_frame_start(s, s->avctx) < 0)
  1409. return -1;
  1410. ff_er_frame_start(s);
  1411. /*
  1412. * MPV_frame_start uses pict_type to derive key_frame.
  1413. * This is incorrect for H.264; IDR markings must be used.
  1414. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1415. * See decode_nal_units().
  1416. */
  1417. s->current_picture_ptr->key_frame= 0;
  1418. s->current_picture_ptr->mmco_reset= 0;
  1419. assert(s->linesize && s->uvlinesize);
  1420. for(i=0; i<16; i++){
  1421. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1422. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  1423. }
  1424. for(i=0; i<4; i++){
  1425. h->block_offset[16+i]=
  1426. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1427. h->block_offset[24+16+i]=
  1428. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1429. }
  1430. /* can't be in alloc_tables because linesize isn't known there.
  1431. * FIXME: redo bipred weight to not require extra buffer? */
  1432. for(i = 0; i < s->avctx->thread_count; i++)
  1433. if(!h->thread_context[i]->s.obmc_scratchpad)
  1434. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  1435. /* some macroblocks will be accessed before they're available */
  1436. if(FRAME_MBAFF || s->avctx->thread_count > 1)
  1437. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  1438. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1439. // We mark the current picture as non-reference after allocating it, so
  1440. // that if we break out due to an error it can be released automatically
  1441. // in the next MPV_frame_start().
  1442. // SVQ3 as well as most other codecs have only last/next/current and thus
  1443. // get released even with set reference, besides SVQ3 and others do not
  1444. // mark frames as reference later "naturally".
  1445. if(s->codec_id != CODEC_ID_SVQ3)
  1446. s->current_picture_ptr->reference= 0;
  1447. s->current_picture_ptr->field_poc[0]=
  1448. s->current_picture_ptr->field_poc[1]= INT_MAX;
  1449. assert(s->current_picture_ptr->long_ref==0);
  1450. return 0;
  1451. }
  1452. 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){
  1453. MpegEncContext * const s = &h->s;
  1454. int i;
  1455. int step = 1;
  1456. int offset = 1;
  1457. int uvoffset= 1;
  1458. int top_idx = 1;
  1459. int skiplast= 0;
  1460. src_y -= linesize;
  1461. src_cb -= uvlinesize;
  1462. src_cr -= uvlinesize;
  1463. if(!simple && FRAME_MBAFF){
  1464. if(s->mb_y&1){
  1465. offset = MB_MBAFF ? 1 : 17;
  1466. uvoffset= MB_MBAFF ? 1 : 9;
  1467. if(!MB_MBAFF){
  1468. *(uint64_t*)(h->top_borders[0][s->mb_x]+ 0)= *(uint64_t*)(src_y + 15*linesize);
  1469. *(uint64_t*)(h->top_borders[0][s->mb_x]+ 8)= *(uint64_t*)(src_y +8+15*linesize);
  1470. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1471. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+7*uvlinesize);
  1472. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+7*uvlinesize);
  1473. }
  1474. }
  1475. }else{
  1476. if(!MB_MBAFF){
  1477. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  1478. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1479. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7 ];
  1480. h->left_border[34+18]= h->top_borders[0][s->mb_x][16+8+7];
  1481. }
  1482. skiplast= 1;
  1483. }
  1484. offset =
  1485. uvoffset=
  1486. top_idx = MB_MBAFF ? 0 : 1;
  1487. }
  1488. step= MB_MBAFF ? 2 : 1;
  1489. }
  1490. // There are two lines saved, the line above the the top macroblock of a pair,
  1491. // and the line above the bottom macroblock
  1492. h->left_border[offset]= h->top_borders[top_idx][s->mb_x][15];
  1493. for(i=1; i<17 - skiplast; i++){
  1494. h->left_border[offset+i*step]= src_y[15+i* linesize];
  1495. }
  1496. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  1497. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  1498. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1499. h->left_border[uvoffset+34 ]= h->top_borders[top_idx][s->mb_x][16+7];
  1500. h->left_border[uvoffset+34+18]= h->top_borders[top_idx][s->mb_x][24+7];
  1501. for(i=1; i<9 - skiplast; i++){
  1502. h->left_border[uvoffset+34 +i*step]= src_cb[7+i*uvlinesize];
  1503. h->left_border[uvoffset+34+18+i*step]= src_cr[7+i*uvlinesize];
  1504. }
  1505. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  1506. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  1507. }
  1508. }
  1509. 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){
  1510. MpegEncContext * const s = &h->s;
  1511. int temp8, i;
  1512. uint64_t temp64;
  1513. int deblock_left;
  1514. int deblock_top;
  1515. int mb_xy;
  1516. int step = 1;
  1517. int offset = 1;
  1518. int uvoffset= 1;
  1519. int top_idx = 1;
  1520. if(!simple && FRAME_MBAFF){
  1521. if(s->mb_y&1){
  1522. offset = MB_MBAFF ? 1 : 17;
  1523. uvoffset= MB_MBAFF ? 1 : 9;
  1524. }else{
  1525. offset =
  1526. uvoffset=
  1527. top_idx = MB_MBAFF ? 0 : 1;
  1528. }
  1529. step= MB_MBAFF ? 2 : 1;
  1530. }
  1531. if(h->deblocking_filter == 2) {
  1532. mb_xy = h->mb_xy;
  1533. deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
  1534. deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
  1535. } else {
  1536. deblock_left = (s->mb_x > 0);
  1537. deblock_top = (s->mb_y > !!MB_FIELD);
  1538. }
  1539. src_y -= linesize + 1;
  1540. src_cb -= uvlinesize + 1;
  1541. src_cr -= uvlinesize + 1;
  1542. #define XCHG(a,b,t,xchg)\
  1543. t= a;\
  1544. if(xchg)\
  1545. a= b;\
  1546. b= t;
  1547. if(deblock_left){
  1548. for(i = !deblock_top; i<16; i++){
  1549. XCHG(h->left_border[offset+i*step], src_y [i* linesize], temp8, xchg);
  1550. }
  1551. XCHG(h->left_border[offset+i*step], src_y [i* linesize], temp8, 1);
  1552. }
  1553. if(deblock_top){
  1554. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  1555. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  1556. if(s->mb_x+1 < s->mb_width){
  1557. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  1558. }
  1559. }
  1560. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1561. if(deblock_left){
  1562. for(i = !deblock_top; i<8; i++){
  1563. XCHG(h->left_border[uvoffset+34 +i*step], src_cb[i*uvlinesize], temp8, xchg);
  1564. XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, xchg);
  1565. }
  1566. XCHG(h->left_border[uvoffset+34 +i*step], src_cb[i*uvlinesize], temp8, 1);
  1567. XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, 1);
  1568. }
  1569. if(deblock_top){
  1570. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  1571. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  1572. }
  1573. }
  1574. }
  1575. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  1576. MpegEncContext * const s = &h->s;
  1577. const int mb_x= s->mb_x;
  1578. const int mb_y= s->mb_y;
  1579. const int mb_xy= h->mb_xy;
  1580. const int mb_type= s->current_picture.mb_type[mb_xy];
  1581. uint8_t *dest_y, *dest_cb, *dest_cr;
  1582. int linesize, uvlinesize /*dct_offset*/;
  1583. int i;
  1584. int *block_offset = &h->block_offset[0];
  1585. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  1586. /* is_h264 should always be true if SVQ3 is disabled. */
  1587. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  1588. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1589. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  1590. dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
  1591. dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
  1592. dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
  1593. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  1594. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  1595. if (!simple && MB_FIELD) {
  1596. linesize = h->mb_linesize = s->linesize * 2;
  1597. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  1598. block_offset = &h->block_offset[24];
  1599. if(mb_y&1){ //FIXME move out of this function?
  1600. dest_y -= s->linesize*15;
  1601. dest_cb-= s->uvlinesize*7;
  1602. dest_cr-= s->uvlinesize*7;
  1603. }
  1604. if(FRAME_MBAFF) {
  1605. int list;
  1606. for(list=0; list<h->list_count; list++){
  1607. if(!USES_LIST(mb_type, list))
  1608. continue;
  1609. if(IS_16X16(mb_type)){
  1610. int8_t *ref = &h->ref_cache[list][scan8[0]];
  1611. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  1612. }else{
  1613. for(i=0; i<16; i+=4){
  1614. int ref = h->ref_cache[list][scan8[i]];
  1615. if(ref >= 0)
  1616. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  1617. }
  1618. }
  1619. }
  1620. }
  1621. } else {
  1622. linesize = h->mb_linesize = s->linesize;
  1623. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  1624. // dct_offset = s->linesize * 16;
  1625. }
  1626. if (!simple && IS_INTRA_PCM(mb_type)) {
  1627. for (i=0; i<16; i++) {
  1628. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  1629. }
  1630. for (i=0; i<8; i++) {
  1631. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  1632. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  1633. }
  1634. } else {
  1635. if(IS_INTRA(mb_type)){
  1636. if(h->deblocking_filter)
  1637. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
  1638. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1639. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1640. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1641. }
  1642. if(IS_INTRA4x4(mb_type)){
  1643. if(simple || !s->encoding){
  1644. if(IS_8x8DCT(mb_type)){
  1645. if(transform_bypass){
  1646. idct_dc_add =
  1647. idct_add = s->dsp.add_pixels8;
  1648. }else{
  1649. idct_dc_add = s->dsp.h264_idct8_dc_add;
  1650. idct_add = s->dsp.h264_idct8_add;
  1651. }
  1652. for(i=0; i<16; i+=4){
  1653. uint8_t * const ptr= dest_y + block_offset[i];
  1654. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1655. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1656. h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
  1657. }else{
  1658. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  1659. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1660. (h->topright_samples_available<<i)&0x4000, linesize);
  1661. if(nnz){
  1662. if(nnz == 1 && h->mb[i*16])
  1663. idct_dc_add(ptr, h->mb + i*16, linesize);
  1664. else
  1665. idct_add (ptr, h->mb + i*16, linesize);
  1666. }
  1667. }
  1668. }
  1669. }else{
  1670. if(transform_bypass){
  1671. idct_dc_add =
  1672. idct_add = s->dsp.add_pixels4;
  1673. }else{
  1674. idct_dc_add = s->dsp.h264_idct_dc_add;
  1675. idct_add = s->dsp.h264_idct_add;
  1676. }
  1677. for(i=0; i<16; i++){
  1678. uint8_t * const ptr= dest_y + block_offset[i];
  1679. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1680. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1681. h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
  1682. }else{
  1683. uint8_t *topright;
  1684. int nnz, tr;
  1685. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1686. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1687. assert(mb_y || linesize <= block_offset[i]);
  1688. if(!topright_avail){
  1689. tr= ptr[3 - linesize]*0x01010101;
  1690. topright= (uint8_t*) &tr;
  1691. }else
  1692. topright= ptr + 4 - linesize;
  1693. }else
  1694. topright= NULL;
  1695. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1696. nnz = h->non_zero_count_cache[ scan8[i] ];
  1697. if(nnz){
  1698. if(is_h264){
  1699. if(nnz == 1 && h->mb[i*16])
  1700. idct_dc_add(ptr, h->mb + i*16, linesize);
  1701. else
  1702. idct_add (ptr, h->mb + i*16, linesize);
  1703. }else
  1704. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1705. }
  1706. }
  1707. }
  1708. }
  1709. }
  1710. }else{
  1711. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1712. if(is_h264){
  1713. if(!transform_bypass)
  1714. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
  1715. }else
  1716. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1717. }
  1718. if(h->deblocking_filter)
  1719. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
  1720. }else if(is_h264){
  1721. hl_motion(h, dest_y, dest_cb, dest_cr,
  1722. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1723. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1724. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  1725. }
  1726. if(!IS_INTRA4x4(mb_type)){
  1727. if(is_h264){
  1728. if(IS_INTRA16x16(mb_type)){
  1729. if(transform_bypass){
  1730. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1731. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  1732. }else{
  1733. for(i=0; i<16; i++){
  1734. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1735. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
  1736. }
  1737. }
  1738. }else{
  1739. s->dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1740. }
  1741. }else if(h->cbp&15){
  1742. if(transform_bypass){
  1743. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1744. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1745. for(i=0; i<16; i+=di){
  1746. if(h->non_zero_count_cache[ scan8[i] ]){
  1747. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  1748. }
  1749. }
  1750. }else{
  1751. if(IS_8x8DCT(mb_type)){
  1752. s->dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1753. }else{
  1754. s->dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1755. }
  1756. }
  1757. }
  1758. }else{
  1759. for(i=0; i<16; i++){
  1760. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1761. uint8_t * const ptr= dest_y + block_offset[i];
  1762. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1763. }
  1764. }
  1765. }
  1766. }
  1767. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1768. uint8_t *dest[2] = {dest_cb, dest_cr};
  1769. if(transform_bypass){
  1770. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1771. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
  1772. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
  1773. }else{
  1774. idct_add = s->dsp.add_pixels4;
  1775. for(i=16; i<16+8; i++){
  1776. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1777. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1778. }
  1779. }
  1780. }else{
  1781. 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]);
  1782. 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]);
  1783. if(is_h264){
  1784. idct_add = s->dsp.h264_idct_add;
  1785. idct_dc_add = s->dsp.h264_idct_dc_add;
  1786. for(i=16; i<16+8; i++){
  1787. if(h->non_zero_count_cache[ scan8[i] ])
  1788. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1789. else if(h->mb[i*16])
  1790. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1791. }
  1792. }else{
  1793. for(i=16; i<16+8; i++){
  1794. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1795. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  1796. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
  1797. }
  1798. }
  1799. }
  1800. }
  1801. }
  1802. }
  1803. if(h->cbp || IS_INTRA(mb_type))
  1804. s->dsp.clear_blocks(h->mb);
  1805. if(h->deblocking_filter) {
  1806. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
  1807. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  1808. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  1809. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  1810. if (!simple && FRAME_MBAFF) {
  1811. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  1812. } else {
  1813. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  1814. }
  1815. }
  1816. }
  1817. /**
  1818. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1819. */
  1820. static void hl_decode_mb_simple(H264Context *h){
  1821. hl_decode_mb_internal(h, 1);
  1822. }
  1823. /**
  1824. * Process a macroblock; this handles edge cases, such as interlacing.
  1825. */
  1826. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1827. hl_decode_mb_internal(h, 0);
  1828. }
  1829. void ff_h264_hl_decode_mb(H264Context *h){
  1830. MpegEncContext * const s = &h->s;
  1831. const int mb_xy= h->mb_xy;
  1832. const int mb_type= s->current_picture.mb_type[mb_xy];
  1833. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1834. if (is_complex)
  1835. hl_decode_mb_complex(h);
  1836. else hl_decode_mb_simple(h);
  1837. }
  1838. static void pic_as_field(Picture *pic, const int parity){
  1839. int i;
  1840. for (i = 0; i < 4; ++i) {
  1841. if (parity == PICT_BOTTOM_FIELD)
  1842. pic->data[i] += pic->linesize[i];
  1843. pic->reference = parity;
  1844. pic->linesize[i] *= 2;
  1845. }
  1846. pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
  1847. }
  1848. static int split_field_copy(Picture *dest, Picture *src,
  1849. int parity, int id_add){
  1850. int match = !!(src->reference & parity);
  1851. if (match) {
  1852. *dest = *src;
  1853. if(parity != PICT_FRAME){
  1854. pic_as_field(dest, parity);
  1855. dest->pic_id *= 2;
  1856. dest->pic_id += id_add;
  1857. }
  1858. }
  1859. return match;
  1860. }
  1861. static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
  1862. int i[2]={0};
  1863. int index=0;
  1864. while(i[0]<len || i[1]<len){
  1865. while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
  1866. i[0]++;
  1867. while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
  1868. i[1]++;
  1869. if(i[0] < len){
  1870. in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
  1871. split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
  1872. }
  1873. if(i[1] < len){
  1874. in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
  1875. split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
  1876. }
  1877. }
  1878. return index;
  1879. }
  1880. static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
  1881. int i, best_poc;
  1882. int out_i= 0;
  1883. for(;;){
  1884. best_poc= dir ? INT_MIN : INT_MAX;
  1885. for(i=0; i<len; i++){
  1886. const int poc= src[i]->poc;
  1887. if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
  1888. best_poc= poc;
  1889. sorted[out_i]= src[i];
  1890. }
  1891. }
  1892. if(best_poc == (dir ? INT_MIN : INT_MAX))
  1893. break;
  1894. limit= sorted[out_i++]->poc - dir;
  1895. }
  1896. return out_i;
  1897. }
  1898. /**
  1899. * fills the default_ref_list.
  1900. */
  1901. static int fill_default_ref_list(H264Context *h){
  1902. MpegEncContext * const s = &h->s;
  1903. int i, len;
  1904. if(h->slice_type_nos==FF_B_TYPE){
  1905. Picture *sorted[32];
  1906. int cur_poc, list;
  1907. int lens[2];
  1908. if(FIELD_PICTURE)
  1909. cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
  1910. else
  1911. cur_poc= s->current_picture_ptr->poc;
  1912. for(list= 0; list<2; list++){
  1913. len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
  1914. len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
  1915. assert(len<=32);
  1916. len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
  1917. len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
  1918. assert(len<=32);
  1919. if(len < h->ref_count[list])
  1920. memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
  1921. lens[list]= len;
  1922. }
  1923. if(lens[0] == lens[1] && lens[1] > 1){
  1924. for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
  1925. if(i == lens[0])
  1926. FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
  1927. }
  1928. }else{
  1929. len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
  1930. len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
  1931. assert(len <= 32);
  1932. if(len < h->ref_count[0])
  1933. memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
  1934. }
  1935. #ifdef TRACE
  1936. for (i=0; i<h->ref_count[0]; i++) {
  1937. 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]);
  1938. }
  1939. if(h->slice_type_nos==FF_B_TYPE){
  1940. for (i=0; i<h->ref_count[1]; i++) {
  1941. 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]);
  1942. }
  1943. }
  1944. #endif
  1945. return 0;
  1946. }
  1947. static void print_short_term(H264Context *h);
  1948. static void print_long_term(H264Context *h);
  1949. /**
  1950. * Extract structure information about the picture described by pic_num in
  1951. * the current decoding context (frame or field). Note that pic_num is
  1952. * picture number without wrapping (so, 0<=pic_num<max_pic_num).
  1953. * @param pic_num picture number for which to extract structure information
  1954. * @param structure one of PICT_XXX describing structure of picture
  1955. * with pic_num
  1956. * @return frame number (short term) or long term index of picture
  1957. * described by pic_num
  1958. */
  1959. static int pic_num_extract(H264Context *h, int pic_num, int *structure){
  1960. MpegEncContext * const s = &h->s;
  1961. *structure = s->picture_structure;
  1962. if(FIELD_PICTURE){
  1963. if (!(pic_num & 1))
  1964. /* opposite field */
  1965. *structure ^= PICT_FRAME;
  1966. pic_num >>= 1;
  1967. }
  1968. return pic_num;
  1969. }
  1970. static int decode_ref_pic_list_reordering(H264Context *h){
  1971. MpegEncContext * const s = &h->s;
  1972. int list, index, pic_structure;
  1973. print_short_term(h);
  1974. print_long_term(h);
  1975. for(list=0; list<h->list_count; list++){
  1976. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  1977. if(get_bits1(&s->gb)){
  1978. int pred= h->curr_pic_num;
  1979. for(index=0; ; index++){
  1980. unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
  1981. unsigned int pic_id;
  1982. int i;
  1983. Picture *ref = NULL;
  1984. if(reordering_of_pic_nums_idc==3)
  1985. break;
  1986. if(index >= h->ref_count[list]){
  1987. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  1988. return -1;
  1989. }
  1990. if(reordering_of_pic_nums_idc<3){
  1991. if(reordering_of_pic_nums_idc<2){
  1992. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  1993. int frame_num;
  1994. if(abs_diff_pic_num > h->max_pic_num){
  1995. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  1996. return -1;
  1997. }
  1998. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  1999. else pred+= abs_diff_pic_num;
  2000. pred &= h->max_pic_num - 1;
  2001. frame_num = pic_num_extract(h, pred, &pic_structure);
  2002. for(i= h->short_ref_count-1; i>=0; i--){
  2003. ref = h->short_ref[i];
  2004. assert(ref->reference);
  2005. assert(!ref->long_ref);
  2006. if(
  2007. ref->frame_num == frame_num &&
  2008. (ref->reference & pic_structure)
  2009. )
  2010. break;
  2011. }
  2012. if(i>=0)
  2013. ref->pic_id= pred;
  2014. }else{
  2015. int long_idx;
  2016. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2017. long_idx= pic_num_extract(h, pic_id, &pic_structure);
  2018. if(long_idx>31){
  2019. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  2020. return -1;
  2021. }
  2022. ref = h->long_ref[long_idx];
  2023. assert(!(ref && !ref->reference));
  2024. if(ref && (ref->reference & pic_structure)){
  2025. ref->pic_id= pic_id;
  2026. assert(ref->long_ref);
  2027. i=0;
  2028. }else{
  2029. i=-1;
  2030. }
  2031. }
  2032. if (i < 0) {
  2033. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  2034. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2035. } else {
  2036. for(i=index; i+1<h->ref_count[list]; i++){
  2037. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  2038. break;
  2039. }
  2040. for(; i > index; i--){
  2041. h->ref_list[list][i]= h->ref_list[list][i-1];
  2042. }
  2043. h->ref_list[list][index]= *ref;
  2044. if (FIELD_PICTURE){
  2045. pic_as_field(&h->ref_list[list][index], pic_structure);
  2046. }
  2047. }
  2048. }else{
  2049. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  2050. return -1;
  2051. }
  2052. }
  2053. }
  2054. }
  2055. for(list=0; list<h->list_count; list++){
  2056. for(index= 0; index < h->ref_count[list]; index++){
  2057. if(!h->ref_list[list][index].data[0]){
  2058. av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
  2059. if(h->default_ref_list[list][0].data[0])
  2060. h->ref_list[list][index]= h->default_ref_list[list][0];
  2061. else
  2062. return -1;
  2063. }
  2064. }
  2065. }
  2066. return 0;
  2067. }
  2068. static void fill_mbaff_ref_list(H264Context *h){
  2069. int list, i, j;
  2070. for(list=0; list<2; list++){ //FIXME try list_count
  2071. for(i=0; i<h->ref_count[list]; i++){
  2072. Picture *frame = &h->ref_list[list][i];
  2073. Picture *field = &h->ref_list[list][16+2*i];
  2074. field[0] = *frame;
  2075. for(j=0; j<3; j++)
  2076. field[0].linesize[j] <<= 1;
  2077. field[0].reference = PICT_TOP_FIELD;
  2078. field[0].poc= field[0].field_poc[0];
  2079. field[1] = field[0];
  2080. for(j=0; j<3; j++)
  2081. field[1].data[j] += frame->linesize[j];
  2082. field[1].reference = PICT_BOTTOM_FIELD;
  2083. field[1].poc= field[1].field_poc[1];
  2084. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  2085. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  2086. for(j=0; j<2; j++){
  2087. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  2088. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  2089. }
  2090. }
  2091. }
  2092. for(j=0; j<h->ref_count[1]; j++){
  2093. for(i=0; i<h->ref_count[0]; i++)
  2094. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  2095. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  2096. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  2097. }
  2098. }
  2099. static int pred_weight_table(H264Context *h){
  2100. MpegEncContext * const s = &h->s;
  2101. int list, i;
  2102. int luma_def, chroma_def;
  2103. h->use_weight= 0;
  2104. h->use_weight_chroma= 0;
  2105. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2106. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2107. luma_def = 1<<h->luma_log2_weight_denom;
  2108. chroma_def = 1<<h->chroma_log2_weight_denom;
  2109. for(list=0; list<2; list++){
  2110. h->luma_weight_flag[list] = 0;
  2111. h->chroma_weight_flag[list] = 0;
  2112. for(i=0; i<h->ref_count[list]; i++){
  2113. int luma_weight_flag, chroma_weight_flag;
  2114. luma_weight_flag= get_bits1(&s->gb);
  2115. if(luma_weight_flag){
  2116. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2117. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2118. if( h->luma_weight[list][i] != luma_def
  2119. || h->luma_offset[list][i] != 0) {
  2120. h->use_weight= 1;
  2121. h->luma_weight_flag[list]= 1;
  2122. }
  2123. }else{
  2124. h->luma_weight[list][i]= luma_def;
  2125. h->luma_offset[list][i]= 0;
  2126. }
  2127. if(CHROMA){
  2128. chroma_weight_flag= get_bits1(&s->gb);
  2129. if(chroma_weight_flag){
  2130. int j;
  2131. for(j=0; j<2; j++){
  2132. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2133. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2134. if( h->chroma_weight[list][i][j] != chroma_def
  2135. || h->chroma_offset[list][i][j] != 0) {
  2136. h->use_weight_chroma= 1;
  2137. h->chroma_weight_flag[list]= 1;
  2138. }
  2139. }
  2140. }else{
  2141. int j;
  2142. for(j=0; j<2; j++){
  2143. h->chroma_weight[list][i][j]= chroma_def;
  2144. h->chroma_offset[list][i][j]= 0;
  2145. }
  2146. }
  2147. }
  2148. }
  2149. if(h->slice_type_nos != FF_B_TYPE) break;
  2150. }
  2151. h->use_weight= h->use_weight || h->use_weight_chroma;
  2152. return 0;
  2153. }
  2154. static void implicit_weight_table(H264Context *h){
  2155. MpegEncContext * const s = &h->s;
  2156. int ref0, ref1, i;
  2157. int cur_poc = s->current_picture_ptr->poc;
  2158. for (i = 0; i < 2; i++) {
  2159. h->luma_weight_flag[i] = 0;
  2160. h->chroma_weight_flag[i] = 0;
  2161. }
  2162. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  2163. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  2164. h->use_weight= 0;
  2165. h->use_weight_chroma= 0;
  2166. return;
  2167. }
  2168. h->use_weight= 2;
  2169. h->use_weight_chroma= 2;
  2170. h->luma_log2_weight_denom= 5;
  2171. h->chroma_log2_weight_denom= 5;
  2172. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  2173. int poc0 = h->ref_list[0][ref0].poc;
  2174. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  2175. int poc1 = h->ref_list[1][ref1].poc;
  2176. int td = av_clip(poc1 - poc0, -128, 127);
  2177. if(td){
  2178. int tb = av_clip(cur_poc - poc0, -128, 127);
  2179. int tx = (16384 + (FFABS(td) >> 1)) / td;
  2180. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  2181. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  2182. h->implicit_weight[ref0][ref1] = 32;
  2183. else
  2184. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  2185. }else
  2186. h->implicit_weight[ref0][ref1] = 32;
  2187. }
  2188. }
  2189. }
  2190. /**
  2191. * Mark a picture as no longer needed for reference. The refmask
  2192. * argument allows unreferencing of individual fields or the whole frame.
  2193. * If the picture becomes entirely unreferenced, but is being held for
  2194. * display purposes, it is marked as such.
  2195. * @param refmask mask of fields to unreference; the mask is bitwise
  2196. * anded with the reference marking of pic
  2197. * @return non-zero if pic becomes entirely unreferenced (except possibly
  2198. * for display purposes) zero if one of the fields remains in
  2199. * reference
  2200. */
  2201. static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
  2202. int i;
  2203. if (pic->reference &= refmask) {
  2204. return 0;
  2205. } else {
  2206. for(i = 0; h->delayed_pic[i]; i++)
  2207. if(pic == h->delayed_pic[i]){
  2208. pic->reference=DELAYED_PIC_REF;
  2209. break;
  2210. }
  2211. return 1;
  2212. }
  2213. }
  2214. /**
  2215. * instantaneous decoder refresh.
  2216. */
  2217. static void idr(H264Context *h){
  2218. int i;
  2219. for(i=0; i<16; i++){
  2220. remove_long(h, i, 0);
  2221. }
  2222. assert(h->long_ref_count==0);
  2223. for(i=0; i<h->short_ref_count; i++){
  2224. unreference_pic(h, h->short_ref[i], 0);
  2225. h->short_ref[i]= NULL;
  2226. }
  2227. h->short_ref_count=0;
  2228. h->prev_frame_num= 0;
  2229. h->prev_frame_num_offset= 0;
  2230. h->prev_poc_msb=
  2231. h->prev_poc_lsb= 0;
  2232. }
  2233. /* forget old pics after a seek */
  2234. static void flush_dpb(AVCodecContext *avctx){
  2235. H264Context *h= avctx->priv_data;
  2236. int i;
  2237. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  2238. if(h->delayed_pic[i])
  2239. h->delayed_pic[i]->reference= 0;
  2240. h->delayed_pic[i]= NULL;
  2241. }
  2242. h->outputed_poc= INT_MIN;
  2243. h->prev_interlaced_frame = 1;
  2244. idr(h);
  2245. if(h->s.current_picture_ptr)
  2246. h->s.current_picture_ptr->reference= 0;
  2247. h->s.first_field= 0;
  2248. ff_h264_reset_sei(h);
  2249. ff_mpeg_flush(avctx);
  2250. }
  2251. /**
  2252. * Find a Picture in the short term reference list by frame number.
  2253. * @param frame_num frame number to search for
  2254. * @param idx the index into h->short_ref where returned picture is found
  2255. * undefined if no picture found.
  2256. * @return pointer to the found picture, or NULL if no pic with the provided
  2257. * frame number is found
  2258. */
  2259. static Picture * find_short(H264Context *h, int frame_num, int *idx){
  2260. MpegEncContext * const s = &h->s;
  2261. int i;
  2262. for(i=0; i<h->short_ref_count; i++){
  2263. Picture *pic= h->short_ref[i];
  2264. if(s->avctx->debug&FF_DEBUG_MMCO)
  2265. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  2266. if(pic->frame_num == frame_num) {
  2267. *idx = i;
  2268. return pic;
  2269. }
  2270. }
  2271. return NULL;
  2272. }
  2273. /**
  2274. * Remove a picture from the short term reference list by its index in
  2275. * that list. This does no checking on the provided index; it is assumed
  2276. * to be valid. Other list entries are shifted down.
  2277. * @param i index into h->short_ref of picture to remove.
  2278. */
  2279. static void remove_short_at_index(H264Context *h, int i){
  2280. assert(i >= 0 && i < h->short_ref_count);
  2281. h->short_ref[i]= NULL;
  2282. if (--h->short_ref_count)
  2283. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
  2284. }
  2285. /**
  2286. *
  2287. * @return the removed picture or NULL if an error occurs
  2288. */
  2289. static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
  2290. MpegEncContext * const s = &h->s;
  2291. Picture *pic;
  2292. int i;
  2293. if(s->avctx->debug&FF_DEBUG_MMCO)
  2294. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  2295. pic = find_short(h, frame_num, &i);
  2296. if (pic){
  2297. if(unreference_pic(h, pic, ref_mask))
  2298. remove_short_at_index(h, i);
  2299. }
  2300. return pic;
  2301. }
  2302. /**
  2303. * Remove a picture from the long term reference list by its index in
  2304. * that list.
  2305. * @return the removed picture or NULL if an error occurs
  2306. */
  2307. static Picture * remove_long(H264Context *h, int i, int ref_mask){
  2308. Picture *pic;
  2309. pic= h->long_ref[i];
  2310. if (pic){
  2311. if(unreference_pic(h, pic, ref_mask)){
  2312. assert(h->long_ref[i]->long_ref == 1);
  2313. h->long_ref[i]->long_ref= 0;
  2314. h->long_ref[i]= NULL;
  2315. h->long_ref_count--;
  2316. }
  2317. }
  2318. return pic;
  2319. }
  2320. /**
  2321. * print short term list
  2322. */
  2323. static void print_short_term(H264Context *h) {
  2324. uint32_t i;
  2325. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2326. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  2327. for(i=0; i<h->short_ref_count; i++){
  2328. Picture *pic= h->short_ref[i];
  2329. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2330. }
  2331. }
  2332. }
  2333. /**
  2334. * print long term list
  2335. */
  2336. static void print_long_term(H264Context *h) {
  2337. uint32_t i;
  2338. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2339. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  2340. for(i = 0; i < 16; i++){
  2341. Picture *pic= h->long_ref[i];
  2342. if (pic) {
  2343. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2344. }
  2345. }
  2346. }
  2347. }
  2348. /**
  2349. * Executes the reference picture marking (memory management control operations).
  2350. */
  2351. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2352. MpegEncContext * const s = &h->s;
  2353. int i, av_uninit(j);
  2354. int current_ref_assigned=0;
  2355. Picture *av_uninit(pic);
  2356. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2357. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  2358. for(i=0; i<mmco_count; i++){
  2359. int av_uninit(structure), av_uninit(frame_num);
  2360. if(s->avctx->debug&FF_DEBUG_MMCO)
  2361. 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);
  2362. if( mmco[i].opcode == MMCO_SHORT2UNUSED
  2363. || mmco[i].opcode == MMCO_SHORT2LONG){
  2364. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  2365. pic = find_short(h, frame_num, &j);
  2366. if(!pic){
  2367. if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
  2368. || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
  2369. av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
  2370. continue;
  2371. }
  2372. }
  2373. switch(mmco[i].opcode){
  2374. case MMCO_SHORT2UNUSED:
  2375. if(s->avctx->debug&FF_DEBUG_MMCO)
  2376. 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);
  2377. remove_short(h, frame_num, structure ^ PICT_FRAME);
  2378. break;
  2379. case MMCO_SHORT2LONG:
  2380. if (h->long_ref[mmco[i].long_arg] != pic)
  2381. remove_long(h, mmco[i].long_arg, 0);
  2382. remove_short_at_index(h, j);
  2383. h->long_ref[ mmco[i].long_arg ]= pic;
  2384. if (h->long_ref[ mmco[i].long_arg ]){
  2385. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  2386. h->long_ref_count++;
  2387. }
  2388. break;
  2389. case MMCO_LONG2UNUSED:
  2390. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  2391. pic = h->long_ref[j];
  2392. if (pic) {
  2393. remove_long(h, j, structure ^ PICT_FRAME);
  2394. } else if(s->avctx->debug&FF_DEBUG_MMCO)
  2395. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  2396. break;
  2397. case MMCO_LONG:
  2398. // Comment below left from previous code as it is an interresting note.
  2399. /* First field in pair is in short term list or
  2400. * at a different long term index.
  2401. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  2402. * Report the problem and keep the pair where it is,
  2403. * and mark this field valid.
  2404. */
  2405. if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
  2406. remove_long(h, mmco[i].long_arg, 0);
  2407. h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
  2408. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  2409. h->long_ref_count++;
  2410. }
  2411. s->current_picture_ptr->reference |= s->picture_structure;
  2412. current_ref_assigned=1;
  2413. break;
  2414. case MMCO_SET_MAX_LONG:
  2415. assert(mmco[i].long_arg <= 16);
  2416. // just remove the long term which index is greater than new max
  2417. for(j = mmco[i].long_arg; j<16; j++){
  2418. remove_long(h, j, 0);
  2419. }
  2420. break;
  2421. case MMCO_RESET:
  2422. while(h->short_ref_count){
  2423. remove_short(h, h->short_ref[0]->frame_num, 0);
  2424. }
  2425. for(j = 0; j < 16; j++) {
  2426. remove_long(h, j, 0);
  2427. }
  2428. s->current_picture_ptr->poc=
  2429. s->current_picture_ptr->field_poc[0]=
  2430. s->current_picture_ptr->field_poc[1]=
  2431. h->poc_lsb=
  2432. h->poc_msb=
  2433. h->frame_num=
  2434. s->current_picture_ptr->frame_num= 0;
  2435. s->current_picture_ptr->mmco_reset=1;
  2436. break;
  2437. default: assert(0);
  2438. }
  2439. }
  2440. if (!current_ref_assigned) {
  2441. /* Second field of complementary field pair; the first field of
  2442. * which is already referenced. If short referenced, it
  2443. * should be first entry in short_ref. If not, it must exist
  2444. * in long_ref; trying to put it on the short list here is an
  2445. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  2446. */
  2447. if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
  2448. /* Just mark the second field valid */
  2449. s->current_picture_ptr->reference = PICT_FRAME;
  2450. } else if (s->current_picture_ptr->long_ref) {
  2451. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
  2452. "assignment for second field "
  2453. "in complementary field pair "
  2454. "(first field is long term)\n");
  2455. } else {
  2456. pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
  2457. if(pic){
  2458. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  2459. }
  2460. if(h->short_ref_count)
  2461. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  2462. h->short_ref[0]= s->current_picture_ptr;
  2463. h->short_ref_count++;
  2464. s->current_picture_ptr->reference |= s->picture_structure;
  2465. }
  2466. }
  2467. if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
  2468. /* We have too many reference frames, probably due to corrupted
  2469. * stream. Need to discard one frame. Prevents overrun of the
  2470. * short_ref and long_ref buffers.
  2471. */
  2472. av_log(h->s.avctx, AV_LOG_ERROR,
  2473. "number of reference frames exceeds max (probably "
  2474. "corrupt input), discarding one\n");
  2475. if (h->long_ref_count && !h->short_ref_count) {
  2476. for (i = 0; i < 16; ++i)
  2477. if (h->long_ref[i])
  2478. break;
  2479. assert(i < 16);
  2480. remove_long(h, i, 0);
  2481. } else {
  2482. pic = h->short_ref[h->short_ref_count - 1];
  2483. remove_short(h, pic->frame_num, 0);
  2484. }
  2485. }
  2486. print_short_term(h);
  2487. print_long_term(h);
  2488. return 0;
  2489. }
  2490. static int decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
  2491. MpegEncContext * const s = &h->s;
  2492. int i;
  2493. h->mmco_index= 0;
  2494. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  2495. s->broken_link= get_bits1(gb) -1;
  2496. if(get_bits1(gb)){
  2497. h->mmco[0].opcode= MMCO_LONG;
  2498. h->mmco[0].long_arg= 0;
  2499. h->mmco_index= 1;
  2500. }
  2501. }else{
  2502. if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
  2503. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  2504. MMCOOpcode opcode= get_ue_golomb_31(gb);
  2505. h->mmco[i].opcode= opcode;
  2506. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  2507. h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
  2508. /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
  2509. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  2510. return -1;
  2511. }*/
  2512. }
  2513. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  2514. unsigned int long_arg= get_ue_golomb_31(gb);
  2515. if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
  2516. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  2517. return -1;
  2518. }
  2519. h->mmco[i].long_arg= long_arg;
  2520. }
  2521. if(opcode > (unsigned)MMCO_LONG){
  2522. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  2523. return -1;
  2524. }
  2525. if(opcode == MMCO_END)
  2526. break;
  2527. }
  2528. h->mmco_index= i;
  2529. }else{
  2530. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  2531. if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
  2532. !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
  2533. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  2534. h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  2535. h->mmco_index= 1;
  2536. if (FIELD_PICTURE) {
  2537. h->mmco[0].short_pic_num *= 2;
  2538. h->mmco[1].opcode= MMCO_SHORT2UNUSED;
  2539. h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
  2540. h->mmco_index= 2;
  2541. }
  2542. }
  2543. }
  2544. }
  2545. return 0;
  2546. }
  2547. static int init_poc(H264Context *h){
  2548. MpegEncContext * const s = &h->s;
  2549. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2550. int field_poc[2];
  2551. Picture *cur = s->current_picture_ptr;
  2552. h->frame_num_offset= h->prev_frame_num_offset;
  2553. if(h->frame_num < h->prev_frame_num)
  2554. h->frame_num_offset += max_frame_num;
  2555. if(h->sps.poc_type==0){
  2556. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2557. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  2558. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2559. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2560. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2561. else
  2562. h->poc_msb = h->prev_poc_msb;
  2563. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2564. field_poc[0] =
  2565. field_poc[1] = h->poc_msb + h->poc_lsb;
  2566. if(s->picture_structure == PICT_FRAME)
  2567. field_poc[1] += h->delta_poc_bottom;
  2568. }else if(h->sps.poc_type==1){
  2569. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2570. int i;
  2571. if(h->sps.poc_cycle_length != 0)
  2572. abs_frame_num = h->frame_num_offset + h->frame_num;
  2573. else
  2574. abs_frame_num = 0;
  2575. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2576. abs_frame_num--;
  2577. expected_delta_per_poc_cycle = 0;
  2578. for(i=0; i < h->sps.poc_cycle_length; i++)
  2579. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2580. if(abs_frame_num > 0){
  2581. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2582. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2583. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2584. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2585. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2586. } else
  2587. expectedpoc = 0;
  2588. if(h->nal_ref_idc == 0)
  2589. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2590. field_poc[0] = expectedpoc + h->delta_poc[0];
  2591. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2592. if(s->picture_structure == PICT_FRAME)
  2593. field_poc[1] += h->delta_poc[1];
  2594. }else{
  2595. int poc= 2*(h->frame_num_offset + h->frame_num);
  2596. if(!h->nal_ref_idc)
  2597. poc--;
  2598. field_poc[0]= poc;
  2599. field_poc[1]= poc;
  2600. }
  2601. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2602. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2603. if(s->picture_structure != PICT_TOP_FIELD)
  2604. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2605. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  2606. return 0;
  2607. }
  2608. /**
  2609. * initialize scan tables
  2610. */
  2611. static void init_scan_tables(H264Context *h){
  2612. MpegEncContext * const s = &h->s;
  2613. int i;
  2614. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  2615. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  2616. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  2617. }else{
  2618. for(i=0; i<16; i++){
  2619. #define T(x) (x>>2) | ((x<<2) & 0xF)
  2620. h->zigzag_scan[i] = T(zigzag_scan[i]);
  2621. h-> field_scan[i] = T( field_scan[i]);
  2622. #undef T
  2623. }
  2624. }
  2625. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  2626. memcpy(h->zigzag_scan8x8, ff_zigzag_direct, 64*sizeof(uint8_t));
  2627. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  2628. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  2629. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  2630. }else{
  2631. for(i=0; i<64; i++){
  2632. #define T(x) (x>>3) | ((x&7)<<3)
  2633. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  2634. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  2635. h->field_scan8x8[i] = T(field_scan8x8[i]);
  2636. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  2637. #undef T
  2638. }
  2639. }
  2640. if(h->sps.transform_bypass){ //FIXME same ugly
  2641. h->zigzag_scan_q0 = zigzag_scan;
  2642. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  2643. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  2644. h->field_scan_q0 = field_scan;
  2645. h->field_scan8x8_q0 = field_scan8x8;
  2646. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  2647. }else{
  2648. h->zigzag_scan_q0 = h->zigzag_scan;
  2649. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  2650. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  2651. h->field_scan_q0 = h->field_scan;
  2652. h->field_scan8x8_q0 = h->field_scan8x8;
  2653. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  2654. }
  2655. }
  2656. static void field_end(H264Context *h){
  2657. MpegEncContext * const s = &h->s;
  2658. AVCodecContext * const avctx= s->avctx;
  2659. s->mb_y= 0;
  2660. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  2661. s->current_picture_ptr->pict_type= s->pict_type;
  2662. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2663. ff_vdpau_h264_set_reference_frames(s);
  2664. if(!s->dropable) {
  2665. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2666. h->prev_poc_msb= h->poc_msb;
  2667. h->prev_poc_lsb= h->poc_lsb;
  2668. }
  2669. h->prev_frame_num_offset= h->frame_num_offset;
  2670. h->prev_frame_num= h->frame_num;
  2671. if (avctx->hwaccel) {
  2672. if (avctx->hwaccel->end_frame(avctx) < 0)
  2673. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  2674. }
  2675. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2676. ff_vdpau_h264_picture_complete(s);
  2677. /*
  2678. * FIXME: Error handling code does not seem to support interlaced
  2679. * when slices span multiple rows
  2680. * The ff_er_add_slice calls don't work right for bottom
  2681. * fields; they cause massive erroneous error concealing
  2682. * Error marking covers both fields (top and bottom).
  2683. * This causes a mismatched s->error_count
  2684. * and a bad error table. Further, the error count goes to
  2685. * INT_MAX when called for bottom field, because mb_y is
  2686. * past end by one (callers fault) and resync_mb_y != 0
  2687. * causes problems for the first MB line, too.
  2688. */
  2689. if (!FIELD_PICTURE)
  2690. ff_er_frame_end(s);
  2691. MPV_frame_end(s);
  2692. h->current_slice=0;
  2693. }
  2694. /**
  2695. * Replicates H264 "master" context to thread contexts.
  2696. */
  2697. static void clone_slice(H264Context *dst, H264Context *src)
  2698. {
  2699. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2700. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  2701. dst->s.current_picture = src->s.current_picture;
  2702. dst->s.linesize = src->s.linesize;
  2703. dst->s.uvlinesize = src->s.uvlinesize;
  2704. dst->s.first_field = src->s.first_field;
  2705. dst->prev_poc_msb = src->prev_poc_msb;
  2706. dst->prev_poc_lsb = src->prev_poc_lsb;
  2707. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2708. dst->prev_frame_num = src->prev_frame_num;
  2709. dst->short_ref_count = src->short_ref_count;
  2710. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2711. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2712. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2713. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  2714. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2715. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2716. }
  2717. /**
  2718. * decodes a slice header.
  2719. * This will also call MPV_common_init() and frame_start() as needed.
  2720. *
  2721. * @param h h264context
  2722. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  2723. *
  2724. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  2725. */
  2726. static int decode_slice_header(H264Context *h, H264Context *h0){
  2727. MpegEncContext * const s = &h->s;
  2728. MpegEncContext * const s0 = &h0->s;
  2729. unsigned int first_mb_in_slice;
  2730. unsigned int pps_id;
  2731. int num_ref_idx_active_override_flag;
  2732. unsigned int slice_type, tmp, i, j;
  2733. int default_ref_list_done = 0;
  2734. int last_pic_structure;
  2735. s->dropable= h->nal_ref_idc == 0;
  2736. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  2737. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  2738. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  2739. }else{
  2740. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  2741. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  2742. }
  2743. first_mb_in_slice= get_ue_golomb(&s->gb);
  2744. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  2745. if(h0->current_slice && FIELD_PICTURE){
  2746. field_end(h);
  2747. }
  2748. h0->current_slice = 0;
  2749. if (!s0->first_field)
  2750. s->current_picture_ptr= NULL;
  2751. }
  2752. slice_type= get_ue_golomb_31(&s->gb);
  2753. if(slice_type > 9){
  2754. 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);
  2755. return -1;
  2756. }
  2757. if(slice_type > 4){
  2758. slice_type -= 5;
  2759. h->slice_type_fixed=1;
  2760. }else
  2761. h->slice_type_fixed=0;
  2762. slice_type= golomb_to_pict_type[ slice_type ];
  2763. if (slice_type == FF_I_TYPE
  2764. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  2765. default_ref_list_done = 1;
  2766. }
  2767. h->slice_type= slice_type;
  2768. h->slice_type_nos= slice_type & 3;
  2769. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  2770. if (s->pict_type == FF_B_TYPE && s0->last_picture_ptr == NULL) {
  2771. av_log(h->s.avctx, AV_LOG_ERROR,
  2772. "B picture before any references, skipping\n");
  2773. return -1;
  2774. }
  2775. pps_id= get_ue_golomb(&s->gb);
  2776. if(pps_id>=MAX_PPS_COUNT){
  2777. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  2778. return -1;
  2779. }
  2780. if(!h0->pps_buffers[pps_id]) {
  2781. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  2782. return -1;
  2783. }
  2784. h->pps= *h0->pps_buffers[pps_id];
  2785. if(!h0->sps_buffers[h->pps.sps_id]) {
  2786. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  2787. return -1;
  2788. }
  2789. h->sps = *h0->sps_buffers[h->pps.sps_id];
  2790. if(h == h0 && h->dequant_coeff_pps != pps_id){
  2791. h->dequant_coeff_pps = pps_id;
  2792. init_dequant_tables(h);
  2793. }
  2794. s->mb_width= h->sps.mb_width;
  2795. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  2796. h->b_stride= s->mb_width*4;
  2797. h->b8_stride= s->mb_width*2;
  2798. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  2799. if(h->sps.frame_mbs_only_flag)
  2800. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  2801. else
  2802. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3);
  2803. if (s->context_initialized
  2804. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  2805. if(h != h0)
  2806. return -1; // width / height changed during parallelized decoding
  2807. free_tables(h);
  2808. flush_dpb(s->avctx);
  2809. MPV_common_end(s);
  2810. }
  2811. if (!s->context_initialized) {
  2812. if(h != h0)
  2813. return -1; // we cant (re-)initialize context during parallel decoding
  2814. avcodec_set_dimensions(s->avctx, s->width, s->height);
  2815. s->avctx->sample_aspect_ratio= h->sps.sar;
  2816. if(!s->avctx->sample_aspect_ratio.den)
  2817. s->avctx->sample_aspect_ratio.den = 1;
  2818. if(h->sps.video_signal_type_present_flag){
  2819. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  2820. if(h->sps.colour_description_present_flag){
  2821. s->avctx->color_primaries = h->sps.color_primaries;
  2822. s->avctx->color_trc = h->sps.color_trc;
  2823. s->avctx->colorspace = h->sps.colorspace;
  2824. }
  2825. }
  2826. if(h->sps.timing_info_present_flag){
  2827. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick, h->sps.time_scale};
  2828. if(h->x264_build > 0 && h->x264_build < 44)
  2829. s->avctx->time_base.den *= 2;
  2830. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  2831. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  2832. }
  2833. s->avctx->pix_fmt = s->avctx->get_format(s->avctx, s->avctx->codec->pix_fmts);
  2834. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  2835. if (MPV_common_init(s) < 0)
  2836. return -1;
  2837. s->first_field = 0;
  2838. h->prev_interlaced_frame = 1;
  2839. init_scan_tables(h);
  2840. ff_h264_alloc_tables(h);
  2841. for(i = 1; i < s->avctx->thread_count; i++) {
  2842. H264Context *c;
  2843. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  2844. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  2845. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  2846. c->sps = h->sps;
  2847. c->pps = h->pps;
  2848. init_scan_tables(c);
  2849. clone_tables(c, h);
  2850. }
  2851. for(i = 0; i < s->avctx->thread_count; i++)
  2852. if(context_init(h->thread_context[i]) < 0)
  2853. return -1;
  2854. }
  2855. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2856. h->mb_mbaff = 0;
  2857. h->mb_aff_frame = 0;
  2858. last_pic_structure = s0->picture_structure;
  2859. if(h->sps.frame_mbs_only_flag){
  2860. s->picture_structure= PICT_FRAME;
  2861. }else{
  2862. if(get_bits1(&s->gb)) { //field_pic_flag
  2863. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2864. } else {
  2865. s->picture_structure= PICT_FRAME;
  2866. h->mb_aff_frame = h->sps.mb_aff;
  2867. }
  2868. }
  2869. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  2870. if(h0->current_slice == 0){
  2871. while(h->frame_num != h->prev_frame_num &&
  2872. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  2873. av_log(NULL, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  2874. if (ff_h264_frame_start(h) < 0)
  2875. return -1;
  2876. h->prev_frame_num++;
  2877. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  2878. s->current_picture_ptr->frame_num= h->prev_frame_num;
  2879. execute_ref_pic_marking(h, NULL, 0);
  2880. }
  2881. /* See if we have a decoded first field looking for a pair... */
  2882. if (s0->first_field) {
  2883. assert(s0->current_picture_ptr);
  2884. assert(s0->current_picture_ptr->data[0]);
  2885. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  2886. /* figure out if we have a complementary field pair */
  2887. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2888. /*
  2889. * Previous field is unmatched. Don't display it, but let it
  2890. * remain for reference if marked as such.
  2891. */
  2892. s0->current_picture_ptr = NULL;
  2893. s0->first_field = FIELD_PICTURE;
  2894. } else {
  2895. if (h->nal_ref_idc &&
  2896. s0->current_picture_ptr->reference &&
  2897. s0->current_picture_ptr->frame_num != h->frame_num) {
  2898. /*
  2899. * This and previous field were reference, but had
  2900. * different frame_nums. Consider this field first in
  2901. * pair. Throw away previous field except for reference
  2902. * purposes.
  2903. */
  2904. s0->first_field = 1;
  2905. s0->current_picture_ptr = NULL;
  2906. } else {
  2907. /* Second field in complementary pair */
  2908. s0->first_field = 0;
  2909. }
  2910. }
  2911. } else {
  2912. /* Frame or first field in a potentially complementary pair */
  2913. assert(!s0->current_picture_ptr);
  2914. s0->first_field = FIELD_PICTURE;
  2915. }
  2916. if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
  2917. s0->first_field = 0;
  2918. return -1;
  2919. }
  2920. }
  2921. if(h != h0)
  2922. clone_slice(h, h0);
  2923. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  2924. assert(s->mb_num == s->mb_width * s->mb_height);
  2925. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  2926. first_mb_in_slice >= s->mb_num){
  2927. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  2928. return -1;
  2929. }
  2930. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  2931. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  2932. if (s->picture_structure == PICT_BOTTOM_FIELD)
  2933. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  2934. assert(s->mb_y < s->mb_height);
  2935. if(s->picture_structure==PICT_FRAME){
  2936. h->curr_pic_num= h->frame_num;
  2937. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2938. }else{
  2939. h->curr_pic_num= 2*h->frame_num + 1;
  2940. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2941. }
  2942. if(h->nal_unit_type == NAL_IDR_SLICE){
  2943. get_ue_golomb(&s->gb); /* idr_pic_id */
  2944. }
  2945. if(h->sps.poc_type==0){
  2946. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2947. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2948. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2949. }
  2950. }
  2951. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2952. h->delta_poc[0]= get_se_golomb(&s->gb);
  2953. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2954. h->delta_poc[1]= get_se_golomb(&s->gb);
  2955. }
  2956. init_poc(h);
  2957. if(h->pps.redundant_pic_cnt_present){
  2958. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2959. }
  2960. //set defaults, might be overridden a few lines later
  2961. h->ref_count[0]= h->pps.ref_count[0];
  2962. h->ref_count[1]= h->pps.ref_count[1];
  2963. if(h->slice_type_nos != FF_I_TYPE){
  2964. if(h->slice_type_nos == FF_B_TYPE){
  2965. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2966. }
  2967. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2968. if(num_ref_idx_active_override_flag){
  2969. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2970. if(h->slice_type_nos==FF_B_TYPE)
  2971. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2972. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  2973. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  2974. h->ref_count[0]= h->ref_count[1]= 1;
  2975. return -1;
  2976. }
  2977. }
  2978. if(h->slice_type_nos == FF_B_TYPE)
  2979. h->list_count= 2;
  2980. else
  2981. h->list_count= 1;
  2982. }else
  2983. h->list_count= 0;
  2984. if(!default_ref_list_done){
  2985. fill_default_ref_list(h);
  2986. }
  2987. if(h->slice_type_nos!=FF_I_TYPE && decode_ref_pic_list_reordering(h) < 0)
  2988. return -1;
  2989. if(h->slice_type_nos!=FF_I_TYPE){
  2990. s->last_picture_ptr= &h->ref_list[0][0];
  2991. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  2992. }
  2993. if(h->slice_type_nos==FF_B_TYPE){
  2994. s->next_picture_ptr= &h->ref_list[1][0];
  2995. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  2996. }
  2997. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  2998. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  2999. pred_weight_table(h);
  3000. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE)
  3001. implicit_weight_table(h);
  3002. else {
  3003. h->use_weight = 0;
  3004. for (i = 0; i < 2; i++) {
  3005. h->luma_weight_flag[i] = 0;
  3006. h->chroma_weight_flag[i] = 0;
  3007. }
  3008. }
  3009. if(h->nal_ref_idc)
  3010. decode_ref_pic_marking(h0, &s->gb);
  3011. if(FRAME_MBAFF)
  3012. fill_mbaff_ref_list(h);
  3013. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  3014. ff_h264_direct_dist_scale_factor(h);
  3015. ff_h264_direct_ref_list_init(h);
  3016. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  3017. tmp = get_ue_golomb_31(&s->gb);
  3018. if(tmp > 2){
  3019. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3020. return -1;
  3021. }
  3022. h->cabac_init_idc= tmp;
  3023. }
  3024. h->last_qscale_diff = 0;
  3025. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  3026. if(tmp>51){
  3027. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3028. return -1;
  3029. }
  3030. s->qscale= tmp;
  3031. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  3032. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  3033. //FIXME qscale / qp ... stuff
  3034. if(h->slice_type == FF_SP_TYPE){
  3035. get_bits1(&s->gb); /* sp_for_switch_flag */
  3036. }
  3037. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  3038. get_se_golomb(&s->gb); /* slice_qs_delta */
  3039. }
  3040. h->deblocking_filter = 1;
  3041. h->slice_alpha_c0_offset = 0;
  3042. h->slice_beta_offset = 0;
  3043. if( h->pps.deblocking_filter_parameters_present ) {
  3044. tmp= get_ue_golomb_31(&s->gb);
  3045. if(tmp > 2){
  3046. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  3047. return -1;
  3048. }
  3049. h->deblocking_filter= tmp;
  3050. if(h->deblocking_filter < 2)
  3051. h->deblocking_filter^= 1; // 1<->0
  3052. if( h->deblocking_filter ) {
  3053. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3054. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3055. }
  3056. }
  3057. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  3058. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  3059. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  3060. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  3061. h->deblocking_filter= 0;
  3062. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  3063. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  3064. /* Cheat slightly for speed:
  3065. Do not bother to deblock across slices. */
  3066. h->deblocking_filter = 2;
  3067. } else {
  3068. h0->max_contexts = 1;
  3069. if(!h0->single_decode_warning) {
  3070. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  3071. h0->single_decode_warning = 1;
  3072. }
  3073. if(h != h0)
  3074. return 1; // deblocking switched inside frame
  3075. }
  3076. }
  3077. #if 0 //FMO
  3078. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3079. slice_group_change_cycle= get_bits(&s->gb, ?);
  3080. #endif
  3081. h0->last_slice_type = slice_type;
  3082. h->slice_num = ++h0->current_slice;
  3083. if(h->slice_num >= MAX_SLICES){
  3084. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  3085. }
  3086. for(j=0; j<2; j++){
  3087. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  3088. ref2frm[0]=
  3089. ref2frm[1]= -1;
  3090. for(i=0; i<16; i++)
  3091. ref2frm[i+2]= 4*h->ref_list[j][i].frame_num
  3092. +(h->ref_list[j][i].reference&3);
  3093. ref2frm[18+0]=
  3094. ref2frm[18+1]= -1;
  3095. for(i=16; i<48; i++)
  3096. ref2frm[i+4]= 4*h->ref_list[j][i].frame_num
  3097. +(h->ref_list[j][i].reference&3);
  3098. }
  3099. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  3100. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  3101. s->avctx->refs= h->sps.ref_frame_count;
  3102. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3103. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
  3104. h->slice_num,
  3105. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3106. first_mb_in_slice,
  3107. av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  3108. pps_id, h->frame_num,
  3109. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3110. h->ref_count[0], h->ref_count[1],
  3111. s->qscale,
  3112. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3113. h->use_weight,
  3114. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  3115. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  3116. );
  3117. }
  3118. return 0;
  3119. }
  3120. int ff_h264_get_slice_type(H264Context *h)
  3121. {
  3122. switch (h->slice_type) {
  3123. case FF_P_TYPE: return 0;
  3124. case FF_B_TYPE: return 1;
  3125. case FF_I_TYPE: return 2;
  3126. case FF_SP_TYPE: return 3;
  3127. case FF_SI_TYPE: return 4;
  3128. default: return -1;
  3129. }
  3130. }
  3131. /**
  3132. *
  3133. */
  3134. static inline int get_level_prefix(GetBitContext *gb){
  3135. unsigned int buf;
  3136. int log;
  3137. OPEN_READER(re, gb);
  3138. UPDATE_CACHE(re, gb);
  3139. buf=GET_CACHE(re, gb);
  3140. log= 32 - av_log2(buf);
  3141. #ifdef TRACE
  3142. print_bin(buf>>(32-log), log);
  3143. 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__);
  3144. #endif
  3145. LAST_SKIP_BITS(re, gb, log);
  3146. CLOSE_READER(re, gb);
  3147. return log-1;
  3148. }
  3149. static inline int get_dct8x8_allowed(H264Context *h){
  3150. if(h->sps.direct_8x8_inference_flag)
  3151. return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
  3152. else
  3153. return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
  3154. }
  3155. /**
  3156. * decodes a residual block.
  3157. * @param n block index
  3158. * @param scantable scantable
  3159. * @param max_coeff number of coefficients in the block
  3160. * @return <0 if an error occurred
  3161. */
  3162. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  3163. MpegEncContext * const s = &h->s;
  3164. 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};
  3165. int level[16];
  3166. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  3167. //FIXME put trailing_onex into the context
  3168. if(n == CHROMA_DC_BLOCK_INDEX){
  3169. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  3170. total_coeff= coeff_token>>2;
  3171. }else{
  3172. if(n == LUMA_DC_BLOCK_INDEX){
  3173. total_coeff= pred_non_zero_count(h, 0);
  3174. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3175. total_coeff= coeff_token>>2;
  3176. }else{
  3177. total_coeff= pred_non_zero_count(h, n);
  3178. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3179. total_coeff= coeff_token>>2;
  3180. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  3181. }
  3182. }
  3183. //FIXME set last_non_zero?
  3184. if(total_coeff==0)
  3185. return 0;
  3186. if(total_coeff > (unsigned)max_coeff) {
  3187. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  3188. return -1;
  3189. }
  3190. trailing_ones= coeff_token&3;
  3191. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  3192. assert(total_coeff<=16);
  3193. i = show_bits(gb, 3);
  3194. skip_bits(gb, trailing_ones);
  3195. level[0] = 1-((i&4)>>1);
  3196. level[1] = 1-((i&2) );
  3197. level[2] = 1-((i&1)<<1);
  3198. if(trailing_ones<total_coeff) {
  3199. int mask, prefix;
  3200. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  3201. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  3202. int level_code= cavlc_level_tab[suffix_length][bitsi][0];
  3203. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  3204. if(level_code >= 100){
  3205. prefix= level_code - 100;
  3206. if(prefix == LEVEL_TAB_BITS)
  3207. prefix += get_level_prefix(gb);
  3208. //first coefficient has suffix_length equal to 0 or 1
  3209. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  3210. if(suffix_length)
  3211. level_code= (prefix<<1) + get_bits1(gb); //part
  3212. else
  3213. level_code= prefix; //part
  3214. }else if(prefix==14){
  3215. if(suffix_length)
  3216. level_code= (prefix<<1) + get_bits1(gb); //part
  3217. else
  3218. level_code= prefix + get_bits(gb, 4); //part
  3219. }else{
  3220. level_code= 30 + get_bits(gb, prefix-3); //part
  3221. if(prefix>=16)
  3222. level_code += (1<<(prefix-3))-4096;
  3223. }
  3224. if(trailing_ones < 3) level_code += 2;
  3225. suffix_length = 2;
  3226. mask= -(level_code&1);
  3227. level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask;
  3228. }else{
  3229. if(trailing_ones < 3) level_code += (level_code>>31)|1;
  3230. suffix_length = 1;
  3231. if(level_code + 3U > 6U)
  3232. suffix_length++;
  3233. level[trailing_ones]= level_code;
  3234. }
  3235. //remaining coefficients have suffix_length > 0
  3236. for(i=trailing_ones+1;i<total_coeff;i++) {
  3237. static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX };
  3238. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  3239. level_code= cavlc_level_tab[suffix_length][bitsi][0];
  3240. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  3241. if(level_code >= 100){
  3242. prefix= level_code - 100;
  3243. if(prefix == LEVEL_TAB_BITS){
  3244. prefix += get_level_prefix(gb);
  3245. }
  3246. if(prefix<15){
  3247. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  3248. }else{
  3249. level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
  3250. if(prefix>=16)
  3251. level_code += (1<<(prefix-3))-4096;
  3252. }
  3253. mask= -(level_code&1);
  3254. level_code= (((2+level_code)>>1) ^ mask) - mask;
  3255. }
  3256. level[i]= level_code;
  3257. if(suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length])
  3258. suffix_length++;
  3259. }
  3260. }
  3261. if(total_coeff == max_coeff)
  3262. zeros_left=0;
  3263. else{
  3264. if(n == CHROMA_DC_BLOCK_INDEX)
  3265. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  3266. else
  3267. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  3268. }
  3269. coeff_num = zeros_left + total_coeff - 1;
  3270. j = scantable[coeff_num];
  3271. if(n > 24){
  3272. block[j] = level[0];
  3273. for(i=1;i<total_coeff;i++) {
  3274. if(zeros_left <= 0)
  3275. run_before = 0;
  3276. else if(zeros_left < 7){
  3277. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3278. }else{
  3279. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3280. }
  3281. zeros_left -= run_before;
  3282. coeff_num -= 1 + run_before;
  3283. j= scantable[ coeff_num ];
  3284. block[j]= level[i];
  3285. }
  3286. }else{
  3287. block[j] = (level[0] * qmul[j] + 32)>>6;
  3288. for(i=1;i<total_coeff;i++) {
  3289. if(zeros_left <= 0)
  3290. run_before = 0;
  3291. else if(zeros_left < 7){
  3292. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3293. }else{
  3294. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3295. }
  3296. zeros_left -= run_before;
  3297. coeff_num -= 1 + run_before;
  3298. j= scantable[ coeff_num ];
  3299. block[j]= (level[i] * qmul[j] + 32)>>6;
  3300. }
  3301. }
  3302. if(zeros_left<0){
  3303. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  3304. return -1;
  3305. }
  3306. return 0;
  3307. }
  3308. static void predict_field_decoding_flag(H264Context *h){
  3309. MpegEncContext * const s = &h->s;
  3310. const int mb_xy= h->mb_xy;
  3311. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  3312. ? s->current_picture.mb_type[mb_xy-1]
  3313. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  3314. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  3315. : 0;
  3316. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  3317. }
  3318. /**
  3319. * decodes a P_SKIP or B_SKIP macroblock
  3320. */
  3321. static void decode_mb_skip(H264Context *h){
  3322. MpegEncContext * const s = &h->s;
  3323. const int mb_xy= h->mb_xy;
  3324. int mb_type=0;
  3325. memset(h->non_zero_count[mb_xy], 0, 16);
  3326. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  3327. if(MB_FIELD)
  3328. mb_type|= MB_TYPE_INTERLACED;
  3329. if( h->slice_type_nos == FF_B_TYPE )
  3330. {
  3331. // just for fill_caches. pred_direct_motion will set the real mb_type
  3332. mb_type|= MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  3333. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3334. ff_h264_pred_direct_motion(h, &mb_type);
  3335. mb_type|= MB_TYPE_SKIP;
  3336. }
  3337. else
  3338. {
  3339. int mx, my;
  3340. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  3341. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3342. pred_pskip_motion(h, &mx, &my);
  3343. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  3344. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  3345. }
  3346. write_back_motion(h, mb_type);
  3347. s->current_picture.mb_type[mb_xy]= mb_type;
  3348. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3349. h->slice_table[ mb_xy ]= h->slice_num;
  3350. h->prev_mb_skipped= 1;
  3351. }
  3352. /**
  3353. * decodes a macroblock
  3354. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  3355. */
  3356. static int decode_mb_cavlc(H264Context *h){
  3357. MpegEncContext * const s = &h->s;
  3358. int mb_xy;
  3359. int partition_count;
  3360. unsigned int mb_type, cbp;
  3361. int dct8x8_allowed= h->pps.transform_8x8_mode;
  3362. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3363. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  3364. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  3365. down the code */
  3366. if(h->slice_type_nos != FF_I_TYPE){
  3367. if(s->mb_skip_run==-1)
  3368. s->mb_skip_run= get_ue_golomb(&s->gb);
  3369. if (s->mb_skip_run--) {
  3370. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  3371. if(s->mb_skip_run==0)
  3372. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  3373. else
  3374. predict_field_decoding_flag(h);
  3375. }
  3376. decode_mb_skip(h);
  3377. return 0;
  3378. }
  3379. }
  3380. if(FRAME_MBAFF){
  3381. if( (s->mb_y&1) == 0 )
  3382. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  3383. }
  3384. h->prev_mb_skipped= 0;
  3385. mb_type= get_ue_golomb(&s->gb);
  3386. if(h->slice_type_nos == FF_B_TYPE){
  3387. if(mb_type < 23){
  3388. partition_count= b_mb_type_info[mb_type].partition_count;
  3389. mb_type= b_mb_type_info[mb_type].type;
  3390. }else{
  3391. mb_type -= 23;
  3392. goto decode_intra_mb;
  3393. }
  3394. }else if(h->slice_type_nos == FF_P_TYPE){
  3395. if(mb_type < 5){
  3396. partition_count= p_mb_type_info[mb_type].partition_count;
  3397. mb_type= p_mb_type_info[mb_type].type;
  3398. }else{
  3399. mb_type -= 5;
  3400. goto decode_intra_mb;
  3401. }
  3402. }else{
  3403. assert(h->slice_type_nos == FF_I_TYPE);
  3404. if(h->slice_type == FF_SI_TYPE && mb_type)
  3405. mb_type--;
  3406. decode_intra_mb:
  3407. if(mb_type > 25){
  3408. 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);
  3409. return -1;
  3410. }
  3411. partition_count=0;
  3412. cbp= i_mb_type_info[mb_type].cbp;
  3413. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  3414. mb_type= i_mb_type_info[mb_type].type;
  3415. }
  3416. if(MB_FIELD)
  3417. mb_type |= MB_TYPE_INTERLACED;
  3418. h->slice_table[ mb_xy ]= h->slice_num;
  3419. if(IS_INTRA_PCM(mb_type)){
  3420. unsigned int x;
  3421. // We assume these blocks are very rare so we do not optimize it.
  3422. align_get_bits(&s->gb);
  3423. // The pixels are stored in the same order as levels in h->mb array.
  3424. for(x=0; x < (CHROMA ? 384 : 256); x++){
  3425. ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
  3426. }
  3427. // In deblocking, the quantizer is 0
  3428. s->current_picture.qscale_table[mb_xy]= 0;
  3429. // All coeffs are present
  3430. memset(h->non_zero_count[mb_xy], 16, 16);
  3431. s->current_picture.mb_type[mb_xy]= mb_type;
  3432. return 0;
  3433. }
  3434. if(MB_MBAFF){
  3435. h->ref_count[0] <<= 1;
  3436. h->ref_count[1] <<= 1;
  3437. }
  3438. fill_caches(h, mb_type, 0);
  3439. //mb_pred
  3440. if(IS_INTRA(mb_type)){
  3441. int pred_mode;
  3442. // init_top_left_availability(h);
  3443. if(IS_INTRA4x4(mb_type)){
  3444. int i;
  3445. int di = 1;
  3446. if(dct8x8_allowed && get_bits1(&s->gb)){
  3447. mb_type |= MB_TYPE_8x8DCT;
  3448. di = 4;
  3449. }
  3450. // fill_intra4x4_pred_table(h);
  3451. for(i=0; i<16; i+=di){
  3452. int mode= pred_intra_mode(h, i);
  3453. if(!get_bits1(&s->gb)){
  3454. const int rem_mode= get_bits(&s->gb, 3);
  3455. mode = rem_mode + (rem_mode >= mode);
  3456. }
  3457. if(di==4)
  3458. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  3459. else
  3460. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  3461. }
  3462. ff_h264_write_back_intra_pred_mode(h);
  3463. if( check_intra4x4_pred_mode(h) < 0)
  3464. return -1;
  3465. }else{
  3466. h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode(h, h->intra16x16_pred_mode);
  3467. if(h->intra16x16_pred_mode < 0)
  3468. return -1;
  3469. }
  3470. if(CHROMA){
  3471. pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb));
  3472. if(pred_mode < 0)
  3473. return -1;
  3474. h->chroma_pred_mode= pred_mode;
  3475. }
  3476. }else if(partition_count==4){
  3477. int i, j, sub_partition_count[4], list, ref[2][4];
  3478. if(h->slice_type_nos == FF_B_TYPE){
  3479. for(i=0; i<4; i++){
  3480. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  3481. if(h->sub_mb_type[i] >=13){
  3482. 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);
  3483. return -1;
  3484. }
  3485. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3486. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3487. }
  3488. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  3489. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  3490. ff_h264_pred_direct_motion(h, &mb_type);
  3491. h->ref_cache[0][scan8[4]] =
  3492. h->ref_cache[1][scan8[4]] =
  3493. h->ref_cache[0][scan8[12]] =
  3494. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  3495. }
  3496. }else{
  3497. assert(h->slice_type_nos == FF_P_TYPE); //FIXME SP correct ?
  3498. for(i=0; i<4; i++){
  3499. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  3500. if(h->sub_mb_type[i] >=4){
  3501. 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);
  3502. return -1;
  3503. }
  3504. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3505. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3506. }
  3507. }
  3508. for(list=0; list<h->list_count; list++){
  3509. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3510. for(i=0; i<4; i++){
  3511. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3512. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3513. unsigned int tmp;
  3514. if(ref_count == 1){
  3515. tmp= 0;
  3516. }else if(ref_count == 2){
  3517. tmp= get_bits1(&s->gb)^1;
  3518. }else{
  3519. tmp= get_ue_golomb_31(&s->gb);
  3520. if(tmp>=ref_count){
  3521. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  3522. return -1;
  3523. }
  3524. }
  3525. ref[list][i]= tmp;
  3526. }else{
  3527. //FIXME
  3528. ref[list][i] = -1;
  3529. }
  3530. }
  3531. }
  3532. if(dct8x8_allowed)
  3533. dct8x8_allowed = get_dct8x8_allowed(h);
  3534. for(list=0; list<h->list_count; list++){
  3535. for(i=0; i<4; i++){
  3536. if(IS_DIRECT(h->sub_mb_type[i])) {
  3537. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  3538. continue;
  3539. }
  3540. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  3541. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  3542. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3543. const int sub_mb_type= h->sub_mb_type[i];
  3544. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  3545. for(j=0; j<sub_partition_count[i]; j++){
  3546. int mx, my;
  3547. const int index= 4*i + block_width*j;
  3548. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  3549. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  3550. mx += get_se_golomb(&s->gb);
  3551. my += get_se_golomb(&s->gb);
  3552. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3553. if(IS_SUB_8X8(sub_mb_type)){
  3554. mv_cache[ 1 ][0]=
  3555. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  3556. mv_cache[ 1 ][1]=
  3557. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  3558. }else if(IS_SUB_8X4(sub_mb_type)){
  3559. mv_cache[ 1 ][0]= mx;
  3560. mv_cache[ 1 ][1]= my;
  3561. }else if(IS_SUB_4X8(sub_mb_type)){
  3562. mv_cache[ 8 ][0]= mx;
  3563. mv_cache[ 8 ][1]= my;
  3564. }
  3565. mv_cache[ 0 ][0]= mx;
  3566. mv_cache[ 0 ][1]= my;
  3567. }
  3568. }else{
  3569. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  3570. p[0] = p[1]=
  3571. p[8] = p[9]= 0;
  3572. }
  3573. }
  3574. }
  3575. }else if(IS_DIRECT(mb_type)){
  3576. ff_h264_pred_direct_motion(h, &mb_type);
  3577. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  3578. }else{
  3579. int list, mx, my, i;
  3580. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  3581. if(IS_16X16(mb_type)){
  3582. for(list=0; list<h->list_count; list++){
  3583. unsigned int val;
  3584. if(IS_DIR(mb_type, 0, list)){
  3585. if(h->ref_count[list]==1){
  3586. val= 0;
  3587. }else if(h->ref_count[list]==2){
  3588. val= get_bits1(&s->gb)^1;
  3589. }else{
  3590. val= get_ue_golomb_31(&s->gb);
  3591. if(val >= h->ref_count[list]){
  3592. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  3593. return -1;
  3594. }
  3595. }
  3596. }else
  3597. val= LIST_NOT_USED&0xFF;
  3598. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  3599. }
  3600. for(list=0; list<h->list_count; list++){
  3601. unsigned int val;
  3602. if(IS_DIR(mb_type, 0, list)){
  3603. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  3604. mx += get_se_golomb(&s->gb);
  3605. my += get_se_golomb(&s->gb);
  3606. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3607. val= pack16to32(mx,my);
  3608. }else
  3609. val=0;
  3610. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  3611. }
  3612. }
  3613. else if(IS_16X8(mb_type)){
  3614. for(list=0; list<h->list_count; list++){
  3615. for(i=0; i<2; i++){
  3616. unsigned int val;
  3617. if(IS_DIR(mb_type, i, list)){
  3618. if(h->ref_count[list] == 1){
  3619. val= 0;
  3620. }else if(h->ref_count[list] == 2){
  3621. val= get_bits1(&s->gb)^1;
  3622. }else{
  3623. val= get_ue_golomb_31(&s->gb);
  3624. if(val >= h->ref_count[list]){
  3625. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  3626. return -1;
  3627. }
  3628. }
  3629. }else
  3630. val= LIST_NOT_USED&0xFF;
  3631. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  3632. }
  3633. }
  3634. for(list=0; list<h->list_count; list++){
  3635. for(i=0; i<2; i++){
  3636. unsigned int val;
  3637. if(IS_DIR(mb_type, i, list)){
  3638. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  3639. mx += get_se_golomb(&s->gb);
  3640. my += get_se_golomb(&s->gb);
  3641. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3642. val= pack16to32(mx,my);
  3643. }else
  3644. val=0;
  3645. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  3646. }
  3647. }
  3648. }else{
  3649. assert(IS_8X16(mb_type));
  3650. for(list=0; list<h->list_count; list++){
  3651. for(i=0; i<2; i++){
  3652. unsigned int val;
  3653. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  3654. if(h->ref_count[list]==1){
  3655. val= 0;
  3656. }else if(h->ref_count[list]==2){
  3657. val= get_bits1(&s->gb)^1;
  3658. }else{
  3659. val= get_ue_golomb_31(&s->gb);
  3660. if(val >= h->ref_count[list]){
  3661. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  3662. return -1;
  3663. }
  3664. }
  3665. }else
  3666. val= LIST_NOT_USED&0xFF;
  3667. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  3668. }
  3669. }
  3670. for(list=0; list<h->list_count; list++){
  3671. for(i=0; i<2; i++){
  3672. unsigned int val;
  3673. if(IS_DIR(mb_type, i, list)){
  3674. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  3675. mx += get_se_golomb(&s->gb);
  3676. my += get_se_golomb(&s->gb);
  3677. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3678. val= pack16to32(mx,my);
  3679. }else
  3680. val=0;
  3681. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  3682. }
  3683. }
  3684. }
  3685. }
  3686. if(IS_INTER(mb_type))
  3687. write_back_motion(h, mb_type);
  3688. if(!IS_INTRA16x16(mb_type)){
  3689. cbp= get_ue_golomb(&s->gb);
  3690. if(cbp > 47){
  3691. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3692. return -1;
  3693. }
  3694. if(CHROMA){
  3695. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
  3696. else cbp= golomb_to_inter_cbp [cbp];
  3697. }else{
  3698. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
  3699. else cbp= golomb_to_inter_cbp_gray[cbp];
  3700. }
  3701. }
  3702. h->cbp = cbp;
  3703. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  3704. if(get_bits1(&s->gb)){
  3705. mb_type |= MB_TYPE_8x8DCT;
  3706. h->cbp_table[mb_xy]= cbp;
  3707. }
  3708. }
  3709. s->current_picture.mb_type[mb_xy]= mb_type;
  3710. if(cbp || IS_INTRA16x16(mb_type)){
  3711. int i8x8, i4x4, chroma_idx;
  3712. int dquant;
  3713. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3714. const uint8_t *scan, *scan8x8, *dc_scan;
  3715. // fill_non_zero_count_cache(h);
  3716. if(IS_INTERLACED(mb_type)){
  3717. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  3718. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  3719. dc_scan= luma_dc_field_scan;
  3720. }else{
  3721. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  3722. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  3723. dc_scan= luma_dc_zigzag_scan;
  3724. }
  3725. dquant= get_se_golomb(&s->gb);
  3726. if( dquant > 25 || dquant < -26 ){
  3727. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  3728. return -1;
  3729. }
  3730. s->qscale += dquant;
  3731. if(((unsigned)s->qscale) > 51){
  3732. if(s->qscale<0) s->qscale+= 52;
  3733. else s->qscale-= 52;
  3734. }
  3735. h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
  3736. h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
  3737. if(IS_INTRA16x16(mb_type)){
  3738. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  3739. return -1; //FIXME continue if partitioned and other return -1 too
  3740. }
  3741. assert((cbp&15) == 0 || (cbp&15) == 15);
  3742. if(cbp&15){
  3743. for(i8x8=0; i8x8<4; i8x8++){
  3744. for(i4x4=0; i4x4<4; i4x4++){
  3745. const int index= i4x4 + 4*i8x8;
  3746. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  3747. return -1;
  3748. }
  3749. }
  3750. }
  3751. }else{
  3752. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  3753. }
  3754. }else{
  3755. for(i8x8=0; i8x8<4; i8x8++){
  3756. if(cbp & (1<<i8x8)){
  3757. if(IS_8x8DCT(mb_type)){
  3758. DCTELEM *buf = &h->mb[64*i8x8];
  3759. uint8_t *nnz;
  3760. for(i4x4=0; i4x4<4; i4x4++){
  3761. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  3762. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  3763. return -1;
  3764. }
  3765. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3766. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  3767. }else{
  3768. for(i4x4=0; i4x4<4; i4x4++){
  3769. const int index= i4x4 + 4*i8x8;
  3770. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  3771. return -1;
  3772. }
  3773. }
  3774. }
  3775. }else{
  3776. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3777. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3778. }
  3779. }
  3780. }
  3781. if(cbp&0x30){
  3782. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  3783. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  3784. return -1;
  3785. }
  3786. }
  3787. if(cbp&0x20){
  3788. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  3789. const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
  3790. for(i4x4=0; i4x4<4; i4x4++){
  3791. const int index= 16 + 4*chroma_idx + i4x4;
  3792. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
  3793. return -1;
  3794. }
  3795. }
  3796. }
  3797. }else{
  3798. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3799. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3800. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3801. }
  3802. }else{
  3803. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3804. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  3805. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3806. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3807. }
  3808. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3809. write_back_non_zero_count(h);
  3810. if(MB_MBAFF){
  3811. h->ref_count[0] >>= 1;
  3812. h->ref_count[1] >>= 1;
  3813. }
  3814. return 0;
  3815. }
  3816. static int decode_cabac_field_decoding_flag(H264Context *h) {
  3817. MpegEncContext * const s = &h->s;
  3818. const int mb_x = s->mb_x;
  3819. const int mb_y = s->mb_y & ~1;
  3820. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  3821. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  3822. unsigned int ctx = 0;
  3823. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  3824. ctx += 1;
  3825. }
  3826. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  3827. ctx += 1;
  3828. }
  3829. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  3830. }
  3831. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  3832. uint8_t *state= &h->cabac_state[ctx_base];
  3833. int mb_type;
  3834. if(intra_slice){
  3835. MpegEncContext * const s = &h->s;
  3836. const int mba_xy = h->left_mb_xy[0];
  3837. const int mbb_xy = h->top_mb_xy;
  3838. int ctx=0;
  3839. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  3840. ctx++;
  3841. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  3842. ctx++;
  3843. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  3844. return 0; /* I4x4 */
  3845. state += 2;
  3846. }else{
  3847. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  3848. return 0; /* I4x4 */
  3849. }
  3850. if( get_cabac_terminate( &h->cabac ) )
  3851. return 25; /* PCM */
  3852. mb_type = 1; /* I16x16 */
  3853. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  3854. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  3855. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  3856. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  3857. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  3858. return mb_type;
  3859. }
  3860. static int decode_cabac_mb_type_b( H264Context *h ) {
  3861. MpegEncContext * const s = &h->s;
  3862. const int mba_xy = h->left_mb_xy[0];
  3863. const int mbb_xy = h->top_mb_xy;
  3864. int ctx = 0;
  3865. int bits;
  3866. assert(h->slice_type_nos == FF_B_TYPE);
  3867. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  3868. ctx++;
  3869. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  3870. ctx++;
  3871. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  3872. return 0; /* B_Direct_16x16 */
  3873. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  3874. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  3875. }
  3876. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  3877. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  3878. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  3879. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  3880. if( bits < 8 )
  3881. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  3882. else if( bits == 13 ) {
  3883. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  3884. } else if( bits == 14 )
  3885. return 11; /* B_L1_L0_8x16 */
  3886. else if( bits == 15 )
  3887. return 22; /* B_8x8 */
  3888. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  3889. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  3890. }
  3891. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  3892. MpegEncContext * const s = &h->s;
  3893. int mba_xy, mbb_xy;
  3894. int ctx = 0;
  3895. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  3896. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  3897. mba_xy = mb_xy - 1;
  3898. if( (mb_y&1)
  3899. && h->slice_table[mba_xy] == h->slice_num
  3900. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  3901. mba_xy += s->mb_stride;
  3902. if( MB_FIELD ){
  3903. mbb_xy = mb_xy - s->mb_stride;
  3904. if( !(mb_y&1)
  3905. && h->slice_table[mbb_xy] == h->slice_num
  3906. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  3907. mbb_xy -= s->mb_stride;
  3908. }else
  3909. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  3910. }else{
  3911. int mb_xy = h->mb_xy;
  3912. mba_xy = mb_xy - 1;
  3913. mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  3914. }
  3915. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  3916. ctx++;
  3917. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  3918. ctx++;
  3919. if( h->slice_type_nos == FF_B_TYPE )
  3920. ctx += 13;
  3921. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  3922. }
  3923. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  3924. int mode = 0;
  3925. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  3926. return pred_mode;
  3927. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  3928. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  3929. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  3930. if( mode >= pred_mode )
  3931. return mode + 1;
  3932. else
  3933. return mode;
  3934. }
  3935. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  3936. const int mba_xy = h->left_mb_xy[0];
  3937. const int mbb_xy = h->top_mb_xy;
  3938. int ctx = 0;
  3939. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  3940. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  3941. ctx++;
  3942. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  3943. ctx++;
  3944. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  3945. return 0;
  3946. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3947. return 1;
  3948. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3949. return 2;
  3950. else
  3951. return 3;
  3952. }
  3953. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  3954. int cbp_b, cbp_a, ctx, cbp = 0;
  3955. cbp_a = h->slice_table[h->left_mb_xy[0]] == h->slice_num ? h->left_cbp : -1;
  3956. cbp_b = h->slice_table[h->top_mb_xy] == h->slice_num ? h->top_cbp : -1;
  3957. ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04);
  3958. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]);
  3959. ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08);
  3960. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1;
  3961. ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01);
  3962. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2;
  3963. ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02);
  3964. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3;
  3965. return cbp;
  3966. }
  3967. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  3968. int ctx;
  3969. int cbp_a, cbp_b;
  3970. cbp_a = (h->left_cbp>>4)&0x03;
  3971. cbp_b = (h-> top_cbp>>4)&0x03;
  3972. ctx = 0;
  3973. if( cbp_a > 0 ) ctx++;
  3974. if( cbp_b > 0 ) ctx += 2;
  3975. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  3976. return 0;
  3977. ctx = 4;
  3978. if( cbp_a == 2 ) ctx++;
  3979. if( cbp_b == 2 ) ctx += 2;
  3980. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  3981. }
  3982. static int decode_cabac_mb_dqp( H264Context *h) {
  3983. int ctx= h->last_qscale_diff != 0;
  3984. int val = 0;
  3985. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  3986. ctx= 2+(ctx>>1);
  3987. val++;
  3988. if(val > 102) //prevent infinite loop
  3989. return INT_MIN;
  3990. }
  3991. if( val&0x01 )
  3992. return (val + 1)>>1 ;
  3993. else
  3994. return -((val + 1)>>1);
  3995. }
  3996. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  3997. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  3998. return 0; /* 8x8 */
  3999. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4000. return 1; /* 8x4 */
  4001. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4002. return 2; /* 4x8 */
  4003. return 3; /* 4x4 */
  4004. }
  4005. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4006. int type;
  4007. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4008. return 0; /* B_Direct_8x8 */
  4009. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4010. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4011. type = 3;
  4012. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4013. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4014. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4015. type += 4;
  4016. }
  4017. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4018. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4019. return type;
  4020. }
  4021. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4022. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4023. }
  4024. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4025. int refa = h->ref_cache[list][scan8[n] - 1];
  4026. int refb = h->ref_cache[list][scan8[n] - 8];
  4027. int ref = 0;
  4028. int ctx = 0;
  4029. if( h->slice_type_nos == FF_B_TYPE) {
  4030. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4031. ctx++;
  4032. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4033. ctx += 2;
  4034. } else {
  4035. if( refa > 0 )
  4036. ctx++;
  4037. if( refb > 0 )
  4038. ctx += 2;
  4039. }
  4040. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4041. ref++;
  4042. ctx = (ctx>>2)+4;
  4043. if(ref >= 32 /*h->ref_list[list]*/){
  4044. return -1;
  4045. }
  4046. }
  4047. return ref;
  4048. }
  4049. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4050. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4051. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4052. int ctxbase = (l == 0) ? 40 : 47;
  4053. int mvd;
  4054. int ctx = (amvd>2) + (amvd>32);
  4055. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4056. return 0;
  4057. mvd= 1;
  4058. ctx= 3;
  4059. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4060. mvd++;
  4061. if( ctx < 6 )
  4062. ctx++;
  4063. }
  4064. if( mvd >= 9 ) {
  4065. int k = 3;
  4066. while( get_cabac_bypass( &h->cabac ) ) {
  4067. mvd += 1 << k;
  4068. k++;
  4069. if(k>24){
  4070. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  4071. return INT_MIN;
  4072. }
  4073. }
  4074. while( k-- ) {
  4075. if( get_cabac_bypass( &h->cabac ) )
  4076. mvd += 1 << k;
  4077. }
  4078. }
  4079. return get_cabac_bypass_sign( &h->cabac, -mvd );
  4080. }
  4081. static av_always_inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx, int is_dc ) {
  4082. int nza, nzb;
  4083. int ctx = 0;
  4084. if( is_dc ) {
  4085. if( cat == 0 ) {
  4086. nza = h->left_cbp&0x100;
  4087. nzb = h-> top_cbp&0x100;
  4088. } else {
  4089. nza = (h->left_cbp>>(6+idx))&0x01;
  4090. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4091. }
  4092. } else {
  4093. assert(cat == 1 || cat == 2 || cat == 4);
  4094. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4095. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4096. }
  4097. if( nza > 0 )
  4098. ctx++;
  4099. if( nzb > 0 )
  4100. ctx += 2;
  4101. return ctx + 4 * cat;
  4102. }
  4103. DECLARE_ASM_CONST(1, uint8_t, last_coeff_flag_offset_8x8[63]) = {
  4104. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  4105. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  4106. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  4107. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  4108. };
  4109. 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 ) {
  4110. static const int significant_coeff_flag_offset[2][6] = {
  4111. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  4112. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  4113. };
  4114. static const int last_coeff_flag_offset[2][6] = {
  4115. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  4116. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  4117. };
  4118. static const int coeff_abs_level_m1_offset[6] = {
  4119. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  4120. };
  4121. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  4122. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  4123. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  4124. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  4125. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  4126. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  4127. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  4128. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  4129. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  4130. };
  4131. /* node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
  4132. * 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
  4133. * map node ctx => cabac ctx for level=1 */
  4134. static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
  4135. /* map node ctx => cabac ctx for level>1 */
  4136. static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
  4137. static const uint8_t coeff_abs_level_transition[2][8] = {
  4138. /* update node ctx after decoding a level=1 */
  4139. { 1, 2, 3, 3, 4, 5, 6, 7 },
  4140. /* update node ctx after decoding a level>1 */
  4141. { 4, 4, 4, 4, 5, 6, 7, 7 }
  4142. };
  4143. int index[64];
  4144. int av_unused last;
  4145. int coeff_count = 0;
  4146. int node_ctx = 0;
  4147. uint8_t *significant_coeff_ctx_base;
  4148. uint8_t *last_coeff_ctx_base;
  4149. uint8_t *abs_level_m1_ctx_base;
  4150. #if !ARCH_X86
  4151. #define CABAC_ON_STACK
  4152. #endif
  4153. #ifdef CABAC_ON_STACK
  4154. #define CC &cc
  4155. CABACContext cc;
  4156. cc.range = h->cabac.range;
  4157. cc.low = h->cabac.low;
  4158. cc.bytestream= h->cabac.bytestream;
  4159. #else
  4160. #define CC &h->cabac
  4161. #endif
  4162. /* cat: 0-> DC 16x16 n = 0
  4163. * 1-> AC 16x16 n = luma4x4idx
  4164. * 2-> Luma4x4 n = luma4x4idx
  4165. * 3-> DC Chroma n = iCbCr
  4166. * 4-> AC Chroma n = 16 + 4 * iCbCr + chroma4x4idx
  4167. * 5-> Luma8x8 n = 4 * luma8x8idx
  4168. */
  4169. /* read coded block flag */
  4170. if( is_dc || cat != 5 ) {
  4171. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n, is_dc ) ] ) == 0 ) {
  4172. if( !is_dc )
  4173. h->non_zero_count_cache[scan8[n]] = 0;
  4174. #ifdef CABAC_ON_STACK
  4175. h->cabac.range = cc.range ;
  4176. h->cabac.low = cc.low ;
  4177. h->cabac.bytestream= cc.bytestream;
  4178. #endif
  4179. return;
  4180. }
  4181. }
  4182. significant_coeff_ctx_base = h->cabac_state
  4183. + significant_coeff_flag_offset[MB_FIELD][cat];
  4184. last_coeff_ctx_base = h->cabac_state
  4185. + last_coeff_flag_offset[MB_FIELD][cat];
  4186. abs_level_m1_ctx_base = h->cabac_state
  4187. + coeff_abs_level_m1_offset[cat];
  4188. if( !is_dc && cat == 5 ) {
  4189. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  4190. for(last= 0; last < coefs; last++) { \
  4191. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  4192. if( get_cabac( CC, sig_ctx )) { \
  4193. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  4194. index[coeff_count++] = last; \
  4195. if( get_cabac( CC, last_ctx ) ) { \
  4196. last= max_coeff; \
  4197. break; \
  4198. } \
  4199. } \
  4200. }\
  4201. if( last == max_coeff -1 ) {\
  4202. index[coeff_count++] = last;\
  4203. }
  4204. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  4205. #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)
  4206. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  4207. } else {
  4208. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  4209. #else
  4210. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  4211. } else {
  4212. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  4213. #endif
  4214. }
  4215. assert(coeff_count > 0);
  4216. if( is_dc ) {
  4217. if( cat == 0 )
  4218. h->cbp_table[h->mb_xy] |= 0x100;
  4219. else
  4220. h->cbp_table[h->mb_xy] |= 0x40 << n;
  4221. } else {
  4222. if( cat == 5 )
  4223. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  4224. else {
  4225. assert( cat == 1 || cat == 2 || cat == 4 );
  4226. h->non_zero_count_cache[scan8[n]] = coeff_count;
  4227. }
  4228. }
  4229. do {
  4230. uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base;
  4231. int j= scantable[index[--coeff_count]];
  4232. if( get_cabac( CC, ctx ) == 0 ) {
  4233. node_ctx = coeff_abs_level_transition[0][node_ctx];
  4234. if( is_dc ) {
  4235. block[j] = get_cabac_bypass_sign( CC, -1);
  4236. }else{
  4237. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;
  4238. }
  4239. } else {
  4240. int coeff_abs = 2;
  4241. ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base;
  4242. node_ctx = coeff_abs_level_transition[1][node_ctx];
  4243. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  4244. coeff_abs++;
  4245. }
  4246. if( coeff_abs >= 15 ) {
  4247. int j = 0;
  4248. while( get_cabac_bypass( CC ) ) {
  4249. j++;
  4250. }
  4251. coeff_abs=1;
  4252. while( j-- ) {
  4253. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  4254. }
  4255. coeff_abs+= 14;
  4256. }
  4257. if( is_dc ) {
  4258. block[j] = get_cabac_bypass_sign( CC, -coeff_abs );
  4259. }else{
  4260. block[j] = (get_cabac_bypass_sign( CC, -coeff_abs ) * qmul[j] + 32) >> 6;
  4261. }
  4262. }
  4263. } while( coeff_count );
  4264. #ifdef CABAC_ON_STACK
  4265. h->cabac.range = cc.range ;
  4266. h->cabac.low = cc.low ;
  4267. h->cabac.bytestream= cc.bytestream;
  4268. #endif
  4269. }
  4270. #if !CONFIG_SMALL
  4271. 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 ) {
  4272. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 1);
  4273. }
  4274. 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 ) {
  4275. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0);
  4276. }
  4277. #endif
  4278. static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  4279. #if CONFIG_SMALL
  4280. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, cat == 0 || cat == 3);
  4281. #else
  4282. if( cat == 0 || cat == 3 ) decode_cabac_residual_dc(h, block, cat, n, scantable, qmul, max_coeff);
  4283. else decode_cabac_residual_nondc(h, block, cat, n, scantable, qmul, max_coeff);
  4284. #endif
  4285. }
  4286. static inline void compute_mb_neighbors(H264Context *h)
  4287. {
  4288. MpegEncContext * const s = &h->s;
  4289. const int mb_xy = h->mb_xy;
  4290. h->top_mb_xy = mb_xy - s->mb_stride;
  4291. h->left_mb_xy[0] = mb_xy - 1;
  4292. if(FRAME_MBAFF){
  4293. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  4294. const int top_pair_xy = pair_xy - s->mb_stride;
  4295. const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  4296. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  4297. const int curr_mb_field_flag = MB_FIELD;
  4298. const int bottom = (s->mb_y & 1);
  4299. if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
  4300. h->top_mb_xy -= s->mb_stride;
  4301. }
  4302. if (!left_mb_field_flag == curr_mb_field_flag) {
  4303. h->left_mb_xy[0] = pair_xy - 1;
  4304. }
  4305. } else if (FIELD_PICTURE) {
  4306. h->top_mb_xy -= s->mb_stride;
  4307. }
  4308. return;
  4309. }
  4310. /**
  4311. * decodes a macroblock
  4312. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4313. */
  4314. static int decode_mb_cabac(H264Context *h) {
  4315. MpegEncContext * const s = &h->s;
  4316. int mb_xy;
  4317. int mb_type, partition_count, cbp = 0;
  4318. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4319. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  4320. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4321. if( h->slice_type_nos != FF_I_TYPE ) {
  4322. int skip;
  4323. /* a skipped mb needs the aff flag from the following mb */
  4324. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  4325. predict_field_decoding_flag(h);
  4326. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  4327. skip = h->next_mb_skipped;
  4328. else
  4329. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  4330. /* read skip flags */
  4331. if( skip ) {
  4332. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  4333. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  4334. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  4335. if(!h->next_mb_skipped)
  4336. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  4337. }
  4338. decode_mb_skip(h);
  4339. h->cbp_table[mb_xy] = 0;
  4340. h->chroma_pred_mode_table[mb_xy] = 0;
  4341. h->last_qscale_diff = 0;
  4342. return 0;
  4343. }
  4344. }
  4345. if(FRAME_MBAFF){
  4346. if( (s->mb_y&1) == 0 )
  4347. h->mb_mbaff =
  4348. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  4349. }
  4350. h->prev_mb_skipped = 0;
  4351. compute_mb_neighbors(h);
  4352. if( h->slice_type_nos == FF_B_TYPE ) {
  4353. mb_type = decode_cabac_mb_type_b( h );
  4354. if( mb_type < 23 ){
  4355. partition_count= b_mb_type_info[mb_type].partition_count;
  4356. mb_type= b_mb_type_info[mb_type].type;
  4357. }else{
  4358. mb_type -= 23;
  4359. goto decode_intra_mb;
  4360. }
  4361. } else if( h->slice_type_nos == FF_P_TYPE ) {
  4362. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4363. /* P-type */
  4364. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4365. /* P_L0_D16x16, P_8x8 */
  4366. mb_type= 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4367. } else {
  4368. /* P_L0_D8x16, P_L0_D16x8 */
  4369. mb_type= 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4370. }
  4371. partition_count= p_mb_type_info[mb_type].partition_count;
  4372. mb_type= p_mb_type_info[mb_type].type;
  4373. } else {
  4374. mb_type= decode_cabac_intra_mb_type(h, 17, 0);
  4375. goto decode_intra_mb;
  4376. }
  4377. } else {
  4378. mb_type= decode_cabac_intra_mb_type(h, 3, 1);
  4379. if(h->slice_type == FF_SI_TYPE && mb_type)
  4380. mb_type--;
  4381. assert(h->slice_type_nos == FF_I_TYPE);
  4382. decode_intra_mb:
  4383. partition_count = 0;
  4384. cbp= i_mb_type_info[mb_type].cbp;
  4385. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4386. mb_type= i_mb_type_info[mb_type].type;
  4387. }
  4388. if(MB_FIELD)
  4389. mb_type |= MB_TYPE_INTERLACED;
  4390. h->slice_table[ mb_xy ]= h->slice_num;
  4391. if(IS_INTRA_PCM(mb_type)) {
  4392. const uint8_t *ptr;
  4393. // We assume these blocks are very rare so we do not optimize it.
  4394. // FIXME The two following lines get the bitstream position in the cabac
  4395. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  4396. ptr= h->cabac.bytestream;
  4397. if(h->cabac.low&0x1) ptr--;
  4398. if(CABAC_BITS==16){
  4399. if(h->cabac.low&0x1FF) ptr--;
  4400. }
  4401. // The pixels are stored in the same order as levels in h->mb array.
  4402. memcpy(h->mb, ptr, 256); ptr+=256;
  4403. if(CHROMA){
  4404. memcpy(h->mb+128, ptr, 128); ptr+=128;
  4405. }
  4406. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  4407. // All blocks are present
  4408. h->cbp_table[mb_xy] = 0x1ef;
  4409. h->chroma_pred_mode_table[mb_xy] = 0;
  4410. // In deblocking, the quantizer is 0
  4411. s->current_picture.qscale_table[mb_xy]= 0;
  4412. // All coeffs are present
  4413. memset(h->non_zero_count[mb_xy], 16, 16);
  4414. s->current_picture.mb_type[mb_xy]= mb_type;
  4415. h->last_qscale_diff = 0;
  4416. return 0;
  4417. }
  4418. if(MB_MBAFF){
  4419. h->ref_count[0] <<= 1;
  4420. h->ref_count[1] <<= 1;
  4421. }
  4422. fill_caches(h, mb_type, 0);
  4423. if( IS_INTRA( mb_type ) ) {
  4424. int i, pred_mode;
  4425. if( IS_INTRA4x4( mb_type ) ) {
  4426. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  4427. mb_type |= MB_TYPE_8x8DCT;
  4428. for( i = 0; i < 16; i+=4 ) {
  4429. int pred = pred_intra_mode( h, i );
  4430. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4431. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4432. }
  4433. } else {
  4434. for( i = 0; i < 16; i++ ) {
  4435. int pred = pred_intra_mode( h, i );
  4436. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4437. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  4438. }
  4439. }
  4440. ff_h264_write_back_intra_pred_mode(h);
  4441. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  4442. } else {
  4443. h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode( h, h->intra16x16_pred_mode );
  4444. if( h->intra16x16_pred_mode < 0 ) return -1;
  4445. }
  4446. if(CHROMA){
  4447. h->chroma_pred_mode_table[mb_xy] =
  4448. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  4449. pred_mode= ff_h264_check_intra_pred_mode( h, pred_mode );
  4450. if( pred_mode < 0 ) return -1;
  4451. h->chroma_pred_mode= pred_mode;
  4452. }
  4453. } else if( partition_count == 4 ) {
  4454. int i, j, sub_partition_count[4], list, ref[2][4];
  4455. if( h->slice_type_nos == FF_B_TYPE ) {
  4456. for( i = 0; i < 4; i++ ) {
  4457. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  4458. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4459. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4460. }
  4461. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  4462. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  4463. ff_h264_pred_direct_motion(h, &mb_type);
  4464. h->ref_cache[0][scan8[4]] =
  4465. h->ref_cache[1][scan8[4]] =
  4466. h->ref_cache[0][scan8[12]] =
  4467. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4468. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  4469. for( i = 0; i < 4; i++ )
  4470. if( IS_DIRECT(h->sub_mb_type[i]) )
  4471. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  4472. }
  4473. }
  4474. } else {
  4475. for( i = 0; i < 4; i++ ) {
  4476. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  4477. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4478. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4479. }
  4480. }
  4481. for( list = 0; list < h->list_count; list++ ) {
  4482. for( i = 0; i < 4; i++ ) {
  4483. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4484. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4485. if( h->ref_count[list] > 1 ){
  4486. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  4487. if(ref[list][i] >= (unsigned)h->ref_count[list]){
  4488. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref[list][i], h->ref_count[list]);
  4489. return -1;
  4490. }
  4491. }else
  4492. ref[list][i] = 0;
  4493. } else {
  4494. ref[list][i] = -1;
  4495. }
  4496. h->ref_cache[list][ scan8[4*i]+1 ]=
  4497. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4498. }
  4499. }
  4500. if(dct8x8_allowed)
  4501. dct8x8_allowed = get_dct8x8_allowed(h);
  4502. for(list=0; list<h->list_count; list++){
  4503. for(i=0; i<4; i++){
  4504. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  4505. if(IS_DIRECT(h->sub_mb_type[i])){
  4506. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  4507. continue;
  4508. }
  4509. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  4510. const int sub_mb_type= h->sub_mb_type[i];
  4511. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4512. for(j=0; j<sub_partition_count[i]; j++){
  4513. int mpx, mpy;
  4514. int mx, my;
  4515. const int index= 4*i + block_width*j;
  4516. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4517. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  4518. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  4519. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  4520. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  4521. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4522. if(IS_SUB_8X8(sub_mb_type)){
  4523. mv_cache[ 1 ][0]=
  4524. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4525. mv_cache[ 1 ][1]=
  4526. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4527. mvd_cache[ 1 ][0]=
  4528. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  4529. mvd_cache[ 1 ][1]=
  4530. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  4531. }else if(IS_SUB_8X4(sub_mb_type)){
  4532. mv_cache[ 1 ][0]= mx;
  4533. mv_cache[ 1 ][1]= my;
  4534. mvd_cache[ 1 ][0]= mx - mpx;
  4535. mvd_cache[ 1 ][1]= my - mpy;
  4536. }else if(IS_SUB_4X8(sub_mb_type)){
  4537. mv_cache[ 8 ][0]= mx;
  4538. mv_cache[ 8 ][1]= my;
  4539. mvd_cache[ 8 ][0]= mx - mpx;
  4540. mvd_cache[ 8 ][1]= my - mpy;
  4541. }
  4542. mv_cache[ 0 ][0]= mx;
  4543. mv_cache[ 0 ][1]= my;
  4544. mvd_cache[ 0 ][0]= mx - mpx;
  4545. mvd_cache[ 0 ][1]= my - mpy;
  4546. }
  4547. }else{
  4548. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4549. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  4550. p[0] = p[1] = p[8] = p[9] = 0;
  4551. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  4552. }
  4553. }
  4554. }
  4555. } else if( IS_DIRECT(mb_type) ) {
  4556. ff_h264_pred_direct_motion(h, &mb_type);
  4557. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4558. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  4559. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4560. } else {
  4561. int list, mx, my, i, mpx, mpy;
  4562. if(IS_16X16(mb_type)){
  4563. for(list=0; list<h->list_count; list++){
  4564. if(IS_DIR(mb_type, 0, list)){
  4565. int ref;
  4566. if(h->ref_count[list] > 1){
  4567. ref= decode_cabac_mb_ref(h, list, 0);
  4568. if(ref >= (unsigned)h->ref_count[list]){
  4569. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  4570. return -1;
  4571. }
  4572. }else
  4573. ref=0;
  4574. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  4575. }else
  4576. 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
  4577. }
  4578. for(list=0; list<h->list_count; list++){
  4579. if(IS_DIR(mb_type, 0, list)){
  4580. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  4581. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  4582. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  4583. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4584. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4585. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4586. }else
  4587. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4588. }
  4589. }
  4590. else if(IS_16X8(mb_type)){
  4591. for(list=0; list<h->list_count; list++){
  4592. for(i=0; i<2; i++){
  4593. if(IS_DIR(mb_type, i, list)){
  4594. int ref;
  4595. if(h->ref_count[list] > 1){
  4596. ref= decode_cabac_mb_ref( h, list, 8*i );
  4597. if(ref >= (unsigned)h->ref_count[list]){
  4598. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  4599. return -1;
  4600. }
  4601. }else
  4602. ref=0;
  4603. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  4604. }else
  4605. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4606. }
  4607. }
  4608. for(list=0; list<h->list_count; list++){
  4609. for(i=0; i<2; i++){
  4610. if(IS_DIR(mb_type, i, list)){
  4611. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  4612. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  4613. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  4614. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4615. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  4616. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4617. }else{
  4618. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4619. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4620. }
  4621. }
  4622. }
  4623. }else{
  4624. assert(IS_8X16(mb_type));
  4625. for(list=0; list<h->list_count; list++){
  4626. for(i=0; i<2; i++){
  4627. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4628. int ref;
  4629. if(h->ref_count[list] > 1){
  4630. ref= decode_cabac_mb_ref( h, list, 4*i );
  4631. if(ref >= (unsigned)h->ref_count[list]){
  4632. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  4633. return -1;
  4634. }
  4635. }else
  4636. ref=0;
  4637. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  4638. }else
  4639. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4640. }
  4641. }
  4642. for(list=0; list<h->list_count; list++){
  4643. for(i=0; i<2; i++){
  4644. if(IS_DIR(mb_type, i, list)){
  4645. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  4646. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  4647. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  4648. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4649. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4650. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4651. }else{
  4652. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4653. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4654. }
  4655. }
  4656. }
  4657. }
  4658. }
  4659. if( IS_INTER( mb_type ) ) {
  4660. h->chroma_pred_mode_table[mb_xy] = 0;
  4661. write_back_motion( h, mb_type );
  4662. }
  4663. if( !IS_INTRA16x16( mb_type ) ) {
  4664. cbp = decode_cabac_mb_cbp_luma( h );
  4665. if(CHROMA)
  4666. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  4667. }
  4668. h->cbp_table[mb_xy] = h->cbp = cbp;
  4669. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  4670. if( decode_cabac_mb_transform_size( h ) )
  4671. mb_type |= MB_TYPE_8x8DCT;
  4672. }
  4673. s->current_picture.mb_type[mb_xy]= mb_type;
  4674. if( cbp || IS_INTRA16x16( mb_type ) ) {
  4675. const uint8_t *scan, *scan8x8, *dc_scan;
  4676. const uint32_t *qmul;
  4677. int dqp;
  4678. if(IS_INTERLACED(mb_type)){
  4679. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  4680. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4681. dc_scan= luma_dc_field_scan;
  4682. }else{
  4683. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  4684. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4685. dc_scan= luma_dc_zigzag_scan;
  4686. }
  4687. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  4688. if( dqp == INT_MIN ){
  4689. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  4690. return -1;
  4691. }
  4692. s->qscale += dqp;
  4693. if(((unsigned)s->qscale) > 51){
  4694. if(s->qscale<0) s->qscale+= 52;
  4695. else s->qscale-= 52;
  4696. }
  4697. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  4698. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  4699. if( IS_INTRA16x16( mb_type ) ) {
  4700. int i;
  4701. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  4702. decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16);
  4703. if( cbp&15 ) {
  4704. qmul = h->dequant4_coeff[0][s->qscale];
  4705. for( i = 0; i < 16; i++ ) {
  4706. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  4707. decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15);
  4708. }
  4709. } else {
  4710. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4711. }
  4712. } else {
  4713. int i8x8, i4x4;
  4714. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4715. if( cbp & (1<<i8x8) ) {
  4716. if( IS_8x8DCT(mb_type) ) {
  4717. decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  4718. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64);
  4719. } else {
  4720. qmul = h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale];
  4721. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  4722. const int index = 4*i8x8 + i4x4;
  4723. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  4724. //START_TIMER
  4725. decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, qmul, 16);
  4726. //STOP_TIMER("decode_residual")
  4727. }
  4728. }
  4729. } else {
  4730. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4731. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4732. }
  4733. }
  4734. }
  4735. if( cbp&0x30 ){
  4736. int c;
  4737. for( c = 0; c < 2; c++ ) {
  4738. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  4739. decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4);
  4740. }
  4741. }
  4742. if( cbp&0x20 ) {
  4743. int c, i;
  4744. for( c = 0; c < 2; c++ ) {
  4745. qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];
  4746. for( i = 0; i < 4; i++ ) {
  4747. const int index = 16 + 4 * c + i;
  4748. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  4749. decode_cabac_residual(h, h->mb + 16*index, 4, index, scan + 1, qmul, 15);
  4750. }
  4751. }
  4752. } else {
  4753. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4754. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4755. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4756. }
  4757. } else {
  4758. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4759. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4760. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4761. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4762. h->last_qscale_diff = 0;
  4763. }
  4764. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4765. write_back_non_zero_count(h);
  4766. if(MB_MBAFF){
  4767. h->ref_count[0] >>= 1;
  4768. h->ref_count[1] >>= 1;
  4769. }
  4770. return 0;
  4771. }
  4772. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  4773. H264Context *h = *(void**)arg;
  4774. MpegEncContext * const s = &h->s;
  4775. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  4776. s->mb_skip_run= -1;
  4777. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  4778. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  4779. if( h->pps.cabac ) {
  4780. int i;
  4781. /* realign */
  4782. align_get_bits( &s->gb );
  4783. /* init cabac */
  4784. ff_init_cabac_states( &h->cabac);
  4785. ff_init_cabac_decoder( &h->cabac,
  4786. s->gb.buffer + get_bits_count(&s->gb)/8,
  4787. (get_bits_left(&s->gb) + 7)/8);
  4788. /* calculate pre-state */
  4789. for( i= 0; i < 460; i++ ) {
  4790. int pre;
  4791. if( h->slice_type_nos == FF_I_TYPE )
  4792. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  4793. else
  4794. 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 );
  4795. if( pre <= 63 )
  4796. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  4797. else
  4798. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  4799. }
  4800. for(;;){
  4801. //START_TIMER
  4802. int ret = decode_mb_cabac(h);
  4803. int eos;
  4804. //STOP_TIMER("decode_mb_cabac")
  4805. if(ret>=0) ff_h264_hl_decode_mb(h);
  4806. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  4807. s->mb_y++;
  4808. ret = decode_mb_cabac(h);
  4809. if(ret>=0) ff_h264_hl_decode_mb(h);
  4810. s->mb_y--;
  4811. }
  4812. eos = get_cabac_terminate( &h->cabac );
  4813. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  4814. 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);
  4815. 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);
  4816. return -1;
  4817. }
  4818. if( ++s->mb_x >= s->mb_width ) {
  4819. s->mb_x = 0;
  4820. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4821. ++s->mb_y;
  4822. if(FIELD_OR_MBAFF_PICTURE) {
  4823. ++s->mb_y;
  4824. }
  4825. }
  4826. if( eos || s->mb_y >= s->mb_height ) {
  4827. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4828. 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);
  4829. return 0;
  4830. }
  4831. }
  4832. } else {
  4833. for(;;){
  4834. int ret = decode_mb_cavlc(h);
  4835. if(ret>=0) ff_h264_hl_decode_mb(h);
  4836. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  4837. s->mb_y++;
  4838. ret = decode_mb_cavlc(h);
  4839. if(ret>=0) ff_h264_hl_decode_mb(h);
  4840. s->mb_y--;
  4841. }
  4842. if(ret<0){
  4843. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  4844. 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);
  4845. return -1;
  4846. }
  4847. if(++s->mb_x >= s->mb_width){
  4848. s->mb_x=0;
  4849. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4850. ++s->mb_y;
  4851. if(FIELD_OR_MBAFF_PICTURE) {
  4852. ++s->mb_y;
  4853. }
  4854. if(s->mb_y >= s->mb_height){
  4855. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4856. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  4857. 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);
  4858. return 0;
  4859. }else{
  4860. 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);
  4861. return -1;
  4862. }
  4863. }
  4864. }
  4865. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  4866. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4867. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  4868. 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);
  4869. return 0;
  4870. }else{
  4871. 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);
  4872. return -1;
  4873. }
  4874. }
  4875. }
  4876. }
  4877. #if 0
  4878. for(;s->mb_y < s->mb_height; s->mb_y++){
  4879. for(;s->mb_x < s->mb_width; s->mb_x++){
  4880. int ret= decode_mb(h);
  4881. ff_h264_hl_decode_mb(h);
  4882. if(ret<0){
  4883. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  4884. 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);
  4885. return -1;
  4886. }
  4887. if(++s->mb_x >= s->mb_width){
  4888. s->mb_x=0;
  4889. if(++s->mb_y >= s->mb_height){
  4890. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  4891. 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);
  4892. return 0;
  4893. }else{
  4894. 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);
  4895. return -1;
  4896. }
  4897. }
  4898. }
  4899. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  4900. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  4901. 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);
  4902. return 0;
  4903. }else{
  4904. 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);
  4905. return -1;
  4906. }
  4907. }
  4908. }
  4909. s->mb_x=0;
  4910. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4911. }
  4912. #endif
  4913. return -1; //not reached
  4914. }
  4915. /**
  4916. * Call decode_slice() for each context.
  4917. *
  4918. * @param h h264 master context
  4919. * @param context_count number of contexts to execute
  4920. */
  4921. static void execute_decode_slices(H264Context *h, int context_count){
  4922. MpegEncContext * const s = &h->s;
  4923. AVCodecContext * const avctx= s->avctx;
  4924. H264Context *hx;
  4925. int i;
  4926. if (s->avctx->hwaccel)
  4927. return;
  4928. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  4929. return;
  4930. if(context_count == 1) {
  4931. decode_slice(avctx, &h);
  4932. } else {
  4933. for(i = 1; i < context_count; i++) {
  4934. hx = h->thread_context[i];
  4935. hx->s.error_recognition = avctx->error_recognition;
  4936. hx->s.error_count = 0;
  4937. }
  4938. avctx->execute(avctx, (void *)decode_slice,
  4939. h->thread_context, NULL, context_count, sizeof(void*));
  4940. /* pull back stuff from slices to master context */
  4941. hx = h->thread_context[context_count - 1];
  4942. s->mb_x = hx->s.mb_x;
  4943. s->mb_y = hx->s.mb_y;
  4944. s->dropable = hx->s.dropable;
  4945. s->picture_structure = hx->s.picture_structure;
  4946. for(i = 1; i < context_count; i++)
  4947. h->s.error_count += h->thread_context[i]->s.error_count;
  4948. }
  4949. }
  4950. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  4951. MpegEncContext * const s = &h->s;
  4952. AVCodecContext * const avctx= s->avctx;
  4953. int buf_index=0;
  4954. H264Context *hx; ///< thread context
  4955. int context_count = 0;
  4956. int next_avc= h->is_avc ? 0 : buf_size;
  4957. h->max_contexts = avctx->thread_count;
  4958. #if 0
  4959. int i;
  4960. for(i=0; i<50; i++){
  4961. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  4962. }
  4963. #endif
  4964. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  4965. h->current_slice = 0;
  4966. if (!s->first_field)
  4967. s->current_picture_ptr= NULL;
  4968. ff_h264_reset_sei(h);
  4969. }
  4970. for(;;){
  4971. int consumed;
  4972. int dst_length;
  4973. int bit_length;
  4974. const uint8_t *ptr;
  4975. int i, nalsize = 0;
  4976. int err;
  4977. if(buf_index >= next_avc) {
  4978. if(buf_index >= buf_size) break;
  4979. nalsize = 0;
  4980. for(i = 0; i < h->nal_length_size; i++)
  4981. nalsize = (nalsize << 8) | buf[buf_index++];
  4982. if(nalsize <= 1 || nalsize > buf_size - buf_index){
  4983. if(nalsize == 1){
  4984. buf_index++;
  4985. continue;
  4986. }else{
  4987. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  4988. break;
  4989. }
  4990. }
  4991. next_avc= buf_index + nalsize;
  4992. } else {
  4993. // start code prefix search
  4994. for(; buf_index + 3 < next_avc; buf_index++){
  4995. // This should always succeed in the first iteration.
  4996. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  4997. break;
  4998. }
  4999. if(buf_index+3 >= buf_size) break;
  5000. buf_index+=3;
  5001. if(buf_index >= next_avc) continue;
  5002. }
  5003. hx = h->thread_context[context_count];
  5004. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  5005. if (ptr==NULL || dst_length < 0){
  5006. return -1;
  5007. }
  5008. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  5009. dst_length--;
  5010. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  5011. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  5012. 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);
  5013. }
  5014. if (h->is_avc && (nalsize != consumed) && nalsize){
  5015. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  5016. }
  5017. buf_index += consumed;
  5018. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
  5019. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  5020. continue;
  5021. again:
  5022. err = 0;
  5023. switch(hx->nal_unit_type){
  5024. case NAL_IDR_SLICE:
  5025. if (h->nal_unit_type != NAL_IDR_SLICE) {
  5026. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  5027. return -1;
  5028. }
  5029. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  5030. case NAL_SLICE:
  5031. init_get_bits(&hx->s.gb, ptr, bit_length);
  5032. hx->intra_gb_ptr=
  5033. hx->inter_gb_ptr= &hx->s.gb;
  5034. hx->s.data_partitioning = 0;
  5035. if((err = decode_slice_header(hx, h)))
  5036. break;
  5037. if (s->avctx->hwaccel && h->current_slice == 1) {
  5038. if (s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  5039. return -1;
  5040. }
  5041. s->current_picture_ptr->key_frame |=
  5042. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  5043. (h->sei_recovery_frame_cnt >= 0);
  5044. if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
  5045. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  5046. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  5047. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  5048. && avctx->skip_frame < AVDISCARD_ALL){
  5049. if(avctx->hwaccel) {
  5050. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  5051. return -1;
  5052. }else
  5053. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  5054. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  5055. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  5056. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  5057. }else
  5058. context_count++;
  5059. }
  5060. break;
  5061. case NAL_DPA:
  5062. init_get_bits(&hx->s.gb, ptr, bit_length);
  5063. hx->intra_gb_ptr=
  5064. hx->inter_gb_ptr= NULL;
  5065. if ((err = decode_slice_header(hx, h)) < 0)
  5066. break;
  5067. hx->s.data_partitioning = 1;
  5068. break;
  5069. case NAL_DPB:
  5070. init_get_bits(&hx->intra_gb, ptr, bit_length);
  5071. hx->intra_gb_ptr= &hx->intra_gb;
  5072. break;
  5073. case NAL_DPC:
  5074. init_get_bits(&hx->inter_gb, ptr, bit_length);
  5075. hx->inter_gb_ptr= &hx->inter_gb;
  5076. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  5077. && s->context_initialized
  5078. && s->hurry_up < 5
  5079. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  5080. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  5081. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  5082. && avctx->skip_frame < AVDISCARD_ALL)
  5083. context_count++;
  5084. break;
  5085. case NAL_SEI:
  5086. init_get_bits(&s->gb, ptr, bit_length);
  5087. ff_h264_decode_sei(h);
  5088. break;
  5089. case NAL_SPS:
  5090. init_get_bits(&s->gb, ptr, bit_length);
  5091. ff_h264_decode_seq_parameter_set(h);
  5092. if(s->flags& CODEC_FLAG_LOW_DELAY)
  5093. s->low_delay=1;
  5094. if(avctx->has_b_frames < 2)
  5095. avctx->has_b_frames= !s->low_delay;
  5096. break;
  5097. case NAL_PPS:
  5098. init_get_bits(&s->gb, ptr, bit_length);
  5099. ff_h264_decode_picture_parameter_set(h, bit_length);
  5100. break;
  5101. case NAL_AUD:
  5102. case NAL_END_SEQUENCE:
  5103. case NAL_END_STREAM:
  5104. case NAL_FILLER_DATA:
  5105. case NAL_SPS_EXT:
  5106. case NAL_AUXILIARY_SLICE:
  5107. break;
  5108. default:
  5109. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  5110. }
  5111. if(context_count == h->max_contexts) {
  5112. execute_decode_slices(h, context_count);
  5113. context_count = 0;
  5114. }
  5115. if (err < 0)
  5116. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  5117. else if(err == 1) {
  5118. /* Slice could not be decoded in parallel mode, copy down
  5119. * NAL unit stuff to context 0 and restart. Note that
  5120. * rbsp_buffer is not transferred, but since we no longer
  5121. * run in parallel mode this should not be an issue. */
  5122. h->nal_unit_type = hx->nal_unit_type;
  5123. h->nal_ref_idc = hx->nal_ref_idc;
  5124. hx = h;
  5125. goto again;
  5126. }
  5127. }
  5128. if(context_count)
  5129. execute_decode_slices(h, context_count);
  5130. return buf_index;
  5131. }
  5132. /**
  5133. * returns the number of bytes consumed for building the current frame
  5134. */
  5135. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  5136. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  5137. if(pos+10>buf_size) pos=buf_size; // oops ;)
  5138. return pos;
  5139. }
  5140. static int decode_frame(AVCodecContext *avctx,
  5141. void *data, int *data_size,
  5142. AVPacket *avpkt)
  5143. {
  5144. const uint8_t *buf = avpkt->data;
  5145. int buf_size = avpkt->size;
  5146. H264Context *h = avctx->priv_data;
  5147. MpegEncContext *s = &h->s;
  5148. AVFrame *pict = data;
  5149. int buf_index;
  5150. s->flags= avctx->flags;
  5151. s->flags2= avctx->flags2;
  5152. /* end of stream, output what is still in the buffers */
  5153. if (buf_size == 0) {
  5154. Picture *out;
  5155. int i, out_idx;
  5156. //FIXME factorize this with the output code below
  5157. out = h->delayed_pic[0];
  5158. out_idx = 0;
  5159. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  5160. if(h->delayed_pic[i]->poc < out->poc){
  5161. out = h->delayed_pic[i];
  5162. out_idx = i;
  5163. }
  5164. for(i=out_idx; h->delayed_pic[i]; i++)
  5165. h->delayed_pic[i] = h->delayed_pic[i+1];
  5166. if(out){
  5167. *data_size = sizeof(AVFrame);
  5168. *pict= *(AVFrame*)out;
  5169. }
  5170. return 0;
  5171. }
  5172. if(h->is_avc && !h->got_avcC) {
  5173. int i, cnt, nalsize;
  5174. unsigned char *p = avctx->extradata;
  5175. if(avctx->extradata_size < 7) {
  5176. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  5177. return -1;
  5178. }
  5179. if(*p != 1) {
  5180. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  5181. return -1;
  5182. }
  5183. /* sps and pps in the avcC always have length coded with 2 bytes,
  5184. so put a fake nal_length_size = 2 while parsing them */
  5185. h->nal_length_size = 2;
  5186. // Decode sps from avcC
  5187. cnt = *(p+5) & 0x1f; // Number of sps
  5188. p += 6;
  5189. for (i = 0; i < cnt; i++) {
  5190. nalsize = AV_RB16(p) + 2;
  5191. if(decode_nal_units(h, p, nalsize) < 0) {
  5192. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  5193. return -1;
  5194. }
  5195. p += nalsize;
  5196. }
  5197. // Decode pps from avcC
  5198. cnt = *(p++); // Number of pps
  5199. for (i = 0; i < cnt; i++) {
  5200. nalsize = AV_RB16(p) + 2;
  5201. if(decode_nal_units(h, p, nalsize) != nalsize) {
  5202. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  5203. return -1;
  5204. }
  5205. p += nalsize;
  5206. }
  5207. // Now store right nal length size, that will be use to parse all other nals
  5208. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  5209. // Do not reparse avcC
  5210. h->got_avcC = 1;
  5211. }
  5212. if(!h->got_avcC && !h->is_avc && s->avctx->extradata_size){
  5213. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  5214. return -1;
  5215. h->got_avcC = 1;
  5216. }
  5217. buf_index=decode_nal_units(h, buf, buf_size);
  5218. if(buf_index < 0)
  5219. return -1;
  5220. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  5221. if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
  5222. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  5223. return -1;
  5224. }
  5225. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  5226. Picture *out = s->current_picture_ptr;
  5227. Picture *cur = s->current_picture_ptr;
  5228. int i, pics, out_of_order, out_idx;
  5229. field_end(h);
  5230. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  5231. /* Wait for second field. */
  5232. *data_size = 0;
  5233. } else {
  5234. cur->interlaced_frame = 0;
  5235. cur->repeat_pict = 0;
  5236. /* Signal interlacing information externally. */
  5237. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  5238. if(h->sps.pic_struct_present_flag){
  5239. switch (h->sei_pic_struct)
  5240. {
  5241. case SEI_PIC_STRUCT_FRAME:
  5242. break;
  5243. case SEI_PIC_STRUCT_TOP_FIELD:
  5244. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  5245. cur->interlaced_frame = 1;
  5246. break;
  5247. case SEI_PIC_STRUCT_TOP_BOTTOM:
  5248. case SEI_PIC_STRUCT_BOTTOM_TOP:
  5249. if (FIELD_OR_MBAFF_PICTURE)
  5250. cur->interlaced_frame = 1;
  5251. else
  5252. // try to flag soft telecine progressive
  5253. cur->interlaced_frame = h->prev_interlaced_frame;
  5254. break;
  5255. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  5256. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  5257. // Signal the possibility of telecined film externally (pic_struct 5,6)
  5258. // From these hints, let the applications decide if they apply deinterlacing.
  5259. cur->repeat_pict = 1;
  5260. break;
  5261. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  5262. // Force progressive here, as doubling interlaced frame is a bad idea.
  5263. cur->repeat_pict = 2;
  5264. break;
  5265. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  5266. cur->repeat_pict = 4;
  5267. break;
  5268. }
  5269. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  5270. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  5271. }else{
  5272. /* Derive interlacing flag from used decoding process. */
  5273. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  5274. }
  5275. h->prev_interlaced_frame = cur->interlaced_frame;
  5276. if (cur->field_poc[0] != cur->field_poc[1]){
  5277. /* Derive top_field_first from field pocs. */
  5278. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  5279. }else{
  5280. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  5281. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  5282. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  5283. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  5284. cur->top_field_first = 1;
  5285. else
  5286. cur->top_field_first = 0;
  5287. }else{
  5288. /* Most likely progressive */
  5289. cur->top_field_first = 0;
  5290. }
  5291. }
  5292. //FIXME do something with unavailable reference frames
  5293. /* Sort B-frames into display order */
  5294. if(h->sps.bitstream_restriction_flag
  5295. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  5296. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  5297. s->low_delay = 0;
  5298. }
  5299. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  5300. && !h->sps.bitstream_restriction_flag){
  5301. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  5302. s->low_delay= 0;
  5303. }
  5304. pics = 0;
  5305. while(h->delayed_pic[pics]) pics++;
  5306. assert(pics <= MAX_DELAYED_PIC_COUNT);
  5307. h->delayed_pic[pics++] = cur;
  5308. if(cur->reference == 0)
  5309. cur->reference = DELAYED_PIC_REF;
  5310. out = h->delayed_pic[0];
  5311. out_idx = 0;
  5312. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  5313. if(h->delayed_pic[i]->poc < out->poc){
  5314. out = h->delayed_pic[i];
  5315. out_idx = i;
  5316. }
  5317. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  5318. h->outputed_poc= INT_MIN;
  5319. out_of_order = out->poc < h->outputed_poc;
  5320. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  5321. { }
  5322. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  5323. || (s->low_delay &&
  5324. ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
  5325. || cur->pict_type == FF_B_TYPE)))
  5326. {
  5327. s->low_delay = 0;
  5328. s->avctx->has_b_frames++;
  5329. }
  5330. if(out_of_order || pics > s->avctx->has_b_frames){
  5331. out->reference &= ~DELAYED_PIC_REF;
  5332. for(i=out_idx; h->delayed_pic[i]; i++)
  5333. h->delayed_pic[i] = h->delayed_pic[i+1];
  5334. }
  5335. if(!out_of_order && pics > s->avctx->has_b_frames){
  5336. *data_size = sizeof(AVFrame);
  5337. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  5338. h->outputed_poc = INT_MIN;
  5339. } else
  5340. h->outputed_poc = out->poc;
  5341. *pict= *(AVFrame*)out;
  5342. }else{
  5343. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  5344. }
  5345. }
  5346. }
  5347. assert(pict->data[0] || !*data_size);
  5348. ff_print_debug_info(s, pict);
  5349. //printf("out %d\n", (int)pict->data[0]);
  5350. return get_consumed_bytes(s, buf_index, buf_size);
  5351. }
  5352. #if 0
  5353. static inline void fill_mb_avail(H264Context *h){
  5354. MpegEncContext * const s = &h->s;
  5355. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5356. if(s->mb_y){
  5357. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  5358. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  5359. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  5360. }else{
  5361. h->mb_avail[0]=
  5362. h->mb_avail[1]=
  5363. h->mb_avail[2]= 0;
  5364. }
  5365. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  5366. h->mb_avail[4]= 1; //FIXME move out
  5367. h->mb_avail[5]= 0; //FIXME move out
  5368. }
  5369. #endif
  5370. #ifdef TEST
  5371. #undef printf
  5372. #undef random
  5373. #define COUNT 8000
  5374. #define SIZE (COUNT*40)
  5375. int main(void){
  5376. int i;
  5377. uint8_t temp[SIZE];
  5378. PutBitContext pb;
  5379. GetBitContext gb;
  5380. // int int_temp[10000];
  5381. DSPContext dsp;
  5382. AVCodecContext avctx;
  5383. dsputil_init(&dsp, &avctx);
  5384. init_put_bits(&pb, temp, SIZE);
  5385. printf("testing unsigned exp golomb\n");
  5386. for(i=0; i<COUNT; i++){
  5387. START_TIMER
  5388. set_ue_golomb(&pb, i);
  5389. STOP_TIMER("set_ue_golomb");
  5390. }
  5391. flush_put_bits(&pb);
  5392. init_get_bits(&gb, temp, 8*SIZE);
  5393. for(i=0; i<COUNT; i++){
  5394. int j, s;
  5395. s= show_bits(&gb, 24);
  5396. START_TIMER
  5397. j= get_ue_golomb(&gb);
  5398. if(j != i){
  5399. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  5400. // return -1;
  5401. }
  5402. STOP_TIMER("get_ue_golomb");
  5403. }
  5404. init_put_bits(&pb, temp, SIZE);
  5405. printf("testing signed exp golomb\n");
  5406. for(i=0; i<COUNT; i++){
  5407. START_TIMER
  5408. set_se_golomb(&pb, i - COUNT/2);
  5409. STOP_TIMER("set_se_golomb");
  5410. }
  5411. flush_put_bits(&pb);
  5412. init_get_bits(&gb, temp, 8*SIZE);
  5413. for(i=0; i<COUNT; i++){
  5414. int j, s;
  5415. s= show_bits(&gb, 24);
  5416. START_TIMER
  5417. j= get_se_golomb(&gb);
  5418. if(j != i - COUNT/2){
  5419. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  5420. // return -1;
  5421. }
  5422. STOP_TIMER("get_se_golomb");
  5423. }
  5424. #if 0
  5425. printf("testing 4x4 (I)DCT\n");
  5426. DCTELEM block[16];
  5427. uint8_t src[16], ref[16];
  5428. uint64_t error= 0, max_error=0;
  5429. for(i=0; i<COUNT; i++){
  5430. int j;
  5431. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  5432. for(j=0; j<16; j++){
  5433. ref[j]= random()%255;
  5434. src[j]= random()%255;
  5435. }
  5436. h264_diff_dct_c(block, src, ref, 4);
  5437. //normalize
  5438. for(j=0; j<16; j++){
  5439. // printf("%d ", block[j]);
  5440. block[j]= block[j]*4;
  5441. if(j&1) block[j]= (block[j]*4 + 2)/5;
  5442. if(j&4) block[j]= (block[j]*4 + 2)/5;
  5443. }
  5444. // printf("\n");
  5445. s->dsp.h264_idct_add(ref, block, 4);
  5446. /* for(j=0; j<16; j++){
  5447. printf("%d ", ref[j]);
  5448. }
  5449. printf("\n");*/
  5450. for(j=0; j<16; j++){
  5451. int diff= FFABS(src[j] - ref[j]);
  5452. error+= diff*diff;
  5453. max_error= FFMAX(max_error, diff);
  5454. }
  5455. }
  5456. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  5457. printf("testing quantizer\n");
  5458. for(qp=0; qp<52; qp++){
  5459. for(i=0; i<16; i++)
  5460. src1_block[i]= src2_block[i]= random()%255;
  5461. }
  5462. printf("Testing NAL layer\n");
  5463. uint8_t bitstream[COUNT];
  5464. uint8_t nal[COUNT*2];
  5465. H264Context h;
  5466. memset(&h, 0, sizeof(H264Context));
  5467. for(i=0; i<COUNT; i++){
  5468. int zeros= i;
  5469. int nal_length;
  5470. int consumed;
  5471. int out_length;
  5472. uint8_t *out;
  5473. int j;
  5474. for(j=0; j<COUNT; j++){
  5475. bitstream[j]= (random() % 255) + 1;
  5476. }
  5477. for(j=0; j<zeros; j++){
  5478. int pos= random() % COUNT;
  5479. while(bitstream[pos] == 0){
  5480. pos++;
  5481. pos %= COUNT;
  5482. }
  5483. bitstream[pos]=0;
  5484. }
  5485. START_TIMER
  5486. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  5487. if(nal_length<0){
  5488. printf("encoding failed\n");
  5489. return -1;
  5490. }
  5491. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  5492. STOP_TIMER("NAL")
  5493. if(out_length != COUNT){
  5494. printf("incorrect length %d %d\n", out_length, COUNT);
  5495. return -1;
  5496. }
  5497. if(consumed != nal_length){
  5498. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  5499. return -1;
  5500. }
  5501. if(memcmp(bitstream, out, COUNT)){
  5502. printf("mismatch\n");
  5503. return -1;
  5504. }
  5505. }
  5506. #endif
  5507. printf("Testing RBSP\n");
  5508. return 0;
  5509. }
  5510. #endif /* TEST */
  5511. av_cold void ff_h264_free_context(H264Context *h)
  5512. {
  5513. int i;
  5514. free_tables(h); //FIXME cleanup init stuff perhaps
  5515. for(i = 0; i < MAX_SPS_COUNT; i++)
  5516. av_freep(h->sps_buffers + i);
  5517. for(i = 0; i < MAX_PPS_COUNT; i++)
  5518. av_freep(h->pps_buffers + i);
  5519. }
  5520. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  5521. {
  5522. H264Context *h = avctx->priv_data;
  5523. MpegEncContext *s = &h->s;
  5524. ff_h264_free_context(h);
  5525. MPV_common_end(s);
  5526. // memset(h, 0, sizeof(H264Context));
  5527. return 0;
  5528. }
  5529. AVCodec h264_decoder = {
  5530. "h264",
  5531. CODEC_TYPE_VIDEO,
  5532. CODEC_ID_H264,
  5533. sizeof(H264Context),
  5534. ff_h264_decode_init,
  5535. NULL,
  5536. ff_h264_decode_end,
  5537. decode_frame,
  5538. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  5539. .flush= flush_dpb,
  5540. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  5541. .pix_fmts= ff_hwaccel_pixfmt_list_420,
  5542. };
  5543. #if CONFIG_H264_VDPAU_DECODER
  5544. AVCodec h264_vdpau_decoder = {
  5545. "h264_vdpau",
  5546. CODEC_TYPE_VIDEO,
  5547. CODEC_ID_H264,
  5548. sizeof(H264Context),
  5549. ff_h264_decode_init,
  5550. NULL,
  5551. ff_h264_decode_end,
  5552. decode_frame,
  5553. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  5554. .flush= flush_dpb,
  5555. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  5556. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  5557. };
  5558. #endif