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.

4263 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 || size <= 0)
  900. return -1;
  901. if(buf[0] == 1){
  902. int i, cnt, nalsize;
  903. const unsigned char *p = buf;
  904. h->is_avc = 1;
  905. if(size < 7) {
  906. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  907. return -1;
  908. }
  909. /* sps and pps in the avcC always have length coded with 2 bytes,
  910. so put a fake nal_length_size = 2 while parsing them */
  911. h->nal_length_size = 2;
  912. // Decode sps from avcC
  913. cnt = *(p+5) & 0x1f; // Number of sps
  914. p += 6;
  915. for (i = 0; i < cnt; i++) {
  916. nalsize = AV_RB16(p) + 2;
  917. if(nalsize > size - (p-buf))
  918. return -1;
  919. if(decode_nal_units(h, p, nalsize) < 0) {
  920. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  921. return -1;
  922. }
  923. p += nalsize;
  924. }
  925. // Decode pps from avcC
  926. cnt = *(p++); // Number of pps
  927. for (i = 0; i < cnt; i++) {
  928. nalsize = AV_RB16(p) + 2;
  929. if(nalsize > size - (p-buf))
  930. return -1;
  931. if (decode_nal_units(h, p, nalsize) < 0) {
  932. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  933. return -1;
  934. }
  935. p += nalsize;
  936. }
  937. // Now store right nal length size, that will be use to parse all other nals
  938. h->nal_length_size = (buf[4] & 0x03) + 1;
  939. } else {
  940. h->is_avc = 0;
  941. if(decode_nal_units(h, buf, size) < 0)
  942. return -1;
  943. }
  944. return 0;
  945. }
  946. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  947. H264Context *h= avctx->priv_data;
  948. MpegEncContext * const s = &h->s;
  949. MPV_decode_defaults(s);
  950. s->avctx = avctx;
  951. common_init(h);
  952. s->out_format = FMT_H264;
  953. s->workaround_bugs= avctx->workaround_bugs;
  954. // set defaults
  955. // s->decode_mb= ff_h263_decode_mb;
  956. s->quarter_sample = 1;
  957. if(!avctx->has_b_frames)
  958. s->low_delay= 1;
  959. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  960. ff_h264_decode_init_vlc();
  961. h->pixel_shift = 0;
  962. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  963. h->thread_context[0] = h;
  964. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  965. h->prev_poc_msb= 1<<16;
  966. h->x264_build = -1;
  967. ff_h264_reset_sei(h);
  968. if(avctx->codec_id == CODEC_ID_H264){
  969. if(avctx->ticks_per_frame == 1){
  970. s->avctx->time_base.den *=2;
  971. }
  972. avctx->ticks_per_frame = 2;
  973. }
  974. if(avctx->extradata_size > 0 && avctx->extradata &&
  975. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size))
  976. return -1;
  977. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  978. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  979. s->low_delay = 0;
  980. }
  981. return 0;
  982. }
  983. #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b)+(size))))
  984. static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
  985. {
  986. int i;
  987. for (i=0; i<count; i++){
  988. assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
  989. IN_RANGE(from[i], old_base->picture, sizeof(Picture) * old_base->picture_count) ||
  990. !from[i]));
  991. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  992. }
  993. }
  994. static void copy_parameter_set(void **to, void **from, int count, int size)
  995. {
  996. int i;
  997. for (i=0; i<count; i++){
  998. if (to[i] && !from[i]) av_freep(&to[i]);
  999. else if (from[i] && !to[i]) to[i] = av_malloc(size);
  1000. if (from[i]) memcpy(to[i], from[i], size);
  1001. }
  1002. }
  1003. static int decode_init_thread_copy(AVCodecContext *avctx){
  1004. H264Context *h= avctx->priv_data;
  1005. if (!avctx->is_copy) return 0;
  1006. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1007. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1008. return 0;
  1009. }
  1010. #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
  1011. static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
  1012. H264Context *h= dst->priv_data, *h1= src->priv_data;
  1013. MpegEncContext * const s = &h->s, * const s1 = &h1->s;
  1014. int inited = s->context_initialized, err;
  1015. int i;
  1016. if(dst == src || !s1->context_initialized) return 0;
  1017. err = ff_mpeg_update_thread_context(dst, src);
  1018. if(err) return err;
  1019. //FIXME handle width/height changing
  1020. if(!inited){
  1021. for(i = 0; i < MAX_SPS_COUNT; i++)
  1022. av_freep(h->sps_buffers + i);
  1023. for(i = 0; i < MAX_PPS_COUNT; i++)
  1024. av_freep(h->pps_buffers + i);
  1025. memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
  1026. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1027. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1028. if (ff_h264_alloc_tables(h) < 0) {
  1029. av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
  1030. return AVERROR(ENOMEM);
  1031. }
  1032. context_init(h);
  1033. for(i=0; i<2; i++){
  1034. h->rbsp_buffer[i] = NULL;
  1035. h->rbsp_buffer_size[i] = 0;
  1036. }
  1037. h->thread_context[0] = h;
  1038. // frame_start may not be called for the next thread (if it's decoding a bottom field)
  1039. // so this has to be allocated here
  1040. h->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
  1041. s->dsp.clear_blocks(h->mb);
  1042. s->dsp.clear_blocks(h->mb+(24*16<<h->pixel_shift));
  1043. }
  1044. //extradata/NAL handling
  1045. h->is_avc = h1->is_avc;
  1046. //SPS/PPS
  1047. copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
  1048. h->sps = h1->sps;
  1049. copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
  1050. h->pps = h1->pps;
  1051. //Dequantization matrices
  1052. //FIXME these are big - can they be only copied when PPS changes?
  1053. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  1054. for(i=0; i<6; i++)
  1055. h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  1056. for(i=0; i<6; i++)
  1057. h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  1058. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  1059. //POC timing
  1060. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  1061. //reference lists
  1062. copy_fields(h, h1, ref_count, list_count);
  1063. copy_fields(h, h1, ref_list, intra_gb);
  1064. copy_fields(h, h1, short_ref, cabac_init_idc);
  1065. copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
  1066. copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
  1067. copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
  1068. h->last_slice_type = h1->last_slice_type;
  1069. if(!s->current_picture_ptr) return 0;
  1070. if(!s->dropable) {
  1071. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1072. h->prev_poc_msb = h->poc_msb;
  1073. h->prev_poc_lsb = h->poc_lsb;
  1074. }
  1075. h->prev_frame_num_offset= h->frame_num_offset;
  1076. h->prev_frame_num = h->frame_num;
  1077. h->outputed_poc = h->next_outputed_poc;
  1078. return 0;
  1079. }
  1080. int ff_h264_frame_start(H264Context *h){
  1081. MpegEncContext * const s = &h->s;
  1082. int i;
  1083. const int pixel_shift = h->pixel_shift;
  1084. int thread_count = (s->avctx->active_thread_type & FF_THREAD_SLICE) ? s->avctx->thread_count : 1;
  1085. if(MPV_frame_start(s, s->avctx) < 0)
  1086. return -1;
  1087. ff_er_frame_start(s);
  1088. /*
  1089. * MPV_frame_start uses pict_type to derive key_frame.
  1090. * This is incorrect for H.264; IDR markings must be used.
  1091. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1092. * See decode_nal_units().
  1093. */
  1094. s->current_picture_ptr->key_frame= 0;
  1095. s->current_picture_ptr->mmco_reset= 0;
  1096. assert(s->linesize && s->uvlinesize);
  1097. for(i=0; i<16; i++){
  1098. h->block_offset[i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1099. h->block_offset[48+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  1100. }
  1101. for(i=0; i<16; i++){
  1102. h->block_offset[16+i]=
  1103. h->block_offset[32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1104. h->block_offset[48+16+i]=
  1105. h->block_offset[48+32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1106. }
  1107. /* can't be in alloc_tables because linesize isn't known there.
  1108. * FIXME: redo bipred weight to not require extra buffer? */
  1109. for(i = 0; i < thread_count; i++)
  1110. if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  1111. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
  1112. /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
  1113. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  1114. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1115. // We mark the current picture as non-reference after allocating it, so
  1116. // that if we break out due to an error it can be released automatically
  1117. // in the next MPV_frame_start().
  1118. // SVQ3 as well as most other codecs have only last/next/current and thus
  1119. // get released even with set reference, besides SVQ3 and others do not
  1120. // mark frames as reference later "naturally".
  1121. if(s->codec_id != CODEC_ID_SVQ3)
  1122. s->current_picture_ptr->reference= 0;
  1123. s->current_picture_ptr->field_poc[0]=
  1124. s->current_picture_ptr->field_poc[1]= INT_MAX;
  1125. h->next_output_pic = NULL;
  1126. assert(s->current_picture_ptr->long_ref==0);
  1127. return 0;
  1128. }
  1129. /**
  1130. * Run setup operations that must be run after slice header decoding.
  1131. * This includes finding the next displayed frame.
  1132. *
  1133. * @param h h264 master context
  1134. * @param setup_finished enough NALs have been read that we can call
  1135. * ff_thread_finish_setup()
  1136. */
  1137. static void decode_postinit(H264Context *h, int setup_finished){
  1138. MpegEncContext * const s = &h->s;
  1139. Picture *out = s->current_picture_ptr;
  1140. Picture *cur = s->current_picture_ptr;
  1141. int i, pics, out_of_order, out_idx;
  1142. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  1143. s->current_picture_ptr->pict_type= s->pict_type;
  1144. if (h->next_output_pic) return;
  1145. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  1146. //FIXME: if we have two PAFF fields in one packet, we can't start the next thread here.
  1147. //If we have one field per packet, we can. The check in decode_nal_units() is not good enough
  1148. //to find this yet, so we assume the worst for now.
  1149. //if (setup_finished)
  1150. // ff_thread_finish_setup(s->avctx);
  1151. return;
  1152. }
  1153. cur->interlaced_frame = 0;
  1154. cur->repeat_pict = 0;
  1155. /* Signal interlacing information externally. */
  1156. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  1157. if(h->sps.pic_struct_present_flag){
  1158. switch (h->sei_pic_struct)
  1159. {
  1160. case SEI_PIC_STRUCT_FRAME:
  1161. break;
  1162. case SEI_PIC_STRUCT_TOP_FIELD:
  1163. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  1164. cur->interlaced_frame = 1;
  1165. break;
  1166. case SEI_PIC_STRUCT_TOP_BOTTOM:
  1167. case SEI_PIC_STRUCT_BOTTOM_TOP:
  1168. if (FIELD_OR_MBAFF_PICTURE)
  1169. cur->interlaced_frame = 1;
  1170. else
  1171. // try to flag soft telecine progressive
  1172. cur->interlaced_frame = h->prev_interlaced_frame;
  1173. break;
  1174. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  1175. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  1176. // Signal the possibility of telecined film externally (pic_struct 5,6)
  1177. // From these hints, let the applications decide if they apply deinterlacing.
  1178. cur->repeat_pict = 1;
  1179. break;
  1180. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  1181. // Force progressive here, as doubling interlaced frame is a bad idea.
  1182. cur->repeat_pict = 2;
  1183. break;
  1184. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  1185. cur->repeat_pict = 4;
  1186. break;
  1187. }
  1188. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  1189. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  1190. }else{
  1191. /* Derive interlacing flag from used decoding process. */
  1192. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  1193. }
  1194. h->prev_interlaced_frame = cur->interlaced_frame;
  1195. if (cur->field_poc[0] != cur->field_poc[1]){
  1196. /* Derive top_field_first from field pocs. */
  1197. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  1198. }else{
  1199. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  1200. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  1201. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  1202. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  1203. cur->top_field_first = 1;
  1204. else
  1205. cur->top_field_first = 0;
  1206. }else{
  1207. /* Most likely progressive */
  1208. cur->top_field_first = 0;
  1209. }
  1210. }
  1211. //FIXME do something with unavailable reference frames
  1212. /* Sort B-frames into display order */
  1213. if(h->sps.bitstream_restriction_flag
  1214. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  1215. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  1216. s->low_delay = 0;
  1217. }
  1218. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  1219. && !h->sps.bitstream_restriction_flag){
  1220. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  1221. s->low_delay= 0;
  1222. }
  1223. pics = 0;
  1224. while(h->delayed_pic[pics]) pics++;
  1225. av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
  1226. h->delayed_pic[pics++] = cur;
  1227. if(cur->reference == 0)
  1228. cur->reference = DELAYED_PIC_REF;
  1229. out = h->delayed_pic[0];
  1230. out_idx = 0;
  1231. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  1232. if(h->delayed_pic[i]->poc < out->poc){
  1233. out = h->delayed_pic[i];
  1234. out_idx = i;
  1235. }
  1236. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  1237. h->next_outputed_poc= INT_MIN;
  1238. out_of_order = out->poc < h->next_outputed_poc;
  1239. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  1240. { }
  1241. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  1242. || (s->low_delay &&
  1243. ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
  1244. || cur->pict_type == AV_PICTURE_TYPE_B)))
  1245. {
  1246. s->low_delay = 0;
  1247. s->avctx->has_b_frames++;
  1248. }
  1249. if(out_of_order || pics > s->avctx->has_b_frames){
  1250. out->reference &= ~DELAYED_PIC_REF;
  1251. out->owner2 = s; // for frame threading, the owner must be the second field's thread
  1252. // or else the first thread can release the picture and reuse it unsafely
  1253. for(i=out_idx; h->delayed_pic[i]; i++)
  1254. h->delayed_pic[i] = h->delayed_pic[i+1];
  1255. }
  1256. if(!out_of_order && pics > s->avctx->has_b_frames){
  1257. h->next_output_pic = out;
  1258. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  1259. h->next_outputed_poc = INT_MIN;
  1260. } else
  1261. h->next_outputed_poc = out->poc;
  1262. }else{
  1263. av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
  1264. }
  1265. if (setup_finished)
  1266. ff_thread_finish_setup(s->avctx);
  1267. }
  1268. 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){
  1269. MpegEncContext * const s = &h->s;
  1270. uint8_t *top_border;
  1271. int top_idx = 1;
  1272. const int pixel_shift = h->pixel_shift;
  1273. src_y -= linesize;
  1274. src_cb -= uvlinesize;
  1275. src_cr -= uvlinesize;
  1276. if(!simple && FRAME_MBAFF){
  1277. if(s->mb_y&1){
  1278. if(!MB_MBAFF){
  1279. top_border = h->top_borders[0][s->mb_x];
  1280. AV_COPY128(top_border, src_y + 15*linesize);
  1281. if (pixel_shift)
  1282. AV_COPY128(top_border+16, src_y+15*linesize+16);
  1283. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1284. if(chroma444){
  1285. if (pixel_shift){
  1286. AV_COPY128(top_border+32, src_cb + 15*uvlinesize);
  1287. AV_COPY128(top_border+48, src_cb + 15*uvlinesize+16);
  1288. AV_COPY128(top_border+64, src_cr + 15*uvlinesize);
  1289. AV_COPY128(top_border+80, src_cr + 15*uvlinesize+16);
  1290. } else {
  1291. AV_COPY128(top_border+16, src_cb + 15*uvlinesize);
  1292. AV_COPY128(top_border+32, src_cr + 15*uvlinesize);
  1293. }
  1294. } else {
  1295. if (pixel_shift) {
  1296. AV_COPY128(top_border+32, src_cb+7*uvlinesize);
  1297. AV_COPY128(top_border+48, src_cr+7*uvlinesize);
  1298. } else {
  1299. AV_COPY64(top_border+16, src_cb+7*uvlinesize);
  1300. AV_COPY64(top_border+24, src_cr+7*uvlinesize);
  1301. }
  1302. }
  1303. }
  1304. }
  1305. }else if(MB_MBAFF){
  1306. top_idx = 0;
  1307. }else
  1308. return;
  1309. }
  1310. top_border = h->top_borders[top_idx][s->mb_x];
  1311. // There are two lines saved, the line above the the top macroblock of a pair,
  1312. // and the line above the bottom macroblock
  1313. AV_COPY128(top_border, src_y + 16*linesize);
  1314. if (pixel_shift)
  1315. AV_COPY128(top_border+16, src_y+16*linesize+16);
  1316. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1317. if(chroma444){
  1318. if (pixel_shift){
  1319. AV_COPY128(top_border+32, src_cb + 16*linesize);
  1320. AV_COPY128(top_border+48, src_cb + 16*linesize+16);
  1321. AV_COPY128(top_border+64, src_cr + 16*linesize);
  1322. AV_COPY128(top_border+80, src_cr + 16*linesize+16);
  1323. } else {
  1324. AV_COPY128(top_border+16, src_cb + 16*linesize);
  1325. AV_COPY128(top_border+32, src_cr + 16*linesize);
  1326. }
  1327. } else {
  1328. if (pixel_shift) {
  1329. AV_COPY128(top_border+32, src_cb+8*uvlinesize);
  1330. AV_COPY128(top_border+48, src_cr+8*uvlinesize);
  1331. } else {
  1332. AV_COPY64(top_border+16, src_cb+8*uvlinesize);
  1333. AV_COPY64(top_border+24, src_cr+8*uvlinesize);
  1334. }
  1335. }
  1336. }
  1337. }
  1338. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  1339. uint8_t *src_cb, uint8_t *src_cr,
  1340. int linesize, int uvlinesize,
  1341. int xchg, int chroma444,
  1342. int simple, int pixel_shift){
  1343. MpegEncContext * const s = &h->s;
  1344. int deblock_topleft;
  1345. int deblock_top;
  1346. int top_idx = 1;
  1347. uint8_t *top_border_m1;
  1348. uint8_t *top_border;
  1349. if(!simple && FRAME_MBAFF){
  1350. if(s->mb_y&1){
  1351. if(!MB_MBAFF)
  1352. return;
  1353. }else{
  1354. top_idx = MB_MBAFF ? 0 : 1;
  1355. }
  1356. }
  1357. if(h->deblocking_filter == 2) {
  1358. deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
  1359. deblock_top = h->top_type;
  1360. } else {
  1361. deblock_topleft = (s->mb_x > 0);
  1362. deblock_top = (s->mb_y > !!MB_FIELD);
  1363. }
  1364. src_y -= linesize + 1 + pixel_shift;
  1365. src_cb -= uvlinesize + 1 + pixel_shift;
  1366. src_cr -= uvlinesize + 1 + pixel_shift;
  1367. top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
  1368. top_border = h->top_borders[top_idx][s->mb_x];
  1369. #define XCHG(a,b,xchg)\
  1370. if (pixel_shift) {\
  1371. if (xchg) {\
  1372. AV_SWAP64(b+0,a+0);\
  1373. AV_SWAP64(b+8,a+8);\
  1374. } else {\
  1375. AV_COPY128(b,a); \
  1376. }\
  1377. } else \
  1378. if (xchg) AV_SWAP64(b,a);\
  1379. else AV_COPY64(b,a);
  1380. if(deblock_top){
  1381. if(deblock_topleft){
  1382. XCHG(top_border_m1 + (8 << pixel_shift), src_y - (7 << pixel_shift), 1);
  1383. }
  1384. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  1385. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  1386. if(s->mb_x+1 < s->mb_width){
  1387. XCHG(h->top_borders[top_idx][s->mb_x+1], src_y + (17 << pixel_shift), 1);
  1388. }
  1389. }
  1390. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1391. if(chroma444){
  1392. if(deblock_topleft){
  1393. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1394. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1395. }
  1396. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  1397. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  1398. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  1399. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  1400. if(s->mb_x+1 < s->mb_width){
  1401. XCHG(h->top_borders[top_idx][s->mb_x+1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  1402. XCHG(h->top_borders[top_idx][s->mb_x+1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  1403. }
  1404. } else {
  1405. if(deblock_top){
  1406. if(deblock_topleft){
  1407. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1408. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1409. }
  1410. XCHG(top_border + (16 << pixel_shift), src_cb+1+pixel_shift, 1);
  1411. XCHG(top_border + (24 << pixel_shift), src_cr+1+pixel_shift, 1);
  1412. }
  1413. }
  1414. }
  1415. }
  1416. static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth, int index) {
  1417. if (high_bit_depth) {
  1418. return AV_RN32A(((int32_t*)mb) + index);
  1419. } else
  1420. return AV_RN16A(mb + index);
  1421. }
  1422. static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth, int index, int value) {
  1423. if (high_bit_depth) {
  1424. AV_WN32A(((int32_t*)mb) + index, value);
  1425. } else
  1426. AV_WN16A(mb + index, value);
  1427. }
  1428. static av_always_inline void hl_decode_mb_predict_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
  1429. int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
  1430. {
  1431. MpegEncContext * const s = &h->s;
  1432. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1433. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  1434. int i;
  1435. int qscale = p == 0 ? s->qscale : h->chroma_qp[p-1];
  1436. block_offset += 16*p;
  1437. if(IS_INTRA4x4(mb_type)){
  1438. if(simple || !s->encoding){
  1439. if(IS_8x8DCT(mb_type)){
  1440. if(transform_bypass){
  1441. idct_dc_add =
  1442. idct_add = s->dsp.add_pixels8;
  1443. }else{
  1444. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1445. idct_add = h->h264dsp.h264_idct8_add;
  1446. }
  1447. for(i=0; i<16; i+=4){
  1448. uint8_t * const ptr= dest_y + block_offset[i];
  1449. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1450. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1451. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1452. }else{
  1453. const int nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
  1454. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1455. (h->topright_samples_available<<i)&0x4000, linesize);
  1456. if(nnz){
  1457. if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
  1458. idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1459. else
  1460. idct_add (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1461. }
  1462. }
  1463. }
  1464. }else{
  1465. if(transform_bypass){
  1466. idct_dc_add =
  1467. idct_add = s->dsp.add_pixels4;
  1468. }else{
  1469. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1470. idct_add = h->h264dsp.h264_idct_add;
  1471. }
  1472. for(i=0; i<16; i++){
  1473. uint8_t * const ptr= dest_y + block_offset[i];
  1474. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1475. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1476. h->hpc.pred4x4_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1477. }else{
  1478. uint8_t *topright;
  1479. int nnz, tr;
  1480. uint64_t tr_high;
  1481. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1482. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1483. assert(s->mb_y || linesize <= block_offset[i]);
  1484. if(!topright_avail){
  1485. if (pixel_shift) {
  1486. tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
  1487. topright= (uint8_t*) &tr_high;
  1488. } else {
  1489. tr= ptr[3 - linesize]*0x01010101;
  1490. topright= (uint8_t*) &tr;
  1491. }
  1492. }else
  1493. topright= ptr + (4 << pixel_shift) - linesize;
  1494. }else
  1495. topright= NULL;
  1496. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1497. nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
  1498. if(nnz){
  1499. if(is_h264){
  1500. if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
  1501. idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1502. else
  1503. idct_add (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1504. }else
  1505. ff_svq3_add_idct_c(ptr, h->mb + i*16+p*256, linesize, qscale, 0);
  1506. }
  1507. }
  1508. }
  1509. }
  1510. }
  1511. }else{
  1512. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1513. if(is_h264){
  1514. if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX+p] ]){
  1515. if(!transform_bypass)
  1516. h->h264dsp.h264_luma_dc_dequant_idct(h->mb+(p*256 << pixel_shift), h->mb_luma_dc[p], h->dequant4_coeff[p][qscale][0]);
  1517. else{
  1518. static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
  1519. 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
  1520. for(i = 0; i < 16; i++)
  1521. dctcoef_set(h->mb+p*256, pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
  1522. }
  1523. }
  1524. }else
  1525. ff_svq3_luma_dc_dequant_idct_c(h->mb+p*256, h->mb_luma_dc[p], qscale);
  1526. }
  1527. }
  1528. static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
  1529. int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
  1530. {
  1531. MpegEncContext * const s = &h->s;
  1532. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1533. int i;
  1534. block_offset += 16*p;
  1535. if(!IS_INTRA4x4(mb_type)){
  1536. if(is_h264){
  1537. if(IS_INTRA16x16(mb_type)){
  1538. if(transform_bypass){
  1539. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1540. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize);
  1541. }else{
  1542. for(i=0; i<16; i++){
  1543. if(h->non_zero_count_cache[ scan8[i+p*16] ] || dctcoef_get(h->mb, pixel_shift, i*16+p*256))
  1544. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
  1545. }
  1546. }
  1547. }else{
  1548. h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
  1549. }
  1550. }else if(h->cbp&15){
  1551. if(transform_bypass){
  1552. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1553. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1554. for(i=0; i<16; i+=di){
  1555. if(h->non_zero_count_cache[ scan8[i+p*16] ]){
  1556. idct_add(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
  1557. }
  1558. }
  1559. }else{
  1560. if(IS_8x8DCT(mb_type)){
  1561. h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
  1562. }else{
  1563. h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
  1564. }
  1565. }
  1566. }
  1567. }else{
  1568. for(i=0; i<16; i++){
  1569. if(h->non_zero_count_cache[ scan8[i+p*16] ] || h->mb[i*16+p*256]){ //FIXME benchmark weird rule, & below
  1570. uint8_t * const ptr= dest_y + block_offset[i];
  1571. ff_svq3_add_idct_c(ptr, h->mb + i*16 + p*256, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1572. }
  1573. }
  1574. }
  1575. }
  1576. }
  1577. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift){
  1578. MpegEncContext * const s = &h->s;
  1579. const int mb_x= s->mb_x;
  1580. const int mb_y= s->mb_y;
  1581. const int mb_xy= h->mb_xy;
  1582. const int mb_type= s->current_picture.mb_type[mb_xy];
  1583. uint8_t *dest_y, *dest_cb, *dest_cr;
  1584. int linesize, uvlinesize /*dct_offset*/;
  1585. int i, j;
  1586. int *block_offset = &h->block_offset[0];
  1587. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  1588. /* is_h264 should always be true if SVQ3 is disabled. */
  1589. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  1590. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1591. dest_y = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize ) * 16;
  1592. dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  1593. dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  1594. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
  1595. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
  1596. h->list_counts[mb_xy]= h->list_count;
  1597. if (!simple && MB_FIELD) {
  1598. linesize = h->mb_linesize = s->linesize * 2;
  1599. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  1600. block_offset = &h->block_offset[48];
  1601. if(mb_y&1){ //FIXME move out of this function?
  1602. dest_y -= s->linesize*15;
  1603. dest_cb-= s->uvlinesize*7;
  1604. dest_cr-= s->uvlinesize*7;
  1605. }
  1606. if(FRAME_MBAFF) {
  1607. int list;
  1608. for(list=0; list<h->list_count; list++){
  1609. if(!USES_LIST(mb_type, list))
  1610. continue;
  1611. if(IS_16X16(mb_type)){
  1612. int8_t *ref = &h->ref_cache[list][scan8[0]];
  1613. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  1614. }else{
  1615. for(i=0; i<16; i+=4){
  1616. int ref = h->ref_cache[list][scan8[i]];
  1617. if(ref >= 0)
  1618. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  1619. }
  1620. }
  1621. }
  1622. }
  1623. } else {
  1624. linesize = h->mb_linesize = s->linesize;
  1625. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  1626. // dct_offset = s->linesize * 16;
  1627. }
  1628. if (!simple && IS_INTRA_PCM(mb_type)) {
  1629. if (pixel_shift) {
  1630. const int bit_depth = h->sps.bit_depth_luma;
  1631. int j;
  1632. GetBitContext gb;
  1633. init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
  1634. for (i = 0; i < 16; i++) {
  1635. uint16_t *tmp_y = (uint16_t*)(dest_y + i*linesize);
  1636. for (j = 0; j < 16; j++)
  1637. tmp_y[j] = get_bits(&gb, bit_depth);
  1638. }
  1639. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1640. if (!h->sps.chroma_format_idc) {
  1641. for (i = 0; i < 8; i++) {
  1642. uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
  1643. for (j = 0; j < 8; j++) {
  1644. tmp_cb[j] = 1 << (bit_depth - 1);
  1645. }
  1646. }
  1647. for (i = 0; i < 8; i++) {
  1648. uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
  1649. for (j = 0; j < 8; j++) {
  1650. tmp_cr[j] = 1 << (bit_depth - 1);
  1651. }
  1652. }
  1653. } else {
  1654. for (i = 0; i < 8; i++) {
  1655. uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
  1656. for (j = 0; j < 8; j++)
  1657. tmp_cb[j] = get_bits(&gb, bit_depth);
  1658. }
  1659. for (i = 0; i < 8; i++) {
  1660. uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
  1661. for (j = 0; j < 8; j++)
  1662. tmp_cr[j] = get_bits(&gb, bit_depth);
  1663. }
  1664. }
  1665. }
  1666. } else {
  1667. for (i=0; i<16; i++) {
  1668. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  1669. }
  1670. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1671. if (!h->sps.chroma_format_idc) {
  1672. for (i = 0; i < 8; i++) {
  1673. memset(dest_cb + i*uvlinesize, 128, 8);
  1674. memset(dest_cr + i*uvlinesize, 128, 8);
  1675. }
  1676. } else {
  1677. for (i = 0; i < 8; i++) {
  1678. memcpy(dest_cb + i*uvlinesize, h->mb + 128 + i*4, 8);
  1679. memcpy(dest_cr + i*uvlinesize, h->mb + 160 + i*4, 8);
  1680. }
  1681. }
  1682. }
  1683. }
  1684. } else {
  1685. if(IS_INTRA(mb_type)){
  1686. if(h->deblocking_filter)
  1687. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, 0, simple, pixel_shift);
  1688. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1689. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1690. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1691. }
  1692. hl_decode_mb_predict_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
  1693. if(h->deblocking_filter)
  1694. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, 0, simple, pixel_shift);
  1695. }else if(is_h264){
  1696. if (pixel_shift) {
  1697. hl_motion_16(h, dest_y, dest_cb, dest_cr,
  1698. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1699. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1700. h->h264dsp.weight_h264_pixels_tab,
  1701. h->h264dsp.biweight_h264_pixels_tab, 0);
  1702. } else
  1703. hl_motion_8(h, dest_y, dest_cb, dest_cr,
  1704. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1705. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1706. h->h264dsp.weight_h264_pixels_tab,
  1707. h->h264dsp.biweight_h264_pixels_tab, 0);
  1708. }
  1709. hl_decode_mb_idct_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
  1710. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1711. uint8_t *dest[2] = {dest_cb, dest_cr};
  1712. if(transform_bypass){
  1713. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1714. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + (16*16*1 << pixel_shift), uvlinesize);
  1715. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 32, h->mb + (16*16*2 << pixel_shift), uvlinesize);
  1716. }else{
  1717. idct_add = s->dsp.add_pixels4;
  1718. for(j=1; j<3; j++){
  1719. for(i=j*16; i<j*16+4; i++){
  1720. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
  1721. idct_add (dest[j-1] + block_offset[i], h->mb + (i*16 << pixel_shift), uvlinesize);
  1722. }
  1723. }
  1724. }
  1725. }else{
  1726. if(is_h264){
  1727. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
  1728. 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]);
  1729. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
  1730. 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]);
  1731. h->h264dsp.h264_idct_add8(dest, block_offset,
  1732. h->mb, uvlinesize,
  1733. h->non_zero_count_cache);
  1734. }
  1735. #if CONFIG_SVQ3_DECODER
  1736. else{
  1737. 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]);
  1738. 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]);
  1739. for(j=1; j<3; j++){
  1740. for(i=j*16; i<j*16+4; i++){
  1741. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1742. uint8_t * const ptr= dest[j-1] + block_offset[i];
  1743. ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
  1744. }
  1745. }
  1746. }
  1747. }
  1748. #endif
  1749. }
  1750. }
  1751. }
  1752. if(h->cbp || IS_INTRA(mb_type))
  1753. {
  1754. s->dsp.clear_blocks(h->mb);
  1755. s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
  1756. }
  1757. }
  1758. static av_always_inline void hl_decode_mb_444_internal(H264Context *h, int simple, int pixel_shift){
  1759. MpegEncContext * const s = &h->s;
  1760. const int mb_x= s->mb_x;
  1761. const int mb_y= s->mb_y;
  1762. const int mb_xy= h->mb_xy;
  1763. const int mb_type= s->current_picture.mb_type[mb_xy];
  1764. uint8_t *dest[3];
  1765. int linesize;
  1766. int i, j, p;
  1767. int *block_offset = &h->block_offset[0];
  1768. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  1769. const int plane_count = (simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) ? 3 : 1;
  1770. for (p = 0; p < plane_count; p++)
  1771. {
  1772. dest[p] = s->current_picture.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
  1773. s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
  1774. }
  1775. h->list_counts[mb_xy]= h->list_count;
  1776. if (!simple && MB_FIELD) {
  1777. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
  1778. block_offset = &h->block_offset[48];
  1779. if(mb_y&1) //FIXME move out of this function?
  1780. for (p = 0; p < 3; p++)
  1781. dest[p] -= s->linesize*15;
  1782. if(FRAME_MBAFF) {
  1783. int list;
  1784. for(list=0; list<h->list_count; list++){
  1785. if(!USES_LIST(mb_type, list))
  1786. continue;
  1787. if(IS_16X16(mb_type)){
  1788. int8_t *ref = &h->ref_cache[list][scan8[0]];
  1789. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  1790. }else{
  1791. for(i=0; i<16; i+=4){
  1792. int ref = h->ref_cache[list][scan8[i]];
  1793. if(ref >= 0)
  1794. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  1795. }
  1796. }
  1797. }
  1798. }
  1799. } else {
  1800. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;
  1801. }
  1802. if (!simple && IS_INTRA_PCM(mb_type)) {
  1803. if (pixel_shift) {
  1804. const int bit_depth = h->sps.bit_depth_luma;
  1805. GetBitContext gb;
  1806. init_get_bits(&gb, (uint8_t*)h->mb, 768*bit_depth);
  1807. for (p = 0; p < plane_count; p++) {
  1808. for (i = 0; i < 16; i++) {
  1809. uint16_t *tmp = (uint16_t*)(dest[p] + i*linesize);
  1810. for (j = 0; j < 16; j++)
  1811. tmp[j] = get_bits(&gb, bit_depth);
  1812. }
  1813. }
  1814. } else {
  1815. for (p = 0; p < plane_count; p++) {
  1816. for (i = 0; i < 16; i++) {
  1817. memcpy(dest[p] + i*linesize, h->mb + p*128 + i*8, 16);
  1818. }
  1819. }
  1820. }
  1821. } else {
  1822. if(IS_INTRA(mb_type)){
  1823. if(h->deblocking_filter)
  1824. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 1, 1, simple, pixel_shift);
  1825. for (p = 0; p < plane_count; p++)
  1826. hl_decode_mb_predict_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
  1827. if(h->deblocking_filter)
  1828. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);
  1829. }else{
  1830. if (pixel_shift) {
  1831. hl_motion_16(h, dest[0], dest[1], dest[2],
  1832. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1833. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1834. h->h264dsp.weight_h264_pixels_tab,
  1835. h->h264dsp.biweight_h264_pixels_tab, 1);
  1836. } else
  1837. hl_motion_8(h, dest[0], dest[1], dest[2],
  1838. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1839. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1840. h->h264dsp.weight_h264_pixels_tab,
  1841. h->h264dsp.biweight_h264_pixels_tab, 1);
  1842. }
  1843. for (p = 0; p < plane_count; p++)
  1844. hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
  1845. }
  1846. if(h->cbp || IS_INTRA(mb_type))
  1847. {
  1848. s->dsp.clear_blocks(h->mb);
  1849. s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
  1850. }
  1851. }
  1852. /**
  1853. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1854. */
  1855. #define hl_decode_mb_simple(sh, bits) \
  1856. static void hl_decode_mb_simple_ ## bits(H264Context *h){ \
  1857. hl_decode_mb_internal(h, 1, sh); \
  1858. }
  1859. hl_decode_mb_simple(0, 8);
  1860. hl_decode_mb_simple(1, 16);
  1861. /**
  1862. * Process a macroblock; this handles edge cases, such as interlacing.
  1863. */
  1864. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1865. hl_decode_mb_internal(h, 0, h->pixel_shift);
  1866. }
  1867. static void av_noinline hl_decode_mb_444_complex(H264Context *h){
  1868. hl_decode_mb_444_internal(h, 0, h->pixel_shift);
  1869. }
  1870. static void av_noinline hl_decode_mb_444_simple(H264Context *h){
  1871. hl_decode_mb_444_internal(h, 1, 0);
  1872. }
  1873. void ff_h264_hl_decode_mb(H264Context *h){
  1874. MpegEncContext * const s = &h->s;
  1875. const int mb_xy= h->mb_xy;
  1876. const int mb_type= s->current_picture.mb_type[mb_xy];
  1877. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1878. if (CHROMA444) {
  1879. if(is_complex || h->pixel_shift)
  1880. hl_decode_mb_444_complex(h);
  1881. else
  1882. hl_decode_mb_444_simple(h);
  1883. } else if (is_complex) {
  1884. hl_decode_mb_complex(h);
  1885. } else if (h->pixel_shift) {
  1886. hl_decode_mb_simple_16(h);
  1887. } else
  1888. hl_decode_mb_simple_8(h);
  1889. }
  1890. static int pred_weight_table(H264Context *h){
  1891. MpegEncContext * const s = &h->s;
  1892. int list, i;
  1893. int luma_def, chroma_def;
  1894. h->use_weight= 0;
  1895. h->use_weight_chroma= 0;
  1896. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1897. if(h->sps.chroma_format_idc)
  1898. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1899. luma_def = 1<<h->luma_log2_weight_denom;
  1900. chroma_def = 1<<h->chroma_log2_weight_denom;
  1901. for(list=0; list<2; list++){
  1902. h->luma_weight_flag[list] = 0;
  1903. h->chroma_weight_flag[list] = 0;
  1904. for(i=0; i<h->ref_count[list]; i++){
  1905. int luma_weight_flag, chroma_weight_flag;
  1906. luma_weight_flag= get_bits1(&s->gb);
  1907. if(luma_weight_flag){
  1908. h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
  1909. h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
  1910. if( h->luma_weight[i][list][0] != luma_def
  1911. || h->luma_weight[i][list][1] != 0) {
  1912. h->use_weight= 1;
  1913. h->luma_weight_flag[list]= 1;
  1914. }
  1915. }else{
  1916. h->luma_weight[i][list][0]= luma_def;
  1917. h->luma_weight[i][list][1]= 0;
  1918. }
  1919. if(h->sps.chroma_format_idc){
  1920. chroma_weight_flag= get_bits1(&s->gb);
  1921. if(chroma_weight_flag){
  1922. int j;
  1923. for(j=0; j<2; j++){
  1924. h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
  1925. h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
  1926. if( h->chroma_weight[i][list][j][0] != chroma_def
  1927. || h->chroma_weight[i][list][j][1] != 0) {
  1928. h->use_weight_chroma= 1;
  1929. h->chroma_weight_flag[list]= 1;
  1930. }
  1931. }
  1932. }else{
  1933. int j;
  1934. for(j=0; j<2; j++){
  1935. h->chroma_weight[i][list][j][0]= chroma_def;
  1936. h->chroma_weight[i][list][j][1]= 0;
  1937. }
  1938. }
  1939. }
  1940. }
  1941. if(h->slice_type_nos != AV_PICTURE_TYPE_B) break;
  1942. }
  1943. h->use_weight= h->use_weight || h->use_weight_chroma;
  1944. return 0;
  1945. }
  1946. /**
  1947. * Initialize implicit_weight table.
  1948. * @param field 0/1 initialize the weight for interlaced MBAFF
  1949. * -1 initializes the rest
  1950. */
  1951. static void implicit_weight_table(H264Context *h, int field){
  1952. MpegEncContext * const s = &h->s;
  1953. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  1954. for (i = 0; i < 2; i++) {
  1955. h->luma_weight_flag[i] = 0;
  1956. h->chroma_weight_flag[i] = 0;
  1957. }
  1958. if(field < 0){
  1959. cur_poc = s->current_picture_ptr->poc;
  1960. if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
  1961. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1962. h->use_weight= 0;
  1963. h->use_weight_chroma= 0;
  1964. return;
  1965. }
  1966. ref_start= 0;
  1967. ref_count0= h->ref_count[0];
  1968. ref_count1= h->ref_count[1];
  1969. }else{
  1970. cur_poc = s->current_picture_ptr->field_poc[field];
  1971. ref_start= 16;
  1972. ref_count0= 16+2*h->ref_count[0];
  1973. ref_count1= 16+2*h->ref_count[1];
  1974. }
  1975. h->use_weight= 2;
  1976. h->use_weight_chroma= 2;
  1977. h->luma_log2_weight_denom= 5;
  1978. h->chroma_log2_weight_denom= 5;
  1979. for(ref0=ref_start; ref0 < ref_count0; ref0++){
  1980. int poc0 = h->ref_list[0][ref0].poc;
  1981. for(ref1=ref_start; ref1 < ref_count1; ref1++){
  1982. int w = 32;
  1983. if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
  1984. int poc1 = h->ref_list[1][ref1].poc;
  1985. int td = av_clip(poc1 - poc0, -128, 127);
  1986. if(td){
  1987. int tb = av_clip(cur_poc - poc0, -128, 127);
  1988. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1989. int dist_scale_factor = (tb*tx + 32) >> 8;
  1990. if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
  1991. w = 64 - dist_scale_factor;
  1992. }
  1993. }
  1994. if(field<0){
  1995. h->implicit_weight[ref0][ref1][0]=
  1996. h->implicit_weight[ref0][ref1][1]= w;
  1997. }else{
  1998. h->implicit_weight[ref0][ref1][field]=w;
  1999. }
  2000. }
  2001. }
  2002. }
  2003. /**
  2004. * instantaneous decoder refresh.
  2005. */
  2006. static void idr(H264Context *h){
  2007. ff_h264_remove_all_refs(h);
  2008. h->prev_frame_num= 0;
  2009. h->prev_frame_num_offset= 0;
  2010. h->prev_poc_msb=
  2011. h->prev_poc_lsb= 0;
  2012. }
  2013. /* forget old pics after a seek */
  2014. static void flush_dpb(AVCodecContext *avctx){
  2015. H264Context *h= avctx->priv_data;
  2016. int i;
  2017. for(i=0; i<=MAX_DELAYED_PIC_COUNT; i++) {
  2018. if(h->delayed_pic[i])
  2019. h->delayed_pic[i]->reference= 0;
  2020. h->delayed_pic[i]= NULL;
  2021. }
  2022. h->outputed_poc=h->next_outputed_poc= INT_MIN;
  2023. h->prev_interlaced_frame = 1;
  2024. idr(h);
  2025. if(h->s.current_picture_ptr)
  2026. h->s.current_picture_ptr->reference= 0;
  2027. h->s.first_field= 0;
  2028. ff_h264_reset_sei(h);
  2029. ff_mpeg_flush(avctx);
  2030. }
  2031. static int init_poc(H264Context *h){
  2032. MpegEncContext * const s = &h->s;
  2033. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2034. int field_poc[2];
  2035. Picture *cur = s->current_picture_ptr;
  2036. h->frame_num_offset= h->prev_frame_num_offset;
  2037. if(h->frame_num < h->prev_frame_num)
  2038. h->frame_num_offset += max_frame_num;
  2039. if(h->sps.poc_type==0){
  2040. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2041. 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 if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2044. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2045. else
  2046. h->poc_msb = h->prev_poc_msb;
  2047. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2048. field_poc[0] =
  2049. field_poc[1] = h->poc_msb + h->poc_lsb;
  2050. if(s->picture_structure == PICT_FRAME)
  2051. field_poc[1] += h->delta_poc_bottom;
  2052. }else if(h->sps.poc_type==1){
  2053. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2054. int i;
  2055. if(h->sps.poc_cycle_length != 0)
  2056. abs_frame_num = h->frame_num_offset + h->frame_num;
  2057. else
  2058. abs_frame_num = 0;
  2059. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2060. abs_frame_num--;
  2061. expected_delta_per_poc_cycle = 0;
  2062. for(i=0; i < h->sps.poc_cycle_length; i++)
  2063. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2064. if(abs_frame_num > 0){
  2065. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2066. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2067. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2068. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2069. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2070. } else
  2071. expectedpoc = 0;
  2072. if(h->nal_ref_idc == 0)
  2073. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2074. field_poc[0] = expectedpoc + h->delta_poc[0];
  2075. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2076. if(s->picture_structure == PICT_FRAME)
  2077. field_poc[1] += h->delta_poc[1];
  2078. }else{
  2079. int poc= 2*(h->frame_num_offset + h->frame_num);
  2080. if(!h->nal_ref_idc)
  2081. poc--;
  2082. field_poc[0]= poc;
  2083. field_poc[1]= poc;
  2084. }
  2085. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2086. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2087. if(s->picture_structure != PICT_TOP_FIELD)
  2088. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2089. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  2090. return 0;
  2091. }
  2092. /**
  2093. * initialize scan tables
  2094. */
  2095. static void init_scan_tables(H264Context *h){
  2096. int i;
  2097. for(i=0; i<16; i++){
  2098. #define T(x) (x>>2) | ((x<<2) & 0xF)
  2099. h->zigzag_scan[i] = T(zigzag_scan[i]);
  2100. h-> field_scan[i] = T( field_scan[i]);
  2101. #undef T
  2102. }
  2103. for(i=0; i<64; i++){
  2104. #define T(x) (x>>3) | ((x&7)<<3)
  2105. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  2106. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  2107. h->field_scan8x8[i] = T(field_scan8x8[i]);
  2108. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  2109. #undef T
  2110. }
  2111. if(h->sps.transform_bypass){ //FIXME same ugly
  2112. h->zigzag_scan_q0 = zigzag_scan;
  2113. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  2114. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  2115. h->field_scan_q0 = field_scan;
  2116. h->field_scan8x8_q0 = field_scan8x8;
  2117. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  2118. }else{
  2119. h->zigzag_scan_q0 = h->zigzag_scan;
  2120. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  2121. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  2122. h->field_scan_q0 = h->field_scan;
  2123. h->field_scan8x8_q0 = h->field_scan8x8;
  2124. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  2125. }
  2126. }
  2127. static void field_end(H264Context *h, int in_setup){
  2128. MpegEncContext * const s = &h->s;
  2129. AVCodecContext * const avctx= s->avctx;
  2130. s->mb_y= 0;
  2131. if (!in_setup && !s->dropable)
  2132. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
  2133. s->picture_structure==PICT_BOTTOM_FIELD);
  2134. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2135. ff_vdpau_h264_set_reference_frames(s);
  2136. if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
  2137. if(!s->dropable) {
  2138. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2139. h->prev_poc_msb= h->poc_msb;
  2140. h->prev_poc_lsb= h->poc_lsb;
  2141. }
  2142. h->prev_frame_num_offset= h->frame_num_offset;
  2143. h->prev_frame_num= h->frame_num;
  2144. h->outputed_poc = h->next_outputed_poc;
  2145. }
  2146. if (avctx->hwaccel) {
  2147. if (avctx->hwaccel->end_frame(avctx) < 0)
  2148. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  2149. }
  2150. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2151. ff_vdpau_h264_picture_complete(s);
  2152. /*
  2153. * FIXME: Error handling code does not seem to support interlaced
  2154. * when slices span multiple rows
  2155. * The ff_er_add_slice calls don't work right for bottom
  2156. * fields; they cause massive erroneous error concealing
  2157. * Error marking covers both fields (top and bottom).
  2158. * This causes a mismatched s->error_count
  2159. * and a bad error table. Further, the error count goes to
  2160. * INT_MAX when called for bottom field, because mb_y is
  2161. * past end by one (callers fault) and resync_mb_y != 0
  2162. * causes problems for the first MB line, too.
  2163. */
  2164. if (!FIELD_PICTURE)
  2165. ff_er_frame_end(s);
  2166. MPV_frame_end(s);
  2167. h->current_slice=0;
  2168. }
  2169. /**
  2170. * Replicate H264 "master" context to thread contexts.
  2171. */
  2172. static void clone_slice(H264Context *dst, H264Context *src)
  2173. {
  2174. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2175. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  2176. dst->s.current_picture = src->s.current_picture;
  2177. dst->s.linesize = src->s.linesize;
  2178. dst->s.uvlinesize = src->s.uvlinesize;
  2179. dst->s.first_field = src->s.first_field;
  2180. dst->prev_poc_msb = src->prev_poc_msb;
  2181. dst->prev_poc_lsb = src->prev_poc_lsb;
  2182. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2183. dst->prev_frame_num = src->prev_frame_num;
  2184. dst->short_ref_count = src->short_ref_count;
  2185. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2186. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2187. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2188. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  2189. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2190. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2191. }
  2192. /**
  2193. * computes profile from profile_idc and constraint_set?_flags
  2194. *
  2195. * @param sps SPS
  2196. *
  2197. * @return profile as defined by FF_PROFILE_H264_*
  2198. */
  2199. int ff_h264_get_profile(SPS *sps)
  2200. {
  2201. int profile = sps->profile_idc;
  2202. switch(sps->profile_idc) {
  2203. case FF_PROFILE_H264_BASELINE:
  2204. // constraint_set1_flag set to 1
  2205. profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  2206. break;
  2207. case FF_PROFILE_H264_HIGH_10:
  2208. case FF_PROFILE_H264_HIGH_422:
  2209. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  2210. // constraint_set3_flag set to 1
  2211. profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
  2212. break;
  2213. }
  2214. return profile;
  2215. }
  2216. /**
  2217. * decodes a slice header.
  2218. * This will also call MPV_common_init() and frame_start() as needed.
  2219. *
  2220. * @param h h264context
  2221. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  2222. *
  2223. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  2224. */
  2225. static int decode_slice_header(H264Context *h, H264Context *h0){
  2226. MpegEncContext * const s = &h->s;
  2227. MpegEncContext * const s0 = &h0->s;
  2228. unsigned int first_mb_in_slice;
  2229. unsigned int pps_id;
  2230. int num_ref_idx_active_override_flag;
  2231. unsigned int slice_type, tmp, i, j;
  2232. int default_ref_list_done = 0;
  2233. int last_pic_structure;
  2234. s->dropable= h->nal_ref_idc == 0;
  2235. /* FIXME: 2tap qpel isn't implemented for high bit depth. */
  2236. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc && !h->pixel_shift){
  2237. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  2238. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  2239. }else{
  2240. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  2241. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  2242. }
  2243. first_mb_in_slice= get_ue_golomb(&s->gb);
  2244. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  2245. if(h0->current_slice && FIELD_PICTURE){
  2246. field_end(h, 1);
  2247. }
  2248. h0->current_slice = 0;
  2249. if (!s0->first_field)
  2250. s->current_picture_ptr= NULL;
  2251. }
  2252. slice_type= get_ue_golomb_31(&s->gb);
  2253. if(slice_type > 9){
  2254. 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);
  2255. return -1;
  2256. }
  2257. if(slice_type > 4){
  2258. slice_type -= 5;
  2259. h->slice_type_fixed=1;
  2260. }else
  2261. h->slice_type_fixed=0;
  2262. slice_type= golomb_to_pict_type[ slice_type ];
  2263. if (slice_type == AV_PICTURE_TYPE_I
  2264. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  2265. default_ref_list_done = 1;
  2266. }
  2267. h->slice_type= slice_type;
  2268. h->slice_type_nos= slice_type & 3;
  2269. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  2270. pps_id= get_ue_golomb(&s->gb);
  2271. if(pps_id>=MAX_PPS_COUNT){
  2272. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  2273. return -1;
  2274. }
  2275. if(!h0->pps_buffers[pps_id]) {
  2276. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  2277. return -1;
  2278. }
  2279. h->pps= *h0->pps_buffers[pps_id];
  2280. if(!h0->sps_buffers[h->pps.sps_id]) {
  2281. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  2282. return -1;
  2283. }
  2284. h->sps = *h0->sps_buffers[h->pps.sps_id];
  2285. s->avctx->profile = ff_h264_get_profile(&h->sps);
  2286. s->avctx->level = h->sps.level_idc;
  2287. s->avctx->refs = h->sps.ref_frame_count;
  2288. if(h == h0 && h->dequant_coeff_pps != pps_id){
  2289. h->dequant_coeff_pps = pps_id;
  2290. init_dequant_tables(h);
  2291. }
  2292. s->mb_width= h->sps.mb_width;
  2293. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  2294. h->b_stride= s->mb_width*4;
  2295. s->width = 16*s->mb_width - (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
  2296. if(h->sps.frame_mbs_only_flag)
  2297. s->height= 16*s->mb_height - (2>>CHROMA444)*FFMIN(h->sps.crop_bottom, (8<<CHROMA444)-1);
  2298. else
  2299. s->height= 16*s->mb_height - (4>>CHROMA444)*FFMIN(h->sps.crop_bottom, (8<<CHROMA444)-1);
  2300. if (s->context_initialized
  2301. && ( s->width != s->avctx->width || s->height != s->avctx->height
  2302. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
  2303. if(h != h0) {
  2304. av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
  2305. return -1; // width / height changed during parallelized decoding
  2306. }
  2307. free_tables(h, 0);
  2308. flush_dpb(s->avctx);
  2309. MPV_common_end(s);
  2310. }
  2311. if (!s->context_initialized) {
  2312. if (h != h0) {
  2313. av_log(h->s.avctx, AV_LOG_ERROR, "Cannot (re-)initialize context during parallel decoding.\n");
  2314. return -1;
  2315. }
  2316. avcodec_set_dimensions(s->avctx, s->width, s->height);
  2317. s->avctx->sample_aspect_ratio= h->sps.sar;
  2318. av_assert0(s->avctx->sample_aspect_ratio.den);
  2319. h->s.avctx->coded_width = 16*s->mb_width;
  2320. h->s.avctx->coded_height = 16*s->mb_height;
  2321. if(h->sps.video_signal_type_present_flag){
  2322. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  2323. if(h->sps.colour_description_present_flag){
  2324. s->avctx->color_primaries = h->sps.color_primaries;
  2325. s->avctx->color_trc = h->sps.color_trc;
  2326. s->avctx->colorspace = h->sps.colorspace;
  2327. }
  2328. }
  2329. if(h->sps.timing_info_present_flag){
  2330. int64_t den= h->sps.time_scale;
  2331. if(h->x264_build < 44U)
  2332. den *= 2;
  2333. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  2334. h->sps.num_units_in_tick, den, 1<<30);
  2335. }
  2336. switch (h->sps.bit_depth_luma) {
  2337. case 9 :
  2338. s->avctx->pix_fmt = CHROMA444 ? PIX_FMT_YUV444P9 : PIX_FMT_YUV420P9;
  2339. break;
  2340. case 10 :
  2341. s->avctx->pix_fmt = CHROMA444 ? PIX_FMT_YUV444P10 : PIX_FMT_YUV420P10;
  2342. break;
  2343. default:
  2344. if (CHROMA444){
  2345. s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P : PIX_FMT_YUV444P;
  2346. }else{
  2347. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  2348. s->avctx->codec->pix_fmts ?
  2349. s->avctx->codec->pix_fmts :
  2350. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  2351. hwaccel_pixfmt_list_h264_jpeg_420 :
  2352. ff_hwaccel_pixfmt_list_420);
  2353. }
  2354. }
  2355. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  2356. if (MPV_common_init(s) < 0) {
  2357. av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed.\n");
  2358. return -1;
  2359. }
  2360. s->first_field = 0;
  2361. h->prev_interlaced_frame = 1;
  2362. init_scan_tables(h);
  2363. if (ff_h264_alloc_tables(h) < 0) {
  2364. av_log(h->s.avctx, AV_LOG_ERROR, "Could not allocate memory for h264\n");
  2365. return AVERROR(ENOMEM);
  2366. }
  2367. if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  2368. if (context_init(h) < 0) {
  2369. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2370. return -1;
  2371. }
  2372. } else {
  2373. for(i = 1; i < s->avctx->thread_count; i++) {
  2374. H264Context *c;
  2375. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  2376. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  2377. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  2378. c->h264dsp = h->h264dsp;
  2379. c->sps = h->sps;
  2380. c->pps = h->pps;
  2381. c->pixel_shift = h->pixel_shift;
  2382. init_scan_tables(c);
  2383. clone_tables(c, h, i);
  2384. }
  2385. for(i = 0; i < s->avctx->thread_count; i++)
  2386. if (context_init(h->thread_context[i]) < 0) {
  2387. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2388. return -1;
  2389. }
  2390. }
  2391. }
  2392. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2393. h->mb_mbaff = 0;
  2394. h->mb_aff_frame = 0;
  2395. last_pic_structure = s0->picture_structure;
  2396. if(h->sps.frame_mbs_only_flag){
  2397. s->picture_structure= PICT_FRAME;
  2398. }else{
  2399. if(get_bits1(&s->gb)) { //field_pic_flag
  2400. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2401. } else {
  2402. s->picture_structure= PICT_FRAME;
  2403. h->mb_aff_frame = h->sps.mb_aff;
  2404. }
  2405. }
  2406. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  2407. if(h0->current_slice == 0){
  2408. // Shorten frame num gaps so we don't have to allocate reference frames just to throw them away
  2409. if(h->frame_num != h->prev_frame_num) {
  2410. int unwrap_prev_frame_num = h->prev_frame_num, max_frame_num = 1<<h->sps.log2_max_frame_num;
  2411. if (unwrap_prev_frame_num > h->frame_num) unwrap_prev_frame_num -= max_frame_num;
  2412. if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
  2413. unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
  2414. if (unwrap_prev_frame_num < 0)
  2415. unwrap_prev_frame_num += max_frame_num;
  2416. h->prev_frame_num = unwrap_prev_frame_num;
  2417. }
  2418. }
  2419. while(h->frame_num != h->prev_frame_num &&
  2420. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  2421. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  2422. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  2423. if (ff_h264_frame_start(h) < 0)
  2424. return -1;
  2425. h->prev_frame_num++;
  2426. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  2427. s->current_picture_ptr->frame_num= h->prev_frame_num;
  2428. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
  2429. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
  2430. ff_generate_sliding_window_mmcos(h);
  2431. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2432. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  2433. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  2434. * about there being no actual duplicates.
  2435. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  2436. * concealing a lost frame, this probably isn't noticable by comparison, but it should
  2437. * be fixed. */
  2438. if (h->short_ref_count) {
  2439. if (prev) {
  2440. av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
  2441. (const uint8_t**)prev->data, prev->linesize,
  2442. s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
  2443. h->short_ref[0]->poc = prev->poc+2;
  2444. }
  2445. h->short_ref[0]->frame_num = h->prev_frame_num;
  2446. }
  2447. }
  2448. /* See if we have a decoded first field looking for a pair... */
  2449. if (s0->first_field) {
  2450. assert(s0->current_picture_ptr);
  2451. assert(s0->current_picture_ptr->data[0]);
  2452. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  2453. /* figure out if we have a complementary field pair */
  2454. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2455. /*
  2456. * Previous field is unmatched. Don't display it, but let it
  2457. * remain for reference if marked as such.
  2458. */
  2459. s0->current_picture_ptr = NULL;
  2460. s0->first_field = FIELD_PICTURE;
  2461. } else {
  2462. if (h->nal_ref_idc &&
  2463. s0->current_picture_ptr->reference &&
  2464. s0->current_picture_ptr->frame_num != h->frame_num) {
  2465. /*
  2466. * This and previous field were reference, but had
  2467. * different frame_nums. Consider this field first in
  2468. * pair. Throw away previous field except for reference
  2469. * purposes.
  2470. */
  2471. s0->first_field = 1;
  2472. s0->current_picture_ptr = NULL;
  2473. } else {
  2474. /* Second field in complementary pair */
  2475. s0->first_field = 0;
  2476. }
  2477. }
  2478. } else {
  2479. /* Frame or first field in a potentially complementary pair */
  2480. assert(!s0->current_picture_ptr);
  2481. s0->first_field = FIELD_PICTURE;
  2482. }
  2483. if(!FIELD_PICTURE || s0->first_field) {
  2484. if (ff_h264_frame_start(h) < 0) {
  2485. s0->first_field = 0;
  2486. return -1;
  2487. }
  2488. } else {
  2489. ff_release_unused_pictures(s, 0);
  2490. }
  2491. }
  2492. if(h != h0)
  2493. clone_slice(h, h0);
  2494. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  2495. assert(s->mb_num == s->mb_width * s->mb_height);
  2496. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  2497. first_mb_in_slice >= s->mb_num){
  2498. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  2499. return -1;
  2500. }
  2501. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  2502. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  2503. if (s->picture_structure == PICT_BOTTOM_FIELD)
  2504. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  2505. assert(s->mb_y < s->mb_height);
  2506. if(s->picture_structure==PICT_FRAME){
  2507. h->curr_pic_num= h->frame_num;
  2508. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2509. }else{
  2510. h->curr_pic_num= 2*h->frame_num + 1;
  2511. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2512. }
  2513. if(h->nal_unit_type == NAL_IDR_SLICE){
  2514. get_ue_golomb(&s->gb); /* idr_pic_id */
  2515. }
  2516. if(h->sps.poc_type==0){
  2517. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2518. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2519. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2520. }
  2521. }
  2522. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2523. h->delta_poc[0]= get_se_golomb(&s->gb);
  2524. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2525. h->delta_poc[1]= get_se_golomb(&s->gb);
  2526. }
  2527. init_poc(h);
  2528. if(h->pps.redundant_pic_cnt_present){
  2529. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2530. }
  2531. //set defaults, might be overridden a few lines later
  2532. h->ref_count[0]= h->pps.ref_count[0];
  2533. h->ref_count[1]= h->pps.ref_count[1];
  2534. if(h->slice_type_nos != AV_PICTURE_TYPE_I){
  2535. unsigned max= (16<<(s->picture_structure != PICT_FRAME))-1;
  2536. if(h->slice_type_nos == AV_PICTURE_TYPE_B){
  2537. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2538. }
  2539. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2540. if(num_ref_idx_active_override_flag){
  2541. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2542. if(h->slice_type_nos==AV_PICTURE_TYPE_B)
  2543. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2544. }
  2545. if(h->ref_count[0]-1 > max || h->ref_count[1]-1 > max){
  2546. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  2547. h->ref_count[0]= h->ref_count[1]= 1;
  2548. return -1;
  2549. }
  2550. if(h->slice_type_nos == AV_PICTURE_TYPE_B)
  2551. h->list_count= 2;
  2552. else
  2553. h->list_count= 1;
  2554. }else
  2555. h->list_count= 0;
  2556. if(!default_ref_list_done){
  2557. ff_h264_fill_default_ref_list(h);
  2558. }
  2559. if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  2560. return -1;
  2561. if(h->slice_type_nos!=AV_PICTURE_TYPE_I){
  2562. s->last_picture_ptr= &h->ref_list[0][0];
  2563. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  2564. }
  2565. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  2566. s->next_picture_ptr= &h->ref_list[1][0];
  2567. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  2568. }
  2569. if( (h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P )
  2570. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== AV_PICTURE_TYPE_B ) )
  2571. pred_weight_table(h);
  2572. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  2573. implicit_weight_table(h, -1);
  2574. }else {
  2575. h->use_weight = 0;
  2576. for (i = 0; i < 2; i++) {
  2577. h->luma_weight_flag[i] = 0;
  2578. h->chroma_weight_flag[i] = 0;
  2579. }
  2580. }
  2581. if(h->nal_ref_idc)
  2582. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  2583. if(FRAME_MBAFF){
  2584. ff_h264_fill_mbaff_ref_list(h);
  2585. if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  2586. implicit_weight_table(h, 0);
  2587. implicit_weight_table(h, 1);
  2588. }
  2589. }
  2590. if(h->slice_type_nos==AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  2591. ff_h264_direct_dist_scale_factor(h);
  2592. ff_h264_direct_ref_list_init(h);
  2593. if( h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac ){
  2594. tmp = get_ue_golomb_31(&s->gb);
  2595. if(tmp > 2){
  2596. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  2597. return -1;
  2598. }
  2599. h->cabac_init_idc= tmp;
  2600. }
  2601. h->last_qscale_diff = 0;
  2602. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  2603. if(tmp>51+6*(h->sps.bit_depth_luma-8)){
  2604. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  2605. return -1;
  2606. }
  2607. s->qscale= tmp;
  2608. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2609. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2610. //FIXME qscale / qp ... stuff
  2611. if(h->slice_type == AV_PICTURE_TYPE_SP){
  2612. get_bits1(&s->gb); /* sp_for_switch_flag */
  2613. }
  2614. if(h->slice_type==AV_PICTURE_TYPE_SP || h->slice_type == AV_PICTURE_TYPE_SI){
  2615. get_se_golomb(&s->gb); /* slice_qs_delta */
  2616. }
  2617. h->deblocking_filter = 1;
  2618. h->slice_alpha_c0_offset = 52;
  2619. h->slice_beta_offset = 52;
  2620. if( h->pps.deblocking_filter_parameters_present ) {
  2621. tmp= get_ue_golomb_31(&s->gb);
  2622. if(tmp > 2){
  2623. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  2624. return -1;
  2625. }
  2626. h->deblocking_filter= tmp;
  2627. if(h->deblocking_filter < 2)
  2628. h->deblocking_filter^= 1; // 1<->0
  2629. if( h->deblocking_filter ) {
  2630. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  2631. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  2632. if( h->slice_alpha_c0_offset > 104U
  2633. || h->slice_beta_offset > 104U){
  2634. 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);
  2635. return -1;
  2636. }
  2637. }
  2638. }
  2639. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  2640. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != AV_PICTURE_TYPE_I)
  2641. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == AV_PICTURE_TYPE_B)
  2642. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  2643. h->deblocking_filter= 0;
  2644. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  2645. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  2646. /* Cheat slightly for speed:
  2647. Do not bother to deblock across slices. */
  2648. h->deblocking_filter = 2;
  2649. } else {
  2650. h0->max_contexts = 1;
  2651. if(!h0->single_decode_warning) {
  2652. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  2653. h0->single_decode_warning = 1;
  2654. }
  2655. if (h != h0) {
  2656. av_log(h->s.avctx, AV_LOG_ERROR, "Deblocking switched inside frame.\n");
  2657. return 1;
  2658. }
  2659. }
  2660. }
  2661. 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]);
  2662. #if 0 //FMO
  2663. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  2664. slice_group_change_cycle= get_bits(&s->gb, ?);
  2665. #endif
  2666. h0->last_slice_type = slice_type;
  2667. h->slice_num = ++h0->current_slice;
  2668. if(h->slice_num >= MAX_SLICES){
  2669. av_log(s->avctx, AV_LOG_ERROR, "Too many slices (%d >= %d), increase MAX_SLICES and recompile\n", h->slice_num, MAX_SLICES);
  2670. }
  2671. for(j=0; j<2; j++){
  2672. int id_list[16];
  2673. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  2674. for(i=0; i<16; i++){
  2675. id_list[i]= 60;
  2676. if(h->ref_list[j][i].data[0]){
  2677. int k;
  2678. uint8_t *base= h->ref_list[j][i].base[0];
  2679. for(k=0; k<h->short_ref_count; k++)
  2680. if(h->short_ref[k]->base[0] == base){
  2681. id_list[i]= k;
  2682. break;
  2683. }
  2684. for(k=0; k<h->long_ref_count; k++)
  2685. if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
  2686. id_list[i]= h->short_ref_count + k;
  2687. break;
  2688. }
  2689. }
  2690. }
  2691. ref2frm[0]=
  2692. ref2frm[1]= -1;
  2693. for(i=0; i<16; i++)
  2694. ref2frm[i+2]= 4*id_list[i]
  2695. +(h->ref_list[j][i].reference&3);
  2696. ref2frm[18+0]=
  2697. ref2frm[18+1]= -1;
  2698. for(i=16; i<48; i++)
  2699. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  2700. +(h->ref_list[j][i].reference&3);
  2701. }
  2702. //FIXME: fix draw_edges+PAFF+frame threads
  2703. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type)) ? 0 : 16;
  2704. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2705. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2706. 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",
  2707. h->slice_num,
  2708. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  2709. first_mb_in_slice,
  2710. av_get_picture_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2711. pps_id, h->frame_num,
  2712. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2713. h->ref_count[0], h->ref_count[1],
  2714. s->qscale,
  2715. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  2716. h->use_weight,
  2717. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  2718. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  2719. );
  2720. }
  2721. return 0;
  2722. }
  2723. int ff_h264_get_slice_type(const H264Context *h)
  2724. {
  2725. switch (h->slice_type) {
  2726. case AV_PICTURE_TYPE_P: return 0;
  2727. case AV_PICTURE_TYPE_B: return 1;
  2728. case AV_PICTURE_TYPE_I: return 2;
  2729. case AV_PICTURE_TYPE_SP: return 3;
  2730. case AV_PICTURE_TYPE_SI: return 4;
  2731. default: return -1;
  2732. }
  2733. }
  2734. /**
  2735. *
  2736. * @return non zero if the loop filter can be skiped
  2737. */
  2738. static int fill_filter_caches(H264Context *h, int mb_type){
  2739. MpegEncContext * const s = &h->s;
  2740. const int mb_xy= h->mb_xy;
  2741. int top_xy, left_xy[2];
  2742. int top_type, left_type[2];
  2743. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  2744. //FIXME deblocking could skip the intra and nnz parts.
  2745. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  2746. * stuff, I can't imagine that these complex rules are worth it. */
  2747. left_xy[1] = left_xy[0] = mb_xy-1;
  2748. if(FRAME_MBAFF){
  2749. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
  2750. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  2751. if(s->mb_y&1){
  2752. if (left_mb_field_flag != curr_mb_field_flag) {
  2753. left_xy[0] -= s->mb_stride;
  2754. }
  2755. }else{
  2756. if(curr_mb_field_flag){
  2757. top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
  2758. }
  2759. if (left_mb_field_flag != curr_mb_field_flag) {
  2760. left_xy[1] += s->mb_stride;
  2761. }
  2762. }
  2763. }
  2764. h->top_mb_xy = top_xy;
  2765. h->left_mb_xy[0] = left_xy[0];
  2766. h->left_mb_xy[1] = left_xy[1];
  2767. {
  2768. //for sufficiently low qp, filtering wouldn't do anything
  2769. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  2770. int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
  2771. int qp = s->current_picture.qscale_table[mb_xy];
  2772. if(qp <= qp_thresh
  2773. && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
  2774. && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
  2775. if(!FRAME_MBAFF)
  2776. return 1;
  2777. if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
  2778. && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
  2779. return 1;
  2780. }
  2781. }
  2782. top_type = s->current_picture.mb_type[top_xy] ;
  2783. left_type[0] = s->current_picture.mb_type[left_xy[0]];
  2784. left_type[1] = s->current_picture.mb_type[left_xy[1]];
  2785. if(h->deblocking_filter == 2){
  2786. if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
  2787. if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
  2788. }else{
  2789. if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
  2790. if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
  2791. }
  2792. h->top_type = top_type ;
  2793. h->left_type[0]= left_type[0];
  2794. h->left_type[1]= left_type[1];
  2795. if(IS_INTRA(mb_type))
  2796. return 0;
  2797. AV_COPY32(&h->non_zero_count_cache[4+8* 1], &h->non_zero_count[mb_xy][ 0]);
  2798. AV_COPY32(&h->non_zero_count_cache[4+8* 2], &h->non_zero_count[mb_xy][ 4]);
  2799. AV_COPY32(&h->non_zero_count_cache[4+8* 3], &h->non_zero_count[mb_xy][ 8]);
  2800. AV_COPY32(&h->non_zero_count_cache[4+8* 4], &h->non_zero_count[mb_xy][12]);
  2801. h->cbp= h->cbp_table[mb_xy];
  2802. {
  2803. int list;
  2804. for(list=0; list<h->list_count; list++){
  2805. int8_t *ref;
  2806. int y, b_stride;
  2807. int16_t (*mv_dst)[2];
  2808. int16_t (*mv_src)[2];
  2809. if(!USES_LIST(mb_type, list)){
  2810. fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
  2811. AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2812. AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2813. AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2814. AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2815. continue;
  2816. }
  2817. ref = &s->current_picture.ref_index[list][4*mb_xy];
  2818. {
  2819. int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2820. AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2821. AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2822. ref += 2;
  2823. AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2824. AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2825. }
  2826. b_stride = h->b_stride;
  2827. mv_dst = &h->mv_cache[list][scan8[0]];
  2828. mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
  2829. for(y=0; y<4; y++){
  2830. AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
  2831. }
  2832. }
  2833. }
  2834. /*
  2835. 0 . T T. T T T T
  2836. 1 L . .L . . . .
  2837. 2 L . .L . . . .
  2838. 3 . T TL . . . .
  2839. 4 L . .L . . . .
  2840. 5 L . .. . . . .
  2841. */
  2842. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  2843. if(top_type){
  2844. AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][3*4]);
  2845. }
  2846. if(left_type[0]){
  2847. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][3+0*4];
  2848. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][3+1*4];
  2849. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][3+2*4];
  2850. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][3+3*4];
  2851. }
  2852. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  2853. if(!CABAC && h->pps.transform_8x8_mode){
  2854. if(IS_8x8DCT(top_type)){
  2855. h->non_zero_count_cache[4+8*0]=
  2856. h->non_zero_count_cache[5+8*0]= (h->cbp_table[top_xy] & 0x4000) >> 12;
  2857. h->non_zero_count_cache[6+8*0]=
  2858. h->non_zero_count_cache[7+8*0]= (h->cbp_table[top_xy] & 0x8000) >> 12;
  2859. }
  2860. if(IS_8x8DCT(left_type[0])){
  2861. h->non_zero_count_cache[3+8*1]=
  2862. h->non_zero_count_cache[3+8*2]= (h->cbp_table[left_xy[0]]&0x2000) >> 12; //FIXME check MBAFF
  2863. }
  2864. if(IS_8x8DCT(left_type[1])){
  2865. h->non_zero_count_cache[3+8*3]=
  2866. h->non_zero_count_cache[3+8*4]= (h->cbp_table[left_xy[1]]&0x8000) >> 12; //FIXME check MBAFF
  2867. }
  2868. if(IS_8x8DCT(mb_type)){
  2869. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  2870. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= (h->cbp & 0x1000) >> 12;
  2871. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  2872. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= (h->cbp & 0x2000) >> 12;
  2873. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  2874. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= (h->cbp & 0x4000) >> 12;
  2875. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  2876. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= (h->cbp & 0x8000) >> 12;
  2877. }
  2878. }
  2879. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  2880. int list;
  2881. for(list=0; list<h->list_count; list++){
  2882. if(USES_LIST(top_type, list)){
  2883. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  2884. const int b8_xy= 4*top_xy + 2;
  2885. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2886. AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
  2887. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  2888. h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
  2889. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  2890. h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
  2891. }else{
  2892. AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
  2893. AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2894. }
  2895. if(!IS_INTERLACED(mb_type^left_type[0])){
  2896. if(USES_LIST(left_type[0], list)){
  2897. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  2898. const int b8_xy= 4*left_xy[0] + 1;
  2899. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2900. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
  2901. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
  2902. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
  2903. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
  2904. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2905. h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
  2906. h->ref_cache[list][scan8[0] - 1 +16 ]=
  2907. h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
  2908. }else{
  2909. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
  2910. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
  2911. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
  2912. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
  2913. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2914. h->ref_cache[list][scan8[0] - 1 + 8 ]=
  2915. h->ref_cache[list][scan8[0] - 1 + 16 ]=
  2916. h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
  2917. }
  2918. }
  2919. }
  2920. }
  2921. return 0;
  2922. }
  2923. static void loop_filter(H264Context *h, int start_x, int end_x){
  2924. MpegEncContext * const s = &h->s;
  2925. uint8_t *dest_y, *dest_cb, *dest_cr;
  2926. int linesize, uvlinesize, mb_x, mb_y;
  2927. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  2928. const int old_slice_type= h->slice_type;
  2929. const int pixel_shift = h->pixel_shift;
  2930. if(h->deblocking_filter) {
  2931. for(mb_x= start_x; mb_x<end_x; mb_x++){
  2932. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  2933. int mb_xy, mb_type;
  2934. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  2935. h->slice_num= h->slice_table[mb_xy];
  2936. mb_type= s->current_picture.mb_type[mb_xy];
  2937. h->list_count= h->list_counts[mb_xy];
  2938. if(FRAME_MBAFF)
  2939. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2940. s->mb_x= mb_x;
  2941. s->mb_y= mb_y;
  2942. dest_y = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize ) * 16;
  2943. dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * (8 << CHROMA444);
  2944. dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * (8 << CHROMA444);
  2945. //FIXME simplify above
  2946. if (MB_FIELD) {
  2947. linesize = h->mb_linesize = s->linesize * 2;
  2948. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2949. if(mb_y&1){ //FIXME move out of this function?
  2950. dest_y -= s->linesize*15;
  2951. dest_cb-= s->uvlinesize*((8 << CHROMA444)-1);
  2952. dest_cr-= s->uvlinesize*((8 << CHROMA444)-1);
  2953. }
  2954. } else {
  2955. linesize = h->mb_linesize = s->linesize;
  2956. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2957. }
  2958. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, CHROMA444, 0);
  2959. if(fill_filter_caches(h, mb_type))
  2960. continue;
  2961. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2962. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2963. if (FRAME_MBAFF) {
  2964. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2965. } else {
  2966. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2967. }
  2968. }
  2969. }
  2970. }
  2971. h->slice_type= old_slice_type;
  2972. s->mb_x= end_x;
  2973. s->mb_y= end_mb_y - FRAME_MBAFF;
  2974. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2975. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2976. }
  2977. static void predict_field_decoding_flag(H264Context *h){
  2978. MpegEncContext * const s = &h->s;
  2979. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2980. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2981. ? s->current_picture.mb_type[mb_xy-1]
  2982. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2983. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  2984. : 0;
  2985. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2986. }
  2987. /**
  2988. * Draw edges and report progress for the last MB row.
  2989. */
  2990. static void decode_finish_row(H264Context *h){
  2991. MpegEncContext * const s = &h->s;
  2992. int top = 16*(s->mb_y >> FIELD_PICTURE);
  2993. int height = 16 << FRAME_MBAFF;
  2994. int deblock_border = (16 + 4) << FRAME_MBAFF;
  2995. int pic_height = 16*s->mb_height >> FIELD_PICTURE;
  2996. if (h->deblocking_filter) {
  2997. if((top + height) >= pic_height)
  2998. height += deblock_border;
  2999. top -= deblock_border;
  3000. }
  3001. if (top >= pic_height || (top + height) < h->emu_edge_height)
  3002. return;
  3003. height = FFMIN(height, pic_height - top);
  3004. if (top < h->emu_edge_height) {
  3005. height = top+height;
  3006. top = 0;
  3007. }
  3008. ff_draw_horiz_band(s, top, height);
  3009. if (s->dropable) return;
  3010. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
  3011. s->picture_structure==PICT_BOTTOM_FIELD);
  3012. }
  3013. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  3014. H264Context *h = *(void**)arg;
  3015. MpegEncContext * const s = &h->s;
  3016. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  3017. int lf_x_start = s->mb_x;
  3018. s->mb_skip_run= -1;
  3019. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  3020. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  3021. if( h->pps.cabac ) {
  3022. /* realign */
  3023. align_get_bits( &s->gb );
  3024. /* init cabac */
  3025. ff_init_cabac_states( &h->cabac);
  3026. ff_init_cabac_decoder( &h->cabac,
  3027. s->gb.buffer + get_bits_count(&s->gb)/8,
  3028. (get_bits_left(&s->gb) + 7)/8);
  3029. ff_h264_init_cabac_states(h);
  3030. for(;;){
  3031. //START_TIMER
  3032. int ret = ff_h264_decode_mb_cabac(h);
  3033. int eos;
  3034. //STOP_TIMER("decode_mb_cabac")
  3035. if(ret>=0) ff_h264_hl_decode_mb(h);
  3036. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  3037. s->mb_y++;
  3038. ret = ff_h264_decode_mb_cabac(h);
  3039. if(ret>=0) ff_h264_hl_decode_mb(h);
  3040. s->mb_y--;
  3041. }
  3042. eos = get_cabac_terminate( &h->cabac );
  3043. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  3044. 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);
  3045. if (s->mb_x >= lf_x_start) loop_filter(h, lf_x_start, s->mb_x + 1);
  3046. return 0;
  3047. }
  3048. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  3049. 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);
  3050. 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);
  3051. return -1;
  3052. }
  3053. if( ++s->mb_x >= s->mb_width ) {
  3054. loop_filter(h, lf_x_start, s->mb_x);
  3055. s->mb_x = lf_x_start = 0;
  3056. decode_finish_row(h);
  3057. ++s->mb_y;
  3058. if(FIELD_OR_MBAFF_PICTURE) {
  3059. ++s->mb_y;
  3060. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  3061. predict_field_decoding_flag(h);
  3062. }
  3063. }
  3064. if( eos || s->mb_y >= s->mb_height ) {
  3065. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3066. 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);
  3067. if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
  3068. return 0;
  3069. }
  3070. }
  3071. } else {
  3072. for(;;){
  3073. int ret = ff_h264_decode_mb_cavlc(h);
  3074. if(ret>=0) ff_h264_hl_decode_mb(h);
  3075. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  3076. s->mb_y++;
  3077. ret = ff_h264_decode_mb_cavlc(h);
  3078. if(ret>=0) ff_h264_hl_decode_mb(h);
  3079. s->mb_y--;
  3080. }
  3081. if(ret<0){
  3082. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3083. 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);
  3084. return -1;
  3085. }
  3086. if(++s->mb_x >= s->mb_width){
  3087. loop_filter(h, lf_x_start, s->mb_x);
  3088. s->mb_x = lf_x_start = 0;
  3089. decode_finish_row(h);
  3090. ++s->mb_y;
  3091. if(FIELD_OR_MBAFF_PICTURE) {
  3092. ++s->mb_y;
  3093. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  3094. predict_field_decoding_flag(h);
  3095. }
  3096. if(s->mb_y >= s->mb_height){
  3097. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3098. if( get_bits_count(&s->gb) == s->gb.size_in_bits
  3099. || get_bits_count(&s->gb) < s->gb.size_in_bits && s->avctx->error_recognition < FF_ER_AGGRESSIVE) {
  3100. 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);
  3101. return 0;
  3102. }else{
  3103. 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);
  3104. return -1;
  3105. }
  3106. }
  3107. }
  3108. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  3109. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3110. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  3111. 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);
  3112. if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
  3113. return 0;
  3114. }else{
  3115. 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);
  3116. return -1;
  3117. }
  3118. }
  3119. }
  3120. }
  3121. #if 0
  3122. for(;s->mb_y < s->mb_height; s->mb_y++){
  3123. for(;s->mb_x < s->mb_width; s->mb_x++){
  3124. int ret= decode_mb(h);
  3125. ff_h264_hl_decode_mb(h);
  3126. if(ret<0){
  3127. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3128. 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);
  3129. return -1;
  3130. }
  3131. if(++s->mb_x >= s->mb_width){
  3132. s->mb_x=0;
  3133. if(++s->mb_y >= s->mb_height){
  3134. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  3135. 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);
  3136. return 0;
  3137. }else{
  3138. 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);
  3139. return -1;
  3140. }
  3141. }
  3142. }
  3143. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  3144. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  3145. 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);
  3146. return 0;
  3147. }else{
  3148. 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);
  3149. return -1;
  3150. }
  3151. }
  3152. }
  3153. s->mb_x=0;
  3154. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  3155. }
  3156. #endif
  3157. return -1; //not reached
  3158. }
  3159. /**
  3160. * Call decode_slice() for each context.
  3161. *
  3162. * @param h h264 master context
  3163. * @param context_count number of contexts to execute
  3164. */
  3165. static void execute_decode_slices(H264Context *h, int context_count){
  3166. MpegEncContext * const s = &h->s;
  3167. AVCodecContext * const avctx= s->avctx;
  3168. H264Context *hx;
  3169. int i;
  3170. if (s->avctx->hwaccel)
  3171. return;
  3172. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  3173. return;
  3174. if(context_count == 1) {
  3175. decode_slice(avctx, &h);
  3176. } else {
  3177. for(i = 1; i < context_count; i++) {
  3178. hx = h->thread_context[i];
  3179. hx->s.error_recognition = avctx->error_recognition;
  3180. hx->s.error_count = 0;
  3181. hx->x264_build= h->x264_build;
  3182. }
  3183. avctx->execute(avctx, (void *)decode_slice,
  3184. h->thread_context, NULL, context_count, sizeof(void*));
  3185. /* pull back stuff from slices to master context */
  3186. hx = h->thread_context[context_count - 1];
  3187. s->mb_x = hx->s.mb_x;
  3188. s->mb_y = hx->s.mb_y;
  3189. s->dropable = hx->s.dropable;
  3190. s->picture_structure = hx->s.picture_structure;
  3191. for(i = 1; i < context_count; i++)
  3192. h->s.error_count += h->thread_context[i]->s.error_count;
  3193. }
  3194. }
  3195. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  3196. MpegEncContext * const s = &h->s;
  3197. AVCodecContext * const avctx= s->avctx;
  3198. H264Context *hx; ///< thread context
  3199. int buf_index;
  3200. int context_count;
  3201. int next_avc;
  3202. int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
  3203. int nals_needed=0; ///< number of NALs that need decoding before the next frame thread starts
  3204. int nal_index;
  3205. h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;
  3206. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  3207. h->current_slice = 0;
  3208. if (!s->first_field)
  3209. s->current_picture_ptr= NULL;
  3210. ff_h264_reset_sei(h);
  3211. }
  3212. for(;pass <= 1;pass++){
  3213. buf_index = 0;
  3214. context_count = 0;
  3215. next_avc = h->is_avc ? 0 : buf_size;
  3216. nal_index = 0;
  3217. for(;;){
  3218. int consumed;
  3219. int dst_length;
  3220. int bit_length;
  3221. const uint8_t *ptr;
  3222. int i, nalsize = 0;
  3223. int err;
  3224. if(buf_index >= next_avc) {
  3225. if(buf_index >= buf_size) break;
  3226. nalsize = 0;
  3227. for(i = 0; i < h->nal_length_size; i++)
  3228. nalsize = (nalsize << 8) | buf[buf_index++];
  3229. if(nalsize <= 0 || nalsize > buf_size - buf_index){
  3230. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  3231. break;
  3232. }
  3233. next_avc= buf_index + nalsize;
  3234. } else {
  3235. // start code prefix search
  3236. for(; buf_index + 3 < next_avc; buf_index++){
  3237. // This should always succeed in the first iteration.
  3238. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  3239. break;
  3240. }
  3241. if(buf_index+3 >= buf_size) break;
  3242. buf_index+=3;
  3243. if(buf_index >= next_avc) continue;
  3244. }
  3245. hx = h->thread_context[context_count];
  3246. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  3247. if (ptr==NULL || dst_length < 0){
  3248. return -1;
  3249. }
  3250. i= buf_index + consumed;
  3251. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  3252. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  3253. s->workaround_bugs |= FF_BUG_TRUNCATED;
  3254. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  3255. while(dst_length > 0 && ptr[dst_length - 1] == 0)
  3256. dst_length--;
  3257. }
  3258. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  3259. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  3260. 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);
  3261. }
  3262. if (h->is_avc && (nalsize != consumed) && nalsize){
  3263. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  3264. }
  3265. buf_index += consumed;
  3266. nal_index++;
  3267. if(pass == 0) {
  3268. // packets can sometimes contain multiple PPS/SPS
  3269. // e.g. two PAFF field pictures in one packet, or a demuxer which splits NALs strangely
  3270. // if so, when frame threading we can't start the next thread until we've read all of them
  3271. switch (hx->nal_unit_type) {
  3272. case NAL_SPS:
  3273. case NAL_PPS:
  3274. case NAL_IDR_SLICE:
  3275. case NAL_SLICE:
  3276. nals_needed = nal_index;
  3277. }
  3278. continue;
  3279. }
  3280. //FIXME do not discard SEI id
  3281. if(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
  3282. continue;
  3283. again:
  3284. err = 0;
  3285. switch(hx->nal_unit_type){
  3286. case NAL_IDR_SLICE:
  3287. if (h->nal_unit_type != NAL_IDR_SLICE) {
  3288. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  3289. return -1;
  3290. }
  3291. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  3292. case NAL_SLICE:
  3293. init_get_bits(&hx->s.gb, ptr, bit_length);
  3294. hx->intra_gb_ptr=
  3295. hx->inter_gb_ptr= &hx->s.gb;
  3296. hx->s.data_partitioning = 0;
  3297. if((err = decode_slice_header(hx, h)))
  3298. break;
  3299. s->current_picture_ptr->key_frame |=
  3300. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  3301. (h->sei_recovery_frame_cnt >= 0);
  3302. if (h->current_slice == 1) {
  3303. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
  3304. decode_postinit(h, nal_index >= nals_needed);
  3305. }
  3306. if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  3307. return -1;
  3308. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  3309. ff_vdpau_h264_picture_start(s);
  3310. }
  3311. if(hx->redundant_pic_count==0
  3312. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  3313. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  3314. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  3315. && avctx->skip_frame < AVDISCARD_ALL){
  3316. if(avctx->hwaccel) {
  3317. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  3318. return -1;
  3319. }else
  3320. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  3321. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  3322. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  3323. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  3324. }else
  3325. context_count++;
  3326. }
  3327. break;
  3328. case NAL_DPA:
  3329. init_get_bits(&hx->s.gb, ptr, bit_length);
  3330. hx->intra_gb_ptr=
  3331. hx->inter_gb_ptr= NULL;
  3332. if ((err = decode_slice_header(hx, h)) < 0)
  3333. break;
  3334. hx->s.data_partitioning = 1;
  3335. break;
  3336. case NAL_DPB:
  3337. init_get_bits(&hx->intra_gb, ptr, bit_length);
  3338. hx->intra_gb_ptr= &hx->intra_gb;
  3339. break;
  3340. case NAL_DPC:
  3341. init_get_bits(&hx->inter_gb, ptr, bit_length);
  3342. hx->inter_gb_ptr= &hx->inter_gb;
  3343. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  3344. && s->context_initialized
  3345. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  3346. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  3347. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  3348. && avctx->skip_frame < AVDISCARD_ALL)
  3349. context_count++;
  3350. break;
  3351. case NAL_SEI:
  3352. init_get_bits(&s->gb, ptr, bit_length);
  3353. ff_h264_decode_sei(h);
  3354. break;
  3355. case NAL_SPS:
  3356. init_get_bits(&s->gb, ptr, bit_length);
  3357. ff_h264_decode_seq_parameter_set(h);
  3358. if (s->flags& CODEC_FLAG_LOW_DELAY ||
  3359. (h->sps.bitstream_restriction_flag && !h->sps.num_reorder_frames))
  3360. s->low_delay=1;
  3361. if(avctx->has_b_frames < 2)
  3362. avctx->has_b_frames= !s->low_delay;
  3363. if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
  3364. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  3365. avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  3366. h->pixel_shift = h->sps.bit_depth_luma > 8;
  3367. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
  3368. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
  3369. dsputil_init(&s->dsp, s->avctx);
  3370. } else {
  3371. av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  3372. return -1;
  3373. }
  3374. }
  3375. break;
  3376. case NAL_PPS:
  3377. init_get_bits(&s->gb, ptr, bit_length);
  3378. ff_h264_decode_picture_parameter_set(h, bit_length);
  3379. break;
  3380. case NAL_AUD:
  3381. case NAL_END_SEQUENCE:
  3382. case NAL_END_STREAM:
  3383. case NAL_FILLER_DATA:
  3384. case NAL_SPS_EXT:
  3385. case NAL_AUXILIARY_SLICE:
  3386. break;
  3387. default:
  3388. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  3389. }
  3390. if(context_count == h->max_contexts) {
  3391. execute_decode_slices(h, context_count);
  3392. context_count = 0;
  3393. }
  3394. if (err < 0)
  3395. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  3396. else if(err == 1) {
  3397. /* Slice could not be decoded in parallel mode, copy down
  3398. * NAL unit stuff to context 0 and restart. Note that
  3399. * rbsp_buffer is not transferred, but since we no longer
  3400. * run in parallel mode this should not be an issue. */
  3401. h->nal_unit_type = hx->nal_unit_type;
  3402. h->nal_ref_idc = hx->nal_ref_idc;
  3403. hx = h;
  3404. goto again;
  3405. }
  3406. }
  3407. }
  3408. if(context_count)
  3409. execute_decode_slices(h, context_count);
  3410. return buf_index;
  3411. }
  3412. /**
  3413. * returns the number of bytes consumed for building the current frame
  3414. */
  3415. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  3416. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  3417. if(pos+10>buf_size) pos=buf_size; // oops ;)
  3418. return pos;
  3419. }
  3420. static int decode_frame(AVCodecContext *avctx,
  3421. void *data, int *data_size,
  3422. AVPacket *avpkt)
  3423. {
  3424. const uint8_t *buf = avpkt->data;
  3425. int buf_size = avpkt->size;
  3426. H264Context *h = avctx->priv_data;
  3427. MpegEncContext *s = &h->s;
  3428. AVFrame *pict = data;
  3429. int buf_index;
  3430. s->flags= avctx->flags;
  3431. s->flags2= avctx->flags2;
  3432. /* end of stream, output what is still in the buffers */
  3433. out:
  3434. if (buf_size == 0) {
  3435. Picture *out;
  3436. int i, out_idx;
  3437. s->current_picture_ptr = NULL;
  3438. //FIXME factorize this with the output code below
  3439. out = h->delayed_pic[0];
  3440. out_idx = 0;
  3441. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  3442. if(h->delayed_pic[i]->poc < out->poc){
  3443. out = h->delayed_pic[i];
  3444. out_idx = i;
  3445. }
  3446. for(i=out_idx; h->delayed_pic[i]; i++)
  3447. h->delayed_pic[i] = h->delayed_pic[i+1];
  3448. if(out){
  3449. *data_size = sizeof(AVFrame);
  3450. *pict= *(AVFrame*)out;
  3451. }
  3452. return 0;
  3453. }
  3454. buf_index=decode_nal_units(h, buf, buf_size);
  3455. if(buf_index < 0)
  3456. return -1;
  3457. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  3458. buf_size = 0;
  3459. goto out;
  3460. }
  3461. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  3462. if (avctx->skip_frame >= AVDISCARD_NONREF)
  3463. return 0;
  3464. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  3465. return -1;
  3466. }
  3467. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  3468. if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h, 1);
  3469. field_end(h, 0);
  3470. if (!h->next_output_pic) {
  3471. /* Wait for second field. */
  3472. *data_size = 0;
  3473. } else {
  3474. *data_size = sizeof(AVFrame);
  3475. *pict = *(AVFrame*)h->next_output_pic;
  3476. }
  3477. }
  3478. assert(pict->data[0] || !*data_size);
  3479. ff_print_debug_info(s, pict);
  3480. //printf("out %d\n", (int)pict->data[0]);
  3481. return get_consumed_bytes(s, buf_index, buf_size);
  3482. }
  3483. #if 0
  3484. static inline void fill_mb_avail(H264Context *h){
  3485. MpegEncContext * const s = &h->s;
  3486. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3487. if(s->mb_y){
  3488. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  3489. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  3490. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  3491. }else{
  3492. h->mb_avail[0]=
  3493. h->mb_avail[1]=
  3494. h->mb_avail[2]= 0;
  3495. }
  3496. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  3497. h->mb_avail[4]= 1; //FIXME move out
  3498. h->mb_avail[5]= 0; //FIXME move out
  3499. }
  3500. #endif
  3501. #ifdef TEST
  3502. #undef printf
  3503. #undef random
  3504. #define COUNT 8000
  3505. #define SIZE (COUNT*40)
  3506. int main(void){
  3507. int i;
  3508. uint8_t temp[SIZE];
  3509. PutBitContext pb;
  3510. GetBitContext gb;
  3511. // int int_temp[10000];
  3512. DSPContext dsp;
  3513. AVCodecContext avctx;
  3514. dsputil_init(&dsp, &avctx);
  3515. init_put_bits(&pb, temp, SIZE);
  3516. printf("testing unsigned exp golomb\n");
  3517. for(i=0; i<COUNT; i++){
  3518. START_TIMER
  3519. set_ue_golomb(&pb, i);
  3520. STOP_TIMER("set_ue_golomb");
  3521. }
  3522. flush_put_bits(&pb);
  3523. init_get_bits(&gb, temp, 8*SIZE);
  3524. for(i=0; i<COUNT; i++){
  3525. int j, s;
  3526. s= show_bits(&gb, 24);
  3527. START_TIMER
  3528. j= get_ue_golomb(&gb);
  3529. if(j != i){
  3530. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3531. // return -1;
  3532. }
  3533. STOP_TIMER("get_ue_golomb");
  3534. }
  3535. init_put_bits(&pb, temp, SIZE);
  3536. printf("testing signed exp golomb\n");
  3537. for(i=0; i<COUNT; i++){
  3538. START_TIMER
  3539. set_se_golomb(&pb, i - COUNT/2);
  3540. STOP_TIMER("set_se_golomb");
  3541. }
  3542. flush_put_bits(&pb);
  3543. init_get_bits(&gb, temp, 8*SIZE);
  3544. for(i=0; i<COUNT; i++){
  3545. int j, s;
  3546. s= show_bits(&gb, 24);
  3547. START_TIMER
  3548. j= get_se_golomb(&gb);
  3549. if(j != i - COUNT/2){
  3550. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3551. // return -1;
  3552. }
  3553. STOP_TIMER("get_se_golomb");
  3554. }
  3555. #if 0
  3556. printf("testing 4x4 (I)DCT\n");
  3557. DCTELEM block[16];
  3558. uint8_t src[16], ref[16];
  3559. uint64_t error= 0, max_error=0;
  3560. for(i=0; i<COUNT; i++){
  3561. int j;
  3562. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  3563. for(j=0; j<16; j++){
  3564. ref[j]= random()%255;
  3565. src[j]= random()%255;
  3566. }
  3567. h264_diff_dct_c(block, src, ref, 4);
  3568. //normalize
  3569. for(j=0; j<16; j++){
  3570. // printf("%d ", block[j]);
  3571. block[j]= block[j]*4;
  3572. if(j&1) block[j]= (block[j]*4 + 2)/5;
  3573. if(j&4) block[j]= (block[j]*4 + 2)/5;
  3574. }
  3575. // printf("\n");
  3576. h->h264dsp.h264_idct_add(ref, block, 4);
  3577. /* for(j=0; j<16; j++){
  3578. printf("%d ", ref[j]);
  3579. }
  3580. printf("\n");*/
  3581. for(j=0; j<16; j++){
  3582. int diff= FFABS(src[j] - ref[j]);
  3583. error+= diff*diff;
  3584. max_error= FFMAX(max_error, diff);
  3585. }
  3586. }
  3587. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  3588. printf("testing quantizer\n");
  3589. for(qp=0; qp<52; qp++){
  3590. for(i=0; i<16; i++)
  3591. src1_block[i]= src2_block[i]= random()%255;
  3592. }
  3593. printf("Testing NAL layer\n");
  3594. uint8_t bitstream[COUNT];
  3595. uint8_t nal[COUNT*2];
  3596. H264Context h;
  3597. memset(&h, 0, sizeof(H264Context));
  3598. for(i=0; i<COUNT; i++){
  3599. int zeros= i;
  3600. int nal_length;
  3601. int consumed;
  3602. int out_length;
  3603. uint8_t *out;
  3604. int j;
  3605. for(j=0; j<COUNT; j++){
  3606. bitstream[j]= (random() % 255) + 1;
  3607. }
  3608. for(j=0; j<zeros; j++){
  3609. int pos= random() % COUNT;
  3610. while(bitstream[pos] == 0){
  3611. pos++;
  3612. pos %= COUNT;
  3613. }
  3614. bitstream[pos]=0;
  3615. }
  3616. START_TIMER
  3617. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  3618. if(nal_length<0){
  3619. printf("encoding failed\n");
  3620. return -1;
  3621. }
  3622. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  3623. STOP_TIMER("NAL")
  3624. if(out_length != COUNT){
  3625. printf("incorrect length %d %d\n", out_length, COUNT);
  3626. return -1;
  3627. }
  3628. if(consumed != nal_length){
  3629. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  3630. return -1;
  3631. }
  3632. if(memcmp(bitstream, out, COUNT)){
  3633. printf("mismatch\n");
  3634. return -1;
  3635. }
  3636. }
  3637. #endif
  3638. printf("Testing RBSP\n");
  3639. return 0;
  3640. }
  3641. #endif /* TEST */
  3642. av_cold void ff_h264_free_context(H264Context *h)
  3643. {
  3644. int i;
  3645. free_tables(h, 1); //FIXME cleanup init stuff perhaps
  3646. for(i = 0; i < MAX_SPS_COUNT; i++)
  3647. av_freep(h->sps_buffers + i);
  3648. for(i = 0; i < MAX_PPS_COUNT; i++)
  3649. av_freep(h->pps_buffers + i);
  3650. }
  3651. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  3652. {
  3653. H264Context *h = avctx->priv_data;
  3654. MpegEncContext *s = &h->s;
  3655. ff_h264_free_context(h);
  3656. MPV_common_end(s);
  3657. // memset(h, 0, sizeof(H264Context));
  3658. return 0;
  3659. }
  3660. static const AVProfile profiles[] = {
  3661. { FF_PROFILE_H264_BASELINE, "Baseline" },
  3662. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  3663. { FF_PROFILE_H264_MAIN, "Main" },
  3664. { FF_PROFILE_H264_EXTENDED, "Extended" },
  3665. { FF_PROFILE_H264_HIGH, "High" },
  3666. { FF_PROFILE_H264_HIGH_10, "High 10" },
  3667. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  3668. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  3669. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  3670. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  3671. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  3672. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  3673. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  3674. { FF_PROFILE_UNKNOWN },
  3675. };
  3676. AVCodec ff_h264_decoder = {
  3677. "h264",
  3678. AVMEDIA_TYPE_VIDEO,
  3679. CODEC_ID_H264,
  3680. sizeof(H264Context),
  3681. ff_h264_decode_init,
  3682. NULL,
  3683. ff_h264_decode_end,
  3684. decode_frame,
  3685. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  3686. CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
  3687. .flush= flush_dpb,
  3688. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  3689. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  3690. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  3691. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3692. };
  3693. #if CONFIG_H264_VDPAU_DECODER
  3694. AVCodec ff_h264_vdpau_decoder = {
  3695. "h264_vdpau",
  3696. AVMEDIA_TYPE_VIDEO,
  3697. CODEC_ID_H264,
  3698. sizeof(H264Context),
  3699. ff_h264_decode_init,
  3700. NULL,
  3701. ff_h264_decode_end,
  3702. decode_frame,
  3703. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  3704. .flush= flush_dpb,
  3705. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  3706. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  3707. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3708. };
  3709. #endif