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.

4221 lines
161KB

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