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.

4194 lines
159KB

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