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.

8211 lines
317KB

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