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.

4184 lines
161KB

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