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.

3428 lines
127KB

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