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.

3497 lines
126KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... 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
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "internal.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264.h"
  32. #include "h264data.h"
  33. #include "h264_mvpred.h"
  34. #include "golomb.h"
  35. #include "mathops.h"
  36. #include "rectangle.h"
  37. #include "thread.h"
  38. #include "vdpau_internal.h"
  39. #include "libavutil/avassert.h"
  40. #include "cabac.h"
  41. //#undef NDEBUG
  42. #include <assert.h>
  43. static const uint8_t rem6[QP_MAX_MAX+1]={
  44. 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, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  45. };
  46. static const uint8_t div6[QP_MAX_MAX+1]={
  47. 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, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,
  48. };
  49. static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
  50. PIX_FMT_DXVA2_VLD,
  51. PIX_FMT_VAAPI_VLD,
  52. PIX_FMT_YUVJ420P,
  53. PIX_FMT_NONE
  54. };
  55. void ff_h264_write_back_intra_pred_mode(H264Context *h){
  56. int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
  57. AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
  58. mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
  59. mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
  60. mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
  61. }
  62. /**
  63. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  64. */
  65. int ff_h264_check_intra4x4_pred_mode(H264Context *h){
  66. MpegEncContext * const s = &h->s;
  67. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  68. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  69. int i;
  70. if(!(h->top_samples_available&0x8000)){
  71. for(i=0; i<4; i++){
  72. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  73. if(status<0){
  74. 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);
  75. return -1;
  76. } else if(status){
  77. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  78. }
  79. }
  80. }
  81. if((h->left_samples_available&0x8888)!=0x8888){
  82. static const int mask[4]={0x8000,0x2000,0x80,0x20};
  83. for(i=0; i<4; i++){
  84. if(!(h->left_samples_available&mask[i])){
  85. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  86. if(status<0){
  87. 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);
  88. return -1;
  89. } else if(status){
  90. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  91. }
  92. }
  93. }
  94. }
  95. return 0;
  96. } //FIXME cleanup like ff_h264_check_intra_pred_mode
  97. /**
  98. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  99. */
  100. int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
  101. MpegEncContext * const s = &h->s;
  102. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  103. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  104. if(mode > 6U) {
  105. 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);
  106. return -1;
  107. }
  108. if(!(h->top_samples_available&0x8000)){
  109. mode= top[ mode ];
  110. if(mode<0){
  111. 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);
  112. return -1;
  113. }
  114. }
  115. if((h->left_samples_available&0x8080) != 0x8080){
  116. mode= left[ mode ];
  117. if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
  118. mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
  119. }
  120. if(mode<0){
  121. 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);
  122. return -1;
  123. }
  124. }
  125. return mode;
  126. }
  127. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  128. int i, si, di;
  129. uint8_t *dst;
  130. int bufidx;
  131. // src[0]&0x80; //forbidden bit
  132. h->nal_ref_idc= src[0]>>5;
  133. h->nal_unit_type= src[0]&0x1F;
  134. src++; length--;
  135. #if 0
  136. for(i=0; i<length; i++)
  137. printf("%2X ", src[i]);
  138. #endif
  139. #if HAVE_FAST_UNALIGNED
  140. # if HAVE_FAST_64BIT
  141. # define RS 7
  142. for(i=0; i+1<length; i+=9){
  143. if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  144. # else
  145. # define RS 3
  146. for(i=0; i+1<length; i+=5){
  147. if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
  148. # endif
  149. continue;
  150. if(i>0 && !src[i]) i--;
  151. while(src[i]) i++;
  152. #else
  153. # define RS 0
  154. for(i=0; i+1<length; i+=2){
  155. if(src[i]) continue;
  156. if(i>0 && src[i-1]==0) i--;
  157. #endif
  158. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  159. if(src[i+2]!=3){
  160. /* startcode, so we must be past the end */
  161. length=i;
  162. }
  163. break;
  164. }
  165. i-= RS;
  166. }
  167. if(i>=length-1){ //no escaped 0
  168. *dst_length= length;
  169. *consumed= length+1; //+1 for the header
  170. return src;
  171. }
  172. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  173. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  174. dst= h->rbsp_buffer[bufidx];
  175. if (dst == NULL){
  176. return NULL;
  177. }
  178. //printf("decoding esc\n");
  179. memcpy(dst, src, i);
  180. si=di=i;
  181. while(si+2<length){
  182. //remove escapes (very rare 1:2^22)
  183. if(src[si+2]>3){
  184. dst[di++]= src[si++];
  185. dst[di++]= src[si++];
  186. }else if(src[si]==0 && src[si+1]==0){
  187. if(src[si+2]==3){ //escape
  188. dst[di++]= 0;
  189. dst[di++]= 0;
  190. si+=3;
  191. continue;
  192. }else //next start code
  193. goto nsc;
  194. }
  195. dst[di++]= src[si++];
  196. }
  197. while(si<length)
  198. dst[di++]= src[si++];
  199. nsc:
  200. memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  201. *dst_length= di;
  202. *consumed= si + 1;//+1 for the header
  203. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  204. return dst;
  205. }
  206. /**
  207. * Identify the exact end of the bitstream
  208. * @return the length of the trailing, or 0 if damaged
  209. */
  210. static int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  211. int v= *src;
  212. int r;
  213. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  214. for(r=1; r<9; r++){
  215. if(v&1) return r;
  216. v>>=1;
  217. }
  218. return 0;
  219. }
  220. #if 0
  221. /**
  222. * DCT transforms the 16 dc values.
  223. * @param qp quantization parameter ??? FIXME
  224. */
  225. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  226. // const int qmul= dequant_coeff[qp][0];
  227. int i;
  228. int temp[16]; //FIXME check if this is a good idea
  229. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  230. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  231. for(i=0; i<4; i++){
  232. const int offset= y_offset[i];
  233. const int z0= block[offset+stride*0] + block[offset+stride*4];
  234. const int z1= block[offset+stride*0] - block[offset+stride*4];
  235. const int z2= block[offset+stride*1] - block[offset+stride*5];
  236. const int z3= block[offset+stride*1] + block[offset+stride*5];
  237. temp[4*i+0]= z0+z3;
  238. temp[4*i+1]= z1+z2;
  239. temp[4*i+2]= z1-z2;
  240. temp[4*i+3]= z0-z3;
  241. }
  242. for(i=0; i<4; i++){
  243. const int offset= x_offset[i];
  244. const int z0= temp[4*0+i] + temp[4*2+i];
  245. const int z1= temp[4*0+i] - temp[4*2+i];
  246. const int z2= temp[4*1+i] - temp[4*3+i];
  247. const int z3= temp[4*1+i] + temp[4*3+i];
  248. block[stride*0 +offset]= (z0 + z3)>>1;
  249. block[stride*2 +offset]= (z1 + z2)>>1;
  250. block[stride*8 +offset]= (z1 - z2)>>1;
  251. block[stride*10+offset]= (z0 - z3)>>1;
  252. }
  253. }
  254. #endif
  255. #undef xStride
  256. #undef stride
  257. #if 0
  258. static void chroma_dc_dct_c(DCTELEM *block){
  259. const int stride= 16*2;
  260. const int xStride= 16;
  261. int a,b,c,d,e;
  262. a= block[stride*0 + xStride*0];
  263. b= block[stride*0 + xStride*1];
  264. c= block[stride*1 + xStride*0];
  265. d= block[stride*1 + xStride*1];
  266. e= a-b;
  267. a= a+b;
  268. b= c-d;
  269. c= c+d;
  270. block[stride*0 + xStride*0]= (a+c);
  271. block[stride*0 + xStride*1]= (e+b);
  272. block[stride*1 + xStride*0]= (a-c);
  273. block[stride*1 + xStride*1]= (e-b);
  274. }
  275. #endif
  276. static void free_tables(H264Context *h, int free_rbsp){
  277. int i;
  278. H264Context *hx;
  279. av_freep(&h->intra4x4_pred_mode);
  280. av_freep(&h->chroma_pred_mode_table);
  281. av_freep(&h->cbp_table);
  282. av_freep(&h->mvd_table[0]);
  283. av_freep(&h->mvd_table[1]);
  284. av_freep(&h->direct_table);
  285. av_freep(&h->non_zero_count);
  286. av_freep(&h->slice_table_base);
  287. h->slice_table= NULL;
  288. av_freep(&h->list_counts);
  289. av_freep(&h->mb2b_xy);
  290. av_freep(&h->mb2br_xy);
  291. for(i = 0; i < MAX_THREADS; i++) {
  292. hx = h->thread_context[i];
  293. if(!hx) continue;
  294. av_freep(&hx->top_borders[1]);
  295. av_freep(&hx->top_borders[0]);
  296. av_freep(&hx->s.obmc_scratchpad);
  297. if (free_rbsp){
  298. av_freep(&hx->rbsp_buffer[1]);
  299. av_freep(&hx->rbsp_buffer[0]);
  300. hx->rbsp_buffer_size[0] = 0;
  301. hx->rbsp_buffer_size[1] = 0;
  302. }
  303. if (i) av_freep(&h->thread_context[i]);
  304. }
  305. }
  306. static void init_dequant8_coeff_table(H264Context *h){
  307. int i,q,x;
  308. const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
  309. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  310. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  311. for(i=0; i<2; i++ ){
  312. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  313. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  314. break;
  315. }
  316. for(q=0; q<max_qp+1; q++){
  317. int shift = div6[q];
  318. int idx = rem6[q];
  319. for(x=0; x<64; x++)
  320. h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
  321. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  322. h->pps.scaling_matrix8[i][x]) << shift;
  323. }
  324. }
  325. }
  326. static void init_dequant4_coeff_table(H264Context *h){
  327. int i,j,q,x;
  328. const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
  329. for(i=0; i<6; i++ ){
  330. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  331. for(j=0; j<i; j++){
  332. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  333. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  334. break;
  335. }
  336. }
  337. if(j<i)
  338. continue;
  339. for(q=0; q<max_qp+1; q++){
  340. int shift = div6[q] + 2;
  341. int idx = rem6[q];
  342. for(x=0; x<16; x++)
  343. h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
  344. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  345. h->pps.scaling_matrix4[i][x]) << shift;
  346. }
  347. }
  348. }
  349. static void init_dequant_tables(H264Context *h){
  350. int i,x;
  351. init_dequant4_coeff_table(h);
  352. if(h->pps.transform_8x8_mode)
  353. init_dequant8_coeff_table(h);
  354. if(h->sps.transform_bypass){
  355. for(i=0; i<6; i++)
  356. for(x=0; x<16; x++)
  357. h->dequant4_coeff[i][0][x] = 1<<6;
  358. if(h->pps.transform_8x8_mode)
  359. for(i=0; i<2; i++)
  360. for(x=0; x<64; x++)
  361. h->dequant8_coeff[i][0][x] = 1<<6;
  362. }
  363. }
  364. int ff_h264_alloc_tables(H264Context *h){
  365. MpegEncContext * const s = &h->s;
  366. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  367. const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
  368. int x,y;
  369. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail)
  370. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
  371. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
  372. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
  373. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
  374. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
  375. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
  376. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
  377. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
  378. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  379. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  380. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
  381. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
  382. for(y=0; y<s->mb_height; y++){
  383. for(x=0; x<s->mb_width; x++){
  384. const int mb_xy= x + y*s->mb_stride;
  385. const int b_xy = 4*x + 4*y*h->b_stride;
  386. h->mb2b_xy [mb_xy]= b_xy;
  387. h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
  388. }
  389. }
  390. s->obmc_scratchpad = NULL;
  391. if(!h->dequant4_coeff[0])
  392. init_dequant_tables(h);
  393. return 0;
  394. fail:
  395. free_tables(h, 1);
  396. return -1;
  397. }
  398. /**
  399. * Mimic alloc_tables(), but for every context thread.
  400. */
  401. static void clone_tables(H264Context *dst, H264Context *src, int i){
  402. MpegEncContext * const s = &src->s;
  403. dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
  404. dst->non_zero_count = src->non_zero_count;
  405. dst->slice_table = src->slice_table;
  406. dst->cbp_table = src->cbp_table;
  407. dst->mb2b_xy = src->mb2b_xy;
  408. dst->mb2br_xy = src->mb2br_xy;
  409. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  410. dst->mvd_table[0] = src->mvd_table[0] + i*8*2*s->mb_stride;
  411. dst->mvd_table[1] = src->mvd_table[1] + i*8*2*s->mb_stride;
  412. dst->direct_table = src->direct_table;
  413. dst->list_counts = src->list_counts;
  414. dst->s.obmc_scratchpad = NULL;
  415. ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma);
  416. }
  417. /**
  418. * Init context
  419. * Allocate buffers which are not shared amongst multiple threads.
  420. */
  421. static int context_init(H264Context *h){
  422. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
  423. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
  424. h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
  425. h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
  426. return 0;
  427. fail:
  428. return -1; // free_tables will clean up for us
  429. }
  430. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
  431. static av_cold void common_init(H264Context *h){
  432. MpegEncContext * const s = &h->s;
  433. s->width = s->avctx->width;
  434. s->height = s->avctx->height;
  435. s->codec_id= s->avctx->codec->id;
  436. ff_h264dsp_init(&h->h264dsp, 8);
  437. ff_h264_pred_init(&h->hpc, s->codec_id, 8);
  438. h->dequant_coeff_pps= -1;
  439. s->unrestricted_mv=1;
  440. s->decode=1; //FIXME
  441. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  442. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  443. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  444. }
  445. int ff_h264_decode_extradata(H264Context *h)
  446. {
  447. AVCodecContext *avctx = h->s.avctx;
  448. if(*(char *)avctx->extradata == 1){
  449. int i, cnt, nalsize;
  450. unsigned char *p = avctx->extradata;
  451. h->is_avc = 1;
  452. if(avctx->extradata_size < 7) {
  453. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  454. return -1;
  455. }
  456. /* sps and pps in the avcC always have length coded with 2 bytes,
  457. so put a fake nal_length_size = 2 while parsing them */
  458. h->nal_length_size = 2;
  459. // Decode sps from avcC
  460. cnt = *(p+5) & 0x1f; // Number of sps
  461. p += 6;
  462. for (i = 0; i < cnt; i++) {
  463. nalsize = AV_RB16(p) + 2;
  464. if(decode_nal_units(h, p, nalsize) < 0) {
  465. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  466. return -1;
  467. }
  468. p += nalsize;
  469. }
  470. // Decode pps from avcC
  471. cnt = *(p++); // Number of pps
  472. for (i = 0; i < cnt; i++) {
  473. nalsize = AV_RB16(p) + 2;
  474. if(decode_nal_units(h, p, nalsize) < 0) {
  475. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  476. return -1;
  477. }
  478. p += nalsize;
  479. }
  480. // Now store right nal length size, that will be use to parse all other nals
  481. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  482. } else {
  483. h->is_avc = 0;
  484. if(decode_nal_units(h, avctx->extradata, avctx->extradata_size) < 0)
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  490. H264Context *h= avctx->priv_data;
  491. MpegEncContext * const s = &h->s;
  492. MPV_decode_defaults(s);
  493. s->avctx = avctx;
  494. common_init(h);
  495. s->out_format = FMT_H264;
  496. s->workaround_bugs= avctx->workaround_bugs;
  497. // set defaults
  498. // s->decode_mb= ff_h263_decode_mb;
  499. s->quarter_sample = 1;
  500. if(!avctx->has_b_frames)
  501. s->low_delay= 1;
  502. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  503. ff_h264_decode_init_vlc();
  504. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  505. h->pixel_shift = 0;
  506. h->thread_context[0] = h;
  507. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  508. h->prev_poc_msb= 1<<16;
  509. h->x264_build = -1;
  510. ff_h264_reset_sei(h);
  511. if(avctx->codec_id == CODEC_ID_H264){
  512. if(avctx->ticks_per_frame == 1){
  513. s->avctx->time_base.den *=2;
  514. }
  515. avctx->ticks_per_frame = 2;
  516. }
  517. if(avctx->extradata_size > 0 && avctx->extradata &&
  518. ff_h264_decode_extradata(h))
  519. return -1;
  520. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  521. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  522. s->low_delay = 0;
  523. }
  524. return 0;
  525. }
  526. static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
  527. {
  528. int i;
  529. for (i=0; i<count; i++){
  530. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  531. }
  532. }
  533. static void copy_parameter_set(void **to, void **from, int count, int size)
  534. {
  535. int i;
  536. for (i=0; i<count; i++){
  537. if (to[i] && !from[i]) av_freep(&to[i]);
  538. else if (from[i] && !to[i]) to[i] = av_malloc(size);
  539. if (from[i]) memcpy(to[i], from[i], size);
  540. }
  541. }
  542. static int decode_init_thread_copy(AVCodecContext *avctx){
  543. H264Context *h= avctx->priv_data;
  544. if (!avctx->is_copy) return 0;
  545. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  546. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  547. return 0;
  548. }
  549. #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
  550. static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
  551. H264Context *h= dst->priv_data, *h1= src->priv_data;
  552. MpegEncContext * const s = &h->s, * const s1 = &h1->s;
  553. int inited = s->context_initialized, err;
  554. int i;
  555. if(dst == src || !s1->context_initialized) return 0;
  556. err = ff_mpeg_update_thread_context(dst, src);
  557. if(err) return err;
  558. //FIXME handle width/height changing
  559. if(!inited){
  560. for(i = 0; i < MAX_SPS_COUNT; i++)
  561. av_freep(h->sps_buffers + i);
  562. for(i = 0; i < MAX_PPS_COUNT; i++)
  563. av_freep(h->pps_buffers + i);
  564. memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
  565. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  566. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  567. ff_h264_alloc_tables(h);
  568. context_init(h);
  569. for(i=0; i<2; i++){
  570. h->rbsp_buffer[i] = NULL;
  571. h->rbsp_buffer_size[i] = 0;
  572. }
  573. h->thread_context[0] = h;
  574. // frame_start may not be called for the next thread (if it's decoding a bottom field)
  575. // so this has to be allocated here
  576. h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  577. s->dsp.clear_blocks(h->mb);
  578. }
  579. //extradata/NAL handling
  580. h->is_avc = h1->is_avc;
  581. //SPS/PPS
  582. copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
  583. h->sps = h1->sps;
  584. copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
  585. h->pps = h1->pps;
  586. //Dequantization matrices
  587. //FIXME these are big - can they be only copied when PPS changes?
  588. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  589. for(i=0; i<6; i++)
  590. h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  591. for(i=0; i<2; i++)
  592. h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  593. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  594. //POC timing
  595. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  596. //reference lists
  597. copy_fields(h, h1, ref_count, intra_gb);
  598. copy_fields(h, h1, short_ref, cabac_init_idc);
  599. copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
  600. copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
  601. copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
  602. h->last_slice_type = h1->last_slice_type;
  603. if(!s->current_picture_ptr) return 0;
  604. if(!s->dropable) {
  605. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  606. h->prev_poc_msb = h->poc_msb;
  607. h->prev_poc_lsb = h->poc_lsb;
  608. }
  609. h->prev_frame_num_offset= h->frame_num_offset;
  610. h->prev_frame_num = h->frame_num;
  611. h->outputed_poc = h->next_outputed_poc;
  612. return 0;
  613. }
  614. int ff_h264_frame_start(H264Context *h){
  615. MpegEncContext * const s = &h->s;
  616. int i;
  617. if(MPV_frame_start(s, s->avctx) < 0)
  618. return -1;
  619. ff_er_frame_start(s);
  620. /*
  621. * MPV_frame_start uses pict_type to derive key_frame.
  622. * This is incorrect for H.264; IDR markings must be used.
  623. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  624. * See decode_nal_units().
  625. */
  626. s->current_picture_ptr->key_frame= 0;
  627. s->current_picture_ptr->mmco_reset= 0;
  628. assert(s->linesize && s->uvlinesize);
  629. for(i=0; i<16; i++){
  630. h->block_offset[i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  631. h->block_offset[24+i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  632. }
  633. for(i=0; i<4; i++){
  634. h->block_offset[16+i]=
  635. h->block_offset[20+i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  636. h->block_offset[24+16+i]=
  637. h->block_offset[24+20+i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  638. }
  639. /* can't be in alloc_tables because linesize isn't known there.
  640. * FIXME: redo bipred weight to not require extra buffer? */
  641. for(i = 0; i < s->avctx->thread_count; i++)
  642. if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  643. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  644. /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
  645. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  646. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  647. // We mark the current picture as non-reference after allocating it, so
  648. // that if we break out due to an error it can be released automatically
  649. // in the next MPV_frame_start().
  650. // SVQ3 as well as most other codecs have only last/next/current and thus
  651. // get released even with set reference, besides SVQ3 and others do not
  652. // mark frames as reference later "naturally".
  653. if(s->codec_id != CODEC_ID_SVQ3)
  654. s->current_picture_ptr->reference= 0;
  655. s->current_picture_ptr->field_poc[0]=
  656. s->current_picture_ptr->field_poc[1]= INT_MAX;
  657. h->next_output_pic = NULL;
  658. assert(s->current_picture_ptr->long_ref==0);
  659. return 0;
  660. }
  661. /**
  662. * Run setup operations that must be run after slice header decoding.
  663. * This includes finding the next displayed frame.
  664. *
  665. * @param h h264 master context
  666. */
  667. static void decode_postinit(H264Context *h){
  668. MpegEncContext * const s = &h->s;
  669. Picture *out = s->current_picture_ptr;
  670. Picture *cur = s->current_picture_ptr;
  671. int i, pics, out_of_order, out_idx;
  672. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  673. s->current_picture_ptr->pict_type= s->pict_type;
  674. if (h->next_output_pic) return;
  675. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  676. //FIXME this allows the next thread to start once we encounter the first field of a PAFF packet
  677. //This works if the next packet contains the second field. It does not work if both fields are
  678. //in the same packet.
  679. //ff_thread_finish_setup(s->avctx);
  680. return;
  681. }
  682. cur->interlaced_frame = 0;
  683. cur->repeat_pict = 0;
  684. /* Signal interlacing information externally. */
  685. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  686. if(h->sps.pic_struct_present_flag){
  687. switch (h->sei_pic_struct)
  688. {
  689. case SEI_PIC_STRUCT_FRAME:
  690. break;
  691. case SEI_PIC_STRUCT_TOP_FIELD:
  692. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  693. cur->interlaced_frame = 1;
  694. break;
  695. case SEI_PIC_STRUCT_TOP_BOTTOM:
  696. case SEI_PIC_STRUCT_BOTTOM_TOP:
  697. if (FIELD_OR_MBAFF_PICTURE)
  698. cur->interlaced_frame = 1;
  699. else
  700. // try to flag soft telecine progressive
  701. cur->interlaced_frame = h->prev_interlaced_frame;
  702. break;
  703. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  704. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  705. // Signal the possibility of telecined film externally (pic_struct 5,6)
  706. // From these hints, let the applications decide if they apply deinterlacing.
  707. cur->repeat_pict = 1;
  708. break;
  709. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  710. // Force progressive here, as doubling interlaced frame is a bad idea.
  711. cur->repeat_pict = 2;
  712. break;
  713. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  714. cur->repeat_pict = 4;
  715. break;
  716. }
  717. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  718. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  719. }else{
  720. /* Derive interlacing flag from used decoding process. */
  721. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  722. }
  723. h->prev_interlaced_frame = cur->interlaced_frame;
  724. if (cur->field_poc[0] != cur->field_poc[1]){
  725. /* Derive top_field_first from field pocs. */
  726. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  727. }else{
  728. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  729. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  730. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  731. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  732. cur->top_field_first = 1;
  733. else
  734. cur->top_field_first = 0;
  735. }else{
  736. /* Most likely progressive */
  737. cur->top_field_first = 0;
  738. }
  739. }
  740. //FIXME do something with unavailable reference frames
  741. /* Sort B-frames into display order */
  742. if(h->sps.bitstream_restriction_flag
  743. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  744. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  745. s->low_delay = 0;
  746. }
  747. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  748. && !h->sps.bitstream_restriction_flag){
  749. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  750. s->low_delay= 0;
  751. }
  752. pics = 0;
  753. while(h->delayed_pic[pics]) pics++;
  754. assert(pics <= MAX_DELAYED_PIC_COUNT);
  755. h->delayed_pic[pics++] = cur;
  756. if(cur->reference == 0)
  757. cur->reference = DELAYED_PIC_REF;
  758. out = h->delayed_pic[0];
  759. out_idx = 0;
  760. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  761. if(h->delayed_pic[i]->poc < out->poc){
  762. out = h->delayed_pic[i];
  763. out_idx = i;
  764. }
  765. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  766. h->next_outputed_poc= INT_MIN;
  767. out_of_order = out->poc < h->next_outputed_poc;
  768. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  769. { }
  770. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  771. || (s->low_delay &&
  772. ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
  773. || cur->pict_type == FF_B_TYPE)))
  774. {
  775. s->low_delay = 0;
  776. s->avctx->has_b_frames++;
  777. }
  778. if(out_of_order || pics > s->avctx->has_b_frames){
  779. out->reference &= ~DELAYED_PIC_REF;
  780. out->owner2 = s; // for frame threading, the owner must be the second field's thread
  781. // or else the first thread can release the picture and reuse it unsafely
  782. for(i=out_idx; h->delayed_pic[i]; i++)
  783. h->delayed_pic[i] = h->delayed_pic[i+1];
  784. }
  785. if(!out_of_order && pics > s->avctx->has_b_frames){
  786. h->next_output_pic = out;
  787. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  788. h->next_outputed_poc = INT_MIN;
  789. } else
  790. h->next_outputed_poc = out->poc;
  791. }else{
  792. av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
  793. }
  794. ff_thread_finish_setup(s->avctx);
  795. }
  796. 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){
  797. MpegEncContext * const s = &h->s;
  798. uint8_t *top_border;
  799. int top_idx = 1;
  800. src_y -= linesize;
  801. src_cb -= uvlinesize;
  802. src_cr -= uvlinesize;
  803. if(!simple && FRAME_MBAFF){
  804. if(s->mb_y&1){
  805. if(!MB_MBAFF){
  806. top_border = h->top_borders[0][s->mb_x];
  807. AV_COPY128(top_border, src_y + 15*linesize);
  808. if (h->pixel_shift)
  809. AV_COPY128(top_border+16, src_y+15*linesize+16);
  810. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  811. if (h->pixel_shift) {
  812. AV_COPY128(top_border+32, src_cb+7*uvlinesize);
  813. AV_COPY128(top_border+48, src_cr+7*uvlinesize);
  814. } else {
  815. AV_COPY64(top_border+16, src_cb+7*uvlinesize);
  816. AV_COPY64(top_border+24, src_cr+7*uvlinesize);
  817. }
  818. }
  819. }
  820. }else if(MB_MBAFF){
  821. top_idx = 0;
  822. }else
  823. return;
  824. }
  825. top_border = h->top_borders[top_idx][s->mb_x];
  826. // There are two lines saved, the line above the the top macroblock of a pair,
  827. // and the line above the bottom macroblock
  828. AV_COPY128(top_border, src_y + 16*linesize);
  829. if (h->pixel_shift)
  830. AV_COPY128(top_border+16, src_y+16*linesize+16);
  831. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  832. if (h->pixel_shift) {
  833. AV_COPY128(top_border+32, src_cb+8*uvlinesize);
  834. AV_COPY128(top_border+48, src_cr+8*uvlinesize);
  835. } else {
  836. AV_COPY64(top_border+16, src_cb+8*uvlinesize);
  837. AV_COPY64(top_border+24, src_cr+8*uvlinesize);
  838. }
  839. }
  840. }
  841. 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, int pixel_shift){
  842. MpegEncContext * const s = &h->s;
  843. int deblock_left;
  844. int deblock_top;
  845. int top_idx = 1;
  846. uint8_t *top_border_m1;
  847. uint8_t *top_border;
  848. if(!simple && FRAME_MBAFF){
  849. if(s->mb_y&1){
  850. if(!MB_MBAFF)
  851. return;
  852. }else{
  853. top_idx = MB_MBAFF ? 0 : 1;
  854. }
  855. }
  856. if(h->deblocking_filter == 2) {
  857. deblock_left = h->left_type[0];
  858. deblock_top = h->top_type;
  859. } else {
  860. deblock_left = (s->mb_x > 0);
  861. deblock_top = (s->mb_y > !!MB_FIELD);
  862. }
  863. src_y -= linesize + 1 + pixel_shift;
  864. src_cb -= uvlinesize + 1 + pixel_shift;
  865. src_cr -= uvlinesize + 1 + pixel_shift;
  866. top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
  867. top_border = h->top_borders[top_idx][s->mb_x];
  868. #define XCHG(a,b,xchg)\
  869. if (pixel_shift) {\
  870. if (xchg) {\
  871. AV_SWAP64(b+0,a+0);\
  872. AV_SWAP64(b+8,a+8);\
  873. } else {\
  874. AV_COPY128(b,a); \
  875. }\
  876. } else \
  877. if (xchg) AV_SWAP64(b,a);\
  878. else AV_COPY64(b,a);
  879. if(deblock_top){
  880. if(deblock_left){
  881. XCHG(top_border_m1+(8<<pixel_shift), src_y -(7<<h->pixel_shift), 1);
  882. }
  883. XCHG(top_border+(0<<pixel_shift), src_y +(1<<pixel_shift), xchg);
  884. XCHG(top_border+(8<<pixel_shift), src_y +(9<<pixel_shift), 1);
  885. if(s->mb_x+1 < s->mb_width){
  886. XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +(17<<pixel_shift), 1);
  887. }
  888. }
  889. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  890. if(deblock_top){
  891. if(deblock_left){
  892. XCHG(top_border_m1+(16<<pixel_shift), src_cb -(7<<pixel_shift), 1);
  893. XCHG(top_border_m1+(24<<pixel_shift), src_cr -(7<<pixel_shift), 1);
  894. }
  895. XCHG(top_border+(16<<pixel_shift), src_cb+1+pixel_shift, 1);
  896. XCHG(top_border+(24<<pixel_shift), src_cr+1+pixel_shift, 1);
  897. }
  898. }
  899. }
  900. static av_always_inline int dctcoef_get(H264Context *h, DCTELEM *mb, int index, int pixel_shift) {
  901. if (!pixel_shift)
  902. return mb[index];
  903. else
  904. return ((int32_t*)mb)[index];
  905. }
  906. static av_always_inline void dctcoef_set(H264Context *h, DCTELEM *mb, int index, int value, int pixel_shift) {
  907. if (!pixel_shift)
  908. mb[index] = value;
  909. else
  910. ((int32_t*)mb)[index] = value;
  911. }
  912. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift){
  913. MpegEncContext * const s = &h->s;
  914. const int mb_x= s->mb_x;
  915. const int mb_y= s->mb_y;
  916. const int mb_xy= h->mb_xy;
  917. const int mb_type= s->current_picture.mb_type[mb_xy];
  918. uint8_t *dest_y, *dest_cb, *dest_cr;
  919. int linesize, uvlinesize /*dct_offset*/;
  920. int i;
  921. int *block_offset = &h->block_offset[0];
  922. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  923. /* is_h264 should always be true if SVQ3 is disabled. */
  924. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  925. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  926. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  927. dest_y = s->current_picture.data[0] + ((mb_x<<pixel_shift) + mb_y * s->linesize ) * 16;
  928. dest_cb = s->current_picture.data[1] + ((mb_x<<pixel_shift) + mb_y * s->uvlinesize) * 8;
  929. dest_cr = s->current_picture.data[2] + ((mb_x<<pixel_shift) + mb_y * s->uvlinesize) * 8;
  930. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64<<pixel_shift), s->linesize, 4);
  931. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64<<pixel_shift), dest_cr - dest_cb, 2);
  932. h->list_counts[mb_xy]= h->list_count;
  933. if (!simple && MB_FIELD) {
  934. linesize = h->mb_linesize = s->linesize * 2;
  935. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  936. block_offset = &h->block_offset[24];
  937. if(mb_y&1){ //FIXME move out of this function?
  938. dest_y -= s->linesize*15;
  939. dest_cb-= s->uvlinesize*7;
  940. dest_cr-= s->uvlinesize*7;
  941. }
  942. if(FRAME_MBAFF) {
  943. int list;
  944. for(list=0; list<h->list_count; list++){
  945. if(!USES_LIST(mb_type, list))
  946. continue;
  947. if(IS_16X16(mb_type)){
  948. int8_t *ref = &h->ref_cache[list][scan8[0]];
  949. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  950. }else{
  951. for(i=0; i<16; i+=4){
  952. int ref = h->ref_cache[list][scan8[i]];
  953. if(ref >= 0)
  954. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  955. }
  956. }
  957. }
  958. }
  959. } else {
  960. linesize = h->mb_linesize = s->linesize;
  961. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  962. // dct_offset = s->linesize * 16;
  963. }
  964. if (!simple && IS_INTRA_PCM(mb_type)) {
  965. if (pixel_shift) {
  966. const int bit_depth = h->sps.bit_depth_luma;
  967. int j;
  968. GetBitContext gb;
  969. init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
  970. for (i = 0; i < 16; i++) {
  971. uint16_t *tmp_y = (uint16_t*)(dest_y + i*linesize);
  972. for (j = 0; j < 16; j++)
  973. tmp_y[j] = get_bits(&gb, bit_depth);
  974. }
  975. for (i = 0; i < 8; i++) {
  976. uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
  977. for (j = 0; j < 8; j++)
  978. tmp_cb[j] = get_bits(&gb, bit_depth);
  979. }
  980. for (i = 0; i < 8; i++) {
  981. uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
  982. for (j = 0; j < 8; j++)
  983. tmp_cr[j] = get_bits(&gb, bit_depth);
  984. }
  985. } else {
  986. for (i=0; i<16; i++) {
  987. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  988. }
  989. for (i=0; i<8; i++) {
  990. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  991. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  992. }
  993. }
  994. } else {
  995. if(IS_INTRA(mb_type)){
  996. if(h->deblocking_filter)
  997. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple, pixel_shift);
  998. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  999. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1000. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1001. }
  1002. if(IS_INTRA4x4(mb_type)){
  1003. if(simple || !s->encoding){
  1004. if(IS_8x8DCT(mb_type)){
  1005. if(transform_bypass){
  1006. idct_dc_add =
  1007. idct_add = s->dsp.add_pixels8;
  1008. }else{
  1009. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1010. idct_add = h->h264dsp.h264_idct8_add;
  1011. }
  1012. for(i=0; i<16; i+=4){
  1013. uint8_t * const ptr= dest_y + block_offset[i];
  1014. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1015. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1016. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i*16<<pixel_shift), linesize);
  1017. }else{
  1018. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  1019. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1020. (h->topright_samples_available<<i)&0x4000, linesize);
  1021. if(nnz){
  1022. if(nnz == 1 && dctcoef_get(h, h->mb, i*16, pixel_shift))
  1023. idct_dc_add(ptr, h->mb + (i*16<<pixel_shift), linesize);
  1024. else
  1025. idct_add (ptr, h->mb + (i*16<<pixel_shift), linesize);
  1026. }
  1027. }
  1028. }
  1029. }else{
  1030. if(transform_bypass){
  1031. idct_dc_add =
  1032. idct_add = s->dsp.add_pixels4;
  1033. }else{
  1034. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1035. idct_add = h->h264dsp.h264_idct_add;
  1036. }
  1037. for(i=0; i<16; i++){
  1038. uint8_t * const ptr= dest_y + block_offset[i];
  1039. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1040. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1041. h->hpc.pred4x4_add[dir](ptr, h->mb + (i*16<<pixel_shift), linesize);
  1042. }else{
  1043. uint8_t *topright;
  1044. int nnz, tr;
  1045. uint64_t tr_high;
  1046. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1047. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1048. assert(mb_y || linesize <= block_offset[i]);
  1049. if(!topright_avail){
  1050. if (pixel_shift) {
  1051. tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
  1052. topright= (uint8_t*) &tr_high;
  1053. } else {
  1054. tr= ptr[3 - linesize]*0x01010101;
  1055. topright= (uint8_t*) &tr;
  1056. }
  1057. }else
  1058. topright= ptr + (4<<pixel_shift) - linesize;
  1059. }else
  1060. topright= NULL;
  1061. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1062. nnz = h->non_zero_count_cache[ scan8[i] ];
  1063. if(nnz){
  1064. if(is_h264){
  1065. if(nnz == 1 && dctcoef_get(h, h->mb, i*16, pixel_shift))
  1066. idct_dc_add(ptr, h->mb + (i*16<<pixel_shift), linesize);
  1067. else
  1068. idct_add (ptr, h->mb + (i*16<<pixel_shift), linesize);
  1069. }
  1070. #if CONFIG_SVQ3_DECODER
  1071. else
  1072. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1073. #endif
  1074. }
  1075. }
  1076. }
  1077. }
  1078. }
  1079. }else{
  1080. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1081. if(is_h264){
  1082. if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX] ]){
  1083. if(!transform_bypass)
  1084. h->h264dsp.h264_luma_dc_dequant_idct(h->mb, h->mb_luma_dc, h->dequant4_coeff[0][s->qscale][0]);
  1085. else{
  1086. static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
  1087. 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
  1088. for(i = 0; i < 16; i++)
  1089. dctcoef_set(h, h->mb, dc_mapping[i], dctcoef_get(h, h->mb_luma_dc, i,pixel_shift),pixel_shift);
  1090. }
  1091. }
  1092. }
  1093. #if CONFIG_SVQ3_DECODER
  1094. else
  1095. ff_svq3_luma_dc_dequant_idct_c(h->mb, h->mb_luma_dc, s->qscale);
  1096. #endif
  1097. }
  1098. if(h->deblocking_filter)
  1099. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple, pixel_shift);
  1100. }else if(is_h264){
  1101. ff_hl_motion(h, dest_y, dest_cb, dest_cr,
  1102. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1103. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1104. h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
  1105. }
  1106. if(!IS_INTRA4x4(mb_type)){
  1107. if(is_h264){
  1108. if(IS_INTRA16x16(mb_type)){
  1109. if(transform_bypass){
  1110. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1111. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  1112. }else{
  1113. for(i=0; i<16; i++){
  1114. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h, h->mb, i*16,pixel_shift))
  1115. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + (i*16<<pixel_shift), linesize);
  1116. }
  1117. }
  1118. }else{
  1119. h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1120. }
  1121. }else if(h->cbp&15){
  1122. if(transform_bypass){
  1123. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1124. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1125. for(i=0; i<16; i+=di){
  1126. if(h->non_zero_count_cache[ scan8[i] ]){
  1127. idct_add(dest_y + block_offset[i], h->mb + (i*16<<pixel_shift), linesize);
  1128. }
  1129. }
  1130. }else{
  1131. if(IS_8x8DCT(mb_type)){
  1132. h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1133. }else{
  1134. h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1135. }
  1136. }
  1137. }
  1138. }
  1139. #if CONFIG_SVQ3_DECODER
  1140. else{
  1141. for(i=0; i<16; i++){
  1142. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1143. uint8_t * const ptr= dest_y + block_offset[i];
  1144. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1145. }
  1146. }
  1147. }
  1148. #endif
  1149. }
  1150. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1151. uint8_t *dest[2] = {dest_cb, dest_cr};
  1152. if(transform_bypass){
  1153. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1154. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + (16*16<<pixel_shift), uvlinesize);
  1155. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + (20*16<<pixel_shift), uvlinesize);
  1156. }else{
  1157. idct_add = s->dsp.add_pixels4;
  1158. for(i=16; i<16+8; i++){
  1159. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h, h->mb, i*16,pixel_shift))
  1160. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + (i*16<<pixel_shift), uvlinesize);
  1161. }
  1162. }
  1163. }else{
  1164. if(is_h264){
  1165. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
  1166. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16<<pixel_shift) , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  1167. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
  1168. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + ((16*16+4*16)<<pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  1169. h->h264dsp.h264_idct_add8(dest, block_offset,
  1170. h->mb, uvlinesize,
  1171. h->non_zero_count_cache);
  1172. }
  1173. #if CONFIG_SVQ3_DECODER
  1174. else{
  1175. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16 , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  1176. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  1177. for(i=16; i<16+8; i++){
  1178. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1179. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  1180. ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
  1181. }
  1182. }
  1183. }
  1184. #endif
  1185. }
  1186. }
  1187. }
  1188. if(h->cbp || IS_INTRA(mb_type))
  1189. s->dsp.clear_blocks(h->mb);
  1190. }
  1191. /**
  1192. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1193. */
  1194. static void hl_decode_mb_simple8(H264Context *h){
  1195. hl_decode_mb_internal(h, 1, 0);
  1196. }
  1197. /**
  1198. * Process a macroblock; this handles edge cases, such as interlacing.
  1199. */
  1200. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1201. hl_decode_mb_internal(h, 0, h->pixel_shift);
  1202. }
  1203. void ff_h264_hl_decode_mb(H264Context *h){
  1204. MpegEncContext * const s = &h->s;
  1205. const int mb_xy= h->mb_xy;
  1206. const int mb_type= s->current_picture.mb_type[mb_xy];
  1207. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1208. if (is_complex || h->pixel_shift)
  1209. hl_decode_mb_complex(h);
  1210. else{
  1211. hl_decode_mb_simple8(h);
  1212. }
  1213. }
  1214. static int pred_weight_table(H264Context *h){
  1215. MpegEncContext * const s = &h->s;
  1216. int list, i;
  1217. int luma_def, chroma_def;
  1218. h->use_weight= 0;
  1219. h->use_weight_chroma= 0;
  1220. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1221. if(CHROMA)
  1222. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1223. luma_def = 1<<h->luma_log2_weight_denom;
  1224. chroma_def = 1<<h->chroma_log2_weight_denom;
  1225. for(list=0; list<2; list++){
  1226. h->luma_weight_flag[list] = 0;
  1227. h->chroma_weight_flag[list] = 0;
  1228. for(i=0; i<h->ref_count[list]; i++){
  1229. int luma_weight_flag, chroma_weight_flag;
  1230. luma_weight_flag= get_bits1(&s->gb);
  1231. if(luma_weight_flag){
  1232. h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
  1233. h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
  1234. if( h->luma_weight[i][list][0] != luma_def
  1235. || h->luma_weight[i][list][1] != 0) {
  1236. h->use_weight= 1;
  1237. h->luma_weight_flag[list]= 1;
  1238. }
  1239. }else{
  1240. h->luma_weight[i][list][0]= luma_def;
  1241. h->luma_weight[i][list][1]= 0;
  1242. }
  1243. if(CHROMA){
  1244. chroma_weight_flag= get_bits1(&s->gb);
  1245. if(chroma_weight_flag){
  1246. int j;
  1247. for(j=0; j<2; j++){
  1248. h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
  1249. h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
  1250. if( h->chroma_weight[i][list][j][0] != chroma_def
  1251. || h->chroma_weight[i][list][j][1] != 0) {
  1252. h->use_weight_chroma= 1;
  1253. h->chroma_weight_flag[list]= 1;
  1254. }
  1255. }
  1256. }else{
  1257. int j;
  1258. for(j=0; j<2; j++){
  1259. h->chroma_weight[i][list][j][0]= chroma_def;
  1260. h->chroma_weight[i][list][j][1]= 0;
  1261. }
  1262. }
  1263. }
  1264. }
  1265. if(h->slice_type_nos != FF_B_TYPE) break;
  1266. }
  1267. h->use_weight= h->use_weight || h->use_weight_chroma;
  1268. return 0;
  1269. }
  1270. /**
  1271. * Initialize implicit_weight table.
  1272. * @param field 0/1 initialize the weight for interlaced MBAFF
  1273. * -1 initializes the rest
  1274. */
  1275. static void implicit_weight_table(H264Context *h, int field){
  1276. MpegEncContext * const s = &h->s;
  1277. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  1278. for (i = 0; i < 2; i++) {
  1279. h->luma_weight_flag[i] = 0;
  1280. h->chroma_weight_flag[i] = 0;
  1281. }
  1282. if(field < 0){
  1283. cur_poc = s->current_picture_ptr->poc;
  1284. if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
  1285. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1286. h->use_weight= 0;
  1287. h->use_weight_chroma= 0;
  1288. return;
  1289. }
  1290. ref_start= 0;
  1291. ref_count0= h->ref_count[0];
  1292. ref_count1= h->ref_count[1];
  1293. }else{
  1294. cur_poc = s->current_picture_ptr->field_poc[field];
  1295. ref_start= 16;
  1296. ref_count0= 16+2*h->ref_count[0];
  1297. ref_count1= 16+2*h->ref_count[1];
  1298. }
  1299. h->use_weight= 2;
  1300. h->use_weight_chroma= 2;
  1301. h->luma_log2_weight_denom= 5;
  1302. h->chroma_log2_weight_denom= 5;
  1303. for(ref0=ref_start; ref0 < ref_count0; ref0++){
  1304. int poc0 = h->ref_list[0][ref0].poc;
  1305. for(ref1=ref_start; ref1 < ref_count1; ref1++){
  1306. int poc1 = h->ref_list[1][ref1].poc;
  1307. int td = av_clip(poc1 - poc0, -128, 127);
  1308. int w= 32;
  1309. if(td){
  1310. int tb = av_clip(cur_poc - poc0, -128, 127);
  1311. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1312. int dist_scale_factor = (tb*tx + 32) >> 8;
  1313. if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
  1314. w = 64 - dist_scale_factor;
  1315. }
  1316. if(field<0){
  1317. h->implicit_weight[ref0][ref1][0]=
  1318. h->implicit_weight[ref0][ref1][1]= w;
  1319. }else{
  1320. h->implicit_weight[ref0][ref1][field]=w;
  1321. }
  1322. }
  1323. }
  1324. }
  1325. /**
  1326. * instantaneous decoder refresh.
  1327. */
  1328. static void idr(H264Context *h){
  1329. ff_h264_remove_all_refs(h);
  1330. h->prev_frame_num= 0;
  1331. h->prev_frame_num_offset= 0;
  1332. h->prev_poc_msb=
  1333. h->prev_poc_lsb= 0;
  1334. }
  1335. /* forget old pics after a seek */
  1336. static void flush_dpb(AVCodecContext *avctx){
  1337. H264Context *h= avctx->priv_data;
  1338. int i;
  1339. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  1340. if(h->delayed_pic[i])
  1341. h->delayed_pic[i]->reference= 0;
  1342. h->delayed_pic[i]= NULL;
  1343. }
  1344. h->outputed_poc=h->next_outputed_poc= INT_MIN;
  1345. h->prev_interlaced_frame = 1;
  1346. idr(h);
  1347. if(h->s.current_picture_ptr)
  1348. h->s.current_picture_ptr->reference= 0;
  1349. h->s.first_field= 0;
  1350. ff_h264_reset_sei(h);
  1351. ff_mpeg_flush(avctx);
  1352. }
  1353. static int init_poc(H264Context *h){
  1354. MpegEncContext * const s = &h->s;
  1355. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  1356. int field_poc[2];
  1357. Picture *cur = s->current_picture_ptr;
  1358. h->frame_num_offset= h->prev_frame_num_offset;
  1359. if(h->frame_num < h->prev_frame_num)
  1360. h->frame_num_offset += max_frame_num;
  1361. if(h->sps.poc_type==0){
  1362. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  1363. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  1364. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  1365. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  1366. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  1367. else
  1368. h->poc_msb = h->prev_poc_msb;
  1369. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  1370. field_poc[0] =
  1371. field_poc[1] = h->poc_msb + h->poc_lsb;
  1372. if(s->picture_structure == PICT_FRAME)
  1373. field_poc[1] += h->delta_poc_bottom;
  1374. }else if(h->sps.poc_type==1){
  1375. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  1376. int i;
  1377. if(h->sps.poc_cycle_length != 0)
  1378. abs_frame_num = h->frame_num_offset + h->frame_num;
  1379. else
  1380. abs_frame_num = 0;
  1381. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  1382. abs_frame_num--;
  1383. expected_delta_per_poc_cycle = 0;
  1384. for(i=0; i < h->sps.poc_cycle_length; i++)
  1385. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  1386. if(abs_frame_num > 0){
  1387. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  1388. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  1389. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  1390. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  1391. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  1392. } else
  1393. expectedpoc = 0;
  1394. if(h->nal_ref_idc == 0)
  1395. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  1396. field_poc[0] = expectedpoc + h->delta_poc[0];
  1397. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  1398. if(s->picture_structure == PICT_FRAME)
  1399. field_poc[1] += h->delta_poc[1];
  1400. }else{
  1401. int poc= 2*(h->frame_num_offset + h->frame_num);
  1402. if(!h->nal_ref_idc)
  1403. poc--;
  1404. field_poc[0]= poc;
  1405. field_poc[1]= poc;
  1406. }
  1407. if(s->picture_structure != PICT_BOTTOM_FIELD)
  1408. s->current_picture_ptr->field_poc[0]= field_poc[0];
  1409. if(s->picture_structure != PICT_TOP_FIELD)
  1410. s->current_picture_ptr->field_poc[1]= field_poc[1];
  1411. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  1412. return 0;
  1413. }
  1414. /**
  1415. * initialize scan tables
  1416. */
  1417. static void init_scan_tables(H264Context *h){
  1418. int i;
  1419. for(i=0; i<16; i++){
  1420. #define T(x) (x>>2) | ((x<<2) & 0xF)
  1421. h->zigzag_scan[i] = T(zigzag_scan[i]);
  1422. h-> field_scan[i] = T( field_scan[i]);
  1423. #undef T
  1424. }
  1425. for(i=0; i<64; i++){
  1426. #define T(x) (x>>3) | ((x&7)<<3)
  1427. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  1428. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  1429. h->field_scan8x8[i] = T(field_scan8x8[i]);
  1430. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  1431. #undef T
  1432. }
  1433. if(h->sps.transform_bypass){ //FIXME same ugly
  1434. h->zigzag_scan_q0 = zigzag_scan;
  1435. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  1436. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  1437. h->field_scan_q0 = field_scan;
  1438. h->field_scan8x8_q0 = field_scan8x8;
  1439. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  1440. }else{
  1441. h->zigzag_scan_q0 = h->zigzag_scan;
  1442. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  1443. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  1444. h->field_scan_q0 = h->field_scan;
  1445. h->field_scan8x8_q0 = h->field_scan8x8;
  1446. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  1447. }
  1448. }
  1449. static void field_end(H264Context *h, int in_setup){
  1450. MpegEncContext * const s = &h->s;
  1451. AVCodecContext * const avctx= s->avctx;
  1452. s->mb_y= 0;
  1453. if (!in_setup && !s->dropable)
  1454. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
  1455. s->picture_structure==PICT_BOTTOM_FIELD);
  1456. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1457. ff_vdpau_h264_set_reference_frames(s);
  1458. if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
  1459. if(!s->dropable) {
  1460. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1461. h->prev_poc_msb= h->poc_msb;
  1462. h->prev_poc_lsb= h->poc_lsb;
  1463. }
  1464. h->prev_frame_num_offset= h->frame_num_offset;
  1465. h->prev_frame_num= h->frame_num;
  1466. h->outputed_poc = h->next_outputed_poc;
  1467. }
  1468. if (avctx->hwaccel) {
  1469. if (avctx->hwaccel->end_frame(avctx) < 0)
  1470. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1471. }
  1472. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1473. ff_vdpau_h264_picture_complete(s);
  1474. /*
  1475. * FIXME: Error handling code does not seem to support interlaced
  1476. * when slices span multiple rows
  1477. * The ff_er_add_slice calls don't work right for bottom
  1478. * fields; they cause massive erroneous error concealing
  1479. * Error marking covers both fields (top and bottom).
  1480. * This causes a mismatched s->error_count
  1481. * and a bad error table. Further, the error count goes to
  1482. * INT_MAX when called for bottom field, because mb_y is
  1483. * past end by one (callers fault) and resync_mb_y != 0
  1484. * causes problems for the first MB line, too.
  1485. */
  1486. if (!FIELD_PICTURE)
  1487. ff_er_frame_end(s);
  1488. MPV_frame_end(s);
  1489. h->current_slice=0;
  1490. }
  1491. /**
  1492. * Replicate H264 "master" context to thread contexts.
  1493. */
  1494. static void clone_slice(H264Context *dst, H264Context *src)
  1495. {
  1496. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  1497. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  1498. dst->s.current_picture = src->s.current_picture;
  1499. dst->s.linesize = src->s.linesize;
  1500. dst->s.uvlinesize = src->s.uvlinesize;
  1501. dst->s.first_field = src->s.first_field;
  1502. dst->prev_poc_msb = src->prev_poc_msb;
  1503. dst->prev_poc_lsb = src->prev_poc_lsb;
  1504. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  1505. dst->prev_frame_num = src->prev_frame_num;
  1506. dst->short_ref_count = src->short_ref_count;
  1507. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  1508. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  1509. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  1510. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  1511. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  1512. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  1513. }
  1514. /**
  1515. * computes profile from profile_idc and constraint_set?_flags
  1516. *
  1517. * @param sps SPS
  1518. *
  1519. * @return profile as defined by FF_PROFILE_H264_*
  1520. */
  1521. int ff_h264_get_profile(SPS *sps)
  1522. {
  1523. int profile = sps->profile_idc;
  1524. switch(sps->profile_idc) {
  1525. case FF_PROFILE_H264_BASELINE:
  1526. // constraint_set1_flag set to 1
  1527. profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  1528. break;
  1529. case FF_PROFILE_H264_HIGH_10:
  1530. case FF_PROFILE_H264_HIGH_422:
  1531. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  1532. // constraint_set3_flag set to 1
  1533. profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
  1534. break;
  1535. }
  1536. return profile;
  1537. }
  1538. /**
  1539. * decodes a slice header.
  1540. * This will also call MPV_common_init() and frame_start() as needed.
  1541. *
  1542. * @param h h264context
  1543. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  1544. *
  1545. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  1546. */
  1547. static int decode_slice_header(H264Context *h, H264Context *h0){
  1548. MpegEncContext * const s = &h->s;
  1549. MpegEncContext * const s0 = &h0->s;
  1550. unsigned int first_mb_in_slice;
  1551. unsigned int pps_id;
  1552. int num_ref_idx_active_override_flag;
  1553. unsigned int slice_type, tmp, i, j;
  1554. int default_ref_list_done = 0;
  1555. int last_pic_structure;
  1556. s->dropable= h->nal_ref_idc == 0;
  1557. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  1558. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  1559. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  1560. }else{
  1561. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  1562. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  1563. }
  1564. first_mb_in_slice= get_ue_golomb(&s->gb);
  1565. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  1566. if(h0->current_slice && FIELD_PICTURE){
  1567. field_end(h, 1);
  1568. }
  1569. h0->current_slice = 0;
  1570. if (!s0->first_field)
  1571. s->current_picture_ptr= NULL;
  1572. }
  1573. slice_type= get_ue_golomb_31(&s->gb);
  1574. if(slice_type > 9){
  1575. 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);
  1576. return -1;
  1577. }
  1578. if(slice_type > 4){
  1579. slice_type -= 5;
  1580. h->slice_type_fixed=1;
  1581. }else
  1582. h->slice_type_fixed=0;
  1583. slice_type= golomb_to_pict_type[ slice_type ];
  1584. if (slice_type == FF_I_TYPE
  1585. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  1586. default_ref_list_done = 1;
  1587. }
  1588. h->slice_type= slice_type;
  1589. h->slice_type_nos= slice_type & 3;
  1590. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  1591. pps_id= get_ue_golomb(&s->gb);
  1592. if(pps_id>=MAX_PPS_COUNT){
  1593. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  1594. return -1;
  1595. }
  1596. if(!h0->pps_buffers[pps_id]) {
  1597. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  1598. return -1;
  1599. }
  1600. h->pps= *h0->pps_buffers[pps_id];
  1601. if(!h0->sps_buffers[h->pps.sps_id]) {
  1602. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  1603. return -1;
  1604. }
  1605. h->sps = *h0->sps_buffers[h->pps.sps_id];
  1606. s->avctx->profile = ff_h264_get_profile(&h->sps);
  1607. s->avctx->level = h->sps.level_idc;
  1608. s->avctx->refs = h->sps.ref_frame_count;
  1609. if(h == h0 && h->dequant_coeff_pps != pps_id){
  1610. h->dequant_coeff_pps = pps_id;
  1611. init_dequant_tables(h);
  1612. }
  1613. s->mb_width= h->sps.mb_width;
  1614. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  1615. h->b_stride= s->mb_width*4;
  1616. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  1617. if(h->sps.frame_mbs_only_flag)
  1618. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  1619. else
  1620. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 7);
  1621. if (s->context_initialized
  1622. && ( s->width != s->avctx->width || s->height != s->avctx->height
  1623. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
  1624. if(h != h0) {
  1625. av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
  1626. return -1; // width / height changed during parallelized decoding
  1627. }
  1628. free_tables(h, 0);
  1629. flush_dpb(s->avctx);
  1630. MPV_common_end(s);
  1631. }
  1632. if (!s->context_initialized) {
  1633. if(h != h0){
  1634. av_log(h->s.avctx, AV_LOG_ERROR, "we cant (re-)initialize context during parallel decoding\n");
  1635. return -1;
  1636. }
  1637. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1638. s->avctx->sample_aspect_ratio= h->sps.sar;
  1639. av_assert0(s->avctx->sample_aspect_ratio.den);
  1640. if(h->sps.video_signal_type_present_flag){
  1641. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  1642. if(h->sps.colour_description_present_flag){
  1643. s->avctx->color_primaries = h->sps.color_primaries;
  1644. s->avctx->color_trc = h->sps.color_trc;
  1645. s->avctx->colorspace = h->sps.colorspace;
  1646. }
  1647. }
  1648. if(h->sps.timing_info_present_flag){
  1649. int64_t den= h->sps.time_scale;
  1650. if(h->x264_build < 44U)
  1651. den *= 2;
  1652. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  1653. h->sps.num_units_in_tick, den, 1<<30);
  1654. }
  1655. switch (h->sps.bit_depth_luma) {
  1656. case 9 :
  1657. s->avctx->pix_fmt = PIX_FMT_YUV420P9;
  1658. break;
  1659. case 10 :
  1660. s->avctx->pix_fmt = PIX_FMT_YUV420P10;
  1661. break;
  1662. default:
  1663. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  1664. s->avctx->codec->pix_fmts ?
  1665. s->avctx->codec->pix_fmts :
  1666. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  1667. hwaccel_pixfmt_list_h264_jpeg_420 :
  1668. ff_hwaccel_pixfmt_list_420);
  1669. }
  1670. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  1671. if (MPV_common_init(s) < 0){
  1672. av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed\n");
  1673. return -1;
  1674. }
  1675. s->first_field = 0;
  1676. h->prev_interlaced_frame = 1;
  1677. init_scan_tables(h);
  1678. ff_h264_alloc_tables(h);
  1679. if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  1680. if (context_init(h) < 0){
  1681. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
  1682. return -1;
  1683. }
  1684. } else {
  1685. for(i = 1; i < s->avctx->thread_count; i++) {
  1686. H264Context *c;
  1687. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  1688. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  1689. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  1690. c->h264dsp = h->h264dsp;
  1691. c->sps = h->sps;
  1692. c->pps = h->pps;
  1693. init_scan_tables(c);
  1694. clone_tables(c, h, i);
  1695. }
  1696. for(i = 0; i < s->avctx->thread_count; i++)
  1697. if(context_init(h->thread_context[i]) < 0){
  1698. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
  1699. return -1;
  1700. }
  1701. }
  1702. }
  1703. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  1704. h->mb_mbaff = 0;
  1705. h->mb_aff_frame = 0;
  1706. last_pic_structure = s0->picture_structure;
  1707. if(h->sps.frame_mbs_only_flag){
  1708. s->picture_structure= PICT_FRAME;
  1709. }else{
  1710. if(get_bits1(&s->gb)) { //field_pic_flag
  1711. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  1712. } else {
  1713. s->picture_structure= PICT_FRAME;
  1714. h->mb_aff_frame = h->sps.mb_aff;
  1715. }
  1716. }
  1717. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  1718. if(h0->current_slice == 0){
  1719. if(h->frame_num != h->prev_frame_num &&
  1720. (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num) < (h->frame_num - h->sps.ref_frame_count))
  1721. h->prev_frame_num = h->frame_num - h->sps.ref_frame_count - 1;
  1722. while(h->frame_num != h->prev_frame_num &&
  1723. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  1724. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1725. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  1726. if (ff_h264_frame_start(h) < 0)
  1727. return -1;
  1728. h->prev_frame_num++;
  1729. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  1730. s->current_picture_ptr->frame_num= h->prev_frame_num;
  1731. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
  1732. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
  1733. ff_generate_sliding_window_mmcos(h);
  1734. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1735. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  1736. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  1737. * about there being no actual duplicates.
  1738. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  1739. * concealing a lost frame, this probably isn't noticable by comparison, but it should
  1740. * be fixed. */
  1741. if (h->short_ref_count) {
  1742. if (prev) {
  1743. av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
  1744. (const uint8_t**)prev->data, prev->linesize,
  1745. s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
  1746. h->short_ref[0]->poc = prev->poc+2;
  1747. }
  1748. h->short_ref[0]->frame_num = h->prev_frame_num;
  1749. }
  1750. }
  1751. /* See if we have a decoded first field looking for a pair... */
  1752. if (s0->first_field) {
  1753. assert(s0->current_picture_ptr);
  1754. assert(s0->current_picture_ptr->data[0]);
  1755. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  1756. /* figure out if we have a complementary field pair */
  1757. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  1758. /*
  1759. * Previous field is unmatched. Don't display it, but let it
  1760. * remain for reference if marked as such.
  1761. */
  1762. s0->current_picture_ptr = NULL;
  1763. s0->first_field = FIELD_PICTURE;
  1764. } else {
  1765. if (h->nal_ref_idc &&
  1766. s0->current_picture_ptr->reference &&
  1767. s0->current_picture_ptr->frame_num != h->frame_num) {
  1768. /*
  1769. * This and previous field were reference, but had
  1770. * different frame_nums. Consider this field first in
  1771. * pair. Throw away previous field except for reference
  1772. * purposes.
  1773. */
  1774. s0->first_field = 1;
  1775. s0->current_picture_ptr = NULL;
  1776. } else {
  1777. /* Second field in complementary pair */
  1778. s0->first_field = 0;
  1779. }
  1780. }
  1781. } else {
  1782. /* Frame or first field in a potentially complementary pair */
  1783. assert(!s0->current_picture_ptr);
  1784. s0->first_field = FIELD_PICTURE;
  1785. }
  1786. if(!FIELD_PICTURE || s0->first_field) {
  1787. if (ff_h264_frame_start(h) < 0) {
  1788. s0->first_field = 0;
  1789. return -1;
  1790. }
  1791. } else {
  1792. ff_release_unused_pictures(s, 0);
  1793. }
  1794. }
  1795. if(h != h0)
  1796. clone_slice(h, h0);
  1797. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  1798. assert(s->mb_num == s->mb_width * s->mb_height);
  1799. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  1800. first_mb_in_slice >= s->mb_num){
  1801. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1802. return -1;
  1803. }
  1804. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  1805. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  1806. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1807. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  1808. assert(s->mb_y < s->mb_height);
  1809. if(s->picture_structure==PICT_FRAME){
  1810. h->curr_pic_num= h->frame_num;
  1811. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  1812. }else{
  1813. h->curr_pic_num= 2*h->frame_num + 1;
  1814. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  1815. }
  1816. if(h->nal_unit_type == NAL_IDR_SLICE){
  1817. get_ue_golomb(&s->gb); /* idr_pic_id */
  1818. }
  1819. if(h->sps.poc_type==0){
  1820. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  1821. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  1822. h->delta_poc_bottom= get_se_golomb(&s->gb);
  1823. }
  1824. }
  1825. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  1826. h->delta_poc[0]= get_se_golomb(&s->gb);
  1827. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  1828. h->delta_poc[1]= get_se_golomb(&s->gb);
  1829. }
  1830. init_poc(h);
  1831. if(h->pps.redundant_pic_cnt_present){
  1832. h->redundant_pic_count= get_ue_golomb(&s->gb);
  1833. }
  1834. //set defaults, might be overridden a few lines later
  1835. h->ref_count[0]= h->pps.ref_count[0];
  1836. h->ref_count[1]= h->pps.ref_count[1];
  1837. if(h->slice_type_nos != FF_I_TYPE){
  1838. if(h->slice_type_nos == FF_B_TYPE){
  1839. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  1840. }
  1841. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  1842. if(num_ref_idx_active_override_flag){
  1843. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  1844. if(h->slice_type_nos==FF_B_TYPE)
  1845. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  1846. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  1847. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  1848. h->ref_count[0]= h->ref_count[1]= 1;
  1849. return -1;
  1850. }
  1851. }
  1852. if(h->slice_type_nos == FF_B_TYPE)
  1853. h->list_count= 2;
  1854. else
  1855. h->list_count= 1;
  1856. }else
  1857. h->list_count= 0;
  1858. if(!default_ref_list_done){
  1859. ff_h264_fill_default_ref_list(h);
  1860. }
  1861. if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  1862. return -1;
  1863. if(h->slice_type_nos!=FF_I_TYPE){
  1864. s->last_picture_ptr= &h->ref_list[0][0];
  1865. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1866. }
  1867. if(h->slice_type_nos==FF_B_TYPE){
  1868. s->next_picture_ptr= &h->ref_list[1][0];
  1869. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1870. }
  1871. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  1872. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  1873. pred_weight_table(h);
  1874. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
  1875. implicit_weight_table(h, -1);
  1876. }else {
  1877. h->use_weight = 0;
  1878. for (i = 0; i < 2; i++) {
  1879. h->luma_weight_flag[i] = 0;
  1880. h->chroma_weight_flag[i] = 0;
  1881. }
  1882. }
  1883. if(h->nal_ref_idc)
  1884. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  1885. if(FRAME_MBAFF){
  1886. ff_h264_fill_mbaff_ref_list(h);
  1887. if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
  1888. implicit_weight_table(h, 0);
  1889. implicit_weight_table(h, 1);
  1890. }
  1891. }
  1892. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  1893. ff_h264_direct_dist_scale_factor(h);
  1894. ff_h264_direct_ref_list_init(h);
  1895. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  1896. tmp = get_ue_golomb_31(&s->gb);
  1897. if(tmp > 2){
  1898. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  1899. return -1;
  1900. }
  1901. h->cabac_init_idc= tmp;
  1902. }
  1903. h->last_qscale_diff = 0;
  1904. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  1905. if(tmp>51+6*(h->sps.bit_depth_luma-8)){
  1906. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1907. return -1;
  1908. }
  1909. s->qscale= tmp;
  1910. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  1911. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  1912. //FIXME qscale / qp ... stuff
  1913. if(h->slice_type == FF_SP_TYPE){
  1914. get_bits1(&s->gb); /* sp_for_switch_flag */
  1915. }
  1916. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  1917. get_se_golomb(&s->gb); /* slice_qs_delta */
  1918. }
  1919. h->deblocking_filter = 1;
  1920. h->slice_alpha_c0_offset = 52;
  1921. h->slice_beta_offset = 52;
  1922. if( h->pps.deblocking_filter_parameters_present ) {
  1923. tmp= get_ue_golomb_31(&s->gb);
  1924. if(tmp > 2){
  1925. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  1926. return -1;
  1927. }
  1928. h->deblocking_filter= tmp;
  1929. if(h->deblocking_filter < 2)
  1930. h->deblocking_filter^= 1; // 1<->0
  1931. if( h->deblocking_filter ) {
  1932. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  1933. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  1934. if( h->slice_alpha_c0_offset > 104U
  1935. || h->slice_beta_offset > 104U){
  1936. av_log(s->avctx, AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", h->slice_alpha_c0_offset, h->slice_beta_offset);
  1937. return -1;
  1938. }
  1939. }
  1940. }
  1941. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  1942. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  1943. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  1944. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  1945. h->deblocking_filter= 0;
  1946. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  1947. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  1948. /* Cheat slightly for speed:
  1949. Do not bother to deblock across slices. */
  1950. h->deblocking_filter = 2;
  1951. } else {
  1952. h0->max_contexts = 1;
  1953. if(!h0->single_decode_warning) {
  1954. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  1955. h0->single_decode_warning = 1;
  1956. }
  1957. if(h != h0){
  1958. av_log(h->s.avctx, AV_LOG_ERROR, "deblocking switched inside frame\n");
  1959. return 1;
  1960. }
  1961. }
  1962. }
  1963. h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
  1964. #if 0 //FMO
  1965. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  1966. slice_group_change_cycle= get_bits(&s->gb, ?);
  1967. #endif
  1968. h0->last_slice_type = slice_type;
  1969. h->slice_num = ++h0->current_slice;
  1970. if(h->slice_num >= MAX_SLICES){
  1971. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  1972. }
  1973. for(j=0; j<2; j++){
  1974. int id_list[16];
  1975. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  1976. for(i=0; i<16; i++){
  1977. id_list[i]= 60;
  1978. if(h->ref_list[j][i].data[0]){
  1979. int k;
  1980. uint8_t *base= h->ref_list[j][i].base[0];
  1981. for(k=0; k<h->short_ref_count; k++)
  1982. if(h->short_ref[k]->base[0] == base){
  1983. id_list[i]= k;
  1984. break;
  1985. }
  1986. for(k=0; k<h->long_ref_count; k++)
  1987. if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
  1988. id_list[i]= h->short_ref_count + k;
  1989. break;
  1990. }
  1991. }
  1992. }
  1993. ref2frm[0]=
  1994. ref2frm[1]= -1;
  1995. for(i=0; i<16; i++)
  1996. ref2frm[i+2]= 4*id_list[i]
  1997. +(h->ref_list[j][i].reference&3);
  1998. ref2frm[18+0]=
  1999. ref2frm[18+1]= -1;
  2000. for(i=16; i<48; i++)
  2001. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  2002. +(h->ref_list[j][i].reference&3);
  2003. }
  2004. //FIXME: fix draw_edges+PAFF+frame threads
  2005. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type&FF_THREAD_FRAME)) ? 0 : 16;
  2006. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2007. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2008. 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",
  2009. h->slice_num,
  2010. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  2011. first_mb_in_slice,
  2012. av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2013. pps_id, h->frame_num,
  2014. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2015. h->ref_count[0], h->ref_count[1],
  2016. s->qscale,
  2017. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  2018. h->use_weight,
  2019. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  2020. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  2021. );
  2022. }
  2023. return 0;
  2024. }
  2025. int ff_h264_get_slice_type(const H264Context *h)
  2026. {
  2027. switch (h->slice_type) {
  2028. case FF_P_TYPE: return 0;
  2029. case FF_B_TYPE: return 1;
  2030. case FF_I_TYPE: return 2;
  2031. case FF_SP_TYPE: return 3;
  2032. case FF_SI_TYPE: return 4;
  2033. default: return -1;
  2034. }
  2035. }
  2036. /**
  2037. *
  2038. * @return non zero if the loop filter can be skiped
  2039. */
  2040. static int fill_filter_caches(H264Context *h, int mb_type){
  2041. MpegEncContext * const s = &h->s;
  2042. const int mb_xy= h->mb_xy;
  2043. int top_xy, left_xy[2];
  2044. int top_type, left_type[2];
  2045. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  2046. //FIXME deblocking could skip the intra and nnz parts.
  2047. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  2048. * stuff, I can't imagine that these complex rules are worth it. */
  2049. left_xy[1] = left_xy[0] = mb_xy-1;
  2050. if(FRAME_MBAFF){
  2051. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
  2052. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  2053. if(s->mb_y&1){
  2054. if (left_mb_field_flag != curr_mb_field_flag) {
  2055. left_xy[0] -= s->mb_stride;
  2056. }
  2057. }else{
  2058. if(curr_mb_field_flag){
  2059. top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
  2060. }
  2061. if (left_mb_field_flag != curr_mb_field_flag) {
  2062. left_xy[1] += s->mb_stride;
  2063. }
  2064. }
  2065. }
  2066. h->top_mb_xy = top_xy;
  2067. h->left_mb_xy[0] = left_xy[0];
  2068. h->left_mb_xy[1] = left_xy[1];
  2069. {
  2070. //for sufficiently low qp, filtering wouldn't do anything
  2071. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  2072. int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
  2073. int qp = s->current_picture.qscale_table[mb_xy];
  2074. if(qp <= qp_thresh
  2075. && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
  2076. && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
  2077. if(!FRAME_MBAFF)
  2078. return 1;
  2079. if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
  2080. && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
  2081. return 1;
  2082. }
  2083. }
  2084. top_type = s->current_picture.mb_type[top_xy] ;
  2085. left_type[0] = s->current_picture.mb_type[left_xy[0]];
  2086. left_type[1] = s->current_picture.mb_type[left_xy[1]];
  2087. if(h->deblocking_filter == 2){
  2088. if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
  2089. if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
  2090. }else{
  2091. if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
  2092. if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
  2093. }
  2094. h->top_type = top_type ;
  2095. h->left_type[0]= left_type[0];
  2096. h->left_type[1]= left_type[1];
  2097. if(IS_INTRA(mb_type))
  2098. return 0;
  2099. AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
  2100. AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
  2101. AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
  2102. AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
  2103. AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
  2104. h->cbp= h->cbp_table[mb_xy];
  2105. {
  2106. int list;
  2107. for(list=0; list<h->list_count; list++){
  2108. int8_t *ref;
  2109. int y, b_stride;
  2110. int16_t (*mv_dst)[2];
  2111. int16_t (*mv_src)[2];
  2112. if(!USES_LIST(mb_type, list)){
  2113. fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
  2114. AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2115. AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2116. AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2117. AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2118. continue;
  2119. }
  2120. ref = &s->current_picture.ref_index[list][4*mb_xy];
  2121. {
  2122. int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2123. AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2124. AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2125. ref += 2;
  2126. AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2127. AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2128. }
  2129. b_stride = h->b_stride;
  2130. mv_dst = &h->mv_cache[list][scan8[0]];
  2131. mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
  2132. for(y=0; y<4; y++){
  2133. AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
  2134. }
  2135. }
  2136. }
  2137. /*
  2138. 0 . T T. T T T T
  2139. 1 L . .L . . . .
  2140. 2 L . .L . . . .
  2141. 3 . T TL . . . .
  2142. 4 L . .L . . . .
  2143. 5 L . .. . . . .
  2144. */
  2145. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  2146. if(top_type){
  2147. AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
  2148. }
  2149. if(left_type[0]){
  2150. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
  2151. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
  2152. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
  2153. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
  2154. }
  2155. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  2156. if(!CABAC && h->pps.transform_8x8_mode){
  2157. if(IS_8x8DCT(top_type)){
  2158. h->non_zero_count_cache[4+8*0]=
  2159. h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
  2160. h->non_zero_count_cache[6+8*0]=
  2161. h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
  2162. }
  2163. if(IS_8x8DCT(left_type[0])){
  2164. h->non_zero_count_cache[3+8*1]=
  2165. h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2; //FIXME check MBAFF
  2166. }
  2167. if(IS_8x8DCT(left_type[1])){
  2168. h->non_zero_count_cache[3+8*3]=
  2169. h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8; //FIXME check MBAFF
  2170. }
  2171. if(IS_8x8DCT(mb_type)){
  2172. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  2173. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
  2174. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  2175. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
  2176. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  2177. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
  2178. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  2179. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
  2180. }
  2181. }
  2182. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  2183. int list;
  2184. for(list=0; list<h->list_count; list++){
  2185. if(USES_LIST(top_type, list)){
  2186. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  2187. const int b8_xy= 4*top_xy + 2;
  2188. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2189. AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
  2190. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  2191. h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
  2192. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  2193. h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
  2194. }else{
  2195. AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
  2196. AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2197. }
  2198. if(!IS_INTERLACED(mb_type^left_type[0])){
  2199. if(USES_LIST(left_type[0], list)){
  2200. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  2201. const int b8_xy= 4*left_xy[0] + 1;
  2202. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2203. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
  2204. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
  2205. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
  2206. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
  2207. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2208. h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
  2209. h->ref_cache[list][scan8[0] - 1 +16 ]=
  2210. h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
  2211. }else{
  2212. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
  2213. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
  2214. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
  2215. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
  2216. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2217. h->ref_cache[list][scan8[0] - 1 + 8 ]=
  2218. h->ref_cache[list][scan8[0] - 1 + 16 ]=
  2219. h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
  2220. }
  2221. }
  2222. }
  2223. }
  2224. return 0;
  2225. }
  2226. static void loop_filter(H264Context *h){
  2227. MpegEncContext * const s = &h->s;
  2228. uint8_t *dest_y, *dest_cb, *dest_cr;
  2229. int linesize, uvlinesize, mb_x, mb_y;
  2230. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  2231. const int old_slice_type= h->slice_type;
  2232. const int end_mb_x = s->mb_x;
  2233. if(h->deblocking_filter) {
  2234. int start_x= s->resync_mb_y == s->mb_y ? s->resync_mb_x : 0;
  2235. for(mb_x= start_x; mb_x<end_mb_x; mb_x++){
  2236. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  2237. int mb_xy, mb_type;
  2238. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  2239. h->slice_num= h->slice_table[mb_xy];
  2240. mb_type= s->current_picture.mb_type[mb_xy];
  2241. h->list_count= h->list_counts[mb_xy];
  2242. if(FRAME_MBAFF)
  2243. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2244. s->mb_x= mb_x;
  2245. s->mb_y= mb_y;
  2246. dest_y = s->current_picture.data[0] + ((mb_x<<h->pixel_shift) + mb_y * s->linesize ) * 16;
  2247. dest_cb = s->current_picture.data[1] + ((mb_x<<h->pixel_shift) + mb_y * s->uvlinesize) * 8;
  2248. dest_cr = s->current_picture.data[2] + ((mb_x<<h->pixel_shift) + mb_y * s->uvlinesize) * 8;
  2249. //FIXME simplify above
  2250. if (MB_FIELD) {
  2251. linesize = h->mb_linesize = s->linesize * 2;
  2252. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2253. if(mb_y&1){ //FIXME move out of this function?
  2254. dest_y -= s->linesize*15;
  2255. dest_cb-= s->uvlinesize*7;
  2256. dest_cr-= s->uvlinesize*7;
  2257. }
  2258. } else {
  2259. linesize = h->mb_linesize = s->linesize;
  2260. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2261. }
  2262. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2263. if(fill_filter_caches(h, mb_type))
  2264. continue;
  2265. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2266. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2267. if (FRAME_MBAFF) {
  2268. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2269. } else {
  2270. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2271. }
  2272. }
  2273. }
  2274. }
  2275. h->slice_type= old_slice_type;
  2276. s->mb_x= end_mb_x;
  2277. s->mb_y= end_mb_y - FRAME_MBAFF;
  2278. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2279. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2280. }
  2281. static void predict_field_decoding_flag(H264Context *h){
  2282. MpegEncContext * const s = &h->s;
  2283. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2284. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2285. ? s->current_picture.mb_type[mb_xy-1]
  2286. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2287. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  2288. : 0;
  2289. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2290. }
  2291. /**
  2292. * Draw edges and report progress for the last MB row.
  2293. */
  2294. static void decode_finish_row(H264Context *h){
  2295. MpegEncContext * const s = &h->s;
  2296. int top = 16*(s->mb_y >> FIELD_PICTURE);
  2297. int height = 16 << FRAME_MBAFF;
  2298. int deblock_border = (16 + 4) << FRAME_MBAFF;
  2299. int pic_height = 16*s->mb_height >> FIELD_PICTURE;
  2300. if (h->deblocking_filter) {
  2301. if((top + height) >= pic_height)
  2302. height += deblock_border;
  2303. top -= deblock_border;
  2304. }
  2305. if (top >= pic_height || (top + height) < h->emu_edge_height)
  2306. return;
  2307. height = FFMIN(height, pic_height - top);
  2308. if (top < h->emu_edge_height) {
  2309. height = top+height;
  2310. top = 0;
  2311. }
  2312. ff_draw_horiz_band(s, top, height);
  2313. if (s->dropable) return;
  2314. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
  2315. s->picture_structure==PICT_BOTTOM_FIELD);
  2316. }
  2317. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  2318. H264Context *h = *(void**)arg;
  2319. MpegEncContext * const s = &h->s;
  2320. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  2321. s->mb_skip_run= -1;
  2322. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  2323. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  2324. if( h->pps.cabac ) {
  2325. /* realign */
  2326. align_get_bits( &s->gb );
  2327. /* init cabac */
  2328. ff_init_cabac_states( &h->cabac);
  2329. ff_init_cabac_decoder( &h->cabac,
  2330. s->gb.buffer + get_bits_count(&s->gb)/8,
  2331. (get_bits_left(&s->gb) + 7)/8);
  2332. ff_h264_init_cabac_states(h);
  2333. for(;;){
  2334. //START_TIMER
  2335. int ret = ff_h264_decode_mb_cabac(h);
  2336. int eos;
  2337. //STOP_TIMER("decode_mb_cabac")
  2338. if(ret>=0) ff_h264_hl_decode_mb(h);
  2339. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  2340. s->mb_y++;
  2341. ret = ff_h264_decode_mb_cabac(h);
  2342. if(ret>=0) ff_h264_hl_decode_mb(h);
  2343. s->mb_y--;
  2344. }
  2345. eos = get_cabac_terminate( &h->cabac );
  2346. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  2347. 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);
  2348. return 0;
  2349. }
  2350. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  2351. 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);
  2352. 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);
  2353. return -1;
  2354. }
  2355. if( ++s->mb_x >= s->mb_width ) {
  2356. loop_filter(h);
  2357. s->mb_x = 0;
  2358. decode_finish_row(h);
  2359. ++s->mb_y;
  2360. if(FIELD_OR_MBAFF_PICTURE) {
  2361. ++s->mb_y;
  2362. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2363. predict_field_decoding_flag(h);
  2364. }
  2365. }
  2366. if( eos || s->mb_y >= s->mb_height ) {
  2367. if(s->mb_x)
  2368. loop_filter(h);
  2369. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2370. 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);
  2371. return 0;
  2372. }
  2373. }
  2374. } else {
  2375. for(;;){
  2376. int ret = ff_h264_decode_mb_cavlc(h);
  2377. if(ret>=0) ff_h264_hl_decode_mb(h);
  2378. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  2379. s->mb_y++;
  2380. ret = ff_h264_decode_mb_cavlc(h);
  2381. if(ret>=0) ff_h264_hl_decode_mb(h);
  2382. s->mb_y--;
  2383. }
  2384. if(ret<0){
  2385. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2386. 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);
  2387. return -1;
  2388. }
  2389. if(++s->mb_x >= s->mb_width){
  2390. loop_filter(h);
  2391. s->mb_x=0;
  2392. decode_finish_row(h);
  2393. ++s->mb_y;
  2394. if(FIELD_OR_MBAFF_PICTURE) {
  2395. ++s->mb_y;
  2396. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2397. predict_field_decoding_flag(h);
  2398. }
  2399. if(s->mb_y >= s->mb_height){
  2400. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2401. if( get_bits_count(&s->gb) == s->gb.size_in_bits
  2402. || get_bits_count(&s->gb) < s->gb.size_in_bits && s->avctx->error_recognition < FF_ER_AGGRESSIVE) {
  2403. 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);
  2404. return 0;
  2405. }else{
  2406. 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);
  2407. return -1;
  2408. }
  2409. }
  2410. }
  2411. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  2412. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2413. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  2414. if(s->mb_x)
  2415. loop_filter(h);
  2416. 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);
  2417. return 0;
  2418. }else{
  2419. 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);
  2420. return -1;
  2421. }
  2422. }
  2423. }
  2424. }
  2425. #if 0
  2426. for(;s->mb_y < s->mb_height; s->mb_y++){
  2427. for(;s->mb_x < s->mb_width; s->mb_x++){
  2428. int ret= decode_mb(h);
  2429. ff_h264_hl_decode_mb(h);
  2430. if(ret<0){
  2431. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2432. 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);
  2433. return -1;
  2434. }
  2435. if(++s->mb_x >= s->mb_width){
  2436. s->mb_x=0;
  2437. if(++s->mb_y >= s->mb_height){
  2438. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2439. 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);
  2440. return 0;
  2441. }else{
  2442. 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);
  2443. return -1;
  2444. }
  2445. }
  2446. }
  2447. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  2448. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2449. 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);
  2450. return 0;
  2451. }else{
  2452. 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);
  2453. return -1;
  2454. }
  2455. }
  2456. }
  2457. s->mb_x=0;
  2458. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2459. }
  2460. #endif
  2461. return -1; //not reached
  2462. }
  2463. /**
  2464. * Call decode_slice() for each context.
  2465. *
  2466. * @param h h264 master context
  2467. * @param context_count number of contexts to execute
  2468. */
  2469. static void execute_decode_slices(H264Context *h, int context_count){
  2470. MpegEncContext * const s = &h->s;
  2471. AVCodecContext * const avctx= s->avctx;
  2472. H264Context *hx;
  2473. int i;
  2474. if (s->avctx->hwaccel)
  2475. return;
  2476. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2477. return;
  2478. if(context_count == 1) {
  2479. decode_slice(avctx, &h);
  2480. } else {
  2481. for(i = 1; i < context_count; i++) {
  2482. hx = h->thread_context[i];
  2483. hx->s.error_recognition = avctx->error_recognition;
  2484. hx->s.error_count = 0;
  2485. hx->x264_build= h->x264_build;
  2486. }
  2487. avctx->execute(avctx, (void *)decode_slice,
  2488. h->thread_context, NULL, context_count, sizeof(void*));
  2489. /* pull back stuff from slices to master context */
  2490. hx = h->thread_context[context_count - 1];
  2491. s->mb_x = hx->s.mb_x;
  2492. s->mb_y = hx->s.mb_y;
  2493. s->dropable = hx->s.dropable;
  2494. s->picture_structure = hx->s.picture_structure;
  2495. for(i = 1; i < context_count; i++)
  2496. h->s.error_count += h->thread_context[i]->s.error_count;
  2497. }
  2498. }
  2499. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  2500. MpegEncContext * const s = &h->s;
  2501. AVCodecContext * const avctx= s->avctx;
  2502. int buf_index=0;
  2503. H264Context *hx; ///< thread context
  2504. int context_count = 0;
  2505. int next_avc= h->is_avc ? 0 : buf_size;
  2506. h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;
  2507. #if 0
  2508. int i;
  2509. for(i=0; i<50; i++){
  2510. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  2511. }
  2512. #endif
  2513. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  2514. h->current_slice = 0;
  2515. if (!s->first_field)
  2516. s->current_picture_ptr= NULL;
  2517. ff_h264_reset_sei(h);
  2518. }
  2519. for(;;){
  2520. int consumed;
  2521. int dst_length;
  2522. int bit_length;
  2523. const uint8_t *ptr;
  2524. int i, nalsize = 0;
  2525. int err;
  2526. if(buf_index >= next_avc) {
  2527. if(buf_index >= buf_size) break;
  2528. nalsize = 0;
  2529. for(i = 0; i < h->nal_length_size; i++)
  2530. nalsize = (nalsize << 8) | buf[buf_index++];
  2531. if(nalsize <= 0 || nalsize > buf_size - buf_index){
  2532. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  2533. break;
  2534. }
  2535. next_avc= buf_index + nalsize;
  2536. } else {
  2537. // start code prefix search
  2538. for(; buf_index + 3 < next_avc; buf_index++){
  2539. // This should always succeed in the first iteration.
  2540. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  2541. break;
  2542. }
  2543. if(buf_index+3 >= buf_size) break;
  2544. buf_index+=3;
  2545. if(buf_index >= next_avc) continue;
  2546. }
  2547. hx = h->thread_context[context_count];
  2548. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  2549. if (ptr==NULL || dst_length < 0){
  2550. return -1;
  2551. }
  2552. i= buf_index + consumed;
  2553. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  2554. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  2555. s->workaround_bugs |= FF_BUG_TRUNCATED;
  2556. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  2557. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  2558. dst_length--;
  2559. }
  2560. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  2561. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  2562. 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);
  2563. }
  2564. if (h->is_avc && (nalsize != consumed) && nalsize){
  2565. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  2566. }
  2567. buf_index += consumed;
  2568. //FIXME do not discard SEI id
  2569. if(
  2570. #if FF_API_HURRY_UP
  2571. (s->hurry_up == 1 && h->nal_ref_idc == 0) ||
  2572. #endif
  2573. (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  2574. continue;
  2575. again:
  2576. err = 0;
  2577. switch(hx->nal_unit_type){
  2578. case NAL_IDR_SLICE:
  2579. if (h->nal_unit_type != NAL_IDR_SLICE) {
  2580. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  2581. return -1;
  2582. }
  2583. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  2584. case NAL_SLICE:
  2585. init_get_bits(&hx->s.gb, ptr, bit_length);
  2586. hx->intra_gb_ptr=
  2587. hx->inter_gb_ptr= &hx->s.gb;
  2588. hx->s.data_partitioning = 0;
  2589. if((err = decode_slice_header(hx, h)))
  2590. break;
  2591. s->current_picture_ptr->key_frame |=
  2592. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  2593. (h->sei_recovery_frame_cnt >= 0);
  2594. if (h->current_slice == 1) {
  2595. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
  2596. decode_postinit(h);
  2597. }
  2598. if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  2599. return -1;
  2600. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2601. ff_vdpau_h264_picture_start(s);
  2602. }
  2603. if(hx->redundant_pic_count==0
  2604. #if FF_API_HURRY_UP
  2605. && hx->s.hurry_up < 5
  2606. #endif
  2607. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2608. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2609. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2610. && avctx->skip_frame < AVDISCARD_ALL){
  2611. if(avctx->hwaccel) {
  2612. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  2613. return -1;
  2614. }else
  2615. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  2616. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  2617. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  2618. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  2619. }else
  2620. context_count++;
  2621. }
  2622. break;
  2623. case NAL_DPA:
  2624. init_get_bits(&hx->s.gb, ptr, bit_length);
  2625. hx->intra_gb_ptr=
  2626. hx->inter_gb_ptr= NULL;
  2627. if ((err = decode_slice_header(hx, h)) < 0)
  2628. break;
  2629. hx->s.data_partitioning = 1;
  2630. break;
  2631. case NAL_DPB:
  2632. init_get_bits(&hx->intra_gb, ptr, bit_length);
  2633. hx->intra_gb_ptr= &hx->intra_gb;
  2634. break;
  2635. case NAL_DPC:
  2636. init_get_bits(&hx->inter_gb, ptr, bit_length);
  2637. hx->inter_gb_ptr= &hx->inter_gb;
  2638. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  2639. && s->context_initialized
  2640. #if FF_API_HURRY_UP
  2641. && s->hurry_up < 5
  2642. #endif
  2643. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2644. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2645. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2646. && avctx->skip_frame < AVDISCARD_ALL)
  2647. context_count++;
  2648. break;
  2649. case NAL_SEI:
  2650. init_get_bits(&s->gb, ptr, bit_length);
  2651. ff_h264_decode_sei(h);
  2652. break;
  2653. case NAL_SPS:
  2654. init_get_bits(&s->gb, ptr, bit_length);
  2655. ff_h264_decode_seq_parameter_set(h);
  2656. if(s->flags& CODEC_FLAG_LOW_DELAY)
  2657. s->low_delay=1;
  2658. if(avctx->has_b_frames < 2)
  2659. avctx->has_b_frames= !s->low_delay;
  2660. if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
  2661. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  2662. avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2663. h->pixel_shift = h->sps.bit_depth_luma/9;
  2664. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
  2665. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
  2666. dsputil_init(&s->dsp, s->avctx);
  2667. } else {
  2668. av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2669. return -1;
  2670. }
  2671. }
  2672. break;
  2673. case NAL_PPS:
  2674. init_get_bits(&s->gb, ptr, bit_length);
  2675. ff_h264_decode_picture_parameter_set(h, bit_length);
  2676. break;
  2677. case NAL_AUD:
  2678. case NAL_END_SEQUENCE:
  2679. case NAL_END_STREAM:
  2680. case NAL_FILLER_DATA:
  2681. case NAL_SPS_EXT:
  2682. case NAL_AUXILIARY_SLICE:
  2683. break;
  2684. default:
  2685. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  2686. }
  2687. if(context_count == h->max_contexts) {
  2688. execute_decode_slices(h, context_count);
  2689. context_count = 0;
  2690. }
  2691. if (err < 0)
  2692. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  2693. else if(err == 1) {
  2694. /* Slice could not be decoded in parallel mode, copy down
  2695. * NAL unit stuff to context 0 and restart. Note that
  2696. * rbsp_buffer is not transferred, but since we no longer
  2697. * run in parallel mode this should not be an issue. */
  2698. h->nal_unit_type = hx->nal_unit_type;
  2699. h->nal_ref_idc = hx->nal_ref_idc;
  2700. hx = h;
  2701. goto again;
  2702. }
  2703. }
  2704. if(context_count)
  2705. execute_decode_slices(h, context_count);
  2706. return buf_index;
  2707. }
  2708. /**
  2709. * returns the number of bytes consumed for building the current frame
  2710. */
  2711. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  2712. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  2713. if(pos+10>buf_size) pos=buf_size; // oops ;)
  2714. return pos;
  2715. }
  2716. static int decode_frame(AVCodecContext *avctx,
  2717. void *data, int *data_size,
  2718. AVPacket *avpkt)
  2719. {
  2720. const uint8_t *buf = avpkt->data;
  2721. int buf_size = avpkt->size;
  2722. H264Context *h = avctx->priv_data;
  2723. MpegEncContext *s = &h->s;
  2724. AVFrame *pict = data;
  2725. int buf_index;
  2726. s->flags= avctx->flags;
  2727. s->flags2= avctx->flags2;
  2728. /* end of stream, output what is still in the buffers */
  2729. out:
  2730. if (buf_size == 0) {
  2731. Picture *out;
  2732. int i, out_idx;
  2733. s->current_picture_ptr = NULL;
  2734. //FIXME factorize this with the output code below
  2735. out = h->delayed_pic[0];
  2736. out_idx = 0;
  2737. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2738. if(h->delayed_pic[i]->poc < out->poc){
  2739. out = h->delayed_pic[i];
  2740. out_idx = i;
  2741. }
  2742. for(i=out_idx; h->delayed_pic[i]; i++)
  2743. h->delayed_pic[i] = h->delayed_pic[i+1];
  2744. if(out){
  2745. *data_size = sizeof(AVFrame);
  2746. *pict= *(AVFrame*)out;
  2747. }
  2748. return 0;
  2749. }
  2750. buf_index=decode_nal_units(h, buf, buf_size);
  2751. if(buf_index < 0)
  2752. return -1;
  2753. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  2754. buf_size = 0;
  2755. goto out;
  2756. }
  2757. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  2758. if (avctx->skip_frame >= AVDISCARD_NONREF
  2759. #if FF_API_HURRY_UP
  2760. || s->hurry_up
  2761. #endif
  2762. )
  2763. return 0;
  2764. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  2765. return -1;
  2766. }
  2767. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  2768. if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h);
  2769. field_end(h, 0);
  2770. if (!h->next_output_pic) {
  2771. /* Wait for second field. */
  2772. *data_size = 0;
  2773. } else {
  2774. *data_size = sizeof(AVFrame);
  2775. *pict = *(AVFrame*)h->next_output_pic;
  2776. }
  2777. }
  2778. assert(pict->data[0] || !*data_size);
  2779. ff_print_debug_info(s, pict);
  2780. //printf("out %d\n", (int)pict->data[0]);
  2781. return get_consumed_bytes(s, buf_index, buf_size);
  2782. }
  2783. #if 0
  2784. static inline void fill_mb_avail(H264Context *h){
  2785. MpegEncContext * const s = &h->s;
  2786. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2787. if(s->mb_y){
  2788. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  2789. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  2790. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  2791. }else{
  2792. h->mb_avail[0]=
  2793. h->mb_avail[1]=
  2794. h->mb_avail[2]= 0;
  2795. }
  2796. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  2797. h->mb_avail[4]= 1; //FIXME move out
  2798. h->mb_avail[5]= 0; //FIXME move out
  2799. }
  2800. #endif
  2801. #ifdef TEST
  2802. #undef printf
  2803. #undef random
  2804. #define COUNT 8000
  2805. #define SIZE (COUNT*40)
  2806. int main(void){
  2807. int i;
  2808. uint8_t temp[SIZE];
  2809. PutBitContext pb;
  2810. GetBitContext gb;
  2811. // int int_temp[10000];
  2812. DSPContext dsp;
  2813. AVCodecContext avctx;
  2814. dsputil_init(&dsp, &avctx);
  2815. init_put_bits(&pb, temp, SIZE);
  2816. printf("testing unsigned exp golomb\n");
  2817. for(i=0; i<COUNT; i++){
  2818. START_TIMER
  2819. set_ue_golomb(&pb, i);
  2820. STOP_TIMER("set_ue_golomb");
  2821. }
  2822. flush_put_bits(&pb);
  2823. init_get_bits(&gb, temp, 8*SIZE);
  2824. for(i=0; i<COUNT; i++){
  2825. int j, s;
  2826. s= show_bits(&gb, 24);
  2827. START_TIMER
  2828. j= get_ue_golomb(&gb);
  2829. if(j != i){
  2830. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2831. // return -1;
  2832. }
  2833. STOP_TIMER("get_ue_golomb");
  2834. }
  2835. init_put_bits(&pb, temp, SIZE);
  2836. printf("testing signed exp golomb\n");
  2837. for(i=0; i<COUNT; i++){
  2838. START_TIMER
  2839. set_se_golomb(&pb, i - COUNT/2);
  2840. STOP_TIMER("set_se_golomb");
  2841. }
  2842. flush_put_bits(&pb);
  2843. init_get_bits(&gb, temp, 8*SIZE);
  2844. for(i=0; i<COUNT; i++){
  2845. int j, s;
  2846. s= show_bits(&gb, 24);
  2847. START_TIMER
  2848. j= get_se_golomb(&gb);
  2849. if(j != i - COUNT/2){
  2850. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2851. // return -1;
  2852. }
  2853. STOP_TIMER("get_se_golomb");
  2854. }
  2855. #if 0
  2856. printf("testing 4x4 (I)DCT\n");
  2857. DCTELEM block[16];
  2858. uint8_t src[16], ref[16];
  2859. uint64_t error= 0, max_error=0;
  2860. for(i=0; i<COUNT; i++){
  2861. int j;
  2862. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  2863. for(j=0; j<16; j++){
  2864. ref[j]= random()%255;
  2865. src[j]= random()%255;
  2866. }
  2867. h264_diff_dct_c(block, src, ref, 4);
  2868. //normalize
  2869. for(j=0; j<16; j++){
  2870. // printf("%d ", block[j]);
  2871. block[j]= block[j]*4;
  2872. if(j&1) block[j]= (block[j]*4 + 2)/5;
  2873. if(j&4) block[j]= (block[j]*4 + 2)/5;
  2874. }
  2875. // printf("\n");
  2876. h->h264dsp.h264_idct_add(ref, block, 4);
  2877. /* for(j=0; j<16; j++){
  2878. printf("%d ", ref[j]);
  2879. }
  2880. printf("\n");*/
  2881. for(j=0; j<16; j++){
  2882. int diff= FFABS(src[j] - ref[j]);
  2883. error+= diff*diff;
  2884. max_error= FFMAX(max_error, diff);
  2885. }
  2886. }
  2887. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  2888. printf("testing quantizer\n");
  2889. for(qp=0; qp<52; qp++){
  2890. for(i=0; i<16; i++)
  2891. src1_block[i]= src2_block[i]= random()%255;
  2892. }
  2893. printf("Testing NAL layer\n");
  2894. uint8_t bitstream[COUNT];
  2895. uint8_t nal[COUNT*2];
  2896. H264Context h;
  2897. memset(&h, 0, sizeof(H264Context));
  2898. for(i=0; i<COUNT; i++){
  2899. int zeros= i;
  2900. int nal_length;
  2901. int consumed;
  2902. int out_length;
  2903. uint8_t *out;
  2904. int j;
  2905. for(j=0; j<COUNT; j++){
  2906. bitstream[j]= (random() % 255) + 1;
  2907. }
  2908. for(j=0; j<zeros; j++){
  2909. int pos= random() % COUNT;
  2910. while(bitstream[pos] == 0){
  2911. pos++;
  2912. pos %= COUNT;
  2913. }
  2914. bitstream[pos]=0;
  2915. }
  2916. START_TIMER
  2917. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  2918. if(nal_length<0){
  2919. printf("encoding failed\n");
  2920. return -1;
  2921. }
  2922. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  2923. STOP_TIMER("NAL")
  2924. if(out_length != COUNT){
  2925. printf("incorrect length %d %d\n", out_length, COUNT);
  2926. return -1;
  2927. }
  2928. if(consumed != nal_length){
  2929. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  2930. return -1;
  2931. }
  2932. if(memcmp(bitstream, out, COUNT)){
  2933. printf("mismatch\n");
  2934. return -1;
  2935. }
  2936. }
  2937. #endif
  2938. printf("Testing RBSP\n");
  2939. return 0;
  2940. }
  2941. #endif /* TEST */
  2942. av_cold void ff_h264_free_context(H264Context *h)
  2943. {
  2944. int i;
  2945. free_tables(h, 1); //FIXME cleanup init stuff perhaps
  2946. for(i = 0; i < MAX_SPS_COUNT; i++)
  2947. av_freep(h->sps_buffers + i);
  2948. for(i = 0; i < MAX_PPS_COUNT; i++)
  2949. av_freep(h->pps_buffers + i);
  2950. }
  2951. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  2952. {
  2953. H264Context *h = avctx->priv_data;
  2954. MpegEncContext *s = &h->s;
  2955. ff_h264_free_context(h);
  2956. MPV_common_end(s);
  2957. // memset(h, 0, sizeof(H264Context));
  2958. return 0;
  2959. }
  2960. static const AVProfile profiles[] = {
  2961. { FF_PROFILE_H264_BASELINE, "Baseline" },
  2962. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  2963. { FF_PROFILE_H264_MAIN, "Main" },
  2964. { FF_PROFILE_H264_EXTENDED, "Extended" },
  2965. { FF_PROFILE_H264_HIGH, "High" },
  2966. { FF_PROFILE_H264_HIGH_10, "High 10" },
  2967. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  2968. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  2969. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  2970. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  2971. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  2972. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  2973. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  2974. { FF_PROFILE_UNKNOWN },
  2975. };
  2976. AVCodec ff_h264_decoder = {
  2977. "h264",
  2978. AVMEDIA_TYPE_VIDEO,
  2979. CODEC_ID_H264,
  2980. sizeof(H264Context),
  2981. ff_h264_decode_init,
  2982. NULL,
  2983. ff_h264_decode_end,
  2984. decode_frame,
  2985. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  2986. CODEC_CAP_FRAME_THREADS |
  2987. CODEC_CAP_SLICE_THREADS,
  2988. .flush= flush_dpb,
  2989. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  2990. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  2991. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  2992. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  2993. };
  2994. #if CONFIG_H264_VDPAU_DECODER
  2995. AVCodec ff_h264_vdpau_decoder = {
  2996. "h264_vdpau",
  2997. AVMEDIA_TYPE_VIDEO,
  2998. CODEC_ID_H264,
  2999. sizeof(H264Context),
  3000. ff_h264_decode_init,
  3001. NULL,
  3002. ff_h264_decode_end,
  3003. decode_frame,
  3004. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  3005. .flush= flush_dpb,
  3006. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  3007. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  3008. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3009. };
  3010. #endif