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.

4200 lines
162KB

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