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.

3303 lines
123KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/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 "libavcore/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 "h264_parser.h"
  35. #include "golomb.h"
  36. #include "mathops.h"
  37. #include "rectangle.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[52]={
  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,
  45. };
  46. static const uint8_t div6[52]={
  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,
  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 0
  136. for(i=0; i<length; i++)
  137. printf("%2X ", src[i]);
  138. #endif
  139. #if HAVE_FAST_UNALIGNED
  140. # if HAVE_FAST_64BIT
  141. # define RS 7
  142. for(i=0; i+1<length; i+=9){
  143. if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  144. # else
  145. # define RS 3
  146. for(i=0; i+1<length; i+=5){
  147. if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
  148. # endif
  149. continue;
  150. if(i>0 && !src[i]) i--;
  151. while(src[i]) i++;
  152. #else
  153. # define RS 0
  154. for(i=0; i+1<length; i+=2){
  155. if(src[i]) continue;
  156. if(i>0 && src[i-1]==0) i--;
  157. #endif
  158. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  159. if(src[i+2]!=3){
  160. /* startcode, so we must be past the end */
  161. length=i;
  162. }
  163. break;
  164. }
  165. i-= RS;
  166. }
  167. if(i>=length-1){ //no escaped 0
  168. *dst_length= length;
  169. *consumed= length+1; //+1 for the header
  170. return src;
  171. }
  172. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  173. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  174. dst= h->rbsp_buffer[bufidx];
  175. if (dst == NULL){
  176. return NULL;
  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. int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  207. int v= *src;
  208. int r;
  209. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  210. for(r=1; r<9; r++){
  211. if(v&1) return r;
  212. v>>=1;
  213. }
  214. return 0;
  215. }
  216. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  217. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  218. int src_x_offset, int src_y_offset,
  219. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  220. MpegEncContext * const s = &h->s;
  221. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  222. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  223. const int luma_xy= (mx&3) + ((my&3)<<2);
  224. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  225. uint8_t * src_cb, * src_cr;
  226. int extra_width= h->emu_edge_width;
  227. int extra_height= h->emu_edge_height;
  228. int emu=0;
  229. const int full_mx= mx>>2;
  230. const int full_my= my>>2;
  231. const int pic_width = 16*s->mb_width;
  232. const int pic_height = 16*s->mb_height >> MB_FIELD;
  233. if(mx&7) extra_width -= 3;
  234. if(my&7) extra_height -= 3;
  235. if( full_mx < 0-extra_width
  236. || full_my < 0-extra_height
  237. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  238. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  239. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  240. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  241. emu=1;
  242. }
  243. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  244. if(!square){
  245. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  246. }
  247. if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  248. if(MB_FIELD){
  249. // chroma offset when predicting from a field of opposite parity
  250. my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
  251. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  252. }
  253. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  254. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  255. if(emu){
  256. ff_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);
  257. src_cb= s->edge_emu_buffer;
  258. }
  259. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  260. if(emu){
  261. ff_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);
  262. src_cr= s->edge_emu_buffer;
  263. }
  264. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  265. }
  266. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  267. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  268. int x_offset, int y_offset,
  269. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  270. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  271. int list0, int list1){
  272. MpegEncContext * const s = &h->s;
  273. qpel_mc_func *qpix_op= qpix_put;
  274. h264_chroma_mc_func chroma_op= chroma_put;
  275. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  276. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  277. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  278. x_offset += 8*s->mb_x;
  279. y_offset += 8*(s->mb_y >> MB_FIELD);
  280. if(list0){
  281. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  282. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  283. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  284. qpix_op, chroma_op);
  285. qpix_op= qpix_avg;
  286. chroma_op= chroma_avg;
  287. }
  288. if(list1){
  289. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  290. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  291. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  292. qpix_op, chroma_op);
  293. }
  294. }
  295. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  296. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  297. int x_offset, int y_offset,
  298. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  299. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  300. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  301. int list0, int list1){
  302. MpegEncContext * const s = &h->s;
  303. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  304. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  305. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  306. x_offset += 8*s->mb_x;
  307. y_offset += 8*(s->mb_y >> MB_FIELD);
  308. if(list0 && list1){
  309. /* don't optimize for luma-only case, since B-frames usually
  310. * use implicit weights => chroma too. */
  311. uint8_t *tmp_cb = s->obmc_scratchpad;
  312. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  313. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  314. int refn0 = h->ref_cache[0][ scan8[n] ];
  315. int refn1 = h->ref_cache[1][ scan8[n] ];
  316. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  317. dest_y, dest_cb, dest_cr,
  318. x_offset, y_offset, qpix_put, chroma_put);
  319. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  320. tmp_y, tmp_cb, tmp_cr,
  321. x_offset, y_offset, qpix_put, chroma_put);
  322. if(h->use_weight == 2){
  323. int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
  324. int weight1 = 64 - weight0;
  325. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  326. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  327. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  328. }else{
  329. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  330. h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
  331. h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
  332. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  333. h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
  334. h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
  335. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  336. h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
  337. h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
  338. }
  339. }else{
  340. int list = list1 ? 1 : 0;
  341. int refn = h->ref_cache[list][ scan8[n] ];
  342. Picture *ref= &h->ref_list[list][refn];
  343. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  344. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  345. qpix_put, chroma_put);
  346. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  347. h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
  348. if(h->use_weight_chroma){
  349. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  350. h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
  351. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  352. h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
  353. }
  354. }
  355. }
  356. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  357. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  358. int x_offset, int y_offset,
  359. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  360. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  361. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  362. int list0, int list1){
  363. if((h->use_weight==2 && list0 && list1
  364. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
  365. || h->use_weight==1)
  366. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  367. x_offset, y_offset, qpix_put, chroma_put,
  368. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  369. else
  370. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  371. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  372. }
  373. static inline void prefetch_motion(H264Context *h, int list){
  374. /* fetch pixels for estimated mv 4 macroblocks ahead
  375. * optimized for 64byte cache lines */
  376. MpegEncContext * const s = &h->s;
  377. const int refn = h->ref_cache[list][scan8[0]];
  378. if(refn >= 0){
  379. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  380. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  381. uint8_t **src= h->ref_list[list][refn].data;
  382. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  383. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  384. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  385. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  386. }
  387. }
  388. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  389. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  390. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  391. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  392. MpegEncContext * const s = &h->s;
  393. const int mb_xy= h->mb_xy;
  394. const int mb_type= s->current_picture.mb_type[mb_xy];
  395. assert(IS_INTER(mb_type));
  396. prefetch_motion(h, 0);
  397. if(IS_16X16(mb_type)){
  398. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  399. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  400. weight_op, weight_avg,
  401. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  402. }else if(IS_16X8(mb_type)){
  403. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  404. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  405. &weight_op[1], &weight_avg[1],
  406. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  407. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  408. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  409. &weight_op[1], &weight_avg[1],
  410. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  411. }else if(IS_8X16(mb_type)){
  412. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  413. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  414. &weight_op[2], &weight_avg[2],
  415. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  416. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  417. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  418. &weight_op[2], &weight_avg[2],
  419. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  420. }else{
  421. int i;
  422. assert(IS_8X8(mb_type));
  423. for(i=0; i<4; i++){
  424. const int sub_mb_type= h->sub_mb_type[i];
  425. const int n= 4*i;
  426. int x_offset= (i&1)<<2;
  427. int y_offset= (i&2)<<1;
  428. if(IS_SUB_8X8(sub_mb_type)){
  429. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  430. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  431. &weight_op[3], &weight_avg[3],
  432. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  433. }else if(IS_SUB_8X4(sub_mb_type)){
  434. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  435. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  436. &weight_op[4], &weight_avg[4],
  437. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  438. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  439. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  440. &weight_op[4], &weight_avg[4],
  441. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  442. }else if(IS_SUB_4X8(sub_mb_type)){
  443. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  444. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  445. &weight_op[5], &weight_avg[5],
  446. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  447. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  448. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  449. &weight_op[5], &weight_avg[5],
  450. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  451. }else{
  452. int j;
  453. assert(IS_SUB_4X4(sub_mb_type));
  454. for(j=0; j<4; j++){
  455. int sub_x_offset= x_offset + 2*(j&1);
  456. int sub_y_offset= y_offset + (j&2);
  457. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  458. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  459. &weight_op[6], &weight_avg[6],
  460. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  461. }
  462. }
  463. }
  464. }
  465. prefetch_motion(h, 1);
  466. }
  467. static void free_tables(H264Context *h){
  468. int i;
  469. H264Context *hx;
  470. av_freep(&h->intra4x4_pred_mode);
  471. av_freep(&h->chroma_pred_mode_table);
  472. av_freep(&h->cbp_table);
  473. av_freep(&h->mvd_table[0]);
  474. av_freep(&h->mvd_table[1]);
  475. av_freep(&h->direct_table);
  476. av_freep(&h->non_zero_count);
  477. av_freep(&h->slice_table_base);
  478. h->slice_table= NULL;
  479. av_freep(&h->list_counts);
  480. av_freep(&h->mb2b_xy);
  481. av_freep(&h->mb2br_xy);
  482. for(i = 0; i < MAX_THREADS; i++) {
  483. hx = h->thread_context[i];
  484. if(!hx) continue;
  485. av_freep(&hx->top_borders[1]);
  486. av_freep(&hx->top_borders[0]);
  487. av_freep(&hx->s.obmc_scratchpad);
  488. av_freep(&hx->rbsp_buffer[1]);
  489. av_freep(&hx->rbsp_buffer[0]);
  490. hx->rbsp_buffer_size[0] = 0;
  491. hx->rbsp_buffer_size[1] = 0;
  492. if (i) av_freep(&h->thread_context[i]);
  493. }
  494. }
  495. static void init_dequant8_coeff_table(H264Context *h){
  496. int i,q,x;
  497. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  498. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  499. for(i=0; i<2; i++ ){
  500. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  501. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  502. break;
  503. }
  504. for(q=0; q<52; q++){
  505. int shift = div6[q];
  506. int idx = rem6[q];
  507. for(x=0; x<64; x++)
  508. h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
  509. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  510. h->pps.scaling_matrix8[i][x]) << shift;
  511. }
  512. }
  513. }
  514. static void init_dequant4_coeff_table(H264Context *h){
  515. int i,j,q,x;
  516. for(i=0; i<6; i++ ){
  517. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  518. for(j=0; j<i; j++){
  519. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  520. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  521. break;
  522. }
  523. }
  524. if(j<i)
  525. continue;
  526. for(q=0; q<52; q++){
  527. int shift = div6[q] + 2;
  528. int idx = rem6[q];
  529. for(x=0; x<16; x++)
  530. h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
  531. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  532. h->pps.scaling_matrix4[i][x]) << shift;
  533. }
  534. }
  535. }
  536. static void init_dequant_tables(H264Context *h){
  537. int i,x;
  538. init_dequant4_coeff_table(h);
  539. if(h->pps.transform_8x8_mode)
  540. init_dequant8_coeff_table(h);
  541. if(h->sps.transform_bypass){
  542. for(i=0; i<6; i++)
  543. for(x=0; x<16; x++)
  544. h->dequant4_coeff[i][0][x] = 1<<6;
  545. if(h->pps.transform_8x8_mode)
  546. for(i=0; i<2; i++)
  547. for(x=0; x<64; x++)
  548. h->dequant8_coeff[i][0][x] = 1<<6;
  549. }
  550. }
  551. int ff_h264_alloc_tables(H264Context *h){
  552. MpegEncContext * const s = &h->s;
  553. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  554. const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
  555. int x,y;
  556. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail)
  557. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
  558. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
  559. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
  560. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
  561. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
  562. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
  563. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
  564. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
  565. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  566. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  567. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
  568. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
  569. for(y=0; y<s->mb_height; y++){
  570. for(x=0; x<s->mb_width; x++){
  571. const int mb_xy= x + y*s->mb_stride;
  572. const int b_xy = 4*x + 4*y*h->b_stride;
  573. h->mb2b_xy [mb_xy]= b_xy;
  574. h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
  575. }
  576. }
  577. s->obmc_scratchpad = NULL;
  578. if(!h->dequant4_coeff[0])
  579. init_dequant_tables(h);
  580. return 0;
  581. fail:
  582. free_tables(h);
  583. return -1;
  584. }
  585. /**
  586. * Mimic alloc_tables(), but for every context thread.
  587. */
  588. static void clone_tables(H264Context *dst, H264Context *src, int i){
  589. MpegEncContext * const s = &src->s;
  590. dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
  591. dst->non_zero_count = src->non_zero_count;
  592. dst->slice_table = src->slice_table;
  593. dst->cbp_table = src->cbp_table;
  594. dst->mb2b_xy = src->mb2b_xy;
  595. dst->mb2br_xy = src->mb2br_xy;
  596. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  597. dst->mvd_table[0] = src->mvd_table[0] + i*8*2*s->mb_stride;
  598. dst->mvd_table[1] = src->mvd_table[1] + i*8*2*s->mb_stride;
  599. dst->direct_table = src->direct_table;
  600. dst->list_counts = src->list_counts;
  601. dst->s.obmc_scratchpad = NULL;
  602. ff_h264_pred_init(&dst->hpc, src->s.codec_id);
  603. }
  604. /**
  605. * Init context
  606. * Allocate buffers which are not shared amongst multiple threads.
  607. */
  608. static int context_init(H264Context *h){
  609. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  610. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  611. h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
  612. h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
  613. return 0;
  614. fail:
  615. return -1; // free_tables will clean up for us
  616. }
  617. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
  618. static av_cold void common_init(H264Context *h){
  619. MpegEncContext * const s = &h->s;
  620. s->width = s->avctx->width;
  621. s->height = s->avctx->height;
  622. s->codec_id= s->avctx->codec->id;
  623. ff_h264dsp_init(&h->h264dsp);
  624. ff_h264_pred_init(&h->hpc, s->codec_id);
  625. h->dequant_coeff_pps= -1;
  626. s->unrestricted_mv=1;
  627. s->decode=1; //FIXME
  628. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  629. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  630. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  631. }
  632. int ff_h264_decode_extradata(H264Context *h)
  633. {
  634. AVCodecContext *avctx = h->s.avctx;
  635. if(*(char *)avctx->extradata == 1){
  636. int i, cnt, nalsize;
  637. unsigned char *p = avctx->extradata;
  638. h->is_avc = 1;
  639. if(avctx->extradata_size < 7) {
  640. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  641. return -1;
  642. }
  643. /* sps and pps in the avcC always have length coded with 2 bytes,
  644. so put a fake nal_length_size = 2 while parsing them */
  645. h->nal_length_size = 2;
  646. // Decode sps from avcC
  647. cnt = *(p+5) & 0x1f; // Number of sps
  648. p += 6;
  649. for (i = 0; i < cnt; i++) {
  650. nalsize = AV_RB16(p) + 2;
  651. if(decode_nal_units(h, p, nalsize) < 0) {
  652. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  653. return -1;
  654. }
  655. p += nalsize;
  656. }
  657. // Decode pps from avcC
  658. cnt = *(p++); // Number of pps
  659. for (i = 0; i < cnt; i++) {
  660. nalsize = AV_RB16(p) + 2;
  661. if(decode_nal_units(h, p, nalsize) != nalsize) {
  662. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  663. return -1;
  664. }
  665. p += nalsize;
  666. }
  667. // Now store right nal length size, that will be use to parse all other nals
  668. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  669. } else {
  670. h->is_avc = 0;
  671. if(decode_nal_units(h, avctx->extradata, avctx->extradata_size) < 0)
  672. return -1;
  673. }
  674. return 0;
  675. }
  676. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  677. H264Context *h= avctx->priv_data;
  678. MpegEncContext * const s = &h->s;
  679. MPV_decode_defaults(s);
  680. s->avctx = avctx;
  681. common_init(h);
  682. s->out_format = FMT_H264;
  683. s->workaround_bugs= avctx->workaround_bugs;
  684. // set defaults
  685. // s->decode_mb= ff_h263_decode_mb;
  686. s->quarter_sample = 1;
  687. if(!avctx->has_b_frames)
  688. s->low_delay= 1;
  689. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  690. ff_h264_decode_init_vlc();
  691. h->thread_context[0] = h;
  692. h->outputed_poc = INT_MIN;
  693. h->prev_poc_msb= 1<<16;
  694. h->x264_build = -1;
  695. ff_h264_reset_sei(h);
  696. if(avctx->codec_id == CODEC_ID_H264){
  697. if(avctx->ticks_per_frame == 1){
  698. s->avctx->time_base.den *=2;
  699. }
  700. avctx->ticks_per_frame = 2;
  701. }
  702. if(avctx->extradata_size > 0 && avctx->extradata &&
  703. ff_h264_decode_extradata(h))
  704. return -1;
  705. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  706. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  707. s->low_delay = 0;
  708. }
  709. return 0;
  710. }
  711. int ff_h264_frame_start(H264Context *h){
  712. MpegEncContext * const s = &h->s;
  713. int i;
  714. if(MPV_frame_start(s, s->avctx) < 0)
  715. return -1;
  716. ff_er_frame_start(s);
  717. /*
  718. * MPV_frame_start uses pict_type to derive key_frame.
  719. * This is incorrect for H.264; IDR markings must be used.
  720. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  721. * See decode_nal_units().
  722. */
  723. s->current_picture_ptr->key_frame= 0;
  724. s->current_picture_ptr->mmco_reset= 0;
  725. assert(s->linesize && s->uvlinesize);
  726. for(i=0; i<16; i++){
  727. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  728. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  729. }
  730. for(i=0; i<4; i++){
  731. h->block_offset[16+i]=
  732. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  733. h->block_offset[24+16+i]=
  734. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  735. }
  736. /* can't be in alloc_tables because linesize isn't known there.
  737. * FIXME: redo bipred weight to not require extra buffer? */
  738. for(i = 0; i < s->avctx->thread_count; i++)
  739. if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  740. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  741. /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
  742. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  743. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  744. // We mark the current picture as non-reference after allocating it, so
  745. // that if we break out due to an error it can be released automatically
  746. // in the next MPV_frame_start().
  747. // SVQ3 as well as most other codecs have only last/next/current and thus
  748. // get released even with set reference, besides SVQ3 and others do not
  749. // mark frames as reference later "naturally".
  750. if(s->codec_id != CODEC_ID_SVQ3)
  751. s->current_picture_ptr->reference= 0;
  752. s->current_picture_ptr->field_poc[0]=
  753. s->current_picture_ptr->field_poc[1]= INT_MAX;
  754. assert(s->current_picture_ptr->long_ref==0);
  755. return 0;
  756. }
  757. 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 simple){
  758. MpegEncContext * const s = &h->s;
  759. uint8_t *top_border;
  760. int top_idx = 1;
  761. src_y -= linesize;
  762. src_cb -= uvlinesize;
  763. src_cr -= uvlinesize;
  764. if(!simple && FRAME_MBAFF){
  765. if(s->mb_y&1){
  766. if(!MB_MBAFF){
  767. top_border = h->top_borders[0][s->mb_x];
  768. AV_COPY128(top_border, src_y + 15*linesize);
  769. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  770. AV_COPY64(top_border+16, src_cb+7*uvlinesize);
  771. AV_COPY64(top_border+24, src_cr+7*uvlinesize);
  772. }
  773. }
  774. }else if(MB_MBAFF){
  775. top_idx = 0;
  776. }else
  777. return;
  778. }
  779. top_border = h->top_borders[top_idx][s->mb_x];
  780. // There are two lines saved, the line above the the top macroblock of a pair,
  781. // and the line above the bottom macroblock
  782. AV_COPY128(top_border, src_y + 16*linesize);
  783. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  784. AV_COPY64(top_border+16, src_cb+8*uvlinesize);
  785. AV_COPY64(top_border+24, src_cr+8*uvlinesize);
  786. }
  787. }
  788. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
  789. MpegEncContext * const s = &h->s;
  790. int deblock_left;
  791. int deblock_top;
  792. int top_idx = 1;
  793. uint8_t *top_border_m1;
  794. uint8_t *top_border;
  795. if(!simple && FRAME_MBAFF){
  796. if(s->mb_y&1){
  797. if(!MB_MBAFF)
  798. return;
  799. }else{
  800. top_idx = MB_MBAFF ? 0 : 1;
  801. }
  802. }
  803. if(h->deblocking_filter == 2) {
  804. deblock_left = h->left_type[0];
  805. deblock_top = h->top_type;
  806. } else {
  807. deblock_left = (s->mb_x > 0);
  808. deblock_top = (s->mb_y > !!MB_FIELD);
  809. }
  810. src_y -= linesize + 1;
  811. src_cb -= uvlinesize + 1;
  812. src_cr -= uvlinesize + 1;
  813. top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
  814. top_border = h->top_borders[top_idx][s->mb_x];
  815. #define XCHG(a,b,xchg)\
  816. if (xchg) AV_SWAP64(b,a);\
  817. else AV_COPY64(b,a);
  818. if(deblock_top){
  819. if(deblock_left){
  820. XCHG(top_border_m1+8, src_y -7, 1);
  821. }
  822. XCHG(top_border+0, src_y +1, xchg);
  823. XCHG(top_border+8, src_y +9, 1);
  824. if(s->mb_x+1 < s->mb_width){
  825. XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +17, 1);
  826. }
  827. }
  828. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  829. if(deblock_top){
  830. if(deblock_left){
  831. XCHG(top_border_m1+16, src_cb -7, 1);
  832. XCHG(top_border_m1+24, src_cr -7, 1);
  833. }
  834. XCHG(top_border+16, src_cb+1, 1);
  835. XCHG(top_border+24, src_cr+1, 1);
  836. }
  837. }
  838. }
  839. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  840. MpegEncContext * const s = &h->s;
  841. const int mb_x= s->mb_x;
  842. const int mb_y= s->mb_y;
  843. const int mb_xy= h->mb_xy;
  844. const int mb_type= s->current_picture.mb_type[mb_xy];
  845. uint8_t *dest_y, *dest_cb, *dest_cr;
  846. int linesize, uvlinesize /*dct_offset*/;
  847. int i;
  848. int *block_offset = &h->block_offset[0];
  849. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  850. /* is_h264 should always be true if SVQ3 is disabled. */
  851. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  852. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  853. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  854. dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
  855. dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
  856. dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
  857. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  858. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  859. h->list_counts[mb_xy]= h->list_count;
  860. if (!simple && MB_FIELD) {
  861. linesize = h->mb_linesize = s->linesize * 2;
  862. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  863. block_offset = &h->block_offset[24];
  864. if(mb_y&1){ //FIXME move out of this function?
  865. dest_y -= s->linesize*15;
  866. dest_cb-= s->uvlinesize*7;
  867. dest_cr-= s->uvlinesize*7;
  868. }
  869. if(FRAME_MBAFF) {
  870. int list;
  871. for(list=0; list<h->list_count; list++){
  872. if(!USES_LIST(mb_type, list))
  873. continue;
  874. if(IS_16X16(mb_type)){
  875. int8_t *ref = &h->ref_cache[list][scan8[0]];
  876. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  877. }else{
  878. for(i=0; i<16; i+=4){
  879. int ref = h->ref_cache[list][scan8[i]];
  880. if(ref >= 0)
  881. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  882. }
  883. }
  884. }
  885. }
  886. } else {
  887. linesize = h->mb_linesize = s->linesize;
  888. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  889. // dct_offset = s->linesize * 16;
  890. }
  891. if (!simple && IS_INTRA_PCM(mb_type)) {
  892. for (i=0; i<16; i++) {
  893. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  894. }
  895. for (i=0; i<8; i++) {
  896. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  897. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  898. }
  899. } else {
  900. if(IS_INTRA(mb_type)){
  901. if(h->deblocking_filter)
  902. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
  903. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  904. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  905. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  906. }
  907. if(IS_INTRA4x4(mb_type)){
  908. if(simple || !s->encoding){
  909. if(IS_8x8DCT(mb_type)){
  910. if(transform_bypass){
  911. idct_dc_add =
  912. idct_add = s->dsp.add_pixels8;
  913. }else{
  914. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  915. idct_add = h->h264dsp.h264_idct8_add;
  916. }
  917. for(i=0; i<16; i+=4){
  918. uint8_t * const ptr= dest_y + block_offset[i];
  919. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  920. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  921. h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
  922. }else{
  923. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  924. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  925. (h->topright_samples_available<<i)&0x4000, linesize);
  926. if(nnz){
  927. if(nnz == 1 && h->mb[i*16])
  928. idct_dc_add(ptr, h->mb + i*16, linesize);
  929. else
  930. idct_add (ptr, h->mb + i*16, linesize);
  931. }
  932. }
  933. }
  934. }else{
  935. if(transform_bypass){
  936. idct_dc_add =
  937. idct_add = s->dsp.add_pixels4;
  938. }else{
  939. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  940. idct_add = h->h264dsp.h264_idct_add;
  941. }
  942. for(i=0; i<16; i++){
  943. uint8_t * const ptr= dest_y + block_offset[i];
  944. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  945. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  946. h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
  947. }else{
  948. uint8_t *topright;
  949. int nnz, tr;
  950. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  951. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  952. assert(mb_y || linesize <= block_offset[i]);
  953. if(!topright_avail){
  954. tr= ptr[3 - linesize]*0x01010101;
  955. topright= (uint8_t*) &tr;
  956. }else
  957. topright= ptr + 4 - linesize;
  958. }else
  959. topright= NULL;
  960. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  961. nnz = h->non_zero_count_cache[ scan8[i] ];
  962. if(nnz){
  963. if(is_h264){
  964. if(nnz == 1 && h->mb[i*16])
  965. idct_dc_add(ptr, h->mb + i*16, linesize);
  966. else
  967. idct_add (ptr, h->mb + i*16, linesize);
  968. }else
  969. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  970. }
  971. }
  972. }
  973. }
  974. }
  975. }else{
  976. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  977. if(is_h264){
  978. if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX] ]){
  979. if(!transform_bypass)
  980. h->h264dsp.h264_luma_dc_dequant_idct(h->mb, h->mb_luma_dc, h->dequant4_coeff[0][s->qscale][0]);
  981. else{
  982. static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
  983. 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
  984. for(i = 0; i < 16; i++)
  985. h->mb[dc_mapping[i]] = h->mb_luma_dc[i];
  986. }
  987. }
  988. }else
  989. ff_svq3_luma_dc_dequant_idct_c(h->mb, h->mb_luma_dc, s->qscale);
  990. }
  991. if(h->deblocking_filter)
  992. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
  993. }else if(is_h264){
  994. hl_motion(h, dest_y, dest_cb, dest_cr,
  995. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  996. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  997. h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
  998. }
  999. if(!IS_INTRA4x4(mb_type)){
  1000. if(is_h264){
  1001. if(IS_INTRA16x16(mb_type)){
  1002. if(transform_bypass){
  1003. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1004. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  1005. }else{
  1006. for(i=0; i<16; i++){
  1007. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1008. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
  1009. }
  1010. }
  1011. }else{
  1012. h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1013. }
  1014. }else if(h->cbp&15){
  1015. if(transform_bypass){
  1016. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1017. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1018. for(i=0; i<16; i+=di){
  1019. if(h->non_zero_count_cache[ scan8[i] ]){
  1020. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  1021. }
  1022. }
  1023. }else{
  1024. if(IS_8x8DCT(mb_type)){
  1025. h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1026. }else{
  1027. h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1028. }
  1029. }
  1030. }
  1031. }else{
  1032. for(i=0; i<16; i++){
  1033. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1034. uint8_t * const ptr= dest_y + block_offset[i];
  1035. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1036. }
  1037. }
  1038. }
  1039. }
  1040. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1041. uint8_t *dest[2] = {dest_cb, dest_cr};
  1042. if(transform_bypass){
  1043. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1044. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
  1045. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
  1046. }else{
  1047. idct_add = s->dsp.add_pixels4;
  1048. for(i=16; i<16+8; i++){
  1049. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1050. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1051. }
  1052. }
  1053. }else{
  1054. int chroma_qpu = h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0];
  1055. int chroma_qpv = h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0];
  1056. if(is_h264){
  1057. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
  1058. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+0*16, &h->mb_chroma_dc[0], chroma_qpu );
  1059. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
  1060. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, &h->mb_chroma_dc[1], chroma_qpv );
  1061. h->h264dsp.h264_idct_add8(dest, block_offset,
  1062. h->mb, uvlinesize,
  1063. h->non_zero_count_cache);
  1064. }else{
  1065. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+0*16, &h->mb_chroma_dc[0], chroma_qpu );
  1066. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, &h->mb_chroma_dc[1], chroma_qpv );
  1067. for(i=16; i<16+8; i++){
  1068. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1069. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  1070. ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
  1071. }
  1072. }
  1073. }
  1074. }
  1075. }
  1076. }
  1077. if(h->cbp || IS_INTRA(mb_type))
  1078. s->dsp.clear_blocks(h->mb);
  1079. }
  1080. /**
  1081. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1082. */
  1083. static void hl_decode_mb_simple(H264Context *h){
  1084. hl_decode_mb_internal(h, 1);
  1085. }
  1086. /**
  1087. * Process a macroblock; this handles edge cases, such as interlacing.
  1088. */
  1089. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1090. hl_decode_mb_internal(h, 0);
  1091. }
  1092. void ff_h264_hl_decode_mb(H264Context *h){
  1093. MpegEncContext * const s = &h->s;
  1094. const int mb_xy= h->mb_xy;
  1095. const int mb_type= s->current_picture.mb_type[mb_xy];
  1096. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1097. if (is_complex)
  1098. hl_decode_mb_complex(h);
  1099. else hl_decode_mb_simple(h);
  1100. }
  1101. static int pred_weight_table(H264Context *h){
  1102. MpegEncContext * const s = &h->s;
  1103. int list, i;
  1104. int luma_def, chroma_def;
  1105. h->use_weight= 0;
  1106. h->use_weight_chroma= 0;
  1107. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1108. if(CHROMA)
  1109. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1110. luma_def = 1<<h->luma_log2_weight_denom;
  1111. chroma_def = 1<<h->chroma_log2_weight_denom;
  1112. for(list=0; list<2; list++){
  1113. h->luma_weight_flag[list] = 0;
  1114. h->chroma_weight_flag[list] = 0;
  1115. for(i=0; i<h->ref_count[list]; i++){
  1116. int luma_weight_flag, chroma_weight_flag;
  1117. luma_weight_flag= get_bits1(&s->gb);
  1118. if(luma_weight_flag){
  1119. h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
  1120. h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
  1121. if( h->luma_weight[i][list][0] != luma_def
  1122. || h->luma_weight[i][list][1] != 0) {
  1123. h->use_weight= 1;
  1124. h->luma_weight_flag[list]= 1;
  1125. }
  1126. }else{
  1127. h->luma_weight[i][list][0]= luma_def;
  1128. h->luma_weight[i][list][1]= 0;
  1129. }
  1130. if(CHROMA){
  1131. chroma_weight_flag= get_bits1(&s->gb);
  1132. if(chroma_weight_flag){
  1133. int j;
  1134. for(j=0; j<2; j++){
  1135. h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
  1136. h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
  1137. if( h->chroma_weight[i][list][j][0] != chroma_def
  1138. || h->chroma_weight[i][list][j][1] != 0) {
  1139. h->use_weight_chroma= 1;
  1140. h->chroma_weight_flag[list]= 1;
  1141. }
  1142. }
  1143. }else{
  1144. int j;
  1145. for(j=0; j<2; j++){
  1146. h->chroma_weight[i][list][j][0]= chroma_def;
  1147. h->chroma_weight[i][list][j][1]= 0;
  1148. }
  1149. }
  1150. }
  1151. }
  1152. if(h->slice_type_nos != FF_B_TYPE) break;
  1153. }
  1154. h->use_weight= h->use_weight || h->use_weight_chroma;
  1155. return 0;
  1156. }
  1157. /**
  1158. * Initialize implicit_weight table.
  1159. * @param field 0/1 initialize the weight for interlaced MBAFF
  1160. * -1 initializes the rest
  1161. */
  1162. static void implicit_weight_table(H264Context *h, int field){
  1163. MpegEncContext * const s = &h->s;
  1164. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  1165. for (i = 0; i < 2; i++) {
  1166. h->luma_weight_flag[i] = 0;
  1167. h->chroma_weight_flag[i] = 0;
  1168. }
  1169. if(field < 0){
  1170. cur_poc = s->current_picture_ptr->poc;
  1171. if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
  1172. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1173. h->use_weight= 0;
  1174. h->use_weight_chroma= 0;
  1175. return;
  1176. }
  1177. ref_start= 0;
  1178. ref_count0= h->ref_count[0];
  1179. ref_count1= h->ref_count[1];
  1180. }else{
  1181. cur_poc = s->current_picture_ptr->field_poc[field];
  1182. ref_start= 16;
  1183. ref_count0= 16+2*h->ref_count[0];
  1184. ref_count1= 16+2*h->ref_count[1];
  1185. }
  1186. h->use_weight= 2;
  1187. h->use_weight_chroma= 2;
  1188. h->luma_log2_weight_denom= 5;
  1189. h->chroma_log2_weight_denom= 5;
  1190. for(ref0=ref_start; ref0 < ref_count0; ref0++){
  1191. int poc0 = h->ref_list[0][ref0].poc;
  1192. for(ref1=ref_start; ref1 < ref_count1; ref1++){
  1193. int poc1 = h->ref_list[1][ref1].poc;
  1194. int td = av_clip(poc1 - poc0, -128, 127);
  1195. int w= 32;
  1196. if(td){
  1197. int tb = av_clip(cur_poc - poc0, -128, 127);
  1198. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1199. int dist_scale_factor = (tb*tx + 32) >> 8;
  1200. if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
  1201. w = 64 - dist_scale_factor;
  1202. }
  1203. if(field<0){
  1204. h->implicit_weight[ref0][ref1][0]=
  1205. h->implicit_weight[ref0][ref1][1]= w;
  1206. }else{
  1207. h->implicit_weight[ref0][ref1][field]=w;
  1208. }
  1209. }
  1210. }
  1211. }
  1212. /**
  1213. * instantaneous decoder refresh.
  1214. */
  1215. static void idr(H264Context *h){
  1216. ff_h264_remove_all_refs(h);
  1217. h->prev_frame_num= 0;
  1218. h->prev_frame_num_offset= 0;
  1219. h->prev_poc_msb=
  1220. h->prev_poc_lsb= 0;
  1221. }
  1222. /* forget old pics after a seek */
  1223. static void flush_dpb(AVCodecContext *avctx){
  1224. H264Context *h= avctx->priv_data;
  1225. int i;
  1226. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  1227. if(h->delayed_pic[i])
  1228. h->delayed_pic[i]->reference= 0;
  1229. h->delayed_pic[i]= NULL;
  1230. }
  1231. h->outputed_poc= INT_MIN;
  1232. h->prev_interlaced_frame = 1;
  1233. idr(h);
  1234. if(h->s.current_picture_ptr)
  1235. h->s.current_picture_ptr->reference= 0;
  1236. h->s.first_field= 0;
  1237. ff_h264_reset_sei(h);
  1238. ff_mpeg_flush(avctx);
  1239. }
  1240. static int init_poc(H264Context *h){
  1241. MpegEncContext * const s = &h->s;
  1242. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  1243. int field_poc[2];
  1244. Picture *cur = s->current_picture_ptr;
  1245. h->frame_num_offset= h->prev_frame_num_offset;
  1246. if(h->frame_num < h->prev_frame_num)
  1247. h->frame_num_offset += max_frame_num;
  1248. if(h->sps.poc_type==0){
  1249. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  1250. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  1251. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  1252. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  1253. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  1254. else
  1255. h->poc_msb = h->prev_poc_msb;
  1256. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  1257. field_poc[0] =
  1258. field_poc[1] = h->poc_msb + h->poc_lsb;
  1259. if(s->picture_structure == PICT_FRAME)
  1260. field_poc[1] += h->delta_poc_bottom;
  1261. }else if(h->sps.poc_type==1){
  1262. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  1263. int i;
  1264. if(h->sps.poc_cycle_length != 0)
  1265. abs_frame_num = h->frame_num_offset + h->frame_num;
  1266. else
  1267. abs_frame_num = 0;
  1268. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  1269. abs_frame_num--;
  1270. expected_delta_per_poc_cycle = 0;
  1271. for(i=0; i < h->sps.poc_cycle_length; i++)
  1272. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  1273. if(abs_frame_num > 0){
  1274. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  1275. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  1276. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  1277. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  1278. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  1279. } else
  1280. expectedpoc = 0;
  1281. if(h->nal_ref_idc == 0)
  1282. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  1283. field_poc[0] = expectedpoc + h->delta_poc[0];
  1284. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  1285. if(s->picture_structure == PICT_FRAME)
  1286. field_poc[1] += h->delta_poc[1];
  1287. }else{
  1288. int poc= 2*(h->frame_num_offset + h->frame_num);
  1289. if(!h->nal_ref_idc)
  1290. poc--;
  1291. field_poc[0]= poc;
  1292. field_poc[1]= poc;
  1293. }
  1294. if(s->picture_structure != PICT_BOTTOM_FIELD)
  1295. s->current_picture_ptr->field_poc[0]= field_poc[0];
  1296. if(s->picture_structure != PICT_TOP_FIELD)
  1297. s->current_picture_ptr->field_poc[1]= field_poc[1];
  1298. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  1299. return 0;
  1300. }
  1301. /**
  1302. * initialize scan tables
  1303. */
  1304. static void init_scan_tables(H264Context *h){
  1305. int i;
  1306. for(i=0; i<16; i++){
  1307. #define T(x) (x>>2) | ((x<<2) & 0xF)
  1308. h->zigzag_scan[i] = T(zigzag_scan[i]);
  1309. h-> field_scan[i] = T( field_scan[i]);
  1310. #undef T
  1311. }
  1312. for(i=0; i<64; i++){
  1313. #define T(x) (x>>3) | ((x&7)<<3)
  1314. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  1315. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  1316. h->field_scan8x8[i] = T(field_scan8x8[i]);
  1317. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  1318. #undef T
  1319. }
  1320. if(h->sps.transform_bypass){ //FIXME same ugly
  1321. h->zigzag_scan_q0 = zigzag_scan;
  1322. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  1323. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  1324. h->field_scan_q0 = field_scan;
  1325. h->field_scan8x8_q0 = field_scan8x8;
  1326. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  1327. }else{
  1328. h->zigzag_scan_q0 = h->zigzag_scan;
  1329. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  1330. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  1331. h->field_scan_q0 = h->field_scan;
  1332. h->field_scan8x8_q0 = h->field_scan8x8;
  1333. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  1334. }
  1335. }
  1336. static void field_end(H264Context *h){
  1337. MpegEncContext * const s = &h->s;
  1338. AVCodecContext * const avctx= s->avctx;
  1339. s->mb_y= 0;
  1340. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  1341. s->current_picture_ptr->pict_type= s->pict_type;
  1342. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1343. ff_vdpau_h264_set_reference_frames(s);
  1344. if(!s->dropable) {
  1345. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1346. h->prev_poc_msb= h->poc_msb;
  1347. h->prev_poc_lsb= h->poc_lsb;
  1348. }
  1349. h->prev_frame_num_offset= h->frame_num_offset;
  1350. h->prev_frame_num= h->frame_num;
  1351. if (avctx->hwaccel) {
  1352. if (avctx->hwaccel->end_frame(avctx) < 0)
  1353. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1354. }
  1355. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1356. ff_vdpau_h264_picture_complete(s);
  1357. /*
  1358. * FIXME: Error handling code does not seem to support interlaced
  1359. * when slices span multiple rows
  1360. * The ff_er_add_slice calls don't work right for bottom
  1361. * fields; they cause massive erroneous error concealing
  1362. * Error marking covers both fields (top and bottom).
  1363. * This causes a mismatched s->error_count
  1364. * and a bad error table. Further, the error count goes to
  1365. * INT_MAX when called for bottom field, because mb_y is
  1366. * past end by one (callers fault) and resync_mb_y != 0
  1367. * causes problems for the first MB line, too.
  1368. */
  1369. if (!FIELD_PICTURE)
  1370. ff_er_frame_end(s);
  1371. MPV_frame_end(s);
  1372. h->current_slice=0;
  1373. }
  1374. /**
  1375. * Replicate H264 "master" context to thread contexts.
  1376. */
  1377. static void clone_slice(H264Context *dst, H264Context *src)
  1378. {
  1379. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  1380. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  1381. dst->s.current_picture = src->s.current_picture;
  1382. dst->s.linesize = src->s.linesize;
  1383. dst->s.uvlinesize = src->s.uvlinesize;
  1384. dst->s.first_field = src->s.first_field;
  1385. dst->prev_poc_msb = src->prev_poc_msb;
  1386. dst->prev_poc_lsb = src->prev_poc_lsb;
  1387. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  1388. dst->prev_frame_num = src->prev_frame_num;
  1389. dst->short_ref_count = src->short_ref_count;
  1390. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  1391. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  1392. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  1393. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  1394. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  1395. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  1396. }
  1397. /**
  1398. * decodes a slice header.
  1399. * This will also call MPV_common_init() and frame_start() as needed.
  1400. *
  1401. * @param h h264context
  1402. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  1403. *
  1404. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  1405. */
  1406. static int decode_slice_header(H264Context *h, H264Context *h0){
  1407. MpegEncContext * const s = &h->s;
  1408. MpegEncContext * const s0 = &h0->s;
  1409. unsigned int first_mb_in_slice;
  1410. unsigned int pps_id;
  1411. int num_ref_idx_active_override_flag;
  1412. unsigned int slice_type, tmp, i, j;
  1413. int default_ref_list_done = 0;
  1414. int last_pic_structure;
  1415. s->dropable= h->nal_ref_idc == 0;
  1416. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  1417. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  1418. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  1419. }else{
  1420. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  1421. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  1422. }
  1423. first_mb_in_slice= get_ue_golomb(&s->gb);
  1424. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  1425. if(h0->current_slice && FIELD_PICTURE){
  1426. field_end(h);
  1427. }
  1428. h0->current_slice = 0;
  1429. if (!s0->first_field)
  1430. s->current_picture_ptr= NULL;
  1431. }
  1432. slice_type= get_ue_golomb_31(&s->gb);
  1433. if(slice_type > 9){
  1434. 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);
  1435. return -1;
  1436. }
  1437. if(slice_type > 4){
  1438. slice_type -= 5;
  1439. h->slice_type_fixed=1;
  1440. }else
  1441. h->slice_type_fixed=0;
  1442. slice_type= golomb_to_pict_type[ slice_type ];
  1443. if (slice_type == FF_I_TYPE
  1444. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  1445. default_ref_list_done = 1;
  1446. }
  1447. h->slice_type= slice_type;
  1448. h->slice_type_nos= slice_type & 3;
  1449. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  1450. pps_id= get_ue_golomb(&s->gb);
  1451. if(pps_id>=MAX_PPS_COUNT){
  1452. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  1453. return -1;
  1454. }
  1455. if(!h0->pps_buffers[pps_id]) {
  1456. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  1457. return -1;
  1458. }
  1459. h->pps= *h0->pps_buffers[pps_id];
  1460. if(!h0->sps_buffers[h->pps.sps_id]) {
  1461. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  1462. return -1;
  1463. }
  1464. h->sps = *h0->sps_buffers[h->pps.sps_id];
  1465. s->avctx->profile = h->sps.profile_idc;
  1466. s->avctx->level = h->sps.level_idc;
  1467. s->avctx->refs = h->sps.ref_frame_count;
  1468. if(h == h0 && h->dequant_coeff_pps != pps_id){
  1469. h->dequant_coeff_pps = pps_id;
  1470. init_dequant_tables(h);
  1471. }
  1472. s->mb_width= h->sps.mb_width;
  1473. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  1474. h->b_stride= s->mb_width*4;
  1475. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  1476. if(h->sps.frame_mbs_only_flag)
  1477. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  1478. else
  1479. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 7);
  1480. if (s->context_initialized
  1481. && ( s->width != s->avctx->width || s->height != s->avctx->height
  1482. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
  1483. if(h != h0)
  1484. return -1; // width / height changed during parallelized decoding
  1485. free_tables(h);
  1486. flush_dpb(s->avctx);
  1487. MPV_common_end(s);
  1488. }
  1489. if (!s->context_initialized) {
  1490. if(h != h0)
  1491. return -1; // we cant (re-)initialize context during parallel decoding
  1492. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1493. s->avctx->sample_aspect_ratio= h->sps.sar;
  1494. av_assert0(s->avctx->sample_aspect_ratio.den);
  1495. if(h->sps.video_signal_type_present_flag){
  1496. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  1497. if(h->sps.colour_description_present_flag){
  1498. s->avctx->color_primaries = h->sps.color_primaries;
  1499. s->avctx->color_trc = h->sps.color_trc;
  1500. s->avctx->colorspace = h->sps.colorspace;
  1501. }
  1502. }
  1503. if(h->sps.timing_info_present_flag){
  1504. int64_t den= h->sps.time_scale;
  1505. if(h->x264_build < 44U)
  1506. den *= 2;
  1507. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  1508. h->sps.num_units_in_tick, den, 1<<30);
  1509. }
  1510. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  1511. s->avctx->codec->pix_fmts ?
  1512. s->avctx->codec->pix_fmts :
  1513. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  1514. hwaccel_pixfmt_list_h264_jpeg_420 :
  1515. ff_hwaccel_pixfmt_list_420);
  1516. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  1517. if (MPV_common_init(s) < 0)
  1518. return -1;
  1519. s->first_field = 0;
  1520. h->prev_interlaced_frame = 1;
  1521. init_scan_tables(h);
  1522. ff_h264_alloc_tables(h);
  1523. for(i = 1; i < s->avctx->thread_count; i++) {
  1524. H264Context *c;
  1525. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  1526. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  1527. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  1528. c->h264dsp = h->h264dsp;
  1529. c->sps = h->sps;
  1530. c->pps = h->pps;
  1531. init_scan_tables(c);
  1532. clone_tables(c, h, i);
  1533. }
  1534. for(i = 0; i < s->avctx->thread_count; i++)
  1535. if(context_init(h->thread_context[i]) < 0)
  1536. return -1;
  1537. }
  1538. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  1539. h->mb_mbaff = 0;
  1540. h->mb_aff_frame = 0;
  1541. last_pic_structure = s0->picture_structure;
  1542. if(h->sps.frame_mbs_only_flag){
  1543. s->picture_structure= PICT_FRAME;
  1544. }else{
  1545. if(get_bits1(&s->gb)) { //field_pic_flag
  1546. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  1547. } else {
  1548. s->picture_structure= PICT_FRAME;
  1549. h->mb_aff_frame = h->sps.mb_aff;
  1550. }
  1551. }
  1552. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  1553. if(h0->current_slice == 0){
  1554. while(h->frame_num != h->prev_frame_num &&
  1555. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  1556. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1557. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  1558. if (ff_h264_frame_start(h) < 0)
  1559. return -1;
  1560. h->prev_frame_num++;
  1561. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  1562. s->current_picture_ptr->frame_num= h->prev_frame_num;
  1563. ff_generate_sliding_window_mmcos(h);
  1564. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1565. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  1566. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  1567. * about there being no actual duplicates.
  1568. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  1569. * concealing a lost frame, this probably isn't noticable by comparison, but it should
  1570. * be fixed. */
  1571. if (h->short_ref_count) {
  1572. if (prev) {
  1573. av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
  1574. (const uint8_t**)prev->data, prev->linesize,
  1575. s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
  1576. h->short_ref[0]->poc = prev->poc+2;
  1577. }
  1578. h->short_ref[0]->frame_num = h->prev_frame_num;
  1579. }
  1580. }
  1581. /* See if we have a decoded first field looking for a pair... */
  1582. if (s0->first_field) {
  1583. assert(s0->current_picture_ptr);
  1584. assert(s0->current_picture_ptr->data[0]);
  1585. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  1586. /* figure out if we have a complementary field pair */
  1587. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  1588. /*
  1589. * Previous field is unmatched. Don't display it, but let it
  1590. * remain for reference if marked as such.
  1591. */
  1592. s0->current_picture_ptr = NULL;
  1593. s0->first_field = FIELD_PICTURE;
  1594. } else {
  1595. if (h->nal_ref_idc &&
  1596. s0->current_picture_ptr->reference &&
  1597. s0->current_picture_ptr->frame_num != h->frame_num) {
  1598. /*
  1599. * This and previous field were reference, but had
  1600. * different frame_nums. Consider this field first in
  1601. * pair. Throw away previous field except for reference
  1602. * purposes.
  1603. */
  1604. s0->first_field = 1;
  1605. s0->current_picture_ptr = NULL;
  1606. } else {
  1607. /* Second field in complementary pair */
  1608. s0->first_field = 0;
  1609. }
  1610. }
  1611. } else {
  1612. /* Frame or first field in a potentially complementary pair */
  1613. assert(!s0->current_picture_ptr);
  1614. s0->first_field = FIELD_PICTURE;
  1615. }
  1616. if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
  1617. s0->first_field = 0;
  1618. return -1;
  1619. }
  1620. }
  1621. if(h != h0)
  1622. clone_slice(h, h0);
  1623. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  1624. assert(s->mb_num == s->mb_width * s->mb_height);
  1625. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  1626. first_mb_in_slice >= s->mb_num){
  1627. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1628. return -1;
  1629. }
  1630. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  1631. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  1632. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1633. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  1634. assert(s->mb_y < s->mb_height);
  1635. if(s->picture_structure==PICT_FRAME){
  1636. h->curr_pic_num= h->frame_num;
  1637. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  1638. }else{
  1639. h->curr_pic_num= 2*h->frame_num + 1;
  1640. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  1641. }
  1642. if(h->nal_unit_type == NAL_IDR_SLICE){
  1643. get_ue_golomb(&s->gb); /* idr_pic_id */
  1644. }
  1645. if(h->sps.poc_type==0){
  1646. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  1647. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  1648. h->delta_poc_bottom= get_se_golomb(&s->gb);
  1649. }
  1650. }
  1651. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  1652. h->delta_poc[0]= get_se_golomb(&s->gb);
  1653. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  1654. h->delta_poc[1]= get_se_golomb(&s->gb);
  1655. }
  1656. init_poc(h);
  1657. if(h->pps.redundant_pic_cnt_present){
  1658. h->redundant_pic_count= get_ue_golomb(&s->gb);
  1659. }
  1660. //set defaults, might be overridden a few lines later
  1661. h->ref_count[0]= h->pps.ref_count[0];
  1662. h->ref_count[1]= h->pps.ref_count[1];
  1663. if(h->slice_type_nos != FF_I_TYPE){
  1664. if(h->slice_type_nos == FF_B_TYPE){
  1665. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  1666. }
  1667. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  1668. if(num_ref_idx_active_override_flag){
  1669. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  1670. if(h->slice_type_nos==FF_B_TYPE)
  1671. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  1672. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  1673. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  1674. h->ref_count[0]= h->ref_count[1]= 1;
  1675. return -1;
  1676. }
  1677. }
  1678. if(h->slice_type_nos == FF_B_TYPE)
  1679. h->list_count= 2;
  1680. else
  1681. h->list_count= 1;
  1682. }else
  1683. h->list_count= 0;
  1684. if(!default_ref_list_done){
  1685. ff_h264_fill_default_ref_list(h);
  1686. }
  1687. if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  1688. return -1;
  1689. if(h->slice_type_nos!=FF_I_TYPE){
  1690. s->last_picture_ptr= &h->ref_list[0][0];
  1691. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1692. }
  1693. if(h->slice_type_nos==FF_B_TYPE){
  1694. s->next_picture_ptr= &h->ref_list[1][0];
  1695. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1696. }
  1697. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  1698. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  1699. pred_weight_table(h);
  1700. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
  1701. implicit_weight_table(h, -1);
  1702. }else {
  1703. h->use_weight = 0;
  1704. for (i = 0; i < 2; i++) {
  1705. h->luma_weight_flag[i] = 0;
  1706. h->chroma_weight_flag[i] = 0;
  1707. }
  1708. }
  1709. if(h->nal_ref_idc)
  1710. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  1711. if(FRAME_MBAFF){
  1712. ff_h264_fill_mbaff_ref_list(h);
  1713. if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
  1714. implicit_weight_table(h, 0);
  1715. implicit_weight_table(h, 1);
  1716. }
  1717. }
  1718. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  1719. ff_h264_direct_dist_scale_factor(h);
  1720. ff_h264_direct_ref_list_init(h);
  1721. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  1722. tmp = get_ue_golomb_31(&s->gb);
  1723. if(tmp > 2){
  1724. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  1725. return -1;
  1726. }
  1727. h->cabac_init_idc= tmp;
  1728. }
  1729. h->last_qscale_diff = 0;
  1730. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  1731. if(tmp>51){
  1732. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1733. return -1;
  1734. }
  1735. s->qscale= tmp;
  1736. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  1737. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  1738. //FIXME qscale / qp ... stuff
  1739. if(h->slice_type == FF_SP_TYPE){
  1740. get_bits1(&s->gb); /* sp_for_switch_flag */
  1741. }
  1742. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  1743. get_se_golomb(&s->gb); /* slice_qs_delta */
  1744. }
  1745. h->deblocking_filter = 1;
  1746. h->slice_alpha_c0_offset = 52;
  1747. h->slice_beta_offset = 52;
  1748. if( h->pps.deblocking_filter_parameters_present ) {
  1749. tmp= get_ue_golomb_31(&s->gb);
  1750. if(tmp > 2){
  1751. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  1752. return -1;
  1753. }
  1754. h->deblocking_filter= tmp;
  1755. if(h->deblocking_filter < 2)
  1756. h->deblocking_filter^= 1; // 1<->0
  1757. if( h->deblocking_filter ) {
  1758. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  1759. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  1760. if( h->slice_alpha_c0_offset > 104U
  1761. || h->slice_beta_offset > 104U){
  1762. 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);
  1763. return -1;
  1764. }
  1765. }
  1766. }
  1767. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  1768. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  1769. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  1770. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  1771. h->deblocking_filter= 0;
  1772. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  1773. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  1774. /* Cheat slightly for speed:
  1775. Do not bother to deblock across slices. */
  1776. h->deblocking_filter = 2;
  1777. } else {
  1778. h0->max_contexts = 1;
  1779. if(!h0->single_decode_warning) {
  1780. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  1781. h0->single_decode_warning = 1;
  1782. }
  1783. if(h != h0)
  1784. return 1; // deblocking switched inside frame
  1785. }
  1786. }
  1787. 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]);
  1788. #if 0 //FMO
  1789. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  1790. slice_group_change_cycle= get_bits(&s->gb, ?);
  1791. #endif
  1792. h0->last_slice_type = slice_type;
  1793. h->slice_num = ++h0->current_slice;
  1794. if(h->slice_num >= MAX_SLICES){
  1795. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  1796. }
  1797. for(j=0; j<2; j++){
  1798. int id_list[16];
  1799. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  1800. for(i=0; i<16; i++){
  1801. id_list[i]= 60;
  1802. if(h->ref_list[j][i].data[0]){
  1803. int k;
  1804. uint8_t *base= h->ref_list[j][i].base[0];
  1805. for(k=0; k<h->short_ref_count; k++)
  1806. if(h->short_ref[k]->base[0] == base){
  1807. id_list[i]= k;
  1808. break;
  1809. }
  1810. for(k=0; k<h->long_ref_count; k++)
  1811. if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
  1812. id_list[i]= h->short_ref_count + k;
  1813. break;
  1814. }
  1815. }
  1816. }
  1817. ref2frm[0]=
  1818. ref2frm[1]= -1;
  1819. for(i=0; i<16; i++)
  1820. ref2frm[i+2]= 4*id_list[i]
  1821. +(h->ref_list[j][i].reference&3);
  1822. ref2frm[18+0]=
  1823. ref2frm[18+1]= -1;
  1824. for(i=16; i<48; i++)
  1825. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  1826. +(h->ref_list[j][i].reference&3);
  1827. }
  1828. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  1829. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  1830. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1831. 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",
  1832. h->slice_num,
  1833. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  1834. first_mb_in_slice,
  1835. av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  1836. pps_id, h->frame_num,
  1837. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  1838. h->ref_count[0], h->ref_count[1],
  1839. s->qscale,
  1840. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  1841. h->use_weight,
  1842. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  1843. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  1844. );
  1845. }
  1846. return 0;
  1847. }
  1848. int ff_h264_get_slice_type(const H264Context *h)
  1849. {
  1850. switch (h->slice_type) {
  1851. case FF_P_TYPE: return 0;
  1852. case FF_B_TYPE: return 1;
  1853. case FF_I_TYPE: return 2;
  1854. case FF_SP_TYPE: return 3;
  1855. case FF_SI_TYPE: return 4;
  1856. default: return -1;
  1857. }
  1858. }
  1859. /**
  1860. *
  1861. * @return non zero if the loop filter can be skiped
  1862. */
  1863. static int fill_filter_caches(H264Context *h, int mb_type){
  1864. MpegEncContext * const s = &h->s;
  1865. const int mb_xy= h->mb_xy;
  1866. int top_xy, left_xy[2];
  1867. int top_type, left_type[2];
  1868. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  1869. //FIXME deblocking could skip the intra and nnz parts.
  1870. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  1871. * stuff, I can't imagine that these complex rules are worth it. */
  1872. left_xy[1] = left_xy[0] = mb_xy-1;
  1873. if(FRAME_MBAFF){
  1874. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
  1875. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  1876. if(s->mb_y&1){
  1877. if (left_mb_field_flag != curr_mb_field_flag) {
  1878. left_xy[0] -= s->mb_stride;
  1879. }
  1880. }else{
  1881. if(curr_mb_field_flag){
  1882. top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
  1883. }
  1884. if (left_mb_field_flag != curr_mb_field_flag) {
  1885. left_xy[1] += s->mb_stride;
  1886. }
  1887. }
  1888. }
  1889. h->top_mb_xy = top_xy;
  1890. h->left_mb_xy[0] = left_xy[0];
  1891. h->left_mb_xy[1] = left_xy[1];
  1892. {
  1893. //for sufficiently low qp, filtering wouldn't do anything
  1894. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  1895. int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
  1896. int qp = s->current_picture.qscale_table[mb_xy];
  1897. if(qp <= qp_thresh
  1898. && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
  1899. && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
  1900. if(!FRAME_MBAFF)
  1901. return 1;
  1902. if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
  1903. && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
  1904. return 1;
  1905. }
  1906. }
  1907. top_type = s->current_picture.mb_type[top_xy] ;
  1908. left_type[0] = s->current_picture.mb_type[left_xy[0]];
  1909. left_type[1] = s->current_picture.mb_type[left_xy[1]];
  1910. if(h->deblocking_filter == 2){
  1911. if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
  1912. if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
  1913. }else{
  1914. if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
  1915. if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
  1916. }
  1917. h->top_type = top_type ;
  1918. h->left_type[0]= left_type[0];
  1919. h->left_type[1]= left_type[1];
  1920. if(IS_INTRA(mb_type))
  1921. return 0;
  1922. AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
  1923. AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
  1924. AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
  1925. AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
  1926. AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
  1927. h->cbp= h->cbp_table[mb_xy];
  1928. {
  1929. int list;
  1930. for(list=0; list<h->list_count; list++){
  1931. int8_t *ref;
  1932. int y, b_stride;
  1933. int16_t (*mv_dst)[2];
  1934. int16_t (*mv_src)[2];
  1935. if(!USES_LIST(mb_type, list)){
  1936. fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
  1937. AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  1938. AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  1939. AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  1940. AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  1941. continue;
  1942. }
  1943. ref = &s->current_picture.ref_index[list][4*mb_xy];
  1944. {
  1945. int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  1946. AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  1947. AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  1948. ref += 2;
  1949. AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  1950. AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  1951. }
  1952. b_stride = h->b_stride;
  1953. mv_dst = &h->mv_cache[list][scan8[0]];
  1954. mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
  1955. for(y=0; y<4; y++){
  1956. AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
  1957. }
  1958. }
  1959. }
  1960. /*
  1961. 0 . T T. T T T T
  1962. 1 L . .L . . . .
  1963. 2 L . .L . . . .
  1964. 3 . T TL . . . .
  1965. 4 L . .L . . . .
  1966. 5 L . .. . . . .
  1967. */
  1968. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  1969. if(top_type){
  1970. AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
  1971. }
  1972. if(left_type[0]){
  1973. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
  1974. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
  1975. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
  1976. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
  1977. }
  1978. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  1979. if(!CABAC && h->pps.transform_8x8_mode){
  1980. if(IS_8x8DCT(top_type)){
  1981. h->non_zero_count_cache[4+8*0]=
  1982. h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
  1983. h->non_zero_count_cache[6+8*0]=
  1984. h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
  1985. }
  1986. if(IS_8x8DCT(left_type[0])){
  1987. h->non_zero_count_cache[3+8*1]=
  1988. h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2; //FIXME check MBAFF
  1989. }
  1990. if(IS_8x8DCT(left_type[1])){
  1991. h->non_zero_count_cache[3+8*3]=
  1992. h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8; //FIXME check MBAFF
  1993. }
  1994. if(IS_8x8DCT(mb_type)){
  1995. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  1996. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
  1997. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  1998. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
  1999. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  2000. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
  2001. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  2002. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
  2003. }
  2004. }
  2005. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  2006. int list;
  2007. for(list=0; list<h->list_count; list++){
  2008. if(USES_LIST(top_type, list)){
  2009. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  2010. const int b8_xy= 4*top_xy + 2;
  2011. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2012. AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
  2013. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  2014. h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
  2015. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  2016. h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
  2017. }else{
  2018. AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
  2019. AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2020. }
  2021. if(!IS_INTERLACED(mb_type^left_type[0])){
  2022. if(USES_LIST(left_type[0], list)){
  2023. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  2024. const int b8_xy= 4*left_xy[0] + 1;
  2025. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2026. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
  2027. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
  2028. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
  2029. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
  2030. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2031. h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
  2032. h->ref_cache[list][scan8[0] - 1 +16 ]=
  2033. h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
  2034. }else{
  2035. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
  2036. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
  2037. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
  2038. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
  2039. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2040. h->ref_cache[list][scan8[0] - 1 + 8 ]=
  2041. h->ref_cache[list][scan8[0] - 1 + 16 ]=
  2042. h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
  2043. }
  2044. }
  2045. }
  2046. }
  2047. return 0;
  2048. }
  2049. static void loop_filter(H264Context *h){
  2050. MpegEncContext * const s = &h->s;
  2051. uint8_t *dest_y, *dest_cb, *dest_cr;
  2052. int linesize, uvlinesize, mb_x, mb_y;
  2053. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  2054. const int old_slice_type= h->slice_type;
  2055. if(h->deblocking_filter) {
  2056. for(mb_x= 0; mb_x<s->mb_width; mb_x++){
  2057. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  2058. int mb_xy, mb_type;
  2059. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  2060. h->slice_num= h->slice_table[mb_xy];
  2061. mb_type= s->current_picture.mb_type[mb_xy];
  2062. h->list_count= h->list_counts[mb_xy];
  2063. if(FRAME_MBAFF)
  2064. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2065. s->mb_x= mb_x;
  2066. s->mb_y= mb_y;
  2067. dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
  2068. dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
  2069. dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
  2070. //FIXME simplify above
  2071. if (MB_FIELD) {
  2072. linesize = h->mb_linesize = s->linesize * 2;
  2073. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2074. if(mb_y&1){ //FIXME move out of this function?
  2075. dest_y -= s->linesize*15;
  2076. dest_cb-= s->uvlinesize*7;
  2077. dest_cr-= s->uvlinesize*7;
  2078. }
  2079. } else {
  2080. linesize = h->mb_linesize = s->linesize;
  2081. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2082. }
  2083. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2084. if(fill_filter_caches(h, mb_type))
  2085. continue;
  2086. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2087. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2088. if (FRAME_MBAFF) {
  2089. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2090. } else {
  2091. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2092. }
  2093. }
  2094. }
  2095. }
  2096. h->slice_type= old_slice_type;
  2097. s->mb_x= 0;
  2098. s->mb_y= end_mb_y - FRAME_MBAFF;
  2099. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2100. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2101. }
  2102. static void predict_field_decoding_flag(H264Context *h){
  2103. MpegEncContext * const s = &h->s;
  2104. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2105. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2106. ? s->current_picture.mb_type[mb_xy-1]
  2107. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2108. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  2109. : 0;
  2110. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2111. }
  2112. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  2113. H264Context *h = *(void**)arg;
  2114. MpegEncContext * const s = &h->s;
  2115. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  2116. s->mb_skip_run= -1;
  2117. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  2118. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  2119. if( h->pps.cabac ) {
  2120. /* realign */
  2121. align_get_bits( &s->gb );
  2122. /* init cabac */
  2123. ff_init_cabac_states( &h->cabac);
  2124. ff_init_cabac_decoder( &h->cabac,
  2125. s->gb.buffer + get_bits_count(&s->gb)/8,
  2126. (get_bits_left(&s->gb) + 7)/8);
  2127. ff_h264_init_cabac_states(h);
  2128. for(;;){
  2129. //START_TIMER
  2130. int ret = ff_h264_decode_mb_cabac(h);
  2131. int eos;
  2132. //STOP_TIMER("decode_mb_cabac")
  2133. if(ret>=0) ff_h264_hl_decode_mb(h);
  2134. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  2135. s->mb_y++;
  2136. ret = ff_h264_decode_mb_cabac(h);
  2137. if(ret>=0) ff_h264_hl_decode_mb(h);
  2138. s->mb_y--;
  2139. }
  2140. eos = get_cabac_terminate( &h->cabac );
  2141. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  2142. 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);
  2143. return 0;
  2144. }
  2145. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  2146. 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);
  2147. 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);
  2148. return -1;
  2149. }
  2150. if( ++s->mb_x >= s->mb_width ) {
  2151. s->mb_x = 0;
  2152. loop_filter(h);
  2153. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2154. ++s->mb_y;
  2155. if(FIELD_OR_MBAFF_PICTURE) {
  2156. ++s->mb_y;
  2157. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2158. predict_field_decoding_flag(h);
  2159. }
  2160. }
  2161. if( eos || s->mb_y >= s->mb_height ) {
  2162. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2163. 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);
  2164. return 0;
  2165. }
  2166. }
  2167. } else {
  2168. for(;;){
  2169. int ret = ff_h264_decode_mb_cavlc(h);
  2170. if(ret>=0) ff_h264_hl_decode_mb(h);
  2171. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  2172. s->mb_y++;
  2173. ret = ff_h264_decode_mb_cavlc(h);
  2174. if(ret>=0) ff_h264_hl_decode_mb(h);
  2175. s->mb_y--;
  2176. }
  2177. if(ret<0){
  2178. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2179. 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);
  2180. return -1;
  2181. }
  2182. if(++s->mb_x >= s->mb_width){
  2183. s->mb_x=0;
  2184. loop_filter(h);
  2185. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2186. ++s->mb_y;
  2187. if(FIELD_OR_MBAFF_PICTURE) {
  2188. ++s->mb_y;
  2189. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2190. predict_field_decoding_flag(h);
  2191. }
  2192. if(s->mb_y >= s->mb_height){
  2193. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2194. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  2195. 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);
  2196. return 0;
  2197. }else{
  2198. 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);
  2199. return -1;
  2200. }
  2201. }
  2202. }
  2203. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  2204. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2205. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  2206. 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);
  2207. return 0;
  2208. }else{
  2209. 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);
  2210. return -1;
  2211. }
  2212. }
  2213. }
  2214. }
  2215. #if 0
  2216. for(;s->mb_y < s->mb_height; s->mb_y++){
  2217. for(;s->mb_x < s->mb_width; s->mb_x++){
  2218. int ret= decode_mb(h);
  2219. ff_h264_hl_decode_mb(h);
  2220. if(ret<0){
  2221. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2222. 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);
  2223. return -1;
  2224. }
  2225. if(++s->mb_x >= s->mb_width){
  2226. s->mb_x=0;
  2227. if(++s->mb_y >= s->mb_height){
  2228. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2229. 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);
  2230. return 0;
  2231. }else{
  2232. 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);
  2233. return -1;
  2234. }
  2235. }
  2236. }
  2237. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  2238. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2239. 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);
  2240. return 0;
  2241. }else{
  2242. 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);
  2243. return -1;
  2244. }
  2245. }
  2246. }
  2247. s->mb_x=0;
  2248. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2249. }
  2250. #endif
  2251. return -1; //not reached
  2252. }
  2253. /**
  2254. * Call decode_slice() for each context.
  2255. *
  2256. * @param h h264 master context
  2257. * @param context_count number of contexts to execute
  2258. */
  2259. static void execute_decode_slices(H264Context *h, int context_count){
  2260. MpegEncContext * const s = &h->s;
  2261. AVCodecContext * const avctx= s->avctx;
  2262. H264Context *hx;
  2263. int i;
  2264. if (s->avctx->hwaccel)
  2265. return;
  2266. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2267. return;
  2268. if(context_count == 1) {
  2269. decode_slice(avctx, &h);
  2270. } else {
  2271. for(i = 1; i < context_count; i++) {
  2272. hx = h->thread_context[i];
  2273. hx->s.error_recognition = avctx->error_recognition;
  2274. hx->s.error_count = 0;
  2275. }
  2276. avctx->execute(avctx, (void *)decode_slice,
  2277. h->thread_context, NULL, context_count, sizeof(void*));
  2278. /* pull back stuff from slices to master context */
  2279. hx = h->thread_context[context_count - 1];
  2280. s->mb_x = hx->s.mb_x;
  2281. s->mb_y = hx->s.mb_y;
  2282. s->dropable = hx->s.dropable;
  2283. s->picture_structure = hx->s.picture_structure;
  2284. for(i = 1; i < context_count; i++)
  2285. h->s.error_count += h->thread_context[i]->s.error_count;
  2286. }
  2287. }
  2288. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  2289. MpegEncContext * const s = &h->s;
  2290. AVCodecContext * const avctx= s->avctx;
  2291. int buf_index=0;
  2292. H264Context *hx; ///< thread context
  2293. int context_count = 0;
  2294. int next_avc= h->is_avc ? 0 : buf_size;
  2295. h->max_contexts = avctx->thread_count;
  2296. #if 0
  2297. int i;
  2298. for(i=0; i<50; i++){
  2299. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  2300. }
  2301. #endif
  2302. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  2303. h->current_slice = 0;
  2304. if (!s->first_field)
  2305. s->current_picture_ptr= NULL;
  2306. ff_h264_reset_sei(h);
  2307. }
  2308. for(;;){
  2309. int consumed;
  2310. int dst_length;
  2311. int bit_length;
  2312. const uint8_t *ptr;
  2313. int i, nalsize = 0;
  2314. int err;
  2315. if(buf_index >= next_avc) {
  2316. if(buf_index >= buf_size) break;
  2317. nalsize = 0;
  2318. for(i = 0; i < h->nal_length_size; i++)
  2319. nalsize = (nalsize << 8) | buf[buf_index++];
  2320. if(nalsize <= 0 || nalsize > buf_size - buf_index){
  2321. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  2322. break;
  2323. }
  2324. next_avc= buf_index + nalsize;
  2325. } else {
  2326. // start code prefix search
  2327. for(; buf_index + 3 < next_avc; buf_index++){
  2328. // This should always succeed in the first iteration.
  2329. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  2330. break;
  2331. }
  2332. if(buf_index+3 >= buf_size) break;
  2333. buf_index+=3;
  2334. if(buf_index >= next_avc) continue;
  2335. }
  2336. hx = h->thread_context[context_count];
  2337. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  2338. if (ptr==NULL || dst_length < 0){
  2339. return -1;
  2340. }
  2341. i= buf_index + consumed;
  2342. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  2343. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  2344. s->workaround_bugs |= FF_BUG_TRUNCATED;
  2345. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  2346. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  2347. dst_length--;
  2348. }
  2349. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  2350. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  2351. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
  2352. }
  2353. if (h->is_avc && (nalsize != consumed) && nalsize){
  2354. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  2355. }
  2356. buf_index += consumed;
  2357. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
  2358. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  2359. continue;
  2360. again:
  2361. err = 0;
  2362. switch(hx->nal_unit_type){
  2363. case NAL_IDR_SLICE:
  2364. if (h->nal_unit_type != NAL_IDR_SLICE) {
  2365. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  2366. return -1;
  2367. }
  2368. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  2369. case NAL_SLICE:
  2370. init_get_bits(&hx->s.gb, ptr, bit_length);
  2371. hx->intra_gb_ptr=
  2372. hx->inter_gb_ptr= &hx->s.gb;
  2373. hx->s.data_partitioning = 0;
  2374. if((err = decode_slice_header(hx, h)))
  2375. break;
  2376. if (h->current_slice == 1) {
  2377. if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  2378. return -1;
  2379. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2380. ff_vdpau_h264_picture_start(s);
  2381. }
  2382. s->current_picture_ptr->key_frame |=
  2383. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  2384. (h->sei_recovery_frame_cnt >= 0);
  2385. if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
  2386. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2387. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2388. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2389. && avctx->skip_frame < AVDISCARD_ALL){
  2390. if(avctx->hwaccel) {
  2391. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  2392. return -1;
  2393. }else
  2394. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  2395. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  2396. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  2397. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  2398. }else
  2399. context_count++;
  2400. }
  2401. break;
  2402. case NAL_DPA:
  2403. init_get_bits(&hx->s.gb, ptr, bit_length);
  2404. hx->intra_gb_ptr=
  2405. hx->inter_gb_ptr= NULL;
  2406. if ((err = decode_slice_header(hx, h)) < 0)
  2407. break;
  2408. hx->s.data_partitioning = 1;
  2409. break;
  2410. case NAL_DPB:
  2411. init_get_bits(&hx->intra_gb, ptr, bit_length);
  2412. hx->intra_gb_ptr= &hx->intra_gb;
  2413. break;
  2414. case NAL_DPC:
  2415. init_get_bits(&hx->inter_gb, ptr, bit_length);
  2416. hx->inter_gb_ptr= &hx->inter_gb;
  2417. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  2418. && s->context_initialized
  2419. && s->hurry_up < 5
  2420. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2421. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2422. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2423. && avctx->skip_frame < AVDISCARD_ALL)
  2424. context_count++;
  2425. break;
  2426. case NAL_SEI:
  2427. init_get_bits(&s->gb, ptr, bit_length);
  2428. ff_h264_decode_sei(h);
  2429. break;
  2430. case NAL_SPS:
  2431. init_get_bits(&s->gb, ptr, bit_length);
  2432. ff_h264_decode_seq_parameter_set(h);
  2433. if(s->flags& CODEC_FLAG_LOW_DELAY)
  2434. s->low_delay=1;
  2435. if(avctx->has_b_frames < 2)
  2436. avctx->has_b_frames= !s->low_delay;
  2437. break;
  2438. case NAL_PPS:
  2439. init_get_bits(&s->gb, ptr, bit_length);
  2440. ff_h264_decode_picture_parameter_set(h, bit_length);
  2441. break;
  2442. case NAL_AUD:
  2443. case NAL_END_SEQUENCE:
  2444. case NAL_END_STREAM:
  2445. case NAL_FILLER_DATA:
  2446. case NAL_SPS_EXT:
  2447. case NAL_AUXILIARY_SLICE:
  2448. break;
  2449. default:
  2450. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  2451. }
  2452. if(context_count == h->max_contexts) {
  2453. execute_decode_slices(h, context_count);
  2454. context_count = 0;
  2455. }
  2456. if (err < 0)
  2457. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  2458. else if(err == 1) {
  2459. /* Slice could not be decoded in parallel mode, copy down
  2460. * NAL unit stuff to context 0 and restart. Note that
  2461. * rbsp_buffer is not transferred, but since we no longer
  2462. * run in parallel mode this should not be an issue. */
  2463. h->nal_unit_type = hx->nal_unit_type;
  2464. h->nal_ref_idc = hx->nal_ref_idc;
  2465. hx = h;
  2466. goto again;
  2467. }
  2468. }
  2469. if(context_count)
  2470. execute_decode_slices(h, context_count);
  2471. return buf_index;
  2472. }
  2473. /**
  2474. * returns the number of bytes consumed for building the current frame
  2475. */
  2476. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  2477. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  2478. if(pos+10>buf_size) pos=buf_size; // oops ;)
  2479. return pos;
  2480. }
  2481. static int decode_frame(AVCodecContext *avctx,
  2482. void *data, int *data_size,
  2483. AVPacket *avpkt)
  2484. {
  2485. const uint8_t *buf = avpkt->data;
  2486. int buf_size = avpkt->size;
  2487. H264Context *h = avctx->priv_data;
  2488. MpegEncContext *s = &h->s;
  2489. AVFrame *pict = data;
  2490. int buf_index;
  2491. s->flags= avctx->flags;
  2492. s->flags2= avctx->flags2;
  2493. /* end of stream, output what is still in the buffers */
  2494. out:
  2495. if (buf_size == 0) {
  2496. Picture *out;
  2497. int i, out_idx;
  2498. //FIXME factorize this with the output code below
  2499. out = h->delayed_pic[0];
  2500. out_idx = 0;
  2501. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2502. if(h->delayed_pic[i]->poc < out->poc){
  2503. out = h->delayed_pic[i];
  2504. out_idx = i;
  2505. }
  2506. for(i=out_idx; h->delayed_pic[i]; i++)
  2507. h->delayed_pic[i] = h->delayed_pic[i+1];
  2508. if(out){
  2509. *data_size = sizeof(AVFrame);
  2510. *pict= *(AVFrame*)out;
  2511. }
  2512. return 0;
  2513. }
  2514. buf_index=decode_nal_units(h, buf, buf_size);
  2515. if(buf_index < 0)
  2516. return -1;
  2517. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  2518. buf_size = 0;
  2519. goto out;
  2520. }
  2521. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  2522. if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
  2523. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  2524. return -1;
  2525. }
  2526. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  2527. Picture *out = s->current_picture_ptr;
  2528. Picture *cur = s->current_picture_ptr;
  2529. int i, pics, out_of_order, out_idx;
  2530. field_end(h);
  2531. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  2532. /* Wait for second field. */
  2533. *data_size = 0;
  2534. } else {
  2535. cur->interlaced_frame = 0;
  2536. cur->repeat_pict = 0;
  2537. /* Signal interlacing information externally. */
  2538. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  2539. if(h->sps.pic_struct_present_flag){
  2540. switch (h->sei_pic_struct)
  2541. {
  2542. case SEI_PIC_STRUCT_FRAME:
  2543. break;
  2544. case SEI_PIC_STRUCT_TOP_FIELD:
  2545. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  2546. cur->interlaced_frame = 1;
  2547. break;
  2548. case SEI_PIC_STRUCT_TOP_BOTTOM:
  2549. case SEI_PIC_STRUCT_BOTTOM_TOP:
  2550. if (FIELD_OR_MBAFF_PICTURE)
  2551. cur->interlaced_frame = 1;
  2552. else
  2553. // try to flag soft telecine progressive
  2554. cur->interlaced_frame = h->prev_interlaced_frame;
  2555. break;
  2556. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  2557. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  2558. // Signal the possibility of telecined film externally (pic_struct 5,6)
  2559. // From these hints, let the applications decide if they apply deinterlacing.
  2560. cur->repeat_pict = 1;
  2561. break;
  2562. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  2563. // Force progressive here, as doubling interlaced frame is a bad idea.
  2564. cur->repeat_pict = 2;
  2565. break;
  2566. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  2567. cur->repeat_pict = 4;
  2568. break;
  2569. }
  2570. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  2571. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  2572. }else{
  2573. /* Derive interlacing flag from used decoding process. */
  2574. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  2575. }
  2576. h->prev_interlaced_frame = cur->interlaced_frame;
  2577. if (cur->field_poc[0] != cur->field_poc[1]){
  2578. /* Derive top_field_first from field pocs. */
  2579. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  2580. }else{
  2581. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  2582. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  2583. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  2584. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  2585. cur->top_field_first = 1;
  2586. else
  2587. cur->top_field_first = 0;
  2588. }else{
  2589. /* Most likely progressive */
  2590. cur->top_field_first = 0;
  2591. }
  2592. }
  2593. //FIXME do something with unavailable reference frames
  2594. /* Sort B-frames into display order */
  2595. if(h->sps.bitstream_restriction_flag
  2596. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  2597. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  2598. s->low_delay = 0;
  2599. }
  2600. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  2601. && !h->sps.bitstream_restriction_flag){
  2602. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  2603. s->low_delay= 0;
  2604. }
  2605. pics = 0;
  2606. while(h->delayed_pic[pics]) pics++;
  2607. assert(pics <= MAX_DELAYED_PIC_COUNT);
  2608. h->delayed_pic[pics++] = cur;
  2609. if(cur->reference == 0)
  2610. cur->reference = DELAYED_PIC_REF;
  2611. out = h->delayed_pic[0];
  2612. out_idx = 0;
  2613. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2614. if(h->delayed_pic[i]->poc < out->poc){
  2615. out = h->delayed_pic[i];
  2616. out_idx = i;
  2617. }
  2618. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  2619. h->outputed_poc= INT_MIN;
  2620. out_of_order = out->poc < h->outputed_poc;
  2621. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  2622. { }
  2623. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  2624. || (s->low_delay &&
  2625. ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
  2626. || cur->pict_type == FF_B_TYPE)))
  2627. {
  2628. s->low_delay = 0;
  2629. s->avctx->has_b_frames++;
  2630. }
  2631. if(out_of_order || pics > s->avctx->has_b_frames){
  2632. out->reference &= ~DELAYED_PIC_REF;
  2633. for(i=out_idx; h->delayed_pic[i]; i++)
  2634. h->delayed_pic[i] = h->delayed_pic[i+1];
  2635. }
  2636. if(!out_of_order && pics > s->avctx->has_b_frames){
  2637. *data_size = sizeof(AVFrame);
  2638. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  2639. h->outputed_poc = INT_MIN;
  2640. } else
  2641. h->outputed_poc = out->poc;
  2642. *pict= *(AVFrame*)out;
  2643. }else{
  2644. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  2645. }
  2646. }
  2647. }
  2648. assert(pict->data[0] || !*data_size);
  2649. ff_print_debug_info(s, pict);
  2650. //printf("out %d\n", (int)pict->data[0]);
  2651. return get_consumed_bytes(s, buf_index, buf_size);
  2652. }
  2653. #if 0
  2654. static inline void fill_mb_avail(H264Context *h){
  2655. MpegEncContext * const s = &h->s;
  2656. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2657. if(s->mb_y){
  2658. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  2659. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  2660. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  2661. }else{
  2662. h->mb_avail[0]=
  2663. h->mb_avail[1]=
  2664. h->mb_avail[2]= 0;
  2665. }
  2666. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  2667. h->mb_avail[4]= 1; //FIXME move out
  2668. h->mb_avail[5]= 0; //FIXME move out
  2669. }
  2670. #endif
  2671. #ifdef TEST
  2672. #undef printf
  2673. #undef random
  2674. #define COUNT 8000
  2675. #define SIZE (COUNT*40)
  2676. int main(void){
  2677. int i;
  2678. uint8_t temp[SIZE];
  2679. PutBitContext pb;
  2680. GetBitContext gb;
  2681. // int int_temp[10000];
  2682. DSPContext dsp;
  2683. AVCodecContext avctx;
  2684. dsputil_init(&dsp, &avctx);
  2685. init_put_bits(&pb, temp, SIZE);
  2686. printf("testing unsigned exp golomb\n");
  2687. for(i=0; i<COUNT; i++){
  2688. START_TIMER
  2689. set_ue_golomb(&pb, i);
  2690. STOP_TIMER("set_ue_golomb");
  2691. }
  2692. flush_put_bits(&pb);
  2693. init_get_bits(&gb, temp, 8*SIZE);
  2694. for(i=0; i<COUNT; i++){
  2695. int j, s;
  2696. s= show_bits(&gb, 24);
  2697. START_TIMER
  2698. j= get_ue_golomb(&gb);
  2699. if(j != i){
  2700. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2701. // return -1;
  2702. }
  2703. STOP_TIMER("get_ue_golomb");
  2704. }
  2705. init_put_bits(&pb, temp, SIZE);
  2706. printf("testing signed exp golomb\n");
  2707. for(i=0; i<COUNT; i++){
  2708. START_TIMER
  2709. set_se_golomb(&pb, i - COUNT/2);
  2710. STOP_TIMER("set_se_golomb");
  2711. }
  2712. flush_put_bits(&pb);
  2713. init_get_bits(&gb, temp, 8*SIZE);
  2714. for(i=0; i<COUNT; i++){
  2715. int j, s;
  2716. s= show_bits(&gb, 24);
  2717. START_TIMER
  2718. j= get_se_golomb(&gb);
  2719. if(j != i - COUNT/2){
  2720. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2721. // return -1;
  2722. }
  2723. STOP_TIMER("get_se_golomb");
  2724. }
  2725. #if 0
  2726. printf("testing 4x4 (I)DCT\n");
  2727. DCTELEM block[16];
  2728. uint8_t src[16], ref[16];
  2729. uint64_t error= 0, max_error=0;
  2730. for(i=0; i<COUNT; i++){
  2731. int j;
  2732. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  2733. for(j=0; j<16; j++){
  2734. ref[j]= random()%255;
  2735. src[j]= random()%255;
  2736. }
  2737. h264_diff_dct_c(block, src, ref, 4);
  2738. //normalize
  2739. for(j=0; j<16; j++){
  2740. // printf("%d ", block[j]);
  2741. block[j]= block[j]*4;
  2742. if(j&1) block[j]= (block[j]*4 + 2)/5;
  2743. if(j&4) block[j]= (block[j]*4 + 2)/5;
  2744. }
  2745. // printf("\n");
  2746. h->h264dsp.h264_idct_add(ref, block, 4);
  2747. /* for(j=0; j<16; j++){
  2748. printf("%d ", ref[j]);
  2749. }
  2750. printf("\n");*/
  2751. for(j=0; j<16; j++){
  2752. int diff= FFABS(src[j] - ref[j]);
  2753. error+= diff*diff;
  2754. max_error= FFMAX(max_error, diff);
  2755. }
  2756. }
  2757. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  2758. printf("testing quantizer\n");
  2759. for(qp=0; qp<52; qp++){
  2760. for(i=0; i<16; i++)
  2761. src1_block[i]= src2_block[i]= random()%255;
  2762. }
  2763. printf("Testing NAL layer\n");
  2764. uint8_t bitstream[COUNT];
  2765. uint8_t nal[COUNT*2];
  2766. H264Context h;
  2767. memset(&h, 0, sizeof(H264Context));
  2768. for(i=0; i<COUNT; i++){
  2769. int zeros= i;
  2770. int nal_length;
  2771. int consumed;
  2772. int out_length;
  2773. uint8_t *out;
  2774. int j;
  2775. for(j=0; j<COUNT; j++){
  2776. bitstream[j]= (random() % 255) + 1;
  2777. }
  2778. for(j=0; j<zeros; j++){
  2779. int pos= random() % COUNT;
  2780. while(bitstream[pos] == 0){
  2781. pos++;
  2782. pos %= COUNT;
  2783. }
  2784. bitstream[pos]=0;
  2785. }
  2786. START_TIMER
  2787. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  2788. if(nal_length<0){
  2789. printf("encoding failed\n");
  2790. return -1;
  2791. }
  2792. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  2793. STOP_TIMER("NAL")
  2794. if(out_length != COUNT){
  2795. printf("incorrect length %d %d\n", out_length, COUNT);
  2796. return -1;
  2797. }
  2798. if(consumed != nal_length){
  2799. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  2800. return -1;
  2801. }
  2802. if(memcmp(bitstream, out, COUNT)){
  2803. printf("mismatch\n");
  2804. return -1;
  2805. }
  2806. }
  2807. #endif
  2808. printf("Testing RBSP\n");
  2809. return 0;
  2810. }
  2811. #endif /* TEST */
  2812. av_cold void ff_h264_free_context(H264Context *h)
  2813. {
  2814. int i;
  2815. free_tables(h); //FIXME cleanup init stuff perhaps
  2816. for(i = 0; i < MAX_SPS_COUNT; i++)
  2817. av_freep(h->sps_buffers + i);
  2818. for(i = 0; i < MAX_PPS_COUNT; i++)
  2819. av_freep(h->pps_buffers + i);
  2820. }
  2821. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  2822. {
  2823. H264Context *h = avctx->priv_data;
  2824. MpegEncContext *s = &h->s;
  2825. ff_h264_free_context(h);
  2826. MPV_common_end(s);
  2827. // memset(h, 0, sizeof(H264Context));
  2828. return 0;
  2829. }
  2830. AVCodec h264_decoder = {
  2831. "h264",
  2832. AVMEDIA_TYPE_VIDEO,
  2833. CODEC_ID_H264,
  2834. sizeof(H264Context),
  2835. ff_h264_decode_init,
  2836. NULL,
  2837. ff_h264_decode_end,
  2838. decode_frame,
  2839. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  2840. .flush= flush_dpb,
  2841. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  2842. };
  2843. #if CONFIG_H264_VDPAU_DECODER
  2844. AVCodec h264_vdpau_decoder = {
  2845. "h264_vdpau",
  2846. AVMEDIA_TYPE_VIDEO,
  2847. CODEC_ID_H264,
  2848. sizeof(H264Context),
  2849. ff_h264_decode_init,
  2850. NULL,
  2851. ff_h264_decode_end,
  2852. decode_frame,
  2853. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  2854. .flush= flush_dpb,
  2855. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  2856. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  2857. };
  2858. #endif