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.

4302 lines
164KB

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