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.

4260 lines
162KB

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