vdpau.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Video Decode and Presentation API for UNIX (VDPAU) is used for
  3. * HW decode acceleration for MPEG-1/2, H.264 and VC-1.
  4. *
  5. * Copyright (c) 2008 NVIDIA
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <limits.h>
  24. #include "avcodec.h"
  25. #include "h264.h"
  26. #include "vc1.h"
  27. #undef NDEBUG
  28. #include <assert.h>
  29. #include "vdpau.h"
  30. #include "vdpau_internal.h"
  31. /**
  32. * \addtogroup VDPAU_Decoding
  33. *
  34. * @{
  35. */
  36. void ff_vdpau_h264_set_reference_frames(MpegEncContext *s)
  37. {
  38. H264Context *h = s->avctx->priv_data;
  39. struct vdpau_render_state *render, *render_ref;
  40. VdpReferenceFrameH264 *rf, *rf2;
  41. Picture *pic;
  42. int i, list, pic_frame_idx;
  43. render = (struct vdpau_render_state *)s->current_picture_ptr->data[0];
  44. assert(render);
  45. rf = &render->info.h264.referenceFrames[0];
  46. #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
  47. for (list = 0; list < 2; ++list) {
  48. Picture **lp = list ? h->long_ref : h->short_ref;
  49. int ls = list ? h->long_ref_count : h->short_ref_count;
  50. for (i = 0; i < ls; ++i) {
  51. pic = lp[i];
  52. if (!pic || !pic->reference)
  53. continue;
  54. pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  55. render_ref = (struct vdpau_render_state *)pic->data[0];
  56. assert(render_ref);
  57. rf2 = &render->info.h264.referenceFrames[0];
  58. while (rf2 != rf) {
  59. if (
  60. (rf2->surface == render_ref->surface)
  61. && (rf2->is_long_term == pic->long_ref)
  62. && (rf2->frame_idx == pic_frame_idx)
  63. )
  64. break;
  65. ++rf2;
  66. }
  67. if (rf2 != rf) {
  68. rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  69. rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  70. continue;
  71. }
  72. if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
  73. continue;
  74. rf->surface = render_ref->surface;
  75. rf->is_long_term = pic->long_ref;
  76. rf->top_is_reference = (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  77. rf->bottom_is_reference = (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  78. rf->field_order_cnt[0] = pic->field_poc[0];
  79. rf->field_order_cnt[1] = pic->field_poc[1];
  80. rf->frame_idx = pic_frame_idx;
  81. ++rf;
  82. }
  83. }
  84. for (; rf < &render->info.h264.referenceFrames[H264_RF_COUNT]; ++rf) {
  85. rf->surface = VDP_INVALID_HANDLE;
  86. rf->is_long_term = 0;
  87. rf->top_is_reference = 0;
  88. rf->bottom_is_reference = 0;
  89. rf->field_order_cnt[0] = 0;
  90. rf->field_order_cnt[1] = 0;
  91. rf->frame_idx = 0;
  92. }
  93. }
  94. void ff_vdpau_add_data_chunk(MpegEncContext *s,
  95. const uint8_t *buf, int buf_size)
  96. {
  97. struct vdpau_render_state *render;
  98. render = (struct vdpau_render_state *)s->current_picture_ptr->data[0];
  99. assert(render);
  100. render->bitstream_buffers= av_fast_realloc(
  101. render->bitstream_buffers,
  102. &render->bitstream_buffers_allocated,
  103. sizeof(*render->bitstream_buffers)*(render->bitstream_buffers_used + 1)
  104. );
  105. render->bitstream_buffers[render->bitstream_buffers_used].struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  106. render->bitstream_buffers[render->bitstream_buffers_used].bitstream = buf;
  107. render->bitstream_buffers[render->bitstream_buffers_used].bitstream_bytes = buf_size;
  108. render->bitstream_buffers_used++;
  109. }
  110. void ff_vdpau_h264_picture_complete(MpegEncContext *s)
  111. {
  112. H264Context *h = s->avctx->priv_data;
  113. struct vdpau_render_state *render;
  114. int i;
  115. render = (struct vdpau_render_state *)s->current_picture_ptr->data[0];
  116. assert(render);
  117. render->info.h264.slice_count = h->slice_num;
  118. if (render->info.h264.slice_count < 1)
  119. return;
  120. for (i = 0; i < 2; ++i) {
  121. int foc = s->current_picture_ptr->field_poc[i];
  122. if (foc == INT_MAX)
  123. foc = 0;
  124. render->info.h264.field_order_cnt[i] = foc;
  125. }
  126. render->info.h264.is_reference = (s->current_picture_ptr->reference & 3) ? VDP_TRUE : VDP_FALSE;
  127. render->info.h264.frame_num = h->frame_num;
  128. render->info.h264.field_pic_flag = s->picture_structure != PICT_FRAME;
  129. render->info.h264.bottom_field_flag = s->picture_structure == PICT_BOTTOM_FIELD;
  130. render->info.h264.num_ref_frames = h->sps.ref_frame_count;
  131. render->info.h264.mb_adaptive_frame_field_flag = h->sps.mb_aff && !render->info.h264.field_pic_flag;
  132. render->info.h264.constrained_intra_pred_flag = h->pps.constrained_intra_pred;
  133. render->info.h264.weighted_pred_flag = h->pps.weighted_pred;
  134. render->info.h264.weighted_bipred_idc = h->pps.weighted_bipred_idc;
  135. render->info.h264.frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
  136. render->info.h264.transform_8x8_mode_flag = h->pps.transform_8x8_mode;
  137. render->info.h264.chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
  138. render->info.h264.second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
  139. render->info.h264.pic_init_qp_minus26 = h->pps.init_qp - 26;
  140. render->info.h264.num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
  141. render->info.h264.num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
  142. render->info.h264.log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
  143. render->info.h264.pic_order_cnt_type = h->sps.poc_type;
  144. render->info.h264.log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
  145. render->info.h264.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
  146. render->info.h264.direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
  147. render->info.h264.entropy_coding_mode_flag = h->pps.cabac;
  148. render->info.h264.pic_order_present_flag = h->pps.pic_order_present;
  149. render->info.h264.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
  150. render->info.h264.redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
  151. memcpy(render->info.h264.scaling_lists_4x4, h->pps.scaling_matrix4, sizeof(render->info.h264.scaling_lists_4x4));
  152. memcpy(render->info.h264.scaling_lists_8x8, h->pps.scaling_matrix8, sizeof(render->info.h264.scaling_lists_8x8));
  153. ff_draw_horiz_band(s, 0, s->avctx->height);
  154. render->bitstream_buffers_used = 0;
  155. }
  156. void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
  157. int buf_size, int slice_count)
  158. {
  159. struct vdpau_render_state *render, *last, *next;
  160. int i;
  161. if (!s->current_picture_ptr) return;
  162. render = (struct vdpau_render_state *)s->current_picture_ptr->data[0];
  163. assert(render);
  164. /* fill VdpPictureInfoMPEG1Or2 struct */
  165. render->info.mpeg.picture_structure = s->picture_structure;
  166. render->info.mpeg.picture_coding_type = s->pict_type;
  167. render->info.mpeg.intra_dc_precision = s->intra_dc_precision;
  168. render->info.mpeg.frame_pred_frame_dct = s->frame_pred_frame_dct;
  169. render->info.mpeg.concealment_motion_vectors = s->concealment_motion_vectors;
  170. render->info.mpeg.intra_vlc_format = s->intra_vlc_format;
  171. render->info.mpeg.alternate_scan = s->alternate_scan;
  172. render->info.mpeg.q_scale_type = s->q_scale_type;
  173. render->info.mpeg.top_field_first = s->top_field_first;
  174. render->info.mpeg.full_pel_forward_vector = s->full_pel[0]; // MPEG-1 only. Set 0 for MPEG-2
  175. render->info.mpeg.full_pel_backward_vector = s->full_pel[1]; // MPEG-1 only. Set 0 for MPEG-2
  176. render->info.mpeg.f_code[0][0] = s->mpeg_f_code[0][0]; // For MPEG-1 fill both horiz. & vert.
  177. render->info.mpeg.f_code[0][1] = s->mpeg_f_code[0][1];
  178. render->info.mpeg.f_code[1][0] = s->mpeg_f_code[1][0];
  179. render->info.mpeg.f_code[1][1] = s->mpeg_f_code[1][1];
  180. for (i = 0; i < 64; ++i) {
  181. render->info.mpeg.intra_quantizer_matrix[i] = s->intra_matrix[i];
  182. render->info.mpeg.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
  183. }
  184. render->info.mpeg.forward_reference = VDP_INVALID_HANDLE;
  185. render->info.mpeg.backward_reference = VDP_INVALID_HANDLE;
  186. switch(s->pict_type){
  187. case FF_B_TYPE:
  188. next = (struct vdpau_render_state *)s->next_picture.data[0];
  189. assert(next);
  190. render->info.mpeg.backward_reference = next->surface;
  191. // no return here, going to set forward prediction
  192. case FF_P_TYPE:
  193. last = (struct vdpau_render_state *)s->last_picture.data[0];
  194. if (!last) // FIXME: Does this test make sense?
  195. last = render; // predict second field from the first
  196. render->info.mpeg.forward_reference = last->surface;
  197. }
  198. ff_vdpau_add_data_chunk(s, buf, buf_size);
  199. render->info.mpeg.slice_count = slice_count;
  200. if (slice_count)
  201. ff_draw_horiz_band(s, 0, s->avctx->height);
  202. render->bitstream_buffers_used = 0;
  203. }
  204. void ff_vdpau_vc1_decode_picture(MpegEncContext *s, const uint8_t *buf,
  205. int buf_size)
  206. {
  207. VC1Context *v = s->avctx->priv_data;
  208. struct vdpau_render_state *render, *last, *next;
  209. render = (struct vdpau_render_state *)s->current_picture.data[0];
  210. assert(render);
  211. /* fill LvPictureInfoVC1 struct */
  212. render->info.vc1.frame_coding_mode = v->fcm;
  213. render->info.vc1.postprocflag = v->postprocflag;
  214. render->info.vc1.pulldown = v->broadcast;
  215. render->info.vc1.interlace = v->interlace;
  216. render->info.vc1.tfcntrflag = v->tfcntrflag;
  217. render->info.vc1.finterpflag = v->finterpflag;
  218. render->info.vc1.psf = v->psf;
  219. render->info.vc1.dquant = v->dquant;
  220. render->info.vc1.panscan_flag = v->panscanflag;
  221. render->info.vc1.refdist_flag = v->refdist_flag;
  222. render->info.vc1.quantizer = v->quantizer_mode;
  223. render->info.vc1.extended_mv = v->extended_mv;
  224. render->info.vc1.extended_dmv = v->extended_dmv;
  225. render->info.vc1.overlap = v->overlap;
  226. render->info.vc1.vstransform = v->vstransform;
  227. render->info.vc1.loopfilter = v->s.loop_filter;
  228. render->info.vc1.fastuvmc = v->fastuvmc;
  229. render->info.vc1.range_mapy_flag = v->range_mapy_flag;
  230. render->info.vc1.range_mapy = v->range_mapy;
  231. render->info.vc1.range_mapuv_flag = v->range_mapuv_flag;
  232. render->info.vc1.range_mapuv = v->range_mapuv;
  233. /* Specific to simple/main profile only */
  234. render->info.vc1.multires = v->multires;
  235. render->info.vc1.syncmarker = v->s.resync_marker;
  236. render->info.vc1.rangered = v->rangered | (v->rangeredfrm << 1);
  237. render->info.vc1.maxbframes = v->s.max_b_frames;
  238. render->info.vc1.deblockEnable = v->postprocflag & 1;
  239. render->info.vc1.pquant = v->pq;
  240. render->info.vc1.forward_reference = VDP_INVALID_HANDLE;
  241. render->info.vc1.backward_reference = VDP_INVALID_HANDLE;
  242. if (v->bi_type)
  243. render->info.vc1.picture_type = 4;
  244. else
  245. render->info.vc1.picture_type = s->pict_type - 1 + s->pict_type / 3;
  246. switch(s->pict_type){
  247. case FF_B_TYPE:
  248. next = (struct vdpau_render_state *)s->next_picture.data[0];
  249. assert(next);
  250. render->info.vc1.backward_reference = next->surface;
  251. // no break here, going to set forward prediction
  252. case FF_P_TYPE:
  253. last = (struct vdpau_render_state *)s->last_picture.data[0];
  254. if (!last) // FIXME: Does this test make sense?
  255. last = render; // predict second field from the first
  256. render->info.vc1.forward_reference = last->surface;
  257. }
  258. ff_vdpau_add_data_chunk(s, buf, buf_size);
  259. render->info.vc1.slice_count = 1;
  260. ff_draw_horiz_band(s, 0, s->avctx->height);
  261. render->bitstream_buffers_used = 0;
  262. }
  263. /* @}*/