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.

3554 lines
135KB

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