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.

4269 lines
162KB

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