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.

4211 lines
160KB

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