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.

3553 lines
134KB

  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. init_scan_tables(c);
  1736. clone_tables(c, h, i);
  1737. }
  1738. for(i = 0; i < s->avctx->thread_count; i++)
  1739. if(context_init(h->thread_context[i]) < 0)
  1740. return -1;
  1741. }
  1742. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  1743. h->mb_mbaff = 0;
  1744. h->mb_aff_frame = 0;
  1745. last_pic_structure = s0->picture_structure;
  1746. if(h->sps.frame_mbs_only_flag){
  1747. s->picture_structure= PICT_FRAME;
  1748. }else{
  1749. if(get_bits1(&s->gb)) { //field_pic_flag
  1750. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  1751. } else {
  1752. s->picture_structure= PICT_FRAME;
  1753. h->mb_aff_frame = h->sps.mb_aff;
  1754. }
  1755. }
  1756. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  1757. if(h0->current_slice == 0){
  1758. while(h->frame_num != h->prev_frame_num &&
  1759. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  1760. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1761. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  1762. if (ff_h264_frame_start(h) < 0)
  1763. return -1;
  1764. h->prev_frame_num++;
  1765. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  1766. s->current_picture_ptr->frame_num= h->prev_frame_num;
  1767. ff_generate_sliding_window_mmcos(h);
  1768. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1769. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  1770. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  1771. * about there being no actual duplicates.
  1772. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  1773. * concealing a lost frame, this probably isn't noticable by comparison, but it should
  1774. * be fixed. */
  1775. if (h->short_ref_count) {
  1776. if (prev) {
  1777. av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
  1778. (const uint8_t**)prev->data, prev->linesize,
  1779. s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
  1780. h->short_ref[0]->poc = prev->poc+2;
  1781. }
  1782. h->short_ref[0]->frame_num = h->prev_frame_num;
  1783. }
  1784. }
  1785. /* See if we have a decoded first field looking for a pair... */
  1786. if (s0->first_field) {
  1787. assert(s0->current_picture_ptr);
  1788. assert(s0->current_picture_ptr->data[0]);
  1789. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  1790. /* figure out if we have a complementary field pair */
  1791. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  1792. /*
  1793. * Previous field is unmatched. Don't display it, but let it
  1794. * remain for reference if marked as such.
  1795. */
  1796. s0->current_picture_ptr = NULL;
  1797. s0->first_field = FIELD_PICTURE;
  1798. } else {
  1799. if (h->nal_ref_idc &&
  1800. s0->current_picture_ptr->reference &&
  1801. s0->current_picture_ptr->frame_num != h->frame_num) {
  1802. /*
  1803. * This and previous field were reference, but had
  1804. * different frame_nums. Consider this field first in
  1805. * pair. Throw away previous field except for reference
  1806. * purposes.
  1807. */
  1808. s0->first_field = 1;
  1809. s0->current_picture_ptr = NULL;
  1810. } else {
  1811. /* Second field in complementary pair */
  1812. s0->first_field = 0;
  1813. }
  1814. }
  1815. } else {
  1816. /* Frame or first field in a potentially complementary pair */
  1817. assert(!s0->current_picture_ptr);
  1818. s0->first_field = FIELD_PICTURE;
  1819. }
  1820. if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
  1821. s0->first_field = 0;
  1822. return -1;
  1823. }
  1824. }
  1825. if(h != h0)
  1826. clone_slice(h, h0);
  1827. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  1828. assert(s->mb_num == s->mb_width * s->mb_height);
  1829. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  1830. first_mb_in_slice >= s->mb_num){
  1831. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1832. return -1;
  1833. }
  1834. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  1835. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  1836. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1837. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  1838. assert(s->mb_y < s->mb_height);
  1839. if(s->picture_structure==PICT_FRAME){
  1840. h->curr_pic_num= h->frame_num;
  1841. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  1842. }else{
  1843. h->curr_pic_num= 2*h->frame_num + 1;
  1844. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  1845. }
  1846. if(h->nal_unit_type == NAL_IDR_SLICE){
  1847. get_ue_golomb(&s->gb); /* idr_pic_id */
  1848. }
  1849. if(h->sps.poc_type==0){
  1850. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  1851. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  1852. h->delta_poc_bottom= get_se_golomb(&s->gb);
  1853. }
  1854. }
  1855. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  1856. h->delta_poc[0]= get_se_golomb(&s->gb);
  1857. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  1858. h->delta_poc[1]= get_se_golomb(&s->gb);
  1859. }
  1860. init_poc(h);
  1861. if(h->pps.redundant_pic_cnt_present){
  1862. h->redundant_pic_count= get_ue_golomb(&s->gb);
  1863. }
  1864. //set defaults, might be overridden a few lines later
  1865. h->ref_count[0]= h->pps.ref_count[0];
  1866. h->ref_count[1]= h->pps.ref_count[1];
  1867. if(h->slice_type_nos != AV_PICTURE_TYPE_I){
  1868. if(h->slice_type_nos == AV_PICTURE_TYPE_B){
  1869. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  1870. }
  1871. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  1872. if(num_ref_idx_active_override_flag){
  1873. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  1874. if(h->slice_type_nos==AV_PICTURE_TYPE_B)
  1875. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  1876. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  1877. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  1878. h->ref_count[0]= h->ref_count[1]= 1;
  1879. return -1;
  1880. }
  1881. }
  1882. if(h->slice_type_nos == AV_PICTURE_TYPE_B)
  1883. h->list_count= 2;
  1884. else
  1885. h->list_count= 1;
  1886. }else
  1887. h->list_count= 0;
  1888. if(!default_ref_list_done){
  1889. ff_h264_fill_default_ref_list(h);
  1890. }
  1891. if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  1892. return -1;
  1893. if(h->slice_type_nos!=AV_PICTURE_TYPE_I){
  1894. s->last_picture_ptr= &h->ref_list[0][0];
  1895. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1896. }
  1897. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  1898. s->next_picture_ptr= &h->ref_list[1][0];
  1899. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1900. }
  1901. if( (h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P )
  1902. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== AV_PICTURE_TYPE_B ) )
  1903. pred_weight_table(h);
  1904. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  1905. implicit_weight_table(h, -1);
  1906. }else {
  1907. h->use_weight = 0;
  1908. for (i = 0; i < 2; i++) {
  1909. h->luma_weight_flag[i] = 0;
  1910. h->chroma_weight_flag[i] = 0;
  1911. }
  1912. }
  1913. if(h->nal_ref_idc)
  1914. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  1915. if(FRAME_MBAFF){
  1916. ff_h264_fill_mbaff_ref_list(h);
  1917. if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  1918. implicit_weight_table(h, 0);
  1919. implicit_weight_table(h, 1);
  1920. }
  1921. }
  1922. if(h->slice_type_nos==AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  1923. ff_h264_direct_dist_scale_factor(h);
  1924. ff_h264_direct_ref_list_init(h);
  1925. if( h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac ){
  1926. tmp = get_ue_golomb_31(&s->gb);
  1927. if(tmp > 2){
  1928. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  1929. return -1;
  1930. }
  1931. h->cabac_init_idc= tmp;
  1932. }
  1933. h->last_qscale_diff = 0;
  1934. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  1935. if(tmp>51+6*(h->sps.bit_depth_luma-8)){
  1936. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1937. return -1;
  1938. }
  1939. s->qscale= tmp;
  1940. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  1941. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  1942. //FIXME qscale / qp ... stuff
  1943. if(h->slice_type == AV_PICTURE_TYPE_SP){
  1944. get_bits1(&s->gb); /* sp_for_switch_flag */
  1945. }
  1946. if(h->slice_type==AV_PICTURE_TYPE_SP || h->slice_type == AV_PICTURE_TYPE_SI){
  1947. get_se_golomb(&s->gb); /* slice_qs_delta */
  1948. }
  1949. h->deblocking_filter = 1;
  1950. h->slice_alpha_c0_offset = 52;
  1951. h->slice_beta_offset = 52;
  1952. if( h->pps.deblocking_filter_parameters_present ) {
  1953. tmp= get_ue_golomb_31(&s->gb);
  1954. if(tmp > 2){
  1955. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  1956. return -1;
  1957. }
  1958. h->deblocking_filter= tmp;
  1959. if(h->deblocking_filter < 2)
  1960. h->deblocking_filter^= 1; // 1<->0
  1961. if( h->deblocking_filter ) {
  1962. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  1963. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  1964. if( h->slice_alpha_c0_offset > 104U
  1965. || h->slice_beta_offset > 104U){
  1966. 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);
  1967. return -1;
  1968. }
  1969. }
  1970. }
  1971. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  1972. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != AV_PICTURE_TYPE_I)
  1973. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == AV_PICTURE_TYPE_B)
  1974. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  1975. h->deblocking_filter= 0;
  1976. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  1977. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  1978. /* Cheat slightly for speed:
  1979. Do not bother to deblock across slices. */
  1980. h->deblocking_filter = 2;
  1981. } else {
  1982. h0->max_contexts = 1;
  1983. if(!h0->single_decode_warning) {
  1984. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  1985. h0->single_decode_warning = 1;
  1986. }
  1987. if(h != h0)
  1988. return 1; // deblocking switched inside frame
  1989. }
  1990. }
  1991. 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]);
  1992. #if 0 //FMO
  1993. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  1994. slice_group_change_cycle= get_bits(&s->gb, ?);
  1995. #endif
  1996. h0->last_slice_type = slice_type;
  1997. h->slice_num = ++h0->current_slice;
  1998. if(h->slice_num >= MAX_SLICES){
  1999. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  2000. }
  2001. for(j=0; j<2; j++){
  2002. int id_list[16];
  2003. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  2004. for(i=0; i<16; i++){
  2005. id_list[i]= 60;
  2006. if(h->ref_list[j][i].data[0]){
  2007. int k;
  2008. uint8_t *base= h->ref_list[j][i].base[0];
  2009. for(k=0; k<h->short_ref_count; k++)
  2010. if(h->short_ref[k]->base[0] == base){
  2011. id_list[i]= k;
  2012. break;
  2013. }
  2014. for(k=0; k<h->long_ref_count; k++)
  2015. if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
  2016. id_list[i]= h->short_ref_count + k;
  2017. break;
  2018. }
  2019. }
  2020. }
  2021. ref2frm[0]=
  2022. ref2frm[1]= -1;
  2023. for(i=0; i<16; i++)
  2024. ref2frm[i+2]= 4*id_list[i]
  2025. +(h->ref_list[j][i].reference&3);
  2026. ref2frm[18+0]=
  2027. ref2frm[18+1]= -1;
  2028. for(i=16; i<48; i++)
  2029. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  2030. +(h->ref_list[j][i].reference&3);
  2031. }
  2032. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  2033. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2034. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2035. 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",
  2036. h->slice_num,
  2037. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  2038. first_mb_in_slice,
  2039. av_get_picture_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2040. pps_id, h->frame_num,
  2041. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2042. h->ref_count[0], h->ref_count[1],
  2043. s->qscale,
  2044. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  2045. h->use_weight,
  2046. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  2047. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  2048. );
  2049. }
  2050. return 0;
  2051. }
  2052. int ff_h264_get_slice_type(const H264Context *h)
  2053. {
  2054. switch (h->slice_type) {
  2055. case AV_PICTURE_TYPE_P: return 0;
  2056. case AV_PICTURE_TYPE_B: return 1;
  2057. case AV_PICTURE_TYPE_I: return 2;
  2058. case AV_PICTURE_TYPE_SP: return 3;
  2059. case AV_PICTURE_TYPE_SI: return 4;
  2060. default: return -1;
  2061. }
  2062. }
  2063. /**
  2064. *
  2065. * @return non zero if the loop filter can be skiped
  2066. */
  2067. static int fill_filter_caches(H264Context *h, int mb_type){
  2068. MpegEncContext * const s = &h->s;
  2069. const int mb_xy= h->mb_xy;
  2070. int top_xy, left_xy[2];
  2071. int top_type, left_type[2];
  2072. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  2073. //FIXME deblocking could skip the intra and nnz parts.
  2074. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  2075. * stuff, I can't imagine that these complex rules are worth it. */
  2076. left_xy[1] = left_xy[0] = mb_xy-1;
  2077. if(FRAME_MBAFF){
  2078. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
  2079. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  2080. if(s->mb_y&1){
  2081. if (left_mb_field_flag != curr_mb_field_flag) {
  2082. left_xy[0] -= s->mb_stride;
  2083. }
  2084. }else{
  2085. if(curr_mb_field_flag){
  2086. top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
  2087. }
  2088. if (left_mb_field_flag != curr_mb_field_flag) {
  2089. left_xy[1] += s->mb_stride;
  2090. }
  2091. }
  2092. }
  2093. h->top_mb_xy = top_xy;
  2094. h->left_mb_xy[0] = left_xy[0];
  2095. h->left_mb_xy[1] = left_xy[1];
  2096. {
  2097. //for sufficiently low qp, filtering wouldn't do anything
  2098. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  2099. int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
  2100. int qp = s->current_picture.qscale_table[mb_xy];
  2101. if(qp <= qp_thresh
  2102. && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
  2103. && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
  2104. if(!FRAME_MBAFF)
  2105. return 1;
  2106. if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
  2107. && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
  2108. return 1;
  2109. }
  2110. }
  2111. top_type = s->current_picture.mb_type[top_xy] ;
  2112. left_type[0] = s->current_picture.mb_type[left_xy[0]];
  2113. left_type[1] = s->current_picture.mb_type[left_xy[1]];
  2114. if(h->deblocking_filter == 2){
  2115. if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
  2116. if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
  2117. }else{
  2118. if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
  2119. if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
  2120. }
  2121. h->top_type = top_type ;
  2122. h->left_type[0]= left_type[0];
  2123. h->left_type[1]= left_type[1];
  2124. if(IS_INTRA(mb_type))
  2125. return 0;
  2126. AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
  2127. AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
  2128. AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
  2129. AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
  2130. AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
  2131. h->cbp= h->cbp_table[mb_xy];
  2132. {
  2133. int list;
  2134. for(list=0; list<h->list_count; list++){
  2135. int8_t *ref;
  2136. int y, b_stride;
  2137. int16_t (*mv_dst)[2];
  2138. int16_t (*mv_src)[2];
  2139. if(!USES_LIST(mb_type, list)){
  2140. fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
  2141. AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2142. AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2143. AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2144. AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2145. continue;
  2146. }
  2147. ref = &s->current_picture.ref_index[list][4*mb_xy];
  2148. {
  2149. int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2150. AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2151. AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2152. ref += 2;
  2153. AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2154. AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2155. }
  2156. b_stride = h->b_stride;
  2157. mv_dst = &h->mv_cache[list][scan8[0]];
  2158. mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
  2159. for(y=0; y<4; y++){
  2160. AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
  2161. }
  2162. }
  2163. }
  2164. /*
  2165. 0 . T T. T T T T
  2166. 1 L . .L . . . .
  2167. 2 L . .L . . . .
  2168. 3 . T TL . . . .
  2169. 4 L . .L . . . .
  2170. 5 L . .. . . . .
  2171. */
  2172. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  2173. if(top_type){
  2174. AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
  2175. }
  2176. if(left_type[0]){
  2177. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
  2178. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
  2179. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
  2180. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
  2181. }
  2182. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  2183. if(!CABAC && h->pps.transform_8x8_mode){
  2184. if(IS_8x8DCT(top_type)){
  2185. h->non_zero_count_cache[4+8*0]=
  2186. h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
  2187. h->non_zero_count_cache[6+8*0]=
  2188. h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
  2189. }
  2190. if(IS_8x8DCT(left_type[0])){
  2191. h->non_zero_count_cache[3+8*1]=
  2192. h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2; //FIXME check MBAFF
  2193. }
  2194. if(IS_8x8DCT(left_type[1])){
  2195. h->non_zero_count_cache[3+8*3]=
  2196. h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8; //FIXME check MBAFF
  2197. }
  2198. if(IS_8x8DCT(mb_type)){
  2199. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  2200. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
  2201. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  2202. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
  2203. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  2204. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
  2205. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  2206. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
  2207. }
  2208. }
  2209. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  2210. int list;
  2211. for(list=0; list<h->list_count; list++){
  2212. if(USES_LIST(top_type, list)){
  2213. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  2214. const int b8_xy= 4*top_xy + 2;
  2215. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2216. AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
  2217. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  2218. h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
  2219. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  2220. h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
  2221. }else{
  2222. AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
  2223. AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2224. }
  2225. if(!IS_INTERLACED(mb_type^left_type[0])){
  2226. if(USES_LIST(left_type[0], list)){
  2227. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  2228. const int b8_xy= 4*left_xy[0] + 1;
  2229. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2230. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
  2231. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
  2232. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
  2233. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
  2234. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2235. h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
  2236. h->ref_cache[list][scan8[0] - 1 +16 ]=
  2237. h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
  2238. }else{
  2239. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
  2240. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
  2241. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
  2242. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
  2243. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2244. h->ref_cache[list][scan8[0] - 1 + 8 ]=
  2245. h->ref_cache[list][scan8[0] - 1 + 16 ]=
  2246. h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
  2247. }
  2248. }
  2249. }
  2250. }
  2251. return 0;
  2252. }
  2253. static void loop_filter(H264Context *h){
  2254. MpegEncContext * const s = &h->s;
  2255. uint8_t *dest_y, *dest_cb, *dest_cr;
  2256. int linesize, uvlinesize, mb_x, mb_y;
  2257. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  2258. const int old_slice_type= h->slice_type;
  2259. const int pixel_shift = h->pixel_shift;
  2260. if(h->deblocking_filter) {
  2261. for(mb_x= 0; mb_x<s->mb_width; mb_x++){
  2262. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  2263. int mb_xy, mb_type;
  2264. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  2265. h->slice_num= h->slice_table[mb_xy];
  2266. mb_type= s->current_picture.mb_type[mb_xy];
  2267. h->list_count= h->list_counts[mb_xy];
  2268. if(FRAME_MBAFF)
  2269. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2270. s->mb_x= mb_x;
  2271. s->mb_y= mb_y;
  2272. dest_y = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize ) * 16;
  2273. dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  2274. dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  2275. //FIXME simplify above
  2276. if (MB_FIELD) {
  2277. linesize = h->mb_linesize = s->linesize * 2;
  2278. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2279. if(mb_y&1){ //FIXME move out of this function?
  2280. dest_y -= s->linesize*15;
  2281. dest_cb-= s->uvlinesize*7;
  2282. dest_cr-= s->uvlinesize*7;
  2283. }
  2284. } else {
  2285. linesize = h->mb_linesize = s->linesize;
  2286. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2287. }
  2288. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2289. if(fill_filter_caches(h, mb_type))
  2290. continue;
  2291. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2292. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2293. if (FRAME_MBAFF) {
  2294. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2295. } else {
  2296. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2297. }
  2298. }
  2299. }
  2300. }
  2301. h->slice_type= old_slice_type;
  2302. s->mb_x= 0;
  2303. s->mb_y= end_mb_y - FRAME_MBAFF;
  2304. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2305. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2306. }
  2307. static void predict_field_decoding_flag(H264Context *h){
  2308. MpegEncContext * const s = &h->s;
  2309. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2310. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2311. ? s->current_picture.mb_type[mb_xy-1]
  2312. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2313. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  2314. : 0;
  2315. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2316. }
  2317. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  2318. H264Context *h = *(void**)arg;
  2319. MpegEncContext * const s = &h->s;
  2320. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  2321. s->mb_skip_run= -1;
  2322. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  2323. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  2324. if( h->pps.cabac ) {
  2325. /* realign */
  2326. align_get_bits( &s->gb );
  2327. /* init cabac */
  2328. ff_init_cabac_states( &h->cabac);
  2329. ff_init_cabac_decoder( &h->cabac,
  2330. s->gb.buffer + get_bits_count(&s->gb)/8,
  2331. (get_bits_left(&s->gb) + 7)/8);
  2332. ff_h264_init_cabac_states(h);
  2333. for(;;){
  2334. //START_TIMER
  2335. int ret = ff_h264_decode_mb_cabac(h);
  2336. int eos;
  2337. //STOP_TIMER("decode_mb_cabac")
  2338. if(ret>=0) ff_h264_hl_decode_mb(h);
  2339. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  2340. s->mb_y++;
  2341. ret = ff_h264_decode_mb_cabac(h);
  2342. if(ret>=0) ff_h264_hl_decode_mb(h);
  2343. s->mb_y--;
  2344. }
  2345. eos = get_cabac_terminate( &h->cabac );
  2346. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  2347. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2348. return 0;
  2349. }
  2350. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  2351. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
  2352. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  2353. return -1;
  2354. }
  2355. if( ++s->mb_x >= s->mb_width ) {
  2356. s->mb_x = 0;
  2357. loop_filter(h);
  2358. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2359. ++s->mb_y;
  2360. if(FIELD_OR_MBAFF_PICTURE) {
  2361. ++s->mb_y;
  2362. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2363. predict_field_decoding_flag(h);
  2364. }
  2365. }
  2366. if( eos || s->mb_y >= s->mb_height ) {
  2367. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2368. 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);
  2369. return 0;
  2370. }
  2371. }
  2372. } else {
  2373. for(;;){
  2374. int ret = ff_h264_decode_mb_cavlc(h);
  2375. if(ret>=0) ff_h264_hl_decode_mb(h);
  2376. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  2377. s->mb_y++;
  2378. ret = ff_h264_decode_mb_cavlc(h);
  2379. if(ret>=0) ff_h264_hl_decode_mb(h);
  2380. s->mb_y--;
  2381. }
  2382. if(ret<0){
  2383. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2384. 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);
  2385. return -1;
  2386. }
  2387. if(++s->mb_x >= s->mb_width){
  2388. s->mb_x=0;
  2389. loop_filter(h);
  2390. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2391. ++s->mb_y;
  2392. if(FIELD_OR_MBAFF_PICTURE) {
  2393. ++s->mb_y;
  2394. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2395. predict_field_decoding_flag(h);
  2396. }
  2397. if(s->mb_y >= s->mb_height){
  2398. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2399. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  2400. 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);
  2401. return 0;
  2402. }else{
  2403. 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);
  2404. return -1;
  2405. }
  2406. }
  2407. }
  2408. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  2409. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2410. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  2411. 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);
  2412. return 0;
  2413. }else{
  2414. 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);
  2415. return -1;
  2416. }
  2417. }
  2418. }
  2419. }
  2420. #if 0
  2421. for(;s->mb_y < s->mb_height; s->mb_y++){
  2422. for(;s->mb_x < s->mb_width; s->mb_x++){
  2423. int ret= decode_mb(h);
  2424. ff_h264_hl_decode_mb(h);
  2425. if(ret<0){
  2426. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2427. 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);
  2428. return -1;
  2429. }
  2430. if(++s->mb_x >= s->mb_width){
  2431. s->mb_x=0;
  2432. if(++s->mb_y >= s->mb_height){
  2433. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2434. 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);
  2435. return 0;
  2436. }else{
  2437. 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);
  2438. return -1;
  2439. }
  2440. }
  2441. }
  2442. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  2443. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2444. 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);
  2445. return 0;
  2446. }else{
  2447. 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);
  2448. return -1;
  2449. }
  2450. }
  2451. }
  2452. s->mb_x=0;
  2453. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2454. }
  2455. #endif
  2456. return -1; //not reached
  2457. }
  2458. /**
  2459. * Call decode_slice() for each context.
  2460. *
  2461. * @param h h264 master context
  2462. * @param context_count number of contexts to execute
  2463. */
  2464. static void execute_decode_slices(H264Context *h, int context_count){
  2465. MpegEncContext * const s = &h->s;
  2466. AVCodecContext * const avctx= s->avctx;
  2467. H264Context *hx;
  2468. int i;
  2469. if (s->avctx->hwaccel)
  2470. return;
  2471. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2472. return;
  2473. if(context_count == 1) {
  2474. decode_slice(avctx, &h);
  2475. } else {
  2476. for(i = 1; i < context_count; i++) {
  2477. hx = h->thread_context[i];
  2478. hx->s.error_recognition = avctx->error_recognition;
  2479. hx->s.error_count = 0;
  2480. }
  2481. avctx->execute(avctx, (void *)decode_slice,
  2482. h->thread_context, NULL, context_count, sizeof(void*));
  2483. /* pull back stuff from slices to master context */
  2484. hx = h->thread_context[context_count - 1];
  2485. s->mb_x = hx->s.mb_x;
  2486. s->mb_y = hx->s.mb_y;
  2487. s->dropable = hx->s.dropable;
  2488. s->picture_structure = hx->s.picture_structure;
  2489. for(i = 1; i < context_count; i++)
  2490. h->s.error_count += h->thread_context[i]->s.error_count;
  2491. }
  2492. }
  2493. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  2494. MpegEncContext * const s = &h->s;
  2495. AVCodecContext * const avctx= s->avctx;
  2496. int buf_index=0;
  2497. H264Context *hx; ///< thread context
  2498. int context_count = 0;
  2499. int next_avc= h->is_avc ? 0 : buf_size;
  2500. h->max_contexts = avctx->thread_count;
  2501. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  2502. h->current_slice = 0;
  2503. if (!s->first_field)
  2504. s->current_picture_ptr= NULL;
  2505. ff_h264_reset_sei(h);
  2506. }
  2507. for(;;){
  2508. int consumed;
  2509. int dst_length;
  2510. int bit_length;
  2511. const uint8_t *ptr;
  2512. int i, nalsize = 0;
  2513. int err;
  2514. if(buf_index >= next_avc) {
  2515. if(buf_index >= buf_size) break;
  2516. nalsize = 0;
  2517. for(i = 0; i < h->nal_length_size; i++)
  2518. nalsize = (nalsize << 8) | buf[buf_index++];
  2519. if(nalsize <= 0 || nalsize > buf_size - buf_index){
  2520. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  2521. break;
  2522. }
  2523. next_avc= buf_index + nalsize;
  2524. } else {
  2525. // start code prefix search
  2526. for(; buf_index + 3 < next_avc; buf_index++){
  2527. // This should always succeed in the first iteration.
  2528. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  2529. break;
  2530. }
  2531. if(buf_index+3 >= buf_size) break;
  2532. buf_index+=3;
  2533. if(buf_index >= next_avc) continue;
  2534. }
  2535. hx = h->thread_context[context_count];
  2536. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  2537. if (ptr==NULL || dst_length < 0){
  2538. return -1;
  2539. }
  2540. i= buf_index + consumed;
  2541. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  2542. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  2543. s->workaround_bugs |= FF_BUG_TRUNCATED;
  2544. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  2545. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  2546. dst_length--;
  2547. }
  2548. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  2549. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  2550. 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);
  2551. }
  2552. if (h->is_avc && (nalsize != consumed) && nalsize){
  2553. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  2554. }
  2555. buf_index += consumed;
  2556. //FIXME do not discard SEI id
  2557. if(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
  2558. continue;
  2559. again:
  2560. err = 0;
  2561. switch(hx->nal_unit_type){
  2562. case NAL_IDR_SLICE:
  2563. if (h->nal_unit_type != NAL_IDR_SLICE) {
  2564. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  2565. return -1;
  2566. }
  2567. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  2568. case NAL_SLICE:
  2569. init_get_bits(&hx->s.gb, ptr, bit_length);
  2570. hx->intra_gb_ptr=
  2571. hx->inter_gb_ptr= &hx->s.gb;
  2572. hx->s.data_partitioning = 0;
  2573. if((err = decode_slice_header(hx, h)))
  2574. break;
  2575. if (h->current_slice == 1) {
  2576. if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  2577. return -1;
  2578. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2579. ff_vdpau_h264_picture_start(s);
  2580. }
  2581. s->current_picture_ptr->key_frame |=
  2582. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  2583. (h->sei_recovery_frame_cnt >= 0);
  2584. if(hx->redundant_pic_count==0
  2585. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2586. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  2587. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  2588. && avctx->skip_frame < AVDISCARD_ALL){
  2589. if(avctx->hwaccel) {
  2590. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  2591. return -1;
  2592. }else
  2593. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  2594. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  2595. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  2596. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  2597. }else
  2598. context_count++;
  2599. }
  2600. break;
  2601. case NAL_DPA:
  2602. init_get_bits(&hx->s.gb, ptr, bit_length);
  2603. hx->intra_gb_ptr=
  2604. hx->inter_gb_ptr= NULL;
  2605. if ((err = decode_slice_header(hx, h)) < 0)
  2606. break;
  2607. hx->s.data_partitioning = 1;
  2608. break;
  2609. case NAL_DPB:
  2610. init_get_bits(&hx->intra_gb, ptr, bit_length);
  2611. hx->intra_gb_ptr= &hx->intra_gb;
  2612. break;
  2613. case NAL_DPC:
  2614. init_get_bits(&hx->inter_gb, ptr, bit_length);
  2615. hx->inter_gb_ptr= &hx->inter_gb;
  2616. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  2617. && s->context_initialized
  2618. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2619. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  2620. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  2621. && avctx->skip_frame < AVDISCARD_ALL)
  2622. context_count++;
  2623. break;
  2624. case NAL_SEI:
  2625. init_get_bits(&s->gb, ptr, bit_length);
  2626. ff_h264_decode_sei(h);
  2627. break;
  2628. case NAL_SPS:
  2629. init_get_bits(&s->gb, ptr, bit_length);
  2630. ff_h264_decode_seq_parameter_set(h);
  2631. if(s->flags& CODEC_FLAG_LOW_DELAY)
  2632. s->low_delay=1;
  2633. if(avctx->has_b_frames < 2)
  2634. avctx->has_b_frames= !s->low_delay;
  2635. if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
  2636. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  2637. avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2638. h->pixel_shift = h->sps.bit_depth_luma > 8;
  2639. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
  2640. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
  2641. dsputil_init(&s->dsp, s->avctx);
  2642. } else {
  2643. av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2644. return -1;
  2645. }
  2646. }
  2647. break;
  2648. case NAL_PPS:
  2649. init_get_bits(&s->gb, ptr, bit_length);
  2650. ff_h264_decode_picture_parameter_set(h, bit_length);
  2651. break;
  2652. case NAL_AUD:
  2653. case NAL_END_SEQUENCE:
  2654. case NAL_END_STREAM:
  2655. case NAL_FILLER_DATA:
  2656. case NAL_SPS_EXT:
  2657. case NAL_AUXILIARY_SLICE:
  2658. break;
  2659. default:
  2660. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  2661. }
  2662. if(context_count == h->max_contexts) {
  2663. execute_decode_slices(h, context_count);
  2664. context_count = 0;
  2665. }
  2666. if (err < 0)
  2667. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  2668. else if(err == 1) {
  2669. /* Slice could not be decoded in parallel mode, copy down
  2670. * NAL unit stuff to context 0 and restart. Note that
  2671. * rbsp_buffer is not transferred, but since we no longer
  2672. * run in parallel mode this should not be an issue. */
  2673. h->nal_unit_type = hx->nal_unit_type;
  2674. h->nal_ref_idc = hx->nal_ref_idc;
  2675. hx = h;
  2676. goto again;
  2677. }
  2678. }
  2679. if(context_count)
  2680. execute_decode_slices(h, context_count);
  2681. return buf_index;
  2682. }
  2683. /**
  2684. * returns the number of bytes consumed for building the current frame
  2685. */
  2686. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  2687. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  2688. if(pos+10>buf_size) pos=buf_size; // oops ;)
  2689. return pos;
  2690. }
  2691. static int decode_frame(AVCodecContext *avctx,
  2692. void *data, int *data_size,
  2693. AVPacket *avpkt)
  2694. {
  2695. const uint8_t *buf = avpkt->data;
  2696. int buf_size = avpkt->size;
  2697. H264Context *h = avctx->priv_data;
  2698. MpegEncContext *s = &h->s;
  2699. AVFrame *pict = data;
  2700. int buf_index;
  2701. s->flags= avctx->flags;
  2702. s->flags2= avctx->flags2;
  2703. /* end of stream, output what is still in the buffers */
  2704. out:
  2705. if (buf_size == 0) {
  2706. Picture *out;
  2707. int i, out_idx;
  2708. //FIXME factorize this with the output code below
  2709. out = h->delayed_pic[0];
  2710. out_idx = 0;
  2711. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2712. if(h->delayed_pic[i]->poc < out->poc){
  2713. out = h->delayed_pic[i];
  2714. out_idx = i;
  2715. }
  2716. for(i=out_idx; h->delayed_pic[i]; i++)
  2717. h->delayed_pic[i] = h->delayed_pic[i+1];
  2718. if(out){
  2719. *data_size = sizeof(AVFrame);
  2720. *pict= *(AVFrame*)out;
  2721. }
  2722. return 0;
  2723. }
  2724. buf_index=decode_nal_units(h, buf, buf_size);
  2725. if(buf_index < 0)
  2726. return -1;
  2727. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  2728. buf_size = 0;
  2729. goto out;
  2730. }
  2731. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  2732. if (avctx->skip_frame >= AVDISCARD_NONREF)
  2733. return 0;
  2734. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  2735. return -1;
  2736. }
  2737. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  2738. Picture *out = s->current_picture_ptr;
  2739. Picture *cur = s->current_picture_ptr;
  2740. int i, pics, out_of_order, out_idx;
  2741. field_end(h);
  2742. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  2743. /* Wait for second field. */
  2744. *data_size = 0;
  2745. } else {
  2746. cur->interlaced_frame = 0;
  2747. cur->repeat_pict = 0;
  2748. /* Signal interlacing information externally. */
  2749. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  2750. if(h->sps.pic_struct_present_flag){
  2751. switch (h->sei_pic_struct)
  2752. {
  2753. case SEI_PIC_STRUCT_FRAME:
  2754. break;
  2755. case SEI_PIC_STRUCT_TOP_FIELD:
  2756. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  2757. cur->interlaced_frame = 1;
  2758. break;
  2759. case SEI_PIC_STRUCT_TOP_BOTTOM:
  2760. case SEI_PIC_STRUCT_BOTTOM_TOP:
  2761. if (FIELD_OR_MBAFF_PICTURE)
  2762. cur->interlaced_frame = 1;
  2763. else
  2764. // try to flag soft telecine progressive
  2765. cur->interlaced_frame = h->prev_interlaced_frame;
  2766. break;
  2767. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  2768. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  2769. // Signal the possibility of telecined film externally (pic_struct 5,6)
  2770. // From these hints, let the applications decide if they apply deinterlacing.
  2771. cur->repeat_pict = 1;
  2772. break;
  2773. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  2774. // Force progressive here, as doubling interlaced frame is a bad idea.
  2775. cur->repeat_pict = 2;
  2776. break;
  2777. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  2778. cur->repeat_pict = 4;
  2779. break;
  2780. }
  2781. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  2782. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  2783. }else{
  2784. /* Derive interlacing flag from used decoding process. */
  2785. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  2786. }
  2787. h->prev_interlaced_frame = cur->interlaced_frame;
  2788. if (cur->field_poc[0] != cur->field_poc[1]){
  2789. /* Derive top_field_first from field pocs. */
  2790. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  2791. }else{
  2792. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  2793. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  2794. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  2795. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  2796. cur->top_field_first = 1;
  2797. else
  2798. cur->top_field_first = 0;
  2799. }else{
  2800. /* Most likely progressive */
  2801. cur->top_field_first = 0;
  2802. }
  2803. }
  2804. //FIXME do something with unavailable reference frames
  2805. /* Sort B-frames into display order */
  2806. if(h->sps.bitstream_restriction_flag
  2807. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  2808. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  2809. s->low_delay = 0;
  2810. }
  2811. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  2812. && !h->sps.bitstream_restriction_flag){
  2813. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  2814. s->low_delay= 0;
  2815. }
  2816. pics = 0;
  2817. while(h->delayed_pic[pics]) pics++;
  2818. assert(pics <= MAX_DELAYED_PIC_COUNT);
  2819. h->delayed_pic[pics++] = cur;
  2820. if(cur->reference == 0)
  2821. cur->reference = DELAYED_PIC_REF;
  2822. out = h->delayed_pic[0];
  2823. out_idx = 0;
  2824. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2825. if(h->delayed_pic[i]->poc < out->poc){
  2826. out = h->delayed_pic[i];
  2827. out_idx = i;
  2828. }
  2829. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  2830. h->outputed_poc= INT_MIN;
  2831. out_of_order = out->poc < h->outputed_poc;
  2832. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  2833. { }
  2834. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  2835. || (s->low_delay &&
  2836. ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
  2837. || cur->pict_type == AV_PICTURE_TYPE_B)))
  2838. {
  2839. s->low_delay = 0;
  2840. s->avctx->has_b_frames++;
  2841. }
  2842. if(out_of_order || pics > s->avctx->has_b_frames){
  2843. out->reference &= ~DELAYED_PIC_REF;
  2844. for(i=out_idx; h->delayed_pic[i]; i++)
  2845. h->delayed_pic[i] = h->delayed_pic[i+1];
  2846. }
  2847. if(!out_of_order && pics > s->avctx->has_b_frames){
  2848. *data_size = sizeof(AVFrame);
  2849. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  2850. h->outputed_poc = INT_MIN;
  2851. } else
  2852. h->outputed_poc = out->poc;
  2853. *pict= *(AVFrame*)out;
  2854. }else{
  2855. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  2856. }
  2857. }
  2858. }
  2859. assert(pict->data[0] || !*data_size);
  2860. ff_print_debug_info(s, pict);
  2861. //printf("out %d\n", (int)pict->data[0]);
  2862. return get_consumed_bytes(s, buf_index, buf_size);
  2863. }
  2864. #if 0
  2865. static inline void fill_mb_avail(H264Context *h){
  2866. MpegEncContext * const s = &h->s;
  2867. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2868. if(s->mb_y){
  2869. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  2870. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  2871. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  2872. }else{
  2873. h->mb_avail[0]=
  2874. h->mb_avail[1]=
  2875. h->mb_avail[2]= 0;
  2876. }
  2877. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  2878. h->mb_avail[4]= 1; //FIXME move out
  2879. h->mb_avail[5]= 0; //FIXME move out
  2880. }
  2881. #endif
  2882. #ifdef TEST
  2883. #undef printf
  2884. #undef random
  2885. #define COUNT 8000
  2886. #define SIZE (COUNT*40)
  2887. int main(void){
  2888. int i;
  2889. uint8_t temp[SIZE];
  2890. PutBitContext pb;
  2891. GetBitContext gb;
  2892. // int int_temp[10000];
  2893. DSPContext dsp;
  2894. AVCodecContext avctx;
  2895. dsputil_init(&dsp, &avctx);
  2896. init_put_bits(&pb, temp, SIZE);
  2897. printf("testing unsigned exp golomb\n");
  2898. for(i=0; i<COUNT; i++){
  2899. START_TIMER
  2900. set_ue_golomb(&pb, i);
  2901. STOP_TIMER("set_ue_golomb");
  2902. }
  2903. flush_put_bits(&pb);
  2904. init_get_bits(&gb, temp, 8*SIZE);
  2905. for(i=0; i<COUNT; i++){
  2906. int j, s;
  2907. s= show_bits(&gb, 24);
  2908. START_TIMER
  2909. j= get_ue_golomb(&gb);
  2910. if(j != i){
  2911. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2912. // return -1;
  2913. }
  2914. STOP_TIMER("get_ue_golomb");
  2915. }
  2916. init_put_bits(&pb, temp, SIZE);
  2917. printf("testing signed exp golomb\n");
  2918. for(i=0; i<COUNT; i++){
  2919. START_TIMER
  2920. set_se_golomb(&pb, i - COUNT/2);
  2921. STOP_TIMER("set_se_golomb");
  2922. }
  2923. flush_put_bits(&pb);
  2924. init_get_bits(&gb, temp, 8*SIZE);
  2925. for(i=0; i<COUNT; i++){
  2926. int j, s;
  2927. s= show_bits(&gb, 24);
  2928. START_TIMER
  2929. j= get_se_golomb(&gb);
  2930. if(j != i - COUNT/2){
  2931. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2932. // return -1;
  2933. }
  2934. STOP_TIMER("get_se_golomb");
  2935. }
  2936. #if 0
  2937. printf("testing 4x4 (I)DCT\n");
  2938. DCTELEM block[16];
  2939. uint8_t src[16], ref[16];
  2940. uint64_t error= 0, max_error=0;
  2941. for(i=0; i<COUNT; i++){
  2942. int j;
  2943. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  2944. for(j=0; j<16; j++){
  2945. ref[j]= random()%255;
  2946. src[j]= random()%255;
  2947. }
  2948. h264_diff_dct_c(block, src, ref, 4);
  2949. //normalize
  2950. for(j=0; j<16; j++){
  2951. // printf("%d ", block[j]);
  2952. block[j]= block[j]*4;
  2953. if(j&1) block[j]= (block[j]*4 + 2)/5;
  2954. if(j&4) block[j]= (block[j]*4 + 2)/5;
  2955. }
  2956. // printf("\n");
  2957. h->h264dsp.h264_idct_add(ref, block, 4);
  2958. /* for(j=0; j<16; j++){
  2959. printf("%d ", ref[j]);
  2960. }
  2961. printf("\n");*/
  2962. for(j=0; j<16; j++){
  2963. int diff= FFABS(src[j] - ref[j]);
  2964. error+= diff*diff;
  2965. max_error= FFMAX(max_error, diff);
  2966. }
  2967. }
  2968. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  2969. printf("testing quantizer\n");
  2970. for(qp=0; qp<52; qp++){
  2971. for(i=0; i<16; i++)
  2972. src1_block[i]= src2_block[i]= random()%255;
  2973. }
  2974. printf("Testing NAL layer\n");
  2975. uint8_t bitstream[COUNT];
  2976. uint8_t nal[COUNT*2];
  2977. H264Context h;
  2978. memset(&h, 0, sizeof(H264Context));
  2979. for(i=0; i<COUNT; i++){
  2980. int zeros= i;
  2981. int nal_length;
  2982. int consumed;
  2983. int out_length;
  2984. uint8_t *out;
  2985. int j;
  2986. for(j=0; j<COUNT; j++){
  2987. bitstream[j]= (random() % 255) + 1;
  2988. }
  2989. for(j=0; j<zeros; j++){
  2990. int pos= random() % COUNT;
  2991. while(bitstream[pos] == 0){
  2992. pos++;
  2993. pos %= COUNT;
  2994. }
  2995. bitstream[pos]=0;
  2996. }
  2997. START_TIMER
  2998. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  2999. if(nal_length<0){
  3000. printf("encoding failed\n");
  3001. return -1;
  3002. }
  3003. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  3004. STOP_TIMER("NAL")
  3005. if(out_length != COUNT){
  3006. printf("incorrect length %d %d\n", out_length, COUNT);
  3007. return -1;
  3008. }
  3009. if(consumed != nal_length){
  3010. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  3011. return -1;
  3012. }
  3013. if(memcmp(bitstream, out, COUNT)){
  3014. printf("mismatch\n");
  3015. return -1;
  3016. }
  3017. }
  3018. #endif
  3019. printf("Testing RBSP\n");
  3020. return 0;
  3021. }
  3022. #endif /* TEST */
  3023. av_cold void ff_h264_free_context(H264Context *h)
  3024. {
  3025. int i;
  3026. free_tables(h, 1); //FIXME cleanup init stuff perhaps
  3027. for(i = 0; i < MAX_SPS_COUNT; i++)
  3028. av_freep(h->sps_buffers + i);
  3029. for(i = 0; i < MAX_PPS_COUNT; i++)
  3030. av_freep(h->pps_buffers + i);
  3031. }
  3032. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  3033. {
  3034. H264Context *h = avctx->priv_data;
  3035. MpegEncContext *s = &h->s;
  3036. ff_h264_free_context(h);
  3037. MPV_common_end(s);
  3038. // memset(h, 0, sizeof(H264Context));
  3039. return 0;
  3040. }
  3041. static const AVProfile profiles[] = {
  3042. { FF_PROFILE_H264_BASELINE, "Baseline" },
  3043. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  3044. { FF_PROFILE_H264_MAIN, "Main" },
  3045. { FF_PROFILE_H264_EXTENDED, "Extended" },
  3046. { FF_PROFILE_H264_HIGH, "High" },
  3047. { FF_PROFILE_H264_HIGH_10, "High 10" },
  3048. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  3049. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  3050. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  3051. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  3052. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  3053. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  3054. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  3055. { FF_PROFILE_UNKNOWN },
  3056. };
  3057. AVCodec ff_h264_decoder = {
  3058. "h264",
  3059. AVMEDIA_TYPE_VIDEO,
  3060. CODEC_ID_H264,
  3061. sizeof(H264Context),
  3062. ff_h264_decode_init,
  3063. NULL,
  3064. ff_h264_decode_end,
  3065. decode_frame,
  3066. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  3067. CODEC_CAP_SLICE_THREADS,
  3068. .flush= flush_dpb,
  3069. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  3070. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3071. };
  3072. #if CONFIG_H264_VDPAU_DECODER
  3073. AVCodec ff_h264_vdpau_decoder = {
  3074. "h264_vdpau",
  3075. AVMEDIA_TYPE_VIDEO,
  3076. CODEC_ID_H264,
  3077. sizeof(H264Context),
  3078. ff_h264_decode_init,
  3079. NULL,
  3080. ff_h264_decode_end,
  3081. decode_frame,
  3082. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  3083. .flush= flush_dpb,
  3084. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  3085. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  3086. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3087. };
  3088. #endif