1 /**************************************************************************
3 * Copyright 2013 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Christian König <christian.koenig@amd.com>
36 #include "pipe/p_video_codec.h"
38 #include "util/u_video.h"
39 #include "util/u_memory.h"
41 #include "vl/vl_video_buffer.h"
43 #include "r600_pipe_common.h"
44 #include "radeon_video.h"
45 #include "radeon_vce.h"
48 * flush commands to the hardware
50 static void flush(struct rvce_encoder
*enc
)
52 enc
->ws
->cs_flush(enc
->cs
, RADEON_FLUSH_ASYNC
, NULL
, 0);
56 static void dump_feedback(struct rvce_encoder
*enc
, struct rvid_buffer
*fb
)
58 uint32_t *ptr
= enc
->ws
->buffer_map(fb
->res
->cs_buf
, enc
->cs
, PIPE_TRANSFER_READ_WRITE
);
60 fprintf(stderr
, "\n");
61 fprintf(stderr
, "encStatus:\t\t\t%08x\n", ptr
[i
++]);
62 fprintf(stderr
, "encHasBitstream:\t\t%08x\n", ptr
[i
++]);
63 fprintf(stderr
, "encHasAudioBitstream:\t\t%08x\n", ptr
[i
++]);
64 fprintf(stderr
, "encBitstreamOffset:\t\t%08x\n", ptr
[i
++]);
65 fprintf(stderr
, "encBitstreamSize:\t\t%08x\n", ptr
[i
++]);
66 fprintf(stderr
, "encAudioBitstreamOffset:\t%08x\n", ptr
[i
++]);
67 fprintf(stderr
, "encAudioBitstreamSize:\t\t%08x\n", ptr
[i
++]);
68 fprintf(stderr
, "encExtrabytes:\t\t\t%08x\n", ptr
[i
++]);
69 fprintf(stderr
, "encAudioExtrabytes:\t\t%08x\n", ptr
[i
++]);
70 fprintf(stderr
, "videoTimeStamp:\t\t\t%08x\n", ptr
[i
++]);
71 fprintf(stderr
, "audioTimeStamp:\t\t\t%08x\n", ptr
[i
++]);
72 fprintf(stderr
, "videoOutputType:\t\t%08x\n", ptr
[i
++]);
73 fprintf(stderr
, "attributeFlags:\t\t\t%08x\n", ptr
[i
++]);
74 fprintf(stderr
, "seiPrivatePackageOffset:\t%08x\n", ptr
[i
++]);
75 fprintf(stderr
, "seiPrivatePackageSize:\t\t%08x\n", ptr
[i
++]);
76 fprintf(stderr
, "\n");
77 enc
->ws
->buffer_unmap(fb
->res
->cs_buf
);
82 * reset the CPB handling
84 static void reset_cpb(struct rvce_encoder
*enc
)
88 LIST_INITHEAD(&enc
->cpb_slots
);
89 for (i
= 0; i
< enc
->cpb_num
; ++i
) {
90 struct rvce_cpb_slot
*slot
= &enc
->cpb_array
[i
];
92 slot
->picture_type
= PIPE_H264_ENC_PICTURE_TYPE_SKIP
;
94 slot
->pic_order_cnt
= 0;
95 LIST_ADDTAIL(&slot
->list
, &enc
->cpb_slots
);
100 * sort l0 and l1 to the top of the list
102 static void sort_cpb(struct rvce_encoder
*enc
)
104 struct rvce_cpb_slot
*i
, *l0
= NULL
, *l1
= NULL
;
106 LIST_FOR_EACH_ENTRY(i
, &enc
->cpb_slots
, list
) {
107 if (i
->frame_num
== enc
->pic
.ref_idx_l0
)
110 if (i
->frame_num
== enc
->pic
.ref_idx_l1
)
113 if (enc
->pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_P
&& l0
)
116 if (enc
->pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_B
&&
123 LIST_ADD(&l1
->list
, &enc
->cpb_slots
);
128 LIST_ADD(&l0
->list
, &enc
->cpb_slots
);
133 * get number of cpbs based on dpb
135 static unsigned get_cpb_num(struct rvce_encoder
*enc
)
137 unsigned w
= align(enc
->base
.width
, 16) / 16;
138 unsigned h
= align(enc
->base
.height
, 16) / 16;
141 switch (enc
->base
.level
) {
182 return MIN2(dpb
/ (w
* h
), 16);
186 * destroy this video encoder
188 static void rvce_destroy(struct pipe_video_codec
*encoder
)
190 struct rvce_encoder
*enc
= (struct rvce_encoder
*)encoder
;
191 if (enc
->stream_handle
) {
192 struct rvid_buffer fb
;
193 rvid_create_buffer(enc
->screen
, &fb
, 512, PIPE_USAGE_STAGING
);
199 rvid_destroy_buffer(&fb
);
201 rvid_destroy_buffer(&enc
->cpb
);
202 enc
->ws
->cs_destroy(enc
->cs
);
203 FREE(enc
->cpb_array
);
207 static void rvce_begin_frame(struct pipe_video_codec
*encoder
,
208 struct pipe_video_buffer
*source
,
209 struct pipe_picture_desc
*picture
)
211 struct rvce_encoder
*enc
= (struct rvce_encoder
*)encoder
;
212 struct vl_video_buffer
*vid_buf
= (struct vl_video_buffer
*)source
;
213 struct pipe_h264_enc_picture_desc
*pic
= (struct pipe_h264_enc_picture_desc
*)picture
;
215 bool need_rate_control
=
216 enc
->pic
.rate_ctrl
.rate_ctrl_method
!= pic
->rate_ctrl
.rate_ctrl_method
||
217 enc
->pic
.quant_i_frames
!= pic
->quant_i_frames
||
218 enc
->pic
.quant_p_frames
!= pic
->quant_p_frames
||
219 enc
->pic
.quant_b_frames
!= pic
->quant_b_frames
;
223 enc
->get_buffer(vid_buf
->resources
[0], &enc
->handle
, &enc
->luma
);
224 enc
->get_buffer(vid_buf
->resources
[1], NULL
, &enc
->chroma
);
226 if (pic
->picture_type
== PIPE_H264_ENC_PICTURE_TYPE_IDR
)
228 else if (pic
->picture_type
== PIPE_H264_ENC_PICTURE_TYPE_P
||
229 pic
->picture_type
== PIPE_H264_ENC_PICTURE_TYPE_B
)
232 if (!enc
->stream_handle
) {
233 struct rvid_buffer fb
;
234 enc
->stream_handle
= rvid_alloc_stream_handle();
235 rvid_create_buffer(enc
->screen
, &fb
, 512, PIPE_USAGE_STAGING
);
239 enc
->rate_control(enc
);
240 need_rate_control
= false;
241 enc
->config_extension(enc
);
242 enc
->motion_estimation(enc
);
246 enc
->pic_control(enc
);
249 //dump_feedback(enc, &fb);
250 rvid_destroy_buffer(&fb
);
255 if (need_rate_control
)
256 enc
->rate_control(enc
);
259 static void rvce_encode_bitstream(struct pipe_video_codec
*encoder
,
260 struct pipe_video_buffer
*source
,
261 struct pipe_resource
*destination
,
264 struct rvce_encoder
*enc
= (struct rvce_encoder
*)encoder
;
265 enc
->get_buffer(destination
, &enc
->bs_handle
, NULL
);
266 enc
->bs_size
= destination
->width0
;
268 *fb
= enc
->fb
= CALLOC_STRUCT(rvid_buffer
);
269 if (!rvid_create_buffer(enc
->screen
, enc
->fb
, 512, PIPE_USAGE_STAGING
)) {
270 RVID_ERR("Can't create feedback buffer.\n");
277 static void rvce_end_frame(struct pipe_video_codec
*encoder
,
278 struct pipe_video_buffer
*source
,
279 struct pipe_picture_desc
*picture
)
281 struct rvce_encoder
*enc
= (struct rvce_encoder
*)encoder
;
282 struct rvce_cpb_slot
*slot
= LIST_ENTRY(
283 struct rvce_cpb_slot
, enc
->cpb_slots
.prev
, list
);
287 /* update the CPB backtrack with the just encoded frame */
288 slot
->picture_type
= enc
->pic
.picture_type
;
289 slot
->frame_num
= enc
->pic
.frame_num
;
290 slot
->pic_order_cnt
= enc
->pic
.pic_order_cnt
;
291 if (!enc
->pic
.not_referenced
) {
292 LIST_DEL(&slot
->list
);
293 LIST_ADD(&slot
->list
, &enc
->cpb_slots
);
297 static void rvce_get_feedback(struct pipe_video_codec
*encoder
,
298 void *feedback
, unsigned *size
)
300 struct rvce_encoder
*enc
= (struct rvce_encoder
*)encoder
;
301 struct rvid_buffer
*fb
= feedback
;
304 uint32_t *ptr
= enc
->ws
->buffer_map(fb
->res
->cs_buf
, enc
->cs
, PIPE_TRANSFER_READ_WRITE
);
307 *size
= ptr
[4] - ptr
[9];
312 enc
->ws
->buffer_unmap(fb
->res
->cs_buf
);
314 //dump_feedback(enc, fb);
315 rvid_destroy_buffer(fb
);
320 * flush any outstanding command buffers to the hardware
322 static void rvce_flush(struct pipe_video_codec
*encoder
)
326 static void rvce_cs_flush(void *ctx
, unsigned flags
,
327 struct pipe_fence_handle
**fence
)
332 struct pipe_video_codec
*rvce_create_encoder(struct pipe_context
*context
,
333 const struct pipe_video_codec
*templ
,
334 struct radeon_winsys
* ws
,
335 rvce_get_buffer get_buffer
)
337 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)context
->screen
;
338 struct rvce_encoder
*enc
;
339 struct pipe_video_buffer
*tmp_buf
, templat
= {};
340 struct radeon_surf
*tmp_surf
;
343 if (!rscreen
->info
.vce_fw_version
) {
344 RVID_ERR("Kernel doesn't supports VCE!\n");
347 } else if (!rvce_is_fw_version_supported(rscreen
)) {
348 RVID_ERR("Unsupported VCE fw version loaded!\n");
352 enc
= CALLOC_STRUCT(rvce_encoder
);
356 if ((rscreen
->info
.drm_major
> 2) || (rscreen
->info
.drm_minor
>= 42))
360 enc
->base
.context
= context
;
362 enc
->base
.destroy
= rvce_destroy
;
363 enc
->base
.begin_frame
= rvce_begin_frame
;
364 enc
->base
.encode_bitstream
= rvce_encode_bitstream
;
365 enc
->base
.end_frame
= rvce_end_frame
;
366 enc
->base
.flush
= rvce_flush
;
367 enc
->base
.get_feedback
= rvce_get_feedback
;
368 enc
->get_buffer
= get_buffer
;
370 enc
->screen
= context
->screen
;
372 enc
->cs
= ws
->cs_create(ws
, RING_VCE
, rvce_cs_flush
, enc
, NULL
);
374 RVID_ERR("Can't get command submission context.\n");
378 templat
.buffer_format
= PIPE_FORMAT_NV12
;
379 templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
380 templat
.width
= enc
->base
.width
;
381 templat
.height
= enc
->base
.height
;
382 templat
.interlaced
= false;
383 if (!(tmp_buf
= context
->create_video_buffer(context
, &templat
))) {
384 RVID_ERR("Can't create video buffer.\n");
388 enc
->cpb_num
= get_cpb_num(enc
);
392 get_buffer(((struct vl_video_buffer
*)tmp_buf
)->resources
[0], NULL
, &tmp_surf
);
393 cpb_size
= align(tmp_surf
->level
[0].pitch_bytes
, 128);
394 cpb_size
= cpb_size
* align(tmp_surf
->npix_y
, 16);
395 cpb_size
= cpb_size
* 3 / 2;
396 cpb_size
= cpb_size
* enc
->cpb_num
;
397 tmp_buf
->destroy(tmp_buf
);
398 if (!rvid_create_buffer(enc
->screen
, &enc
->cpb
, cpb_size
, PIPE_USAGE_DEFAULT
)) {
399 RVID_ERR("Can't create CPB buffer.\n");
403 enc
->cpb_array
= CALLOC(enc
->cpb_num
, sizeof(struct rvce_cpb_slot
));
409 radeon_vce_40_2_2_init(enc
);
415 enc
->ws
->cs_destroy(enc
->cs
);
417 rvid_destroy_buffer(&enc
->cpb
);
419 FREE(enc
->cpb_array
);
425 * check if kernel has the right fw version loaded
427 bool rvce_is_fw_version_supported(struct r600_common_screen
*rscreen
)
429 return rscreen
->info
.vce_fw_version
== ((40 << 24) | (2 << 16) | (2 << 8));